ti_usb_3410_5052.c 43.8 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
/* 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>.
 */

#include <linux/kernel.h>
#include <linux/errno.h>
A
Alan Cox 已提交
23
#include <linux/firmware.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31 32
#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>
33
#include <linux/kfifo.h>
34
#include <linux/mutex.h>
A
Alan Cox 已提交
35
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
36
#include <linux/usb.h>
37
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

#include "ti_usb_3410_5052.h"

/* Defines */

#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_CLOSING_WAIT	4000		/* in .01 secs */

/* supported setserial flags */
55
#define TI_SET_SERIAL_FLAGS	0
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

/* 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_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 */
	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;
81
	struct kfifo		write_fifo;
L
Linus Torvalds 已提交
82 83 84
};

struct ti_device {
85
	struct mutex		td_open_close_lock;
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94 95
	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);
96
static void ti_release(struct usb_serial *serial);
97 98
static int ti_port_probe(struct usb_serial_port *port);
static int ti_port_remove(struct usb_serial_port *port);
99
static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
100
static void ti_close(struct usb_serial_port *port);
A
Alan Cox 已提交
101
static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
A
Alan Cox 已提交
102
		const unsigned char *data, int count);
A
Alan Cox 已提交
103 104 105 106
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);
107
static int ti_ioctl(struct tty_struct *tty,
A
Alan Cox 已提交
108 109 110
		unsigned int cmd, unsigned long arg);
static void ti_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios);
111
static int ti_tiocmget(struct tty_struct *tty);
112
static int ti_tiocmset(struct tty_struct *tty,
A
Alan Cox 已提交
113
		unsigned int set, unsigned int clear);
A
Alan Cox 已提交
114
static void ti_break(struct tty_struct *tty, int break_state);
115 116 117
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 已提交
118

J
Jiri Slaby 已提交
119 120
static void ti_recv(struct usb_serial_port *port, unsigned char *data,
		int length);
L
Linus Torvalds 已提交
121 122
static void ti_send(struct ti_port *tport);
static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
123
static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
L
Linus Torvalds 已提交
124 125
static int ti_get_serial_info(struct ti_port *tport,
	struct serial_struct __user *ret_arg);
A
Alan Cox 已提交
126
static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
L
Linus Torvalds 已提交
127 128 129
	struct serial_struct __user *new_arg);
static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);

130
static void ti_drain(struct ti_port *tport, unsigned long timeout);
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138 139

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);

140 141
static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
			 unsigned long addr, __u8 mask, __u8 byte);
L
Linus Torvalds 已提交
142

143
static int ti_download_firmware(struct ti_device *tdev);
L
Linus Torvalds 已提交
144 145 146 147 148 149 150


/* Data */

/* module parameters */
static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
151
static unsigned int vendor_3410_count;
L
Linus Torvalds 已提交
152
static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
153
static unsigned int product_3410_count;
L
Linus Torvalds 已提交
154
static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
155
static unsigned int vendor_5052_count;
L
Linus Torvalds 已提交
156
static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
157
static unsigned int product_5052_count;
L
Linus Torvalds 已提交
158 159 160 161 162

