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