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/uaccess.h>
42 #include <linux/usb.h>
43 #include <linux/usb/serial.h>
44 #include <linux/usb/ezusb.h>
47 #define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
48 #define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
50 #define INSTAT_BUFLEN 32
51 #define GLOCONT_BUFLEN 64
52 #define INDAT49W_BUFLEN 512
55 #define INACK_BUFLEN 1
56 #define OUTCONT_BUFLEN 64
58 /* Per device and per port private data */
59 struct keyspan_serial_private
{
60 const struct keyspan_device_details
*device_details
;
62 struct urb
*instat_urb
;
65 /* added to support 49wg, where data from all 4 ports comes in
66 on 1 EP and high-speed supported */
67 struct urb
*indat_urb
;
70 /* XXX this one probably will need a lock */
71 struct urb
*glocont_urb
;
73 char *ctrl_buf
; /* for EP0 control message */
76 struct keyspan_port_private
{
77 /* Keep track of which input & output endpoints to use */
81 /* Keep duplicate of device details in each port
82 structure as well - simplifies some of the
83 callback functions etc. */
84 const struct keyspan_device_details
*device_details
;
86 /* Input endpoints and buffer for this port */
87 struct urb
*in_urbs
[2];
89 /* Output endpoints and buffer for this port */
90 struct urb
*out_urbs
[2];
93 /* Input ack endpoint */
94 struct urb
*inack_urb
;
97 /* Output control endpoint */
98 struct urb
*outcont_urb
;
101 /* Settings for the port */
105 unsigned int old_cflag
;
106 enum {flow_none
, flow_cts
, flow_xon
} flow_control
;
107 int rts_state
; /* Handshaking pins (outputs) */
109 int cts_state
; /* Handshaking pins (inputs) */
115 unsigned long tx_start_time
[2];
116 int resend_cont
; /* need to resend control packet */
119 /* Include Keyspan message headers. All current Keyspan Adapters
120 make use of one of five message formats which are referred
121 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
122 within this driver. */
123 #include "keyspan_usa26msg.h"
124 #include "keyspan_usa28msg.h"
125 #include "keyspan_usa49msg.h"
126 #include "keyspan_usa90msg.h"
127 #include "keyspan_usa67msg.h"
130 module_usb_serial_driver(serial_drivers
, keyspan_ids_combined
);
132 static void keyspan_break_ctl(struct tty_struct
*tty
, int break_state
)
134 struct usb_serial_port
*port
= tty
->driver_data
;
135 struct keyspan_port_private
*p_priv
;
137 p_priv
= usb_get_serial_port_data(port
);
139 if (break_state
== -1)
140 p_priv
->break_on
= 1;
142 p_priv
->break_on
= 0;
144 keyspan_send_setup(port
, 0);
148 static void keyspan_set_termios(struct tty_struct
*tty
,
149 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
151 int baud_rate
, device_port
;
152 struct keyspan_port_private
*p_priv
;
153 const struct keyspan_device_details
*d_details
;
156 p_priv
= usb_get_serial_port_data(port
);
157 d_details
= p_priv
->device_details
;
158 cflag
= tty
->termios
.c_cflag
;
159 device_port
= port
->port_number
;
161 /* Baud rate calculation takes baud rate as an integer
162 so other rates can be generated if desired. */
163 baud_rate
= tty_get_baud_rate(tty
);
164 /* If no match or invalid, don't change */
165 if (d_details
->calculate_baud_rate(port
, baud_rate
, d_details
->baudclk
,
166 NULL
, NULL
, NULL
, device_port
) == KEYSPAN_BAUD_RATE_OK
) {
167 /* FIXME - more to do here to ensure rate changes cleanly */
168 /* FIXME - calcuate exact rate from divisor ? */
169 p_priv
->baud
= baud_rate
;
171 baud_rate
= tty_termios_baud_rate(old_termios
);
173 tty_encode_baud_rate(tty
, baud_rate
, baud_rate
);
174 /* set CTS/RTS handshake etc. */
175 p_priv
->cflag
= cflag
;
176 p_priv
->flow_control
= (cflag
& CRTSCTS
) ? flow_cts
: flow_none
;
178 /* Mark/Space not supported */
179 tty
->termios
.c_cflag
&= ~CMSPAR
;
181 keyspan_send_setup(port
, 0);
184 static int keyspan_tiocmget(struct tty_struct
*tty
)
186 struct usb_serial_port
*port
= tty
->driver_data
;
187 struct keyspan_port_private
*p_priv
= usb_get_serial_port_data(port
);
190 value
= ((p_priv
->rts_state
) ? TIOCM_RTS
: 0) |
191 ((p_priv
->dtr_state
) ? TIOCM_DTR
: 0) |
192 ((p_priv
->cts_state
) ? TIOCM_CTS
: 0) |
193 ((p_priv
->dsr_state
) ? TIOCM_DSR
: 0) |
194 ((p_priv
->dcd_state
) ? TIOCM_CAR
: 0) |
195 ((p_priv
->ri_state
) ? TIOCM_RNG
: 0);
200 static int keyspan_tiocmset(struct tty_struct
*tty
,
201 unsigned int set
, unsigned int clear
)
203 struct usb_serial_port
*port
= tty
->driver_data
;
204 struct keyspan_port_private
*p_priv
= usb_get_serial_port_data(port
);
207 p_priv
->rts_state
= 1;
209 p_priv
->dtr_state
= 1;
210 if (clear
& TIOCM_RTS
)
211 p_priv
->rts_state
= 0;
212 if (clear
& TIOCM_DTR
)
213 p_priv
->dtr_state
= 0;
214 keyspan_send_setup(port
, 0);
218 /* Write function is similar for the four protocols used
219 with only a minor change for usa90 (usa19hs) required */
220 static int keyspan_write(struct tty_struct
*tty
,
221 struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
223 struct keyspan_port_private
*p_priv
;
224 const struct keyspan_device_details
*d_details
;
227 struct urb
*this_urb
;
228 int err
, maxDataLen
, dataOffset
;
230 p_priv
= usb_get_serial_port_data(port
);
231 d_details
= p_priv
->device_details
;
233 if (d_details
->msg_format
== msg_usa90
) {
241 dev_dbg(&port
->dev
, "%s - %d chars, flip=%d\n", __func__
, count
,
244 for (left
= count
; left
> 0; left
-= todo
) {
246 if (todo
> maxDataLen
)
249 flip
= p_priv
->out_flip
;
251 /* Check we have a valid urb/endpoint before we use it... */
252 this_urb
= p_priv
->out_urbs
[flip
];
253 if (this_urb
== NULL
) {
254 /* no bulk out, so return 0 bytes written */
255 dev_dbg(&port
->dev
, "%s - no output urb :(\n", __func__
);
259 dev_dbg(&port
->dev
, "%s - endpoint %d flip %d\n",
260 __func__
, usb_pipeendpoint(this_urb
->pipe
), flip
);
262 if (this_urb
->status
== -EINPROGRESS
) {
263 if (time_before(jiffies
,
264 p_priv
->tx_start_time
[flip
] + 10 * HZ
))
266 usb_unlink_urb(this_urb
);
270 /* First byte in buffer is "last flag" (except for usa19hx)
271 - unused so for now so set to zero */
272 ((char *)this_urb
->transfer_buffer
)[0] = 0;
274 memcpy(this_urb
->transfer_buffer
+ dataOffset
, buf
, todo
);
277 /* send the data out the bulk port */
278 this_urb
->transfer_buffer_length
= todo
+ dataOffset
;
280 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
282 dev_dbg(&port
->dev
, "usb_submit_urb(write bulk) failed (%d)\n", err
);
283 p_priv
->tx_start_time
[flip
] = jiffies
;
285 /* Flip for next time if usa26 or usa28 interface
286 (not used on usa49) */
287 p_priv
->out_flip
= (flip
+ 1) & d_details
->outdat_endp_flip
;
293 static void usa26_indat_callback(struct urb
*urb
)
297 struct usb_serial_port
*port
;
298 unsigned char *data
= urb
->transfer_buffer
;
299 int status
= urb
->status
;
301 endpoint
= usb_pipeendpoint(urb
->pipe
);
304 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x on endpoint %d.\n",
305 __func__
, status
, endpoint
);
310 if (urb
->actual_length
) {
311 /* 0x80 bit is error flag */
312 if ((data
[0] & 0x80) == 0) {
313 /* no errors on individual bytes, only
314 possible overrun err */
315 if (data
[0] & RXERROR_OVERRUN
) {
316 tty_insert_flip_char(&port
->port
, 0,
319 for (i
= 1; i
< urb
->actual_length
; ++i
)
320 tty_insert_flip_char(&port
->port
, data
[i
],
323 /* some bytes had errors, every byte has status */
324 dev_dbg(&port
->dev
, "%s - RX error!!!!\n", __func__
);
325 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
327 int flag
= TTY_NORMAL
;
329 if (stat
& RXERROR_OVERRUN
) {
330 tty_insert_flip_char(&port
->port
, 0,
333 /* XXX should handle break (0x10) */
334 if (stat
& RXERROR_PARITY
)
336 else if (stat
& RXERROR_FRAMING
)
339 tty_insert_flip_char(&port
->port
, data
[i
+1],
343 tty_flip_buffer_push(&port
->port
);
346 /* Resubmit urb so we continue receiving */
347 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
349 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
352 /* Outdat handling is common for all devices */
353 static void usa2x_outdat_callback(struct urb
*urb
)
355 struct usb_serial_port
*port
;
356 struct keyspan_port_private
*p_priv
;
359 p_priv
= usb_get_serial_port_data(port
);
360 dev_dbg(&port
->dev
, "%s - urb %d\n", __func__
, urb
== p_priv
->out_urbs
[1]);
362 usb_serial_port_softint(port
);
365 static void usa26_inack_callback(struct urb
*urb
)
369 static void usa26_outcont_callback(struct urb
*urb
)
371 struct usb_serial_port
*port
;
372 struct keyspan_port_private
*p_priv
;
375 p_priv
= usb_get_serial_port_data(port
);
377 if (p_priv
->resend_cont
) {
378 dev_dbg(&port
->dev
, "%s - sending setup\n", __func__
);
379 keyspan_usa26_send_setup(port
->serial
, port
,
380 p_priv
->resend_cont
- 1);
384 static void usa26_instat_callback(struct urb
*urb
)
386 unsigned char *data
= urb
->transfer_buffer
;
387 struct keyspan_usa26_portStatusMessage
*msg
;
388 struct usb_serial
*serial
;
389 struct usb_serial_port
*port
;
390 struct keyspan_port_private
*p_priv
;
391 int old_dcd_state
, err
;
392 int status
= urb
->status
;
394 serial
= urb
->context
;
397 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
400 if (urb
->actual_length
!= 9) {
401 dev_dbg(&urb
->dev
->dev
, "%s - %d byte report??\n", __func__
, urb
->actual_length
);
405 msg
= (struct keyspan_usa26_portStatusMessage
*)data
;
408 dev_dbg(&urb
->dev
->dev
,
409 "%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
410 __func__
, msg
->port
, msg
->hskia_cts
, msg
->gpia_dcd
, msg
->dsr
,
411 msg
->ri
, msg
->_txOff
, msg
->_txXoff
, msg
->rxEnabled
,
412 msg
->controlResponse
);
415 /* Now do something useful with the data */
418 /* Check port number from message and retrieve private data */
419 if (msg
->port
>= serial
->num_ports
) {
420 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n", __func__
, msg
->port
);
423 port
= serial
->port
[msg
->port
];
424 p_priv
= usb_get_serial_port_data(port
);
428 /* Update handshaking pin state information */
429 old_dcd_state
= p_priv
->dcd_state
;
430 p_priv
->cts_state
= ((msg
->hskia_cts
) ? 1 : 0);
431 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
432 p_priv
->dcd_state
= ((msg
->gpia_dcd
) ? 1 : 0);
433 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
435 if (old_dcd_state
!= p_priv
->dcd_state
)
436 tty_port_tty_hangup(&port
->port
, true);
438 /* Resubmit urb so we continue receiving */
439 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
441 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
445 static void usa26_glocont_callback(struct urb
*urb
)
450 static void usa28_indat_callback(struct urb
*urb
)
453 struct usb_serial_port
*port
;
455 struct keyspan_port_private
*p_priv
;
456 int status
= urb
->status
;
459 p_priv
= usb_get_serial_port_data(port
);
460 data
= urb
->transfer_buffer
;
462 if (urb
!= p_priv
->in_urbs
[p_priv
->in_flip
])
467 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x on endpoint %d.\n",
468 __func__
, status
, usb_pipeendpoint(urb
->pipe
));
473 p_priv
= usb_get_serial_port_data(port
);
474 data
= urb
->transfer_buffer
;
476 if (urb
->actual_length
) {
477 tty_insert_flip_string(&port
->port
, data
,
479 tty_flip_buffer_push(&port
->port
);
482 /* Resubmit urb so we continue receiving */
483 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
485 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n",
487 p_priv
->in_flip
^= 1;
489 urb
= p_priv
->in_urbs
[p_priv
->in_flip
];
490 } while (urb
->status
!= -EINPROGRESS
);
493 static void usa28_inack_callback(struct urb
*urb
)
497 static void usa28_outcont_callback(struct urb
*urb
)
499 struct usb_serial_port
*port
;
500 struct keyspan_port_private
*p_priv
;
503 p_priv
= usb_get_serial_port_data(port
);
505 if (p_priv
->resend_cont
) {
506 dev_dbg(&port
->dev
, "%s - sending setup\n", __func__
);
507 keyspan_usa28_send_setup(port
->serial
, port
,
508 p_priv
->resend_cont
- 1);
512 static void usa28_instat_callback(struct urb
*urb
)
515 unsigned char *data
= urb
->transfer_buffer
;
516 struct keyspan_usa28_portStatusMessage
*msg
;
517 struct usb_serial
*serial
;
518 struct usb_serial_port
*port
;
519 struct keyspan_port_private
*p_priv
;
521 int status
= urb
->status
;
523 serial
= urb
->context
;
526 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
530 if (urb
->actual_length
!= sizeof(struct keyspan_usa28_portStatusMessage
)) {
531 dev_dbg(&urb
->dev
->dev
, "%s - bad length %d\n", __func__
, urb
->actual_length
);
535 /*dev_dbg(&urb->dev->dev, "%s %12ph", __func__, data);*/
537 /* Now do something useful with the data */
538 msg
= (struct keyspan_usa28_portStatusMessage
*)data
;
540 /* Check port number from message and retrieve private data */
541 if (msg
->port
>= serial
->num_ports
) {
542 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n", __func__
, msg
->port
);
545 port
= serial
->port
[msg
->port
];
546 p_priv
= usb_get_serial_port_data(port
);
550 /* Update handshaking pin state information */
551 old_dcd_state
= p_priv
->dcd_state
;
552 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
553 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
554 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
555 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
557 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
)
558 tty_port_tty_hangup(&port
->port
, true);
560 /* Resubmit urb so we continue receiving */
561 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
563 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
567 static void usa28_glocont_callback(struct urb
*urb
)
572 static void usa49_glocont_callback(struct urb
*urb
)
574 struct usb_serial
*serial
;
575 struct usb_serial_port
*port
;
576 struct keyspan_port_private
*p_priv
;
579 serial
= urb
->context
;
580 for (i
= 0; i
< serial
->num_ports
; ++i
) {
581 port
= serial
->port
[i
];
582 p_priv
= usb_get_serial_port_data(port
);
584 if (p_priv
->resend_cont
) {
585 dev_dbg(&port
->dev
, "%s - sending setup\n", __func__
);
586 keyspan_usa49_send_setup(serial
, port
,
587 p_priv
->resend_cont
- 1);
593 /* This is actually called glostat in the Keyspan
595 static void usa49_instat_callback(struct urb
*urb
)
598 unsigned char *data
= urb
->transfer_buffer
;
599 struct keyspan_usa49_portStatusMessage
*msg
;
600 struct usb_serial
*serial
;
601 struct usb_serial_port
*port
;
602 struct keyspan_port_private
*p_priv
;
604 int status
= urb
->status
;
606 serial
= urb
->context
;
609 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
613 if (urb
->actual_length
!=
614 sizeof(struct keyspan_usa49_portStatusMessage
)) {
615 dev_dbg(&urb
->dev
->dev
, "%s - bad length %d\n", __func__
, urb
->actual_length
);
619 /*dev_dbg(&urb->dev->dev, "%s: %11ph", __func__, data);*/
621 /* Now do something useful with the data */
622 msg
= (struct keyspan_usa49_portStatusMessage
*)data
;
624 /* Check port number from message and retrieve private data */
625 if (msg
->portNumber
>= serial
->num_ports
) {
626 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n",
627 __func__
, msg
->portNumber
);
630 port
= serial
->port
[msg
->portNumber
];
631 p_priv
= usb_get_serial_port_data(port
);
635 /* Update handshaking pin state information */
636 old_dcd_state
= p_priv
->dcd_state
;
637 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
638 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
639 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
640 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
642 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
)
643 tty_port_tty_hangup(&port
->port
, true);
645 /* Resubmit urb so we continue receiving */
646 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
648 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
652 static void usa49_inack_callback(struct urb
*urb
)
656 static void usa49_indat_callback(struct urb
*urb
)
660 struct usb_serial_port
*port
;
661 unsigned char *data
= urb
->transfer_buffer
;
662 int status
= urb
->status
;
664 endpoint
= usb_pipeendpoint(urb
->pipe
);
667 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x on endpoint %d.\n",
668 __func__
, status
, endpoint
);
673 if (urb
->actual_length
) {
674 /* 0x80 bit is error flag */
675 if ((data
[0] & 0x80) == 0) {
676 /* no error on any byte */
677 tty_insert_flip_string(&port
->port
, data
+ 1,
678 urb
->actual_length
- 1);
680 /* some bytes had errors, every byte has status */
681 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
683 int flag
= TTY_NORMAL
;
685 if (stat
& RXERROR_OVERRUN
) {
686 tty_insert_flip_char(&port
->port
, 0,
689 /* XXX should handle break (0x10) */
690 if (stat
& RXERROR_PARITY
)
692 else if (stat
& RXERROR_FRAMING
)
695 tty_insert_flip_char(&port
->port
, data
[i
+1],
699 tty_flip_buffer_push(&port
->port
);
702 /* Resubmit urb so we continue receiving */
703 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
705 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
708 static void usa49wg_indat_callback(struct urb
*urb
)
711 struct usb_serial
*serial
;
712 struct usb_serial_port
*port
;
713 unsigned char *data
= urb
->transfer_buffer
;
714 int status
= urb
->status
;
716 serial
= urb
->context
;
719 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
723 /* inbound data is in the form P#, len, status, data */
727 while (i
< urb
->actual_length
) {
729 /* Check port number from message */
730 if (data
[i
] >= serial
->num_ports
) {
731 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n",
735 port
= serial
->port
[data
[i
++]];
738 /* 0x80 bit is error flag */
739 if ((data
[i
] & 0x80) == 0) {
740 /* no error on any byte */
742 for (x
= 1; x
< len
&& i
< urb
->actual_length
; ++x
)
743 tty_insert_flip_char(&port
->port
,
747 * some bytes had errors, every byte has status
749 for (x
= 0; x
+ 1 < len
&&
750 i
+ 1 < urb
->actual_length
; x
+= 2) {
752 int flag
= TTY_NORMAL
;
754 if (stat
& RXERROR_OVERRUN
) {
755 tty_insert_flip_char(&port
->port
, 0,
758 /* XXX should handle break (0x10) */
759 if (stat
& RXERROR_PARITY
)
761 else if (stat
& RXERROR_FRAMING
)
764 tty_insert_flip_char(&port
->port
, data
[i
+1],
769 tty_flip_buffer_push(&port
->port
);
772 /* Resubmit urb so we continue receiving */
773 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
775 dev_dbg(&urb
->dev
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
778 /* not used, usa-49 doesn't have per-port control endpoints */
779 static void usa49_outcont_callback(struct urb
*urb
)
783 static void usa90_indat_callback(struct urb
*urb
)
787 struct usb_serial_port
*port
;
788 struct keyspan_port_private
*p_priv
;
789 unsigned char *data
= urb
->transfer_buffer
;
790 int status
= urb
->status
;
792 endpoint
= usb_pipeendpoint(urb
->pipe
);
795 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x on endpoint %d.\n",
796 __func__
, status
, endpoint
);
801 p_priv
= usb_get_serial_port_data(port
);
803 if (urb
->actual_length
) {
804 /* if current mode is DMA, looks like usa28 format
805 otherwise looks like usa26 data format */
807 if (p_priv
->baud
> 57600)
808 tty_insert_flip_string(&port
->port
, data
,
811 /* 0x80 bit is error flag */
812 if ((data
[0] & 0x80) == 0) {
813 /* no errors on individual bytes, only
814 possible overrun err*/
815 if (data
[0] & RXERROR_OVERRUN
) {
816 tty_insert_flip_char(&port
->port
, 0,
819 for (i
= 1; i
< urb
->actual_length
; ++i
)
820 tty_insert_flip_char(&port
->port
,
821 data
[i
], TTY_NORMAL
);
823 /* some bytes had errors, every byte has status */
824 dev_dbg(&port
->dev
, "%s - RX error!!!!\n", __func__
);
825 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
827 int flag
= TTY_NORMAL
;
829 if (stat
& RXERROR_OVERRUN
) {
830 tty_insert_flip_char(
834 /* XXX should handle break (0x10) */
835 if (stat
& RXERROR_PARITY
)
837 else if (stat
& RXERROR_FRAMING
)
840 tty_insert_flip_char(&port
->port
,
845 tty_flip_buffer_push(&port
->port
);
848 /* Resubmit urb so we continue receiving */
849 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
851 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
855 static void usa90_instat_callback(struct urb
*urb
)
857 unsigned char *data
= urb
->transfer_buffer
;
858 struct keyspan_usa90_portStatusMessage
*msg
;
859 struct usb_serial
*serial
;
860 struct usb_serial_port
*port
;
861 struct keyspan_port_private
*p_priv
;
862 int old_dcd_state
, err
;
863 int status
= urb
->status
;
865 serial
= urb
->context
;
868 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
871 if (urb
->actual_length
< 14) {
872 dev_dbg(&urb
->dev
->dev
, "%s - %d byte report??\n", __func__
, urb
->actual_length
);
876 msg
= (struct keyspan_usa90_portStatusMessage
*)data
;
878 /* Now do something useful with the data */
880 port
= serial
->port
[0];
881 p_priv
= usb_get_serial_port_data(port
);
885 /* Update handshaking pin state information */
886 old_dcd_state
= p_priv
->dcd_state
;
887 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
888 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
889 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
890 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
892 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
)
893 tty_port_tty_hangup(&port
->port
, true);
895 /* Resubmit urb so we continue receiving */
896 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
898 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
903 static void usa90_outcont_callback(struct urb
*urb
)
905 struct usb_serial_port
*port
;
906 struct keyspan_port_private
*p_priv
;
909 p_priv
= usb_get_serial_port_data(port
);
911 if (p_priv
->resend_cont
) {
912 dev_dbg(&urb
->dev
->dev
, "%s - sending setup\n", __func__
);
913 keyspan_usa90_send_setup(port
->serial
, port
,
914 p_priv
->resend_cont
- 1);
918 /* Status messages from the 28xg */
919 static void usa67_instat_callback(struct urb
*urb
)
922 unsigned char *data
= urb
->transfer_buffer
;
923 struct keyspan_usa67_portStatusMessage
*msg
;
924 struct usb_serial
*serial
;
925 struct usb_serial_port
*port
;
926 struct keyspan_port_private
*p_priv
;
928 int status
= urb
->status
;
930 serial
= urb
->context
;
933 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
937 if (urb
->actual_length
!=
938 sizeof(struct keyspan_usa67_portStatusMessage
)) {
939 dev_dbg(&urb
->dev
->dev
, "%s - bad length %d\n", __func__
, urb
->actual_length
);
944 /* Now do something useful with the data */
945 msg
= (struct keyspan_usa67_portStatusMessage
*)data
;
947 /* Check port number from message and retrieve private data */
948 if (msg
->port
>= serial
->num_ports
) {
949 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n", __func__
, msg
->port
);
953 port
= serial
->port
[msg
->port
];
954 p_priv
= usb_get_serial_port_data(port
);
958 /* Update handshaking pin state information */
959 old_dcd_state
= p_priv
->dcd_state
;
960 p_priv
->cts_state
= ((msg
->hskia_cts
) ? 1 : 0);
961 p_priv
->dcd_state
= ((msg
->gpia_dcd
) ? 1 : 0);
963 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
)
964 tty_port_tty_hangup(&port
->port
, true);
966 /* Resubmit urb so we continue receiving */
967 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
969 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
972 static void usa67_glocont_callback(struct urb
*urb
)
974 struct usb_serial
*serial
;
975 struct usb_serial_port
*port
;
976 struct keyspan_port_private
*p_priv
;
979 serial
= urb
->context
;
980 for (i
= 0; i
< serial
->num_ports
; ++i
) {
981 port
= serial
->port
[i
];
982 p_priv
= usb_get_serial_port_data(port
);
984 if (p_priv
->resend_cont
) {
985 dev_dbg(&port
->dev
, "%s - sending setup\n", __func__
);
986 keyspan_usa67_send_setup(serial
, port
,
987 p_priv
->resend_cont
- 1);
993 static int keyspan_write_room(struct tty_struct
*tty
)
995 struct usb_serial_port
*port
= tty
->driver_data
;
996 struct keyspan_port_private
*p_priv
;
997 const struct keyspan_device_details
*d_details
;
1000 struct urb
*this_urb
;
1002 p_priv
= usb_get_serial_port_data(port
);
1003 d_details
= p_priv
->device_details
;
1005 /* FIXME: locking */
1006 if (d_details
->msg_format
== msg_usa90
)
1011 flip
= p_priv
->out_flip
;
1013 /* Check both endpoints to see if any are available. */
1014 this_urb
= p_priv
->out_urbs
[flip
];
1015 if (this_urb
!= NULL
) {
1016 if (this_urb
->status
!= -EINPROGRESS
)
1018 flip
= (flip
+ 1) & d_details
->outdat_endp_flip
;
1019 this_urb
= p_priv
->out_urbs
[flip
];
1020 if (this_urb
!= NULL
) {
1021 if (this_urb
->status
!= -EINPROGRESS
)
1029 static int keyspan_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
1031 struct keyspan_port_private
*p_priv
;
1032 const struct keyspan_device_details
*d_details
;
1034 int baud_rate
, device_port
;
1036 unsigned int cflag
= 0;
1038 p_priv
= usb_get_serial_port_data(port
);
1039 d_details
= p_priv
->device_details
;
1041 /* Set some sane defaults */
1042 p_priv
->rts_state
= 1;
1043 p_priv
->dtr_state
= 1;
1044 p_priv
->baud
= 9600;
1046 /* force baud and lcr to be set on open */
1047 p_priv
->old_baud
= 0;
1048 p_priv
->old_cflag
= 0;
1050 p_priv
->out_flip
= 0;
1051 p_priv
->in_flip
= 0;
1053 /* Reset low level data toggle and start reading from endpoints */
1054 for (i
= 0; i
< 2; i
++) {
1055 urb
= p_priv
->in_urbs
[i
];
1059 /* make sure endpoint data toggle is synchronized
1061 usb_clear_halt(urb
->dev
, urb
->pipe
);
1062 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1064 dev_dbg(&port
->dev
, "%s - submit urb %d failed (%d)\n", __func__
, i
, err
);
1067 /* Reset low level data toggle on out endpoints */
1068 for (i
= 0; i
< 2; i
++) {
1069 urb
= p_priv
->out_urbs
[i
];
1072 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1073 usb_pipeout(urb->pipe), 0); */
1076 /* get the terminal config for the setup message now so we don't
1077 * need to send 2 of them */
1079 device_port
= port
->port_number
;
1081 cflag
= tty
->termios
.c_cflag
;
1082 /* Baud rate calculation takes baud rate as an integer
1083 so other rates can be generated if desired. */
1084 baud_rate
= tty_get_baud_rate(tty
);
1085 /* If no match or invalid, leave as default */
1087 && d_details
->calculate_baud_rate(port
, baud_rate
, d_details
->baudclk
,
1088 NULL
, NULL
, NULL
, device_port
) == KEYSPAN_BAUD_RATE_OK
) {
1089 p_priv
->baud
= baud_rate
;
1092 /* set CTS/RTS handshake etc. */
1093 p_priv
->cflag
= cflag
;
1094 p_priv
->flow_control
= (cflag
& CRTSCTS
) ? flow_cts
: flow_none
;
1096 keyspan_send_setup(port
, 1);
1098 /* keyspan_set_termios(port, NULL); */
1103 static inline void stop_urb(struct urb
*urb
)
1105 if (urb
&& urb
->status
== -EINPROGRESS
)
1109 static void keyspan_dtr_rts(struct usb_serial_port
*port
, int on
)
1111 struct keyspan_port_private
*p_priv
= usb_get_serial_port_data(port
);
1113 p_priv
->rts_state
= on
;
1114 p_priv
->dtr_state
= on
;
1115 keyspan_send_setup(port
, 0);
1118 static void keyspan_close(struct usb_serial_port
*port
)
1121 struct keyspan_port_private
*p_priv
;
1123 p_priv
= usb_get_serial_port_data(port
);
1125 p_priv
->rts_state
= 0;
1126 p_priv
->dtr_state
= 0;
1128 keyspan_send_setup(port
, 2);
1129 /* pilot-xfer seems to work best with this delay */
1132 p_priv
->out_flip
= 0;
1133 p_priv
->in_flip
= 0;
1135 stop_urb(p_priv
->inack_urb
);
1136 for (i
= 0; i
< 2; i
++) {
1137 stop_urb(p_priv
->in_urbs
[i
]);
1138 stop_urb(p_priv
->out_urbs
[i
]);
1142 /* download the firmware to a pre-renumeration device */
1143 static int keyspan_fake_startup(struct usb_serial
*serial
)
1147 dev_dbg(&serial
->dev
->dev
, "Keyspan startup version %04x product %04x\n",
1148 le16_to_cpu(serial
->dev
->descriptor
.bcdDevice
),
1149 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
1151 if ((le16_to_cpu(serial
->dev
->descriptor
.bcdDevice
) & 0x8000)
1153 dev_dbg(&serial
->dev
->dev
, "Firmware already loaded. Quitting.\n");
1157 /* Select firmware image on the basis of idProduct */
1158 switch (le16_to_cpu(serial
->dev
->descriptor
.idProduct
)) {
1159 case keyspan_usa28_pre_product_id
:
1160 fw_name
= "keyspan/usa28.fw";
1163 case keyspan_usa28x_pre_product_id
:
1164 fw_name
= "keyspan/usa28x.fw";
1167 case keyspan_usa28xa_pre_product_id
:
1168 fw_name
= "keyspan/usa28xa.fw";
1171 case keyspan_usa28xb_pre_product_id
:
1172 fw_name
= "keyspan/usa28xb.fw";
1175 case keyspan_usa19_pre_product_id
:
1176 fw_name
= "keyspan/usa19.fw";
1179 case keyspan_usa19qi_pre_product_id
:
1180 fw_name
= "keyspan/usa19qi.fw";
1183 case keyspan_mpr_pre_product_id
:
1184 fw_name
= "keyspan/mpr.fw";
1187 case keyspan_usa19qw_pre_product_id
:
1188 fw_name
= "keyspan/usa19qw.fw";
1191 case keyspan_usa18x_pre_product_id
:
1192 fw_name
= "keyspan/usa18x.fw";
1195 case keyspan_usa19w_pre_product_id
:
1196 fw_name
= "keyspan/usa19w.fw";
1199 case keyspan_usa49w_pre_product_id
:
1200 fw_name
= "keyspan/usa49w.fw";
1203 case keyspan_usa49wlc_pre_product_id
:
1204 fw_name
= "keyspan/usa49wlc.fw";
1208 dev_err(&serial
->dev
->dev
, "Unknown product ID (%04x)\n",
1209 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
1213 dev_dbg(&serial
->dev
->dev
, "Uploading Keyspan %s firmware.\n", fw_name
);
1215 if (ezusb_fx1_ihex_firmware_download(serial
->dev
, fw_name
) < 0) {
1216 dev_err(&serial
->dev
->dev
, "failed to load firmware \"%s\"\n",
1221 /* after downloading firmware Renumeration will occur in a
1222 moment and the new device will bind to the real driver */
1224 /* we don't want this device to have a driver assigned to it. */
1228 /* Helper functions used by keyspan_setup_urbs */
1229 static struct usb_endpoint_descriptor
const *find_ep(struct usb_serial
const *serial
,
1232 struct usb_host_interface
*iface_desc
;
1233 struct usb_endpoint_descriptor
*ep
;
1236 iface_desc
= serial
->interface
->cur_altsetting
;
1237 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1238 ep
= &iface_desc
->endpoint
[i
].desc
;
1239 if (ep
->bEndpointAddress
== endpoint
)
1242 dev_warn(&serial
->interface
->dev
, "found no endpoint descriptor for "
1243 "endpoint %x\n", endpoint
);
1247 static struct urb
*keyspan_setup_urb(struct usb_serial
*serial
, int endpoint
,
1248 int dir
, void *ctx
, char *buf
, int len
,
1249 void (*callback
)(struct urb
*))
1252 struct usb_endpoint_descriptor
const *ep_desc
;
1253 char const *ep_type_name
;
1256 return NULL
; /* endpoint not needed */
1258 dev_dbg(&serial
->interface
->dev
, "%s - alloc for endpoint %d.\n", __func__
, endpoint
);
1259 urb
= usb_alloc_urb(0, GFP_KERNEL
); /* No ISO */
1261 dev_dbg(&serial
->interface
->dev
, "%s - alloc for endpoint %d failed.\n", __func__
, endpoint
);
1265 if (endpoint
== 0) {
1266 /* control EP filled in when used */
1270 ep_desc
= find_ep(serial
, endpoint
);
1272 /* leak the urb, something's wrong and the callers don't care */
1275 if (usb_endpoint_xfer_int(ep_desc
)) {
1276 ep_type_name
= "INT";
1277 usb_fill_int_urb(urb
, serial
->dev
,
1278 usb_sndintpipe(serial
->dev
, endpoint
) | dir
,
1279 buf
, len
, callback
, ctx
,
1280 ep_desc
->bInterval
);
1281 } else if (usb_endpoint_xfer_bulk(ep_desc
)) {
1282 ep_type_name
= "BULK";
1283 usb_fill_bulk_urb(urb
, serial
->dev
,
1284 usb_sndbulkpipe(serial
->dev
, endpoint
) | dir
,
1285 buf
, len
, callback
, ctx
);
1287 dev_warn(&serial
->interface
->dev
,
1288 "unsupported endpoint type %x\n",
1289 usb_endpoint_type(ep_desc
));
1294 dev_dbg(&serial
->interface
->dev
, "%s - using urb %p for %s endpoint %x\n",
1295 __func__
, urb
, ep_type_name
, endpoint
);
1299 static struct callbacks
{
1300 void (*instat_callback
)(struct urb
*);
1301 void (*glocont_callback
)(struct urb
*);
1302 void (*indat_callback
)(struct urb
*);
1303 void (*outdat_callback
)(struct urb
*);
1304 void (*inack_callback
)(struct urb
*);
1305 void (*outcont_callback
)(struct urb
*);
1306 } keyspan_callbacks
[] = {
1308 /* msg_usa26 callbacks */
1309 .instat_callback
= usa26_instat_callback
,
1310 .glocont_callback
= usa26_glocont_callback
,
1311 .indat_callback
= usa26_indat_callback
,
1312 .outdat_callback
= usa2x_outdat_callback
,
1313 .inack_callback
= usa26_inack_callback
,
1314 .outcont_callback
= usa26_outcont_callback
,
1316 /* msg_usa28 callbacks */
1317 .instat_callback
= usa28_instat_callback
,
1318 .glocont_callback
= usa28_glocont_callback
,
1319 .indat_callback
= usa28_indat_callback
,
1320 .outdat_callback
= usa2x_outdat_callback
,
1321 .inack_callback
= usa28_inack_callback
,
1322 .outcont_callback
= usa28_outcont_callback
,
1324 /* msg_usa49 callbacks */
1325 .instat_callback
= usa49_instat_callback
,
1326 .glocont_callback
= usa49_glocont_callback
,
1327 .indat_callback
= usa49_indat_callback
,
1328 .outdat_callback
= usa2x_outdat_callback
,
1329 .inack_callback
= usa49_inack_callback
,
1330 .outcont_callback
= usa49_outcont_callback
,
1332 /* msg_usa90 callbacks */
1333 .instat_callback
= usa90_instat_callback
,
1334 .glocont_callback
= usa28_glocont_callback
,
1335 .indat_callback
= usa90_indat_callback
,
1336 .outdat_callback
= usa2x_outdat_callback
,
1337 .inack_callback
= usa28_inack_callback
,
1338 .outcont_callback
= usa90_outcont_callback
,
1340 /* msg_usa67 callbacks */
1341 .instat_callback
= usa67_instat_callback
,
1342 .glocont_callback
= usa67_glocont_callback
,
1343 .indat_callback
= usa26_indat_callback
,
1344 .outdat_callback
= usa2x_outdat_callback
,
1345 .inack_callback
= usa26_inack_callback
,
1346 .outcont_callback
= usa26_outcont_callback
,
1350 /* Generic setup urbs function that uses
1351 data in device_details */
1352 static void keyspan_setup_urbs(struct usb_serial
*serial
)
1354 struct keyspan_serial_private
*s_priv
;
1355 const struct keyspan_device_details
*d_details
;
1356 struct callbacks
*cback
;
1358 s_priv
= usb_get_serial_data(serial
);
1359 d_details
= s_priv
->device_details
;
1361 /* Setup values for the various callback routines */
1362 cback
= &keyspan_callbacks
[d_details
->msg_format
];
1364 /* Allocate and set up urbs for each one that is in use,
1365 starting with instat endpoints */
1366 s_priv
->instat_urb
= keyspan_setup_urb
1367 (serial
, d_details
->instat_endpoint
, USB_DIR_IN
,
1368 serial
, s_priv
->instat_buf
, INSTAT_BUFLEN
,
1369 cback
->instat_callback
);
1371 s_priv
->indat_urb
= keyspan_setup_urb
1372 (serial
, d_details
->indat_endpoint
, USB_DIR_IN
,
1373 serial
, s_priv
->indat_buf
, INDAT49W_BUFLEN
,
1374 usa49wg_indat_callback
);
1376 s_priv
->glocont_urb
= keyspan_setup_urb
1377 (serial
, d_details
->glocont_endpoint
, USB_DIR_OUT
,
1378 serial
, s_priv
->glocont_buf
, GLOCONT_BUFLEN
,
1379 cback
->glocont_callback
);
1382 /* usa19 function doesn't require prescaler */
1383 static int keyspan_usa19_calc_baud(struct usb_serial_port
*port
,
1384 u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1385 u8
*rate_low
, u8
*prescaler
, int portnum
)
1387 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1389 cnt
; /* inverse of divisor (programmed into 8051) */
1391 dev_dbg(&port
->dev
, "%s - %d.\n", __func__
, baud_rate
);
1393 /* prevent divide by zero... */
1394 b16
= baud_rate
* 16L;
1396 return KEYSPAN_INVALID_BAUD_RATE
;
1397 /* Any "standard" rate over 57k6 is marginal on the USA-19
1398 as we run out of divisor resolution. */
1399 if (baud_rate
> 57600)
1400 return KEYSPAN_INVALID_BAUD_RATE
;
1402 /* calculate the divisor and the counter (its inverse) */
1403 div
= baudclk
/ b16
;
1405 return KEYSPAN_INVALID_BAUD_RATE
;
1410 return KEYSPAN_INVALID_BAUD_RATE
;
1412 /* return the counter values if non-null */
1414 *rate_low
= (u8
) (cnt
& 0xff);
1416 *rate_hi
= (u8
) ((cnt
>> 8) & 0xff);
1417 if (rate_low
&& rate_hi
)
1418 dev_dbg(&port
->dev
, "%s - %d %02x %02x.\n",
1419 __func__
, baud_rate
, *rate_hi
, *rate_low
);
1420 return KEYSPAN_BAUD_RATE_OK
;
1423 /* usa19hs function doesn't require prescaler */
1424 static int keyspan_usa19hs_calc_baud(struct usb_serial_port
*port
,
1425 u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1426 u8
*rate_low
, u8
*prescaler
, int portnum
)
1428 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1431 dev_dbg(&port
->dev
, "%s - %d.\n", __func__
, baud_rate
);
1433 /* prevent divide by zero... */
1434 b16
= baud_rate
* 16L;
1436 return KEYSPAN_INVALID_BAUD_RATE
;
1438 /* calculate the divisor */
1439 div
= baudclk
/ b16
;
1441 return KEYSPAN_INVALID_BAUD_RATE
;
1444 return KEYSPAN_INVALID_BAUD_RATE
;
1446 /* return the counter values if non-null */
1448 *rate_low
= (u8
) (div
& 0xff);
1451 *rate_hi
= (u8
) ((div
>> 8) & 0xff);
1453 if (rate_low
&& rate_hi
)
1454 dev_dbg(&port
->dev
, "%s - %d %02x %02x.\n",
1455 __func__
, baud_rate
, *rate_hi
, *rate_low
);
1457 return KEYSPAN_BAUD_RATE_OK
;
1460 static int keyspan_usa19w_calc_baud(struct usb_serial_port
*port
,
1461 u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1462 u8
*rate_low
, u8
*prescaler
, int portnum
)
1464 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1465 clk
, /* clock with 13/8 prescaler */
1466 div
, /* divisor using 13/8 prescaler */
1467 res
, /* resulting baud rate using 13/8 prescaler */
1468 diff
, /* error using 13/8 prescaler */
1473 dev_dbg(&port
->dev
, "%s - %d.\n", __func__
, baud_rate
);
1475 /* prevent divide by zero */
1476 b16
= baud_rate
* 16L;
1478 return KEYSPAN_INVALID_BAUD_RATE
;
1480 /* Calculate prescaler by trying them all and looking
1483 /* start with largest possible difference */
1484 smallest_diff
= 0xffffffff;
1486 /* 0 is an invalid prescaler, used as a flag */
1489 for (i
= 8; i
<= 0xff; ++i
) {
1490 clk
= (baudclk
* 8) / (u32
) i
;
1497 diff
= (res
> b16
) ? (res
-b16
) : (b16
-res
);
1499 if (diff
< smallest_diff
) {
1501 smallest_diff
= diff
;
1505 if (best_prescaler
== 0)
1506 return KEYSPAN_INVALID_BAUD_RATE
;
1508 clk
= (baudclk
* 8) / (u32
) best_prescaler
;
1511 /* return the divisor and prescaler if non-null */
1513 *rate_low
= (u8
) (div
& 0xff);
1515 *rate_hi
= (u8
) ((div
>> 8) & 0xff);
1517 *prescaler
= best_prescaler
;
1518 /* dev_dbg(&port->dev, "%s - %d %d\n", __func__, *prescaler, div); */
1520 return KEYSPAN_BAUD_RATE_OK
;
1523 /* USA-28 supports different maximum baud rates on each port */
1524 static int keyspan_usa28_calc_baud(struct usb_serial_port
*port
,
1525 u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1526 u8
*rate_low
, u8
*prescaler
, int portnum
)
1528 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1530 cnt
; /* inverse of divisor (programmed into 8051) */
1532 dev_dbg(&port
->dev
, "%s - %d.\n", __func__
, baud_rate
);
1534 /* prevent divide by zero */
1535 b16
= baud_rate
* 16L;
1537 return KEYSPAN_INVALID_BAUD_RATE
;
1539 /* calculate the divisor and the counter (its inverse) */
1540 div
= KEYSPAN_USA28_BAUDCLK
/ b16
;
1542 return KEYSPAN_INVALID_BAUD_RATE
;
1546 /* check for out of range, based on portnum,
1547 and return result */
1550 return KEYSPAN_INVALID_BAUD_RATE
;
1554 return KEYSPAN_INVALID_BAUD_RATE
;
1556 return KEYSPAN_INVALID_BAUD_RATE
;
1559 /* return the counter values if not NULL
1560 (port 1 will ignore retHi) */
1562 *rate_low
= (u8
) (cnt
& 0xff);
1564 *rate_hi
= (u8
) ((cnt
>> 8) & 0xff);
1565 dev_dbg(&port
->dev
, "%s - %d OK.\n", __func__
, baud_rate
);
1566 return KEYSPAN_BAUD_RATE_OK
;
1569 static int keyspan_usa26_send_setup(struct usb_serial
*serial
,
1570 struct usb_serial_port
*port
,
1573 struct keyspan_usa26_portControlMessage msg
;
1574 struct keyspan_serial_private
*s_priv
;
1575 struct keyspan_port_private
*p_priv
;
1576 const struct keyspan_device_details
*d_details
;
1577 struct urb
*this_urb
;
1578 int device_port
, err
;
1580 dev_dbg(&port
->dev
, "%s reset=%d\n", __func__
, reset_port
);
1582 s_priv
= usb_get_serial_data(serial
);
1583 p_priv
= usb_get_serial_port_data(port
);
1584 d_details
= s_priv
->device_details
;
1585 device_port
= port
->port_number
;
1587 this_urb
= p_priv
->outcont_urb
;
1589 dev_dbg(&port
->dev
, "%s - endpoint %d\n", __func__
, usb_pipeendpoint(this_urb
->pipe
));
1591 /* Make sure we have an urb then send the message */
1592 if (this_urb
== NULL
) {
1593 dev_dbg(&port
->dev
, "%s - oops no urb.\n", __func__
);
1597 /* Save reset port val for resend.
1598 Don't overwrite resend for open/close condition. */
1599 if ((reset_port
+ 1) > p_priv
->resend_cont
)
1600 p_priv
->resend_cont
= reset_port
+ 1;
1601 if (this_urb
->status
== -EINPROGRESS
) {
1602 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1607 memset(&msg
, 0, sizeof(struct keyspan_usa26_portControlMessage
));
1609 /* Only set baud rate if it's changed */
1610 if (p_priv
->old_baud
!= p_priv
->baud
) {
1611 p_priv
->old_baud
= p_priv
->baud
;
1612 msg
.setClocking
= 0xff;
1613 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
1614 &msg
.baudHi
, &msg
.baudLo
, &msg
.prescaler
,
1615 device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
1616 dev_dbg(&port
->dev
, "%s - Invalid baud rate %d requested, using 9600.\n",
1617 __func__
, p_priv
->baud
);
1619 msg
.baudHi
= 125; /* Values for 9600 baud */
1622 msg
.setPrescaler
= 0xff;
1625 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
1626 switch (p_priv
->cflag
& CSIZE
) {
1628 msg
.lcr
|= USA_DATABITS_5
;
1631 msg
.lcr
|= USA_DATABITS_6
;
1634 msg
.lcr
|= USA_DATABITS_7
;
1637 msg
.lcr
|= USA_DATABITS_8
;
1640 if (p_priv
->cflag
& PARENB
) {
1641 /* note USA_PARITY_NONE == 0 */
1642 msg
.lcr
|= (p_priv
->cflag
& PARODD
) ?
1643 USA_PARITY_ODD
: USA_PARITY_EVEN
;
1647 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
1648 msg
.xonFlowControl
= 0;
1649 msg
.setFlowControl
= 0xff;
1650 msg
.forwardingLength
= 16;
1655 if (reset_port
== 1) {
1664 msg
.returnStatus
= 0;
1665 msg
.resetDataToggle
= 0xff;
1669 else if (reset_port
== 2) {
1678 msg
.returnStatus
= 0;
1679 msg
.resetDataToggle
= 0;
1682 /* Sending intermediate configs */
1684 msg
._txOn
= (!p_priv
->break_on
);
1687 msg
.txBreak
= (p_priv
->break_on
);
1692 msg
.returnStatus
= 0;
1693 msg
.resetDataToggle
= 0x0;
1696 /* Do handshaking outputs */
1697 msg
.setTxTriState_setRts
= 0xff;
1698 msg
.txTriState_rts
= p_priv
->rts_state
;
1700 msg
.setHskoa_setDtr
= 0xff;
1701 msg
.hskoa_dtr
= p_priv
->dtr_state
;
1703 p_priv
->resend_cont
= 0;
1704 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
1706 /* send the data out the device on control endpoint */
1707 this_urb
->transfer_buffer_length
= sizeof(msg
);
1709 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
1711 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed (%d)\n", __func__
, err
);
1715 static int keyspan_usa28_send_setup(struct usb_serial
*serial
,
1716 struct usb_serial_port
*port
,
1719 struct keyspan_usa28_portControlMessage msg
;
1720 struct keyspan_serial_private
*s_priv
;
1721 struct keyspan_port_private
*p_priv
;
1722 const struct keyspan_device_details
*d_details
;
1723 struct urb
*this_urb
;
1724 int device_port
, err
;
1726 s_priv
= usb_get_serial_data(serial
);
1727 p_priv
= usb_get_serial_port_data(port
);
1728 d_details
= s_priv
->device_details
;
1729 device_port
= port
->port_number
;
1731 /* only do something if we have a bulk out endpoint */
1732 this_urb
= p_priv
->outcont_urb
;
1733 if (this_urb
== NULL
) {
1734 dev_dbg(&port
->dev
, "%s - oops no urb.\n", __func__
);
1738 /* Save reset port val for resend.
1739 Don't overwrite resend for open/close condition. */
1740 if ((reset_port
+ 1) > p_priv
->resend_cont
)
1741 p_priv
->resend_cont
= reset_port
+ 1;
1742 if (this_urb
->status
== -EINPROGRESS
) {
1743 dev_dbg(&port
->dev
, "%s already writing\n", __func__
);
1748 memset(&msg
, 0, sizeof(struct keyspan_usa28_portControlMessage
));
1750 msg
.setBaudRate
= 1;
1751 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
1752 &msg
.baudHi
, &msg
.baudLo
, NULL
,
1753 device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
1754 dev_dbg(&port
->dev
, "%s - Invalid baud rate requested %d.\n",
1755 __func__
, p_priv
->baud
);
1757 msg
.baudHi
= 0xb2; /* Values for 9600 baud */
1760 /* If parity is enabled, we must calculate it ourselves. */
1761 msg
.parity
= 0; /* XXX for now */
1763 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
1764 msg
.xonFlowControl
= 0;
1766 /* Do handshaking outputs, DTR is inverted relative to RTS */
1767 msg
.rts
= p_priv
->rts_state
;
1768 msg
.dtr
= p_priv
->dtr_state
;
1770 msg
.forwardingLength
= 16;
1772 msg
.breakThreshold
= 45;
1776 /*msg.returnStatus = 1;
1777 msg.resetDataToggle = 0xff;*/
1779 if (reset_port
== 1) {
1783 msg
.txForceXoff
= 0;
1789 msg
.returnStatus
= 0;
1790 msg
.resetDataToggle
= 0xff;
1793 else if (reset_port
== 2) {
1797 msg
.txForceXoff
= 0;
1803 msg
.returnStatus
= 0;
1804 msg
.resetDataToggle
= 0;
1806 /* Sending intermediate configs */
1808 msg
._txOn
= (!p_priv
->break_on
);
1811 msg
.txForceXoff
= 0;
1812 msg
.txBreak
= (p_priv
->break_on
);
1817 msg
.returnStatus
= 0;
1818 msg
.resetDataToggle
= 0x0;
1821 p_priv
->resend_cont
= 0;
1822 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
1824 /* send the data out the device on control endpoint */
1825 this_urb
->transfer_buffer_length
= sizeof(msg
);
1827 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
1829 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed\n", __func__
);
1832 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) OK %d bytes\n", __func__
,
1833 this_urb
->transfer_buffer_length
);
1840 static int keyspan_usa49_send_setup(struct usb_serial
*serial
,
1841 struct usb_serial_port
*port
,
1844 struct keyspan_usa49_portControlMessage msg
;
1845 struct usb_ctrlrequest
*dr
= NULL
;
1846 struct keyspan_serial_private
*s_priv
;
1847 struct keyspan_port_private
*p_priv
;
1848 const struct keyspan_device_details
*d_details
;
1849 struct urb
*this_urb
;
1850 int err
, device_port
;
1852 s_priv
= usb_get_serial_data(serial
);
1853 p_priv
= usb_get_serial_port_data(port
);
1854 d_details
= s_priv
->device_details
;
1856 this_urb
= s_priv
->glocont_urb
;
1858 /* Work out which port within the device is being setup */
1859 device_port
= port
->port_number
;
1861 /* Make sure we have an urb then send the message */
1862 if (this_urb
== NULL
) {
1863 dev_dbg(&port
->dev
, "%s - oops no urb for port.\n", __func__
);
1867 dev_dbg(&port
->dev
, "%s - endpoint %d (%d)\n",
1868 __func__
, usb_pipeendpoint(this_urb
->pipe
), device_port
);
1870 /* Save reset port val for resend.
1871 Don't overwrite resend for open/close condition. */
1872 if ((reset_port
+ 1) > p_priv
->resend_cont
)
1873 p_priv
->resend_cont
= reset_port
+ 1;
1875 if (this_urb
->status
== -EINPROGRESS
) {
1876 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1881 memset(&msg
, 0, sizeof(struct keyspan_usa49_portControlMessage
));
1883 msg
.portNumber
= device_port
;
1885 /* Only set baud rate if it's changed */
1886 if (p_priv
->old_baud
!= p_priv
->baud
) {
1887 p_priv
->old_baud
= p_priv
->baud
;
1888 msg
.setClocking
= 0xff;
1889 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
1890 &msg
.baudHi
, &msg
.baudLo
, &msg
.prescaler
,
1891 device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
1892 dev_dbg(&port
->dev
, "%s - Invalid baud rate %d requested, using 9600.\n",
1893 __func__
, p_priv
->baud
);
1895 msg
.baudHi
= 125; /* Values for 9600 baud */
1898 /* msg.setPrescaler = 0xff; */
1901 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
1902 switch (p_priv
->cflag
& CSIZE
) {
1904 msg
.lcr
|= USA_DATABITS_5
;
1907 msg
.lcr
|= USA_DATABITS_6
;
1910 msg
.lcr
|= USA_DATABITS_7
;
1913 msg
.lcr
|= USA_DATABITS_8
;
1916 if (p_priv
->cflag
& PARENB
) {
1917 /* note USA_PARITY_NONE == 0 */
1918 msg
.lcr
|= (p_priv
->cflag
& PARODD
) ?
1919 USA_PARITY_ODD
: USA_PARITY_EVEN
;
1923 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
1924 msg
.xonFlowControl
= 0;
1925 msg
.setFlowControl
= 0xff;
1927 msg
.forwardingLength
= 16;
1932 if (reset_port
== 1) {
1941 msg
.returnStatus
= 0;
1942 msg
.resetDataToggle
= 0xff;
1944 msg
.disablePort
= 0;
1947 else if (reset_port
== 2) {
1956 msg
.returnStatus
= 0;
1957 msg
.resetDataToggle
= 0;
1959 msg
.disablePort
= 1;
1961 /* Sending intermediate configs */
1963 msg
._txOn
= (!p_priv
->break_on
);
1966 msg
.txBreak
= (p_priv
->break_on
);
1971 msg
.returnStatus
= 0;
1972 msg
.resetDataToggle
= 0x0;
1974 msg
.disablePort
= 0;
1977 /* Do handshaking outputs */
1979 msg
.rts
= p_priv
->rts_state
;
1982 msg
.dtr
= p_priv
->dtr_state
;
1984 p_priv
->resend_cont
= 0;
1986 /* if the device is a 49wg, we send control message on usb
1989 if (d_details
->product_id
== keyspan_usa49wg_product_id
) {
1990 dr
= (void *)(s_priv
->ctrl_buf
);
1991 dr
->bRequestType
= USB_TYPE_VENDOR
| USB_DIR_OUT
;
1992 dr
->bRequest
= 0xB0; /* 49wg control message */;
1995 dr
->wLength
= cpu_to_le16(sizeof(msg
));
1997 memcpy(s_priv
->glocont_buf
, &msg
, sizeof(msg
));
1999 usb_fill_control_urb(this_urb
, serial
->dev
,
2000 usb_sndctrlpipe(serial
->dev
, 0),
2001 (unsigned char *)dr
, s_priv
->glocont_buf
,
2002 sizeof(msg
), usa49_glocont_callback
, serial
);
2005 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2007 /* send the data out the device on control endpoint */
2008 this_urb
->transfer_buffer_length
= sizeof(msg
);
2010 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2012 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed (%d)\n", __func__
, err
);
2015 dev_dbg(&port
->dev
, "%s - usb_submit_urb(%d) OK %d bytes (end %d)\n", __func__
,
2016 outcont_urb
, this_urb
->transfer_buffer_length
,
2017 usb_pipeendpoint(this_urb
->pipe
));
2024 static int keyspan_usa90_send_setup(struct usb_serial
*serial
,
2025 struct usb_serial_port
*port
,
2028 struct keyspan_usa90_portControlMessage msg
;
2029 struct keyspan_serial_private
*s_priv
;
2030 struct keyspan_port_private
*p_priv
;
2031 const struct keyspan_device_details
*d_details
;
2032 struct urb
*this_urb
;
2036 s_priv
= usb_get_serial_data(serial
);
2037 p_priv
= usb_get_serial_port_data(port
);
2038 d_details
= s_priv
->device_details
;
2040 /* only do something if we have a bulk out endpoint */
2041 this_urb
= p_priv
->outcont_urb
;
2042 if (this_urb
== NULL
) {
2043 dev_dbg(&port
->dev
, "%s - oops no urb.\n", __func__
);
2047 /* Save reset port val for resend.
2048 Don't overwrite resend for open/close condition. */
2049 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2050 p_priv
->resend_cont
= reset_port
+ 1;
2051 if (this_urb
->status
== -EINPROGRESS
) {
2052 dev_dbg(&port
->dev
, "%s already writing\n", __func__
);
2057 memset(&msg
, 0, sizeof(struct keyspan_usa90_portControlMessage
));
2059 /* Only set baud rate if it's changed */
2060 if (p_priv
->old_baud
!= p_priv
->baud
) {
2061 p_priv
->old_baud
= p_priv
->baud
;
2062 msg
.setClocking
= 0x01;
2063 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
2064 &msg
.baudHi
, &msg
.baudLo
, &prescaler
, 0) == KEYSPAN_INVALID_BAUD_RATE
) {
2065 dev_dbg(&port
->dev
, "%s - Invalid baud rate %d requested, using 9600.\n",
2066 __func__
, p_priv
->baud
);
2067 p_priv
->baud
= 9600;
2068 d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
2069 &msg
.baudHi
, &msg
.baudLo
, &prescaler
, 0);
2075 /* modes must always be correctly specified */
2076 if (p_priv
->baud
> 57600) {
2077 msg
.rxMode
= RXMODE_DMA
;
2078 msg
.txMode
= TXMODE_DMA
;
2080 msg
.rxMode
= RXMODE_BYHAND
;
2081 msg
.txMode
= TXMODE_BYHAND
;
2084 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
2085 switch (p_priv
->cflag
& CSIZE
) {
2087 msg
.lcr
|= USA_DATABITS_5
;
2090 msg
.lcr
|= USA_DATABITS_6
;
2093 msg
.lcr
|= USA_DATABITS_7
;
2096 msg
.lcr
|= USA_DATABITS_8
;
2099 if (p_priv
->cflag
& PARENB
) {
2100 /* note USA_PARITY_NONE == 0 */
2101 msg
.lcr
|= (p_priv
->cflag
& PARODD
) ?
2102 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2104 if (p_priv
->old_cflag
!= p_priv
->cflag
) {
2105 p_priv
->old_cflag
= p_priv
->cflag
;
2109 if (p_priv
->flow_control
== flow_cts
)
2110 msg
.txFlowControl
= TXFLOW_CTS
;
2111 msg
.setTxFlowControl
= 0x01;
2112 msg
.setRxFlowControl
= 0x01;
2114 msg
.rxForwardingLength
= 16;
2115 msg
.rxForwardingTimeout
= 16;
2116 msg
.txAckSetting
= 0;
2121 if (reset_port
== 1) {
2122 msg
.portEnabled
= 1;
2124 msg
.txBreak
= (p_priv
->break_on
);
2127 else if (reset_port
== 2)
2128 msg
.portEnabled
= 0;
2129 /* Sending intermediate configs */
2131 msg
.portEnabled
= 1;
2132 msg
.txBreak
= (p_priv
->break_on
);
2135 /* Do handshaking outputs */
2137 msg
.rts
= p_priv
->rts_state
;
2140 msg
.dtr
= p_priv
->dtr_state
;
2142 p_priv
->resend_cont
= 0;
2143 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2145 /* send the data out the device on control endpoint */
2146 this_urb
->transfer_buffer_length
= sizeof(msg
);
2148 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2150 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed (%d)\n", __func__
, err
);
2154 static int keyspan_usa67_send_setup(struct usb_serial
*serial
,
2155 struct usb_serial_port
*port
,
2158 struct keyspan_usa67_portControlMessage msg
;
2159 struct keyspan_serial_private
*s_priv
;
2160 struct keyspan_port_private
*p_priv
;
2161 const struct keyspan_device_details
*d_details
;
2162 struct urb
*this_urb
;
2163 int err
, device_port
;
2165 s_priv
= usb_get_serial_data(serial
);
2166 p_priv
= usb_get_serial_port_data(port
);
2167 d_details
= s_priv
->device_details
;
2169 this_urb
= s_priv
->glocont_urb
;
2171 /* Work out which port within the device is being setup */
2172 device_port
= port
->port_number
;
2174 /* Make sure we have an urb then send the message */
2175 if (this_urb
== NULL
) {
2176 dev_dbg(&port
->dev
, "%s - oops no urb for port.\n", __func__
);
2180 /* Save reset port val for resend.
2181 Don't overwrite resend for open/close condition. */
2182 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2183 p_priv
->resend_cont
= reset_port
+ 1;
2184 if (this_urb
->status
== -EINPROGRESS
) {
2185 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
2190 memset(&msg
, 0, sizeof(struct keyspan_usa67_portControlMessage
));
2192 msg
.port
= device_port
;
2194 /* Only set baud rate if it's changed */
2195 if (p_priv
->old_baud
!= p_priv
->baud
) {
2196 p_priv
->old_baud
= p_priv
->baud
;
2197 msg
.setClocking
= 0xff;
2198 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
2199 &msg
.baudHi
, &msg
.baudLo
, &msg
.prescaler
,
2200 device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
2201 dev_dbg(&port
->dev
, "%s - Invalid baud rate %d requested, using 9600.\n",
2202 __func__
, p_priv
->baud
);
2204 msg
.baudHi
= 125; /* Values for 9600 baud */
2207 msg
.setPrescaler
= 0xff;
2210 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
2211 switch (p_priv
->cflag
& CSIZE
) {
2213 msg
.lcr
|= USA_DATABITS_5
;
2216 msg
.lcr
|= USA_DATABITS_6
;
2219 msg
.lcr
|= USA_DATABITS_7
;
2222 msg
.lcr
|= USA_DATABITS_8
;
2225 if (p_priv
->cflag
& PARENB
) {
2226 /* note USA_PARITY_NONE == 0 */
2227 msg
.lcr
|= (p_priv
->cflag
& PARODD
) ?
2228 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2232 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
2233 msg
.xonFlowControl
= 0;
2234 msg
.setFlowControl
= 0xff;
2235 msg
.forwardingLength
= 16;
2239 if (reset_port
== 1) {
2249 msg
.returnStatus
= 0;
2250 msg
.resetDataToggle
= 0xff;
2251 } else if (reset_port
== 2) {
2261 msg
.returnStatus
= 0;
2262 msg
.resetDataToggle
= 0;
2264 /* Sending intermediate configs */
2265 msg
._txOn
= (!p_priv
->break_on
);
2268 msg
.txBreak
= (p_priv
->break_on
);
2273 msg
.returnStatus
= 0;
2274 msg
.resetDataToggle
= 0x0;
2277 /* Do handshaking outputs */
2278 msg
.setTxTriState_setRts
= 0xff;
2279 msg
.txTriState_rts
= p_priv
->rts_state
;
2281 msg
.setHskoa_setDtr
= 0xff;
2282 msg
.hskoa_dtr
= p_priv
->dtr_state
;
2284 p_priv
->resend_cont
= 0;
2286 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2288 /* send the data out the device on control endpoint */
2289 this_urb
->transfer_buffer_length
= sizeof(msg
);
2291 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2293 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed (%d)\n", __func__
, err
);
2297 static void keyspan_send_setup(struct usb_serial_port
*port
, int reset_port
)
2299 struct usb_serial
*serial
= port
->serial
;
2300 struct keyspan_serial_private
*s_priv
;
2301 const struct keyspan_device_details
*d_details
;
2303 s_priv
= usb_get_serial_data(serial
);
2304 d_details
= s_priv
->device_details
;
2306 switch (d_details
->msg_format
) {
2308 keyspan_usa26_send_setup(serial
, port
, reset_port
);
2311 keyspan_usa28_send_setup(serial
, port
, reset_port
);
2314 keyspan_usa49_send_setup(serial
, port
, reset_port
);
2317 keyspan_usa90_send_setup(serial
, port
, reset_port
);
2320 keyspan_usa67_send_setup(serial
, port
, reset_port
);
2326 /* Gets called by the "real" driver (ie once firmware is loaded
2327 and renumeration has taken place. */
2328 static int keyspan_startup(struct usb_serial
*serial
)
2331 struct keyspan_serial_private
*s_priv
;
2332 const struct keyspan_device_details
*d_details
;
2334 for (i
= 0; (d_details
= keyspan_devices
[i
]) != NULL
; ++i
)
2335 if (d_details
->product_id
==
2336 le16_to_cpu(serial
->dev
->descriptor
.idProduct
))
2338 if (d_details
== NULL
) {
2339 dev_err(&serial
->dev
->dev
, "%s - unknown product id %x\n",
2340 __func__
, le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
2344 /* Setup private data for serial driver */
2345 s_priv
= kzalloc(sizeof(struct keyspan_serial_private
), GFP_KERNEL
);
2347 dev_dbg(&serial
->dev
->dev
, "%s - kmalloc for keyspan_serial_private failed.\n", __func__
);
2351 s_priv
->instat_buf
= kzalloc(INSTAT_BUFLEN
, GFP_KERNEL
);
2352 if (!s_priv
->instat_buf
)
2353 goto err_instat_buf
;
2355 s_priv
->indat_buf
= kzalloc(INDAT49W_BUFLEN
, GFP_KERNEL
);
2356 if (!s_priv
->indat_buf
)
2359 s_priv
->glocont_buf
= kzalloc(GLOCONT_BUFLEN
, GFP_KERNEL
);
2360 if (!s_priv
->glocont_buf
)
2361 goto err_glocont_buf
;
2363 s_priv
->ctrl_buf
= kzalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
2364 if (!s_priv
->ctrl_buf
)
2367 s_priv
->device_details
= d_details
;
2368 usb_set_serial_data(serial
, s_priv
);
2370 keyspan_setup_urbs(serial
);
2372 if (s_priv
->instat_urb
!= NULL
) {
2373 err
= usb_submit_urb(s_priv
->instat_urb
, GFP_KERNEL
);
2375 dev_dbg(&serial
->dev
->dev
, "%s - submit instat urb failed %d\n", __func__
, err
);
2377 if (s_priv
->indat_urb
!= NULL
) {
2378 err
= usb_submit_urb(s_priv
->indat_urb
, GFP_KERNEL
);
2380 dev_dbg(&serial
->dev
->dev
, "%s - submit indat urb failed %d\n", __func__
, err
);
2386 kfree(s_priv
->glocont_buf
);
2388 kfree(s_priv
->indat_buf
);
2390 kfree(s_priv
->instat_buf
);
2397 static void keyspan_disconnect(struct usb_serial
*serial
)
2399 struct keyspan_serial_private
*s_priv
;
2401 s_priv
= usb_get_serial_data(serial
);
2403 stop_urb(s_priv
->instat_urb
);
2404 stop_urb(s_priv
->glocont_urb
);
2405 stop_urb(s_priv
->indat_urb
);
2408 static void keyspan_release(struct usb_serial
*serial
)
2410 struct keyspan_serial_private
*s_priv
;
2412 s_priv
= usb_get_serial_data(serial
);
2414 usb_free_urb(s_priv
->instat_urb
);
2415 usb_free_urb(s_priv
->indat_urb
);
2416 usb_free_urb(s_priv
->glocont_urb
);
2418 kfree(s_priv
->ctrl_buf
);
2419 kfree(s_priv
->glocont_buf
);
2420 kfree(s_priv
->indat_buf
);
2421 kfree(s_priv
->instat_buf
);
2426 static int keyspan_port_probe(struct usb_serial_port
*port
)
2428 struct usb_serial
*serial
= port
->serial
;
2429 struct keyspan_serial_private
*s_priv
;
2430 struct keyspan_port_private
*p_priv
;
2431 const struct keyspan_device_details
*d_details
;
2432 struct callbacks
*cback
;
2437 s_priv
= usb_get_serial_data(serial
);
2438 d_details
= s_priv
->device_details
;
2440 p_priv
= kzalloc(sizeof(*p_priv
), GFP_KERNEL
);
2444 for (i
= 0; i
< ARRAY_SIZE(p_priv
->in_buffer
); ++i
) {
2445 p_priv
->in_buffer
[i
] = kzalloc(IN_BUFLEN
, GFP_KERNEL
);
2446 if (!p_priv
->in_buffer
[i
])
2450 for (i
= 0; i
< ARRAY_SIZE(p_priv
->out_buffer
); ++i
) {
2451 p_priv
->out_buffer
[i
] = kzalloc(OUT_BUFLEN
, GFP_KERNEL
);
2452 if (!p_priv
->out_buffer
[i
])
2453 goto err_out_buffer
;
2456 p_priv
->inack_buffer
= kzalloc(INACK_BUFLEN
, GFP_KERNEL
);
2457 if (!p_priv
->inack_buffer
)
2458 goto err_inack_buffer
;
2460 p_priv
->outcont_buffer
= kzalloc(OUTCONT_BUFLEN
, GFP_KERNEL
);
2461 if (!p_priv
->outcont_buffer
)
2462 goto err_outcont_buffer
;
2464 p_priv
->device_details
= d_details
;
2466 /* Setup values for the various callback routines */
2467 cback
= &keyspan_callbacks
[d_details
->msg_format
];
2469 port_num
= port
->port_number
;
2471 /* Do indat endpoints first, once for each flip */
2472 endp
= d_details
->indat_endpoints
[port_num
];
2473 for (i
= 0; i
<= d_details
->indat_endp_flip
; ++i
, ++endp
) {
2474 p_priv
->in_urbs
[i
] = keyspan_setup_urb(serial
, endp
,
2476 p_priv
->in_buffer
[i
],
2478 cback
->indat_callback
);
2480 /* outdat endpoints also have flip */
2481 endp
= d_details
->outdat_endpoints
[port_num
];
2482 for (i
= 0; i
<= d_details
->outdat_endp_flip
; ++i
, ++endp
) {
2483 p_priv
->out_urbs
[i
] = keyspan_setup_urb(serial
, endp
,
2485 p_priv
->out_buffer
[i
],
2487 cback
->outdat_callback
);
2489 /* inack endpoint */
2490 p_priv
->inack_urb
= keyspan_setup_urb(serial
,
2491 d_details
->inack_endpoints
[port_num
],
2493 p_priv
->inack_buffer
,
2495 cback
->inack_callback
);
2496 /* outcont endpoint */
2497 p_priv
->outcont_urb
= keyspan_setup_urb(serial
,
2498 d_details
->outcont_endpoints
[port_num
],
2500 p_priv
->outcont_buffer
,
2502 cback
->outcont_callback
);
2504 usb_set_serial_port_data(port
, p_priv
);
2509 kfree(p_priv
->inack_buffer
);
2511 for (i
= 0; i
< ARRAY_SIZE(p_priv
->out_buffer
); ++i
)
2512 kfree(p_priv
->out_buffer
[i
]);
2514 for (i
= 0; i
< ARRAY_SIZE(p_priv
->in_buffer
); ++i
)
2515 kfree(p_priv
->in_buffer
[i
]);
2522 static int keyspan_port_remove(struct usb_serial_port
*port
)
2524 struct keyspan_port_private
*p_priv
;
2527 p_priv
= usb_get_serial_port_data(port
);
2529 stop_urb(p_priv
->inack_urb
);
2530 stop_urb(p_priv
->outcont_urb
);
2531 for (i
= 0; i
< 2; i
++) {
2532 stop_urb(p_priv
->in_urbs
[i
]);
2533 stop_urb(p_priv
->out_urbs
[i
]);
2536 usb_free_urb(p_priv
->inack_urb
);
2537 usb_free_urb(p_priv
->outcont_urb
);
2538 for (i
= 0; i
< 2; i
++) {
2539 usb_free_urb(p_priv
->in_urbs
[i
]);
2540 usb_free_urb(p_priv
->out_urbs
[i
]);
2543 kfree(p_priv
->outcont_buffer
);
2544 kfree(p_priv
->inack_buffer
);
2545 for (i
= 0; i
< ARRAY_SIZE(p_priv
->out_buffer
); ++i
)
2546 kfree(p_priv
->out_buffer
[i
]);
2547 for (i
= 0; i
< ARRAY_SIZE(p_priv
->in_buffer
); ++i
)
2548 kfree(p_priv
->in_buffer
[i
]);
2555 MODULE_AUTHOR(DRIVER_AUTHOR
);
2556 MODULE_DESCRIPTION(DRIVER_DESC
);
2557 MODULE_LICENSE("GPL");
2559 MODULE_FIRMWARE("keyspan/usa28.fw");
2560 MODULE_FIRMWARE("keyspan/usa28x.fw");
2561 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2562 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2563 MODULE_FIRMWARE("keyspan/usa19.fw");
2564 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2565 MODULE_FIRMWARE("keyspan/mpr.fw");
2566 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2567 MODULE_FIRMWARE("keyspan/usa18x.fw");
2568 MODULE_FIRMWARE("keyspan/usa19w.fw");
2569 MODULE_FIRMWARE("keyspan/usa49w.fw");
2570 MODULE_FIRMWARE("keyspan/usa49wlc.fw");