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