/* 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 */
163
static struct usb_device_id ti_id_table_3410[15+TI_EXTRA_VID_PID_COUNT+1] = {
L
Linus Torvalds 已提交
164
	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
165
	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
166 167 168 169 170
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
171 172 173
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
174
	{ USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
175 176
	{ USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
	{ USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
177
	{ USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
178
	{ USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
L
Linus Torvalds 已提交
179 180
};

181
static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
L
Linus Torvalds 已提交
182 183 184 185 186 187
	{ 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) },
};

188
static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = {
L
Linus Torvalds 已提交
189
	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
190
	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
191 192 193 194 195
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
196 197 198
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
L
Linus Torvalds 已提交
199 200 201 202
	{ 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) },
203
	{ USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
204 205
	{ USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
	{ USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
206
	{ USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
207
	{ USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
L
Linus Torvalds 已提交
208 209 210
	{ }
};

211
static struct usb_serial_driver ti_1port_device = {
212
	.driver = {
213 214
		.owner		= THIS_MODULE,
		.name		= "ti_usb_3410_5052_1",
215
	},
216
	.description		= "TI USB 3410 1 port adapter",
L
Linus Torvalds 已提交
217 218 219
	.id_table		= ti_id_table_3410,
	.num_ports		= 1,
	.attach			= ti_startup,
220
	.release		= ti_release,
221 222
	.port_probe		= ti_port_probe,
	.port_remove		= ti_port_remove,
L
Linus Torvalds 已提交
223 224 225 226 227 228 229 230 231 232 233
	.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,
234
	.tiocmiwait		= usb_serial_generic_tiocmiwait,
235
	.get_icount		= usb_serial_generic_get_icount,
L
Linus Torvalds 已提交
236 237 238 239 240 241
	.break_ctl		= ti_break,
	.read_int_callback	= ti_interrupt_callback,
	.read_bulk_callback	= ti_bulk_in_callback,
	.write_bulk_callback	= ti_bulk_out_callback,
};

242
static struct usb_serial_driver ti_2port_device = {
243
	.driver = {
244 245
		.owner		= THIS_MODULE,
		.name		= "ti_usb_3410_5052_2",
246
	},
247
	.description		= "TI USB 5052 2 port adapter",
L
Linus Torvalds 已提交
248 249 250
	.id_table		= ti_id_table_5052,
	.num_ports		= 2,
	.attach			= ti_startup,
251
	.release		= ti_release,
252 253
	.port_probe		= ti_port_probe,
	.port_remove		= ti_port_remove,
L
Linus Torvalds 已提交
254 255 256 257 258 259 260 261 262 263 264
	.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,
265
	.tiocmiwait		= usb_serial_generic_tiocmiwait,
266
	.get_icount		= usb_serial_generic_get_icount,
L
Linus Torvalds 已提交
267 268 269 270 271 272
	.break_ctl		= ti_break,
	.read_int_callback	= ti_interrupt_callback,
	.read_bulk_callback	= ti_bulk_in_callback,
	.write_bulk_callback	= ti_bulk_out_callback,
};

273 274 275
static struct usb_serial_driver * const serial_drivers[] = {
	&ti_1port_device, &ti_2port_device, NULL
};
L
Linus Torvalds 已提交
276 277 278 279 280 281 282

/* Module */

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

283 284
MODULE_FIRMWARE("ti_3410.fw");
MODULE_FIRMWARE("ti_5052.fw");
285 286 287
MODULE_FIRMWARE("mts_cdma.fw");
MODULE_FIRMWARE("mts_gsm.fw");
MODULE_FIRMWARE("mts_edge.fw");
288 289
MODULE_FIRMWARE("mts_mt9234mu.fw");
MODULE_FIRMWARE("mts_mt9234zba.fw");
290

L
Linus Torvalds 已提交
291
module_param(closing_wait, int, S_IRUGO | S_IWUSR);
A
Alan Cox 已提交
292 293
MODULE_PARM_DESC(closing_wait,
    "Maximum wait for data to drain in close, in .01 secs, default is 4000");
L
Linus Torvalds 已提交
294 295

module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
A
Alan Cox 已提交
296 297
MODULE_PARM_DESC(vendor_3410,
		"Vendor ids for 3410 based devices, 1-5 short integers");
L
Linus Torvalds 已提交
298
module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
A
Alan Cox 已提交
299 300
MODULE_PARM_DESC(product_3410,
		"Product ids for 3410 based devices, 1-5 short integers");
L
Linus Torvalds 已提交
301
module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
A
Alan Cox 已提交
302 303
MODULE_PARM_DESC(vendor_5052,
		"Vendor ids for 5052 based devices, 1-5 short integers");
L
Linus Torvalds 已提交
304
module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
A
Alan Cox 已提交
305 306
MODULE_PARM_DESC(product_5052,
		"Product ids for 5052 based devices, 1-5 short integers");
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314

MODULE_DEVICE_TABLE(usb, ti_id_table_combined);


/* Functions */

static int __init ti_init(void)
{
315
	int i, j, c;
L
Linus Torvalds 已提交
316 317

	/* insert extra vendor and product ids */
318
	c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
319
	j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
320
	for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
L
Linus Torvalds 已提交
321 322 323
		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;
324 325 326
		ti_id_table_combined[c].idVendor = vendor_3410[i];
		ti_id_table_combined[c].idProduct = product_3410[i];
		ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
L
Linus Torvalds 已提交
327
	}
328
	j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
329
	for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
L
Linus Torvalds 已提交
330 331 332
		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;
333 334 335
		ti_id_table_combined[c].idVendor = vendor_5052[i];
		ti_id_table_combined[c].idProduct = product_5052[i];
		ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
L
Linus Torvalds 已提交
336 337
	}

338
	return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ti_id_table_combined);
L
Linus Torvalds 已提交
339 340 341 342
}

static void __exit ti_exit(void)
{
343
	usb_serial_deregister_drivers(serial_drivers);
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352 353 354 355
}

module_init(ti_init);
module_exit(ti_exit);


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

356 357 358 359 360
	dev_dbg(&dev->dev,
		"%s - product 0x%4X, num configurations %d, configuration value %d",
		__func__, le16_to_cpu(dev->descriptor.idProduct),
		dev->descriptor.bNumConfigurations,
		dev->actconfig->desc.bConfigurationValue);
L
Linus Torvalds 已提交
361 362

	/* create device structure */
363
	tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
L
Linus Torvalds 已提交
364
	if (tdev == NULL) {
365
		dev_err(&dev->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
366 367
		return -ENOMEM;
	}
368
	mutex_init(&tdev->td_open_close_lock);
L
Linus Torvalds 已提交
369 370 371 372 373 374
	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;
375 376
	dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
		tdev->td_is_3410 ? "3410" : "5052");
L
Linus Torvalds 已提交
377 378 379

	/* if we have only 1 configuration, download firmware */
	if (dev->descriptor.bNumConfigurations == 1) {
380 381 382
		status = ti_download_firmware(tdev);

		if (status != 0)
L
Linus Torvalds 已提交
383 384 385 386 387 388 389 390 391 392
			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;
A
Alan Cox 已提交
393
	}
L
Linus Torvalds 已提交
394

395
	/* the second configuration must be set */
L
Linus Torvalds 已提交
396
	if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
397 398
		status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
		status = status ? status : -ENODEV;
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409 410
		goto free_tdev;
	}

	return 0;

free_tdev:
	kfree(tdev);
	usb_set_serial_data(serial, NULL);
	return status;
}


411
static void ti_release(struct usb_serial *serial)
L
Linus Torvalds 已提交
412 413
{
	struct ti_device *tdev = usb_get_serial_data(serial);
414 415 416 417 418 419

	kfree(tdev);
}

static int ti_port_probe(struct usb_serial_port *port)
{
L
Linus Torvalds 已提交
420 421
	struct ti_port *tport;

422 423 424 425 426 427 428 429 430 431 432 433 434 435
	tport = kzalloc(sizeof(*tport), GFP_KERNEL);
	if (!tport)
		return -ENOMEM;

	spin_lock_init(&tport->tp_lock);
	if (port == port->serial->port[0])
		tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
	else
		tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
	tport->tp_closing_wait = closing_wait;
	init_waitqueue_head(&tport->tp_write_wait);
	if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE, GFP_KERNEL)) {
		kfree(tport);
		return -ENOMEM;
L
Linus Torvalds 已提交
436
	}
437 438 439
	tport->tp_port = port;
	tport->tp_tdev = usb_get_serial_data(port->serial);
	tport->tp_uart_mode = 0;	/* default is RS232 */
L
Linus Torvalds 已提交
440

441 442 443
	usb_set_serial_port_data(port, tport);

