blob: f5033d482eeec1060fd57fbb85388503ad405e40 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include "whiteheat_fw.h" /* firmware for the ConnectTech WhiteHEAT device */
85#include "whiteheat.h" /* WhiteHEAT specific commands */
86
87static int debug;
88
89#ifndef CMSPAR
90#define CMSPAR 0
91#endif
92
93/*
94 * Version Information
95 */
96#define DRIVER_VERSION "v2.0"
97#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
98#define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
99
100#define CONNECT_TECH_VENDOR_ID 0x0710
101#define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
102#define CONNECT_TECH_WHITE_HEAT_ID 0x8001
103
104/*
105 ID tables for whiteheat are unusual, because we want to different
106 things for different versions of the device. Eventually, this
107 will be doable from a single table. But, for now, we define two
108 separate ID tables, and then a third table that combines them
109 just for the purpose of exporting the autoloading information.
110*/
111static struct usb_device_id id_table_std [] = {
112 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
113 { } /* Terminating entry */
114};
115
116static struct usb_device_id id_table_prerenumeration [] = {
117 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
118 { } /* Terminating entry */
119};
120
121static struct usb_device_id id_table_combined [] = {
122 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
123 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
124 { } /* Terminating entry */
125};
126
127MODULE_DEVICE_TABLE (usb, id_table_combined);
128
129static struct usb_driver whiteheat_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .name = "whiteheat",
131 .probe = usb_serial_probe,
132 .disconnect = usb_serial_disconnect,
133 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800134 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
137/* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
138static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id);
139static int whiteheat_firmware_attach (struct usb_serial *serial);
140
141/* function prototypes for the Connect Tech WhiteHEAT serial converter */
142static int whiteheat_attach (struct usb_serial *serial);
143static void whiteheat_shutdown (struct usb_serial *serial);
144static int whiteheat_open (struct usb_serial_port *port, struct file *filp);
145static void whiteheat_close (struct usb_serial_port *port, struct file *filp);
146static int whiteheat_write (struct usb_serial_port *port, const unsigned char *buf, int count);
147static int whiteheat_write_room (struct usb_serial_port *port);
148static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
Alan Cox606d0992006-12-08 02:38:45 -0800149static void whiteheat_set_termios (struct usb_serial_port *port, struct ktermios * old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file);
151static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
152static void whiteheat_break_ctl (struct usb_serial_port *port, int break_state);
153static int whiteheat_chars_in_buffer (struct usb_serial_port *port);
154static void whiteheat_throttle (struct usb_serial_port *port);
155static void whiteheat_unthrottle (struct usb_serial_port *port);
David Howells7d12e782006-10-05 14:55:46 +0100156static void whiteheat_read_callback (struct urb *urb);
157static void whiteheat_write_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700159static struct usb_serial_driver whiteheat_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700160 .driver = {
161 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700162 .name = "whiteheatnofirm",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700163 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700164 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100165 .usb_driver = &whiteheat_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 .id_table = id_table_prerenumeration,
167 .num_interrupt_in = NUM_DONT_CARE,
168 .num_bulk_in = NUM_DONT_CARE,
169 .num_bulk_out = NUM_DONT_CARE,
170 .num_ports = 1,
171 .probe = whiteheat_firmware_download,
172 .attach = whiteheat_firmware_attach,
173};
174
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700175static struct usb_serial_driver whiteheat_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700176 .driver = {
177 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700178 .name = "whiteheat",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700179 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700180 .description = "Connect Tech - WhiteHEAT",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100181 .usb_driver = &whiteheat_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .id_table = id_table_std,
183 .num_interrupt_in = NUM_DONT_CARE,
184 .num_bulk_in = NUM_DONT_CARE,
185 .num_bulk_out = NUM_DONT_CARE,
186 .num_ports = 4,
187 .attach = whiteheat_attach,
188 .shutdown = whiteheat_shutdown,
189 .open = whiteheat_open,
190 .close = whiteheat_close,
191 .write = whiteheat_write,
192 .write_room = whiteheat_write_room,
193 .ioctl = whiteheat_ioctl,
194 .set_termios = whiteheat_set_termios,
195 .break_ctl = whiteheat_break_ctl,
196 .tiocmget = whiteheat_tiocmget,
197 .tiocmset = whiteheat_tiocmset,
198 .chars_in_buffer = whiteheat_chars_in_buffer,
199 .throttle = whiteheat_throttle,
200 .unthrottle = whiteheat_unthrottle,
201 .read_bulk_callback = whiteheat_read_callback,
202 .write_bulk_callback = whiteheat_write_callback,
203};
204
205
206struct whiteheat_command_private {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200207 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 __u8 port_running;
209 __u8 command_finished;
210 wait_queue_head_t wait_command; /* for handling sleeping while waiting for a command to finish */
211 __u8 result_buffer[64];
212};
213
214
215#define THROTTLED 0x01
216#define ACTUALLY_THROTTLED 0x02
217
218static int urb_pool_size = 8;
219
220struct whiteheat_urb_wrap {
221 struct list_head list;
222 struct urb *urb;
223};
224
225struct whiteheat_private {
226 spinlock_t lock;
227 __u8 flags;
228 __u8 mcr;
229 struct list_head rx_urbs_free;
230 struct list_head rx_urbs_submitted;
231 struct list_head rx_urb_q;
232 struct work_struct rx_work;
David Howellsc4028952006-11-22 14:57:56 +0000233 struct usb_serial_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 struct list_head tx_urbs_free;
235 struct list_head tx_urbs_submitted;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200236 struct mutex deathwarrant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};
238
239
240/* local function prototypes */
241static int start_command_port(struct usb_serial *serial);
242static void stop_command_port(struct usb_serial *serial);
David Howells7d12e782006-10-05 14:55:46 +0100243static void command_port_write_callback(struct urb *urb);
244static void command_port_read_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246static int start_port_read(struct usb_serial_port *port);
247static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb, struct list_head *head);
248static struct list_head *list_first(struct list_head *head);
David Howellsc4028952006-11-22 14:57:56 +0000249static void rx_data_softint(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize);
252static int firm_open(struct usb_serial_port *port);
253static int firm_close(struct usb_serial_port *port);
254static int firm_setup_port(struct usb_serial_port *port);
255static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
256static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
257static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
258static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
259static int firm_get_dtr_rts(struct usb_serial_port *port);
260static int firm_report_tx_done(struct usb_serial_port *port);
261
262
263#define COMMAND_PORT 4
264#define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
265#define COMMAND_TIMEOUT_MS 2000
266#define CLOSING_DELAY (30 * HZ)
267
268
269/*****************************************************************************
270 * Connect Tech's White Heat prerenumeration driver functions
271 *****************************************************************************/
272
273/* steps to download the firmware to the WhiteHEAT device:
274 - hold the reset (by writing to the reset bit of the CPUCS register)
275 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
276 - release the reset (by writing to the CPUCS register)
277 - download the WH.HEX file for all addresses greater than 0x1b3f using
278 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
279 - hold the reset
280 - download the WH.HEX file for all addresses less than 0x1b40 using
281 VENDOR_REQUEST_ANCHOR_LOAD
282 - release the reset
283 - device renumerated itself and comes up as new device id with all
284 firmware download completed.
285*/
286static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id)
287{
288 int response;
289 const struct whiteheat_hex_record *record;
290
291 dbg("%s", __FUNCTION__);
292
293 response = ezusb_set_reset (serial, 1);
294
295 record = &whiteheat_loader[0];
296 while (record->address != 0xffff) {
297 response = ezusb_writememory (serial, record->address,
298 (unsigned char *)record->data, record->data_size, 0xa0);
299 if (response < 0) {
300 err("%s - ezusb_writememory failed for loader (%d %04X %p %d)",
301 __FUNCTION__, response, record->address, record->data, record->data_size);
302 break;
303 }
304 ++record;
305 }
306
307 response = ezusb_set_reset (serial, 0);
308
309 record = &whiteheat_firmware[0];
310 while (record->address < 0x1b40) {
311 ++record;
312 }
313 while (record->address != 0xffff) {
314 response = ezusb_writememory (serial, record->address,
315 (unsigned char *)record->data, record->data_size, 0xa3);
316 if (response < 0) {
317 err("%s - ezusb_writememory failed for first firmware step (%d %04X %p %d)",
318 __FUNCTION__, response, record->address, record->data, record->data_size);
319 break;
320 }
321 ++record;
322 }
323
324 response = ezusb_set_reset (serial, 1);
325
326 record = &whiteheat_firmware[0];
327 while (record->address < 0x1b40) {
328 response = ezusb_writememory (serial, record->address,
329 (unsigned char *)record->data, record->data_size, 0xa0);
330 if (response < 0) {
331 err("%s - ezusb_writememory failed for second firmware step (%d %04X %p %d)",
332 __FUNCTION__, response, record->address, record->data, record->data_size);
333 break;
334 }
335 ++record;
336 }
337
338 response = ezusb_set_reset (serial, 0);
339
340 return 0;
341}
342
343
344static int whiteheat_firmware_attach (struct usb_serial *serial)
345{
346 /* We want this device to fail to have a driver assigned to it */
347 return 1;
348}
349
350
351/*****************************************************************************
352 * Connect Tech's White Heat serial driver functions
353 *****************************************************************************/
354static int whiteheat_attach (struct usb_serial *serial)
355{
356 struct usb_serial_port *command_port;
357 struct whiteheat_command_private *command_info;
358 struct usb_serial_port *port;
359 struct whiteheat_private *info;
360 struct whiteheat_hw_info *hw_info;
361 int pipe;
362 int ret;
363 int alen;
364 __u8 *command;
365 __u8 *result;
366 int i;
367 int j;
368 struct urb *urb;
369 int buf_size;
370 struct whiteheat_urb_wrap *wrap;
371 struct list_head *tmp;
372
373 command_port = serial->port[COMMAND_PORT];
374
375 pipe = usb_sndbulkpipe (serial->dev, command_port->bulk_out_endpointAddress);
376 command = kmalloc(2, GFP_KERNEL);
377 if (!command)
378 goto no_command_buffer;
379 command[0] = WHITEHEAT_GET_HW_INFO;
380 command[1] = 0;
381
382 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
383 if (!result)
384 goto no_result_buffer;
385 /*
386 * When the module is reloaded the firmware is still there and
387 * the endpoints are still in the usb core unchanged. This is the
388 * unlinking bug in disguise. Same for the call below.
389 */
390 usb_clear_halt(serial->dev, pipe);
391 ret = usb_bulk_msg (serial->dev, pipe, command, 2, &alen, COMMAND_TIMEOUT_MS);
392 if (ret) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700393 err("%s: Couldn't send command [%d]", serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400395 } else if (alen != 2) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700396 err("%s: Send command incomplete [%d]", serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 goto no_firmware;
398 }
399
400 pipe = usb_rcvbulkpipe (serial->dev, command_port->bulk_in_endpointAddress);
401 /* See the comment on the usb_clear_halt() above */
402 usb_clear_halt(serial->dev, pipe);
403 ret = usb_bulk_msg (serial->dev, pipe, result, sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
404 if (ret) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700405 err("%s: Couldn't get results [%d]", serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400407 } else if (alen != sizeof(*hw_info) + 1) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700408 err("%s: Get results incomplete [%d]", serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 goto no_firmware;
410 } else if (result[0] != command[0]) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700411 err("%s: Command failed [%d]", serial->type->description, result[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto no_firmware;
413 }
414
415 hw_info = (struct whiteheat_hw_info *)&result[1];
416
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700417 info("%s: Driver %s: Firmware v%d.%02d", serial->type->description,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 DRIVER_VERSION, hw_info->sw_major_rev, hw_info->sw_minor_rev);
419
420 for (i = 0; i < serial->num_ports; i++) {
421 port = serial->port[i];
422
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800423 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (info == NULL) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700425 err("%s: Out of memory for port structures\n", serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto no_private;
427 }
428
429 spin_lock_init(&info->lock);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200430 mutex_init(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 info->flags = 0;
432 info->mcr = 0;
David Howellsc4028952006-11-22 14:57:56 +0000433 INIT_WORK(&info->rx_work, rx_data_softint);
434 info->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 INIT_LIST_HEAD(&info->rx_urbs_free);
437 INIT_LIST_HEAD(&info->rx_urbs_submitted);
438 INIT_LIST_HEAD(&info->rx_urb_q);
439 INIT_LIST_HEAD(&info->tx_urbs_free);
440 INIT_LIST_HEAD(&info->tx_urbs_submitted);
441
442 for (j = 0; j < urb_pool_size; j++) {
443 urb = usb_alloc_urb(0, GFP_KERNEL);
444 if (!urb) {
445 err("No free urbs available");
446 goto no_rx_urb;
447 }
448 buf_size = port->read_urb->transfer_buffer_length;
449 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
450 if (!urb->transfer_buffer) {
451 err("Couldn't allocate urb buffer");
452 goto no_rx_buf;
453 }
454 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
455 if (!wrap) {
456 err("Couldn't allocate urb wrapper");
457 goto no_rx_wrap;
458 }
459 usb_fill_bulk_urb(urb, serial->dev,
460 usb_rcvbulkpipe(serial->dev,
461 port->bulk_in_endpointAddress),
462 urb->transfer_buffer, buf_size,
463 whiteheat_read_callback, port);
464 wrap->urb = urb;
465 list_add(&wrap->list, &info->rx_urbs_free);
466
467 urb = usb_alloc_urb(0, GFP_KERNEL);
468 if (!urb) {
469 err("No free urbs available");
470 goto no_tx_urb;
471 }
472 buf_size = port->write_urb->transfer_buffer_length;
473 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
474 if (!urb->transfer_buffer) {
475 err("Couldn't allocate urb buffer");
476 goto no_tx_buf;
477 }
478 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
479 if (!wrap) {
480 err("Couldn't allocate urb wrapper");
481 goto no_tx_wrap;
482 }
483 usb_fill_bulk_urb(urb, serial->dev,
484 usb_sndbulkpipe(serial->dev,
485 port->bulk_out_endpointAddress),
486 urb->transfer_buffer, buf_size,
487 whiteheat_write_callback, port);
488 wrap->urb = urb;
489 list_add(&wrap->list, &info->tx_urbs_free);
490 }
491
492 usb_set_serial_port_data(port, info);
493 }
494
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800495 command_info = kmalloc(sizeof(struct whiteheat_command_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (command_info == NULL) {
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700497 err("%s: Out of memory for port structures\n", serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto no_command_private;
499 }
500
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200501 mutex_init(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 command_info->port_running = 0;
503 init_waitqueue_head(&command_info->wait_command);
504 usb_set_serial_port_data(command_port, command_info);
505 command_port->write_urb->complete = command_port_write_callback;
506 command_port->read_urb->complete = command_port_read_callback;
507 kfree(result);
508 kfree(command);
509
510 return 0;
511
512no_firmware:
513 /* Firmware likely not running */
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700514 err("%s: Unable to retrieve firmware version, try replugging\n", serial->type->description);
515 err("%s: If the firmware is not running (status led not blinking)\n", serial->type->description);
516 err("%s: please contact support@connecttech.com\n", serial->type->description);
Jesper Juhl67ca0282006-04-23 19:59:23 +0200517 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -ENODEV;
519
520no_command_private:
521 for (i = serial->num_ports - 1; i >= 0; i--) {
522 port = serial->port[i];
523 info = usb_get_serial_port_data(port);
524 for (j = urb_pool_size - 1; j >= 0; j--) {
525 tmp = list_first(&info->tx_urbs_free);
526 list_del(tmp);
527 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
528 urb = wrap->urb;
529 kfree(wrap);
530no_tx_wrap:
531 kfree(urb->transfer_buffer);
532no_tx_buf:
533 usb_free_urb(urb);
534no_tx_urb:
535 tmp = list_first(&info->rx_urbs_free);
536 list_del(tmp);
537 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
538 urb = wrap->urb;
539 kfree(wrap);
540no_rx_wrap:
541 kfree(urb->transfer_buffer);
542no_rx_buf:
543 usb_free_urb(urb);
544no_rx_urb:
545 ;
546 }
547 kfree(info);
548no_private:
549 ;
550 }
551 kfree(result);
552no_result_buffer:
553 kfree(command);
554no_command_buffer:
555 return -ENOMEM;
556}
557
558
559static void whiteheat_shutdown (struct usb_serial *serial)
560{
561 struct usb_serial_port *command_port;
562 struct usb_serial_port *port;
563 struct whiteheat_private *info;
564 struct whiteheat_urb_wrap *wrap;
565 struct urb *urb;
566 struct list_head *tmp;
567 struct list_head *tmp2;
568 int i;
569
570 dbg("%s", __FUNCTION__);
571
572 /* free up our private data for our command port */
573 command_port = serial->port[COMMAND_PORT];
574 kfree (usb_get_serial_port_data(command_port));
575
576 for (i = 0; i < serial->num_ports; i++) {
577 port = serial->port[i];
578 info = usb_get_serial_port_data(port);
579 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
580 list_del(tmp);
581 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
582 urb = wrap->urb;
583 kfree(wrap);
584 kfree(urb->transfer_buffer);
585 usb_free_urb(urb);
586 }
587 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
588 list_del(tmp);
589 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
590 urb = wrap->urb;
591 kfree(wrap);
592 kfree(urb->transfer_buffer);
593 usb_free_urb(urb);
594 }
595 kfree(info);
596 }
597
598 return;
599}
600
601
602static int whiteheat_open (struct usb_serial_port *port, struct file *filp)
603{
604 int retval = 0;
Alan Cox606d0992006-12-08 02:38:45 -0800605 struct ktermios old_term;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 dbg("%s - port %d", __FUNCTION__, port->number);
608
609 retval = start_command_port(port->serial);
610 if (retval)
611 goto exit;
612
Oliver Neukume58679802007-12-20 12:23:03 +0100613 port->tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /* send an open port command */
616 retval = firm_open(port);
617 if (retval) {
618 stop_command_port(port->serial);
619 goto exit;
620 }
621
622 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
623 if (retval) {
624 firm_close(port);
625 stop_command_port(port->serial);
626 goto exit;
627 }
628
629 old_term.c_cflag = ~port->tty->termios->c_cflag;
630 old_term.c_iflag = ~port->tty->termios->c_iflag;
631 whiteheat_set_termios(port, &old_term);
632
633 /* Work around HCD bugs */
634 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
635 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
636
637 /* Start reading from the device */
638 retval = start_port_read(port);
639 if (retval) {
640 err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
641 firm_close(port);
642 stop_command_port(port->serial);
643 goto exit;
644 }
645
646exit:
647 dbg("%s - exit, retval = %d", __FUNCTION__, retval);
648 return retval;
649}
650
651
652static void whiteheat_close(struct usb_serial_port *port, struct file * filp)
653{
654 struct whiteheat_private *info = usb_get_serial_port_data(port);
655 struct whiteheat_urb_wrap *wrap;
656 struct urb *urb;
657 struct list_head *tmp;
658 struct list_head *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 dbg("%s - port %d", __FUNCTION__, port->number);
661
662 /* filp is NULL when called from usb_serial_disconnect */
663 if (filp && (tty_hung_up_p(filp))) {
664 return;
665 }
666
667 port->tty->closing = 1;
668
669/*
670 * Not currently in use; tty_wait_until_sent() calls
671 * serial_chars_in_buffer() which deadlocks on the second semaphore
672 * acquisition. This should be fixed at some point. Greg's been
673 * notified.
674 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
675 tty_wait_until_sent(port->tty, CLOSING_DELAY);
676 }
677*/
678
679 if (port->tty->driver->flush_buffer)
680 port->tty->driver->flush_buffer(port->tty);
681 tty_ldisc_flush(port->tty);
682
683 firm_report_tx_done(port);
684
685 firm_close(port);
686
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200687printk(KERN_ERR"Before processing rx_urbs_submitted.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* shutdown our bulk reads and writes */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200689 mutex_lock(&info->deathwarrant);
690 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
692 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
693 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200694 list_del(tmp);
695 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200697 spin_lock_irq(&info->lock);
698 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Akinobu Mita179e0912006-06-26 00:24:41 -0700700 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
701 list_move(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
703 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
704 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200705 list_del(tmp);
706 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200708 spin_lock_irq(&info->lock);
709 list_add(tmp, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200711 spin_unlock_irq(&info->lock);
712 mutex_unlock(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 stop_command_port(port->serial);
715
716 port->tty->closing = 0;
717}
718
719
720static int whiteheat_write(struct usb_serial_port *port, const unsigned char *buf, int count)
721{
722 struct usb_serial *serial = port->serial;
723 struct whiteheat_private *info = usb_get_serial_port_data(port);
724 struct whiteheat_urb_wrap *wrap;
725 struct urb *urb;
726 int result;
727 int bytes;
728 int sent = 0;
729 unsigned long flags;
730 struct list_head *tmp;
731
732 dbg("%s - port %d", __FUNCTION__, port->number);
733
734 if (count == 0) {
735 dbg("%s - write request of 0 bytes", __FUNCTION__);
736 return (0);
737 }
738
739 while (count) {
740 spin_lock_irqsave(&info->lock, flags);
741 if (list_empty(&info->tx_urbs_free)) {
742 spin_unlock_irqrestore(&info->lock, flags);
743 break;
744 }
745 tmp = list_first(&info->tx_urbs_free);
746 list_del(tmp);
747 spin_unlock_irqrestore(&info->lock, flags);
748
749 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
750 urb = wrap->urb;
751 bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count;
752 memcpy (urb->transfer_buffer, buf + sent, bytes);
753
754 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, bytes, urb->transfer_buffer);
755
756 urb->dev = serial->dev;
757 urb->transfer_buffer_length = bytes;
758 result = usb_submit_urb(urb, GFP_ATOMIC);
759 if (result) {
760 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
761 sent = result;
762 spin_lock_irqsave(&info->lock, flags);
763 list_add(tmp, &info->tx_urbs_free);
764 spin_unlock_irqrestore(&info->lock, flags);
765 break;
766 } else {
767 sent += bytes;
768 count -= bytes;
769 spin_lock_irqsave(&info->lock, flags);
770 list_add(tmp, &info->tx_urbs_submitted);
771 spin_unlock_irqrestore(&info->lock, flags);
772 }
773 }
774
775 return sent;
776}
777
778
779static int whiteheat_write_room(struct usb_serial_port *port)
780{
781 struct whiteheat_private *info = usb_get_serial_port_data(port);
782 struct list_head *tmp;
783 int room = 0;
784 unsigned long flags;
785
786 dbg("%s - port %d", __FUNCTION__, port->number);
787
788 spin_lock_irqsave(&info->lock, flags);
789 list_for_each(tmp, &info->tx_urbs_free)
790 room++;
791 spin_unlock_irqrestore(&info->lock, flags);
792 room *= port->bulk_out_size;
793
794 dbg("%s - returns %d", __FUNCTION__, room);
795 return (room);
796}
797
798
799static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file)
800{
801 struct whiteheat_private *info = usb_get_serial_port_data(port);
802 unsigned int modem_signals = 0;
803
804 dbg("%s - port %d", __FUNCTION__, port->number);
805
806 firm_get_dtr_rts(port);
807 if (info->mcr & UART_MCR_DTR)
808 modem_signals |= TIOCM_DTR;
809 if (info->mcr & UART_MCR_RTS)
810 modem_signals |= TIOCM_RTS;
811
812 return modem_signals;
813}
814
815
816static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file,
817 unsigned int set, unsigned int clear)
818{
819 struct whiteheat_private *info = usb_get_serial_port_data(port);
820
821 dbg("%s - port %d", __FUNCTION__, port->number);
822
823 if (set & TIOCM_RTS)
824 info->mcr |= UART_MCR_RTS;
825 if (set & TIOCM_DTR)
826 info->mcr |= UART_MCR_DTR;
827
828 if (clear & TIOCM_RTS)
829 info->mcr &= ~UART_MCR_RTS;
830 if (clear & TIOCM_DTR)
831 info->mcr &= ~UART_MCR_DTR;
832
833 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
834 firm_set_rts(port, info->mcr & UART_MCR_RTS);
835 return 0;
836}
837
838
839static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
840{
841 struct serial_struct serstruct;
842 void __user *user_arg = (void __user *)arg;
843
844 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
845
846 switch (cmd) {
847 case TIOCGSERIAL:
848 memset(&serstruct, 0, sizeof(serstruct));
849 serstruct.type = PORT_16654;
850 serstruct.line = port->serial->minor;
851 serstruct.port = port->number;
852 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
853 serstruct.xmit_fifo_size = port->bulk_out_size;
854 serstruct.custom_divisor = 0;
855 serstruct.baud_base = 460800;
856 serstruct.close_delay = CLOSING_DELAY;
857 serstruct.closing_wait = CLOSING_DELAY;
858
859 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
860 return -EFAULT;
861
862 break;
863
864 case TIOCSSERIAL:
865 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
866 return -EFAULT;
867
868 /*
869 * For now this is ignored. dip sets the ASYNC_[V]HI flags
870 * but this isn't used by us at all. Maybe someone somewhere
871 * will need the custom_divisor setting.
872 */
873
874 break;
875
876 default:
877 break;
878 }
879
880 return -ENOIOCTLCMD;
881}
882
883
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200884static void whiteheat_set_termios(struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 dbg("%s -port %d", __FUNCTION__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 firm_setup_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
890
891static void whiteheat_break_ctl(struct usb_serial_port *port, int break_state) {
892 firm_set_break(port, break_state);
893}
894
895
896static int whiteheat_chars_in_buffer(struct usb_serial_port *port)
897{
898 struct whiteheat_private *info = usb_get_serial_port_data(port);
899 struct list_head *tmp;
900 struct whiteheat_urb_wrap *wrap;
901 int chars = 0;
902 unsigned long flags;
903
904 dbg("%s - port %d", __FUNCTION__, port->number);
905
906 spin_lock_irqsave(&info->lock, flags);
907 list_for_each(tmp, &info->tx_urbs_submitted) {
908 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
909 chars += wrap->urb->transfer_buffer_length;
910 }
911 spin_unlock_irqrestore(&info->lock, flags);
912
913 dbg ("%s - returns %d", __FUNCTION__, chars);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200914 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917
918static void whiteheat_throttle (struct usb_serial_port *port)
919{
920 struct whiteheat_private *info = usb_get_serial_port_data(port);
921 unsigned long flags;
922
923 dbg("%s - port %d", __FUNCTION__, port->number);
924
925 spin_lock_irqsave(&info->lock, flags);
926 info->flags |= THROTTLED;
927 spin_unlock_irqrestore(&info->lock, flags);
928
929 return;
930}
931
932
933static void whiteheat_unthrottle (struct usb_serial_port *port)
934{
935 struct whiteheat_private *info = usb_get_serial_port_data(port);
936 int actually_throttled;
937 unsigned long flags;
938
939 dbg("%s - port %d", __FUNCTION__, port->number);
940
941 spin_lock_irqsave(&info->lock, flags);
942 actually_throttled = info->flags & ACTUALLY_THROTTLED;
943 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
944 spin_unlock_irqrestore(&info->lock, flags);
945
946 if (actually_throttled)
David Howellsc4028952006-11-22 14:57:56 +0000947 rx_data_softint(&info->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 return;
950}
951
952
953/*****************************************************************************
954 * Connect Tech's White Heat callback routines
955 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200956static void command_port_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700958 int status = urb->status;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 dbg("%s", __FUNCTION__);
961
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700962 if (status) {
963 dbg("nonzero urb status: %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return;
965 }
966}
967
968
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200969static void command_port_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 struct usb_serial_port *command_port = (struct usb_serial_port *)urb->context;
972 struct whiteheat_command_private *command_info;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700973 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned char *data = urb->transfer_buffer;
975 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 dbg("%s", __FUNCTION__);
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 command_info = usb_get_serial_port_data(command_port);
980 if (!command_info) {
981 dbg ("%s - command_info is NULL, exiting.", __FUNCTION__);
982 return;
983 }
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700984 if (status) {
985 dbg("%s - nonzero urb status: %d", __FUNCTION__, status);
986 if (status != -ENOENT)
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200987 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
988 wake_up(&command_info->wait_command);
989 return;
990 }
991
992 usb_serial_debug_data(debug, &command_port->dev, __FUNCTION__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
995 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200996 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
998 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200999 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 } else if (data[0] == WHITEHEAT_EVENT) {
1001 /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */
1002 dbg("%s - event received", __FUNCTION__);
1003 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
1004 memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1);
1005 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001006 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 } else {
1008 dbg("%s - bad reply from firmware", __FUNCTION__);
1009 }
1010
1011 /* Continue trying to always read */
1012 command_port->read_urb->dev = command_port->serial->dev;
1013 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (result)
1015 dbg("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1016}
1017
1018
David Howells7d12e782006-10-05 14:55:46 +01001019static void whiteheat_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1022 struct whiteheat_urb_wrap *wrap;
1023 unsigned char *data = urb->transfer_buffer;
1024 struct whiteheat_private *info = usb_get_serial_port_data(port);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001025 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 dbg("%s - port %d", __FUNCTION__, port->number);
1028
1029 spin_lock(&info->lock);
1030 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1031 if (!wrap) {
1032 spin_unlock(&info->lock);
1033 err("%s - Not my urb!", __FUNCTION__);
1034 return;
1035 }
1036 list_del(&wrap->list);
1037 spin_unlock(&info->lock);
1038
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001039 if (status) {
1040 dbg("%s - nonzero read bulk status received: %d",
1041 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 spin_lock(&info->lock);
1043 list_add(&wrap->list, &info->rx_urbs_free);
1044 spin_unlock(&info->lock);
1045 return;
1046 }
1047
1048 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
1049
1050 spin_lock(&info->lock);
1051 list_add_tail(&wrap->list, &info->rx_urb_q);
1052 if (info->flags & THROTTLED) {
1053 info->flags |= ACTUALLY_THROTTLED;
1054 spin_unlock(&info->lock);
1055 return;
1056 }
1057 spin_unlock(&info->lock);
1058
1059 schedule_work(&info->rx_work);
1060}
1061
1062
David Howells7d12e782006-10-05 14:55:46 +01001063static void whiteheat_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1066 struct whiteheat_private *info = usb_get_serial_port_data(port);
1067 struct whiteheat_urb_wrap *wrap;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001068 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 dbg("%s - port %d", __FUNCTION__, port->number);
1071
1072 spin_lock(&info->lock);
1073 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1074 if (!wrap) {
1075 spin_unlock(&info->lock);
1076 err("%s - Not my urb!", __FUNCTION__);
1077 return;
1078 }
Akinobu Mita179e0912006-06-26 00:24:41 -07001079 list_move(&wrap->list, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 spin_unlock(&info->lock);
1081
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001082 if (status) {
1083 dbg("%s - nonzero write bulk status received: %d",
1084 __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return;
1086 }
1087
Pete Zaitcevcf2c7482006-05-22 21:58:49 -07001088 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091
1092/*****************************************************************************
1093 * Connect Tech's White Heat firmware interface
1094 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001095static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct usb_serial_port *command_port;
1098 struct whiteheat_command_private *command_info;
1099 struct whiteheat_private *info;
1100 __u8 *transfer_buffer;
1101 int retval = 0;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001102 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 dbg("%s - command %d", __FUNCTION__, command);
1105
1106 command_port = port->serial->port[COMMAND_PORT];
1107 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001108 mutex_lock(&command_info->mutex);
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001109 command_info->command_finished = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1112 transfer_buffer[0] = command;
1113 memcpy (&transfer_buffer[1], data, datasize);
1114 command_port->write_urb->transfer_buffer_length = datasize + 1;
1115 command_port->write_urb->dev = port->serial->dev;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001116 retval = usb_submit_urb (command_port->write_urb, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (retval) {
1118 dbg("%s - submit urb failed", __FUNCTION__);
1119 goto exit;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /* wait for the command to complete */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001123 t = wait_event_timeout(command_info->wait_command,
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001124 (bool)command_info->command_finished, COMMAND_TIMEOUT);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001125 if (!t)
1126 usb_kill_urb(command_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001128 if (command_info->command_finished == false) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 dbg("%s - command timed out.", __FUNCTION__);
1130 retval = -ETIMEDOUT;
1131 goto exit;
1132 }
1133
1134 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1135 dbg("%s - command failed.", __FUNCTION__);
1136 retval = -EIO;
1137 goto exit;
1138 }
1139
1140 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1141 dbg("%s - command completed.", __FUNCTION__);
1142 switch (command) {
1143 case WHITEHEAT_GET_DTR_RTS:
1144 info = usb_get_serial_port_data(port);
1145 memcpy(&info->mcr, command_info->result_buffer, sizeof(struct whiteheat_dr_info));
1146 break;
1147 }
1148 }
1149
1150exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001151 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return retval;
1153}
1154
1155
1156static int firm_open(struct usb_serial_port *port) {
1157 struct whiteheat_simple open_command;
1158
1159 open_command.port = port->number - port->serial->minor + 1;
1160 return firm_send_command(port, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command));
1161}
1162
1163
1164static int firm_close(struct usb_serial_port *port) {
1165 struct whiteheat_simple close_command;
1166
1167 close_command.port = port->number - port->serial->minor + 1;
1168 return firm_send_command(port, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command));
1169}
1170
1171
1172static int firm_setup_port(struct usb_serial_port *port) {
1173 struct whiteheat_port_settings port_settings;
1174 unsigned int cflag = port->tty->termios->c_cflag;
1175
1176 port_settings.port = port->number + 1;
1177
1178 /* get the byte size */
1179 switch (cflag & CSIZE) {
1180 case CS5: port_settings.bits = 5; break;
1181 case CS6: port_settings.bits = 6; break;
1182 case CS7: port_settings.bits = 7; break;
1183 default:
1184 case CS8: port_settings.bits = 8; break;
1185 }
1186 dbg("%s - data bits = %d", __FUNCTION__, port_settings.bits);
1187
1188 /* determine the parity */
1189 if (cflag & PARENB)
1190 if (cflag & CMSPAR)
1191 if (cflag & PARODD)
1192 port_settings.parity = WHITEHEAT_PAR_MARK;
1193 else
1194 port_settings.parity = WHITEHEAT_PAR_SPACE;
1195 else
1196 if (cflag & PARODD)
1197 port_settings.parity = WHITEHEAT_PAR_ODD;
1198 else
1199 port_settings.parity = WHITEHEAT_PAR_EVEN;
1200 else
1201 port_settings.parity = WHITEHEAT_PAR_NONE;
1202 dbg("%s - parity = %c", __FUNCTION__, port_settings.parity);
1203
1204 /* figure out the stop bits requested */
1205 if (cflag & CSTOPB)
1206 port_settings.stop = 2;
1207 else
1208 port_settings.stop = 1;
1209 dbg("%s - stop bits = %d", __FUNCTION__, port_settings.stop);
1210
1211 /* figure out the flow control settings */
1212 if (cflag & CRTSCTS)
1213 port_settings.hflow = (WHITEHEAT_HFLOW_CTS | WHITEHEAT_HFLOW_RTS);
1214 else
1215 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1216 dbg("%s - hardware flow control = %s %s %s %s", __FUNCTION__,
1217 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1218 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1219 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1220 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1221
1222 /* determine software flow control */
1223 if (I_IXOFF(port->tty))
1224 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1225 else
1226 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1227 dbg("%s - software flow control = %c", __FUNCTION__, port_settings.sflow);
1228
1229 port_settings.xon = START_CHAR(port->tty);
1230 port_settings.xoff = STOP_CHAR(port->tty);
1231 dbg("%s - XON = %2x, XOFF = %2x", __FUNCTION__, port_settings.xon, port_settings.xoff);
1232
1233 /* get the baud rate wanted */
1234 port_settings.baud = tty_get_baud_rate(port->tty);
1235 dbg("%s - baud rate = %d", __FUNCTION__, port_settings.baud);
1236
Alan Cox01d1df22007-10-18 01:24:23 -07001237 /* fixme: should set validated settings */
1238 tty_encode_baud_rate(port->tty, port_settings.baud, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /* handle any settings that aren't specified in the tty structure */
1240 port_settings.lloop = 0;
1241
1242 /* now send the message to the device */
1243 return firm_send_command(port, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings));
1244}
1245
1246
1247static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) {
1248 struct whiteheat_set_rdb rts_command;
1249
1250 rts_command.port = port->number - port->serial->minor + 1;
1251 rts_command.state = onoff;
1252 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&rts_command, sizeof(rts_command));
1253}
1254
1255
1256static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) {
1257 struct whiteheat_set_rdb dtr_command;
1258
1259 dtr_command.port = port->number - port->serial->minor + 1;
1260 dtr_command.state = onoff;
1261 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&dtr_command, sizeof(dtr_command));
1262}
1263
1264
1265static int firm_set_break(struct usb_serial_port *port, __u8 onoff) {
1266 struct whiteheat_set_rdb break_command;
1267
1268 break_command.port = port->number - port->serial->minor + 1;
1269 break_command.state = onoff;
1270 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&break_command, sizeof(break_command));
1271}
1272
1273
1274static int firm_purge(struct usb_serial_port *port, __u8 rxtx) {
1275 struct whiteheat_purge purge_command;
1276
1277 purge_command.port = port->number - port->serial->minor + 1;
1278 purge_command.what = rxtx;
1279 return firm_send_command(port, WHITEHEAT_PURGE, (__u8 *)&purge_command, sizeof(purge_command));
1280}
1281
1282
1283static int firm_get_dtr_rts(struct usb_serial_port *port) {
1284 struct whiteheat_simple get_dr_command;
1285
1286 get_dr_command.port = port->number - port->serial->minor + 1;
1287 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, (__u8 *)&get_dr_command, sizeof(get_dr_command));
1288}
1289
1290
1291static int firm_report_tx_done(struct usb_serial_port *port) {
1292 struct whiteheat_simple close_command;
1293
1294 close_command.port = port->number - port->serial->minor + 1;
1295 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, (__u8 *)&close_command, sizeof(close_command));
1296}
1297
1298
1299/*****************************************************************************
1300 * Connect Tech's White Heat utility functions
1301 *****************************************************************************/
1302static int start_command_port(struct usb_serial *serial)
1303{
1304 struct usb_serial_port *command_port;
1305 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 int retval = 0;
1307
1308 command_port = serial->port[COMMAND_PORT];
1309 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001310 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (!command_info->port_running) {
1312 /* Work around HCD bugs */
1313 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1314
1315 command_port->read_urb->dev = serial->dev;
1316 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1317 if (retval) {
1318 err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
1319 goto exit;
1320 }
1321 }
1322 command_info->port_running++;
1323
1324exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001325 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return retval;
1327}
1328
1329
1330static void stop_command_port(struct usb_serial *serial)
1331{
1332 struct usb_serial_port *command_port;
1333 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 command_port = serial->port[COMMAND_PORT];
1336 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001337 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 command_info->port_running--;
1339 if (!command_info->port_running)
1340 usb_kill_urb(command_port->read_urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001341 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
1344
1345static int start_port_read(struct usb_serial_port *port)
1346{
1347 struct whiteheat_private *info = usb_get_serial_port_data(port);
1348 struct whiteheat_urb_wrap *wrap;
1349 struct urb *urb;
1350 int retval = 0;
1351 unsigned long flags;
1352 struct list_head *tmp;
1353 struct list_head *tmp2;
1354
1355 spin_lock_irqsave(&info->lock, flags);
1356
1357 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1358 list_del(tmp);
1359 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1360 urb = wrap->urb;
1361 urb->dev = port->serial->dev;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001362 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 retval = usb_submit_urb(urb, GFP_KERNEL);
1364 if (retval) {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001365 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 list_add(tmp, &info->rx_urbs_free);
1367 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1368 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1369 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001370 list_del(tmp);
1371 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001373 spin_lock_irqsave(&info->lock, flags);
1374 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376 break;
1377 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001378 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 list_add(tmp, &info->rx_urbs_submitted);
1380 }
1381
1382 spin_unlock_irqrestore(&info->lock, flags);
1383
1384 return retval;
1385}
1386
1387
1388static struct whiteheat_urb_wrap *urb_to_wrap(struct urb* urb, struct list_head *head)
1389{
1390 struct whiteheat_urb_wrap *wrap;
1391 struct list_head *tmp;
1392
1393 list_for_each(tmp, head) {
1394 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1395 if (wrap->urb == urb)
1396 return wrap;
1397 }
1398
1399 return NULL;
1400}
1401
1402
1403static struct list_head *list_first(struct list_head *head)
1404{
1405 return head->next;
1406}
1407
1408
David Howellsc4028952006-11-22 14:57:56 +00001409static void rx_data_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
David Howellsc4028952006-11-22 14:57:56 +00001411 struct whiteheat_private *info =
1412 container_of(work, struct whiteheat_private, rx_work);
1413 struct usb_serial_port *port = info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 struct tty_struct *tty = port->tty;
1415 struct whiteheat_urb_wrap *wrap;
1416 struct urb *urb;
1417 unsigned long flags;
1418 struct list_head *tmp;
1419 struct list_head *tmp2;
1420 int result;
1421 int sent = 0;
1422
1423 spin_lock_irqsave(&info->lock, flags);
1424 if (info->flags & THROTTLED) {
1425 spin_unlock_irqrestore(&info->lock, flags);
1426 return;
1427 }
1428
1429 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1430 list_del(tmp);
1431 spin_unlock_irqrestore(&info->lock, flags);
1432
1433 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1434 urb = wrap->urb;
1435
1436 if (tty && urb->actual_length) {
Alan Cox33f0f882006-01-09 20:54:13 -08001437 int len = tty_buffer_request_room(tty, urb->actual_length);
1438 /* This stuff can go away now I suspect */
1439 if (unlikely(len < urb->actual_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 spin_lock_irqsave(&info->lock, flags);
1441 list_add(tmp, &info->rx_urb_q);
1442 spin_unlock_irqrestore(&info->lock, flags);
1443 tty_flip_buffer_push(tty);
1444 schedule_work(&info->rx_work);
1445 return;
1446 }
Alan Cox33f0f882006-01-09 20:54:13 -08001447 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1448 sent += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
1451 urb->dev = port->serial->dev;
1452 result = usb_submit_urb(urb, GFP_ATOMIC);
1453 if (result) {
1454 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1455 spin_lock_irqsave(&info->lock, flags);
1456 list_add(tmp, &info->rx_urbs_free);
1457 continue;
1458 }
1459
1460 spin_lock_irqsave(&info->lock, flags);
1461 list_add(tmp, &info->rx_urbs_submitted);
1462 }
1463 spin_unlock_irqrestore(&info->lock, flags);
1464
1465 if (sent)
1466 tty_flip_buffer_push(tty);
1467}
1468
1469
1470/*****************************************************************************
1471 * Connect Tech's White Heat module functions
1472 *****************************************************************************/
1473static int __init whiteheat_init (void)
1474{
1475 int retval;
1476 retval = usb_serial_register(&whiteheat_fake_device);
1477 if (retval)
1478 goto failed_fake_register;
1479 retval = usb_serial_register(&whiteheat_device);
1480 if (retval)
1481 goto failed_device_register;
1482 retval = usb_register(&whiteheat_driver);
1483 if (retval)
1484 goto failed_usb_register;
1485 info(DRIVER_DESC " " DRIVER_VERSION);
1486 return 0;
1487failed_usb_register:
1488 usb_serial_deregister(&whiteheat_device);
1489failed_device_register:
1490 usb_serial_deregister(&whiteheat_fake_device);
1491failed_fake_register:
1492 return retval;
1493}
1494
1495
1496static void __exit whiteheat_exit (void)
1497{
1498 usb_deregister (&whiteheat_driver);
1499 usb_serial_deregister (&whiteheat_fake_device);
1500 usb_serial_deregister (&whiteheat_device);
1501}
1502
1503
1504module_init(whiteheat_init);
1505module_exit(whiteheat_exit);
1506
1507MODULE_AUTHOR( DRIVER_AUTHOR );
1508MODULE_DESCRIPTION( DRIVER_DESC );
1509MODULE_LICENSE("GPL");
1510
1511module_param(urb_pool_size, int, 0);
1512MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1513
1514module_param(debug, bool, S_IRUGO | S_IWUSR);
1515MODULE_PARM_DESC(debug, "Debug enabled or not");