]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/usb/serial/whiteheat.c
a0df894b342895af5277d92c62b6a0570708b7d2
[linux-beck.git] / drivers / usb / serial / whiteheat.c
1 /*
2  * USB ConnectTech WhiteHEAT driver
3  *
4  *      Copyright (C) 2002
5  *          Connect Tech Inc.
6  *
7  *      Copyright (C) 1999 - 2001
8  *          Greg Kroah-Hartman (greg@kroah.com)
9  *
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.
14  *
15  * See Documentation/usb/usb-serial.txt for more information on using this
16  * driver
17  */
18
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 */
38
39 static bool debug;
40
41 #ifndef CMSPAR
42 #define CMSPAR 0
43 #endif
44
45 /*
46  * Version Information
47  */
48 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
49 #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
50
51 #define CONNECT_TECH_VENDOR_ID          0x0710
52 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
53 #define CONNECT_TECH_WHITE_HEAT_ID      0x8001
54
55 /*
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.
61 */
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 */
65 };
66
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 */
70 };
71
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 */
76 };
77
78 MODULE_DEVICE_TABLE(usb, id_table_combined);
79
80 static struct usb_driver whiteheat_driver = {
81         .name =         "whiteheat",
82         .disconnect =   usb_serial_disconnect,
83         .id_table =     id_table_combined,
84 };
85
86 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
87 static int  whiteheat_firmware_download(struct usb_serial *serial,
88                                         const struct usb_device_id *id);
89 static int  whiteheat_firmware_attach(struct usb_serial *serial);
90
91 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
92 static int  whiteheat_attach(struct usb_serial *serial);
93 static void whiteheat_release(struct usb_serial *serial);
94 static int  whiteheat_open(struct tty_struct *tty,
95                         struct usb_serial_port *port);
96 static void whiteheat_close(struct usb_serial_port *port);
97 static int  whiteheat_ioctl(struct tty_struct *tty,
98                         unsigned int cmd, unsigned long arg);
99 static void whiteheat_set_termios(struct tty_struct *tty,
100                         struct usb_serial_port *port, struct ktermios *old);
101 static int  whiteheat_tiocmget(struct tty_struct *tty);
102 static int  whiteheat_tiocmset(struct tty_struct *tty,
103                         unsigned int set, unsigned int clear);
104 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
105
106 static struct usb_serial_driver whiteheat_fake_device = {
107         .driver = {
108                 .owner =        THIS_MODULE,
109                 .name =         "whiteheatnofirm",
110         },
111         .description =          "Connect Tech - WhiteHEAT - (prerenumeration)",
112         .id_table =             id_table_prerenumeration,
113         .num_ports =            1,
114         .probe =                whiteheat_firmware_download,
115         .attach =               whiteheat_firmware_attach,
116 };
117
118 static struct usb_serial_driver whiteheat_device = {
119         .driver = {
120                 .owner =        THIS_MODULE,
121                 .name =         "whiteheat",
122         },
123         .description =          "Connect Tech - WhiteHEAT",
124         .id_table =             id_table_std,
125         .num_ports =            4,
126         .attach =               whiteheat_attach,
127         .release =              whiteheat_release,
128         .open =                 whiteheat_open,
129         .close =                whiteheat_close,
130         .ioctl =                whiteheat_ioctl,
131         .set_termios =          whiteheat_set_termios,
132         .break_ctl =            whiteheat_break_ctl,
133         .tiocmget =             whiteheat_tiocmget,
134         .tiocmset =             whiteheat_tiocmset,
135         .throttle =             usb_serial_generic_throttle,
136         .unthrottle =           usb_serial_generic_unthrottle,
137 };
138
139 static struct usb_serial_driver * const serial_drivers[] = {
140         &whiteheat_fake_device, &whiteheat_device, NULL
141 };
142
143 struct whiteheat_command_private {
144         struct mutex            mutex;
145         __u8                    port_running;
146         __u8                    command_finished;
147         wait_queue_head_t       wait_command; /* for handling sleeping whilst
148                                                  waiting for a command to
149                                                  finish */
150         __u8                    result_buffer[64];
151 };
152
153 struct whiteheat_private {
154         __u8                    mcr;            /* FIXME: no locking on mcr */
155 };
156
157
158 /* local function prototypes */
159 static int start_command_port(struct usb_serial *serial);
160 static void stop_command_port(struct usb_serial *serial);
161 static void command_port_write_callback(struct urb *urb);
162 static void command_port_read_callback(struct urb *urb);
163
164 static int firm_send_command(struct usb_serial_port *port, __u8 command,
165                                                 __u8 *data, __u8 datasize);
166 static int firm_open(struct usb_serial_port *port);
167 static int firm_close(struct usb_serial_port *port);
168 static void firm_setup_port(struct tty_struct *tty);
169 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
170 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
171 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
172 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
173 static int firm_get_dtr_rts(struct usb_serial_port *port);
174 static int firm_report_tx_done(struct usb_serial_port *port);
175
176
177 #define COMMAND_PORT            4
178 #define COMMAND_TIMEOUT         (2*HZ)  /* 2 second timeout for a command */
179 #define COMMAND_TIMEOUT_MS      2000
180 #define CLOSING_DELAY           (30 * HZ)
181
182
183 /*****************************************************************************
184  * Connect Tech's White Heat prerenumeration driver functions
185  *****************************************************************************/
186
187 /* steps to download the firmware to the WhiteHEAT device:
188  - hold the reset (by writing to the reset bit of the CPUCS register)
189  - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
190  - release the reset (by writing to the CPUCS register)
191  - download the WH.HEX file for all addresses greater than 0x1b3f using
192    VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
193  - hold the reset
194  - download the WH.HEX file for all addresses less than 0x1b40 using
195    VENDOR_REQUEST_ANCHOR_LOAD
196  - release the reset
197  - device renumerated itself and comes up as new device id with all
198    firmware download completed.
199 */
200 static int whiteheat_firmware_download(struct usb_serial *serial,
201                                         const struct usb_device_id *id)
202 {
203         int response, ret = -ENOENT;
204         const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
205         const struct ihex_binrec *record;
206
207         if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
208                                   &serial->dev->dev)) {
209                 dev_err(&serial->dev->dev,
210                         "%s - request \"whiteheat.fw\" failed\n", __func__);
211                 goto out;
212         }
213         if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
214                              &serial->dev->dev)) {
215                 dev_err(&serial->dev->dev,
216                         "%s - request \"whiteheat_loader.fw\" failed\n",
217                         __func__);
218                 goto out;
219         }
220         ret = 0;
221         response = ezusb_set_reset (serial, 1);
222
223         record = (const struct ihex_binrec *)loader_fw->data;
224         while (record) {
225                 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
226                                               (unsigned char *)record->data,
227                                               be16_to_cpu(record->len), 0xa0);
228                 if (response < 0) {
229                         dev_err(&serial->dev->dev, "%s - ezusb_writememory "
230                                 "failed for loader (%d %04X %p %d)\n",
231                                 __func__, response, be32_to_cpu(record->addr),
232                                 record->data, be16_to_cpu(record->len));
233                         break;
234                 }
235                 record = ihex_next_binrec(record);
236         }
237
238         response = ezusb_set_reset(serial, 0);
239
240         record = (const struct ihex_binrec *)firmware_fw->data;
241         while (record && be32_to_cpu(record->addr) < 0x1b40)
242                 record = ihex_next_binrec(record);
243         while (record) {
244                 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
245                                               (unsigned char *)record->data,
246                                               be16_to_cpu(record->len), 0xa3);
247                 if (response < 0) {
248                         dev_err(&serial->dev->dev, "%s - ezusb_writememory "
249                                 "failed for first firmware step "
250                                 "(%d %04X %p %d)\n", __func__, response,
251                                 be32_to_cpu(record->addr), record->data,
252                                 be16_to_cpu(record->len));
253                         break;
254                 }
255                 ++record;
256         }
257
258         response = ezusb_set_reset(serial, 1);
259
260         record = (const struct ihex_binrec *)firmware_fw->data;
261         while (record && be32_to_cpu(record->addr) < 0x1b40) {
262                 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
263                                               (unsigned char *)record->data,
264                                               be16_to_cpu(record->len), 0xa0);
265                 if (response < 0) {
266                         dev_err(&serial->dev->dev, "%s - ezusb_writememory "
267                                 "failed for second firmware step "
268                                 "(%d %04X %p %d)\n", __func__, response,
269                                 be32_to_cpu(record->addr), record->data,
270                                 be16_to_cpu(record->len));
271                         break;
272                 }
273                 ++record;
274         }
275         ret = 0;
276         response = ezusb_set_reset (serial, 0);
277  out:
278         release_firmware(loader_fw);
279         release_firmware(firmware_fw);
280         return ret;
281 }
282
283
284 static int whiteheat_firmware_attach(struct usb_serial *serial)
285 {
286         /* We want this device to fail to have a driver assigned to it */
287         return 1;
288 }
289
290
291 /*****************************************************************************
292  * Connect Tech's White Heat serial driver functions
293  *****************************************************************************/
294 static int whiteheat_attach(struct usb_serial *serial)
295 {
296         struct usb_serial_port *command_port;
297         struct whiteheat_command_private *command_info;
298         struct usb_serial_port *port;
299         struct whiteheat_private *info;
300         struct whiteheat_hw_info *hw_info;
301         int pipe;
302         int ret;
303         int alen;
304         __u8 *command;
305         __u8 *result;
306         int i;
307
308         command_port = serial->port[COMMAND_PORT];
309
310         pipe = usb_sndbulkpipe(serial->dev,
311                         command_port->bulk_out_endpointAddress);
312         command = kmalloc(2, GFP_KERNEL);
313         if (!command)
314                 goto no_command_buffer;
315         command[0] = WHITEHEAT_GET_HW_INFO;
316         command[1] = 0;
317
318         result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
319         if (!result)
320                 goto no_result_buffer;
321         /*
322          * When the module is reloaded the firmware is still there and
323          * the endpoints are still in the usb core unchanged. This is the
324          * unlinking bug in disguise. Same for the call below.
325          */
326         usb_clear_halt(serial->dev, pipe);
327         ret = usb_bulk_msg(serial->dev, pipe, command, 2,
328                                                 &alen, COMMAND_TIMEOUT_MS);
329         if (ret) {
330                 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
331                         serial->type->description, ret);
332                 goto no_firmware;
333         } else if (alen != 2) {
334                 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
335                         serial->type->description, alen);
336                 goto no_firmware;
337         }
338
339         pipe = usb_rcvbulkpipe(serial->dev,
340                                 command_port->bulk_in_endpointAddress);
341         /* See the comment on the usb_clear_halt() above */
342         usb_clear_halt(serial->dev, pipe);
343         ret = usb_bulk_msg(serial->dev, pipe, result,
344                         sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
345         if (ret) {
346                 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
347                         serial->type->description, ret);
348                 goto no_firmware;
349         } else if (alen != sizeof(*hw_info) + 1) {
350                 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
351                         serial->type->description, alen);
352                 goto no_firmware;
353         } else if (result[0] != command[0]) {
354                 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
355                         serial->type->description, result[0]);
356                 goto no_firmware;
357         }
358
359         hw_info = (struct whiteheat_hw_info *)&result[1];
360
361         dev_info(&serial->dev->dev, "%s: Firmware v%d.%02d\n",
362                  serial->type->description,
363                  hw_info->sw_major_rev, hw_info->sw_minor_rev);
364
365         for (i = 0; i < serial->num_ports; i++) {
366                 port = serial->port[i];
367
368                 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
369                 if (info == NULL) {
370                         dev_err(&port->dev,
371                                 "%s: Out of memory for port structures\n",
372                                 serial->type->description);
373                         goto no_private;
374                 }
375
376                 info->mcr = 0;
377
378                 usb_set_serial_port_data(port, info);
379         }
380
381         command_info = kmalloc(sizeof(struct whiteheat_command_private),
382                                                                 GFP_KERNEL);
383         if (command_info == NULL) {
384                 dev_err(&serial->dev->dev,
385                         "%s: Out of memory for port structures\n",
386                         serial->type->description);
387                 goto no_command_private;
388         }
389
390         mutex_init(&command_info->mutex);
391         command_info->port_running = 0;
392         init_waitqueue_head(&command_info->wait_command);
393         usb_set_serial_port_data(command_port, command_info);
394         command_port->write_urb->complete = command_port_write_callback;
395         command_port->read_urb->complete = command_port_read_callback;
396         kfree(result);
397         kfree(command);
398
399         return 0;
400
401 no_firmware:
402         /* Firmware likely not running */
403         dev_err(&serial->dev->dev,
404                 "%s: Unable to retrieve firmware version, try replugging\n",
405                 serial->type->description);
406         dev_err(&serial->dev->dev,
407                 "%s: If the firmware is not running (status led not blinking)\n",
408                 serial->type->description);
409         dev_err(&serial->dev->dev,
410                 "%s: please contact support@connecttech.com\n",
411                 serial->type->description);
412         kfree(result);
413         return -ENODEV;
414
415 no_command_private:
416         for (i = serial->num_ports - 1; i >= 0; i--) {
417                 port = serial->port[i];
418                 info = usb_get_serial_port_data(port);
419                 kfree(info);
420 no_private:
421                 ;
422         }
423         kfree(result);
424 no_result_buffer:
425         kfree(command);
426 no_command_buffer:
427         return -ENOMEM;
428 }
429
430
431 static void whiteheat_release(struct usb_serial *serial)
432 {
433         struct usb_serial_port *command_port;
434         struct whiteheat_private *info;
435         int i;
436
437         /* free up our private data for our command port */
438         command_port = serial->port[COMMAND_PORT];
439         kfree(usb_get_serial_port_data(command_port));
440
441         for (i = 0; i < serial->num_ports; i++) {
442                 info = usb_get_serial_port_data(serial->port[i]);
443                 kfree(info);
444         }
445 }
446
447 static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
448 {
449         int retval;
450
451         retval = start_command_port(port->serial);
452         if (retval)
453                 goto exit;
454
455         /* send an open port command */
456         retval = firm_open(port);
457         if (retval) {
458                 stop_command_port(port->serial);
459                 goto exit;
460         }
461
462         retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
463         if (retval) {
464                 firm_close(port);
465                 stop_command_port(port->serial);
466                 goto exit;
467         }
468
469         if (tty)
470                 firm_setup_port(tty);
471
472         /* Work around HCD bugs */
473         usb_clear_halt(port->serial->dev, port->read_urb->pipe);
474         usb_clear_halt(port->serial->dev, port->write_urb->pipe);
475
476         retval = usb_serial_generic_open(tty, port);
477         if (retval) {
478                 firm_close(port);
479                 stop_command_port(port->serial);
480                 goto exit;
481         }
482 exit:
483         return retval;
484 }
485
486
487 static void whiteheat_close(struct usb_serial_port *port)
488 {
489         firm_report_tx_done(port);
490         firm_close(port);
491
492         usb_serial_generic_close(port);
493
494         stop_command_port(port->serial);
495 }
496
497 static int whiteheat_tiocmget(struct tty_struct *tty)
498 {
499         struct usb_serial_port *port = tty->driver_data;
500         struct whiteheat_private *info = usb_get_serial_port_data(port);
501         unsigned int modem_signals = 0;
502
503         firm_get_dtr_rts(port);
504         if (info->mcr & UART_MCR_DTR)
505                 modem_signals |= TIOCM_DTR;
506         if (info->mcr & UART_MCR_RTS)
507                 modem_signals |= TIOCM_RTS;
508
509         return modem_signals;
510 }
511
512 static int whiteheat_tiocmset(struct tty_struct *tty,
513                                unsigned int set, unsigned int clear)
514 {
515         struct usb_serial_port *port = tty->driver_data;
516         struct whiteheat_private *info = usb_get_serial_port_data(port);
517
518         if (set & TIOCM_RTS)
519                 info->mcr |= UART_MCR_RTS;
520         if (set & TIOCM_DTR)
521                 info->mcr |= UART_MCR_DTR;
522
523         if (clear & TIOCM_RTS)
524                 info->mcr &= ~UART_MCR_RTS;
525         if (clear & TIOCM_DTR)
526                 info->mcr &= ~UART_MCR_DTR;
527
528         firm_set_dtr(port, info->mcr & UART_MCR_DTR);
529         firm_set_rts(port, info->mcr & UART_MCR_RTS);
530         return 0;
531 }
532
533
534 static int whiteheat_ioctl(struct tty_struct *tty,
535                                         unsigned int cmd, unsigned long arg)
536 {
537         struct usb_serial_port *port = tty->driver_data;
538         struct serial_struct serstruct;
539         void __user *user_arg = (void __user *)arg;
540
541         dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
542
543         switch (cmd) {
544         case TIOCGSERIAL:
545                 memset(&serstruct, 0, sizeof(serstruct));
546                 serstruct.type = PORT_16654;
547                 serstruct.line = port->serial->minor;
548                 serstruct.port = port->number;
549                 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
550                 serstruct.xmit_fifo_size = kfifo_size(&port->write_fifo);
551                 serstruct.custom_divisor = 0;
552                 serstruct.baud_base = 460800;
553                 serstruct.close_delay = CLOSING_DELAY;
554                 serstruct.closing_wait = CLOSING_DELAY;
555
556                 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
557                         return -EFAULT;
558                 break;
559         default:
560                 break;
561         }
562
563         return -ENOIOCTLCMD;
564 }
565
566
567 static void whiteheat_set_termios(struct tty_struct *tty,
568         struct usb_serial_port *port, struct ktermios *old_termios)
569 {
570         firm_setup_port(tty);
571 }
572
573 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
574 {
575         struct usb_serial_port *port = tty->driver_data;
576         firm_set_break(port, break_state);
577 }
578
579
580 /*****************************************************************************
581  * Connect Tech's White Heat callback routines
582  *****************************************************************************/
583 static void command_port_write_callback(struct urb *urb)
584 {
585         int status = urb->status;
586
587         if (status) {
588                 dbg("nonzero urb status: %d", status);
589                 return;
590         }
591 }
592
593
594 static void command_port_read_callback(struct urb *urb)
595 {
596         struct usb_serial_port *command_port = urb->context;
597         struct whiteheat_command_private *command_info;
598         int status = urb->status;
599         unsigned char *data = urb->transfer_buffer;
600         int result;
601
602         command_info = usb_get_serial_port_data(command_port);
603         if (!command_info) {
604                 dbg("%s - command_info is NULL, exiting.", __func__);
605                 return;
606         }
607         if (status) {
608                 dbg("%s - nonzero urb status: %d", __func__, status);
609                 if (status != -ENOENT)
610                         command_info->command_finished = WHITEHEAT_CMD_FAILURE;
611                 wake_up(&command_info->wait_command);
612                 return;
613         }
614
615         usb_serial_debug_data(debug, &command_port->dev,
616                                 __func__, urb->actual_length, data);
617
618         if (data[0] == WHITEHEAT_CMD_COMPLETE) {
619                 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
620                 wake_up(&command_info->wait_command);
621         } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
622                 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
623                 wake_up(&command_info->wait_command);
624         } else if (data[0] == WHITEHEAT_EVENT) {
625                 /* These are unsolicited reports from the firmware, hence no
626                    waiting command to wakeup */
627                 dbg("%s - event received", __func__);
628         } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
629                 memcpy(command_info->result_buffer, &data[1],
630                                                 urb->actual_length - 1);
631                 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
632                 wake_up(&command_info->wait_command);
633         } else
634                 dbg("%s - bad reply from firmware", __func__);
635
636         /* Continue trying to always read */
637         result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
638         if (result)
639                 dbg("%s - failed resubmitting read urb, error %d",
640                         __func__, result);
641 }
642
643
644 /*****************************************************************************
645  * Connect Tech's White Heat firmware interface
646  *****************************************************************************/
647 static int firm_send_command(struct usb_serial_port *port, __u8 command,
648                                                 __u8 *data, __u8 datasize)
649 {
650         struct usb_serial_port *command_port;
651         struct whiteheat_command_private *command_info;
652         struct whiteheat_private *info;
653         __u8 *transfer_buffer;
654         int retval = 0;
655         int t;
656
657         dbg("%s - command %d", __func__, command);
658
659         command_port = port->serial->port[COMMAND_PORT];
660         command_info = usb_get_serial_port_data(command_port);
661         mutex_lock(&command_info->mutex);
662         command_info->command_finished = false;
663
664         transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
665         transfer_buffer[0] = command;
666         memcpy(&transfer_buffer[1], data, datasize);
667         command_port->write_urb->transfer_buffer_length = datasize + 1;
668         retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
669         if (retval) {
670                 dbg("%s - submit urb failed", __func__);
671                 goto exit;
672         }
673
674         /* wait for the command to complete */
675         t = wait_event_timeout(command_info->wait_command,
676                 (bool)command_info->command_finished, COMMAND_TIMEOUT);
677         if (!t)
678                 usb_kill_urb(command_port->write_urb);
679
680         if (command_info->command_finished == false) {
681                 dbg("%s - command timed out.", __func__);
682                 retval = -ETIMEDOUT;
683                 goto exit;
684         }
685
686         if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
687                 dbg("%s - command failed.", __func__);
688                 retval = -EIO;
689                 goto exit;
690         }
691
692         if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
693                 dbg("%s - command completed.", __func__);
694                 switch (command) {
695                 case WHITEHEAT_GET_DTR_RTS:
696                         info = usb_get_serial_port_data(port);
697                         memcpy(&info->mcr, command_info->result_buffer,
698                                         sizeof(struct whiteheat_dr_info));
699                                 break;
700                 }
701         }
702 exit:
703         mutex_unlock(&command_info->mutex);
704         return retval;
705 }
706
707
708 static int firm_open(struct usb_serial_port *port)
709 {
710         struct whiteheat_simple open_command;
711
712         open_command.port = port->number - port->serial->minor + 1;
713         return firm_send_command(port, WHITEHEAT_OPEN,
714                 (__u8 *)&open_command, sizeof(open_command));
715 }
716
717
718 static int firm_close(struct usb_serial_port *port)
719 {
720         struct whiteheat_simple close_command;
721
722         close_command.port = port->number - port->serial->minor + 1;
723         return firm_send_command(port, WHITEHEAT_CLOSE,
724                         (__u8 *)&close_command, sizeof(close_command));
725 }
726
727
728 static void firm_setup_port(struct tty_struct *tty)
729 {
730         struct usb_serial_port *port = tty->driver_data;
731         struct whiteheat_port_settings port_settings;
732         unsigned int cflag = tty->termios->c_cflag;
733
734         port_settings.port = port->number + 1;
735
736         /* get the byte size */
737         switch (cflag & CSIZE) {
738         case CS5:       port_settings.bits = 5;   break;
739         case CS6:       port_settings.bits = 6;   break;
740         case CS7:       port_settings.bits = 7;   break;
741         default:
742         case CS8:       port_settings.bits = 8;   break;
743         }
744         dbg("%s - data bits = %d", __func__, port_settings.bits);
745
746         /* determine the parity */
747         if (cflag & PARENB)
748                 if (cflag & CMSPAR)
749                         if (cflag & PARODD)
750                                 port_settings.parity = WHITEHEAT_PAR_MARK;
751                         else
752                                 port_settings.parity = WHITEHEAT_PAR_SPACE;
753                 else
754                         if (cflag & PARODD)
755                                 port_settings.parity = WHITEHEAT_PAR_ODD;
756                         else
757                                 port_settings.parity = WHITEHEAT_PAR_EVEN;
758         else
759                 port_settings.parity = WHITEHEAT_PAR_NONE;
760         dbg("%s - parity = %c", __func__, port_settings.parity);
761
762         /* figure out the stop bits requested */
763         if (cflag & CSTOPB)
764                 port_settings.stop = 2;
765         else
766                 port_settings.stop = 1;
767         dbg("%s - stop bits = %d", __func__, port_settings.stop);
768
769         /* figure out the flow control settings */
770         if (cflag & CRTSCTS)
771                 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
772                                                 WHITEHEAT_HFLOW_RTS);
773         else
774                 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
775         dbg("%s - hardware flow control = %s %s %s %s", __func__,
776             (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
777             (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
778             (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
779             (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
780
781         /* determine software flow control */
782         if (I_IXOFF(tty))
783                 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
784         else
785                 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
786         dbg("%s - software flow control = %c", __func__, port_settings.sflow);
787
788         port_settings.xon = START_CHAR(tty);
789         port_settings.xoff = STOP_CHAR(tty);
790         dbg("%s - XON = %2x, XOFF = %2x",
791                         __func__, port_settings.xon, port_settings.xoff);
792
793         /* get the baud rate wanted */
794         port_settings.baud = tty_get_baud_rate(tty);
795         dbg("%s - baud rate = %d", __func__, port_settings.baud);
796
797         /* fixme: should set validated settings */
798         tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
799         /* handle any settings that aren't specified in the tty structure */
800         port_settings.lloop = 0;
801
802         /* now send the message to the device */
803         firm_send_command(port, WHITEHEAT_SETUP_PORT,
804                         (__u8 *)&port_settings, sizeof(port_settings));
805 }
806
807
808 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
809 {
810         struct whiteheat_set_rdb rts_command;
811
812         rts_command.port = port->number - port->serial->minor + 1;
813         rts_command.state = onoff;
814         return firm_send_command(port, WHITEHEAT_SET_RTS,
815                         (__u8 *)&rts_command, sizeof(rts_command));
816 }
817
818
819 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
820 {
821         struct whiteheat_set_rdb dtr_command;
822
823         dtr_command.port = port->number - port->serial->minor + 1;
824         dtr_command.state = onoff;
825         return firm_send_command(port, WHITEHEAT_SET_DTR,
826                         (__u8 *)&dtr_command, sizeof(dtr_command));
827 }
828
829
830 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
831 {
832         struct whiteheat_set_rdb break_command;
833
834         break_command.port = port->number - port->serial->minor + 1;
835         break_command.state = onoff;
836         return firm_send_command(port, WHITEHEAT_SET_BREAK,
837                         (__u8 *)&break_command, sizeof(break_command));
838 }
839
840
841 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
842 {
843         struct whiteheat_purge purge_command;
844
845         purge_command.port = port->number - port->serial->minor + 1;
846         purge_command.what = rxtx;
847         return firm_send_command(port, WHITEHEAT_PURGE,
848                         (__u8 *)&purge_command, sizeof(purge_command));
849 }
850
851
852 static int firm_get_dtr_rts(struct usb_serial_port *port)
853 {
854         struct whiteheat_simple get_dr_command;
855
856         get_dr_command.port = port->number - port->serial->minor + 1;
857         return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
858                         (__u8 *)&get_dr_command, sizeof(get_dr_command));
859 }
860
861
862 static int firm_report_tx_done(struct usb_serial_port *port)
863 {
864         struct whiteheat_simple close_command;
865
866         close_command.port = port->number - port->serial->minor + 1;
867         return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
868                         (__u8 *)&close_command, sizeof(close_command));
869 }
870
871
872 /*****************************************************************************
873  * Connect Tech's White Heat utility functions
874  *****************************************************************************/
875 static int start_command_port(struct usb_serial *serial)
876 {
877         struct usb_serial_port *command_port;
878         struct whiteheat_command_private *command_info;
879         int retval = 0;
880
881         command_port = serial->port[COMMAND_PORT];
882         command_info = usb_get_serial_port_data(command_port);
883         mutex_lock(&command_info->mutex);
884         if (!command_info->port_running) {
885                 /* Work around HCD bugs */
886                 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
887
888                 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
889                 if (retval) {
890                         dev_err(&serial->dev->dev,
891                                 "%s - failed submitting read urb, error %d\n",
892                                 __func__, retval);
893                         goto exit;
894                 }
895         }
896         command_info->port_running++;
897
898 exit:
899         mutex_unlock(&command_info->mutex);
900         return retval;
901 }
902
903
904 static void stop_command_port(struct usb_serial *serial)
905 {
906         struct usb_serial_port *command_port;
907         struct whiteheat_command_private *command_info;
908
909         command_port = serial->port[COMMAND_PORT];
910         command_info = usb_get_serial_port_data(command_port);
911         mutex_lock(&command_info->mutex);
912         command_info->port_running--;
913         if (!command_info->port_running)
914                 usb_kill_urb(command_port->read_urb);
915         mutex_unlock(&command_info->mutex);
916 }
917
918 module_usb_serial_driver(whiteheat_driver, serial_drivers);
919
920 MODULE_AUTHOR(DRIVER_AUTHOR);
921 MODULE_DESCRIPTION(DRIVER_DESC);
922 MODULE_LICENSE("GPL");
923
924 MODULE_FIRMWARE("whiteheat.fw");
925 MODULE_FIRMWARE("whiteheat_loader.fw");
926
927 module_param(debug, bool, S_IRUGO | S_IWUSR);
928 MODULE_PARM_DESC(debug, "Debug enabled or not");