blob: bf7014d49a50ba2d6162b11ba1430ad4bca3acff [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 }
James Forshaw67f7e362014-08-23 14:39:48 -0700956 if (!urb->actual_length) {
957 dev_dbg(&urb->dev->dev, "%s - empty response, exiting.\n", __func__);
958 return;
959 }
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700960 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800961 dbg("%s - nonzero urb status: %d", __func__, status);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700962 if (status != -ENOENT)
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200963 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
964 wake_up(&command_info->wait_command);
965 return;
966 }
967
Alan Cox80359a92008-07-22 11:09:16 +0100968 usb_serial_debug_data(debug, &command_port->dev,
969 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
972 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200973 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
975 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200976 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 } else if (data[0] == WHITEHEAT_EVENT) {
Alan Cox80359a92008-07-22 11:09:16 +0100978 /* These are unsolicited reports from the firmware, hence no
979 waiting command to wakeup */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800980 dbg("%s - event received", __func__);
James Forshaw67f7e362014-08-23 14:39:48 -0700981 } else if ((data[0] == WHITEHEAT_GET_DTR_RTS) &&
982 (urb->actual_length - 1 <= sizeof(command_info->result_buffer))) {
Alan Cox80359a92008-07-22 11:09:16 +0100983 memcpy(command_info->result_buffer, &data[1],
984 urb->actual_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200986 wake_up(&command_info->wait_command);
Alan Cox80359a92008-07-22 11:09:16 +0100987 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800988 dbg("%s - bad reply from firmware", __func__);
Alan Cox80359a92008-07-22 11:09:16 +0100989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (result)
Alan Cox80359a92008-07-22 11:09:16 +0100993 dbg("%s - failed resubmitting read urb, error %d",
994 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997
David Howells7d12e782006-10-05 14:55:46 +0100998static void whiteheat_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Ming Leicdc97792008-02-24 18:41:47 +08001000 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 struct whiteheat_urb_wrap *wrap;
1002 unsigned char *data = urb->transfer_buffer;
1003 struct whiteheat_private *info = usb_get_serial_port_data(port);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001004 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Harvey Harrison441b62c2008-03-03 16:08:34 -08001006 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 spin_lock(&info->lock);
1009 wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1010 if (!wrap) {
1011 spin_unlock(&info->lock);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001012 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return;
1014 }
1015 list_del(&wrap->list);
1016 spin_unlock(&info->lock);
1017
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001018 if (status) {
1019 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001020 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 spin_lock(&info->lock);
1022 list_add(&wrap->list, &info->rx_urbs_free);
1023 spin_unlock(&info->lock);
1024 return;
1025 }
1026
Alan Cox80359a92008-07-22 11:09:16 +01001027 usb_serial_debug_data(debug, &port->dev,
1028 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 spin_lock(&info->lock);
1031 list_add_tail(&wrap->list, &info->rx_urb_q);
1032 if (info->flags & THROTTLED) {
1033 info->flags |= ACTUALLY_THROTTLED;
1034 spin_unlock(&info->lock);
1035 return;
1036 }
1037 spin_unlock(&info->lock);
1038
1039 schedule_work(&info->rx_work);
1040}
1041
1042
David Howells7d12e782006-10-05 14:55:46 +01001043static void whiteheat_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Ming Leicdc97792008-02-24 18:41:47 +08001045 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct whiteheat_private *info = usb_get_serial_port_data(port);
1047 struct whiteheat_urb_wrap *wrap;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001048 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Harvey Harrison441b62c2008-03-03 16:08:34 -08001050 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 spin_lock(&info->lock);
1053 wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1054 if (!wrap) {
1055 spin_unlock(&info->lock);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001056 dev_err(&port->dev, "%s - Not my urb!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return;
1058 }
Akinobu Mita179e0912006-06-26 00:24:41 -07001059 list_move(&wrap->list, &info->tx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 spin_unlock(&info->lock);
1061
Greg Kroah-Hartman05400012007-06-15 15:44:13 -07001062 if (status) {
1063 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001064 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return;
1066 }
1067
Pete Zaitcevcf2c7482006-05-22 21:58:49 -07001068 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071
1072/*****************************************************************************
1073 * Connect Tech's White Heat firmware interface
1074 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +01001075static int firm_send_command(struct usb_serial_port *port, __u8 command,
1076 __u8 *data, __u8 datasize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 struct usb_serial_port *command_port;
1079 struct whiteheat_command_private *command_info;
1080 struct whiteheat_private *info;
1081 __u8 *transfer_buffer;
1082 int retval = 0;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001083 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Harvey Harrison441b62c2008-03-03 16:08:34 -08001085 dbg("%s - command %d", __func__, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 command_port = port->serial->port[COMMAND_PORT];
1088 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001089 mutex_lock(&command_info->mutex);
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001090 command_info->command_finished = false;
Alan Cox80359a92008-07-22 11:09:16 +01001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1093 transfer_buffer[0] = command;
Alan Cox80359a92008-07-22 11:09:16 +01001094 memcpy(&transfer_buffer[1], data, datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 command_port->write_urb->transfer_buffer_length = datasize + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001096 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001098 dbg("%s - submit urb failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 goto exit;
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /* wait for the command to complete */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001103 t = wait_event_timeout(command_info->wait_command,
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001104 (bool)command_info->command_finished, COMMAND_TIMEOUT);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001105 if (!t)
1106 usb_kill_urb(command_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Richard Knutsson38c3cb52007-03-26 22:00:28 -08001108 if (command_info->command_finished == false) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001109 dbg("%s - command timed out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 retval = -ETIMEDOUT;
1111 goto exit;
1112 }
1113
1114 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001115 dbg("%s - command failed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 retval = -EIO;
1117 goto exit;
1118 }
1119
1120 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001121 dbg("%s - command completed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 switch (command) {
Alan Cox80359a92008-07-22 11:09:16 +01001123 case WHITEHEAT_GET_DTR_RTS:
1124 info = usb_get_serial_port_data(port);
1125 memcpy(&info->mcr, command_info->result_buffer,
1126 sizeof(struct whiteheat_dr_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 break;
1128 }
1129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001131 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return retval;
1133}
1134
1135
Alan Cox80359a92008-07-22 11:09:16 +01001136static int firm_open(struct usb_serial_port *port)
1137{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 struct whiteheat_simple open_command;
1139
1140 open_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001141 return firm_send_command(port, WHITEHEAT_OPEN,
1142 (__u8 *)&open_command, sizeof(open_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145
Alan Cox80359a92008-07-22 11:09:16 +01001146static int firm_close(struct usb_serial_port *port)
1147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 struct whiteheat_simple close_command;
1149
1150 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001151 return firm_send_command(port, WHITEHEAT_CLOSE,
1152 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001156static void firm_setup_port(struct tty_struct *tty)
Alan Cox80359a92008-07-22 11:09:16 +01001157{
Alan Cox95da3102008-07-22 11:09:07 +01001158 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct whiteheat_port_settings port_settings;
Alan Cox95da3102008-07-22 11:09:07 +01001160 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Johan Hovoldd3a677e2013-06-06 13:32:47 +02001162 port_settings.port = port->number - port->serial->minor + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /* get the byte size */
1165 switch (cflag & CSIZE) {
Alan Cox80359a92008-07-22 11:09:16 +01001166 case CS5: port_settings.bits = 5; break;
1167 case CS6: port_settings.bits = 6; break;
1168 case CS7: port_settings.bits = 7; break;
1169 default:
1170 case CS8: port_settings.bits = 8; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
Harvey Harrison441b62c2008-03-03 16:08:34 -08001172 dbg("%s - data bits = %d", __func__, port_settings.bits);
Alan Cox80359a92008-07-22 11:09:16 +01001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /* determine the parity */
1175 if (cflag & PARENB)
1176 if (cflag & CMSPAR)
1177 if (cflag & PARODD)
1178 port_settings.parity = WHITEHEAT_PAR_MARK;
1179 else
1180 port_settings.parity = WHITEHEAT_PAR_SPACE;
1181 else
1182 if (cflag & PARODD)
1183 port_settings.parity = WHITEHEAT_PAR_ODD;
1184 else
1185 port_settings.parity = WHITEHEAT_PAR_EVEN;
1186 else
1187 port_settings.parity = WHITEHEAT_PAR_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001188 dbg("%s - parity = %c", __func__, port_settings.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /* figure out the stop bits requested */
1191 if (cflag & CSTOPB)
1192 port_settings.stop = 2;
1193 else
1194 port_settings.stop = 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001195 dbg("%s - stop bits = %d", __func__, port_settings.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 /* figure out the flow control settings */
1198 if (cflag & CRTSCTS)
Alan Cox80359a92008-07-22 11:09:16 +01001199 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1200 WHITEHEAT_HFLOW_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 else
1202 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001203 dbg("%s - hardware flow control = %s %s %s %s", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1205 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1206 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1207 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
Alan Cox80359a92008-07-22 11:09:16 +01001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* determine software flow control */
Alan Cox95da3102008-07-22 11:09:07 +01001210 if (I_IXOFF(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1212 else
1213 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -08001214 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
Alan Cox80359a92008-07-22 11:09:16 +01001215
Alan Cox95da3102008-07-22 11:09:07 +01001216 port_settings.xon = START_CHAR(tty);
1217 port_settings.xoff = STOP_CHAR(tty);
Alan Cox80359a92008-07-22 11:09:16 +01001218 dbg("%s - XON = %2x, XOFF = %2x",
1219 __func__, port_settings.xon, port_settings.xoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /* get the baud rate wanted */
Alan Cox95da3102008-07-22 11:09:07 +01001222 port_settings.baud = tty_get_baud_rate(tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -08001223 dbg("%s - baud rate = %d", __func__, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Alan Cox01d1df22007-10-18 01:24:23 -07001225 /* fixme: should set validated settings */
Alan Cox95da3102008-07-22 11:09:07 +01001226 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /* handle any settings that aren't specified in the tty structure */
1228 port_settings.lloop = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /* now send the message to the device */
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001231 firm_send_command(port, WHITEHEAT_SETUP_PORT,
Alan Cox80359a92008-07-22 11:09:16 +01001232 (__u8 *)&port_settings, sizeof(port_settings));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235
Alan Cox80359a92008-07-22 11:09:16 +01001236static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 struct whiteheat_set_rdb rts_command;
1239
1240 rts_command.port = port->number - port->serial->minor + 1;
1241 rts_command.state = onoff;
Alan Cox80359a92008-07-22 11:09:16 +01001242 return firm_send_command(port, WHITEHEAT_SET_RTS,
1243 (__u8 *)&rts_command, sizeof(rts_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246
Alan Cox80359a92008-07-22 11:09:16 +01001247static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1248{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct whiteheat_set_rdb dtr_command;
1250
1251 dtr_command.port = port->number - port->serial->minor + 1;
1252 dtr_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001253 return firm_send_command(port, WHITEHEAT_SET_DTR,
Alan Cox80359a92008-07-22 11:09:16 +01001254 (__u8 *)&dtr_command, sizeof(dtr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
1256
1257
Alan Cox80359a92008-07-22 11:09:16 +01001258static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct whiteheat_set_rdb break_command;
1261
1262 break_command.port = port->number - port->serial->minor + 1;
1263 break_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +01001264 return firm_send_command(port, WHITEHEAT_SET_BREAK,
Alan Cox80359a92008-07-22 11:09:16 +01001265 (__u8 *)&break_command, sizeof(break_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268
Alan Cox80359a92008-07-22 11:09:16 +01001269static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1270{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct whiteheat_purge purge_command;
1272
1273 purge_command.port = port->number - port->serial->minor + 1;
1274 purge_command.what = rxtx;
Alan Cox80359a92008-07-22 11:09:16 +01001275 return firm_send_command(port, WHITEHEAT_PURGE,
1276 (__u8 *)&purge_command, sizeof(purge_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
1279
Alan Cox80359a92008-07-22 11:09:16 +01001280static int firm_get_dtr_rts(struct usb_serial_port *port)
1281{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 struct whiteheat_simple get_dr_command;
1283
1284 get_dr_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001285 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1286 (__u8 *)&get_dr_command, sizeof(get_dr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289
Alan Cox80359a92008-07-22 11:09:16 +01001290static int firm_report_tx_done(struct usb_serial_port *port)
1291{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct whiteheat_simple close_command;
1293
1294 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +01001295 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1296 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
1299
1300/*****************************************************************************
1301 * Connect Tech's White Heat utility functions
1302 *****************************************************************************/
1303static int start_command_port(struct usb_serial *serial)
1304{
1305 struct usb_serial_port *command_port;
1306 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 int retval = 0;
Alan Cox80359a92008-07-22 11:09:16 +01001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 command_port = serial->port[COMMAND_PORT];
1310 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001311 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (!command_info->port_running) {
1313 /* Work around HCD bugs */
1314 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1317 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001318 dev_err(&serial->dev->dev,
1319 "%s - failed submitting read urb, error %d\n",
1320 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 goto exit;
1322 }
1323 }
1324 command_info->port_running++;
1325
1326exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001327 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return retval;
1329}
1330
1331
1332static void stop_command_port(struct usb_serial *serial)
1333{
1334 struct usb_serial_port *command_port;
1335 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 command_port = serial->port[COMMAND_PORT];
1338 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001339 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 command_info->port_running--;
1341 if (!command_info->port_running)
1342 usb_kill_urb(command_port->read_urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001343 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346
1347static int start_port_read(struct usb_serial_port *port)
1348{
1349 struct whiteheat_private *info = usb_get_serial_port_data(port);
1350 struct whiteheat_urb_wrap *wrap;
1351 struct urb *urb;
1352 int retval = 0;
1353 unsigned long flags;
1354 struct list_head *tmp;
1355 struct list_head *tmp2;
1356
1357 spin_lock_irqsave(&info->lock, flags);
1358
1359 list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1360 list_del(tmp);
1361 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1362 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001363 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 retval = usb_submit_urb(urb, GFP_KERNEL);
1365 if (retval) {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001366 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 list_add(tmp, &info->rx_urbs_free);
1368 list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1369 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1370 urb = wrap->urb;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001371 list_del(tmp);
1372 spin_unlock_irqrestore(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 usb_kill_urb(urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001374 spin_lock_irqsave(&info->lock, flags);
1375 list_add(tmp, &info->rx_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 break;
1378 }
Oliver Neukum08a2b3b2007-05-24 13:52:51 +02001379 spin_lock_irqsave(&info->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 list_add(tmp, &info->rx_urbs_submitted);
1381 }
1382
1383 spin_unlock_irqrestore(&info->lock, flags);
1384
1385 return retval;
1386}
1387
1388
Alan Cox80359a92008-07-22 11:09:16 +01001389static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1390 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct whiteheat_urb_wrap *wrap;
1393 struct list_head *tmp;
1394
1395 list_for_each(tmp, head) {
1396 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1397 if (wrap->urb == urb)
1398 return wrap;
1399 }
1400
1401 return NULL;
1402}
1403
1404
1405static struct list_head *list_first(struct list_head *head)
1406{
1407 return head->next;
1408}
1409
1410
David Howellsc4028952006-11-22 14:57:56 +00001411static void rx_data_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
David Howellsc4028952006-11-22 14:57:56 +00001413 struct whiteheat_private *info =
1414 container_of(work, struct whiteheat_private, rx_work);
1415 struct usb_serial_port *port = info->port;
Alan Cox4a90f092008-10-13 10:39:46 +01001416 struct tty_struct *tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 struct whiteheat_urb_wrap *wrap;
1418 struct urb *urb;
1419 unsigned long flags;
1420 struct list_head *tmp;
1421 struct list_head *tmp2;
1422 int result;
1423 int sent = 0;
1424
1425 spin_lock_irqsave(&info->lock, flags);
1426 if (info->flags & THROTTLED) {
1427 spin_unlock_irqrestore(&info->lock, flags);
Alan Cox4a90f092008-10-13 10:39:46 +01001428 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1432 list_del(tmp);
1433 spin_unlock_irqrestore(&info->lock, flags);
1434
1435 wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1436 urb = wrap->urb;
1437
Alan Cox67ccbd62010-02-17 13:06:57 +00001438 if (tty && urb->actual_length)
1439 sent += tty_insert_flip_string(tty,
1440 urb->transfer_buffer, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 result = usb_submit_urb(urb, GFP_ATOMIC);
1443 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -07001444 dev_err(&port->dev,
1445 "%s - failed resubmitting read urb, error %d\n",
Alan Cox80359a92008-07-22 11:09:16 +01001446 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 spin_lock_irqsave(&info->lock, flags);
1448 list_add(tmp, &info->rx_urbs_free);
1449 continue;
1450 }
1451
1452 spin_lock_irqsave(&info->lock, flags);
1453 list_add(tmp, &info->rx_urbs_submitted);
1454 }
1455 spin_unlock_irqrestore(&info->lock, flags);
1456
1457 if (sent)
1458 tty_flip_buffer_push(tty);
Alan Cox4a90f092008-10-13 10:39:46 +01001459out:
1460 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Greg Kroah-Hartman421f3882012-02-28 13:12:44 -08001463module_usb_serial_driver(whiteheat_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Alan Cox80359a92008-07-22 11:09:16 +01001465MODULE_AUTHOR(DRIVER_AUTHOR);
1466MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467MODULE_LICENSE("GPL");
1468
David Woodhouseec6752f2008-05-31 01:35:29 +03001469MODULE_FIRMWARE("whiteheat.fw");
1470MODULE_FIRMWARE("whiteheat_loader.fw");
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472module_param(urb_pool_size, int, 0);
1473MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1474
1475module_param(debug, bool, S_IRUGO | S_IWUSR);
1476MODULE_PARM_DESC(debug, "Debug enabled or not");