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