ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / serial / whiteheat.c
1 /*
2  * USB ConnectTech WhiteHEAT driver
3  *
4  *      Copyright (C) 2002
5  *          Connect Tech Inc.
6  *
7  *      Copyright (C) 1999 - 2001
8  *          Greg Kroah-Hartman (greg@kroah.com)
9  *
10  *      This program is free software; you can redistribute it and/or modify
11  *      it under the terms of the GNU General Public License as published by
12  *      the Free Software Foundation; either version 2 of the License, or
13  *      (at your option) any later version.
14  *
15  * See Documentation/usb/usb-serial.txt for more information on using this 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
84 #ifdef CONFIG_USB_SERIAL_DEBUG
85         static int debug = 1;
86 #else
87         static int debug;
88 #endif
89
90 #include "usb-serial.h"
91 #include "whiteheat_fw.h"               /* firmware for the ConnectTech WhiteHEAT device */
92 #include "whiteheat.h"                  /* WhiteHEAT specific commands */
93
94 #ifndef CMSPAR
95 #define CMSPAR 0
96 #endif
97
98 /*
99  * Version Information
100  */
101 #define DRIVER_VERSION "v2.0"
102 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
103 #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
104
105 #define CONNECT_TECH_VENDOR_ID          0x0710
106 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
107 #define CONNECT_TECH_WHITE_HEAT_ID      0x8001
108
109 /*
110    ID tables for whiteheat are unusual, because we want to different
111    things for different versions of the device.  Eventually, this
112    will be doable from a single table.  But, for now, we define two
113    separate ID tables, and then a third table that combines them
114    just for the purpose of exporting the autoloading information.
115 */
116 static struct usb_device_id id_table_std [] = {
117         { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
118         { }                                             /* Terminating entry */
119 };
120
121 static struct usb_device_id id_table_prerenumeration [] = {
122         { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
123         { }                                             /* Terminating entry */
124 };
125
126 static struct usb_device_id id_table_combined [] = {
127         { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
128         { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
129         { }                                             /* Terminating entry */
130 };
131
132 MODULE_DEVICE_TABLE (usb, id_table_combined);
133
134 static struct usb_driver whiteheat_driver = {
135         .owner =        THIS_MODULE,
136         .name =         "whiteheat",
137         .probe =        usb_serial_probe,
138         .disconnect =   usb_serial_disconnect,
139         .id_table =     id_table_combined,
140 };
141
142 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
143 static int  whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id);
144 static int  whiteheat_firmware_attach   (struct usb_serial *serial);
145
146 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
147 static int  whiteheat_attach            (struct usb_serial *serial);
148 static void whiteheat_shutdown          (struct usb_serial *serial);
149 static int  whiteheat_open              (struct usb_serial_port *port, struct file *filp);
150 static void whiteheat_close             (struct usb_serial_port *port, struct file *filp);
151 static int  whiteheat_write             (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
152 static int  whiteheat_write_room        (struct usb_serial_port *port);
153 static int  whiteheat_ioctl             (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
154 static void whiteheat_set_termios       (struct usb_serial_port *port, struct termios * old);
155 static int  whiteheat_tiocmget          (struct usb_serial_port *port, struct file *file);
156 static int  whiteheat_tiocmset          (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
157 static void whiteheat_break_ctl         (struct usb_serial_port *port, int break_state);
158 static int  whiteheat_chars_in_buffer   (struct usb_serial_port *port);
159 static void whiteheat_throttle          (struct usb_serial_port *port);
160 static void whiteheat_unthrottle        (struct usb_serial_port *port);
161 static void whiteheat_read_callback     (struct urb *urb, struct pt_regs *regs);
162 static void whiteheat_write_callback    (struct urb *urb, struct pt_regs *regs);
163
164 static struct usb_serial_device_type whiteheat_fake_device = {
165         .owner =                THIS_MODULE,
166         .name =                 "Connect Tech - WhiteHEAT - (prerenumeration)",
167         .short_name =           "whiteheatnofirm",
168         .id_table =             id_table_prerenumeration,
169         .num_interrupt_in =     NUM_DONT_CARE,
170         .num_bulk_in =          NUM_DONT_CARE,
171         .num_bulk_out =         NUM_DONT_CARE,
172         .num_ports =            1,
173         .probe =                whiteheat_firmware_download,
174         .attach =               whiteheat_firmware_attach,
175 };
176
177 static struct usb_serial_device_type whiteheat_device = {
178         .owner =                THIS_MODULE,
179         .name =                 "Connect Tech - WhiteHEAT",
180         .short_name =           "whiteheat",
181         .id_table =             id_table_std,
182         .num_interrupt_in =     NUM_DONT_CARE,
183         .num_bulk_in =          NUM_DONT_CARE,
184         .num_bulk_out =         NUM_DONT_CARE,
185         .num_ports =            4,
186         .attach =               whiteheat_attach,
187         .shutdown =             whiteheat_shutdown,
188         .open =                 whiteheat_open,
189         .close =                whiteheat_close,
190         .write =                whiteheat_write,
191         .write_room =           whiteheat_write_room,
192         .ioctl =                whiteheat_ioctl,
193         .set_termios =          whiteheat_set_termios,
194         .break_ctl =            whiteheat_break_ctl,
195         .tiocmget =             whiteheat_tiocmget,
196         .tiocmset =             whiteheat_tiocmset,
197         .chars_in_buffer =      whiteheat_chars_in_buffer,
198         .throttle =             whiteheat_throttle,
199         .unthrottle =           whiteheat_unthrottle,
200         .read_bulk_callback =   whiteheat_read_callback,
201         .write_bulk_callback =  whiteheat_write_callback,
202 };
203
204
205 struct whiteheat_command_private {
206         spinlock_t              lock;
207         __u8                    port_running;
208         __u8                    command_finished;
209         wait_queue_head_t       wait_command;   /* for handling sleeping while waiting for a command to finish */
210         __u8                    result_buffer[64];
211 };
212
213
214 #define THROTTLED               0x01
215 #define ACTUALLY_THROTTLED      0x02
216
217 static int urb_pool_size = 8;
218
219 struct whiteheat_urb_wrap {
220         struct list_head        list;
221         struct urb              *urb;
222 };
223
224 struct whiteheat_private {
225         spinlock_t              lock;
226         __u8                    flags;
227         __u8                    mcr;
228         struct list_head        rx_urbs_free;
229         struct list_head        rx_urbs_submitted;
230         struct list_head        rx_urb_q;
231         struct work_struct      rx_work;
232         struct list_head        tx_urbs_free;
233         struct list_head        tx_urbs_submitted;
234 };
235
236
237 /* local function prototypes */
238 static int start_command_port(struct usb_serial *serial);
239 static void stop_command_port(struct usb_serial *serial);
240 static void command_port_write_callback(struct urb *urb, struct pt_regs *regs);
241 static void command_port_read_callback(struct urb *urb, struct pt_regs *regs);
242
243 static int start_port_read(struct usb_serial_port *port);
244 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb, struct list_head *head);
245 static struct list_head *list_first(struct list_head *head);
246 static void rx_data_softint(void *private);
247
248 static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize);
249 static int firm_open(struct usb_serial_port *port);
250 static int firm_close(struct usb_serial_port *port);
251 static int firm_setup_port(struct usb_serial_port *port);
252 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
253 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
254 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
255 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
256 static int firm_get_dtr_rts(struct usb_serial_port *port);
257 static int firm_report_tx_done(struct usb_serial_port *port);
258
259
260 #define COMMAND_PORT            4
261 #define COMMAND_TIMEOUT         (2*HZ)  /* 2 second timeout for a command */
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);
388         if (ret) {
389                 err("%s: Couldn't send command [%d]", serial->type->name, ret);
390                 goto no_firmware;
391         } else if (alen != sizeof(command)) {
392                 err("%s: Send command incomplete [%d]", serial->type->name, 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);
400         if (ret) {
401                 err("%s: Couldn't get results [%d]", serial->type->name, ret);
402                 goto no_firmware;
403         } else if (alen != sizeof(result)) {
404                 err("%s: Get results incomplete [%d]", serial->type->name, alen);
405                 goto no_firmware;
406         } else if (result[0] != command[0]) {
407                 err("%s: Command failed [%d]", serial->type->name, 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->name,
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->name);
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->name);
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->name);
509         err("%s: If the firmware is not running (status led not blinking)\n", serial->type->name);
510         err("%s: please contact support@connecttech.com\n", serial->type->name);
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         if (port->tty->ldisc.flush_buffer)
677                 port->tty->ldisc.flush_buffer(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_unlink_urb(urb);
689                 list_del(tmp);
690                 list_add(tmp, &info->rx_urbs_free);
691         }
692         list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
693                 list_del(tmp);
694                 list_add(tmp, &info->rx_urbs_free);
695         }
696         list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
697                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
698                 urb = wrap->urb;
699                 usb_unlink_urb(urb);
700                 list_del(tmp);
701                 list_add(tmp, &info->tx_urbs_free);
702         }
703         spin_unlock_irqrestore(&info->lock, flags);
704
705         stop_command_port(port->serial);
706
707         port->tty->closing = 0;
708 }
709
710
711 static int whiteheat_write(struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
712 {
713         struct usb_serial *serial = port->serial;
714         struct whiteheat_private *info = usb_get_serial_port_data(port);
715         struct whiteheat_urb_wrap *wrap;
716         struct urb *urb;
717         int result;
718         int bytes;
719         int sent = 0;
720         unsigned long flags;
721         struct list_head *tmp;
722
723         dbg("%s - port %d", __FUNCTION__, port->number);
724
725         if (count == 0) {
726                 dbg("%s - write request of 0 bytes", __FUNCTION__);
727                 return (0);
728         }
729
730         while (count) {
731                 spin_lock_irqsave(&info->lock, flags);
732                 if (list_empty(&info->tx_urbs_free)) {
733                         spin_unlock_irqrestore(&info->lock, flags);
734                         break;
735                 }
736                 tmp = list_first(&info->tx_urbs_free);
737                 list_del(tmp);
738                 spin_unlock_irqrestore(&info->lock, flags);
739
740                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
741                 urb = wrap->urb;
742                 bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count;
743                 if (from_user) {
744                         if (copy_from_user(urb->transfer_buffer, buf + sent, bytes))
745                                 return -EFAULT;
746                 } else {
747                         memcpy (urb->transfer_buffer, buf + sent, bytes);
748                 }
749
750                 usb_serial_debug_data (__FILE__, __FUNCTION__, bytes, urb->transfer_buffer);
751
752                 urb->dev = serial->dev;
753                 urb->transfer_buffer_length = bytes;
754                 result = usb_submit_urb(urb, GFP_ATOMIC);
755                 if (result) {
756                         err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
757                         sent = result;
758                         spin_lock_irqsave(&info->lock, flags);
759                         list_add(tmp, &info->tx_urbs_free);
760                         spin_unlock_irqrestore(&info->lock, flags);
761                         break;
762                 } else {
763                         sent += bytes;
764                         count -= bytes;
765                         spin_lock_irqsave(&info->lock, flags);
766                         list_add(tmp, &info->tx_urbs_submitted);
767                         spin_unlock_irqrestore(&info->lock, flags);
768                 }
769         }
770
771         return sent;
772 }
773
774
775 static int whiteheat_write_room(struct usb_serial_port *port)
776 {
777         struct whiteheat_private *info = usb_get_serial_port_data(port);
778         struct list_head *tmp;
779         int room = 0;
780         unsigned long flags;
781
782         dbg("%s - port %d", __FUNCTION__, port->number);
783         
784         spin_lock_irqsave(&info->lock, flags);
785         list_for_each(tmp, &info->tx_urbs_free)
786                 room++;
787         spin_unlock_irqrestore(&info->lock, flags);
788         room *= port->bulk_out_size;
789
790         dbg("%s - returns %d", __FUNCTION__, room);
791         return (room);
792 }
793
794
795 static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file)
796 {
797         struct whiteheat_private *info = usb_get_serial_port_data(port);
798         unsigned int modem_signals = 0;
799
800         dbg("%s - port %d", __FUNCTION__, port->number);
801
802         firm_get_dtr_rts(port);
803         if (info->mcr & UART_MCR_DTR)
804                 modem_signals |= TIOCM_DTR;
805         if (info->mcr & UART_MCR_RTS)
806                 modem_signals |= TIOCM_RTS;
807
808         return modem_signals;
809 }
810
811
812 static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file,
813                                unsigned int set, unsigned int clear)
814 {
815         struct whiteheat_private *info = usb_get_serial_port_data(port);
816
817         dbg("%s - port %d", __FUNCTION__, port->number);
818
819         if (set & TIOCM_RTS)
820                 info->mcr |= UART_MCR_RTS;
821         if (set & TIOCM_DTR)
822                 info->mcr |= UART_MCR_DTR;
823
824         if (clear & TIOCM_RTS)
825                 info->mcr &= ~UART_MCR_RTS;
826         if (clear & TIOCM_DTR)
827                 info->mcr &= ~UART_MCR_DTR;
828
829         firm_set_dtr(port, info->mcr & UART_MCR_DTR);
830         firm_set_rts(port, info->mcr & UART_MCR_RTS);
831         return 0;
832 }
833
834
835 static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
836 {
837         struct serial_struct serstruct;
838
839         dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
840
841         switch (cmd) {
842                 case TIOCGSERIAL:
843                         memset(&serstruct, 0, sizeof(serstruct));
844                         serstruct.type = PORT_16654;
845                         serstruct.line = port->serial->minor;
846                         serstruct.port = port->number;
847                         serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
848                         serstruct.xmit_fifo_size = port->bulk_out_size;
849                         serstruct.custom_divisor = 0;
850                         serstruct.baud_base = 460800;
851                         serstruct.close_delay = CLOSING_DELAY;
852                         serstruct.closing_wait = CLOSING_DELAY;
853
854                         if (copy_to_user((void *)arg, &serstruct, sizeof(serstruct)))
855                                 return -EFAULT;
856
857                         break;
858
859                 case TIOCSSERIAL:
860                         if (copy_from_user(&serstruct, (void *)arg, sizeof(serstruct)))
861                                 return -EFAULT;
862
863                         /*
864                          * For now this is ignored. dip sets the ASYNC_[V]HI flags
865                          * but this isn't used by us at all. Maybe someone somewhere
866                          * will need the custom_divisor setting.
867                          */
868
869                         break;
870
871                 default:
872                         break;
873         }
874
875         return -ENOIOCTLCMD;
876 }
877
878
879 static void whiteheat_set_termios (struct usb_serial_port *port, struct termios *old_termios)
880 {
881         dbg("%s -port %d", __FUNCTION__, port->number);
882
883         if ((!port->tty) || (!port->tty->termios)) {
884                 dbg("%s - no tty structures", __FUNCTION__);
885                 goto exit;
886         }
887         
888         /* check that they really want us to change something */
889         if (old_termios) {
890                 if ((port->tty->termios->c_cflag == old_termios->c_cflag) &&
891                     (port->tty->termios->c_iflag == old_termios->c_iflag)) {
892                         dbg("%s - nothing to change...", __FUNCTION__);
893                         goto exit;
894                 }
895         }
896
897         firm_setup_port(port);
898
899 exit:
900         return;
901 }
902
903
904 static void whiteheat_break_ctl(struct usb_serial_port *port, int break_state) {
905         firm_set_break(port, break_state);
906 }
907
908
909 static int whiteheat_chars_in_buffer(struct usb_serial_port *port)
910 {
911         struct whiteheat_private *info = usb_get_serial_port_data(port);
912         struct list_head *tmp;
913         struct whiteheat_urb_wrap *wrap;
914         int chars = 0;
915         unsigned long flags;
916
917         dbg("%s - port %d", __FUNCTION__, port->number);
918
919         spin_lock_irqsave(&info->lock, flags);
920         list_for_each(tmp, &info->tx_urbs_submitted) {
921                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
922                 chars += wrap->urb->transfer_buffer_length;
923         }
924         spin_unlock_irqrestore(&info->lock, flags);
925
926         dbg ("%s - returns %d", __FUNCTION__, chars);
927         return (chars);
928 }
929
930
931 static void whiteheat_throttle (struct usb_serial_port *port)
932 {
933         struct whiteheat_private *info = usb_get_serial_port_data(port);
934         unsigned long flags;
935
936         dbg("%s - port %d", __FUNCTION__, port->number);
937
938         spin_lock_irqsave(&info->lock, flags);
939         info->flags |= THROTTLED;
940         spin_unlock_irqrestore(&info->lock, flags);
941
942         return;
943 }
944
945
946 static void whiteheat_unthrottle (struct usb_serial_port *port)
947 {
948         struct whiteheat_private *info = usb_get_serial_port_data(port);
949         int actually_throttled;
950         unsigned long flags;
951
952         dbg("%s - port %d", __FUNCTION__, port->number);
953
954         spin_lock_irqsave(&info->lock, flags);
955         actually_throttled = info->flags & ACTUALLY_THROTTLED;
956         info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
957         spin_unlock_irqrestore(&info->lock, flags);
958
959         if (actually_throttled)
960                 rx_data_softint(port);
961
962         return;
963 }
964
965
966 /*****************************************************************************
967  * Connect Tech's White Heat callback routines
968  *****************************************************************************/
969 static void command_port_write_callback (struct urb *urb, struct pt_regs *regs)
970 {
971         dbg("%s", __FUNCTION__);
972
973         if (urb->status) {
974                 dbg ("nonzero urb status: %d", urb->status);
975                 return;
976         }
977
978         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, urb->transfer_buffer);
979
980         return;
981 }
982
983
984 static void command_port_read_callback (struct urb *urb, struct pt_regs *regs)
985 {
986         struct usb_serial_port *command_port = (struct usb_serial_port *)urb->context;
987         struct usb_serial *serial = get_usb_serial (command_port, __FUNCTION__);
988         struct whiteheat_command_private *command_info;
989         unsigned char *data = urb->transfer_buffer;
990         int result;
991         unsigned long flags;
992
993         dbg("%s", __FUNCTION__);
994
995         if (urb->status) {
996                 dbg("%s - nonzero urb status: %d", __FUNCTION__, urb->status);
997                 return;
998         }
999
1000         if (!serial) {
1001                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
1002                 return;
1003         }
1004         
1005         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
1006
1007         command_info = usb_get_serial_port_data(command_port);
1008         if (!command_info) {
1009                 dbg ("%s - command_info is NULL, exiting.", __FUNCTION__);
1010                 return;
1011         }
1012         spin_lock_irqsave(&command_info->lock, flags);
1013
1014         if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1015                 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1016                 wake_up_interruptible(&command_info->wait_command);
1017         } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1018                 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1019                 wake_up_interruptible(&command_info->wait_command);
1020         } else if (data[0] == WHITEHEAT_EVENT) {
1021                 /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */
1022                 dbg("%s - event received", __FUNCTION__);
1023         } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
1024                 memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1);
1025                 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1026                 wake_up_interruptible(&command_info->wait_command);
1027         } else {
1028                 dbg("%s - bad reply from firmware", __FUNCTION__);
1029         }
1030         
1031         /* Continue trying to always read */
1032         command_port->read_urb->dev = serial->dev;
1033         result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
1034         spin_unlock_irqrestore(&command_info->lock, flags);
1035         if (result)
1036                 dbg("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1037 }
1038
1039
1040 static void whiteheat_read_callback(struct urb *urb, struct pt_regs *regs)
1041 {
1042         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1043         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1044         struct whiteheat_urb_wrap *wrap;
1045         unsigned char *data = urb->transfer_buffer;
1046         struct whiteheat_private *info = usb_get_serial_port_data(port);
1047
1048         dbg("%s - port %d", __FUNCTION__, port->number);
1049
1050         spin_lock(&info->lock);
1051         wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1052         if (!wrap) {
1053                 spin_unlock(&info->lock);
1054                 err("%s - Not my urb!", __FUNCTION__);
1055                 return;
1056         }
1057         list_del(&wrap->list);
1058         spin_unlock(&info->lock);
1059
1060         if (!serial) {
1061                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
1062                 spin_lock(&info->lock);
1063                 list_add(&wrap->list, &info->rx_urbs_free);
1064                 spin_unlock(&info->lock);
1065                 return;
1066         }
1067
1068         if (urb->status) {
1069                 dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
1070                 spin_lock(&info->lock);
1071                 list_add(&wrap->list, &info->rx_urbs_free);
1072                 spin_unlock(&info->lock);
1073                 return;
1074         }
1075
1076         usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
1077
1078         spin_lock(&info->lock);
1079         list_add_tail(&wrap->list, &info->rx_urb_q);
1080         if (info->flags & THROTTLED) {
1081                 info->flags |= ACTUALLY_THROTTLED;
1082                 spin_unlock(&info->lock);
1083                 return;
1084         }
1085         spin_unlock(&info->lock);
1086
1087         schedule_work(&info->rx_work);
1088 }
1089
1090
1091 static void whiteheat_write_callback(struct urb *urb, struct pt_regs *regs)
1092 {
1093         struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1094         struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
1095         struct whiteheat_private *info = usb_get_serial_port_data(port);
1096         struct whiteheat_urb_wrap *wrap;
1097
1098         dbg("%s - port %d", __FUNCTION__, port->number);
1099
1100         spin_lock(&info->lock);
1101         wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1102         if (!wrap) {
1103                 spin_unlock(&info->lock);
1104                 err("%s - Not my urb!", __FUNCTION__);
1105                 return;
1106         }
1107         list_del(&wrap->list);
1108         list_add(&wrap->list, &info->tx_urbs_free);
1109         spin_unlock(&info->lock);
1110
1111         if (!serial) {
1112                 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
1113                 return;
1114         }
1115
1116         if (urb->status) {
1117                 dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
1118                 return;
1119         }
1120
1121         usb_serial_port_softint((void *)port);
1122
1123         schedule_work(&port->work);
1124 }
1125
1126
1127 /*****************************************************************************
1128  * Connect Tech's White Heat firmware interface
1129  *****************************************************************************/
1130 static int firm_send_command (struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize)
1131 {
1132         struct usb_serial_port *command_port;
1133         struct whiteheat_command_private *command_info;
1134         struct whiteheat_private *info;
1135         __u8 *transfer_buffer;
1136         int retval = 0;
1137         unsigned long flags;
1138
1139         dbg("%s - command %d", __FUNCTION__, command);
1140
1141         command_port = port->serial->port[COMMAND_PORT];
1142         command_info = usb_get_serial_port_data(command_port);
1143         spin_lock_irqsave(&command_info->lock, flags);
1144         command_info->command_finished = FALSE;
1145         
1146         transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1147         transfer_buffer[0] = command;
1148         memcpy (&transfer_buffer[1], data, datasize);
1149         command_port->write_urb->transfer_buffer_length = datasize + 1;
1150         command_port->write_urb->dev = port->serial->dev;
1151         retval = usb_submit_urb (command_port->write_urb, GFP_KERNEL);
1152         spin_unlock_irqrestore(&command_info->lock, flags);
1153         if (retval) {
1154                 dbg("%s - submit urb failed", __FUNCTION__);
1155                 goto exit;
1156         }
1157
1158         /* wait for the command to complete */
1159         wait_event_interruptible_timeout(command_info->wait_command, 
1160                 (command_info->command_finished != FALSE), COMMAND_TIMEOUT);
1161
1162         spin_lock_irqsave(&command_info->lock, flags);
1163
1164         if (command_info->command_finished == FALSE) {
1165                 dbg("%s - command timed out.", __FUNCTION__);
1166                 retval = -ETIMEDOUT;
1167                 goto exit;
1168         }
1169
1170         if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1171                 dbg("%s - command failed.", __FUNCTION__);
1172                 retval = -EIO;
1173                 goto exit;
1174         }
1175
1176         if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1177                 dbg("%s - command completed.", __FUNCTION__);
1178                 switch (command) {
1179                         case WHITEHEAT_GET_DTR_RTS:
1180                                 info = usb_get_serial_port_data(port);
1181                                 memcpy(&info->mcr, command_info->result_buffer, sizeof(struct whiteheat_dr_info));
1182                                 break;
1183                 }
1184         }
1185
1186 exit:
1187         spin_unlock_irqrestore(&command_info->lock, flags);
1188         return retval;
1189 }
1190
1191
1192 static int firm_open(struct usb_serial_port *port) {
1193         struct whiteheat_simple open_command;
1194
1195         open_command.port = port->number - port->serial->minor + 1;
1196         return firm_send_command(port, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command));
1197 }
1198
1199
1200 static int firm_close(struct usb_serial_port *port) {
1201         struct whiteheat_simple close_command;
1202
1203         close_command.port = port->number - port->serial->minor + 1;
1204         return firm_send_command(port, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command));
1205 }
1206
1207
1208 static int firm_setup_port(struct usb_serial_port *port) {
1209         struct whiteheat_port_settings port_settings;
1210         unsigned int cflag = port->tty->termios->c_cflag;
1211
1212         port_settings.port = port->number + 1;
1213
1214         /* get the byte size */
1215         switch (cflag & CSIZE) {
1216                 case CS5:       port_settings.bits = 5;   break;
1217                 case CS6:       port_settings.bits = 6;   break;
1218                 case CS7:       port_settings.bits = 7;   break;
1219                 default:
1220                 case CS8:       port_settings.bits = 8;   break;
1221         }
1222         dbg("%s - data bits = %d", __FUNCTION__, port_settings.bits);
1223         
1224         /* determine the parity */
1225         if (cflag & PARENB)
1226                 if (cflag & CMSPAR)
1227                         if (cflag & PARODD)
1228                                 port_settings.parity = WHITEHEAT_PAR_MARK;
1229                         else
1230                                 port_settings.parity = WHITEHEAT_PAR_SPACE;
1231                 else
1232                         if (cflag & PARODD)
1233                                 port_settings.parity = WHITEHEAT_PAR_ODD;
1234                         else
1235                                 port_settings.parity = WHITEHEAT_PAR_EVEN;
1236         else
1237                 port_settings.parity = WHITEHEAT_PAR_NONE;
1238         dbg("%s - parity = %c", __FUNCTION__, port_settings.parity);
1239
1240         /* figure out the stop bits requested */
1241         if (cflag & CSTOPB)
1242                 port_settings.stop = 2;
1243         else
1244                 port_settings.stop = 1;
1245         dbg("%s - stop bits = %d", __FUNCTION__, port_settings.stop);
1246
1247         /* figure out the flow control settings */
1248         if (cflag & CRTSCTS)
1249                 port_settings.hflow = (WHITEHEAT_HFLOW_CTS | WHITEHEAT_HFLOW_RTS);
1250         else
1251                 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1252         dbg("%s - hardware flow control = %s %s %s %s", __FUNCTION__,
1253             (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1254             (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1255             (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1256             (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1257         
1258         /* determine software flow control */
1259         if (I_IXOFF(port->tty))
1260                 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1261         else
1262                 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1263         dbg("%s - software flow control = %c", __FUNCTION__, port_settings.sflow);
1264         
1265         port_settings.xon = START_CHAR(port->tty);
1266         port_settings.xoff = STOP_CHAR(port->tty);
1267         dbg("%s - XON = %2x, XOFF = %2x", __FUNCTION__, port_settings.xon, port_settings.xoff);
1268
1269         /* get the baud rate wanted */
1270         port_settings.baud = tty_get_baud_rate(port->tty);
1271         dbg("%s - baud rate = %d", __FUNCTION__, port_settings.baud);
1272
1273         /* handle any settings that aren't specified in the tty structure */
1274         port_settings.lloop = 0;
1275         
1276         /* now send the message to the device */
1277         return firm_send_command(port, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings));
1278 }
1279
1280
1281 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) {
1282         struct whiteheat_set_rdb rts_command;
1283
1284         rts_command.port = port->number - port->serial->minor + 1;
1285         rts_command.state = onoff;
1286         return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&rts_command, sizeof(rts_command));
1287 }
1288
1289
1290 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) {
1291         struct whiteheat_set_rdb dtr_command;
1292
1293         dtr_command.port = port->number - port->serial->minor + 1;
1294         dtr_command.state = onoff;
1295         return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&dtr_command, sizeof(dtr_command));
1296 }
1297
1298
1299 static int firm_set_break(struct usb_serial_port *port, __u8 onoff) {
1300         struct whiteheat_set_rdb break_command;
1301
1302         break_command.port = port->number - port->serial->minor + 1;
1303         break_command.state = onoff;
1304         return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&break_command, sizeof(break_command));
1305 }
1306
1307
1308 static int firm_purge(struct usb_serial_port *port, __u8 rxtx) {
1309         struct whiteheat_purge purge_command;
1310
1311         purge_command.port = port->number - port->serial->minor + 1;
1312         purge_command.what = rxtx;
1313         return firm_send_command(port, WHITEHEAT_PURGE, (__u8 *)&purge_command, sizeof(purge_command));
1314 }
1315
1316
1317 static int firm_get_dtr_rts(struct usb_serial_port *port) {
1318         struct whiteheat_simple get_dr_command;
1319
1320         get_dr_command.port = port->number - port->serial->minor + 1;
1321         return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, (__u8 *)&get_dr_command, sizeof(get_dr_command));
1322 }
1323
1324
1325 static int firm_report_tx_done(struct usb_serial_port *port) {
1326         struct whiteheat_simple close_command;
1327
1328         close_command.port = port->number - port->serial->minor + 1;
1329         return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, (__u8 *)&close_command, sizeof(close_command));
1330 }
1331
1332
1333 /*****************************************************************************
1334  * Connect Tech's White Heat utility functions
1335  *****************************************************************************/
1336 static int start_command_port(struct usb_serial *serial)
1337 {
1338         struct usb_serial_port *command_port;
1339         struct whiteheat_command_private *command_info;
1340         unsigned long flags;
1341         int retval = 0;
1342         
1343         command_port = serial->port[COMMAND_PORT];
1344         command_info = usb_get_serial_port_data(command_port);
1345         spin_lock_irqsave(&command_info->lock, flags);
1346         if (!command_info->port_running) {
1347                 /* Work around HCD bugs */
1348                 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1349
1350                 command_port->read_urb->dev = serial->dev;
1351                 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1352                 if (retval) {
1353                         err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
1354                         goto exit;
1355                 }
1356         }
1357         command_info->port_running++;
1358
1359 exit:
1360         spin_unlock_irqrestore(&command_info->lock, flags);
1361         return retval;
1362 }
1363
1364
1365 static void stop_command_port(struct usb_serial *serial)
1366 {
1367         struct usb_serial_port *command_port;
1368         struct whiteheat_command_private *command_info;
1369         unsigned long flags;
1370
1371         command_port = serial->port[COMMAND_PORT];
1372         command_info = usb_get_serial_port_data(command_port);
1373         spin_lock_irqsave(&command_info->lock, flags);
1374         command_info->port_running--;
1375         if (!command_info->port_running)
1376                 usb_unlink_urb(command_port->read_urb);
1377         spin_unlock_irqrestore(&command_info->lock, flags);
1378 }
1379
1380
1381 static int start_port_read(struct usb_serial_port *port)
1382 {
1383         struct whiteheat_private *info = usb_get_serial_port_data(port);
1384         struct whiteheat_urb_wrap *wrap;
1385         struct urb *urb;
1386         int retval = 0;
1387         unsigned long flags;
1388         struct list_head *tmp;
1389         struct list_head *tmp2;
1390
1391         spin_lock_irqsave(&info->lock, flags);
1392
1393         list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1394                 list_del(tmp);
1395                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1396                 urb = wrap->urb;
1397                 urb->dev = port->serial->dev;
1398                 retval = usb_submit_urb(urb, GFP_KERNEL);
1399                 if (retval) {
1400                         list_add(tmp, &info->rx_urbs_free);
1401                         list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1402                                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1403                                 urb = wrap->urb;
1404                                 usb_unlink_urb(urb);
1405                                 list_del(tmp);
1406                                 list_add(tmp, &info->rx_urbs_free);
1407                         }
1408                         break;
1409                 }
1410                 list_add(tmp, &info->rx_urbs_submitted);
1411         }
1412
1413         spin_unlock_irqrestore(&info->lock, flags);
1414
1415         return retval;
1416 }
1417
1418
1419 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb* urb, struct list_head *head)
1420 {
1421         struct whiteheat_urb_wrap *wrap;
1422         struct list_head *tmp;
1423
1424         list_for_each(tmp, head) {
1425                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1426                 if (wrap->urb == urb)
1427                         return wrap;
1428         }
1429
1430         return NULL;
1431 }
1432
1433
1434 static struct list_head *list_first(struct list_head *head)
1435 {
1436         return head->next;
1437 }
1438
1439
1440 static void rx_data_softint(void *private)
1441 {
1442         struct usb_serial_port *port = (struct usb_serial_port *)private;
1443         struct whiteheat_private *info = usb_get_serial_port_data(port);
1444         struct tty_struct *tty = port->tty;
1445         struct whiteheat_urb_wrap *wrap;
1446         struct urb *urb;
1447         unsigned long flags;
1448         struct list_head *tmp;
1449         struct list_head *tmp2;
1450         int result;
1451         int sent = 0;
1452
1453         spin_lock_irqsave(&info->lock, flags);
1454         if (info->flags & THROTTLED) {
1455                 spin_unlock_irqrestore(&info->lock, flags);
1456                 return;
1457         }
1458
1459         list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1460                 list_del(tmp);
1461                 spin_unlock_irqrestore(&info->lock, flags);
1462
1463                 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1464                 urb = wrap->urb;
1465
1466                 if (tty && urb->actual_length) {
1467                         if (urb->actual_length > TTY_FLIPBUF_SIZE - tty->flip.count) {
1468                                 spin_lock_irqsave(&info->lock, flags);
1469                                 list_add(tmp, &info->rx_urb_q);
1470                                 spin_unlock_irqrestore(&info->lock, flags);
1471                                 tty_flip_buffer_push(tty);
1472                                 schedule_work(&info->rx_work);
1473                                 return;
1474                         }
1475
1476                         memcpy(tty->flip.char_buf_ptr, urb->transfer_buffer, urb->actual_length);
1477                         tty->flip.char_buf_ptr += urb->actual_length;
1478                         tty->flip.count += urb->actual_length;
1479                         sent += urb->actual_length;
1480                 }
1481
1482                 urb->dev = port->serial->dev;
1483                 result = usb_submit_urb(urb, GFP_ATOMIC);
1484                 if (result) {
1485                         err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
1486                         spin_lock_irqsave(&info->lock, flags);
1487                         list_add(tmp, &info->rx_urbs_free);
1488                         continue;
1489                 }
1490
1491                 spin_lock_irqsave(&info->lock, flags);
1492                 list_add(tmp, &info->rx_urbs_submitted);
1493         }
1494         spin_unlock_irqrestore(&info->lock, flags);
1495
1496         if (sent)
1497                 tty_flip_buffer_push(tty);
1498 }
1499
1500
1501 /*****************************************************************************
1502  * Connect Tech's White Heat module functions
1503  *****************************************************************************/
1504 static int __init whiteheat_init (void)
1505 {
1506         int retval;
1507         retval = usb_serial_register(&whiteheat_fake_device);
1508         if (retval)
1509                 goto failed_fake_register;
1510         retval = usb_serial_register(&whiteheat_device);
1511         if (retval)
1512                 goto failed_device_register;
1513         retval = usb_register(&whiteheat_driver);
1514         if (retval)
1515                 goto failed_usb_register;
1516         info(DRIVER_DESC " " DRIVER_VERSION);
1517         return 0;
1518 failed_usb_register:
1519         usb_serial_deregister(&whiteheat_device);
1520 failed_device_register:
1521         usb_serial_deregister(&whiteheat_fake_device);
1522 failed_fake_register:
1523         return retval;
1524 }
1525
1526
1527 static void __exit whiteheat_exit (void)
1528 {
1529         usb_deregister (&whiteheat_driver);
1530         usb_serial_deregister (&whiteheat_fake_device);
1531         usb_serial_deregister (&whiteheat_device);
1532 }
1533
1534
1535 module_init(whiteheat_init);
1536 module_exit(whiteheat_exit);
1537
1538 MODULE_AUTHOR( DRIVER_AUTHOR );
1539 MODULE_DESCRIPTION( DRIVER_DESC );
1540 MODULE_LICENSE("GPL");
1541
1542 MODULE_PARM(urb_pool_size, "i");
1543 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1544
1545 MODULE_PARM(debug, "i");
1546 MODULE_PARM_DESC(debug, "Debug enabled or not");