	return 0;
L
Linus Torvalds 已提交
444 445
}

446 447 448 449 450 451 452 453 454 455
static int ti_port_remove(struct usb_serial_port *port)
{
	struct ti_port *tport;

	tport = usb_get_serial_port_data(port);
	kfifo_free(&tport->write_fifo);
	kfree(tport);

	return 0;
}
L
Linus Torvalds 已提交
456

457
static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
458 459 460 461 462 463 464
{
	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;
A
Alan Cox 已提交
465 466
	__u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
			     TI_PIPE_TIMEOUT_ENABLE |
L
Linus Torvalds 已提交
467 468 469 470 471 472 473 474 475
			     (TI_TRANSFER_TIMEOUT << 2));

	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 */
476
	if (mutex_lock_interruptible(&tdev->td_open_close_lock))
L
Linus Torvalds 已提交
477 478 479 480 481 482 483 484 485
		return -ERESTARTSYS;

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

	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) {
486
		dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
L
Linus Torvalds 已提交
487 488
		urb = tdev->td_serial->port[0]->interrupt_in_urb;
		if (!urb) {
489
			dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
L
Linus Torvalds 已提交
490
			status = -EINVAL;
491
			goto release_lock;
L
Linus Torvalds 已提交
492 493 494 495
		}
		urb->context = tdev;
		status = usb_submit_urb(urb, GFP_KERNEL);
		if (status) {
496
			dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
497
			goto release_lock;
L
Linus Torvalds 已提交
498 499 500
		}
	}

A
Alan Cox 已提交
501
	if (tty)
502
		ti_set_termios(tty, port, &tty->termios);
L
Linus Torvalds 已提交
503

504
	dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__);
