blob: e89e0d589eb64e75359bb965ccea9378132dfa9a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -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 *
Alan Cox80359a92008-07-22 11:09:16 +010015 * See Documentation/usb/usb-serial.txt for more information on using this
16 * driver
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
18 * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com)
19 * Upgrade to full working driver
20 *
21 * (05/30/2001) gkh
Alan Cox80359a92008-07-22 11:09:16 +010022 * switched from using spinlock to a semaphore, which fixes lots of
23 * problems.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 *
25 * (04/08/2001) gb
26 * Identify version on module load.
Alan Cox80359a92008-07-22 11:09:16 +010027 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * 2001_Mar_19 gkh
Alan Cox80359a92008-07-22 11:09:16 +010029 * Fixed MOD_INC and MOD_DEC logic, the ability to open a port more
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * than once, and the got the proper usb_device_id table entries so
31 * the driver works again.
32 *
33 * (11/01/2000) Adam J. Richter
34 * usb_device_id table support
Alan Cox80359a92008-07-22 11:09:16 +010035 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * (10/05/2000) gkh
37 * Fixed bug with urb->dev not being set properly, now that the usb
38 * core needs it.
Alan Cox80359a92008-07-22 11:09:16 +010039 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * (10/03/2000) smd
41 * firmware is improved to guard against crap sent to device
42 * firmware now replies CMD_FAILURE on bad things
43 * read_callback fix you provided for private info struct
44 * command_finished now indicates success or fail
45 * setup_port struct now packed to avoid gcc padding
46 * firmware uses 1 based port numbering, driver now handles that
47 *
48 * (09/11/2000) gkh
49 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
50 *
51 * (07/19/2000) gkh
52 * Added module_init and module_exit functions to handle the fact that this
53 * driver is a loadable module now.
54 * Fixed bug with port->minor that was found by Al Borchers
55 *
56 * (07/04/2000) gkh
Alan Cox80359a92008-07-22 11:09:16 +010057 * Added support for port settings. Baud rate can now be changed. Line
58 * signals are not transferred to and from the tty layer yet, but things
59 * seem to be working well now.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
61 * (05/04/2000) gkh
62 * First cut at open and close commands. Data can flow through the ports at
63 * default speeds now.
64 *
65 * (03/26/2000) gkh
66 * Split driver up into device specific pieces.
Alan Cox80359a92008-07-22 11:09:16 +010067 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 */
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/kernel.h>
71#include <linux/errno.h>
72#include <linux/init.h>
73#include <linux/slab.h>
74#include <linux/tty.h>
75#include <linux/tty_driver.h>
76#include <linux/tty_flip.h>
77#include <linux/module.h>
78#include <linux/spinlock.h>
Oliver Neukum08a2b3b2007-05-24 13:52:51 +020079#include <linux/mutex.h>
Alan Cox80359a92008-07-22 11:09:16 +010080#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <asm/termbits.h>
82#include <linux/usb.h>
83#include <linux/serial_reg.h>
84#include <linux/serial.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070085#include <linux/usb/serial.h>
David Woodhouseec6752f2008-05-31 01:35:29 +030086#include <linux/firmware.h>
87#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include "whiteheat.h" /* WhiteHEAT specific commands */
89
90static int debug;
91
92#ifndef CMSPAR
93#define CMSPAR 0
94#endif
95
96/*
97 * Version Information
98 */
99#define DRIVER_VERSION "v2.0"
100#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
101#define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
102
103#define CONNECT_TECH_VENDOR_ID 0x0710
104#define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
105#define CONNECT_TECH_WHITE_HEAT_ID 0x8001
106
107/*
108 ID tables for whiteheat are unusual, because we want to different
109 things for different versions of the device. Eventually, this
110 will be doable from a single table. But, for now, we define two
111 separate ID tables, and then a third table that combines them
112 just for the purpose of exporting the autoloading information.
113*/
Németh Márton7d40d7e2010-01-10 15:34:24 +0100114static const struct usb_device_id id_table_std[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
116 { } /* Terminating entry */
117};
118
Németh Márton7d40d7e2010-01-10 15:34:24 +0100119static const struct usb_device_id id_table_prerenumeration[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
121 { } /* Terminating entry */
122};
123
Németh Márton7d40d7e2010-01-10 15:34:24 +0100124static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
126 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
127 { } /* Terminating entry */
128};
129
Alan Cox80359a92008-07-22 11:09:16 +0100130MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132static struct usb_driver whiteheat_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .name = "whiteheat",
134 .probe = usb_serial_probe,
135 .disconnect = usb_serial_disconnect,
136 .id_table = id_table_combined,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800137 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140/* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
Alan Cox80359a92008-07-22 11:09:16 +0100141static int whiteheat_firmware_download(struct usb_serial *serial,
142 const struct usb_device_id *id);
143static int whiteheat_firmware_attach(struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145/* function prototypes for the Connect Tech WhiteHEAT serial converter */
Alan Cox80359a92008-07-22 11:09:16 +0100146static int whiteheat_attach(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400147static void whiteheat_release(struct usb_serial *serial);
Alan Cox80359a92008-07-22 11:09:16 +0100148static int whiteheat_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -0700149 struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +0100150static void whiteheat_close(struct usb_serial_port *port);
Alan Cox80359a92008-07-22 11:09:16 +0100151static int whiteheat_write(struct tty_struct *tty,
152 struct usb_serial_port *port,
153 const unsigned char *buf, int count);
154static int whiteheat_write_room(struct tty_struct *tty);
155static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
156 unsigned int cmd, unsigned long arg);
157static void whiteheat_set_termios(struct tty_struct *tty,
158 struct usb_serial_port *port, struct ktermios *old);
159static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file);
160static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
161 unsigned int set, unsigned int clear);
162static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
163static int whiteheat_chars_in_buffer(struct tty_struct *tty);
164static void whiteheat_throttle(struct tty_struct *tty);
165static void whiteheat_unthrottle(struct tty_struct *tty);
166static void whiteheat_read_callback(struct urb *urb);
167static void whiteheat_write_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700169static struct usb_serial_driver whiteheat_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700170 .driver = {
171 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700172 .name = "whiteheatnofirm",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700173 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700174 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100175 .usb_driver = &whiteheat_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .id_table = id_table_prerenumeration,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .num_ports = 1,
178 .probe = whiteheat_firmware_download,
179 .attach = whiteheat_firmware_attach,
180};
181
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700182static struct usb_serial_driver whiteheat_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700183 .driver = {
184 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700185 .name = "whiteheat",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700186 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700187 .description = "Connect Tech - WhiteHEAT",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100188 .usb_driver = &whiteheat_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .id_table = id_table_std,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .num_ports = 4,
191 .attach = whiteheat_attach,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400192 .release = whiteheat_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .open = whiteheat_open,
194 .close = whiteheat_close,
195 .write = whiteheat_write,
196 .write_room = whiteheat_write_room,
197 .ioctl = whiteheat_ioctl,
198 .set_termios = whiteheat_set_termios,
199 .break_ctl = whiteheat_break_ctl,
200 .tiocmget = whiteheat_tiocmget,
201 .tiocmset = whiteheat_tiocmset,
202 .chars_in_buffer = whiteheat_chars_in_buffer,
203 .throttle = whiteheat_throttle,
204 .unthrottle = whiteheat_unthrottle,
205 .read_bulk_callback = whiteheat_read_callback,
206 .write_bulk_callback = whiteheat_write_callback,
207};
208
209
210struct whiteheat_command_private {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200211 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 __u8 port_running;
213 __u8 command_finished;
Alan Cox80359a92008-07-22 11:09:16 +0100214 wait_queue_head_t wait_command; /* for handling sleeping whilst
215 waiting for a command to
216 finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 __u8 result_buffer[64];
218};
219
220
221#define THROTTLED 0x01
222#define ACTUALLY_THROTTLED 0x02
223
224static int urb_pool_size = 8;
225
226struct whiteheat_urb_wrap {
227 struct list_head list;
228 struct urb *urb;
229};
230
231struct whiteheat_private {
232 spinlock_t lock;
233 __u8 flags;
Alan Coxa5b6f602008-04-08 17:16:06 +0100234 __u8 mcr; /* FIXME: no locking on mcr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct list_head rx_urbs_free;
236 struct list_head rx_urbs_submitted;
237 struct list_head rx_urb_q;
238 struct work_struct rx_work;
David Howellsc4028952006-11-22 14:57:56 +0000239 struct usb_serial_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct list_head tx_urbs_free;
241 struct list_head tx_urbs_submitted;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200242 struct mutex deathwarrant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
245
246/* local function prototypes */
247static int start_command_port(struct usb_serial *serial);
248static void stop_command_port(struct usb_serial *serial);
David Howells7d12e782006-10-05 14:55:46 +0100249static void command_port_write_callback(struct urb *urb);
250static void command_port_read_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252static int start_port_read(struct usb_serial_port *port);
Alan Cox80359a92008-07-22 11:09:16 +0100253static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
254 struct list_head *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static struct list_head *list_first(struct list_head *head);
David Howellsc4028952006-11-22 14:57:56 +0000256static void rx_data_softint(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Alan Cox80359a92008-07-22 11:09:16 +0100258static int firm_send_command(struct usb_serial_port *port, __u8 command,
259 __u8 *data, __u8 datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static int firm_open(struct usb_serial_port *port);
261static int firm_close(struct usb_serial_port *port);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700262static void firm_setup_port(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
264static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
265static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
266static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
267static int firm_get_dtr_rts(struct usb_serial_port *port);
268static int firm_report_tx_done(struct usb_serial_port *port);
269
270
271#define COMMAND_PORT 4
272#define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
273#define COMMAND_TIMEOUT_MS 2000
274#define CLOSING_DELAY (30 * HZ)
275
276
277/*****************************************************************************
278 * Connect Tech's White Heat prerenumeration driver functions
279 *****************************************************************************/
280
281/* steps to download the firmware to the WhiteHEAT device:
282 - hold the reset (by writing to the reset bit of the CPUCS register)
283 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
284 - release the reset (by writing to the CPUCS register)
285 - download the WH.HEX file for all addresses greater than 0x1b3f using
286 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
287 - hold the reset
288 - download the WH.HEX file for all addresses less than 0x1b40 using
289 VENDOR_REQUEST_ANCHOR_LOAD
290 - release the reset
291 - device renumerated itself and comes up as new device id with all
292 firmware download completed.
293*/
Alan Cox80359a92008-07-22 11:09:16 +0100294static int whiteheat_firmware_download(struct usb_serial *serial,
295 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
David Woodhouseec6752f2008-05-31 01:35:29 +0300297 int response, ret = -ENOENT;
298 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
299 const struct ihex_binrec *record;
300
Harvey Harrison441b62c2008-03-03 16:08:34 -0800301 dbg("%s", __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300302
303 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
304 &serial->dev->dev)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700305 dev_err(&serial->dev->dev,
306 "%s - request \"whiteheat.fw\" failed\n", __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300307 goto out;
308 }
309 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
310 &serial->dev->dev)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700311 dev_err(&serial->dev->dev,
312 "%s - request \"whiteheat_loader.fw\" failed\n",
313 __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300314 goto out;
315 }
316 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 response = ezusb_set_reset (serial, 1);
318
David Woodhouseec6752f2008-05-31 01:35:29 +0300319 record = (const struct ihex_binrec *)loader_fw->data;
320 while (record) {
321 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
322 (unsigned char *)record->data,
323 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700325 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
326 "failed for loader (%d %04X %p %d)\n",
327 __func__, response, be32_to_cpu(record->addr),
328 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
330 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300331 record = ihex_next_binrec(record);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333
Alan Cox80359a92008-07-22 11:09:16 +0100334 response = ezusb_set_reset(serial, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
David Woodhouseec6752f2008-05-31 01:35:29 +0300336 record = (const struct ihex_binrec *)firmware_fw->data;
337 while (record && be32_to_cpu(record->addr) < 0x1b40)
338 record = ihex_next_binrec(record);
339 while (record) {
340 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
341 (unsigned char *)record->data,
342 be16_to_cpu(record->len), 0xa3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700344 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
345 "failed for first firmware step "
346 "(%d %04X %p %d)\n", __func__, response,
347 be32_to_cpu(record->addr), record->data,
348 be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350 }
351 ++record;
352 }
Alan Cox80359a92008-07-22 11:09:16 +0100353
354 response = ezusb_set_reset(serial, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
David Woodhouseec6752f2008-05-31 01:35:29 +0300356 record = (const struct ihex_binrec *)firmware_fw->data;
357 while (record && be32_to_cpu(record->addr) < 0x1b40) {
358 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
359 (unsigned char *)record->data,
360 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700362 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
363 "failed for second firmware step "
364 "(%d %04X %p %d)\n", __func__, response,
365 be32_to_cpu(record->addr), record->data,
366 be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368 }
369 ++record;
370 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300371 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 response = ezusb_set_reset (serial, 0);
David Woodhouseec6752f2008-05-31 01:35:29 +0300373 out:
374 release_firmware(loader_fw);
375 release_firmware(firmware_fw);
376 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379
Alan Cox80359a92008-07-22 11:09:16 +0100380static int whiteheat_firmware_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 /* We want this device to fail to have a driver assigned to it */
383 return 1;
384}
385
386
387/*****************************************************************************
388 * Connect Tech's White Heat serial driver functions
389 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +0100390static int whiteheat_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct usb_serial_port *command_port;
393 struct whiteheat_command_private *command_info;
394 struct usb_serial_port *port;
395 struct whiteheat_private *info;
396 struct whiteheat_hw_info *hw_info;
397 int pipe;
398 int ret;
399 int alen;
400 __u8 *command;
401 __u8 *result;
402 int i;
403 int j;
404 struct urb *urb;
405 int buf_size;
406 struct whiteheat_urb_wrap *wrap;
407 struct list_head *tmp;
408
409 command_port = serial->port[COMMAND_PORT];
410
Alan Cox80359a92008-07-22 11:09:16 +0100411 pipe = usb_sndbulkpipe(serial->dev,
412 command_port->bulk_out_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 command = kmalloc(2, GFP_KERNEL);
414 if (!command)
415 goto no_command_buffer;
416 command[0] = WHITEHEAT_GET_HW_INFO;
417 command[1] = 0;
Alan Cox80359a92008-07-22 11:09:16 +0100418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
420 if (!result)
421 goto no_result_buffer;
422 /*
423 * When the module is reloaded the firmware is still there and
424 * the endpoints are still in the usb core unchanged. This is the
Alan Cox80359a92008-07-22 11:09:16 +0100425 * unlinking bug in disguise. Same for the call below.
426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100428 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
429 &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (ret) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700431 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
432 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400434 } else if (alen != 2) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700435 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
436 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 goto no_firmware;
438 }
439
Alan Cox80359a92008-07-22 11:09:16 +0100440 pipe = usb_rcvbulkpipe(serial->dev,
441 command_port->bulk_in_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /* See the comment on the usb_clear_halt() above */
443 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100444 ret = usb_bulk_msg(serial->dev, pipe, result,
445 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (ret) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700447 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
448 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400450 } else if (alen != sizeof(*hw_info) + 1) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700451 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
452 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto no_firmware;
454 } else if (result[0] != command[0]) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700455 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
456 serial->type->description, result[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto no_firmware;
458 }
459
460 hw_info = (struct whiteheat_hw_info *)&result[1];
461
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700462 dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
463 serial->type->description, DRIVER_VERSION,
464 hw_info->sw_major_rev, hw_info->sw_minor_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 for (i = 0; i < serial->num_ports; i++) {
467 port = serial->port[i];
468
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800469 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (info == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700471 dev_err(&port->dev,
472 "%s: Out of memory for port structures\n",
473 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto no_private;
475 }
476
477 spin_lock_init(&info->lock);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200478 mutex_init(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 info->flags = 0;
480 info->mcr = 0;
David Howellsc4028952006-11-22 14:57:56 +0000481 INIT_WORK(&info->rx_work, rx_data_softint);
482 info->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 INIT_LIST_HEAD(&info->rx_urbs_free);
485 INIT_LIST_HEAD(&info->rx_urbs_submitted);
486 INIT_LIST_HEAD(&info->rx_urb_q);
487 INIT_LIST_HEAD(&info->tx_urbs_free);
488 INIT_LIST_HEAD(&info->tx_urbs_submitted);
489
490 for (j = 0; j < urb_pool_size; j++) {
491 urb = usb_alloc_urb(0, GFP_KERNEL);
492 if (!urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700493 dev_err(&port->dev, "No free urbs available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 goto no_rx_urb;
495 }
496 buf_size = port->read_urb->transfer_buffer_length;
497 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
498 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700499 dev_err(&port->dev,
500 "Couldn't allocate urb buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 goto no_rx_buf;
502 }
503 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
504 if (!wrap) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700505 dev_err(&port->dev,
506 "Couldn't allocate urb wrapper\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 goto no_rx_wrap;
508 }
509 usb_fill_bulk_urb(urb, serial->dev,
510 usb_rcvbulkpipe(serial->dev,
511 port->bulk_in_endpointAddress),
512 urb->transfer_buffer, buf_size,
513 whiteheat_read_callback, port);
514 wrap->urb = urb;
515 list_add(&wrap->list, &info->rx_urbs_free);
516
517 urb = usb_alloc_urb(0, GFP_KERNEL);
518 if (!urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700519 dev_err(&port->dev, "No free urbs available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 goto no_tx_urb;
521 }
522 buf_size = port->write_urb->transfer_buffer_length;
523 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
524 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700525 dev_err(&port->dev,
526 "Couldn't allocate urb buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto no_tx_buf;
528 }
529 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
530 if (!wrap) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700531 dev_err(&port->dev,
532 "Couldn't allocate urb wrapper\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto no_tx_wrap;
534 }
535 usb_fill_bulk_urb(urb, serial->dev,
536 usb_sndbulkpipe(serial->dev,
537 port->bulk_out_endpointAddress),
538 urb->transfer_buffer, buf_size,
539 whiteheat_write_callback, port);
540 wrap->urb = urb;
541 list_add(&wrap->list, &info->tx_urbs_free);
542 }
543
544 usb_set_serial_port_data(port, info);
545 }
546
Alan Cox80359a92008-07-22 11:09:16 +0100547 command_info = kmalloc(sizeof(struct whiteheat_command_private),
548 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (command_info == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700550 dev_err(&serial->dev->dev,
551 "%s: Out of memory for port structures\n",
552 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto no_command_private;
554 }
555
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200556 mutex_init(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 command_info->port_running = 0;
558 init_waitqueue_head(&command_info->wait_command);
559 usb_set_serial_port_data(command_port, command_info);
560 command_port->write_urb->complete = command_port_write_callback;
561 command_port->read_urb->complete = command_port_read_callback;
562 kfree(result);
563 kfree(command);
564
565 return 0;
566
567no_firmware:
568 /* Firmware likely not running */
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700569 dev_err(&serial->dev->dev,
570 "%s: Unable to retrieve firmware version, try replugging\n",
571 serial->type->description);
572 dev_err(&serial->dev->dev,
573 "%s: If the firmware is not running (status led not blinking)\n",
574 serial->type->description);
575 dev_err(&serial->dev->dev,
576 "%s: please contact support@connecttech.com\n",
577 serial->type->description);
Jesper Juhl67ca0282006-04-23 19:59:23 +0200578 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -ENODEV;
580
581no_command_private:
582 for (i = serial->num_ports - 1; i >= 0; i--) {
583 port = serial->port[i];
584 info = usb_get_serial_port_data(port);
585 for (j = urb_pool_size - 1; j >= 0; j--) {
586 tmp = list_first(&info->tx_urbs_free);
587 list_del(tmp);
588 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
589 urb = wrap->urb;
590 kfree(wrap);
591no_tx_wrap:
592 kfree(urb->transfer_buffer);
593no_tx_buf:
594 usb_free_urb(urb);
595no_tx_urb:
596 tmp = list_first(&info->rx_urbs_free);
597 list_del(tmp);
598 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
599 urb = wrap->urb;
600 kfree(wrap);
601no_rx_wrap:
602 kfree(urb->transfer_buffer);
603no_rx_buf:
604 usb_free_urb(urb);
605no_rx_urb:
606 ;
607 }
608 kfree(info);
609no_private:
610 ;
611 }
612 kfree(result);
613no_result_buffer:
614 kfree(command);
615no_command_buffer:
616 return -ENOMEM;
617}
618
619
Alan Sternf9c99bb2009-06-02 11:53:55 -0400620static void whiteheat_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 struct usb_serial_port *command_port;
623 struct usb_serial_port *port;
624 struct whiteheat_private *info;
625 struct whiteheat_urb_wrap *wrap;
626 struct urb *urb;
627 struct list_head *tmp;
628 struct list_head *tmp2;
629 int i;
630
Harvey Harrison441b62c2008-03-03 16:08:34 -0800631 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* free up our private data for our command port */
634 command_port = serial->port[COMMAND_PORT];
Alan Cox80359a92008-07-22 11:09:16 +0100635 kfree(usb_get_serial_port_data(command_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 for (i = 0; i < serial->num_ports; i++) {
638 port = serial->port[i];
639 info = usb_get_serial_port_data(port);
640 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
641 list_del(tmp);
642 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
643 urb = wrap->urb;
644 kfree(wrap);
645 kfree(urb->transfer_buffer);
646 usb_free_urb(urb);
647 }
648 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
649 list_del(tmp);
650 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
651 urb = wrap->urb;
652 kfree(wrap);
653 kfree(urb->transfer_buffer);
654 usb_free_urb(urb);
655 }
656 kfree(info);
657 }
658
659 return;
660}
661
Alan Coxa509a7e2009-09-19 13:13:26 -0700662static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Harvey Harrison441b62c2008-03-03 16:08:34 -0800666 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 retval = start_command_port(port->serial);
669 if (retval)
670 goto exit;
671
Alan Cox95da3102008-07-22 11:09:07 +0100672 if (tty)
673 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 /* send an open port command */
676 retval = firm_open(port);
677 if (retval) {
678 stop_command_port(port->serial);
679 goto exit;
680 }
681
682 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
683 if (retval) {
684 firm_close(port);
685 stop_command_port(port->serial);
686 goto exit;
687 }
688
Alan Cox72e27412008-07-22 11:09:29 +0100689 if (tty)
690 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* Work around HCD bugs */
693 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
694 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
695
696 /* Start reading from the device */
697 retval = start_port_read(port);
698 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700699 dev_err(&port->dev,
700 "%s - failed submitting read urb, error %d\n",
701 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 firm_close(port);
703 stop_command_port(port->serial);
704 goto exit;
705 }
706
707exit:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800708 dbg("%s - exit, retval = %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return retval;
710}
711
712
Alan Cox335f8512009-06-11 12:26:29 +0100713static void whiteheat_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct whiteheat_private *info = usb_get_serial_port_data(port);
716 struct whiteheat_urb_wrap *wrap;
717 struct urb *urb;
718 struct list_head *tmp;
719 struct list_head *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Harvey Harrison441b62c2008-03-03 16:08:34 -0800721 dbg("%s - port %d", __func__, port->number);
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 firm_report_tx_done(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 firm_close(port);
725
726 /* shutdown our bulk reads and writes */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200727 mutex_lock(&info->deathwarrant);
728 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
730 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
731 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200732 list_del(tmp);
733 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200735 spin_lock_irq(&info->lock);
736 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Akinobu Mita179e0912006-06-26 00:24:41 -0700738 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
739 list_move(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
741 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
742 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200743 list_del(tmp);
744 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200746 spin_lock_irq(&info->lock);
747 list_add(tmp, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200749 spin_unlock_irq(&info->lock);
750 mutex_unlock(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 stop_command_port(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754
Alan Cox95da3102008-07-22 11:09:07 +0100755static int whiteheat_write(struct tty_struct *tty,
756 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct usb_serial *serial = port->serial;
759 struct whiteheat_private *info = usb_get_serial_port_data(port);
760 struct whiteheat_urb_wrap *wrap;
761 struct urb *urb;
762 int result;
763 int bytes;
764 int sent = 0;
765 unsigned long flags;
766 struct list_head *tmp;
767
Harvey Harrison441b62c2008-03-03 16:08:34 -0800768 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800771 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return (0);
773 }
774
775 while (count) {
776 spin_lock_irqsave(&info->lock, flags);
777 if (list_empty(&info->tx_urbs_free)) {
778 spin_unlock_irqrestore(&info->lock, flags);
779 break;
780 }
781 tmp = list_first(&info->tx_urbs_free);
782 list_del(tmp);
783 spin_unlock_irqrestore(&info->lock, flags);
784
785 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
786 urb = wrap->urb;
Alan Cox80359a92008-07-22 11:09:16 +0100787 bytes = (count > port->bulk_out_size) ?
788 port->bulk_out_size : count;
789 memcpy(urb->transfer_buffer, buf + sent, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Alan Cox80359a92008-07-22 11:09:16 +0100791 usb_serial_debug_data(debug, &port->dev,
792 __func__, bytes, urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 urb->dev = serial->dev;
795 urb->transfer_buffer_length = bytes;
796 result = usb_submit_urb(urb, GFP_ATOMIC);
797 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700798 dev_err(&port->dev,
799 "%s - failed submitting write urb, error %d\n",
800 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 sent = result;
802 spin_lock_irqsave(&info->lock, flags);
803 list_add(tmp, &info->tx_urbs_free);
804 spin_unlock_irqrestore(&info->lock, flags);
805 break;
806 } else {
807 sent += bytes;
808 count -= bytes;
809 spin_lock_irqsave(&info->lock, flags);
810 list_add(tmp, &info->tx_urbs_submitted);
811 spin_unlock_irqrestore(&info->lock, flags);
812 }
813 }
814
815 return sent;
816}
817
Alan Cox95da3102008-07-22 11:09:07 +0100818static int whiteheat_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Alan Cox95da3102008-07-22 11:09:07 +0100820 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 struct whiteheat_private *info = usb_get_serial_port_data(port);
822 struct list_head *tmp;
823 int room = 0;
824 unsigned long flags;
825
Harvey Harrison441b62c2008-03-03 16:08:34 -0800826 dbg("%s - port %d", __func__, port->number);
Alan Cox80359a92008-07-22 11:09:16 +0100827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 spin_lock_irqsave(&info->lock, flags);
829 list_for_each(tmp, &info->tx_urbs_free)
830 room++;
831 spin_unlock_irqrestore(&info->lock, flags);
832 room *= port->bulk_out_size;
833
Harvey Harrison441b62c2008-03-03 16:08:34 -0800834 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return (room);
836}
837
Alan Cox80359a92008-07-22 11:09:16 +0100838static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Alan Cox95da3102008-07-22 11:09:07 +0100840 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct whiteheat_private *info = usb_get_serial_port_data(port);
842 unsigned int modem_signals = 0;
843
Harvey Harrison441b62c2008-03-03 16:08:34 -0800844 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 firm_get_dtr_rts(port);
847 if (info->mcr & UART_MCR_DTR)
848 modem_signals |= TIOCM_DTR;
849 if (info->mcr & UART_MCR_RTS)
850 modem_signals |= TIOCM_RTS;
851
852 return modem_signals;
853}
854
Alan Cox80359a92008-07-22 11:09:16 +0100855static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 unsigned int set, unsigned int clear)
857{
Alan Cox95da3102008-07-22 11:09:07 +0100858 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct whiteheat_private *info = usb_get_serial_port_data(port);
860
Harvey Harrison441b62c2008-03-03 16:08:34 -0800861 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (set & TIOCM_RTS)
864 info->mcr |= UART_MCR_RTS;
865 if (set & TIOCM_DTR)
866 info->mcr |= UART_MCR_DTR;
867
868 if (clear & TIOCM_RTS)
869 info->mcr &= ~UART_MCR_RTS;
870 if (clear & TIOCM_DTR)
871 info->mcr &= ~UART_MCR_DTR;
872
873 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
874 firm_set_rts(port, info->mcr & UART_MCR_RTS);
875 return 0;
876}
877
878
Alan Cox80359a92008-07-22 11:09:16 +0100879static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
880 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Alan Cox95da3102008-07-22 11:09:07 +0100882 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct serial_struct serstruct;
884 void __user *user_arg = (void __user *)arg;
885
Harvey Harrison441b62c2008-03-03 16:08:34 -0800886 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 switch (cmd) {
Alan Cox80359a92008-07-22 11:09:16 +0100889 case TIOCGSERIAL:
890 memset(&serstruct, 0, sizeof(serstruct));
891 serstruct.type = PORT_16654;
892 serstruct.line = port->serial->minor;
893 serstruct.port = port->number;
894 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
895 serstruct.xmit_fifo_size = port->bulk_out_size;
896 serstruct.custom_divisor = 0;
897 serstruct.baud_base = 460800;
898 serstruct.close_delay = CLOSING_DELAY;
899 serstruct.closing_wait = CLOSING_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Alan Cox80359a92008-07-22 11:09:16 +0100901 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
902 return -EFAULT;
903 break;
904 default:
905 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908 return -ENOIOCTLCMD;
909}
910
911
Alan Cox80359a92008-07-22 11:09:16 +0100912static void whiteheat_set_termios(struct tty_struct *tty,
913 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Alan Cox95da3102008-07-22 11:09:07 +0100915 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Alan Cox80359a92008-07-22 11:09:16 +0100918static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
919{
Alan Cox95da3102008-07-22 11:09:07 +0100920 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 firm_set_break(port, break_state);
922}
923
924
Alan Cox95da3102008-07-22 11:09:07 +0100925static int whiteheat_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Alan Cox95da3102008-07-22 11:09:07 +0100927 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct whiteheat_private *info = usb_get_serial_port_data(port);
929 struct list_head *tmp;
930 struct whiteheat_urb_wrap *wrap;
931 int chars = 0;
932 unsigned long flags;
933
Harvey Harrison441b62c2008-03-03 16:08:34 -0800934 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 spin_lock_irqsave(&info->lock, flags);
937 list_for_each(tmp, &info->tx_urbs_submitted) {
938 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
939 chars += wrap->urb->transfer_buffer_length;
940 }
941 spin_unlock_irqrestore(&info->lock, flags);
942
Alan Cox80359a92008-07-22 11:09:16 +0100943 dbg("%s - returns %d", __func__, chars);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200944 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947
Alan Cox80359a92008-07-22 11:09:16 +0100948static void whiteheat_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Alan Cox95da3102008-07-22 11:09:07 +0100950 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct whiteheat_private *info = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Harvey Harrison441b62c2008-03-03 16:08:34 -0800953 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Oliver Neukum63832512009-10-07 10:50:23 +0200955 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 info->flags |= THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +0200957 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 return;
960}
961
962
Alan Cox80359a92008-07-22 11:09:16 +0100963static void whiteheat_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Alan Cox95da3102008-07-22 11:09:07 +0100965 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct whiteheat_private *info = usb_get_serial_port_data(port);
967 int actually_throttled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Harvey Harrison441b62c2008-03-03 16:08:34 -0800969 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Oliver Neukum63832512009-10-07 10:50:23 +0200971 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 actually_throttled = info->flags & ACTUALLY_THROTTLED;
973 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
Oliver Neukum63832512009-10-07 10:50:23 +0200974 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (actually_throttled)
David Howellsc4028952006-11-22 14:57:56 +0000977 rx_data_softint(&info->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 return;
980}
981
982
983/*****************************************************************************
984 * Connect Tech's White Heat callback routines
985 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200986static void command_port_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700988 int status = urb->status;
989
Harvey Harrison441b62c2008-03-03 16:08:34 -0800990 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700992 if (status) {
993 dbg("nonzero urb status: %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return;
995 }
996}
997
998
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200999static void command_port_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Ming Leicdc97792008-02-24 18:41:47 +08001001 struct usb_serial_port *command_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct whiteheat_command_private *command_info;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001003 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 unsigned char *data = urb->transfer_buffer;
1005 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Harvey Harrison441b62c2008-03-03 16:08:34 -08001007 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 command_info = usb_get_serial_port_data(command_port);
1010 if (!command_info) {
Alan Cox80359a92008-07-22 11:09:16 +01001011 dbg("%s - command_info is NULL, exiting.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return;
1013 }
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001014 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001015 dbg("%s - nonzero urb status: %d", __func__, status);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001016 if (status != -ENOENT)
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001017 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1018 wake_up(&command_info->wait_command);
1019 return;
1020 }
1021
Alan Cox80359a92008-07-22 11:09:16 +01001022 usb_serial_debug_data(debug, &command_port->dev,
1023 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1026 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001027 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1029 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001030 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 } else if (data[0] == WHITEHEAT_EVENT) {
Alan Cox80359a92008-07-22 11:09:16 +01001032 /* These are unsolicited reports from the firmware, hence no
1033 waiting command to wakeup */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001034 dbg("%s - event received", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
Alan Cox80359a92008-07-22 11:09:16 +01001036 memcpy(command_info->result_buffer, &data[1],
1037 urb->actual_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001039 wake_up(&command_info->wait_command);
Alan Cox80359a92008-07-22 11:09:16 +01001040 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -08001041 dbg("%s - bad reply from firmware", __func__);
Alan Cox80359a92008-07-22 11:09:16 +01001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /* Continue trying to always read */
1044 command_port->read_urb->dev = command_port->serial->dev;
1045 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (result)
Alan Cox80359a92008-07-22 11:09:16 +01001047 dbg("%s - failed resubmitting read urb, error %d",
1048 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051
David Howells7d12e782006-10-05 14:55:46 +01001052static void whiteheat_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Ming Leicdc97792008-02-24 18:41:47 +08001054 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 struct whiteheat_urb_wrap *wrap;
1056 unsigned char *data = urb->transfer_buffer;
1057 struct whiteheat_private *info = usb_get_serial_port_data(port);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001058 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Harvey Harrison441b62c2008-03-03 16:08:34 -08001060 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 spin_lock(&info->lock);
1063 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1064 if (!wrap) {
1065 spin_unlock(&info->lock);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001066 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return;
1068 }
1069 list_del(&wrap->list);
1070 spin_unlock(&info->lock);
1071
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001072 if (status) {
1073 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001074 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 spin_lock(&info->lock);
1076 list_add(&wrap->list, &info->rx_urbs_free);
1077 spin_unlock(&info->lock);
1078 return;
1079 }
1080
Alan Cox80359a92008-07-22 11:09:16 +01001081 usb_serial_debug_data(debug, &port->dev,
1082 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 spin_lock(&info->lock);
1085 list_add_tail(&wrap->list, &info->rx_urb_q);
1086 if (info->flags & THROTTLED) {
1087 info->flags |= ACTUALLY_THROTTLED;
1088 spin_unlock(&info->lock);
1089 return;
1090 }
1091 spin_unlock(&info->lock);
1092
1093 schedule_work(&info->rx_work);
1094}
1095
1096
David Howells7d12e782006-10-05 14:55:46 +01001097static void whiteheat_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Ming Leicdc97792008-02-24 18:41:47 +08001099 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct whiteheat_private *info = usb_get_serial_port_data(port);
1101 struct whiteheat_urb_wrap *wrap;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001102 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Harvey Harrison441b62c2008-03-03 16:08:34 -08001104 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 spin_lock(&info->lock);
1107 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1108 if (!wrap) {
1109 spin_unlock(&info->lock);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001110 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return;
1112 }
Akinobu Mita179e0912006-06-26 00:24:41 -07001113 list_move(&wrap->list, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 spin_unlock(&info->lock);
1115
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001116 if (status) {
1117 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001118 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return;
1120 }
1121
Pete Zaitcevcf2c7482006-05-22 21:58:49 -07001122 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125
1126/*****************************************************************************
1127 * Connect Tech's White Heat firmware interface
1128 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +01001129static int firm_send_command(struct usb_serial_port *port, __u8 command,
1130 __u8 *data, __u8 datasize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct usb_serial_port *command_port;
1133 struct whiteheat_command_private *command_info;
1134 struct whiteheat_private *info;
1135 __u8 *transfer_buffer;
1136 int retval = 0;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001137 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Harvey Harrison441b62c2008-03-03 16:08:34 -08001139 dbg("%s - command %d", __func__, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 command_port = port->serial->port[COMMAND_PORT];
1142 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001143 mutex_lock(&command_info->mutex);
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001144 command_info->command_finished = false;
Alan Cox80359a92008-07-22 11:09:16 +01001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1147 transfer_buffer[0] = command;
Alan Cox80359a92008-07-22 11:09:16 +01001148 memcpy(&transfer_buffer[1], data, datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 command_port->write_urb->transfer_buffer_length = datasize + 1;
1150 command_port->write_urb->dev = port->serial->dev;
Alan Cox80359a92008-07-22 11:09:16 +01001151 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001153 dbg("%s - submit urb failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 goto exit;
1155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /* wait for the command to complete */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001158 t = wait_event_timeout(command_info->wait_command,
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001159 (bool)command_info->command_finished, COMMAND_TIMEOUT);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001160 if (!t)
1161 usb_kill_urb(command_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001163 if (command_info->command_finished == false) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001164 dbg("%s - command timed out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 retval = -ETIMEDOUT;
1166 goto exit;
1167 }
1168
1169 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001170 dbg("%s - command failed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 retval = -EIO;
1172 goto exit;
1173 }
1174
1175 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001176 dbg("%s - command completed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 switch (command) {
Alan Cox80359a92008-07-22 11:09:16 +01001178 case WHITEHEAT_GET_DTR_RTS:
1179 info = usb_get_serial_port_data(port);
1180 memcpy(&info->mcr, command_info->result_buffer,
1181 sizeof(struct whiteheat_dr_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
1183 }
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001186 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return retval;
1188}
1189
1190
Alan Cox80359a92008-07-22 11:09:16 +01001191static int firm_open(struct usb_serial_port *port)
1192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 struct whiteheat_simple open_command;
1194
1195 open_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001196 return firm_send_command(port, WHITEHEAT_OPEN,
1197 (__u8 *)&open_command, sizeof(open_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
1200
Alan Cox80359a92008-07-22 11:09:16 +01001201static int firm_close(struct usb_serial_port *port)
1202{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct whiteheat_simple close_command;
1204
1205 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001206 return firm_send_command(port, WHITEHEAT_CLOSE,
1207 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001211static void firm_setup_port(struct tty_struct *tty)
Alan Cox80359a92008-07-22 11:09:16 +01001212{
Alan Cox95da3102008-07-22 11:09:07 +01001213 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 struct whiteheat_port_settings port_settings;
Alan Cox95da3102008-07-22 11:09:07 +01001215 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 port_settings.port = port->number + 1;
1218
1219 /* get the byte size */
1220 switch (cflag & CSIZE) {
Alan Cox80359a92008-07-22 11:09:16 +01001221 case CS5: port_settings.bits = 5; break;
1222 case CS6: port_settings.bits = 6; break;
1223 case CS7: port_settings.bits = 7; break;
1224 default:
1225 case CS8: port_settings.bits = 8; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001227 dbg("%s - data bits = %d", __func__, port_settings.bits);
Alan Cox80359a92008-07-22 11:09:16 +01001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /* determine the parity */
1230 if (cflag & PARENB)
1231 if (cflag & CMSPAR)
1232 if (cflag & PARODD)
1233 port_settings.parity = WHITEHEAT_PAR_MARK;
1234 else
1235 port_settings.parity = WHITEHEAT_PAR_SPACE;
1236 else
1237 if (cflag & PARODD)
1238 port_settings.parity = WHITEHEAT_PAR_ODD;
1239 else
1240 port_settings.parity = WHITEHEAT_PAR_EVEN;
1241 else
1242 port_settings.parity = WHITEHEAT_PAR_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001243 dbg("%s - parity = %c", __func__, port_settings.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /* figure out the stop bits requested */
1246 if (cflag & CSTOPB)
1247 port_settings.stop = 2;
1248 else
1249 port_settings.stop = 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001250 dbg("%s - stop bits = %d", __func__, port_settings.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /* figure out the flow control settings */
1253 if (cflag & CRTSCTS)
Alan Cox80359a92008-07-22 11:09:16 +01001254 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1255 WHITEHEAT_HFLOW_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 else
1257 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001258 dbg("%s - hardware flow control = %s %s %s %s", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1260 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1261 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1262 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
Alan Cox80359a92008-07-22 11:09:16 +01001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /* determine software flow control */
Alan Cox95da3102008-07-22 11:09:07 +01001265 if (I_IXOFF(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1267 else
1268 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001269 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
Alan Cox80359a92008-07-22 11:09:16 +01001270
Alan Cox95da3102008-07-22 11:09:07 +01001271 port_settings.xon = START_CHAR(tty);
1272 port_settings.xoff = STOP_CHAR(tty);
Alan Cox80359a92008-07-22 11:09:16 +01001273 dbg("%s - XON = %2x, XOFF = %2x",
1274 __func__, port_settings.xon, port_settings.xoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* get the baud rate wanted */
Alan Cox95da3102008-07-22 11:09:07 +01001277 port_settings.baud = tty_get_baud_rate(tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001278 dbg("%s - baud rate = %d", __func__, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Alan Cox01d1df22007-10-18 01:24:23 -07001280 /* fixme: should set validated settings */
Alan Cox95da3102008-07-22 11:09:07 +01001281 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /* handle any settings that aren't specified in the tty structure */
1283 port_settings.lloop = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 /* now send the message to the device */
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001286 firm_send_command(port, WHITEHEAT_SETUP_PORT,
Alan Cox80359a92008-07-22 11:09:16 +01001287 (__u8 *)&port_settings, sizeof(port_settings));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288}
1289
1290
Alan Cox80359a92008-07-22 11:09:16 +01001291static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct whiteheat_set_rdb rts_command;
1294
1295 rts_command.port = port->number - port->serial->minor + 1;
1296 rts_command.state = onoff;
Alan Cox80359a92008-07-22 11:09:16 +01001297 return firm_send_command(port, WHITEHEAT_SET_RTS,
1298 (__u8 *)&rts_command, sizeof(rts_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301
Alan Cox80359a92008-07-22 11:09:16 +01001302static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 struct whiteheat_set_rdb dtr_command;
1305
1306 dtr_command.port = port->number - port->serial->minor + 1;
1307 dtr_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001308 return firm_send_command(port, WHITEHEAT_SET_DTR,
Alan Cox80359a92008-07-22 11:09:16 +01001309 (__u8 *)&dtr_command, sizeof(dtr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312
Alan Cox80359a92008-07-22 11:09:16 +01001313static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct whiteheat_set_rdb break_command;
1316
1317 break_command.port = port->number - port->serial->minor + 1;
1318 break_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001319 return firm_send_command(port, WHITEHEAT_SET_BREAK,
Alan Cox80359a92008-07-22 11:09:16 +01001320 (__u8 *)&break_command, sizeof(break_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
1323
Alan Cox80359a92008-07-22 11:09:16 +01001324static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct whiteheat_purge purge_command;
1327
1328 purge_command.port = port->number - port->serial->minor + 1;
1329 purge_command.what = rxtx;
Alan Cox80359a92008-07-22 11:09:16 +01001330 return firm_send_command(port, WHITEHEAT_PURGE,
1331 (__u8 *)&purge_command, sizeof(purge_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
1334
Alan Cox80359a92008-07-22 11:09:16 +01001335static int firm_get_dtr_rts(struct usb_serial_port *port)
1336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 struct whiteheat_simple get_dr_command;
1338
1339 get_dr_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001340 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1341 (__u8 *)&get_dr_command, sizeof(get_dr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
1344
Alan Cox80359a92008-07-22 11:09:16 +01001345static int firm_report_tx_done(struct usb_serial_port *port)
1346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct whiteheat_simple close_command;
1348
1349 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001350 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1351 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354
1355/*****************************************************************************
1356 * Connect Tech's White Heat utility functions
1357 *****************************************************************************/
1358static int start_command_port(struct usb_serial *serial)
1359{
1360 struct usb_serial_port *command_port;
1361 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 int retval = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 command_port = serial->port[COMMAND_PORT];
1365 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001366 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (!command_info->port_running) {
1368 /* Work around HCD bugs */
1369 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1370
1371 command_port->read_urb->dev = serial->dev;
1372 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1373 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001374 dev_err(&serial->dev->dev,
1375 "%s - failed submitting read urb, error %d\n",
1376 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 goto exit;
1378 }
1379 }
1380 command_info->port_running++;
1381
1382exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001383 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return retval;
1385}
1386
1387
1388static void stop_command_port(struct usb_serial *serial)
1389{
1390 struct usb_serial_port *command_port;
1391 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 command_port = serial->port[COMMAND_PORT];
1394 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001395 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 command_info->port_running--;
1397 if (!command_info->port_running)
1398 usb_kill_urb(command_port->read_urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001399 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402
1403static int start_port_read(struct usb_serial_port *port)
1404{
1405 struct whiteheat_private *info = usb_get_serial_port_data(port);
1406 struct whiteheat_urb_wrap *wrap;
1407 struct urb *urb;
1408 int retval = 0;
1409 unsigned long flags;
1410 struct list_head *tmp;
1411 struct list_head *tmp2;
1412
1413 spin_lock_irqsave(&info->lock, flags);
1414
1415 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1416 list_del(tmp);
1417 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1418 urb = wrap->urb;
1419 urb->dev = port->serial->dev;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001420 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 retval = usb_submit_urb(urb, GFP_KERNEL);
1422 if (retval) {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001423 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 list_add(tmp, &info->rx_urbs_free);
1425 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1426 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1427 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001428 list_del(tmp);
1429 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001431 spin_lock_irqsave(&info->lock, flags);
1432 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434 break;
1435 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001436 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 list_add(tmp, &info->rx_urbs_submitted);
1438 }
1439
1440 spin_unlock_irqrestore(&info->lock, flags);
1441
1442 return retval;
1443}
1444
1445
Alan Cox80359a92008-07-22 11:09:16 +01001446static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1447 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
1449 struct whiteheat_urb_wrap *wrap;
1450 struct list_head *tmp;
1451
1452 list_for_each(tmp, head) {
1453 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1454 if (wrap->urb == urb)
1455 return wrap;
1456 }
1457
1458 return NULL;
1459}
1460
1461
1462static struct list_head *list_first(struct list_head *head)
1463{
1464 return head->next;
1465}
1466
1467
David Howellsc4028952006-11-22 14:57:56 +00001468static void rx_data_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
David Howellsc4028952006-11-22 14:57:56 +00001470 struct whiteheat_private *info =
1471 container_of(work, struct whiteheat_private, rx_work);
1472 struct usb_serial_port *port = info->port;
Alan Cox4a90f092008-10-13 10:39:46 +01001473 struct tty_struct *tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 struct whiteheat_urb_wrap *wrap;
1475 struct urb *urb;
1476 unsigned long flags;
1477 struct list_head *tmp;
1478 struct list_head *tmp2;
1479 int result;
1480 int sent = 0;
1481
1482 spin_lock_irqsave(&info->lock, flags);
1483 if (info->flags & THROTTLED) {
1484 spin_unlock_irqrestore(&info->lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +01001485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487
1488 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1489 list_del(tmp);
1490 spin_unlock_irqrestore(&info->lock, flags);
1491
1492 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1493 urb = wrap->urb;
1494
1495 if (tty && urb->actual_length) {
Alan Cox80359a92008-07-22 11:09:16 +01001496 int len = tty_buffer_request_room(tty,
1497 urb->actual_length);
Alan Cox33f0f882006-01-09 20:54:13 -08001498 /* This stuff can go away now I suspect */
1499 if (unlikely(len < urb->actual_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 spin_lock_irqsave(&info->lock, flags);
1501 list_add(tmp, &info->rx_urb_q);
1502 spin_unlock_irqrestore(&info->lock, flags);
1503 tty_flip_buffer_push(tty);
1504 schedule_work(&info->rx_work);
Alan Cox4a90f092008-10-13 10:39:46 +01001505 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
Alan Cox33f0f882006-01-09 20:54:13 -08001507 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1508 sent += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510
1511 urb->dev = port->serial->dev;
1512 result = usb_submit_urb(urb, GFP_ATOMIC);
1513 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001514 dev_err(&port->dev,
1515 "%s - failed resubmitting read urb, error %d\n",
Alan Cox80359a92008-07-22 11:09:16 +01001516 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 spin_lock_irqsave(&info->lock, flags);
1518 list_add(tmp, &info->rx_urbs_free);
1519 continue;
1520 }
1521
1522 spin_lock_irqsave(&info->lock, flags);
1523 list_add(tmp, &info->rx_urbs_submitted);
1524 }
1525 spin_unlock_irqrestore(&info->lock, flags);
1526
1527 if (sent)
1528 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001529out:
1530 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533
1534/*****************************************************************************
1535 * Connect Tech's White Heat module functions
1536 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +01001537static int __init whiteheat_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
1539 int retval;
1540 retval = usb_serial_register(&whiteheat_fake_device);
1541 if (retval)
1542 goto failed_fake_register;
1543 retval = usb_serial_register(&whiteheat_device);
1544 if (retval)
1545 goto failed_device_register;
1546 retval = usb_register(&whiteheat_driver);
1547 if (retval)
1548 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001549 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1550 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return 0;
1552failed_usb_register:
1553 usb_serial_deregister(&whiteheat_device);
1554failed_device_register:
1555 usb_serial_deregister(&whiteheat_fake_device);
1556failed_fake_register:
1557 return retval;
1558}
1559
1560
Alan Cox80359a92008-07-22 11:09:16 +01001561static void __exit whiteheat_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Alan Cox80359a92008-07-22 11:09:16 +01001563 usb_deregister(&whiteheat_driver);
1564 usb_serial_deregister(&whiteheat_fake_device);
1565 usb_serial_deregister(&whiteheat_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568
1569module_init(whiteheat_init);
1570module_exit(whiteheat_exit);
1571
Alan Cox80359a92008-07-22 11:09:16 +01001572MODULE_AUTHOR(DRIVER_AUTHOR);
1573MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574MODULE_LICENSE("GPL");
1575
David Woodhouseec6752f2008-05-31 01:35:29 +03001576MODULE_FIRMWARE("whiteheat.fw");
1577MODULE_FIRMWARE("whiteheat_loader.fw");
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579module_param(urb_pool_size, int, 0);
1580MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1581
1582module_param(debug, bool, S_IRUGO | S_IWUSR);
1583MODULE_PARM_DESC(debug, "Debug enabled or not");