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