L
Linus Torvalds 已提交
505 506 507
	status = ti_command_out_sync(tdev, TI_OPEN_PORT,
		(__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
	if (status) {
A
Alan Cox 已提交
508
		dev_err(&port->dev, "%s - cannot send open command, %d\n",
509
			__func__, status);
L
Linus Torvalds 已提交
510 511 512
		goto unlink_int_urb;
	}

513
	dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__);
L
Linus Torvalds 已提交
514 515 516
	status = ti_command_out_sync(tdev, TI_START_PORT,
		(__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
	if (status) {
A
Alan Cox 已提交
517 518
		dev_err(&port->dev, "%s - cannot send start command, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
519 520 521
		goto unlink_int_urb;
	}

522
	dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__);
L
Linus Torvalds 已提交
523 524 525
	status = ti_command_out_sync(tdev, TI_PURGE_PORT,
		(__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
	if (status) {
A
Alan Cox 已提交
526 527
		dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
528 529 530 531 532
		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) {
A
Alan Cox 已提交
533 534
		dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
535 536 537 538 539 540 541 542
		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 已提交
543
	if (tty)
544
		ti_set_termios(tty, port, &tty->termios);
L
Linus Torvalds 已提交
545

546
	dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __func__);
L
Linus Torvalds 已提交
547 548 549
	status = ti_command_out_sync(tdev, TI_OPEN_PORT,
		(__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
	if (status) {
A
Alan Cox 已提交
550 551
		dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
							__func__, status);
L
Linus Torvalds 已提交
552 553 554
		goto unlink_int_urb;
	}

555
	dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __func__);
L
Linus Torvalds 已提交
556 557 558
	status = ti_command_out_sync(tdev, TI_START_PORT,
		(__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
	if (status) {
A
Alan Cox 已提交
559 560
		dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
							__func__, status);
L
Linus Torvalds 已提交
561 562 563 564
		goto unlink_int_urb;
	}

	/* start read urb */
565
	dev_dbg(&port->dev, "%s - start read urb\n", __func__);
L
Linus Torvalds 已提交
566 567
	urb = port->read_urb;
	if (!urb) {
568
		dev_err(&port->dev, "%s - no read urb\n", __func__);
L
Linus Torvalds 已提交
569 570 571 572 573 574 575
		status = -EINVAL;
		goto unlink_int_urb;
	}
	tport->tp_read_urb_state = TI_READ_URB_RUNNING;
	urb->context = tport;
	status = usb_submit_urb(urb, GFP_KERNEL);
	if (status) {
A
Alan Cox 已提交
576 577
		dev_err(&port->dev, "%s - submit read urb failed, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
578 579 580 581 582 583
		goto unlink_int_urb;
	}

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

584
	goto release_lock;
L
Linus Torvalds 已提交
585 586 587 588

unlink_int_urb:
	if (tdev->td_open_port_count == 0)
		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
589 590
release_lock:
	mutex_unlock(&tdev->td_open_close_lock);
591
	dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
L
Linus Torvalds 已提交
592 593 594 595
	return status;
}


596
static void ti_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
597 598 599 600 601
{
	struct ti_device *tdev;
	struct ti_port *tport;
	int port_number;
	int status;
602
	int do_unlock;
603
	unsigned long flags;
L
Linus Torvalds 已提交
604 605 606 607 608 609 610 611

	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;

612
	ti_drain(tport, (tport->tp_closing_wait*HZ)/100);
L
Linus Torvalds 已提交
613 614 615 616

	usb_kill_urb(port->read_urb);
	usb_kill_urb(port->write_urb);
	tport->tp_write_urb_in_use = 0;
617 618 619
	spin_lock_irqsave(&tport->tp_lock, flags);
	kfifo_reset_out(&tport->write_fifo);
	spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
620 621 622

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

623
	dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
L
Linus Torvalds 已提交
624 625 626
	status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
		     (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
	if (status)
A
Alan Cox 已提交
627 628 629
		dev_err(&port->dev,
			"%s - cannot send close port command, %d\n"
							, __func__, status);
L
Linus Torvalds 已提交
630

631 632
	/* if mutex_lock is interrupted, continue anyway */
	do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
L
Linus Torvalds 已提交
633 634 635 636 637 638
	--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;
	}
639 640
	if (do_unlock)
		mutex_unlock(&tdev->td_open_close_lock);
L
Linus Torvalds 已提交
641 642 643
}


A
Alan Cox 已提交
644 645
static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
			const unsigned char *data, int count)
L
Linus Torvalds 已提交
646 647 648 649
{
	struct ti_port *tport = usb_get_serial_port_data(port);

	if (count == 0) {
650
		dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
L
Linus Torvalds 已提交
651 652 653 654 655 656
		return 0;
	}

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

657 658
	count = kfifo_in_locked(&tport->write_fifo, data, count,
							&tport->tp_lock);
L
Linus Torvalds 已提交
659 660 661 662 663 664
	ti_send(tport);

	return count;
}


A
Alan Cox 已提交
665
static int ti_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
666
{
A
Alan Cox 已提交
667
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
668 669 670 671 672
	struct ti_port *tport = usb_get_serial_port_data(port);
	int room = 0;
	unsigned long flags;

	if (tport == NULL)
A
Alan Cox 已提交
673
		return 0;
A
Alan Cox 已提交
674

L
Linus Torvalds 已提交
675
	spin_lock_irqsave(&tport->tp_lock, flags);
676
	room = kfifo_avail(&tport->write_fifo);
L
Linus Torvalds 已提交
677 678
	spin_unlock_irqrestore(&tport->tp_lock, flags);

679
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
L
Linus Torvalds 已提交
680 681 682 683
	return room;
}


A
Alan Cox 已提交
684
static int ti_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
685
{
A
Alan Cox 已提交
686
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
687 688 689
	struct ti_port *tport = usb_get_serial_port_data(port);
	int chars = 0;
	unsigned long flags;
690 691
	int ret;
	u8 lsr;
L
Linus Torvalds 已提交
692 693

	if (tport == NULL)
A
Alan Cox 已提交
694
		return 0;
L
Linus Torvalds 已提交
695 696

	spin_lock_irqsave(&tport->tp_lock, flags);
697
	chars = kfifo_len(&tport->write_fifo);
L
Linus Torvalds 已提交
698 699
	spin_unlock_irqrestore(&tport->tp_lock, flags);

700 701 702 703 704 705
	if (!chars) {
		ret = ti_get_lsr(tport, &lsr);
		if (!ret && !(lsr & TI_LSR_TX_EMPTY))
			chars = 1;
	}

706
	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
L
Linus Torvalds 已提交
707 708 709 710
	return chars;
}


A
Alan Cox 已提交
711
static void ti_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
712
{
A
Alan Cox 已提交
713
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
714 715 716 717 718 719 720 721 722 723 724
	struct ti_port *tport = usb_get_serial_port_data(port);

	if (tport == NULL)
		return;

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

}


A
Alan Cox 已提交
725
static void ti_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
726
{
A
Alan Cox 已提交
727
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
728 729 730 731 732 733 734 735 736
	struct ti_port *tport = usb_get_serial_port_data(port);
	int status;

	if (tport == NULL)
		return;

	if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
		status = ti_restart_read(tport, tty);
		if (status)
A
Alan Cox 已提交
737 738
			dev_err(&port->dev, "%s - cannot restart read, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
739 740 741
	}
}

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

748
	dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd);
L
Linus Torvalds 已提交
749 750 751 752 753

	if (tport == NULL)
		return -ENODEV;

	switch (cmd) {
A
Alan Cox 已提交
754
	case TIOCGSERIAL:
755
		dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
A
Alan Cox 已提交
756 757 758
		return ti_get_serial_info(tport,
				(struct serial_struct __user *)arg);
	case TIOCSSERIAL:
759
		dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
A
Alan Cox 已提交
760 761
		return ti_set_serial_info(tty, tport,
				(struct serial_struct __user *)arg);
L
Linus Torvalds 已提交
762 763 764 765 766
	}
	return -ENOIOCTLCMD;
}


A
Alan Cox 已提交
767 768
static void ti_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
769 770 771
{
	struct ti_port *tport = usb_get_serial_port_data(port);
	struct ti_uart_config *config;
A
Alan Cox 已提交
772
	tcflag_t cflag, iflag;
L
Linus Torvalds 已提交
773 774 775 776 777
	int baud;
	int status;
	int port_number = port->number - port->serial->minor;
	unsigned int mcr;

778 779
	cflag = tty->termios.c_cflag;
	iflag = tty->termios.c_iflag;
L
Linus Torvalds 已提交
780

781 782 783
	dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
	dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
		old_termios->c_cflag, old_termios->c_iflag);
L
Linus Torvalds 已提交
784 785 786 787 788 789

	if (tport == NULL)
		return;

	config = kmalloc(sizeof(*config), GFP_KERNEL);
	if (!config) {
790
		dev_err(&port->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
791 792 793 794 795 796 797 798 799 800 801
		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) {
A
Alan Cox 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814
	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;
L
Linus Torvalds 已提交
815 816
	}

A
Alan Cox 已提交
817
	/* CMSPAR isn't supported by this driver */
818
	tty->termios.c_cflag &= ~CMSPAR;
A
Alan Cox 已提交
819

L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827 828 829
	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;
A
Alan Cox 已提交
830
		config->bParity = TI_UART_NO_PARITY;
L
Linus Torvalds 已提交
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
	}

	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 已提交
862 863
	if (!baud)
		baud = 9600;
L
Linus Torvalds 已提交
864 865 866 867 868
	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 已提交
869 870 871 872
	/* FIXME: Should calculate resulting baud here and report it back */
	if ((cflag & CBAUD) != B0)
		tty_encode_baud_rate(tty, baud, baud);

873 874 875 876 877
	dev_dbg(&port->dev,
		"%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
		__func__, baud, config->wBaudRate, config->wFlags,
		config->bDataBits, config->bParity, config->bStopBits,
		config->cXon, config->cXoff, config->bUartMode);
L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885

	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)
A
Alan Cox 已提交
886 887
		dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
					__func__, port_number, status);
L
Linus Torvalds 已提交
888 889 890 891 892 893 894 895

	/* 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)
A
Alan Cox 已提交
896 897 898
		dev_err(&port->dev,
			"%s - cannot set modem control on port %d, %d\n",
						__func__, port_number, status);
L
Linus Torvalds 已提交
899 900 901 902 903

	kfree(config);
}


904
static int ti_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
905
{
A
Alan Cox 已提交
906
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
907 908 909 910
	struct ti_port *tport = usb_get_serial_port_data(port);
	unsigned int result;
	unsigned int msr;
	unsigned int mcr;
911
	unsigned long flags;
L
Linus Torvalds 已提交
912 913 914 915

	if (tport == NULL)
		return -ENODEV;

916
	spin_lock_irqsave(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
917 918
	msr = tport->tp_msr;
	mcr = tport->tp_shadow_mcr;
919
	spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928

	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);

929
	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
L
Linus Torvalds 已提交
930 931 932 933 934

	return result;
}


935 936
static int ti_tiocmset(struct tty_struct *tty,
				unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
937
{
A
Alan Cox 已提交
938
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
939 940
	struct ti_port *tport = usb_get_serial_port_data(port);
	unsigned int mcr;
941
	unsigned long flags;
L
Linus Torvalds 已提交
942 943 944 945

	if (tport == NULL)
		return -ENODEV;

946
	spin_lock_irqsave(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
	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;
962
	spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
963 964 965 966 967

	return ti_set_mcr(tport, mcr);
}


A
Alan Cox 已提交
968
static void ti_break(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
969
{
A
Alan Cox 已提交
970
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
971 972 973
	struct ti_port *tport = usb_get_serial_port_data(port);
	int status;

974
	dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
L
Linus Torvalds 已提交
975 976 977 978

	if (tport == NULL)
		return;

979
	status = ti_write_byte(port, tport->tp_tdev,
L
Linus Torvalds 已提交
980 981 982 983
		tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
		TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);

	if (status)
984
		dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
L
Linus Torvalds 已提交
985 986 987
}


988
static void ti_interrupt_callback(struct urb *urb)
L
Linus Torvalds 已提交
989
{
990
	struct ti_device *tdev = urb->context;
L
Linus Torvalds 已提交
991 992 993 994 995 996 997 998
	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;
999 1000
	int status = urb->status;
	int retval;
L
Linus Torvalds 已提交
1001 1002
	__u8 msr;

1003
	switch (status) {
L
Linus Torvalds 已提交
1004 1005 1006 1007 1008
	case 0:
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
1009
		dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
L
Linus Torvalds 已提交
1010 1011 1012
		tdev->td_urb_error = 1;
		return;
	default:
1013
		dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
L
Linus Torvalds 已提交
1014 1015 1016 1017 1018
		tdev->td_urb_error = 1;
		goto exit;
	}

	if (length != 2) {
1019
		dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
L
Linus Torvalds 已提交
1020 1021 1022 1023
		goto exit;
	}

	if (data[0] == TI_CODE_HARDWARE_ERROR) {
1024
		dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029 1030
		goto exit;
	}

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

1031 1032
	dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
		__func__, port_number, function, data[1]);
L
Linus Torvalds 已提交
1033 1034

	if (port_number >= serial->num_ports) {
A
Alan Cox 已提交
1035 1036
		dev_err(dev, "%s - bad port number, %d\n",
						__func__, port_number);
L
Linus Torvalds 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
		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:
A
Alan Cox 已提交
1048
		dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1049
			__func__, port_number, data[1]);
L
Linus Torvalds 已提交
1050 1051 1052 1053
		break;

	case TI_CODE_MODEM_STATUS:
		msr = data[1];
1054
		dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
L
Linus Torvalds 已提交
1055 1056 1057 1058
		ti_handle_new_msr(tport, msr);
		break;

	default:
A
Alan Cox 已提交
1059 1060
		dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
							__func__, data[1]);
L
Linus Torvalds 已提交
1061 1062 1063 1064
		break;
	}

exit:
1065 1066 1067
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval)
		dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1068
			__func__, retval);
L
Linus Torvalds 已提交
1069 1070 1071
}


1072
static void ti_bulk_in_callback(struct urb *urb)
L
Linus Torvalds 已提交
1073
{
1074
	struct ti_port *tport = urb->context;
L
Linus Torvalds 已提交
1075 1076
	struct usb_serial_port *port = tport->tp_port;
	struct device *dev = &urb->dev->dev;
1077 1078
	int status = urb->status;
	int retval = 0;
L
Linus Torvalds 已提交
1079

1080
	switch (status) {
L
Linus Torvalds 已提交
1081 1082 1083 1084 1085
	case 0:
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
1086
		dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
L
Linus Torvalds 已提交
1087 1088 1089 1090
		tport->tp_tdev->td_urb_error = 1;
		wake_up_interruptible(&tport->tp_write_wait);
		return;
	default:
1091
		dev_err(dev, "%s - nonzero urb status, %d\n",
A
Alan Cox 已提交
1092
			__func__, status);
L
Linus Torvalds 已提交
1093 1094 1095 1096
		tport->tp_tdev->td_urb_error = 1;
		wake_up_interruptible(&tport->tp_write_wait);
	}

1097
	if (status == -EPIPE)
L
Linus Torvalds 已提交
1098 1099
		goto exit;

1100
	if (status) {
1101
		dev_err(dev, "%s - stopping read!\n", __func__);
L
Linus Torvalds 已提交
1102 1103 1104
		return;
	}

J
Jiri Slaby 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	if (urb->actual_length) {
		usb_serial_debug_data(dev, __func__, urb->actual_length,
				      urb->transfer_buffer);

		if (!tport->tp_is_open)
			dev_dbg(dev, "%s - port closed, dropping data\n",
				__func__);
		else
			ti_recv(port, urb->transfer_buffer, urb->actual_length);
		spin_lock(&tport->tp_lock);
1115
		port->icount.rx += urb->actual_length;
J
Jiri Slaby 已提交
1116
		spin_unlock(&tport->tp_lock);
L
Linus Torvalds 已提交
1117 1118 1119 1120 1121
	}

exit:
	/* continue to read unless stopping */
	spin_lock(&tport->tp_lock);
1122
	if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1123
		retval = usb_submit_urb(urb, GFP_ATOMIC);
1124
	else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
L
Linus Torvalds 已提交
1125
		tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1126

L
Linus Torvalds 已提交
1127
	spin_unlock(&tport->tp_lock);
1128 1129
	if (retval)
		dev_err(dev, "%s - resubmit read urb failed, %d\n",
1130
			__func__, retval);
L
Linus Torvalds 已提交
1131 1132 1133
}


