blob: 68016e7a86f70846243f133d9972bde9e5cde880 [file] [log] [blame]
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -08001/*
2 * Fintek F81232 USB to serial adaptor driver
3 *
4 * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
5 * Copyright (C) 2012 Linux Foundation
6 *
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.
10 *
11 */
12
13#include <linux/kernel.h>
14#include <linux/errno.h>
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080015#include <linux/slab.h>
16#include <linux/tty.h>
17#include <linux/tty_driver.h>
18#include <linux/tty_flip.h>
19#include <linux/serial.h>
20#include <linux/module.h>
21#include <linux/moduleparam.h>
Peter Hung7139c932015-03-17 17:48:21 +080022#include <linux/mutex.h>
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080023#include <linux/uaccess.h>
24#include <linux/usb.h>
25#include <linux/usb/serial.h>
Peter Hung88850782015-03-17 17:48:20 +080026#include <linux/serial_reg.h>
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080027
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080028static const struct usb_device_id id_table[] = {
29 { USB_DEVICE(0x1934, 0x0706) },
30 { } /* Terminating entry */
31};
32MODULE_DEVICE_TABLE(usb, id_table);
33
Peter Hung8bb4ca62015-03-17 17:48:25 +080034/* Maximum baudrate for F81232 */
35#define F81232_MAX_BAUDRATE 115200
36
Peter Hung87fe5ad2015-03-17 17:48:22 +080037/* USB Control EP parameter */
38#define F81232_REGISTER_REQUEST 0xa0
39#define F81232_GET_REGISTER 0xc0
Peter Hung691545f2015-03-17 17:48:23 +080040#define F81232_SET_REGISTER 0x40
Peter Hung87fe5ad2015-03-17 17:48:22 +080041
42#define SERIAL_BASE_ADDRESS 0x0120
Peter Hung8bb4ca62015-03-17 17:48:25 +080043#define RECEIVE_BUFFER_REGISTER (0x00 + SERIAL_BASE_ADDRESS)
Peter Hung94f87302015-03-17 17:48:24 +080044#define INTERRUPT_ENABLE_REGISTER (0x01 + SERIAL_BASE_ADDRESS)
45#define FIFO_CONTROL_REGISTER (0x02 + SERIAL_BASE_ADDRESS)
Peter Hung8bb4ca62015-03-17 17:48:25 +080046#define LINE_CONTROL_REGISTER (0x03 + SERIAL_BASE_ADDRESS)
Peter Hung691545f2015-03-17 17:48:23 +080047#define MODEM_CONTROL_REGISTER (0x04 + SERIAL_BASE_ADDRESS)
Peter Hung87fe5ad2015-03-17 17:48:22 +080048#define MODEM_STATUS_REGISTER (0x06 + SERIAL_BASE_ADDRESS)
49
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080050struct f81232_private {
Peter Hung7139c932015-03-17 17:48:21 +080051 struct mutex lock;
Peter Hung691545f2015-03-17 17:48:23 +080052 u8 modem_control;
Peter Hungb830d072015-03-17 17:48:19 +080053 u8 modem_status;
Peter Hung87fe5ad2015-03-17 17:48:22 +080054 struct work_struct interrupt_work;
55 struct usb_serial_port *port;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080056};
57
Peter Hung8bb4ca62015-03-17 17:48:25 +080058static int calc_baud_divisor(speed_t baudrate)
59{
60 return DIV_ROUND_CLOSEST(F81232_MAX_BAUDRATE, baudrate);
61}
62
Peter Hung87fe5ad2015-03-17 17:48:22 +080063static int f81232_get_register(struct usb_serial_port *port, u16 reg, u8 *val)
64{
65 int status;
66 u8 *tmp;
67 struct usb_device *dev = port->serial->dev;
68
69 tmp = kmalloc(sizeof(*val), GFP_KERNEL);
70 if (!tmp)
71 return -ENOMEM;
72
73 status = usb_control_msg(dev,
74 usb_rcvctrlpipe(dev, 0),
75 F81232_REGISTER_REQUEST,
76 F81232_GET_REGISTER,
77 reg,
78 0,
79 tmp,
80 sizeof(*val),
81 USB_CTRL_GET_TIMEOUT);
82 if (status != sizeof(*val)) {
83 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
84
85 if (status < 0)
86 status = usb_translate_errors(status);
87 else
88 status = -EIO;
89 } else {
90 status = 0;
91 *val = *tmp;
92 }
93
94 kfree(tmp);
95 return status;
96}
97
Peter Hung691545f2015-03-17 17:48:23 +080098static int f81232_set_register(struct usb_serial_port *port, u16 reg, u8 val)
99{
100 int status;
101 u8 *tmp;
102 struct usb_device *dev = port->serial->dev;
103
104 tmp = kmalloc(sizeof(val), GFP_KERNEL);
105 if (!tmp)
106 return -ENOMEM;
107
108 *tmp = val;
109
110 status = usb_control_msg(dev,
111 usb_sndctrlpipe(dev, 0),
112 F81232_REGISTER_REQUEST,
113 F81232_SET_REGISTER,
114 reg,
115 0,
116 tmp,
117 sizeof(val),
118 USB_CTRL_SET_TIMEOUT);
119 if (status != sizeof(val)) {
120 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
121
122 if (status < 0)
123 status = usb_translate_errors(status);
124 else
125 status = -EIO;
126 } else {
127 status = 0;
128 }
129
130 kfree(tmp);
131 return status;
132}
133
Peter Hung87fe5ad2015-03-17 17:48:22 +0800134static void f81232_read_msr(struct usb_serial_port *port)
135{
136 int status;
137 u8 current_msr;
138 struct tty_struct *tty;
139 struct f81232_private *priv = usb_get_serial_port_data(port);
140
141 mutex_lock(&priv->lock);
142 status = f81232_get_register(port, MODEM_STATUS_REGISTER,
143 &current_msr);
144 if (status) {
145 dev_err(&port->dev, "%s fail, status: %d\n", __func__, status);
146 mutex_unlock(&priv->lock);
147 return;
148 }
149
150 if (!(current_msr & UART_MSR_ANY_DELTA)) {
151 mutex_unlock(&priv->lock);
152 return;
153 }
154
155 priv->modem_status = current_msr;
156
157 if (current_msr & UART_MSR_DCTS)
158 port->icount.cts++;
159 if (current_msr & UART_MSR_DDSR)
160 port->icount.dsr++;
161 if (current_msr & UART_MSR_TERI)
162 port->icount.rng++;
163 if (current_msr & UART_MSR_DDCD) {
164 port->icount.dcd++;
165 tty = tty_port_tty_get(&port->port);
166 if (tty) {
167 usb_serial_handle_dcd_change(port, tty,
168 current_msr & UART_MSR_DCD);
169
170 tty_kref_put(tty);
171 }
172 }
173
174 wake_up_interruptible(&port->port.delta_msr_wait);
175 mutex_unlock(&priv->lock);
176}
177
Peter Hung691545f2015-03-17 17:48:23 +0800178static int f81232_set_mctrl(struct usb_serial_port *port,
179 unsigned int set, unsigned int clear)
180{
181 u8 val;
182 int status;
183 struct f81232_private *priv = usb_get_serial_port_data(port);
184
185 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
186 return 0; /* no change */
187
188 /* 'set' takes precedence over 'clear' */
189 clear &= ~set;
190
191 /* force enable interrupt with OUT2 */
192 mutex_lock(&priv->lock);
193 val = UART_MCR_OUT2 | priv->modem_control;
194
195 if (clear & TIOCM_DTR)
196 val &= ~UART_MCR_DTR;
197
198 if (clear & TIOCM_RTS)
199 val &= ~UART_MCR_RTS;
200
201 if (set & TIOCM_DTR)
202 val |= UART_MCR_DTR;
203
204 if (set & TIOCM_RTS)
205 val |= UART_MCR_RTS;
206
207 dev_dbg(&port->dev, "%s new:%02x old:%02x\n", __func__,
208 val, priv->modem_control);
209
210 status = f81232_set_register(port, MODEM_CONTROL_REGISTER, val);
211 if (status) {
212 dev_err(&port->dev, "%s set MCR status < 0\n", __func__);
213 mutex_unlock(&priv->lock);
214 return status;
215 }
216
217 priv->modem_control = val;
218 mutex_unlock(&priv->lock);
219
220 return 0;
221}
222
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800223static void f81232_update_line_status(struct usb_serial_port *port,
224 unsigned char *data,
Peter Hung87fe5ad2015-03-17 17:48:22 +0800225 size_t actual_length)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800226{
Peter Hung87fe5ad2015-03-17 17:48:22 +0800227 struct f81232_private *priv = usb_get_serial_port_data(port);
228
229 if (!actual_length)
230 return;
231
232 switch (data[0] & 0x07) {
233 case 0x00: /* msr change */
234 dev_dbg(&port->dev, "IIR: MSR Change: %02x\n", data[0]);
235 schedule_work(&priv->interrupt_work);
236 break;
237 case 0x02: /* tx-empty */
238 break;
239 case 0x04: /* rx data available */
240 break;
241 case 0x06: /* lsr change */
242 /* we can forget it. the LSR will read from bulk-in */
243 dev_dbg(&port->dev, "IIR: LSR Change: %02x\n", data[0]);
244 break;
245 }
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800246}
247
248static void f81232_read_int_callback(struct urb *urb)
249{
250 struct usb_serial_port *port = urb->context;
251 unsigned char *data = urb->transfer_buffer;
252 unsigned int actual_length = urb->actual_length;
253 int status = urb->status;
254 int retval;
255
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800256 switch (status) {
257 case 0:
258 /* success */
259 break;
260 case -ECONNRESET:
261 case -ENOENT:
262 case -ESHUTDOWN:
263 /* this urb is terminated, clean up */
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700264 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
265 __func__, status);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800266 return;
267 default:
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700268 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
269 __func__, status);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800270 goto exit;
271 }
272
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100273 usb_serial_debug_data(&port->dev, __func__,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800274 urb->actual_length, urb->transfer_buffer);
275
276 f81232_update_line_status(port, data, actual_length);
277
278exit:
279 retval = usb_submit_urb(urb, GFP_ATOMIC);
280 if (retval)
281 dev_err(&urb->dev->dev,
282 "%s - usb_submit_urb failed with result %d\n",
283 __func__, retval);
284}
285
286static void f81232_process_read_urb(struct urb *urb)
287{
288 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800289 unsigned char *data = urb->transfer_buffer;
Peter Hung88850782015-03-17 17:48:20 +0800290 char tty_flag;
291 unsigned int i;
292 u8 lsr;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800293
Peter Hung88850782015-03-17 17:48:20 +0800294 /*
295 * When opening the port we get a 1-byte packet with the current LSR,
296 * which we discard.
297 */
298 if ((urb->actual_length < 2) || (urb->actual_length % 2))
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800299 return;
300
Peter Hung88850782015-03-17 17:48:20 +0800301 /* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800302
Peter Hung88850782015-03-17 17:48:20 +0800303 for (i = 0; i < urb->actual_length; i += 2) {
304 tty_flag = TTY_NORMAL;
305 lsr = data[i];
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800306
Peter Hung88850782015-03-17 17:48:20 +0800307 if (lsr & UART_LSR_BRK_ERROR_BITS) {
308 if (lsr & UART_LSR_BI) {
309 tty_flag = TTY_BREAK;
310 port->icount.brk++;
311 usb_serial_handle_break(port);
312 } else if (lsr & UART_LSR_PE) {
313 tty_flag = TTY_PARITY;
314 port->icount.parity++;
315 } else if (lsr & UART_LSR_FE) {
316 tty_flag = TTY_FRAME;
317 port->icount.frame++;
318 }
319
320 if (lsr & UART_LSR_OE) {
321 port->icount.overrun++;
322 tty_insert_flip_char(&port->port, 0,
323 TTY_OVERRUN);
324 }
325 }
326
327 if (port->port.console && port->sysrq) {
328 if (usb_serial_handle_sysrq_char(port, data[i + 1]))
329 continue;
330 }
331
332 tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800333 }
334
Jiri Slaby2e124b42013-01-03 15:53:06 +0100335 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800336}
337
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800338static void f81232_break_ctl(struct tty_struct *tty, int break_state)
339{
340 /* FIXME - Stubbed out for now */
341
342 /*
343 * break_state = -1 to turn on break, and 0 to turn off break
344 * see drivers/char/tty_io.c to see it used.
345 * last_set_data_urb_value NEVER has the break bit set in it.
346 */
347}
348
Peter Hung8bb4ca62015-03-17 17:48:25 +0800349static void f81232_set_baudrate(struct usb_serial_port *port, speed_t baudrate)
350{
351 u8 lcr;
352 int divisor, status = 0;
353
354 divisor = calc_baud_divisor(baudrate);
355
356 status = f81232_get_register(port, LINE_CONTROL_REGISTER,
357 &lcr); /* get LCR */
358 if (status) {
359 dev_err(&port->dev, "%s failed to get LCR: %d\n",
360 __func__, status);
361 return;
362 }
363
364 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
365 lcr | UART_LCR_DLAB); /* Enable DLAB */
366 if (status) {
367 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
368 __func__, status);
369 return;
370 }
371
372 status = f81232_set_register(port, RECEIVE_BUFFER_REGISTER,
373 divisor & 0x00ff); /* low */
374 if (status) {
375 dev_err(&port->dev, "%s failed to set baudrate MSB: %d\n",
376 __func__, status);
377 goto reapply_lcr;
378 }
379
380 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
381 (divisor & 0xff00) >> 8); /* high */
382 if (status) {
383 dev_err(&port->dev, "%s failed to set baudrate LSB: %d\n",
384 __func__, status);
385 }
386
387reapply_lcr:
388 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
389 lcr & ~UART_LCR_DLAB);
390 if (status) {
391 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
392 __func__, status);
393 }
394}
395
Peter Hung94f87302015-03-17 17:48:24 +0800396static int f81232_port_enable(struct usb_serial_port *port)
397{
398 u8 val;
399 int status;
400
401 /* fifo on, trigger8, clear TX/RX*/
402 val = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
403 UART_FCR_CLEAR_XMIT;
404
405 status = f81232_set_register(port, FIFO_CONTROL_REGISTER, val);
406 if (status) {
407 dev_err(&port->dev, "%s failed to set FCR: %d\n",
408 __func__, status);
409 return status;
410 }
411
412 /* MSR Interrupt only, LSR will read from Bulk-in odd byte */
413 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
414 UART_IER_MSI);
415 if (status) {
416 dev_err(&port->dev, "%s failed to set IER: %d\n",
417 __func__, status);
418 return status;
419 }
420
421 return 0;
422}
423
424static int f81232_port_disable(struct usb_serial_port *port)
425{
426 int status;
427
428 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER, 0);
429 if (status) {
430 dev_err(&port->dev, "%s failed to set IER: %d\n",
431 __func__, status);
432 return status;
433 }
434
435 return 0;
436}
437
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800438static void f81232_set_termios(struct tty_struct *tty,
439 struct usb_serial_port *port, struct ktermios *old_termios)
440{
Peter Hung8bb4ca62015-03-17 17:48:25 +0800441 u8 new_lcr = 0;
442 int status = 0;
443 speed_t baudrate;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800444
445 /* Don't change anything if nothing has changed */
Johan Hovold21886722013-06-10 18:29:37 +0200446 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800447 return;
448
Peter Hung8bb4ca62015-03-17 17:48:25 +0800449 if (C_BAUD(tty) == B0)
450 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
451 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
452 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
453
454 baudrate = tty_get_baud_rate(tty);
455 if (baudrate > 0) {
456 if (baudrate > F81232_MAX_BAUDRATE) {
457 baudrate = F81232_MAX_BAUDRATE;
458 tty_encode_baud_rate(tty, baudrate, baudrate);
459 }
460 f81232_set_baudrate(port, baudrate);
461 }
462
463 if (C_PARENB(tty)) {
464 new_lcr |= UART_LCR_PARITY;
465
466 if (!C_PARODD(tty))
467 new_lcr |= UART_LCR_EPAR;
468
469 if (C_CMSPAR(tty))
470 new_lcr |= UART_LCR_SPAR;
471 }
472
473 if (C_CSTOPB(tty))
474 new_lcr |= UART_LCR_STOP;
475
476 switch (C_CSIZE(tty)) {
477 case CS5:
478 new_lcr |= UART_LCR_WLEN5;
479 break;
480 case CS6:
481 new_lcr |= UART_LCR_WLEN6;
482 break;
483 case CS7:
484 new_lcr |= UART_LCR_WLEN7;
485 break;
486 default:
487 case CS8:
488 new_lcr |= UART_LCR_WLEN8;
489 break;
490 }
491
492 status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
493 if (status) {
494 dev_err(&port->dev, "%s failed to set LCR: %d\n",
495 __func__, status);
496 }
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800497}
498
499static int f81232_tiocmget(struct tty_struct *tty)
500{
Peter Hung691545f2015-03-17 17:48:23 +0800501 int r;
502 struct usb_serial_port *port = tty->driver_data;
503 struct f81232_private *port_priv = usb_get_serial_port_data(port);
504 u8 mcr, msr;
505
506 /* force get current MSR changed state */
507 f81232_read_msr(port);
508
509 mutex_lock(&port_priv->lock);
510 mcr = port_priv->modem_control;
511 msr = port_priv->modem_status;
512 mutex_unlock(&port_priv->lock);
513
514 r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
515 (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
516 (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
517 (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
518 (msr & UART_MSR_RI ? TIOCM_RI : 0) |
519 (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
520
521 return r;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800522}
523
524static int f81232_tiocmset(struct tty_struct *tty,
525 unsigned int set, unsigned int clear)
526{
Peter Hung691545f2015-03-17 17:48:23 +0800527 struct usb_serial_port *port = tty->driver_data;
528
529 return f81232_set_mctrl(port, set, clear);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800530}
531
532static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
533{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800534 int result;
535
Peter Hung94f87302015-03-17 17:48:24 +0800536 result = f81232_port_enable(port);
537 if (result)
538 return result;
539
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800540 /* Setup termios */
541 if (tty)
Johan Hovold21886722013-06-10 18:29:37 +0200542 f81232_set_termios(tty, port, NULL);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800543
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800544 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
545 if (result) {
546 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
547 " error %d\n", __func__, result);
548 return result;
549 }
550
551 result = usb_serial_generic_open(tty, port);
552 if (result) {
553 usb_kill_urb(port->interrupt_in_urb);
554 return result;
555 }
556
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800557 return 0;
558}
559
560static void f81232_close(struct usb_serial_port *port)
561{
Peter Hung94f87302015-03-17 17:48:24 +0800562 f81232_port_disable(port);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800563 usb_serial_generic_close(port);
564 usb_kill_urb(port->interrupt_in_urb);
565}
566
567static void f81232_dtr_rts(struct usb_serial_port *port, int on)
568{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800569 if (on)
Peter Hung691545f2015-03-17 17:48:23 +0800570 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800571 else
Peter Hung691545f2015-03-17 17:48:23 +0800572 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800573}
574
575static int f81232_carrier_raised(struct usb_serial_port *port)
576{
Peter Hung691545f2015-03-17 17:48:23 +0800577 u8 msr;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800578 struct f81232_private *priv = usb_get_serial_port_data(port);
Peter Hung691545f2015-03-17 17:48:23 +0800579
580 mutex_lock(&priv->lock);
581 msr = priv->modem_status;
582 mutex_unlock(&priv->lock);
583
584 if (msr & UART_MSR_DCD)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800585 return 1;
586 return 0;
587}
588
Peter Hung88d35cf2015-03-17 17:48:26 +0800589static int f81232_get_serial_info(struct usb_serial_port *port,
590 unsigned long arg)
591{
592 struct serial_struct ser;
593
594 memset(&ser, 0, sizeof(ser));
595
596 ser.type = PORT_16550A;
597 ser.line = port->minor;
598 ser.port = port->port_number;
599 ser.baud_base = F81232_MAX_BAUDRATE;
600
601 if (copy_to_user((void __user *)arg, &ser, sizeof(ser)))
602 return -EFAULT;
603
604 return 0;
605}
606
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800607static int f81232_ioctl(struct tty_struct *tty,
608 unsigned int cmd, unsigned long arg)
609{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800610 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700611
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800612 switch (cmd) {
613 case TIOCGSERIAL:
Peter Hung88d35cf2015-03-17 17:48:26 +0800614 return f81232_get_serial_info(port, arg);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800615 default:
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800616 break;
617 }
618 return -ENOIOCTLCMD;
619}
620
Peter Hung87fe5ad2015-03-17 17:48:22 +0800621static void f81232_interrupt_work(struct work_struct *work)
622{
623 struct f81232_private *priv =
624 container_of(work, struct f81232_private, interrupt_work);
625
626 f81232_read_msr(priv->port);
627}
628
Johan Hovold3124d1d2012-10-17 13:34:56 +0200629static int f81232_port_probe(struct usb_serial_port *port)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800630{
631 struct f81232_private *priv;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800632
Johan Hovold3124d1d2012-10-17 13:34:56 +0200633 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
634 if (!priv)
635 return -ENOMEM;
636
Peter Hung7139c932015-03-17 17:48:21 +0800637 mutex_init(&priv->lock);
Peter Hung87fe5ad2015-03-17 17:48:22 +0800638 INIT_WORK(&priv->interrupt_work, f81232_interrupt_work);
Johan Hovold3124d1d2012-10-17 13:34:56 +0200639
640 usb_set_serial_port_data(port, priv);
641
Johan Hovoldd7be6222013-06-26 16:47:23 +0200642 port->port.drain_delay = 256;
Peter Hung87fe5ad2015-03-17 17:48:22 +0800643 priv->port = port;
Johan Hovoldd7be6222013-06-26 16:47:23 +0200644
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800645 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800646}
647
Johan Hovold3124d1d2012-10-17 13:34:56 +0200648static int f81232_port_remove(struct usb_serial_port *port)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800649{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800650 struct f81232_private *priv;
651
Johan Hovold3124d1d2012-10-17 13:34:56 +0200652 priv = usb_get_serial_port_data(port);
653 kfree(priv);
654
655 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800656}
657
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800658static struct usb_serial_driver f81232_device = {
659 .driver = {
660 .owner = THIS_MODULE,
661 .name = "f81232",
662 },
663 .id_table = id_table,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800664 .num_ports = 1,
665 .bulk_in_size = 256,
666 .bulk_out_size = 256,
667 .open = f81232_open,
668 .close = f81232_close,
669 .dtr_rts = f81232_dtr_rts,
670 .carrier_raised = f81232_carrier_raised,
671 .ioctl = f81232_ioctl,
672 .break_ctl = f81232_break_ctl,
673 .set_termios = f81232_set_termios,
674 .tiocmget = f81232_tiocmget,
675 .tiocmset = f81232_tiocmset,
Johan Hovoldc50db822013-12-29 19:22:58 +0100676 .tiocmiwait = usb_serial_generic_tiocmiwait,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800677 .process_read_urb = f81232_process_read_urb,
678 .read_int_callback = f81232_read_int_callback,
Johan Hovold3124d1d2012-10-17 13:34:56 +0200679 .port_probe = f81232_port_probe,
680 .port_remove = f81232_port_remove,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800681};
682
683static struct usb_serial_driver * const serial_drivers[] = {
684 &f81232_device,
685 NULL,
686};
687
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700688module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800689
690MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
Peter Hung96ee85c2015-03-17 17:48:28 +0800691MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
692MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800693MODULE_LICENSE("GPL v2");