kbuild: Fix instrumentation removal breakage on avr32
[wrt350n-kernel.git] / drivers / usb / serial / whiteheat.c
blob38726ef3132b70d83121d8bdaa894b47990f0843
1 /*
2 * USB ConnectTech WhiteHEAT driver
4 * Copyright (C) 2002
5 * Connect Tech Inc.
7 * Copyright (C) 1999 - 2001
8 * Greg Kroah-Hartman (greg@kroah.com)
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.
15 * See Documentation/usb/usb-serial.txt for more information on using this driver
17 * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com)
18 * Upgrade to full working driver
20 * (05/30/2001) gkh
21 * switched from using spinlock to a semaphore, which fixes lots of problems.
23 * (04/08/2001) gb
24 * Identify version on module load.
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.
31 * (11/01/2000) Adam J. Richter
32 * usb_device_id table support
34 * (10/05/2000) gkh
35 * Fixed bug with urb->dev not being set properly, now that the usb
36 * core needs it.
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
46 * (09/11/2000) gkh
47 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
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
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.
59 * (05/04/2000) gkh
60 * First cut at open and close commands. Data can flow through the ports at
61 * default speeds now.
63 * (03/26/2000) gkh
64 * Split driver up into device specific pieces.
68 #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>
77 #include <linux/mutex.h>
78 #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>
83 #include <linux/usb/serial.h>
84 #include "whiteheat_fw.h" /* firmware for the ConnectTech WhiteHEAT device */
85 #include "whiteheat.h" /* WhiteHEAT specific commands */
87 static int debug;
89 #ifndef CMSPAR
90 #define CMSPAR 0
91 #endif
94 * Version Information
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"
100 #define CONNECT_TECH_VENDOR_ID 0x0710
101 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
102 #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
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.
111 static struct usb_device_id id_table_std [] = {
112 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
113 { } /* Terminating entry */
116 static struct usb_device_id id_table_prerenumeration [] = {
117 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
118 { } /* Terminating entry */
121 static 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 */
127 MODULE_DEVICE_TABLE (usb, id_table_combined);
129 static struct usb_driver whiteheat_driver = {
130 .name = "whiteheat",
131 .probe = usb_serial_probe,
132 .disconnect = usb_serial_disconnect,
133 .id_table = id_table_combined,
134 .no_dynamic_id = 1,
137 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
138 static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id);
139 static int whiteheat_firmware_attach (struct usb_serial *serial);
141 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
142 static int whiteheat_attach (struct usb_serial *serial);
143 static void whiteheat_shutdown (struct usb_serial *serial);
144 static int whiteheat_open (struct usb_serial_port *port, struct file *filp);
145 static void whiteheat_close (struct usb_serial_port *port, struct file *filp);
146 static int whiteheat_write (struct usb_serial_port *port, const unsigned char *buf, int count);
147 static int whiteheat_write_room (struct usb_serial_port *port);
148 static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
149 static void whiteheat_set_termios (struct usb_serial_port *port, struct ktermios * old);
150 static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file);
151 static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
152 static void whiteheat_break_ctl (struct usb_serial_port *port, int break_state);
153 static int whiteheat_chars_in_buffer (struct usb_serial_port *port);
154 static void whiteheat_throttle (struct usb_serial_port *port);
155 static void whiteheat_unthrottle (struct usb_serial_port *port);
156 static void whiteheat_read_callback (struct urb *urb);
157 static void whiteheat_write_callback (struct urb *urb);
159 static struct usb_serial_driver whiteheat_fake_device = {
160 .driver = {
161 .owner = THIS_MODULE,
162 .name = "whiteheatnofirm",
164 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
165 .usb_driver = &whiteheat_driver,
166 .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,
175 static struct usb_serial_driver whiteheat_device = {
176 .driver = {
177 .owner = THIS_MODULE,
178 .name = "whiteheat",
180 .description = "Connect Tech - WhiteHEAT",
181 .usb_driver = &whiteheat_driver,
182 .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,
206 struct whiteheat_command_private {
207 struct mutex mutex;
208 __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];
215 #define THROTTLED 0x01
216 #define ACTUALLY_THROTTLED 0x02
218 static int urb_pool_size = 8;
220 struct whiteheat_urb_wrap {
221 struct list_head list;
222 struct urb *urb;
225 struct 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;
233 struct usb_serial_port *port;
234 struct list_head tx_urbs_free;
235 struct list_head tx_urbs_submitted;
236 struct mutex deathwarrant;
240 /* local function prototypes */
241 static int start_command_port(struct usb_serial *serial);
242 static void stop_command_port(struct usb_serial *serial);
243 static void command_port_write_callback(struct urb *urb);
244 static void command_port_read_callback(struct urb *urb);
246 static int start_port_read(struct usb_serial_port *port);
247 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb, struct list_head *head);
248 static struct list_head *list_first(struct list_head *head);
249 static void rx_data_softint(struct work_struct *work);
251 static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize);
252 static int firm_open(struct usb_serial_port *port);
253 static int firm_close(struct usb_serial_port *port);
254 static int firm_setup_port(struct usb_serial_port *port);
255 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
256 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
257 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
258 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
259 static int firm_get_dtr_rts(struct usb_serial_port *port);
260 static int firm_report_tx_done(struct usb_serial_port *port);
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)
269 /*****************************************************************************
270 * Connect Tech's White Heat prerenumeration driver functions
271 *****************************************************************************/
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.
286 static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id)
288 int response;
289 const struct whiteheat_hex_record *record;
291 dbg("%s", __FUNCTION__);
293 response = ezusb_set_reset (serial, 1);
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;
304 ++record;
307 response = ezusb_set_reset (serial, 0);
309 record = &whiteheat_firmware[0];
310 while (record->address < 0x1b40) {
311 ++record;
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;
321 ++record;
324 response = ezusb_set_reset (serial, 1);
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;
335 ++record;
338 response = ezusb_set_reset (serial, 0);
340 return 0;
344 static int whiteheat_firmware_attach (struct usb_serial *serial)
346 /* We want this device to fail to have a driver assigned to it */
347 return 1;
351 /*****************************************************************************
352 * Connect Tech's White Heat serial driver functions
353 *****************************************************************************/
354 static int whiteheat_attach (struct usb_serial *serial)
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;
373 command_port = serial->port[COMMAND_PORT];
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;
382 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
383 if (!result)
384 goto no_result_buffer;
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.
390 usb_clear_halt(serial->dev, pipe);
391 ret = usb_bulk_msg (serial->dev, pipe, command, 2, &alen, COMMAND_TIMEOUT_MS);
392 if (ret) {
393 err("%s: Couldn't send command [%d]", serial->type->description, ret);
394 goto no_firmware;
395 } else if (alen != 2) {
396 err("%s: Send command incomplete [%d]", serial->type->description, alen);
397 goto no_firmware;
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) {
405 err("%s: Couldn't get results [%d]", serial->type->description, ret);
406 goto no_firmware;
407 } else if (alen != sizeof(*hw_info) + 1) {
408 err("%s: Get results incomplete [%d]", serial->type->description, alen);
409 goto no_firmware;
410 } else if (result[0] != command[0]) {
411 err("%s: Command failed [%d]", serial->type->description, result[0]);
412 goto no_firmware;
415 hw_info = (struct whiteheat_hw_info *)&result[1];
417 info("%s: Driver %s: Firmware v%d.%02d", serial->type->description,
418 DRIVER_VERSION, hw_info->sw_major_rev, hw_info->sw_minor_rev);
420 for (i = 0; i < serial->num_ports; i++) {
421 port = serial->port[i];
423 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
424 if (info == NULL) {
425 err("%s: Out of memory for port structures\n", serial->type->description);
426 goto no_private;
429 spin_lock_init(&info->lock);
430 mutex_init(&info->deathwarrant);
431 info->flags = 0;
432 info->mcr = 0;
433 INIT_WORK(&info->rx_work, rx_data_softint);
434 info->port = port;
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);
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;
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;
454 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
455 if (!wrap) {
456 err("Couldn't allocate urb wrapper");
457 goto no_rx_wrap;
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);
467 urb = usb_alloc_urb(0, GFP_KERNEL);
468 if (!urb) {
469 err("No free urbs available");
470 goto no_tx_urb;
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;
478 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
479 if (!wrap) {
480 err("Couldn't allocate urb wrapper");
481 goto no_tx_wrap;
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);
492 usb_set_serial_port_data(port, info);
495 command_info = kmalloc(sizeof(struct whiteheat_command_private), GFP_KERNEL);
496 if (command_info == NULL) {
497 err("%s: Out of memory for port structures\n", serial->type->description);
498 goto no_command_private;
501 mutex_init(&command_info->mutex);
502 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);
510 return 0;
512 no_firmware:
513 /* Firmware likely not running */
514 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);
517 kfree(result);
518 return -ENODEV;
520 no_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);
530 no_tx_wrap:
531 kfree(urb->transfer_buffer);
532 no_tx_buf:
533 usb_free_urb(urb);
534 no_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);
540 no_rx_wrap:
541 kfree(urb->transfer_buffer);
542 no_rx_buf:
543 usb_free_urb(urb);
544 no_rx_urb:
547 kfree(info);
548 no_private:
551 kfree(result);
552 no_result_buffer:
553 kfree(command);
554 no_command_buffer:
555 return -ENOMEM;
559 static void whiteheat_shutdown (struct usb_serial *serial)
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;
570 dbg("%s", __FUNCTION__);
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));
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);
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);
595 kfree(info);
598 return;
602 static int whiteheat_open (struct usb_serial_port *port, struct file *filp)
604 int retval = 0;
605 struct ktermios old_term;
607 dbg("%s - port %d", __FUNCTION__, port->number);
609 retval = start_command_port(port->serial);
610 if (retval)
611 goto exit;
613 port->tty->low_latency = 1;
615 /* send an open port command */
616 retval = firm_open(port);
617 if (retval) {
618 stop_command_port(port->serial);
619 goto exit;
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;
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);
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);
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;
646 exit:
647 dbg("%s - exit, retval = %d", __FUNCTION__, retval);
648 return retval;
652 static void whiteheat_close(struct usb_serial_port *port, struct file * filp)
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;
660 dbg("%s - port %d", __FUNCTION__, port->number);
662 mutex_lock(&port->serial->disc_mutex);
663 /* filp is NULL when called from usb_serial_disconnect */
664 if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
665 mutex_unlock(&port->serial->disc_mutex);
666 return;
668 mutex_unlock(&port->serial->disc_mutex);
670 port->tty->closing = 1;
673 * Not currently in use; tty_wait_until_sent() calls
674 * serial_chars_in_buffer() which deadlocks on the second semaphore
675 * acquisition. This should be fixed at some point. Greg's been
676 * notified.
677 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
678 tty_wait_until_sent(port->tty, CLOSING_DELAY);
682 if (port->tty->driver->flush_buffer)
683 port->tty->driver->flush_buffer(port->tty);
684 tty_ldisc_flush(port->tty);
686 firm_report_tx_done(port);
688 firm_close(port);
690 printk(KERN_ERR"Before processing rx_urbs_submitted.\n");
691 /* shutdown our bulk reads and writes */
692 mutex_lock(&info->deathwarrant);
693 spin_lock_irq(&info->lock);
694 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
695 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
696 urb = wrap->urb;
697 list_del(tmp);
698 spin_unlock_irq(&info->lock);
699 usb_kill_urb(urb);
700 spin_lock_irq(&info->lock);
701 list_add(tmp, &info->rx_urbs_free);
703 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
704 list_move(tmp, &info->rx_urbs_free);
705 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
706 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
707 urb = wrap->urb;
708 list_del(tmp);
709 spin_unlock_irq(&info->lock);
710 usb_kill_urb(urb);
711 spin_lock_irq(&info->lock);
712 list_add(tmp, &info->tx_urbs_free);
714 spin_unlock_irq(&info->lock);
715 mutex_unlock(&info->deathwarrant);
717 stop_command_port(port->serial);
719 port->tty->closing = 0;
723 static int whiteheat_write(struct usb_serial_port *port, const unsigned char *buf, int count)
725 struct usb_serial *serial = port->serial;
726 struct whiteheat_private *info = usb_get_serial_port_data(port);
727 struct whiteheat_urb_wrap *wrap;
728 struct urb *urb;
729 int result;
730 int bytes;
731 int sent = 0;
732 unsigned long flags;
733 struct list_head *tmp;
735 dbg("%s - port %d", __FUNCTION__, port->number);
737 if (count == 0) {
738 dbg("%s - write request of 0 bytes", __FUNCTION__);
739 return (0);
742 while (count) {
743 spin_lock_irqsave(&info->lock, flags);
744 if (list_empty(&info->tx_urbs_free)) {
745 spin_unlock_irqrestore(&info->lock, flags);
746 break;
748 tmp = list_first(&info->tx_urbs_free);
749 list_del(tmp);
750 spin_unlock_irqrestore(&info->lock, flags);
752 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
753 urb = wrap->urb;
754 bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count;
755 memcpy (urb->transfer_buffer, buf + sent, bytes);
757 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, bytes, urb->transfer_buffer);
759 urb->dev = serial->dev;
760 urb->transfer_buffer_length = bytes;
761 result = usb_submit_urb(urb, GFP_ATOMIC);
762 if (result) {
763 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
764 sent = result;
765 spin_lock_irqsave(&info->lock, flags);
766 list_add(tmp, &info->tx_urbs_free);
767 spin_unlock_irqrestore(&info->lock, flags);
768 break;
769 } else {
770 sent += bytes;
771 count -= bytes;
772 spin_lock_irqsave(&info->lock, flags);
773 list_add(tmp, &info->tx_urbs_submitted);
774 spin_unlock_irqrestore(&info->lock, flags);
778 return sent;
782 static int whiteheat_write_room(struct usb_serial_port *port)
784 struct whiteheat_private *info = usb_get_serial_port_data(port);
785 struct list_head *tmp;
786 int room = 0;
787 unsigned long flags;
789 dbg("%s - port %d", __FUNCTION__, port->number);
791 spin_lock_irqsave(&info->lock, flags);
792 list_for_each(tmp, &info->tx_urbs_free)
793 room++;
794 spin_unlock_irqrestore(&info->lock, flags);
795 room *= port->bulk_out_size;
797 dbg("%s - returns %d", __FUNCTION__, room);
798 return (room);
802 static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file)
804 struct whiteheat_private *info = usb_get_serial_port_data(port);
805 unsigned int modem_signals = 0;
807 dbg("%s - port %d", __FUNCTION__, port->number);
809 firm_get_dtr_rts(port);
810 if (info->mcr & UART_MCR_DTR)
811 modem_signals |= TIOCM_DTR;
812 if (info->mcr & UART_MCR_RTS)
813 modem_signals |= TIOCM_RTS;
815 return modem_signals;
819 static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file,
820 unsigned int set, unsigned int clear)
822 struct whiteheat_private *info = usb_get_serial_port_data(port);
824 dbg("%s - port %d", __FUNCTION__, port->number);
826 if (set & TIOCM_RTS)
827 info->mcr |= UART_MCR_RTS;
828 if (set & TIOCM_DTR)
829 info->mcr |= UART_MCR_DTR;
831 if (clear & TIOCM_RTS)
832 info->mcr &= ~UART_MCR_RTS;
833 if (clear & TIOCM_DTR)
834 info->mcr &= ~UART_MCR_DTR;
836 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
837 firm_set_rts(port, info->mcr & UART_MCR_RTS);
838 return 0;
842 static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
844 struct serial_struct serstruct;
845 void __user *user_arg = (void __user *)arg;
847 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
849 switch (cmd) {
850 case TIOCGSERIAL:
851 memset(&serstruct, 0, sizeof(serstruct));
852 serstruct.type = PORT_16654;
853 serstruct.line = port->serial->minor;
854 serstruct.port = port->number;
855 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
856 serstruct.xmit_fifo_size = port->bulk_out_size;
857 serstruct.custom_divisor = 0;
858 serstruct.baud_base = 460800;
859 serstruct.close_delay = CLOSING_DELAY;
860 serstruct.closing_wait = CLOSING_DELAY;
862 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
863 return -EFAULT;
865 break;
867 case TIOCSSERIAL:
868 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
869 return -EFAULT;
872 * For now this is ignored. dip sets the ASYNC_[V]HI flags
873 * but this isn't used by us at all. Maybe someone somewhere
874 * will need the custom_divisor setting.
877 break;
879 default:
880 break;
883 return -ENOIOCTLCMD;
887 static void whiteheat_set_termios(struct usb_serial_port *port, struct ktermios *old_termios)
889 dbg("%s -port %d", __FUNCTION__, port->number);
890 firm_setup_port(port);
894 static void whiteheat_break_ctl(struct usb_serial_port *port, int break_state) {
895 firm_set_break(port, break_state);
899 static int whiteheat_chars_in_buffer(struct usb_serial_port *port)
901 struct whiteheat_private *info = usb_get_serial_port_data(port);
902 struct list_head *tmp;
903 struct whiteheat_urb_wrap *wrap;
904 int chars = 0;
905 unsigned long flags;
907 dbg("%s - port %d", __FUNCTION__, port->number);
909 spin_lock_irqsave(&info->lock, flags);
910 list_for_each(tmp, &info->tx_urbs_submitted) {
911 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
912 chars += wrap->urb->transfer_buffer_length;
914 spin_unlock_irqrestore(&info->lock, flags);
916 dbg ("%s - returns %d", __FUNCTION__, chars);
917 return chars;
921 static void whiteheat_throttle (struct usb_serial_port *port)
923 struct whiteheat_private *info = usb_get_serial_port_data(port);
924 unsigned long flags;
926 dbg("%s - port %d", __FUNCTION__, port->number);
928 spin_lock_irqsave(&info->lock, flags);
929 info->flags |= THROTTLED;
930 spin_unlock_irqrestore(&info->lock, flags);
932 return;
936 static void whiteheat_unthrottle (struct usb_serial_port *port)
938 struct whiteheat_private *info = usb_get_serial_port_data(port);
939 int actually_throttled;
940 unsigned long flags;
942 dbg("%s - port %d", __FUNCTION__, port->number);
944 spin_lock_irqsave(&info->lock, flags);
945 actually_throttled = info->flags & ACTUALLY_THROTTLED;
946 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
947 spin_unlock_irqrestore(&info->lock, flags);
949 if (actually_throttled)
950 rx_data_softint(&info->rx_work);
952 return;
956 /*****************************************************************************
957 * Connect Tech's White Heat callback routines
958 *****************************************************************************/
959 static void command_port_write_callback(struct urb *urb)
961 int status = urb->status;
963 dbg("%s", __FUNCTION__);
965 if (status) {
966 dbg("nonzero urb status: %d", status);
967 return;
972 static void command_port_read_callback(struct urb *urb)
974 struct usb_serial_port *command_port = (struct usb_serial_port *)urb->context;
975 struct whiteheat_command_private *command_info;
976 int status = urb->status;
977 unsigned char *data = urb->transfer_buffer;
978 int result;
980 dbg("%s", __FUNCTION__);
982 command_info = usb_get_serial_port_data(command_port);
983 if (!command_info) {
984 dbg ("%s - command_info is NULL, exiting.", __FUNCTION__);
985 return;
987 if (status) {
988 dbg("%s - nonzero urb status: %d", __FUNCTION__, status);
989 if (status != -ENOENT)
990 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
991 wake_up(&command_info->wait_command);
992 return;
995 usb_serial_debug_data(debug, &command_port->dev, __FUNCTION__, urb->actual_length, data);
997 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
998 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
999 wake_up(&command_info->wait_command);
1000 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1001 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1002 wake_up(&command_info->wait_command);
1003 } else if (data[0] == WHITEHEAT_EVENT) {
1004 /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */
1005 dbg("%s - event received", __FUNCTION__);
1006 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
1007 memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1);
1008 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1009 wake_up(&command_info->wait_command);
1010 } else {
1011 dbg("%s - bad reply from firmware", __FUNCTION__);
1014 /* Continue trying to always read */
1015 command_port->read_urb->dev = command_port->serial->dev;
1016 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
1017 if (result)
1018 dbg("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1022 static void whiteheat_read_callback(struct urb *urb)
1024 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1025 struct whiteheat_urb_wrap *wrap;
1026 unsigned char *data = urb->transfer_buffer;
1027 struct whiteheat_private *info = usb_get_serial_port_data(port);
1028 int status = urb->status;
1030 dbg("%s - port %d", __FUNCTION__, port->number);
1032 spin_lock(&info->lock);
1033 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1034 if (!wrap) {
1035 spin_unlock(&info->lock);
1036 err("%s - Not my urb!", __FUNCTION__);
1037 return;
1039 list_del(&wrap->list);
1040 spin_unlock(&info->lock);
1042 if (status) {
1043 dbg("%s - nonzero read bulk status received: %d",
1044 __FUNCTION__, status);
1045 spin_lock(&info->lock);
1046 list_add(&wrap->list, &info->rx_urbs_free);
1047 spin_unlock(&info->lock);
1048 return;
1051 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
1053 spin_lock(&info->lock);
1054 list_add_tail(&wrap->list, &info->rx_urb_q);
1055 if (info->flags & THROTTLED) {
1056 info->flags |= ACTUALLY_THROTTLED;
1057 spin_unlock(&info->lock);
1058 return;
1060 spin_unlock(&info->lock);
1062 schedule_work(&info->rx_work);
1066 static void whiteheat_write_callback(struct urb *urb)
1068 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1069 struct whiteheat_private *info = usb_get_serial_port_data(port);
1070 struct whiteheat_urb_wrap *wrap;
1071 int status = urb->status;
1073 dbg("%s - port %d", __FUNCTION__, port->number);
1075 spin_lock(&info->lock);
1076 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1077 if (!wrap) {
1078 spin_unlock(&info->lock);
1079 err("%s - Not my urb!", __FUNCTION__);
1080 return;
1082 list_move(&wrap->list, &info->tx_urbs_free);
1083 spin_unlock(&info->lock);
1085 if (status) {
1086 dbg("%s - nonzero write bulk status received: %d",
1087 __FUNCTION__, status);
1088 return;
1091 usb_serial_port_softint(port);
1095 /*****************************************************************************
1096 * Connect Tech's White Heat firmware interface
1097 *****************************************************************************/
1098 static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize)
1100 struct usb_serial_port *command_port;
1101 struct whiteheat_command_private *command_info;
1102 struct whiteheat_private *info;
1103 __u8 *transfer_buffer;
1104 int retval = 0;
1105 int t;
1107 dbg("%s - command %d", __FUNCTION__, command);
1109 command_port = port->serial->port[COMMAND_PORT];
1110 command_info = usb_get_serial_port_data(command_port);
1111 mutex_lock(&command_info->mutex);
1112 command_info->command_finished = false;
1114 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1115 transfer_buffer[0] = command;
1116 memcpy (&transfer_buffer[1], data, datasize);
1117 command_port->write_urb->transfer_buffer_length = datasize + 1;
1118 command_port->write_urb->dev = port->serial->dev;
1119 retval = usb_submit_urb (command_port->write_urb, GFP_NOIO);
1120 if (retval) {
1121 dbg("%s - submit urb failed", __FUNCTION__);
1122 goto exit;
1125 /* wait for the command to complete */
1126 t = wait_event_timeout(command_info->wait_command,
1127 (bool)command_info->command_finished, COMMAND_TIMEOUT);
1128 if (!t)
1129 usb_kill_urb(command_port->write_urb);
1131 if (command_info->command_finished == false) {
1132 dbg("%s - command timed out.", __FUNCTION__);
1133 retval = -ETIMEDOUT;
1134 goto exit;
1137 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1138 dbg("%s - command failed.", __FUNCTION__);
1139 retval = -EIO;
1140 goto exit;
1143 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1144 dbg("%s - command completed.", __FUNCTION__);
1145 switch (command) {
1146 case WHITEHEAT_GET_DTR_RTS:
1147 info = usb_get_serial_port_data(port);
1148 memcpy(&info->mcr, command_info->result_buffer, sizeof(struct whiteheat_dr_info));
1149 break;
1153 exit:
1154 mutex_unlock(&command_info->mutex);
1155 return retval;
1159 static int firm_open(struct usb_serial_port *port) {
1160 struct whiteheat_simple open_command;
1162 open_command.port = port->number - port->serial->minor + 1;
1163 return firm_send_command(port, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command));
1167 static int firm_close(struct usb_serial_port *port) {
1168 struct whiteheat_simple close_command;
1170 close_command.port = port->number - port->serial->minor + 1;
1171 return firm_send_command(port, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command));
1175 static int firm_setup_port(struct usb_serial_port *port) {
1176 struct whiteheat_port_settings port_settings;
1177 unsigned int cflag = port->tty->termios->c_cflag;
1179 port_settings.port = port->number + 1;
1181 /* get the byte size */
1182 switch (cflag & CSIZE) {
1183 case CS5: port_settings.bits = 5; break;
1184 case CS6: port_settings.bits = 6; break;
1185 case CS7: port_settings.bits = 7; break;
1186 default:
1187 case CS8: port_settings.bits = 8; break;
1189 dbg("%s - data bits = %d", __FUNCTION__, port_settings.bits);
1191 /* determine the parity */
1192 if (cflag & PARENB)
1193 if (cflag & CMSPAR)
1194 if (cflag & PARODD)
1195 port_settings.parity = WHITEHEAT_PAR_MARK;
1196 else
1197 port_settings.parity = WHITEHEAT_PAR_SPACE;
1198 else
1199 if (cflag & PARODD)
1200 port_settings.parity = WHITEHEAT_PAR_ODD;
1201 else
1202 port_settings.parity = WHITEHEAT_PAR_EVEN;
1203 else
1204 port_settings.parity = WHITEHEAT_PAR_NONE;
1205 dbg("%s - parity = %c", __FUNCTION__, port_settings.parity);
1207 /* figure out the stop bits requested */
1208 if (cflag & CSTOPB)
1209 port_settings.stop = 2;
1210 else
1211 port_settings.stop = 1;
1212 dbg("%s - stop bits = %d", __FUNCTION__, port_settings.stop);
1214 /* figure out the flow control settings */
1215 if (cflag & CRTSCTS)
1216 port_settings.hflow = (WHITEHEAT_HFLOW_CTS | WHITEHEAT_HFLOW_RTS);
1217 else
1218 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1219 dbg("%s - hardware flow control = %s %s %s %s", __FUNCTION__,
1220 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1221 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1222 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1223 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1225 /* determine software flow control */
1226 if (I_IXOFF(port->tty))
1227 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1228 else
1229 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1230 dbg("%s - software flow control = %c", __FUNCTION__, port_settings.sflow);
1232 port_settings.xon = START_CHAR(port->tty);
1233 port_settings.xoff = STOP_CHAR(port->tty);
1234 dbg("%s - XON = %2x, XOFF = %2x", __FUNCTION__, port_settings.xon, port_settings.xoff);
1236 /* get the baud rate wanted */
1237 port_settings.baud = tty_get_baud_rate(port->tty);
1238 dbg("%s - baud rate = %d", __FUNCTION__, port_settings.baud);
1240 /* fixme: should set validated settings */
1241 tty_encode_baud_rate(port->tty, port_settings.baud, port_settings.baud);
1242 /* handle any settings that aren't specified in the tty structure */
1243 port_settings.lloop = 0;
1245 /* now send the message to the device */
1246 return firm_send_command(port, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings));
1250 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) {
1251 struct whiteheat_set_rdb rts_command;
1253 rts_command.port = port->number - port->serial->minor + 1;
1254 rts_command.state = onoff;
1255 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&rts_command, sizeof(rts_command));
1259 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) {
1260 struct whiteheat_set_rdb dtr_command;
1262 dtr_command.port = port->number - port->serial->minor + 1;
1263 dtr_command.state = onoff;
1264 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&dtr_command, sizeof(dtr_command));
1268 static int firm_set_break(struct usb_serial_port *port, __u8 onoff) {
1269 struct whiteheat_set_rdb break_command;
1271 break_command.port = port->number - port->serial->minor + 1;
1272 break_command.state = onoff;
1273 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&break_command, sizeof(break_command));
1277 static int firm_purge(struct usb_serial_port *port, __u8 rxtx) {
1278 struct whiteheat_purge purge_command;
1280 purge_command.port = port->number - port->serial->minor + 1;
1281 purge_command.what = rxtx;
1282 return firm_send_command(port, WHITEHEAT_PURGE, (__u8 *)&purge_command, sizeof(purge_command));
1286 static int firm_get_dtr_rts(struct usb_serial_port *port) {
1287 struct whiteheat_simple get_dr_command;
1289 get_dr_command.port = port->number - port->serial->minor + 1;
1290 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, (__u8 *)&get_dr_command, sizeof(get_dr_command));
1294 static int firm_report_tx_done(struct usb_serial_port *port) {
1295 struct whiteheat_simple close_command;
1297 close_command.port = port->number - port->serial->minor + 1;
1298 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, (__u8 *)&close_command, sizeof(close_command));
1302 /*****************************************************************************
1303 * Connect Tech's White Heat utility functions
1304 *****************************************************************************/
1305 static int start_command_port(struct usb_serial *serial)
1307 struct usb_serial_port *command_port;
1308 struct whiteheat_command_private *command_info;
1309 int retval = 0;
1311 command_port = serial->port[COMMAND_PORT];
1312 command_info = usb_get_serial_port_data(command_port);
1313 mutex_lock(&command_info->mutex);
1314 if (!command_info->port_running) {
1315 /* Work around HCD bugs */
1316 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1318 command_port->read_urb->dev = serial->dev;
1319 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1320 if (retval) {
1321 err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
1322 goto exit;
1325 command_info->port_running++;
1327 exit:
1328 mutex_unlock(&command_info->mutex);
1329 return retval;
1333 static void stop_command_port(struct usb_serial *serial)
1335 struct usb_serial_port *command_port;
1336 struct whiteheat_command_private *command_info;
1338 command_port = serial->port[COMMAND_PORT];
1339 command_info = usb_get_serial_port_data(command_port);
1340 mutex_lock(&command_info->mutex);
1341 command_info->port_running--;
1342 if (!command_info->port_running)
1343 usb_kill_urb(command_port->read_urb);
1344 mutex_unlock(&command_info->mutex);
1348 static int start_port_read(struct usb_serial_port *port)
1350 struct whiteheat_private *info = usb_get_serial_port_data(port);
1351 struct whiteheat_urb_wrap *wrap;
1352 struct urb *urb;
1353 int retval = 0;
1354 unsigned long flags;
1355 struct list_head *tmp;
1356 struct list_head *tmp2;
1358 spin_lock_irqsave(&info->lock, flags);
1360 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1361 list_del(tmp);
1362 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1363 urb = wrap->urb;
1364 urb->dev = port->serial->dev;
1365 spin_unlock_irqrestore(&info->lock, flags);
1366 retval = usb_submit_urb(urb, GFP_KERNEL);
1367 if (retval) {
1368 spin_lock_irqsave(&info->lock, flags);
1369 list_add(tmp, &info->rx_urbs_free);
1370 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1371 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1372 urb = wrap->urb;
1373 list_del(tmp);
1374 spin_unlock_irqrestore(&info->lock, flags);
1375 usb_kill_urb(urb);
1376 spin_lock_irqsave(&info->lock, flags);
1377 list_add(tmp, &info->rx_urbs_free);
1379 break;
1381 spin_lock_irqsave(&info->lock, flags);
1382 list_add(tmp, &info->rx_urbs_submitted);
1385 spin_unlock_irqrestore(&info->lock, flags);
1387 return retval;
1391 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb* urb, struct list_head *head)
1393 struct whiteheat_urb_wrap *wrap;
1394 struct list_head *tmp;
1396 list_for_each(tmp, head) {
1397 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1398 if (wrap->urb == urb)
1399 return wrap;
1402 return NULL;
1406 static struct list_head *list_first(struct list_head *head)
1408 return head->next;
1412 static void rx_data_softint(struct work_struct *work)
1414 struct whiteheat_private *info =
1415 container_of(work, struct whiteheat_private, rx_work);
1416 struct usb_serial_port *port = info->port;
1417 struct tty_struct *tty = port->tty;
1418 struct whiteheat_urb_wrap *wrap;
1419 struct urb *urb;
1420 unsigned long flags;
1421 struct list_head *tmp;
1422 struct list_head *tmp2;
1423 int result;
1424 int sent = 0;
1426 spin_lock_irqsave(&info->lock, flags);
1427 if (info->flags & THROTTLED) {
1428 spin_unlock_irqrestore(&info->lock, flags);
1429 return;
1432 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1433 list_del(tmp);
1434 spin_unlock_irqrestore(&info->lock, flags);
1436 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1437 urb = wrap->urb;
1439 if (tty && urb->actual_length) {
1440 int len = tty_buffer_request_room(tty, urb->actual_length);
1441 /* This stuff can go away now I suspect */
1442 if (unlikely(len < urb->actual_length)) {
1443 spin_lock_irqsave(&info->lock, flags);
1444 list_add(tmp, &info->rx_urb_q);
1445 spin_unlock_irqrestore(&info->lock, flags);
1446 tty_flip_buffer_push(tty);
1447 schedule_work(&info->rx_work);
1448 return;
1450 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1451 sent += len;
1454 urb->dev = port->serial->dev;
1455 result = usb_submit_urb(urb, GFP_ATOMIC);
1456 if (result) {
1457 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1458 spin_lock_irqsave(&info->lock, flags);
1459 list_add(tmp, &info->rx_urbs_free);
1460 continue;
1463 spin_lock_irqsave(&info->lock, flags);
1464 list_add(tmp, &info->rx_urbs_submitted);
1466 spin_unlock_irqrestore(&info->lock, flags);
1468 if (sent)
1469 tty_flip_buffer_push(tty);
1473 /*****************************************************************************
1474 * Connect Tech's White Heat module functions
1475 *****************************************************************************/
1476 static int __init whiteheat_init (void)
1478 int retval;
1479 retval = usb_serial_register(&whiteheat_fake_device);
1480 if (retval)
1481 goto failed_fake_register;
1482 retval = usb_serial_register(&whiteheat_device);
1483 if (retval)
1484 goto failed_device_register;
1485 retval = usb_register(&whiteheat_driver);
1486 if (retval)
1487 goto failed_usb_register;
1488 info(DRIVER_DESC " " DRIVER_VERSION);
1489 return 0;
1490 failed_usb_register:
1491 usb_serial_deregister(&whiteheat_device);
1492 failed_device_register:
1493 usb_serial_deregister(&whiteheat_fake_device);
1494 failed_fake_register:
1495 return retval;
1499 static void __exit whiteheat_exit (void)
1501 usb_deregister (&whiteheat_driver);
1502 usb_serial_deregister (&whiteheat_fake_device);
1503 usb_serial_deregister (&whiteheat_device);
1507 module_init(whiteheat_init);
1508 module_exit(whiteheat_exit);
1510 MODULE_AUTHOR( DRIVER_AUTHOR );
1511 MODULE_DESCRIPTION( DRIVER_DESC );
1512 MODULE_LICENSE("GPL");
1514 module_param(urb_pool_size, int, 0);
1515 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1517 module_param(debug, bool, S_IRUGO | S_IWUSR);
1518 MODULE_PARM_DESC(debug, "Debug enabled or not");