1134
static void ti_bulk_out_callback(struct urb *urb)
L
Linus Torvalds 已提交
1135
{
1136
	struct ti_port *tport = urb->context;
L
Linus Torvalds 已提交
1137
	struct usb_serial_port *port = tport->tp_port;
1138
	int status = urb->status;
L
Linus Torvalds 已提交
1139 1140 1141

	tport->tp_write_urb_in_use = 0;

1142
	switch (status) {
L
Linus Torvalds 已提交
1143 1144 1145 1146 1147
	case 0:
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
1148
		dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
L
Linus Torvalds 已提交
1149 1150 1151 1152
		tport->tp_tdev->td_urb_error = 1;
		wake_up_interruptible(&tport->tp_write_wait);
		return;
	default:
1153
		dev_err_console(port, "%s - nonzero urb status, %d\n",
1154
			__func__, status);
L
Linus Torvalds 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163
		tport->tp_tdev->td_urb_error = 1;
		wake_up_interruptible(&tport->tp_write_wait);
	}

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


J
Jiri Slaby 已提交
1164 1165
static void ti_recv(struct usb_serial_port *port, unsigned char *data,
		int length)
L
Linus Torvalds 已提交
1166 1167 1168 1169
{
	int cnt;

	do {
J
Jiri Slaby 已提交
1170
		cnt = tty_insert_flip_string(&port->port, data, length);
A
Alan Cox 已提交
1171
		if (cnt < length) {
J
Jiri Slaby 已提交
1172
			dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
A
Alan Cox 已提交
1173 1174
						__func__, length - cnt);
			if (cnt == 0)
A
Alan Cox 已提交
1175
				break;
L
Linus Torvalds 已提交
1176
		}
J
Jiri Slaby 已提交
1177
		tty_flip_buffer_push(&port->port);
L
Linus Torvalds 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		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 已提交
1188
	struct tty_struct *tty = tty_port_tty_get(&port->port);	/* FIXME */
L
Linus Torvalds 已提交
1189 1190 1191 1192
	unsigned long flags;

	spin_lock_irqsave(&tport->tp_lock, flags);

A
Alan Cox 已提交
1193 1194
	if (tport->tp_write_urb_in_use)
		goto unlock;
L
Linus Torvalds 已提交
1195

1196
	count = kfifo_out(&tport->write_fifo,
L
Linus Torvalds 已提交
1197 1198 1199
				port->write_urb->transfer_buffer,
				port->bulk_out_size);

A
Alan Cox 已提交
1200 1201
	if (count == 0)
		goto unlock;
L
Linus Torvalds 已提交
1202 1203 1204 1205 1206

	tport->tp_write_urb_in_use = 1;

	spin_unlock_irqrestore(&tport->tp_lock, flags);

1207 1208
	usb_serial_debug_data(&port->dev, __func__, count,
			      port->write_urb->transfer_buffer);
L
Linus Torvalds 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217

	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) {
1218
		dev_err_console(port, "%s - submit write urb failed, %d\n",
A
Alan Cox 已提交
1219 1220
							__func__, result);
		tport->tp_write_urb_in_use = 0;
L
Linus Torvalds 已提交
1221 1222 1223
		/* TODO: reschedule ti_send */
	} else {
		spin_lock_irqsave(&tport->tp_lock, flags);
1224
		port->icount.tx += count;
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229 1230
		spin_unlock_irqrestore(&tport->tp_lock, flags);
	}

	/* more room in the buffer for new writes, wakeup */
	if (tty)
		tty_wakeup(tty);
A
Alan Cox 已提交
1231
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1232
	wake_up_interruptible(&tport->tp_write_wait);
A
Alan Cox 已提交
1233 1234 1235 1236 1237
	return;
unlock:
	spin_unlock_irqrestore(&tport->tp_lock, flags);
	tty_kref_put(tty);
	return;
L
Linus Torvalds 已提交
1238 1239 1240 1241 1242
}


