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