blob: 665aa77a917b1c464c5f6da22860a54a6afd1fd6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB ConnectTech WhiteHEAT driver
3 *
4 * Copyright (C) 2002
5 * Connect Tech Inc.
6 *
7 * Copyright (C) 1999 - 2001
8 * Greg Kroah-Hartman (greg@kroah.com)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * See Documentation/usb/usb-serial.txt for more information on using this driver
16 *
17 * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com)
18 * Upgrade to full working driver
19 *
20 * (05/30/2001) gkh
21 * switched from using spinlock to a semaphore, which fixes lots of problems.
22 *
23 * (04/08/2001) gb
24 * Identify version on module load.
25 *
26 * 2001_Mar_19 gkh
27 * Fixed MOD_INC and MOD_DEC logic, the ability to open a port more
28 * than once, and the got the proper usb_device_id table entries so
29 * the driver works again.
30 *
31 * (11/01/2000) Adam J. Richter
32 * usb_device_id table support
33 *
34 * (10/05/2000) gkh
35 * Fixed bug with urb->dev not being set properly, now that the usb
36 * core needs it.
37 *
38 * (10/03/2000) smd
39 * firmware is improved to guard against crap sent to device
40 * firmware now replies CMD_FAILURE on bad things
41 * read_callback fix you provided for private info struct
42 * command_finished now indicates success or fail
43 * setup_port struct now packed to avoid gcc padding
44 * firmware uses 1 based port numbering, driver now handles that
45 *
46 * (09/11/2000) gkh
47 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
48 *
49 * (07/19/2000) gkh
50 * Added module_init and module_exit functions to handle the fact that this
51 * driver is a loadable module now.
52 * Fixed bug with port->minor that was found by Al Borchers
53 *
54 * (07/04/2000) gkh
55 * Added support for port settings. Baud rate can now be changed. Line signals
56 * are not transferred to and from the tty layer yet, but things seem to be
57 * working well now.
58 *
59 * (05/04/2000) gkh
60 * First cut at open and close commands. Data can flow through the ports at
61 * default speeds now.
62 *
63 * (03/26/2000) gkh
64 * Split driver up into device specific pieces.
65 *
66 */
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kernel.h>
69#include <linux/errno.h>
70#include <linux/init.h>
71#include <linux/slab.h>
72#include <linux/tty.h>
73#include <linux/tty_driver.h>
74#include <linux/tty_flip.h>
75#include <linux/module.h>
76#include <linux/spinlock.h>
Oliver Neukum08a2b3b2007-05-24 13:52:51 +020077#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <asm/uaccess.h>
79#include <asm/termbits.h>
80#include <linux/usb.h>
81#include <linux/serial_reg.h>
82#include <linux/serial.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070083#include <linux/usb/serial.h>
David Woodhouseec6752f2008-05-31 01:35:29 +030084#include <linux/firmware.h>
85#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include "whiteheat.h" /* WhiteHEAT specific commands */
87
88static int debug;
89
90#ifndef CMSPAR
91#define CMSPAR 0
92#endif
93
94/*
95 * Version Information
96 */
97#define DRIVER_VERSION "v2.0"
98#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
99#define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
100
101#define CONNECT_TECH_VENDOR_ID 0x0710
102#define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
103#define CONNECT_TECH_WHITE_HEAT_ID 0x8001
104
105/*
106 ID tables for whiteheat are unusual, because we want to different
107 things for different versions of the device. Eventually, this
108 will be doable from a single table. But, for now, we define two
109 separate ID tables, and then a third table that combines them
110 just for the purpose of exporting the autoloading information.
111*/
112static struct usb_device_id id_table_std [] = {
113 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
114 { } /* Terminating entry */
115};
116
117static struct usb_device_id id_table_prerenumeration [] = {
118 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
119 { } /* Terminating entry */
120};
121
122static struct usb_device_id id_table_combined [] = {
123 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
124 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
125 { } /* Terminating entry */
126};
127
128MODULE_DEVICE_TABLE (usb, id_table_combined);
129
130static struct usb_driver whiteheat_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .name = "whiteheat",
132 .probe = usb_serial_probe,
133 .disconnect = usb_serial_disconnect,
134 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800135 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138/* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
139static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id);
140static int whiteheat_firmware_attach (struct usb_serial *serial);
141
142/* function prototypes for the Connect Tech WhiteHEAT serial converter */
143static int whiteheat_attach (struct usb_serial *serial);
144static void whiteheat_shutdown (struct usb_serial *serial);
145static int whiteheat_open (struct usb_serial_port *port, struct file *filp);
146static void whiteheat_close (struct usb_serial_port *port, struct file *filp);
147static int whiteheat_write (struct usb_serial_port *port, const unsigned char *buf, int count);
148static int whiteheat_write_room (struct usb_serial_port *port);
149static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
Alan Cox606d0992006-12-08 02:38:45 -0800150static void whiteheat_set_termios (struct usb_serial_port *port, struct ktermios * old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file);
152static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
153static void whiteheat_break_ctl (struct usb_serial_port *port, int break_state);
154static int whiteheat_chars_in_buffer (struct usb_serial_port *port);
155static void whiteheat_throttle (struct usb_serial_port *port);
156static void whiteheat_unthrottle (struct usb_serial_port *port);
David Howells7d12e782006-10-05 14:55:46 +0100157static void whiteheat_read_callback (struct urb *urb);
158static void whiteheat_write_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700160static struct usb_serial_driver whiteheat_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700161 .driver = {
162 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700163 .name = "whiteheatnofirm",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700164 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700165 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100166 .usb_driver = &whiteheat_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .id_table = id_table_prerenumeration,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .num_ports = 1,
169 .probe = whiteheat_firmware_download,
170 .attach = whiteheat_firmware_attach,
171};
172
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700173static struct usb_serial_driver whiteheat_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700174 .driver = {
175 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700176 .name = "whiteheat",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700177 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700178 .description = "Connect Tech - WhiteHEAT",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100179 .usb_driver = &whiteheat_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .id_table = id_table_std,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 .num_ports = 4,
182 .attach = whiteheat_attach,
183 .shutdown = whiteheat_shutdown,
184 .open = whiteheat_open,
185 .close = whiteheat_close,
186 .write = whiteheat_write,
187 .write_room = whiteheat_write_room,
188 .ioctl = whiteheat_ioctl,
189 .set_termios = whiteheat_set_termios,
190 .break_ctl = whiteheat_break_ctl,
191 .tiocmget = whiteheat_tiocmget,
192 .tiocmset = whiteheat_tiocmset,
193 .chars_in_buffer = whiteheat_chars_in_buffer,
194 .throttle = whiteheat_throttle,
195 .unthrottle = whiteheat_unthrottle,
196 .read_bulk_callback = whiteheat_read_callback,
197 .write_bulk_callback = whiteheat_write_callback,
198};
199
200
201struct whiteheat_command_private {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200202 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 __u8 port_running;
204 __u8 command_finished;
205 wait_queue_head_t wait_command; /* for handling sleeping while waiting for a command to finish */
206 __u8 result_buffer[64];
207};
208
209
210#define THROTTLED 0x01
211#define ACTUALLY_THROTTLED 0x02
212
213static int urb_pool_size = 8;
214
215struct whiteheat_urb_wrap {
216 struct list_head list;
217 struct urb *urb;
218};
219
220struct whiteheat_private {
221 spinlock_t lock;
222 __u8 flags;
Alan Coxa5b6f602008-04-08 17:16:06 +0100223 __u8 mcr; /* FIXME: no locking on mcr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct list_head rx_urbs_free;
225 struct list_head rx_urbs_submitted;
226 struct list_head rx_urb_q;
227 struct work_struct rx_work;
David Howellsc4028952006-11-22 14:57:56 +0000228 struct usb_serial_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct list_head tx_urbs_free;
230 struct list_head tx_urbs_submitted;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200231 struct mutex deathwarrant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
234
235/* local function prototypes */
236static int start_command_port(struct usb_serial *serial);
237static void stop_command_port(struct usb_serial *serial);
David Howells7d12e782006-10-05 14:55:46 +0100238static void command_port_write_callback(struct urb *urb);
239static void command_port_read_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static int start_port_read(struct usb_serial_port *port);
242static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb, struct list_head *head);
243static struct list_head *list_first(struct list_head *head);
David Howellsc4028952006-11-22 14:57:56 +0000244static void rx_data_softint(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize);
247static int firm_open(struct usb_serial_port *port);
248static int firm_close(struct usb_serial_port *port);
249static int firm_setup_port(struct usb_serial_port *port);
250static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
251static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
252static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
253static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
254static int firm_get_dtr_rts(struct usb_serial_port *port);
255static int firm_report_tx_done(struct usb_serial_port *port);
256
257
258#define COMMAND_PORT 4
259#define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
260#define COMMAND_TIMEOUT_MS 2000
261#define CLOSING_DELAY (30 * HZ)
262
263
264/*****************************************************************************
265 * Connect Tech's White Heat prerenumeration driver functions
266 *****************************************************************************/
267
268/* steps to download the firmware to the WhiteHEAT device:
269 - hold the reset (by writing to the reset bit of the CPUCS register)
270 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
271 - release the reset (by writing to the CPUCS register)
272 - download the WH.HEX file for all addresses greater than 0x1b3f using
273 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
274 - hold the reset
275 - download the WH.HEX file for all addresses less than 0x1b40 using
276 VENDOR_REQUEST_ANCHOR_LOAD
277 - release the reset
278 - device renumerated itself and comes up as new device id with all
279 firmware download completed.
280*/
281static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id)
282{
David Woodhouseec6752f2008-05-31 01:35:29 +0300283 int response, ret = -ENOENT;
284 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
285 const struct ihex_binrec *record;
286
Harvey Harrison441b62c2008-03-03 16:08:34 -0800287 dbg("%s", __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300288
289 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
290 &serial->dev->dev)) {
291 err("%s - request \"whiteheat.fw\" failed", __func__);
292 goto out;
293 }
294 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
295 &serial->dev->dev)) {
296 err("%s - request \"whiteheat_loader.fw\" failed", __func__);
297 goto out;
298 }
299 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 response = ezusb_set_reset (serial, 1);
301
David Woodhouseec6752f2008-05-31 01:35:29 +0300302 record = (const struct ihex_binrec *)loader_fw->data;
303 while (record) {
304 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
305 (unsigned char *)record->data,
306 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (response < 0) {
308 err("%s - ezusb_writememory failed for loader (%d %04X %p %d)",
David Woodhouseec6752f2008-05-31 01:35:29 +0300309 __func__, response, be32_to_cpu(record->addr),
310 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300313 record = ihex_next_binrec(record);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 response = ezusb_set_reset (serial, 0);
317
David Woodhouseec6752f2008-05-31 01:35:29 +0300318 record = (const struct ihex_binrec *)firmware_fw->data;
319 while (record && be32_to_cpu(record->addr) < 0x1b40)
320 record = ihex_next_binrec(record);
321 while (record) {
322 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
323 (unsigned char *)record->data,
324 be16_to_cpu(record->len), 0xa3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (response < 0) {
326 err("%s - ezusb_writememory failed for first firmware step (%d %04X %p %d)",
David Woodhouseec6752f2008-05-31 01:35:29 +0300327 __func__, response, be32_to_cpu(record->addr),
328 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
330 }
331 ++record;
332 }
333
334 response = ezusb_set_reset (serial, 1);
335
David Woodhouseec6752f2008-05-31 01:35:29 +0300336 record = (const struct ihex_binrec *)firmware_fw->data;
337 while (record && be32_to_cpu(record->addr) < 0x1b40) {
338 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
339 (unsigned char *)record->data,
340 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (response < 0) {
342 err("%s - ezusb_writememory failed for second firmware step (%d %04X %p %d)",
David Woodhouseec6752f2008-05-31 01:35:29 +0300343 __func__, response, be32_to_cpu(record->addr),
344 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 break;
346 }
347 ++record;
348 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300349 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 response = ezusb_set_reset (serial, 0);
David Woodhouseec6752f2008-05-31 01:35:29 +0300351 out:
352 release_firmware(loader_fw);
353 release_firmware(firmware_fw);
354 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357
358static int whiteheat_firmware_attach (struct usb_serial *serial)
359{
360 /* We want this device to fail to have a driver assigned to it */
361 return 1;
362}
363
364
365/*****************************************************************************
366 * Connect Tech's White Heat serial driver functions
367 *****************************************************************************/
368static int whiteheat_attach (struct usb_serial *serial)
369{
370 struct usb_serial_port *command_port;
371 struct whiteheat_command_private *command_info;
372 struct usb_serial_port *port;
373 struct whiteheat_private *info;
374 struct whiteheat_hw_info *hw_info;
375 int pipe;
376 int ret;
377 int alen;
378 __u8 *command;
379 __u8 *result;
380 int i;
381 int j;
382 struct urb *urb;
383 int buf_size;
384 struct whiteheat_urb_wrap *wrap;
385 struct list_head *tmp;
386
387 command_port = serial->port[COMMAND_PORT];
388
389 pipe = usb_sndbulkpipe (serial->dev, command_port->bulk_out_endpointAddress);
390 command = kmalloc(2, GFP_KERNEL);
391 if (!command)
392 goto no_command_buffer;
393 command[0] = WHITEHEAT_GET_HW_INFO;
394 command[1] = 0;
395
396 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
397 if (!result)
398 goto no_result_buffer;
399 /*
400 * When the module is reloaded the firmware is still there and
401 * the endpoints are still in the usb core unchanged. This is the
402 * unlinking bug in disguise. Same for the call below.
403 */
404 usb_clear_halt(serial->dev, pipe);
405 ret = usb_bulk_msg (serial->dev, pipe, command, 2, &alen, COMMAND_TIMEOUT_MS);
406 if (ret) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700407 err("%s: Couldn't send command [%d]", serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400409 } else if (alen != 2) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700410 err("%s: Send command incomplete [%d]", serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 goto no_firmware;
412 }
413
414 pipe = usb_rcvbulkpipe (serial->dev, command_port->bulk_in_endpointAddress);
415 /* See the comment on the usb_clear_halt() above */
416 usb_clear_halt(serial->dev, pipe);
417 ret = usb_bulk_msg (serial->dev, pipe, result, sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
418 if (ret) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700419 err("%s: Couldn't get results [%d]", serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400421 } else if (alen != sizeof(*hw_info) + 1) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700422 err("%s: Get results incomplete [%d]", serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 goto no_firmware;
424 } else if (result[0] != command[0]) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700425 err("%s: Command failed [%d]", serial->type->description, result[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto no_firmware;
427 }
428
429 hw_info = (struct whiteheat_hw_info *)&result[1];
430
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700431 info("%s: Driver %s: Firmware v%d.%02d", serial->type->description,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 DRIVER_VERSION, hw_info->sw_major_rev, hw_info->sw_minor_rev);
433
434 for (i = 0; i < serial->num_ports; i++) {
435 port = serial->port[i];
436
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800437 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (info == NULL) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700439 err("%s: Out of memory for port structures\n", serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 goto no_private;
441 }
442
443 spin_lock_init(&info->lock);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200444 mutex_init(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 info->flags = 0;
446 info->mcr = 0;
David Howellsc4028952006-11-22 14:57:56 +0000447 INIT_WORK(&info->rx_work, rx_data_softint);
448 info->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 INIT_LIST_HEAD(&info->rx_urbs_free);
451 INIT_LIST_HEAD(&info->rx_urbs_submitted);
452 INIT_LIST_HEAD(&info->rx_urb_q);
453 INIT_LIST_HEAD(&info->tx_urbs_free);
454 INIT_LIST_HEAD(&info->tx_urbs_submitted);
455
456 for (j = 0; j < urb_pool_size; j++) {
457 urb = usb_alloc_urb(0, GFP_KERNEL);
458 if (!urb) {
459 err("No free urbs available");
460 goto no_rx_urb;
461 }
462 buf_size = port->read_urb->transfer_buffer_length;
463 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
464 if (!urb->transfer_buffer) {
465 err("Couldn't allocate urb buffer");
466 goto no_rx_buf;
467 }
468 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
469 if (!wrap) {
470 err("Couldn't allocate urb wrapper");
471 goto no_rx_wrap;
472 }
473 usb_fill_bulk_urb(urb, serial->dev,
474 usb_rcvbulkpipe(serial->dev,
475 port->bulk_in_endpointAddress),
476 urb->transfer_buffer, buf_size,
477 whiteheat_read_callback, port);
478 wrap->urb = urb;
479 list_add(&wrap->list, &info->rx_urbs_free);
480
481 urb = usb_alloc_urb(0, GFP_KERNEL);
482 if (!urb) {
483 err("No free urbs available");
484 goto no_tx_urb;
485 }
486 buf_size = port->write_urb->transfer_buffer_length;
487 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
488 if (!urb->transfer_buffer) {
489 err("Couldn't allocate urb buffer");
490 goto no_tx_buf;
491 }
492 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
493 if (!wrap) {
494 err("Couldn't allocate urb wrapper");
495 goto no_tx_wrap;
496 }
497 usb_fill_bulk_urb(urb, serial->dev,
498 usb_sndbulkpipe(serial->dev,
499 port->bulk_out_endpointAddress),
500 urb->transfer_buffer, buf_size,
501 whiteheat_write_callback, port);
502 wrap->urb = urb;
503 list_add(&wrap->list, &info->tx_urbs_free);
504 }
505
506 usb_set_serial_port_data(port, info);
507 }
508
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800509 command_info = kmalloc(sizeof(struct whiteheat_command_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (command_info == NULL) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700511 err("%s: Out of memory for port structures\n", serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto no_command_private;
513 }
514
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200515 mutex_init(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 command_info->port_running = 0;
517 init_waitqueue_head(&command_info->wait_command);
518 usb_set_serial_port_data(command_port, command_info);
519 command_port->write_urb->complete = command_port_write_callback;
520 command_port->read_urb->complete = command_port_read_callback;
521 kfree(result);
522 kfree(command);
523
524 return 0;
525
526no_firmware:
527 /* Firmware likely not running */
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700528 err("%s: Unable to retrieve firmware version, try replugging\n", serial->type->description);
529 err("%s: If the firmware is not running (status led not blinking)\n", serial->type->description);
530 err("%s: please contact support@connecttech.com\n", serial->type->description);
Jesper Juhl67ca0282006-04-23 19:59:23 +0200531 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return -ENODEV;
533
534no_command_private:
535 for (i = serial->num_ports - 1; i >= 0; i--) {
536 port = serial->port[i];
537 info = usb_get_serial_port_data(port);
538 for (j = urb_pool_size - 1; j >= 0; j--) {
539 tmp = list_first(&info->tx_urbs_free);
540 list_del(tmp);
541 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
542 urb = wrap->urb;
543 kfree(wrap);
544no_tx_wrap:
545 kfree(urb->transfer_buffer);
546no_tx_buf:
547 usb_free_urb(urb);
548no_tx_urb:
549 tmp = list_first(&info->rx_urbs_free);
550 list_del(tmp);
551 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
552 urb = wrap->urb;
553 kfree(wrap);
554no_rx_wrap:
555 kfree(urb->transfer_buffer);
556no_rx_buf:
557 usb_free_urb(urb);
558no_rx_urb:
559 ;
560 }
561 kfree(info);
562no_private:
563 ;
564 }
565 kfree(result);
566no_result_buffer:
567 kfree(command);
568no_command_buffer:
569 return -ENOMEM;
570}
571
572
573static void whiteheat_shutdown (struct usb_serial *serial)
574{
575 struct usb_serial_port *command_port;
576 struct usb_serial_port *port;
577 struct whiteheat_private *info;
578 struct whiteheat_urb_wrap *wrap;
579 struct urb *urb;
580 struct list_head *tmp;
581 struct list_head *tmp2;
582 int i;
583
Harvey Harrison441b62c2008-03-03 16:08:34 -0800584 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* free up our private data for our command port */
587 command_port = serial->port[COMMAND_PORT];
588 kfree (usb_get_serial_port_data(command_port));
589
590 for (i = 0; i < serial->num_ports; i++) {
591 port = serial->port[i];
592 info = usb_get_serial_port_data(port);
593 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
594 list_del(tmp);
595 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
596 urb = wrap->urb;
597 kfree(wrap);
598 kfree(urb->transfer_buffer);
599 usb_free_urb(urb);
600 }
601 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
602 list_del(tmp);
603 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
604 urb = wrap->urb;
605 kfree(wrap);
606 kfree(urb->transfer_buffer);
607 usb_free_urb(urb);
608 }
609 kfree(info);
610 }
611
612 return;
613}
614
615
616static int whiteheat_open (struct usb_serial_port *port, struct file *filp)
617{
618 int retval = 0;
Alan Cox606d0992006-12-08 02:38:45 -0800619 struct ktermios old_term;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Harvey Harrison441b62c2008-03-03 16:08:34 -0800621 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 retval = start_command_port(port->serial);
624 if (retval)
625 goto exit;
626
Oliver Neukume5867982007-12-20 12:23:03 +0100627 port->tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* send an open port command */
630 retval = firm_open(port);
631 if (retval) {
632 stop_command_port(port->serial);
633 goto exit;
634 }
635
636 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
637 if (retval) {
638 firm_close(port);
639 stop_command_port(port->serial);
640 goto exit;
641 }
642
643 old_term.c_cflag = ~port->tty->termios->c_cflag;
644 old_term.c_iflag = ~port->tty->termios->c_iflag;
645 whiteheat_set_termios(port, &old_term);
646
647 /* Work around HCD bugs */
648 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
649 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
650
651 /* Start reading from the device */
652 retval = start_port_read(port);
653 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800654 err("%s - failed submitting read urb, error %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 firm_close(port);
656 stop_command_port(port->serial);
657 goto exit;
658 }
659
660exit:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800661 dbg("%s - exit, retval = %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return retval;
663}
664
665
666static void whiteheat_close(struct usb_serial_port *port, struct file * filp)
667{
668 struct whiteheat_private *info = usb_get_serial_port_data(port);
669 struct whiteheat_urb_wrap *wrap;
670 struct urb *urb;
671 struct list_head *tmp;
672 struct list_head *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Harvey Harrison441b62c2008-03-03 16:08:34 -0800674 dbg("%s - port %d", __func__, port->number);
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100675
676 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* filp is NULL when called from usb_serial_disconnect */
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100678 if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
679 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return;
681 }
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100682 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 port->tty->closing = 1;
685
686/*
687 * Not currently in use; tty_wait_until_sent() calls
688 * serial_chars_in_buffer() which deadlocks on the second semaphore
689 * acquisition. This should be fixed at some point. Greg's been
690 * notified.
691 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
692 tty_wait_until_sent(port->tty, CLOSING_DELAY);
693 }
694*/
695
Alan Coxf34d7a52008-04-30 00:54:13 -0700696 tty_driver_flush_buffer(port->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 tty_ldisc_flush(port->tty);
698
699 firm_report_tx_done(port);
700
701 firm_close(port);
702
703 /* shutdown our bulk reads and writes */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200704 mutex_lock(&info->deathwarrant);
705 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
707 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
708 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200709 list_del(tmp);
710 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200712 spin_lock_irq(&info->lock);
713 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Akinobu Mita179e0912006-06-26 00:24:41 -0700715 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
716 list_move(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
718 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
719 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200720 list_del(tmp);
721 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200723 spin_lock_irq(&info->lock);
724 list_add(tmp, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200726 spin_unlock_irq(&info->lock);
727 mutex_unlock(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 stop_command_port(port->serial);
730
731 port->tty->closing = 0;
732}
733
734
735static int whiteheat_write(struct usb_serial_port *port, const unsigned char *buf, int count)
736{
737 struct usb_serial *serial = port->serial;
738 struct whiteheat_private *info = usb_get_serial_port_data(port);
739 struct whiteheat_urb_wrap *wrap;
740 struct urb *urb;
741 int result;
742 int bytes;
743 int sent = 0;
744 unsigned long flags;
745 struct list_head *tmp;
746
Harvey Harrison441b62c2008-03-03 16:08:34 -0800747 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800750 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return (0);
752 }
753
754 while (count) {
755 spin_lock_irqsave(&info->lock, flags);
756 if (list_empty(&info->tx_urbs_free)) {
757 spin_unlock_irqrestore(&info->lock, flags);
758 break;
759 }
760 tmp = list_first(&info->tx_urbs_free);
761 list_del(tmp);
762 spin_unlock_irqrestore(&info->lock, flags);
763
764 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
765 urb = wrap->urb;
766 bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count;
767 memcpy (urb->transfer_buffer, buf + sent, bytes);
768
Harvey Harrison441b62c2008-03-03 16:08:34 -0800769 usb_serial_debug_data(debug, &port->dev, __func__, bytes, urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 urb->dev = serial->dev;
772 urb->transfer_buffer_length = bytes;
773 result = usb_submit_urb(urb, GFP_ATOMIC);
774 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800775 err("%s - failed submitting write urb, error %d", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 sent = result;
777 spin_lock_irqsave(&info->lock, flags);
778 list_add(tmp, &info->tx_urbs_free);
779 spin_unlock_irqrestore(&info->lock, flags);
780 break;
781 } else {
782 sent += bytes;
783 count -= bytes;
784 spin_lock_irqsave(&info->lock, flags);
785 list_add(tmp, &info->tx_urbs_submitted);
786 spin_unlock_irqrestore(&info->lock, flags);
787 }
788 }
789
790 return sent;
791}
792
793
794static int whiteheat_write_room(struct usb_serial_port *port)
795{
796 struct whiteheat_private *info = usb_get_serial_port_data(port);
797 struct list_head *tmp;
798 int room = 0;
799 unsigned long flags;
800
Harvey Harrison441b62c2008-03-03 16:08:34 -0800801 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 spin_lock_irqsave(&info->lock, flags);
804 list_for_each(tmp, &info->tx_urbs_free)
805 room++;
806 spin_unlock_irqrestore(&info->lock, flags);
807 room *= port->bulk_out_size;
808
Harvey Harrison441b62c2008-03-03 16:08:34 -0800809 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return (room);
811}
812
813
814static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file)
815{
816 struct whiteheat_private *info = usb_get_serial_port_data(port);
817 unsigned int modem_signals = 0;
818
Harvey Harrison441b62c2008-03-03 16:08:34 -0800819 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 firm_get_dtr_rts(port);
822 if (info->mcr & UART_MCR_DTR)
823 modem_signals |= TIOCM_DTR;
824 if (info->mcr & UART_MCR_RTS)
825 modem_signals |= TIOCM_RTS;
826
827 return modem_signals;
828}
829
830
831static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file,
832 unsigned int set, unsigned int clear)
833{
834 struct whiteheat_private *info = usb_get_serial_port_data(port);
835
Harvey Harrison441b62c2008-03-03 16:08:34 -0800836 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (set & TIOCM_RTS)
839 info->mcr |= UART_MCR_RTS;
840 if (set & TIOCM_DTR)
841 info->mcr |= UART_MCR_DTR;
842
843 if (clear & TIOCM_RTS)
844 info->mcr &= ~UART_MCR_RTS;
845 if (clear & TIOCM_DTR)
846 info->mcr &= ~UART_MCR_DTR;
847
848 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
849 firm_set_rts(port, info->mcr & UART_MCR_RTS);
850 return 0;
851}
852
853
854static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
855{
856 struct serial_struct serstruct;
857 void __user *user_arg = (void __user *)arg;
858
Harvey Harrison441b62c2008-03-03 16:08:34 -0800859 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 switch (cmd) {
862 case TIOCGSERIAL:
863 memset(&serstruct, 0, sizeof(serstruct));
864 serstruct.type = PORT_16654;
865 serstruct.line = port->serial->minor;
866 serstruct.port = port->number;
867 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
868 serstruct.xmit_fifo_size = port->bulk_out_size;
869 serstruct.custom_divisor = 0;
870 serstruct.baud_base = 460800;
871 serstruct.close_delay = CLOSING_DELAY;
872 serstruct.closing_wait = CLOSING_DELAY;
873
874 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
875 return -EFAULT;
876
877 break;
878
879 case TIOCSSERIAL:
880 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
881 return -EFAULT;
882
883 /*
884 * For now this is ignored. dip sets the ASYNC_[V]HI flags
885 * but this isn't used by us at all. Maybe someone somewhere
886 * will need the custom_divisor setting.
887 */
888
889 break;
890
891 default:
892 break;
893 }
894
895 return -ENOIOCTLCMD;
896}
897
898
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200899static void whiteheat_set_termios(struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800901 dbg("%s -port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 firm_setup_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905
906static void whiteheat_break_ctl(struct usb_serial_port *port, int break_state) {
907 firm_set_break(port, break_state);
908}
909
910
911static int whiteheat_chars_in_buffer(struct usb_serial_port *port)
912{
913 struct whiteheat_private *info = usb_get_serial_port_data(port);
914 struct list_head *tmp;
915 struct whiteheat_urb_wrap *wrap;
916 int chars = 0;
917 unsigned long flags;
918
Harvey Harrison441b62c2008-03-03 16:08:34 -0800919 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 spin_lock_irqsave(&info->lock, flags);
922 list_for_each(tmp, &info->tx_urbs_submitted) {
923 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
924 chars += wrap->urb->transfer_buffer_length;
925 }
926 spin_unlock_irqrestore(&info->lock, flags);
927
Harvey Harrison441b62c2008-03-03 16:08:34 -0800928 dbg ("%s - returns %d", __func__, chars);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200929 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932
933static void whiteheat_throttle (struct usb_serial_port *port)
934{
935 struct whiteheat_private *info = usb_get_serial_port_data(port);
936 unsigned long flags;
937
Harvey Harrison441b62c2008-03-03 16:08:34 -0800938 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 spin_lock_irqsave(&info->lock, flags);
941 info->flags |= THROTTLED;
942 spin_unlock_irqrestore(&info->lock, flags);
943
944 return;
945}
946
947
948static void whiteheat_unthrottle (struct usb_serial_port *port)
949{
950 struct whiteheat_private *info = usb_get_serial_port_data(port);
951 int actually_throttled;
952 unsigned long flags;
953
Harvey Harrison441b62c2008-03-03 16:08:34 -0800954 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 spin_lock_irqsave(&info->lock, flags);
957 actually_throttled = info->flags & ACTUALLY_THROTTLED;
958 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
959 spin_unlock_irqrestore(&info->lock, flags);
960
961 if (actually_throttled)
David Howellsc4028952006-11-22 14:57:56 +0000962 rx_data_softint(&info->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 return;
965}
966
967
968/*****************************************************************************
969 * Connect Tech's White Heat callback routines
970 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200971static void command_port_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700973 int status = urb->status;
974
Harvey Harrison441b62c2008-03-03 16:08:34 -0800975 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700977 if (status) {
978 dbg("nonzero urb status: %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return;
980 }
981}
982
983
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200984static void command_port_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Ming Leicdc97792008-02-24 18:41:47 +0800986 struct usb_serial_port *command_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 struct whiteheat_command_private *command_info;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700988 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 unsigned char *data = urb->transfer_buffer;
990 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Harvey Harrison441b62c2008-03-03 16:08:34 -0800992 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 command_info = usb_get_serial_port_data(command_port);
995 if (!command_info) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800996 dbg ("%s - command_info is NULL, exiting.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return;
998 }
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700999 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001000 dbg("%s - nonzero urb status: %d", __func__, status);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001001 if (status != -ENOENT)
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001002 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1003 wake_up(&command_info->wait_command);
1004 return;
1005 }
1006
Harvey Harrison441b62c2008-03-03 16:08:34 -08001007 usb_serial_debug_data(debug, &command_port->dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1010 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001011 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1013 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001014 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 } else if (data[0] == WHITEHEAT_EVENT) {
1016 /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001017 dbg("%s - event received", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
1019 memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1);
1020 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001021 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001023 dbg("%s - bad reply from firmware", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
1026 /* Continue trying to always read */
1027 command_port->read_urb->dev = command_port->serial->dev;
1028 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001030 dbg("%s - failed resubmitting read urb, error %d", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033
David Howells7d12e782006-10-05 14:55:46 +01001034static void whiteheat_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Ming Leicdc97792008-02-24 18:41:47 +08001036 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct whiteheat_urb_wrap *wrap;
1038 unsigned char *data = urb->transfer_buffer;
1039 struct whiteheat_private *info = usb_get_serial_port_data(port);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001040 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Harvey Harrison441b62c2008-03-03 16:08:34 -08001042 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 spin_lock(&info->lock);
1045 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1046 if (!wrap) {
1047 spin_unlock(&info->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001048 err("%s - Not my urb!", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return;
1050 }
1051 list_del(&wrap->list);
1052 spin_unlock(&info->lock);
1053
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001054 if (status) {
1055 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001056 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 spin_lock(&info->lock);
1058 list_add(&wrap->list, &info->rx_urbs_free);
1059 spin_unlock(&info->lock);
1060 return;
1061 }
1062
Harvey Harrison441b62c2008-03-03 16:08:34 -08001063 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 spin_lock(&info->lock);
1066 list_add_tail(&wrap->list, &info->rx_urb_q);
1067 if (info->flags & THROTTLED) {
1068 info->flags |= ACTUALLY_THROTTLED;
1069 spin_unlock(&info->lock);
1070 return;
1071 }
1072 spin_unlock(&info->lock);
1073
1074 schedule_work(&info->rx_work);
1075}
1076
1077
David Howells7d12e782006-10-05 14:55:46 +01001078static void whiteheat_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Ming Leicdc97792008-02-24 18:41:47 +08001080 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct whiteheat_private *info = usb_get_serial_port_data(port);
1082 struct whiteheat_urb_wrap *wrap;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001083 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Harvey Harrison441b62c2008-03-03 16:08:34 -08001085 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 spin_lock(&info->lock);
1088 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1089 if (!wrap) {
1090 spin_unlock(&info->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001091 err("%s - Not my urb!", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return;
1093 }
Akinobu Mita179e0912006-06-26 00:24:41 -07001094 list_move(&wrap->list, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 spin_unlock(&info->lock);
1096
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001097 if (status) {
1098 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001099 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return;
1101 }
1102
Pete Zaitcevcf2c7482006-05-22 21:58:49 -07001103 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106
1107/*****************************************************************************
1108 * Connect Tech's White Heat firmware interface
1109 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001110static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 struct usb_serial_port *command_port;
1113 struct whiteheat_command_private *command_info;
1114 struct whiteheat_private *info;
1115 __u8 *transfer_buffer;
1116 int retval = 0;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001117 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Harvey Harrison441b62c2008-03-03 16:08:34 -08001119 dbg("%s - command %d", __func__, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 command_port = port->serial->port[COMMAND_PORT];
1122 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001123 mutex_lock(&command_info->mutex);
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001124 command_info->command_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1127 transfer_buffer[0] = command;
1128 memcpy (&transfer_buffer[1], data, datasize);
1129 command_port->write_urb->transfer_buffer_length = datasize + 1;
1130 command_port->write_urb->dev = port->serial->dev;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001131 retval = usb_submit_urb (command_port->write_urb, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001133 dbg("%s - submit urb failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 goto exit;
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /* wait for the command to complete */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001138 t = wait_event_timeout(command_info->wait_command,
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001139 (bool)command_info->command_finished, COMMAND_TIMEOUT);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001140 if (!t)
1141 usb_kill_urb(command_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001143 if (command_info->command_finished == false) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001144 dbg("%s - command timed out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 retval = -ETIMEDOUT;
1146 goto exit;
1147 }
1148
1149 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001150 dbg("%s - command failed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 retval = -EIO;
1152 goto exit;
1153 }
1154
1155 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001156 dbg("%s - command completed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 switch (command) {
1158 case WHITEHEAT_GET_DTR_RTS:
1159 info = usb_get_serial_port_data(port);
1160 memcpy(&info->mcr, command_info->result_buffer, sizeof(struct whiteheat_dr_info));
1161 break;
1162 }
1163 }
1164
1165exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001166 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return retval;
1168}
1169
1170
1171static int firm_open(struct usb_serial_port *port) {
1172 struct whiteheat_simple open_command;
1173
1174 open_command.port = port->number - port->serial->minor + 1;
1175 return firm_send_command(port, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command));
1176}
1177
1178
1179static int firm_close(struct usb_serial_port *port) {
1180 struct whiteheat_simple close_command;
1181
1182 close_command.port = port->number - port->serial->minor + 1;
1183 return firm_send_command(port, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command));
1184}
1185
1186
1187static int firm_setup_port(struct usb_serial_port *port) {
1188 struct whiteheat_port_settings port_settings;
1189 unsigned int cflag = port->tty->termios->c_cflag;
1190
1191 port_settings.port = port->number + 1;
1192
1193 /* get the byte size */
1194 switch (cflag & CSIZE) {
1195 case CS5: port_settings.bits = 5; break;
1196 case CS6: port_settings.bits = 6; break;
1197 case CS7: port_settings.bits = 7; break;
1198 default:
1199 case CS8: port_settings.bits = 8; break;
1200 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001201 dbg("%s - data bits = %d", __func__, port_settings.bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 /* determine the parity */
1204 if (cflag & PARENB)
1205 if (cflag & CMSPAR)
1206 if (cflag & PARODD)
1207 port_settings.parity = WHITEHEAT_PAR_MARK;
1208 else
1209 port_settings.parity = WHITEHEAT_PAR_SPACE;
1210 else
1211 if (cflag & PARODD)
1212 port_settings.parity = WHITEHEAT_PAR_ODD;
1213 else
1214 port_settings.parity = WHITEHEAT_PAR_EVEN;
1215 else
1216 port_settings.parity = WHITEHEAT_PAR_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001217 dbg("%s - parity = %c", __func__, port_settings.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /* figure out the stop bits requested */
1220 if (cflag & CSTOPB)
1221 port_settings.stop = 2;
1222 else
1223 port_settings.stop = 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001224 dbg("%s - stop bits = %d", __func__, port_settings.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* figure out the flow control settings */
1227 if (cflag & CRTSCTS)
1228 port_settings.hflow = (WHITEHEAT_HFLOW_CTS | WHITEHEAT_HFLOW_RTS);
1229 else
1230 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001231 dbg("%s - hardware flow control = %s %s %s %s", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1233 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1234 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1235 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1236
1237 /* determine software flow control */
1238 if (I_IXOFF(port->tty))
1239 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1240 else
1241 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001242 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 port_settings.xon = START_CHAR(port->tty);
1245 port_settings.xoff = STOP_CHAR(port->tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001246 dbg("%s - XON = %2x, XOFF = %2x", __func__, port_settings.xon, port_settings.xoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /* get the baud rate wanted */
1249 port_settings.baud = tty_get_baud_rate(port->tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001250 dbg("%s - baud rate = %d", __func__, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Alan Cox01d1df22007-10-18 01:24:23 -07001252 /* fixme: should set validated settings */
1253 tty_encode_baud_rate(port->tty, port_settings.baud, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /* handle any settings that aren't specified in the tty structure */
1255 port_settings.lloop = 0;
1256
1257 /* now send the message to the device */
1258 return firm_send_command(port, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings));
1259}
1260
1261
1262static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) {
1263 struct whiteheat_set_rdb rts_command;
1264
1265 rts_command.port = port->number - port->serial->minor + 1;
1266 rts_command.state = onoff;
1267 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&rts_command, sizeof(rts_command));
1268}
1269
1270
1271static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) {
1272 struct whiteheat_set_rdb dtr_command;
1273
1274 dtr_command.port = port->number - port->serial->minor + 1;
1275 dtr_command.state = onoff;
1276 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&dtr_command, sizeof(dtr_command));
1277}
1278
1279
1280static int firm_set_break(struct usb_serial_port *port, __u8 onoff) {
1281 struct whiteheat_set_rdb break_command;
1282
1283 break_command.port = port->number - port->serial->minor + 1;
1284 break_command.state = onoff;
1285 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&break_command, sizeof(break_command));
1286}
1287
1288
1289static int firm_purge(struct usb_serial_port *port, __u8 rxtx) {
1290 struct whiteheat_purge purge_command;
1291
1292 purge_command.port = port->number - port->serial->minor + 1;
1293 purge_command.what = rxtx;
1294 return firm_send_command(port, WHITEHEAT_PURGE, (__u8 *)&purge_command, sizeof(purge_command));
1295}
1296
1297
1298static int firm_get_dtr_rts(struct usb_serial_port *port) {
1299 struct whiteheat_simple get_dr_command;
1300
1301 get_dr_command.port = port->number - port->serial->minor + 1;
1302 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, (__u8 *)&get_dr_command, sizeof(get_dr_command));
1303}
1304
1305
1306static int firm_report_tx_done(struct usb_serial_port *port) {
1307 struct whiteheat_simple close_command;
1308
1309 close_command.port = port->number - port->serial->minor + 1;
1310 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, (__u8 *)&close_command, sizeof(close_command));
1311}
1312
1313
1314/*****************************************************************************
1315 * Connect Tech's White Heat utility functions
1316 *****************************************************************************/
1317static int start_command_port(struct usb_serial *serial)
1318{
1319 struct usb_serial_port *command_port;
1320 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int retval = 0;
1322
1323 command_port = serial->port[COMMAND_PORT];
1324 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001325 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (!command_info->port_running) {
1327 /* Work around HCD bugs */
1328 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1329
1330 command_port->read_urb->dev = serial->dev;
1331 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1332 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001333 err("%s - failed submitting read urb, error %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 goto exit;
1335 }
1336 }
1337 command_info->port_running++;
1338
1339exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001340 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return retval;
1342}
1343
1344
1345static void stop_command_port(struct usb_serial *serial)
1346{
1347 struct usb_serial_port *command_port;
1348 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 command_port = serial->port[COMMAND_PORT];
1351 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001352 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 command_info->port_running--;
1354 if (!command_info->port_running)
1355 usb_kill_urb(command_port->read_urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001356 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359
1360static int start_port_read(struct usb_serial_port *port)
1361{
1362 struct whiteheat_private *info = usb_get_serial_port_data(port);
1363 struct whiteheat_urb_wrap *wrap;
1364 struct urb *urb;
1365 int retval = 0;
1366 unsigned long flags;
1367 struct list_head *tmp;
1368 struct list_head *tmp2;
1369
1370 spin_lock_irqsave(&info->lock, flags);
1371
1372 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1373 list_del(tmp);
1374 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1375 urb = wrap->urb;
1376 urb->dev = port->serial->dev;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001377 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 retval = usb_submit_urb(urb, GFP_KERNEL);
1379 if (retval) {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001380 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 list_add(tmp, &info->rx_urbs_free);
1382 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1383 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1384 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001385 list_del(tmp);
1386 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001388 spin_lock_irqsave(&info->lock, flags);
1389 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391 break;
1392 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001393 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 list_add(tmp, &info->rx_urbs_submitted);
1395 }
1396
1397 spin_unlock_irqrestore(&info->lock, flags);
1398
1399 return retval;
1400}
1401
1402
1403static struct whiteheat_urb_wrap *urb_to_wrap(struct urb* urb, struct list_head *head)
1404{
1405 struct whiteheat_urb_wrap *wrap;
1406 struct list_head *tmp;
1407
1408 list_for_each(tmp, head) {
1409 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1410 if (wrap->urb == urb)
1411 return wrap;
1412 }
1413
1414 return NULL;
1415}
1416
1417
1418static struct list_head *list_first(struct list_head *head)
1419{
1420 return head->next;
1421}
1422
1423
David Howellsc4028952006-11-22 14:57:56 +00001424static void rx_data_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
David Howellsc4028952006-11-22 14:57:56 +00001426 struct whiteheat_private *info =
1427 container_of(work, struct whiteheat_private, rx_work);
1428 struct usb_serial_port *port = info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct tty_struct *tty = port->tty;
1430 struct whiteheat_urb_wrap *wrap;
1431 struct urb *urb;
1432 unsigned long flags;
1433 struct list_head *tmp;
1434 struct list_head *tmp2;
1435 int result;
1436 int sent = 0;
1437
1438 spin_lock_irqsave(&info->lock, flags);
1439 if (info->flags & THROTTLED) {
1440 spin_unlock_irqrestore(&info->lock, flags);
1441 return;
1442 }
1443
1444 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1445 list_del(tmp);
1446 spin_unlock_irqrestore(&info->lock, flags);
1447
1448 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1449 urb = wrap->urb;
1450
1451 if (tty && urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -08001452 int len = tty_buffer_request_room(tty, urb->actual_length);
1453 /* This stuff can go away now I suspect */
1454 if (unlikely(len < urb->actual_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 spin_lock_irqsave(&info->lock, flags);
1456 list_add(tmp, &info->rx_urb_q);
1457 spin_unlock_irqrestore(&info->lock, flags);
1458 tty_flip_buffer_push(tty);
1459 schedule_work(&info->rx_work);
1460 return;
1461 }
Alan Cox33f0f882006-01-09 20:54:13 -08001462 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1463 sent += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465
1466 urb->dev = port->serial->dev;
1467 result = usb_submit_urb(urb, GFP_ATOMIC);
1468 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001469 err("%s - failed resubmitting read urb, error %d", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 spin_lock_irqsave(&info->lock, flags);
1471 list_add(tmp, &info->rx_urbs_free);
1472 continue;
1473 }
1474
1475 spin_lock_irqsave(&info->lock, flags);
1476 list_add(tmp, &info->rx_urbs_submitted);
1477 }
1478 spin_unlock_irqrestore(&info->lock, flags);
1479
1480 if (sent)
1481 tty_flip_buffer_push(tty);
1482}
1483
1484
1485/*****************************************************************************
1486 * Connect Tech's White Heat module functions
1487 *****************************************************************************/
1488static int __init whiteheat_init (void)
1489{
1490 int retval;
1491 retval = usb_serial_register(&whiteheat_fake_device);
1492 if (retval)
1493 goto failed_fake_register;
1494 retval = usb_serial_register(&whiteheat_device);
1495 if (retval)
1496 goto failed_device_register;
1497 retval = usb_register(&whiteheat_driver);
1498 if (retval)
1499 goto failed_usb_register;
1500 info(DRIVER_DESC " " DRIVER_VERSION);
1501 return 0;
1502failed_usb_register:
1503 usb_serial_deregister(&whiteheat_device);
1504failed_device_register:
1505 usb_serial_deregister(&whiteheat_fake_device);
1506failed_fake_register:
1507 return retval;
1508}
1509
1510
1511static void __exit whiteheat_exit (void)
1512{
1513 usb_deregister (&whiteheat_driver);
1514 usb_serial_deregister (&whiteheat_fake_device);
1515 usb_serial_deregister (&whiteheat_device);
1516}
1517
1518
1519module_init(whiteheat_init);
1520module_exit(whiteheat_exit);
1521
1522MODULE_AUTHOR( DRIVER_AUTHOR );
1523MODULE_DESCRIPTION( DRIVER_DESC );
1524MODULE_LICENSE("GPL");
1525
David Woodhouseec6752f2008-05-31 01:35:29 +03001526MODULE_FIRMWARE("whiteheat.fw");
1527MODULE_FIRMWARE("whiteheat_loader.fw");
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529module_param(urb_pool_size, int, 0);
1530MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1531
1532module_param(debug, bool, S_IRUGO | S_IWUSR);
1533MODULE_PARM_DESC(debug, "Debug enabled or not");