2 * USB ConnectTech WhiteHEAT driver
4 * Copyright (C) 1999 - 2001
5 * Greg Kroah-Hartman (greg@kroah.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * See Documentation/usb/usb-serial.txt for more information on using this driver
15 * switched from using spinlock to a semaphore, which fixes lots of problems.
18 * Identify version on module load.
21 * Fixed MOD_INC and MOD_DEC logic, the ability to open a port more
22 * than once, and the got the proper usb_device_id table entries so
23 * the driver works again.
25 * (11/01/2000) Adam J. Richter
26 * usb_device_id table support
29 * Fixed bug with urb->dev not being set properly, now that the usb
33 * firmware is improved to guard against crap sent to device
34 * firmware now replies CMD_FAILURE on bad things
35 * read_callback fix you provided for private info struct
36 * command_finished now indicates success or fail
37 * setup_port struct now packed to avoid gcc padding
38 * firmware uses 1 based port numbering, driver now handles that
41 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
44 * Added module_init and module_exit functions to handle the fact that this
45 * driver is a loadable module now.
46 * Fixed bug with port->minor that was found by Al Borchers
49 * Added support for port settings. Baud rate can now be changed. Line signals
50 * are not transferred to and from the tty layer yet, but things seem to be
54 * First cut at open and close commands. Data can flow through the ports at
58 * Split driver up into device specific pieces.
62 #include <linux/config.h>
63 #include <linux/kernel.h>
64 #include <linux/errno.h>
65 #include <linux/init.h>
66 #include <linux/slab.h>
67 #include <linux/tty.h>
68 #include <linux/tty_driver.h>
69 #include <linux/tty_flip.h>
70 #include <linux/module.h>
71 #include <linux/spinlock.h>
72 #include <asm/uaccess.h>
73 #include <linux/usb.h>
75 #ifdef CONFIG_USB_SERIAL_DEBUG
81 #include "usb-serial.h"
82 #include "whiteheat_fw.h" /* firmware for the ConnectTech WhiteHEAT device */
83 #include "whiteheat.h" /* WhiteHEAT specific commands */
88 #define DRIVER_VERSION "v1.2"
89 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
90 #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
92 #define CONNECT_TECH_VENDOR_ID 0x0710
93 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
94 #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
97 ID tables for whiteheat are unusual, because we want to different
98 things for different versions of the device. Eventually, this
99 will be doable from a single table. But, for now, we define two
100 separate ID tables, and then a third table that combines them
101 just for the purpose of exporting the autoloading information.
103 static struct usb_device_id id_table_std [] = {
104 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
105 { } /* Terminating entry */
108 static struct usb_device_id id_table_prerenumeration [] = {
109 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
110 { } /* Terminating entry */
113 static __devinitdata struct usb_device_id id_table_combined [] = {
114 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
115 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
116 { } /* Terminating entry */
119 MODULE_DEVICE_TABLE (usb, id_table_combined);
121 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
122 static int whiteheat_open (struct usb_serial_port *port, struct file *filp);
123 static void whiteheat_close (struct usb_serial_port *port, struct file *filp);
124 static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
125 static void whiteheat_set_termios (struct usb_serial_port *port, struct termios * old);
126 static void whiteheat_throttle (struct usb_serial_port *port);
127 static void whiteheat_unthrottle (struct usb_serial_port *port);
128 static int whiteheat_fake_startup (struct usb_serial *serial);
129 static int whiteheat_real_startup (struct usb_serial *serial);
130 static void whiteheat_real_shutdown (struct usb_serial *serial);
132 static struct usb_serial_device_type whiteheat_fake_device = {
133 .owner = THIS_MODULE,
134 .name = "Connect Tech - WhiteHEAT - (prerenumeration)",
135 .id_table = id_table_prerenumeration,
136 .num_interrupt_in = NUM_DONT_CARE,
137 .num_bulk_in = NUM_DONT_CARE,
138 .num_bulk_out = NUM_DONT_CARE,
140 .startup = whiteheat_fake_startup,
143 static struct usb_serial_device_type whiteheat_device = {
144 .owner = THIS_MODULE,
145 .name = "Connect Tech - WhiteHEAT",
146 .id_table = id_table_std,
147 .num_interrupt_in = NUM_DONT_CARE,
148 .num_bulk_in = NUM_DONT_CARE,
149 .num_bulk_out = NUM_DONT_CARE,
151 .open = whiteheat_open,
152 .close = whiteheat_close,
153 .throttle = whiteheat_throttle,
154 .unthrottle = whiteheat_unthrottle,
155 .ioctl = whiteheat_ioctl,
156 .set_termios = whiteheat_set_termios,
157 .startup = whiteheat_real_startup,
158 .shutdown = whiteheat_real_shutdown,
161 struct whiteheat_private {
162 __u8 command_finished;
163 wait_queue_head_t wait_command; /* for handling sleeping while waiting for a command to finish */
167 /* local function prototypes */
168 static inline void set_rts (struct usb_serial_port *port, unsigned char rts);
169 static inline void set_dtr (struct usb_serial_port *port, unsigned char dtr);
170 static inline void set_break (struct usb_serial_port *port, unsigned char brk);
174 #define COMMAND_PORT 4
175 #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
177 /*****************************************************************************
178 * Connect Tech's White Heat specific driver functions
179 *****************************************************************************/
180 static void command_port_write_callback (struct urb *urb)
182 dbg("%s", __FUNCTION__);
185 dbg ("nonzero urb status: %d", urb->status);
189 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
195 static void command_port_read_callback (struct urb *urb)
197 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
198 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
199 struct whiteheat_private *info;
200 unsigned char *data = urb->transfer_buffer;
203 dbg("%s", __FUNCTION__);
206 dbg("%s - nonzero urb status: %d", __FUNCTION__, urb->status);
211 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
215 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
217 info = (struct whiteheat_private *)port->private;
219 dbg("%s - info is NULL, exiting.", __FUNCTION__);
223 /* right now, if the command is COMMAND_COMPLETE, just flip the bit saying the command finished */
224 /* in the future we're going to have to pay attention to the actual command that completed */
225 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
226 info->command_finished = WHITEHEAT_CMD_COMPLETE;
227 wake_up_interruptible(&info->wait_command);
230 if (data[0] == WHITEHEAT_CMD_FAILURE) {
231 info->command_finished = WHITEHEAT_CMD_FAILURE;
232 wake_up_interruptible(&info->wait_command);
235 /* Continue trying to always read */
236 FILL_BULK_URB(port->read_urb, serial->dev,
237 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
238 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
239 command_port_read_callback, port);
240 result = usb_submit_urb(port->read_urb);
242 dbg("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
246 static int whiteheat_send_cmd (struct usb_serial *serial, __u8 command, __u8 *data, __u8 datasize)
248 struct whiteheat_private *info;
249 struct usb_serial_port *port;
251 __u8 *transfer_buffer;
254 dbg("%s - command %d", __FUNCTION__, command);
256 port = &serial->port[COMMAND_PORT];
257 info = (struct whiteheat_private *)port->private;
258 info->command_finished = FALSE;
260 transfer_buffer = (__u8 *)port->write_urb->transfer_buffer;
261 transfer_buffer[0] = command;
262 memcpy (&transfer_buffer[1], data, datasize);
263 port->write_urb->transfer_buffer_length = datasize + 1;
264 port->write_urb->dev = serial->dev;
265 retval = usb_submit_urb (port->write_urb);
267 dbg("%s - submit urb failed", __FUNCTION__);
271 /* wait for the command to complete */
272 timeout = COMMAND_TIMEOUT;
273 while (timeout && (info->command_finished == FALSE)) {
274 timeout = interruptible_sleep_on_timeout (&info->wait_command, timeout);
277 if (info->command_finished == FALSE) {
278 dbg("%s - command timed out.", __FUNCTION__);
283 if (info->command_finished == WHITEHEAT_CMD_FAILURE) {
284 dbg("%s - command failed.", __FUNCTION__);
289 if (info->command_finished == WHITEHEAT_CMD_COMPLETE)
290 dbg("%s - command completed.", __FUNCTION__);
297 static int whiteheat_open (struct usb_serial_port *port, struct file *filp)
299 struct whiteheat_min_set open_command;
300 struct usb_serial_port *command_port;
301 struct whiteheat_private *info;
304 dbg("%s - port %d", __FUNCTION__, port->number);
306 /* set up some stuff for our command port */
307 command_port = &port->serial->port[COMMAND_PORT];
308 if (command_port->private == NULL) {
309 info = (struct whiteheat_private *)kmalloc (sizeof(struct whiteheat_private), GFP_KERNEL);
311 err("%s - out of memory", __FUNCTION__);
316 init_waitqueue_head(&info->wait_command);
317 command_port->private = info;
318 command_port->write_urb->complete = command_port_write_callback;
319 command_port->read_urb->complete = command_port_read_callback;
320 command_port->read_urb->dev = port->serial->dev;
321 command_port->tty = port->tty; /* need this to "fake" our our sanity check macros */
322 retval = usb_submit_urb (command_port->read_urb);
324 err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
329 /* Start reading from the device */
330 port->read_urb->dev = port->serial->dev;
331 retval = usb_submit_urb(port->read_urb);
333 err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
337 /* send an open port command */
338 /* firmware uses 1 based port numbering */
339 open_command.port = port->number - port->serial->minor + 1;
340 retval = whiteheat_send_cmd (port->serial, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command));
344 /* Need to do device specific setup here (control lines, baud rate, etc.) */
348 dbg("%s - exit, retval = %d", __FUNCTION__, retval);
353 static void whiteheat_close(struct usb_serial_port *port, struct file * filp)
355 struct whiteheat_min_set close_command;
357 dbg("%s - port %d", __FUNCTION__, port->number);
359 /* send a close command to the port */
360 /* firmware uses 1 based port numbering */
361 close_command.port = port->number - port->serial->minor + 1;
362 whiteheat_send_cmd (port->serial, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command));
364 /* Need to change the control lines here */
367 /* shutdown our bulk reads and writes */
368 usb_unlink_urb (port->write_urb);
369 usb_unlink_urb (port->read_urb);
373 static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
375 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
381 static void whiteheat_set_termios (struct usb_serial_port *port, struct termios *old_termios)
384 struct whiteheat_port_settings port_settings;
386 dbg("%s -port %d", __FUNCTION__, port->number);
388 if ((!port->tty) || (!port->tty->termios)) {
389 dbg("%s - no tty structures", __FUNCTION__);
393 cflag = port->tty->termios->c_cflag;
394 /* check that they really want us to change something */
396 if ((cflag == old_termios->c_cflag) &&
397 (RELEVANT_IFLAG(port->tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
398 dbg("%s - nothing to change...", __FUNCTION__);
403 /* set the port number */
404 /* firmware uses 1 based port numbering */
405 port_settings.port = port->number + 1;
407 /* get the byte size */
408 switch (cflag & CSIZE) {
409 case CS5: port_settings.bits = 5; break;
410 case CS6: port_settings.bits = 6; break;
411 case CS7: port_settings.bits = 7; break;
413 case CS8: port_settings.bits = 8; break;
415 dbg("%s - data bits = %d", __FUNCTION__, port_settings.bits);
417 /* determine the parity */
420 port_settings.parity = 'o';
422 port_settings.parity = 'e';
424 port_settings.parity = 'n';
425 dbg("%s - parity = %c", __FUNCTION__, port_settings.parity);
427 /* figure out the stop bits requested */
429 port_settings.stop = 2;
431 port_settings.stop = 1;
432 dbg("%s - stop bits = %d", __FUNCTION__, port_settings.stop);
435 /* figure out the flow control settings */
437 port_settings.hflow = (WHITEHEAT_CTS_FLOW | WHITEHEAT_RTS_FLOW);
439 port_settings.hflow = 0;
440 dbg("%s - hardware flow control = %s %s %s %s", __FUNCTION__,
441 (port_settings.hflow & WHITEHEAT_CTS_FLOW) ? "CTS" : "",
442 (port_settings.hflow & WHITEHEAT_RTS_FLOW) ? "RTS" : "",
443 (port_settings.hflow & WHITEHEAT_DSR_FLOW) ? "DSR" : "",
444 (port_settings.hflow & WHITEHEAT_DTR_FLOW) ? "DTR" : "");
446 /* determine software flow control */
447 if (I_IXOFF(port->tty))
448 port_settings.sflow = 'b';
450 port_settings.sflow = 'n';
451 dbg("%s - software flow control = %c", __FUNCTION__, port_settings.sflow);
453 port_settings.xon = START_CHAR(port->tty);
454 port_settings.xoff = STOP_CHAR(port->tty);
455 dbg("%s - XON = %2x, XOFF = %2x", __FUNCTION__, port_settings.xon, port_settings.xoff);
457 /* get the baud rate wanted */
458 port_settings.baud = tty_get_baud_rate(port->tty);
459 dbg("%s - baud rate = %d", __FUNCTION__, port_settings.baud);
461 /* handle any settings that aren't specified in the tty structure */
462 port_settings.lloop = 0;
464 /* now send the message to the device */
465 whiteheat_send_cmd (port->serial, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings));
472 static void whiteheat_throttle (struct usb_serial_port *port)
474 dbg("%s - port %d", __FUNCTION__, port->number);
476 /* Change the control signals */
483 static void whiteheat_unthrottle (struct usb_serial_port *port)
485 dbg("%s - port %d", __FUNCTION__, port->number);
487 /* Change the control signals */
494 /* steps to download the firmware to the WhiteHEAT device:
495 - hold the reset (by writing to the reset bit of the CPUCS register)
496 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
497 - release the reset (by writing to the CPUCS register)
498 - download the WH.HEX file for all addresses greater than 0x1b3f using
499 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
501 - download the WH.HEX file for all addresses less than 0x1b40 using
502 VENDOR_REQUEST_ANCHOR_LOAD
504 - device renumerated itself and comes up as new device id with all
505 firmware download completed.
507 static int whiteheat_fake_startup (struct usb_serial *serial)
510 const struct whiteheat_hex_record *record;
512 dbg("%s", __FUNCTION__);
514 response = ezusb_set_reset (serial, 1);
516 record = &whiteheat_loader[0];
517 while (record->address != 0xffff) {
518 response = ezusb_writememory (serial, record->address,
519 (unsigned char *)record->data, record->data_size, 0xa0);
521 err("%s - ezusb_writememory failed for loader (%d %04X %p %d)",
522 __FUNCTION__, response, record->address, record->data, record->data_size);
528 response = ezusb_set_reset (serial, 0);
530 record = &whiteheat_firmware[0];
531 while (record->address < 0x1b40) {
534 while (record->address != 0xffff) {
535 response = ezusb_writememory (serial, record->address,
536 (unsigned char *)record->data, record->data_size, 0xa3);
538 err("%s - ezusb_writememory failed for first firmware step (%d %04X %p %d)",
539 __FUNCTION__, response, record->address, record->data, record->data_size);
545 response = ezusb_set_reset (serial, 1);
547 record = &whiteheat_firmware[0];
548 while (record->address < 0x1b40) {
549 response = ezusb_writememory (serial, record->address,
550 (unsigned char *)record->data, record->data_size, 0xa0);
552 err("%s - ezusb_writememory failed for second firmware step (%d %04X %p %d)",
553 __FUNCTION__, response, record->address, record->data, record->data_size);
559 response = ezusb_set_reset (serial, 0);
561 /* we want this device to fail to have a driver assigned to it. */
566 static int whiteheat_real_startup (struct usb_serial *serial)
568 struct whiteheat_hw_info *hw_info;
572 __u8 command[2] = { WHITEHEAT_GET_HW_INFO, 0 };
573 __u8 result[sizeof(*hw_info) + 1];
575 pipe = usb_rcvbulkpipe (serial->dev, 7);
576 usb_bulk_msg (serial->dev, pipe, result, sizeof(result), &alen, 2 * HZ);
578 * We ignore the return code. In the case where rmmod/insmod is
579 * performed with a WhiteHEAT connected, the above times out
580 * because the endpoint is already prepped, meaning the below succeeds
581 * regardless. All other cases the above succeeds.
584 pipe = usb_sndbulkpipe (serial->dev, 7);
585 ret = usb_bulk_msg (serial->dev, pipe, command, sizeof(command), &alen, 2 * HZ);
587 err("%s: Couldn't send command [%d]", serial->type->name, ret);
589 } else if (alen != sizeof(command)) {
590 err("%s: Send command incomplete [%d]", serial->type->name, alen);
594 pipe = usb_rcvbulkpipe (serial->dev, 7);
595 ret = usb_bulk_msg (serial->dev, pipe, result, sizeof(result), &alen, 2 * HZ);
597 err("%s: Couldn't get results [%d]", serial->type->name, ret);
599 } else if (alen != sizeof(result)) {
600 err("%s: Get results incomplete [%d]", serial->type->name, alen);
602 } else if (result[0] != command[0]) {
603 err("%s: Command failed [%d]", serial->type->name, result[0]);
607 hw_info = (struct whiteheat_hw_info *)&result[1];
609 info("%s: Driver %s: Firmware v%d.%02d", serial->type->name,
610 DRIVER_VERSION, hw_info->sw_major_rev, hw_info->sw_minor_rev);
615 err("%s: Unable to retrieve firmware version, try replugging\n", serial->type->name);
617 * Return that we've claimed the interface. A failure here may be
618 * due to interception by the command_callback routine or other
619 * causes that don't mean that the firmware isn't running. This may
620 * change in the future. Probably should actually.
625 static void whiteheat_real_shutdown (struct usb_serial *serial)
627 struct usb_serial_port *command_port;
629 dbg("%s", __FUNCTION__);
631 /* free up our private data for our command port */
632 command_port = &serial->port[COMMAND_PORT];
633 if (command_port->private != NULL) {
634 kfree (command_port->private);
635 command_port->private = NULL;
642 static void set_command (struct usb_serial_port *port, unsigned char state, unsigned char command)
644 struct whiteheat_rdb_set rdb_command;
646 /* send a set rts command to the port */
647 /* firmware uses 1 based port numbering */
648 rdb_command.port = port->number - port->serial->minor + 1;
649 rdb_command.state = state;
651 whiteheat_send_cmd (port->serial, command, (__u8 *)&rdb_command, sizeof(rdb_command));
655 static inline void set_rts (struct usb_serial_port *port, unsigned char rts)
657 set_command (port, rts, WHITEHEAT_SET_RTS);
661 static inline void set_dtr (struct usb_serial_port *port, unsigned char dtr)
663 set_command (port, dtr, WHITEHEAT_SET_DTR);
667 static inline void set_break (struct usb_serial_port *port, unsigned char brk)
669 set_command (port, brk, WHITEHEAT_SET_BREAK);
673 static int __init whiteheat_init (void)
675 usb_serial_register (&whiteheat_fake_device);
676 usb_serial_register (&whiteheat_device);
677 info(DRIVER_DESC " " DRIVER_VERSION);
682 static void __exit whiteheat_exit (void)
684 usb_serial_deregister (&whiteheat_fake_device);
685 usb_serial_deregister (&whiteheat_device);
689 module_init(whiteheat_init);
690 module_exit(whiteheat_exit);
692 MODULE_AUTHOR( DRIVER_AUTHOR );
693 MODULE_DESCRIPTION( DRIVER_DESC );
694 MODULE_LICENSE("GPL");
696 MODULE_PARM(debug, "i");
697 MODULE_PARM_DESC(debug, "Debug enabled or not");