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