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.
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
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>
51 #define DRIVER_VERSION "v1.1.5"
52 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
53 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
55 #define INSTAT_BUFLEN 32
56 #define GLOCONT_BUFLEN 64
57 #define INDAT49W_BUFLEN 512
59 /* Per device and per port private data */
60 struct keyspan_serial_private {
61 const struct keyspan_device_details *device_details;
63 struct urb *instat_urb;
64 char instat_buf[INSTAT_BUFLEN];
66 /* added to support 49wg, where data from all 4 ports comes in
67 on 1 EP and high-speed supported */
68 struct urb *indat_urb;
69 char indat_buf[INDAT49W_BUFLEN];
71 /* XXX this one probably will need a lock */
72 struct urb *glocont_urb;
73 char glocont_buf[GLOCONT_BUFLEN];
74 char ctrl_buf[8]; /* for EP0 control message */
77 struct keyspan_port_private {
78 /* Keep track of which input & output endpoints to use */
82 /* Keep duplicate of device details in each port
83 structure as well - simplifies some of the
84 callback functions etc. */
85 const struct keyspan_device_details *device_details;
87 /* Input endpoints and buffer for this port */
88 struct urb *in_urbs[2];
89 char in_buffer[2][64];
90 /* Output endpoints and buffer for this port */
91 struct urb *out_urbs[2];
92 char out_buffer[2][64];
94 /* Input ack endpoint */
95 struct urb *inack_urb;
98 /* Output control endpoint */
99 struct urb *outcont_urb;
100 char outcont_buffer[64];
102 /* Settings for the port */
106 unsigned int old_cflag;
107 enum {flow_none, flow_cts, flow_xon} flow_control;
108 int rts_state; /* Handshaking pins (outputs) */
110 int cts_state; /* Handshaking pins (inputs) */
116 unsigned long tx_start_time[2];
117 int resend_cont; /* need to resend control packet */
120 /* Include Keyspan message headers. All current Keyspan Adapters
121 make use of one of five message formats which are referred
122 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
123 within this driver. */
124 #include "keyspan_usa26msg.h"
125 #include "keyspan_usa28msg.h"
126 #include "keyspan_usa49msg.h"
127 #include "keyspan_usa90msg.h"
128 #include "keyspan_usa67msg.h"
131 module_usb_serial_driver(serial_drivers, keyspan_ids_combined);
133 static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
135 struct usb_serial_port *port = tty->driver_data;
136 struct keyspan_port_private *p_priv;
138 p_priv = usb_get_serial_port_data(port);
140 if (break_state == -1)
141 p_priv->break_on = 1;
143 p_priv->break_on = 0;
145 keyspan_send_setup(port, 0);
149 static void keyspan_set_termios(struct tty_struct *tty,
150 struct usb_serial_port *port, struct ktermios *old_termios)
152 int baud_rate, device_port;
153 struct keyspan_port_private *p_priv;
154 const struct keyspan_device_details *d_details;
157 p_priv = usb_get_serial_port_data(port);
158 d_details = p_priv->device_details;
159 cflag = tty->termios->c_cflag;
160 device_port = port->number - port->serial->minor;
162 /* Baud rate calculation takes baud rate as an integer
163 so other rates can be generated if desired. */
164 baud_rate = tty_get_baud_rate(tty);
165 /* If no match or invalid, don't change */
166 if (d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
167 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
168 /* FIXME - more to do here to ensure rate changes cleanly */
169 /* FIXME - calcuate exact rate from divisor ? */
170 p_priv->baud = baud_rate;
172 baud_rate = tty_termios_baud_rate(old_termios);
174 tty_encode_baud_rate(tty, baud_rate, baud_rate);
175 /* set CTS/RTS handshake etc. */
176 p_priv->cflag = cflag;
177 p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
179 /* Mark/Space not supported */
180 tty->termios->c_cflag &= ~CMSPAR;
182 keyspan_send_setup(port, 0);
185 static int keyspan_tiocmget(struct tty_struct *tty)
187 struct usb_serial_port *port = tty->driver_data;
188 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
191 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
192 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
193 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
194 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
195 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
196 ((p_priv->ri_state) ? TIOCM_RNG : 0);
201 static int keyspan_tiocmset(struct tty_struct *tty,
202 unsigned int set, unsigned int clear)
204 struct usb_serial_port *port = tty->driver_data;
205 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
208 p_priv->rts_state = 1;
210 p_priv->dtr_state = 1;
211 if (clear & TIOCM_RTS)
212 p_priv->rts_state = 0;
213 if (clear & TIOCM_DTR)
214 p_priv->dtr_state = 0;
215 keyspan_send_setup(port, 0);
219 /* Write function is similar for the four protocols used
220 with only a minor change for usa90 (usa19hs) required */
221 static int keyspan_write(struct tty_struct *tty,
222 struct usb_serial_port *port, const unsigned char *buf, int count)
224 struct keyspan_port_private *p_priv;
225 const struct keyspan_device_details *d_details;
228 struct urb *this_urb;
229 int err, maxDataLen, dataOffset;
231 p_priv = usb_get_serial_port_data(port);
232 d_details = p_priv->device_details;
234 if (d_details->msg_format == msg_usa90) {
242 dev_dbg(&port->dev, "%s - for port %d (%d chars), flip=%d\n",
243 __func__, port->number, count, p_priv->out_flip);
245 for (left = count; left > 0; left -= todo) {
247 if (todo > maxDataLen)
250 flip = p_priv->out_flip;
252 /* Check we have a valid urb/endpoint before we use it... */
253 this_urb = p_priv->out_urbs[flip];
254 if (this_urb == NULL) {
255 /* no bulk out, so return 0 bytes written */
256 dev_dbg(&port->dev, "%s - no output urb :(\n", __func__);
260 dev_dbg(&port->dev, "%s - endpoint %d flip %d\n",
261 __func__, usb_pipeendpoint(this_urb->pipe), flip);
263 if (this_urb->status == -EINPROGRESS) {
264 if (time_before(jiffies,
265 p_priv->tx_start_time[flip] + 10 * HZ))
267 usb_unlink_urb(this_urb);
271 /* First byte in buffer is "last flag" (except for usa19hx)
272 - unused so for now so set to zero */
273 ((char *)this_urb->transfer_buffer)[0] = 0;
275 memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
278 /* send the data out the bulk port */
279 this_urb->transfer_buffer_length = todo + dataOffset;
281 err = usb_submit_urb(this_urb, GFP_ATOMIC);
283 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed (%d)\n", err);
284 p_priv->tx_start_time[flip] = jiffies;
286 /* Flip for next time if usa26 or usa28 interface
287 (not used on usa49) */
288 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
294 static void usa26_indat_callback(struct urb *urb)
298 struct usb_serial_port *port;
299 struct tty_struct *tty;
300 unsigned char *data = urb->transfer_buffer;
301 int status = urb->status;
303 endpoint = usb_pipeendpoint(urb->pipe);
306 dev_dbg(&urb->dev->dev,"%s - nonzero status: %x on endpoint %d.\n",
307 __func__, status, endpoint);
312 tty = tty_port_tty_get(&port->port);
313 if (tty && urb->actual_length) {
314 /* 0x80 bit is error flag */
315 if ((data[0] & 0x80) == 0) {
316 /* no errors on individual bytes, only
317 possible overrun err */
318 if (data[0] & RXERROR_OVERRUN)
322 for (i = 1; i < urb->actual_length ; ++i)
323 tty_insert_flip_char(tty, data[i], err);
325 /* some bytes had errors, every byte has status */
326 dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
327 for (i = 0; i + 1 < urb->actual_length; i += 2) {
328 int stat = data[i], flag = 0;
329 if (stat & RXERROR_OVERRUN)
331 if (stat & RXERROR_FRAMING)
333 if (stat & RXERROR_PARITY)
335 /* XXX should handle break (0x10) */
336 tty_insert_flip_char(tty, data[i+1], flag);
339 tty_flip_buffer_push(tty);
343 /* Resubmit urb so we continue receiving */
344 err = usb_submit_urb(urb, GFP_ATOMIC);
346 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
349 /* Outdat handling is common for all devices */
350 static void usa2x_outdat_callback(struct urb *urb)
352 struct usb_serial_port *port;
353 struct keyspan_port_private *p_priv;
356 p_priv = usb_get_serial_port_data(port);
357 dev_dbg(&port->dev, "%s - urb %d\n", __func__, urb == p_priv->out_urbs[1]);
359 usb_serial_port_softint(port);
362 static void usa26_inack_callback(struct urb *urb)
366 static void usa26_outcont_callback(struct urb *urb)
368 struct usb_serial_port *port;
369 struct keyspan_port_private *p_priv;
372 p_priv = usb_get_serial_port_data(port);
374 if (p_priv->resend_cont) {
375 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
376 keyspan_usa26_send_setup(port->serial, port,
377 p_priv->resend_cont - 1);
381 static void usa26_instat_callback(struct urb *urb)
383 unsigned char *data = urb->transfer_buffer;
384 struct keyspan_usa26_portStatusMessage *msg;
385 struct usb_serial *serial;
386 struct usb_serial_port *port;
387 struct keyspan_port_private *p_priv;
388 struct tty_struct *tty;
389 int old_dcd_state, err;
390 int status = urb->status;
392 serial = urb->context;
395 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
398 if (urb->actual_length != 9) {
399 dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
403 msg = (struct keyspan_usa26_portStatusMessage *)data;
406 dev_dbg(&urb->dev->dev,
407 "%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
408 __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr,
409 msg->ri, msg->_txOff, msg->_txXoff, msg->rxEnabled,
410 msg->controlResponse);
413 /* Now do something useful with the data */
416 /* Check port number from message and retrieve private data */
417 if (msg->port >= serial->num_ports) {
418 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
421 port = serial->port[msg->port];
422 p_priv = usb_get_serial_port_data(port);
424 /* Update handshaking pin state information */
425 old_dcd_state = p_priv->dcd_state;
426 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
427 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
428 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
429 p_priv->ri_state = ((msg->ri) ? 1 : 0);
431 if (old_dcd_state != p_priv->dcd_state) {
432 tty = tty_port_tty_get(&port->port);
433 if (tty && !C_CLOCAL(tty))
438 /* Resubmit urb so we continue receiving */
439 err = usb_submit_urb(urb, GFP_ATOMIC);
441 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
445 static void usa26_glocont_callback(struct urb *urb)
450 static void usa28_indat_callback(struct urb *urb)
453 struct usb_serial_port *port;
454 struct tty_struct *tty;
456 struct keyspan_port_private *p_priv;
457 int status = urb->status;
460 p_priv = usb_get_serial_port_data(port);
461 data = urb->transfer_buffer;
463 if (urb != p_priv->in_urbs[p_priv->in_flip])
468 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
469 __func__, status, usb_pipeendpoint(urb->pipe));
474 p_priv = usb_get_serial_port_data(port);
475 data = urb->transfer_buffer;
477 tty = tty_port_tty_get(&port->port);
478 if (tty && urb->actual_length) {
479 tty_insert_flip_string(tty, data, urb->actual_length);
480 tty_flip_buffer_push(tty);
484 /* Resubmit urb so we continue receiving */
485 err = usb_submit_urb(urb, GFP_ATOMIC);
487 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n",
489 p_priv->in_flip ^= 1;
491 urb = p_priv->in_urbs[p_priv->in_flip];
492 } while (urb->status != -EINPROGRESS);
495 static void usa28_inack_callback(struct urb *urb)
499 static void usa28_outcont_callback(struct urb *urb)
501 struct usb_serial_port *port;
502 struct keyspan_port_private *p_priv;
505 p_priv = usb_get_serial_port_data(port);
507 if (p_priv->resend_cont) {
508 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
509 keyspan_usa28_send_setup(port->serial, port,
510 p_priv->resend_cont - 1);
514 static void usa28_instat_callback(struct urb *urb)
517 unsigned char *data = urb->transfer_buffer;
518 struct keyspan_usa28_portStatusMessage *msg;
519 struct usb_serial *serial;
520 struct usb_serial_port *port;
521 struct keyspan_port_private *p_priv;
522 struct tty_struct *tty;
524 int status = urb->status;
526 serial = urb->context;
529 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
533 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
534 dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
539 dev_dbg(&urb->dev->dev,
540 "%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__,
541 data[0], data[1], data[2], data[3], data[4], data[5],
542 data[6], data[7], data[8], data[9], data[10], data[11]);
545 /* Now do something useful with the data */
546 msg = (struct keyspan_usa28_portStatusMessage *)data;
548 /* Check port number from message and retrieve private data */
549 if (msg->port >= serial->num_ports) {
550 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
553 port = serial->port[msg->port];
554 p_priv = usb_get_serial_port_data(port);
556 /* Update handshaking pin state information */
557 old_dcd_state = p_priv->dcd_state;
558 p_priv->cts_state = ((msg->cts) ? 1 : 0);
559 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
560 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
561 p_priv->ri_state = ((msg->ri) ? 1 : 0);
563 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
564 tty = tty_port_tty_get(&port->port);
565 if (tty && !C_CLOCAL(tty))
570 /* Resubmit urb so we continue receiving */
571 err = usb_submit_urb(urb, GFP_ATOMIC);
573 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
577 static void usa28_glocont_callback(struct urb *urb)
582 static void usa49_glocont_callback(struct urb *urb)
584 struct usb_serial *serial;
585 struct usb_serial_port *port;
586 struct keyspan_port_private *p_priv;
589 serial = urb->context;
590 for (i = 0; i < serial->num_ports; ++i) {
591 port = serial->port[i];
592 p_priv = usb_get_serial_port_data(port);
594 if (p_priv->resend_cont) {
595 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
596 keyspan_usa49_send_setup(serial, port,
597 p_priv->resend_cont - 1);
603 /* This is actually called glostat in the Keyspan
605 static void usa49_instat_callback(struct urb *urb)
608 unsigned char *data = urb->transfer_buffer;
609 struct keyspan_usa49_portStatusMessage *msg;
610 struct usb_serial *serial;
611 struct usb_serial_port *port;
612 struct keyspan_port_private *p_priv;
614 int status = urb->status;
616 serial = urb->context;
619 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
623 if (urb->actual_length !=
624 sizeof(struct keyspan_usa49_portStatusMessage)) {
625 dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
630 dev_dbg(&urb->dev->dev, "%s: %x %x %x %x %x %x %x %x %x %x %x",
631 __func__, data[0], data[1], data[2], data[3], data[4],
632 data[5], data[6], data[7], data[8], data[9], data[10]);
635 /* Now do something useful with the data */
636 msg = (struct keyspan_usa49_portStatusMessage *)data;
638 /* Check port number from message and retrieve private data */
639 if (msg->portNumber >= serial->num_ports) {
640 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
641 __func__, msg->portNumber);
644 port = serial->port[msg->portNumber];
645 p_priv = usb_get_serial_port_data(port);
647 /* Update handshaking pin state information */
648 old_dcd_state = p_priv->dcd_state;
649 p_priv->cts_state = ((msg->cts) ? 1 : 0);
650 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
651 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
652 p_priv->ri_state = ((msg->ri) ? 1 : 0);
654 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
655 struct tty_struct *tty = tty_port_tty_get(&port->port);
656 if (tty && !C_CLOCAL(tty))
661 /* Resubmit urb so we continue receiving */
662 err = usb_submit_urb(urb, GFP_ATOMIC);
664 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
668 static void usa49_inack_callback(struct urb *urb)
672 static void usa49_indat_callback(struct urb *urb)
676 struct usb_serial_port *port;
677 struct tty_struct *tty;
678 unsigned char *data = urb->transfer_buffer;
679 int status = urb->status;
681 endpoint = usb_pipeendpoint(urb->pipe);
684 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
685 __func__, status, endpoint);
690 tty = tty_port_tty_get(&port->port);
691 if (tty && urb->actual_length) {
692 /* 0x80 bit is error flag */
693 if ((data[0] & 0x80) == 0) {
694 /* no error on any byte */
695 tty_insert_flip_string(tty, data + 1,
696 urb->actual_length - 1);
698 /* some bytes had errors, every byte has status */
699 for (i = 0; i + 1 < urb->actual_length; i += 2) {
700 int stat = data[i], flag = 0;
701 if (stat & RXERROR_OVERRUN)
703 if (stat & RXERROR_FRAMING)
705 if (stat & RXERROR_PARITY)
707 /* XXX should handle break (0x10) */
708 tty_insert_flip_char(tty, data[i+1], flag);
711 tty_flip_buffer_push(tty);
715 /* Resubmit urb so we continue receiving */
716 err = usb_submit_urb(urb, GFP_ATOMIC);
718 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
721 static void usa49wg_indat_callback(struct urb *urb)
724 struct usb_serial *serial;
725 struct usb_serial_port *port;
726 struct tty_struct *tty;
727 unsigned char *data = urb->transfer_buffer;
728 int status = urb->status;
730 serial = urb->context;
733 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
737 /* inbound data is in the form P#, len, status, data */
741 if (urb->actual_length) {
742 while (i < urb->actual_length) {
744 /* Check port number from message*/
745 if (data[i] >= serial->num_ports) {
746 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
750 port = serial->port[data[i++]];
751 tty = tty_port_tty_get(&port->port);
754 /* 0x80 bit is error flag */
755 if ((data[i] & 0x80) == 0) {
756 /* no error on any byte */
758 for (x = 1; x < len ; ++x)
759 tty_insert_flip_char(tty, data[i++], 0);
762 * some bytes had errors, every byte has status
764 for (x = 0; x + 1 < len; x += 2) {
765 int stat = data[i], flag = 0;
766 if (stat & RXERROR_OVERRUN)
768 if (stat & RXERROR_FRAMING)
770 if (stat & RXERROR_PARITY)
772 /* XXX should handle break (0x10) */
773 tty_insert_flip_char(tty,
778 tty_flip_buffer_push(tty);
783 /* Resubmit urb so we continue receiving */
784 err = usb_submit_urb(urb, GFP_ATOMIC);
786 dev_dbg(&urb->dev->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
789 /* not used, usa-49 doesn't have per-port control endpoints */
790 static void usa49_outcont_callback(struct urb *urb)
794 static void usa90_indat_callback(struct urb *urb)
798 struct usb_serial_port *port;
799 struct keyspan_port_private *p_priv;
800 struct tty_struct *tty;
801 unsigned char *data = urb->transfer_buffer;
802 int status = urb->status;
804 endpoint = usb_pipeendpoint(urb->pipe);
807 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
808 __func__, status, endpoint);
813 p_priv = usb_get_serial_port_data(port);
815 if (urb->actual_length) {
816 tty = tty_port_tty_get(&port->port);
817 /* if current mode is DMA, looks like usa28 format
818 otherwise looks like usa26 data format */
820 if (p_priv->baud > 57600)
821 tty_insert_flip_string(tty, data, urb->actual_length);
823 /* 0x80 bit is error flag */
824 if ((data[0] & 0x80) == 0) {
825 /* no errors on individual bytes, only
826 possible overrun err*/
827 if (data[0] & RXERROR_OVERRUN)
831 for (i = 1; i < urb->actual_length ; ++i)
832 tty_insert_flip_char(tty, data[i],
835 /* some bytes had errors, every byte has status */
836 dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
837 for (i = 0; i + 1 < urb->actual_length; i += 2) {
838 int stat = data[i], flag = 0;
839 if (stat & RXERROR_OVERRUN)
841 if (stat & RXERROR_FRAMING)
843 if (stat & RXERROR_PARITY)
845 /* XXX should handle break (0x10) */
846 tty_insert_flip_char(tty, data[i+1],
851 tty_flip_buffer_push(tty);
855 /* Resubmit urb so we continue receiving */
856 err = usb_submit_urb(urb, GFP_ATOMIC);
858 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
862 static void usa90_instat_callback(struct urb *urb)
864 unsigned char *data = urb->transfer_buffer;
865 struct keyspan_usa90_portStatusMessage *msg;
866 struct usb_serial *serial;
867 struct usb_serial_port *port;
868 struct keyspan_port_private *p_priv;
869 struct tty_struct *tty;
870 int old_dcd_state, err;
871 int status = urb->status;
873 serial = urb->context;
876 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
879 if (urb->actual_length < 14) {
880 dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
884 msg = (struct keyspan_usa90_portStatusMessage *)data;
886 /* Now do something useful with the data */
888 port = serial->port[0];
889 p_priv = usb_get_serial_port_data(port);
891 /* Update handshaking pin state information */
892 old_dcd_state = p_priv->dcd_state;
893 p_priv->cts_state = ((msg->cts) ? 1 : 0);
894 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
895 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
896 p_priv->ri_state = ((msg->ri) ? 1 : 0);
898 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
899 tty = tty_port_tty_get(&port->port);
900 if (tty && !C_CLOCAL(tty))
905 /* Resubmit urb so we continue receiving */
906 err = usb_submit_urb(urb, GFP_ATOMIC);
908 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
913 static void usa90_outcont_callback(struct urb *urb)
915 struct usb_serial_port *port;
916 struct keyspan_port_private *p_priv;
919 p_priv = usb_get_serial_port_data(port);
921 if (p_priv->resend_cont) {
922 dev_dbg(&urb->dev->dev, "%s - sending setup\n", __func__);
923 keyspan_usa90_send_setup(port->serial, port,
924 p_priv->resend_cont - 1);
928 /* Status messages from the 28xg */
929 static void usa67_instat_callback(struct urb *urb)
932 unsigned char *data = urb->transfer_buffer;
933 struct keyspan_usa67_portStatusMessage *msg;
934 struct usb_serial *serial;
935 struct usb_serial_port *port;
936 struct keyspan_port_private *p_priv;
938 int status = urb->status;
940 serial = urb->context;
943 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
947 if (urb->actual_length !=
948 sizeof(struct keyspan_usa67_portStatusMessage)) {
949 dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
954 /* Now do something useful with the data */
955 msg = (struct keyspan_usa67_portStatusMessage *)data;
957 /* Check port number from message and retrieve private data */
958 if (msg->port >= serial->num_ports) {
959 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
963 port = serial->port[msg->port];
964 p_priv = usb_get_serial_port_data(port);
966 /* Update handshaking pin state information */
967 old_dcd_state = p_priv->dcd_state;
968 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
969 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
971 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
972 struct tty_struct *tty = tty_port_tty_get(&port->port);
973 if (tty && !C_CLOCAL(tty))
978 /* Resubmit urb so we continue receiving */
979 err = usb_submit_urb(urb, GFP_ATOMIC);
981 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
984 static void usa67_glocont_callback(struct urb *urb)
986 struct usb_serial *serial;
987 struct usb_serial_port *port;
988 struct keyspan_port_private *p_priv;
991 serial = urb->context;
992 for (i = 0; i < serial->num_ports; ++i) {
993 port = serial->port[i];
994 p_priv = usb_get_serial_port_data(port);
996 if (p_priv->resend_cont) {
997 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
998 keyspan_usa67_send_setup(serial, port,
999 p_priv->resend_cont - 1);
1005 static int keyspan_write_room(struct tty_struct *tty)
1007 struct usb_serial_port *port = tty->driver_data;
1008 struct keyspan_port_private *p_priv;
1009 const struct keyspan_device_details *d_details;
1012 struct urb *this_urb;
1014 p_priv = usb_get_serial_port_data(port);
1015 d_details = p_priv->device_details;
1017 /* FIXME: locking */
1018 if (d_details->msg_format == msg_usa90)
1023 flip = p_priv->out_flip;
1025 /* Check both endpoints to see if any are available. */
1026 this_urb = p_priv->out_urbs[flip];
1027 if (this_urb != NULL) {
1028 if (this_urb->status != -EINPROGRESS)
1030 flip = (flip + 1) & d_details->outdat_endp_flip;
1031 this_urb = p_priv->out_urbs[flip];
1032 if (this_urb != NULL) {
1033 if (this_urb->status != -EINPROGRESS)
1041 static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port)
1043 struct keyspan_port_private *p_priv;
1044 const struct keyspan_device_details *d_details;
1046 int baud_rate, device_port;
1048 unsigned int cflag = 0;
1050 p_priv = usb_get_serial_port_data(port);
1051 d_details = p_priv->device_details;
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 urb = p_priv->in_urbs[i];
1071 /* make sure endpoint data toggle is synchronized
1073 usb_clear_halt(urb->dev, urb->pipe);
1074 err = usb_submit_urb(urb, GFP_KERNEL);
1076 dev_dbg(&port->dev, "%s - submit urb %d failed (%d)\n", __func__, i, err);
1079 /* Reset low level data toggle on out endpoints */
1080 for (i = 0; i < 2; i++) {
1081 urb = p_priv->out_urbs[i];
1084 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1085 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 device_port = port->number - port->serial->minor;
1093 cflag = tty->termios->c_cflag;
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(tty);
1097 /* If no match or invalid, leave as default */
1099 && d_details->calculate_baud_rate(port, 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);
1110 /* keyspan_set_termios(port, NULL); */
1115 static inline void stop_urb(struct urb *urb)
1117 if (urb && urb->status == -EINPROGRESS)
1121 static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
1123 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
1125 p_priv->rts_state = on;
1126 p_priv->dtr_state = on;
1127 keyspan_send_setup(port, 0);
1130 static void keyspan_close(struct usb_serial_port *port)
1133 struct usb_serial *serial = port->serial;
1134 struct keyspan_port_private *p_priv;
1136 p_priv = usb_get_serial_port_data(port);
1138 p_priv->rts_state = 0;
1139 p_priv->dtr_state = 0;
1142 keyspan_send_setup(port, 2);
1143 /* pilot-xfer seems to work best with this delay */
1145 /* keyspan_set_termios(port, NULL); */
1148 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1149 dev_dbg(&port->dev, "%s - urb in progress\n", __func__);
1152 p_priv->out_flip = 0;
1153 p_priv->in_flip = 0;
1156 /* Stop reading/writing urbs */
1157 stop_urb(p_priv->inack_urb);
1158 /* stop_urb(p_priv->outcont_urb); */
1159 for (i = 0; i < 2; i++) {
1160 stop_urb(p_priv->in_urbs[i]);
1161 stop_urb(p_priv->out_urbs[i]);
1166 /* download the firmware to a pre-renumeration device */
1167 static int keyspan_fake_startup(struct usb_serial *serial)
1170 const struct ihex_binrec *record;
1172 const struct firmware *fw;
1174 dev_dbg(&serial->dev->dev, "Keyspan startup version %04x product %04x\n",
1175 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1176 le16_to_cpu(serial->dev->descriptor.idProduct));
1178 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
1180 dev_dbg(&serial->dev->dev, "Firmware already loaded. Quitting.\n");
1184 /* Select firmware image on the basis of idProduct */
1185 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1186 case keyspan_usa28_pre_product_id:
1187 fw_name = "keyspan/usa28.fw";
1190 case keyspan_usa28x_pre_product_id:
1191 fw_name = "keyspan/usa28x.fw";
1194 case keyspan_usa28xa_pre_product_id:
1195 fw_name = "keyspan/usa28xa.fw";
1198 case keyspan_usa28xb_pre_product_id:
1199 fw_name = "keyspan/usa28xb.fw";
1202 case keyspan_usa19_pre_product_id:
1203 fw_name = "keyspan/usa19.fw";
1206 case keyspan_usa19qi_pre_product_id:
1207 fw_name = "keyspan/usa19qi.fw";
1210 case keyspan_mpr_pre_product_id:
1211 fw_name = "keyspan/mpr.fw";
1214 case keyspan_usa19qw_pre_product_id:
1215 fw_name = "keyspan/usa19qw.fw";
1218 case keyspan_usa18x_pre_product_id:
1219 fw_name = "keyspan/usa18x.fw";
1222 case keyspan_usa19w_pre_product_id:
1223 fw_name = "keyspan/usa19w.fw";
1226 case keyspan_usa49w_pre_product_id:
1227 fw_name = "keyspan/usa49w.fw";
1230 case keyspan_usa49wlc_pre_product_id:
1231 fw_name = "keyspan/usa49wlc.fw";
1235 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1236 le16_to_cpu(serial->dev->descriptor.idProduct));
1240 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
1241 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1245 dev_dbg(&serial->dev->dev, "Uploading Keyspan %s firmware.\n", fw_name);
1247 /* download the firmware image */
1248 response = ezusb_set_reset(serial->dev, 1);
1250 record = (const struct ihex_binrec *)fw->data;
1253 response = ezusb_writememory(serial->dev, be32_to_cpu(record->addr),
1254 (unsigned char *)record->data,
1255 be16_to_cpu(record->len), 0xa0);
1257 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
1258 response, be32_to_cpu(record->addr),
1259 record->data, be16_to_cpu(record->len));
1262 record = ihex_next_binrec(record);
1264 release_firmware(fw);
1265 /* bring device out of reset. Renumeration will occur in a
1266 moment and the new device will bind to the real driver */
1267 response = ezusb_set_reset(serial->dev, 0);
1269 /* we don't want this device to have a driver assigned to it. */
1273 /* Helper functions used by keyspan_setup_urbs */
1274 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1277 struct usb_host_interface *iface_desc;
1278 struct usb_endpoint_descriptor *ep;
1281 iface_desc = serial->interface->cur_altsetting;
1282 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1283 ep = &iface_desc->endpoint[i].desc;
1284 if (ep->bEndpointAddress == endpoint)
1287 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1288 "endpoint %x\n", endpoint);
1292 static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
1293 int dir, void *ctx, char *buf, int len,
1294 void (*callback)(struct urb *))
1297 struct usb_endpoint_descriptor const *ep_desc;
1298 char const *ep_type_name;
1301 return NULL; /* endpoint not needed */
1303 dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %d.\n", __func__, endpoint);
1304 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1306 dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %d failed.\n", __func__, endpoint);
1310 if (endpoint == 0) {
1311 /* control EP filled in when used */
1315 ep_desc = find_ep(serial, endpoint);
1317 /* leak the urb, something's wrong and the callers don't care */
1320 if (usb_endpoint_xfer_int(ep_desc)) {
1321 ep_type_name = "INT";
1322 usb_fill_int_urb(urb, serial->dev,
1323 usb_sndintpipe(serial->dev, endpoint) | dir,
1324 buf, len, callback, ctx,
1325 ep_desc->bInterval);
1326 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1327 ep_type_name = "BULK";
1328 usb_fill_bulk_urb(urb, serial->dev,
1329 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1330 buf, len, callback, ctx);
1332 dev_warn(&serial->interface->dev,
1333 "unsupported endpoint type %x\n",
1334 usb_endpoint_type(ep_desc));
1339 dev_dbg(&serial->interface->dev, "%s - using urb %p for %s endpoint %x\n",
1340 __func__, urb, ep_type_name, endpoint);
1344 static struct callbacks {
1345 void (*instat_callback)(struct urb *);
1346 void (*glocont_callback)(struct urb *);
1347 void (*indat_callback)(struct urb *);
1348 void (*outdat_callback)(struct urb *);
1349 void (*inack_callback)(struct urb *);
1350 void (*outcont_callback)(struct urb *);
1351 } keyspan_callbacks[] = {
1353 /* msg_usa26 callbacks */
1354 .instat_callback = usa26_instat_callback,
1355 .glocont_callback = usa26_glocont_callback,
1356 .indat_callback = usa26_indat_callback,
1357 .outdat_callback = usa2x_outdat_callback,
1358 .inack_callback = usa26_inack_callback,
1359 .outcont_callback = usa26_outcont_callback,
1361 /* msg_usa28 callbacks */
1362 .instat_callback = usa28_instat_callback,
1363 .glocont_callback = usa28_glocont_callback,
1364 .indat_callback = usa28_indat_callback,
1365 .outdat_callback = usa2x_outdat_callback,
1366 .inack_callback = usa28_inack_callback,
1367 .outcont_callback = usa28_outcont_callback,
1369 /* msg_usa49 callbacks */
1370 .instat_callback = usa49_instat_callback,
1371 .glocont_callback = usa49_glocont_callback,
1372 .indat_callback = usa49_indat_callback,
1373 .outdat_callback = usa2x_outdat_callback,
1374 .inack_callback = usa49_inack_callback,
1375 .outcont_callback = usa49_outcont_callback,
1377 /* msg_usa90 callbacks */
1378 .instat_callback = usa90_instat_callback,
1379 .glocont_callback = usa28_glocont_callback,
1380 .indat_callback = usa90_indat_callback,
1381 .outdat_callback = usa2x_outdat_callback,
1382 .inack_callback = usa28_inack_callback,
1383 .outcont_callback = usa90_outcont_callback,
1385 /* msg_usa67 callbacks */
1386 .instat_callback = usa67_instat_callback,
1387 .glocont_callback = usa67_glocont_callback,
1388 .indat_callback = usa26_indat_callback,
1389 .outdat_callback = usa2x_outdat_callback,
1390 .inack_callback = usa26_inack_callback,
1391 .outcont_callback = usa26_outcont_callback,
1395 /* Generic setup urbs function that uses
1396 data in device_details */
1397 static void keyspan_setup_urbs(struct usb_serial *serial)
1400 struct keyspan_serial_private *s_priv;
1401 const struct keyspan_device_details *d_details;
1402 struct usb_serial_port *port;
1403 struct keyspan_port_private *p_priv;
1404 struct callbacks *cback;
1407 s_priv = usb_get_serial_data(serial);
1408 d_details = s_priv->device_details;
1410 /* Setup values for the various callback routines */
1411 cback = &keyspan_callbacks[d_details->msg_format];
1413 /* Allocate and set up urbs for each one that is in use,
1414 starting with instat endpoints */
1415 s_priv->instat_urb = keyspan_setup_urb
1416 (serial, d_details->instat_endpoint, USB_DIR_IN,
1417 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1418 cback->instat_callback);
1420 s_priv->indat_urb = keyspan_setup_urb
1421 (serial, d_details->indat_endpoint, USB_DIR_IN,
1422 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1423 usa49wg_indat_callback);
1425 s_priv->glocont_urb = keyspan_setup_urb
1426 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1427 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1428 cback->glocont_callback);
1430 /* Setup endpoints for each port specific thing */
1431 for (i = 0; i < d_details->num_ports; i++) {
1432 port = serial->port[i];
1433 p_priv = usb_get_serial_port_data(port);
1435 /* Do indat endpoints first, once for each flip */
1436 endp = d_details->indat_endpoints[i];
1437 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1438 p_priv->in_urbs[j] = keyspan_setup_urb
1439 (serial, endp, USB_DIR_IN, port,
1440 p_priv->in_buffer[j], 64,
1441 cback->indat_callback);
1444 p_priv->in_urbs[j] = NULL;
1446 /* outdat endpoints also have flip */
1447 endp = d_details->outdat_endpoints[i];
1448 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1449 p_priv->out_urbs[j] = keyspan_setup_urb
1450 (serial, endp, USB_DIR_OUT, port,
1451 p_priv->out_buffer[j], 64,
1452 cback->outdat_callback);
1455 p_priv->out_urbs[j] = NULL;
1457 /* inack endpoint */
1458 p_priv->inack_urb = keyspan_setup_urb
1459 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1460 port, p_priv->inack_buffer, 1, cback->inack_callback);
1462 /* outcont endpoint */
1463 p_priv->outcont_urb = keyspan_setup_urb
1464 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1465 port, p_priv->outcont_buffer, 64,
1466 cback->outcont_callback);
1470 /* usa19 function doesn't require prescaler */
1471 static int keyspan_usa19_calc_baud(struct usb_serial_port *port,
1472 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1473 u8 *rate_low, u8 *prescaler, int portnum)
1475 u32 b16, /* baud rate times 16 (actual rate used internally) */
1477 cnt; /* inverse of divisor (programmed into 8051) */
1479 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1481 /* prevent divide by zero... */
1482 b16 = baud_rate * 16L;
1484 return KEYSPAN_INVALID_BAUD_RATE;
1485 /* Any "standard" rate over 57k6 is marginal on the USA-19
1486 as we run out of divisor resolution. */
1487 if (baud_rate > 57600)
1488 return KEYSPAN_INVALID_BAUD_RATE;
1490 /* calculate the divisor and the counter (its inverse) */
1491 div = baudclk / b16;
1493 return KEYSPAN_INVALID_BAUD_RATE;
1498 return KEYSPAN_INVALID_BAUD_RATE;
1500 /* return the counter values if non-null */
1502 *rate_low = (u8) (cnt & 0xff);
1504 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1505 if (rate_low && rate_hi)
1506 dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
1507 __func__, baud_rate, *rate_hi, *rate_low);
1508 return KEYSPAN_BAUD_RATE_OK;
1511 /* usa19hs function doesn't require prescaler */
1512 static int keyspan_usa19hs_calc_baud(struct usb_serial_port *port,
1513 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1514 u8 *rate_low, u8 *prescaler, int portnum)
1516 u32 b16, /* baud rate times 16 (actual rate used internally) */
1519 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1521 /* prevent divide by zero... */
1522 b16 = baud_rate * 16L;
1524 return KEYSPAN_INVALID_BAUD_RATE;
1526 /* calculate the divisor */
1527 div = baudclk / b16;
1529 return KEYSPAN_INVALID_BAUD_RATE;
1532 return KEYSPAN_INVALID_BAUD_RATE;
1534 /* return the counter values if non-null */
1536 *rate_low = (u8) (div & 0xff);
1539 *rate_hi = (u8) ((div >> 8) & 0xff);
1541 if (rate_low && rate_hi)
1542 dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
1543 __func__, baud_rate, *rate_hi, *rate_low);
1545 return KEYSPAN_BAUD_RATE_OK;
1548 static int keyspan_usa19w_calc_baud(struct usb_serial_port *port,
1549 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1550 u8 *rate_low, u8 *prescaler, int portnum)
1552 u32 b16, /* baud rate times 16 (actual rate used internally) */
1553 clk, /* clock with 13/8 prescaler */
1554 div, /* divisor using 13/8 prescaler */
1555 res, /* resulting baud rate using 13/8 prescaler */
1556 diff, /* error using 13/8 prescaler */
1561 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1563 /* prevent divide by zero */
1564 b16 = baud_rate * 16L;
1566 return KEYSPAN_INVALID_BAUD_RATE;
1568 /* Calculate prescaler by trying them all and looking
1571 /* start with largest possible difference */
1572 smallest_diff = 0xffffffff;
1574 /* 0 is an invalid prescaler, used as a flag */
1577 for (i = 8; i <= 0xff; ++i) {
1578 clk = (baudclk * 8) / (u32) i;
1585 diff = (res > b16) ? (res-b16) : (b16-res);
1587 if (diff < smallest_diff) {
1589 smallest_diff = diff;
1593 if (best_prescaler == 0)
1594 return KEYSPAN_INVALID_BAUD_RATE;
1596 clk = (baudclk * 8) / (u32) best_prescaler;
1599 /* return the divisor and prescaler if non-null */
1601 *rate_low = (u8) (div & 0xff);
1603 *rate_hi = (u8) ((div >> 8) & 0xff);
1605 *prescaler = best_prescaler;
1606 /* dev_dbg(&port->dev, "%s - %d %d\n", __func__, *prescaler, div); */
1608 return KEYSPAN_BAUD_RATE_OK;
1611 /* USA-28 supports different maximum baud rates on each port */
1612 static int keyspan_usa28_calc_baud(struct usb_serial_port *port,
1613 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1614 u8 *rate_low, u8 *prescaler, int portnum)
1616 u32 b16, /* baud rate times 16 (actual rate used internally) */
1618 cnt; /* inverse of divisor (programmed into 8051) */
1620 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1622 /* prevent divide by zero */
1623 b16 = baud_rate * 16L;
1625 return KEYSPAN_INVALID_BAUD_RATE;
1627 /* calculate the divisor and the counter (its inverse) */
1628 div = KEYSPAN_USA28_BAUDCLK / b16;
1630 return KEYSPAN_INVALID_BAUD_RATE;
1634 /* check for out of range, based on portnum,
1635 and return result */
1638 return KEYSPAN_INVALID_BAUD_RATE;
1642 return KEYSPAN_INVALID_BAUD_RATE;
1644 return KEYSPAN_INVALID_BAUD_RATE;
1647 /* return the counter values if not NULL
1648 (port 1 will ignore retHi) */
1650 *rate_low = (u8) (cnt & 0xff);
1652 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1653 dev_dbg(&port->dev, "%s - %d OK.\n", __func__, baud_rate);
1654 return KEYSPAN_BAUD_RATE_OK;
1657 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1658 struct usb_serial_port *port,
1661 struct keyspan_usa26_portControlMessage msg;
1662 struct keyspan_serial_private *s_priv;
1663 struct keyspan_port_private *p_priv;
1664 const struct keyspan_device_details *d_details;
1666 struct urb *this_urb;
1667 int device_port, err;
1669 dev_dbg(&port->dev, "%s reset=%d\n", __func__, reset_port);
1671 s_priv = usb_get_serial_data(serial);
1672 p_priv = usb_get_serial_port_data(port);
1673 d_details = s_priv->device_details;
1674 device_port = port->number - port->serial->minor;
1676 outcont_urb = d_details->outcont_endpoints[port->number];
1677 this_urb = p_priv->outcont_urb;
1679 dev_dbg(&port->dev, "%s - endpoint %d\n", __func__, usb_pipeendpoint(this_urb->pipe));
1681 /* Make sure we have an urb then send the message */
1682 if (this_urb == NULL) {
1683 dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
1687 /* Save reset port val for resend.
1688 Don't overwrite resend for open/close condition. */
1689 if ((reset_port + 1) > p_priv->resend_cont)
1690 p_priv->resend_cont = reset_port + 1;
1691 if (this_urb->status == -EINPROGRESS) {
1692 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1697 memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
1699 /* Only set baud rate if it's changed */
1700 if (p_priv->old_baud != p_priv->baud) {
1701 p_priv->old_baud = p_priv->baud;
1702 msg.setClocking = 0xff;
1703 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
1704 &msg.baudHi, &msg.baudLo, &msg.prescaler,
1705 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1706 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
1707 __func__, p_priv->baud);
1709 msg.baudHi = 125; /* Values for 9600 baud */
1712 msg.setPrescaler = 0xff;
1715 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
1716 switch (p_priv->cflag & CSIZE) {
1718 msg.lcr |= USA_DATABITS_5;
1721 msg.lcr |= USA_DATABITS_6;
1724 msg.lcr |= USA_DATABITS_7;
1727 msg.lcr |= USA_DATABITS_8;
1730 if (p_priv->cflag & PARENB) {
1731 /* note USA_PARITY_NONE == 0 */
1732 msg.lcr |= (p_priv->cflag & PARODD) ?
1733 USA_PARITY_ODD : USA_PARITY_EVEN;
1737 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1738 msg.xonFlowControl = 0;
1739 msg.setFlowControl = 0xff;
1740 msg.forwardingLength = 16;
1745 if (reset_port == 1) {
1754 msg.returnStatus = 0;
1755 msg.resetDataToggle = 0xff;
1759 else if (reset_port == 2) {
1768 msg.returnStatus = 0;
1769 msg.resetDataToggle = 0;
1772 /* Sending intermediate configs */
1774 msg._txOn = (!p_priv->break_on);
1777 msg.txBreak = (p_priv->break_on);
1782 msg.returnStatus = 0;
1783 msg.resetDataToggle = 0x0;
1786 /* Do handshaking outputs */
1787 msg.setTxTriState_setRts = 0xff;
1788 msg.txTriState_rts = p_priv->rts_state;
1790 msg.setHskoa_setDtr = 0xff;
1791 msg.hskoa_dtr = p_priv->dtr_state;
1793 p_priv->resend_cont = 0;
1794 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1796 /* send the data out the device on control endpoint */
1797 this_urb->transfer_buffer_length = sizeof(msg);
1799 err = usb_submit_urb(this_urb, GFP_ATOMIC);
1801 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
1804 dev_dbg(&port->dev, "%s - usb_submit_urb(%d) OK %d bytes (end %d)\n", __func__
1805 outcont_urb, this_urb->transfer_buffer_length,
1806 usb_pipeendpoint(this_urb->pipe));
1813 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1814 struct usb_serial_port *port,
1817 struct keyspan_usa28_portControlMessage msg;
1818 struct keyspan_serial_private *s_priv;
1819 struct keyspan_port_private *p_priv;
1820 const struct keyspan_device_details *d_details;
1821 struct urb *this_urb;
1822 int device_port, err;
1824 s_priv = usb_get_serial_data(serial);
1825 p_priv = usb_get_serial_port_data(port);
1826 d_details = s_priv->device_details;
1827 device_port = port->number - port->serial->minor;
1829 /* only do something if we have a bulk out endpoint */
1830 this_urb = p_priv->outcont_urb;
1831 if (this_urb == NULL) {
1832 dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
1836 /* Save reset port val for resend.
1837 Don't overwrite resend for open/close condition. */
1838 if ((reset_port + 1) > p_priv->resend_cont)
1839 p_priv->resend_cont = reset_port + 1;
1840 if (this_urb->status == -EINPROGRESS) {
1841 dev_dbg(&port->dev, "%s already writing\n", __func__);
1846 memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
1848 msg.setBaudRate = 1;
1849 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
1850 &msg.baudHi, &msg.baudLo, NULL,
1851 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1852 dev_dbg(&port->dev, "%s - Invalid baud rate requested %d.\n",
1853 __func__, p_priv->baud);
1855 msg.baudHi = 0xb2; /* Values for 9600 baud */
1858 /* If parity is enabled, we must calculate it ourselves. */
1859 msg.parity = 0; /* XXX for now */
1861 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1862 msg.xonFlowControl = 0;
1864 /* Do handshaking outputs, DTR is inverted relative to RTS */
1865 msg.rts = p_priv->rts_state;
1866 msg.dtr = p_priv->dtr_state;
1868 msg.forwardingLength = 16;
1870 msg.breakThreshold = 45;
1874 /*msg.returnStatus = 1;
1875 msg.resetDataToggle = 0xff;*/
1877 if (reset_port == 1) {
1881 msg.txForceXoff = 0;
1887 msg.returnStatus = 0;
1888 msg.resetDataToggle = 0xff;
1891 else if (reset_port == 2) {
1895 msg.txForceXoff = 0;
1901 msg.returnStatus = 0;
1902 msg.resetDataToggle = 0;
1904 /* Sending intermediate configs */
1906 msg._txOn = (!p_priv->break_on);
1909 msg.txForceXoff = 0;
1910 msg.txBreak = (p_priv->break_on);
1915 msg.returnStatus = 0;
1916 msg.resetDataToggle = 0x0;
1919 p_priv->resend_cont = 0;
1920 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1922 /* send the data out the device on control endpoint */
1923 this_urb->transfer_buffer_length = sizeof(msg);
1925 err = usb_submit_urb(this_urb, GFP_ATOMIC);
1927 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed\n", __func__);
1930 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) OK %d bytes\n", __func__,
1931 this_urb->transfer_buffer_length);
1938 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1939 struct usb_serial_port *port,
1942 struct keyspan_usa49_portControlMessage msg;
1943 struct usb_ctrlrequest *dr = NULL;
1944 struct keyspan_serial_private *s_priv;
1945 struct keyspan_port_private *p_priv;
1946 const struct keyspan_device_details *d_details;
1947 struct urb *this_urb;
1948 int err, device_port;
1950 s_priv = usb_get_serial_data(serial);
1951 p_priv = usb_get_serial_port_data(port);
1952 d_details = s_priv->device_details;
1954 this_urb = s_priv->glocont_urb;
1956 /* Work out which port within the device is being setup */
1957 device_port = port->number - port->serial->minor;
1959 /* Make sure we have an urb then send the message */
1960 if (this_urb == NULL) {
1961 dev_dbg(&port->dev, "%s - oops no urb for port %d.\n", __func__, port->number);
1965 dev_dbg(&port->dev, "%s - endpoint %d port %d (%d)\n",
1966 __func__, usb_pipeendpoint(this_urb->pipe),
1967 port->number, device_port);
1969 /* Save reset port val for resend.
1970 Don't overwrite resend for open/close condition. */
1971 if ((reset_port + 1) > p_priv->resend_cont)
1972 p_priv->resend_cont = reset_port + 1;
1974 if (this_urb->status == -EINPROGRESS) {
1975 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1980 memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
1982 /*msg.portNumber = port->number;*/
1983 msg.portNumber = device_port;
1985 /* Only set baud rate if it's changed */
1986 if (p_priv->old_baud != p_priv->baud) {
1987 p_priv->old_baud = p_priv->baud;
1988 msg.setClocking = 0xff;
1989 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
1990 &msg.baudHi, &msg.baudLo, &msg.prescaler,
1991 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1992 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
1993 __func__, p_priv->baud);
1995 msg.baudHi = 125; /* Values for 9600 baud */
1998 /* msg.setPrescaler = 0xff; */
2001 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2002 switch (p_priv->cflag & CSIZE) {
2004 msg.lcr |= USA_DATABITS_5;
2007 msg.lcr |= USA_DATABITS_6;
2010 msg.lcr |= USA_DATABITS_7;
2013 msg.lcr |= USA_DATABITS_8;
2016 if (p_priv->cflag & PARENB) {
2017 /* note USA_PARITY_NONE == 0 */
2018 msg.lcr |= (p_priv->cflag & PARODD) ?
2019 USA_PARITY_ODD : USA_PARITY_EVEN;
2023 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2024 msg.xonFlowControl = 0;
2025 msg.setFlowControl = 0xff;
2027 msg.forwardingLength = 16;
2032 if (reset_port == 1) {
2041 msg.returnStatus = 0;
2042 msg.resetDataToggle = 0xff;
2044 msg.disablePort = 0;
2047 else if (reset_port == 2) {
2056 msg.returnStatus = 0;
2057 msg.resetDataToggle = 0;
2059 msg.disablePort = 1;
2061 /* Sending intermediate configs */
2063 msg._txOn = (!p_priv->break_on);
2066 msg.txBreak = (p_priv->break_on);
2071 msg.returnStatus = 0;
2072 msg.resetDataToggle = 0x0;
2074 msg.disablePort = 0;
2077 /* Do handshaking outputs */
2079 msg.rts = p_priv->rts_state;
2082 msg.dtr = p_priv->dtr_state;
2084 p_priv->resend_cont = 0;
2086 /* if the device is a 49wg, we send control message on usb
2089 if (d_details->product_id == keyspan_usa49wg_product_id) {
2090 dr = (void *)(s_priv->ctrl_buf);
2091 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2092 dr->bRequest = 0xB0; /* 49wg control message */;
2095 dr->wLength = cpu_to_le16(sizeof(msg));
2097 memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
2099 usb_fill_control_urb(this_urb, serial->dev,
2100 usb_sndctrlpipe(serial->dev, 0),
2101 (unsigned char *)dr, s_priv->glocont_buf,
2102 sizeof(msg), usa49_glocont_callback, serial);
2105 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2107 /* send the data out the device on control endpoint */
2108 this_urb->transfer_buffer_length = sizeof(msg);
2110 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2112 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2115 dev_dbg(&port->dev, "%s - usb_submit_urb(%d) OK %d bytes (end %d)\n", __func__,
2116 outcont_urb, this_urb->transfer_buffer_length,
2117 usb_pipeendpoint(this_urb->pipe));
2124 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2125 struct usb_serial_port *port,
2128 struct keyspan_usa90_portControlMessage msg;
2129 struct keyspan_serial_private *s_priv;
2130 struct keyspan_port_private *p_priv;
2131 const struct keyspan_device_details *d_details;
2132 struct urb *this_urb;
2136 s_priv = usb_get_serial_data(serial);
2137 p_priv = usb_get_serial_port_data(port);
2138 d_details = s_priv->device_details;
2140 /* only do something if we have a bulk out endpoint */
2141 this_urb = p_priv->outcont_urb;
2142 if (this_urb == NULL) {
2143 dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
2147 /* Save reset port val for resend.
2148 Don't overwrite resend for open/close condition. */
2149 if ((reset_port + 1) > p_priv->resend_cont)
2150 p_priv->resend_cont = reset_port + 1;
2151 if (this_urb->status == -EINPROGRESS) {
2152 dev_dbg(&port->dev, "%s already writing\n", __func__);
2157 memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
2159 /* Only set baud rate if it's changed */
2160 if (p_priv->old_baud != p_priv->baud) {
2161 p_priv->old_baud = p_priv->baud;
2162 msg.setClocking = 0x01;
2163 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2164 &msg.baudHi, &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
2165 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2166 __func__, p_priv->baud);
2167 p_priv->baud = 9600;
2168 d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2169 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2175 /* modes must always be correctly specified */
2176 if (p_priv->baud > 57600) {
2177 msg.rxMode = RXMODE_DMA;
2178 msg.txMode = TXMODE_DMA;
2180 msg.rxMode = RXMODE_BYHAND;
2181 msg.txMode = TXMODE_BYHAND;
2184 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2185 switch (p_priv->cflag & CSIZE) {
2187 msg.lcr |= USA_DATABITS_5;
2190 msg.lcr |= USA_DATABITS_6;
2193 msg.lcr |= USA_DATABITS_7;
2196 msg.lcr |= USA_DATABITS_8;
2199 if (p_priv->cflag & PARENB) {
2200 /* note USA_PARITY_NONE == 0 */
2201 msg.lcr |= (p_priv->cflag & PARODD) ?
2202 USA_PARITY_ODD : USA_PARITY_EVEN;
2204 if (p_priv->old_cflag != p_priv->cflag) {
2205 p_priv->old_cflag = p_priv->cflag;
2209 if (p_priv->flow_control == flow_cts)
2210 msg.txFlowControl = TXFLOW_CTS;
2211 msg.setTxFlowControl = 0x01;
2212 msg.setRxFlowControl = 0x01;
2214 msg.rxForwardingLength = 16;
2215 msg.rxForwardingTimeout = 16;
2216 msg.txAckSetting = 0;
2221 if (reset_port == 1) {
2222 msg.portEnabled = 1;
2224 msg.txBreak = (p_priv->break_on);
2227 else if (reset_port == 2)
2228 msg.portEnabled = 0;
2229 /* Sending intermediate configs */
2231 msg.portEnabled = 1;
2232 msg.txBreak = (p_priv->break_on);
2235 /* Do handshaking outputs */
2237 msg.rts = p_priv->rts_state;
2240 msg.dtr = p_priv->dtr_state;
2242 p_priv->resend_cont = 0;
2243 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2245 /* send the data out the device on control endpoint */
2246 this_urb->transfer_buffer_length = sizeof(msg);
2248 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2250 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2254 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2255 struct usb_serial_port *port,
2258 struct keyspan_usa67_portControlMessage msg;
2259 struct keyspan_serial_private *s_priv;
2260 struct keyspan_port_private *p_priv;
2261 const struct keyspan_device_details *d_details;
2262 struct urb *this_urb;
2263 int err, device_port;
2265 s_priv = usb_get_serial_data(serial);
2266 p_priv = usb_get_serial_port_data(port);
2267 d_details = s_priv->device_details;
2269 this_urb = s_priv->glocont_urb;
2271 /* Work out which port within the device is being setup */
2272 device_port = port->number - port->serial->minor;
2274 /* Make sure we have an urb then send the message */
2275 if (this_urb == NULL) {
2276 dev_dbg(&port->dev, "%s - oops no urb for port %d.\n", __func__,
2281 /* Save reset port val for resend.
2282 Don't overwrite resend for open/close condition. */
2283 if ((reset_port + 1) > p_priv->resend_cont)
2284 p_priv->resend_cont = reset_port + 1;
2285 if (this_urb->status == -EINPROGRESS) {
2286 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
2291 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2293 msg.port = device_port;
2295 /* Only set baud rate if it's changed */
2296 if (p_priv->old_baud != p_priv->baud) {
2297 p_priv->old_baud = p_priv->baud;
2298 msg.setClocking = 0xff;
2299 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2300 &msg.baudHi, &msg.baudLo, &msg.prescaler,
2301 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2302 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2303 __func__, p_priv->baud);
2305 msg.baudHi = 125; /* Values for 9600 baud */
2308 msg.setPrescaler = 0xff;
2311 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2312 switch (p_priv->cflag & CSIZE) {
2314 msg.lcr |= USA_DATABITS_5;
2317 msg.lcr |= USA_DATABITS_6;
2320 msg.lcr |= USA_DATABITS_7;
2323 msg.lcr |= USA_DATABITS_8;
2326 if (p_priv->cflag & PARENB) {
2327 /* note USA_PARITY_NONE == 0 */
2328 msg.lcr |= (p_priv->cflag & PARODD) ?
2329 USA_PARITY_ODD : USA_PARITY_EVEN;
2333 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2334 msg.xonFlowControl = 0;
2335 msg.setFlowControl = 0xff;
2336 msg.forwardingLength = 16;
2340 if (reset_port == 1) {
2350 msg.returnStatus = 0;
2351 msg.resetDataToggle = 0xff;
2352 } else if (reset_port == 2) {
2362 msg.returnStatus = 0;
2363 msg.resetDataToggle = 0;
2365 /* Sending intermediate configs */
2366 msg._txOn = (!p_priv->break_on);
2369 msg.txBreak = (p_priv->break_on);
2374 msg.returnStatus = 0;
2375 msg.resetDataToggle = 0x0;
2378 /* Do handshaking outputs */
2379 msg.setTxTriState_setRts = 0xff;
2380 msg.txTriState_rts = p_priv->rts_state;
2382 msg.setHskoa_setDtr = 0xff;
2383 msg.hskoa_dtr = p_priv->dtr_state;
2385 p_priv->resend_cont = 0;
2387 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2389 /* send the data out the device on control endpoint */
2390 this_urb->transfer_buffer_length = sizeof(msg);
2392 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2394 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2398 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2400 struct usb_serial *serial = port->serial;
2401 struct keyspan_serial_private *s_priv;
2402 const struct keyspan_device_details *d_details;
2404 s_priv = usb_get_serial_data(serial);
2405 d_details = s_priv->device_details;
2407 switch (d_details->msg_format) {
2409 keyspan_usa26_send_setup(serial, port, reset_port);
2412 keyspan_usa28_send_setup(serial, port, reset_port);
2415 keyspan_usa49_send_setup(serial, port, reset_port);
2418 keyspan_usa90_send_setup(serial, port, reset_port);
2421 keyspan_usa67_send_setup(serial, port, reset_port);
2427 /* Gets called by the "real" driver (ie once firmware is loaded
2428 and renumeration has taken place. */
2429 static int keyspan_startup(struct usb_serial *serial)
2432 struct usb_serial_port *port;
2433 struct keyspan_serial_private *s_priv;
2434 struct keyspan_port_private *p_priv;
2435 const struct keyspan_device_details *d_details;
2437 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2438 if (d_details->product_id ==
2439 le16_to_cpu(serial->dev->descriptor.idProduct))
2441 if (d_details == NULL) {
2442 dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
2443 __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2447 /* Setup private data for serial driver */
2448 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2450 dev_dbg(&serial->dev->dev, "%s - kmalloc for keyspan_serial_private failed.\n", __func__);
2454 s_priv->device_details = d_details;
2455 usb_set_serial_data(serial, s_priv);
2457 /* Now setup per port private data */
2458 for (i = 0; i < serial->num_ports; i++) {
2459 port = serial->port[i];
2460 p_priv = kzalloc(sizeof(struct keyspan_port_private),
2463 dev_dbg(&port->dev, "%s - kmalloc for keyspan_port_private (%d) failed!.\n", __func__, i);
2466 p_priv->device_details = d_details;
2467 usb_set_serial_port_data(port, p_priv);
2470 keyspan_setup_urbs(serial);
2472 if (s_priv->instat_urb != NULL) {
2473 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2475 dev_dbg(&serial->dev->dev, "%s - submit instat urb failed %d\n", __func__, err);
2477 if (s_priv->indat_urb != NULL) {
2478 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2480 dev_dbg(&serial->dev->dev, "%s - submit indat urb failed %d\n", __func__, err);
2486 static void keyspan_disconnect(struct usb_serial *serial)
2489 struct usb_serial_port *port;
2490 struct keyspan_serial_private *s_priv;
2491 struct keyspan_port_private *p_priv;
2493 s_priv = usb_get_serial_data(serial);
2495 /* Stop reading/writing urbs */
2496 stop_urb(s_priv->instat_urb);
2497 stop_urb(s_priv->glocont_urb);
2498 stop_urb(s_priv->indat_urb);
2499 for (i = 0; i < serial->num_ports; ++i) {
2500 port = serial->port[i];
2501 p_priv = usb_get_serial_port_data(port);
2502 stop_urb(p_priv->inack_urb);
2503 stop_urb(p_priv->outcont_urb);
2504 for (j = 0; j < 2; j++) {
2505 stop_urb(p_priv->in_urbs[j]);
2506 stop_urb(p_priv->out_urbs[j]);
2511 usb_free_urb(s_priv->instat_urb);
2512 usb_free_urb(s_priv->indat_urb);
2513 usb_free_urb(s_priv->glocont_urb);
2514 for (i = 0; i < serial->num_ports; ++i) {
2515 port = serial->port[i];
2516 p_priv = usb_get_serial_port_data(port);
2517 usb_free_urb(p_priv->inack_urb);
2518 usb_free_urb(p_priv->outcont_urb);
2519 for (j = 0; j < 2; j++) {
2520 usb_free_urb(p_priv->in_urbs[j]);
2521 usb_free_urb(p_priv->out_urbs[j]);
2526 static void keyspan_release(struct usb_serial *serial)
2529 struct usb_serial_port *port;
2530 struct keyspan_serial_private *s_priv;
2532 s_priv = usb_get_serial_data(serial);
2536 /* Now free per port private data */
2537 for (i = 0; i < serial->num_ports; i++) {
2538 port = serial->port[i];
2539 kfree(usb_get_serial_port_data(port));
2543 MODULE_AUTHOR(DRIVER_AUTHOR);
2544 MODULE_DESCRIPTION(DRIVER_DESC);
2545 MODULE_LICENSE("GPL");
2547 MODULE_FIRMWARE("keyspan/usa28.fw");
2548 MODULE_FIRMWARE("keyspan/usa28x.fw");
2549 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2550 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2551 MODULE_FIRMWARE("keyspan/usa19.fw");
2552 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2553 MODULE_FIRMWARE("keyspan/mpr.fw");
2554 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2555 MODULE_FIRMWARE("keyspan/usa18x.fw");
2556 MODULE_FIRMWARE("keyspan/usa19w.fw");
2557 MODULE_FIRMWARE("keyspan/usa49w.fw");
2558 MODULE_FIRMWARE("keyspan/usa49wlc.fw");