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
54 /* Per device and per port private data */
55 struct keyspan_serial_private
{
56 const struct keyspan_device_details
*device_details
;
58 struct urb
*instat_urb
;
59 char instat_buf
[INSTAT_BUFLEN
];
61 /* added to support 49wg, where data from all 4 ports comes in
62 on 1 EP and high-speed supported */
63 struct urb
*indat_urb
;
64 char indat_buf
[INDAT49W_BUFLEN
];
66 /* XXX this one probably will need a lock */
67 struct urb
*glocont_urb
;
68 char glocont_buf
[GLOCONT_BUFLEN
];
69 char ctrl_buf
[8]; /* for EP0 control message */
72 struct keyspan_port_private
{
73 /* Keep track of which input & output endpoints to use */
77 /* Keep duplicate of device details in each port
78 structure as well - simplifies some of the
79 callback functions etc. */
80 const struct keyspan_device_details
*device_details
;
82 /* Input endpoints and buffer for this port */
83 struct urb
*in_urbs
[2];
84 char in_buffer
[2][64];
85 /* Output endpoints and buffer for this port */
86 struct urb
*out_urbs
[2];
87 char out_buffer
[2][64];
89 /* Input ack endpoint */
90 struct urb
*inack_urb
;
93 /* Output control endpoint */
94 struct urb
*outcont_urb
;
95 char outcont_buffer
[64];
97 /* Settings for the port */
101 unsigned int old_cflag
;
102 enum {flow_none
, flow_cts
, flow_xon
} flow_control
;
103 int rts_state
; /* Handshaking pins (outputs) */
105 int cts_state
; /* Handshaking pins (inputs) */
111 unsigned long tx_start_time
[2];
112 int resend_cont
; /* need to resend control packet */
115 /* Include Keyspan message headers. All current Keyspan Adapters
116 make use of one of five message formats which are referred
117 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
118 within this driver. */
119 #include "keyspan_usa26msg.h"
120 #include "keyspan_usa28msg.h"
121 #include "keyspan_usa49msg.h"
122 #include "keyspan_usa90msg.h"
123 #include "keyspan_usa67msg.h"
126 module_usb_serial_driver(serial_drivers
, keyspan_ids_combined
);
128 static void keyspan_break_ctl(struct tty_struct
*tty
, int break_state
)
130 struct usb_serial_port
*port
= tty
->driver_data
;
131 struct keyspan_port_private
*p_priv
;
133 p_priv
= usb_get_serial_port_data(port
);
135 if (break_state
== -1)
136 p_priv
->break_on
= 1;
138 p_priv
->break_on
= 0;
140 keyspan_send_setup(port
, 0);
144 static void keyspan_set_termios(struct tty_struct
*tty
,
145 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
147 int baud_rate
, device_port
;
148 struct keyspan_port_private
*p_priv
;
149 const struct keyspan_device_details
*d_details
;
152 p_priv
= usb_get_serial_port_data(port
);
153 d_details
= p_priv
->device_details
;
154 cflag
= tty
->termios
.c_cflag
;
155 device_port
= port
->number
- port
->serial
->minor
;
157 /* Baud rate calculation takes baud rate as an integer
158 so other rates can be generated if desired. */
159 baud_rate
= tty_get_baud_rate(tty
);
160 /* If no match or invalid, don't change */
161 if (d_details
->calculate_baud_rate(port
, baud_rate
, d_details
->baudclk
,
162 NULL
, NULL
, NULL
, device_port
) == KEYSPAN_BAUD_RATE_OK
) {
163 /* FIXME - more to do here to ensure rate changes cleanly */
164 /* FIXME - calcuate exact rate from divisor ? */
165 p_priv
->baud
= baud_rate
;
167 baud_rate
= tty_termios_baud_rate(old_termios
);
169 tty_encode_baud_rate(tty
, baud_rate
, baud_rate
);
170 /* set CTS/RTS handshake etc. */
171 p_priv
->cflag
= cflag
;
172 p_priv
->flow_control
= (cflag
& CRTSCTS
) ? flow_cts
: flow_none
;
174 /* Mark/Space not supported */
175 tty
->termios
.c_cflag
&= ~CMSPAR
;
177 keyspan_send_setup(port
, 0);
180 static int keyspan_tiocmget(struct tty_struct
*tty
)
182 struct usb_serial_port
*port
= tty
->driver_data
;
183 struct keyspan_port_private
*p_priv
= usb_get_serial_port_data(port
);
186 value
= ((p_priv
->rts_state
) ? TIOCM_RTS
: 0) |
187 ((p_priv
->dtr_state
) ? TIOCM_DTR
: 0) |
188 ((p_priv
->cts_state
) ? TIOCM_CTS
: 0) |
189 ((p_priv
->dsr_state
) ? TIOCM_DSR
: 0) |
190 ((p_priv
->dcd_state
) ? TIOCM_CAR
: 0) |
191 ((p_priv
->ri_state
) ? TIOCM_RNG
: 0);
196 static int keyspan_tiocmset(struct tty_struct
*tty
,
197 unsigned int set
, unsigned int clear
)
199 struct usb_serial_port
*port
= tty
->driver_data
;
200 struct keyspan_port_private
*p_priv
= usb_get_serial_port_data(port
);
203 p_priv
->rts_state
= 1;
205 p_priv
->dtr_state
= 1;
206 if (clear
& TIOCM_RTS
)
207 p_priv
->rts_state
= 0;
208 if (clear
& TIOCM_DTR
)
209 p_priv
->dtr_state
= 0;
210 keyspan_send_setup(port
, 0);
214 /* Write function is similar for the four protocols used
215 with only a minor change for usa90 (usa19hs) required */
216 static int keyspan_write(struct tty_struct
*tty
,
217 struct usb_serial_port
*port
, const unsigned char *buf
, int count
)
219 struct keyspan_port_private
*p_priv
;
220 const struct keyspan_device_details
*d_details
;
223 struct urb
*this_urb
;
224 int err
, maxDataLen
, dataOffset
;
226 p_priv
= usb_get_serial_port_data(port
);
227 d_details
= p_priv
->device_details
;
229 if (d_details
->msg_format
== msg_usa90
) {
237 dev_dbg(&port
->dev
, "%s - for port %d (%d chars), flip=%d\n",
238 __func__
, port
->number
, count
, p_priv
->out_flip
);
240 for (left
= count
; left
> 0; left
-= todo
) {
242 if (todo
> maxDataLen
)
245 flip
= p_priv
->out_flip
;
247 /* Check we have a valid urb/endpoint before we use it... */
248 this_urb
= p_priv
->out_urbs
[flip
];
249 if (this_urb
== NULL
) {
250 /* no bulk out, so return 0 bytes written */
251 dev_dbg(&port
->dev
, "%s - no output urb :(\n", __func__
);
255 dev_dbg(&port
->dev
, "%s - endpoint %d flip %d\n",
256 __func__
, usb_pipeendpoint(this_urb
->pipe
), flip
);
258 if (this_urb
->status
== -EINPROGRESS
) {
259 if (time_before(jiffies
,
260 p_priv
->tx_start_time
[flip
] + 10 * HZ
))
262 usb_unlink_urb(this_urb
);
266 /* First byte in buffer is "last flag" (except for usa19hx)
267 - unused so for now so set to zero */
268 ((char *)this_urb
->transfer_buffer
)[0] = 0;
270 memcpy(this_urb
->transfer_buffer
+ dataOffset
, buf
, todo
);
273 /* send the data out the bulk port */
274 this_urb
->transfer_buffer_length
= todo
+ dataOffset
;
276 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
278 dev_dbg(&port
->dev
, "usb_submit_urb(write bulk) failed (%d)\n", err
);
279 p_priv
->tx_start_time
[flip
] = jiffies
;
281 /* Flip for next time if usa26 or usa28 interface
282 (not used on usa49) */
283 p_priv
->out_flip
= (flip
+ 1) & d_details
->outdat_endp_flip
;
289 static void usa26_indat_callback(struct urb
*urb
)
293 struct usb_serial_port
*port
;
294 unsigned char *data
= urb
->transfer_buffer
;
295 int status
= urb
->status
;
297 endpoint
= usb_pipeendpoint(urb
->pipe
);
300 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x on endpoint %d.\n",
301 __func__
, status
, endpoint
);
306 if (urb
->actual_length
) {
307 /* 0x80 bit is error flag */
308 if ((data
[0] & 0x80) == 0) {
309 /* no errors on individual bytes, only
310 possible overrun err */
311 if (data
[0] & RXERROR_OVERRUN
)
315 for (i
= 1; i
< urb
->actual_length
; ++i
)
316 tty_insert_flip_char(&port
->port
, data
[i
], err
);
318 /* some bytes had errors, every byte has status */
319 dev_dbg(&port
->dev
, "%s - RX error!!!!\n", __func__
);
320 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
321 int stat
= data
[i
], flag
= 0;
322 if (stat
& RXERROR_OVERRUN
)
324 if (stat
& RXERROR_FRAMING
)
326 if (stat
& RXERROR_PARITY
)
328 /* XXX should handle break (0x10) */
329 tty_insert_flip_char(&port
->port
, data
[i
+1],
333 tty_flip_buffer_push(&port
->port
);
336 /* Resubmit urb so we continue receiving */
337 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
339 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
342 /* Outdat handling is common for all devices */
343 static void usa2x_outdat_callback(struct urb
*urb
)
345 struct usb_serial_port
*port
;
346 struct keyspan_port_private
*p_priv
;
349 p_priv
= usb_get_serial_port_data(port
);
350 dev_dbg(&port
->dev
, "%s - urb %d\n", __func__
, urb
== p_priv
->out_urbs
[1]);
352 usb_serial_port_softint(port
);
355 static void usa26_inack_callback(struct urb
*urb
)
359 static void usa26_outcont_callback(struct urb
*urb
)
361 struct usb_serial_port
*port
;
362 struct keyspan_port_private
*p_priv
;
365 p_priv
= usb_get_serial_port_data(port
);
367 if (p_priv
->resend_cont
) {
368 dev_dbg(&port
->dev
, "%s - sending setup\n", __func__
);
369 keyspan_usa26_send_setup(port
->serial
, port
,
370 p_priv
->resend_cont
- 1);
374 static void usa26_instat_callback(struct urb
*urb
)
376 unsigned char *data
= urb
->transfer_buffer
;
377 struct keyspan_usa26_portStatusMessage
*msg
;
378 struct usb_serial
*serial
;
379 struct usb_serial_port
*port
;
380 struct keyspan_port_private
*p_priv
;
381 struct tty_struct
*tty
;
382 int old_dcd_state
, err
;
383 int status
= urb
->status
;
385 serial
= urb
->context
;
388 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
391 if (urb
->actual_length
!= 9) {
392 dev_dbg(&urb
->dev
->dev
, "%s - %d byte report??\n", __func__
, urb
->actual_length
);
396 msg
= (struct keyspan_usa26_portStatusMessage
*)data
;
399 dev_dbg(&urb
->dev
->dev
,
400 "%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
401 __func__
, msg
->port
, msg
->hskia_cts
, msg
->gpia_dcd
, msg
->dsr
,
402 msg
->ri
, msg
->_txOff
, msg
->_txXoff
, msg
->rxEnabled
,
403 msg
->controlResponse
);
406 /* Now do something useful with the data */
409 /* Check port number from message and retrieve private data */
410 if (msg
->port
>= serial
->num_ports
) {
411 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n", __func__
, msg
->port
);
414 port
= serial
->port
[msg
->port
];
415 p_priv
= usb_get_serial_port_data(port
);
417 /* Update handshaking pin state information */
418 old_dcd_state
= p_priv
->dcd_state
;
419 p_priv
->cts_state
= ((msg
->hskia_cts
) ? 1 : 0);
420 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
421 p_priv
->dcd_state
= ((msg
->gpia_dcd
) ? 1 : 0);
422 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
424 if (old_dcd_state
!= p_priv
->dcd_state
) {
425 tty
= tty_port_tty_get(&port
->port
);
426 if (tty
&& !C_CLOCAL(tty
))
431 /* Resubmit urb so we continue receiving */
432 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
434 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
438 static void usa26_glocont_callback(struct urb
*urb
)
443 static void usa28_indat_callback(struct urb
*urb
)
446 struct usb_serial_port
*port
;
448 struct keyspan_port_private
*p_priv
;
449 int status
= urb
->status
;
452 p_priv
= usb_get_serial_port_data(port
);
453 data
= urb
->transfer_buffer
;
455 if (urb
!= p_priv
->in_urbs
[p_priv
->in_flip
])
460 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x on endpoint %d.\n",
461 __func__
, status
, usb_pipeendpoint(urb
->pipe
));
466 p_priv
= usb_get_serial_port_data(port
);
467 data
= urb
->transfer_buffer
;
469 if (urb
->actual_length
) {
470 tty_insert_flip_string(&port
->port
, data
,
472 tty_flip_buffer_push(&port
->port
);
475 /* Resubmit urb so we continue receiving */
476 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
478 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n",
480 p_priv
->in_flip
^= 1;
482 urb
= p_priv
->in_urbs
[p_priv
->in_flip
];
483 } while (urb
->status
!= -EINPROGRESS
);
486 static void usa28_inack_callback(struct urb
*urb
)
490 static void usa28_outcont_callback(struct urb
*urb
)
492 struct usb_serial_port
*port
;
493 struct keyspan_port_private
*p_priv
;
496 p_priv
= usb_get_serial_port_data(port
);
498 if (p_priv
->resend_cont
) {
499 dev_dbg(&port
->dev
, "%s - sending setup\n", __func__
);
500 keyspan_usa28_send_setup(port
->serial
, port
,
501 p_priv
->resend_cont
- 1);
505 static void usa28_instat_callback(struct urb
*urb
)
508 unsigned char *data
= urb
->transfer_buffer
;
509 struct keyspan_usa28_portStatusMessage
*msg
;
510 struct usb_serial
*serial
;
511 struct usb_serial_port
*port
;
512 struct keyspan_port_private
*p_priv
;
513 struct tty_struct
*tty
;
515 int status
= urb
->status
;
517 serial
= urb
->context
;
520 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
524 if (urb
->actual_length
!= sizeof(struct keyspan_usa28_portStatusMessage
)) {
525 dev_dbg(&urb
->dev
->dev
, "%s - bad length %d\n", __func__
, urb
->actual_length
);
530 dev_dbg(&urb->dev->dev,
531 "%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__,
532 data[0], data[1], data[2], data[3], data[4], data[5],
533 data[6], data[7], data[8], data[9], data[10], data[11]);
536 /* Now do something useful with the data */
537 msg
= (struct keyspan_usa28_portStatusMessage
*)data
;
539 /* Check port number from message and retrieve private data */
540 if (msg
->port
>= serial
->num_ports
) {
541 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n", __func__
, msg
->port
);
544 port
= serial
->port
[msg
->port
];
545 p_priv
= usb_get_serial_port_data(port
);
547 /* Update handshaking pin state information */
548 old_dcd_state
= p_priv
->dcd_state
;
549 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
550 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
551 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
552 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
554 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
) {
555 tty
= tty_port_tty_get(&port
->port
);
556 if (tty
&& !C_CLOCAL(tty
))
561 /* Resubmit urb so we continue receiving */
562 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
564 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
568 static void usa28_glocont_callback(struct urb
*urb
)
573 static void usa49_glocont_callback(struct urb
*urb
)
575 struct usb_serial
*serial
;
576 struct usb_serial_port
*port
;
577 struct keyspan_port_private
*p_priv
;
580 serial
= urb
->context
;
581 for (i
= 0; i
< serial
->num_ports
; ++i
) {
582 port
= serial
->port
[i
];
583 p_priv
= usb_get_serial_port_data(port
);
585 if (p_priv
->resend_cont
) {
586 dev_dbg(&port
->dev
, "%s - sending setup\n", __func__
);
587 keyspan_usa49_send_setup(serial
, port
,
588 p_priv
->resend_cont
- 1);
594 /* This is actually called glostat in the Keyspan
596 static void usa49_instat_callback(struct urb
*urb
)
599 unsigned char *data
= urb
->transfer_buffer
;
600 struct keyspan_usa49_portStatusMessage
*msg
;
601 struct usb_serial
*serial
;
602 struct usb_serial_port
*port
;
603 struct keyspan_port_private
*p_priv
;
605 int status
= urb
->status
;
607 serial
= urb
->context
;
610 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
614 if (urb
->actual_length
!=
615 sizeof(struct keyspan_usa49_portStatusMessage
)) {
616 dev_dbg(&urb
->dev
->dev
, "%s - bad length %d\n", __func__
, urb
->actual_length
);
621 dev_dbg(&urb->dev->dev, "%s: %x %x %x %x %x %x %x %x %x %x %x",
622 __func__, data[0], data[1], data[2], data[3], data[4],
623 data[5], data[6], data[7], data[8], data[9], data[10]);
626 /* Now do something useful with the data */
627 msg
= (struct keyspan_usa49_portStatusMessage
*)data
;
629 /* Check port number from message and retrieve private data */
630 if (msg
->portNumber
>= serial
->num_ports
) {
631 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n",
632 __func__
, msg
->portNumber
);
635 port
= serial
->port
[msg
->portNumber
];
636 p_priv
= usb_get_serial_port_data(port
);
638 /* Update handshaking pin state information */
639 old_dcd_state
= p_priv
->dcd_state
;
640 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
641 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
642 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
643 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
645 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
) {
646 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
647 if (tty
&& !C_CLOCAL(tty
))
652 /* Resubmit urb so we continue receiving */
653 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
655 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
659 static void usa49_inack_callback(struct urb
*urb
)
663 static void usa49_indat_callback(struct urb
*urb
)
667 struct usb_serial_port
*port
;
668 unsigned char *data
= urb
->transfer_buffer
;
669 int status
= urb
->status
;
671 endpoint
= usb_pipeendpoint(urb
->pipe
);
674 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x on endpoint %d.\n",
675 __func__
, status
, endpoint
);
680 if (urb
->actual_length
) {
681 /* 0x80 bit is error flag */
682 if ((data
[0] & 0x80) == 0) {
683 /* no error on any byte */
684 tty_insert_flip_string(&port
->port
, data
+ 1,
685 urb
->actual_length
- 1);
687 /* some bytes had errors, every byte has status */
688 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
689 int stat
= data
[i
], flag
= 0;
690 if (stat
& RXERROR_OVERRUN
)
692 if (stat
& RXERROR_FRAMING
)
694 if (stat
& RXERROR_PARITY
)
696 /* XXX should handle break (0x10) */
697 tty_insert_flip_char(&port
->port
, data
[i
+1],
701 tty_flip_buffer_push(&port
->port
);
704 /* Resubmit urb so we continue receiving */
705 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
707 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
710 static void usa49wg_indat_callback(struct urb
*urb
)
713 struct usb_serial
*serial
;
714 struct usb_serial_port
*port
;
715 unsigned char *data
= urb
->transfer_buffer
;
716 int status
= urb
->status
;
718 serial
= urb
->context
;
721 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
725 /* inbound data is in the form P#, len, status, data */
729 while (i
< urb
->actual_length
) {
731 /* Check port number from message */
732 if (data
[i
] >= serial
->num_ports
) {
733 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n",
737 port
= serial
->port
[data
[i
++]];
740 /* 0x80 bit is error flag */
741 if ((data
[i
] & 0x80) == 0) {
742 /* no error on any byte */
744 for (x
= 1; x
< len
&& i
< urb
->actual_length
; ++x
)
745 tty_insert_flip_char(&port
->port
,
749 * some bytes had errors, every byte has status
751 for (x
= 0; x
+ 1 < len
&&
752 i
+ 1 < urb
->actual_length
; x
+= 2) {
753 int stat
= data
[i
], flag
= 0;
755 if (stat
& RXERROR_OVERRUN
)
757 if (stat
& RXERROR_FRAMING
)
759 if (stat
& RXERROR_PARITY
)
761 /* XXX should handle break (0x10) */
762 tty_insert_flip_char(&port
->port
, data
[i
+1],
767 tty_flip_buffer_push(&port
->port
);
770 /* Resubmit urb so we continue receiving */
771 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
773 dev_dbg(&urb
->dev
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
776 /* not used, usa-49 doesn't have per-port control endpoints */
777 static void usa49_outcont_callback(struct urb
*urb
)
781 static void usa90_indat_callback(struct urb
*urb
)
785 struct usb_serial_port
*port
;
786 struct keyspan_port_private
*p_priv
;
787 unsigned char *data
= urb
->transfer_buffer
;
788 int status
= urb
->status
;
790 endpoint
= usb_pipeendpoint(urb
->pipe
);
793 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x on endpoint %d.\n",
794 __func__
, status
, endpoint
);
799 p_priv
= usb_get_serial_port_data(port
);
801 if (urb
->actual_length
) {
802 /* if current mode is DMA, looks like usa28 format
803 otherwise looks like usa26 data format */
805 if (p_priv
->baud
> 57600)
806 tty_insert_flip_string(&port
->port
, data
,
809 /* 0x80 bit is error flag */
810 if ((data
[0] & 0x80) == 0) {
811 /* no errors on individual bytes, only
812 possible overrun err*/
813 if (data
[0] & RXERROR_OVERRUN
)
817 for (i
= 1; i
< urb
->actual_length
; ++i
)
818 tty_insert_flip_char(&port
->port
,
821 /* some bytes had errors, every byte has status */
822 dev_dbg(&port
->dev
, "%s - RX error!!!!\n", __func__
);
823 for (i
= 0; i
+ 1 < urb
->actual_length
; i
+= 2) {
824 int stat
= data
[i
], flag
= 0;
825 if (stat
& RXERROR_OVERRUN
)
827 if (stat
& RXERROR_FRAMING
)
829 if (stat
& RXERROR_PARITY
)
831 /* XXX should handle break (0x10) */
832 tty_insert_flip_char(&port
->port
,
837 tty_flip_buffer_push(&port
->port
);
840 /* Resubmit urb so we continue receiving */
841 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
843 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
847 static void usa90_instat_callback(struct urb
*urb
)
849 unsigned char *data
= urb
->transfer_buffer
;
850 struct keyspan_usa90_portStatusMessage
*msg
;
851 struct usb_serial
*serial
;
852 struct usb_serial_port
*port
;
853 struct keyspan_port_private
*p_priv
;
854 struct tty_struct
*tty
;
855 int old_dcd_state
, err
;
856 int status
= urb
->status
;
858 serial
= urb
->context
;
861 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
864 if (urb
->actual_length
< 14) {
865 dev_dbg(&urb
->dev
->dev
, "%s - %d byte report??\n", __func__
, urb
->actual_length
);
869 msg
= (struct keyspan_usa90_portStatusMessage
*)data
;
871 /* Now do something useful with the data */
873 port
= serial
->port
[0];
874 p_priv
= usb_get_serial_port_data(port
);
876 /* Update handshaking pin state information */
877 old_dcd_state
= p_priv
->dcd_state
;
878 p_priv
->cts_state
= ((msg
->cts
) ? 1 : 0);
879 p_priv
->dsr_state
= ((msg
->dsr
) ? 1 : 0);
880 p_priv
->dcd_state
= ((msg
->dcd
) ? 1 : 0);
881 p_priv
->ri_state
= ((msg
->ri
) ? 1 : 0);
883 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
) {
884 tty
= tty_port_tty_get(&port
->port
);
885 if (tty
&& !C_CLOCAL(tty
))
890 /* Resubmit urb so we continue receiving */
891 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
893 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
898 static void usa90_outcont_callback(struct urb
*urb
)
900 struct usb_serial_port
*port
;
901 struct keyspan_port_private
*p_priv
;
904 p_priv
= usb_get_serial_port_data(port
);
906 if (p_priv
->resend_cont
) {
907 dev_dbg(&urb
->dev
->dev
, "%s - sending setup\n", __func__
);
908 keyspan_usa90_send_setup(port
->serial
, port
,
909 p_priv
->resend_cont
- 1);
913 /* Status messages from the 28xg */
914 static void usa67_instat_callback(struct urb
*urb
)
917 unsigned char *data
= urb
->transfer_buffer
;
918 struct keyspan_usa67_portStatusMessage
*msg
;
919 struct usb_serial
*serial
;
920 struct usb_serial_port
*port
;
921 struct keyspan_port_private
*p_priv
;
923 int status
= urb
->status
;
925 serial
= urb
->context
;
928 dev_dbg(&urb
->dev
->dev
, "%s - nonzero status: %x\n", __func__
, status
);
932 if (urb
->actual_length
!=
933 sizeof(struct keyspan_usa67_portStatusMessage
)) {
934 dev_dbg(&urb
->dev
->dev
, "%s - bad length %d\n", __func__
, urb
->actual_length
);
939 /* Now do something useful with the data */
940 msg
= (struct keyspan_usa67_portStatusMessage
*)data
;
942 /* Check port number from message and retrieve private data */
943 if (msg
->port
>= serial
->num_ports
) {
944 dev_dbg(&urb
->dev
->dev
, "%s - Unexpected port number %d\n", __func__
, msg
->port
);
948 port
= serial
->port
[msg
->port
];
949 p_priv
= usb_get_serial_port_data(port
);
951 /* Update handshaking pin state information */
952 old_dcd_state
= p_priv
->dcd_state
;
953 p_priv
->cts_state
= ((msg
->hskia_cts
) ? 1 : 0);
954 p_priv
->dcd_state
= ((msg
->gpia_dcd
) ? 1 : 0);
956 if (old_dcd_state
!= p_priv
->dcd_state
&& old_dcd_state
) {
957 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
958 if (tty
&& !C_CLOCAL(tty
))
963 /* Resubmit urb so we continue receiving */
964 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
966 dev_dbg(&port
->dev
, "%s - resubmit read urb failed. (%d)\n", __func__
, err
);
969 static void usa67_glocont_callback(struct urb
*urb
)
971 struct usb_serial
*serial
;
972 struct usb_serial_port
*port
;
973 struct keyspan_port_private
*p_priv
;
976 serial
= urb
->context
;
977 for (i
= 0; i
< serial
->num_ports
; ++i
) {
978 port
= serial
->port
[i
];
979 p_priv
= usb_get_serial_port_data(port
);
981 if (p_priv
->resend_cont
) {
982 dev_dbg(&port
->dev
, "%s - sending setup\n", __func__
);
983 keyspan_usa67_send_setup(serial
, port
,
984 p_priv
->resend_cont
- 1);
990 static int keyspan_write_room(struct tty_struct
*tty
)
992 struct usb_serial_port
*port
= tty
->driver_data
;
993 struct keyspan_port_private
*p_priv
;
994 const struct keyspan_device_details
*d_details
;
997 struct urb
*this_urb
;
999 p_priv
= usb_get_serial_port_data(port
);
1000 d_details
= p_priv
->device_details
;
1002 /* FIXME: locking */
1003 if (d_details
->msg_format
== msg_usa90
)
1008 flip
= p_priv
->out_flip
;
1010 /* Check both endpoints to see if any are available. */
1011 this_urb
= p_priv
->out_urbs
[flip
];
1012 if (this_urb
!= NULL
) {
1013 if (this_urb
->status
!= -EINPROGRESS
)
1015 flip
= (flip
+ 1) & d_details
->outdat_endp_flip
;
1016 this_urb
= p_priv
->out_urbs
[flip
];
1017 if (this_urb
!= NULL
) {
1018 if (this_urb
->status
!= -EINPROGRESS
)
1026 static int keyspan_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
1028 struct keyspan_port_private
*p_priv
;
1029 const struct keyspan_device_details
*d_details
;
1031 int baud_rate
, device_port
;
1033 unsigned int cflag
= 0;
1035 p_priv
= usb_get_serial_port_data(port
);
1036 d_details
= p_priv
->device_details
;
1038 /* Set some sane defaults */
1039 p_priv
->rts_state
= 1;
1040 p_priv
->dtr_state
= 1;
1041 p_priv
->baud
= 9600;
1043 /* force baud and lcr to be set on open */
1044 p_priv
->old_baud
= 0;
1045 p_priv
->old_cflag
= 0;
1047 p_priv
->out_flip
= 0;
1048 p_priv
->in_flip
= 0;
1050 /* Reset low level data toggle and start reading from endpoints */
1051 for (i
= 0; i
< 2; i
++) {
1052 urb
= p_priv
->in_urbs
[i
];
1056 /* make sure endpoint data toggle is synchronized
1058 usb_clear_halt(urb
->dev
, urb
->pipe
);
1059 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1061 dev_dbg(&port
->dev
, "%s - submit urb %d failed (%d)\n", __func__
, i
, err
);
1064 /* Reset low level data toggle on out endpoints */
1065 for (i
= 0; i
< 2; i
++) {
1066 urb
= p_priv
->out_urbs
[i
];
1069 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1070 usb_pipeout(urb->pipe), 0); */
1073 /* get the terminal config for the setup message now so we don't
1074 * need to send 2 of them */
1076 device_port
= port
->number
- port
->serial
->minor
;
1078 cflag
= tty
->termios
.c_cflag
;
1079 /* Baud rate calculation takes baud rate as an integer
1080 so other rates can be generated if desired. */
1081 baud_rate
= tty_get_baud_rate(tty
);
1082 /* If no match or invalid, leave as default */
1084 && d_details
->calculate_baud_rate(port
, baud_rate
, d_details
->baudclk
,
1085 NULL
, NULL
, NULL
, device_port
) == KEYSPAN_BAUD_RATE_OK
) {
1086 p_priv
->baud
= baud_rate
;
1089 /* set CTS/RTS handshake etc. */
1090 p_priv
->cflag
= cflag
;
1091 p_priv
->flow_control
= (cflag
& CRTSCTS
) ? flow_cts
: flow_none
;
1093 keyspan_send_setup(port
, 1);
1095 /* keyspan_set_termios(port, NULL); */
1100 static inline void stop_urb(struct urb
*urb
)
1102 if (urb
&& urb
->status
== -EINPROGRESS
)
1106 static void keyspan_dtr_rts(struct usb_serial_port
*port
, int on
)
1108 struct keyspan_port_private
*p_priv
= usb_get_serial_port_data(port
);
1110 p_priv
->rts_state
= on
;
1111 p_priv
->dtr_state
= on
;
1112 keyspan_send_setup(port
, 0);
1115 static void keyspan_close(struct usb_serial_port
*port
)
1118 struct keyspan_port_private
*p_priv
;
1120 p_priv
= usb_get_serial_port_data(port
);
1122 p_priv
->rts_state
= 0;
1123 p_priv
->dtr_state
= 0;
1125 keyspan_send_setup(port
, 2);
1126 /* pilot-xfer seems to work best with this delay */
1129 p_priv
->out_flip
= 0;
1130 p_priv
->in_flip
= 0;
1132 stop_urb(p_priv
->inack_urb
);
1133 for (i
= 0; i
< 2; i
++) {
1134 stop_urb(p_priv
->in_urbs
[i
]);
1135 stop_urb(p_priv
->out_urbs
[i
]);
1139 /* download the firmware to a pre-renumeration device */
1140 static int keyspan_fake_startup(struct usb_serial
*serial
)
1144 dev_dbg(&serial
->dev
->dev
, "Keyspan startup version %04x product %04x\n",
1145 le16_to_cpu(serial
->dev
->descriptor
.bcdDevice
),
1146 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
1148 if ((le16_to_cpu(serial
->dev
->descriptor
.bcdDevice
) & 0x8000)
1150 dev_dbg(&serial
->dev
->dev
, "Firmware already loaded. Quitting.\n");
1154 /* Select firmware image on the basis of idProduct */
1155 switch (le16_to_cpu(serial
->dev
->descriptor
.idProduct
)) {
1156 case keyspan_usa28_pre_product_id
:
1157 fw_name
= "keyspan/usa28.fw";
1160 case keyspan_usa28x_pre_product_id
:
1161 fw_name
= "keyspan/usa28x.fw";
1164 case keyspan_usa28xa_pre_product_id
:
1165 fw_name
= "keyspan/usa28xa.fw";
1168 case keyspan_usa28xb_pre_product_id
:
1169 fw_name
= "keyspan/usa28xb.fw";
1172 case keyspan_usa19_pre_product_id
:
1173 fw_name
= "keyspan/usa19.fw";
1176 case keyspan_usa19qi_pre_product_id
:
1177 fw_name
= "keyspan/usa19qi.fw";
1180 case keyspan_mpr_pre_product_id
:
1181 fw_name
= "keyspan/mpr.fw";
1184 case keyspan_usa19qw_pre_product_id
:
1185 fw_name
= "keyspan/usa19qw.fw";
1188 case keyspan_usa18x_pre_product_id
:
1189 fw_name
= "keyspan/usa18x.fw";
1192 case keyspan_usa19w_pre_product_id
:
1193 fw_name
= "keyspan/usa19w.fw";
1196 case keyspan_usa49w_pre_product_id
:
1197 fw_name
= "keyspan/usa49w.fw";
1200 case keyspan_usa49wlc_pre_product_id
:
1201 fw_name
= "keyspan/usa49wlc.fw";
1205 dev_err(&serial
->dev
->dev
, "Unknown product ID (%04x)\n",
1206 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
1210 dev_dbg(&serial
->dev
->dev
, "Uploading Keyspan %s firmware.\n", fw_name
);
1212 if (ezusb_fx1_ihex_firmware_download(serial
->dev
, fw_name
) < 0) {
1213 dev_err(&serial
->dev
->dev
, "failed to load firmware \"%s\"\n",
1218 /* after downloading firmware Renumeration will occur in a
1219 moment and the new device will bind to the real driver */
1221 /* we don't want this device to have a driver assigned to it. */
1225 /* Helper functions used by keyspan_setup_urbs */
1226 static struct usb_endpoint_descriptor
const *find_ep(struct usb_serial
const *serial
,
1229 struct usb_host_interface
*iface_desc
;
1230 struct usb_endpoint_descriptor
*ep
;
1233 iface_desc
= serial
->interface
->cur_altsetting
;
1234 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
1235 ep
= &iface_desc
->endpoint
[i
].desc
;
1236 if (ep
->bEndpointAddress
== endpoint
)
1239 dev_warn(&serial
->interface
->dev
, "found no endpoint descriptor for "
1240 "endpoint %x\n", endpoint
);
1244 static struct urb
*keyspan_setup_urb(struct usb_serial
*serial
, int endpoint
,
1245 int dir
, void *ctx
, char *buf
, int len
,
1246 void (*callback
)(struct urb
*))
1249 struct usb_endpoint_descriptor
const *ep_desc
;
1250 char const *ep_type_name
;
1253 return NULL
; /* endpoint not needed */
1255 dev_dbg(&serial
->interface
->dev
, "%s - alloc for endpoint %d.\n", __func__
, endpoint
);
1256 urb
= usb_alloc_urb(0, GFP_KERNEL
); /* No ISO */
1258 dev_dbg(&serial
->interface
->dev
, "%s - alloc for endpoint %d failed.\n", __func__
, endpoint
);
1262 if (endpoint
== 0) {
1263 /* control EP filled in when used */
1267 ep_desc
= find_ep(serial
, endpoint
);
1269 /* leak the urb, something's wrong and the callers don't care */
1272 if (usb_endpoint_xfer_int(ep_desc
)) {
1273 ep_type_name
= "INT";
1274 usb_fill_int_urb(urb
, serial
->dev
,
1275 usb_sndintpipe(serial
->dev
, endpoint
) | dir
,
1276 buf
, len
, callback
, ctx
,
1277 ep_desc
->bInterval
);
1278 } else if (usb_endpoint_xfer_bulk(ep_desc
)) {
1279 ep_type_name
= "BULK";
1280 usb_fill_bulk_urb(urb
, serial
->dev
,
1281 usb_sndbulkpipe(serial
->dev
, endpoint
) | dir
,
1282 buf
, len
, callback
, ctx
);
1284 dev_warn(&serial
->interface
->dev
,
1285 "unsupported endpoint type %x\n",
1286 usb_endpoint_type(ep_desc
));
1291 dev_dbg(&serial
->interface
->dev
, "%s - using urb %p for %s endpoint %x\n",
1292 __func__
, urb
, ep_type_name
, endpoint
);
1296 static struct callbacks
{
1297 void (*instat_callback
)(struct urb
*);
1298 void (*glocont_callback
)(struct urb
*);
1299 void (*indat_callback
)(struct urb
*);
1300 void (*outdat_callback
)(struct urb
*);
1301 void (*inack_callback
)(struct urb
*);
1302 void (*outcont_callback
)(struct urb
*);
1303 } keyspan_callbacks
[] = {
1305 /* msg_usa26 callbacks */
1306 .instat_callback
= usa26_instat_callback
,
1307 .glocont_callback
= usa26_glocont_callback
,
1308 .indat_callback
= usa26_indat_callback
,
1309 .outdat_callback
= usa2x_outdat_callback
,
1310 .inack_callback
= usa26_inack_callback
,
1311 .outcont_callback
= usa26_outcont_callback
,
1313 /* msg_usa28 callbacks */
1314 .instat_callback
= usa28_instat_callback
,
1315 .glocont_callback
= usa28_glocont_callback
,
1316 .indat_callback
= usa28_indat_callback
,
1317 .outdat_callback
= usa2x_outdat_callback
,
1318 .inack_callback
= usa28_inack_callback
,
1319 .outcont_callback
= usa28_outcont_callback
,
1321 /* msg_usa49 callbacks */
1322 .instat_callback
= usa49_instat_callback
,
1323 .glocont_callback
= usa49_glocont_callback
,
1324 .indat_callback
= usa49_indat_callback
,
1325 .outdat_callback
= usa2x_outdat_callback
,
1326 .inack_callback
= usa49_inack_callback
,
1327 .outcont_callback
= usa49_outcont_callback
,
1329 /* msg_usa90 callbacks */
1330 .instat_callback
= usa90_instat_callback
,
1331 .glocont_callback
= usa28_glocont_callback
,
1332 .indat_callback
= usa90_indat_callback
,
1333 .outdat_callback
= usa2x_outdat_callback
,
1334 .inack_callback
= usa28_inack_callback
,
1335 .outcont_callback
= usa90_outcont_callback
,
1337 /* msg_usa67 callbacks */
1338 .instat_callback
= usa67_instat_callback
,
1339 .glocont_callback
= usa67_glocont_callback
,
1340 .indat_callback
= usa26_indat_callback
,
1341 .outdat_callback
= usa2x_outdat_callback
,
1342 .inack_callback
= usa26_inack_callback
,
1343 .outcont_callback
= usa26_outcont_callback
,
1347 /* Generic setup urbs function that uses
1348 data in device_details */
1349 static void keyspan_setup_urbs(struct usb_serial
*serial
)
1351 struct keyspan_serial_private
*s_priv
;
1352 const struct keyspan_device_details
*d_details
;
1353 struct callbacks
*cback
;
1355 s_priv
= usb_get_serial_data(serial
);
1356 d_details
= s_priv
->device_details
;
1358 /* Setup values for the various callback routines */
1359 cback
= &keyspan_callbacks
[d_details
->msg_format
];
1361 /* Allocate and set up urbs for each one that is in use,
1362 starting with instat endpoints */
1363 s_priv
->instat_urb
= keyspan_setup_urb
1364 (serial
, d_details
->instat_endpoint
, USB_DIR_IN
,
1365 serial
, s_priv
->instat_buf
, INSTAT_BUFLEN
,
1366 cback
->instat_callback
);
1368 s_priv
->indat_urb
= keyspan_setup_urb
1369 (serial
, d_details
->indat_endpoint
, USB_DIR_IN
,
1370 serial
, s_priv
->indat_buf
, INDAT49W_BUFLEN
,
1371 usa49wg_indat_callback
);
1373 s_priv
->glocont_urb
= keyspan_setup_urb
1374 (serial
, d_details
->glocont_endpoint
, USB_DIR_OUT
,
1375 serial
, s_priv
->glocont_buf
, GLOCONT_BUFLEN
,
1376 cback
->glocont_callback
);
1379 /* usa19 function doesn't require prescaler */
1380 static int keyspan_usa19_calc_baud(struct usb_serial_port
*port
,
1381 u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1382 u8
*rate_low
, u8
*prescaler
, int portnum
)
1384 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1386 cnt
; /* inverse of divisor (programmed into 8051) */
1388 dev_dbg(&port
->dev
, "%s - %d.\n", __func__
, baud_rate
);
1390 /* prevent divide by zero... */
1391 b16
= baud_rate
* 16L;
1393 return KEYSPAN_INVALID_BAUD_RATE
;
1394 /* Any "standard" rate over 57k6 is marginal on the USA-19
1395 as we run out of divisor resolution. */
1396 if (baud_rate
> 57600)
1397 return KEYSPAN_INVALID_BAUD_RATE
;
1399 /* calculate the divisor and the counter (its inverse) */
1400 div
= baudclk
/ b16
;
1402 return KEYSPAN_INVALID_BAUD_RATE
;
1407 return KEYSPAN_INVALID_BAUD_RATE
;
1409 /* return the counter values if non-null */
1411 *rate_low
= (u8
) (cnt
& 0xff);
1413 *rate_hi
= (u8
) ((cnt
>> 8) & 0xff);
1414 if (rate_low
&& rate_hi
)
1415 dev_dbg(&port
->dev
, "%s - %d %02x %02x.\n",
1416 __func__
, baud_rate
, *rate_hi
, *rate_low
);
1417 return KEYSPAN_BAUD_RATE_OK
;
1420 /* usa19hs function doesn't require prescaler */
1421 static int keyspan_usa19hs_calc_baud(struct usb_serial_port
*port
,
1422 u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1423 u8
*rate_low
, u8
*prescaler
, int portnum
)
1425 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1428 dev_dbg(&port
->dev
, "%s - %d.\n", __func__
, baud_rate
);
1430 /* prevent divide by zero... */
1431 b16
= baud_rate
* 16L;
1433 return KEYSPAN_INVALID_BAUD_RATE
;
1435 /* calculate the divisor */
1436 div
= baudclk
/ b16
;
1438 return KEYSPAN_INVALID_BAUD_RATE
;
1441 return KEYSPAN_INVALID_BAUD_RATE
;
1443 /* return the counter values if non-null */
1445 *rate_low
= (u8
) (div
& 0xff);
1448 *rate_hi
= (u8
) ((div
>> 8) & 0xff);
1450 if (rate_low
&& rate_hi
)
1451 dev_dbg(&port
->dev
, "%s - %d %02x %02x.\n",
1452 __func__
, baud_rate
, *rate_hi
, *rate_low
);
1454 return KEYSPAN_BAUD_RATE_OK
;
1457 static int keyspan_usa19w_calc_baud(struct usb_serial_port
*port
,
1458 u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1459 u8
*rate_low
, u8
*prescaler
, int portnum
)
1461 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1462 clk
, /* clock with 13/8 prescaler */
1463 div
, /* divisor using 13/8 prescaler */
1464 res
, /* resulting baud rate using 13/8 prescaler */
1465 diff
, /* error using 13/8 prescaler */
1470 dev_dbg(&port
->dev
, "%s - %d.\n", __func__
, baud_rate
);
1472 /* prevent divide by zero */
1473 b16
= baud_rate
* 16L;
1475 return KEYSPAN_INVALID_BAUD_RATE
;
1477 /* Calculate prescaler by trying them all and looking
1480 /* start with largest possible difference */
1481 smallest_diff
= 0xffffffff;
1483 /* 0 is an invalid prescaler, used as a flag */
1486 for (i
= 8; i
<= 0xff; ++i
) {
1487 clk
= (baudclk
* 8) / (u32
) i
;
1494 diff
= (res
> b16
) ? (res
-b16
) : (b16
-res
);
1496 if (diff
< smallest_diff
) {
1498 smallest_diff
= diff
;
1502 if (best_prescaler
== 0)
1503 return KEYSPAN_INVALID_BAUD_RATE
;
1505 clk
= (baudclk
* 8) / (u32
) best_prescaler
;
1508 /* return the divisor and prescaler if non-null */
1510 *rate_low
= (u8
) (div
& 0xff);
1512 *rate_hi
= (u8
) ((div
>> 8) & 0xff);
1514 *prescaler
= best_prescaler
;
1515 /* dev_dbg(&port->dev, "%s - %d %d\n", __func__, *prescaler, div); */
1517 return KEYSPAN_BAUD_RATE_OK
;
1520 /* USA-28 supports different maximum baud rates on each port */
1521 static int keyspan_usa28_calc_baud(struct usb_serial_port
*port
,
1522 u32 baud_rate
, u32 baudclk
, u8
*rate_hi
,
1523 u8
*rate_low
, u8
*prescaler
, int portnum
)
1525 u32 b16
, /* baud rate times 16 (actual rate used internally) */
1527 cnt
; /* inverse of divisor (programmed into 8051) */
1529 dev_dbg(&port
->dev
, "%s - %d.\n", __func__
, baud_rate
);
1531 /* prevent divide by zero */
1532 b16
= baud_rate
* 16L;
1534 return KEYSPAN_INVALID_BAUD_RATE
;
1536 /* calculate the divisor and the counter (its inverse) */
1537 div
= KEYSPAN_USA28_BAUDCLK
/ b16
;
1539 return KEYSPAN_INVALID_BAUD_RATE
;
1543 /* check for out of range, based on portnum,
1544 and return result */
1547 return KEYSPAN_INVALID_BAUD_RATE
;
1551 return KEYSPAN_INVALID_BAUD_RATE
;
1553 return KEYSPAN_INVALID_BAUD_RATE
;
1556 /* return the counter values if not NULL
1557 (port 1 will ignore retHi) */
1559 *rate_low
= (u8
) (cnt
& 0xff);
1561 *rate_hi
= (u8
) ((cnt
>> 8) & 0xff);
1562 dev_dbg(&port
->dev
, "%s - %d OK.\n", __func__
, baud_rate
);
1563 return KEYSPAN_BAUD_RATE_OK
;
1566 static int keyspan_usa26_send_setup(struct usb_serial
*serial
,
1567 struct usb_serial_port
*port
,
1570 struct keyspan_usa26_portControlMessage msg
;
1571 struct keyspan_serial_private
*s_priv
;
1572 struct keyspan_port_private
*p_priv
;
1573 const struct keyspan_device_details
*d_details
;
1575 struct urb
*this_urb
;
1576 int device_port
, err
;
1578 dev_dbg(&port
->dev
, "%s reset=%d\n", __func__
, reset_port
);
1580 s_priv
= usb_get_serial_data(serial
);
1581 p_priv
= usb_get_serial_port_data(port
);
1582 d_details
= s_priv
->device_details
;
1583 device_port
= port
->number
- port
->serial
->minor
;
1585 outcont_urb
= d_details
->outcont_endpoints
[port
->number
];
1586 this_urb
= p_priv
->outcont_urb
;
1588 dev_dbg(&port
->dev
, "%s - endpoint %d\n", __func__
, usb_pipeendpoint(this_urb
->pipe
));
1590 /* Make sure we have an urb then send the message */
1591 if (this_urb
== NULL
) {
1592 dev_dbg(&port
->dev
, "%s - oops no urb.\n", __func__
);
1596 /* Save reset port val for resend.
1597 Don't overwrite resend for open/close condition. */
1598 if ((reset_port
+ 1) > p_priv
->resend_cont
)
1599 p_priv
->resend_cont
= reset_port
+ 1;
1600 if (this_urb
->status
== -EINPROGRESS
) {
1601 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1606 memset(&msg
, 0, sizeof(struct keyspan_usa26_portControlMessage
));
1608 /* Only set baud rate if it's changed */
1609 if (p_priv
->old_baud
!= p_priv
->baud
) {
1610 p_priv
->old_baud
= p_priv
->baud
;
1611 msg
.setClocking
= 0xff;
1612 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
1613 &msg
.baudHi
, &msg
.baudLo
, &msg
.prescaler
,
1614 device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
1615 dev_dbg(&port
->dev
, "%s - Invalid baud rate %d requested, using 9600.\n",
1616 __func__
, p_priv
->baud
);
1618 msg
.baudHi
= 125; /* Values for 9600 baud */
1621 msg
.setPrescaler
= 0xff;
1624 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
1625 switch (p_priv
->cflag
& CSIZE
) {
1627 msg
.lcr
|= USA_DATABITS_5
;
1630 msg
.lcr
|= USA_DATABITS_6
;
1633 msg
.lcr
|= USA_DATABITS_7
;
1636 msg
.lcr
|= USA_DATABITS_8
;
1639 if (p_priv
->cflag
& PARENB
) {
1640 /* note USA_PARITY_NONE == 0 */
1641 msg
.lcr
|= (p_priv
->cflag
& PARODD
) ?
1642 USA_PARITY_ODD
: USA_PARITY_EVEN
;
1646 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
1647 msg
.xonFlowControl
= 0;
1648 msg
.setFlowControl
= 0xff;
1649 msg
.forwardingLength
= 16;
1654 if (reset_port
== 1) {
1663 msg
.returnStatus
= 0;
1664 msg
.resetDataToggle
= 0xff;
1668 else if (reset_port
== 2) {
1677 msg
.returnStatus
= 0;
1678 msg
.resetDataToggle
= 0;
1681 /* Sending intermediate configs */
1683 msg
._txOn
= (!p_priv
->break_on
);
1686 msg
.txBreak
= (p_priv
->break_on
);
1691 msg
.returnStatus
= 0;
1692 msg
.resetDataToggle
= 0x0;
1695 /* Do handshaking outputs */
1696 msg
.setTxTriState_setRts
= 0xff;
1697 msg
.txTriState_rts
= p_priv
->rts_state
;
1699 msg
.setHskoa_setDtr
= 0xff;
1700 msg
.hskoa_dtr
= p_priv
->dtr_state
;
1702 p_priv
->resend_cont
= 0;
1703 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
1705 /* send the data out the device on control endpoint */
1706 this_urb
->transfer_buffer_length
= sizeof(msg
);
1708 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
1710 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed (%d)\n", __func__
, err
);
1713 dev_dbg(&port
->dev
, "%s - usb_submit_urb(%d) OK %d bytes (end %d)\n", __func__
1714 outcont_urb
, this_urb
->transfer_buffer_length
,
1715 usb_pipeendpoint(this_urb
->pipe
));
1722 static int keyspan_usa28_send_setup(struct usb_serial
*serial
,
1723 struct usb_serial_port
*port
,
1726 struct keyspan_usa28_portControlMessage msg
;
1727 struct keyspan_serial_private
*s_priv
;
1728 struct keyspan_port_private
*p_priv
;
1729 const struct keyspan_device_details
*d_details
;
1730 struct urb
*this_urb
;
1731 int device_port
, err
;
1733 s_priv
= usb_get_serial_data(serial
);
1734 p_priv
= usb_get_serial_port_data(port
);
1735 d_details
= s_priv
->device_details
;
1736 device_port
= port
->number
- port
->serial
->minor
;
1738 /* only do something if we have a bulk out endpoint */
1739 this_urb
= p_priv
->outcont_urb
;
1740 if (this_urb
== NULL
) {
1741 dev_dbg(&port
->dev
, "%s - oops no urb.\n", __func__
);
1745 /* Save reset port val for resend.
1746 Don't overwrite resend for open/close condition. */
1747 if ((reset_port
+ 1) > p_priv
->resend_cont
)
1748 p_priv
->resend_cont
= reset_port
+ 1;
1749 if (this_urb
->status
== -EINPROGRESS
) {
1750 dev_dbg(&port
->dev
, "%s already writing\n", __func__
);
1755 memset(&msg
, 0, sizeof(struct keyspan_usa28_portControlMessage
));
1757 msg
.setBaudRate
= 1;
1758 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
1759 &msg
.baudHi
, &msg
.baudLo
, NULL
,
1760 device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
1761 dev_dbg(&port
->dev
, "%s - Invalid baud rate requested %d.\n",
1762 __func__
, p_priv
->baud
);
1764 msg
.baudHi
= 0xb2; /* Values for 9600 baud */
1767 /* If parity is enabled, we must calculate it ourselves. */
1768 msg
.parity
= 0; /* XXX for now */
1770 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
1771 msg
.xonFlowControl
= 0;
1773 /* Do handshaking outputs, DTR is inverted relative to RTS */
1774 msg
.rts
= p_priv
->rts_state
;
1775 msg
.dtr
= p_priv
->dtr_state
;
1777 msg
.forwardingLength
= 16;
1779 msg
.breakThreshold
= 45;
1783 /*msg.returnStatus = 1;
1784 msg.resetDataToggle = 0xff;*/
1786 if (reset_port
== 1) {
1790 msg
.txForceXoff
= 0;
1796 msg
.returnStatus
= 0;
1797 msg
.resetDataToggle
= 0xff;
1800 else if (reset_port
== 2) {
1804 msg
.txForceXoff
= 0;
1810 msg
.returnStatus
= 0;
1811 msg
.resetDataToggle
= 0;
1813 /* Sending intermediate configs */
1815 msg
._txOn
= (!p_priv
->break_on
);
1818 msg
.txForceXoff
= 0;
1819 msg
.txBreak
= (p_priv
->break_on
);
1824 msg
.returnStatus
= 0;
1825 msg
.resetDataToggle
= 0x0;
1828 p_priv
->resend_cont
= 0;
1829 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
1831 /* send the data out the device on control endpoint */
1832 this_urb
->transfer_buffer_length
= sizeof(msg
);
1834 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
1836 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed\n", __func__
);
1839 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) OK %d bytes\n", __func__
,
1840 this_urb
->transfer_buffer_length
);
1847 static int keyspan_usa49_send_setup(struct usb_serial
*serial
,
1848 struct usb_serial_port
*port
,
1851 struct keyspan_usa49_portControlMessage msg
;
1852 struct usb_ctrlrequest
*dr
= NULL
;
1853 struct keyspan_serial_private
*s_priv
;
1854 struct keyspan_port_private
*p_priv
;
1855 const struct keyspan_device_details
*d_details
;
1856 struct urb
*this_urb
;
1857 int err
, device_port
;
1859 s_priv
= usb_get_serial_data(serial
);
1860 p_priv
= usb_get_serial_port_data(port
);
1861 d_details
= s_priv
->device_details
;
1863 this_urb
= s_priv
->glocont_urb
;
1865 /* Work out which port within the device is being setup */
1866 device_port
= port
->number
- port
->serial
->minor
;
1868 /* Make sure we have an urb then send the message */
1869 if (this_urb
== NULL
) {
1870 dev_dbg(&port
->dev
, "%s - oops no urb for port %d.\n", __func__
, port
->number
);
1874 dev_dbg(&port
->dev
, "%s - endpoint %d port %d (%d)\n",
1875 __func__
, usb_pipeendpoint(this_urb
->pipe
),
1876 port
->number
, device_port
);
1878 /* Save reset port val for resend.
1879 Don't overwrite resend for open/close condition. */
1880 if ((reset_port
+ 1) > p_priv
->resend_cont
)
1881 p_priv
->resend_cont
= reset_port
+ 1;
1883 if (this_urb
->status
== -EINPROGRESS
) {
1884 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
1889 memset(&msg
, 0, sizeof(struct keyspan_usa49_portControlMessage
));
1891 /*msg.portNumber = port->number;*/
1892 msg
.portNumber
= device_port
;
1894 /* Only set baud rate if it's changed */
1895 if (p_priv
->old_baud
!= p_priv
->baud
) {
1896 p_priv
->old_baud
= p_priv
->baud
;
1897 msg
.setClocking
= 0xff;
1898 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
1899 &msg
.baudHi
, &msg
.baudLo
, &msg
.prescaler
,
1900 device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
1901 dev_dbg(&port
->dev
, "%s - Invalid baud rate %d requested, using 9600.\n",
1902 __func__
, p_priv
->baud
);
1904 msg
.baudHi
= 125; /* Values for 9600 baud */
1907 /* msg.setPrescaler = 0xff; */
1910 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
1911 switch (p_priv
->cflag
& CSIZE
) {
1913 msg
.lcr
|= USA_DATABITS_5
;
1916 msg
.lcr
|= USA_DATABITS_6
;
1919 msg
.lcr
|= USA_DATABITS_7
;
1922 msg
.lcr
|= USA_DATABITS_8
;
1925 if (p_priv
->cflag
& PARENB
) {
1926 /* note USA_PARITY_NONE == 0 */
1927 msg
.lcr
|= (p_priv
->cflag
& PARODD
) ?
1928 USA_PARITY_ODD
: USA_PARITY_EVEN
;
1932 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
1933 msg
.xonFlowControl
= 0;
1934 msg
.setFlowControl
= 0xff;
1936 msg
.forwardingLength
= 16;
1941 if (reset_port
== 1) {
1950 msg
.returnStatus
= 0;
1951 msg
.resetDataToggle
= 0xff;
1953 msg
.disablePort
= 0;
1956 else if (reset_port
== 2) {
1965 msg
.returnStatus
= 0;
1966 msg
.resetDataToggle
= 0;
1968 msg
.disablePort
= 1;
1970 /* Sending intermediate configs */
1972 msg
._txOn
= (!p_priv
->break_on
);
1975 msg
.txBreak
= (p_priv
->break_on
);
1980 msg
.returnStatus
= 0;
1981 msg
.resetDataToggle
= 0x0;
1983 msg
.disablePort
= 0;
1986 /* Do handshaking outputs */
1988 msg
.rts
= p_priv
->rts_state
;
1991 msg
.dtr
= p_priv
->dtr_state
;
1993 p_priv
->resend_cont
= 0;
1995 /* if the device is a 49wg, we send control message on usb
1998 if (d_details
->product_id
== keyspan_usa49wg_product_id
) {
1999 dr
= (void *)(s_priv
->ctrl_buf
);
2000 dr
->bRequestType
= USB_TYPE_VENDOR
| USB_DIR_OUT
;
2001 dr
->bRequest
= 0xB0; /* 49wg control message */;
2004 dr
->wLength
= cpu_to_le16(sizeof(msg
));
2006 memcpy(s_priv
->glocont_buf
, &msg
, sizeof(msg
));
2008 usb_fill_control_urb(this_urb
, serial
->dev
,
2009 usb_sndctrlpipe(serial
->dev
, 0),
2010 (unsigned char *)dr
, s_priv
->glocont_buf
,
2011 sizeof(msg
), usa49_glocont_callback
, serial
);
2014 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2016 /* send the data out the device on control endpoint */
2017 this_urb
->transfer_buffer_length
= sizeof(msg
);
2019 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2021 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed (%d)\n", __func__
, err
);
2024 dev_dbg(&port
->dev
, "%s - usb_submit_urb(%d) OK %d bytes (end %d)\n", __func__
,
2025 outcont_urb
, this_urb
->transfer_buffer_length
,
2026 usb_pipeendpoint(this_urb
->pipe
));
2033 static int keyspan_usa90_send_setup(struct usb_serial
*serial
,
2034 struct usb_serial_port
*port
,
2037 struct keyspan_usa90_portControlMessage msg
;
2038 struct keyspan_serial_private
*s_priv
;
2039 struct keyspan_port_private
*p_priv
;
2040 const struct keyspan_device_details
*d_details
;
2041 struct urb
*this_urb
;
2045 s_priv
= usb_get_serial_data(serial
);
2046 p_priv
= usb_get_serial_port_data(port
);
2047 d_details
= s_priv
->device_details
;
2049 /* only do something if we have a bulk out endpoint */
2050 this_urb
= p_priv
->outcont_urb
;
2051 if (this_urb
== NULL
) {
2052 dev_dbg(&port
->dev
, "%s - oops no urb.\n", __func__
);
2056 /* Save reset port val for resend.
2057 Don't overwrite resend for open/close condition. */
2058 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2059 p_priv
->resend_cont
= reset_port
+ 1;
2060 if (this_urb
->status
== -EINPROGRESS
) {
2061 dev_dbg(&port
->dev
, "%s already writing\n", __func__
);
2066 memset(&msg
, 0, sizeof(struct keyspan_usa90_portControlMessage
));
2068 /* Only set baud rate if it's changed */
2069 if (p_priv
->old_baud
!= p_priv
->baud
) {
2070 p_priv
->old_baud
= p_priv
->baud
;
2071 msg
.setClocking
= 0x01;
2072 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
2073 &msg
.baudHi
, &msg
.baudLo
, &prescaler
, 0) == KEYSPAN_INVALID_BAUD_RATE
) {
2074 dev_dbg(&port
->dev
, "%s - Invalid baud rate %d requested, using 9600.\n",
2075 __func__
, p_priv
->baud
);
2076 p_priv
->baud
= 9600;
2077 d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
2078 &msg
.baudHi
, &msg
.baudLo
, &prescaler
, 0);
2084 /* modes must always be correctly specified */
2085 if (p_priv
->baud
> 57600) {
2086 msg
.rxMode
= RXMODE_DMA
;
2087 msg
.txMode
= TXMODE_DMA
;
2089 msg
.rxMode
= RXMODE_BYHAND
;
2090 msg
.txMode
= TXMODE_BYHAND
;
2093 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
2094 switch (p_priv
->cflag
& CSIZE
) {
2096 msg
.lcr
|= USA_DATABITS_5
;
2099 msg
.lcr
|= USA_DATABITS_6
;
2102 msg
.lcr
|= USA_DATABITS_7
;
2105 msg
.lcr
|= USA_DATABITS_8
;
2108 if (p_priv
->cflag
& PARENB
) {
2109 /* note USA_PARITY_NONE == 0 */
2110 msg
.lcr
|= (p_priv
->cflag
& PARODD
) ?
2111 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2113 if (p_priv
->old_cflag
!= p_priv
->cflag
) {
2114 p_priv
->old_cflag
= p_priv
->cflag
;
2118 if (p_priv
->flow_control
== flow_cts
)
2119 msg
.txFlowControl
= TXFLOW_CTS
;
2120 msg
.setTxFlowControl
= 0x01;
2121 msg
.setRxFlowControl
= 0x01;
2123 msg
.rxForwardingLength
= 16;
2124 msg
.rxForwardingTimeout
= 16;
2125 msg
.txAckSetting
= 0;
2130 if (reset_port
== 1) {
2131 msg
.portEnabled
= 1;
2133 msg
.txBreak
= (p_priv
->break_on
);
2136 else if (reset_port
== 2)
2137 msg
.portEnabled
= 0;
2138 /* Sending intermediate configs */
2140 msg
.portEnabled
= 1;
2141 msg
.txBreak
= (p_priv
->break_on
);
2144 /* Do handshaking outputs */
2146 msg
.rts
= p_priv
->rts_state
;
2149 msg
.dtr
= p_priv
->dtr_state
;
2151 p_priv
->resend_cont
= 0;
2152 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2154 /* send the data out the device on control endpoint */
2155 this_urb
->transfer_buffer_length
= sizeof(msg
);
2157 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2159 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed (%d)\n", __func__
, err
);
2163 static int keyspan_usa67_send_setup(struct usb_serial
*serial
,
2164 struct usb_serial_port
*port
,
2167 struct keyspan_usa67_portControlMessage msg
;
2168 struct keyspan_serial_private
*s_priv
;
2169 struct keyspan_port_private
*p_priv
;
2170 const struct keyspan_device_details
*d_details
;
2171 struct urb
*this_urb
;
2172 int err
, device_port
;
2174 s_priv
= usb_get_serial_data(serial
);
2175 p_priv
= usb_get_serial_port_data(port
);
2176 d_details
= s_priv
->device_details
;
2178 this_urb
= s_priv
->glocont_urb
;
2180 /* Work out which port within the device is being setup */
2181 device_port
= port
->number
- port
->serial
->minor
;
2183 /* Make sure we have an urb then send the message */
2184 if (this_urb
== NULL
) {
2185 dev_dbg(&port
->dev
, "%s - oops no urb for port %d.\n", __func__
,
2190 /* Save reset port val for resend.
2191 Don't overwrite resend for open/close condition. */
2192 if ((reset_port
+ 1) > p_priv
->resend_cont
)
2193 p_priv
->resend_cont
= reset_port
+ 1;
2194 if (this_urb
->status
== -EINPROGRESS
) {
2195 /* dev_dbg(&port->dev, "%s - already writing\n", __func__); */
2200 memset(&msg
, 0, sizeof(struct keyspan_usa67_portControlMessage
));
2202 msg
.port
= device_port
;
2204 /* Only set baud rate if it's changed */
2205 if (p_priv
->old_baud
!= p_priv
->baud
) {
2206 p_priv
->old_baud
= p_priv
->baud
;
2207 msg
.setClocking
= 0xff;
2208 if (d_details
->calculate_baud_rate(port
, p_priv
->baud
, d_details
->baudclk
,
2209 &msg
.baudHi
, &msg
.baudLo
, &msg
.prescaler
,
2210 device_port
) == KEYSPAN_INVALID_BAUD_RATE
) {
2211 dev_dbg(&port
->dev
, "%s - Invalid baud rate %d requested, using 9600.\n",
2212 __func__
, p_priv
->baud
);
2214 msg
.baudHi
= 125; /* Values for 9600 baud */
2217 msg
.setPrescaler
= 0xff;
2220 msg
.lcr
= (p_priv
->cflag
& CSTOPB
) ? STOPBITS_678_2
: STOPBITS_5678_1
;
2221 switch (p_priv
->cflag
& CSIZE
) {
2223 msg
.lcr
|= USA_DATABITS_5
;
2226 msg
.lcr
|= USA_DATABITS_6
;
2229 msg
.lcr
|= USA_DATABITS_7
;
2232 msg
.lcr
|= USA_DATABITS_8
;
2235 if (p_priv
->cflag
& PARENB
) {
2236 /* note USA_PARITY_NONE == 0 */
2237 msg
.lcr
|= (p_priv
->cflag
& PARODD
) ?
2238 USA_PARITY_ODD
: USA_PARITY_EVEN
;
2242 msg
.ctsFlowControl
= (p_priv
->flow_control
== flow_cts
);
2243 msg
.xonFlowControl
= 0;
2244 msg
.setFlowControl
= 0xff;
2245 msg
.forwardingLength
= 16;
2249 if (reset_port
== 1) {
2259 msg
.returnStatus
= 0;
2260 msg
.resetDataToggle
= 0xff;
2261 } else if (reset_port
== 2) {
2271 msg
.returnStatus
= 0;
2272 msg
.resetDataToggle
= 0;
2274 /* Sending intermediate configs */
2275 msg
._txOn
= (!p_priv
->break_on
);
2278 msg
.txBreak
= (p_priv
->break_on
);
2283 msg
.returnStatus
= 0;
2284 msg
.resetDataToggle
= 0x0;
2287 /* Do handshaking outputs */
2288 msg
.setTxTriState_setRts
= 0xff;
2289 msg
.txTriState_rts
= p_priv
->rts_state
;
2291 msg
.setHskoa_setDtr
= 0xff;
2292 msg
.hskoa_dtr
= p_priv
->dtr_state
;
2294 p_priv
->resend_cont
= 0;
2296 memcpy(this_urb
->transfer_buffer
, &msg
, sizeof(msg
));
2298 /* send the data out the device on control endpoint */
2299 this_urb
->transfer_buffer_length
= sizeof(msg
);
2301 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
2303 dev_dbg(&port
->dev
, "%s - usb_submit_urb(setup) failed (%d)\n", __func__
, err
);
2307 static void keyspan_send_setup(struct usb_serial_port
*port
, int reset_port
)
2309 struct usb_serial
*serial
= port
->serial
;
2310 struct keyspan_serial_private
*s_priv
;
2311 const struct keyspan_device_details
*d_details
;
2313 s_priv
= usb_get_serial_data(serial
);
2314 d_details
= s_priv
->device_details
;
2316 switch (d_details
->msg_format
) {
2318 keyspan_usa26_send_setup(serial
, port
, reset_port
);
2321 keyspan_usa28_send_setup(serial
, port
, reset_port
);
2324 keyspan_usa49_send_setup(serial
, port
, reset_port
);
2327 keyspan_usa90_send_setup(serial
, port
, reset_port
);
2330 keyspan_usa67_send_setup(serial
, port
, reset_port
);
2336 /* Gets called by the "real" driver (ie once firmware is loaded
2337 and renumeration has taken place. */
2338 static int keyspan_startup(struct usb_serial
*serial
)
2341 struct keyspan_serial_private
*s_priv
;
2342 const struct keyspan_device_details
*d_details
;
2344 for (i
= 0; (d_details
= keyspan_devices
[i
]) != NULL
; ++i
)
2345 if (d_details
->product_id
==
2346 le16_to_cpu(serial
->dev
->descriptor
.idProduct
))
2348 if (d_details
== NULL
) {
2349 dev_err(&serial
->dev
->dev
, "%s - unknown product id %x\n",
2350 __func__
, le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
2354 /* Setup private data for serial driver */
2355 s_priv
= kzalloc(sizeof(struct keyspan_serial_private
), GFP_KERNEL
);
2357 dev_dbg(&serial
->dev
->dev
, "%s - kmalloc for keyspan_serial_private failed.\n", __func__
);
2361 s_priv
->device_details
= d_details
;
2362 usb_set_serial_data(serial
, s_priv
);
2364 keyspan_setup_urbs(serial
);
2366 if (s_priv
->instat_urb
!= NULL
) {
2367 err
= usb_submit_urb(s_priv
->instat_urb
, GFP_KERNEL
);
2369 dev_dbg(&serial
->dev
->dev
, "%s - submit instat urb failed %d\n", __func__
, err
);
2371 if (s_priv
->indat_urb
!= NULL
) {
2372 err
= usb_submit_urb(s_priv
->indat_urb
, GFP_KERNEL
);
2374 dev_dbg(&serial
->dev
->dev
, "%s - submit indat urb failed %d\n", __func__
, err
);
2380 static void keyspan_disconnect(struct usb_serial
*serial
)
2382 struct keyspan_serial_private
*s_priv
;
2384 s_priv
= usb_get_serial_data(serial
);
2386 stop_urb(s_priv
->instat_urb
);
2387 stop_urb(s_priv
->glocont_urb
);
2388 stop_urb(s_priv
->indat_urb
);
2391 static void keyspan_release(struct usb_serial
*serial
)
2393 struct keyspan_serial_private
*s_priv
;
2395 s_priv
= usb_get_serial_data(serial
);
2397 usb_free_urb(s_priv
->instat_urb
);
2398 usb_free_urb(s_priv
->indat_urb
);
2399 usb_free_urb(s_priv
->glocont_urb
);
2404 static int keyspan_port_probe(struct usb_serial_port
*port
)
2406 struct usb_serial
*serial
= port
->serial
;
2407 struct keyspan_serial_private
*s_priv
;
2408 struct keyspan_port_private
*p_priv
;
2409 const struct keyspan_device_details
*d_details
;
2410 struct callbacks
*cback
;
2415 s_priv
= usb_get_serial_data(serial
);
2416 d_details
= s_priv
->device_details
;
2418 p_priv
= kzalloc(sizeof(*p_priv
), GFP_KERNEL
);
2422 p_priv
->device_details
= d_details
;
2424 /* Setup values for the various callback routines */
2425 cback
= &keyspan_callbacks
[d_details
->msg_format
];
2427 port_num
= port
->number
- port
->serial
->minor
;
2429 /* Do indat endpoints first, once for each flip */
2430 endp
= d_details
->indat_endpoints
[port_num
];
2431 for (i
= 0; i
<= d_details
->indat_endp_flip
; ++i
, ++endp
) {
2432 p_priv
->in_urbs
[i
] = keyspan_setup_urb(serial
, endp
,
2434 p_priv
->in_buffer
[i
], 64,
2435 cback
->indat_callback
);
2437 /* outdat endpoints also have flip */
2438 endp
= d_details
->outdat_endpoints
[port_num
];
2439 for (i
= 0; i
<= d_details
->outdat_endp_flip
; ++i
, ++endp
) {
2440 p_priv
->out_urbs
[i
] = keyspan_setup_urb(serial
, endp
,
2442 p_priv
->out_buffer
[i
], 64,
2443 cback
->outdat_callback
);
2445 /* inack endpoint */
2446 p_priv
->inack_urb
= keyspan_setup_urb(serial
,
2447 d_details
->inack_endpoints
[port_num
],
2449 p_priv
->inack_buffer
, 1,
2450 cback
->inack_callback
);
2451 /* outcont endpoint */
2452 p_priv
->outcont_urb
= keyspan_setup_urb(serial
,
2453 d_details
->outcont_endpoints
[port_num
],
2455 p_priv
->outcont_buffer
, 64,
2456 cback
->outcont_callback
);
2458 usb_set_serial_port_data(port
, p_priv
);
2463 static int keyspan_port_remove(struct usb_serial_port
*port
)
2465 struct keyspan_port_private
*p_priv
;
2468 p_priv
= usb_get_serial_port_data(port
);
2470 stop_urb(p_priv
->inack_urb
);
2471 stop_urb(p_priv
->outcont_urb
);
2472 for (i
= 0; i
< 2; i
++) {
2473 stop_urb(p_priv
->in_urbs
[i
]);
2474 stop_urb(p_priv
->out_urbs
[i
]);
2477 usb_free_urb(p_priv
->inack_urb
);
2478 usb_free_urb(p_priv
->outcont_urb
);
2479 for (i
= 0; i
< 2; i
++) {
2480 usb_free_urb(p_priv
->in_urbs
[i
]);
2481 usb_free_urb(p_priv
->out_urbs
[i
]);
2489 MODULE_AUTHOR(DRIVER_AUTHOR
);
2490 MODULE_DESCRIPTION(DRIVER_DESC
);
2491 MODULE_LICENSE("GPL");
2493 MODULE_FIRMWARE("keyspan/usa28.fw");
2494 MODULE_FIRMWARE("keyspan/usa28x.fw");
2495 MODULE_FIRMWARE("keyspan/usa28xa.fw");
2496 MODULE_FIRMWARE("keyspan/usa28xb.fw");
2497 MODULE_FIRMWARE("keyspan/usa19.fw");
2498 MODULE_FIRMWARE("keyspan/usa19qi.fw");
2499 MODULE_FIRMWARE("keyspan/mpr.fw");
2500 MODULE_FIRMWARE("keyspan/usa19qw.fw");
2501 MODULE_FIRMWARE("keyspan/usa18x.fw");
2502 MODULE_FIRMWARE("keyspan/usa19w.fw");
2503 MODULE_FIRMWARE("keyspan/usa49w.fw");
2504 MODULE_FIRMWARE("keyspan/usa49wlc.fw");