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