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