ti_usb_3410_5052.c 47.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/* vi: ts=8 sw=8
 *
 * TI 3410/5052 USB Serial Driver
 *
 * Copyright (C) 2004 Texas Instruments
 *
 * This driver is based on the Linux io_ti driver, which is
 *   Copyright (C) 2000-2002 Inside Out Networks
 *   Copyright (C) 2001-2002 Greg Kroah-Hartman
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * For questions or problems with this driver, contact Texas Instruments
 * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
 * Peter Berger <pberger@brimson.com>.
 * 
 * This driver needs this hotplug script in /etc/hotplug/usb/ti_usb_3410_5052
 * or in /etc/hotplug.d/usb/ti_usb_3410_5052.hotplug to set the device
 * configuration.
 *
 * #!/bin/bash
 *
 * BOOT_CONFIG=1
 * ACTIVE_CONFIG=2
 *
 * if [[ "$ACTION" != "add" ]]
 * then
 * 	exit
 * fi
 *
 * CONFIG_PATH=/sys${DEVPATH%/?*}/bConfigurationValue
 *
 * if [[ 0`cat $CONFIG_PATH` -ne $BOOT_CONFIG ]]
 * then
 * 	exit
 * fi
 *
 * PRODUCT=${PRODUCT%/?*}		# delete version
 * VENDOR_ID=`printf "%d" 0x${PRODUCT%/?*}`
 * PRODUCT_ID=`printf "%d" 0x${PRODUCT#*?/}`
 *
 * PARAM_PATH=/sys/module/ti_usb_3410_5052/parameters
 *
 * function scan() {
 * 	s=$1
 * 	shift
 * 	for i
 * 	do
 * 		if [[ $s -eq $i ]]
 * 		then
 * 			return 0
 * 		fi
 * 	done
 * 	return 1
 * }
 *
 * IFS=$IFS,
 *
 * if (scan $VENDOR_ID 1105 `cat $PARAM_PATH/vendor_3410` &&
 * scan $PRODUCT_ID 13328 `cat $PARAM_PATH/product_3410`) ||
 * (scan $VENDOR_ID 1105 `cat $PARAM_PATH/vendor_5052` &&
 * scan $PRODUCT_ID 20562 20818 20570 20575 `cat $PARAM_PATH/product_5052`)
 * then
 * 	echo $ACTIVE_CONFIG > $CONFIG_PATH
 * fi
 */

#include <linux/kernel.h>
#include <linux/errno.h>
A
Alan Cox 已提交
73
#include <linux/firmware.h>
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/ioctl.h>
#include <linux/serial.h>
#include <linux/circ_buf.h>
84
#include <linux/mutex.h>
L
Linus Torvalds 已提交
85 86
#include <asm/uaccess.h>
#include <linux/usb.h>
87
#include <linux/usb/serial.h>
88
#include <linux/firmware.h>
L
Linus Torvalds 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

#include "ti_usb_3410_5052.h"

/* Defines */

#define TI_DRIVER_VERSION	"v0.9"
#define TI_DRIVER_AUTHOR	"Al Borchers <alborchers@steinerpoint.com>"
#define TI_DRIVER_DESC		"TI USB 3410/5052 Serial Driver"

#define TI_FIRMWARE_BUF_SIZE	16284

#define TI_WRITE_BUF_SIZE	1024

#define TI_TRANSFER_TIMEOUT	2

#define TI_DEFAULT_LOW_LATENCY	0
#define TI_DEFAULT_CLOSING_WAIT	4000		/* in .01 secs */

/* supported setserial flags */
#define TI_SET_SERIAL_FLAGS	(ASYNC_LOW_LATENCY)

/* read urb states */
#define TI_READ_URB_RUNNING	0
#define TI_READ_URB_STOPPING	1
#define TI_READ_URB_STOPPED	2

#define TI_EXTRA_VID_PID_COUNT	5


/* Structures */

struct ti_port {
	int			tp_is_open;
	__u8			tp_msr;
	__u8			tp_lsr;
	__u8			tp_shadow_mcr;
	__u8			tp_uart_mode;	/* 232 or 485 modes */
	unsigned int		tp_uart_base_addr;
	int			tp_flags;
	int			tp_closing_wait;/* in .01 secs */
	struct async_icount	tp_icount;
	wait_queue_head_t	tp_msr_wait;	/* wait for msr change */
	wait_queue_head_t	tp_write_wait;
	struct ti_device	*tp_tdev;
	struct usb_serial_port	*tp_port;
	spinlock_t		tp_lock;
	int			tp_read_urb_state;
	int			tp_write_urb_in_use;
	struct circ_buf		*tp_write_buf;
};

struct ti_device {
141
	struct mutex		td_open_close_lock;
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151 152
	int			td_open_port_count;
	struct usb_serial	*td_serial;
	int			td_is_3410;
	int			td_urb_error;
};


/* Function Declarations */

static int ti_startup(struct usb_serial *serial);
static void ti_shutdown(struct usb_serial *serial);
A
Alan Cox 已提交
153 154 155 156 157 158 159 160 161 162 163 164
static int ti_open(struct tty_struct *tty, struct usb_serial_port *port,
				struct file *file);
static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
				struct file *file);
static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
				const unsigned char *data, int count);
static int ti_write_room(struct tty_struct *tty);
static int ti_chars_in_buffer(struct tty_struct *tty);
static void ti_throttle(struct tty_struct *tty);
static void ti_unthrottle(struct tty_struct *tty);
static int ti_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
static void ti_set_termios(struct tty_struct *tty, struct usb_serial_port *port,
A
Alan Cox 已提交
165
	struct ktermios *old_termios);
A
Alan Cox 已提交
166 167
static int ti_tiocmget(struct tty_struct *tty, struct file *file);
static int ti_tiocmset(struct tty_struct *tty, struct file *file,
L
Linus Torvalds 已提交
168
	unsigned int set, unsigned int clear);
A
Alan Cox 已提交
169
static void ti_break(struct tty_struct *tty, int break_state);
170 171 172
static void ti_interrupt_callback(struct urb *urb);
static void ti_bulk_in_callback(struct urb *urb);
static void ti_bulk_out_callback(struct urb *urb);
L
Linus Torvalds 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

static void ti_recv(struct device *dev, struct tty_struct *tty,
	unsigned char *data, int length);
static void ti_send(struct ti_port *tport);
static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
static int ti_get_lsr(struct ti_port *tport);
static int ti_get_serial_info(struct ti_port *tport,
	struct serial_struct __user *ret_arg);
static int ti_set_serial_info(struct ti_port *tport,
	struct serial_struct __user *new_arg);
static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);

static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush);

static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);

static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
	__u16 moduleid, __u16 value, __u8 *data, int size);
static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
	__u16 moduleid, __u16 value, __u8 *data, int size);

static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
	__u8 mask, __u8 byte);

A
Alan Cox 已提交
198
static int ti_download_firmware(struct ti_device *tdev, int type);
L
Linus Torvalds 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

/* circular buffer */
static struct circ_buf *ti_buf_alloc(void);
static void ti_buf_free(struct circ_buf *cb);
static void ti_buf_clear(struct circ_buf *cb);
static int ti_buf_data_avail(struct circ_buf *cb);
static int ti_buf_space_avail(struct circ_buf *cb);
static int ti_buf_put(struct circ_buf *cb, const char *buf, int count);
static int ti_buf_get(struct circ_buf *cb, char *buf, int count);


/* Data */

/* module parameters */
static int debug;
static int low_latency = TI_DEFAULT_LOW_LATENCY;
static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
217
static unsigned int vendor_3410_count;
L
Linus Torvalds 已提交
218
static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
219
static unsigned int product_3410_count;
L
Linus Torvalds 已提交
220
static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
221
static unsigned int vendor_5052_count;
L
Linus Torvalds 已提交
222
static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
223
static unsigned int product_5052_count;
L
Linus Torvalds 已提交
224 225 226 227 228 229 230

