blob: 0dcdcb4b2cde5ecb640218e5aa63265c0ebe2067 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -08002/*
3 * Fintek F81232 USB to serial adaptor driver
4 *
5 * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
6 * Copyright (C) 2012 Linux Foundation
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -08007 */
8
9#include <linux/kernel.h>
10#include <linux/errno.h>
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080011#include <linux/slab.h>
12#include <linux/tty.h>
13#include <linux/tty_driver.h>
14#include <linux/tty_flip.h>
15#include <linux/serial.h>
16#include <linux/module.h>
17#include <linux/moduleparam.h>
Peter Hung7139c932015-03-17 17:48:21 +080018#include <linux/mutex.h>
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080019#include <linux/uaccess.h>
20#include <linux/usb.h>
21#include <linux/usb/serial.h>
Peter Hung88850782015-03-17 17:48:20 +080022#include <linux/serial_reg.h>
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080023
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080024static const struct usb_device_id id_table[] = {
25 { USB_DEVICE(0x1934, 0x0706) },
26 { } /* Terminating entry */
27};
28MODULE_DEVICE_TABLE(usb, id_table);
29
Peter Hung8bb4ca62015-03-17 17:48:25 +080030/* Maximum baudrate for F81232 */
Johan Hovold65dd82a2015-03-27 17:33:38 +010031#define F81232_MAX_BAUDRATE 115200
Peter Hung8bb4ca62015-03-17 17:48:25 +080032
Peter Hung87fe5ad2015-03-17 17:48:22 +080033/* USB Control EP parameter */
Johan Hovold65dd82a2015-03-27 17:33:38 +010034#define F81232_REGISTER_REQUEST 0xa0
Peter Hung87fe5ad2015-03-17 17:48:22 +080035#define F81232_GET_REGISTER 0xc0
Peter Hung691545f2015-03-17 17:48:23 +080036#define F81232_SET_REGISTER 0x40
Peter Hung87fe5ad2015-03-17 17:48:22 +080037
Johan Hovold65dd82a2015-03-27 17:33:38 +010038#define SERIAL_BASE_ADDRESS 0x0120
Peter Hung8bb4ca62015-03-17 17:48:25 +080039#define RECEIVE_BUFFER_REGISTER (0x00 + SERIAL_BASE_ADDRESS)
Peter Hung94f87302015-03-17 17:48:24 +080040#define INTERRUPT_ENABLE_REGISTER (0x01 + SERIAL_BASE_ADDRESS)
41#define FIFO_CONTROL_REGISTER (0x02 + SERIAL_BASE_ADDRESS)
Peter Hung8bb4ca62015-03-17 17:48:25 +080042#define LINE_CONTROL_REGISTER (0x03 + SERIAL_BASE_ADDRESS)
Peter Hung691545f2015-03-17 17:48:23 +080043#define MODEM_CONTROL_REGISTER (0x04 + SERIAL_BASE_ADDRESS)
Peter Hung87fe5ad2015-03-17 17:48:22 +080044#define MODEM_STATUS_REGISTER (0x06 + SERIAL_BASE_ADDRESS)
45
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080046struct f81232_private {
Peter Hung7139c932015-03-17 17:48:21 +080047 struct mutex lock;
Peter Hung691545f2015-03-17 17:48:23 +080048 u8 modem_control;
Peter Hungb830d072015-03-17 17:48:19 +080049 u8 modem_status;
Peter Hung87fe5ad2015-03-17 17:48:22 +080050 struct work_struct interrupt_work;
51 struct usb_serial_port *port;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -080052};
53
Peter Hung8bb4ca62015-03-17 17:48:25 +080054static int calc_baud_divisor(speed_t baudrate)
55{
56 return DIV_ROUND_CLOSEST(F81232_MAX_BAUDRATE, baudrate);
57}
58
Peter Hung87fe5ad2015-03-17 17:48:22 +080059static int f81232_get_register(struct usb_serial_port *port, u16 reg, u8 *val)
60{
61 int status;
62 u8 *tmp;
63 struct usb_device *dev = port->serial->dev;
64
65 tmp = kmalloc(sizeof(*val), GFP_KERNEL);
66 if (!tmp)
67 return -ENOMEM;
68
69 status = usb_control_msg(dev,
70 usb_rcvctrlpipe(dev, 0),
71 F81232_REGISTER_REQUEST,
72 F81232_GET_REGISTER,
73 reg,
74 0,
75 tmp,
76 sizeof(*val),
77 USB_CTRL_GET_TIMEOUT);
78 if (status != sizeof(*val)) {
79 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
80
81 if (status < 0)
82 status = usb_translate_errors(status);
83 else
84 status = -EIO;
85 } else {
86 status = 0;
87 *val = *tmp;
88 }
89
90 kfree(tmp);
91 return status;
92}
93
Peter Hung691545f2015-03-17 17:48:23 +080094static int f81232_set_register(struct usb_serial_port *port, u16 reg, u8 val)
95{
96 int status;
97 u8 *tmp;
98 struct usb_device *dev = port->serial->dev;
99
100 tmp = kmalloc(sizeof(val), GFP_KERNEL);
101 if (!tmp)
102 return -ENOMEM;
103
104 *tmp = val;
105
106 status = usb_control_msg(dev,
107 usb_sndctrlpipe(dev, 0),
108 F81232_REGISTER_REQUEST,
109 F81232_SET_REGISTER,
110 reg,
111 0,
112 tmp,
113 sizeof(val),
114 USB_CTRL_SET_TIMEOUT);
115 if (status != sizeof(val)) {
116 dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
117
118 if (status < 0)
119 status = usb_translate_errors(status);
120 else
121 status = -EIO;
122 } else {
123 status = 0;
124 }
125
126 kfree(tmp);
127 return status;
128}
129
Peter Hung87fe5ad2015-03-17 17:48:22 +0800130static void f81232_read_msr(struct usb_serial_port *port)
131{
132 int status;
133 u8 current_msr;
134 struct tty_struct *tty;
135 struct f81232_private *priv = usb_get_serial_port_data(port);
136
137 mutex_lock(&priv->lock);
138 status = f81232_get_register(port, MODEM_STATUS_REGISTER,
139 &current_msr);
140 if (status) {
141 dev_err(&port->dev, "%s fail, status: %d\n", __func__, status);
142 mutex_unlock(&priv->lock);
143 return;
144 }
145
146 if (!(current_msr & UART_MSR_ANY_DELTA)) {
147 mutex_unlock(&priv->lock);
148 return;
149 }
150
151 priv->modem_status = current_msr;
152
153 if (current_msr & UART_MSR_DCTS)
154 port->icount.cts++;
155 if (current_msr & UART_MSR_DDSR)
156 port->icount.dsr++;
157 if (current_msr & UART_MSR_TERI)
158 port->icount.rng++;
159 if (current_msr & UART_MSR_DDCD) {
160 port->icount.dcd++;
161 tty = tty_port_tty_get(&port->port);
162 if (tty) {
163 usb_serial_handle_dcd_change(port, tty,
164 current_msr & UART_MSR_DCD);
165
166 tty_kref_put(tty);
167 }
168 }
169
170 wake_up_interruptible(&port->port.delta_msr_wait);
171 mutex_unlock(&priv->lock);
172}
173
Peter Hung691545f2015-03-17 17:48:23 +0800174static int f81232_set_mctrl(struct usb_serial_port *port,
175 unsigned int set, unsigned int clear)
176{
177 u8 val;
178 int status;
179 struct f81232_private *priv = usb_get_serial_port_data(port);
180
181 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
182 return 0; /* no change */
183
184 /* 'set' takes precedence over 'clear' */
185 clear &= ~set;
186
187 /* force enable interrupt with OUT2 */
188 mutex_lock(&priv->lock);
189 val = UART_MCR_OUT2 | priv->modem_control;
190
191 if (clear & TIOCM_DTR)
192 val &= ~UART_MCR_DTR;
193
194 if (clear & TIOCM_RTS)
195 val &= ~UART_MCR_RTS;
196
197 if (set & TIOCM_DTR)
198 val |= UART_MCR_DTR;
199
200 if (set & TIOCM_RTS)
201 val |= UART_MCR_RTS;
202
203 dev_dbg(&port->dev, "%s new:%02x old:%02x\n", __func__,
204 val, priv->modem_control);
205
206 status = f81232_set_register(port, MODEM_CONTROL_REGISTER, val);
207 if (status) {
208 dev_err(&port->dev, "%s set MCR status < 0\n", __func__);
209 mutex_unlock(&priv->lock);
210 return status;
211 }
212
213 priv->modem_control = val;
214 mutex_unlock(&priv->lock);
215
216 return 0;
217}
218
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800219static void f81232_update_line_status(struct usb_serial_port *port,
220 unsigned char *data,
Peter Hung87fe5ad2015-03-17 17:48:22 +0800221 size_t actual_length)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800222{
Peter Hung87fe5ad2015-03-17 17:48:22 +0800223 struct f81232_private *priv = usb_get_serial_port_data(port);
224
225 if (!actual_length)
226 return;
227
228 switch (data[0] & 0x07) {
229 case 0x00: /* msr change */
230 dev_dbg(&port->dev, "IIR: MSR Change: %02x\n", data[0]);
231 schedule_work(&priv->interrupt_work);
232 break;
233 case 0x02: /* tx-empty */
234 break;
235 case 0x04: /* rx data available */
236 break;
237 case 0x06: /* lsr change */
238 /* we can forget it. the LSR will read from bulk-in */
239 dev_dbg(&port->dev, "IIR: LSR Change: %02x\n", data[0]);
240 break;
241 }
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800242}
243
244static void f81232_read_int_callback(struct urb *urb)
245{
246 struct usb_serial_port *port = urb->context;
247 unsigned char *data = urb->transfer_buffer;
248 unsigned int actual_length = urb->actual_length;
249 int status = urb->status;
250 int retval;
251
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800252 switch (status) {
253 case 0:
254 /* success */
255 break;
256 case -ECONNRESET:
257 case -ENOENT:
258 case -ESHUTDOWN:
259 /* this urb is terminated, clean up */
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700260 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
261 __func__, status);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800262 return;
263 default:
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700264 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
265 __func__, status);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800266 goto exit;
267 }
268
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100269 usb_serial_debug_data(&port->dev, __func__,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800270 urb->actual_length, urb->transfer_buffer);
271
272 f81232_update_line_status(port, data, actual_length);
273
274exit:
275 retval = usb_submit_urb(urb, GFP_ATOMIC);
276 if (retval)
277 dev_err(&urb->dev->dev,
278 "%s - usb_submit_urb failed with result %d\n",
279 __func__, retval);
280}
281
282static void f81232_process_read_urb(struct urb *urb)
283{
284 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800285 unsigned char *data = urb->transfer_buffer;
Peter Hung88850782015-03-17 17:48:20 +0800286 char tty_flag;
287 unsigned int i;
288 u8 lsr;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800289
Peter Hung88850782015-03-17 17:48:20 +0800290 /*
291 * When opening the port we get a 1-byte packet with the current LSR,
292 * which we discard.
293 */
294 if ((urb->actual_length < 2) || (urb->actual_length % 2))
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800295 return;
296
Peter Hung88850782015-03-17 17:48:20 +0800297 /* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800298
Peter Hung88850782015-03-17 17:48:20 +0800299 for (i = 0; i < urb->actual_length; i += 2) {
300 tty_flag = TTY_NORMAL;
301 lsr = data[i];
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800302
Peter Hung88850782015-03-17 17:48:20 +0800303 if (lsr & UART_LSR_BRK_ERROR_BITS) {
304 if (lsr & UART_LSR_BI) {
305 tty_flag = TTY_BREAK;
306 port->icount.brk++;
307 usb_serial_handle_break(port);
308 } else if (lsr & UART_LSR_PE) {
309 tty_flag = TTY_PARITY;
310 port->icount.parity++;
311 } else if (lsr & UART_LSR_FE) {
312 tty_flag = TTY_FRAME;
313 port->icount.frame++;
314 }
315
316 if (lsr & UART_LSR_OE) {
317 port->icount.overrun++;
318 tty_insert_flip_char(&port->port, 0,
319 TTY_OVERRUN);
320 }
321 }
322
323 if (port->port.console && port->sysrq) {
324 if (usb_serial_handle_sysrq_char(port, data[i + 1]))
325 continue;
326 }
327
328 tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800329 }
330
Jiri Slaby2e124b42013-01-03 15:53:06 +0100331 tty_flip_buffer_push(&port->port);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800332}
333
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800334static void f81232_break_ctl(struct tty_struct *tty, int break_state)
335{
336 /* FIXME - Stubbed out for now */
337
338 /*
339 * break_state = -1 to turn on break, and 0 to turn off break
340 * see drivers/char/tty_io.c to see it used.
341 * last_set_data_urb_value NEVER has the break bit set in it.
342 */
343}
344
Peter Hung8bb4ca62015-03-17 17:48:25 +0800345static void f81232_set_baudrate(struct usb_serial_port *port, speed_t baudrate)
346{
347 u8 lcr;
Johan Hovold65dd82a2015-03-27 17:33:38 +0100348 int divisor;
349 int status = 0;
Peter Hung8bb4ca62015-03-17 17:48:25 +0800350
351 divisor = calc_baud_divisor(baudrate);
352
353 status = f81232_get_register(port, LINE_CONTROL_REGISTER,
354 &lcr); /* get LCR */
355 if (status) {
356 dev_err(&port->dev, "%s failed to get LCR: %d\n",
357 __func__, status);
358 return;
359 }
360
361 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
362 lcr | UART_LCR_DLAB); /* Enable DLAB */
363 if (status) {
364 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
365 __func__, status);
366 return;
367 }
368
369 status = f81232_set_register(port, RECEIVE_BUFFER_REGISTER,
370 divisor & 0x00ff); /* low */
371 if (status) {
372 dev_err(&port->dev, "%s failed to set baudrate MSB: %d\n",
373 __func__, status);
374 goto reapply_lcr;
375 }
376
377 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
378 (divisor & 0xff00) >> 8); /* high */
379 if (status) {
380 dev_err(&port->dev, "%s failed to set baudrate LSB: %d\n",
381 __func__, status);
382 }
383
384reapply_lcr:
385 status = f81232_set_register(port, LINE_CONTROL_REGISTER,
386 lcr & ~UART_LCR_DLAB);
387 if (status) {
388 dev_err(&port->dev, "%s failed to set DLAB: %d\n",
389 __func__, status);
390 }
391}
392
Peter Hung94f87302015-03-17 17:48:24 +0800393static int f81232_port_enable(struct usb_serial_port *port)
394{
395 u8 val;
396 int status;
397
398 /* fifo on, trigger8, clear TX/RX*/
399 val = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
400 UART_FCR_CLEAR_XMIT;
401
402 status = f81232_set_register(port, FIFO_CONTROL_REGISTER, val);
403 if (status) {
404 dev_err(&port->dev, "%s failed to set FCR: %d\n",
405 __func__, status);
406 return status;
407 }
408
409 /* MSR Interrupt only, LSR will read from Bulk-in odd byte */
410 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
411 UART_IER_MSI);
412 if (status) {
413 dev_err(&port->dev, "%s failed to set IER: %d\n",
414 __func__, status);
415 return status;
416 }
417
418 return 0;
419}
420
421static int f81232_port_disable(struct usb_serial_port *port)
422{
423 int status;
424
425 status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER, 0);
426 if (status) {
427 dev_err(&port->dev, "%s failed to set IER: %d\n",
428 __func__, status);
429 return status;
430 }
431
432 return 0;
433}
434
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800435static void f81232_set_termios(struct tty_struct *tty,
436 struct usb_serial_port *port, struct ktermios *old_termios)
437{
Peter Hung8bb4ca62015-03-17 17:48:25 +0800438 u8 new_lcr = 0;
439 int status = 0;
440 speed_t baudrate;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800441
442 /* Don't change anything if nothing has changed */
Johan Hovold21886722013-06-10 18:29:37 +0200443 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800444 return;
445
Peter Hung8bb4ca62015-03-17 17:48:25 +0800446 if (C_BAUD(tty) == B0)
447 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
448 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
449 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
450
451 baudrate = tty_get_baud_rate(tty);
452 if (baudrate > 0) {
453 if (baudrate > F81232_MAX_BAUDRATE) {
454 baudrate = F81232_MAX_BAUDRATE;
455 tty_encode_baud_rate(tty, baudrate, baudrate);
456 }
457 f81232_set_baudrate(port, baudrate);
458 }
459
460 if (C_PARENB(tty)) {
461 new_lcr |= UART_LCR_PARITY;
462
463 if (!C_PARODD(tty))
464 new_lcr |= UART_LCR_EPAR;
465
466 if (C_CMSPAR(tty))
467 new_lcr |= UART_LCR_SPAR;
468 }
469
470 if (C_CSTOPB(tty))
471 new_lcr |= UART_LCR_STOP;
472
473 switch (C_CSIZE(tty)) {
474 case CS5:
475 new_lcr |= UART_LCR_WLEN5;
476 break;
477 case CS6:
478 new_lcr |= UART_LCR_WLEN6;
479 break;
480 case CS7:
481 new_lcr |= UART_LCR_WLEN7;
482 break;
483 default:
484 case CS8:
485 new_lcr |= UART_LCR_WLEN8;
486 break;
487 }
488
489 status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
490 if (status) {
491 dev_err(&port->dev, "%s failed to set LCR: %d\n",
492 __func__, status);
493 }
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800494}
495
496static int f81232_tiocmget(struct tty_struct *tty)
497{
Peter Hung691545f2015-03-17 17:48:23 +0800498 int r;
499 struct usb_serial_port *port = tty->driver_data;
500 struct f81232_private *port_priv = usb_get_serial_port_data(port);
501 u8 mcr, msr;
502
503 /* force get current MSR changed state */
504 f81232_read_msr(port);
505
506 mutex_lock(&port_priv->lock);
507 mcr = port_priv->modem_control;
508 msr = port_priv->modem_status;
509 mutex_unlock(&port_priv->lock);
510
511 r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
512 (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
513 (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
514 (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
515 (msr & UART_MSR_RI ? TIOCM_RI : 0) |
516 (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
517
518 return r;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800519}
520
521static int f81232_tiocmset(struct tty_struct *tty,
522 unsigned int set, unsigned int clear)
523{
Peter Hung691545f2015-03-17 17:48:23 +0800524 struct usb_serial_port *port = tty->driver_data;
525
526 return f81232_set_mctrl(port, set, clear);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800527}
528
529static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
530{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800531 int result;
532
Peter Hung94f87302015-03-17 17:48:24 +0800533 result = f81232_port_enable(port);
534 if (result)
535 return result;
536
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800537 /* Setup termios */
538 if (tty)
Johan Hovold21886722013-06-10 18:29:37 +0200539 f81232_set_termios(tty, port, NULL);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800540
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800541 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
542 if (result) {
543 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
544 " error %d\n", __func__, result);
545 return result;
546 }
547
548 result = usb_serial_generic_open(tty, port);
549 if (result) {
550 usb_kill_urb(port->interrupt_in_urb);
551 return result;
552 }
553
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800554 return 0;
555}
556
557static void f81232_close(struct usb_serial_port *port)
558{
Peter Hung94f87302015-03-17 17:48:24 +0800559 f81232_port_disable(port);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800560 usb_serial_generic_close(port);
561 usb_kill_urb(port->interrupt_in_urb);
562}
563
564static void f81232_dtr_rts(struct usb_serial_port *port, int on)
565{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800566 if (on)
Peter Hung691545f2015-03-17 17:48:23 +0800567 f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800568 else
Peter Hung691545f2015-03-17 17:48:23 +0800569 f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800570}
571
572static int f81232_carrier_raised(struct usb_serial_port *port)
573{
Peter Hung691545f2015-03-17 17:48:23 +0800574 u8 msr;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800575 struct f81232_private *priv = usb_get_serial_port_data(port);
Peter Hung691545f2015-03-17 17:48:23 +0800576
577 mutex_lock(&priv->lock);
578 msr = priv->modem_status;
579 mutex_unlock(&priv->lock);
580
581 if (msr & UART_MSR_DCD)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800582 return 1;
583 return 0;
584}
585
Al Viro056abed2018-09-11 23:37:01 -0400586static int f81232_get_serial_info(struct tty_struct *tty,
587 struct serial_struct *ss)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800588{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800589 struct usb_serial_port *port = tty->driver_data;
Greg Kroah-Hartmana94e9b92012-05-15 16:27:17 -0700590
Al Viro056abed2018-09-11 23:37:01 -0400591 ss->type = PORT_16550A;
592 ss->line = port->minor;
593 ss->port = port->port_number;
594 ss->baud_base = F81232_MAX_BAUDRATE;
595 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800596}
597
Peter Hung87fe5ad2015-03-17 17:48:22 +0800598static void f81232_interrupt_work(struct work_struct *work)
599{
600 struct f81232_private *priv =
601 container_of(work, struct f81232_private, interrupt_work);
602
603 f81232_read_msr(priv->port);
604}
605
Johan Hovold3124d1d2012-10-17 13:34:56 +0200606static int f81232_port_probe(struct usb_serial_port *port)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800607{
608 struct f81232_private *priv;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800609
Johan Hovold3124d1d2012-10-17 13:34:56 +0200610 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
611 if (!priv)
612 return -ENOMEM;
613
Peter Hung7139c932015-03-17 17:48:21 +0800614 mutex_init(&priv->lock);
Peter Hung87fe5ad2015-03-17 17:48:22 +0800615 INIT_WORK(&priv->interrupt_work, f81232_interrupt_work);
Johan Hovold3124d1d2012-10-17 13:34:56 +0200616
617 usb_set_serial_port_data(port, priv);
618
Johan Hovoldd7be6222013-06-26 16:47:23 +0200619 port->port.drain_delay = 256;
Peter Hung87fe5ad2015-03-17 17:48:22 +0800620 priv->port = port;
Johan Hovoldd7be6222013-06-26 16:47:23 +0200621
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800622 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800623}
624
Johan Hovold3124d1d2012-10-17 13:34:56 +0200625static int f81232_port_remove(struct usb_serial_port *port)
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800626{
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800627 struct f81232_private *priv;
628
Johan Hovold3124d1d2012-10-17 13:34:56 +0200629 priv = usb_get_serial_port_data(port);
630 kfree(priv);
631
632 return 0;
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800633}
634
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800635static struct usb_serial_driver f81232_device = {
636 .driver = {
637 .owner = THIS_MODULE,
638 .name = "f81232",
639 },
640 .id_table = id_table,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800641 .num_ports = 1,
642 .bulk_in_size = 256,
643 .bulk_out_size = 256,
644 .open = f81232_open,
645 .close = f81232_close,
Johan Hovold65dd82a2015-03-27 17:33:38 +0100646 .dtr_rts = f81232_dtr_rts,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800647 .carrier_raised = f81232_carrier_raised,
Al Viro056abed2018-09-11 23:37:01 -0400648 .get_serial = f81232_get_serial_info,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800649 .break_ctl = f81232_break_ctl,
650 .set_termios = f81232_set_termios,
651 .tiocmget = f81232_tiocmget,
652 .tiocmset = f81232_tiocmset,
Johan Hovoldc50db822013-12-29 19:22:58 +0100653 .tiocmiwait = usb_serial_generic_tiocmiwait,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800654 .process_read_urb = f81232_process_read_urb,
655 .read_int_callback = f81232_read_int_callback,
Johan Hovold3124d1d2012-10-17 13:34:56 +0200656 .port_probe = f81232_port_probe,
657 .port_remove = f81232_port_remove,
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800658};
659
660static struct usb_serial_driver * const serial_drivers[] = {
661 &f81232_device,
662 NULL,
663};
664
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700665module_usb_serial_driver(serial_drivers, id_table);
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800666
667MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
Peter Hung96ee85c2015-03-17 17:48:28 +0800668MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
669MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
Greg Kroah-Hartmanaac1fc32012-02-28 13:36:35 -0800670MODULE_LICENSE("GPL v2");