]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/serial/whiteheat.c
USB: whiteheat: fix memory leak in error path
[mirror_ubuntu-bionic-kernel.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>
cc183e2a 35#include <linux/usb/ezusb.h>
1da177e4
LT
36#include "whiteheat.h" /* WhiteHEAT specific commands */
37
1da177e4
LT
38#ifndef CMSPAR
39#define CMSPAR 0
40#endif
41
42/*
43 * Version Information
44 */
1da177e4
LT
45#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
46#define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
47
48#define CONNECT_TECH_VENDOR_ID 0x0710
49#define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
50#define CONNECT_TECH_WHITE_HEAT_ID 0x8001
51
52/*
53 ID tables for whiteheat are unusual, because we want to different
54 things for different versions of the device. Eventually, this
55 will be doable from a single table. But, for now, we define two
56 separate ID tables, and then a third table that combines them
57 just for the purpose of exporting the autoloading information.
58*/
7d40d7e8 59static const struct usb_device_id id_table_std[] = {
1da177e4
LT
60 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
61 { } /* Terminating entry */
62};
63
7d40d7e8 64static const struct usb_device_id id_table_prerenumeration[] = {
1da177e4
LT
65 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
66 { } /* Terminating entry */
67};
68
7d40d7e8 69static const struct usb_device_id id_table_combined[] = {
1da177e4
LT
70 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
71 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
72 { } /* Terminating entry */
73};
74
80359a9c 75MODULE_DEVICE_TABLE(usb, id_table_combined);
1da177e4 76
1da177e4
LT
77
78/* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
80359a9c
AC
79static int whiteheat_firmware_download(struct usb_serial *serial,
80 const struct usb_device_id *id);
81static int whiteheat_firmware_attach(struct usb_serial *serial);
1da177e4
LT
82
83/* function prototypes for the Connect Tech WhiteHEAT serial converter */
80359a9c 84static int whiteheat_attach(struct usb_serial *serial);
f9c99bb8 85static void whiteheat_release(struct usb_serial *serial);
80359a9c 86static int whiteheat_open(struct tty_struct *tty,
a509a7e4 87 struct usb_serial_port *port);
335f8514 88static void whiteheat_close(struct usb_serial_port *port);
00a0d0d6 89static int whiteheat_ioctl(struct tty_struct *tty,
80359a9c
AC
90 unsigned int cmd, unsigned long arg);
91static void whiteheat_set_termios(struct tty_struct *tty,
92 struct usb_serial_port *port, struct ktermios *old);
60b33c13 93static int whiteheat_tiocmget(struct tty_struct *tty);
20b9d177 94static int whiteheat_tiocmset(struct tty_struct *tty,
80359a9c
AC
95 unsigned int set, unsigned int clear);
96static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
1da177e4 97
ea65370d 98static struct usb_serial_driver whiteheat_fake_device = {
18fcac35
GKH
99 .driver = {
100 .owner = THIS_MODULE,
269bda1c 101 .name = "whiteheatnofirm",
18fcac35 102 },
269bda1c 103 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
1da177e4 104 .id_table = id_table_prerenumeration,
1da177e4
LT
105 .num_ports = 1,
106 .probe = whiteheat_firmware_download,
107 .attach = whiteheat_firmware_attach,
108};
109
ea65370d 110static struct usb_serial_driver whiteheat_device = {
18fcac35
GKH
111 .driver = {
112 .owner = THIS_MODULE,
269bda1c 113 .name = "whiteheat",
18fcac35 114 },
269bda1c 115 .description = "Connect Tech - WhiteHEAT",
1da177e4 116 .id_table = id_table_std,
1da177e4
LT
117 .num_ports = 4,
118 .attach = whiteheat_attach,
f9c99bb8 119 .release = whiteheat_release,
1da177e4
LT
120 .open = whiteheat_open,
121 .close = whiteheat_close,
1da177e4
LT
122 .ioctl = whiteheat_ioctl,
123 .set_termios = whiteheat_set_termios,
124 .break_ctl = whiteheat_break_ctl,
125 .tiocmget = whiteheat_tiocmget,
126 .tiocmset = whiteheat_tiocmset,
5c0c7582
JH
127 .throttle = usb_serial_generic_throttle,
128 .unthrottle = usb_serial_generic_unthrottle,
1da177e4
LT
129};
130
29618e9f
AS
131static struct usb_serial_driver * const serial_drivers[] = {
132 &whiteheat_fake_device, &whiteheat_device, NULL
133};
1da177e4
LT
134
135struct whiteheat_command_private {
08a2b3b6 136 struct mutex mutex;
1da177e4
LT
137 __u8 port_running;
138 __u8 command_finished;
80359a9c
AC
139 wait_queue_head_t wait_command; /* for handling sleeping whilst
140 waiting for a command to
141 finish */
1da177e4
LT
142 __u8 result_buffer[64];
143};
144
1da177e4 145struct whiteheat_private {
a5b6f60c 146 __u8 mcr; /* FIXME: no locking on mcr */
1da177e4
LT
147};
148
149
150/* local function prototypes */
151static int start_command_port(struct usb_serial *serial);
152static void stop_command_port(struct usb_serial *serial);
7d12e780
DH
153static void command_port_write_callback(struct urb *urb);
154static void command_port_read_callback(struct urb *urb);
1da177e4 155
80359a9c
AC
156static int firm_send_command(struct usb_serial_port *port, __u8 command,
157 __u8 *data, __u8 datasize);
1da177e4
LT
158static int firm_open(struct usb_serial_port *port);
159static int firm_close(struct usb_serial_port *port);
fe1ae7fd 160static void firm_setup_port(struct tty_struct *tty);
1da177e4
LT
161static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
162static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
163static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
164static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
165static int firm_get_dtr_rts(struct usb_serial_port *port);
166static int firm_report_tx_done(struct usb_serial_port *port);
167
168
169#define COMMAND_PORT 4
170#define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
171#define COMMAND_TIMEOUT_MS 2000
172#define CLOSING_DELAY (30 * HZ)
173
174
175/*****************************************************************************
176 * Connect Tech's White Heat prerenumeration driver functions
177 *****************************************************************************/
178
179/* steps to download the firmware to the WhiteHEAT device:
180 - hold the reset (by writing to the reset bit of the CPUCS register)
181 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
182 - release the reset (by writing to the CPUCS register)
183 - download the WH.HEX file for all addresses greater than 0x1b3f using
184 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
185 - hold the reset
186 - download the WH.HEX file for all addresses less than 0x1b40 using
187 VENDOR_REQUEST_ANCHOR_LOAD
188 - release the reset
189 - device renumerated itself and comes up as new device id with all
190 firmware download completed.
191*/
80359a9c
AC
192static int whiteheat_firmware_download(struct usb_serial *serial,
193 const struct usb_device_id *id)
1da177e4 194{
8d733e26 195 int response;
ec6752f5 196
8d733e26
RB
197 response = ezusb_fx1_ihex_firmware_download(serial->dev, "whiteheat_loader.fw");
198 if (response >= 0) {
199 response = ezusb_fx1_ihex_firmware_download(serial->dev, "whiteheat.fw");
200 if (response >= 0)
201 return 0;
ec6752f5 202 }
8d733e26 203 return -ENOENT;
1da177e4
LT
204}
205
206
80359a9c 207static int whiteheat_firmware_attach(struct usb_serial *serial)
1da177e4
LT
208{
209 /* We want this device to fail to have a driver assigned to it */
210 return 1;
211}
212
213
214/*****************************************************************************
215 * Connect Tech's White Heat serial driver functions
216 *****************************************************************************/
80359a9c 217static int whiteheat_attach(struct usb_serial *serial)
1da177e4
LT
218{
219 struct usb_serial_port *command_port;
220 struct whiteheat_command_private *command_info;
221 struct usb_serial_port *port;
222 struct whiteheat_private *info;
223 struct whiteheat_hw_info *hw_info;
224 int pipe;
225 int ret;
226 int alen;
227 __u8 *command;
228 __u8 *result;
229 int i;
1da177e4
LT
230
231 command_port = serial->port[COMMAND_PORT];
232
80359a9c
AC
233 pipe = usb_sndbulkpipe(serial->dev,
234 command_port->bulk_out_endpointAddress);
1da177e4
LT
235 command = kmalloc(2, GFP_KERNEL);
236 if (!command)
237 goto no_command_buffer;
238 command[0] = WHITEHEAT_GET_HW_INFO;
239 command[1] = 0;
80359a9c 240
1da177e4
LT
241 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
242 if (!result)
243 goto no_result_buffer;
244 /*
245 * When the module is reloaded the firmware is still there and
246 * the endpoints are still in the usb core unchanged. This is the
80359a9c
AC
247 * unlinking bug in disguise. Same for the call below.
248 */
1da177e4 249 usb_clear_halt(serial->dev, pipe);
80359a9c
AC
250 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
251 &alen, COMMAND_TIMEOUT_MS);
1da177e4 252 if (ret) {
194343d9
GKH
253 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
254 serial->type->description, ret);
1da177e4 255 goto no_firmware;
09fd6bc8 256 } else if (alen != 2) {
194343d9
GKH
257 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
258 serial->type->description, alen);
1da177e4
LT
259 goto no_firmware;
260 }
261
80359a9c
AC
262 pipe = usb_rcvbulkpipe(serial->dev,
263 command_port->bulk_in_endpointAddress);
1da177e4
LT
264 /* See the comment on the usb_clear_halt() above */
265 usb_clear_halt(serial->dev, pipe);
80359a9c
AC
266 ret = usb_bulk_msg(serial->dev, pipe, result,
267 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
1da177e4 268 if (ret) {
194343d9
GKH
269 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
270 serial->type->description, ret);
1da177e4 271 goto no_firmware;
09fd6bc8 272 } else if (alen != sizeof(*hw_info) + 1) {
194343d9
GKH
273 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
274 serial->type->description, alen);
1da177e4
LT
275 goto no_firmware;
276 } else if (result[0] != command[0]) {
194343d9
GKH
277 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
278 serial->type->description, result[0]);
1da177e4
LT
279 goto no_firmware;
280 }
281
282 hw_info = (struct whiteheat_hw_info *)&result[1];
283
a6765cba
JH
284 dev_info(&serial->dev->dev, "%s: Firmware v%d.%02d\n",
285 serial->type->description,
c197a8db 286 hw_info->sw_major_rev, hw_info->sw_minor_rev);
1da177e4
LT
287
288 for (i = 0; i < serial->num_ports; i++) {
289 port = serial->port[i];
290
5cbded58 291 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
1da177e4 292 if (info == NULL) {
194343d9
GKH
293 dev_err(&port->dev,
294 "%s: Out of memory for port structures\n",
295 serial->type->description);
1da177e4
LT
296 goto no_private;
297 }
298
1da177e4 299 info->mcr = 0;
1da177e4
LT
300
301 usb_set_serial_port_data(port, info);
302 }
303
80359a9c
AC
304 command_info = kmalloc(sizeof(struct whiteheat_command_private),
305 GFP_KERNEL);
1da177e4 306 if (command_info == NULL) {
194343d9
GKH
307 dev_err(&serial->dev->dev,
308 "%s: Out of memory for port structures\n",
309 serial->type->description);
1da177e4
LT
310 goto no_command_private;
311 }
312
08a2b3b6 313 mutex_init(&command_info->mutex);
1da177e4
LT
314 command_info->port_running = 0;
315 init_waitqueue_head(&command_info->wait_command);
316 usb_set_serial_port_data(command_port, command_info);
317 command_port->write_urb->complete = command_port_write_callback;
318 command_port->read_urb->complete = command_port_read_callback;
319 kfree(result);
320 kfree(command);
321
322 return 0;
323
324no_firmware:
325 /* Firmware likely not running */
194343d9
GKH
326 dev_err(&serial->dev->dev,
327 "%s: Unable to retrieve firmware version, try replugging\n",
328 serial->type->description);
329 dev_err(&serial->dev->dev,
330 "%s: If the firmware is not running (status led not blinking)\n",
331 serial->type->description);
332 dev_err(&serial->dev->dev,
333 "%s: please contact support@connecttech.com\n",
334 serial->type->description);
67ca0284 335 kfree(result);
c129197c 336 kfree(command);
1da177e4
LT
337 return -ENODEV;
338
339no_command_private:
340 for (i = serial->num_ports - 1; i >= 0; i--) {
341 port = serial->port[i];
342 info = usb_get_serial_port_data(port);
1da177e4
LT
343 kfree(info);
344no_private:
345 ;
346 }
347 kfree(result);
348no_result_buffer:
349 kfree(command);
350no_command_buffer:
351 return -ENOMEM;
352}
353
354
f9c99bb8 355static void whiteheat_release(struct usb_serial *serial)
1da177e4
LT
356{
357 struct usb_serial_port *command_port;
1da177e4 358 struct whiteheat_private *info;
1da177e4
LT
359 int i;
360
1da177e4
LT
361 /* free up our private data for our command port */
362 command_port = serial->port[COMMAND_PORT];
80359a9c 363 kfree(usb_get_serial_port_data(command_port));
1da177e4
LT
364
365 for (i = 0; i < serial->num_ports; i++) {
5c0c7582 366 info = usb_get_serial_port_data(serial->port[i]);
1da177e4
LT
367 kfree(info);
368 }
1da177e4
LT
369}
370
a509a7e4 371static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
1da177e4 372{
5c0c7582 373 int retval;
1da177e4 374
1da177e4
LT
375 retval = start_command_port(port->serial);
376 if (retval)
377 goto exit;
378
1da177e4
LT
379 /* send an open port command */
380 retval = firm_open(port);
381 if (retval) {
382 stop_command_port(port->serial);
383 goto exit;
384 }
385
386 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
387 if (retval) {
388 firm_close(port);
389 stop_command_port(port->serial);
390 goto exit;
391 }
392
72e27412
AC
393 if (tty)
394 firm_setup_port(tty);
1da177e4
LT
395
396 /* Work around HCD bugs */
397 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
398 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
399
5c0c7582 400 retval = usb_serial_generic_open(tty, port);
1da177e4 401 if (retval) {
1da177e4
LT
402 firm_close(port);
403 stop_command_port(port->serial);
404 goto exit;
405 }
1da177e4 406exit:
1da177e4
LT
407 return retval;
408}
409
410
335f8514 411static void whiteheat_close(struct usb_serial_port *port)
1da177e4 412{
1da177e4 413 firm_report_tx_done(port);
1da177e4
LT
414 firm_close(port);
415
5c0c7582 416 usb_serial_generic_close(port);
1da177e4 417
5c0c7582 418 stop_command_port(port->serial);
1da177e4
LT
419}
420
60b33c13 421static int whiteheat_tiocmget(struct tty_struct *tty)
1da177e4 422{
95da310e 423 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
424 struct whiteheat_private *info = usb_get_serial_port_data(port);
425 unsigned int modem_signals = 0;
426
1da177e4
LT
427 firm_get_dtr_rts(port);
428 if (info->mcr & UART_MCR_DTR)
429 modem_signals |= TIOCM_DTR;
430 if (info->mcr & UART_MCR_RTS)
431 modem_signals |= TIOCM_RTS;
432
433 return modem_signals;
434}
435
20b9d177 436static int whiteheat_tiocmset(struct tty_struct *tty,
1da177e4
LT
437 unsigned int set, unsigned int clear)
438{
95da310e 439 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
440 struct whiteheat_private *info = usb_get_serial_port_data(port);
441
1da177e4
LT
442 if (set & TIOCM_RTS)
443 info->mcr |= UART_MCR_RTS;
444 if (set & TIOCM_DTR)
445 info->mcr |= UART_MCR_DTR;
446
447 if (clear & TIOCM_RTS)
448 info->mcr &= ~UART_MCR_RTS;
449 if (clear & TIOCM_DTR)
450 info->mcr &= ~UART_MCR_DTR;
451
452 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
453 firm_set_rts(port, info->mcr & UART_MCR_RTS);
454 return 0;
455}
456
457
00a0d0d6 458static int whiteheat_ioctl(struct tty_struct *tty,
80359a9c 459 unsigned int cmd, unsigned long arg)
1da177e4 460{
95da310e 461 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
462 struct serial_struct serstruct;
463 void __user *user_arg = (void __user *)arg;
464
255b703a 465 dev_dbg(&port->dev, "%s - cmd 0x%.4x\n", __func__, cmd);
1da177e4
LT
466
467 switch (cmd) {
80359a9c
AC
468 case TIOCGSERIAL:
469 memset(&serstruct, 0, sizeof(serstruct));
470 serstruct.type = PORT_16654;
471 serstruct.line = port->serial->minor;
472 serstruct.port = port->number;
473 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
5c0c7582 474 serstruct.xmit_fifo_size = kfifo_size(&port->write_fifo);
80359a9c
AC
475 serstruct.custom_divisor = 0;
476 serstruct.baud_base = 460800;
477 serstruct.close_delay = CLOSING_DELAY;
478 serstruct.closing_wait = CLOSING_DELAY;
479
480 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
481 return -EFAULT;
482 break;
483 default:
484 break;
1da177e4
LT
485 }
486
487 return -ENOIOCTLCMD;
488}
489
490
80359a9c
AC
491static void whiteheat_set_termios(struct tty_struct *tty,
492 struct usb_serial_port *port, struct ktermios *old_termios)
1da177e4 493{
95da310e 494 firm_setup_port(tty);
1da177e4
LT
495}
496
80359a9c
AC
497static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
498{
95da310e 499 struct usb_serial_port *port = tty->driver_data;
1da177e4
LT
500 firm_set_break(port, break_state);
501}
502
503
1da177e4
LT
504/*****************************************************************************
505 * Connect Tech's White Heat callback routines
506 *****************************************************************************/
08a2b3b6 507static void command_port_write_callback(struct urb *urb)
1da177e4 508{
05400013
GKH
509 int status = urb->status;
510
05400013 511 if (status) {
255b703a 512 dev_dbg(&urb->dev->dev, "nonzero urb status: %d\n", status);
1da177e4
LT
513 return;
514 }
515}
516
517
08a2b3b6 518static void command_port_read_callback(struct urb *urb)
1da177e4 519{
cdc97792 520 struct usb_serial_port *command_port = urb->context;
1da177e4 521 struct whiteheat_command_private *command_info;
05400013 522 int status = urb->status;
1da177e4
LT
523 unsigned char *data = urb->transfer_buffer;
524 int result;
1da177e4 525
08a2b3b6
ON
526 command_info = usb_get_serial_port_data(command_port);
527 if (!command_info) {
255b703a 528 dev_dbg(&urb->dev->dev, "%s - command_info is NULL, exiting.\n", __func__);
08a2b3b6
ON
529 return;
530 }
05400013 531 if (status) {
255b703a 532 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", __func__, status);
05400013 533 if (status != -ENOENT)
08a2b3b6
ON
534 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
535 wake_up(&command_info->wait_command);
1da177e4
LT
536 return;
537 }
538
59d33f2f 539 usb_serial_debug_data(&command_port->dev, __func__, urb->actual_length, data);
1da177e4 540
1da177e4
LT
541 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
542 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
08a2b3b6 543 wake_up(&command_info->wait_command);
1da177e4
LT
544 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
545 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
08a2b3b6 546 wake_up(&command_info->wait_command);
1da177e4 547 } else if (data[0] == WHITEHEAT_EVENT) {
80359a9c
AC
548 /* These are unsolicited reports from the firmware, hence no
549 waiting command to wakeup */
255b703a 550 dev_dbg(&urb->dev->dev, "%s - event received\n", __func__);
1da177e4 551 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
80359a9c
AC
552 memcpy(command_info->result_buffer, &data[1],
553 urb->actual_length - 1);
1da177e4 554 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
08a2b3b6 555 wake_up(&command_info->wait_command);
80359a9c 556 } else
255b703a 557 dev_dbg(&urb->dev->dev, "%s - bad reply from firmware\n", __func__);
80359a9c 558
1da177e4 559 /* Continue trying to always read */
1da177e4 560 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
1da177e4 561 if (result)
255b703a 562 dev_dbg(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n",
80359a9c 563 __func__, result);
1da177e4
LT
564}
565
566
1da177e4
LT
567/*****************************************************************************
568 * Connect Tech's White Heat firmware interface
569 *****************************************************************************/
80359a9c
AC
570static int firm_send_command(struct usb_serial_port *port, __u8 command,
571 __u8 *data, __u8 datasize)
1da177e4
LT
572{
573 struct usb_serial_port *command_port;
574 struct whiteheat_command_private *command_info;
575 struct whiteheat_private *info;
255b703a 576 struct device *dev = &port->dev;
1da177e4
LT
577 __u8 *transfer_buffer;
578 int retval = 0;
08a2b3b6 579 int t;
1da177e4 580
255b703a 581 dev_dbg(dev, "%s - command %d\n", __func__, command);
1da177e4
LT
582
583 command_port = port->serial->port[COMMAND_PORT];
584 command_info = usb_get_serial_port_data(command_port);
08a2b3b6 585 mutex_lock(&command_info->mutex);
38c3cb5b 586 command_info->command_finished = false;
80359a9c 587
1da177e4
LT
588 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
589 transfer_buffer[0] = command;
80359a9c 590 memcpy(&transfer_buffer[1], data, datasize);
1da177e4 591 command_port->write_urb->transfer_buffer_length = datasize + 1;
80359a9c 592 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1da177e4 593 if (retval) {
255b703a 594 dev_dbg(dev, "%s - submit urb failed\n", __func__);
1da177e4
LT
595 goto exit;
596 }
1da177e4
LT
597
598 /* wait for the command to complete */
08a2b3b6 599 t = wait_event_timeout(command_info->wait_command,
38c3cb5b 600 (bool)command_info->command_finished, COMMAND_TIMEOUT);
08a2b3b6
ON
601 if (!t)
602 usb_kill_urb(command_port->write_urb);
1da177e4 603
38c3cb5b 604 if (command_info->command_finished == false) {
255b703a 605 dev_dbg(dev, "%s - command timed out.\n", __func__);
1da177e4
LT
606 retval = -ETIMEDOUT;
607 goto exit;
608 }
609
610 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
255b703a 611 dev_dbg(dev, "%s - command failed.\n", __func__);
1da177e4
LT
612 retval = -EIO;
613 goto exit;
614 }
615
616 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
255b703a 617 dev_dbg(dev, "%s - command completed.\n", __func__);
1da177e4 618 switch (command) {
80359a9c
AC
619 case WHITEHEAT_GET_DTR_RTS:
620 info = usb_get_serial_port_data(port);
621 memcpy(&info->mcr, command_info->result_buffer,
622 sizeof(struct whiteheat_dr_info));
1da177e4
LT
623 break;
624 }
625 }
1da177e4 626exit:
08a2b3b6 627 mutex_unlock(&command_info->mutex);
1da177e4
LT
628 return retval;
629}
630
631
80359a9c
AC
632static int firm_open(struct usb_serial_port *port)
633{
1da177e4
LT
634 struct whiteheat_simple open_command;
635
636 open_command.port = port->number - port->serial->minor + 1;
80359a9c
AC
637 return firm_send_command(port, WHITEHEAT_OPEN,
638 (__u8 *)&open_command, sizeof(open_command));
1da177e4
LT
639}
640
641
80359a9c
AC
642static int firm_close(struct usb_serial_port *port)
643{
1da177e4
LT
644 struct whiteheat_simple close_command;
645
646 close_command.port = port->number - port->serial->minor + 1;
80359a9c
AC
647 return firm_send_command(port, WHITEHEAT_CLOSE,
648 (__u8 *)&close_command, sizeof(close_command));
1da177e4
LT
649}
650
651
fe1ae7fd 652static void firm_setup_port(struct tty_struct *tty)
80359a9c 653{
95da310e 654 struct usb_serial_port *port = tty->driver_data;
255b703a 655 struct device *dev = &port->dev;
1da177e4 656 struct whiteheat_port_settings port_settings;
adc8d746 657 unsigned int cflag = tty->termios.c_cflag;
1da177e4
LT
658
659 port_settings.port = port->number + 1;
660
661 /* get the byte size */
662 switch (cflag & CSIZE) {
80359a9c
AC
663 case CS5: port_settings.bits = 5; break;
664 case CS6: port_settings.bits = 6; break;
665 case CS7: port_settings.bits = 7; break;
666 default:
667 case CS8: port_settings.bits = 8; break;
1da177e4 668 }
255b703a 669 dev_dbg(dev, "%s - data bits = %d\n", __func__, port_settings.bits);
80359a9c 670
1da177e4
LT
671 /* determine the parity */
672 if (cflag & PARENB)
673 if (cflag & CMSPAR)
674 if (cflag & PARODD)
675 port_settings.parity = WHITEHEAT_PAR_MARK;
676 else
677 port_settings.parity = WHITEHEAT_PAR_SPACE;
678 else
679 if (cflag & PARODD)
680 port_settings.parity = WHITEHEAT_PAR_ODD;
681 else
682 port_settings.parity = WHITEHEAT_PAR_EVEN;
683 else
684 port_settings.parity = WHITEHEAT_PAR_NONE;
255b703a 685 dev_dbg(dev, "%s - parity = %c\n", __func__, port_settings.parity);
1da177e4
LT
686
687 /* figure out the stop bits requested */
688 if (cflag & CSTOPB)
689 port_settings.stop = 2;
690 else
691 port_settings.stop = 1;
255b703a 692 dev_dbg(dev, "%s - stop bits = %d\n", __func__, port_settings.stop);
1da177e4
LT
693
694 /* figure out the flow control settings */
695 if (cflag & CRTSCTS)
80359a9c
AC
696 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
697 WHITEHEAT_HFLOW_RTS);
1da177e4
LT
698 else
699 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
255b703a 700 dev_dbg(dev, "%s - hardware flow control = %s %s %s %s\n", __func__,
1da177e4
LT
701 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
702 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
703 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
704 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
80359a9c 705
1da177e4 706 /* determine software flow control */
95da310e 707 if (I_IXOFF(tty))
1da177e4
LT
708 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
709 else
710 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
255b703a 711 dev_dbg(dev, "%s - software flow control = %c\n", __func__, port_settings.sflow);
80359a9c 712
95da310e
AC
713 port_settings.xon = START_CHAR(tty);
714 port_settings.xoff = STOP_CHAR(tty);
255b703a 715 dev_dbg(dev, "%s - XON = %2x, XOFF = %2x\n", __func__, port_settings.xon, port_settings.xoff);
1da177e4
LT
716
717 /* get the baud rate wanted */
95da310e 718 port_settings.baud = tty_get_baud_rate(tty);
255b703a 719 dev_dbg(dev, "%s - baud rate = %d\n", __func__, port_settings.baud);
1da177e4 720
01d1df29 721 /* fixme: should set validated settings */
95da310e 722 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1da177e4
LT
723 /* handle any settings that aren't specified in the tty structure */
724 port_settings.lloop = 0;
80359a9c 725
1da177e4 726 /* now send the message to the device */
fe1ae7fd 727 firm_send_command(port, WHITEHEAT_SETUP_PORT,
80359a9c 728 (__u8 *)&port_settings, sizeof(port_settings));
1da177e4
LT
729}
730
731
80359a9c
AC
732static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
733{
1da177e4
LT
734 struct whiteheat_set_rdb rts_command;
735
736 rts_command.port = port->number - port->serial->minor + 1;
737 rts_command.state = onoff;
80359a9c
AC
738 return firm_send_command(port, WHITEHEAT_SET_RTS,
739 (__u8 *)&rts_command, sizeof(rts_command));
1da177e4
LT
740}
741
742
80359a9c
AC
743static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
744{
1da177e4
LT
745 struct whiteheat_set_rdb dtr_command;
746
747 dtr_command.port = port->number - port->serial->minor + 1;
748 dtr_command.state = onoff;
72e27412 749 return firm_send_command(port, WHITEHEAT_SET_DTR,
80359a9c 750 (__u8 *)&dtr_command, sizeof(dtr_command));
1da177e4
LT
751}
752
753
80359a9c
AC
754static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
755{
1da177e4
LT
756 struct whiteheat_set_rdb break_command;
757
758 break_command.port = port->number - port->serial->minor + 1;
759 break_command.state = onoff;
72e27412 760 return firm_send_command(port, WHITEHEAT_SET_BREAK,
80359a9c 761 (__u8 *)&break_command, sizeof(break_command));
1da177e4
LT
762}
763
764
80359a9c
AC
765static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
766{
1da177e4
LT
767 struct whiteheat_purge purge_command;
768
769 purge_command.port = port->number - port->serial->minor + 1;
770 purge_command.what = rxtx;
80359a9c
AC
771 return firm_send_command(port, WHITEHEAT_PURGE,
772 (__u8 *)&purge_command, sizeof(purge_command));
1da177e4
LT
773}
774
775
80359a9c
AC
776static int firm_get_dtr_rts(struct usb_serial_port *port)
777{
1da177e4
LT
778 struct whiteheat_simple get_dr_command;
779
780 get_dr_command.port = port->number - port->serial->minor + 1;
80359a9c
AC
781 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
782 (__u8 *)&get_dr_command, sizeof(get_dr_command));
1da177e4
LT
783}
784
785
80359a9c
AC
786static int firm_report_tx_done(struct usb_serial_port *port)
787{
1da177e4
LT
788 struct whiteheat_simple close_command;
789
790 close_command.port = port->number - port->serial->minor + 1;
80359a9c
AC
791 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
792 (__u8 *)&close_command, sizeof(close_command));
1da177e4
LT
793}
794
795
796/*****************************************************************************
797 * Connect Tech's White Heat utility functions
798 *****************************************************************************/
799static int start_command_port(struct usb_serial *serial)
800{
801 struct usb_serial_port *command_port;
802 struct whiteheat_command_private *command_info;
1da177e4 803 int retval = 0;
80359a9c 804
1da177e4
LT
805 command_port = serial->port[COMMAND_PORT];
806 command_info = usb_get_serial_port_data(command_port);
08a2b3b6 807 mutex_lock(&command_info->mutex);
1da177e4
LT
808 if (!command_info->port_running) {
809 /* Work around HCD bugs */
810 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
811
1da177e4
LT
812 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
813 if (retval) {
194343d9
GKH
814 dev_err(&serial->dev->dev,
815 "%s - failed submitting read urb, error %d\n",
816 __func__, retval);
1da177e4
LT
817 goto exit;
818 }
819 }
820 command_info->port_running++;
821
822exit:
08a2b3b6 823 mutex_unlock(&command_info->mutex);
1da177e4
LT
824 return retval;
825}
826
827
828static void stop_command_port(struct usb_serial *serial)
829{
830 struct usb_serial_port *command_port;
831 struct whiteheat_command_private *command_info;
1da177e4
LT
832
833 command_port = serial->port[COMMAND_PORT];
834 command_info = usb_get_serial_port_data(command_port);
08a2b3b6 835 mutex_lock(&command_info->mutex);
1da177e4
LT
836 command_info->port_running--;
837 if (!command_info->port_running)
838 usb_kill_urb(command_port->read_urb);
08a2b3b6 839 mutex_unlock(&command_info->mutex);
1da177e4
LT
840}
841
68e24113 842module_usb_serial_driver(serial_drivers, id_table_combined);
1da177e4 843
80359a9c
AC
844MODULE_AUTHOR(DRIVER_AUTHOR);
845MODULE_DESCRIPTION(DRIVER_DESC);
1da177e4
LT
846MODULE_LICENSE("GPL");
847
ec6752f5
DW
848MODULE_FIRMWARE("whiteheat.fw");
849MODULE_FIRMWARE("whiteheat_loader.fw");