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