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