2 * USB ConnectTech WhiteHEAT driver
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
18 * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com)
19 * Upgrade to full working driver
22 * switched from using spinlock to a semaphore, which fixes lots of
26 * Identify version on module load.
29 * Fixed MOD_INC and MOD_DEC logic, the ability to open a port more
30 * than once, and the got the proper usb_device_id table entries so
31 * the driver works again.
33 * (11/01/2000) Adam J. Richter
34 * usb_device_id table support
37 * Fixed bug with urb->dev not being set properly, now that the usb
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
49 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
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
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.
62 * First cut at open and close commands. Data can flow through the ports at
66 * Split driver up into device specific pieces.
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>
79 #include <linux/mutex.h>
80 #include <linux/uaccess.h>
81 #include <asm/termbits.h>
82 #include <linux/usb.h>
83 #include <linux/serial_reg.h>
84 #include <linux/serial.h>
85 #include <linux/usb/serial.h>
86 #include <linux/firmware.h>
87 #include <linux/ihex.h>
88 #include "whiteheat.h" /* WhiteHEAT specific commands */
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"
103 #define CONNECT_TECH_VENDOR_ID 0x0710
104 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
105 #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
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.
114 static struct usb_device_id id_table_std [] = {
115 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
116 { } /* Terminating entry */
119 static struct usb_device_id id_table_prerenumeration [] = {
120 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
121 { } /* Terminating entry */
124 static 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 */
130 MODULE_DEVICE_TABLE(usb, id_table_combined);
132 static struct usb_driver whiteheat_driver = {
134 .probe = usb_serial_probe,
135 .disconnect = usb_serial_disconnect,
136 .id_table = id_table_combined,
140 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
141 static int whiteheat_firmware_download(struct usb_serial *serial,
142 const struct usb_device_id *id);
143 static int whiteheat_firmware_attach(struct usb_serial *serial);
145 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
146 static int whiteheat_attach(struct usb_serial *serial);
147 static void whiteheat_shutdown(struct usb_serial *serial);
148 static int whiteheat_open(struct tty_struct *tty,
149 struct usb_serial_port *port, struct file *filp);
150 static void whiteheat_close(struct tty_struct *tty,
151 struct usb_serial_port *port, struct file *filp);
152 static int whiteheat_write(struct tty_struct *tty,
153 struct usb_serial_port *port,
154 const unsigned char *buf, int count);
155 static int whiteheat_write_room(struct tty_struct *tty);
156 static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
157 unsigned int cmd, unsigned long arg);
158 static void whiteheat_set_termios(struct tty_struct *tty,
159 struct usb_serial_port *port, struct ktermios *old);
160 static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file);
161 static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
162 unsigned int set, unsigned int clear);
163 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
164 static int whiteheat_chars_in_buffer(struct tty_struct *tty);
165 static void whiteheat_throttle(struct tty_struct *tty);
166 static void whiteheat_unthrottle(struct tty_struct *tty);
167 static void whiteheat_read_callback(struct urb *urb);
168 static void whiteheat_write_callback(struct urb *urb);
170 static struct usb_serial_driver whiteheat_fake_device = {
172 .owner = THIS_MODULE,
173 .name = "whiteheatnofirm",
175 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
176 .usb_driver = &whiteheat_driver,
177 .id_table = id_table_prerenumeration,
179 .probe = whiteheat_firmware_download,
180 .attach = whiteheat_firmware_attach,
183 static struct usb_serial_driver whiteheat_device = {
185 .owner = THIS_MODULE,
188 .description = "Connect Tech - WhiteHEAT",
189 .usb_driver = &whiteheat_driver,
190 .id_table = id_table_std,
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,
211 struct whiteheat_command_private {
214 __u8 command_finished;
215 wait_queue_head_t wait_command; /* for handling sleeping whilst
216 waiting for a command to
218 __u8 result_buffer[64];
222 #define THROTTLED 0x01
223 #define ACTUALLY_THROTTLED 0x02
225 static int urb_pool_size = 8;
227 struct whiteheat_urb_wrap {
228 struct list_head list;
232 struct whiteheat_private {
235 __u8 mcr; /* FIXME: no locking on mcr */
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;
240 struct usb_serial_port *port;
241 struct list_head tx_urbs_free;
242 struct list_head tx_urbs_submitted;
243 struct mutex deathwarrant;
247 /* local function prototypes */
248 static int start_command_port(struct usb_serial *serial);
249 static void stop_command_port(struct usb_serial *serial);
250 static void command_port_write_callback(struct urb *urb);
251 static void command_port_read_callback(struct urb *urb);
253 static int start_port_read(struct usb_serial_port *port);
254 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
255 struct list_head *head);
256 static struct list_head *list_first(struct list_head *head);
257 static void rx_data_softint(struct work_struct *work);
259 static int firm_send_command(struct usb_serial_port *port, __u8 command,
260 __u8 *data, __u8 datasize);
261 static int firm_open(struct usb_serial_port *port);
262 static int firm_close(struct usb_serial_port *port);
263 static int firm_setup_port(struct tty_struct *tty);
264 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
265 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
266 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
267 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
268 static int firm_get_dtr_rts(struct usb_serial_port *port);
269 static int firm_report_tx_done(struct usb_serial_port *port);
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)
278 /*****************************************************************************
279 * Connect Tech's White Heat prerenumeration driver functions
280 *****************************************************************************/
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
289 - download the WH.HEX file for all addresses less than 0x1b40 using
290 VENDOR_REQUEST_ANCHOR_LOAD
292 - device renumerated itself and comes up as new device id with all
293 firmware download completed.
295 static int whiteheat_firmware_download(struct usb_serial *serial,
296 const struct usb_device_id *id)
298 int response, ret = -ENOENT;
299 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
300 const struct ihex_binrec *record;
304 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
305 &serial->dev->dev)) {
306 err("%s - request \"whiteheat.fw\" failed", __func__);
309 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
310 &serial->dev->dev)) {
311 err("%s - request \"whiteheat_loader.fw\" failed", __func__);
315 response = ezusb_set_reset (serial, 1);
317 record = (const struct ihex_binrec *)loader_fw->data;
319 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
320 (unsigned char *)record->data,
321 be16_to_cpu(record->len), 0xa0);
323 err("%s - ezusb_writememory failed for loader (%d %04X %p %d)",
324 __func__, response, be32_to_cpu(record->addr),
325 record->data, be16_to_cpu(record->len));
328 record = ihex_next_binrec(record);
331 response = ezusb_set_reset(serial, 0);
333 record = (const struct ihex_binrec *)firmware_fw->data;
334 while (record && be32_to_cpu(record->addr) < 0x1b40)
335 record = ihex_next_binrec(record);
337 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
338 (unsigned char *)record->data,
339 be16_to_cpu(record->len), 0xa3);
341 err("%s - ezusb_writememory failed for first firmware step (%d %04X %p %d)",
342 __func__, response, be32_to_cpu(record->addr),
343 record->data, be16_to_cpu(record->len));
349 response = ezusb_set_reset(serial, 1);
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);
357 err("%s - ezusb_writememory failed for second firmware step (%d %04X %p %d)",
358 __func__, response, be32_to_cpu(record->addr),
359 record->data, be16_to_cpu(record->len));
365 response = ezusb_set_reset (serial, 0);
367 release_firmware(loader_fw);
368 release_firmware(firmware_fw);
373 static int whiteheat_firmware_attach(struct usb_serial *serial)
375 /* We want this device to fail to have a driver assigned to it */
380 /*****************************************************************************
381 * Connect Tech's White Heat serial driver functions
382 *****************************************************************************/
383 static int whiteheat_attach(struct usb_serial *serial)
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;
399 struct whiteheat_urb_wrap *wrap;
400 struct list_head *tmp;
402 command_port = serial->port[COMMAND_PORT];
404 pipe = usb_sndbulkpipe(serial->dev,
405 command_port->bulk_out_endpointAddress);
406 command = kmalloc(2, GFP_KERNEL);
408 goto no_command_buffer;
409 command[0] = WHITEHEAT_GET_HW_INFO;
412 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
414 goto no_result_buffer;
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
418 * unlinking bug in disguise. Same for the call below.
420 usb_clear_halt(serial->dev, pipe);
421 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
422 &alen, COMMAND_TIMEOUT_MS);
424 err("%s: Couldn't send command [%d]",
425 serial->type->description, ret);
427 } else if (alen != 2) {
428 err("%s: Send command incomplete [%d]",
429 serial->type->description, alen);
433 pipe = usb_rcvbulkpipe(serial->dev,
434 command_port->bulk_in_endpointAddress);
435 /* See the comment on the usb_clear_halt() above */
436 usb_clear_halt(serial->dev, pipe);
437 ret = usb_bulk_msg(serial->dev, pipe, result,
438 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
440 err("%s: Couldn't get results [%d]",
441 serial->type->description, ret);
443 } else if (alen != sizeof(*hw_info) + 1) {
444 err("%s: Get results incomplete [%d]",
445 serial->type->description, alen);
447 } else if (result[0] != command[0]) {
448 err("%s: Command failed [%d]",
449 serial->type->description, result[0]);
453 hw_info = (struct whiteheat_hw_info *)&result[1];
455 dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
456 serial->type->description, DRIVER_VERSION,
457 hw_info->sw_major_rev, hw_info->sw_minor_rev);
459 for (i = 0; i < serial->num_ports; i++) {
460 port = serial->port[i];
462 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
464 err("%s: Out of memory for port structures\n",
465 serial->type->description);
469 spin_lock_init(&info->lock);
470 mutex_init(&info->deathwarrant);
473 INIT_WORK(&info->rx_work, rx_data_softint);
476 INIT_LIST_HEAD(&info->rx_urbs_free);
477 INIT_LIST_HEAD(&info->rx_urbs_submitted);
478 INIT_LIST_HEAD(&info->rx_urb_q);
479 INIT_LIST_HEAD(&info->tx_urbs_free);
480 INIT_LIST_HEAD(&info->tx_urbs_submitted);
482 for (j = 0; j < urb_pool_size; j++) {
483 urb = usb_alloc_urb(0, GFP_KERNEL);
485 err("No free urbs available");
488 buf_size = port->read_urb->transfer_buffer_length;
489 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
490 if (!urb->transfer_buffer) {
491 err("Couldn't allocate urb buffer");
494 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
496 err("Couldn't allocate urb wrapper");
499 usb_fill_bulk_urb(urb, serial->dev,
500 usb_rcvbulkpipe(serial->dev,
501 port->bulk_in_endpointAddress),
502 urb->transfer_buffer, buf_size,
503 whiteheat_read_callback, port);
505 list_add(&wrap->list, &info->rx_urbs_free);
507 urb = usb_alloc_urb(0, GFP_KERNEL);
509 err("No free urbs available");
512 buf_size = port->write_urb->transfer_buffer_length;
513 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
514 if (!urb->transfer_buffer) {
515 err("Couldn't allocate urb buffer");
518 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
520 err("Couldn't allocate urb wrapper");
523 usb_fill_bulk_urb(urb, serial->dev,
524 usb_sndbulkpipe(serial->dev,
525 port->bulk_out_endpointAddress),
526 urb->transfer_buffer, buf_size,
527 whiteheat_write_callback, port);
529 list_add(&wrap->list, &info->tx_urbs_free);
532 usb_set_serial_port_data(port, info);
535 command_info = kmalloc(sizeof(struct whiteheat_command_private),
537 if (command_info == NULL) {
538 err("%s: Out of memory for port structures\n",
539 serial->type->description);
540 goto no_command_private;
543 mutex_init(&command_info->mutex);
544 command_info->port_running = 0;
545 init_waitqueue_head(&command_info->wait_command);
546 usb_set_serial_port_data(command_port, command_info);
547 command_port->write_urb->complete = command_port_write_callback;
548 command_port->read_urb->complete = command_port_read_callback;
555 /* Firmware likely not running */
556 err("%s: Unable to retrieve firmware version, try replugging\n",
557 serial->type->description);
558 err("%s: If the firmware is not running (status led not blinking)\n",
559 serial->type->description);
560 err("%s: please contact support@connecttech.com\n",
561 serial->type->description);
566 for (i = serial->num_ports - 1; i >= 0; i--) {
567 port = serial->port[i];
568 info = usb_get_serial_port_data(port);
569 for (j = urb_pool_size - 1; j >= 0; j--) {
570 tmp = list_first(&info->tx_urbs_free);
572 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
576 kfree(urb->transfer_buffer);
580 tmp = list_first(&info->rx_urbs_free);
582 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
586 kfree(urb->transfer_buffer);
604 static void whiteheat_shutdown(struct usb_serial *serial)
606 struct usb_serial_port *command_port;
607 struct usb_serial_port *port;
608 struct whiteheat_private *info;
609 struct whiteheat_urb_wrap *wrap;
611 struct list_head *tmp;
612 struct list_head *tmp2;
617 /* free up our private data for our command port */
618 command_port = serial->port[COMMAND_PORT];
619 kfree(usb_get_serial_port_data(command_port));
621 for (i = 0; i < serial->num_ports; i++) {
622 port = serial->port[i];
623 info = usb_get_serial_port_data(port);
624 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
626 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
629 kfree(urb->transfer_buffer);
632 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
634 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
637 kfree(urb->transfer_buffer);
646 static int whiteheat_open(struct tty_struct *tty,
647 struct usb_serial_port *port, struct file *filp)
651 dbg("%s - port %d", __func__, port->number);
653 retval = start_command_port(port->serial);
658 tty->low_latency = 1;
660 /* send an open port command */
661 retval = firm_open(port);
663 stop_command_port(port->serial);
667 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
670 stop_command_port(port->serial);
675 firm_setup_port(tty);
677 /* Work around HCD bugs */
678 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
679 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
681 /* Start reading from the device */
682 retval = start_port_read(port);
684 err("%s - failed submitting read urb, error %d",
687 stop_command_port(port->serial);
692 dbg("%s - exit, retval = %d", __func__, retval);
697 static void whiteheat_close(struct tty_struct *tty,
698 struct usb_serial_port *port, struct file *filp)
700 struct whiteheat_private *info = usb_get_serial_port_data(port);
701 struct whiteheat_urb_wrap *wrap;
703 struct list_head *tmp;
704 struct list_head *tmp2;
706 dbg("%s - port %d", __func__, port->number);
708 mutex_lock(&port->serial->disc_mutex);
709 /* filp is NULL when called from usb_serial_disconnect */
710 if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
711 mutex_unlock(&port->serial->disc_mutex);
714 mutex_unlock(&port->serial->disc_mutex);
719 * Not currently in use; tty_wait_until_sent() calls
720 * serial_chars_in_buffer() which deadlocks on the second semaphore
721 * acquisition. This should be fixed at some point. Greg's been
723 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
724 tty_wait_until_sent(tty, CLOSING_DELAY);
728 tty_driver_flush_buffer(tty);
729 tty_ldisc_flush(tty);
731 firm_report_tx_done(port);
735 /* shutdown our bulk reads and writes */
736 mutex_lock(&info->deathwarrant);
737 spin_lock_irq(&info->lock);
738 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
739 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
742 spin_unlock_irq(&info->lock);
744 spin_lock_irq(&info->lock);
745 list_add(tmp, &info->rx_urbs_free);
747 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
748 list_move(tmp, &info->rx_urbs_free);
749 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
750 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
753 spin_unlock_irq(&info->lock);
755 spin_lock_irq(&info->lock);
756 list_add(tmp, &info->tx_urbs_free);
758 spin_unlock_irq(&info->lock);
759 mutex_unlock(&info->deathwarrant);
761 stop_command_port(port->serial);
767 static int whiteheat_write(struct tty_struct *tty,
768 struct usb_serial_port *port, const unsigned char *buf, int count)
770 struct usb_serial *serial = port->serial;
771 struct whiteheat_private *info = usb_get_serial_port_data(port);
772 struct whiteheat_urb_wrap *wrap;
778 struct list_head *tmp;
780 dbg("%s - port %d", __func__, port->number);
783 dbg("%s - write request of 0 bytes", __func__);
788 spin_lock_irqsave(&info->lock, flags);
789 if (list_empty(&info->tx_urbs_free)) {
790 spin_unlock_irqrestore(&info->lock, flags);
793 tmp = list_first(&info->tx_urbs_free);
795 spin_unlock_irqrestore(&info->lock, flags);
797 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
799 bytes = (count > port->bulk_out_size) ?
800 port->bulk_out_size : count;
801 memcpy(urb->transfer_buffer, buf + sent, bytes);
803 usb_serial_debug_data(debug, &port->dev,
804 __func__, bytes, urb->transfer_buffer);
806 urb->dev = serial->dev;
807 urb->transfer_buffer_length = bytes;
808 result = usb_submit_urb(urb, GFP_ATOMIC);
810 err("%s - failed submitting write urb, error %d",
813 spin_lock_irqsave(&info->lock, flags);
814 list_add(tmp, &info->tx_urbs_free);
815 spin_unlock_irqrestore(&info->lock, flags);
820 spin_lock_irqsave(&info->lock, flags);
821 list_add(tmp, &info->tx_urbs_submitted);
822 spin_unlock_irqrestore(&info->lock, flags);
829 static int whiteheat_write_room(struct tty_struct *tty)
831 struct usb_serial_port *port = tty->driver_data;
832 struct whiteheat_private *info = usb_get_serial_port_data(port);
833 struct list_head *tmp;
837 dbg("%s - port %d", __func__, port->number);
839 spin_lock_irqsave(&info->lock, flags);
840 list_for_each(tmp, &info->tx_urbs_free)
842 spin_unlock_irqrestore(&info->lock, flags);
843 room *= port->bulk_out_size;
845 dbg("%s - returns %d", __func__, room);
849 static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file)
851 struct usb_serial_port *port = tty->driver_data;
852 struct whiteheat_private *info = usb_get_serial_port_data(port);
853 unsigned int modem_signals = 0;
855 dbg("%s - port %d", __func__, port->number);
857 firm_get_dtr_rts(port);
858 if (info->mcr & UART_MCR_DTR)
859 modem_signals |= TIOCM_DTR;
860 if (info->mcr & UART_MCR_RTS)
861 modem_signals |= TIOCM_RTS;
863 return modem_signals;
866 static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
867 unsigned int set, unsigned int clear)
869 struct usb_serial_port *port = tty->driver_data;
870 struct whiteheat_private *info = usb_get_serial_port_data(port);
872 dbg("%s - port %d", __func__, port->number);
875 info->mcr |= UART_MCR_RTS;
877 info->mcr |= UART_MCR_DTR;
879 if (clear & TIOCM_RTS)
880 info->mcr &= ~UART_MCR_RTS;
881 if (clear & TIOCM_DTR)
882 info->mcr &= ~UART_MCR_DTR;
884 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
885 firm_set_rts(port, info->mcr & UART_MCR_RTS);
890 static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
891 unsigned int cmd, unsigned long arg)
893 struct usb_serial_port *port = tty->driver_data;
894 struct serial_struct serstruct;
895 void __user *user_arg = (void __user *)arg;
897 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
901 memset(&serstruct, 0, sizeof(serstruct));
902 serstruct.type = PORT_16654;
903 serstruct.line = port->serial->minor;
904 serstruct.port = port->number;
905 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
906 serstruct.xmit_fifo_size = port->bulk_out_size;
907 serstruct.custom_divisor = 0;
908 serstruct.baud_base = 460800;
909 serstruct.close_delay = CLOSING_DELAY;
910 serstruct.closing_wait = CLOSING_DELAY;
912 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
923 static void whiteheat_set_termios(struct tty_struct *tty,
924 struct usb_serial_port *port, struct ktermios *old_termios)
926 firm_setup_port(tty);
929 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
931 struct usb_serial_port *port = tty->driver_data;
932 firm_set_break(port, break_state);
936 static int whiteheat_chars_in_buffer(struct tty_struct *tty)
938 struct usb_serial_port *port = tty->driver_data;
939 struct whiteheat_private *info = usb_get_serial_port_data(port);
940 struct list_head *tmp;
941 struct whiteheat_urb_wrap *wrap;
945 dbg("%s - port %d", __func__, port->number);
947 spin_lock_irqsave(&info->lock, flags);
948 list_for_each(tmp, &info->tx_urbs_submitted) {
949 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
950 chars += wrap->urb->transfer_buffer_length;
952 spin_unlock_irqrestore(&info->lock, flags);
954 dbg("%s - returns %d", __func__, chars);
959 static void whiteheat_throttle(struct tty_struct *tty)
961 struct usb_serial_port *port = tty->driver_data;
962 struct whiteheat_private *info = usb_get_serial_port_data(port);
965 dbg("%s - port %d", __func__, port->number);
967 spin_lock_irqsave(&info->lock, flags);
968 info->flags |= THROTTLED;
969 spin_unlock_irqrestore(&info->lock, flags);
975 static void whiteheat_unthrottle(struct tty_struct *tty)
977 struct usb_serial_port *port = tty->driver_data;
978 struct whiteheat_private *info = usb_get_serial_port_data(port);
979 int actually_throttled;
982 dbg("%s - port %d", __func__, port->number);
984 spin_lock_irqsave(&info->lock, flags);
985 actually_throttled = info->flags & ACTUALLY_THROTTLED;
986 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
987 spin_unlock_irqrestore(&info->lock, flags);
989 if (actually_throttled)
990 rx_data_softint(&info->rx_work);
996 /*****************************************************************************
997 * Connect Tech's White Heat callback routines
998 *****************************************************************************/
999 static void command_port_write_callback(struct urb *urb)
1001 int status = urb->status;
1003 dbg("%s", __func__);
1006 dbg("nonzero urb status: %d", status);
1012 static void command_port_read_callback(struct urb *urb)
1014 struct usb_serial_port *command_port = urb->context;
1015 struct whiteheat_command_private *command_info;
1016 int status = urb->status;
1017 unsigned char *data = urb->transfer_buffer;
1020 dbg("%s", __func__);
1022 command_info = usb_get_serial_port_data(command_port);
1023 if (!command_info) {
1024 dbg("%s - command_info is NULL, exiting.", __func__);
1028 dbg("%s - nonzero urb status: %d", __func__, status);
1029 if (status != -ENOENT)
1030 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1031 wake_up(&command_info->wait_command);
1035 usb_serial_debug_data(debug, &command_port->dev,
1036 __func__, urb->actual_length, data);
1038 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1039 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1040 wake_up(&command_info->wait_command);
1041 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1042 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1043 wake_up(&command_info->wait_command);
1044 } else if (data[0] == WHITEHEAT_EVENT) {
1045 /* These are unsolicited reports from the firmware, hence no
1046 waiting command to wakeup */
1047 dbg("%s - event received", __func__);
1048 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
1049 memcpy(command_info->result_buffer, &data[1],
1050 urb->actual_length - 1);
1051 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1052 wake_up(&command_info->wait_command);
1054 dbg("%s - bad reply from firmware", __func__);
1056 /* Continue trying to always read */
1057 command_port->read_urb->dev = command_port->serial->dev;
1058 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
1060 dbg("%s - failed resubmitting read urb, error %d",
1065 static void whiteheat_read_callback(struct urb *urb)
1067 struct usb_serial_port *port = urb->context;
1068 struct whiteheat_urb_wrap *wrap;
1069 unsigned char *data = urb->transfer_buffer;
1070 struct whiteheat_private *info = usb_get_serial_port_data(port);
1071 int status = urb->status;
1073 dbg("%s - port %d", __func__, port->number);
1075 spin_lock(&info->lock);
1076 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1078 spin_unlock(&info->lock);
1079 err("%s - Not my urb!", __func__);
1082 list_del(&wrap->list);
1083 spin_unlock(&info->lock);
1086 dbg("%s - nonzero read bulk status received: %d",
1088 spin_lock(&info->lock);
1089 list_add(&wrap->list, &info->rx_urbs_free);
1090 spin_unlock(&info->lock);
1094 usb_serial_debug_data(debug, &port->dev,
1095 __func__, urb->actual_length, data);
1097 spin_lock(&info->lock);
1098 list_add_tail(&wrap->list, &info->rx_urb_q);
1099 if (info->flags & THROTTLED) {
1100 info->flags |= ACTUALLY_THROTTLED;
1101 spin_unlock(&info->lock);
1104 spin_unlock(&info->lock);
1106 schedule_work(&info->rx_work);
1110 static void whiteheat_write_callback(struct urb *urb)
1112 struct usb_serial_port *port = urb->context;
1113 struct whiteheat_private *info = usb_get_serial_port_data(port);
1114 struct whiteheat_urb_wrap *wrap;
1115 int status = urb->status;
1117 dbg("%s - port %d", __func__, port->number);
1119 spin_lock(&info->lock);
1120 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1122 spin_unlock(&info->lock);
1123 err("%s - Not my urb!", __func__);
1126 list_move(&wrap->list, &info->tx_urbs_free);
1127 spin_unlock(&info->lock);
1130 dbg("%s - nonzero write bulk status received: %d",
1135 usb_serial_port_softint(port);
1139 /*****************************************************************************
1140 * Connect Tech's White Heat firmware interface
1141 *****************************************************************************/
1142 static int firm_send_command(struct usb_serial_port *port, __u8 command,
1143 __u8 *data, __u8 datasize)
1145 struct usb_serial_port *command_port;
1146 struct whiteheat_command_private *command_info;
1147 struct whiteheat_private *info;
1148 __u8 *transfer_buffer;
1152 dbg("%s - command %d", __func__, command);
1154 command_port = port->serial->port[COMMAND_PORT];
1155 command_info = usb_get_serial_port_data(command_port);
1156 mutex_lock(&command_info->mutex);
1157 command_info->command_finished = false;
1159 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1160 transfer_buffer[0] = command;
1161 memcpy(&transfer_buffer[1], data, datasize);
1162 command_port->write_urb->transfer_buffer_length = datasize + 1;
1163 command_port->write_urb->dev = port->serial->dev;
1164 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1166 dbg("%s - submit urb failed", __func__);
1170 /* wait for the command to complete */
1171 t = wait_event_timeout(command_info->wait_command,
1172 (bool)command_info->command_finished, COMMAND_TIMEOUT);
1174 usb_kill_urb(command_port->write_urb);
1176 if (command_info->command_finished == false) {
1177 dbg("%s - command timed out.", __func__);
1178 retval = -ETIMEDOUT;
1182 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1183 dbg("%s - command failed.", __func__);
1188 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1189 dbg("%s - command completed.", __func__);
1191 case WHITEHEAT_GET_DTR_RTS:
1192 info = usb_get_serial_port_data(port);
1193 memcpy(&info->mcr, command_info->result_buffer,
1194 sizeof(struct whiteheat_dr_info));
1199 mutex_unlock(&command_info->mutex);
1204 static int firm_open(struct usb_serial_port *port)
1206 struct whiteheat_simple open_command;
1208 open_command.port = port->number - port->serial->minor + 1;
1209 return firm_send_command(port, WHITEHEAT_OPEN,
1210 (__u8 *)&open_command, sizeof(open_command));
1214 static int firm_close(struct usb_serial_port *port)
1216 struct whiteheat_simple close_command;
1218 close_command.port = port->number - port->serial->minor + 1;
1219 return firm_send_command(port, WHITEHEAT_CLOSE,
1220 (__u8 *)&close_command, sizeof(close_command));
1224 static int firm_setup_port(struct tty_struct *tty)
1226 struct usb_serial_port *port = tty->driver_data;
1227 struct whiteheat_port_settings port_settings;
1228 unsigned int cflag = tty->termios->c_cflag;
1230 port_settings.port = port->number + 1;
1232 /* get the byte size */
1233 switch (cflag & CSIZE) {
1234 case CS5: port_settings.bits = 5; break;
1235 case CS6: port_settings.bits = 6; break;
1236 case CS7: port_settings.bits = 7; break;
1238 case CS8: port_settings.bits = 8; break;
1240 dbg("%s - data bits = %d", __func__, port_settings.bits);
1242 /* determine the parity */
1246 port_settings.parity = WHITEHEAT_PAR_MARK;
1248 port_settings.parity = WHITEHEAT_PAR_SPACE;
1251 port_settings.parity = WHITEHEAT_PAR_ODD;
1253 port_settings.parity = WHITEHEAT_PAR_EVEN;
1255 port_settings.parity = WHITEHEAT_PAR_NONE;
1256 dbg("%s - parity = %c", __func__, port_settings.parity);
1258 /* figure out the stop bits requested */
1260 port_settings.stop = 2;
1262 port_settings.stop = 1;
1263 dbg("%s - stop bits = %d", __func__, port_settings.stop);
1265 /* figure out the flow control settings */
1266 if (cflag & CRTSCTS)
1267 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1268 WHITEHEAT_HFLOW_RTS);
1270 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1271 dbg("%s - hardware flow control = %s %s %s %s", __func__,
1272 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1273 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1274 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1275 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1277 /* determine software flow control */
1279 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1281 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1282 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
1284 port_settings.xon = START_CHAR(tty);
1285 port_settings.xoff = STOP_CHAR(tty);
1286 dbg("%s - XON = %2x, XOFF = %2x",
1287 __func__, port_settings.xon, port_settings.xoff);
1289 /* get the baud rate wanted */
1290 port_settings.baud = tty_get_baud_rate(tty);
1291 dbg("%s - baud rate = %d", __func__, port_settings.baud);
1293 /* fixme: should set validated settings */
1294 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1295 /* handle any settings that aren't specified in the tty structure */
1296 port_settings.lloop = 0;
1298 /* now send the message to the device */
1299 return firm_send_command(port, WHITEHEAT_SETUP_PORT,
1300 (__u8 *)&port_settings, sizeof(port_settings));
1304 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1306 struct whiteheat_set_rdb rts_command;
1308 rts_command.port = port->number - port->serial->minor + 1;
1309 rts_command.state = onoff;
1310 return firm_send_command(port, WHITEHEAT_SET_RTS,
1311 (__u8 *)&rts_command, sizeof(rts_command));
1315 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1317 struct whiteheat_set_rdb dtr_command;
1319 dtr_command.port = port->number - port->serial->minor + 1;
1320 dtr_command.state = onoff;
1321 return firm_send_command(port, WHITEHEAT_SET_DTR,
1322 (__u8 *)&dtr_command, sizeof(dtr_command));
1326 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1328 struct whiteheat_set_rdb break_command;
1330 break_command.port = port->number - port->serial->minor + 1;
1331 break_command.state = onoff;
1332 return firm_send_command(port, WHITEHEAT_SET_BREAK,
1333 (__u8 *)&break_command, sizeof(break_command));
1337 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1339 struct whiteheat_purge purge_command;
1341 purge_command.port = port->number - port->serial->minor + 1;
1342 purge_command.what = rxtx;
1343 return firm_send_command(port, WHITEHEAT_PURGE,
1344 (__u8 *)&purge_command, sizeof(purge_command));
1348 static int firm_get_dtr_rts(struct usb_serial_port *port)
1350 struct whiteheat_simple get_dr_command;
1352 get_dr_command.port = port->number - port->serial->minor + 1;
1353 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1354 (__u8 *)&get_dr_command, sizeof(get_dr_command));
1358 static int firm_report_tx_done(struct usb_serial_port *port)
1360 struct whiteheat_simple close_command;
1362 close_command.port = port->number - port->serial->minor + 1;
1363 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1364 (__u8 *)&close_command, sizeof(close_command));
1368 /*****************************************************************************
1369 * Connect Tech's White Heat utility functions
1370 *****************************************************************************/
1371 static int start_command_port(struct usb_serial *serial)
1373 struct usb_serial_port *command_port;
1374 struct whiteheat_command_private *command_info;
1377 command_port = serial->port[COMMAND_PORT];
1378 command_info = usb_get_serial_port_data(command_port);
1379 mutex_lock(&command_info->mutex);
1380 if (!command_info->port_running) {
1381 /* Work around HCD bugs */
1382 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1384 command_port->read_urb->dev = serial->dev;
1385 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1387 err("%s - failed submitting read urb, error %d",
1392 command_info->port_running++;
1395 mutex_unlock(&command_info->mutex);
1400 static void stop_command_port(struct usb_serial *serial)
1402 struct usb_serial_port *command_port;
1403 struct whiteheat_command_private *command_info;
1405 command_port = serial->port[COMMAND_PORT];
1406 command_info = usb_get_serial_port_data(command_port);
1407 mutex_lock(&command_info->mutex);
1408 command_info->port_running--;
1409 if (!command_info->port_running)
1410 usb_kill_urb(command_port->read_urb);
1411 mutex_unlock(&command_info->mutex);
1415 static int start_port_read(struct usb_serial_port *port)
1417 struct whiteheat_private *info = usb_get_serial_port_data(port);
1418 struct whiteheat_urb_wrap *wrap;
1421 unsigned long flags;
1422 struct list_head *tmp;
1423 struct list_head *tmp2;
1425 spin_lock_irqsave(&info->lock, flags);
1427 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1429 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1431 urb->dev = port->serial->dev;
1432 spin_unlock_irqrestore(&info->lock, flags);
1433 retval = usb_submit_urb(urb, GFP_KERNEL);
1435 spin_lock_irqsave(&info->lock, flags);
1436 list_add(tmp, &info->rx_urbs_free);
1437 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1438 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1441 spin_unlock_irqrestore(&info->lock, flags);
1443 spin_lock_irqsave(&info->lock, flags);
1444 list_add(tmp, &info->rx_urbs_free);
1448 spin_lock_irqsave(&info->lock, flags);
1449 list_add(tmp, &info->rx_urbs_submitted);
1452 spin_unlock_irqrestore(&info->lock, flags);
1458 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1459 struct list_head *head)
1461 struct whiteheat_urb_wrap *wrap;
1462 struct list_head *tmp;
1464 list_for_each(tmp, head) {
1465 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1466 if (wrap->urb == urb)
1474 static struct list_head *list_first(struct list_head *head)
1480 static void rx_data_softint(struct work_struct *work)
1482 struct whiteheat_private *info =
1483 container_of(work, struct whiteheat_private, rx_work);
1484 struct usb_serial_port *port = info->port;
1485 struct tty_struct *tty = tty_port_tty_get(&port->port);
1486 struct whiteheat_urb_wrap *wrap;
1488 unsigned long flags;
1489 struct list_head *tmp;
1490 struct list_head *tmp2;
1494 spin_lock_irqsave(&info->lock, flags);
1495 if (info->flags & THROTTLED) {
1496 spin_unlock_irqrestore(&info->lock, flags);
1500 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1502 spin_unlock_irqrestore(&info->lock, flags);
1504 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1507 if (tty && urb->actual_length) {
1508 int len = tty_buffer_request_room(tty,
1509 urb->actual_length);
1510 /* This stuff can go away now I suspect */
1511 if (unlikely(len < urb->actual_length)) {
1512 spin_lock_irqsave(&info->lock, flags);
1513 list_add(tmp, &info->rx_urb_q);
1514 spin_unlock_irqrestore(&info->lock, flags);
1515 tty_flip_buffer_push(tty);
1516 schedule_work(&info->rx_work);
1519 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1523 urb->dev = port->serial->dev;
1524 result = usb_submit_urb(urb, GFP_ATOMIC);
1526 err("%s - failed resubmitting read urb, error %d",
1528 spin_lock_irqsave(&info->lock, flags);
1529 list_add(tmp, &info->rx_urbs_free);
1533 spin_lock_irqsave(&info->lock, flags);
1534 list_add(tmp, &info->rx_urbs_submitted);
1536 spin_unlock_irqrestore(&info->lock, flags);
1539 tty_flip_buffer_push(tty);
1545 /*****************************************************************************
1546 * Connect Tech's White Heat module functions
1547 *****************************************************************************/
1548 static int __init whiteheat_init(void)
1551 retval = usb_serial_register(&whiteheat_fake_device);
1553 goto failed_fake_register;
1554 retval = usb_serial_register(&whiteheat_device);
1556 goto failed_device_register;
1557 retval = usb_register(&whiteheat_driver);
1559 goto failed_usb_register;
1560 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1563 failed_usb_register:
1564 usb_serial_deregister(&whiteheat_device);
1565 failed_device_register:
1566 usb_serial_deregister(&whiteheat_fake_device);
1567 failed_fake_register:
1572 static void __exit whiteheat_exit(void)
1574 usb_deregister(&whiteheat_driver);
1575 usb_serial_deregister(&whiteheat_fake_device);
1576 usb_serial_deregister(&whiteheat_device);
1580 module_init(whiteheat_init);
1581 module_exit(whiteheat_exit);
1583 MODULE_AUTHOR(DRIVER_AUTHOR);
1584 MODULE_DESCRIPTION(DRIVER_DESC);
1585 MODULE_LICENSE("GPL");
1587 MODULE_FIRMWARE("whiteheat.fw");
1588 MODULE_FIRMWARE("whiteheat_loader.fw");
1590 module_param(urb_pool_size, int, 0);
1591 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1593 module_param(debug, bool, S_IRUGO | S_IWUSR);
1594 MODULE_PARM_DESC(debug, "Debug enabled or not");