blob: f1195a98f316df62f0d465b144b32fc3f6f9da66 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 Keyspan USB to Serial Converter driver
Alan Coxdeb91682008-07-22 11:13:08 +01003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 (C) Copyright (C) 2000-2001 Hugh Blemings <hugh@blemings.org>
5 (C) Copyright (C) 2002 Greg Kroah-Hartman <greg@kroah.com>
Alan Coxdeb91682008-07-22 11:13:08 +01006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 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.
Alan Coxdeb91682008-07-22 11:13:08 +010013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 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 :)
Alan Coxdeb91682008-07-22 11:13:08 +010020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 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
Alan Coxdeb91682008-07-22 11:13:08 +010024 stuff.
25
26 Tip 'o the hat to IBM (and previously Linuxcare :) for supporting
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 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
Alan Coxdeb91682008-07-22 11:13:08 +010073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 (04/08/2001) gb
75 Identify version on module load.
Alan Coxdeb91682008-07-22 11:13:08 +010076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 (11/01/2000) Adam J. Richter
78 usb_device_id table support.
Alan Coxdeb91682008-07-22 11:13:08 +010079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 Tue Oct 10 23:15:33 EST 2000 Hugh
81 Merged Paul's changes with my USA-49W mods. Work in progress
82 still...
Alan Coxdeb91682008-07-22 11:13:08 +010083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 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.
Alan Coxdeb91682008-07-22 11:13:08 +010087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 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>
David Woodhouse2971c572008-05-30 14:04:03 +0300108#include <linux/firmware.h>
109#include <linux/ihex.h>
Alan Coxdeb91682008-07-22 11:13:08 +0100110#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -0700112#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include "keyspan.h"
114
115static int debug;
116
117/*
118 * Version Information
119 */
Lucy McCoy0ca12682007-05-18 12:10:41 -0700120#define DRIVER_VERSION "v1.1.5"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define DRIVER_AUTHOR "Hugh Blemings <hugh@misc.nu"
122#define DRIVER_DESC "Keyspan USB to Serial Converter Driver"
123
124#define INSTAT_BUFLEN 32
125#define GLOCONT_BUFLEN 64
Lucy McCoy0ca12682007-05-18 12:10:41 -0700126#define INDAT49W_BUFLEN 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* Per device and per port private data */
129struct keyspan_serial_private {
130 const struct keyspan_device_details *device_details;
131
132 struct urb *instat_urb;
133 char instat_buf[INSTAT_BUFLEN];
134
Alan Coxdeb91682008-07-22 11:13:08 +0100135 /* added to support 49wg, where data from all 4 ports comes in
136 on 1 EP and high-speed supported */
Lucy McCoy0ca12682007-05-18 12:10:41 -0700137 struct urb *indat_urb;
138 char indat_buf[INDAT49W_BUFLEN];
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* XXX this one probably will need a lock */
141 struct urb *glocont_urb;
142 char glocont_buf[GLOCONT_BUFLEN];
Alan Coxdeb91682008-07-22 11:13:08 +0100143 char ctrl_buf[8]; /* for EP0 control message */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
146struct keyspan_port_private {
147 /* Keep track of which input & output endpoints to use */
148 int in_flip;
149 int out_flip;
150
151 /* Keep duplicate of device details in each port
152 structure as well - simplifies some of the
153 callback functions etc. */
154 const struct keyspan_device_details *device_details;
155
156 /* Input endpoints and buffer for this port */
157 struct urb *in_urbs[2];
158 char in_buffer[2][64];
159 /* Output endpoints and buffer for this port */
160 struct urb *out_urbs[2];
161 char out_buffer[2][64];
162
163 /* Input ack endpoint */
164 struct urb *inack_urb;
165 char inack_buffer[1];
166
167 /* Output control endpoint */
168 struct urb *outcont_urb;
169 char outcont_buffer[64];
170
171 /* Settings for the port */
172 int baud;
173 int old_baud;
174 unsigned int cflag;
175 unsigned int old_cflag;
176 enum {flow_none, flow_cts, flow_xon} flow_control;
177 int rts_state; /* Handshaking pins (outputs) */
178 int dtr_state;
179 int cts_state; /* Handshaking pins (inputs) */
180 int dsr_state;
181 int dcd_state;
182 int ri_state;
183 int break_on;
184
185 unsigned long tx_start_time[2];
186 int resend_cont; /* need to resend control packet */
187};
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* Include Keyspan message headers. All current Keyspan Adapters
Lucy McCoy0ca12682007-05-18 12:10:41 -0700190 make use of one of five message formats which are referred
Alan Coxdeb91682008-07-22 11:13:08 +0100191 to as USA-26, USA-28, USA-49, USA-90, USA-67 by Keyspan and
192 within this driver. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#include "keyspan_usa26msg.h"
194#include "keyspan_usa28msg.h"
195#include "keyspan_usa49msg.h"
196#include "keyspan_usa90msg.h"
Lucy McCoy0ca12682007-05-18 12:10:41 -0700197#include "keyspan_usa67msg.h"
Alan Coxdeb91682008-07-22 11:13:08 +0100198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/* Functions used by new usb-serial code. */
Alan Coxdeb91682008-07-22 11:13:08 +0100201static int __init keyspan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int retval;
204 retval = usb_serial_register(&keyspan_pre_device);
205 if (retval)
206 goto failed_pre_device_register;
207 retval = usb_serial_register(&keyspan_1port_device);
208 if (retval)
209 goto failed_1port_device_register;
210 retval = usb_serial_register(&keyspan_2port_device);
211 if (retval)
212 goto failed_2port_device_register;
213 retval = usb_serial_register(&keyspan_4port_device);
214 if (retval)
215 goto failed_4port_device_register;
216 retval = usb_register(&keyspan_driver);
Alan Coxdeb91682008-07-22 11:13:08 +0100217 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 goto failed_usb_register;
219
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700220 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
221 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 return 0;
224failed_usb_register:
225 usb_serial_deregister(&keyspan_4port_device);
226failed_4port_device_register:
227 usb_serial_deregister(&keyspan_2port_device);
228failed_2port_device_register:
229 usb_serial_deregister(&keyspan_1port_device);
230failed_1port_device_register:
231 usb_serial_deregister(&keyspan_pre_device);
232failed_pre_device_register:
233 return retval;
234}
235
Alan Coxdeb91682008-07-22 11:13:08 +0100236static void __exit keyspan_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Alan Coxdeb91682008-07-22 11:13:08 +0100238 usb_deregister(&keyspan_driver);
239 usb_serial_deregister(&keyspan_pre_device);
240 usb_serial_deregister(&keyspan_1port_device);
241 usb_serial_deregister(&keyspan_2port_device);
242 usb_serial_deregister(&keyspan_4port_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245module_init(keyspan_init);
246module_exit(keyspan_exit);
247
Alan Cox95da3102008-07-22 11:09:07 +0100248static void keyspan_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Alan Cox95da3102008-07-22 11:09:07 +0100250 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct keyspan_port_private *p_priv;
252
Alan Coxdeb91682008-07-22 11:13:08 +0100253 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
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
Alan Coxdeb91682008-07-22 11:13:08 +0100266static void keyspan_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100267 struct usb_serial_port *port, 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
Harvey Harrison441b62c2008-03-03 16:08:34 -0800274 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 p_priv = usb_get_serial_port_data(port);
277 d_details = p_priv->device_details;
Alan Cox74240b02007-10-18 01:24:20 -0700278 cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 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. */
Alan Cox74240b02007-10-18 01:24:20 -0700283 baud_rate = tty_get_baud_rate(tty);
Alan Coxdeb91682008-07-22 11:13:08 +0100284 /* If no match or invalid, don't change */
Alan Cox74240b02007-10-18 01:24:20 -0700285 if (d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
287 /* FIXME - more to do here to ensure rate changes cleanly */
Alan Cox74240b02007-10-18 01:24:20 -0700288 /* FIXME - calcuate exact rate from divisor ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 p_priv->baud = baud_rate;
Alan Cox74240b02007-10-18 01:24:20 -0700290 } else
291 baud_rate = tty_termios_baud_rate(old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Alan Cox74240b02007-10-18 01:24:20 -0700293 tty_encode_baud_rate(tty, baud_rate, baud_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /* set CTS/RTS handshake etc. */
295 p_priv->cflag = cflag;
296 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
297
Alan Cox74240b02007-10-18 01:24:20 -0700298 /* Mark/Space not supported */
299 tty->termios->c_cflag &= ~CMSPAR;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 keyspan_send_setup(port, 0);
302}
303
Alan Cox95da3102008-07-22 11:09:07 +0100304static int keyspan_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Alan Cox95da3102008-07-22 11:09:07 +0100306 struct usb_serial_port *port = tty->driver_data;
307 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned int value;
Alan Coxdeb91682008-07-22 11:13:08 +0100309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 value = ((p_priv->rts_state) ? TIOCM_RTS : 0) |
311 ((p_priv->dtr_state) ? TIOCM_DTR : 0) |
312 ((p_priv->cts_state) ? TIOCM_CTS : 0) |
313 ((p_priv->dsr_state) ? TIOCM_DSR : 0) |
314 ((p_priv->dcd_state) ? TIOCM_CAR : 0) |
Alan Coxdeb91682008-07-22 11:13:08 +0100315 ((p_priv->ri_state) ? TIOCM_RNG : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 return value;
318}
319
Alan Cox95da3102008-07-22 11:09:07 +0100320static int keyspan_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 unsigned int set, unsigned int clear)
322{
Alan Cox95da3102008-07-22 11:09:07 +0100323 struct usb_serial_port *port = tty->driver_data;
324 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
Alan Coxdeb91682008-07-22 11:13:08 +0100325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (set & TIOCM_RTS)
327 p_priv->rts_state = 1;
328 if (set & TIOCM_DTR)
329 p_priv->dtr_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (clear & TIOCM_RTS)
331 p_priv->rts_state = 0;
332 if (clear & TIOCM_DTR)
333 p_priv->dtr_state = 0;
334 keyspan_send_setup(port, 0);
335 return 0;
336}
337
Alan Cox95da3102008-07-22 11:09:07 +0100338/* Write function is similar for the four protocols used
339 with only a minor change for usa90 (usa19hs) required */
340static int keyspan_write(struct tty_struct *tty,
341 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 struct keyspan_port_private *p_priv;
344 const struct keyspan_device_details *d_details;
345 int flip;
346 int left, todo;
347 struct urb *this_urb;
Alan Coxdeb91682008-07-22 11:13:08 +0100348 int err, maxDataLen, dataOffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 p_priv = usb_get_serial_port_data(port);
351 d_details = p_priv->device_details;
352
353 if (d_details->msg_format == msg_usa90) {
Alan Coxdeb91682008-07-22 11:13:08 +0100354 maxDataLen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dataOffset = 0;
356 } else {
357 maxDataLen = 63;
358 dataOffset = 1;
359 }
Alan Coxdeb91682008-07-22 11:13:08 +0100360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dbg("%s - for port %d (%d chars), flip=%d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800362 __func__, port->number, count, p_priv->out_flip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 for (left = count; left > 0; left -= todo) {
365 todo = left;
366 if (todo > maxDataLen)
367 todo = maxDataLen;
368
369 flip = p_priv->out_flip;
Alan Coxdeb91682008-07-22 11:13:08 +0100370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* Check we have a valid urb/endpoint before we use it... */
Alan Coxdeb91682008-07-22 11:13:08 +0100372 this_urb = p_priv->out_urbs[flip];
373 if (this_urb == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* no bulk out, so return 0 bytes written */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800375 dbg("%s - no output urb :(", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return count;
377 }
378
Alan Coxdeb91682008-07-22 11:13:08 +0100379 dbg("%s - endpoint %d flip %d",
380 __func__, usb_pipeendpoint(this_urb->pipe), flip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (this_urb->status == -EINPROGRESS) {
Alan Coxdeb91682008-07-22 11:13:08 +0100383 if (time_before(jiffies,
384 p_priv->tx_start_time[flip] + 10 * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 usb_unlink_urb(this_urb);
387 break;
388 }
389
Alan Coxdeb91682008-07-22 11:13:08 +0100390 /* First byte in buffer is "last flag" (except for usa19hx)
391 - unused so for now so set to zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 ((char *)this_urb->transfer_buffer)[0] = 0;
393
Alan Coxdeb91682008-07-22 11:13:08 +0100394 memcpy(this_urb->transfer_buffer + dataOffset, buf, todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 buf += todo;
396
397 /* send the data out the bulk port */
398 this_urb->transfer_buffer_length = todo + dataOffset;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 this_urb->dev = port->serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +0100401 err = usb_submit_urb(this_urb, GFP_ATOMIC);
402 if (err != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dbg("usb_submit_urb(write bulk) failed (%d)", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 p_priv->tx_start_time[flip] = jiffies;
405
406 /* Flip for next time if usa26 or usa28 interface
407 (not used on usa49) */
408 p_priv->out_flip = (flip + 1) & d_details->outdat_endp_flip;
409 }
410
411 return count - left;
412}
413
David Howells7d12e782006-10-05 14:55:46 +0100414static void usa26_indat_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 int i, err;
417 int endpoint;
418 struct usb_serial_port *port;
419 struct tty_struct *tty;
420 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700421 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Alan Coxdeb91682008-07-22 11:13:08 +0100423 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 endpoint = usb_pipeendpoint(urb->pipe);
426
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700427 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 dbg("%s - nonzero status: %x on endpoint %d.",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800429 __func__, status, endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return;
431 }
432
Ming Leicdc97792008-02-24 18:41:47 +0800433 port = urb->context;
Alan Cox4a90f092008-10-13 10:39:46 +0100434 tty = tty_port_tty_get(&port->port);
Alan Coxa5569a52008-01-21 17:18:24 -0800435 if (tty && urb->actual_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* 0x80 bit is error flag */
437 if ((data[0] & 0x80) == 0) {
Alan Coxdeb91682008-07-22 11:13:08 +0100438 /* no errors on individual bytes, only
439 possible overrun err */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (data[0] & RXERROR_OVERRUN)
Alan Coxdeb91682008-07-22 11:13:08 +0100441 err = TTY_OVERRUN;
442 else
443 err = 0;
444 for (i = 1; i < urb->actual_length ; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 tty_insert_flip_char(tty, data[i], err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 } else {
447 /* some bytes had errors, every byte has status */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800448 dbg("%s - RX error!!!!", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 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 }
Alan Cox4a90f092008-10-13 10:39:46 +0100463 tty_kref_put(tty);
Alan Coxdeb91682008-07-22 11:13:08 +0100464
465 /* Resubmit urb so we continue receiving */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 urb->dev = port->serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +0100467 if (port->port.count) {
468 err = usb_submit_urb(urb, GFP_ATOMIC);
469 if (err != 0)
470 dbg("%s - resubmit read urb failed. (%d)",
471 __func__, err);
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return;
474}
475
Alan Coxdeb91682008-07-22 11:13:08 +0100476/* Outdat handling is common for all devices */
David Howells7d12e782006-10-05 14:55:46 +0100477static void usa2x_outdat_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct usb_serial_port *port;
480 struct keyspan_port_private *p_priv;
481
Ming Leicdc97792008-02-24 18:41:47 +0800482 port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 p_priv = usb_get_serial_port_data(port);
Alan Coxdeb91682008-07-22 11:13:08 +0100484 dbg("%s - urb %d", __func__, urb == p_priv->out_urbs[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Alan Cox95da3102008-07-22 11:09:07 +0100486 if (port->port.count)
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700487 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
David Howells7d12e782006-10-05 14:55:46 +0100490static void usa26_inack_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Alan Coxdeb91682008-07-22 11:13:08 +0100492 dbg("%s", __func__);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
David Howells7d12e782006-10-05 14:55:46 +0100496static void usa26_outcont_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct usb_serial_port *port;
499 struct keyspan_port_private *p_priv;
500
Ming Leicdc97792008-02-24 18:41:47 +0800501 port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 p_priv = usb_get_serial_port_data(port);
503
504 if (p_priv->resend_cont) {
Alan Coxdeb91682008-07-22 11:13:08 +0100505 dbg("%s - sending setup", __func__);
506 keyspan_usa26_send_setup(port->serial, port,
507 p_priv->resend_cont - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509}
510
David Howells7d12e782006-10-05 14:55:46 +0100511static void usa26_instat_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 unsigned char *data = urb->transfer_buffer;
514 struct keyspan_usa26_portStatusMessage *msg;
515 struct usb_serial *serial;
516 struct usb_serial_port *port;
517 struct keyspan_port_private *p_priv;
Alan Cox4a90f092008-10-13 10:39:46 +0100518 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int old_dcd_state, err;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700520 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Ming Leicdc97792008-02-24 18:41:47 +0800522 serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700524 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800525 dbg("%s - nonzero status: %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return;
527 }
528 if (urb->actual_length != 9) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800529 dbg("%s - %d byte report??", __func__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto exit;
531 }
532
533 msg = (struct keyspan_usa26_portStatusMessage *)data;
534
535#if 0
536 dbg("%s - port status: port %d cts %d dcd %d dsr %d ri %d toff %d txoff %d rxen %d cr %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800537 __func__, msg->port, msg->hskia_cts, msg->gpia_dcd, msg->dsr, msg->ri, msg->_txOff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 msg->_txXoff, msg->rxEnabled, msg->controlResponse);
539#endif
540
541 /* Now do something useful with the data */
542
543
Alan Coxdeb91682008-07-22 11:13:08 +0100544 /* Check port number from message and retrieve private data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (msg->port >= serial->num_ports) {
Alan Coxdeb91682008-07-22 11:13:08 +0100546 dbg("%s - Unexpected port number %d", __func__, msg->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 goto exit;
548 }
549 port = serial->port[msg->port];
550 p_priv = usb_get_serial_port_data(port);
Alan Coxdeb91682008-07-22 11:13:08 +0100551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* Update handshaking pin state information */
553 old_dcd_state = p_priv->dcd_state;
554 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
555 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
556 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
557 p_priv->ri_state = ((msg->ri) ? 1 : 0);
558
Alan Cox4a90f092008-10-13 10:39:46 +0100559 if (old_dcd_state != p_priv->dcd_state) {
560 tty = tty_port_tty_get(&port->port);
561 if (tty && !C_CLOCAL(tty))
562 tty_hangup(tty);
563 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Alan Coxdeb91682008-07-22 11:13:08 +0100565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* Resubmit urb so we continue receiving */
567 urb->dev = serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +0100568 err = usb_submit_urb(urb, GFP_ATOMIC);
569 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800570 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571exit: ;
572}
573
David Howells7d12e782006-10-05 14:55:46 +0100574static void usa26_glocont_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Alan Coxdeb91682008-07-22 11:13:08 +0100576 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579
David Howells7d12e782006-10-05 14:55:46 +0100580static void usa28_indat_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Alan Coxf035a8a2008-07-22 11:13:32 +0100582 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct usb_serial_port *port;
584 struct tty_struct *tty;
585 unsigned char *data;
586 struct keyspan_port_private *p_priv;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700587 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Alan Coxdeb91682008-07-22 11:13:08 +0100589 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Ming Leicdc97792008-02-24 18:41:47 +0800591 port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 p_priv = usb_get_serial_port_data(port);
593 data = urb->transfer_buffer;
594
595 if (urb != p_priv->in_urbs[p_priv->in_flip])
596 return;
597
598 do {
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700599 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 dbg("%s - nonzero status: %x on endpoint %d.",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800601 __func__, status, usb_pipeendpoint(urb->pipe));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return;
603 }
604
Ming Leicdc97792008-02-24 18:41:47 +0800605 port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 p_priv = usb_get_serial_port_data(port);
607 data = urb->transfer_buffer;
608
Alan Cox4a90f092008-10-13 10:39:46 +0100609 tty =tty_port_tty_get(&port->port);
610 if (tty && urb->actual_length) {
Alan Coxf035a8a2008-07-22 11:13:32 +0100611 tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 tty_flip_buffer_push(tty);
613 }
Alan Cox4a90f092008-10-13 10:39:46 +0100614 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /* Resubmit urb so we continue receiving */
617 urb->dev = port->serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +0100618 if (port->port.count) {
619 err = usb_submit_urb(urb, GFP_ATOMIC);
620 if (err != 0)
621 dbg("%s - resubmit read urb failed. (%d)",
622 __func__, err);
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 p_priv->in_flip ^= 1;
625
626 urb = p_priv->in_urbs[p_priv->in_flip];
627 } while (urb->status != -EINPROGRESS);
628}
629
David Howells7d12e782006-10-05 14:55:46 +0100630static void usa28_inack_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Alan Coxdeb91682008-07-22 11:13:08 +0100632 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
David Howells7d12e782006-10-05 14:55:46 +0100635static void usa28_outcont_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct usb_serial_port *port;
638 struct keyspan_port_private *p_priv;
639
Ming Leicdc97792008-02-24 18:41:47 +0800640 port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 p_priv = usb_get_serial_port_data(port);
642
643 if (p_priv->resend_cont) {
Alan Coxdeb91682008-07-22 11:13:08 +0100644 dbg("%s - sending setup", __func__);
645 keyspan_usa28_send_setup(port->serial, port,
646 p_priv->resend_cont - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648}
649
David Howells7d12e782006-10-05 14:55:46 +0100650static void usa28_instat_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 int err;
653 unsigned char *data = urb->transfer_buffer;
654 struct keyspan_usa28_portStatusMessage *msg;
655 struct usb_serial *serial;
656 struct usb_serial_port *port;
657 struct keyspan_port_private *p_priv;
Alan Cox4a90f092008-10-13 10:39:46 +0100658 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int old_dcd_state;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700660 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Ming Leicdc97792008-02-24 18:41:47 +0800662 serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700664 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800665 dbg("%s - nonzero status: %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return;
667 }
668
669 if (urb->actual_length != sizeof(struct keyspan_usa28_portStatusMessage)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800670 dbg("%s - bad length %d", __func__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto exit;
672 }
673
Harvey Harrison441b62c2008-03-03 16:08:34 -0800674 /*dbg("%s %x %x %x %x %x %x %x %x %x %x %x %x", __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 data[0], data[1], data[2], data[3], data[4], data[5],
676 data[6], data[7], data[8], data[9], data[10], data[11]);*/
Alan Coxdeb91682008-07-22 11:13:08 +0100677
678 /* Now do something useful with the data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 msg = (struct keyspan_usa28_portStatusMessage *)data;
680
Alan Coxdeb91682008-07-22 11:13:08 +0100681 /* Check port number from message and retrieve private data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (msg->port >= serial->num_ports) {
Alan Coxdeb91682008-07-22 11:13:08 +0100683 dbg("%s - Unexpected port number %d", __func__, msg->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 goto exit;
685 }
686 port = serial->port[msg->port];
687 p_priv = usb_get_serial_port_data(port);
Alan Coxdeb91682008-07-22 11:13:08 +0100688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Update handshaking pin state information */
690 old_dcd_state = p_priv->dcd_state;
691 p_priv->cts_state = ((msg->cts) ? 1 : 0);
692 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
693 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
694 p_priv->ri_state = ((msg->ri) ? 1 : 0);
695
Alan Cox4a90f092008-10-13 10:39:46 +0100696 if( old_dcd_state != p_priv->dcd_state && old_dcd_state) {
697 tty = tty_port_tty_get(&port->port);
698 if (tty && !C_CLOCAL(tty))
699 tty_hangup(tty);
700 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 /* Resubmit urb so we continue receiving */
704 urb->dev = serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +0100705 err = usb_submit_urb(urb, GFP_ATOMIC);
706 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800707 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708exit: ;
709}
710
David Howells7d12e782006-10-05 14:55:46 +0100711static void usa28_glocont_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Alan Coxdeb91682008-07-22 11:13:08 +0100713 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716
David Howells7d12e782006-10-05 14:55:46 +0100717static void usa49_glocont_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 struct usb_serial *serial;
720 struct usb_serial_port *port;
721 struct keyspan_port_private *p_priv;
722 int i;
723
Alan Coxdeb91682008-07-22 11:13:08 +0100724 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Ming Leicdc97792008-02-24 18:41:47 +0800726 serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 for (i = 0; i < serial->num_ports; ++i) {
728 port = serial->port[i];
729 p_priv = usb_get_serial_port_data(port);
730
731 if (p_priv->resend_cont) {
Alan Coxdeb91682008-07-22 11:13:08 +0100732 dbg("%s - sending setup", __func__);
733 keyspan_usa49_send_setup(serial, port,
734 p_priv->resend_cont - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
736 }
737 }
738}
739
740 /* This is actually called glostat in the Keyspan
741 doco */
David Howells7d12e782006-10-05 14:55:46 +0100742static void usa49_instat_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 int err;
745 unsigned char *data = urb->transfer_buffer;
746 struct keyspan_usa49_portStatusMessage *msg;
747 struct usb_serial *serial;
748 struct usb_serial_port *port;
749 struct keyspan_port_private *p_priv;
750 int old_dcd_state;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700751 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Alan Coxdeb91682008-07-22 11:13:08 +0100753 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Ming Leicdc97792008-02-24 18:41:47 +0800755 serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700757 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800758 dbg("%s - nonzero status: %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return;
760 }
761
Alan Coxdeb91682008-07-22 11:13:08 +0100762 if (urb->actual_length !=
763 sizeof(struct keyspan_usa49_portStatusMessage)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800764 dbg("%s - bad length %d", __func__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 goto exit;
766 }
767
Harvey Harrison441b62c2008-03-03 16:08:34 -0800768 /*dbg(" %x %x %x %x %x %x %x %x %x %x %x", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 data[0], data[1], data[2], data[3], data[4], data[5],
770 data[6], data[7], data[8], data[9], data[10]);*/
Alan Coxdeb91682008-07-22 11:13:08 +0100771
772 /* Now do something useful with the data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 msg = (struct keyspan_usa49_portStatusMessage *)data;
774
Alan Coxdeb91682008-07-22 11:13:08 +0100775 /* Check port number from message and retrieve private data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (msg->portNumber >= serial->num_ports) {
Alan Coxdeb91682008-07-22 11:13:08 +0100777 dbg("%s - Unexpected port number %d",
778 __func__, msg->portNumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto exit;
780 }
781 port = serial->port[msg->portNumber];
782 p_priv = usb_get_serial_port_data(port);
Alan Coxdeb91682008-07-22 11:13:08 +0100783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /* Update handshaking pin state information */
785 old_dcd_state = p_priv->dcd_state;
786 p_priv->cts_state = ((msg->cts) ? 1 : 0);
787 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
788 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
789 p_priv->ri_state = ((msg->ri) ? 1 : 0);
790
Alan Cox4a90f092008-10-13 10:39:46 +0100791 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
792 struct tty_struct *tty = tty_port_tty_get(&port->port);
793 if (tty && !C_CLOCAL(tty))
794 tty_hangup(tty);
795 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
Alan Coxdeb91682008-07-22 11:13:08 +0100798 /* Resubmit urb so we continue receiving */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 urb->dev = serial->dev;
800
Alan Coxdeb91682008-07-22 11:13:08 +0100801 err = usb_submit_urb(urb, GFP_ATOMIC);
802 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800803 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804exit: ;
805}
806
David Howells7d12e782006-10-05 14:55:46 +0100807static void usa49_inack_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Alan Coxdeb91682008-07-22 11:13:08 +0100809 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
David Howells7d12e782006-10-05 14:55:46 +0100812static void usa49_indat_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 int i, err;
815 int endpoint;
816 struct usb_serial_port *port;
817 struct tty_struct *tty;
818 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700819 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Alan Coxdeb91682008-07-22 11:13:08 +0100821 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 endpoint = usb_pipeendpoint(urb->pipe);
824
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700825 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800826 dbg("%s - nonzero status: %x on endpoint %d.", __func__,
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700827 status, endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return;
829 }
830
Ming Leicdc97792008-02-24 18:41:47 +0800831 port = urb->context;
Alan Cox4a90f092008-10-13 10:39:46 +0100832 tty = tty_port_tty_get(&port->port);
Alan Cox3004e532008-01-03 16:59:04 +0000833 if (tty && urb->actual_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* 0x80 bit is error flag */
835 if ((data[0] & 0x80) == 0) {
836 /* no error on any byte */
Alan Coxf035a8a2008-07-22 11:13:32 +0100837 tty_insert_flip_string(tty, data + 1,
838 urb->actual_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 } else {
840 /* some bytes had errors, every byte has status */
841 for (i = 0; i + 1 < urb->actual_length; i += 2) {
842 int stat = data[i], flag = 0;
843 if (stat & RXERROR_OVERRUN)
844 flag |= TTY_OVERRUN;
845 if (stat & RXERROR_FRAMING)
846 flag |= TTY_FRAME;
847 if (stat & RXERROR_PARITY)
848 flag |= TTY_PARITY;
849 /* XXX should handle break (0x10) */
850 tty_insert_flip_char(tty, data[i+1], flag);
851 }
852 }
853 tty_flip_buffer_push(tty);
854 }
Alan Cox4a90f092008-10-13 10:39:46 +0100855 tty_kref_put(tty);
Alan Coxdeb91682008-07-22 11:13:08 +0100856
857 /* Resubmit urb so we continue receiving */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 urb->dev = port->serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +0100859 if (port->port.count) {
860 err = usb_submit_urb(urb, GFP_ATOMIC);
861 if (err != 0)
862 dbg("%s - resubmit read urb failed. (%d)",
863 __func__, err);
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Lucy McCoy0ca12682007-05-18 12:10:41 -0700867static void usa49wg_indat_callback(struct urb *urb)
868{
869 int i, len, x, err;
870 struct usb_serial *serial;
871 struct usb_serial_port *port;
872 struct tty_struct *tty;
873 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700874 int status = urb->status;
Lucy McCoy0ca12682007-05-18 12:10:41 -0700875
Alan Coxdeb91682008-07-22 11:13:08 +0100876 dbg("%s", __func__);
Lucy McCoy0ca12682007-05-18 12:10:41 -0700877
878 serial = urb->context;
879
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700880 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800881 dbg("%s - nonzero status: %x", __func__, status);
Lucy McCoy0ca12682007-05-18 12:10:41 -0700882 return;
883 }
884
885 /* inbound data is in the form P#, len, status, data */
886 i = 0;
887 len = 0;
888
889 if (urb->actual_length) {
890 while (i < urb->actual_length) {
891
892 /* Check port number from message*/
893 if (data[i] >= serial->num_ports) {
Alan Coxdeb91682008-07-22 11:13:08 +0100894 dbg("%s - Unexpected port number %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800895 __func__, data[i]);
Lucy McCoy0ca12682007-05-18 12:10:41 -0700896 return;
897 }
898 port = serial->port[data[i++]];
Alan Cox4a90f092008-10-13 10:39:46 +0100899 tty = tty_port_tty_get(&port->port);
Lucy McCoy0ca12682007-05-18 12:10:41 -0700900 len = data[i++];
901
902 /* 0x80 bit is error flag */
903 if ((data[i] & 0x80) == 0) {
904 /* no error on any byte */
905 i++;
906 for (x = 1; x < len ; ++x)
Alan Cox95da3102008-07-22 11:09:07 +0100907 if (port->port.count)
Lucy McCoy0ca12682007-05-18 12:10:41 -0700908 tty_insert_flip_char(tty,
909 data[i++], 0);
910 else
911 i++;
912 } else {
913 /*
914 * some bytes had errors, every byte has status
915 */
916 for (x = 0; x + 1 < len; x += 2) {
917 int stat = data[i], flag = 0;
918 if (stat & RXERROR_OVERRUN)
919 flag |= TTY_OVERRUN;
920 if (stat & RXERROR_FRAMING)
921 flag |= TTY_FRAME;
922 if (stat & RXERROR_PARITY)
923 flag |= TTY_PARITY;
924 /* XXX should handle break (0x10) */
Alan Cox95da3102008-07-22 11:09:07 +0100925 if (port->port.count)
Lucy McCoy0ca12682007-05-18 12:10:41 -0700926 tty_insert_flip_char(tty,
927 data[i+1], flag);
928 i += 2;
929 }
930 }
Alan Cox95da3102008-07-22 11:09:07 +0100931 if (port->port.count)
Lucy McCoy0ca12682007-05-18 12:10:41 -0700932 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +0100933 tty_kref_put(tty);
Lucy McCoy0ca12682007-05-18 12:10:41 -0700934 }
935 }
936
937 /* Resubmit urb so we continue receiving */
938 urb->dev = serial->dev;
939
940 err = usb_submit_urb(urb, GFP_ATOMIC);
941 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -0800942 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Lucy McCoy0ca12682007-05-18 12:10:41 -0700943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/* not used, usa-49 doesn't have per-port control endpoints */
Lucy McCoy0ca12682007-05-18 12:10:41 -0700946static void usa49_outcont_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Alan Coxdeb91682008-07-22 11:13:08 +0100948 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Lucy McCoy0ca12682007-05-18 12:10:41 -0700951static void usa90_indat_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 int i, err;
954 int endpoint;
955 struct usb_serial_port *port;
956 struct keyspan_port_private *p_priv;
957 struct tty_struct *tty;
958 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700959 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Alan Coxdeb91682008-07-22 11:13:08 +0100961 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 endpoint = usb_pipeendpoint(urb->pipe);
964
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -0700965 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 dbg("%s - nonzero status: %x on endpoint %d.",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800967 __func__, status, endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return;
969 }
970
Ming Leicdc97792008-02-24 18:41:47 +0800971 port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 p_priv = usb_get_serial_port_data(port);
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (urb->actual_length) {
Alan Cox4a90f092008-10-13 10:39:46 +0100975 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* if current mode is DMA, looks like usa28 format
Alan Coxdeb91682008-07-22 11:13:08 +0100977 otherwise looks like usa26 data format */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Alan Coxf035a8a2008-07-22 11:13:32 +0100979 if (p_priv->baud > 57600)
980 tty_insert_flip_string(tty, data, urb->actual_length);
981 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* 0x80 bit is error flag */
983 if ((data[0] & 0x80) == 0) {
Alan Coxdeb91682008-07-22 11:13:08 +0100984 /* no errors on individual bytes, only
985 possible overrun err*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (data[0] & RXERROR_OVERRUN)
Alan Coxdeb91682008-07-22 11:13:08 +0100987 err = TTY_OVERRUN;
988 else
989 err = 0;
990 for (i = 1; i < urb->actual_length ; ++i)
991 tty_insert_flip_char(tty, data[i],
992 err);
993 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* some bytes had errors, every byte has status */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800995 dbg("%s - RX error!!!!", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 for (i = 0; i + 1 < urb->actual_length; i += 2) {
997 int stat = data[i], flag = 0;
998 if (stat & RXERROR_OVERRUN)
999 flag |= TTY_OVERRUN;
1000 if (stat & RXERROR_FRAMING)
1001 flag |= TTY_FRAME;
1002 if (stat & RXERROR_PARITY)
1003 flag |= TTY_PARITY;
1004 /* XXX should handle break (0x10) */
Alan Coxdeb91682008-07-22 11:13:08 +01001005 tty_insert_flip_char(tty, data[i+1],
1006 flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008 }
1009 }
1010 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001011 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Alan Coxdeb91682008-07-22 11:13:08 +01001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* Resubmit urb so we continue receiving */
1015 urb->dev = port->serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +01001016 if (port->port.count) {
1017 err = usb_submit_urb(urb, GFP_ATOMIC);
1018 if (err != 0)
1019 dbg("%s - resubmit read urb failed. (%d)",
1020 __func__, err);
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return;
1023}
1024
1025
David Howells7d12e782006-10-05 14:55:46 +01001026static void usa90_instat_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 unsigned char *data = urb->transfer_buffer;
1029 struct keyspan_usa90_portStatusMessage *msg;
1030 struct usb_serial *serial;
1031 struct usb_serial_port *port;
1032 struct keyspan_port_private *p_priv;
Alan Cox4a90f092008-10-13 10:39:46 +01001033 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int old_dcd_state, err;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -07001035 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Ming Leicdc97792008-02-24 18:41:47 +08001037 serial = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -07001039 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001040 dbg("%s - nonzero status: %x", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return;
1042 }
1043 if (urb->actual_length < 14) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001044 dbg("%s - %d byte report??", __func__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 goto exit;
1046 }
1047
1048 msg = (struct keyspan_usa90_portStatusMessage *)data;
1049
1050 /* Now do something useful with the data */
1051
1052 port = serial->port[0];
1053 p_priv = usb_get_serial_port_data(port);
Alan Coxdeb91682008-07-22 11:13:08 +01001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* Update handshaking pin state information */
1056 old_dcd_state = p_priv->dcd_state;
1057 p_priv->cts_state = ((msg->cts) ? 1 : 0);
1058 p_priv->dsr_state = ((msg->dsr) ? 1 : 0);
1059 p_priv->dcd_state = ((msg->dcd) ? 1 : 0);
1060 p_priv->ri_state = ((msg->ri) ? 1 : 0);
1061
Alan Cox4a90f092008-10-13 10:39:46 +01001062 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
1063 tty = tty_port_tty_get(&port->port);
1064 if (tty && !C_CLOCAL(tty))
1065 tty_hangup(tty);
1066 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
Alan Coxdeb91682008-07-22 11:13:08 +01001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* Resubmit urb so we continue receiving */
1070 urb->dev = serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +01001071 err = usb_submit_urb(urb, GFP_ATOMIC);
1072 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001073 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074exit:
1075 ;
1076}
1077
David Howells7d12e782006-10-05 14:55:46 +01001078static void usa90_outcont_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 struct usb_serial_port *port;
1081 struct keyspan_port_private *p_priv;
1082
Ming Leicdc97792008-02-24 18:41:47 +08001083 port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 p_priv = usb_get_serial_port_data(port);
1085
1086 if (p_priv->resend_cont) {
Alan Coxdeb91682008-07-22 11:13:08 +01001087 dbg("%s - sending setup", __func__);
1088 keyspan_usa90_send_setup(port->serial, port,
1089 p_priv->resend_cont - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091}
1092
Lucy McCoy0ca12682007-05-18 12:10:41 -07001093/* Status messages from the 28xg */
1094static void usa67_instat_callback(struct urb *urb)
1095{
1096 int err;
1097 unsigned char *data = urb->transfer_buffer;
1098 struct keyspan_usa67_portStatusMessage *msg;
1099 struct usb_serial *serial;
1100 struct usb_serial_port *port;
1101 struct keyspan_port_private *p_priv;
1102 int old_dcd_state;
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -07001103 int status = urb->status;
Lucy McCoy0ca12682007-05-18 12:10:41 -07001104
Alan Coxdeb91682008-07-22 11:13:08 +01001105 dbg("%s", __func__);
Lucy McCoy0ca12682007-05-18 12:10:41 -07001106
1107 serial = urb->context;
1108
Greg Kroah-Hartman95b93452007-06-15 15:44:13 -07001109 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001110 dbg("%s - nonzero status: %x", __func__, status);
Lucy McCoy0ca12682007-05-18 12:10:41 -07001111 return;
1112 }
1113
Alan Coxdeb91682008-07-22 11:13:08 +01001114 if (urb->actual_length !=
1115 sizeof(struct keyspan_usa67_portStatusMessage)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001116 dbg("%s - bad length %d", __func__, urb->actual_length);
Lucy McCoy0ca12682007-05-18 12:10:41 -07001117 return;
1118 }
1119
1120
1121 /* Now do something useful with the data */
1122 msg = (struct keyspan_usa67_portStatusMessage *)data;
1123
1124 /* Check port number from message and retrieve private data */
1125 if (msg->port >= serial->num_ports) {
Alan Coxdeb91682008-07-22 11:13:08 +01001126 dbg("%s - Unexpected port number %d", __func__, msg->port);
Lucy McCoy0ca12682007-05-18 12:10:41 -07001127 return;
1128 }
1129
1130 port = serial->port[msg->port];
1131 p_priv = usb_get_serial_port_data(port);
1132
1133 /* Update handshaking pin state information */
1134 old_dcd_state = p_priv->dcd_state;
1135 p_priv->cts_state = ((msg->hskia_cts) ? 1 : 0);
1136 p_priv->dcd_state = ((msg->gpia_dcd) ? 1 : 0);
1137
Alan Cox4a90f092008-10-13 10:39:46 +01001138 if (old_dcd_state != p_priv->dcd_state && old_dcd_state) {
1139 struct tty_struct *tty = tty_port_tty_get(&port->port);
1140 if (tty && !C_CLOCAL(tty))
1141 tty_hangup(tty);
1142 tty_kref_put(tty);
Lucy McCoy0ca12682007-05-18 12:10:41 -07001143 }
1144
1145 /* Resubmit urb so we continue receiving */
1146 urb->dev = serial->dev;
1147 err = usb_submit_urb(urb, GFP_ATOMIC);
1148 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001149 dbg("%s - resubmit read urb failed. (%d)", __func__, err);
Lucy McCoy0ca12682007-05-18 12:10:41 -07001150}
1151
1152static void usa67_glocont_callback(struct urb *urb)
1153{
1154 struct usb_serial *serial;
1155 struct usb_serial_port *port;
1156 struct keyspan_port_private *p_priv;
1157 int i;
1158
Alan Coxdeb91682008-07-22 11:13:08 +01001159 dbg("%s", __func__);
Lucy McCoy0ca12682007-05-18 12:10:41 -07001160
1161 serial = urb->context;
1162 for (i = 0; i < serial->num_ports; ++i) {
1163 port = serial->port[i];
1164 p_priv = usb_get_serial_port_data(port);
1165
1166 if (p_priv->resend_cont) {
Alan Coxdeb91682008-07-22 11:13:08 +01001167 dbg("%s - sending setup", __func__);
Lucy McCoy0ca12682007-05-18 12:10:41 -07001168 keyspan_usa67_send_setup(serial, port,
1169 p_priv->resend_cont - 1);
1170 break;
1171 }
1172 }
1173}
1174
Alan Cox95da3102008-07-22 11:09:07 +01001175static int keyspan_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Alan Cox95da3102008-07-22 11:09:07 +01001177 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct keyspan_port_private *p_priv;
1179 const struct keyspan_device_details *d_details;
1180 int flip;
1181 int data_len;
1182 struct urb *this_urb;
1183
Harvey Harrison441b62c2008-03-03 16:08:34 -08001184 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 p_priv = usb_get_serial_port_data(port);
1186 d_details = p_priv->device_details;
1187
Alan Coxa5b6f602008-04-08 17:16:06 +01001188 /* FIXME: locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (d_details->msg_format == msg_usa90)
Alan Coxdeb91682008-07-22 11:13:08 +01001190 data_len = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 else
1192 data_len = 63;
1193
1194 flip = p_priv->out_flip;
1195
1196 /* Check both endpoints to see if any are available. */
Alan Coxdeb91682008-07-22 11:13:08 +01001197 this_urb = p_priv->out_urbs[flip];
1198 if (this_urb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (this_urb->status != -EINPROGRESS)
Alan Coxdeb91682008-07-22 11:13:08 +01001200 return data_len;
1201 flip = (flip + 1) & d_details->outdat_endp_flip;
1202 this_urb = p_priv->out_urbs[flip];
1203 if (this_urb != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (this_urb->status != -EINPROGRESS)
Alan Coxdeb91682008-07-22 11:13:08 +01001205 return data_len;
1206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Alan Coxa5b6f602008-04-08 17:16:06 +01001208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211
Alan Cox95da3102008-07-22 11:09:07 +01001212static int keyspan_open(struct tty_struct *tty,
1213 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Andrew Mortonf78ba152007-11-28 16:21:54 -08001215 struct keyspan_port_private *p_priv;
1216 struct keyspan_serial_private *s_priv;
1217 struct usb_serial *serial = port->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 const struct keyspan_device_details *d_details;
1219 int i, err;
Andrew Mortonf78ba152007-11-28 16:21:54 -08001220 int baud_rate, device_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 struct urb *urb;
Alan Cox95da3102008-07-22 11:09:07 +01001222 unsigned int cflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 s_priv = usb_get_serial_data(serial);
1225 p_priv = usb_get_serial_port_data(port);
1226 d_details = p_priv->device_details;
Borislav Petkov7eea4362007-11-14 17:00:39 -08001227
Harvey Harrison441b62c2008-03-03 16:08:34 -08001228 dbg("%s - port%d.", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* Set some sane defaults */
1231 p_priv->rts_state = 1;
1232 p_priv->dtr_state = 1;
1233 p_priv->baud = 9600;
1234
1235 /* force baud and lcr to be set on open */
1236 p_priv->old_baud = 0;
1237 p_priv->old_cflag = 0;
1238
1239 p_priv->out_flip = 0;
1240 p_priv->in_flip = 0;
1241
1242 /* Reset low level data toggle and start reading from endpoints */
1243 for (i = 0; i < 2; i++) {
Alan Coxdeb91682008-07-22 11:13:08 +01001244 urb = p_priv->in_urbs[i];
1245 if (urb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 continue;
1247 urb->dev = serial->dev;
1248
Alan Coxdeb91682008-07-22 11:13:08 +01001249 /* make sure endpoint data toggle is synchronized
1250 with the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 usb_clear_halt(urb->dev, urb->pipe);
Alan Coxdeb91682008-07-22 11:13:08 +01001252 err = usb_submit_urb(urb, GFP_KERNEL);
1253 if (err != 0)
1254 dbg("%s - submit urb %d failed (%d)",
1255 __func__, i, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
1258 /* Reset low level data toggle on out endpoints */
1259 for (i = 0; i < 2; i++) {
Alan Coxdeb91682008-07-22 11:13:08 +01001260 urb = p_priv->out_urbs[i];
1261 if (urb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 continue;
1263 urb->dev = serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +01001264 /* usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
1265 usb_pipeout(urb->pipe), 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
Andrew Mortonf78ba152007-11-28 16:21:54 -08001268 /* get the terminal config for the setup message now so we don't
1269 * need to send 2 of them */
1270
Andrew Mortonf78ba152007-11-28 16:21:54 -08001271 device_port = port->number - port->serial->minor;
Alan Cox95da3102008-07-22 11:09:07 +01001272 if (tty) {
1273 cflag = tty->termios->c_cflag;
1274 /* Baud rate calculation takes baud rate as an integer
1275 so other rates can be generated if desired. */
1276 baud_rate = tty_get_baud_rate(tty);
1277 /* If no match or invalid, leave as default */
1278 if (baud_rate >= 0
1279 && d_details->calculate_baud_rate(baud_rate, d_details->baudclk,
1280 NULL, NULL, NULL, device_port) == KEYSPAN_BAUD_RATE_OK) {
1281 p_priv->baud = baud_rate;
1282 }
Andrew Mortonf78ba152007-11-28 16:21:54 -08001283 }
Andrew Mortonf78ba152007-11-28 16:21:54 -08001284 /* set CTS/RTS handshake etc. */
1285 p_priv->cflag = cflag;
1286 p_priv->flow_control = (cflag & CRTSCTS)? flow_cts: flow_none;
1287
1288 keyspan_send_setup(port, 1);
Alan Coxdeb91682008-07-22 11:13:08 +01001289 /* mdelay(100); */
1290 /* keyspan_set_termios(port, NULL); */
Andrew Mortonf78ba152007-11-28 16:21:54 -08001291
Alan Coxa5b6f602008-04-08 17:16:06 +01001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295static inline void stop_urb(struct urb *urb)
1296{
Greg Kroah-Hartman242cf672005-07-29 16:11:07 -04001297 if (urb && urb->status == -EINPROGRESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 usb_kill_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Alan Cox335f8512009-06-11 12:26:29 +01001301static void keyspan_dtr_rts(struct usb_serial_port *port, int on)
1302{
1303 struct keyspan_port_private *p_priv = usb_get_serial_port_data(port);
1304
1305 p_priv->rts_state = on;
1306 p_priv->dtr_state = on;
1307 keyspan_send_setup(port, 0);
1308}
1309
1310static void keyspan_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 int i;
1313 struct usb_serial *serial = port->serial;
1314 struct keyspan_serial_private *s_priv;
1315 struct keyspan_port_private *p_priv;
1316
Harvey Harrison441b62c2008-03-03 16:08:34 -08001317 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 s_priv = usb_get_serial_data(serial);
1319 p_priv = usb_get_serial_port_data(port);
Alan Coxdeb91682008-07-22 11:13:08 +01001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 p_priv->rts_state = 0;
1322 p_priv->dtr_state = 0;
Alan Coxdeb91682008-07-22 11:13:08 +01001323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (serial->dev) {
1325 keyspan_send_setup(port, 2);
1326 /* pilot-xfer seems to work best with this delay */
1327 mdelay(100);
Alan Coxdeb91682008-07-22 11:13:08 +01001328 /* keyspan_set_termios(port, NULL); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330
1331 /*while (p_priv->outcont_urb->status == -EINPROGRESS) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001332 dbg("%s - urb in progress", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }*/
1334
1335 p_priv->out_flip = 0;
1336 p_priv->in_flip = 0;
1337
1338 if (serial->dev) {
1339 /* Stop reading/writing urbs */
1340 stop_urb(p_priv->inack_urb);
1341 /* stop_urb(p_priv->outcont_urb); */
1342 for (i = 0; i < 2; i++) {
1343 stop_urb(p_priv->in_urbs[i]);
1344 stop_urb(p_priv->out_urbs[i]);
1345 }
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Alan Coxdeb91682008-07-22 11:13:08 +01001349/* download the firmware to a pre-renumeration device */
1350static int keyspan_fake_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 int response;
David Woodhouse2971c572008-05-30 14:04:03 +03001353 const struct ihex_binrec *record;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 char *fw_name;
David Woodhouse2971c572008-05-30 14:04:03 +03001355 const struct firmware *fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 dbg("Keyspan startup version %04x product %04x",
1358 le16_to_cpu(serial->dev->descriptor.bcdDevice),
1359 le16_to_cpu(serial->dev->descriptor.idProduct));
Alan Coxdeb91682008-07-22 11:13:08 +01001360
1361 if ((le16_to_cpu(serial->dev->descriptor.bcdDevice) & 0x8000)
1362 != 0x8000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 dbg("Firmware already loaded. Quitting.");
Alan Coxdeb91682008-07-22 11:13:08 +01001364 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
1367 /* Select firmware image on the basis of idProduct */
1368 switch (le16_to_cpu(serial->dev->descriptor.idProduct)) {
1369 case keyspan_usa28_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001370 fw_name = "keyspan/usa28.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 break;
1372
1373 case keyspan_usa28x_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001374 fw_name = "keyspan/usa28x.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 break;
1376
1377 case keyspan_usa28xa_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001378 fw_name = "keyspan/usa28xa.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 break;
1380
1381 case keyspan_usa28xb_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001382 fw_name = "keyspan/usa28xb.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 break;
1384
1385 case keyspan_usa19_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001386 fw_name = "keyspan/usa19.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 break;
Alan Coxdeb91682008-07-22 11:13:08 +01001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 case keyspan_usa19qi_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001390 fw_name = "keyspan/usa19qi.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
Alan Coxdeb91682008-07-22 11:13:08 +01001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 case keyspan_mpr_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001394 fw_name = "keyspan/mpr.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 break;
1396
1397 case keyspan_usa19qw_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001398 fw_name = "keyspan/usa19qw.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 break;
Alan Coxdeb91682008-07-22 11:13:08 +01001400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 case keyspan_usa18x_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001402 fw_name = "keyspan/usa18x.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 break;
Alan Coxdeb91682008-07-22 11:13:08 +01001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 case keyspan_usa19w_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001406 fw_name = "keyspan/usa19w.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 break;
Alan Coxdeb91682008-07-22 11:13:08 +01001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 case keyspan_usa49w_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001410 fw_name = "keyspan/usa49w.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 break;
1412
1413 case keyspan_usa49wlc_pre_product_id:
David Woodhouse2971c572008-05-30 14:04:03 +03001414 fw_name = "keyspan/usa49wlc.fw";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 break;
1416
1417 default:
David Woodhouse2971c572008-05-30 14:04:03 +03001418 dev_err(&serial->dev->dev, "Unknown product ID (%04x)\n",
1419 le16_to_cpu(serial->dev->descriptor.idProduct));
1420 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422
David Woodhouse2971c572008-05-30 14:04:03 +03001423 if (request_ihex_firmware(&fw, fw_name, &serial->dev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 dev_err(&serial->dev->dev, "Required keyspan firmware image (%s) unavailable.\n", fw_name);
1425 return(1);
1426 }
1427
1428 dbg("Uploading Keyspan %s firmware.", fw_name);
1429
1430 /* download the firmware image */
1431 response = ezusb_set_reset(serial, 1);
1432
David Woodhouse2971c572008-05-30 14:04:03 +03001433 record = (const struct ihex_binrec *)fw->data;
1434
1435 while (record) {
1436 response = ezusb_writememory(serial, be32_to_cpu(record->addr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 (unsigned char *)record->data,
David Woodhouse2971c572008-05-30 14:04:03 +03001438 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (response < 0) {
Alan Coxdeb91682008-07-22 11:13:08 +01001440 dev_err(&serial->dev->dev, "ezusb_writememory failed for Keyspan firmware (%d %04X %p %d)\n",
David Woodhouse2971c572008-05-30 14:04:03 +03001441 response, be32_to_cpu(record->addr),
1442 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 break;
1444 }
David Woodhouse2971c572008-05-30 14:04:03 +03001445 record = ihex_next_binrec(record);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
David Woodhouse2971c572008-05-30 14:04:03 +03001447 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* bring device out of reset. Renumeration will occur in a
1449 moment and the new device will bind to the real driver */
1450 response = ezusb_set_reset(serial, 0);
1451
1452 /* we don't want this device to have a driver assigned to it. */
Alan Coxdeb91682008-07-22 11:13:08 +01001453 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456/* Helper functions used by keyspan_setup_urbs */
Rainer Weikusatfdcba532007-01-03 15:36:25 +01001457static struct usb_endpoint_descriptor const *find_ep(struct usb_serial const *serial,
1458 int endpoint)
1459{
1460 struct usb_host_interface *iface_desc;
1461 struct usb_endpoint_descriptor *ep;
1462 int i;
1463
1464 iface_desc = serial->interface->cur_altsetting;
1465 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1466 ep = &iface_desc->endpoint[i].desc;
1467 if (ep->bEndpointAddress == endpoint)
1468 return ep;
1469 }
1470 dev_warn(&serial->interface->dev, "found no endpoint descriptor for "
1471 "endpoint %x\n", endpoint);
1472 return NULL;
1473}
1474
Alan Coxdeb91682008-07-22 11:13:08 +01001475static struct urb *keyspan_setup_urb(struct usb_serial *serial, int endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 int dir, void *ctx, char *buf, int len,
David Howells7d12e782006-10-05 14:55:46 +01001477 void (*callback)(struct urb *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct urb *urb;
Rainer Weikusatfdcba532007-01-03 15:36:25 +01001480 struct usb_endpoint_descriptor const *ep_desc;
1481 char const *ep_type_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (endpoint == -1)
1484 return NULL; /* endpoint not needed */
1485
Alan Coxdeb91682008-07-22 11:13:08 +01001486 dbg("%s - alloc for endpoint %d.", __func__, endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
1488 if (urb == NULL) {
Alan Coxdeb91682008-07-22 11:13:08 +01001489 dbg("%s - alloc for endpoint %d failed.", __func__, endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return NULL;
1491 }
1492
Lucy McCoy0ca12682007-05-18 12:10:41 -07001493 if (endpoint == 0) {
1494 /* control EP filled in when used */
1495 return urb;
1496 }
1497
Rainer Weikusatfdcba532007-01-03 15:36:25 +01001498 ep_desc = find_ep(serial, endpoint);
1499 if (!ep_desc) {
1500 /* leak the urb, something's wrong and the callers don't care */
1501 return urb;
1502 }
1503 if (usb_endpoint_xfer_int(ep_desc)) {
1504 ep_type_name = "INT";
1505 usb_fill_int_urb(urb, serial->dev,
1506 usb_sndintpipe(serial->dev, endpoint) | dir,
1507 buf, len, callback, ctx,
1508 ep_desc->bInterval);
1509 } else if (usb_endpoint_xfer_bulk(ep_desc)) {
1510 ep_type_name = "BULK";
1511 usb_fill_bulk_urb(urb, serial->dev,
1512 usb_sndbulkpipe(serial->dev, endpoint) | dir,
1513 buf, len, callback, ctx);
1514 } else {
1515 dev_warn(&serial->interface->dev,
1516 "unsupported endpoint type %x\n",
Julia Lawall2e0fe702008-12-29 11:22:14 +01001517 usb_endpoint_type(ep_desc));
Rainer Weikusatfdcba532007-01-03 15:36:25 +01001518 usb_free_urb(urb);
1519 return NULL;
1520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Rainer Weikusatfdcba532007-01-03 15:36:25 +01001522 dbg("%s - using urb %p for %s endpoint %x",
1523 __func__, urb, ep_type_name, endpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return urb;
1525}
1526
1527static struct callbacks {
David Howells7d12e782006-10-05 14:55:46 +01001528 void (*instat_callback)(struct urb *);
1529 void (*glocont_callback)(struct urb *);
1530 void (*indat_callback)(struct urb *);
1531 void (*outdat_callback)(struct urb *);
1532 void (*inack_callback)(struct urb *);
1533 void (*outcont_callback)(struct urb *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534} keyspan_callbacks[] = {
1535 {
1536 /* msg_usa26 callbacks */
1537 .instat_callback = usa26_instat_callback,
1538 .glocont_callback = usa26_glocont_callback,
1539 .indat_callback = usa26_indat_callback,
1540 .outdat_callback = usa2x_outdat_callback,
1541 .inack_callback = usa26_inack_callback,
1542 .outcont_callback = usa26_outcont_callback,
1543 }, {
1544 /* msg_usa28 callbacks */
1545 .instat_callback = usa28_instat_callback,
1546 .glocont_callback = usa28_glocont_callback,
1547 .indat_callback = usa28_indat_callback,
1548 .outdat_callback = usa2x_outdat_callback,
1549 .inack_callback = usa28_inack_callback,
1550 .outcont_callback = usa28_outcont_callback,
1551 }, {
1552 /* msg_usa49 callbacks */
1553 .instat_callback = usa49_instat_callback,
1554 .glocont_callback = usa49_glocont_callback,
1555 .indat_callback = usa49_indat_callback,
1556 .outdat_callback = usa2x_outdat_callback,
1557 .inack_callback = usa49_inack_callback,
1558 .outcont_callback = usa49_outcont_callback,
1559 }, {
1560 /* msg_usa90 callbacks */
1561 .instat_callback = usa90_instat_callback,
Alan Coxdeb91682008-07-22 11:13:08 +01001562 .glocont_callback = usa28_glocont_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 .indat_callback = usa90_indat_callback,
1564 .outdat_callback = usa2x_outdat_callback,
1565 .inack_callback = usa28_inack_callback,
1566 .outcont_callback = usa90_outcont_callback,
Lucy McCoy0ca12682007-05-18 12:10:41 -07001567 }, {
1568 /* msg_usa67 callbacks */
1569 .instat_callback = usa67_instat_callback,
1570 .glocont_callback = usa67_glocont_callback,
1571 .indat_callback = usa26_indat_callback,
1572 .outdat_callback = usa2x_outdat_callback,
1573 .inack_callback = usa26_inack_callback,
1574 .outcont_callback = usa26_outcont_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576};
1577
1578 /* Generic setup urbs function that uses
1579 data in device_details */
1580static void keyspan_setup_urbs(struct usb_serial *serial)
1581{
1582 int i, j;
1583 struct keyspan_serial_private *s_priv;
1584 const struct keyspan_device_details *d_details;
1585 struct usb_serial_port *port;
1586 struct keyspan_port_private *p_priv;
1587 struct callbacks *cback;
1588 int endp;
1589
Alan Coxdeb91682008-07-22 11:13:08 +01001590 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 s_priv = usb_get_serial_data(serial);
1593 d_details = s_priv->device_details;
1594
Alan Coxdeb91682008-07-22 11:13:08 +01001595 /* Setup values for the various callback routines */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 cback = &keyspan_callbacks[d_details->msg_format];
1597
Alan Coxdeb91682008-07-22 11:13:08 +01001598 /* Allocate and set up urbs for each one that is in use,
1599 starting with instat endpoints */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 s_priv->instat_urb = keyspan_setup_urb
1601 (serial, d_details->instat_endpoint, USB_DIR_IN,
1602 serial, s_priv->instat_buf, INSTAT_BUFLEN,
1603 cback->instat_callback);
1604
Lucy McCoy0ca12682007-05-18 12:10:41 -07001605 s_priv->indat_urb = keyspan_setup_urb
1606 (serial, d_details->indat_endpoint, USB_DIR_IN,
1607 serial, s_priv->indat_buf, INDAT49W_BUFLEN,
1608 usa49wg_indat_callback);
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 s_priv->glocont_urb = keyspan_setup_urb
1611 (serial, d_details->glocont_endpoint, USB_DIR_OUT,
1612 serial, s_priv->glocont_buf, GLOCONT_BUFLEN,
1613 cback->glocont_callback);
1614
Alan Coxdeb91682008-07-22 11:13:08 +01001615 /* Setup endpoints for each port specific thing */
1616 for (i = 0; i < d_details->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 port = serial->port[i];
1618 p_priv = usb_get_serial_port_data(port);
1619
1620 /* Do indat endpoints first, once for each flip */
1621 endp = d_details->indat_endpoints[i];
1622 for (j = 0; j <= d_details->indat_endp_flip; ++j, ++endp) {
1623 p_priv->in_urbs[j] = keyspan_setup_urb
1624 (serial, endp, USB_DIR_IN, port,
1625 p_priv->in_buffer[j], 64,
1626 cback->indat_callback);
1627 }
1628 for (; j < 2; ++j)
1629 p_priv->in_urbs[j] = NULL;
1630
1631 /* outdat endpoints also have flip */
1632 endp = d_details->outdat_endpoints[i];
1633 for (j = 0; j <= d_details->outdat_endp_flip; ++j, ++endp) {
1634 p_priv->out_urbs[j] = keyspan_setup_urb
1635 (serial, endp, USB_DIR_OUT, port,
1636 p_priv->out_buffer[j], 64,
1637 cback->outdat_callback);
1638 }
1639 for (; j < 2; ++j)
1640 p_priv->out_urbs[j] = NULL;
1641
1642 /* inack endpoint */
1643 p_priv->inack_urb = keyspan_setup_urb
1644 (serial, d_details->inack_endpoints[i], USB_DIR_IN,
1645 port, p_priv->inack_buffer, 1, cback->inack_callback);
1646
1647 /* outcont endpoint */
1648 p_priv->outcont_urb = keyspan_setup_urb
1649 (serial, d_details->outcont_endpoints[i], USB_DIR_OUT,
1650 port, p_priv->outcont_buffer, 64,
1651 cback->outcont_callback);
Alan Coxdeb91682008-07-22 11:13:08 +01001652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
1655/* usa19 function doesn't require prescaler */
1656static int keyspan_usa19_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1657 u8 *rate_low, u8 *prescaler, int portnum)
1658{
1659 u32 b16, /* baud rate times 16 (actual rate used internally) */
Alan Coxdeb91682008-07-22 11:13:08 +01001660 div, /* divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 cnt; /* inverse of divisor (programmed into 8051) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Alan Coxdeb91682008-07-22 11:13:08 +01001663 dbg("%s - %d.", __func__, baud_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Alan Coxdeb91682008-07-22 11:13:08 +01001665 /* prevent divide by zero... */
1666 b16 = baud_rate * 16L;
1667 if (b16 == 0)
1668 return KEYSPAN_INVALID_BAUD_RATE;
1669 /* Any "standard" rate over 57k6 is marginal on the USA-19
1670 as we run out of divisor resolution. */
1671 if (baud_rate > 57600)
1672 return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Alan Coxdeb91682008-07-22 11:13:08 +01001674 /* calculate the divisor and the counter (its inverse) */
1675 div = baudclk / b16;
1676 if (div == 0)
1677 return KEYSPAN_INVALID_BAUD_RATE;
1678 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 cnt = 0 - div;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Alan Coxdeb91682008-07-22 11:13:08 +01001681 if (div > 0xffff)
1682 return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Alan Coxdeb91682008-07-22 11:13:08 +01001684 /* return the counter values if non-null */
1685 if (rate_low)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 *rate_low = (u8) (cnt & 0xff);
Alan Coxdeb91682008-07-22 11:13:08 +01001687 if (rate_hi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 *rate_hi = (u8) ((cnt >> 8) & 0xff);
Alan Coxdeb91682008-07-22 11:13:08 +01001689 if (rate_low && rate_hi)
1690 dbg("%s - %d %02x %02x.",
1691 __func__, baud_rate, *rate_hi, *rate_low);
1692 return KEYSPAN_BAUD_RATE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695/* usa19hs function doesn't require prescaler */
1696static int keyspan_usa19hs_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1697 u8 *rate_low, u8 *prescaler, int portnum)
1698{
1699 u32 b16, /* baud rate times 16 (actual rate used internally) */
Alan Coxdeb91682008-07-22 11:13:08 +01001700 div; /* divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Alan Coxdeb91682008-07-22 11:13:08 +01001702 dbg("%s - %d.", __func__, baud_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Alan Coxdeb91682008-07-22 11:13:08 +01001704 /* prevent divide by zero... */
1705 b16 = baud_rate * 16L;
1706 if (b16 == 0)
1707 return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Alan Coxdeb91682008-07-22 11:13:08 +01001709 /* calculate the divisor */
1710 div = baudclk / b16;
1711 if (div == 0)
1712 return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Alan Coxdeb91682008-07-22 11:13:08 +01001714 if (div > 0xffff)
1715 return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Alan Coxdeb91682008-07-22 11:13:08 +01001717 /* return the counter values if non-null */
1718 if (rate_low)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 *rate_low = (u8) (div & 0xff);
Alan Coxdeb91682008-07-22 11:13:08 +01001720
1721 if (rate_hi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 *rate_hi = (u8) ((div >> 8) & 0xff);
Alan Coxdeb91682008-07-22 11:13:08 +01001723
1724 if (rate_low && rate_hi)
1725 dbg("%s - %d %02x %02x.",
1726 __func__, baud_rate, *rate_hi, *rate_low);
1727
1728 return KEYSPAN_BAUD_RATE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
1731static int keyspan_usa19w_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1732 u8 *rate_low, u8 *prescaler, int portnum)
1733{
1734 u32 b16, /* baud rate times 16 (actual rate used internally) */
1735 clk, /* clock with 13/8 prescaler */
Alan Coxdeb91682008-07-22 11:13:08 +01001736 div, /* divisor using 13/8 prescaler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 res, /* resulting baud rate using 13/8 prescaler */
1738 diff, /* error using 13/8 prescaler */
1739 smallest_diff;
1740 u8 best_prescaler;
1741 int i;
1742
Alan Coxdeb91682008-07-22 11:13:08 +01001743 dbg("%s - %d.", __func__, baud_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Alan Coxdeb91682008-07-22 11:13:08 +01001745 /* prevent divide by zero */
1746 b16 = baud_rate * 16L;
1747 if (b16 == 0)
1748 return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Alan Coxdeb91682008-07-22 11:13:08 +01001750 /* Calculate prescaler by trying them all and looking
1751 for best fit */
1752
1753 /* start with largest possible difference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 smallest_diff = 0xffffffff;
1755
1756 /* 0 is an invalid prescaler, used as a flag */
1757 best_prescaler = 0;
1758
Alan Coxdeb91682008-07-22 11:13:08 +01001759 for (i = 8; i <= 0xff; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 clk = (baudclk * 8) / (u32) i;
Alan Coxdeb91682008-07-22 11:13:08 +01001761
1762 div = clk / b16;
1763 if (div == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 res = clk / div;
Alan Coxdeb91682008-07-22 11:13:08 +01001767 diff = (res > b16) ? (res-b16) : (b16-res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Alan Coxdeb91682008-07-22 11:13:08 +01001769 if (diff < smallest_diff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 best_prescaler = i;
1771 smallest_diff = diff;
1772 }
1773 }
1774
Alan Coxdeb91682008-07-22 11:13:08 +01001775 if (best_prescaler == 0)
1776 return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 clk = (baudclk * 8) / (u32) best_prescaler;
1779 div = clk / b16;
1780
Alan Coxdeb91682008-07-22 11:13:08 +01001781 /* return the divisor and prescaler if non-null */
1782 if (rate_low)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 *rate_low = (u8) (div & 0xff);
Alan Coxdeb91682008-07-22 11:13:08 +01001784 if (rate_hi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 *rate_hi = (u8) ((div >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (prescaler) {
1787 *prescaler = best_prescaler;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001788 /* dbg("%s - %d %d", __func__, *prescaler, div); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
Alan Coxdeb91682008-07-22 11:13:08 +01001790 return KEYSPAN_BAUD_RATE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
1793 /* USA-28 supports different maximum baud rates on each port */
1794static int keyspan_usa28_calc_baud(u32 baud_rate, u32 baudclk, u8 *rate_hi,
1795 u8 *rate_low, u8 *prescaler, int portnum)
1796{
1797 u32 b16, /* baud rate times 16 (actual rate used internally) */
Alan Coxdeb91682008-07-22 11:13:08 +01001798 div, /* divisor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 cnt; /* inverse of divisor (programmed into 8051) */
1800
Alan Coxdeb91682008-07-22 11:13:08 +01001801 dbg("%s - %d.", __func__, baud_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 /* prevent divide by zero */
Alan Coxdeb91682008-07-22 11:13:08 +01001804 b16 = baud_rate * 16L;
1805 if (b16 == 0)
1806 return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Alan Coxdeb91682008-07-22 11:13:08 +01001808 /* calculate the divisor and the counter (its inverse) */
1809 div = KEYSPAN_USA28_BAUDCLK / b16;
1810 if (div == 0)
1811 return KEYSPAN_INVALID_BAUD_RATE;
1812 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 cnt = 0 - div;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Alan Coxdeb91682008-07-22 11:13:08 +01001815 /* check for out of range, based on portnum,
1816 and return result */
1817 if (portnum == 0) {
1818 if (div > 0xffff)
1819 return KEYSPAN_INVALID_BAUD_RATE;
1820 } else {
1821 if (portnum == 1) {
1822 if (div > 0xff)
1823 return KEYSPAN_INVALID_BAUD_RATE;
1824 } else
1825 return KEYSPAN_INVALID_BAUD_RATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827
1828 /* return the counter values if not NULL
1829 (port 1 will ignore retHi) */
Alan Coxdeb91682008-07-22 11:13:08 +01001830 if (rate_low)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 *rate_low = (u8) (cnt & 0xff);
Alan Coxdeb91682008-07-22 11:13:08 +01001832 if (rate_hi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 *rate_hi = (u8) ((cnt >> 8) & 0xff);
Alan Coxdeb91682008-07-22 11:13:08 +01001834 dbg("%s - %d OK.", __func__, baud_rate);
1835 return KEYSPAN_BAUD_RATE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
1838static int keyspan_usa26_send_setup(struct usb_serial *serial,
1839 struct usb_serial_port *port,
1840 int reset_port)
1841{
Alan Coxdeb91682008-07-22 11:13:08 +01001842 struct keyspan_usa26_portControlMessage msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 struct keyspan_serial_private *s_priv;
1844 struct keyspan_port_private *p_priv;
1845 const struct keyspan_device_details *d_details;
1846 int outcont_urb;
1847 struct urb *this_urb;
1848 int device_port, err;
1849
Alan Coxdeb91682008-07-22 11:13:08 +01001850 dbg("%s reset=%d", __func__, reset_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 s_priv = usb_get_serial_data(serial);
1853 p_priv = usb_get_serial_port_data(port);
1854 d_details = s_priv->device_details;
1855 device_port = port->number - port->serial->minor;
1856
1857 outcont_urb = d_details->outcont_endpoints[port->number];
1858 this_urb = p_priv->outcont_urb;
1859
Harvey Harrison441b62c2008-03-03 16:08:34 -08001860 dbg("%s - endpoint %d", __func__, usb_pipeendpoint(this_urb->pipe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /* Make sure we have an urb then send the message */
1863 if (this_urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001864 dbg("%s - oops no urb.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 return -1;
1866 }
1867
1868 /* Save reset port val for resend.
Lucy McCoy0ca12682007-05-18 12:10:41 -07001869 Don't overwrite resend for open/close condition. */
1870 if ((reset_port + 1) > p_priv->resend_cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 p_priv->resend_cont = reset_port + 1;
1872 if (this_urb->status == -EINPROGRESS) {
Alan Coxdeb91682008-07-22 11:13:08 +01001873 /* dbg("%s - already writing", __func__); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 mdelay(5);
Alan Coxdeb91682008-07-22 11:13:08 +01001875 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877
Alan Coxdeb91682008-07-22 11:13:08 +01001878 memset(&msg, 0, sizeof(struct keyspan_usa26_portControlMessage));
1879
1880 /* Only set baud rate if it's changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (p_priv->old_baud != p_priv->baud) {
1882 p_priv->old_baud = p_priv->baud;
1883 msg.setClocking = 0xff;
1884 if (d_details->calculate_baud_rate
1885 (p_priv->baud, d_details->baudclk, &msg.baudHi,
Alan Coxdeb91682008-07-22 11:13:08 +01001886 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
1887 dbg("%s - Invalid baud rate %d requested, using 9600.",
1888 __func__, p_priv->baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 msg.baudLo = 0;
1890 msg.baudHi = 125; /* Values for 9600 baud */
1891 msg.prescaler = 10;
1892 }
1893 msg.setPrescaler = 0xff;
1894 }
1895
1896 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
1897 switch (p_priv->cflag & CSIZE) {
1898 case CS5:
1899 msg.lcr |= USA_DATABITS_5;
1900 break;
1901 case CS6:
1902 msg.lcr |= USA_DATABITS_6;
1903 break;
1904 case CS7:
1905 msg.lcr |= USA_DATABITS_7;
1906 break;
1907 case CS8:
1908 msg.lcr |= USA_DATABITS_8;
1909 break;
1910 }
1911 if (p_priv->cflag & PARENB) {
1912 /* note USA_PARITY_NONE == 0 */
1913 msg.lcr |= (p_priv->cflag & PARODD)?
Alan Coxdeb91682008-07-22 11:13:08 +01001914 USA_PARITY_ODD : USA_PARITY_EVEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916 msg.setLcr = 0xff;
1917
1918 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
1919 msg.xonFlowControl = 0;
1920 msg.setFlowControl = 0xff;
1921 msg.forwardingLength = 16;
1922 msg.xonChar = 17;
1923 msg.xoffChar = 19;
1924
1925 /* Opening port */
1926 if (reset_port == 1) {
1927 msg._txOn = 1;
1928 msg._txOff = 0;
1929 msg.txFlush = 0;
1930 msg.txBreak = 0;
1931 msg.rxOn = 1;
1932 msg.rxOff = 0;
1933 msg.rxFlush = 1;
1934 msg.rxForward = 0;
1935 msg.returnStatus = 0;
1936 msg.resetDataToggle = 0xff;
1937 }
1938
1939 /* Closing port */
1940 else if (reset_port == 2) {
1941 msg._txOn = 0;
1942 msg._txOff = 1;
1943 msg.txFlush = 0;
1944 msg.txBreak = 0;
1945 msg.rxOn = 0;
1946 msg.rxOff = 1;
1947 msg.rxFlush = 1;
1948 msg.rxForward = 0;
1949 msg.returnStatus = 0;
1950 msg.resetDataToggle = 0;
1951 }
1952
1953 /* Sending intermediate configs */
1954 else {
Alan Coxdeb91682008-07-22 11:13:08 +01001955 msg._txOn = (!p_priv->break_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 msg._txOff = 0;
1957 msg.txFlush = 0;
1958 msg.txBreak = (p_priv->break_on);
1959 msg.rxOn = 0;
1960 msg.rxOff = 0;
1961 msg.rxFlush = 0;
1962 msg.rxForward = 0;
1963 msg.returnStatus = 0;
1964 msg.resetDataToggle = 0x0;
1965 }
1966
Alan Coxdeb91682008-07-22 11:13:08 +01001967 /* Do handshaking outputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 msg.setTxTriState_setRts = 0xff;
1969 msg.txTriState_rts = p_priv->rts_state;
1970
1971 msg.setHskoa_setDtr = 0xff;
1972 msg.hskoa_dtr = p_priv->dtr_state;
Alan Coxdeb91682008-07-22 11:13:08 +01001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 p_priv->resend_cont = 0;
Alan Coxdeb91682008-07-22 11:13:08 +01001975 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /* send the data out the device on control endpoint */
1978 this_urb->transfer_buffer_length = sizeof(msg);
1979
1980 this_urb->dev = serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +01001981 err = usb_submit_urb(this_urb, GFP_ATOMIC);
1982 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001983 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984#if 0
1985 else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001986 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 outcont_urb, this_urb->transfer_buffer_length,
1988 usb_pipeendpoint(this_urb->pipe));
1989 }
1990#endif
1991
Alan Coxa5b6f602008-04-08 17:16:06 +01001992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995static int keyspan_usa28_send_setup(struct usb_serial *serial,
1996 struct usb_serial_port *port,
1997 int reset_port)
1998{
Alan Coxdeb91682008-07-22 11:13:08 +01001999 struct keyspan_usa28_portControlMessage msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 struct keyspan_serial_private *s_priv;
2001 struct keyspan_port_private *p_priv;
2002 const struct keyspan_device_details *d_details;
2003 struct urb *this_urb;
2004 int device_port, err;
2005
Alan Coxdeb91682008-07-22 11:13:08 +01002006 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 s_priv = usb_get_serial_data(serial);
2009 p_priv = usb_get_serial_port_data(port);
2010 d_details = s_priv->device_details;
2011 device_port = port->number - port->serial->minor;
2012
2013 /* only do something if we have a bulk out endpoint */
Alan Coxdeb91682008-07-22 11:13:08 +01002014 this_urb = p_priv->outcont_urb;
2015 if (this_urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002016 dbg("%s - oops no urb.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return -1;
2018 }
2019
2020 /* Save reset port val for resend.
Lucy McCoy0ca12682007-05-18 12:10:41 -07002021 Don't overwrite resend for open/close condition. */
2022 if ((reset_port + 1) > p_priv->resend_cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 p_priv->resend_cont = reset_port + 1;
2024 if (this_urb->status == -EINPROGRESS) {
Alan Coxdeb91682008-07-22 11:13:08 +01002025 dbg("%s already writing", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 mdelay(5);
Alan Coxdeb91682008-07-22 11:13:08 +01002027 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029
Alan Coxdeb91682008-07-22 11:13:08 +01002030 memset(&msg, 0, sizeof(struct keyspan_usa28_portControlMessage));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 msg.setBaudRate = 1;
2033 if (d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
Alan Coxdeb91682008-07-22 11:13:08 +01002034 &msg.baudHi, &msg.baudLo, NULL, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2035 dbg("%s - Invalid baud rate requested %d.",
2036 __func__, p_priv->baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 msg.baudLo = 0xff;
2038 msg.baudHi = 0xb2; /* Values for 9600 baud */
2039 }
2040
2041 /* If parity is enabled, we must calculate it ourselves. */
2042 msg.parity = 0; /* XXX for now */
2043
2044 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2045 msg.xonFlowControl = 0;
2046
Alan Coxdeb91682008-07-22 11:13:08 +01002047 /* Do handshaking outputs, DTR is inverted relative to RTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 msg.rts = p_priv->rts_state;
2049 msg.dtr = p_priv->dtr_state;
2050
2051 msg.forwardingLength = 16;
2052 msg.forwardMs = 10;
2053 msg.breakThreshold = 45;
2054 msg.xonChar = 17;
2055 msg.xoffChar = 19;
2056
2057 /*msg.returnStatus = 1;
2058 msg.resetDataToggle = 0xff;*/
2059 /* Opening port */
2060 if (reset_port == 1) {
2061 msg._txOn = 1;
2062 msg._txOff = 0;
2063 msg.txFlush = 0;
2064 msg.txForceXoff = 0;
2065 msg.txBreak = 0;
2066 msg.rxOn = 1;
2067 msg.rxOff = 0;
2068 msg.rxFlush = 1;
2069 msg.rxForward = 0;
2070 msg.returnStatus = 0;
2071 msg.resetDataToggle = 0xff;
2072 }
2073 /* Closing port */
2074 else if (reset_port == 2) {
2075 msg._txOn = 0;
2076 msg._txOff = 1;
2077 msg.txFlush = 0;
2078 msg.txForceXoff = 0;
2079 msg.txBreak = 0;
2080 msg.rxOn = 0;
2081 msg.rxOff = 1;
2082 msg.rxFlush = 1;
2083 msg.rxForward = 0;
2084 msg.returnStatus = 0;
2085 msg.resetDataToggle = 0;
2086 }
2087 /* Sending intermediate configs */
2088 else {
Alan Coxdeb91682008-07-22 11:13:08 +01002089 msg._txOn = (!p_priv->break_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 msg._txOff = 0;
2091 msg.txFlush = 0;
2092 msg.txForceXoff = 0;
2093 msg.txBreak = (p_priv->break_on);
2094 msg.rxOn = 0;
2095 msg.rxOff = 0;
2096 msg.rxFlush = 0;
2097 msg.rxForward = 0;
2098 msg.returnStatus = 0;
2099 msg.resetDataToggle = 0x0;
2100 }
2101
2102 p_priv->resend_cont = 0;
Alan Coxdeb91682008-07-22 11:13:08 +01002103 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /* send the data out the device on control endpoint */
2106 this_urb->transfer_buffer_length = sizeof(msg);
2107
2108 this_urb->dev = serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +01002109 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2110 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08002111 dbg("%s - usb_submit_urb(setup) failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112#if 0
2113 else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002114 dbg("%s - usb_submit_urb(setup) OK %d bytes", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 this_urb->transfer_buffer_length);
2116 }
2117#endif
2118
Alan Coxa5b6f602008-04-08 17:16:06 +01002119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
2122static int keyspan_usa49_send_setup(struct usb_serial *serial,
2123 struct usb_serial_port *port,
2124 int reset_port)
2125{
Lucy McCoy0ca12682007-05-18 12:10:41 -07002126 struct keyspan_usa49_portControlMessage msg;
2127 struct usb_ctrlrequest *dr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 struct keyspan_serial_private *s_priv;
2129 struct keyspan_port_private *p_priv;
2130 const struct keyspan_device_details *d_details;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 struct urb *this_urb;
2132 int err, device_port;
2133
Alan Coxdeb91682008-07-22 11:13:08 +01002134 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 s_priv = usb_get_serial_data(serial);
2137 p_priv = usb_get_serial_port_data(port);
2138 d_details = s_priv->device_details;
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 this_urb = s_priv->glocont_urb;
2141
Lucy McCoy0ca12682007-05-18 12:10:41 -07002142 /* Work out which port within the device is being setup */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 device_port = port->number - port->serial->minor;
2144
Alan Coxdeb91682008-07-22 11:13:08 +01002145 dbg("%s - endpoint %d port %d (%d)",
2146 __func__, usb_pipeendpoint(this_urb->pipe),
2147 port->number, device_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /* Make sure we have an urb then send the message */
2150 if (this_urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002151 dbg("%s - oops no urb for port %d.", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return -1;
2153 }
2154
2155 /* Save reset port val for resend.
Lucy McCoy0ca12682007-05-18 12:10:41 -07002156 Don't overwrite resend for open/close condition. */
2157 if ((reset_port + 1) > p_priv->resend_cont)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 p_priv->resend_cont = reset_port + 1;
Lucy McCoy0ca12682007-05-18 12:10:41 -07002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (this_urb->status == -EINPROGRESS) {
Alan Coxdeb91682008-07-22 11:13:08 +01002161 /* dbg("%s - already writing", __func__); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 mdelay(5);
Alan Coxdeb91682008-07-22 11:13:08 +01002163 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165
Alan Coxdeb91682008-07-22 11:13:08 +01002166 memset(&msg, 0, sizeof(struct keyspan_usa49_portControlMessage));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /*msg.portNumber = port->number;*/
2169 msg.portNumber = device_port;
Alan Coxdeb91682008-07-22 11:13:08 +01002170
2171 /* Only set baud rate if it's changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (p_priv->old_baud != p_priv->baud) {
2173 p_priv->old_baud = p_priv->baud;
2174 msg.setClocking = 0xff;
2175 if (d_details->calculate_baud_rate
2176 (p_priv->baud, d_details->baudclk, &msg.baudHi,
Alan Coxdeb91682008-07-22 11:13:08 +01002177 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2178 dbg("%s - Invalid baud rate %d requested, using 9600.",
2179 __func__, p_priv->baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 msg.baudLo = 0;
2181 msg.baudHi = 125; /* Values for 9600 baud */
2182 msg.prescaler = 10;
2183 }
Alan Coxdeb91682008-07-22 11:13:08 +01002184 /* msg.setPrescaler = 0xff; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
2186
2187 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2188 switch (p_priv->cflag & CSIZE) {
2189 case CS5:
2190 msg.lcr |= USA_DATABITS_5;
2191 break;
2192 case CS6:
2193 msg.lcr |= USA_DATABITS_6;
2194 break;
2195 case CS7:
2196 msg.lcr |= USA_DATABITS_7;
2197 break;
2198 case CS8:
2199 msg.lcr |= USA_DATABITS_8;
2200 break;
2201 }
2202 if (p_priv->cflag & PARENB) {
2203 /* note USA_PARITY_NONE == 0 */
2204 msg.lcr |= (p_priv->cflag & PARODD)?
Alan Coxdeb91682008-07-22 11:13:08 +01002205 USA_PARITY_ODD : USA_PARITY_EVEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207 msg.setLcr = 0xff;
2208
2209 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2210 msg.xonFlowControl = 0;
2211 msg.setFlowControl = 0xff;
Alan Coxdeb91682008-07-22 11:13:08 +01002212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 msg.forwardingLength = 16;
2214 msg.xonChar = 17;
2215 msg.xoffChar = 19;
2216
Alan Coxdeb91682008-07-22 11:13:08 +01002217 /* Opening port */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (reset_port == 1) {
2219 msg._txOn = 1;
2220 msg._txOff = 0;
2221 msg.txFlush = 0;
2222 msg.txBreak = 0;
2223 msg.rxOn = 1;
2224 msg.rxOff = 0;
2225 msg.rxFlush = 1;
2226 msg.rxForward = 0;
2227 msg.returnStatus = 0;
2228 msg.resetDataToggle = 0xff;
2229 msg.enablePort = 1;
2230 msg.disablePort = 0;
2231 }
2232 /* Closing port */
2233 else if (reset_port == 2) {
2234 msg._txOn = 0;
2235 msg._txOff = 1;
2236 msg.txFlush = 0;
2237 msg.txBreak = 0;
2238 msg.rxOn = 0;
2239 msg.rxOff = 1;
2240 msg.rxFlush = 1;
2241 msg.rxForward = 0;
2242 msg.returnStatus = 0;
2243 msg.resetDataToggle = 0;
2244 msg.enablePort = 0;
2245 msg.disablePort = 1;
2246 }
2247 /* Sending intermediate configs */
2248 else {
Alan Coxdeb91682008-07-22 11:13:08 +01002249 msg._txOn = (!p_priv->break_on);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 msg._txOff = 0;
2251 msg.txFlush = 0;
2252 msg.txBreak = (p_priv->break_on);
2253 msg.rxOn = 0;
2254 msg.rxOff = 0;
2255 msg.rxFlush = 0;
2256 msg.rxForward = 0;
2257 msg.returnStatus = 0;
2258 msg.resetDataToggle = 0x0;
2259 msg.enablePort = 0;
2260 msg.disablePort = 0;
2261 }
2262
Alan Coxdeb91682008-07-22 11:13:08 +01002263 /* Do handshaking outputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 msg.setRts = 0xff;
2265 msg.rts = p_priv->rts_state;
2266
2267 msg.setDtr = 0xff;
2268 msg.dtr = p_priv->dtr_state;
Alan Coxdeb91682008-07-22 11:13:08 +01002269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 p_priv->resend_cont = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Alan Coxdeb91682008-07-22 11:13:08 +01002272 /* if the device is a 49wg, we send control message on usb
2273 control EP 0 */
Lucy McCoy0ca12682007-05-18 12:10:41 -07002274
2275 if (d_details->product_id == keyspan_usa49wg_product_id) {
2276 dr = (void *)(s_priv->ctrl_buf);
2277 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_OUT;
2278 dr->bRequest = 0xB0; /* 49wg control message */;
2279 dr->wValue = 0;
2280 dr->wIndex = 0;
2281 dr->wLength = cpu_to_le16(sizeof(msg));
2282
Alan Coxdeb91682008-07-22 11:13:08 +01002283 memcpy(s_priv->glocont_buf, &msg, sizeof(msg));
Lucy McCoy0ca12682007-05-18 12:10:41 -07002284
Alan Coxdeb91682008-07-22 11:13:08 +01002285 usb_fill_control_urb(this_urb, serial->dev,
2286 usb_sndctrlpipe(serial->dev, 0),
2287 (unsigned char *)dr, s_priv->glocont_buf,
2288 sizeof(msg), usa49_glocont_callback, serial);
Lucy McCoy0ca12682007-05-18 12:10:41 -07002289
2290 } else {
2291 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
Alan Coxdeb91682008-07-22 11:13:08 +01002292
Lucy McCoy0ca12682007-05-18 12:10:41 -07002293 /* send the data out the device on control endpoint */
2294 this_urb->transfer_buffer_length = sizeof(msg);
2295
2296 this_urb->dev = serial->dev;
2297 }
Alan Coxdeb91682008-07-22 11:13:08 +01002298 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2299 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08002300 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301#if 0
2302 else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002303 dbg("%s - usb_submit_urb(%d) OK %d bytes (end %d)", __func__,
Lucy McCoy0ca12682007-05-18 12:10:41 -07002304 outcont_urb, this_urb->transfer_buffer_length,
2305 usb_pipeendpoint(this_urb->pipe));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307#endif
2308
Alan Coxa5b6f602008-04-08 17:16:06 +01002309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
2312static int keyspan_usa90_send_setup(struct usb_serial *serial,
2313 struct usb_serial_port *port,
2314 int reset_port)
2315{
Alan Coxdeb91682008-07-22 11:13:08 +01002316 struct keyspan_usa90_portControlMessage msg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 struct keyspan_serial_private *s_priv;
2318 struct keyspan_port_private *p_priv;
2319 const struct keyspan_device_details *d_details;
2320 struct urb *this_urb;
2321 int err;
2322 u8 prescaler;
2323
Alan Coxdeb91682008-07-22 11:13:08 +01002324 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326 s_priv = usb_get_serial_data(serial);
2327 p_priv = usb_get_serial_port_data(port);
2328 d_details = s_priv->device_details;
2329
2330 /* only do something if we have a bulk out endpoint */
Alan Coxdeb91682008-07-22 11:13:08 +01002331 this_urb = p_priv->outcont_urb;
2332 if (this_urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002333 dbg("%s - oops no urb.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return -1;
2335 }
2336
2337 /* Save reset port val for resend.
2338 Don't overwrite resend for open/close condition. */
2339 if ((reset_port + 1) > p_priv->resend_cont)
2340 p_priv->resend_cont = reset_port + 1;
2341 if (this_urb->status == -EINPROGRESS) {
Alan Coxdeb91682008-07-22 11:13:08 +01002342 dbg("%s already writing", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 mdelay(5);
Alan Coxdeb91682008-07-22 11:13:08 +01002344 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346
Alan Coxdeb91682008-07-22 11:13:08 +01002347 memset(&msg, 0, sizeof(struct keyspan_usa90_portControlMessage));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Alan Coxdeb91682008-07-22 11:13:08 +01002349 /* Only set baud rate if it's changed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (p_priv->old_baud != p_priv->baud) {
2351 p_priv->old_baud = p_priv->baud;
2352 msg.setClocking = 0x01;
2353 if (d_details->calculate_baud_rate
2354 (p_priv->baud, d_details->baudclk, &msg.baudHi,
Alan Coxdeb91682008-07-22 11:13:08 +01002355 &msg.baudLo, &prescaler, 0) == KEYSPAN_INVALID_BAUD_RATE) {
2356 dbg("%s - Invalid baud rate %d requested, using 9600.",
2357 __func__, p_priv->baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 p_priv->baud = 9600;
Alan Coxdeb91682008-07-22 11:13:08 +01002359 d_details->calculate_baud_rate(p_priv->baud, d_details->baudclk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 &msg.baudHi, &msg.baudLo, &prescaler, 0);
2361 }
2362 msg.setRxMode = 1;
2363 msg.setTxMode = 1;
2364 }
2365
2366 /* modes must always be correctly specified */
Alan Coxdeb91682008-07-22 11:13:08 +01002367 if (p_priv->baud > 57600) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 msg.rxMode = RXMODE_DMA;
2369 msg.txMode = TXMODE_DMA;
Alan Coxdeb91682008-07-22 11:13:08 +01002370 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 msg.rxMode = RXMODE_BYHAND;
2372 msg.txMode = TXMODE_BYHAND;
2373 }
2374
2375 msg.lcr = (p_priv->cflag & CSTOPB)? STOPBITS_678_2: STOPBITS_5678_1;
2376 switch (p_priv->cflag & CSIZE) {
2377 case CS5:
2378 msg.lcr |= USA_DATABITS_5;
2379 break;
2380 case CS6:
2381 msg.lcr |= USA_DATABITS_6;
2382 break;
2383 case CS7:
2384 msg.lcr |= USA_DATABITS_7;
2385 break;
2386 case CS8:
2387 msg.lcr |= USA_DATABITS_8;
2388 break;
2389 }
2390 if (p_priv->cflag & PARENB) {
2391 /* note USA_PARITY_NONE == 0 */
2392 msg.lcr |= (p_priv->cflag & PARODD)?
Alan Coxdeb91682008-07-22 11:13:08 +01002393 USA_PARITY_ODD : USA_PARITY_EVEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
2395 if (p_priv->old_cflag != p_priv->cflag) {
2396 p_priv->old_cflag = p_priv->cflag;
2397 msg.setLcr = 0x01;
2398 }
2399
2400 if (p_priv->flow_control == flow_cts)
2401 msg.txFlowControl = TXFLOW_CTS;
2402 msg.setTxFlowControl = 0x01;
2403 msg.setRxFlowControl = 0x01;
Alan Coxdeb91682008-07-22 11:13:08 +01002404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 msg.rxForwardingLength = 16;
Alan Coxdeb91682008-07-22 11:13:08 +01002406 msg.rxForwardingTimeout = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 msg.txAckSetting = 0;
2408 msg.xonChar = 17;
2409 msg.xoffChar = 19;
2410
Alan Coxdeb91682008-07-22 11:13:08 +01002411 /* Opening port */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (reset_port == 1) {
2413 msg.portEnabled = 1;
2414 msg.rxFlush = 1;
2415 msg.txBreak = (p_priv->break_on);
2416 }
2417 /* Closing port */
Alan Coxdeb91682008-07-22 11:13:08 +01002418 else if (reset_port == 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 msg.portEnabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 /* Sending intermediate configs */
2421 else {
Alan Cox95da3102008-07-22 11:09:07 +01002422 if (port->port.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 msg.portEnabled = 1;
2424 msg.txBreak = (p_priv->break_on);
2425 }
2426
Alan Coxdeb91682008-07-22 11:13:08 +01002427 /* Do handshaking outputs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 msg.setRts = 0x01;
2429 msg.rts = p_priv->rts_state;
2430
2431 msg.setDtr = 0x01;
2432 msg.dtr = p_priv->dtr_state;
Alan Coxdeb91682008-07-22 11:13:08 +01002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 p_priv->resend_cont = 0;
Alan Coxdeb91682008-07-22 11:13:08 +01002435 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 /* send the data out the device on control endpoint */
2438 this_urb->transfer_buffer_length = sizeof(msg);
2439
2440 this_urb->dev = serial->dev;
Alan Coxdeb91682008-07-22 11:13:08 +01002441 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2442 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08002443 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__, err);
Alan Coxa5b6f602008-04-08 17:16:06 +01002444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Lucy McCoy0ca12682007-05-18 12:10:41 -07002447static int keyspan_usa67_send_setup(struct usb_serial *serial,
2448 struct usb_serial_port *port,
2449 int reset_port)
2450{
2451 struct keyspan_usa67_portControlMessage msg;
2452 struct keyspan_serial_private *s_priv;
2453 struct keyspan_port_private *p_priv;
2454 const struct keyspan_device_details *d_details;
2455 struct urb *this_urb;
2456 int err, device_port;
2457
Alan Coxdeb91682008-07-22 11:13:08 +01002458 dbg("%s", __func__);
Lucy McCoy0ca12682007-05-18 12:10:41 -07002459
2460 s_priv = usb_get_serial_data(serial);
2461 p_priv = usb_get_serial_port_data(port);
2462 d_details = s_priv->device_details;
2463
2464 this_urb = s_priv->glocont_urb;
2465
2466 /* Work out which port within the device is being setup */
2467 device_port = port->number - port->serial->minor;
2468
2469 /* Make sure we have an urb then send the message */
2470 if (this_urb == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002471 dbg("%s - oops no urb for port %d.", __func__,
Lucy McCoy0ca12682007-05-18 12:10:41 -07002472 port->number);
2473 return -1;
2474 }
2475
2476 /* Save reset port val for resend.
2477 Don't overwrite resend for open/close condition. */
2478 if ((reset_port + 1) > p_priv->resend_cont)
2479 p_priv->resend_cont = reset_port + 1;
2480 if (this_urb->status == -EINPROGRESS) {
Alan Coxdeb91682008-07-22 11:13:08 +01002481 /* dbg("%s - already writing", __func__); */
Lucy McCoy0ca12682007-05-18 12:10:41 -07002482 mdelay(5);
Alan Coxdeb91682008-07-22 11:13:08 +01002483 return -1;
Lucy McCoy0ca12682007-05-18 12:10:41 -07002484 }
2485
2486 memset(&msg, 0, sizeof(struct keyspan_usa67_portControlMessage));
2487
2488 msg.port = device_port;
2489
2490 /* Only set baud rate if it's changed */
2491 if (p_priv->old_baud != p_priv->baud) {
2492 p_priv->old_baud = p_priv->baud;
2493 msg.setClocking = 0xff;
2494 if (d_details->calculate_baud_rate
2495 (p_priv->baud, d_details->baudclk, &msg.baudHi,
Alan Coxdeb91682008-07-22 11:13:08 +01002496 &msg.baudLo, &msg.prescaler, device_port) == KEYSPAN_INVALID_BAUD_RATE) {
2497 dbg("%s - Invalid baud rate %d requested, using 9600.",
2498 __func__, p_priv->baud);
Lucy McCoy0ca12682007-05-18 12:10:41 -07002499 msg.baudLo = 0;
2500 msg.baudHi = 125; /* Values for 9600 baud */
2501 msg.prescaler = 10;
2502 }
2503 msg.setPrescaler = 0xff;
2504 }
2505
2506 msg.lcr = (p_priv->cflag & CSTOPB) ? STOPBITS_678_2 : STOPBITS_5678_1;
2507 switch (p_priv->cflag & CSIZE) {
2508 case CS5:
2509 msg.lcr |= USA_DATABITS_5;
2510 break;
2511 case CS6:
2512 msg.lcr |= USA_DATABITS_6;
2513 break;
2514 case CS7:
2515 msg.lcr |= USA_DATABITS_7;
2516 break;
2517 case CS8:
2518 msg.lcr |= USA_DATABITS_8;
2519 break;
2520 }
2521 if (p_priv->cflag & PARENB) {
2522 /* note USA_PARITY_NONE == 0 */
2523 msg.lcr |= (p_priv->cflag & PARODD)?
Alan Coxdeb91682008-07-22 11:13:08 +01002524 USA_PARITY_ODD : USA_PARITY_EVEN;
Lucy McCoy0ca12682007-05-18 12:10:41 -07002525 }
2526 msg.setLcr = 0xff;
2527
2528 msg.ctsFlowControl = (p_priv->flow_control == flow_cts);
2529 msg.xonFlowControl = 0;
2530 msg.setFlowControl = 0xff;
2531 msg.forwardingLength = 16;
2532 msg.xonChar = 17;
2533 msg.xoffChar = 19;
2534
2535 if (reset_port == 1) {
2536 /* Opening port */
2537 msg._txOn = 1;
2538 msg._txOff = 0;
2539 msg.txFlush = 0;
2540 msg.txBreak = 0;
2541 msg.rxOn = 1;
2542 msg.rxOff = 0;
2543 msg.rxFlush = 1;
2544 msg.rxForward = 0;
2545 msg.returnStatus = 0;
2546 msg.resetDataToggle = 0xff;
2547 } else if (reset_port == 2) {
2548 /* Closing port */
2549 msg._txOn = 0;
2550 msg._txOff = 1;
2551 msg.txFlush = 0;
2552 msg.txBreak = 0;
2553 msg.rxOn = 0;
2554 msg.rxOff = 1;
2555 msg.rxFlush = 1;
2556 msg.rxForward = 0;
2557 msg.returnStatus = 0;
2558 msg.resetDataToggle = 0;
2559 } else {
2560 /* Sending intermediate configs */
Alan Coxdeb91682008-07-22 11:13:08 +01002561 msg._txOn = (!p_priv->break_on);
Lucy McCoy0ca12682007-05-18 12:10:41 -07002562 msg._txOff = 0;
2563 msg.txFlush = 0;
2564 msg.txBreak = (p_priv->break_on);
2565 msg.rxOn = 0;
2566 msg.rxOff = 0;
2567 msg.rxFlush = 0;
2568 msg.rxForward = 0;
2569 msg.returnStatus = 0;
2570 msg.resetDataToggle = 0x0;
2571 }
2572
2573 /* Do handshaking outputs */
2574 msg.setTxTriState_setRts = 0xff;
2575 msg.txTriState_rts = p_priv->rts_state;
2576
2577 msg.setHskoa_setDtr = 0xff;
2578 msg.hskoa_dtr = p_priv->dtr_state;
2579
2580 p_priv->resend_cont = 0;
2581
2582 memcpy(this_urb->transfer_buffer, &msg, sizeof(msg));
2583
2584 /* send the data out the device on control endpoint */
2585 this_urb->transfer_buffer_length = sizeof(msg);
2586 this_urb->dev = serial->dev;
2587
2588 err = usb_submit_urb(this_urb, GFP_ATOMIC);
2589 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08002590 dbg("%s - usb_submit_urb(setup) failed (%d)", __func__,
Lucy McCoy0ca12682007-05-18 12:10:41 -07002591 err);
Alan Coxa5b6f602008-04-08 17:16:06 +01002592 return 0;
Lucy McCoy0ca12682007-05-18 12:10:41 -07002593}
2594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595static void keyspan_send_setup(struct usb_serial_port *port, int reset_port)
2596{
2597 struct usb_serial *serial = port->serial;
2598 struct keyspan_serial_private *s_priv;
2599 const struct keyspan_device_details *d_details;
2600
Alan Coxdeb91682008-07-22 11:13:08 +01002601 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 s_priv = usb_get_serial_data(serial);
2604 d_details = s_priv->device_details;
2605
2606 switch (d_details->msg_format) {
2607 case msg_usa26:
2608 keyspan_usa26_send_setup(serial, port, reset_port);
2609 break;
2610 case msg_usa28:
2611 keyspan_usa28_send_setup(serial, port, reset_port);
2612 break;
2613 case msg_usa49:
2614 keyspan_usa49_send_setup(serial, port, reset_port);
2615 break;
2616 case msg_usa90:
2617 keyspan_usa90_send_setup(serial, port, reset_port);
2618 break;
Lucy McCoy0ca12682007-05-18 12:10:41 -07002619 case msg_usa67:
2620 keyspan_usa67_send_setup(serial, port, reset_port);
2621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623}
2624
2625
2626/* Gets called by the "real" driver (ie once firmware is loaded
2627 and renumeration has taken place. */
Alan Coxdeb91682008-07-22 11:13:08 +01002628static int keyspan_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 int i, err;
2631 struct usb_serial_port *port;
2632 struct keyspan_serial_private *s_priv;
2633 struct keyspan_port_private *p_priv;
2634 const struct keyspan_device_details *d_details;
2635
Harvey Harrison441b62c2008-03-03 16:08:34 -08002636 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 for (i = 0; (d_details = keyspan_devices[i]) != NULL; ++i)
Alan Coxdeb91682008-07-22 11:13:08 +01002639 if (d_details->product_id ==
2640 le16_to_cpu(serial->dev->descriptor.idProduct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 break;
2642 if (d_details == NULL) {
Alan Coxdeb91682008-07-22 11:13:08 +01002643 dev_err(&serial->dev->dev, "%s - unknown product id %x\n",
2644 __func__, le16_to_cpu(serial->dev->descriptor.idProduct));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 return 1;
2646 }
2647
2648 /* Setup private data for serial driver */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +01002649 s_priv = kzalloc(sizeof(struct keyspan_serial_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (!s_priv) {
Alan Coxdeb91682008-07-22 11:13:08 +01002651 dbg("%s - kmalloc for keyspan_serial_private failed.",
2652 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return -ENOMEM;
2654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656 s_priv->device_details = d_details;
2657 usb_set_serial_data(serial, s_priv);
2658
2659 /* Now setup per port private data */
2660 for (i = 0; i < serial->num_ports; i++) {
2661 port = serial->port[i];
Alan Coxdeb91682008-07-22 11:13:08 +01002662 p_priv = kzalloc(sizeof(struct keyspan_port_private),
2663 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (!p_priv) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08002665 dbg("%s - kmalloc for keyspan_port_private (%d) failed!.", __func__, i);
Alan Coxdeb91682008-07-22 11:13:08 +01002666 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 p_priv->device_details = d_details;
2669 usb_set_serial_port_data(port, p_priv);
2670 }
2671
2672 keyspan_setup_urbs(serial);
2673
Lucy McCoy0ca12682007-05-18 12:10:41 -07002674 if (s_priv->instat_urb != NULL) {
2675 s_priv->instat_urb->dev = serial->dev;
2676 err = usb_submit_urb(s_priv->instat_urb, GFP_KERNEL);
2677 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08002678 dbg("%s - submit instat urb failed %d", __func__,
Lucy McCoy0ca12682007-05-18 12:10:41 -07002679 err);
2680 }
2681 if (s_priv->indat_urb != NULL) {
2682 s_priv->indat_urb->dev = serial->dev;
2683 err = usb_submit_urb(s_priv->indat_urb, GFP_KERNEL);
2684 if (err != 0)
Harvey Harrison441b62c2008-03-03 16:08:34 -08002685 dbg("%s - submit indat urb failed %d", __func__,
Lucy McCoy0ca12682007-05-18 12:10:41 -07002686 err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
Alan Coxdeb91682008-07-22 11:13:08 +01002688
Alan Coxa5b6f602008-04-08 17:16:06 +01002689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
2691
Alan Coxdeb91682008-07-22 11:13:08 +01002692static void keyspan_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 int i, j;
2695 struct usb_serial_port *port;
2696 struct keyspan_serial_private *s_priv;
2697 struct keyspan_port_private *p_priv;
2698
Harvey Harrison441b62c2008-03-03 16:08:34 -08002699 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 s_priv = usb_get_serial_data(serial);
2702
2703 /* Stop reading/writing urbs */
2704 stop_urb(s_priv->instat_urb);
2705 stop_urb(s_priv->glocont_urb);
Lucy McCoy0ca12682007-05-18 12:10:41 -07002706 stop_urb(s_priv->indat_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 for (i = 0; i < serial->num_ports; ++i) {
2708 port = serial->port[i];
2709 p_priv = usb_get_serial_port_data(port);
2710 stop_urb(p_priv->inack_urb);
2711 stop_urb(p_priv->outcont_urb);
2712 for (j = 0; j < 2; j++) {
2713 stop_urb(p_priv->in_urbs[j]);
2714 stop_urb(p_priv->out_urbs[j]);
2715 }
2716 }
2717
2718 /* Now free them */
Mariusz Kozlowski1cadc132006-11-08 15:36:34 +01002719 usb_free_urb(s_priv->instat_urb);
Lucy McCoy0ca12682007-05-18 12:10:41 -07002720 usb_free_urb(s_priv->indat_urb);
Mariusz Kozlowski1cadc132006-11-08 15:36:34 +01002721 usb_free_urb(s_priv->glocont_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 for (i = 0; i < serial->num_ports; ++i) {
2723 port = serial->port[i];
2724 p_priv = usb_get_serial_port_data(port);
Mariusz Kozlowski1cadc132006-11-08 15:36:34 +01002725 usb_free_urb(p_priv->inack_urb);
2726 usb_free_urb(p_priv->outcont_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 for (j = 0; j < 2; j++) {
Mariusz Kozlowski1cadc132006-11-08 15:36:34 +01002728 usb_free_urb(p_priv->in_urbs[j]);
2729 usb_free_urb(p_priv->out_urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
2731 }
2732
2733 /* dbg("Freeing serial->private."); */
2734 kfree(s_priv);
2735
2736 /* dbg("Freeing port->private."); */
2737 /* Now free per port private data */
2738 for (i = 0; i < serial->num_ports; i++) {
2739 port = serial->port[i];
2740 kfree(usb_get_serial_port_data(port));
2741 }
2742}
2743
Alan Coxdeb91682008-07-22 11:13:08 +01002744MODULE_AUTHOR(DRIVER_AUTHOR);
2745MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746MODULE_LICENSE("GPL");
2747
David Woodhouse2971c572008-05-30 14:04:03 +03002748MODULE_FIRMWARE("keyspan/usa28.fw");
2749MODULE_FIRMWARE("keyspan/usa28x.fw");
2750MODULE_FIRMWARE("keyspan/usa28xa.fw");
2751MODULE_FIRMWARE("keyspan/usa28xb.fw");
2752MODULE_FIRMWARE("keyspan/usa19.fw");
2753MODULE_FIRMWARE("keyspan/usa19qi.fw");
2754MODULE_FIRMWARE("keyspan/mpr.fw");
2755MODULE_FIRMWARE("keyspan/usa19qw.fw");
2756MODULE_FIRMWARE("keyspan/usa18x.fw");
2757MODULE_FIRMWARE("keyspan/usa19w.fw");
2758MODULE_FIRMWARE("keyspan/usa49w.fw");
2759MODULE_FIRMWARE("keyspan/usa49wlc.fw");
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761module_param(debug, bool, S_IRUGO | S_IWUSR);
2762MODULE_PARM_DESC(debug, "Debug enabled or not");
2763