xref: /openbmc/linux/drivers/usb/serial/whiteheat.c (revision fd589a8f)
1 /*
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  *
15  * See Documentation/usb/usb-serial.txt for more information on using this
16  * driver
17  *
18  * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com)
19  *	Upgrade to full working driver
20  *
21  * (05/30/2001) gkh
22  *	switched from using spinlock to a semaphore, which fixes lots of
23  *	problems.
24  *
25  * (04/08/2001) gb
26  *	Identify version on module load.
27  *
28  * 2001_Mar_19 gkh
29  *	Fixed MOD_INC and MOD_DEC logic, the ability to open a port more
30  *	than once, and the got the proper usb_device_id table entries so
31  *	the driver works again.
32  *
33  * (11/01/2000) Adam J. Richter
34  *	usb_device_id table support
35  *
36  * (10/05/2000) gkh
37  *	Fixed bug with urb->dev not being set properly, now that the usb
38  *	core needs it.
39  *
40  * (10/03/2000) smd
41  *	firmware is improved to guard against crap sent to device
42  *	firmware now replies CMD_FAILURE on bad things
43  *	read_callback fix you provided for private info struct
44  *	command_finished now indicates success or fail
45  *	setup_port struct now packed to avoid gcc padding
46  *	firmware uses 1 based port numbering, driver now handles that
47  *
48  * (09/11/2000) gkh
49  *	Removed DEBUG #ifdefs with call to usb_serial_debug_data
50  *
51  * (07/19/2000) gkh
52  *	Added module_init and module_exit functions to handle the fact that this
53  *	driver is a loadable module now.
54  *	Fixed bug with port->minor that was found by Al Borchers
55  *
56  * (07/04/2000) gkh
57  *	Added support for port settings. Baud rate can now be changed. Line
58  *	signals are not transferred to and from the tty layer yet, but things
59  *	seem to be working well now.
60  *
61  * (05/04/2000) gkh
62  *	First cut at open and close commands. Data can flow through the ports at
63  *	default speeds now.
64  *
65  * (03/26/2000) gkh
66  *	Split driver up into device specific pieces.
67  *
68  */
69 
70 #include <linux/kernel.h>
71 #include <linux/errno.h>
72 #include <linux/init.h>
73 #include <linux/slab.h>
74 #include <linux/tty.h>
75 #include <linux/tty_driver.h>
76 #include <linux/tty_flip.h>
77 #include <linux/module.h>
78 #include <linux/spinlock.h>
79 #include <linux/mutex.h>
80 #include <linux/uaccess.h>
81 #include <asm/termbits.h>
82 #include <linux/usb.h>
83 #include <linux/serial_reg.h>
84 #include <linux/serial.h>
85 #include <linux/usb/serial.h>
86 #include <linux/firmware.h>
87 #include <linux/ihex.h>
88 #include "whiteheat.h"			/* WhiteHEAT specific commands */
89 
90 static int debug;
91 
92 #ifndef CMSPAR
93 #define CMSPAR 0
94 #endif
95 
96 /*
97  * Version Information
98  */
99 #define DRIVER_VERSION "v2.0"
100 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
101 #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
102 
103 #define CONNECT_TECH_VENDOR_ID		0x0710
104 #define CONNECT_TECH_FAKE_WHITE_HEAT_ID	0x0001
105 #define CONNECT_TECH_WHITE_HEAT_ID	0x8001
106 
107 /*
108    ID tables for whiteheat are unusual, because we want to different
109    things for different versions of the device.  Eventually, this
110    will be doable from a single table.  But, for now, we define two
111    separate ID tables, and then a third table that combines them
112    just for the purpose of exporting the autoloading information.
113 */
114 static struct usb_device_id id_table_std [] = {
115 	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
116 	{ }						/* Terminating entry */
117 };
118 
119 static struct usb_device_id id_table_prerenumeration [] = {
120 	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
121 	{ }						/* Terminating entry */
122 };
123 
124 static struct usb_device_id id_table_combined [] = {
125 	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
126 	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
127 	{ }						/* Terminating entry */
128 };
129 
130 MODULE_DEVICE_TABLE(usb, id_table_combined);
131 
132 static struct usb_driver whiteheat_driver = {
133 	.name =		"whiteheat",
134 	.probe =	usb_serial_probe,
135 	.disconnect =	usb_serial_disconnect,
136 	.id_table =	id_table_combined,
137 	.no_dynamic_id = 	1,
138 };
139 
140 /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
141 static int  whiteheat_firmware_download(struct usb_serial *serial,
142 					const struct usb_device_id *id);
143 static int  whiteheat_firmware_attach(struct usb_serial *serial);
144 
145 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
146 static int  whiteheat_attach(struct usb_serial *serial);
147 static void whiteheat_release(struct usb_serial *serial);
148 static int  whiteheat_open(struct tty_struct *tty,
149 			struct usb_serial_port *port);
150 static void whiteheat_close(struct usb_serial_port *port);
151 static int  whiteheat_write(struct tty_struct *tty,
152 			struct usb_serial_port *port,
153 			const unsigned char *buf, int count);
154 static int  whiteheat_write_room(struct tty_struct *tty);
155 static int  whiteheat_ioctl(struct tty_struct *tty, struct file *file,
156 			unsigned int cmd, unsigned long arg);
157 static void whiteheat_set_termios(struct tty_struct *tty,
158 			struct usb_serial_port *port, struct ktermios *old);
159 static int  whiteheat_tiocmget(struct tty_struct *tty, struct file *file);
160 static int  whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
161 			unsigned int set, unsigned int clear);
162 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
163 static int  whiteheat_chars_in_buffer(struct tty_struct *tty);
164 static void whiteheat_throttle(struct tty_struct *tty);
165 static void whiteheat_unthrottle(struct tty_struct *tty);
166 static void whiteheat_read_callback(struct urb *urb);
167 static void whiteheat_write_callback(struct urb *urb);
168 
169 static struct usb_serial_driver whiteheat_fake_device = {
170 	.driver = {
171 		.owner =	THIS_MODULE,
172 		.name =		"whiteheatnofirm",
173 	},
174 	.description =		"Connect Tech - WhiteHEAT - (prerenumeration)",
175 	.usb_driver =		&whiteheat_driver,
176 	.id_table =		id_table_prerenumeration,
177 	.num_ports =		1,
178 	.probe =		whiteheat_firmware_download,
179 	.attach =		whiteheat_firmware_attach,
180 };
181 
182 static struct usb_serial_driver whiteheat_device = {
183 	.driver = {
184 		.owner =	THIS_MODULE,
185 		.name =		"whiteheat",
186 	},
187 	.description =		"Connect Tech - WhiteHEAT",
188 	.usb_driver =		&whiteheat_driver,
189 	.id_table =		id_table_std,
190 	.num_ports =		4,
191 	.attach =		whiteheat_attach,
192 	.release =		whiteheat_release,
193 	.open =			whiteheat_open,
194 	.close =		whiteheat_close,
195 	.write =		whiteheat_write,
196 	.write_room =		whiteheat_write_room,
197 	.ioctl =		whiteheat_ioctl,
198 	.set_termios =		whiteheat_set_termios,
199 	.break_ctl =		whiteheat_break_ctl,
200 	.tiocmget =		whiteheat_tiocmget,
201 	.tiocmset =		whiteheat_tiocmset,
202 	.chars_in_buffer =	whiteheat_chars_in_buffer,
203 	.throttle =		whiteheat_throttle,
204 	.unthrottle =		whiteheat_unthrottle,
205 	.read_bulk_callback =	whiteheat_read_callback,
206 	.write_bulk_callback =	whiteheat_write_callback,
207 };
208 
209 
210 struct whiteheat_command_private {
211 	struct mutex		mutex;
212 	__u8			port_running;
213 	__u8			command_finished;
214 	wait_queue_head_t	wait_command; /* for handling sleeping whilst
215 						 waiting for a command to
216 						 finish */
217 	__u8			result_buffer[64];
218 };
219 
220 
221 #define THROTTLED		0x01
222 #define ACTUALLY_THROTTLED	0x02
223 
224 static int urb_pool_size = 8;
225 
226 struct whiteheat_urb_wrap {
227 	struct list_head	list;
228 	struct urb		*urb;
229 };
230 
231 struct whiteheat_private {
232 	spinlock_t		lock;
233 	__u8			flags;
234 	__u8			mcr;		/* FIXME: no locking on mcr */
235 	struct list_head	rx_urbs_free;
236 	struct list_head	rx_urbs_submitted;
237 	struct list_head	rx_urb_q;
238 	struct work_struct	rx_work;
239 	struct usb_serial_port	*port;
240 	struct list_head	tx_urbs_free;
241 	struct list_head	tx_urbs_submitted;
242 	struct mutex		deathwarrant;
243 };
244 
245 
246 /* local function prototypes */
247 static int start_command_port(struct usb_serial *serial);
248 static void stop_command_port(struct usb_serial *serial);
249 static void command_port_write_callback(struct urb *urb);
250 static void command_port_read_callback(struct urb *urb);
251 
252 static int start_port_read(struct usb_serial_port *port);
253 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
254 						struct list_head *head);
255 static struct list_head *list_first(struct list_head *head);
256 static void rx_data_softint(struct work_struct *work);
257 
258 static int firm_send_command(struct usb_serial_port *port, __u8 command,
259 						__u8 *data, __u8 datasize);
260 static int firm_open(struct usb_serial_port *port);
261 static int firm_close(struct usb_serial_port *port);
262 static void firm_setup_port(struct tty_struct *tty);
263 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
264 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
265 static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
266 static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
267 static int firm_get_dtr_rts(struct usb_serial_port *port);
268 static int firm_report_tx_done(struct usb_serial_port *port);
269 
270 
271 #define COMMAND_PORT		4
272 #define COMMAND_TIMEOUT		(2*HZ)	/* 2 second timeout for a command */
273 #define	COMMAND_TIMEOUT_MS	2000
274 #define CLOSING_DELAY		(30 * HZ)
275 
276 
277 /*****************************************************************************
278  * Connect Tech's White Heat prerenumeration driver functions
279  *****************************************************************************/
280 
281 /* steps to download the firmware to the WhiteHEAT device:
282  - hold the reset (by writing to the reset bit of the CPUCS register)
283  - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
284  - release the reset (by writing to the CPUCS register)
285  - download the WH.HEX file for all addresses greater than 0x1b3f using
286    VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
287  - hold the reset
288  - download the WH.HEX file for all addresses less than 0x1b40 using
289    VENDOR_REQUEST_ANCHOR_LOAD
290  - release the reset
291  - device renumerated itself and comes up as new device id with all
292    firmware download completed.
293 */
294 static int whiteheat_firmware_download(struct usb_serial *serial,
295 					const struct usb_device_id *id)
296 {
297 	int response, ret = -ENOENT;
298 	const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
299 	const struct ihex_binrec *record;
300 
301 	dbg("%s", __func__);
302 
303 	if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
304 				  &serial->dev->dev)) {
305 		dev_err(&serial->dev->dev,
306 			"%s - request \"whiteheat.fw\" failed\n", __func__);
307 		goto out;
308 	}
309 	if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
310 			     &serial->dev->dev)) {
311 		dev_err(&serial->dev->dev,
312 			"%s - request \"whiteheat_loader.fw\" failed\n",
313 			__func__);
314 		goto out;
315 	}
316 	ret = 0;
317 	response = ezusb_set_reset (serial, 1);
318 
319 	record = (const struct ihex_binrec *)loader_fw->data;
320 	while (record) {
321 		response = ezusb_writememory (serial, be32_to_cpu(record->addr),
322 					      (unsigned char *)record->data,
323 					      be16_to_cpu(record->len), 0xa0);
324 		if (response < 0) {
325 			dev_err(&serial->dev->dev, "%s - ezusb_writememory "
326 				"failed for loader (%d %04X %p %d)\n",
327 				__func__, response, be32_to_cpu(record->addr),
328 				record->data, be16_to_cpu(record->len));
329 			break;
330 		}
331 		record = ihex_next_binrec(record);
332 	}
333 
334 	response = ezusb_set_reset(serial, 0);
335 
336 	record = (const struct ihex_binrec *)firmware_fw->data;
337 	while (record && be32_to_cpu(record->addr) < 0x1b40)
338 		record = ihex_next_binrec(record);
339 	while (record) {
340 		response = ezusb_writememory (serial, be32_to_cpu(record->addr),
341 					      (unsigned char *)record->data,
342 					      be16_to_cpu(record->len), 0xa3);
343 		if (response < 0) {
344 			dev_err(&serial->dev->dev, "%s - ezusb_writememory "
345 				"failed for first firmware step "
346 				"(%d %04X %p %d)\n", __func__, response,
347 				be32_to_cpu(record->addr), record->data,
348 				be16_to_cpu(record->len));
349 			break;
350 		}
351 		++record;
352 	}
353 
354 	response = ezusb_set_reset(serial, 1);
355 
356 	record = (const struct ihex_binrec *)firmware_fw->data;
357 	while (record && be32_to_cpu(record->addr) < 0x1b40) {
358 		response = ezusb_writememory (serial, be32_to_cpu(record->addr),
359 					      (unsigned char *)record->data,
360 					      be16_to_cpu(record->len), 0xa0);
361 		if (response < 0) {
362 			dev_err(&serial->dev->dev, "%s - ezusb_writememory "
363 				"failed for second firmware step "
364 				"(%d %04X %p %d)\n", __func__, response,
365 				be32_to_cpu(record->addr), record->data,
366 				be16_to_cpu(record->len));
367 			break;
368 		}
369 		++record;
370 	}
371 	ret = 0;
372 	response = ezusb_set_reset (serial, 0);
373  out:
374 	release_firmware(loader_fw);
375 	release_firmware(firmware_fw);
376 	return ret;
377 }
378 
379 
380 static int whiteheat_firmware_attach(struct usb_serial *serial)
381 {
382 	/* We want this device to fail to have a driver assigned to it */
383 	return 1;
384 }
385 
386 
387 /*****************************************************************************
388  * Connect Tech's White Heat serial driver functions
389  *****************************************************************************/
390 static int whiteheat_attach(struct usb_serial *serial)
391 {
392 	struct usb_serial_port *command_port;
393 	struct whiteheat_command_private *command_info;
394 	struct usb_serial_port *port;
395 	struct whiteheat_private *info;
396 	struct whiteheat_hw_info *hw_info;
397 	int pipe;
398 	int ret;
399 	int alen;
400 	__u8 *command;
401 	__u8 *result;
402 	int i;
403 	int j;
404 	struct urb *urb;
405 	int buf_size;
406 	struct whiteheat_urb_wrap *wrap;
407 	struct list_head *tmp;
408 
409 	command_port = serial->port[COMMAND_PORT];
410 
411 	pipe = usb_sndbulkpipe(serial->dev,
412 			command_port->bulk_out_endpointAddress);
413 	command = kmalloc(2, GFP_KERNEL);
414 	if (!command)
415 		goto no_command_buffer;
416 	command[0] = WHITEHEAT_GET_HW_INFO;
417 	command[1] = 0;
418 
419 	result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
420 	if (!result)
421 		goto no_result_buffer;
422 	/*
423 	 * When the module is reloaded the firmware is still there and
424 	 * the endpoints are still in the usb core unchanged. This is the
425 	 * unlinking bug in disguise. Same for the call below.
426 	 */
427 	usb_clear_halt(serial->dev, pipe);
428 	ret = usb_bulk_msg(serial->dev, pipe, command, 2,
429 						&alen, COMMAND_TIMEOUT_MS);
430 	if (ret) {
431 		dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
432 			serial->type->description, ret);
433 		goto no_firmware;
434 	} else if (alen != 2) {
435 		dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
436 			serial->type->description, alen);
437 		goto no_firmware;
438 	}
439 
440 	pipe = usb_rcvbulkpipe(serial->dev,
441 				command_port->bulk_in_endpointAddress);
442 	/* See the comment on the usb_clear_halt() above */
443 	usb_clear_halt(serial->dev, pipe);
444 	ret = usb_bulk_msg(serial->dev, pipe, result,
445 			sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
446 	if (ret) {
447 		dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
448 			serial->type->description, ret);
449 		goto no_firmware;
450 	} else if (alen != sizeof(*hw_info) + 1) {
451 		dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
452 			serial->type->description, alen);
453 		goto no_firmware;
454 	} else if (result[0] != command[0]) {
455 		dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
456 			serial->type->description, result[0]);
457 		goto no_firmware;
458 	}
459 
460 	hw_info = (struct whiteheat_hw_info *)&result[1];
461 
462 	dev_info(&serial->dev->dev, "%s: Driver %s: Firmware v%d.%02d\n",
463 		 serial->type->description, DRIVER_VERSION,
464 		 hw_info->sw_major_rev, hw_info->sw_minor_rev);
465 
466 	for (i = 0; i < serial->num_ports; i++) {
467 		port = serial->port[i];
468 
469 		info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
470 		if (info == NULL) {
471 			dev_err(&port->dev,
472 				"%s: Out of memory for port structures\n",
473 				serial->type->description);
474 			goto no_private;
475 		}
476 
477 		spin_lock_init(&info->lock);
478 		mutex_init(&info->deathwarrant);
479 		info->flags = 0;
480 		info->mcr = 0;
481 		INIT_WORK(&info->rx_work, rx_data_softint);
482 		info->port = port;
483 
484 		INIT_LIST_HEAD(&info->rx_urbs_free);
485 		INIT_LIST_HEAD(&info->rx_urbs_submitted);
486 		INIT_LIST_HEAD(&info->rx_urb_q);
487 		INIT_LIST_HEAD(&info->tx_urbs_free);
488 		INIT_LIST_HEAD(&info->tx_urbs_submitted);
489 
490 		for (j = 0; j < urb_pool_size; j++) {
491 			urb = usb_alloc_urb(0, GFP_KERNEL);
492 			if (!urb) {
493 				dev_err(&port->dev, "No free urbs available\n");
494 				goto no_rx_urb;
495 			}
496 			buf_size = port->read_urb->transfer_buffer_length;
497 			urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
498 			if (!urb->transfer_buffer) {
499 				dev_err(&port->dev,
500 					"Couldn't allocate urb buffer\n");
501 				goto no_rx_buf;
502 			}
503 			wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
504 			if (!wrap) {
505 				dev_err(&port->dev,
506 					"Couldn't allocate urb wrapper\n");
507 				goto no_rx_wrap;
508 			}
509 			usb_fill_bulk_urb(urb, serial->dev,
510 					usb_rcvbulkpipe(serial->dev,
511 						port->bulk_in_endpointAddress),
512 					urb->transfer_buffer, buf_size,
513 					whiteheat_read_callback, port);
514 			wrap->urb = urb;
515 			list_add(&wrap->list, &info->rx_urbs_free);
516 
517 			urb = usb_alloc_urb(0, GFP_KERNEL);
518 			if (!urb) {
519 				dev_err(&port->dev, "No free urbs available\n");
520 				goto no_tx_urb;
521 			}
522 			buf_size = port->write_urb->transfer_buffer_length;
523 			urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
524 			if (!urb->transfer_buffer) {
525 				dev_err(&port->dev,
526 					"Couldn't allocate urb buffer\n");
527 				goto no_tx_buf;
528 			}
529 			wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
530 			if (!wrap) {
531 				dev_err(&port->dev,
532 					"Couldn't allocate urb wrapper\n");
533 				goto no_tx_wrap;
534 			}
535 			usb_fill_bulk_urb(urb, serial->dev,
536 					usb_sndbulkpipe(serial->dev,
537 						port->bulk_out_endpointAddress),
538 					urb->transfer_buffer, buf_size,
539 					whiteheat_write_callback, port);
540 			wrap->urb = urb;
541 			list_add(&wrap->list, &info->tx_urbs_free);
542 		}
543 
544 		usb_set_serial_port_data(port, info);
545 	}
546 
547 	command_info = kmalloc(sizeof(struct whiteheat_command_private),
548 								GFP_KERNEL);
549 	if (command_info == NULL) {
550 		dev_err(&serial->dev->dev,
551 			"%s: Out of memory for port structures\n",
552 			serial->type->description);
553 		goto no_command_private;
554 	}
555 
556 	mutex_init(&command_info->mutex);
557 	command_info->port_running = 0;
558 	init_waitqueue_head(&command_info->wait_command);
559 	usb_set_serial_port_data(command_port, command_info);
560 	command_port->write_urb->complete = command_port_write_callback;
561 	command_port->read_urb->complete = command_port_read_callback;
562 	kfree(result);
563 	kfree(command);
564 
565 	return 0;
566 
567 no_firmware:
568 	/* Firmware likely not running */
569 	dev_err(&serial->dev->dev,
570 		"%s: Unable to retrieve firmware version, try replugging\n",
571 		serial->type->description);
572 	dev_err(&serial->dev->dev,
573 		"%s: If the firmware is not running (status led not blinking)\n",
574 		serial->type->description);
575 	dev_err(&serial->dev->dev,
576 		"%s: please contact support@connecttech.com\n",
577 		serial->type->description);
578 	kfree(result);
579 	return -ENODEV;
580 
581 no_command_private:
582 	for (i = serial->num_ports - 1; i >= 0; i--) {
583 		port = serial->port[i];
584 		info = usb_get_serial_port_data(port);
585 		for (j = urb_pool_size - 1; j >= 0; j--) {
586 			tmp = list_first(&info->tx_urbs_free);
587 			list_del(tmp);
588 			wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
589 			urb = wrap->urb;
590 			kfree(wrap);
591 no_tx_wrap:
592 			kfree(urb->transfer_buffer);
593 no_tx_buf:
594 			usb_free_urb(urb);
595 no_tx_urb:
596 			tmp = list_first(&info->rx_urbs_free);
597 			list_del(tmp);
598 			wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
599 			urb = wrap->urb;
600 			kfree(wrap);
601 no_rx_wrap:
602 			kfree(urb->transfer_buffer);
603 no_rx_buf:
604 			usb_free_urb(urb);
605 no_rx_urb:
606 			;
607 		}
608 		kfree(info);
609 no_private:
610 		;
611 	}
612 	kfree(result);
613 no_result_buffer:
614 	kfree(command);
615 no_command_buffer:
616 	return -ENOMEM;
617 }
618 
619 
620 static void whiteheat_release(struct usb_serial *serial)
621 {
622 	struct usb_serial_port *command_port;
623 	struct usb_serial_port *port;
624 	struct whiteheat_private *info;
625 	struct whiteheat_urb_wrap *wrap;
626 	struct urb *urb;
627 	struct list_head *tmp;
628 	struct list_head *tmp2;
629 	int i;
630 
631 	dbg("%s", __func__);
632 
633 	/* free up our private data for our command port */
634 	command_port = serial->port[COMMAND_PORT];
635 	kfree(usb_get_serial_port_data(command_port));
636 
637 	for (i = 0; i < serial->num_ports; i++) {
638 		port = serial->port[i];
639 		info = usb_get_serial_port_data(port);
640 		list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
641 			list_del(tmp);
642 			wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
643 			urb = wrap->urb;
644 			kfree(wrap);
645 			kfree(urb->transfer_buffer);
646 			usb_free_urb(urb);
647 		}
648 		list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
649 			list_del(tmp);
650 			wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
651 			urb = wrap->urb;
652 			kfree(wrap);
653 			kfree(urb->transfer_buffer);
654 			usb_free_urb(urb);
655 		}
656 		kfree(info);
657 	}
658 
659 	return;
660 }
661 
662 static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
663 {
664 	int		retval = 0;
665 
666 	dbg("%s - port %d", __func__, port->number);
667 
668 	retval = start_command_port(port->serial);
669 	if (retval)
670 		goto exit;
671 
672 	if (tty)
673 		tty->low_latency = 1;
674 
675 	/* send an open port command */
676 	retval = firm_open(port);
677 	if (retval) {
678 		stop_command_port(port->serial);
679 		goto exit;
680 	}
681 
682 	retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
683 	if (retval) {
684 		firm_close(port);
685 		stop_command_port(port->serial);
686 		goto exit;
687 	}
688 
689 	if (tty)
690 		firm_setup_port(tty);
691 
692 	/* Work around HCD bugs */
693 	usb_clear_halt(port->serial->dev, port->read_urb->pipe);
694 	usb_clear_halt(port->serial->dev, port->write_urb->pipe);
695 
696 	/* Start reading from the device */
697 	retval = start_port_read(port);
698 	if (retval) {
699 		dev_err(&port->dev,
700 			"%s - failed submitting read urb, error %d\n",
701 			__func__, retval);
702 		firm_close(port);
703 		stop_command_port(port->serial);
704 		goto exit;
705 	}
706 
707 exit:
708 	dbg("%s - exit, retval = %d", __func__, retval);
709 	return retval;
710 }
711 
712 
713 static void whiteheat_close(struct usb_serial_port *port)
714 {
715 	struct whiteheat_private *info = usb_get_serial_port_data(port);
716 	struct whiteheat_urb_wrap *wrap;
717 	struct urb *urb;
718 	struct list_head *tmp;
719 	struct list_head *tmp2;
720 
721 	dbg("%s - port %d", __func__, port->number);
722 
723 	firm_report_tx_done(port);
724 	firm_close(port);
725 
726 	/* shutdown our bulk reads and writes */
727 	mutex_lock(&info->deathwarrant);
728 	spin_lock_irq(&info->lock);
729 	list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
730 		wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
731 		urb = wrap->urb;
732 		list_del(tmp);
733 		spin_unlock_irq(&info->lock);
734 		usb_kill_urb(urb);
735 		spin_lock_irq(&info->lock);
736 		list_add(tmp, &info->rx_urbs_free);
737 	}
738 	list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
739 		list_move(tmp, &info->rx_urbs_free);
740 	list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
741 		wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
742 		urb = wrap->urb;
743 		list_del(tmp);
744 		spin_unlock_irq(&info->lock);
745 		usb_kill_urb(urb);
746 		spin_lock_irq(&info->lock);
747 		list_add(tmp, &info->tx_urbs_free);
748 	}
749 	spin_unlock_irq(&info->lock);
750 	mutex_unlock(&info->deathwarrant);
751 	stop_command_port(port->serial);
752 }
753 
754 
755 static int whiteheat_write(struct tty_struct *tty,
756 	struct usb_serial_port *port, const unsigned char *buf, int count)
757 {
758 	struct usb_serial *serial = port->serial;
759 	struct whiteheat_private *info = usb_get_serial_port_data(port);
760 	struct whiteheat_urb_wrap *wrap;
761 	struct urb *urb;
762 	int result;
763 	int bytes;
764 	int sent = 0;
765 	unsigned long flags;
766 	struct list_head *tmp;
767 
768 	dbg("%s - port %d", __func__, port->number);
769 
770 	if (count == 0) {
771 		dbg("%s - write request of 0 bytes", __func__);
772 		return (0);
773 	}
774 
775 	while (count) {
776 		spin_lock_irqsave(&info->lock, flags);
777 		if (list_empty(&info->tx_urbs_free)) {
778 			spin_unlock_irqrestore(&info->lock, flags);
779 			break;
780 		}
781 		tmp = list_first(&info->tx_urbs_free);
782 		list_del(tmp);
783 		spin_unlock_irqrestore(&info->lock, flags);
784 
785 		wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
786 		urb = wrap->urb;
787 		bytes = (count > port->bulk_out_size) ?
788 					port->bulk_out_size : count;
789 		memcpy(urb->transfer_buffer, buf + sent, bytes);
790 
791 		usb_serial_debug_data(debug, &port->dev,
792 				__func__, bytes, urb->transfer_buffer);
793 
794 		urb->dev = serial->dev;
795 		urb->transfer_buffer_length = bytes;
796 		result = usb_submit_urb(urb, GFP_ATOMIC);
797 		if (result) {
798 			dev_err(&port->dev,
799 				"%s - failed submitting write urb, error %d\n",
800 				__func__, result);
801 			sent = result;
802 			spin_lock_irqsave(&info->lock, flags);
803 			list_add(tmp, &info->tx_urbs_free);
804 			spin_unlock_irqrestore(&info->lock, flags);
805 			break;
806 		} else {
807 			sent += bytes;
808 			count -= bytes;
809 			spin_lock_irqsave(&info->lock, flags);
810 			list_add(tmp, &info->tx_urbs_submitted);
811 			spin_unlock_irqrestore(&info->lock, flags);
812 		}
813 	}
814 
815 	return sent;
816 }
817 
818 static int whiteheat_write_room(struct tty_struct *tty)
819 {
820 	struct usb_serial_port *port = tty->driver_data;
821 	struct whiteheat_private *info = usb_get_serial_port_data(port);
822 	struct list_head *tmp;
823 	int room = 0;
824 	unsigned long flags;
825 
826 	dbg("%s - port %d", __func__, port->number);
827 
828 	spin_lock_irqsave(&info->lock, flags);
829 	list_for_each(tmp, &info->tx_urbs_free)
830 		room++;
831 	spin_unlock_irqrestore(&info->lock, flags);
832 	room *= port->bulk_out_size;
833 
834 	dbg("%s - returns %d", __func__, room);
835 	return (room);
836 }
837 
838 static int whiteheat_tiocmget(struct tty_struct *tty, struct file *file)
839 {
840 	struct usb_serial_port *port = tty->driver_data;
841 	struct whiteheat_private *info = usb_get_serial_port_data(port);
842 	unsigned int modem_signals = 0;
843 
844 	dbg("%s - port %d", __func__, port->number);
845 
846 	firm_get_dtr_rts(port);
847 	if (info->mcr & UART_MCR_DTR)
848 		modem_signals |= TIOCM_DTR;
849 	if (info->mcr & UART_MCR_RTS)
850 		modem_signals |= TIOCM_RTS;
851 
852 	return modem_signals;
853 }
854 
855 static int whiteheat_tiocmset(struct tty_struct *tty, struct file *file,
856 			       unsigned int set, unsigned int clear)
857 {
858 	struct usb_serial_port *port = tty->driver_data;
859 	struct whiteheat_private *info = usb_get_serial_port_data(port);
860 
861 	dbg("%s - port %d", __func__, port->number);
862 
863 	if (set & TIOCM_RTS)
864 		info->mcr |= UART_MCR_RTS;
865 	if (set & TIOCM_DTR)
866 		info->mcr |= UART_MCR_DTR;
867 
868 	if (clear & TIOCM_RTS)
869 		info->mcr &= ~UART_MCR_RTS;
870 	if (clear & TIOCM_DTR)
871 		info->mcr &= ~UART_MCR_DTR;
872 
873 	firm_set_dtr(port, info->mcr & UART_MCR_DTR);
874 	firm_set_rts(port, info->mcr & UART_MCR_RTS);
875 	return 0;
876 }
877 
878 
879 static int whiteheat_ioctl(struct tty_struct *tty, struct file *file,
880 					unsigned int cmd, unsigned long arg)
881 {
882 	struct usb_serial_port *port = tty->driver_data;
883 	struct serial_struct serstruct;
884 	void __user *user_arg = (void __user *)arg;
885 
886 	dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
887 
888 	switch (cmd) {
889 	case TIOCGSERIAL:
890 		memset(&serstruct, 0, sizeof(serstruct));
891 		serstruct.type = PORT_16654;
892 		serstruct.line = port->serial->minor;
893 		serstruct.port = port->number;
894 		serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
895 		serstruct.xmit_fifo_size = port->bulk_out_size;
896 		serstruct.custom_divisor = 0;
897 		serstruct.baud_base = 460800;
898 		serstruct.close_delay = CLOSING_DELAY;
899 		serstruct.closing_wait = CLOSING_DELAY;
900 
901 		if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
902 			return -EFAULT;
903 		break;
904 	default:
905 		break;
906 	}
907 
908 	return -ENOIOCTLCMD;
909 }
910 
911 
912 static void whiteheat_set_termios(struct tty_struct *tty,
913 	struct usb_serial_port *port, struct ktermios *old_termios)
914 {
915 	firm_setup_port(tty);
916 }
917 
918 static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
919 {
920 	struct usb_serial_port *port = tty->driver_data;
921 	firm_set_break(port, break_state);
922 }
923 
924 
925 static int whiteheat_chars_in_buffer(struct tty_struct *tty)
926 {
927 	struct usb_serial_port *port = tty->driver_data;
928 	struct whiteheat_private *info = usb_get_serial_port_data(port);
929 	struct list_head *tmp;
930 	struct whiteheat_urb_wrap *wrap;
931 	int chars = 0;
932 	unsigned long flags;
933 
934 	dbg("%s - port %d", __func__, port->number);
935 
936 	spin_lock_irqsave(&info->lock, flags);
937 	list_for_each(tmp, &info->tx_urbs_submitted) {
938 		wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
939 		chars += wrap->urb->transfer_buffer_length;
940 	}
941 	spin_unlock_irqrestore(&info->lock, flags);
942 
943 	dbg("%s - returns %d", __func__, chars);
944 	return chars;
945 }
946 
947 
948 static void whiteheat_throttle(struct tty_struct *tty)
949 {
950 	struct usb_serial_port *port = tty->driver_data;
951 	struct whiteheat_private *info = usb_get_serial_port_data(port);
952 	unsigned long flags;
953 
954 	dbg("%s - port %d", __func__, port->number);
955 
956 	spin_lock_irqsave(&info->lock, flags);
957 	info->flags |= THROTTLED;
958 	spin_unlock_irqrestore(&info->lock, flags);
959 
960 	return;
961 }
962 
963 
964 static void whiteheat_unthrottle(struct tty_struct *tty)
965 {
966 	struct usb_serial_port *port = tty->driver_data;
967 	struct whiteheat_private *info = usb_get_serial_port_data(port);
968 	int actually_throttled;
969 	unsigned long flags;
970 
971 	dbg("%s - port %d", __func__, port->number);
972 
973 	spin_lock_irqsave(&info->lock, flags);
974 	actually_throttled = info->flags & ACTUALLY_THROTTLED;
975 	info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
976 	spin_unlock_irqrestore(&info->lock, flags);
977 
978 	if (actually_throttled)
979 		rx_data_softint(&info->rx_work);
980 
981 	return;
982 }
983 
984 
985 /*****************************************************************************
986  * Connect Tech's White Heat callback routines
987  *****************************************************************************/
988 static void command_port_write_callback(struct urb *urb)
989 {
990 	int status = urb->status;
991 
992 	dbg("%s", __func__);
993 
994 	if (status) {
995 		dbg("nonzero urb status: %d", status);
996 		return;
997 	}
998 }
999 
1000 
1001 static void command_port_read_callback(struct urb *urb)
1002 {
1003 	struct usb_serial_port *command_port = urb->context;
1004 	struct whiteheat_command_private *command_info;
1005 	int status = urb->status;
1006 	unsigned char *data = urb->transfer_buffer;
1007 	int result;
1008 
1009 	dbg("%s", __func__);
1010 
1011 	command_info = usb_get_serial_port_data(command_port);
1012 	if (!command_info) {
1013 		dbg("%s - command_info is NULL, exiting.", __func__);
1014 		return;
1015 	}
1016 	if (status) {
1017 		dbg("%s - nonzero urb status: %d", __func__, status);
1018 		if (status != -ENOENT)
1019 			command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1020 		wake_up(&command_info->wait_command);
1021 		return;
1022 	}
1023 
1024 	usb_serial_debug_data(debug, &command_port->dev,
1025 				__func__, urb->actual_length, data);
1026 
1027 	if (data[0] == WHITEHEAT_CMD_COMPLETE) {
1028 		command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1029 		wake_up(&command_info->wait_command);
1030 	} else if (data[0] == WHITEHEAT_CMD_FAILURE) {
1031 		command_info->command_finished = WHITEHEAT_CMD_FAILURE;
1032 		wake_up(&command_info->wait_command);
1033 	} else if (data[0] == WHITEHEAT_EVENT) {
1034 		/* These are unsolicited reports from the firmware, hence no
1035 		   waiting command to wakeup */
1036 		dbg("%s - event received", __func__);
1037 	} else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
1038 		memcpy(command_info->result_buffer, &data[1],
1039 						urb->actual_length - 1);
1040 		command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
1041 		wake_up(&command_info->wait_command);
1042 	} else
1043 		dbg("%s - bad reply from firmware", __func__);
1044 
1045 	/* Continue trying to always read */
1046 	command_port->read_urb->dev = command_port->serial->dev;
1047 	result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
1048 	if (result)
1049 		dbg("%s - failed resubmitting read urb, error %d",
1050 			__func__, result);
1051 }
1052 
1053 
1054 static void whiteheat_read_callback(struct urb *urb)
1055 {
1056 	struct usb_serial_port *port = urb->context;
1057 	struct whiteheat_urb_wrap *wrap;
1058 	unsigned char *data = urb->transfer_buffer;
1059 	struct whiteheat_private *info = usb_get_serial_port_data(port);
1060 	int status = urb->status;
1061 
1062 	dbg("%s - port %d", __func__, port->number);
1063 
1064 	spin_lock(&info->lock);
1065 	wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
1066 	if (!wrap) {
1067 		spin_unlock(&info->lock);
1068 		dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1069 		return;
1070 	}
1071 	list_del(&wrap->list);
1072 	spin_unlock(&info->lock);
1073 
1074 	if (status) {
1075 		dbg("%s - nonzero read bulk status received: %d",
1076 		    __func__, status);
1077 		spin_lock(&info->lock);
1078 		list_add(&wrap->list, &info->rx_urbs_free);
1079 		spin_unlock(&info->lock);
1080 		return;
1081 	}
1082 
1083 	usb_serial_debug_data(debug, &port->dev,
1084 				__func__, urb->actual_length, data);
1085 
1086 	spin_lock(&info->lock);
1087 	list_add_tail(&wrap->list, &info->rx_urb_q);
1088 	if (info->flags & THROTTLED) {
1089 		info->flags |= ACTUALLY_THROTTLED;
1090 		spin_unlock(&info->lock);
1091 		return;
1092 	}
1093 	spin_unlock(&info->lock);
1094 
1095 	schedule_work(&info->rx_work);
1096 }
1097 
1098 
1099 static void whiteheat_write_callback(struct urb *urb)
1100 {
1101 	struct usb_serial_port *port = urb->context;
1102 	struct whiteheat_private *info = usb_get_serial_port_data(port);
1103 	struct whiteheat_urb_wrap *wrap;
1104 	int status = urb->status;
1105 
1106 	dbg("%s - port %d", __func__, port->number);
1107 
1108 	spin_lock(&info->lock);
1109 	wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
1110 	if (!wrap) {
1111 		spin_unlock(&info->lock);
1112 		dev_err(&port->dev, "%s - Not my urb!\n", __func__);
1113 		return;
1114 	}
1115 	list_move(&wrap->list, &info->tx_urbs_free);
1116 	spin_unlock(&info->lock);
1117 
1118 	if (status) {
1119 		dbg("%s - nonzero write bulk status received: %d",
1120 		    __func__, status);
1121 		return;
1122 	}
1123 
1124 	usb_serial_port_softint(port);
1125 }
1126 
1127 
1128 /*****************************************************************************
1129  * Connect Tech's White Heat firmware interface
1130  *****************************************************************************/
1131 static int firm_send_command(struct usb_serial_port *port, __u8 command,
1132 						__u8 *data, __u8 datasize)
1133 {
1134 	struct usb_serial_port *command_port;
1135 	struct whiteheat_command_private *command_info;
1136 	struct whiteheat_private *info;
1137 	__u8 *transfer_buffer;
1138 	int retval = 0;
1139 	int t;
1140 
1141 	dbg("%s - command %d", __func__, command);
1142 
1143 	command_port = port->serial->port[COMMAND_PORT];
1144 	command_info = usb_get_serial_port_data(command_port);
1145 	mutex_lock(&command_info->mutex);
1146 	command_info->command_finished = false;
1147 
1148 	transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
1149 	transfer_buffer[0] = command;
1150 	memcpy(&transfer_buffer[1], data, datasize);
1151 	command_port->write_urb->transfer_buffer_length = datasize + 1;
1152 	command_port->write_urb->dev = port->serial->dev;
1153 	retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
1154 	if (retval) {
1155 		dbg("%s - submit urb failed", __func__);
1156 		goto exit;
1157 	}
1158 
1159 	/* wait for the command to complete */
1160 	t = wait_event_timeout(command_info->wait_command,
1161 		(bool)command_info->command_finished, COMMAND_TIMEOUT);
1162 	if (!t)
1163 		usb_kill_urb(command_port->write_urb);
1164 
1165 	if (command_info->command_finished == false) {
1166 		dbg("%s - command timed out.", __func__);
1167 		retval = -ETIMEDOUT;
1168 		goto exit;
1169 	}
1170 
1171 	if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
1172 		dbg("%s - command failed.", __func__);
1173 		retval = -EIO;
1174 		goto exit;
1175 	}
1176 
1177 	if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
1178 		dbg("%s - command completed.", __func__);
1179 		switch (command) {
1180 		case WHITEHEAT_GET_DTR_RTS:
1181 			info = usb_get_serial_port_data(port);
1182 			memcpy(&info->mcr, command_info->result_buffer,
1183 					sizeof(struct whiteheat_dr_info));
1184 				break;
1185 		}
1186 	}
1187 exit:
1188 	mutex_unlock(&command_info->mutex);
1189 	return retval;
1190 }
1191 
1192 
1193 static int firm_open(struct usb_serial_port *port)
1194 {
1195 	struct whiteheat_simple open_command;
1196 
1197 	open_command.port = port->number - port->serial->minor + 1;
1198 	return firm_send_command(port, WHITEHEAT_OPEN,
1199 		(__u8 *)&open_command, sizeof(open_command));
1200 }
1201 
1202 
1203 static int firm_close(struct usb_serial_port *port)
1204 {
1205 	struct whiteheat_simple close_command;
1206 
1207 	close_command.port = port->number - port->serial->minor + 1;
1208 	return firm_send_command(port, WHITEHEAT_CLOSE,
1209 			(__u8 *)&close_command, sizeof(close_command));
1210 }
1211 
1212 
1213 static void firm_setup_port(struct tty_struct *tty)
1214 {
1215 	struct usb_serial_port *port = tty->driver_data;
1216 	struct whiteheat_port_settings port_settings;
1217 	unsigned int cflag = tty->termios->c_cflag;
1218 
1219 	port_settings.port = port->number + 1;
1220 
1221 	/* get the byte size */
1222 	switch (cflag & CSIZE) {
1223 	case CS5:	port_settings.bits = 5;   break;
1224 	case CS6:	port_settings.bits = 6;   break;
1225 	case CS7:	port_settings.bits = 7;   break;
1226 	default:
1227 	case CS8:	port_settings.bits = 8;   break;
1228 	}
1229 	dbg("%s - data bits = %d", __func__, port_settings.bits);
1230 
1231 	/* determine the parity */
1232 	if (cflag & PARENB)
1233 		if (cflag & CMSPAR)
1234 			if (cflag & PARODD)
1235 				port_settings.parity = WHITEHEAT_PAR_MARK;
1236 			else
1237 				port_settings.parity = WHITEHEAT_PAR_SPACE;
1238 		else
1239 			if (cflag & PARODD)
1240 				port_settings.parity = WHITEHEAT_PAR_ODD;
1241 			else
1242 				port_settings.parity = WHITEHEAT_PAR_EVEN;
1243 	else
1244 		port_settings.parity = WHITEHEAT_PAR_NONE;
1245 	dbg("%s - parity = %c", __func__, port_settings.parity);
1246 
1247 	/* figure out the stop bits requested */
1248 	if (cflag & CSTOPB)
1249 		port_settings.stop = 2;
1250 	else
1251 		port_settings.stop = 1;
1252 	dbg("%s - stop bits = %d", __func__, port_settings.stop);
1253 
1254 	/* figure out the flow control settings */
1255 	if (cflag & CRTSCTS)
1256 		port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
1257 						WHITEHEAT_HFLOW_RTS);
1258 	else
1259 		port_settings.hflow = WHITEHEAT_HFLOW_NONE;
1260 	dbg("%s - hardware flow control = %s %s %s %s", __func__,
1261 	    (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
1262 	    (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
1263 	    (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
1264 	    (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
1265 
1266 	/* determine software flow control */
1267 	if (I_IXOFF(tty))
1268 		port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
1269 	else
1270 		port_settings.sflow = WHITEHEAT_SFLOW_NONE;
1271 	dbg("%s - software flow control = %c", __func__, port_settings.sflow);
1272 
1273 	port_settings.xon = START_CHAR(tty);
1274 	port_settings.xoff = STOP_CHAR(tty);
1275 	dbg("%s - XON = %2x, XOFF = %2x",
1276 			__func__, port_settings.xon, port_settings.xoff);
1277 
1278 	/* get the baud rate wanted */
1279 	port_settings.baud = tty_get_baud_rate(tty);
1280 	dbg("%s - baud rate = %d", __func__, port_settings.baud);
1281 
1282 	/* fixme: should set validated settings */
1283 	tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
1284 	/* handle any settings that aren't specified in the tty structure */
1285 	port_settings.lloop = 0;
1286 
1287 	/* now send the message to the device */
1288 	firm_send_command(port, WHITEHEAT_SETUP_PORT,
1289 			(__u8 *)&port_settings, sizeof(port_settings));
1290 }
1291 
1292 
1293 static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
1294 {
1295 	struct whiteheat_set_rdb rts_command;
1296 
1297 	rts_command.port = port->number - port->serial->minor + 1;
1298 	rts_command.state = onoff;
1299 	return firm_send_command(port, WHITEHEAT_SET_RTS,
1300 			(__u8 *)&rts_command, sizeof(rts_command));
1301 }
1302 
1303 
1304 static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
1305 {
1306 	struct whiteheat_set_rdb dtr_command;
1307 
1308 	dtr_command.port = port->number - port->serial->minor + 1;
1309 	dtr_command.state = onoff;
1310 	return firm_send_command(port, WHITEHEAT_SET_DTR,
1311 			(__u8 *)&dtr_command, sizeof(dtr_command));
1312 }
1313 
1314 
1315 static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
1316 {
1317 	struct whiteheat_set_rdb break_command;
1318 
1319 	break_command.port = port->number - port->serial->minor + 1;
1320 	break_command.state = onoff;
1321 	return firm_send_command(port, WHITEHEAT_SET_BREAK,
1322 			(__u8 *)&break_command, sizeof(break_command));
1323 }
1324 
1325 
1326 static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
1327 {
1328 	struct whiteheat_purge purge_command;
1329 
1330 	purge_command.port = port->number - port->serial->minor + 1;
1331 	purge_command.what = rxtx;
1332 	return firm_send_command(port, WHITEHEAT_PURGE,
1333 			(__u8 *)&purge_command, sizeof(purge_command));
1334 }
1335 
1336 
1337 static int firm_get_dtr_rts(struct usb_serial_port *port)
1338 {
1339 	struct whiteheat_simple get_dr_command;
1340 
1341 	get_dr_command.port = port->number - port->serial->minor + 1;
1342 	return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
1343 			(__u8 *)&get_dr_command, sizeof(get_dr_command));
1344 }
1345 
1346 
1347 static int firm_report_tx_done(struct usb_serial_port *port)
1348 {
1349 	struct whiteheat_simple close_command;
1350 
1351 	close_command.port = port->number - port->serial->minor + 1;
1352 	return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
1353 			(__u8 *)&close_command, sizeof(close_command));
1354 }
1355 
1356 
1357 /*****************************************************************************
1358  * Connect Tech's White Heat utility functions
1359  *****************************************************************************/
1360 static int start_command_port(struct usb_serial *serial)
1361 {
1362 	struct usb_serial_port *command_port;
1363 	struct whiteheat_command_private *command_info;
1364 	int retval = 0;
1365 
1366 	command_port = serial->port[COMMAND_PORT];
1367 	command_info = usb_get_serial_port_data(command_port);
1368 	mutex_lock(&command_info->mutex);
1369 	if (!command_info->port_running) {
1370 		/* Work around HCD bugs */
1371 		usb_clear_halt(serial->dev, command_port->read_urb->pipe);
1372 
1373 		command_port->read_urb->dev = serial->dev;
1374 		retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
1375 		if (retval) {
1376 			dev_err(&serial->dev->dev,
1377 				"%s - failed submitting read urb, error %d\n",
1378 				__func__, retval);
1379 			goto exit;
1380 		}
1381 	}
1382 	command_info->port_running++;
1383 
1384 exit:
1385 	mutex_unlock(&command_info->mutex);
1386 	return retval;
1387 }
1388 
1389 
1390 static void stop_command_port(struct usb_serial *serial)
1391 {
1392 	struct usb_serial_port *command_port;
1393 	struct whiteheat_command_private *command_info;
1394 
1395 	command_port = serial->port[COMMAND_PORT];
1396 	command_info = usb_get_serial_port_data(command_port);
1397 	mutex_lock(&command_info->mutex);
1398 	command_info->port_running--;
1399 	if (!command_info->port_running)
1400 		usb_kill_urb(command_port->read_urb);
1401 	mutex_unlock(&command_info->mutex);
1402 }
1403 
1404 
1405 static int start_port_read(struct usb_serial_port *port)
1406 {
1407 	struct whiteheat_private *info = usb_get_serial_port_data(port);
1408 	struct whiteheat_urb_wrap *wrap;
1409 	struct urb *urb;
1410 	int retval = 0;
1411 	unsigned long flags;
1412 	struct list_head *tmp;
1413 	struct list_head *tmp2;
1414 
1415 	spin_lock_irqsave(&info->lock, flags);
1416 
1417 	list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
1418 		list_del(tmp);
1419 		wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1420 		urb = wrap->urb;
1421 		urb->dev = port->serial->dev;
1422 		spin_unlock_irqrestore(&info->lock, flags);
1423 		retval = usb_submit_urb(urb, GFP_KERNEL);
1424 		if (retval) {
1425 			spin_lock_irqsave(&info->lock, flags);
1426 			list_add(tmp, &info->rx_urbs_free);
1427 			list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
1428 				wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1429 				urb = wrap->urb;
1430 				list_del(tmp);
1431 				spin_unlock_irqrestore(&info->lock, flags);
1432 				usb_kill_urb(urb);
1433 				spin_lock_irqsave(&info->lock, flags);
1434 				list_add(tmp, &info->rx_urbs_free);
1435 			}
1436 			break;
1437 		}
1438 		spin_lock_irqsave(&info->lock, flags);
1439 		list_add(tmp, &info->rx_urbs_submitted);
1440 	}
1441 
1442 	spin_unlock_irqrestore(&info->lock, flags);
1443 
1444 	return retval;
1445 }
1446 
1447 
1448 static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
1449 						struct list_head *head)
1450 {
1451 	struct whiteheat_urb_wrap *wrap;
1452 	struct list_head *tmp;
1453 
1454 	list_for_each(tmp, head) {
1455 		wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1456 		if (wrap->urb == urb)
1457 			return wrap;
1458 	}
1459 
1460 	return NULL;
1461 }
1462 
1463 
1464 static struct list_head *list_first(struct list_head *head)
1465 {
1466 	return head->next;
1467 }
1468 
1469 
1470 static void rx_data_softint(struct work_struct *work)
1471 {
1472 	struct whiteheat_private *info =
1473 		container_of(work, struct whiteheat_private, rx_work);
1474 	struct usb_serial_port *port = info->port;
1475 	struct tty_struct *tty = tty_port_tty_get(&port->port);
1476 	struct whiteheat_urb_wrap *wrap;
1477 	struct urb *urb;
1478 	unsigned long flags;
1479 	struct list_head *tmp;
1480 	struct list_head *tmp2;
1481 	int result;
1482 	int sent = 0;
1483 
1484 	spin_lock_irqsave(&info->lock, flags);
1485 	if (info->flags & THROTTLED) {
1486 		spin_unlock_irqrestore(&info->lock, flags);
1487 		goto out;
1488 	}
1489 
1490 	list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
1491 		list_del(tmp);
1492 		spin_unlock_irqrestore(&info->lock, flags);
1493 
1494 		wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
1495 		urb = wrap->urb;
1496 
1497 		if (tty && urb->actual_length) {
1498 			int len = tty_buffer_request_room(tty,
1499 							urb->actual_length);
1500 			/* This stuff can go away now I suspect */
1501 			if (unlikely(len < urb->actual_length)) {
1502 				spin_lock_irqsave(&info->lock, flags);
1503 				list_add(tmp, &info->rx_urb_q);
1504 				spin_unlock_irqrestore(&info->lock, flags);
1505 				tty_flip_buffer_push(tty);
1506 				schedule_work(&info->rx_work);
1507 				goto out;
1508 			}
1509 			tty_insert_flip_string(tty, urb->transfer_buffer, len);
1510 			sent += len;
1511 		}
1512 
1513 		urb->dev = port->serial->dev;
1514 		result = usb_submit_urb(urb, GFP_ATOMIC);
1515 		if (result) {
1516 			dev_err(&port->dev,
1517 				"%s - failed resubmitting read urb, error %d\n",
1518 				__func__, result);
1519 			spin_lock_irqsave(&info->lock, flags);
1520 			list_add(tmp, &info->rx_urbs_free);
1521 			continue;
1522 		}
1523 
1524 		spin_lock_irqsave(&info->lock, flags);
1525 		list_add(tmp, &info->rx_urbs_submitted);
1526 	}
1527 	spin_unlock_irqrestore(&info->lock, flags);
1528 
1529 	if (sent)
1530 		tty_flip_buffer_push(tty);
1531 out:
1532 	tty_kref_put(tty);
1533 }
1534 
1535 
1536 /*****************************************************************************
1537  * Connect Tech's White Heat module functions
1538  *****************************************************************************/
1539 static int __init whiteheat_init(void)
1540 {
1541 	int retval;
1542 	retval = usb_serial_register(&whiteheat_fake_device);
1543 	if (retval)
1544 		goto failed_fake_register;
1545 	retval = usb_serial_register(&whiteheat_device);
1546 	if (retval)
1547 		goto failed_device_register;
1548 	retval = usb_register(&whiteheat_driver);
1549 	if (retval)
1550 		goto failed_usb_register;
1551 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1552 	       DRIVER_DESC "\n");
1553 	return 0;
1554 failed_usb_register:
1555 	usb_serial_deregister(&whiteheat_device);
1556 failed_device_register:
1557 	usb_serial_deregister(&whiteheat_fake_device);
1558 failed_fake_register:
1559 	return retval;
1560 }
1561 
1562 
1563 static void __exit whiteheat_exit(void)
1564 {
1565 	usb_deregister(&whiteheat_driver);
1566 	usb_serial_deregister(&whiteheat_fake_device);
1567 	usb_serial_deregister(&whiteheat_device);
1568 }
1569 
1570 
1571 module_init(whiteheat_init);
1572 module_exit(whiteheat_exit);
1573 
1574 MODULE_AUTHOR(DRIVER_AUTHOR);
1575 MODULE_DESCRIPTION(DRIVER_DESC);
1576 MODULE_LICENSE("GPL");
1577 
1578 MODULE_FIRMWARE("whiteheat.fw");
1579 MODULE_FIRMWARE("whiteheat_loader.fw");
1580 
1581 module_param(urb_pool_size, int, 0);
1582 MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
1583 
1584 module_param(debug, bool, S_IRUGO | S_IWUSR);
1585 MODULE_PARM_DESC(debug, "Debug enabled or not");
1586