blob: 39f7f8d291d6ec8669d9a0ca0c65a22f4fbc9073 [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)) {
306 err("%s - request \"whiteheat.fw\" failed", __func__);
307 goto out;
308 }
309 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
310 &serial->dev->dev)) {
311 err("%s - request \"whiteheat_loader.fw\" failed", __func__);
312 goto out;
313 }
314 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 response = ezusb_set_reset (serial, 1);
316
David Woodhouseec6752f2008-05-31 01:35:29 +0300317 record = (const struct ihex_binrec *)loader_fw->data;
318 while (record) {
319 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
320 (unsigned char *)record->data,
321 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (response < 0) {
323 err("%s - ezusb_writememory failed for loader (%d %04X %p %d)",
David Woodhouseec6752f2008-05-31 01:35:29 +0300324 __func__, response, be32_to_cpu(record->addr),
325 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 break;
327 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300328 record = ihex_next_binrec(record);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
Alan Cox80359a92008-07-22 11:09:16 +0100331 response = ezusb_set_reset(serial, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
David Woodhouseec6752f2008-05-31 01:35:29 +0300333 record = (const struct ihex_binrec *)firmware_fw->data;
334 while (record && be32_to_cpu(record->addr) < 0x1b40)
335 record = ihex_next_binrec(record);
336 while (record) {
337 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
338 (unsigned char *)record->data,
339 be16_to_cpu(record->len), 0xa3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (response < 0) {
341 err("%s - ezusb_writememory failed for first firmware step (%d %04X %p %d)",
David Woodhouseec6752f2008-05-31 01:35:29 +0300342 __func__, response, be32_to_cpu(record->addr),
343 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 break;
345 }
346 ++record;
347 }
Alan Cox80359a92008-07-22 11:09:16 +0100348
349 response = ezusb_set_reset(serial, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
David Woodhouseec6752f2008-05-31 01:35:29 +0300351 record = (const struct ihex_binrec *)firmware_fw->data;
352 while (record && be32_to_cpu(record->addr) < 0x1b40) {
353 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
354 (unsigned char *)record->data,
355 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (response < 0) {
357 err("%s - ezusb_writememory failed for second firmware step (%d %04X %p %d)",
David Woodhouseec6752f2008-05-31 01:35:29 +0300358 __func__, response, be32_to_cpu(record->addr),
359 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 }
362 ++record;
363 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300364 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 response = ezusb_set_reset (serial, 0);
David Woodhouseec6752f2008-05-31 01:35:29 +0300366 out:
367 release_firmware(loader_fw);
368 release_firmware(firmware_fw);
369 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372
Alan Cox80359a92008-07-22 11:09:16 +0100373static int whiteheat_firmware_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 /* We want this device to fail to have a driver assigned to it */
376 return 1;
377}
378
379
380/*****************************************************************************
381 * Connect Tech's White Heat serial driver functions
382 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +0100383static int whiteheat_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct usb_serial_port *command_port;
386 struct whiteheat_command_private *command_info;
387 struct usb_serial_port *port;
388 struct whiteheat_private *info;
389 struct whiteheat_hw_info *hw_info;
390 int pipe;
391 int ret;
392 int alen;
393 __u8 *command;
394 __u8 *result;
395 int i;
396 int j;
397 struct urb *urb;
398 int buf_size;
399 struct whiteheat_urb_wrap *wrap;
400 struct list_head *tmp;
401
402 command_port = serial->port[COMMAND_PORT];
403
Alan Cox80359a92008-07-22 11:09:16 +0100404 pipe = usb_sndbulkpipe(serial->dev,
405 command_port->bulk_out_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 command = kmalloc(2, GFP_KERNEL);
407 if (!command)
408 goto no_command_buffer;
409 command[0] = WHITEHEAT_GET_HW_INFO;
410 command[1] = 0;
Alan Cox80359a92008-07-22 11:09:16 +0100411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
413 if (!result)
414 goto no_result_buffer;
415 /*
416 * When the module is reloaded the firmware is still there and
417 * the endpoints are still in the usb core unchanged. This is the
Alan Cox80359a92008-07-22 11:09:16 +0100418 * unlinking bug in disguise. Same for the call below.
419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100421 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
422 &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (ret) {
Alan Cox80359a92008-07-22 11:09:16 +0100424 err("%s: Couldn't send command [%d]",
425 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400427 } else if (alen != 2) {
Alan Cox80359a92008-07-22 11:09:16 +0100428 err("%s: Send command incomplete [%d]",
429 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto no_firmware;
431 }
432
Alan Cox80359a92008-07-22 11:09:16 +0100433 pipe = usb_rcvbulkpipe(serial->dev,
434 command_port->bulk_in_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /* See the comment on the usb_clear_halt() above */
436 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100437 ret = usb_bulk_msg(serial->dev, pipe, result,
438 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (ret) {
Alan Cox80359a92008-07-22 11:09:16 +0100440 err("%s: Couldn't get results [%d]",
441 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400443 } else if (alen != sizeof(*hw_info) + 1) {
Alan Cox80359a92008-07-22 11:09:16 +0100444 err("%s: Get results incomplete [%d]",
445 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 goto no_firmware;
447 } else if (result[0] != command[0]) {
Alan Cox80359a92008-07-22 11:09:16 +0100448 err("%s: Command failed [%d]",
449 serial->type->description, result[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto no_firmware;
451 }
452
453 hw_info = (struct whiteheat_hw_info *)&result[1];
454
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700455 dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
456 serial->type->description, DRIVER_VERSION,
457 hw_info->sw_major_rev, hw_info->sw_minor_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 for (i = 0; i < serial->num_ports; i++) {
460 port = serial->port[i];
461
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800462 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (info == NULL) {
Alan Cox80359a92008-07-22 11:09:16 +0100464 err("%s: Out of memory for port structures\n",
465 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 goto no_private;
467 }
468
469 spin_lock_init(&info->lock);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200470 mutex_init(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 info->flags = 0;
472 info->mcr = 0;
David Howellsc4028952006-11-22 14:57:56 +0000473 INIT_WORK(&info->rx_work, rx_data_softint);
474 info->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 INIT_LIST_HEAD(&info->rx_urbs_free);
477 INIT_LIST_HEAD(&info->rx_urbs_submitted);
478 INIT_LIST_HEAD(&info->rx_urb_q);
479 INIT_LIST_HEAD(&info->tx_urbs_free);
480 INIT_LIST_HEAD(&info->tx_urbs_submitted);
481
482 for (j = 0; j < urb_pool_size; j++) {
483 urb = usb_alloc_urb(0, GFP_KERNEL);
484 if (!urb) {
485 err("No free urbs available");
486 goto no_rx_urb;
487 }
488 buf_size = port->read_urb->transfer_buffer_length;
489 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
490 if (!urb->transfer_buffer) {
491 err("Couldn't allocate urb buffer");
492 goto no_rx_buf;
493 }
494 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
495 if (!wrap) {
496 err("Couldn't allocate urb wrapper");
497 goto no_rx_wrap;
498 }
499 usb_fill_bulk_urb(urb, serial->dev,
500 usb_rcvbulkpipe(serial->dev,
501 port->bulk_in_endpointAddress),
502 urb->transfer_buffer, buf_size,
503 whiteheat_read_callback, port);
504 wrap->urb = urb;
505 list_add(&wrap->list, &info->rx_urbs_free);
506
507 urb = usb_alloc_urb(0, GFP_KERNEL);
508 if (!urb) {
509 err("No free urbs available");
510 goto no_tx_urb;
511 }
512 buf_size = port->write_urb->transfer_buffer_length;
513 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
514 if (!urb->transfer_buffer) {
515 err("Couldn't allocate urb buffer");
516 goto no_tx_buf;
517 }
518 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
519 if (!wrap) {
520 err("Couldn't allocate urb wrapper");
521 goto no_tx_wrap;
522 }
523 usb_fill_bulk_urb(urb, serial->dev,
524 usb_sndbulkpipe(serial->dev,
525 port->bulk_out_endpointAddress),
526 urb->transfer_buffer, buf_size,
527 whiteheat_write_callback, port);
528 wrap->urb = urb;
529 list_add(&wrap->list, &info->tx_urbs_free);
530 }
531
532 usb_set_serial_port_data(port, info);
533 }
534
Alan Cox80359a92008-07-22 11:09:16 +0100535 command_info = kmalloc(sizeof(struct whiteheat_command_private),
536 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (command_info == NULL) {
Alan Cox80359a92008-07-22 11:09:16 +0100538 err("%s: Out of memory for port structures\n",
539 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto no_command_private;
541 }
542
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200543 mutex_init(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 command_info->port_running = 0;
545 init_waitqueue_head(&command_info->wait_command);
546 usb_set_serial_port_data(command_port, command_info);
547 command_port->write_urb->complete = command_port_write_callback;
548 command_port->read_urb->complete = command_port_read_callback;
549 kfree(result);
550 kfree(command);
551
552 return 0;
553
554no_firmware:
555 /* Firmware likely not running */
Alan Cox80359a92008-07-22 11:09:16 +0100556 err("%s: Unable to retrieve firmware version, try replugging\n",
557 serial->type->description);
558 err("%s: If the firmware is not running (status led not blinking)\n",
559 serial->type->description);
560 err("%s: please contact support@connecttech.com\n",
561 serial->type->description);
Jesper Juhl67ca0282006-04-23 19:59:23 +0200562 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -ENODEV;
564
565no_command_private:
566 for (i = serial->num_ports - 1; i >= 0; i--) {
567 port = serial->port[i];
568 info = usb_get_serial_port_data(port);
569 for (j = urb_pool_size - 1; j >= 0; j--) {
570 tmp = list_first(&info->tx_urbs_free);
571 list_del(tmp);
572 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
573 urb = wrap->urb;
574 kfree(wrap);
575no_tx_wrap:
576 kfree(urb->transfer_buffer);
577no_tx_buf:
578 usb_free_urb(urb);
579no_tx_urb:
580 tmp = list_first(&info->rx_urbs_free);
581 list_del(tmp);
582 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
583 urb = wrap->urb;
584 kfree(wrap);
585no_rx_wrap:
586 kfree(urb->transfer_buffer);
587no_rx_buf:
588 usb_free_urb(urb);
589no_rx_urb:
590 ;
591 }
592 kfree(info);
593no_private:
594 ;
595 }
596 kfree(result);
597no_result_buffer:
598 kfree(command);
599no_command_buffer:
600 return -ENOMEM;
601}
602
603
Alan Cox80359a92008-07-22 11:09:16 +0100604static void whiteheat_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct usb_serial_port *command_port;
607 struct usb_serial_port *port;
608 struct whiteheat_private *info;
609 struct whiteheat_urb_wrap *wrap;
610 struct urb *urb;
611 struct list_head *tmp;
612 struct list_head *tmp2;
613 int i;
614
Harvey Harrison441b62c2008-03-03 16:08:34 -0800615 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /* free up our private data for our command port */
618 command_port = serial->port[COMMAND_PORT];
Alan Cox80359a92008-07-22 11:09:16 +0100619 kfree(usb_get_serial_port_data(command_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 for (i = 0; i < serial->num_ports; i++) {
622 port = serial->port[i];
623 info = usb_get_serial_port_data(port);
624 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
625 list_del(tmp);
626 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
627 urb = wrap->urb;
628 kfree(wrap);
629 kfree(urb->transfer_buffer);
630 usb_free_urb(urb);
631 }
632 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
633 list_del(tmp);
634 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
635 urb = wrap->urb;
636 kfree(wrap);
637 kfree(urb->transfer_buffer);
638 usb_free_urb(urb);
639 }
640 kfree(info);
641 }
642
643 return;
644}
645
Alan Cox80359a92008-07-22 11:09:16 +0100646static int whiteheat_open(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100647 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Harvey Harrison441b62c2008-03-03 16:08:34 -0800651 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 retval = start_command_port(port->serial);
654 if (retval)
655 goto exit;
656
Alan Cox95da3102008-07-22 11:09:07 +0100657 if (tty)
658 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* send an open port command */
661 retval = firm_open(port);
662 if (retval) {
663 stop_command_port(port->serial);
664 goto exit;
665 }
666
667 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
668 if (retval) {
669 firm_close(port);
670 stop_command_port(port->serial);
671 goto exit;
672 }
673
Alan Cox72e27412008-07-22 11:09:29 +0100674 if (tty)
675 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* Work around HCD bugs */
678 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
679 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
680
681 /* Start reading from the device */
682 retval = start_port_read(port);
683 if (retval) {
Alan Cox80359a92008-07-22 11:09:16 +0100684 err("%s - failed submitting read urb, error %d",
685 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 firm_close(port);
687 stop_command_port(port->serial);
688 goto exit;
689 }
690
691exit:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800692 dbg("%s - exit, retval = %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return retval;
694}
695
696
Alan Cox95da3102008-07-22 11:09:07 +0100697static void whiteheat_close(struct tty_struct *tty,
Alan Cox80359a92008-07-22 11:09:16 +0100698 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 struct whiteheat_private *info = usb_get_serial_port_data(port);
701 struct whiteheat_urb_wrap *wrap;
702 struct urb *urb;
703 struct list_head *tmp;
704 struct list_head *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Harvey Harrison441b62c2008-03-03 16:08:34 -0800706 dbg("%s - port %d", __func__, port->number);
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100707
708 mutex_lock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* filp is NULL when called from usb_serial_disconnect */
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100710 if ((filp && (tty_hung_up_p(filp))) || port->serial->disconnected) {
711 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return;
713 }
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100714 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Alan Cox95da3102008-07-22 11:09:07 +0100716 tty->closing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718/*
719 * Not currently in use; tty_wait_until_sent() calls
720 * serial_chars_in_buffer() which deadlocks on the second semaphore
721 * acquisition. This should be fixed at some point. Greg's been
722 * notified.
723 if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
Alan Cox95da3102008-07-22 11:09:07 +0100724 tty_wait_until_sent(tty, CLOSING_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726*/
727
Alan Cox95da3102008-07-22 11:09:07 +0100728 tty_driver_flush_buffer(tty);
729 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 firm_report_tx_done(port);
732
733 firm_close(port);
734
735 /* shutdown our bulk reads and writes */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200736 mutex_lock(&info->deathwarrant);
737 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
739 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
740 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200741 list_del(tmp);
742 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200744 spin_lock_irq(&info->lock);
745 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Akinobu Mita179e0912006-06-26 00:24:41 -0700747 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
748 list_move(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
750 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
751 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200752 list_del(tmp);
753 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200755 spin_lock_irq(&info->lock);
756 list_add(tmp, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200758 spin_unlock_irq(&info->lock);
759 mutex_unlock(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 stop_command_port(port->serial);
762
Alan Cox95da3102008-07-22 11:09:07 +0100763 tty->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766
Alan Cox95da3102008-07-22 11:09:07 +0100767static int whiteheat_write(struct tty_struct *tty,
768 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct usb_serial *serial = port->serial;
771 struct whiteheat_private *info = usb_get_serial_port_data(port);
772 struct whiteheat_urb_wrap *wrap;
773 struct urb *urb;
774 int result;
775 int bytes;
776 int sent = 0;
777 unsigned long flags;
778 struct list_head *tmp;
779
Harvey Harrison441b62c2008-03-03 16:08:34 -0800780 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800783 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return (0);
785 }
786
787 while (count) {
788 spin_lock_irqsave(&info->lock, flags);
789 if (list_empty(&info->tx_urbs_free)) {
790 spin_unlock_irqrestore(&info->lock, flags);
791 break;
792 }
793 tmp = list_first(&info->tx_urbs_free);
794 list_del(tmp);
795 spin_unlock_irqrestore(&info->lock, flags);
796
797 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
798 urb = wrap->urb;
Alan Cox80359a92008-07-22 11:09:16 +0100799 bytes = (count > port->bulk_out_size) ?
800 port->bulk_out_size : count;
801 memcpy(urb->transfer_buffer, buf + sent, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Alan Cox80359a92008-07-22 11:09:16 +0100803 usb_serial_debug_data(debug, &port->dev,
804 __func__, bytes, urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 urb->dev = serial->dev;
807 urb->transfer_buffer_length = bytes;
808 result = usb_submit_urb(urb, GFP_ATOMIC);
809 if (result) {
Alan Cox80359a92008-07-22 11:09:16 +0100810 err("%s - failed submitting write urb, error %d",
811 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 sent = result;
813 spin_lock_irqsave(&info->lock, flags);
814 list_add(tmp, &info->tx_urbs_free);
815 spin_unlock_irqrestore(&info->lock, flags);
816 break;
817 } else {
818 sent += bytes;
819 count -= bytes;
820 spin_lock_irqsave(&info->lock, flags);
821 list_add(tmp, &info->tx_urbs_submitted);
822 spin_unlock_irqrestore(&info->lock, flags);
823 }
824 }
825
826 return sent;
827}
828
Alan Cox95da3102008-07-22 11:09:07 +0100829static int whiteheat_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
Alan Cox95da3102008-07-22 11:09:07 +0100831 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 struct whiteheat_private *info = usb_get_serial_port_data(port);
833 struct list_head *tmp;
834 int room = 0;
835 unsigned long flags;
836
Harvey Harrison441b62c2008-03-03 16:08:34 -0800837 dbg("%s - port %d", __func__, port->number);
Alan Cox80359a92008-07-22 11:09:16 +0100838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 spin_lock_irqsave(&info->lock, flags);
840 list_for_each(tmp, &info->tx_urbs_free)
841 room++;
842 spin_unlock_irqrestore(&info->lock, flags);
843 room *= port->bulk_out_size;
844
Harvey Harrison441b62c2008-03-03 16:08:34 -0800845 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return (room);
847}
848
Alan Cox80359a92008-07-22 11:09:16 +0100849static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Alan Cox95da3102008-07-22 11:09:07 +0100851 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct whiteheat_private *info = usb_get_serial_port_data(port);
853 unsigned int modem_signals = 0;
854
Harvey Harrison441b62c2008-03-03 16:08:34 -0800855 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 firm_get_dtr_rts(port);
858 if (info->mcr & UART_MCR_DTR)
859 modem_signals |= TIOCM_DTR;
860 if (info->mcr & UART_MCR_RTS)
861 modem_signals |= TIOCM_RTS;
862
863 return modem_signals;
864}
865
Alan Cox80359a92008-07-22 11:09:16 +0100866static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 unsigned int set, unsigned int clear)
868{
Alan Cox95da3102008-07-22 11:09:07 +0100869 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct whiteheat_private *info = usb_get_serial_port_data(port);
871
Harvey Harrison441b62c2008-03-03 16:08:34 -0800872 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (set & TIOCM_RTS)
875 info->mcr |= UART_MCR_RTS;
876 if (set & TIOCM_DTR)
877 info->mcr |= UART_MCR_DTR;
878
879 if (clear & TIOCM_RTS)
880 info->mcr &= ~UART_MCR_RTS;
881 if (clear & TIOCM_DTR)
882 info->mcr &= ~UART_MCR_DTR;
883
884 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
885 firm_set_rts(port, info->mcr & UART_MCR_RTS);
886 return 0;
887}
888
889
Alan Cox80359a92008-07-22 11:09:16 +0100890static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
891 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Alan Cox95da3102008-07-22 11:09:07 +0100893 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 struct serial_struct serstruct;
895 void __user *user_arg = (void __user *)arg;
896
Harvey Harrison441b62c2008-03-03 16:08:34 -0800897 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 switch (cmd) {
Alan Cox80359a92008-07-22 11:09:16 +0100900 case TIOCGSERIAL:
901 memset(&serstruct, 0, sizeof(serstruct));
902 serstruct.type = PORT_16654;
903 serstruct.line = port->serial->minor;
904 serstruct.port = port->number;
905 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
906 serstruct.xmit_fifo_size = port->bulk_out_size;
907 serstruct.custom_divisor = 0;
908 serstruct.baud_base = 460800;
909 serstruct.close_delay = CLOSING_DELAY;
910 serstruct.closing_wait = CLOSING_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Alan Cox80359a92008-07-22 11:09:16 +0100912 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
913 return -EFAULT;
914 break;
915 default:
916 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
919 return -ENOIOCTLCMD;
920}
921
922
Alan Cox80359a92008-07-22 11:09:16 +0100923static void whiteheat_set_termios(struct tty_struct *tty,
924 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Alan Cox95da3102008-07-22 11:09:07 +0100926 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Alan Cox80359a92008-07-22 11:09:16 +0100929static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
930{
Alan Cox95da3102008-07-22 11:09:07 +0100931 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 firm_set_break(port, break_state);
933}
934
935
Alan Cox95da3102008-07-22 11:09:07 +0100936static int whiteheat_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Alan Cox95da3102008-07-22 11:09:07 +0100938 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct whiteheat_private *info = usb_get_serial_port_data(port);
940 struct list_head *tmp;
941 struct whiteheat_urb_wrap *wrap;
942 int chars = 0;
943 unsigned long flags;
944
Harvey Harrison441b62c2008-03-03 16:08:34 -0800945 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 spin_lock_irqsave(&info->lock, flags);
948 list_for_each(tmp, &info->tx_urbs_submitted) {
949 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
950 chars += wrap->urb->transfer_buffer_length;
951 }
952 spin_unlock_irqrestore(&info->lock, flags);
953
Alan Cox80359a92008-07-22 11:09:16 +0100954 dbg("%s - returns %d", __func__, chars);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200955 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958
Alan Cox80359a92008-07-22 11:09:16 +0100959static void whiteheat_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Alan Cox95da3102008-07-22 11:09:07 +0100961 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct whiteheat_private *info = usb_get_serial_port_data(port);
963 unsigned long flags;
964
Harvey Harrison441b62c2008-03-03 16:08:34 -0800965 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 spin_lock_irqsave(&info->lock, flags);
968 info->flags |= THROTTLED;
969 spin_unlock_irqrestore(&info->lock, flags);
970
971 return;
972}
973
974
Alan Cox80359a92008-07-22 11:09:16 +0100975static void whiteheat_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Alan Cox95da3102008-07-22 11:09:07 +0100977 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct whiteheat_private *info = usb_get_serial_port_data(port);
979 int actually_throttled;
980 unsigned long flags;
981
Harvey Harrison441b62c2008-03-03 16:08:34 -0800982 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 spin_lock_irqsave(&info->lock, flags);
985 actually_throttled = info->flags & ACTUALLY_THROTTLED;
986 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
987 spin_unlock_irqrestore(&info->lock, flags);
988
989 if (actually_throttled)
David Howellsc4028952006-11-22 14:57:56 +0000990 rx_data_softint(&info->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 return;
993}
994
995
996/*****************************************************************************
997 * Connect Tech's White Heat callback routines
998 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200999static void command_port_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001001 int status = urb->status;
1002
Harvey Harrison441b62c2008-03-03 16:08:34 -08001003 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001005 if (status) {
1006 dbg("nonzero urb status: %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return;
1008 }
1009}
1010
1011
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001012static void command_port_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Ming Leicdc97792008-02-24 18:41:47 +08001014 struct usb_serial_port *command_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 struct whiteheat_command_private *command_info;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001016 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 unsigned char *data = urb->transfer_buffer;
1018 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Harvey Harrison441b62c2008-03-03 16:08:34 -08001020 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 command_info = usb_get_serial_port_data(command_port);
1023 if (!command_info) {
Alan Cox80359a92008-07-22 11:09:16 +01001024 dbg("%s - command_info is NULL, exiting.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return;
1026 }
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001027 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001028 dbg("%s - nonzero urb status: %d", __func__, status);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001029 if (status != -ENOENT)
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001030 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1031 wake_up(&command_info->wait_command);
1032 return;
1033 }
1034
Alan Cox80359a92008-07-22 11:09:16 +01001035 usb_serial_debug_data(debug, &command_port->dev,
1036 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1039 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001040 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1042 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001043 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 } else if (data[0] == WHITEHEAT_EVENT) {
Alan Cox80359a92008-07-22 11:09:16 +01001045 /* These are unsolicited reports from the firmware, hence no
1046 waiting command to wakeup */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001047 dbg("%s - event received", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
Alan Cox80359a92008-07-22 11:09:16 +01001049 memcpy(command_info->result_buffer, &data[1],
1050 urb->actual_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001052 wake_up(&command_info->wait_command);
Alan Cox80359a92008-07-22 11:09:16 +01001053 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -08001054 dbg("%s - bad reply from firmware", __func__);
Alan Cox80359a92008-07-22 11:09:16 +01001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /* Continue trying to always read */
1057 command_port->read_urb->dev = command_port->serial->dev;
1058 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (result)
Alan Cox80359a92008-07-22 11:09:16 +01001060 dbg("%s - failed resubmitting read urb, error %d",
1061 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064
David Howells7d12e782006-10-05 14:55:46 +01001065static void whiteheat_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Ming Leicdc97792008-02-24 18:41:47 +08001067 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct whiteheat_urb_wrap *wrap;
1069 unsigned char *data = urb->transfer_buffer;
1070 struct whiteheat_private *info = usb_get_serial_port_data(port);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001071 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Harvey Harrison441b62c2008-03-03 16:08:34 -08001073 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 spin_lock(&info->lock);
1076 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1077 if (!wrap) {
1078 spin_unlock(&info->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001079 err("%s - Not my urb!", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return;
1081 }
1082 list_del(&wrap->list);
1083 spin_unlock(&info->lock);
1084
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001085 if (status) {
1086 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001087 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 spin_lock(&info->lock);
1089 list_add(&wrap->list, &info->rx_urbs_free);
1090 spin_unlock(&info->lock);
1091 return;
1092 }
1093
Alan Cox80359a92008-07-22 11:09:16 +01001094 usb_serial_debug_data(debug, &port->dev,
1095 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 spin_lock(&info->lock);
1098 list_add_tail(&wrap->list, &info->rx_urb_q);
1099 if (info->flags & THROTTLED) {
1100 info->flags |= ACTUALLY_THROTTLED;
1101 spin_unlock(&info->lock);
1102 return;
1103 }
1104 spin_unlock(&info->lock);
1105
1106 schedule_work(&info->rx_work);
1107}
1108
1109
David Howells7d12e782006-10-05 14:55:46 +01001110static void whiteheat_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Ming Leicdc97792008-02-24 18:41:47 +08001112 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct whiteheat_private *info = usb_get_serial_port_data(port);
1114 struct whiteheat_urb_wrap *wrap;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001115 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Harvey Harrison441b62c2008-03-03 16:08:34 -08001117 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 spin_lock(&info->lock);
1120 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1121 if (!wrap) {
1122 spin_unlock(&info->lock);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001123 err("%s - Not my urb!", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return;
1125 }
Akinobu Mita179e0912006-06-26 00:24:41 -07001126 list_move(&wrap->list, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 spin_unlock(&info->lock);
1128
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001129 if (status) {
1130 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001131 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return;
1133 }
1134
Pete Zaitcevcf2c7482006-05-22 21:58:49 -07001135 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138
1139/*****************************************************************************
1140 * Connect Tech's White Heat firmware interface
1141 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +01001142static int firm_send_command(struct usb_serial_port *port, __u8 command,
1143 __u8 *data, __u8 datasize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 struct usb_serial_port *command_port;
1146 struct whiteheat_command_private *command_info;
1147 struct whiteheat_private *info;
1148 __u8 *transfer_buffer;
1149 int retval = 0;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001150 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Harvey Harrison441b62c2008-03-03 16:08:34 -08001152 dbg("%s - command %d", __func__, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 command_port = port->serial->port[COMMAND_PORT];
1155 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001156 mutex_lock(&command_info->mutex);
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001157 command_info->command_finished = false;
Alan Cox80359a92008-07-22 11:09:16 +01001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1160 transfer_buffer[0] = command;
Alan Cox80359a92008-07-22 11:09:16 +01001161 memcpy(&transfer_buffer[1], data, datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 command_port->write_urb->transfer_buffer_length = datasize + 1;
1163 command_port->write_urb->dev = port->serial->dev;
Alan Cox80359a92008-07-22 11:09:16 +01001164 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001166 dbg("%s - submit urb failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 goto exit;
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /* wait for the command to complete */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001171 t = wait_event_timeout(command_info->wait_command,
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001172 (bool)command_info->command_finished, COMMAND_TIMEOUT);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001173 if (!t)
1174 usb_kill_urb(command_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001176 if (command_info->command_finished == false) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001177 dbg("%s - command timed out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 retval = -ETIMEDOUT;
1179 goto exit;
1180 }
1181
1182 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001183 dbg("%s - command failed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 retval = -EIO;
1185 goto exit;
1186 }
1187
1188 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001189 dbg("%s - command completed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 switch (command) {
Alan Cox80359a92008-07-22 11:09:16 +01001191 case WHITEHEAT_GET_DTR_RTS:
1192 info = usb_get_serial_port_data(port);
1193 memcpy(&info->mcr, command_info->result_buffer,
1194 sizeof(struct whiteheat_dr_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
1196 }
1197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001199 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return retval;
1201}
1202
1203
Alan Cox80359a92008-07-22 11:09:16 +01001204static int firm_open(struct usb_serial_port *port)
1205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 struct whiteheat_simple open_command;
1207
1208 open_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001209 return firm_send_command(port, WHITEHEAT_OPEN,
1210 (__u8 *)&open_command, sizeof(open_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213
Alan Cox80359a92008-07-22 11:09:16 +01001214static int firm_close(struct usb_serial_port *port)
1215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct whiteheat_simple close_command;
1217
1218 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001219 return firm_send_command(port, WHITEHEAT_CLOSE,
1220 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223
Alan Cox80359a92008-07-22 11:09:16 +01001224static int firm_setup_port(struct tty_struct *tty)
1225{
Alan Cox95da3102008-07-22 11:09:07 +01001226 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct whiteheat_port_settings port_settings;
Alan Cox95da3102008-07-22 11:09:07 +01001228 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 port_settings.port = port->number + 1;
1231
1232 /* get the byte size */
1233 switch (cflag & CSIZE) {
Alan Cox80359a92008-07-22 11:09:16 +01001234 case CS5: port_settings.bits = 5; break;
1235 case CS6: port_settings.bits = 6; break;
1236 case CS7: port_settings.bits = 7; break;
1237 default:
1238 case CS8: port_settings.bits = 8; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001240 dbg("%s - data bits = %d", __func__, port_settings.bits);
Alan Cox80359a92008-07-22 11:09:16 +01001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /* determine the parity */
1243 if (cflag & PARENB)
1244 if (cflag & CMSPAR)
1245 if (cflag & PARODD)
1246 port_settings.parity = WHITEHEAT_PAR_MARK;
1247 else
1248 port_settings.parity = WHITEHEAT_PAR_SPACE;
1249 else
1250 if (cflag & PARODD)
1251 port_settings.parity = WHITEHEAT_PAR_ODD;
1252 else
1253 port_settings.parity = WHITEHEAT_PAR_EVEN;
1254 else
1255 port_settings.parity = WHITEHEAT_PAR_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001256 dbg("%s - parity = %c", __func__, port_settings.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /* figure out the stop bits requested */
1259 if (cflag & CSTOPB)
1260 port_settings.stop = 2;
1261 else
1262 port_settings.stop = 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001263 dbg("%s - stop bits = %d", __func__, port_settings.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /* figure out the flow control settings */
1266 if (cflag & CRTSCTS)
Alan Cox80359a92008-07-22 11:09:16 +01001267 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1268 WHITEHEAT_HFLOW_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 else
1270 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001271 dbg("%s - hardware flow control = %s %s %s %s", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1273 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1274 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1275 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
Alan Cox80359a92008-07-22 11:09:16 +01001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* determine software flow control */
Alan Cox95da3102008-07-22 11:09:07 +01001278 if (I_IXOFF(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1280 else
1281 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001282 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
Alan Cox80359a92008-07-22 11:09:16 +01001283
Alan Cox95da3102008-07-22 11:09:07 +01001284 port_settings.xon = START_CHAR(tty);
1285 port_settings.xoff = STOP_CHAR(tty);
Alan Cox80359a92008-07-22 11:09:16 +01001286 dbg("%s - XON = %2x, XOFF = %2x",
1287 __func__, port_settings.xon, port_settings.xoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /* get the baud rate wanted */
Alan Cox95da3102008-07-22 11:09:07 +01001290 port_settings.baud = tty_get_baud_rate(tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001291 dbg("%s - baud rate = %d", __func__, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Alan Cox01d1df22007-10-18 01:24:23 -07001293 /* fixme: should set validated settings */
Alan Cox95da3102008-07-22 11:09:07 +01001294 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* handle any settings that aren't specified in the tty structure */
1296 port_settings.lloop = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* now send the message to the device */
Alan Cox80359a92008-07-22 11:09:16 +01001299 return firm_send_command(port, WHITEHEAT_SETUP_PORT,
1300 (__u8 *)&port_settings, sizeof(port_settings));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303
Alan Cox80359a92008-07-22 11:09:16 +01001304static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct whiteheat_set_rdb rts_command;
1307
1308 rts_command.port = port->number - port->serial->minor + 1;
1309 rts_command.state = onoff;
Alan Cox80359a92008-07-22 11:09:16 +01001310 return firm_send_command(port, WHITEHEAT_SET_RTS,
1311 (__u8 *)&rts_command, sizeof(rts_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314
Alan Cox80359a92008-07-22 11:09:16 +01001315static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1316{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct whiteheat_set_rdb dtr_command;
1318
1319 dtr_command.port = port->number - port->serial->minor + 1;
1320 dtr_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001321 return firm_send_command(port, WHITEHEAT_SET_DTR,
Alan Cox80359a92008-07-22 11:09:16 +01001322 (__u8 *)&dtr_command, sizeof(dtr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325
Alan Cox80359a92008-07-22 11:09:16 +01001326static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct whiteheat_set_rdb break_command;
1329
1330 break_command.port = port->number - port->serial->minor + 1;
1331 break_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001332 return firm_send_command(port, WHITEHEAT_SET_BREAK,
Alan Cox80359a92008-07-22 11:09:16 +01001333 (__u8 *)&break_command, sizeof(break_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
1336
Alan Cox80359a92008-07-22 11:09:16 +01001337static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct whiteheat_purge purge_command;
1340
1341 purge_command.port = port->number - port->serial->minor + 1;
1342 purge_command.what = rxtx;
Alan Cox80359a92008-07-22 11:09:16 +01001343 return firm_send_command(port, WHITEHEAT_PURGE,
1344 (__u8 *)&purge_command, sizeof(purge_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
1347
Alan Cox80359a92008-07-22 11:09:16 +01001348static int firm_get_dtr_rts(struct usb_serial_port *port)
1349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct whiteheat_simple get_dr_command;
1351
1352 get_dr_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001353 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1354 (__u8 *)&get_dr_command, sizeof(get_dr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
1357
Alan Cox80359a92008-07-22 11:09:16 +01001358static int firm_report_tx_done(struct usb_serial_port *port)
1359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 struct whiteheat_simple close_command;
1361
1362 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001363 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1364 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367
1368/*****************************************************************************
1369 * Connect Tech's White Heat utility functions
1370 *****************************************************************************/
1371static int start_command_port(struct usb_serial *serial)
1372{
1373 struct usb_serial_port *command_port;
1374 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 int retval = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 command_port = serial->port[COMMAND_PORT];
1378 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001379 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (!command_info->port_running) {
1381 /* Work around HCD bugs */
1382 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1383
1384 command_port->read_urb->dev = serial->dev;
1385 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1386 if (retval) {
Alan Cox80359a92008-07-22 11:09:16 +01001387 err("%s - failed submitting read urb, error %d",
1388 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 goto exit;
1390 }
1391 }
1392 command_info->port_running++;
1393
1394exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001395 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return retval;
1397}
1398
1399
1400static void stop_command_port(struct usb_serial *serial)
1401{
1402 struct usb_serial_port *command_port;
1403 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 command_port = serial->port[COMMAND_PORT];
1406 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001407 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 command_info->port_running--;
1409 if (!command_info->port_running)
1410 usb_kill_urb(command_port->read_urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001411 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
1414
1415static int start_port_read(struct usb_serial_port *port)
1416{
1417 struct whiteheat_private *info = usb_get_serial_port_data(port);
1418 struct whiteheat_urb_wrap *wrap;
1419 struct urb *urb;
1420 int retval = 0;
1421 unsigned long flags;
1422 struct list_head *tmp;
1423 struct list_head *tmp2;
1424
1425 spin_lock_irqsave(&info->lock, flags);
1426
1427 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1428 list_del(tmp);
1429 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1430 urb = wrap->urb;
1431 urb->dev = port->serial->dev;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001432 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 retval = usb_submit_urb(urb, GFP_KERNEL);
1434 if (retval) {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001435 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 list_add(tmp, &info->rx_urbs_free);
1437 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1438 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1439 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001440 list_del(tmp);
1441 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001443 spin_lock_irqsave(&info->lock, flags);
1444 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446 break;
1447 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001448 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 list_add(tmp, &info->rx_urbs_submitted);
1450 }
1451
1452 spin_unlock_irqrestore(&info->lock, flags);
1453
1454 return retval;
1455}
1456
1457
Alan Cox80359a92008-07-22 11:09:16 +01001458static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1459 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct whiteheat_urb_wrap *wrap;
1462 struct list_head *tmp;
1463
1464 list_for_each(tmp, head) {
1465 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1466 if (wrap->urb == urb)
1467 return wrap;
1468 }
1469
1470 return NULL;
1471}
1472
1473
1474static struct list_head *list_first(struct list_head *head)
1475{
1476 return head->next;
1477}
1478
1479
David Howellsc4028952006-11-22 14:57:56 +00001480static void rx_data_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
David Howellsc4028952006-11-22 14:57:56 +00001482 struct whiteheat_private *info =
1483 container_of(work, struct whiteheat_private, rx_work);
1484 struct usb_serial_port *port = info->port;
Alan Cox4a90f092008-10-13 10:39:46 +01001485 struct tty_struct *tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 struct whiteheat_urb_wrap *wrap;
1487 struct urb *urb;
1488 unsigned long flags;
1489 struct list_head *tmp;
1490 struct list_head *tmp2;
1491 int result;
1492 int sent = 0;
1493
1494 spin_lock_irqsave(&info->lock, flags);
1495 if (info->flags & THROTTLED) {
1496 spin_unlock_irqrestore(&info->lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +01001497 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499
1500 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1501 list_del(tmp);
1502 spin_unlock_irqrestore(&info->lock, flags);
1503
1504 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1505 urb = wrap->urb;
1506
1507 if (tty && urb->actual_length) {
Alan Cox80359a92008-07-22 11:09:16 +01001508 int len = tty_buffer_request_room(tty,
1509 urb->actual_length);
Alan Cox33f0f882006-01-09 20:54:13 -08001510 /* This stuff can go away now I suspect */
1511 if (unlikely(len < urb->actual_length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 spin_lock_irqsave(&info->lock, flags);
1513 list_add(tmp, &info->rx_urb_q);
1514 spin_unlock_irqrestore(&info->lock, flags);
1515 tty_flip_buffer_push(tty);
1516 schedule_work(&info->rx_work);
Alan Cox4a90f092008-10-13 10:39:46 +01001517 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
Alan Cox33f0f882006-01-09 20:54:13 -08001519 tty_insert_flip_string(tty, urb->transfer_buffer, len);
1520 sent += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
1523 urb->dev = port->serial->dev;
1524 result = usb_submit_urb(urb, GFP_ATOMIC);
1525 if (result) {
Alan Cox80359a92008-07-22 11:09:16 +01001526 err("%s - failed resubmitting read urb, error %d",
1527 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 spin_lock_irqsave(&info->lock, flags);
1529 list_add(tmp, &info->rx_urbs_free);
1530 continue;
1531 }
1532
1533 spin_lock_irqsave(&info->lock, flags);
1534 list_add(tmp, &info->rx_urbs_submitted);
1535 }
1536 spin_unlock_irqrestore(&info->lock, flags);
1537
1538 if (sent)
1539 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001540out:
1541 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544
1545/*****************************************************************************
1546 * Connect Tech's White Heat module functions
1547 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +01001548static int __init whiteheat_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 int retval;
1551 retval = usb_serial_register(&whiteheat_fake_device);
1552 if (retval)
1553 goto failed_fake_register;
1554 retval = usb_serial_register(&whiteheat_device);
1555 if (retval)
1556 goto failed_device_register;
1557 retval = usb_register(&whiteheat_driver);
1558 if (retval)
1559 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001560 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1561 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return 0;
1563failed_usb_register:
1564 usb_serial_deregister(&whiteheat_device);
1565failed_device_register:
1566 usb_serial_deregister(&whiteheat_fake_device);
1567failed_fake_register:
1568 return retval;
1569}
1570
1571
Alan Cox80359a92008-07-22 11:09:16 +01001572static void __exit whiteheat_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Alan Cox80359a92008-07-22 11:09:16 +01001574 usb_deregister(&whiteheat_driver);
1575 usb_serial_deregister(&whiteheat_fake_device);
1576 usb_serial_deregister(&whiteheat_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579
1580module_init(whiteheat_init);
1581module_exit(whiteheat_exit);
1582
Alan Cox80359a92008-07-22 11:09:16 +01001583MODULE_AUTHOR(DRIVER_AUTHOR);
1584MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585MODULE_LICENSE("GPL");
1586
David Woodhouseec6752f2008-05-31 01:35:29 +03001587MODULE_FIRMWARE("whiteheat.fw");
1588MODULE_FIRMWARE("whiteheat_loader.fw");
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590module_param(urb_pool_size, int, 0);
1591MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1592
1593module_param(debug, bool, S_IRUGO | S_IWUSR);
1594MODULE_PARM_DESC(debug, "Debug enabled or not");