blob: 5335d3211c073c1c661d895aeaa0488fa0196ba1 [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*/
114static struct usb_device_id id_table_std [] = {
115 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
116 { } /* Terminating entry */
117};
118
119static struct usb_device_id id_table_prerenumeration [] = {
120 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
121 { } /* Terminating entry */
122};
123
124static struct usb_device_id id_table_combined [] = {
125 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
126 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
127 { } /* Terminating entry */
128};
129
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);
147static void whiteheat_shutdown(struct usb_serial *serial);
148static int whiteheat_open(struct tty_struct *tty,
149 struct usb_serial_port *port, struct file *filp);
150static void whiteheat_close(struct tty_struct *tty,
151 struct usb_serial_port *port, struct file *filp);
152static int whiteheat_write(struct tty_struct *tty,
153 struct usb_serial_port *port,
154 const unsigned char *buf, int count);
155static int whiteheat_write_room(struct tty_struct *tty);
156static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
157 unsigned int cmd, unsigned long arg);
158static void whiteheat_set_termios(struct tty_struct *tty,
159 struct usb_serial_port *port, struct ktermios *old);
160static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file);
161static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
162 unsigned int set, unsigned int clear);
163static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
164static int whiteheat_chars_in_buffer(struct tty_struct *tty);
165static void whiteheat_throttle(struct tty_struct *tty);
166static void whiteheat_unthrottle(struct tty_struct *tty);
167static void whiteheat_read_callback(struct urb *urb);
168static void whiteheat_write_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700170static struct usb_serial_driver whiteheat_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700171 .driver = {
172 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700173 .name = "whiteheatnofirm",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700174 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700175 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100176 .usb_driver = &whiteheat_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .id_table = id_table_prerenumeration,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .num_ports = 1,
179 .probe = whiteheat_firmware_download,
180 .attach = whiteheat_firmware_attach,
181};
182
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700183static struct usb_serial_driver whiteheat_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700184 .driver = {
185 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700186 .name = "whiteheat",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700187 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700188 .description = "Connect Tech - WhiteHEAT",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100189 .usb_driver = &whiteheat_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .id_table = id_table_std,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .num_ports = 4,
192 .attach = whiteheat_attach,
193 .shutdown = whiteheat_shutdown,
194 .open = whiteheat_open,
195 .close = whiteheat_close,
196 .write = whiteheat_write,
197 .write_room = whiteheat_write_room,
198 .ioctl = whiteheat_ioctl,
199 .set_termios = whiteheat_set_termios,
200 .break_ctl = whiteheat_break_ctl,
201 .tiocmget = whiteheat_tiocmget,
202 .tiocmset = whiteheat_tiocmset,
203 .chars_in_buffer = whiteheat_chars_in_buffer,
204 .throttle = whiteheat_throttle,
205 .unthrottle = whiteheat_unthrottle,
206 .read_bulk_callback = whiteheat_read_callback,
207 .write_bulk_callback = whiteheat_write_callback,
208};
209
210
211struct whiteheat_command_private {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200212 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 __u8 port_running;
214 __u8 command_finished;
Alan Cox80359a92008-07-22 11:09:16 +0100215 wait_queue_head_t wait_command; /* for handling sleeping whilst
216 waiting for a command to
217 finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 __u8 result_buffer[64];
219};
220
221
222#define THROTTLED 0x01
223#define ACTUALLY_THROTTLED 0x02
224
225static int urb_pool_size = 8;
226
227struct whiteheat_urb_wrap {
228 struct list_head list;
229 struct urb *urb;
230};
231
232struct whiteheat_private {
233 spinlock_t lock;
234 __u8 flags;
Alan Coxa5b6f602008-04-08 17:16:06 +0100235 __u8 mcr; /* FIXME: no locking on mcr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct list_head rx_urbs_free;
237 struct list_head rx_urbs_submitted;
238 struct list_head rx_urb_q;
239 struct work_struct rx_work;
David Howellsc4028952006-11-22 14:57:56 +0000240 struct usb_serial_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct list_head tx_urbs_free;
242 struct list_head tx_urbs_submitted;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200243 struct mutex deathwarrant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
246
247/* local function prototypes */
248static int start_command_port(struct usb_serial *serial);
249static void stop_command_port(struct usb_serial *serial);
David Howells7d12e782006-10-05 14:55:46 +0100250static void command_port_write_callback(struct urb *urb);
251static void command_port_read_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253static int start_port_read(struct usb_serial_port *port);
Alan Cox80359a92008-07-22 11:09:16 +0100254static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
255 struct list_head *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static struct list_head *list_first(struct list_head *head);
David Howellsc4028952006-11-22 14:57:56 +0000257static void rx_data_softint(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Alan Cox80359a92008-07-22 11:09:16 +0100259static int firm_send_command(struct usb_serial_port *port, __u8 command,
260 __u8 *data, __u8 datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static int firm_open(struct usb_serial_port *port);
262static int firm_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +0100263static int firm_setup_port(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
265static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
266static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
267static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
268static int firm_get_dtr_rts(struct usb_serial_port *port);
269static int firm_report_tx_done(struct usb_serial_port *port);
270
271
272#define COMMAND_PORT 4
273#define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
274#define COMMAND_TIMEOUT_MS 2000
275#define CLOSING_DELAY (30 * HZ)
276
277
278/*****************************************************************************
279 * Connect Tech's White Heat prerenumeration driver functions
280 *****************************************************************************/
281
282/* steps to download the firmware to the WhiteHEAT device:
283 - hold the reset (by writing to the reset bit of the CPUCS register)
284 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
285 - release the reset (by writing to the CPUCS register)
286 - download the WH.HEX file for all addresses greater than 0x1b3f using
287 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
288 - hold the reset
289 - download the WH.HEX file for all addresses less than 0x1b40 using
290 VENDOR_REQUEST_ANCHOR_LOAD
291 - release the reset
292 - device renumerated itself and comes up as new device id with all
293 firmware download completed.
294*/
Alan Cox80359a92008-07-22 11:09:16 +0100295static int whiteheat_firmware_download(struct usb_serial *serial,
296 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
David Woodhouseec6752f2008-05-31 01:35:29 +0300298 int response, ret = -ENOENT;
299 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
300 const struct ihex_binrec *record;
301
Harvey Harrison441b62c2008-03-03 16:08:34 -0800302 dbg("%s", __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300303
304 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
305 &serial->dev->dev)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700306 dev_err(&serial->dev->dev,
307 "%s - request \"whiteheat.fw\" failed\n", __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300308 goto out;
309 }
310 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
311 &serial->dev->dev)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700312 dev_err(&serial->dev->dev,
313 "%s - request \"whiteheat_loader.fw\" failed\n",
314 __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300315 goto out;
316 }
317 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 response = ezusb_set_reset (serial, 1);
319
David Woodhouseec6752f2008-05-31 01:35:29 +0300320 record = (const struct ihex_binrec *)loader_fw->data;
321 while (record) {
322 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
323 (unsigned char *)record->data,
324 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700326 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
327 "failed for loader (%d %04X %p %d)\n",
328 __func__, response, be32_to_cpu(record->addr),
329 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 break;
331 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300332 record = ihex_next_binrec(record);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
Alan Cox80359a92008-07-22 11:09:16 +0100335 response = ezusb_set_reset(serial, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David Woodhouseec6752f2008-05-31 01:35:29 +0300337 record = (const struct ihex_binrec *)firmware_fw->data;
338 while (record && be32_to_cpu(record->addr) < 0x1b40)
339 record = ihex_next_binrec(record);
340 while (record) {
341 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
342 (unsigned char *)record->data,
343 be16_to_cpu(record->len), 0xa3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700345 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
346 "failed for first firmware step "
347 "(%d %04X %p %d)\n", __func__, response,
348 be32_to_cpu(record->addr), record->data,
349 be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 break;
351 }
352 ++record;
353 }
Alan Cox80359a92008-07-22 11:09:16 +0100354
355 response = ezusb_set_reset(serial, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
David Woodhouseec6752f2008-05-31 01:35:29 +0300357 record = (const struct ihex_binrec *)firmware_fw->data;
358 while (record && be32_to_cpu(record->addr) < 0x1b40) {
359 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
360 (unsigned char *)record->data,
361 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700363 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
364 "failed for second firmware step "
365 "(%d %04X %p %d)\n", __func__, response,
366 be32_to_cpu(record->addr), record->data,
367 be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
369 }
370 ++record;
371 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300372 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 response = ezusb_set_reset (serial, 0);
David Woodhouseec6752f2008-05-31 01:35:29 +0300374 out:
375 release_firmware(loader_fw);
376 release_firmware(firmware_fw);
377 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380
Alan Cox80359a92008-07-22 11:09:16 +0100381static int whiteheat_firmware_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 /* We want this device to fail to have a driver assigned to it */
384 return 1;
385}
386
387
388/*****************************************************************************
389 * Connect Tech's White Heat serial driver functions
390 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +0100391static int whiteheat_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct usb_serial_port *command_port;
394 struct whiteheat_command_private *command_info;
395 struct usb_serial_port *port;
396 struct whiteheat_private *info;
397 struct whiteheat_hw_info *hw_info;
398 int pipe;
399 int ret;
400 int alen;
401 __u8 *command;
402 __u8 *result;
403 int i;
404 int j;
405 struct urb *urb;
406 int buf_size;
407 struct whiteheat_urb_wrap *wrap;
408 struct list_head *tmp;
409
410 command_port = serial->port[COMMAND_PORT];
411
Alan Cox80359a92008-07-22 11:09:16 +0100412 pipe = usb_sndbulkpipe(serial->dev,
413 command_port->bulk_out_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 command = kmalloc(2, GFP_KERNEL);
415 if (!command)
416 goto no_command_buffer;
417 command[0] = WHITEHEAT_GET_HW_INFO;
418 command[1] = 0;
Alan Cox80359a92008-07-22 11:09:16 +0100419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
421 if (!result)
422 goto no_result_buffer;
423 /*
424 * When the module is reloaded the firmware is still there and
425 * the endpoints are still in the usb core unchanged. This is the
Alan Cox80359a92008-07-22 11:09:16 +0100426 * unlinking bug in disguise. Same for the call below.
427 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100429 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
430 &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (ret) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700432 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
433 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400435 } else if (alen != 2) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700436 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
437 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto no_firmware;
439 }
440
Alan Cox80359a92008-07-22 11:09:16 +0100441 pipe = usb_rcvbulkpipe(serial->dev,
442 command_port->bulk_in_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* See the comment on the usb_clear_halt() above */
444 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100445 ret = usb_bulk_msg(serial->dev, pipe, result,
446 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (ret) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700448 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
449 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400451 } else if (alen != sizeof(*hw_info) + 1) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700452 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
453 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto no_firmware;
455 } else if (result[0] != command[0]) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700456 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
457 serial->type->description, result[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 goto no_firmware;
459 }
460
461 hw_info = (struct whiteheat_hw_info *)&result[1];
462
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700463 dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
464 serial->type->description, DRIVER_VERSION,
465 hw_info->sw_major_rev, hw_info->sw_minor_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 for (i = 0; i < serial->num_ports; i++) {
468 port = serial->port[i];
469
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800470 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (info == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700472 dev_err(&port->dev,
473 "%s: Out of memory for port structures\n",
474 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 goto no_private;
476 }
477
478 spin_lock_init(&info->lock);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200479 mutex_init(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 info->flags = 0;
481 info->mcr = 0;
David Howellsc4028952006-11-22 14:57:56 +0000482 INIT_WORK(&info->rx_work, rx_data_softint);
483 info->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 INIT_LIST_HEAD(&info->rx_urbs_free);
486 INIT_LIST_HEAD(&info->rx_urbs_submitted);
487 INIT_LIST_HEAD(&info->rx_urb_q);
488 INIT_LIST_HEAD(&info->tx_urbs_free);
489 INIT_LIST_HEAD(&info->tx_urbs_submitted);
490
491 for (j = 0; j < urb_pool_size; j++) {
492 urb = usb_alloc_urb(0, GFP_KERNEL);
493 if (!urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700494 dev_err(&port->dev, "No free urbs available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 goto no_rx_urb;
496 }
497 buf_size = port->read_urb->transfer_buffer_length;
498 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
499 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700500 dev_err(&port->dev,
501 "Couldn't allocate urb buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto no_rx_buf;
503 }
504 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
505 if (!wrap) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700506 dev_err(&port->dev,
507 "Couldn't allocate urb wrapper\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto no_rx_wrap;
509 }
510 usb_fill_bulk_urb(urb, serial->dev,
511 usb_rcvbulkpipe(serial->dev,
512 port->bulk_in_endpointAddress),
513 urb->transfer_buffer, buf_size,
514 whiteheat_read_callback, port);
515 wrap->urb = urb;
516 list_add(&wrap->list, &info->rx_urbs_free);
517
518 urb = usb_alloc_urb(0, GFP_KERNEL);
519 if (!urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700520 dev_err(&port->dev, "No free urbs available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 goto no_tx_urb;
522 }
523 buf_size = port->write_urb->transfer_buffer_length;
524 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
525 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700526 dev_err(&port->dev,
527 "Couldn't allocate urb buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto no_tx_buf;
529 }
530 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
531 if (!wrap) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700532 dev_err(&port->dev,
533 "Couldn't allocate urb wrapper\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto no_tx_wrap;
535 }
536 usb_fill_bulk_urb(urb, serial->dev,
537 usb_sndbulkpipe(serial->dev,
538 port->bulk_out_endpointAddress),
539 urb->transfer_buffer, buf_size,
540 whiteheat_write_callback, port);
541 wrap->urb = urb;
542 list_add(&wrap->list, &info->tx_urbs_free);
543 }
544
545 usb_set_serial_port_data(port, info);
546 }
547
Alan Cox80359a92008-07-22 11:09:16 +0100548 command_info = kmalloc(sizeof(struct whiteheat_command_private),
549 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (command_info == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700551 dev_err(&serial->dev->dev,
552 "%s: Out of memory for port structures\n",
553 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto no_command_private;
555 }
556
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200557 mutex_init(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 command_info->port_running = 0;
559 init_waitqueue_head(&command_info->wait_command);
560 usb_set_serial_port_data(command_port, command_info);
561 command_port->write_urb->complete = command_port_write_callback;
562 command_port->read_urb->complete = command_port_read_callback;
563 kfree(result);
564 kfree(command);
565
566 return 0;
567
568no_firmware:
569 /* Firmware likely not running */
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700570 dev_err(&serial->dev->dev,
571 "%s: Unable to retrieve firmware version, try replugging\n",
572 serial->type->description);
573 dev_err(&serial->dev->dev,
574 "%s: If the firmware is not running (status led not blinking)\n",
575 serial->type->description);
576 dev_err(&serial->dev->dev,
577 "%s: please contact support@connecttech.com\n",
578 serial->type->description);
Jesper Juhl67ca0282006-04-23 19:59:23 +0200579 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -ENODEV;
581
582no_command_private:
583 for (i = serial->num_ports - 1; i >= 0; i--) {
584 port = serial->port[i];
585 info = usb_get_serial_port_data(port);
586 for (j = urb_pool_size - 1; j >= 0; j--) {
587 tmp = list_first(&info->tx_urbs_free);
588 list_del(tmp);
589 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
590 urb = wrap->urb;
591 kfree(wrap);
592no_tx_wrap:
593 kfree(urb->transfer_buffer);
594no_tx_buf:
595 usb_free_urb(urb);
596no_tx_urb:
597 tmp = list_first(&info->rx_urbs_free);
598 list_del(tmp);
599 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
600 urb = wrap->urb;
601 kfree(wrap);
602no_rx_wrap:
603 kfree(urb->transfer_buffer);
604no_rx_buf:
605 usb_free_urb(urb);
606no_rx_urb:
607 ;
608 }
609 kfree(info);
610no_private:
611 ;
612 }
613 kfree(result);
614no_result_buffer:
615 kfree(command);
616no_command_buffer:
617 return -ENOMEM;
618}
619
620
Alan Cox80359a92008-07-22 11:09:16 +0100621static void whiteheat_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct usb_serial_port *command_port;
624 struct usb_serial_port *port;
625 struct whiteheat_private *info;
626 struct whiteheat_urb_wrap *wrap;
627 struct urb *urb;
628 struct list_head *tmp;
629 struct list_head *tmp2;
630 int i;
631
Harvey Harrison441b62c2008-03-03 16:08:34 -0800632 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* free up our private data for our command port */
635 command_port = serial->port[COMMAND_PORT];
Alan Cox80359a92008-07-22 11:09:16 +0100636 kfree(usb_get_serial_port_data(command_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 for (i = 0; i < serial->num_ports; i++) {
639 port = serial->port[i];
640 info = usb_get_serial_port_data(port);
641 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
642 list_del(tmp);
643 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
644 urb = wrap->urb;
645 kfree(wrap);
646 kfree(urb->transfer_buffer);
647 usb_free_urb(urb);
648 }
649 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
650 list_del(tmp);
651 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
652 urb = wrap->urb;
653 kfree(wrap);
654 kfree(urb->transfer_buffer);
655 usb_free_urb(urb);
656 }
657 kfree(info);
658 }
659
660 return;
661}
662
Alan Cox80359a92008-07-22 11:09:16 +0100663static int whiteheat_open(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100664 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Harvey Harrison441b62c2008-03-03 16:08:34 -0800668 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 retval = start_command_port(port->serial);
671 if (retval)
672 goto exit;
673
Alan Cox95da3102008-07-22 11:09:07 +0100674 if (tty)
675 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* send an open port command */
678 retval = firm_open(port);
679 if (retval) {
680 stop_command_port(port->serial);
681 goto exit;
682 }
683
684 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
685 if (retval) {
686 firm_close(port);
687 stop_command_port(port->serial);
688 goto exit;
689 }
690
Alan Cox72e27412008-07-22 11:09:29 +0100691 if (tty)
692 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* Work around HCD bugs */
695 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
696 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
697
698 /* Start reading from the device */
699 retval = start_port_read(port);
700 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700701 dev_err(&port->dev,
702 "%s - failed submitting read urb, error %d\n",
703 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 firm_close(port);
705 stop_command_port(port->serial);
706 goto exit;
707 }
708
709exit:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800710 dbg("%s - exit, retval = %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return retval;
712}
713
714
Alan Cox95da3102008-07-22 11:09:07 +0100715static void whiteheat_close(struct tty_struct *tty,
Alan Cox80359a92008-07-22 11:09:16 +0100716 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct whiteheat_private *info = usb_get_serial_port_data(port);
719 struct whiteheat_urb_wrap *wrap;
720 struct urb *urb;
721 struct list_head *tmp;
722 struct list_head *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Harvey Harrison441b62c2008-03-03 16:08:34 -0800724 dbg("%s - port %d", __func__, port->number);
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100725
726 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* filp is NULL when called from usb_serial_disconnect */
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100728 if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
729 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return;
731 }
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100732 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Alan Cox95da3102008-07-22 11:09:07 +0100734 tty->closing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736/*
737 * Not currently in use; tty_wait_until_sent() calls
738 * serial_chars_in_buffer() which deadlocks on the second semaphore
739 * acquisition. This should be fixed at some point. Greg's been
740 * notified.
741 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
Alan Cox95da3102008-07-22 11:09:07 +0100742 tty_wait_until_sent(tty, CLOSING_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744*/
745
Alan Cox95da3102008-07-22 11:09:07 +0100746 tty_driver_flush_buffer(tty);
747 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 firm_report_tx_done(port);
750
751 firm_close(port);
752
753 /* shutdown our bulk reads and writes */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200754 mutex_lock(&info->deathwarrant);
755 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
757 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
758 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200759 list_del(tmp);
760 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200762 spin_lock_irq(&info->lock);
763 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Akinobu Mita179e0912006-06-26 00:24:41 -0700765 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
766 list_move(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
768 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
769 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200770 list_del(tmp);
771 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200773 spin_lock_irq(&info->lock);
774 list_add(tmp, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200776 spin_unlock_irq(&info->lock);
777 mutex_unlock(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 stop_command_port(port->serial);
780
Alan Cox95da3102008-07-22 11:09:07 +0100781 tty->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784
Alan Cox95da3102008-07-22 11:09:07 +0100785static int whiteheat_write(struct tty_struct *tty,
786 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 struct usb_serial *serial = port->serial;
789 struct whiteheat_private *info = usb_get_serial_port_data(port);
790 struct whiteheat_urb_wrap *wrap;
791 struct urb *urb;
792 int result;
793 int bytes;
794 int sent = 0;
795 unsigned long flags;
796 struct list_head *tmp;
797
Harvey Harrison441b62c2008-03-03 16:08:34 -0800798 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800801 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return (0);
803 }
804
805 while (count) {
806 spin_lock_irqsave(&info->lock, flags);
807 if (list_empty(&info->tx_urbs_free)) {
808 spin_unlock_irqrestore(&info->lock, flags);
809 break;
810 }
811 tmp = list_first(&info->tx_urbs_free);
812 list_del(tmp);
813 spin_unlock_irqrestore(&info->lock, flags);
814
815 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
816 urb = wrap->urb;
Alan Cox80359a92008-07-22 11:09:16 +0100817 bytes = (count > port->bulk_out_size) ?
818 port->bulk_out_size : count;
819 memcpy(urb->transfer_buffer, buf + sent, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Alan Cox80359a92008-07-22 11:09:16 +0100821 usb_serial_debug_data(debug, &port->dev,
822 __func__, bytes, urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 urb->dev = serial->dev;
825 urb->transfer_buffer_length = bytes;
826 result = usb_submit_urb(urb, GFP_ATOMIC);
827 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700828 dev_err(&port->dev,
829 "%s - failed submitting write urb, error %d\n",
830 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 sent = result;
832 spin_lock_irqsave(&info->lock, flags);
833 list_add(tmp, &info->tx_urbs_free);
834 spin_unlock_irqrestore(&info->lock, flags);
835 break;
836 } else {
837 sent += bytes;
838 count -= bytes;
839 spin_lock_irqsave(&info->lock, flags);
840 list_add(tmp, &info->tx_urbs_submitted);
841 spin_unlock_irqrestore(&info->lock, flags);
842 }
843 }
844
845 return sent;
846}
847
Alan Cox95da3102008-07-22 11:09:07 +0100848static int whiteheat_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Alan Cox95da3102008-07-22 11:09:07 +0100850 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 struct whiteheat_private *info = usb_get_serial_port_data(port);
852 struct list_head *tmp;
853 int room = 0;
854 unsigned long flags;
855
Harvey Harrison441b62c2008-03-03 16:08:34 -0800856 dbg("%s - port %d", __func__, port->number);
Alan Cox80359a92008-07-22 11:09:16 +0100857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 spin_lock_irqsave(&info->lock, flags);
859 list_for_each(tmp, &info->tx_urbs_free)
860 room++;
861 spin_unlock_irqrestore(&info->lock, flags);
862 room *= port->bulk_out_size;
863
Harvey Harrison441b62c2008-03-03 16:08:34 -0800864 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return (room);
866}
867
Alan Cox80359a92008-07-22 11:09:16 +0100868static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Alan Cox95da3102008-07-22 11:09:07 +0100870 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 struct whiteheat_private *info = usb_get_serial_port_data(port);
872 unsigned int modem_signals = 0;
873
Harvey Harrison441b62c2008-03-03 16:08:34 -0800874 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 firm_get_dtr_rts(port);
877 if (info->mcr & UART_MCR_DTR)
878 modem_signals |= TIOCM_DTR;
879 if (info->mcr & UART_MCR_RTS)
880 modem_signals |= TIOCM_RTS;
881
882 return modem_signals;
883}
884
Alan Cox80359a92008-07-22 11:09:16 +0100885static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 unsigned int set, unsigned int clear)
887{
Alan Cox95da3102008-07-22 11:09:07 +0100888 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 struct whiteheat_private *info = usb_get_serial_port_data(port);
890
Harvey Harrison441b62c2008-03-03 16:08:34 -0800891 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (set & TIOCM_RTS)
894 info->mcr |= UART_MCR_RTS;
895 if (set & TIOCM_DTR)
896 info->mcr |= UART_MCR_DTR;
897
898 if (clear & TIOCM_RTS)
899 info->mcr &= ~UART_MCR_RTS;
900 if (clear & TIOCM_DTR)
901 info->mcr &= ~UART_MCR_DTR;
902
903 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
904 firm_set_rts(port, info->mcr & UART_MCR_RTS);
905 return 0;
906}
907
908
Alan Cox80359a92008-07-22 11:09:16 +0100909static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
910 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Alan Cox95da3102008-07-22 11:09:07 +0100912 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct serial_struct serstruct;
914 void __user *user_arg = (void __user *)arg;
915
Harvey Harrison441b62c2008-03-03 16:08:34 -0800916 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 switch (cmd) {
Alan Cox80359a92008-07-22 11:09:16 +0100919 case TIOCGSERIAL:
920 memset(&serstruct, 0, sizeof(serstruct));
921 serstruct.type = PORT_16654;
922 serstruct.line = port->serial->minor;
923 serstruct.port = port->number;
924 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
925 serstruct.xmit_fifo_size = port->bulk_out_size;
926 serstruct.custom_divisor = 0;
927 serstruct.baud_base = 460800;
928 serstruct.close_delay = CLOSING_DELAY;
929 serstruct.closing_wait = CLOSING_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Alan Cox80359a92008-07-22 11:09:16 +0100931 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
932 return -EFAULT;
933 break;
934 default:
935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
938 return -ENOIOCTLCMD;
939}
940
941
Alan Cox80359a92008-07-22 11:09:16 +0100942static void whiteheat_set_termios(struct tty_struct *tty,
943 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Alan Cox95da3102008-07-22 11:09:07 +0100945 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Alan Cox80359a92008-07-22 11:09:16 +0100948static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
949{
Alan Cox95da3102008-07-22 11:09:07 +0100950 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 firm_set_break(port, break_state);
952}
953
954
Alan Cox95da3102008-07-22 11:09:07 +0100955static int whiteheat_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Alan Cox95da3102008-07-22 11:09:07 +0100957 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct whiteheat_private *info = usb_get_serial_port_data(port);
959 struct list_head *tmp;
960 struct whiteheat_urb_wrap *wrap;
961 int chars = 0;
962 unsigned long flags;
963
Harvey Harrison441b62c2008-03-03 16:08:34 -0800964 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 spin_lock_irqsave(&info->lock, flags);
967 list_for_each(tmp, &info->tx_urbs_submitted) {
968 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
969 chars += wrap->urb->transfer_buffer_length;
970 }
971 spin_unlock_irqrestore(&info->lock, flags);
972
Alan Cox80359a92008-07-22 11:09:16 +0100973 dbg("%s - returns %d", __func__, chars);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200974 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977
Alan Cox80359a92008-07-22 11:09:16 +0100978static void whiteheat_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Alan Cox95da3102008-07-22 11:09:07 +0100980 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 struct whiteheat_private *info = usb_get_serial_port_data(port);
982 unsigned long flags;
983
Harvey Harrison441b62c2008-03-03 16:08:34 -0800984 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 spin_lock_irqsave(&info->lock, flags);
987 info->flags |= THROTTLED;
988 spin_unlock_irqrestore(&info->lock, flags);
989
990 return;
991}
992
993
Alan Cox80359a92008-07-22 11:09:16 +0100994static void whiteheat_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Alan Cox95da3102008-07-22 11:09:07 +0100996 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 struct whiteheat_private *info = usb_get_serial_port_data(port);
998 int actually_throttled;
999 unsigned long flags;
1000
Harvey Harrison441b62c2008-03-03 16:08:34 -08001001 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 spin_lock_irqsave(&info->lock, flags);
1004 actually_throttled = info->flags & ACTUALLY_THROTTLED;
1005 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
1006 spin_unlock_irqrestore(&info->lock, flags);
1007
1008 if (actually_throttled)
David Howellsc4028952006-11-22 14:57:56 +00001009 rx_data_softint(&info->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 return;
1012}
1013
1014
1015/*****************************************************************************
1016 * Connect Tech's White Heat callback routines
1017 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001018static void command_port_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001020 int status = urb->status;
1021
Harvey Harrison441b62c2008-03-03 16:08:34 -08001022 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001024 if (status) {
1025 dbg("nonzero urb status: %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return;
1027 }
1028}
1029
1030
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001031static void command_port_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Ming Leicdc97792008-02-24 18:41:47 +08001033 struct usb_serial_port *command_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 struct whiteheat_command_private *command_info;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001035 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unsigned char *data = urb->transfer_buffer;
1037 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Harvey Harrison441b62c2008-03-03 16:08:34 -08001039 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 command_info = usb_get_serial_port_data(command_port);
1042 if (!command_info) {
Alan Cox80359a92008-07-22 11:09:16 +01001043 dbg("%s - command_info is NULL, exiting.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return;
1045 }
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001046 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001047 dbg("%s - nonzero urb status: %d", __func__, status);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001048 if (status != -ENOENT)
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001049 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1050 wake_up(&command_info->wait_command);
1051 return;
1052 }
1053
Alan Cox80359a92008-07-22 11:09:16 +01001054 usb_serial_debug_data(debug, &command_port->dev,
1055 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1058 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001059 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1061 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001062 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 } else if (data[0] == WHITEHEAT_EVENT) {
Alan Cox80359a92008-07-22 11:09:16 +01001064 /* These are unsolicited reports from the firmware, hence no
1065 waiting command to wakeup */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001066 dbg("%s - event received", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
Alan Cox80359a92008-07-22 11:09:16 +01001068 memcpy(command_info->result_buffer, &data[1],
1069 urb->actual_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001071 wake_up(&command_info->wait_command);
Alan Cox80359a92008-07-22 11:09:16 +01001072 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -08001073 dbg("%s - bad reply from firmware", __func__);
Alan Cox80359a92008-07-22 11:09:16 +01001074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 /* Continue trying to always read */
1076 command_port->read_urb->dev = command_port->serial->dev;
1077 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (result)
Alan Cox80359a92008-07-22 11:09:16 +01001079 dbg("%s - failed resubmitting read urb, error %d",
1080 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
1083
David Howells7d12e782006-10-05 14:55:46 +01001084static void whiteheat_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Ming Leicdc97792008-02-24 18:41:47 +08001086 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 struct whiteheat_urb_wrap *wrap;
1088 unsigned char *data = urb->transfer_buffer;
1089 struct whiteheat_private *info = usb_get_serial_port_data(port);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001090 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Harvey Harrison441b62c2008-03-03 16:08:34 -08001092 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 spin_lock(&info->lock);
1095 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1096 if (!wrap) {
1097 spin_unlock(&info->lock);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001098 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return;
1100 }
1101 list_del(&wrap->list);
1102 spin_unlock(&info->lock);
1103
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001104 if (status) {
1105 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001106 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 spin_lock(&info->lock);
1108 list_add(&wrap->list, &info->rx_urbs_free);
1109 spin_unlock(&info->lock);
1110 return;
1111 }
1112
Alan Cox80359a92008-07-22 11:09:16 +01001113 usb_serial_debug_data(debug, &port->dev,
1114 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 spin_lock(&info->lock);
1117 list_add_tail(&wrap->list, &info->rx_urb_q);
1118 if (info->flags & THROTTLED) {
1119 info->flags |= ACTUALLY_THROTTLED;
1120 spin_unlock(&info->lock);
1121 return;
1122 }
1123 spin_unlock(&info->lock);
1124
1125 schedule_work(&info->rx_work);
1126}
1127
1128
David Howells7d12e782006-10-05 14:55:46 +01001129static void whiteheat_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Ming Leicdc97792008-02-24 18:41:47 +08001131 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 struct whiteheat_private *info = usb_get_serial_port_data(port);
1133 struct whiteheat_urb_wrap *wrap;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001134 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Harvey Harrison441b62c2008-03-03 16:08:34 -08001136 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 spin_lock(&info->lock);
1139 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1140 if (!wrap) {
1141 spin_unlock(&info->lock);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001142 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return;
1144 }
Akinobu Mita179e0912006-06-26 00:24:41 -07001145 list_move(&wrap->list, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 spin_unlock(&info->lock);
1147
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001148 if (status) {
1149 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001150 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return;
1152 }
1153
Pete Zaitcevcf2c7482006-05-22 21:58:49 -07001154 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
1157
1158/*****************************************************************************
1159 * Connect Tech's White Heat firmware interface
1160 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +01001161static int firm_send_command(struct usb_serial_port *port, __u8 command,
1162 __u8 *data, __u8 datasize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 struct usb_serial_port *command_port;
1165 struct whiteheat_command_private *command_info;
1166 struct whiteheat_private *info;
1167 __u8 *transfer_buffer;
1168 int retval = 0;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001169 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Harvey Harrison441b62c2008-03-03 16:08:34 -08001171 dbg("%s - command %d", __func__, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 command_port = port->serial->port[COMMAND_PORT];
1174 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001175 mutex_lock(&command_info->mutex);
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001176 command_info->command_finished = false;
Alan Cox80359a92008-07-22 11:09:16 +01001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1179 transfer_buffer[0] = command;
Alan Cox80359a92008-07-22 11:09:16 +01001180 memcpy(&transfer_buffer[1], data, datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 command_port->write_urb->transfer_buffer_length = datasize + 1;
1182 command_port->write_urb->dev = port->serial->dev;
Alan Cox80359a92008-07-22 11:09:16 +01001183 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001185 dbg("%s - submit urb failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto exit;
1187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /* wait for the command to complete */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001190 t = wait_event_timeout(command_info->wait_command,
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001191 (bool)command_info->command_finished, COMMAND_TIMEOUT);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001192 if (!t)
1193 usb_kill_urb(command_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001195 if (command_info->command_finished == false) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001196 dbg("%s - command timed out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 retval = -ETIMEDOUT;
1198 goto exit;
1199 }
1200
1201 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001202 dbg("%s - command failed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 retval = -EIO;
1204 goto exit;
1205 }
1206
1207 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001208 dbg("%s - command completed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 switch (command) {
Alan Cox80359a92008-07-22 11:09:16 +01001210 case WHITEHEAT_GET_DTR_RTS:
1211 info = usb_get_serial_port_data(port);
1212 memcpy(&info->mcr, command_info->result_buffer,
1213 sizeof(struct whiteheat_dr_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 break;
1215 }
1216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001218 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return retval;
1220}
1221
1222
Alan Cox80359a92008-07-22 11:09:16 +01001223static int firm_open(struct usb_serial_port *port)
1224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 struct whiteheat_simple open_command;
1226
1227 open_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001228 return firm_send_command(port, WHITEHEAT_OPEN,
1229 (__u8 *)&open_command, sizeof(open_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232
Alan Cox80359a92008-07-22 11:09:16 +01001233static int firm_close(struct usb_serial_port *port)
1234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 struct whiteheat_simple close_command;
1236
1237 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001238 return firm_send_command(port, WHITEHEAT_CLOSE,
1239 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242
Alan Cox80359a92008-07-22 11:09:16 +01001243static int firm_setup_port(struct tty_struct *tty)
1244{
Alan Cox95da3102008-07-22 11:09:07 +01001245 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 struct whiteheat_port_settings port_settings;
Alan Cox95da3102008-07-22 11:09:07 +01001247 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 port_settings.port = port->number + 1;
1250
1251 /* get the byte size */
1252 switch (cflag & CSIZE) {
Alan Cox80359a92008-07-22 11:09:16 +01001253 case CS5: port_settings.bits = 5; break;
1254 case CS6: port_settings.bits = 6; break;
1255 case CS7: port_settings.bits = 7; break;
1256 default:
1257 case CS8: port_settings.bits = 8; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001259 dbg("%s - data bits = %d", __func__, port_settings.bits);
Alan Cox80359a92008-07-22 11:09:16 +01001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /* determine the parity */
1262 if (cflag & PARENB)
1263 if (cflag & CMSPAR)
1264 if (cflag & PARODD)
1265 port_settings.parity = WHITEHEAT_PAR_MARK;
1266 else
1267 port_settings.parity = WHITEHEAT_PAR_SPACE;
1268 else
1269 if (cflag & PARODD)
1270 port_settings.parity = WHITEHEAT_PAR_ODD;
1271 else
1272 port_settings.parity = WHITEHEAT_PAR_EVEN;
1273 else
1274 port_settings.parity = WHITEHEAT_PAR_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001275 dbg("%s - parity = %c", __func__, port_settings.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 /* figure out the stop bits requested */
1278 if (cflag & CSTOPB)
1279 port_settings.stop = 2;
1280 else
1281 port_settings.stop = 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001282 dbg("%s - stop bits = %d", __func__, port_settings.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 /* figure out the flow control settings */
1285 if (cflag & CRTSCTS)
Alan Cox80359a92008-07-22 11:09:16 +01001286 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1287 WHITEHEAT_HFLOW_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 else
1289 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001290 dbg("%s - hardware flow control = %s %s %s %s", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1292 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1293 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1294 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
Alan Cox80359a92008-07-22 11:09:16 +01001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /* determine software flow control */
Alan Cox95da3102008-07-22 11:09:07 +01001297 if (I_IXOFF(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1299 else
1300 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001301 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
Alan Cox80359a92008-07-22 11:09:16 +01001302
Alan Cox95da3102008-07-22 11:09:07 +01001303 port_settings.xon = START_CHAR(tty);
1304 port_settings.xoff = STOP_CHAR(tty);
Alan Cox80359a92008-07-22 11:09:16 +01001305 dbg("%s - XON = %2x, XOFF = %2x",
1306 __func__, port_settings.xon, port_settings.xoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* get the baud rate wanted */
Alan Cox95da3102008-07-22 11:09:07 +01001309 port_settings.baud = tty_get_baud_rate(tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001310 dbg("%s - baud rate = %d", __func__, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Alan Cox01d1df22007-10-18 01:24:23 -07001312 /* fixme: should set validated settings */
Alan Cox95da3102008-07-22 11:09:07 +01001313 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /* handle any settings that aren't specified in the tty structure */
1315 port_settings.lloop = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* now send the message to the device */
Alan Cox80359a92008-07-22 11:09:16 +01001318 return firm_send_command(port, WHITEHEAT_SETUP_PORT,
1319 (__u8 *)&port_settings, sizeof(port_settings));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
1322
Alan Cox80359a92008-07-22 11:09:16 +01001323static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 struct whiteheat_set_rdb rts_command;
1326
1327 rts_command.port = port->number - port->serial->minor + 1;
1328 rts_command.state = onoff;
Alan Cox80359a92008-07-22 11:09:16 +01001329 return firm_send_command(port, WHITEHEAT_SET_RTS,
1330 (__u8 *)&rts_command, sizeof(rts_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
1333
Alan Cox80359a92008-07-22 11:09:16 +01001334static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 struct whiteheat_set_rdb dtr_command;
1337
1338 dtr_command.port = port->number - port->serial->minor + 1;
1339 dtr_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001340 return firm_send_command(port, WHITEHEAT_SET_DTR,
Alan Cox80359a92008-07-22 11:09:16 +01001341 (__u8 *)&dtr_command, sizeof(dtr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
1344
Alan Cox80359a92008-07-22 11:09:16 +01001345static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct whiteheat_set_rdb break_command;
1348
1349 break_command.port = port->number - port->serial->minor + 1;
1350 break_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001351 return firm_send_command(port, WHITEHEAT_SET_BREAK,
Alan Cox80359a92008-07-22 11:09:16 +01001352 (__u8 *)&break_command, sizeof(break_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355
Alan Cox80359a92008-07-22 11:09:16 +01001356static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 struct whiteheat_purge purge_command;
1359
1360 purge_command.port = port->number - port->serial->minor + 1;
1361 purge_command.what = rxtx;
Alan Cox80359a92008-07-22 11:09:16 +01001362 return firm_send_command(port, WHITEHEAT_PURGE,
1363 (__u8 *)&purge_command, sizeof(purge_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
1366
Alan Cox80359a92008-07-22 11:09:16 +01001367static int firm_get_dtr_rts(struct usb_serial_port *port)
1368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct whiteheat_simple get_dr_command;
1370
1371 get_dr_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001372 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1373 (__u8 *)&get_dr_command, sizeof(get_dr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
1376
Alan Cox80359a92008-07-22 11:09:16 +01001377static int firm_report_tx_done(struct usb_serial_port *port)
1378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 struct whiteheat_simple close_command;
1380
1381 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001382 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1383 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386
1387/*****************************************************************************
1388 * Connect Tech's White Heat utility functions
1389 *****************************************************************************/
1390static int start_command_port(struct usb_serial *serial)
1391{
1392 struct usb_serial_port *command_port;
1393 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 int retval = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 command_port = serial->port[COMMAND_PORT];
1397 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001398 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!command_info->port_running) {
1400 /* Work around HCD bugs */
1401 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1402
1403 command_port->read_urb->dev = serial->dev;
1404 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1405 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001406 dev_err(&serial->dev->dev,
1407 "%s - failed submitting read urb, error %d\n",
1408 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 goto exit;
1410 }
1411 }
1412 command_info->port_running++;
1413
1414exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001415 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return retval;
1417}
1418
1419
1420static void stop_command_port(struct usb_serial *serial)
1421{
1422 struct usb_serial_port *command_port;
1423 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 command_port = serial->port[COMMAND_PORT];
1426 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001427 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 command_info->port_running--;
1429 if (!command_info->port_running)
1430 usb_kill_urb(command_port->read_urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001431 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
1434
1435static int start_port_read(struct usb_serial_port *port)
1436{
1437 struct whiteheat_private *info = usb_get_serial_port_data(port);
1438 struct whiteheat_urb_wrap *wrap;
1439 struct urb *urb;
1440 int retval = 0;
1441 unsigned long flags;
1442 struct list_head *tmp;
1443 struct list_head *tmp2;
1444
1445 spin_lock_irqsave(&info->lock, flags);
1446
1447 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1448 list_del(tmp);
1449 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1450 urb = wrap->urb;
1451 urb->dev = port->serial->dev;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001452 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 retval = usb_submit_urb(urb, GFP_KERNEL);
1454 if (retval) {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001455 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 list_add(tmp, &info->rx_urbs_free);
1457 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1458 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1459 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001460 list_del(tmp);
1461 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001463 spin_lock_irqsave(&info->lock, flags);
1464 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466 break;
1467 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001468 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 list_add(tmp, &info->rx_urbs_submitted);
1470 }
1471
1472 spin_unlock_irqrestore(&info->lock, flags);
1473
1474 return retval;
1475}
1476
1477
Alan Cox80359a92008-07-22 11:09:16 +01001478static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1479 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 struct whiteheat_urb_wrap *wrap;
1482 struct list_head *tmp;
1483
1484 list_for_each(tmp, head) {
1485 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1486 if (wrap->urb == urb)
1487 return wrap;
1488 }
1489
1490 return NULL;
1491}
1492
1493
1494static struct list_head *list_first(struct list_head *head)
1495{
1496 return head->next;
1497}
1498
1499
David Howellsc4028952006-11-22 14:57:56 +00001500static void rx_data_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
David Howellsc4028952006-11-22 14:57:56 +00001502 struct whiteheat_private *info =
1503 container_of(work, struct whiteheat_private, rx_work);
1504 struct usb_serial_port *port = info->port;
Alan Cox4a90f092008-10-13 10:39:46 +01001505 struct tty_struct *tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 struct whiteheat_urb_wrap *wrap;
1507 struct urb *urb;
1508 unsigned long flags;
1509 struct list_head *tmp;
1510 struct list_head *tmp2;
1511 int result;
1512 int sent = 0;
1513
1514 spin_lock_irqsave(&info->lock, flags);
1515 if (info->flags & THROTTLED) {
1516 spin_unlock_irqrestore(&info->lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +01001517 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519
1520 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1521 list_del(tmp);
1522 spin_unlock_irqrestore(&info->lock, flags);
1523
1524 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1525 urb = wrap->urb;
1526
1527 if (tty && urb->actual_length) {
Alan Cox80359a92008-07-22 11:09:16 +01001528 int len = tty_buffer_request_room(tty,
1529 urb->actual_length);
Alan Cox33f0f882006-01-09 20:54:13 -08001530 /* This stuff can go away now I suspect */
1531 if (unlikely(len < urb->actual_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 spin_lock_irqsave(&info->lock, flags);
1533 list_add(tmp, &info->rx_urb_q);
1534 spin_unlock_irqrestore(&info->lock, flags);
1535 tty_flip_buffer_push(tty);
1536 schedule_work(&info->rx_work);
Alan Cox4a90f092008-10-13 10:39:46 +01001537 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
Alan Cox33f0f882006-01-09 20:54:13 -08001539 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1540 sent += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542
1543 urb->dev = port->serial->dev;
1544 result = usb_submit_urb(urb, GFP_ATOMIC);
1545 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001546 dev_err(&port->dev,
1547 "%s - failed resubmitting read urb, error %d\n",
Alan Cox80359a92008-07-22 11:09:16 +01001548 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 spin_lock_irqsave(&info->lock, flags);
1550 list_add(tmp, &info->rx_urbs_free);
1551 continue;
1552 }
1553
1554 spin_lock_irqsave(&info->lock, flags);
1555 list_add(tmp, &info->rx_urbs_submitted);
1556 }
1557 spin_unlock_irqrestore(&info->lock, flags);
1558
1559 if (sent)
1560 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001561out:
1562 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565
1566/*****************************************************************************
1567 * Connect Tech's White Heat module functions
1568 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +01001569static int __init whiteheat_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 int retval;
1572 retval = usb_serial_register(&whiteheat_fake_device);
1573 if (retval)
1574 goto failed_fake_register;
1575 retval = usb_serial_register(&whiteheat_device);
1576 if (retval)
1577 goto failed_device_register;
1578 retval = usb_register(&whiteheat_driver);
1579 if (retval)
1580 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001581 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1582 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return 0;
1584failed_usb_register:
1585 usb_serial_deregister(&whiteheat_device);
1586failed_device_register:
1587 usb_serial_deregister(&whiteheat_fake_device);
1588failed_fake_register:
1589 return retval;
1590}
1591
1592
Alan Cox80359a92008-07-22 11:09:16 +01001593static void __exit whiteheat_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Alan Cox80359a92008-07-22 11:09:16 +01001595 usb_deregister(&whiteheat_driver);
1596 usb_serial_deregister(&whiteheat_fake_device);
1597 usb_serial_deregister(&whiteheat_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
1600
1601module_init(whiteheat_init);
1602module_exit(whiteheat_exit);
1603
Alan Cox80359a92008-07-22 11:09:16 +01001604MODULE_AUTHOR(DRIVER_AUTHOR);
1605MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606MODULE_LICENSE("GPL");
1607
David Woodhouseec6752f2008-05-31 01:35:29 +03001608MODULE_FIRMWARE("whiteheat.fw");
1609MODULE_FIRMWARE("whiteheat_loader.fw");
1610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611module_param(urb_pool_size, int, 0);
1612MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1613
1614module_param(debug, bool, S_IRUGO | S_IWUSR);
1615MODULE_PARM_DESC(debug, "Debug enabled or not");