static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
{
1243
	unsigned long flags;
L
Linus Torvalds 已提交
1244 1245
	int status;

1246
	status = ti_write_byte(tport->tp_port, tport->tp_tdev,
L
Linus Torvalds 已提交
1247 1248 1249
		tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
		TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);

1250
	spin_lock_irqsave(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1251 1252
	if (!status)
		tport->tp_shadow_mcr = mcr;
1253
	spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1254 1255 1256 1257 1258

	return status;
}


1259
static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
L
Linus Torvalds 已提交
1260
{
A
Alan Cox 已提交
1261
	int size, status;
L
Linus Torvalds 已提交
1262 1263 1264 1265 1266 1267 1268 1269
	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;

	size = sizeof(struct ti_port_status);
	data = kmalloc(size, GFP_KERNEL);
	if (!data) {
1270
		dev_err(&port->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
1271 1272 1273 1274 1275 1276
		return -ENOMEM;
	}

	status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
		(__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
	if (status) {
A
Alan Cox 已提交
1277 1278 1279
		dev_err(&port->dev,
			"%s - get port status command failed, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1280 1281 1282
		goto free_data;
	}

1283
	dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
L
Linus Torvalds 已提交
1284

1285
	*lsr = data->bLSR;
L
Linus Torvalds 已提交
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318

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;
}


A
Alan Cox 已提交
1319
static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
L
Linus Torvalds 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
	struct serial_struct __user *new_arg)
{
	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;
	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;

1340
	dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
L
Linus Torvalds 已提交
1341 1342 1343

	if (msr & TI_MSR_DELTA_MASK) {
		spin_lock_irqsave(&tport->tp_lock, flags);
1344
		icount = &tport->tp_port->icount;
L
Linus Torvalds 已提交
1345 1346 1347 1348 1349 1350 1351 1352
		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++;
1353
		wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
L
Linus Torvalds 已提交
1354 1355 1356 1357 1358 1359
		spin_unlock_irqrestore(&tport->tp_lock, flags);
	}

	tport->tp_msr = msr & TI_MSR_MASK;

	/* handle CTS flow control */
A
Alan Cox 已提交
1360
	tty = tty_port_tty_get(&tport->tp_port->port);
L
Linus Torvalds 已提交
1361 1362 1363 1364 1365 1366 1367 1368
	if (tty && C_CRTSCTS(tty)) {
		if (msr & TI_MSR_CTS) {
			tty->hw_stopped = 0;
			tty_wakeup(tty);
		} else {
			tty->hw_stopped = 1;
		}
	}
A
Alan Cox 已提交
1369
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1370 1371 1372
}


1373
static void ti_drain(struct ti_port *tport, unsigned long timeout)
L
Linus Torvalds 已提交
1374 1375 1376 1377
{
	struct ti_device *tdev = tport->tp_tdev;
	struct usb_serial_port *port = tport->tp_port;
	wait_queue_t wait;
1378
	u8 lsr;
L
Linus Torvalds 已提交
1379

1380
	spin_lock_irq(&tport->tp_lock);
L
Linus Torvalds 已提交
1381 1382 1383 1384 1385 1386 1387

	/* 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);
1388
		if (kfifo_len(&tport->write_fifo) == 0
L
Linus Torvalds 已提交
1389 1390
		|| timeout == 0 || signal_pending(current)
		|| tdev->td_urb_error
1391
		|| port->serial->disconnected)  /* disconnect */
L
Linus Torvalds 已提交
1392
			break;
1393
		spin_unlock_irq(&tport->tp_lock);
L
Linus Torvalds 已提交
1394
		timeout = schedule_timeout(timeout);
1395
		spin_lock_irq(&tport->tp_lock);
L
Linus Torvalds 已提交
1396 1397 1398
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&tport->tp_write_wait, &wait);
1399
	spin_unlock_irq(&tport->tp_lock);
L
Linus Torvalds 已提交
1400

1401
	mutex_lock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1402 1403 1404
	/* wait for data to drain from the device */
	/* wait for empty tx register, plus 20 ms */
	timeout += jiffies;
1405
	lsr = 0;
L
Linus Torvalds 已提交
1406
	while ((long)(jiffies - timeout) < 0 && !signal_pending(current)
1407
	&& !(lsr & TI_LSR_TX_EMPTY) && !tdev->td_urb_error
1408
	&& !port->serial->disconnected) {
1409
		if (ti_get_lsr(tport, &lsr))
L
Linus Torvalds 已提交
1410
			break;
1411
		mutex_unlock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1412
		msleep_interruptible(20);
1413
		mutex_lock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1414
	}
1415
	mutex_unlock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
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
}


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) {
1441
		tport->tp_read_urb_state = TI_READ_URB_RUNNING;
L
Linus Torvalds 已提交
1442
		urb = tport->tp_port->read_urb;
1443
		spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1444 1445
		urb->context = tport;
		status = usb_submit_urb(urb, GFP_KERNEL);
1446 1447 1448
	} else  {
		tport->tp_read_urb_state = TI_READ_URB_RUNNING;
		spin_unlock_irqrestore(&tport->tp_lock, flags);
L
Linus Torvalds 已提交
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
	}

	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;
}


