i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / usb / serial / keyspan.c
blob4cc36c76180126f80a9d81e23759318971873599
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://blemings.org/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.
31 #include <linux/kernel.h>
32 #include <linux/jiffies.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/tty.h>
37 #include <linux/tty_driver.h>
38 #include <linux/tty_flip.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/firmware.h>
42 #include <linux/ihex.h>
43 #include <linux/uaccess.h>
44 #include <linux/usb.h>
45 #include <linux/usb/serial.h>
46 #include "keyspan.h"
48 static bool debug;
51 * Version Information
53 #define DRIVER_VERSION "v1.1.5"
54 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
55 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
57 #define INSTAT_BUFLEN 32
58 #define GLOCONT_BUFLEN 64
59 #define INDAT49W_BUFLEN 512
61 /* Per device and per port private data */
62 struct keyspan_serial_private {
63 const struct keyspan_device_details *device_details;
65 struct urb *instat_urb;
66 char instat_buf[INSTAT_BUFLEN];
68 /* added to support 49wg, where data from all 4 ports comes in
69 on 1 EP and high-speed supported */
70 struct urb *indat_urb;
71 char indat_buf[INDAT49W_BUFLEN];
73 /* XXX this one probably will need a lock */
74 struct urb *glocont_urb;
75 char glocont_buf[GLOCONT_BUFLEN];
76 char ctrl_buf[8]; /* for EP0 control message */
79 struct keyspan_port_private {
80 /* Keep track of which input & output endpoints to use */
81 int in_flip;
82 int out_flip;
84 /* Keep duplicate of device details in each port
85 structure as well - simplifies some of the
86 callback functions etc. */
87 const struct keyspan_device_details *device_details;
89 /* Input endpoints and buffer for this port */
90 struct urb *in_urbs[2];
91 char in_buffer[2][64];
92 /* Output endpoints and buffer for this port */
93 struct urb *out_urbs[2];
94 char out_buffer[2][64];
96 /* Input ack endpoint */
97 struct urb *inack_urb;
98 char inack_buffer[1];
100 /* Output control endpoint */
101 struct urb *outcont_urb;
102 char outcont_buffer[64];
104 /* Settings for the port */
105 int baud;
106 int old_baud;
107 unsigned int cflag;
108 unsigned int old_cflag;
109 enum {flow_none, flow_cts, flow_xon} flow_control;
110 int rts_state; /* Handshaking pins (outputs) */
111 int dtr_state;
112 int cts_state; /* Handshaking pins (inputs) */
113 int dsr_state;
114 int dcd_state;
115 int ri_state;
116 int break_on;
118 unsigned long tx_start_time[2];
119 int resend_cont; /* need to resend control packet */
122 /* Include Keyspan message headers. All current Keyspan Adapters
123 make use of one of five message formats which are referred
124 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
125 within this driver. */
126 #include "keyspan_usa26msg.h"
127 #include "keyspan_usa28msg.h"
128 #include "keyspan_usa49msg.h"
129 #include "keyspan_usa90msg.h"
130 #include "keyspan_usa67msg.h"
133 /* Functions used by new usb-serial code. */
134 static int __init keyspan_init(void)
136 int retval;
137 retval = usb_serial_register(&keyspan_pre_device);
138 if (retval)
139 goto failed_pre_device_register;
140 retval = usb_serial_register(&keyspan_1port_device);
141 if (retval)
142 goto failed_1port_device_register;
143 retval = usb_serial_register(&keyspan_2port_device);
144 if (retval)
145 goto failed_2port_device_register;
146 retval = usb_serial_register(&keyspan_4port_device);
147 if (retval)
148 goto failed_4port_device_register;
149 retval = usb_register(&keyspan_driver);
150 if (retval)
151 goto failed_usb_register;
153 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
154 DRIVER_DESC "\n");
156 return 0;
157 failed_usb_register:
158 usb_serial_deregister(&keyspan_4port_device);
159 failed_4port_device_register:
160 usb_serial_deregister(&keyspan_2port_device);
161 failed_2port_device_register:
162 usb_serial_deregister(&keyspan_1port_device);
163 failed_1port_device_register:
164 usb_serial_deregister(&keyspan_pre_device);
165 failed_pre_device_register:
166 return retval;
169 static void __exit keyspan_exit(void)
171 usb_deregister(&keyspan_driver);
172 usb_serial_deregister(&keyspan_pre_device);
173 usb_serial_deregister(&keyspan_1port_device);
174 usb_serial_deregister(&keyspan_2port_device);
175 usb_serial_deregister(&keyspan_4port_device);
178 module_init(keyspan_init);
179 module_exit(keyspan_exit);
181 static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
183 struct usb_serial_port *port = tty->driver_data;
184 struct keyspan_port_private *p_priv;
186 dbg("%s", __func__);
188 p_priv = usb_get_serial_port_data(port);
190 if (break_state == -1)
191 p_priv->break_on = 1;
192 else
193 p_priv->break_on = 0;
195 keyspan_send_setup(port, 0);
199 static void keyspan_set_termios(struct tty_struct *tty,
200 struct usb_serial_port *port, struct ktermios *old_termios)
202 int baud_rate, device_port;
203 struct keyspan_port_private *p_priv;
204 const struct keyspan_device_details *d_details;
205 unsigned int cflag;
207 dbg("%s", __func__);
209 p_priv = usb_get_serial_port_data(port);
210 d_details = p_priv->device_details;
211 cflag = tty->termios->c_cflag;
212 device_port = port->number - port->serial->minor;
214 /* Baud rate calculation takes baud rate as an integer
215 so other rates can be generated if desired. */
216 baud_rate = tty_get_baud_rate(tty);
217 /* If no match or invalid, don't change */
218 if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
219 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
220 /* FIXME - more to do here to ensure rate changes cleanly */
221 /* FIXME - calcuate exact rate from divisor ? */
222 p_priv->baud = baud_rate;
223 } else
224 baud_rate = tty_termios_baud_rate(old_termios);
226 tty_encode_baud_rate(tty, baud_rate, baud_rate);
227 /* set CTS/RTS handshake etc. */
228 p_priv->cflag = cflag;
229 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
231 /* Mark/Space not supported */
232 tty->termios->c_cflag &= ~CMSPAR;
234 keyspan_send_setup(port, 0);
237 static int keyspan_tiocmget(struct tty_struct *tty)
239 struct usb_serial_port *port = tty->driver_data;
240 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
241 unsigned int value;
243 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
244 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
245 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
246 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
247 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
248 ((p_priv->ri_state) ? TIOCM_RNG : 0);
250 return value;
253 static int keyspan_tiocmset(struct tty_struct *tty,
254 unsigned int set, unsigned int clear)
256 struct usb_serial_port *port = tty->driver_data;
257 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
259 if (set & TIOCM_RTS)
260 p_priv->rts_state = 1;
261 if (set & TIOCM_DTR)
262 p_priv->dtr_state = 1;
263 if (clear & TIOCM_RTS)
264 p_priv->rts_state = 0;
265 if (clear & TIOCM_DTR)
266 p_priv->dtr_state = 0;
267 keyspan_send_setup(port, 0);
268 return 0;
271 /* Write function is similar for the four protocols used
272 with only a minor change for usa90 (usa19hs) required */
273 static int keyspan_write(struct tty_struct *tty,
274 struct usb_serial_port *port, const unsigned char *buf, int count)
276 struct keyspan_port_private *p_priv;
277 const struct keyspan_device_details *d_details;
278 int flip;
279 int left, todo;
280 struct urb *this_urb;
281 int err, maxDataLen, dataOffset;
283 p_priv = usb_get_serial_port_data(port);
284 d_details = p_priv->device_details;
286 if (d_details->msg_format == msg_usa90) {
287 maxDataLen = 64;
288 dataOffset = 0;
289 } else {
290 maxDataLen = 63;
291 dataOffset = 1;
294 dbg("%s - for port %d (%d chars), flip=%d",
295 __func__, port->number, count, p_priv->out_flip);
297 for (left = count; left > 0; left -= todo) {
298 todo = left;
299 if (todo > maxDataLen)
300 todo = maxDataLen;
302 flip = p_priv->out_flip;
304 /* Check we have a valid urb/endpoint before we use it... */
305 this_urb = p_priv->out_urbs[flip];
306 if (this_urb == NULL) {
307 /* no bulk out, so return 0 bytes written */
308 dbg("%s - no output urb :(", __func__);
309 return count;
312 dbg("%s - endpoint %d flip %d",
313 __func__, usb_pipeendpoint(this_urb->pipe), flip);
315 if (this_urb->status == -EINPROGRESS) {
316 if (time_before(jiffies,
317 p_priv->tx_start_time[flip] + 10 * HZ))
318 break;
319 usb_unlink_urb(this_urb);
320 break;
323 /* First byte in buffer is "last flag" (except for usa19hx)
324 - unused so for now so set to zero */
325 ((char *)this_urb->transfer_buffer)[0] = 0;
327 memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
328 buf += todo;
330 /* send the data out the bulk port */
331 this_urb->transfer_buffer_length = todo + dataOffset;
333 err = usb_submit_urb(this_urb, GFP_ATOMIC);
334 if (err != 0)
335 dbg("usb_submit_urb(write bulk) failed (%d)", err);
336 p_priv->tx_start_time[flip] = jiffies;
338 /* Flip for next time if usa26 or usa28 interface
339 (not used on usa49) */
340 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
343 return count - left;
346 static void usa26_indat_callback(struct urb *urb)
348 int i, err;
349 int endpoint;
350 struct usb_serial_port *port;
351 struct tty_struct *tty;
352 unsigned char *data = urb->transfer_buffer;
353 int status = urb->status;
355 dbg("%s", __func__);
357 endpoint = usb_pipeendpoint(urb->pipe);
359 if (status) {
360 dbg("%s - nonzero status: %x on endpoint %d.",
361 __func__, status, endpoint);
362 return;
365 port = urb->context;
366 tty = tty_port_tty_get(&port->port);
367 if (tty && urb->actual_length) {
368 /* 0x80 bit is error flag */
369 if ((data[0] & 0x80) == 0) {
370 /* no errors on individual bytes, only
371 possible overrun err */
372 if (data[0] & RXERROR_OVERRUN)
373 err = TTY_OVERRUN;
374 else
375 err = 0;
376 for (i = 1; i < urb->actual_length ; ++i)
377 tty_insert_flip_char(tty, data[i], err);
378 } else {
379 /* some bytes had errors, every byte has status */
380 dbg("%s - RX error!!!!", __func__);
381 for (i = 0; i + 1 < urb->actual_length; i += 2) {
382 int stat = data[i], flag = 0;
383 if (stat & RXERROR_OVERRUN)
384 flag |= TTY_OVERRUN;
385 if (stat & RXERROR_FRAMING)
386 flag |= TTY_FRAME;
387 if (stat & RXERROR_PARITY)
388 flag |= TTY_PARITY;
389 /* XXX should handle break (0x10) */
390 tty_insert_flip_char(tty, data[i+1], flag);
393 tty_flip_buffer_push(tty);
395 tty_kref_put(tty);
397 /* Resubmit urb so we continue receiving */
398 err = usb_submit_urb(urb, GFP_ATOMIC);
399 if (err != 0)
400 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
403 /* Outdat handling is common for all devices */
404 static void usa2x_outdat_callback(struct urb *urb)
406 struct usb_serial_port *port;
407 struct keyspan_port_private *p_priv;
409 port = urb->context;
410 p_priv = usb_get_serial_port_data(port);
411 dbg("%s - urb %d", __func__, urb == p_priv->out_urbs[1]);
413 usb_serial_port_softint(port);
416 static void usa26_inack_callback(struct urb *urb)
418 dbg("%s", __func__);
422 static void usa26_outcont_callback(struct urb *urb)
424 struct usb_serial_port *port;
425 struct keyspan_port_private *p_priv;
427 port = urb->context;
428 p_priv = usb_get_serial_port_data(port);
430 if (p_priv->resend_cont) {
431 dbg("%s - sending setup", __func__);
432 keyspan_usa26_send_setup(port->serial, port,
433 p_priv->resend_cont - 1);
437 static void usa26_instat_callback(struct urb *urb)
439 unsigned char *data = urb->transfer_buffer;
440 struct keyspan_usa26_portStatusMessage *msg;
441 struct usb_serial *serial;
442 struct usb_serial_port *port;
443 struct keyspan_port_private *p_priv;
444 struct tty_struct *tty;
445 int old_dcd_state, err;
446 int status = urb->status;
448 serial = urb->context;
450 if (status) {
451 dbg("%s - nonzero status: %x", __func__, status);
452 return;
454 if (urb->actual_length != 9) {
455 dbg("%s - %d byte report??", __func__, urb->actual_length);
456 goto exit;
459 msg = (struct keyspan_usa26_portStatusMessage *)data;
461 #if 0
462 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
463 __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
464 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
465 #endif
467 /* Now do something useful with the data */
470 /* Check port number from message and retrieve private data */
471 if (msg->port >= serial->num_ports) {
472 dbg("%s - Unexpected port number %d", __func__, msg->port);
473 goto exit;
475 port = serial->port[msg->port];
476 p_priv = usb_get_serial_port_data(port);
478 /* Update handshaking pin state information */
479 old_dcd_state = p_priv->dcd_state;
480 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
481 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
482 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
483 p_priv->ri_state = ((msg->ri) ? 1 : 0);
485 if (old_dcd_state != p_priv->dcd_state) {
486 tty = tty_port_tty_get(&port->port);
487 if (tty && !C_CLOCAL(tty))
488 tty_hangup(tty);
489 tty_kref_put(tty);
492 /* Resubmit urb so we continue receiving */
493 err = usb_submit_urb(urb, GFP_ATOMIC);
494 if (err != 0)
495 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
496 exit: ;
499 static void usa26_glocont_callback(struct urb *urb)
501 dbg("%s", __func__);
505 static void usa28_indat_callback(struct urb *urb)
507 int err;
508 struct usb_serial_port *port;
509 struct tty_struct *tty;
510 unsigned char *data;
511 struct keyspan_port_private *p_priv;
512 int status = urb->status;
514 dbg("%s", __func__);
516 port = urb->context;
517 p_priv = usb_get_serial_port_data(port);
518 data = urb->transfer_buffer;
520 if (urb != p_priv->in_urbs[p_priv->in_flip])
521 return;
523 do {
524 if (status) {
525 dbg("%s - nonzero status: %x on endpoint %d.",
526 __func__, status, usb_pipeendpoint(urb->pipe));
527 return;
530 port = urb->context;
531 p_priv = usb_get_serial_port_data(port);
532 data = urb->transfer_buffer;
534 tty =tty_port_tty_get(&port->port);
535 if (tty && urb->actual_length) {
536 tty_insert_flip_string(tty, data, urb->actual_length);
537 tty_flip_buffer_push(tty);
539 tty_kref_put(tty);
541 /* Resubmit urb so we continue receiving */
542 err = usb_submit_urb(urb, GFP_ATOMIC);
543 if (err != 0)
544 dbg("%s - resubmit read urb failed. (%d)",
545 __func__, err);
546 p_priv->in_flip ^= 1;
548 urb = p_priv->in_urbs[p_priv->in_flip];
549 } while (urb->status != -EINPROGRESS);
552 static void usa28_inack_callback(struct urb *urb)
554 dbg("%s", __func__);
557 static void usa28_outcont_callback(struct urb *urb)
559 struct usb_serial_port *port;
560 struct keyspan_port_private *p_priv;
562 port = urb->context;
563 p_priv = usb_get_serial_port_data(port);
565 if (p_priv->resend_cont) {
566 dbg("%s - sending setup", __func__);
567 keyspan_usa28_send_setup(port->serial, port,
568 p_priv->resend_cont - 1);
572 static void usa28_instat_callback(struct urb *urb)
574 int err;
575 unsigned char *data = urb->transfer_buffer;
576 struct keyspan_usa28_portStatusMessage *msg;
577 struct usb_serial *serial;
578 struct usb_serial_port *port;
579 struct keyspan_port_private *p_priv;
580 struct tty_struct *tty;
581 int old_dcd_state;
582 int status = urb->status;
584 serial = urb->context;
586 if (status) {
587 dbg("%s - nonzero status: %x", __func__, status);
588 return;
591 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
592 dbg("%s - bad length %d", __func__, urb->actual_length);
593 goto exit;
596 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
597 data[0], data[1], data[2], data[3], data[4], data[5],
598 data[6], data[7], data[8], data[9], data[10], data[11]);*/
600 /* Now do something useful with the data */
601 msg = (struct keyspan_usa28_portStatusMessage *)data;
603 /* Check port number from message and retrieve private data */
604 if (msg->port >= serial->num_ports) {
605 dbg("%s - Unexpected port number %d", __func__, msg->port);
606 goto exit;
608 port = serial->port[msg->port];
609 p_priv = usb_get_serial_port_data(port);
611 /* Update handshaking pin state information */
612 old_dcd_state = p_priv->dcd_state;
613 p_priv->cts_state = ((msg->cts) ? 1 : 0);
614 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
615 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
616 p_priv->ri_state = ((msg->ri) ? 1 : 0);
618 if( old_dcd_state != p_priv->dcd_state && old_dcd_state) {
619 tty = tty_port_tty_get(&port->port);
620 if (tty && !C_CLOCAL(tty))
621 tty_hangup(tty);
622 tty_kref_put(tty);
625 /* Resubmit urb so we continue receiving */
626 err = usb_submit_urb(urb, GFP_ATOMIC);
627 if (err != 0)
628 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
629 exit: ;
632 static void usa28_glocont_callback(struct urb *urb)
634 dbg("%s", __func__);
638 static void usa49_glocont_callback(struct urb *urb)
640 struct usb_serial *serial;
641 struct usb_serial_port *port;
642 struct keyspan_port_private *p_priv;
643 int i;
645 dbg("%s", __func__);
647 serial = urb->context;
648 for (i = 0; i < serial->num_ports; ++i) {
649 port = serial->port[i];
650 p_priv = usb_get_serial_port_data(port);
652 if (p_priv->resend_cont) {
653 dbg("%s - sending setup", __func__);
654 keyspan_usa49_send_setup(serial, port,
655 p_priv->resend_cont - 1);
656 break;
661 /* This is actually called glostat in the Keyspan
662 doco */
663 static void usa49_instat_callback(struct urb *urb)
665 int err;
666 unsigned char *data = urb->transfer_buffer;
667 struct keyspan_usa49_portStatusMessage *msg;
668 struct usb_serial *serial;
669 struct usb_serial_port *port;
670 struct keyspan_port_private *p_priv;
671 int old_dcd_state;
672 int status = urb->status;
674 dbg("%s", __func__);
676 serial = urb->context;
678 if (status) {
679 dbg("%s - nonzero status: %x", __func__, status);
680 return;
683 if (urb->actual_length !=
684 sizeof(struct keyspan_usa49_portStatusMessage)) {
685 dbg("%s - bad length %d", __func__, urb->actual_length);
686 goto exit;
689 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
690 data[0], data[1], data[2], data[3], data[4], data[5],
691 data[6], data[7], data[8], data[9], data[10]);*/
693 /* Now do something useful with the data */
694 msg = (struct keyspan_usa49_portStatusMessage *)data;
696 /* Check port number from message and retrieve private data */
697 if (msg->portNumber >= serial->num_ports) {
698 dbg("%s - Unexpected port number %d",
699 __func__, msg->portNumber);
700 goto exit;
702 port = serial->port[msg->portNumber];
703 p_priv = usb_get_serial_port_data(port);
705 /* Update handshaking pin state information */
706 old_dcd_state = p_priv->dcd_state;
707 p_priv->cts_state = ((msg->cts) ? 1 : 0);
708 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
709 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
710 p_priv->ri_state = ((msg->ri) ? 1 : 0);
712 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
713 struct tty_struct *tty = tty_port_tty_get(&port->port);
714 if (tty && !C_CLOCAL(tty))
715 tty_hangup(tty);
716 tty_kref_put(tty);
719 /* Resubmit urb so we continue receiving */
720 err = usb_submit_urb(urb, GFP_ATOMIC);
721 if (err != 0)
722 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
723 exit: ;
726 static void usa49_inack_callback(struct urb *urb)
728 dbg("%s", __func__);
731 static void usa49_indat_callback(struct urb *urb)
733 int i, err;
734 int endpoint;
735 struct usb_serial_port *port;
736 struct tty_struct *tty;
737 unsigned char *data = urb->transfer_buffer;
738 int status = urb->status;
740 dbg("%s", __func__);
742 endpoint = usb_pipeendpoint(urb->pipe);
744 if (status) {
745 dbg("%s - nonzero status: %x on endpoint %d.", __func__,
746 status, endpoint);
747 return;
750 port = urb->context;
751 tty = tty_port_tty_get(&port->port);
752 if (tty && urb->actual_length) {
753 /* 0x80 bit is error flag */
754 if ((data[0] & 0x80) == 0) {
755 /* no error on any byte */
756 tty_insert_flip_string(tty, data + 1,
757 urb->actual_length - 1);
758 } else {
759 /* some bytes had errors, every byte has status */
760 for (i = 0; i + 1 < urb->actual_length; i += 2) {
761 int stat = data[i], flag = 0;
762 if (stat & RXERROR_OVERRUN)
763 flag |= TTY_OVERRUN;
764 if (stat & RXERROR_FRAMING)
765 flag |= TTY_FRAME;
766 if (stat & RXERROR_PARITY)
767 flag |= TTY_PARITY;
768 /* XXX should handle break (0x10) */
769 tty_insert_flip_char(tty, data[i+1], flag);
772 tty_flip_buffer_push(tty);
774 tty_kref_put(tty);
776 /* Resubmit urb so we continue receiving */
777 err = usb_submit_urb(urb, GFP_ATOMIC);
778 if (err != 0)
779 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
782 static void usa49wg_indat_callback(struct urb *urb)
784 int i, len, x, err;
785 struct usb_serial *serial;
786 struct usb_serial_port *port;
787 struct tty_struct *tty;
788 unsigned char *data = urb->transfer_buffer;
789 int status = urb->status;
791 dbg("%s", __func__);
793 serial = urb->context;
795 if (status) {
796 dbg("%s - nonzero status: %x", __func__, status);
797 return;
800 /* inbound data is in the form P#, len, status, data */
801 i = 0;
802 len = 0;
804 if (urb->actual_length) {
805 while (i < urb->actual_length) {
807 /* Check port number from message*/
808 if (data[i] >= serial->num_ports) {
809 dbg("%s - Unexpected port number %d",
810 __func__, data[i]);
811 return;
813 port = serial->port[data[i++]];
814 tty = tty_port_tty_get(&port->port);
815 len = data[i++];
817 /* 0x80 bit is error flag */
818 if ((data[i] & 0x80) == 0) {
819 /* no error on any byte */
820 i++;
821 for (x = 1; x < len ; ++x)
822 tty_insert_flip_char(tty, data[i++], 0);
823 } else {
825 * some bytes had errors, every byte has status
827 for (x = 0; x + 1 < len; x += 2) {
828 int stat = data[i], flag = 0;
829 if (stat & RXERROR_OVERRUN)
830 flag |= TTY_OVERRUN;
831 if (stat & RXERROR_FRAMING)
832 flag |= TTY_FRAME;
833 if (stat & RXERROR_PARITY)
834 flag |= TTY_PARITY;
835 /* XXX should handle break (0x10) */
836 tty_insert_flip_char(tty,
837 data[i+1], flag);
838 i += 2;
841 tty_flip_buffer_push(tty);
842 tty_kref_put(tty);
846 /* Resubmit urb so we continue receiving */
847 err = usb_submit_urb(urb, GFP_ATOMIC);
848 if (err != 0)
849 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
852 /* not used, usa-49 doesn't have per-port control endpoints */
853 static void usa49_outcont_callback(struct urb *urb)
855 dbg("%s", __func__);
858 static void usa90_indat_callback(struct urb *urb)
860 int i, err;
861 int endpoint;
862 struct usb_serial_port *port;
863 struct keyspan_port_private *p_priv;
864 struct tty_struct *tty;
865 unsigned char *data = urb->transfer_buffer;
866 int status = urb->status;
868 dbg("%s", __func__);
870 endpoint = usb_pipeendpoint(urb->pipe);
872 if (status) {
873 dbg("%s - nonzero status: %x on endpoint %d.",
874 __func__, status, endpoint);
875 return;
878 port = urb->context;
879 p_priv = usb_get_serial_port_data(port);
881 if (urb->actual_length) {
882 tty = tty_port_tty_get(&port->port);
883 /* if current mode is DMA, looks like usa28 format
884 otherwise looks like usa26 data format */
886 if (p_priv->baud > 57600)
887 tty_insert_flip_string(tty, data, urb->actual_length);
888 else {
889 /* 0x80 bit is error flag */
890 if ((data[0] & 0x80) == 0) {
891 /* no errors on individual bytes, only
892 possible overrun err*/
893 if (data[0] & RXERROR_OVERRUN)
894 err = TTY_OVERRUN;
895 else
896 err = 0;
897 for (i = 1; i < urb->actual_length ; ++i)
898 tty_insert_flip_char(tty, data[i],
899 err);
900 } else {
901 /* some bytes had errors, every byte has status */
902 dbg("%s - RX error!!!!", __func__);
903 for (i = 0; i + 1 < urb->actual_length; i += 2) {
904 int stat = data[i], flag = 0;
905 if (stat & RXERROR_OVERRUN)
906 flag |= TTY_OVERRUN;
907 if (stat & RXERROR_FRAMING)
908 flag |= TTY_FRAME;
909 if (stat & RXERROR_PARITY)
910 flag |= TTY_PARITY;
911 /* XXX should handle break (0x10) */
912 tty_insert_flip_char(tty, data[i+1],
913 flag);
917 tty_flip_buffer_push(tty);
918 tty_kref_put(tty);
921 /* Resubmit urb so we continue receiving */
922 err = usb_submit_urb(urb, GFP_ATOMIC);
923 if (err != 0)
924 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
928 static void usa90_instat_callback(struct urb *urb)
930 unsigned char *data = urb->transfer_buffer;
931 struct keyspan_usa90_portStatusMessage *msg;
932 struct usb_serial *serial;
933 struct usb_serial_port *port;
934 struct keyspan_port_private *p_priv;
935 struct tty_struct *tty;
936 int old_dcd_state, err;
937 int status = urb->status;
939 serial = urb->context;
941 if (status) {
942 dbg("%s - nonzero status: %x", __func__, status);
943 return;
945 if (urb->actual_length < 14) {
946 dbg("%s - %d byte report??", __func__, urb->actual_length);
947 goto exit;
950 msg = (struct keyspan_usa90_portStatusMessage *)data;
952 /* Now do something useful with the data */
954 port = serial->port[0];
955 p_priv = usb_get_serial_port_data(port);
957 /* Update handshaking pin state information */
958 old_dcd_state = p_priv->dcd_state;
959 p_priv->cts_state = ((msg->cts) ? 1 : 0);
960 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
961 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
962 p_priv->ri_state = ((msg->ri) ? 1 : 0);
964 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
965 tty = tty_port_tty_get(&port->port);
966 if (tty && !C_CLOCAL(tty))
967 tty_hangup(tty);
968 tty_kref_put(tty);
971 /* Resubmit urb so we continue receiving */
972 err = usb_submit_urb(urb, GFP_ATOMIC);
973 if (err != 0)
974 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
975 exit:
979 static void usa90_outcont_callback(struct urb *urb)
981 struct usb_serial_port *port;
982 struct keyspan_port_private *p_priv;
984 port = urb->context;
985 p_priv = usb_get_serial_port_data(port);
987 if (p_priv->resend_cont) {
988 dbg("%s - sending setup", __func__);
989 keyspan_usa90_send_setup(port->serial, port,
990 p_priv->resend_cont - 1);
994 /* Status messages from the 28xg */
995 static void usa67_instat_callback(struct urb *urb)
997 int err;
998 unsigned char *data = urb->transfer_buffer;
999 struct keyspan_usa67_portStatusMessage *msg;
1000 struct usb_serial *serial;
1001 struct usb_serial_port *port;
1002 struct keyspan_port_private *p_priv;
1003 int old_dcd_state;
1004 int status = urb->status;
1006 dbg("%s", __func__);
1008 serial = urb->context;
1010 if (status) {
1011 dbg("%s - nonzero status: %x", __func__, status);
1012 return;
1015 if (urb->actual_length !=
1016 sizeof(struct keyspan_usa67_portStatusMessage)) {
1017 dbg("%s - bad length %d", __func__, urb->actual_length);
1018 return;
1022 /* Now do something useful with the data */
1023 msg = (struct keyspan_usa67_portStatusMessage *)data;
1025 /* Check port number from message and retrieve private data */
1026 if (msg->port >= serial->num_ports) {
1027 dbg("%s - Unexpected port number %d", __func__, msg->port);
1028 return;
1031 port = serial->port[msg->port];
1032 p_priv = usb_get_serial_port_data(port);
1034 /* Update handshaking pin state information */
1035 old_dcd_state = p_priv->dcd_state;
1036 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1037 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1039 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
1040 struct tty_struct *tty = tty_port_tty_get(&port->port);
1041 if (tty && !C_CLOCAL(tty))
1042 tty_hangup(tty);
1043 tty_kref_put(tty);
1046 /* Resubmit urb so we continue receiving */
1047 err = usb_submit_urb(urb, GFP_ATOMIC);
1048 if (err != 0)
1049 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1052 static void usa67_glocont_callback(struct urb *urb)
1054 struct usb_serial *serial;
1055 struct usb_serial_port *port;
1056 struct keyspan_port_private *p_priv;
1057 int i;
1059 dbg("%s", __func__);
1061 serial = urb->context;
1062 for (i = 0; i < serial->num_ports; ++i) {
1063 port = serial->port[i];
1064 p_priv = usb_get_serial_port_data(port);
1066 if (p_priv->resend_cont) {
1067 dbg("%s - sending setup", __func__);
1068 keyspan_usa67_send_setup(serial, port,
1069 p_priv->resend_cont - 1);
1070 break;
1075 static int keyspan_write_room(struct tty_struct *tty)
1077 struct usb_serial_port *port = tty->driver_data;
1078 struct keyspan_port_private *p_priv;
1079 const struct keyspan_device_details *d_details;
1080 int flip;
1081 int data_len;
1082 struct urb *this_urb;
1084 dbg("%s", __func__);
1085 p_priv = usb_get_serial_port_data(port);
1086 d_details = p_priv->device_details;
1088 /* FIXME: locking */
1089 if (d_details->msg_format == msg_usa90)
1090 data_len = 64;
1091 else
1092 data_len = 63;
1094 flip = p_priv->out_flip;
1096 /* Check both endpoints to see if any are available. */
1097 this_urb = p_priv->out_urbs[flip];
1098 if (this_urb != NULL) {
1099 if (this_urb->status != -EINPROGRESS)
1100 return data_len;
1101 flip = (flip + 1) & d_details->outdat_endp_flip;
1102 this_urb = p_priv->out_urbs[flip];
1103 if (this_urb != NULL) {
1104 if (this_urb->status != -EINPROGRESS)
1105 return data_len;
1108 return 0;
1112 static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port)
1114 struct keyspan_port_private *p_priv;
1115 struct keyspan_serial_private *s_priv;
1116 struct usb_serial *serial = port->serial;
1117 const struct keyspan_device_details *d_details;
1118 int i, err;
1119 int baud_rate, device_port;
1120 struct urb *urb;
1121 unsigned int cflag = 0;
1123 s_priv = usb_get_serial_data(serial);
1124 p_priv = usb_get_serial_port_data(port);
1125 d_details = p_priv->device_details;
1127 dbg("%s - port%d.", __func__, port->number);
1129 /* Set some sane defaults */
1130 p_priv->rts_state = 1;
1131 p_priv->dtr_state = 1;
1132 p_priv->baud = 9600;
1134 /* force baud and lcr to be set on open */
1135 p_priv->old_baud = 0;
1136 p_priv->old_cflag = 0;
1138 p_priv->out_flip = 0;
1139 p_priv->in_flip = 0;
1141 /* Reset low level data toggle and start reading from endpoints */
1142 for (i = 0; i < 2; i++) {
1143 urb = p_priv->in_urbs[i];
1144 if (urb == NULL)
1145 continue;
1147 /* make sure endpoint data toggle is synchronized
1148 with the device */
1149 usb_clear_halt(urb->dev, urb->pipe);
1150 err = usb_submit_urb(urb, GFP_KERNEL);
1151 if (err != 0)
1152 dbg("%s - submit urb %d failed (%d)",
1153 __func__, i, err);
1156 /* Reset low level data toggle on out endpoints */
1157 for (i = 0; i < 2; i++) {
1158 urb = p_priv->out_urbs[i];
1159 if (urb == NULL)
1160 continue;
1161 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1162 usb_pipeout(urb->pipe), 0); */
1165 /* get the terminal config for the setup message now so we don't
1166 * need to send 2 of them */
1168 device_port = port->number - port->serial->minor;
1169 if (tty) {
1170 cflag = tty->termios->c_cflag;
1171 /* Baud rate calculation takes baud rate as an integer
1172 so other rates can be generated if desired. */
1173 baud_rate = tty_get_baud_rate(tty);
1174 /* If no match or invalid, leave as default */
1175 if (baud_rate >= 0
1176 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1177 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1178 p_priv->baud = baud_rate;
1181 /* set CTS/RTS handshake etc. */
1182 p_priv->cflag = cflag;
1183 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1185 keyspan_send_setup(port, 1);
1186 /* mdelay(100); */
1187 /* keyspan_set_termios(port, NULL); */
1189 return 0;
1192 static inline void stop_urb(struct urb *urb)
1194 if (urb && urb->status == -EINPROGRESS)
1195 usb_kill_urb(urb);
1198 static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
1200 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
1202 p_priv->rts_state = on;
1203 p_priv->dtr_state = on;
1204 keyspan_send_setup(port, 0);
1207 static void keyspan_close(struct usb_serial_port *port)
1209 int i;
1210 struct usb_serial *serial = port->serial;
1211 struct keyspan_serial_private *s_priv;
1212 struct keyspan_port_private *p_priv;
1214 dbg("%s", __func__);
1215 s_priv = usb_get_serial_data(serial);
1216 p_priv = usb_get_serial_port_data(port);
1218 p_priv->rts_state = 0;
1219 p_priv->dtr_state = 0;
1221 if (serial->dev) {
1222 keyspan_send_setup(port, 2);
1223 /* pilot-xfer seems to work best with this delay */
1224 mdelay(100);
1225 /* keyspan_set_termios(port, NULL); */
1228 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1229 dbg("%s - urb in progress", __func__);
1232 p_priv->out_flip = 0;
1233 p_priv->in_flip = 0;
1235 if (serial->dev) {
1236 /* Stop reading/writing urbs */
1237 stop_urb(p_priv->inack_urb);
1238 /* stop_urb(p_priv->outcont_urb); */
1239 for (i = 0; i < 2; i++) {
1240 stop_urb(p_priv->in_urbs[i]);
1241 stop_urb(p_priv->out_urbs[i]);
1246 /* download the firmware to a pre-renumeration device */
1247 static int keyspan_fake_startup(struct usb_serial *serial)
1249 int response;
1250 const struct ihex_binrec *record;
1251 char *fw_name;
1252 const struct firmware *fw;
1254 dbg("Keyspan startup version %04x product %04x",
1255 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1256 le16_to_cpu(serial->dev->descriptor.idProduct));
1258 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
1259 != 0x8000) {
1260 dbg("Firmware already loaded. Quitting.");
1261 return 1;
1264 /* Select firmware image on the basis of idProduct */
1265 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1266 case keyspan_usa28_pre_product_id:
1267 fw_name = "keyspan/usa28.fw";
1268 break;
1270 case keyspan_usa28x_pre_product_id:
1271 fw_name = "keyspan/usa28x.fw";
1272 break;
1274 case keyspan_usa28xa_pre_product_id:
1275 fw_name = "keyspan/usa28xa.fw";
1276 break;
1278 case keyspan_usa28xb_pre_product_id:
1279 fw_name = "keyspan/usa28xb.fw";
1280 break;
1282 case keyspan_usa19_pre_product_id:
1283 fw_name = "keyspan/usa19.fw";
1284 break;
1286 case keyspan_usa19qi_pre_product_id:
1287 fw_name = "keyspan/usa19qi.fw";
1288 break;
1290 case keyspan_mpr_pre_product_id:
1291 fw_name = "keyspan/mpr.fw";
1292 break;
1294 case keyspan_usa19qw_pre_product_id:
1295 fw_name = "keyspan/usa19qw.fw";
1296 break;
1298 case keyspan_usa18x_pre_product_id:
1299 fw_name = "keyspan/usa18x.fw";
1300 break;
1302 case keyspan_usa19w_pre_product_id:
1303 fw_name = "keyspan/usa19w.fw";
1304 break;
1306 case keyspan_usa49w_pre_product_id:
1307 fw_name = "keyspan/usa49w.fw";
1308 break;
1310 case keyspan_usa49wlc_pre_product_id:
1311 fw_name = "keyspan/usa49wlc.fw";
1312 break;
1314 default:
1315 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1316 le16_to_cpu(serial->dev->descriptor.idProduct));
1317 return 1;
1320 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
1321 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1322 return(1);
1325 dbg("Uploading Keyspan %s firmware.", fw_name);
1327 /* download the firmware image */
1328 response = ezusb_set_reset(serial, 1);
1330 record = (const struct ihex_binrec *)fw->data;
1332 while (record) {
1333 response = ezusb_writememory(serial, be32_to_cpu(record->addr),
1334 (unsigned char *)record->data,
1335 be16_to_cpu(record->len), 0xa0);
1336 if (response < 0) {
1337 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
1338 response, be32_to_cpu(record->addr),
1339 record->data, be16_to_cpu(record->len));
1340 break;
1342 record = ihex_next_binrec(record);
1344 release_firmware(fw);
1345 /* bring device out of reset. Renumeration will occur in a
1346 moment and the new device will bind to the real driver */
1347 response = ezusb_set_reset(serial, 0);
1349 /* we don't want this device to have a driver assigned to it. */
1350 return 1;
1353 /* Helper functions used by keyspan_setup_urbs */
1354 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1355 int endpoint)
1357 struct usb_host_interface *iface_desc;
1358 struct usb_endpoint_descriptor *ep;
1359 int i;
1361 iface_desc = serial->interface->cur_altsetting;
1362 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1363 ep = &iface_desc->endpoint[i].desc;
1364 if (ep->bEndpointAddress == endpoint)
1365 return ep;
1367 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1368 "endpoint %x\n", endpoint);
1369 return NULL;
1372 static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
1373 int dir, void *ctx, char *buf, int len,
1374 void (*callback)(struct urb *))
1376 struct urb *urb;
1377 struct usb_endpoint_descriptor const *ep_desc;
1378 char const *ep_type_name;
1380 if (endpoint == -1)
1381 return NULL; /* endpoint not needed */
1383 dbg("%s - alloc for endpoint %d.", __func__, endpoint);
1384 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1385 if (urb == NULL) {
1386 dbg("%s - alloc for endpoint %d failed.", __func__, endpoint);
1387 return NULL;
1390 if (endpoint == 0) {
1391 /* control EP filled in when used */
1392 return urb;
1395 ep_desc = find_ep(serial, endpoint);
1396 if (!ep_desc) {
1397 /* leak the urb, something's wrong and the callers don't care */
1398 return urb;
1400 if (usb_endpoint_xfer_int(ep_desc)) {
1401 ep_type_name = "INT";
1402 usb_fill_int_urb(urb, serial->dev,
1403 usb_sndintpipe(serial->dev, endpoint) | dir,
1404 buf, len, callback, ctx,
1405 ep_desc->bInterval);
1406 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1407 ep_type_name = "BULK";
1408 usb_fill_bulk_urb(urb, serial->dev,
1409 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1410 buf, len, callback, ctx);
1411 } else {
1412 dev_warn(&serial->interface->dev,
1413 "unsupported endpoint type %x\n",
1414 usb_endpoint_type(ep_desc));
1415 usb_free_urb(urb);
1416 return NULL;
1419 dbg("%s - using urb %p for %s endpoint %x",
1420 __func__, urb, ep_type_name, endpoint);
1421 return urb;
1424 static struct callbacks {
1425 void (*instat_callback)(struct urb *);
1426 void (*glocont_callback)(struct urb *);
1427 void (*indat_callback)(struct urb *);
1428 void (*outdat_callback)(struct urb *);
1429 void (*inack_callback)(struct urb *);
1430 void (*outcont_callback)(struct urb *);
1431 } keyspan_callbacks[] = {
1433 /* msg_usa26 callbacks */
1434 .instat_callback = usa26_instat_callback,
1435 .glocont_callback = usa26_glocont_callback,
1436 .indat_callback = usa26_indat_callback,
1437 .outdat_callback = usa2x_outdat_callback,
1438 .inack_callback = usa26_inack_callback,
1439 .outcont_callback = usa26_outcont_callback,
1440 }, {
1441 /* msg_usa28 callbacks */
1442 .instat_callback = usa28_instat_callback,
1443 .glocont_callback = usa28_glocont_callback,
1444 .indat_callback = usa28_indat_callback,
1445 .outdat_callback = usa2x_outdat_callback,
1446 .inack_callback = usa28_inack_callback,
1447 .outcont_callback = usa28_outcont_callback,
1448 }, {
1449 /* msg_usa49 callbacks */
1450 .instat_callback = usa49_instat_callback,
1451 .glocont_callback = usa49_glocont_callback,
1452 .indat_callback = usa49_indat_callback,
1453 .outdat_callback = usa2x_outdat_callback,
1454 .inack_callback = usa49_inack_callback,
1455 .outcont_callback = usa49_outcont_callback,
1456 }, {
1457 /* msg_usa90 callbacks */
1458 .instat_callback = usa90_instat_callback,
1459 .glocont_callback = usa28_glocont_callback,
1460 .indat_callback = usa90_indat_callback,
1461 .outdat_callback = usa2x_outdat_callback,
1462 .inack_callback = usa28_inack_callback,
1463 .outcont_callback = usa90_outcont_callback,
1464 }, {
1465 /* msg_usa67 callbacks */
1466 .instat_callback = usa67_instat_callback,
1467 .glocont_callback = usa67_glocont_callback,
1468 .indat_callback = usa26_indat_callback,
1469 .outdat_callback = usa2x_outdat_callback,
1470 .inack_callback = usa26_inack_callback,
1471 .outcont_callback = usa26_outcont_callback,
1475 /* Generic setup urbs function that uses
1476 data in device_details */
1477 static void keyspan_setup_urbs(struct usb_serial *serial)
1479 int i, j;
1480 struct keyspan_serial_private *s_priv;
1481 const struct keyspan_device_details *d_details;
1482 struct usb_serial_port *port;
1483 struct keyspan_port_private *p_priv;
1484 struct callbacks *cback;
1485 int endp;
1487 dbg("%s", __func__);
1489 s_priv = usb_get_serial_data(serial);
1490 d_details = s_priv->device_details;
1492 /* Setup values for the various callback routines */
1493 cback = &keyspan_callbacks[d_details->msg_format];
1495 /* Allocate and set up urbs for each one that is in use,
1496 starting with instat endpoints */
1497 s_priv->instat_urb = keyspan_setup_urb
1498 (serial, d_details->instat_endpoint, USB_DIR_IN,
1499 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1500 cback->instat_callback);
1502 s_priv->indat_urb = keyspan_setup_urb
1503 (serial, d_details->indat_endpoint, USB_DIR_IN,
1504 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1505 usa49wg_indat_callback);
1507 s_priv->glocont_urb = keyspan_setup_urb
1508 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1509 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1510 cback->glocont_callback);
1512 /* Setup endpoints for each port specific thing */
1513 for (i = 0; i < d_details->num_ports; i++) {
1514 port = serial->port[i];
1515 p_priv = usb_get_serial_port_data(port);
1517 /* Do indat endpoints first, once for each flip */
1518 endp = d_details->indat_endpoints[i];
1519 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1520 p_priv->in_urbs[j] = keyspan_setup_urb
1521 (serial, endp, USB_DIR_IN, port,
1522 p_priv->in_buffer[j], 64,
1523 cback->indat_callback);
1525 for (; j < 2; ++j)
1526 p_priv->in_urbs[j] = NULL;
1528 /* outdat endpoints also have flip */
1529 endp = d_details->outdat_endpoints[i];
1530 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1531 p_priv->out_urbs[j] = keyspan_setup_urb
1532 (serial, endp, USB_DIR_OUT, port,
1533 p_priv->out_buffer[j], 64,
1534 cback->outdat_callback);
1536 for (; j < 2; ++j)
1537 p_priv->out_urbs[j] = NULL;
1539 /* inack endpoint */
1540 p_priv->inack_urb = keyspan_setup_urb
1541 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1542 port, p_priv->inack_buffer, 1, cback->inack_callback);
1544 /* outcont endpoint */
1545 p_priv->outcont_urb = keyspan_setup_urb
1546 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1547 port, p_priv->outcont_buffer, 64,
1548 cback->outcont_callback);
1552 /* usa19 function doesn't require prescaler */
1553 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1554 u8 *rate_low, u8 *prescaler, int portnum)
1556 u32 b16, /* baud rate times 16 (actual rate used internally) */
1557 div, /* divisor */
1558 cnt; /* inverse of divisor (programmed into 8051) */
1560 dbg("%s - %d.", __func__, baud_rate);
1562 /* prevent divide by zero... */
1563 b16 = baud_rate * 16L;
1564 if (b16 == 0)
1565 return KEYSPAN_INVALID_BAUD_RATE;
1566 /* Any "standard" rate over 57k6 is marginal on the USA-19
1567 as we run out of divisor resolution. */
1568 if (baud_rate > 57600)
1569 return KEYSPAN_INVALID_BAUD_RATE;
1571 /* calculate the divisor and the counter (its inverse) */
1572 div = baudclk / b16;
1573 if (div == 0)
1574 return KEYSPAN_INVALID_BAUD_RATE;
1575 else
1576 cnt = 0 - div;
1578 if (div > 0xffff)
1579 return KEYSPAN_INVALID_BAUD_RATE;
1581 /* return the counter values if non-null */
1582 if (rate_low)
1583 *rate_low = (u8) (cnt & 0xff);
1584 if (rate_hi)
1585 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1586 if (rate_low && rate_hi)
1587 dbg("%s - %d %02x %02x.",
1588 __func__, baud_rate, *rate_hi, *rate_low);
1589 return KEYSPAN_BAUD_RATE_OK;
1592 /* usa19hs function doesn't require prescaler */
1593 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1594 u8 *rate_low, u8 *prescaler, int portnum)
1596 u32 b16, /* baud rate times 16 (actual rate used internally) */
1597 div; /* divisor */
1599 dbg("%s - %d.", __func__, baud_rate);
1601 /* prevent divide by zero... */
1602 b16 = baud_rate * 16L;
1603 if (b16 == 0)
1604 return KEYSPAN_INVALID_BAUD_RATE;
1606 /* calculate the divisor */
1607 div = baudclk / b16;
1608 if (div == 0)
1609 return KEYSPAN_INVALID_BAUD_RATE;
1611 if (div > 0xffff)
1612 return KEYSPAN_INVALID_BAUD_RATE;
1614 /* return the counter values if non-null */
1615 if (rate_low)
1616 *rate_low = (u8) (div & 0xff);
1618 if (rate_hi)
1619 *rate_hi = (u8) ((div >> 8) & 0xff);
1621 if (rate_low && rate_hi)
1622 dbg("%s - %d %02x %02x.",
1623 __func__, baud_rate, *rate_hi, *rate_low);
1625 return KEYSPAN_BAUD_RATE_OK;
1628 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1629 u8 *rate_low, u8 *prescaler, int portnum)
1631 u32 b16, /* baud rate times 16 (actual rate used internally) */
1632 clk, /* clock with 13/8 prescaler */
1633 div, /* divisor using 13/8 prescaler */
1634 res, /* resulting baud rate using 13/8 prescaler */
1635 diff, /* error using 13/8 prescaler */
1636 smallest_diff;
1637 u8 best_prescaler;
1638 int i;
1640 dbg("%s - %d.", __func__, baud_rate);
1642 /* prevent divide by zero */
1643 b16 = baud_rate * 16L;
1644 if (b16 == 0)
1645 return KEYSPAN_INVALID_BAUD_RATE;
1647 /* Calculate prescaler by trying them all and looking
1648 for best fit */
1650 /* start with largest possible difference */
1651 smallest_diff = 0xffffffff;
1653 /* 0 is an invalid prescaler, used as a flag */
1654 best_prescaler = 0;
1656 for (i = 8; i <= 0xff; ++i) {
1657 clk = (baudclk * 8) / (u32) i;
1659 div = clk / b16;
1660 if (div == 0)
1661 continue;
1663 res = clk / div;
1664 diff = (res > b16) ? (res-b16) : (b16-res);
1666 if (diff < smallest_diff) {
1667 best_prescaler = i;
1668 smallest_diff = diff;
1672 if (best_prescaler == 0)
1673 return KEYSPAN_INVALID_BAUD_RATE;
1675 clk = (baudclk * 8) / (u32) best_prescaler;
1676 div = clk / b16;
1678 /* return the divisor and prescaler if non-null */
1679 if (rate_low)
1680 *rate_low = (u8) (div & 0xff);
1681 if (rate_hi)
1682 *rate_hi = (u8) ((div >> 8) & 0xff);
1683 if (prescaler) {
1684 *prescaler = best_prescaler;
1685 /* dbg("%s - %d %d", __func__, *prescaler, div); */
1687 return KEYSPAN_BAUD_RATE_OK;
1690 /* USA-28 supports different maximum baud rates on each port */
1691 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1692 u8 *rate_low, u8 *prescaler, int portnum)
1694 u32 b16, /* baud rate times 16 (actual rate used internally) */
1695 div, /* divisor */
1696 cnt; /* inverse of divisor (programmed into 8051) */
1698 dbg("%s - %d.", __func__, baud_rate);
1700 /* prevent divide by zero */
1701 b16 = baud_rate * 16L;
1702 if (b16 == 0)
1703 return KEYSPAN_INVALID_BAUD_RATE;
1705 /* calculate the divisor and the counter (its inverse) */
1706 div = KEYSPAN_USA28_BAUDCLK / b16;
1707 if (div == 0)
1708 return KEYSPAN_INVALID_BAUD_RATE;
1709 else
1710 cnt = 0 - div;
1712 /* check for out of range, based on portnum,
1713 and return result */
1714 if (portnum == 0) {
1715 if (div > 0xffff)
1716 return KEYSPAN_INVALID_BAUD_RATE;
1717 } else {
1718 if (portnum == 1) {
1719 if (div > 0xff)
1720 return KEYSPAN_INVALID_BAUD_RATE;
1721 } else
1722 return KEYSPAN_INVALID_BAUD_RATE;
1725 /* return the counter values if not NULL
1726 (port 1 will ignore retHi) */
1727 if (rate_low)
1728 *rate_low = (u8) (cnt & 0xff);
1729 if (rate_hi)
1730 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1731 dbg("%s - %d OK.", __func__, baud_rate);
1732 return KEYSPAN_BAUD_RATE_OK;
1735 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1736 struct usb_serial_port *port,
1737 int reset_port)
1739 struct keyspan_usa26_portControlMessage msg;
1740 struct keyspan_serial_private *s_priv;
1741 struct keyspan_port_private *p_priv;
1742 const struct keyspan_device_details *d_details;
1743 int outcont_urb;
1744 struct urb *this_urb;
1745 int device_port, err;
1747 dbg("%s reset=%d", __func__, reset_port);
1749 s_priv = usb_get_serial_data(serial);
1750 p_priv = usb_get_serial_port_data(port);
1751 d_details = s_priv->device_details;
1752 device_port = port->number - port->serial->minor;
1754 outcont_urb = d_details->outcont_endpoints[port->number];
1755 this_urb = p_priv->outcont_urb;
1757 dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe));
1759 /* Make sure we have an urb then send the message */
1760 if (this_urb == NULL) {
1761 dbg("%s - oops no urb.", __func__);
1762 return -1;
1765 /* Save reset port val for resend.
1766 Don't overwrite resend for open/close condition. */
1767 if ((reset_port + 1) > p_priv->resend_cont)
1768 p_priv->resend_cont = reset_port + 1;
1769 if (this_urb->status == -EINPROGRESS) {
1770 /* dbg("%s - already writing", __func__); */
1771 mdelay(5);
1772 return -1;
1775 memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
1777 /* Only set baud rate if it's changed */
1778 if (p_priv->old_baud != p_priv->baud) {
1779 p_priv->old_baud = p_priv->baud;
1780 msg.setClocking = 0xff;
1781 if (d_details->calculate_baud_rate
1782 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1783 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1784 dbg("%s - Invalid baud rate %d requested, using 9600.",
1785 __func__, p_priv->baud);
1786 msg.baudLo = 0;
1787 msg.baudHi = 125; /* Values for 9600 baud */
1788 msg.prescaler = 10;
1790 msg.setPrescaler = 0xff;
1793 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1794 switch (p_priv->cflag & CSIZE) {
1795 case CS5:
1796 msg.lcr |= USA_DATABITS_5;
1797 break;
1798 case CS6:
1799 msg.lcr |= USA_DATABITS_6;
1800 break;
1801 case CS7:
1802 msg.lcr |= USA_DATABITS_7;
1803 break;
1804 case CS8:
1805 msg.lcr |= USA_DATABITS_8;
1806 break;
1808 if (p_priv->cflag & PARENB) {
1809 /* note USA_PARITY_NONE == 0 */
1810 msg.lcr |= (p_priv->cflag & PARODD)?
1811 USA_PARITY_ODD : USA_PARITY_EVEN;
1813 msg.setLcr = 0xff;
1815 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1816 msg.xonFlowControl = 0;
1817 msg.setFlowControl = 0xff;
1818 msg.forwardingLength = 16;
1819 msg.xonChar = 17;
1820 msg.xoffChar = 19;
1822 /* Opening port */
1823 if (reset_port == 1) {
1824 msg._txOn = 1;
1825 msg._txOff = 0;
1826 msg.txFlush = 0;
1827 msg.txBreak = 0;
1828 msg.rxOn = 1;
1829 msg.rxOff = 0;
1830 msg.rxFlush = 1;
1831 msg.rxForward = 0;
1832 msg.returnStatus = 0;
1833 msg.resetDataToggle = 0xff;
1836 /* Closing port */
1837 else if (reset_port == 2) {
1838 msg._txOn = 0;
1839 msg._txOff = 1;
1840 msg.txFlush = 0;
1841 msg.txBreak = 0;
1842 msg.rxOn = 0;
1843 msg.rxOff = 1;
1844 msg.rxFlush = 1;
1845 msg.rxForward = 0;
1846 msg.returnStatus = 0;
1847 msg.resetDataToggle = 0;
1850 /* Sending intermediate configs */
1851 else {
1852 msg._txOn = (!p_priv->break_on);
1853 msg._txOff = 0;
1854 msg.txFlush = 0;
1855 msg.txBreak = (p_priv->break_on);
1856 msg.rxOn = 0;
1857 msg.rxOff = 0;
1858 msg.rxFlush = 0;
1859 msg.rxForward = 0;
1860 msg.returnStatus = 0;
1861 msg.resetDataToggle = 0x0;
1864 /* Do handshaking outputs */
1865 msg.setTxTriState_setRts = 0xff;
1866 msg.txTriState_rts = p_priv->rts_state;
1868 msg.setHskoa_setDtr = 0xff;
1869 msg.hskoa_dtr = p_priv->dtr_state;
1871 p_priv->resend_cont = 0;
1872 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1874 /* send the data out the device on control endpoint */
1875 this_urb->transfer_buffer_length = sizeof(msg);
1877 err = usb_submit_urb(this_urb, GFP_ATOMIC);
1878 if (err != 0)
1879 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
1880 #if 0
1881 else {
1882 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
1883 outcont_urb, this_urb->transfer_buffer_length,
1884 usb_pipeendpoint(this_urb->pipe));
1886 #endif
1888 return 0;
1891 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1892 struct usb_serial_port *port,
1893 int reset_port)
1895 struct keyspan_usa28_portControlMessage msg;
1896 struct keyspan_serial_private *s_priv;
1897 struct keyspan_port_private *p_priv;
1898 const struct keyspan_device_details *d_details;
1899 struct urb *this_urb;
1900 int device_port, err;
1902 dbg("%s", __func__);
1904 s_priv = usb_get_serial_data(serial);
1905 p_priv = usb_get_serial_port_data(port);
1906 d_details = s_priv->device_details;
1907 device_port = port->number - port->serial->minor;
1909 /* only do something if we have a bulk out endpoint */
1910 this_urb = p_priv->outcont_urb;
1911 if (this_urb == NULL) {
1912 dbg("%s - oops no urb.", __func__);
1913 return -1;
1916 /* Save reset port val for resend.
1917 Don't overwrite resend for open/close condition. */
1918 if ((reset_port + 1) > p_priv->resend_cont)
1919 p_priv->resend_cont = reset_port + 1;
1920 if (this_urb->status == -EINPROGRESS) {
1921 dbg("%s already writing", __func__);
1922 mdelay(5);
1923 return -1;
1926 memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
1928 msg.setBaudRate = 1;
1929 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1930 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1931 dbg("%s - Invalid baud rate requested %d.",
1932 __func__, p_priv->baud);
1933 msg.baudLo = 0xff;
1934 msg.baudHi = 0xb2; /* Values for 9600 baud */
1937 /* If parity is enabled, we must calculate it ourselves. */
1938 msg.parity = 0; /* XXX for now */
1940 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1941 msg.xonFlowControl = 0;
1943 /* Do handshaking outputs, DTR is inverted relative to RTS */
1944 msg.rts = p_priv->rts_state;
1945 msg.dtr = p_priv->dtr_state;
1947 msg.forwardingLength = 16;
1948 msg.forwardMs = 10;
1949 msg.breakThreshold = 45;
1950 msg.xonChar = 17;
1951 msg.xoffChar = 19;
1953 /*msg.returnStatus = 1;
1954 msg.resetDataToggle = 0xff;*/
1955 /* Opening port */
1956 if (reset_port == 1) {
1957 msg._txOn = 1;
1958 msg._txOff = 0;
1959 msg.txFlush = 0;
1960 msg.txForceXoff = 0;
1961 msg.txBreak = 0;
1962 msg.rxOn = 1;
1963 msg.rxOff = 0;
1964 msg.rxFlush = 1;
1965 msg.rxForward = 0;
1966 msg.returnStatus = 0;
1967 msg.resetDataToggle = 0xff;
1969 /* Closing port */
1970 else if (reset_port == 2) {
1971 msg._txOn = 0;
1972 msg._txOff = 1;
1973 msg.txFlush = 0;
1974 msg.txForceXoff = 0;
1975 msg.txBreak = 0;
1976 msg.rxOn = 0;
1977 msg.rxOff = 1;
1978 msg.rxFlush = 1;
1979 msg.rxForward = 0;
1980 msg.returnStatus = 0;
1981 msg.resetDataToggle = 0;
1983 /* Sending intermediate configs */
1984 else {
1985 msg._txOn = (!p_priv->break_on);
1986 msg._txOff = 0;
1987 msg.txFlush = 0;
1988 msg.txForceXoff = 0;
1989 msg.txBreak = (p_priv->break_on);
1990 msg.rxOn = 0;
1991 msg.rxOff = 0;
1992 msg.rxFlush = 0;
1993 msg.rxForward = 0;
1994 msg.returnStatus = 0;
1995 msg.resetDataToggle = 0x0;
1998 p_priv->resend_cont = 0;
1999 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2001 /* send the data out the device on control endpoint */
2002 this_urb->transfer_buffer_length = sizeof(msg);
2004 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2005 if (err != 0)
2006 dbg("%s - usb_submit_urb(setup) failed", __func__);
2007 #if 0
2008 else {
2009 dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__,
2010 this_urb->transfer_buffer_length);
2012 #endif
2014 return 0;
2017 static int keyspan_usa49_send_setup(struct usb_serial *serial,
2018 struct usb_serial_port *port,
2019 int reset_port)
2021 struct keyspan_usa49_portControlMessage msg;
2022 struct usb_ctrlrequest *dr = NULL;
2023 struct keyspan_serial_private *s_priv;
2024 struct keyspan_port_private *p_priv;
2025 const struct keyspan_device_details *d_details;
2026 struct urb *this_urb;
2027 int err, device_port;
2029 dbg("%s", __func__);
2031 s_priv = usb_get_serial_data(serial);
2032 p_priv = usb_get_serial_port_data(port);
2033 d_details = s_priv->device_details;
2035 this_urb = s_priv->glocont_urb;
2037 /* Work out which port within the device is being setup */
2038 device_port = port->number - port->serial->minor;
2040 /* Make sure we have an urb then send the message */
2041 if (this_urb == NULL) {
2042 dbg("%s - oops no urb for port %d.", __func__, port->number);
2043 return -1;
2046 dbg("%s - endpoint %d port %d (%d)",
2047 __func__, usb_pipeendpoint(this_urb->pipe),
2048 port->number, device_port);
2050 /* Save reset port val for resend.
2051 Don't overwrite resend for open/close condition. */
2052 if ((reset_port + 1) > p_priv->resend_cont)
2053 p_priv->resend_cont = reset_port + 1;
2055 if (this_urb->status == -EINPROGRESS) {
2056 /* dbg("%s - already writing", __func__); */
2057 mdelay(5);
2058 return -1;
2061 memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
2063 /*msg.portNumber = port->number;*/
2064 msg.portNumber = device_port;
2066 /* Only set baud rate if it's changed */
2067 if (p_priv->old_baud != p_priv->baud) {
2068 p_priv->old_baud = p_priv->baud;
2069 msg.setClocking = 0xff;
2070 if (d_details->calculate_baud_rate
2071 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2072 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2073 dbg("%s - Invalid baud rate %d requested, using 9600.",
2074 __func__, p_priv->baud);
2075 msg.baudLo = 0;
2076 msg.baudHi = 125; /* Values for 9600 baud */
2077 msg.prescaler = 10;
2079 /* msg.setPrescaler = 0xff; */
2082 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2083 switch (p_priv->cflag & CSIZE) {
2084 case CS5:
2085 msg.lcr |= USA_DATABITS_5;
2086 break;
2087 case CS6:
2088 msg.lcr |= USA_DATABITS_6;
2089 break;
2090 case CS7:
2091 msg.lcr |= USA_DATABITS_7;
2092 break;
2093 case CS8:
2094 msg.lcr |= USA_DATABITS_8;
2095 break;
2097 if (p_priv->cflag & PARENB) {
2098 /* note USA_PARITY_NONE == 0 */
2099 msg.lcr |= (p_priv->cflag & PARODD)?
2100 USA_PARITY_ODD : USA_PARITY_EVEN;
2102 msg.setLcr = 0xff;
2104 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2105 msg.xonFlowControl = 0;
2106 msg.setFlowControl = 0xff;
2108 msg.forwardingLength = 16;
2109 msg.xonChar = 17;
2110 msg.xoffChar = 19;
2112 /* Opening port */
2113 if (reset_port == 1) {
2114 msg._txOn = 1;
2115 msg._txOff = 0;
2116 msg.txFlush = 0;
2117 msg.txBreak = 0;
2118 msg.rxOn = 1;
2119 msg.rxOff = 0;
2120 msg.rxFlush = 1;
2121 msg.rxForward = 0;
2122 msg.returnStatus = 0;
2123 msg.resetDataToggle = 0xff;
2124 msg.enablePort = 1;
2125 msg.disablePort = 0;
2127 /* Closing port */
2128 else if (reset_port == 2) {
2129 msg._txOn = 0;
2130 msg._txOff = 1;
2131 msg.txFlush = 0;
2132 msg.txBreak = 0;
2133 msg.rxOn = 0;
2134 msg.rxOff = 1;
2135 msg.rxFlush = 1;
2136 msg.rxForward = 0;
2137 msg.returnStatus = 0;
2138 msg.resetDataToggle = 0;
2139 msg.enablePort = 0;
2140 msg.disablePort = 1;
2142 /* Sending intermediate configs */
2143 else {
2144 msg._txOn = (!p_priv->break_on);
2145 msg._txOff = 0;
2146 msg.txFlush = 0;
2147 msg.txBreak = (p_priv->break_on);
2148 msg.rxOn = 0;
2149 msg.rxOff = 0;
2150 msg.rxFlush = 0;
2151 msg.rxForward = 0;
2152 msg.returnStatus = 0;
2153 msg.resetDataToggle = 0x0;
2154 msg.enablePort = 0;
2155 msg.disablePort = 0;
2158 /* Do handshaking outputs */
2159 msg.setRts = 0xff;
2160 msg.rts = p_priv->rts_state;
2162 msg.setDtr = 0xff;
2163 msg.dtr = p_priv->dtr_state;
2165 p_priv->resend_cont = 0;
2167 /* if the device is a 49wg, we send control message on usb
2168 control EP 0 */
2170 if (d_details->product_id == keyspan_usa49wg_product_id) {
2171 dr = (void *)(s_priv->ctrl_buf);
2172 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2173 dr->bRequest = 0xB0; /* 49wg control message */;
2174 dr->wValue = 0;
2175 dr->wIndex = 0;
2176 dr->wLength = cpu_to_le16(sizeof(msg));
2178 memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
2180 usb_fill_control_urb(this_urb, serial->dev,
2181 usb_sndctrlpipe(serial->dev, 0),
2182 (unsigned char *)dr, s_priv->glocont_buf,
2183 sizeof(msg), usa49_glocont_callback, serial);
2185 } else {
2186 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2188 /* send the data out the device on control endpoint */
2189 this_urb->transfer_buffer_length = sizeof(msg);
2191 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2192 if (err != 0)
2193 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2194 #if 0
2195 else {
2196 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__,
2197 outcont_urb, this_urb->transfer_buffer_length,
2198 usb_pipeendpoint(this_urb->pipe));
2200 #endif
2202 return 0;
2205 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2206 struct usb_serial_port *port,
2207 int reset_port)
2209 struct keyspan_usa90_portControlMessage msg;
2210 struct keyspan_serial_private *s_priv;
2211 struct keyspan_port_private *p_priv;
2212 const struct keyspan_device_details *d_details;
2213 struct urb *this_urb;
2214 int err;
2215 u8 prescaler;
2217 dbg("%s", __func__);
2219 s_priv = usb_get_serial_data(serial);
2220 p_priv = usb_get_serial_port_data(port);
2221 d_details = s_priv->device_details;
2223 /* only do something if we have a bulk out endpoint */
2224 this_urb = p_priv->outcont_urb;
2225 if (this_urb == NULL) {
2226 dbg("%s - oops no urb.", __func__);
2227 return -1;
2230 /* Save reset port val for resend.
2231 Don't overwrite resend for open/close condition. */
2232 if ((reset_port + 1) > p_priv->resend_cont)
2233 p_priv->resend_cont = reset_port + 1;
2234 if (this_urb->status == -EINPROGRESS) {
2235 dbg("%s already writing", __func__);
2236 mdelay(5);
2237 return -1;
2240 memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
2242 /* Only set baud rate if it's changed */
2243 if (p_priv->old_baud != p_priv->baud) {
2244 p_priv->old_baud = p_priv->baud;
2245 msg.setClocking = 0x01;
2246 if (d_details->calculate_baud_rate
2247 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2248 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
2249 dbg("%s - Invalid baud rate %d requested, using 9600.",
2250 __func__, p_priv->baud);
2251 p_priv->baud = 9600;
2252 d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2253 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2255 msg.setRxMode = 1;
2256 msg.setTxMode = 1;
2259 /* modes must always be correctly specified */
2260 if (p_priv->baud > 57600) {
2261 msg.rxMode = RXMODE_DMA;
2262 msg.txMode = TXMODE_DMA;
2263 } else {
2264 msg.rxMode = RXMODE_BYHAND;
2265 msg.txMode = TXMODE_BYHAND;
2268 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2269 switch (p_priv->cflag & CSIZE) {
2270 case CS5:
2271 msg.lcr |= USA_DATABITS_5;
2272 break;
2273 case CS6:
2274 msg.lcr |= USA_DATABITS_6;
2275 break;
2276 case CS7:
2277 msg.lcr |= USA_DATABITS_7;
2278 break;
2279 case CS8:
2280 msg.lcr |= USA_DATABITS_8;
2281 break;
2283 if (p_priv->cflag & PARENB) {
2284 /* note USA_PARITY_NONE == 0 */
2285 msg.lcr |= (p_priv->cflag & PARODD)?
2286 USA_PARITY_ODD : USA_PARITY_EVEN;
2288 if (p_priv->old_cflag != p_priv->cflag) {
2289 p_priv->old_cflag = p_priv->cflag;
2290 msg.setLcr = 0x01;
2293 if (p_priv->flow_control == flow_cts)
2294 msg.txFlowControl = TXFLOW_CTS;
2295 msg.setTxFlowControl = 0x01;
2296 msg.setRxFlowControl = 0x01;
2298 msg.rxForwardingLength = 16;
2299 msg.rxForwardingTimeout = 16;
2300 msg.txAckSetting = 0;
2301 msg.xonChar = 17;
2302 msg.xoffChar = 19;
2304 /* Opening port */
2305 if (reset_port == 1) {
2306 msg.portEnabled = 1;
2307 msg.rxFlush = 1;
2308 msg.txBreak = (p_priv->break_on);
2310 /* Closing port */
2311 else if (reset_port == 2)
2312 msg.portEnabled = 0;
2313 /* Sending intermediate configs */
2314 else {
2315 msg.portEnabled = 1;
2316 msg.txBreak = (p_priv->break_on);
2319 /* Do handshaking outputs */
2320 msg.setRts = 0x01;
2321 msg.rts = p_priv->rts_state;
2323 msg.setDtr = 0x01;
2324 msg.dtr = p_priv->dtr_state;
2326 p_priv->resend_cont = 0;
2327 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2329 /* send the data out the device on control endpoint */
2330 this_urb->transfer_buffer_length = sizeof(msg);
2332 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2333 if (err != 0)
2334 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2335 return 0;
2338 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2339 struct usb_serial_port *port,
2340 int reset_port)
2342 struct keyspan_usa67_portControlMessage msg;
2343 struct keyspan_serial_private *s_priv;
2344 struct keyspan_port_private *p_priv;
2345 const struct keyspan_device_details *d_details;
2346 struct urb *this_urb;
2347 int err, device_port;
2349 dbg("%s", __func__);
2351 s_priv = usb_get_serial_data(serial);
2352 p_priv = usb_get_serial_port_data(port);
2353 d_details = s_priv->device_details;
2355 this_urb = s_priv->glocont_urb;
2357 /* Work out which port within the device is being setup */
2358 device_port = port->number - port->serial->minor;
2360 /* Make sure we have an urb then send the message */
2361 if (this_urb == NULL) {
2362 dbg("%s - oops no urb for port %d.", __func__,
2363 port->number);
2364 return -1;
2367 /* Save reset port val for resend.
2368 Don't overwrite resend for open/close condition. */
2369 if ((reset_port + 1) > p_priv->resend_cont)
2370 p_priv->resend_cont = reset_port + 1;
2371 if (this_urb->status == -EINPROGRESS) {
2372 /* dbg("%s - already writing", __func__); */
2373 mdelay(5);
2374 return -1;
2377 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2379 msg.port = device_port;
2381 /* Only set baud rate if it's changed */
2382 if (p_priv->old_baud != p_priv->baud) {
2383 p_priv->old_baud = p_priv->baud;
2384 msg.setClocking = 0xff;
2385 if (d_details->calculate_baud_rate
2386 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2387 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2388 dbg("%s - Invalid baud rate %d requested, using 9600.",
2389 __func__, p_priv->baud);
2390 msg.baudLo = 0;
2391 msg.baudHi = 125; /* Values for 9600 baud */
2392 msg.prescaler = 10;
2394 msg.setPrescaler = 0xff;
2397 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2398 switch (p_priv->cflag & CSIZE) {
2399 case CS5:
2400 msg.lcr |= USA_DATABITS_5;
2401 break;
2402 case CS6:
2403 msg.lcr |= USA_DATABITS_6;
2404 break;
2405 case CS7:
2406 msg.lcr |= USA_DATABITS_7;
2407 break;
2408 case CS8:
2409 msg.lcr |= USA_DATABITS_8;
2410 break;
2412 if (p_priv->cflag & PARENB) {
2413 /* note USA_PARITY_NONE == 0 */
2414 msg.lcr |= (p_priv->cflag & PARODD)?
2415 USA_PARITY_ODD : USA_PARITY_EVEN;
2417 msg.setLcr = 0xff;
2419 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2420 msg.xonFlowControl = 0;
2421 msg.setFlowControl = 0xff;
2422 msg.forwardingLength = 16;
2423 msg.xonChar = 17;
2424 msg.xoffChar = 19;
2426 if (reset_port == 1) {
2427 /* Opening port */
2428 msg._txOn = 1;
2429 msg._txOff = 0;
2430 msg.txFlush = 0;
2431 msg.txBreak = 0;
2432 msg.rxOn = 1;
2433 msg.rxOff = 0;
2434 msg.rxFlush = 1;
2435 msg.rxForward = 0;
2436 msg.returnStatus = 0;
2437 msg.resetDataToggle = 0xff;
2438 } else if (reset_port == 2) {
2439 /* Closing port */
2440 msg._txOn = 0;
2441 msg._txOff = 1;
2442 msg.txFlush = 0;
2443 msg.txBreak = 0;
2444 msg.rxOn = 0;
2445 msg.rxOff = 1;
2446 msg.rxFlush = 1;
2447 msg.rxForward = 0;
2448 msg.returnStatus = 0;
2449 msg.resetDataToggle = 0;
2450 } else {
2451 /* Sending intermediate configs */
2452 msg._txOn = (!p_priv->break_on);
2453 msg._txOff = 0;
2454 msg.txFlush = 0;
2455 msg.txBreak = (p_priv->break_on);
2456 msg.rxOn = 0;
2457 msg.rxOff = 0;
2458 msg.rxFlush = 0;
2459 msg.rxForward = 0;
2460 msg.returnStatus = 0;
2461 msg.resetDataToggle = 0x0;
2464 /* Do handshaking outputs */
2465 msg.setTxTriState_setRts = 0xff;
2466 msg.txTriState_rts = p_priv->rts_state;
2468 msg.setHskoa_setDtr = 0xff;
2469 msg.hskoa_dtr = p_priv->dtr_state;
2471 p_priv->resend_cont = 0;
2473 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2475 /* send the data out the device on control endpoint */
2476 this_urb->transfer_buffer_length = sizeof(msg);
2478 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2479 if (err != 0)
2480 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__,
2481 err);
2482 return 0;
2485 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2487 struct usb_serial *serial = port->serial;
2488 struct keyspan_serial_private *s_priv;
2489 const struct keyspan_device_details *d_details;
2491 dbg("%s", __func__);
2493 s_priv = usb_get_serial_data(serial);
2494 d_details = s_priv->device_details;
2496 switch (d_details->msg_format) {
2497 case msg_usa26:
2498 keyspan_usa26_send_setup(serial, port, reset_port);
2499 break;
2500 case msg_usa28:
2501 keyspan_usa28_send_setup(serial, port, reset_port);
2502 break;
2503 case msg_usa49:
2504 keyspan_usa49_send_setup(serial, port, reset_port);
2505 break;
2506 case msg_usa90:
2507 keyspan_usa90_send_setup(serial, port, reset_port);
2508 break;
2509 case msg_usa67:
2510 keyspan_usa67_send_setup(serial, port, reset_port);
2511 break;
2516 /* Gets called by the "real" driver (ie once firmware is loaded
2517 and renumeration has taken place. */
2518 static int keyspan_startup(struct usb_serial *serial)
2520 int i, err;
2521 struct usb_serial_port *port;
2522 struct keyspan_serial_private *s_priv;
2523 struct keyspan_port_private *p_priv;
2524 const struct keyspan_device_details *d_details;
2526 dbg("%s", __func__);
2528 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2529 if (d_details->product_id ==
2530 le16_to_cpu(serial->dev->descriptor.idProduct))
2531 break;
2532 if (d_details == NULL) {
2533 dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
2534 __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2535 return 1;
2538 /* Setup private data for serial driver */
2539 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2540 if (!s_priv) {
2541 dbg("%s - kmalloc for keyspan_serial_private failed.",
2542 __func__);
2543 return -ENOMEM;
2546 s_priv->device_details = d_details;
2547 usb_set_serial_data(serial, s_priv);
2549 /* Now setup per port private data */
2550 for (i = 0; i < serial->num_ports; i++) {
2551 port = serial->port[i];
2552 p_priv = kzalloc(sizeof(struct keyspan_port_private),
2553 GFP_KERNEL);
2554 if (!p_priv) {
2555 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i);
2556 return 1;
2558 p_priv->device_details = d_details;
2559 usb_set_serial_port_data(port, p_priv);
2562 keyspan_setup_urbs(serial);
2564 if (s_priv->instat_urb != NULL) {
2565 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2566 if (err != 0)
2567 dbg("%s - submit instat urb failed %d", __func__,
2568 err);
2570 if (s_priv->indat_urb != NULL) {
2571 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2572 if (err != 0)
2573 dbg("%s - submit indat urb failed %d", __func__,
2574 err);
2577 return 0;
2580 static void keyspan_disconnect(struct usb_serial *serial)
2582 int i, j;
2583 struct usb_serial_port *port;
2584 struct keyspan_serial_private *s_priv;
2585 struct keyspan_port_private *p_priv;
2587 dbg("%s", __func__);
2589 s_priv = usb_get_serial_data(serial);
2591 /* Stop reading/writing urbs */
2592 stop_urb(s_priv->instat_urb);
2593 stop_urb(s_priv->glocont_urb);
2594 stop_urb(s_priv->indat_urb);
2595 for (i = 0; i < serial->num_ports; ++i) {
2596 port = serial->port[i];
2597 p_priv = usb_get_serial_port_data(port);
2598 stop_urb(p_priv->inack_urb);
2599 stop_urb(p_priv->outcont_urb);
2600 for (j = 0; j < 2; j++) {
2601 stop_urb(p_priv->in_urbs[j]);
2602 stop_urb(p_priv->out_urbs[j]);
2606 /* Now free them */
2607 usb_free_urb(s_priv->instat_urb);
2608 usb_free_urb(s_priv->indat_urb);
2609 usb_free_urb(s_priv->glocont_urb);
2610 for (i = 0; i < serial->num_ports; ++i) {
2611 port = serial->port[i];
2612 p_priv = usb_get_serial_port_data(port);
2613 usb_free_urb(p_priv->inack_urb);
2614 usb_free_urb(p_priv->outcont_urb);
2615 for (j = 0; j < 2; j++) {
2616 usb_free_urb(p_priv->in_urbs[j]);
2617 usb_free_urb(p_priv->out_urbs[j]);
2622 static void keyspan_release(struct usb_serial *serial)
2624 int i;
2625 struct usb_serial_port *port;
2626 struct keyspan_serial_private *s_priv;
2628 dbg("%s", __func__);
2630 s_priv = usb_get_serial_data(serial);
2632 /* dbg("Freeing serial->private."); */
2633 kfree(s_priv);
2635 /* dbg("Freeing port->private."); */
2636 /* Now free per port private data */
2637 for (i = 0; i < serial->num_ports; i++) {
2638 port = serial->port[i];
2639 kfree(usb_get_serial_port_data(port));
2643 MODULE_AUTHOR(DRIVER_AUTHOR);
2644 MODULE_DESCRIPTION(DRIVER_DESC);
2645 MODULE_LICENSE("GPL");
2647 MODULE_FIRMWARE("keyspan/usa28.fw");
2648 MODULE_FIRMWARE("keyspan/usa28x.fw");
2649 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2650 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2651 MODULE_FIRMWARE("keyspan/usa19.fw");
2652 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2653 MODULE_FIRMWARE("keyspan/mpr.fw");
2654 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2655 MODULE_FIRMWARE("keyspan/usa18x.fw");
2656 MODULE_FIRMWARE("keyspan/usa19w.fw");
2657 MODULE_FIRMWARE("keyspan/usa49w.fw");
2658 MODULE_FIRMWARE("keyspan/usa49wlc.fw");
2660 module_param(debug, bool, S_IRUGO | S_IWUSR);
2661 MODULE_PARM_DESC(debug, "Debug enabled or not");