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.
21 Thanks to Paulus for miscellaneous tidy ups, some largish chunks
22 of much nicer and/or completely new code and (perhaps most uniquely)
23 having the patience to sit down and explain why and where he'd changed
26 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
27 staff in their work on open source projects.
31 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
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
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>
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 */
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 */
173 unsigned int old_cflag
;
174 enum {flow_none
, flow_cts
, flow_xon
} flow_control
;
175 int rts_state
; /* Handshaking pins (outputs) */
177 int cts_state
; /* Handshaking pins (inputs) */
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)
202 retval
= usb_serial_register(&keyspan_pre_device
);
204 goto failed_pre_device_register
;
205 retval
= usb_serial_register(&keyspan_1port_device
);
207 goto failed_1port_device_register
;
208 retval
= usb_serial_register(&keyspan_2port_device
);
210 goto failed_2port_device_register
;
211 retval
= usb_serial_register(&keyspan_4port_device
);
213 goto failed_4port_device_register
;
214 retval
= usb_register(&keyspan_driver
);
216 goto failed_usb_register
;
218 info(DRIVER_VERSION
":" DRIVER_DESC
);
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
:
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", __func__
, port
->number
);
251 static void keyspan_rx_unthrottle (struct usb_serial_port
*port
)
253 dbg("%s - port %d", __func__
, port
->number
);
257 static void keyspan_break_ctl (struct usb_serial_port
*port
, int break_state
)
259 struct keyspan_port_private
*p_priv
;
263 p_priv
= usb_get_serial_port_data(port
);
265 if (break_state
== -1)
266 p_priv
->break_on
= 1;
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
;
281 struct tty_struct
*tty
= port
->tty
;
285 p_priv
= usb_get_serial_port_data(port
);
286 d_details
= p_priv
->device_details
;
287 cflag
= tty
->termios
->c_cflag
;
288 device_port
= port
->number
- port
->serial
->minor
;
290 /* Baud rate calculation takes baud rate as an integer
291 so other rates can be generated if desired. */
292 baud_rate
= tty_get_baud_rate(tty
);
293 /* If no match or invalid, don't change */
294 if (d_details
->calculate_baud_rate(baud_rate
, d_details
->baudclk
,
295 NULL
, NULL
, NULL
, device_port
) == KEYSPAN_BAUD_RATE_OK
) {
296 /* FIXME - more to do here to ensure rate changes cleanly */
297 /* FIXME - calcuate exact rate from divisor ? */
298 p_priv
->baud
= baud_rate
;
300 baud_rate
= tty_termios_baud_rate(old_termios
);
302 tty_encode_baud_rate(tty
, baud_rate
, baud_rate
);
303 /* set CTS/RTS handshake etc. */
304 p_priv
->cflag
= cflag
;
305 p_priv
->flow_control
= (cflag
& CRTSCTS
)? flow_cts
: flow_none
;
307 /* Mark/Space not supported */
308 tty
->termios
->c_cflag
&= ~CMSPAR
;
310 keyspan_send_setup(port
, 0);
313 static int keyspan_tiocmget(struct usb_serial_port
*port
, struct file
*file
)
316 struct keyspan_port_private
*p_priv
;
318 p_priv
= usb_get_serial_port_data(port
);
320 value
= ((p_priv
->rts_state
) ? TIOCM_RTS
: 0) |
321 ((p_priv
->dtr_state
) ? TIOCM_DTR
: 0) |
322 ((p_priv
->cts_state
) ? TIOCM_CTS
: 0) |
323 ((p_priv
->dsr_state
) ? TIOCM_DSR
: 0) |
324 ((p_priv
->dcd_state
) ? TIOCM_CAR
: 0) |
325 ((p_priv
->ri_state
) ? TIOCM_RNG
: 0);
330 static int keyspan_tiocmset(struct usb_serial_port
*port
, struct file
*file
,
331 unsigned int set
, unsigned int clear
)
333 struct keyspan_port_private
*p_priv
;
335 p_priv
= usb_get_serial_port_data(port
);
338 p_priv
->rts_state
= 1;
340 p_priv
->dtr_state
= 1;
342 if (clear
& TIOCM_RTS
)
343 p_priv
->rts_state
= 0;
344 if (clear
& TIOCM_DTR
)
345 p_priv
->dtr_state
= 0;
346 keyspan_send_setup(port
, 0);
350 static int keyspan_ioctl(struct usb_serial_port
*port
, struct file
*file
,
351 unsigned int cmd
, unsigned long arg
)
356 /* Write function is similar for the four protocols used
357 with only a minor change for usa90 (usa19hs) required */
358 static int keyspan_write(struct usb_serial_port
*port
,
359 const unsigned char *buf
, int count
)
361 struct keyspan_port_private
*p_priv
;
362 const struct keyspan_device_details
*d_details
;
365 struct urb
*this_urb
;
366 int err
, maxDataLen
, dataOffset
;
368 p_priv
= usb_get_serial_port_data(port
);
369 d_details
= p_priv
->device_details
;
371 if (d_details
->msg_format
== msg_usa90
) {
379 dbg("%s - for port %d (%d chars), flip=%d",
380 __func__
, port
->number
, count
, p_priv
->out_flip
);
382 for (left
= count
; left
> 0; left
-= todo
) {
384 if (todo
> maxDataLen
)
387 flip
= p_priv
->out_flip
;
389 /* Check we have a valid urb/endpoint before we use it... */
390 if ((this_urb
= p_priv
->out_urbs
[flip
]) == NULL
) {
391 /* no bulk out, so return 0 bytes written */
392 dbg("%s - no output urb :(", __func__
);
396 dbg("%s - endpoint %d flip %d", __func__
, usb_pipeendpoint(this_urb
->pipe
), flip
);
398 if (this_urb
->status
== -EINPROGRESS
) {
399 if (time_before(jiffies
, p_priv
->tx_start_time
[flip
] + 10 * HZ
))
401 usb_unlink_urb(this_urb
);
405 /* First byte in buffer is "last flag" (except for usa19hx) - unused so
406 for now so set to zero */
407 ((char *)this_urb
->transfer_buffer
)[0] = 0;
409 memcpy (this_urb
->transfer_buffer
+ dataOffset
, buf
, todo
);
412 /* send the data out the bulk port */
413 this_urb
->transfer_buffer_length
= todo
+ dataOffset
;
415 this_urb
->dev
= port
->serial
->dev
;
416 if ((err
= usb_submit_urb(this_urb
, GFP_ATOMIC
)) != 0) {
417 dbg("usb_submit_urb(write bulk) failed (%d)", err
);
419 p_priv
->tx_start_time
[flip
] = jiffies
;
421 /* Flip for next time if usa26 or usa28 interface
422 (not used on usa49) */
423 p_priv
->out_flip
= (flip
+ 1) & d_details
->outdat_endp_flip
;
429 static void usa26_indat_callback(struct urb
*urb
)
433 struct usb_serial_port
*port
;
434 struct tty_struct
*tty
;
435 unsigned char *data
= urb
->transfer_buffer
;
436 int status
= urb
->status
;
438 dbg ("%s", __func__
);
440 endpoint
= usb_pipeendpoint(urb
->pipe
);
443 dbg("%s - nonzero status: %x on endpoint %d.",
444 __func__
, status
, endpoint
);
450 if (tty
&& urb
->actual_length
) {
451 /* 0x80 bit is error flag */
452 if ((data
[0] & 0x80) == 0) {
453 /* no errors on individual bytes, only possible overrun err*/
454 if (data
[0] & RXERROR_OVERRUN
)
457 for (i
= 1; i
< urb
->actual_length
; ++i
) {
458 tty_insert_flip_char(tty
, data
[i
], err
);
461 /* some bytes had errors, every byte has status */
462 dbg("%s - RX error!!!!", __func__
);
463 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
464 int stat
= data
[i
], flag
= 0;
465 if (stat
& RXERROR_OVERRUN
)
467 if (stat
& RXERROR_FRAMING
)
469 if (stat
& RXERROR_PARITY
)
471 /* XXX should handle break (0x10) */
472 tty_insert_flip_char(tty
, data
[i
+1], flag
);
475 tty_flip_buffer_push(tty
);
478 /* Resubmit urb so we continue receiving */
479 urb
->dev
= port
->serial
->dev
;
480 if (port
->open_count
)
481 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0) {
482 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
487 /* Outdat handling is common for all devices */
488 static void usa2x_outdat_callback(struct urb
*urb
)
490 struct usb_serial_port
*port
;
491 struct keyspan_port_private
*p_priv
;
494 p_priv
= usb_get_serial_port_data(port
);
495 dbg ("%s - urb %d", __func__
, urb
== p_priv
->out_urbs
[1]);
497 if (port
->open_count
)
498 usb_serial_port_softint(port
);
501 static void usa26_inack_callback(struct urb
*urb
)
503 dbg ("%s", __func__
);
507 static void usa26_outcont_callback(struct urb
*urb
)
509 struct usb_serial_port
*port
;
510 struct keyspan_port_private
*p_priv
;
513 p_priv
= usb_get_serial_port_data(port
);
515 if (p_priv
->resend_cont
) {
516 dbg ("%s - sending setup", __func__
);
517 keyspan_usa26_send_setup(port
->serial
, port
, p_priv
->resend_cont
- 1);
521 static void usa26_instat_callback(struct urb
*urb
)
523 unsigned char *data
= urb
->transfer_buffer
;
524 struct keyspan_usa26_portStatusMessage
*msg
;
525 struct usb_serial
*serial
;
526 struct usb_serial_port
*port
;
527 struct keyspan_port_private
*p_priv
;
528 int old_dcd_state
, err
;
529 int status
= urb
->status
;
531 serial
= urb
->context
;
534 dbg("%s - nonzero status: %x", __func__
, status
);
537 if (urb
->actual_length
!= 9) {
538 dbg("%s - %d byte report??", __func__
, urb
->actual_length
);
542 msg
= (struct keyspan_usa26_portStatusMessage
*)data
;
545 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
546 __func__
, msg
->port
, msg
->hskia_cts
, msg
->gpia_dcd
, msg
->dsr
, msg
->ri
, msg
->_txOff
,
547 msg
->_txXoff
, msg
->rxEnabled
, msg
->controlResponse
);
550 /* Now do something useful with the data */
553 /* Check port number from message and retrieve private data */
554 if (msg
->port
>= serial
->num_ports
) {
555 dbg ("%s - Unexpected port number %d", __func__
, msg
->port
);
558 port
= serial
->port
[msg
->port
];
559 p_priv
= usb_get_serial_port_data(port
);
561 /* Update handshaking pin state information */
562 old_dcd_state
= p_priv
->dcd_state
;
563 p_priv
->cts_state
= ((msg
->hskia_cts
) ? 1 : 0);
564 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
565 p_priv
->dcd_state
= ((msg
->gpia_dcd
) ? 1 : 0);
566 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
568 if (port
->tty
&& !C_CLOCAL(port
->tty
)
569 && old_dcd_state
!= p_priv
->dcd_state
) {
571 tty_hangup(port
->tty
);
573 /* wake_up_interruptible(&p_priv->open_wait); */
576 /* Resubmit urb so we continue receiving */
577 urb
->dev
= serial
->dev
;
578 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0) {
579 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
584 static void usa26_glocont_callback(struct urb
*urb
)
586 dbg ("%s", __func__
);
591 static void usa28_indat_callback(struct urb
*urb
)
594 struct usb_serial_port
*port
;
595 struct tty_struct
*tty
;
597 struct keyspan_port_private
*p_priv
;
598 int status
= urb
->status
;
600 dbg ("%s", __func__
);
603 p_priv
= usb_get_serial_port_data(port
);
604 data
= urb
->transfer_buffer
;
606 if (urb
!= p_priv
->in_urbs
[p_priv
->in_flip
])
611 dbg("%s - nonzero status: %x on endpoint %d.",
612 __func__
, status
, usb_pipeendpoint(urb
->pipe
));
617 p_priv
= usb_get_serial_port_data(port
);
618 data
= urb
->transfer_buffer
;
621 if (urb
->actual_length
) {
622 for (i
= 0; i
< urb
->actual_length
; ++i
) {
623 tty_insert_flip_char(tty
, data
[i
], 0);
625 tty_flip_buffer_push(tty
);
628 /* Resubmit urb so we continue receiving */
629 urb
->dev
= port
->serial
->dev
;
630 if (port
->open_count
)
631 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0) {
632 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
634 p_priv
->in_flip
^= 1;
636 urb
= p_priv
->in_urbs
[p_priv
->in_flip
];
637 } while (urb
->status
!= -EINPROGRESS
);
640 static void usa28_inack_callback(struct urb
*urb
)
642 dbg ("%s", __func__
);
645 static void usa28_outcont_callback(struct urb
*urb
)
647 struct usb_serial_port
*port
;
648 struct keyspan_port_private
*p_priv
;
651 p_priv
= usb_get_serial_port_data(port
);
653 if (p_priv
->resend_cont
) {
654 dbg ("%s - sending setup", __func__
);
655 keyspan_usa28_send_setup(port
->serial
, port
, p_priv
->resend_cont
- 1);
659 static void usa28_instat_callback(struct urb
*urb
)
662 unsigned char *data
= urb
->transfer_buffer
;
663 struct keyspan_usa28_portStatusMessage
*msg
;
664 struct usb_serial
*serial
;
665 struct usb_serial_port
*port
;
666 struct keyspan_port_private
*p_priv
;
668 int status
= urb
->status
;
670 serial
= urb
->context
;
673 dbg("%s - nonzero status: %x", __func__
, status
);
677 if (urb
->actual_length
!= sizeof(struct keyspan_usa28_portStatusMessage
)) {
678 dbg("%s - bad length %d", __func__
, urb
->actual_length
);
682 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
683 data[0], data[1], data[2], data[3], data[4], data[5],
684 data[6], data[7], data[8], data[9], data[10], data[11]);*/
686 /* Now do something useful with the data */
687 msg
= (struct keyspan_usa28_portStatusMessage
*)data
;
690 /* Check port number from message and retrieve private data */
691 if (msg
->port
>= serial
->num_ports
) {
692 dbg ("%s - Unexpected port number %d", __func__
, msg
->port
);
695 port
= serial
->port
[msg
->port
];
696 p_priv
= usb_get_serial_port_data(port
);
698 /* Update handshaking pin state information */
699 old_dcd_state
= p_priv
->dcd_state
;
700 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
701 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
702 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
703 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
705 if (port
->tty
&& !C_CLOCAL(port
->tty
)
706 && old_dcd_state
!= p_priv
->dcd_state
) {
708 tty_hangup(port
->tty
);
710 /* wake_up_interruptible(&p_priv->open_wait); */
713 /* Resubmit urb so we continue receiving */
714 urb
->dev
= serial
->dev
;
715 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0) {
716 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
721 static void usa28_glocont_callback(struct urb
*urb
)
723 dbg ("%s", __func__
);
727 static void usa49_glocont_callback(struct urb
*urb
)
729 struct usb_serial
*serial
;
730 struct usb_serial_port
*port
;
731 struct keyspan_port_private
*p_priv
;
734 dbg ("%s", __func__
);
736 serial
= urb
->context
;
737 for (i
= 0; i
< serial
->num_ports
; ++i
) {
738 port
= serial
->port
[i
];
739 p_priv
= usb_get_serial_port_data(port
);
741 if (p_priv
->resend_cont
) {
742 dbg ("%s - sending setup", __func__
);
743 keyspan_usa49_send_setup(serial
, port
, p_priv
->resend_cont
- 1);
749 /* This is actually called glostat in the Keyspan
751 static void usa49_instat_callback(struct urb
*urb
)
754 unsigned char *data
= urb
->transfer_buffer
;
755 struct keyspan_usa49_portStatusMessage
*msg
;
756 struct usb_serial
*serial
;
757 struct usb_serial_port
*port
;
758 struct keyspan_port_private
*p_priv
;
760 int status
= urb
->status
;
762 dbg ("%s", __func__
);
764 serial
= urb
->context
;
767 dbg("%s - nonzero status: %x", __func__
, status
);
771 if (urb
->actual_length
!= sizeof(struct keyspan_usa49_portStatusMessage
)) {
772 dbg("%s - bad length %d", __func__
, urb
->actual_length
);
776 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
777 data[0], data[1], data[2], data[3], data[4], data[5],
778 data[6], data[7], data[8], data[9], data[10]);*/
780 /* Now do something useful with the data */
781 msg
= (struct keyspan_usa49_portStatusMessage
*)data
;
783 /* Check port number from message and retrieve private data */
784 if (msg
->portNumber
>= serial
->num_ports
) {
785 dbg ("%s - Unexpected port number %d", __func__
, msg
->portNumber
);
788 port
= serial
->port
[msg
->portNumber
];
789 p_priv
= usb_get_serial_port_data(port
);
791 /* Update handshaking pin state information */
792 old_dcd_state
= p_priv
->dcd_state
;
793 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
794 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
795 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
796 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
798 if (port
->tty
&& !C_CLOCAL(port
->tty
)
799 && old_dcd_state
!= p_priv
->dcd_state
) {
801 tty_hangup(port
->tty
);
803 /* wake_up_interruptible(&p_priv->open_wait); */
806 /* Resubmit urb so we continue receiving */
807 urb
->dev
= serial
->dev
;
809 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0) {
810 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
815 static void usa49_inack_callback(struct urb
*urb
)
817 dbg ("%s", __func__
);
820 static void usa49_indat_callback(struct urb
*urb
)
824 struct usb_serial_port
*port
;
825 struct tty_struct
*tty
;
826 unsigned char *data
= urb
->transfer_buffer
;
827 int status
= urb
->status
;
829 dbg ("%s", __func__
);
831 endpoint
= usb_pipeendpoint(urb
->pipe
);
834 dbg("%s - nonzero status: %x on endpoint %d.", __func__
,
841 if (tty
&& urb
->actual_length
) {
842 /* 0x80 bit is error flag */
843 if ((data
[0] & 0x80) == 0) {
844 /* no error on any byte */
845 for (i
= 1; i
< urb
->actual_length
; ++i
) {
846 tty_insert_flip_char(tty
, data
[i
], 0);
849 /* some bytes had errors, every byte has status */
850 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
851 int stat
= data
[i
], flag
= 0;
852 if (stat
& RXERROR_OVERRUN
)
854 if (stat
& RXERROR_FRAMING
)
856 if (stat
& RXERROR_PARITY
)
858 /* XXX should handle break (0x10) */
859 tty_insert_flip_char(tty
, data
[i
+1], flag
);
862 tty_flip_buffer_push(tty
);
865 /* Resubmit urb so we continue receiving */
866 urb
->dev
= port
->serial
->dev
;
867 if (port
->open_count
)
868 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0) {
869 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
873 static void usa49wg_indat_callback(struct urb
*urb
)
876 struct usb_serial
*serial
;
877 struct usb_serial_port
*port
;
878 struct tty_struct
*tty
;
879 unsigned char *data
= urb
->transfer_buffer
;
880 int status
= urb
->status
;
882 dbg ("%s", __func__
);
884 serial
= urb
->context
;
887 dbg("%s - nonzero status: %x", __func__
, status
);
891 /* inbound data is in the form P#, len, status, data */
895 if (urb
->actual_length
) {
896 while (i
< urb
->actual_length
) {
898 /* Check port number from message*/
899 if (data
[i
] >= serial
->num_ports
) {
900 dbg ("%s - Unexpected port number %d",
904 port
= serial
->port
[data
[i
++]];
908 /* 0x80 bit is error flag */
909 if ((data
[i
] & 0x80) == 0) {
910 /* no error on any byte */
912 for (x
= 1; x
< len
; ++x
)
913 if (port
->open_count
)
914 tty_insert_flip_char(tty
,
920 * some bytes had errors, every byte has status
922 for (x
= 0; x
+ 1 < len
; x
+= 2) {
923 int stat
= data
[i
], flag
= 0;
924 if (stat
& RXERROR_OVERRUN
)
926 if (stat
& RXERROR_FRAMING
)
928 if (stat
& RXERROR_PARITY
)
930 /* XXX should handle break (0x10) */
931 if (port
->open_count
)
932 tty_insert_flip_char(tty
,
937 if (port
->open_count
)
938 tty_flip_buffer_push(tty
);
942 /* Resubmit urb so we continue receiving */
943 urb
->dev
= serial
->dev
;
945 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
947 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
950 /* not used, usa-49 doesn't have per-port control endpoints */
951 static void usa49_outcont_callback(struct urb
*urb
)
953 dbg ("%s", __func__
);
956 static void usa90_indat_callback(struct urb
*urb
)
960 struct usb_serial_port
*port
;
961 struct keyspan_port_private
*p_priv
;
962 struct tty_struct
*tty
;
963 unsigned char *data
= urb
->transfer_buffer
;
964 int status
= urb
->status
;
966 dbg ("%s", __func__
);
968 endpoint
= usb_pipeendpoint(urb
->pipe
);
971 dbg("%s - nonzero status: %x on endpoint %d.",
972 __func__
, status
, endpoint
);
977 p_priv
= usb_get_serial_port_data(port
);
980 if (urb
->actual_length
) {
982 /* if current mode is DMA, looks like usa28 format
983 otherwise looks like usa26 data format */
985 if (p_priv
->baud
> 57600) {
986 for (i
= 0; i
< urb
->actual_length
; ++i
)
987 tty_insert_flip_char(tty
, data
[i
], 0);
991 /* 0x80 bit is error flag */
992 if ((data
[0] & 0x80) == 0) {
993 /* no errors on individual bytes, only possible overrun err*/
994 if (data
[0] & RXERROR_OVERRUN
)
997 for (i
= 1; i
< urb
->actual_length
; ++i
)
998 tty_insert_flip_char(tty
, data
[i
], err
);
1002 /* some bytes had errors, every byte has status */
1003 dbg("%s - RX error!!!!", __func__
);
1004 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
1005 int stat
= data
[i
], flag
= 0;
1006 if (stat
& RXERROR_OVERRUN
)
1007 flag
|= TTY_OVERRUN
;
1008 if (stat
& RXERROR_FRAMING
)
1010 if (stat
& RXERROR_PARITY
)
1012 /* XXX should handle break (0x10) */
1013 tty_insert_flip_char(tty
, data
[i
+1], flag
);
1017 tty_flip_buffer_push(tty
);
1020 /* Resubmit urb so we continue receiving */
1021 urb
->dev
= port
->serial
->dev
;
1022 if (port
->open_count
)
1023 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0) {
1024 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
1030 static void usa90_instat_callback(struct urb
*urb
)
1032 unsigned char *data
= urb
->transfer_buffer
;
1033 struct keyspan_usa90_portStatusMessage
*msg
;
1034 struct usb_serial
*serial
;
1035 struct usb_serial_port
*port
;
1036 struct keyspan_port_private
*p_priv
;
1037 int old_dcd_state
, err
;
1038 int status
= urb
->status
;
1040 serial
= urb
->context
;
1043 dbg("%s - nonzero status: %x", __func__
, status
);
1046 if (urb
->actual_length
< 14) {
1047 dbg("%s - %d byte report??", __func__
, urb
->actual_length
);
1051 msg
= (struct keyspan_usa90_portStatusMessage
*)data
;
1053 /* Now do something useful with the data */
1055 port
= serial
->port
[0];
1056 p_priv
= usb_get_serial_port_data(port
);
1058 /* Update handshaking pin state information */
1059 old_dcd_state
= p_priv
->dcd_state
;
1060 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
1061 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
1062 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
1063 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
1065 if (port
->tty
&& !C_CLOCAL(port
->tty
)
1066 && old_dcd_state
!= p_priv
->dcd_state
) {
1068 tty_hangup(port
->tty
);
1070 /* wake_up_interruptible(&p_priv->open_wait); */
1073 /* Resubmit urb so we continue receiving */
1074 urb
->dev
= serial
->dev
;
1075 if ((err
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0) {
1076 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
1082 static void usa90_outcont_callback(struct urb
*urb
)
1084 struct usb_serial_port
*port
;
1085 struct keyspan_port_private
*p_priv
;
1087 port
= urb
->context
;
1088 p_priv
= usb_get_serial_port_data(port
);
1090 if (p_priv
->resend_cont
) {
1091 dbg ("%s - sending setup", __func__
);
1092 keyspan_usa90_send_setup(port
->serial
, port
, p_priv
->resend_cont
- 1);
1096 /* Status messages from the 28xg */
1097 static void usa67_instat_callback(struct urb
*urb
)
1100 unsigned char *data
= urb
->transfer_buffer
;
1101 struct keyspan_usa67_portStatusMessage
*msg
;
1102 struct usb_serial
*serial
;
1103 struct usb_serial_port
*port
;
1104 struct keyspan_port_private
*p_priv
;
1106 int status
= urb
->status
;
1108 dbg ("%s", __func__
);
1110 serial
= urb
->context
;
1113 dbg("%s - nonzero status: %x", __func__
, status
);
1117 if (urb
->actual_length
!= sizeof(struct keyspan_usa67_portStatusMessage
)) {
1118 dbg("%s - bad length %d", __func__
, urb
->actual_length
);
1123 /* Now do something useful with the data */
1124 msg
= (struct keyspan_usa67_portStatusMessage
*)data
;
1126 /* Check port number from message and retrieve private data */
1127 if (msg
->port
>= serial
->num_ports
) {
1128 dbg ("%s - Unexpected port number %d", __func__
, msg
->port
);
1132 port
= serial
->port
[msg
->port
];
1133 p_priv
= usb_get_serial_port_data(port
);
1135 /* Update handshaking pin state information */
1136 old_dcd_state
= p_priv
->dcd_state
;
1137 p_priv
->cts_state
= ((msg
->hskia_cts
) ? 1 : 0);
1138 p_priv
->dcd_state
= ((msg
->gpia_dcd
) ? 1 : 0);
1140 if (port
->tty
&& !C_CLOCAL(port
->tty
)
1141 && old_dcd_state
!= p_priv
->dcd_state
) {
1143 tty_hangup(port
->tty
);
1145 /* wake_up_interruptible(&p_priv->open_wait); */
1148 /* Resubmit urb so we continue receiving */
1149 urb
->dev
= serial
->dev
;
1150 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1152 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
1155 static void usa67_glocont_callback(struct urb
*urb
)
1157 struct usb_serial
*serial
;
1158 struct usb_serial_port
*port
;
1159 struct keyspan_port_private
*p_priv
;
1162 dbg ("%s", __func__
);
1164 serial
= urb
->context
;
1165 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1166 port
= serial
->port
[i
];
1167 p_priv
= usb_get_serial_port_data(port
);
1169 if (p_priv
->resend_cont
) {
1170 dbg ("%s - sending setup", __func__
);
1171 keyspan_usa67_send_setup(serial
, port
,
1172 p_priv
->resend_cont
- 1);
1178 static int keyspan_write_room (struct usb_serial_port
*port
)
1180 struct keyspan_port_private
*p_priv
;
1181 const struct keyspan_device_details
*d_details
;
1184 struct urb
*this_urb
;
1186 dbg("%s", __func__
);
1187 p_priv
= usb_get_serial_port_data(port
);
1188 d_details
= p_priv
->device_details
;
1190 /* FIXME: locking */
1191 if (d_details
->msg_format
== msg_usa90
)
1196 flip
= p_priv
->out_flip
;
1198 /* Check both endpoints to see if any are available. */
1199 if ((this_urb
= p_priv
->out_urbs
[flip
]) != NULL
) {
1200 if (this_urb
->status
!= -EINPROGRESS
)
1202 flip
= (flip
+ 1) & d_details
->outdat_endp_flip
;
1203 if ((this_urb
= p_priv
->out_urbs
[flip
]) != NULL
)
1204 if (this_urb
->status
!= -EINPROGRESS
)
1211 static int keyspan_chars_in_buffer (struct usb_serial_port
*port
)
1217 static int keyspan_open (struct usb_serial_port
*port
, struct file
*filp
)
1219 struct keyspan_port_private
*p_priv
;
1220 struct keyspan_serial_private
*s_priv
;
1221 struct usb_serial
*serial
= port
->serial
;
1222 const struct keyspan_device_details
*d_details
;
1224 int baud_rate
, device_port
;
1228 s_priv
= usb_get_serial_data(serial
);
1229 p_priv
= usb_get_serial_port_data(port
);
1230 d_details
= p_priv
->device_details
;
1232 dbg("%s - port%d.", __func__
, port
->number
);
1234 /* Set some sane defaults */
1235 p_priv
->rts_state
= 1;
1236 p_priv
->dtr_state
= 1;
1237 p_priv
->baud
= 9600;
1239 /* force baud and lcr to be set on open */
1240 p_priv
->old_baud
= 0;
1241 p_priv
->old_cflag
= 0;
1243 p_priv
->out_flip
= 0;
1244 p_priv
->in_flip
= 0;
1246 /* Reset low level data toggle and start reading from endpoints */
1247 for (i
= 0; i
< 2; i
++) {
1248 if ((urb
= p_priv
->in_urbs
[i
]) == NULL
)
1250 urb
->dev
= serial
->dev
;
1252 /* make sure endpoint data toggle is synchronized with the device */
1254 usb_clear_halt(urb
->dev
, urb
->pipe
);
1256 if ((err
= usb_submit_urb(urb
, GFP_KERNEL
)) != 0) {
1257 dbg("%s - submit urb %d failed (%d)", __func__
, i
, err
);
1261 /* Reset low level data toggle on out endpoints */
1262 for (i
= 0; i
< 2; i
++) {
1263 if ((urb
= p_priv
->out_urbs
[i
]) == NULL
)
1265 urb
->dev
= serial
->dev
;
1266 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe), usb_pipeout(urb->pipe), 0); */
1269 /* get the terminal config for the setup message now so we don't
1270 * need to send 2 of them */
1272 cflag
= port
->tty
->termios
->c_cflag
;
1273 device_port
= port
->number
- port
->serial
->minor
;
1275 /* Baud rate calculation takes baud rate as an integer
1276 so other rates can be generated if desired. */
1277 baud_rate
= tty_get_baud_rate(port
->tty
);
1278 /* If no match or invalid, leave as default */
1280 && d_details
->calculate_baud_rate(baud_rate
, d_details
->baudclk
,
1281 NULL
, NULL
, NULL
, device_port
) == KEYSPAN_BAUD_RATE_OK
) {
1282 p_priv
->baud
= baud_rate
;
1285 /* set CTS/RTS handshake etc. */
1286 p_priv
->cflag
= cflag
;
1287 p_priv
->flow_control
= (cflag
& CRTSCTS
)? flow_cts
: flow_none
;
1289 keyspan_send_setup(port
, 1);
1291 //keyspan_set_termios(port, NULL);
1296 static inline void stop_urb(struct urb
*urb
)
1298 if (urb
&& urb
->status
== -EINPROGRESS
)
1302 static void keyspan_close(struct usb_serial_port
*port
, struct file
*filp
)
1305 struct usb_serial
*serial
= port
->serial
;
1306 struct keyspan_serial_private
*s_priv
;
1307 struct keyspan_port_private
*p_priv
;
1309 dbg("%s", __func__
);
1310 s_priv
= usb_get_serial_data(serial
);
1311 p_priv
= usb_get_serial_port_data(port
);
1313 p_priv
->rts_state
= 0;
1314 p_priv
->dtr_state
= 0;
1317 keyspan_send_setup(port
, 2);
1318 /* pilot-xfer seems to work best with this delay */
1320 // keyspan_set_termios(port, NULL);
1323 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1324 dbg("%s - urb in progress", __func__);
1327 p_priv
->out_flip
= 0;
1328 p_priv
->in_flip
= 0;
1331 /* Stop reading/writing urbs */
1332 stop_urb(p_priv
->inack_urb
);
1333 /* stop_urb(p_priv->outcont_urb); */
1334 for (i
= 0; i
< 2; i
++) {
1335 stop_urb(p_priv
->in_urbs
[i
]);
1336 stop_urb(p_priv
->out_urbs
[i
]);
1343 /* download the firmware to a pre-renumeration device */
1344 static int keyspan_fake_startup (struct usb_serial
*serial
)
1347 const struct ezusb_hex_record
*record
;
1350 dbg("Keyspan startup version %04x product %04x",
1351 le16_to_cpu(serial
->dev
->descriptor
.bcdDevice
),
1352 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
1354 if ((le16_to_cpu(serial
->dev
->descriptor
.bcdDevice
) & 0x8000) != 0x8000) {
1355 dbg("Firmware already loaded. Quitting.");
1359 /* Select firmware image on the basis of idProduct */
1360 switch (le16_to_cpu(serial
->dev
->descriptor
.idProduct
)) {
1361 case keyspan_usa28_pre_product_id
:
1362 record
= &keyspan_usa28_firmware
[0];
1366 case keyspan_usa28x_pre_product_id
:
1367 record
= &keyspan_usa28x_firmware
[0];
1371 case keyspan_usa28xa_pre_product_id
:
1372 record
= &keyspan_usa28xa_firmware
[0];
1373 fw_name
= "USA28XA";
1376 case keyspan_usa28xb_pre_product_id
:
1377 record
= &keyspan_usa28xb_firmware
[0];
1378 fw_name
= "USA28XB";
1381 case keyspan_usa19_pre_product_id
:
1382 record
= &keyspan_usa19_firmware
[0];
1386 case keyspan_usa19qi_pre_product_id
:
1387 record
= &keyspan_usa19qi_firmware
[0];
1388 fw_name
= "USA19QI";
1391 case keyspan_mpr_pre_product_id
:
1392 record
= &keyspan_mpr_firmware
[0];
1396 case keyspan_usa19qw_pre_product_id
:
1397 record
= &keyspan_usa19qw_firmware
[0];
1398 fw_name
= "USA19QI";
1401 case keyspan_usa18x_pre_product_id
:
1402 record
= &keyspan_usa18x_firmware
[0];
1406 case keyspan_usa19w_pre_product_id
:
1407 record
= &keyspan_usa19w_firmware
[0];
1411 case keyspan_usa49w_pre_product_id
:
1412 record
= &keyspan_usa49w_firmware
[0];
1416 case keyspan_usa49wlc_pre_product_id
:
1417 record
= &keyspan_usa49wlc_firmware
[0];
1418 fw_name
= "USA49WLC";
1423 fw_name
= "Unknown";
1427 if (record
== NULL
) {
1428 dev_err(&serial
->dev
->dev
, "Required keyspan firmware image (%s) unavailable.\n", fw_name
);
1432 dbg("Uploading Keyspan %s firmware.", fw_name
);
1434 /* download the firmware image */
1435 response
= ezusb_set_reset(serial
, 1);
1437 while(record
->address
!= 0xffff) {
1438 response
= ezusb_writememory(serial
, record
->address
,
1439 (unsigned char *)record
->data
,
1440 record
->data_size
, 0xa0);
1442 dev_err(&serial
->dev
->dev
, "ezusb_writememory failed for Keyspan"
1443 "firmware (%d %04X %p %d)\n",
1445 record
->address
, record
->data
, record
->data_size
);
1450 /* bring device out of reset. Renumeration will occur in a
1451 moment and the new device will bind to the real driver */
1452 response
= ezusb_set_reset(serial
, 0);
1454 /* we don't want this device to have a driver assigned to it. */
1458 /* Helper functions used by keyspan_setup_urbs */
1459 static struct usb_endpoint_descriptor
const *find_ep(struct usb_serial
const *serial
,
1462 struct usb_host_interface
*iface_desc
;
1463 struct usb_endpoint_descriptor
*ep
;
1466 iface_desc
= serial
->interface
->cur_altsetting
;
1467 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1468 ep
= &iface_desc
->endpoint
[i
].desc
;
1469 if (ep
->bEndpointAddress
== endpoint
)
1472 dev_warn(&serial
->interface
->dev
, "found no endpoint descriptor for "
1473 "endpoint %x\n", endpoint
);
1477 static struct urb
*keyspan_setup_urb (struct usb_serial
*serial
, int endpoint
,
1478 int dir
, void *ctx
, char *buf
, int len
,
1479 void (*callback
)(struct urb
*))
1482 struct usb_endpoint_descriptor
const *ep_desc
;
1483 char const *ep_type_name
;
1486 return NULL
; /* endpoint not needed */
1488 dbg ("%s - alloc for endpoint %d.", __func__
, endpoint
);
1489 urb
= usb_alloc_urb(0, GFP_KERNEL
); /* No ISO */
1491 dbg ("%s - alloc for endpoint %d failed.", __func__
, endpoint
);
1495 if (endpoint
== 0) {
1496 /* control EP filled in when used */
1500 ep_desc
= find_ep(serial
, endpoint
);
1502 /* leak the urb, something's wrong and the callers don't care */
1505 if (usb_endpoint_xfer_int(ep_desc
)) {
1506 ep_type_name
= "INT";
1507 usb_fill_int_urb(urb
, serial
->dev
,
1508 usb_sndintpipe(serial
->dev
, endpoint
) | dir
,
1509 buf
, len
, callback
, ctx
,
1510 ep_desc
->bInterval
);
1511 } else if (usb_endpoint_xfer_bulk(ep_desc
)) {
1512 ep_type_name
= "BULK";
1513 usb_fill_bulk_urb(urb
, serial
->dev
,
1514 usb_sndbulkpipe(serial
->dev
, endpoint
) | dir
,
1515 buf
, len
, callback
, ctx
);
1517 dev_warn(&serial
->interface
->dev
,
1518 "unsupported endpoint type %x\n",
1519 ep_desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
1524 dbg("%s - using urb %p for %s endpoint %x",
1525 __func__
, urb
, ep_type_name
, endpoint
);
1529 static struct callbacks
{
1530 void (*instat_callback
)(struct urb
*);
1531 void (*glocont_callback
)(struct urb
*);
1532 void (*indat_callback
)(struct urb
*);
1533 void (*outdat_callback
)(struct urb
*);
1534 void (*inack_callback
)(struct urb
*);
1535 void (*outcont_callback
)(struct urb
*);
1536 } keyspan_callbacks
[] = {
1538 /* msg_usa26 callbacks */
1539 .instat_callback
= usa26_instat_callback
,
1540 .glocont_callback
= usa26_glocont_callback
,
1541 .indat_callback
= usa26_indat_callback
,
1542 .outdat_callback
= usa2x_outdat_callback
,
1543 .inack_callback
= usa26_inack_callback
,
1544 .outcont_callback
= usa26_outcont_callback
,
1546 /* msg_usa28 callbacks */
1547 .instat_callback
= usa28_instat_callback
,
1548 .glocont_callback
= usa28_glocont_callback
,
1549 .indat_callback
= usa28_indat_callback
,
1550 .outdat_callback
= usa2x_outdat_callback
,
1551 .inack_callback
= usa28_inack_callback
,
1552 .outcont_callback
= usa28_outcont_callback
,
1554 /* msg_usa49 callbacks */
1555 .instat_callback
= usa49_instat_callback
,
1556 .glocont_callback
= usa49_glocont_callback
,
1557 .indat_callback
= usa49_indat_callback
,
1558 .outdat_callback
= usa2x_outdat_callback
,
1559 .inack_callback
= usa49_inack_callback
,
1560 .outcont_callback
= usa49_outcont_callback
,
1562 /* msg_usa90 callbacks */
1563 .instat_callback
= usa90_instat_callback
,
1564 .glocont_callback
= usa28_glocont_callback
,
1565 .indat_callback
= usa90_indat_callback
,
1566 .outdat_callback
= usa2x_outdat_callback
,
1567 .inack_callback
= usa28_inack_callback
,
1568 .outcont_callback
= usa90_outcont_callback
,
1570 /* msg_usa67 callbacks */
1571 .instat_callback
= usa67_instat_callback
,
1572 .glocont_callback
= usa67_glocont_callback
,
1573 .indat_callback
= usa26_indat_callback
,
1574 .outdat_callback
= usa2x_outdat_callback
,
1575 .inack_callback
= usa26_inack_callback
,
1576 .outcont_callback
= usa26_outcont_callback
,
1580 /* Generic setup urbs function that uses
1581 data in device_details */
1582 static void keyspan_setup_urbs(struct usb_serial
*serial
)
1585 struct keyspan_serial_private
*s_priv
;
1586 const struct keyspan_device_details
*d_details
;
1587 struct usb_serial_port
*port
;
1588 struct keyspan_port_private
*p_priv
;
1589 struct callbacks
*cback
;
1592 dbg ("%s", __func__
);
1594 s_priv
= usb_get_serial_data(serial
);
1595 d_details
= s_priv
->device_details
;
1597 /* Setup values for the various callback routines */
1598 cback
= &keyspan_callbacks
[d_details
->msg_format
];
1600 /* Allocate and set up urbs for each one that is in use,
1601 starting with instat endpoints */
1602 s_priv
->instat_urb
= keyspan_setup_urb
1603 (serial
, d_details
->instat_endpoint
, USB_DIR_IN
,
1604 serial
, s_priv
->instat_buf
, INSTAT_BUFLEN
,
1605 cback
->instat_callback
);
1607 s_priv
->indat_urb
= keyspan_setup_urb
1608 (serial
, d_details
->indat_endpoint
, USB_DIR_IN
,
1609 serial
, s_priv
->indat_buf
, INDAT49W_BUFLEN
,
1610 usa49wg_indat_callback
);
1612 s_priv
->glocont_urb
= keyspan_setup_urb
1613 (serial
, d_details
->glocont_endpoint
, USB_DIR_OUT
,
1614 serial
, s_priv
->glocont_buf
, GLOCONT_BUFLEN
,
1615 cback
->glocont_callback
);
1617 /* Setup endpoints for each port specific thing */
1618 for (i
= 0; i
< d_details
->num_ports
; i
++) {
1619 port
= serial
->port
[i
];
1620 p_priv
= usb_get_serial_port_data(port
);
1622 /* Do indat endpoints first, once for each flip */
1623 endp
= d_details
->indat_endpoints
[i
];
1624 for (j
= 0; j
<= d_details
->indat_endp_flip
; ++j
, ++endp
) {
1625 p_priv
->in_urbs
[j
] = keyspan_setup_urb
1626 (serial
, endp
, USB_DIR_IN
, port
,
1627 p_priv
->in_buffer
[j
], 64,
1628 cback
->indat_callback
);
1631 p_priv
->in_urbs
[j
] = NULL
;
1633 /* outdat endpoints also have flip */
1634 endp
= d_details
->outdat_endpoints
[i
];
1635 for (j
= 0; j
<= d_details
->outdat_endp_flip
; ++j
, ++endp
) {
1636 p_priv
->out_urbs
[j
] = keyspan_setup_urb
1637 (serial
, endp
, USB_DIR_OUT
, port
,
1638 p_priv
->out_buffer
[j
], 64,
1639 cback
->outdat_callback
);
1642 p_priv
->out_urbs
[j
] = NULL
;
1644 /* inack endpoint */
1645 p_priv
->inack_urb
= keyspan_setup_urb
1646 (serial
, d_details
->inack_endpoints
[i
], USB_DIR_IN
,
1647 port
, p_priv
->inack_buffer
, 1, cback
->inack_callback
);
1649 /* outcont endpoint */
1650 p_priv
->outcont_urb
= keyspan_setup_urb
1651 (serial
, d_details
->outcont_endpoints
[i
], USB_DIR_OUT
,
1652 port
, p_priv
->outcont_buffer
, 64,
1653 cback
->outcont_callback
);
1658 /* usa19 function doesn't require prescaler */
1659 static int keyspan_usa19_calc_baud(u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1660 u8
*rate_low
, u8
*prescaler
, int portnum
)
1662 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1664 cnt
; /* inverse of divisor (programmed into 8051) */
1666 dbg ("%s - %d.", __func__
, baud_rate
);
1668 /* prevent divide by zero... */
1669 if( (b16
= (baud_rate
* 16L)) == 0) {
1670 return (KEYSPAN_INVALID_BAUD_RATE
);
1673 /* Any "standard" rate over 57k6 is marginal on the USA-19
1674 as we run out of divisor resolution. */
1675 if (baud_rate
> 57600) {
1676 return (KEYSPAN_INVALID_BAUD_RATE
);
1679 /* calculate the divisor and the counter (its inverse) */
1680 if( (div
= (baudclk
/ b16
)) == 0) {
1681 return (KEYSPAN_INVALID_BAUD_RATE
);
1688 return (KEYSPAN_INVALID_BAUD_RATE
);
1691 /* return the counter values if non-null */
1693 *rate_low
= (u8
) (cnt
& 0xff);
1696 *rate_hi
= (u8
) ((cnt
>> 8) & 0xff);
1698 if (rate_low
&& rate_hi
) {
1699 dbg ("%s - %d %02x %02x.", __func__
, baud_rate
, *rate_hi
, *rate_low
);
1702 return (KEYSPAN_BAUD_RATE_OK
);
1705 /* usa19hs function doesn't require prescaler */
1706 static int keyspan_usa19hs_calc_baud(u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1707 u8
*rate_low
, u8
*prescaler
, int portnum
)
1709 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1712 dbg ("%s - %d.", __func__
, baud_rate
);
1714 /* prevent divide by zero... */
1715 if( (b16
= (baud_rate
* 16L)) == 0)
1716 return (KEYSPAN_INVALID_BAUD_RATE
);
1720 /* calculate the divisor */
1721 if( (div
= (baudclk
/ b16
)) == 0)
1722 return (KEYSPAN_INVALID_BAUD_RATE
);
1725 return (KEYSPAN_INVALID_BAUD_RATE
);
1727 /* return the counter values if non-null */
1729 *rate_low
= (u8
) (div
& 0xff);
1732 *rate_hi
= (u8
) ((div
>> 8) & 0xff);
1734 if (rate_low
&& rate_hi
)
1735 dbg ("%s - %d %02x %02x.", __func__
, baud_rate
, *rate_hi
, *rate_low
);
1737 return (KEYSPAN_BAUD_RATE_OK
);
1740 static int keyspan_usa19w_calc_baud(u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1741 u8
*rate_low
, u8
*prescaler
, int portnum
)
1743 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1744 clk
, /* clock with 13/8 prescaler */
1745 div
, /* divisor using 13/8 prescaler */
1746 res
, /* resulting baud rate using 13/8 prescaler */
1747 diff
, /* error using 13/8 prescaler */
1752 dbg ("%s - %d.", __func__
, baud_rate
);
1754 /* prevent divide by zero */
1755 if( (b16
= baud_rate
* 16L) == 0) {
1756 return (KEYSPAN_INVALID_BAUD_RATE
);
1759 /* Calculate prescaler by trying them all and looking
1762 /* start with largest possible difference */
1763 smallest_diff
= 0xffffffff;
1765 /* 0 is an invalid prescaler, used as a flag */
1768 for(i
= 8; i
<= 0xff; ++i
) {
1769 clk
= (baudclk
* 8) / (u32
) i
;
1771 if( (div
= clk
/ b16
) == 0) {
1776 diff
= (res
> b16
) ? (res
-b16
) : (b16
-res
);
1778 if(diff
< smallest_diff
) {
1780 smallest_diff
= diff
;
1784 if(best_prescaler
== 0) {
1785 return (KEYSPAN_INVALID_BAUD_RATE
);
1788 clk
= (baudclk
* 8) / (u32
) best_prescaler
;
1791 /* return the divisor and prescaler if non-null */
1793 *rate_low
= (u8
) (div
& 0xff);
1796 *rate_hi
= (u8
) ((div
>> 8) & 0xff);
1799 *prescaler
= best_prescaler
;
1800 /* dbg("%s - %d %d", __func__, *prescaler, div); */
1802 return (KEYSPAN_BAUD_RATE_OK
);
1805 /* USA-28 supports different maximum baud rates on each port */
1806 static int keyspan_usa28_calc_baud(u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1807 u8
*rate_low
, u8
*prescaler
, int portnum
)
1809 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1811 cnt
; /* inverse of divisor (programmed into 8051) */
1813 dbg ("%s - %d.", __func__
, baud_rate
);
1815 /* prevent divide by zero */
1816 if ((b16
= baud_rate
* 16L) == 0)
1817 return (KEYSPAN_INVALID_BAUD_RATE
);
1819 /* calculate the divisor and the counter (its inverse) */
1820 if ((div
= (KEYSPAN_USA28_BAUDCLK
/ b16
)) == 0) {
1821 return (KEYSPAN_INVALID_BAUD_RATE
);
1827 /* check for out of range, based on portnum,
1828 and return result */
1831 return (KEYSPAN_INVALID_BAUD_RATE
);
1836 return (KEYSPAN_INVALID_BAUD_RATE
);
1840 return (KEYSPAN_INVALID_BAUD_RATE
);
1844 /* return the counter values if not NULL
1845 (port 1 will ignore retHi) */
1847 *rate_low
= (u8
) (cnt
& 0xff);
1850 *rate_hi
= (u8
) ((cnt
>> 8) & 0xff);
1852 dbg ("%s - %d OK.", __func__
, baud_rate
);
1853 return (KEYSPAN_BAUD_RATE_OK
);
1856 static int keyspan_usa26_send_setup(struct usb_serial
*serial
,
1857 struct usb_serial_port
*port
,
1860 struct keyspan_usa26_portControlMessage msg
;
1861 struct keyspan_serial_private
*s_priv
;
1862 struct keyspan_port_private
*p_priv
;
1863 const struct keyspan_device_details
*d_details
;
1865 struct urb
*this_urb
;
1866 int device_port
, err
;
1868 dbg ("%s reset=%d", __func__
, reset_port
);
1870 s_priv
= usb_get_serial_data(serial
);
1871 p_priv
= usb_get_serial_port_data(port
);
1872 d_details
= s_priv
->device_details
;
1873 device_port
= port
->number
- port
->serial
->minor
;
1875 outcont_urb
= d_details
->outcont_endpoints
[port
->number
];
1876 this_urb
= p_priv
->outcont_urb
;
1878 dbg("%s - endpoint %d", __func__
, usb_pipeendpoint(this_urb
->pipe
));
1880 /* Make sure we have an urb then send the message */
1881 if (this_urb
== NULL
) {
1882 dbg("%s - oops no urb.", __func__
);
1886 /* Save reset port val for resend.
1887 Don't overwrite resend for open/close condition. */
1888 if ((reset_port
+ 1) > p_priv
->resend_cont
)
1889 p_priv
->resend_cont
= reset_port
+ 1;
1890 if (this_urb
->status
== -EINPROGRESS
) {
1891 /* dbg ("%s - already writing", __func__); */
1896 memset(&msg
, 0, sizeof (struct keyspan_usa26_portControlMessage
));
1898 /* Only set baud rate if it's changed */
1899 if (p_priv
->old_baud
!= p_priv
->baud
) {
1900 p_priv
->old_baud
= p_priv
->baud
;
1901 msg
.setClocking
= 0xff;
1902 if (d_details
->calculate_baud_rate
1903 (p_priv
->baud
, d_details
->baudclk
, &msg
.baudHi
,
1904 &msg
.baudLo
, &msg
.prescaler
, device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
1905 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__
,
1908 msg
.baudHi
= 125; /* Values for 9600 baud */
1911 msg
.setPrescaler
= 0xff;
1914 msg
.lcr
= (p_priv
->cflag
& CSTOPB
)? STOPBITS_678_2
: STOPBITS_5678_1
;
1915 switch (p_priv
->cflag
& CSIZE
) {
1917 msg
.lcr
|= USA_DATABITS_5
;
1920 msg
.lcr
|= USA_DATABITS_6
;
1923 msg
.lcr
|= USA_DATABITS_7
;
1926 msg
.lcr
|= USA_DATABITS_8
;
1929 if (p_priv
->cflag
& PARENB
) {
1930 /* note USA_PARITY_NONE == 0 */
1931 msg
.lcr
|= (p_priv
->cflag
& PARODD
)?
1932 USA_PARITY_ODD
: USA_PARITY_EVEN
;
1936 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
1937 msg
.xonFlowControl
= 0;
1938 msg
.setFlowControl
= 0xff;
1939 msg
.forwardingLength
= 16;
1944 if (reset_port
== 1) {
1953 msg
.returnStatus
= 0;
1954 msg
.resetDataToggle
= 0xff;
1958 else if (reset_port
== 2) {
1967 msg
.returnStatus
= 0;
1968 msg
.resetDataToggle
= 0;
1971 /* Sending intermediate configs */
1973 msg
._txOn
= (! p_priv
->break_on
);
1976 msg
.txBreak
= (p_priv
->break_on
);
1981 msg
.returnStatus
= 0;
1982 msg
.resetDataToggle
= 0x0;
1985 /* Do handshaking outputs */
1986 msg
.setTxTriState_setRts
= 0xff;
1987 msg
.txTriState_rts
= p_priv
->rts_state
;
1989 msg
.setHskoa_setDtr
= 0xff;
1990 msg
.hskoa_dtr
= p_priv
->dtr_state
;
1992 p_priv
->resend_cont
= 0;
1993 memcpy (this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
1995 /* send the data out the device on control endpoint */
1996 this_urb
->transfer_buffer_length
= sizeof(msg
);
1998 this_urb
->dev
= serial
->dev
;
1999 if ((err
= usb_submit_urb(this_urb
, GFP_ATOMIC
)) != 0) {
2000 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__
, err
);
2004 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
2005 outcont_urb
, this_urb
->transfer_buffer_length
,
2006 usb_pipeendpoint(this_urb
->pipe
));
2013 static int keyspan_usa28_send_setup(struct usb_serial
*serial
,
2014 struct usb_serial_port
*port
,
2017 struct keyspan_usa28_portControlMessage msg
;
2018 struct keyspan_serial_private
*s_priv
;
2019 struct keyspan_port_private
*p_priv
;
2020 const struct keyspan_device_details
*d_details
;
2021 struct urb
*this_urb
;
2022 int device_port
, err
;
2024 dbg ("%s", __func__
);
2026 s_priv
= usb_get_serial_data(serial
);
2027 p_priv
= usb_get_serial_port_data(port
);
2028 d_details
= s_priv
->device_details
;
2029 device_port
= port
->number
- port
->serial
->minor
;
2031 /* only do something if we have a bulk out endpoint */
2032 if ((this_urb
= p_priv
->outcont_urb
) == NULL
) {
2033 dbg("%s - oops no urb.", __func__
);
2037 /* Save reset port val for resend.
2038 Don't overwrite resend for open/close condition. */
2039 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2040 p_priv
->resend_cont
= reset_port
+ 1;
2041 if (this_urb
->status
== -EINPROGRESS
) {
2042 dbg ("%s already writing", __func__
);
2047 memset(&msg
, 0, sizeof (struct keyspan_usa28_portControlMessage
));
2049 msg
.setBaudRate
= 1;
2050 if (d_details
->calculate_baud_rate(p_priv
->baud
, d_details
->baudclk
,
2051 &msg
.baudHi
, &msg
.baudLo
, NULL
, device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
2052 dbg("%s - Invalid baud rate requested %d.", __func__
, p_priv
->baud
);
2054 msg
.baudHi
= 0xb2; /* Values for 9600 baud */
2057 /* If parity is enabled, we must calculate it ourselves. */
2058 msg
.parity
= 0; /* XXX for now */
2060 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
2061 msg
.xonFlowControl
= 0;
2063 /* Do handshaking outputs, DTR is inverted relative to RTS */
2064 msg
.rts
= p_priv
->rts_state
;
2065 msg
.dtr
= p_priv
->dtr_state
;
2067 msg
.forwardingLength
= 16;
2069 msg
.breakThreshold
= 45;
2073 /*msg.returnStatus = 1;
2074 msg.resetDataToggle = 0xff;*/
2076 if (reset_port
== 1) {
2080 msg
.txForceXoff
= 0;
2086 msg
.returnStatus
= 0;
2087 msg
.resetDataToggle
= 0xff;
2090 else if (reset_port
== 2) {
2094 msg
.txForceXoff
= 0;
2100 msg
.returnStatus
= 0;
2101 msg
.resetDataToggle
= 0;
2103 /* Sending intermediate configs */
2105 msg
._txOn
= (! p_priv
->break_on
);
2108 msg
.txForceXoff
= 0;
2109 msg
.txBreak
= (p_priv
->break_on
);
2114 msg
.returnStatus
= 0;
2115 msg
.resetDataToggle
= 0x0;
2118 p_priv
->resend_cont
= 0;
2119 memcpy (this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2121 /* send the data out the device on control endpoint */
2122 this_urb
->transfer_buffer_length
= sizeof(msg
);
2124 this_urb
->dev
= serial
->dev
;
2125 if ((err
= usb_submit_urb(this_urb
, GFP_ATOMIC
)) != 0) {
2126 dbg("%s - usb_submit_urb(setup) failed", __func__
);
2130 dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__
,
2131 this_urb
->transfer_buffer_length
);
2138 static int keyspan_usa49_send_setup(struct usb_serial
*serial
,
2139 struct usb_serial_port
*port
,
2142 struct keyspan_usa49_portControlMessage msg
;
2143 struct usb_ctrlrequest
*dr
= NULL
;
2144 struct keyspan_serial_private
*s_priv
;
2145 struct keyspan_port_private
*p_priv
;
2146 const struct keyspan_device_details
*d_details
;
2147 struct urb
*this_urb
;
2148 int err
, device_port
;
2150 dbg ("%s", __func__
);
2152 s_priv
= usb_get_serial_data(serial
);
2153 p_priv
= usb_get_serial_port_data(port
);
2154 d_details
= s_priv
->device_details
;
2156 this_urb
= s_priv
->glocont_urb
;
2158 /* Work out which port within the device is being setup */
2159 device_port
= port
->number
- port
->serial
->minor
;
2161 dbg("%s - endpoint %d port %d (%d)",__func__
, usb_pipeendpoint(this_urb
->pipe
), port
->number
, device_port
);
2163 /* Make sure we have an urb then send the message */
2164 if (this_urb
== NULL
) {
2165 dbg("%s - oops no urb for port %d.", __func__
, port
->number
);
2169 /* Save reset port val for resend.
2170 Don't overwrite resend for open/close condition. */
2171 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2172 p_priv
->resend_cont
= reset_port
+ 1;
2174 if (this_urb
->status
== -EINPROGRESS
) {
2175 /* dbg ("%s - already writing", __func__); */
2180 memset(&msg
, 0, sizeof (struct keyspan_usa49_portControlMessage
));
2182 /*msg.portNumber = port->number;*/
2183 msg
.portNumber
= device_port
;
2185 /* Only set baud rate if it's changed */
2186 if (p_priv
->old_baud
!= p_priv
->baud
) {
2187 p_priv
->old_baud
= p_priv
->baud
;
2188 msg
.setClocking
= 0xff;
2189 if (d_details
->calculate_baud_rate
2190 (p_priv
->baud
, d_details
->baudclk
, &msg
.baudHi
,
2191 &msg
.baudLo
, &msg
.prescaler
, device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
2192 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__
,
2195 msg
.baudHi
= 125; /* Values for 9600 baud */
2198 //msg.setPrescaler = 0xff;
2201 msg
.lcr
= (p_priv
->cflag
& CSTOPB
)? STOPBITS_678_2
: STOPBITS_5678_1
;
2202 switch (p_priv
->cflag
& CSIZE
) {
2204 msg
.lcr
|= USA_DATABITS_5
;
2207 msg
.lcr
|= USA_DATABITS_6
;
2210 msg
.lcr
|= USA_DATABITS_7
;
2213 msg
.lcr
|= USA_DATABITS_8
;
2216 if (p_priv
->cflag
& PARENB
) {
2217 /* note USA_PARITY_NONE == 0 */
2218 msg
.lcr
|= (p_priv
->cflag
& PARODD
)?
2219 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2223 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
2224 msg
.xonFlowControl
= 0;
2225 msg
.setFlowControl
= 0xff;
2227 msg
.forwardingLength
= 16;
2232 if (reset_port
== 1) {
2241 msg
.returnStatus
= 0;
2242 msg
.resetDataToggle
= 0xff;
2244 msg
.disablePort
= 0;
2247 else if (reset_port
== 2) {
2256 msg
.returnStatus
= 0;
2257 msg
.resetDataToggle
= 0;
2259 msg
.disablePort
= 1;
2261 /* Sending intermediate configs */
2263 msg
._txOn
= (! p_priv
->break_on
);
2266 msg
.txBreak
= (p_priv
->break_on
);
2271 msg
.returnStatus
= 0;
2272 msg
.resetDataToggle
= 0x0;
2274 msg
.disablePort
= 0;
2277 /* Do handshaking outputs */
2279 msg
.rts
= p_priv
->rts_state
;
2282 msg
.dtr
= p_priv
->dtr_state
;
2284 p_priv
->resend_cont
= 0;
2286 /* if the device is a 49wg, we send control message on usb control EP 0 */
2288 if (d_details
->product_id
== keyspan_usa49wg_product_id
) {
2289 dr
= (void *)(s_priv
->ctrl_buf
);
2290 dr
->bRequestType
= USB_TYPE_VENDOR
| USB_DIR_OUT
;
2291 dr
->bRequest
= 0xB0; /* 49wg control message */;
2294 dr
->wLength
= cpu_to_le16(sizeof(msg
));
2296 memcpy (s_priv
->glocont_buf
, &msg
, sizeof(msg
));
2298 usb_fill_control_urb(this_urb
, serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2299 (unsigned char *)dr
, s_priv
->glocont_buf
, sizeof(msg
),
2300 usa49_glocont_callback
, serial
);
2303 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2305 /* send the data out the device on control endpoint */
2306 this_urb
->transfer_buffer_length
= sizeof(msg
);
2308 this_urb
->dev
= serial
->dev
;
2310 if ((err
= usb_submit_urb(this_urb
, GFP_ATOMIC
)) != 0) {
2311 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__
, err
);
2315 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
,
2316 outcont_urb
, this_urb
->transfer_buffer_length
,
2317 usb_pipeendpoint(this_urb
->pipe
));
2324 static int keyspan_usa90_send_setup(struct usb_serial
*serial
,
2325 struct usb_serial_port
*port
,
2328 struct keyspan_usa90_portControlMessage msg
;
2329 struct keyspan_serial_private
*s_priv
;
2330 struct keyspan_port_private
*p_priv
;
2331 const struct keyspan_device_details
*d_details
;
2332 struct urb
*this_urb
;
2336 dbg ("%s", __func__
);
2338 s_priv
= usb_get_serial_data(serial
);
2339 p_priv
= usb_get_serial_port_data(port
);
2340 d_details
= s_priv
->device_details
;
2342 /* only do something if we have a bulk out endpoint */
2343 if ((this_urb
= p_priv
->outcont_urb
) == NULL
) {
2344 dbg("%s - oops no urb.", __func__
);
2348 /* Save reset port val for resend.
2349 Don't overwrite resend for open/close condition. */
2350 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2351 p_priv
->resend_cont
= reset_port
+ 1;
2352 if (this_urb
->status
== -EINPROGRESS
) {
2353 dbg ("%s already writing", __func__
);
2358 memset(&msg
, 0, sizeof (struct keyspan_usa90_portControlMessage
));
2360 /* Only set baud rate if it's changed */
2361 if (p_priv
->old_baud
!= p_priv
->baud
) {
2362 p_priv
->old_baud
= p_priv
->baud
;
2363 msg
.setClocking
= 0x01;
2364 if (d_details
->calculate_baud_rate
2365 (p_priv
->baud
, d_details
->baudclk
, &msg
.baudHi
,
2366 &msg
.baudLo
, &prescaler
, 0) == KEYSPAN_INVALID_BAUD_RATE
) {
2367 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__
,
2369 p_priv
->baud
= 9600;
2370 d_details
->calculate_baud_rate (p_priv
->baud
, d_details
->baudclk
,
2371 &msg
.baudHi
, &msg
.baudLo
, &prescaler
, 0);
2377 /* modes must always be correctly specified */
2378 if (p_priv
->baud
> 57600)
2380 msg
.rxMode
= RXMODE_DMA
;
2381 msg
.txMode
= TXMODE_DMA
;
2385 msg
.rxMode
= RXMODE_BYHAND
;
2386 msg
.txMode
= TXMODE_BYHAND
;
2389 msg
.lcr
= (p_priv
->cflag
& CSTOPB
)? STOPBITS_678_2
: STOPBITS_5678_1
;
2390 switch (p_priv
->cflag
& CSIZE
) {
2392 msg
.lcr
|= USA_DATABITS_5
;
2395 msg
.lcr
|= USA_DATABITS_6
;
2398 msg
.lcr
|= USA_DATABITS_7
;
2401 msg
.lcr
|= USA_DATABITS_8
;
2404 if (p_priv
->cflag
& PARENB
) {
2405 /* note USA_PARITY_NONE == 0 */
2406 msg
.lcr
|= (p_priv
->cflag
& PARODD
)?
2407 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2409 if (p_priv
->old_cflag
!= p_priv
->cflag
) {
2410 p_priv
->old_cflag
= p_priv
->cflag
;
2414 if (p_priv
->flow_control
== flow_cts
)
2415 msg
.txFlowControl
= TXFLOW_CTS
;
2416 msg
.setTxFlowControl
= 0x01;
2417 msg
.setRxFlowControl
= 0x01;
2419 msg
.rxForwardingLength
= 16;
2420 msg
.rxForwardingTimeout
= 16;
2421 msg
.txAckSetting
= 0;
2426 if (reset_port
== 1) {
2427 msg
.portEnabled
= 1;
2429 msg
.txBreak
= (p_priv
->break_on
);
2432 else if (reset_port
== 2) {
2433 msg
.portEnabled
= 0;
2435 /* Sending intermediate configs */
2437 if (port
->open_count
)
2438 msg
.portEnabled
= 1;
2439 msg
.txBreak
= (p_priv
->break_on
);
2442 /* Do handshaking outputs */
2444 msg
.rts
= p_priv
->rts_state
;
2447 msg
.dtr
= p_priv
->dtr_state
;
2449 p_priv
->resend_cont
= 0;
2450 memcpy (this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2452 /* send the data out the device on control endpoint */
2453 this_urb
->transfer_buffer_length
= sizeof(msg
);
2455 this_urb
->dev
= serial
->dev
;
2456 if ((err
= usb_submit_urb(this_urb
, GFP_ATOMIC
)) != 0) {
2457 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__
, err
);
2462 static int keyspan_usa67_send_setup(struct usb_serial
*serial
,
2463 struct usb_serial_port
*port
,
2466 struct keyspan_usa67_portControlMessage msg
;
2467 struct keyspan_serial_private
*s_priv
;
2468 struct keyspan_port_private
*p_priv
;
2469 const struct keyspan_device_details
*d_details
;
2470 struct urb
*this_urb
;
2471 int err
, device_port
;
2473 dbg ("%s", __func__
);
2475 s_priv
= usb_get_serial_data(serial
);
2476 p_priv
= usb_get_serial_port_data(port
);
2477 d_details
= s_priv
->device_details
;
2479 this_urb
= s_priv
->glocont_urb
;
2481 /* Work out which port within the device is being setup */
2482 device_port
= port
->number
- port
->serial
->minor
;
2484 /* Make sure we have an urb then send the message */
2485 if (this_urb
== NULL
) {
2486 dbg("%s - oops no urb for port %d.", __func__
,
2491 /* Save reset port val for resend.
2492 Don't overwrite resend for open/close condition. */
2493 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2494 p_priv
->resend_cont
= reset_port
+ 1;
2495 if (this_urb
->status
== -EINPROGRESS
) {
2496 /* dbg ("%s - already writing", __func__); */
2501 memset(&msg
, 0, sizeof(struct keyspan_usa67_portControlMessage
));
2503 msg
.port
= device_port
;
2505 /* Only set baud rate if it's changed */
2506 if (p_priv
->old_baud
!= p_priv
->baud
) {
2507 p_priv
->old_baud
= p_priv
->baud
;
2508 msg
.setClocking
= 0xff;
2509 if (d_details
->calculate_baud_rate
2510 (p_priv
->baud
, d_details
->baudclk
, &msg
.baudHi
,
2511 &msg
.baudLo
, &msg
.prescaler
, device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
2512 dbg("%s - Invalid baud rate %d requested, using 9600.", __func__
,
2515 msg
.baudHi
= 125; /* Values for 9600 baud */
2518 msg
.setPrescaler
= 0xff;
2521 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
2522 switch (p_priv
->cflag
& CSIZE
) {
2524 msg
.lcr
|= USA_DATABITS_5
;
2527 msg
.lcr
|= USA_DATABITS_6
;
2530 msg
.lcr
|= USA_DATABITS_7
;
2533 msg
.lcr
|= USA_DATABITS_8
;
2536 if (p_priv
->cflag
& PARENB
) {
2537 /* note USA_PARITY_NONE == 0 */
2538 msg
.lcr
|= (p_priv
->cflag
& PARODD
)?
2539 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2543 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
2544 msg
.xonFlowControl
= 0;
2545 msg
.setFlowControl
= 0xff;
2546 msg
.forwardingLength
= 16;
2550 if (reset_port
== 1) {
2560 msg
.returnStatus
= 0;
2561 msg
.resetDataToggle
= 0xff;
2562 } else if (reset_port
== 2) {
2572 msg
.returnStatus
= 0;
2573 msg
.resetDataToggle
= 0;
2575 /* Sending intermediate configs */
2576 msg
._txOn
= (! p_priv
->break_on
);
2579 msg
.txBreak
= (p_priv
->break_on
);
2584 msg
.returnStatus
= 0;
2585 msg
.resetDataToggle
= 0x0;
2588 /* Do handshaking outputs */
2589 msg
.setTxTriState_setRts
= 0xff;
2590 msg
.txTriState_rts
= p_priv
->rts_state
;
2592 msg
.setHskoa_setDtr
= 0xff;
2593 msg
.hskoa_dtr
= p_priv
->dtr_state
;
2595 p_priv
->resend_cont
= 0;
2597 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2599 /* send the data out the device on control endpoint */
2600 this_urb
->transfer_buffer_length
= sizeof(msg
);
2601 this_urb
->dev
= serial
->dev
;
2603 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2605 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__
,
2610 static void keyspan_send_setup(struct usb_serial_port
*port
, int reset_port
)
2612 struct usb_serial
*serial
= port
->serial
;
2613 struct keyspan_serial_private
*s_priv
;
2614 const struct keyspan_device_details
*d_details
;
2616 dbg ("%s", __func__
);
2618 s_priv
= usb_get_serial_data(serial
);
2619 d_details
= s_priv
->device_details
;
2621 switch (d_details
->msg_format
) {
2623 keyspan_usa26_send_setup(serial
, port
, reset_port
);
2626 keyspan_usa28_send_setup(serial
, port
, reset_port
);
2629 keyspan_usa49_send_setup(serial
, port
, reset_port
);
2632 keyspan_usa90_send_setup(serial
, port
, reset_port
);
2635 keyspan_usa67_send_setup(serial
, port
, reset_port
);
2641 /* Gets called by the "real" driver (ie once firmware is loaded
2642 and renumeration has taken place. */
2643 static int keyspan_startup (struct usb_serial
*serial
)
2646 struct usb_serial_port
*port
;
2647 struct keyspan_serial_private
*s_priv
;
2648 struct keyspan_port_private
*p_priv
;
2649 const struct keyspan_device_details
*d_details
;
2651 dbg("%s", __func__
);
2653 for (i
= 0; (d_details
= keyspan_devices
[i
]) != NULL
; ++i
)
2654 if (d_details
->product_id
== le16_to_cpu(serial
->dev
->descriptor
.idProduct
))
2656 if (d_details
== NULL
) {
2657 dev_err(&serial
->dev
->dev
, "%s - unknown product id %x\n", __func__
, le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
2661 /* Setup private data for serial driver */
2662 s_priv
= kzalloc(sizeof(struct keyspan_serial_private
), GFP_KERNEL
);
2664 dbg("%s - kmalloc for keyspan_serial_private failed.", __func__
);
2668 s_priv
->device_details
= d_details
;
2669 usb_set_serial_data(serial
, s_priv
);
2671 /* Now setup per port private data */
2672 for (i
= 0; i
< serial
->num_ports
; i
++) {
2673 port
= serial
->port
[i
];
2674 p_priv
= kzalloc(sizeof(struct keyspan_port_private
), GFP_KERNEL
);
2676 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__
, i
);
2679 p_priv
->device_details
= d_details
;
2680 usb_set_serial_port_data(port
, p_priv
);
2683 keyspan_setup_urbs(serial
);
2685 if (s_priv
->instat_urb
!= NULL
) {
2686 s_priv
->instat_urb
->dev
= serial
->dev
;
2687 err
= usb_submit_urb(s_priv
->instat_urb
, GFP_KERNEL
);
2689 dbg("%s - submit instat urb failed %d", __func__
,
2692 if (s_priv
->indat_urb
!= NULL
) {
2693 s_priv
->indat_urb
->dev
= serial
->dev
;
2694 err
= usb_submit_urb(s_priv
->indat_urb
, GFP_KERNEL
);
2696 dbg("%s - submit indat urb failed %d", __func__
,
2703 static void keyspan_shutdown (struct usb_serial
*serial
)
2706 struct usb_serial_port
*port
;
2707 struct keyspan_serial_private
*s_priv
;
2708 struct keyspan_port_private
*p_priv
;
2710 dbg("%s", __func__
);
2712 s_priv
= usb_get_serial_data(serial
);
2714 /* Stop reading/writing urbs */
2715 stop_urb(s_priv
->instat_urb
);
2716 stop_urb(s_priv
->glocont_urb
);
2717 stop_urb(s_priv
->indat_urb
);
2718 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2719 port
= serial
->port
[i
];
2720 p_priv
= usb_get_serial_port_data(port
);
2721 stop_urb(p_priv
->inack_urb
);
2722 stop_urb(p_priv
->outcont_urb
);
2723 for (j
= 0; j
< 2; j
++) {
2724 stop_urb(p_priv
->in_urbs
[j
]);
2725 stop_urb(p_priv
->out_urbs
[j
]);
2730 usb_free_urb(s_priv
->instat_urb
);
2731 usb_free_urb(s_priv
->indat_urb
);
2732 usb_free_urb(s_priv
->glocont_urb
);
2733 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2734 port
= serial
->port
[i
];
2735 p_priv
= usb_get_serial_port_data(port
);
2736 usb_free_urb(p_priv
->inack_urb
);
2737 usb_free_urb(p_priv
->outcont_urb
);
2738 for (j
= 0; j
< 2; j
++) {
2739 usb_free_urb(p_priv
->in_urbs
[j
]);
2740 usb_free_urb(p_priv
->out_urbs
[j
]);
2744 /* dbg("Freeing serial->private."); */
2747 /* dbg("Freeing port->private."); */
2748 /* Now free per port private data */
2749 for (i
= 0; i
< serial
->num_ports
; i
++) {
2750 port
= serial
->port
[i
];
2751 kfree(usb_get_serial_port_data(port
));
2755 MODULE_AUTHOR( DRIVER_AUTHOR
);
2756 MODULE_DESCRIPTION( DRIVER_DESC
);
2757 MODULE_LICENSE("GPL");
2759 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
2760 MODULE_PARM_DESC(debug
, "Debug enabled or not");