[RFKILL]: fix net/rfkill/rfkill-input.c bug on 64-bit systems
[usb.git] / drivers / usb / serial / keyspan.c
blobf2a6fce5de1e9fe688b32004364e8589a1f0e8e3
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;
282 dbg("%s", __FUNCTION__);
284 p_priv = usb_get_serial_port_data(port);
285 d_details = p_priv->device_details;
286 cflag = port->tty->termios->c_cflag;
287 device_port = port->number - port->serial->minor;
289 /* Baud rate calculation takes baud rate as an integer
290 so other rates can be generated if desired. */
291 baud_rate = tty_get_baud_rate(port->tty);
292 /* If no match or invalid, don't change */
293 if (baud_rate >= 0
294 && 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 p_priv->baud = baud_rate;
300 /* set CTS/RTS handshake etc. */
301 p_priv->cflag = cflag;
302 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
304 keyspan_send_setup(port, 0);
307 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
309 unsigned int value;
310 struct keyspan_port_private *p_priv;
312 p_priv = usb_get_serial_port_data(port);
314 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
315 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
316 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
317 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
318 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
319 ((p_priv->ri_state) ? TIOCM_RNG : 0);
321 return value;
324 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
325 unsigned int set, unsigned int clear)
327 struct keyspan_port_private *p_priv;
329 p_priv = usb_get_serial_port_data(port);
331 if (set & TIOCM_RTS)
332 p_priv->rts_state = 1;
333 if (set & TIOCM_DTR)
334 p_priv->dtr_state = 1;
336 if (clear & TIOCM_RTS)
337 p_priv->rts_state = 0;
338 if (clear & TIOCM_DTR)
339 p_priv->dtr_state = 0;
340 keyspan_send_setup(port, 0);
341 return 0;
344 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
345 unsigned int cmd, unsigned long arg)
347 return -ENOIOCTLCMD;
350 /* Write function is similar for the four protocols used
351 with only a minor change for usa90 (usa19hs) required */
352 static int keyspan_write(struct usb_serial_port *port,
353 const unsigned char *buf, int count)
355 struct keyspan_port_private *p_priv;
356 const struct keyspan_device_details *d_details;
357 int flip;
358 int left, todo;
359 struct urb *this_urb;
360 int err, maxDataLen, dataOffset;
362 p_priv = usb_get_serial_port_data(port);
363 d_details = p_priv->device_details;
365 if (d_details->msg_format == msg_usa90) {
366 maxDataLen = 64;
367 dataOffset = 0;
368 } else {
369 maxDataLen = 63;
370 dataOffset = 1;
373 dbg("%s - for port %d (%d chars), flip=%d",
374 __FUNCTION__, port->number, count, p_priv->out_flip);
376 for (left = count; left > 0; left -= todo) {
377 todo = left;
378 if (todo > maxDataLen)
379 todo = maxDataLen;
381 flip = p_priv->out_flip;
383 /* Check we have a valid urb/endpoint before we use it... */
384 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
385 /* no bulk out, so return 0 bytes written */
386 dbg("%s - no output urb :(", __FUNCTION__);
387 return count;
390 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
392 if (this_urb->status == -EINPROGRESS) {
393 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
394 break;
395 usb_unlink_urb(this_urb);
396 break;
399 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
400 for now so set to zero */
401 ((char *)this_urb->transfer_buffer)[0] = 0;
403 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
404 buf += todo;
406 /* send the data out the bulk port */
407 this_urb->transfer_buffer_length = todo + dataOffset;
409 this_urb->dev = port->serial->dev;
410 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
411 dbg("usb_submit_urb(write bulk) failed (%d)", err);
413 p_priv->tx_start_time[flip] = jiffies;
415 /* Flip for next time if usa26 or usa28 interface
416 (not used on usa49) */
417 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
420 return count - left;
423 static void usa26_indat_callback(struct urb *urb)
425 int i, err;
426 int endpoint;
427 struct usb_serial_port *port;
428 struct tty_struct *tty;
429 unsigned char *data = urb->transfer_buffer;
430 int status = urb->status;
432 dbg ("%s", __FUNCTION__);
434 endpoint = usb_pipeendpoint(urb->pipe);
436 if (status) {
437 dbg("%s - nonzero status: %x on endpoint %d.",
438 __FUNCTION__, status, endpoint);
439 return;
442 port = (struct usb_serial_port *) urb->context;
443 tty = port->tty;
444 if (urb->actual_length) {
445 /* 0x80 bit is error flag */
446 if ((data[0] & 0x80) == 0) {
447 /* no errors on individual bytes, only possible overrun err*/
448 if (data[0] & RXERROR_OVERRUN)
449 err = TTY_OVERRUN;
450 else err = 0;
451 for (i = 1; i < urb->actual_length ; ++i) {
452 tty_insert_flip_char(tty, data[i], err);
454 } else {
455 /* some bytes had errors, every byte has status */
456 dbg("%s - RX error!!!!", __FUNCTION__);
457 for (i = 0; i + 1 < urb->actual_length; i += 2) {
458 int stat = data[i], flag = 0;
459 if (stat & RXERROR_OVERRUN)
460 flag |= TTY_OVERRUN;
461 if (stat & RXERROR_FRAMING)
462 flag |= TTY_FRAME;
463 if (stat & RXERROR_PARITY)
464 flag |= TTY_PARITY;
465 /* XXX should handle break (0x10) */
466 tty_insert_flip_char(tty, data[i+1], flag);
469 tty_flip_buffer_push(tty);
472 /* Resubmit urb so we continue receiving */
473 urb->dev = port->serial->dev;
474 if (port->open_count)
475 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
476 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
478 return;
481 /* Outdat handling is common for all devices */
482 static void usa2x_outdat_callback(struct urb *urb)
484 struct usb_serial_port *port;
485 struct keyspan_port_private *p_priv;
487 port = (struct usb_serial_port *) urb->context;
488 p_priv = usb_get_serial_port_data(port);
489 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
491 if (port->open_count)
492 usb_serial_port_softint(port);
495 static void usa26_inack_callback(struct urb *urb)
497 dbg ("%s", __FUNCTION__);
501 static void usa26_outcont_callback(struct urb *urb)
503 struct usb_serial_port *port;
504 struct keyspan_port_private *p_priv;
506 port = (struct usb_serial_port *) urb->context;
507 p_priv = usb_get_serial_port_data(port);
509 if (p_priv->resend_cont) {
510 dbg ("%s - sending setup", __FUNCTION__);
511 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
515 static void usa26_instat_callback(struct urb *urb)
517 unsigned char *data = urb->transfer_buffer;
518 struct keyspan_usa26_portStatusMessage *msg;
519 struct usb_serial *serial;
520 struct usb_serial_port *port;
521 struct keyspan_port_private *p_priv;
522 int old_dcd_state, err;
523 int status = urb->status;
525 serial = (struct usb_serial *) urb->context;
527 if (status) {
528 dbg("%s - nonzero status: %x", __FUNCTION__, status);
529 return;
531 if (urb->actual_length != 9) {
532 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
533 goto exit;
536 msg = (struct keyspan_usa26_portStatusMessage *)data;
538 #if 0
539 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
540 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
541 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
542 #endif
544 /* Now do something useful with the data */
547 /* Check port number from message and retrieve private data */
548 if (msg->port >= serial->num_ports) {
549 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
550 goto exit;
552 port = serial->port[msg->port];
553 p_priv = usb_get_serial_port_data(port);
555 /* Update handshaking pin state information */
556 old_dcd_state = p_priv->dcd_state;
557 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
558 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
559 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
560 p_priv->ri_state = ((msg->ri) ? 1 : 0);
562 if (port->tty && !C_CLOCAL(port->tty)
563 && old_dcd_state != p_priv->dcd_state) {
564 if (old_dcd_state)
565 tty_hangup(port->tty);
566 /* else */
567 /* wake_up_interruptible(&p_priv->open_wait); */
570 /* Resubmit urb so we continue receiving */
571 urb->dev = serial->dev;
572 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
573 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
575 exit: ;
578 static void usa26_glocont_callback(struct urb *urb)
580 dbg ("%s", __FUNCTION__);
585 static void usa28_indat_callback(struct urb *urb)
587 int i, err;
588 struct usb_serial_port *port;
589 struct tty_struct *tty;
590 unsigned char *data;
591 struct keyspan_port_private *p_priv;
592 int status = urb->status;
594 dbg ("%s", __FUNCTION__);
596 port = (struct usb_serial_port *) urb->context;
597 p_priv = usb_get_serial_port_data(port);
598 data = urb->transfer_buffer;
600 if (urb != p_priv->in_urbs[p_priv->in_flip])
601 return;
603 do {
604 if (status) {
605 dbg("%s - nonzero status: %x on endpoint %d.",
606 __FUNCTION__, status, usb_pipeendpoint(urb->pipe));
607 return;
610 port = (struct usb_serial_port *) urb->context;
611 p_priv = usb_get_serial_port_data(port);
612 data = urb->transfer_buffer;
614 tty = port->tty;
615 if (urb->actual_length) {
616 for (i = 0; i < urb->actual_length ; ++i) {
617 tty_insert_flip_char(tty, data[i], 0);
619 tty_flip_buffer_push(tty);
622 /* Resubmit urb so we continue receiving */
623 urb->dev = port->serial->dev;
624 if (port->open_count)
625 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
626 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
628 p_priv->in_flip ^= 1;
630 urb = p_priv->in_urbs[p_priv->in_flip];
631 } while (urb->status != -EINPROGRESS);
634 static void usa28_inack_callback(struct urb *urb)
636 dbg ("%s", __FUNCTION__);
639 static void usa28_outcont_callback(struct urb *urb)
641 struct usb_serial_port *port;
642 struct keyspan_port_private *p_priv;
644 port = (struct usb_serial_port *) urb->context;
645 p_priv = usb_get_serial_port_data(port);
647 if (p_priv->resend_cont) {
648 dbg ("%s - sending setup", __FUNCTION__);
649 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
653 static void usa28_instat_callback(struct urb *urb)
655 int err;
656 unsigned char *data = urb->transfer_buffer;
657 struct keyspan_usa28_portStatusMessage *msg;
658 struct usb_serial *serial;
659 struct usb_serial_port *port;
660 struct keyspan_port_private *p_priv;
661 int old_dcd_state;
662 int status = urb->status;
664 serial = (struct usb_serial *) urb->context;
666 if (status) {
667 dbg("%s - nonzero status: %x", __FUNCTION__, status);
668 return;
671 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
672 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
673 goto exit;
676 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
677 data[0], data[1], data[2], data[3], data[4], data[5],
678 data[6], data[7], data[8], data[9], data[10], data[11]);*/
680 /* Now do something useful with the data */
681 msg = (struct keyspan_usa28_portStatusMessage *)data;
684 /* Check port number from message and retrieve private data */
685 if (msg->port >= serial->num_ports) {
686 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
687 goto exit;
689 port = serial->port[msg->port];
690 p_priv = usb_get_serial_port_data(port);
692 /* Update handshaking pin state information */
693 old_dcd_state = p_priv->dcd_state;
694 p_priv->cts_state = ((msg->cts) ? 1 : 0);
695 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
696 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
697 p_priv->ri_state = ((msg->ri) ? 1 : 0);
699 if (port->tty && !C_CLOCAL(port->tty)
700 && old_dcd_state != p_priv->dcd_state) {
701 if (old_dcd_state)
702 tty_hangup(port->tty);
703 /* else */
704 /* wake_up_interruptible(&p_priv->open_wait); */
707 /* Resubmit urb so we continue receiving */
708 urb->dev = serial->dev;
709 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
710 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
712 exit: ;
715 static void usa28_glocont_callback(struct urb *urb)
717 dbg ("%s", __FUNCTION__);
721 static void usa49_glocont_callback(struct urb *urb)
723 struct usb_serial *serial;
724 struct usb_serial_port *port;
725 struct keyspan_port_private *p_priv;
726 int i;
728 dbg ("%s", __FUNCTION__);
730 serial = (struct usb_serial *) urb->context;
731 for (i = 0; i < serial->num_ports; ++i) {
732 port = serial->port[i];
733 p_priv = usb_get_serial_port_data(port);
735 if (p_priv->resend_cont) {
736 dbg ("%s - sending setup", __FUNCTION__);
737 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
738 break;
743 /* This is actually called glostat in the Keyspan
744 doco */
745 static void usa49_instat_callback(struct urb *urb)
747 int err;
748 unsigned char *data = urb->transfer_buffer;
749 struct keyspan_usa49_portStatusMessage *msg;
750 struct usb_serial *serial;
751 struct usb_serial_port *port;
752 struct keyspan_port_private *p_priv;
753 int old_dcd_state;
754 int status = urb->status;
756 dbg ("%s", __FUNCTION__);
758 serial = (struct usb_serial *) urb->context;
760 if (status) {
761 dbg("%s - nonzero status: %x", __FUNCTION__, status);
762 return;
765 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
766 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
767 goto exit;
770 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
771 data[0], data[1], data[2], data[3], data[4], data[5],
772 data[6], data[7], data[8], data[9], data[10]);*/
774 /* Now do something useful with the data */
775 msg = (struct keyspan_usa49_portStatusMessage *)data;
777 /* Check port number from message and retrieve private data */
778 if (msg->portNumber >= serial->num_ports) {
779 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
780 goto exit;
782 port = serial->port[msg->portNumber];
783 p_priv = usb_get_serial_port_data(port);
785 /* Update handshaking pin state information */
786 old_dcd_state = p_priv->dcd_state;
787 p_priv->cts_state = ((msg->cts) ? 1 : 0);
788 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
789 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
790 p_priv->ri_state = ((msg->ri) ? 1 : 0);
792 if (port->tty && !C_CLOCAL(port->tty)
793 && old_dcd_state != p_priv->dcd_state) {
794 if (old_dcd_state)
795 tty_hangup(port->tty);
796 /* else */
797 /* wake_up_interruptible(&p_priv->open_wait); */
800 /* Resubmit urb so we continue receiving */
801 urb->dev = serial->dev;
803 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
804 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
806 exit: ;
809 static void usa49_inack_callback(struct urb *urb)
811 dbg ("%s", __FUNCTION__);
814 static void usa49_indat_callback(struct urb *urb)
816 int i, err;
817 int endpoint;
818 struct usb_serial_port *port;
819 struct tty_struct *tty;
820 unsigned char *data = urb->transfer_buffer;
821 int status = urb->status;
823 dbg ("%s", __FUNCTION__);
825 endpoint = usb_pipeendpoint(urb->pipe);
827 if (status) {
828 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
829 status, endpoint);
830 return;
833 port = (struct usb_serial_port *) urb->context;
834 tty = port->tty;
835 if (urb->actual_length) {
836 /* 0x80 bit is error flag */
837 if ((data[0] & 0x80) == 0) {
838 /* no error on any byte */
839 for (i = 1; i < urb->actual_length ; ++i) {
840 tty_insert_flip_char(tty, data[i], 0);
842 } else {
843 /* some bytes had errors, every byte has status */
844 for (i = 0; i + 1 < urb->actual_length; i += 2) {
845 int stat = data[i], flag = 0;
846 if (stat & RXERROR_OVERRUN)
847 flag |= TTY_OVERRUN;
848 if (stat & RXERROR_FRAMING)
849 flag |= TTY_FRAME;
850 if (stat & RXERROR_PARITY)
851 flag |= TTY_PARITY;
852 /* XXX should handle break (0x10) */
853 tty_insert_flip_char(tty, data[i+1], flag);
856 tty_flip_buffer_push(tty);
859 /* Resubmit urb so we continue receiving */
860 urb->dev = port->serial->dev;
861 if (port->open_count)
862 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
863 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
867 static void usa49wg_indat_callback(struct urb *urb)
869 int i, len, x, err;
870 struct usb_serial *serial;
871 struct usb_serial_port *port;
872 struct tty_struct *tty;
873 unsigned char *data = urb->transfer_buffer;
874 int status = urb->status;
876 dbg ("%s", __FUNCTION__);
878 serial = urb->context;
880 if (status) {
881 dbg("%s - nonzero status: %x", __FUNCTION__, status);
882 return;
885 /* inbound data is in the form P#, len, status, data */
886 i = 0;
887 len = 0;
889 if (urb->actual_length) {
890 while (i < urb->actual_length) {
892 /* Check port number from message*/
893 if (data[i] >= serial->num_ports) {
894 dbg ("%s - Unexpected port number %d",
895 __FUNCTION__, data[i]);
896 return;
898 port = serial->port[data[i++]];
899 tty = port->tty;
900 len = data[i++];
902 /* 0x80 bit is error flag */
903 if ((data[i] & 0x80) == 0) {
904 /* no error on any byte */
905 i++;
906 for (x = 1; x < len ; ++x)
907 if (port->open_count)
908 tty_insert_flip_char(tty,
909 data[i++], 0);
910 else
911 i++;
912 } else {
914 * some bytes had errors, every byte has status
916 for (x = 0; x + 1 < len; x += 2) {
917 int stat = data[i], flag = 0;
918 if (stat & RXERROR_OVERRUN)
919 flag |= TTY_OVERRUN;
920 if (stat & RXERROR_FRAMING)
921 flag |= TTY_FRAME;
922 if (stat & RXERROR_PARITY)
923 flag |= TTY_PARITY;
924 /* XXX should handle break (0x10) */
925 if (port->open_count)
926 tty_insert_flip_char(tty,
927 data[i+1], flag);
928 i += 2;
931 if (port->open_count)
932 tty_flip_buffer_push(tty);
936 /* Resubmit urb so we continue receiving */
937 urb->dev = serial->dev;
939 err = usb_submit_urb(urb, GFP_ATOMIC);
940 if (err != 0)
941 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
944 /* not used, usa-49 doesn't have per-port control endpoints */
945 static void usa49_outcont_callback(struct urb *urb)
947 dbg ("%s", __FUNCTION__);
950 static void usa90_indat_callback(struct urb *urb)
952 int i, err;
953 int endpoint;
954 struct usb_serial_port *port;
955 struct keyspan_port_private *p_priv;
956 struct tty_struct *tty;
957 unsigned char *data = urb->transfer_buffer;
958 int status = urb->status;
960 dbg ("%s", __FUNCTION__);
962 endpoint = usb_pipeendpoint(urb->pipe);
964 if (status) {
965 dbg("%s - nonzero status: %x on endpoint %d.",
966 __FUNCTION__, status, endpoint);
967 return;
970 port = (struct usb_serial_port *) urb->context;
971 p_priv = usb_get_serial_port_data(port);
973 tty = port->tty;
974 if (urb->actual_length) {
976 /* if current mode is DMA, looks like usa28 format
977 otherwise looks like usa26 data format */
979 if (p_priv->baud > 57600) {
980 for (i = 0; i < urb->actual_length ; ++i)
981 tty_insert_flip_char(tty, data[i], 0);
983 else {
985 /* 0x80 bit is error flag */
986 if ((data[0] & 0x80) == 0) {
987 /* no errors on individual bytes, only possible overrun err*/
988 if (data[0] & RXERROR_OVERRUN)
989 err = TTY_OVERRUN;
990 else err = 0;
991 for (i = 1; i < urb->actual_length ; ++i)
992 tty_insert_flip_char(tty, data[i], err);
995 else {
996 /* some bytes had errors, every byte has status */
997 dbg("%s - RX error!!!!", __FUNCTION__);
998 for (i = 0; i + 1 < urb->actual_length; i += 2) {
999 int stat = data[i], flag = 0;
1000 if (stat & RXERROR_OVERRUN)
1001 flag |= TTY_OVERRUN;
1002 if (stat & RXERROR_FRAMING)
1003 flag |= TTY_FRAME;
1004 if (stat & RXERROR_PARITY)
1005 flag |= TTY_PARITY;
1006 /* XXX should handle break (0x10) */
1007 tty_insert_flip_char(tty, data[i+1], flag);
1011 tty_flip_buffer_push(tty);
1014 /* Resubmit urb so we continue receiving */
1015 urb->dev = port->serial->dev;
1016 if (port->open_count)
1017 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1018 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1020 return;
1024 static void usa90_instat_callback(struct urb *urb)
1026 unsigned char *data = urb->transfer_buffer;
1027 struct keyspan_usa90_portStatusMessage *msg;
1028 struct usb_serial *serial;
1029 struct usb_serial_port *port;
1030 struct keyspan_port_private *p_priv;
1031 int old_dcd_state, err;
1032 int status = urb->status;
1034 serial = (struct usb_serial *) urb->context;
1036 if (status) {
1037 dbg("%s - nonzero status: %x", __FUNCTION__, status);
1038 return;
1040 if (urb->actual_length < 14) {
1041 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
1042 goto exit;
1045 msg = (struct keyspan_usa90_portStatusMessage *)data;
1047 /* Now do something useful with the data */
1049 port = serial->port[0];
1050 p_priv = usb_get_serial_port_data(port);
1052 /* Update handshaking pin state information */
1053 old_dcd_state = p_priv->dcd_state;
1054 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1055 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1056 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1057 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1059 if (port->tty && !C_CLOCAL(port->tty)
1060 && old_dcd_state != p_priv->dcd_state) {
1061 if (old_dcd_state)
1062 tty_hangup(port->tty);
1063 /* else */
1064 /* wake_up_interruptible(&p_priv->open_wait); */
1067 /* Resubmit urb so we continue receiving */
1068 urb->dev = serial->dev;
1069 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1070 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1072 exit:
1076 static void usa90_outcont_callback(struct urb *urb)
1078 struct usb_serial_port *port;
1079 struct keyspan_port_private *p_priv;
1081 port = (struct usb_serial_port *) urb->context;
1082 p_priv = usb_get_serial_port_data(port);
1084 if (p_priv->resend_cont) {
1085 dbg ("%s - sending setup", __FUNCTION__);
1086 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
1090 /* Status messages from the 28xg */
1091 static void usa67_instat_callback(struct urb *urb)
1093 int err;
1094 unsigned char *data = urb->transfer_buffer;
1095 struct keyspan_usa67_portStatusMessage *msg;
1096 struct usb_serial *serial;
1097 struct usb_serial_port *port;
1098 struct keyspan_port_private *p_priv;
1099 int old_dcd_state;
1100 int status = urb->status;
1102 dbg ("%s", __FUNCTION__);
1104 serial = urb->context;
1106 if (status) {
1107 dbg("%s - nonzero status: %x", __FUNCTION__, status);
1108 return;
1111 if (urb->actual_length != sizeof(struct keyspan_usa67_portStatusMessage)) {
1112 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
1113 return;
1117 /* Now do something useful with the data */
1118 msg = (struct keyspan_usa67_portStatusMessage *)data;
1120 /* Check port number from message and retrieve private data */
1121 if (msg->port >= serial->num_ports) {
1122 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
1123 return;
1126 port = serial->port[msg->port];
1127 p_priv = usb_get_serial_port_data(port);
1129 /* Update handshaking pin state information */
1130 old_dcd_state = p_priv->dcd_state;
1131 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1132 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1134 if (port->tty && !C_CLOCAL(port->tty)
1135 && old_dcd_state != p_priv->dcd_state) {
1136 if (old_dcd_state)
1137 tty_hangup(port->tty);
1138 /* else */
1139 /* wake_up_interruptible(&p_priv->open_wait); */
1142 /* Resubmit urb so we continue receiving */
1143 urb->dev = serial->dev;
1144 err = usb_submit_urb(urb, GFP_ATOMIC);
1145 if (err != 0)
1146 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
1149 static void usa67_glocont_callback(struct urb *urb)
1151 struct usb_serial *serial;
1152 struct usb_serial_port *port;
1153 struct keyspan_port_private *p_priv;
1154 int i;
1156 dbg ("%s", __FUNCTION__);
1158 serial = urb->context;
1159 for (i = 0; i < serial->num_ports; ++i) {
1160 port = serial->port[i];
1161 p_priv = usb_get_serial_port_data(port);
1163 if (p_priv->resend_cont) {
1164 dbg ("%s - sending setup", __FUNCTION__);
1165 keyspan_usa67_send_setup(serial, port,
1166 p_priv->resend_cont - 1);
1167 break;
1172 static int keyspan_write_room (struct usb_serial_port *port)
1174 struct keyspan_port_private *p_priv;
1175 const struct keyspan_device_details *d_details;
1176 int flip;
1177 int data_len;
1178 struct urb *this_urb;
1180 dbg("%s", __FUNCTION__);
1181 p_priv = usb_get_serial_port_data(port);
1182 d_details = p_priv->device_details;
1184 if (d_details->msg_format == msg_usa90)
1185 data_len = 64;
1186 else
1187 data_len = 63;
1189 flip = p_priv->out_flip;
1191 /* Check both endpoints to see if any are available. */
1192 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1193 if (this_urb->status != -EINPROGRESS)
1194 return (data_len);
1195 flip = (flip + 1) & d_details->outdat_endp_flip;
1196 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1197 if (this_urb->status != -EINPROGRESS)
1198 return (data_len);
1200 return (0);
1204 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1206 return (0);
1210 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1212 struct keyspan_port_private *p_priv;
1213 struct keyspan_serial_private *s_priv;
1214 struct usb_serial *serial = port->serial;
1215 const struct keyspan_device_details *d_details;
1216 int i, err;
1217 int baud_rate, device_port;
1218 struct urb *urb;
1219 unsigned int cflag;
1221 s_priv = usb_get_serial_data(serial);
1222 p_priv = usb_get_serial_port_data(port);
1223 d_details = p_priv->device_details;
1225 dbg("%s - port%d.", __FUNCTION__, port->number);
1227 /* Set some sane defaults */
1228 p_priv->rts_state = 1;
1229 p_priv->dtr_state = 1;
1230 p_priv->baud = 9600;
1232 /* force baud and lcr to be set on open */
1233 p_priv->old_baud = 0;
1234 p_priv->old_cflag = 0;
1236 p_priv->out_flip = 0;
1237 p_priv->in_flip = 0;
1239 /* Reset low level data toggle and start reading from endpoints */
1240 for (i = 0; i < 2; i++) {
1241 if ((urb = p_priv->in_urbs[i]) == NULL)
1242 continue;
1243 urb->dev = serial->dev;
1245 /* make sure endpoint data toggle is synchronized with the device */
1247 usb_clear_halt(urb->dev, urb->pipe);
1249 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1250 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1254 /* Reset low level data toggle on out endpoints */
1255 for (i = 0; i < 2; i++) {
1256 if ((urb = p_priv->out_urbs[i]) == NULL)
1257 continue;
1258 urb->dev = serial->dev;
1259 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1262 /* get the terminal config for the setup message now so we don't
1263 * need to send 2 of them */
1265 cflag = port->tty->termios->c_cflag;
1266 device_port = port->number - port->serial->minor;
1268 /* Baud rate calculation takes baud rate as an integer
1269 so other rates can be generated if desired. */
1270 baud_rate = tty_get_baud_rate(port->tty);
1271 /* If no match or invalid, leave as default */
1272 if (baud_rate >= 0
1273 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1274 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1275 p_priv->baud = baud_rate;
1278 /* set CTS/RTS handshake etc. */
1279 p_priv->cflag = cflag;
1280 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1282 keyspan_send_setup(port, 1);
1283 //mdelay(100);
1284 //keyspan_set_termios(port, NULL);
1286 return (0);
1289 static inline void stop_urb(struct urb *urb)
1291 if (urb && urb->status == -EINPROGRESS)
1292 usb_kill_urb(urb);
1295 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1297 int i;
1298 struct usb_serial *serial = port->serial;
1299 struct keyspan_serial_private *s_priv;
1300 struct keyspan_port_private *p_priv;
1302 dbg("%s", __FUNCTION__);
1303 s_priv = usb_get_serial_data(serial);
1304 p_priv = usb_get_serial_port_data(port);
1306 p_priv->rts_state = 0;
1307 p_priv->dtr_state = 0;
1309 if (serial->dev) {
1310 keyspan_send_setup(port, 2);
1311 /* pilot-xfer seems to work best with this delay */
1312 mdelay(100);
1313 // keyspan_set_termios(port, NULL);
1316 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1317 dbg("%s - urb in progress", __FUNCTION__);
1320 p_priv->out_flip = 0;
1321 p_priv->in_flip = 0;
1323 if (serial->dev) {
1324 /* Stop reading/writing urbs */
1325 stop_urb(p_priv->inack_urb);
1326 /* stop_urb(p_priv->outcont_urb); */
1327 for (i = 0; i < 2; i++) {
1328 stop_urb(p_priv->in_urbs[i]);
1329 stop_urb(p_priv->out_urbs[i]);
1332 port->tty = NULL;
1336 /* download the firmware to a pre-renumeration device */
1337 static int keyspan_fake_startup (struct usb_serial *serial)
1339 int response;
1340 const struct ezusb_hex_record *record;
1341 char *fw_name;
1343 dbg("Keyspan startup version %04x product %04x",
1344 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1345 le16_to_cpu(serial->dev->descriptor.idProduct));
1347 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1348 dbg("Firmware already loaded. Quitting.");
1349 return(1);
1352 /* Select firmware image on the basis of idProduct */
1353 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1354 case keyspan_usa28_pre_product_id:
1355 record = &keyspan_usa28_firmware[0];
1356 fw_name = "USA28";
1357 break;
1359 case keyspan_usa28x_pre_product_id:
1360 record = &keyspan_usa28x_firmware[0];
1361 fw_name = "USA28X";
1362 break;
1364 case keyspan_usa28xa_pre_product_id:
1365 record = &keyspan_usa28xa_firmware[0];
1366 fw_name = "USA28XA";
1367 break;
1369 case keyspan_usa28xb_pre_product_id:
1370 record = &keyspan_usa28xb_firmware[0];
1371 fw_name = "USA28XB";
1372 break;
1374 case keyspan_usa19_pre_product_id:
1375 record = &keyspan_usa19_firmware[0];
1376 fw_name = "USA19";
1377 break;
1379 case keyspan_usa19qi_pre_product_id:
1380 record = &keyspan_usa19qi_firmware[0];
1381 fw_name = "USA19QI";
1382 break;
1384 case keyspan_mpr_pre_product_id:
1385 record = &keyspan_mpr_firmware[0];
1386 fw_name = "MPR";
1387 break;
1389 case keyspan_usa19qw_pre_product_id:
1390 record = &keyspan_usa19qw_firmware[0];
1391 fw_name = "USA19QI";
1392 break;
1394 case keyspan_usa18x_pre_product_id:
1395 record = &keyspan_usa18x_firmware[0];
1396 fw_name = "USA18X";
1397 break;
1399 case keyspan_usa19w_pre_product_id:
1400 record = &keyspan_usa19w_firmware[0];
1401 fw_name = "USA19W";
1402 break;
1404 case keyspan_usa49w_pre_product_id:
1405 record = &keyspan_usa49w_firmware[0];
1406 fw_name = "USA49W";
1407 break;
1409 case keyspan_usa49wlc_pre_product_id:
1410 record = &keyspan_usa49wlc_firmware[0];
1411 fw_name = "USA49WLC";
1412 break;
1414 default:
1415 record = NULL;
1416 fw_name = "Unknown";
1417 break;
1420 if (record == NULL) {
1421 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1422 return(1);
1425 dbg("Uploading Keyspan %s firmware.", fw_name);
1427 /* download the firmware image */
1428 response = ezusb_set_reset(serial, 1);
1430 while(record->address != 0xffff) {
1431 response = ezusb_writememory(serial, record->address,
1432 (unsigned char *)record->data,
1433 record->data_size, 0xa0);
1434 if (response < 0) {
1435 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1436 "firmware (%d %04X %p %d)\n",
1437 response,
1438 record->address, record->data, record->data_size);
1439 break;
1441 record++;
1443 /* bring device out of reset. Renumeration will occur in a
1444 moment and the new device will bind to the real driver */
1445 response = ezusb_set_reset(serial, 0);
1447 /* we don't want this device to have a driver assigned to it. */
1448 return (1);
1451 /* Helper functions used by keyspan_setup_urbs */
1452 static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1453 int endpoint)
1455 struct usb_host_interface *iface_desc;
1456 struct usb_endpoint_descriptor *ep;
1457 int i;
1459 iface_desc = serial->interface->cur_altsetting;
1460 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1461 ep = &iface_desc->endpoint[i].desc;
1462 if (ep->bEndpointAddress == endpoint)
1463 return ep;
1465 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1466 "endpoint %x\n", endpoint);
1467 return NULL;
1470 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1471 int dir, void *ctx, char *buf, int len,
1472 void (*callback)(struct urb *))
1474 struct urb *urb;
1475 struct usb_endpoint_descriptor const *ep_desc;
1476 char const *ep_type_name;
1478 if (endpoint == -1)
1479 return NULL; /* endpoint not needed */
1481 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1482 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1483 if (urb == NULL) {
1484 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1485 return NULL;
1488 if (endpoint == 0) {
1489 /* control EP filled in when used */
1490 return urb;
1493 ep_desc = find_ep(serial, endpoint);
1494 if (!ep_desc) {
1495 /* leak the urb, something's wrong and the callers don't care */
1496 return urb;
1498 if (usb_endpoint_xfer_int(ep_desc)) {
1499 ep_type_name = "INT";
1500 usb_fill_int_urb(urb, serial->dev,
1501 usb_sndintpipe(serial->dev, endpoint) | dir,
1502 buf, len, callback, ctx,
1503 ep_desc->bInterval);
1504 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1505 ep_type_name = "BULK";
1506 usb_fill_bulk_urb(urb, serial->dev,
1507 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1508 buf, len, callback, ctx);
1509 } else {
1510 dev_warn(&serial->interface->dev,
1511 "unsupported endpoint type %x\n",
1512 ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
1513 usb_free_urb(urb);
1514 return NULL;
1517 dbg("%s - using urb %p for %s endpoint %x",
1518 __func__, urb, ep_type_name, endpoint);
1519 return urb;
1522 static struct callbacks {
1523 void (*instat_callback)(struct urb *);
1524 void (*glocont_callback)(struct urb *);
1525 void (*indat_callback)(struct urb *);
1526 void (*outdat_callback)(struct urb *);
1527 void (*inack_callback)(struct urb *);
1528 void (*outcont_callback)(struct urb *);
1529 } keyspan_callbacks[] = {
1531 /* msg_usa26 callbacks */
1532 .instat_callback = usa26_instat_callback,
1533 .glocont_callback = usa26_glocont_callback,
1534 .indat_callback = usa26_indat_callback,
1535 .outdat_callback = usa2x_outdat_callback,
1536 .inack_callback = usa26_inack_callback,
1537 .outcont_callback = usa26_outcont_callback,
1538 }, {
1539 /* msg_usa28 callbacks */
1540 .instat_callback = usa28_instat_callback,
1541 .glocont_callback = usa28_glocont_callback,
1542 .indat_callback = usa28_indat_callback,
1543 .outdat_callback = usa2x_outdat_callback,
1544 .inack_callback = usa28_inack_callback,
1545 .outcont_callback = usa28_outcont_callback,
1546 }, {
1547 /* msg_usa49 callbacks */
1548 .instat_callback = usa49_instat_callback,
1549 .glocont_callback = usa49_glocont_callback,
1550 .indat_callback = usa49_indat_callback,
1551 .outdat_callback = usa2x_outdat_callback,
1552 .inack_callback = usa49_inack_callback,
1553 .outcont_callback = usa49_outcont_callback,
1554 }, {
1555 /* msg_usa90 callbacks */
1556 .instat_callback = usa90_instat_callback,
1557 .glocont_callback = usa28_glocont_callback,
1558 .indat_callback = usa90_indat_callback,
1559 .outdat_callback = usa2x_outdat_callback,
1560 .inack_callback = usa28_inack_callback,
1561 .outcont_callback = usa90_outcont_callback,
1562 }, {
1563 /* msg_usa67 callbacks */
1564 .instat_callback = usa67_instat_callback,
1565 .glocont_callback = usa67_glocont_callback,
1566 .indat_callback = usa26_indat_callback,
1567 .outdat_callback = usa2x_outdat_callback,
1568 .inack_callback = usa26_inack_callback,
1569 .outcont_callback = usa26_outcont_callback,
1573 /* Generic setup urbs function that uses
1574 data in device_details */
1575 static void keyspan_setup_urbs(struct usb_serial *serial)
1577 int i, j;
1578 struct keyspan_serial_private *s_priv;
1579 const struct keyspan_device_details *d_details;
1580 struct usb_serial_port *port;
1581 struct keyspan_port_private *p_priv;
1582 struct callbacks *cback;
1583 int endp;
1585 dbg ("%s", __FUNCTION__);
1587 s_priv = usb_get_serial_data(serial);
1588 d_details = s_priv->device_details;
1590 /* Setup values for the various callback routines */
1591 cback = &keyspan_callbacks[d_details->msg_format];
1593 /* Allocate and set up urbs for each one that is in use,
1594 starting with instat endpoints */
1595 s_priv->instat_urb = keyspan_setup_urb
1596 (serial, d_details->instat_endpoint, USB_DIR_IN,
1597 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1598 cback->instat_callback);
1600 s_priv->indat_urb = keyspan_setup_urb
1601 (serial, d_details->indat_endpoint, USB_DIR_IN,
1602 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1603 usa49wg_indat_callback);
1605 s_priv->glocont_urb = keyspan_setup_urb
1606 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1607 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1608 cback->glocont_callback);
1610 /* Setup endpoints for each port specific thing */
1611 for (i = 0; i < d_details->num_ports; i ++) {
1612 port = serial->port[i];
1613 p_priv = usb_get_serial_port_data(port);
1615 /* Do indat endpoints first, once for each flip */
1616 endp = d_details->indat_endpoints[i];
1617 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1618 p_priv->in_urbs[j] = keyspan_setup_urb
1619 (serial, endp, USB_DIR_IN, port,
1620 p_priv->in_buffer[j], 64,
1621 cback->indat_callback);
1623 for (; j < 2; ++j)
1624 p_priv->in_urbs[j] = NULL;
1626 /* outdat endpoints also have flip */
1627 endp = d_details->outdat_endpoints[i];
1628 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1629 p_priv->out_urbs[j] = keyspan_setup_urb
1630 (serial, endp, USB_DIR_OUT, port,
1631 p_priv->out_buffer[j], 64,
1632 cback->outdat_callback);
1634 for (; j < 2; ++j)
1635 p_priv->out_urbs[j] = NULL;
1637 /* inack endpoint */
1638 p_priv->inack_urb = keyspan_setup_urb
1639 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1640 port, p_priv->inack_buffer, 1, cback->inack_callback);
1642 /* outcont endpoint */
1643 p_priv->outcont_urb = keyspan_setup_urb
1644 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1645 port, p_priv->outcont_buffer, 64,
1646 cback->outcont_callback);
1651 /* usa19 function doesn't require prescaler */
1652 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1653 u8 *rate_low, u8 *prescaler, int portnum)
1655 u32 b16, /* baud rate times 16 (actual rate used internally) */
1656 div, /* divisor */
1657 cnt; /* inverse of divisor (programmed into 8051) */
1659 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1661 /* prevent divide by zero... */
1662 if( (b16 = (baud_rate * 16L)) == 0) {
1663 return (KEYSPAN_INVALID_BAUD_RATE);
1666 /* Any "standard" rate over 57k6 is marginal on the USA-19
1667 as we run out of divisor resolution. */
1668 if (baud_rate > 57600) {
1669 return (KEYSPAN_INVALID_BAUD_RATE);
1672 /* calculate the divisor and the counter (its inverse) */
1673 if( (div = (baudclk / b16)) == 0) {
1674 return (KEYSPAN_INVALID_BAUD_RATE);
1676 else {
1677 cnt = 0 - div;
1680 if(div > 0xffff) {
1681 return (KEYSPAN_INVALID_BAUD_RATE);
1684 /* return the counter values if non-null */
1685 if (rate_low) {
1686 *rate_low = (u8) (cnt & 0xff);
1688 if (rate_hi) {
1689 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1691 if (rate_low && rate_hi) {
1692 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1695 return (KEYSPAN_BAUD_RATE_OK);
1698 /* usa19hs function doesn't require prescaler */
1699 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1700 u8 *rate_low, u8 *prescaler, int portnum)
1702 u32 b16, /* baud rate times 16 (actual rate used internally) */
1703 div; /* divisor */
1705 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1707 /* prevent divide by zero... */
1708 if( (b16 = (baud_rate * 16L)) == 0)
1709 return (KEYSPAN_INVALID_BAUD_RATE);
1713 /* calculate the divisor */
1714 if( (div = (baudclk / b16)) == 0)
1715 return (KEYSPAN_INVALID_BAUD_RATE);
1717 if(div > 0xffff)
1718 return (KEYSPAN_INVALID_BAUD_RATE);
1720 /* return the counter values if non-null */
1721 if (rate_low)
1722 *rate_low = (u8) (div & 0xff);
1724 if (rate_hi)
1725 *rate_hi = (u8) ((div >> 8) & 0xff);
1727 if (rate_low && rate_hi)
1728 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1730 return (KEYSPAN_BAUD_RATE_OK);
1733 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1734 u8 *rate_low, u8 *prescaler, int portnum)
1736 u32 b16, /* baud rate times 16 (actual rate used internally) */
1737 clk, /* clock with 13/8 prescaler */
1738 div, /* divisor using 13/8 prescaler */
1739 res, /* resulting baud rate using 13/8 prescaler */
1740 diff, /* error using 13/8 prescaler */
1741 smallest_diff;
1742 u8 best_prescaler;
1743 int i;
1745 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1747 /* prevent divide by zero */
1748 if( (b16 = baud_rate * 16L) == 0) {
1749 return (KEYSPAN_INVALID_BAUD_RATE);
1752 /* Calculate prescaler by trying them all and looking
1753 for best fit */
1755 /* start with largest possible difference */
1756 smallest_diff = 0xffffffff;
1758 /* 0 is an invalid prescaler, used as a flag */
1759 best_prescaler = 0;
1761 for(i = 8; i <= 0xff; ++i) {
1762 clk = (baudclk * 8) / (u32) i;
1764 if( (div = clk / b16) == 0) {
1765 continue;
1768 res = clk / div;
1769 diff= (res > b16) ? (res-b16) : (b16-res);
1771 if(diff < smallest_diff) {
1772 best_prescaler = i;
1773 smallest_diff = diff;
1777 if(best_prescaler == 0) {
1778 return (KEYSPAN_INVALID_BAUD_RATE);
1781 clk = (baudclk * 8) / (u32) best_prescaler;
1782 div = clk / b16;
1784 /* return the divisor and prescaler if non-null */
1785 if (rate_low) {
1786 *rate_low = (u8) (div & 0xff);
1788 if (rate_hi) {
1789 *rate_hi = (u8) ((div >> 8) & 0xff);
1791 if (prescaler) {
1792 *prescaler = best_prescaler;
1793 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1795 return (KEYSPAN_BAUD_RATE_OK);
1798 /* USA-28 supports different maximum baud rates on each port */
1799 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1800 u8 *rate_low, u8 *prescaler, int portnum)
1802 u32 b16, /* baud rate times 16 (actual rate used internally) */
1803 div, /* divisor */
1804 cnt; /* inverse of divisor (programmed into 8051) */
1806 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1808 /* prevent divide by zero */
1809 if ((b16 = baud_rate * 16L) == 0)
1810 return (KEYSPAN_INVALID_BAUD_RATE);
1812 /* calculate the divisor and the counter (its inverse) */
1813 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1814 return (KEYSPAN_INVALID_BAUD_RATE);
1816 else {
1817 cnt = 0 - div;
1820 /* check for out of range, based on portnum,
1821 and return result */
1822 if(portnum == 0) {
1823 if(div > 0xffff)
1824 return (KEYSPAN_INVALID_BAUD_RATE);
1826 else {
1827 if(portnum == 1) {
1828 if(div > 0xff) {
1829 return (KEYSPAN_INVALID_BAUD_RATE);
1832 else {
1833 return (KEYSPAN_INVALID_BAUD_RATE);
1837 /* return the counter values if not NULL
1838 (port 1 will ignore retHi) */
1839 if (rate_low) {
1840 *rate_low = (u8) (cnt & 0xff);
1842 if (rate_hi) {
1843 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1845 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1846 return (KEYSPAN_BAUD_RATE_OK);
1849 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1850 struct usb_serial_port *port,
1851 int reset_port)
1853 struct keyspan_usa26_portControlMessage msg;
1854 struct keyspan_serial_private *s_priv;
1855 struct keyspan_port_private *p_priv;
1856 const struct keyspan_device_details *d_details;
1857 int outcont_urb;
1858 struct urb *this_urb;
1859 int device_port, err;
1861 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1863 s_priv = usb_get_serial_data(serial);
1864 p_priv = usb_get_serial_port_data(port);
1865 d_details = s_priv->device_details;
1866 device_port = port->number - port->serial->minor;
1868 outcont_urb = d_details->outcont_endpoints[port->number];
1869 this_urb = p_priv->outcont_urb;
1871 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1873 /* Make sure we have an urb then send the message */
1874 if (this_urb == NULL) {
1875 dbg("%s - oops no urb.", __FUNCTION__);
1876 return -1;
1879 /* Save reset port val for resend.
1880 Don't overwrite resend for open/close condition. */
1881 if ((reset_port + 1) > p_priv->resend_cont)
1882 p_priv->resend_cont = reset_port + 1;
1883 if (this_urb->status == -EINPROGRESS) {
1884 /* dbg ("%s - already writing", __FUNCTION__); */
1885 mdelay(5);
1886 return(-1);
1889 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1891 /* Only set baud rate if it's changed */
1892 if (p_priv->old_baud != p_priv->baud) {
1893 p_priv->old_baud = p_priv->baud;
1894 msg.setClocking = 0xff;
1895 if (d_details->calculate_baud_rate
1896 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1897 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1898 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1899 p_priv->baud);
1900 msg.baudLo = 0;
1901 msg.baudHi = 125; /* Values for 9600 baud */
1902 msg.prescaler = 10;
1904 msg.setPrescaler = 0xff;
1907 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1908 switch (p_priv->cflag & CSIZE) {
1909 case CS5:
1910 msg.lcr |= USA_DATABITS_5;
1911 break;
1912 case CS6:
1913 msg.lcr |= USA_DATABITS_6;
1914 break;
1915 case CS7:
1916 msg.lcr |= USA_DATABITS_7;
1917 break;
1918 case CS8:
1919 msg.lcr |= USA_DATABITS_8;
1920 break;
1922 if (p_priv->cflag & PARENB) {
1923 /* note USA_PARITY_NONE == 0 */
1924 msg.lcr |= (p_priv->cflag & PARODD)?
1925 USA_PARITY_ODD: USA_PARITY_EVEN;
1927 msg.setLcr = 0xff;
1929 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1930 msg.xonFlowControl = 0;
1931 msg.setFlowControl = 0xff;
1932 msg.forwardingLength = 16;
1933 msg.xonChar = 17;
1934 msg.xoffChar = 19;
1936 /* Opening port */
1937 if (reset_port == 1) {
1938 msg._txOn = 1;
1939 msg._txOff = 0;
1940 msg.txFlush = 0;
1941 msg.txBreak = 0;
1942 msg.rxOn = 1;
1943 msg.rxOff = 0;
1944 msg.rxFlush = 1;
1945 msg.rxForward = 0;
1946 msg.returnStatus = 0;
1947 msg.resetDataToggle = 0xff;
1950 /* Closing port */
1951 else if (reset_port == 2) {
1952 msg._txOn = 0;
1953 msg._txOff = 1;
1954 msg.txFlush = 0;
1955 msg.txBreak = 0;
1956 msg.rxOn = 0;
1957 msg.rxOff = 1;
1958 msg.rxFlush = 1;
1959 msg.rxForward = 0;
1960 msg.returnStatus = 0;
1961 msg.resetDataToggle = 0;
1964 /* Sending intermediate configs */
1965 else {
1966 msg._txOn = (! p_priv->break_on);
1967 msg._txOff = 0;
1968 msg.txFlush = 0;
1969 msg.txBreak = (p_priv->break_on);
1970 msg.rxOn = 0;
1971 msg.rxOff = 0;
1972 msg.rxFlush = 0;
1973 msg.rxForward = 0;
1974 msg.returnStatus = 0;
1975 msg.resetDataToggle = 0x0;
1978 /* Do handshaking outputs */
1979 msg.setTxTriState_setRts = 0xff;
1980 msg.txTriState_rts = p_priv->rts_state;
1982 msg.setHskoa_setDtr = 0xff;
1983 msg.hskoa_dtr = p_priv->dtr_state;
1985 p_priv->resend_cont = 0;
1986 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1988 /* send the data out the device on control endpoint */
1989 this_urb->transfer_buffer_length = sizeof(msg);
1991 this_urb->dev = serial->dev;
1992 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1993 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1995 #if 0
1996 else {
1997 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1998 outcont_urb, this_urb->transfer_buffer_length,
1999 usb_pipeendpoint(this_urb->pipe));
2001 #endif
2003 return (0);
2006 static int keyspan_usa28_send_setup(struct usb_serial *serial,
2007 struct usb_serial_port *port,
2008 int reset_port)
2010 struct keyspan_usa28_portControlMessage msg;
2011 struct keyspan_serial_private *s_priv;
2012 struct keyspan_port_private *p_priv;
2013 const struct keyspan_device_details *d_details;
2014 struct urb *this_urb;
2015 int device_port, err;
2017 dbg ("%s", __FUNCTION__);
2019 s_priv = usb_get_serial_data(serial);
2020 p_priv = usb_get_serial_port_data(port);
2021 d_details = s_priv->device_details;
2022 device_port = port->number - port->serial->minor;
2024 /* only do something if we have a bulk out endpoint */
2025 if ((this_urb = p_priv->outcont_urb) == NULL) {
2026 dbg("%s - oops no urb.", __FUNCTION__);
2027 return -1;
2030 /* Save reset port val for resend.
2031 Don't overwrite resend for open/close condition. */
2032 if ((reset_port + 1) > p_priv->resend_cont)
2033 p_priv->resend_cont = reset_port + 1;
2034 if (this_urb->status == -EINPROGRESS) {
2035 dbg ("%s already writing", __FUNCTION__);
2036 mdelay(5);
2037 return(-1);
2040 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
2042 msg.setBaudRate = 1;
2043 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2044 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2045 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
2046 msg.baudLo = 0xff;
2047 msg.baudHi = 0xb2; /* Values for 9600 baud */
2050 /* If parity is enabled, we must calculate it ourselves. */
2051 msg.parity = 0; /* XXX for now */
2053 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2054 msg.xonFlowControl = 0;
2056 /* Do handshaking outputs, DTR is inverted relative to RTS */
2057 msg.rts = p_priv->rts_state;
2058 msg.dtr = p_priv->dtr_state;
2060 msg.forwardingLength = 16;
2061 msg.forwardMs = 10;
2062 msg.breakThreshold = 45;
2063 msg.xonChar = 17;
2064 msg.xoffChar = 19;
2066 /*msg.returnStatus = 1;
2067 msg.resetDataToggle = 0xff;*/
2068 /* Opening port */
2069 if (reset_port == 1) {
2070 msg._txOn = 1;
2071 msg._txOff = 0;
2072 msg.txFlush = 0;
2073 msg.txForceXoff = 0;
2074 msg.txBreak = 0;
2075 msg.rxOn = 1;
2076 msg.rxOff = 0;
2077 msg.rxFlush = 1;
2078 msg.rxForward = 0;
2079 msg.returnStatus = 0;
2080 msg.resetDataToggle = 0xff;
2082 /* Closing port */
2083 else if (reset_port == 2) {
2084 msg._txOn = 0;
2085 msg._txOff = 1;
2086 msg.txFlush = 0;
2087 msg.txForceXoff = 0;
2088 msg.txBreak = 0;
2089 msg.rxOn = 0;
2090 msg.rxOff = 1;
2091 msg.rxFlush = 1;
2092 msg.rxForward = 0;
2093 msg.returnStatus = 0;
2094 msg.resetDataToggle = 0;
2096 /* Sending intermediate configs */
2097 else {
2098 msg._txOn = (! p_priv->break_on);
2099 msg._txOff = 0;
2100 msg.txFlush = 0;
2101 msg.txForceXoff = 0;
2102 msg.txBreak = (p_priv->break_on);
2103 msg.rxOn = 0;
2104 msg.rxOff = 0;
2105 msg.rxFlush = 0;
2106 msg.rxForward = 0;
2107 msg.returnStatus = 0;
2108 msg.resetDataToggle = 0x0;
2111 p_priv->resend_cont = 0;
2112 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2114 /* send the data out the device on control endpoint */
2115 this_urb->transfer_buffer_length = sizeof(msg);
2117 this_urb->dev = serial->dev;
2118 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2119 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
2121 #if 0
2122 else {
2123 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
2124 this_urb->transfer_buffer_length);
2126 #endif
2128 return (0);
2131 static int keyspan_usa49_send_setup(struct usb_serial *serial,
2132 struct usb_serial_port *port,
2133 int reset_port)
2135 struct keyspan_usa49_portControlMessage msg;
2136 struct usb_ctrlrequest *dr = NULL;
2137 struct keyspan_serial_private *s_priv;
2138 struct keyspan_port_private *p_priv;
2139 const struct keyspan_device_details *d_details;
2140 struct urb *this_urb;
2141 int err, device_port;
2143 dbg ("%s", __FUNCTION__);
2145 s_priv = usb_get_serial_data(serial);
2146 p_priv = usb_get_serial_port_data(port);
2147 d_details = s_priv->device_details;
2149 this_urb = s_priv->glocont_urb;
2151 /* Work out which port within the device is being setup */
2152 device_port = port->number - port->serial->minor;
2154 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
2156 /* Make sure we have an urb then send the message */
2157 if (this_urb == NULL) {
2158 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
2159 return -1;
2162 /* Save reset port val for resend.
2163 Don't overwrite resend for open/close condition. */
2164 if ((reset_port + 1) > p_priv->resend_cont)
2165 p_priv->resend_cont = reset_port + 1;
2167 if (this_urb->status == -EINPROGRESS) {
2168 /* dbg ("%s - already writing", __FUNCTION__); */
2169 mdelay(5);
2170 return(-1);
2173 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
2175 /*msg.portNumber = port->number;*/
2176 msg.portNumber = device_port;
2178 /* Only set baud rate if it's changed */
2179 if (p_priv->old_baud != p_priv->baud) {
2180 p_priv->old_baud = p_priv->baud;
2181 msg.setClocking = 0xff;
2182 if (d_details->calculate_baud_rate
2183 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2184 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2185 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2186 p_priv->baud);
2187 msg.baudLo = 0;
2188 msg.baudHi = 125; /* Values for 9600 baud */
2189 msg.prescaler = 10;
2191 //msg.setPrescaler = 0xff;
2194 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2195 switch (p_priv->cflag & CSIZE) {
2196 case CS5:
2197 msg.lcr |= USA_DATABITS_5;
2198 break;
2199 case CS6:
2200 msg.lcr |= USA_DATABITS_6;
2201 break;
2202 case CS7:
2203 msg.lcr |= USA_DATABITS_7;
2204 break;
2205 case CS8:
2206 msg.lcr |= USA_DATABITS_8;
2207 break;
2209 if (p_priv->cflag & PARENB) {
2210 /* note USA_PARITY_NONE == 0 */
2211 msg.lcr |= (p_priv->cflag & PARODD)?
2212 USA_PARITY_ODD: USA_PARITY_EVEN;
2214 msg.setLcr = 0xff;
2216 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2217 msg.xonFlowControl = 0;
2218 msg.setFlowControl = 0xff;
2220 msg.forwardingLength = 16;
2221 msg.xonChar = 17;
2222 msg.xoffChar = 19;
2224 /* Opening port */
2225 if (reset_port == 1) {
2226 msg._txOn = 1;
2227 msg._txOff = 0;
2228 msg.txFlush = 0;
2229 msg.txBreak = 0;
2230 msg.rxOn = 1;
2231 msg.rxOff = 0;
2232 msg.rxFlush = 1;
2233 msg.rxForward = 0;
2234 msg.returnStatus = 0;
2235 msg.resetDataToggle = 0xff;
2236 msg.enablePort = 1;
2237 msg.disablePort = 0;
2239 /* Closing port */
2240 else if (reset_port == 2) {
2241 msg._txOn = 0;
2242 msg._txOff = 1;
2243 msg.txFlush = 0;
2244 msg.txBreak = 0;
2245 msg.rxOn = 0;
2246 msg.rxOff = 1;
2247 msg.rxFlush = 1;
2248 msg.rxForward = 0;
2249 msg.returnStatus = 0;
2250 msg.resetDataToggle = 0;
2251 msg.enablePort = 0;
2252 msg.disablePort = 1;
2254 /* Sending intermediate configs */
2255 else {
2256 msg._txOn = (! p_priv->break_on);
2257 msg._txOff = 0;
2258 msg.txFlush = 0;
2259 msg.txBreak = (p_priv->break_on);
2260 msg.rxOn = 0;
2261 msg.rxOff = 0;
2262 msg.rxFlush = 0;
2263 msg.rxForward = 0;
2264 msg.returnStatus = 0;
2265 msg.resetDataToggle = 0x0;
2266 msg.enablePort = 0;
2267 msg.disablePort = 0;
2270 /* Do handshaking outputs */
2271 msg.setRts = 0xff;
2272 msg.rts = p_priv->rts_state;
2274 msg.setDtr = 0xff;
2275 msg.dtr = p_priv->dtr_state;
2277 p_priv->resend_cont = 0;
2279 /* if the device is a 49wg, we send control message on usb control EP 0 */
2281 if (d_details->product_id == keyspan_usa49wg_product_id) {
2282 dr = (void *)(s_priv->ctrl_buf);
2283 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2284 dr->bRequest = 0xB0; /* 49wg control message */;
2285 dr->wValue = 0;
2286 dr->wIndex = 0;
2287 dr->wLength = cpu_to_le16(sizeof(msg));
2289 memcpy (s_priv->glocont_buf, &msg, sizeof(msg));
2291 usb_fill_control_urb(this_urb, serial->dev, usb_sndctrlpipe(serial->dev, 0),
2292 (unsigned char *)dr, s_priv->glocont_buf, sizeof(msg),
2293 usa49_glocont_callback, serial);
2295 } else {
2296 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2298 /* send the data out the device on control endpoint */
2299 this_urb->transfer_buffer_length = sizeof(msg);
2301 this_urb->dev = serial->dev;
2303 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2304 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2306 #if 0
2307 else {
2308 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2309 outcont_urb, this_urb->transfer_buffer_length,
2310 usb_pipeendpoint(this_urb->pipe));
2312 #endif
2314 return (0);
2317 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2318 struct usb_serial_port *port,
2319 int reset_port)
2321 struct keyspan_usa90_portControlMessage msg;
2322 struct keyspan_serial_private *s_priv;
2323 struct keyspan_port_private *p_priv;
2324 const struct keyspan_device_details *d_details;
2325 struct urb *this_urb;
2326 int err;
2327 u8 prescaler;
2329 dbg ("%s", __FUNCTION__);
2331 s_priv = usb_get_serial_data(serial);
2332 p_priv = usb_get_serial_port_data(port);
2333 d_details = s_priv->device_details;
2335 /* only do something if we have a bulk out endpoint */
2336 if ((this_urb = p_priv->outcont_urb) == NULL) {
2337 dbg("%s - oops no urb.", __FUNCTION__);
2338 return -1;
2341 /* Save reset port val for resend.
2342 Don't overwrite resend for open/close condition. */
2343 if ((reset_port + 1) > p_priv->resend_cont)
2344 p_priv->resend_cont = reset_port + 1;
2345 if (this_urb->status == -EINPROGRESS) {
2346 dbg ("%s already writing", __FUNCTION__);
2347 mdelay(5);
2348 return(-1);
2351 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2353 /* Only set baud rate if it's changed */
2354 if (p_priv->old_baud != p_priv->baud) {
2355 p_priv->old_baud = p_priv->baud;
2356 msg.setClocking = 0x01;
2357 if (d_details->calculate_baud_rate
2358 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2359 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2360 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2361 p_priv->baud);
2362 p_priv->baud = 9600;
2363 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2364 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2366 msg.setRxMode = 1;
2367 msg.setTxMode = 1;
2370 /* modes must always be correctly specified */
2371 if (p_priv->baud > 57600)
2373 msg.rxMode = RXMODE_DMA;
2374 msg.txMode = TXMODE_DMA;
2376 else
2378 msg.rxMode = RXMODE_BYHAND;
2379 msg.txMode = TXMODE_BYHAND;
2382 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2383 switch (p_priv->cflag & CSIZE) {
2384 case CS5:
2385 msg.lcr |= USA_DATABITS_5;
2386 break;
2387 case CS6:
2388 msg.lcr |= USA_DATABITS_6;
2389 break;
2390 case CS7:
2391 msg.lcr |= USA_DATABITS_7;
2392 break;
2393 case CS8:
2394 msg.lcr |= USA_DATABITS_8;
2395 break;
2397 if (p_priv->cflag & PARENB) {
2398 /* note USA_PARITY_NONE == 0 */
2399 msg.lcr |= (p_priv->cflag & PARODD)?
2400 USA_PARITY_ODD: USA_PARITY_EVEN;
2402 if (p_priv->old_cflag != p_priv->cflag) {
2403 p_priv->old_cflag = p_priv->cflag;
2404 msg.setLcr = 0x01;
2407 if (p_priv->flow_control == flow_cts)
2408 msg.txFlowControl = TXFLOW_CTS;
2409 msg.setTxFlowControl = 0x01;
2410 msg.setRxFlowControl = 0x01;
2412 msg.rxForwardingLength = 16;
2413 msg.rxForwardingTimeout = 16;
2414 msg.txAckSetting = 0;
2415 msg.xonChar = 17;
2416 msg.xoffChar = 19;
2418 /* Opening port */
2419 if (reset_port == 1) {
2420 msg.portEnabled = 1;
2421 msg.rxFlush = 1;
2422 msg.txBreak = (p_priv->break_on);
2424 /* Closing port */
2425 else if (reset_port == 2) {
2426 msg.portEnabled = 0;
2428 /* Sending intermediate configs */
2429 else {
2430 if (port->open_count)
2431 msg.portEnabled = 1;
2432 msg.txBreak = (p_priv->break_on);
2435 /* Do handshaking outputs */
2436 msg.setRts = 0x01;
2437 msg.rts = p_priv->rts_state;
2439 msg.setDtr = 0x01;
2440 msg.dtr = p_priv->dtr_state;
2442 p_priv->resend_cont = 0;
2443 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2445 /* send the data out the device on control endpoint */
2446 this_urb->transfer_buffer_length = sizeof(msg);
2448 this_urb->dev = serial->dev;
2449 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2450 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2452 return (0);
2455 static int keyspan_usa67_send_setup(struct usb_serial *serial,
2456 struct usb_serial_port *port,
2457 int reset_port)
2459 struct keyspan_usa67_portControlMessage msg;
2460 struct keyspan_serial_private *s_priv;
2461 struct keyspan_port_private *p_priv;
2462 const struct keyspan_device_details *d_details;
2463 struct urb *this_urb;
2464 int err, device_port;
2466 dbg ("%s", __FUNCTION__);
2468 s_priv = usb_get_serial_data(serial);
2469 p_priv = usb_get_serial_port_data(port);
2470 d_details = s_priv->device_details;
2472 this_urb = s_priv->glocont_urb;
2474 /* Work out which port within the device is being setup */
2475 device_port = port->number - port->serial->minor;
2477 /* Make sure we have an urb then send the message */
2478 if (this_urb == NULL) {
2479 dbg("%s - oops no urb for port %d.", __FUNCTION__,
2480 port->number);
2481 return -1;
2484 /* Save reset port val for resend.
2485 Don't overwrite resend for open/close condition. */
2486 if ((reset_port + 1) > p_priv->resend_cont)
2487 p_priv->resend_cont = reset_port + 1;
2488 if (this_urb->status == -EINPROGRESS) {
2489 /* dbg ("%s - already writing", __FUNCTION__); */
2490 mdelay(5);
2491 return(-1);
2494 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2496 msg.port = device_port;
2498 /* Only set baud rate if it's changed */
2499 if (p_priv->old_baud != p_priv->baud) {
2500 p_priv->old_baud = p_priv->baud;
2501 msg.setClocking = 0xff;
2502 if (d_details->calculate_baud_rate
2503 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2504 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
2505 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2506 p_priv->baud);
2507 msg.baudLo = 0;
2508 msg.baudHi = 125; /* Values for 9600 baud */
2509 msg.prescaler = 10;
2511 msg.setPrescaler = 0xff;
2514 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2515 switch (p_priv->cflag & CSIZE) {
2516 case CS5:
2517 msg.lcr |= USA_DATABITS_5;
2518 break;
2519 case CS6:
2520 msg.lcr |= USA_DATABITS_6;
2521 break;
2522 case CS7:
2523 msg.lcr |= USA_DATABITS_7;
2524 break;
2525 case CS8:
2526 msg.lcr |= USA_DATABITS_8;
2527 break;
2529 if (p_priv->cflag & PARENB) {
2530 /* note USA_PARITY_NONE == 0 */
2531 msg.lcr |= (p_priv->cflag & PARODD)?
2532 USA_PARITY_ODD: USA_PARITY_EVEN;
2534 msg.setLcr = 0xff;
2536 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2537 msg.xonFlowControl = 0;
2538 msg.setFlowControl = 0xff;
2539 msg.forwardingLength = 16;
2540 msg.xonChar = 17;
2541 msg.xoffChar = 19;
2543 if (reset_port == 1) {
2544 /* Opening port */
2545 msg._txOn = 1;
2546 msg._txOff = 0;
2547 msg.txFlush = 0;
2548 msg.txBreak = 0;
2549 msg.rxOn = 1;
2550 msg.rxOff = 0;
2551 msg.rxFlush = 1;
2552 msg.rxForward = 0;
2553 msg.returnStatus = 0;
2554 msg.resetDataToggle = 0xff;
2555 } else if (reset_port == 2) {
2556 /* Closing port */
2557 msg._txOn = 0;
2558 msg._txOff = 1;
2559 msg.txFlush = 0;
2560 msg.txBreak = 0;
2561 msg.rxOn = 0;
2562 msg.rxOff = 1;
2563 msg.rxFlush = 1;
2564 msg.rxForward = 0;
2565 msg.returnStatus = 0;
2566 msg.resetDataToggle = 0;
2567 } else {
2568 /* Sending intermediate configs */
2569 msg._txOn = (! p_priv->break_on);
2570 msg._txOff = 0;
2571 msg.txFlush = 0;
2572 msg.txBreak = (p_priv->break_on);
2573 msg.rxOn = 0;
2574 msg.rxOff = 0;
2575 msg.rxFlush = 0;
2576 msg.rxForward = 0;
2577 msg.returnStatus = 0;
2578 msg.resetDataToggle = 0x0;
2581 /* Do handshaking outputs */
2582 msg.setTxTriState_setRts = 0xff;
2583 msg.txTriState_rts = p_priv->rts_state;
2585 msg.setHskoa_setDtr = 0xff;
2586 msg.hskoa_dtr = p_priv->dtr_state;
2588 p_priv->resend_cont = 0;
2590 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2592 /* send the data out the device on control endpoint */
2593 this_urb->transfer_buffer_length = sizeof(msg);
2594 this_urb->dev = serial->dev;
2596 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2597 if (err != 0)
2598 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__,
2599 err);
2600 return (0);
2603 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2605 struct usb_serial *serial = port->serial;
2606 struct keyspan_serial_private *s_priv;
2607 const struct keyspan_device_details *d_details;
2609 dbg ("%s", __FUNCTION__);
2611 s_priv = usb_get_serial_data(serial);
2612 d_details = s_priv->device_details;
2614 switch (d_details->msg_format) {
2615 case msg_usa26:
2616 keyspan_usa26_send_setup(serial, port, reset_port);
2617 break;
2618 case msg_usa28:
2619 keyspan_usa28_send_setup(serial, port, reset_port);
2620 break;
2621 case msg_usa49:
2622 keyspan_usa49_send_setup(serial, port, reset_port);
2623 break;
2624 case msg_usa90:
2625 keyspan_usa90_send_setup(serial, port, reset_port);
2626 break;
2627 case msg_usa67:
2628 keyspan_usa67_send_setup(serial, port, reset_port);
2629 break;
2634 /* Gets called by the "real" driver (ie once firmware is loaded
2635 and renumeration has taken place. */
2636 static int keyspan_startup (struct usb_serial *serial)
2638 int i, err;
2639 struct usb_serial_port *port;
2640 struct keyspan_serial_private *s_priv;
2641 struct keyspan_port_private *p_priv;
2642 const struct keyspan_device_details *d_details;
2644 dbg("%s", __FUNCTION__);
2646 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2647 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2648 break;
2649 if (d_details == NULL) {
2650 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
2651 return 1;
2654 /* Setup private data for serial driver */
2655 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2656 if (!s_priv) {
2657 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2658 return -ENOMEM;
2661 s_priv->device_details = d_details;
2662 usb_set_serial_data(serial, s_priv);
2664 /* Now setup per port private data */
2665 for (i = 0; i < serial->num_ports; i++) {
2666 port = serial->port[i];
2667 p_priv = kzalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2668 if (!p_priv) {
2669 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2670 return (1);
2672 p_priv->device_details = d_details;
2673 usb_set_serial_port_data(port, p_priv);
2676 keyspan_setup_urbs(serial);
2678 if (s_priv->instat_urb != NULL) {
2679 s_priv->instat_urb->dev = serial->dev;
2680 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2681 if (err != 0)
2682 dbg("%s - submit instat urb failed %d", __FUNCTION__,
2683 err);
2685 if (s_priv->indat_urb != NULL) {
2686 s_priv->indat_urb->dev = serial->dev;
2687 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2688 if (err != 0)
2689 dbg("%s - submit indat urb failed %d", __FUNCTION__,
2690 err);
2693 return (0);
2696 static void keyspan_shutdown (struct usb_serial *serial)
2698 int i, j;
2699 struct usb_serial_port *port;
2700 struct keyspan_serial_private *s_priv;
2701 struct keyspan_port_private *p_priv;
2703 dbg("%s", __FUNCTION__);
2705 s_priv = usb_get_serial_data(serial);
2707 /* Stop reading/writing urbs */
2708 stop_urb(s_priv->instat_urb);
2709 stop_urb(s_priv->glocont_urb);
2710 stop_urb(s_priv->indat_urb);
2711 for (i = 0; i < serial->num_ports; ++i) {
2712 port = serial->port[i];
2713 p_priv = usb_get_serial_port_data(port);
2714 stop_urb(p_priv->inack_urb);
2715 stop_urb(p_priv->outcont_urb);
2716 for (j = 0; j < 2; j++) {
2717 stop_urb(p_priv->in_urbs[j]);
2718 stop_urb(p_priv->out_urbs[j]);
2722 /* Now free them */
2723 usb_free_urb(s_priv->instat_urb);
2724 usb_free_urb(s_priv->indat_urb);
2725 usb_free_urb(s_priv->glocont_urb);
2726 for (i = 0; i < serial->num_ports; ++i) {
2727 port = serial->port[i];
2728 p_priv = usb_get_serial_port_data(port);
2729 usb_free_urb(p_priv->inack_urb);
2730 usb_free_urb(p_priv->outcont_urb);
2731 for (j = 0; j < 2; j++) {
2732 usb_free_urb(p_priv->in_urbs[j]);
2733 usb_free_urb(p_priv->out_urbs[j]);
2737 /* dbg("Freeing serial->private."); */
2738 kfree(s_priv);
2740 /* dbg("Freeing port->private."); */
2741 /* Now free per port private data */
2742 for (i = 0; i < serial->num_ports; i++) {
2743 port = serial->port[i];
2744 kfree(usb_get_serial_port_data(port));
2748 MODULE_AUTHOR( DRIVER_AUTHOR );
2749 MODULE_DESCRIPTION( DRIVER_DESC );
2750 MODULE_LICENSE("GPL");
2752 module_param(debug, bool, S_IRUGO | S_IWUSR);
2753 MODULE_PARM_DESC(debug, "Debug enabled or not");