whiteheat.c revision 07764958947c381ef095fc69503c1ef1775f316e
1/*
2 * USB ConnectTech WhiteHEAT driver
3 *
4 *	Copyright (C) 2002
5 *	    Connect Tech Inc.
6 *
7 *	Copyright (C) 1999 - 2001
8 *	    Greg Kroah-Hartman (greg@kroah.com)
9 *
10 *	This program is free software; you can redistribute it and/or modify
11 *	it under the terms of the GNU General Public License as published by
12 *	the Free Software Foundation; either version 2 of the License, or
13 *	(at your option) any later version.
14 *
15 * See Documentation/usb/usb-serial.txt for more information on using this
16 * driver
17 */
18
19#include <linux/kernel.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/tty.h>
24#include <linux/tty_driver.h>
25#include <linux/tty_flip.h>
26#include <linux/module.h>
27#include <linux/spinlock.h>
28#include <linux/mutex.h>
29#include <linux/uaccess.h>
30#include <asm/termbits.h>
31#include <linux/usb.h>
32#include <linux/serial_reg.h>
33#include <linux/serial.h>
34#include <linux/usb/serial.h>
35#include <linux/firmware.h>
36#include <linux/ihex.h>
37#include "whiteheat.h"			/* WhiteHEAT specific commands */
38
39#ifndef CMSPAR
40#define CMSPAR 0
41#endif
42
43/*
44 * Version Information
45 */
46#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
47#define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
48
49#define CONNECT_TECH_VENDOR_ID		0x0710
50#define CONNECT_TECH_FAKE_WHITE_HEAT_ID	0x0001
51#define CONNECT_TECH_WHITE_HEAT_ID	0x8001
52
53/*
54   ID tables for whiteheat are unusual, because we want to different
55   things for different versions of the device.  Eventually, this
56   will be doable from a single table.  But, for now, we define two
57   separate ID tables, and then a third table that combines them
58   just for the purpose of exporting the autoloading information.
59*/
60static const struct usb_device_id id_table_std[] = {
61	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
62	{ }						/* Terminating entry */
63};
64
65static const struct usb_device_id id_table_prerenumeration[] = {
66	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
67	{ }						/* Terminating entry */
68};
69
70static const struct usb_device_id id_table_combined[] = {
71	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
72	{ USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
73	{ }						/* Terminating entry */
74};
75
76MODULE_DEVICE_TABLE(usb, id_table_combined);
77
78
79/* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
80static int  whiteheat_firmware_download(struct usb_serial *serial,
81					const struct usb_device_id *id);
82static int  whiteheat_firmware_attach(struct usb_serial *serial);
83
84/* function prototypes for the Connect Tech WhiteHEAT serial converter */
85static int  whiteheat_attach(struct usb_serial *serial);
86static void whiteheat_release(struct usb_serial *serial);
87static int  whiteheat_open(struct tty_struct *tty,
88			struct usb_serial_port *port);
89static void whiteheat_close(struct usb_serial_port *port);
90static int  whiteheat_ioctl(struct tty_struct *tty,
91			unsigned int cmd, unsigned long arg);
92static void whiteheat_set_termios(struct tty_struct *tty,
93			struct usb_serial_port *port, struct ktermios *old);
94static int  whiteheat_tiocmget(struct tty_struct *tty);
95static int  whiteheat_tiocmset(struct tty_struct *tty,
96			unsigned int set, unsigned int clear);
97static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
98
99static struct usb_serial_driver whiteheat_fake_device = {
100	.driver = {
101		.owner =	THIS_MODULE,
102		.name =		"whiteheatnofirm",
103	},
104	.description =		"Connect Tech - WhiteHEAT - (prerenumeration)",
105	.id_table =		id_table_prerenumeration,
106	.num_ports =		1,
107	.probe =		whiteheat_firmware_download,
108	.attach =		whiteheat_firmware_attach,
109};
110
111static struct usb_serial_driver whiteheat_device = {
112	.driver = {
113		.owner =	THIS_MODULE,
114		.name =		"whiteheat",
115	},
116	.description =		"Connect Tech - WhiteHEAT",
117	.id_table =		id_table_std,
118	.num_ports =		4,
119	.attach =		whiteheat_attach,
120	.release =		whiteheat_release,
121	.open =			whiteheat_open,
122	.close =		whiteheat_close,
123	.ioctl =		whiteheat_ioctl,
124	.set_termios =		whiteheat_set_termios,
125	.break_ctl =		whiteheat_break_ctl,
126	.tiocmget =		whiteheat_tiocmget,
127	.tiocmset =		whiteheat_tiocmset,
128	.throttle =		usb_serial_generic_throttle,
129	.unthrottle =		usb_serial_generic_unthrottle,
130};
131
132static struct usb_serial_driver * const serial_drivers[] = {
133	&whiteheat_fake_device, &whiteheat_device, NULL
134};
135
136struct whiteheat_command_private {
137	struct mutex		mutex;
138	__u8			port_running;
139	__u8			command_finished;
140	wait_queue_head_t	wait_command; /* for handling sleeping whilst
141						 waiting for a command to
142						 finish */
143	__u8			result_buffer[64];
144};
145
146struct whiteheat_private {
147	__u8			mcr;		/* FIXME: no locking on mcr */
148};
149
150
151/* local function prototypes */
152static int start_command_port(struct usb_serial *serial);
153static void stop_command_port(struct usb_serial *serial);
154static void command_port_write_callback(struct urb *urb);
155static void command_port_read_callback(struct urb *urb);
156
157static int firm_send_command(struct usb_serial_port *port, __u8 command,
158						__u8 *data, __u8 datasize);
159static int firm_open(struct usb_serial_port *port);
160static int firm_close(struct usb_serial_port *port);
161static void firm_setup_port(struct tty_struct *tty);
162static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
163static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
164static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
165static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
166static int firm_get_dtr_rts(struct usb_serial_port *port);
167static int firm_report_tx_done(struct usb_serial_port *port);
168
169
170#define COMMAND_PORT		4
171#define COMMAND_TIMEOUT		(2*HZ)	/* 2 second timeout for a command */
172#define	COMMAND_TIMEOUT_MS	2000
173#define CLOSING_DELAY		(30 * HZ)
174
175
176/*****************************************************************************
177 * Connect Tech's White Heat prerenumeration driver functions
178 *****************************************************************************/
179
180/* steps to download the firmware to the WhiteHEAT device:
181 - hold the reset (by writing to the reset bit of the CPUCS register)
182 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
183 - release the reset (by writing to the CPUCS register)
184 - download the WH.HEX file for all addresses greater than 0x1b3f using
185   VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
186 - hold the reset
187 - download the WH.HEX file for all addresses less than 0x1b40 using
188   VENDOR_REQUEST_ANCHOR_LOAD
189 - release the reset
190 - device renumerated itself and comes up as new device id with all
191   firmware download completed.
192*/
193static int whiteheat_firmware_download(struct usb_serial *serial,
194					const struct usb_device_id *id)
195{
196	int response, ret = -ENOENT;
197	const struct firmware *loader_fw = NULL, *firmware_fw = NULL;
198	const struct ihex_binrec *record;
199
200	if (request_ihex_firmware(&firmware_fw, "whiteheat.fw",
201				  &serial->dev->dev)) {
202		dev_err(&serial->dev->dev,
203			"%s - request \"whiteheat.fw\" failed\n", __func__);
204		goto out;
205	}
206	if (request_ihex_firmware(&loader_fw, "whiteheat_loader.fw",
207			     &serial->dev->dev)) {
208		dev_err(&serial->dev->dev,
209			"%s - request \"whiteheat_loader.fw\" failed\n",
210			__func__);
211		goto out;
212	}
213	ret = 0;
214	response = ezusb_set_reset(serial->dev, 1);
215
216	record = (const struct ihex_binrec *)loader_fw->data;
217	while (record) {
218		response = ezusb_writememory(serial->dev, be32_to_cpu(record->addr),
219					     (unsigned char *)record->data,
220					     be16_to_cpu(record->len), 0xa0);
221		if (response < 0) {
222			dev_err(&serial->dev->dev, "%s - ezusb_writememory "
223				"failed for loader (%d %04X %p %d)\n",
224				__func__, response, be32_to_cpu(record->addr),
225				record->data, be16_to_cpu(record->len));
226			break;
227		}
228		record = ihex_next_binrec(record);
229	}
230
231	response = ezusb_set_reset(serial->dev, 0);
232
233	record = (const struct ihex_binrec *)firmware_fw->data;
234	while (record && be32_to_cpu(record->addr) < 0x1b40)
235		record = ihex_next_binrec(record);
236	while (record) {
237		response = ezusb_writememory(serial->dev, be32_to_cpu(record->addr),
238					     (unsigned char *)record->data,
239					     be16_to_cpu(record->len), 0xa3);
240		if (response < 0) {
241			dev_err(&serial->dev->dev, "%s - ezusb_writememory "
242				"failed for first firmware step "
243				"(%d %04X %p %d)\n", __func__, response,
244				be32_to_cpu(record->addr), record->data,
245				be16_to_cpu(record->len));
246			break;
247		}
248		++record;
249	}
250
251	response = ezusb_set_reset(serial->dev, 1);
252
253	record = (const struct ihex_binrec *)firmware_fw->data;
254	while (record && be32_to_cpu(record->addr) < 0x1b40) {
255		response = ezusb_writememory(serial->dev, be32_to_cpu(record->addr),
256					     (unsigned char *)record->data,
257					     be16_to_cpu(record->len), 0xa0);
258		if (response < 0) {
259			dev_err(&serial->dev->dev, "%s - ezusb_writememory "
260				"failed for second firmware step "
261				"(%d %04X %p %d)\n", __func__, response,
262				be32_to_cpu(record->addr), record->data,
263				be16_to_cpu(record->len));
264			break;
265		}
266		++record;
267	}
268	ret = 0;
269	response = ezusb_set_reset(serial->dev, 0);
270 out:
271	release_firmware(loader_fw);
272	release_firmware(firmware_fw);
273	return ret;
274}
275
276
277static int whiteheat_firmware_attach(struct usb_serial *serial)
278{
279	/* We want this device to fail to have a driver assigned to it */
280	return 1;
281}
282
283
284/*****************************************************************************
285 * Connect Tech's White Heat serial driver functions
286 *****************************************************************************/
287static int whiteheat_attach(struct usb_serial *serial)
288{
289	struct usb_serial_port *command_port;
290	struct whiteheat_command_private *command_info;
291	struct usb_serial_port *port;
292	struct whiteheat_private *info;
293	struct whiteheat_hw_info *hw_info;
294	int pipe;
295	int ret;
296	int alen;
297	__u8 *command;
298	__u8 *result;
299	int i;
300
301	command_port = serial->port[COMMAND_PORT];
302
303	pipe = usb_sndbulkpipe(serial->dev,
304			command_port->bulk_out_endpointAddress);
305	command = kmalloc(2, GFP_KERNEL);
306	if (!command)
307		goto no_command_buffer;
308	command[0] = WHITEHEAT_GET_HW_INFO;
309	command[1] = 0;
310
311	result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
312	if (!result)
313		goto no_result_buffer;
314	/*
315	 * When the module is reloaded the firmware is still there and
316	 * the endpoints are still in the usb core unchanged. This is the
317	 * unlinking bug in disguise. Same for the call below.
318	 */
319	usb_clear_halt(serial->dev, pipe);
320	ret = usb_bulk_msg(serial->dev, pipe, command, 2,
321						&alen, COMMAND_TIMEOUT_MS);
322	if (ret) {
323		dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
324			serial->type->description, ret);
325		goto no_firmware;
326	} else if (alen != 2) {
327		dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
328			serial->type->description, alen);
329		goto no_firmware;
330	}
331
332	pipe = usb_rcvbulkpipe(serial->dev,
333				command_port->bulk_in_endpointAddress);
334	/* See the comment on the usb_clear_halt() above */
335	usb_clear_halt(serial->dev, pipe);
336	ret = usb_bulk_msg(serial->dev, pipe, result,
337			sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
338	if (ret) {
339		dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
340			serial->type->description, ret);
341		goto no_firmware;
342	} else if (alen != sizeof(*hw_info) + 1) {
343		dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
344			serial->type->description, alen);
345		goto no_firmware;
346	} else if (result[0] != command[0]) {
347		dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
348			serial->type->description, result[0]);
349		goto no_firmware;
350	}
351
352	hw_info = (struct whiteheat_hw_info *)&result[1];
353
354	dev_info(&serial->dev->dev, "%s: Firmware v%d.%02d\n",
355		 serial->type->description,
356		 hw_info->sw_major_rev, hw_info->sw_minor_rev);
357
358	for (i = 0; i < serial->num_ports; i++) {
359		port = serial->port[i];
360
361		info = kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
362		if (info == NULL) {
363			dev_err(&port->dev,
364				"%s: Out of memory for port structures\n",
365				serial->type->description);
366			goto no_private;
367		}
368
369		info->mcr = 0;
370
371		usb_set_serial_port_data(port, info);
372	}
373
374	command_info = kmalloc(sizeof(struct whiteheat_command_private),
375								GFP_KERNEL);
376	if (command_info == NULL) {
377		dev_err(&serial->dev->dev,
378			"%s: Out of memory for port structures\n",
379			serial->type->description);
380		goto no_command_private;
381	}
382
383	mutex_init(&command_info->mutex);
384	command_info->port_running = 0;
385	init_waitqueue_head(&command_info->wait_command);
386	usb_set_serial_port_data(command_port, command_info);
387	command_port->write_urb->complete = command_port_write_callback;
388	command_port->read_urb->complete = command_port_read_callback;
389	kfree(result);
390	kfree(command);
391
392	return 0;
393
394no_firmware:
395	/* Firmware likely not running */
396	dev_err(&serial->dev->dev,
397		"%s: Unable to retrieve firmware version, try replugging\n",
398		serial->type->description);
399	dev_err(&serial->dev->dev,
400		"%s: If the firmware is not running (status led not blinking)\n",
401		serial->type->description);
402	dev_err(&serial->dev->dev,
403		"%s: please contact support@connecttech.com\n",
404		serial->type->description);
405	kfree(result);
406	return -ENODEV;
407
408no_command_private:
409	for (i = serial->num_ports - 1; i >= 0; i--) {
410		port = serial->port[i];
411		info = usb_get_serial_port_data(port);
412		kfree(info);
413no_private:
414		;
415	}
416	kfree(result);
417no_result_buffer:
418	kfree(command);
419no_command_buffer:
420	return -ENOMEM;
421}
422
423
424static void whiteheat_release(struct usb_serial *serial)
425{
426	struct usb_serial_port *command_port;
427	struct whiteheat_private *info;
428	int i;
429
430	/* free up our private data for our command port */
431	command_port = serial->port[COMMAND_PORT];
432	kfree(usb_get_serial_port_data(command_port));
433
434	for (i = 0; i < serial->num_ports; i++) {
435		info = usb_get_serial_port_data(serial->port[i]);
436		kfree(info);
437	}
438}
439
440static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
441{
442	int retval;
443
444	retval = start_command_port(port->serial);
445	if (retval)
446		goto exit;
447
448	/* send an open port command */
449	retval = firm_open(port);
450	if (retval) {
451		stop_command_port(port->serial);
452		goto exit;
453	}
454
455	retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
456	if (retval) {
457		firm_close(port);
458		stop_command_port(port->serial);
459		goto exit;
460	}
461
462	if (tty)
463		firm_setup_port(tty);
464
465	/* Work around HCD bugs */
466	usb_clear_halt(port->serial->dev, port->read_urb->pipe);
467	usb_clear_halt(port->serial->dev, port->write_urb->pipe);
468
469	retval = usb_serial_generic_open(tty, port);
470	if (retval) {
471		firm_close(port);
472		stop_command_port(port->serial);
473		goto exit;
474	}
475exit:
476	return retval;
477}
478
479
480static void whiteheat_close(struct usb_serial_port *port)
481{
482	firm_report_tx_done(port);
483	firm_close(port);
484
485	usb_serial_generic_close(port);
486
487	stop_command_port(port->serial);
488}
489
490static int whiteheat_tiocmget(struct tty_struct *tty)
491{
492	struct usb_serial_port *port = tty->driver_data;
493	struct whiteheat_private *info = usb_get_serial_port_data(port);
494	unsigned int modem_signals = 0;
495
496	firm_get_dtr_rts(port);
497	if (info->mcr & UART_MCR_DTR)
498		modem_signals |= TIOCM_DTR;
499	if (info->mcr & UART_MCR_RTS)
500		modem_signals |= TIOCM_RTS;
501
502	return modem_signals;
503}
504
505static int whiteheat_tiocmset(struct tty_struct *tty,
506			       unsigned int set, unsigned int clear)
507{
508	struct usb_serial_port *port = tty->driver_data;
509	struct whiteheat_private *info = usb_get_serial_port_data(port);
510
511	if (set & TIOCM_RTS)
512		info->mcr |= UART_MCR_RTS;
513	if (set & TIOCM_DTR)
514		info->mcr |= UART_MCR_DTR;
515
516	if (clear & TIOCM_RTS)
517		info->mcr &= ~UART_MCR_RTS;
518	if (clear & TIOCM_DTR)
519		info->mcr &= ~UART_MCR_DTR;
520
521	firm_set_dtr(port, info->mcr & UART_MCR_DTR);
522	firm_set_rts(port, info->mcr & UART_MCR_RTS);
523	return 0;
524}
525
526
527static int whiteheat_ioctl(struct tty_struct *tty,
528					unsigned int cmd, unsigned long arg)
529{
530	struct usb_serial_port *port = tty->driver_data;
531	struct serial_struct serstruct;
532	void __user *user_arg = (void __user *)arg;
533
534	dev_dbg(&port->dev, "%s - cmd 0x%.4x\n", __func__, cmd);
535
536	switch (cmd) {
537	case TIOCGSERIAL:
538		memset(&serstruct, 0, sizeof(serstruct));
539		serstruct.type = PORT_16654;
540		serstruct.line = port->serial->minor;
541		serstruct.port = port->number;
542		serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
543		serstruct.xmit_fifo_size = kfifo_size(&port->write_fifo);
544		serstruct.custom_divisor = 0;
545		serstruct.baud_base = 460800;
546		serstruct.close_delay = CLOSING_DELAY;
547		serstruct.closing_wait = CLOSING_DELAY;
548
549		if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
550			return -EFAULT;
551		break;
552	default:
553		break;
554	}
555
556	return -ENOIOCTLCMD;
557}
558
559
560static void whiteheat_set_termios(struct tty_struct *tty,
561	struct usb_serial_port *port, struct ktermios *old_termios)
562{
563	firm_setup_port(tty);
564}
565
566static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
567{
568	struct usb_serial_port *port = tty->driver_data;
569	firm_set_break(port, break_state);
570}
571
572
573/*****************************************************************************
574 * Connect Tech's White Heat callback routines
575 *****************************************************************************/
576static void command_port_write_callback(struct urb *urb)
577{
578	int status = urb->status;
579
580	if (status) {
581		dev_dbg(&urb->dev->dev, "nonzero urb status: %d\n", status);
582		return;
583	}
584}
585
586
587static void command_port_read_callback(struct urb *urb)
588{
589	struct usb_serial_port *command_port = urb->context;
590	struct whiteheat_command_private *command_info;
591	int status = urb->status;
592	unsigned char *data = urb->transfer_buffer;
593	int result;
594
595	command_info = usb_get_serial_port_data(command_port);
596	if (!command_info) {
597		dev_dbg(&urb->dev->dev, "%s - command_info is NULL, exiting.\n", __func__);
598		return;
599	}
600	if (status) {
601		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", __func__, status);
602		if (status != -ENOENT)
603			command_info->command_finished = WHITEHEAT_CMD_FAILURE;
604		wake_up(&command_info->wait_command);
605		return;
606	}
607
608	usb_serial_debug_data(&command_port->dev, __func__, urb->actual_length, data);
609
610	if (data[0] == WHITEHEAT_CMD_COMPLETE) {
611		command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
612		wake_up(&command_info->wait_command);
613	} else if (data[0] == WHITEHEAT_CMD_FAILURE) {
614		command_info->command_finished = WHITEHEAT_CMD_FAILURE;
615		wake_up(&command_info->wait_command);
616	} else if (data[0] == WHITEHEAT_EVENT) {
617		/* These are unsolicited reports from the firmware, hence no
618		   waiting command to wakeup */
619		dev_dbg(&urb->dev->dev, "%s - event received\n", __func__);
620	} else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
621		memcpy(command_info->result_buffer, &data[1],
622						urb->actual_length - 1);
623		command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
624		wake_up(&command_info->wait_command);
625	} else
626		dev_dbg(&urb->dev->dev, "%s - bad reply from firmware\n", __func__);
627
628	/* Continue trying to always read */
629	result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
630	if (result)
631		dev_dbg(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n",
632			__func__, result);
633}
634
635
636/*****************************************************************************
637 * Connect Tech's White Heat firmware interface
638 *****************************************************************************/
639static int firm_send_command(struct usb_serial_port *port, __u8 command,
640						__u8 *data, __u8 datasize)
641{
642	struct usb_serial_port *command_port;
643	struct whiteheat_command_private *command_info;
644	struct whiteheat_private *info;
645	struct device *dev = &port->dev;
646	__u8 *transfer_buffer;
647	int retval = 0;
648	int t;
649
650	dev_dbg(dev, "%s - command %d\n", __func__, command);
651
652	command_port = port->serial->port[COMMAND_PORT];
653	command_info = usb_get_serial_port_data(command_port);
654	mutex_lock(&command_info->mutex);
655	command_info->command_finished = false;
656
657	transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
658	transfer_buffer[0] = command;
659	memcpy(&transfer_buffer[1], data, datasize);
660	command_port->write_urb->transfer_buffer_length = datasize + 1;
661	retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
662	if (retval) {
663		dev_dbg(dev, "%s - submit urb failed\n", __func__);
664		goto exit;
665	}
666
667	/* wait for the command to complete */
668	t = wait_event_timeout(command_info->wait_command,
669		(bool)command_info->command_finished, COMMAND_TIMEOUT);
670	if (!t)
671		usb_kill_urb(command_port->write_urb);
672
673	if (command_info->command_finished == false) {
674		dev_dbg(dev, "%s - command timed out.\n", __func__);
675		retval = -ETIMEDOUT;
676		goto exit;
677	}
678
679	if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
680		dev_dbg(dev, "%s - command failed.\n", __func__);
681		retval = -EIO;
682		goto exit;
683	}
684
685	if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
686		dev_dbg(dev, "%s - command completed.\n", __func__);
687		switch (command) {
688		case WHITEHEAT_GET_DTR_RTS:
689			info = usb_get_serial_port_data(port);
690			memcpy(&info->mcr, command_info->result_buffer,
691					sizeof(struct whiteheat_dr_info));
692				break;
693		}
694	}
695exit:
696	mutex_unlock(&command_info->mutex);
697	return retval;
698}
699
700
701static int firm_open(struct usb_serial_port *port)
702{
703	struct whiteheat_simple open_command;
704
705	open_command.port = port->number - port->serial->minor + 1;
706	return firm_send_command(port, WHITEHEAT_OPEN,
707		(__u8 *)&open_command, sizeof(open_command));
708}
709
710
711static int firm_close(struct usb_serial_port *port)
712{
713	struct whiteheat_simple close_command;
714
715	close_command.port = port->number - port->serial->minor + 1;
716	return firm_send_command(port, WHITEHEAT_CLOSE,
717			(__u8 *)&close_command, sizeof(close_command));
718}
719
720
721static void firm_setup_port(struct tty_struct *tty)
722{
723	struct usb_serial_port *port = tty->driver_data;
724	struct device *dev = &port->dev;
725	struct whiteheat_port_settings port_settings;
726	unsigned int cflag = tty->termios->c_cflag;
727
728	port_settings.port = port->number + 1;
729
730	/* get the byte size */
731	switch (cflag & CSIZE) {
732	case CS5:	port_settings.bits = 5;   break;
733	case CS6:	port_settings.bits = 6;   break;
734	case CS7:	port_settings.bits = 7;   break;
735	default:
736	case CS8:	port_settings.bits = 8;   break;
737	}
738	dev_dbg(dev, "%s - data bits = %d\n", __func__, port_settings.bits);
739
740	/* determine the parity */
741	if (cflag & PARENB)
742		if (cflag & CMSPAR)
743			if (cflag & PARODD)
744				port_settings.parity = WHITEHEAT_PAR_MARK;
745			else
746				port_settings.parity = WHITEHEAT_PAR_SPACE;
747		else
748			if (cflag & PARODD)
749				port_settings.parity = WHITEHEAT_PAR_ODD;
750			else
751				port_settings.parity = WHITEHEAT_PAR_EVEN;
752	else
753		port_settings.parity = WHITEHEAT_PAR_NONE;
754	dev_dbg(dev, "%s - parity = %c\n", __func__, port_settings.parity);
755
756	/* figure out the stop bits requested */
757	if (cflag & CSTOPB)
758		port_settings.stop = 2;
759	else
760		port_settings.stop = 1;
761	dev_dbg(dev, "%s - stop bits = %d\n", __func__, port_settings.stop);
762
763	/* figure out the flow control settings */
764	if (cflag & CRTSCTS)
765		port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
766						WHITEHEAT_HFLOW_RTS);
767	else
768		port_settings.hflow = WHITEHEAT_HFLOW_NONE;
769	dev_dbg(dev, "%s - hardware flow control = %s %s %s %s\n", __func__,
770	    (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
771	    (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
772	    (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
773	    (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
774
775	/* determine software flow control */
776	if (I_IXOFF(tty))
777		port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
778	else
779		port_settings.sflow = WHITEHEAT_SFLOW_NONE;
780	dev_dbg(dev, "%s - software flow control = %c\n", __func__, port_settings.sflow);
781
782	port_settings.xon = START_CHAR(tty);
783	port_settings.xoff = STOP_CHAR(tty);
784	dev_dbg(dev, "%s - XON = %2x, XOFF = %2x\n", __func__, port_settings.xon, port_settings.xoff);
785
786	/* get the baud rate wanted */
787	port_settings.baud = tty_get_baud_rate(tty);
788	dev_dbg(dev, "%s - baud rate = %d\n", __func__, port_settings.baud);
789
790	/* fixme: should set validated settings */
791	tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
792	/* handle any settings that aren't specified in the tty structure */
793	port_settings.lloop = 0;
794
795	/* now send the message to the device */
796	firm_send_command(port, WHITEHEAT_SETUP_PORT,
797			(__u8 *)&port_settings, sizeof(port_settings));
798}
799
800
801static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
802{
803	struct whiteheat_set_rdb rts_command;
804
805	rts_command.port = port->number - port->serial->minor + 1;
806	rts_command.state = onoff;
807	return firm_send_command(port, WHITEHEAT_SET_RTS,
808			(__u8 *)&rts_command, sizeof(rts_command));
809}
810
811
812static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
813{
814	struct whiteheat_set_rdb dtr_command;
815
816	dtr_command.port = port->number - port->serial->minor + 1;
817	dtr_command.state = onoff;
818	return firm_send_command(port, WHITEHEAT_SET_DTR,
819			(__u8 *)&dtr_command, sizeof(dtr_command));
820}
821
822
823static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
824{
825	struct whiteheat_set_rdb break_command;
826
827	break_command.port = port->number - port->serial->minor + 1;
828	break_command.state = onoff;
829	return firm_send_command(port, WHITEHEAT_SET_BREAK,
830			(__u8 *)&break_command, sizeof(break_command));
831}
832
833
834static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
835{
836	struct whiteheat_purge purge_command;
837
838	purge_command.port = port->number - port->serial->minor + 1;
839	purge_command.what = rxtx;
840	return firm_send_command(port, WHITEHEAT_PURGE,
841			(__u8 *)&purge_command, sizeof(purge_command));
842}
843
844
845static int firm_get_dtr_rts(struct usb_serial_port *port)
846{
847	struct whiteheat_simple get_dr_command;
848
849	get_dr_command.port = port->number - port->serial->minor + 1;
850	return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
851			(__u8 *)&get_dr_command, sizeof(get_dr_command));
852}
853
854
855static int firm_report_tx_done(struct usb_serial_port *port)
856{
857	struct whiteheat_simple close_command;
858
859	close_command.port = port->number - port->serial->minor + 1;
860	return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
861			(__u8 *)&close_command, sizeof(close_command));
862}
863
864
865/*****************************************************************************
866 * Connect Tech's White Heat utility functions
867 *****************************************************************************/
868static int start_command_port(struct usb_serial *serial)
869{
870	struct usb_serial_port *command_port;
871	struct whiteheat_command_private *command_info;
872	int retval = 0;
873
874	command_port = serial->port[COMMAND_PORT];
875	command_info = usb_get_serial_port_data(command_port);
876	mutex_lock(&command_info->mutex);
877	if (!command_info->port_running) {
878		/* Work around HCD bugs */
879		usb_clear_halt(serial->dev, command_port->read_urb->pipe);
880
881		retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
882		if (retval) {
883			dev_err(&serial->dev->dev,
884				"%s - failed submitting read urb, error %d\n",
885				__func__, retval);
886			goto exit;
887		}
888	}
889	command_info->port_running++;
890
891exit:
892	mutex_unlock(&command_info->mutex);
893	return retval;
894}
895
896
897static void stop_command_port(struct usb_serial *serial)
898{
899	struct usb_serial_port *command_port;
900	struct whiteheat_command_private *command_info;
901
902	command_port = serial->port[COMMAND_PORT];
903	command_info = usb_get_serial_port_data(command_port);
904	mutex_lock(&command_info->mutex);
905	command_info->port_running--;
906	if (!command_info->port_running)
907		usb_kill_urb(command_port->read_urb);
908	mutex_unlock(&command_info->mutex);
909}
910
911module_usb_serial_driver(serial_drivers, id_table_combined);
912
913MODULE_AUTHOR(DRIVER_AUTHOR);
914MODULE_DESCRIPTION(DRIVER_DESC);
915MODULE_LICENSE("GPL");
916
917MODULE_FIRMWARE("whiteheat.fw");
918MODULE_FIRMWARE("whiteheat_loader.fw");
919