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
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/tty.h>
24 #include <linux/tty_driver.h>
25 #include <linux/tty_flip.h>
26 #include <linux/module.h>
27 #include <linux/spinlock.h>
28 #include <linux/mutex.h>
29 #include <linux/uaccess.h>
30 #include <asm/termbits.h>
31 #include <linux/usb.h>
32 #include <linux/serial_reg.h>
33 #include <linux/serial.h>
34 #include <linux/usb/serial.h>
35 #include <linux/firmware.h>
36 #include <linux/ihex.h>
37 #include "whiteheat.h" /* WhiteHEAT specific commands */
48 #define DRIVER_VERSION "v2.0"
49 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
50 #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
52 #define CONNECT_TECH_VENDOR_ID 0x0710
53 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
54 #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
57 ID tables for whiteheat are unusual, because we want to different
58 things for different versions of the device. Eventually, this
59 will be doable from a single table. But, for now, we define two
60 separate ID tables, and then a third table that combines them
61 just for the purpose of exporting the autoloading information.
63 static const struct usb_device_id id_table_std[] = {
64 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
65 { } /* Terminating entry */
68 static const struct usb_device_id id_table_prerenumeration[] = {
69 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
70 { } /* Terminating entry */
73 static const struct usb_device_id id_table_combined[] = {
74 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
75 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
76 { } /* Terminating entry */
79 MODULE_DEVICE_TABLE(usb, id_table_combined);
81 static struct usb_driver whiteheat_driver = {
83 .probe = usb_serial_probe,
84 .disconnect = usb_serial_disconnect,
85 .id_table = id_table_combined,
89 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
90 static int whiteheat_firmware_download(struct usb_serial *serial,
91 const struct usb_device_id *id);
92 static int whiteheat_firmware_attach(struct usb_serial *serial);
94 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
95 static int whiteheat_attach(struct usb_serial *serial);
96 static void whiteheat_release(struct usb_serial *serial);
97 static int whiteheat_open(struct tty_struct *tty,
98 struct usb_serial_port *port);
99 static void whiteheat_close(struct usb_serial_port *port);
100 static int whiteheat_write(struct tty_struct *tty,
101 struct usb_serial_port *port,
102 const unsigned char *buf, int count);
103 static int whiteheat_write_room(struct tty_struct *tty);
104 static int whiteheat_ioctl(struct tty_struct *tty,
105 unsigned int cmd, unsigned long arg);
106 static void whiteheat_set_termios(struct tty_struct *tty,
107 struct usb_serial_port *port, struct ktermios *old);
108 static int whiteheat_tiocmget(struct tty_struct *tty);
109 static int whiteheat_tiocmset(struct tty_struct *tty,
110 unsigned int set, unsigned int clear);
111 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
112 static int whiteheat_chars_in_buffer(struct tty_struct *tty);
113 static void whiteheat_throttle(struct tty_struct *tty);
114 static void whiteheat_unthrottle(struct tty_struct *tty);
115 static void whiteheat_read_callback(struct urb *urb);
116 static void whiteheat_write_callback(struct urb *urb);
118 static struct usb_serial_driver whiteheat_fake_device = {
120 .owner = THIS_MODULE,
121 .name = "whiteheatnofirm",
123 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
124 .usb_driver = &whiteheat_driver,
125 .id_table = id_table_prerenumeration,
127 .probe = whiteheat_firmware_download,
128 .attach = whiteheat_firmware_attach,
131 static struct usb_serial_driver whiteheat_device = {
133 .owner = THIS_MODULE,
136 .description = "Connect Tech - WhiteHEAT",
137 .usb_driver = &whiteheat_driver,
138 .id_table = id_table_std,
140 .attach = whiteheat_attach,
141 .release = whiteheat_release,
142 .open = whiteheat_open,
143 .close = whiteheat_close,
144 .write = whiteheat_write,
145 .write_room = whiteheat_write_room,
146 .ioctl = whiteheat_ioctl,
147 .set_termios = whiteheat_set_termios,
148 .break_ctl = whiteheat_break_ctl,
149 .tiocmget = whiteheat_tiocmget,
150 .tiocmset = whiteheat_tiocmset,
151 .chars_in_buffer = whiteheat_chars_in_buffer,
152 .throttle = whiteheat_throttle,
153 .unthrottle = whiteheat_unthrottle,
154 .read_bulk_callback = whiteheat_read_callback,
155 .write_bulk_callback = whiteheat_write_callback,
159 struct whiteheat_command_private {
162 __u8 command_finished;
163 wait_queue_head_t wait_command; /* for handling sleeping whilst
164 waiting for a command to
166 __u8 result_buffer[64];
170 #define THROTTLED 0x01
171 #define ACTUALLY_THROTTLED 0x02
173 static int urb_pool_size = 8;
175 struct whiteheat_urb_wrap {
176 struct list_head list;
180 struct whiteheat_private {
183 __u8 mcr; /* FIXME: no locking on mcr */
184 struct list_head rx_urbs_free;
185 struct list_head rx_urbs_submitted;
186 struct list_head rx_urb_q;
187 struct work_struct rx_work;
188 struct usb_serial_port *port;
189 struct list_head tx_urbs_free;
190 struct list_head tx_urbs_submitted;
191 struct mutex deathwarrant;
195 /* local function prototypes */
196 static int start_command_port(struct usb_serial *serial);
197 static void stop_command_port(struct usb_serial *serial);
198 static void command_port_write_callback(struct urb *urb);
199 static void command_port_read_callback(struct urb *urb);
201 static int start_port_read(struct usb_serial_port *port);
202 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
203 struct list_head *head);
204 static struct list_head *list_first(struct list_head *head);
205 static void rx_data_softint(struct work_struct *work);
207 static int firm_send_command(struct usb_serial_port *port, __u8 command,
208 __u8 *data, __u8 datasize);
209 static int firm_open(struct usb_serial_port *port);
210 static int firm_close(struct usb_serial_port *port);
211 static void firm_setup_port(struct tty_struct *tty);
212 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
213 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
214 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
215 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
216 static int firm_get_dtr_rts(struct usb_serial_port *port);
217 static int firm_report_tx_done(struct usb_serial_port *port);
220 #define COMMAND_PORT 4
221 #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
222 #define COMMAND_TIMEOUT_MS 2000
223 #define CLOSING_DELAY (30 * HZ)
226 /*****************************************************************************
227 * Connect Tech's White Heat prerenumeration driver functions
228 *****************************************************************************/
230 /* steps to download the firmware to the WhiteHEAT device:
231 - hold the reset (by writing to the reset bit of the CPUCS register)
232 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
233 - release the reset (by writing to the CPUCS register)
234 - download the WH.HEX file for all addresses greater than 0x1b3f using
235 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
237 - download the WH.HEX file for all addresses less than 0x1b40 using
238 VENDOR_REQUEST_ANCHOR_LOAD
240 - device renumerated itself and comes up as new device id with all
241 firmware download completed.
243 static int whiteheat_firmware_download(struct usb_serial *serial,
244 const struct usb_device_id *id)
246 int response, ret = -ENOENT;
247 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
248 const struct ihex_binrec *record;
252 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
253 &serial->dev->dev)) {
254 dev_err(&serial->dev->dev,
255 "%s - request \"whiteheat.fw\" failed\n", __func__);
258 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
259 &serial->dev->dev)) {
260 dev_err(&serial->dev->dev,
261 "%s - request \"whiteheat_loader.fw\" failed\n",
266 response = ezusb_set_reset (serial, 1);
268 record = (const struct ihex_binrec *)loader_fw->data;
270 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
271 (unsigned char *)record->data,
272 be16_to_cpu(record->len), 0xa0);
274 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
275 "failed for loader (%d %04X %p %d)\n",
276 __func__, response, be32_to_cpu(record->addr),
277 record->data, be16_to_cpu(record->len));
280 record = ihex_next_binrec(record);
283 response = ezusb_set_reset(serial, 0);
285 record = (const struct ihex_binrec *)firmware_fw->data;
286 while (record && be32_to_cpu(record->addr) < 0x1b40)
287 record = ihex_next_binrec(record);
289 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
290 (unsigned char *)record->data,
291 be16_to_cpu(record->len), 0xa3);
293 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
294 "failed for first firmware step "
295 "(%d %04X %p %d)\n", __func__, response,
296 be32_to_cpu(record->addr), record->data,
297 be16_to_cpu(record->len));
303 response = ezusb_set_reset(serial, 1);
305 record = (const struct ihex_binrec *)firmware_fw->data;
306 while (record && be32_to_cpu(record->addr) < 0x1b40) {
307 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
308 (unsigned char *)record->data,
309 be16_to_cpu(record->len), 0xa0);
311 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
312 "failed for second firmware step "
313 "(%d %04X %p %d)\n", __func__, response,
314 be32_to_cpu(record->addr), record->data,
315 be16_to_cpu(record->len));
321 response = ezusb_set_reset (serial, 0);
323 release_firmware(loader_fw);
324 release_firmware(firmware_fw);
329 static int whiteheat_firmware_attach(struct usb_serial *serial)
331 /* We want this device to fail to have a driver assigned to it */
336 /*****************************************************************************
337 * Connect Tech's White Heat serial driver functions
338 *****************************************************************************/
339 static int whiteheat_attach(struct usb_serial *serial)
341 struct usb_serial_port *command_port;
342 struct whiteheat_command_private *command_info;
343 struct usb_serial_port *port;
344 struct whiteheat_private *info;
345 struct whiteheat_hw_info *hw_info;
355 struct whiteheat_urb_wrap *wrap;
356 struct list_head *tmp;
358 command_port = serial->port[COMMAND_PORT];
360 pipe = usb_sndbulkpipe(serial->dev,
361 command_port->bulk_out_endpointAddress);
362 command = kmalloc(2, GFP_KERNEL);
364 goto no_command_buffer;
365 command[0] = WHITEHEAT_GET_HW_INFO;
368 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
370 goto no_result_buffer;
372 * When the module is reloaded the firmware is still there and
373 * the endpoints are still in the usb core unchanged. This is the
374 * unlinking bug in disguise. Same for the call below.
376 usb_clear_halt(serial->dev, pipe);
377 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
378 &alen, COMMAND_TIMEOUT_MS);
380 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
381 serial->type->description, ret);
383 } else if (alen != 2) {
384 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
385 serial->type->description, alen);
389 pipe = usb_rcvbulkpipe(serial->dev,
390 command_port->bulk_in_endpointAddress);
391 /* See the comment on the usb_clear_halt() above */
392 usb_clear_halt(serial->dev, pipe);
393 ret = usb_bulk_msg(serial->dev, pipe, result,
394 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
396 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
397 serial->type->description, ret);
399 } else if (alen != sizeof(*hw_info) + 1) {
400 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
401 serial->type->description, alen);
403 } else if (result[0] != command[0]) {
404 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
405 serial->type->description, result[0]);
409 hw_info = (struct whiteheat_hw_info *)&result[1];
411 dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
412 serial->type->description, DRIVER_VERSION,
413 hw_info->sw_major_rev, hw_info->sw_minor_rev);
415 for (i = 0; i < serial->num_ports; i++) {
416 port = serial->port[i];
418 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
421 "%s: Out of memory for port structures\n",
422 serial->type->description);
426 spin_lock_init(&info->lock);
427 mutex_init(&info->deathwarrant);
430 INIT_WORK(&info->rx_work, rx_data_softint);
433 INIT_LIST_HEAD(&info->rx_urbs_free);
434 INIT_LIST_HEAD(&info->rx_urbs_submitted);
435 INIT_LIST_HEAD(&info->rx_urb_q);
436 INIT_LIST_HEAD(&info->tx_urbs_free);
437 INIT_LIST_HEAD(&info->tx_urbs_submitted);
439 for (j = 0; j < urb_pool_size; j++) {
440 urb = usb_alloc_urb(0, GFP_KERNEL);
442 dev_err(&port->dev, "No free urbs available\n");
445 buf_size = port->read_urb->transfer_buffer_length;
446 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
447 if (!urb->transfer_buffer) {
449 "Couldn't allocate urb buffer\n");
452 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
455 "Couldn't allocate urb wrapper\n");
458 usb_fill_bulk_urb(urb, serial->dev,
459 usb_rcvbulkpipe(serial->dev,
460 port->bulk_in_endpointAddress),
461 urb->transfer_buffer, buf_size,
462 whiteheat_read_callback, port);
464 list_add(&wrap->list, &info->rx_urbs_free);
466 urb = usb_alloc_urb(0, GFP_KERNEL);
468 dev_err(&port->dev, "No free urbs available\n");
471 buf_size = port->write_urb->transfer_buffer_length;
472 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
473 if (!urb->transfer_buffer) {
475 "Couldn't allocate urb buffer\n");
478 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
481 "Couldn't allocate urb wrapper\n");
484 usb_fill_bulk_urb(urb, serial->dev,
485 usb_sndbulkpipe(serial->dev,
486 port->bulk_out_endpointAddress),
487 urb->transfer_buffer, buf_size,
488 whiteheat_write_callback, port);
490 list_add(&wrap->list, &info->tx_urbs_free);
493 usb_set_serial_port_data(port, info);
496 command_info = kmalloc(sizeof(struct whiteheat_command_private),
498 if (command_info == NULL) {
499 dev_err(&serial->dev->dev,
500 "%s: Out of memory for port structures\n",
501 serial->type->description);
502 goto no_command_private;
505 mutex_init(&command_info->mutex);
506 command_info->port_running = 0;
507 init_waitqueue_head(&command_info->wait_command);
508 usb_set_serial_port_data(command_port, command_info);
509 command_port->write_urb->complete = command_port_write_callback;
510 command_port->read_urb->complete = command_port_read_callback;
517 /* Firmware likely not running */
518 dev_err(&serial->dev->dev,
519 "%s: Unable to retrieve firmware version, try replugging\n",
520 serial->type->description);
521 dev_err(&serial->dev->dev,
522 "%s: If the firmware is not running (status led not blinking)\n",
523 serial->type->description);
524 dev_err(&serial->dev->dev,
525 "%s: please contact support@connecttech.com\n",
526 serial->type->description);
531 for (i = serial->num_ports - 1; i >= 0; i--) {
532 port = serial->port[i];
533 info = usb_get_serial_port_data(port);
534 for (j = urb_pool_size - 1; j >= 0; j--) {
535 tmp = list_first(&info->tx_urbs_free);
537 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
541 kfree(urb->transfer_buffer);
545 tmp = list_first(&info->rx_urbs_free);
547 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
551 kfree(urb->transfer_buffer);
569 static void whiteheat_release(struct usb_serial *serial)
571 struct usb_serial_port *command_port;
572 struct usb_serial_port *port;
573 struct whiteheat_private *info;
574 struct whiteheat_urb_wrap *wrap;
576 struct list_head *tmp;
577 struct list_head *tmp2;
582 /* free up our private data for our command port */
583 command_port = serial->port[COMMAND_PORT];
584 kfree(usb_get_serial_port_data(command_port));
586 for (i = 0; i < serial->num_ports; i++) {
587 port = serial->port[i];
588 info = usb_get_serial_port_data(port);
589 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
591 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
594 kfree(urb->transfer_buffer);
597 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
599 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
602 kfree(urb->transfer_buffer);
609 static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
613 dbg("%s - port %d", __func__, port->number);
615 retval = start_command_port(port->serial);
620 tty->low_latency = 1;
622 /* send an open port command */
623 retval = firm_open(port);
625 stop_command_port(port->serial);
629 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
632 stop_command_port(port->serial);
637 firm_setup_port(tty);
639 /* Work around HCD bugs */
640 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
641 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
643 /* Start reading from the device */
644 retval = start_port_read(port);
647 "%s - failed submitting read urb, error %d\n",
650 stop_command_port(port->serial);
655 dbg("%s - exit, retval = %d", __func__, retval);
660 static void whiteheat_close(struct usb_serial_port *port)
662 struct whiteheat_private *info = usb_get_serial_port_data(port);
663 struct whiteheat_urb_wrap *wrap;
665 struct list_head *tmp;
666 struct list_head *tmp2;
668 dbg("%s - port %d", __func__, port->number);
670 firm_report_tx_done(port);
673 /* shutdown our bulk reads and writes */
674 mutex_lock(&info->deathwarrant);
675 spin_lock_irq(&info->lock);
676 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
677 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
680 spin_unlock_irq(&info->lock);
682 spin_lock_irq(&info->lock);
683 list_add(tmp, &info->rx_urbs_free);
685 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
686 list_move(tmp, &info->rx_urbs_free);
687 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
688 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
691 spin_unlock_irq(&info->lock);
693 spin_lock_irq(&info->lock);
694 list_add(tmp, &info->tx_urbs_free);
696 spin_unlock_irq(&info->lock);
697 mutex_unlock(&info->deathwarrant);
698 stop_command_port(port->serial);
702 static int whiteheat_write(struct tty_struct *tty,
703 struct usb_serial_port *port, const unsigned char *buf, int count)
705 struct usb_serial *serial = port->serial;
706 struct whiteheat_private *info = usb_get_serial_port_data(port);
707 struct whiteheat_urb_wrap *wrap;
713 struct list_head *tmp;
715 dbg("%s - port %d", __func__, port->number);
718 dbg("%s - write request of 0 bytes", __func__);
723 spin_lock_irqsave(&info->lock, flags);
724 if (list_empty(&info->tx_urbs_free)) {
725 spin_unlock_irqrestore(&info->lock, flags);
728 tmp = list_first(&info->tx_urbs_free);
730 spin_unlock_irqrestore(&info->lock, flags);
732 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
734 bytes = (count > port->bulk_out_size) ?
735 port->bulk_out_size : count;
736 memcpy(urb->transfer_buffer, buf + sent, bytes);
738 usb_serial_debug_data(debug, &port->dev,
739 __func__, bytes, urb->transfer_buffer);
741 urb->dev = serial->dev;
742 urb->transfer_buffer_length = bytes;
743 result = usb_submit_urb(urb, GFP_ATOMIC);
746 "%s - failed submitting write urb, error %d\n",
749 spin_lock_irqsave(&info->lock, flags);
750 list_add(tmp, &info->tx_urbs_free);
751 spin_unlock_irqrestore(&info->lock, flags);
756 spin_lock_irqsave(&info->lock, flags);
757 list_add(tmp, &info->tx_urbs_submitted);
758 spin_unlock_irqrestore(&info->lock, flags);
765 static int whiteheat_write_room(struct tty_struct *tty)
767 struct usb_serial_port *port = tty->driver_data;
768 struct whiteheat_private *info = usb_get_serial_port_data(port);
769 struct list_head *tmp;
773 dbg("%s - port %d", __func__, port->number);
775 spin_lock_irqsave(&info->lock, flags);
776 list_for_each(tmp, &info->tx_urbs_free)
778 spin_unlock_irqrestore(&info->lock, flags);
779 room *= port->bulk_out_size;
781 dbg("%s - returns %d", __func__, room);
785 static int whiteheat_tiocmget(struct tty_struct *tty)
787 struct usb_serial_port *port = tty->driver_data;
788 struct whiteheat_private *info = usb_get_serial_port_data(port);
789 unsigned int modem_signals = 0;
791 dbg("%s - port %d", __func__, port->number);
793 firm_get_dtr_rts(port);
794 if (info->mcr & UART_MCR_DTR)
795 modem_signals |= TIOCM_DTR;
796 if (info->mcr & UART_MCR_RTS)
797 modem_signals |= TIOCM_RTS;
799 return modem_signals;
802 static int whiteheat_tiocmset(struct tty_struct *tty,
803 unsigned int set, unsigned int clear)
805 struct usb_serial_port *port = tty->driver_data;
806 struct whiteheat_private *info = usb_get_serial_port_data(port);
808 dbg("%s - port %d", __func__, port->number);
811 info->mcr |= UART_MCR_RTS;
813 info->mcr |= UART_MCR_DTR;
815 if (clear & TIOCM_RTS)
816 info->mcr &= ~UART_MCR_RTS;
817 if (clear & TIOCM_DTR)
818 info->mcr &= ~UART_MCR_DTR;
820 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
821 firm_set_rts(port, info->mcr & UART_MCR_RTS);
826 static int whiteheat_ioctl(struct tty_struct *tty,
827 unsigned int cmd, unsigned long arg)
829 struct usb_serial_port *port = tty->driver_data;
830 struct serial_struct serstruct;
831 void __user *user_arg = (void __user *)arg;
833 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
837 memset(&serstruct, 0, sizeof(serstruct));
838 serstruct.type = PORT_16654;
839 serstruct.line = port->serial->minor;
840 serstruct.port = port->number;
841 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
842 serstruct.xmit_fifo_size = port->bulk_out_size;
843 serstruct.custom_divisor = 0;
844 serstruct.baud_base = 460800;
845 serstruct.close_delay = CLOSING_DELAY;
846 serstruct.closing_wait = CLOSING_DELAY;
848 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
859 static void whiteheat_set_termios(struct tty_struct *tty,
860 struct usb_serial_port *port, struct ktermios *old_termios)
862 firm_setup_port(tty);
865 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
867 struct usb_serial_port *port = tty->driver_data;
868 firm_set_break(port, break_state);
872 static int whiteheat_chars_in_buffer(struct tty_struct *tty)
874 struct usb_serial_port *port = tty->driver_data;
875 struct whiteheat_private *info = usb_get_serial_port_data(port);
876 struct list_head *tmp;
877 struct whiteheat_urb_wrap *wrap;
881 dbg("%s - port %d", __func__, port->number);
883 spin_lock_irqsave(&info->lock, flags);
884 list_for_each(tmp, &info->tx_urbs_submitted) {
885 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
886 chars += wrap->urb->transfer_buffer_length;
888 spin_unlock_irqrestore(&info->lock, flags);
890 dbg("%s - returns %d", __func__, chars);
895 static void whiteheat_throttle(struct tty_struct *tty)
897 struct usb_serial_port *port = tty->driver_data;
898 struct whiteheat_private *info = usb_get_serial_port_data(port);
900 dbg("%s - port %d", __func__, port->number);
902 spin_lock_irq(&info->lock);
903 info->flags |= THROTTLED;
904 spin_unlock_irq(&info->lock);
908 static void whiteheat_unthrottle(struct tty_struct *tty)
910 struct usb_serial_port *port = tty->driver_data;
911 struct whiteheat_private *info = usb_get_serial_port_data(port);
912 int actually_throttled;
914 dbg("%s - port %d", __func__, port->number);
916 spin_lock_irq(&info->lock);
917 actually_throttled = info->flags & ACTUALLY_THROTTLED;
918 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
919 spin_unlock_irq(&info->lock);
921 if (actually_throttled)
922 rx_data_softint(&info->rx_work);
926 /*****************************************************************************
927 * Connect Tech's White Heat callback routines
928 *****************************************************************************/
929 static void command_port_write_callback(struct urb *urb)
931 int status = urb->status;
936 dbg("nonzero urb status: %d", status);
942 static void command_port_read_callback(struct urb *urb)
944 struct usb_serial_port *command_port = urb->context;
945 struct whiteheat_command_private *command_info;
946 int status = urb->status;
947 unsigned char *data = urb->transfer_buffer;
952 command_info = usb_get_serial_port_data(command_port);
954 dbg("%s - command_info is NULL, exiting.", __func__);
958 dbg("%s - nonzero urb status: %d", __func__, status);
959 if (status != -ENOENT)
960 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
961 wake_up(&command_info->wait_command);
965 usb_serial_debug_data(debug, &command_port->dev,
966 __func__, urb->actual_length, data);
968 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
969 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
970 wake_up(&command_info->wait_command);
971 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
972 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
973 wake_up(&command_info->wait_command);
974 } else if (data[0] == WHITEHEAT_EVENT) {
975 /* These are unsolicited reports from the firmware, hence no
976 waiting command to wakeup */
977 dbg("%s - event received", __func__);
978 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
979 memcpy(command_info->result_buffer, &data[1],
980 urb->actual_length - 1);
981 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
982 wake_up(&command_info->wait_command);
984 dbg("%s - bad reply from firmware", __func__);
986 /* Continue trying to always read */
987 command_port->read_urb->dev = command_port->serial->dev;
988 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
990 dbg("%s - failed resubmitting read urb, error %d",
995 static void whiteheat_read_callback(struct urb *urb)
997 struct usb_serial_port *port = urb->context;
998 struct whiteheat_urb_wrap *wrap;
999 unsigned char *data = urb->transfer_buffer;
1000 struct whiteheat_private *info = usb_get_serial_port_data(port);
1001 int status = urb->status;
1003 dbg("%s - port %d", __func__, port->number);
1005 spin_lock(&info->lock);
1006 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1008 spin_unlock(&info->lock);
1009 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1012 list_del(&wrap->list);
1013 spin_unlock(&info->lock);
1016 dbg("%s - nonzero read bulk status received: %d",
1018 spin_lock(&info->lock);
1019 list_add(&wrap->list, &info->rx_urbs_free);
1020 spin_unlock(&info->lock);
1024 usb_serial_debug_data(debug, &port->dev,
1025 __func__, urb->actual_length, data);
1027 spin_lock(&info->lock);
1028 list_add_tail(&wrap->list, &info->rx_urb_q);
1029 if (info->flags & THROTTLED) {
1030 info->flags |= ACTUALLY_THROTTLED;
1031 spin_unlock(&info->lock);
1034 spin_unlock(&info->lock);
1036 schedule_work(&info->rx_work);
1040 static void whiteheat_write_callback(struct urb *urb)
1042 struct usb_serial_port *port = urb->context;
1043 struct whiteheat_private *info = usb_get_serial_port_data(port);
1044 struct whiteheat_urb_wrap *wrap;
1045 int status = urb->status;
1047 dbg("%s - port %d", __func__, port->number);
1049 spin_lock(&info->lock);
1050 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1052 spin_unlock(&info->lock);
1053 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1056 list_move(&wrap->list, &info->tx_urbs_free);
1057 spin_unlock(&info->lock);
1060 dbg("%s - nonzero write bulk status received: %d",
1065 usb_serial_port_softint(port);
1069 /*****************************************************************************
1070 * Connect Tech's White Heat firmware interface
1071 *****************************************************************************/
1072 static int firm_send_command(struct usb_serial_port *port, __u8 command,
1073 __u8 *data, __u8 datasize)
1075 struct usb_serial_port *command_port;
1076 struct whiteheat_command_private *command_info;
1077 struct whiteheat_private *info;
1078 __u8 *transfer_buffer;
1082 dbg("%s - command %d", __func__, command);
1084 command_port = port->serial->port[COMMAND_PORT];
1085 command_info = usb_get_serial_port_data(command_port);
1086 mutex_lock(&command_info->mutex);
1087 command_info->command_finished = false;
1089 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1090 transfer_buffer[0] = command;
1091 memcpy(&transfer_buffer[1], data, datasize);
1092 command_port->write_urb->transfer_buffer_length = datasize + 1;
1093 command_port->write_urb->dev = port->serial->dev;
1094 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1096 dbg("%s - submit urb failed", __func__);
1100 /* wait for the command to complete */
1101 t = wait_event_timeout(command_info->wait_command,
1102 (bool)command_info->command_finished, COMMAND_TIMEOUT);
1104 usb_kill_urb(command_port->write_urb);
1106 if (command_info->command_finished == false) {
1107 dbg("%s - command timed out.", __func__);
1108 retval = -ETIMEDOUT;
1112 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1113 dbg("%s - command failed.", __func__);
1118 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1119 dbg("%s - command completed.", __func__);
1121 case WHITEHEAT_GET_DTR_RTS:
1122 info = usb_get_serial_port_data(port);
1123 memcpy(&info->mcr, command_info->result_buffer,
1124 sizeof(struct whiteheat_dr_info));
1129 mutex_unlock(&command_info->mutex);
1134 static int firm_open(struct usb_serial_port *port)
1136 struct whiteheat_simple open_command;
1138 open_command.port = port->number - port->serial->minor + 1;
1139 return firm_send_command(port, WHITEHEAT_OPEN,
1140 (__u8 *)&open_command, sizeof(open_command));
1144 static int firm_close(struct usb_serial_port *port)
1146 struct whiteheat_simple close_command;
1148 close_command.port = port->number - port->serial->minor + 1;
1149 return firm_send_command(port, WHITEHEAT_CLOSE,
1150 (__u8 *)&close_command, sizeof(close_command));
1154 static void firm_setup_port(struct tty_struct *tty)
1156 struct usb_serial_port *port = tty->driver_data;
1157 struct whiteheat_port_settings port_settings;
1158 unsigned int cflag = tty->termios->c_cflag;
1160 port_settings.port = port->number + 1;
1162 /* get the byte size */
1163 switch (cflag & CSIZE) {
1164 case CS5: port_settings.bits = 5; break;
1165 case CS6: port_settings.bits = 6; break;
1166 case CS7: port_settings.bits = 7; break;
1168 case CS8: port_settings.bits = 8; break;
1170 dbg("%s - data bits = %d", __func__, port_settings.bits);
1172 /* determine the parity */
1176 port_settings.parity = WHITEHEAT_PAR_MARK;
1178 port_settings.parity = WHITEHEAT_PAR_SPACE;
1181 port_settings.parity = WHITEHEAT_PAR_ODD;
1183 port_settings.parity = WHITEHEAT_PAR_EVEN;
1185 port_settings.parity = WHITEHEAT_PAR_NONE;
1186 dbg("%s - parity = %c", __func__, port_settings.parity);
1188 /* figure out the stop bits requested */
1190 port_settings.stop = 2;
1192 port_settings.stop = 1;
1193 dbg("%s - stop bits = %d", __func__, port_settings.stop);
1195 /* figure out the flow control settings */
1196 if (cflag & CRTSCTS)
1197 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1198 WHITEHEAT_HFLOW_RTS);
1200 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1201 dbg("%s - hardware flow control = %s %s %s %s", __func__,
1202 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1203 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1204 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1205 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1207 /* determine software flow control */
1209 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1211 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1212 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
1214 port_settings.xon = START_CHAR(tty);
1215 port_settings.xoff = STOP_CHAR(tty);
1216 dbg("%s - XON = %2x, XOFF = %2x",
1217 __func__, port_settings.xon, port_settings.xoff);
1219 /* get the baud rate wanted */
1220 port_settings.baud = tty_get_baud_rate(tty);
1221 dbg("%s - baud rate = %d", __func__, port_settings.baud);
1223 /* fixme: should set validated settings */
1224 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1225 /* handle any settings that aren't specified in the tty structure */
1226 port_settings.lloop = 0;
1228 /* now send the message to the device */
1229 firm_send_command(port, WHITEHEAT_SETUP_PORT,
1230 (__u8 *)&port_settings, sizeof(port_settings));
1234 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1236 struct whiteheat_set_rdb rts_command;
1238 rts_command.port = port->number - port->serial->minor + 1;
1239 rts_command.state = onoff;
1240 return firm_send_command(port, WHITEHEAT_SET_RTS,
1241 (__u8 *)&rts_command, sizeof(rts_command));
1245 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1247 struct whiteheat_set_rdb dtr_command;
1249 dtr_command.port = port->number - port->serial->minor + 1;
1250 dtr_command.state = onoff;
1251 return firm_send_command(port, WHITEHEAT_SET_DTR,
1252 (__u8 *)&dtr_command, sizeof(dtr_command));
1256 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1258 struct whiteheat_set_rdb break_command;
1260 break_command.port = port->number - port->serial->minor + 1;
1261 break_command.state = onoff;
1262 return firm_send_command(port, WHITEHEAT_SET_BREAK,
1263 (__u8 *)&break_command, sizeof(break_command));
1267 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1269 struct whiteheat_purge purge_command;
1271 purge_command.port = port->number - port->serial->minor + 1;
1272 purge_command.what = rxtx;
1273 return firm_send_command(port, WHITEHEAT_PURGE,
1274 (__u8 *)&purge_command, sizeof(purge_command));
1278 static int firm_get_dtr_rts(struct usb_serial_port *port)
1280 struct whiteheat_simple get_dr_command;
1282 get_dr_command.port = port->number - port->serial->minor + 1;
1283 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1284 (__u8 *)&get_dr_command, sizeof(get_dr_command));
1288 static int firm_report_tx_done(struct usb_serial_port *port)
1290 struct whiteheat_simple close_command;
1292 close_command.port = port->number - port->serial->minor + 1;
1293 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1294 (__u8 *)&close_command, sizeof(close_command));
1298 /*****************************************************************************
1299 * Connect Tech's White Heat utility functions
1300 *****************************************************************************/
1301 static int start_command_port(struct usb_serial *serial)
1303 struct usb_serial_port *command_port;
1304 struct whiteheat_command_private *command_info;
1307 command_port = serial->port[COMMAND_PORT];
1308 command_info = usb_get_serial_port_data(command_port);
1309 mutex_lock(&command_info->mutex);
1310 if (!command_info->port_running) {
1311 /* Work around HCD bugs */
1312 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1314 command_port->read_urb->dev = serial->dev;
1315 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1317 dev_err(&serial->dev->dev,
1318 "%s - failed submitting read urb, error %d\n",
1323 command_info->port_running++;
1326 mutex_unlock(&command_info->mutex);
1331 static void stop_command_port(struct usb_serial *serial)
1333 struct usb_serial_port *command_port;
1334 struct whiteheat_command_private *command_info;
1336 command_port = serial->port[COMMAND_PORT];
1337 command_info = usb_get_serial_port_data(command_port);
1338 mutex_lock(&command_info->mutex);
1339 command_info->port_running--;
1340 if (!command_info->port_running)
1341 usb_kill_urb(command_port->read_urb);
1342 mutex_unlock(&command_info->mutex);
1346 static int start_port_read(struct usb_serial_port *port)
1348 struct whiteheat_private *info = usb_get_serial_port_data(port);
1349 struct whiteheat_urb_wrap *wrap;
1352 unsigned long flags;
1353 struct list_head *tmp;
1354 struct list_head *tmp2;
1356 spin_lock_irqsave(&info->lock, flags);
1358 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1360 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1362 urb->dev = port->serial->dev;
1363 spin_unlock_irqrestore(&info->lock, flags);
1364 retval = usb_submit_urb(urb, GFP_KERNEL);
1366 spin_lock_irqsave(&info->lock, flags);
1367 list_add(tmp, &info->rx_urbs_free);
1368 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1369 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1372 spin_unlock_irqrestore(&info->lock, flags);
1374 spin_lock_irqsave(&info->lock, flags);
1375 list_add(tmp, &info->rx_urbs_free);
1379 spin_lock_irqsave(&info->lock, flags);
1380 list_add(tmp, &info->rx_urbs_submitted);
1383 spin_unlock_irqrestore(&info->lock, flags);
1389 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1390 struct list_head *head)
1392 struct whiteheat_urb_wrap *wrap;
1393 struct list_head *tmp;
1395 list_for_each(tmp, head) {
1396 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1397 if (wrap->urb == urb)
1405 static struct list_head *list_first(struct list_head *head)
1411 static void rx_data_softint(struct work_struct *work)
1413 struct whiteheat_private *info =
1414 container_of(work, struct whiteheat_private, rx_work);
1415 struct usb_serial_port *port = info->port;
1416 struct tty_struct *tty = tty_port_tty_get(&port->port);
1417 struct whiteheat_urb_wrap *wrap;
1419 unsigned long flags;
1420 struct list_head *tmp;
1421 struct list_head *tmp2;
1425 spin_lock_irqsave(&info->lock, flags);
1426 if (info->flags & THROTTLED) {
1427 spin_unlock_irqrestore(&info->lock, flags);
1431 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1433 spin_unlock_irqrestore(&info->lock, flags);
1435 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1438 if (tty && urb->actual_length)
1439 sent += tty_insert_flip_string(tty,
1440 urb->transfer_buffer, urb->actual_length);
1442 urb->dev = port->serial->dev;
1443 result = usb_submit_urb(urb, GFP_ATOMIC);
1446 "%s - failed resubmitting read urb, error %d\n",
1448 spin_lock_irqsave(&info->lock, flags);
1449 list_add(tmp, &info->rx_urbs_free);
1453 spin_lock_irqsave(&info->lock, flags);
1454 list_add(tmp, &info->rx_urbs_submitted);
1456 spin_unlock_irqrestore(&info->lock, flags);
1459 tty_flip_buffer_push(tty);
1465 /*****************************************************************************
1466 * Connect Tech's White Heat module functions
1467 *****************************************************************************/
1468 static int __init whiteheat_init(void)
1471 retval = usb_serial_register(&whiteheat_fake_device);
1473 goto failed_fake_register;
1474 retval = usb_serial_register(&whiteheat_device);
1476 goto failed_device_register;
1477 retval = usb_register(&whiteheat_driver);
1479 goto failed_usb_register;
1480 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1483 failed_usb_register:
1484 usb_serial_deregister(&whiteheat_device);
1485 failed_device_register:
1486 usb_serial_deregister(&whiteheat_fake_device);
1487 failed_fake_register:
1492 static void __exit whiteheat_exit(void)
1494 usb_deregister(&whiteheat_driver);
1495 usb_serial_deregister(&whiteheat_fake_device);
1496 usb_serial_deregister(&whiteheat_device);
1500 module_init(whiteheat_init);
1501 module_exit(whiteheat_exit);
1503 MODULE_AUTHOR(DRIVER_AUTHOR);
1504 MODULE_DESCRIPTION(DRIVER_DESC);
1505 MODULE_LICENSE("GPL");
1507 MODULE_FIRMWARE("whiteheat.fw");
1508 MODULE_FIRMWARE("whiteheat_loader.fw");
1510 module_param(urb_pool_size, int, 0);
1511 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1513 module_param(debug, bool, S_IRUGO | S_IWUSR);
1514 MODULE_PARM_DESC(debug, "Debug enabled or not");