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 const struct usb_device_id id_table_std[] = {
115 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
116 { } /* Terminating entry */
119 static const 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 const 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_probe(struct usb_serial *serial,
147 const struct usb_device_id *id);
148 static int whiteheat_attach(struct usb_serial *serial);
149 static void whiteheat_release(struct usb_serial *serial);
150 static int whiteheat_open(struct tty_struct *tty,
151 struct usb_serial_port *port);
152 static void whiteheat_close(struct usb_serial_port *port);
153 static int whiteheat_write(struct tty_struct *tty,
154 struct usb_serial_port *port,
155 const unsigned char *buf, int count);
156 static int whiteheat_write_room(struct tty_struct *tty);
157 static int whiteheat_ioctl(struct tty_struct *tty,
158 unsigned int cmd, unsigned long arg);
159 static void whiteheat_set_termios(struct tty_struct *tty,
160 struct usb_serial_port *port, struct ktermios *old);
161 static int whiteheat_tiocmget(struct tty_struct *tty);
162 static int whiteheat_tiocmset(struct tty_struct *tty,
163 unsigned int set, unsigned int clear);
164 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
165 static int whiteheat_chars_in_buffer(struct tty_struct *tty);
166 static void whiteheat_throttle(struct tty_struct *tty);
167 static void whiteheat_unthrottle(struct tty_struct *tty);
168 static void whiteheat_read_callback(struct urb *urb);
169 static void whiteheat_write_callback(struct urb *urb);
171 static struct usb_serial_driver whiteheat_fake_device = {
173 .owner = THIS_MODULE,
174 .name = "whiteheatnofirm",
176 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
177 .usb_driver = &whiteheat_driver,
178 .id_table = id_table_prerenumeration,
180 .probe = whiteheat_firmware_download,
181 .attach = whiteheat_firmware_attach,
184 static struct usb_serial_driver whiteheat_device = {
186 .owner = THIS_MODULE,
189 .description = "Connect Tech - WhiteHEAT",
190 .usb_driver = &whiteheat_driver,
191 .id_table = id_table_std,
193 .probe = whiteheat_probe,
194 .attach = whiteheat_attach,
195 .release = whiteheat_release,
196 .open = whiteheat_open,
197 .close = whiteheat_close,
198 .write = whiteheat_write,
199 .write_room = whiteheat_write_room,
200 .ioctl = whiteheat_ioctl,
201 .set_termios = whiteheat_set_termios,
202 .break_ctl = whiteheat_break_ctl,
203 .tiocmget = whiteheat_tiocmget,
204 .tiocmset = whiteheat_tiocmset,
205 .chars_in_buffer = whiteheat_chars_in_buffer,
206 .throttle = whiteheat_throttle,
207 .unthrottle = whiteheat_unthrottle,
208 .read_bulk_callback = whiteheat_read_callback,
209 .write_bulk_callback = whiteheat_write_callback,
213 struct whiteheat_command_private {
216 __u8 command_finished;
217 wait_queue_head_t wait_command; /* for handling sleeping whilst
218 waiting for a command to
220 __u8 result_buffer[64];
224 #define THROTTLED 0x01
225 #define ACTUALLY_THROTTLED 0x02
227 static int urb_pool_size = 8;
229 struct whiteheat_urb_wrap {
230 struct list_head list;
234 struct whiteheat_private {
237 __u8 mcr; /* FIXME: no locking on mcr */
238 struct list_head rx_urbs_free;
239 struct list_head rx_urbs_submitted;
240 struct list_head rx_urb_q;
241 struct work_struct rx_work;
242 struct usb_serial_port *port;
243 struct list_head tx_urbs_free;
244 struct list_head tx_urbs_submitted;
245 struct mutex deathwarrant;
249 /* local function prototypes */
250 static int start_command_port(struct usb_serial *serial);
251 static void stop_command_port(struct usb_serial *serial);
252 static void command_port_write_callback(struct urb *urb);
253 static void command_port_read_callback(struct urb *urb);
255 static int start_port_read(struct usb_serial_port *port);
256 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
257 struct list_head *head);
258 static struct list_head *list_first(struct list_head *head);
259 static void rx_data_softint(struct work_struct *work);
261 static int firm_send_command(struct usb_serial_port *port, __u8 command,
262 __u8 *data, __u8 datasize);
263 static int firm_open(struct usb_serial_port *port);
264 static int firm_close(struct usb_serial_port *port);
265 static void firm_setup_port(struct tty_struct *tty);
266 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
267 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
268 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
269 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
270 static int firm_get_dtr_rts(struct usb_serial_port *port);
271 static int firm_report_tx_done(struct usb_serial_port *port);
274 #define COMMAND_PORT 4
275 #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
276 #define COMMAND_TIMEOUT_MS 2000
277 #define CLOSING_DELAY (30 * HZ)
280 /*****************************************************************************
281 * Connect Tech's White Heat prerenumeration driver functions
282 *****************************************************************************/
284 /* steps to download the firmware to the WhiteHEAT device:
285 - hold the reset (by writing to the reset bit of the CPUCS register)
286 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
287 - release the reset (by writing to the CPUCS register)
288 - download the WH.HEX file for all addresses greater than 0x1b3f using
289 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
291 - download the WH.HEX file for all addresses less than 0x1b40 using
292 VENDOR_REQUEST_ANCHOR_LOAD
294 - device renumerated itself and comes up as new device id with all
295 firmware download completed.
297 static int whiteheat_firmware_download(struct usb_serial *serial,
298 const struct usb_device_id *id)
300 int response, ret = -ENOENT;
301 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
302 const struct ihex_binrec *record;
306 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
307 &serial->dev->dev)) {
308 dev_err(&serial->dev->dev,
309 "%s - request \"whiteheat.fw\" failed\n", __func__);
312 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
313 &serial->dev->dev)) {
314 dev_err(&serial->dev->dev,
315 "%s - request \"whiteheat_loader.fw\" failed\n",
320 response = ezusb_set_reset (serial, 1);
322 record = (const struct ihex_binrec *)loader_fw->data;
324 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
325 (unsigned char *)record->data,
326 be16_to_cpu(record->len), 0xa0);
328 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
329 "failed for loader (%d %04X %p %d)\n",
330 __func__, response, be32_to_cpu(record->addr),
331 record->data, be16_to_cpu(record->len));
334 record = ihex_next_binrec(record);
337 response = ezusb_set_reset(serial, 0);
339 record = (const struct ihex_binrec *)firmware_fw->data;
340 while (record && be32_to_cpu(record->addr) < 0x1b40)
341 record = ihex_next_binrec(record);
343 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
344 (unsigned char *)record->data,
345 be16_to_cpu(record->len), 0xa3);
347 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
348 "failed for first firmware step "
349 "(%d %04X %p %d)\n", __func__, response,
350 be32_to_cpu(record->addr), record->data,
351 be16_to_cpu(record->len));
357 response = ezusb_set_reset(serial, 1);
359 record = (const struct ihex_binrec *)firmware_fw->data;
360 while (record && be32_to_cpu(record->addr) < 0x1b40) {
361 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
362 (unsigned char *)record->data,
363 be16_to_cpu(record->len), 0xa0);
365 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
366 "failed for second firmware step "
367 "(%d %04X %p %d)\n", __func__, response,
368 be32_to_cpu(record->addr), record->data,
369 be16_to_cpu(record->len));
375 response = ezusb_set_reset (serial, 0);
377 release_firmware(loader_fw);
378 release_firmware(firmware_fw);
383 static int whiteheat_firmware_attach(struct usb_serial *serial)
385 /* We want this device to fail to have a driver assigned to it */
390 /*****************************************************************************
391 * Connect Tech's White Heat serial driver functions
392 *****************************************************************************/
394 static int whiteheat_probe(struct usb_serial *serial,
395 const struct usb_device_id *id)
397 struct usb_host_interface *iface_desc;
398 struct usb_endpoint_descriptor *endpoint;
399 size_t num_bulk_in = 0;
400 size_t num_bulk_out = 0;
404 iface_desc = serial->interface->cur_altsetting;
406 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
407 endpoint = &iface_desc->endpoint[i].desc;
408 if (usb_endpoint_is_bulk_in(endpoint))
410 if (usb_endpoint_is_bulk_out(endpoint))
414 min_num_bulk = COMMAND_PORT + 1;
415 if (num_bulk_in < min_num_bulk || num_bulk_out < min_num_bulk)
421 static int whiteheat_attach(struct usb_serial *serial)
423 struct usb_serial_port *command_port;
424 struct whiteheat_command_private *command_info;
425 struct usb_serial_port *port;
426 struct whiteheat_private *info;
427 struct whiteheat_hw_info *hw_info;
437 struct whiteheat_urb_wrap *wrap;
438 struct list_head *tmp;
440 command_port = serial->port[COMMAND_PORT];
442 pipe = usb_sndbulkpipe(serial->dev,
443 command_port->bulk_out_endpointAddress);
444 command = kmalloc(2, GFP_KERNEL);
446 goto no_command_buffer;
447 command[0] = WHITEHEAT_GET_HW_INFO;
450 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
452 goto no_result_buffer;
454 * When the module is reloaded the firmware is still there and
455 * the endpoints are still in the usb core unchanged. This is the
456 * unlinking bug in disguise. Same for the call below.
458 usb_clear_halt(serial->dev, pipe);
459 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
460 &alen, COMMAND_TIMEOUT_MS);
462 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
463 serial->type->description, ret);
465 } else if (alen != 2) {
466 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
467 serial->type->description, alen);
471 pipe = usb_rcvbulkpipe(serial->dev,
472 command_port->bulk_in_endpointAddress);
473 /* See the comment on the usb_clear_halt() above */
474 usb_clear_halt(serial->dev, pipe);
475 ret = usb_bulk_msg(serial->dev, pipe, result,
476 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
478 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
479 serial->type->description, ret);
481 } else if (alen != sizeof(*hw_info) + 1) {
482 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
483 serial->type->description, alen);
485 } else if (result[0] != command[0]) {
486 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
487 serial->type->description, result[0]);
491 hw_info = (struct whiteheat_hw_info *)&result[1];
493 dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
494 serial->type->description, DRIVER_VERSION,
495 hw_info->sw_major_rev, hw_info->sw_minor_rev);
497 for (i = 0; i < serial->num_ports; i++) {
498 port = serial->port[i];
500 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
503 "%s: Out of memory for port structures\n",
504 serial->type->description);
508 spin_lock_init(&info->lock);
509 mutex_init(&info->deathwarrant);
512 INIT_WORK(&info->rx_work, rx_data_softint);
515 INIT_LIST_HEAD(&info->rx_urbs_free);
516 INIT_LIST_HEAD(&info->rx_urbs_submitted);
517 INIT_LIST_HEAD(&info->rx_urb_q);
518 INIT_LIST_HEAD(&info->tx_urbs_free);
519 INIT_LIST_HEAD(&info->tx_urbs_submitted);
521 for (j = 0; j < urb_pool_size; j++) {
522 urb = usb_alloc_urb(0, GFP_KERNEL);
524 dev_err(&port->dev, "No free urbs available\n");
527 buf_size = port->read_urb->transfer_buffer_length;
528 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
529 if (!urb->transfer_buffer) {
531 "Couldn't allocate urb buffer\n");
534 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
537 "Couldn't allocate urb wrapper\n");
540 usb_fill_bulk_urb(urb, serial->dev,
541 usb_rcvbulkpipe(serial->dev,
542 port->bulk_in_endpointAddress),
543 urb->transfer_buffer, buf_size,
544 whiteheat_read_callback, port);
546 list_add(&wrap->list, &info->rx_urbs_free);
548 urb = usb_alloc_urb(0, GFP_KERNEL);
550 dev_err(&port->dev, "No free urbs available\n");
553 buf_size = port->write_urb->transfer_buffer_length;
554 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
555 if (!urb->transfer_buffer) {
557 "Couldn't allocate urb buffer\n");
560 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
563 "Couldn't allocate urb wrapper\n");
566 usb_fill_bulk_urb(urb, serial->dev,
567 usb_sndbulkpipe(serial->dev,
568 port->bulk_out_endpointAddress),
569 urb->transfer_buffer, buf_size,
570 whiteheat_write_callback, port);
572 list_add(&wrap->list, &info->tx_urbs_free);
575 usb_set_serial_port_data(port, info);
578 command_info = kmalloc(sizeof(struct whiteheat_command_private),
580 if (command_info == NULL) {
581 dev_err(&serial->dev->dev,
582 "%s: Out of memory for port structures\n",
583 serial->type->description);
584 goto no_command_private;
587 mutex_init(&command_info->mutex);
588 command_info->port_running = 0;
589 init_waitqueue_head(&command_info->wait_command);
590 usb_set_serial_port_data(command_port, command_info);
591 command_port->write_urb->complete = command_port_write_callback;
592 command_port->read_urb->complete = command_port_read_callback;
599 /* Firmware likely not running */
600 dev_err(&serial->dev->dev,
601 "%s: Unable to retrieve firmware version, try replugging\n",
602 serial->type->description);
603 dev_err(&serial->dev->dev,
604 "%s: If the firmware is not running (status led not blinking)\n",
605 serial->type->description);
606 dev_err(&serial->dev->dev,
607 "%s: please contact support@connecttech.com\n",
608 serial->type->description);
614 for (i = serial->num_ports - 1; i >= 0; i--) {
615 port = serial->port[i];
616 info = usb_get_serial_port_data(port);
617 for (j = urb_pool_size - 1; j >= 0; j--) {
618 tmp = list_first(&info->tx_urbs_free);
620 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
624 kfree(urb->transfer_buffer);
628 tmp = list_first(&info->rx_urbs_free);
630 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
634 kfree(urb->transfer_buffer);
652 static void whiteheat_release(struct usb_serial *serial)
654 struct usb_serial_port *command_port;
655 struct usb_serial_port *port;
656 struct whiteheat_private *info;
657 struct whiteheat_urb_wrap *wrap;
659 struct list_head *tmp;
660 struct list_head *tmp2;
665 /* free up our private data for our command port */
666 command_port = serial->port[COMMAND_PORT];
667 kfree(usb_get_serial_port_data(command_port));
669 for (i = 0; i < serial->num_ports; i++) {
670 port = serial->port[i];
671 info = usb_get_serial_port_data(port);
672 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
674 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
677 kfree(urb->transfer_buffer);
680 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
682 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
685 kfree(urb->transfer_buffer);
692 static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
696 dbg("%s - port %d", __func__, port->number);
698 retval = start_command_port(port->serial);
703 tty->low_latency = 1;
705 /* send an open port command */
706 retval = firm_open(port);
708 stop_command_port(port->serial);
712 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
715 stop_command_port(port->serial);
720 firm_setup_port(tty);
722 /* Work around HCD bugs */
723 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
724 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
726 /* Start reading from the device */
727 retval = start_port_read(port);
730 "%s - failed submitting read urb, error %d\n",
733 stop_command_port(port->serial);
738 dbg("%s - exit, retval = %d", __func__, retval);
743 static void whiteheat_close(struct usb_serial_port *port)
745 struct whiteheat_private *info = usb_get_serial_port_data(port);
746 struct whiteheat_urb_wrap *wrap;
748 struct list_head *tmp;
749 struct list_head *tmp2;
751 dbg("%s - port %d", __func__, port->number);
753 firm_report_tx_done(port);
756 /* shutdown our bulk reads and writes */
757 mutex_lock(&info->deathwarrant);
758 spin_lock_irq(&info->lock);
759 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
760 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
763 spin_unlock_irq(&info->lock);
765 spin_lock_irq(&info->lock);
766 list_add(tmp, &info->rx_urbs_free);
768 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
769 list_move(tmp, &info->rx_urbs_free);
770 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
771 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
774 spin_unlock_irq(&info->lock);
776 spin_lock_irq(&info->lock);
777 list_add(tmp, &info->tx_urbs_free);
779 spin_unlock_irq(&info->lock);
780 mutex_unlock(&info->deathwarrant);
781 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)
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,
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,
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);
983 dbg("%s - port %d", __func__, port->number);
985 spin_lock_irq(&info->lock);
986 info->flags |= THROTTLED;
987 spin_unlock_irq(&info->lock);
991 static void whiteheat_unthrottle(struct tty_struct *tty)
993 struct usb_serial_port *port = tty->driver_data;
994 struct whiteheat_private *info = usb_get_serial_port_data(port);
995 int actually_throttled;
997 dbg("%s - port %d", __func__, port->number);
999 spin_lock_irq(&info->lock);
1000 actually_throttled = info->flags & ACTUALLY_THROTTLED;
1001 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
1002 spin_unlock_irq(&info->lock);
1004 if (actually_throttled)
1005 rx_data_softint(&info->rx_work);
1009 /*****************************************************************************
1010 * Connect Tech's White Heat callback routines
1011 *****************************************************************************/
1012 static void command_port_write_callback(struct urb *urb)
1014 int status = urb->status;
1016 dbg("%s", __func__);
1019 dbg("nonzero urb status: %d", status);
1025 static void command_port_read_callback(struct urb *urb)
1027 struct usb_serial_port *command_port = urb->context;
1028 struct whiteheat_command_private *command_info;
1029 int status = urb->status;
1030 unsigned char *data = urb->transfer_buffer;
1033 dbg("%s", __func__);
1035 command_info = usb_get_serial_port_data(command_port);
1036 if (!command_info) {
1037 dbg("%s - command_info is NULL, exiting.", __func__);
1040 if (!urb->actual_length) {
1041 dev_dbg(&urb->dev->dev, "%s - empty response, exiting.\n", __func__);
1045 dbg("%s - nonzero urb status: %d", __func__, status);
1046 if (status != -ENOENT)
1047 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1048 wake_up(&command_info->wait_command);
1052 usb_serial_debug_data(debug, &command_port->dev,
1053 __func__, urb->actual_length, data);
1055 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1056 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1057 wake_up(&command_info->wait_command);
1058 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1059 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1060 wake_up(&command_info->wait_command);
1061 } else if (data[0] == WHITEHEAT_EVENT) {
1062 /* These are unsolicited reports from the firmware, hence no
1063 waiting command to wakeup */
1064 dbg("%s - event received", __func__);
1065 } else if ((data[0] == WHITEHEAT_GET_DTR_RTS) &&
1066 (urb->actual_length - 1 <= sizeof(command_info->result_buffer))) {
1067 memcpy(command_info->result_buffer, &data[1],
1068 urb->actual_length - 1);
1069 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1070 wake_up(&command_info->wait_command);
1072 dbg("%s - bad reply from firmware", __func__);
1074 /* Continue trying to always read */
1075 command_port->read_urb->dev = command_port->serial->dev;
1076 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
1078 dbg("%s - failed resubmitting read urb, error %d",
1083 static void whiteheat_read_callback(struct urb *urb)
1085 struct usb_serial_port *port = urb->context;
1086 struct whiteheat_urb_wrap *wrap;
1087 unsigned char *data = urb->transfer_buffer;
1088 struct whiteheat_private *info = usb_get_serial_port_data(port);
1089 int status = urb->status;
1091 dbg("%s - port %d", __func__, port->number);
1093 spin_lock(&info->lock);
1094 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1096 spin_unlock(&info->lock);
1097 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1100 list_del(&wrap->list);
1101 spin_unlock(&info->lock);
1104 dbg("%s - nonzero read bulk status received: %d",
1106 spin_lock(&info->lock);
1107 list_add(&wrap->list, &info->rx_urbs_free);
1108 spin_unlock(&info->lock);
1112 usb_serial_debug_data(debug, &port->dev,
1113 __func__, urb->actual_length, data);
1115 spin_lock(&info->lock);
1116 list_add_tail(&wrap->list, &info->rx_urb_q);
1117 if (info->flags & THROTTLED) {
1118 info->flags |= ACTUALLY_THROTTLED;
1119 spin_unlock(&info->lock);
1122 spin_unlock(&info->lock);
1124 schedule_work(&info->rx_work);
1128 static void whiteheat_write_callback(struct urb *urb)
1130 struct usb_serial_port *port = urb->context;
1131 struct whiteheat_private *info = usb_get_serial_port_data(port);
1132 struct whiteheat_urb_wrap *wrap;
1133 int status = urb->status;
1135 dbg("%s - port %d", __func__, port->number);
1137 spin_lock(&info->lock);
1138 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1140 spin_unlock(&info->lock);
1141 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1144 list_move(&wrap->list, &info->tx_urbs_free);
1145 spin_unlock(&info->lock);
1148 dbg("%s - nonzero write bulk status received: %d",
1153 usb_serial_port_softint(port);
1157 /*****************************************************************************
1158 * Connect Tech's White Heat firmware interface
1159 *****************************************************************************/
1160 static int firm_send_command(struct usb_serial_port *port, __u8 command,
1161 __u8 *data, __u8 datasize)
1163 struct usb_serial_port *command_port;
1164 struct whiteheat_command_private *command_info;
1165 struct whiteheat_private *info;
1166 __u8 *transfer_buffer;
1170 dbg("%s - command %d", __func__, command);
1172 command_port = port->serial->port[COMMAND_PORT];
1173 command_info = usb_get_serial_port_data(command_port);
1174 mutex_lock(&command_info->mutex);
1175 command_info->command_finished = false;
1177 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1178 transfer_buffer[0] = command;
1179 memcpy(&transfer_buffer[1], data, datasize);
1180 command_port->write_urb->transfer_buffer_length = datasize + 1;
1181 command_port->write_urb->dev = port->serial->dev;
1182 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1184 dbg("%s - submit urb failed", __func__);
1188 /* wait for the command to complete */
1189 t = wait_event_timeout(command_info->wait_command,
1190 (bool)command_info->command_finished, COMMAND_TIMEOUT);
1192 usb_kill_urb(command_port->write_urb);
1194 if (command_info->command_finished == false) {
1195 dbg("%s - command timed out.", __func__);
1196 retval = -ETIMEDOUT;
1200 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1201 dbg("%s - command failed.", __func__);
1206 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1207 dbg("%s - command completed.", __func__);
1209 case WHITEHEAT_GET_DTR_RTS:
1210 info = usb_get_serial_port_data(port);
1211 memcpy(&info->mcr, command_info->result_buffer,
1212 sizeof(struct whiteheat_dr_info));
1217 mutex_unlock(&command_info->mutex);
1222 static int firm_open(struct usb_serial_port *port)
1224 struct whiteheat_simple open_command;
1226 open_command.port = port->number - port->serial->minor + 1;
1227 return firm_send_command(port, WHITEHEAT_OPEN,
1228 (__u8 *)&open_command, sizeof(open_command));
1232 static int firm_close(struct usb_serial_port *port)
1234 struct whiteheat_simple close_command;
1236 close_command.port = port->number - port->serial->minor + 1;
1237 return firm_send_command(port, WHITEHEAT_CLOSE,
1238 (__u8 *)&close_command, sizeof(close_command));
1242 static void firm_setup_port(struct tty_struct *tty)
1244 struct usb_serial_port *port = tty->driver_data;
1245 struct whiteheat_port_settings port_settings;
1246 unsigned int cflag = tty->termios->c_cflag;
1248 port_settings.port = port->number - port->serial->minor + 1;
1250 /* get the byte size */
1251 switch (cflag & CSIZE) {
1252 case CS5: port_settings.bits = 5; break;
1253 case CS6: port_settings.bits = 6; break;
1254 case CS7: port_settings.bits = 7; break;
1256 case CS8: port_settings.bits = 8; break;
1258 dbg("%s - data bits = %d", __func__, port_settings.bits);
1260 /* determine the parity */
1264 port_settings.parity = WHITEHEAT_PAR_MARK;
1266 port_settings.parity = WHITEHEAT_PAR_SPACE;
1269 port_settings.parity = WHITEHEAT_PAR_ODD;
1271 port_settings.parity = WHITEHEAT_PAR_EVEN;
1273 port_settings.parity = WHITEHEAT_PAR_NONE;
1274 dbg("%s - parity = %c", __func__, port_settings.parity);
1276 /* figure out the stop bits requested */
1278 port_settings.stop = 2;
1280 port_settings.stop = 1;
1281 dbg("%s - stop bits = %d", __func__, port_settings.stop);
1283 /* figure out the flow control settings */
1284 if (cflag & CRTSCTS)
1285 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1286 WHITEHEAT_HFLOW_RTS);
1288 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1289 dbg("%s - hardware flow control = %s %s %s %s", __func__,
1290 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1291 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1292 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1293 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1295 /* determine software flow control */
1297 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1299 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1300 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
1302 port_settings.xon = START_CHAR(tty);
1303 port_settings.xoff = STOP_CHAR(tty);
1304 dbg("%s - XON = %2x, XOFF = %2x",
1305 __func__, port_settings.xon, port_settings.xoff);
1307 /* get the baud rate wanted */
1308 port_settings.baud = tty_get_baud_rate(tty);
1309 dbg("%s - baud rate = %d", __func__, port_settings.baud);
1311 /* fixme: should set validated settings */
1312 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1313 /* handle any settings that aren't specified in the tty structure */
1314 port_settings.lloop = 0;
1316 /* now send the message to the device */
1317 firm_send_command(port, WHITEHEAT_SETUP_PORT,
1318 (__u8 *)&port_settings, sizeof(port_settings));
1322 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1324 struct whiteheat_set_rdb rts_command;
1326 rts_command.port = port->number - port->serial->minor + 1;
1327 rts_command.state = onoff;
1328 return firm_send_command(port, WHITEHEAT_SET_RTS,
1329 (__u8 *)&rts_command, sizeof(rts_command));
1333 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1335 struct whiteheat_set_rdb dtr_command;
1337 dtr_command.port = port->number - port->serial->minor + 1;
1338 dtr_command.state = onoff;
1339 return firm_send_command(port, WHITEHEAT_SET_DTR,
1340 (__u8 *)&dtr_command, sizeof(dtr_command));
1344 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1346 struct whiteheat_set_rdb break_command;
1348 break_command.port = port->number - port->serial->minor + 1;
1349 break_command.state = onoff;
1350 return firm_send_command(port, WHITEHEAT_SET_BREAK,
1351 (__u8 *)&break_command, sizeof(break_command));
1355 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1357 struct whiteheat_purge purge_command;
1359 purge_command.port = port->number - port->serial->minor + 1;
1360 purge_command.what = rxtx;
1361 return firm_send_command(port, WHITEHEAT_PURGE,
1362 (__u8 *)&purge_command, sizeof(purge_command));
1366 static int firm_get_dtr_rts(struct usb_serial_port *port)
1368 struct whiteheat_simple get_dr_command;
1370 get_dr_command.port = port->number - port->serial->minor + 1;
1371 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1372 (__u8 *)&get_dr_command, sizeof(get_dr_command));
1376 static int firm_report_tx_done(struct usb_serial_port *port)
1378 struct whiteheat_simple close_command;
1380 close_command.port = port->number - port->serial->minor + 1;
1381 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1382 (__u8 *)&close_command, sizeof(close_command));
1386 /*****************************************************************************
1387 * Connect Tech's White Heat utility functions
1388 *****************************************************************************/
1389 static int start_command_port(struct usb_serial *serial)
1391 struct usb_serial_port *command_port;
1392 struct whiteheat_command_private *command_info;
1395 command_port = serial->port[COMMAND_PORT];
1396 command_info = usb_get_serial_port_data(command_port);
1397 mutex_lock(&command_info->mutex);
1398 if (!command_info->port_running) {
1399 /* Work around HCD bugs */
1400 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1402 command_port->read_urb->dev = serial->dev;
1403 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1405 dev_err(&serial->dev->dev,
1406 "%s - failed submitting read urb, error %d\n",
1411 command_info->port_running++;
1414 mutex_unlock(&command_info->mutex);
1419 static void stop_command_port(struct usb_serial *serial)
1421 struct usb_serial_port *command_port;
1422 struct whiteheat_command_private *command_info;
1424 command_port = serial->port[COMMAND_PORT];
1425 command_info = usb_get_serial_port_data(command_port);
1426 mutex_lock(&command_info->mutex);
1427 command_info->port_running--;
1428 if (!command_info->port_running)
1429 usb_kill_urb(command_port->read_urb);
1430 mutex_unlock(&command_info->mutex);
1434 static int start_port_read(struct usb_serial_port *port)
1436 struct whiteheat_private *info = usb_get_serial_port_data(port);
1437 struct whiteheat_urb_wrap *wrap;
1440 unsigned long flags;
1441 struct list_head *tmp;
1442 struct list_head *tmp2;
1444 spin_lock_irqsave(&info->lock, flags);
1446 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1448 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1450 urb->dev = port->serial->dev;
1451 spin_unlock_irqrestore(&info->lock, flags);
1452 retval = usb_submit_urb(urb, GFP_KERNEL);
1454 spin_lock_irqsave(&info->lock, flags);
1455 list_add(tmp, &info->rx_urbs_free);
1456 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1457 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1460 spin_unlock_irqrestore(&info->lock, flags);
1462 spin_lock_irqsave(&info->lock, flags);
1463 list_add(tmp, &info->rx_urbs_free);
1467 spin_lock_irqsave(&info->lock, flags);
1468 list_add(tmp, &info->rx_urbs_submitted);
1471 spin_unlock_irqrestore(&info->lock, flags);
1477 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1478 struct list_head *head)
1480 struct whiteheat_urb_wrap *wrap;
1481 struct list_head *tmp;
1483 list_for_each(tmp, head) {
1484 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1485 if (wrap->urb == urb)
1493 static struct list_head *list_first(struct list_head *head)
1499 static void rx_data_softint(struct work_struct *work)
1501 struct whiteheat_private *info =
1502 container_of(work, struct whiteheat_private, rx_work);
1503 struct usb_serial_port *port = info->port;
1504 struct tty_struct *tty = tty_port_tty_get(&port->port);
1505 struct whiteheat_urb_wrap *wrap;
1507 unsigned long flags;
1508 struct list_head *tmp;
1509 struct list_head *tmp2;
1513 spin_lock_irqsave(&info->lock, flags);
1514 if (info->flags & THROTTLED) {
1515 spin_unlock_irqrestore(&info->lock, flags);
1519 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1521 spin_unlock_irqrestore(&info->lock, flags);
1523 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1526 if (tty && urb->actual_length)
1527 sent += tty_insert_flip_string(tty,
1528 urb->transfer_buffer, urb->actual_length);
1530 urb->dev = port->serial->dev;
1531 result = usb_submit_urb(urb, GFP_ATOMIC);
1534 "%s - failed resubmitting read urb, error %d\n",
1536 spin_lock_irqsave(&info->lock, flags);
1537 list_add(tmp, &info->rx_urbs_free);
1541 spin_lock_irqsave(&info->lock, flags);
1542 list_add(tmp, &info->rx_urbs_submitted);
1544 spin_unlock_irqrestore(&info->lock, flags);
1547 tty_flip_buffer_push(tty);
1553 /*****************************************************************************
1554 * Connect Tech's White Heat module functions
1555 *****************************************************************************/
1556 static int __init whiteheat_init(void)
1559 retval = usb_serial_register(&whiteheat_fake_device);
1561 goto failed_fake_register;
1562 retval = usb_serial_register(&whiteheat_device);
1564 goto failed_device_register;
1565 retval = usb_register(&whiteheat_driver);
1567 goto failed_usb_register;
1568 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1571 failed_usb_register:
1572 usb_serial_deregister(&whiteheat_device);
1573 failed_device_register:
1574 usb_serial_deregister(&whiteheat_fake_device);
1575 failed_fake_register:
1580 static void __exit whiteheat_exit(void)
1582 usb_deregister(&whiteheat_driver);
1583 usb_serial_deregister(&whiteheat_fake_device);
1584 usb_serial_deregister(&whiteheat_device);
1588 module_init(whiteheat_init);
1589 module_exit(whiteheat_exit);
1591 MODULE_AUTHOR(DRIVER_AUTHOR);
1592 MODULE_DESCRIPTION(DRIVER_DESC);
1593 MODULE_LICENSE("GPL");
1595 MODULE_FIRMWARE("whiteheat.fw");
1596 MODULE_FIRMWARE("whiteheat_loader.fw");
1598 module_param(urb_pool_size, int, 0);
1599 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1601 module_param(debug, bool, S_IRUGO | S_IWUSR);
1602 MODULE_PARM_DESC(debug, "Debug enabled or not");