blob: bf7014d49a50ba2d6162b11ba1430ad4bca3acff [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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
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>
28#include <linux/mutex.h>
29#include <linux/uaccess.h>
30#include <asm/termbits.h>
31#include <linux/usb.h>
32#include <linux/serial_reg.h>
33#include <linux/serial.h>
34#include <linux/usb/serial.h>
35#include <linux/firmware.h>
36#include <linux/ihex.h>
37#include "whiteheat.h" /* WhiteHEAT specific commands */
38
39static bool 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*/
63static const struct usb_device_id id_table_std[] = {
64 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
65 { } /* Terminating entry */
66};
67
68static const struct usb_device_id id_table_prerenumeration[] = {
69 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
70 { } /* Terminating entry */
71};
72
73static const struct usb_device_id id_table_combined[] = {
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
79MODULE_DEVICE_TABLE(usb, id_table_combined);
80
81static struct usb_driver whiteheat_driver = {
82 .name = "whiteheat",
83 .probe = usb_serial_probe,
84 .disconnect = usb_serial_disconnect,
85 .id_table = id_table_combined,
86};
87
88/* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
89static int whiteheat_firmware_download(struct usb_serial *serial,
90 const struct usb_device_id *id);
91static int whiteheat_firmware_attach(struct usb_serial *serial);
92
93/* function prototypes for the Connect Tech WhiteHEAT serial converter */
94static int whiteheat_attach(struct usb_serial *serial);
95static void whiteheat_release(struct usb_serial *serial);
96static int whiteheat_open(struct tty_struct *tty,
97 struct usb_serial_port *port);
98static void whiteheat_close(struct usb_serial_port *port);
99static int whiteheat_write(struct tty_struct *tty,
100 struct usb_serial_port *port,
101 const unsigned char *buf, int count);
102static int whiteheat_write_room(struct tty_struct *tty);
103static int whiteheat_ioctl(struct tty_struct *tty,
104 unsigned int cmd, unsigned long arg);
105static void whiteheat_set_termios(struct tty_struct *tty,
106 struct usb_serial_port *port, struct ktermios *old);
107static int whiteheat_tiocmget(struct tty_struct *tty);
108static int whiteheat_tiocmset(struct tty_struct *tty,
109 unsigned int set, unsigned int clear);
110static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
111static int whiteheat_chars_in_buffer(struct tty_struct *tty);
112static void whiteheat_throttle(struct tty_struct *tty);
113static void whiteheat_unthrottle(struct tty_struct *tty);
114static void whiteheat_read_callback(struct urb *urb);
115static void whiteheat_write_callback(struct urb *urb);
116
117static struct usb_serial_driver whiteheat_fake_device = {
118 .driver = {
119 .owner = THIS_MODULE,
120 .name = "whiteheatnofirm",
121 },
122 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
123 .id_table = id_table_prerenumeration,
124 .num_ports = 1,
125 .probe = whiteheat_firmware_download,
126 .attach = whiteheat_firmware_attach,
127};
128
129static struct usb_serial_driver whiteheat_device = {
130 .driver = {
131 .owner = THIS_MODULE,
132 .name = "whiteheat",
133 },
134 .description = "Connect Tech - WhiteHEAT",
135 .id_table = id_table_std,
136 .num_ports = 4,
137 .attach = whiteheat_attach,
138 .release = whiteheat_release,
139 .open = whiteheat_open,
140 .close = whiteheat_close,
141 .write = whiteheat_write,
142 .write_room = whiteheat_write_room,
143 .ioctl = whiteheat_ioctl,
144 .set_termios = whiteheat_set_termios,
145 .break_ctl = whiteheat_break_ctl,
146 .tiocmget = whiteheat_tiocmget,
147 .tiocmset = whiteheat_tiocmset,
148 .chars_in_buffer = whiteheat_chars_in_buffer,
149 .throttle = whiteheat_throttle,
150 .unthrottle = whiteheat_unthrottle,
151 .read_bulk_callback = whiteheat_read_callback,
152 .write_bulk_callback = whiteheat_write_callback,
153};
154
155static struct usb_serial_driver * const serial_drivers[] = {
156 &whiteheat_fake_device, &whiteheat_device, NULL
157};
158
159struct whiteheat_command_private {
160 struct mutex mutex;
161 __u8 port_running;
162 __u8 command_finished;
163 wait_queue_head_t wait_command; /* for handling sleeping whilst
164 waiting for a command to
165 finish */
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;
183 __u8 mcr; /* FIXME: no locking on mcr */
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;
188 struct usb_serial_port *port;
189 struct list_head tx_urbs_free;
190 struct list_head tx_urbs_submitted;
191 struct mutex deathwarrant;
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);
198static void command_port_write_callback(struct urb *urb);
199static void command_port_read_callback(struct urb *urb);
200
201static int start_port_read(struct usb_serial_port *port);
202static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
203 struct list_head *head);
204static struct list_head *list_first(struct list_head *head);
205static void rx_data_softint(struct work_struct *work);
206
207static int firm_send_command(struct usb_serial_port *port, __u8 command,
208 __u8 *data, __u8 datasize);
209static int firm_open(struct usb_serial_port *port);
210static int firm_close(struct usb_serial_port *port);
211static void firm_setup_port(struct tty_struct *tty);
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*/
243static int whiteheat_firmware_download(struct usb_serial *serial,
244 const struct usb_device_id *id)
245{
246 int response, ret = -ENOENT;
247 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
248 const struct ihex_binrec *record;
249
250 dbg("%s", __func__);
251
252 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
253 &serial->dev->dev)) {
254 dev_err(&serial->dev->dev,
255 "%s - request \"whiteheat.fw\" failed\n", __func__);
256 goto out;
257 }
258 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
259 &serial->dev->dev)) {
260 dev_err(&serial->dev->dev,
261 "%s - request \"whiteheat_loader.fw\" failed\n",
262 __func__);
263 goto out;
264 }
265 ret = 0;
266 response = ezusb_set_reset (serial, 1);
267
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);
273 if (response < 0) {
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));
278 break;
279 }
280 record = ihex_next_binrec(record);
281 }
282
283 response = ezusb_set_reset(serial, 0);
284
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);
292 if (response < 0) {
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));
298 break;
299 }
300 ++record;
301 }
302
303 response = ezusb_set_reset(serial, 1);
304
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);
310 if (response < 0) {
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));
316 break;
317 }
318 ++record;
319 }
320 ret = 0;
321 response = ezusb_set_reset (serial, 0);
322 out:
323 release_firmware(loader_fw);
324 release_firmware(firmware_fw);
325 return ret;
326}
327
328
329static int whiteheat_firmware_attach(struct usb_serial *serial)
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 *****************************************************************************/
339static int whiteheat_attach(struct usb_serial *serial)
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
360 pipe = usb_sndbulkpipe(serial->dev,
361 command_port->bulk_out_endpointAddress);
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;
367
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
374 * unlinking bug in disguise. Same for the call below.
375 */
376 usb_clear_halt(serial->dev, pipe);
377 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
378 &alen, COMMAND_TIMEOUT_MS);
379 if (ret) {
380 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
381 serial->type->description, ret);
382 goto no_firmware;
383 } else if (alen != 2) {
384 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
385 serial->type->description, alen);
386 goto no_firmware;
387 }
388
389 pipe = usb_rcvbulkpipe(serial->dev,
390 command_port->bulk_in_endpointAddress);
391 /* See the comment on the usb_clear_halt() above */
392 usb_clear_halt(serial->dev, pipe);
393 ret = usb_bulk_msg(serial->dev, pipe, result,
394 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
395 if (ret) {
396 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
397 serial->type->description, ret);
398 goto no_firmware;
399 } else if (alen != sizeof(*hw_info) + 1) {
400 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
401 serial->type->description, alen);
402 goto no_firmware;
403 } else if (result[0] != command[0]) {
404 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
405 serial->type->description, result[0]);
406 goto no_firmware;
407 }
408
409 hw_info = (struct whiteheat_hw_info *)&result[1];
410
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);
414
415 for (i = 0; i < serial->num_ports; i++) {
416 port = serial->port[i];
417
418 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
419 if (info == NULL) {
420 dev_err(&port->dev,
421 "%s: Out of memory for port structures\n",
422 serial->type->description);
423 goto no_private;
424 }
425
426 spin_lock_init(&info->lock);
427 mutex_init(&info->deathwarrant);
428 info->flags = 0;
429 info->mcr = 0;
430 INIT_WORK(&info->rx_work, rx_data_softint);
431 info->port = port;
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) {
442 dev_err(&port->dev, "No free urbs available\n");
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) {
448 dev_err(&port->dev,
449 "Couldn't allocate urb buffer\n");
450 goto no_rx_buf;
451 }
452 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
453 if (!wrap) {
454 dev_err(&port->dev,
455 "Couldn't allocate urb wrapper\n");
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) {
468 dev_err(&port->dev, "No free urbs available\n");
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) {
474 dev_err(&port->dev,
475 "Couldn't allocate urb buffer\n");
476 goto no_tx_buf;
477 }
478 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
479 if (!wrap) {
480 dev_err(&port->dev,
481 "Couldn't allocate urb wrapper\n");
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
496 command_info = kmalloc(sizeof(struct whiteheat_command_private),
497 GFP_KERNEL);
498 if (command_info == NULL) {
499 dev_err(&serial->dev->dev,
500 "%s: Out of memory for port structures\n",
501 serial->type->description);
502 goto no_command_private;
503 }
504
505 mutex_init(&command_info->mutex);
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 */
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);
527 kfree(result);
528 kfree(command);
529 return -ENODEV;
530
531no_command_private:
532 for (i = serial->num_ports - 1; i >= 0; i--) {
533 port = serial->port[i];
534 info = usb_get_serial_port_data(port);
535 for (j = urb_pool_size - 1; j >= 0; j--) {
536 tmp = list_first(&info->tx_urbs_free);
537 list_del(tmp);
538 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
539 urb = wrap->urb;
540 kfree(wrap);
541no_tx_wrap:
542 kfree(urb->transfer_buffer);
543no_tx_buf:
544 usb_free_urb(urb);
545no_tx_urb:
546 tmp = list_first(&info->rx_urbs_free);
547 list_del(tmp);
548 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
549 urb = wrap->urb;
550 kfree(wrap);
551no_rx_wrap:
552 kfree(urb->transfer_buffer);
553no_rx_buf:
554 usb_free_urb(urb);
555no_rx_urb:
556 ;
557 }
558 kfree(info);
559no_private:
560 ;
561 }
562 kfree(result);
563no_result_buffer:
564 kfree(command);
565no_command_buffer:
566 return -ENOMEM;
567}
568
569
570static void whiteheat_release(struct usb_serial *serial)
571{
572 struct usb_serial_port *command_port;
573 struct usb_serial_port *port;
574 struct whiteheat_private *info;
575 struct whiteheat_urb_wrap *wrap;
576 struct urb *urb;
577 struct list_head *tmp;
578 struct list_head *tmp2;
579 int i;
580
581 dbg("%s", __func__);
582
583 /* free up our private data for our command port */
584 command_port = serial->port[COMMAND_PORT];
585 kfree(usb_get_serial_port_data(command_port));
586
587 for (i = 0; i < serial->num_ports; i++) {
588 port = serial->port[i];
589 info = usb_get_serial_port_data(port);
590 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
591 list_del(tmp);
592 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
593 urb = wrap->urb;
594 kfree(wrap);
595 kfree(urb->transfer_buffer);
596 usb_free_urb(urb);
597 }
598 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
599 list_del(tmp);
600 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
601 urb = wrap->urb;
602 kfree(wrap);
603 kfree(urb->transfer_buffer);
604 usb_free_urb(urb);
605 }
606 kfree(info);
607 }
608}
609
610static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
611{
612 int retval = 0;
613
614 dbg("%s - port %d", __func__, port->number);
615
616 retval = start_command_port(port->serial);
617 if (retval)
618 goto exit;
619
620 if (tty)
621 tty->low_latency = 1;
622
623 /* send an open port command */
624 retval = firm_open(port);
625 if (retval) {
626 stop_command_port(port->serial);
627 goto exit;
628 }
629
630 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
631 if (retval) {
632 firm_close(port);
633 stop_command_port(port->serial);
634 goto exit;
635 }
636
637 if (tty)
638 firm_setup_port(tty);
639
640 /* Work around HCD bugs */
641 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
642 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
643
644 /* Start reading from the device */
645 retval = start_port_read(port);
646 if (retval) {
647 dev_err(&port->dev,
648 "%s - failed submitting read urb, error %d\n",
649 __func__, retval);
650 firm_close(port);
651 stop_command_port(port->serial);
652 goto exit;
653 }
654
655exit:
656 dbg("%s - exit, retval = %d", __func__, retval);
657 return retval;
658}
659
660
661static void whiteheat_close(struct usb_serial_port *port)
662{
663 struct whiteheat_private *info = usb_get_serial_port_data(port);
664 struct whiteheat_urb_wrap *wrap;
665 struct urb *urb;
666 struct list_head *tmp;
667 struct list_head *tmp2;
668
669 dbg("%s - port %d", __func__, port->number);
670
671 firm_report_tx_done(port);
672 firm_close(port);
673
674 /* shutdown our bulk reads and writes */
675 mutex_lock(&info->deathwarrant);
676 spin_lock_irq(&info->lock);
677 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
678 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
679 urb = wrap->urb;
680 list_del(tmp);
681 spin_unlock_irq(&info->lock);
682 usb_kill_urb(urb);
683 spin_lock_irq(&info->lock);
684 list_add(tmp, &info->rx_urbs_free);
685 }
686 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
687 list_move(tmp, &info->rx_urbs_free);
688 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
689 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
690 urb = wrap->urb;
691 list_del(tmp);
692 spin_unlock_irq(&info->lock);
693 usb_kill_urb(urb);
694 spin_lock_irq(&info->lock);
695 list_add(tmp, &info->tx_urbs_free);
696 }
697 spin_unlock_irq(&info->lock);
698 mutex_unlock(&info->deathwarrant);
699 stop_command_port(port->serial);
700}
701
702
703static int whiteheat_write(struct tty_struct *tty,
704 struct usb_serial_port *port, const unsigned char *buf, int count)
705{
706 struct whiteheat_private *info = usb_get_serial_port_data(port);
707 struct whiteheat_urb_wrap *wrap;
708 struct urb *urb;
709 int result;
710 int bytes;
711 int sent = 0;
712 unsigned long flags;
713 struct list_head *tmp;
714
715 dbg("%s - port %d", __func__, port->number);
716
717 if (count == 0) {
718 dbg("%s - write request of 0 bytes", __func__);
719 return (0);
720 }
721
722 while (count) {
723 spin_lock_irqsave(&info->lock, flags);
724 if (list_empty(&info->tx_urbs_free)) {
725 spin_unlock_irqrestore(&info->lock, flags);
726 break;
727 }
728 tmp = list_first(&info->tx_urbs_free);
729 list_del(tmp);
730 spin_unlock_irqrestore(&info->lock, flags);
731
732 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
733 urb = wrap->urb;
734 bytes = (count > port->bulk_out_size) ?
735 port->bulk_out_size : count;
736 memcpy(urb->transfer_buffer, buf + sent, bytes);
737
738 usb_serial_debug_data(debug, &port->dev,
739 __func__, bytes, urb->transfer_buffer);
740
741 urb->transfer_buffer_length = bytes;
742 result = usb_submit_urb(urb, GFP_ATOMIC);
743 if (result) {
744 dev_err_console(port,
745 "%s - failed submitting write urb, error %d\n",
746 __func__, result);
747 sent = result;
748 spin_lock_irqsave(&info->lock, flags);
749 list_add(tmp, &info->tx_urbs_free);
750 spin_unlock_irqrestore(&info->lock, flags);
751 break;
752 } else {
753 sent += bytes;
754 count -= bytes;
755 spin_lock_irqsave(&info->lock, flags);
756 list_add(tmp, &info->tx_urbs_submitted);
757 spin_unlock_irqrestore(&info->lock, flags);
758 }
759 }
760
761 return sent;
762}
763
764static int whiteheat_write_room(struct tty_struct *tty)
765{
766 struct usb_serial_port *port = tty->driver_data;
767 struct whiteheat_private *info = usb_get_serial_port_data(port);
768 struct list_head *tmp;
769 int room = 0;
770 unsigned long flags;
771
772 dbg("%s - port %d", __func__, port->number);
773
774 spin_lock_irqsave(&info->lock, flags);
775 list_for_each(tmp, &info->tx_urbs_free)
776 room++;
777 spin_unlock_irqrestore(&info->lock, flags);
778 room *= port->bulk_out_size;
779
780 dbg("%s - returns %d", __func__, room);
781 return (room);
782}
783
784static int whiteheat_tiocmget(struct tty_struct *tty)
785{
786 struct usb_serial_port *port = tty->driver_data;
787 struct whiteheat_private *info = usb_get_serial_port_data(port);
788 unsigned int modem_signals = 0;
789
790 dbg("%s - port %d", __func__, port->number);
791
792 firm_get_dtr_rts(port);
793 if (info->mcr & UART_MCR_DTR)
794 modem_signals |= TIOCM_DTR;
795 if (info->mcr & UART_MCR_RTS)
796 modem_signals |= TIOCM_RTS;
797
798 return modem_signals;
799}
800
801static int whiteheat_tiocmset(struct tty_struct *tty,
802 unsigned int set, unsigned int clear)
803{
804 struct usb_serial_port *port = tty->driver_data;
805 struct whiteheat_private *info = usb_get_serial_port_data(port);
806
807 dbg("%s - port %d", __func__, port->number);
808
809 if (set & TIOCM_RTS)
810 info->mcr |= UART_MCR_RTS;
811 if (set & TIOCM_DTR)
812 info->mcr |= UART_MCR_DTR;
813
814 if (clear & TIOCM_RTS)
815 info->mcr &= ~UART_MCR_RTS;
816 if (clear & TIOCM_DTR)
817 info->mcr &= ~UART_MCR_DTR;
818
819 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
820 firm_set_rts(port, info->mcr & UART_MCR_RTS);
821 return 0;
822}
823
824
825static int whiteheat_ioctl(struct tty_struct *tty,
826 unsigned int cmd, unsigned long arg)
827{
828 struct usb_serial_port *port = tty->driver_data;
829 struct serial_struct serstruct;
830 void __user *user_arg = (void __user *)arg;
831
832 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
833
834 switch (cmd) {
835 case TIOCGSERIAL:
836 memset(&serstruct, 0, sizeof(serstruct));
837 serstruct.type = PORT_16654;
838 serstruct.line = port->serial->minor;
839 serstruct.port = port->number;
840 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
841 serstruct.xmit_fifo_size = port->bulk_out_size;
842 serstruct.custom_divisor = 0;
843 serstruct.baud_base = 460800;
844 serstruct.close_delay = CLOSING_DELAY;
845 serstruct.closing_wait = CLOSING_DELAY;
846
847 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
848 return -EFAULT;
849 break;
850 default:
851 break;
852 }
853
854 return -ENOIOCTLCMD;
855}
856
857
858static void whiteheat_set_termios(struct tty_struct *tty,
859 struct usb_serial_port *port, struct ktermios *old_termios)
860{
861 firm_setup_port(tty);
862}
863
864static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
865{
866 struct usb_serial_port *port = tty->driver_data;
867 firm_set_break(port, break_state);
868}
869
870
871static int whiteheat_chars_in_buffer(struct tty_struct *tty)
872{
873 struct usb_serial_port *port = tty->driver_data;
874 struct whiteheat_private *info = usb_get_serial_port_data(port);
875 struct list_head *tmp;
876 struct whiteheat_urb_wrap *wrap;
877 int chars = 0;
878 unsigned long flags;
879
880 dbg("%s - port %d", __func__, port->number);
881
882 spin_lock_irqsave(&info->lock, flags);
883 list_for_each(tmp, &info->tx_urbs_submitted) {
884 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
885 chars += wrap->urb->transfer_buffer_length;
886 }
887 spin_unlock_irqrestore(&info->lock, flags);
888
889 dbg("%s - returns %d", __func__, chars);
890 return chars;
891}
892
893
894static void whiteheat_throttle(struct tty_struct *tty)
895{
896 struct usb_serial_port *port = tty->driver_data;
897 struct whiteheat_private *info = usb_get_serial_port_data(port);
898
899 dbg("%s - port %d", __func__, port->number);
900
901 spin_lock_irq(&info->lock);
902 info->flags |= THROTTLED;
903 spin_unlock_irq(&info->lock);
904}
905
906
907static void whiteheat_unthrottle(struct tty_struct *tty)
908{
909 struct usb_serial_port *port = tty->driver_data;
910 struct whiteheat_private *info = usb_get_serial_port_data(port);
911 int actually_throttled;
912
913 dbg("%s - port %d", __func__, port->number);
914
915 spin_lock_irq(&info->lock);
916 actually_throttled = info->flags & ACTUALLY_THROTTLED;
917 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
918 spin_unlock_irq(&info->lock);
919
920 if (actually_throttled)
921 rx_data_softint(&info->rx_work);
922}
923
924
925/*****************************************************************************
926 * Connect Tech's White Heat callback routines
927 *****************************************************************************/
928static void command_port_write_callback(struct urb *urb)
929{
930 int status = urb->status;
931
932 dbg("%s", __func__);
933
934 if (status) {
935 dbg("nonzero urb status: %d", status);
936 return;
937 }
938}
939
940
941static void command_port_read_callback(struct urb *urb)
942{
943 struct usb_serial_port *command_port = urb->context;
944 struct whiteheat_command_private *command_info;
945 int status = urb->status;
946 unsigned char *data = urb->transfer_buffer;
947 int result;
948
949 dbg("%s", __func__);
950
951 command_info = usb_get_serial_port_data(command_port);
952 if (!command_info) {
953 dbg("%s - command_info is NULL, exiting.", __func__);
954 return;
955 }
956 if (!urb->actual_length) {
957 dev_dbg(&urb->dev->dev, "%s - empty response, exiting.\n", __func__);
958 return;
959 }
960 if (status) {
961 dbg("%s - nonzero urb status: %d", __func__, status);
962 if (status != -ENOENT)
963 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
964 wake_up(&command_info->wait_command);
965 return;
966 }
967
968 usb_serial_debug_data(debug, &command_port->dev,
969 __func__, urb->actual_length, data);
970
971 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
972 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
973 wake_up(&command_info->wait_command);
974 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
975 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
976 wake_up(&command_info->wait_command);
977 } else if (data[0] == WHITEHEAT_EVENT) {
978 /* These are unsolicited reports from the firmware, hence no
979 waiting command to wakeup */
980 dbg("%s - event received", __func__);
981 } else if ((data[0] == WHITEHEAT_GET_DTR_RTS) &&
982 (urb->actual_length - 1 <= sizeof(command_info->result_buffer))) {
983 memcpy(command_info->result_buffer, &data[1],
984 urb->actual_length - 1);
985 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
986 wake_up(&command_info->wait_command);
987 } else
988 dbg("%s - bad reply from firmware", __func__);
989
990 /* Continue trying to always read */
991 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
992 if (result)
993 dbg("%s - failed resubmitting read urb, error %d",
994 __func__, result);
995}
996
997
998static void whiteheat_read_callback(struct urb *urb)
999{
1000 struct usb_serial_port *port = urb->context;
1001 struct whiteheat_urb_wrap *wrap;
1002 unsigned char *data = urb->transfer_buffer;
1003 struct whiteheat_private *info = usb_get_serial_port_data(port);
1004 int status = urb->status;
1005
1006 dbg("%s - port %d", __func__, port->number);
1007
1008 spin_lock(&info->lock);
1009 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1010 if (!wrap) {
1011 spin_unlock(&info->lock);
1012 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1013 return;
1014 }
1015 list_del(&wrap->list);
1016 spin_unlock(&info->lock);
1017
1018 if (status) {
1019 dbg("%s - nonzero read bulk status received: %d",
1020 __func__, status);
1021 spin_lock(&info->lock);
1022 list_add(&wrap->list, &info->rx_urbs_free);
1023 spin_unlock(&info->lock);
1024 return;
1025 }
1026
1027 usb_serial_debug_data(debug, &port->dev,
1028 __func__, urb->actual_length, data);
1029
1030 spin_lock(&info->lock);
1031 list_add_tail(&wrap->list, &info->rx_urb_q);
1032 if (info->flags & THROTTLED) {
1033 info->flags |= ACTUALLY_THROTTLED;
1034 spin_unlock(&info->lock);
1035 return;
1036 }
1037 spin_unlock(&info->lock);
1038
1039 schedule_work(&info->rx_work);
1040}
1041
1042
1043static void whiteheat_write_callback(struct urb *urb)
1044{
1045 struct usb_serial_port *port = urb->context;
1046 struct whiteheat_private *info = usb_get_serial_port_data(port);
1047 struct whiteheat_urb_wrap *wrap;
1048 int status = urb->status;
1049
1050 dbg("%s - port %d", __func__, port->number);
1051
1052 spin_lock(&info->lock);
1053 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1054 if (!wrap) {
1055 spin_unlock(&info->lock);
1056 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1057 return;
1058 }
1059 list_move(&wrap->list, &info->tx_urbs_free);
1060 spin_unlock(&info->lock);
1061
1062 if (status) {
1063 dbg("%s - nonzero write bulk status received: %d",
1064 __func__, status);
1065 return;
1066 }
1067
1068 usb_serial_port_softint(port);
1069}
1070
1071
1072/*****************************************************************************
1073 * Connect Tech's White Heat firmware interface
1074 *****************************************************************************/
1075static int firm_send_command(struct usb_serial_port *port, __u8 command,
1076 __u8 *data, __u8 datasize)
1077{
1078 struct usb_serial_port *command_port;
1079 struct whiteheat_command_private *command_info;
1080 struct whiteheat_private *info;
1081 __u8 *transfer_buffer;
1082 int retval = 0;
1083 int t;
1084
1085 dbg("%s - command %d", __func__, command);
1086
1087 command_port = port->serial->port[COMMAND_PORT];
1088 command_info = usb_get_serial_port_data(command_port);
1089 mutex_lock(&command_info->mutex);
1090 command_info->command_finished = false;
1091
1092 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1093 transfer_buffer[0] = command;
1094 memcpy(&transfer_buffer[1], data, datasize);
1095 command_port->write_urb->transfer_buffer_length = datasize + 1;
1096 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1097 if (retval) {
1098 dbg("%s - submit urb failed", __func__);
1099 goto exit;
1100 }
1101
1102 /* wait for the command to complete */
1103 t = wait_event_timeout(command_info->wait_command,
1104 (bool)command_info->command_finished, COMMAND_TIMEOUT);
1105 if (!t)
1106 usb_kill_urb(command_port->write_urb);
1107
1108 if (command_info->command_finished == false) {
1109 dbg("%s - command timed out.", __func__);
1110 retval = -ETIMEDOUT;
1111 goto exit;
1112 }
1113
1114 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1115 dbg("%s - command failed.", __func__);
1116 retval = -EIO;
1117 goto exit;
1118 }
1119
1120 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1121 dbg("%s - command completed.", __func__);
1122 switch (command) {
1123 case WHITEHEAT_GET_DTR_RTS:
1124 info = usb_get_serial_port_data(port);
1125 memcpy(&info->mcr, command_info->result_buffer,
1126 sizeof(struct whiteheat_dr_info));
1127 break;
1128 }
1129 }
1130exit:
1131 mutex_unlock(&command_info->mutex);
1132 return retval;
1133}
1134
1135
1136static int firm_open(struct usb_serial_port *port)
1137{
1138 struct whiteheat_simple open_command;
1139
1140 open_command.port = port->number - port->serial->minor + 1;
1141 return firm_send_command(port, WHITEHEAT_OPEN,
1142 (__u8 *)&open_command, sizeof(open_command));
1143}
1144
1145
1146static int firm_close(struct usb_serial_port *port)
1147{
1148 struct whiteheat_simple close_command;
1149
1150 close_command.port = port->number - port->serial->minor + 1;
1151 return firm_send_command(port, WHITEHEAT_CLOSE,
1152 (__u8 *)&close_command, sizeof(close_command));
1153}
1154
1155
1156static void firm_setup_port(struct tty_struct *tty)
1157{
1158 struct usb_serial_port *port = tty->driver_data;
1159 struct whiteheat_port_settings port_settings;
1160 unsigned int cflag = tty->termios->c_cflag;
1161
1162 port_settings.port = port->number - port->serial->minor + 1;
1163
1164 /* get the byte size */
1165 switch (cflag & CSIZE) {
1166 case CS5: port_settings.bits = 5; break;
1167 case CS6: port_settings.bits = 6; break;
1168 case CS7: port_settings.bits = 7; break;
1169 default:
1170 case CS8: port_settings.bits = 8; break;
1171 }
1172 dbg("%s - data bits = %d", __func__, port_settings.bits);
1173
1174 /* determine the parity */
1175 if (cflag & PARENB)
1176 if (cflag & CMSPAR)
1177 if (cflag & PARODD)
1178 port_settings.parity = WHITEHEAT_PAR_MARK;
1179 else
1180 port_settings.parity = WHITEHEAT_PAR_SPACE;
1181 else
1182 if (cflag & PARODD)
1183 port_settings.parity = WHITEHEAT_PAR_ODD;
1184 else
1185 port_settings.parity = WHITEHEAT_PAR_EVEN;
1186 else
1187 port_settings.parity = WHITEHEAT_PAR_NONE;
1188 dbg("%s - parity = %c", __func__, port_settings.parity);
1189
1190 /* figure out the stop bits requested */
1191 if (cflag & CSTOPB)
1192 port_settings.stop = 2;
1193 else
1194 port_settings.stop = 1;
1195 dbg("%s - stop bits = %d", __func__, port_settings.stop);
1196
1197 /* figure out the flow control settings */
1198 if (cflag & CRTSCTS)
1199 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1200 WHITEHEAT_HFLOW_RTS);
1201 else
1202 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1203 dbg("%s - hardware flow control = %s %s %s %s", __func__,
1204 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1205 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1206 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1207 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1208
1209 /* determine software flow control */
1210 if (I_IXOFF(tty))
1211 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1212 else
1213 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1214 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
1215
1216 port_settings.xon = START_CHAR(tty);
1217 port_settings.xoff = STOP_CHAR(tty);
1218 dbg("%s - XON = %2x, XOFF = %2x",
1219 __func__, port_settings.xon, port_settings.xoff);
1220
1221 /* get the baud rate wanted */
1222 port_settings.baud = tty_get_baud_rate(tty);
1223 dbg("%s - baud rate = %d", __func__, port_settings.baud);
1224
1225 /* fixme: should set validated settings */
1226 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1227 /* handle any settings that aren't specified in the tty structure */
1228 port_settings.lloop = 0;
1229
1230 /* now send the message to the device */
1231 firm_send_command(port, WHITEHEAT_SETUP_PORT,
1232 (__u8 *)&port_settings, sizeof(port_settings));
1233}
1234
1235
1236static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1237{
1238 struct whiteheat_set_rdb rts_command;
1239
1240 rts_command.port = port->number - port->serial->minor + 1;
1241 rts_command.state = onoff;
1242 return firm_send_command(port, WHITEHEAT_SET_RTS,
1243 (__u8 *)&rts_command, sizeof(rts_command));
1244}
1245
1246
1247static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1248{
1249 struct whiteheat_set_rdb dtr_command;
1250
1251 dtr_command.port = port->number - port->serial->minor + 1;
1252 dtr_command.state = onoff;
1253 return firm_send_command(port, WHITEHEAT_SET_DTR,
1254 (__u8 *)&dtr_command, sizeof(dtr_command));
1255}
1256
1257
1258static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1259{
1260 struct whiteheat_set_rdb break_command;
1261
1262 break_command.port = port->number - port->serial->minor + 1;
1263 break_command.state = onoff;
1264 return firm_send_command(port, WHITEHEAT_SET_BREAK,
1265 (__u8 *)&break_command, sizeof(break_command));
1266}
1267
1268
1269static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1270{
1271 struct whiteheat_purge purge_command;
1272
1273 purge_command.port = port->number - port->serial->minor + 1;
1274 purge_command.what = rxtx;
1275 return firm_send_command(port, WHITEHEAT_PURGE,
1276 (__u8 *)&purge_command, sizeof(purge_command));
1277}
1278
1279
1280static int firm_get_dtr_rts(struct usb_serial_port *port)
1281{
1282 struct whiteheat_simple get_dr_command;
1283
1284 get_dr_command.port = port->number - port->serial->minor + 1;
1285 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1286 (__u8 *)&get_dr_command, sizeof(get_dr_command));
1287}
1288
1289
1290static int firm_report_tx_done(struct usb_serial_port *port)
1291{
1292 struct whiteheat_simple close_command;
1293
1294 close_command.port = port->number - port->serial->minor + 1;
1295 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1296 (__u8 *)&close_command, sizeof(close_command));
1297}
1298
1299
1300/*****************************************************************************
1301 * Connect Tech's White Heat utility functions
1302 *****************************************************************************/
1303static int start_command_port(struct usb_serial *serial)
1304{
1305 struct usb_serial_port *command_port;
1306 struct whiteheat_command_private *command_info;
1307 int retval = 0;
1308
1309 command_port = serial->port[COMMAND_PORT];
1310 command_info = usb_get_serial_port_data(command_port);
1311 mutex_lock(&command_info->mutex);
1312 if (!command_info->port_running) {
1313 /* Work around HCD bugs */
1314 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1315
1316 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1317 if (retval) {
1318 dev_err(&serial->dev->dev,
1319 "%s - failed submitting read urb, error %d\n",
1320 __func__, retval);
1321 goto exit;
1322 }
1323 }
1324 command_info->port_running++;
1325
1326exit:
1327 mutex_unlock(&command_info->mutex);
1328 return retval;
1329}
1330
1331
1332static void stop_command_port(struct usb_serial *serial)
1333{
1334 struct usb_serial_port *command_port;
1335 struct whiteheat_command_private *command_info;
1336
1337 command_port = serial->port[COMMAND_PORT];
1338 command_info = usb_get_serial_port_data(command_port);
1339 mutex_lock(&command_info->mutex);
1340 command_info->port_running--;
1341 if (!command_info->port_running)
1342 usb_kill_urb(command_port->read_urb);
1343 mutex_unlock(&command_info->mutex);
1344}
1345
1346
1347static int start_port_read(struct usb_serial_port *port)
1348{
1349 struct whiteheat_private *info = usb_get_serial_port_data(port);
1350 struct whiteheat_urb_wrap *wrap;
1351 struct urb *urb;
1352 int retval = 0;
1353 unsigned long flags;
1354 struct list_head *tmp;
1355 struct list_head *tmp2;
1356
1357 spin_lock_irqsave(&info->lock, flags);
1358
1359 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1360 list_del(tmp);
1361 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1362 urb = wrap->urb;
1363 spin_unlock_irqrestore(&info->lock, flags);
1364 retval = usb_submit_urb(urb, GFP_KERNEL);
1365 if (retval) {
1366 spin_lock_irqsave(&info->lock, flags);
1367 list_add(tmp, &info->rx_urbs_free);
1368 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1369 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1370 urb = wrap->urb;
1371 list_del(tmp);
1372 spin_unlock_irqrestore(&info->lock, flags);
1373 usb_kill_urb(urb);
1374 spin_lock_irqsave(&info->lock, flags);
1375 list_add(tmp, &info->rx_urbs_free);
1376 }
1377 break;
1378 }
1379 spin_lock_irqsave(&info->lock, flags);
1380 list_add(tmp, &info->rx_urbs_submitted);
1381 }
1382
1383 spin_unlock_irqrestore(&info->lock, flags);
1384
1385 return retval;
1386}
1387
1388
1389static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1390 struct list_head *head)
1391{
1392 struct whiteheat_urb_wrap *wrap;
1393 struct list_head *tmp;
1394
1395 list_for_each(tmp, head) {
1396 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1397 if (wrap->urb == urb)
1398 return wrap;
1399 }
1400
1401 return NULL;
1402}
1403
1404
1405static struct list_head *list_first(struct list_head *head)
1406{
1407 return head->next;
1408}
1409
1410
1411static void rx_data_softint(struct work_struct *work)
1412{
1413 struct whiteheat_private *info =
1414 container_of(work, struct whiteheat_private, rx_work);
1415 struct usb_serial_port *port = info->port;
1416 struct tty_struct *tty = tty_port_tty_get(&port->port);
1417 struct whiteheat_urb_wrap *wrap;
1418 struct urb *urb;
1419 unsigned long flags;
1420 struct list_head *tmp;
1421 struct list_head *tmp2;
1422 int result;
1423 int sent = 0;
1424
1425 spin_lock_irqsave(&info->lock, flags);
1426 if (info->flags & THROTTLED) {
1427 spin_unlock_irqrestore(&info->lock, flags);
1428 goto out;
1429 }
1430
1431 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1432 list_del(tmp);
1433 spin_unlock_irqrestore(&info->lock, flags);
1434
1435 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1436 urb = wrap->urb;
1437
1438 if (tty && urb->actual_length)
1439 sent += tty_insert_flip_string(tty,
1440 urb->transfer_buffer, urb->actual_length);
1441
1442 result = usb_submit_urb(urb, GFP_ATOMIC);
1443 if (result) {
1444 dev_err(&port->dev,
1445 "%s - failed resubmitting read urb, error %d\n",
1446 __func__, result);
1447 spin_lock_irqsave(&info->lock, flags);
1448 list_add(tmp, &info->rx_urbs_free);
1449 continue;
1450 }
1451
1452 spin_lock_irqsave(&info->lock, flags);
1453 list_add(tmp, &info->rx_urbs_submitted);
1454 }
1455 spin_unlock_irqrestore(&info->lock, flags);
1456
1457 if (sent)
1458 tty_flip_buffer_push(tty);
1459out:
1460 tty_kref_put(tty);
1461}
1462
1463module_usb_serial_driver(whiteheat_driver, serial_drivers);
1464
1465MODULE_AUTHOR(DRIVER_AUTHOR);
1466MODULE_DESCRIPTION(DRIVER_DESC);
1467MODULE_LICENSE("GPL");
1468
1469MODULE_FIRMWARE("whiteheat.fw");
1470MODULE_FIRMWARE("whiteheat_loader.fw");
1471
1472module_param(urb_pool_size, int, 0);
1473MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1474
1475module_param(debug, bool, S_IRUGO | S_IWUSR);
1476MODULE_PARM_DESC(debug, "Debug enabled or not");