/* supported devices */
/* the array dimension is the number of default entries plus */
/* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
/* null entry */
static struct usb_device_id ti_id_table_3410[1+TI_EXTRA_VID_PID_COUNT+1] = {
	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
231
	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242
};

static struct usb_device_id ti_id_table_5052[4+TI_EXTRA_VID_PID_COUNT+1] = {
	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
	{ USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
};

static struct usb_device_id ti_id_table_combined[] = {
	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
243
	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253 254 255
	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
	{ USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
	{ }
};

static struct usb_driver ti_usb_driver = {
	.name			= "ti_usb_3410_5052",
	.probe			= usb_serial_probe,
	.disconnect		= usb_serial_disconnect,
	.id_table		= ti_id_table_combined,
256
	.no_dynamic_id = 	1,
L
Linus Torvalds 已提交
257 258
};

259
static struct usb_serial_driver ti_1port_device = {
260
	.driver = {
261 262
		.owner		= THIS_MODULE,
		.name		= "ti_usb_3410_5052_1",
263
	},
264
	.description		= "TI USB 3410 1 port adapter",
265
	.usb_driver		= &ti_usb_driver,
L
Linus Torvalds 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	.id_table		= ti_id_table_3410,
	.num_ports		= 1,
	.attach			= ti_startup,
	.shutdown		= ti_shutdown,
	.open			= ti_open,
	.close			= ti_close,
	.write			= ti_write,
	.write_room		= ti_write_room,
	.chars_in_buffer	= ti_chars_in_buffer,
	.throttle		= ti_throttle,
	.unthrottle		= ti_unthrottle,
	.ioctl			= ti_ioctl,
	.set_termios		= ti_set_termios,
	.tiocmget		= ti_tiocmget,
	.tiocmset		= ti_tiocmset,
	.break_ctl		= ti_break,
	.read_int_callback	= ti_interrupt_callback,
	.read_bulk_callback	= ti_bulk_in_callback,
	.write_bulk_callback	= ti_bulk_out_callback,
};

287
static struct usb_serial_driver ti_2port_device = {
288
	.driver = {
289 290
		.owner		= THIS_MODULE,
		.name		= "ti_usb_3410_5052_2",
291
	},
292
	.description		= "TI USB 5052 2 port adapter",
293
	.usb_driver		= &ti_usb_driver,
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	.id_table		= ti_id_table_5052,
	.num_ports		= 2,
	.attach			= ti_startup,
	.shutdown		= ti_shutdown,
	.open			= ti_open,
	.close			= ti_close,
	.write			= ti_write,
	.write_room		= ti_write_room,
	.chars_in_buffer	= ti_chars_in_buffer,
	.throttle		= ti_throttle,
	.unthrottle		= ti_unthrottle,
	.ioctl			= ti_ioctl,
	.set_termios		= ti_set_termios,
	.tiocmget		= ti_tiocmget,
	.tiocmset		= ti_tiocmset,
	.break_ctl		= ti_break,
	.read_int_callback	= ti_interrupt_callback,
	.read_bulk_callback	= ti_bulk_in_callback,
	.write_bulk_callback	= ti_bulk_out_callback,
};


/* Module */

MODULE_AUTHOR(TI_DRIVER_AUTHOR);
MODULE_DESCRIPTION(TI_DRIVER_DESC);
MODULE_VERSION(TI_DRIVER_VERSION);
MODULE_LICENSE("GPL");

323 324 325
MODULE_FIRMWARE("ti_3410.fw");
MODULE_FIRMWARE("ti_5052.fw");

L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Enable debugging, 0=no, 1=yes");

module_param(low_latency, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(low_latency, "TTY low_latency flag, 0=off, 1=on, default is off");

module_param(closing_wait, int, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(closing_wait, "Maximum wait for data to drain in close, in .01 secs, default is 4000");

module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
MODULE_PARM_DESC(vendor_3410, "Vendor ids for 3410 based devices, 1-5 short integers");
module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
MODULE_PARM_DESC(product_3410, "Product ids for 3410 based devices, 1-5 short integers");
module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
MODULE_PARM_DESC(vendor_5052, "Vendor ids for 5052 based devices, 1-5 short integers");
module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
MODULE_PARM_DESC(product_5052, "Product ids for 5052 based devices, 1-5 short integers");

MODULE_DEVICE_TABLE(usb, ti_id_table_combined);


/* Functions */

static int __init ti_init(void)
{
	int i,j;
	int ret;

	/* insert extra vendor and product ids */
355
	j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
L
Linus Torvalds 已提交
356 357 358 359 360
	for (i=0; i<min(vendor_3410_count,product_3410_count); i++,j++) {
		ti_id_table_3410[j].idVendor = vendor_3410[i];
		ti_id_table_3410[j].idProduct = product_3410[i];
		ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
	}
361
	j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
L
Linus Torvalds 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	for (i=0; i<min(vendor_5052_count,product_5052_count); i++,j++) {
		ti_id_table_5052[j].idVendor = vendor_5052[i];
		ti_id_table_5052[j].idProduct = product_5052[i];
		ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
	}

	ret = usb_serial_register(&ti_1port_device);
	if (ret)
		goto failed_1port;
	ret = usb_serial_register(&ti_2port_device);
	if (ret)
		goto failed_2port;

	ret = usb_register(&ti_usb_driver);
	if (ret)
		goto failed_usb;

	info(TI_DRIVER_DESC " " TI_DRIVER_VERSION);

	return 0;

failed_usb:
	usb_serial_deregister(&ti_2port_device);
failed_2port:
	usb_serial_deregister(&ti_1port_device);
failed_1port:
	return ret;
}


static void __exit ti_exit(void)
{
	usb_serial_deregister(&ti_1port_device);
	usb_serial_deregister(&ti_2port_device);
	usb_deregister(&ti_usb_driver);
}


module_init(ti_init);
module_exit(ti_exit);


static int ti_startup(struct usb_serial *serial)
{
	struct ti_device *tdev;
	struct ti_port *tport;
	struct usb_device *dev = serial->dev;
	int status;
	int i;


	dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
414
	    __func__, le16_to_cpu(dev->descriptor.idProduct),
L
Linus Torvalds 已提交
415 416 417 418
	    dev->descriptor.bNumConfigurations,
	    dev->actconfig->desc.bConfigurationValue);

	/* create device structure */
419
	tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
L
Linus Torvalds 已提交
420
	if (tdev == NULL) {
421
		dev_err(&dev->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
422 423
		return -ENOMEM;
	}
424
	mutex_init(&tdev->td_open_close_lock);
L
Linus Torvalds 已提交
425 426 427 428 429 430
	tdev->td_serial = serial;
	usb_set_serial_data(serial, tdev);

	/* determine device type */
	if (usb_match_id(serial->interface, ti_id_table_3410))
		tdev->td_is_3410 = 1;
431
	dbg("%s - device type is %s", __func__, tdev->td_is_3410 ? "3410" : "5052");
L
Linus Torvalds 已提交
432 433 434 435

	/* if we have only 1 configuration, download firmware */
	if (dev->descriptor.bNumConfigurations == 1) {
		if (tdev->td_is_3410)
A
Alan Cox 已提交
436
			status = ti_download_firmware(tdev, 3410);
L
Linus Torvalds 已提交
437
		else
A
Alan Cox 已提交
438
			status = ti_download_firmware(tdev, 5052);
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
		if (status)
			goto free_tdev;

		/* 3410 must be reset, 5052 resets itself */
		if (tdev->td_is_3410) {
			msleep_interruptible(100);
			usb_reset_device(dev);
		}

		status = -ENODEV;
		goto free_tdev;
	} 

	/* the second configuration must be set (in sysfs by hotplug script) */
	if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
		status = -ENODEV;
		goto free_tdev;
	}

	/* set up port structures */
	for (i = 0; i < serial->num_ports; ++i) {
460
		tport = kzalloc(sizeof(struct ti_port), GFP_KERNEL);
L
Linus Torvalds 已提交
461
		if (tport == NULL) {
462
			dev_err(&dev->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
463 464 465 466 467 468 469 470 471 472 473
			status = -ENOMEM;
			goto free_tports;
		}
		spin_lock_init(&tport->tp_lock);
		tport->tp_uart_base_addr = (i == 0 ? TI_UART1_BASE_ADDR : TI_UART2_BASE_ADDR);
		tport->tp_flags = low_latency ? ASYNC_LOW_LATENCY : 0;
		tport->tp_closing_wait = closing_wait;
		init_waitqueue_head(&tport->tp_msr_wait);
		init_waitqueue_head(&tport->tp_write_wait);
		tport->tp_write_buf = ti_buf_alloc();
		if (tport->tp_write_buf == NULL) {
474
			dev_err(&dev->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
			kfree(tport);
			status = -ENOMEM;
			goto free_tports;
		}
		tport->tp_port = serial->port[i];
		tport->tp_tdev = tdev;
		usb_set_serial_port_data(serial->port[i], tport);
		tport->tp_uart_mode = 0;	/* default is RS232 */
	}
	
	return 0;

free_tports:
	for (--i; i>=0; --i) {
		tport = usb_get_serial_port_data(serial->port[i]);
		ti_buf_free(tport->tp_write_buf);
		kfree(tport);
		usb_set_serial_port_data(serial->port[i], NULL);
	}
free_tdev:
	kfree(tdev);
	usb_set_serial_data(serial, NULL);
	return status;
}


static void ti_shutdown(struct usb_serial *serial)
{
	int i;
	struct ti_device *tdev = usb_get_serial_data(serial);
	struct ti_port *tport;

507
	dbg("%s", __func__);
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517

	for (i=0; i < serial->num_ports; ++i) {
		tport = usb_get_serial_port_data(serial->port[i]);
		if (tport) {
			ti_buf_free(tport->tp_write_buf);
			kfree(tport);
			usb_set_serial_port_data(serial->port[i], NULL);
		}
	}

518
	kfree(tdev);
L
Linus Torvalds 已提交
519 520 521 522
	usb_set_serial_data(serial, NULL);
}


A
Alan Cox 已提交
523 524
static int ti_open(struct tty_struct *tty,
			struct usb_serial_port *port, struct file *file)
L
Linus Torvalds 已提交
525 526 527 528 529 530 531 532 533 534 535
{
	struct ti_port *tport = usb_get_serial_port_data(port);
	struct ti_device *tdev;
	struct usb_device *dev;
	struct urb *urb;
	int port_number;
	int status;
	__u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS | 
			     TI_PIPE_TIMEOUT_ENABLE | 
			     (TI_TRANSFER_TIMEOUT << 2));

536
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
537 538 539 540 541 542 543 544

	if (tport == NULL)
		return -ENODEV;

	dev = port->serial->dev;
	tdev = tport->tp_tdev;

	/* only one open on any port on a device at a time */
545
	if (mutex_lock_interruptible(&tdev->td_open_close_lock))
L
Linus Torvalds 已提交
546 547
		return -ERESTARTSYS;

A
Alan Cox 已提交
548 549 550
	if (tty)
		tty->low_latency =
				(tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
L
Linus Torvalds 已提交
551 552 553 554 555 556 557 558 559 560

	port_number = port->number - port->serial->minor;

	memset(&(tport->tp_icount), 0x00, sizeof(tport->tp_icount));

	tport->tp_msr = 0;
	tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);

	/* start interrupt urb the first time a port is opened on this device */
	if (tdev->td_open_port_count == 0) {
561
		dbg("%s - start interrupt in urb", __func__);
L
Linus Torvalds 已提交
562 563
		urb = tdev->td_serial->port[0]->interrupt_in_urb;
		if (!urb) {
564
			dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
L
Linus Torvalds 已提交
565
			status = -EINVAL;
566
			goto release_lock;
L
Linus Torvalds 已提交
567 568 569 570 571 572
		}
		urb->complete = ti_interrupt_callback;
		urb->context = tdev;
		urb->dev = dev;
		status = usb_submit_urb(urb, GFP_KERNEL);
		if (status) {
573
			dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
574
			goto release_lock;
L
Linus Torvalds 已提交
575 576 577
		}
	}

A
Alan Cox 已提交
578 579
	if (tty)
		ti_set_termios(tty, port, tty->termios);
L
Linus Torvalds 已提交
580

581
	dbg("%s - sending TI_OPEN_PORT", __func__);
L
Linus Torvalds 已提交
582 583 584
	status = ti_command_out_sync(tdev, TI_OPEN_PORT,
		(__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
	if (status) {
585
		dev_err(&port->dev, "%s - cannot send open command, %d\n", __func__, status);
L
Linus Torvalds 已提交
586 587 588
		goto unlink_int_urb;
	}

589
	dbg("%s - sending TI_START_PORT", __func__);
L
Linus Torvalds 已提交
590 591 592
	status = ti_command_out_sync(tdev, TI_START_PORT,
		(__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
	if (status) {
593
		dev_err(&port->dev, "%s - cannot send start command, %d\n", __func__, status);
L
Linus Torvalds 已提交
594 595 596
		goto unlink_int_urb;
	}

597
	dbg("%s - sending TI_PURGE_PORT", __func__);
L
Linus Torvalds 已提交
598 599 600
	status = ti_command_out_sync(tdev, TI_PURGE_PORT,
		(__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
	if (status) {
601
		dev_err(&port->dev, "%s - cannot clear input buffers, %d\n", __func__, status);
L
Linus Torvalds 已提交
602 603 604 605 606
		goto unlink_int_urb;
	}
	status = ti_command_out_sync(tdev, TI_PURGE_PORT,
		(__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
	if (status) {
607
		dev_err(&port->dev, "%s - cannot clear output buffers, %d\n", __func__, status);
L
Linus Torvalds 已提交
608 609 610 611 612 613 614 615
		goto unlink_int_urb;
	}

	/* reset the data toggle on the bulk endpoints to work around bug in
	 * host controllers where things get out of sync some times */
	usb_clear_halt(dev, port->write_urb->pipe);
	usb_clear_halt(dev, port->read_urb->pipe);

A
Alan Cox 已提交
616 617
	if (tty)
		ti_set_termios(tty, port, tty->termios);
L
Linus Torvalds 已提交
618

619
	dbg("%s - sending TI_OPEN_PORT (2)", __func__);
L
Linus Torvalds 已提交
620 621 622
	status = ti_command_out_sync(tdev, TI_OPEN_PORT,
		(__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
	if (status) {
623
		dev_err(&port->dev, "%s - cannot send open command (2), %d\n", __func__, status);
L
Linus Torvalds 已提交
624 625 626
		goto unlink_int_urb;
	}

627
	dbg("%s - sending TI_START_PORT (2)", __func__);
L
Linus Torvalds 已提交
628 629 630
	status = ti_command_out_sync(tdev, TI_START_PORT,
		(__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
	if (status) {
631
		dev_err(&port->dev, "%s - cannot send start command (2), %d\n", __func__, status);
L
Linus Torvalds 已提交
632 633 634 635
		goto unlink_int_urb;
	}

	/* start read urb */
636
	dbg("%s - start read urb", __func__);
L
Linus Torvalds 已提交
637 638
	urb = port->read_urb;
	if (!urb) {
639
		dev_err(&port->dev, "%s - no read urb\n", __func__);
L
Linus Torvalds 已提交
640 641 642 643 644 645 646 647 648
		status = -EINVAL;
		goto unlink_int_urb;
	}
	tport->tp_read_urb_state = TI_READ_URB_RUNNING;
	urb->complete = ti_bulk_in_callback;
	urb->context = tport;
	urb->dev = dev;
	status = usb_submit_urb(urb, GFP_KERNEL);
	if (status) {
649
		dev_err(&port->dev, "%s - submit read urb failed, %d\n", __func__, status);
L
Linus Torvalds 已提交
650 651 652 653 654 655
		goto unlink_int_urb;
	}

	tport->tp_is_open = 1;
	++tdev->td_open_port_count;

656
	goto release_lock;
L
Linus Torvalds 已提交
657 658 659 660

unlink_int_urb:
	if (tdev->td_open_port_count == 0)
		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
661 662
release_lock:
	mutex_unlock(&tdev->td_open_close_lock);
663
	dbg("%s - exit %d", __func__, status);
L
Linus Torvalds 已提交
664 665 666 667
	return status;
}


A
Alan Cox 已提交
668 669
static void ti_close(struct tty_struct *tty, struct usb_serial_port *port,
							struct file *file)
L
Linus Torvalds 已提交
670 671 672 673 674
{
	struct ti_device *tdev;
	struct ti_port *tport;
	int port_number;
	int status;
675
	int do_unlock;
L
Linus Torvalds 已提交
676

677
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
			 
	tdev = usb_get_serial_data(port->serial);
	tport = usb_get_serial_port_data(port);
	if (tdev == NULL || tport == NULL)
		return;

	tport->tp_is_open = 0;

	ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 1);

	usb_kill_urb(port->read_urb);
	usb_kill_urb(port->write_urb);
	tport->tp_write_urb_in_use = 0;

	port_number = port->number - port->serial->minor;

694
	dbg("%s - sending TI_CLOSE_PORT", __func__);
L
Linus Torvalds 已提交
695 696 697
	status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
		     (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
	if (status)
698
		dev_err(&port->dev, "%s - cannot send close port command, %d\n" , __func__, status);
L
Linus Torvalds 已提交
699

700 701
	/* if mutex_lock is interrupted, continue anyway */
	do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
L
Linus Torvalds 已提交
702 703 704 705 706 707
	--tport->tp_tdev->td_open_port_count;
	if (tport->tp_tdev->td_open_port_count <= 0) {
		/* last port is closed, shut down interrupt urb */
		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
		tport->tp_tdev->td_open_port_count = 0;
	}
708 709
	if (do_unlock)
		mutex_unlock(&tdev->td_open_close_lock);
L
Linus Torvalds 已提交
710

711
	dbg("%s - exit", __func__);
L
Linus Torvalds 已提交
712 713 714
}


A
Alan Cox 已提交
715 716
static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
			const unsigned char *data, int count)
L
Linus Torvalds 已提交
717 718 719 720
{
	struct ti_port *tport = usb_get_serial_port_data(port);
	unsigned long flags;

721
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
722 723

	if (count == 0) {
724
		dbg("%s - write request of 0 bytes", __func__);
L
Linus Torvalds 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
		return 0;
	}

	if (tport == NULL || !tport->tp_is_open)
		return -ENODEV;

	spin_lock_irqsave(&tport->tp_lock, flags);
	count = ti_buf_put(tport->tp_write_buf, data, count);
	spin_unlock_irqrestore(&tport->tp_lock, flags);

	ti_send(tport);

	return count;
}


A
Alan Cox 已提交
741
static int ti_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
742
{
A
Alan Cox 已提交
743
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
744 745 746 747
	struct ti_port *tport = usb_get_serial_port_data(port);
	int room = 0;
	unsigned long flags;

748
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
749 750 751 752 753 754 755 756

	if (tport == NULL)
		return -ENODEV;
	
	spin_lock_irqsave(&tport->tp_lock, flags);
	room = ti_buf_space_avail(tport->tp_write_buf);
	spin_unlock_irqrestore(&tport->tp_lock, flags);

757
	dbg("%s - returns %d", __func__, room);
L
Linus Torvalds 已提交
758 759 760 761
	return room;
}


A
Alan Cox 已提交
762
static int ti_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
763
{
A
Alan Cox 已提交
764
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
765 766 767 768
	struct ti_port *tport = usb_get_serial_port_data(port);
	int chars = 0;
	unsigned long flags;

769
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
770 771 772 773 774 775 776 777

	if (tport == NULL)
		return -ENODEV;

	spin_lock_irqsave(&tport->tp_lock, flags);
	chars = ti_buf_data_avail(tport->tp_write_buf);
	spin_unlock_irqrestore(&tport->tp_lock, flags);

778
	dbg("%s - returns %d", __func__, chars);
L
Linus Torvalds 已提交
779 780 781 782
	return chars;
}


A
Alan Cox 已提交
783
static void ti_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
784
{
A
Alan Cox 已提交
785
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
786 787
	struct ti_port *tport = usb_get_serial_port_data(port);

788
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
789 790 791 792 793 794 795 796 797 798

	if (tport == NULL)
		return;

	if (I_IXOFF(tty) || C_CRTSCTS(tty))
		ti_stop_read(tport, tty);

}


A
Alan Cox 已提交
799
static void ti_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
800
{
A
Alan Cox 已提交
801
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
802 803 804
	struct ti_port *tport = usb_get_serial_port_data(port);
	int status;

805
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
806 807 808 809 810 811 812

	if (tport == NULL)
		return;

	if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
		status = ti_restart_read(tport, tty);
		if (status)
813
			dev_err(&port->dev, "%s - cannot restart read, %d\n", __func__, status);
L
Linus Torvalds 已提交
814 815 816 817
	}
}


A
Alan Cox 已提交
818
static int ti_ioctl(struct tty_struct *tty, struct file *file,
L
Linus Torvalds 已提交
819 820
	unsigned int cmd, unsigned long arg)
{
A
Alan Cox 已提交
821
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
822 823 824 825
	struct ti_port *tport = usb_get_serial_port_data(port);
	struct async_icount cnow;
	struct async_icount cprev;

826
	dbg("%s - port %d, cmd = 0x%04X", __func__, port->number, cmd);
L
Linus Torvalds 已提交
827 828 829 830 831 832

	if (tport == NULL)
		return -ENODEV;

	switch (cmd) {
		case TIOCGSERIAL:
833
			dbg("%s - (%d) TIOCGSERIAL", __func__, port->number);
L
Linus Torvalds 已提交
834 835 836 837
			return ti_get_serial_info(tport, (struct serial_struct __user *)arg);
			break;

		case TIOCSSERIAL:
838
			dbg("%s - (%d) TIOCSSERIAL", __func__, port->number);
L
Linus Torvalds 已提交
839 840 841 842
			return ti_set_serial_info(tport, (struct serial_struct __user *)arg);
			break;

		case TIOCMIWAIT:
843
			dbg("%s - (%d) TIOCMIWAIT", __func__, port->number);
L
Linus Torvalds 已提交
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
			cprev = tport->tp_icount;
			while (1) {
				interruptible_sleep_on(&tport->tp_msr_wait);
				if (signal_pending(current))
					return -ERESTARTSYS;
				cnow = tport->tp_icount;
				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
					return -EIO; /* no change => error */
				if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
				    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
				    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
				    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
					return 0;
				}
				cprev = cnow;
			}
			break;

		case TIOCGICOUNT:
864
			dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d", __func__, port->number, tport->tp_icount.rx, tport->tp_icount.tx);
L
Linus Torvalds 已提交
865 866 867 868 869 870 871 872 873
			if (copy_to_user((void __user *)arg, &tport->tp_icount, sizeof(tport->tp_icount)))
				return -EFAULT;
			return 0;
	}

	return -ENOIOCTLCMD;
}


A
Alan Cox 已提交
874 875
static void ti_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
876 877 878 879 880 881 882 883 884
{
	struct ti_port *tport = usb_get_serial_port_data(port);
	struct ti_uart_config *config;
	tcflag_t cflag,iflag;
	int baud;
	int status;
	int port_number = port->number - port->serial->minor;
	unsigned int mcr;

885
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
886 887 888 889

	cflag = tty->termios->c_cflag;
	iflag = tty->termios->c_iflag;

890 891
	dbg("%s - cflag %08x, iflag %08x", __func__, cflag, iflag);
	dbg("%s - old clfag %08x, old iflag %08x", __func__, old_termios->c_cflag, old_termios->c_iflag);
L
Linus Torvalds 已提交
892 893 894 895 896 897

	if (tport == NULL)
		return;

	config = kmalloc(sizeof(*config), GFP_KERNEL);
	if (!config) {
898
		dev_err(&port->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
		return;
	}

	config->wFlags = 0;

	/* these flags must be set */
	config->wFlags |= TI_UART_ENABLE_MS_INTS;
	config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
	config->bUartMode = (__u8)(tport->tp_uart_mode);

	switch (cflag & CSIZE) {
		case CS5:
			    config->bDataBits = TI_UART_5_DATA_BITS;
			    break;
		case CS6:
			    config->bDataBits = TI_UART_6_DATA_BITS;
			    break;
		case CS7:
			    config->bDataBits = TI_UART_7_DATA_BITS;
			    break;
		default:
		case CS8:
			    config->bDataBits = TI_UART_8_DATA_BITS;
			    break;
	}

A
Alan Cox 已提交
925 926 927
	/* CMSPAR isn't supported by this driver */
	tty->termios->c_cflag &= ~CMSPAR;

L
Linus Torvalds 已提交
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	if (cflag & PARENB) {
		if (cflag & PARODD) {
			config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
			config->bParity = TI_UART_ODD_PARITY;
		} else {
			config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
			config->bParity = TI_UART_EVEN_PARITY;
		}
	} else {
		config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
		config->bParity = TI_UART_NO_PARITY; 	
	}

	if (cflag & CSTOPB)
		config->bStopBits = TI_UART_2_STOP_BITS;
	else
		config->bStopBits = TI_UART_1_STOP_BITS;

	if (cflag & CRTSCTS) {
		/* RTS flow control must be off to drop RTS for baud rate B0 */
		if ((cflag & CBAUD) != B0)
			config->wFlags |= TI_UART_ENABLE_RTS_IN;
		config->wFlags |= TI_UART_ENABLE_CTS_OUT;
	} else {
		tty->hw_stopped = 0;
		ti_restart_read(tport, tty);
	}

	if (I_IXOFF(tty) || I_IXON(tty)) {
		config->cXon  = START_CHAR(tty);
		config->cXoff = STOP_CHAR(tty);

		if (I_IXOFF(tty))
			config->wFlags |= TI_UART_ENABLE_X_IN;
		else
			ti_restart_read(tport, tty);

		if (I_IXON(tty))
			config->wFlags |= TI_UART_ENABLE_X_OUT;
	}

	baud = tty_get_baud_rate(tty);
A
Alan Cox 已提交
970 971
	if (!baud)
		baud = 9600;
L
Linus Torvalds 已提交
972 973 974 975 976
	if (tport->tp_tdev->td_is_3410)
		config->wBaudRate = (__u16)((923077 + baud/2) / baud);
	else
		config->wBaudRate = (__u16)((461538 + baud/2) / baud);

A
Alan Cox 已提交
977 978 979 980
	/* FIXME: Should calculate resulting baud here and report it back */
	if ((cflag & CBAUD) != B0)
		tty_encode_baud_rate(tty, baud, baud);

L
Linus Torvalds 已提交
981
	dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
982
	__func__, baud, config->wBaudRate, config->wFlags, config->bDataBits, config->bParity, config->bStopBits, config->cXon, config->cXoff, config->bUartMode);
L
Linus Torvalds 已提交
983 984 985 986 987 988 989 990

	cpu_to_be16s(&config->wBaudRate);
	cpu_to_be16s(&config->wFlags);

	status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
		(__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
		sizeof(*config));
	if (status)
991
		dev_err(&port->dev, "%s - cannot set config on port %d, %d\n", __func__, port_number, status);
L
Linus Torvalds 已提交
992 993 994 995 996 997 998 999

	/* SET_CONFIG asserts RTS and DTR, reset them correctly */
	mcr = tport->tp_shadow_mcr;
	/* if baud rate is B0, clear RTS and DTR */
	if ((cflag & CBAUD) == B0)
		mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
	status = ti_set_mcr(tport, mcr);
	if (status)
1000
		dev_err(&port->dev, "%s - cannot set modem control on port %d, %d\n", __func__, port_number, status);
L
Linus Torvalds 已提交
1001 1002 1003 1004 1005

	kfree(config);
}


A
Alan Cox 已提交
1006
static int ti_tiocmget(struct tty_struct *tty, struct file *file)
L
Linus Torvalds 已提交
1007
{
A
Alan Cox 已提交
1008
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1009 1010 1011 1012
	struct ti_port *tport = usb_get_serial_port_data(port);
	unsigned int result;
	unsigned int msr;
	unsigned int mcr;
1013
	unsigned long flags;
L
Linus Torvalds 已提交
1014

1015
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
1016 1017 1018 1019

	if (tport == NULL)
		return -ENODEV;

1020
	spin_lock_irqsave(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1021 1022
	msr = tport->tp_msr;
	mcr = tport->tp_shadow_mcr;
1023
	spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032

	result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
		| ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
		| ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
		| ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
		| ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
		| ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
		| ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);

1033
	dbg("%s - 0x%04X", __func__, result);
L
Linus Torvalds 已提交
1034 1035 1036 1037 1038

	return result;
}


A
Alan Cox 已提交
1039
static int ti_tiocmset(struct tty_struct *tty, struct file *file,
L
Linus Torvalds 已提交
1040 1041
	unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
1042
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1043 1044
	struct ti_port *tport = usb_get_serial_port_data(port);
	unsigned int mcr;
1045
	unsigned long flags;
L
Linus Torvalds 已提交
1046

1047
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
1048 1049 1050 1051

	if (tport == NULL)
		return -ENODEV;

1052
	spin_lock_irqsave(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
	mcr = tport->tp_shadow_mcr;

	if (set & TIOCM_RTS)
		mcr |= TI_MCR_RTS;
	if (set & TIOCM_DTR)
		mcr |= TI_MCR_DTR;
	if (set & TIOCM_LOOP)
		mcr |= TI_MCR_LOOP;

	if (clear & TIOCM_RTS)
		mcr &= ~TI_MCR_RTS;
	if (clear & TIOCM_DTR)
		mcr &= ~TI_MCR_DTR;
	if (clear & TIOCM_LOOP)
		mcr &= ~TI_MCR_LOOP;
1068
	spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1069 1070 1071 1072 1073

	return ti_set_mcr(tport, mcr);
}


A
Alan Cox 已提交
1074
static void ti_break(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
1075
{
A
Alan Cox 已提交
1076
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1077 1078 1079
	struct ti_port *tport = usb_get_serial_port_data(port);
	int status;

1080
	dbg("%s - state = %d", __func__, break_state);
L
Linus Torvalds 已提交
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

	if (tport == NULL)
		return;

	ti_drain(tport, (tport->tp_closing_wait*HZ)/100, 0);

	status = ti_write_byte(tport->tp_tdev,
		tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
		TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);

	if (status)
1092
		dbg("%s - error setting break, %d", __func__, status);
L
Linus Torvalds 已提交
1093 1094 1095
}


1096
static void ti_interrupt_callback(struct urb *urb)
L
Linus Torvalds 已提交
1097
{
1098
	struct ti_device *tdev = urb->context;
L
Linus Torvalds 已提交
1099 1100 1101 1102 1103 1104 1105 1106
	struct usb_serial_port *port;
	struct usb_serial *serial = tdev->td_serial;
	struct ti_port *tport;
	struct device *dev = &urb->dev->dev;
	unsigned char *data = urb->transfer_buffer;
	int length = urb->actual_length;
	int port_number;
	int function;
1107 1108
	int status = urb->status;
	int retval;
L
Linus Torvalds 已提交
1109 1110
	__u8 msr;

1111
	dbg("%s", __func__);
L
Linus Torvalds 已提交
1112

1113
	switch (status) {
L
Linus Torvalds 已提交
1114 1115 1116 1117 1118
	case 0:
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
1119
		dbg("%s - urb shutting down, %d", __func__, status);
L
Linus Torvalds 已提交
1120 1121 1122
		tdev->td_urb_error = 1;
		return;
	default:
1123
		dev_err(dev, "%s - nonzero urb status, %d\n",
1124
			__func__, status);
L
Linus Torvalds 已提交
1125 1126 1127 1128 1129
		tdev->td_urb_error = 1;
		goto exit;
	}

	if (length != 2) {
1130
		dbg("%s - bad packet size, %d", __func__, length);
L
Linus Torvalds 已提交
1131 1132 1133 1134
		goto exit;
	}

	if (data[0] == TI_CODE_HARDWARE_ERROR) {
1135
		dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
L
Linus Torvalds 已提交
1136 1137 1138 1139 1140 1141
		goto exit;
	}

	port_number = TI_GET_PORT_FROM_CODE(data[0]);
	function = TI_GET_FUNC_FROM_CODE(data[0]);

1142
	dbg("%s - port_number %d, function %d, data 0x%02X", __func__, port_number, function, data[1]);
L
Linus Torvalds 已提交
1143 1144

	if (port_number >= serial->num_ports) {
1145
		dev_err(dev, "%s - bad port number, %d\n", __func__, port_number);
L
Linus Torvalds 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
		goto exit;
	}

	port = serial->port[port_number];

	tport = usb_get_serial_port_data(port);
	if (!tport)
		goto exit;

	switch (function) {
	case TI_CODE_DATA_ERROR:
1157
		dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n", __func__, port_number, data[1]);
L
Linus Torvalds 已提交
1158 1159 1160 1161
		break;

	case TI_CODE_MODEM_STATUS:
		msr = data[1];
1162
		dbg("%s - port %d, msr 0x%02X", __func__, port_number, msr);
L
Linus Torvalds 已提交
1163 1164 1165 1166
		ti_handle_new_msr(tport, msr);
		break;

	default:
1167
		dev_err(dev, "%s - unknown interrupt code, 0x%02X\n", __func__, data[1]);
L
Linus Torvalds 已提交
1168 1169 1170 1171
		break;
	}

exit:
1172 1173 1174
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval)
		dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1175
			__func__, retval);
L
Linus Torvalds 已提交
1176 1177 1178
}


1179
static void ti_bulk_in_callback(struct urb *urb)
L
Linus Torvalds 已提交
1180
{
1181
	struct ti_port *tport = urb->context;
L
Linus Torvalds 已提交
1182 1183
	struct usb_serial_port *port = tport->tp_port;
	struct device *dev = &urb->dev->dev;
1184 1185
	int status = urb->status;
	int retval = 0;
L
Linus Torvalds 已提交
1186

1187
	dbg("%s", __func__);
L
Linus Torvalds 已提交
1188

1189
	switch (status) {
L
Linus Torvalds 已提交
1190 1191 1192 1193 1194
	case 0:
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
1195
		dbg("%s - urb shutting down, %d", __func__, status);
L
Linus Torvalds 已提交
1196 1197 1198 1199
		tport->tp_tdev->td_urb_error = 1;
		wake_up_interruptible(&tport->tp_write_wait);
		return;
	default:
1200
		dev_err(dev, "%s - nonzero urb status, %d\n",
1201
			__func__, status );
L
Linus Torvalds 已提交
1202 1203 1204 1205
		tport->tp_tdev->td_urb_error = 1;
		wake_up_interruptible(&tport->tp_write_wait);
	}

1206
	if (status == -EPIPE)
L
Linus Torvalds 已提交
1207 1208
		goto exit;

1209
	if (status) {
1210
		dev_err(dev, "%s - stopping read!\n", __func__);
L
Linus Torvalds 已提交
1211 1212 1213
		return;
	}

A
Alan Cox 已提交
1214
	if (port->port.tty && urb->actual_length) {
1215
		usb_serial_debug_data(debug, dev, __func__,
L
Linus Torvalds 已提交
1216 1217 1218
			urb->actual_length, urb->transfer_buffer);

		if (!tport->tp_is_open)
1219
			dbg("%s - port closed, dropping data", __func__);
L
Linus Torvalds 已提交
1220
		else
A
Alan Cox 已提交
1221
			ti_recv(&urb->dev->dev, port->port.tty, urb->transfer_buffer,
L
Linus Torvalds 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
				urb->actual_length);

		spin_lock(&tport->tp_lock);
		tport->tp_icount.rx += urb->actual_length;
		spin_unlock(&tport->tp_lock);
	}

exit:
	/* continue to read unless stopping */
	spin_lock(&tport->tp_lock);
	if (tport->tp_read_urb_state == TI_READ_URB_RUNNING) {
		urb->dev = port->serial->dev;
1234
		retval = usb_submit_urb(urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
1235 1236 1237 1238
	} else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING) {
		tport->tp_read_urb_state = TI_READ_URB_STOPPED;
	}
	spin_unlock(&tport->tp_lock);
1239 1240
	if (retval)
		dev_err(dev, "%s - resubmit read urb failed, %d\n",
1241
			__func__, retval);
L
Linus Torvalds 已提交
1242 1243 1244
}


1245
static void ti_bulk_out_callback(struct urb *urb)
L
Linus Torvalds 已提交
1246
{
1247
	struct ti_port *tport = urb->context;
L
Linus Torvalds 已提交
1248 1249
	struct usb_serial_port *port = tport->tp_port;
	struct device *dev = &urb->dev->dev;
1250
	int status = urb->status;
L
Linus Torvalds 已提交
1251

1252
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
1253 1254 1255

	tport->tp_write_urb_in_use = 0;

1256
	switch (status) {
L
Linus Torvalds 已提交
1257 1258 1259 1260 1261
	case 0:
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
1262
		dbg("%s - urb shutting down, %d", __func__, status);
L
Linus Torvalds 已提交
1263 1264 1265 1266
		tport->tp_tdev->td_urb_error = 1;
		wake_up_interruptible(&tport->tp_write_wait);
		return;
	default:
1267
		dev_err(dev, "%s - nonzero urb status, %d\n",
1268
			__func__, status);
L
Linus Torvalds 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
		tport->tp_tdev->td_urb_error = 1;
		wake_up_interruptible(&tport->tp_write_wait);
	}

	/* send any buffered data */
	ti_send(tport);
}


static void ti_recv(struct device *dev, struct tty_struct *tty,
	unsigned char *data, int length)
{
	int cnt;

	do {
A
Alan Cox 已提交
1284 1285
		cnt = tty_buffer_request_room(tty, length);
		if (cnt < length) {
1286
			dev_err(dev, "%s - dropping data, %d bytes lost\n", __func__, length - cnt);
A
Alan Cox 已提交
1287 1288
			if(cnt == 0)
				break;
L
Linus Torvalds 已提交
1289
		}
A
Alan Cox 已提交
1290 1291
		tty_insert_flip_string(tty, data, cnt);
		tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
		data += cnt;
		length -= cnt;
	} while (length > 0);

}


static void ti_send(struct ti_port *tport)
{
	int count, result;
	struct usb_serial_port *port = tport->tp_port;
A
Alan Cox 已提交
1303
	struct tty_struct *tty = port->port.tty;	/* FIXME */
L
Linus Torvalds 已提交
1304 1305 1306
	unsigned long flags;


1307
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

	spin_lock_irqsave(&tport->tp_lock, flags);

	if (tport->tp_write_urb_in_use) {
		spin_unlock_irqrestore(&tport->tp_lock, flags);
		return;
	}

	count = ti_buf_get(tport->tp_write_buf,
				port->write_urb->transfer_buffer,
				port->bulk_out_size);

	if (count == 0) {
		spin_unlock_irqrestore(&tport->tp_lock, flags);
		return;
	}

	tport->tp_write_urb_in_use = 1;

	spin_unlock_irqrestore(&tport->tp_lock, flags);

1329
	usb_serial_debug_data(debug, &port->dev, __func__, count, port->write_urb->transfer_buffer);
L
Linus Torvalds 已提交
1330 1331 1332 1333 1334 1335 1336 1337 1338

	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
			   usb_sndbulkpipe(port->serial->dev,
					    port->bulk_out_endpointAddress),
			   port->write_urb->transfer_buffer, count,
			   ti_bulk_out_callback, tport);

	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
	if (result) {
1339
		dev_err(&port->dev, "%s - submit write urb failed, %d\n", __func__, result);
L
Linus Torvalds 已提交
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
		tport->tp_write_urb_in_use = 0; 
		/* TODO: reschedule ti_send */
	} else {
		spin_lock_irqsave(&tport->tp_lock, flags);
		tport->tp_icount.tx += count;
		spin_unlock_irqrestore(&tport->tp_lock, flags);
	}

	/* more room in the buffer for new writes, wakeup */
	if (tty)
		tty_wakeup(tty);
	wake_up_interruptible(&tport->tp_write_wait);
}


static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
{
1357
	unsigned long flags;
L
Linus Torvalds 已提交
1358 1359 1360 1361 1362 1363
	int status;

	status = ti_write_byte(tport->tp_tdev,
		tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
		TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);

1364
	spin_lock_irqsave(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1365 1366
	if (!status)
		tport->tp_shadow_mcr = mcr;
1367
	spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380

	return status;
}


static int ti_get_lsr(struct ti_port *tport)
{
	int size,status;
	struct ti_device *tdev = tport->tp_tdev;
	struct usb_serial_port *port = tport->tp_port;
	int port_number = port->number - port->serial->minor;
	struct ti_port_status *data;

1381
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
1382 1383 1384 1385

	size = sizeof(struct ti_port_status);
	data = kmalloc(size, GFP_KERNEL);
	if (!data) {
1386
		dev_err(&port->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
1387 1388 1389 1390 1391 1392
		return -ENOMEM;
	}

	status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
		(__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
	if (status) {
1393
		dev_err(&port->dev, "%s - get port status command failed, %d\n", __func__, status);
L
Linus Torvalds 已提交
1394 1395 1396
		goto free_data;
	}

1397
	dbg("%s - lsr 0x%02X", __func__, data->bLSR);
L
Linus Torvalds 已提交
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442

	tport->tp_lsr = data->bLSR;

free_data:
	kfree(data);
	return status;
}


static int ti_get_serial_info(struct ti_port *tport,
	struct serial_struct __user *ret_arg)
{
	struct usb_serial_port *port = tport->tp_port;
	struct serial_struct ret_serial;

	if (!ret_arg)
		return -EFAULT;

	memset(&ret_serial, 0, sizeof(ret_serial));

	ret_serial.type = PORT_16550A;
	ret_serial.line = port->serial->minor;
	ret_serial.port = port->number - port->serial->minor;
	ret_serial.flags = tport->tp_flags;
	ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
	ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
	ret_serial.closing_wait = tport->tp_closing_wait;

	if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
		return -EFAULT;

	return 0;
}


static int ti_set_serial_info(struct ti_port *tport,
	struct serial_struct __user *new_arg)
{
	struct usb_serial_port *port = tport->tp_port;
	struct serial_struct new_serial;

	if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
		return -EFAULT;

	tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
A
Alan Cox 已提交
1443 1444 1445
	/* FIXME */
	if (port->port.tty)
		port->port.tty->low_latency =
L
Linus Torvalds 已提交
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
			(tport->tp_flags & ASYNC_LOW_LATENCY) ? 1 : 0;
	tport->tp_closing_wait = new_serial.closing_wait;

	return 0;
}


static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
{
	struct async_icount *icount;
	struct tty_struct *tty;
	unsigned long flags;

1459
	dbg("%s - msr 0x%02X", __func__, msr);
L
Linus Torvalds 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478

	if (msr & TI_MSR_DELTA_MASK) {
		spin_lock_irqsave(&tport->tp_lock, flags);
		icount = &tport->tp_icount;
		if (msr & TI_MSR_DELTA_CTS)
			icount->cts++;
		if (msr & TI_MSR_DELTA_DSR)
			icount->dsr++;
		if (msr & TI_MSR_DELTA_CD)
			icount->dcd++;
		if (msr & TI_MSR_DELTA_RI)
			icount->rng++;
		wake_up_interruptible(&tport->tp_msr_wait);
		spin_unlock_irqrestore(&tport->tp_lock, flags);
	}

	tport->tp_msr = msr & TI_MSR_MASK;

	/* handle CTS flow control */
A
Alan Cox 已提交
1479
	tty = tport->tp_port->port.tty;
L
Linus Torvalds 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
	if (tty && C_CRTSCTS(tty)) {
		if (msr & TI_MSR_CTS) {
			tty->hw_stopped = 0;
			tty_wakeup(tty);
		} else {
			tty->hw_stopped = 1;
		}
	}
}


static void ti_drain(struct ti_port *tport, unsigned long timeout, int flush)
{
	struct ti_device *tdev = tport->tp_tdev;
	struct usb_serial_port *port = tport->tp_port;
	wait_queue_t wait;

1497
	dbg("%s - port %d", __func__, port->number);
L
Linus Torvalds 已提交
1498

1499
	spin_lock_irq(&tport->tp_lock);
L
Linus Torvalds 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509

	/* wait for data to drain from the buffer */
	tdev->td_urb_error = 0;
	init_waitqueue_entry(&wait, current);
	add_wait_queue(&tport->tp_write_wait, &wait);
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (ti_buf_data_avail(tport->tp_write_buf) == 0
		|| timeout == 0 || signal_pending(current)
		|| tdev->td_urb_error
1510
		|| port->serial->disconnected)  /* disconnect */
L
Linus Torvalds 已提交
1511
			break;
1512
		spin_unlock_irq(&tport->tp_lock);
L
Linus Torvalds 已提交
1513
		timeout = schedule_timeout(timeout);
1514
		spin_lock_irq(&tport->tp_lock);
L
Linus Torvalds 已提交
1515 1516 1517 1518 1519 1520 1521 1522
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&tport->tp_write_wait, &wait);

	/* flush any remaining data in the buffer */
	if (flush)
		ti_buf_clear(tport->tp_write_buf);

1523
	spin_unlock_irq(&tport->tp_lock);
L
Linus Torvalds 已提交
1524

1525
	mutex_lock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1526 1527 1528 1529 1530 1531
	/* wait for data to drain from the device */
	/* wait for empty tx register, plus 20 ms */
	timeout += jiffies;
	tport->tp_lsr &= ~TI_LSR_TX_EMPTY;
	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
	&& !(tport->tp_lsr&TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1532
	&& !port->serial->disconnected) {
L
Linus Torvalds 已提交
1533 1534
		if (ti_get_lsr(tport))
			break;
1535
		mutex_unlock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1536
		msleep_interruptible(20);
1537
		mutex_lock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1538
	}
1539
	mutex_unlock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
}


static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
{
	unsigned long flags;

	spin_lock_irqsave(&tport->tp_lock, flags);

	if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
		tport->tp_read_urb_state = TI_READ_URB_STOPPING;

	spin_unlock_irqrestore(&tport->tp_lock, flags);
}


static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
{
	struct urb *urb;
	int status = 0;
	unsigned long flags;

	spin_lock_irqsave(&tport->tp_lock, flags);

	if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1565
		tport->tp_read_urb_state = TI_READ_URB_RUNNING;
L
Linus Torvalds 已提交
1566
		urb = tport->tp_port->read_urb;
1567
		spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1568 1569 1570 1571
		urb->complete = ti_bulk_in_callback;
		urb->context = tport;
		urb->dev = tport->tp_port->serial->dev;
		status = usb_submit_urb(urb, GFP_KERNEL);
1572 1573 1574
	} else  {
		tport->tp_read_urb_state = TI_READ_URB_RUNNING;
		spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
	}

	return status;
}


static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
	__u16 moduleid, __u16 value, __u8 *data, int size)
{
	int status;

	status = usb_control_msg(tdev->td_serial->dev,
		usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
		(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
		value, moduleid, data, size, 1000);

	if (status == size)
		status = 0;

	if (status > 0)
		status = -ECOMM;

	return status;
}


static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
	__u16 moduleid, __u16 value, __u8 *data, int size)
{
	int status;

	status = usb_control_msg(tdev->td_serial->dev,
		usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
		(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
		value, moduleid, data, size, 1000);

	if (status == size)
		status = 0;

	if (status > 0)
		status = -ECOMM;

	return status;
}


static int ti_write_byte(struct ti_device *tdev, unsigned long addr,
	__u8 mask, __u8 byte)
{
	int status;
	unsigned int size;
	struct ti_write_data_bytes *data;
	struct device *dev = &tdev->td_serial->dev->dev;

1629
	dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X", __func__, addr, mask, byte);
L
Linus Torvalds 已提交
1630 1631 1632 1633

	size = sizeof(struct ti_write_data_bytes) + 2;
	data = kmalloc(size, GFP_KERNEL);
	if (!data) {
1634
		dev_err(dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
		return -ENOMEM;
	}

	data->bAddrType = TI_RW_DATA_ADDR_XDATA;
	data->bDataType = TI_RW_DATA_BYTE;
	data->bDataCounter = 1;
	data->wBaseAddrHi = cpu_to_be16(addr>>16);
	data->wBaseAddrLo = cpu_to_be16(addr);
	data->bData[0] = mask;
	data->bData[1] = byte;

	status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
		(__u8 *)data, size);

	if (status < 0)
1650
		dev_err(dev, "%s - failed, %d\n", __func__, status);
L
Linus Torvalds 已提交
1651 1652 1653 1654 1655 1656

	kfree(data);

	return status;
}

A
Alan Cox 已提交
1657 1658
static int ti_do_download(struct usb_device *dev, int pipe,
						u8 *buffer, int size)
L
Linus Torvalds 已提交
1659 1660
{
	int pos;
A
Alan Cox 已提交
1661
	u8 cs = 0;
L
Linus Torvalds 已提交
1662 1663
	int done;
	struct ti_firmware_header *header;
A
Alan Cox 已提交
1664 1665 1666 1667
	int status;
	int len;
	
	for(pos = sizeof(struct ti_firmware_header); pos < size; pos++)
L
Linus Torvalds 已提交
1668 1669 1670
		cs = (__u8)(cs + buffer[pos]);

	header = (struct ti_firmware_header *)buffer;
A
Alan Cox 已提交
1671 1672
	header->wLength = cpu_to_le16((__u16)(size 
					- sizeof(struct ti_firmware_header)));
L
Linus Torvalds 已提交
1673 1674
	header->bCheckSum = cs;

1675
	dbg("%s - downloading firmware", __func__);
A
Alan Cox 已提交
1676 1677 1678 1679
	for (pos = 0; pos < size; pos += done) {
		len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
		status = usb_bulk_msg(dev, pipe, buffer + pos, len,
								&done, 1000);
L
Linus Torvalds 已提交
1680 1681 1682
		if (status)
			break;
	}
A
Alan Cox 已提交
1683 1684
	return status;
}
L
Linus Torvalds 已提交
1685

A
Alan Cox 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
static int ti_download_firmware(struct ti_device *tdev, int type)
{
	int status = -ENOMEM;
	int buffer_size;
	__u8 *buffer;
	struct usb_device *dev = tdev->td_serial->dev;
	unsigned int pipe = usb_sndbulkpipe(dev,
		tdev->td_serial->port[0]->bulk_out_endpointAddress);
	const struct firmware *fw_p;
	char buf[32];
	sprintf(buf, "ti_usb-%d.bin", type);
L
Linus Torvalds 已提交
1697

A
Alan Cox 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
	if (request_firmware(&fw_p, buf, &dev->dev)) {
		dev_err(&dev->dev, "%s - firmware not found\n", __func__);
		return -ENOENT;
	}
	if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
		dev_err(&dev->dev, "%s - firmware too large\n", __func__);
		return -ENOENT;
	}

	buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
	buffer = kmalloc(buffer_size, GFP_KERNEL);
	if (buffer) {
		memcpy(buffer, fw_p->data, fw_p->size);
		memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
		ti_do_download(dev, pipe, buffer, fw_p->size);
		kfree(buffer);
	}
	release_firmware(fw_p);
L
Linus Torvalds 已提交
1716
	if (status) {
1717
		dev_err(&dev->dev, "%s - error downloading firmware, %d\n", __func__, status);
L
Linus Torvalds 已提交
1718 1719 1720
		return status;
	}

1721
	dbg("%s - download successful", __func__);
L
Linus Torvalds 已提交
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

	return 0;
}


/* Circular Buffer Functions */

/*
 * ti_buf_alloc
 *
 * Allocate a circular buffer and all associated memory.
 */

static struct circ_buf *ti_buf_alloc(void)
{
	struct circ_buf *cb;

1739
	cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
L
Linus Torvalds 已提交
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
	if (cb == NULL)
		return NULL;

	cb->buf = kmalloc(TI_WRITE_BUF_SIZE, GFP_KERNEL);
	if (cb->buf == NULL) {
		kfree(cb);
		return NULL;
	}

	ti_buf_clear(cb);

	return cb;
}


/*
 * ti_buf_free
 *
 * Free the buffer and all associated memory.
 */

static void ti_buf_free(struct circ_buf *cb)
{
	kfree(cb->buf);
	kfree(cb);
}


/*
 * ti_buf_clear
 *
 * Clear out all data in the circular buffer.
 */

static void ti_buf_clear(struct circ_buf *cb)
{
	cb->head = cb->tail = 0;
}


/*
 * ti_buf_data_avail
 *
 * Return the number of bytes of data available in the circular
 * buffer.
 */

static int ti_buf_data_avail(struct circ_buf *cb)
{
	return CIRC_CNT(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
}


/*
 * ti_buf_space_avail
 *
 * Return the number of bytes of space available in the circular
 * buffer.
 */

static int ti_buf_space_avail(struct circ_buf *cb)
{
	return CIRC_SPACE(cb->head,cb->tail,TI_WRITE_BUF_SIZE);
}


/*
 * ti_buf_put
 *
 * Copy data data from a user buffer and put it into the circular buffer.
 * Restrict to the amount of space available.
 *
 * Return the number of bytes copied.
 */

static int ti_buf_put(struct circ_buf *cb, const char *buf, int count)
{
	int c, ret = 0;

	while (1) {
		c = CIRC_SPACE_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
		if (count < c)
			c = count;
		if (c <= 0)
			break;
		memcpy(cb->buf + cb->head, buf, c);
		cb->head = (cb->head + c) & (TI_WRITE_BUF_SIZE-1);
		buf += c;
		count -= c;
		ret += c;
	}

	return ret;
}


/*
 * ti_buf_get
 *
 * Get data from the circular buffer and copy to the given buffer.
 * Restrict to the amount of data available.
 *
 * Return the number of bytes copied.
 */

static int ti_buf_get(struct circ_buf *cb, char *buf, int count)
{
	int c, ret = 0;

	while (1) {
		c = CIRC_CNT_TO_END(cb->head, cb->tail, TI_WRITE_BUF_SIZE);
		if (count < c)
			c = count;
		if (c <= 0)
			break;
		memcpy(buf, cb->buf + cb->tail, c);
		cb->tail = (cb->tail + c) & (TI_WRITE_BUF_SIZE-1);
		buf += c;
		count -= c;
		ret += c;
	}

	return ret;
}