Linux 3.12.39
[linux/fpc-iii.git] / drivers / usb / serial / keyspan.c
blobe58e21b46ef0753126a2736886836d3c6da29698
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/uaccess.h>
42 #include <linux/usb.h>
43 #include <linux/usb/serial.h>
44 #include <linux/usb/ezusb.h>
45 #include "keyspan.h"
47 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
48 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
50 #define INSTAT_BUFLEN 32
51 #define GLOCONT_BUFLEN 64
52 #define INDAT49W_BUFLEN 512
53 #define IN_BUFLEN 64
54 #define OUT_BUFLEN 64
55 #define INACK_BUFLEN 1
56 #define OUTCONT_BUFLEN 64
58 /* Per device and per port private data */
59 struct keyspan_serial_private {
60 const struct keyspan_device_details *device_details;
62 struct urb *instat_urb;
63 char *instat_buf;
65 /* added to support 49wg, where data from all 4 ports comes in
66 on 1 EP and high-speed supported */
67 struct urb *indat_urb;
68 char *indat_buf;
70 /* XXX this one probably will need a lock */
71 struct urb *glocont_urb;
72 char *glocont_buf;
73 char *ctrl_buf; /* for EP0 control message */
76 struct keyspan_port_private {
77 /* Keep track of which input & output endpoints to use */
78 int in_flip;
79 int out_flip;
81 /* Keep duplicate of device details in each port
82 structure as well - simplifies some of the
83 callback functions etc. */
84 const struct keyspan_device_details *device_details;
86 /* Input endpoints and buffer for this port */
87 struct urb *in_urbs[2];
88 char *in_buffer[2];
89 /* Output endpoints and buffer for this port */
90 struct urb *out_urbs[2];
91 char *out_buffer[2];
93 /* Input ack endpoint */
94 struct urb *inack_urb;
95 char *inack_buffer;
97 /* Output control endpoint */
98 struct urb *outcont_urb;
99 char *outcont_buffer;
101 /* Settings for the port */
102 int baud;
103 int old_baud;
104 unsigned int cflag;
105 unsigned int old_cflag;
106 enum {flow_none, flow_cts, flow_xon} flow_control;
107 int rts_state; /* Handshaking pins (outputs) */
108 int dtr_state;
109 int cts_state; /* Handshaking pins (inputs) */
110 int dsr_state;
111 int dcd_state;
112 int ri_state;
113 int break_on;
115 unsigned long tx_start_time[2];
116 int resend_cont; /* need to resend control packet */
119 /* Include Keyspan message headers. All current Keyspan Adapters
120 make use of one of five message formats which are referred
121 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
122 within this driver. */
123 #include "keyspan_usa26msg.h"
124 #include "keyspan_usa28msg.h"
125 #include "keyspan_usa49msg.h"
126 #include "keyspan_usa90msg.h"
127 #include "keyspan_usa67msg.h"
130 module_usb_serial_driver(serial_drivers, keyspan_ids_combined);
132 static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
134 struct usb_serial_port *port = tty->driver_data;
135 struct keyspan_port_private *p_priv;
137 p_priv = usb_get_serial_port_data(port);
139 if (break_state == -1)
140 p_priv->break_on = 1;
141 else
142 p_priv->break_on = 0;
144 keyspan_send_setup(port, 0);
148 static void keyspan_set_termios(struct tty_struct *tty,
149 struct usb_serial_port *port, struct ktermios *old_termios)
151 int baud_rate, device_port;
152 struct keyspan_port_private *p_priv;
153 const struct keyspan_device_details *d_details;
154 unsigned int cflag;
156 p_priv = usb_get_serial_port_data(port);
157 d_details = p_priv->device_details;
158 cflag = tty->termios.c_cflag;
159 device_port = port->port_number;
161 /* Baud rate calculation takes baud rate as an integer
162 so other rates can be generated if desired. */
163 baud_rate = tty_get_baud_rate(tty);
164 /* If no match or invalid, don't change */
165 if (d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
166 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
167 /* FIXME - more to do here to ensure rate changes cleanly */
168 /* FIXME - calcuate exact rate from divisor ? */
169 p_priv->baud = baud_rate;
170 } else
171 baud_rate = tty_termios_baud_rate(old_termios);
173 tty_encode_baud_rate(tty, baud_rate, baud_rate);
174 /* set CTS/RTS handshake etc. */
175 p_priv->cflag = cflag;
176 p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
178 /* Mark/Space not supported */
179 tty->termios.c_cflag &= ~CMSPAR;
181 keyspan_send_setup(port, 0);
184 static int keyspan_tiocmget(struct tty_struct *tty)
186 struct usb_serial_port *port = tty->driver_data;
187 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
188 unsigned int value;
190 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
191 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
192 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
193 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
194 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
195 ((p_priv->ri_state) ? TIOCM_RNG : 0);
197 return value;
200 static int keyspan_tiocmset(struct tty_struct *tty,
201 unsigned int set, unsigned int clear)
203 struct usb_serial_port *port = tty->driver_data;
204 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
206 if (set & TIOCM_RTS)
207 p_priv->rts_state = 1;
208 if (set & TIOCM_DTR)
209 p_priv->dtr_state = 1;
210 if (clear & TIOCM_RTS)
211 p_priv->rts_state = 0;
212 if (clear & TIOCM_DTR)
213 p_priv->dtr_state = 0;
214 keyspan_send_setup(port, 0);
215 return 0;
218 /* Write function is similar for the four protocols used
219 with only a minor change for usa90 (usa19hs) required */
220 static int keyspan_write(struct tty_struct *tty,
221 struct usb_serial_port *port, const unsigned char *buf, int count)
223 struct keyspan_port_private *p_priv;
224 const struct keyspan_device_details *d_details;
225 int flip;
226 int left, todo;
227 struct urb *this_urb;
228 int err, maxDataLen, dataOffset;
230 p_priv = usb_get_serial_port_data(port);
231 d_details = p_priv->device_details;
233 if (d_details->msg_format == msg_usa90) {
234 maxDataLen = 64;
235 dataOffset = 0;
236 } else {
237 maxDataLen = 63;
238 dataOffset = 1;
241 dev_dbg(&port->dev, "%s - %d chars, flip=%d\n", __func__, count,
242 p_priv->out_flip);
244 for (left = count; left > 0; left -= todo) {
245 todo = left;
246 if (todo > maxDataLen)
247 todo = maxDataLen;
249 flip = p_priv->out_flip;
251 /* Check we have a valid urb/endpoint before we use it... */
252 this_urb = p_priv->out_urbs[flip];
253 if (this_urb == NULL) {
254 /* no bulk out, so return 0 bytes written */
255 dev_dbg(&port->dev, "%s - no output urb :(\n", __func__);
256 return count;
259 dev_dbg(&port->dev, "%s - endpoint %d flip %d\n",
260 __func__, usb_pipeendpoint(this_urb->pipe), flip);
262 if (this_urb->status == -EINPROGRESS) {
263 if (time_before(jiffies,
264 p_priv->tx_start_time[flip] + 10 * HZ))
265 break;
266 usb_unlink_urb(this_urb);
267 break;
270 /* First byte in buffer is "last flag" (except for usa19hx)
271 - unused so for now so set to zero */
272 ((char *)this_urb->transfer_buffer)[0] = 0;
274 memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
275 buf += todo;
277 /* send the data out the bulk port */
278 this_urb->transfer_buffer_length = todo + dataOffset;
280 err = usb_submit_urb(this_urb, GFP_ATOMIC);
281 if (err != 0)
282 dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed (%d)\n", err);
283 p_priv->tx_start_time[flip] = jiffies;
285 /* Flip for next time if usa26 or usa28 interface
286 (not used on usa49) */
287 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
290 return count - left;
293 static void usa26_indat_callback(struct urb *urb)
295 int i, err;
296 int endpoint;
297 struct usb_serial_port *port;
298 unsigned char *data = urb->transfer_buffer;
299 int status = urb->status;
301 endpoint = usb_pipeendpoint(urb->pipe);
303 if (status) {
304 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
305 __func__, status, endpoint);
306 return;
309 port = urb->context;
310 if (urb->actual_length) {
311 /* 0x80 bit is error flag */
312 if ((data[0] & 0x80) == 0) {
313 /* no errors on individual bytes, only
314 possible overrun err */
315 if (data[0] & RXERROR_OVERRUN) {
316 tty_insert_flip_char(&port->port, 0,
317 TTY_OVERRUN);
319 for (i = 1; i < urb->actual_length ; ++i)
320 tty_insert_flip_char(&port->port, data[i],
321 TTY_NORMAL);
322 } else {
323 /* some bytes had errors, every byte has status */
324 dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
325 for (i = 0; i + 1 < urb->actual_length; i += 2) {
326 int stat = data[i];
327 int flag = TTY_NORMAL;
329 if (stat & RXERROR_OVERRUN) {
330 tty_insert_flip_char(&port->port, 0,
331 TTY_OVERRUN);
333 /* XXX should handle break (0x10) */
334 if (stat & RXERROR_PARITY)
335 flag = TTY_PARITY;
336 else if (stat & RXERROR_FRAMING)
337 flag = TTY_FRAME;
339 tty_insert_flip_char(&port->port, data[i+1],
340 flag);
343 tty_flip_buffer_push(&port->port);
346 /* Resubmit urb so we continue receiving */
347 err = usb_submit_urb(urb, GFP_ATOMIC);
348 if (err != 0)
349 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
352 /* Outdat handling is common for all devices */
353 static void usa2x_outdat_callback(struct urb *urb)
355 struct usb_serial_port *port;
356 struct keyspan_port_private *p_priv;
358 port = urb->context;
359 p_priv = usb_get_serial_port_data(port);
360 dev_dbg(&port->dev, "%s - urb %d\n", __func__, urb == p_priv->out_urbs[1]);
362 usb_serial_port_softint(port);
365 static void usa26_inack_callback(struct urb *urb)
369 static void usa26_outcont_callback(struct urb *urb)
371 struct usb_serial_port *port;
372 struct keyspan_port_private *p_priv;
374 port = urb->context;
375 p_priv = usb_get_serial_port_data(port);
377 if (p_priv->resend_cont) {
378 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
379 keyspan_usa26_send_setup(port->serial, port,
380 p_priv->resend_cont - 1);
384 static void usa26_instat_callback(struct urb *urb)
386 unsigned char *data = urb->transfer_buffer;
387 struct keyspan_usa26_portStatusMessage *msg;
388 struct usb_serial *serial;
389 struct usb_serial_port *port;
390 struct keyspan_port_private *p_priv;
391 int old_dcd_state, err;
392 int status = urb->status;
394 serial = urb->context;
396 if (status) {
397 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
398 return;
400 if (urb->actual_length != 9) {
401 dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
402 goto exit;
405 msg = (struct keyspan_usa26_portStatusMessage *)data;
407 #if 0
408 dev_dbg(&urb->dev->dev,
409 "%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
410 __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr,
411 msg->ri, msg->_txOff, msg->_txXoff, msg->rxEnabled,
412 msg->controlResponse);
413 #endif
415 /* Now do something useful with the data */
418 /* Check port number from message and retrieve private data */
419 if (msg->port >= serial->num_ports) {
420 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
421 goto exit;
423 port = serial->port[msg->port];
424 p_priv = usb_get_serial_port_data(port);
425 if (!p_priv)
426 goto resubmit;
428 /* Update handshaking pin state information */
429 old_dcd_state = p_priv->dcd_state;
430 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
431 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
432 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
433 p_priv->ri_state = ((msg->ri) ? 1 : 0);
435 if (old_dcd_state != p_priv->dcd_state)
436 tty_port_tty_hangup(&port->port, true);
437 resubmit:
438 /* Resubmit urb so we continue receiving */
439 err = usb_submit_urb(urb, GFP_ATOMIC);
440 if (err != 0)
441 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
442 exit: ;
445 static void usa26_glocont_callback(struct urb *urb)
450 static void usa28_indat_callback(struct urb *urb)
452 int err;
453 struct usb_serial_port *port;
454 unsigned char *data;
455 struct keyspan_port_private *p_priv;
456 int status = urb->status;
458 port = urb->context;
459 p_priv = usb_get_serial_port_data(port);
460 data = urb->transfer_buffer;
462 if (urb != p_priv->in_urbs[p_priv->in_flip])
463 return;
465 do {
466 if (status) {
467 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
468 __func__, status, usb_pipeendpoint(urb->pipe));
469 return;
472 port = urb->context;
473 p_priv = usb_get_serial_port_data(port);
474 data = urb->transfer_buffer;
476 if (urb->actual_length) {
477 tty_insert_flip_string(&port->port, data,
478 urb->actual_length);
479 tty_flip_buffer_push(&port->port);
482 /* Resubmit urb so we continue receiving */
483 err = usb_submit_urb(urb, GFP_ATOMIC);
484 if (err != 0)
485 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n",
486 __func__, err);
487 p_priv->in_flip ^= 1;
489 urb = p_priv->in_urbs[p_priv->in_flip];
490 } while (urb->status != -EINPROGRESS);
493 static void usa28_inack_callback(struct urb *urb)
497 static void usa28_outcont_callback(struct urb *urb)
499 struct usb_serial_port *port;
500 struct keyspan_port_private *p_priv;
502 port = urb->context;
503 p_priv = usb_get_serial_port_data(port);
505 if (p_priv->resend_cont) {
506 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
507 keyspan_usa28_send_setup(port->serial, port,
508 p_priv->resend_cont - 1);
512 static void usa28_instat_callback(struct urb *urb)
514 int err;
515 unsigned char *data = urb->transfer_buffer;
516 struct keyspan_usa28_portStatusMessage *msg;
517 struct usb_serial *serial;
518 struct usb_serial_port *port;
519 struct keyspan_port_private *p_priv;
520 int old_dcd_state;
521 int status = urb->status;
523 serial = urb->context;
525 if (status) {
526 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
527 return;
530 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
531 dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
532 goto exit;
535 /*dev_dbg(&urb->dev->dev, "%s %12ph", __func__, data);*/
537 /* Now do something useful with the data */
538 msg = (struct keyspan_usa28_portStatusMessage *)data;
540 /* Check port number from message and retrieve private data */
541 if (msg->port >= serial->num_ports) {
542 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
543 goto exit;
545 port = serial->port[msg->port];
546 p_priv = usb_get_serial_port_data(port);
547 if (!p_priv)
548 goto resubmit;
550 /* Update handshaking pin state information */
551 old_dcd_state = p_priv->dcd_state;
552 p_priv->cts_state = ((msg->cts) ? 1 : 0);
553 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
554 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
555 p_priv->ri_state = ((msg->ri) ? 1 : 0);
557 if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
558 tty_port_tty_hangup(&port->port, true);
559 resubmit:
560 /* Resubmit urb so we continue receiving */
561 err = usb_submit_urb(urb, GFP_ATOMIC);
562 if (err != 0)
563 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
564 exit: ;
567 static void usa28_glocont_callback(struct urb *urb)
572 static void usa49_glocont_callback(struct urb *urb)
574 struct usb_serial *serial;
575 struct usb_serial_port *port;
576 struct keyspan_port_private *p_priv;
577 int i;
579 serial = urb->context;
580 for (i = 0; i < serial->num_ports; ++i) {
581 port = serial->port[i];
582 p_priv = usb_get_serial_port_data(port);
584 if (p_priv->resend_cont) {
585 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
586 keyspan_usa49_send_setup(serial, port,
587 p_priv->resend_cont - 1);
588 break;
593 /* This is actually called glostat in the Keyspan
594 doco */
595 static void usa49_instat_callback(struct urb *urb)
597 int err;
598 unsigned char *data = urb->transfer_buffer;
599 struct keyspan_usa49_portStatusMessage *msg;
600 struct usb_serial *serial;
601 struct usb_serial_port *port;
602 struct keyspan_port_private *p_priv;
603 int old_dcd_state;
604 int status = urb->status;
606 serial = urb->context;
608 if (status) {
609 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
610 return;
613 if (urb->actual_length !=
614 sizeof(struct keyspan_usa49_portStatusMessage)) {
615 dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
616 goto exit;
619 /*dev_dbg(&urb->dev->dev, "%s: %11ph", __func__, data);*/
621 /* Now do something useful with the data */
622 msg = (struct keyspan_usa49_portStatusMessage *)data;
624 /* Check port number from message and retrieve private data */
625 if (msg->portNumber >= serial->num_ports) {
626 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
627 __func__, msg->portNumber);
628 goto exit;
630 port = serial->port[msg->portNumber];
631 p_priv = usb_get_serial_port_data(port);
632 if (!p_priv)
633 goto resubmit;
635 /* Update handshaking pin state information */
636 old_dcd_state = p_priv->dcd_state;
637 p_priv->cts_state = ((msg->cts) ? 1 : 0);
638 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
639 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
640 p_priv->ri_state = ((msg->ri) ? 1 : 0);
642 if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
643 tty_port_tty_hangup(&port->port, true);
644 resubmit:
645 /* Resubmit urb so we continue receiving */
646 err = usb_submit_urb(urb, GFP_ATOMIC);
647 if (err != 0)
648 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
649 exit: ;
652 static void usa49_inack_callback(struct urb *urb)
656 static void usa49_indat_callback(struct urb *urb)
658 int i, err;
659 int endpoint;
660 struct usb_serial_port *port;
661 unsigned char *data = urb->transfer_buffer;
662 int status = urb->status;
664 endpoint = usb_pipeendpoint(urb->pipe);
666 if (status) {
667 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
668 __func__, status, endpoint);
669 return;
672 port = urb->context;
673 if (urb->actual_length) {
674 /* 0x80 bit is error flag */
675 if ((data[0] & 0x80) == 0) {
676 /* no error on any byte */
677 tty_insert_flip_string(&port->port, data + 1,
678 urb->actual_length - 1);
679 } else {
680 /* some bytes had errors, every byte has status */
681 for (i = 0; i + 1 < urb->actual_length; i += 2) {
682 int stat = data[i];
683 int flag = TTY_NORMAL;
685 if (stat & RXERROR_OVERRUN) {
686 tty_insert_flip_char(&port->port, 0,
687 TTY_OVERRUN);
689 /* XXX should handle break (0x10) */
690 if (stat & RXERROR_PARITY)
691 flag = TTY_PARITY;
692 else if (stat & RXERROR_FRAMING)
693 flag = TTY_FRAME;
695 tty_insert_flip_char(&port->port, data[i+1],
696 flag);
699 tty_flip_buffer_push(&port->port);
702 /* Resubmit urb so we continue receiving */
703 err = usb_submit_urb(urb, GFP_ATOMIC);
704 if (err != 0)
705 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
708 static void usa49wg_indat_callback(struct urb *urb)
710 int i, len, x, err;
711 struct usb_serial *serial;
712 struct usb_serial_port *port;
713 unsigned char *data = urb->transfer_buffer;
714 int status = urb->status;
716 serial = urb->context;
718 if (status) {
719 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
720 return;
723 /* inbound data is in the form P#, len, status, data */
724 i = 0;
725 len = 0;
727 while (i < urb->actual_length) {
729 /* Check port number from message */
730 if (data[i] >= serial->num_ports) {
731 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n",
732 __func__, data[i]);
733 return;
735 port = serial->port[data[i++]];
736 len = data[i++];
738 /* 0x80 bit is error flag */
739 if ((data[i] & 0x80) == 0) {
740 /* no error on any byte */
741 i++;
742 for (x = 1; x < len && i < urb->actual_length; ++x)
743 tty_insert_flip_char(&port->port,
744 data[i++], 0);
745 } else {
747 * some bytes had errors, every byte has status
749 for (x = 0; x + 1 < len &&
750 i + 1 < urb->actual_length; x += 2) {
751 int stat = data[i];
752 int flag = TTY_NORMAL;
754 if (stat & RXERROR_OVERRUN) {
755 tty_insert_flip_char(&port->port, 0,
756 TTY_OVERRUN);
758 /* XXX should handle break (0x10) */
759 if (stat & RXERROR_PARITY)
760 flag = TTY_PARITY;
761 else if (stat & RXERROR_FRAMING)
762 flag = TTY_FRAME;
764 tty_insert_flip_char(&port->port, data[i+1],
765 flag);
766 i += 2;
769 tty_flip_buffer_push(&port->port);
772 /* Resubmit urb so we continue receiving */
773 err = usb_submit_urb(urb, GFP_ATOMIC);
774 if (err != 0)
775 dev_dbg(&urb->dev->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
778 /* not used, usa-49 doesn't have per-port control endpoints */
779 static void usa49_outcont_callback(struct urb *urb)
783 static void usa90_indat_callback(struct urb *urb)
785 int i, err;
786 int endpoint;
787 struct usb_serial_port *port;
788 struct keyspan_port_private *p_priv;
789 unsigned char *data = urb->transfer_buffer;
790 int status = urb->status;
792 endpoint = usb_pipeendpoint(urb->pipe);
794 if (status) {
795 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x on endpoint %d.\n",
796 __func__, status, endpoint);
797 return;
800 port = urb->context;
801 p_priv = usb_get_serial_port_data(port);
803 if (urb->actual_length) {
804 /* if current mode is DMA, looks like usa28 format
805 otherwise looks like usa26 data format */
807 if (p_priv->baud > 57600)
808 tty_insert_flip_string(&port->port, data,
809 urb->actual_length);
810 else {
811 /* 0x80 bit is error flag */
812 if ((data[0] & 0x80) == 0) {
813 /* no errors on individual bytes, only
814 possible overrun err*/
815 if (data[0] & RXERROR_OVERRUN) {
816 tty_insert_flip_char(&port->port, 0,
817 TTY_OVERRUN);
819 for (i = 1; i < urb->actual_length ; ++i)
820 tty_insert_flip_char(&port->port,
821 data[i], TTY_NORMAL);
822 } else {
823 /* some bytes had errors, every byte has status */
824 dev_dbg(&port->dev, "%s - RX error!!!!\n", __func__);
825 for (i = 0; i + 1 < urb->actual_length; i += 2) {
826 int stat = data[i];
827 int flag = TTY_NORMAL;
829 if (stat & RXERROR_OVERRUN) {
830 tty_insert_flip_char(
831 &port->port, 0,
832 TTY_OVERRUN);
834 /* XXX should handle break (0x10) */
835 if (stat & RXERROR_PARITY)
836 flag = TTY_PARITY;
837 else if (stat & RXERROR_FRAMING)
838 flag = TTY_FRAME;
840 tty_insert_flip_char(&port->port,
841 data[i+1], flag);
845 tty_flip_buffer_push(&port->port);
848 /* Resubmit urb so we continue receiving */
849 err = usb_submit_urb(urb, GFP_ATOMIC);
850 if (err != 0)
851 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
855 static void usa90_instat_callback(struct urb *urb)
857 unsigned char *data = urb->transfer_buffer;
858 struct keyspan_usa90_portStatusMessage *msg;
859 struct usb_serial *serial;
860 struct usb_serial_port *port;
861 struct keyspan_port_private *p_priv;
862 int old_dcd_state, err;
863 int status = urb->status;
865 serial = urb->context;
867 if (status) {
868 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
869 return;
871 if (urb->actual_length < 14) {
872 dev_dbg(&urb->dev->dev, "%s - %d byte report??\n", __func__, urb->actual_length);
873 goto exit;
876 msg = (struct keyspan_usa90_portStatusMessage *)data;
878 /* Now do something useful with the data */
880 port = serial->port[0];
881 p_priv = usb_get_serial_port_data(port);
882 if (!p_priv)
883 goto resubmit;
885 /* Update handshaking pin state information */
886 old_dcd_state = p_priv->dcd_state;
887 p_priv->cts_state = ((msg->cts) ? 1 : 0);
888 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
889 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
890 p_priv->ri_state = ((msg->ri) ? 1 : 0);
892 if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
893 tty_port_tty_hangup(&port->port, true);
894 resubmit:
895 /* Resubmit urb so we continue receiving */
896 err = usb_submit_urb(urb, GFP_ATOMIC);
897 if (err != 0)
898 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
899 exit:
903 static void usa90_outcont_callback(struct urb *urb)
905 struct usb_serial_port *port;
906 struct keyspan_port_private *p_priv;
908 port = urb->context;
909 p_priv = usb_get_serial_port_data(port);
911 if (p_priv->resend_cont) {
912 dev_dbg(&urb->dev->dev, "%s - sending setup\n", __func__);
913 keyspan_usa90_send_setup(port->serial, port,
914 p_priv->resend_cont - 1);
918 /* Status messages from the 28xg */
919 static void usa67_instat_callback(struct urb *urb)
921 int err;
922 unsigned char *data = urb->transfer_buffer;
923 struct keyspan_usa67_portStatusMessage *msg;
924 struct usb_serial *serial;
925 struct usb_serial_port *port;
926 struct keyspan_port_private *p_priv;
927 int old_dcd_state;
928 int status = urb->status;
930 serial = urb->context;
932 if (status) {
933 dev_dbg(&urb->dev->dev, "%s - nonzero status: %x\n", __func__, status);
934 return;
937 if (urb->actual_length !=
938 sizeof(struct keyspan_usa67_portStatusMessage)) {
939 dev_dbg(&urb->dev->dev, "%s - bad length %d\n", __func__, urb->actual_length);
940 return;
944 /* Now do something useful with the data */
945 msg = (struct keyspan_usa67_portStatusMessage *)data;
947 /* Check port number from message and retrieve private data */
948 if (msg->port >= serial->num_ports) {
949 dev_dbg(&urb->dev->dev, "%s - Unexpected port number %d\n", __func__, msg->port);
950 return;
953 port = serial->port[msg->port];
954 p_priv = usb_get_serial_port_data(port);
955 if (!p_priv)
956 goto resubmit;
958 /* Update handshaking pin state information */
959 old_dcd_state = p_priv->dcd_state;
960 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
961 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
963 if (old_dcd_state != p_priv->dcd_state && old_dcd_state)
964 tty_port_tty_hangup(&port->port, true);
965 resubmit:
966 /* Resubmit urb so we continue receiving */
967 err = usb_submit_urb(urb, GFP_ATOMIC);
968 if (err != 0)
969 dev_dbg(&port->dev, "%s - resubmit read urb failed. (%d)\n", __func__, err);
972 static void usa67_glocont_callback(struct urb *urb)
974 struct usb_serial *serial;
975 struct usb_serial_port *port;
976 struct keyspan_port_private *p_priv;
977 int i;
979 serial = urb->context;
980 for (i = 0; i < serial->num_ports; ++i) {
981 port = serial->port[i];
982 p_priv = usb_get_serial_port_data(port);
984 if (p_priv->resend_cont) {
985 dev_dbg(&port->dev, "%s - sending setup\n", __func__);
986 keyspan_usa67_send_setup(serial, port,
987 p_priv->resend_cont - 1);
988 break;
993 static int keyspan_write_room(struct tty_struct *tty)
995 struct usb_serial_port *port = tty->driver_data;
996 struct keyspan_port_private *p_priv;
997 const struct keyspan_device_details *d_details;
998 int flip;
999 int data_len;
1000 struct urb *this_urb;
1002 p_priv = usb_get_serial_port_data(port);
1003 d_details = p_priv->device_details;
1005 /* FIXME: locking */
1006 if (d_details->msg_format == msg_usa90)
1007 data_len = 64;
1008 else
1009 data_len = 63;
1011 flip = p_priv->out_flip;
1013 /* Check both endpoints to see if any are available. */
1014 this_urb = p_priv->out_urbs[flip];
1015 if (this_urb != NULL) {
1016 if (this_urb->status != -EINPROGRESS)
1017 return data_len;
1018 flip = (flip + 1) & d_details->outdat_endp_flip;
1019 this_urb = p_priv->out_urbs[flip];
1020 if (this_urb != NULL) {
1021 if (this_urb->status != -EINPROGRESS)
1022 return data_len;
1025 return 0;
1029 static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port)
1031 struct keyspan_port_private *p_priv;
1032 const struct keyspan_device_details *d_details;
1033 int i, err;
1034 int baud_rate, device_port;
1035 struct urb *urb;
1036 unsigned int cflag = 0;
1038 p_priv = usb_get_serial_port_data(port);
1039 d_details = p_priv->device_details;
1041 /* Set some sane defaults */
1042 p_priv->rts_state = 1;
1043 p_priv->dtr_state = 1;
1044 p_priv->baud = 9600;
1046 /* force baud and lcr to be set on open */
1047 p_priv->old_baud = 0;
1048 p_priv->old_cflag = 0;
1050 p_priv->out_flip = 0;
1051 p_priv->in_flip = 0;
1053 /* Reset low level data toggle and start reading from endpoints */
1054 for (i = 0; i < 2; i++) {
1055 urb = p_priv->in_urbs[i];
1056 if (urb == NULL)
1057 continue;
1059 /* make sure endpoint data toggle is synchronized
1060 with the device */
1061 usb_clear_halt(urb->dev, urb->pipe);
1062 err = usb_submit_urb(urb, GFP_KERNEL);
1063 if (err != 0)
1064 dev_dbg(&port->dev, "%s - submit urb %d failed (%d)\n", __func__, i, err);
1067 /* Reset low level data toggle on out endpoints */
1068 for (i = 0; i < 2; i++) {
1069 urb = p_priv->out_urbs[i];
1070 if (urb == NULL)
1071 continue;
1072 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1073 usb_pipeout(urb->pipe), 0); */
1076 /* get the terminal config for the setup message now so we don't
1077 * need to send 2 of them */
1079 device_port = port->port_number;
1080 if (tty) {
1081 cflag = tty->termios.c_cflag;
1082 /* Baud rate calculation takes baud rate as an integer
1083 so other rates can be generated if desired. */
1084 baud_rate = tty_get_baud_rate(tty);
1085 /* If no match or invalid, leave as default */
1086 if (baud_rate >= 0
1087 && d_details->calculate_baud_rate(port, baud_rate, d_details->baudclk,
1088 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1089 p_priv->baud = baud_rate;
1092 /* set CTS/RTS handshake etc. */
1093 p_priv->cflag = cflag;
1094 p_priv->flow_control = (cflag & CRTSCTS) ? flow_cts : flow_none;
1096 keyspan_send_setup(port, 1);
1097 /* mdelay(100); */
1098 /* keyspan_set_termios(port, NULL); */
1100 return 0;
1103 static inline void stop_urb(struct urb *urb)
1105 if (urb && urb->status == -EINPROGRESS)
1106 usb_kill_urb(urb);
1109 static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
1111 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
1113 p_priv->rts_state = on;
1114 p_priv->dtr_state = on;
1115 keyspan_send_setup(port, 0);
1118 static void keyspan_close(struct usb_serial_port *port)
1120 int i;
1121 struct keyspan_port_private *p_priv;
1123 p_priv = usb_get_serial_port_data(port);
1125 p_priv->rts_state = 0;
1126 p_priv->dtr_state = 0;
1128 keyspan_send_setup(port, 2);
1129 /* pilot-xfer seems to work best with this delay */
1130 mdelay(100);
1132 p_priv->out_flip = 0;
1133 p_priv->in_flip = 0;
1135 stop_urb(p_priv->inack_urb);
1136 for (i = 0; i < 2; i++) {
1137 stop_urb(p_priv->in_urbs[i]);
1138 stop_urb(p_priv->out_urbs[i]);
1142 /* download the firmware to a pre-renumeration device */
1143 static int keyspan_fake_startup(struct usb_serial *serial)
1145 char *fw_name;
1147 dev_dbg(&serial->dev->dev, "Keyspan startup version %04x product %04x\n",
1148 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1149 le16_to_cpu(serial->dev->descriptor.idProduct));
1151 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
1152 != 0x8000) {
1153 dev_dbg(&serial->dev->dev, "Firmware already loaded. Quitting.\n");
1154 return 1;
1157 /* Select firmware image on the basis of idProduct */
1158 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1159 case keyspan_usa28_pre_product_id:
1160 fw_name = "keyspan/usa28.fw";
1161 break;
1163 case keyspan_usa28x_pre_product_id:
1164 fw_name = "keyspan/usa28x.fw";
1165 break;
1167 case keyspan_usa28xa_pre_product_id:
1168 fw_name = "keyspan/usa28xa.fw";
1169 break;
1171 case keyspan_usa28xb_pre_product_id:
1172 fw_name = "keyspan/usa28xb.fw";
1173 break;
1175 case keyspan_usa19_pre_product_id:
1176 fw_name = "keyspan/usa19.fw";
1177 break;
1179 case keyspan_usa19qi_pre_product_id:
1180 fw_name = "keyspan/usa19qi.fw";
1181 break;
1183 case keyspan_mpr_pre_product_id:
1184 fw_name = "keyspan/mpr.fw";
1185 break;
1187 case keyspan_usa19qw_pre_product_id:
1188 fw_name = "keyspan/usa19qw.fw";
1189 break;
1191 case keyspan_usa18x_pre_product_id:
1192 fw_name = "keyspan/usa18x.fw";
1193 break;
1195 case keyspan_usa19w_pre_product_id:
1196 fw_name = "keyspan/usa19w.fw";
1197 break;
1199 case keyspan_usa49w_pre_product_id:
1200 fw_name = "keyspan/usa49w.fw";
1201 break;
1203 case keyspan_usa49wlc_pre_product_id:
1204 fw_name = "keyspan/usa49wlc.fw";
1205 break;
1207 default:
1208 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1209 le16_to_cpu(serial->dev->descriptor.idProduct));
1210 return 1;
1213 dev_dbg(&serial->dev->dev, "Uploading Keyspan %s firmware.\n", fw_name);
1215 if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
1216 dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
1217 fw_name);
1218 return -ENOENT;
1221 /* after downloading firmware Renumeration will occur in a
1222 moment and the new device will bind to the real driver */
1224 /* we don't want this device to have a driver assigned to it. */
1225 return 1;
1228 /* Helper functions used by keyspan_setup_urbs */
1229 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1230 int endpoint)
1232 struct usb_host_interface *iface_desc;
1233 struct usb_endpoint_descriptor *ep;
1234 int i;
1236 iface_desc = serial->interface->cur_altsetting;
1237 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1238 ep = &iface_desc->endpoint[i].desc;
1239 if (ep->bEndpointAddress == endpoint)
1240 return ep;
1242 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1243 "endpoint %x\n", endpoint);
1244 return NULL;
1247 static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
1248 int dir, void *ctx, char *buf, int len,
1249 void (*callback)(struct urb *))
1251 struct urb *urb;
1252 struct usb_endpoint_descriptor const *ep_desc;
1253 char const *ep_type_name;
1255 if (endpoint == -1)
1256 return NULL; /* endpoint not needed */
1258 dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %d.\n", __func__, endpoint);
1259 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1260 if (urb == NULL) {
1261 dev_dbg(&serial->interface->dev, "%s - alloc for endpoint %d failed.\n", __func__, endpoint);
1262 return NULL;
1265 if (endpoint == 0) {
1266 /* control EP filled in when used */
1267 return urb;
1270 ep_desc = find_ep(serial, endpoint);
1271 if (!ep_desc) {
1272 /* leak the urb, something's wrong and the callers don't care */
1273 return urb;
1275 if (usb_endpoint_xfer_int(ep_desc)) {
1276 ep_type_name = "INT";
1277 usb_fill_int_urb(urb, serial->dev,
1278 usb_sndintpipe(serial->dev, endpoint) | dir,
1279 buf, len, callback, ctx,
1280 ep_desc->bInterval);
1281 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1282 ep_type_name = "BULK";
1283 usb_fill_bulk_urb(urb, serial->dev,
1284 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1285 buf, len, callback, ctx);
1286 } else {
1287 dev_warn(&serial->interface->dev,
1288 "unsupported endpoint type %x\n",
1289 usb_endpoint_type(ep_desc));
1290 usb_free_urb(urb);
1291 return NULL;
1294 dev_dbg(&serial->interface->dev, "%s - using urb %p for %s endpoint %x\n",
1295 __func__, urb, ep_type_name, endpoint);
1296 return urb;
1299 static struct callbacks {
1300 void (*instat_callback)(struct urb *);
1301 void (*glocont_callback)(struct urb *);
1302 void (*indat_callback)(struct urb *);
1303 void (*outdat_callback)(struct urb *);
1304 void (*inack_callback)(struct urb *);
1305 void (*outcont_callback)(struct urb *);
1306 } keyspan_callbacks[] = {
1308 /* msg_usa26 callbacks */
1309 .instat_callback = usa26_instat_callback,
1310 .glocont_callback = usa26_glocont_callback,
1311 .indat_callback = usa26_indat_callback,
1312 .outdat_callback = usa2x_outdat_callback,
1313 .inack_callback = usa26_inack_callback,
1314 .outcont_callback = usa26_outcont_callback,
1315 }, {
1316 /* msg_usa28 callbacks */
1317 .instat_callback = usa28_instat_callback,
1318 .glocont_callback = usa28_glocont_callback,
1319 .indat_callback = usa28_indat_callback,
1320 .outdat_callback = usa2x_outdat_callback,
1321 .inack_callback = usa28_inack_callback,
1322 .outcont_callback = usa28_outcont_callback,
1323 }, {
1324 /* msg_usa49 callbacks */
1325 .instat_callback = usa49_instat_callback,
1326 .glocont_callback = usa49_glocont_callback,
1327 .indat_callback = usa49_indat_callback,
1328 .outdat_callback = usa2x_outdat_callback,
1329 .inack_callback = usa49_inack_callback,
1330 .outcont_callback = usa49_outcont_callback,
1331 }, {
1332 /* msg_usa90 callbacks */
1333 .instat_callback = usa90_instat_callback,
1334 .glocont_callback = usa28_glocont_callback,
1335 .indat_callback = usa90_indat_callback,
1336 .outdat_callback = usa2x_outdat_callback,
1337 .inack_callback = usa28_inack_callback,
1338 .outcont_callback = usa90_outcont_callback,
1339 }, {
1340 /* msg_usa67 callbacks */
1341 .instat_callback = usa67_instat_callback,
1342 .glocont_callback = usa67_glocont_callback,
1343 .indat_callback = usa26_indat_callback,
1344 .outdat_callback = usa2x_outdat_callback,
1345 .inack_callback = usa26_inack_callback,
1346 .outcont_callback = usa26_outcont_callback,
1350 /* Generic setup urbs function that uses
1351 data in device_details */
1352 static void keyspan_setup_urbs(struct usb_serial *serial)
1354 struct keyspan_serial_private *s_priv;
1355 const struct keyspan_device_details *d_details;
1356 struct callbacks *cback;
1358 s_priv = usb_get_serial_data(serial);
1359 d_details = s_priv->device_details;
1361 /* Setup values for the various callback routines */
1362 cback = &keyspan_callbacks[d_details->msg_format];
1364 /* Allocate and set up urbs for each one that is in use,
1365 starting with instat endpoints */
1366 s_priv->instat_urb = keyspan_setup_urb
1367 (serial, d_details->instat_endpoint, USB_DIR_IN,
1368 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1369 cback->instat_callback);
1371 s_priv->indat_urb = keyspan_setup_urb
1372 (serial, d_details->indat_endpoint, USB_DIR_IN,
1373 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1374 usa49wg_indat_callback);
1376 s_priv->glocont_urb = keyspan_setup_urb
1377 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1378 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1379 cback->glocont_callback);
1382 /* usa19 function doesn't require prescaler */
1383 static int keyspan_usa19_calc_baud(struct usb_serial_port *port,
1384 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1385 u8 *rate_low, u8 *prescaler, int portnum)
1387 u32 b16, /* baud rate times 16 (actual rate used internally) */
1388 div, /* divisor */
1389 cnt; /* inverse of divisor (programmed into 8051) */
1391 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1393 /* prevent divide by zero... */
1394 b16 = baud_rate * 16L;
1395 if (b16 == 0)
1396 return KEYSPAN_INVALID_BAUD_RATE;
1397 /* Any "standard" rate over 57k6 is marginal on the USA-19
1398 as we run out of divisor resolution. */
1399 if (baud_rate > 57600)
1400 return KEYSPAN_INVALID_BAUD_RATE;
1402 /* calculate the divisor and the counter (its inverse) */
1403 div = baudclk / b16;
1404 if (div == 0)
1405 return KEYSPAN_INVALID_BAUD_RATE;
1406 else
1407 cnt = 0 - div;
1409 if (div > 0xffff)
1410 return KEYSPAN_INVALID_BAUD_RATE;
1412 /* return the counter values if non-null */
1413 if (rate_low)
1414 *rate_low = (u8) (cnt & 0xff);
1415 if (rate_hi)
1416 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1417 if (rate_low && rate_hi)
1418 dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
1419 __func__, baud_rate, *rate_hi, *rate_low);
1420 return KEYSPAN_BAUD_RATE_OK;
1423 /* usa19hs function doesn't require prescaler */
1424 static int keyspan_usa19hs_calc_baud(struct usb_serial_port *port,
1425 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1426 u8 *rate_low, u8 *prescaler, int portnum)
1428 u32 b16, /* baud rate times 16 (actual rate used internally) */
1429 div; /* divisor */
1431 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1433 /* prevent divide by zero... */
1434 b16 = baud_rate * 16L;
1435 if (b16 == 0)
1436 return KEYSPAN_INVALID_BAUD_RATE;
1438 /* calculate the divisor */
1439 div = baudclk / b16;
1440 if (div == 0)
1441 return KEYSPAN_INVALID_BAUD_RATE;
1443 if (div > 0xffff)
1444 return KEYSPAN_INVALID_BAUD_RATE;
1446 /* return the counter values if non-null */
1447 if (rate_low)
1448 *rate_low = (u8) (div & 0xff);
1450 if (rate_hi)
1451 *rate_hi = (u8) ((div >> 8) & 0xff);
1453 if (rate_low && rate_hi)
1454 dev_dbg(&port->dev, "%s - %d %02x %02x.\n",
1455 __func__, baud_rate, *rate_hi, *rate_low);
1457 return KEYSPAN_BAUD_RATE_OK;
1460 static int keyspan_usa19w_calc_baud(struct usb_serial_port *port,
1461 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1462 u8 *rate_low, u8 *prescaler, int portnum)
1464 u32 b16, /* baud rate times 16 (actual rate used internally) */
1465 clk, /* clock with 13/8 prescaler */
1466 div, /* divisor using 13/8 prescaler */
1467 res, /* resulting baud rate using 13/8 prescaler */
1468 diff, /* error using 13/8 prescaler */
1469 smallest_diff;
1470 u8 best_prescaler;
1471 int i;
1473 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1475 /* prevent divide by zero */
1476 b16 = baud_rate * 16L;
1477 if (b16 == 0)
1478 return KEYSPAN_INVALID_BAUD_RATE;
1480 /* Calculate prescaler by trying them all and looking
1481 for best fit */
1483 /* start with largest possible difference */
1484 smallest_diff = 0xffffffff;
1486 /* 0 is an invalid prescaler, used as a flag */
1487 best_prescaler = 0;
1489 for (i = 8; i <= 0xff; ++i) {
1490 clk = (baudclk * 8) / (u32) i;
1492 div = clk / b16;
1493 if (div == 0)
1494 continue;
1496 res = clk / div;
1497 diff = (res > b16) ? (res-b16) : (b16-res);
1499 if (diff < smallest_diff) {
1500 best_prescaler = i;
1501 smallest_diff = diff;
1505 if (best_prescaler == 0)
1506 return KEYSPAN_INVALID_BAUD_RATE;
1508 clk = (baudclk * 8) / (u32) best_prescaler;
1509 div = clk / b16;
1511 /* return the divisor and prescaler if non-null */
1512 if (rate_low)
1513 *rate_low = (u8) (div & 0xff);
1514 if (rate_hi)
1515 *rate_hi = (u8) ((div >> 8) & 0xff);
1516 if (prescaler) {
1517 *prescaler = best_prescaler;
1518 /* dev_dbg(&port->dev, "%s - %d %d\n", __func__, *prescaler, div); */
1520 return KEYSPAN_BAUD_RATE_OK;
1523 /* USA-28 supports different maximum baud rates on each port */
1524 static int keyspan_usa28_calc_baud(struct usb_serial_port *port,
1525 u32 baud_rate, u32 baudclk, u8 *rate_hi,
1526 u8 *rate_low, u8 *prescaler, int portnum)
1528 u32 b16, /* baud rate times 16 (actual rate used internally) */
1529 div, /* divisor */
1530 cnt; /* inverse of divisor (programmed into 8051) */
1532 dev_dbg(&port->dev, "%s - %d.\n", __func__, baud_rate);
1534 /* prevent divide by zero */
1535 b16 = baud_rate * 16L;
1536 if (b16 == 0)
1537 return KEYSPAN_INVALID_BAUD_RATE;
1539 /* calculate the divisor and the counter (its inverse) */
1540 div = KEYSPAN_USA28_BAUDCLK / b16;
1541 if (div == 0)
1542 return KEYSPAN_INVALID_BAUD_RATE;
1543 else
1544 cnt = 0 - div;
1546 /* check for out of range, based on portnum,
1547 and return result */
1548 if (portnum == 0) {
1549 if (div > 0xffff)
1550 return KEYSPAN_INVALID_BAUD_RATE;
1551 } else {
1552 if (portnum == 1) {
1553 if (div > 0xff)
1554 return KEYSPAN_INVALID_BAUD_RATE;
1555 } else
1556 return KEYSPAN_INVALID_BAUD_RATE;
1559 /* return the counter values if not NULL
1560 (port 1 will ignore retHi) */
1561 if (rate_low)
1562 *rate_low = (u8) (cnt & 0xff);
1563 if (rate_hi)
1564 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1565 dev_dbg(&port->dev, "%s - %d OK.\n", __func__, baud_rate);
1566 return KEYSPAN_BAUD_RATE_OK;
1569 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1570 struct usb_serial_port *port,
1571 int reset_port)
1573 struct keyspan_usa26_portControlMessage msg;
1574 struct keyspan_serial_private *s_priv;
1575 struct keyspan_port_private *p_priv;
1576 const struct keyspan_device_details *d_details;
1577 struct urb *this_urb;
1578 int device_port, err;
1580 dev_dbg(&port->dev, "%s reset=%d\n", __func__, reset_port);
1582 s_priv = usb_get_serial_data(serial);
1583 p_priv = usb_get_serial_port_data(port);
1584 d_details = s_priv->device_details;
1585 device_port = port->port_number;
1587 this_urb = p_priv->outcont_urb;
1589 dev_dbg(&port->dev, "%s - endpoint %d\n", __func__, usb_pipeendpoint(this_urb->pipe));
1591 /* Make sure we have an urb then send the message */
1592 if (this_urb == NULL) {
1593 dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
1594 return -1;
1597 /* Save reset port val for resend.
1598 Don't overwrite resend for open/close condition. */
1599 if ((reset_port + 1) > p_priv->resend_cont)
1600 p_priv->resend_cont = reset_port + 1;
1601 if (this_urb->status == -EINPROGRESS) {
1602 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1603 mdelay(5);
1604 return -1;
1607 memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
1609 /* Only set baud rate if it's changed */
1610 if (p_priv->old_baud != p_priv->baud) {
1611 p_priv->old_baud = p_priv->baud;
1612 msg.setClocking = 0xff;
1613 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
1614 &msg.baudHi, &msg.baudLo, &msg.prescaler,
1615 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1616 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
1617 __func__, p_priv->baud);
1618 msg.baudLo = 0;
1619 msg.baudHi = 125; /* Values for 9600 baud */
1620 msg.prescaler = 10;
1622 msg.setPrescaler = 0xff;
1625 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
1626 switch (p_priv->cflag & CSIZE) {
1627 case CS5:
1628 msg.lcr |= USA_DATABITS_5;
1629 break;
1630 case CS6:
1631 msg.lcr |= USA_DATABITS_6;
1632 break;
1633 case CS7:
1634 msg.lcr |= USA_DATABITS_7;
1635 break;
1636 case CS8:
1637 msg.lcr |= USA_DATABITS_8;
1638 break;
1640 if (p_priv->cflag & PARENB) {
1641 /* note USA_PARITY_NONE == 0 */
1642 msg.lcr |= (p_priv->cflag & PARODD) ?
1643 USA_PARITY_ODD : USA_PARITY_EVEN;
1645 msg.setLcr = 0xff;
1647 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1648 msg.xonFlowControl = 0;
1649 msg.setFlowControl = 0xff;
1650 msg.forwardingLength = 16;
1651 msg.xonChar = 17;
1652 msg.xoffChar = 19;
1654 /* Opening port */
1655 if (reset_port == 1) {
1656 msg._txOn = 1;
1657 msg._txOff = 0;
1658 msg.txFlush = 0;
1659 msg.txBreak = 0;
1660 msg.rxOn = 1;
1661 msg.rxOff = 0;
1662 msg.rxFlush = 1;
1663 msg.rxForward = 0;
1664 msg.returnStatus = 0;
1665 msg.resetDataToggle = 0xff;
1668 /* Closing port */
1669 else if (reset_port == 2) {
1670 msg._txOn = 0;
1671 msg._txOff = 1;
1672 msg.txFlush = 0;
1673 msg.txBreak = 0;
1674 msg.rxOn = 0;
1675 msg.rxOff = 1;
1676 msg.rxFlush = 1;
1677 msg.rxForward = 0;
1678 msg.returnStatus = 0;
1679 msg.resetDataToggle = 0;
1682 /* Sending intermediate configs */
1683 else {
1684 msg._txOn = (!p_priv->break_on);
1685 msg._txOff = 0;
1686 msg.txFlush = 0;
1687 msg.txBreak = (p_priv->break_on);
1688 msg.rxOn = 0;
1689 msg.rxOff = 0;
1690 msg.rxFlush = 0;
1691 msg.rxForward = 0;
1692 msg.returnStatus = 0;
1693 msg.resetDataToggle = 0x0;
1696 /* Do handshaking outputs */
1697 msg.setTxTriState_setRts = 0xff;
1698 msg.txTriState_rts = p_priv->rts_state;
1700 msg.setHskoa_setDtr = 0xff;
1701 msg.hskoa_dtr = p_priv->dtr_state;
1703 p_priv->resend_cont = 0;
1704 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1706 /* send the data out the device on control endpoint */
1707 this_urb->transfer_buffer_length = sizeof(msg);
1709 err = usb_submit_urb(this_urb, GFP_ATOMIC);
1710 if (err != 0)
1711 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
1712 return 0;
1715 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1716 struct usb_serial_port *port,
1717 int reset_port)
1719 struct keyspan_usa28_portControlMessage msg;
1720 struct keyspan_serial_private *s_priv;
1721 struct keyspan_port_private *p_priv;
1722 const struct keyspan_device_details *d_details;
1723 struct urb *this_urb;
1724 int device_port, err;
1726 s_priv = usb_get_serial_data(serial);
1727 p_priv = usb_get_serial_port_data(port);
1728 d_details = s_priv->device_details;
1729 device_port = port->port_number;
1731 /* only do something if we have a bulk out endpoint */
1732 this_urb = p_priv->outcont_urb;
1733 if (this_urb == NULL) {
1734 dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
1735 return -1;
1738 /* Save reset port val for resend.
1739 Don't overwrite resend for open/close condition. */
1740 if ((reset_port + 1) > p_priv->resend_cont)
1741 p_priv->resend_cont = reset_port + 1;
1742 if (this_urb->status == -EINPROGRESS) {
1743 dev_dbg(&port->dev, "%s already writing\n", __func__);
1744 mdelay(5);
1745 return -1;
1748 memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
1750 msg.setBaudRate = 1;
1751 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
1752 &msg.baudHi, &msg.baudLo, NULL,
1753 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1754 dev_dbg(&port->dev, "%s - Invalid baud rate requested %d.\n",
1755 __func__, p_priv->baud);
1756 msg.baudLo = 0xff;
1757 msg.baudHi = 0xb2; /* Values for 9600 baud */
1760 /* If parity is enabled, we must calculate it ourselves. */
1761 msg.parity = 0; /* XXX for now */
1763 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1764 msg.xonFlowControl = 0;
1766 /* Do handshaking outputs, DTR is inverted relative to RTS */
1767 msg.rts = p_priv->rts_state;
1768 msg.dtr = p_priv->dtr_state;
1770 msg.forwardingLength = 16;
1771 msg.forwardMs = 10;
1772 msg.breakThreshold = 45;
1773 msg.xonChar = 17;
1774 msg.xoffChar = 19;
1776 /*msg.returnStatus = 1;
1777 msg.resetDataToggle = 0xff;*/
1778 /* Opening port */
1779 if (reset_port == 1) {
1780 msg._txOn = 1;
1781 msg._txOff = 0;
1782 msg.txFlush = 0;
1783 msg.txForceXoff = 0;
1784 msg.txBreak = 0;
1785 msg.rxOn = 1;
1786 msg.rxOff = 0;
1787 msg.rxFlush = 1;
1788 msg.rxForward = 0;
1789 msg.returnStatus = 0;
1790 msg.resetDataToggle = 0xff;
1792 /* Closing port */
1793 else if (reset_port == 2) {
1794 msg._txOn = 0;
1795 msg._txOff = 1;
1796 msg.txFlush = 0;
1797 msg.txForceXoff = 0;
1798 msg.txBreak = 0;
1799 msg.rxOn = 0;
1800 msg.rxOff = 1;
1801 msg.rxFlush = 1;
1802 msg.rxForward = 0;
1803 msg.returnStatus = 0;
1804 msg.resetDataToggle = 0;
1806 /* Sending intermediate configs */
1807 else {
1808 msg._txOn = (!p_priv->break_on);
1809 msg._txOff = 0;
1810 msg.txFlush = 0;
1811 msg.txForceXoff = 0;
1812 msg.txBreak = (p_priv->break_on);
1813 msg.rxOn = 0;
1814 msg.rxOff = 0;
1815 msg.rxFlush = 0;
1816 msg.rxForward = 0;
1817 msg.returnStatus = 0;
1818 msg.resetDataToggle = 0x0;
1821 p_priv->resend_cont = 0;
1822 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1824 /* send the data out the device on control endpoint */
1825 this_urb->transfer_buffer_length = sizeof(msg);
1827 err = usb_submit_urb(this_urb, GFP_ATOMIC);
1828 if (err != 0)
1829 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed\n", __func__);
1830 #if 0
1831 else {
1832 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) OK %d bytes\n", __func__,
1833 this_urb->transfer_buffer_length);
1835 #endif
1837 return 0;
1840 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1841 struct usb_serial_port *port,
1842 int reset_port)
1844 struct keyspan_usa49_portControlMessage msg;
1845 struct usb_ctrlrequest *dr = NULL;
1846 struct keyspan_serial_private *s_priv;
1847 struct keyspan_port_private *p_priv;
1848 const struct keyspan_device_details *d_details;
1849 struct urb *this_urb;
1850 int err, device_port;
1852 s_priv = usb_get_serial_data(serial);
1853 p_priv = usb_get_serial_port_data(port);
1854 d_details = s_priv->device_details;
1856 this_urb = s_priv->glocont_urb;
1858 /* Work out which port within the device is being setup */
1859 device_port = port->port_number;
1861 /* Make sure we have an urb then send the message */
1862 if (this_urb == NULL) {
1863 dev_dbg(&port->dev, "%s - oops no urb for port.\n", __func__);
1864 return -1;
1867 dev_dbg(&port->dev, "%s - endpoint %d (%d)\n",
1868 __func__, usb_pipeendpoint(this_urb->pipe), device_port);
1870 /* Save reset port val for resend.
1871 Don't overwrite resend for open/close condition. */
1872 if ((reset_port + 1) > p_priv->resend_cont)
1873 p_priv->resend_cont = reset_port + 1;
1875 if (this_urb->status == -EINPROGRESS) {
1876 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1877 mdelay(5);
1878 return -1;
1881 memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
1883 msg.portNumber = device_port;
1885 /* Only set baud rate if it's changed */
1886 if (p_priv->old_baud != p_priv->baud) {
1887 p_priv->old_baud = p_priv->baud;
1888 msg.setClocking = 0xff;
1889 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
1890 &msg.baudHi, &msg.baudLo, &msg.prescaler,
1891 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1892 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
1893 __func__, p_priv->baud);
1894 msg.baudLo = 0;
1895 msg.baudHi = 125; /* Values for 9600 baud */
1896 msg.prescaler = 10;
1898 /* msg.setPrescaler = 0xff; */
1901 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
1902 switch (p_priv->cflag & CSIZE) {
1903 case CS5:
1904 msg.lcr |= USA_DATABITS_5;
1905 break;
1906 case CS6:
1907 msg.lcr |= USA_DATABITS_6;
1908 break;
1909 case CS7:
1910 msg.lcr |= USA_DATABITS_7;
1911 break;
1912 case CS8:
1913 msg.lcr |= USA_DATABITS_8;
1914 break;
1916 if (p_priv->cflag & PARENB) {
1917 /* note USA_PARITY_NONE == 0 */
1918 msg.lcr |= (p_priv->cflag & PARODD) ?
1919 USA_PARITY_ODD : USA_PARITY_EVEN;
1921 msg.setLcr = 0xff;
1923 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1924 msg.xonFlowControl = 0;
1925 msg.setFlowControl = 0xff;
1927 msg.forwardingLength = 16;
1928 msg.xonChar = 17;
1929 msg.xoffChar = 19;
1931 /* Opening port */
1932 if (reset_port == 1) {
1933 msg._txOn = 1;
1934 msg._txOff = 0;
1935 msg.txFlush = 0;
1936 msg.txBreak = 0;
1937 msg.rxOn = 1;
1938 msg.rxOff = 0;
1939 msg.rxFlush = 1;
1940 msg.rxForward = 0;
1941 msg.returnStatus = 0;
1942 msg.resetDataToggle = 0xff;
1943 msg.enablePort = 1;
1944 msg.disablePort = 0;
1946 /* Closing port */
1947 else if (reset_port == 2) {
1948 msg._txOn = 0;
1949 msg._txOff = 1;
1950 msg.txFlush = 0;
1951 msg.txBreak = 0;
1952 msg.rxOn = 0;
1953 msg.rxOff = 1;
1954 msg.rxFlush = 1;
1955 msg.rxForward = 0;
1956 msg.returnStatus = 0;
1957 msg.resetDataToggle = 0;
1958 msg.enablePort = 0;
1959 msg.disablePort = 1;
1961 /* Sending intermediate configs */
1962 else {
1963 msg._txOn = (!p_priv->break_on);
1964 msg._txOff = 0;
1965 msg.txFlush = 0;
1966 msg.txBreak = (p_priv->break_on);
1967 msg.rxOn = 0;
1968 msg.rxOff = 0;
1969 msg.rxFlush = 0;
1970 msg.rxForward = 0;
1971 msg.returnStatus = 0;
1972 msg.resetDataToggle = 0x0;
1973 msg.enablePort = 0;
1974 msg.disablePort = 0;
1977 /* Do handshaking outputs */
1978 msg.setRts = 0xff;
1979 msg.rts = p_priv->rts_state;
1981 msg.setDtr = 0xff;
1982 msg.dtr = p_priv->dtr_state;
1984 p_priv->resend_cont = 0;
1986 /* if the device is a 49wg, we send control message on usb
1987 control EP 0 */
1989 if (d_details->product_id == keyspan_usa49wg_product_id) {
1990 dr = (void *)(s_priv->ctrl_buf);
1991 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
1992 dr->bRequest = 0xB0; /* 49wg control message */;
1993 dr->wValue = 0;
1994 dr->wIndex = 0;
1995 dr->wLength = cpu_to_le16(sizeof(msg));
1997 memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
1999 usb_fill_control_urb(this_urb, serial->dev,
2000 usb_sndctrlpipe(serial->dev, 0),
2001 (unsigned char *)dr, s_priv->glocont_buf,
2002 sizeof(msg), usa49_glocont_callback, serial);
2004 } else {
2005 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2007 /* send the data out the device on control endpoint */
2008 this_urb->transfer_buffer_length = sizeof(msg);
2010 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2011 if (err != 0)
2012 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2013 #if 0
2014 else {
2015 dev_dbg(&port->dev, "%s - usb_submit_urb(%d) OK %d bytes (end %d)\n", __func__,
2016 outcont_urb, this_urb->transfer_buffer_length,
2017 usb_pipeendpoint(this_urb->pipe));
2019 #endif
2021 return 0;
2024 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2025 struct usb_serial_port *port,
2026 int reset_port)
2028 struct keyspan_usa90_portControlMessage msg;
2029 struct keyspan_serial_private *s_priv;
2030 struct keyspan_port_private *p_priv;
2031 const struct keyspan_device_details *d_details;
2032 struct urb *this_urb;
2033 int err;
2034 u8 prescaler;
2036 s_priv = usb_get_serial_data(serial);
2037 p_priv = usb_get_serial_port_data(port);
2038 d_details = s_priv->device_details;
2040 /* only do something if we have a bulk out endpoint */
2041 this_urb = p_priv->outcont_urb;
2042 if (this_urb == NULL) {
2043 dev_dbg(&port->dev, "%s - oops no urb.\n", __func__);
2044 return -1;
2047 /* Save reset port val for resend.
2048 Don't overwrite resend for open/close condition. */
2049 if ((reset_port + 1) > p_priv->resend_cont)
2050 p_priv->resend_cont = reset_port + 1;
2051 if (this_urb->status == -EINPROGRESS) {
2052 dev_dbg(&port->dev, "%s already writing\n", __func__);
2053 mdelay(5);
2054 return -1;
2057 memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
2059 /* Only set baud rate if it's changed */
2060 if (p_priv->old_baud != p_priv->baud) {
2061 p_priv->old_baud = p_priv->baud;
2062 msg.setClocking = 0x01;
2063 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2064 &msg.baudHi, &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
2065 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2066 __func__, p_priv->baud);
2067 p_priv->baud = 9600;
2068 d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2069 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2071 msg.setRxMode = 1;
2072 msg.setTxMode = 1;
2075 /* modes must always be correctly specified */
2076 if (p_priv->baud > 57600) {
2077 msg.rxMode = RXMODE_DMA;
2078 msg.txMode = TXMODE_DMA;
2079 } else {
2080 msg.rxMode = RXMODE_BYHAND;
2081 msg.txMode = TXMODE_BYHAND;
2084 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2085 switch (p_priv->cflag & CSIZE) {
2086 case CS5:
2087 msg.lcr |= USA_DATABITS_5;
2088 break;
2089 case CS6:
2090 msg.lcr |= USA_DATABITS_6;
2091 break;
2092 case CS7:
2093 msg.lcr |= USA_DATABITS_7;
2094 break;
2095 case CS8:
2096 msg.lcr |= USA_DATABITS_8;
2097 break;
2099 if (p_priv->cflag & PARENB) {
2100 /* note USA_PARITY_NONE == 0 */
2101 msg.lcr |= (p_priv->cflag & PARODD) ?
2102 USA_PARITY_ODD : USA_PARITY_EVEN;
2104 if (p_priv->old_cflag != p_priv->cflag) {
2105 p_priv->old_cflag = p_priv->cflag;
2106 msg.setLcr = 0x01;
2109 if (p_priv->flow_control == flow_cts)
2110 msg.txFlowControl = TXFLOW_CTS;
2111 msg.setTxFlowControl = 0x01;
2112 msg.setRxFlowControl = 0x01;
2114 msg.rxForwardingLength = 16;
2115 msg.rxForwardingTimeout = 16;
2116 msg.txAckSetting = 0;
2117 msg.xonChar = 17;
2118 msg.xoffChar = 19;
2120 /* Opening port */
2121 if (reset_port == 1) {
2122 msg.portEnabled = 1;
2123 msg.rxFlush = 1;
2124 msg.txBreak = (p_priv->break_on);
2126 /* Closing port */
2127 else if (reset_port == 2)
2128 msg.portEnabled = 0;
2129 /* Sending intermediate configs */
2130 else {
2131 msg.portEnabled = 1;
2132 msg.txBreak = (p_priv->break_on);
2135 /* Do handshaking outputs */
2136 msg.setRts = 0x01;
2137 msg.rts = p_priv->rts_state;
2139 msg.setDtr = 0x01;
2140 msg.dtr = p_priv->dtr_state;
2142 p_priv->resend_cont = 0;
2143 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2145 /* send the data out the device on control endpoint */
2146 this_urb->transfer_buffer_length = sizeof(msg);
2148 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2149 if (err != 0)
2150 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2151 return 0;
2154 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2155 struct usb_serial_port *port,
2156 int reset_port)
2158 struct keyspan_usa67_portControlMessage msg;
2159 struct keyspan_serial_private *s_priv;
2160 struct keyspan_port_private *p_priv;
2161 const struct keyspan_device_details *d_details;
2162 struct urb *this_urb;
2163 int err, device_port;
2165 s_priv = usb_get_serial_data(serial);
2166 p_priv = usb_get_serial_port_data(port);
2167 d_details = s_priv->device_details;
2169 this_urb = s_priv->glocont_urb;
2171 /* Work out which port within the device is being setup */
2172 device_port = port->port_number;
2174 /* Make sure we have an urb then send the message */
2175 if (this_urb == NULL) {
2176 dev_dbg(&port->dev, "%s - oops no urb for port.\n", __func__);
2177 return -1;
2180 /* Save reset port val for resend.
2181 Don't overwrite resend for open/close condition. */
2182 if ((reset_port + 1) > p_priv->resend_cont)
2183 p_priv->resend_cont = reset_port + 1;
2184 if (this_urb->status == -EINPROGRESS) {
2185 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
2186 mdelay(5);
2187 return -1;
2190 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2192 msg.port = device_port;
2194 /* Only set baud rate if it's changed */
2195 if (p_priv->old_baud != p_priv->baud) {
2196 p_priv->old_baud = p_priv->baud;
2197 msg.setClocking = 0xff;
2198 if (d_details->calculate_baud_rate(port, p_priv->baud, d_details->baudclk,
2199 &msg.baudHi, &msg.baudLo, &msg.prescaler,
2200 device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2201 dev_dbg(&port->dev, "%s - Invalid baud rate %d requested, using 9600.\n",
2202 __func__, p_priv->baud);
2203 msg.baudLo = 0;
2204 msg.baudHi = 125; /* Values for 9600 baud */
2205 msg.prescaler = 10;
2207 msg.setPrescaler = 0xff;
2210 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2211 switch (p_priv->cflag & CSIZE) {
2212 case CS5:
2213 msg.lcr |= USA_DATABITS_5;
2214 break;
2215 case CS6:
2216 msg.lcr |= USA_DATABITS_6;
2217 break;
2218 case CS7:
2219 msg.lcr |= USA_DATABITS_7;
2220 break;
2221 case CS8:
2222 msg.lcr |= USA_DATABITS_8;
2223 break;
2225 if (p_priv->cflag & PARENB) {
2226 /* note USA_PARITY_NONE == 0 */
2227 msg.lcr |= (p_priv->cflag & PARODD) ?
2228 USA_PARITY_ODD : USA_PARITY_EVEN;
2230 msg.setLcr = 0xff;
2232 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2233 msg.xonFlowControl = 0;
2234 msg.setFlowControl = 0xff;
2235 msg.forwardingLength = 16;
2236 msg.xonChar = 17;
2237 msg.xoffChar = 19;
2239 if (reset_port == 1) {
2240 /* Opening port */
2241 msg._txOn = 1;
2242 msg._txOff = 0;
2243 msg.txFlush = 0;
2244 msg.txBreak = 0;
2245 msg.rxOn = 1;
2246 msg.rxOff = 0;
2247 msg.rxFlush = 1;
2248 msg.rxForward = 0;
2249 msg.returnStatus = 0;
2250 msg.resetDataToggle = 0xff;
2251 } else if (reset_port == 2) {
2252 /* Closing port */
2253 msg._txOn = 0;
2254 msg._txOff = 1;
2255 msg.txFlush = 0;
2256 msg.txBreak = 0;
2257 msg.rxOn = 0;
2258 msg.rxOff = 1;
2259 msg.rxFlush = 1;
2260 msg.rxForward = 0;
2261 msg.returnStatus = 0;
2262 msg.resetDataToggle = 0;
2263 } else {
2264 /* Sending intermediate configs */
2265 msg._txOn = (!p_priv->break_on);
2266 msg._txOff = 0;
2267 msg.txFlush = 0;
2268 msg.txBreak = (p_priv->break_on);
2269 msg.rxOn = 0;
2270 msg.rxOff = 0;
2271 msg.rxFlush = 0;
2272 msg.rxForward = 0;
2273 msg.returnStatus = 0;
2274 msg.resetDataToggle = 0x0;
2277 /* Do handshaking outputs */
2278 msg.setTxTriState_setRts = 0xff;
2279 msg.txTriState_rts = p_priv->rts_state;
2281 msg.setHskoa_setDtr = 0xff;
2282 msg.hskoa_dtr = p_priv->dtr_state;
2284 p_priv->resend_cont = 0;
2286 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2288 /* send the data out the device on control endpoint */
2289 this_urb->transfer_buffer_length = sizeof(msg);
2291 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2292 if (err != 0)
2293 dev_dbg(&port->dev, "%s - usb_submit_urb(setup) failed (%d)\n", __func__, err);
2294 return 0;
2297 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2299 struct usb_serial *serial = port->serial;
2300 struct keyspan_serial_private *s_priv;
2301 const struct keyspan_device_details *d_details;
2303 s_priv = usb_get_serial_data(serial);
2304 d_details = s_priv->device_details;
2306 switch (d_details->msg_format) {
2307 case msg_usa26:
2308 keyspan_usa26_send_setup(serial, port, reset_port);
2309 break;
2310 case msg_usa28:
2311 keyspan_usa28_send_setup(serial, port, reset_port);
2312 break;
2313 case msg_usa49:
2314 keyspan_usa49_send_setup(serial, port, reset_port);
2315 break;
2316 case msg_usa90:
2317 keyspan_usa90_send_setup(serial, port, reset_port);
2318 break;
2319 case msg_usa67:
2320 keyspan_usa67_send_setup(serial, port, reset_port);
2321 break;
2326 /* Gets called by the "real" driver (ie once firmware is loaded
2327 and renumeration has taken place. */
2328 static int keyspan_startup(struct usb_serial *serial)
2330 int i, err;
2331 struct keyspan_serial_private *s_priv;
2332 const struct keyspan_device_details *d_details;
2334 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2335 if (d_details->product_id ==
2336 le16_to_cpu(serial->dev->descriptor.idProduct))
2337 break;
2338 if (d_details == NULL) {
2339 dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
2340 __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2341 return -ENODEV;
2344 /* Setup private data for serial driver */
2345 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2346 if (!s_priv) {
2347 dev_dbg(&serial->dev->dev, "%s - kmalloc for keyspan_serial_private failed.\n", __func__);
2348 return -ENOMEM;
2351 s_priv->instat_buf = kzalloc(INSTAT_BUFLEN, GFP_KERNEL);
2352 if (!s_priv->instat_buf)
2353 goto err_instat_buf;
2355 s_priv->indat_buf = kzalloc(INDAT49W_BUFLEN, GFP_KERNEL);
2356 if (!s_priv->indat_buf)
2357 goto err_indat_buf;
2359 s_priv->glocont_buf = kzalloc(GLOCONT_BUFLEN, GFP_KERNEL);
2360 if (!s_priv->glocont_buf)
2361 goto err_glocont_buf;
2363 s_priv->ctrl_buf = kzalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
2364 if (!s_priv->ctrl_buf)
2365 goto err_ctrl_buf;
2367 s_priv->device_details = d_details;
2368 usb_set_serial_data(serial, s_priv);
2370 keyspan_setup_urbs(serial);
2372 if (s_priv->instat_urb != NULL) {
2373 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2374 if (err != 0)
2375 dev_dbg(&serial->dev->dev, "%s - submit instat urb failed %d\n", __func__, err);
2377 if (s_priv->indat_urb != NULL) {
2378 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2379 if (err != 0)
2380 dev_dbg(&serial->dev->dev, "%s - submit indat urb failed %d\n", __func__, err);
2383 return 0;
2385 err_ctrl_buf:
2386 kfree(s_priv->glocont_buf);
2387 err_glocont_buf:
2388 kfree(s_priv->indat_buf);
2389 err_indat_buf:
2390 kfree(s_priv->instat_buf);
2391 err_instat_buf:
2392 kfree(s_priv);
2394 return -ENOMEM;
2397 static void keyspan_disconnect(struct usb_serial *serial)
2399 struct keyspan_serial_private *s_priv;
2401 s_priv = usb_get_serial_data(serial);
2403 stop_urb(s_priv->instat_urb);
2404 stop_urb(s_priv->glocont_urb);
2405 stop_urb(s_priv->indat_urb);
2408 static void keyspan_release(struct usb_serial *serial)
2410 struct keyspan_serial_private *s_priv;
2412 s_priv = usb_get_serial_data(serial);
2414 usb_free_urb(s_priv->instat_urb);
2415 usb_free_urb(s_priv->indat_urb);
2416 usb_free_urb(s_priv->glocont_urb);
2418 kfree(s_priv->ctrl_buf);
2419 kfree(s_priv->glocont_buf);
2420 kfree(s_priv->indat_buf);
2421 kfree(s_priv->instat_buf);
2423 kfree(s_priv);
2426 static int keyspan_port_probe(struct usb_serial_port *port)
2428 struct usb_serial *serial = port->serial;
2429 struct keyspan_serial_private *s_priv;
2430 struct keyspan_port_private *p_priv;
2431 const struct keyspan_device_details *d_details;
2432 struct callbacks *cback;
2433 int endp;
2434 int port_num;
2435 int i;
2437 s_priv = usb_get_serial_data(serial);
2438 d_details = s_priv->device_details;
2440 p_priv = kzalloc(sizeof(*p_priv), GFP_KERNEL);
2441 if (!p_priv)
2442 return -ENOMEM;
2444 for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i) {
2445 p_priv->in_buffer[i] = kzalloc(IN_BUFLEN, GFP_KERNEL);
2446 if (!p_priv->in_buffer[i])
2447 goto err_in_buffer;
2450 for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i) {
2451 p_priv->out_buffer[i] = kzalloc(OUT_BUFLEN, GFP_KERNEL);
2452 if (!p_priv->out_buffer[i])
2453 goto err_out_buffer;
2456 p_priv->inack_buffer = kzalloc(INACK_BUFLEN, GFP_KERNEL);
2457 if (!p_priv->inack_buffer)
2458 goto err_inack_buffer;
2460 p_priv->outcont_buffer = kzalloc(OUTCONT_BUFLEN, GFP_KERNEL);
2461 if (!p_priv->outcont_buffer)
2462 goto err_outcont_buffer;
2464 p_priv->device_details = d_details;
2466 /* Setup values for the various callback routines */
2467 cback = &keyspan_callbacks[d_details->msg_format];
2469 port_num = port->port_number;
2471 /* Do indat endpoints first, once for each flip */
2472 endp = d_details->indat_endpoints[port_num];
2473 for (i = 0; i <= d_details->indat_endp_flip; ++i, ++endp) {
2474 p_priv->in_urbs[i] = keyspan_setup_urb(serial, endp,
2475 USB_DIR_IN, port,
2476 p_priv->in_buffer[i],
2477 IN_BUFLEN,
2478 cback->indat_callback);
2480 /* outdat endpoints also have flip */
2481 endp = d_details->outdat_endpoints[port_num];
2482 for (i = 0; i <= d_details->outdat_endp_flip; ++i, ++endp) {
2483 p_priv->out_urbs[i] = keyspan_setup_urb(serial, endp,
2484 USB_DIR_OUT, port,
2485 p_priv->out_buffer[i],
2486 OUT_BUFLEN,
2487 cback->outdat_callback);
2489 /* inack endpoint */
2490 p_priv->inack_urb = keyspan_setup_urb(serial,
2491 d_details->inack_endpoints[port_num],
2492 USB_DIR_IN, port,
2493 p_priv->inack_buffer,
2494 INACK_BUFLEN,
2495 cback->inack_callback);
2496 /* outcont endpoint */
2497 p_priv->outcont_urb = keyspan_setup_urb(serial,
2498 d_details->outcont_endpoints[port_num],
2499 USB_DIR_OUT, port,
2500 p_priv->outcont_buffer,
2501 OUTCONT_BUFLEN,
2502 cback->outcont_callback);
2504 usb_set_serial_port_data(port, p_priv);
2506 return 0;
2508 err_outcont_buffer:
2509 kfree(p_priv->inack_buffer);
2510 err_inack_buffer:
2511 for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i)
2512 kfree(p_priv->out_buffer[i]);
2513 err_out_buffer:
2514 for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i)
2515 kfree(p_priv->in_buffer[i]);
2516 err_in_buffer:
2517 kfree(p_priv);
2519 return -ENOMEM;
2522 static int keyspan_port_remove(struct usb_serial_port *port)
2524 struct keyspan_port_private *p_priv;
2525 int i;
2527 p_priv = usb_get_serial_port_data(port);
2529 stop_urb(p_priv->inack_urb);
2530 stop_urb(p_priv->outcont_urb);
2531 for (i = 0; i < 2; i++) {
2532 stop_urb(p_priv->in_urbs[i]);
2533 stop_urb(p_priv->out_urbs[i]);
2536 usb_free_urb(p_priv->inack_urb);
2537 usb_free_urb(p_priv->outcont_urb);
2538 for (i = 0; i < 2; i++) {
2539 usb_free_urb(p_priv->in_urbs[i]);
2540 usb_free_urb(p_priv->out_urbs[i]);
2543 kfree(p_priv->outcont_buffer);
2544 kfree(p_priv->inack_buffer);
2545 for (i = 0; i < ARRAY_SIZE(p_priv->out_buffer); ++i)
2546 kfree(p_priv->out_buffer[i]);
2547 for (i = 0; i < ARRAY_SIZE(p_priv->in_buffer); ++i)
2548 kfree(p_priv->in_buffer[i]);
2550 kfree(p_priv);
2552 return 0;
2555 MODULE_AUTHOR(DRIVER_AUTHOR);
2556 MODULE_DESCRIPTION(DRIVER_DESC);
2557 MODULE_LICENSE("GPL");
2559 MODULE_FIRMWARE("keyspan/usa28.fw");
2560 MODULE_FIRMWARE("keyspan/usa28x.fw");
2561 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2562 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2563 MODULE_FIRMWARE("keyspan/usa19.fw");
2564 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2565 MODULE_FIRMWARE("keyspan/mpr.fw");
2566 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2567 MODULE_FIRMWARE("keyspan/usa18x.fw");
2568 MODULE_FIRMWARE("keyspan/usa19w.fw");
2569 MODULE_FIRMWARE("keyspan/usa49w.fw");
2570 MODULE_FIRMWARE("keyspan/usa49wlc.fw");