USB: serial: remove changelogs and old todo entries
[cascardo/linux.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 usb_serial *serial = port->serial;
706         struct whiteheat_private *info = usb_get_serial_port_data(port);
707         struct whiteheat_urb_wrap *wrap;
708         struct urb *urb;
709         int result;
710         int bytes;
711         int sent = 0;
712         unsigned long flags;
713         struct list_head *tmp;
714
715         dbg("%s - port %d", __func__, port->number);
716
717         if (count == 0) {
718                 dbg("%s - write request of 0 bytes", __func__);
719                 return (0);
720         }
721
722         while (count) {
723                 spin_lock_irqsave(&info->lock, flags);
724                 if (list_empty(&info->tx_urbs_free)) {
725                         spin_unlock_irqrestore(&info->lock, flags);
726                         break;
727                 }
728                 tmp = list_first(&info->tx_urbs_free);
729                 list_del(tmp);
730                 spin_unlock_irqrestore(&info->lock, flags);
731
732                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
733                 urb = wrap->urb;
734                 bytes = (count > port->bulk_out_size) ?
735                                         port->bulk_out_size : count;
736                 memcpy(urb->transfer_buffer, buf + sent, bytes);
737
738                 usb_serial_debug_data(debug, &port->dev,
739                                 __func__, bytes, urb->transfer_buffer);
740
741                 urb->dev = serial->dev;
742                 urb->transfer_buffer_length = bytes;
743                 result = usb_submit_urb(urb, GFP_ATOMIC);
744                 if (result) {
745                         dev_err(&port->dev,
746                                 "%s - failed submitting write urb, error %d\n",
747                                 __func__, result);
748                         sent = result;
749                         spin_lock_irqsave(&info->lock, flags);
750                         list_add(tmp, &info->tx_urbs_free);
751                         spin_unlock_irqrestore(&info->lock, flags);
752                         break;
753                 } else {
754                         sent += bytes;
755                         count -= bytes;
756                         spin_lock_irqsave(&info->lock, flags);
757                         list_add(tmp, &info->tx_urbs_submitted);
758                         spin_unlock_irqrestore(&info->lock, flags);
759                 }
760         }
761
762         return sent;
763 }
764
765 static int whiteheat_write_room(struct tty_struct *tty)
766 {
767         struct usb_serial_port *port = tty->driver_data;
768         struct whiteheat_private *info = usb_get_serial_port_data(port);
769         struct list_head *tmp;
770         int room = 0;
771         unsigned long flags;
772
773         dbg("%s - port %d", __func__, port->number);
774
775         spin_lock_irqsave(&info->lock, flags);
776         list_for_each(tmp, &info->tx_urbs_free)
777                 room++;
778         spin_unlock_irqrestore(&info->lock, flags);
779         room *= port->bulk_out_size;
780
781         dbg("%s - returns %d", __func__, room);
782         return (room);
783 }
784
785 static int whiteheat_tiocmget(struct tty_struct *tty)
786 {
787         struct usb_serial_port *port = tty->driver_data;
788         struct whiteheat_private *info = usb_get_serial_port_data(port);
789         unsigned int modem_signals = 0;
790
791         dbg("%s - port %d", __func__, port->number);
792
793         firm_get_dtr_rts(port);
794         if (info->mcr & UART_MCR_DTR)
795                 modem_signals |= TIOCM_DTR;
796         if (info->mcr & UART_MCR_RTS)
797                 modem_signals |= TIOCM_RTS;
798
799         return modem_signals;
800 }
801
802 static int whiteheat_tiocmset(struct tty_struct *tty,
803                                unsigned int set, unsigned int clear)
804 {
805         struct usb_serial_port *port = tty->driver_data;
806         struct whiteheat_private *info = usb_get_serial_port_data(port);
807
808         dbg("%s - port %d", __func__, port->number);
809
810         if (set & TIOCM_RTS)
811                 info->mcr |= UART_MCR_RTS;
812         if (set & TIOCM_DTR)
813                 info->mcr |= UART_MCR_DTR;
814
815         if (clear & TIOCM_RTS)
816                 info->mcr &= ~UART_MCR_RTS;
817         if (clear & TIOCM_DTR)
818                 info->mcr &= ~UART_MCR_DTR;
819
820         firm_set_dtr(port, info->mcr & UART_MCR_DTR);
821         firm_set_rts(port, info->mcr & UART_MCR_RTS);
822         return 0;
823 }
824
825
826 static int whiteheat_ioctl(struct tty_struct *tty,
827                                         unsigned int cmd, unsigned long arg)
828 {
829         struct usb_serial_port *port = tty->driver_data;
830         struct serial_struct serstruct;
831         void __user *user_arg = (void __user *)arg;
832
833         dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
834
835         switch (cmd) {
836         case TIOCGSERIAL:
837                 memset(&serstruct, 0, sizeof(serstruct));
838                 serstruct.type = PORT_16654;
839                 serstruct.line = port->serial->minor;
840                 serstruct.port = port->number;
841                 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
842                 serstruct.xmit_fifo_size = port->bulk_out_size;
843                 serstruct.custom_divisor = 0;
844                 serstruct.baud_base = 460800;
845                 serstruct.close_delay = CLOSING_DELAY;
846                 serstruct.closing_wait = CLOSING_DELAY;
847
848                 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
849                         return -EFAULT;
850                 break;
851         default:
852                 break;
853         }
854
855         return -ENOIOCTLCMD;
856 }
857
858
859 static void whiteheat_set_termios(struct tty_struct *tty,
860         struct usb_serial_port *port, struct ktermios *old_termios)
861 {
862         firm_setup_port(tty);
863 }
864
865 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
866 {
867         struct usb_serial_port *port = tty->driver_data;
868         firm_set_break(port, break_state);
869 }
870
871
872 static int whiteheat_chars_in_buffer(struct tty_struct *tty)
873 {
874         struct usb_serial_port *port = tty->driver_data;
875         struct whiteheat_private *info = usb_get_serial_port_data(port);
876         struct list_head *tmp;
877         struct whiteheat_urb_wrap *wrap;
878         int chars = 0;
879         unsigned long flags;
880
881         dbg("%s - port %d", __func__, port->number);
882
883         spin_lock_irqsave(&info->lock, flags);
884         list_for_each(tmp, &info->tx_urbs_submitted) {
885                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
886                 chars += wrap->urb->transfer_buffer_length;
887         }
888         spin_unlock_irqrestore(&info->lock, flags);
889
890         dbg("%s - returns %d", __func__, chars);
891         return chars;
892 }
893
894
895 static void whiteheat_throttle(struct tty_struct *tty)
896 {
897         struct usb_serial_port *port = tty->driver_data;
898         struct whiteheat_private *info = usb_get_serial_port_data(port);
899
900         dbg("%s - port %d", __func__, port->number);
901
902         spin_lock_irq(&info->lock);
903         info->flags |= THROTTLED;
904         spin_unlock_irq(&info->lock);
905 }
906
907
908 static void whiteheat_unthrottle(struct tty_struct *tty)
909 {
910         struct usb_serial_port *port = tty->driver_data;
911         struct whiteheat_private *info = usb_get_serial_port_data(port);
912         int actually_throttled;
913
914         dbg("%s - port %d", __func__, port->number);
915
916         spin_lock_irq(&info->lock);
917         actually_throttled = info->flags & ACTUALLY_THROTTLED;
918         info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
919         spin_unlock_irq(&info->lock);
920
921         if (actually_throttled)
922                 rx_data_softint(&info->rx_work);
923 }
924
925
926 /*****************************************************************************
927  * Connect Tech's White Heat callback routines
928  *****************************************************************************/
929 static void command_port_write_callback(struct urb *urb)
930 {
931         int status = urb->status;
932
933         dbg("%s", __func__);
934
935         if (status) {
936                 dbg("nonzero urb status: %d", status);
937                 return;
938         }
939 }
940
941
942 static void command_port_read_callback(struct urb *urb)
943 {
944         struct usb_serial_port *command_port = urb->context;
945         struct whiteheat_command_private *command_info;
946         int status = urb->status;
947         unsigned char *data = urb->transfer_buffer;
948         int result;
949
950         dbg("%s", __func__);
951
952         command_info = usb_get_serial_port_data(command_port);
953         if (!command_info) {
954                 dbg("%s - command_info is NULL, exiting.", __func__);
955                 return;
956         }
957         if (status) {
958                 dbg("%s - nonzero urb status: %d", __func__, status);
959                 if (status != -ENOENT)
960                         command_info->command_finished = WHITEHEAT_CMD_FAILURE;
961                 wake_up(&command_info->wait_command);
962                 return;
963         }
964
965         usb_serial_debug_data(debug, &command_port->dev,
966                                 __func__, urb->actual_length, data);
967
968         if (data[0] == WHITEHEAT_CMD_COMPLETE) {
969                 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
970                 wake_up(&command_info->wait_command);
971         } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
972                 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
973                 wake_up(&command_info->wait_command);
974         } else if (data[0] == WHITEHEAT_EVENT) {
975                 /* These are unsolicited reports from the firmware, hence no
976                    waiting command to wakeup */
977                 dbg("%s - event received", __func__);
978         } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
979                 memcpy(command_info->result_buffer, &data[1],
980                                                 urb->actual_length - 1);
981                 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
982                 wake_up(&command_info->wait_command);
983         } else
984                 dbg("%s - bad reply from firmware", __func__);
985
986         /* Continue trying to always read */
987         command_port->read_urb->dev = command_port->serial->dev;
988         result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
989         if (result)
990                 dbg("%s - failed resubmitting read urb, error %d",
991                         __func__, result);
992 }
993
994
995 static void whiteheat_read_callback(struct urb *urb)
996 {
997         struct usb_serial_port *port = urb->context;
998         struct whiteheat_urb_wrap *wrap;
999         unsigned char *data = urb->transfer_buffer;
1000         struct whiteheat_private *info = usb_get_serial_port_data(port);
1001         int status = urb->status;
1002
1003         dbg("%s - port %d", __func__, port->number);
1004
1005         spin_lock(&info->lock);
1006         wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1007         if (!wrap) {
1008                 spin_unlock(&info->lock);
1009                 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1010                 return;
1011         }
1012         list_del(&wrap->list);
1013         spin_unlock(&info->lock);
1014
1015         if (status) {
1016                 dbg("%s - nonzero read bulk status received: %d",
1017                     __func__, status);
1018                 spin_lock(&info->lock);
1019                 list_add(&wrap->list, &info->rx_urbs_free);
1020                 spin_unlock(&info->lock);
1021                 return;
1022         }
1023
1024         usb_serial_debug_data(debug, &port->dev,
1025                                 __func__, urb->actual_length, data);
1026
1027         spin_lock(&info->lock);
1028         list_add_tail(&wrap->list, &info->rx_urb_q);
1029         if (info->flags & THROTTLED) {
1030                 info->flags |= ACTUALLY_THROTTLED;
1031                 spin_unlock(&info->lock);
1032                 return;
1033         }
1034         spin_unlock(&info->lock);
1035
1036         schedule_work(&info->rx_work);
1037 }
1038
1039
1040 static void whiteheat_write_callback(struct urb *urb)
1041 {
1042         struct usb_serial_port *port = urb->context;
1043         struct whiteheat_private *info = usb_get_serial_port_data(port);
1044         struct whiteheat_urb_wrap *wrap;
1045         int status = urb->status;
1046
1047         dbg("%s - port %d", __func__, port->number);
1048
1049         spin_lock(&info->lock);
1050         wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1051         if (!wrap) {
1052                 spin_unlock(&info->lock);
1053                 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1054                 return;
1055         }
1056         list_move(&wrap->list, &info->tx_urbs_free);
1057         spin_unlock(&info->lock);
1058
1059         if (status) {
1060                 dbg("%s - nonzero write bulk status received: %d",
1061                     __func__, status);
1062                 return;
1063         }
1064
1065         usb_serial_port_softint(port);
1066 }
1067
1068
1069 /*****************************************************************************
1070  * Connect Tech's White Heat firmware interface
1071  *****************************************************************************/
1072 static int firm_send_command(struct usb_serial_port *port, __u8 command,
1073                                                 __u8 *data, __u8 datasize)
1074 {
1075         struct usb_serial_port *command_port;
1076         struct whiteheat_command_private *command_info;
1077         struct whiteheat_private *info;
1078         __u8 *transfer_buffer;
1079         int retval = 0;
1080         int t;
1081
1082         dbg("%s - command %d", __func__, command);
1083
1084         command_port = port->serial->port[COMMAND_PORT];
1085         command_info = usb_get_serial_port_data(command_port);
1086         mutex_lock(&command_info->mutex);
1087         command_info->command_finished = false;
1088
1089         transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1090         transfer_buffer[0] = command;
1091         memcpy(&transfer_buffer[1], data, datasize);
1092         command_port->write_urb->transfer_buffer_length = datasize + 1;
1093         command_port->write_urb->dev = port->serial->dev;
1094         retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1095         if (retval) {
1096                 dbg("%s - submit urb failed", __func__);
1097                 goto exit;
1098         }
1099
1100         /* wait for the command to complete */
1101         t = wait_event_timeout(command_info->wait_command,
1102                 (bool)command_info->command_finished, COMMAND_TIMEOUT);
1103         if (!t)
1104                 usb_kill_urb(command_port->write_urb);
1105
1106         if (command_info->command_finished == false) {
1107                 dbg("%s - command timed out.", __func__);
1108                 retval = -ETIMEDOUT;
1109                 goto exit;
1110         }
1111
1112         if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1113                 dbg("%s - command failed.", __func__);
1114                 retval = -EIO;
1115                 goto exit;
1116         }
1117
1118         if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1119                 dbg("%s - command completed.", __func__);
1120                 switch (command) {
1121                 case WHITEHEAT_GET_DTR_RTS:
1122                         info = usb_get_serial_port_data(port);
1123                         memcpy(&info->mcr, command_info->result_buffer,
1124                                         sizeof(struct whiteheat_dr_info));
1125                                 break;
1126                 }
1127         }
1128 exit:
1129         mutex_unlock(&command_info->mutex);
1130         return retval;
1131 }
1132
1133
1134 static int firm_open(struct usb_serial_port *port)
1135 {
1136         struct whiteheat_simple open_command;
1137
1138         open_command.port = port->number - port->serial->minor + 1;
1139         return firm_send_command(port, WHITEHEAT_OPEN,
1140                 (__u8 *)&open_command, sizeof(open_command));
1141 }
1142
1143
1144 static int firm_close(struct usb_serial_port *port)
1145 {
1146         struct whiteheat_simple close_command;
1147
1148         close_command.port = port->number - port->serial->minor + 1;
1149         return firm_send_command(port, WHITEHEAT_CLOSE,
1150                         (__u8 *)&close_command, sizeof(close_command));
1151 }
1152
1153
1154 static void firm_setup_port(struct tty_struct *tty)
1155 {
1156         struct usb_serial_port *port = tty->driver_data;
1157         struct whiteheat_port_settings port_settings;
1158         unsigned int cflag = tty->termios->c_cflag;
1159
1160         port_settings.port = port->number + 1;
1161
1162         /* get the byte size */
1163         switch (cflag & CSIZE) {
1164         case CS5:       port_settings.bits = 5;   break;
1165         case CS6:       port_settings.bits = 6;   break;
1166         case CS7:       port_settings.bits = 7;   break;
1167         default:
1168         case CS8:       port_settings.bits = 8;   break;
1169         }
1170         dbg("%s - data bits = %d", __func__, port_settings.bits);
1171
1172         /* determine the parity */
1173         if (cflag & PARENB)
1174                 if (cflag & CMSPAR)
1175                         if (cflag & PARODD)
1176                                 port_settings.parity = WHITEHEAT_PAR_MARK;
1177                         else
1178                                 port_settings.parity = WHITEHEAT_PAR_SPACE;
1179                 else
1180                         if (cflag & PARODD)
1181                                 port_settings.parity = WHITEHEAT_PAR_ODD;
1182                         else
1183                                 port_settings.parity = WHITEHEAT_PAR_EVEN;
1184         else
1185                 port_settings.parity = WHITEHEAT_PAR_NONE;
1186         dbg("%s - parity = %c", __func__, port_settings.parity);
1187
1188         /* figure out the stop bits requested */
1189         if (cflag & CSTOPB)
1190                 port_settings.stop = 2;
1191         else
1192                 port_settings.stop = 1;
1193         dbg("%s - stop bits = %d", __func__, port_settings.stop);
1194
1195         /* figure out the flow control settings */
1196         if (cflag & CRTSCTS)
1197                 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1198                                                 WHITEHEAT_HFLOW_RTS);
1199         else
1200                 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1201         dbg("%s - hardware flow control = %s %s %s %s", __func__,
1202             (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1203             (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1204             (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1205             (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1206
1207         /* determine software flow control */
1208         if (I_IXOFF(tty))
1209                 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1210         else
1211                 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1212         dbg("%s - software flow control = %c", __func__, port_settings.sflow);
1213
1214         port_settings.xon = START_CHAR(tty);
1215         port_settings.xoff = STOP_CHAR(tty);
1216         dbg("%s - XON = %2x, XOFF = %2x",
1217                         __func__, port_settings.xon, port_settings.xoff);
1218
1219         /* get the baud rate wanted */
1220         port_settings.baud = tty_get_baud_rate(tty);
1221         dbg("%s - baud rate = %d", __func__, port_settings.baud);
1222
1223         /* fixme: should set validated settings */
1224         tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1225         /* handle any settings that aren't specified in the tty structure */
1226         port_settings.lloop = 0;
1227
1228         /* now send the message to the device */
1229         firm_send_command(port, WHITEHEAT_SETUP_PORT,
1230                         (__u8 *)&port_settings, sizeof(port_settings));
1231 }
1232
1233
1234 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1235 {
1236         struct whiteheat_set_rdb rts_command;
1237
1238         rts_command.port = port->number - port->serial->minor + 1;
1239         rts_command.state = onoff;
1240         return firm_send_command(port, WHITEHEAT_SET_RTS,
1241                         (__u8 *)&rts_command, sizeof(rts_command));
1242 }
1243
1244
1245 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1246 {
1247         struct whiteheat_set_rdb dtr_command;
1248
1249         dtr_command.port = port->number - port->serial->minor + 1;
1250         dtr_command.state = onoff;
1251         return firm_send_command(port, WHITEHEAT_SET_DTR,
1252                         (__u8 *)&dtr_command, sizeof(dtr_command));
1253 }
1254
1255
1256 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1257 {
1258         struct whiteheat_set_rdb break_command;
1259
1260         break_command.port = port->number - port->serial->minor + 1;
1261         break_command.state = onoff;
1262         return firm_send_command(port, WHITEHEAT_SET_BREAK,
1263                         (__u8 *)&break_command, sizeof(break_command));
1264 }
1265
1266
1267 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1268 {
1269         struct whiteheat_purge purge_command;
1270
1271         purge_command.port = port->number - port->serial->minor + 1;
1272         purge_command.what = rxtx;
1273         return firm_send_command(port, WHITEHEAT_PURGE,
1274                         (__u8 *)&purge_command, sizeof(purge_command));
1275 }
1276
1277
1278 static int firm_get_dtr_rts(struct usb_serial_port *port)
1279 {
1280         struct whiteheat_simple get_dr_command;
1281
1282         get_dr_command.port = port->number - port->serial->minor + 1;
1283         return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1284                         (__u8 *)&get_dr_command, sizeof(get_dr_command));
1285 }
1286
1287
1288 static int firm_report_tx_done(struct usb_serial_port *port)
1289 {
1290         struct whiteheat_simple close_command;
1291
1292         close_command.port = port->number - port->serial->minor + 1;
1293         return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1294                         (__u8 *)&close_command, sizeof(close_command));
1295 }
1296
1297
1298 /*****************************************************************************
1299  * Connect Tech's White Heat utility functions
1300  *****************************************************************************/
1301 static int start_command_port(struct usb_serial *serial)
1302 {
1303         struct usb_serial_port *command_port;
1304         struct whiteheat_command_private *command_info;
1305         int retval = 0;
1306
1307         command_port = serial->port[COMMAND_PORT];
1308         command_info = usb_get_serial_port_data(command_port);
1309         mutex_lock(&command_info->mutex);
1310         if (!command_info->port_running) {
1311                 /* Work around HCD bugs */
1312                 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1313
1314                 command_port->read_urb->dev = serial->dev;
1315                 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1316                 if (retval) {
1317                         dev_err(&serial->dev->dev,
1318                                 "%s - failed submitting read urb, error %d\n",
1319                                 __func__, retval);
1320                         goto exit;
1321                 }
1322         }
1323         command_info->port_running++;
1324
1325 exit:
1326         mutex_unlock(&command_info->mutex);
1327         return retval;
1328 }
1329
1330
1331 static void stop_command_port(struct usb_serial *serial)
1332 {
1333         struct usb_serial_port *command_port;
1334         struct whiteheat_command_private *command_info;
1335
1336         command_port = serial->port[COMMAND_PORT];
1337         command_info = usb_get_serial_port_data(command_port);
1338         mutex_lock(&command_info->mutex);
1339         command_info->port_running--;
1340         if (!command_info->port_running)
1341                 usb_kill_urb(command_port->read_urb);
1342         mutex_unlock(&command_info->mutex);
1343 }
1344
1345
1346 static int start_port_read(struct usb_serial_port *port)
1347 {
1348         struct whiteheat_private *info = usb_get_serial_port_data(port);
1349         struct whiteheat_urb_wrap *wrap;
1350         struct urb *urb;
1351         int retval = 0;
1352         unsigned long flags;
1353         struct list_head *tmp;
1354         struct list_head *tmp2;
1355
1356         spin_lock_irqsave(&info->lock, flags);
1357
1358         list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1359                 list_del(tmp);
1360                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1361                 urb = wrap->urb;
1362                 urb->dev = port->serial->dev;
1363                 spin_unlock_irqrestore(&info->lock, flags);
1364                 retval = usb_submit_urb(urb, GFP_KERNEL);
1365                 if (retval) {
1366                         spin_lock_irqsave(&info->lock, flags);
1367                         list_add(tmp, &info->rx_urbs_free);
1368                         list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1369                                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1370                                 urb = wrap->urb;
1371                                 list_del(tmp);
1372                                 spin_unlock_irqrestore(&info->lock, flags);
1373                                 usb_kill_urb(urb);
1374                                 spin_lock_irqsave(&info->lock, flags);
1375                                 list_add(tmp, &info->rx_urbs_free);
1376                         }
1377                         break;
1378                 }
1379                 spin_lock_irqsave(&info->lock, flags);
1380                 list_add(tmp, &info->rx_urbs_submitted);
1381         }
1382
1383         spin_unlock_irqrestore(&info->lock, flags);
1384
1385         return retval;
1386 }
1387
1388
1389 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1390                                                 struct list_head *head)
1391 {
1392         struct whiteheat_urb_wrap *wrap;
1393         struct list_head *tmp;
1394
1395         list_for_each(tmp, head) {
1396                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1397                 if (wrap->urb == urb)
1398                         return wrap;
1399         }
1400
1401         return NULL;
1402 }
1403
1404
1405 static struct list_head *list_first(struct list_head *head)
1406 {
1407         return head->next;
1408 }
1409
1410
1411 static void rx_data_softint(struct work_struct *work)
1412 {
1413         struct whiteheat_private *info =
1414                 container_of(work, struct whiteheat_private, rx_work);
1415         struct usb_serial_port *port = info->port;
1416         struct tty_struct *tty = tty_port_tty_get(&port->port);
1417         struct whiteheat_urb_wrap *wrap;
1418         struct urb *urb;
1419         unsigned long flags;
1420         struct list_head *tmp;
1421         struct list_head *tmp2;
1422         int result;
1423         int sent = 0;
1424
1425         spin_lock_irqsave(&info->lock, flags);
1426         if (info->flags & THROTTLED) {
1427                 spin_unlock_irqrestore(&info->lock, flags);
1428                 goto out;
1429         }
1430
1431         list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1432                 list_del(tmp);
1433                 spin_unlock_irqrestore(&info->lock, flags);
1434
1435                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1436                 urb = wrap->urb;
1437
1438                 if (tty && urb->actual_length)
1439                         sent += tty_insert_flip_string(tty,
1440                                 urb->transfer_buffer, urb->actual_length);
1441
1442                 urb->dev = port->serial->dev;
1443                 result = usb_submit_urb(urb, GFP_ATOMIC);
1444                 if (result) {
1445                         dev_err(&port->dev,
1446                                 "%s - failed resubmitting read urb, error %d\n",
1447                                 __func__, result);
1448                         spin_lock_irqsave(&info->lock, flags);
1449                         list_add(tmp, &info->rx_urbs_free);
1450                         continue;
1451                 }
1452
1453                 spin_lock_irqsave(&info->lock, flags);
1454                 list_add(tmp, &info->rx_urbs_submitted);
1455         }
1456         spin_unlock_irqrestore(&info->lock, flags);
1457
1458         if (sent)
1459                 tty_flip_buffer_push(tty);
1460 out:
1461         tty_kref_put(tty);
1462 }
1463
1464
1465 /*****************************************************************************
1466  * Connect Tech's White Heat module functions
1467  *****************************************************************************/
1468 static int __init whiteheat_init(void)
1469 {
1470         int retval;
1471         retval = usb_serial_register(&whiteheat_fake_device);
1472         if (retval)
1473                 goto failed_fake_register;
1474         retval = usb_serial_register(&whiteheat_device);
1475         if (retval)
1476                 goto failed_device_register;
1477         retval = usb_register(&whiteheat_driver);
1478         if (retval)
1479                 goto failed_usb_register;
1480         printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1481                DRIVER_DESC "\n");
1482         return 0;
1483 failed_usb_register:
1484         usb_serial_deregister(&whiteheat_device);
1485 failed_device_register:
1486         usb_serial_deregister(&whiteheat_fake_device);
1487 failed_fake_register:
1488         return retval;
1489 }
1490
1491
1492 static void __exit whiteheat_exit(void)
1493 {
1494         usb_deregister(&whiteheat_driver);
1495         usb_serial_deregister(&whiteheat_fake_device);
1496         usb_serial_deregister(&whiteheat_device);
1497 }
1498
1499
1500 module_init(whiteheat_init);
1501 module_exit(whiteheat_exit);
1502
1503 MODULE_AUTHOR(DRIVER_AUTHOR);
1504 MODULE_DESCRIPTION(DRIVER_DESC);
1505 MODULE_LICENSE("GPL");
1506
1507 MODULE_FIRMWARE("whiteheat.fw");
1508 MODULE_FIRMWARE("whiteheat_loader.fw");
1509
1510 module_param(urb_pool_size, int, 0);
1511 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1512
1513 module_param(debug, bool, S_IRUGO | S_IWUSR);
1514 MODULE_PARM_DESC(debug, "Debug enabled or not");