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