blob: 0879ac7fd2cbfc361767cf6ebd801a46e0c09d41 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 Keyspan USB to Serial Converter driver
3
4 (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
5 (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
6
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.
11
12 See http://blemings.org/hugh/keyspan.html for more information.
13
14 Code in this driver inspired by and in a number of places taken
15 from Brian Warner's original Keyspan-PDA driver.
16
17 This driver has been put together with the support of Innosys, Inc.
18 and Keyspan, Inc the manufacturers of the Keyspan USB-serial products.
19 Thanks Guys :)
20
21 Thanks to Paulus for miscellaneous tidy ups, some largish chunks
22 of much nicer and/or completely new code and (perhaps most uniquely)
23 having the patience to sit down and explain why and where he'd changed
24 stuff.
25
26 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
27 staff in their work on open source projects.
28*/
29
30
31#include <linux/kernel.h>
32#include <linux/jiffies.h>
33#include <linux/errno.h>
34#include <linux/init.h>
35#include <linux/slab.h>
36#include <linux/tty.h>
37#include <linux/tty_driver.h>
38#include <linux/tty_flip.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/firmware.h>
42#include <linux/ihex.h>
43#include <linux/uaccess.h>
44#include <linux/usb.h>
45#include <linux/usb/serial.h>
46#include "keyspan.h"
47
48static bool debug;
49
50/*
51 * Version Information
52 */
53#define DRIVER_VERSION "v1.1.5"
54#define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
55#define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
56
57#define INSTAT_BUFLEN 32
58#define GLOCONT_BUFLEN 64
59#define INDAT49W_BUFLEN 512
60
61 /* Per device and per port private data */
62struct keyspan_serial_private {
63 const struct keyspan_device_details *device_details;
64
65 struct urb *instat_urb;
66 char instat_buf[INSTAT_BUFLEN];
67
68 /* added to support 49wg, where data from all 4 ports comes in
69 on 1 EP and high-speed supported */
70 struct urb *indat_urb;
71 char indat_buf[INDAT49W_BUFLEN];
72
73 /* XXX this one probably will need a lock */
74 struct urb *glocont_urb;
75 char glocont_buf[GLOCONT_BUFLEN];
76 char ctrl_buf[8]; /* for EP0 control message */
77};
78
79struct keyspan_port_private {
80 /* Keep track of which input & output endpoints to use */
81 int in_flip;
82 int out_flip;
83
84 /* Keep duplicate of device details in each port
85 structure as well - simplifies some of the
86 callback functions etc. */
87 const struct keyspan_device_details *device_details;
88
89 /* Input endpoints and buffer for this port */
90 struct urb *in_urbs[2];
91 char in_buffer[2][64];
92 /* Output endpoints and buffer for this port */
93 struct urb *out_urbs[2];
94 char out_buffer[2][64];
95
96 /* Input ack endpoint */
97 struct urb *inack_urb;
98 char inack_buffer[1];
99
100 /* Output control endpoint */
101 struct urb *outcont_urb;
102 char outcont_buffer[64];
103
104 /* Settings for the port */
105 int baud;
106 int old_baud;
107 unsigned int cflag;
108 unsigned int old_cflag;
109 enum {flow_none, flow_cts, flow_xon} flow_control;
110 int rts_state; /* Handshaking pins (outputs) */
111 int dtr_state;
112 int cts_state; /* Handshaking pins (inputs) */
113 int dsr_state;
114 int dcd_state;
115 int ri_state;
116 int break_on;
117
118 unsigned long tx_start_time[2];
119 int resend_cont; /* need to resend control packet */
120};
121
122/* Include Keyspan message headers. All current Keyspan Adapters
123 make use of one of five message formats which are referred
124 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
125 within this driver. */
126#include "keyspan_usa26msg.h"
127#include "keyspan_usa28msg.h"
128#include "keyspan_usa49msg.h"
129#include "keyspan_usa90msg.h"
130#include "keyspan_usa67msg.h"
131
132
133module_usb_serial_driver(keyspan_driver, serial_drivers);
134
135static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
136{
137 struct usb_serial_port *port = tty->driver_data;
138 struct keyspan_port_private *p_priv;
139
140 dbg("%s", __func__);
141
142 p_priv = usb_get_serial_port_data(port);
143
144 if (break_state == -1)
145 p_priv->break_on = 1;
146 else
147 p_priv->break_on = 0;
148
149 keyspan_send_setup(port, 0);
150}
151
152
153static void keyspan_set_termios(struct tty_struct *tty,
154 struct usb_serial_port *port, struct ktermios *old_termios)
155{
156 int baud_rate, device_port;
157 struct keyspan_port_private *p_priv;
158 const struct keyspan_device_details *d_details;
159 unsigned int cflag;
160
161 dbg("%s", __func__);
162
163 p_priv = usb_get_serial_port_data(port);
164 d_details = p_priv->device_details;
165 cflag = tty->termios->c_cflag;
166 device_port = port->number - port->serial->minor;
167
168 /* Baud rate calculation takes baud rate as an integer
169 so other rates can be generated if desired. */
170 baud_rate = tty_get_baud_rate(tty);
171 /* If no match or invalid, don't change */
172 if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
173 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
174 /* FIXME - more to do here to ensure rate changes cleanly */
175 /* FIXME - calcuate exact rate from divisor ? */
176 p_priv->baud = baud_rate;
177 } else
178 baud_rate = tty_termios_baud_rate(old_termios);
179
180 tty_encode_baud_rate(tty, baud_rate, baud_rate);
181 /* set CTS/RTS handshake etc. */
182 p_priv->cflag = cflag;
183 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
184
185 /* Mark/Space not supported */
186 tty->termios->c_cflag &= ~CMSPAR;
187
188 keyspan_send_setup(port, 0);
189}
190
191static int keyspan_tiocmget(struct tty_struct *tty)
192{
193 struct usb_serial_port *port = tty->driver_data;
194 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
195 unsigned int value;
196
197 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
198 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
199 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
200 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
201 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
202 ((p_priv->ri_state) ? TIOCM_RNG : 0);
203
204 return value;
205}
206
207static int keyspan_tiocmset(struct tty_struct *tty,
208 unsigned int set, unsigned int clear)
209{
210 struct usb_serial_port *port = tty->driver_data;
211 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
212
213 if (set & TIOCM_RTS)
214 p_priv->rts_state = 1;
215 if (set & TIOCM_DTR)
216 p_priv->dtr_state = 1;
217 if (clear & TIOCM_RTS)
218 p_priv->rts_state = 0;
219 if (clear & TIOCM_DTR)
220 p_priv->dtr_state = 0;
221 keyspan_send_setup(port, 0);
222 return 0;
223}
224
225/* Write function is similar for the four protocols used
226 with only a minor change for usa90 (usa19hs) required */
227static int keyspan_write(struct tty_struct *tty,
228 struct usb_serial_port *port, const unsigned char *buf, int count)
229{
230 struct keyspan_port_private *p_priv;
231 const struct keyspan_device_details *d_details;
232 int flip;
233 int left, todo;
234 struct urb *this_urb;
235 int err, maxDataLen, dataOffset;
236
237 p_priv = usb_get_serial_port_data(port);
238 d_details = p_priv->device_details;
239
240 if (d_details->msg_format == msg_usa90) {
241 maxDataLen = 64;
242 dataOffset = 0;
243 } else {
244 maxDataLen = 63;
245 dataOffset = 1;
246 }
247
248 dbg("%s - for port %d (%d chars), flip=%d",
249 __func__, port->number, count, p_priv->out_flip);
250
251 for (left = count; left > 0; left -= todo) {
252 todo = left;
253 if (todo > maxDataLen)
254 todo = maxDataLen;
255
256 flip = p_priv->out_flip;
257
258 /* Check we have a valid urb/endpoint before we use it... */
259 this_urb = p_priv->out_urbs[flip];
260 if (this_urb == NULL) {
261 /* no bulk out, so return 0 bytes written */
262 dbg("%s - no output urb :(", __func__);
263 return count;
264 }
265
266 dbg("%s - endpoint %d flip %d",
267 __func__, usb_pipeendpoint(this_urb->pipe), flip);
268
269 if (this_urb->status == -EINPROGRESS) {
270 if (time_before(jiffies,
271 p_priv->tx_start_time[flip] + 10 * HZ))
272 break;
273 usb_unlink_urb(this_urb);
274 break;
275 }
276
277 /* First byte in buffer is "last flag" (except for usa19hx)
278 - unused so for now so set to zero */
279 ((char *)this_urb->transfer_buffer)[0] = 0;
280
281 memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
282 buf += todo;
283
284 /* send the data out the bulk port */
285 this_urb->transfer_buffer_length = todo + dataOffset;
286
287 err = usb_submit_urb(this_urb, GFP_ATOMIC);
288 if (err != 0)
289 dbg("usb_submit_urb(write bulk) failed (%d)", err);
290 p_priv->tx_start_time[flip] = jiffies;
291
292 /* Flip for next time if usa26 or usa28 interface
293 (not used on usa49) */
294 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
295 }
296
297 return count - left;
298}
299
300static void usa26_indat_callback(struct urb *urb)
301{
302 int i, err;
303 int endpoint;
304 struct usb_serial_port *port;
305 struct tty_struct *tty;
306 unsigned char *data = urb->transfer_buffer;
307 int status = urb->status;
308
309 dbg("%s", __func__);
310
311 endpoint = usb_pipeendpoint(urb->pipe);
312
313 if (status) {
314 dbg("%s - nonzero status: %x on endpoint %d.",
315 __func__, status, endpoint);
316 return;
317 }
318
319 port = urb->context;
320 tty = tty_port_tty_get(&port->port);
321 if (tty && urb->actual_length) {
322 /* 0x80 bit is error flag */
323 if ((data[0] & 0x80) == 0) {
324 /* no errors on individual bytes, only
325 possible overrun err */
326 if (data[0] & RXERROR_OVERRUN) {
327 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
328 }
329 for (i = 1; i < urb->actual_length ; ++i)
330 tty_insert_flip_char(tty, data[i], TTY_NORMAL);
331 } else {
332 /* some bytes had errors, every byte has status */
333 dbg("%s - RX error!!!!", __func__);
334 for (i = 0; i + 1 < urb->actual_length; i += 2) {
335 int stat = data[i];
336 int flag = TTY_NORMAL;
337
338 if (stat & RXERROR_OVERRUN) {
339 tty_insert_flip_char(tty, 0,
340 TTY_OVERRUN);
341 }
342 /* XXX should handle break (0x10) */
343 if (stat & RXERROR_PARITY)
344 flag = TTY_PARITY;
345 else if (stat & RXERROR_FRAMING)
346 flag = TTY_FRAME;
347
348 tty_insert_flip_char(tty, data[i+1], flag);
349 }
350 }
351 tty_flip_buffer_push(tty);
352 }
353 tty_kref_put(tty);
354
355 /* Resubmit urb so we continue receiving */
356 err = usb_submit_urb(urb, GFP_ATOMIC);
357 if (err != 0)
358 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
359}
360
361/* Outdat handling is common for all devices */
362static void usa2x_outdat_callback(struct urb *urb)
363{
364 struct usb_serial_port *port;
365 struct keyspan_port_private *p_priv;
366
367 port = urb->context;
368 p_priv = usb_get_serial_port_data(port);
369 dbg("%s - urb %d", __func__, urb == p_priv->out_urbs[1]);
370
371 usb_serial_port_softint(port);
372}
373
374static void usa26_inack_callback(struct urb *urb)
375{
376 dbg("%s", __func__);
377
378}
379
380static void usa26_outcont_callback(struct urb *urb)
381{
382 struct usb_serial_port *port;
383 struct keyspan_port_private *p_priv;
384
385 port = urb->context;
386 p_priv = usb_get_serial_port_data(port);
387
388 if (p_priv->resend_cont) {
389 dbg("%s - sending setup", __func__);
390 keyspan_usa26_send_setup(port->serial, port,
391 p_priv->resend_cont - 1);
392 }
393}
394
395static void usa26_instat_callback(struct urb *urb)
396{
397 unsigned char *data = urb->transfer_buffer;
398 struct keyspan_usa26_portStatusMessage *msg;
399 struct usb_serial *serial;
400 struct usb_serial_port *port;
401 struct keyspan_port_private *p_priv;
402 struct tty_struct *tty;
403 int old_dcd_state, err;
404 int status = urb->status;
405
406 serial = urb->context;
407
408 if (status) {
409 dbg("%s - nonzero status: %x", __func__, status);
410 return;
411 }
412 if (urb->actual_length != 9) {
413 dbg("%s - %d byte report??", __func__, urb->actual_length);
414 goto exit;
415 }
416
417 msg = (struct keyspan_usa26_portStatusMessage *)data;
418
419#if 0
420 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
421 __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
422 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
423#endif
424
425 /* Now do something useful with the data */
426
427
428 /* Check port number from message and retrieve private data */
429 if (msg->port >= serial->num_ports) {
430 dbg("%s - Unexpected port number %d", __func__, msg->port);
431 goto exit;
432 }
433 port = serial->port[msg->port];
434 p_priv = usb_get_serial_port_data(port);
435
436 /* Update handshaking pin state information */
437 old_dcd_state = p_priv->dcd_state;
438 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
439 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
440 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
441 p_priv->ri_state = ((msg->ri) ? 1 : 0);
442
443 if (old_dcd_state != p_priv->dcd_state) {
444 tty = tty_port_tty_get(&port->port);
445 if (tty && !C_CLOCAL(tty))
446 tty_hangup(tty);
447 tty_kref_put(tty);
448 }
449
450 /* Resubmit urb so we continue receiving */
451 err = usb_submit_urb(urb, GFP_ATOMIC);
452 if (err != 0)
453 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
454exit: ;
455}
456
457static void usa26_glocont_callback(struct urb *urb)
458{
459 dbg("%s", __func__);
460}
461
462
463static void usa28_indat_callback(struct urb *urb)
464{
465 int err;
466 struct usb_serial_port *port;
467 struct tty_struct *tty;
468 unsigned char *data;
469 struct keyspan_port_private *p_priv;
470 int status = urb->status;
471
472 dbg("%s", __func__);
473
474 port = urb->context;
475 p_priv = usb_get_serial_port_data(port);
476 data = urb->transfer_buffer;
477
478 if (urb != p_priv->in_urbs[p_priv->in_flip])
479 return;
480
481 do {
482 if (status) {
483 dbg("%s - nonzero status: %x on endpoint %d.",
484 __func__, status, usb_pipeendpoint(urb->pipe));
485 return;
486 }
487
488 port = urb->context;
489 p_priv = usb_get_serial_port_data(port);
490 data = urb->transfer_buffer;
491
492 tty =tty_port_tty_get(&port->port);
493 if (tty && urb->actual_length) {
494 tty_insert_flip_string(tty, data, urb->actual_length);
495 tty_flip_buffer_push(tty);
496 }
497 tty_kref_put(tty);
498
499 /* Resubmit urb so we continue receiving */
500 err = usb_submit_urb(urb, GFP_ATOMIC);
501 if (err != 0)
502 dbg("%s - resubmit read urb failed. (%d)",
503 __func__, err);
504 p_priv->in_flip ^= 1;
505
506 urb = p_priv->in_urbs[p_priv->in_flip];
507 } while (urb->status != -EINPROGRESS);
508}
509
510static void usa28_inack_callback(struct urb *urb)
511{
512 dbg("%s", __func__);
513}
514
515static void usa28_outcont_callback(struct urb *urb)
516{
517 struct usb_serial_port *port;
518 struct keyspan_port_private *p_priv;
519
520 port = urb->context;
521 p_priv = usb_get_serial_port_data(port);
522
523 if (p_priv->resend_cont) {
524 dbg("%s - sending setup", __func__);
525 keyspan_usa28_send_setup(port->serial, port,
526 p_priv->resend_cont - 1);
527 }
528}
529
530static void usa28_instat_callback(struct urb *urb)
531{
532 int err;
533 unsigned char *data = urb->transfer_buffer;
534 struct keyspan_usa28_portStatusMessage *msg;
535 struct usb_serial *serial;
536 struct usb_serial_port *port;
537 struct keyspan_port_private *p_priv;
538 struct tty_struct *tty;
539 int old_dcd_state;
540 int status = urb->status;
541
542 serial = urb->context;
543
544 if (status) {
545 dbg("%s - nonzero status: %x", __func__, status);
546 return;
547 }
548
549 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
550 dbg("%s - bad length %d", __func__, urb->actual_length);
551 goto exit;
552 }
553
554 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
555 data[0], data[1], data[2], data[3], data[4], data[5],
556 data[6], data[7], data[8], data[9], data[10], data[11]);*/
557
558 /* Now do something useful with the data */
559 msg = (struct keyspan_usa28_portStatusMessage *)data;
560
561 /* Check port number from message and retrieve private data */
562 if (msg->port >= serial->num_ports) {
563 dbg("%s - Unexpected port number %d", __func__, msg->port);
564 goto exit;
565 }
566 port = serial->port[msg->port];
567 p_priv = usb_get_serial_port_data(port);
568
569 /* Update handshaking pin state information */
570 old_dcd_state = p_priv->dcd_state;
571 p_priv->cts_state = ((msg->cts) ? 1 : 0);
572 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
573 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
574 p_priv->ri_state = ((msg->ri) ? 1 : 0);
575
576 if( old_dcd_state != p_priv->dcd_state && old_dcd_state) {
577 tty = tty_port_tty_get(&port->port);
578 if (tty && !C_CLOCAL(tty))
579 tty_hangup(tty);
580 tty_kref_put(tty);
581 }
582
583 /* Resubmit urb so we continue receiving */
584 err = usb_submit_urb(urb, GFP_ATOMIC);
585 if (err != 0)
586 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
587exit: ;
588}
589
590static void usa28_glocont_callback(struct urb *urb)
591{
592 dbg("%s", __func__);
593}
594
595
596static void usa49_glocont_callback(struct urb *urb)
597{
598 struct usb_serial *serial;
599 struct usb_serial_port *port;
600 struct keyspan_port_private *p_priv;
601 int i;
602
603 dbg("%s", __func__);
604
605 serial = urb->context;
606 for (i = 0; i < serial->num_ports; ++i) {
607 port = serial->port[i];
608 p_priv = usb_get_serial_port_data(port);
609
610 if (p_priv->resend_cont) {
611 dbg("%s - sending setup", __func__);
612 keyspan_usa49_send_setup(serial, port,
613 p_priv->resend_cont - 1);
614 break;
615 }
616 }
617}
618
619 /* This is actually called glostat in the Keyspan
620 doco */
621static void usa49_instat_callback(struct urb *urb)
622{
623 int err;
624 unsigned char *data = urb->transfer_buffer;
625 struct keyspan_usa49_portStatusMessage *msg;
626 struct usb_serial *serial;
627 struct usb_serial_port *port;
628 struct keyspan_port_private *p_priv;
629 int old_dcd_state;
630 int status = urb->status;
631
632 dbg("%s", __func__);
633
634 serial = urb->context;
635
636 if (status) {
637 dbg("%s - nonzero status: %x", __func__, status);
638 return;
639 }
640
641 if (urb->actual_length !=
642 sizeof(struct keyspan_usa49_portStatusMessage)) {
643 dbg("%s - bad length %d", __func__, urb->actual_length);
644 goto exit;
645 }
646
647 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
648 data[0], data[1], data[2], data[3], data[4], data[5],
649 data[6], data[7], data[8], data[9], data[10]);*/
650
651 /* Now do something useful with the data */
652 msg = (struct keyspan_usa49_portStatusMessage *)data;
653
654 /* Check port number from message and retrieve private data */
655 if (msg->portNumber >= serial->num_ports) {
656 dbg("%s - Unexpected port number %d",
657 __func__, msg->portNumber);
658 goto exit;
659 }
660 port = serial->port[msg->portNumber];
661 p_priv = usb_get_serial_port_data(port);
662
663 /* Update handshaking pin state information */
664 old_dcd_state = p_priv->dcd_state;
665 p_priv->cts_state = ((msg->cts) ? 1 : 0);
666 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
667 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
668 p_priv->ri_state = ((msg->ri) ? 1 : 0);
669
670 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
671 struct tty_struct *tty = tty_port_tty_get(&port->port);
672 if (tty && !C_CLOCAL(tty))
673 tty_hangup(tty);
674 tty_kref_put(tty);
675 }
676
677 /* Resubmit urb so we continue receiving */
678 err = usb_submit_urb(urb, GFP_ATOMIC);
679 if (err != 0)
680 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
681exit: ;
682}
683
684static void usa49_inack_callback(struct urb *urb)
685{
686 dbg("%s", __func__);
687}
688
689static void usa49_indat_callback(struct urb *urb)
690{
691 int i, err;
692 int endpoint;
693 struct usb_serial_port *port;
694 struct tty_struct *tty;
695 unsigned char *data = urb->transfer_buffer;
696 int status = urb->status;
697
698 dbg("%s", __func__);
699
700 endpoint = usb_pipeendpoint(urb->pipe);
701
702 if (status) {
703 dbg("%s - nonzero status: %x on endpoint %d.", __func__,
704 status, endpoint);
705 return;
706 }
707
708 port = urb->context;
709 tty = tty_port_tty_get(&port->port);
710 if (tty && urb->actual_length) {
711 /* 0x80 bit is error flag */
712 if ((data[0] & 0x80) == 0) {
713 /* no error on any byte */
714 tty_insert_flip_string(tty, data + 1,
715 urb->actual_length - 1);
716 } else {
717 /* some bytes had errors, every byte has status */
718 for (i = 0; i + 1 < urb->actual_length; i += 2) {
719 int stat = data[i];
720 int flag = TTY_NORMAL;
721
722 if (stat & RXERROR_OVERRUN) {
723 tty_insert_flip_char(tty, 0,
724 TTY_OVERRUN);
725 }
726 /* XXX should handle break (0x10) */
727 if (stat & RXERROR_PARITY)
728 flag = TTY_PARITY;
729 else if (stat & RXERROR_FRAMING)
730 flag = TTY_FRAME;
731
732 tty_insert_flip_char(tty, data[i+1], flag);
733 }
734 }
735 tty_flip_buffer_push(tty);
736 }
737 tty_kref_put(tty);
738
739 /* Resubmit urb so we continue receiving */
740 err = usb_submit_urb(urb, GFP_ATOMIC);
741 if (err != 0)
742 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
743}
744
745static void usa49wg_indat_callback(struct urb *urb)
746{
747 int i, len, x, err;
748 struct usb_serial *serial;
749 struct usb_serial_port *port;
750 struct tty_struct *tty;
751 unsigned char *data = urb->transfer_buffer;
752 int status = urb->status;
753
754 dbg("%s", __func__);
755
756 serial = urb->context;
757
758 if (status) {
759 dbg("%s - nonzero status: %x", __func__, status);
760 return;
761 }
762
763 /* inbound data is in the form P#, len, status, data */
764 i = 0;
765 len = 0;
766
767 if (urb->actual_length) {
768 while (i < urb->actual_length) {
769
770 /* Check port number from message*/
771 if (data[i] >= serial->num_ports) {
772 dbg("%s - Unexpected port number %d",
773 __func__, data[i]);
774 return;
775 }
776 port = serial->port[data[i++]];
777 tty = tty_port_tty_get(&port->port);
778 len = data[i++];
779
780 /* 0x80 bit is error flag */
781 if ((data[i] & 0x80) == 0) {
782 /* no error on any byte */
783 i++;
784 for (x = 1; x < len ; ++x)
785 tty_insert_flip_char(tty, data[i++], 0);
786 } else {
787 /*
788 * some bytes had errors, every byte has status
789 */
790 for (x = 0; x + 1 < len; x += 2) {
791 int stat = data[i];
792 int flag = TTY_NORMAL;
793
794 if (stat & RXERROR_OVERRUN) {
795 tty_insert_flip_char(tty, 0,
796 TTY_OVERRUN);
797 }
798 /* XXX should handle break (0x10) */
799 if (stat & RXERROR_PARITY)
800 flag = TTY_PARITY;
801 else if (stat & RXERROR_FRAMING)
802 flag = TTY_FRAME;
803
804 tty_insert_flip_char(tty,
805 data[i+1], flag);
806 i += 2;
807 }
808 }
809 tty_flip_buffer_push(tty);
810 tty_kref_put(tty);
811 }
812 }
813
814 /* Resubmit urb so we continue receiving */
815 err = usb_submit_urb(urb, GFP_ATOMIC);
816 if (err != 0)
817 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
818}
819
820/* not used, usa-49 doesn't have per-port control endpoints */
821static void usa49_outcont_callback(struct urb *urb)
822{
823 dbg("%s", __func__);
824}
825
826static void usa90_indat_callback(struct urb *urb)
827{
828 int i, err;
829 int endpoint;
830 struct usb_serial_port *port;
831 struct keyspan_port_private *p_priv;
832 struct tty_struct *tty;
833 unsigned char *data = urb->transfer_buffer;
834 int status = urb->status;
835
836 dbg("%s", __func__);
837
838 endpoint = usb_pipeendpoint(urb->pipe);
839
840 if (status) {
841 dbg("%s - nonzero status: %x on endpoint %d.",
842 __func__, status, endpoint);
843 return;
844 }
845
846 port = urb->context;
847 p_priv = usb_get_serial_port_data(port);
848
849 if (urb->actual_length) {
850 tty = tty_port_tty_get(&port->port);
851 /* if current mode is DMA, looks like usa28 format
852 otherwise looks like usa26 data format */
853
854 if (p_priv->baud > 57600)
855 tty_insert_flip_string(tty, data, urb->actual_length);
856 else {
857 /* 0x80 bit is error flag */
858 if ((data[0] & 0x80) == 0) {
859 /* no errors on individual bytes, only
860 possible overrun err*/
861 if (data[0] & RXERROR_OVERRUN) {
862 tty_insert_flip_char(tty, 0,
863 TTY_OVERRUN);
864 }
865 for (i = 1; i < urb->actual_length ; ++i)
866 tty_insert_flip_char(tty, data[i],
867 TTY_NORMAL);
868 } else {
869 /* some bytes had errors, every byte has status */
870 dbg("%s - RX error!!!!", __func__);
871 for (i = 0; i + 1 < urb->actual_length; i += 2) {
872 int stat = data[i];
873 int flag = TTY_NORMAL;
874
875 if (stat & RXERROR_OVERRUN) {
876 tty_insert_flip_char(
877 tty, 0,
878 TTY_OVERRUN);
879 }
880
881 /* XXX should handle break (0x10) */
882 if (stat & RXERROR_PARITY)
883 flag = TTY_PARITY;
884 else if (stat & RXERROR_FRAMING)
885 flag = TTY_FRAME;
886
887 tty_insert_flip_char(tty, data[i+1],
888 flag);
889 }
890 }
891 }
892 tty_flip_buffer_push(tty);
893 tty_kref_put(tty);
894 }
895
896 /* Resubmit urb so we continue receiving */
897 err = usb_submit_urb(urb, GFP_ATOMIC);
898 if (err != 0)
899 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
900}
901
902
903static void usa90_instat_callback(struct urb *urb)
904{
905 unsigned char *data = urb->transfer_buffer;
906 struct keyspan_usa90_portStatusMessage *msg;
907 struct usb_serial *serial;
908 struct usb_serial_port *port;
909 struct keyspan_port_private *p_priv;
910 struct tty_struct *tty;
911 int old_dcd_state, err;
912 int status = urb->status;
913
914 serial = urb->context;
915
916 if (status) {
917 dbg("%s - nonzero status: %x", __func__, status);
918 return;
919 }
920 if (urb->actual_length < 14) {
921 dbg("%s - %d byte report??", __func__, urb->actual_length);
922 goto exit;
923 }
924
925 msg = (struct keyspan_usa90_portStatusMessage *)data;
926
927 /* Now do something useful with the data */
928
929 port = serial->port[0];
930 p_priv = usb_get_serial_port_data(port);
931
932 /* Update handshaking pin state information */
933 old_dcd_state = p_priv->dcd_state;
934 p_priv->cts_state = ((msg->cts) ? 1 : 0);
935 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
936 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
937 p_priv->ri_state = ((msg->ri) ? 1 : 0);
938
939 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
940 tty = tty_port_tty_get(&port->port);
941 if (tty && !C_CLOCAL(tty))
942 tty_hangup(tty);
943 tty_kref_put(tty);
944 }
945
946 /* Resubmit urb so we continue receiving */
947 err = usb_submit_urb(urb, GFP_ATOMIC);
948 if (err != 0)
949 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
950exit:
951 ;
952}
953
954static void usa90_outcont_callback(struct urb *urb)
955{
956 struct usb_serial_port *port;
957 struct keyspan_port_private *p_priv;
958
959 port = urb->context;
960 p_priv = usb_get_serial_port_data(port);
961
962 if (p_priv->resend_cont) {
963 dbg("%s - sending setup", __func__);
964 keyspan_usa90_send_setup(port->serial, port,
965 p_priv->resend_cont - 1);
966 }
967}
968
969/* Status messages from the 28xg */
970static void usa67_instat_callback(struct urb *urb)
971{
972 int err;
973 unsigned char *data = urb->transfer_buffer;
974 struct keyspan_usa67_portStatusMessage *msg;
975 struct usb_serial *serial;
976 struct usb_serial_port *port;
977 struct keyspan_port_private *p_priv;
978 int old_dcd_state;
979 int status = urb->status;
980
981 dbg("%s", __func__);
982
983 serial = urb->context;
984
985 if (status) {
986 dbg("%s - nonzero status: %x", __func__, status);
987 return;
988 }
989
990 if (urb->actual_length !=
991 sizeof(struct keyspan_usa67_portStatusMessage)) {
992 dbg("%s - bad length %d", __func__, urb->actual_length);
993 return;
994 }
995
996
997 /* Now do something useful with the data */
998 msg = (struct keyspan_usa67_portStatusMessage *)data;
999
1000 /* Check port number from message and retrieve private data */
1001 if (msg->port >= serial->num_ports) {
1002 dbg("%s - Unexpected port number %d", __func__, msg->port);
1003 return;
1004 }
1005
1006 port = serial->port[msg->port];
1007 p_priv = usb_get_serial_port_data(port);
1008
1009 /* Update handshaking pin state information */
1010 old_dcd_state = p_priv->dcd_state;
1011 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1012 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1013
1014 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
1015 struct tty_struct *tty = tty_port_tty_get(&port->port);
1016 if (tty && !C_CLOCAL(tty))
1017 tty_hangup(tty);
1018 tty_kref_put(tty);
1019 }
1020
1021 /* Resubmit urb so we continue receiving */
1022 err = usb_submit_urb(urb, GFP_ATOMIC);
1023 if (err != 0)
1024 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
1025}
1026
1027static void usa67_glocont_callback(struct urb *urb)
1028{
1029 struct usb_serial *serial;
1030 struct usb_serial_port *port;
1031 struct keyspan_port_private *p_priv;
1032 int i;
1033
1034 dbg("%s", __func__);
1035
1036 serial = urb->context;
1037 for (i = 0; i < serial->num_ports; ++i) {
1038 port = serial->port[i];
1039 p_priv = usb_get_serial_port_data(port);
1040
1041 if (p_priv->resend_cont) {
1042 dbg("%s - sending setup", __func__);
1043 keyspan_usa67_send_setup(serial, port,
1044 p_priv->resend_cont - 1);
1045 break;
1046 }
1047 }
1048}
1049
1050static int keyspan_write_room(struct tty_struct *tty)
1051{
1052 struct usb_serial_port *port = tty->driver_data;
1053 struct keyspan_port_private *p_priv;
1054 const struct keyspan_device_details *d_details;
1055 int flip;
1056 int data_len;
1057 struct urb *this_urb;
1058
1059 dbg("%s", __func__);
1060 p_priv = usb_get_serial_port_data(port);
1061 d_details = p_priv->device_details;
1062
1063 /* FIXME: locking */
1064 if (d_details->msg_format == msg_usa90)
1065 data_len = 64;
1066 else
1067 data_len = 63;
1068
1069 flip = p_priv->out_flip;
1070
1071 /* Check both endpoints to see if any are available. */
1072 this_urb = p_priv->out_urbs[flip];
1073 if (this_urb != NULL) {
1074 if (this_urb->status != -EINPROGRESS)
1075 return data_len;
1076 flip = (flip + 1) & d_details->outdat_endp_flip;
1077 this_urb = p_priv->out_urbs[flip];
1078 if (this_urb != NULL) {
1079 if (this_urb->status != -EINPROGRESS)
1080 return data_len;
1081 }
1082 }
1083 return 0;
1084}
1085
1086
1087static int keyspan_open(struct tty_struct *tty, struct usb_serial_port *port)
1088{
1089 struct keyspan_port_private *p_priv;
1090 struct keyspan_serial_private *s_priv;
1091 struct usb_serial *serial = port->serial;
1092 const struct keyspan_device_details *d_details;
1093 int i, err;
1094 int baud_rate, device_port;
1095 struct urb *urb;
1096 unsigned int cflag = 0;
1097
1098 s_priv = usb_get_serial_data(serial);
1099 p_priv = usb_get_serial_port_data(port);
1100 d_details = p_priv->device_details;
1101
1102 dbg("%s - port%d.", __func__, port->number);
1103
1104 /* Set some sane defaults */
1105 p_priv->rts_state = 1;
1106 p_priv->dtr_state = 1;
1107 p_priv->baud = 9600;
1108
1109 /* force baud and lcr to be set on open */
1110 p_priv->old_baud = 0;
1111 p_priv->old_cflag = 0;
1112
1113 p_priv->out_flip = 0;
1114 p_priv->in_flip = 0;
1115
1116 /* Reset low level data toggle and start reading from endpoints */
1117 for (i = 0; i < 2; i++) {
1118 urb = p_priv->in_urbs[i];
1119 if (urb == NULL)
1120 continue;
1121
1122 /* make sure endpoint data toggle is synchronized
1123 with the device */
1124 usb_clear_halt(urb->dev, urb->pipe);
1125 err = usb_submit_urb(urb, GFP_KERNEL);
1126 if (err != 0)
1127 dbg("%s - submit urb %d failed (%d)",
1128 __func__, i, err);
1129 }
1130
1131 /* Reset low level data toggle on out endpoints */
1132 for (i = 0; i < 2; i++) {
1133 urb = p_priv->out_urbs[i];
1134 if (urb == NULL)
1135 continue;
1136 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1137 usb_pipeout(urb->pipe), 0); */
1138 }
1139
1140 /* get the terminal config for the setup message now so we don't
1141 * need to send 2 of them */
1142
1143 device_port = port->number - port->serial->minor;
1144 if (tty) {
1145 cflag = tty->termios->c_cflag;
1146 /* Baud rate calculation takes baud rate as an integer
1147 so other rates can be generated if desired. */
1148 baud_rate = tty_get_baud_rate(tty);
1149 /* If no match or invalid, leave as default */
1150 if (baud_rate >= 0
1151 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1152 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1153 p_priv->baud = baud_rate;
1154 }
1155 }
1156 /* set CTS/RTS handshake etc. */
1157 p_priv->cflag = cflag;
1158 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1159
1160 keyspan_send_setup(port, 1);
1161 /* mdelay(100); */
1162 /* keyspan_set_termios(port, NULL); */
1163
1164 return 0;
1165}
1166
1167static inline void stop_urb(struct urb *urb)
1168{
1169 if (urb && urb->status == -EINPROGRESS)
1170 usb_kill_urb(urb);
1171}
1172
1173static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
1174{
1175 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
1176
1177 p_priv->rts_state = on;
1178 p_priv->dtr_state = on;
1179 keyspan_send_setup(port, 0);
1180}
1181
1182static void keyspan_close(struct usb_serial_port *port)
1183{
1184 int i;
1185 struct usb_serial *serial = port->serial;
1186 struct keyspan_serial_private *s_priv;
1187 struct keyspan_port_private *p_priv;
1188
1189 dbg("%s", __func__);
1190 s_priv = usb_get_serial_data(serial);
1191 p_priv = usb_get_serial_port_data(port);
1192
1193 p_priv->rts_state = 0;
1194 p_priv->dtr_state = 0;
1195
1196 if (serial->dev) {
1197 keyspan_send_setup(port, 2);
1198 /* pilot-xfer seems to work best with this delay */
1199 mdelay(100);
1200 /* keyspan_set_termios(port, NULL); */
1201 }
1202
1203 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
1204 dbg("%s - urb in progress", __func__);
1205 }*/
1206
1207 p_priv->out_flip = 0;
1208 p_priv->in_flip = 0;
1209
1210 if (serial->dev) {
1211 /* Stop reading/writing urbs */
1212 stop_urb(p_priv->inack_urb);
1213 /* stop_urb(p_priv->outcont_urb); */
1214 for (i = 0; i < 2; i++) {
1215 stop_urb(p_priv->in_urbs[i]);
1216 stop_urb(p_priv->out_urbs[i]);
1217 }
1218 }
1219}
1220
1221/* download the firmware to a pre-renumeration device */
1222static int keyspan_fake_startup(struct usb_serial *serial)
1223{
1224 int response;
1225 const struct ihex_binrec *record;
1226 char *fw_name;
1227 const struct firmware *fw;
1228
1229 dbg("Keyspan startup version %04x product %04x",
1230 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1231 le16_to_cpu(serial->dev->descriptor.idProduct));
1232
1233 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
1234 != 0x8000) {
1235 dbg("Firmware already loaded. Quitting.");
1236 return 1;
1237 }
1238
1239 /* Select firmware image on the basis of idProduct */
1240 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1241 case keyspan_usa28_pre_product_id:
1242 fw_name = "keyspan/usa28.fw";
1243 break;
1244
1245 case keyspan_usa28x_pre_product_id:
1246 fw_name = "keyspan/usa28x.fw";
1247 break;
1248
1249 case keyspan_usa28xa_pre_product_id:
1250 fw_name = "keyspan/usa28xa.fw";
1251 break;
1252
1253 case keyspan_usa28xb_pre_product_id:
1254 fw_name = "keyspan/usa28xb.fw";
1255 break;
1256
1257 case keyspan_usa19_pre_product_id:
1258 fw_name = "keyspan/usa19.fw";
1259 break;
1260
1261 case keyspan_usa19qi_pre_product_id:
1262 fw_name = "keyspan/usa19qi.fw";
1263 break;
1264
1265 case keyspan_mpr_pre_product_id:
1266 fw_name = "keyspan/mpr.fw";
1267 break;
1268
1269 case keyspan_usa19qw_pre_product_id:
1270 fw_name = "keyspan/usa19qw.fw";
1271 break;
1272
1273 case keyspan_usa18x_pre_product_id:
1274 fw_name = "keyspan/usa18x.fw";
1275 break;
1276
1277 case keyspan_usa19w_pre_product_id:
1278 fw_name = "keyspan/usa19w.fw";
1279 break;
1280
1281 case keyspan_usa49w_pre_product_id:
1282 fw_name = "keyspan/usa49w.fw";
1283 break;
1284
1285 case keyspan_usa49wlc_pre_product_id:
1286 fw_name = "keyspan/usa49wlc.fw";
1287 break;
1288
1289 default:
1290 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1291 le16_to_cpu(serial->dev->descriptor.idProduct));
1292 return 1;
1293 }
1294
1295 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
1296 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1297 return(1);
1298 }
1299
1300 dbg("Uploading Keyspan %s firmware.", fw_name);
1301
1302 /* download the firmware image */
1303 response = ezusb_set_reset(serial, 1);
1304
1305 record = (const struct ihex_binrec *)fw->data;
1306
1307 while (record) {
1308 response = ezusb_writememory(serial, be32_to_cpu(record->addr),
1309 (unsigned char *)record->data,
1310 be16_to_cpu(record->len), 0xa0);
1311 if (response < 0) {
1312 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
1313 response, be32_to_cpu(record->addr),
1314 record->data, be16_to_cpu(record->len));
1315 break;
1316 }
1317 record = ihex_next_binrec(record);
1318 }
1319 release_firmware(fw);
1320 /* bring device out of reset. Renumeration will occur in a
1321 moment and the new device will bind to the real driver */
1322 response = ezusb_set_reset(serial, 0);
1323
1324 /* we don't want this device to have a driver assigned to it. */
1325 return 1;
1326}
1327
1328/* Helper functions used by keyspan_setup_urbs */
1329static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1330 int endpoint)
1331{
1332 struct usb_host_interface *iface_desc;
1333 struct usb_endpoint_descriptor *ep;
1334 int i;
1335
1336 iface_desc = serial->interface->cur_altsetting;
1337 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1338 ep = &iface_desc->endpoint[i].desc;
1339 if (ep->bEndpointAddress == endpoint)
1340 return ep;
1341 }
1342 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1343 "endpoint %x\n", endpoint);
1344 return NULL;
1345}
1346
1347static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
1348 int dir, void *ctx, char *buf, int len,
1349 void (*callback)(struct urb *))
1350{
1351 struct urb *urb;
1352 struct usb_endpoint_descriptor const *ep_desc;
1353 char const *ep_type_name;
1354
1355 if (endpoint == -1)
1356 return NULL; /* endpoint not needed */
1357
1358 dbg("%s - alloc for endpoint %d.", __func__, endpoint);
1359 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1360 if (urb == NULL) {
1361 dbg("%s - alloc for endpoint %d failed.", __func__, endpoint);
1362 return NULL;
1363 }
1364
1365 if (endpoint == 0) {
1366 /* control EP filled in when used */
1367 return urb;
1368 }
1369
1370 ep_desc = find_ep(serial, endpoint);
1371 if (!ep_desc) {
1372 /* leak the urb, something's wrong and the callers don't care */
1373 return urb;
1374 }
1375 if (usb_endpoint_xfer_int(ep_desc)) {
1376 ep_type_name = "INT";
1377 usb_fill_int_urb(urb, serial->dev,
1378 usb_sndintpipe(serial->dev, endpoint) | dir,
1379 buf, len, callback, ctx,
1380 ep_desc->bInterval);
1381 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1382 ep_type_name = "BULK";
1383 usb_fill_bulk_urb(urb, serial->dev,
1384 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1385 buf, len, callback, ctx);
1386 } else {
1387 dev_warn(&serial->interface->dev,
1388 "unsupported endpoint type %x\n",
1389 usb_endpoint_type(ep_desc));
1390 usb_free_urb(urb);
1391 return NULL;
1392 }
1393
1394 dbg("%s - using urb %p for %s endpoint %x",
1395 __func__, urb, ep_type_name, endpoint);
1396 return urb;
1397}
1398
1399static struct callbacks {
1400 void (*instat_callback)(struct urb *);
1401 void (*glocont_callback)(struct urb *);
1402 void (*indat_callback)(struct urb *);
1403 void (*outdat_callback)(struct urb *);
1404 void (*inack_callback)(struct urb *);
1405 void (*outcont_callback)(struct urb *);
1406} keyspan_callbacks[] = {
1407 {
1408 /* msg_usa26 callbacks */
1409 .instat_callback = usa26_instat_callback,
1410 .glocont_callback = usa26_glocont_callback,
1411 .indat_callback = usa26_indat_callback,
1412 .outdat_callback = usa2x_outdat_callback,
1413 .inack_callback = usa26_inack_callback,
1414 .outcont_callback = usa26_outcont_callback,
1415 }, {
1416 /* msg_usa28 callbacks */
1417 .instat_callback = usa28_instat_callback,
1418 .glocont_callback = usa28_glocont_callback,
1419 .indat_callback = usa28_indat_callback,
1420 .outdat_callback = usa2x_outdat_callback,
1421 .inack_callback = usa28_inack_callback,
1422 .outcont_callback = usa28_outcont_callback,
1423 }, {
1424 /* msg_usa49 callbacks */
1425 .instat_callback = usa49_instat_callback,
1426 .glocont_callback = usa49_glocont_callback,
1427 .indat_callback = usa49_indat_callback,
1428 .outdat_callback = usa2x_outdat_callback,
1429 .inack_callback = usa49_inack_callback,
1430 .outcont_callback = usa49_outcont_callback,
1431 }, {
1432 /* msg_usa90 callbacks */
1433 .instat_callback = usa90_instat_callback,
1434 .glocont_callback = usa28_glocont_callback,
1435 .indat_callback = usa90_indat_callback,
1436 .outdat_callback = usa2x_outdat_callback,
1437 .inack_callback = usa28_inack_callback,
1438 .outcont_callback = usa90_outcont_callback,
1439 }, {
1440 /* msg_usa67 callbacks */
1441 .instat_callback = usa67_instat_callback,
1442 .glocont_callback = usa67_glocont_callback,
1443 .indat_callback = usa26_indat_callback,
1444 .outdat_callback = usa2x_outdat_callback,
1445 .inack_callback = usa26_inack_callback,
1446 .outcont_callback = usa26_outcont_callback,
1447 }
1448};
1449
1450 /* Generic setup urbs function that uses
1451 data in device_details */
1452static void keyspan_setup_urbs(struct usb_serial *serial)
1453{
1454 int i, j;
1455 struct keyspan_serial_private *s_priv;
1456 const struct keyspan_device_details *d_details;
1457 struct usb_serial_port *port;
1458 struct keyspan_port_private *p_priv;
1459 struct callbacks *cback;
1460 int endp;
1461
1462 dbg("%s", __func__);
1463
1464 s_priv = usb_get_serial_data(serial);
1465 d_details = s_priv->device_details;
1466
1467 /* Setup values for the various callback routines */
1468 cback = &keyspan_callbacks[d_details->msg_format];
1469
1470 /* Allocate and set up urbs for each one that is in use,
1471 starting with instat endpoints */
1472 s_priv->instat_urb = keyspan_setup_urb
1473 (serial, d_details->instat_endpoint, USB_DIR_IN,
1474 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1475 cback->instat_callback);
1476
1477 s_priv->indat_urb = keyspan_setup_urb
1478 (serial, d_details->indat_endpoint, USB_DIR_IN,
1479 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1480 usa49wg_indat_callback);
1481
1482 s_priv->glocont_urb = keyspan_setup_urb
1483 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1484 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1485 cback->glocont_callback);
1486
1487 /* Setup endpoints for each port specific thing */
1488 for (i = 0; i < d_details->num_ports; i++) {
1489 port = serial->port[i];
1490 p_priv = usb_get_serial_port_data(port);
1491
1492 /* Do indat endpoints first, once for each flip */
1493 endp = d_details->indat_endpoints[i];
1494 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1495 p_priv->in_urbs[j] = keyspan_setup_urb
1496 (serial, endp, USB_DIR_IN, port,
1497 p_priv->in_buffer[j], 64,
1498 cback->indat_callback);
1499 }
1500 for (; j < 2; ++j)
1501 p_priv->in_urbs[j] = NULL;
1502
1503 /* outdat endpoints also have flip */
1504 endp = d_details->outdat_endpoints[i];
1505 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1506 p_priv->out_urbs[j] = keyspan_setup_urb
1507 (serial, endp, USB_DIR_OUT, port,
1508 p_priv->out_buffer[j], 64,
1509 cback->outdat_callback);
1510 }
1511 for (; j < 2; ++j)
1512 p_priv->out_urbs[j] = NULL;
1513
1514 /* inack endpoint */
1515 p_priv->inack_urb = keyspan_setup_urb
1516 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1517 port, p_priv->inack_buffer, 1, cback->inack_callback);
1518
1519 /* outcont endpoint */
1520 p_priv->outcont_urb = keyspan_setup_urb
1521 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1522 port, p_priv->outcont_buffer, 64,
1523 cback->outcont_callback);
1524 }
1525}
1526
1527/* usa19 function doesn't require prescaler */
1528static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1529 u8 *rate_low, u8 *prescaler, int portnum)
1530{
1531 u32 b16, /* baud rate times 16 (actual rate used internally) */
1532 div, /* divisor */
1533 cnt; /* inverse of divisor (programmed into 8051) */
1534
1535 dbg("%s - %d.", __func__, baud_rate);
1536
1537 /* prevent divide by zero... */
1538 b16 = baud_rate * 16L;
1539 if (b16 == 0)
1540 return KEYSPAN_INVALID_BAUD_RATE;
1541 /* Any "standard" rate over 57k6 is marginal on the USA-19
1542 as we run out of divisor resolution. */
1543 if (baud_rate > 57600)
1544 return KEYSPAN_INVALID_BAUD_RATE;
1545
1546 /* calculate the divisor and the counter (its inverse) */
1547 div = baudclk / b16;
1548 if (div == 0)
1549 return KEYSPAN_INVALID_BAUD_RATE;
1550 else
1551 cnt = 0 - div;
1552
1553 if (div > 0xffff)
1554 return KEYSPAN_INVALID_BAUD_RATE;
1555
1556 /* return the counter values if non-null */
1557 if (rate_low)
1558 *rate_low = (u8) (cnt & 0xff);
1559 if (rate_hi)
1560 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1561 if (rate_low && rate_hi)
1562 dbg("%s - %d %02x %02x.",
1563 __func__, baud_rate, *rate_hi, *rate_low);
1564 return KEYSPAN_BAUD_RATE_OK;
1565}
1566
1567/* usa19hs function doesn't require prescaler */
1568static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1569 u8 *rate_low, u8 *prescaler, int portnum)
1570{
1571 u32 b16, /* baud rate times 16 (actual rate used internally) */
1572 div; /* divisor */
1573
1574 dbg("%s - %d.", __func__, baud_rate);
1575
1576 /* prevent divide by zero... */
1577 b16 = baud_rate * 16L;
1578 if (b16 == 0)
1579 return KEYSPAN_INVALID_BAUD_RATE;
1580
1581 /* calculate the divisor */
1582 div = baudclk / b16;
1583 if (div == 0)
1584 return KEYSPAN_INVALID_BAUD_RATE;
1585
1586 if (div > 0xffff)
1587 return KEYSPAN_INVALID_BAUD_RATE;
1588
1589 /* return the counter values if non-null */
1590 if (rate_low)
1591 *rate_low = (u8) (div & 0xff);
1592
1593 if (rate_hi)
1594 *rate_hi = (u8) ((div >> 8) & 0xff);
1595
1596 if (rate_low && rate_hi)
1597 dbg("%s - %d %02x %02x.",
1598 __func__, baud_rate, *rate_hi, *rate_low);
1599
1600 return KEYSPAN_BAUD_RATE_OK;
1601}
1602
1603static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1604 u8 *rate_low, u8 *prescaler, int portnum)
1605{
1606 u32 b16, /* baud rate times 16 (actual rate used internally) */
1607 clk, /* clock with 13/8 prescaler */
1608 div, /* divisor using 13/8 prescaler */
1609 res, /* resulting baud rate using 13/8 prescaler */
1610 diff, /* error using 13/8 prescaler */
1611 smallest_diff;
1612 u8 best_prescaler;
1613 int i;
1614
1615 dbg("%s - %d.", __func__, baud_rate);
1616
1617 /* prevent divide by zero */
1618 b16 = baud_rate * 16L;
1619 if (b16 == 0)
1620 return KEYSPAN_INVALID_BAUD_RATE;
1621
1622 /* Calculate prescaler by trying them all and looking
1623 for best fit */
1624
1625 /* start with largest possible difference */
1626 smallest_diff = 0xffffffff;
1627
1628 /* 0 is an invalid prescaler, used as a flag */
1629 best_prescaler = 0;
1630
1631 for (i = 8; i <= 0xff; ++i) {
1632 clk = (baudclk * 8) / (u32) i;
1633
1634 div = clk / b16;
1635 if (div == 0)
1636 continue;
1637
1638 res = clk / div;
1639 diff = (res > b16) ? (res-b16) : (b16-res);
1640
1641 if (diff < smallest_diff) {
1642 best_prescaler = i;
1643 smallest_diff = diff;
1644 }
1645 }
1646
1647 if (best_prescaler == 0)
1648 return KEYSPAN_INVALID_BAUD_RATE;
1649
1650 clk = (baudclk * 8) / (u32) best_prescaler;
1651 div = clk / b16;
1652
1653 /* return the divisor and prescaler if non-null */
1654 if (rate_low)
1655 *rate_low = (u8) (div & 0xff);
1656 if (rate_hi)
1657 *rate_hi = (u8) ((div >> 8) & 0xff);
1658 if (prescaler) {
1659 *prescaler = best_prescaler;
1660 /* dbg("%s - %d %d", __func__, *prescaler, div); */
1661 }
1662 return KEYSPAN_BAUD_RATE_OK;
1663}
1664
1665 /* USA-28 supports different maximum baud rates on each port */
1666static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1667 u8 *rate_low, u8 *prescaler, int portnum)
1668{
1669 u32 b16, /* baud rate times 16 (actual rate used internally) */
1670 div, /* divisor */
1671 cnt; /* inverse of divisor (programmed into 8051) */
1672
1673 dbg("%s - %d.", __func__, baud_rate);
1674
1675 /* prevent divide by zero */
1676 b16 = baud_rate * 16L;
1677 if (b16 == 0)
1678 return KEYSPAN_INVALID_BAUD_RATE;
1679
1680 /* calculate the divisor and the counter (its inverse) */
1681 div = KEYSPAN_USA28_BAUDCLK / b16;
1682 if (div == 0)
1683 return KEYSPAN_INVALID_BAUD_RATE;
1684 else
1685 cnt = 0 - div;
1686
1687 /* check for out of range, based on portnum,
1688 and return result */
1689 if (portnum == 0) {
1690 if (div > 0xffff)
1691 return KEYSPAN_INVALID_BAUD_RATE;
1692 } else {
1693 if (portnum == 1) {
1694 if (div > 0xff)
1695 return KEYSPAN_INVALID_BAUD_RATE;
1696 } else
1697 return KEYSPAN_INVALID_BAUD_RATE;
1698 }
1699
1700 /* return the counter values if not NULL
1701 (port 1 will ignore retHi) */
1702 if (rate_low)
1703 *rate_low = (u8) (cnt & 0xff);
1704 if (rate_hi)
1705 *rate_hi = (u8) ((cnt >> 8) & 0xff);
1706 dbg("%s - %d OK.", __func__, baud_rate);
1707 return KEYSPAN_BAUD_RATE_OK;
1708}
1709
1710static int keyspan_usa26_send_setup(struct usb_serial *serial,
1711 struct usb_serial_port *port,
1712 int reset_port)
1713{
1714 struct keyspan_usa26_portControlMessage msg;
1715 struct keyspan_serial_private *s_priv;
1716 struct keyspan_port_private *p_priv;
1717 const struct keyspan_device_details *d_details;
1718 int outcont_urb;
1719 struct urb *this_urb;
1720 int device_port, err;
1721
1722 dbg("%s reset=%d", __func__, reset_port);
1723
1724 s_priv = usb_get_serial_data(serial);
1725 p_priv = usb_get_serial_port_data(port);
1726 d_details = s_priv->device_details;
1727 device_port = port->number - port->serial->minor;
1728
1729 outcont_urb = d_details->outcont_endpoints[device_port];
1730 this_urb = p_priv->outcont_urb;
1731
1732 dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe));
1733
1734 /* Make sure we have an urb then send the message */
1735 if (this_urb == NULL) {
1736 dbg("%s - oops no urb.", __func__);
1737 return -1;
1738 }
1739
1740 /* Save reset port val for resend.
1741 Don't overwrite resend for open/close condition. */
1742 if ((reset_port + 1) > p_priv->resend_cont)
1743 p_priv->resend_cont = reset_port + 1;
1744 if (this_urb->status == -EINPROGRESS) {
1745 /* dbg("%s - already writing", __func__); */
1746 mdelay(5);
1747 return -1;
1748 }
1749
1750 memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
1751
1752 /* Only set baud rate if it's changed */
1753 if (p_priv->old_baud != p_priv->baud) {
1754 p_priv->old_baud = p_priv->baud;
1755 msg.setClocking = 0xff;
1756 if (d_details->calculate_baud_rate
1757 (p_priv->baud, d_details->baudclk, &msg.baudHi,
1758 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1759 dbg("%s - Invalid baud rate %d requested, using 9600.",
1760 __func__, p_priv->baud);
1761 msg.baudLo = 0;
1762 msg.baudHi = 125; /* Values for 9600 baud */
1763 msg.prescaler = 10;
1764 }
1765 msg.setPrescaler = 0xff;
1766 }
1767
1768 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1769 switch (p_priv->cflag & CSIZE) {
1770 case CS5:
1771 msg.lcr |= USA_DATABITS_5;
1772 break;
1773 case CS6:
1774 msg.lcr |= USA_DATABITS_6;
1775 break;
1776 case CS7:
1777 msg.lcr |= USA_DATABITS_7;
1778 break;
1779 case CS8:
1780 msg.lcr |= USA_DATABITS_8;
1781 break;
1782 }
1783 if (p_priv->cflag & PARENB) {
1784 /* note USA_PARITY_NONE == 0 */
1785 msg.lcr |= (p_priv->cflag & PARODD)?
1786 USA_PARITY_ODD : USA_PARITY_EVEN;
1787 }
1788 msg.setLcr = 0xff;
1789
1790 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1791 msg.xonFlowControl = 0;
1792 msg.setFlowControl = 0xff;
1793 msg.forwardingLength = 16;
1794 msg.xonChar = 17;
1795 msg.xoffChar = 19;
1796
1797 /* Opening port */
1798 if (reset_port == 1) {
1799 msg._txOn = 1;
1800 msg._txOff = 0;
1801 msg.txFlush = 0;
1802 msg.txBreak = 0;
1803 msg.rxOn = 1;
1804 msg.rxOff = 0;
1805 msg.rxFlush = 1;
1806 msg.rxForward = 0;
1807 msg.returnStatus = 0;
1808 msg.resetDataToggle = 0xff;
1809 }
1810
1811 /* Closing port */
1812 else if (reset_port == 2) {
1813 msg._txOn = 0;
1814 msg._txOff = 1;
1815 msg.txFlush = 0;
1816 msg.txBreak = 0;
1817 msg.rxOn = 0;
1818 msg.rxOff = 1;
1819 msg.rxFlush = 1;
1820 msg.rxForward = 0;
1821 msg.returnStatus = 0;
1822 msg.resetDataToggle = 0;
1823 }
1824
1825 /* Sending intermediate configs */
1826 else {
1827 msg._txOn = (!p_priv->break_on);
1828 msg._txOff = 0;
1829 msg.txFlush = 0;
1830 msg.txBreak = (p_priv->break_on);
1831 msg.rxOn = 0;
1832 msg.rxOff = 0;
1833 msg.rxFlush = 0;
1834 msg.rxForward = 0;
1835 msg.returnStatus = 0;
1836 msg.resetDataToggle = 0x0;
1837 }
1838
1839 /* Do handshaking outputs */
1840 msg.setTxTriState_setRts = 0xff;
1841 msg.txTriState_rts = p_priv->rts_state;
1842
1843 msg.setHskoa_setDtr = 0xff;
1844 msg.hskoa_dtr = p_priv->dtr_state;
1845
1846 p_priv->resend_cont = 0;
1847 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1848
1849 /* send the data out the device on control endpoint */
1850 this_urb->transfer_buffer_length = sizeof(msg);
1851
1852 err = usb_submit_urb(this_urb, GFP_ATOMIC);
1853 if (err != 0)
1854 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
1855#if 0
1856 else {
1857 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
1858 outcont_urb, this_urb->transfer_buffer_length,
1859 usb_pipeendpoint(this_urb->pipe));
1860 }
1861#endif
1862
1863 return 0;
1864}
1865
1866static int keyspan_usa28_send_setup(struct usb_serial *serial,
1867 struct usb_serial_port *port,
1868 int reset_port)
1869{
1870 struct keyspan_usa28_portControlMessage msg;
1871 struct keyspan_serial_private *s_priv;
1872 struct keyspan_port_private *p_priv;
1873 const struct keyspan_device_details *d_details;
1874 struct urb *this_urb;
1875 int device_port, err;
1876
1877 dbg("%s", __func__);
1878
1879 s_priv = usb_get_serial_data(serial);
1880 p_priv = usb_get_serial_port_data(port);
1881 d_details = s_priv->device_details;
1882 device_port = port->number - port->serial->minor;
1883
1884 /* only do something if we have a bulk out endpoint */
1885 this_urb = p_priv->outcont_urb;
1886 if (this_urb == NULL) {
1887 dbg("%s - oops no urb.", __func__);
1888 return -1;
1889 }
1890
1891 /* Save reset port val for resend.
1892 Don't overwrite resend for open/close condition. */
1893 if ((reset_port + 1) > p_priv->resend_cont)
1894 p_priv->resend_cont = reset_port + 1;
1895 if (this_urb->status == -EINPROGRESS) {
1896 dbg("%s already writing", __func__);
1897 mdelay(5);
1898 return -1;
1899 }
1900
1901 memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
1902
1903 msg.setBaudRate = 1;
1904 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
1905 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1906 dbg("%s - Invalid baud rate requested %d.",
1907 __func__, p_priv->baud);
1908 msg.baudLo = 0xff;
1909 msg.baudHi = 0xb2; /* Values for 9600 baud */
1910 }
1911
1912 /* If parity is enabled, we must calculate it ourselves. */
1913 msg.parity = 0; /* XXX for now */
1914
1915 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1916 msg.xonFlowControl = 0;
1917
1918 /* Do handshaking outputs, DTR is inverted relative to RTS */
1919 msg.rts = p_priv->rts_state;
1920 msg.dtr = p_priv->dtr_state;
1921
1922 msg.forwardingLength = 16;
1923 msg.forwardMs = 10;
1924 msg.breakThreshold = 45;
1925 msg.xonChar = 17;
1926 msg.xoffChar = 19;
1927
1928 /*msg.returnStatus = 1;
1929 msg.resetDataToggle = 0xff;*/
1930 /* Opening port */
1931 if (reset_port == 1) {
1932 msg._txOn = 1;
1933 msg._txOff = 0;
1934 msg.txFlush = 0;
1935 msg.txForceXoff = 0;
1936 msg.txBreak = 0;
1937 msg.rxOn = 1;
1938 msg.rxOff = 0;
1939 msg.rxFlush = 1;
1940 msg.rxForward = 0;
1941 msg.returnStatus = 0;
1942 msg.resetDataToggle = 0xff;
1943 }
1944 /* Closing port */
1945 else if (reset_port == 2) {
1946 msg._txOn = 0;
1947 msg._txOff = 1;
1948 msg.txFlush = 0;
1949 msg.txForceXoff = 0;
1950 msg.txBreak = 0;
1951 msg.rxOn = 0;
1952 msg.rxOff = 1;
1953 msg.rxFlush = 1;
1954 msg.rxForward = 0;
1955 msg.returnStatus = 0;
1956 msg.resetDataToggle = 0;
1957 }
1958 /* Sending intermediate configs */
1959 else {
1960 msg._txOn = (!p_priv->break_on);
1961 msg._txOff = 0;
1962 msg.txFlush = 0;
1963 msg.txForceXoff = 0;
1964 msg.txBreak = (p_priv->break_on);
1965 msg.rxOn = 0;
1966 msg.rxOff = 0;
1967 msg.rxFlush = 0;
1968 msg.rxForward = 0;
1969 msg.returnStatus = 0;
1970 msg.resetDataToggle = 0x0;
1971 }
1972
1973 p_priv->resend_cont = 0;
1974 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1975
1976 /* send the data out the device on control endpoint */
1977 this_urb->transfer_buffer_length = sizeof(msg);
1978
1979 err = usb_submit_urb(this_urb, GFP_ATOMIC);
1980 if (err != 0)
1981 dbg("%s - usb_submit_urb(setup) failed", __func__);
1982#if 0
1983 else {
1984 dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__,
1985 this_urb->transfer_buffer_length);
1986 }
1987#endif
1988
1989 return 0;
1990}
1991
1992static int keyspan_usa49_send_setup(struct usb_serial *serial,
1993 struct usb_serial_port *port,
1994 int reset_port)
1995{
1996 struct keyspan_usa49_portControlMessage msg;
1997 struct usb_ctrlrequest *dr = NULL;
1998 struct keyspan_serial_private *s_priv;
1999 struct keyspan_port_private *p_priv;
2000 const struct keyspan_device_details *d_details;
2001 struct urb *this_urb;
2002 int err, device_port;
2003
2004 dbg("%s", __func__);
2005
2006 s_priv = usb_get_serial_data(serial);
2007 p_priv = usb_get_serial_port_data(port);
2008 d_details = s_priv->device_details;
2009
2010 this_urb = s_priv->glocont_urb;
2011
2012 /* Work out which port within the device is being setup */
2013 device_port = port->number - port->serial->minor;
2014
2015 /* Make sure we have an urb then send the message */
2016 if (this_urb == NULL) {
2017 dbg("%s - oops no urb for port %d.", __func__, port->number);
2018 return -1;
2019 }
2020
2021 dbg("%s - endpoint %d port %d (%d)",
2022 __func__, usb_pipeendpoint(this_urb->pipe),
2023 port->number, device_port);
2024
2025 /* Save reset port val for resend.
2026 Don't overwrite resend for open/close condition. */
2027 if ((reset_port + 1) > p_priv->resend_cont)
2028 p_priv->resend_cont = reset_port + 1;
2029
2030 if (this_urb->status == -EINPROGRESS) {
2031 /* dbg("%s - already writing", __func__); */
2032 mdelay(5);
2033 return -1;
2034 }
2035
2036 memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
2037
2038 /*msg.portNumber = port->number;*/
2039 msg.portNumber = device_port;
2040
2041 /* Only set baud rate if it's changed */
2042 if (p_priv->old_baud != p_priv->baud) {
2043 p_priv->old_baud = p_priv->baud;
2044 msg.setClocking = 0xff;
2045 if (d_details->calculate_baud_rate
2046 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2047 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2048 dbg("%s - Invalid baud rate %d requested, using 9600.",
2049 __func__, p_priv->baud);
2050 msg.baudLo = 0;
2051 msg.baudHi = 125; /* Values for 9600 baud */
2052 msg.prescaler = 10;
2053 }
2054 /* msg.setPrescaler = 0xff; */
2055 }
2056
2057 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2058 switch (p_priv->cflag & CSIZE) {
2059 case CS5:
2060 msg.lcr |= USA_DATABITS_5;
2061 break;
2062 case CS6:
2063 msg.lcr |= USA_DATABITS_6;
2064 break;
2065 case CS7:
2066 msg.lcr |= USA_DATABITS_7;
2067 break;
2068 case CS8:
2069 msg.lcr |= USA_DATABITS_8;
2070 break;
2071 }
2072 if (p_priv->cflag & PARENB) {
2073 /* note USA_PARITY_NONE == 0 */
2074 msg.lcr |= (p_priv->cflag & PARODD)?
2075 USA_PARITY_ODD : USA_PARITY_EVEN;
2076 }
2077 msg.setLcr = 0xff;
2078
2079 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2080 msg.xonFlowControl = 0;
2081 msg.setFlowControl = 0xff;
2082
2083 msg.forwardingLength = 16;
2084 msg.xonChar = 17;
2085 msg.xoffChar = 19;
2086
2087 /* Opening port */
2088 if (reset_port == 1) {
2089 msg._txOn = 1;
2090 msg._txOff = 0;
2091 msg.txFlush = 0;
2092 msg.txBreak = 0;
2093 msg.rxOn = 1;
2094 msg.rxOff = 0;
2095 msg.rxFlush = 1;
2096 msg.rxForward = 0;
2097 msg.returnStatus = 0;
2098 msg.resetDataToggle = 0xff;
2099 msg.enablePort = 1;
2100 msg.disablePort = 0;
2101 }
2102 /* Closing port */
2103 else if (reset_port == 2) {
2104 msg._txOn = 0;
2105 msg._txOff = 1;
2106 msg.txFlush = 0;
2107 msg.txBreak = 0;
2108 msg.rxOn = 0;
2109 msg.rxOff = 1;
2110 msg.rxFlush = 1;
2111 msg.rxForward = 0;
2112 msg.returnStatus = 0;
2113 msg.resetDataToggle = 0;
2114 msg.enablePort = 0;
2115 msg.disablePort = 1;
2116 }
2117 /* Sending intermediate configs */
2118 else {
2119 msg._txOn = (!p_priv->break_on);
2120 msg._txOff = 0;
2121 msg.txFlush = 0;
2122 msg.txBreak = (p_priv->break_on);
2123 msg.rxOn = 0;
2124 msg.rxOff = 0;
2125 msg.rxFlush = 0;
2126 msg.rxForward = 0;
2127 msg.returnStatus = 0;
2128 msg.resetDataToggle = 0x0;
2129 msg.enablePort = 0;
2130 msg.disablePort = 0;
2131 }
2132
2133 /* Do handshaking outputs */
2134 msg.setRts = 0xff;
2135 msg.rts = p_priv->rts_state;
2136
2137 msg.setDtr = 0xff;
2138 msg.dtr = p_priv->dtr_state;
2139
2140 p_priv->resend_cont = 0;
2141
2142 /* if the device is a 49wg, we send control message on usb
2143 control EP 0 */
2144
2145 if (d_details->product_id == keyspan_usa49wg_product_id) {
2146 dr = (void *)(s_priv->ctrl_buf);
2147 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2148 dr->bRequest = 0xB0; /* 49wg control message */;
2149 dr->wValue = 0;
2150 dr->wIndex = 0;
2151 dr->wLength = cpu_to_le16(sizeof(msg));
2152
2153 memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
2154
2155 usb_fill_control_urb(this_urb, serial->dev,
2156 usb_sndctrlpipe(serial->dev, 0),
2157 (unsigned char *)dr, s_priv->glocont_buf,
2158 sizeof(msg), usa49_glocont_callback, serial);
2159
2160 } else {
2161 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2162
2163 /* send the data out the device on control endpoint */
2164 this_urb->transfer_buffer_length = sizeof(msg);
2165 }
2166 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2167 if (err != 0)
2168 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2169#if 0
2170 else {
2171 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__,
2172 outcont_urb, this_urb->transfer_buffer_length,
2173 usb_pipeendpoint(this_urb->pipe));
2174 }
2175#endif
2176
2177 return 0;
2178}
2179
2180static int keyspan_usa90_send_setup(struct usb_serial *serial,
2181 struct usb_serial_port *port,
2182 int reset_port)
2183{
2184 struct keyspan_usa90_portControlMessage msg;
2185 struct keyspan_serial_private *s_priv;
2186 struct keyspan_port_private *p_priv;
2187 const struct keyspan_device_details *d_details;
2188 struct urb *this_urb;
2189 int err;
2190 u8 prescaler;
2191
2192 dbg("%s", __func__);
2193
2194 s_priv = usb_get_serial_data(serial);
2195 p_priv = usb_get_serial_port_data(port);
2196 d_details = s_priv->device_details;
2197
2198 /* only do something if we have a bulk out endpoint */
2199 this_urb = p_priv->outcont_urb;
2200 if (this_urb == NULL) {
2201 dbg("%s - oops no urb.", __func__);
2202 return -1;
2203 }
2204
2205 /* Save reset port val for resend.
2206 Don't overwrite resend for open/close condition. */
2207 if ((reset_port + 1) > p_priv->resend_cont)
2208 p_priv->resend_cont = reset_port + 1;
2209 if (this_urb->status == -EINPROGRESS) {
2210 dbg("%s already writing", __func__);
2211 mdelay(5);
2212 return -1;
2213 }
2214
2215 memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
2216
2217 /* Only set baud rate if it's changed */
2218 if (p_priv->old_baud != p_priv->baud) {
2219 p_priv->old_baud = p_priv->baud;
2220 msg.setClocking = 0x01;
2221 if (d_details->calculate_baud_rate
2222 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2223 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
2224 dbg("%s - Invalid baud rate %d requested, using 9600.",
2225 __func__, p_priv->baud);
2226 p_priv->baud = 9600;
2227 d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
2228 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2229 }
2230 msg.setRxMode = 1;
2231 msg.setTxMode = 1;
2232 }
2233
2234 /* modes must always be correctly specified */
2235 if (p_priv->baud > 57600) {
2236 msg.rxMode = RXMODE_DMA;
2237 msg.txMode = TXMODE_DMA;
2238 } else {
2239 msg.rxMode = RXMODE_BYHAND;
2240 msg.txMode = TXMODE_BYHAND;
2241 }
2242
2243 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2244 switch (p_priv->cflag & CSIZE) {
2245 case CS5:
2246 msg.lcr |= USA_DATABITS_5;
2247 break;
2248 case CS6:
2249 msg.lcr |= USA_DATABITS_6;
2250 break;
2251 case CS7:
2252 msg.lcr |= USA_DATABITS_7;
2253 break;
2254 case CS8:
2255 msg.lcr |= USA_DATABITS_8;
2256 break;
2257 }
2258 if (p_priv->cflag & PARENB) {
2259 /* note USA_PARITY_NONE == 0 */
2260 msg.lcr |= (p_priv->cflag & PARODD)?
2261 USA_PARITY_ODD : USA_PARITY_EVEN;
2262 }
2263 if (p_priv->old_cflag != p_priv->cflag) {
2264 p_priv->old_cflag = p_priv->cflag;
2265 msg.setLcr = 0x01;
2266 }
2267
2268 if (p_priv->flow_control == flow_cts)
2269 msg.txFlowControl = TXFLOW_CTS;
2270 msg.setTxFlowControl = 0x01;
2271 msg.setRxFlowControl = 0x01;
2272
2273 msg.rxForwardingLength = 16;
2274 msg.rxForwardingTimeout = 16;
2275 msg.txAckSetting = 0;
2276 msg.xonChar = 17;
2277 msg.xoffChar = 19;
2278
2279 /* Opening port */
2280 if (reset_port == 1) {
2281 msg.portEnabled = 1;
2282 msg.rxFlush = 1;
2283 msg.txBreak = (p_priv->break_on);
2284 }
2285 /* Closing port */
2286 else if (reset_port == 2)
2287 msg.portEnabled = 0;
2288 /* Sending intermediate configs */
2289 else {
2290 msg.portEnabled = 1;
2291 msg.txBreak = (p_priv->break_on);
2292 }
2293
2294 /* Do handshaking outputs */
2295 msg.setRts = 0x01;
2296 msg.rts = p_priv->rts_state;
2297
2298 msg.setDtr = 0x01;
2299 msg.dtr = p_priv->dtr_state;
2300
2301 p_priv->resend_cont = 0;
2302 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2303
2304 /* send the data out the device on control endpoint */
2305 this_urb->transfer_buffer_length = sizeof(msg);
2306
2307 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2308 if (err != 0)
2309 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
2310 return 0;
2311}
2312
2313static int keyspan_usa67_send_setup(struct usb_serial *serial,
2314 struct usb_serial_port *port,
2315 int reset_port)
2316{
2317 struct keyspan_usa67_portControlMessage msg;
2318 struct keyspan_serial_private *s_priv;
2319 struct keyspan_port_private *p_priv;
2320 const struct keyspan_device_details *d_details;
2321 struct urb *this_urb;
2322 int err, device_port;
2323
2324 dbg("%s", __func__);
2325
2326 s_priv = usb_get_serial_data(serial);
2327 p_priv = usb_get_serial_port_data(port);
2328 d_details = s_priv->device_details;
2329
2330 this_urb = s_priv->glocont_urb;
2331
2332 /* Work out which port within the device is being setup */
2333 device_port = port->number - port->serial->minor;
2334
2335 /* Make sure we have an urb then send the message */
2336 if (this_urb == NULL) {
2337 dbg("%s - oops no urb for port %d.", __func__,
2338 port->number);
2339 return -1;
2340 }
2341
2342 /* Save reset port val for resend.
2343 Don't overwrite resend for open/close condition. */
2344 if ((reset_port + 1) > p_priv->resend_cont)
2345 p_priv->resend_cont = reset_port + 1;
2346 if (this_urb->status == -EINPROGRESS) {
2347 /* dbg("%s - already writing", __func__); */
2348 mdelay(5);
2349 return -1;
2350 }
2351
2352 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2353
2354 msg.port = device_port;
2355
2356 /* Only set baud rate if it's changed */
2357 if (p_priv->old_baud != p_priv->baud) {
2358 p_priv->old_baud = p_priv->baud;
2359 msg.setClocking = 0xff;
2360 if (d_details->calculate_baud_rate
2361 (p_priv->baud, d_details->baudclk, &msg.baudHi,
2362 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2363 dbg("%s - Invalid baud rate %d requested, using 9600.",
2364 __func__, p_priv->baud);
2365 msg.baudLo = 0;
2366 msg.baudHi = 125; /* Values for 9600 baud */
2367 msg.prescaler = 10;
2368 }
2369 msg.setPrescaler = 0xff;
2370 }
2371
2372 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2373 switch (p_priv->cflag & CSIZE) {
2374 case CS5:
2375 msg.lcr |= USA_DATABITS_5;
2376 break;
2377 case CS6:
2378 msg.lcr |= USA_DATABITS_6;
2379 break;
2380 case CS7:
2381 msg.lcr |= USA_DATABITS_7;
2382 break;
2383 case CS8:
2384 msg.lcr |= USA_DATABITS_8;
2385 break;
2386 }
2387 if (p_priv->cflag & PARENB) {
2388 /* note USA_PARITY_NONE == 0 */
2389 msg.lcr |= (p_priv->cflag & PARODD)?
2390 USA_PARITY_ODD : USA_PARITY_EVEN;
2391 }
2392 msg.setLcr = 0xff;
2393
2394 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2395 msg.xonFlowControl = 0;
2396 msg.setFlowControl = 0xff;
2397 msg.forwardingLength = 16;
2398 msg.xonChar = 17;
2399 msg.xoffChar = 19;
2400
2401 if (reset_port == 1) {
2402 /* Opening port */
2403 msg._txOn = 1;
2404 msg._txOff = 0;
2405 msg.txFlush = 0;
2406 msg.txBreak = 0;
2407 msg.rxOn = 1;
2408 msg.rxOff = 0;
2409 msg.rxFlush = 1;
2410 msg.rxForward = 0;
2411 msg.returnStatus = 0;
2412 msg.resetDataToggle = 0xff;
2413 } else if (reset_port == 2) {
2414 /* Closing port */
2415 msg._txOn = 0;
2416 msg._txOff = 1;
2417 msg.txFlush = 0;
2418 msg.txBreak = 0;
2419 msg.rxOn = 0;
2420 msg.rxOff = 1;
2421 msg.rxFlush = 1;
2422 msg.rxForward = 0;
2423 msg.returnStatus = 0;
2424 msg.resetDataToggle = 0;
2425 } else {
2426 /* Sending intermediate configs */
2427 msg._txOn = (!p_priv->break_on);
2428 msg._txOff = 0;
2429 msg.txFlush = 0;
2430 msg.txBreak = (p_priv->break_on);
2431 msg.rxOn = 0;
2432 msg.rxOff = 0;
2433 msg.rxFlush = 0;
2434 msg.rxForward = 0;
2435 msg.returnStatus = 0;
2436 msg.resetDataToggle = 0x0;
2437 }
2438
2439 /* Do handshaking outputs */
2440 msg.setTxTriState_setRts = 0xff;
2441 msg.txTriState_rts = p_priv->rts_state;
2442
2443 msg.setHskoa_setDtr = 0xff;
2444 msg.hskoa_dtr = p_priv->dtr_state;
2445
2446 p_priv->resend_cont = 0;
2447
2448 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2449
2450 /* send the data out the device on control endpoint */
2451 this_urb->transfer_buffer_length = sizeof(msg);
2452
2453 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2454 if (err != 0)
2455 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__,
2456 err);
2457 return 0;
2458}
2459
2460static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2461{
2462 struct usb_serial *serial = port->serial;
2463 struct keyspan_serial_private *s_priv;
2464 const struct keyspan_device_details *d_details;
2465
2466 dbg("%s", __func__);
2467
2468 s_priv = usb_get_serial_data(serial);
2469 d_details = s_priv->device_details;
2470
2471 switch (d_details->msg_format) {
2472 case msg_usa26:
2473 keyspan_usa26_send_setup(serial, port, reset_port);
2474 break;
2475 case msg_usa28:
2476 keyspan_usa28_send_setup(serial, port, reset_port);
2477 break;
2478 case msg_usa49:
2479 keyspan_usa49_send_setup(serial, port, reset_port);
2480 break;
2481 case msg_usa90:
2482 keyspan_usa90_send_setup(serial, port, reset_port);
2483 break;
2484 case msg_usa67:
2485 keyspan_usa67_send_setup(serial, port, reset_port);
2486 break;
2487 }
2488}
2489
2490
2491/* Gets called by the "real" driver (ie once firmware is loaded
2492 and renumeration has taken place. */
2493static int keyspan_startup(struct usb_serial *serial)
2494{
2495 int i, err;
2496 struct usb_serial_port *port;
2497 struct keyspan_serial_private *s_priv;
2498 struct keyspan_port_private *p_priv;
2499 const struct keyspan_device_details *d_details;
2500
2501 dbg("%s", __func__);
2502
2503 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
2504 if (d_details->product_id ==
2505 le16_to_cpu(serial->dev->descriptor.idProduct))
2506 break;
2507 if (d_details == NULL) {
2508 dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
2509 __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
2510 return -ENODEV;
2511 }
2512
2513 /* Setup private data for serial driver */
2514 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
2515 if (!s_priv) {
2516 dbg("%s - kmalloc for keyspan_serial_private failed.",
2517 __func__);
2518 return -ENOMEM;
2519 }
2520
2521 s_priv->device_details = d_details;
2522 usb_set_serial_data(serial, s_priv);
2523
2524 /* Now setup per port private data */
2525 for (i = 0; i < serial->num_ports; i++) {
2526 port = serial->port[i];
2527 p_priv = kzalloc(sizeof(struct keyspan_port_private),
2528 GFP_KERNEL);
2529 if (!p_priv) {
2530 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i);
2531 return 1;
2532 }
2533 p_priv->device_details = d_details;
2534 usb_set_serial_port_data(port, p_priv);
2535 }
2536
2537 keyspan_setup_urbs(serial);
2538
2539 if (s_priv->instat_urb != NULL) {
2540 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2541 if (err != 0)
2542 dbg("%s - submit instat urb failed %d", __func__,
2543 err);
2544 }
2545 if (s_priv->indat_urb != NULL) {
2546 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2547 if (err != 0)
2548 dbg("%s - submit indat urb failed %d", __func__,
2549 err);
2550 }
2551
2552 return 0;
2553}
2554
2555static void keyspan_disconnect(struct usb_serial *serial)
2556{
2557 int i, j;
2558 struct usb_serial_port *port;
2559 struct keyspan_serial_private *s_priv;
2560 struct keyspan_port_private *p_priv;
2561
2562 dbg("%s", __func__);
2563
2564 s_priv = usb_get_serial_data(serial);
2565
2566 /* Stop reading/writing urbs */
2567 stop_urb(s_priv->instat_urb);
2568 stop_urb(s_priv->glocont_urb);
2569 stop_urb(s_priv->indat_urb);
2570 for (i = 0; i < serial->num_ports; ++i) {
2571 port = serial->port[i];
2572 p_priv = usb_get_serial_port_data(port);
2573 stop_urb(p_priv->inack_urb);
2574 stop_urb(p_priv->outcont_urb);
2575 for (j = 0; j < 2; j++) {
2576 stop_urb(p_priv->in_urbs[j]);
2577 stop_urb(p_priv->out_urbs[j]);
2578 }
2579 }
2580
2581 /* Now free them */
2582 usb_free_urb(s_priv->instat_urb);
2583 usb_free_urb(s_priv->indat_urb);
2584 usb_free_urb(s_priv->glocont_urb);
2585 for (i = 0; i < serial->num_ports; ++i) {
2586 port = serial->port[i];
2587 p_priv = usb_get_serial_port_data(port);
2588 usb_free_urb(p_priv->inack_urb);
2589 usb_free_urb(p_priv->outcont_urb);
2590 for (j = 0; j < 2; j++) {
2591 usb_free_urb(p_priv->in_urbs[j]);
2592 usb_free_urb(p_priv->out_urbs[j]);
2593 }
2594 }
2595}
2596
2597static void keyspan_release(struct usb_serial *serial)
2598{
2599 int i;
2600 struct usb_serial_port *port;
2601 struct keyspan_serial_private *s_priv;
2602
2603 dbg("%s", __func__);
2604
2605 s_priv = usb_get_serial_data(serial);
2606
2607 /* dbg("Freeing serial->private."); */
2608 kfree(s_priv);
2609
2610 /* dbg("Freeing port->private."); */
2611 /* Now free per port private data */
2612 for (i = 0; i < serial->num_ports; i++) {
2613 port = serial->port[i];
2614 kfree(usb_get_serial_port_data(port));
2615 }
2616}
2617
2618MODULE_AUTHOR(DRIVER_AUTHOR);
2619MODULE_DESCRIPTION(DRIVER_DESC);
2620MODULE_LICENSE("GPL");
2621
2622MODULE_FIRMWARE("keyspan/usa28.fw");
2623MODULE_FIRMWARE("keyspan/usa28x.fw");
2624MODULE_FIRMWARE("keyspan/usa28xa.fw");
2625MODULE_FIRMWARE("keyspan/usa28xb.fw");
2626MODULE_FIRMWARE("keyspan/usa19.fw");
2627MODULE_FIRMWARE("keyspan/usa19qi.fw");
2628MODULE_FIRMWARE("keyspan/mpr.fw");
2629MODULE_FIRMWARE("keyspan/usa19qw.fw");
2630MODULE_FIRMWARE("keyspan/usa18x.fw");
2631MODULE_FIRMWARE("keyspan/usa19w.fw");
2632MODULE_FIRMWARE("keyspan/usa49w.fw");
2633MODULE_FIRMWARE("keyspan/usa49wlc.fw");
2634
2635module_param(debug, bool, S_IRUGO | S_IWUSR);
2636MODULE_PARM_DESC(debug, "Debug enabled or not");
2637