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 dev_err(&serial->dev->dev,
307 "%s - request \"whiteheat.fw\" failed\n", __func__);
310 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
311 &serial->dev->dev)) {
312 dev_err(&serial->dev->dev,
313 "%s - request \"whiteheat_loader.fw\" failed\n",
318 response = ezusb_set_reset (serial, 1);
320 record = (const struct ihex_binrec *)loader_fw->data;
322 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
323 (unsigned char *)record->data,
324 be16_to_cpu(record->len), 0xa0);
326 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
327 "failed for loader (%d %04X %p %d)\n",
328 __func__, response, be32_to_cpu(record->addr),
329 record->data, be16_to_cpu(record->len));
332 record = ihex_next_binrec(record);
335 response = ezusb_set_reset(serial, 0);
337 record = (const struct ihex_binrec *)firmware_fw->data;
338 while (record && be32_to_cpu(record->addr) < 0x1b40)
339 record = ihex_next_binrec(record);
341 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
342 (unsigned char *)record->data,
343 be16_to_cpu(record->len), 0xa3);
345 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
346 "failed for first firmware step "
347 "(%d %04X %p %d)\n", __func__, response,
348 be32_to_cpu(record->addr), record->data,
349 be16_to_cpu(record->len));
355 response = ezusb_set_reset(serial, 1);
357 record = (const struct ihex_binrec *)firmware_fw->data;
358 while (record && be32_to_cpu(record->addr) < 0x1b40) {
359 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
360 (unsigned char *)record->data,
361 be16_to_cpu(record->len), 0xa0);
363 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
364 "failed for second firmware step "
365 "(%d %04X %p %d)\n", __func__, response,
366 be32_to_cpu(record->addr), record->data,
367 be16_to_cpu(record->len));
373 response = ezusb_set_reset (serial, 0);
375 release_firmware(loader_fw);
376 release_firmware(firmware_fw);
381 static int whiteheat_firmware_attach(struct usb_serial *serial)
383 /* We want this device to fail to have a driver assigned to it */
388 /*****************************************************************************
389 * Connect Tech's White Heat serial driver functions
390 *****************************************************************************/
391 static int whiteheat_attach(struct usb_serial *serial)
393 struct usb_serial_port *command_port;
394 struct whiteheat_command_private *command_info;
395 struct usb_serial_port *port;
396 struct whiteheat_private *info;
397 struct whiteheat_hw_info *hw_info;
407 struct whiteheat_urb_wrap *wrap;
408 struct list_head *tmp;
410 command_port = serial->port[COMMAND_PORT];
412 pipe = usb_sndbulkpipe(serial->dev,
413 command_port->bulk_out_endpointAddress);
414 command = kmalloc(2, GFP_KERNEL);
416 goto no_command_buffer;
417 command[0] = WHITEHEAT_GET_HW_INFO;
420 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
422 goto no_result_buffer;
424 * When the module is reloaded the firmware is still there and
425 * the endpoints are still in the usb core unchanged. This is the
426 * unlinking bug in disguise. Same for the call below.
428 usb_clear_halt(serial->dev, pipe);
429 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
430 &alen, COMMAND_TIMEOUT_MS);
432 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
433 serial->type->description, ret);
435 } else if (alen != 2) {
436 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
437 serial->type->description, alen);
441 pipe = usb_rcvbulkpipe(serial->dev,
442 command_port->bulk_in_endpointAddress);
443 /* See the comment on the usb_clear_halt() above */
444 usb_clear_halt(serial->dev, pipe);
445 ret = usb_bulk_msg(serial->dev, pipe, result,
446 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
448 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
449 serial->type->description, ret);
451 } else if (alen != sizeof(*hw_info) + 1) {
452 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
453 serial->type->description, alen);
455 } else if (result[0] != command[0]) {
456 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
457 serial->type->description, result[0]);
461 hw_info = (struct whiteheat_hw_info *)&result[1];
463 dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
464 serial->type->description, DRIVER_VERSION,
465 hw_info->sw_major_rev, hw_info->sw_minor_rev);
467 for (i = 0; i < serial->num_ports; i++) {
468 port = serial->port[i];
470 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
473 "%s: Out of memory for port structures\n",
474 serial->type->description);
478 spin_lock_init(&info->lock);
479 mutex_init(&info->deathwarrant);
482 INIT_WORK(&info->rx_work, rx_data_softint);
485 INIT_LIST_HEAD(&info->rx_urbs_free);
486 INIT_LIST_HEAD(&info->rx_urbs_submitted);
487 INIT_LIST_HEAD(&info->rx_urb_q);
488 INIT_LIST_HEAD(&info->tx_urbs_free);
489 INIT_LIST_HEAD(&info->tx_urbs_submitted);
491 for (j = 0; j < urb_pool_size; j++) {
492 urb = usb_alloc_urb(0, GFP_KERNEL);
494 dev_err(&port->dev, "No free urbs available\n");
497 buf_size = port->read_urb->transfer_buffer_length;
498 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
499 if (!urb->transfer_buffer) {
501 "Couldn't allocate urb buffer\n");
504 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
507 "Couldn't allocate urb wrapper\n");
510 usb_fill_bulk_urb(urb, serial->dev,
511 usb_rcvbulkpipe(serial->dev,
512 port->bulk_in_endpointAddress),
513 urb->transfer_buffer, buf_size,
514 whiteheat_read_callback, port);
516 list_add(&wrap->list, &info->rx_urbs_free);
518 urb = usb_alloc_urb(0, GFP_KERNEL);
520 dev_err(&port->dev, "No free urbs available\n");
523 buf_size = port->write_urb->transfer_buffer_length;
524 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
525 if (!urb->transfer_buffer) {
527 "Couldn't allocate urb buffer\n");
530 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
533 "Couldn't allocate urb wrapper\n");
536 usb_fill_bulk_urb(urb, serial->dev,
537 usb_sndbulkpipe(serial->dev,
538 port->bulk_out_endpointAddress),
539 urb->transfer_buffer, buf_size,
540 whiteheat_write_callback, port);
542 list_add(&wrap->list, &info->tx_urbs_free);
545 usb_set_serial_port_data(port, info);
548 command_info = kmalloc(sizeof(struct whiteheat_command_private),
550 if (command_info == NULL) {
551 dev_err(&serial->dev->dev,
552 "%s: Out of memory for port structures\n",
553 serial->type->description);
554 goto no_command_private;
557 mutex_init(&command_info->mutex);
558 command_info->port_running = 0;
559 init_waitqueue_head(&command_info->wait_command);
560 usb_set_serial_port_data(command_port, command_info);
561 command_port->write_urb->complete = command_port_write_callback;
562 command_port->read_urb->complete = command_port_read_callback;
569 /* Firmware likely not running */
570 dev_err(&serial->dev->dev,
571 "%s: Unable to retrieve firmware version, try replugging\n",
572 serial->type->description);
573 dev_err(&serial->dev->dev,
574 "%s: If the firmware is not running (status led not blinking)\n",
575 serial->type->description);
576 dev_err(&serial->dev->dev,
577 "%s: please contact support@connecttech.com\n",
578 serial->type->description);
583 for (i = serial->num_ports - 1; i >= 0; i--) {
584 port = serial->port[i];
585 info = usb_get_serial_port_data(port);
586 for (j = urb_pool_size - 1; j >= 0; j--) {
587 tmp = list_first(&info->tx_urbs_free);
589 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
593 kfree(urb->transfer_buffer);
597 tmp = list_first(&info->rx_urbs_free);
599 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
603 kfree(urb->transfer_buffer);
621 static void whiteheat_shutdown(struct usb_serial *serial)
623 struct usb_serial_port *command_port;
624 struct usb_serial_port *port;
625 struct whiteheat_private *info;
626 struct whiteheat_urb_wrap *wrap;
628 struct list_head *tmp;
629 struct list_head *tmp2;
634 /* free up our private data for our command port */
635 command_port = serial->port[COMMAND_PORT];
636 kfree(usb_get_serial_port_data(command_port));
638 for (i = 0; i < serial->num_ports; i++) {
639 port = serial->port[i];
640 info = usb_get_serial_port_data(port);
641 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
643 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
646 kfree(urb->transfer_buffer);
649 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
651 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
654 kfree(urb->transfer_buffer);
663 static int whiteheat_open(struct tty_struct *tty,
664 struct usb_serial_port *port, struct file *filp)
668 dbg("%s - port %d", __func__, port->number);
670 retval = start_command_port(port->serial);
675 tty->low_latency = 1;
677 /* send an open port command */
678 retval = firm_open(port);
680 stop_command_port(port->serial);
684 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
687 stop_command_port(port->serial);
692 firm_setup_port(tty);
694 /* Work around HCD bugs */
695 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
696 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
698 /* Start reading from the device */
699 retval = start_port_read(port);
702 "%s - failed submitting read urb, error %d\n",
705 stop_command_port(port->serial);
710 dbg("%s - exit, retval = %d", __func__, retval);
715 static void whiteheat_close(struct tty_struct *tty,
716 struct usb_serial_port *port, struct file *filp)
718 struct whiteheat_private *info = usb_get_serial_port_data(port);
719 struct whiteheat_urb_wrap *wrap;
721 struct list_head *tmp;
722 struct list_head *tmp2;
724 dbg("%s - port %d", __func__, port->number);
726 mutex_lock(&port->serial->disc_mutex);
727 /* filp is NULL when called from usb_serial_disconnect */
728 if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
729 mutex_unlock(&port->serial->disc_mutex);
732 mutex_unlock(&port->serial->disc_mutex);
737 * Not currently in use; tty_wait_until_sent() calls
738 * serial_chars_in_buffer() which deadlocks on the second semaphore
739 * acquisition. This should be fixed at some point. Greg's been
741 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
742 tty_wait_until_sent(tty, CLOSING_DELAY);
746 tty_driver_flush_buffer(tty);
747 tty_ldisc_flush(tty);
749 firm_report_tx_done(port);
753 /* shutdown our bulk reads and writes */
754 mutex_lock(&info->deathwarrant);
755 spin_lock_irq(&info->lock);
756 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
757 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
760 spin_unlock_irq(&info->lock);
762 spin_lock_irq(&info->lock);
763 list_add(tmp, &info->rx_urbs_free);
765 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
766 list_move(tmp, &info->rx_urbs_free);
767 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
768 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
771 spin_unlock_irq(&info->lock);
773 spin_lock_irq(&info->lock);
774 list_add(tmp, &info->tx_urbs_free);
776 spin_unlock_irq(&info->lock);
777 mutex_unlock(&info->deathwarrant);
779 stop_command_port(port->serial);
785 static int whiteheat_write(struct tty_struct *tty,
786 struct usb_serial_port *port, const unsigned char *buf, int count)
788 struct usb_serial *serial = port->serial;
789 struct whiteheat_private *info = usb_get_serial_port_data(port);
790 struct whiteheat_urb_wrap *wrap;
796 struct list_head *tmp;
798 dbg("%s - port %d", __func__, port->number);
801 dbg("%s - write request of 0 bytes", __func__);
806 spin_lock_irqsave(&info->lock, flags);
807 if (list_empty(&info->tx_urbs_free)) {
808 spin_unlock_irqrestore(&info->lock, flags);
811 tmp = list_first(&info->tx_urbs_free);
813 spin_unlock_irqrestore(&info->lock, flags);
815 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
817 bytes = (count > port->bulk_out_size) ?
818 port->bulk_out_size : count;
819 memcpy(urb->transfer_buffer, buf + sent, bytes);
821 usb_serial_debug_data(debug, &port->dev,
822 __func__, bytes, urb->transfer_buffer);
824 urb->dev = serial->dev;
825 urb->transfer_buffer_length = bytes;
826 result = usb_submit_urb(urb, GFP_ATOMIC);
829 "%s - failed submitting write urb, error %d\n",
832 spin_lock_irqsave(&info->lock, flags);
833 list_add(tmp, &info->tx_urbs_free);
834 spin_unlock_irqrestore(&info->lock, flags);
839 spin_lock_irqsave(&info->lock, flags);
840 list_add(tmp, &info->tx_urbs_submitted);
841 spin_unlock_irqrestore(&info->lock, flags);
848 static int whiteheat_write_room(struct tty_struct *tty)
850 struct usb_serial_port *port = tty->driver_data;
851 struct whiteheat_private *info = usb_get_serial_port_data(port);
852 struct list_head *tmp;
856 dbg("%s - port %d", __func__, port->number);
858 spin_lock_irqsave(&info->lock, flags);
859 list_for_each(tmp, &info->tx_urbs_free)
861 spin_unlock_irqrestore(&info->lock, flags);
862 room *= port->bulk_out_size;
864 dbg("%s - returns %d", __func__, room);
868 static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file)
870 struct usb_serial_port *port = tty->driver_data;
871 struct whiteheat_private *info = usb_get_serial_port_data(port);
872 unsigned int modem_signals = 0;
874 dbg("%s - port %d", __func__, port->number);
876 firm_get_dtr_rts(port);
877 if (info->mcr & UART_MCR_DTR)
878 modem_signals |= TIOCM_DTR;
879 if (info->mcr & UART_MCR_RTS)
880 modem_signals |= TIOCM_RTS;
882 return modem_signals;
885 static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
886 unsigned int set, unsigned int clear)
888 struct usb_serial_port *port = tty->driver_data;
889 struct whiteheat_private *info = usb_get_serial_port_data(port);
891 dbg("%s - port %d", __func__, port->number);
894 info->mcr |= UART_MCR_RTS;
896 info->mcr |= UART_MCR_DTR;
898 if (clear & TIOCM_RTS)
899 info->mcr &= ~UART_MCR_RTS;
900 if (clear & TIOCM_DTR)
901 info->mcr &= ~UART_MCR_DTR;
903 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
904 firm_set_rts(port, info->mcr & UART_MCR_RTS);
909 static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
910 unsigned int cmd, unsigned long arg)
912 struct usb_serial_port *port = tty->driver_data;
913 struct serial_struct serstruct;
914 void __user *user_arg = (void __user *)arg;
916 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
920 memset(&serstruct, 0, sizeof(serstruct));
921 serstruct.type = PORT_16654;
922 serstruct.line = port->serial->minor;
923 serstruct.port = port->number;
924 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
925 serstruct.xmit_fifo_size = port->bulk_out_size;
926 serstruct.custom_divisor = 0;
927 serstruct.baud_base = 460800;
928 serstruct.close_delay = CLOSING_DELAY;
929 serstruct.closing_wait = CLOSING_DELAY;
931 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
942 static void whiteheat_set_termios(struct tty_struct *tty,
943 struct usb_serial_port *port, struct ktermios *old_termios)
945 firm_setup_port(tty);
948 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
950 struct usb_serial_port *port = tty->driver_data;
951 firm_set_break(port, break_state);
955 static int whiteheat_chars_in_buffer(struct tty_struct *tty)
957 struct usb_serial_port *port = tty->driver_data;
958 struct whiteheat_private *info = usb_get_serial_port_data(port);
959 struct list_head *tmp;
960 struct whiteheat_urb_wrap *wrap;
964 dbg("%s - port %d", __func__, port->number);
966 spin_lock_irqsave(&info->lock, flags);
967 list_for_each(tmp, &info->tx_urbs_submitted) {
968 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
969 chars += wrap->urb->transfer_buffer_length;
971 spin_unlock_irqrestore(&info->lock, flags);
973 dbg("%s - returns %d", __func__, chars);
978 static void whiteheat_throttle(struct tty_struct *tty)
980 struct usb_serial_port *port = tty->driver_data;
981 struct whiteheat_private *info = usb_get_serial_port_data(port);
984 dbg("%s - port %d", __func__, port->number);
986 spin_lock_irqsave(&info->lock, flags);
987 info->flags |= THROTTLED;
988 spin_unlock_irqrestore(&info->lock, flags);
994 static void whiteheat_unthrottle(struct tty_struct *tty)
996 struct usb_serial_port *port = tty->driver_data;
997 struct whiteheat_private *info = usb_get_serial_port_data(port);
998 int actually_throttled;
1001 dbg("%s - port %d", __func__, port->number);
1003 spin_lock_irqsave(&info->lock, flags);
1004 actually_throttled = info->flags & ACTUALLY_THROTTLED;
1005 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
1006 spin_unlock_irqrestore(&info->lock, flags);
1008 if (actually_throttled)
1009 rx_data_softint(&info->rx_work);
1015 /*****************************************************************************
1016 * Connect Tech's White Heat callback routines
1017 *****************************************************************************/
1018 static void command_port_write_callback(struct urb *urb)
1020 int status = urb->status;
1022 dbg("%s", __func__);
1025 dbg("nonzero urb status: %d", status);
1031 static void command_port_read_callback(struct urb *urb)
1033 struct usb_serial_port *command_port = urb->context;
1034 struct whiteheat_command_private *command_info;
1035 int status = urb->status;
1036 unsigned char *data = urb->transfer_buffer;
1039 dbg("%s", __func__);
1041 command_info = usb_get_serial_port_data(command_port);
1042 if (!command_info) {
1043 dbg("%s - command_info is NULL, exiting.", __func__);
1047 dbg("%s - nonzero urb status: %d", __func__, status);
1048 if (status != -ENOENT)
1049 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1050 wake_up(&command_info->wait_command);
1054 usb_serial_debug_data(debug, &command_port->dev,
1055 __func__, urb->actual_length, data);
1057 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1058 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1059 wake_up(&command_info->wait_command);
1060 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1061 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1062 wake_up(&command_info->wait_command);
1063 } else if (data[0] == WHITEHEAT_EVENT) {
1064 /* These are unsolicited reports from the firmware, hence no
1065 waiting command to wakeup */
1066 dbg("%s - event received", __func__);
1067 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
1068 memcpy(command_info->result_buffer, &data[1],
1069 urb->actual_length - 1);
1070 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1071 wake_up(&command_info->wait_command);
1073 dbg("%s - bad reply from firmware", __func__);
1075 /* Continue trying to always read */
1076 command_port->read_urb->dev = command_port->serial->dev;
1077 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
1079 dbg("%s - failed resubmitting read urb, error %d",
1084 static void whiteheat_read_callback(struct urb *urb)
1086 struct usb_serial_port *port = urb->context;
1087 struct whiteheat_urb_wrap *wrap;
1088 unsigned char *data = urb->transfer_buffer;
1089 struct whiteheat_private *info = usb_get_serial_port_data(port);
1090 int status = urb->status;
1092 dbg("%s - port %d", __func__, port->number);
1094 spin_lock(&info->lock);
1095 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1097 spin_unlock(&info->lock);
1098 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1101 list_del(&wrap->list);
1102 spin_unlock(&info->lock);
1105 dbg("%s - nonzero read bulk status received: %d",
1107 spin_lock(&info->lock);
1108 list_add(&wrap->list, &info->rx_urbs_free);
1109 spin_unlock(&info->lock);
1113 usb_serial_debug_data(debug, &port->dev,
1114 __func__, urb->actual_length, data);
1116 spin_lock(&info->lock);
1117 list_add_tail(&wrap->list, &info->rx_urb_q);
1118 if (info->flags & THROTTLED) {
1119 info->flags |= ACTUALLY_THROTTLED;
1120 spin_unlock(&info->lock);
1123 spin_unlock(&info->lock);
1125 schedule_work(&info->rx_work);
1129 static void whiteheat_write_callback(struct urb *urb)
1131 struct usb_serial_port *port = urb->context;
1132 struct whiteheat_private *info = usb_get_serial_port_data(port);
1133 struct whiteheat_urb_wrap *wrap;
1134 int status = urb->status;
1136 dbg("%s - port %d", __func__, port->number);
1138 spin_lock(&info->lock);
1139 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1141 spin_unlock(&info->lock);
1142 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1145 list_move(&wrap->list, &info->tx_urbs_free);
1146 spin_unlock(&info->lock);
1149 dbg("%s - nonzero write bulk status received: %d",
1154 usb_serial_port_softint(port);
1158 /*****************************************************************************
1159 * Connect Tech's White Heat firmware interface
1160 *****************************************************************************/
1161 static int firm_send_command(struct usb_serial_port *port, __u8 command,
1162 __u8 *data, __u8 datasize)
1164 struct usb_serial_port *command_port;
1165 struct whiteheat_command_private *command_info;
1166 struct whiteheat_private *info;
1167 __u8 *transfer_buffer;
1171 dbg("%s - command %d", __func__, command);
1173 command_port = port->serial->port[COMMAND_PORT];
1174 command_info = usb_get_serial_port_data(command_port);
1175 mutex_lock(&command_info->mutex);
1176 command_info->command_finished = false;
1178 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1179 transfer_buffer[0] = command;
1180 memcpy(&transfer_buffer[1], data, datasize);
1181 command_port->write_urb->transfer_buffer_length = datasize + 1;
1182 command_port->write_urb->dev = port->serial->dev;
1183 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1185 dbg("%s - submit urb failed", __func__);
1189 /* wait for the command to complete */
1190 t = wait_event_timeout(command_info->wait_command,
1191 (bool)command_info->command_finished, COMMAND_TIMEOUT);
1193 usb_kill_urb(command_port->write_urb);
1195 if (command_info->command_finished == false) {
1196 dbg("%s - command timed out.", __func__);
1197 retval = -ETIMEDOUT;
1201 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1202 dbg("%s - command failed.", __func__);
1207 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1208 dbg("%s - command completed.", __func__);
1210 case WHITEHEAT_GET_DTR_RTS:
1211 info = usb_get_serial_port_data(port);
1212 memcpy(&info->mcr, command_info->result_buffer,
1213 sizeof(struct whiteheat_dr_info));
1218 mutex_unlock(&command_info->mutex);
1223 static int firm_open(struct usb_serial_port *port)
1225 struct whiteheat_simple open_command;
1227 open_command.port = port->number - port->serial->minor + 1;
1228 return firm_send_command(port, WHITEHEAT_OPEN,
1229 (__u8 *)&open_command, sizeof(open_command));
1233 static int firm_close(struct usb_serial_port *port)
1235 struct whiteheat_simple close_command;
1237 close_command.port = port->number - port->serial->minor + 1;
1238 return firm_send_command(port, WHITEHEAT_CLOSE,
1239 (__u8 *)&close_command, sizeof(close_command));
1243 static int firm_setup_port(struct tty_struct *tty)
1245 struct usb_serial_port *port = tty->driver_data;
1246 struct whiteheat_port_settings port_settings;
1247 unsigned int cflag = tty->termios->c_cflag;
1249 port_settings.port = port->number + 1;
1251 /* get the byte size */
1252 switch (cflag & CSIZE) {
1253 case CS5: port_settings.bits = 5; break;
1254 case CS6: port_settings.bits = 6; break;
1255 case CS7: port_settings.bits = 7; break;
1257 case CS8: port_settings.bits = 8; break;
1259 dbg("%s - data bits = %d", __func__, port_settings.bits);
1261 /* determine the parity */
1265 port_settings.parity = WHITEHEAT_PAR_MARK;
1267 port_settings.parity = WHITEHEAT_PAR_SPACE;
1270 port_settings.parity = WHITEHEAT_PAR_ODD;
1272 port_settings.parity = WHITEHEAT_PAR_EVEN;
1274 port_settings.parity = WHITEHEAT_PAR_NONE;
1275 dbg("%s - parity = %c", __func__, port_settings.parity);
1277 /* figure out the stop bits requested */
1279 port_settings.stop = 2;
1281 port_settings.stop = 1;
1282 dbg("%s - stop bits = %d", __func__, port_settings.stop);
1284 /* figure out the flow control settings */
1285 if (cflag & CRTSCTS)
1286 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1287 WHITEHEAT_HFLOW_RTS);
1289 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1290 dbg("%s - hardware flow control = %s %s %s %s", __func__,
1291 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1292 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1293 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1294 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1296 /* determine software flow control */
1298 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1300 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1301 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
1303 port_settings.xon = START_CHAR(tty);
1304 port_settings.xoff = STOP_CHAR(tty);
1305 dbg("%s - XON = %2x, XOFF = %2x",
1306 __func__, port_settings.xon, port_settings.xoff);
1308 /* get the baud rate wanted */
1309 port_settings.baud = tty_get_baud_rate(tty);
1310 dbg("%s - baud rate = %d", __func__, port_settings.baud);
1312 /* fixme: should set validated settings */
1313 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1314 /* handle any settings that aren't specified in the tty structure */
1315 port_settings.lloop = 0;
1317 /* now send the message to the device */
1318 return firm_send_command(port, WHITEHEAT_SETUP_PORT,
1319 (__u8 *)&port_settings, sizeof(port_settings));
1323 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1325 struct whiteheat_set_rdb rts_command;
1327 rts_command.port = port->number - port->serial->minor + 1;
1328 rts_command.state = onoff;
1329 return firm_send_command(port, WHITEHEAT_SET_RTS,
1330 (__u8 *)&rts_command, sizeof(rts_command));
1334 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1336 struct whiteheat_set_rdb dtr_command;
1338 dtr_command.port = port->number - port->serial->minor + 1;
1339 dtr_command.state = onoff;
1340 return firm_send_command(port, WHITEHEAT_SET_DTR,
1341 (__u8 *)&dtr_command, sizeof(dtr_command));
1345 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1347 struct whiteheat_set_rdb break_command;
1349 break_command.port = port->number - port->serial->minor + 1;
1350 break_command.state = onoff;
1351 return firm_send_command(port, WHITEHEAT_SET_BREAK,
1352 (__u8 *)&break_command, sizeof(break_command));
1356 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1358 struct whiteheat_purge purge_command;
1360 purge_command.port = port->number - port->serial->minor + 1;
1361 purge_command.what = rxtx;
1362 return firm_send_command(port, WHITEHEAT_PURGE,
1363 (__u8 *)&purge_command, sizeof(purge_command));
1367 static int firm_get_dtr_rts(struct usb_serial_port *port)
1369 struct whiteheat_simple get_dr_command;
1371 get_dr_command.port = port->number - port->serial->minor + 1;
1372 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1373 (__u8 *)&get_dr_command, sizeof(get_dr_command));
1377 static int firm_report_tx_done(struct usb_serial_port *port)
1379 struct whiteheat_simple close_command;
1381 close_command.port = port->number - port->serial->minor + 1;
1382 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1383 (__u8 *)&close_command, sizeof(close_command));
1387 /*****************************************************************************
1388 * Connect Tech's White Heat utility functions
1389 *****************************************************************************/
1390 static int start_command_port(struct usb_serial *serial)
1392 struct usb_serial_port *command_port;
1393 struct whiteheat_command_private *command_info;
1396 command_port = serial->port[COMMAND_PORT];
1397 command_info = usb_get_serial_port_data(command_port);
1398 mutex_lock(&command_info->mutex);
1399 if (!command_info->port_running) {
1400 /* Work around HCD bugs */
1401 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1403 command_port->read_urb->dev = serial->dev;
1404 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1406 dev_err(&serial->dev->dev,
1407 "%s - failed submitting read urb, error %d\n",
1412 command_info->port_running++;
1415 mutex_unlock(&command_info->mutex);
1420 static void stop_command_port(struct usb_serial *serial)
1422 struct usb_serial_port *command_port;
1423 struct whiteheat_command_private *command_info;
1425 command_port = serial->port[COMMAND_PORT];
1426 command_info = usb_get_serial_port_data(command_port);
1427 mutex_lock(&command_info->mutex);
1428 command_info->port_running--;
1429 if (!command_info->port_running)
1430 usb_kill_urb(command_port->read_urb);
1431 mutex_unlock(&command_info->mutex);
1435 static int start_port_read(struct usb_serial_port *port)
1437 struct whiteheat_private *info = usb_get_serial_port_data(port);
1438 struct whiteheat_urb_wrap *wrap;
1441 unsigned long flags;
1442 struct list_head *tmp;
1443 struct list_head *tmp2;
1445 spin_lock_irqsave(&info->lock, flags);
1447 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1449 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1451 urb->dev = port->serial->dev;
1452 spin_unlock_irqrestore(&info->lock, flags);
1453 retval = usb_submit_urb(urb, GFP_KERNEL);
1455 spin_lock_irqsave(&info->lock, flags);
1456 list_add(tmp, &info->rx_urbs_free);
1457 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1458 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1461 spin_unlock_irqrestore(&info->lock, flags);
1463 spin_lock_irqsave(&info->lock, flags);
1464 list_add(tmp, &info->rx_urbs_free);
1468 spin_lock_irqsave(&info->lock, flags);
1469 list_add(tmp, &info->rx_urbs_submitted);
1472 spin_unlock_irqrestore(&info->lock, flags);
1478 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1479 struct list_head *head)
1481 struct whiteheat_urb_wrap *wrap;
1482 struct list_head *tmp;
1484 list_for_each(tmp, head) {
1485 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1486 if (wrap->urb == urb)
1494 static struct list_head *list_first(struct list_head *head)
1500 static void rx_data_softint(struct work_struct *work)
1502 struct whiteheat_private *info =
1503 container_of(work, struct whiteheat_private, rx_work);
1504 struct usb_serial_port *port = info->port;
1505 struct tty_struct *tty = tty_port_tty_get(&port->port);
1506 struct whiteheat_urb_wrap *wrap;
1508 unsigned long flags;
1509 struct list_head *tmp;
1510 struct list_head *tmp2;
1514 spin_lock_irqsave(&info->lock, flags);
1515 if (info->flags & THROTTLED) {
1516 spin_unlock_irqrestore(&info->lock, flags);
1520 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1522 spin_unlock_irqrestore(&info->lock, flags);
1524 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1527 if (tty && urb->actual_length) {
1528 int len = tty_buffer_request_room(tty,
1529 urb->actual_length);
1530 /* This stuff can go away now I suspect */
1531 if (unlikely(len < urb->actual_length)) {
1532 spin_lock_irqsave(&info->lock, flags);
1533 list_add(tmp, &info->rx_urb_q);
1534 spin_unlock_irqrestore(&info->lock, flags);
1535 tty_flip_buffer_push(tty);
1536 schedule_work(&info->rx_work);
1539 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1543 urb->dev = port->serial->dev;
1544 result = usb_submit_urb(urb, GFP_ATOMIC);
1547 "%s - failed resubmitting read urb, error %d\n",
1549 spin_lock_irqsave(&info->lock, flags);
1550 list_add(tmp, &info->rx_urbs_free);
1554 spin_lock_irqsave(&info->lock, flags);
1555 list_add(tmp, &info->rx_urbs_submitted);
1557 spin_unlock_irqrestore(&info->lock, flags);
1560 tty_flip_buffer_push(tty);
1566 /*****************************************************************************
1567 * Connect Tech's White Heat module functions
1568 *****************************************************************************/
1569 static int __init whiteheat_init(void)
1572 retval = usb_serial_register(&whiteheat_fake_device);
1574 goto failed_fake_register;
1575 retval = usb_serial_register(&whiteheat_device);
1577 goto failed_device_register;
1578 retval = usb_register(&whiteheat_driver);
1580 goto failed_usb_register;
1581 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1584 failed_usb_register:
1585 usb_serial_deregister(&whiteheat_device);
1586 failed_device_register:
1587 usb_serial_deregister(&whiteheat_fake_device);
1588 failed_fake_register:
1593 static void __exit whiteheat_exit(void)
1595 usb_deregister(&whiteheat_driver);
1596 usb_serial_deregister(&whiteheat_fake_device);
1597 usb_serial_deregister(&whiteheat_device);
1601 module_init(whiteheat_init);
1602 module_exit(whiteheat_exit);
1604 MODULE_AUTHOR(DRIVER_AUTHOR);
1605 MODULE_DESCRIPTION(DRIVER_DESC);
1606 MODULE_LICENSE("GPL");
1608 MODULE_FIRMWARE("whiteheat.fw");
1609 MODULE_FIRMWARE("whiteheat_loader.fw");
1611 module_param(urb_pool_size, int, 0);
1612 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1614 module_param(debug, bool, S_IRUGO | S_IWUSR);
1615 MODULE_PARM_DESC(debug, "Debug enabled or not");