Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / usb / serial / keyspan.c
blobea7bba69f4da129397e111463a0ec13995d0ac5a
1 /*
2 Keyspan USB to Serial Converter driver
4 (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
5 (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 See http://misc.nu/hugh/keyspan.html for more information.
14 Code in this driver inspired by and in a number of places taken
15 from Brian Warner's original Keyspan-PDA driver.
17 This driver has been put together with the support of Innosys, Inc.
18 and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
19 Thanks Guys :)
21 Thanks to Paulus for miscellaneous tidy ups, some largish chunks
22 of much nicer and/or completely new code and (perhaps most uniquely)
23 having the patience to sit down and explain why and where he'd changed
24 stuff.
26 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
27 staff in their work on open source projects.
29 Change History
31 2003sep04 LPM (Keyspan) add support for new single port product USA19HS.
32 Improve setup message handling for all devices.
34 Wed Feb 19 22:00:00 PST 2003 (Jeffrey S. Laing <keyspan@jsl.com>)
35 Merged the current (1/31/03) Keyspan code with the current (2.4.21-pre4)
36 Linux source tree. The Linux tree lacked support for the 49WLC and
37 others. The Keyspan patches didn't work with the current kernel.
39 2003jan30 LPM add support for the 49WLC and MPR
41 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
42 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
43 now supported (including QI and QW). Modified port open, port
44 close, and send setup() logic to fix various data and endpoint
45 synchronization bugs and device LED status bugs. Changed keyspan_
46 write_room() to accurately return transmit buffer availability.
47 Changed forwardingLength from 1 to 16 for all adapters.
49 Fri Oct 12 16:45:00 EST 2001
50 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
52 Wed Apr 25 12:00:00 PST 2002 (Keyspan)
53 Started with Hugh Blemings' code dated Jan 17, 2002. All adapters
54 now supported (including QI and QW). Modified port open, port
55 close, and send setup() logic to fix various data and endpoint
56 synchronization bugs and device LED status bugs. Changed keyspan_
57 write_room() to accurately return transmit buffer availability.
58 Changed forwardingLength from 1 to 16 for all adapters.
60 Fri Oct 12 16:45:00 EST 2001
61 Preliminary USA-19QI and USA-28 support (both test OK for me, YMMV)
63 Mon Oct 8 14:29:00 EST 2001 hugh
64 Fixed bug that prevented mulitport devices operating correctly
65 if they weren't the first unit attached.
67 Sat Oct 6 12:31:21 EST 2001 hugh
68 Added support for USA-28XA and -28XB, misc cleanups, break support
69 for usa26 based models thanks to David Gibson.
71 Thu May 31 11:56:42 PDT 2001 gkh
72 switched from using spinlock to a semaphore
74 (04/08/2001) gb
75 Identify version on module load.
77 (11/01/2000) Adam J. Richter
78 usb_device_id table support.
80 Tue Oct 10 23:15:33 EST 2000 Hugh
81 Merged Paul's changes with my USA-49W mods. Work in progress
82 still...
84 Wed Jul 19 14:00:42 EST 2000 gkh
85 Added module_init and module_exit functions to handle the fact that
86 this driver is a loadable module now.
88 Tue Jul 18 16:14:52 EST 2000 Hugh
89 Basic character input/output for USA-19 now mostly works,
90 fixed at 9600 baud for the moment.
92 Sat Jul 8 11:11:48 EST 2000 Hugh
93 First public release - nothing works except the firmware upload.
94 Tested on PPC and x86 architectures, seems to behave...
98 #include <linux/kernel.h>
99 #include <linux/jiffies.h>
100 #include <linux/errno.h>
101 #include <linux/init.h>
102 #include <linux/slab.h>
103 #include <linux/tty.h>
104 #include <linux/tty_driver.h>
105 #include <linux/tty_flip.h>
106 #include <linux/module.h>
107 #include <linux/spinlock.h>
108 #include <asm/uaccess.h>
109 #include <linux/usb.h>
110 #include <linux/usb/serial.h>
111 #include "keyspan.h"
113 static int debug;
116 * Version Information
118 #define DRIVER_VERSION "v1.1.5"
119 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
120 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
122 #define INSTAT_BUFLEN 32
123 #define GLOCONT_BUFLEN 64
124 #define INDAT49W_BUFLEN 512
126 /* Per device and per port private data */
127 struct keyspan_serial_private {
128 const struct keyspan_device_details *device_details;
130 struct urb *instat_urb;
131 char instat_buf[INSTAT_BUFLEN];
133 /* added to support 49wg, where data from all 4 ports comes in on 1 EP */
134 /* and high-speed supported */
135 struct urb *indat_urb;
136 char indat_buf[INDAT49W_BUFLEN];
138 /* XXX this one probably will need a lock */
139 struct urb *glocont_urb;
140 char glocont_buf[GLOCONT_BUFLEN];
141 char ctrl_buf[8]; // for EP0 control message
144 struct keyspan_port_private {
145 /* Keep track of which input & output endpoints to use */
146 int in_flip;
147 int out_flip;
149 /* Keep duplicate of device details in each port
150 structure as well - simplifies some of the
151 callback functions etc. */
152 const struct keyspan_device_details *device_details;
154 /* Input endpoints and buffer for this port */
155 struct urb *in_urbs[2];
156 char in_buffer[2][64];
157 /* Output endpoints and buffer for this port */
158 struct urb *out_urbs[2];
159 char out_buffer[2][64];
161 /* Input ack endpoint */
162 struct urb *inack_urb;
163 char inack_buffer[1];
165 /* Output control endpoint */
166 struct urb *outcont_urb;
167 char outcont_buffer[64];
169 /* Settings for the port */
170 int baud;
171 int old_baud;
172 unsigned int cflag;
173 unsigned int old_cflag;
174 enum {flow_none, flow_cts, flow_xon} flow_control;
175 int rts_state; /* Handshaking pins (outputs) */
176 int dtr_state;
177 int cts_state; /* Handshaking pins (inputs) */
178 int dsr_state;
179 int dcd_state;
180 int ri_state;
181 int break_on;
183 unsigned long tx_start_time[2];
184 int resend_cont; /* need to resend control packet */
188 /* Include Keyspan message headers. All current Keyspan Adapters
189 make use of one of five message formats which are referred
190 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and within this driver. */
191 #include "keyspan_usa26msg.h"
192 #include "keyspan_usa28msg.h"
193 #include "keyspan_usa49msg.h"
194 #include "keyspan_usa90msg.h"
195 #include "keyspan_usa67msg.h"
198 /* Functions used by new usb-serial code. */
199 static int __init keyspan_init (void)
201 int retval;
202 retval = usb_serial_register(&keyspan_pre_device);
203 if (retval)
204 goto failed_pre_device_register;
205 retval = usb_serial_register(&keyspan_1port_device);
206 if (retval)
207 goto failed_1port_device_register;
208 retval = usb_serial_register(&keyspan_2port_device);
209 if (retval)
210 goto failed_2port_device_register;
211 retval = usb_serial_register(&keyspan_4port_device);
212 if (retval)
213 goto failed_4port_device_register;
214 retval = usb_register(&keyspan_driver);
215 if (retval)
216 goto failed_usb_register;
218 info(DRIVER_VERSION ":" DRIVER_DESC);
220 return 0;
221 failed_usb_register:
222 usb_serial_deregister(&keyspan_4port_device);
223 failed_4port_device_register:
224 usb_serial_deregister(&keyspan_2port_device);
225 failed_2port_device_register:
226 usb_serial_deregister(&keyspan_1port_device);
227 failed_1port_device_register:
228 usb_serial_deregister(&keyspan_pre_device);
229 failed_pre_device_register:
230 return retval;
233 static void __exit keyspan_exit (void)
235 usb_deregister (&keyspan_driver);
236 usb_serial_deregister (&keyspan_pre_device);
237 usb_serial_deregister (&keyspan_1port_device);
238 usb_serial_deregister (&keyspan_2port_device);
239 usb_serial_deregister (&keyspan_4port_device);
242 module_init(keyspan_init);
243 module_exit(keyspan_exit);
245 static void keyspan_rx_throttle (struct usb_serial_port *port)
247 dbg("%s - port %d", __FUNCTION__, port->number);
251 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
253 dbg("%s - port %d", __FUNCTION__, port->number);
257 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
259 struct keyspan_port_private *p_priv;
261 dbg("%s", __FUNCTION__);
263 p_priv = usb_get_serial_port_data(port);
265 if (break_state == -1)
266 p_priv->break_on = 1;
267 else
268 p_priv->break_on = 0;
270 keyspan_send_setup(port, 0);
274 static void keyspan_set_termios (struct usb_serial_port *port,
275 struct ktermios *old_termios)
277 int baud_rate, device_port;
278 struct keyspan_port_private *p_priv;
279 const struct keyspan_device_details *d_details;
280 unsigned int cflag;
281 struct tty_struct *tty = port->tty;
283 dbg("%s", __FUNCTION__);
285 p_priv = usb_get_serial_port_data(port);
286 d_details = p_priv->device_details;
287 cflag = tty->termios->c_cflag;
288 device_port = port->number - port->serial->minor;
290 /* Baud rate calculation takes baud rate as an integer
291 so other rates can be generated if desired. */
292 baud_rate = tty_get_baud_rate(tty);
293 /* If no match or invalid, don't change */
294 if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
295 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
296 /* FIXME - more to do here to ensure rate changes cleanly */
297 /* FIXME - calcuate exact rate from divisor ? */
298 p_priv->baud = baud_rate;
299 } else
300 baud_rate = tty_termios_baud_rate(old_termios);
302 tty_encode_baud_rate(tty, baud_rate, baud_rate);
303 /* set CTS/RTS handshake etc. */
304 p_priv->cflag = cflag;
305 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
307 /* Mark/Space not supported */
308 tty->termios->c_cflag &= ~CMSPAR;
310 keyspan_send_setup(port, 0);
313 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
315 unsigned int value;
316 struct keyspan_port_private *p_priv;
318 p_priv = usb_get_serial_port_data(port);
320 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
321 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
322 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
323 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
324 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
325 ((p_priv->ri_state) ? TIOCM_RNG : 0);
327 return value;
330 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
331 unsigned int set, unsigned int clear)
333 struct keyspan_port_private *p_priv;
335 p_priv = usb_get_serial_port_data(port);
337 if (set & TIOCM_RTS)
338 p_priv->rts_state = 1;
339 if (set & TIOCM_DTR)
340 p_priv->dtr_state = 1;
342 if (clear & TIOCM_RTS)
343 p_priv->rts_state = 0;
344 if (clear & TIOCM_DTR)
345 p_priv->dtr_state = 0;
346 keyspan_send_setup(port, 0);
347 return 0;
350 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
351 unsigned int cmd, unsigned long arg)
353 return -ENOIOCTLCMD;
356 /* Write function is similar for the four protocols used
357 with only a minor change for usa90 (usa19hs) required */
358 static int keyspan_write(struct usb_serial_port *port,
359 const unsigned char *buf, int count)
361 struct keyspan_port_private *p_priv;
362 const struct keyspan_device_details *d_details;
363 int flip;
364 int left, todo;
365 struct urb *this_urb;
366 int err, maxDataLen, dataOffset;
368 p_priv = usb_get_serial_port_data(port);
369 d_details = p_priv->device_details;
371 if (d_details->msg_format == msg_usa90) {
372 maxDataLen = 64;
373 dataOffset = 0;
374 } else {
375 maxDataLen = 63;
376 dataOffset = 1;
379 dbg("%s - for port %d (%d chars), flip=%d",
380 __FUNCTION__, port->number, count, p_priv->out_flip);
382 for (left = count; left > 0; left -= todo) {
383 todo = left;
384 if (todo > maxDataLen)
385 todo = maxDataLen;
387 flip = p_priv->out_flip;
389 /* Check we have a valid urb/endpoint before we use it... */
390 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
391 /* no bulk out, so return 0 bytes written */
392 dbg("%s - no output urb :(", __FUNCTION__);
393 return count;
396 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
398 if (this_urb->status == -EINPROGRESS) {
399 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
400 break;
401 usb_unlink_urb(this_urb);
402 break;
405 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
406 for now so set to zero */
407 ((char *)this_urb->transfer_buffer)[0] = 0;
409 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
410 buf += todo;
412 /* send the data out the bulk port */
413 this_urb->transfer_buffer_length = todo + dataOffset;
415 this_urb->dev = port->serial->dev;
416 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
417 dbg("usb_submit_urb(write bulk) failed (%d)", err);
419 p_priv->tx_start_time[flip] = jiffies;
421 /* Flip for next time if usa26 or usa28 interface
422 (not used on usa49) */
423 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
426 return count - left;
429 static void usa26_indat_callback(struct urb *urb)
431 int i, err;
432 int endpoint;
433 struct usb_serial_port *port;
434 struct tty_struct *tty;
435 unsigned char *data = urb->transfer_buffer;
436 int status = urb->status;
438 dbg ("%s", __FUNCTION__);
440 endpoint = usb_pipeendpoint(urb->pipe);
442 if (status) {
443 dbg("%s - nonzero status: %x on endpoint %d.",
444 __FUNCTION__, status, endpoint);
445 return;
448 port = (struct usb_serial_port *) urb->context;
449 tty = port->tty;
450 if (tty && urb->actual_length) {
451 /* 0x80 bit is error flag */
452 if ((data[0] & 0x80) == 0) {
453 /* no errors on individual bytes, only possible overrun err*/
454 if (data[0] & RXERROR_OVERRUN)
455 err = TTY_OVERRUN;
456 else err = 0;
457 for (i = 1; i < urb->actual_length ; ++i) {
458 tty_insert_flip_char(tty, data[i], err);
460 } else {
461 /* some bytes had errors, every byte has status */
462 dbg("%s - RX error!!!!", __FUNCTION__);
463 for (i = 0; i + 1 < urb->actual_length; i += 2) {
464 int stat = data[i], flag = 0;
465 if (stat & RXERROR_OVERRUN)
466 flag |= TTY_OVERRUN;
467 if (stat & RXERROR_FRAMING)
468 flag |= TTY_FRAME;
469 if (stat & RXERROR_PARITY)
470 flag |= TTY_PARITY;
471 /* XXX should handle break (0x10) */
472 tty_insert_flip_char(tty, data[i+1], flag);
475 tty_flip_buffer_push(tty);
478 /* Resubmit urb so we continue receiving */
479 urb->dev = port->serial->dev;
480 if (port->open_count)
481 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
482 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
484 return;
487 /* Outdat handling is common for all devices */
488 static void usa2x_outdat_callback(struct urb *urb)
490 struct usb_serial_port *port;
491 struct keyspan_port_private *p_priv;
493 port = (struct usb_serial_port *) urb->context;
494 p_priv = usb_get_serial_port_data(port);
495 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
497 if (port->open_count)
498 usb_serial_port_softint(port);
501 static void usa26_inack_callback(struct urb *urb)
503 dbg ("%s", __FUNCTION__);
507 static void usa26_outcont_callback(struct urb *urb)
509 struct usb_serial_port *port;
510 struct keyspan_port_private *p_priv;
512 port = (struct usb_serial_port *) urb->context;
513 p_priv = usb_get_serial_port_data(port);
515 if (p_priv->resend_cont) {
516 dbg ("%s - sending setup", __FUNCTION__);
517 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
521 static void usa26_instat_callback(struct urb *urb)
523 unsigned char *data = urb->transfer_buffer;
524 struct keyspan_usa26_portStatusMessage *msg;
525 struct usb_serial *serial;
526 struct usb_serial_port *port;
527 struct keyspan_port_private *p_priv;
528 int old_dcd_state, err;
529 int status = urb->status;
531 serial = (struct usb_serial *) urb->context;
533 if (status) {
534 dbg("%s - nonzero status: %x", __FUNCTION__, status);
535 return;
537 if (urb->actual_length != 9) {
538 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
539 goto exit;
542 msg = (struct keyspan_usa26_portStatusMessage *)data;
544 #if 0
545 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
546 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
547 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
548 #endif
550 /* Now do something useful with the data */
553 /* Check port number from message and retrieve private data */
554 if (msg->port >= serial->num_ports) {
555 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
556 goto exit;
558 port = serial->port[msg->port];
559 p_priv = usb_get_serial_port_data(port);
561 /* Update handshaking pin state information */
562 old_dcd_state = p_priv->dcd_state;
563 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
564 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
565 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
566 p_priv->ri_state = ((msg->ri) ? 1 : 0);
568 if (port->tty && !C_CLOCAL(port->tty)
569 && old_dcd_state != p_priv->dcd_state) {
570 if (old_dcd_state)
571 tty_hangup(port->tty);
572 /* else */
573 /* wake_up_interruptible(&p_priv->open_wait); */
576 /* Resubmit urb so we continue receiving */
577 urb->dev = serial->dev;
578 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
579 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
581 exit: ;
584 static void usa26_glocont_callback(struct urb *urb)
586 dbg ("%s", __FUNCTION__);
591 static void usa28_indat_callback(struct urb *urb)
593 int i, err;
594 struct usb_serial_port *port;
595 struct tty_struct *tty;
596 unsigned char *data;
597 struct keyspan_port_private *p_priv;
598 int status = urb->status;
600 dbg ("%s", __FUNCTION__);
602 port = (struct usb_serial_port *) urb->context;
603 p_priv = usb_get_serial_port_data(port);
604 data = urb->transfer_buffer;
606 if (urb != p_priv->in_urbs[p_priv->in_flip])
607 return;
609 do {
610 if (status) {
611 dbg("%s - nonzero status: %x on endpoint %d.",
612 __FUNCTION__, status, usb_pipeendpoint(urb->pipe));
613 return;
616 port = (struct usb_serial_port *) urb->context;
617 p_priv = usb_get_serial_port_data(port);
618 data = urb->transfer_buffer;
620 tty = port->tty;
621 if (urb->actual_length) {
622 for (i = 0; i < urb->actual_length ; ++i) {
623 tty_insert_flip_char(tty, data[i], 0);
625 tty_flip_buffer_push(tty);
628 /* Resubmit urb so we continue receiving */
629 urb->dev = port->serial->dev;
630 if (port->open_count)
631 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
632 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
634 p_priv->in_flip ^= 1;
636 urb = p_priv->in_urbs[p_priv->in_flip];
637 } while (urb->status != -EINPROGRESS);
640 static void usa28_inack_callback(struct urb *urb)
642 dbg ("%s", __FUNCTION__);
645 static void usa28_outcont_callback(struct urb *urb)
647 struct usb_serial_port *port;
648 struct keyspan_port_private *p_priv;
650 port = (struct usb_serial_port *) urb->context;
651 p_priv = usb_get_serial_port_data(port);
653 if (p_priv->resend_cont) {
654 dbg ("%s - sending setup", __FUNCTION__);
655 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
659 static void usa28_instat_callback(struct urb *urb)
661 int err;
662 unsigned char *data = urb->transfer_buffer;
663 struct keyspan_usa28_portStatusMessage *msg;
664 struct usb_serial *serial;
665 struct usb_serial_port *port;
666 struct keyspan_port_private *p_priv;
667 int old_dcd_state;
668 int status = urb->status;
670 serial = (struct usb_serial *) urb->context;
672 if (status) {
673 dbg("%s - nonzero status: %x", __FUNCTION__, status);
674 return;
677 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
678 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
679 goto exit;
682 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
683 data[0], data[1], data[2], data[3], data[4], data[5],
684 data[6], data[7], data[8], data[9], data[10], data[11]);*/
686 /* Now do something useful with the data */
687 msg = (struct keyspan_usa28_portStatusMessage *)data;
690 /* Check port number from message and retrieve private data */
691 if (msg->port >= serial->num_ports) {
692 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
693 goto exit;
695 port = serial->port[msg->port];
696 p_priv = usb_get_serial_port_data(port);
698 /* Update handshaking pin state information */
699 old_dcd_state = p_priv->dcd_state;
700 p_priv->cts_state = ((msg->cts) ? 1 : 0);
701 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
702 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
703 p_priv->ri_state = ((msg->ri) ? 1 : 0);
705 if (port->tty && !C_CLOCAL(port->tty)
706 && old_dcd_state != p_priv->dcd_state) {
707 if (old_dcd_state)
708 tty_hangup(port->tty);
709 /* else */
710 /* wake_up_interruptible(&p_priv->open_wait); */
713 /* Resubmit urb so we continue receiving */
714 urb->dev = serial->dev;
715 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
716 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
718 exit: ;
721 static void usa28_glocont_callback(struct urb *urb)
723 dbg ("%s", __FUNCTION__);
727 static void usa49_glocont_callback(struct urb *urb)
729 struct usb_serial *serial;
730 struct usb_serial_port *port;
731 struct keyspan_port_private *p_priv;
732 int i;
734 dbg ("%s", __FUNCTION__);
736 serial = (struct usb_serial *) urb->context;
737 for (i = 0; i < serial->num_ports; ++i) {
738 port = serial->port[i];
739 p_priv = usb_get_serial_port_data(port);
741 if (p_priv->resend_cont) {
742 dbg ("%s - sending setup", __FUNCTION__);
743 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
744 break;
749 /* This is actually called glostat in the Keyspan
750 doco */
751 static void usa49_instat_callback(struct urb *urb)
753 int err;
754 unsigned char *data = urb->transfer_buffer;
755 struct keyspan_usa49_portStatusMessage *msg;
756 struct usb_serial *serial;
757 struct usb_serial_port *port;
758 struct keyspan_port_private *p_priv;
759 int old_dcd_state;
760 int status = urb->status;
762 dbg ("%s", __FUNCTION__);
764 serial = (struct usb_serial *) urb->context;
766 if (status) {
767 dbg("%s - nonzero status: %x", __FUNCTION__, status);
768 return;
771 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
772 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
773 goto exit;
776 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
777 data[0], data[1], data[2], data[3], data[4], data[5],
778 data[6], data[7], data[8], data[9], data[10]);*/
780 /* Now do something useful with the data */
781 msg = (struct keyspan_usa49_portStatusMessage *)data;
783 /* Check port number from message and retrieve private data */
784 if (msg->portNumber >= serial->num_ports) {
785 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
786 goto exit;
788 port = serial->port[msg->portNumber];
789 p_priv = usb_get_serial_port_data(port);
791 /* Update handshaking pin state information */
792 old_dcd_state = p_priv->dcd_state;
793 p_priv->cts_state = ((msg->cts) ? 1 : 0);
794 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
795 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
796 p_priv->ri_state = ((msg->ri) ? 1 : 0);
798 if (port->tty && !C_CLOCAL(port->tty)
799 && old_dcd_state != p_priv->dcd_state) {
800 if (old_dcd_state)
801 tty_hangup(port->tty);
802 /* else */
803 /* wake_up_interruptible(&p_priv->open_wait); */
806 /* Resubmit urb so we continue receiving */
807 urb->dev = serial->dev;
809 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
810 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
812 exit: ;
815 static void usa49_inack_callback(struct urb *urb)
817 dbg ("%s", __FUNCTION__);
820 static void usa49_indat_callback(struct urb *urb)
822 int i, err;
823 int endpoint;
824 struct usb_serial_port *port;
825 struct tty_struct *tty;
826 unsigned char *data = urb->transfer_buffer;
827 int status = urb->status;
829 dbg ("%s", __FUNCTION__);
831 endpoint = usb_pipeendpoint(urb->pipe);
833 if (status) {
834 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
835 status, endpoint);
836 return;
839 port = (struct usb_serial_port *) urb->context;
840 tty = port->tty;
841 if (tty && urb->actual_length) {
842 /* 0x80 bit is error flag */
843 if ((data[0] & 0x80) == 0) {
844 /* no error on any byte */
845 for (i = 1; i < urb->actual_length ; ++i) {
846 tty_insert_flip_char(tty, data[i], 0);
848 } else {
849 /* some bytes had errors, every byte has status */
850 for (i = 0; i + 1 < urb->actual_length; i += 2) {
851 int stat = data[i], flag = 0;
852 if (stat & RXERROR_OVERRUN)
853 flag |= TTY_OVERRUN;
854 if (stat & RXERROR_FRAMING)
855 flag |= TTY_FRAME;
856 if (stat & RXERROR_PARITY)
857 flag |= TTY_PARITY;
858 /* XXX should handle break (0x10) */
859 tty_insert_flip_char(tty, data[i+1], flag);
862 tty_flip_buffer_push(tty);
865 /* Resubmit urb so we continue receiving */
866 urb->dev = port->serial->dev;
867 if (port->open_count)
868 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
869 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
873 static void usa49wg_indat_callback(struct urb *urb)
875 int i, len, x, err;
876 struct usb_serial *serial;
877 struct usb_serial_port *port;
878 struct tty_struct *tty;
879 unsigned char *data = urb->transfer_buffer;
880 int status = urb->status;
882 dbg ("%s", __FUNCTION__);
884 serial = urb->context;
886 if (status) {
887 dbg("%s - nonzero status: %x", __FUNCTION__, status);
888 return;
891 /* inbound data is in the form P#, len, status, data */
892 i = 0;
893 len = 0;
895 if (urb->actual_length) {
896 while (i < urb->actual_length) {
898 /* Check port number from message*/
899 if (data[i] >= serial->num_ports) {
900 dbg ("%s - Unexpected port number %d",
901 __FUNCTION__, data[i]);
902 return;
904 port = serial->port[data[i++]];
905 tty = port->tty;
906 len = data[i++];
908 /* 0x80 bit is error flag */
909 if ((data[i] & 0x80) == 0) {
910 /* no error on any byte */
911 i++;
912 for (x = 1; x < len ; ++x)
913 if (port->open_count)
914 tty_insert_flip_char(tty,
915 data[i++], 0);
916 else
917 i++;
918 } else {
920 * some bytes had errors, every byte has status
922 for (x = 0; x + 1 < len; x += 2) {
923 int stat = data[i], flag = 0;
924 if (stat & RXERROR_OVERRUN)
925 flag |= TTY_OVERRUN;
926 if (stat & RXERROR_FRAMING)
927 flag |= TTY_FRAME;
928 if (stat & RXERROR_PARITY)
929 flag |= TTY_PARITY;
930 /* XXX should handle break (0x10) */
931 if (port->open_count)
932 tty_insert_flip_char(tty,
933 data[i+1], flag);
934 i += 2;
937 if (port->open_count)
938 tty_flip_buffer_push(tty);
942 /* Resubmit urb so we continue receiving */
943 urb->dev = serial->dev;
945 err = usb_submit_urb(urb, GFP_ATOMIC);
946 if (err != 0)
947 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
950 /* not used, usa-49 doesn't have per-port control endpoints */
951 static void usa49_outcont_callback(struct urb *urb)
953 dbg ("%s", __FUNCTION__);
956 static void usa90_indat_callback(struct urb *urb)
958 int i, err;
959 int endpoint;
960 struct usb_serial_port *port;
961 struct keyspan_port_private *p_priv;
962 struct tty_struct *tty;
963 unsigned char *data = urb->transfer_buffer;
964 int status = urb->status;
966 dbg ("%s", __FUNCTION__);
968 endpoint = usb_pipeendpoint(urb->pipe);
970 if (status) {
971 dbg("%s - nonzero status: %x on endpoint %d.",
972 __FUNCTION__, status, endpoint);
973 return;
976 port = (struct usb_serial_port *) urb->context;
977 p_priv = usb_get_serial_port_data(port);
979 tty = port->tty;
980 if (urb->actual_length) {
982 /* if current mode is DMA, looks like usa28 format
983 otherwise looks like usa26 data format */
985 if (p_priv->baud > 57600) {
986 for (i = 0; i < urb->actual_length ; ++i)
987 tty_insert_flip_char(tty, data[i], 0);
989 else {
991 /* 0x80 bit is error flag */
992 if ((data[0] & 0x80) == 0) {
993 /* no errors on individual bytes, only possible overrun err*/
994 if (data[0] & RXERROR_OVERRUN)
995 err = TTY_OVERRUN;
996 else err = 0;
997 for (i = 1; i < urb->actual_length ; ++i)
998 tty_insert_flip_char(tty, data[i], err);
1001 else {
1002 /* some bytes had errors, every byte has status */
1003 dbg("%s - RX error!!!!", __FUNCTION__);
1004 for (i = 0; i + 1 < urb->actual_length; i += 2) {
1005 int stat = data[i], flag = 0;
1006 if (stat & RXERROR_OVERRUN)
1007 flag |= TTY_OVERRUN;
1008 if (stat & RXERROR_FRAMING)
1009 flag |= TTY_FRAME;
1010 if (stat & RXERROR_PARITY)
1011 flag |= TTY_PARITY;
1012 /* XXX should handle break (0x10) */
1013 tty_insert_flip_char(tty, data[i+1], flag);
1017 tty_flip_buffer_push(tty);
1020 /* Resubmit urb so we continue receiving */
1021 urb->dev = port->serial->dev;
1022 if (port->open_count)
1023 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1024 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1026 return;
1030 static void usa90_instat_callback(struct urb *urb)
1032 unsigned char *data = urb->transfer_buffer;
1033 struct keyspan_usa90_portStatusMessage *msg;
1034 struct usb_serial *serial;
1035 struct usb_serial_port *port;
1036 struct keyspan_port_private *p_priv;
1037 int old_dcd_state, err;
1038 int status = urb->status;
1040 serial = (struct usb_serial *) urb->context;
1042 if (status) {
1043 dbg("%s - nonzero status: %x", __FUNCTION__, status);
1044 return;
1046 if (urb->actual_length < 14) {
1047 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
1048 goto exit;
1051 msg = (struct keyspan_usa90_portStatusMessage *)data;
1053 /* Now do something useful with the data */
1055 port = serial->port[0];
1056 p_priv = usb_get_serial_port_data(port);
1058 /* Update handshaking pin state information */
1059 old_dcd_state = p_priv->dcd_state;
1060 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1061 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1062 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1063 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1065 if (port->tty && !C_CLOCAL(port->tty)
1066 && old_dcd_state != p_priv->dcd_state) {
1067 if (old_dcd_state)
1068 tty_hangup(port->tty);
1069 /* else */
1070 /* wake_up_interruptible(&p_priv->open_wait); */
1073 /* Resubmit urb so we continue receiving */
1074 urb->dev = serial->dev;
1075 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1076 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1078 exit:
1082 static void usa90_outcont_callback(struct urb *urb)
1084 struct usb_serial_port *port;
1085 struct keyspan_port_private *p_priv;
1087 port = (struct usb_serial_port *) urb->context;
1088 p_priv = usb_get_serial_port_data(port);
1090 if (p_priv->resend_cont) {
1091 dbg ("%s - sending setup", __FUNCTION__);
1092 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1096 /* Status messages from the 28xg */
1097 static void usa67_instat_callback(struct urb *urb)
1099 int err;
1100 unsigned char *data = urb->transfer_buffer;
1101 struct keyspan_usa67_portStatusMessage *msg;
1102 struct usb_serial *serial;
1103 struct usb_serial_port *port;
1104 struct keyspan_port_private *p_priv;
1105 int old_dcd_state;
1106 int status = urb->status;
1108 dbg ("%s", __FUNCTION__);
1110 serial = urb->context;
1112 if (status) {
1113 dbg("%s - nonzero status: %x", __FUNCTION__, status);
1114 return;
1117 if (urb->actual_length != sizeof(struct keyspan_usa67_portStatusMessage)) {
1118 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
1119 return;
1123 /* Now do something useful with the data */
1124 msg = (struct keyspan_usa67_portStatusMessage *)data;
1126 /* Check port number from message and retrieve private data */
1127 if (msg->port >= serial->num_ports) {
1128 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
1129 return;
1132 port = serial->port[msg->port];
1133 p_priv = usb_get_serial_port_data(port);
1135 /* Update handshaking pin state information */
1136 old_dcd_state = p_priv->dcd_state;
1137 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1138 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1140 if (port->tty && !C_CLOCAL(port->tty)
1141 && old_dcd_state != p_priv->dcd_state) {
1142 if (old_dcd_state)
1143 tty_hangup(port->tty);
1144 /* else */
1145 /* wake_up_interruptible(&p_priv->open_wait); */
1148 /* Resubmit urb so we continue receiving */
1149 urb->dev = serial->dev;
1150 err = usb_submit_urb(urb, GFP_ATOMIC);
1151 if (err != 0)
1152 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1155 static void usa67_glocont_callback(struct urb *urb)
1157 struct usb_serial *serial;
1158 struct usb_serial_port *port;
1159 struct keyspan_port_private *p_priv;
1160 int i;
1162 dbg ("%s", __FUNCTION__);
1164 serial = urb->context;
1165 for (i = 0; i < serial->num_ports; ++i) {
1166 port = serial->port[i];
1167 p_priv = usb_get_serial_port_data(port);
1169 if (p_priv->resend_cont) {
1170 dbg ("%s - sending setup", __FUNCTION__);
1171 keyspan_usa67_send_setup(serial, port,
1172 p_priv->resend_cont - 1);
1173 break;
1178 static int keyspan_write_room (struct usb_serial_port *port)
1180 struct keyspan_port_private *p_priv;
1181 const struct keyspan_device_details *d_details;
1182 int flip;
1183 int data_len;
1184 struct urb *this_urb;
1186 dbg("%s", __FUNCTION__);
1187 p_priv = usb_get_serial_port_data(port);
1188 d_details = p_priv->device_details;
1190 if (d_details->msg_format == msg_usa90)
1191 data_len = 64;
1192 else
1193 data_len = 63;
1195 flip = p_priv->out_flip;
1197 /* Check both endpoints to see if any are available. */
1198 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1199 if (this_urb->status != -EINPROGRESS)
1200 return (data_len);
1201 flip = (flip + 1) & d_details->outdat_endp_flip;
1202 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1203 if (this_urb->status != -EINPROGRESS)
1204 return (data_len);
1206 return (0);
1210 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1212 return (0);
1216 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1218 struct keyspan_port_private *p_priv;
1219 struct keyspan_serial_private *s_priv;
1220 struct usb_serial *serial = port->serial;
1221 const struct keyspan_device_details *d_details;
1222 int i, err;
1223 int baud_rate, device_port;
1224 struct urb *urb;
1225 unsigned int cflag;
1227 s_priv = usb_get_serial_data(serial);
1228 p_priv = usb_get_serial_port_data(port);
1229 d_details = p_priv->device_details;
1231 dbg("%s - port%d.", __FUNCTION__, port->number);
1233 /* Set some sane defaults */
1234 p_priv->rts_state = 1;
1235 p_priv->dtr_state = 1;
1236 p_priv->baud = 9600;
1238 /* force baud and lcr to be set on open */
1239 p_priv->old_baud = 0;
1240 p_priv->old_cflag = 0;
1242 p_priv->out_flip = 0;
1243 p_priv->in_flip = 0;
1245 /* Reset low level data toggle and start reading from endpoints */
1246 for (i = 0; i < 2; i++) {
1247 if ((urb = p_priv->in_urbs[i]) == NULL)
1248 continue;
1249 urb->dev = serial->dev;
1251 /* make sure endpoint data toggle is synchronized with the device */
1253 usb_clear_halt(urb->dev, urb->pipe);
1255 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1256 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1260 /* Reset low level data toggle on out endpoints */
1261 for (i = 0; i < 2; i++) {
1262 if ((urb = p_priv->out_urbs[i]) == NULL)
1263 continue;
1264 urb->dev = serial->dev;
1265 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1268 /* get the terminal config for the setup message now so we don't
1269 * need to send 2 of them */
1271 cflag = port->tty->termios->c_cflag;
1272 device_port = port->number - port->serial->minor;
1274 /* Baud rate calculation takes baud rate as an integer
1275 so other rates can be generated if desired. */
1276 baud_rate = tty_get_baud_rate(port->tty);
1277 /* If no match or invalid, leave as default */
1278 if (baud_rate >= 0
1279 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1280 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1281 p_priv->baud = baud_rate;
1284 /* set CTS/RTS handshake etc. */
1285 p_priv->cflag = cflag;
1286 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1288 keyspan_send_setup(port, 1);
1289 //mdelay(100);
1290 //keyspan_set_termios(port, NULL);
1292 return (0);
1295 static inline void stop_urb(struct urb *urb)
1297 if (urb && urb->status == -EINPROGRESS)
1298 usb_kill_urb(urb);
1301 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1303 int i;
1304 struct usb_serial *serial = port->serial;
1305 struct keyspan_serial_private *s_priv;
1306 struct keyspan_port_private *p_priv;
1308 dbg("%s", __FUNCTION__);
1309 s_priv = usb_get_serial_data(serial);
1310 p_priv = usb_get_serial_port_data(port);
1312 p_priv->rts_state = 0;
1313 p_priv->dtr_state = 0;
1315 if (serial->dev) {
1316 keyspan_send_setup(port, 2);
1317 /* pilot-xfer seems to work best with this delay */
1318 mdelay(100);
1319 // keyspan_set_termios(port, NULL);
1322 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1323 dbg("%s - urb in progress", __FUNCTION__);
1326 p_priv->out_flip = 0;
1327 p_priv->in_flip = 0;
1329 if (serial->dev) {
1330 /* Stop reading/writing urbs */
1331 stop_urb(p_priv->inack_urb);
1332 /* stop_urb(p_priv->outcont_urb); */
1333 for (i = 0; i < 2; i++) {
1334 stop_urb(p_priv->in_urbs[i]);
1335 stop_urb(p_priv->out_urbs[i]);
1338 port->tty = NULL;
1342 /* download the firmware to a pre-renumeration device */
1343 static int keyspan_fake_startup (struct usb_serial *serial)
1345 int response;
1346 const struct ezusb_hex_record *record;
1347 char *fw_name;
1349 dbg("Keyspan startup version %04x product %04x",
1350 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1351 le16_to_cpu(serial->dev->descriptor.idProduct));
1353 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1354 dbg("Firmware already loaded. Quitting.");
1355 return(1);
1358 /* Select firmware image on the basis of idProduct */
1359 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1360 case keyspan_usa28_pre_product_id:
1361 record = &keyspan_usa28_firmware[0];
1362 fw_name = "USA28";
1363 break;
1365 case keyspan_usa28x_pre_product_id:
1366 record = &keyspan_usa28x_firmware[0];
1367 fw_name = "USA28X";
1368 break;
1370 case keyspan_usa28xa_pre_product_id:
1371 record = &keyspan_usa28xa_firmware[0];
1372 fw_name = "USA28XA";
1373 break;
1375 case keyspan_usa28xb_pre_product_id:
1376 record = &keyspan_usa28xb_firmware[0];
1377 fw_name = "USA28XB";
1378 break;
1380 case keyspan_usa19_pre_product_id:
1381 record = &keyspan_usa19_firmware[0];
1382 fw_name = "USA19";
1383 break;
1385 case keyspan_usa19qi_pre_product_id:
1386 record = &keyspan_usa19qi_firmware[0];
1387 fw_name = "USA19QI";
1388 break;
1390 case keyspan_mpr_pre_product_id:
1391 record = &keyspan_mpr_firmware[0];
1392 fw_name = "MPR";
1393 break;
1395 case keyspan_usa19qw_pre_product_id:
1396 record = &keyspan_usa19qw_firmware[0];
1397 fw_name = "USA19QI";
1398 break;
1400 case keyspan_usa18x_pre_product_id:
1401 record = &keyspan_usa18x_firmware[0];
1402 fw_name = "USA18X";
1403 break;
1405 case keyspan_usa19w_pre_product_id:
1406 record = &keyspan_usa19w_firmware[0];
1407 fw_name = "USA19W";
1408 break;
1410 case keyspan_usa49w_pre_product_id:
1411 record = &keyspan_usa49w_firmware[0];
1412 fw_name = "USA49W";
1413 break;
1415 case keyspan_usa49wlc_pre_product_id:
1416 record = &keyspan_usa49wlc_firmware[0];
1417 fw_name = "USA49WLC";
1418 break;
1420 default:
1421 record = NULL;
1422 fw_name = "Unknown";
1423 break;
1426 if (record == NULL) {
1427 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1428 return(1);
1431 dbg("Uploading Keyspan %s firmware.", fw_name);
1433 /* download the firmware image */
1434 response = ezusb_set_reset(serial, 1);
1436 while(record->address != 0xffff) {
1437 response = ezusb_writememory(serial, record->address,
1438 (unsigned char *)record->data,
1439 record->data_size, 0xa0);
1440 if (response < 0) {
1441 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1442 "firmware (%d %04X %p %d)\n",
1443 response,
1444 record->address, record->data, record->data_size);
1445 break;
1447 record++;
1449 /* bring device out of reset. Renumeration will occur in a
1450 moment and the new device will bind to the real driver */
1451 response = ezusb_set_reset(serial, 0);
1453 /* we don't want this device to have a driver assigned to it. */
1454 return (1);
1457 /* Helper functions used by keyspan_setup_urbs */
1458 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1459 int endpoint)
1461 struct usb_host_interface *iface_desc;
1462 struct usb_endpoint_descriptor *ep;
1463 int i;
1465 iface_desc = serial->interface->cur_altsetting;
1466 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1467 ep = &iface_desc->endpoint[i].desc;
1468 if (ep->bEndpointAddress == endpoint)
1469 return ep;
1471 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1472 "endpoint %x\n", endpoint);
1473 return NULL;
1476 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1477 int dir, void *ctx, char *buf, int len,
1478 void (*callback)(struct urb *))
1480 struct urb *urb;
1481 struct usb_endpoint_descriptor const *ep_desc;
1482 char const *ep_type_name;
1484 if (endpoint == -1)
1485 return NULL; /* endpoint not needed */
1487 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1488 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1489 if (urb == NULL) {
1490 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1491 return NULL;
1494 if (endpoint == 0) {
1495 /* control EP filled in when used */
1496 return urb;
1499 ep_desc = find_ep(serial, endpoint);
1500 if (!ep_desc) {
1501 /* leak the urb, something's wrong and the callers don't care */
1502 return urb;
1504 if (usb_endpoint_xfer_int(ep_desc)) {
1505 ep_type_name = "INT";
1506 usb_fill_int_urb(urb, serial->dev,
1507 usb_sndintpipe(serial->dev, endpoint) | dir,
1508 buf, len, callback, ctx,
1509 ep_desc->bInterval);
1510 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1511 ep_type_name = "BULK";
1512 usb_fill_bulk_urb(urb, serial->dev,
1513 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1514 buf, len, callback, ctx);
1515 } else {
1516 dev_warn(&serial->interface->dev,
1517 "unsupported endpoint type %x\n",
1518 ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1519 usb_free_urb(urb);
1520 return NULL;
1523 dbg("%s - using urb %p for %s endpoint %x",
1524 __func__, urb, ep_type_name, endpoint);
1525 return urb;
1528 static struct callbacks {
1529 void (*instat_callback)(struct urb *);
1530 void (*glocont_callback)(struct urb *);
1531 void (*indat_callback)(struct urb *);
1532 void (*outdat_callback)(struct urb *);
1533 void (*inack_callback)(struct urb *);
1534 void (*outcont_callback)(struct urb *);
1535 } keyspan_callbacks[] = {
1537 /* msg_usa26 callbacks */
1538 .instat_callback = usa26_instat_callback,
1539 .glocont_callback = usa26_glocont_callback,
1540 .indat_callback = usa26_indat_callback,
1541 .outdat_callback = usa2x_outdat_callback,
1542 .inack_callback = usa26_inack_callback,
1543 .outcont_callback = usa26_outcont_callback,
1544 }, {
1545 /* msg_usa28 callbacks */
1546 .instat_callback = usa28_instat_callback,
1547 .glocont_callback = usa28_glocont_callback,
1548 .indat_callback = usa28_indat_callback,
1549 .outdat_callback = usa2x_outdat_callback,
1550 .inack_callback = usa28_inack_callback,
1551 .outcont_callback = usa28_outcont_callback,
1552 }, {
1553 /* msg_usa49 callbacks */
1554 .instat_callback = usa49_instat_callback,
1555 .glocont_callback = usa49_glocont_callback,
1556 .indat_callback = usa49_indat_callback,
1557 .outdat_callback = usa2x_outdat_callback,
1558 .inack_callback = usa49_inack_callback,
1559 .outcont_callback = usa49_outcont_callback,
1560 }, {
1561 /* msg_usa90 callbacks */
1562 .instat_callback = usa90_instat_callback,
1563 .glocont_callback = usa28_glocont_callback,
1564 .indat_callback = usa90_indat_callback,
1565 .outdat_callback = usa2x_outdat_callback,
1566 .inack_callback = usa28_inack_callback,
1567 .outcont_callback = usa90_outcont_callback,
1568 }, {
1569 /* msg_usa67 callbacks */
1570 .instat_callback = usa67_instat_callback,
1571 .glocont_callback = usa67_glocont_callback,
1572 .indat_callback = usa26_indat_callback,
1573 .outdat_callback = usa2x_outdat_callback,
1574 .inack_callback = usa26_inack_callback,
1575 .outcont_callback = usa26_outcont_callback,
1579 /* Generic setup urbs function that uses
1580 data in device_details */
1581 static void keyspan_setup_urbs(struct usb_serial *serial)
1583 int i, j;
1584 struct keyspan_serial_private *s_priv;
1585 const struct keyspan_device_details *d_details;
1586 struct usb_serial_port *port;
1587 struct keyspan_port_private *p_priv;
1588 struct callbacks *cback;
1589 int endp;
1591 dbg ("%s", __FUNCTION__);
1593 s_priv = usb_get_serial_data(serial);
1594 d_details = s_priv->device_details;
1596 /* Setup values for the various callback routines */
1597 cback = &keyspan_callbacks[d_details->msg_format];
1599 /* Allocate and set up urbs for each one that is in use,
1600 starting with instat endpoints */
1601 s_priv->instat_urb = keyspan_setup_urb
1602 (serial, d_details->instat_endpoint, USB_DIR_IN,
1603 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1604 cback->instat_callback);
1606 s_priv->indat_urb = keyspan_setup_urb
1607 (serial, d_details->indat_endpoint, USB_DIR_IN,
1608 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1609 usa49wg_indat_callback);
1611 s_priv->glocont_urb = keyspan_setup_urb
1612 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1613 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1614 cback->glocont_callback);
1616 /* Setup endpoints for each port specific thing */
1617 for (i = 0; i < d_details->num_ports; i ++) {
1618 port = serial->port[i];
1619 p_priv = usb_get_serial_port_data(port);
1621 /* Do indat endpoints first, once for each flip */
1622 endp = d_details->indat_endpoints[i];
1623 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1624 p_priv->in_urbs[j] = keyspan_setup_urb
1625 (serial, endp, USB_DIR_IN, port,
1626 p_priv->in_buffer[j], 64,
1627 cback->indat_callback);
1629 for (; j < 2; ++j)
1630 p_priv->in_urbs[j] = NULL;
1632 /* outdat endpoints also have flip */
1633 endp = d_details->outdat_endpoints[i];
1634 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1635 p_priv->out_urbs[j] = keyspan_setup_urb
1636 (serial, endp, USB_DIR_OUT, port,
1637 p_priv->out_buffer[j], 64,
1638 cback->outdat_callback);
1640 for (; j < 2; ++j)
1641 p_priv->out_urbs[j] = NULL;
1643 /* inack endpoint */
1644 p_priv->inack_urb = keyspan_setup_urb
1645 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1646 port, p_priv->inack_buffer, 1, cback->inack_callback);
1648 /* outcont endpoint */
1649 p_priv->outcont_urb = keyspan_setup_urb
1650 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1651 port, p_priv->outcont_buffer, 64,
1652 cback->outcont_callback);
1657 /* usa19 function doesn't require prescaler */
1658 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1659 u8 *rate_low, u8 *prescaler, int portnum)
1661 u32 b16, /* baud rate times 16 (actual rate used internally) */
1662 div, /* divisor */
1663 cnt; /* inverse of divisor (programmed into 8051) */
1665 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1667 /* prevent divide by zero... */
1668 if( (b16 = (baud_rate * 16L)) == 0) {
1669 return (KEYSPAN_INVALID_BAUD_RATE);
1672 /* Any "standard" rate over 57k6 is marginal on the USA-19
1673 as we run out of divisor resolution. */
1674 if (baud_rate > 57600) {
1675 return (KEYSPAN_INVALID_BAUD_RATE);
1678 /* calculate the divisor and the counter (its inverse) */
1679 if( (div = (baudclk / b16)) == 0) {
1680 return (KEYSPAN_INVALID_BAUD_RATE);
1682 else {
1683 cnt = 0 - div;
1686 if(div > 0xffff) {
1687 return (KEYSPAN_INVALID_BAUD_RATE);
1690 /* return the counter values if non-null */
1691 if (rate_low) {
1692 *rate_low = (u8) (cnt & 0xff);
1694 if (rate_hi) {
1695 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1697 if (rate_low && rate_hi) {
1698 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1701 return (KEYSPAN_BAUD_RATE_OK);
1704 /* usa19hs function doesn't require prescaler */
1705 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1706 u8 *rate_low, u8 *prescaler, int portnum)
1708 u32 b16, /* baud rate times 16 (actual rate used internally) */
1709 div; /* divisor */
1711 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1713 /* prevent divide by zero... */
1714 if( (b16 = (baud_rate * 16L)) == 0)
1715 return (KEYSPAN_INVALID_BAUD_RATE);
1719 /* calculate the divisor */
1720 if( (div = (baudclk / b16)) == 0)
1721 return (KEYSPAN_INVALID_BAUD_RATE);
1723 if(div > 0xffff)
1724 return (KEYSPAN_INVALID_BAUD_RATE);
1726 /* return the counter values if non-null */
1727 if (rate_low)
1728 *rate_low = (u8) (div & 0xff);
1730 if (rate_hi)
1731 *rate_hi = (u8) ((div >> 8) & 0xff);
1733 if (rate_low && rate_hi)
1734 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1736 return (KEYSPAN_BAUD_RATE_OK);
1739 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1740 u8 *rate_low, u8 *prescaler, int portnum)
1742 u32 b16, /* baud rate times 16 (actual rate used internally) */
1743 clk, /* clock with 13/8 prescaler */
1744 div, /* divisor using 13/8 prescaler */
1745 res, /* resulting baud rate using 13/8 prescaler */
1746 diff, /* error using 13/8 prescaler */
1747 smallest_diff;
1748 u8 best_prescaler;
1749 int i;
1751 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1753 /* prevent divide by zero */
1754 if( (b16 = baud_rate * 16L) == 0) {
1755 return (KEYSPAN_INVALID_BAUD_RATE);
1758 /* Calculate prescaler by trying them all and looking
1759 for best fit */
1761 /* start with largest possible difference */
1762 smallest_diff = 0xffffffff;
1764 /* 0 is an invalid prescaler, used as a flag */
1765 best_prescaler = 0;
1767 for(i = 8; i <= 0xff; ++i) {
1768 clk = (baudclk * 8) / (u32) i;
1770 if( (div = clk / b16) == 0) {
1771 continue;
1774 res = clk / div;
1775 diff= (res > b16) ? (res-b16) : (b16-res);
1777 if(diff < smallest_diff) {
1778 best_prescaler = i;
1779 smallest_diff = diff;
1783 if(best_prescaler == 0) {
1784 return (KEYSPAN_INVALID_BAUD_RATE);
1787 clk = (baudclk * 8) / (u32) best_prescaler;
1788 div = clk / b16;
1790 /* return the divisor and prescaler if non-null */
1791 if (rate_low) {
1792 *rate_low = (u8) (div & 0xff);
1794 if (rate_hi) {
1795 *rate_hi = (u8) ((div >> 8) & 0xff);
1797 if (prescaler) {
1798 *prescaler = best_prescaler;
1799 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1801 return (KEYSPAN_BAUD_RATE_OK);
1804 /* USA-28 supports different maximum baud rates on each port */
1805 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1806 u8 *rate_low, u8 *prescaler, int portnum)
1808 u32 b16, /* baud rate times 16 (actual rate used internally) */
1809 div, /* divisor */
1810 cnt; /* inverse of divisor (programmed into 8051) */
1812 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1814 /* prevent divide by zero */
1815 if ((b16 = baud_rate * 16L) == 0)
1816 return (KEYSPAN_INVALID_BAUD_RATE);
1818 /* calculate the divisor and the counter (its inverse) */
1819 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1820 return (KEYSPAN_INVALID_BAUD_RATE);
1822 else {
1823 cnt = 0 - div;
1826 /* check for out of range, based on portnum,
1827 and return result */
1828 if(portnum == 0) {
1829 if(div > 0xffff)
1830 return (KEYSPAN_INVALID_BAUD_RATE);
1832 else {
1833 if(portnum == 1) {
1834 if(div > 0xff) {
1835 return (KEYSPAN_INVALID_BAUD_RATE);
1838 else {
1839 return (KEYSPAN_INVALID_BAUD_RATE);
1843 /* return the counter values if not NULL
1844 (port 1 will ignore retHi) */
1845 if (rate_low) {
1846 *rate_low = (u8) (cnt & 0xff);
1848 if (rate_hi) {
1849 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1851 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1852 return (KEYSPAN_BAUD_RATE_OK);
1855 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1856 struct usb_serial_port *port,
1857 int reset_port)
1859 struct keyspan_usa26_portControlMessage msg;
1860 struct keyspan_serial_private *s_priv;
1861 struct keyspan_port_private *p_priv;
1862 const struct keyspan_device_details *d_details;
1863 int outcont_urb;
1864 struct urb *this_urb;
1865 int device_port, err;
1867 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1869 s_priv = usb_get_serial_data(serial);
1870 p_priv = usb_get_serial_port_data(port);
1871 d_details = s_priv->device_details;
1872 device_port = port->number - port->serial->minor;
1874 outcont_urb = d_details->outcont_endpoints[port->number];
1875 this_urb = p_priv->outcont_urb;
1877 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1879 /* Make sure we have an urb then send the message */
1880 if (this_urb == NULL) {
1881 dbg("%s - oops no urb.", __FUNCTION__);
1882 return -1;
1885 /* Save reset port val for resend.
1886 Don't overwrite resend for open/close condition. */
1887 if ((reset_port + 1) > p_priv->resend_cont)
1888 p_priv->resend_cont = reset_port + 1;
1889 if (this_urb->status == -EINPROGRESS) {
1890 /* dbg ("%s - already writing", __FUNCTION__); */
1891 mdelay(5);
1892 return(-1);
1895 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1897 /* Only set baud rate if it's changed */
1898 if (p_priv->old_baud != p_priv->baud) {
1899 p_priv->old_baud = p_priv->baud;
1900 msg.setClocking = 0xff;
1901 if (d_details->calculate_baud_rate
1902 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1903 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1904 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1905 p_priv->baud);
1906 msg.baudLo = 0;
1907 msg.baudHi = 125; /* Values for 9600 baud */
1908 msg.prescaler = 10;
1910 msg.setPrescaler = 0xff;
1913 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1914 switch (p_priv->cflag & CSIZE) {
1915 case CS5:
1916 msg.lcr |= USA_DATABITS_5;
1917 break;
1918 case CS6:
1919 msg.lcr |= USA_DATABITS_6;
1920 break;
1921 case CS7:
1922 msg.lcr |= USA_DATABITS_7;
1923 break;
1924 case CS8:
1925 msg.lcr |= USA_DATABITS_8;
1926 break;
1928 if (p_priv->cflag & PARENB) {
1929 /* note USA_PARITY_NONE == 0 */
1930 msg.lcr |= (p_priv->cflag & PARODD)?
1931 USA_PARITY_ODD: USA_PARITY_EVEN;
1933 msg.setLcr = 0xff;
1935 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1936 msg.xonFlowControl = 0;
1937 msg.setFlowControl = 0xff;
1938 msg.forwardingLength = 16;
1939 msg.xonChar = 17;
1940 msg.xoffChar = 19;
1942 /* Opening port */
1943 if (reset_port == 1) {
1944 msg._txOn = 1;
1945 msg._txOff = 0;
1946 msg.txFlush = 0;
1947 msg.txBreak = 0;
1948 msg.rxOn = 1;
1949 msg.rxOff = 0;
1950 msg.rxFlush = 1;
1951 msg.rxForward = 0;
1952 msg.returnStatus = 0;
1953 msg.resetDataToggle = 0xff;
1956 /* Closing port */
1957 else if (reset_port == 2) {
1958 msg._txOn = 0;
1959 msg._txOff = 1;
1960 msg.txFlush = 0;
1961 msg.txBreak = 0;
1962 msg.rxOn = 0;
1963 msg.rxOff = 1;
1964 msg.rxFlush = 1;
1965 msg.rxForward = 0;
1966 msg.returnStatus = 0;
1967 msg.resetDataToggle = 0;
1970 /* Sending intermediate configs */
1971 else {
1972 msg._txOn = (! p_priv->break_on);
1973 msg._txOff = 0;
1974 msg.txFlush = 0;
1975 msg.txBreak = (p_priv->break_on);
1976 msg.rxOn = 0;
1977 msg.rxOff = 0;
1978 msg.rxFlush = 0;
1979 msg.rxForward = 0;
1980 msg.returnStatus = 0;
1981 msg.resetDataToggle = 0x0;
1984 /* Do handshaking outputs */
1985 msg.setTxTriState_setRts = 0xff;
1986 msg.txTriState_rts = p_priv->rts_state;
1988 msg.setHskoa_setDtr = 0xff;
1989 msg.hskoa_dtr = p_priv->dtr_state;
1991 p_priv->resend_cont = 0;
1992 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1994 /* send the data out the device on control endpoint */
1995 this_urb->transfer_buffer_length = sizeof(msg);
1997 this_urb->dev = serial->dev;
1998 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1999 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2001 #if 0
2002 else {
2003 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
2004 outcont_urb, this_urb->transfer_buffer_length,
2005 usb_pipeendpoint(this_urb->pipe));
2007 #endif
2009 return (0);
2012 static int keyspan_usa28_send_setup(struct usb_serial *serial,
2013 struct usb_serial_port *port,
2014 int reset_port)
2016 struct keyspan_usa28_portControlMessage msg;
2017 struct keyspan_serial_private *s_priv;
2018 struct keyspan_port_private *p_priv;
2019 const struct keyspan_device_details *d_details;
2020 struct urb *this_urb;
2021 int device_port, err;
2023 dbg ("%s", __FUNCTION__);
2025 s_priv = usb_get_serial_data(serial);
2026 p_priv = usb_get_serial_port_data(port);
2027 d_details = s_priv->device_details;
2028 device_port = port->number - port->serial->minor;
2030 /* only do something if we have a bulk out endpoint */
2031 if ((this_urb = p_priv->outcont_urb) == NULL) {
2032 dbg("%s - oops no urb.", __FUNCTION__);
2033 return -1;
2036 /* Save reset port val for resend.
2037 Don't overwrite resend for open/close condition. */
2038 if ((reset_port + 1) > p_priv->resend_cont)
2039 p_priv->resend_cont = reset_port + 1;
2040 if (this_urb->status == -EINPROGRESS) {
2041 dbg ("%s already writing", __FUNCTION__);
2042 mdelay(5);
2043 return(-1);
2046 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
2048 msg.setBaudRate = 1;
2049 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2050 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2051 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
2052 msg.baudLo = 0xff;
2053 msg.baudHi = 0xb2; /* Values for 9600 baud */
2056 /* If parity is enabled, we must calculate it ourselves. */
2057 msg.parity = 0; /* XXX for now */
2059 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2060 msg.xonFlowControl = 0;
2062 /* Do handshaking outputs, DTR is inverted relative to RTS */
2063 msg.rts = p_priv->rts_state;
2064 msg.dtr = p_priv->dtr_state;
2066 msg.forwardingLength = 16;
2067 msg.forwardMs = 10;
2068 msg.breakThreshold = 45;
2069 msg.xonChar = 17;
2070 msg.xoffChar = 19;
2072 /*msg.returnStatus = 1;
2073 msg.resetDataToggle = 0xff;*/
2074 /* Opening port */
2075 if (reset_port == 1) {
2076 msg._txOn = 1;
2077 msg._txOff = 0;
2078 msg.txFlush = 0;
2079 msg.txForceXoff = 0;
2080 msg.txBreak = 0;
2081 msg.rxOn = 1;
2082 msg.rxOff = 0;
2083 msg.rxFlush = 1;
2084 msg.rxForward = 0;
2085 msg.returnStatus = 0;
2086 msg.resetDataToggle = 0xff;
2088 /* Closing port */
2089 else if (reset_port == 2) {
2090 msg._txOn = 0;
2091 msg._txOff = 1;
2092 msg.txFlush = 0;
2093 msg.txForceXoff = 0;
2094 msg.txBreak = 0;
2095 msg.rxOn = 0;
2096 msg.rxOff = 1;
2097 msg.rxFlush = 1;
2098 msg.rxForward = 0;
2099 msg.returnStatus = 0;
2100 msg.resetDataToggle = 0;
2102 /* Sending intermediate configs */
2103 else {
2104 msg._txOn = (! p_priv->break_on);
2105 msg._txOff = 0;
2106 msg.txFlush = 0;
2107 msg.txForceXoff = 0;
2108 msg.txBreak = (p_priv->break_on);
2109 msg.rxOn = 0;
2110 msg.rxOff = 0;
2111 msg.rxFlush = 0;
2112 msg.rxForward = 0;
2113 msg.returnStatus = 0;
2114 msg.resetDataToggle = 0x0;
2117 p_priv->resend_cont = 0;
2118 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2120 /* send the data out the device on control endpoint */
2121 this_urb->transfer_buffer_length = sizeof(msg);
2123 this_urb->dev = serial->dev;
2124 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2125 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
2127 #if 0
2128 else {
2129 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
2130 this_urb->transfer_buffer_length);
2132 #endif
2134 return (0);
2137 static int keyspan_usa49_send_setup(struct usb_serial *serial,
2138 struct usb_serial_port *port,
2139 int reset_port)
2141 struct keyspan_usa49_portControlMessage msg;
2142 struct usb_ctrlrequest *dr = NULL;
2143 struct keyspan_serial_private *s_priv;
2144 struct keyspan_port_private *p_priv;
2145 const struct keyspan_device_details *d_details;
2146 struct urb *this_urb;
2147 int err, device_port;
2149 dbg ("%s", __FUNCTION__);
2151 s_priv = usb_get_serial_data(serial);
2152 p_priv = usb_get_serial_port_data(port);
2153 d_details = s_priv->device_details;
2155 this_urb = s_priv->glocont_urb;
2157 /* Work out which port within the device is being setup */
2158 device_port = port->number - port->serial->minor;
2160 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
2162 /* Make sure we have an urb then send the message */
2163 if (this_urb == NULL) {
2164 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
2165 return -1;
2168 /* Save reset port val for resend.
2169 Don't overwrite resend for open/close condition. */
2170 if ((reset_port + 1) > p_priv->resend_cont)
2171 p_priv->resend_cont = reset_port + 1;
2173 if (this_urb->status == -EINPROGRESS) {
2174 /* dbg ("%s - already writing", __FUNCTION__); */
2175 mdelay(5);
2176 return(-1);
2179 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
2181 /*msg.portNumber = port->number;*/
2182 msg.portNumber = device_port;
2184 /* Only set baud rate if it's changed */
2185 if (p_priv->old_baud != p_priv->baud) {
2186 p_priv->old_baud = p_priv->baud;
2187 msg.setClocking = 0xff;
2188 if (d_details->calculate_baud_rate
2189 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2190 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2191 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2192 p_priv->baud);
2193 msg.baudLo = 0;
2194 msg.baudHi = 125; /* Values for 9600 baud */
2195 msg.prescaler = 10;
2197 //msg.setPrescaler = 0xff;
2200 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2201 switch (p_priv->cflag & CSIZE) {
2202 case CS5:
2203 msg.lcr |= USA_DATABITS_5;
2204 break;
2205 case CS6:
2206 msg.lcr |= USA_DATABITS_6;
2207 break;
2208 case CS7:
2209 msg.lcr |= USA_DATABITS_7;
2210 break;
2211 case CS8:
2212 msg.lcr |= USA_DATABITS_8;
2213 break;
2215 if (p_priv->cflag & PARENB) {
2216 /* note USA_PARITY_NONE == 0 */
2217 msg.lcr |= (p_priv->cflag & PARODD)?
2218 USA_PARITY_ODD: USA_PARITY_EVEN;
2220 msg.setLcr = 0xff;
2222 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2223 msg.xonFlowControl = 0;
2224 msg.setFlowControl = 0xff;
2226 msg.forwardingLength = 16;
2227 msg.xonChar = 17;
2228 msg.xoffChar = 19;
2230 /* Opening port */
2231 if (reset_port == 1) {
2232 msg._txOn = 1;
2233 msg._txOff = 0;
2234 msg.txFlush = 0;
2235 msg.txBreak = 0;
2236 msg.rxOn = 1;
2237 msg.rxOff = 0;
2238 msg.rxFlush = 1;
2239 msg.rxForward = 0;
2240 msg.returnStatus = 0;
2241 msg.resetDataToggle = 0xff;
2242 msg.enablePort = 1;
2243 msg.disablePort = 0;
2245 /* Closing port */
2246 else if (reset_port == 2) {
2247 msg._txOn = 0;
2248 msg._txOff = 1;
2249 msg.txFlush = 0;
2250 msg.txBreak = 0;
2251 msg.rxOn = 0;
2252 msg.rxOff = 1;
2253 msg.rxFlush = 1;
2254 msg.rxForward = 0;
2255 msg.returnStatus = 0;
2256 msg.resetDataToggle = 0;
2257 msg.enablePort = 0;
2258 msg.disablePort = 1;
2260 /* Sending intermediate configs */
2261 else {
2262 msg._txOn = (! p_priv->break_on);
2263 msg._txOff = 0;
2264 msg.txFlush = 0;
2265 msg.txBreak = (p_priv->break_on);
2266 msg.rxOn = 0;
2267 msg.rxOff = 0;
2268 msg.rxFlush = 0;
2269 msg.rxForward = 0;
2270 msg.returnStatus = 0;
2271 msg.resetDataToggle = 0x0;
2272 msg.enablePort = 0;
2273 msg.disablePort = 0;
2276 /* Do handshaking outputs */
2277 msg.setRts = 0xff;
2278 msg.rts = p_priv->rts_state;
2280 msg.setDtr = 0xff;
2281 msg.dtr = p_priv->dtr_state;
2283 p_priv->resend_cont = 0;
2285 /* if the device is a 49wg, we send control message on usb control EP 0 */
2287 if (d_details->product_id == keyspan_usa49wg_product_id) {
2288 dr = (void *)(s_priv->ctrl_buf);
2289 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2290 dr->bRequest = 0xB0; /* 49wg control message */;
2291 dr->wValue = 0;
2292 dr->wIndex = 0;
2293 dr->wLength = cpu_to_le16(sizeof(msg));
2295 memcpy (s_priv->glocont_buf, &msg, sizeof(msg));
2297 usb_fill_control_urb(this_urb, serial->dev, usb_sndctrlpipe(serial->dev, 0),
2298 (unsigned char *)dr, s_priv->glocont_buf, sizeof(msg),
2299 usa49_glocont_callback, serial);
2301 } else {
2302 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2304 /* send the data out the device on control endpoint */
2305 this_urb->transfer_buffer_length = sizeof(msg);
2307 this_urb->dev = serial->dev;
2309 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2310 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2312 #if 0
2313 else {
2314 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2315 outcont_urb, this_urb->transfer_buffer_length,
2316 usb_pipeendpoint(this_urb->pipe));
2318 #endif
2320 return (0);
2323 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2324 struct usb_serial_port *port,
2325 int reset_port)
2327 struct keyspan_usa90_portControlMessage msg;
2328 struct keyspan_serial_private *s_priv;
2329 struct keyspan_port_private *p_priv;
2330 const struct keyspan_device_details *d_details;
2331 struct urb *this_urb;
2332 int err;
2333 u8 prescaler;
2335 dbg ("%s", __FUNCTION__);
2337 s_priv = usb_get_serial_data(serial);
2338 p_priv = usb_get_serial_port_data(port);
2339 d_details = s_priv->device_details;
2341 /* only do something if we have a bulk out endpoint */
2342 if ((this_urb = p_priv->outcont_urb) == NULL) {
2343 dbg("%s - oops no urb.", __FUNCTION__);
2344 return -1;
2347 /* Save reset port val for resend.
2348 Don't overwrite resend for open/close condition. */
2349 if ((reset_port + 1) > p_priv->resend_cont)
2350 p_priv->resend_cont = reset_port + 1;
2351 if (this_urb->status == -EINPROGRESS) {
2352 dbg ("%s already writing", __FUNCTION__);
2353 mdelay(5);
2354 return(-1);
2357 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2359 /* Only set baud rate if it's changed */
2360 if (p_priv->old_baud != p_priv->baud) {
2361 p_priv->old_baud = p_priv->baud;
2362 msg.setClocking = 0x01;
2363 if (d_details->calculate_baud_rate
2364 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2365 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2366 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2367 p_priv->baud);
2368 p_priv->baud = 9600;
2369 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2370 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2372 msg.setRxMode = 1;
2373 msg.setTxMode = 1;
2376 /* modes must always be correctly specified */
2377 if (p_priv->baud > 57600)
2379 msg.rxMode = RXMODE_DMA;
2380 msg.txMode = TXMODE_DMA;
2382 else
2384 msg.rxMode = RXMODE_BYHAND;
2385 msg.txMode = TXMODE_BYHAND;
2388 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2389 switch (p_priv->cflag & CSIZE) {
2390 case CS5:
2391 msg.lcr |= USA_DATABITS_5;
2392 break;
2393 case CS6:
2394 msg.lcr |= USA_DATABITS_6;
2395 break;
2396 case CS7:
2397 msg.lcr |= USA_DATABITS_7;
2398 break;
2399 case CS8:
2400 msg.lcr |= USA_DATABITS_8;
2401 break;
2403 if (p_priv->cflag & PARENB) {
2404 /* note USA_PARITY_NONE == 0 */
2405 msg.lcr |= (p_priv->cflag & PARODD)?
2406 USA_PARITY_ODD: USA_PARITY_EVEN;
2408 if (p_priv->old_cflag != p_priv->cflag) {
2409 p_priv->old_cflag = p_priv->cflag;
2410 msg.setLcr = 0x01;
2413 if (p_priv->flow_control == flow_cts)
2414 msg.txFlowControl = TXFLOW_CTS;
2415 msg.setTxFlowControl = 0x01;
2416 msg.setRxFlowControl = 0x01;
2418 msg.rxForwardingLength = 16;
2419 msg.rxForwardingTimeout = 16;
2420 msg.txAckSetting = 0;
2421 msg.xonChar = 17;
2422 msg.xoffChar = 19;
2424 /* Opening port */
2425 if (reset_port == 1) {
2426 msg.portEnabled = 1;
2427 msg.rxFlush = 1;
2428 msg.txBreak = (p_priv->break_on);
2430 /* Closing port */
2431 else if (reset_port == 2) {
2432 msg.portEnabled = 0;
2434 /* Sending intermediate configs */
2435 else {
2436 if (port->open_count)
2437 msg.portEnabled = 1;
2438 msg.txBreak = (p_priv->break_on);
2441 /* Do handshaking outputs */
2442 msg.setRts = 0x01;
2443 msg.rts = p_priv->rts_state;
2445 msg.setDtr = 0x01;
2446 msg.dtr = p_priv->dtr_state;
2448 p_priv->resend_cont = 0;
2449 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2451 /* send the data out the device on control endpoint */
2452 this_urb->transfer_buffer_length = sizeof(msg);
2454 this_urb->dev = serial->dev;
2455 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2456 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2458 return (0);
2461 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2462 struct usb_serial_port *port,
2463 int reset_port)
2465 struct keyspan_usa67_portControlMessage msg;
2466 struct keyspan_serial_private *s_priv;
2467 struct keyspan_port_private *p_priv;
2468 const struct keyspan_device_details *d_details;
2469 struct urb *this_urb;
2470 int err, device_port;
2472 dbg ("%s", __FUNCTION__);
2474 s_priv = usb_get_serial_data(serial);
2475 p_priv = usb_get_serial_port_data(port);
2476 d_details = s_priv->device_details;
2478 this_urb = s_priv->glocont_urb;
2480 /* Work out which port within the device is being setup */
2481 device_port = port->number - port->serial->minor;
2483 /* Make sure we have an urb then send the message */
2484 if (this_urb == NULL) {
2485 dbg("%s - oops no urb for port %d.", __FUNCTION__,
2486 port->number);
2487 return -1;
2490 /* Save reset port val for resend.
2491 Don't overwrite resend for open/close condition. */
2492 if ((reset_port + 1) > p_priv->resend_cont)
2493 p_priv->resend_cont = reset_port + 1;
2494 if (this_urb->status == -EINPROGRESS) {
2495 /* dbg ("%s - already writing", __FUNCTION__); */
2496 mdelay(5);
2497 return(-1);
2500 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2502 msg.port = device_port;
2504 /* Only set baud rate if it's changed */
2505 if (p_priv->old_baud != p_priv->baud) {
2506 p_priv->old_baud = p_priv->baud;
2507 msg.setClocking = 0xff;
2508 if (d_details->calculate_baud_rate
2509 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2510 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2511 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2512 p_priv->baud);
2513 msg.baudLo = 0;
2514 msg.baudHi = 125; /* Values for 9600 baud */
2515 msg.prescaler = 10;
2517 msg.setPrescaler = 0xff;
2520 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2521 switch (p_priv->cflag & CSIZE) {
2522 case CS5:
2523 msg.lcr |= USA_DATABITS_5;
2524 break;
2525 case CS6:
2526 msg.lcr |= USA_DATABITS_6;
2527 break;
2528 case CS7:
2529 msg.lcr |= USA_DATABITS_7;
2530 break;
2531 case CS8:
2532 msg.lcr |= USA_DATABITS_8;
2533 break;
2535 if (p_priv->cflag & PARENB) {
2536 /* note USA_PARITY_NONE == 0 */
2537 msg.lcr |= (p_priv->cflag & PARODD)?
2538 USA_PARITY_ODD: USA_PARITY_EVEN;
2540 msg.setLcr = 0xff;
2542 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2543 msg.xonFlowControl = 0;
2544 msg.setFlowControl = 0xff;
2545 msg.forwardingLength = 16;
2546 msg.xonChar = 17;
2547 msg.xoffChar = 19;
2549 if (reset_port == 1) {
2550 /* Opening port */
2551 msg._txOn = 1;
2552 msg._txOff = 0;
2553 msg.txFlush = 0;
2554 msg.txBreak = 0;
2555 msg.rxOn = 1;
2556 msg.rxOff = 0;
2557 msg.rxFlush = 1;
2558 msg.rxForward = 0;
2559 msg.returnStatus = 0;
2560 msg.resetDataToggle = 0xff;
2561 } else if (reset_port == 2) {
2562 /* Closing port */
2563 msg._txOn = 0;
2564 msg._txOff = 1;
2565 msg.txFlush = 0;
2566 msg.txBreak = 0;
2567 msg.rxOn = 0;
2568 msg.rxOff = 1;
2569 msg.rxFlush = 1;
2570 msg.rxForward = 0;
2571 msg.returnStatus = 0;
2572 msg.resetDataToggle = 0;
2573 } else {
2574 /* Sending intermediate configs */
2575 msg._txOn = (! p_priv->break_on);
2576 msg._txOff = 0;
2577 msg.txFlush = 0;
2578 msg.txBreak = (p_priv->break_on);
2579 msg.rxOn = 0;
2580 msg.rxOff = 0;
2581 msg.rxFlush = 0;
2582 msg.rxForward = 0;
2583 msg.returnStatus = 0;
2584 msg.resetDataToggle = 0x0;
2587 /* Do handshaking outputs */
2588 msg.setTxTriState_setRts = 0xff;
2589 msg.txTriState_rts = p_priv->rts_state;
2591 msg.setHskoa_setDtr = 0xff;
2592 msg.hskoa_dtr = p_priv->dtr_state;
2594 p_priv->resend_cont = 0;
2596 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2598 /* send the data out the device on control endpoint */
2599 this_urb->transfer_buffer_length = sizeof(msg);
2600 this_urb->dev = serial->dev;
2602 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2603 if (err != 0)
2604 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__,
2605 err);
2606 return (0);
2609 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2611 struct usb_serial *serial = port->serial;
2612 struct keyspan_serial_private *s_priv;
2613 const struct keyspan_device_details *d_details;
2615 dbg ("%s", __FUNCTION__);
2617 s_priv = usb_get_serial_data(serial);
2618 d_details = s_priv->device_details;
2620 switch (d_details->msg_format) {
2621 case msg_usa26:
2622 keyspan_usa26_send_setup(serial, port, reset_port);
2623 break;
2624 case msg_usa28:
2625 keyspan_usa28_send_setup(serial, port, reset_port);
2626 break;
2627 case msg_usa49:
2628 keyspan_usa49_send_setup(serial, port, reset_port);
2629 break;
2630 case msg_usa90:
2631 keyspan_usa90_send_setup(serial, port, reset_port);
2632 break;
2633 case msg_usa67:
2634 keyspan_usa67_send_setup(serial, port, reset_port);
2635 break;
2640 /* Gets called by the "real" driver (ie once firmware is loaded
2641 and renumeration has taken place. */
2642 static int keyspan_startup (struct usb_serial *serial)
2644 int i, err;
2645 struct usb_serial_port *port;
2646 struct keyspan_serial_private *s_priv;
2647 struct keyspan_port_private *p_priv;
2648 const struct keyspan_device_details *d_details;
2650 dbg("%s", __FUNCTION__);
2652 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2653 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2654 break;
2655 if (d_details == NULL) {
2656 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
2657 return 1;
2660 /* Setup private data for serial driver */
2661 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2662 if (!s_priv) {
2663 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2664 return -ENOMEM;
2667 s_priv->device_details = d_details;
2668 usb_set_serial_data(serial, s_priv);
2670 /* Now setup per port private data */
2671 for (i = 0; i < serial->num_ports; i++) {
2672 port = serial->port[i];
2673 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2674 if (!p_priv) {
2675 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2676 return (1);
2678 p_priv->device_details = d_details;
2679 usb_set_serial_port_data(port, p_priv);
2682 keyspan_setup_urbs(serial);
2684 if (s_priv->instat_urb != NULL) {
2685 s_priv->instat_urb->dev = serial->dev;
2686 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2687 if (err != 0)
2688 dbg("%s - submit instat urb failed %d", __FUNCTION__,
2689 err);
2691 if (s_priv->indat_urb != NULL) {
2692 s_priv->indat_urb->dev = serial->dev;
2693 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2694 if (err != 0)
2695 dbg("%s - submit indat urb failed %d", __FUNCTION__,
2696 err);
2699 return (0);
2702 static void keyspan_shutdown (struct usb_serial *serial)
2704 int i, j;
2705 struct usb_serial_port *port;
2706 struct keyspan_serial_private *s_priv;
2707 struct keyspan_port_private *p_priv;
2709 dbg("%s", __FUNCTION__);
2711 s_priv = usb_get_serial_data(serial);
2713 /* Stop reading/writing urbs */
2714 stop_urb(s_priv->instat_urb);
2715 stop_urb(s_priv->glocont_urb);
2716 stop_urb(s_priv->indat_urb);
2717 for (i = 0; i < serial->num_ports; ++i) {
2718 port = serial->port[i];
2719 p_priv = usb_get_serial_port_data(port);
2720 stop_urb(p_priv->inack_urb);
2721 stop_urb(p_priv->outcont_urb);
2722 for (j = 0; j < 2; j++) {
2723 stop_urb(p_priv->in_urbs[j]);
2724 stop_urb(p_priv->out_urbs[j]);
2728 /* Now free them */
2729 usb_free_urb(s_priv->instat_urb);
2730 usb_free_urb(s_priv->indat_urb);
2731 usb_free_urb(s_priv->glocont_urb);
2732 for (i = 0; i < serial->num_ports; ++i) {
2733 port = serial->port[i];
2734 p_priv = usb_get_serial_port_data(port);
2735 usb_free_urb(p_priv->inack_urb);
2736 usb_free_urb(p_priv->outcont_urb);
2737 for (j = 0; j < 2; j++) {
2738 usb_free_urb(p_priv->in_urbs[j]);
2739 usb_free_urb(p_priv->out_urbs[j]);
2743 /* dbg("Freeing serial->private."); */
2744 kfree(s_priv);
2746 /* dbg("Freeing port->private."); */
2747 /* Now free per port private data */
2748 for (i = 0; i < serial->num_ports; i++) {
2749 port = serial->port[i];
2750 kfree(usb_get_serial_port_data(port));
2754 MODULE_AUTHOR( DRIVER_AUTHOR );
2755 MODULE_DESCRIPTION( DRIVER_DESC );
2756 MODULE_LICENSE("GPL");
2758 module_param(debug, bool, S_IRUGO | S_IWUSR);
2759 MODULE_PARM_DESC(debug, "Debug enabled or not");