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