Linux Audio

Check our new training course

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