2 Keyspan USB to Serial Converter driver
4 (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
5 (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 See http://blemings.org/hugh/keyspan.html for more information.
14 Code in this driver inspired by and in a number of places taken
15 from Brian Warner's original Keyspan-PDA driver.
17 This driver has been put together with the support of Innosys, Inc.
18 and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
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 #include <linux/kernel.h>
32 #include <linux/jiffies.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/tty.h>
37 #include <linux/tty_driver.h>
38 #include <linux/tty_flip.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/firmware.h>
42 #include <linux/ihex.h>
43 #include <linux/uaccess.h>
44 #include <linux/usb.h>
45 #include <linux/usb/serial.h>
53 #define DRIVER_VERSION "v1.1.5"
54 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
55 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
57 #define INSTAT_BUFLEN 32
58 #define GLOCONT_BUFLEN 64
59 #define INDAT49W_BUFLEN 512
61 /* Per device and per port private data */
62 struct keyspan_serial_private
{
63 const struct keyspan_device_details
*device_details
;
65 struct urb
*instat_urb
;
66 char instat_buf
[INSTAT_BUFLEN
];
68 /* added to support 49wg, where data from all 4 ports comes in
69 on 1 EP and high-speed supported */
70 struct urb
*indat_urb
;
71 char indat_buf
[INDAT49W_BUFLEN
];
73 /* XXX this one probably will need a lock */
74 struct urb
*glocont_urb
;
75 char glocont_buf
[GLOCONT_BUFLEN
];
76 char ctrl_buf
[8]; /* for EP0 control message */
79 struct keyspan_port_private
{
80 /* Keep track of which input & output endpoints to use */
84 /* Keep duplicate of device details in each port
85 structure as well - simplifies some of the
86 callback functions etc. */
87 const struct keyspan_device_details
*device_details
;
89 /* Input endpoints and buffer for this port */
90 struct urb
*in_urbs
[2];
91 char in_buffer
[2][64];
92 /* Output endpoints and buffer for this port */
93 struct urb
*out_urbs
[2];
94 char out_buffer
[2][64];
96 /* Input ack endpoint */
97 struct urb
*inack_urb
;
100 /* Output control endpoint */
101 struct urb
*outcont_urb
;
102 char outcont_buffer
[64];
104 /* Settings for the port */
108 unsigned int old_cflag
;
109 enum {flow_none
, flow_cts
, flow_xon
} flow_control
;
110 int rts_state
; /* Handshaking pins (outputs) */
112 int cts_state
; /* Handshaking pins (inputs) */
118 unsigned long tx_start_time
[2];
119 int resend_cont
; /* need to resend control packet */
122 /* Include Keyspan message headers. All current Keyspan Adapters
123 make use of one of five message formats which are referred
124 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
125 within this driver. */
126 #include "keyspan_usa26msg.h"
127 #include "keyspan_usa28msg.h"
128 #include "keyspan_usa49msg.h"
129 #include "keyspan_usa90msg.h"
130 #include "keyspan_usa67msg.h"
133 /* Functions used by new usb-serial code. */
134 static int __init
keyspan_init(void)
137 retval
= usb_serial_register(&keyspan_pre_device
);
139 goto failed_pre_device_register
;
140 retval
= usb_serial_register(&keyspan_1port_device
);
142 goto failed_1port_device_register
;
143 retval
= usb_serial_register(&keyspan_2port_device
);
145 goto failed_2port_device_register
;
146 retval
= usb_serial_register(&keyspan_4port_device
);
148 goto failed_4port_device_register
;
149 retval
= usb_register(&keyspan_driver
);
151 goto failed_usb_register
;
153 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
158 usb_serial_deregister(&keyspan_4port_device
);
159 failed_4port_device_register
:
160 usb_serial_deregister(&keyspan_2port_device
);
161 failed_2port_device_register
:
162 usb_serial_deregister(&keyspan_1port_device
);
163 failed_1port_device_register
:
164 usb_serial_deregister(&keyspan_pre_device
);
165 failed_pre_device_register
:
169 static void __exit
keyspan_exit(void)
171 usb_deregister(&keyspan_driver
);
172 usb_serial_deregister(&keyspan_pre_device
);
173 usb_serial_deregister(&keyspan_1port_device
);
174 usb_serial_deregister(&keyspan_2port_device
);
175 usb_serial_deregister(&keyspan_4port_device
);
178 module_init(keyspan_init
);
179 module_exit(keyspan_exit
);
181 static void keyspan_break_ctl(struct tty_struct
*tty
, int break_state
)
183 struct usb_serial_port
*port
= tty
->driver_data
;
184 struct keyspan_port_private
*p_priv
;
188 p_priv
= usb_get_serial_port_data(port
);
190 if (break_state
== -1)
191 p_priv
->break_on
= 1;
193 p_priv
->break_on
= 0;
195 keyspan_send_setup(port
, 0);
199 static void keyspan_set_termios(struct tty_struct
*tty
,
200 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
202 int baud_rate
, device_port
;
203 struct keyspan_port_private
*p_priv
;
204 const struct keyspan_device_details
*d_details
;
209 p_priv
= usb_get_serial_port_data(port
);
210 d_details
= p_priv
->device_details
;
211 cflag
= tty
->termios
->c_cflag
;
212 device_port
= port
->number
- port
->serial
->minor
;
214 /* Baud rate calculation takes baud rate as an integer
215 so other rates can be generated if desired. */
216 baud_rate
= tty_get_baud_rate(tty
);
217 /* If no match or invalid, don't change */
218 if (d_details
->calculate_baud_rate(baud_rate
, d_details
->baudclk
,
219 NULL
, NULL
, NULL
, device_port
) == KEYSPAN_BAUD_RATE_OK
) {
220 /* FIXME - more to do here to ensure rate changes cleanly */
221 /* FIXME - calcuate exact rate from divisor ? */
222 p_priv
->baud
= baud_rate
;
224 baud_rate
= tty_termios_baud_rate(old_termios
);
226 tty_encode_baud_rate(tty
, baud_rate
, baud_rate
);
227 /* set CTS/RTS handshake etc. */
228 p_priv
->cflag
= cflag
;
229 p_priv
->flow_control
= (cflag
& CRTSCTS
)? flow_cts
: flow_none
;
231 /* Mark/Space not supported */
232 tty
->termios
->c_cflag
&= ~CMSPAR
;
234 keyspan_send_setup(port
, 0);
237 static int keyspan_tiocmget(struct tty_struct
*tty
)
239 struct usb_serial_port
*port
= tty
->driver_data
;
240 struct keyspan_port_private
*p_priv
= usb_get_serial_port_data(port
);
243 value
= ((p_priv
->rts_state
) ? TIOCM_RTS
: 0) |
244 ((p_priv
->dtr_state
) ? TIOCM_DTR
: 0) |
245 ((p_priv
->cts_state
) ? TIOCM_CTS
: 0) |
246 ((p_priv
->dsr_state
) ? TIOCM_DSR
: 0) |
247 ((p_priv
->dcd_state
) ? TIOCM_CAR
: 0) |
248 ((p_priv
->ri_state
) ? TIOCM_RNG
: 0);
253 static int keyspan_tiocmset(struct tty_struct
*tty
,
254 unsigned int set
, unsigned int clear
)
256 struct usb_serial_port
*port
= tty
->driver_data
;
257 struct keyspan_port_private
*p_priv
= usb_get_serial_port_data(port
);
260 p_priv
->rts_state
= 1;
262 p_priv
->dtr_state
= 1;
263 if (clear
& TIOCM_RTS
)
264 p_priv
->rts_state
= 0;
265 if (clear
& TIOCM_DTR
)
266 p_priv
->dtr_state
= 0;
267 keyspan_send_setup(port
, 0);
271 /* Write function is similar for the four protocols used
272 with only a minor change for usa90 (usa19hs) required */
273 static int keyspan_write(struct tty_struct
*tty
,
274 struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
276 struct keyspan_port_private
*p_priv
;
277 const struct keyspan_device_details
*d_details
;
280 struct urb
*this_urb
;
281 int err
, maxDataLen
, dataOffset
;
283 p_priv
= usb_get_serial_port_data(port
);
284 d_details
= p_priv
->device_details
;
286 if (d_details
->msg_format
== msg_usa90
) {
294 dbg("%s - for port %d (%d chars), flip=%d",
295 __func__
, port
->number
, count
, p_priv
->out_flip
);
297 for (left
= count
; left
> 0; left
-= todo
) {
299 if (todo
> maxDataLen
)
302 flip
= p_priv
->out_flip
;
304 /* Check we have a valid urb/endpoint before we use it... */
305 this_urb
= p_priv
->out_urbs
[flip
];
306 if (this_urb
== NULL
) {
307 /* no bulk out, so return 0 bytes written */
308 dbg("%s - no output urb :(", __func__
);
312 dbg("%s - endpoint %d flip %d",
313 __func__
, usb_pipeendpoint(this_urb
->pipe
), flip
);
315 if (this_urb
->status
== -EINPROGRESS
) {
316 if (time_before(jiffies
,
317 p_priv
->tx_start_time
[flip
] + 10 * HZ
))
319 usb_unlink_urb(this_urb
);
323 /* First byte in buffer is "last flag" (except for usa19hx)
324 - unused so for now so set to zero */
325 ((char *)this_urb
->transfer_buffer
)[0] = 0;
327 memcpy(this_urb
->transfer_buffer
+ dataOffset
, buf
, todo
);
330 /* send the data out the bulk port */
331 this_urb
->transfer_buffer_length
= todo
+ dataOffset
;
333 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
335 dbg("usb_submit_urb(write bulk) failed (%d)", err
);
336 p_priv
->tx_start_time
[flip
] = jiffies
;
338 /* Flip for next time if usa26 or usa28 interface
339 (not used on usa49) */
340 p_priv
->out_flip
= (flip
+ 1) & d_details
->outdat_endp_flip
;
346 static void usa26_indat_callback(struct urb
*urb
)
350 struct usb_serial_port
*port
;
351 struct tty_struct
*tty
;
352 unsigned char *data
= urb
->transfer_buffer
;
353 int status
= urb
->status
;
357 endpoint
= usb_pipeendpoint(urb
->pipe
);
360 dbg("%s - nonzero status: %x on endpoint %d.",
361 __func__
, status
, endpoint
);
366 tty
= tty_port_tty_get(&port
->port
);
367 if (tty
&& urb
->actual_length
) {
368 /* 0x80 bit is error flag */
369 if ((data
[0] & 0x80) == 0) {
370 /* no errors on individual bytes, only
371 possible overrun err */
372 if (data
[0] & RXERROR_OVERRUN
)
376 for (i
= 1; i
< urb
->actual_length
; ++i
)
377 tty_insert_flip_char(tty
, data
[i
], err
);
379 /* some bytes had errors, every byte has status */
380 dbg("%s - RX error!!!!", __func__
);
381 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
382 int stat
= data
[i
], flag
= 0;
383 if (stat
& RXERROR_OVERRUN
)
385 if (stat
& RXERROR_FRAMING
)
387 if (stat
& RXERROR_PARITY
)
389 /* XXX should handle break (0x10) */
390 tty_insert_flip_char(tty
, data
[i
+1], flag
);
393 tty_flip_buffer_push(tty
);
397 /* Resubmit urb so we continue receiving */
398 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
400 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
403 /* Outdat handling is common for all devices */
404 static void usa2x_outdat_callback(struct urb
*urb
)
406 struct usb_serial_port
*port
;
407 struct keyspan_port_private
*p_priv
;
410 p_priv
= usb_get_serial_port_data(port
);
411 dbg("%s - urb %d", __func__
, urb
== p_priv
->out_urbs
[1]);
413 usb_serial_port_softint(port
);
416 static void usa26_inack_callback(struct urb
*urb
)
422 static void usa26_outcont_callback(struct urb
*urb
)
424 struct usb_serial_port
*port
;
425 struct keyspan_port_private
*p_priv
;
428 p_priv
= usb_get_serial_port_data(port
);
430 if (p_priv
->resend_cont
) {
431 dbg("%s - sending setup", __func__
);
432 keyspan_usa26_send_setup(port
->serial
, port
,
433 p_priv
->resend_cont
- 1);
437 static void usa26_instat_callback(struct urb
*urb
)
439 unsigned char *data
= urb
->transfer_buffer
;
440 struct keyspan_usa26_portStatusMessage
*msg
;
441 struct usb_serial
*serial
;
442 struct usb_serial_port
*port
;
443 struct keyspan_port_private
*p_priv
;
444 struct tty_struct
*tty
;
445 int old_dcd_state
, err
;
446 int status
= urb
->status
;
448 serial
= urb
->context
;
451 dbg("%s - nonzero status: %x", __func__
, status
);
454 if (urb
->actual_length
!= 9) {
455 dbg("%s - %d byte report??", __func__
, urb
->actual_length
);
459 msg
= (struct keyspan_usa26_portStatusMessage
*)data
;
462 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
463 __func__
, msg
->port
, msg
->hskia_cts
, msg
->gpia_dcd
, msg
->dsr
, msg
->ri
, msg
->_txOff
,
464 msg
->_txXoff
, msg
->rxEnabled
, msg
->controlResponse
);
467 /* Now do something useful with the data */
470 /* Check port number from message and retrieve private data */
471 if (msg
->port
>= serial
->num_ports
) {
472 dbg("%s - Unexpected port number %d", __func__
, msg
->port
);
475 port
= serial
->port
[msg
->port
];
476 p_priv
= usb_get_serial_port_data(port
);
478 /* Update handshaking pin state information */
479 old_dcd_state
= p_priv
->dcd_state
;
480 p_priv
->cts_state
= ((msg
->hskia_cts
) ? 1 : 0);
481 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
482 p_priv
->dcd_state
= ((msg
->gpia_dcd
) ? 1 : 0);
483 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
485 if (old_dcd_state
!= p_priv
->dcd_state
) {
486 tty
= tty_port_tty_get(&port
->port
);
487 if (tty
&& !C_CLOCAL(tty
))
492 /* Resubmit urb so we continue receiving */
493 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
495 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
499 static void usa26_glocont_callback(struct urb
*urb
)
505 static void usa28_indat_callback(struct urb
*urb
)
508 struct usb_serial_port
*port
;
509 struct tty_struct
*tty
;
511 struct keyspan_port_private
*p_priv
;
512 int status
= urb
->status
;
517 p_priv
= usb_get_serial_port_data(port
);
518 data
= urb
->transfer_buffer
;
520 if (urb
!= p_priv
->in_urbs
[p_priv
->in_flip
])
525 dbg("%s - nonzero status: %x on endpoint %d.",
526 __func__
, status
, usb_pipeendpoint(urb
->pipe
));
531 p_priv
= usb_get_serial_port_data(port
);
532 data
= urb
->transfer_buffer
;
534 tty
=tty_port_tty_get(&port
->port
);
535 if (tty
&& urb
->actual_length
) {
536 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
537 tty_flip_buffer_push(tty
);
541 /* Resubmit urb so we continue receiving */
542 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
544 dbg("%s - resubmit read urb failed. (%d)",
546 p_priv
->in_flip
^= 1;
548 urb
= p_priv
->in_urbs
[p_priv
->in_flip
];
549 } while (urb
->status
!= -EINPROGRESS
);
552 static void usa28_inack_callback(struct urb
*urb
)
557 static void usa28_outcont_callback(struct urb
*urb
)
559 struct usb_serial_port
*port
;
560 struct keyspan_port_private
*p_priv
;
563 p_priv
= usb_get_serial_port_data(port
);
565 if (p_priv
->resend_cont
) {
566 dbg("%s - sending setup", __func__
);
567 keyspan_usa28_send_setup(port
->serial
, port
,
568 p_priv
->resend_cont
- 1);
572 static void usa28_instat_callback(struct urb
*urb
)
575 unsigned char *data
= urb
->transfer_buffer
;
576 struct keyspan_usa28_portStatusMessage
*msg
;
577 struct usb_serial
*serial
;
578 struct usb_serial_port
*port
;
579 struct keyspan_port_private
*p_priv
;
580 struct tty_struct
*tty
;
582 int status
= urb
->status
;
584 serial
= urb
->context
;
587 dbg("%s - nonzero status: %x", __func__
, status
);
591 if (urb
->actual_length
!= sizeof(struct keyspan_usa28_portStatusMessage
)) {
592 dbg("%s - bad length %d", __func__
, urb
->actual_length
);
596 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
597 data[0], data[1], data[2], data[3], data[4], data[5],
598 data[6], data[7], data[8], data[9], data[10], data[11]);*/
600 /* Now do something useful with the data */
601 msg
= (struct keyspan_usa28_portStatusMessage
*)data
;
603 /* Check port number from message and retrieve private data */
604 if (msg
->port
>= serial
->num_ports
) {
605 dbg("%s - Unexpected port number %d", __func__
, msg
->port
);
608 port
= serial
->port
[msg
->port
];
609 p_priv
= usb_get_serial_port_data(port
);
611 /* Update handshaking pin state information */
612 old_dcd_state
= p_priv
->dcd_state
;
613 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
614 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
615 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
616 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
618 if( old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
) {
619 tty
= tty_port_tty_get(&port
->port
);
620 if (tty
&& !C_CLOCAL(tty
))
625 /* Resubmit urb so we continue receiving */
626 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
628 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
632 static void usa28_glocont_callback(struct urb
*urb
)
638 static void usa49_glocont_callback(struct urb
*urb
)
640 struct usb_serial
*serial
;
641 struct usb_serial_port
*port
;
642 struct keyspan_port_private
*p_priv
;
647 serial
= urb
->context
;
648 for (i
= 0; i
< serial
->num_ports
; ++i
) {
649 port
= serial
->port
[i
];
650 p_priv
= usb_get_serial_port_data(port
);
652 if (p_priv
->resend_cont
) {
653 dbg("%s - sending setup", __func__
);
654 keyspan_usa49_send_setup(serial
, port
,
655 p_priv
->resend_cont
- 1);
661 /* This is actually called glostat in the Keyspan
663 static void usa49_instat_callback(struct urb
*urb
)
666 unsigned char *data
= urb
->transfer_buffer
;
667 struct keyspan_usa49_portStatusMessage
*msg
;
668 struct usb_serial
*serial
;
669 struct usb_serial_port
*port
;
670 struct keyspan_port_private
*p_priv
;
672 int status
= urb
->status
;
676 serial
= urb
->context
;
679 dbg("%s - nonzero status: %x", __func__
, status
);
683 if (urb
->actual_length
!=
684 sizeof(struct keyspan_usa49_portStatusMessage
)) {
685 dbg("%s - bad length %d", __func__
, urb
->actual_length
);
689 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
690 data[0], data[1], data[2], data[3], data[4], data[5],
691 data[6], data[7], data[8], data[9], data[10]);*/
693 /* Now do something useful with the data */
694 msg
= (struct keyspan_usa49_portStatusMessage
*)data
;
696 /* Check port number from message and retrieve private data */
697 if (msg
->portNumber
>= serial
->num_ports
) {
698 dbg("%s - Unexpected port number %d",
699 __func__
, msg
->portNumber
);
702 port
= serial
->port
[msg
->portNumber
];
703 p_priv
= usb_get_serial_port_data(port
);
705 /* Update handshaking pin state information */
706 old_dcd_state
= p_priv
->dcd_state
;
707 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
708 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
709 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
710 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
712 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
) {
713 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
714 if (tty
&& !C_CLOCAL(tty
))
719 /* Resubmit urb so we continue receiving */
720 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
722 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
726 static void usa49_inack_callback(struct urb
*urb
)
731 static void usa49_indat_callback(struct urb
*urb
)
735 struct usb_serial_port
*port
;
736 struct tty_struct
*tty
;
737 unsigned char *data
= urb
->transfer_buffer
;
738 int status
= urb
->status
;
742 endpoint
= usb_pipeendpoint(urb
->pipe
);
745 dbg("%s - nonzero status: %x on endpoint %d.", __func__
,
751 tty
= tty_port_tty_get(&port
->port
);
752 if (tty
&& urb
->actual_length
) {
753 /* 0x80 bit is error flag */
754 if ((data
[0] & 0x80) == 0) {
755 /* no error on any byte */
756 tty_insert_flip_string(tty
, data
+ 1,
757 urb
->actual_length
- 1);
759 /* some bytes had errors, every byte has status */
760 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
761 int stat
= data
[i
], flag
= 0;
762 if (stat
& RXERROR_OVERRUN
)
764 if (stat
& RXERROR_FRAMING
)
766 if (stat
& RXERROR_PARITY
)
768 /* XXX should handle break (0x10) */
769 tty_insert_flip_char(tty
, data
[i
+1], flag
);
772 tty_flip_buffer_push(tty
);
776 /* Resubmit urb so we continue receiving */
777 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
779 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
782 static void usa49wg_indat_callback(struct urb
*urb
)
785 struct usb_serial
*serial
;
786 struct usb_serial_port
*port
;
787 struct tty_struct
*tty
;
788 unsigned char *data
= urb
->transfer_buffer
;
789 int status
= urb
->status
;
793 serial
= urb
->context
;
796 dbg("%s - nonzero status: %x", __func__
, status
);
800 /* inbound data is in the form P#, len, status, data */
804 if (urb
->actual_length
) {
805 while (i
< urb
->actual_length
) {
807 /* Check port number from message*/
808 if (data
[i
] >= serial
->num_ports
) {
809 dbg("%s - Unexpected port number %d",
813 port
= serial
->port
[data
[i
++]];
814 tty
= tty_port_tty_get(&port
->port
);
817 /* 0x80 bit is error flag */
818 if ((data
[i
] & 0x80) == 0) {
819 /* no error on any byte */
821 for (x
= 1; x
< len
; ++x
)
822 tty_insert_flip_char(tty
, data
[i
++], 0);
825 * some bytes had errors, every byte has status
827 for (x
= 0; x
+ 1 < len
; x
+= 2) {
828 int stat
= data
[i
], flag
= 0;
829 if (stat
& RXERROR_OVERRUN
)
831 if (stat
& RXERROR_FRAMING
)
833 if (stat
& RXERROR_PARITY
)
835 /* XXX should handle break (0x10) */
836 tty_insert_flip_char(tty
,
841 tty_flip_buffer_push(tty
);
846 /* Resubmit urb so we continue receiving */
847 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
849 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
852 /* not used, usa-49 doesn't have per-port control endpoints */
853 static void usa49_outcont_callback(struct urb
*urb
)
858 static void usa90_indat_callback(struct urb
*urb
)
862 struct usb_serial_port
*port
;
863 struct keyspan_port_private
*p_priv
;
864 struct tty_struct
*tty
;
865 unsigned char *data
= urb
->transfer_buffer
;
866 int status
= urb
->status
;
870 endpoint
= usb_pipeendpoint(urb
->pipe
);
873 dbg("%s - nonzero status: %x on endpoint %d.",
874 __func__
, status
, endpoint
);
879 p_priv
= usb_get_serial_port_data(port
);
881 if (urb
->actual_length
) {
882 tty
= tty_port_tty_get(&port
->port
);
883 /* if current mode is DMA, looks like usa28 format
884 otherwise looks like usa26 data format */
886 if (p_priv
->baud
> 57600)
887 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
889 /* 0x80 bit is error flag */
890 if ((data
[0] & 0x80) == 0) {
891 /* no errors on individual bytes, only
892 possible overrun err*/
893 if (data
[0] & RXERROR_OVERRUN
)
897 for (i
= 1; i
< urb
->actual_length
; ++i
)
898 tty_insert_flip_char(tty
, data
[i
],
901 /* some bytes had errors, every byte has status */
902 dbg("%s - RX error!!!!", __func__
);
903 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
904 int stat
= data
[i
], flag
= 0;
905 if (stat
& RXERROR_OVERRUN
)
907 if (stat
& RXERROR_FRAMING
)
909 if (stat
& RXERROR_PARITY
)
911 /* XXX should handle break (0x10) */
912 tty_insert_flip_char(tty
, data
[i
+1],
917 tty_flip_buffer_push(tty
);
921 /* Resubmit urb so we continue receiving */
922 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
924 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
928 static void usa90_instat_callback(struct urb
*urb
)
930 unsigned char *data
= urb
->transfer_buffer
;
931 struct keyspan_usa90_portStatusMessage
*msg
;
932 struct usb_serial
*serial
;
933 struct usb_serial_port
*port
;
934 struct keyspan_port_private
*p_priv
;
935 struct tty_struct
*tty
;
936 int old_dcd_state
, err
;
937 int status
= urb
->status
;
939 serial
= urb
->context
;
942 dbg("%s - nonzero status: %x", __func__
, status
);
945 if (urb
->actual_length
< 14) {
946 dbg("%s - %d byte report??", __func__
, urb
->actual_length
);
950 msg
= (struct keyspan_usa90_portStatusMessage
*)data
;
952 /* Now do something useful with the data */
954 port
= serial
->port
[0];
955 p_priv
= usb_get_serial_port_data(port
);
957 /* Update handshaking pin state information */
958 old_dcd_state
= p_priv
->dcd_state
;
959 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
960 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
961 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
962 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
964 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
) {
965 tty
= tty_port_tty_get(&port
->port
);
966 if (tty
&& !C_CLOCAL(tty
))
971 /* Resubmit urb so we continue receiving */
972 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
974 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
979 static void usa90_outcont_callback(struct urb
*urb
)
981 struct usb_serial_port
*port
;
982 struct keyspan_port_private
*p_priv
;
985 p_priv
= usb_get_serial_port_data(port
);
987 if (p_priv
->resend_cont
) {
988 dbg("%s - sending setup", __func__
);
989 keyspan_usa90_send_setup(port
->serial
, port
,
990 p_priv
->resend_cont
- 1);
994 /* Status messages from the 28xg */
995 static void usa67_instat_callback(struct urb
*urb
)
998 unsigned char *data
= urb
->transfer_buffer
;
999 struct keyspan_usa67_portStatusMessage
*msg
;
1000 struct usb_serial
*serial
;
1001 struct usb_serial_port
*port
;
1002 struct keyspan_port_private
*p_priv
;
1004 int status
= urb
->status
;
1006 dbg("%s", __func__
);
1008 serial
= urb
->context
;
1011 dbg("%s - nonzero status: %x", __func__
, status
);
1015 if (urb
->actual_length
!=
1016 sizeof(struct keyspan_usa67_portStatusMessage
)) {
1017 dbg("%s - bad length %d", __func__
, urb
->actual_length
);
1022 /* Now do something useful with the data */
1023 msg
= (struct keyspan_usa67_portStatusMessage
*)data
;
1025 /* Check port number from message and retrieve private data */
1026 if (msg
->port
>= serial
->num_ports
) {
1027 dbg("%s - Unexpected port number %d", __func__
, msg
->port
);
1031 port
= serial
->port
[msg
->port
];
1032 p_priv
= usb_get_serial_port_data(port
);
1034 /* Update handshaking pin state information */
1035 old_dcd_state
= p_priv
->dcd_state
;
1036 p_priv
->cts_state
= ((msg
->hskia_cts
) ? 1 : 0);
1037 p_priv
->dcd_state
= ((msg
->gpia_dcd
) ? 1 : 0);
1039 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
) {
1040 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
1041 if (tty
&& !C_CLOCAL(tty
))
1046 /* Resubmit urb so we continue receiving */
1047 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
1049 dbg("%s - resubmit read urb failed. (%d)", __func__
, err
);
1052 static void usa67_glocont_callback(struct urb
*urb
)
1054 struct usb_serial
*serial
;
1055 struct usb_serial_port
*port
;
1056 struct keyspan_port_private
*p_priv
;
1059 dbg("%s", __func__
);
1061 serial
= urb
->context
;
1062 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1063 port
= serial
->port
[i
];
1064 p_priv
= usb_get_serial_port_data(port
);
1066 if (p_priv
->resend_cont
) {
1067 dbg("%s - sending setup", __func__
);
1068 keyspan_usa67_send_setup(serial
, port
,
1069 p_priv
->resend_cont
- 1);
1075 static int keyspan_write_room(struct tty_struct
*tty
)
1077 struct usb_serial_port
*port
= tty
->driver_data
;
1078 struct keyspan_port_private
*p_priv
;
1079 const struct keyspan_device_details
*d_details
;
1082 struct urb
*this_urb
;
1084 dbg("%s", __func__
);
1085 p_priv
= usb_get_serial_port_data(port
);
1086 d_details
= p_priv
->device_details
;
1088 /* FIXME: locking */
1089 if (d_details
->msg_format
== msg_usa90
)
1094 flip
= p_priv
->out_flip
;
1096 /* Check both endpoints to see if any are available. */
1097 this_urb
= p_priv
->out_urbs
[flip
];
1098 if (this_urb
!= NULL
) {
1099 if (this_urb
->status
!= -EINPROGRESS
)
1101 flip
= (flip
+ 1) & d_details
->outdat_endp_flip
;
1102 this_urb
= p_priv
->out_urbs
[flip
];
1103 if (this_urb
!= NULL
) {
1104 if (this_urb
->status
!= -EINPROGRESS
)
1112 static int keyspan_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
1114 struct keyspan_port_private
*p_priv
;
1115 struct keyspan_serial_private
*s_priv
;
1116 struct usb_serial
*serial
= port
->serial
;
1117 const struct keyspan_device_details
*d_details
;
1119 int baud_rate
, device_port
;
1121 unsigned int cflag
= 0;
1123 s_priv
= usb_get_serial_data(serial
);
1124 p_priv
= usb_get_serial_port_data(port
);
1125 d_details
= p_priv
->device_details
;
1127 dbg("%s - port%d.", __func__
, port
->number
);
1129 /* Set some sane defaults */
1130 p_priv
->rts_state
= 1;
1131 p_priv
->dtr_state
= 1;
1132 p_priv
->baud
= 9600;
1134 /* force baud and lcr to be set on open */
1135 p_priv
->old_baud
= 0;
1136 p_priv
->old_cflag
= 0;
1138 p_priv
->out_flip
= 0;
1139 p_priv
->in_flip
= 0;
1141 /* Reset low level data toggle and start reading from endpoints */
1142 for (i
= 0; i
< 2; i
++) {
1143 urb
= p_priv
->in_urbs
[i
];
1147 /* make sure endpoint data toggle is synchronized
1149 usb_clear_halt(urb
->dev
, urb
->pipe
);
1150 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1152 dbg("%s - submit urb %d failed (%d)",
1156 /* Reset low level data toggle on out endpoints */
1157 for (i
= 0; i
< 2; i
++) {
1158 urb
= p_priv
->out_urbs
[i
];
1161 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1162 usb_pipeout(urb->pipe), 0); */
1165 /* get the terminal config for the setup message now so we don't
1166 * need to send 2 of them */
1168 device_port
= port
->number
- port
->serial
->minor
;
1170 cflag
= tty
->termios
->c_cflag
;
1171 /* Baud rate calculation takes baud rate as an integer
1172 so other rates can be generated if desired. */
1173 baud_rate
= tty_get_baud_rate(tty
);
1174 /* If no match or invalid, leave as default */
1176 && d_details
->calculate_baud_rate(baud_rate
, d_details
->baudclk
,
1177 NULL
, NULL
, NULL
, device_port
) == KEYSPAN_BAUD_RATE_OK
) {
1178 p_priv
->baud
= baud_rate
;
1181 /* set CTS/RTS handshake etc. */
1182 p_priv
->cflag
= cflag
;
1183 p_priv
->flow_control
= (cflag
& CRTSCTS
)? flow_cts
: flow_none
;
1185 keyspan_send_setup(port
, 1);
1187 /* keyspan_set_termios(port, NULL); */
1192 static inline void stop_urb(struct urb
*urb
)
1194 if (urb
&& urb
->status
== -EINPROGRESS
)
1198 static void keyspan_dtr_rts(struct usb_serial_port
*port
, int on
)
1200 struct keyspan_port_private
*p_priv
= usb_get_serial_port_data(port
);
1202 p_priv
->rts_state
= on
;
1203 p_priv
->dtr_state
= on
;
1204 keyspan_send_setup(port
, 0);
1207 static void keyspan_close(struct usb_serial_port
*port
)
1210 struct usb_serial
*serial
= port
->serial
;
1211 struct keyspan_serial_private
*s_priv
;
1212 struct keyspan_port_private
*p_priv
;
1214 dbg("%s", __func__
);
1215 s_priv
= usb_get_serial_data(serial
);
1216 p_priv
= usb_get_serial_port_data(port
);
1218 p_priv
->rts_state
= 0;
1219 p_priv
->dtr_state
= 0;
1222 keyspan_send_setup(port
, 2);
1223 /* pilot-xfer seems to work best with this delay */
1225 /* keyspan_set_termios(port, NULL); */
1228 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1229 dbg("%s - urb in progress", __func__);
1232 p_priv
->out_flip
= 0;
1233 p_priv
->in_flip
= 0;
1236 /* Stop reading/writing urbs */
1237 stop_urb(p_priv
->inack_urb
);
1238 /* stop_urb(p_priv->outcont_urb); */
1239 for (i
= 0; i
< 2; i
++) {
1240 stop_urb(p_priv
->in_urbs
[i
]);
1241 stop_urb(p_priv
->out_urbs
[i
]);
1246 /* download the firmware to a pre-renumeration device */
1247 static int keyspan_fake_startup(struct usb_serial
*serial
)
1250 const struct ihex_binrec
*record
;
1252 const struct firmware
*fw
;
1254 dbg("Keyspan startup version %04x product %04x",
1255 le16_to_cpu(serial
->dev
->descriptor
.bcdDevice
),
1256 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
1258 if ((le16_to_cpu(serial
->dev
->descriptor
.bcdDevice
) & 0x8000)
1260 dbg("Firmware already loaded. Quitting.");
1264 /* Select firmware image on the basis of idProduct */
1265 switch (le16_to_cpu(serial
->dev
->descriptor
.idProduct
)) {
1266 case keyspan_usa28_pre_product_id
:
1267 fw_name
= "keyspan/usa28.fw";
1270 case keyspan_usa28x_pre_product_id
:
1271 fw_name
= "keyspan/usa28x.fw";
1274 case keyspan_usa28xa_pre_product_id
:
1275 fw_name
= "keyspan/usa28xa.fw";
1278 case keyspan_usa28xb_pre_product_id
:
1279 fw_name
= "keyspan/usa28xb.fw";
1282 case keyspan_usa19_pre_product_id
:
1283 fw_name
= "keyspan/usa19.fw";
1286 case keyspan_usa19qi_pre_product_id
:
1287 fw_name
= "keyspan/usa19qi.fw";
1290 case keyspan_mpr_pre_product_id
:
1291 fw_name
= "keyspan/mpr.fw";
1294 case keyspan_usa19qw_pre_product_id
:
1295 fw_name
= "keyspan/usa19qw.fw";
1298 case keyspan_usa18x_pre_product_id
:
1299 fw_name
= "keyspan/usa18x.fw";
1302 case keyspan_usa19w_pre_product_id
:
1303 fw_name
= "keyspan/usa19w.fw";
1306 case keyspan_usa49w_pre_product_id
:
1307 fw_name
= "keyspan/usa49w.fw";
1310 case keyspan_usa49wlc_pre_product_id
:
1311 fw_name
= "keyspan/usa49wlc.fw";
1315 dev_err(&serial
->dev
->dev
, "Unknown product ID (%04x)\n",
1316 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
1320 if (request_ihex_firmware(&fw
, fw_name
, &serial
->dev
->dev
)) {
1321 dev_err(&serial
->dev
->dev
, "Required keyspan firmware image (%s) unavailable.\n", fw_name
);
1325 dbg("Uploading Keyspan %s firmware.", fw_name
);
1327 /* download the firmware image */
1328 response
= ezusb_set_reset(serial
, 1);
1330 record
= (const struct ihex_binrec
*)fw
->data
;
1333 response
= ezusb_writememory(serial
, be32_to_cpu(record
->addr
),
1334 (unsigned char *)record
->data
,
1335 be16_to_cpu(record
->len
), 0xa0);
1337 dev_err(&serial
->dev
->dev
, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
1338 response
, be32_to_cpu(record
->addr
),
1339 record
->data
, be16_to_cpu(record
->len
));
1342 record
= ihex_next_binrec(record
);
1344 release_firmware(fw
);
1345 /* bring device out of reset. Renumeration will occur in a
1346 moment and the new device will bind to the real driver */
1347 response
= ezusb_set_reset(serial
, 0);
1349 /* we don't want this device to have a driver assigned to it. */
1353 /* Helper functions used by keyspan_setup_urbs */
1354 static struct usb_endpoint_descriptor
const *find_ep(struct usb_serial
const *serial
,
1357 struct usb_host_interface
*iface_desc
;
1358 struct usb_endpoint_descriptor
*ep
;
1361 iface_desc
= serial
->interface
->cur_altsetting
;
1362 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1363 ep
= &iface_desc
->endpoint
[i
].desc
;
1364 if (ep
->bEndpointAddress
== endpoint
)
1367 dev_warn(&serial
->interface
->dev
, "found no endpoint descriptor for "
1368 "endpoint %x\n", endpoint
);
1372 static struct urb
*keyspan_setup_urb(struct usb_serial
*serial
, int endpoint
,
1373 int dir
, void *ctx
, char *buf
, int len
,
1374 void (*callback
)(struct urb
*))
1377 struct usb_endpoint_descriptor
const *ep_desc
;
1378 char const *ep_type_name
;
1381 return NULL
; /* endpoint not needed */
1383 dbg("%s - alloc for endpoint %d.", __func__
, endpoint
);
1384 urb
= usb_alloc_urb(0, GFP_KERNEL
); /* No ISO */
1386 dbg("%s - alloc for endpoint %d failed.", __func__
, endpoint
);
1390 if (endpoint
== 0) {
1391 /* control EP filled in when used */
1395 ep_desc
= find_ep(serial
, endpoint
);
1397 /* leak the urb, something's wrong and the callers don't care */
1400 if (usb_endpoint_xfer_int(ep_desc
)) {
1401 ep_type_name
= "INT";
1402 usb_fill_int_urb(urb
, serial
->dev
,
1403 usb_sndintpipe(serial
->dev
, endpoint
) | dir
,
1404 buf
, len
, callback
, ctx
,
1405 ep_desc
->bInterval
);
1406 } else if (usb_endpoint_xfer_bulk(ep_desc
)) {
1407 ep_type_name
= "BULK";
1408 usb_fill_bulk_urb(urb
, serial
->dev
,
1409 usb_sndbulkpipe(serial
->dev
, endpoint
) | dir
,
1410 buf
, len
, callback
, ctx
);
1412 dev_warn(&serial
->interface
->dev
,
1413 "unsupported endpoint type %x\n",
1414 usb_endpoint_type(ep_desc
));
1419 dbg("%s - using urb %p for %s endpoint %x",
1420 __func__
, urb
, ep_type_name
, endpoint
);
1424 static struct callbacks
{
1425 void (*instat_callback
)(struct urb
*);
1426 void (*glocont_callback
)(struct urb
*);
1427 void (*indat_callback
)(struct urb
*);
1428 void (*outdat_callback
)(struct urb
*);
1429 void (*inack_callback
)(struct urb
*);
1430 void (*outcont_callback
)(struct urb
*);
1431 } keyspan_callbacks
[] = {
1433 /* msg_usa26 callbacks */
1434 .instat_callback
= usa26_instat_callback
,
1435 .glocont_callback
= usa26_glocont_callback
,
1436 .indat_callback
= usa26_indat_callback
,
1437 .outdat_callback
= usa2x_outdat_callback
,
1438 .inack_callback
= usa26_inack_callback
,
1439 .outcont_callback
= usa26_outcont_callback
,
1441 /* msg_usa28 callbacks */
1442 .instat_callback
= usa28_instat_callback
,
1443 .glocont_callback
= usa28_glocont_callback
,
1444 .indat_callback
= usa28_indat_callback
,
1445 .outdat_callback
= usa2x_outdat_callback
,
1446 .inack_callback
= usa28_inack_callback
,
1447 .outcont_callback
= usa28_outcont_callback
,
1449 /* msg_usa49 callbacks */
1450 .instat_callback
= usa49_instat_callback
,
1451 .glocont_callback
= usa49_glocont_callback
,
1452 .indat_callback
= usa49_indat_callback
,
1453 .outdat_callback
= usa2x_outdat_callback
,
1454 .inack_callback
= usa49_inack_callback
,
1455 .outcont_callback
= usa49_outcont_callback
,
1457 /* msg_usa90 callbacks */
1458 .instat_callback
= usa90_instat_callback
,
1459 .glocont_callback
= usa28_glocont_callback
,
1460 .indat_callback
= usa90_indat_callback
,
1461 .outdat_callback
= usa2x_outdat_callback
,
1462 .inack_callback
= usa28_inack_callback
,
1463 .outcont_callback
= usa90_outcont_callback
,
1465 /* msg_usa67 callbacks */
1466 .instat_callback
= usa67_instat_callback
,
1467 .glocont_callback
= usa67_glocont_callback
,
1468 .indat_callback
= usa26_indat_callback
,
1469 .outdat_callback
= usa2x_outdat_callback
,
1470 .inack_callback
= usa26_inack_callback
,
1471 .outcont_callback
= usa26_outcont_callback
,
1475 /* Generic setup urbs function that uses
1476 data in device_details */
1477 static void keyspan_setup_urbs(struct usb_serial
*serial
)
1480 struct keyspan_serial_private
*s_priv
;
1481 const struct keyspan_device_details
*d_details
;
1482 struct usb_serial_port
*port
;
1483 struct keyspan_port_private
*p_priv
;
1484 struct callbacks
*cback
;
1487 dbg("%s", __func__
);
1489 s_priv
= usb_get_serial_data(serial
);
1490 d_details
= s_priv
->device_details
;
1492 /* Setup values for the various callback routines */
1493 cback
= &keyspan_callbacks
[d_details
->msg_format
];
1495 /* Allocate and set up urbs for each one that is in use,
1496 starting with instat endpoints */
1497 s_priv
->instat_urb
= keyspan_setup_urb
1498 (serial
, d_details
->instat_endpoint
, USB_DIR_IN
,
1499 serial
, s_priv
->instat_buf
, INSTAT_BUFLEN
,
1500 cback
->instat_callback
);
1502 s_priv
->indat_urb
= keyspan_setup_urb
1503 (serial
, d_details
->indat_endpoint
, USB_DIR_IN
,
1504 serial
, s_priv
->indat_buf
, INDAT49W_BUFLEN
,
1505 usa49wg_indat_callback
);
1507 s_priv
->glocont_urb
= keyspan_setup_urb
1508 (serial
, d_details
->glocont_endpoint
, USB_DIR_OUT
,
1509 serial
, s_priv
->glocont_buf
, GLOCONT_BUFLEN
,
1510 cback
->glocont_callback
);
1512 /* Setup endpoints for each port specific thing */
1513 for (i
= 0; i
< d_details
->num_ports
; i
++) {
1514 port
= serial
->port
[i
];
1515 p_priv
= usb_get_serial_port_data(port
);
1517 /* Do indat endpoints first, once for each flip */
1518 endp
= d_details
->indat_endpoints
[i
];
1519 for (j
= 0; j
<= d_details
->indat_endp_flip
; ++j
, ++endp
) {
1520 p_priv
->in_urbs
[j
] = keyspan_setup_urb
1521 (serial
, endp
, USB_DIR_IN
, port
,
1522 p_priv
->in_buffer
[j
], 64,
1523 cback
->indat_callback
);
1526 p_priv
->in_urbs
[j
] = NULL
;
1528 /* outdat endpoints also have flip */
1529 endp
= d_details
->outdat_endpoints
[i
];
1530 for (j
= 0; j
<= d_details
->outdat_endp_flip
; ++j
, ++endp
) {
1531 p_priv
->out_urbs
[j
] = keyspan_setup_urb
1532 (serial
, endp
, USB_DIR_OUT
, port
,
1533 p_priv
->out_buffer
[j
], 64,
1534 cback
->outdat_callback
);
1537 p_priv
->out_urbs
[j
] = NULL
;
1539 /* inack endpoint */
1540 p_priv
->inack_urb
= keyspan_setup_urb
1541 (serial
, d_details
->inack_endpoints
[i
], USB_DIR_IN
,
1542 port
, p_priv
->inack_buffer
, 1, cback
->inack_callback
);
1544 /* outcont endpoint */
1545 p_priv
->outcont_urb
= keyspan_setup_urb
1546 (serial
, d_details
->outcont_endpoints
[i
], USB_DIR_OUT
,
1547 port
, p_priv
->outcont_buffer
, 64,
1548 cback
->outcont_callback
);
1552 /* usa19 function doesn't require prescaler */
1553 static int keyspan_usa19_calc_baud(u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1554 u8
*rate_low
, u8
*prescaler
, int portnum
)
1556 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1558 cnt
; /* inverse of divisor (programmed into 8051) */
1560 dbg("%s - %d.", __func__
, baud_rate
);
1562 /* prevent divide by zero... */
1563 b16
= baud_rate
* 16L;
1565 return KEYSPAN_INVALID_BAUD_RATE
;
1566 /* Any "standard" rate over 57k6 is marginal on the USA-19
1567 as we run out of divisor resolution. */
1568 if (baud_rate
> 57600)
1569 return KEYSPAN_INVALID_BAUD_RATE
;
1571 /* calculate the divisor and the counter (its inverse) */
1572 div
= baudclk
/ b16
;
1574 return KEYSPAN_INVALID_BAUD_RATE
;
1579 return KEYSPAN_INVALID_BAUD_RATE
;
1581 /* return the counter values if non-null */
1583 *rate_low
= (u8
) (cnt
& 0xff);
1585 *rate_hi
= (u8
) ((cnt
>> 8) & 0xff);
1586 if (rate_low
&& rate_hi
)
1587 dbg("%s - %d %02x %02x.",
1588 __func__
, baud_rate
, *rate_hi
, *rate_low
);
1589 return KEYSPAN_BAUD_RATE_OK
;
1592 /* usa19hs function doesn't require prescaler */
1593 static int keyspan_usa19hs_calc_baud(u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1594 u8
*rate_low
, u8
*prescaler
, int portnum
)
1596 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1599 dbg("%s - %d.", __func__
, baud_rate
);
1601 /* prevent divide by zero... */
1602 b16
= baud_rate
* 16L;
1604 return KEYSPAN_INVALID_BAUD_RATE
;
1606 /* calculate the divisor */
1607 div
= baudclk
/ b16
;
1609 return KEYSPAN_INVALID_BAUD_RATE
;
1612 return KEYSPAN_INVALID_BAUD_RATE
;
1614 /* return the counter values if non-null */
1616 *rate_low
= (u8
) (div
& 0xff);
1619 *rate_hi
= (u8
) ((div
>> 8) & 0xff);
1621 if (rate_low
&& rate_hi
)
1622 dbg("%s - %d %02x %02x.",
1623 __func__
, baud_rate
, *rate_hi
, *rate_low
);
1625 return KEYSPAN_BAUD_RATE_OK
;
1628 static int keyspan_usa19w_calc_baud(u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1629 u8
*rate_low
, u8
*prescaler
, int portnum
)
1631 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1632 clk
, /* clock with 13/8 prescaler */
1633 div
, /* divisor using 13/8 prescaler */
1634 res
, /* resulting baud rate using 13/8 prescaler */
1635 diff
, /* error using 13/8 prescaler */
1640 dbg("%s - %d.", __func__
, baud_rate
);
1642 /* prevent divide by zero */
1643 b16
= baud_rate
* 16L;
1645 return KEYSPAN_INVALID_BAUD_RATE
;
1647 /* Calculate prescaler by trying them all and looking
1650 /* start with largest possible difference */
1651 smallest_diff
= 0xffffffff;
1653 /* 0 is an invalid prescaler, used as a flag */
1656 for (i
= 8; i
<= 0xff; ++i
) {
1657 clk
= (baudclk
* 8) / (u32
) i
;
1664 diff
= (res
> b16
) ? (res
-b16
) : (b16
-res
);
1666 if (diff
< smallest_diff
) {
1668 smallest_diff
= diff
;
1672 if (best_prescaler
== 0)
1673 return KEYSPAN_INVALID_BAUD_RATE
;
1675 clk
= (baudclk
* 8) / (u32
) best_prescaler
;
1678 /* return the divisor and prescaler if non-null */
1680 *rate_low
= (u8
) (div
& 0xff);
1682 *rate_hi
= (u8
) ((div
>> 8) & 0xff);
1684 *prescaler
= best_prescaler
;
1685 /* dbg("%s - %d %d", __func__, *prescaler, div); */
1687 return KEYSPAN_BAUD_RATE_OK
;
1690 /* USA-28 supports different maximum baud rates on each port */
1691 static int keyspan_usa28_calc_baud(u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1692 u8
*rate_low
, u8
*prescaler
, int portnum
)
1694 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1696 cnt
; /* inverse of divisor (programmed into 8051) */
1698 dbg("%s - %d.", __func__
, baud_rate
);
1700 /* prevent divide by zero */
1701 b16
= baud_rate
* 16L;
1703 return KEYSPAN_INVALID_BAUD_RATE
;
1705 /* calculate the divisor and the counter (its inverse) */
1706 div
= KEYSPAN_USA28_BAUDCLK
/ b16
;
1708 return KEYSPAN_INVALID_BAUD_RATE
;
1712 /* check for out of range, based on portnum,
1713 and return result */
1716 return KEYSPAN_INVALID_BAUD_RATE
;
1720 return KEYSPAN_INVALID_BAUD_RATE
;
1722 return KEYSPAN_INVALID_BAUD_RATE
;
1725 /* return the counter values if not NULL
1726 (port 1 will ignore retHi) */
1728 *rate_low
= (u8
) (cnt
& 0xff);
1730 *rate_hi
= (u8
) ((cnt
>> 8) & 0xff);
1731 dbg("%s - %d OK.", __func__
, baud_rate
);
1732 return KEYSPAN_BAUD_RATE_OK
;
1735 static int keyspan_usa26_send_setup(struct usb_serial
*serial
,
1736 struct usb_serial_port
*port
,
1739 struct keyspan_usa26_portControlMessage msg
;
1740 struct keyspan_serial_private
*s_priv
;
1741 struct keyspan_port_private
*p_priv
;
1742 const struct keyspan_device_details
*d_details
;
1744 struct urb
*this_urb
;
1745 int device_port
, err
;
1747 dbg("%s reset=%d", __func__
, reset_port
);
1749 s_priv
= usb_get_serial_data(serial
);
1750 p_priv
= usb_get_serial_port_data(port
);
1751 d_details
= s_priv
->device_details
;
1752 device_port
= port
->number
- port
->serial
->minor
;
1754 outcont_urb
= d_details
->outcont_endpoints
[port
->number
];
1755 this_urb
= p_priv
->outcont_urb
;
1757 dbg("%s - endpoint %d", __func__
, usb_pipeendpoint(this_urb
->pipe
));
1759 /* Make sure we have an urb then send the message */
1760 if (this_urb
== NULL
) {
1761 dbg("%s - oops no urb.", __func__
);
1765 /* Save reset port val for resend.
1766 Don't overwrite resend for open/close condition. */
1767 if ((reset_port
+ 1) > p_priv
->resend_cont
)
1768 p_priv
->resend_cont
= reset_port
+ 1;
1769 if (this_urb
->status
== -EINPROGRESS
) {
1770 /* dbg("%s - already writing", __func__); */
1775 memset(&msg
, 0, sizeof(struct keyspan_usa26_portControlMessage
));
1777 /* Only set baud rate if it's changed */
1778 if (p_priv
->old_baud
!= p_priv
->baud
) {
1779 p_priv
->old_baud
= p_priv
->baud
;
1780 msg
.setClocking
= 0xff;
1781 if (d_details
->calculate_baud_rate
1782 (p_priv
->baud
, d_details
->baudclk
, &msg
.baudHi
,
1783 &msg
.baudLo
, &msg
.prescaler
, device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
1784 dbg("%s - Invalid baud rate %d requested, using 9600.",
1785 __func__
, p_priv
->baud
);
1787 msg
.baudHi
= 125; /* Values for 9600 baud */
1790 msg
.setPrescaler
= 0xff;
1793 msg
.lcr
= (p_priv
->cflag
& CSTOPB
)? STOPBITS_678_2
: STOPBITS_5678_1
;
1794 switch (p_priv
->cflag
& CSIZE
) {
1796 msg
.lcr
|= USA_DATABITS_5
;
1799 msg
.lcr
|= USA_DATABITS_6
;
1802 msg
.lcr
|= USA_DATABITS_7
;
1805 msg
.lcr
|= USA_DATABITS_8
;
1808 if (p_priv
->cflag
& PARENB
) {
1809 /* note USA_PARITY_NONE == 0 */
1810 msg
.lcr
|= (p_priv
->cflag
& PARODD
)?
1811 USA_PARITY_ODD
: USA_PARITY_EVEN
;
1815 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
1816 msg
.xonFlowControl
= 0;
1817 msg
.setFlowControl
= 0xff;
1818 msg
.forwardingLength
= 16;
1823 if (reset_port
== 1) {
1832 msg
.returnStatus
= 0;
1833 msg
.resetDataToggle
= 0xff;
1837 else if (reset_port
== 2) {
1846 msg
.returnStatus
= 0;
1847 msg
.resetDataToggle
= 0;
1850 /* Sending intermediate configs */
1852 msg
._txOn
= (!p_priv
->break_on
);
1855 msg
.txBreak
= (p_priv
->break_on
);
1860 msg
.returnStatus
= 0;
1861 msg
.resetDataToggle
= 0x0;
1864 /* Do handshaking outputs */
1865 msg
.setTxTriState_setRts
= 0xff;
1866 msg
.txTriState_rts
= p_priv
->rts_state
;
1868 msg
.setHskoa_setDtr
= 0xff;
1869 msg
.hskoa_dtr
= p_priv
->dtr_state
;
1871 p_priv
->resend_cont
= 0;
1872 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
1874 /* send the data out the device on control endpoint */
1875 this_urb
->transfer_buffer_length
= sizeof(msg
);
1877 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
1879 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__
, err
);
1882 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
1883 outcont_urb
, this_urb
->transfer_buffer_length
,
1884 usb_pipeendpoint(this_urb
->pipe
));
1891 static int keyspan_usa28_send_setup(struct usb_serial
*serial
,
1892 struct usb_serial_port
*port
,
1895 struct keyspan_usa28_portControlMessage msg
;
1896 struct keyspan_serial_private
*s_priv
;
1897 struct keyspan_port_private
*p_priv
;
1898 const struct keyspan_device_details
*d_details
;
1899 struct urb
*this_urb
;
1900 int device_port
, err
;
1902 dbg("%s", __func__
);
1904 s_priv
= usb_get_serial_data(serial
);
1905 p_priv
= usb_get_serial_port_data(port
);
1906 d_details
= s_priv
->device_details
;
1907 device_port
= port
->number
- port
->serial
->minor
;
1909 /* only do something if we have a bulk out endpoint */
1910 this_urb
= p_priv
->outcont_urb
;
1911 if (this_urb
== NULL
) {
1912 dbg("%s - oops no urb.", __func__
);
1916 /* Save reset port val for resend.
1917 Don't overwrite resend for open/close condition. */
1918 if ((reset_port
+ 1) > p_priv
->resend_cont
)
1919 p_priv
->resend_cont
= reset_port
+ 1;
1920 if (this_urb
->status
== -EINPROGRESS
) {
1921 dbg("%s already writing", __func__
);
1926 memset(&msg
, 0, sizeof(struct keyspan_usa28_portControlMessage
));
1928 msg
.setBaudRate
= 1;
1929 if (d_details
->calculate_baud_rate(p_priv
->baud
, d_details
->baudclk
,
1930 &msg
.baudHi
, &msg
.baudLo
, NULL
, device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
1931 dbg("%s - Invalid baud rate requested %d.",
1932 __func__
, p_priv
->baud
);
1934 msg
.baudHi
= 0xb2; /* Values for 9600 baud */
1937 /* If parity is enabled, we must calculate it ourselves. */
1938 msg
.parity
= 0; /* XXX for now */
1940 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
1941 msg
.xonFlowControl
= 0;
1943 /* Do handshaking outputs, DTR is inverted relative to RTS */
1944 msg
.rts
= p_priv
->rts_state
;
1945 msg
.dtr
= p_priv
->dtr_state
;
1947 msg
.forwardingLength
= 16;
1949 msg
.breakThreshold
= 45;
1953 /*msg.returnStatus = 1;
1954 msg.resetDataToggle = 0xff;*/
1956 if (reset_port
== 1) {
1960 msg
.txForceXoff
= 0;
1966 msg
.returnStatus
= 0;
1967 msg
.resetDataToggle
= 0xff;
1970 else if (reset_port
== 2) {
1974 msg
.txForceXoff
= 0;
1980 msg
.returnStatus
= 0;
1981 msg
.resetDataToggle
= 0;
1983 /* Sending intermediate configs */
1985 msg
._txOn
= (!p_priv
->break_on
);
1988 msg
.txForceXoff
= 0;
1989 msg
.txBreak
= (p_priv
->break_on
);
1994 msg
.returnStatus
= 0;
1995 msg
.resetDataToggle
= 0x0;
1998 p_priv
->resend_cont
= 0;
1999 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2001 /* send the data out the device on control endpoint */
2002 this_urb
->transfer_buffer_length
= sizeof(msg
);
2004 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2006 dbg("%s - usb_submit_urb(setup) failed", __func__
);
2009 dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__
,
2010 this_urb
->transfer_buffer_length
);
2017 static int keyspan_usa49_send_setup(struct usb_serial
*serial
,
2018 struct usb_serial_port
*port
,
2021 struct keyspan_usa49_portControlMessage msg
;
2022 struct usb_ctrlrequest
*dr
= NULL
;
2023 struct keyspan_serial_private
*s_priv
;
2024 struct keyspan_port_private
*p_priv
;
2025 const struct keyspan_device_details
*d_details
;
2026 struct urb
*this_urb
;
2027 int err
, device_port
;
2029 dbg("%s", __func__
);
2031 s_priv
= usb_get_serial_data(serial
);
2032 p_priv
= usb_get_serial_port_data(port
);
2033 d_details
= s_priv
->device_details
;
2035 this_urb
= s_priv
->glocont_urb
;
2037 /* Work out which port within the device is being setup */
2038 device_port
= port
->number
- port
->serial
->minor
;
2040 /* Make sure we have an urb then send the message */
2041 if (this_urb
== NULL
) {
2042 dbg("%s - oops no urb for port %d.", __func__
, port
->number
);
2046 dbg("%s - endpoint %d port %d (%d)",
2047 __func__
, usb_pipeendpoint(this_urb
->pipe
),
2048 port
->number
, device_port
);
2050 /* Save reset port val for resend.
2051 Don't overwrite resend for open/close condition. */
2052 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2053 p_priv
->resend_cont
= reset_port
+ 1;
2055 if (this_urb
->status
== -EINPROGRESS
) {
2056 /* dbg("%s - already writing", __func__); */
2061 memset(&msg
, 0, sizeof(struct keyspan_usa49_portControlMessage
));
2063 /*msg.portNumber = port->number;*/
2064 msg
.portNumber
= device_port
;
2066 /* Only set baud rate if it's changed */
2067 if (p_priv
->old_baud
!= p_priv
->baud
) {
2068 p_priv
->old_baud
= p_priv
->baud
;
2069 msg
.setClocking
= 0xff;
2070 if (d_details
->calculate_baud_rate
2071 (p_priv
->baud
, d_details
->baudclk
, &msg
.baudHi
,
2072 &msg
.baudLo
, &msg
.prescaler
, device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
2073 dbg("%s - Invalid baud rate %d requested, using 9600.",
2074 __func__
, p_priv
->baud
);
2076 msg
.baudHi
= 125; /* Values for 9600 baud */
2079 /* msg.setPrescaler = 0xff; */
2082 msg
.lcr
= (p_priv
->cflag
& CSTOPB
)? STOPBITS_678_2
: STOPBITS_5678_1
;
2083 switch (p_priv
->cflag
& CSIZE
) {
2085 msg
.lcr
|= USA_DATABITS_5
;
2088 msg
.lcr
|= USA_DATABITS_6
;
2091 msg
.lcr
|= USA_DATABITS_7
;
2094 msg
.lcr
|= USA_DATABITS_8
;
2097 if (p_priv
->cflag
& PARENB
) {
2098 /* note USA_PARITY_NONE == 0 */
2099 msg
.lcr
|= (p_priv
->cflag
& PARODD
)?
2100 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2104 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
2105 msg
.xonFlowControl
= 0;
2106 msg
.setFlowControl
= 0xff;
2108 msg
.forwardingLength
= 16;
2113 if (reset_port
== 1) {
2122 msg
.returnStatus
= 0;
2123 msg
.resetDataToggle
= 0xff;
2125 msg
.disablePort
= 0;
2128 else if (reset_port
== 2) {
2137 msg
.returnStatus
= 0;
2138 msg
.resetDataToggle
= 0;
2140 msg
.disablePort
= 1;
2142 /* Sending intermediate configs */
2144 msg
._txOn
= (!p_priv
->break_on
);
2147 msg
.txBreak
= (p_priv
->break_on
);
2152 msg
.returnStatus
= 0;
2153 msg
.resetDataToggle
= 0x0;
2155 msg
.disablePort
= 0;
2158 /* Do handshaking outputs */
2160 msg
.rts
= p_priv
->rts_state
;
2163 msg
.dtr
= p_priv
->dtr_state
;
2165 p_priv
->resend_cont
= 0;
2167 /* if the device is a 49wg, we send control message on usb
2170 if (d_details
->product_id
== keyspan_usa49wg_product_id
) {
2171 dr
= (void *)(s_priv
->ctrl_buf
);
2172 dr
->bRequestType
= USB_TYPE_VENDOR
| USB_DIR_OUT
;
2173 dr
->bRequest
= 0xB0; /* 49wg control message */;
2176 dr
->wLength
= cpu_to_le16(sizeof(msg
));
2178 memcpy(s_priv
->glocont_buf
, &msg
, sizeof(msg
));
2180 usb_fill_control_urb(this_urb
, serial
->dev
,
2181 usb_sndctrlpipe(serial
->dev
, 0),
2182 (unsigned char *)dr
, s_priv
->glocont_buf
,
2183 sizeof(msg
), usa49_glocont_callback
, serial
);
2186 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2188 /* send the data out the device on control endpoint */
2189 this_urb
->transfer_buffer_length
= sizeof(msg
);
2191 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2193 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__
, err
);
2196 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
,
2197 outcont_urb
, this_urb
->transfer_buffer_length
,
2198 usb_pipeendpoint(this_urb
->pipe
));
2205 static int keyspan_usa90_send_setup(struct usb_serial
*serial
,
2206 struct usb_serial_port
*port
,
2209 struct keyspan_usa90_portControlMessage msg
;
2210 struct keyspan_serial_private
*s_priv
;
2211 struct keyspan_port_private
*p_priv
;
2212 const struct keyspan_device_details
*d_details
;
2213 struct urb
*this_urb
;
2217 dbg("%s", __func__
);
2219 s_priv
= usb_get_serial_data(serial
);
2220 p_priv
= usb_get_serial_port_data(port
);
2221 d_details
= s_priv
->device_details
;
2223 /* only do something if we have a bulk out endpoint */
2224 this_urb
= p_priv
->outcont_urb
;
2225 if (this_urb
== NULL
) {
2226 dbg("%s - oops no urb.", __func__
);
2230 /* Save reset port val for resend.
2231 Don't overwrite resend for open/close condition. */
2232 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2233 p_priv
->resend_cont
= reset_port
+ 1;
2234 if (this_urb
->status
== -EINPROGRESS
) {
2235 dbg("%s already writing", __func__
);
2240 memset(&msg
, 0, sizeof(struct keyspan_usa90_portControlMessage
));
2242 /* Only set baud rate if it's changed */
2243 if (p_priv
->old_baud
!= p_priv
->baud
) {
2244 p_priv
->old_baud
= p_priv
->baud
;
2245 msg
.setClocking
= 0x01;
2246 if (d_details
->calculate_baud_rate
2247 (p_priv
->baud
, d_details
->baudclk
, &msg
.baudHi
,
2248 &msg
.baudLo
, &prescaler
, 0) == KEYSPAN_INVALID_BAUD_RATE
) {
2249 dbg("%s - Invalid baud rate %d requested, using 9600.",
2250 __func__
, p_priv
->baud
);
2251 p_priv
->baud
= 9600;
2252 d_details
->calculate_baud_rate(p_priv
->baud
, d_details
->baudclk
,
2253 &msg
.baudHi
, &msg
.baudLo
, &prescaler
, 0);
2259 /* modes must always be correctly specified */
2260 if (p_priv
->baud
> 57600) {
2261 msg
.rxMode
= RXMODE_DMA
;
2262 msg
.txMode
= TXMODE_DMA
;
2264 msg
.rxMode
= RXMODE_BYHAND
;
2265 msg
.txMode
= TXMODE_BYHAND
;
2268 msg
.lcr
= (p_priv
->cflag
& CSTOPB
)? STOPBITS_678_2
: STOPBITS_5678_1
;
2269 switch (p_priv
->cflag
& CSIZE
) {
2271 msg
.lcr
|= USA_DATABITS_5
;
2274 msg
.lcr
|= USA_DATABITS_6
;
2277 msg
.lcr
|= USA_DATABITS_7
;
2280 msg
.lcr
|= USA_DATABITS_8
;
2283 if (p_priv
->cflag
& PARENB
) {
2284 /* note USA_PARITY_NONE == 0 */
2285 msg
.lcr
|= (p_priv
->cflag
& PARODD
)?
2286 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2288 if (p_priv
->old_cflag
!= p_priv
->cflag
) {
2289 p_priv
->old_cflag
= p_priv
->cflag
;
2293 if (p_priv
->flow_control
== flow_cts
)
2294 msg
.txFlowControl
= TXFLOW_CTS
;
2295 msg
.setTxFlowControl
= 0x01;
2296 msg
.setRxFlowControl
= 0x01;
2298 msg
.rxForwardingLength
= 16;
2299 msg
.rxForwardingTimeout
= 16;
2300 msg
.txAckSetting
= 0;
2305 if (reset_port
== 1) {
2306 msg
.portEnabled
= 1;
2308 msg
.txBreak
= (p_priv
->break_on
);
2311 else if (reset_port
== 2)
2312 msg
.portEnabled
= 0;
2313 /* Sending intermediate configs */
2315 msg
.portEnabled
= 1;
2316 msg
.txBreak
= (p_priv
->break_on
);
2319 /* Do handshaking outputs */
2321 msg
.rts
= p_priv
->rts_state
;
2324 msg
.dtr
= p_priv
->dtr_state
;
2326 p_priv
->resend_cont
= 0;
2327 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2329 /* send the data out the device on control endpoint */
2330 this_urb
->transfer_buffer_length
= sizeof(msg
);
2332 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2334 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__
, err
);
2338 static int keyspan_usa67_send_setup(struct usb_serial
*serial
,
2339 struct usb_serial_port
*port
,
2342 struct keyspan_usa67_portControlMessage msg
;
2343 struct keyspan_serial_private
*s_priv
;
2344 struct keyspan_port_private
*p_priv
;
2345 const struct keyspan_device_details
*d_details
;
2346 struct urb
*this_urb
;
2347 int err
, device_port
;
2349 dbg("%s", __func__
);
2351 s_priv
= usb_get_serial_data(serial
);
2352 p_priv
= usb_get_serial_port_data(port
);
2353 d_details
= s_priv
->device_details
;
2355 this_urb
= s_priv
->glocont_urb
;
2357 /* Work out which port within the device is being setup */
2358 device_port
= port
->number
- port
->serial
->minor
;
2360 /* Make sure we have an urb then send the message */
2361 if (this_urb
== NULL
) {
2362 dbg("%s - oops no urb for port %d.", __func__
,
2367 /* Save reset port val for resend.
2368 Don't overwrite resend for open/close condition. */
2369 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2370 p_priv
->resend_cont
= reset_port
+ 1;
2371 if (this_urb
->status
== -EINPROGRESS
) {
2372 /* dbg("%s - already writing", __func__); */
2377 memset(&msg
, 0, sizeof(struct keyspan_usa67_portControlMessage
));
2379 msg
.port
= device_port
;
2381 /* Only set baud rate if it's changed */
2382 if (p_priv
->old_baud
!= p_priv
->baud
) {
2383 p_priv
->old_baud
= p_priv
->baud
;
2384 msg
.setClocking
= 0xff;
2385 if (d_details
->calculate_baud_rate
2386 (p_priv
->baud
, d_details
->baudclk
, &msg
.baudHi
,
2387 &msg
.baudLo
, &msg
.prescaler
, device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
2388 dbg("%s - Invalid baud rate %d requested, using 9600.",
2389 __func__
, p_priv
->baud
);
2391 msg
.baudHi
= 125; /* Values for 9600 baud */
2394 msg
.setPrescaler
= 0xff;
2397 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
2398 switch (p_priv
->cflag
& CSIZE
) {
2400 msg
.lcr
|= USA_DATABITS_5
;
2403 msg
.lcr
|= USA_DATABITS_6
;
2406 msg
.lcr
|= USA_DATABITS_7
;
2409 msg
.lcr
|= USA_DATABITS_8
;
2412 if (p_priv
->cflag
& PARENB
) {
2413 /* note USA_PARITY_NONE == 0 */
2414 msg
.lcr
|= (p_priv
->cflag
& PARODD
)?
2415 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2419 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
2420 msg
.xonFlowControl
= 0;
2421 msg
.setFlowControl
= 0xff;
2422 msg
.forwardingLength
= 16;
2426 if (reset_port
== 1) {
2436 msg
.returnStatus
= 0;
2437 msg
.resetDataToggle
= 0xff;
2438 } else if (reset_port
== 2) {
2448 msg
.returnStatus
= 0;
2449 msg
.resetDataToggle
= 0;
2451 /* Sending intermediate configs */
2452 msg
._txOn
= (!p_priv
->break_on
);
2455 msg
.txBreak
= (p_priv
->break_on
);
2460 msg
.returnStatus
= 0;
2461 msg
.resetDataToggle
= 0x0;
2464 /* Do handshaking outputs */
2465 msg
.setTxTriState_setRts
= 0xff;
2466 msg
.txTriState_rts
= p_priv
->rts_state
;
2468 msg
.setHskoa_setDtr
= 0xff;
2469 msg
.hskoa_dtr
= p_priv
->dtr_state
;
2471 p_priv
->resend_cont
= 0;
2473 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2475 /* send the data out the device on control endpoint */
2476 this_urb
->transfer_buffer_length
= sizeof(msg
);
2478 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2480 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__
,
2485 static void keyspan_send_setup(struct usb_serial_port
*port
, int reset_port
)
2487 struct usb_serial
*serial
= port
->serial
;
2488 struct keyspan_serial_private
*s_priv
;
2489 const struct keyspan_device_details
*d_details
;
2491 dbg("%s", __func__
);
2493 s_priv
= usb_get_serial_data(serial
);
2494 d_details
= s_priv
->device_details
;
2496 switch (d_details
->msg_format
) {
2498 keyspan_usa26_send_setup(serial
, port
, reset_port
);
2501 keyspan_usa28_send_setup(serial
, port
, reset_port
);
2504 keyspan_usa49_send_setup(serial
, port
, reset_port
);
2507 keyspan_usa90_send_setup(serial
, port
, reset_port
);
2510 keyspan_usa67_send_setup(serial
, port
, reset_port
);
2516 /* Gets called by the "real" driver (ie once firmware is loaded
2517 and renumeration has taken place. */
2518 static int keyspan_startup(struct usb_serial
*serial
)
2521 struct usb_serial_port
*port
;
2522 struct keyspan_serial_private
*s_priv
;
2523 struct keyspan_port_private
*p_priv
;
2524 const struct keyspan_device_details
*d_details
;
2526 dbg("%s", __func__
);
2528 for (i
= 0; (d_details
= keyspan_devices
[i
]) != NULL
; ++i
)
2529 if (d_details
->product_id
==
2530 le16_to_cpu(serial
->dev
->descriptor
.idProduct
))
2532 if (d_details
== NULL
) {
2533 dev_err(&serial
->dev
->dev
, "%s - unknown product id %x\n",
2534 __func__
, le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
2538 /* Setup private data for serial driver */
2539 s_priv
= kzalloc(sizeof(struct keyspan_serial_private
), GFP_KERNEL
);
2541 dbg("%s - kmalloc for keyspan_serial_private failed.",
2546 s_priv
->device_details
= d_details
;
2547 usb_set_serial_data(serial
, s_priv
);
2549 /* Now setup per port private data */
2550 for (i
= 0; i
< serial
->num_ports
; i
++) {
2551 port
= serial
->port
[i
];
2552 p_priv
= kzalloc(sizeof(struct keyspan_port_private
),
2555 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__
, i
);
2558 p_priv
->device_details
= d_details
;
2559 usb_set_serial_port_data(port
, p_priv
);
2562 keyspan_setup_urbs(serial
);
2564 if (s_priv
->instat_urb
!= NULL
) {
2565 err
= usb_submit_urb(s_priv
->instat_urb
, GFP_KERNEL
);
2567 dbg("%s - submit instat urb failed %d", __func__
,
2570 if (s_priv
->indat_urb
!= NULL
) {
2571 err
= usb_submit_urb(s_priv
->indat_urb
, GFP_KERNEL
);
2573 dbg("%s - submit indat urb failed %d", __func__
,
2580 static void keyspan_disconnect(struct usb_serial
*serial
)
2583 struct usb_serial_port
*port
;
2584 struct keyspan_serial_private
*s_priv
;
2585 struct keyspan_port_private
*p_priv
;
2587 dbg("%s", __func__
);
2589 s_priv
= usb_get_serial_data(serial
);
2591 /* Stop reading/writing urbs */
2592 stop_urb(s_priv
->instat_urb
);
2593 stop_urb(s_priv
->glocont_urb
);
2594 stop_urb(s_priv
->indat_urb
);
2595 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2596 port
= serial
->port
[i
];
2597 p_priv
= usb_get_serial_port_data(port
);
2598 stop_urb(p_priv
->inack_urb
);
2599 stop_urb(p_priv
->outcont_urb
);
2600 for (j
= 0; j
< 2; j
++) {
2601 stop_urb(p_priv
->in_urbs
[j
]);
2602 stop_urb(p_priv
->out_urbs
[j
]);
2607 usb_free_urb(s_priv
->instat_urb
);
2608 usb_free_urb(s_priv
->indat_urb
);
2609 usb_free_urb(s_priv
->glocont_urb
);
2610 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2611 port
= serial
->port
[i
];
2612 p_priv
= usb_get_serial_port_data(port
);
2613 usb_free_urb(p_priv
->inack_urb
);
2614 usb_free_urb(p_priv
->outcont_urb
);
2615 for (j
= 0; j
< 2; j
++) {
2616 usb_free_urb(p_priv
->in_urbs
[j
]);
2617 usb_free_urb(p_priv
->out_urbs
[j
]);
2622 static void keyspan_release(struct usb_serial
*serial
)
2625 struct usb_serial_port
*port
;
2626 struct keyspan_serial_private
*s_priv
;
2628 dbg("%s", __func__
);
2630 s_priv
= usb_get_serial_data(serial
);
2632 /* dbg("Freeing serial->private."); */
2635 /* dbg("Freeing port->private."); */
2636 /* Now free per port private data */
2637 for (i
= 0; i
< serial
->num_ports
; i
++) {
2638 port
= serial
->port
[i
];
2639 kfree(usb_get_serial_port_data(port
));
2643 MODULE_AUTHOR(DRIVER_AUTHOR
);
2644 MODULE_DESCRIPTION(DRIVER_DESC
);
2645 MODULE_LICENSE("GPL");
2647 MODULE_FIRMWARE("keyspan/usa28.fw");
2648 MODULE_FIRMWARE("keyspan/usa28x.fw");
2649 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2650 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2651 MODULE_FIRMWARE("keyspan/usa19.fw");
2652 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2653 MODULE_FIRMWARE("keyspan/mpr.fw");
2654 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2655 MODULE_FIRMWARE("keyspan/usa18x.fw");
2656 MODULE_FIRMWARE("keyspan/usa19w.fw");
2657 MODULE_FIRMWARE("keyspan/usa49w.fw");
2658 MODULE_FIRMWARE("keyspan/usa49wlc.fw");
2660 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
2661 MODULE_PARM_DESC(debug
, "Debug enabled or not");