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