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