2 * Fintek F81232 USB to serial adaptor driver
4 * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
5 * Copyright (C) 2012 Linux Foundation
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/tty.h>
18 #include <linux/tty_driver.h>
19 #include <linux/tty_flip.h>
20 #include <linux/serial.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/spinlock.h>
24 #include <linux/uaccess.h>
25 #include <linux/usb.h>
26 #include <linux/usb/serial.h>
28 static const struct usb_device_id id_table
[] = {
29 { USB_DEVICE(0x1934, 0x0706) },
30 { } /* Terminating entry */
32 MODULE_DEVICE_TABLE(usb
, id_table
);
34 #define CONTROL_DTR 0x01
35 #define CONTROL_RTS 0x02
37 #define UART_STATE 0x08
38 #define UART_STATE_TRANSIENT_MASK 0x74
41 #define UART_BREAK_ERROR 0x04
42 #define UART_RING 0x08
43 #define UART_FRAME_ERROR 0x10
44 #define UART_PARITY_ERROR 0x20
45 #define UART_OVERRUN_ERROR 0x40
48 struct f81232_private
{
50 wait_queue_head_t delta_msr_wait
;
55 static void f81232_update_line_status(struct usb_serial_port
*port
,
57 unsigned int actual_length
)
61 static void f81232_read_int_callback(struct urb
*urb
)
63 struct usb_serial_port
*port
= urb
->context
;
64 unsigned char *data
= urb
->transfer_buffer
;
65 unsigned int actual_length
= urb
->actual_length
;
66 int status
= urb
->status
;
76 /* this urb is terminated, clean up */
77 dev_dbg(&port
->dev
, "%s - urb shutting down with status: %d\n",
81 dev_dbg(&port
->dev
, "%s - nonzero urb status received: %d\n",
86 usb_serial_debug_data(&port
->dev
, __func__
,
87 urb
->actual_length
, urb
->transfer_buffer
);
89 f81232_update_line_status(port
, data
, actual_length
);
92 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
94 dev_err(&urb
->dev
->dev
,
95 "%s - usb_submit_urb failed with result %d\n",
99 static void f81232_process_read_urb(struct urb
*urb
)
101 struct usb_serial_port
*port
= urb
->context
;
102 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
103 struct tty_struct
*tty
;
104 unsigned char *data
= urb
->transfer_buffer
;
105 char tty_flag
= TTY_NORMAL
;
110 /* update line status */
111 spin_lock_irqsave(&priv
->lock
, flags
);
112 line_status
= priv
->line_status
;
113 priv
->line_status
&= ~UART_STATE_TRANSIENT_MASK
;
114 spin_unlock_irqrestore(&priv
->lock
, flags
);
115 wake_up_interruptible(&priv
->delta_msr_wait
);
117 if (!urb
->actual_length
)
120 tty
= tty_port_tty_get(&port
->port
);
124 /* break takes precedence over parity, */
125 /* which takes precedence over framing errors */
126 if (line_status
& UART_BREAK_ERROR
)
127 tty_flag
= TTY_BREAK
;
128 else if (line_status
& UART_PARITY_ERROR
)
129 tty_flag
= TTY_PARITY
;
130 else if (line_status
& UART_FRAME_ERROR
)
131 tty_flag
= TTY_FRAME
;
132 dev_dbg(&port
->dev
, "%s - tty_flag = %d\n", __func__
, tty_flag
);
134 /* overrun is special, not associated with a char */
135 if (line_status
& UART_OVERRUN_ERROR
)
136 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
138 if (port
->port
.console
&& port
->sysrq
) {
139 for (i
= 0; i
< urb
->actual_length
; ++i
)
140 if (!usb_serial_handle_sysrq_char(port
, data
[i
]))
141 tty_insert_flip_char(tty
, data
[i
], tty_flag
);
143 tty_insert_flip_string_fixed_flag(tty
, data
, tty_flag
,
147 tty_flip_buffer_push(tty
);
151 static int set_control_lines(struct usb_device
*dev
, u8 value
)
153 /* FIXME - Stubbed out for now */
157 static void f81232_break_ctl(struct tty_struct
*tty
, int break_state
)
159 /* FIXME - Stubbed out for now */
162 * break_state = -1 to turn on break, and 0 to turn off break
163 * see drivers/char/tty_io.c to see it used.
164 * last_set_data_urb_value NEVER has the break bit set in it.
168 static void f81232_set_termios(struct tty_struct
*tty
,
169 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
171 /* FIXME - Stubbed out for now */
173 /* Don't change anything if nothing has changed */
174 if (!tty_termios_hw_change(tty
->termios
, old_termios
))
177 /* Do the real work here... */
180 static int f81232_tiocmget(struct tty_struct
*tty
)
182 /* FIXME - Stubbed out for now */
186 static int f81232_tiocmset(struct tty_struct
*tty
,
187 unsigned int set
, unsigned int clear
)
189 /* FIXME - Stubbed out for now */
193 static int f81232_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
195 struct ktermios tmp_termios
;
200 f81232_set_termios(tty
, port
, &tmp_termios
);
202 result
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
204 dev_err(&port
->dev
, "%s - failed submitting interrupt urb,"
205 " error %d\n", __func__
, result
);
209 result
= usb_serial_generic_open(tty
, port
);
211 usb_kill_urb(port
->interrupt_in_urb
);
215 port
->port
.drain_delay
= 256;
219 static void f81232_close(struct usb_serial_port
*port
)
221 usb_serial_generic_close(port
);
222 usb_kill_urb(port
->interrupt_in_urb
);
225 static void f81232_dtr_rts(struct usb_serial_port
*port
, int on
)
227 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
231 spin_lock_irqsave(&priv
->lock
, flags
);
232 /* Change DTR and RTS */
234 priv
->line_control
|= (CONTROL_DTR
| CONTROL_RTS
);
236 priv
->line_control
&= ~(CONTROL_DTR
| CONTROL_RTS
);
237 control
= priv
->line_control
;
238 spin_unlock_irqrestore(&priv
->lock
, flags
);
239 set_control_lines(port
->serial
->dev
, control
);
242 static int f81232_carrier_raised(struct usb_serial_port
*port
)
244 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
245 if (priv
->line_status
& UART_DCD
)
250 static int wait_modem_info(struct usb_serial_port
*port
, unsigned int arg
)
252 struct f81232_private
*priv
= usb_get_serial_port_data(port
);
254 unsigned int prevstatus
;
256 unsigned int changed
;
258 spin_lock_irqsave(&priv
->lock
, flags
);
259 prevstatus
= priv
->line_status
;
260 spin_unlock_irqrestore(&priv
->lock
, flags
);
263 interruptible_sleep_on(&priv
->delta_msr_wait
);
264 /* see if a signal did it */
265 if (signal_pending(current
))
268 spin_lock_irqsave(&priv
->lock
, flags
);
269 status
= priv
->line_status
;
270 spin_unlock_irqrestore(&priv
->lock
, flags
);
272 changed
= prevstatus
^ status
;
274 if (((arg
& TIOCM_RNG
) && (changed
& UART_RING
)) ||
275 ((arg
& TIOCM_DSR
) && (changed
& UART_DSR
)) ||
276 ((arg
& TIOCM_CD
) && (changed
& UART_DCD
)) ||
277 ((arg
& TIOCM_CTS
) && (changed
& UART_CTS
))) {
286 static int f81232_ioctl(struct tty_struct
*tty
,
287 unsigned int cmd
, unsigned long arg
)
289 struct serial_struct ser
;
290 struct usb_serial_port
*port
= tty
->driver_data
;
292 dev_dbg(&port
->dev
, "%s (%d) cmd = 0x%04x\n", __func__
,
297 memset(&ser
, 0, sizeof ser
);
298 ser
.type
= PORT_16654
;
299 ser
.line
= port
->serial
->minor
;
300 ser
.port
= port
->number
;
301 ser
.baud_base
= 460800;
303 if (copy_to_user((void __user
*)arg
, &ser
, sizeof ser
))
309 dev_dbg(&port
->dev
, "%s (%d) TIOCMIWAIT\n", __func__
,
311 return wait_modem_info(port
, arg
);
313 dev_dbg(&port
->dev
, "%s not supported = 0x%04x\n",
320 static int f81232_startup(struct usb_serial
*serial
)
322 struct f81232_private
*priv
;
325 for (i
= 0; i
< serial
->num_ports
; ++i
) {
326 priv
= kzalloc(sizeof(struct f81232_private
), GFP_KERNEL
);
329 spin_lock_init(&priv
->lock
);
330 init_waitqueue_head(&priv
->delta_msr_wait
);
331 usb_set_serial_port_data(serial
->port
[i
], priv
);
336 for (--i
; i
>= 0; --i
) {
337 priv
= usb_get_serial_port_data(serial
->port
[i
]);
339 usb_set_serial_port_data(serial
->port
[i
], NULL
);
344 static void f81232_release(struct usb_serial
*serial
)
347 struct f81232_private
*priv
;
349 for (i
= 0; i
< serial
->num_ports
; ++i
) {
350 priv
= usb_get_serial_port_data(serial
->port
[i
]);
355 static struct usb_serial_driver f81232_device
= {
357 .owner
= THIS_MODULE
,
360 .id_table
= id_table
,
363 .bulk_out_size
= 256,
365 .close
= f81232_close
,
366 .dtr_rts
= f81232_dtr_rts
,
367 .carrier_raised
= f81232_carrier_raised
,
368 .ioctl
= f81232_ioctl
,
369 .break_ctl
= f81232_break_ctl
,
370 .set_termios
= f81232_set_termios
,
371 .tiocmget
= f81232_tiocmget
,
372 .tiocmset
= f81232_tiocmset
,
373 .process_read_urb
= f81232_process_read_urb
,
374 .read_int_callback
= f81232_read_int_callback
,
375 .attach
= f81232_startup
,
376 .release
= f81232_release
,
379 static struct usb_serial_driver
* const serial_drivers
[] = {
384 module_usb_serial_driver(serial_drivers
, id_table
);
386 MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
387 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org");
388 MODULE_LICENSE("GPL v2");