blob: 0d06d7ca86f93e36731df7a807ccc49dbedb8dc8 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/tty.h>
24#include <linux/tty_driver.h>
25#include <linux/tty_flip.h>
26#include <linux/module.h>
27#include <linux/spinlock.h>
Oliver Neukum08a2b3b2007-05-24 13:52:51 +020028#include <linux/mutex.h>
Alan Cox80359a92008-07-22 11:09:16 +010029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/termbits.h>
31#include <linux/usb.h>
32#include <linux/serial_reg.h>
33#include <linux/serial.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070034#include <linux/usb/serial.h>
David Woodhouseec6752f2008-05-31 01:35:29 +030035#include <linux/firmware.h>
36#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "whiteheat.h" /* WhiteHEAT specific commands */
38
Rusty Russell90ab5ee2012-01-13 09:32:20 +103039static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#ifndef CMSPAR
42#define CMSPAR 0
43#endif
44
45/*
46 * Version Information
47 */
48#define DRIVER_VERSION "v2.0"
49#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
50#define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
51
52#define CONNECT_TECH_VENDOR_ID 0x0710
53#define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
54#define CONNECT_TECH_WHITE_HEAT_ID 0x8001
55
56/*
57 ID tables for whiteheat are unusual, because we want to different
58 things for different versions of the device. Eventually, this
59 will be doable from a single table. But, for now, we define two
60 separate ID tables, and then a third table that combines them
61 just for the purpose of exporting the autoloading information.
62*/
Németh Márton7d40d7e2010-01-10 15:34:24 +010063static const struct usb_device_id id_table_std[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
65 { } /* Terminating entry */
66};
67
Németh Márton7d40d7e2010-01-10 15:34:24 +010068static const struct usb_device_id id_table_prerenumeration[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
70 { } /* Terminating entry */
71};
72
Németh Márton7d40d7e2010-01-10 15:34:24 +010073static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
75 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
76 { } /* Terminating entry */
77};
78
Alan Cox80359a92008-07-22 11:09:16 +010079MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81static struct usb_driver whiteheat_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 .name = "whiteheat",
83 .probe = usb_serial_probe,
84 .disconnect = usb_serial_disconnect,
85 .id_table = id_table_combined,
86};
87
88/* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
Alan Cox80359a92008-07-22 11:09:16 +010089static int whiteheat_firmware_download(struct usb_serial *serial,
90 const struct usb_device_id *id);
91static int whiteheat_firmware_attach(struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/* function prototypes for the Connect Tech WhiteHEAT serial converter */
Alan Cox80359a92008-07-22 11:09:16 +010094static int whiteheat_attach(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040095static void whiteheat_release(struct usb_serial *serial);
Alan Cox80359a92008-07-22 11:09:16 +010096static int whiteheat_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -070097 struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010098static void whiteheat_close(struct usb_serial_port *port);
Alan Cox80359a92008-07-22 11:09:16 +010099static int whiteheat_write(struct tty_struct *tty,
100 struct usb_serial_port *port,
101 const unsigned char *buf, int count);
102static int whiteheat_write_room(struct tty_struct *tty);
Alan Cox00a0d0d2011-02-14 16:27:06 +0000103static int whiteheat_ioctl(struct tty_struct *tty,
Alan Cox80359a92008-07-22 11:09:16 +0100104 unsigned int cmd, unsigned long arg);
105static void whiteheat_set_termios(struct tty_struct *tty,
106 struct usb_serial_port *port, struct ktermios *old);
Alan Cox60b33c12011-02-14 16:26:14 +0000107static int whiteheat_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000108static int whiteheat_tiocmset(struct tty_struct *tty,
Alan Cox80359a92008-07-22 11:09:16 +0100109 unsigned int set, unsigned int clear);
110static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
111static int whiteheat_chars_in_buffer(struct tty_struct *tty);
112static void whiteheat_throttle(struct tty_struct *tty);
113static void whiteheat_unthrottle(struct tty_struct *tty);
114static void whiteheat_read_callback(struct urb *urb);
115static void whiteheat_write_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700117static struct usb_serial_driver whiteheat_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700118 .driver = {
119 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700120 .name = "whiteheatnofirm",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700121 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700122 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .id_table = id_table_prerenumeration,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .num_ports = 1,
125 .probe = whiteheat_firmware_download,
126 .attach = whiteheat_firmware_attach,
127};
128
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700129static struct usb_serial_driver whiteheat_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700130 .driver = {
131 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700132 .name = "whiteheat",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700133 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700134 .description = "Connect Tech - WhiteHEAT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .id_table = id_table_std,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .num_ports = 4,
137 .attach = whiteheat_attach,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400138 .release = whiteheat_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .open = whiteheat_open,
140 .close = whiteheat_close,
141 .write = whiteheat_write,
142 .write_room = whiteheat_write_room,
143 .ioctl = whiteheat_ioctl,
144 .set_termios = whiteheat_set_termios,
145 .break_ctl = whiteheat_break_ctl,
146 .tiocmget = whiteheat_tiocmget,
147 .tiocmset = whiteheat_tiocmset,
148 .chars_in_buffer = whiteheat_chars_in_buffer,
149 .throttle = whiteheat_throttle,
150 .unthrottle = whiteheat_unthrottle,
151 .read_bulk_callback = whiteheat_read_callback,
152 .write_bulk_callback = whiteheat_write_callback,
153};
154
Alan Stern29618e92012-02-23 14:57:32 -0500155static struct usb_serial_driver * const serial_drivers[] = {
156 &whiteheat_fake_device, &whiteheat_device, NULL
157};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159struct whiteheat_command_private {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200160 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 __u8 port_running;
162 __u8 command_finished;
Alan Cox80359a92008-07-22 11:09:16 +0100163 wait_queue_head_t wait_command; /* for handling sleeping whilst
164 waiting for a command to
165 finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 __u8 result_buffer[64];
167};
168
169
170#define THROTTLED 0x01
171#define ACTUALLY_THROTTLED 0x02
172
173static int urb_pool_size = 8;
174
175struct whiteheat_urb_wrap {
176 struct list_head list;
177 struct urb *urb;
178};
179
180struct whiteheat_private {
181 spinlock_t lock;
182 __u8 flags;
Alan Coxa5b6f602008-04-08 17:16:06 +0100183 __u8 mcr; /* FIXME: no locking on mcr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct list_head rx_urbs_free;
185 struct list_head rx_urbs_submitted;
186 struct list_head rx_urb_q;
187 struct work_struct rx_work;
David Howellsc4028952006-11-22 14:57:56 +0000188 struct usb_serial_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct list_head tx_urbs_free;
190 struct list_head tx_urbs_submitted;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200191 struct mutex deathwarrant;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
194
195/* local function prototypes */
196static int start_command_port(struct usb_serial *serial);
197static void stop_command_port(struct usb_serial *serial);
David Howells7d12e782006-10-05 14:55:46 +0100198static void command_port_write_callback(struct urb *urb);
199static void command_port_read_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201static int start_port_read(struct usb_serial_port *port);
Alan Cox80359a92008-07-22 11:09:16 +0100202static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
203 struct list_head *head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static struct list_head *list_first(struct list_head *head);
David Howellsc4028952006-11-22 14:57:56 +0000205static void rx_data_softint(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Alan Cox80359a92008-07-22 11:09:16 +0100207static int firm_send_command(struct usb_serial_port *port, __u8 command,
208 __u8 *data, __u8 datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static int firm_open(struct usb_serial_port *port);
210static int firm_close(struct usb_serial_port *port);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700211static void firm_setup_port(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
213static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
214static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
215static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
216static int firm_get_dtr_rts(struct usb_serial_port *port);
217static int firm_report_tx_done(struct usb_serial_port *port);
218
219
220#define COMMAND_PORT 4
221#define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
222#define COMMAND_TIMEOUT_MS 2000
223#define CLOSING_DELAY (30 * HZ)
224
225
226/*****************************************************************************
227 * Connect Tech's White Heat prerenumeration driver functions
228 *****************************************************************************/
229
230/* steps to download the firmware to the WhiteHEAT device:
231 - hold the reset (by writing to the reset bit of the CPUCS register)
232 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
233 - release the reset (by writing to the CPUCS register)
234 - download the WH.HEX file for all addresses greater than 0x1b3f using
235 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
236 - hold the reset
237 - download the WH.HEX file for all addresses less than 0x1b40 using
238 VENDOR_REQUEST_ANCHOR_LOAD
239 - release the reset
240 - device renumerated itself and comes up as new device id with all
241 firmware download completed.
242*/
Alan Cox80359a92008-07-22 11:09:16 +0100243static int whiteheat_firmware_download(struct usb_serial *serial,
244 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
David Woodhouseec6752f2008-05-31 01:35:29 +0300246 int response, ret = -ENOENT;
247 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
248 const struct ihex_binrec *record;
249
Harvey Harrison441b62c2008-03-03 16:08:34 -0800250 dbg("%s", __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300251
252 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
253 &serial->dev->dev)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700254 dev_err(&serial->dev->dev,
255 "%s - request \"whiteheat.fw\" failed\n", __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300256 goto out;
257 }
258 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
259 &serial->dev->dev)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700260 dev_err(&serial->dev->dev,
261 "%s - request \"whiteheat_loader.fw\" failed\n",
262 __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300263 goto out;
264 }
265 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 response = ezusb_set_reset (serial, 1);
267
David Woodhouseec6752f2008-05-31 01:35:29 +0300268 record = (const struct ihex_binrec *)loader_fw->data;
269 while (record) {
270 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
271 (unsigned char *)record->data,
272 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700274 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
275 "failed for loader (%d %04X %p %d)\n",
276 __func__, response, be32_to_cpu(record->addr),
277 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
279 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300280 record = ihex_next_binrec(record);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
Alan Cox80359a92008-07-22 11:09:16 +0100283 response = ezusb_set_reset(serial, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
David Woodhouseec6752f2008-05-31 01:35:29 +0300285 record = (const struct ihex_binrec *)firmware_fw->data;
286 while (record && be32_to_cpu(record->addr) < 0x1b40)
287 record = ihex_next_binrec(record);
288 while (record) {
289 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
290 (unsigned char *)record->data,
291 be16_to_cpu(record->len), 0xa3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700293 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
294 "failed for first firmware step "
295 "(%d %04X %p %d)\n", __func__, response,
296 be32_to_cpu(record->addr), record->data,
297 be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 break;
299 }
300 ++record;
301 }
Alan Cox80359a92008-07-22 11:09:16 +0100302
303 response = ezusb_set_reset(serial, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
David Woodhouseec6752f2008-05-31 01:35:29 +0300305 record = (const struct ihex_binrec *)firmware_fw->data;
306 while (record && be32_to_cpu(record->addr) < 0x1b40) {
307 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
308 (unsigned char *)record->data,
309 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700311 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
312 "failed for second firmware step "
313 "(%d %04X %p %d)\n", __func__, response,
314 be32_to_cpu(record->addr), record->data,
315 be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317 }
318 ++record;
319 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300320 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 response = ezusb_set_reset (serial, 0);
David Woodhouseec6752f2008-05-31 01:35:29 +0300322 out:
323 release_firmware(loader_fw);
324 release_firmware(firmware_fw);
325 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328
Alan Cox80359a92008-07-22 11:09:16 +0100329static int whiteheat_firmware_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 /* We want this device to fail to have a driver assigned to it */
332 return 1;
333}
334
335
336/*****************************************************************************
337 * Connect Tech's White Heat serial driver functions
338 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +0100339static int whiteheat_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 struct usb_serial_port *command_port;
342 struct whiteheat_command_private *command_info;
343 struct usb_serial_port *port;
344 struct whiteheat_private *info;
345 struct whiteheat_hw_info *hw_info;
346 int pipe;
347 int ret;
348 int alen;
349 __u8 *command;
350 __u8 *result;
351 int i;
352 int j;
353 struct urb *urb;
354 int buf_size;
355 struct whiteheat_urb_wrap *wrap;
356 struct list_head *tmp;
357
358 command_port = serial->port[COMMAND_PORT];
359
Alan Cox80359a92008-07-22 11:09:16 +0100360 pipe = usb_sndbulkpipe(serial->dev,
361 command_port->bulk_out_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 command = kmalloc(2, GFP_KERNEL);
363 if (!command)
364 goto no_command_buffer;
365 command[0] = WHITEHEAT_GET_HW_INFO;
366 command[1] = 0;
Alan Cox80359a92008-07-22 11:09:16 +0100367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
369 if (!result)
370 goto no_result_buffer;
371 /*
372 * When the module is reloaded the firmware is still there and
373 * the endpoints are still in the usb core unchanged. This is the
Alan Cox80359a92008-07-22 11:09:16 +0100374 * unlinking bug in disguise. Same for the call below.
375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100377 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
378 &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (ret) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700380 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
381 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400383 } else if (alen != 2) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700384 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
385 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto no_firmware;
387 }
388
Alan Cox80359a92008-07-22 11:09:16 +0100389 pipe = usb_rcvbulkpipe(serial->dev,
390 command_port->bulk_in_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* See the comment on the usb_clear_halt() above */
392 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100393 ret = usb_bulk_msg(serial->dev, pipe, result,
394 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (ret) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700396 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
397 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400399 } else if (alen != sizeof(*hw_info) + 1) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700400 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
401 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 goto no_firmware;
403 } else if (result[0] != command[0]) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700404 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
405 serial->type->description, result[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto no_firmware;
407 }
408
409 hw_info = (struct whiteheat_hw_info *)&result[1];
410
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700411 dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
412 serial->type->description, DRIVER_VERSION,
413 hw_info->sw_major_rev, hw_info->sw_minor_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 for (i = 0; i < serial->num_ports; i++) {
416 port = serial->port[i];
417
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800418 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (info == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700420 dev_err(&port->dev,
421 "%s: Out of memory for port structures\n",
422 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 goto no_private;
424 }
425
426 spin_lock_init(&info->lock);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200427 mutex_init(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 info->flags = 0;
429 info->mcr = 0;
David Howellsc4028952006-11-22 14:57:56 +0000430 INIT_WORK(&info->rx_work, rx_data_softint);
431 info->port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 INIT_LIST_HEAD(&info->rx_urbs_free);
434 INIT_LIST_HEAD(&info->rx_urbs_submitted);
435 INIT_LIST_HEAD(&info->rx_urb_q);
436 INIT_LIST_HEAD(&info->tx_urbs_free);
437 INIT_LIST_HEAD(&info->tx_urbs_submitted);
438
439 for (j = 0; j < urb_pool_size; j++) {
440 urb = usb_alloc_urb(0, GFP_KERNEL);
441 if (!urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700442 dev_err(&port->dev, "No free urbs available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 goto no_rx_urb;
444 }
445 buf_size = port->read_urb->transfer_buffer_length;
446 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
447 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700448 dev_err(&port->dev,
449 "Couldn't allocate urb buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 goto no_rx_buf;
451 }
452 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
453 if (!wrap) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700454 dev_err(&port->dev,
455 "Couldn't allocate urb wrapper\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto no_rx_wrap;
457 }
458 usb_fill_bulk_urb(urb, serial->dev,
459 usb_rcvbulkpipe(serial->dev,
460 port->bulk_in_endpointAddress),
461 urb->transfer_buffer, buf_size,
462 whiteheat_read_callback, port);
463 wrap->urb = urb;
464 list_add(&wrap->list, &info->rx_urbs_free);
465
466 urb = usb_alloc_urb(0, GFP_KERNEL);
467 if (!urb) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700468 dev_err(&port->dev, "No free urbs available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto no_tx_urb;
470 }
471 buf_size = port->write_urb->transfer_buffer_length;
472 urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
473 if (!urb->transfer_buffer) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700474 dev_err(&port->dev,
475 "Couldn't allocate urb buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto no_tx_buf;
477 }
478 wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
479 if (!wrap) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700480 dev_err(&port->dev,
481 "Couldn't allocate urb wrapper\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto no_tx_wrap;
483 }
484 usb_fill_bulk_urb(urb, serial->dev,
485 usb_sndbulkpipe(serial->dev,
486 port->bulk_out_endpointAddress),
487 urb->transfer_buffer, buf_size,
488 whiteheat_write_callback, port);
489 wrap->urb = urb;
490 list_add(&wrap->list, &info->tx_urbs_free);
491 }
492
493 usb_set_serial_port_data(port, info);
494 }
495
Alan Cox80359a92008-07-22 11:09:16 +0100496 command_info = kmalloc(sizeof(struct whiteheat_command_private),
497 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (command_info == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700499 dev_err(&serial->dev->dev,
500 "%s: Out of memory for port structures\n",
501 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto no_command_private;
503 }
504
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200505 mutex_init(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 command_info->port_running = 0;
507 init_waitqueue_head(&command_info->wait_command);
508 usb_set_serial_port_data(command_port, command_info);
509 command_port->write_urb->complete = command_port_write_callback;
510 command_port->read_urb->complete = command_port_read_callback;
511 kfree(result);
512 kfree(command);
513
514 return 0;
515
516no_firmware:
517 /* Firmware likely not running */
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700518 dev_err(&serial->dev->dev,
519 "%s: Unable to retrieve firmware version, try replugging\n",
520 serial->type->description);
521 dev_err(&serial->dev->dev,
522 "%s: If the firmware is not running (status led not blinking)\n",
523 serial->type->description);
524 dev_err(&serial->dev->dev,
525 "%s: please contact support@connecttech.com\n",
526 serial->type->description);
Jesper Juhl67ca0282006-04-23 19:59:23 +0200527 kfree(result);
Johan Hovold02c6b1f2012-10-25 10:29:01 +0200528 kfree(command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -ENODEV;
530
531no_command_private:
532 for (i = serial->num_ports - 1; i >= 0; i--) {
533 port = serial->port[i];
534 info = usb_get_serial_port_data(port);
535 for (j = urb_pool_size - 1; j >= 0; j--) {
536 tmp = list_first(&info->tx_urbs_free);
537 list_del(tmp);
538 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
539 urb = wrap->urb;
540 kfree(wrap);
541no_tx_wrap:
542 kfree(urb->transfer_buffer);
543no_tx_buf:
544 usb_free_urb(urb);
545no_tx_urb:
546 tmp = list_first(&info->rx_urbs_free);
547 list_del(tmp);
548 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
549 urb = wrap->urb;
550 kfree(wrap);
551no_rx_wrap:
552 kfree(urb->transfer_buffer);
553no_rx_buf:
554 usb_free_urb(urb);
555no_rx_urb:
556 ;
557 }
558 kfree(info);
559no_private:
560 ;
561 }
562 kfree(result);
563no_result_buffer:
564 kfree(command);
565no_command_buffer:
566 return -ENOMEM;
567}
568
569
Alan Sternf9c99bb2009-06-02 11:53:55 -0400570static void whiteheat_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct usb_serial_port *command_port;
573 struct usb_serial_port *port;
574 struct whiteheat_private *info;
575 struct whiteheat_urb_wrap *wrap;
576 struct urb *urb;
577 struct list_head *tmp;
578 struct list_head *tmp2;
579 int i;
580
Harvey Harrison441b62c2008-03-03 16:08:34 -0800581 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* free up our private data for our command port */
584 command_port = serial->port[COMMAND_PORT];
Alan Cox80359a92008-07-22 11:09:16 +0100585 kfree(usb_get_serial_port_data(command_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 for (i = 0; i < serial->num_ports; i++) {
588 port = serial->port[i];
589 info = usb_get_serial_port_data(port);
590 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
591 list_del(tmp);
592 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
593 urb = wrap->urb;
594 kfree(wrap);
595 kfree(urb->transfer_buffer);
596 usb_free_urb(urb);
597 }
598 list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
599 list_del(tmp);
600 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
601 urb = wrap->urb;
602 kfree(wrap);
603 kfree(urb->transfer_buffer);
604 usb_free_urb(urb);
605 }
606 kfree(info);
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Alan Coxa509a7e2009-09-19 13:13:26 -0700610static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Harvey Harrison441b62c2008-03-03 16:08:34 -0800614 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 retval = start_command_port(port->serial);
617 if (retval)
618 goto exit;
619
Alan Cox95da3102008-07-22 11:09:07 +0100620 if (tty)
621 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* send an open port command */
624 retval = firm_open(port);
625 if (retval) {
626 stop_command_port(port->serial);
627 goto exit;
628 }
629
630 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
631 if (retval) {
632 firm_close(port);
633 stop_command_port(port->serial);
634 goto exit;
635 }
636
Alan Cox72e27412008-07-22 11:09:29 +0100637 if (tty)
638 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 /* Work around HCD bugs */
641 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
642 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
643
644 /* Start reading from the device */
645 retval = start_port_read(port);
646 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700647 dev_err(&port->dev,
648 "%s - failed submitting read urb, error %d\n",
649 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 firm_close(port);
651 stop_command_port(port->serial);
652 goto exit;
653 }
654
655exit:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800656 dbg("%s - exit, retval = %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return retval;
658}
659
660
Alan Cox335f8512009-06-11 12:26:29 +0100661static void whiteheat_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 struct whiteheat_private *info = usb_get_serial_port_data(port);
664 struct whiteheat_urb_wrap *wrap;
665 struct urb *urb;
666 struct list_head *tmp;
667 struct list_head *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Harvey Harrison441b62c2008-03-03 16:08:34 -0800669 dbg("%s - port %d", __func__, port->number);
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 firm_report_tx_done(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 firm_close(port);
673
674 /* shutdown our bulk reads and writes */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200675 mutex_lock(&info->deathwarrant);
676 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
678 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
679 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200680 list_del(tmp);
681 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200683 spin_lock_irq(&info->lock);
684 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
Akinobu Mita179e0912006-06-26 00:24:41 -0700686 list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
687 list_move(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
689 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
690 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200691 list_del(tmp);
692 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200694 spin_lock_irq(&info->lock);
695 list_add(tmp, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200697 spin_unlock_irq(&info->lock);
698 mutex_unlock(&info->deathwarrant);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 stop_command_port(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702
Alan Cox95da3102008-07-22 11:09:07 +0100703static int whiteheat_write(struct tty_struct *tty,
704 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct whiteheat_private *info = usb_get_serial_port_data(port);
707 struct whiteheat_urb_wrap *wrap;
708 struct urb *urb;
709 int result;
710 int bytes;
711 int sent = 0;
712 unsigned long flags;
713 struct list_head *tmp;
714
Harvey Harrison441b62c2008-03-03 16:08:34 -0800715 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if (count == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800718 dbg("%s - write request of 0 bytes", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return (0);
720 }
721
722 while (count) {
723 spin_lock_irqsave(&info->lock, flags);
724 if (list_empty(&info->tx_urbs_free)) {
725 spin_unlock_irqrestore(&info->lock, flags);
726 break;
727 }
728 tmp = list_first(&info->tx_urbs_free);
729 list_del(tmp);
730 spin_unlock_irqrestore(&info->lock, flags);
731
732 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
733 urb = wrap->urb;
Alan Cox80359a92008-07-22 11:09:16 +0100734 bytes = (count > port->bulk_out_size) ?
735 port->bulk_out_size : count;
736 memcpy(urb->transfer_buffer, buf + sent, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Alan Cox80359a92008-07-22 11:09:16 +0100738 usb_serial_debug_data(debug, &port->dev,
739 __func__, bytes, urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 urb->transfer_buffer_length = bytes;
742 result = usb_submit_urb(urb, GFP_ATOMIC);
743 if (result) {
Johan Hovold22a416c2012-02-10 13:20:51 +0100744 dev_err_console(port,
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700745 "%s - failed submitting write urb, error %d\n",
746 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 sent = result;
748 spin_lock_irqsave(&info->lock, flags);
749 list_add(tmp, &info->tx_urbs_free);
750 spin_unlock_irqrestore(&info->lock, flags);
751 break;
752 } else {
753 sent += bytes;
754 count -= bytes;
755 spin_lock_irqsave(&info->lock, flags);
756 list_add(tmp, &info->tx_urbs_submitted);
757 spin_unlock_irqrestore(&info->lock, flags);
758 }
759 }
760
761 return sent;
762}
763
Alan Cox95da3102008-07-22 11:09:07 +0100764static int whiteheat_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Alan Cox95da3102008-07-22 11:09:07 +0100766 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct whiteheat_private *info = usb_get_serial_port_data(port);
768 struct list_head *tmp;
769 int room = 0;
770 unsigned long flags;
771
Harvey Harrison441b62c2008-03-03 16:08:34 -0800772 dbg("%s - port %d", __func__, port->number);
Alan Cox80359a92008-07-22 11:09:16 +0100773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 spin_lock_irqsave(&info->lock, flags);
775 list_for_each(tmp, &info->tx_urbs_free)
776 room++;
777 spin_unlock_irqrestore(&info->lock, flags);
778 room *= port->bulk_out_size;
779
Harvey Harrison441b62c2008-03-03 16:08:34 -0800780 dbg("%s - returns %d", __func__, room);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return (room);
782}
783
Alan Cox60b33c12011-02-14 16:26:14 +0000784static int whiteheat_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Alan Cox95da3102008-07-22 11:09:07 +0100786 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct whiteheat_private *info = usb_get_serial_port_data(port);
788 unsigned int modem_signals = 0;
789
Harvey Harrison441b62c2008-03-03 16:08:34 -0800790 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 firm_get_dtr_rts(port);
793 if (info->mcr & UART_MCR_DTR)
794 modem_signals |= TIOCM_DTR;
795 if (info->mcr & UART_MCR_RTS)
796 modem_signals |= TIOCM_RTS;
797
798 return modem_signals;
799}
800
Alan Cox20b9d172011-02-14 16:26:50 +0000801static int whiteheat_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 unsigned int set, unsigned int clear)
803{
Alan Cox95da3102008-07-22 11:09:07 +0100804 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct whiteheat_private *info = usb_get_serial_port_data(port);
806
Harvey Harrison441b62c2008-03-03 16:08:34 -0800807 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (set & TIOCM_RTS)
810 info->mcr |= UART_MCR_RTS;
811 if (set & TIOCM_DTR)
812 info->mcr |= UART_MCR_DTR;
813
814 if (clear & TIOCM_RTS)
815 info->mcr &= ~UART_MCR_RTS;
816 if (clear & TIOCM_DTR)
817 info->mcr &= ~UART_MCR_DTR;
818
819 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
820 firm_set_rts(port, info->mcr & UART_MCR_RTS);
821 return 0;
822}
823
824
Alan Cox00a0d0d2011-02-14 16:27:06 +0000825static int whiteheat_ioctl(struct tty_struct *tty,
Alan Cox80359a92008-07-22 11:09:16 +0100826 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Alan Cox95da3102008-07-22 11:09:07 +0100828 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct serial_struct serstruct;
830 void __user *user_arg = (void __user *)arg;
831
Harvey Harrison441b62c2008-03-03 16:08:34 -0800832 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 switch (cmd) {
Alan Cox80359a92008-07-22 11:09:16 +0100835 case TIOCGSERIAL:
836 memset(&serstruct, 0, sizeof(serstruct));
837 serstruct.type = PORT_16654;
838 serstruct.line = port->serial->minor;
839 serstruct.port = port->number;
840 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
841 serstruct.xmit_fifo_size = port->bulk_out_size;
842 serstruct.custom_divisor = 0;
843 serstruct.baud_base = 460800;
844 serstruct.close_delay = CLOSING_DELAY;
845 serstruct.closing_wait = CLOSING_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Alan Cox80359a92008-07-22 11:09:16 +0100847 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
848 return -EFAULT;
849 break;
850 default:
851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
854 return -ENOIOCTLCMD;
855}
856
857
Alan Cox80359a92008-07-22 11:09:16 +0100858static void whiteheat_set_termios(struct tty_struct *tty,
859 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Alan Cox95da3102008-07-22 11:09:07 +0100861 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Alan Cox80359a92008-07-22 11:09:16 +0100864static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
865{
Alan Cox95da3102008-07-22 11:09:07 +0100866 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 firm_set_break(port, break_state);
868}
869
870
Alan Cox95da3102008-07-22 11:09:07 +0100871static int whiteheat_chars_in_buffer(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Alan Cox95da3102008-07-22 11:09:07 +0100873 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 struct whiteheat_private *info = usb_get_serial_port_data(port);
875 struct list_head *tmp;
876 struct whiteheat_urb_wrap *wrap;
877 int chars = 0;
878 unsigned long flags;
879
Harvey Harrison441b62c2008-03-03 16:08:34 -0800880 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 spin_lock_irqsave(&info->lock, flags);
883 list_for_each(tmp, &info->tx_urbs_submitted) {
884 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
885 chars += wrap->urb->transfer_buffer_length;
886 }
887 spin_unlock_irqrestore(&info->lock, flags);
888
Alan Cox80359a92008-07-22 11:09:16 +0100889 dbg("%s - returns %d", __func__, chars);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200890 return chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893
Alan Cox80359a92008-07-22 11:09:16 +0100894static void whiteheat_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Alan Cox95da3102008-07-22 11:09:07 +0100896 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct whiteheat_private *info = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Harvey Harrison441b62c2008-03-03 16:08:34 -0800899 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Oliver Neukum63832512009-10-07 10:50:23 +0200901 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 info->flags |= THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +0200903 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906
Alan Cox80359a92008-07-22 11:09:16 +0100907static void whiteheat_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Alan Cox95da3102008-07-22 11:09:07 +0100909 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct whiteheat_private *info = usb_get_serial_port_data(port);
911 int actually_throttled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Harvey Harrison441b62c2008-03-03 16:08:34 -0800913 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Oliver Neukum63832512009-10-07 10:50:23 +0200915 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 actually_throttled = info->flags & ACTUALLY_THROTTLED;
917 info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
Oliver Neukum63832512009-10-07 10:50:23 +0200918 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (actually_throttled)
David Howellsc4028952006-11-22 14:57:56 +0000921 rx_data_softint(&info->rx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924
925/*****************************************************************************
926 * Connect Tech's White Heat callback routines
927 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200928static void command_port_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700930 int status = urb->status;
931
Harvey Harrison441b62c2008-03-03 16:08:34 -0800932 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700934 if (status) {
935 dbg("nonzero urb status: %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return;
937 }
938}
939
940
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200941static void command_port_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Ming Leicdc97792008-02-24 18:41:47 +0800943 struct usb_serial_port *command_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct whiteheat_command_private *command_info;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700945 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 unsigned char *data = urb->transfer_buffer;
947 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Harvey Harrison441b62c2008-03-03 16:08:34 -0800949 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 command_info = usb_get_serial_port_data(command_port);
952 if (!command_info) {
Alan Cox80359a92008-07-22 11:09:16 +0100953 dbg("%s - command_info is NULL, exiting.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return;
955 }
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700956 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800957 dbg("%s - nonzero urb status: %d", __func__, status);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700958 if (status != -ENOENT)
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200959 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
960 wake_up(&command_info->wait_command);
961 return;
962 }
963
Alan Cox80359a92008-07-22 11:09:16 +0100964 usb_serial_debug_data(debug, &command_port->dev,
965 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
968 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200969 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
971 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200972 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 } else if (data[0] == WHITEHEAT_EVENT) {
Alan Cox80359a92008-07-22 11:09:16 +0100974 /* These are unsolicited reports from the firmware, hence no
975 waiting command to wakeup */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800976 dbg("%s - event received", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
Alan Cox80359a92008-07-22 11:09:16 +0100978 memcpy(command_info->result_buffer, &data[1],
979 urb->actual_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200981 wake_up(&command_info->wait_command);
Alan Cox80359a92008-07-22 11:09:16 +0100982 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800983 dbg("%s - bad reply from firmware", __func__);
Alan Cox80359a92008-07-22 11:09:16 +0100984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (result)
Alan Cox80359a92008-07-22 11:09:16 +0100988 dbg("%s - failed resubmitting read urb, error %d",
989 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992
David Howells7d12e782006-10-05 14:55:46 +0100993static void whiteheat_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Ming Leicdc97792008-02-24 18:41:47 +0800995 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct whiteheat_urb_wrap *wrap;
997 unsigned char *data = urb->transfer_buffer;
998 struct whiteheat_private *info = usb_get_serial_port_data(port);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700999 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
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(&info->lock);
1004 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1005 if (!wrap) {
1006 spin_unlock(&info->lock);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001007 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return;
1009 }
1010 list_del(&wrap->list);
1011 spin_unlock(&info->lock);
1012
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001013 if (status) {
1014 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001015 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 spin_lock(&info->lock);
1017 list_add(&wrap->list, &info->rx_urbs_free);
1018 spin_unlock(&info->lock);
1019 return;
1020 }
1021
Alan Cox80359a92008-07-22 11:09:16 +01001022 usb_serial_debug_data(debug, &port->dev,
1023 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 spin_lock(&info->lock);
1026 list_add_tail(&wrap->list, &info->rx_urb_q);
1027 if (info->flags & THROTTLED) {
1028 info->flags |= ACTUALLY_THROTTLED;
1029 spin_unlock(&info->lock);
1030 return;
1031 }
1032 spin_unlock(&info->lock);
1033
1034 schedule_work(&info->rx_work);
1035}
1036
1037
David Howells7d12e782006-10-05 14:55:46 +01001038static void whiteheat_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Ming Leicdc97792008-02-24 18:41:47 +08001040 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 struct whiteheat_private *info = usb_get_serial_port_data(port);
1042 struct whiteheat_urb_wrap *wrap;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001043 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Harvey Harrison441b62c2008-03-03 16:08:34 -08001045 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 spin_lock(&info->lock);
1048 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1049 if (!wrap) {
1050 spin_unlock(&info->lock);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001051 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return;
1053 }
Akinobu Mita179e0912006-06-26 00:24:41 -07001054 list_move(&wrap->list, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 spin_unlock(&info->lock);
1056
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001057 if (status) {
1058 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001059 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return;
1061 }
1062
Pete Zaitcevcf2c7482006-05-22 21:58:49 -07001063 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066
1067/*****************************************************************************
1068 * Connect Tech's White Heat firmware interface
1069 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +01001070static int firm_send_command(struct usb_serial_port *port, __u8 command,
1071 __u8 *data, __u8 datasize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 struct usb_serial_port *command_port;
1074 struct whiteheat_command_private *command_info;
1075 struct whiteheat_private *info;
1076 __u8 *transfer_buffer;
1077 int retval = 0;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001078 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Harvey Harrison441b62c2008-03-03 16:08:34 -08001080 dbg("%s - command %d", __func__, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 command_port = port->serial->port[COMMAND_PORT];
1083 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001084 mutex_lock(&command_info->mutex);
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001085 command_info->command_finished = false;
Alan Cox80359a92008-07-22 11:09:16 +01001086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1088 transfer_buffer[0] = command;
Alan Cox80359a92008-07-22 11:09:16 +01001089 memcpy(&transfer_buffer[1], data, datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 command_port->write_urb->transfer_buffer_length = datasize + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001091 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001093 dbg("%s - submit urb failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto exit;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 /* wait for the command to complete */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001098 t = wait_event_timeout(command_info->wait_command,
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001099 (bool)command_info->command_finished, COMMAND_TIMEOUT);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001100 if (!t)
1101 usb_kill_urb(command_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001103 if (command_info->command_finished == false) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001104 dbg("%s - command timed out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 retval = -ETIMEDOUT;
1106 goto exit;
1107 }
1108
1109 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001110 dbg("%s - command failed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 retval = -EIO;
1112 goto exit;
1113 }
1114
1115 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001116 dbg("%s - command completed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 switch (command) {
Alan Cox80359a92008-07-22 11:09:16 +01001118 case WHITEHEAT_GET_DTR_RTS:
1119 info = usb_get_serial_port_data(port);
1120 memcpy(&info->mcr, command_info->result_buffer,
1121 sizeof(struct whiteheat_dr_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 break;
1123 }
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001126 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return retval;
1128}
1129
1130
Alan Cox80359a92008-07-22 11:09:16 +01001131static int firm_open(struct usb_serial_port *port)
1132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 struct whiteheat_simple open_command;
1134
1135 open_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001136 return firm_send_command(port, WHITEHEAT_OPEN,
1137 (__u8 *)&open_command, sizeof(open_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
1139
1140
Alan Cox80359a92008-07-22 11:09:16 +01001141static int firm_close(struct usb_serial_port *port)
1142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 struct whiteheat_simple close_command;
1144
1145 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001146 return firm_send_command(port, WHITEHEAT_CLOSE,
1147 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
1150
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001151static void firm_setup_port(struct tty_struct *tty)
Alan Cox80359a92008-07-22 11:09:16 +01001152{
Alan Cox95da3102008-07-22 11:09:07 +01001153 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 struct whiteheat_port_settings port_settings;
Alan Cox95da3102008-07-22 11:09:07 +01001155 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Johan Hovoldd3a677e2013-06-06 13:32:47 +02001157 port_settings.port = port->number - port->serial->minor + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /* get the byte size */
1160 switch (cflag & CSIZE) {
Alan Cox80359a92008-07-22 11:09:16 +01001161 case CS5: port_settings.bits = 5; break;
1162 case CS6: port_settings.bits = 6; break;
1163 case CS7: port_settings.bits = 7; break;
1164 default:
1165 case CS8: port_settings.bits = 8; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001167 dbg("%s - data bits = %d", __func__, port_settings.bits);
Alan Cox80359a92008-07-22 11:09:16 +01001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* determine the parity */
1170 if (cflag & PARENB)
1171 if (cflag & CMSPAR)
1172 if (cflag & PARODD)
1173 port_settings.parity = WHITEHEAT_PAR_MARK;
1174 else
1175 port_settings.parity = WHITEHEAT_PAR_SPACE;
1176 else
1177 if (cflag & PARODD)
1178 port_settings.parity = WHITEHEAT_PAR_ODD;
1179 else
1180 port_settings.parity = WHITEHEAT_PAR_EVEN;
1181 else
1182 port_settings.parity = WHITEHEAT_PAR_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001183 dbg("%s - parity = %c", __func__, port_settings.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /* figure out the stop bits requested */
1186 if (cflag & CSTOPB)
1187 port_settings.stop = 2;
1188 else
1189 port_settings.stop = 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001190 dbg("%s - stop bits = %d", __func__, port_settings.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /* figure out the flow control settings */
1193 if (cflag & CRTSCTS)
Alan Cox80359a92008-07-22 11:09:16 +01001194 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1195 WHITEHEAT_HFLOW_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 else
1197 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001198 dbg("%s - hardware flow control = %s %s %s %s", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1200 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1201 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1202 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
Alan Cox80359a92008-07-22 11:09:16 +01001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* determine software flow control */
Alan Cox95da3102008-07-22 11:09:07 +01001205 if (I_IXOFF(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1207 else
1208 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001209 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
Alan Cox80359a92008-07-22 11:09:16 +01001210
Alan Cox95da3102008-07-22 11:09:07 +01001211 port_settings.xon = START_CHAR(tty);
1212 port_settings.xoff = STOP_CHAR(tty);
Alan Cox80359a92008-07-22 11:09:16 +01001213 dbg("%s - XON = %2x, XOFF = %2x",
1214 __func__, port_settings.xon, port_settings.xoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* get the baud rate wanted */
Alan Cox95da3102008-07-22 11:09:07 +01001217 port_settings.baud = tty_get_baud_rate(tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001218 dbg("%s - baud rate = %d", __func__, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Alan Cox01d1df22007-10-18 01:24:23 -07001220 /* fixme: should set validated settings */
Alan Cox95da3102008-07-22 11:09:07 +01001221 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /* handle any settings that aren't specified in the tty structure */
1223 port_settings.lloop = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /* now send the message to the device */
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001226 firm_send_command(port, WHITEHEAT_SETUP_PORT,
Alan Cox80359a92008-07-22 11:09:16 +01001227 (__u8 *)&port_settings, sizeof(port_settings));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230
Alan Cox80359a92008-07-22 11:09:16 +01001231static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 struct whiteheat_set_rdb rts_command;
1234
1235 rts_command.port = port->number - port->serial->minor + 1;
1236 rts_command.state = onoff;
Alan Cox80359a92008-07-22 11:09:16 +01001237 return firm_send_command(port, WHITEHEAT_SET_RTS,
1238 (__u8 *)&rts_command, sizeof(rts_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
1241
Alan Cox80359a92008-07-22 11:09:16 +01001242static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 struct whiteheat_set_rdb dtr_command;
1245
1246 dtr_command.port = port->number - port->serial->minor + 1;
1247 dtr_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001248 return firm_send_command(port, WHITEHEAT_SET_DTR,
Alan Cox80359a92008-07-22 11:09:16 +01001249 (__u8 *)&dtr_command, sizeof(dtr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250}
1251
1252
Alan Cox80359a92008-07-22 11:09:16 +01001253static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct whiteheat_set_rdb break_command;
1256
1257 break_command.port = port->number - port->serial->minor + 1;
1258 break_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001259 return firm_send_command(port, WHITEHEAT_SET_BREAK,
Alan Cox80359a92008-07-22 11:09:16 +01001260 (__u8 *)&break_command, sizeof(break_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
1263
Alan Cox80359a92008-07-22 11:09:16 +01001264static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct whiteheat_purge purge_command;
1267
1268 purge_command.port = port->number - port->serial->minor + 1;
1269 purge_command.what = rxtx;
Alan Cox80359a92008-07-22 11:09:16 +01001270 return firm_send_command(port, WHITEHEAT_PURGE,
1271 (__u8 *)&purge_command, sizeof(purge_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274
Alan Cox80359a92008-07-22 11:09:16 +01001275static int firm_get_dtr_rts(struct usb_serial_port *port)
1276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct whiteheat_simple get_dr_command;
1278
1279 get_dr_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001280 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1281 (__u8 *)&get_dr_command, sizeof(get_dr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
1284
Alan Cox80359a92008-07-22 11:09:16 +01001285static int firm_report_tx_done(struct usb_serial_port *port)
1286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct whiteheat_simple close_command;
1288
1289 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001290 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1291 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294
1295/*****************************************************************************
1296 * Connect Tech's White Heat utility functions
1297 *****************************************************************************/
1298static int start_command_port(struct usb_serial *serial)
1299{
1300 struct usb_serial_port *command_port;
1301 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int retval = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 command_port = serial->port[COMMAND_PORT];
1305 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001306 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (!command_info->port_running) {
1308 /* Work around HCD bugs */
1309 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1312 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001313 dev_err(&serial->dev->dev,
1314 "%s - failed submitting read urb, error %d\n",
1315 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 goto exit;
1317 }
1318 }
1319 command_info->port_running++;
1320
1321exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001322 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return retval;
1324}
1325
1326
1327static void stop_command_port(struct usb_serial *serial)
1328{
1329 struct usb_serial_port *command_port;
1330 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 command_port = serial->port[COMMAND_PORT];
1333 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001334 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 command_info->port_running--;
1336 if (!command_info->port_running)
1337 usb_kill_urb(command_port->read_urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001338 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341
1342static int start_port_read(struct usb_serial_port *port)
1343{
1344 struct whiteheat_private *info = usb_get_serial_port_data(port);
1345 struct whiteheat_urb_wrap *wrap;
1346 struct urb *urb;
1347 int retval = 0;
1348 unsigned long flags;
1349 struct list_head *tmp;
1350 struct list_head *tmp2;
1351
1352 spin_lock_irqsave(&info->lock, flags);
1353
1354 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1355 list_del(tmp);
1356 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1357 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001358 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 retval = usb_submit_urb(urb, GFP_KERNEL);
1360 if (retval) {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001361 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 list_add(tmp, &info->rx_urbs_free);
1363 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1364 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1365 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001366 list_del(tmp);
1367 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001369 spin_lock_irqsave(&info->lock, flags);
1370 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372 break;
1373 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001374 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 list_add(tmp, &info->rx_urbs_submitted);
1376 }
1377
1378 spin_unlock_irqrestore(&info->lock, flags);
1379
1380 return retval;
1381}
1382
1383
Alan Cox80359a92008-07-22 11:09:16 +01001384static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1385 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct whiteheat_urb_wrap *wrap;
1388 struct list_head *tmp;
1389
1390 list_for_each(tmp, head) {
1391 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1392 if (wrap->urb == urb)
1393 return wrap;
1394 }
1395
1396 return NULL;
1397}
1398
1399
1400static struct list_head *list_first(struct list_head *head)
1401{
1402 return head->next;
1403}
1404
1405
David Howellsc4028952006-11-22 14:57:56 +00001406static void rx_data_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
David Howellsc4028952006-11-22 14:57:56 +00001408 struct whiteheat_private *info =
1409 container_of(work, struct whiteheat_private, rx_work);
1410 struct usb_serial_port *port = info->port;
Alan Cox4a90f092008-10-13 10:39:46 +01001411 struct tty_struct *tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct whiteheat_urb_wrap *wrap;
1413 struct urb *urb;
1414 unsigned long flags;
1415 struct list_head *tmp;
1416 struct list_head *tmp2;
1417 int result;
1418 int sent = 0;
1419
1420 spin_lock_irqsave(&info->lock, flags);
1421 if (info->flags & THROTTLED) {
1422 spin_unlock_irqrestore(&info->lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +01001423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
1426 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1427 list_del(tmp);
1428 spin_unlock_irqrestore(&info->lock, flags);
1429
1430 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1431 urb = wrap->urb;
1432
Alan Cox67ccbd62010-02-17 13:06:57 +00001433 if (tty && urb->actual_length)
1434 sent += tty_insert_flip_string(tty,
1435 urb->transfer_buffer, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 result = usb_submit_urb(urb, GFP_ATOMIC);
1438 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001439 dev_err(&port->dev,
1440 "%s - failed resubmitting read urb, error %d\n",
Alan Cox80359a92008-07-22 11:09:16 +01001441 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 spin_lock_irqsave(&info->lock, flags);
1443 list_add(tmp, &info->rx_urbs_free);
1444 continue;
1445 }
1446
1447 spin_lock_irqsave(&info->lock, flags);
1448 list_add(tmp, &info->rx_urbs_submitted);
1449 }
1450 spin_unlock_irqrestore(&info->lock, flags);
1451
1452 if (sent)
1453 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001454out:
1455 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Greg Kroah-Hartman421f3882012-02-28 13:12:44 -08001458module_usb_serial_driver(whiteheat_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Alan Cox80359a92008-07-22 11:09:16 +01001460MODULE_AUTHOR(DRIVER_AUTHOR);
1461MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462MODULE_LICENSE("GPL");
1463
David Woodhouseec6752f2008-05-31 01:35:29 +03001464MODULE_FIRMWARE("whiteheat.fw");
1465MODULE_FIRMWARE("whiteheat_loader.fw");
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467module_param(urb_pool_size, int, 0);
1468MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1469
1470module_param(debug, bool, S_IRUGO | S_IWUSR);
1471MODULE_PARM_DESC(debug, "Debug enabled or not");