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