USB: ch341: forward USB errors to USB serial core
[pandora-kernel.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 int debug;
40
41 #ifndef CMSPAR
42 #define CMSPAR 0
43 #endif
44
45 /*
46  * Version Information
47  */
48 #define DRIVER_VERSION "v2.0"
49 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
50 #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
51
52 #define CONNECT_TECH_VENDOR_ID          0x0710
53 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
54 #define CONNECT_TECH_WHITE_HEAT_ID      0x8001
55
56 /*
57    ID tables for whiteheat are unusual, because we want to different
58    things for different versions of the device.  Eventually, this
59    will be doable from a single table.  But, for now, we define two
60    separate ID tables, and then a third table that combines them
61    just for the purpose of exporting the autoloading information.
62 */
63 static const struct usb_device_id id_table_std[] = {
64         { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
65         { }                                             /* Terminating entry */
66 };
67
68 static const struct usb_device_id id_table_prerenumeration[] = {
69         { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
70         { }                                             /* Terminating entry */
71 };
72
73 static const struct usb_device_id id_table_combined[] = {
74         { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
75         { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
76         { }                                             /* Terminating entry */
77 };
78
79 MODULE_DEVICE_TABLE(usb, id_table_combined);
80
81 static struct usb_driver whiteheat_driver = {
82         .name =         "whiteheat",
83         .probe =        usb_serial_probe,
84         .disconnect =   usb_serial_disconnect,
85         .id_table =     id_table_combined,
86         .no_dynamic_id =        1,
87 };
88
89 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
90 static int  whiteheat_firmware_download(struct usb_serial *serial,
91                                         const struct usb_device_id *id);
92 static int  whiteheat_firmware_attach(struct usb_serial *serial);
93
94 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
95 static int  whiteheat_attach(struct usb_serial *serial);
96 static void whiteheat_release(struct usb_serial *serial);
97 static int  whiteheat_open(struct tty_struct *tty,
98                         struct usb_serial_port *port);
99 static void whiteheat_close(struct usb_serial_port *port);
100 static int  whiteheat_write(struct tty_struct *tty,
101                         struct usb_serial_port *port,
102                         const unsigned char *buf, int count);
103 static int  whiteheat_write_room(struct tty_struct *tty);
104 static int  whiteheat_ioctl(struct tty_struct *tty,
105                         unsigned int cmd, unsigned long arg);
106 static void whiteheat_set_termios(struct tty_struct *tty,
107                         struct usb_serial_port *port, struct ktermios *old);
108 static int  whiteheat_tiocmget(struct tty_struct *tty);
109 static int  whiteheat_tiocmset(struct tty_struct *tty,
110                         unsigned int set, unsigned int clear);
111 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
112 static int  whiteheat_chars_in_buffer(struct tty_struct *tty);
113 static void whiteheat_throttle(struct tty_struct *tty);
114 static void whiteheat_unthrottle(struct tty_struct *tty);
115 static void whiteheat_read_callback(struct urb *urb);
116 static void whiteheat_write_callback(struct urb *urb);
117
118 static struct usb_serial_driver whiteheat_fake_device = {
119         .driver = {
120                 .owner =        THIS_MODULE,
121                 .name =         "whiteheatnofirm",
122         },
123         .description =          "Connect Tech - WhiteHEAT - (prerenumeration)",
124         .usb_driver =           &whiteheat_driver,
125         .id_table =             id_table_prerenumeration,
126         .num_ports =            1,
127         .probe =                whiteheat_firmware_download,
128         .attach =               whiteheat_firmware_attach,
129 };
130
131 static struct usb_serial_driver whiteheat_device = {
132         .driver = {
133                 .owner =        THIS_MODULE,
134                 .name =         "whiteheat",
135         },
136         .description =          "Connect Tech - WhiteHEAT",
137         .usb_driver =           &whiteheat_driver,
138         .id_table =             id_table_std,
139         .num_ports =            4,
140         .attach =               whiteheat_attach,
141         .release =              whiteheat_release,
142         .open =                 whiteheat_open,
143         .close =                whiteheat_close,
144         .write =                whiteheat_write,
145         .write_room =           whiteheat_write_room,
146         .ioctl =                whiteheat_ioctl,
147         .set_termios =          whiteheat_set_termios,
148         .break_ctl =            whiteheat_break_ctl,
149         .tiocmget =             whiteheat_tiocmget,
150         .tiocmset =             whiteheat_tiocmset,
151         .chars_in_buffer =      whiteheat_chars_in_buffer,
152         .throttle =             whiteheat_throttle,
153         .unthrottle =           whiteheat_unthrottle,
154         .read_bulk_callback =   whiteheat_read_callback,
155         .write_bulk_callback =  whiteheat_write_callback,
156 };
157
158
159 struct whiteheat_command_private {
160         struct mutex            mutex;
161         __u8                    port_running;
162         __u8                    command_finished;
163         wait_queue_head_t       wait_command; /* for handling sleeping whilst
164                                                  waiting for a command to
165                                                  finish */
166         __u8                    result_buffer[64];
167 };
168
169
170 #define THROTTLED               0x01
171 #define ACTUALLY_THROTTLED      0x02
172
173 static int urb_pool_size = 8;
174
175 struct whiteheat_urb_wrap {
176         struct list_head        list;
177         struct urb              *urb;
178 };
179
180 struct whiteheat_private {
181         spinlock_t              lock;
182         __u8                    flags;
183         __u8                    mcr;            /* FIXME: no locking on mcr */
184         struct list_head        rx_urbs_free;
185         struct list_head        rx_urbs_submitted;
186         struct list_head        rx_urb_q;
187         struct work_struct      rx_work;
188         struct usb_serial_port  *port;
189         struct list_head        tx_urbs_free;
190         struct list_head        tx_urbs_submitted;
191         struct mutex            deathwarrant;
192 };
193
194
195 /* local function prototypes */
196 static int start_command_port(struct usb_serial *serial);
197 static void stop_command_port(struct usb_serial *serial);
198 static void command_port_write_callback(struct urb *urb);
199 static void command_port_read_callback(struct urb *urb);
200
201 static int start_port_read(struct usb_serial_port *port);
202 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
203                                                 struct list_head *head);
204 static struct list_head *list_first(struct list_head *head);
205 static void rx_data_softint(struct work_struct *work);
206
207 static int firm_send_command(struct usb_serial_port *port, __u8 command,
208                                                 __u8 *data, __u8 datasize);
209 static int firm_open(struct usb_serial_port *port);
210 static int firm_close(struct usb_serial_port *port);
211 static void firm_setup_port(struct tty_struct *tty);
212 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
213 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
214 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
215 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
216 static int firm_get_dtr_rts(struct usb_serial_port *port);
217 static int firm_report_tx_done(struct usb_serial_port *port);
218
219
220 #define COMMAND_PORT            4
221 #define COMMAND_TIMEOUT         (2*HZ)  /* 2 second timeout for a command */
222 #define COMMAND_TIMEOUT_MS      2000
223 #define CLOSING_DELAY           (30 * HZ)
224
225
226 /*****************************************************************************
227  * Connect Tech's White Heat prerenumeration driver functions
228  *****************************************************************************/
229
230 /* steps to download the firmware to the WhiteHEAT device:
231  - hold the reset (by writing to the reset bit of the CPUCS register)
232  - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
233  - release the reset (by writing to the CPUCS register)
234  - download the WH.HEX file for all addresses greater than 0x1b3f using
235    VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
236  - hold the reset
237  - download the WH.HEX file for all addresses less than 0x1b40 using
238    VENDOR_REQUEST_ANCHOR_LOAD
239  - release the reset
240  - device renumerated itself and comes up as new device id with all
241    firmware download completed.
242 */
243 static int whiteheat_firmware_download(struct usb_serial *serial,
244                                         const struct usb_device_id *id)
245 {
246         int response, ret = -ENOENT;
247         const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
248         const struct ihex_binrec *record;
249
250         dbg("%s", __func__);
251
252         if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
253                                   &serial->dev->dev)) {
254                 dev_err(&serial->dev->dev,
255                         "%s - request \"whiteheat.fw\" failed\n", __func__);
256                 goto out;
257         }
258         if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
259                              &serial->dev->dev)) {
260                 dev_err(&serial->dev->dev,
261                         "%s - request \"whiteheat_loader.fw\" failed\n",
262                         __func__);
263                 goto out;
264         }
265         ret = 0;
266         response = ezusb_set_reset (serial, 1);
267
268         record = (const struct ihex_binrec *)loader_fw->data;
269         while (record) {
270                 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
271                                               (unsigned char *)record->data,
272                                               be16_to_cpu(record->len), 0xa0);
273                 if (response < 0) {
274                         dev_err(&serial->dev->dev, "%s - ezusb_writememory "
275                                 "failed for loader (%d %04X %p %d)\n",
276                                 __func__, response, be32_to_cpu(record->addr),
277                                 record->data, be16_to_cpu(record->len));
278                         break;
279                 }
280                 record = ihex_next_binrec(record);
281         }
282
283         response = ezusb_set_reset(serial, 0);
284
285         record = (const struct ihex_binrec *)firmware_fw->data;
286         while (record && be32_to_cpu(record->addr) < 0x1b40)
287                 record = ihex_next_binrec(record);
288         while (record) {
289                 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
290                                               (unsigned char *)record->data,
291                                               be16_to_cpu(record->len), 0xa3);
292                 if (response < 0) {
293                         dev_err(&serial->dev->dev, "%s - ezusb_writememory "
294                                 "failed for first firmware step "
295                                 "(%d %04X %p %d)\n", __func__, response,
296                                 be32_to_cpu(record->addr), record->data,
297                                 be16_to_cpu(record->len));
298                         break;
299                 }
300                 ++record;
301         }
302
303         response = ezusb_set_reset(serial, 1);
304
305         record = (const struct ihex_binrec *)firmware_fw->data;
306         while (record && be32_to_cpu(record->addr) < 0x1b40) {
307                 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
308                                               (unsigned char *)record->data,
309                                               be16_to_cpu(record->len), 0xa0);
310                 if (response < 0) {
311                         dev_err(&serial->dev->dev, "%s - ezusb_writememory "
312                                 "failed for second firmware step "
313                                 "(%d %04X %p %d)\n", __func__, response,
314                                 be32_to_cpu(record->addr), record->data,
315                                 be16_to_cpu(record->len));
316                         break;
317                 }
318                 ++record;
319         }
320         ret = 0;
321         response = ezusb_set_reset (serial, 0);
322  out:
323         release_firmware(loader_fw);
324         release_firmware(firmware_fw);
325         return ret;
326 }
327
328
329 static int whiteheat_firmware_attach(struct usb_serial *serial)
330 {
331         /* We want this device to fail to have a driver assigned to it */
332         return 1;
333 }
334
335
336 /*****************************************************************************
337  * Connect Tech's White Heat serial driver functions
338  *****************************************************************************/
339 static int whiteheat_attach(struct usb_serial *serial)
340 {
341         struct usb_serial_port *command_port;
342         struct whiteheat_command_private *command_info;
343         struct usb_serial_port *port;
344         struct whiteheat_private *info;
345         struct whiteheat_hw_info *hw_info;
346         int pipe;
347         int ret;
348         int alen;
349         __u8 *command;
350         __u8 *result;
351         int i;
352         int j;
353         struct urb *urb;
354         int buf_size;
355         struct whiteheat_urb_wrap *wrap;
356         struct list_head *tmp;
357
358         command_port = serial->port[COMMAND_PORT];
359
360         pipe = usb_sndbulkpipe(serial->dev,
361                         command_port->bulk_out_endpointAddress);
362         command = kmalloc(2, GFP_KERNEL);
363         if (!command)
364                 goto no_command_buffer;
365         command[0] = WHITEHEAT_GET_HW_INFO;
366         command[1] = 0;
367
368         result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
369         if (!result)
370                 goto no_result_buffer;
371         /*
372          * When the module is reloaded the firmware is still there and
373          * the endpoints are still in the usb core unchanged. This is the
374          * unlinking bug in disguise. Same for the call below.
375          */
376         usb_clear_halt(serial->dev, pipe);
377         ret = usb_bulk_msg(serial->dev, pipe, command, 2,
378                                                 &alen, COMMAND_TIMEOUT_MS);
379         if (ret) {
380                 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
381                         serial->type->description, ret);
382                 goto no_firmware;
383         } else if (alen != 2) {
384                 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
385                         serial->type->description, alen);
386                 goto no_firmware;
387         }
388
389         pipe = usb_rcvbulkpipe(serial->dev,
390                                 command_port->bulk_in_endpointAddress);
391         /* See the comment on the usb_clear_halt() above */
392         usb_clear_halt(serial->dev, pipe);
393         ret = usb_bulk_msg(serial->dev, pipe, result,
394                         sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
395         if (ret) {
396                 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
397                         serial->type->description, ret);
398                 goto no_firmware;
399         } else if (alen != sizeof(*hw_info) + 1) {
400                 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
401                         serial->type->description, alen);
402                 goto no_firmware;
403         } else if (result[0] != command[0]) {
404                 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
405                         serial->type->description, result[0]);
406                 goto no_firmware;
407         }
408
409         hw_info = (struct whiteheat_hw_info *)&result[1];
410
411         dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
412                  serial->type->description, DRIVER_VERSION,
413                  hw_info->sw_major_rev, hw_info->sw_minor_rev);
414
415         for (i = 0; i < serial->num_ports; i++) {
416                 port = serial->port[i];
417
418                 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
419                 if (info == NULL) {
420                         dev_err(&port->dev,
421                                 "%s: Out of memory for port structures\n",
422                                 serial->type->description);
423                         goto no_private;
424                 }
425
426                 spin_lock_init(&info->lock);
427                 mutex_init(&info->deathwarrant);
428                 info->flags = 0;
429                 info->mcr = 0;
430                 INIT_WORK(&info->rx_work, rx_data_softint);
431                 info->port = port;
432
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);
438
439                 for (j = 0; j < urb_pool_size; j++) {
440                         urb = usb_alloc_urb(0, GFP_KERNEL);
441                         if (!urb) {
442                                 dev_err(&port->dev, "No free urbs available\n");
443                                 goto no_rx_urb;
444                         }
445                         buf_size = port->read_urb->transfer_buffer_length;
446                         urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
447                         if (!urb->transfer_buffer) {
448                                 dev_err(&port->dev,
449                                         "Couldn't allocate urb buffer\n");
450                                 goto no_rx_buf;
451                         }
452                         wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
453                         if (!wrap) {
454                                 dev_err(&port->dev,
455                                         "Couldn't allocate urb wrapper\n");
456                                 goto no_rx_wrap;
457                         }
458                         usb_fill_bulk_urb(urb, serial->dev,
459                                         usb_rcvbulkpipe(serial->dev,
460                                                 port->bulk_in_endpointAddress),
461                                         urb->transfer_buffer, buf_size,
462                                         whiteheat_read_callback, port);
463                         wrap->urb = urb;
464                         list_add(&wrap->list, &info->rx_urbs_free);
465
466                         urb = usb_alloc_urb(0, GFP_KERNEL);
467                         if (!urb) {
468                                 dev_err(&port->dev, "No free urbs available\n");
469                                 goto no_tx_urb;
470                         }
471                         buf_size = port->write_urb->transfer_buffer_length;
472                         urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
473                         if (!urb->transfer_buffer) {
474                                 dev_err(&port->dev,
475                                         "Couldn't allocate urb buffer\n");
476                                 goto no_tx_buf;
477                         }
478                         wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
479                         if (!wrap) {
480                                 dev_err(&port->dev,
481                                         "Couldn't allocate urb wrapper\n");
482                                 goto no_tx_wrap;
483                         }
484                         usb_fill_bulk_urb(urb, serial->dev,
485                                         usb_sndbulkpipe(serial->dev,
486                                                 port->bulk_out_endpointAddress),
487                                         urb->transfer_buffer, buf_size,
488                                         whiteheat_write_callback, port);
489                         wrap->urb = urb;
490                         list_add(&wrap->list, &info->tx_urbs_free);
491                 }
492
493                 usb_set_serial_port_data(port, info);
494         }
495
496         command_info = kmalloc(sizeof(struct whiteheat_command_private),
497                                                                 GFP_KERNEL);
498         if (command_info == NULL) {
499                 dev_err(&serial->dev->dev,
500                         "%s: Out of memory for port structures\n",
501                         serial->type->description);
502                 goto no_command_private;
503         }
504
505         mutex_init(&command_info->mutex);
506         command_info->port_running = 0;
507         init_waitqueue_head(&command_info->wait_command);
508         usb_set_serial_port_data(command_port, command_info);
509         command_port->write_urb->complete = command_port_write_callback;
510         command_port->read_urb->complete = command_port_read_callback;
511         kfree(result);
512         kfree(command);
513
514         return 0;
515
516 no_firmware:
517         /* Firmware likely not running */
518         dev_err(&serial->dev->dev,
519                 "%s: Unable to retrieve firmware version, try replugging\n",
520                 serial->type->description);
521         dev_err(&serial->dev->dev,
522                 "%s: If the firmware is not running (status led not blinking)\n",
523                 serial->type->description);
524         dev_err(&serial->dev->dev,
525                 "%s: please contact support@connecttech.com\n",
526                 serial->type->description);
527         kfree(result);
528         return -ENODEV;
529
530 no_command_private:
531         for (i = serial->num_ports - 1; i >= 0; i--) {
532                 port = serial->port[i];
533                 info = usb_get_serial_port_data(port);
534                 for (j = urb_pool_size - 1; j >= 0; j--) {
535                         tmp = list_first(&info->tx_urbs_free);
536                         list_del(tmp);
537                         wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
538                         urb = wrap->urb;
539                         kfree(wrap);
540 no_tx_wrap:
541                         kfree(urb->transfer_buffer);
542 no_tx_buf:
543                         usb_free_urb(urb);
544 no_tx_urb:
545                         tmp = list_first(&info->rx_urbs_free);
546                         list_del(tmp);
547                         wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
548                         urb = wrap->urb;
549                         kfree(wrap);
550 no_rx_wrap:
551                         kfree(urb->transfer_buffer);
552 no_rx_buf:
553                         usb_free_urb(urb);
554 no_rx_urb:
555                         ;
556                 }
557                 kfree(info);
558 no_private:
559                 ;
560         }
561         kfree(result);
562 no_result_buffer:
563         kfree(command);
564 no_command_buffer:
565         return -ENOMEM;
566 }
567
568
569 static void whiteheat_release(struct usb_serial *serial)
570 {
571         struct usb_serial_port *command_port;
572         struct usb_serial_port *port;
573         struct whiteheat_private *info;
574         struct whiteheat_urb_wrap *wrap;
575         struct urb *urb;
576         struct list_head *tmp;
577         struct list_head *tmp2;
578         int i;
579
580         dbg("%s", __func__);
581
582         /* free up our private data for our command port */
583         command_port = serial->port[COMMAND_PORT];
584         kfree(usb_get_serial_port_data(command_port));
585
586         for (i = 0; i < serial->num_ports; i++) {
587                 port = serial->port[i];
588                 info = usb_get_serial_port_data(port);
589                 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
590                         list_del(tmp);
591                         wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
592                         urb = wrap->urb;
593                         kfree(wrap);
594                         kfree(urb->transfer_buffer);
595                         usb_free_urb(urb);
596                 }
597                 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
598                         list_del(tmp);
599                         wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
600                         urb = wrap->urb;
601                         kfree(wrap);
602                         kfree(urb->transfer_buffer);
603                         usb_free_urb(urb);
604                 }
605                 kfree(info);
606         }
607 }
608
609 static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
610 {
611         int             retval = 0;
612
613         dbg("%s - port %d", __func__, port->number);
614
615         retval = start_command_port(port->serial);
616         if (retval)
617                 goto exit;
618
619         if (tty)
620                 tty->low_latency = 1;
621
622         /* send an open port command */
623         retval = firm_open(port);
624         if (retval) {
625                 stop_command_port(port->serial);
626                 goto exit;
627         }
628
629         retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
630         if (retval) {
631                 firm_close(port);
632                 stop_command_port(port->serial);
633                 goto exit;
634         }
635
636         if (tty)
637                 firm_setup_port(tty);
638
639         /* Work around HCD bugs */
640         usb_clear_halt(port->serial->dev, port->read_urb->pipe);
641         usb_clear_halt(port->serial->dev, port->write_urb->pipe);
642
643         /* Start reading from the device */
644         retval = start_port_read(port);
645         if (retval) {
646                 dev_err(&port->dev,
647                         "%s - failed submitting read urb, error %d\n",
648                         __func__, retval);
649                 firm_close(port);
650                 stop_command_port(port->serial);
651                 goto exit;
652         }
653
654 exit:
655         dbg("%s - exit, retval = %d", __func__, retval);
656         return retval;
657 }
658
659
660 static void whiteheat_close(struct usb_serial_port *port)
661 {
662         struct whiteheat_private *info = usb_get_serial_port_data(port);
663         struct whiteheat_urb_wrap *wrap;
664         struct urb *urb;
665         struct list_head *tmp;
666         struct list_head *tmp2;
667
668         dbg("%s - port %d", __func__, port->number);
669
670         firm_report_tx_done(port);
671         firm_close(port);
672
673         /* shutdown our bulk reads and writes */
674         mutex_lock(&info->deathwarrant);
675         spin_lock_irq(&info->lock);
676         list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
677                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
678                 urb = wrap->urb;
679                 list_del(tmp);
680                 spin_unlock_irq(&info->lock);
681                 usb_kill_urb(urb);
682                 spin_lock_irq(&info->lock);
683                 list_add(tmp, &info->rx_urbs_free);
684         }
685         list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
686                 list_move(tmp, &info->rx_urbs_free);
687         list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
688                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
689                 urb = wrap->urb;
690                 list_del(tmp);
691                 spin_unlock_irq(&info->lock);
692                 usb_kill_urb(urb);
693                 spin_lock_irq(&info->lock);
694                 list_add(tmp, &info->tx_urbs_free);
695         }
696         spin_unlock_irq(&info->lock);
697         mutex_unlock(&info->deathwarrant);
698         stop_command_port(port->serial);
699 }
700
701
702 static int whiteheat_write(struct tty_struct *tty,
703         struct usb_serial_port *port, const unsigned char *buf, int count)
704 {
705         struct whiteheat_private *info = usb_get_serial_port_data(port);
706         struct whiteheat_urb_wrap *wrap;
707         struct urb *urb;
708         int result;
709         int bytes;
710         int sent = 0;
711         unsigned long flags;
712         struct list_head *tmp;
713
714         dbg("%s - port %d", __func__, port->number);
715
716         if (count == 0) {
717                 dbg("%s - write request of 0 bytes", __func__);
718                 return (0);
719         }
720
721         while (count) {
722                 spin_lock_irqsave(&info->lock, flags);
723                 if (list_empty(&info->tx_urbs_free)) {
724                         spin_unlock_irqrestore(&info->lock, flags);
725                         break;
726                 }
727                 tmp = list_first(&info->tx_urbs_free);
728                 list_del(tmp);
729                 spin_unlock_irqrestore(&info->lock, flags);
730
731                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
732                 urb = wrap->urb;
733                 bytes = (count > port->bulk_out_size) ?
734                                         port->bulk_out_size : count;
735                 memcpy(urb->transfer_buffer, buf + sent, bytes);
736
737                 usb_serial_debug_data(debug, &port->dev,
738                                 __func__, bytes, urb->transfer_buffer);
739
740                 urb->transfer_buffer_length = bytes;
741                 result = usb_submit_urb(urb, GFP_ATOMIC);
742                 if (result) {
743                         dev_err(&port->dev,
744                                 "%s - failed submitting write urb, error %d\n",
745                                 __func__, result);
746                         sent = result;
747                         spin_lock_irqsave(&info->lock, flags);
748                         list_add(tmp, &info->tx_urbs_free);
749                         spin_unlock_irqrestore(&info->lock, flags);
750                         break;
751                 } else {
752                         sent += bytes;
753                         count -= bytes;
754                         spin_lock_irqsave(&info->lock, flags);
755                         list_add(tmp, &info->tx_urbs_submitted);
756                         spin_unlock_irqrestore(&info->lock, flags);
757                 }
758         }
759
760         return sent;
761 }
762
763 static int whiteheat_write_room(struct tty_struct *tty)
764 {
765         struct usb_serial_port *port = tty->driver_data;
766         struct whiteheat_private *info = usb_get_serial_port_data(port);
767         struct list_head *tmp;
768         int room = 0;
769         unsigned long flags;
770
771         dbg("%s - port %d", __func__, port->number);
772
773         spin_lock_irqsave(&info->lock, flags);
774         list_for_each(tmp, &info->tx_urbs_free)
775                 room++;
776         spin_unlock_irqrestore(&info->lock, flags);
777         room *= port->bulk_out_size;
778
779         dbg("%s - returns %d", __func__, room);
780         return (room);
781 }
782
783 static int whiteheat_tiocmget(struct tty_struct *tty)
784 {
785         struct usb_serial_port *port = tty->driver_data;
786         struct whiteheat_private *info = usb_get_serial_port_data(port);
787         unsigned int modem_signals = 0;
788
789         dbg("%s - port %d", __func__, port->number);
790
791         firm_get_dtr_rts(port);
792         if (info->mcr & UART_MCR_DTR)
793                 modem_signals |= TIOCM_DTR;
794         if (info->mcr & UART_MCR_RTS)
795                 modem_signals |= TIOCM_RTS;
796
797         return modem_signals;
798 }
799
800 static int whiteheat_tiocmset(struct tty_struct *tty,
801                                unsigned int set, unsigned int clear)
802 {
803         struct usb_serial_port *port = tty->driver_data;
804         struct whiteheat_private *info = usb_get_serial_port_data(port);
805
806         dbg("%s - port %d", __func__, port->number);
807
808         if (set & TIOCM_RTS)
809                 info->mcr |= UART_MCR_RTS;
810         if (set & TIOCM_DTR)
811                 info->mcr |= UART_MCR_DTR;
812
813         if (clear & TIOCM_RTS)
814                 info->mcr &= ~UART_MCR_RTS;
815         if (clear & TIOCM_DTR)
816                 info->mcr &= ~UART_MCR_DTR;
817
818         firm_set_dtr(port, info->mcr & UART_MCR_DTR);
819         firm_set_rts(port, info->mcr & UART_MCR_RTS);
820         return 0;
821 }
822
823
824 static int whiteheat_ioctl(struct tty_struct *tty,
825                                         unsigned int cmd, unsigned long arg)
826 {
827         struct usb_serial_port *port = tty->driver_data;
828         struct serial_struct serstruct;
829         void __user *user_arg = (void __user *)arg;
830
831         dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
832
833         switch (cmd) {
834         case TIOCGSERIAL:
835                 memset(&serstruct, 0, sizeof(serstruct));
836                 serstruct.type = PORT_16654;
837                 serstruct.line = port->serial->minor;
838                 serstruct.port = port->number;
839                 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
840                 serstruct.xmit_fifo_size = port->bulk_out_size;
841                 serstruct.custom_divisor = 0;
842                 serstruct.baud_base = 460800;
843                 serstruct.close_delay = CLOSING_DELAY;
844                 serstruct.closing_wait = CLOSING_DELAY;
845
846                 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
847                         return -EFAULT;
848                 break;
849         default:
850                 break;
851         }
852
853         return -ENOIOCTLCMD;
854 }
855
856
857 static void whiteheat_set_termios(struct tty_struct *tty,
858         struct usb_serial_port *port, struct ktermios *old_termios)
859 {
860         firm_setup_port(tty);
861 }
862
863 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
864 {
865         struct usb_serial_port *port = tty->driver_data;
866         firm_set_break(port, break_state);
867 }
868
869
870 static int whiteheat_chars_in_buffer(struct tty_struct *tty)
871 {
872         struct usb_serial_port *port = tty->driver_data;
873         struct whiteheat_private *info = usb_get_serial_port_data(port);
874         struct list_head *tmp;
875         struct whiteheat_urb_wrap *wrap;
876         int chars = 0;
877         unsigned long flags;
878
879         dbg("%s - port %d", __func__, port->number);
880
881         spin_lock_irqsave(&info->lock, flags);
882         list_for_each(tmp, &info->tx_urbs_submitted) {
883                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
884                 chars += wrap->urb->transfer_buffer_length;
885         }
886         spin_unlock_irqrestore(&info->lock, flags);
887
888         dbg("%s - returns %d", __func__, chars);
889         return chars;
890 }
891
892
893 static void whiteheat_throttle(struct tty_struct *tty)
894 {
895         struct usb_serial_port *port = tty->driver_data;
896         struct whiteheat_private *info = usb_get_serial_port_data(port);
897
898         dbg("%s - port %d", __func__, port->number);
899
900         spin_lock_irq(&info->lock);
901         info->flags |= THROTTLED;
902         spin_unlock_irq(&info->lock);
903 }
904
905
906 static void whiteheat_unthrottle(struct tty_struct *tty)
907 {
908         struct usb_serial_port *port = tty->driver_data;
909         struct whiteheat_private *info = usb_get_serial_port_data(port);
910         int actually_throttled;
911
912         dbg("%s - port %d", __func__, port->number);
913
914         spin_lock_irq(&info->lock);
915         actually_throttled = info->flags & ACTUALLY_THROTTLED;
916         info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
917         spin_unlock_irq(&info->lock);
918
919         if (actually_throttled)
920                 rx_data_softint(&info->rx_work);
921 }
922
923
924 /*****************************************************************************
925  * Connect Tech's White Heat callback routines
926  *****************************************************************************/
927 static void command_port_write_callback(struct urb *urb)
928 {
929         int status = urb->status;
930
931         dbg("%s", __func__);
932
933         if (status) {
934                 dbg("nonzero urb status: %d", status);
935                 return;
936         }
937 }
938
939
940 static void command_port_read_callback(struct urb *urb)
941 {
942         struct usb_serial_port *command_port = urb->context;
943         struct whiteheat_command_private *command_info;
944         int status = urb->status;
945         unsigned char *data = urb->transfer_buffer;
946         int result;
947
948         dbg("%s", __func__);
949
950         command_info = usb_get_serial_port_data(command_port);
951         if (!command_info) {
952                 dbg("%s - command_info is NULL, exiting.", __func__);
953                 return;
954         }
955         if (status) {
956                 dbg("%s - nonzero urb status: %d", __func__, status);
957                 if (status != -ENOENT)
958                         command_info->command_finished = WHITEHEAT_CMD_FAILURE;
959                 wake_up(&command_info->wait_command);
960                 return;
961         }
962
963         usb_serial_debug_data(debug, &command_port->dev,
964                                 __func__, urb->actual_length, data);
965
966         if (data[0] == WHITEHEAT_CMD_COMPLETE) {
967                 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
968                 wake_up(&command_info->wait_command);
969         } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
970                 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
971                 wake_up(&command_info->wait_command);
972         } else if (data[0] == WHITEHEAT_EVENT) {
973                 /* These are unsolicited reports from the firmware, hence no
974                    waiting command to wakeup */
975                 dbg("%s - event received", __func__);
976         } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
977                 memcpy(command_info->result_buffer, &data[1],
978                                                 urb->actual_length - 1);
979                 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
980                 wake_up(&command_info->wait_command);
981         } else
982                 dbg("%s - bad reply from firmware", __func__);
983
984         /* Continue trying to always read */
985         result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
986         if (result)
987                 dbg("%s - failed resubmitting read urb, error %d",
988                         __func__, result);
989 }
990
991
992 static void whiteheat_read_callback(struct urb *urb)
993 {
994         struct usb_serial_port *port = urb->context;
995         struct whiteheat_urb_wrap *wrap;
996         unsigned char *data = urb->transfer_buffer;
997         struct whiteheat_private *info = usb_get_serial_port_data(port);
998         int status = urb->status;
999
1000         dbg("%s - port %d", __func__, port->number);
1001
1002         spin_lock(&info->lock);
1003         wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1004         if (!wrap) {
1005                 spin_unlock(&info->lock);
1006                 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1007                 return;
1008         }
1009         list_del(&wrap->list);
1010         spin_unlock(&info->lock);
1011
1012         if (status) {
1013                 dbg("%s - nonzero read bulk status received: %d",
1014                     __func__, status);
1015                 spin_lock(&info->lock);
1016                 list_add(&wrap->list, &info->rx_urbs_free);
1017                 spin_unlock(&info->lock);
1018                 return;
1019         }
1020
1021         usb_serial_debug_data(debug, &port->dev,
1022                                 __func__, urb->actual_length, data);
1023
1024         spin_lock(&info->lock);
1025         list_add_tail(&wrap->list, &info->rx_urb_q);
1026         if (info->flags & THROTTLED) {
1027                 info->flags |= ACTUALLY_THROTTLED;
1028                 spin_unlock(&info->lock);
1029                 return;
1030         }
1031         spin_unlock(&info->lock);
1032
1033         schedule_work(&info->rx_work);
1034 }
1035
1036
1037 static void whiteheat_write_callback(struct urb *urb)
1038 {
1039         struct usb_serial_port *port = urb->context;
1040         struct whiteheat_private *info = usb_get_serial_port_data(port);
1041         struct whiteheat_urb_wrap *wrap;
1042         int status = urb->status;
1043
1044         dbg("%s - port %d", __func__, port->number);
1045
1046         spin_lock(&info->lock);
1047         wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1048         if (!wrap) {
1049                 spin_unlock(&info->lock);
1050                 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1051                 return;
1052         }
1053         list_move(&wrap->list, &info->tx_urbs_free);
1054         spin_unlock(&info->lock);
1055
1056         if (status) {
1057                 dbg("%s - nonzero write bulk status received: %d",
1058                     __func__, status);
1059                 return;
1060         }
1061
1062         usb_serial_port_softint(port);
1063 }
1064
1065
1066 /*****************************************************************************
1067  * Connect Tech's White Heat firmware interface
1068  *****************************************************************************/
1069 static int firm_send_command(struct usb_serial_port *port, __u8 command,
1070                                                 __u8 *data, __u8 datasize)
1071 {
1072         struct usb_serial_port *command_port;
1073         struct whiteheat_command_private *command_info;
1074         struct whiteheat_private *info;
1075         __u8 *transfer_buffer;
1076         int retval = 0;
1077         int t;
1078
1079         dbg("%s - command %d", __func__, command);
1080
1081         command_port = port->serial->port[COMMAND_PORT];
1082         command_info = usb_get_serial_port_data(command_port);
1083         mutex_lock(&command_info->mutex);
1084         command_info->command_finished = false;
1085
1086         transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1087         transfer_buffer[0] = command;
1088         memcpy(&transfer_buffer[1], data, datasize);
1089         command_port->write_urb->transfer_buffer_length = datasize + 1;
1090         retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1091         if (retval) {
1092                 dbg("%s - submit urb failed", __func__);
1093                 goto exit;
1094         }
1095
1096         /* wait for the command to complete */
1097         t = wait_event_timeout(command_info->wait_command,
1098                 (bool)command_info->command_finished, COMMAND_TIMEOUT);
1099         if (!t)
1100                 usb_kill_urb(command_port->write_urb);
1101
1102         if (command_info->command_finished == false) {
1103                 dbg("%s - command timed out.", __func__);
1104                 retval = -ETIMEDOUT;
1105                 goto exit;
1106         }
1107
1108         if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1109                 dbg("%s - command failed.", __func__);
1110                 retval = -EIO;
1111                 goto exit;
1112         }
1113
1114         if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1115                 dbg("%s - command completed.", __func__);
1116                 switch (command) {
1117                 case WHITEHEAT_GET_DTR_RTS:
1118                         info = usb_get_serial_port_data(port);
1119                         memcpy(&info->mcr, command_info->result_buffer,
1120                                         sizeof(struct whiteheat_dr_info));
1121                                 break;
1122                 }
1123         }
1124 exit:
1125         mutex_unlock(&command_info->mutex);
1126         return retval;
1127 }
1128
1129
1130 static int firm_open(struct usb_serial_port *port)
1131 {
1132         struct whiteheat_simple open_command;
1133
1134         open_command.port = port->number - port->serial->minor + 1;
1135         return firm_send_command(port, WHITEHEAT_OPEN,
1136                 (__u8 *)&open_command, sizeof(open_command));
1137 }
1138
1139
1140 static int firm_close(struct usb_serial_port *port)
1141 {
1142         struct whiteheat_simple close_command;
1143
1144         close_command.port = port->number - port->serial->minor + 1;
1145         return firm_send_command(port, WHITEHEAT_CLOSE,
1146                         (__u8 *)&close_command, sizeof(close_command));
1147 }
1148
1149
1150 static void firm_setup_port(struct tty_struct *tty)
1151 {
1152         struct usb_serial_port *port = tty->driver_data;
1153         struct whiteheat_port_settings port_settings;
1154         unsigned int cflag = tty->termios->c_cflag;
1155
1156         port_settings.port = port->number + 1;
1157
1158         /* get the byte size */
1159         switch (cflag & CSIZE) {
1160         case CS5:       port_settings.bits = 5;   break;
1161         case CS6:       port_settings.bits = 6;   break;
1162         case CS7:       port_settings.bits = 7;   break;
1163         default:
1164         case CS8:       port_settings.bits = 8;   break;
1165         }
1166         dbg("%s - data bits = %d", __func__, port_settings.bits);
1167
1168         /* determine the parity */
1169         if (cflag & PARENB)
1170                 if (cflag & CMSPAR)
1171                         if (cflag & PARODD)
1172                                 port_settings.parity = WHITEHEAT_PAR_MARK;
1173                         else
1174                                 port_settings.parity = WHITEHEAT_PAR_SPACE;
1175                 else
1176                         if (cflag & PARODD)
1177                                 port_settings.parity = WHITEHEAT_PAR_ODD;
1178                         else
1179                                 port_settings.parity = WHITEHEAT_PAR_EVEN;
1180         else
1181                 port_settings.parity = WHITEHEAT_PAR_NONE;
1182         dbg("%s - parity = %c", __func__, port_settings.parity);
1183
1184         /* figure out the stop bits requested */
1185         if (cflag & CSTOPB)
1186                 port_settings.stop = 2;
1187         else
1188                 port_settings.stop = 1;
1189         dbg("%s - stop bits = %d", __func__, port_settings.stop);
1190
1191         /* figure out the flow control settings */
1192         if (cflag & CRTSCTS)
1193                 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1194                                                 WHITEHEAT_HFLOW_RTS);
1195         else
1196                 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1197         dbg("%s - hardware flow control = %s %s %s %s", __func__,
1198             (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1199             (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1200             (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1201             (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1202
1203         /* determine software flow control */
1204         if (I_IXOFF(tty))
1205                 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1206         else
1207                 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1208         dbg("%s - software flow control = %c", __func__, port_settings.sflow);
1209
1210         port_settings.xon = START_CHAR(tty);
1211         port_settings.xoff = STOP_CHAR(tty);
1212         dbg("%s - XON = %2x, XOFF = %2x",
1213                         __func__, port_settings.xon, port_settings.xoff);
1214
1215         /* get the baud rate wanted */
1216         port_settings.baud = tty_get_baud_rate(tty);
1217         dbg("%s - baud rate = %d", __func__, port_settings.baud);
1218
1219         /* fixme: should set validated settings */
1220         tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1221         /* handle any settings that aren't specified in the tty structure */
1222         port_settings.lloop = 0;
1223
1224         /* now send the message to the device */
1225         firm_send_command(port, WHITEHEAT_SETUP_PORT,
1226                         (__u8 *)&port_settings, sizeof(port_settings));
1227 }
1228
1229
1230 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1231 {
1232         struct whiteheat_set_rdb rts_command;
1233
1234         rts_command.port = port->number - port->serial->minor + 1;
1235         rts_command.state = onoff;
1236         return firm_send_command(port, WHITEHEAT_SET_RTS,
1237                         (__u8 *)&rts_command, sizeof(rts_command));
1238 }
1239
1240
1241 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1242 {
1243         struct whiteheat_set_rdb dtr_command;
1244
1245         dtr_command.port = port->number - port->serial->minor + 1;
1246         dtr_command.state = onoff;
1247         return firm_send_command(port, WHITEHEAT_SET_DTR,
1248                         (__u8 *)&dtr_command, sizeof(dtr_command));
1249 }
1250
1251
1252 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1253 {
1254         struct whiteheat_set_rdb break_command;
1255
1256         break_command.port = port->number - port->serial->minor + 1;
1257         break_command.state = onoff;
1258         return firm_send_command(port, WHITEHEAT_SET_BREAK,
1259                         (__u8 *)&break_command, sizeof(break_command));
1260 }
1261
1262
1263 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1264 {
1265         struct whiteheat_purge purge_command;
1266
1267         purge_command.port = port->number - port->serial->minor + 1;
1268         purge_command.what = rxtx;
1269         return firm_send_command(port, WHITEHEAT_PURGE,
1270                         (__u8 *)&purge_command, sizeof(purge_command));
1271 }
1272
1273
1274 static int firm_get_dtr_rts(struct usb_serial_port *port)
1275 {
1276         struct whiteheat_simple get_dr_command;
1277
1278         get_dr_command.port = port->number - port->serial->minor + 1;
1279         return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1280                         (__u8 *)&get_dr_command, sizeof(get_dr_command));
1281 }
1282
1283
1284 static int firm_report_tx_done(struct usb_serial_port *port)
1285 {
1286         struct whiteheat_simple close_command;
1287
1288         close_command.port = port->number - port->serial->minor + 1;
1289         return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1290                         (__u8 *)&close_command, sizeof(close_command));
1291 }
1292
1293
1294 /*****************************************************************************
1295  * Connect Tech's White Heat utility functions
1296  *****************************************************************************/
1297 static int start_command_port(struct usb_serial *serial)
1298 {
1299         struct usb_serial_port *command_port;
1300         struct whiteheat_command_private *command_info;
1301         int retval = 0;
1302
1303         command_port = serial->port[COMMAND_PORT];
1304         command_info = usb_get_serial_port_data(command_port);
1305         mutex_lock(&command_info->mutex);
1306         if (!command_info->port_running) {
1307                 /* Work around HCD bugs */
1308                 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1309
1310                 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1311                 if (retval) {
1312                         dev_err(&serial->dev->dev,
1313                                 "%s - failed submitting read urb, error %d\n",
1314                                 __func__, retval);
1315                         goto exit;
1316                 }
1317         }
1318         command_info->port_running++;
1319
1320 exit:
1321         mutex_unlock(&command_info->mutex);
1322         return retval;
1323 }
1324
1325
1326 static void stop_command_port(struct usb_serial *serial)
1327 {
1328         struct usb_serial_port *command_port;
1329         struct whiteheat_command_private *command_info;
1330
1331         command_port = serial->port[COMMAND_PORT];
1332         command_info = usb_get_serial_port_data(command_port);
1333         mutex_lock(&command_info->mutex);
1334         command_info->port_running--;
1335         if (!command_info->port_running)
1336                 usb_kill_urb(command_port->read_urb);
1337         mutex_unlock(&command_info->mutex);
1338 }
1339
1340
1341 static int start_port_read(struct usb_serial_port *port)
1342 {
1343         struct whiteheat_private *info = usb_get_serial_port_data(port);
1344         struct whiteheat_urb_wrap *wrap;
1345         struct urb *urb;
1346         int retval = 0;
1347         unsigned long flags;
1348         struct list_head *tmp;
1349         struct list_head *tmp2;
1350
1351         spin_lock_irqsave(&info->lock, flags);
1352
1353         list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1354                 list_del(tmp);
1355                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1356                 urb = wrap->urb;
1357                 spin_unlock_irqrestore(&info->lock, flags);
1358                 retval = usb_submit_urb(urb, GFP_KERNEL);
1359                 if (retval) {
1360                         spin_lock_irqsave(&info->lock, flags);
1361                         list_add(tmp, &info->rx_urbs_free);
1362                         list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1363                                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1364                                 urb = wrap->urb;
1365                                 list_del(tmp);
1366                                 spin_unlock_irqrestore(&info->lock, flags);
1367                                 usb_kill_urb(urb);
1368                                 spin_lock_irqsave(&info->lock, flags);
1369                                 list_add(tmp, &info->rx_urbs_free);
1370                         }
1371                         break;
1372                 }
1373                 spin_lock_irqsave(&info->lock, flags);
1374                 list_add(tmp, &info->rx_urbs_submitted);
1375         }
1376
1377         spin_unlock_irqrestore(&info->lock, flags);
1378
1379         return retval;
1380 }
1381
1382
1383 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1384                                                 struct list_head *head)
1385 {
1386         struct whiteheat_urb_wrap *wrap;
1387         struct list_head *tmp;
1388
1389         list_for_each(tmp, head) {
1390                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1391                 if (wrap->urb == urb)
1392                         return wrap;
1393         }
1394
1395         return NULL;
1396 }
1397
1398
1399 static struct list_head *list_first(struct list_head *head)
1400 {
1401         return head->next;
1402 }
1403
1404
1405 static void rx_data_softint(struct work_struct *work)
1406 {
1407         struct whiteheat_private *info =
1408                 container_of(work, struct whiteheat_private, rx_work);
1409         struct usb_serial_port *port = info->port;
1410         struct tty_struct *tty = tty_port_tty_get(&port->port);
1411         struct whiteheat_urb_wrap *wrap;
1412         struct urb *urb;
1413         unsigned long flags;
1414         struct list_head *tmp;
1415         struct list_head *tmp2;
1416         int result;
1417         int sent = 0;
1418
1419         spin_lock_irqsave(&info->lock, flags);
1420         if (info->flags & THROTTLED) {
1421                 spin_unlock_irqrestore(&info->lock, flags);
1422                 goto out;
1423         }
1424
1425         list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1426                 list_del(tmp);
1427                 spin_unlock_irqrestore(&info->lock, flags);
1428
1429                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1430                 urb = wrap->urb;
1431
1432                 if (tty && urb->actual_length)
1433                         sent += tty_insert_flip_string(tty,
1434                                 urb->transfer_buffer, urb->actual_length);
1435
1436                 result = usb_submit_urb(urb, GFP_ATOMIC);
1437                 if (result) {
1438                         dev_err(&port->dev,
1439                                 "%s - failed resubmitting read urb, error %d\n",
1440                                 __func__, result);
1441                         spin_lock_irqsave(&info->lock, flags);
1442                         list_add(tmp, &info->rx_urbs_free);
1443                         continue;
1444                 }
1445
1446                 spin_lock_irqsave(&info->lock, flags);
1447                 list_add(tmp, &info->rx_urbs_submitted);
1448         }
1449         spin_unlock_irqrestore(&info->lock, flags);
1450
1451         if (sent)
1452                 tty_flip_buffer_push(tty);
1453 out:
1454         tty_kref_put(tty);
1455 }
1456
1457
1458 /*****************************************************************************
1459  * Connect Tech's White Heat module functions
1460  *****************************************************************************/
1461 static int __init whiteheat_init(void)
1462 {
1463         int retval;
1464         retval = usb_serial_register(&whiteheat_fake_device);
1465         if (retval)
1466                 goto failed_fake_register;
1467         retval = usb_serial_register(&whiteheat_device);
1468         if (retval)
1469                 goto failed_device_register;
1470         retval = usb_register(&whiteheat_driver);
1471         if (retval)
1472                 goto failed_usb_register;
1473         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1474                DRIVER_DESC "\n");
1475         return 0;
1476 failed_usb_register:
1477         usb_serial_deregister(&whiteheat_device);
1478 failed_device_register:
1479         usb_serial_deregister(&whiteheat_fake_device);
1480 failed_fake_register:
1481         return retval;
1482 }
1483
1484
1485 static void __exit whiteheat_exit(void)
1486 {
1487         usb_deregister(&whiteheat_driver);
1488         usb_serial_deregister(&whiteheat_fake_device);
1489         usb_serial_deregister(&whiteheat_device);
1490 }
1491
1492
1493 module_init(whiteheat_init);
1494 module_exit(whiteheat_exit);
1495
1496 MODULE_AUTHOR(DRIVER_AUTHOR);
1497 MODULE_DESCRIPTION(DRIVER_DESC);
1498 MODULE_LICENSE("GPL");
1499
1500 MODULE_FIRMWARE("whiteheat.fw");
1501 MODULE_FIRMWARE("whiteheat_loader.fw");
1502
1503 module_param(urb_pool_size, int, 0);
1504 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1505
1506 module_param(debug, bool, S_IRUGO | S_IWUSR);
1507 MODULE_PARM_DESC(debug, "Debug enabled or not");