[PATCH] vr41xx: ARRAY_SIZE cleanup
[linux-2.6/mini2440.git] / drivers / usb / serial / keyspan.c
blob3b958e60f5e8ee717513d3f0d35cb36c0425db94
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/config.h>
99 #include <linux/kernel.h>
100 #include <linux/jiffies.h>
101 #include <linux/errno.h>
102 #include <linux/init.h>
103 #include <linux/slab.h>
104 #include <linux/tty.h>
105 #include <linux/tty_driver.h>
106 #include <linux/tty_flip.h>
107 #include <linux/module.h>
108 #include <linux/spinlock.h>
109 #include <asm/uaccess.h>
110 #include <linux/usb.h>
111 #include "usb-serial.h"
112 #include "keyspan.h"
114 static int debug;
117 * Version Information
119 #define DRIVER_VERSION "v1.1.4"
120 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
121 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
123 #define INSTAT_BUFLEN 32
124 #define GLOCONT_BUFLEN 64
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 /* XXX this one probably will need a lock */
134 struct urb *glocont_urb;
135 char glocont_buf[GLOCONT_BUFLEN];
138 struct keyspan_port_private {
139 /* Keep track of which input & output endpoints to use */
140 int in_flip;
141 int out_flip;
143 /* Keep duplicate of device details in each port
144 structure as well - simplifies some of the
145 callback functions etc. */
146 const struct keyspan_device_details *device_details;
148 /* Input endpoints and buffer for this port */
149 struct urb *in_urbs[2];
150 char in_buffer[2][64];
151 /* Output endpoints and buffer for this port */
152 struct urb *out_urbs[2];
153 char out_buffer[2][64];
155 /* Input ack endpoint */
156 struct urb *inack_urb;
157 char inack_buffer[1];
159 /* Output control endpoint */
160 struct urb *outcont_urb;
161 char outcont_buffer[64];
163 /* Settings for the port */
164 int baud;
165 int old_baud;
166 unsigned int cflag;
167 unsigned int old_cflag;
168 enum {flow_none, flow_cts, flow_xon} flow_control;
169 int rts_state; /* Handshaking pins (outputs) */
170 int dtr_state;
171 int cts_state; /* Handshaking pins (inputs) */
172 int dsr_state;
173 int dcd_state;
174 int ri_state;
175 int break_on;
177 unsigned long tx_start_time[2];
178 int resend_cont; /* need to resend control packet */
182 /* Include Keyspan message headers. All current Keyspan Adapters
183 make use of one of four message formats which are referred
184 to as USA-26, USA-28 and USA-49, USA-90 by Keyspan and within this driver. */
185 #include "keyspan_usa26msg.h"
186 #include "keyspan_usa28msg.h"
187 #include "keyspan_usa49msg.h"
188 #include "keyspan_usa90msg.h"
191 /* Functions used by new usb-serial code. */
192 static int __init keyspan_init (void)
194 int retval;
195 retval = usb_serial_register(&keyspan_pre_device);
196 if (retval)
197 goto failed_pre_device_register;
198 retval = usb_serial_register(&keyspan_1port_device);
199 if (retval)
200 goto failed_1port_device_register;
201 retval = usb_serial_register(&keyspan_2port_device);
202 if (retval)
203 goto failed_2port_device_register;
204 retval = usb_serial_register(&keyspan_4port_device);
205 if (retval)
206 goto failed_4port_device_register;
207 retval = usb_register(&keyspan_driver);
208 if (retval)
209 goto failed_usb_register;
211 info(DRIVER_VERSION ":" DRIVER_DESC);
213 return 0;
214 failed_usb_register:
215 usb_serial_deregister(&keyspan_4port_device);
216 failed_4port_device_register:
217 usb_serial_deregister(&keyspan_2port_device);
218 failed_2port_device_register:
219 usb_serial_deregister(&keyspan_1port_device);
220 failed_1port_device_register:
221 usb_serial_deregister(&keyspan_pre_device);
222 failed_pre_device_register:
223 return retval;
226 static void __exit keyspan_exit (void)
228 usb_deregister (&keyspan_driver);
229 usb_serial_deregister (&keyspan_pre_device);
230 usb_serial_deregister (&keyspan_1port_device);
231 usb_serial_deregister (&keyspan_2port_device);
232 usb_serial_deregister (&keyspan_4port_device);
235 module_init(keyspan_init);
236 module_exit(keyspan_exit);
238 static void keyspan_rx_throttle (struct usb_serial_port *port)
240 dbg("%s - port %d", __FUNCTION__, port->number);
244 static void keyspan_rx_unthrottle (struct usb_serial_port *port)
246 dbg("%s - port %d", __FUNCTION__, port->number);
250 static void keyspan_break_ctl (struct usb_serial_port *port, int break_state)
252 struct keyspan_port_private *p_priv;
254 dbg("%s", __FUNCTION__);
256 p_priv = usb_get_serial_port_data(port);
258 if (break_state == -1)
259 p_priv->break_on = 1;
260 else
261 p_priv->break_on = 0;
263 keyspan_send_setup(port, 0);
267 static void keyspan_set_termios (struct usb_serial_port *port,
268 struct termios *old_termios)
270 int baud_rate, device_port;
271 struct keyspan_port_private *p_priv;
272 const struct keyspan_device_details *d_details;
273 unsigned int cflag;
275 dbg("%s", __FUNCTION__);
277 p_priv = usb_get_serial_port_data(port);
278 d_details = p_priv->device_details;
279 cflag = port->tty->termios->c_cflag;
280 device_port = port->number - port->serial->minor;
282 /* Baud rate calculation takes baud rate as an integer
283 so other rates can be generated if desired. */
284 baud_rate = tty_get_baud_rate(port->tty);
285 /* If no match or invalid, don't change */
286 if (baud_rate >= 0
287 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
288 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
289 /* FIXME - more to do here to ensure rate changes cleanly */
290 p_priv->baud = baud_rate;
293 /* set CTS/RTS handshake etc. */
294 p_priv->cflag = cflag;
295 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
297 keyspan_send_setup(port, 0);
300 static int keyspan_tiocmget(struct usb_serial_port *port, struct file *file)
302 unsigned int value;
303 struct keyspan_port_private *p_priv;
305 p_priv = usb_get_serial_port_data(port);
307 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
308 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
309 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
310 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
311 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
312 ((p_priv->ri_state) ? TIOCM_RNG : 0);
314 return value;
317 static int keyspan_tiocmset(struct usb_serial_port *port, struct file *file,
318 unsigned int set, unsigned int clear)
320 struct keyspan_port_private *p_priv;
322 p_priv = usb_get_serial_port_data(port);
324 if (set & TIOCM_RTS)
325 p_priv->rts_state = 1;
326 if (set & TIOCM_DTR)
327 p_priv->dtr_state = 1;
329 if (clear & TIOCM_RTS)
330 p_priv->rts_state = 0;
331 if (clear & TIOCM_DTR)
332 p_priv->dtr_state = 0;
333 keyspan_send_setup(port, 0);
334 return 0;
337 static int keyspan_ioctl(struct usb_serial_port *port, struct file *file,
338 unsigned int cmd, unsigned long arg)
340 return -ENOIOCTLCMD;
343 /* Write function is similar for the four protocols used
344 with only a minor change for usa90 (usa19hs) required */
345 static int keyspan_write(struct usb_serial_port *port,
346 const unsigned char *buf, int count)
348 struct keyspan_port_private *p_priv;
349 const struct keyspan_device_details *d_details;
350 int flip;
351 int left, todo;
352 struct urb *this_urb;
353 int err, maxDataLen, dataOffset;
355 p_priv = usb_get_serial_port_data(port);
356 d_details = p_priv->device_details;
358 if (d_details->msg_format == msg_usa90) {
359 maxDataLen = 64;
360 dataOffset = 0;
361 } else {
362 maxDataLen = 63;
363 dataOffset = 1;
366 dbg("%s - for port %d (%d chars), flip=%d",
367 __FUNCTION__, port->number, count, p_priv->out_flip);
369 for (left = count; left > 0; left -= todo) {
370 todo = left;
371 if (todo > maxDataLen)
372 todo = maxDataLen;
374 flip = p_priv->out_flip;
376 /* Check we have a valid urb/endpoint before we use it... */
377 if ((this_urb = p_priv->out_urbs[flip]) == NULL) {
378 /* no bulk out, so return 0 bytes written */
379 dbg("%s - no output urb :(", __FUNCTION__);
380 return count;
383 dbg("%s - endpoint %d flip %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe), flip);
385 if (this_urb->status == -EINPROGRESS) {
386 if (time_before(jiffies, p_priv->tx_start_time[flip] + 10 * HZ))
387 break;
388 usb_unlink_urb(this_urb);
389 break;
392 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
393 for now so set to zero */
394 ((char *)this_urb->transfer_buffer)[0] = 0;
396 memcpy (this_urb->transfer_buffer + dataOffset, buf, todo);
397 buf += todo;
399 /* send the data out the bulk port */
400 this_urb->transfer_buffer_length = todo + dataOffset;
402 this_urb->dev = port->serial->dev;
403 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
404 dbg("usb_submit_urb(write bulk) failed (%d)", err);
406 p_priv->tx_start_time[flip] = jiffies;
408 /* Flip for next time if usa26 or usa28 interface
409 (not used on usa49) */
410 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
413 return count - left;
416 static void usa26_indat_callback(struct urb *urb, struct pt_regs *regs)
418 int i, err;
419 int endpoint;
420 struct usb_serial_port *port;
421 struct tty_struct *tty;
422 unsigned char *data = urb->transfer_buffer;
424 dbg ("%s", __FUNCTION__);
426 endpoint = usb_pipeendpoint(urb->pipe);
428 if (urb->status) {
429 dbg("%s - nonzero status: %x on endpoint %d.",
430 __FUNCTION__, urb->status, endpoint);
431 return;
434 port = (struct usb_serial_port *) urb->context;
435 tty = port->tty;
436 if (urb->actual_length) {
437 /* 0x80 bit is error flag */
438 if ((data[0] & 0x80) == 0) {
439 /* no errors on individual bytes, only possible overrun err*/
440 if (data[0] & RXERROR_OVERRUN)
441 err = TTY_OVERRUN;
442 else err = 0;
443 for (i = 1; i < urb->actual_length ; ++i) {
444 tty_insert_flip_char(tty, data[i], err);
446 } else {
447 /* some bytes had errors, every byte has status */
448 dbg("%s - RX error!!!!", __FUNCTION__);
449 for (i = 0; i + 1 < urb->actual_length; i += 2) {
450 int stat = data[i], flag = 0;
451 if (stat & RXERROR_OVERRUN)
452 flag |= TTY_OVERRUN;
453 if (stat & RXERROR_FRAMING)
454 flag |= TTY_FRAME;
455 if (stat & RXERROR_PARITY)
456 flag |= TTY_PARITY;
457 /* XXX should handle break (0x10) */
458 tty_insert_flip_char(tty, data[i+1], flag);
461 tty_flip_buffer_push(tty);
464 /* Resubmit urb so we continue receiving */
465 urb->dev = port->serial->dev;
466 if (port->open_count)
467 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
468 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
470 return;
473 /* Outdat handling is common for all devices */
474 static void usa2x_outdat_callback(struct urb *urb, struct pt_regs *regs)
476 struct usb_serial_port *port;
477 struct keyspan_port_private *p_priv;
479 port = (struct usb_serial_port *) urb->context;
480 p_priv = usb_get_serial_port_data(port);
481 dbg ("%s - urb %d", __FUNCTION__, urb == p_priv->out_urbs[1]);
483 if (port->open_count)
484 schedule_work(&port->work);
487 static void usa26_inack_callback(struct urb *urb, struct pt_regs *regs)
489 dbg ("%s", __FUNCTION__);
493 static void usa26_outcont_callback(struct urb *urb, struct pt_regs *regs)
495 struct usb_serial_port *port;
496 struct keyspan_port_private *p_priv;
498 port = (struct usb_serial_port *) urb->context;
499 p_priv = usb_get_serial_port_data(port);
501 if (p_priv->resend_cont) {
502 dbg ("%s - sending setup", __FUNCTION__);
503 keyspan_usa26_send_setup(port->serial, port, p_priv->resend_cont - 1);
507 static void usa26_instat_callback(struct urb *urb, struct pt_regs *regs)
509 unsigned char *data = urb->transfer_buffer;
510 struct keyspan_usa26_portStatusMessage *msg;
511 struct usb_serial *serial;
512 struct usb_serial_port *port;
513 struct keyspan_port_private *p_priv;
514 int old_dcd_state, err;
516 serial = (struct usb_serial *) urb->context;
518 if (urb->status) {
519 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
520 return;
522 if (urb->actual_length != 9) {
523 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
524 goto exit;
527 msg = (struct keyspan_usa26_portStatusMessage *)data;
529 #if 0
530 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
531 __FUNCTION__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
532 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
533 #endif
535 /* Now do something useful with the data */
538 /* Check port number from message and retrieve private data */
539 if (msg->port >= serial->num_ports) {
540 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
541 goto exit;
543 port = serial->port[msg->port];
544 p_priv = usb_get_serial_port_data(port);
546 /* Update handshaking pin state information */
547 old_dcd_state = p_priv->dcd_state;
548 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
549 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
550 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
551 p_priv->ri_state = ((msg->ri) ? 1 : 0);
553 if (port->tty && !C_CLOCAL(port->tty)
554 && old_dcd_state != p_priv->dcd_state) {
555 if (old_dcd_state)
556 tty_hangup(port->tty);
557 /* else */
558 /* wake_up_interruptible(&p_priv->open_wait); */
561 /* Resubmit urb so we continue receiving */
562 urb->dev = serial->dev;
563 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
564 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
566 exit: ;
569 static void usa26_glocont_callback(struct urb *urb, struct pt_regs *regs)
571 dbg ("%s", __FUNCTION__);
576 static void usa28_indat_callback(struct urb *urb, struct pt_regs *regs)
578 int i, err;
579 struct usb_serial_port *port;
580 struct tty_struct *tty;
581 unsigned char *data;
582 struct keyspan_port_private *p_priv;
584 dbg ("%s", __FUNCTION__);
586 port = (struct usb_serial_port *) urb->context;
587 p_priv = usb_get_serial_port_data(port);
588 data = urb->transfer_buffer;
590 if (urb != p_priv->in_urbs[p_priv->in_flip])
591 return;
593 do {
594 if (urb->status) {
595 dbg("%s - nonzero status: %x on endpoint %d.",
596 __FUNCTION__, urb->status, usb_pipeendpoint(urb->pipe));
597 return;
600 port = (struct usb_serial_port *) urb->context;
601 p_priv = usb_get_serial_port_data(port);
602 data = urb->transfer_buffer;
604 tty = port->tty;
605 if (urb->actual_length) {
606 for (i = 0; i < urb->actual_length ; ++i) {
607 tty_insert_flip_char(tty, data[i], 0);
609 tty_flip_buffer_push(tty);
612 /* Resubmit urb so we continue receiving */
613 urb->dev = port->serial->dev;
614 if (port->open_count)
615 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
616 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
618 p_priv->in_flip ^= 1;
620 urb = p_priv->in_urbs[p_priv->in_flip];
621 } while (urb->status != -EINPROGRESS);
624 static void usa28_inack_callback(struct urb *urb, struct pt_regs *regs)
626 dbg ("%s", __FUNCTION__);
629 static void usa28_outcont_callback(struct urb *urb, struct pt_regs *regs)
631 struct usb_serial_port *port;
632 struct keyspan_port_private *p_priv;
634 port = (struct usb_serial_port *) urb->context;
635 p_priv = usb_get_serial_port_data(port);
637 if (p_priv->resend_cont) {
638 dbg ("%s - sending setup", __FUNCTION__);
639 keyspan_usa28_send_setup(port->serial, port, p_priv->resend_cont - 1);
643 static void usa28_instat_callback(struct urb *urb, struct pt_regs *regs)
645 int err;
646 unsigned char *data = urb->transfer_buffer;
647 struct keyspan_usa28_portStatusMessage *msg;
648 struct usb_serial *serial;
649 struct usb_serial_port *port;
650 struct keyspan_port_private *p_priv;
651 int old_dcd_state;
653 serial = (struct usb_serial *) urb->context;
655 if (urb->status) {
656 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
657 return;
660 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
661 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
662 goto exit;
665 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__
666 data[0], data[1], data[2], data[3], data[4], data[5],
667 data[6], data[7], data[8], data[9], data[10], data[11]);*/
669 /* Now do something useful with the data */
670 msg = (struct keyspan_usa28_portStatusMessage *)data;
673 /* Check port number from message and retrieve private data */
674 if (msg->port >= serial->num_ports) {
675 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->port);
676 goto exit;
678 port = serial->port[msg->port];
679 p_priv = usb_get_serial_port_data(port);
681 /* Update handshaking pin state information */
682 old_dcd_state = p_priv->dcd_state;
683 p_priv->cts_state = ((msg->cts) ? 1 : 0);
684 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
685 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
686 p_priv->ri_state = ((msg->ri) ? 1 : 0);
688 if (port->tty && !C_CLOCAL(port->tty)
689 && old_dcd_state != p_priv->dcd_state) {
690 if (old_dcd_state)
691 tty_hangup(port->tty);
692 /* else */
693 /* wake_up_interruptible(&p_priv->open_wait); */
696 /* Resubmit urb so we continue receiving */
697 urb->dev = serial->dev;
698 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
699 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
701 exit: ;
704 static void usa28_glocont_callback(struct urb *urb, struct pt_regs *regs)
706 dbg ("%s", __FUNCTION__);
710 static void usa49_glocont_callback(struct urb *urb, struct pt_regs *regs)
712 struct usb_serial *serial;
713 struct usb_serial_port *port;
714 struct keyspan_port_private *p_priv;
715 int i;
717 dbg ("%s", __FUNCTION__);
719 serial = (struct usb_serial *) urb->context;
720 for (i = 0; i < serial->num_ports; ++i) {
721 port = serial->port[i];
722 p_priv = usb_get_serial_port_data(port);
724 if (p_priv->resend_cont) {
725 dbg ("%s - sending setup", __FUNCTION__);
726 keyspan_usa49_send_setup(serial, port, p_priv->resend_cont - 1);
727 break;
732 /* This is actually called glostat in the Keyspan
733 doco */
734 static void usa49_instat_callback(struct urb *urb, struct pt_regs *regs)
736 int err;
737 unsigned char *data = urb->transfer_buffer;
738 struct keyspan_usa49_portStatusMessage *msg;
739 struct usb_serial *serial;
740 struct usb_serial_port *port;
741 struct keyspan_port_private *p_priv;
742 int old_dcd_state;
744 dbg ("%s", __FUNCTION__);
746 serial = (struct usb_serial *) urb->context;
748 if (urb->status) {
749 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
750 return;
753 if (urb->actual_length != sizeof(struct keyspan_usa49_portStatusMessage)) {
754 dbg("%s - bad length %d", __FUNCTION__, urb->actual_length);
755 goto exit;
758 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __FUNCTION__,
759 data[0], data[1], data[2], data[3], data[4], data[5],
760 data[6], data[7], data[8], data[9], data[10]);*/
762 /* Now do something useful with the data */
763 msg = (struct keyspan_usa49_portStatusMessage *)data;
765 /* Check port number from message and retrieve private data */
766 if (msg->portNumber >= serial->num_ports) {
767 dbg ("%s - Unexpected port number %d", __FUNCTION__, msg->portNumber);
768 goto exit;
770 port = serial->port[msg->portNumber];
771 p_priv = usb_get_serial_port_data(port);
773 /* Update handshaking pin state information */
774 old_dcd_state = p_priv->dcd_state;
775 p_priv->cts_state = ((msg->cts) ? 1 : 0);
776 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
777 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
778 p_priv->ri_state = ((msg->ri) ? 1 : 0);
780 if (port->tty && !C_CLOCAL(port->tty)
781 && old_dcd_state != p_priv->dcd_state) {
782 if (old_dcd_state)
783 tty_hangup(port->tty);
784 /* else */
785 /* wake_up_interruptible(&p_priv->open_wait); */
788 /* Resubmit urb so we continue receiving */
789 urb->dev = serial->dev;
791 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
792 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
794 exit: ;
797 static void usa49_inack_callback(struct urb *urb, struct pt_regs *regs)
799 dbg ("%s", __FUNCTION__);
802 static void usa49_indat_callback(struct urb *urb, struct pt_regs *regs)
804 int i, err;
805 int endpoint;
806 struct usb_serial_port *port;
807 struct tty_struct *tty;
808 unsigned char *data = urb->transfer_buffer;
810 dbg ("%s", __FUNCTION__);
812 endpoint = usb_pipeendpoint(urb->pipe);
814 if (urb->status) {
815 dbg("%s - nonzero status: %x on endpoint %d.", __FUNCTION__,
816 urb->status, endpoint);
817 return;
820 port = (struct usb_serial_port *) urb->context;
821 tty = port->tty;
822 if (urb->actual_length) {
823 /* 0x80 bit is error flag */
824 if ((data[0] & 0x80) == 0) {
825 /* no error on any byte */
826 for (i = 1; i < urb->actual_length ; ++i) {
827 tty_insert_flip_char(tty, data[i], 0);
829 } else {
830 /* some bytes had errors, every byte has status */
831 for (i = 0; i + 1 < urb->actual_length; i += 2) {
832 int stat = data[i], flag = 0;
833 if (stat & RXERROR_OVERRUN)
834 flag |= TTY_OVERRUN;
835 if (stat & RXERROR_FRAMING)
836 flag |= TTY_FRAME;
837 if (stat & RXERROR_PARITY)
838 flag |= TTY_PARITY;
839 /* XXX should handle break (0x10) */
840 tty_insert_flip_char(tty, data[i+1], flag);
843 tty_flip_buffer_push(tty);
846 /* Resubmit urb so we continue receiving */
847 urb->dev = port->serial->dev;
848 if (port->open_count)
849 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
850 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
854 /* not used, usa-49 doesn't have per-port control endpoints */
855 static void usa49_outcont_callback(struct urb *urb, struct pt_regs *regs)
857 dbg ("%s", __FUNCTION__);
860 static void usa90_indat_callback(struct urb *urb, struct pt_regs *regs)
862 int i, err;
863 int endpoint;
864 struct usb_serial_port *port;
865 struct keyspan_port_private *p_priv;
866 struct tty_struct *tty;
867 unsigned char *data = urb->transfer_buffer;
869 dbg ("%s", __FUNCTION__);
871 endpoint = usb_pipeendpoint(urb->pipe);
874 if (urb->status) {
875 dbg("%s - nonzero status: %x on endpoint %d.",
876 __FUNCTION__, urb->status, endpoint);
877 return;
880 port = (struct usb_serial_port *) urb->context;
881 p_priv = usb_get_serial_port_data(port);
883 tty = port->tty;
884 if (urb->actual_length) {
886 /* if current mode is DMA, looks like usa28 format
887 otherwise looks like usa26 data format */
889 if (p_priv->baud > 57600) {
890 for (i = 0; i < urb->actual_length ; ++i)
891 tty_insert_flip_char(tty, data[i], 0);
893 else {
895 /* 0x80 bit is error flag */
896 if ((data[0] & 0x80) == 0) {
897 /* no errors on individual bytes, only possible overrun err*/
898 if (data[0] & RXERROR_OVERRUN)
899 err = TTY_OVERRUN;
900 else err = 0;
901 for (i = 1; i < urb->actual_length ; ++i)
902 tty_insert_flip_char(tty, data[i], err);
905 else {
906 /* some bytes had errors, every byte has status */
907 dbg("%s - RX error!!!!", __FUNCTION__);
908 for (i = 0; i + 1 < urb->actual_length; i += 2) {
909 int stat = data[i], flag = 0;
910 if (stat & RXERROR_OVERRUN)
911 flag |= TTY_OVERRUN;
912 if (stat & RXERROR_FRAMING)
913 flag |= TTY_FRAME;
914 if (stat & RXERROR_PARITY)
915 flag |= TTY_PARITY;
916 /* XXX should handle break (0x10) */
917 tty_insert_flip_char(tty, data[i+1], flag);
921 tty_flip_buffer_push(tty);
924 /* Resubmit urb so we continue receiving */
925 urb->dev = port->serial->dev;
926 if (port->open_count)
927 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
928 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
930 return;
934 static void usa90_instat_callback(struct urb *urb, struct pt_regs *regs)
936 unsigned char *data = urb->transfer_buffer;
937 struct keyspan_usa90_portStatusMessage *msg;
938 struct usb_serial *serial;
939 struct usb_serial_port *port;
940 struct keyspan_port_private *p_priv;
941 int old_dcd_state, err;
943 serial = (struct usb_serial *) urb->context;
945 if (urb->status) {
946 dbg("%s - nonzero status: %x", __FUNCTION__, urb->status);
947 return;
949 if (urb->actual_length < 14) {
950 dbg("%s - %d byte report??", __FUNCTION__, urb->actual_length);
951 goto exit;
954 msg = (struct keyspan_usa90_portStatusMessage *)data;
956 /* Now do something useful with the data */
958 port = serial->port[0];
959 p_priv = usb_get_serial_port_data(port);
961 /* Update handshaking pin state information */
962 old_dcd_state = p_priv->dcd_state;
963 p_priv->cts_state = ((msg->cts) ? 1 : 0);
964 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
965 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
966 p_priv->ri_state = ((msg->ri) ? 1 : 0);
968 if (port->tty && !C_CLOCAL(port->tty)
969 && old_dcd_state != p_priv->dcd_state) {
970 if (old_dcd_state)
971 tty_hangup(port->tty);
972 /* else */
973 /* wake_up_interruptible(&p_priv->open_wait); */
976 /* Resubmit urb so we continue receiving */
977 urb->dev = serial->dev;
978 if ((err = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
979 dbg("%s - resubmit read urb failed. (%d)", __FUNCTION__, err);
981 exit:
985 static void usa90_outcont_callback(struct urb *urb, struct pt_regs *regs)
987 struct usb_serial_port *port;
988 struct keyspan_port_private *p_priv;
990 port = (struct usb_serial_port *) urb->context;
991 p_priv = usb_get_serial_port_data(port);
993 if (p_priv->resend_cont) {
994 dbg ("%s - sending setup", __FUNCTION__);
995 keyspan_usa90_send_setup(port->serial, port, p_priv->resend_cont - 1);
999 static int keyspan_write_room (struct usb_serial_port *port)
1001 struct keyspan_port_private *p_priv;
1002 const struct keyspan_device_details *d_details;
1003 int flip;
1004 int data_len;
1005 struct urb *this_urb;
1007 dbg("%s", __FUNCTION__);
1008 p_priv = usb_get_serial_port_data(port);
1009 d_details = p_priv->device_details;
1011 if (d_details->msg_format == msg_usa90)
1012 data_len = 64;
1013 else
1014 data_len = 63;
1016 flip = p_priv->out_flip;
1018 /* Check both endpoints to see if any are available. */
1019 if ((this_urb = p_priv->out_urbs[flip]) != NULL) {
1020 if (this_urb->status != -EINPROGRESS)
1021 return (data_len);
1022 flip = (flip + 1) & d_details->outdat_endp_flip;
1023 if ((this_urb = p_priv->out_urbs[flip]) != NULL)
1024 if (this_urb->status != -EINPROGRESS)
1025 return (data_len);
1027 return (0);
1031 static int keyspan_chars_in_buffer (struct usb_serial_port *port)
1033 return (0);
1037 static int keyspan_open (struct usb_serial_port *port, struct file *filp)
1039 struct keyspan_port_private *p_priv;
1040 struct keyspan_serial_private *s_priv;
1041 struct usb_serial *serial = port->serial;
1042 const struct keyspan_device_details *d_details;
1043 int i, err;
1044 int baud_rate, device_port;
1045 struct urb *urb;
1046 unsigned int cflag;
1048 s_priv = usb_get_serial_data(serial);
1049 p_priv = usb_get_serial_port_data(port);
1050 d_details = p_priv->device_details;
1052 dbg("%s - port%d.", __FUNCTION__, port->number);
1054 /* Set some sane defaults */
1055 p_priv->rts_state = 1;
1056 p_priv->dtr_state = 1;
1057 p_priv->baud = 9600;
1059 /* force baud and lcr to be set on open */
1060 p_priv->old_baud = 0;
1061 p_priv->old_cflag = 0;
1063 p_priv->out_flip = 0;
1064 p_priv->in_flip = 0;
1066 /* Reset low level data toggle and start reading from endpoints */
1067 for (i = 0; i < 2; i++) {
1068 if ((urb = p_priv->in_urbs[i]) == NULL)
1069 continue;
1070 urb->dev = serial->dev;
1072 /* make sure endpoint data toggle is synchronized with the device */
1074 usb_clear_halt(urb->dev, urb->pipe);
1076 if ((err = usb_submit_urb(urb, GFP_KERNEL)) != 0) {
1077 dbg("%s - submit urb %d failed (%d)", __FUNCTION__, i, err);
1081 /* Reset low level data toggle on out endpoints */
1082 for (i = 0; i < 2; i++) {
1083 if ((urb = p_priv->out_urbs[i]) == NULL)
1084 continue;
1085 urb->dev = serial->dev;
1086 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1089 /* get the terminal config for the setup message now so we don't
1090 * need to send 2 of them */
1092 cflag = port->tty->termios->c_cflag;
1093 device_port = port->number - port->serial->minor;
1095 /* Baud rate calculation takes baud rate as an integer
1096 so other rates can be generated if desired. */
1097 baud_rate = tty_get_baud_rate(port->tty);
1098 /* If no match or invalid, leave as default */
1099 if (baud_rate >= 0
1100 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1101 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1102 p_priv->baud = baud_rate;
1105 /* set CTS/RTS handshake etc. */
1106 p_priv->cflag = cflag;
1107 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1109 keyspan_send_setup(port, 1);
1110 //mdelay(100);
1111 //keyspan_set_termios(port, NULL);
1113 return (0);
1116 static inline void stop_urb(struct urb *urb)
1118 if (urb && urb->status == -EINPROGRESS)
1119 usb_kill_urb(urb);
1122 static void keyspan_close(struct usb_serial_port *port, struct file *filp)
1124 int i;
1125 struct usb_serial *serial = port->serial;
1126 struct keyspan_serial_private *s_priv;
1127 struct keyspan_port_private *p_priv;
1129 dbg("%s", __FUNCTION__);
1130 s_priv = usb_get_serial_data(serial);
1131 p_priv = usb_get_serial_port_data(port);
1133 p_priv->rts_state = 0;
1134 p_priv->dtr_state = 0;
1136 if (serial->dev) {
1137 keyspan_send_setup(port, 2);
1138 /* pilot-xfer seems to work best with this delay */
1139 mdelay(100);
1140 // keyspan_set_termios(port, NULL);
1143 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1144 dbg("%s - urb in progress", __FUNCTION__);
1147 p_priv->out_flip = 0;
1148 p_priv->in_flip = 0;
1150 if (serial->dev) {
1151 /* Stop reading/writing urbs */
1152 stop_urb(p_priv->inack_urb);
1153 /* stop_urb(p_priv->outcont_urb); */
1154 for (i = 0; i < 2; i++) {
1155 stop_urb(p_priv->in_urbs[i]);
1156 stop_urb(p_priv->out_urbs[i]);
1159 port->tty = NULL;
1163 /* download the firmware to a pre-renumeration device */
1164 static int keyspan_fake_startup (struct usb_serial *serial)
1166 int response;
1167 const struct ezusb_hex_record *record;
1168 char *fw_name;
1170 dbg("Keyspan startup version %04x product %04x",
1171 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1172 le16_to_cpu(serial->dev->descriptor.idProduct));
1174 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000) != 0x8000) {
1175 dbg("Firmware already loaded. Quitting.");
1176 return(1);
1179 /* Select firmware image on the basis of idProduct */
1180 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1181 case keyspan_usa28_pre_product_id:
1182 record = &keyspan_usa28_firmware[0];
1183 fw_name = "USA28";
1184 break;
1186 case keyspan_usa28x_pre_product_id:
1187 record = &keyspan_usa28x_firmware[0];
1188 fw_name = "USA28X";
1189 break;
1191 case keyspan_usa28xa_pre_product_id:
1192 record = &keyspan_usa28xa_firmware[0];
1193 fw_name = "USA28XA";
1194 break;
1196 case keyspan_usa28xb_pre_product_id:
1197 record = &keyspan_usa28xb_firmware[0];
1198 fw_name = "USA28XB";
1199 break;
1201 case keyspan_usa19_pre_product_id:
1202 record = &keyspan_usa19_firmware[0];
1203 fw_name = "USA19";
1204 break;
1206 case keyspan_usa19qi_pre_product_id:
1207 record = &keyspan_usa19qi_firmware[0];
1208 fw_name = "USA19QI";
1209 break;
1211 case keyspan_mpr_pre_product_id:
1212 record = &keyspan_mpr_firmware[0];
1213 fw_name = "MPR";
1214 break;
1216 case keyspan_usa19qw_pre_product_id:
1217 record = &keyspan_usa19qw_firmware[0];
1218 fw_name = "USA19QI";
1219 break;
1221 case keyspan_usa18x_pre_product_id:
1222 record = &keyspan_usa18x_firmware[0];
1223 fw_name = "USA18X";
1224 break;
1226 case keyspan_usa19w_pre_product_id:
1227 record = &keyspan_usa19w_firmware[0];
1228 fw_name = "USA19W";
1229 break;
1231 case keyspan_usa49w_pre_product_id:
1232 record = &keyspan_usa49w_firmware[0];
1233 fw_name = "USA49W";
1234 break;
1236 case keyspan_usa49wlc_pre_product_id:
1237 record = &keyspan_usa49wlc_firmware[0];
1238 fw_name = "USA49WLC";
1239 break;
1241 default:
1242 record = NULL;
1243 fw_name = "Unknown";
1244 break;
1247 if (record == NULL) {
1248 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1249 return(1);
1252 dbg("Uploading Keyspan %s firmware.", fw_name);
1254 /* download the firmware image */
1255 response = ezusb_set_reset(serial, 1);
1257 while(record->address != 0xffff) {
1258 response = ezusb_writememory(serial, record->address,
1259 (unsigned char *)record->data,
1260 record->data_size, 0xa0);
1261 if (response < 0) {
1262 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan"
1263 "firmware (%d %04X %p %d)\n",
1264 response,
1265 record->address, record->data, record->data_size);
1266 break;
1268 record++;
1270 /* bring device out of reset. Renumeration will occur in a
1271 moment and the new device will bind to the real driver */
1272 response = ezusb_set_reset(serial, 0);
1274 /* we don't want this device to have a driver assigned to it. */
1275 return (1);
1278 /* Helper functions used by keyspan_setup_urbs */
1279 static struct urb *keyspan_setup_urb (struct usb_serial *serial, int endpoint,
1280 int dir, void *ctx, char *buf, int len,
1281 void (*callback)(struct urb *, struct pt_regs *regs))
1283 struct urb *urb;
1285 if (endpoint == -1)
1286 return NULL; /* endpoint not needed */
1288 dbg ("%s - alloc for endpoint %d.", __FUNCTION__, endpoint);
1289 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1290 if (urb == NULL) {
1291 dbg ("%s - alloc for endpoint %d failed.", __FUNCTION__, endpoint);
1292 return NULL;
1295 /* Fill URB using supplied data. */
1296 usb_fill_bulk_urb(urb, serial->dev,
1297 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1298 buf, len, callback, ctx);
1300 return urb;
1303 static struct callbacks {
1304 void (*instat_callback)(struct urb *, struct pt_regs *regs);
1305 void (*glocont_callback)(struct urb *, struct pt_regs *regs);
1306 void (*indat_callback)(struct urb *, struct pt_regs *regs);
1307 void (*outdat_callback)(struct urb *, struct pt_regs *regs);
1308 void (*inack_callback)(struct urb *, struct pt_regs *regs);
1309 void (*outcont_callback)(struct urb *, struct pt_regs *regs);
1310 } keyspan_callbacks[] = {
1312 /* msg_usa26 callbacks */
1313 .instat_callback = usa26_instat_callback,
1314 .glocont_callback = usa26_glocont_callback,
1315 .indat_callback = usa26_indat_callback,
1316 .outdat_callback = usa2x_outdat_callback,
1317 .inack_callback = usa26_inack_callback,
1318 .outcont_callback = usa26_outcont_callback,
1319 }, {
1320 /* msg_usa28 callbacks */
1321 .instat_callback = usa28_instat_callback,
1322 .glocont_callback = usa28_glocont_callback,
1323 .indat_callback = usa28_indat_callback,
1324 .outdat_callback = usa2x_outdat_callback,
1325 .inack_callback = usa28_inack_callback,
1326 .outcont_callback = usa28_outcont_callback,
1327 }, {
1328 /* msg_usa49 callbacks */
1329 .instat_callback = usa49_instat_callback,
1330 .glocont_callback = usa49_glocont_callback,
1331 .indat_callback = usa49_indat_callback,
1332 .outdat_callback = usa2x_outdat_callback,
1333 .inack_callback = usa49_inack_callback,
1334 .outcont_callback = usa49_outcont_callback,
1335 }, {
1336 /* msg_usa90 callbacks */
1337 .instat_callback = usa90_instat_callback,
1338 .glocont_callback = usa28_glocont_callback,
1339 .indat_callback = usa90_indat_callback,
1340 .outdat_callback = usa2x_outdat_callback,
1341 .inack_callback = usa28_inack_callback,
1342 .outcont_callback = usa90_outcont_callback,
1346 /* Generic setup urbs function that uses
1347 data in device_details */
1348 static void keyspan_setup_urbs(struct usb_serial *serial)
1350 int i, j;
1351 struct keyspan_serial_private *s_priv;
1352 const struct keyspan_device_details *d_details;
1353 struct usb_serial_port *port;
1354 struct keyspan_port_private *p_priv;
1355 struct callbacks *cback;
1356 int endp;
1358 dbg ("%s", __FUNCTION__);
1360 s_priv = usb_get_serial_data(serial);
1361 d_details = s_priv->device_details;
1363 /* Setup values for the various callback routines */
1364 cback = &keyspan_callbacks[d_details->msg_format];
1366 /* Allocate and set up urbs for each one that is in use,
1367 starting with instat endpoints */
1368 s_priv->instat_urb = keyspan_setup_urb
1369 (serial, d_details->instat_endpoint, USB_DIR_IN,
1370 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1371 cback->instat_callback);
1373 s_priv->glocont_urb = keyspan_setup_urb
1374 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1375 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1376 cback->glocont_callback);
1378 /* Setup endpoints for each port specific thing */
1379 for (i = 0; i < d_details->num_ports; i ++) {
1380 port = serial->port[i];
1381 p_priv = usb_get_serial_port_data(port);
1383 /* Do indat endpoints first, once for each flip */
1384 endp = d_details->indat_endpoints[i];
1385 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1386 p_priv->in_urbs[j] = keyspan_setup_urb
1387 (serial, endp, USB_DIR_IN, port,
1388 p_priv->in_buffer[j], 64,
1389 cback->indat_callback);
1391 for (; j < 2; ++j)
1392 p_priv->in_urbs[j] = NULL;
1394 /* outdat endpoints also have flip */
1395 endp = d_details->outdat_endpoints[i];
1396 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1397 p_priv->out_urbs[j] = keyspan_setup_urb
1398 (serial, endp, USB_DIR_OUT, port,
1399 p_priv->out_buffer[j], 64,
1400 cback->outdat_callback);
1402 for (; j < 2; ++j)
1403 p_priv->out_urbs[j] = NULL;
1405 /* inack endpoint */
1406 p_priv->inack_urb = keyspan_setup_urb
1407 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1408 port, p_priv->inack_buffer, 1, cback->inack_callback);
1410 /* outcont endpoint */
1411 p_priv->outcont_urb = keyspan_setup_urb
1412 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1413 port, p_priv->outcont_buffer, 64,
1414 cback->outcont_callback);
1419 /* usa19 function doesn't require prescaler */
1420 static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1421 u8 *rate_low, u8 *prescaler, int portnum)
1423 u32 b16, /* baud rate times 16 (actual rate used internally) */
1424 div, /* divisor */
1425 cnt; /* inverse of divisor (programmed into 8051) */
1427 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1429 /* prevent divide by zero... */
1430 if( (b16 = (baud_rate * 16L)) == 0) {
1431 return (KEYSPAN_INVALID_BAUD_RATE);
1434 /* Any "standard" rate over 57k6 is marginal on the USA-19
1435 as we run out of divisor resolution. */
1436 if (baud_rate > 57600) {
1437 return (KEYSPAN_INVALID_BAUD_RATE);
1440 /* calculate the divisor and the counter (its inverse) */
1441 if( (div = (baudclk / b16)) == 0) {
1442 return (KEYSPAN_INVALID_BAUD_RATE);
1444 else {
1445 cnt = 0 - div;
1448 if(div > 0xffff) {
1449 return (KEYSPAN_INVALID_BAUD_RATE);
1452 /* return the counter values if non-null */
1453 if (rate_low) {
1454 *rate_low = (u8) (cnt & 0xff);
1456 if (rate_hi) {
1457 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1459 if (rate_low && rate_hi) {
1460 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1463 return (KEYSPAN_BAUD_RATE_OK);
1466 /* usa19hs function doesn't require prescaler */
1467 static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1468 u8 *rate_low, u8 *prescaler, int portnum)
1470 u32 b16, /* baud rate times 16 (actual rate used internally) */
1471 div; /* divisor */
1473 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1475 /* prevent divide by zero... */
1476 if( (b16 = (baud_rate * 16L)) == 0)
1477 return (KEYSPAN_INVALID_BAUD_RATE);
1481 /* calculate the divisor */
1482 if( (div = (baudclk / b16)) == 0)
1483 return (KEYSPAN_INVALID_BAUD_RATE);
1485 if(div > 0xffff)
1486 return (KEYSPAN_INVALID_BAUD_RATE);
1488 /* return the counter values if non-null */
1489 if (rate_low)
1490 *rate_low = (u8) (div & 0xff);
1492 if (rate_hi)
1493 *rate_hi = (u8) ((div >> 8) & 0xff);
1495 if (rate_low && rate_hi)
1496 dbg ("%s - %d %02x %02x.", __FUNCTION__, baud_rate, *rate_hi, *rate_low);
1498 return (KEYSPAN_BAUD_RATE_OK);
1501 static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1502 u8 *rate_low, u8 *prescaler, int portnum)
1504 u32 b16, /* baud rate times 16 (actual rate used internally) */
1505 clk, /* clock with 13/8 prescaler */
1506 div, /* divisor using 13/8 prescaler */
1507 res, /* resulting baud rate using 13/8 prescaler */
1508 diff, /* error using 13/8 prescaler */
1509 smallest_diff;
1510 u8 best_prescaler;
1511 int i;
1513 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1515 /* prevent divide by zero */
1516 if( (b16 = baud_rate * 16L) == 0) {
1517 return (KEYSPAN_INVALID_BAUD_RATE);
1520 /* Calculate prescaler by trying them all and looking
1521 for best fit */
1523 /* start with largest possible difference */
1524 smallest_diff = 0xffffffff;
1526 /* 0 is an invalid prescaler, used as a flag */
1527 best_prescaler = 0;
1529 for(i = 8; i <= 0xff; ++i) {
1530 clk = (baudclk * 8) / (u32) i;
1532 if( (div = clk / b16) == 0) {
1533 continue;
1536 res = clk / div;
1537 diff= (res > b16) ? (res-b16) : (b16-res);
1539 if(diff < smallest_diff) {
1540 best_prescaler = i;
1541 smallest_diff = diff;
1545 if(best_prescaler == 0) {
1546 return (KEYSPAN_INVALID_BAUD_RATE);
1549 clk = (baudclk * 8) / (u32) best_prescaler;
1550 div = clk / b16;
1552 /* return the divisor and prescaler if non-null */
1553 if (rate_low) {
1554 *rate_low = (u8) (div & 0xff);
1556 if (rate_hi) {
1557 *rate_hi = (u8) ((div >> 8) & 0xff);
1559 if (prescaler) {
1560 *prescaler = best_prescaler;
1561 /* dbg("%s - %d %d", __FUNCTION__, *prescaler, div); */
1563 return (KEYSPAN_BAUD_RATE_OK);
1566 /* USA-28 supports different maximum baud rates on each port */
1567 static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1568 u8 *rate_low, u8 *prescaler, int portnum)
1570 u32 b16, /* baud rate times 16 (actual rate used internally) */
1571 div, /* divisor */
1572 cnt; /* inverse of divisor (programmed into 8051) */
1574 dbg ("%s - %d.", __FUNCTION__, baud_rate);
1576 /* prevent divide by zero */
1577 if ((b16 = baud_rate * 16L) == 0)
1578 return (KEYSPAN_INVALID_BAUD_RATE);
1580 /* calculate the divisor and the counter (its inverse) */
1581 if ((div = (KEYSPAN_USA28_BAUDCLK / b16)) == 0) {
1582 return (KEYSPAN_INVALID_BAUD_RATE);
1584 else {
1585 cnt = 0 - div;
1588 /* check for out of range, based on portnum,
1589 and return result */
1590 if(portnum == 0) {
1591 if(div > 0xffff)
1592 return (KEYSPAN_INVALID_BAUD_RATE);
1594 else {
1595 if(portnum == 1) {
1596 if(div > 0xff) {
1597 return (KEYSPAN_INVALID_BAUD_RATE);
1600 else {
1601 return (KEYSPAN_INVALID_BAUD_RATE);
1605 /* return the counter values if not NULL
1606 (port 1 will ignore retHi) */
1607 if (rate_low) {
1608 *rate_low = (u8) (cnt & 0xff);
1610 if (rate_hi) {
1611 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1613 dbg ("%s - %d OK.", __FUNCTION__, baud_rate);
1614 return (KEYSPAN_BAUD_RATE_OK);
1617 static int keyspan_usa26_send_setup(struct usb_serial *serial,
1618 struct usb_serial_port *port,
1619 int reset_port)
1621 struct keyspan_usa26_portControlMessage msg;
1622 struct keyspan_serial_private *s_priv;
1623 struct keyspan_port_private *p_priv;
1624 const struct keyspan_device_details *d_details;
1625 int outcont_urb;
1626 struct urb *this_urb;
1627 int device_port, err;
1629 dbg ("%s reset=%d", __FUNCTION__, reset_port);
1631 s_priv = usb_get_serial_data(serial);
1632 p_priv = usb_get_serial_port_data(port);
1633 d_details = s_priv->device_details;
1634 device_port = port->number - port->serial->minor;
1636 outcont_urb = d_details->outcont_endpoints[port->number];
1637 this_urb = p_priv->outcont_urb;
1639 dbg("%s - endpoint %d", __FUNCTION__, usb_pipeendpoint(this_urb->pipe));
1641 /* Make sure we have an urb then send the message */
1642 if (this_urb == NULL) {
1643 dbg("%s - oops no urb.", __FUNCTION__);
1644 return -1;
1647 /* Save reset port val for resend.
1648 Don't overwrite resend for close condition. */
1649 if (p_priv->resend_cont != 3)
1650 p_priv->resend_cont = reset_port + 1;
1651 if (this_urb->status == -EINPROGRESS) {
1652 /* dbg ("%s - already writing", __FUNCTION__); */
1653 mdelay(5);
1654 return(-1);
1657 memset(&msg, 0, sizeof (struct keyspan_usa26_portControlMessage));
1659 /* Only set baud rate if it's changed */
1660 if (p_priv->old_baud != p_priv->baud) {
1661 p_priv->old_baud = p_priv->baud;
1662 msg.setClocking = 0xff;
1663 if (d_details->calculate_baud_rate
1664 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1665 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1666 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1667 p_priv->baud);
1668 msg.baudLo = 0;
1669 msg.baudHi = 125; /* Values for 9600 baud */
1670 msg.prescaler = 10;
1672 msg.setPrescaler = 0xff;
1675 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1676 switch (p_priv->cflag & CSIZE) {
1677 case CS5:
1678 msg.lcr |= USA_DATABITS_5;
1679 break;
1680 case CS6:
1681 msg.lcr |= USA_DATABITS_6;
1682 break;
1683 case CS7:
1684 msg.lcr |= USA_DATABITS_7;
1685 break;
1686 case CS8:
1687 msg.lcr |= USA_DATABITS_8;
1688 break;
1690 if (p_priv->cflag & PARENB) {
1691 /* note USA_PARITY_NONE == 0 */
1692 msg.lcr |= (p_priv->cflag & PARODD)?
1693 USA_PARITY_ODD: USA_PARITY_EVEN;
1695 msg.setLcr = 0xff;
1697 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1698 msg.xonFlowControl = 0;
1699 msg.setFlowControl = 0xff;
1700 msg.forwardingLength = 16;
1701 msg.xonChar = 17;
1702 msg.xoffChar = 19;
1704 /* Opening port */
1705 if (reset_port == 1) {
1706 msg._txOn = 1;
1707 msg._txOff = 0;
1708 msg.txFlush = 0;
1709 msg.txBreak = 0;
1710 msg.rxOn = 1;
1711 msg.rxOff = 0;
1712 msg.rxFlush = 1;
1713 msg.rxForward = 0;
1714 msg.returnStatus = 0;
1715 msg.resetDataToggle = 0xff;
1718 /* Closing port */
1719 else if (reset_port == 2) {
1720 msg._txOn = 0;
1721 msg._txOff = 1;
1722 msg.txFlush = 0;
1723 msg.txBreak = 0;
1724 msg.rxOn = 0;
1725 msg.rxOff = 1;
1726 msg.rxFlush = 1;
1727 msg.rxForward = 0;
1728 msg.returnStatus = 0;
1729 msg.resetDataToggle = 0;
1732 /* Sending intermediate configs */
1733 else {
1734 msg._txOn = (! p_priv->break_on);
1735 msg._txOff = 0;
1736 msg.txFlush = 0;
1737 msg.txBreak = (p_priv->break_on);
1738 msg.rxOn = 0;
1739 msg.rxOff = 0;
1740 msg.rxFlush = 0;
1741 msg.rxForward = 0;
1742 msg.returnStatus = 0;
1743 msg.resetDataToggle = 0x0;
1746 /* Do handshaking outputs */
1747 msg.setTxTriState_setRts = 0xff;
1748 msg.txTriState_rts = p_priv->rts_state;
1750 msg.setHskoa_setDtr = 0xff;
1751 msg.hskoa_dtr = p_priv->dtr_state;
1753 p_priv->resend_cont = 0;
1754 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1756 /* send the data out the device on control endpoint */
1757 this_urb->transfer_buffer_length = sizeof(msg);
1759 this_urb->dev = serial->dev;
1760 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1761 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
1763 #if 0
1764 else {
1765 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__
1766 outcont_urb, this_urb->transfer_buffer_length,
1767 usb_pipeendpoint(this_urb->pipe));
1769 #endif
1771 return (0);
1774 static int keyspan_usa28_send_setup(struct usb_serial *serial,
1775 struct usb_serial_port *port,
1776 int reset_port)
1778 struct keyspan_usa28_portControlMessage msg;
1779 struct keyspan_serial_private *s_priv;
1780 struct keyspan_port_private *p_priv;
1781 const struct keyspan_device_details *d_details;
1782 struct urb *this_urb;
1783 int device_port, err;
1785 dbg ("%s", __FUNCTION__);
1787 s_priv = usb_get_serial_data(serial);
1788 p_priv = usb_get_serial_port_data(port);
1789 d_details = s_priv->device_details;
1790 device_port = port->number - port->serial->minor;
1792 /* only do something if we have a bulk out endpoint */
1793 if ((this_urb = p_priv->outcont_urb) == NULL) {
1794 dbg("%s - oops no urb.", __FUNCTION__);
1795 return -1;
1798 /* Save reset port val for resend.
1799 Don't overwrite resend for close condition. */
1800 if (p_priv->resend_cont != 3)
1801 p_priv->resend_cont = reset_port + 1;
1802 if (this_urb->status == -EINPROGRESS) {
1803 dbg ("%s already writing", __FUNCTION__);
1804 mdelay(5);
1805 return(-1);
1808 memset(&msg, 0, sizeof (struct keyspan_usa28_portControlMessage));
1810 msg.setBaudRate = 1;
1811 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1812 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1813 dbg("%s - Invalid baud rate requested %d.", __FUNCTION__, p_priv->baud);
1814 msg.baudLo = 0xff;
1815 msg.baudHi = 0xb2; /* Values for 9600 baud */
1818 /* If parity is enabled, we must calculate it ourselves. */
1819 msg.parity = 0; /* XXX for now */
1821 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1822 msg.xonFlowControl = 0;
1824 /* Do handshaking outputs, DTR is inverted relative to RTS */
1825 msg.rts = p_priv->rts_state;
1826 msg.dtr = p_priv->dtr_state;
1828 msg.forwardingLength = 16;
1829 msg.forwardMs = 10;
1830 msg.breakThreshold = 45;
1831 msg.xonChar = 17;
1832 msg.xoffChar = 19;
1834 /*msg.returnStatus = 1;
1835 msg.resetDataToggle = 0xff;*/
1836 /* Opening port */
1837 if (reset_port == 1) {
1838 msg._txOn = 1;
1839 msg._txOff = 0;
1840 msg.txFlush = 0;
1841 msg.txForceXoff = 0;
1842 msg.txBreak = 0;
1843 msg.rxOn = 1;
1844 msg.rxOff = 0;
1845 msg.rxFlush = 1;
1846 msg.rxForward = 0;
1847 msg.returnStatus = 0;
1848 msg.resetDataToggle = 0xff;
1850 /* Closing port */
1851 else if (reset_port == 2) {
1852 msg._txOn = 0;
1853 msg._txOff = 1;
1854 msg.txFlush = 0;
1855 msg.txForceXoff = 0;
1856 msg.txBreak = 0;
1857 msg.rxOn = 0;
1858 msg.rxOff = 1;
1859 msg.rxFlush = 1;
1860 msg.rxForward = 0;
1861 msg.returnStatus = 0;
1862 msg.resetDataToggle = 0;
1864 /* Sending intermediate configs */
1865 else {
1866 msg._txOn = (! p_priv->break_on);
1867 msg._txOff = 0;
1868 msg.txFlush = 0;
1869 msg.txForceXoff = 0;
1870 msg.txBreak = (p_priv->break_on);
1871 msg.rxOn = 0;
1872 msg.rxOff = 0;
1873 msg.rxFlush = 0;
1874 msg.rxForward = 0;
1875 msg.returnStatus = 0;
1876 msg.resetDataToggle = 0x0;
1879 p_priv->resend_cont = 0;
1880 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
1882 /* send the data out the device on control endpoint */
1883 this_urb->transfer_buffer_length = sizeof(msg);
1885 this_urb->dev = serial->dev;
1886 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
1887 dbg("%s - usb_submit_urb(setup) failed", __FUNCTION__);
1889 #if 0
1890 else {
1891 dbg("%s - usb_submit_urb(setup) OK %d bytes", __FUNCTION__,
1892 this_urb->transfer_buffer_length);
1894 #endif
1896 return (0);
1899 static int keyspan_usa49_send_setup(struct usb_serial *serial,
1900 struct usb_serial_port *port,
1901 int reset_port)
1903 struct keyspan_usa49_portControlMessage msg;
1904 struct keyspan_serial_private *s_priv;
1905 struct keyspan_port_private *p_priv;
1906 const struct keyspan_device_details *d_details;
1907 int glocont_urb;
1908 struct urb *this_urb;
1909 int err, device_port;
1911 dbg ("%s", __FUNCTION__);
1913 s_priv = usb_get_serial_data(serial);
1914 p_priv = usb_get_serial_port_data(port);
1915 d_details = s_priv->device_details;
1917 glocont_urb = d_details->glocont_endpoint;
1918 this_urb = s_priv->glocont_urb;
1920 /* Work out which port within the device is being setup */
1921 device_port = port->number - port->serial->minor;
1923 dbg("%s - endpoint %d port %d (%d)",__FUNCTION__, usb_pipeendpoint(this_urb->pipe), port->number, device_port);
1925 /* Make sure we have an urb then send the message */
1926 if (this_urb == NULL) {
1927 dbg("%s - oops no urb for port %d.", __FUNCTION__, port->number);
1928 return -1;
1931 /* Save reset port val for resend.
1932 Don't overwrite resend for close condition. */
1933 if (p_priv->resend_cont != 3)
1934 p_priv->resend_cont = reset_port + 1;
1935 if (this_urb->status == -EINPROGRESS) {
1936 /* dbg ("%s - already writing", __FUNCTION__); */
1937 mdelay(5);
1938 return(-1);
1941 memset(&msg, 0, sizeof (struct keyspan_usa49_portControlMessage));
1943 /*msg.portNumber = port->number;*/
1944 msg.portNumber = device_port;
1946 /* Only set baud rate if it's changed */
1947 if (p_priv->old_baud != p_priv->baud) {
1948 p_priv->old_baud = p_priv->baud;
1949 msg.setClocking = 0xff;
1950 if (d_details->calculate_baud_rate
1951 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1952 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE ) {
1953 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
1954 p_priv->baud);
1955 msg.baudLo = 0;
1956 msg.baudHi = 125; /* Values for 9600 baud */
1957 msg.prescaler = 10;
1959 //msg.setPrescaler = 0xff;
1962 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1963 switch (p_priv->cflag & CSIZE) {
1964 case CS5:
1965 msg.lcr |= USA_DATABITS_5;
1966 break;
1967 case CS6:
1968 msg.lcr |= USA_DATABITS_6;
1969 break;
1970 case CS7:
1971 msg.lcr |= USA_DATABITS_7;
1972 break;
1973 case CS8:
1974 msg.lcr |= USA_DATABITS_8;
1975 break;
1977 if (p_priv->cflag & PARENB) {
1978 /* note USA_PARITY_NONE == 0 */
1979 msg.lcr |= (p_priv->cflag & PARODD)?
1980 USA_PARITY_ODD: USA_PARITY_EVEN;
1982 msg.setLcr = 0xff;
1984 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1985 msg.xonFlowControl = 0;
1986 msg.setFlowControl = 0xff;
1988 msg.forwardingLength = 16;
1989 msg.xonChar = 17;
1990 msg.xoffChar = 19;
1992 /* Opening port */
1993 if (reset_port == 1) {
1994 msg._txOn = 1;
1995 msg._txOff = 0;
1996 msg.txFlush = 0;
1997 msg.txBreak = 0;
1998 msg.rxOn = 1;
1999 msg.rxOff = 0;
2000 msg.rxFlush = 1;
2001 msg.rxForward = 0;
2002 msg.returnStatus = 0;
2003 msg.resetDataToggle = 0xff;
2004 msg.enablePort = 1;
2005 msg.disablePort = 0;
2007 /* Closing port */
2008 else if (reset_port == 2) {
2009 msg._txOn = 0;
2010 msg._txOff = 1;
2011 msg.txFlush = 0;
2012 msg.txBreak = 0;
2013 msg.rxOn = 0;
2014 msg.rxOff = 1;
2015 msg.rxFlush = 1;
2016 msg.rxForward = 0;
2017 msg.returnStatus = 0;
2018 msg.resetDataToggle = 0;
2019 msg.enablePort = 0;
2020 msg.disablePort = 1;
2022 /* Sending intermediate configs */
2023 else {
2024 msg._txOn = (! p_priv->break_on);
2025 msg._txOff = 0;
2026 msg.txFlush = 0;
2027 msg.txBreak = (p_priv->break_on);
2028 msg.rxOn = 0;
2029 msg.rxOff = 0;
2030 msg.rxFlush = 0;
2031 msg.rxForward = 0;
2032 msg.returnStatus = 0;
2033 msg.resetDataToggle = 0x0;
2034 msg.enablePort = 0;
2035 msg.disablePort = 0;
2038 /* Do handshaking outputs */
2039 msg.setRts = 0xff;
2040 msg.rts = p_priv->rts_state;
2042 msg.setDtr = 0xff;
2043 msg.dtr = p_priv->dtr_state;
2045 p_priv->resend_cont = 0;
2046 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2048 /* send the data out the device on control endpoint */
2049 this_urb->transfer_buffer_length = sizeof(msg);
2051 this_urb->dev = serial->dev;
2052 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2053 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2055 #if 0
2056 else {
2057 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __FUNCTION__,
2058 outcont_urb, this_urb->transfer_buffer_length,
2059 usb_pipeendpoint(this_urb->pipe));
2061 #endif
2063 return (0);
2066 static int keyspan_usa90_send_setup(struct usb_serial *serial,
2067 struct usb_serial_port *port,
2068 int reset_port)
2070 struct keyspan_usa90_portControlMessage msg;
2071 struct keyspan_serial_private *s_priv;
2072 struct keyspan_port_private *p_priv;
2073 const struct keyspan_device_details *d_details;
2074 struct urb *this_urb;
2075 int err;
2076 u8 prescaler;
2078 dbg ("%s", __FUNCTION__);
2080 s_priv = usb_get_serial_data(serial);
2081 p_priv = usb_get_serial_port_data(port);
2082 d_details = s_priv->device_details;
2084 /* only do something if we have a bulk out endpoint */
2085 if ((this_urb = p_priv->outcont_urb) == NULL) {
2086 dbg("%s - oops no urb.", __FUNCTION__);
2087 return -1;
2090 /* Save reset port val for resend.
2091 Don't overwrite resend for open/close condition. */
2092 if ((reset_port + 1) > p_priv->resend_cont)
2093 p_priv->resend_cont = reset_port + 1;
2094 if (this_urb->status == -EINPROGRESS) {
2095 dbg ("%s already writing", __FUNCTION__);
2096 mdelay(5);
2097 return(-1);
2100 memset(&msg, 0, sizeof (struct keyspan_usa90_portControlMessage));
2102 /* Only set baud rate if it's changed */
2103 if (p_priv->old_baud != p_priv->baud) {
2104 p_priv->old_baud = p_priv->baud;
2105 msg.setClocking = 0x01;
2106 if (d_details->calculate_baud_rate
2107 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2108 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE ) {
2109 dbg("%s - Invalid baud rate %d requested, using 9600.", __FUNCTION__,
2110 p_priv->baud);
2111 p_priv->baud = 9600;
2112 d_details->calculate_baud_rate (p_priv->baud, d_details->baudclk,
2113 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2115 msg.setRxMode = 1;
2116 msg.setTxMode = 1;
2119 /* modes must always be correctly specified */
2120 if (p_priv->baud > 57600)
2122 msg.rxMode = RXMODE_DMA;
2123 msg.txMode = TXMODE_DMA;
2125 else
2127 msg.rxMode = RXMODE_BYHAND;
2128 msg.txMode = TXMODE_BYHAND;
2131 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2132 switch (p_priv->cflag & CSIZE) {
2133 case CS5:
2134 msg.lcr |= USA_DATABITS_5;
2135 break;
2136 case CS6:
2137 msg.lcr |= USA_DATABITS_6;
2138 break;
2139 case CS7:
2140 msg.lcr |= USA_DATABITS_7;
2141 break;
2142 case CS8:
2143 msg.lcr |= USA_DATABITS_8;
2144 break;
2146 if (p_priv->cflag & PARENB) {
2147 /* note USA_PARITY_NONE == 0 */
2148 msg.lcr |= (p_priv->cflag & PARODD)?
2149 USA_PARITY_ODD: USA_PARITY_EVEN;
2151 if (p_priv->old_cflag != p_priv->cflag) {
2152 p_priv->old_cflag = p_priv->cflag;
2153 msg.setLcr = 0x01;
2156 if (p_priv->flow_control == flow_cts)
2157 msg.txFlowControl = TXFLOW_CTS;
2158 msg.setTxFlowControl = 0x01;
2159 msg.setRxFlowControl = 0x01;
2161 msg.rxForwardingLength = 16;
2162 msg.rxForwardingTimeout = 16;
2163 msg.txAckSetting = 0;
2164 msg.xonChar = 17;
2165 msg.xoffChar = 19;
2167 /* Opening port */
2168 if (reset_port == 1) {
2169 msg.portEnabled = 1;
2170 msg.rxFlush = 1;
2171 msg.txBreak = (p_priv->break_on);
2173 /* Closing port */
2174 else if (reset_port == 2) {
2175 msg.portEnabled = 0;
2177 /* Sending intermediate configs */
2178 else {
2179 if (port->open_count)
2180 msg.portEnabled = 1;
2181 msg.txBreak = (p_priv->break_on);
2184 /* Do handshaking outputs */
2185 msg.setRts = 0x01;
2186 msg.rts = p_priv->rts_state;
2188 msg.setDtr = 0x01;
2189 msg.dtr = p_priv->dtr_state;
2191 p_priv->resend_cont = 0;
2192 memcpy (this_urb->transfer_buffer, &msg, sizeof(msg));
2194 /* send the data out the device on control endpoint */
2195 this_urb->transfer_buffer_length = sizeof(msg);
2197 this_urb->dev = serial->dev;
2198 if ((err = usb_submit_urb(this_urb, GFP_ATOMIC)) != 0) {
2199 dbg("%s - usb_submit_urb(setup) failed (%d)", __FUNCTION__, err);
2201 return (0);
2204 static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2206 struct usb_serial *serial = port->serial;
2207 struct keyspan_serial_private *s_priv;
2208 const struct keyspan_device_details *d_details;
2210 dbg ("%s", __FUNCTION__);
2212 s_priv = usb_get_serial_data(serial);
2213 d_details = s_priv->device_details;
2215 switch (d_details->msg_format) {
2216 case msg_usa26:
2217 keyspan_usa26_send_setup(serial, port, reset_port);
2218 break;
2219 case msg_usa28:
2220 keyspan_usa28_send_setup(serial, port, reset_port);
2221 break;
2222 case msg_usa49:
2223 keyspan_usa49_send_setup(serial, port, reset_port);
2224 break;
2225 case msg_usa90:
2226 keyspan_usa90_send_setup(serial, port, reset_port);
2227 break;
2232 /* Gets called by the "real" driver (ie once firmware is loaded
2233 and renumeration has taken place. */
2234 static int keyspan_startup (struct usb_serial *serial)
2236 int i, err;
2237 struct usb_serial_port *port;
2238 struct keyspan_serial_private *s_priv;
2239 struct keyspan_port_private *p_priv;
2240 const struct keyspan_device_details *d_details;
2242 dbg("%s", __FUNCTION__);
2244 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2245 if (d_details->product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
2246 break;
2247 if (d_details == NULL) {
2248 dev_err(&serial->dev->dev, "%s - unknown product id %x\n", __FUNCTION__, le16_to_cpu(serial->dev->descriptor.idProduct));
2249 return 1;
2252 /* Setup private data for serial driver */
2253 s_priv = kmalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2254 if (!s_priv) {
2255 dbg("%s - kmalloc for keyspan_serial_private failed.", __FUNCTION__);
2256 return -ENOMEM;
2258 memset(s_priv, 0, sizeof(struct keyspan_serial_private));
2260 s_priv->device_details = d_details;
2261 usb_set_serial_data(serial, s_priv);
2263 /* Now setup per port private data */
2264 for (i = 0; i < serial->num_ports; i++) {
2265 port = serial->port[i];
2266 p_priv = kmalloc(sizeof(struct keyspan_port_private), GFP_KERNEL);
2267 if (!p_priv) {
2268 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __FUNCTION__, i);
2269 return (1);
2271 memset(p_priv, 0, sizeof(struct keyspan_port_private));
2272 p_priv->device_details = d_details;
2273 usb_set_serial_port_data(port, p_priv);
2276 keyspan_setup_urbs(serial);
2278 s_priv->instat_urb->dev = serial->dev;
2279 if ((err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL)) != 0) {
2280 dbg("%s - submit instat urb failed %d", __FUNCTION__, err);
2283 return (0);
2286 static void keyspan_shutdown (struct usb_serial *serial)
2288 int i, j;
2289 struct usb_serial_port *port;
2290 struct keyspan_serial_private *s_priv;
2291 struct keyspan_port_private *p_priv;
2293 dbg("%s", __FUNCTION__);
2295 s_priv = usb_get_serial_data(serial);
2297 /* Stop reading/writing urbs */
2298 stop_urb(s_priv->instat_urb);
2299 stop_urb(s_priv->glocont_urb);
2300 for (i = 0; i < serial->num_ports; ++i) {
2301 port = serial->port[i];
2302 p_priv = usb_get_serial_port_data(port);
2303 stop_urb(p_priv->inack_urb);
2304 stop_urb(p_priv->outcont_urb);
2305 for (j = 0; j < 2; j++) {
2306 stop_urb(p_priv->in_urbs[j]);
2307 stop_urb(p_priv->out_urbs[j]);
2311 /* Now free them */
2312 if (s_priv->instat_urb)
2313 usb_free_urb(s_priv->instat_urb);
2314 if (s_priv->glocont_urb)
2315 usb_free_urb(s_priv->glocont_urb);
2316 for (i = 0; i < serial->num_ports; ++i) {
2317 port = serial->port[i];
2318 p_priv = usb_get_serial_port_data(port);
2319 if (p_priv->inack_urb)
2320 usb_free_urb(p_priv->inack_urb);
2321 if (p_priv->outcont_urb)
2322 usb_free_urb(p_priv->outcont_urb);
2323 for (j = 0; j < 2; j++) {
2324 if (p_priv->in_urbs[j])
2325 usb_free_urb(p_priv->in_urbs[j]);
2326 if (p_priv->out_urbs[j])
2327 usb_free_urb(p_priv->out_urbs[j]);
2331 /* dbg("Freeing serial->private."); */
2332 kfree(s_priv);
2334 /* dbg("Freeing port->private."); */
2335 /* Now free per port private data */
2336 for (i = 0; i < serial->num_ports; i++) {
2337 port = serial->port[i];
2338 kfree(usb_get_serial_port_data(port));
2342 MODULE_AUTHOR( DRIVER_AUTHOR );
2343 MODULE_DESCRIPTION( DRIVER_DESC );
2344 MODULE_LICENSE("GPL");
2346 module_param(debug, bool, S_IRUGO | S_IWUSR);
2347 MODULE_PARM_DESC(debug, "Debug enabled or not");