USB: whiteheat: reimplement using generic framework
[linux-2.6.git] / drivers / usb / serial / whiteheat.c
blobc141b472b6a292e7f057bec813f923ceef3aabf5
1 /*
2 * USB ConnectTech WhiteHEAT driver
4 * Copyright (C) 2002
5 * Connect Tech Inc.
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
16 * driver
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 */
39 static bool debug;
41 #ifndef CMSPAR
42 #define CMSPAR 0
43 #endif
46 * Version Information
48 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
49 #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
51 #define CONNECT_TECH_VENDOR_ID 0x0710
52 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
53 #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
56 ID tables for whiteheat are unusual, because we want to different
57 things for different versions of the device. Eventually, this
58 will be doable from a single table. But, for now, we define two
59 separate ID tables, and then a third table that combines them
60 just for the purpose of exporting the autoloading information.
62 static const struct usb_device_id id_table_std[] = {
63 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
64 { } /* Terminating entry */
67 static const struct usb_device_id id_table_prerenumeration[] = {
68 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
69 { } /* Terminating entry */
72 static const struct usb_device_id id_table_combined[] = {
73 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
74 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
75 { } /* Terminating entry */
78 MODULE_DEVICE_TABLE(usb, id_table_combined);
80 static struct usb_driver whiteheat_driver = {
81 .name = "whiteheat",
82 .probe = usb_serial_probe,
83 .disconnect = usb_serial_disconnect,
84 .id_table = id_table_combined,
87 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
88 static int whiteheat_firmware_download(struct usb_serial *serial,
89 const struct usb_device_id *id);
90 static int whiteheat_firmware_attach(struct usb_serial *serial);
92 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
93 static int whiteheat_attach(struct usb_serial *serial);
94 static void whiteheat_release(struct usb_serial *serial);
95 static int whiteheat_open(struct tty_struct *tty,
96 struct usb_serial_port *port);
97 static void whiteheat_close(struct usb_serial_port *port);
98 static int whiteheat_ioctl(struct tty_struct *tty,
99 unsigned int cmd, unsigned long arg);
100 static void whiteheat_set_termios(struct tty_struct *tty,
101 struct usb_serial_port *port, struct ktermios *old);
102 static int whiteheat_tiocmget(struct tty_struct *tty);
103 static int whiteheat_tiocmset(struct tty_struct *tty,
104 unsigned int set, unsigned int clear);
105 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
107 static struct usb_serial_driver whiteheat_fake_device = {
108 .driver = {
109 .owner = THIS_MODULE,
110 .name = "whiteheatnofirm",
112 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
113 .id_table = id_table_prerenumeration,
114 .num_ports = 1,
115 .probe = whiteheat_firmware_download,
116 .attach = whiteheat_firmware_attach,
119 static struct usb_serial_driver whiteheat_device = {
120 .driver = {
121 .owner = THIS_MODULE,
122 .name = "whiteheat",
124 .description = "Connect Tech - WhiteHEAT",
125 .id_table = id_table_std,
126 .num_ports = 4,
127 .attach = whiteheat_attach,
128 .release = whiteheat_release,
129 .open = whiteheat_open,
130 .close = whiteheat_close,
131 .ioctl = whiteheat_ioctl,
132 .set_termios = whiteheat_set_termios,
133 .break_ctl = whiteheat_break_ctl,
134 .tiocmget = whiteheat_tiocmget,
135 .tiocmset = whiteheat_tiocmset,
136 .throttle = usb_serial_generic_throttle,
137 .unthrottle = usb_serial_generic_unthrottle,
140 static struct usb_serial_driver * const serial_drivers[] = {
141 &whiteheat_fake_device, &whiteheat_device, NULL
144 struct whiteheat_command_private {
145 struct mutex mutex;
146 __u8 port_running;
147 __u8 command_finished;
148 wait_queue_head_t wait_command; /* for handling sleeping whilst
149 waiting for a command to
150 finish */
151 __u8 result_buffer[64];
154 struct whiteheat_private {
155 __u8 mcr; /* FIXME: no locking on mcr */
159 /* local function prototypes */
160 static int start_command_port(struct usb_serial *serial);
161 static void stop_command_port(struct usb_serial *serial);
162 static void command_port_write_callback(struct urb *urb);
163 static void command_port_read_callback(struct urb *urb);
165 static int firm_send_command(struct usb_serial_port *port, __u8 command,
166 __u8 *data, __u8 datasize);
167 static int firm_open(struct usb_serial_port *port);
168 static int firm_close(struct usb_serial_port *port);
169 static void firm_setup_port(struct tty_struct *tty);
170 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
171 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
172 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
173 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
174 static int firm_get_dtr_rts(struct usb_serial_port *port);
175 static int firm_report_tx_done(struct usb_serial_port *port);
178 #define COMMAND_PORT 4
179 #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
180 #define COMMAND_TIMEOUT_MS 2000
181 #define CLOSING_DELAY (30 * HZ)
184 /*****************************************************************************
185 * Connect Tech's White Heat prerenumeration driver functions
186 *****************************************************************************/
188 /* steps to download the firmware to the WhiteHEAT device:
189 - hold the reset (by writing to the reset bit of the CPUCS register)
190 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
191 - release the reset (by writing to the CPUCS register)
192 - download the WH.HEX file for all addresses greater than 0x1b3f using
193 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
194 - hold the reset
195 - download the WH.HEX file for all addresses less than 0x1b40 using
196 VENDOR_REQUEST_ANCHOR_LOAD
197 - release the reset
198 - device renumerated itself and comes up as new device id with all
199 firmware download completed.
201 static int whiteheat_firmware_download(struct usb_serial *serial,
202 const struct usb_device_id *id)
204 int response, ret = -ENOENT;
205 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
206 const struct ihex_binrec *record;
208 dbg("%s", __func__);
210 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
211 &serial->dev->dev)) {
212 dev_err(&serial->dev->dev,
213 "%s - request \"whiteheat.fw\" failed\n", __func__);
214 goto out;
216 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
217 &serial->dev->dev)) {
218 dev_err(&serial->dev->dev,
219 "%s - request \"whiteheat_loader.fw\" failed\n",
220 __func__);
221 goto out;
223 ret = 0;
224 response = ezusb_set_reset (serial, 1);
226 record = (const struct ihex_binrec *)loader_fw->data;
227 while (record) {
228 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
229 (unsigned char *)record->data,
230 be16_to_cpu(record->len), 0xa0);
231 if (response < 0) {
232 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
233 "failed for loader (%d %04X %p %d)\n",
234 __func__, response, be32_to_cpu(record->addr),
235 record->data, be16_to_cpu(record->len));
236 break;
238 record = ihex_next_binrec(record);
241 response = ezusb_set_reset(serial, 0);
243 record = (const struct ihex_binrec *)firmware_fw->data;
244 while (record && be32_to_cpu(record->addr) < 0x1b40)
245 record = ihex_next_binrec(record);
246 while (record) {
247 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
248 (unsigned char *)record->data,
249 be16_to_cpu(record->len), 0xa3);
250 if (response < 0) {
251 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
252 "failed for first firmware step "
253 "(%d %04X %p %d)\n", __func__, response,
254 be32_to_cpu(record->addr), record->data,
255 be16_to_cpu(record->len));
256 break;
258 ++record;
261 response = ezusb_set_reset(serial, 1);
263 record = (const struct ihex_binrec *)firmware_fw->data;
264 while (record && be32_to_cpu(record->addr) < 0x1b40) {
265 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
266 (unsigned char *)record->data,
267 be16_to_cpu(record->len), 0xa0);
268 if (response < 0) {
269 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
270 "failed for second firmware step "
271 "(%d %04X %p %d)\n", __func__, response,
272 be32_to_cpu(record->addr), record->data,
273 be16_to_cpu(record->len));
274 break;
276 ++record;
278 ret = 0;
279 response = ezusb_set_reset (serial, 0);
280 out:
281 release_firmware(loader_fw);
282 release_firmware(firmware_fw);
283 return ret;
287 static int whiteheat_firmware_attach(struct usb_serial *serial)
289 /* We want this device to fail to have a driver assigned to it */
290 return 1;
294 /*****************************************************************************
295 * Connect Tech's White Heat serial driver functions
296 *****************************************************************************/
297 static int whiteheat_attach(struct usb_serial *serial)
299 struct usb_serial_port *command_port;
300 struct whiteheat_command_private *command_info;
301 struct usb_serial_port *port;
302 struct whiteheat_private *info;
303 struct whiteheat_hw_info *hw_info;
304 int pipe;
305 int ret;
306 int alen;
307 __u8 *command;
308 __u8 *result;
309 int i;
311 command_port = serial->port[COMMAND_PORT];
313 pipe = usb_sndbulkpipe(serial->dev,
314 command_port->bulk_out_endpointAddress);
315 command = kmalloc(2, GFP_KERNEL);
316 if (!command)
317 goto no_command_buffer;
318 command[0] = WHITEHEAT_GET_HW_INFO;
319 command[1] = 0;
321 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
322 if (!result)
323 goto no_result_buffer;
325 * When the module is reloaded the firmware is still there and
326 * the endpoints are still in the usb core unchanged. This is the
327 * unlinking bug in disguise. Same for the call below.
329 usb_clear_halt(serial->dev, pipe);
330 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
331 &alen, COMMAND_TIMEOUT_MS);
332 if (ret) {
333 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
334 serial->type->description, ret);
335 goto no_firmware;
336 } else if (alen != 2) {
337 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
338 serial->type->description, alen);
339 goto no_firmware;
342 pipe = usb_rcvbulkpipe(serial->dev,
343 command_port->bulk_in_endpointAddress);
344 /* See the comment on the usb_clear_halt() above */
345 usb_clear_halt(serial->dev, pipe);
346 ret = usb_bulk_msg(serial->dev, pipe, result,
347 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
348 if (ret) {
349 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
350 serial->type->description, ret);
351 goto no_firmware;
352 } else if (alen != sizeof(*hw_info) + 1) {
353 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
354 serial->type->description, alen);
355 goto no_firmware;
356 } else if (result[0] != command[0]) {
357 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
358 serial->type->description, result[0]);
359 goto no_firmware;
362 hw_info = (struct whiteheat_hw_info *)&result[1];
364 dev_info(&serial->dev->dev, "%s: Firmware v%d.%02d\n",
365 serial->type->description,
366 hw_info->sw_major_rev, hw_info->sw_minor_rev);
368 for (i = 0; i < serial->num_ports; i++) {
369 port = serial->port[i];
371 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
372 if (info == NULL) {
373 dev_err(&port->dev,
374 "%s: Out of memory for port structures\n",
375 serial->type->description);
376 goto no_private;
379 info->mcr = 0;
381 usb_set_serial_port_data(port, info);
384 command_info = kmalloc(sizeof(struct whiteheat_command_private),
385 GFP_KERNEL);
386 if (command_info == NULL) {
387 dev_err(&serial->dev->dev,
388 "%s: Out of memory for port structures\n",
389 serial->type->description);
390 goto no_command_private;
393 mutex_init(&command_info->mutex);
394 command_info->port_running = 0;
395 init_waitqueue_head(&command_info->wait_command);
396 usb_set_serial_port_data(command_port, command_info);
397 command_port->write_urb->complete = command_port_write_callback;
398 command_port->read_urb->complete = command_port_read_callback;
399 kfree(result);
400 kfree(command);
402 return 0;
404 no_firmware:
405 /* Firmware likely not running */
406 dev_err(&serial->dev->dev,
407 "%s: Unable to retrieve firmware version, try replugging\n",
408 serial->type->description);
409 dev_err(&serial->dev->dev,
410 "%s: If the firmware is not running (status led not blinking)\n",
411 serial->type->description);
412 dev_err(&serial->dev->dev,
413 "%s: please contact support@connecttech.com\n",
414 serial->type->description);
415 kfree(result);
416 return -ENODEV;
418 no_command_private:
419 for (i = serial->num_ports - 1; i >= 0; i--) {
420 port = serial->port[i];
421 info = usb_get_serial_port_data(port);
422 kfree(info);
423 no_private:
426 kfree(result);
427 no_result_buffer:
428 kfree(command);
429 no_command_buffer:
430 return -ENOMEM;
434 static void whiteheat_release(struct usb_serial *serial)
436 struct usb_serial_port *command_port;
437 struct whiteheat_private *info;
438 int i;
440 dbg("%s", __func__);
442 /* free up our private data for our command port */
443 command_port = serial->port[COMMAND_PORT];
444 kfree(usb_get_serial_port_data(command_port));
446 for (i = 0; i < serial->num_ports; i++) {
447 info = usb_get_serial_port_data(serial->port[i]);
448 kfree(info);
452 static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
454 int retval;
456 dbg("%s - port %d", __func__, port->number);
458 retval = start_command_port(port->serial);
459 if (retval)
460 goto exit;
462 /* send an open port command */
463 retval = firm_open(port);
464 if (retval) {
465 stop_command_port(port->serial);
466 goto exit;
469 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
470 if (retval) {
471 firm_close(port);
472 stop_command_port(port->serial);
473 goto exit;
476 if (tty)
477 firm_setup_port(tty);
479 /* Work around HCD bugs */
480 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
481 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
483 retval = usb_serial_generic_open(tty, port);
484 if (retval) {
485 firm_close(port);
486 stop_command_port(port->serial);
487 goto exit;
489 exit:
490 dbg("%s - exit, retval = %d", __func__, retval);
491 return retval;
495 static void whiteheat_close(struct usb_serial_port *port)
497 dbg("%s - port %d", __func__, port->number);
499 firm_report_tx_done(port);
500 firm_close(port);
502 usb_serial_generic_close(port);
504 stop_command_port(port->serial);
507 static int whiteheat_tiocmget(struct tty_struct *tty)
509 struct usb_serial_port *port = tty->driver_data;
510 struct whiteheat_private *info = usb_get_serial_port_data(port);
511 unsigned int modem_signals = 0;
513 dbg("%s - port %d", __func__, port->number);
515 firm_get_dtr_rts(port);
516 if (info->mcr & UART_MCR_DTR)
517 modem_signals |= TIOCM_DTR;
518 if (info->mcr & UART_MCR_RTS)
519 modem_signals |= TIOCM_RTS;
521 return modem_signals;
524 static int whiteheat_tiocmset(struct tty_struct *tty,
525 unsigned int set, unsigned int clear)
527 struct usb_serial_port *port = tty->driver_data;
528 struct whiteheat_private *info = usb_get_serial_port_data(port);
530 dbg("%s - port %d", __func__, port->number);
532 if (set & TIOCM_RTS)
533 info->mcr |= UART_MCR_RTS;
534 if (set & TIOCM_DTR)
535 info->mcr |= UART_MCR_DTR;
537 if (clear & TIOCM_RTS)
538 info->mcr &= ~UART_MCR_RTS;
539 if (clear & TIOCM_DTR)
540 info->mcr &= ~UART_MCR_DTR;
542 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
543 firm_set_rts(port, info->mcr & UART_MCR_RTS);
544 return 0;
548 static int whiteheat_ioctl(struct tty_struct *tty,
549 unsigned int cmd, unsigned long arg)
551 struct usb_serial_port *port = tty->driver_data;
552 struct serial_struct serstruct;
553 void __user *user_arg = (void __user *)arg;
555 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
557 switch (cmd) {
558 case TIOCGSERIAL:
559 memset(&serstruct, 0, sizeof(serstruct));
560 serstruct.type = PORT_16654;
561 serstruct.line = port->serial->minor;
562 serstruct.port = port->number;
563 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
564 serstruct.xmit_fifo_size = kfifo_size(&port->write_fifo);
565 serstruct.custom_divisor = 0;
566 serstruct.baud_base = 460800;
567 serstruct.close_delay = CLOSING_DELAY;
568 serstruct.closing_wait = CLOSING_DELAY;
570 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
571 return -EFAULT;
572 break;
573 default:
574 break;
577 return -ENOIOCTLCMD;
581 static void whiteheat_set_termios(struct tty_struct *tty,
582 struct usb_serial_port *port, struct ktermios *old_termios)
584 firm_setup_port(tty);
587 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
589 struct usb_serial_port *port = tty->driver_data;
590 firm_set_break(port, break_state);
594 /*****************************************************************************
595 * Connect Tech's White Heat callback routines
596 *****************************************************************************/
597 static void command_port_write_callback(struct urb *urb)
599 int status = urb->status;
601 dbg("%s", __func__);
603 if (status) {
604 dbg("nonzero urb status: %d", status);
605 return;
610 static void command_port_read_callback(struct urb *urb)
612 struct usb_serial_port *command_port = urb->context;
613 struct whiteheat_command_private *command_info;
614 int status = urb->status;
615 unsigned char *data = urb->transfer_buffer;
616 int result;
618 dbg("%s", __func__);
620 command_info = usb_get_serial_port_data(command_port);
621 if (!command_info) {
622 dbg("%s - command_info is NULL, exiting.", __func__);
623 return;
625 if (status) {
626 dbg("%s - nonzero urb status: %d", __func__, status);
627 if (status != -ENOENT)
628 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
629 wake_up(&command_info->wait_command);
630 return;
633 usb_serial_debug_data(debug, &command_port->dev,
634 __func__, urb->actual_length, data);
636 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
637 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
638 wake_up(&command_info->wait_command);
639 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
640 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
641 wake_up(&command_info->wait_command);
642 } else if (data[0] == WHITEHEAT_EVENT) {
643 /* These are unsolicited reports from the firmware, hence no
644 waiting command to wakeup */
645 dbg("%s - event received", __func__);
646 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
647 memcpy(command_info->result_buffer, &data[1],
648 urb->actual_length - 1);
649 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
650 wake_up(&command_info->wait_command);
651 } else
652 dbg("%s - bad reply from firmware", __func__);
654 /* Continue trying to always read */
655 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
656 if (result)
657 dbg("%s - failed resubmitting read urb, error %d",
658 __func__, result);
662 /*****************************************************************************
663 * Connect Tech's White Heat firmware interface
664 *****************************************************************************/
665 static int firm_send_command(struct usb_serial_port *port, __u8 command,
666 __u8 *data, __u8 datasize)
668 struct usb_serial_port *command_port;
669 struct whiteheat_command_private *command_info;
670 struct whiteheat_private *info;
671 __u8 *transfer_buffer;
672 int retval = 0;
673 int t;
675 dbg("%s - command %d", __func__, command);
677 command_port = port->serial->port[COMMAND_PORT];
678 command_info = usb_get_serial_port_data(command_port);
679 mutex_lock(&command_info->mutex);
680 command_info->command_finished = false;
682 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
683 transfer_buffer[0] = command;
684 memcpy(&transfer_buffer[1], data, datasize);
685 command_port->write_urb->transfer_buffer_length = datasize + 1;
686 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
687 if (retval) {
688 dbg("%s - submit urb failed", __func__);
689 goto exit;
692 /* wait for the command to complete */
693 t = wait_event_timeout(command_info->wait_command,
694 (bool)command_info->command_finished, COMMAND_TIMEOUT);
695 if (!t)
696 usb_kill_urb(command_port->write_urb);
698 if (command_info->command_finished == false) {
699 dbg("%s - command timed out.", __func__);
700 retval = -ETIMEDOUT;
701 goto exit;
704 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
705 dbg("%s - command failed.", __func__);
706 retval = -EIO;
707 goto exit;
710 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
711 dbg("%s - command completed.", __func__);
712 switch (command) {
713 case WHITEHEAT_GET_DTR_RTS:
714 info = usb_get_serial_port_data(port);
715 memcpy(&info->mcr, command_info->result_buffer,
716 sizeof(struct whiteheat_dr_info));
717 break;
720 exit:
721 mutex_unlock(&command_info->mutex);
722 return retval;
726 static int firm_open(struct usb_serial_port *port)
728 struct whiteheat_simple open_command;
730 open_command.port = port->number - port->serial->minor + 1;
731 return firm_send_command(port, WHITEHEAT_OPEN,
732 (__u8 *)&open_command, sizeof(open_command));
736 static int firm_close(struct usb_serial_port *port)
738 struct whiteheat_simple close_command;
740 close_command.port = port->number - port->serial->minor + 1;
741 return firm_send_command(port, WHITEHEAT_CLOSE,
742 (__u8 *)&close_command, sizeof(close_command));
746 static void firm_setup_port(struct tty_struct *tty)
748 struct usb_serial_port *port = tty->driver_data;
749 struct whiteheat_port_settings port_settings;
750 unsigned int cflag = tty->termios->c_cflag;
752 port_settings.port = port->number + 1;
754 /* get the byte size */
755 switch (cflag & CSIZE) {
756 case CS5: port_settings.bits = 5; break;
757 case CS6: port_settings.bits = 6; break;
758 case CS7: port_settings.bits = 7; break;
759 default:
760 case CS8: port_settings.bits = 8; break;
762 dbg("%s - data bits = %d", __func__, port_settings.bits);
764 /* determine the parity */
765 if (cflag & PARENB)
766 if (cflag & CMSPAR)
767 if (cflag & PARODD)
768 port_settings.parity = WHITEHEAT_PAR_MARK;
769 else
770 port_settings.parity = WHITEHEAT_PAR_SPACE;
771 else
772 if (cflag & PARODD)
773 port_settings.parity = WHITEHEAT_PAR_ODD;
774 else
775 port_settings.parity = WHITEHEAT_PAR_EVEN;
776 else
777 port_settings.parity = WHITEHEAT_PAR_NONE;
778 dbg("%s - parity = %c", __func__, port_settings.parity);
780 /* figure out the stop bits requested */
781 if (cflag & CSTOPB)
782 port_settings.stop = 2;
783 else
784 port_settings.stop = 1;
785 dbg("%s - stop bits = %d", __func__, port_settings.stop);
787 /* figure out the flow control settings */
788 if (cflag & CRTSCTS)
789 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
790 WHITEHEAT_HFLOW_RTS);
791 else
792 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
793 dbg("%s - hardware flow control = %s %s %s %s", __func__,
794 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
795 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
796 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
797 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
799 /* determine software flow control */
800 if (I_IXOFF(tty))
801 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
802 else
803 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
804 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
806 port_settings.xon = START_CHAR(tty);
807 port_settings.xoff = STOP_CHAR(tty);
808 dbg("%s - XON = %2x, XOFF = %2x",
809 __func__, port_settings.xon, port_settings.xoff);
811 /* get the baud rate wanted */
812 port_settings.baud = tty_get_baud_rate(tty);
813 dbg("%s - baud rate = %d", __func__, port_settings.baud);
815 /* fixme: should set validated settings */
816 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
817 /* handle any settings that aren't specified in the tty structure */
818 port_settings.lloop = 0;
820 /* now send the message to the device */
821 firm_send_command(port, WHITEHEAT_SETUP_PORT,
822 (__u8 *)&port_settings, sizeof(port_settings));
826 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
828 struct whiteheat_set_rdb rts_command;
830 rts_command.port = port->number - port->serial->minor + 1;
831 rts_command.state = onoff;
832 return firm_send_command(port, WHITEHEAT_SET_RTS,
833 (__u8 *)&rts_command, sizeof(rts_command));
837 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
839 struct whiteheat_set_rdb dtr_command;
841 dtr_command.port = port->number - port->serial->minor + 1;
842 dtr_command.state = onoff;
843 return firm_send_command(port, WHITEHEAT_SET_DTR,
844 (__u8 *)&dtr_command, sizeof(dtr_command));
848 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
850 struct whiteheat_set_rdb break_command;
852 break_command.port = port->number - port->serial->minor + 1;
853 break_command.state = onoff;
854 return firm_send_command(port, WHITEHEAT_SET_BREAK,
855 (__u8 *)&break_command, sizeof(break_command));
859 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
861 struct whiteheat_purge purge_command;
863 purge_command.port = port->number - port->serial->minor + 1;
864 purge_command.what = rxtx;
865 return firm_send_command(port, WHITEHEAT_PURGE,
866 (__u8 *)&purge_command, sizeof(purge_command));
870 static int firm_get_dtr_rts(struct usb_serial_port *port)
872 struct whiteheat_simple get_dr_command;
874 get_dr_command.port = port->number - port->serial->minor + 1;
875 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
876 (__u8 *)&get_dr_command, sizeof(get_dr_command));
880 static int firm_report_tx_done(struct usb_serial_port *port)
882 struct whiteheat_simple close_command;
884 close_command.port = port->number - port->serial->minor + 1;
885 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
886 (__u8 *)&close_command, sizeof(close_command));
890 /*****************************************************************************
891 * Connect Tech's White Heat utility functions
892 *****************************************************************************/
893 static int start_command_port(struct usb_serial *serial)
895 struct usb_serial_port *command_port;
896 struct whiteheat_command_private *command_info;
897 int retval = 0;
899 command_port = serial->port[COMMAND_PORT];
900 command_info = usb_get_serial_port_data(command_port);
901 mutex_lock(&command_info->mutex);
902 if (!command_info->port_running) {
903 /* Work around HCD bugs */
904 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
906 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
907 if (retval) {
908 dev_err(&serial->dev->dev,
909 "%s - failed submitting read urb, error %d\n",
910 __func__, retval);
911 goto exit;
914 command_info->port_running++;
916 exit:
917 mutex_unlock(&command_info->mutex);
918 return retval;
922 static void stop_command_port(struct usb_serial *serial)
924 struct usb_serial_port *command_port;
925 struct whiteheat_command_private *command_info;
927 command_port = serial->port[COMMAND_PORT];
928 command_info = usb_get_serial_port_data(command_port);
929 mutex_lock(&command_info->mutex);
930 command_info->port_running--;
931 if (!command_info->port_running)
932 usb_kill_urb(command_port->read_urb);
933 mutex_unlock(&command_info->mutex);
936 module_usb_serial_driver(whiteheat_driver, serial_drivers);
938 MODULE_AUTHOR(DRIVER_AUTHOR);
939 MODULE_DESCRIPTION(DRIVER_DESC);
940 MODULE_LICENSE("GPL");
942 MODULE_FIRMWARE("whiteheat.fw");
943 MODULE_FIRMWARE("whiteheat_loader.fw");
945 module_param(debug, bool, S_IRUGO | S_IWUSR);
946 MODULE_PARM_DESC(debug, "Debug enabled or not");