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