blob: c141b472b6a292e7f057bec813f923ceef3aabf5 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
49#define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
50
51#define CONNECT_TECH_VENDOR_ID 0x0710
52#define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
53#define CONNECT_TECH_WHITE_HEAT_ID 0x8001
54
55/*
56 ID tables for whiteheat are unusual, because we want to different
57 things for different versions of the device. Eventually, this
58 will be doable from a single table. But, for now, we define two
59 separate ID tables, and then a third table that combines them
60 just for the purpose of exporting the autoloading information.
61*/
Németh Márton7d40d7e2010-01-10 15:34:24 +010062static const struct usb_device_id id_table_std[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
64 { } /* Terminating entry */
65};
66
Németh Márton7d40d7e2010-01-10 15:34:24 +010067static const struct usb_device_id id_table_prerenumeration[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
69 { } /* Terminating entry */
70};
71
Németh Márton7d40d7e2010-01-10 15:34:24 +010072static const struct usb_device_id id_table_combined[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
74 { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
75 { } /* Terminating entry */
76};
77
Alan Cox80359a92008-07-22 11:09:16 +010078MODULE_DEVICE_TABLE(usb, id_table_combined);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static struct usb_driver whiteheat_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 .name = "whiteheat",
82 .probe = usb_serial_probe,
83 .disconnect = usb_serial_disconnect,
84 .id_table = id_table_combined,
85};
86
87/* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
Alan Cox80359a92008-07-22 11:09:16 +010088static int whiteheat_firmware_download(struct usb_serial *serial,
89 const struct usb_device_id *id);
90static int whiteheat_firmware_attach(struct usb_serial *serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* function prototypes for the Connect Tech WhiteHEAT serial converter */
Alan Cox80359a92008-07-22 11:09:16 +010093static int whiteheat_attach(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040094static void whiteheat_release(struct usb_serial *serial);
Alan Cox80359a92008-07-22 11:09:16 +010095static int whiteheat_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -070096 struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010097static void whiteheat_close(struct usb_serial_port *port);
Alan Cox00a0d0d2011-02-14 16:27:06 +000098static int whiteheat_ioctl(struct tty_struct *tty,
Alan Cox80359a92008-07-22 11:09:16 +010099 unsigned int cmd, unsigned long arg);
100static void whiteheat_set_termios(struct tty_struct *tty,
101 struct usb_serial_port *port, struct ktermios *old);
Alan Cox60b33c12011-02-14 16:26:14 +0000102static int whiteheat_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +0000103static int whiteheat_tiocmset(struct tty_struct *tty,
Alan Cox80359a92008-07-22 11:09:16 +0100104 unsigned int set, unsigned int clear);
105static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700107static struct usb_serial_driver whiteheat_fake_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700108 .driver = {
109 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700110 .name = "whiteheatnofirm",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700111 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700112 .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .id_table = id_table_prerenumeration,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .num_ports = 1,
115 .probe = whiteheat_firmware_download,
116 .attach = whiteheat_firmware_attach,
117};
118
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700119static struct usb_serial_driver whiteheat_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700120 .driver = {
121 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700122 .name = "whiteheat",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700123 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700124 .description = "Connect Tech - WhiteHEAT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .id_table = id_table_std,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .num_ports = 4,
127 .attach = whiteheat_attach,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400128 .release = whiteheat_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .open = whiteheat_open,
130 .close = whiteheat_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .ioctl = whiteheat_ioctl,
132 .set_termios = whiteheat_set_termios,
133 .break_ctl = whiteheat_break_ctl,
134 .tiocmget = whiteheat_tiocmget,
135 .tiocmset = whiteheat_tiocmset,
Johan Hovold5c0c7582012-03-22 16:50:55 +0100136 .throttle = usb_serial_generic_throttle,
137 .unthrottle = usb_serial_generic_unthrottle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Alan Stern29618e92012-02-23 14:57:32 -0500140static struct usb_serial_driver * const serial_drivers[] = {
141 &whiteheat_fake_device, &whiteheat_device, NULL
142};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144struct whiteheat_command_private {
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200145 struct mutex mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 __u8 port_running;
147 __u8 command_finished;
Alan Cox80359a92008-07-22 11:09:16 +0100148 wait_queue_head_t wait_command; /* for handling sleeping whilst
149 waiting for a command to
150 finish */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 __u8 result_buffer[64];
152};
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154struct whiteheat_private {
Alan Coxa5b6f602008-04-08 17:16:06 +0100155 __u8 mcr; /* FIXME: no locking on mcr */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158
159/* local function prototypes */
160static int start_command_port(struct usb_serial *serial);
161static void stop_command_port(struct usb_serial *serial);
David Howells7d12e782006-10-05 14:55:46 +0100162static void command_port_write_callback(struct urb *urb);
163static void command_port_read_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Alan Cox80359a92008-07-22 11:09:16 +0100165static int firm_send_command(struct usb_serial_port *port, __u8 command,
166 __u8 *data, __u8 datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static int firm_open(struct usb_serial_port *port);
168static int firm_close(struct usb_serial_port *port);
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700169static void firm_setup_port(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
171static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
172static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
173static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
174static int firm_get_dtr_rts(struct usb_serial_port *port);
175static int firm_report_tx_done(struct usb_serial_port *port);
176
177
178#define COMMAND_PORT 4
179#define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
180#define COMMAND_TIMEOUT_MS 2000
181#define CLOSING_DELAY (30 * HZ)
182
183
184/*****************************************************************************
185 * Connect Tech's White Heat prerenumeration driver functions
186 *****************************************************************************/
187
188/* steps to download the firmware to the WhiteHEAT device:
189 - hold the reset (by writing to the reset bit of the CPUCS register)
190 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
191 - release the reset (by writing to the CPUCS register)
192 - download the WH.HEX file for all addresses greater than 0x1b3f using
193 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
194 - hold the reset
195 - download the WH.HEX file for all addresses less than 0x1b40 using
196 VENDOR_REQUEST_ANCHOR_LOAD
197 - release the reset
198 - device renumerated itself and comes up as new device id with all
199 firmware download completed.
200*/
Alan Cox80359a92008-07-22 11:09:16 +0100201static int whiteheat_firmware_download(struct usb_serial *serial,
202 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
David Woodhouseec6752f2008-05-31 01:35:29 +0300204 int response, ret = -ENOENT;
205 const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
206 const struct ihex_binrec *record;
207
Harvey Harrison441b62c2008-03-03 16:08:34 -0800208 dbg("%s", __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300209
210 if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
211 &serial->dev->dev)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700212 dev_err(&serial->dev->dev,
213 "%s - request \"whiteheat.fw\" failed\n", __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300214 goto out;
215 }
216 if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
217 &serial->dev->dev)) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700218 dev_err(&serial->dev->dev,
219 "%s - request \"whiteheat_loader.fw\" failed\n",
220 __func__);
David Woodhouseec6752f2008-05-31 01:35:29 +0300221 goto out;
222 }
223 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 response = ezusb_set_reset (serial, 1);
225
David Woodhouseec6752f2008-05-31 01:35:29 +0300226 record = (const struct ihex_binrec *)loader_fw->data;
227 while (record) {
228 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
229 (unsigned char *)record->data,
230 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700232 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
233 "failed for loader (%d %04X %p %d)\n",
234 __func__, response, be32_to_cpu(record->addr),
235 record->data, be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 break;
237 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300238 record = ihex_next_binrec(record);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Alan Cox80359a92008-07-22 11:09:16 +0100241 response = ezusb_set_reset(serial, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
David Woodhouseec6752f2008-05-31 01:35:29 +0300243 record = (const struct ihex_binrec *)firmware_fw->data;
244 while (record && be32_to_cpu(record->addr) < 0x1b40)
245 record = ihex_next_binrec(record);
246 while (record) {
247 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
248 (unsigned char *)record->data,
249 be16_to_cpu(record->len), 0xa3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700251 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
252 "failed for first firmware step "
253 "(%d %04X %p %d)\n", __func__, response,
254 be32_to_cpu(record->addr), record->data,
255 be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break;
257 }
258 ++record;
259 }
Alan Cox80359a92008-07-22 11:09:16 +0100260
261 response = ezusb_set_reset(serial, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David Woodhouseec6752f2008-05-31 01:35:29 +0300263 record = (const struct ihex_binrec *)firmware_fw->data;
264 while (record && be32_to_cpu(record->addr) < 0x1b40) {
265 response = ezusb_writememory (serial, be32_to_cpu(record->addr),
266 (unsigned char *)record->data,
267 be16_to_cpu(record->len), 0xa0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (response < 0) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700269 dev_err(&serial->dev->dev, "%s - ezusb_writememory "
270 "failed for second firmware step "
271 "(%d %04X %p %d)\n", __func__, response,
272 be32_to_cpu(record->addr), record->data,
273 be16_to_cpu(record->len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275 }
276 ++record;
277 }
David Woodhouseec6752f2008-05-31 01:35:29 +0300278 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 response = ezusb_set_reset (serial, 0);
David Woodhouseec6752f2008-05-31 01:35:29 +0300280 out:
281 release_firmware(loader_fw);
282 release_firmware(firmware_fw);
283 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286
Alan Cox80359a92008-07-22 11:09:16 +0100287static int whiteheat_firmware_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 /* We want this device to fail to have a driver assigned to it */
290 return 1;
291}
292
293
294/*****************************************************************************
295 * Connect Tech's White Heat serial driver functions
296 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +0100297static int whiteheat_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct usb_serial_port *command_port;
300 struct whiteheat_command_private *command_info;
301 struct usb_serial_port *port;
302 struct whiteheat_private *info;
303 struct whiteheat_hw_info *hw_info;
304 int pipe;
305 int ret;
306 int alen;
307 __u8 *command;
308 __u8 *result;
309 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 command_port = serial->port[COMMAND_PORT];
312
Alan Cox80359a92008-07-22 11:09:16 +0100313 pipe = usb_sndbulkpipe(serial->dev,
314 command_port->bulk_out_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 command = kmalloc(2, GFP_KERNEL);
316 if (!command)
317 goto no_command_buffer;
318 command[0] = WHITEHEAT_GET_HW_INFO;
319 command[1] = 0;
Alan Cox80359a92008-07-22 11:09:16 +0100320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
322 if (!result)
323 goto no_result_buffer;
324 /*
325 * When the module is reloaded the firmware is still there and
326 * the endpoints are still in the usb core unchanged. This is the
Alan Cox80359a92008-07-22 11:09:16 +0100327 * unlinking bug in disguise. Same for the call below.
328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100330 ret = usb_bulk_msg(serial->dev, pipe, command, 2,
331 &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (ret) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700333 dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
334 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400336 } else if (alen != 2) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700337 dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
338 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 goto no_firmware;
340 }
341
Alan Cox80359a92008-07-22 11:09:16 +0100342 pipe = usb_rcvbulkpipe(serial->dev,
343 command_port->bulk_in_endpointAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* See the comment on the usb_clear_halt() above */
345 usb_clear_halt(serial->dev, pipe);
Alan Cox80359a92008-07-22 11:09:16 +0100346 ret = usb_bulk_msg(serial->dev, pipe, result,
347 sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (ret) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700349 dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
350 serial->type->description, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto no_firmware;
Stuart MacDonald09fd6bc2006-05-31 13:28:40 -0400352 } else if (alen != sizeof(*hw_info) + 1) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700353 dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
354 serial->type->description, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 goto no_firmware;
356 } else if (result[0] != command[0]) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700357 dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
358 serial->type->description, result[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto no_firmware;
360 }
361
362 hw_info = (struct whiteheat_hw_info *)&result[1];
363
Johan Hovolda6765cb2012-03-22 16:50:54 +0100364 dev_info(&serial->dev->dev, "%s: Firmware v%d.%02d\n",
365 serial->type->description,
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700366 hw_info->sw_major_rev, hw_info->sw_minor_rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 for (i = 0; i < serial->num_ports; i++) {
369 port = serial->port[i];
370
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800371 info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (info == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700373 dev_err(&port->dev,
374 "%s: Out of memory for port structures\n",
375 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 goto no_private;
377 }
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 info->mcr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 usb_set_serial_port_data(port, info);
382 }
383
Alan Cox80359a92008-07-22 11:09:16 +0100384 command_info = kmalloc(sizeof(struct whiteheat_command_private),
385 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (command_info == NULL) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700387 dev_err(&serial->dev->dev,
388 "%s: Out of memory for port structures\n",
389 serial->type->description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 goto no_command_private;
391 }
392
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200393 mutex_init(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 command_info->port_running = 0;
395 init_waitqueue_head(&command_info->wait_command);
396 usb_set_serial_port_data(command_port, command_info);
397 command_port->write_urb->complete = command_port_write_callback;
398 command_port->read_urb->complete = command_port_read_callback;
399 kfree(result);
400 kfree(command);
401
402 return 0;
403
404no_firmware:
405 /* Firmware likely not running */
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700406 dev_err(&serial->dev->dev,
407 "%s: Unable to retrieve firmware version, try replugging\n",
408 serial->type->description);
409 dev_err(&serial->dev->dev,
410 "%s: If the firmware is not running (status led not blinking)\n",
411 serial->type->description);
412 dev_err(&serial->dev->dev,
413 "%s: please contact support@connecttech.com\n",
414 serial->type->description);
Jesper Juhl67ca0282006-04-23 19:59:23 +0200415 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -ENODEV;
417
418no_command_private:
419 for (i = serial->num_ports - 1; i >= 0; i--) {
420 port = serial->port[i];
421 info = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 kfree(info);
423no_private:
424 ;
425 }
426 kfree(result);
427no_result_buffer:
428 kfree(command);
429no_command_buffer:
430 return -ENOMEM;
431}
432
433
Alan Sternf9c99bb2009-06-02 11:53:55 -0400434static void whiteheat_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 struct usb_serial_port *command_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct whiteheat_private *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int i;
439
Harvey Harrison441b62c2008-03-03 16:08:34 -0800440 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* free up our private data for our command port */
443 command_port = serial->port[COMMAND_PORT];
Alan Cox80359a92008-07-22 11:09:16 +0100444 kfree(usb_get_serial_port_data(command_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 for (i = 0; i < serial->num_ports; i++) {
Johan Hovold5c0c7582012-03-22 16:50:55 +0100447 info = usb_get_serial_port_data(serial->port[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 kfree(info);
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Alan Coxa509a7e2009-09-19 13:13:26 -0700452static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Johan Hovold5c0c7582012-03-22 16:50:55 +0100454 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Harvey Harrison441b62c2008-03-03 16:08:34 -0800456 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 retval = start_command_port(port->serial);
459 if (retval)
460 goto exit;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* send an open port command */
463 retval = firm_open(port);
464 if (retval) {
465 stop_command_port(port->serial);
466 goto exit;
467 }
468
469 retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
470 if (retval) {
471 firm_close(port);
472 stop_command_port(port->serial);
473 goto exit;
474 }
475
Alan Cox72e27412008-07-22 11:09:29 +0100476 if (tty)
477 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* Work around HCD bugs */
480 usb_clear_halt(port->serial->dev, port->read_urb->pipe);
481 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
482
Johan Hovold5c0c7582012-03-22 16:50:55 +0100483 retval = usb_serial_generic_open(tty, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 firm_close(port);
486 stop_command_port(port->serial);
487 goto exit;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489exit:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800490 dbg("%s - exit, retval = %d", __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return retval;
492}
493
494
Alan Cox335f8512009-06-11 12:26:29 +0100495static void whiteheat_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800497 dbg("%s - port %d", __func__, port->number);
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 firm_report_tx_done(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 firm_close(port);
501
Johan Hovold5c0c7582012-03-22 16:50:55 +0100502 usb_serial_generic_close(port);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 stop_command_port(port->serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Alan Cox60b33c12011-02-14 16:26:14 +0000507static int whiteheat_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Alan Cox95da3102008-07-22 11:09:07 +0100509 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct whiteheat_private *info = usb_get_serial_port_data(port);
511 unsigned int modem_signals = 0;
512
Harvey Harrison441b62c2008-03-03 16:08:34 -0800513 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 firm_get_dtr_rts(port);
516 if (info->mcr & UART_MCR_DTR)
517 modem_signals |= TIOCM_DTR;
518 if (info->mcr & UART_MCR_RTS)
519 modem_signals |= TIOCM_RTS;
520
521 return modem_signals;
522}
523
Alan Cox20b9d172011-02-14 16:26:50 +0000524static int whiteheat_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 unsigned int set, unsigned int clear)
526{
Alan Cox95da3102008-07-22 11:09:07 +0100527 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct whiteheat_private *info = usb_get_serial_port_data(port);
529
Harvey Harrison441b62c2008-03-03 16:08:34 -0800530 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (set & TIOCM_RTS)
533 info->mcr |= UART_MCR_RTS;
534 if (set & TIOCM_DTR)
535 info->mcr |= UART_MCR_DTR;
536
537 if (clear & TIOCM_RTS)
538 info->mcr &= ~UART_MCR_RTS;
539 if (clear & TIOCM_DTR)
540 info->mcr &= ~UART_MCR_DTR;
541
542 firm_set_dtr(port, info->mcr & UART_MCR_DTR);
543 firm_set_rts(port, info->mcr & UART_MCR_RTS);
544 return 0;
545}
546
547
Alan Cox00a0d0d2011-02-14 16:27:06 +0000548static int whiteheat_ioctl(struct tty_struct *tty,
Alan Cox80359a92008-07-22 11:09:16 +0100549 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Alan Cox95da3102008-07-22 11:09:07 +0100551 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 struct serial_struct serstruct;
553 void __user *user_arg = (void __user *)arg;
554
Harvey Harrison441b62c2008-03-03 16:08:34 -0800555 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 switch (cmd) {
Alan Cox80359a92008-07-22 11:09:16 +0100558 case TIOCGSERIAL:
559 memset(&serstruct, 0, sizeof(serstruct));
560 serstruct.type = PORT_16654;
561 serstruct.line = port->serial->minor;
562 serstruct.port = port->number;
563 serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
Johan Hovold5c0c7582012-03-22 16:50:55 +0100564 serstruct.xmit_fifo_size = kfifo_size(&port->write_fifo);
Alan Cox80359a92008-07-22 11:09:16 +0100565 serstruct.custom_divisor = 0;
566 serstruct.baud_base = 460800;
567 serstruct.close_delay = CLOSING_DELAY;
568 serstruct.closing_wait = CLOSING_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Alan Cox80359a92008-07-22 11:09:16 +0100570 if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
571 return -EFAULT;
572 break;
573 default:
574 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
577 return -ENOIOCTLCMD;
578}
579
580
Alan Cox80359a92008-07-22 11:09:16 +0100581static void whiteheat_set_termios(struct tty_struct *tty,
582 struct usb_serial_port *port, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Alan Cox95da3102008-07-22 11:09:07 +0100584 firm_setup_port(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Alan Cox80359a92008-07-22 11:09:16 +0100587static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
588{
Alan Cox95da3102008-07-22 11:09:07 +0100589 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 firm_set_break(port, break_state);
591}
592
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/*****************************************************************************
595 * Connect Tech's White Heat callback routines
596 *****************************************************************************/
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200597static void command_port_write_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700599 int status = urb->status;
600
Harvey Harrison441b62c2008-03-03 16:08:34 -0800601 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700603 if (status) {
604 dbg("nonzero urb status: %d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return;
606 }
607}
608
609
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200610static void command_port_read_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Ming Leicdc97792008-02-24 18:41:47 +0800612 struct usb_serial_port *command_port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct whiteheat_command_private *command_info;
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700614 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unsigned char *data = urb->transfer_buffer;
616 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Harvey Harrison441b62c2008-03-03 16:08:34 -0800618 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 command_info = usb_get_serial_port_data(command_port);
621 if (!command_info) {
Alan Cox80359a92008-07-22 11:09:16 +0100622 dbg("%s - command_info is NULL, exiting.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return;
624 }
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700625 if (status) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800626 dbg("%s - nonzero urb status: %d", __func__, status);
Greg Kroah-Hartman05400012007-06-15 15:44:13 -0700627 if (status != -ENOENT)
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200628 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
629 wake_up(&command_info->wait_command);
630 return;
631 }
632
Alan Cox80359a92008-07-22 11:09:16 +0100633 usb_serial_debug_data(debug, &command_port->dev,
634 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (data[0] == WHITEHEAT_CMD_COMPLETE) {
637 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200638 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
640 command_info->command_finished = WHITEHEAT_CMD_FAILURE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200641 wake_up(&command_info->wait_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 } else if (data[0] == WHITEHEAT_EVENT) {
Alan Cox80359a92008-07-22 11:09:16 +0100643 /* These are unsolicited reports from the firmware, hence no
644 waiting command to wakeup */
Harvey Harrison441b62c2008-03-03 16:08:34 -0800645 dbg("%s - event received", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
Alan Cox80359a92008-07-22 11:09:16 +0100647 memcpy(command_info->result_buffer, &data[1],
648 urb->actual_length - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200650 wake_up(&command_info->wait_command);
Alan Cox80359a92008-07-22 11:09:16 +0100651 } else
Harvey Harrison441b62c2008-03-03 16:08:34 -0800652 dbg("%s - bad reply from firmware", __func__);
Alan Cox80359a92008-07-22 11:09:16 +0100653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* Continue trying to always read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (result)
Alan Cox80359a92008-07-22 11:09:16 +0100657 dbg("%s - failed resubmitting read urb, error %d",
658 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/*****************************************************************************
663 * Connect Tech's White Heat firmware interface
664 *****************************************************************************/
Alan Cox80359a92008-07-22 11:09:16 +0100665static int firm_send_command(struct usb_serial_port *port, __u8 command,
666 __u8 *data, __u8 datasize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct usb_serial_port *command_port;
669 struct whiteheat_command_private *command_info;
670 struct whiteheat_private *info;
671 __u8 *transfer_buffer;
672 int retval = 0;
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200673 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Harvey Harrison441b62c2008-03-03 16:08:34 -0800675 dbg("%s - command %d", __func__, command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 command_port = port->serial->port[COMMAND_PORT];
678 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200679 mutex_lock(&command_info->mutex);
Richard Knutsson38c3cb52007-03-26 22:00:28 -0800680 command_info->command_finished = false;
Alan Cox80359a92008-07-22 11:09:16 +0100681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
683 transfer_buffer[0] = command;
Alan Cox80359a92008-07-22 11:09:16 +0100684 memcpy(&transfer_buffer[1], data, datasize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 command_port->write_urb->transfer_buffer_length = datasize + 1;
Alan Cox80359a92008-07-22 11:09:16 +0100686 retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (retval) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800688 dbg("%s - submit urb failed", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto exit;
690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 /* wait for the command to complete */
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200693 t = wait_event_timeout(command_info->wait_command,
Richard Knutsson38c3cb52007-03-26 22:00:28 -0800694 (bool)command_info->command_finished, COMMAND_TIMEOUT);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200695 if (!t)
696 usb_kill_urb(command_port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Richard Knutsson38c3cb52007-03-26 22:00:28 -0800698 if (command_info->command_finished == false) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800699 dbg("%s - command timed out.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 retval = -ETIMEDOUT;
701 goto exit;
702 }
703
704 if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800705 dbg("%s - command failed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 retval = -EIO;
707 goto exit;
708 }
709
710 if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800711 dbg("%s - command completed.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 switch (command) {
Alan Cox80359a92008-07-22 11:09:16 +0100713 case WHITEHEAT_GET_DTR_RTS:
714 info = usb_get_serial_port_data(port);
715 memcpy(&info->mcr, command_info->result_buffer,
716 sizeof(struct whiteheat_dr_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 break;
718 }
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200721 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return retval;
723}
724
725
Alan Cox80359a92008-07-22 11:09:16 +0100726static int firm_open(struct usb_serial_port *port)
727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct whiteheat_simple open_command;
729
730 open_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +0100731 return firm_send_command(port, WHITEHEAT_OPEN,
732 (__u8 *)&open_command, sizeof(open_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735
Alan Cox80359a92008-07-22 11:09:16 +0100736static int firm_close(struct usb_serial_port *port)
737{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct whiteheat_simple close_command;
739
740 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +0100741 return firm_send_command(port, WHITEHEAT_CLOSE,
742 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700746static void firm_setup_port(struct tty_struct *tty)
Alan Cox80359a92008-07-22 11:09:16 +0100747{
Alan Cox95da3102008-07-22 11:09:07 +0100748 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct whiteheat_port_settings port_settings;
Alan Cox95da3102008-07-22 11:09:07 +0100750 unsigned int cflag = tty->termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 port_settings.port = port->number + 1;
753
754 /* get the byte size */
755 switch (cflag & CSIZE) {
Alan Cox80359a92008-07-22 11:09:16 +0100756 case CS5: port_settings.bits = 5; break;
757 case CS6: port_settings.bits = 6; break;
758 case CS7: port_settings.bits = 7; break;
759 default:
760 case CS8: port_settings.bits = 8; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Harvey Harrison441b62c2008-03-03 16:08:34 -0800762 dbg("%s - data bits = %d", __func__, port_settings.bits);
Alan Cox80359a92008-07-22 11:09:16 +0100763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /* determine the parity */
765 if (cflag & PARENB)
766 if (cflag & CMSPAR)
767 if (cflag & PARODD)
768 port_settings.parity = WHITEHEAT_PAR_MARK;
769 else
770 port_settings.parity = WHITEHEAT_PAR_SPACE;
771 else
772 if (cflag & PARODD)
773 port_settings.parity = WHITEHEAT_PAR_ODD;
774 else
775 port_settings.parity = WHITEHEAT_PAR_EVEN;
776 else
777 port_settings.parity = WHITEHEAT_PAR_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800778 dbg("%s - parity = %c", __func__, port_settings.parity);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* figure out the stop bits requested */
781 if (cflag & CSTOPB)
782 port_settings.stop = 2;
783 else
784 port_settings.stop = 1;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800785 dbg("%s - stop bits = %d", __func__, port_settings.stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 /* figure out the flow control settings */
788 if (cflag & CRTSCTS)
Alan Cox80359a92008-07-22 11:09:16 +0100789 port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
790 WHITEHEAT_HFLOW_RTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 else
792 port_settings.hflow = WHITEHEAT_HFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800793 dbg("%s - hardware flow control = %s %s %s %s", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
795 (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
796 (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
797 (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
Alan Cox80359a92008-07-22 11:09:16 +0100798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /* determine software flow control */
Alan Cox95da3102008-07-22 11:09:07 +0100800 if (I_IXOFF(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
802 else
803 port_settings.sflow = WHITEHEAT_SFLOW_NONE;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800804 dbg("%s - software flow control = %c", __func__, port_settings.sflow);
Alan Cox80359a92008-07-22 11:09:16 +0100805
Alan Cox95da3102008-07-22 11:09:07 +0100806 port_settings.xon = START_CHAR(tty);
807 port_settings.xoff = STOP_CHAR(tty);
Alan Cox80359a92008-07-22 11:09:16 +0100808 dbg("%s - XON = %2x, XOFF = %2x",
809 __func__, port_settings.xon, port_settings.xoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* get the baud rate wanted */
Alan Cox95da3102008-07-22 11:09:07 +0100812 port_settings.baud = tty_get_baud_rate(tty);
Harvey Harrison441b62c2008-03-03 16:08:34 -0800813 dbg("%s - baud rate = %d", __func__, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Alan Cox01d1df22007-10-18 01:24:23 -0700815 /* fixme: should set validated settings */
Alan Cox95da3102008-07-22 11:09:07 +0100816 tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* handle any settings that aren't specified in the tty structure */
818 port_settings.lloop = 0;
Alan Cox80359a92008-07-22 11:09:16 +0100819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* now send the message to the device */
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700821 firm_send_command(port, WHITEHEAT_SETUP_PORT,
Alan Cox80359a92008-07-22 11:09:16 +0100822 (__u8 *)&port_settings, sizeof(port_settings));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825
Alan Cox80359a92008-07-22 11:09:16 +0100826static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 struct whiteheat_set_rdb rts_command;
829
830 rts_command.port = port->number - port->serial->minor + 1;
831 rts_command.state = onoff;
Alan Cox80359a92008-07-22 11:09:16 +0100832 return firm_send_command(port, WHITEHEAT_SET_RTS,
833 (__u8 *)&rts_command, sizeof(rts_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836
Alan Cox80359a92008-07-22 11:09:16 +0100837static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
838{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct whiteheat_set_rdb dtr_command;
840
841 dtr_command.port = port->number - port->serial->minor + 1;
842 dtr_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +0100843 return firm_send_command(port, WHITEHEAT_SET_DTR,
Alan Cox80359a92008-07-22 11:09:16 +0100844 (__u8 *)&dtr_command, sizeof(dtr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847
Alan Cox80359a92008-07-22 11:09:16 +0100848static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
849{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct whiteheat_set_rdb break_command;
851
852 break_command.port = port->number - port->serial->minor + 1;
853 break_command.state = onoff;
Alan Cox72e27412008-07-22 11:09:29 +0100854 return firm_send_command(port, WHITEHEAT_SET_BREAK,
Alan Cox80359a92008-07-22 11:09:16 +0100855 (__u8 *)&break_command, sizeof(break_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858
Alan Cox80359a92008-07-22 11:09:16 +0100859static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
860{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct whiteheat_purge purge_command;
862
863 purge_command.port = port->number - port->serial->minor + 1;
864 purge_command.what = rxtx;
Alan Cox80359a92008-07-22 11:09:16 +0100865 return firm_send_command(port, WHITEHEAT_PURGE,
866 (__u8 *)&purge_command, sizeof(purge_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869
Alan Cox80359a92008-07-22 11:09:16 +0100870static int firm_get_dtr_rts(struct usb_serial_port *port)
871{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 struct whiteheat_simple get_dr_command;
873
874 get_dr_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +0100875 return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
876 (__u8 *)&get_dr_command, sizeof(get_dr_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
879
Alan Cox80359a92008-07-22 11:09:16 +0100880static int firm_report_tx_done(struct usb_serial_port *port)
881{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct whiteheat_simple close_command;
883
884 close_command.port = port->number - port->serial->minor + 1;
Alan Cox80359a92008-07-22 11:09:16 +0100885 return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
886 (__u8 *)&close_command, sizeof(close_command));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889
890/*****************************************************************************
891 * Connect Tech's White Heat utility functions
892 *****************************************************************************/
893static int start_command_port(struct usb_serial *serial)
894{
895 struct usb_serial_port *command_port;
896 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 int retval = 0;
Alan Cox80359a92008-07-22 11:09:16 +0100898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 command_port = serial->port[COMMAND_PORT];
900 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200901 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (!command_info->port_running) {
903 /* Work around HCD bugs */
904 usb_clear_halt(serial->dev, command_port->read_urb->pipe);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
907 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700908 dev_err(&serial->dev->dev,
909 "%s - failed submitting read urb, error %d\n",
910 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto exit;
912 }
913 }
914 command_info->port_running++;
915
916exit:
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200917 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return retval;
919}
920
921
922static void stop_command_port(struct usb_serial *serial)
923{
924 struct usb_serial_port *command_port;
925 struct whiteheat_command_private *command_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 command_port = serial->port[COMMAND_PORT];
928 command_info = usb_get_serial_port_data(command_port);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200929 mutex_lock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 command_info->port_running--;
931 if (!command_info->port_running)
932 usb_kill_urb(command_port->read_urb);
Oliver Neukum08a2b3b2007-05-24 13:52:51 +0200933 mutex_unlock(&command_info->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Greg Kroah-Hartman421f3882012-02-28 13:12:44 -0800936module_usb_serial_driver(whiteheat_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Alan Cox80359a92008-07-22 11:09:16 +0100938MODULE_AUTHOR(DRIVER_AUTHOR);
939MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940MODULE_LICENSE("GPL");
941
David Woodhouseec6752f2008-05-31 01:35:29 +0300942MODULE_FIRMWARE("whiteheat.fw");
943MODULE_FIRMWARE("whiteheat_loader.fw");
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945module_param(debug, bool, S_IRUGO | S_IWUSR);
946MODULE_PARM_DESC(debug, "Debug enabled or not");