1495 1496 1497
static int ti_write_byte(struct usb_serial_port *port,
			struct ti_device *tdev, unsigned long addr,
			__u8 mask, __u8 byte)
L
Linus Torvalds 已提交
1498 1499 1500 1501 1502
{
	int status;
	unsigned int size;
	struct ti_write_data_bytes *data;

1503 1504
	dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
		addr, mask, byte);
L
Linus Torvalds 已提交
1505 1506 1507 1508

	size = sizeof(struct ti_write_data_bytes) + 2;
	data = kmalloc(size, GFP_KERNEL);
	if (!data) {
1509
		dev_err(&port->dev, "%s - out of memory\n", __func__);
L
Linus Torvalds 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
		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)
1525
		dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
L
Linus Torvalds 已提交
1526 1527 1528 1529 1530 1531

	kfree(data);

	return status;
}

A
Alan Cox 已提交
1532 1533
static int ti_do_download(struct usb_device *dev, int pipe,
						u8 *buffer, int size)
L
Linus Torvalds 已提交
1534 1535
{
	int pos;
A
Alan Cox 已提交
1536
	u8 cs = 0;
L
Linus Torvalds 已提交
1537 1538
	int done;
	struct ti_firmware_header *header;
1539
	int status = 0;
A
Alan Cox 已提交
1540
	int len;
A
Alan Cox 已提交
1541 1542

	for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
L
Linus Torvalds 已提交
1543 1544 1545
		cs = (__u8)(cs + buffer[pos]);

	header = (struct ti_firmware_header *)buffer;
A
Alan Cox 已提交
1546
	header->wLength = cpu_to_le16((__u16)(size
A
Alan Cox 已提交
1547
					- sizeof(struct ti_firmware_header)));
L
Linus Torvalds 已提交
1548 1549
	header->bCheckSum = cs;

1550
	dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
A
Alan Cox 已提交
1551 1552 1553 1554
	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 已提交
1555 1556 1557
		if (status)
			break;
	}
