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