MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / usb / serial / keyspan.c
blob53be824eb1bf2070e0e666ea67313de07de035c8
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/kernel.h>
99 #include <linux/jiffies.h>
100 #include <linux/errno.h>
101 #include <linux/init.h>
102 #include <linux/slab.h>
103 #include <linux/tty.h>
104 #include <linux/tty_driver.h>
105 #include <linux/tty_flip.h>
106 #include <linux/module.h>
107 #include <linux/spinlock.h>
108 #include <asm/uaccess.h>
109 #include <linux/usb.h>
110 #include <linux/usb/serial.h>
111 #include "keyspan.h"
113 static int debug;
116 * Version Information
118 #define DRIVER_VERSION "v1.1.4"
119 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
120 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
122 #define INSTAT_BUFLEN 32
123 #define GLOCONT_BUFLEN 64
125 /* Per device and per port private data */
126 struct keyspan_serial_private {
127 const struct keyspan_device_details *device_details;
129 struct urb *instat_urb;
130 char instat_buf[INSTAT_BUFLEN];
132 /* XXX this one probably will need a lock */
133 struct urb *glocont_urb;
134 char glocont_buf[GLOCONT_BUFLEN];
137 struct keyspan_port_private {
138 /* Keep track of which input & output endpoints to use */
139 int in_flip;
140 int out_flip;
142 /* Keep duplicate of device details in each port
143 structure as well - simplifies some of the
144 callback functions etc. */
145 const struct keyspan_device_details *device_details;
147 /* Input endpoints and buffer for this port */
148 struct urb *in_urbs[2];
149 char in_buffer[2][64];
150 /* Output endpoints and buffer for this port */
151 struct urb *out_urbs[2];
152 char out_buffer[2][64];
154 /* Input ack endpoint */
155 struct urb *inack_urb;
156 char inack_buffer[1];
158 /* Output control endpoint */
159 struct urb *outcont_urb;
160 char outcont_buffer[64];
162 /* Settings for the port */
163 int baud;
164 int old_baud;
165 unsigned int cflag;
166 unsigned int old_cflag;
167 enum {flow_none, flow_cts, flow_xon} flow_control;
168 int rts_state; /* Handshaking pins (outputs) */
169 int dtr_state;
170 int cts_state; /* Handshaking pins (inputs) */
171 int dsr_state;
172 int dcd_state;
173 int ri_state;
174 int break_on;
176 unsigned long tx_start_time[2];
177 int resend_cont; /* need to resend control packet */
181 /* Include Keyspan message headers. All current Keyspan Adapters
182 make use of one of four message formats which are referred
183 to as USA-26, USA-28 and USA-49, USA-90 by Keyspan and within this driver. */
184 #include "keyspan_usa26msg.h"
185 #include "keyspan_usa28msg.h"
186 #include "keyspan_usa49msg.h"
187 #include "keyspan_usa90msg.h"
190 /* Functions used by new usb-serial code. */
191 static int __init keyspan_init (void)
193 int retval;
194 retval = usb_serial_register(&keyspan_pre_device);
195 if (retval)
196 goto failed_pre_device_register;
197 retval = usb_serial_register(&keyspan_1port_device);
198 if (retval)
199 goto failed_1port_device_register;
200 retval = usb_serial_register(&keyspan_2port_device);
201 if (retval)
202 goto failed_2port_device_register;
203 retval = usb_serial_register(&keyspan_4port_device);
204 if (retval)
205 goto failed_4port_device_register;
206 retval = usb_register(&keyspan_driver);
207 if (retval)
208 goto failed_usb_register;
210 info(DRIVER_VERSION ":" DRIVER_DESC);
212 return 0;
213 failed_usb_register:
214 usb_serial_deregister(&keyspan_4port_device);
215 failed_4port_device_register:
216 usb_serial_deregister(&keyspan_2port_device);
217 failed_2port_device_register:
218 usb_serial_deregister(&keyspan_1port_device);
219 failed_1port_device_register:
220 usb_serial_deregister(&keyspan_pre_device);
221 failed_pre_device_register:
222 return retval;
225 static void __exit keyspan_exit (void)
227 usb_deregister (&keyspan_driver);
228 usb_serial_deregister (&keyspan_pre_device);
229 usb_serial_deregister (&keyspan_1port_device);
230 usb_serial_deregister (&keyspan_2port_device);
231 usb_serial_deregister (&keyspan_4port_device);
234 module_init(keyspan_init);
235 module_exit(keyspan_exit);
237 static void keyspan_rx_throttle (struct usb_serial_port *port)
239 dbg("%s - port %d", __FUNCTION__, port->number);
243 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
245 dbg("%s - port %d", __FUNCTION__, port->number);
249 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
251 struct keyspan_port_private *p_priv;
253 dbg("%s", __FUNCTION__);
255 p_priv = usb_get_serial_port_data(port);
257 if (break_state == -1)
258 p_priv->break_on = 1;
259 else
260 p_priv->break_on = 0;
262 keyspan_send_setup(port, 0);
266 static void keyspan_set_termios (struct usb_serial_port *port,
267 struct termios *old_termios)
269 int baud_rate, device_port;
270 struct keyspan_port_private *p_priv;
271 const struct keyspan_device_details *d_details;
272 unsigned int cflag;
274 dbg("%s", __FUNCTION__);
276 p_priv = usb_get_serial_port_data(port);
277 d_details = p_priv->device_details;
278 cflag = port->tty->termios->c_cflag;
279 device_port = port->number - port->serial->minor;
281 /* Baud rate calculation takes baud rate as an integer
282 so other rates can be generated if desired. */
283 baud_rate = tty_get_baud_rate(port->tty);
284 /* If no match or invalid, don't change */
285 if (baud_rate >= 0
286 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
287 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
288 /* FIXME - more to do here to ensure rate changes cleanly */
289 p_priv->baud = baud_rate;
292 /* set CTS/RTS handshake etc. */
293 p_priv->cflag = cflag;
294 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
296 keyspan_send_setup(port, 0);
299 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
301 unsigned int value;
302 struct keyspan_port_private *p_priv;
304 p_priv = usb_get_serial_port_data(port);
306 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
307 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
308 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
309 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
310 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
311 ((p_priv->ri_state) ? TIOCM_RNG : 0);
313 return value;
316 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
317 unsigned int set, unsigned int clear)
319 struct keyspan_port_private *p_priv;
321 p_priv = usb_get_serial_port_data(port);
323 if (set & TIOCM_RTS)
324 p_priv->rts_state = 1;
325 if (set & TIOCM_DTR)
326 p_priv->dtr_state = 1;
328 if (clear & TIOCM_RTS)
329 p_priv->rts_state = 0;
330 if (clear & TIOCM_DTR)
331 p_priv->dtr_state = 0;
332 keyspan_send_setup(port, 0);
333 return 0;
336 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
337 unsigned int cmd, unsigned long arg)
339 return -ENOIOCTLCMD;
342 /* Write function is similar for the four protocols used
343 with only a minor change for usa90 (usa19hs) required */
344 static int keyspan_write(struct usb_serial_port *port,
345 const unsigned char *buf, int count)
347 struct keyspan_port_private *p_priv;
348 const struct keyspan_device_details *d_details;
349 int flip;
350 int left, todo;
351 struct urb *this_urb;
352 int err, maxDataLen, dataOffset;
354 p_priv = usb_get_serial_port_data(port);
355 d_details = p_priv->device_details;
357 if (d_details->msg_format == msg_usa90) {
358 maxDataLen = 64;
359 dataOffset = 0;
360 } else {
361 maxDataLen = 63;
362 dataOffset = 1;
365 dbg("%s - for port %d (%d chars), flip=%d",
366 __FUNCTION__, port->number, count, p_priv->out_flip);
368 for (left = count; left > 0; left -= todo) {
369 todo = left;
370 if (todo > maxDataLen)
371 todo = maxDataLen;
373 flip = p_priv->out_flip;
375 /* Check we have a valid urb/endpoint before we use it... */
376 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
377 /* no bulk out, so return 0 bytes written */
378 dbg("%s - no output urb :(", __FUNCTION__);
379 return count;
382 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
384 if (this_urb->status == -EINPROGRESS) {
385 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
386 break;
387 usb_unlink_urb(this_urb);
388 break;
391 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
392 for now so set to zero */
393 ((char *)this_urb->transfer_buffer)[0] = 0;
395 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
396 buf += todo;
398 /* send the data out the bulk port */
399 this_urb->transfer_buffer_length = todo + dataOffset;
401 this_urb->dev = port->serial->dev;
402 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
403 dbg("usb_submit_urb(write bulk) failed (%d)", err);
405 p_priv->tx_start_time[flip] = jiffies;
407 /* Flip for next time if usa26 or usa28 interface
408 (not used on usa49) */
409 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
412 return count - left;
415 static void usa26_indat_callback(struct urb *urb)
417 int i, err;
418 int endpoint;
419 struct usb_serial_port *port;
420 struct tty_struct *tty;
421 unsigned char *data = urb->transfer_buffer;
423 dbg ("%s", __FUNCTION__);
425 endpoint = usb_pipeendpoint(urb->pipe);
427 if (urb->status) {
428 dbg("%s - nonzero status: %x on endpoint %d.",
429 __FUNCTION__, urb->status, endpoint);
430 return;
433 port = (struct usb_serial_port *) urb->context;
434 tty = port->tty;
435 if (urb->actual_length) {
436 /* 0x80 bit is error flag */
437 if ((data[0] & 0x80) == 0) {
438 /* no errors on individual bytes, only possible overrun err*/
439 if (data[0] & RXERROR_OVERRUN)
440 err = TTY_OVERRUN;
441 else err = 0;
442 for (i = 1; i < urb->actual_length ; ++i) {
443 tty_insert_flip_char(tty, data[i], err);
445 } else {
446 /* some bytes had errors, every byte has status */
447 dbg("%s - RX error!!!!", __FUNCTION__);
448 for (i = 0; i + 1 < urb->actual_length; i += 2) {
449 int stat = data[i], flag = 0;
450 if (stat & RXERROR_OVERRUN)
451 flag |= TTY_OVERRUN;
452 if (stat & RXERROR_FRAMING)
453 flag |= TTY_FRAME;
454 if (stat & RXERROR_PARITY)
455 flag |= TTY_PARITY;
456 /* XXX should handle break (0x10) */
457 tty_insert_flip_char(tty, data[i+1], flag);
460 tty_flip_buffer_push(tty);
463 /* Resubmit urb so we continue receiving */
464 urb->dev = port->serial->dev;
465 if (port->open_count)
466 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
467 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
469 return;
472 /* Outdat handling is common for all devices */
473 static void usa2x_outdat_callback(struct urb *urb)
475 struct usb_serial_port *port;
476 struct keyspan_port_private *p_priv;
478 port = (struct usb_serial_port *) urb->context;
479 p_priv = usb_get_serial_port_data(port);
480 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
482 if (port->open_count)
483 usb_serial_port_softint(port);
486 static void usa26_inack_callback(struct urb *urb)
488 dbg ("%s", __FUNCTION__);
492 static void usa26_outcont_callback(struct urb *urb)
494 struct usb_serial_port *port;
495 struct keyspan_port_private *p_priv;
497 port = (struct usb_serial_port *) urb->context;
498 p_priv = usb_get_serial_port_data(port);
500 if (p_priv->resend_cont) {
501 dbg ("%s - sending setup", __FUNCTION__);
502 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
506 static void usa26_instat_callback(struct urb *urb)
508 unsigned char *data = urb->transfer_buffer;
509 struct keyspan_usa26_portStatusMessage *msg;
510 struct usb_serial *serial;
511 struct usb_serial_port *port;
512 struct keyspan_port_private *p_priv;
513 int old_dcd_state, err;
515 serial = (struct usb_serial *) urb->context;
517 if (urb->status) {
518 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
519 return;
521 if (urb->actual_length != 9) {
522 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
523 goto exit;
526 msg = (struct keyspan_usa26_portStatusMessage *)data;
528 #if 0
529 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
530 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
531 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
532 #endif
534 /* Now do something useful with the data */
537 /* Check port number from message and retrieve private data */
538 if (msg->port >= serial->num_ports) {
539 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
540 goto exit;
542 port = serial->port[msg->port];
543 p_priv = usb_get_serial_port_data(port);
545 /* Update handshaking pin state information */
546 old_dcd_state = p_priv->dcd_state;
547 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
548 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
549 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
550 p_priv->ri_state = ((msg->ri) ? 1 : 0);
552 if (port->tty && !C_CLOCAL(port->tty)
553 && old_dcd_state != p_priv->dcd_state) {
554 if (old_dcd_state)
555 tty_hangup(port->tty);
556 /* else */
557 /* wake_up_interruptible(&p_priv->open_wait); */
560 /* Resubmit urb so we continue receiving */
561 urb->dev = serial->dev;
562 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
563 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
565 exit: ;
568 static void usa26_glocont_callback(struct urb *urb)
570 dbg ("%s", __FUNCTION__);
575 static void usa28_indat_callback(struct urb *urb)
577 int i, err;
578 struct usb_serial_port *port;
579 struct tty_struct *tty;
580 unsigned char *data;
581 struct keyspan_port_private *p_priv;
583 dbg ("%s", __FUNCTION__);
585 port = (struct usb_serial_port *) urb->context;
586 p_priv = usb_get_serial_port_data(port);
587 data = urb->transfer_buffer;
589 if (urb != p_priv->in_urbs[p_priv->in_flip])
590 return;
592 do {
593 if (urb->status) {
594 dbg("%s - nonzero status: %x on endpoint %d.",
595 __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
596 return;
599 port = (struct usb_serial_port *) urb->context;
600 p_priv = usb_get_serial_port_data(port);
601 data = urb->transfer_buffer;
603 tty = port->tty;
604 if (urb->actual_length) {
605 for (i = 0; i < urb->actual_length ; ++i) {
606 tty_insert_flip_char(tty, data[i], 0);
608 tty_flip_buffer_push(tty);
611 /* Resubmit urb so we continue receiving */
612 urb->dev = port->serial->dev;
613 if (port->open_count)
614 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
615 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
617 p_priv->in_flip ^= 1;
619 urb = p_priv->in_urbs[p_priv->in_flip];
620 } while (urb->status != -EINPROGRESS);
623 static void usa28_inack_callback(struct urb *urb)
625 dbg ("%s", __FUNCTION__);
628 static void usa28_outcont_callback(struct urb *urb)
630 struct usb_serial_port *port;
631 struct keyspan_port_private *p_priv;
633 port = (struct usb_serial_port *) urb->context;
634 p_priv = usb_get_serial_port_data(port);
636 if (p_priv->resend_cont) {
637 dbg ("%s - sending setup", __FUNCTION__);
638 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
642 static void usa28_instat_callback(struct urb *urb)
644 int err;
645 unsigned char *data = urb->transfer_buffer;
646 struct keyspan_usa28_portStatusMessage *msg;
647 struct usb_serial *serial;
648 struct usb_serial_port *port;
649 struct keyspan_port_private *p_priv;
650 int old_dcd_state;
652 serial = (struct usb_serial *) urb->context;
654 if (urb->status) {
655 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
656 return;
659 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
660 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
661 goto exit;
664 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
665 data[0], data[1], data[2], data[3], data[4], data[5],
666 data[6], data[7], data[8], data[9], data[10], data[11]);*/
668 /* Now do something useful with the data */
669 msg = (struct keyspan_usa28_portStatusMessage *)data;
672 /* Check port number from message and retrieve private data */
673 if (msg->port >= serial->num_ports) {
674 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
675 goto exit;
677 port = serial->port[msg->port];
678 p_priv = usb_get_serial_port_data(port);
680 /* Update handshaking pin state information */
681 old_dcd_state = p_priv->dcd_state;
682 p_priv->cts_state = ((msg->cts) ? 1 : 0);
683 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
684 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
685 p_priv->ri_state = ((msg->ri) ? 1 : 0);
687 if (port->tty && !C_CLOCAL(port->tty)
688 && old_dcd_state != p_priv->dcd_state) {
689 if (old_dcd_state)
690 tty_hangup(port->tty);
691 /* else */
692 /* wake_up_interruptible(&p_priv->open_wait); */
695 /* Resubmit urb so we continue receiving */
696 urb->dev = serial->dev;
697 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
698 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
700 exit: ;
703 static void usa28_glocont_callback(struct urb *urb)
705 dbg ("%s", __FUNCTION__);
709 static void usa49_glocont_callback(struct urb *urb)
711 struct usb_serial *serial;
712 struct usb_serial_port *port;
713 struct keyspan_port_private *p_priv;
714 int i;
716 dbg ("%s", __FUNCTION__);
718 serial = (struct usb_serial *) urb->context;
719 for (i = 0; i < serial->num_ports; ++i) {
720 port = serial->port[i];
721 p_priv = usb_get_serial_port_data(port);
723 if (p_priv->resend_cont) {
724 dbg ("%s - sending setup", __FUNCTION__);
725 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
726 break;
731 /* This is actually called glostat in the Keyspan
732 doco */
733 static void usa49_instat_callback(struct urb *urb)
735 int err;
736 unsigned char *data = urb->transfer_buffer;
737 struct keyspan_usa49_portStatusMessage *msg;
738 struct usb_serial *serial;
739 struct usb_serial_port *port;
740 struct keyspan_port_private *p_priv;
741 int old_dcd_state;
743 dbg ("%s", __FUNCTION__);
745 serial = (struct usb_serial *) urb->context;
747 if (urb->status) {
748 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
749 return;
752 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
753 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
754 goto exit;
757 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
758 data[0], data[1], data[2], data[3], data[4], data[5],
759 data[6], data[7], data[8], data[9], data[10]);*/
761 /* Now do something useful with the data */
762 msg = (struct keyspan_usa49_portStatusMessage *)data;
764 /* Check port number from message and retrieve private data */
765 if (msg->portNumber >= serial->num_ports) {
766 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
767 goto exit;
769 port = serial->port[msg->portNumber];
770 p_priv = usb_get_serial_port_data(port);
772 /* Update handshaking pin state information */
773 old_dcd_state = p_priv->dcd_state;
774 p_priv->cts_state = ((msg->cts) ? 1 : 0);
775 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
776 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
777 p_priv->ri_state = ((msg->ri) ? 1 : 0);
779 if (port->tty && !C_CLOCAL(port->tty)
780 && old_dcd_state != p_priv->dcd_state) {
781 if (old_dcd_state)
782 tty_hangup(port->tty);
783 /* else */
784 /* wake_up_interruptible(&p_priv->open_wait); */
787 /* Resubmit urb so we continue receiving */
788 urb->dev = serial->dev;
790 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
791 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
793 exit: ;
796 static void usa49_inack_callback(struct urb *urb)
798 dbg ("%s", __FUNCTION__);
801 static void usa49_indat_callback(struct urb *urb)
803 int i, err;
804 int endpoint;
805 struct usb_serial_port *port;
806 struct tty_struct *tty;
807 unsigned char *data = urb->transfer_buffer;
809 dbg ("%s", __FUNCTION__);
811 endpoint = usb_pipeendpoint(urb->pipe);
813 if (urb->status) {
814 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
815 urb->status, endpoint);
816 return;
819 port = (struct usb_serial_port *) urb->context;
820 tty = port->tty;
821 if (urb->actual_length) {
822 /* 0x80 bit is error flag */
823 if ((data[0] & 0x80) == 0) {
824 /* no error on any byte */
825 for (i = 1; i < urb->actual_length ; ++i) {
826 tty_insert_flip_char(tty, data[i], 0);
828 } else {
829 /* some bytes had errors, every byte has status */
830 for (i = 0; i + 1 < urb->actual_length; i += 2) {
831 int stat = data[i], flag = 0;
832 if (stat & RXERROR_OVERRUN)
833 flag |= TTY_OVERRUN;
834 if (stat & RXERROR_FRAMING)
835 flag |= TTY_FRAME;
836 if (stat & RXERROR_PARITY)
837 flag |= TTY_PARITY;
838 /* XXX should handle break (0x10) */
839 tty_insert_flip_char(tty, data[i+1], flag);
842 tty_flip_buffer_push(tty);
845 /* Resubmit urb so we continue receiving */
846 urb->dev = port->serial->dev;
847 if (port->open_count)
848 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
849 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
853 /* not used, usa-49 doesn't have per-port control endpoints */
854 static void usa49_outcont_callback(struct urb *urb)
856 dbg ("%s", __FUNCTION__);
859 static void usa90_indat_callback(struct urb *urb)
861 int i, err;
862 int endpoint;
863 struct usb_serial_port *port;
864 struct keyspan_port_private *p_priv;
865 struct tty_struct *tty;
866 unsigned char *data = urb->transfer_buffer;
868 dbg ("%s", __FUNCTION__);
870 endpoint = usb_pipeendpoint(urb->pipe);
873 if (urb->status) {
874 dbg("%s - nonzero status: %x on endpoint %d.",
875 __FUNCTION__, urb->status, endpoint);
876 return;
879 port = (struct usb_serial_port *) urb->context;
880 p_priv = usb_get_serial_port_data(port);
882 tty = port->tty;
883 if (urb->actual_length) {
885 /* if current mode is DMA, looks like usa28 format
886 otherwise looks like usa26 data format */
888 if (p_priv->baud > 57600) {
889 for (i = 0; i < urb->actual_length ; ++i)
890 tty_insert_flip_char(tty, data[i], 0);
892 else {
894 /* 0x80 bit is error flag */
895 if ((data[0] & 0x80) == 0) {
896 /* no errors on individual bytes, only possible overrun err*/
897 if (data[0] & RXERROR_OVERRUN)
898 err = TTY_OVERRUN;
899 else err = 0;
900 for (i = 1; i < urb->actual_length ; ++i)
901 tty_insert_flip_char(tty, data[i], err);
904 else {
905 /* some bytes had errors, every byte has status */
906 dbg("%s - RX error!!!!", __FUNCTION__);
907 for (i = 0; i + 1 < urb->actual_length; i += 2) {
908 int stat = data[i], flag = 0;
909 if (stat & RXERROR_OVERRUN)
910 flag |= TTY_OVERRUN;
911 if (stat & RXERROR_FRAMING)
912 flag |= TTY_FRAME;
913 if (stat & RXERROR_PARITY)
914 flag |= TTY_PARITY;
915 /* XXX should handle break (0x10) */
916 tty_insert_flip_char(tty, data[i+1], flag);
920 tty_flip_buffer_push(tty);
923 /* Resubmit urb so we continue receiving */
924 urb->dev = port->serial->dev;
925 if (port->open_count)
926 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
927 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
929 return;
933 static void usa90_instat_callback(struct urb *urb)
935 unsigned char *data = urb->transfer_buffer;
936 struct keyspan_usa90_portStatusMessage *msg;
937 struct usb_serial *serial;
938 struct usb_serial_port *port;
939 struct keyspan_port_private *p_priv;
940 int old_dcd_state, err;
942 serial = (struct usb_serial *) urb->context;
944 if (urb->status) {
945 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
946 return;
948 if (urb->actual_length < 14) {
949 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
950 goto exit;
953 msg = (struct keyspan_usa90_portStatusMessage *)data;
955 /* Now do something useful with the data */
957 port = serial->port[0];
958 p_priv = usb_get_serial_port_data(port);
960 /* Update handshaking pin state information */
961 old_dcd_state = p_priv->dcd_state;
962 p_priv->cts_state = ((msg->cts) ? 1 : 0);
963 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
964 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
965 p_priv->ri_state = ((msg->ri) ? 1 : 0);
967 if (port->tty && !C_CLOCAL(port->tty)
968 && old_dcd_state != p_priv->dcd_state) {
969 if (old_dcd_state)
970 tty_hangup(port->tty);
971 /* else */
972 /* wake_up_interruptible(&p_priv->open_wait); */
975 /* Resubmit urb so we continue receiving */
976 urb->dev = serial->dev;
977 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
978 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
980 exit:
984 static void usa90_outcont_callback(struct urb *urb)
986 struct usb_serial_port *port;
987 struct keyspan_port_private *p_priv;
989 port = (struct usb_serial_port *) urb->context;
990 p_priv = usb_get_serial_port_data(port);
992 if (p_priv->resend_cont) {
993 dbg ("%s - sending setup", __FUNCTION__);
994 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
998 static int keyspan_write_room (struct usb_serial_port *port)
1000 struct keyspan_port_private *p_priv;
1001 const struct keyspan_device_details *d_details;
1002 int flip;
1003 int data_len;
1004 struct urb *this_urb;
1006 dbg("%s", __FUNCTION__);
1007 p_priv = usb_get_serial_port_data(port);
1008 d_details = p_priv->device_details;
1010 if (d_details->msg_format == msg_usa90)
1011 data_len = 64;
1012 else
1013 data_len = 63;
1015 flip = p_priv->out_flip;
1017 /* Check both endpoints to see if any are available. */
1018 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1019 if (this_urb->status != -EINPROGRESS)
1020 return (data_len);
1021 flip = (flip + 1) & d_details->outdat_endp_flip;
1022 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1023 if (this_urb->status != -EINPROGRESS)
1024 return (data_len);
1026 return (0);
1030 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1032 return (0);
1036 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1038 struct keyspan_port_private *p_priv;
1039 struct keyspan_serial_private *s_priv;
1040 struct usb_serial *serial = port->serial;
1041 const struct keyspan_device_details *d_details;
1042 int i, err;
1043 int baud_rate, device_port;
1044 struct urb *urb;
1045 unsigned int cflag;
1047 s_priv = usb_get_serial_data(serial);
1048 p_priv = usb_get_serial_port_data(port);
1049 d_details = p_priv->device_details;
1051 dbg("%s - port%d.", __FUNCTION__, port->number);
1053 /* Set some sane defaults */
1054 p_priv->rts_state = 1;
1055 p_priv->dtr_state = 1;
1056 p_priv->baud = 9600;
1058 /* force baud and lcr to be set on open */
1059 p_priv->old_baud = 0;
1060 p_priv->old_cflag = 0;
1062 p_priv->out_flip = 0;
1063 p_priv->in_flip = 0;
1065 /* Reset low level data toggle and start reading from endpoints */
1066 for (i = 0; i < 2; i++) {
1067 if ((urb = p_priv->in_urbs[i]) == NULL)
1068 continue;
1069 urb->dev = serial->dev;
1071 /* make sure endpoint data toggle is synchronized with the device */
1073 usb_clear_halt(urb->dev, urb->pipe);
1075 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1076 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1080 /* Reset low level data toggle on out endpoints */
1081 for (i = 0; i < 2; i++) {
1082 if ((urb = p_priv->out_urbs[i]) == NULL)
1083 continue;
1084 urb->dev = serial->dev;
1085 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1088 /* get the terminal config for the setup message now so we don't
1089 * need to send 2 of them */
1091 cflag = port->tty->termios->c_cflag;
1092 device_port = port->number - port->serial->minor;
1094 /* Baud rate calculation takes baud rate as an integer
1095 so other rates can be generated if desired. */
1096 baud_rate = tty_get_baud_rate(port->tty);
1097 /* If no match or invalid, leave as default */
1098 if (baud_rate >= 0
1099 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1100 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1101 p_priv->baud = baud_rate;
1104 /* set CTS/RTS handshake etc. */
1105 p_priv->cflag = cflag;
1106 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1108 keyspan_send_setup(port, 1);
1109 //mdelay(100);
1110 //keyspan_set_termios(port, NULL);
1112 return (0);
1115 static inline void stop_urb(struct urb *urb)
1117 if (urb && urb->status == -EINPROGRESS)
1118 usb_kill_urb(urb);
1121 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1123 int i;
1124 struct usb_serial *serial = port->serial;
1125 struct keyspan_serial_private *s_priv;
1126 struct keyspan_port_private *p_priv;
1128 dbg("%s", __FUNCTION__);
1129 s_priv = usb_get_serial_data(serial);
1130 p_priv = usb_get_serial_port_data(port);
1132 p_priv->rts_state = 0;
1133 p_priv->dtr_state = 0;
1135 if (serial->dev) {
1136 keyspan_send_setup(port, 2);
1137 /* pilot-xfer seems to work best with this delay */
1138 mdelay(100);
1139 // keyspan_set_termios(port, NULL);
1142 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1143 dbg("%s - urb in progress", __FUNCTION__);
1146 p_priv->out_flip = 0;
1147 p_priv->in_flip = 0;
1149 if (serial->dev) {
1150 /* Stop reading/writing urbs */
1151 stop_urb(p_priv->inack_urb);
1152 /* stop_urb(p_priv->outcont_urb); */
1153 for (i = 0; i < 2; i++) {
1154 stop_urb(p_priv->in_urbs[i]);
1155 stop_urb(p_priv->out_urbs[i]);
1158 port->tty = NULL;
1162 /* download the firmware to a pre-renumeration device */
1163 static int keyspan_fake_startup (struct usb_serial *serial)
1165 int response;
1166 const struct ezusb_hex_record *record;
1167 char *fw_name;
1169 dbg("Keyspan startup version %04x product %04x",
1170 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1171 le16_to_cpu(serial->dev->descriptor.idProduct));
1173 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1174 dbg("Firmware already loaded. Quitting.");
1175 return(1);
1178 /* Select firmware image on the basis of idProduct */
1179 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1180 case keyspan_usa28_pre_product_id:
1181 record = &keyspan_usa28_firmware[0];
1182 fw_name = "USA28";
1183 break;
1185 case keyspan_usa28x_pre_product_id:
1186 record = &keyspan_usa28x_firmware[0];
1187 fw_name = "USA28X";
1188 break;
1190 case keyspan_usa28xa_pre_product_id:
1191 record = &keyspan_usa28xa_firmware[0];
1192 fw_name = "USA28XA";
1193 break;
1195 case keyspan_usa28xb_pre_product_id:
1196 record = &keyspan_usa28xb_firmware[0];
1197 fw_name = "USA28XB";
1198 break;
1200 case keyspan_usa19_pre_product_id:
1201 record = &keyspan_usa19_firmware[0];
1202 fw_name = "USA19";
1203 break;
1205 case keyspan_usa19qi_pre_product_id:
1206 record = &keyspan_usa19qi_firmware[0];
1207 fw_name = "USA19QI";
1208 break;
1210 case keyspan_mpr_pre_product_id:
1211 record = &keyspan_mpr_firmware[0];
1212 fw_name = "MPR";
1213 break;
1215 case keyspan_usa19qw_pre_product_id:
1216 record = &keyspan_usa19qw_firmware[0];
1217 fw_name = "USA19QI";
1218 break;
1220 case keyspan_usa18x_pre_product_id:
1221 record = &keyspan_usa18x_firmware[0];
1222 fw_name = "USA18X";
1223 break;
1225 case keyspan_usa19w_pre_product_id:
1226 record = &keyspan_usa19w_firmware[0];
1227 fw_name = "USA19W";
1228 break;
1230 case keyspan_usa49w_pre_product_id:
1231 record = &keyspan_usa49w_firmware[0];
1232 fw_name = "USA49W";
1233 break;
1235 case keyspan_usa49wlc_pre_product_id:
1236 record = &keyspan_usa49wlc_firmware[0];
1237 fw_name = "USA49WLC";
1238 break;
1240 default:
1241 record = NULL;
1242 fw_name = "Unknown";
1243 break;
1246 if (record == NULL) {
1247 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1248 return(1);
1251 dbg("Uploading Keyspan %s firmware.", fw_name);
1253 /* download the firmware image */
1254 response = ezusb_set_reset(serial, 1);
1256 while(record->address != 0xffff) {
1257 response = ezusb_writememory(serial, record->address,
1258 (unsigned char *)record->data,
1259 record->data_size, 0xa0);
1260 if (response < 0) {
1261 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1262 "firmware (%d %04X %p %d)\n",
1263 response,
1264 record->address, record->data, record->data_size);
1265 break;
1267 record++;
1269 /* bring device out of reset. Renumeration will occur in a
1270 moment and the new device will bind to the real driver */
1271 response = ezusb_set_reset(serial, 0);
1273 /* we don't want this device to have a driver assigned to it. */
1274 return (1);
1277 /* Helper functions used by keyspan_setup_urbs */
1278 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1279 int dir, void *ctx, char *buf, int len,
1280 void (*callback)(struct urb *))
1282 struct urb *urb;
1284 if (endpoint == -1)
1285 return NULL; /* endpoint not needed */
1287 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1288 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1289 if (urb == NULL) {
1290 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1291 return NULL;
1294 /* Fill URB using supplied data. */
1295 usb_fill_bulk_urb(urb, serial->dev,
1296 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1297 buf, len, callback, ctx);
1299 return urb;
1302 static struct callbacks {
1303 void (*instat_callback)(struct urb *);
1304 void (*glocont_callback)(struct urb *);
1305 void (*indat_callback)(struct urb *);
1306 void (*outdat_callback)(struct urb *);
1307 void (*inack_callback)(struct urb *);
1308 void (*outcont_callback)(struct urb *);
1309 } keyspan_callbacks[] = {
1311 /* msg_usa26 callbacks */
1312 .instat_callback = usa26_instat_callback,
1313 .glocont_callback = usa26_glocont_callback,
1314 .indat_callback = usa26_indat_callback,
1315 .outdat_callback = usa2x_outdat_callback,
1316 .inack_callback = usa26_inack_callback,
1317 .outcont_callback = usa26_outcont_callback,
1318 }, {
1319 /* msg_usa28 callbacks */
1320 .instat_callback = usa28_instat_callback,
1321 .glocont_callback = usa28_glocont_callback,
1322 .indat_callback = usa28_indat_callback,
1323 .outdat_callback = usa2x_outdat_callback,
1324 .inack_callback = usa28_inack_callback,
1325 .outcont_callback = usa28_outcont_callback,
1326 }, {
1327 /* msg_usa49 callbacks */
1328 .instat_callback = usa49_instat_callback,
1329 .glocont_callback = usa49_glocont_callback,
1330 .indat_callback = usa49_indat_callback,
1331 .outdat_callback = usa2x_outdat_callback,
1332 .inack_callback = usa49_inack_callback,
1333 .outcont_callback = usa49_outcont_callback,
1334 }, {
1335 /* msg_usa90 callbacks */
1336 .instat_callback = usa90_instat_callback,
1337 .glocont_callback = usa28_glocont_callback,
1338 .indat_callback = usa90_indat_callback,
1339 .outdat_callback = usa2x_outdat_callback,
1340 .inack_callback = usa28_inack_callback,
1341 .outcont_callback = usa90_outcont_callback,
1345 /* Generic setup urbs function that uses
1346 data in device_details */
1347 static void keyspan_setup_urbs(struct usb_serial *serial)
1349 int i, j;
1350 struct keyspan_serial_private *s_priv;
1351 const struct keyspan_device_details *d_details;
1352 struct usb_serial_port *port;
1353 struct keyspan_port_private *p_priv;
1354 struct callbacks *cback;
1355 int endp;
1357 dbg ("%s", __FUNCTION__);
1359 s_priv = usb_get_serial_data(serial);
1360 d_details = s_priv->device_details;
1362 /* Setup values for the various callback routines */
1363 cback = &keyspan_callbacks[d_details->msg_format];
1365 /* Allocate and set up urbs for each one that is in use,
1366 starting with instat endpoints */
1367 s_priv->instat_urb = keyspan_setup_urb
1368 (serial, d_details->instat_endpoint, USB_DIR_IN,
1369 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1370 cback->instat_callback);
1372 s_priv->glocont_urb = keyspan_setup_urb
1373 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1374 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1375 cback->glocont_callback);
1377 /* Setup endpoints for each port specific thing */
1378 for (i = 0; i < d_details->num_ports; i ++) {
1379 port = serial->port[i];
1380 p_priv = usb_get_serial_port_data(port);
1382 /* Do indat endpoints first, once for each flip */
1383 endp = d_details->indat_endpoints[i];
1384 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1385 p_priv->in_urbs[j] = keyspan_setup_urb
1386 (serial, endp, USB_DIR_IN, port,
1387 p_priv->in_buffer[j], 64,
1388 cback->indat_callback);
1390 for (; j < 2; ++j)
1391 p_priv->in_urbs[j] = NULL;
1393 /* outdat endpoints also have flip */
1394 endp = d_details->outdat_endpoints[i];
1395 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1396 p_priv->out_urbs[j] = keyspan_setup_urb
1397 (serial, endp, USB_DIR_OUT, port,
1398 p_priv->out_buffer[j], 64,
1399 cback->outdat_callback);
1401 for (; j < 2; ++j)
1402 p_priv->out_urbs[j] = NULL;
1404 /* inack endpoint */
1405 p_priv->inack_urb = keyspan_setup_urb
1406 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1407 port, p_priv->inack_buffer, 1, cback->inack_callback);
1409 /* outcont endpoint */
1410 p_priv->outcont_urb = keyspan_setup_urb
1411 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1412 port, p_priv->outcont_buffer, 64,
1413 cback->outcont_callback);
1418 /* usa19 function doesn't require prescaler */
1419 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1420 u8 *rate_low, u8 *prescaler, int portnum)
1422 u32 b16, /* baud rate times 16 (actual rate used internally) */
1423 div, /* divisor */
1424 cnt; /* inverse of divisor (programmed into 8051) */
1426 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1428 /* prevent divide by zero... */
1429 if( (b16 = (baud_rate * 16L)) == 0) {
1430 return (KEYSPAN_INVALID_BAUD_RATE);
1433 /* Any "standard" rate over 57k6 is marginal on the USA-19
1434 as we run out of divisor resolution. */
1435 if (baud_rate > 57600) {
1436 return (KEYSPAN_INVALID_BAUD_RATE);
1439 /* calculate the divisor and the counter (its inverse) */
1440 if( (div = (baudclk / b16)) == 0) {
1441 return (KEYSPAN_INVALID_BAUD_RATE);
1443 else {
1444 cnt = 0 - div;
1447 if(div > 0xffff) {
1448 return (KEYSPAN_INVALID_BAUD_RATE);
1451 /* return the counter values if non-null */
1452 if (rate_low) {
1453 *rate_low = (u8) (cnt & 0xff);
1455 if (rate_hi) {
1456 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1458 if (rate_low && rate_hi) {
1459 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1462 return (KEYSPAN_BAUD_RATE_OK);
1465 /* usa19hs function doesn't require prescaler */
1466 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1467 u8 *rate_low, u8 *prescaler, int portnum)
1469 u32 b16, /* baud rate times 16 (actual rate used internally) */
1470 div; /* divisor */
1472 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1474 /* prevent divide by zero... */
1475 if( (b16 = (baud_rate * 16L)) == 0)
1476 return (KEYSPAN_INVALID_BAUD_RATE);
1480 /* calculate the divisor */
1481 if( (div = (baudclk / b16)) == 0)
1482 return (KEYSPAN_INVALID_BAUD_RATE);
1484 if(div > 0xffff)
1485 return (KEYSPAN_INVALID_BAUD_RATE);
1487 /* return the counter values if non-null */
1488 if (rate_low)
1489 *rate_low = (u8) (div & 0xff);
1491 if (rate_hi)
1492 *rate_hi = (u8) ((div >> 8) & 0xff);
1494 if (rate_low && rate_hi)
1495 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1497 return (KEYSPAN_BAUD_RATE_OK);
1500 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1501 u8 *rate_low, u8 *prescaler, int portnum)
1503 u32 b16, /* baud rate times 16 (actual rate used internally) */
1504 clk, /* clock with 13/8 prescaler */
1505 div, /* divisor using 13/8 prescaler */
1506 res, /* resulting baud rate using 13/8 prescaler */
1507 diff, /* error using 13/8 prescaler */
1508 smallest_diff;
1509 u8 best_prescaler;
1510 int i;
1512 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1514 /* prevent divide by zero */
1515 if( (b16 = baud_rate * 16L) == 0) {
1516 return (KEYSPAN_INVALID_BAUD_RATE);
1519 /* Calculate prescaler by trying them all and looking
1520 for best fit */
1522 /* start with largest possible difference */
1523 smallest_diff = 0xffffffff;
1525 /* 0 is an invalid prescaler, used as a flag */
1526 best_prescaler = 0;
1528 for(i = 8; i <= 0xff; ++i) {
1529 clk = (baudclk * 8) / (u32) i;
1531 if( (div = clk / b16) == 0) {
1532 continue;
1535 res = clk / div;
1536 diff= (res > b16) ? (res-b16) : (b16-res);
1538 if(diff < smallest_diff) {
1539 best_prescaler = i;
1540 smallest_diff = diff;
1544 if(best_prescaler == 0) {
1545 return (KEYSPAN_INVALID_BAUD_RATE);
1548 clk = (baudclk * 8) / (u32) best_prescaler;
1549 div = clk / b16;
1551 /* return the divisor and prescaler if non-null */
1552 if (rate_low) {
1553 *rate_low = (u8) (div & 0xff);
1555 if (rate_hi) {
1556 *rate_hi = (u8) ((div >> 8) & 0xff);
1558 if (prescaler) {
1559 *prescaler = best_prescaler;
1560 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1562 return (KEYSPAN_BAUD_RATE_OK);
1565 /* USA-28 supports different maximum baud rates on each port */
1566 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1567 u8 *rate_low, u8 *prescaler, int portnum)
1569 u32 b16, /* baud rate times 16 (actual rate used internally) */
1570 div, /* divisor */
1571 cnt; /* inverse of divisor (programmed into 8051) */
1573 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1575 /* prevent divide by zero */
1576 if ((b16 = baud_rate * 16L) == 0)
1577 return (KEYSPAN_INVALID_BAUD_RATE);
1579 /* calculate the divisor and the counter (its inverse) */
1580 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1581 return (KEYSPAN_INVALID_BAUD_RATE);
1583 else {
1584 cnt = 0 - div;
1587 /* check for out of range, based on portnum,
1588 and return result */
1589 if(portnum == 0) {
1590 if(div > 0xffff)
1591 return (KEYSPAN_INVALID_BAUD_RATE);
1593 else {
1594 if(portnum == 1) {
1595 if(div > 0xff) {
1596 return (KEYSPAN_INVALID_BAUD_RATE);
1599 else {
1600 return (KEYSPAN_INVALID_BAUD_RATE);
1604 /* return the counter values if not NULL
1605 (port 1 will ignore retHi) */
1606 if (rate_low) {
1607 *rate_low = (u8) (cnt & 0xff);
1609 if (rate_hi) {
1610 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1612 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1613 return (KEYSPAN_BAUD_RATE_OK);
1616 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1617 struct usb_serial_port *port,
1618 int reset_port)
1620 struct keyspan_usa26_portControlMessage msg;
1621 struct keyspan_serial_private *s_priv;
1622 struct keyspan_port_private *p_priv;
1623 const struct keyspan_device_details *d_details;
1624 int outcont_urb;
1625 struct urb *this_urb;
1626 int device_port, err;
1628 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1630 s_priv = usb_get_serial_data(serial);
1631 p_priv = usb_get_serial_port_data(port);
1632 d_details = s_priv->device_details;
1633 device_port = port->number - port->serial->minor;
1635 outcont_urb = d_details->outcont_endpoints[port->number];
1636 this_urb = p_priv->outcont_urb;
1638 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1640 /* Make sure we have an urb then send the message */
1641 if (this_urb == NULL) {
1642 dbg("%s - oops no urb.", __FUNCTION__);
1643 return -1;
1646 /* Save reset port val for resend.
1647 Don't overwrite resend for close condition. */
1648 if (p_priv->resend_cont != 3)
1649 p_priv->resend_cont = reset_port + 1;
1650 if (this_urb->status == -EINPROGRESS) {
1651 /* dbg ("%s - already writing", __FUNCTION__); */
1652 mdelay(5);
1653 return(-1);
1656 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1658 /* Only set baud rate if it's changed */
1659 if (p_priv->old_baud != p_priv->baud) {
1660 p_priv->old_baud = p_priv->baud;
1661 msg.setClocking = 0xff;
1662 if (d_details->calculate_baud_rate
1663 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1664 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1665 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1666 p_priv->baud);
1667 msg.baudLo = 0;
1668 msg.baudHi = 125; /* Values for 9600 baud */
1669 msg.prescaler = 10;
1671 msg.setPrescaler = 0xff;
1674 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1675 switch (p_priv->cflag & CSIZE) {
1676 case CS5:
1677 msg.lcr |= USA_DATABITS_5;
1678 break;
1679 case CS6:
1680 msg.lcr |= USA_DATABITS_6;
1681 break;
1682 case CS7:
1683 msg.lcr |= USA_DATABITS_7;
1684 break;
1685 case CS8:
1686 msg.lcr |= USA_DATABITS_8;
1687 break;
1689 if (p_priv->cflag & PARENB) {
1690 /* note USA_PARITY_NONE == 0 */
1691 msg.lcr |= (p_priv->cflag & PARODD)?
1692 USA_PARITY_ODD: USA_PARITY_EVEN;
1694 msg.setLcr = 0xff;
1696 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1697 msg.xonFlowControl = 0;
1698 msg.setFlowControl = 0xff;
1699 msg.forwardingLength = 16;
1700 msg.xonChar = 17;
1701 msg.xoffChar = 19;
1703 /* Opening port */
1704 if (reset_port == 1) {
1705 msg._txOn = 1;
1706 msg._txOff = 0;
1707 msg.txFlush = 0;
1708 msg.txBreak = 0;
1709 msg.rxOn = 1;
1710 msg.rxOff = 0;
1711 msg.rxFlush = 1;
1712 msg.rxForward = 0;
1713 msg.returnStatus = 0;
1714 msg.resetDataToggle = 0xff;
1717 /* Closing port */
1718 else if (reset_port == 2) {
1719 msg._txOn = 0;
1720 msg._txOff = 1;
1721 msg.txFlush = 0;
1722 msg.txBreak = 0;
1723 msg.rxOn = 0;
1724 msg.rxOff = 1;
1725 msg.rxFlush = 1;
1726 msg.rxForward = 0;
1727 msg.returnStatus = 0;
1728 msg.resetDataToggle = 0;
1731 /* Sending intermediate configs */
1732 else {
1733 msg._txOn = (! p_priv->break_on);
1734 msg._txOff = 0;
1735 msg.txFlush = 0;
1736 msg.txBreak = (p_priv->break_on);
1737 msg.rxOn = 0;
1738 msg.rxOff = 0;
1739 msg.rxFlush = 0;
1740 msg.rxForward = 0;
1741 msg.returnStatus = 0;
1742 msg.resetDataToggle = 0x0;
1745 /* Do handshaking outputs */
1746 msg.setTxTriState_setRts = 0xff;
1747 msg.txTriState_rts = p_priv->rts_state;
1749 msg.setHskoa_setDtr = 0xff;
1750 msg.hskoa_dtr = p_priv->dtr_state;
1752 p_priv->resend_cont = 0;
1753 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1755 /* send the data out the device on control endpoint */
1756 this_urb->transfer_buffer_length = sizeof(msg);
1758 this_urb->dev = serial->dev;
1759 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1760 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1762 #if 0
1763 else {
1764 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1765 outcont_urb, this_urb->transfer_buffer_length,
1766 usb_pipeendpoint(this_urb->pipe));
1768 #endif
1770 return (0);
1773 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1774 struct usb_serial_port *port,
1775 int reset_port)
1777 struct keyspan_usa28_portControlMessage msg;
1778 struct keyspan_serial_private *s_priv;
1779 struct keyspan_port_private *p_priv;
1780 const struct keyspan_device_details *d_details;
1781 struct urb *this_urb;
1782 int device_port, err;
1784 dbg ("%s", __FUNCTION__);
1786 s_priv = usb_get_serial_data(serial);
1787 p_priv = usb_get_serial_port_data(port);
1788 d_details = s_priv->device_details;
1789 device_port = port->number - port->serial->minor;
1791 /* only do something if we have a bulk out endpoint */
1792 if ((this_urb = p_priv->outcont_urb) == NULL) {
1793 dbg("%s - oops no urb.", __FUNCTION__);
1794 return -1;
1797 /* Save reset port val for resend.
1798 Don't overwrite resend for close condition. */
1799 if (p_priv->resend_cont != 3)
1800 p_priv->resend_cont = reset_port + 1;
1801 if (this_urb->status == -EINPROGRESS) {
1802 dbg ("%s already writing", __FUNCTION__);
1803 mdelay(5);
1804 return(-1);
1807 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
1809 msg.setBaudRate = 1;
1810 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1811 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1812 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
1813 msg.baudLo = 0xff;
1814 msg.baudHi = 0xb2; /* Values for 9600 baud */
1817 /* If parity is enabled, we must calculate it ourselves. */
1818 msg.parity = 0; /* XXX for now */
1820 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1821 msg.xonFlowControl = 0;
1823 /* Do handshaking outputs, DTR is inverted relative to RTS */
1824 msg.rts = p_priv->rts_state;
1825 msg.dtr = p_priv->dtr_state;
1827 msg.forwardingLength = 16;
1828 msg.forwardMs = 10;
1829 msg.breakThreshold = 45;
1830 msg.xonChar = 17;
1831 msg.xoffChar = 19;
1833 /*msg.returnStatus = 1;
1834 msg.resetDataToggle = 0xff;*/
1835 /* Opening port */
1836 if (reset_port == 1) {
1837 msg._txOn = 1;
1838 msg._txOff = 0;
1839 msg.txFlush = 0;
1840 msg.txForceXoff = 0;
1841 msg.txBreak = 0;
1842 msg.rxOn = 1;
1843 msg.rxOff = 0;
1844 msg.rxFlush = 1;
1845 msg.rxForward = 0;
1846 msg.returnStatus = 0;
1847 msg.resetDataToggle = 0xff;
1849 /* Closing port */
1850 else if (reset_port == 2) {
1851 msg._txOn = 0;
1852 msg._txOff = 1;
1853 msg.txFlush = 0;
1854 msg.txForceXoff = 0;
1855 msg.txBreak = 0;
1856 msg.rxOn = 0;
1857 msg.rxOff = 1;
1858 msg.rxFlush = 1;
1859 msg.rxForward = 0;
1860 msg.returnStatus = 0;
1861 msg.resetDataToggle = 0;
1863 /* Sending intermediate configs */
1864 else {
1865 msg._txOn = (! p_priv->break_on);
1866 msg._txOff = 0;
1867 msg.txFlush = 0;
1868 msg.txForceXoff = 0;
1869 msg.txBreak = (p_priv->break_on);
1870 msg.rxOn = 0;
1871 msg.rxOff = 0;
1872 msg.rxFlush = 0;
1873 msg.rxForward = 0;
1874 msg.returnStatus = 0;
1875 msg.resetDataToggle = 0x0;
1878 p_priv->resend_cont = 0;
1879 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1881 /* send the data out the device on control endpoint */
1882 this_urb->transfer_buffer_length = sizeof(msg);
1884 this_urb->dev = serial->dev;
1885 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1886 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
1888 #if 0
1889 else {
1890 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
1891 this_urb->transfer_buffer_length);
1893 #endif
1895 return (0);
1898 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1899 struct usb_serial_port *port,
1900 int reset_port)
1902 struct keyspan_usa49_portControlMessage msg;
1903 struct keyspan_serial_private *s_priv;
1904 struct keyspan_port_private *p_priv;
1905 const struct keyspan_device_details *d_details;
1906 int glocont_urb;
1907 struct urb *this_urb;
1908 int err, device_port;
1910 dbg ("%s", __FUNCTION__);
1912 s_priv = usb_get_serial_data(serial);
1913 p_priv = usb_get_serial_port_data(port);
1914 d_details = s_priv->device_details;
1916 glocont_urb = d_details->glocont_endpoint;
1917 this_urb = s_priv->glocont_urb;
1919 /* Work out which port within the device is being setup */
1920 device_port = port->number - port->serial->minor;
1922 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
1924 /* Make sure we have an urb then send the message */
1925 if (this_urb == NULL) {
1926 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
1927 return -1;
1930 /* Save reset port val for resend.
1931 Don't overwrite resend for close condition. */
1932 if (p_priv->resend_cont != 3)
1933 p_priv->resend_cont = reset_port + 1;
1934 if (this_urb->status == -EINPROGRESS) {
1935 /* dbg ("%s - already writing", __FUNCTION__); */
1936 mdelay(5);
1937 return(-1);
1940 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
1942 /*msg.portNumber = port->number;*/
1943 msg.portNumber = device_port;
1945 /* Only set baud rate if it's changed */
1946 if (p_priv->old_baud != p_priv->baud) {
1947 p_priv->old_baud = p_priv->baud;
1948 msg.setClocking = 0xff;
1949 if (d_details->calculate_baud_rate
1950 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1951 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1952 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1953 p_priv->baud);
1954 msg.baudLo = 0;
1955 msg.baudHi = 125; /* Values for 9600 baud */
1956 msg.prescaler = 10;
1958 //msg.setPrescaler = 0xff;
1961 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1962 switch (p_priv->cflag & CSIZE) {
1963 case CS5:
1964 msg.lcr |= USA_DATABITS_5;
1965 break;
1966 case CS6:
1967 msg.lcr |= USA_DATABITS_6;
1968 break;
1969 case CS7:
1970 msg.lcr |= USA_DATABITS_7;
1971 break;
1972 case CS8:
1973 msg.lcr |= USA_DATABITS_8;
1974 break;
1976 if (p_priv->cflag & PARENB) {
1977 /* note USA_PARITY_NONE == 0 */
1978 msg.lcr |= (p_priv->cflag & PARODD)?
1979 USA_PARITY_ODD: USA_PARITY_EVEN;
1981 msg.setLcr = 0xff;
1983 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1984 msg.xonFlowControl = 0;
1985 msg.setFlowControl = 0xff;
1987 msg.forwardingLength = 16;
1988 msg.xonChar = 17;
1989 msg.xoffChar = 19;
1991 /* Opening port */
1992 if (reset_port == 1) {
1993 msg._txOn = 1;
1994 msg._txOff = 0;
1995 msg.txFlush = 0;
1996 msg.txBreak = 0;
1997 msg.rxOn = 1;
1998 msg.rxOff = 0;
1999 msg.rxFlush = 1;
2000 msg.rxForward = 0;
2001 msg.returnStatus = 0;
2002 msg.resetDataToggle = 0xff;
2003 msg.enablePort = 1;
2004 msg.disablePort = 0;
2006 /* Closing port */
2007 else if (reset_port == 2) {
2008 msg._txOn = 0;
2009 msg._txOff = 1;
2010 msg.txFlush = 0;
2011 msg.txBreak = 0;
2012 msg.rxOn = 0;
2013 msg.rxOff = 1;
2014 msg.rxFlush = 1;
2015 msg.rxForward = 0;
2016 msg.returnStatus = 0;
2017 msg.resetDataToggle = 0;
2018 msg.enablePort = 0;
2019 msg.disablePort = 1;
2021 /* Sending intermediate configs */
2022 else {
2023 msg._txOn = (! p_priv->break_on);
2024 msg._txOff = 0;
2025 msg.txFlush = 0;
2026 msg.txBreak = (p_priv->break_on);
2027 msg.rxOn = 0;
2028 msg.rxOff = 0;
2029 msg.rxFlush = 0;
2030 msg.rxForward = 0;
2031 msg.returnStatus = 0;
2032 msg.resetDataToggle = 0x0;
2033 msg.enablePort = 0;
2034 msg.disablePort = 0;
2037 /* Do handshaking outputs */
2038 msg.setRts = 0xff;
2039 msg.rts = p_priv->rts_state;
2041 msg.setDtr = 0xff;
2042 msg.dtr = p_priv->dtr_state;
2044 p_priv->resend_cont = 0;
2045 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2047 /* send the data out the device on control endpoint */
2048 this_urb->transfer_buffer_length = sizeof(msg);
2050 this_urb->dev = serial->dev;
2051 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2052 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2054 #if 0
2055 else {
2056 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2057 outcont_urb, this_urb->transfer_buffer_length,
2058 usb_pipeendpoint(this_urb->pipe));
2060 #endif
2062 return (0);
2065 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2066 struct usb_serial_port *port,
2067 int reset_port)
2069 struct keyspan_usa90_portControlMessage msg;
2070 struct keyspan_serial_private *s_priv;
2071 struct keyspan_port_private *p_priv;
2072 const struct keyspan_device_details *d_details;
2073 struct urb *this_urb;
2074 int err;
2075 u8 prescaler;
2077 dbg ("%s", __FUNCTION__);
2079 s_priv = usb_get_serial_data(serial);
2080 p_priv = usb_get_serial_port_data(port);
2081 d_details = s_priv->device_details;
2083 /* only do something if we have a bulk out endpoint */
2084 if ((this_urb = p_priv->outcont_urb) == NULL) {
2085 dbg("%s - oops no urb.", __FUNCTION__);
2086 return -1;
2089 /* Save reset port val for resend.
2090 Don't overwrite resend for open/close condition. */
2091 if ((reset_port + 1) > p_priv->resend_cont)
2092 p_priv->resend_cont = reset_port + 1;
2093 if (this_urb->status == -EINPROGRESS) {
2094 dbg ("%s already writing", __FUNCTION__);
2095 mdelay(5);
2096 return(-1);
2099 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2101 /* Only set baud rate if it's changed */
2102 if (p_priv->old_baud != p_priv->baud) {
2103 p_priv->old_baud = p_priv->baud;
2104 msg.setClocking = 0x01;
2105 if (d_details->calculate_baud_rate
2106 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2107 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2108 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2109 p_priv->baud);
2110 p_priv->baud = 9600;
2111 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2112 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2114 msg.setRxMode = 1;
2115 msg.setTxMode = 1;
2118 /* modes must always be correctly specified */
2119 if (p_priv->baud > 57600)
2121 msg.rxMode = RXMODE_DMA;
2122 msg.txMode = TXMODE_DMA;
2124 else
2126 msg.rxMode = RXMODE_BYHAND;
2127 msg.txMode = TXMODE_BYHAND;
2130 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2131 switch (p_priv->cflag & CSIZE) {
2132 case CS5:
2133 msg.lcr |= USA_DATABITS_5;
2134 break;
2135 case CS6:
2136 msg.lcr |= USA_DATABITS_6;
2137 break;
2138 case CS7:
2139 msg.lcr |= USA_DATABITS_7;
2140 break;
2141 case CS8:
2142 msg.lcr |= USA_DATABITS_8;
2143 break;
2145 if (p_priv->cflag & PARENB) {
2146 /* note USA_PARITY_NONE == 0 */
2147 msg.lcr |= (p_priv->cflag & PARODD)?
2148 USA_PARITY_ODD: USA_PARITY_EVEN;
2150 if (p_priv->old_cflag != p_priv->cflag) {
2151 p_priv->old_cflag = p_priv->cflag;
2152 msg.setLcr = 0x01;
2155 if (p_priv->flow_control == flow_cts)
2156 msg.txFlowControl = TXFLOW_CTS;
2157 msg.setTxFlowControl = 0x01;
2158 msg.setRxFlowControl = 0x01;
2160 msg.rxForwardingLength = 16;
2161 msg.rxForwardingTimeout = 16;
2162 msg.txAckSetting = 0;
2163 msg.xonChar = 17;
2164 msg.xoffChar = 19;
2166 /* Opening port */
2167 if (reset_port == 1) {
2168 msg.portEnabled = 1;
2169 msg.rxFlush = 1;
2170 msg.txBreak = (p_priv->break_on);
2172 /* Closing port */
2173 else if (reset_port == 2) {
2174 msg.portEnabled = 0;
2176 /* Sending intermediate configs */
2177 else {
2178 if (port->open_count)
2179 msg.portEnabled = 1;
2180 msg.txBreak = (p_priv->break_on);
2183 /* Do handshaking outputs */
2184 msg.setRts = 0x01;
2185 msg.rts = p_priv->rts_state;
2187 msg.setDtr = 0x01;
2188 msg.dtr = p_priv->dtr_state;
2190 p_priv->resend_cont = 0;
2191 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2193 /* send the data out the device on control endpoint */
2194 this_urb->transfer_buffer_length = sizeof(msg);
2196 this_urb->dev = serial->dev;
2197 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2198 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2200 return (0);
2203 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2205 struct usb_serial *serial = port->serial;
2206 struct keyspan_serial_private *s_priv;
2207 const struct keyspan_device_details *d_details;
2209 dbg ("%s", __FUNCTION__);
2211 s_priv = usb_get_serial_data(serial);
2212 d_details = s_priv->device_details;
2214 switch (d_details->msg_format) {
2215 case msg_usa26:
2216 keyspan_usa26_send_setup(serial, port, reset_port);
2217 break;
2218 case msg_usa28:
2219 keyspan_usa28_send_setup(serial, port, reset_port);
2220 break;
2221 case msg_usa49:
2222 keyspan_usa49_send_setup(serial, port, reset_port);
2223 break;
2224 case msg_usa90:
2225 keyspan_usa90_send_setup(serial, port, reset_port);
2226 break;
2231 /* Gets called by the "real" driver (ie once firmware is loaded
2232 and renumeration has taken place. */
2233 static int keyspan_startup (struct usb_serial *serial)
2235 int i, err;
2236 struct usb_serial_port *port;
2237 struct keyspan_serial_private *s_priv;
2238 struct keyspan_port_private *p_priv;
2239 const struct keyspan_device_details *d_details;
2241 dbg("%s", __FUNCTION__);
2243 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2244 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2245 break;
2246 if (d_details == NULL) {
2247 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
2248 return 1;
2251 /* Setup private data for serial driver */
2252 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2253 if (!s_priv) {
2254 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2255 return -ENOMEM;
2258 s_priv->device_details = d_details;
2259 usb_set_serial_data(serial, s_priv);
2261 /* Now setup per port private data */
2262 for (i = 0; i < serial->num_ports; i++) {
2263 port = serial->port[i];
2264 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2265 if (!p_priv) {
2266 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2267 return (1);
2269 p_priv->device_details = d_details;
2270 usb_set_serial_port_data(port, p_priv);
2273 keyspan_setup_urbs(serial);
2275 s_priv->instat_urb->dev = serial->dev;
2276 if ((err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL)) != 0) {
2277 dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
2280 return (0);
2283 static void keyspan_shutdown (struct usb_serial *serial)
2285 int i, j;
2286 struct usb_serial_port *port;
2287 struct keyspan_serial_private *s_priv;
2288 struct keyspan_port_private *p_priv;
2290 dbg("%s", __FUNCTION__);
2292 s_priv = usb_get_serial_data(serial);
2294 /* Stop reading/writing urbs */
2295 stop_urb(s_priv->instat_urb);
2296 stop_urb(s_priv->glocont_urb);
2297 for (i = 0; i < serial->num_ports; ++i) {
2298 port = serial->port[i];
2299 p_priv = usb_get_serial_port_data(port);
2300 stop_urb(p_priv->inack_urb);
2301 stop_urb(p_priv->outcont_urb);
2302 for (j = 0; j < 2; j++) {
2303 stop_urb(p_priv->in_urbs[j]);
2304 stop_urb(p_priv->out_urbs[j]);
2308 /* Now free them */
2309 if (s_priv->instat_urb)
2310 usb_free_urb(s_priv->instat_urb);
2311 if (s_priv->glocont_urb)
2312 usb_free_urb(s_priv->glocont_urb);
2313 for (i = 0; i < serial->num_ports; ++i) {
2314 port = serial->port[i];
2315 p_priv = usb_get_serial_port_data(port);
2316 if (p_priv->inack_urb)
2317 usb_free_urb(p_priv->inack_urb);
2318 if (p_priv->outcont_urb)
2319 usb_free_urb(p_priv->outcont_urb);
2320 for (j = 0; j < 2; j++) {
2321 if (p_priv->in_urbs[j])
2322 usb_free_urb(p_priv->in_urbs[j]);
2323 if (p_priv->out_urbs[j])
2324 usb_free_urb(p_priv->out_urbs[j]);
2328 /* dbg("Freeing serial->private."); */
2329 kfree(s_priv);
2331 /* dbg("Freeing port->private."); */
2332 /* Now free per port private data */
2333 for (i = 0; i < serial->num_ports; i++) {
2334 port = serial->port[i];
2335 kfree(usb_get_serial_port_data(port));
2339 MODULE_AUTHOR( DRIVER_AUTHOR );
2340 MODULE_DESCRIPTION( DRIVER_DESC );
2341 MODULE_LICENSE("GPL");
2343 module_param(debug, bool, S_IRUGO | S_IWUSR);
2344 MODULE_PARM_DESC(debug, "Debug enabled or not");