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 driver
17 * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com)
18 * Upgrade to full working driver
21 * switched from using spinlock to a semaphore, which fixes lots of problems.
24 * Identify version on module load.
27 * Fixed MOD_INC and MOD_DEC logic, the ability to open a port more
28 * than once, and the got the proper usb_device_id table entries so
29 * the driver works again.
31 * (11/01/2000) Adam J. Richter
32 * usb_device_id table support
35 * Fixed bug with urb->dev not being set properly, now that the usb
39 * firmware is improved to guard against crap sent to device
40 * firmware now replies CMD_FAILURE on bad things
41 * read_callback fix you provided for private info struct
42 * command_finished now indicates success or fail
43 * setup_port struct now packed to avoid gcc padding
44 * firmware uses 1 based port numbering, driver now handles that
47 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
50 * Added module_init and module_exit functions to handle the fact that this
51 * driver is a loadable module now.
52 * Fixed bug with port->minor that was found by Al Borchers
55 * Added support for port settings. Baud rate can now be changed. Line signals
56 * are not transferred to and from the tty layer yet, but things seem to be
60 * First cut at open and close commands. Data can flow through the ports at
64 * Split driver up into device specific pieces.
68 #include <linux/kernel.h>
69 #include <linux/errno.h>
70 #include <linux/init.h>
71 #include <linux/slab.h>
72 #include <linux/tty.h>
73 #include <linux/tty_driver.h>
74 #include <linux/tty_flip.h>
75 #include <linux/module.h>
76 #include <linux/spinlock.h>
77 #include <asm/uaccess.h>
78 #include <asm/termbits.h>
79 #include <linux/usb.h>
80 #include <linux/serial_reg.h>
81 #include <linux/serial.h>
82 #include <linux/usb/serial.h>
83 #include "whiteheat_fw.h" /* firmware for the ConnectTech WhiteHEAT device */
84 #include "whiteheat.h" /* WhiteHEAT specific commands */
95 #define DRIVER_VERSION "v2.0"
96 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
97 #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
99 #define CONNECT_TECH_VENDOR_ID 0x0710
100 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
101 #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
104 ID tables for whiteheat are unusual, because we want to different
105 things for different versions of the device. Eventually, this
106 will be doable from a single table. But, for now, we define two
107 separate ID tables, and then a third table that combines them
108 just for the purpose of exporting the autoloading information.
110 static struct usb_device_id id_table_std [] = {
111 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
112 { } /* Terminating entry */
115 static struct usb_device_id id_table_prerenumeration [] = {
116 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
117 { } /* Terminating entry */
120 static struct usb_device_id id_table_combined [] = {
121 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
122 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
123 { } /* Terminating entry */
126 MODULE_DEVICE_TABLE (usb, id_table_combined);
128 static struct usb_driver whiteheat_driver = {
130 .probe = usb_serial_probe,
131 .disconnect = usb_serial_disconnect,
132 .id_table = id_table_combined,
136 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
137 static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id);
138 static int whiteheat_firmware_attach (struct usb_serial *serial);
140 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
141 static int whiteheat_attach (struct usb_serial *serial);
142 static void whiteheat_shutdown (struct usb_serial *serial);
143 static int whiteheat_open (struct usb_serial_port *port, struct file *filp);
144 static void whiteheat_close (struct usb_serial_port *port, struct file *filp);
145 static int whiteheat_write (struct usb_serial_port *port, const unsigned char *buf, int count);
146 static int whiteheat_write_room (struct usb_serial_port *port);
147 static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
148 static void whiteheat_set_termios (struct usb_serial_port *port, struct ktermios * old);
149 static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file);
150 static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
151 static void whiteheat_break_ctl (struct usb_serial_port *port, int break_state);
152 static int whiteheat_chars_in_buffer (struct usb_serial_port *port);
153 static void whiteheat_throttle (struct usb_serial_port *port);
154 static void whiteheat_unthrottle (struct usb_serial_port *port);
155 static void whiteheat_read_callback (struct urb *urb);
156 static void whiteheat_write_callback (struct urb *urb);
158 static struct usb_serial_driver whiteheat_fake_device = {
160 .owner = THIS_MODULE,
161 .name = "whiteheatnofirm",
163 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
164 .usb_driver = &whiteheat_driver,
165 .id_table = id_table_prerenumeration,
166 .num_interrupt_in = NUM_DONT_CARE,
167 .num_bulk_in = NUM_DONT_CARE,
168 .num_bulk_out = NUM_DONT_CARE,
170 .probe = whiteheat_firmware_download,
171 .attach = whiteheat_firmware_attach,
174 static struct usb_serial_driver whiteheat_device = {
176 .owner = THIS_MODULE,
179 .description = "Connect Tech - WhiteHEAT",
180 .usb_driver = &whiteheat_driver,
181 .id_table = id_table_std,
182 .num_interrupt_in = NUM_DONT_CARE,
183 .num_bulk_in = NUM_DONT_CARE,
184 .num_bulk_out = NUM_DONT_CARE,
186 .attach = whiteheat_attach,
187 .shutdown = whiteheat_shutdown,
188 .open = whiteheat_open,
189 .close = whiteheat_close,
190 .write = whiteheat_write,
191 .write_room = whiteheat_write_room,
192 .ioctl = whiteheat_ioctl,
193 .set_termios = whiteheat_set_termios,
194 .break_ctl = whiteheat_break_ctl,
195 .tiocmget = whiteheat_tiocmget,
196 .tiocmset = whiteheat_tiocmset,
197 .chars_in_buffer = whiteheat_chars_in_buffer,
198 .throttle = whiteheat_throttle,
199 .unthrottle = whiteheat_unthrottle,
200 .read_bulk_callback = whiteheat_read_callback,
201 .write_bulk_callback = whiteheat_write_callback,
205 struct whiteheat_command_private {
208 __u8 command_finished;
209 wait_queue_head_t wait_command; /* for handling sleeping while waiting for a command to finish */
210 __u8 result_buffer[64];
214 #define THROTTLED 0x01
215 #define ACTUALLY_THROTTLED 0x02
217 static int urb_pool_size = 8;
219 struct whiteheat_urb_wrap {
220 struct list_head list;
224 struct whiteheat_private {
228 struct list_head rx_urbs_free;
229 struct list_head rx_urbs_submitted;
230 struct list_head rx_urb_q;
231 struct work_struct rx_work;
232 struct usb_serial_port *port;
233 struct list_head tx_urbs_free;
234 struct list_head tx_urbs_submitted;
238 /* local function prototypes */
239 static int start_command_port(struct usb_serial *serial);
240 static void stop_command_port(struct usb_serial *serial);
241 static void command_port_write_callback(struct urb *urb);
242 static void command_port_read_callback(struct urb *urb);
244 static int start_port_read(struct usb_serial_port *port);
245 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb, struct list_head *head);
246 static struct list_head *list_first(struct list_head *head);
247 static void rx_data_softint(struct work_struct *work);
249 static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize);
250 static int firm_open(struct usb_serial_port *port);
251 static int firm_close(struct usb_serial_port *port);
252 static int firm_setup_port(struct usb_serial_port *port);
253 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
254 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
255 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
256 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
257 static int firm_get_dtr_rts(struct usb_serial_port *port);
258 static int firm_report_tx_done(struct usb_serial_port *port);
261 #define COMMAND_PORT 4
262 #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
263 #define COMMAND_TIMEOUT_MS 2000
264 #define CLOSING_DELAY (30 * HZ)
267 /*****************************************************************************
268 * Connect Tech's White Heat prerenumeration driver functions
269 *****************************************************************************/
271 /* steps to download the firmware to the WhiteHEAT device:
272 - hold the reset (by writing to the reset bit of the CPUCS register)
273 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
274 - release the reset (by writing to the CPUCS register)
275 - download the WH.HEX file for all addresses greater than 0x1b3f using
276 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
278 - download the WH.HEX file for all addresses less than 0x1b40 using
279 VENDOR_REQUEST_ANCHOR_LOAD
281 - device renumerated itself and comes up as new device id with all
282 firmware download completed.
284 static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id)
287 const struct whiteheat_hex_record *record;
289 dbg("%s", __FUNCTION__);
291 response = ezusb_set_reset (serial, 1);
293 record = &whiteheat_loader[0];
294 while (record->address != 0xffff) {
295 response = ezusb_writememory (serial, record->address,
296 (unsigned char *)record->data, record->data_size, 0xa0);
298 err("%s - ezusb_writememory failed for loader (%d %04X %p %d)",
299 __FUNCTION__, response, record->address, record->data, record->data_size);
305 response = ezusb_set_reset (serial, 0);
307 record = &whiteheat_firmware[0];
308 while (record->address < 0x1b40) {
311 while (record->address != 0xffff) {
312 response = ezusb_writememory (serial, record->address,
313 (unsigned char *)record->data, record->data_size, 0xa3);
315 err("%s - ezusb_writememory failed for first firmware step (%d %04X %p %d)",
316 __FUNCTION__, response, record->address, record->data, record->data_size);
322 response = ezusb_set_reset (serial, 1);
324 record = &whiteheat_firmware[0];
325 while (record->address < 0x1b40) {
326 response = ezusb_writememory (serial, record->address,
327 (unsigned char *)record->data, record->data_size, 0xa0);
329 err("%s - ezusb_writememory failed for second firmware step (%d %04X %p %d)",
330 __FUNCTION__, response, record->address, record->data, record->data_size);
336 response = ezusb_set_reset (serial, 0);
342 static int whiteheat_firmware_attach (struct usb_serial *serial)
344 /* We want this device to fail to have a driver assigned to it */
349 /*****************************************************************************
350 * Connect Tech's White Heat serial driver functions
351 *****************************************************************************/
352 static int whiteheat_attach (struct usb_serial *serial)
354 struct usb_serial_port *command_port;
355 struct whiteheat_command_private *command_info;
356 struct usb_serial_port *port;
357 struct whiteheat_private *info;
358 struct whiteheat_hw_info *hw_info;
368 struct whiteheat_urb_wrap *wrap;
369 struct list_head *tmp;
371 command_port = serial->port[COMMAND_PORT];
373 pipe = usb_sndbulkpipe (serial->dev, command_port->bulk_out_endpointAddress);
374 command = kmalloc(2, GFP_KERNEL);
376 goto no_command_buffer;
377 command[0] = WHITEHEAT_GET_HW_INFO;
380 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
382 goto no_result_buffer;
384 * When the module is reloaded the firmware is still there and
385 * the endpoints are still in the usb core unchanged. This is the
386 * unlinking bug in disguise. Same for the call below.
388 usb_clear_halt(serial->dev, pipe);
389 ret = usb_bulk_msg (serial->dev, pipe, command, 2, &alen, COMMAND_TIMEOUT_MS);
391 err("%s: Couldn't send command [%d]", serial->type->description, ret);
393 } else if (alen != 2) {
394 err("%s: Send command incomplete [%d]", serial->type->description, alen);
398 pipe = usb_rcvbulkpipe (serial->dev, command_port->bulk_in_endpointAddress);
399 /* See the comment on the usb_clear_halt() above */
400 usb_clear_halt(serial->dev, pipe);
401 ret = usb_bulk_msg (serial->dev, pipe, result, sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
403 err("%s: Couldn't get results [%d]", serial->type->description, ret);
405 } else if (alen != sizeof(*hw_info) + 1) {
406 err("%s: Get results incomplete [%d]", serial->type->description, alen);
408 } else if (result[0] != command[0]) {
409 err("%s: Command failed [%d]", serial->type->description, result[0]);
413 hw_info = (struct whiteheat_hw_info *)&result[1];
415 info("%s: Driver %s: Firmware v%d.%02d", serial->type->description,
416 DRIVER_VERSION, hw_info->sw_major_rev, hw_info->sw_minor_rev);
418 for (i = 0; i < serial->num_ports; i++) {
419 port = serial->port[i];
421 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
423 err("%s: Out of memory for port structures\n", serial->type->description);
427 spin_lock_init(&info->lock);
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 err("No free urbs available");
445 buf_size = port->read_urb->transfer_buffer_length;
446 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
447 if (!urb->transfer_buffer) {
448 err("Couldn't allocate urb buffer");
451 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
453 err("Couldn't allocate urb wrapper");
456 usb_fill_bulk_urb(urb, serial->dev,
457 usb_rcvbulkpipe(serial->dev,
458 port->bulk_in_endpointAddress),
459 urb->transfer_buffer, buf_size,
460 whiteheat_read_callback, port);
462 list_add(&wrap->list, &info->rx_urbs_free);
464 urb = usb_alloc_urb(0, GFP_KERNEL);
466 err("No free urbs available");
469 buf_size = port->write_urb->transfer_buffer_length;
470 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
471 if (!urb->transfer_buffer) {
472 err("Couldn't allocate urb buffer");
475 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
477 err("Couldn't allocate urb wrapper");
480 usb_fill_bulk_urb(urb, serial->dev,
481 usb_sndbulkpipe(serial->dev,
482 port->bulk_out_endpointAddress),
483 urb->transfer_buffer, buf_size,
484 whiteheat_write_callback, port);
486 list_add(&wrap->list, &info->tx_urbs_free);
489 usb_set_serial_port_data(port, info);
492 command_info = kmalloc(sizeof(struct whiteheat_command_private), GFP_KERNEL);
493 if (command_info == NULL) {
494 err("%s: Out of memory for port structures\n", serial->type->description);
495 goto no_command_private;
498 spin_lock_init(&command_info->lock);
499 command_info->port_running = 0;
500 init_waitqueue_head(&command_info->wait_command);
501 usb_set_serial_port_data(command_port, command_info);
502 command_port->write_urb->complete = command_port_write_callback;
503 command_port->read_urb->complete = command_port_read_callback;
510 /* Firmware likely not running */
511 err("%s: Unable to retrieve firmware version, try replugging\n", serial->type->description);
512 err("%s: If the firmware is not running (status led not blinking)\n", serial->type->description);
513 err("%s: please contact support@connecttech.com\n", serial->type->description);
518 for (i = serial->num_ports - 1; i >= 0; i--) {
519 port = serial->port[i];
520 info = usb_get_serial_port_data(port);
521 for (j = urb_pool_size - 1; j >= 0; j--) {
522 tmp = list_first(&info->tx_urbs_free);
524 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
528 kfree(urb->transfer_buffer);
532 tmp = list_first(&info->rx_urbs_free);
534 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
538 kfree(urb->transfer_buffer);
556 static void whiteheat_shutdown (struct usb_serial *serial)
558 struct usb_serial_port *command_port;
559 struct usb_serial_port *port;
560 struct whiteheat_private *info;
561 struct whiteheat_urb_wrap *wrap;
563 struct list_head *tmp;
564 struct list_head *tmp2;
567 dbg("%s", __FUNCTION__);
569 /* free up our private data for our command port */
570 command_port = serial->port[COMMAND_PORT];
571 kfree (usb_get_serial_port_data(command_port));
573 for (i = 0; i < serial->num_ports; i++) {
574 port = serial->port[i];
575 info = usb_get_serial_port_data(port);
576 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
578 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
581 kfree(urb->transfer_buffer);
584 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
586 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
589 kfree(urb->transfer_buffer);
599 static int whiteheat_open (struct usb_serial_port *port, struct file *filp)
602 struct ktermios old_term;
604 dbg("%s - port %d", __FUNCTION__, port->number);
606 retval = start_command_port(port->serial);
611 port->tty->low_latency = 1;
613 /* send an open port command */
614 retval = firm_open(port);
616 stop_command_port(port->serial);
620 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
623 stop_command_port(port->serial);
627 old_term.c_cflag = ~port->tty->termios->c_cflag;
628 old_term.c_iflag = ~port->tty->termios->c_iflag;
629 whiteheat_set_termios(port, &old_term);
631 /* Work around HCD bugs */
632 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
633 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
635 /* Start reading from the device */
636 retval = start_port_read(port);
638 err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
640 stop_command_port(port->serial);
645 dbg("%s - exit, retval = %d", __FUNCTION__, retval);
650 static void whiteheat_close(struct usb_serial_port *port, struct file * filp)
652 struct whiteheat_private *info = usb_get_serial_port_data(port);
653 struct whiteheat_urb_wrap *wrap;
655 struct list_head *tmp;
656 struct list_head *tmp2;
659 dbg("%s - port %d", __FUNCTION__, port->number);
661 /* filp is NULL when called from usb_serial_disconnect */
662 if (filp && (tty_hung_up_p(filp))) {
666 port->tty->closing = 1;
669 * Not currently in use; tty_wait_until_sent() calls
670 * serial_chars_in_buffer() which deadlocks on the second semaphore
671 * acquisition. This should be fixed at some point. Greg's been
673 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
674 tty_wait_until_sent(port->tty, CLOSING_DELAY);
678 if (port->tty->driver->flush_buffer)
679 port->tty->driver->flush_buffer(port->tty);
680 tty_ldisc_flush(port->tty);
682 firm_report_tx_done(port);
686 /* shutdown our bulk reads and writes */
687 spin_lock_irqsave(&info->lock, flags);
688 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
689 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
692 list_move(tmp, &info->rx_urbs_free);
694 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
695 list_move(tmp, &info->rx_urbs_free);
697 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
698 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
701 list_move(tmp, &info->tx_urbs_free);
703 spin_unlock_irqrestore(&info->lock, flags);
705 stop_command_port(port->serial);
707 port->tty->closing = 0;
711 static int whiteheat_write(struct usb_serial_port *port, const unsigned char *buf, int count)
713 struct usb_serial *serial = port->serial;
714 struct whiteheat_private *info = usb_get_serial_port_data(port);
715 struct whiteheat_urb_wrap *wrap;
721 struct list_head *tmp;
723 dbg("%s - port %d", __FUNCTION__, port->number);
726 dbg("%s - write request of 0 bytes", __FUNCTION__);
731 spin_lock_irqsave(&info->lock, flags);
732 if (list_empty(&info->tx_urbs_free)) {
733 spin_unlock_irqrestore(&info->lock, flags);
736 tmp = list_first(&info->tx_urbs_free);
738 spin_unlock_irqrestore(&info->lock, flags);
740 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
742 bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count;
743 memcpy (urb->transfer_buffer, buf + sent, bytes);
745 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, bytes, urb->transfer_buffer);
747 urb->dev = serial->dev;
748 urb->transfer_buffer_length = bytes;
749 result = usb_submit_urb(urb, GFP_ATOMIC);
751 err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
753 spin_lock_irqsave(&info->lock, flags);
754 list_add(tmp, &info->tx_urbs_free);
755 spin_unlock_irqrestore(&info->lock, flags);
760 spin_lock_irqsave(&info->lock, flags);
761 list_add(tmp, &info->tx_urbs_submitted);
762 spin_unlock_irqrestore(&info->lock, flags);
770 static int whiteheat_write_room(struct usb_serial_port *port)
772 struct whiteheat_private *info = usb_get_serial_port_data(port);
773 struct list_head *tmp;
777 dbg("%s - port %d", __FUNCTION__, port->number);
779 spin_lock_irqsave(&info->lock, flags);
780 list_for_each(tmp, &info->tx_urbs_free)
782 spin_unlock_irqrestore(&info->lock, flags);
783 room *= port->bulk_out_size;
785 dbg("%s - returns %d", __FUNCTION__, room);
790 static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file)
792 struct whiteheat_private *info = usb_get_serial_port_data(port);
793 unsigned int modem_signals = 0;
795 dbg("%s - port %d", __FUNCTION__, port->number);
797 firm_get_dtr_rts(port);
798 if (info->mcr & UART_MCR_DTR)
799 modem_signals |= TIOCM_DTR;
800 if (info->mcr & UART_MCR_RTS)
801 modem_signals |= TIOCM_RTS;
803 return modem_signals;
807 static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file,
808 unsigned int set, unsigned int clear)
810 struct whiteheat_private *info = usb_get_serial_port_data(port);
812 dbg("%s - port %d", __FUNCTION__, port->number);
815 info->mcr |= UART_MCR_RTS;
817 info->mcr |= UART_MCR_DTR;
819 if (clear & TIOCM_RTS)
820 info->mcr &= ~UART_MCR_RTS;
821 if (clear & TIOCM_DTR)
822 info->mcr &= ~UART_MCR_DTR;
824 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
825 firm_set_rts(port, info->mcr & UART_MCR_RTS);
830 static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
832 struct serial_struct serstruct;
833 void __user *user_arg = (void __user *)arg;
835 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
839 memset(&serstruct, 0, sizeof(serstruct));
840 serstruct.type = PORT_16654;
841 serstruct.line = port->serial->minor;
842 serstruct.port = port->number;
843 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
844 serstruct.xmit_fifo_size = port->bulk_out_size;
845 serstruct.custom_divisor = 0;
846 serstruct.baud_base = 460800;
847 serstruct.close_delay = CLOSING_DELAY;
848 serstruct.closing_wait = CLOSING_DELAY;
850 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
856 if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
860 * For now this is ignored. dip sets the ASYNC_[V]HI flags
861 * but this isn't used by us at all. Maybe someone somewhere
862 * will need the custom_divisor setting.
875 static void whiteheat_set_termios (struct usb_serial_port *port, struct ktermios *old_termios)
877 dbg("%s -port %d", __FUNCTION__, port->number);
879 if ((!port->tty) || (!port->tty->termios)) {
880 dbg("%s - no tty structures", __FUNCTION__);
884 /* check that they really want us to change something */
886 if ((port->tty->termios->c_cflag == old_termios->c_cflag) &&
887 (port->tty->termios->c_iflag == old_termios->c_iflag)) {
888 dbg("%s - nothing to change...", __FUNCTION__);
893 firm_setup_port(port);
900 static void whiteheat_break_ctl(struct usb_serial_port *port, int break_state) {
901 firm_set_break(port, break_state);
905 static int whiteheat_chars_in_buffer(struct usb_serial_port *port)
907 struct whiteheat_private *info = usb_get_serial_port_data(port);
908 struct list_head *tmp;
909 struct whiteheat_urb_wrap *wrap;
913 dbg("%s - port %d", __FUNCTION__, port->number);
915 spin_lock_irqsave(&info->lock, flags);
916 list_for_each(tmp, &info->tx_urbs_submitted) {
917 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
918 chars += wrap->urb->transfer_buffer_length;
920 spin_unlock_irqrestore(&info->lock, flags);
922 dbg ("%s - returns %d", __FUNCTION__, chars);
927 static void whiteheat_throttle (struct usb_serial_port *port)
929 struct whiteheat_private *info = usb_get_serial_port_data(port);
932 dbg("%s - port %d", __FUNCTION__, port->number);
934 spin_lock_irqsave(&info->lock, flags);
935 info->flags |= THROTTLED;
936 spin_unlock_irqrestore(&info->lock, flags);
942 static void whiteheat_unthrottle (struct usb_serial_port *port)
944 struct whiteheat_private *info = usb_get_serial_port_data(port);
945 int actually_throttled;
948 dbg("%s - port %d", __FUNCTION__, port->number);
950 spin_lock_irqsave(&info->lock, flags);
951 actually_throttled = info->flags & ACTUALLY_THROTTLED;
952 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
953 spin_unlock_irqrestore(&info->lock, flags);
955 if (actually_throttled)
956 rx_data_softint(&info->rx_work);
962 /*****************************************************************************
963 * Connect Tech's White Heat callback routines
964 *****************************************************************************/
965 static void command_port_write_callback (struct urb *urb)
967 dbg("%s", __FUNCTION__);
970 dbg ("nonzero urb status: %d", urb->status);
976 static void command_port_read_callback (struct urb *urb)
978 struct usb_serial_port *command_port = (struct usb_serial_port *)urb->context;
979 struct whiteheat_command_private *command_info;
980 unsigned char *data = urb->transfer_buffer;
984 dbg("%s", __FUNCTION__);
987 dbg("%s - nonzero urb status: %d", __FUNCTION__, urb->status);
991 usb_serial_debug_data(debug, &command_port->dev, __FUNCTION__, urb->actual_length, data);
993 command_info = usb_get_serial_port_data(command_port);
995 dbg ("%s - command_info is NULL, exiting.", __FUNCTION__);
998 spin_lock_irqsave(&command_info->lock, flags);
1000 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1001 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1002 wake_up_interruptible(&command_info->wait_command);
1003 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1004 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1005 wake_up_interruptible(&command_info->wait_command);
1006 } else if (data[0] == WHITEHEAT_EVENT) {
1007 /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */
1008 dbg("%s - event received", __FUNCTION__);
1009 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
1010 memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1);
1011 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1012 wake_up_interruptible(&command_info->wait_command);
1014 dbg("%s - bad reply from firmware", __FUNCTION__);
1017 /* Continue trying to always read */
1018 command_port->read_urb->dev = command_port->serial->dev;
1019 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
1020 spin_unlock_irqrestore(&command_info->lock, flags);
1022 dbg("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1026 static void whiteheat_read_callback(struct urb *urb)
1028 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1029 struct whiteheat_urb_wrap *wrap;
1030 unsigned char *data = urb->transfer_buffer;
1031 struct whiteheat_private *info = usb_get_serial_port_data(port);
1033 dbg("%s - port %d", __FUNCTION__, port->number);
1035 spin_lock(&info->lock);
1036 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1038 spin_unlock(&info->lock);
1039 err("%s - Not my urb!", __FUNCTION__);
1042 list_del(&wrap->list);
1043 spin_unlock(&info->lock);
1046 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
1047 spin_lock(&info->lock);
1048 list_add(&wrap->list, &info->rx_urbs_free);
1049 spin_unlock(&info->lock);
1053 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
1055 spin_lock(&info->lock);
1056 list_add_tail(&wrap->list, &info->rx_urb_q);
1057 if (info->flags & THROTTLED) {
1058 info->flags |= ACTUALLY_THROTTLED;
1059 spin_unlock(&info->lock);
1062 spin_unlock(&info->lock);
1064 schedule_work(&info->rx_work);
1068 static void whiteheat_write_callback(struct urb *urb)
1070 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1071 struct whiteheat_private *info = usb_get_serial_port_data(port);
1072 struct whiteheat_urb_wrap *wrap;
1074 dbg("%s - port %d", __FUNCTION__, port->number);
1076 spin_lock(&info->lock);
1077 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1079 spin_unlock(&info->lock);
1080 err("%s - Not my urb!", __FUNCTION__);
1083 list_move(&wrap->list, &info->tx_urbs_free);
1084 spin_unlock(&info->lock);
1087 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1091 usb_serial_port_softint(port);
1095 /*****************************************************************************
1096 * Connect Tech's White Heat firmware interface
1097 *****************************************************************************/
1098 static int firm_send_command (struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize)
1100 struct usb_serial_port *command_port;
1101 struct whiteheat_command_private *command_info;
1102 struct whiteheat_private *info;
1103 __u8 *transfer_buffer;
1105 unsigned long flags;
1107 dbg("%s - command %d", __FUNCTION__, command);
1109 command_port = port->serial->port[COMMAND_PORT];
1110 command_info = usb_get_serial_port_data(command_port);
1111 spin_lock_irqsave(&command_info->lock, flags);
1112 command_info->command_finished = FALSE;
1114 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1115 transfer_buffer[0] = command;
1116 memcpy (&transfer_buffer[1], data, datasize);
1117 command_port->write_urb->transfer_buffer_length = datasize + 1;
1118 command_port->write_urb->dev = port->serial->dev;
1119 retval = usb_submit_urb (command_port->write_urb, GFP_KERNEL);
1121 dbg("%s - submit urb failed", __FUNCTION__);
1124 spin_unlock_irqrestore(&command_info->lock, flags);
1126 /* wait for the command to complete */
1127 wait_event_interruptible_timeout(command_info->wait_command,
1128 (command_info->command_finished != FALSE), COMMAND_TIMEOUT);
1130 spin_lock_irqsave(&command_info->lock, flags);
1132 if (command_info->command_finished == FALSE) {
1133 dbg("%s - command timed out.", __FUNCTION__);
1134 retval = -ETIMEDOUT;
1138 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1139 dbg("%s - command failed.", __FUNCTION__);
1144 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1145 dbg("%s - command completed.", __FUNCTION__);
1147 case WHITEHEAT_GET_DTR_RTS:
1148 info = usb_get_serial_port_data(port);
1149 memcpy(&info->mcr, command_info->result_buffer, sizeof(struct whiteheat_dr_info));
1155 spin_unlock_irqrestore(&command_info->lock, flags);
1160 static int firm_open(struct usb_serial_port *port) {
1161 struct whiteheat_simple open_command;
1163 open_command.port = port->number - port->serial->minor + 1;
1164 return firm_send_command(port, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command));
1168 static int firm_close(struct usb_serial_port *port) {
1169 struct whiteheat_simple close_command;
1171 close_command.port = port->number - port->serial->minor + 1;
1172 return firm_send_command(port, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command));
1176 static int firm_setup_port(struct usb_serial_port *port) {
1177 struct whiteheat_port_settings port_settings;
1178 unsigned int cflag = port->tty->termios->c_cflag;
1180 port_settings.port = port->number + 1;
1182 /* get the byte size */
1183 switch (cflag & CSIZE) {
1184 case CS5: port_settings.bits = 5; break;
1185 case CS6: port_settings.bits = 6; break;
1186 case CS7: port_settings.bits = 7; break;
1188 case CS8: port_settings.bits = 8; break;
1190 dbg("%s - data bits = %d", __FUNCTION__, port_settings.bits);
1192 /* determine the parity */
1196 port_settings.parity = WHITEHEAT_PAR_MARK;
1198 port_settings.parity = WHITEHEAT_PAR_SPACE;
1201 port_settings.parity = WHITEHEAT_PAR_ODD;
1203 port_settings.parity = WHITEHEAT_PAR_EVEN;
1205 port_settings.parity = WHITEHEAT_PAR_NONE;
1206 dbg("%s - parity = %c", __FUNCTION__, port_settings.parity);
1208 /* figure out the stop bits requested */
1210 port_settings.stop = 2;
1212 port_settings.stop = 1;
1213 dbg("%s - stop bits = %d", __FUNCTION__, port_settings.stop);
1215 /* figure out the flow control settings */
1216 if (cflag & CRTSCTS)
1217 port_settings.hflow = (WHITEHEAT_HFLOW_CTS | WHITEHEAT_HFLOW_RTS);
1219 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1220 dbg("%s - hardware flow control = %s %s %s %s", __FUNCTION__,
1221 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1222 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1223 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1224 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1226 /* determine software flow control */
1227 if (I_IXOFF(port->tty))
1228 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1230 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1231 dbg("%s - software flow control = %c", __FUNCTION__, port_settings.sflow);
1233 port_settings.xon = START_CHAR(port->tty);
1234 port_settings.xoff = STOP_CHAR(port->tty);
1235 dbg("%s - XON = %2x, XOFF = %2x", __FUNCTION__, port_settings.xon, port_settings.xoff);
1237 /* get the baud rate wanted */
1238 port_settings.baud = tty_get_baud_rate(port->tty);
1239 dbg("%s - baud rate = %d", __FUNCTION__, port_settings.baud);
1241 /* handle any settings that aren't specified in the tty structure */
1242 port_settings.lloop = 0;
1244 /* now send the message to the device */
1245 return firm_send_command(port, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings));
1249 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) {
1250 struct whiteheat_set_rdb rts_command;
1252 rts_command.port = port->number - port->serial->minor + 1;
1253 rts_command.state = onoff;
1254 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&rts_command, sizeof(rts_command));
1258 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) {
1259 struct whiteheat_set_rdb dtr_command;
1261 dtr_command.port = port->number - port->serial->minor + 1;
1262 dtr_command.state = onoff;
1263 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&dtr_command, sizeof(dtr_command));
1267 static int firm_set_break(struct usb_serial_port *port, __u8 onoff) {
1268 struct whiteheat_set_rdb break_command;
1270 break_command.port = port->number - port->serial->minor + 1;
1271 break_command.state = onoff;
1272 return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&break_command, sizeof(break_command));
1276 static int firm_purge(struct usb_serial_port *port, __u8 rxtx) {
1277 struct whiteheat_purge purge_command;
1279 purge_command.port = port->number - port->serial->minor + 1;
1280 purge_command.what = rxtx;
1281 return firm_send_command(port, WHITEHEAT_PURGE, (__u8 *)&purge_command, sizeof(purge_command));
1285 static int firm_get_dtr_rts(struct usb_serial_port *port) {
1286 struct whiteheat_simple get_dr_command;
1288 get_dr_command.port = port->number - port->serial->minor + 1;
1289 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, (__u8 *)&get_dr_command, sizeof(get_dr_command));
1293 static int firm_report_tx_done(struct usb_serial_port *port) {
1294 struct whiteheat_simple close_command;
1296 close_command.port = port->number - port->serial->minor + 1;
1297 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, (__u8 *)&close_command, sizeof(close_command));
1301 /*****************************************************************************
1302 * Connect Tech's White Heat utility functions
1303 *****************************************************************************/
1304 static int start_command_port(struct usb_serial *serial)
1306 struct usb_serial_port *command_port;
1307 struct whiteheat_command_private *command_info;
1308 unsigned long flags;
1311 command_port = serial->port[COMMAND_PORT];
1312 command_info = usb_get_serial_port_data(command_port);
1313 spin_lock_irqsave(&command_info->lock, flags);
1314 if (!command_info->port_running) {
1315 /* Work around HCD bugs */
1316 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1318 command_port->read_urb->dev = serial->dev;
1319 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1321 err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
1325 command_info->port_running++;
1328 spin_unlock_irqrestore(&command_info->lock, flags);
1333 static void stop_command_port(struct usb_serial *serial)
1335 struct usb_serial_port *command_port;
1336 struct whiteheat_command_private *command_info;
1337 unsigned long flags;
1339 command_port = serial->port[COMMAND_PORT];
1340 command_info = usb_get_serial_port_data(command_port);
1341 spin_lock_irqsave(&command_info->lock, flags);
1342 command_info->port_running--;
1343 if (!command_info->port_running)
1344 usb_kill_urb(command_port->read_urb);
1345 spin_unlock_irqrestore(&command_info->lock, flags);
1349 static int start_port_read(struct usb_serial_port *port)
1351 struct whiteheat_private *info = usb_get_serial_port_data(port);
1352 struct whiteheat_urb_wrap *wrap;
1355 unsigned long flags;
1356 struct list_head *tmp;
1357 struct list_head *tmp2;
1359 spin_lock_irqsave(&info->lock, flags);
1361 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1363 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1365 urb->dev = port->serial->dev;
1366 retval = usb_submit_urb(urb, GFP_KERNEL);
1368 list_add(tmp, &info->rx_urbs_free);
1369 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1370 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1373 list_move(tmp, &info->rx_urbs_free);
1377 list_add(tmp, &info->rx_urbs_submitted);
1380 spin_unlock_irqrestore(&info->lock, flags);
1386 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb* urb, struct list_head *head)
1388 struct whiteheat_urb_wrap *wrap;
1389 struct list_head *tmp;
1391 list_for_each(tmp, head) {
1392 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1393 if (wrap->urb == urb)
1401 static struct list_head *list_first(struct list_head *head)
1407 static void rx_data_softint(struct work_struct *work)
1409 struct whiteheat_private *info =
1410 container_of(work, struct whiteheat_private, rx_work);
1411 struct usb_serial_port *port = info->port;
1412 struct tty_struct *tty = port->tty;
1413 struct whiteheat_urb_wrap *wrap;
1415 unsigned long flags;
1416 struct list_head *tmp;
1417 struct list_head *tmp2;
1421 spin_lock_irqsave(&info->lock, flags);
1422 if (info->flags & THROTTLED) {
1423 spin_unlock_irqrestore(&info->lock, flags);
1427 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1429 spin_unlock_irqrestore(&info->lock, flags);
1431 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1434 if (tty && urb->actual_length) {
1435 int len = tty_buffer_request_room(tty, urb->actual_length);
1436 /* This stuff can go away now I suspect */
1437 if (unlikely(len < urb->actual_length)) {
1438 spin_lock_irqsave(&info->lock, flags);
1439 list_add(tmp, &info->rx_urb_q);
1440 spin_unlock_irqrestore(&info->lock, flags);
1441 tty_flip_buffer_push(tty);
1442 schedule_work(&info->rx_work);
1445 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1449 urb->dev = port->serial->dev;
1450 result = usb_submit_urb(urb, GFP_ATOMIC);
1452 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1453 spin_lock_irqsave(&info->lock, flags);
1454 list_add(tmp, &info->rx_urbs_free);
1458 spin_lock_irqsave(&info->lock, flags);
1459 list_add(tmp, &info->rx_urbs_submitted);
1461 spin_unlock_irqrestore(&info->lock, flags);
1464 tty_flip_buffer_push(tty);
1468 /*****************************************************************************
1469 * Connect Tech's White Heat module functions
1470 *****************************************************************************/
1471 static int __init whiteheat_init (void)
1474 retval = usb_serial_register(&whiteheat_fake_device);
1476 goto failed_fake_register;
1477 retval = usb_serial_register(&whiteheat_device);
1479 goto failed_device_register;
1480 retval = usb_register(&whiteheat_driver);
1482 goto failed_usb_register;
1483 info(DRIVER_DESC " " DRIVER_VERSION);
1485 failed_usb_register:
1486 usb_serial_deregister(&whiteheat_device);
1487 failed_device_register:
1488 usb_serial_deregister(&whiteheat_fake_device);
1489 failed_fake_register:
1494 static void __exit whiteheat_exit (void)
1496 usb_deregister (&whiteheat_driver);
1497 usb_serial_deregister (&whiteheat_fake_device);
1498 usb_serial_deregister (&whiteheat_device);
1502 module_init(whiteheat_init);
1503 module_exit(whiteheat_exit);
1505 MODULE_AUTHOR( DRIVER_AUTHOR );
1506 MODULE_DESCRIPTION( DRIVER_DESC );
1507 MODULE_LICENSE("GPL");
1509 module_param(urb_pool_size, int, 0);
1510 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1512 module_param(debug, bool, S_IRUGO | S_IWUSR);
1513 MODULE_PARM_DESC(debug, "Debug enabled or not");