cp2101: coding style
[deliverable/linux.git] / drivers / usb / serial / whiteheat.c
CommitLineData
1da177e4
LT
1/*
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 *
80359a9c
AC
15 * See Documentation/usb/usb-serial.txt for more information on using this
16 * driver
1da177e4
LT
17 *
18 * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com)
19 * Upgrade to full working driver
20 *
21 * (05/30/2001) gkh
80359a9c
AC
22 * switched from using spinlock to a semaphore, which fixes lots of
23 * problems.
1da177e4
LT
24 *
25 * (04/08/2001) gb
26 * Identify version on module load.
80359a9c 27 *
1da177e4 28 * 2001_Mar_19 gkh
80359a9c 29 * Fixed MOD_INC and MOD_DEC logic, the ability to open a port more
1da177e4
LT
30 * than once, and the got the proper usb_device_id table entries so
31 * the driver works again.
32 *
33 * (11/01/2000) Adam J. Richter
34 * usb_device_id table support
80359a9c 35 *
1da177e4
LT
36 * (10/05/2000) gkh
37 * Fixed bug with urb->dev not being set properly, now that the usb
38 * core needs it.
80359a9c 39 *
1da177e4
LT
40 * (10/03/2000) smd
41 * firmware is improved to guard against crap sent to device
42 * firmware now replies CMD_FAILURE on bad things
43 * read_callback fix you provided for private info struct
44 * command_finished now indicates success or fail
45 * setup_port struct now packed to avoid gcc padding
46 * firmware uses 1 based port numbering, driver now handles that
47 *
48 * (09/11/2000) gkh
49 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
50 *
51 * (07/19/2000) gkh
52 * Added module_init and module_exit functions to handle the fact that this
53 * driver is a loadable module now.
54 * Fixed bug with port->minor that was found by Al Borchers
55 *
56 * (07/04/2000) gkh
80359a9c
AC
57 * Added support for port settings. Baud rate can now be changed. Line
58 * signals are not transferred to and from the tty layer yet, but things
59 * seem to be working well now.
1da177e4
LT
60 *
61 * (05/04/2000) gkh
62 * First cut at open and close commands. Data can flow through the ports at
63 * default speeds now.
64 *
65 * (03/26/2000) gkh
66 * Split driver up into device specific pieces.
80359a9c 67 *
1da177e4
LT
68 */
69
1da177e4
LT
70#include <linux/kernel.h>
71#include <linux/errno.h>
72#include <linux/init.h>
73#include <linux/slab.h>
74#include <linux/tty.h>
75#include <linux/tty_driver.h>
76#include <linux/tty_flip.h>
77#include <linux/module.h>
78#include <linux/spinlock.h>
08a2b3b6 79#include <linux/mutex.h>
80359a9c 80#include <linux/uaccess.h>
1da177e4
LT
81#include <asm/termbits.h>
82#include <linux/usb.h>
83#include <linux/serial_reg.h>
84#include <linux/serial.h>
a969888c 85#include <linux/usb/serial.h>
ec6752f5
DW
86#include <linux/firmware.h>
87#include <linux/ihex.h>
1da177e4
LT
88#include "whiteheat.h" /* WhiteHEAT specific commands */
89
90static int debug;
91
92#ifndef CMSPAR
93#define CMSPAR 0
94#endif
95
96/*
97 * Version Information
98 */
99#define DRIVER_VERSION "v2.0"
100#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
101#define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
102
103#define CONNECT_TECH_VENDOR_ID 0x0710
104#define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
105#define CONNECT_TECH_WHITE_HEAT_ID 0x8001
106
107/*
108 ID tables for whiteheat are unusual, because we want to different
109 things for different versions of the device. Eventually, this
110 will be doable from a single table. But, for now, we define two
111 separate ID tables, and then a third table that combines them
112 just for the purpose of exporting the autoloading information.
113*/
114static struct usb_device_id id_table_std [] = {
115 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
116 { } /* Terminating entry */
117};
118
119static struct usb_device_id id_table_prerenumeration [] = {
120 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
121 { } /* Terminating entry */
122};
123
124static struct usb_device_id id_table_combined [] = {
125 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
126 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
127 { } /* Terminating entry */
128};
129
80359a9c 130MODULE_DEVICE_TABLE(usb, id_table_combined);
1da177e4
LT
131
132static struct usb_driver whiteheat_driver = {
1da177e4
LT
133 .name = "whiteheat",
134 .probe = usb_serial_probe,
135 .disconnect = usb_serial_disconnect,
136 .id_table = id_table_combined,
ba9dc657 137 .no_dynamic_id = 1,
1da177e4
LT
138};
139
140/* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
80359a9c
AC
141static int whiteheat_firmware_download(struct usb_serial *serial,
142 const struct usb_device_id *id);
143static int whiteheat_firmware_attach(struct usb_serial *serial);
1da177e4
LT
144
145/* function prototypes for the Connect Tech WhiteHEAT serial converter */
80359a9c
AC
146static int whiteheat_attach(struct usb_serial *serial);
147static void whiteheat_shutdown(struct usb_serial *serial);
148static int whiteheat_open(struct tty_struct *tty,
149 struct usb_serial_port *port, struct file *filp);
150static void whiteheat_close(struct tty_struct *tty,
151 struct usb_serial_port *port, struct file *filp);
152static int whiteheat_write(struct tty_struct *tty,
153 struct usb_serial_port *port,
154 const unsigned char *buf, int count);
155static int whiteheat_write_room(struct tty_struct *tty);
156static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
157 unsigned int cmd, unsigned long arg);
158static void whiteheat_set_termios(struct tty_struct *tty,
159 struct usb_serial_port *port, struct ktermios *old);
160static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file);
161static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
162 unsigned int set, unsigned int clear);
163static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
164static int whiteheat_chars_in_buffer(struct tty_struct *tty);
165static void whiteheat_throttle(struct tty_struct *tty);
166static void whiteheat_unthrottle(struct tty_struct *tty);
167static void whiteheat_read_callback(struct urb *urb);
168static void whiteheat_write_callback(struct urb *urb);
1da177e4 169
ea65370d 170static struct usb_serial_driver whiteheat_fake_device = {
18fcac35
GKH
171 .driver = {
172 .owner = THIS_MODULE,
269bda1c 173 .name = "whiteheatnofirm",
18fcac35 174 },
269bda1c 175 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
d9b1b787 176 .usb_driver = &whiteheat_driver,
1da177e4 177 .id_table = id_table_prerenumeration,
1da177e4
LT
178 .num_ports = 1,
179 .probe = whiteheat_firmware_download,
180 .attach = whiteheat_firmware_attach,
181};
182
ea65370d 183static struct usb_serial_driver whiteheat_device = {
18fcac35
GKH
184 .driver = {
185 .owner = THIS_MODULE,
269bda1c 186 .name = "whiteheat",
18fcac35 187 },
269bda1c 188 .description = "Connect Tech - WhiteHEAT",
d9b1b787 189 .usb_driver = &whiteheat_driver,
1da177e4 190 .id_table = id_table_std,
1da177e4
LT
191 .num_ports = 4,
192 .attach = whiteheat_attach,
193 .shutdown = whiteheat_shutdown,
194 .open = whiteheat_open,
195 .close = whiteheat_close,
196 .write = whiteheat_write,
197 .write_room = whiteheat_write_room,
198 .ioctl = whiteheat_ioctl,
199 .set_termios = whiteheat_set_termios,
200 .break_ctl = whiteheat_break_ctl,
201 .tiocmget = whiteheat_tiocmget,
202 .tiocmset = whiteheat_tiocmset,
203 .chars_in_buffer = whiteheat_chars_in_buffer,
204 .throttle = whiteheat_throttle,
205 .unthrottle = whiteheat_unthrottle,
206 .read_bulk_callback = whiteheat_read_callback,
207 .write_bulk_callback = whiteheat_write_callback,
208};
209
210
211struct whiteheat_command_private {
08a2b3b6 212 struct mutex mutex;
1da177e4
LT
213 __u8 port_running;
214 __u8 command_finished;
80359a9c
AC
215 wait_queue_head_t wait_command; /* for handling sleeping whilst
216 waiting for a command to
217 finish */
1da177e4
LT
218 __u8 result_buffer[64];
219};
220
221
222#define THROTTLED 0x01
223#define ACTUALLY_THROTTLED 0x02
224
225static int urb_pool_size = 8;
226
227struct whiteheat_urb_wrap {
228 struct list_head list;
229 struct urb *urb;
230};
231
232struct whiteheat_private {
233 spinlock_t lock;
234 __u8 flags;
a5b6f60c 235 __u8 mcr; /* FIXME: no locking on mcr */
1da177e4
LT
236 struct list_head rx_urbs_free;
237 struct list_head rx_urbs_submitted;
238 struct list_head rx_urb_q;
239 struct work_struct rx_work;
c4028958 240 struct usb_serial_port *port;
1da177e4
LT
241 struct list_head tx_urbs_free;
242 struct list_head tx_urbs_submitted;
08a2b3b6 243 struct mutex deathwarrant;
1da177e4
LT
244};
245
246
247/* local function prototypes */
248static int start_command_port(struct usb_serial *serial);
249static void stop_command_port(struct usb_serial *serial);
7d12e780
DH
250static void command_port_write_callback(struct urb *urb);
251static void command_port_read_callback(struct urb *urb);
1da177e4
LT
252
253static int start_port_read(struct usb_serial_port *port);
80359a9c
AC
254static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
255 struct list_head *head);
1da177e4 256static struct list_head *list_first(struct list_head *head);
c4028958 257static void rx_data_softint(struct work_struct *work);
1da177e4 258
80359a9c
AC
259static int firm_send_command(struct usb_serial_port *port, __u8 command,
260 __u8 *data, __u8 datasize);
1da177e4
LT
261static int firm_open(struct usb_serial_port *port);
262static int firm_close(struct usb_serial_port *port);
95da310e 263static int firm_setup_port(struct tty_struct *tty);
1da177e4
LT
264static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
265static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
266static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
267static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
268static int firm_get_dtr_rts(struct usb_serial_port *port);
269static int firm_report_tx_done(struct usb_serial_port *port);
270
271
272#define COMMAND_PORT 4
273#define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
274#define COMMAND_TIMEOUT_MS 2000
275#define CLOSING_DELAY (30 * HZ)
276
277
278/*****************************************************************************
279 * Connect Tech's White Heat prerenumeration driver functions
280 *****************************************************************************/
281
282/* steps to download the firmware to the WhiteHEAT device:
283 - hold the reset (by writing to the reset bit of the CPUCS register)
284 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
285 - release the reset (by writing to the CPUCS register)
286 - download the WH.HEX file for all addresses greater than 0x1b3f using
287 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
288 - hold the reset
289 - download the WH.HEX file for all addresses less than 0x1b40 using
290 VENDOR_REQUEST_ANCHOR_LOAD
291 - release the reset
292 - device renumerated itself and comes up as new device id with all
293 firmware download completed.
294*/
80359a9c
AC
295static int whiteheat_firmware_download(struct usb_serial *serial,
296 const struct usb_device_id *id)
1da177e4 297{
ec6752f5
DW
298 int response, ret = -ENOENT;
299 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
300 const struct ihex_binrec *record;
301
441b62c1 302 dbg("%s", __func__);
ec6752f5
DW
303
304 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
305 &serial->dev->dev)) {
306 err("%s - request \"whiteheat.fw\" failed", __func__);
307 goto out;
308 }
309 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
310 &serial->dev->dev)) {
311 err("%s - request \"whiteheat_loader.fw\" failed", __func__);
312 goto out;
313 }
314 ret = 0;
1da177e4
LT
315 response = ezusb_set_reset (serial, 1);
316
ec6752f5
DW
317 record = (const struct ihex_binrec *)loader_fw->data;
318 while (record) {
319 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
320 (unsigned char *)record->data,
321 be16_to_cpu(record->len), 0xa0);
1da177e4
LT
322 if (response < 0) {
323 err("%s - ezusb_writememory failed for loader (%d %04X %p %d)",
ec6752f5
DW
324 __func__, response, be32_to_cpu(record->addr),
325 record->data, be16_to_cpu(record->len));
1da177e4
LT
326 break;
327 }
ec6752f5 328 record = ihex_next_binrec(record);
1da177e4
LT
329 }
330
80359a9c 331 response = ezusb_set_reset(serial, 0);
1da177e4 332
ec6752f5
DW
333 record = (const struct ihex_binrec *)firmware_fw->data;
334 while (record && be32_to_cpu(record->addr) < 0x1b40)
335 record = ihex_next_binrec(record);
336 while (record) {
337 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
338 (unsigned char *)record->data,
339 be16_to_cpu(record->len), 0xa3);
1da177e4
LT
340 if (response < 0) {
341 err("%s - ezusb_writememory failed for first firmware step (%d %04X %p %d)",
ec6752f5
DW
342 __func__, response, be32_to_cpu(record->addr),
343 record->data, be16_to_cpu(record->len));
1da177e4
LT
344 break;
345 }
346 ++record;
347 }
80359a9c
AC
348
349 response = ezusb_set_reset(serial, 1);
1da177e4 350
ec6752f5
DW
351 record = (const struct ihex_binrec *)firmware_fw->data;
352 while (record && be32_to_cpu(record->addr) < 0x1b40) {
353 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
354 (unsigned char *)record->data,
355 be16_to_cpu(record->len), 0xa0);
1da177e4
LT
356 if (response < 0) {
357 err("%s - ezusb_writememory failed for second firmware step (%d %04X %p %d)",
ec6752f5
DW
358 __func__, response, be32_to_cpu(record->addr),
359 record->data, be16_to_cpu(record->len));
1da177e4
LT
360 break;
361 }
362 ++record;
363 }
ec6752f5 364 ret = 0;
1da177e4 365 response = ezusb_set_reset (serial, 0);
ec6752f5
DW
366 out:
367 release_firmware(loader_fw);
368 release_firmware(firmware_fw);
369 return ret;
1da177e4
LT
370}
371
372
80359a9c 373static int whiteheat_firmware_attach(struct usb_serial *serial)
1da177e4
LT
374{
375 /* We want this device to fail to have a driver assigned to it */
376 return 1;
377}
378
379
380/*****************************************************************************
381 * Connect Tech's White Heat serial driver functions
382 *****************************************************************************/
80359a9c 383static int whiteheat_attach(struct usb_serial *serial)
1da177e4
LT
384{
385 struct usb_serial_port *command_port;
386 struct whiteheat_command_private *command_info;
387 struct usb_serial_port *port;
388 struct whiteheat_private *info;
389 struct whiteheat_hw_info *hw_info;
390 int pipe;
391 int ret;
392 int alen;
393 __u8 *command;
394 __u8 *result;
395 int i;
396 int j;
397 struct urb *urb;
398 int buf_size;
399 struct whiteheat_urb_wrap *wrap;
400 struct list_head *tmp;
401
402 command_port = serial->port[COMMAND_PORT];
403
80359a9c
AC
404 pipe = usb_sndbulkpipe(serial->dev,
405 command_port->bulk_out_endpointAddress);
1da177e4
LT
406 command = kmalloc(2, GFP_KERNEL);
407 if (!command)
408 goto no_command_buffer;
409 command[0] = WHITEHEAT_GET_HW_INFO;
410 command[1] = 0;
80359a9c 411
1da177e4
LT
412 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
413 if (!result)
414 goto no_result_buffer;
415 /*
416 * When the module is reloaded the firmware is still there and
417 * the endpoints are still in the usb core unchanged. This is the
80359a9c
AC
418 * unlinking bug in disguise. Same for the call below.
419 */
1da177e4 420 usb_clear_halt(serial->dev, pipe);
80359a9c
AC
421 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
422 &alen, COMMAND_TIMEOUT_MS);
1da177e4 423 if (ret) {
80359a9c
AC
424 err("%s: Couldn't send command [%d]",
425 serial->type->description, ret);
1da177e4 426 goto no_firmware;
09fd6bc8 427 } else if (alen != 2) {
80359a9c
AC
428 err("%s: Send command incomplete [%d]",
429 serial->type->description, alen);
1da177e4
LT
430 goto no_firmware;
431 }
432
80359a9c
AC
433 pipe = usb_rcvbulkpipe(serial->dev,
434 command_port->bulk_in_endpointAddress);
1da177e4
LT
435 /* See the comment on the usb_clear_halt() above */
436 usb_clear_halt(serial->dev, pipe);
80359a9c
AC
437 ret = usb_bulk_msg(serial->dev, pipe, result,
438 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
1da177e4 439 if (ret) {
80359a9c
AC
440 err("%s: Couldn't get results [%d]",
441 serial->type->description, ret);
1da177e4 442 goto no_firmware;
09fd6bc8 443 } else if (alen != sizeof(*hw_info) + 1) {
80359a9c
AC
444 err("%s: Get results incomplete [%d]",
445 serial->type->description, alen);
1da177e4
LT
446 goto no_firmware;
447 } else if (result[0] != command[0]) {
80359a9c
AC
448 err("%s: Command failed [%d]",
449 serial->type->description, result[0]);
1da177e4
LT
450 goto no_firmware;
451 }
452
453 hw_info = (struct whiteheat_hw_info *)&result[1];
454
269bda1c 455 info("%s: Driver %s: Firmware v%d.%02d", serial->type->description,
1da177e4
LT
456 DRIVER_VERSION, hw_info->sw_major_rev, hw_info->sw_minor_rev);
457
458 for (i = 0; i < serial->num_ports; i++) {
459 port = serial->port[i];
460
5cbded58 461 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
1da177e4 462 if (info == NULL) {
80359a9c
AC
463 err("%s: Out of memory for port structures\n",
464 serial->type->description);
1da177e4
LT
465 goto no_private;
466 }
467
468 spin_lock_init(&info->lock);
08a2b3b6 469 mutex_init(&info->deathwarrant);
1da177e4
LT
470 info->flags = 0;
471 info->mcr = 0;
c4028958
DH
472 INIT_WORK(&info->rx_work, rx_data_softint);
473 info->port = port;
1da177e4
LT
474
475 INIT_LIST_HEAD(&info->rx_urbs_free);
476 INIT_LIST_HEAD(&info->rx_urbs_submitted);
477 INIT_LIST_HEAD(&info->rx_urb_q);
478 INIT_LIST_HEAD(&info->tx_urbs_free);
479 INIT_LIST_HEAD(&info->tx_urbs_submitted);
480
481 for (j = 0; j < urb_pool_size; j++) {
482 urb = usb_alloc_urb(0, GFP_KERNEL);
483 if (!urb) {
484 err("No free urbs available");
485 goto no_rx_urb;
486 }
487 buf_size = port->read_urb->transfer_buffer_length;
488 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
489 if (!urb->transfer_buffer) {
490 err("Couldn't allocate urb buffer");
491 goto no_rx_buf;
492 }
493 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
494 if (!wrap) {
495 err("Couldn't allocate urb wrapper");
496 goto no_rx_wrap;
497 }
498 usb_fill_bulk_urb(urb, serial->dev,
499 usb_rcvbulkpipe(serial->dev,
500 port->bulk_in_endpointAddress),
501 urb->transfer_buffer, buf_size,
502 whiteheat_read_callback, port);
503 wrap->urb = urb;
504 list_add(&wrap->list, &info->rx_urbs_free);
505
506 urb = usb_alloc_urb(0, GFP_KERNEL);
507 if (!urb) {
508 err("No free urbs available");
509 goto no_tx_urb;
510 }
511 buf_size = port->write_urb->transfer_buffer_length;
512 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
513 if (!urb->transfer_buffer) {
514 err("Couldn't allocate urb buffer");
515 goto no_tx_buf;
516 }
517 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
518 if (!wrap) {
519 err("Couldn't allocate urb wrapper");
520 goto no_tx_wrap;
521 }
522 usb_fill_bulk_urb(urb, serial->dev,
523 usb_sndbulkpipe(serial->dev,
524 port->bulk_out_endpointAddress),
525 urb->transfer_buffer, buf_size,
526 whiteheat_write_callback, port);
527 wrap->urb = urb;
528 list_add(&wrap->list, &info->tx_urbs_free);
529 }
530
531 usb_set_serial_port_data(port, info);
532 }
533
80359a9c
AC
534 command_info = kmalloc(sizeof(struct whiteheat_command_private),
535 GFP_KERNEL);
1da177e4 536 if (command_info == NULL) {
80359a9c
AC
537 err("%s: Out of memory for port structures\n",
538 serial->type->description);
1da177e4
LT
539 goto no_command_private;
540 }
541
08a2b3b6 542 mutex_init(&command_info->mutex);
1da177e4
LT
543 command_info->port_running = 0;
544 init_waitqueue_head(&command_info->wait_command);
545 usb_set_serial_port_data(command_port, command_info);
546 command_port->write_urb->complete = command_port_write_callback;
547 command_port->read_urb->complete = command_port_read_callback;
548 kfree(result);
549 kfree(command);
550
551 return 0;
552
553no_firmware:
554 /* Firmware likely not running */
80359a9c
AC
555 err("%s: Unable to retrieve firmware version, try replugging\n",
556 serial->type->description);
557 err("%s: If the firmware is not running (status led not blinking)\n",
558 serial->type->description);
559 err("%s: please contact support@connecttech.com\n",
560 serial->type->description);
67ca0284 561 kfree(result);
1da177e4
LT
562 return -ENODEV;
563
564no_command_private:
565 for (i = serial->num_ports - 1; i >= 0; i--) {
566 port = serial->port[i];
567 info = usb_get_serial_port_data(port);
568 for (j = urb_pool_size - 1; j >= 0; j--) {
569 tmp = list_first(&info->tx_urbs_free);
570 list_del(tmp);
571 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
572 urb = wrap->urb;
573 kfree(wrap);
574no_tx_wrap:
575 kfree(urb->transfer_buffer);
576no_tx_buf:
577 usb_free_urb(urb);
578no_tx_urb:
579 tmp = list_first(&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);
584no_rx_wrap:
585 kfree(urb->transfer_buffer);
586no_rx_buf:
587 usb_free_urb(urb);
588no_rx_urb:
589 ;
590 }
591 kfree(info);
592no_private:
593 ;
594 }
595 kfree(result);
596no_result_buffer:
597 kfree(command);
598no_command_buffer:
599 return -ENOMEM;
600}
601
602
80359a9c 603static void whiteheat_shutdown(struct usb_serial *serial)
1da177e4
LT
604{
605 struct usb_serial_port *command_port;
606 struct usb_serial_port *port;
607 struct whiteheat_private *info;
608 struct whiteheat_urb_wrap *wrap;
609 struct urb *urb;
610 struct list_head *tmp;
611 struct list_head *tmp2;
612 int i;
613
441b62c1 614 dbg("%s", __func__);
1da177e4
LT
615
616 /* free up our private data for our command port */
617 command_port = serial->port[COMMAND_PORT];
80359a9c 618 kfree(usb_get_serial_port_data(command_port));
1da177e4
LT
619
620 for (i = 0; i < serial->num_ports; i++) {
621 port = serial->port[i];
622 info = usb_get_serial_port_data(port);
623 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
624 list_del(tmp);
625 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
626 urb = wrap->urb;
627 kfree(wrap);
628 kfree(urb->transfer_buffer);
629 usb_free_urb(urb);
630 }
631 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
632 list_del(tmp);
633 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
634 urb = wrap->urb;
635 kfree(wrap);
636 kfree(urb->transfer_buffer);
637 usb_free_urb(urb);
638 }
639 kfree(info);
640 }
641
642 return;
643}
644
80359a9c 645static int whiteheat_open(struct tty_struct *tty,
95da310e 646 struct usb_serial_port *port, struct file *filp)
1da177e4
LT
647{
648 int retval = 0;
1da177e4 649
441b62c1 650 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
651
652 retval = start_command_port(port->serial);
653 if (retval)
654 goto exit;
655
95da310e
AC
656 if (tty)
657 tty->low_latency = 1;
1da177e4
LT
658
659 /* send an open port command */
660 retval = firm_open(port);
661 if (retval) {
662 stop_command_port(port->serial);
663 goto exit;
664 }
665
666 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
667 if (retval) {
668 firm_close(port);
669 stop_command_port(port->serial);
670 goto exit;
671 }
672
72e27412
AC
673 if (tty)
674 firm_setup_port(tty);
1da177e4
LT
675
676 /* Work around HCD bugs */
677 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
678 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
679
680 /* Start reading from the device */
681 retval = start_port_read(port);
682 if (retval) {
80359a9c
AC
683 err("%s - failed submitting read urb, error %d",
684 __func__, retval);
1da177e4
LT
685 firm_close(port);
686 stop_command_port(port->serial);
687 goto exit;
688 }
689
690exit:
441b62c1 691 dbg("%s - exit, retval = %d", __func__, retval);
1da177e4
LT
692 return retval;
693}
694
695
95da310e 696static void whiteheat_close(struct tty_struct *tty,
80359a9c 697 struct usb_serial_port *port, struct file *filp)
1da177e4
LT
698{
699 struct whiteheat_private *info = usb_get_serial_port_data(port);
700 struct whiteheat_urb_wrap *wrap;
701 struct urb *urb;
702 struct list_head *tmp;
703 struct list_head *tmp2;
1da177e4 704
441b62c1 705 dbg("%s - port %d", __func__, port->number);
e33fe4d8
ON
706
707 mutex_lock(&port->serial->disc_mutex);
1da177e4 708 /* filp is NULL when called from usb_serial_disconnect */
e33fe4d8
ON
709 if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
710 mutex_unlock(&port->serial->disc_mutex);
1da177e4
LT
711 return;
712 }
e33fe4d8 713 mutex_unlock(&port->serial->disc_mutex);
1da177e4 714
95da310e 715 tty->closing = 1;
1da177e4
LT
716
717/*
718 * Not currently in use; tty_wait_until_sent() calls
719 * serial_chars_in_buffer() which deadlocks on the second semaphore
720 * acquisition. This should be fixed at some point. Greg's been
721 * notified.
722 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
95da310e 723 tty_wait_until_sent(tty, CLOSING_DELAY);
1da177e4
LT
724 }
725*/
726
95da310e
AC
727 tty_driver_flush_buffer(tty);
728 tty_ldisc_flush(tty);
1da177e4
LT
729
730 firm_report_tx_done(port);
731
732 firm_close(port);
733
734 /* shutdown our bulk reads and writes */
08a2b3b6
ON
735 mutex_lock(&info->deathwarrant);
736 spin_lock_irq(&info->lock);
1da177e4
LT
737 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
738 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
739 urb = wrap->urb;
08a2b3b6
ON
740 list_del(tmp);
741 spin_unlock_irq(&info->lock);
1da177e4 742 usb_kill_urb(urb);
08a2b3b6
ON
743 spin_lock_irq(&info->lock);
744 list_add(tmp, &info->rx_urbs_free);
1da177e4 745 }
179e0917
AM
746 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
747 list_move(tmp, &info->rx_urbs_free);
1da177e4
LT
748 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
749 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
750 urb = wrap->urb;
08a2b3b6
ON
751 list_del(tmp);
752 spin_unlock_irq(&info->lock);
1da177e4 753 usb_kill_urb(urb);
08a2b3b6
ON
754 spin_lock_irq(&info->lock);
755 list_add(tmp, &info->tx_urbs_free);
1da177e4 756 }
08a2b3b6
ON
757 spin_unlock_irq(&info->lock);
758 mutex_unlock(&info->deathwarrant);
1da177e4
LT
759
760 stop_command_port(port->serial);
761
95da310e 762 tty->closing = 0;
1da177e4
LT
763}
764
765
95da310e
AC
766static int whiteheat_write(struct tty_struct *tty,
767 struct usb_serial_port *port, const unsigned char *buf, int count)
1da177e4
LT
768{
769 struct usb_serial *serial = port->serial;
770 struct whiteheat_private *info = usb_get_serial_port_data(port);
771 struct whiteheat_urb_wrap *wrap;
772 struct urb *urb;
773 int result;
774 int bytes;
775 int sent = 0;
776 unsigned long flags;
777 struct list_head *tmp;
778
441b62c1 779 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
780
781 if (count == 0) {
441b62c1 782 dbg("%s - write request of 0 bytes", __func__);
1da177e4
LT
783 return (0);
784 }
785
786 while (count) {
787 spin_lock_irqsave(&info->lock, flags);
788 if (list_empty(&info->tx_urbs_free)) {
789 spin_unlock_irqrestore(&info->lock, flags);
790 break;
791 }
792 tmp = list_first(&info->tx_urbs_free);
793 list_del(tmp);
794 spin_unlock_irqrestore(&info->lock, flags);
795
796 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
797 urb = wrap->urb;
80359a9c
AC
798 bytes = (count > port->bulk_out_size) ?
799 port->bulk_out_size : count;
800 memcpy(urb->transfer_buffer, buf + sent, bytes);
1da177e4 801
80359a9c
AC
802 usb_serial_debug_data(debug, &port->dev,
803 __func__, bytes, urb->transfer_buffer);
1da177e4
LT
804
805 urb->dev = serial->dev;
806 urb->transfer_buffer_length = bytes;
807 result = usb_submit_urb(urb, GFP_ATOMIC);
808 if (result) {
80359a9c
AC
809 err("%s - failed submitting write urb, error %d",
810 __func__, result);
1da177e4
LT
811 sent = result;
812 spin_lock_irqsave(&info->lock, flags);
813 list_add(tmp, &info->tx_urbs_free);
814 spin_unlock_irqrestore(&info->lock, flags);
815 break;
816 } else {
817 sent += bytes;
818 count -= bytes;
819 spin_lock_irqsave(&info->lock, flags);
820 list_add(tmp, &info->tx_urbs_submitted);
821 spin_unlock_irqrestore(&info->lock, flags);
822 }
823 }
824
825 return sent;
826}
827
95da310e 828static int whiteheat_write_room(struct tty_struct *tty)
1da177e4 829{
95da310e 830 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
831 struct whiteheat_private *info = usb_get_serial_port_data(port);
832 struct list_head *tmp;
833 int room = 0;
834 unsigned long flags;
835
441b62c1 836 dbg("%s - port %d", __func__, port->number);
80359a9c 837
1da177e4
LT
838 spin_lock_irqsave(&info->lock, flags);
839 list_for_each(tmp, &info->tx_urbs_free)
840 room++;
841 spin_unlock_irqrestore(&info->lock, flags);
842 room *= port->bulk_out_size;
843
441b62c1 844 dbg("%s - returns %d", __func__, room);
1da177e4
LT
845 return (room);
846}
847
80359a9c 848static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file)
1da177e4 849{
95da310e 850 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
851 struct whiteheat_private *info = usb_get_serial_port_data(port);
852 unsigned int modem_signals = 0;
853
441b62c1 854 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
855
856 firm_get_dtr_rts(port);
857 if (info->mcr & UART_MCR_DTR)
858 modem_signals |= TIOCM_DTR;
859 if (info->mcr & UART_MCR_RTS)
860 modem_signals |= TIOCM_RTS;
861
862 return modem_signals;
863}
864
80359a9c 865static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
1da177e4
LT
866 unsigned int set, unsigned int clear)
867{
95da310e 868 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
869 struct whiteheat_private *info = usb_get_serial_port_data(port);
870
441b62c1 871 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
872
873 if (set & TIOCM_RTS)
874 info->mcr |= UART_MCR_RTS;
875 if (set & TIOCM_DTR)
876 info->mcr |= UART_MCR_DTR;
877
878 if (clear & TIOCM_RTS)
879 info->mcr &= ~UART_MCR_RTS;
880 if (clear & TIOCM_DTR)
881 info->mcr &= ~UART_MCR_DTR;
882
883 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
884 firm_set_rts(port, info->mcr & UART_MCR_RTS);
885 return 0;
886}
887
888
80359a9c
AC
889static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
890 unsigned int cmd, unsigned long arg)
1da177e4 891{
95da310e 892 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
893 struct serial_struct serstruct;
894 void __user *user_arg = (void __user *)arg;
895
441b62c1 896 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
1da177e4
LT
897
898 switch (cmd) {
80359a9c
AC
899 case TIOCGSERIAL:
900 memset(&serstruct, 0, sizeof(serstruct));
901 serstruct.type = PORT_16654;
902 serstruct.line = port->serial->minor;
903 serstruct.port = port->number;
904 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
905 serstruct.xmit_fifo_size = port->bulk_out_size;
906 serstruct.custom_divisor = 0;
907 serstruct.baud_base = 460800;
908 serstruct.close_delay = CLOSING_DELAY;
909 serstruct.closing_wait = CLOSING_DELAY;
910
911 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
912 return -EFAULT;
913 break;
914 default:
915 break;
1da177e4
LT
916 }
917
918 return -ENOIOCTLCMD;
919}
920
921
80359a9c
AC
922static void whiteheat_set_termios(struct tty_struct *tty,
923 struct usb_serial_port *port, struct ktermios *old_termios)
1da177e4 924{
95da310e 925 firm_setup_port(tty);
1da177e4
LT
926}
927
80359a9c
AC
928static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
929{
95da310e 930 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
931 firm_set_break(port, break_state);
932}
933
934
95da310e 935static int whiteheat_chars_in_buffer(struct tty_struct *tty)
1da177e4 936{
95da310e 937 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
938 struct whiteheat_private *info = usb_get_serial_port_data(port);
939 struct list_head *tmp;
940 struct whiteheat_urb_wrap *wrap;
941 int chars = 0;
942 unsigned long flags;
943
441b62c1 944 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
945
946 spin_lock_irqsave(&info->lock, flags);
947 list_for_each(tmp, &info->tx_urbs_submitted) {
948 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
949 chars += wrap->urb->transfer_buffer_length;
950 }
951 spin_unlock_irqrestore(&info->lock, flags);
952
80359a9c 953 dbg("%s - returns %d", __func__, chars);
08a2b3b6 954 return chars;
1da177e4
LT
955}
956
957
80359a9c 958static void whiteheat_throttle(struct tty_struct *tty)
1da177e4 959{
95da310e 960 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
961 struct whiteheat_private *info = usb_get_serial_port_data(port);
962 unsigned long flags;
963
441b62c1 964 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
965
966 spin_lock_irqsave(&info->lock, flags);
967 info->flags |= THROTTLED;
968 spin_unlock_irqrestore(&info->lock, flags);
969
970 return;
971}
972
973
80359a9c 974static void whiteheat_unthrottle(struct tty_struct *tty)
1da177e4 975{
95da310e 976 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
977 struct whiteheat_private *info = usb_get_serial_port_data(port);
978 int actually_throttled;
979 unsigned long flags;
980
441b62c1 981 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
982
983 spin_lock_irqsave(&info->lock, flags);
984 actually_throttled = info->flags & ACTUALLY_THROTTLED;
985 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
986 spin_unlock_irqrestore(&info->lock, flags);
987
988 if (actually_throttled)
c4028958 989 rx_data_softint(&info->rx_work);
1da177e4
LT
990
991 return;
992}
993
994
995/*****************************************************************************
996 * Connect Tech's White Heat callback routines
997 *****************************************************************************/
08a2b3b6 998static void command_port_write_callback(struct urb *urb)
1da177e4 999{
05400013
GKH
1000 int status = urb->status;
1001
441b62c1 1002 dbg("%s", __func__);
1da177e4 1003
05400013
GKH
1004 if (status) {
1005 dbg("nonzero urb status: %d", status);
1da177e4
LT
1006 return;
1007 }
1008}
1009
1010
08a2b3b6 1011static void command_port_read_callback(struct urb *urb)
1da177e4 1012{
cdc97792 1013 struct usb_serial_port *command_port = urb->context;
1da177e4 1014 struct whiteheat_command_private *command_info;
05400013 1015 int status = urb->status;
1da177e4
LT
1016 unsigned char *data = urb->transfer_buffer;
1017 int result;
1da177e4 1018
441b62c1 1019 dbg("%s", __func__);
1da177e4 1020
08a2b3b6
ON
1021 command_info = usb_get_serial_port_data(command_port);
1022 if (!command_info) {
80359a9c 1023 dbg("%s - command_info is NULL, exiting.", __func__);
08a2b3b6
ON
1024 return;
1025 }
05400013 1026 if (status) {
441b62c1 1027 dbg("%s - nonzero urb status: %d", __func__, status);
05400013 1028 if (status != -ENOENT)
08a2b3b6
ON
1029 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1030 wake_up(&command_info->wait_command);
1da177e4
LT
1031 return;
1032 }
1033
80359a9c
AC
1034 usb_serial_debug_data(debug, &command_port->dev,
1035 __func__, urb->actual_length, data);
1da177e4 1036
1da177e4
LT
1037 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1038 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
08a2b3b6 1039 wake_up(&command_info->wait_command);
1da177e4
LT
1040 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1041 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
08a2b3b6 1042 wake_up(&command_info->wait_command);
1da177e4 1043 } else if (data[0] == WHITEHEAT_EVENT) {
80359a9c
AC
1044 /* These are unsolicited reports from the firmware, hence no
1045 waiting command to wakeup */
441b62c1 1046 dbg("%s - event received", __func__);
1da177e4 1047 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
80359a9c
AC
1048 memcpy(command_info->result_buffer, &data[1],
1049 urb->actual_length - 1);
1da177e4 1050 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
08a2b3b6 1051 wake_up(&command_info->wait_command);
80359a9c 1052 } else
441b62c1 1053 dbg("%s - bad reply from firmware", __func__);
80359a9c 1054
1da177e4
LT
1055 /* Continue trying to always read */
1056 command_port->read_urb->dev = command_port->serial->dev;
1057 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
1da177e4 1058 if (result)
80359a9c
AC
1059 dbg("%s - failed resubmitting read urb, error %d",
1060 __func__, result);
1da177e4
LT
1061}
1062
1063
7d12e780 1064static void whiteheat_read_callback(struct urb *urb)
1da177e4 1065{
cdc97792 1066 struct usb_serial_port *port = urb->context;
1da177e4
LT
1067 struct whiteheat_urb_wrap *wrap;
1068 unsigned char *data = urb->transfer_buffer;
1069 struct whiteheat_private *info = usb_get_serial_port_data(port);
05400013 1070 int status = urb->status;
1da177e4 1071
441b62c1 1072 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
1073
1074 spin_lock(&info->lock);
1075 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1076 if (!wrap) {
1077 spin_unlock(&info->lock);
441b62c1 1078 err("%s - Not my urb!", __func__);
1da177e4
LT
1079 return;
1080 }
1081 list_del(&wrap->list);
1082 spin_unlock(&info->lock);
1083
05400013
GKH
1084 if (status) {
1085 dbg("%s - nonzero read bulk status received: %d",
441b62c1 1086 __func__, status);
1da177e4
LT
1087 spin_lock(&info->lock);
1088 list_add(&wrap->list, &info->rx_urbs_free);
1089 spin_unlock(&info->lock);
1090 return;
1091 }
1092
80359a9c
AC
1093 usb_serial_debug_data(debug, &port->dev,
1094 __func__, urb->actual_length, data);
1da177e4
LT
1095
1096 spin_lock(&info->lock);
1097 list_add_tail(&wrap->list, &info->rx_urb_q);
1098 if (info->flags & THROTTLED) {
1099 info->flags |= ACTUALLY_THROTTLED;
1100 spin_unlock(&info->lock);
1101 return;
1102 }
1103 spin_unlock(&info->lock);
1104
1105 schedule_work(&info->rx_work);
1106}
1107
1108
7d12e780 1109static void whiteheat_write_callback(struct urb *urb)
1da177e4 1110{
cdc97792 1111 struct usb_serial_port *port = urb->context;
1da177e4
LT
1112 struct whiteheat_private *info = usb_get_serial_port_data(port);
1113 struct whiteheat_urb_wrap *wrap;
05400013 1114 int status = urb->status;
1da177e4 1115
441b62c1 1116 dbg("%s - port %d", __func__, port->number);
1da177e4
LT
1117
1118 spin_lock(&info->lock);
1119 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1120 if (!wrap) {
1121 spin_unlock(&info->lock);
441b62c1 1122 err("%s - Not my urb!", __func__);
1da177e4
LT
1123 return;
1124 }
179e0917 1125 list_move(&wrap->list, &info->tx_urbs_free);
1da177e4
LT
1126 spin_unlock(&info->lock);
1127
05400013
GKH
1128 if (status) {
1129 dbg("%s - nonzero write bulk status received: %d",
441b62c1 1130 __func__, status);
1da177e4
LT
1131 return;
1132 }
1133
cf2c7481 1134 usb_serial_port_softint(port);
1da177e4
LT
1135}
1136
1137
1138/*****************************************************************************
1139 * Connect Tech's White Heat firmware interface
1140 *****************************************************************************/
80359a9c
AC
1141static int firm_send_command(struct usb_serial_port *port, __u8 command,
1142 __u8 *data, __u8 datasize)
1da177e4
LT
1143{
1144 struct usb_serial_port *command_port;
1145 struct whiteheat_command_private *command_info;
1146 struct whiteheat_private *info;
1147 __u8 *transfer_buffer;
1148 int retval = 0;
08a2b3b6 1149 int t;
1da177e4 1150
441b62c1 1151 dbg("%s - command %d", __func__, command);
1da177e4
LT
1152
1153 command_port = port->serial->port[COMMAND_PORT];
1154 command_info = usb_get_serial_port_data(command_port);
08a2b3b6 1155 mutex_lock(&command_info->mutex);
38c3cb5b 1156 command_info->command_finished = false;
80359a9c 1157
1da177e4
LT
1158 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1159 transfer_buffer[0] = command;
80359a9c 1160 memcpy(&transfer_buffer[1], data, datasize);
1da177e4
LT
1161 command_port->write_urb->transfer_buffer_length = datasize + 1;
1162 command_port->write_urb->dev = port->serial->dev;
80359a9c 1163 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1da177e4 1164 if (retval) {
441b62c1 1165 dbg("%s - submit urb failed", __func__);
1da177e4
LT
1166 goto exit;
1167 }
1da177e4
LT
1168
1169 /* wait for the command to complete */
08a2b3b6 1170 t = wait_event_timeout(command_info->wait_command,
38c3cb5b 1171 (bool)command_info->command_finished, COMMAND_TIMEOUT);
08a2b3b6
ON
1172 if (!t)
1173 usb_kill_urb(command_port->write_urb);
1da177e4 1174
38c3cb5b 1175 if (command_info->command_finished == false) {
441b62c1 1176 dbg("%s - command timed out.", __func__);
1da177e4
LT
1177 retval = -ETIMEDOUT;
1178 goto exit;
1179 }
1180
1181 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
441b62c1 1182 dbg("%s - command failed.", __func__);
1da177e4
LT
1183 retval = -EIO;
1184 goto exit;
1185 }
1186
1187 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
441b62c1 1188 dbg("%s - command completed.", __func__);
1da177e4 1189 switch (command) {
80359a9c
AC
1190 case WHITEHEAT_GET_DTR_RTS:
1191 info = usb_get_serial_port_data(port);
1192 memcpy(&info->mcr, command_info->result_buffer,
1193 sizeof(struct whiteheat_dr_info));
1da177e4
LT
1194 break;
1195 }
1196 }
1da177e4 1197exit:
08a2b3b6 1198 mutex_unlock(&command_info->mutex);
1da177e4
LT
1199 return retval;
1200}
1201
1202
80359a9c
AC
1203static int firm_open(struct usb_serial_port *port)
1204{
1da177e4
LT
1205 struct whiteheat_simple open_command;
1206
1207 open_command.port = port->number - port->serial->minor + 1;
80359a9c
AC
1208 return firm_send_command(port, WHITEHEAT_OPEN,
1209 (__u8 *)&open_command, sizeof(open_command));
1da177e4
LT
1210}
1211
1212
80359a9c
AC
1213static int firm_close(struct usb_serial_port *port)
1214{
1da177e4
LT
1215 struct whiteheat_simple close_command;
1216
1217 close_command.port = port->number - port->serial->minor + 1;
80359a9c
AC
1218 return firm_send_command(port, WHITEHEAT_CLOSE,
1219 (__u8 *)&close_command, sizeof(close_command));
1da177e4
LT
1220}
1221
1222
80359a9c
AC
1223static int firm_setup_port(struct tty_struct *tty)
1224{
95da310e 1225 struct usb_serial_port *port = tty->driver_data;
1da177e4 1226 struct whiteheat_port_settings port_settings;
95da310e 1227 unsigned int cflag = tty->termios->c_cflag;
1da177e4
LT
1228
1229 port_settings.port = port->number + 1;
1230
1231 /* get the byte size */
1232 switch (cflag & CSIZE) {
80359a9c
AC
1233 case CS5: port_settings.bits = 5; break;
1234 case CS6: port_settings.bits = 6; break;
1235 case CS7: port_settings.bits = 7; break;
1236 default:
1237 case CS8: port_settings.bits = 8; break;
1da177e4 1238 }
441b62c1 1239 dbg("%s - data bits = %d", __func__, port_settings.bits);
80359a9c 1240
1da177e4
LT
1241 /* determine the parity */
1242 if (cflag & PARENB)
1243 if (cflag & CMSPAR)
1244 if (cflag & PARODD)
1245 port_settings.parity = WHITEHEAT_PAR_MARK;
1246 else
1247 port_settings.parity = WHITEHEAT_PAR_SPACE;
1248 else
1249 if (cflag & PARODD)
1250 port_settings.parity = WHITEHEAT_PAR_ODD;
1251 else
1252 port_settings.parity = WHITEHEAT_PAR_EVEN;
1253 else
1254 port_settings.parity = WHITEHEAT_PAR_NONE;
441b62c1 1255 dbg("%s - parity = %c", __func__, port_settings.parity);
1da177e4
LT
1256
1257 /* figure out the stop bits requested */
1258 if (cflag & CSTOPB)
1259 port_settings.stop = 2;
1260 else
1261 port_settings.stop = 1;
441b62c1 1262 dbg("%s - stop bits = %d", __func__, port_settings.stop);
1da177e4
LT
1263
1264 /* figure out the flow control settings */
1265 if (cflag & CRTSCTS)
80359a9c
AC
1266 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1267 WHITEHEAT_HFLOW_RTS);
1da177e4
LT
1268 else
1269 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
441b62c1 1270 dbg("%s - hardware flow control = %s %s %s %s", __func__,
1da177e4
LT
1271 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1272 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1273 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1274 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
80359a9c 1275
1da177e4 1276 /* determine software flow control */
95da310e 1277 if (I_IXOFF(tty))
1da177e4
LT
1278 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1279 else
1280 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
441b62c1 1281 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
80359a9c 1282
95da310e
AC
1283 port_settings.xon = START_CHAR(tty);
1284 port_settings.xoff = STOP_CHAR(tty);
80359a9c
AC
1285 dbg("%s - XON = %2x, XOFF = %2x",
1286 __func__, port_settings.xon, port_settings.xoff);
1da177e4
LT
1287
1288 /* get the baud rate wanted */
95da310e 1289 port_settings.baud = tty_get_baud_rate(tty);
441b62c1 1290 dbg("%s - baud rate = %d", __func__, port_settings.baud);
1da177e4 1291
01d1df29 1292 /* fixme: should set validated settings */
95da310e 1293 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1da177e4
LT
1294 /* handle any settings that aren't specified in the tty structure */
1295 port_settings.lloop = 0;
80359a9c 1296
1da177e4 1297 /* now send the message to the device */
80359a9c
AC
1298 return firm_send_command(port, WHITEHEAT_SETUP_PORT,
1299 (__u8 *)&port_settings, sizeof(port_settings));
1da177e4
LT
1300}
1301
1302
80359a9c
AC
1303static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1304{
1da177e4
LT
1305 struct whiteheat_set_rdb rts_command;
1306
1307 rts_command.port = port->number - port->serial->minor + 1;
1308 rts_command.state = onoff;
80359a9c
AC
1309 return firm_send_command(port, WHITEHEAT_SET_RTS,
1310 (__u8 *)&rts_command, sizeof(rts_command));
1da177e4
LT
1311}
1312
1313
80359a9c
AC
1314static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1315{
1da177e4
LT
1316 struct whiteheat_set_rdb dtr_command;
1317
1318 dtr_command.port = port->number - port->serial->minor + 1;
1319 dtr_command.state = onoff;
72e27412 1320 return firm_send_command(port, WHITEHEAT_SET_DTR,
80359a9c 1321 (__u8 *)&dtr_command, sizeof(dtr_command));
1da177e4
LT
1322}
1323
1324
80359a9c
AC
1325static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1326{
1da177e4
LT
1327 struct whiteheat_set_rdb break_command;
1328
1329 break_command.port = port->number - port->serial->minor + 1;
1330 break_command.state = onoff;
72e27412 1331 return firm_send_command(port, WHITEHEAT_SET_BREAK,
80359a9c 1332 (__u8 *)&break_command, sizeof(break_command));
1da177e4
LT
1333}
1334
1335
80359a9c
AC
1336static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1337{
1da177e4
LT
1338 struct whiteheat_purge purge_command;
1339
1340 purge_command.port = port->number - port->serial->minor + 1;
1341 purge_command.what = rxtx;
80359a9c
AC
1342 return firm_send_command(port, WHITEHEAT_PURGE,
1343 (__u8 *)&purge_command, sizeof(purge_command));
1da177e4
LT
1344}
1345
1346
80359a9c
AC
1347static int firm_get_dtr_rts(struct usb_serial_port *port)
1348{
1da177e4
LT
1349 struct whiteheat_simple get_dr_command;
1350
1351 get_dr_command.port = port->number - port->serial->minor + 1;
80359a9c
AC
1352 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1353 (__u8 *)&get_dr_command, sizeof(get_dr_command));
1da177e4
LT
1354}
1355
1356
80359a9c
AC
1357static int firm_report_tx_done(struct usb_serial_port *port)
1358{
1da177e4
LT
1359 struct whiteheat_simple close_command;
1360
1361 close_command.port = port->number - port->serial->minor + 1;
80359a9c
AC
1362 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1363 (__u8 *)&close_command, sizeof(close_command));
1da177e4
LT
1364}
1365
1366
1367/*****************************************************************************
1368 * Connect Tech's White Heat utility functions
1369 *****************************************************************************/
1370static int start_command_port(struct usb_serial *serial)
1371{
1372 struct usb_serial_port *command_port;
1373 struct whiteheat_command_private *command_info;
1da177e4 1374 int retval = 0;
80359a9c 1375
1da177e4
LT
1376 command_port = serial->port[COMMAND_PORT];
1377 command_info = usb_get_serial_port_data(command_port);
08a2b3b6 1378 mutex_lock(&command_info->mutex);
1da177e4
LT
1379 if (!command_info->port_running) {
1380 /* Work around HCD bugs */
1381 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1382
1383 command_port->read_urb->dev = serial->dev;
1384 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1385 if (retval) {
80359a9c
AC
1386 err("%s - failed submitting read urb, error %d",
1387 __func__, retval);
1da177e4
LT
1388 goto exit;
1389 }
1390 }
1391 command_info->port_running++;
1392
1393exit:
08a2b3b6 1394 mutex_unlock(&command_info->mutex);
1da177e4
LT
1395 return retval;
1396}
1397
1398
1399static void stop_command_port(struct usb_serial *serial)
1400{
1401 struct usb_serial_port *command_port;
1402 struct whiteheat_command_private *command_info;
1da177e4
LT
1403
1404 command_port = serial->port[COMMAND_PORT];
1405 command_info = usb_get_serial_port_data(command_port);
08a2b3b6 1406 mutex_lock(&command_info->mutex);
1da177e4
LT
1407 command_info->port_running--;
1408 if (!command_info->port_running)
1409 usb_kill_urb(command_port->read_urb);
08a2b3b6 1410 mutex_unlock(&command_info->mutex);
1da177e4
LT
1411}
1412
1413
1414static int start_port_read(struct usb_serial_port *port)
1415{
1416 struct whiteheat_private *info = usb_get_serial_port_data(port);
1417 struct whiteheat_urb_wrap *wrap;
1418 struct urb *urb;
1419 int retval = 0;
1420 unsigned long flags;
1421 struct list_head *tmp;
1422 struct list_head *tmp2;
1423
1424 spin_lock_irqsave(&info->lock, flags);
1425
1426 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1427 list_del(tmp);
1428 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1429 urb = wrap->urb;
1430 urb->dev = port->serial->dev;
08a2b3b6 1431 spin_unlock_irqrestore(&info->lock, flags);
1da177e4
LT
1432 retval = usb_submit_urb(urb, GFP_KERNEL);
1433 if (retval) {
08a2b3b6 1434 spin_lock_irqsave(&info->lock, flags);
1da177e4
LT
1435 list_add(tmp, &info->rx_urbs_free);
1436 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1437 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1438 urb = wrap->urb;
08a2b3b6
ON
1439 list_del(tmp);
1440 spin_unlock_irqrestore(&info->lock, flags);
1da177e4 1441 usb_kill_urb(urb);
08a2b3b6
ON
1442 spin_lock_irqsave(&info->lock, flags);
1443 list_add(tmp, &info->rx_urbs_free);
1da177e4
LT
1444 }
1445 break;
1446 }
08a2b3b6 1447 spin_lock_irqsave(&info->lock, flags);
1da177e4
LT
1448 list_add(tmp, &info->rx_urbs_submitted);
1449 }
1450
1451 spin_unlock_irqrestore(&info->lock, flags);
1452
1453 return retval;
1454}
1455
1456
80359a9c
AC
1457static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1458 struct list_head *head)
1da177e4
LT
1459{
1460 struct whiteheat_urb_wrap *wrap;
1461 struct list_head *tmp;
1462
1463 list_for_each(tmp, head) {
1464 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1465 if (wrap->urb == urb)
1466 return wrap;
1467 }
1468
1469 return NULL;
1470}
1471
1472
1473static struct list_head *list_first(struct list_head *head)
1474{
1475 return head->next;
1476}
1477
1478
c4028958 1479static void rx_data_softint(struct work_struct *work)
1da177e4 1480{
c4028958
DH
1481 struct whiteheat_private *info =
1482 container_of(work, struct whiteheat_private, rx_work);
1483 struct usb_serial_port *port = info->port;
95da310e 1484 struct tty_struct *tty = port->port.tty;
1da177e4
LT
1485 struct whiteheat_urb_wrap *wrap;
1486 struct urb *urb;
1487 unsigned long flags;
1488 struct list_head *tmp;
1489 struct list_head *tmp2;
1490 int result;
1491 int sent = 0;
1492
1493 spin_lock_irqsave(&info->lock, flags);
1494 if (info->flags & THROTTLED) {
1495 spin_unlock_irqrestore(&info->lock, flags);
1496 return;
1497 }
1498
1499 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1500 list_del(tmp);
1501 spin_unlock_irqrestore(&info->lock, flags);
1502
1503 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1504 urb = wrap->urb;
1505
1506 if (tty && urb->actual_length) {
80359a9c
AC
1507 int len = tty_buffer_request_room(tty,
1508 urb->actual_length);
33f0f88f
AC
1509 /* This stuff can go away now I suspect */
1510 if (unlikely(len < urb->actual_length)) {
1da177e4
LT
1511 spin_lock_irqsave(&info->lock, flags);
1512 list_add(tmp, &info->rx_urb_q);
1513 spin_unlock_irqrestore(&info->lock, flags);
1514 tty_flip_buffer_push(tty);
1515 schedule_work(&info->rx_work);
1516 return;
1517 }
33f0f88f
AC
1518 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1519 sent += len;
1da177e4
LT
1520 }
1521
1522 urb->dev = port->serial->dev;
1523 result = usb_submit_urb(urb, GFP_ATOMIC);
1524 if (result) {
80359a9c
AC
1525 err("%s - failed resubmitting read urb, error %d",
1526 __func__, result);
1da177e4
LT
1527 spin_lock_irqsave(&info->lock, flags);
1528 list_add(tmp, &info->rx_urbs_free);
1529 continue;
1530 }
1531
1532 spin_lock_irqsave(&info->lock, flags);
1533 list_add(tmp, &info->rx_urbs_submitted);
1534 }
1535 spin_unlock_irqrestore(&info->lock, flags);
1536
1537 if (sent)
1538 tty_flip_buffer_push(tty);
1539}
1540
1541
1542/*****************************************************************************
1543 * Connect Tech's White Heat module functions
1544 *****************************************************************************/
80359a9c 1545static int __init whiteheat_init(void)
1da177e4
LT
1546{
1547 int retval;
1548 retval = usb_serial_register(&whiteheat_fake_device);
1549 if (retval)
1550 goto failed_fake_register;
1551 retval = usb_serial_register(&whiteheat_device);
1552 if (retval)
1553 goto failed_device_register;
1554 retval = usb_register(&whiteheat_driver);
1555 if (retval)
1556 goto failed_usb_register;
1557 info(DRIVER_DESC " " DRIVER_VERSION);
1558 return 0;
1559failed_usb_register:
1560 usb_serial_deregister(&whiteheat_device);
1561failed_device_register:
1562 usb_serial_deregister(&whiteheat_fake_device);
1563failed_fake_register:
1564 return retval;
1565}
1566
1567
80359a9c 1568static void __exit whiteheat_exit(void)
1da177e4 1569{
80359a9c
AC
1570 usb_deregister(&whiteheat_driver);
1571 usb_serial_deregister(&whiteheat_fake_device);
1572 usb_serial_deregister(&whiteheat_device);
1da177e4
LT
1573}
1574
1575
1576module_init(whiteheat_init);
1577module_exit(whiteheat_exit);
1578
80359a9c
AC
1579MODULE_AUTHOR(DRIVER_AUTHOR);
1580MODULE_DESCRIPTION(DRIVER_DESC);
1da177e4
LT
1581MODULE_LICENSE("GPL");
1582
ec6752f5
DW
1583MODULE_FIRMWARE("whiteheat.fw");
1584MODULE_FIRMWARE("whiteheat_loader.fw");
1585
1da177e4
LT
1586module_param(urb_pool_size, int, 0);
1587MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1588
1589module_param(debug, bool, S_IRUGO | S_IWUSR);
1590MODULE_PARM_DESC(debug, "Debug enabled or not");
This page took 0.871642 seconds and 5 git commands to generate.