blob: b07d6a5cac31bb5596de58c7f23539bafec7582a [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);
Alan Cox95da3102008-07-22 11:09:07 +0100145static int whiteheat_open (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
146static void whiteheat_close (struct tty_struct *tty, struct usb_serial_port *port, struct file *filp);
147static int whiteheat_write (struct tty_struct *tty, struct usb_serial_port *port, const unsigned char *buf, int count);
148static int whiteheat_write_room (struct tty_struct *tty);
149static int whiteheat_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg);
150static void whiteheat_set_termios (struct tty_struct *tty, struct usb_serial_port *port, struct ktermios * old);
151static int whiteheat_tiocmget (struct tty_struct *tty, struct file *file);
152static int whiteheat_tiocmset (struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear);
153static void whiteheat_break_ctl (struct tty_struct *tty, int break_state);
154static int whiteheat_chars_in_buffer (struct tty_struct *tty);
155static void whiteheat_throttle (struct tty_struct *tty);
156static void whiteheat_unthrottle (struct tty_struct *tty);
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);
Alan Cox95da3102008-07-22 11:09:07 +0100249static int firm_setup_port(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static 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 Juhl67ca02842006-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
Alan Cox95da3102008-07-22 11:09:07 +0100616static int whiteheat_open (struct tty_struct *tty,
617 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 int retval = 0;
Alan Cox606d0992006-12-08 02:38:45 -0800620 struct ktermios old_term;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Harvey Harrison441b62c2008-03-03 16:08:34 -0800622 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 retval = start_command_port(port->serial);
625 if (retval)
626 goto exit;
627
Alan Cox95da3102008-07-22 11:09:07 +0100628 if (tty)
629 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 /* send an open port command */
632 retval = firm_open(port);
633 if (retval) {
634 stop_command_port(port->serial);
635 goto exit;
636 }
637
638 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
639 if (retval) {
640 firm_close(port);
641 stop_command_port(port->serial);
642 goto exit;
643 }
644
Alan Cox95da3102008-07-22 11:09:07 +0100645 if (tty) {
646 old_term.c_cflag = ~tty->termios->c_cflag;
647 old_term.c_iflag = ~tty->termios->c_iflag;
648 whiteheat_set_termios(tty, port, &old_term);
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 /* Work around HCD bugs */
652 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
653 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
654
655 /* Start reading from the device */
656 retval = start_port_read(port);
657 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800658 err("%s - failed submitting read urb, error %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 firm_close(port);
660 stop_command_port(port->serial);
661 goto exit;
662 }
663
664exit:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800665 dbg("%s - exit, retval = %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return retval;
667}
668
669
Alan Cox95da3102008-07-22 11:09:07 +0100670static void whiteheat_close(struct tty_struct *tty,
671 struct usb_serial_port *port, struct file * filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct whiteheat_private *info = usb_get_serial_port_data(port);
674 struct whiteheat_urb_wrap *wrap;
675 struct urb *urb;
676 struct list_head *tmp;
677 struct list_head *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Harvey Harrison441b62c2008-03-03 16:08:34 -0800679 dbg("%s - port %d", __func__, port->number);
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100680
681 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* filp is NULL when called from usb_serial_disconnect */
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100683 if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
684 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return;
686 }
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100687 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Alan Cox95da3102008-07-22 11:09:07 +0100689 tty->closing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691/*
692 * Not currently in use; tty_wait_until_sent() calls
693 * serial_chars_in_buffer() which deadlocks on the second semaphore
694 * acquisition. This should be fixed at some point. Greg's been
695 * notified.
696 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
Alan Cox95da3102008-07-22 11:09:07 +0100697 tty_wait_until_sent(tty, CLOSING_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699*/
700
Alan Cox95da3102008-07-22 11:09:07 +0100701 tty_driver_flush_buffer(tty);
702 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 firm_report_tx_done(port);
705
706 firm_close(port);
707
708 /* shutdown our bulk reads and writes */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200709 mutex_lock(&info->deathwarrant);
710 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
712 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
713 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200714 list_del(tmp);
715 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200717 spin_lock_irq(&info->lock);
718 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Akinobu Mita179e0912006-06-26 00:24:41 -0700720 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
721 list_move(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
723 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
724 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200725 list_del(tmp);
726 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200728 spin_lock_irq(&info->lock);
729 list_add(tmp, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200731 spin_unlock_irq(&info->lock);
732 mutex_unlock(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 stop_command_port(port->serial);
735
Alan Cox95da3102008-07-22 11:09:07 +0100736 tty->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739
Alan Cox95da3102008-07-22 11:09:07 +0100740static int whiteheat_write(struct tty_struct *tty,
741 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct usb_serial *serial = port->serial;
744 struct whiteheat_private *info = usb_get_serial_port_data(port);
745 struct whiteheat_urb_wrap *wrap;
746 struct urb *urb;
747 int result;
748 int bytes;
749 int sent = 0;
750 unsigned long flags;
751 struct list_head *tmp;
752
Harvey Harrison441b62c2008-03-03 16:08:34 -0800753 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800756 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return (0);
758 }
759
760 while (count) {
761 spin_lock_irqsave(&info->lock, flags);
762 if (list_empty(&info->tx_urbs_free)) {
763 spin_unlock_irqrestore(&info->lock, flags);
764 break;
765 }
766 tmp = list_first(&info->tx_urbs_free);
767 list_del(tmp);
768 spin_unlock_irqrestore(&info->lock, flags);
769
770 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
771 urb = wrap->urb;
772 bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count;
773 memcpy (urb->transfer_buffer, buf + sent, bytes);
774
Harvey Harrison441b62c2008-03-03 16:08:34 -0800775 usb_serial_debug_data(debug, &port->dev, __func__, bytes, urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 urb->dev = serial->dev;
778 urb->transfer_buffer_length = bytes;
779 result = usb_submit_urb(urb, GFP_ATOMIC);
780 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800781 err("%s - failed submitting write urb, error %d", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 sent = result;
783 spin_lock_irqsave(&info->lock, flags);
784 list_add(tmp, &info->tx_urbs_free);
785 spin_unlock_irqrestore(&info->lock, flags);
786 break;
787 } else {
788 sent += bytes;
789 count -= bytes;
790 spin_lock_irqsave(&info->lock, flags);
791 list_add(tmp, &info->tx_urbs_submitted);
792 spin_unlock_irqrestore(&info->lock, flags);
793 }
794 }
795
796 return sent;
797}
798
799
Alan Cox95da3102008-07-22 11:09:07 +0100800static int whiteheat_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Alan Cox95da3102008-07-22 11:09:07 +0100802 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct whiteheat_private *info = usb_get_serial_port_data(port);
804 struct list_head *tmp;
805 int room = 0;
806 unsigned long flags;
807
Harvey Harrison441b62c2008-03-03 16:08:34 -0800808 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 spin_lock_irqsave(&info->lock, flags);
811 list_for_each(tmp, &info->tx_urbs_free)
812 room++;
813 spin_unlock_irqrestore(&info->lock, flags);
814 room *= port->bulk_out_size;
815
Harvey Harrison441b62c2008-03-03 16:08:34 -0800816 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return (room);
818}
819
820
Alan Cox95da3102008-07-22 11:09:07 +0100821static int whiteheat_tiocmget (struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Alan Cox95da3102008-07-22 11:09:07 +0100823 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct whiteheat_private *info = usb_get_serial_port_data(port);
825 unsigned int modem_signals = 0;
826
Harvey Harrison441b62c2008-03-03 16:08:34 -0800827 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 firm_get_dtr_rts(port);
830 if (info->mcr & UART_MCR_DTR)
831 modem_signals |= TIOCM_DTR;
832 if (info->mcr & UART_MCR_RTS)
833 modem_signals |= TIOCM_RTS;
834
835 return modem_signals;
836}
837
838
Alan Cox95da3102008-07-22 11:09:07 +0100839static int whiteheat_tiocmset (struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 unsigned int set, unsigned int clear)
841{
Alan Cox95da3102008-07-22 11:09:07 +0100842 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct whiteheat_private *info = usb_get_serial_port_data(port);
844
Harvey Harrison441b62c2008-03-03 16:08:34 -0800845 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if (set & TIOCM_RTS)
848 info->mcr |= UART_MCR_RTS;
849 if (set & TIOCM_DTR)
850 info->mcr |= UART_MCR_DTR;
851
852 if (clear & TIOCM_RTS)
853 info->mcr &= ~UART_MCR_RTS;
854 if (clear & TIOCM_DTR)
855 info->mcr &= ~UART_MCR_DTR;
856
857 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
858 firm_set_rts(port, info->mcr & UART_MCR_RTS);
859 return 0;
860}
861
862
Alan Cox95da3102008-07-22 11:09:07 +0100863static int whiteheat_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Alan Cox95da3102008-07-22 11:09:07 +0100865 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 struct serial_struct serstruct;
867 void __user *user_arg = (void __user *)arg;
868
Harvey Harrison441b62c2008-03-03 16:08:34 -0800869 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 switch (cmd) {
872 case TIOCGSERIAL:
873 memset(&serstruct, 0, sizeof(serstruct));
874 serstruct.type = PORT_16654;
875 serstruct.line = port->serial->minor;
876 serstruct.port = port->number;
877 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
878 serstruct.xmit_fifo_size = port->bulk_out_size;
879 serstruct.custom_divisor = 0;
880 serstruct.baud_base = 460800;
881 serstruct.close_delay = CLOSING_DELAY;
882 serstruct.closing_wait = CLOSING_DELAY;
883
884 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
885 return -EFAULT;
886
887 break;
888
889 case TIOCSSERIAL:
890 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
891 return -EFAULT;
892
893 /*
894 * For now this is ignored. dip sets the ASYNC_[V]HI flags
895 * but this isn't used by us at all. Maybe someone somewhere
896 * will need the custom_divisor setting.
897 */
898
899 break;
900
901 default:
902 break;
903 }
904
905 return -ENOIOCTLCMD;
906}
907
908
Alan Cox95da3102008-07-22 11:09:07 +0100909static void whiteheat_set_termios(struct tty_struct *tty, struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Alan Cox95da3102008-07-22 11:09:07 +0100911 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914
Alan Cox95da3102008-07-22 11:09:07 +0100915static void whiteheat_break_ctl(struct tty_struct *tty, int break_state) {
916 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 firm_set_break(port, break_state);
918}
919
920
Alan Cox95da3102008-07-22 11:09:07 +0100921static int whiteheat_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Alan Cox95da3102008-07-22 11:09:07 +0100923 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct whiteheat_private *info = usb_get_serial_port_data(port);
925 struct list_head *tmp;
926 struct whiteheat_urb_wrap *wrap;
927 int chars = 0;
928 unsigned long flags;
929
Harvey Harrison441b62c2008-03-03 16:08:34 -0800930 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 spin_lock_irqsave(&info->lock, flags);
933 list_for_each(tmp, &info->tx_urbs_submitted) {
934 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
935 chars += wrap->urb->transfer_buffer_length;
936 }
937 spin_unlock_irqrestore(&info->lock, flags);
938
Harvey Harrison441b62c2008-03-03 16:08:34 -0800939 dbg ("%s - returns %d", __func__, chars);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200940 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
943
Alan Cox95da3102008-07-22 11:09:07 +0100944static void whiteheat_throttle (struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Alan Cox95da3102008-07-22 11:09:07 +0100946 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 struct whiteheat_private *info = usb_get_serial_port_data(port);
948 unsigned long flags;
949
Harvey Harrison441b62c2008-03-03 16:08:34 -0800950 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 spin_lock_irqsave(&info->lock, flags);
953 info->flags |= THROTTLED;
954 spin_unlock_irqrestore(&info->lock, flags);
955
956 return;
957}
958
959
Alan Cox95da3102008-07-22 11:09:07 +0100960static void whiteheat_unthrottle (struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Alan Cox95da3102008-07-22 11:09:07 +0100962 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 struct whiteheat_private *info = usb_get_serial_port_data(port);
964 int actually_throttled;
965 unsigned long flags;
966
Harvey Harrison441b62c2008-03-03 16:08:34 -0800967 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 spin_lock_irqsave(&info->lock, flags);
970 actually_throttled = info->flags & ACTUALLY_THROTTLED;
971 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
972 spin_unlock_irqrestore(&info->lock, flags);
973
974 if (actually_throttled)
David Howellsc4028952006-11-22 14:57:56 +0000975 rx_data_softint(&info->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 return;
978}
979
980
981/*****************************************************************************
982 * Connect Tech's White Heat callback routines
983 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200984static void command_port_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700986 int status = urb->status;
987
Harvey Harrison441b62c2008-03-03 16:08:34 -0800988 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700990 if (status) {
991 dbg("nonzero urb status: %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return;
993 }
994}
995
996
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200997static void command_port_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Ming Leicdc97792008-02-24 18:41:47 +0800999 struct usb_serial_port *command_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 struct whiteheat_command_private *command_info;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001001 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 unsigned char *data = urb->transfer_buffer;
1003 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Harvey Harrison441b62c2008-03-03 16:08:34 -08001005 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 command_info = usb_get_serial_port_data(command_port);
1008 if (!command_info) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001009 dbg ("%s - command_info is NULL, exiting.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return;
1011 }
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001012 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001013 dbg("%s - nonzero urb status: %d", __func__, status);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001014 if (status != -ENOENT)
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001015 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1016 wake_up(&command_info->wait_command);
1017 return;
1018 }
1019
Harvey Harrison441b62c2008-03-03 16:08:34 -08001020 usb_serial_debug_data(debug, &command_port->dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1023 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001024 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1026 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001027 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 } else if (data[0] == WHITEHEAT_EVENT) {
1029 /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001030 dbg("%s - event received", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
1032 memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1);
1033 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001034 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001036 dbg("%s - bad reply from firmware", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
1039 /* Continue trying to always read */
1040 command_port->read_urb->dev = command_port->serial->dev;
1041 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (result)
Harvey Harrison441b62c2008-03-03 16:08:34 -08001043 dbg("%s - failed resubmitting read urb, error %d", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}
1045
1046
David Howells7d12e782006-10-05 14:55:46 +01001047static void whiteheat_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Ming Leicdc97792008-02-24 18:41:47 +08001049 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct whiteheat_urb_wrap *wrap;
1051 unsigned char *data = urb->transfer_buffer;
1052 struct whiteheat_private *info = usb_get_serial_port_data(port);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001053 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Harvey Harrison441b62c2008-03-03 16:08:34 -08001055 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 spin_lock(&info->lock);
1058 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1059 if (!wrap) {
1060 spin_unlock(&info->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001061 err("%s - Not my urb!", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return;
1063 }
1064 list_del(&wrap->list);
1065 spin_unlock(&info->lock);
1066
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001067 if (status) {
1068 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001069 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 spin_lock(&info->lock);
1071 list_add(&wrap->list, &info->rx_urbs_free);
1072 spin_unlock(&info->lock);
1073 return;
1074 }
1075
Harvey Harrison441b62c2008-03-03 16:08:34 -08001076 usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 spin_lock(&info->lock);
1079 list_add_tail(&wrap->list, &info->rx_urb_q);
1080 if (info->flags & THROTTLED) {
1081 info->flags |= ACTUALLY_THROTTLED;
1082 spin_unlock(&info->lock);
1083 return;
1084 }
1085 spin_unlock(&info->lock);
1086
1087 schedule_work(&info->rx_work);
1088}
1089
1090
David Howells7d12e782006-10-05 14:55:46 +01001091static void whiteheat_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Ming Leicdc97792008-02-24 18:41:47 +08001093 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct whiteheat_private *info = usb_get_serial_port_data(port);
1095 struct whiteheat_urb_wrap *wrap;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001096 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Harvey Harrison441b62c2008-03-03 16:08:34 -08001098 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 spin_lock(&info->lock);
1101 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1102 if (!wrap) {
1103 spin_unlock(&info->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001104 err("%s - Not my urb!", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return;
1106 }
Akinobu Mita179e0912006-06-26 00:24:41 -07001107 list_move(&wrap->list, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 spin_unlock(&info->lock);
1109
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001110 if (status) {
1111 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001112 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return;
1114 }
1115
Pete Zaitcevcf2c7482006-05-22 21:58:49 -07001116 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
1119
1120/*****************************************************************************
1121 * Connect Tech's White Heat firmware interface
1122 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001123static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 struct usb_serial_port *command_port;
1126 struct whiteheat_command_private *command_info;
1127 struct whiteheat_private *info;
1128 __u8 *transfer_buffer;
1129 int retval = 0;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001130 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Harvey Harrison441b62c2008-03-03 16:08:34 -08001132 dbg("%s - command %d", __func__, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 command_port = port->serial->port[COMMAND_PORT];
1135 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001136 mutex_lock(&command_info->mutex);
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001137 command_info->command_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1140 transfer_buffer[0] = command;
1141 memcpy (&transfer_buffer[1], data, datasize);
1142 command_port->write_urb->transfer_buffer_length = datasize + 1;
1143 command_port->write_urb->dev = port->serial->dev;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001144 retval = usb_submit_urb (command_port->write_urb, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001146 dbg("%s - submit urb failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 goto exit;
1148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /* wait for the command to complete */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001151 t = wait_event_timeout(command_info->wait_command,
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001152 (bool)command_info->command_finished, COMMAND_TIMEOUT);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001153 if (!t)
1154 usb_kill_urb(command_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001156 if (command_info->command_finished == false) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001157 dbg("%s - command timed out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 retval = -ETIMEDOUT;
1159 goto exit;
1160 }
1161
1162 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001163 dbg("%s - command failed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 retval = -EIO;
1165 goto exit;
1166 }
1167
1168 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001169 dbg("%s - command completed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 switch (command) {
1171 case WHITEHEAT_GET_DTR_RTS:
1172 info = usb_get_serial_port_data(port);
1173 memcpy(&info->mcr, command_info->result_buffer, sizeof(struct whiteheat_dr_info));
1174 break;
1175 }
1176 }
1177
1178exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001179 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return retval;
1181}
1182
1183
1184static int firm_open(struct usb_serial_port *port) {
1185 struct whiteheat_simple open_command;
1186
1187 open_command.port = port->number - port->serial->minor + 1;
1188 return firm_send_command(port, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command));
1189}
1190
1191
1192static int firm_close(struct usb_serial_port *port) {
1193 struct whiteheat_simple close_command;
1194
1195 close_command.port = port->number - port->serial->minor + 1;
1196 return firm_send_command(port, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command));
1197}
1198
1199
Alan Cox95da3102008-07-22 11:09:07 +01001200static int firm_setup_port(struct tty_struct *tty) {
1201 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct whiteheat_port_settings port_settings;
Alan Cox95da3102008-07-22 11:09:07 +01001203 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 port_settings.port = port->number + 1;
1206
1207 /* get the byte size */
1208 switch (cflag & CSIZE) {
1209 case CS5: port_settings.bits = 5; break;
1210 case CS6: port_settings.bits = 6; break;
1211 case CS7: port_settings.bits = 7; break;
1212 default:
1213 case CS8: port_settings.bits = 8; break;
1214 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001215 dbg("%s - data bits = %d", __func__, port_settings.bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /* determine the parity */
1218 if (cflag & PARENB)
1219 if (cflag & CMSPAR)
1220 if (cflag & PARODD)
1221 port_settings.parity = WHITEHEAT_PAR_MARK;
1222 else
1223 port_settings.parity = WHITEHEAT_PAR_SPACE;
1224 else
1225 if (cflag & PARODD)
1226 port_settings.parity = WHITEHEAT_PAR_ODD;
1227 else
1228 port_settings.parity = WHITEHEAT_PAR_EVEN;
1229 else
1230 port_settings.parity = WHITEHEAT_PAR_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001231 dbg("%s - parity = %c", __func__, port_settings.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 /* figure out the stop bits requested */
1234 if (cflag & CSTOPB)
1235 port_settings.stop = 2;
1236 else
1237 port_settings.stop = 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001238 dbg("%s - stop bits = %d", __func__, port_settings.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /* figure out the flow control settings */
1241 if (cflag & CRTSCTS)
1242 port_settings.hflow = (WHITEHEAT_HFLOW_CTS | WHITEHEAT_HFLOW_RTS);
1243 else
1244 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001245 dbg("%s - hardware flow control = %s %s %s %s", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1247 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1248 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1249 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1250
1251 /* determine software flow control */
Alan Cox95da3102008-07-22 11:09:07 +01001252 if (I_IXOFF(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1254 else
1255 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001256 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Alan Cox95da3102008-07-22 11:09:07 +01001258 port_settings.xon = START_CHAR(tty);
1259 port_settings.xoff = STOP_CHAR(tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001260 dbg("%s - XON = %2x, XOFF = %2x", __func__, port_settings.xon, port_settings.xoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 /* get the baud rate wanted */
Alan Cox95da3102008-07-22 11:09:07 +01001263 port_settings.baud = tty_get_baud_rate(tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001264 dbg("%s - baud rate = %d", __func__, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Alan Cox01d1df22007-10-18 01:24:23 -07001266 /* fixme: should set validated settings */
Alan Cox95da3102008-07-22 11:09:07 +01001267 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /* handle any settings that aren't specified in the tty structure */
1269 port_settings.lloop = 0;
1270
1271 /* now send the message to the device */
1272 return firm_send_command(port, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings));
1273}
1274
1275
1276static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) {
1277 struct whiteheat_set_rdb rts_command;
1278
1279 rts_command.port = port->number - port->serial->minor + 1;
1280 rts_command.state = onoff;
1281 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&rts_command, sizeof(rts_command));
1282}
1283
1284
1285static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) {
1286 struct whiteheat_set_rdb dtr_command;
1287
1288 dtr_command.port = port->number - port->serial->minor + 1;
1289 dtr_command.state = onoff;
1290 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&dtr_command, sizeof(dtr_command));
1291}
1292
1293
1294static int firm_set_break(struct usb_serial_port *port, __u8 onoff) {
1295 struct whiteheat_set_rdb break_command;
1296
1297 break_command.port = port->number - port->serial->minor + 1;
1298 break_command.state = onoff;
1299 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&break_command, sizeof(break_command));
1300}
1301
1302
1303static int firm_purge(struct usb_serial_port *port, __u8 rxtx) {
1304 struct whiteheat_purge purge_command;
1305
1306 purge_command.port = port->number - port->serial->minor + 1;
1307 purge_command.what = rxtx;
1308 return firm_send_command(port, WHITEHEAT_PURGE, (__u8 *)&purge_command, sizeof(purge_command));
1309}
1310
1311
1312static int firm_get_dtr_rts(struct usb_serial_port *port) {
1313 struct whiteheat_simple get_dr_command;
1314
1315 get_dr_command.port = port->number - port->serial->minor + 1;
1316 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, (__u8 *)&get_dr_command, sizeof(get_dr_command));
1317}
1318
1319
1320static int firm_report_tx_done(struct usb_serial_port *port) {
1321 struct whiteheat_simple close_command;
1322
1323 close_command.port = port->number - port->serial->minor + 1;
1324 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, (__u8 *)&close_command, sizeof(close_command));
1325}
1326
1327
1328/*****************************************************************************
1329 * Connect Tech's White Heat utility functions
1330 *****************************************************************************/
1331static int start_command_port(struct usb_serial *serial)
1332{
1333 struct usb_serial_port *command_port;
1334 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 int retval = 0;
1336
1337 command_port = serial->port[COMMAND_PORT];
1338 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001339 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (!command_info->port_running) {
1341 /* Work around HCD bugs */
1342 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1343
1344 command_port->read_urb->dev = serial->dev;
1345 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1346 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001347 err("%s - failed submitting read urb, error %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto exit;
1349 }
1350 }
1351 command_info->port_running++;
1352
1353exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001354 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return retval;
1356}
1357
1358
1359static void stop_command_port(struct usb_serial *serial)
1360{
1361 struct usb_serial_port *command_port;
1362 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 command_port = serial->port[COMMAND_PORT];
1365 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001366 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 command_info->port_running--;
1368 if (!command_info->port_running)
1369 usb_kill_urb(command_port->read_urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001370 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373
1374static int start_port_read(struct usb_serial_port *port)
1375{
1376 struct whiteheat_private *info = usb_get_serial_port_data(port);
1377 struct whiteheat_urb_wrap *wrap;
1378 struct urb *urb;
1379 int retval = 0;
1380 unsigned long flags;
1381 struct list_head *tmp;
1382 struct list_head *tmp2;
1383
1384 spin_lock_irqsave(&info->lock, flags);
1385
1386 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1387 list_del(tmp);
1388 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1389 urb = wrap->urb;
1390 urb->dev = port->serial->dev;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001391 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 retval = usb_submit_urb(urb, GFP_KERNEL);
1393 if (retval) {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001394 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 list_add(tmp, &info->rx_urbs_free);
1396 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1397 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1398 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001399 list_del(tmp);
1400 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001402 spin_lock_irqsave(&info->lock, flags);
1403 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405 break;
1406 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001407 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 list_add(tmp, &info->rx_urbs_submitted);
1409 }
1410
1411 spin_unlock_irqrestore(&info->lock, flags);
1412
1413 return retval;
1414}
1415
1416
1417static struct whiteheat_urb_wrap *urb_to_wrap(struct urb* urb, struct list_head *head)
1418{
1419 struct whiteheat_urb_wrap *wrap;
1420 struct list_head *tmp;
1421
1422 list_for_each(tmp, head) {
1423 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1424 if (wrap->urb == urb)
1425 return wrap;
1426 }
1427
1428 return NULL;
1429}
1430
1431
1432static struct list_head *list_first(struct list_head *head)
1433{
1434 return head->next;
1435}
1436
1437
David Howellsc4028952006-11-22 14:57:56 +00001438static void rx_data_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
David Howellsc4028952006-11-22 14:57:56 +00001440 struct whiteheat_private *info =
1441 container_of(work, struct whiteheat_private, rx_work);
1442 struct usb_serial_port *port = info->port;
Alan Cox95da3102008-07-22 11:09:07 +01001443 struct tty_struct *tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 struct whiteheat_urb_wrap *wrap;
1445 struct urb *urb;
1446 unsigned long flags;
1447 struct list_head *tmp;
1448 struct list_head *tmp2;
1449 int result;
1450 int sent = 0;
1451
1452 spin_lock_irqsave(&info->lock, flags);
1453 if (info->flags & THROTTLED) {
1454 spin_unlock_irqrestore(&info->lock, flags);
1455 return;
1456 }
1457
1458 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1459 list_del(tmp);
1460 spin_unlock_irqrestore(&info->lock, flags);
1461
1462 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1463 urb = wrap->urb;
1464
1465 if (tty && urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -08001466 int len = tty_buffer_request_room(tty, urb->actual_length);
1467 /* This stuff can go away now I suspect */
1468 if (unlikely(len < urb->actual_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 spin_lock_irqsave(&info->lock, flags);
1470 list_add(tmp, &info->rx_urb_q);
1471 spin_unlock_irqrestore(&info->lock, flags);
1472 tty_flip_buffer_push(tty);
1473 schedule_work(&info->rx_work);
1474 return;
1475 }
Alan Cox33f0f882006-01-09 20:54:13 -08001476 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1477 sent += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479
1480 urb->dev = port->serial->dev;
1481 result = usb_submit_urb(urb, GFP_ATOMIC);
1482 if (result) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001483 err("%s - failed resubmitting read urb, error %d", __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 spin_lock_irqsave(&info->lock, flags);
1485 list_add(tmp, &info->rx_urbs_free);
1486 continue;
1487 }
1488
1489 spin_lock_irqsave(&info->lock, flags);
1490 list_add(tmp, &info->rx_urbs_submitted);
1491 }
1492 spin_unlock_irqrestore(&info->lock, flags);
1493
1494 if (sent)
1495 tty_flip_buffer_push(tty);
1496}
1497
1498
1499/*****************************************************************************
1500 * Connect Tech's White Heat module functions
1501 *****************************************************************************/
1502static int __init whiteheat_init (void)
1503{
1504 int retval;
1505 retval = usb_serial_register(&whiteheat_fake_device);
1506 if (retval)
1507 goto failed_fake_register;
1508 retval = usb_serial_register(&whiteheat_device);
1509 if (retval)
1510 goto failed_device_register;
1511 retval = usb_register(&whiteheat_driver);
1512 if (retval)
1513 goto failed_usb_register;
1514 info(DRIVER_DESC " " DRIVER_VERSION);
1515 return 0;
1516failed_usb_register:
1517 usb_serial_deregister(&whiteheat_device);
1518failed_device_register:
1519 usb_serial_deregister(&whiteheat_fake_device);
1520failed_fake_register:
1521 return retval;
1522}
1523
1524
1525static void __exit whiteheat_exit (void)
1526{
1527 usb_deregister (&whiteheat_driver);
1528 usb_serial_deregister (&whiteheat_fake_device);
1529 usb_serial_deregister (&whiteheat_device);
1530}
1531
1532
1533module_init(whiteheat_init);
1534module_exit(whiteheat_exit);
1535
1536MODULE_AUTHOR( DRIVER_AUTHOR );
1537MODULE_DESCRIPTION( DRIVER_DESC );
1538MODULE_LICENSE("GPL");
1539
David Woodhouseec6752f2008-05-31 01:35:29 +03001540MODULE_FIRMWARE("whiteheat.fw");
1541MODULE_FIRMWARE("whiteheat_loader.fw");
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543module_param(urb_pool_size, int, 0);
1544MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1545
1546module_param(debug, bool, S_IRUGO | S_IWUSR);
1547MODULE_PARM_DESC(debug, "Debug enabled or not");