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