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