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