3 * TI 3410/5052 USB Serial Driver
5 * Copyright (C) 2004 Texas Instruments
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
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.
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>.
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/firmware.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h>
29 #include <linux/module.h>
30 #include <linux/spinlock.h>
31 #include <linux/ioctl.h>
32 #include <linux/serial.h>
33 #include <linux/circ_buf.h>
34 #include <linux/mutex.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
39 #include "ti_usb_3410_5052.h"
43 #define TI_DRIVER_VERSION "v0.9"
44 #define TI_DRIVER_AUTHOR "Al Borchers <alborchers@steinerpoint.com>"
45 #define TI_DRIVER_DESC "TI USB 3410/5052 Serial Driver"
47 #define TI_FIRMWARE_BUF_SIZE 16284
49 #define TI_WRITE_BUF_SIZE 1024
51 #define TI_TRANSFER_TIMEOUT 2
53 #define TI_DEFAULT_LOW_LATENCY 0
54 #define TI_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
56 /* supported setserial flags */
57 #define TI_SET_SERIAL_FLAGS (ASYNC_LOW_LATENCY)
60 #define TI_READ_URB_RUNNING 0
61 #define TI_READ_URB_STOPPING 1
62 #define TI_READ_URB_STOPPED 2
64 #define TI_EXTRA_VID_PID_COUNT 5
74 __u8 tp_uart_mode
; /* 232 or 485 modes */
75 unsigned int tp_uart_base_addr
;
77 int tp_closing_wait
;/* in .01 secs */
78 struct async_icount tp_icount
;
79 wait_queue_head_t tp_msr_wait
; /* wait for msr change */
80 wait_queue_head_t tp_write_wait
;
81 struct ti_device
*tp_tdev
;
82 struct usb_serial_port
*tp_port
;
84 int tp_read_urb_state
;
85 int tp_write_urb_in_use
;
86 struct circ_buf
*tp_write_buf
;
90 struct mutex td_open_close_lock
;
91 int td_open_port_count
;
92 struct usb_serial
*td_serial
;
98 /* Function Declarations */
100 static int ti_startup(struct usb_serial
*serial
);
101 static void ti_shutdown(struct usb_serial
*serial
);
102 static int ti_open(struct tty_struct
*tty
, struct usb_serial_port
*port
,
104 static void ti_close(struct tty_struct
*tty
, struct usb_serial_port
*port
,
106 static int ti_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
107 const unsigned char *data
, int count
);
108 static int ti_write_room(struct tty_struct
*tty
);
109 static int ti_chars_in_buffer(struct tty_struct
*tty
);
110 static void ti_throttle(struct tty_struct
*tty
);
111 static void ti_unthrottle(struct tty_struct
*tty
);
112 static int ti_ioctl(struct tty_struct
*tty
, struct file
*file
,
113 unsigned int cmd
, unsigned long arg
);
114 static void ti_set_termios(struct tty_struct
*tty
,
115 struct usb_serial_port
*port
, struct ktermios
*old_termios
);
116 static int ti_tiocmget(struct tty_struct
*tty
, struct file
*file
);
117 static int ti_tiocmset(struct tty_struct
*tty
, struct file
*file
,
118 unsigned int set
, unsigned int clear
);
119 static void ti_break(struct tty_struct
*tty
, int break_state
);
120 static void ti_interrupt_callback(struct urb
*urb
);
121 static void ti_bulk_in_callback(struct urb
*urb
);
122 static void ti_bulk_out_callback(struct urb
*urb
);
124 static void ti_recv(struct device
*dev
, struct tty_struct
*tty
,
125 unsigned char *data
, int length
);
126 static void ti_send(struct ti_port
*tport
);
127 static int ti_set_mcr(struct ti_port
*tport
, unsigned int mcr
);
128 static int ti_get_lsr(struct ti_port
*tport
);
129 static int ti_get_serial_info(struct ti_port
*tport
,
130 struct serial_struct __user
*ret_arg
);
131 static int ti_set_serial_info(struct tty_struct
*tty
, struct ti_port
*tport
,
132 struct serial_struct __user
*new_arg
);
133 static void ti_handle_new_msr(struct ti_port
*tport
, __u8 msr
);
135 static void ti_drain(struct ti_port
*tport
, unsigned long timeout
, int flush
);
137 static void ti_stop_read(struct ti_port
*tport
, struct tty_struct
*tty
);
138 static int ti_restart_read(struct ti_port
*tport
, struct tty_struct
*tty
);
140 static int ti_command_out_sync(struct ti_device
*tdev
, __u8 command
,
141 __u16 moduleid
, __u16 value
, __u8
*data
, int size
);
142 static int ti_command_in_sync(struct ti_device
*tdev
, __u8 command
,
143 __u16 moduleid
, __u16 value
, __u8
*data
, int size
);
145 static int ti_write_byte(struct ti_device
*tdev
, unsigned long addr
,
146 __u8 mask
, __u8 byte
);
148 static int ti_download_firmware(struct ti_device
*tdev
);
150 /* circular buffer */
151 static struct circ_buf
*ti_buf_alloc(void);
152 static void ti_buf_free(struct circ_buf
*cb
);
153 static void ti_buf_clear(struct circ_buf
*cb
);
154 static int ti_buf_data_avail(struct circ_buf
*cb
);
155 static int ti_buf_space_avail(struct circ_buf
*cb
);
156 static int ti_buf_put(struct circ_buf
*cb
, const char *buf
, int count
);
157 static int ti_buf_get(struct circ_buf
*cb
, char *buf
, int count
);
162 /* module parameters */
164 static int low_latency
= TI_DEFAULT_LOW_LATENCY
;
165 static int closing_wait
= TI_DEFAULT_CLOSING_WAIT
;
166 static ushort vendor_3410
[TI_EXTRA_VID_PID_COUNT
];
167 static unsigned int vendor_3410_count
;
168 static ushort product_3410
[TI_EXTRA_VID_PID_COUNT
];
169 static unsigned int product_3410_count
;
170 static ushort vendor_5052
[TI_EXTRA_VID_PID_COUNT
];
171 static unsigned int vendor_5052_count
;
172 static ushort product_5052
[TI_EXTRA_VID_PID_COUNT
];
173 static unsigned int product_5052_count
;
175 /* supported devices */
176 /* the array dimension is the number of default entries plus */
177 /* TI_EXTRA_VID_PID_COUNT user defined entries plus 1 terminating */
179 static struct usb_device_id ti_id_table_3410
[2+TI_EXTRA_VID_PID_COUNT
+1] = {
180 { USB_DEVICE(TI_VENDOR_ID
, TI_3410_PRODUCT_ID
) },
181 { USB_DEVICE(TI_VENDOR_ID
, TI_3410_EZ430_ID
) },
184 static struct usb_device_id ti_id_table_5052
[4+TI_EXTRA_VID_PID_COUNT
+1] = {
185 { USB_DEVICE(TI_VENDOR_ID
, TI_5052_BOOT_PRODUCT_ID
) },
186 { USB_DEVICE(TI_VENDOR_ID
, TI_5152_BOOT_PRODUCT_ID
) },
187 { USB_DEVICE(TI_VENDOR_ID
, TI_5052_EEPROM_PRODUCT_ID
) },
188 { USB_DEVICE(TI_VENDOR_ID
, TI_5052_FIRMWARE_PRODUCT_ID
) },
191 static struct usb_device_id ti_id_table_combined
[6+2*TI_EXTRA_VID_PID_COUNT
+1] = {
192 { USB_DEVICE(TI_VENDOR_ID
, TI_3410_PRODUCT_ID
) },
193 { USB_DEVICE(TI_VENDOR_ID
, TI_3410_EZ430_ID
) },
194 { USB_DEVICE(TI_VENDOR_ID
, TI_5052_BOOT_PRODUCT_ID
) },
195 { USB_DEVICE(TI_VENDOR_ID
, TI_5152_BOOT_PRODUCT_ID
) },
196 { USB_DEVICE(TI_VENDOR_ID
, TI_5052_EEPROM_PRODUCT_ID
) },
197 { USB_DEVICE(TI_VENDOR_ID
, TI_5052_FIRMWARE_PRODUCT_ID
) },
201 static struct usb_driver ti_usb_driver
= {
202 .name
= "ti_usb_3410_5052",
203 .probe
= usb_serial_probe
,
204 .disconnect
= usb_serial_disconnect
,
205 .id_table
= ti_id_table_combined
,
209 static struct usb_serial_driver ti_1port_device
= {
211 .owner
= THIS_MODULE
,
212 .name
= "ti_usb_3410_5052_1",
214 .description
= "TI USB 3410 1 port adapter",
215 .usb_driver
= &ti_usb_driver
,
216 .id_table
= ti_id_table_3410
,
218 .attach
= ti_startup
,
219 .shutdown
= ti_shutdown
,
223 .write_room
= ti_write_room
,
224 .chars_in_buffer
= ti_chars_in_buffer
,
225 .throttle
= ti_throttle
,
226 .unthrottle
= ti_unthrottle
,
228 .set_termios
= ti_set_termios
,
229 .tiocmget
= ti_tiocmget
,
230 .tiocmset
= ti_tiocmset
,
231 .break_ctl
= ti_break
,
232 .read_int_callback
= ti_interrupt_callback
,
233 .read_bulk_callback
= ti_bulk_in_callback
,
234 .write_bulk_callback
= ti_bulk_out_callback
,
237 static struct usb_serial_driver ti_2port_device
= {
239 .owner
= THIS_MODULE
,
240 .name
= "ti_usb_3410_5052_2",
242 .description
= "TI USB 5052 2 port adapter",
243 .usb_driver
= &ti_usb_driver
,
244 .id_table
= ti_id_table_5052
,
246 .attach
= ti_startup
,
247 .shutdown
= ti_shutdown
,
251 .write_room
= ti_write_room
,
252 .chars_in_buffer
= ti_chars_in_buffer
,
253 .throttle
= ti_throttle
,
254 .unthrottle
= ti_unthrottle
,
256 .set_termios
= ti_set_termios
,
257 .tiocmget
= ti_tiocmget
,
258 .tiocmset
= ti_tiocmset
,
259 .break_ctl
= ti_break
,
260 .read_int_callback
= ti_interrupt_callback
,
261 .read_bulk_callback
= ti_bulk_in_callback
,
262 .write_bulk_callback
= ti_bulk_out_callback
,
268 MODULE_AUTHOR(TI_DRIVER_AUTHOR
);
269 MODULE_DESCRIPTION(TI_DRIVER_DESC
);
270 MODULE_VERSION(TI_DRIVER_VERSION
);
271 MODULE_LICENSE("GPL");
273 MODULE_FIRMWARE("ti_3410.fw");
274 MODULE_FIRMWARE("ti_5052.fw");
276 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
277 MODULE_PARM_DESC(debug
, "Enable debugging, 0=no, 1=yes");
279 module_param(low_latency
, bool, S_IRUGO
| S_IWUSR
);
280 MODULE_PARM_DESC(low_latency
,
281 "TTY low_latency flag, 0=off, 1=on, default is off");
283 module_param(closing_wait
, int, S_IRUGO
| S_IWUSR
);
284 MODULE_PARM_DESC(closing_wait
,
285 "Maximum wait for data to drain in close, in .01 secs, default is 4000");
287 module_param_array(vendor_3410
, ushort
, &vendor_3410_count
, S_IRUGO
);
288 MODULE_PARM_DESC(vendor_3410
,
289 "Vendor ids for 3410 based devices, 1-5 short integers");
290 module_param_array(product_3410
, ushort
, &product_3410_count
, S_IRUGO
);
291 MODULE_PARM_DESC(product_3410
,
292 "Product ids for 3410 based devices, 1-5 short integers");
293 module_param_array(vendor_5052
, ushort
, &vendor_5052_count
, S_IRUGO
);
294 MODULE_PARM_DESC(vendor_5052
,
295 "Vendor ids for 5052 based devices, 1-5 short integers");
296 module_param_array(product_5052
, ushort
, &product_5052_count
, S_IRUGO
);
297 MODULE_PARM_DESC(product_5052
,
298 "Product ids for 5052 based devices, 1-5 short integers");
300 MODULE_DEVICE_TABLE(usb
, ti_id_table_combined
);
305 static int __init
ti_init(void)
310 /* insert extra vendor and product ids */
311 c
= ARRAY_SIZE(ti_id_table_combined
) - 2 * TI_EXTRA_VID_PID_COUNT
- 1;
312 j
= ARRAY_SIZE(ti_id_table_3410
) - TI_EXTRA_VID_PID_COUNT
- 1;
313 for (i
= 0; i
< min(vendor_3410_count
, product_3410_count
); i
++, j
++, c
++) {
314 ti_id_table_3410
[j
].idVendor
= vendor_3410
[i
];
315 ti_id_table_3410
[j
].idProduct
= product_3410
[i
];
316 ti_id_table_3410
[j
].match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
317 ti_id_table_combined
[c
].idVendor
= vendor_3410
[i
];
318 ti_id_table_combined
[c
].idProduct
= product_3410
[i
];
319 ti_id_table_combined
[c
].match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
321 j
= ARRAY_SIZE(ti_id_table_5052
) - TI_EXTRA_VID_PID_COUNT
- 1;
322 for (i
= 0; i
< min(vendor_5052_count
, product_5052_count
); i
++, j
++, c
++) {
323 ti_id_table_5052
[j
].idVendor
= vendor_5052
[i
];
324 ti_id_table_5052
[j
].idProduct
= product_5052
[i
];
325 ti_id_table_5052
[j
].match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
326 ti_id_table_combined
[c
].idVendor
= vendor_5052
[i
];
327 ti_id_table_combined
[c
].idProduct
= product_5052
[i
];
328 ti_id_table_combined
[c
].match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
331 ret
= usb_serial_register(&ti_1port_device
);
334 ret
= usb_serial_register(&ti_2port_device
);
338 ret
= usb_register(&ti_usb_driver
);
342 printk(KERN_INFO KBUILD_MODNAME
": " TI_DRIVER_VERSION
":"
343 TI_DRIVER_DESC
"\n");
348 usb_serial_deregister(&ti_2port_device
);
350 usb_serial_deregister(&ti_1port_device
);
356 static void __exit
ti_exit(void)
358 usb_serial_deregister(&ti_1port_device
);
359 usb_serial_deregister(&ti_2port_device
);
360 usb_deregister(&ti_usb_driver
);
364 module_init(ti_init
);
365 module_exit(ti_exit
);
368 static int ti_startup(struct usb_serial
*serial
)
370 struct ti_device
*tdev
;
371 struct ti_port
*tport
;
372 struct usb_device
*dev
= serial
->dev
;
377 dbg("%s - product 0x%4X, num configurations %d, configuration value %d",
378 __func__
, le16_to_cpu(dev
->descriptor
.idProduct
),
379 dev
->descriptor
.bNumConfigurations
,
380 dev
->actconfig
->desc
.bConfigurationValue
);
382 /* create device structure */
383 tdev
= kzalloc(sizeof(struct ti_device
), GFP_KERNEL
);
385 dev_err(&dev
->dev
, "%s - out of memory\n", __func__
);
388 mutex_init(&tdev
->td_open_close_lock
);
389 tdev
->td_serial
= serial
;
390 usb_set_serial_data(serial
, tdev
);
392 /* determine device type */
393 if (usb_match_id(serial
->interface
, ti_id_table_3410
))
394 tdev
->td_is_3410
= 1;
395 dbg("%s - device type is %s", __func__
,
396 tdev
->td_is_3410
? "3410" : "5052");
398 /* if we have only 1 configuration, download firmware */
399 if (dev
->descriptor
.bNumConfigurations
== 1) {
400 if ((status
= ti_download_firmware(tdev
)) != 0)
403 /* 3410 must be reset, 5052 resets itself */
404 if (tdev
->td_is_3410
) {
405 msleep_interruptible(100);
406 usb_reset_device(dev
);
413 /* the second configuration must be set */
414 if (dev
->actconfig
->desc
.bConfigurationValue
== TI_BOOT_CONFIG
) {
415 status
= usb_driver_set_configuration(dev
, TI_ACTIVE_CONFIG
);
416 status
= status
? status
: -ENODEV
;
420 /* set up port structures */
421 for (i
= 0; i
< serial
->num_ports
; ++i
) {
422 tport
= kzalloc(sizeof(struct ti_port
), GFP_KERNEL
);
424 dev_err(&dev
->dev
, "%s - out of memory\n", __func__
);
428 spin_lock_init(&tport
->tp_lock
);
429 tport
->tp_uart_base_addr
= (i
== 0 ?
430 TI_UART1_BASE_ADDR
: TI_UART2_BASE_ADDR
);
431 tport
->tp_flags
= low_latency
? ASYNC_LOW_LATENCY
: 0;
432 tport
->tp_closing_wait
= closing_wait
;
433 init_waitqueue_head(&tport
->tp_msr_wait
);
434 init_waitqueue_head(&tport
->tp_write_wait
);
435 tport
->tp_write_buf
= ti_buf_alloc();
436 if (tport
->tp_write_buf
== NULL
) {
437 dev_err(&dev
->dev
, "%s - out of memory\n", __func__
);
442 tport
->tp_port
= serial
->port
[i
];
443 tport
->tp_tdev
= tdev
;
444 usb_set_serial_port_data(serial
->port
[i
], tport
);
445 tport
->tp_uart_mode
= 0; /* default is RS232 */
451 for (--i
; i
>= 0; --i
) {
452 tport
= usb_get_serial_port_data(serial
->port
[i
]);
453 ti_buf_free(tport
->tp_write_buf
);
455 usb_set_serial_port_data(serial
->port
[i
], NULL
);
459 usb_set_serial_data(serial
, NULL
);
464 static void ti_shutdown(struct usb_serial
*serial
)
467 struct ti_device
*tdev
= usb_get_serial_data(serial
);
468 struct ti_port
*tport
;
472 for (i
= 0; i
< serial
->num_ports
; ++i
) {
473 tport
= usb_get_serial_port_data(serial
->port
[i
]);
475 ti_buf_free(tport
->tp_write_buf
);
477 usb_set_serial_port_data(serial
->port
[i
], NULL
);
482 usb_set_serial_data(serial
, NULL
);
486 static int ti_open(struct tty_struct
*tty
,
487 struct usb_serial_port
*port
, struct file
*file
)
489 struct ti_port
*tport
= usb_get_serial_port_data(port
);
490 struct ti_device
*tdev
;
491 struct usb_device
*dev
;
495 __u16 open_settings
= (__u8
)(TI_PIPE_MODE_CONTINOUS
|
496 TI_PIPE_TIMEOUT_ENABLE
|
497 (TI_TRANSFER_TIMEOUT
<< 2));
499 dbg("%s - port %d", __func__
, port
->number
);
504 dev
= port
->serial
->dev
;
505 tdev
= tport
->tp_tdev
;
507 /* only one open on any port on a device at a time */
508 if (mutex_lock_interruptible(&tdev
->td_open_close_lock
))
513 (tport
->tp_flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
515 port_number
= port
->number
- port
->serial
->minor
;
517 memset(&(tport
->tp_icount
), 0x00, sizeof(tport
->tp_icount
));
520 tport
->tp_shadow_mcr
|= (TI_MCR_RTS
| TI_MCR_DTR
);
522 /* start interrupt urb the first time a port is opened on this device */
523 if (tdev
->td_open_port_count
== 0) {
524 dbg("%s - start interrupt in urb", __func__
);
525 urb
= tdev
->td_serial
->port
[0]->interrupt_in_urb
;
527 dev_err(&port
->dev
, "%s - no interrupt urb\n",
532 urb
->complete
= ti_interrupt_callback
;
535 status
= usb_submit_urb(urb
, GFP_KERNEL
);
538 "%s - submit interrupt urb failed, %d\n",
545 ti_set_termios(tty
, port
, tty
->termios
);
547 dbg("%s - sending TI_OPEN_PORT", __func__
);
548 status
= ti_command_out_sync(tdev
, TI_OPEN_PORT
,
549 (__u8
)(TI_UART1_PORT
+ port_number
), open_settings
, NULL
, 0);
551 dev_err(&port
->dev
, "%s - cannot send open command, %d\n",
556 dbg("%s - sending TI_START_PORT", __func__
);
557 status
= ti_command_out_sync(tdev
, TI_START_PORT
,
558 (__u8
)(TI_UART1_PORT
+ port_number
), 0, NULL
, 0);
560 dev_err(&port
->dev
, "%s - cannot send start command, %d\n",
565 dbg("%s - sending TI_PURGE_PORT", __func__
);
566 status
= ti_command_out_sync(tdev
, TI_PURGE_PORT
,
567 (__u8
)(TI_UART1_PORT
+ port_number
), TI_PURGE_INPUT
, NULL
, 0);
569 dev_err(&port
->dev
, "%s - cannot clear input buffers, %d\n",
573 status
= ti_command_out_sync(tdev
, TI_PURGE_PORT
,
574 (__u8
)(TI_UART1_PORT
+ port_number
), TI_PURGE_OUTPUT
, NULL
, 0);
576 dev_err(&port
->dev
, "%s - cannot clear output buffers, %d\n",
581 /* reset the data toggle on the bulk endpoints to work around bug in
582 * host controllers where things get out of sync some times */
583 usb_clear_halt(dev
, port
->write_urb
->pipe
);
584 usb_clear_halt(dev
, port
->read_urb
->pipe
);
587 ti_set_termios(tty
, port
, tty
->termios
);
589 dbg("%s - sending TI_OPEN_PORT (2)", __func__
);
590 status
= ti_command_out_sync(tdev
, TI_OPEN_PORT
,
591 (__u8
)(TI_UART1_PORT
+ port_number
), open_settings
, NULL
, 0);
593 dev_err(&port
->dev
, "%s - cannot send open command (2), %d\n",
598 dbg("%s - sending TI_START_PORT (2)", __func__
);
599 status
= ti_command_out_sync(tdev
, TI_START_PORT
,
600 (__u8
)(TI_UART1_PORT
+ port_number
), 0, NULL
, 0);
602 dev_err(&port
->dev
, "%s - cannot send start command (2), %d\n",
608 dbg("%s - start read urb", __func__
);
609 urb
= port
->read_urb
;
611 dev_err(&port
->dev
, "%s - no read urb\n", __func__
);
615 tport
->tp_read_urb_state
= TI_READ_URB_RUNNING
;
616 urb
->complete
= ti_bulk_in_callback
;
617 urb
->context
= tport
;
619 status
= usb_submit_urb(urb
, GFP_KERNEL
);
621 dev_err(&port
->dev
, "%s - submit read urb failed, %d\n",
626 tport
->tp_is_open
= 1;
627 ++tdev
->td_open_port_count
;
632 if (tdev
->td_open_port_count
== 0)
633 usb_kill_urb(port
->serial
->port
[0]->interrupt_in_urb
);
635 mutex_unlock(&tdev
->td_open_close_lock
);
636 dbg("%s - exit %d", __func__
, status
);
641 static void ti_close(struct tty_struct
*tty
, struct usb_serial_port
*port
,
644 struct ti_device
*tdev
;
645 struct ti_port
*tport
;
650 dbg("%s - port %d", __func__
, port
->number
);
652 tdev
= usb_get_serial_data(port
->serial
);
653 tport
= usb_get_serial_port_data(port
);
654 if (tdev
== NULL
|| tport
== NULL
)
657 tport
->tp_is_open
= 0;
659 ti_drain(tport
, (tport
->tp_closing_wait
*HZ
)/100, 1);
661 usb_kill_urb(port
->read_urb
);
662 usb_kill_urb(port
->write_urb
);
663 tport
->tp_write_urb_in_use
= 0;
665 port_number
= port
->number
- port
->serial
->minor
;
667 dbg("%s - sending TI_CLOSE_PORT", __func__
);
668 status
= ti_command_out_sync(tdev
, TI_CLOSE_PORT
,
669 (__u8
)(TI_UART1_PORT
+ port_number
), 0, NULL
, 0);
672 "%s - cannot send close port command, %d\n"
675 /* if mutex_lock is interrupted, continue anyway */
676 do_unlock
= !mutex_lock_interruptible(&tdev
->td_open_close_lock
);
677 --tport
->tp_tdev
->td_open_port_count
;
678 if (tport
->tp_tdev
->td_open_port_count
<= 0) {
679 /* last port is closed, shut down interrupt urb */
680 usb_kill_urb(port
->serial
->port
[0]->interrupt_in_urb
);
681 tport
->tp_tdev
->td_open_port_count
= 0;
684 mutex_unlock(&tdev
->td_open_close_lock
);
686 dbg("%s - exit", __func__
);
690 static int ti_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
691 const unsigned char *data
, int count
)
693 struct ti_port
*tport
= usb_get_serial_port_data(port
);
696 dbg("%s - port %d", __func__
, port
->number
);
699 dbg("%s - write request of 0 bytes", __func__
);
703 if (tport
== NULL
|| !tport
->tp_is_open
)
706 spin_lock_irqsave(&tport
->tp_lock
, flags
);
707 count
= ti_buf_put(tport
->tp_write_buf
, data
, count
);
708 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
716 static int ti_write_room(struct tty_struct
*tty
)
718 struct usb_serial_port
*port
= tty
->driver_data
;
719 struct ti_port
*tport
= usb_get_serial_port_data(port
);
723 dbg("%s - port %d", __func__
, port
->number
);
728 spin_lock_irqsave(&tport
->tp_lock
, flags
);
729 room
= ti_buf_space_avail(tport
->tp_write_buf
);
730 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
732 dbg("%s - returns %d", __func__
, room
);
737 static int ti_chars_in_buffer(struct tty_struct
*tty
)
739 struct usb_serial_port
*port
= tty
->driver_data
;
740 struct ti_port
*tport
= usb_get_serial_port_data(port
);
744 dbg("%s - port %d", __func__
, port
->number
);
749 spin_lock_irqsave(&tport
->tp_lock
, flags
);
750 chars
= ti_buf_data_avail(tport
->tp_write_buf
);
751 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
753 dbg("%s - returns %d", __func__
, chars
);
758 static void ti_throttle(struct tty_struct
*tty
)
760 struct usb_serial_port
*port
= tty
->driver_data
;
761 struct ti_port
*tport
= usb_get_serial_port_data(port
);
763 dbg("%s - port %d", __func__
, port
->number
);
768 if (I_IXOFF(tty
) || C_CRTSCTS(tty
))
769 ti_stop_read(tport
, tty
);
774 static void ti_unthrottle(struct tty_struct
*tty
)
776 struct usb_serial_port
*port
= tty
->driver_data
;
777 struct ti_port
*tport
= usb_get_serial_port_data(port
);
780 dbg("%s - port %d", __func__
, port
->number
);
785 if (I_IXOFF(tty
) || C_CRTSCTS(tty
)) {
786 status
= ti_restart_read(tport
, tty
);
788 dev_err(&port
->dev
, "%s - cannot restart read, %d\n",
794 static int ti_ioctl(struct tty_struct
*tty
, struct file
*file
,
795 unsigned int cmd
, unsigned long arg
)
797 struct usb_serial_port
*port
= tty
->driver_data
;
798 struct ti_port
*tport
= usb_get_serial_port_data(port
);
799 struct async_icount cnow
;
800 struct async_icount cprev
;
802 dbg("%s - port %d, cmd = 0x%04X", __func__
, port
->number
, cmd
);
809 dbg("%s - (%d) TIOCGSERIAL", __func__
, port
->number
);
810 return ti_get_serial_info(tport
,
811 (struct serial_struct __user
*)arg
);
813 dbg("%s - (%d) TIOCSSERIAL", __func__
, port
->number
);
814 return ti_set_serial_info(tty
, tport
,
815 (struct serial_struct __user
*)arg
);
817 dbg("%s - (%d) TIOCMIWAIT", __func__
, port
->number
);
818 cprev
= tport
->tp_icount
;
820 interruptible_sleep_on(&tport
->tp_msr_wait
);
821 if (signal_pending(current
))
823 cnow
= tport
->tp_icount
;
824 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
825 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
826 return -EIO
; /* no change => error */
827 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
828 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
829 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
830 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
)))
836 dbg("%s - (%d) TIOCGICOUNT RX=%d, TX=%d",
837 __func__
, port
->number
,
838 tport
->tp_icount
.rx
, tport
->tp_icount
.tx
);
839 if (copy_to_user((void __user
*)arg
, &tport
->tp_icount
,
840 sizeof(tport
->tp_icount
)))
848 static void ti_set_termios(struct tty_struct
*tty
,
849 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
851 struct ti_port
*tport
= usb_get_serial_port_data(port
);
852 struct ti_uart_config
*config
;
853 tcflag_t cflag
, iflag
;
856 int port_number
= port
->number
- port
->serial
->minor
;
859 dbg("%s - port %d", __func__
, port
->number
);
861 cflag
= tty
->termios
->c_cflag
;
862 iflag
= tty
->termios
->c_iflag
;
864 dbg("%s - cflag %08x, iflag %08x", __func__
, cflag
, iflag
);
865 dbg("%s - old clfag %08x, old iflag %08x", __func__
,
866 old_termios
->c_cflag
, old_termios
->c_iflag
);
871 config
= kmalloc(sizeof(*config
), GFP_KERNEL
);
873 dev_err(&port
->dev
, "%s - out of memory\n", __func__
);
879 /* these flags must be set */
880 config
->wFlags
|= TI_UART_ENABLE_MS_INTS
;
881 config
->wFlags
|= TI_UART_ENABLE_AUTO_START_DMA
;
882 config
->bUartMode
= (__u8
)(tport
->tp_uart_mode
);
884 switch (cflag
& CSIZE
) {
886 config
->bDataBits
= TI_UART_5_DATA_BITS
;
889 config
->bDataBits
= TI_UART_6_DATA_BITS
;
892 config
->bDataBits
= TI_UART_7_DATA_BITS
;
896 config
->bDataBits
= TI_UART_8_DATA_BITS
;
900 /* CMSPAR isn't supported by this driver */
901 tty
->termios
->c_cflag
&= ~CMSPAR
;
903 if (cflag
& PARENB
) {
904 if (cflag
& PARODD
) {
905 config
->wFlags
|= TI_UART_ENABLE_PARITY_CHECKING
;
906 config
->bParity
= TI_UART_ODD_PARITY
;
908 config
->wFlags
|= TI_UART_ENABLE_PARITY_CHECKING
;
909 config
->bParity
= TI_UART_EVEN_PARITY
;
912 config
->wFlags
&= ~TI_UART_ENABLE_PARITY_CHECKING
;
913 config
->bParity
= TI_UART_NO_PARITY
;
917 config
->bStopBits
= TI_UART_2_STOP_BITS
;
919 config
->bStopBits
= TI_UART_1_STOP_BITS
;
921 if (cflag
& CRTSCTS
) {
922 /* RTS flow control must be off to drop RTS for baud rate B0 */
923 if ((cflag
& CBAUD
) != B0
)
924 config
->wFlags
|= TI_UART_ENABLE_RTS_IN
;
925 config
->wFlags
|= TI_UART_ENABLE_CTS_OUT
;
928 ti_restart_read(tport
, tty
);
931 if (I_IXOFF(tty
) || I_IXON(tty
)) {
932 config
->cXon
= START_CHAR(tty
);
933 config
->cXoff
= STOP_CHAR(tty
);
936 config
->wFlags
|= TI_UART_ENABLE_X_IN
;
938 ti_restart_read(tport
, tty
);
941 config
->wFlags
|= TI_UART_ENABLE_X_OUT
;
944 baud
= tty_get_baud_rate(tty
);
947 if (tport
->tp_tdev
->td_is_3410
)
948 config
->wBaudRate
= (__u16
)((923077 + baud
/2) / baud
);
950 config
->wBaudRate
= (__u16
)((461538 + baud
/2) / baud
);
952 /* FIXME: Should calculate resulting baud here and report it back */
953 if ((cflag
& CBAUD
) != B0
)
954 tty_encode_baud_rate(tty
, baud
, baud
);
956 dbg("%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d",
957 __func__
, baud
, config
->wBaudRate
, config
->wFlags
, config
->bDataBits
, config
->bParity
, config
->bStopBits
, config
->cXon
, config
->cXoff
, config
->bUartMode
);
959 cpu_to_be16s(&config
->wBaudRate
);
960 cpu_to_be16s(&config
->wFlags
);
962 status
= ti_command_out_sync(tport
->tp_tdev
, TI_SET_CONFIG
,
963 (__u8
)(TI_UART1_PORT
+ port_number
), 0, (__u8
*)config
,
966 dev_err(&port
->dev
, "%s - cannot set config on port %d, %d\n",
967 __func__
, port_number
, status
);
969 /* SET_CONFIG asserts RTS and DTR, reset them correctly */
970 mcr
= tport
->tp_shadow_mcr
;
971 /* if baud rate is B0, clear RTS and DTR */
972 if ((cflag
& CBAUD
) == B0
)
973 mcr
&= ~(TI_MCR_DTR
| TI_MCR_RTS
);
974 status
= ti_set_mcr(tport
, mcr
);
977 "%s - cannot set modem control on port %d, %d\n",
978 __func__
, port_number
, status
);
984 static int ti_tiocmget(struct tty_struct
*tty
, struct file
*file
)
986 struct usb_serial_port
*port
= tty
->driver_data
;
987 struct ti_port
*tport
= usb_get_serial_port_data(port
);
993 dbg("%s - port %d", __func__
, port
->number
);
998 spin_lock_irqsave(&tport
->tp_lock
, flags
);
1000 mcr
= tport
->tp_shadow_mcr
;
1001 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1003 result
= ((mcr
& TI_MCR_DTR
) ? TIOCM_DTR
: 0)
1004 | ((mcr
& TI_MCR_RTS
) ? TIOCM_RTS
: 0)
1005 | ((mcr
& TI_MCR_LOOP
) ? TIOCM_LOOP
: 0)
1006 | ((msr
& TI_MSR_CTS
) ? TIOCM_CTS
: 0)
1007 | ((msr
& TI_MSR_CD
) ? TIOCM_CAR
: 0)
1008 | ((msr
& TI_MSR_RI
) ? TIOCM_RI
: 0)
1009 | ((msr
& TI_MSR_DSR
) ? TIOCM_DSR
: 0);
1011 dbg("%s - 0x%04X", __func__
, result
);
1017 static int ti_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1018 unsigned int set
, unsigned int clear
)
1020 struct usb_serial_port
*port
= tty
->driver_data
;
1021 struct ti_port
*tport
= usb_get_serial_port_data(port
);
1023 unsigned long flags
;
1025 dbg("%s - port %d", __func__
, port
->number
);
1030 spin_lock_irqsave(&tport
->tp_lock
, flags
);
1031 mcr
= tport
->tp_shadow_mcr
;
1033 if (set
& TIOCM_RTS
)
1035 if (set
& TIOCM_DTR
)
1037 if (set
& TIOCM_LOOP
)
1040 if (clear
& TIOCM_RTS
)
1042 if (clear
& TIOCM_DTR
)
1044 if (clear
& TIOCM_LOOP
)
1045 mcr
&= ~TI_MCR_LOOP
;
1046 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1048 return ti_set_mcr(tport
, mcr
);
1052 static void ti_break(struct tty_struct
*tty
, int break_state
)
1054 struct usb_serial_port
*port
= tty
->driver_data
;
1055 struct ti_port
*tport
= usb_get_serial_port_data(port
);
1058 dbg("%s - state = %d", __func__
, break_state
);
1063 ti_drain(tport
, (tport
->tp_closing_wait
*HZ
)/100, 0);
1065 status
= ti_write_byte(tport
->tp_tdev
,
1066 tport
->tp_uart_base_addr
+ TI_UART_OFFSET_LCR
,
1067 TI_LCR_BREAK
, break_state
== -1 ? TI_LCR_BREAK
: 0);
1070 dbg("%s - error setting break, %d", __func__
, status
);
1074 static void ti_interrupt_callback(struct urb
*urb
)
1076 struct ti_device
*tdev
= urb
->context
;
1077 struct usb_serial_port
*port
;
1078 struct usb_serial
*serial
= tdev
->td_serial
;
1079 struct ti_port
*tport
;
1080 struct device
*dev
= &urb
->dev
->dev
;
1081 unsigned char *data
= urb
->transfer_buffer
;
1082 int length
= urb
->actual_length
;
1085 int status
= urb
->status
;
1089 dbg("%s", __func__
);
1097 dbg("%s - urb shutting down, %d", __func__
, status
);
1098 tdev
->td_urb_error
= 1;
1101 dev_err(dev
, "%s - nonzero urb status, %d\n",
1103 tdev
->td_urb_error
= 1;
1108 dbg("%s - bad packet size, %d", __func__
, length
);
1112 if (data
[0] == TI_CODE_HARDWARE_ERROR
) {
1113 dev_err(dev
, "%s - hardware error, %d\n", __func__
, data
[1]);
1117 port_number
= TI_GET_PORT_FROM_CODE(data
[0]);
1118 function
= TI_GET_FUNC_FROM_CODE(data
[0]);
1120 dbg("%s - port_number %d, function %d, data 0x%02X",
1121 __func__
, port_number
, function
, data
[1]);
1123 if (port_number
>= serial
->num_ports
) {
1124 dev_err(dev
, "%s - bad port number, %d\n",
1125 __func__
, port_number
);
1129 port
= serial
->port
[port_number
];
1131 tport
= usb_get_serial_port_data(port
);
1136 case TI_CODE_DATA_ERROR
:
1137 dev_err(dev
, "%s - DATA ERROR, port %d, data 0x%02X\n",
1138 __func__
, port_number
, data
[1]);
1141 case TI_CODE_MODEM_STATUS
:
1143 dbg("%s - port %d, msr 0x%02X", __func__
, port_number
, msr
);
1144 ti_handle_new_msr(tport
, msr
);
1148 dev_err(dev
, "%s - unknown interrupt code, 0x%02X\n",
1154 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1156 dev_err(dev
, "%s - resubmit interrupt urb failed, %d\n",
1161 static void ti_bulk_in_callback(struct urb
*urb
)
1163 struct ti_port
*tport
= urb
->context
;
1164 struct usb_serial_port
*port
= tport
->tp_port
;
1165 struct device
*dev
= &urb
->dev
->dev
;
1166 int status
= urb
->status
;
1168 struct tty_struct
*tty
;
1170 dbg("%s", __func__
);
1178 dbg("%s - urb shutting down, %d", __func__
, status
);
1179 tport
->tp_tdev
->td_urb_error
= 1;
1180 wake_up_interruptible(&tport
->tp_write_wait
);
1183 dev_err(dev
, "%s - nonzero urb status, %d\n",
1185 tport
->tp_tdev
->td_urb_error
= 1;
1186 wake_up_interruptible(&tport
->tp_write_wait
);
1189 if (status
== -EPIPE
)
1193 dev_err(dev
, "%s - stopping read!\n", __func__
);
1197 tty
= tty_port_tty_get(&port
->port
);
1198 if (tty
&& urb
->actual_length
) {
1199 usb_serial_debug_data(debug
, dev
, __func__
,
1200 urb
->actual_length
, urb
->transfer_buffer
);
1202 if (!tport
->tp_is_open
)
1203 dbg("%s - port closed, dropping data", __func__
);
1205 ti_recv(&urb
->dev
->dev
, tty
,
1206 urb
->transfer_buffer
,
1207 urb
->actual_length
);
1209 spin_lock(&tport
->tp_lock
);
1210 tport
->tp_icount
.rx
+= urb
->actual_length
;
1211 spin_unlock(&tport
->tp_lock
);
1216 /* continue to read unless stopping */
1217 spin_lock(&tport
->tp_lock
);
1218 if (tport
->tp_read_urb_state
== TI_READ_URB_RUNNING
) {
1219 urb
->dev
= port
->serial
->dev
;
1220 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1221 } else if (tport
->tp_read_urb_state
== TI_READ_URB_STOPPING
) {
1222 tport
->tp_read_urb_state
= TI_READ_URB_STOPPED
;
1224 spin_unlock(&tport
->tp_lock
);
1226 dev_err(dev
, "%s - resubmit read urb failed, %d\n",
1231 static void ti_bulk_out_callback(struct urb
*urb
)
1233 struct ti_port
*tport
= urb
->context
;
1234 struct usb_serial_port
*port
= tport
->tp_port
;
1235 struct device
*dev
= &urb
->dev
->dev
;
1236 int status
= urb
->status
;
1238 dbg("%s - port %d", __func__
, port
->number
);
1240 tport
->tp_write_urb_in_use
= 0;
1248 dbg("%s - urb shutting down, %d", __func__
, status
);
1249 tport
->tp_tdev
->td_urb_error
= 1;
1250 wake_up_interruptible(&tport
->tp_write_wait
);
1253 dev_err(dev
, "%s - nonzero urb status, %d\n",
1255 tport
->tp_tdev
->td_urb_error
= 1;
1256 wake_up_interruptible(&tport
->tp_write_wait
);
1259 /* send any buffered data */
1264 static void ti_recv(struct device
*dev
, struct tty_struct
*tty
,
1265 unsigned char *data
, int length
)
1270 cnt
= tty_buffer_request_room(tty
, length
);
1272 dev_err(dev
, "%s - dropping data, %d bytes lost\n",
1273 __func__
, length
- cnt
);
1277 tty_insert_flip_string(tty
, data
, cnt
);
1278 tty_flip_buffer_push(tty
);
1281 } while (length
> 0);
1286 static void ti_send(struct ti_port
*tport
)
1289 struct usb_serial_port
*port
= tport
->tp_port
;
1290 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
); /* FIXME */
1291 unsigned long flags
;
1294 dbg("%s - port %d", __func__
, port
->number
);
1296 spin_lock_irqsave(&tport
->tp_lock
, flags
);
1298 if (tport
->tp_write_urb_in_use
)
1301 count
= ti_buf_get(tport
->tp_write_buf
,
1302 port
->write_urb
->transfer_buffer
,
1303 port
->bulk_out_size
);
1308 tport
->tp_write_urb_in_use
= 1;
1310 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1312 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
,
1313 port
->write_urb
->transfer_buffer
);
1315 usb_fill_bulk_urb(port
->write_urb
, port
->serial
->dev
,
1316 usb_sndbulkpipe(port
->serial
->dev
,
1317 port
->bulk_out_endpointAddress
),
1318 port
->write_urb
->transfer_buffer
, count
,
1319 ti_bulk_out_callback
, tport
);
1321 result
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
1323 dev_err(&port
->dev
, "%s - submit write urb failed, %d\n",
1325 tport
->tp_write_urb_in_use
= 0;
1326 /* TODO: reschedule ti_send */
1328 spin_lock_irqsave(&tport
->tp_lock
, flags
);
1329 tport
->tp_icount
.tx
+= count
;
1330 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1333 /* more room in the buffer for new writes, wakeup */
1337 wake_up_interruptible(&tport
->tp_write_wait
);
1340 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1346 static int ti_set_mcr(struct ti_port
*tport
, unsigned int mcr
)
1348 unsigned long flags
;
1351 status
= ti_write_byte(tport
->tp_tdev
,
1352 tport
->tp_uart_base_addr
+ TI_UART_OFFSET_MCR
,
1353 TI_MCR_RTS
| TI_MCR_DTR
| TI_MCR_LOOP
, mcr
);
1355 spin_lock_irqsave(&tport
->tp_lock
, flags
);
1357 tport
->tp_shadow_mcr
= mcr
;
1358 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1364 static int ti_get_lsr(struct ti_port
*tport
)
1367 struct ti_device
*tdev
= tport
->tp_tdev
;
1368 struct usb_serial_port
*port
= tport
->tp_port
;
1369 int port_number
= port
->number
- port
->serial
->minor
;
1370 struct ti_port_status
*data
;
1372 dbg("%s - port %d", __func__
, port
->number
);
1374 size
= sizeof(struct ti_port_status
);
1375 data
= kmalloc(size
, GFP_KERNEL
);
1377 dev_err(&port
->dev
, "%s - out of memory\n", __func__
);
1381 status
= ti_command_in_sync(tdev
, TI_GET_PORT_STATUS
,
1382 (__u8
)(TI_UART1_PORT
+port_number
), 0, (__u8
*)data
, size
);
1385 "%s - get port status command failed, %d\n",
1390 dbg("%s - lsr 0x%02X", __func__
, data
->bLSR
);
1392 tport
->tp_lsr
= data
->bLSR
;
1400 static int ti_get_serial_info(struct ti_port
*tport
,
1401 struct serial_struct __user
*ret_arg
)
1403 struct usb_serial_port
*port
= tport
->tp_port
;
1404 struct serial_struct ret_serial
;
1409 memset(&ret_serial
, 0, sizeof(ret_serial
));
1411 ret_serial
.type
= PORT_16550A
;
1412 ret_serial
.line
= port
->serial
->minor
;
1413 ret_serial
.port
= port
->number
- port
->serial
->minor
;
1414 ret_serial
.flags
= tport
->tp_flags
;
1415 ret_serial
.xmit_fifo_size
= TI_WRITE_BUF_SIZE
;
1416 ret_serial
.baud_base
= tport
->tp_tdev
->td_is_3410
? 921600 : 460800;
1417 ret_serial
.closing_wait
= tport
->tp_closing_wait
;
1419 if (copy_to_user(ret_arg
, &ret_serial
, sizeof(*ret_arg
)))
1426 static int ti_set_serial_info(struct tty_struct
*tty
, struct ti_port
*tport
,
1427 struct serial_struct __user
*new_arg
)
1429 struct serial_struct new_serial
;
1431 if (copy_from_user(&new_serial
, new_arg
, sizeof(new_serial
)))
1434 tport
->tp_flags
= new_serial
.flags
& TI_SET_SERIAL_FLAGS
;
1435 tty
->low_latency
= (tport
->tp_flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
1436 tport
->tp_closing_wait
= new_serial
.closing_wait
;
1442 static void ti_handle_new_msr(struct ti_port
*tport
, __u8 msr
)
1444 struct async_icount
*icount
;
1445 struct tty_struct
*tty
;
1446 unsigned long flags
;
1448 dbg("%s - msr 0x%02X", __func__
, msr
);
1450 if (msr
& TI_MSR_DELTA_MASK
) {
1451 spin_lock_irqsave(&tport
->tp_lock
, flags
);
1452 icount
= &tport
->tp_icount
;
1453 if (msr
& TI_MSR_DELTA_CTS
)
1455 if (msr
& TI_MSR_DELTA_DSR
)
1457 if (msr
& TI_MSR_DELTA_CD
)
1459 if (msr
& TI_MSR_DELTA_RI
)
1461 wake_up_interruptible(&tport
->tp_msr_wait
);
1462 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1465 tport
->tp_msr
= msr
& TI_MSR_MASK
;
1467 /* handle CTS flow control */
1468 tty
= tty_port_tty_get(&tport
->tp_port
->port
);
1469 if (tty
&& C_CRTSCTS(tty
)) {
1470 if (msr
& TI_MSR_CTS
) {
1471 tty
->hw_stopped
= 0;
1474 tty
->hw_stopped
= 1;
1481 static void ti_drain(struct ti_port
*tport
, unsigned long timeout
, int flush
)
1483 struct ti_device
*tdev
= tport
->tp_tdev
;
1484 struct usb_serial_port
*port
= tport
->tp_port
;
1487 dbg("%s - port %d", __func__
, port
->number
);
1489 spin_lock_irq(&tport
->tp_lock
);
1491 /* wait for data to drain from the buffer */
1492 tdev
->td_urb_error
= 0;
1493 init_waitqueue_entry(&wait
, current
);
1494 add_wait_queue(&tport
->tp_write_wait
, &wait
);
1496 set_current_state(TASK_INTERRUPTIBLE
);
1497 if (ti_buf_data_avail(tport
->tp_write_buf
) == 0
1498 || timeout
== 0 || signal_pending(current
)
1499 || tdev
->td_urb_error
1500 || port
->serial
->disconnected
) /* disconnect */
1502 spin_unlock_irq(&tport
->tp_lock
);
1503 timeout
= schedule_timeout(timeout
);
1504 spin_lock_irq(&tport
->tp_lock
);
1506 set_current_state(TASK_RUNNING
);
1507 remove_wait_queue(&tport
->tp_write_wait
, &wait
);
1509 /* flush any remaining data in the buffer */
1511 ti_buf_clear(tport
->tp_write_buf
);
1513 spin_unlock_irq(&tport
->tp_lock
);
1515 mutex_lock(&port
->serial
->disc_mutex
);
1516 /* wait for data to drain from the device */
1517 /* wait for empty tx register, plus 20 ms */
1519 tport
->tp_lsr
&= ~TI_LSR_TX_EMPTY
;
1520 while ((long)(jiffies
- timeout
) < 0 && !signal_pending(current
)
1521 && !(tport
->tp_lsr
&TI_LSR_TX_EMPTY
) && !tdev
->td_urb_error
1522 && !port
->serial
->disconnected
) {
1523 if (ti_get_lsr(tport
))
1525 mutex_unlock(&port
->serial
->disc_mutex
);
1526 msleep_interruptible(20);
1527 mutex_lock(&port
->serial
->disc_mutex
);
1529 mutex_unlock(&port
->serial
->disc_mutex
);
1533 static void ti_stop_read(struct ti_port
*tport
, struct tty_struct
*tty
)
1535 unsigned long flags
;
1537 spin_lock_irqsave(&tport
->tp_lock
, flags
);
1539 if (tport
->tp_read_urb_state
== TI_READ_URB_RUNNING
)
1540 tport
->tp_read_urb_state
= TI_READ_URB_STOPPING
;
1542 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1546 static int ti_restart_read(struct ti_port
*tport
, struct tty_struct
*tty
)
1550 unsigned long flags
;
1552 spin_lock_irqsave(&tport
->tp_lock
, flags
);
1554 if (tport
->tp_read_urb_state
== TI_READ_URB_STOPPED
) {
1555 tport
->tp_read_urb_state
= TI_READ_URB_RUNNING
;
1556 urb
= tport
->tp_port
->read_urb
;
1557 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1558 urb
->complete
= ti_bulk_in_callback
;
1559 urb
->context
= tport
;
1560 urb
->dev
= tport
->tp_port
->serial
->dev
;
1561 status
= usb_submit_urb(urb
, GFP_KERNEL
);
1563 tport
->tp_read_urb_state
= TI_READ_URB_RUNNING
;
1564 spin_unlock_irqrestore(&tport
->tp_lock
, flags
);
1571 static int ti_command_out_sync(struct ti_device
*tdev
, __u8 command
,
1572 __u16 moduleid
, __u16 value
, __u8
*data
, int size
)
1576 status
= usb_control_msg(tdev
->td_serial
->dev
,
1577 usb_sndctrlpipe(tdev
->td_serial
->dev
, 0), command
,
1578 (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_OUT
),
1579 value
, moduleid
, data
, size
, 1000);
1591 static int ti_command_in_sync(struct ti_device
*tdev
, __u8 command
,
1592 __u16 moduleid
, __u16 value
, __u8
*data
, int size
)
1596 status
= usb_control_msg(tdev
->td_serial
->dev
,
1597 usb_rcvctrlpipe(tdev
->td_serial
->dev
, 0), command
,
1598 (USB_TYPE_VENDOR
| USB_RECIP_DEVICE
| USB_DIR_IN
),
1599 value
, moduleid
, data
, size
, 1000);
1611 static int ti_write_byte(struct ti_device
*tdev
, unsigned long addr
,
1612 __u8 mask
, __u8 byte
)
1616 struct ti_write_data_bytes
*data
;
1617 struct device
*dev
= &tdev
->td_serial
->dev
->dev
;
1619 dbg("%s - addr 0x%08lX, mask 0x%02X, byte 0x%02X",
1620 __func__
, addr
, mask
, byte
);
1622 size
= sizeof(struct ti_write_data_bytes
) + 2;
1623 data
= kmalloc(size
, GFP_KERNEL
);
1625 dev_err(dev
, "%s - out of memory\n", __func__
);
1629 data
->bAddrType
= TI_RW_DATA_ADDR_XDATA
;
1630 data
->bDataType
= TI_RW_DATA_BYTE
;
1631 data
->bDataCounter
= 1;
1632 data
->wBaseAddrHi
= cpu_to_be16(addr
>>16);
1633 data
->wBaseAddrLo
= cpu_to_be16(addr
);
1634 data
->bData
[0] = mask
;
1635 data
->bData
[1] = byte
;
1637 status
= ti_command_out_sync(tdev
, TI_WRITE_DATA
, TI_RAM_PORT
, 0,
1638 (__u8
*)data
, size
);
1641 dev_err(dev
, "%s - failed, %d\n", __func__
, status
);
1648 static int ti_do_download(struct usb_device
*dev
, int pipe
,
1649 u8
*buffer
, int size
)
1654 struct ti_firmware_header
*header
;
1658 for (pos
= sizeof(struct ti_firmware_header
); pos
< size
; pos
++)
1659 cs
= (__u8
)(cs
+ buffer
[pos
]);
1661 header
= (struct ti_firmware_header
*)buffer
;
1662 header
->wLength
= cpu_to_le16((__u16
)(size
1663 - sizeof(struct ti_firmware_header
)));
1664 header
->bCheckSum
= cs
;
1666 dbg("%s - downloading firmware", __func__
);
1667 for (pos
= 0; pos
< size
; pos
+= done
) {
1668 len
= min(size
- pos
, TI_DOWNLOAD_MAX_PACKET_SIZE
);
1669 status
= usb_bulk_msg(dev
, pipe
, buffer
+ pos
, len
,
1677 static int ti_download_firmware(struct ti_device
*tdev
)
1682 struct usb_device
*dev
= tdev
->td_serial
->dev
;
1683 unsigned int pipe
= usb_sndbulkpipe(dev
,
1684 tdev
->td_serial
->port
[0]->bulk_out_endpointAddress
);
1685 const struct firmware
*fw_p
;
1688 /* try ID specific firmware first, then try generic firmware */
1689 sprintf(buf
, "ti_usb-v%04x-p%04x.fw", dev
->descriptor
.idVendor
,
1690 dev
->descriptor
.idProduct
);
1691 if ((status
= request_firmware(&fw_p
, buf
, &dev
->dev
)) != 0) {
1692 if (tdev
->td_is_3410
)
1693 strcpy(buf
, "ti_3410.fw");
1695 strcpy(buf
, "ti_5052.fw");
1696 status
= request_firmware(&fw_p
, buf
, &dev
->dev
);
1699 dev_err(&dev
->dev
, "%s - firmware not found\n", __func__
);
1702 if (fw_p
->size
> TI_FIRMWARE_BUF_SIZE
) {
1703 dev_err(&dev
->dev
, "%s - firmware too large\n", __func__
);
1707 buffer_size
= TI_FIRMWARE_BUF_SIZE
+ sizeof(struct ti_firmware_header
);
1708 buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
1710 memcpy(buffer
, fw_p
->data
, fw_p
->size
);
1711 memset(buffer
+ fw_p
->size
, 0xff, buffer_size
- fw_p
->size
);
1712 status
= ti_do_download(dev
, pipe
, buffer
, fw_p
->size
);
1717 release_firmware(fw_p
);
1719 dev_err(&dev
->dev
, "%s - error downloading firmware, %d\n",
1724 dbg("%s - download successful", __func__
);
1730 /* Circular Buffer Functions */
1735 * Allocate a circular buffer and all associated memory.
1738 static struct circ_buf
*ti_buf_alloc(void)
1740 struct circ_buf
*cb
;
1742 cb
= kmalloc(sizeof(struct circ_buf
), GFP_KERNEL
);
1746 cb
->buf
= kmalloc(TI_WRITE_BUF_SIZE
, GFP_KERNEL
);
1747 if (cb
->buf
== NULL
) {
1761 * Free the buffer and all associated memory.
1764 static void ti_buf_free(struct circ_buf
*cb
)
1774 * Clear out all data in the circular buffer.
1777 static void ti_buf_clear(struct circ_buf
*cb
)
1779 cb
->head
= cb
->tail
= 0;
1786 * Return the number of bytes of data available in the circular
1790 static int ti_buf_data_avail(struct circ_buf
*cb
)
1792 return CIRC_CNT(cb
->head
, cb
->tail
, TI_WRITE_BUF_SIZE
);
1797 * ti_buf_space_avail
1799 * Return the number of bytes of space available in the circular
1803 static int ti_buf_space_avail(struct circ_buf
*cb
)
1805 return CIRC_SPACE(cb
->head
, cb
->tail
, TI_WRITE_BUF_SIZE
);
1812 * Copy data data from a user buffer and put it into the circular buffer.
1813 * Restrict to the amount of space available.
1815 * Return the number of bytes copied.
1818 static int ti_buf_put(struct circ_buf
*cb
, const char *buf
, int count
)
1823 c
= CIRC_SPACE_TO_END(cb
->head
, cb
->tail
, TI_WRITE_BUF_SIZE
);
1828 memcpy(cb
->buf
+ cb
->head
, buf
, c
);
1829 cb
->head
= (cb
->head
+ c
) & (TI_WRITE_BUF_SIZE
-1);
1842 * Get data from the circular buffer and copy to the given buffer.
1843 * Restrict to the amount of data available.
1845 * Return the number of bytes copied.
1848 static int ti_buf_get(struct circ_buf
*cb
, char *buf
, int count
)
1853 c
= CIRC_CNT_TO_END(cb
->head
, cb
->tail
, TI_WRITE_BUF_SIZE
);
1858 memcpy(buf
, cb
->buf
+ cb
->tail
, c
);
1859 cb
->tail
= (cb
->tail
+ c
) & (TI_WRITE_BUF_SIZE
-1);