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