A
Alan Cox 已提交
1558 1559
	return status;
}
L
Linus Torvalds 已提交
1560

1561
static int ti_download_firmware(struct ti_device *tdev)
A
Alan Cox 已提交
1562
{
1563
	int status;
A
Alan Cox 已提交
1564 1565 1566 1567 1568 1569 1570
	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];
L
Linus Torvalds 已提交
1571

1572 1573 1574
	/* try ID specific firmware first, then try generic firmware */
	sprintf(buf, "ti_usb-v%04x-p%04x.fw", dev->descriptor.idVendor,
	    dev->descriptor.idProduct);
1575 1576 1577
	status = request_firmware(&fw_p, buf, &dev->dev);

	if (status != 0) {
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
		buf[0] = '\0';
		if (dev->descriptor.idVendor == MTS_VENDOR_ID) {
			switch (dev->descriptor.idProduct) {
			case MTS_CDMA_PRODUCT_ID:
				strcpy(buf, "mts_cdma.fw");
				break;
			case MTS_GSM_PRODUCT_ID:
				strcpy(buf, "mts_gsm.fw");
				break;
			case MTS_EDGE_PRODUCT_ID:
				strcpy(buf, "mts_edge.fw");
				break;
1590 1591 1592 1593 1594 1595 1596 1597 1598
			case MTS_MT9234MU_PRODUCT_ID:
				strcpy(buf, "mts_mt9234mu.fw");
				break;
			case MTS_MT9234ZBA_PRODUCT_ID:
				strcpy(buf, "mts_mt9234zba.fw");
				break;
			case MTS_MT9234ZBAOLD_PRODUCT_ID:
				strcpy(buf, "mts_mt9234zba.fw");
				break;			}
1599 1600 1601 1602 1603 1604 1605
		}
		if (buf[0] == '\0') {
			if (tdev->td_is_3410)
				strcpy(buf, "ti_3410.fw");
			else
				strcpy(buf, "ti_5052.fw");
		}
1606 1607 1608
		status = request_firmware(&fw_p, buf, &dev->dev);
	}
	if (status) {
A
Alan Cox 已提交
1609 1610 1611 1612
		dev_err(&dev->dev, "%s - firmware not found\n", __func__);
		return -ENOENT;
	}
	if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1613
		dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1614
		release_firmware(fw_p);
A
Alan Cox 已提交
1615 1616 1617 1618 1619 1620 1621 1622
		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);
1623
		status = ti_do_download(dev, pipe, buffer, fw_p->size);
A
Alan Cox 已提交
1624
		kfree(buffer);
1625
	} else {
1626
		dev_dbg(&dev->dev, "%s ENOMEM\n", __func__);
1627
		status = -ENOMEM;
A
Alan Cox 已提交
1628 1629
	}
	release_firmware(fw_p);
L
Linus Torvalds 已提交
1630
	if (status) {
A
Alan Cox 已提交
1631 1632
		dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
							__func__, status);
L
Linus Torvalds 已提交
1633 1634 1635
		return status;
	}

1636
	dev_dbg(&dev->dev, "%s - download successful\n", __func__);
L
Linus Torvalds 已提交
1637 1638 1639

	return 0;
}