MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / serial / keyspan.c
blob7b4f98407b4bed2fabe557ec0c96ea4f5317ccce
1 /*
2 Keyspan USB to Serial Converter driver
4 (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
5 (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 See http://misc.nu/hugh/keyspan.html for more information.
14 Code in this driver inspired by and in a number of places taken
15 from Brian Warner's original Keyspan-PDA driver.
17 This driver has been put together with the support of Innosys, Inc.
18 and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
19 Thanks Guys :)
21 Thanks to Paulus for miscellaneous tidy ups, some largish chunks
22 of much nicer and/or completely new code and (perhaps most uniquely)
23 having the patience to sit down and explain why and where he'd changed
24 stuff.
26 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
27 staff in their work on open source projects.
29 Change History
31 2003sep04 LPM (Keyspan) add support for new single port product USA19HS.
32 Improve setup message handling for all devices.
34 Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
35 Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
36 Linux source tree. The Linux tree lacked support for the 49WLC and
37 others. The Keyspan patches didn't work with the current kernel.
39 2003jan30 LPM add support for the 49WLC and MPR
41 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
42 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
43 now supported (including QI and QW). Modified port open, port
44 close, and send setup() logic to fix various data and endpoint
45 synchronization bugs and device LED status bugs. Changed keyspan_
46 write_room() to accurately return transmit buffer availability.
47 Changed forwardingLength from 1 to 16 for all adapters.
49 Fri Oct 12 16:45:00 EST 2001
50 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
52 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
53 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
54 now supported (including QI and QW). Modified port open, port
55 close, and send setup() logic to fix various data and endpoint
56 synchronization bugs and device LED status bugs. Changed keyspan_
57 write_room() to accurately return transmit buffer availability.
58 Changed forwardingLength from 1 to 16 for all adapters.
60 Fri Oct 12 16:45:00 EST 2001
61 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
63 Mon Oct 8 14:29:00 EST 2001 hugh
64 Fixed bug that prevented mulitport devices operating correctly
65 if they weren't the first unit attached.
67 Sat Oct 6 12:31:21 EST 2001 hugh
68 Added support for USA-28XA and -28XB, misc cleanups, break support
69 for usa26 based models thanks to David Gibson.
71 Thu May 31 11:56:42 PDT 2001 gkh
72 switched from using spinlock to a semaphore
74 (04/08/2001) gb
75 Identify version on module load.
77 (11/01/2000) Adam J. Richter
78 usb_device_id table support.
80 Tue Oct 10 23:15:33 EST 2000 Hugh
81 Merged Paul's changes with my USA-49W mods. Work in progress
82 still...
84 Wed Jul 19 14:00:42 EST 2000 gkh
85 Added module_init and module_exit functions to handle the fact that
86 this driver is a loadable module now.
88 Tue Jul 18 16:14:52 EST 2000 Hugh
89 Basic character input/output for USA-19 now mostly works,
90 fixed at 9600 baud for the moment.
92 Sat Jul 8 11:11:48 EST 2000 Hugh
93 First public release - nothing works except the firmware upload.
94 Tested on PPC and x86 architectures, seems to behave...
98 #include <linux/config.h>
99 #include <linux/kernel.h>
100 #include <linux/jiffies.h>
101 #include <linux/errno.h>
102 #include <linux/init.h>
103 #include <linux/slab.h>
104 #include <linux/tty.h>
105 #include <linux/tty_driver.h>
106 #include <linux/tty_flip.h>
107 #include <linux/module.h>
108 #include <linux/spinlock.h>
109 #include <asm/uaccess.h>
110 #include <linux/usb.h>
111 #include "usb-serial.h"
112 #include "keyspan.h"
114 static int debug;
117 * Version Information
119 #define DRIVER_VERSION "v1.1.4"
120 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
121 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
123 #define INSTAT_BUFLEN 32
124 #define GLOCONT_BUFLEN 64
126 /* Per device and per port private data */
127 struct keyspan_serial_private {
128 const struct keyspan_device_details *device_details;
130 struct urb *instat_urb;
131 char instat_buf[INSTAT_BUFLEN];
133 /* XXX this one probably will need a lock */
134 struct urb *glocont_urb;
135 char glocont_buf[GLOCONT_BUFLEN];
138 struct keyspan_port_private {
139 /* Keep track of which input & output endpoints to use */
140 int in_flip;
141 int out_flip;
143 /* Keep duplicate of device details in each port
144 structure as well - simplifies some of the
145 callback functions etc. */
146 const struct keyspan_device_details *device_details;
148 /* Input endpoints and buffer for this port */
149 struct urb *in_urbs[2];
150 char in_buffer[2][64];
151 /* Output endpoints and buffer for this port */
152 struct urb *out_urbs[2];
153 char out_buffer[2][64];
155 /* Input ack endpoint */
156 struct urb *inack_urb;
157 char inack_buffer[1];
159 /* Output control endpoint */
160 struct urb *outcont_urb;
161 char outcont_buffer[64];
163 /* Settings for the port */
164 int baud;
165 int old_baud;
166 unsigned int cflag;
167 unsigned int old_cflag;
168 enum {flow_none, flow_cts, flow_xon} flow_control;
169 int rts_state; /* Handshaking pins (outputs) */
170 int dtr_state;
171 int cts_state; /* Handshaking pins (inputs) */
172 int dsr_state;
173 int dcd_state;
174 int ri_state;
175 int break_on;
177 unsigned long tx_start_time[2];
178 int resend_cont; /* need to resend control packet */
182 /* Include Keyspan message headers. All current Keyspan Adapters
183 make use of one of four message formats which are referred
184 to as USA-26, USA-28 and USA-49, USA-90 by Keyspan and within this driver. */
185 #include "keyspan_usa26msg.h"
186 #include "keyspan_usa28msg.h"
187 #include "keyspan_usa49msg.h"
188 #include "keyspan_usa90msg.h"
191 /* Functions used by new usb-serial code. */
192 static int __init keyspan_init (void)
194 int retval;
195 retval = usb_serial_register(&keyspan_pre_device);
196 if (retval)
197 goto failed_pre_device_register;
198 retval = usb_serial_register(&keyspan_1port_device);
199 if (retval)
200 goto failed_1port_device_register;
201 retval = usb_serial_register(&keyspan_2port_device);
202 if (retval)
203 goto failed_2port_device_register;
204 retval = usb_serial_register(&keyspan_4port_device);
205 if (retval)
206 goto failed_4port_device_register;
207 retval = usb_register(&keyspan_driver);
208 if (retval)
209 goto failed_usb_register;
211 info(DRIVER_VERSION ":" DRIVER_DESC);
213 return 0;
214 failed_usb_register:
215 usb_serial_deregister(&keyspan_4port_device);
216 failed_4port_device_register:
217 usb_serial_deregister(&keyspan_2port_device);
218 failed_2port_device_register:
219 usb_serial_deregister(&keyspan_1port_device);
220 failed_1port_device_register:
221 usb_serial_deregister(&keyspan_pre_device);
222 failed_pre_device_register:
223 return retval;
226 static void __exit keyspan_exit (void)
228 usb_deregister (&keyspan_driver);
229 usb_serial_deregister (&keyspan_pre_device);
230 usb_serial_deregister (&keyspan_1port_device);
231 usb_serial_deregister (&keyspan_2port_device);
232 usb_serial_deregister (&keyspan_4port_device);
235 module_init(keyspan_init);
236 module_exit(keyspan_exit);
238 static void keyspan_rx_throttle (struct usb_serial_port *port)
240 dbg("%s - port %d", __FUNCTION__, port->number);
244 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
246 dbg("%s - port %d", __FUNCTION__, port->number);
250 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
252 struct keyspan_port_private *p_priv;
254 dbg("%s", __FUNCTION__);
256 p_priv = usb_get_serial_port_data(port);
258 if (break_state == -1)
259 p_priv->break_on = 1;
260 else
261 p_priv->break_on = 0;
263 keyspan_send_setup(port, 0);
267 static void keyspan_set_termios (struct usb_serial_port *port,
268 struct termios *old_termios)
270 int baud_rate, device_port;
271 struct keyspan_port_private *p_priv;
272 const struct keyspan_device_details *d_details;
273 unsigned int cflag;
275 dbg("%s", __FUNCTION__);
277 p_priv = usb_get_serial_port_data(port);
278 d_details = p_priv->device_details;
279 cflag = port->tty->termios->c_cflag;
280 device_port = port->number - port->serial->minor;
282 /* Baud rate calculation takes baud rate as an integer
283 so other rates can be generated if desired. */
284 baud_rate = tty_get_baud_rate(port->tty);
285 /* If no match or invalid, don't change */
286 if (baud_rate >= 0
287 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
288 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
289 /* FIXME - more to do here to ensure rate changes cleanly */
290 p_priv->baud = baud_rate;
293 /* set CTS/RTS handshake etc. */
294 p_priv->cflag = cflag;
295 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
297 keyspan_send_setup(port, 0);
300 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
302 unsigned int value;
303 struct keyspan_port_private *p_priv;
305 p_priv = usb_get_serial_port_data(port);
307 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
308 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
309 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
310 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
311 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
312 ((p_priv->ri_state) ? TIOCM_RNG : 0);
314 return value;
317 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
318 unsigned int set, unsigned int clear)
320 struct keyspan_port_private *p_priv;
322 p_priv = usb_get_serial_port_data(port);
324 if (set & TIOCM_RTS)
325 p_priv->rts_state = 1;
326 if (set & TIOCM_DTR)
327 p_priv->dtr_state = 1;
329 if (clear & TIOCM_RTS)
330 p_priv->rts_state = 0;
331 if (clear & TIOCM_DTR)
332 p_priv->dtr_state = 0;
333 keyspan_send_setup(port, 0);
334 return 0;
337 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
338 unsigned int cmd, unsigned long arg)
340 return -ENOIOCTLCMD;
343 /* Write function is similar for the four protocols used
344 with only a minor change for usa90 (usa19hs) required */
345 static int keyspan_write(struct usb_serial_port *port, int from_user,
346 const unsigned char *buf, int count)
348 struct keyspan_port_private *p_priv;
349 const struct keyspan_device_details *d_details;
350 int flip;
351 int left, todo;
352 struct urb *this_urb;
353 int err, maxDataLen, dataOffset;
355 p_priv = usb_get_serial_port_data(port);
356 d_details = p_priv->device_details;
358 if (d_details->msg_format == msg_usa90) {
359 maxDataLen = 64;
360 dataOffset = 0;
361 } else {
362 maxDataLen = 63;
363 dataOffset = 1;
366 dbg("%s - for port %d (%d chars), flip=%d",
367 __FUNCTION__, port->number, count, p_priv->out_flip);
369 for (left = count; left > 0; left -= todo) {
370 todo = left;
371 if (todo > maxDataLen)
372 todo = maxDataLen;
374 flip = p_priv->out_flip;
376 /* Check we have a valid urb/endpoint before we use it... */
377 if ((this_urb = p_priv->out_urbs[flip]) == 0) {
378 /* no bulk out, so return 0 bytes written */
379 dbg("%s - no output urb :(", __FUNCTION__);
380 return count;
383 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
385 if (this_urb->status == -EINPROGRESS) {
386 if (this_urb->transfer_flags & URB_ASYNC_UNLINK)
387 break;
388 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
389 break;
390 this_urb->transfer_flags |= URB_ASYNC_UNLINK;
391 usb_unlink_urb(this_urb);
392 break;
395 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
396 for now so set to zero */
397 ((char *)this_urb->transfer_buffer)[0] = 0;
399 if (from_user) {
400 if (copy_from_user(this_urb->transfer_buffer + dataOffset, buf, todo))
401 return -EFAULT;
402 } else {
403 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
405 buf += todo;
407 /* send the data out the bulk port */
408 this_urb->transfer_buffer_length = todo + dataOffset;
410 this_urb->transfer_flags &= ~URB_ASYNC_UNLINK;
411 this_urb->dev = port->serial->dev;
412 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
413 dbg("usb_submit_urb(write bulk) failed (%d)", err);
415 p_priv->tx_start_time[flip] = jiffies;
417 /* Flip for next time if usa26 or usa28 interface
418 (not used on usa49) */
419 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
422 return count - left;
425 static void usa26_indat_callback(struct urb *urb, struct pt_regs *regs)
427 int i, err;
428 int endpoint;
429 struct usb_serial_port *port;
430 struct tty_struct *tty;
431 unsigned char *data = urb->transfer_buffer;
433 dbg ("%s", __FUNCTION__);
435 endpoint = usb_pipeendpoint(urb->pipe);
437 if (urb->status) {
438 dbg("%s - nonzero status: %x on endpoint %d.",
439 __FUNCTION__, urb->status, endpoint);
440 return;
443 port = (struct usb_serial_port *) urb->context;
444 tty = port->tty;
445 if (urb->actual_length) {
446 /* 0x80 bit is error flag */
447 if ((data[0] & 0x80) == 0) {
448 /* no errors on individual bytes, only possible overrun err*/
449 if (data[0] & RXERROR_OVERRUN)
450 err = TTY_OVERRUN;
451 else err = 0;
452 for (i = 1; i < urb->actual_length ; ++i) {
453 tty_insert_flip_char(tty, data[i], err);
455 } else {
456 /* some bytes had errors, every byte has status */
457 dbg("%s - RX error!!!!", __FUNCTION__);
458 for (i = 0; i + 1 < urb->actual_length; i += 2) {
459 int stat = data[i], flag = 0;
460 if (stat & RXERROR_OVERRUN)
461 flag |= TTY_OVERRUN;
462 if (stat & RXERROR_FRAMING)
463 flag |= TTY_FRAME;
464 if (stat & RXERROR_PARITY)
465 flag |= TTY_PARITY;
466 /* XXX should handle break (0x10) */
467 tty_insert_flip_char(tty, data[i+1], flag);
470 tty_flip_buffer_push(tty);
473 /* Resubmit urb so we continue receiving */
474 urb->dev = port->serial->dev;
475 if (port->open_count)
476 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
477 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
479 return;
482 /* Outdat handling is common for all devices */
483 static void usa2x_outdat_callback(struct urb *urb, struct pt_regs *regs)
485 struct usb_serial_port *port;
486 struct keyspan_port_private *p_priv;
488 port = (struct usb_serial_port *) urb->context;
489 p_priv = usb_get_serial_port_data(port);
490 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
492 if (port->open_count)
493 schedule_work(&port->work);
496 static void usa26_inack_callback(struct urb *urb, struct pt_regs *regs)
498 dbg ("%s", __FUNCTION__);
502 static void usa26_outcont_callback(struct urb *urb, struct pt_regs *regs)
504 struct usb_serial_port *port;
505 struct keyspan_port_private *p_priv;
507 port = (struct usb_serial_port *) urb->context;
508 p_priv = usb_get_serial_port_data(port);
510 if (p_priv->resend_cont) {
511 dbg ("%s - sending setup", __FUNCTION__);
512 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
516 static void usa26_instat_callback(struct urb *urb, struct pt_regs *regs)
518 unsigned char *data = urb->transfer_buffer;
519 struct keyspan_usa26_portStatusMessage *msg;
520 struct usb_serial *serial;
521 struct usb_serial_port *port;
522 struct keyspan_port_private *p_priv;
523 int old_dcd_state, err;
525 serial = (struct usb_serial *) urb->context;
527 if (urb->status) {
528 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
529 return;
531 if (urb->actual_length != 9) {
532 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
533 goto exit;
536 msg = (struct keyspan_usa26_portStatusMessage *)data;
538 #if 0
539 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
540 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
541 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
542 #endif
544 /* Now do something useful with the data */
547 /* Check port number from message and retrieve private data */
548 if (msg->port >= serial->num_ports) {
549 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
550 goto exit;
552 port = serial->port[msg->port];
553 p_priv = usb_get_serial_port_data(port);
555 /* Update handshaking pin state information */
556 old_dcd_state = p_priv->dcd_state;
557 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
558 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
559 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
560 p_priv->ri_state = ((msg->ri) ? 1 : 0);
562 if (port->tty && !C_CLOCAL(port->tty)
563 && old_dcd_state != p_priv->dcd_state) {
564 if (old_dcd_state)
565 tty_hangup(port->tty);
566 /* else */
567 /* wake_up_interruptible(&p_priv->open_wait); */
570 /* Resubmit urb so we continue receiving */
571 urb->dev = serial->dev;
572 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
573 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
575 exit: ;
578 static void usa26_glocont_callback(struct urb *urb, struct pt_regs *regs)
580 dbg ("%s", __FUNCTION__);
585 static void usa28_indat_callback(struct urb *urb, struct pt_regs *regs)
587 int i, err;
588 struct usb_serial_port *port;
589 struct tty_struct *tty;
590 unsigned char *data;
591 struct keyspan_port_private *p_priv;
593 dbg ("%s", __FUNCTION__);
595 port = (struct usb_serial_port *) urb->context;
596 p_priv = usb_get_serial_port_data(port);
597 data = urb->transfer_buffer;
599 if (urb != p_priv->in_urbs[p_priv->in_flip])
600 return;
602 do {
603 if (urb->status) {
604 dbg("%s - nonzero status: %x on endpoint %d.",
605 __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
606 return;
609 port = (struct usb_serial_port *) urb->context;
610 p_priv = usb_get_serial_port_data(port);
611 data = urb->transfer_buffer;
613 tty = port->tty;
614 if (urb->actual_length) {
615 for (i = 0; i < urb->actual_length ; ++i) {
616 tty_insert_flip_char(tty, data[i], 0);
618 tty_flip_buffer_push(tty);
621 /* Resubmit urb so we continue receiving */
622 urb->dev = port->serial->dev;
623 if (port->open_count)
624 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
625 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
627 p_priv->in_flip ^= 1;
629 urb = p_priv->in_urbs[p_priv->in_flip];
630 } while (urb->status != -EINPROGRESS);
633 static void usa28_inack_callback(struct urb *urb, struct pt_regs *regs)
635 dbg ("%s", __FUNCTION__);
638 static void usa28_outcont_callback(struct urb *urb, struct pt_regs *regs)
640 struct usb_serial_port *port;
641 struct keyspan_port_private *p_priv;
643 port = (struct usb_serial_port *) urb->context;
644 p_priv = usb_get_serial_port_data(port);
646 if (p_priv->resend_cont) {
647 dbg ("%s - sending setup", __FUNCTION__);
648 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
652 static void usa28_instat_callback(struct urb *urb, struct pt_regs *regs)
654 int err;
655 unsigned char *data = urb->transfer_buffer;
656 struct keyspan_usa28_portStatusMessage *msg;
657 struct usb_serial *serial;
658 struct usb_serial_port *port;
659 struct keyspan_port_private *p_priv;
660 int old_dcd_state;
662 serial = (struct usb_serial *) urb->context;
664 if (urb->status) {
665 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
666 return;
669 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
670 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
671 goto exit;
674 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
675 data[0], data[1], data[2], data[3], data[4], data[5],
676 data[6], data[7], data[8], data[9], data[10], data[11]);*/
678 /* Now do something useful with the data */
679 msg = (struct keyspan_usa28_portStatusMessage *)data;
682 /* Check port number from message and retrieve private data */
683 if (msg->port >= serial->num_ports) {
684 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
685 goto exit;
687 port = serial->port[msg->port];
688 p_priv = usb_get_serial_port_data(port);
690 /* Update handshaking pin state information */
691 old_dcd_state = p_priv->dcd_state;
692 p_priv->cts_state = ((msg->cts) ? 1 : 0);
693 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
694 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
695 p_priv->ri_state = ((msg->ri) ? 1 : 0);
697 if (port->tty && !C_CLOCAL(port->tty)
698 && old_dcd_state != p_priv->dcd_state) {
699 if (old_dcd_state)
700 tty_hangup(port->tty);
701 /* else */
702 /* wake_up_interruptible(&p_priv->open_wait); */
705 /* Resubmit urb so we continue receiving */
706 urb->dev = serial->dev;
707 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
708 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
710 exit: ;
713 static void usa28_glocont_callback(struct urb *urb, struct pt_regs *regs)
715 dbg ("%s", __FUNCTION__);
719 static void usa49_glocont_callback(struct urb *urb, struct pt_regs *regs)
721 struct usb_serial *serial;
722 struct usb_serial_port *port;
723 struct keyspan_port_private *p_priv;
724 int i;
726 dbg ("%s", __FUNCTION__);
728 serial = (struct usb_serial *) urb->context;
729 for (i = 0; i < serial->num_ports; ++i) {
730 port = serial->port[i];
731 p_priv = usb_get_serial_port_data(port);
733 if (p_priv->resend_cont) {
734 dbg ("%s - sending setup", __FUNCTION__);
735 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
736 break;
741 /* This is actually called glostat in the Keyspan
742 doco */
743 static void usa49_instat_callback(struct urb *urb, struct pt_regs *regs)
745 int err;
746 unsigned char *data = urb->transfer_buffer;
747 struct keyspan_usa49_portStatusMessage *msg;
748 struct usb_serial *serial;
749 struct usb_serial_port *port;
750 struct keyspan_port_private *p_priv;
751 int old_dcd_state;
753 dbg ("%s", __FUNCTION__);
755 serial = (struct usb_serial *) urb->context;
757 if (urb->status) {
758 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
759 return;
762 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
763 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
764 goto exit;
767 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
768 data[0], data[1], data[2], data[3], data[4], data[5],
769 data[6], data[7], data[8], data[9], data[10]);*/
771 /* Now do something useful with the data */
772 msg = (struct keyspan_usa49_portStatusMessage *)data;
774 /* Check port number from message and retrieve private data */
775 if (msg->portNumber >= serial->num_ports) {
776 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
777 goto exit;
779 port = serial->port[msg->portNumber];
780 p_priv = usb_get_serial_port_data(port);
782 /* Update handshaking pin state information */
783 old_dcd_state = p_priv->dcd_state;
784 p_priv->cts_state = ((msg->cts) ? 1 : 0);
785 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
786 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
787 p_priv->ri_state = ((msg->ri) ? 1 : 0);
789 if (port->tty && !C_CLOCAL(port->tty)
790 && old_dcd_state != p_priv->dcd_state) {
791 if (old_dcd_state)
792 tty_hangup(port->tty);
793 /* else */
794 /* wake_up_interruptible(&p_priv->open_wait); */
797 /* Resubmit urb so we continue receiving */
798 urb->dev = serial->dev;
800 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
801 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
803 exit: ;
806 static void usa49_inack_callback(struct urb *urb, struct pt_regs *regs)
808 dbg ("%s", __FUNCTION__);
811 static void usa49_indat_callback(struct urb *urb, struct pt_regs *regs)
813 int i, err;
814 int endpoint;
815 struct usb_serial_port *port;
816 struct tty_struct *tty;
817 unsigned char *data = urb->transfer_buffer;
819 dbg ("%s", __FUNCTION__);
821 endpoint = usb_pipeendpoint(urb->pipe);
823 if (urb->status) {
824 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
825 urb->status, endpoint);
826 return;
829 port = (struct usb_serial_port *) urb->context;
830 tty = port->tty;
831 if (urb->actual_length) {
832 /* 0x80 bit is error flag */
833 if ((data[0] & 0x80) == 0) {
834 /* no error on any byte */
835 for (i = 1; i < urb->actual_length ; ++i) {
836 tty_insert_flip_char(tty, data[i], 0);
838 } else {
839 /* some bytes had errors, every byte has status */
840 for (i = 0; i + 1 < urb->actual_length; i += 2) {
841 int stat = data[i], flag = 0;
842 if (stat & RXERROR_OVERRUN)
843 flag |= TTY_OVERRUN;
844 if (stat & RXERROR_FRAMING)
845 flag |= TTY_FRAME;
846 if (stat & RXERROR_PARITY)
847 flag |= TTY_PARITY;
848 /* XXX should handle break (0x10) */
849 tty_insert_flip_char(tty, data[i+1], flag);
852 tty_flip_buffer_push(tty);
855 /* Resubmit urb so we continue receiving */
856 urb->dev = port->serial->dev;
857 if (port->open_count)
858 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
859 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
863 /* not used, usa-49 doesn't have per-port control endpoints */
864 static void usa49_outcont_callback(struct urb *urb, struct pt_regs *regs)
866 dbg ("%s", __FUNCTION__);
869 static void usa90_indat_callback(struct urb *urb, struct pt_regs *regs)
871 int i, err;
872 int endpoint;
873 struct usb_serial_port *port;
874 struct keyspan_port_private *p_priv;
875 struct tty_struct *tty;
876 unsigned char *data = urb->transfer_buffer;
878 dbg ("%s", __FUNCTION__);
880 endpoint = usb_pipeendpoint(urb->pipe);
883 if (urb->status) {
884 dbg("%s - nonzero status: %x on endpoint %d.",
885 __FUNCTION__, urb->status, endpoint);
886 return;
889 port = (struct usb_serial_port *) urb->context;
890 p_priv = usb_get_serial_port_data(port);
892 tty = port->tty;
893 if (urb->actual_length) {
895 /* if current mode is DMA, looks like usa28 format
896 otherwise looks like usa26 data format */
898 if (p_priv->baud > 57600) {
899 for (i = 0; i < urb->actual_length ; ++i)
900 tty_insert_flip_char(tty, data[i], 0);
902 else {
904 /* 0x80 bit is error flag */
905 if ((data[0] & 0x80) == 0) {
906 /* no errors on individual bytes, only possible overrun err*/
907 if (data[0] & RXERROR_OVERRUN)
908 err = TTY_OVERRUN;
909 else err = 0;
910 for (i = 1; i < urb->actual_length ; ++i)
911 tty_insert_flip_char(tty, data[i], err);
914 else {
915 /* some bytes had errors, every byte has status */
916 dbg("%s - RX error!!!!", __FUNCTION__);
917 for (i = 0; i + 1 < urb->actual_length; i += 2) {
918 int stat = data[i], flag = 0;
919 if (stat & RXERROR_OVERRUN)
920 flag |= TTY_OVERRUN;
921 if (stat & RXERROR_FRAMING)
922 flag |= TTY_FRAME;
923 if (stat & RXERROR_PARITY)
924 flag |= TTY_PARITY;
925 /* XXX should handle break (0x10) */
926 tty_insert_flip_char(tty, data[i+1], flag);
930 tty_flip_buffer_push(tty);
933 /* Resubmit urb so we continue receiving */
934 urb->dev = port->serial->dev;
935 if (port->open_count)
936 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
937 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
939 return;
943 static void usa90_instat_callback(struct urb *urb, struct pt_regs *regs)
945 unsigned char *data = urb->transfer_buffer;
946 struct keyspan_usa90_portStatusMessage *msg;
947 struct usb_serial *serial;
948 struct usb_serial_port *port;
949 struct keyspan_port_private *p_priv;
950 int old_dcd_state, err;
952 serial = (struct usb_serial *) urb->context;
954 if (urb->status) {
955 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
956 return;
958 if (urb->actual_length < 14) {
959 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
960 goto exit;
963 msg = (struct keyspan_usa90_portStatusMessage *)data;
965 /* Now do something useful with the data */
967 port = serial->port[0];
968 p_priv = usb_get_serial_port_data(port);
970 /* Update handshaking pin state information */
971 old_dcd_state = p_priv->dcd_state;
972 p_priv->cts_state = ((msg->cts) ? 1 : 0);
973 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
974 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
975 p_priv->ri_state = ((msg->ri) ? 1 : 0);
977 if (port->tty && !C_CLOCAL(port->tty)
978 && old_dcd_state != p_priv->dcd_state) {
979 if (old_dcd_state)
980 tty_hangup(port->tty);
981 /* else */
982 /* wake_up_interruptible(&p_priv->open_wait); */
985 /* Resubmit urb so we continue receiving */
986 urb->dev = serial->dev;
987 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
988 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
990 exit:
994 static void usa90_outcont_callback(struct urb *urb, struct pt_regs *regs)
996 struct usb_serial_port *port;
997 struct keyspan_port_private *p_priv;
999 port = (struct usb_serial_port *) urb->context;
1000 p_priv = usb_get_serial_port_data(port);
1002 if (p_priv->resend_cont) {
1003 dbg ("%s - sending setup", __FUNCTION__);
1004 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1008 static int keyspan_write_room (struct usb_serial_port *port)
1010 struct keyspan_port_private *p_priv;
1011 const struct keyspan_device_details *d_details;
1012 int flip;
1013 int data_len;
1014 struct urb *this_urb;
1016 dbg("%s", __FUNCTION__);
1017 p_priv = usb_get_serial_port_data(port);
1018 d_details = p_priv->device_details;
1020 if (d_details->msg_format == msg_usa90)
1021 data_len = 64;
1022 else
1023 data_len = 63;
1025 flip = p_priv->out_flip;
1027 /* Check both endpoints to see if any are available. */
1028 if ((this_urb = p_priv->out_urbs[flip]) != 0) {
1029 if (this_urb->status != -EINPROGRESS)
1030 return (data_len);
1031 flip = (flip + 1) & d_details->outdat_endp_flip;
1032 if ((this_urb = p_priv->out_urbs[flip]) != 0)
1033 if (this_urb->status != -EINPROGRESS)
1034 return (data_len);
1036 return (0);
1040 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1042 return (0);
1046 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1048 struct keyspan_port_private *p_priv;
1049 struct keyspan_serial_private *s_priv;
1050 struct usb_serial *serial = port->serial;
1051 const struct keyspan_device_details *d_details;
1052 int i, err;
1053 int baud_rate, device_port;
1054 struct urb *urb;
1055 unsigned int cflag;
1057 s_priv = usb_get_serial_data(serial);
1058 p_priv = usb_get_serial_port_data(port);
1059 d_details = p_priv->device_details;
1061 dbg("%s - port%d.", __FUNCTION__, port->number);
1063 /* Set some sane defaults */
1064 p_priv->rts_state = 1;
1065 p_priv->dtr_state = 1;
1066 p_priv->baud = 9600;
1068 /* force baud and lcr to be set on open */
1069 p_priv->old_baud = 0;
1070 p_priv->old_cflag = 0;
1072 p_priv->out_flip = 0;
1073 p_priv->in_flip = 0;
1075 /* Reset low level data toggle and start reading from endpoints */
1076 for (i = 0; i < 2; i++) {
1077 if ((urb = p_priv->in_urbs[i]) == NULL)
1078 continue;
1079 urb->dev = serial->dev;
1081 /* make sure endpoint data toggle is synchronized with the device */
1083 usb_clear_halt(urb->dev, urb->pipe);
1085 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1086 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1090 /* Reset low level data toggle on out endpoints */
1091 for (i = 0; i < 2; i++) {
1092 if ((urb = p_priv->out_urbs[i]) == NULL)
1093 continue;
1094 urb->dev = serial->dev;
1095 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1098 /* get the terminal config for the setup message now so we don't
1099 * need to send 2 of them */
1101 cflag = port->tty->termios->c_cflag;
1102 device_port = port->number - port->serial->minor;
1104 /* Baud rate calculation takes baud rate as an integer
1105 so other rates can be generated if desired. */
1106 baud_rate = tty_get_baud_rate(port->tty);
1107 /* If no match or invalid, leave as default */
1108 if (baud_rate >= 0
1109 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1110 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1111 p_priv->baud = baud_rate;
1114 /* set CTS/RTS handshake etc. */
1115 p_priv->cflag = cflag;
1116 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1118 keyspan_send_setup(port, 1);
1119 //mdelay(100);
1120 //keyspan_set_termios(port, NULL);
1122 return (0);
1125 static inline void stop_urb(struct urb *urb)
1127 if (urb && urb->status == -EINPROGRESS) {
1128 urb->transfer_flags &= ~URB_ASYNC_UNLINK;
1129 usb_unlink_urb(urb);
1133 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1135 int i;
1136 struct usb_serial *serial = port->serial;
1137 struct keyspan_serial_private *s_priv;
1138 struct keyspan_port_private *p_priv;
1140 dbg("%s", __FUNCTION__);
1141 s_priv = usb_get_serial_data(serial);
1142 p_priv = usb_get_serial_port_data(port);
1144 p_priv->rts_state = 0;
1145 p_priv->dtr_state = 0;
1147 if (serial->dev) {
1148 keyspan_send_setup(port, 2);
1149 /* pilot-xfer seems to work best with this delay */
1150 mdelay(100);
1151 // keyspan_set_termios(port, NULL);
1154 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1155 dbg("%s - urb in progress", __FUNCTION__);
1158 p_priv->out_flip = 0;
1159 p_priv->in_flip = 0;
1161 if (serial->dev) {
1162 /* Stop reading/writing urbs */
1163 stop_urb(p_priv->inack_urb);
1164 /* stop_urb(p_priv->outcont_urb); */
1165 for (i = 0; i < 2; i++) {
1166 stop_urb(p_priv->in_urbs[i]);
1167 stop_urb(p_priv->out_urbs[i]);
1170 port->tty = NULL;
1174 /* download the firmware to a pre-renumeration device */
1175 static int keyspan_fake_startup (struct usb_serial *serial)
1177 int response;
1178 const struct ezusb_hex_record *record;
1179 char *fw_name;
1181 dbg("Keyspan startup version %04x product %04x",
1182 serial->dev->descriptor.bcdDevice,
1183 serial->dev->descriptor.idProduct);
1185 if ((serial->dev->descriptor.bcdDevice & 0x8000) != 0x8000) {
1186 dbg("Firmware already loaded. Quitting.");
1187 return(1);
1190 /* Select firmware image on the basis of idProduct */
1191 switch (serial->dev->descriptor.idProduct) {
1192 case keyspan_usa28_pre_product_id:
1193 record = &keyspan_usa28_firmware[0];
1194 fw_name = "USA28";
1195 break;
1197 case keyspan_usa28x_pre_product_id:
1198 record = &keyspan_usa28x_firmware[0];
1199 fw_name = "USA28X";
1200 break;
1202 case keyspan_usa28xa_pre_product_id:
1203 record = &keyspan_usa28xa_firmware[0];
1204 fw_name = "USA28XA";
1205 break;
1207 case keyspan_usa28xb_pre_product_id:
1208 record = &keyspan_usa28xb_firmware[0];
1209 fw_name = "USA28XB";
1210 break;
1212 case keyspan_usa19_pre_product_id:
1213 record = &keyspan_usa19_firmware[0];
1214 fw_name = "USA19";
1215 break;
1217 case keyspan_usa19qi_pre_product_id:
1218 record = &keyspan_usa19qi_firmware[0];
1219 fw_name = "USA19QI";
1220 break;
1222 case keyspan_mpr_pre_product_id:
1223 record = &keyspan_mpr_firmware[0];
1224 fw_name = "MPR";
1225 break;
1227 case keyspan_usa19qw_pre_product_id:
1228 record = &keyspan_usa19qw_firmware[0];
1229 fw_name = "USA19QI";
1230 break;
1232 case keyspan_usa18x_pre_product_id:
1233 record = &keyspan_usa18x_firmware[0];
1234 fw_name = "USA18X";
1235 break;
1237 case keyspan_usa19w_pre_product_id:
1238 record = &keyspan_usa19w_firmware[0];
1239 fw_name = "USA19W";
1240 break;
1242 case keyspan_usa49w_pre_product_id:
1243 record = &keyspan_usa49w_firmware[0];
1244 fw_name = "USA49W";
1245 break;
1247 case keyspan_usa49wlc_pre_product_id:
1248 record = &keyspan_usa49wlc_firmware[0];
1249 fw_name = "USA49WLC";
1250 break;
1252 default:
1253 record = NULL;
1254 fw_name = "Unknown";
1255 break;
1258 if (record == NULL) {
1259 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1260 return(1);
1263 dbg("Uploading Keyspan %s firmware.", fw_name);
1265 /* download the firmware image */
1266 response = ezusb_set_reset(serial, 1);
1268 while(record->address != 0xffff) {
1269 response = ezusb_writememory(serial, record->address,
1270 (unsigned char *)record->data,
1271 record->data_size, 0xa0);
1272 if (response < 0) {
1273 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1274 "firmware (%d %04X %p %d)\n",
1275 response,
1276 record->address, record->data, record->data_size);
1277 break;
1279 record++;
1281 /* bring device out of reset. Renumeration will occur in a
1282 moment and the new device will bind to the real driver */
1283 response = ezusb_set_reset(serial, 0);
1285 /* we don't want this device to have a driver assigned to it. */
1286 return (1);
1289 /* Helper functions used by keyspan_setup_urbs */
1290 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1291 int dir, void *ctx, char *buf, int len,
1292 void (*callback)(struct urb *, struct pt_regs *regs))
1294 struct urb *urb;
1296 if (endpoint == -1)
1297 return NULL; /* endpoint not needed */
1299 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1300 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1301 if (urb == NULL) {
1302 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1303 return NULL;
1306 /* Fill URB using supplied data. */
1307 usb_fill_bulk_urb(urb, serial->dev,
1308 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1309 buf, len, callback, ctx);
1311 return urb;
1314 static struct callbacks {
1315 void (*instat_callback)(struct urb *, struct pt_regs *regs);
1316 void (*glocont_callback)(struct urb *, struct pt_regs *regs);
1317 void (*indat_callback)(struct urb *, struct pt_regs *regs);
1318 void (*outdat_callback)(struct urb *, struct pt_regs *regs);
1319 void (*inack_callback)(struct urb *, struct pt_regs *regs);
1320 void (*outcont_callback)(struct urb *, struct pt_regs *regs);
1321 } keyspan_callbacks[] = {
1323 /* msg_usa26 callbacks */
1324 .instat_callback = usa26_instat_callback,
1325 .glocont_callback = usa26_glocont_callback,
1326 .indat_callback = usa26_indat_callback,
1327 .outdat_callback = usa2x_outdat_callback,
1328 .inack_callback = usa26_inack_callback,
1329 .outcont_callback = usa26_outcont_callback,
1330 }, {
1331 /* msg_usa28 callbacks */
1332 .instat_callback = usa28_instat_callback,
1333 .glocont_callback = usa28_glocont_callback,
1334 .indat_callback = usa28_indat_callback,
1335 .outdat_callback = usa2x_outdat_callback,
1336 .inack_callback = usa28_inack_callback,
1337 .outcont_callback = usa28_outcont_callback,
1338 }, {
1339 /* msg_usa49 callbacks */
1340 .instat_callback = usa49_instat_callback,
1341 .glocont_callback = usa49_glocont_callback,
1342 .indat_callback = usa49_indat_callback,
1343 .outdat_callback = usa2x_outdat_callback,
1344 .inack_callback = usa49_inack_callback,
1345 .outcont_callback = usa49_outcont_callback,
1346 }, {
1347 /* msg_usa90 callbacks */
1348 .instat_callback = usa90_instat_callback,
1349 .glocont_callback = usa28_glocont_callback,
1350 .indat_callback = usa90_indat_callback,
1351 .outdat_callback = usa2x_outdat_callback,
1352 .inack_callback = usa28_inack_callback,
1353 .outcont_callback = usa90_outcont_callback,
1357 /* Generic setup urbs function that uses
1358 data in device_details */
1359 static void keyspan_setup_urbs(struct usb_serial *serial)
1361 int i, j;
1362 struct keyspan_serial_private *s_priv;
1363 const struct keyspan_device_details *d_details;
1364 struct usb_serial_port *port;
1365 struct keyspan_port_private *p_priv;
1366 struct callbacks *cback;
1367 int endp;
1369 dbg ("%s", __FUNCTION__);
1371 s_priv = usb_get_serial_data(serial);
1372 d_details = s_priv->device_details;
1374 /* Setup values for the various callback routines */
1375 cback = &keyspan_callbacks[d_details->msg_format];
1377 /* Allocate and set up urbs for each one that is in use,
1378 starting with instat endpoints */
1379 s_priv->instat_urb = keyspan_setup_urb
1380 (serial, d_details->instat_endpoint, USB_DIR_IN,
1381 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1382 cback->instat_callback);
1384 s_priv->glocont_urb = keyspan_setup_urb
1385 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1386 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1387 cback->glocont_callback);
1389 /* Setup endpoints for each port specific thing */
1390 for (i = 0; i < d_details->num_ports; i ++) {
1391 port = serial->port[i];
1392 p_priv = usb_get_serial_port_data(port);
1394 /* Do indat endpoints first, once for each flip */
1395 endp = d_details->indat_endpoints[i];
1396 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1397 p_priv->in_urbs[j] = keyspan_setup_urb
1398 (serial, endp, USB_DIR_IN, port,
1399 p_priv->in_buffer[j], 64,
1400 cback->indat_callback);
1402 for (; j < 2; ++j)
1403 p_priv->in_urbs[j] = NULL;
1405 /* outdat endpoints also have flip */
1406 endp = d_details->outdat_endpoints[i];
1407 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1408 p_priv->out_urbs[j] = keyspan_setup_urb
1409 (serial, endp, USB_DIR_OUT, port,
1410 p_priv->out_buffer[j], 64,
1411 cback->outdat_callback);
1413 for (; j < 2; ++j)
1414 p_priv->out_urbs[j] = NULL;
1416 /* inack endpoint */
1417 p_priv->inack_urb = keyspan_setup_urb
1418 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1419 port, p_priv->inack_buffer, 1, cback->inack_callback);
1421 /* outcont endpoint */
1422 p_priv->outcont_urb = keyspan_setup_urb
1423 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1424 port, p_priv->outcont_buffer, 64,
1425 cback->outcont_callback);
1430 /* usa19 function doesn't require prescaler */
1431 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1432 u8 *rate_low, u8 *prescaler, int portnum)
1434 u32 b16, /* baud rate times 16 (actual rate used internally) */
1435 div, /* divisor */
1436 cnt; /* inverse of divisor (programmed into 8051) */
1438 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1440 /* prevent divide by zero... */
1441 if( (b16 = (baud_rate * 16L)) == 0) {
1442 return (KEYSPAN_INVALID_BAUD_RATE);
1445 /* Any "standard" rate over 57k6 is marginal on the USA-19
1446 as we run out of divisor resolution. */
1447 if (baud_rate > 57600) {
1448 return (KEYSPAN_INVALID_BAUD_RATE);
1451 /* calculate the divisor and the counter (its inverse) */
1452 if( (div = (baudclk / b16)) == 0) {
1453 return (KEYSPAN_INVALID_BAUD_RATE);
1455 else {
1456 cnt = 0 - div;
1459 if(div > 0xffff) {
1460 return (KEYSPAN_INVALID_BAUD_RATE);
1463 /* return the counter values if non-null */
1464 if (rate_low) {
1465 *rate_low = (u8) (cnt & 0xff);
1467 if (rate_hi) {
1468 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1470 if (rate_low && rate_hi) {
1471 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1474 return (KEYSPAN_BAUD_RATE_OK);
1477 /* usa19hs function doesn't require prescaler */
1478 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1479 u8 *rate_low, u8 *prescaler, int portnum)
1481 u32 b16, /* baud rate times 16 (actual rate used internally) */
1482 div; /* divisor */
1484 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1486 /* prevent divide by zero... */
1487 if( (b16 = (baud_rate * 16L)) == 0)
1488 return (KEYSPAN_INVALID_BAUD_RATE);
1492 /* calculate the divisor */
1493 if( (div = (baudclk / b16)) == 0)
1494 return (KEYSPAN_INVALID_BAUD_RATE);
1496 if(div > 0xffff)
1497 return (KEYSPAN_INVALID_BAUD_RATE);
1499 /* return the counter values if non-null */
1500 if (rate_low)
1501 *rate_low = (u8) (div & 0xff);
1503 if (rate_hi)
1504 *rate_hi = (u8) ((div >> 8) & 0xff);
1506 if (rate_low && rate_hi)
1507 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1509 return (KEYSPAN_BAUD_RATE_OK);
1512 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1513 u8 *rate_low, u8 *prescaler, int portnum)
1515 u32 b16, /* baud rate times 16 (actual rate used internally) */
1516 clk, /* clock with 13/8 prescaler */
1517 div, /* divisor using 13/8 prescaler */
1518 res, /* resulting baud rate using 13/8 prescaler */
1519 diff, /* error using 13/8 prescaler */
1520 smallest_diff;
1521 u8 best_prescaler;
1522 int i;
1524 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1526 /* prevent divide by zero */
1527 if( (b16 = baud_rate * 16L) == 0) {
1528 return (KEYSPAN_INVALID_BAUD_RATE);
1531 /* Calculate prescaler by trying them all and looking
1532 for best fit */
1534 /* start with largest possible difference */
1535 smallest_diff = 0xffffffff;
1537 /* 0 is an invalid prescaler, used as a flag */
1538 best_prescaler = 0;
1540 for(i = 8; i <= 0xff; ++i) {
1541 clk = (baudclk * 8) / (u32) i;
1543 if( (div = clk / b16) == 0) {
1544 continue;
1547 res = clk / div;
1548 diff= (res > b16) ? (res-b16) : (b16-res);
1550 if(diff < smallest_diff) {
1551 best_prescaler = i;
1552 smallest_diff = diff;
1556 if(best_prescaler == 0) {
1557 return (KEYSPAN_INVALID_BAUD_RATE);
1560 clk = (baudclk * 8) / (u32) best_prescaler;
1561 div = clk / b16;
1563 /* return the divisor and prescaler if non-null */
1564 if (rate_low) {
1565 *rate_low = (u8) (div & 0xff);
1567 if (rate_hi) {
1568 *rate_hi = (u8) ((div >> 8) & 0xff);
1570 if (prescaler) {
1571 *prescaler = best_prescaler;
1572 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1574 return (KEYSPAN_BAUD_RATE_OK);
1577 /* USA-28 supports different maximum baud rates on each port */
1578 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1579 u8 *rate_low, u8 *prescaler, int portnum)
1581 u32 b16, /* baud rate times 16 (actual rate used internally) */
1582 div, /* divisor */
1583 cnt; /* inverse of divisor (programmed into 8051) */
1585 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1587 /* prevent divide by zero */
1588 if ((b16 = baud_rate * 16L) == 0)
1589 return (KEYSPAN_INVALID_BAUD_RATE);
1591 /* calculate the divisor and the counter (its inverse) */
1592 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1593 return (KEYSPAN_INVALID_BAUD_RATE);
1595 else {
1596 cnt = 0 - div;
1599 /* check for out of range, based on portnum,
1600 and return result */
1601 if(portnum == 0) {
1602 if(div > 0xffff)
1603 return (KEYSPAN_INVALID_BAUD_RATE);
1605 else {
1606 if(portnum == 1) {
1607 if(div > 0xff) {
1608 return (KEYSPAN_INVALID_BAUD_RATE);
1611 else {
1612 return (KEYSPAN_INVALID_BAUD_RATE);
1616 /* return the counter values if not NULL
1617 (port 1 will ignore retHi) */
1618 if (rate_low) {
1619 *rate_low = (u8) (cnt & 0xff);
1621 if (rate_hi) {
1622 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1624 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1625 return (KEYSPAN_BAUD_RATE_OK);
1628 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1629 struct usb_serial_port *port,
1630 int reset_port)
1632 struct keyspan_usa26_portControlMessage msg;
1633 struct keyspan_serial_private *s_priv;
1634 struct keyspan_port_private *p_priv;
1635 const struct keyspan_device_details *d_details;
1636 int outcont_urb;
1637 struct urb *this_urb;
1638 int device_port, err;
1640 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1642 s_priv = usb_get_serial_data(serial);
1643 p_priv = usb_get_serial_port_data(port);
1644 d_details = s_priv->device_details;
1645 device_port = port->number - port->serial->minor;
1647 outcont_urb = d_details->outcont_endpoints[port->number];
1648 this_urb = p_priv->outcont_urb;
1650 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1652 /* Make sure we have an urb then send the message */
1653 if (this_urb == NULL) {
1654 dbg("%s - oops no urb.", __FUNCTION__);
1655 return -1;
1658 /* Save reset port val for resend.
1659 Don't overwrite resend for close condition. */
1660 if (p_priv->resend_cont != 3)
1661 p_priv->resend_cont = reset_port + 1;
1662 if (this_urb->status == -EINPROGRESS) {
1663 /* dbg ("%s - already writing", __FUNCTION__); */
1664 mdelay(5);
1665 return(-1);
1668 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1670 /* Only set baud rate if it's changed */
1671 if (p_priv->old_baud != p_priv->baud) {
1672 p_priv->old_baud = p_priv->baud;
1673 msg.setClocking = 0xff;
1674 if (d_details->calculate_baud_rate
1675 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1676 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1677 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1678 p_priv->baud);
1679 msg.baudLo = 0;
1680 msg.baudHi = 125; /* Values for 9600 baud */
1681 msg.prescaler = 10;
1683 msg.setPrescaler = 0xff;
1686 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1687 switch (p_priv->cflag & CSIZE) {
1688 case CS5:
1689 msg.lcr |= USA_DATABITS_5;
1690 break;
1691 case CS6:
1692 msg.lcr |= USA_DATABITS_6;
1693 break;
1694 case CS7:
1695 msg.lcr |= USA_DATABITS_7;
1696 break;
1697 case CS8:
1698 msg.lcr |= USA_DATABITS_8;
1699 break;
1701 if (p_priv->cflag & PARENB) {
1702 /* note USA_PARITY_NONE == 0 */
1703 msg.lcr |= (p_priv->cflag & PARODD)?
1704 USA_PARITY_ODD: USA_PARITY_EVEN;
1706 msg.setLcr = 0xff;
1708 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1709 msg.xonFlowControl = 0;
1710 msg.setFlowControl = 0xff;
1711 msg.forwardingLength = 16;
1712 msg.xonChar = 17;
1713 msg.xoffChar = 19;
1715 /* Opening port */
1716 if (reset_port == 1) {
1717 msg._txOn = 1;
1718 msg._txOff = 0;
1719 msg.txFlush = 0;
1720 msg.txBreak = 0;
1721 msg.rxOn = 1;
1722 msg.rxOff = 0;
1723 msg.rxFlush = 1;
1724 msg.rxForward = 0;
1725 msg.returnStatus = 0;
1726 msg.resetDataToggle = 0xff;
1729 /* Closing port */
1730 else if (reset_port == 2) {
1731 msg._txOn = 0;
1732 msg._txOff = 1;
1733 msg.txFlush = 0;
1734 msg.txBreak = 0;
1735 msg.rxOn = 0;
1736 msg.rxOff = 1;
1737 msg.rxFlush = 1;
1738 msg.rxForward = 0;
1739 msg.returnStatus = 0;
1740 msg.resetDataToggle = 0;
1743 /* Sending intermediate configs */
1744 else {
1745 msg._txOn = (! p_priv->break_on);
1746 msg._txOff = 0;
1747 msg.txFlush = 0;
1748 msg.txBreak = (p_priv->break_on);
1749 msg.rxOn = 0;
1750 msg.rxOff = 0;
1751 msg.rxFlush = 0;
1752 msg.rxForward = 0;
1753 msg.returnStatus = 0;
1754 msg.resetDataToggle = 0x0;
1757 /* Do handshaking outputs */
1758 msg.setTxTriState_setRts = 0xff;
1759 msg.txTriState_rts = p_priv->rts_state;
1761 msg.setHskoa_setDtr = 0xff;
1762 msg.hskoa_dtr = p_priv->dtr_state;
1764 p_priv->resend_cont = 0;
1765 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1767 /* send the data out the device on control endpoint */
1768 this_urb->transfer_buffer_length = sizeof(msg);
1770 this_urb->dev = serial->dev;
1771 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1772 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1774 #if 0
1775 else {
1776 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1777 outcont_urb, this_urb->transfer_buffer_length,
1778 usb_pipeendpoint(this_urb->pipe));
1780 #endif
1782 return (0);
1785 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1786 struct usb_serial_port *port,
1787 int reset_port)
1789 struct keyspan_usa28_portControlMessage msg;
1790 struct keyspan_serial_private *s_priv;
1791 struct keyspan_port_private *p_priv;
1792 const struct keyspan_device_details *d_details;
1793 struct urb *this_urb;
1794 int device_port, err;
1796 dbg ("%s", __FUNCTION__);
1798 s_priv = usb_get_serial_data(serial);
1799 p_priv = usb_get_serial_port_data(port);
1800 d_details = s_priv->device_details;
1801 device_port = port->number - port->serial->minor;
1803 /* only do something if we have a bulk out endpoint */
1804 if ((this_urb = p_priv->outcont_urb) == NULL) {
1805 dbg("%s - oops no urb.", __FUNCTION__);
1806 return -1;
1809 /* Save reset port val for resend.
1810 Don't overwrite resend for close condition. */
1811 if (p_priv->resend_cont != 3)
1812 p_priv->resend_cont = reset_port + 1;
1813 if (this_urb->status == -EINPROGRESS) {
1814 dbg ("%s already writing", __FUNCTION__);
1815 mdelay(5);
1816 return(-1);
1819 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
1821 msg.setBaudRate = 1;
1822 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1823 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1824 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
1825 msg.baudLo = 0xff;
1826 msg.baudHi = 0xb2; /* Values for 9600 baud */
1829 /* If parity is enabled, we must calculate it ourselves. */
1830 msg.parity = 0; /* XXX for now */
1832 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1833 msg.xonFlowControl = 0;
1835 /* Do handshaking outputs, DTR is inverted relative to RTS */
1836 msg.rts = p_priv->rts_state;
1837 msg.dtr = p_priv->dtr_state;
1839 msg.forwardingLength = 16;
1840 msg.forwardMs = 10;
1841 msg.breakThreshold = 45;
1842 msg.xonChar = 17;
1843 msg.xoffChar = 19;
1845 /*msg.returnStatus = 1;
1846 msg.resetDataToggle = 0xff;*/
1847 /* Opening port */
1848 if (reset_port == 1) {
1849 msg._txOn = 1;
1850 msg._txOff = 0;
1851 msg.txFlush = 0;
1852 msg.txForceXoff = 0;
1853 msg.txBreak = 0;
1854 msg.rxOn = 1;
1855 msg.rxOff = 0;
1856 msg.rxFlush = 1;
1857 msg.rxForward = 0;
1858 msg.returnStatus = 0;
1859 msg.resetDataToggle = 0xff;
1861 /* Closing port */
1862 else if (reset_port == 2) {
1863 msg._txOn = 0;
1864 msg._txOff = 1;
1865 msg.txFlush = 0;
1866 msg.txForceXoff = 0;
1867 msg.txBreak = 0;
1868 msg.rxOn = 0;
1869 msg.rxOff = 1;
1870 msg.rxFlush = 1;
1871 msg.rxForward = 0;
1872 msg.returnStatus = 0;
1873 msg.resetDataToggle = 0;
1875 /* Sending intermediate configs */
1876 else {
1877 msg._txOn = (! p_priv->break_on);
1878 msg._txOff = 0;
1879 msg.txFlush = 0;
1880 msg.txForceXoff = 0;
1881 msg.txBreak = (p_priv->break_on);
1882 msg.rxOn = 0;
1883 msg.rxOff = 0;
1884 msg.rxFlush = 0;
1885 msg.rxForward = 0;
1886 msg.returnStatus = 0;
1887 msg.resetDataToggle = 0x0;
1890 p_priv->resend_cont = 0;
1891 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1893 /* send the data out the device on control endpoint */
1894 this_urb->transfer_buffer_length = sizeof(msg);
1896 this_urb->dev = serial->dev;
1897 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1898 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
1900 #if 0
1901 else {
1902 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
1903 this_urb->transfer_buffer_length);
1905 #endif
1907 return (0);
1910 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1911 struct usb_serial_port *port,
1912 int reset_port)
1914 struct keyspan_usa49_portControlMessage msg;
1915 struct keyspan_serial_private *s_priv;
1916 struct keyspan_port_private *p_priv;
1917 const struct keyspan_device_details *d_details;
1918 int glocont_urb;
1919 struct urb *this_urb;
1920 int err, device_port;
1922 dbg ("%s", __FUNCTION__);
1924 s_priv = usb_get_serial_data(serial);
1925 p_priv = usb_get_serial_port_data(port);
1926 d_details = s_priv->device_details;
1928 glocont_urb = d_details->glocont_endpoint;
1929 this_urb = s_priv->glocont_urb;
1931 /* Work out which port within the device is being setup */
1932 device_port = port->number - port->serial->minor;
1934 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
1936 /* Make sure we have an urb then send the message */
1937 if (this_urb == NULL) {
1938 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
1939 return -1;
1942 /* Save reset port val for resend.
1943 Don't overwrite resend for close condition. */
1944 if (p_priv->resend_cont != 3)
1945 p_priv->resend_cont = reset_port + 1;
1946 if (this_urb->status == -EINPROGRESS) {
1947 /* dbg ("%s - already writing", __FUNCTION__); */
1948 mdelay(5);
1949 return(-1);
1952 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
1954 /*msg.portNumber = port->number;*/
1955 msg.portNumber = device_port;
1957 /* Only set baud rate if it's changed */
1958 if (p_priv->old_baud != p_priv->baud) {
1959 p_priv->old_baud = p_priv->baud;
1960 msg.setClocking = 0xff;
1961 if (d_details->calculate_baud_rate
1962 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1963 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1964 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1965 p_priv->baud);
1966 msg.baudLo = 0;
1967 msg.baudHi = 125; /* Values for 9600 baud */
1968 msg.prescaler = 10;
1970 //msg.setPrescaler = 0xff;
1973 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1974 switch (p_priv->cflag & CSIZE) {
1975 case CS5:
1976 msg.lcr |= USA_DATABITS_5;
1977 break;
1978 case CS6:
1979 msg.lcr |= USA_DATABITS_6;
1980 break;
1981 case CS7:
1982 msg.lcr |= USA_DATABITS_7;
1983 break;
1984 case CS8:
1985 msg.lcr |= USA_DATABITS_8;
1986 break;
1988 if (p_priv->cflag & PARENB) {
1989 /* note USA_PARITY_NONE == 0 */
1990 msg.lcr |= (p_priv->cflag & PARODD)?
1991 USA_PARITY_ODD: USA_PARITY_EVEN;
1993 msg.setLcr = 0xff;
1995 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1996 msg.xonFlowControl = 0;
1997 msg.setFlowControl = 0xff;
1999 msg.forwardingLength = 16;
2000 msg.xonChar = 17;
2001 msg.xoffChar = 19;
2003 /* Opening port */
2004 if (reset_port == 1) {
2005 msg._txOn = 1;
2006 msg._txOff = 0;
2007 msg.txFlush = 0;
2008 msg.txBreak = 0;
2009 msg.rxOn = 1;
2010 msg.rxOff = 0;
2011 msg.rxFlush = 1;
2012 msg.rxForward = 0;
2013 msg.returnStatus = 0;
2014 msg.resetDataToggle = 0xff;
2015 msg.enablePort = 1;
2016 msg.disablePort = 0;
2018 /* Closing port */
2019 else if (reset_port == 2) {
2020 msg._txOn = 0;
2021 msg._txOff = 1;
2022 msg.txFlush = 0;
2023 msg.txBreak = 0;
2024 msg.rxOn = 0;
2025 msg.rxOff = 1;
2026 msg.rxFlush = 1;
2027 msg.rxForward = 0;
2028 msg.returnStatus = 0;
2029 msg.resetDataToggle = 0;
2030 msg.enablePort = 0;
2031 msg.disablePort = 1;
2033 /* Sending intermediate configs */
2034 else {
2035 msg._txOn = (! p_priv->break_on);
2036 msg._txOff = 0;
2037 msg.txFlush = 0;
2038 msg.txBreak = (p_priv->break_on);
2039 msg.rxOn = 0;
2040 msg.rxOff = 0;
2041 msg.rxFlush = 0;
2042 msg.rxForward = 0;
2043 msg.returnStatus = 0;
2044 msg.resetDataToggle = 0x0;
2045 msg.enablePort = 0;
2046 msg.disablePort = 0;
2049 /* Do handshaking outputs */
2050 msg.setRts = 0xff;
2051 msg.rts = p_priv->rts_state;
2053 msg.setDtr = 0xff;
2054 msg.dtr = p_priv->dtr_state;
2056 p_priv->resend_cont = 0;
2057 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2059 /* send the data out the device on control endpoint */
2060 this_urb->transfer_buffer_length = sizeof(msg);
2062 this_urb->dev = serial->dev;
2063 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2064 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2066 #if 0
2067 else {
2068 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2069 outcont_urb, this_urb->transfer_buffer_length,
2070 usb_pipeendpoint(this_urb->pipe));
2072 #endif
2074 return (0);
2077 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2078 struct usb_serial_port *port,
2079 int reset_port)
2081 struct keyspan_usa90_portControlMessage msg;
2082 struct keyspan_serial_private *s_priv;
2083 struct keyspan_port_private *p_priv;
2084 const struct keyspan_device_details *d_details;
2085 struct urb *this_urb;
2086 int err;
2087 u8 prescaler;
2089 dbg ("%s", __FUNCTION__);
2091 s_priv = usb_get_serial_data(serial);
2092 p_priv = usb_get_serial_port_data(port);
2093 d_details = s_priv->device_details;
2095 /* only do something if we have a bulk out endpoint */
2096 if ((this_urb = p_priv->outcont_urb) == NULL) {
2097 dbg("%s - oops no urb.", __FUNCTION__);
2098 return -1;
2101 /* Save reset port val for resend.
2102 Don't overwrite resend for open/close condition. */
2103 if ((reset_port + 1) > p_priv->resend_cont)
2104 p_priv->resend_cont = reset_port + 1;
2105 if (this_urb->status == -EINPROGRESS) {
2106 dbg ("%s already writing", __FUNCTION__);
2107 mdelay(5);
2108 return(-1);
2111 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2113 /* Only set baud rate if it's changed */
2114 if (p_priv->old_baud != p_priv->baud) {
2115 p_priv->old_baud = p_priv->baud;
2116 msg.setClocking = 0x01;
2117 if (d_details->calculate_baud_rate
2118 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2119 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2120 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2121 p_priv->baud);
2122 p_priv->baud = 9600;
2123 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2124 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2126 msg.setRxMode = 1;
2127 msg.setTxMode = 1;
2130 /* modes must always be correctly specified */
2131 if (p_priv->baud > 57600)
2133 msg.rxMode = RXMODE_DMA;
2134 msg.txMode = TXMODE_DMA;
2136 else
2138 msg.rxMode = RXMODE_BYHAND;
2139 msg.txMode = TXMODE_BYHAND;
2142 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2143 switch (p_priv->cflag & CSIZE) {
2144 case CS5:
2145 msg.lcr |= USA_DATABITS_5;
2146 break;
2147 case CS6:
2148 msg.lcr |= USA_DATABITS_6;
2149 break;
2150 case CS7:
2151 msg.lcr |= USA_DATABITS_7;
2152 break;
2153 case CS8:
2154 msg.lcr |= USA_DATABITS_8;
2155 break;
2157 if (p_priv->cflag & PARENB) {
2158 /* note USA_PARITY_NONE == 0 */
2159 msg.lcr |= (p_priv->cflag & PARODD)?
2160 USA_PARITY_ODD: USA_PARITY_EVEN;
2162 if (p_priv->old_cflag != p_priv->cflag) {
2163 p_priv->old_cflag = p_priv->cflag;
2164 msg.setLcr = 0x01;
2167 if (p_priv->flow_control == flow_cts)
2168 msg.txFlowControl = TXFLOW_CTS;
2169 msg.setTxFlowControl = 0x01;
2170 msg.setRxFlowControl = 0x01;
2172 msg.rxForwardingLength = 16;
2173 msg.rxForwardingTimeout = 16;
2174 msg.txAckSetting = 0;
2175 msg.xonChar = 17;
2176 msg.xoffChar = 19;
2178 /* Opening port */
2179 if (reset_port == 1) {
2180 msg.portEnabled = 1;
2181 msg.rxFlush = 1;
2182 msg.txBreak = (p_priv->break_on);
2184 /* Closing port */
2185 else if (reset_port == 2) {
2186 msg.portEnabled = 0;
2188 /* Sending intermediate configs */
2189 else {
2190 if (port->open_count)
2191 msg.portEnabled = 1;
2192 msg.txBreak = (p_priv->break_on);
2195 /* Do handshaking outputs */
2196 msg.setRts = 0x01;
2197 msg.rts = p_priv->rts_state;
2199 msg.setDtr = 0x01;
2200 msg.dtr = p_priv->dtr_state;
2202 p_priv->resend_cont = 0;
2203 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2205 /* send the data out the device on control endpoint */
2206 this_urb->transfer_buffer_length = sizeof(msg);
2208 this_urb->dev = serial->dev;
2209 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2210 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2212 return (0);
2215 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2217 struct usb_serial *serial = port->serial;
2218 struct keyspan_serial_private *s_priv;
2219 const struct keyspan_device_details *d_details;
2221 dbg ("%s", __FUNCTION__);
2223 s_priv = usb_get_serial_data(serial);
2224 d_details = s_priv->device_details;
2226 switch (d_details->msg_format) {
2227 case msg_usa26:
2228 keyspan_usa26_send_setup(serial, port, reset_port);
2229 break;
2230 case msg_usa28:
2231 keyspan_usa28_send_setup(serial, port, reset_port);
2232 break;
2233 case msg_usa49:
2234 keyspan_usa49_send_setup(serial, port, reset_port);
2235 break;
2236 case msg_usa90:
2237 keyspan_usa90_send_setup(serial, port, reset_port);
2238 break;
2243 /* Gets called by the "real" driver (ie once firmware is loaded
2244 and renumeration has taken place. */
2245 static int keyspan_startup (struct usb_serial *serial)
2247 int i, err;
2248 struct usb_serial_port *port;
2249 struct keyspan_serial_private *s_priv;
2250 struct keyspan_port_private *p_priv;
2251 const struct keyspan_device_details *d_details;
2253 dbg("%s", __FUNCTION__);
2255 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2256 if (d_details->product_id == serial->dev->descriptor.idProduct)
2257 break;
2258 if (d_details == NULL) {
2259 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, serial->dev->descriptor.idProduct);
2260 return 1;
2263 /* Setup private data for serial driver */
2264 s_priv = kmalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2265 if (!s_priv) {
2266 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2267 return -ENOMEM;
2269 memset(s_priv, 0, sizeof(struct keyspan_serial_private));
2271 s_priv->device_details = d_details;
2272 usb_set_serial_data(serial, s_priv);
2274 /* Now setup per port private data */
2275 for (i = 0; i < serial->num_ports; i++) {
2276 port = serial->port[i];
2277 p_priv = kmalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2278 if (!p_priv) {
2279 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2280 return (1);
2282 memset(p_priv, 0, sizeof(struct keyspan_port_private));
2283 p_priv->device_details = d_details;
2284 usb_set_serial_port_data(port, p_priv);
2287 keyspan_setup_urbs(serial);
2289 s_priv->instat_urb->dev = serial->dev;
2290 if ((err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL)) != 0) {
2291 dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
2294 return (0);
2297 static void keyspan_shutdown (struct usb_serial *serial)
2299 int i, j;
2300 struct usb_serial_port *port;
2301 struct keyspan_serial_private *s_priv;
2302 struct keyspan_port_private *p_priv;
2304 dbg("%s", __FUNCTION__);
2306 s_priv = usb_get_serial_data(serial);
2308 /* Stop reading/writing urbs */
2309 stop_urb(s_priv->instat_urb);
2310 stop_urb(s_priv->glocont_urb);
2311 for (i = 0; i < serial->num_ports; ++i) {
2312 port = serial->port[i];
2313 p_priv = usb_get_serial_port_data(port);
2314 stop_urb(p_priv->inack_urb);
2315 stop_urb(p_priv->outcont_urb);
2316 for (j = 0; j < 2; j++) {
2317 stop_urb(p_priv->in_urbs[j]);
2318 stop_urb(p_priv->out_urbs[j]);
2322 /* Now free them */
2323 if (s_priv->instat_urb)
2324 usb_free_urb(s_priv->instat_urb);
2325 if (s_priv->glocont_urb)
2326 usb_free_urb(s_priv->glocont_urb);
2327 for (i = 0; i < serial->num_ports; ++i) {
2328 port = serial->port[i];
2329 p_priv = usb_get_serial_port_data(port);
2330 if (p_priv->inack_urb)
2331 usb_free_urb(p_priv->inack_urb);
2332 if (p_priv->outcont_urb)
2333 usb_free_urb(p_priv->outcont_urb);
2334 for (j = 0; j < 2; j++) {
2335 if (p_priv->in_urbs[j])
2336 usb_free_urb(p_priv->in_urbs[j]);
2337 if (p_priv->out_urbs[j])
2338 usb_free_urb(p_priv->out_urbs[j]);
2342 /* dbg("Freeing serial->private."); */
2343 kfree(s_priv);
2345 /* dbg("Freeing port->private."); */
2346 /* Now free per port private data */
2347 for (i = 0; i < serial->num_ports; i++) {
2348 port = serial->port[i];
2349 kfree(usb_get_serial_port_data(port));
2353 MODULE_AUTHOR( DRIVER_AUTHOR );
2354 MODULE_DESCRIPTION( DRIVER_DESC );
2355 MODULE_LICENSE("GPL");
2357 module_param(debug, bool, S_IRUGO | S_IWUSR);
2358 MODULE_PARM_DESC(debug, "Debug enabled or not");