1 /* vi: ts=8 sw=8
2  *
3  * TI 3410/5052 USB Serial Driver
4  *
5  * Copyright (C) 2004 Texas Instruments
6  *
7  * This driver is based on the Linux io_ti driver, which is
8  *   Copyright (C) 2000-2002 Inside Out Networks
9  *   Copyright (C) 2001-2002 Greg Kroah-Hartman
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * For questions or problems with this driver, contact Texas Instruments
17  * technical support, or Al Borchers <alborchers@steinerpoint.com>, or
18  * Peter Berger <pberger@brimson.com>.
19  */
20 
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/firmware.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h>
29 #include <linux/module.h>
30 #include <linux/spinlock.h>
31 #include <linux/ioctl.h>
32 #include <linux/serial.h>
33 #include <linux/kfifo.h>
34 #include <linux/mutex.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
38 
39 #include "ti_usb_3410_5052.h"
40 
41 /* Defines */
42 
43 #define TI_DRIVER_AUTHOR	"Al Borchers <alborchers@steinerpoint.com>"
44 #define TI_DRIVER_DESC		"TI USB 3410/5052 Serial Driver"
45 
46 #define TI_FIRMWARE_BUF_SIZE	16284
47 
48 #define TI_WRITE_BUF_SIZE	1024
49 
50 #define TI_TRANSFER_TIMEOUT	2
51 
52 #define TI_DEFAULT_CLOSING_WAIT	4000		/* in .01 secs */
53 
54 /* supported setserial flags */
55 #define TI_SET_SERIAL_FLAGS	0
56 
57 /* read urb states */
58 #define TI_READ_URB_RUNNING	0
59 #define TI_READ_URB_STOPPING	1
60 #define TI_READ_URB_STOPPED	2
61 
62 #define TI_EXTRA_VID_PID_COUNT	5
63 
64 
65 /* Structures */
66 
67 struct ti_port {
68 	int			tp_is_open;
69 	__u8			tp_msr;
70 	__u8			tp_shadow_mcr;
71 	__u8			tp_uart_mode;	/* 232 or 485 modes */
72 	unsigned int		tp_uart_base_addr;
73 	int			tp_flags;
74 	wait_queue_head_t	tp_write_wait;
75 	struct ti_device	*tp_tdev;
76 	struct usb_serial_port	*tp_port;
77 	spinlock_t		tp_lock;
78 	int			tp_read_urb_state;
79 	int			tp_write_urb_in_use;
80 	struct kfifo		write_fifo;
81 };
82 
83 struct ti_device {
84 	struct mutex		td_open_close_lock;
85 	int			td_open_port_count;
86 	struct usb_serial	*td_serial;
87 	int			td_is_3410;
88 	int			td_urb_error;
89 };
90 
91 
92 /* Function Declarations */
93 
94 static int ti_startup(struct usb_serial *serial);
95 static void ti_release(struct usb_serial *serial);
96 static int ti_port_probe(struct usb_serial_port *port);
97 static int ti_port_remove(struct usb_serial_port *port);
98 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port);
99 static void ti_close(struct usb_serial_port *port);
100 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
101 		const unsigned char *data, int count);
102 static int ti_write_room(struct tty_struct *tty);
103 static int ti_chars_in_buffer(struct tty_struct *tty);
104 static bool ti_tx_empty(struct usb_serial_port *port);
105 static void ti_throttle(struct tty_struct *tty);
106 static void ti_unthrottle(struct tty_struct *tty);
107 static int ti_ioctl(struct tty_struct *tty,
108 		unsigned int cmd, unsigned long arg);
109 static void ti_set_termios(struct tty_struct *tty,
110 		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,
113 		unsigned int set, unsigned int clear);
114 static void ti_break(struct tty_struct *tty, int break_state);
115 static void ti_interrupt_callback(struct urb *urb);
116 static void ti_bulk_in_callback(struct urb *urb);
117 static void ti_bulk_out_callback(struct urb *urb);
118 
119 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
120 		int length);
121 static void ti_send(struct ti_port *tport);
122 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr);
123 static int ti_get_lsr(struct ti_port *tport, u8 *lsr);
124 static int ti_get_serial_info(struct ti_port *tport,
125 	struct serial_struct __user *ret_arg);
126 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
127 	struct serial_struct __user *new_arg);
128 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr);
129 
130 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty);
131 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty);
132 
133 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
134 	__u16 moduleid, __u16 value, __u8 *data, int size);
135 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
136 	__u16 moduleid, __u16 value, __u8 *data, int size);
137 
138 static int ti_write_byte(struct usb_serial_port *port, struct ti_device *tdev,
139 			 unsigned long addr, __u8 mask, __u8 byte);
140 
141 static int ti_download_firmware(struct ti_device *tdev);
142 
143 
144 /* Data */
145 
146 /* module parameters */
147 static int closing_wait = TI_DEFAULT_CLOSING_WAIT;
148 static ushort vendor_3410[TI_EXTRA_VID_PID_COUNT];
149 static unsigned int vendor_3410_count;
150 static ushort product_3410[TI_EXTRA_VID_PID_COUNT];
151 static unsigned int product_3410_count;
152 static ushort vendor_5052[TI_EXTRA_VID_PID_COUNT];
153 static unsigned int vendor_5052_count;
154 static ushort product_5052[TI_EXTRA_VID_PID_COUNT];
155 static unsigned int product_5052_count;
156 
157 /* supported devices */
158 /* the array dimension is the number of default entries plus */
159 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
160 /* null entry */
161 static struct usb_device_id ti_id_table_3410[15+TI_EXTRA_VID_PID_COUNT+1] = {
162 	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
163 	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
164 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
165 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
166 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
167 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
168 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
169 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
170 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
171 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
172 	{ USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
173 	{ USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
174 	{ USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
175 	{ USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STEREO_PLUG_ID) },
176 	{ USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_STRIP_PORT_ID) },
177 	{ USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
178 };
179 
180 static struct usb_device_id ti_id_table_5052[5+TI_EXTRA_VID_PID_COUNT+1] = {
181 	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
182 	{ USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
183 	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
184 	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
185 };
186 
187 static struct usb_device_id ti_id_table_combined[19+2*TI_EXTRA_VID_PID_COUNT+1] = {
188 	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_PRODUCT_ID) },
189 	{ USB_DEVICE(TI_VENDOR_ID, TI_3410_EZ430_ID) },
190 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_NO_FW_PRODUCT_ID) },
191 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_NO_FW_PRODUCT_ID) },
192 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_CDMA_PRODUCT_ID) },
193 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_GSM_PRODUCT_ID) },
194 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_EDGE_PRODUCT_ID) },
195 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234MU_PRODUCT_ID) },
196 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBA_PRODUCT_ID) },
197 	{ USB_DEVICE(MTS_VENDOR_ID, MTS_MT9234ZBAOLD_PRODUCT_ID) },
198 	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_BOOT_PRODUCT_ID) },
199 	{ USB_DEVICE(TI_VENDOR_ID, TI_5152_BOOT_PRODUCT_ID) },
200 	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_EEPROM_PRODUCT_ID) },
201 	{ USB_DEVICE(TI_VENDOR_ID, TI_5052_FIRMWARE_PRODUCT_ID) },
202 	{ USB_DEVICE(IBM_VENDOR_ID, IBM_4543_PRODUCT_ID) },
203 	{ USB_DEVICE(IBM_VENDOR_ID, IBM_454B_PRODUCT_ID) },
204 	{ USB_DEVICE(IBM_VENDOR_ID, IBM_454C_PRODUCT_ID) },
205 	{ USB_DEVICE(ABBOTT_VENDOR_ID, ABBOTT_PRODUCT_ID) },
206 	{ USB_DEVICE(TI_VENDOR_ID, FRI2_PRODUCT_ID) },
207 	{ }
208 };
209 
210 static struct usb_serial_driver ti_1port_device = {
211 	.driver = {
212 		.owner		= THIS_MODULE,
213 		.name		= "ti_usb_3410_5052_1",
214 	},
215 	.description		= "TI USB 3410 1 port adapter",
216 	.id_table		= ti_id_table_3410,
217 	.num_ports		= 1,
218 	.attach			= ti_startup,
219 	.release		= ti_release,
220 	.port_probe		= ti_port_probe,
221 	.port_remove		= ti_port_remove,
222 	.open			= ti_open,
223 	.close			= ti_close,
224 	.write			= ti_write,
225 	.write_room		= ti_write_room,
226 	.chars_in_buffer	= ti_chars_in_buffer,
227 	.tx_empty		= ti_tx_empty,
228 	.throttle		= ti_throttle,
229 	.unthrottle		= ti_unthrottle,
230 	.ioctl			= ti_ioctl,
231 	.set_termios		= ti_set_termios,
232 	.tiocmget		= ti_tiocmget,
233 	.tiocmset		= ti_tiocmset,
234 	.tiocmiwait		= usb_serial_generic_tiocmiwait,
235 	.get_icount		= usb_serial_generic_get_icount,
236 	.break_ctl		= ti_break,
237 	.read_int_callback	= ti_interrupt_callback,
238 	.read_bulk_callback	= ti_bulk_in_callback,
239 	.write_bulk_callback	= ti_bulk_out_callback,
240 };
241 
242 static struct usb_serial_driver ti_2port_device = {
243 	.driver = {
244 		.owner		= THIS_MODULE,
245 		.name		= "ti_usb_3410_5052_2",
246 	},
247 	.description		= "TI USB 5052 2 port adapter",
248 	.id_table		= ti_id_table_5052,
249 	.num_ports		= 2,
250 	.attach			= ti_startup,
251 	.release		= ti_release,
252 	.port_probe		= ti_port_probe,
253 	.port_remove		= ti_port_remove,
254 	.open			= ti_open,
255 	.close			= ti_close,
256 	.write			= ti_write,
257 	.write_room		= ti_write_room,
258 	.chars_in_buffer	= ti_chars_in_buffer,
259 	.tx_empty		= ti_tx_empty,
260 	.throttle		= ti_throttle,
261 	.unthrottle		= ti_unthrottle,
262 	.ioctl			= ti_ioctl,
263 	.set_termios		= ti_set_termios,
264 	.tiocmget		= ti_tiocmget,
265 	.tiocmset		= ti_tiocmset,
266 	.tiocmiwait		= usb_serial_generic_tiocmiwait,
267 	.get_icount		= usb_serial_generic_get_icount,
268 	.break_ctl		= ti_break,
269 	.read_int_callback	= ti_interrupt_callback,
270 	.read_bulk_callback	= ti_bulk_in_callback,
271 	.write_bulk_callback	= ti_bulk_out_callback,
272 };
273 
274 static struct usb_serial_driver * const serial_drivers[] = {
275 	&ti_1port_device, &ti_2port_device, NULL
276 };
277 
278 /* Module */
279 
280 MODULE_AUTHOR(TI_DRIVER_AUTHOR);
281 MODULE_DESCRIPTION(TI_DRIVER_DESC);
282 MODULE_LICENSE("GPL");
283 
284 MODULE_FIRMWARE("ti_3410.fw");
285 MODULE_FIRMWARE("ti_5052.fw");
286 MODULE_FIRMWARE("mts_cdma.fw");
287 MODULE_FIRMWARE("mts_gsm.fw");
288 MODULE_FIRMWARE("mts_edge.fw");
289 MODULE_FIRMWARE("mts_mt9234mu.fw");
290 MODULE_FIRMWARE("mts_mt9234zba.fw");
291 
292 module_param(closing_wait, int, S_IRUGO | S_IWUSR);
293 MODULE_PARM_DESC(closing_wait,
294     "Maximum wait for data to drain in close, in .01 secs, default is 4000");
295 
296 module_param_array(vendor_3410, ushort, &vendor_3410_count, S_IRUGO);
297 MODULE_PARM_DESC(vendor_3410,
298 		"Vendor ids for 3410 based devices, 1-5 short integers");
299 module_param_array(product_3410, ushort, &product_3410_count, S_IRUGO);
300 MODULE_PARM_DESC(product_3410,
301 		"Product ids for 3410 based devices, 1-5 short integers");
302 module_param_array(vendor_5052, ushort, &vendor_5052_count, S_IRUGO);
303 MODULE_PARM_DESC(vendor_5052,
304 		"Vendor ids for 5052 based devices, 1-5 short integers");
305 module_param_array(product_5052, ushort, &product_5052_count, S_IRUGO);
306 MODULE_PARM_DESC(product_5052,
307 		"Product ids for 5052 based devices, 1-5 short integers");
308 
309 MODULE_DEVICE_TABLE(usb, ti_id_table_combined);
310 
311 
312 /* Functions */
313 
314 static int __init ti_init(void)
315 {
316 	int i, j, c;
317 
318 	/* insert extra vendor and product ids */
319 	c = ARRAY_SIZE(ti_id_table_combined) - 2 * TI_EXTRA_VID_PID_COUNT - 1;
320 	j = ARRAY_SIZE(ti_id_table_3410) - TI_EXTRA_VID_PID_COUNT - 1;
321 	for (i = 0; i < min(vendor_3410_count, product_3410_count); i++, j++, c++) {
322 		ti_id_table_3410[j].idVendor = vendor_3410[i];
323 		ti_id_table_3410[j].idProduct = product_3410[i];
324 		ti_id_table_3410[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
325 		ti_id_table_combined[c].idVendor = vendor_3410[i];
326 		ti_id_table_combined[c].idProduct = product_3410[i];
327 		ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
328 	}
329 	j = ARRAY_SIZE(ti_id_table_5052) - TI_EXTRA_VID_PID_COUNT - 1;
330 	for (i = 0; i < min(vendor_5052_count, product_5052_count); i++, j++, c++) {
331 		ti_id_table_5052[j].idVendor = vendor_5052[i];
332 		ti_id_table_5052[j].idProduct = product_5052[i];
333 		ti_id_table_5052[j].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
334 		ti_id_table_combined[c].idVendor = vendor_5052[i];
335 		ti_id_table_combined[c].idProduct = product_5052[i];
336 		ti_id_table_combined[c].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
337 	}
338 
339 	return usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME, ti_id_table_combined);
340 }
341 
342 static void __exit ti_exit(void)
343 {
344 	usb_serial_deregister_drivers(serial_drivers);
345 }
346 
347 module_init(ti_init);
348 module_exit(ti_exit);
349 
350 
351 static int ti_startup(struct usb_serial *serial)
352 {
353 	struct ti_device *tdev;
354 	struct usb_device *dev = serial->dev;
355 	int status;
356 
357 	dev_dbg(&dev->dev,
358 		"%s - product 0x%4X, num configurations %d, configuration value %d",
359 		__func__, le16_to_cpu(dev->descriptor.idProduct),
360 		dev->descriptor.bNumConfigurations,
361 		dev->actconfig->desc.bConfigurationValue);
362 
363 	/* create device structure */
364 	tdev = kzalloc(sizeof(struct ti_device), GFP_KERNEL);
365 	if (tdev == NULL) {
366 		dev_err(&dev->dev, "%s - out of memory\n", __func__);
367 		return -ENOMEM;
368 	}
369 	mutex_init(&tdev->td_open_close_lock);
370 	tdev->td_serial = serial;
371 	usb_set_serial_data(serial, tdev);
372 
373 	/* determine device type */
374 	if (serial->type == &ti_1port_device)
375 		tdev->td_is_3410 = 1;
376 	dev_dbg(&dev->dev, "%s - device type is %s\n", __func__,
377 		tdev->td_is_3410 ? "3410" : "5052");
378 
379 	/* if we have only 1 configuration, download firmware */
380 	if (dev->descriptor.bNumConfigurations == 1) {
381 		status = ti_download_firmware(tdev);
382 
383 		if (status != 0)
384 			goto free_tdev;
385 
386 		/* 3410 must be reset, 5052 resets itself */
387 		if (tdev->td_is_3410) {
388 			msleep_interruptible(100);
389 			usb_reset_device(dev);
390 		}
391 
392 		status = -ENODEV;
393 		goto free_tdev;
394 	}
395 
396 	/* the second configuration must be set */
397 	if (dev->actconfig->desc.bConfigurationValue == TI_BOOT_CONFIG) {
398 		status = usb_driver_set_configuration(dev, TI_ACTIVE_CONFIG);
399 		status = status ? status : -ENODEV;
400 		goto free_tdev;
401 	}
402 
403 	return 0;
404 
405 free_tdev:
406 	kfree(tdev);
407 	usb_set_serial_data(serial, NULL);
408 	return status;
409 }
410 
411 
412 static void ti_release(struct usb_serial *serial)
413 {
414 	struct ti_device *tdev = usb_get_serial_data(serial);
415 
416 	kfree(tdev);
417 }
418 
419 static int ti_port_probe(struct usb_serial_port *port)
420 {
421 	struct ti_port *tport;
422 
423 	tport = kzalloc(sizeof(*tport), GFP_KERNEL);
424 	if (!tport)
425 		return -ENOMEM;
426 
427 	spin_lock_init(&tport->tp_lock);
428 	if (port == port->serial->port[0])
429 		tport->tp_uart_base_addr = TI_UART1_BASE_ADDR;
430 	else
431 		tport->tp_uart_base_addr = TI_UART2_BASE_ADDR;
432 	port->port.closing_wait = msecs_to_jiffies(10 * closing_wait);
433 	init_waitqueue_head(&tport->tp_write_wait);
434 	if (kfifo_alloc(&tport->write_fifo, TI_WRITE_BUF_SIZE, GFP_KERNEL)) {
435 		kfree(tport);
436 		return -ENOMEM;
437 	}
438 	tport->tp_port = port;
439 	tport->tp_tdev = usb_get_serial_data(port->serial);
440 	tport->tp_uart_mode = 0;	/* default is RS232 */
441 
442 	usb_set_serial_port_data(port, tport);
443 
444 	return 0;
445 }
446 
447 static int ti_port_remove(struct usb_serial_port *port)
448 {
449 	struct ti_port *tport;
450 
451 	tport = usb_get_serial_port_data(port);
452 	kfifo_free(&tport->write_fifo);
453 	kfree(tport);
454 
455 	return 0;
456 }
457 
458 static int ti_open(struct tty_struct *tty, struct usb_serial_port *port)
459 {
460 	struct ti_port *tport = usb_get_serial_port_data(port);
461 	struct ti_device *tdev;
462 	struct usb_device *dev;
463 	struct urb *urb;
464 	int port_number;
465 	int status;
466 	__u16 open_settings = (__u8)(TI_PIPE_MODE_CONTINOUS |
467 			     TI_PIPE_TIMEOUT_ENABLE |
468 			     (TI_TRANSFER_TIMEOUT << 2));
469 
470 	if (tport == NULL)
471 		return -ENODEV;
472 
473 	dev = port->serial->dev;
474 	tdev = tport->tp_tdev;
475 
476 	/* only one open on any port on a device at a time */
477 	if (mutex_lock_interruptible(&tdev->td_open_close_lock))
478 		return -ERESTARTSYS;
479 
480 	port_number = port->port_number;
481 
482 	tport->tp_msr = 0;
483 	tport->tp_shadow_mcr |= (TI_MCR_RTS | TI_MCR_DTR);
484 
485 	/* start interrupt urb the first time a port is opened on this device */
486 	if (tdev->td_open_port_count == 0) {
487 		dev_dbg(&port->dev, "%s - start interrupt in urb\n", __func__);
488 		urb = tdev->td_serial->port[0]->interrupt_in_urb;
489 		if (!urb) {
490 			dev_err(&port->dev, "%s - no interrupt urb\n", __func__);
491 			status = -EINVAL;
492 			goto release_lock;
493 		}
494 		urb->context = tdev;
495 		status = usb_submit_urb(urb, GFP_KERNEL);
496 		if (status) {
497 			dev_err(&port->dev, "%s - submit interrupt urb failed, %d\n", __func__, status);
498 			goto release_lock;
499 		}
500 	}
501 
502 	if (tty)
503 		ti_set_termios(tty, port, &tty->termios);
504 
505 	dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT\n", __func__);
506 	status = ti_command_out_sync(tdev, TI_OPEN_PORT,
507 		(__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
508 	if (status) {
509 		dev_err(&port->dev, "%s - cannot send open command, %d\n",
510 			__func__, status);
511 		goto unlink_int_urb;
512 	}
513 
514 	dev_dbg(&port->dev, "%s - sending TI_START_PORT\n", __func__);
515 	status = ti_command_out_sync(tdev, TI_START_PORT,
516 		(__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
517 	if (status) {
518 		dev_err(&port->dev, "%s - cannot send start command, %d\n",
519 							__func__, status);
520 		goto unlink_int_urb;
521 	}
522 
523 	dev_dbg(&port->dev, "%s - sending TI_PURGE_PORT\n", __func__);
524 	status = ti_command_out_sync(tdev, TI_PURGE_PORT,
525 		(__u8)(TI_UART1_PORT + port_number), TI_PURGE_INPUT, NULL, 0);
526 	if (status) {
527 		dev_err(&port->dev, "%s - cannot clear input buffers, %d\n",
528 							__func__, status);
529 		goto unlink_int_urb;
530 	}
531 	status = ti_command_out_sync(tdev, TI_PURGE_PORT,
532 		(__u8)(TI_UART1_PORT + port_number), TI_PURGE_OUTPUT, NULL, 0);
533 	if (status) {
534 		dev_err(&port->dev, "%s - cannot clear output buffers, %d\n",
535 							__func__, status);
536 		goto unlink_int_urb;
537 	}
538 
539 	/* reset the data toggle on the bulk endpoints to work around bug in
540 	 * host controllers where things get out of sync some times */
541 	usb_clear_halt(dev, port->write_urb->pipe);
542 	usb_clear_halt(dev, port->read_urb->pipe);
543 
544 	if (tty)
545 		ti_set_termios(tty, port, &tty->termios);
546 
547 	dev_dbg(&port->dev, "%s - sending TI_OPEN_PORT (2)\n", __func__);
548 	status = ti_command_out_sync(tdev, TI_OPEN_PORT,
549 		(__u8)(TI_UART1_PORT + port_number), open_settings, NULL, 0);
550 	if (status) {
551 		dev_err(&port->dev, "%s - cannot send open command (2), %d\n",
552 							__func__, status);
553 		goto unlink_int_urb;
554 	}
555 
556 	dev_dbg(&port->dev, "%s - sending TI_START_PORT (2)\n", __func__);
557 	status = ti_command_out_sync(tdev, TI_START_PORT,
558 		(__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
559 	if (status) {
560 		dev_err(&port->dev, "%s - cannot send start command (2), %d\n",
561 							__func__, status);
562 		goto unlink_int_urb;
563 	}
564 
565 	/* start read urb */
566 	dev_dbg(&port->dev, "%s - start read urb\n", __func__);
567 	urb = port->read_urb;
568 	if (!urb) {
569 		dev_err(&port->dev, "%s - no read urb\n", __func__);
570 		status = -EINVAL;
571 		goto unlink_int_urb;
572 	}
573 	tport->tp_read_urb_state = TI_READ_URB_RUNNING;
574 	urb->context = tport;
575 	status = usb_submit_urb(urb, GFP_KERNEL);
576 	if (status) {
577 		dev_err(&port->dev, "%s - submit read urb failed, %d\n",
578 							__func__, status);
579 		goto unlink_int_urb;
580 	}
581 
582 	tport->tp_is_open = 1;
583 	++tdev->td_open_port_count;
584 
585 	port->port.drain_delay = 3;
586 
587 	goto release_lock;
588 
589 unlink_int_urb:
590 	if (tdev->td_open_port_count == 0)
591 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
592 release_lock:
593 	mutex_unlock(&tdev->td_open_close_lock);
594 	dev_dbg(&port->dev, "%s - exit %d\n", __func__, status);
595 	return status;
596 }
597 
598 
599 static void ti_close(struct usb_serial_port *port)
600 {
601 	struct ti_device *tdev;
602 	struct ti_port *tport;
603 	int port_number;
604 	int status;
605 	int do_unlock;
606 	unsigned long flags;
607 
608 	tdev = usb_get_serial_data(port->serial);
609 	tport = usb_get_serial_port_data(port);
610 	if (tdev == NULL || tport == NULL)
611 		return;
612 
613 	tport->tp_is_open = 0;
614 
615 	usb_kill_urb(port->read_urb);
616 	usb_kill_urb(port->write_urb);
617 	tport->tp_write_urb_in_use = 0;
618 	spin_lock_irqsave(&tport->tp_lock, flags);
619 	kfifo_reset_out(&tport->write_fifo);
620 	spin_unlock_irqrestore(&tport->tp_lock, flags);
621 
622 	port_number = port->port_number;
623 
624 	dev_dbg(&port->dev, "%s - sending TI_CLOSE_PORT\n", __func__);
625 	status = ti_command_out_sync(tdev, TI_CLOSE_PORT,
626 		     (__u8)(TI_UART1_PORT + port_number), 0, NULL, 0);
627 	if (status)
628 		dev_err(&port->dev,
629 			"%s - cannot send close port command, %d\n"
630 							, __func__, status);
631 
632 	/* if mutex_lock is interrupted, continue anyway */
633 	do_unlock = !mutex_lock_interruptible(&tdev->td_open_close_lock);
634 	--tport->tp_tdev->td_open_port_count;
635 	if (tport->tp_tdev->td_open_port_count <= 0) {
636 		/* last port is closed, shut down interrupt urb */
637 		usb_kill_urb(port->serial->port[0]->interrupt_in_urb);
638 		tport->tp_tdev->td_open_port_count = 0;
639 	}
640 	if (do_unlock)
641 		mutex_unlock(&tdev->td_open_close_lock);
642 }
643 
644 
645 static int ti_write(struct tty_struct *tty, struct usb_serial_port *port,
646 			const unsigned char *data, int count)
647 {
648 	struct ti_port *tport = usb_get_serial_port_data(port);
649 
650 	if (count == 0) {
651 		dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
652 		return 0;
653 	}
654 
655 	if (tport == NULL || !tport->tp_is_open)
656 		return -ENODEV;
657 
658 	count = kfifo_in_locked(&tport->write_fifo, data, count,
659 							&tport->tp_lock);
660 	ti_send(tport);
661 
662 	return count;
663 }
664 
665 
666 static int ti_write_room(struct tty_struct *tty)
667 {
668 	struct usb_serial_port *port = tty->driver_data;
669 	struct ti_port *tport = usb_get_serial_port_data(port);
670 	int room = 0;
671 	unsigned long flags;
672 
673 	if (tport == NULL)
674 		return 0;
675 
676 	spin_lock_irqsave(&tport->tp_lock, flags);
677 	room = kfifo_avail(&tport->write_fifo);
678 	spin_unlock_irqrestore(&tport->tp_lock, flags);
679 
680 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
681 	return room;
682 }
683 
684 
685 static int ti_chars_in_buffer(struct tty_struct *tty)
686 {
687 	struct usb_serial_port *port = tty->driver_data;
688 	struct ti_port *tport = usb_get_serial_port_data(port);
689 	int chars = 0;
690 	unsigned long flags;
691 
692 	if (tport == NULL)
693 		return 0;
694 
695 	spin_lock_irqsave(&tport->tp_lock, flags);
696 	chars = kfifo_len(&tport->write_fifo);
697 	spin_unlock_irqrestore(&tport->tp_lock, flags);
698 
699 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
700 	return chars;
701 }
702 
703 static bool ti_tx_empty(struct usb_serial_port *port)
704 {
705 	struct ti_port *tport = usb_get_serial_port_data(port);
706 	int ret;
707 	u8 lsr;
708 
709 	ret = ti_get_lsr(tport, &lsr);
710 	if (!ret && !(lsr & TI_LSR_TX_EMPTY))
711 		return false;
712 
713 	return true;
714 }
715 
716 static void ti_throttle(struct tty_struct *tty)
717 {
718 	struct usb_serial_port *port = tty->driver_data;
719 	struct ti_port *tport = usb_get_serial_port_data(port);
720 
721 	if (tport == NULL)
722 		return;
723 
724 	if (I_IXOFF(tty) || C_CRTSCTS(tty))
725 		ti_stop_read(tport, tty);
726 
727 }
728 
729 
730 static void ti_unthrottle(struct tty_struct *tty)
731 {
732 	struct usb_serial_port *port = tty->driver_data;
733 	struct ti_port *tport = usb_get_serial_port_data(port);
734 	int status;
735 
736 	if (tport == NULL)
737 		return;
738 
739 	if (I_IXOFF(tty) || C_CRTSCTS(tty)) {
740 		status = ti_restart_read(tport, tty);
741 		if (status)
742 			dev_err(&port->dev, "%s - cannot restart read, %d\n",
743 							__func__, status);
744 	}
745 }
746 
747 static int ti_ioctl(struct tty_struct *tty,
748 	unsigned int cmd, unsigned long arg)
749 {
750 	struct usb_serial_port *port = tty->driver_data;
751 	struct ti_port *tport = usb_get_serial_port_data(port);
752 
753 	dev_dbg(&port->dev, "%s - cmd = 0x%04X\n", __func__, cmd);
754 
755 	if (tport == NULL)
756 		return -ENODEV;
757 
758 	switch (cmd) {
759 	case TIOCGSERIAL:
760 		dev_dbg(&port->dev, "%s - TIOCGSERIAL\n", __func__);
761 		return ti_get_serial_info(tport,
762 				(struct serial_struct __user *)arg);
763 	case TIOCSSERIAL:
764 		dev_dbg(&port->dev, "%s - TIOCSSERIAL\n", __func__);
765 		return ti_set_serial_info(tty, tport,
766 				(struct serial_struct __user *)arg);
767 	}
768 	return -ENOIOCTLCMD;
769 }
770 
771 
772 static void ti_set_termios(struct tty_struct *tty,
773 		struct usb_serial_port *port, struct ktermios *old_termios)
774 {
775 	struct ti_port *tport = usb_get_serial_port_data(port);
776 	struct ti_uart_config *config;
777 	tcflag_t cflag, iflag;
778 	int baud;
779 	int status;
780 	int port_number = port->port_number;
781 	unsigned int mcr;
782 
783 	cflag = tty->termios.c_cflag;
784 	iflag = tty->termios.c_iflag;
785 
786 	dev_dbg(&port->dev, "%s - cflag %08x, iflag %08x\n", __func__, cflag, iflag);
787 	dev_dbg(&port->dev, "%s - old clfag %08x, old iflag %08x\n", __func__,
788 		old_termios->c_cflag, old_termios->c_iflag);
789 
790 	if (tport == NULL)
791 		return;
792 
793 	config = kmalloc(sizeof(*config), GFP_KERNEL);
794 	if (!config) {
795 		dev_err(&port->dev, "%s - out of memory\n", __func__);
796 		return;
797 	}
798 
799 	config->wFlags = 0;
800 
801 	/* these flags must be set */
802 	config->wFlags |= TI_UART_ENABLE_MS_INTS;
803 	config->wFlags |= TI_UART_ENABLE_AUTO_START_DMA;
804 	config->bUartMode = (__u8)(tport->tp_uart_mode);
805 
806 	switch (cflag & CSIZE) {
807 	case CS5:
808 		    config->bDataBits = TI_UART_5_DATA_BITS;
809 		    break;
810 	case CS6:
811 		    config->bDataBits = TI_UART_6_DATA_BITS;
812 		    break;
813 	case CS7:
814 		    config->bDataBits = TI_UART_7_DATA_BITS;
815 		    break;
816 	default:
817 	case CS8:
818 		    config->bDataBits = TI_UART_8_DATA_BITS;
819 		    break;
820 	}
821 
822 	/* CMSPAR isn't supported by this driver */
823 	tty->termios.c_cflag &= ~CMSPAR;
824 
825 	if (cflag & PARENB) {
826 		if (cflag & PARODD) {
827 			config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
828 			config->bParity = TI_UART_ODD_PARITY;
829 		} else {
830 			config->wFlags |= TI_UART_ENABLE_PARITY_CHECKING;
831 			config->bParity = TI_UART_EVEN_PARITY;
832 		}
833 	} else {
834 		config->wFlags &= ~TI_UART_ENABLE_PARITY_CHECKING;
835 		config->bParity = TI_UART_NO_PARITY;
836 	}
837 
838 	if (cflag & CSTOPB)
839 		config->bStopBits = TI_UART_2_STOP_BITS;
840 	else
841 		config->bStopBits = TI_UART_1_STOP_BITS;
842 
843 	if (cflag & CRTSCTS) {
844 		/* RTS flow control must be off to drop RTS for baud rate B0 */
845 		if ((cflag & CBAUD) != B0)
846 			config->wFlags |= TI_UART_ENABLE_RTS_IN;
847 		config->wFlags |= TI_UART_ENABLE_CTS_OUT;
848 	} else {
849 		tty->hw_stopped = 0;
850 		ti_restart_read(tport, tty);
851 	}
852 
853 	if (I_IXOFF(tty) || I_IXON(tty)) {
854 		config->cXon  = START_CHAR(tty);
855 		config->cXoff = STOP_CHAR(tty);
856 
857 		if (I_IXOFF(tty))
858 			config->wFlags |= TI_UART_ENABLE_X_IN;
859 		else
860 			ti_restart_read(tport, tty);
861 
862 		if (I_IXON(tty))
863 			config->wFlags |= TI_UART_ENABLE_X_OUT;
864 	}
865 
866 	baud = tty_get_baud_rate(tty);
867 	if (!baud)
868 		baud = 9600;
869 	if (tport->tp_tdev->td_is_3410)
870 		config->wBaudRate = (__u16)((923077 + baud/2) / baud);
871 	else
872 		config->wBaudRate = (__u16)((461538 + baud/2) / baud);
873 
874 	/* FIXME: Should calculate resulting baud here and report it back */
875 	if ((cflag & CBAUD) != B0)
876 		tty_encode_baud_rate(tty, baud, baud);
877 
878 	dev_dbg(&port->dev,
879 		"%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
880 		__func__, baud, config->wBaudRate, config->wFlags,
881 		config->bDataBits, config->bParity, config->bStopBits,
882 		config->cXon, config->cXoff, config->bUartMode);
883 
884 	cpu_to_be16s(&config->wBaudRate);
885 	cpu_to_be16s(&config->wFlags);
886 
887 	status = ti_command_out_sync(tport->tp_tdev, TI_SET_CONFIG,
888 		(__u8)(TI_UART1_PORT + port_number), 0, (__u8 *)config,
889 		sizeof(*config));
890 	if (status)
891 		dev_err(&port->dev, "%s - cannot set config on port %d, %d\n",
892 					__func__, port_number, status);
893 
894 	/* SET_CONFIG asserts RTS and DTR, reset them correctly */
895 	mcr = tport->tp_shadow_mcr;
896 	/* if baud rate is B0, clear RTS and DTR */
897 	if ((cflag & CBAUD) == B0)
898 		mcr &= ~(TI_MCR_DTR | TI_MCR_RTS);
899 	status = ti_set_mcr(tport, mcr);
900 	if (status)
901 		dev_err(&port->dev,
902 			"%s - cannot set modem control on port %d, %d\n",
903 						__func__, port_number, status);
904 
905 	kfree(config);
906 }
907 
908 
909 static int ti_tiocmget(struct tty_struct *tty)
910 {
911 	struct usb_serial_port *port = tty->driver_data;
912 	struct ti_port *tport = usb_get_serial_port_data(port);
913 	unsigned int result;
914 	unsigned int msr;
915 	unsigned int mcr;
916 	unsigned long flags;
917 
918 	if (tport == NULL)
919 		return -ENODEV;
920 
921 	spin_lock_irqsave(&tport->tp_lock, flags);
922 	msr = tport->tp_msr;
923 	mcr = tport->tp_shadow_mcr;
924 	spin_unlock_irqrestore(&tport->tp_lock, flags);
925 
926 	result = ((mcr & TI_MCR_DTR) ? TIOCM_DTR : 0)
927 		| ((mcr & TI_MCR_RTS) ? TIOCM_RTS : 0)
928 		| ((mcr & TI_MCR_LOOP) ? TIOCM_LOOP : 0)
929 		| ((msr & TI_MSR_CTS) ? TIOCM_CTS : 0)
930 		| ((msr & TI_MSR_CD) ? TIOCM_CAR : 0)
931 		| ((msr & TI_MSR_RI) ? TIOCM_RI : 0)
932 		| ((msr & TI_MSR_DSR) ? TIOCM_DSR : 0);
933 
934 	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
935 
936 	return result;
937 }
938 
939 
940 static int ti_tiocmset(struct tty_struct *tty,
941 				unsigned int set, unsigned int clear)
942 {
943 	struct usb_serial_port *port = tty->driver_data;
944 	struct ti_port *tport = usb_get_serial_port_data(port);
945 	unsigned int mcr;
946 	unsigned long flags;
947 
948 	if (tport == NULL)
949 		return -ENODEV;
950 
951 	spin_lock_irqsave(&tport->tp_lock, flags);
952 	mcr = tport->tp_shadow_mcr;
953 
954 	if (set & TIOCM_RTS)
955 		mcr |= TI_MCR_RTS;
956 	if (set & TIOCM_DTR)
957 		mcr |= TI_MCR_DTR;
958 	if (set & TIOCM_LOOP)
959 		mcr |= TI_MCR_LOOP;
960 
961 	if (clear & TIOCM_RTS)
962 		mcr &= ~TI_MCR_RTS;
963 	if (clear & TIOCM_DTR)
964 		mcr &= ~TI_MCR_DTR;
965 	if (clear & TIOCM_LOOP)
966 		mcr &= ~TI_MCR_LOOP;
967 	spin_unlock_irqrestore(&tport->tp_lock, flags);
968 
969 	return ti_set_mcr(tport, mcr);
970 }
971 
972 
973 static void ti_break(struct tty_struct *tty, int break_state)
974 {
975 	struct usb_serial_port *port = tty->driver_data;
976 	struct ti_port *tport = usb_get_serial_port_data(port);
977 	int status;
978 
979 	dev_dbg(&port->dev, "%s - state = %d\n", __func__, break_state);
980 
981 	if (tport == NULL)
982 		return;
983 
984 	status = ti_write_byte(port, tport->tp_tdev,
985 		tport->tp_uart_base_addr + TI_UART_OFFSET_LCR,
986 		TI_LCR_BREAK, break_state == -1 ? TI_LCR_BREAK : 0);
987 
988 	if (status)
989 		dev_dbg(&port->dev, "%s - error setting break, %d\n", __func__, status);
990 }
991 
992 
993 static void ti_interrupt_callback(struct urb *urb)
994 {
995 	struct ti_device *tdev = urb->context;
996 	struct usb_serial_port *port;
997 	struct usb_serial *serial = tdev->td_serial;
998 	struct ti_port *tport;
999 	struct device *dev = &urb->dev->dev;
1000 	unsigned char *data = urb->transfer_buffer;
1001 	int length = urb->actual_length;
1002 	int port_number;
1003 	int function;
1004 	int status = urb->status;
1005 	int retval;
1006 	__u8 msr;
1007 
1008 	switch (status) {
1009 	case 0:
1010 		break;
1011 	case -ECONNRESET:
1012 	case -ENOENT:
1013 	case -ESHUTDOWN:
1014 		dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1015 		tdev->td_urb_error = 1;
1016 		return;
1017 	default:
1018 		dev_err(dev, "%s - nonzero urb status, %d\n", __func__, status);
1019 		tdev->td_urb_error = 1;
1020 		goto exit;
1021 	}
1022 
1023 	if (length != 2) {
1024 		dev_dbg(dev, "%s - bad packet size, %d\n", __func__, length);
1025 		goto exit;
1026 	}
1027 
1028 	if (data[0] == TI_CODE_HARDWARE_ERROR) {
1029 		dev_err(dev, "%s - hardware error, %d\n", __func__, data[1]);
1030 		goto exit;
1031 	}
1032 
1033 	port_number = TI_GET_PORT_FROM_CODE(data[0]);
1034 	function = TI_GET_FUNC_FROM_CODE(data[0]);
1035 
1036 	dev_dbg(dev, "%s - port_number %d, function %d, data 0x%02X\n",
1037 		__func__, port_number, function, data[1]);
1038 
1039 	if (port_number >= serial->num_ports) {
1040 		dev_err(dev, "%s - bad port number, %d\n",
1041 						__func__, port_number);
1042 		goto exit;
1043 	}
1044 
1045 	port = serial->port[port_number];
1046 
1047 	tport = usb_get_serial_port_data(port);
1048 	if (!tport)
1049 		goto exit;
1050 
1051 	switch (function) {
1052 	case TI_CODE_DATA_ERROR:
1053 		dev_err(dev, "%s - DATA ERROR, port %d, data 0x%02X\n",
1054 			__func__, port_number, data[1]);
1055 		break;
1056 
1057 	case TI_CODE_MODEM_STATUS:
1058 		msr = data[1];
1059 		dev_dbg(dev, "%s - port %d, msr 0x%02X\n", __func__, port_number, msr);
1060 		ti_handle_new_msr(tport, msr);
1061 		break;
1062 
1063 	default:
1064 		dev_err(dev, "%s - unknown interrupt code, 0x%02X\n",
1065 							__func__, data[1]);
1066 		break;
1067 	}
1068 
1069 exit:
1070 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1071 	if (retval)
1072 		dev_err(dev, "%s - resubmit interrupt urb failed, %d\n",
1073 			__func__, retval);
1074 }
1075 
1076 
1077 static void ti_bulk_in_callback(struct urb *urb)
1078 {
1079 	struct ti_port *tport = urb->context;
1080 	struct usb_serial_port *port = tport->tp_port;
1081 	struct device *dev = &urb->dev->dev;
1082 	int status = urb->status;
1083 	int retval = 0;
1084 
1085 	switch (status) {
1086 	case 0:
1087 		break;
1088 	case -ECONNRESET:
1089 	case -ENOENT:
1090 	case -ESHUTDOWN:
1091 		dev_dbg(dev, "%s - urb shutting down, %d\n", __func__, status);
1092 		tport->tp_tdev->td_urb_error = 1;
1093 		wake_up_interruptible(&tport->tp_write_wait);
1094 		return;
1095 	default:
1096 		dev_err(dev, "%s - nonzero urb status, %d\n",
1097 			__func__, status);
1098 		tport->tp_tdev->td_urb_error = 1;
1099 		wake_up_interruptible(&tport->tp_write_wait);
1100 	}
1101 
1102 	if (status == -EPIPE)
1103 		goto exit;
1104 
1105 	if (status) {
1106 		dev_err(dev, "%s - stopping read!\n", __func__);
1107 		return;
1108 	}
1109 
1110 	if (urb->actual_length) {
1111 		usb_serial_debug_data(dev, __func__, urb->actual_length,
1112 				      urb->transfer_buffer);
1113 
1114 		if (!tport->tp_is_open)
1115 			dev_dbg(dev, "%s - port closed, dropping data\n",
1116 				__func__);
1117 		else
1118 			ti_recv(port, urb->transfer_buffer, urb->actual_length);
1119 		spin_lock(&tport->tp_lock);
1120 		port->icount.rx += urb->actual_length;
1121 		spin_unlock(&tport->tp_lock);
1122 	}
1123 
1124 exit:
1125 	/* continue to read unless stopping */
1126 	spin_lock(&tport->tp_lock);
1127 	if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1128 		retval = usb_submit_urb(urb, GFP_ATOMIC);
1129 	else if (tport->tp_read_urb_state == TI_READ_URB_STOPPING)
1130 		tport->tp_read_urb_state = TI_READ_URB_STOPPED;
1131 
1132 	spin_unlock(&tport->tp_lock);
1133 	if (retval)
1134 		dev_err(dev, "%s - resubmit read urb failed, %d\n",
1135 			__func__, retval);
1136 }
1137 
1138 
1139 static void ti_bulk_out_callback(struct urb *urb)
1140 {
1141 	struct ti_port *tport = urb->context;
1142 	struct usb_serial_port *port = tport->tp_port;
1143 	int status = urb->status;
1144 
1145 	tport->tp_write_urb_in_use = 0;
1146 
1147 	switch (status) {
1148 	case 0:
1149 		break;
1150 	case -ECONNRESET:
1151 	case -ENOENT:
1152 	case -ESHUTDOWN:
1153 		dev_dbg(&port->dev, "%s - urb shutting down, %d\n", __func__, status);
1154 		tport->tp_tdev->td_urb_error = 1;
1155 		wake_up_interruptible(&tport->tp_write_wait);
1156 		return;
1157 	default:
1158 		dev_err_console(port, "%s - nonzero urb status, %d\n",
1159 			__func__, status);
1160 		tport->tp_tdev->td_urb_error = 1;
1161 		wake_up_interruptible(&tport->tp_write_wait);
1162 	}
1163 
1164 	/* send any buffered data */
1165 	ti_send(tport);
1166 }
1167 
1168 
1169 static void ti_recv(struct usb_serial_port *port, unsigned char *data,
1170 		int length)
1171 {
1172 	int cnt;
1173 
1174 	do {
1175 		cnt = tty_insert_flip_string(&port->port, data, length);
1176 		if (cnt < length) {
1177 			dev_err(&port->dev, "%s - dropping data, %d bytes lost\n",
1178 						__func__, length - cnt);
1179 			if (cnt == 0)
1180 				break;
1181 		}
1182 		tty_flip_buffer_push(&port->port);
1183 		data += cnt;
1184 		length -= cnt;
1185 	} while (length > 0);
1186 }
1187 
1188 
1189 static void ti_send(struct ti_port *tport)
1190 {
1191 	int count, result;
1192 	struct usb_serial_port *port = tport->tp_port;
1193 	unsigned long flags;
1194 
1195 	spin_lock_irqsave(&tport->tp_lock, flags);
1196 
1197 	if (tport->tp_write_urb_in_use)
1198 		goto unlock;
1199 
1200 	count = kfifo_out(&tport->write_fifo,
1201 				port->write_urb->transfer_buffer,
1202 				port->bulk_out_size);
1203 
1204 	if (count == 0)
1205 		goto unlock;
1206 
1207 	tport->tp_write_urb_in_use = 1;
1208 
1209 	spin_unlock_irqrestore(&tport->tp_lock, flags);
1210 
1211 	usb_serial_debug_data(&port->dev, __func__, count,
1212 			      port->write_urb->transfer_buffer);
1213 
1214 	usb_fill_bulk_urb(port->write_urb, port->serial->dev,
1215 			   usb_sndbulkpipe(port->serial->dev,
1216 					    port->bulk_out_endpointAddress),
1217 			   port->write_urb->transfer_buffer, count,
1218 			   ti_bulk_out_callback, tport);
1219 
1220 	result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1221 	if (result) {
1222 		dev_err_console(port, "%s - submit write urb failed, %d\n",
1223 							__func__, result);
1224 		tport->tp_write_urb_in_use = 0;
1225 		/* TODO: reschedule ti_send */
1226 	} else {
1227 		spin_lock_irqsave(&tport->tp_lock, flags);
1228 		port->icount.tx += count;
1229 		spin_unlock_irqrestore(&tport->tp_lock, flags);
1230 	}
1231 
1232 	/* more room in the buffer for new writes, wakeup */
1233 	tty_port_tty_wakeup(&port->port);
1234 
1235 	wake_up_interruptible(&tport->tp_write_wait);
1236 	return;
1237 unlock:
1238 	spin_unlock_irqrestore(&tport->tp_lock, flags);
1239 	return;
1240 }
1241 
1242 
1243 static int ti_set_mcr(struct ti_port *tport, unsigned int mcr)
1244 {
1245 	unsigned long flags;
1246 	int status;
1247 
1248 	status = ti_write_byte(tport->tp_port, tport->tp_tdev,
1249 		tport->tp_uart_base_addr + TI_UART_OFFSET_MCR,
1250 		TI_MCR_RTS | TI_MCR_DTR | TI_MCR_LOOP, mcr);
1251 
1252 	spin_lock_irqsave(&tport->tp_lock, flags);
1253 	if (!status)
1254 		tport->tp_shadow_mcr = mcr;
1255 	spin_unlock_irqrestore(&tport->tp_lock, flags);
1256 
1257 	return status;
1258 }
1259 
1260 
1261 static int ti_get_lsr(struct ti_port *tport, u8 *lsr)
1262 {
1263 	int size, status;
1264 	struct ti_device *tdev = tport->tp_tdev;
1265 	struct usb_serial_port *port = tport->tp_port;
1266 	int port_number = port->port_number;
1267 	struct ti_port_status *data;
1268 
1269 	size = sizeof(struct ti_port_status);
1270 	data = kmalloc(size, GFP_KERNEL);
1271 	if (!data) {
1272 		dev_err(&port->dev, "%s - out of memory\n", __func__);
1273 		return -ENOMEM;
1274 	}
1275 
1276 	status = ti_command_in_sync(tdev, TI_GET_PORT_STATUS,
1277 		(__u8)(TI_UART1_PORT+port_number), 0, (__u8 *)data, size);
1278 	if (status) {
1279 		dev_err(&port->dev,
1280 			"%s - get port status command failed, %d\n",
1281 							__func__, status);
1282 		goto free_data;
1283 	}
1284 
1285 	dev_dbg(&port->dev, "%s - lsr 0x%02X\n", __func__, data->bLSR);
1286 
1287 	*lsr = data->bLSR;
1288 
1289 free_data:
1290 	kfree(data);
1291 	return status;
1292 }
1293 
1294 
1295 static int ti_get_serial_info(struct ti_port *tport,
1296 	struct serial_struct __user *ret_arg)
1297 {
1298 	struct usb_serial_port *port = tport->tp_port;
1299 	struct serial_struct ret_serial;
1300 	unsigned cwait;
1301 
1302 	if (!ret_arg)
1303 		return -EFAULT;
1304 
1305 	cwait = port->port.closing_wait;
1306 	if (cwait != ASYNC_CLOSING_WAIT_NONE)
1307 		cwait = jiffies_to_msecs(cwait) / 10;
1308 
1309 	memset(&ret_serial, 0, sizeof(ret_serial));
1310 
1311 	ret_serial.type = PORT_16550A;
1312 	ret_serial.line = port->minor;
1313 	ret_serial.port = port->port_number;
1314 	ret_serial.flags = tport->tp_flags;
1315 	ret_serial.xmit_fifo_size = TI_WRITE_BUF_SIZE;
1316 	ret_serial.baud_base = tport->tp_tdev->td_is_3410 ? 921600 : 460800;
1317 	ret_serial.closing_wait = cwait;
1318 
1319 	if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
1320 		return -EFAULT;
1321 
1322 	return 0;
1323 }
1324 
1325 
1326 static int ti_set_serial_info(struct tty_struct *tty, struct ti_port *tport,
1327 	struct serial_struct __user *new_arg)
1328 {
1329 	struct serial_struct new_serial;
1330 	unsigned cwait;
1331 
1332 	if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
1333 		return -EFAULT;
1334 
1335 	cwait = new_serial.closing_wait;
1336 	if (cwait != ASYNC_CLOSING_WAIT_NONE)
1337 		cwait = msecs_to_jiffies(10 * new_serial.closing_wait);
1338 
1339 	tport->tp_flags = new_serial.flags & TI_SET_SERIAL_FLAGS;
1340 	tport->tp_port->port.closing_wait = cwait;
1341 
1342 	return 0;
1343 }
1344 
1345 
1346 static void ti_handle_new_msr(struct ti_port *tport, __u8 msr)
1347 {
1348 	struct async_icount *icount;
1349 	struct tty_struct *tty;
1350 	unsigned long flags;
1351 
1352 	dev_dbg(&tport->tp_port->dev, "%s - msr 0x%02X\n", __func__, msr);
1353 
1354 	if (msr & TI_MSR_DELTA_MASK) {
1355 		spin_lock_irqsave(&tport->tp_lock, flags);
1356 		icount = &tport->tp_port->icount;
1357 		if (msr & TI_MSR_DELTA_CTS)
1358 			icount->cts++;
1359 		if (msr & TI_MSR_DELTA_DSR)
1360 			icount->dsr++;
1361 		if (msr & TI_MSR_DELTA_CD)
1362 			icount->dcd++;
1363 		if (msr & TI_MSR_DELTA_RI)
1364 			icount->rng++;
1365 		wake_up_interruptible(&tport->tp_port->port.delta_msr_wait);
1366 		spin_unlock_irqrestore(&tport->tp_lock, flags);
1367 	}
1368 
1369 	tport->tp_msr = msr & TI_MSR_MASK;
1370 
1371 	/* handle CTS flow control */
1372 	tty = tty_port_tty_get(&tport->tp_port->port);
1373 	if (tty && C_CRTSCTS(tty)) {
1374 		if (msr & TI_MSR_CTS) {
1375 			tty->hw_stopped = 0;
1376 			tty_wakeup(tty);
1377 		} else {
1378 			tty->hw_stopped = 1;
1379 		}
1380 	}
1381 	tty_kref_put(tty);
1382 }
1383 
1384 
1385 static void ti_stop_read(struct ti_port *tport, struct tty_struct *tty)
1386 {
1387 	unsigned long flags;
1388 
1389 	spin_lock_irqsave(&tport->tp_lock, flags);
1390 
1391 	if (tport->tp_read_urb_state == TI_READ_URB_RUNNING)
1392 		tport->tp_read_urb_state = TI_READ_URB_STOPPING;
1393 
1394 	spin_unlock_irqrestore(&tport->tp_lock, flags);
1395 }
1396 
1397 
1398 static int ti_restart_read(struct ti_port *tport, struct tty_struct *tty)
1399 {
1400 	struct urb *urb;
1401 	int status = 0;
1402 	unsigned long flags;
1403 
1404 	spin_lock_irqsave(&tport->tp_lock, flags);
1405 
1406 	if (tport->tp_read_urb_state == TI_READ_URB_STOPPED) {
1407 		tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1408 		urb = tport->tp_port->read_urb;
1409 		spin_unlock_irqrestore(&tport->tp_lock, flags);
1410 		urb->context = tport;
1411 		status = usb_submit_urb(urb, GFP_KERNEL);
1412 	} else  {
1413 		tport->tp_read_urb_state = TI_READ_URB_RUNNING;
1414 		spin_unlock_irqrestore(&tport->tp_lock, flags);
1415 	}
1416 
1417 	return status;
1418 }
1419 
1420 
1421 static int ti_command_out_sync(struct ti_device *tdev, __u8 command,
1422 	__u16 moduleid, __u16 value, __u8 *data, int size)
1423 {
1424 	int status;
1425 
1426 	status = usb_control_msg(tdev->td_serial->dev,
1427 		usb_sndctrlpipe(tdev->td_serial->dev, 0), command,
1428 		(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT),
1429 		value, moduleid, data, size, 1000);
1430 
1431 	if (status == size)
1432 		status = 0;
1433 
1434 	if (status > 0)
1435 		status = -ECOMM;
1436 
1437 	return status;
1438 }
1439 
1440 
1441 static int ti_command_in_sync(struct ti_device *tdev, __u8 command,
1442 	__u16 moduleid, __u16 value, __u8 *data, int size)
1443 {
1444 	int status;
1445 
1446 	status = usb_control_msg(tdev->td_serial->dev,
1447 		usb_rcvctrlpipe(tdev->td_serial->dev, 0), command,
1448 		(USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN),
1449 		value, moduleid, data, size, 1000);
1450 
1451 	if (status == size)
1452 		status = 0;
1453 
1454 	if (status > 0)
1455 		status = -ECOMM;
1456 
1457 	return status;
1458 }
1459 
1460 
1461 static int ti_write_byte(struct usb_serial_port *port,
1462 			struct ti_device *tdev, unsigned long addr,
1463 			__u8 mask, __u8 byte)
1464 {
1465 	int status;
1466 	unsigned int size;
1467 	struct ti_write_data_bytes *data;
1468 
1469 	dev_dbg(&port->dev, "%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X\n", __func__,
1470 		addr, mask, byte);
1471 
1472 	size = sizeof(struct ti_write_data_bytes) + 2;
1473 	data = kmalloc(size, GFP_KERNEL);
1474 	if (!data) {
1475 		dev_err(&port->dev, "%s - out of memory\n", __func__);
1476 		return -ENOMEM;
1477 	}
1478 
1479 	data->bAddrType = TI_RW_DATA_ADDR_XDATA;
1480 	data->bDataType = TI_RW_DATA_BYTE;
1481 	data->bDataCounter = 1;
1482 	data->wBaseAddrHi = cpu_to_be16(addr>>16);
1483 	data->wBaseAddrLo = cpu_to_be16(addr);
1484 	data->bData[0] = mask;
1485 	data->bData[1] = byte;
1486 
1487 	status = ti_command_out_sync(tdev, TI_WRITE_DATA, TI_RAM_PORT, 0,
1488 		(__u8 *)data, size);
1489 
1490 	if (status < 0)
1491 		dev_err(&port->dev, "%s - failed, %d\n", __func__, status);
1492 
1493 	kfree(data);
1494 
1495 	return status;
1496 }
1497 
1498 static int ti_do_download(struct usb_device *dev, int pipe,
1499 						u8 *buffer, int size)
1500 {
1501 	int pos;
1502 	u8 cs = 0;
1503 	int done;
1504 	struct ti_firmware_header *header;
1505 	int status = 0;
1506 	int len;
1507 
1508 	for (pos = sizeof(struct ti_firmware_header); pos < size; pos++)
1509 		cs = (__u8)(cs + buffer[pos]);
1510 
1511 	header = (struct ti_firmware_header *)buffer;
1512 	header->wLength = cpu_to_le16((__u16)(size
1513 					- sizeof(struct ti_firmware_header)));
1514 	header->bCheckSum = cs;
1515 
1516 	dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
1517 	for (pos = 0; pos < size; pos += done) {
1518 		len = min(size - pos, TI_DOWNLOAD_MAX_PACKET_SIZE);
1519 		status = usb_bulk_msg(dev, pipe, buffer + pos, len,
1520 								&done, 1000);
1521 		if (status)
1522 			break;
1523 	}
1524 	return status;
1525 }
1526 
1527 static int ti_download_firmware(struct ti_device *tdev)
1528 {
1529 	int status;
1530 	int buffer_size;
1531 	__u8 *buffer;
1532 	struct usb_device *dev = tdev->td_serial->dev;
1533 	unsigned int pipe = usb_sndbulkpipe(dev,
1534 		tdev->td_serial->port[0]->bulk_out_endpointAddress);
1535 	const struct firmware *fw_p;
1536 	char buf[32];
1537 
1538 	/* try ID specific firmware first, then try generic firmware */
1539 	sprintf(buf, "ti_usb-v%04x-p%04x.fw",
1540 			le16_to_cpu(dev->descriptor.idVendor),
1541 			le16_to_cpu(dev->descriptor.idProduct));
1542 	status = request_firmware(&fw_p, buf, &dev->dev);
1543 
1544 	if (status != 0) {
1545 		buf[0] = '\0';
1546 		if (le16_to_cpu(dev->descriptor.idVendor) == MTS_VENDOR_ID) {
1547 			switch (le16_to_cpu(dev->descriptor.idProduct)) {
1548 			case MTS_CDMA_PRODUCT_ID:
1549 				strcpy(buf, "mts_cdma.fw");
1550 				break;
1551 			case MTS_GSM_PRODUCT_ID:
1552 				strcpy(buf, "mts_gsm.fw");
1553 				break;
1554 			case MTS_EDGE_PRODUCT_ID:
1555 				strcpy(buf, "mts_edge.fw");
1556 				break;
1557 			case MTS_MT9234MU_PRODUCT_ID:
1558 				strcpy(buf, "mts_mt9234mu.fw");
1559 				break;
1560 			case MTS_MT9234ZBA_PRODUCT_ID:
1561 				strcpy(buf, "mts_mt9234zba.fw");
1562 				break;
1563 			case MTS_MT9234ZBAOLD_PRODUCT_ID:
1564 				strcpy(buf, "mts_mt9234zba.fw");
1565 				break;			}
1566 		}
1567 		if (buf[0] == '\0') {
1568 			if (tdev->td_is_3410)
1569 				strcpy(buf, "ti_3410.fw");
1570 			else
1571 				strcpy(buf, "ti_5052.fw");
1572 		}
1573 		status = request_firmware(&fw_p, buf, &dev->dev);
1574 	}
1575 	if (status) {
1576 		dev_err(&dev->dev, "%s - firmware not found\n", __func__);
1577 		return -ENOENT;
1578 	}
1579 	if (fw_p->size > TI_FIRMWARE_BUF_SIZE) {
1580 		dev_err(&dev->dev, "%s - firmware too large %zu\n", __func__, fw_p->size);
1581 		release_firmware(fw_p);
1582 		return -ENOENT;
1583 	}
1584 
1585 	buffer_size = TI_FIRMWARE_BUF_SIZE + sizeof(struct ti_firmware_header);
1586 	buffer = kmalloc(buffer_size, GFP_KERNEL);
1587 	if (buffer) {
1588 		memcpy(buffer, fw_p->data, fw_p->size);
1589 		memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
1590 		status = ti_do_download(dev, pipe, buffer, fw_p->size);
1591 		kfree(buffer);
1592 	} else {
1593 		dev_dbg(&dev->dev, "%s ENOMEM\n", __func__);
1594 		status = -ENOMEM;
1595 	}
1596 	release_firmware(fw_p);
1597 	if (status) {
1598 		dev_err(&dev->dev, "%s - error downloading firmware, %d\n",
1599 							__func__, status);
1600 		return status;
1601 	}
1602 
1603 	dev_dbg(&dev->dev, "%s - download successful\n", __func__);
1604 
1605 	return 0;
1606 }
1607