Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * F81532/F81534 USB to Serial Ports Bridge
4 *
5 * F81532 => 2 Serial Ports
6 * F81534 => 4 Serial Ports
7 *
8 * Copyright (C) 2016 Feature Integration Technology Inc., (Fintek)
9 * Copyright (C) 2016 Tom Tsai (Tom_Tsai@fintek.com.tw)
10 * Copyright (C) 2016 Peter Hong (Peter_Hong@fintek.com.tw)
11 *
12 * The F81532/F81534 had 1 control endpoint for setting, 1 endpoint bulk-out
13 * for all serial port TX and 1 endpoint bulk-in for all serial port read in
14 * (Read Data/MSR/LSR).
15 *
16 * Write URB is fixed with 512bytes, per serial port used 128Bytes.
17 * It can be described by f81534_prepare_write_buffer()
18 *
19 * Read URB is 512Bytes max, per serial port used 128Bytes.
20 * It can be described by f81534_process_read_urb() and maybe received with
21 * 128x1,2,3,4 bytes.
22 *
23 */
24#include <linux/slab.h>
25#include <linux/tty.h>
26#include <linux/tty_flip.h>
27#include <linux/usb.h>
28#include <linux/usb/serial.h>
29#include <linux/serial_reg.h>
30#include <linux/module.h>
31#include <linux/uaccess.h>
32
33/* Serial Port register Address */
34#define F81534_UART_BASE_ADDRESS 0x1200
35#define F81534_UART_OFFSET 0x10
36#define F81534_DIVISOR_LSB_REG (0x00 + F81534_UART_BASE_ADDRESS)
37#define F81534_DIVISOR_MSB_REG (0x01 + F81534_UART_BASE_ADDRESS)
38#define F81534_INTERRUPT_ENABLE_REG (0x01 + F81534_UART_BASE_ADDRESS)
39#define F81534_FIFO_CONTROL_REG (0x02 + F81534_UART_BASE_ADDRESS)
40#define F81534_LINE_CONTROL_REG (0x03 + F81534_UART_BASE_ADDRESS)
41#define F81534_MODEM_CONTROL_REG (0x04 + F81534_UART_BASE_ADDRESS)
42#define F81534_LINE_STATUS_REG (0x05 + F81534_UART_BASE_ADDRESS)
43#define F81534_MODEM_STATUS_REG (0x06 + F81534_UART_BASE_ADDRESS)
44#define F81534_CLOCK_REG (0x08 + F81534_UART_BASE_ADDRESS)
45#define F81534_CONFIG1_REG (0x09 + F81534_UART_BASE_ADDRESS)
46
47#define F81534_DEF_CONF_ADDRESS_START 0x3000
48#define F81534_DEF_CONF_SIZE 12
49
50#define F81534_CUSTOM_ADDRESS_START 0x2f00
51#define F81534_CUSTOM_DATA_SIZE 0x10
52#define F81534_CUSTOM_NO_CUSTOM_DATA 0xff
53#define F81534_CUSTOM_VALID_TOKEN 0xf0
54#define F81534_CONF_OFFSET 1
55#define F81534_CONF_INIT_GPIO_OFFSET 4
56#define F81534_CONF_WORK_GPIO_OFFSET 8
57#define F81534_CONF_GPIO_SHUTDOWN 7
58#define F81534_CONF_GPIO_RS232 1
59
60#define F81534_MAX_DATA_BLOCK 64
61#define F81534_MAX_BUS_RETRY 20
62
63/* Default URB timeout for USB operations */
64#define F81534_USB_MAX_RETRY 10
65#define F81534_USB_TIMEOUT 2000
66#define F81534_SET_GET_REGISTER 0xA0
67
68#define F81534_NUM_PORT 4
69#define F81534_UNUSED_PORT 0xff
70#define F81534_WRITE_BUFFER_SIZE 512
71
72#define DRIVER_DESC "Fintek F81532/F81534"
73#define FINTEK_VENDOR_ID_1 0x1934
74#define FINTEK_VENDOR_ID_2 0x2C42
75#define FINTEK_DEVICE_ID 0x1202
76#define F81534_MAX_TX_SIZE 124
77#define F81534_MAX_RX_SIZE 124
78#define F81534_RECEIVE_BLOCK_SIZE 128
79#define F81534_MAX_RECEIVE_BLOCK_SIZE 512
80
81#define F81534_TOKEN_RECEIVE 0x01
82#define F81534_TOKEN_WRITE 0x02
83#define F81534_TOKEN_TX_EMPTY 0x03
84#define F81534_TOKEN_MSR_CHANGE 0x04
85
86/*
87 * We used interal SPI bus to access FLASH section. We must wait the SPI bus to
88 * idle if we performed any command.
89 *
90 * SPI Bus status register: F81534_BUS_REG_STATUS
91 * Bit 0/1 : BUSY
92 * Bit 2 : IDLE
93 */
94#define F81534_BUS_BUSY (BIT(0) | BIT(1))
95#define F81534_BUS_IDLE BIT(2)
96#define F81534_BUS_READ_DATA 0x1004
97#define F81534_BUS_REG_STATUS 0x1003
98#define F81534_BUS_REG_START 0x1002
99#define F81534_BUS_REG_END 0x1001
100
101#define F81534_CMD_READ 0x03
102
103#define F81534_DEFAULT_BAUD_RATE 9600
104
105#define F81534_PORT_CONF_RS232 0
106#define F81534_PORT_CONF_RS485 BIT(0)
107#define F81534_PORT_CONF_RS485_INVERT (BIT(0) | BIT(1))
108#define F81534_PORT_CONF_MODE_MASK GENMASK(1, 0)
109#define F81534_PORT_CONF_DISABLE_PORT BIT(3)
110#define F81534_PORT_CONF_NOT_EXIST_PORT BIT(7)
111#define F81534_PORT_UNAVAILABLE \
112 (F81534_PORT_CONF_DISABLE_PORT | F81534_PORT_CONF_NOT_EXIST_PORT)
113
114
115#define F81534_1X_RXTRIGGER 0xc3
116#define F81534_8X_RXTRIGGER 0xcf
117
118/*
119 * F81532/534 Clock registers (offset +08h)
120 *
121 * Bit0: UART Enable (always on)
122 * Bit2-1: Clock source selector
123 * 00: 1.846MHz.
124 * 01: 18.46MHz.
125 * 10: 24MHz.
126 * 11: 14.77MHz.
127 * Bit4: Auto direction(RTS) control (RTS pin Low when TX)
128 * Bit5: Invert direction(RTS) when Bit4 enabled (RTS pin high when TX)
129 */
130
131#define F81534_UART_EN BIT(0)
132#define F81534_CLK_1_846_MHZ 0
133#define F81534_CLK_18_46_MHZ BIT(1)
134#define F81534_CLK_24_MHZ BIT(2)
135#define F81534_CLK_14_77_MHZ (BIT(1) | BIT(2))
136#define F81534_CLK_MASK GENMASK(2, 1)
137#define F81534_CLK_TX_DELAY_1BIT BIT(3)
138#define F81534_CLK_RS485_MODE BIT(4)
139#define F81534_CLK_RS485_INVERT BIT(5)
140
141static const struct usb_device_id f81534_id_table[] = {
142 { USB_DEVICE(FINTEK_VENDOR_ID_1, FINTEK_DEVICE_ID) },
143 { USB_DEVICE(FINTEK_VENDOR_ID_2, FINTEK_DEVICE_ID) },
144 {} /* Terminating entry */
145};
146
147#define F81534_TX_EMPTY_BIT 0
148
149struct f81534_serial_private {
150 u8 conf_data[F81534_DEF_CONF_SIZE];
151 int tty_idx[F81534_NUM_PORT];
152 u8 setting_idx;
153 int opened_port;
154 struct mutex urb_mutex;
155};
156
157struct f81534_port_private {
158 struct mutex mcr_mutex;
159 struct mutex lcr_mutex;
160 struct work_struct lsr_work;
161 struct usb_serial_port *port;
162 unsigned long tx_empty;
163 spinlock_t msr_lock;
164 u32 baud_base;
165 u8 shadow_mcr;
166 u8 shadow_lcr;
167 u8 shadow_msr;
168 u8 shadow_clk;
169 u8 phy_num;
170};
171
172struct f81534_pin_data {
173 const u16 reg_addr;
174 const u8 reg_mask;
175};
176
177struct f81534_port_out_pin {
178 struct f81534_pin_data pin[3];
179};
180
181/* Pin output value for M2/M1/M0(SD) */
182static const struct f81534_port_out_pin f81534_port_out_pins[] = {
183 { { { 0x2ae8, BIT(7) }, { 0x2a90, BIT(5) }, { 0x2a90, BIT(4) } } },
184 { { { 0x2ae8, BIT(6) }, { 0x2ae8, BIT(0) }, { 0x2ae8, BIT(3) } } },
185 { { { 0x2a90, BIT(0) }, { 0x2ae8, BIT(2) }, { 0x2a80, BIT(6) } } },
186 { { { 0x2a90, BIT(3) }, { 0x2a90, BIT(2) }, { 0x2a90, BIT(1) } } },
187};
188
189static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
190static u8 const clock_table[] = { F81534_CLK_1_846_MHZ, F81534_CLK_14_77_MHZ,
191 F81534_CLK_18_46_MHZ, F81534_CLK_24_MHZ };
192
193static int f81534_logic_to_phy_port(struct usb_serial *serial,
194 struct usb_serial_port *port)
195{
196 struct f81534_serial_private *serial_priv =
197 usb_get_serial_data(port->serial);
198 int count = 0;
199 int i;
200
201 for (i = 0; i < F81534_NUM_PORT; ++i) {
202 if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
203 continue;
204
205 if (port->port_number == count)
206 return i;
207
208 ++count;
209 }
210
211 return -ENODEV;
212}
213
214static int f81534_set_register(struct usb_serial *serial, u16 reg, u8 data)
215{
216 struct usb_interface *interface = serial->interface;
217 struct usb_device *dev = serial->dev;
218 size_t count = F81534_USB_MAX_RETRY;
219 int status;
220 u8 *tmp;
221
222 tmp = kmalloc(sizeof(u8), GFP_KERNEL);
223 if (!tmp)
224 return -ENOMEM;
225
226 *tmp = data;
227
228 /*
229 * Our device maybe not reply when heavily loading, We'll retry for
230 * F81534_USB_MAX_RETRY times.
231 */
232 while (count--) {
233 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
234 F81534_SET_GET_REGISTER,
235 USB_TYPE_VENDOR | USB_DIR_OUT,
236 reg, 0, tmp, sizeof(u8),
237 F81534_USB_TIMEOUT);
238 if (status == sizeof(u8)) {
239 status = 0;
240 break;
241 }
242 }
243
244 if (status < 0) {
245 dev_err(&interface->dev, "%s: reg: %x data: %x failed: %d\n",
246 __func__, reg, data, status);
247 }
248
249 kfree(tmp);
250 return status;
251}
252
253static int f81534_get_register(struct usb_serial *serial, u16 reg, u8 *data)
254{
255 struct usb_interface *interface = serial->interface;
256 struct usb_device *dev = serial->dev;
257 size_t count = F81534_USB_MAX_RETRY;
258 int status;
259 u8 *tmp;
260
261 tmp = kmalloc(sizeof(u8), GFP_KERNEL);
262 if (!tmp)
263 return -ENOMEM;
264
265 /*
266 * Our device maybe not reply when heavily loading, We'll retry for
267 * F81534_USB_MAX_RETRY times.
268 */
269 while (count--) {
270 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
271 F81534_SET_GET_REGISTER,
272 USB_TYPE_VENDOR | USB_DIR_IN,
273 reg, 0, tmp, sizeof(u8),
274 F81534_USB_TIMEOUT);
275 if (status > 0) {
276 status = 0;
277 break;
278 } else if (status == 0) {
279 status = -EIO;
280 }
281 }
282
283 if (status < 0) {
284 dev_err(&interface->dev, "%s: reg: %x failed: %d\n", __func__,
285 reg, status);
286 goto end;
287 }
288
289 *data = *tmp;
290
291end:
292 kfree(tmp);
293 return status;
294}
295
296static int f81534_set_mask_register(struct usb_serial *serial, u16 reg,
297 u8 mask, u8 data)
298{
299 int status;
300 u8 tmp;
301
302 status = f81534_get_register(serial, reg, &tmp);
303 if (status)
304 return status;
305
306 tmp &= ~mask;
307 tmp |= (mask & data);
308
309 return f81534_set_register(serial, reg, tmp);
310}
311
312static int f81534_set_phy_port_register(struct usb_serial *serial, int phy,
313 u16 reg, u8 data)
314{
315 return f81534_set_register(serial, reg + F81534_UART_OFFSET * phy,
316 data);
317}
318
319static int f81534_get_phy_port_register(struct usb_serial *serial, int phy,
320 u16 reg, u8 *data)
321{
322 return f81534_get_register(serial, reg + F81534_UART_OFFSET * phy,
323 data);
324}
325
326static int f81534_set_port_register(struct usb_serial_port *port, u16 reg,
327 u8 data)
328{
329 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
330
331 return f81534_set_register(port->serial,
332 reg + port_priv->phy_num * F81534_UART_OFFSET, data);
333}
334
335static int f81534_get_port_register(struct usb_serial_port *port, u16 reg,
336 u8 *data)
337{
338 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
339
340 return f81534_get_register(port->serial,
341 reg + port_priv->phy_num * F81534_UART_OFFSET, data);
342}
343
344/*
345 * If we try to access the internal flash via SPI bus, we should check the bus
346 * status for every command. e.g., F81534_BUS_REG_START/F81534_BUS_REG_END
347 */
348static int f81534_wait_for_spi_idle(struct usb_serial *serial)
349{
350 size_t count = F81534_MAX_BUS_RETRY;
351 u8 tmp;
352 int status;
353
354 do {
355 status = f81534_get_register(serial, F81534_BUS_REG_STATUS,
356 &tmp);
357 if (status)
358 return status;
359
360 if (tmp & F81534_BUS_BUSY)
361 continue;
362
363 if (tmp & F81534_BUS_IDLE)
364 break;
365
366 } while (--count);
367
368 if (!count) {
369 dev_err(&serial->interface->dev,
370 "%s: timed out waiting for idle SPI bus\n",
371 __func__);
372 return -EIO;
373 }
374
375 return f81534_set_register(serial, F81534_BUS_REG_STATUS,
376 tmp & ~F81534_BUS_IDLE);
377}
378
379static int f81534_get_spi_register(struct usb_serial *serial, u16 reg,
380 u8 *data)
381{
382 int status;
383
384 status = f81534_get_register(serial, reg, data);
385 if (status)
386 return status;
387
388 return f81534_wait_for_spi_idle(serial);
389}
390
391static int f81534_set_spi_register(struct usb_serial *serial, u16 reg, u8 data)
392{
393 int status;
394
395 status = f81534_set_register(serial, reg, data);
396 if (status)
397 return status;
398
399 return f81534_wait_for_spi_idle(serial);
400}
401
402static int f81534_read_flash(struct usb_serial *serial, u32 address,
403 size_t size, u8 *buf)
404{
405 u8 tmp_buf[F81534_MAX_DATA_BLOCK];
406 size_t block = 0;
407 size_t read_size;
408 size_t count;
409 int status;
410 int offset;
411 u16 reg_tmp;
412
413 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
414 F81534_CMD_READ);
415 if (status)
416 return status;
417
418 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
419 (address >> 16) & 0xff);
420 if (status)
421 return status;
422
423 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
424 (address >> 8) & 0xff);
425 if (status)
426 return status;
427
428 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
429 (address >> 0) & 0xff);
430 if (status)
431 return status;
432
433 /* Continuous read mode */
434 do {
435 read_size = min_t(size_t, F81534_MAX_DATA_BLOCK, size);
436
437 for (count = 0; count < read_size; ++count) {
438 /* To write F81534_BUS_REG_END when final byte */
439 if (size <= F81534_MAX_DATA_BLOCK &&
440 read_size == count + 1)
441 reg_tmp = F81534_BUS_REG_END;
442 else
443 reg_tmp = F81534_BUS_REG_START;
444
445 /*
446 * Dummy code, force IC to generate a read pulse, the
447 * set of value 0xf1 is dont care (any value is ok)
448 */
449 status = f81534_set_spi_register(serial, reg_tmp,
450 0xf1);
451 if (status)
452 return status;
453
454 status = f81534_get_spi_register(serial,
455 F81534_BUS_READ_DATA,
456 &tmp_buf[count]);
457 if (status)
458 return status;
459
460 offset = count + block * F81534_MAX_DATA_BLOCK;
461 buf[offset] = tmp_buf[count];
462 }
463
464 size -= read_size;
465 ++block;
466 } while (size);
467
468 return 0;
469}
470
471static void f81534_prepare_write_buffer(struct usb_serial_port *port, u8 *buf)
472{
473 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
474 int phy_num = port_priv->phy_num;
475 u8 tx_len;
476 int i;
477
478 /*
479 * The block layout is fixed with 4x128 Bytes, per 128 Bytes a port.
480 * index 0: port phy idx (e.g., 0,1,2,3)
481 * index 1: only F81534_TOKEN_WRITE
482 * index 2: serial TX out length
483 * index 3: fix to 0
484 * index 4~127: serial out data block
485 */
486 for (i = 0; i < F81534_NUM_PORT; ++i) {
487 buf[i * F81534_RECEIVE_BLOCK_SIZE] = i;
488 buf[i * F81534_RECEIVE_BLOCK_SIZE + 1] = F81534_TOKEN_WRITE;
489 buf[i * F81534_RECEIVE_BLOCK_SIZE + 2] = 0;
490 buf[i * F81534_RECEIVE_BLOCK_SIZE + 3] = 0;
491 }
492
493 tx_len = kfifo_out_locked(&port->write_fifo,
494 &buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 4],
495 F81534_MAX_TX_SIZE, &port->lock);
496
497 buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 2] = tx_len;
498}
499
500static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
501{
502 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
503 struct urb *urb;
504 unsigned long flags;
505 int result;
506
507 /* Check is any data in write_fifo */
508 spin_lock_irqsave(&port->lock, flags);
509
510 if (kfifo_is_empty(&port->write_fifo)) {
511 spin_unlock_irqrestore(&port->lock, flags);
512 return 0;
513 }
514
515 spin_unlock_irqrestore(&port->lock, flags);
516
517 /* Check H/W is TXEMPTY */
518 if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
519 return 0;
520
521 urb = port->write_urbs[0];
522 f81534_prepare_write_buffer(port, port->bulk_out_buffers[0]);
523 urb->transfer_buffer_length = F81534_WRITE_BUFFER_SIZE;
524
525 result = usb_submit_urb(urb, mem_flags);
526 if (result) {
527 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
528 dev_err(&port->dev, "%s: submit failed: %d\n", __func__,
529 result);
530 return result;
531 }
532
533 usb_serial_port_softint(port);
534 return 0;
535}
536
537static u32 f81534_calc_baud_divisor(u32 baudrate, u32 clockrate)
538{
539 /* Round to nearest divisor */
540 return DIV_ROUND_CLOSEST(clockrate, baudrate);
541}
542
543static int f81534_find_clk(u32 baudrate)
544{
545 int idx;
546
547 for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
548 if (baudrate <= baudrate_table[idx] &&
549 baudrate_table[idx] % baudrate == 0)
550 return idx;
551 }
552
553 return -EINVAL;
554}
555
556static int f81534_set_port_config(struct usb_serial_port *port,
557 struct tty_struct *tty, u32 baudrate, u32 old_baudrate, u8 lcr)
558{
559 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
560 u32 divisor;
561 int status;
562 int i;
563 int idx;
564 u8 value;
565 u32 baud_list[] = {baudrate, old_baudrate, F81534_DEFAULT_BAUD_RATE};
566
567 for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
568 baudrate = baud_list[i];
569 if (baudrate == 0) {
570 tty_encode_baud_rate(tty, 0, 0);
571 return 0;
572 }
573
574 idx = f81534_find_clk(baudrate);
575 if (idx >= 0) {
576 tty_encode_baud_rate(tty, baudrate, baudrate);
577 break;
578 }
579 }
580
581 if (idx < 0)
582 return -EINVAL;
583
584 port_priv->baud_base = baudrate_table[idx];
585 port_priv->shadow_clk &= ~F81534_CLK_MASK;
586 port_priv->shadow_clk |= clock_table[idx];
587
588 status = f81534_set_port_register(port, F81534_CLOCK_REG,
589 port_priv->shadow_clk);
590 if (status) {
591 dev_err(&port->dev, "CLOCK_REG setting failed\n");
592 return status;
593 }
594
595 if (baudrate <= 1200)
596 value = F81534_1X_RXTRIGGER; /* 128 FIFO & TL: 1x */
597 else
598 value = F81534_8X_RXTRIGGER; /* 128 FIFO & TL: 8x */
599
600 status = f81534_set_port_register(port, F81534_CONFIG1_REG, value);
601 if (status) {
602 dev_err(&port->dev, "%s: CONFIG1 setting failed\n", __func__);
603 return status;
604 }
605
606 if (baudrate <= 1200)
607 value = UART_FCR_TRIGGER_1 | UART_FCR_ENABLE_FIFO; /* TL: 1 */
608 else
609 value = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO; /* TL: 8 */
610
611 status = f81534_set_port_register(port, F81534_FIFO_CONTROL_REG,
612 value);
613 if (status) {
614 dev_err(&port->dev, "%s: FCR setting failed\n", __func__);
615 return status;
616 }
617
618 divisor = f81534_calc_baud_divisor(baudrate, port_priv->baud_base);
619
620 mutex_lock(&port_priv->lcr_mutex);
621
622 value = UART_LCR_DLAB;
623 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
624 value);
625 if (status) {
626 dev_err(&port->dev, "%s: set LCR failed\n", __func__);
627 goto out_unlock;
628 }
629
630 value = divisor & 0xff;
631 status = f81534_set_port_register(port, F81534_DIVISOR_LSB_REG, value);
632 if (status) {
633 dev_err(&port->dev, "%s: set DLAB LSB failed\n", __func__);
634 goto out_unlock;
635 }
636
637 value = (divisor >> 8) & 0xff;
638 status = f81534_set_port_register(port, F81534_DIVISOR_MSB_REG, value);
639 if (status) {
640 dev_err(&port->dev, "%s: set DLAB MSB failed\n", __func__);
641 goto out_unlock;
642 }
643
644 value = lcr | (port_priv->shadow_lcr & UART_LCR_SBC);
645 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
646 value);
647 if (status) {
648 dev_err(&port->dev, "%s: set LCR failed\n", __func__);
649 goto out_unlock;
650 }
651
652 port_priv->shadow_lcr = value;
653out_unlock:
654 mutex_unlock(&port_priv->lcr_mutex);
655
656 return status;
657}
658
659static int f81534_break_ctl(struct tty_struct *tty, int break_state)
660{
661 struct usb_serial_port *port = tty->driver_data;
662 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
663 int status;
664
665 mutex_lock(&port_priv->lcr_mutex);
666
667 if (break_state)
668 port_priv->shadow_lcr |= UART_LCR_SBC;
669 else
670 port_priv->shadow_lcr &= ~UART_LCR_SBC;
671
672 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
673 port_priv->shadow_lcr);
674 if (status)
675 dev_err(&port->dev, "set break failed: %d\n", status);
676
677 mutex_unlock(&port_priv->lcr_mutex);
678
679 return status;
680}
681
682static int f81534_update_mctrl(struct usb_serial_port *port, unsigned int set,
683 unsigned int clear)
684{
685 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
686 int status;
687 u8 tmp;
688
689 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
690 return 0; /* no change */
691
692 mutex_lock(&port_priv->mcr_mutex);
693
694 /* 'Set' takes precedence over 'Clear' */
695 clear &= ~set;
696
697 /* Always enable UART_MCR_OUT2 */
698 tmp = UART_MCR_OUT2 | port_priv->shadow_mcr;
699
700 if (clear & TIOCM_DTR)
701 tmp &= ~UART_MCR_DTR;
702
703 if (clear & TIOCM_RTS)
704 tmp &= ~UART_MCR_RTS;
705
706 if (set & TIOCM_DTR)
707 tmp |= UART_MCR_DTR;
708
709 if (set & TIOCM_RTS)
710 tmp |= UART_MCR_RTS;
711
712 status = f81534_set_port_register(port, F81534_MODEM_CONTROL_REG, tmp);
713 if (status < 0) {
714 dev_err(&port->dev, "%s: MCR write failed\n", __func__);
715 mutex_unlock(&port_priv->mcr_mutex);
716 return status;
717 }
718
719 port_priv->shadow_mcr = tmp;
720 mutex_unlock(&port_priv->mcr_mutex);
721 return 0;
722}
723
724/*
725 * This function will search the data area with token F81534_CUSTOM_VALID_TOKEN
726 * for latest configuration index. If nothing found
727 * (*index = F81534_CUSTOM_NO_CUSTOM_DATA), We'll load default configure in
728 * F81534_DEF_CONF_ADDRESS_START section.
729 *
730 * Due to we only use block0 to save data, so *index should be 0 or
731 * F81534_CUSTOM_NO_CUSTOM_DATA.
732 */
733static int f81534_find_config_idx(struct usb_serial *serial, u8 *index)
734{
735 u8 tmp;
736 int status;
737
738 status = f81534_read_flash(serial, F81534_CUSTOM_ADDRESS_START, 1,
739 &tmp);
740 if (status) {
741 dev_err(&serial->interface->dev, "%s: read failed: %d\n",
742 __func__, status);
743 return status;
744 }
745
746 /* We'll use the custom data when the data is valid. */
747 if (tmp == F81534_CUSTOM_VALID_TOKEN)
748 *index = 0;
749 else
750 *index = F81534_CUSTOM_NO_CUSTOM_DATA;
751
752 return 0;
753}
754
755/*
756 * The F81532/534 will not report serial port to USB serial subsystem when
757 * H/W DCD/DSR/CTS/RI/RX pin connected to ground.
758 *
759 * To detect RX pin status, we'll enable MCR interal loopback, disable it and
760 * delayed for 60ms. It connected to ground If LSR register report UART_LSR_BI.
761 */
762static bool f81534_check_port_hw_disabled(struct usb_serial *serial, int phy)
763{
764 int status;
765 u8 old_mcr;
766 u8 msr;
767 u8 lsr;
768 u8 msr_mask;
769
770 msr_mask = UART_MSR_DCD | UART_MSR_RI | UART_MSR_DSR | UART_MSR_CTS;
771
772 status = f81534_get_phy_port_register(serial, phy,
773 F81534_MODEM_STATUS_REG, &msr);
774 if (status)
775 return false;
776
777 if ((msr & msr_mask) != msr_mask)
778 return false;
779
780 status = f81534_set_phy_port_register(serial, phy,
781 F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
782 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
783 if (status)
784 return false;
785
786 status = f81534_get_phy_port_register(serial, phy,
787 F81534_MODEM_CONTROL_REG, &old_mcr);
788 if (status)
789 return false;
790
791 status = f81534_set_phy_port_register(serial, phy,
792 F81534_MODEM_CONTROL_REG, UART_MCR_LOOP);
793 if (status)
794 return false;
795
796 status = f81534_set_phy_port_register(serial, phy,
797 F81534_MODEM_CONTROL_REG, 0x0);
798 if (status)
799 return false;
800
801 msleep(60);
802
803 status = f81534_get_phy_port_register(serial, phy,
804 F81534_LINE_STATUS_REG, &lsr);
805 if (status)
806 return false;
807
808 status = f81534_set_phy_port_register(serial, phy,
809 F81534_MODEM_CONTROL_REG, old_mcr);
810 if (status)
811 return false;
812
813 if ((lsr & UART_LSR_BI) == UART_LSR_BI)
814 return true;
815
816 return false;
817}
818
819/*
820 * We had 2 generation of F81532/534 IC. All has an internal storage.
821 *
822 * 1st is pure USB-to-TTL RS232 IC and designed for 4 ports only, no any
823 * internal data will used. All mode and gpio control should manually set
824 * by AP or Driver and all storage space value are 0xff. The
825 * f81534_calc_num_ports() will run to final we marked as "oldest version"
826 * for this IC.
827 *
828 * 2rd is designed to more generic to use any transceiver and this is our
829 * mass production type. We'll save data in F81534_CUSTOM_ADDRESS_START
830 * (0x2f00) with 9bytes. The 1st byte is a indicater. If the token is
831 * F81534_CUSTOM_VALID_TOKEN(0xf0), the IC is 2nd gen type, the following
832 * 4bytes save port mode (0:RS232/1:RS485 Invert/2:RS485), and the last
833 * 4bytes save GPIO state(value from 0~7 to represent 3 GPIO output pin).
834 * The f81534_calc_num_ports() will run to "new style" with checking
835 * F81534_PORT_UNAVAILABLE section.
836 */
837static int f81534_calc_num_ports(struct usb_serial *serial,
838 struct usb_serial_endpoints *epds)
839{
840 struct f81534_serial_private *serial_priv;
841 struct device *dev = &serial->interface->dev;
842 int size_bulk_in = usb_endpoint_maxp(epds->bulk_in[0]);
843 int size_bulk_out = usb_endpoint_maxp(epds->bulk_out[0]);
844 u8 num_port = 0;
845 int index = 0;
846 int status;
847 int i;
848
849 if (size_bulk_out != F81534_WRITE_BUFFER_SIZE ||
850 size_bulk_in != F81534_MAX_RECEIVE_BLOCK_SIZE) {
851 dev_err(dev, "unsupported endpoint max packet size\n");
852 return -ENODEV;
853 }
854
855 serial_priv = devm_kzalloc(&serial->interface->dev,
856 sizeof(*serial_priv), GFP_KERNEL);
857 if (!serial_priv)
858 return -ENOMEM;
859
860 usb_set_serial_data(serial, serial_priv);
861 mutex_init(&serial_priv->urb_mutex);
862
863 /* Check had custom setting */
864 status = f81534_find_config_idx(serial, &serial_priv->setting_idx);
865 if (status) {
866 dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
867 __func__, status);
868 return status;
869 }
870
871 /*
872 * We'll read custom data only when data available, otherwise we'll
873 * read default value instead.
874 */
875 if (serial_priv->setting_idx != F81534_CUSTOM_NO_CUSTOM_DATA) {
876 status = f81534_read_flash(serial,
877 F81534_CUSTOM_ADDRESS_START +
878 F81534_CONF_OFFSET,
879 sizeof(serial_priv->conf_data),
880 serial_priv->conf_data);
881 if (status) {
882 dev_err(&serial->interface->dev,
883 "%s: get custom data failed: %d\n",
884 __func__, status);
885 return status;
886 }
887
888 dev_dbg(&serial->interface->dev,
889 "%s: read config from block: %d\n", __func__,
890 serial_priv->setting_idx);
891 } else {
892 /* Read default board setting */
893 status = f81534_read_flash(serial,
894 F81534_DEF_CONF_ADDRESS_START,
895 sizeof(serial_priv->conf_data),
896 serial_priv->conf_data);
897 if (status) {
898 dev_err(&serial->interface->dev,
899 "%s: read failed: %d\n", __func__,
900 status);
901 return status;
902 }
903
904 dev_dbg(&serial->interface->dev, "%s: read default config\n",
905 __func__);
906 }
907
908 /* New style, find all possible ports */
909 for (i = 0; i < F81534_NUM_PORT; ++i) {
910 if (f81534_check_port_hw_disabled(serial, i))
911 serial_priv->conf_data[i] |= F81534_PORT_UNAVAILABLE;
912
913 if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
914 continue;
915
916 ++num_port;
917 }
918
919 if (!num_port) {
920 dev_warn(&serial->interface->dev,
921 "no config found, assuming 4 ports\n");
922 num_port = 4; /* Nothing found, oldest version IC */
923 }
924
925 /* Assign phy-to-logic mapping */
926 for (i = 0; i < F81534_NUM_PORT; ++i) {
927 if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
928 continue;
929
930 serial_priv->tty_idx[i] = index++;
931 dev_dbg(&serial->interface->dev,
932 "%s: phy_num: %d, tty_idx: %d\n", __func__, i,
933 serial_priv->tty_idx[i]);
934 }
935
936 /*
937 * Setup bulk-out endpoint multiplexing. All ports share the same
938 * bulk-out endpoint.
939 */
940 BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < F81534_NUM_PORT);
941
942 for (i = 1; i < num_port; ++i)
943 epds->bulk_out[i] = epds->bulk_out[0];
944
945 epds->num_bulk_out = num_port;
946
947 return num_port;
948}
949
950static void f81534_set_termios(struct tty_struct *tty,
951 struct usb_serial_port *port,
952 const struct ktermios *old_termios)
953{
954 u8 new_lcr = 0;
955 int status;
956 u32 baud;
957 u32 old_baud;
958
959 if (C_BAUD(tty) == B0)
960 f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
961 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
962 f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
963
964 if (C_PARENB(tty)) {
965 new_lcr |= UART_LCR_PARITY;
966
967 if (!C_PARODD(tty))
968 new_lcr |= UART_LCR_EPAR;
969
970 if (C_CMSPAR(tty))
971 new_lcr |= UART_LCR_SPAR;
972 }
973
974 if (C_CSTOPB(tty))
975 new_lcr |= UART_LCR_STOP;
976
977 new_lcr |= UART_LCR_WLEN(tty_get_char_size(tty->termios.c_cflag));
978
979 baud = tty_get_baud_rate(tty);
980 if (!baud)
981 return;
982
983 if (old_termios)
984 old_baud = tty_termios_baud_rate(old_termios);
985 else
986 old_baud = F81534_DEFAULT_BAUD_RATE;
987
988 dev_dbg(&port->dev, "%s: baud: %d\n", __func__, baud);
989
990 status = f81534_set_port_config(port, tty, baud, old_baud, new_lcr);
991 if (status < 0) {
992 dev_err(&port->dev, "%s: set port config failed: %d\n",
993 __func__, status);
994 }
995}
996
997static int f81534_submit_read_urb(struct usb_serial *serial, gfp_t flags)
998{
999 return usb_serial_generic_submit_read_urbs(serial->port[0], flags);
1000}
1001
1002static void f81534_msr_changed(struct usb_serial_port *port, u8 msr)
1003{
1004 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1005 struct tty_struct *tty;
1006 unsigned long flags;
1007 u8 old_msr;
1008
1009 if (!(msr & UART_MSR_ANY_DELTA))
1010 return;
1011
1012 spin_lock_irqsave(&port_priv->msr_lock, flags);
1013 old_msr = port_priv->shadow_msr;
1014 port_priv->shadow_msr = msr;
1015 spin_unlock_irqrestore(&port_priv->msr_lock, flags);
1016
1017 dev_dbg(&port->dev, "%s: MSR from %02x to %02x\n", __func__, old_msr,
1018 msr);
1019
1020 /* Update input line counters */
1021 if (msr & UART_MSR_DCTS)
1022 port->icount.cts++;
1023 if (msr & UART_MSR_DDSR)
1024 port->icount.dsr++;
1025 if (msr & UART_MSR_DDCD)
1026 port->icount.dcd++;
1027 if (msr & UART_MSR_TERI)
1028 port->icount.rng++;
1029
1030 wake_up_interruptible(&port->port.delta_msr_wait);
1031
1032 if (!(msr & UART_MSR_DDCD))
1033 return;
1034
1035 dev_dbg(&port->dev, "%s: DCD Changed: phy_num: %d from %x to %x\n",
1036 __func__, port_priv->phy_num, old_msr, msr);
1037
1038 tty = tty_port_tty_get(&port->port);
1039 if (!tty)
1040 return;
1041
1042 usb_serial_handle_dcd_change(port, tty, msr & UART_MSR_DCD);
1043 tty_kref_put(tty);
1044}
1045
1046static int f81534_read_msr(struct usb_serial_port *port)
1047{
1048 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1049 unsigned long flags;
1050 int status;
1051 u8 msr;
1052
1053 /* Get MSR initial value */
1054 status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
1055 if (status)
1056 return status;
1057
1058 /* Force update current state */
1059 spin_lock_irqsave(&port_priv->msr_lock, flags);
1060 port_priv->shadow_msr = msr;
1061 spin_unlock_irqrestore(&port_priv->msr_lock, flags);
1062
1063 return 0;
1064}
1065
1066static int f81534_open(struct tty_struct *tty, struct usb_serial_port *port)
1067{
1068 struct f81534_serial_private *serial_priv =
1069 usb_get_serial_data(port->serial);
1070 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1071 int status;
1072
1073 status = f81534_set_port_register(port,
1074 F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
1075 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
1076 if (status) {
1077 dev_err(&port->dev, "%s: Clear FIFO failed: %d\n", __func__,
1078 status);
1079 return status;
1080 }
1081
1082 if (tty)
1083 f81534_set_termios(tty, port, NULL);
1084
1085 status = f81534_read_msr(port);
1086 if (status)
1087 return status;
1088
1089 mutex_lock(&serial_priv->urb_mutex);
1090
1091 /* Submit Read URBs for first port opened */
1092 if (!serial_priv->opened_port) {
1093 status = f81534_submit_read_urb(port->serial, GFP_KERNEL);
1094 if (status)
1095 goto exit;
1096 }
1097
1098 serial_priv->opened_port++;
1099
1100exit:
1101 mutex_unlock(&serial_priv->urb_mutex);
1102
1103 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1104 return status;
1105}
1106
1107static void f81534_close(struct usb_serial_port *port)
1108{
1109 struct f81534_serial_private *serial_priv =
1110 usb_get_serial_data(port->serial);
1111 struct usb_serial_port *port0 = port->serial->port[0];
1112 unsigned long flags;
1113 size_t i;
1114
1115 usb_kill_urb(port->write_urbs[0]);
1116
1117 spin_lock_irqsave(&port->lock, flags);
1118 kfifo_reset_out(&port->write_fifo);
1119 spin_unlock_irqrestore(&port->lock, flags);
1120
1121 /* Kill Read URBs when final port closed */
1122 mutex_lock(&serial_priv->urb_mutex);
1123 serial_priv->opened_port--;
1124
1125 if (!serial_priv->opened_port) {
1126 for (i = 0; i < ARRAY_SIZE(port0->read_urbs); ++i)
1127 usb_kill_urb(port0->read_urbs[i]);
1128 }
1129
1130 mutex_unlock(&serial_priv->urb_mutex);
1131}
1132
1133static void f81534_get_serial_info(struct tty_struct *tty, struct serial_struct *ss)
1134{
1135 struct usb_serial_port *port = tty->driver_data;
1136 struct f81534_port_private *port_priv;
1137
1138 port_priv = usb_get_serial_port_data(port);
1139
1140 ss->baud_base = port_priv->baud_base;
1141}
1142
1143static void f81534_process_per_serial_block(struct usb_serial_port *port,
1144 u8 *data)
1145{
1146 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1147 int phy_num = data[0];
1148 size_t read_size = 0;
1149 size_t i;
1150 char tty_flag;
1151 int status;
1152 u8 lsr;
1153
1154 /*
1155 * The block layout is 128 Bytes
1156 * index 0: port phy idx (e.g., 0,1,2,3),
1157 * index 1: It's could be
1158 * F81534_TOKEN_RECEIVE
1159 * F81534_TOKEN_TX_EMPTY
1160 * F81534_TOKEN_MSR_CHANGE
1161 * index 2: serial in size (data+lsr, must be even)
1162 * meaningful for F81534_TOKEN_RECEIVE only
1163 * index 3: current MSR with this device
1164 * index 4~127: serial in data block (data+lsr, must be even)
1165 */
1166 switch (data[1]) {
1167 case F81534_TOKEN_TX_EMPTY:
1168 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1169
1170 /* Try to submit writer */
1171 status = f81534_submit_writer(port, GFP_ATOMIC);
1172 if (status)
1173 dev_err(&port->dev, "%s: submit failed\n", __func__);
1174 return;
1175
1176 case F81534_TOKEN_MSR_CHANGE:
1177 f81534_msr_changed(port, data[3]);
1178 return;
1179
1180 case F81534_TOKEN_RECEIVE:
1181 read_size = data[2];
1182 if (read_size > F81534_MAX_RX_SIZE) {
1183 dev_err(&port->dev,
1184 "%s: phy: %d read_size: %zu larger than: %d\n",
1185 __func__, phy_num, read_size,
1186 F81534_MAX_RX_SIZE);
1187 return;
1188 }
1189
1190 break;
1191
1192 default:
1193 dev_warn(&port->dev, "%s: unknown token: %02x\n", __func__,
1194 data[1]);
1195 return;
1196 }
1197
1198 for (i = 4; i < 4 + read_size; i += 2) {
1199 tty_flag = TTY_NORMAL;
1200 lsr = data[i + 1];
1201
1202 if (lsr & UART_LSR_BRK_ERROR_BITS) {
1203 if (lsr & UART_LSR_BI) {
1204 tty_flag = TTY_BREAK;
1205 port->icount.brk++;
1206 usb_serial_handle_break(port);
1207 } else if (lsr & UART_LSR_PE) {
1208 tty_flag = TTY_PARITY;
1209 port->icount.parity++;
1210 } else if (lsr & UART_LSR_FE) {
1211 tty_flag = TTY_FRAME;
1212 port->icount.frame++;
1213 }
1214
1215 if (lsr & UART_LSR_OE) {
1216 port->icount.overrun++;
1217 tty_insert_flip_char(&port->port, 0,
1218 TTY_OVERRUN);
1219 }
1220
1221 schedule_work(&port_priv->lsr_work);
1222 }
1223
1224 if (port->sysrq) {
1225 if (usb_serial_handle_sysrq_char(port, data[i]))
1226 continue;
1227 }
1228
1229 tty_insert_flip_char(&port->port, data[i], tty_flag);
1230 }
1231
1232 tty_flip_buffer_push(&port->port);
1233}
1234
1235static void f81534_process_read_urb(struct urb *urb)
1236{
1237 struct f81534_serial_private *serial_priv;
1238 struct usb_serial_port *port;
1239 struct usb_serial *serial;
1240 u8 *buf;
1241 int phy_port_num;
1242 int tty_port_num;
1243 size_t i;
1244
1245 if (!urb->actual_length ||
1246 urb->actual_length % F81534_RECEIVE_BLOCK_SIZE) {
1247 return;
1248 }
1249
1250 port = urb->context;
1251 serial = port->serial;
1252 buf = urb->transfer_buffer;
1253 serial_priv = usb_get_serial_data(serial);
1254
1255 for (i = 0; i < urb->actual_length; i += F81534_RECEIVE_BLOCK_SIZE) {
1256 phy_port_num = buf[i];
1257 if (phy_port_num >= F81534_NUM_PORT) {
1258 dev_err(&port->dev,
1259 "%s: phy_port_num: %d larger than: %d\n",
1260 __func__, phy_port_num, F81534_NUM_PORT);
1261 continue;
1262 }
1263
1264 tty_port_num = serial_priv->tty_idx[phy_port_num];
1265 port = serial->port[tty_port_num];
1266
1267 if (tty_port_initialized(&port->port))
1268 f81534_process_per_serial_block(port, &buf[i]);
1269 }
1270}
1271
1272static void f81534_write_usb_callback(struct urb *urb)
1273{
1274 struct usb_serial_port *port = urb->context;
1275
1276 switch (urb->status) {
1277 case 0:
1278 break;
1279 case -ENOENT:
1280 case -ECONNRESET:
1281 case -ESHUTDOWN:
1282 dev_dbg(&port->dev, "%s - urb stopped: %d\n",
1283 __func__, urb->status);
1284 return;
1285 case -EPIPE:
1286 dev_err(&port->dev, "%s - urb stopped: %d\n",
1287 __func__, urb->status);
1288 return;
1289 default:
1290 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
1291 __func__, urb->status);
1292 break;
1293 }
1294}
1295
1296static void f81534_lsr_worker(struct work_struct *work)
1297{
1298 struct f81534_port_private *port_priv;
1299 struct usb_serial_port *port;
1300 int status;
1301 u8 tmp;
1302
1303 port_priv = container_of(work, struct f81534_port_private, lsr_work);
1304 port = port_priv->port;
1305
1306 status = f81534_get_port_register(port, F81534_LINE_STATUS_REG, &tmp);
1307 if (status)
1308 dev_warn(&port->dev, "read LSR failed: %d\n", status);
1309}
1310
1311static int f81534_set_port_output_pin(struct usb_serial_port *port)
1312{
1313 struct f81534_serial_private *serial_priv;
1314 struct f81534_port_private *port_priv;
1315 struct usb_serial *serial;
1316 const struct f81534_port_out_pin *pins;
1317 int status;
1318 int i;
1319 u8 value;
1320 u8 idx;
1321
1322 serial = port->serial;
1323 serial_priv = usb_get_serial_data(serial);
1324 port_priv = usb_get_serial_port_data(port);
1325
1326 idx = F81534_CONF_INIT_GPIO_OFFSET + port_priv->phy_num;
1327 value = serial_priv->conf_data[idx];
1328 if (value >= F81534_CONF_GPIO_SHUTDOWN) {
1329 /*
1330 * Newer IC configure will make transceiver in shutdown mode on
1331 * initial power on. We need enable it before using UARTs.
1332 */
1333 idx = F81534_CONF_WORK_GPIO_OFFSET + port_priv->phy_num;
1334 value = serial_priv->conf_data[idx];
1335 if (value >= F81534_CONF_GPIO_SHUTDOWN)
1336 value = F81534_CONF_GPIO_RS232;
1337 }
1338
1339 pins = &f81534_port_out_pins[port_priv->phy_num];
1340
1341 for (i = 0; i < ARRAY_SIZE(pins->pin); ++i) {
1342 status = f81534_set_mask_register(serial,
1343 pins->pin[i].reg_addr, pins->pin[i].reg_mask,
1344 value & BIT(i) ? pins->pin[i].reg_mask : 0);
1345 if (status)
1346 return status;
1347 }
1348
1349 dev_dbg(&port->dev, "Output pin (M0/M1/M2): %d\n", value);
1350 return 0;
1351}
1352
1353static int f81534_port_probe(struct usb_serial_port *port)
1354{
1355 struct f81534_serial_private *serial_priv;
1356 struct f81534_port_private *port_priv;
1357 int ret;
1358 u8 value;
1359
1360 serial_priv = usb_get_serial_data(port->serial);
1361 port_priv = devm_kzalloc(&port->dev, sizeof(*port_priv), GFP_KERNEL);
1362 if (!port_priv)
1363 return -ENOMEM;
1364
1365 /*
1366 * We'll make tx frame error when baud rate from 384~500kps. So we'll
1367 * delay all tx data frame with 1bit.
1368 */
1369 port_priv->shadow_clk = F81534_UART_EN | F81534_CLK_TX_DELAY_1BIT;
1370 spin_lock_init(&port_priv->msr_lock);
1371 mutex_init(&port_priv->mcr_mutex);
1372 mutex_init(&port_priv->lcr_mutex);
1373 INIT_WORK(&port_priv->lsr_work, f81534_lsr_worker);
1374
1375 /* Assign logic-to-phy mapping */
1376 ret = f81534_logic_to_phy_port(port->serial, port);
1377 if (ret < 0)
1378 return ret;
1379
1380 port_priv->phy_num = ret;
1381 port_priv->port = port;
1382 usb_set_serial_port_data(port, port_priv);
1383 dev_dbg(&port->dev, "%s: port_number: %d, phy_num: %d\n", __func__,
1384 port->port_number, port_priv->phy_num);
1385
1386 /*
1387 * The F81532/534 will hang-up when enable LSR interrupt in IER and
1388 * occur data overrun. So we'll disable the LSR interrupt in probe()
1389 * and submit the LSR worker to clear LSR state when reported LSR error
1390 * bit with bulk-in data in f81534_process_per_serial_block().
1391 */
1392 ret = f81534_set_port_register(port, F81534_INTERRUPT_ENABLE_REG,
1393 UART_IER_RDI | UART_IER_THRI | UART_IER_MSI);
1394 if (ret)
1395 return ret;
1396
1397 value = serial_priv->conf_data[port_priv->phy_num];
1398 switch (value & F81534_PORT_CONF_MODE_MASK) {
1399 case F81534_PORT_CONF_RS485_INVERT:
1400 port_priv->shadow_clk |= F81534_CLK_RS485_MODE |
1401 F81534_CLK_RS485_INVERT;
1402 dev_dbg(&port->dev, "RS485 invert mode\n");
1403 break;
1404 case F81534_PORT_CONF_RS485:
1405 port_priv->shadow_clk |= F81534_CLK_RS485_MODE;
1406 dev_dbg(&port->dev, "RS485 mode\n");
1407 break;
1408
1409 default:
1410 case F81534_PORT_CONF_RS232:
1411 dev_dbg(&port->dev, "RS232 mode\n");
1412 break;
1413 }
1414
1415 return f81534_set_port_output_pin(port);
1416}
1417
1418static void f81534_port_remove(struct usb_serial_port *port)
1419{
1420 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1421
1422 flush_work(&port_priv->lsr_work);
1423}
1424
1425static int f81534_tiocmget(struct tty_struct *tty)
1426{
1427 struct usb_serial_port *port = tty->driver_data;
1428 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1429 int status;
1430 int r;
1431 u8 msr;
1432 u8 mcr;
1433
1434 /* Read current MSR from device */
1435 status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
1436 if (status)
1437 return status;
1438
1439 mutex_lock(&port_priv->mcr_mutex);
1440 mcr = port_priv->shadow_mcr;
1441 mutex_unlock(&port_priv->mcr_mutex);
1442
1443 r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
1444 (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
1445 (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
1446 (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
1447 (msr & UART_MSR_RI ? TIOCM_RI : 0) |
1448 (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
1449
1450 return r;
1451}
1452
1453static int f81534_tiocmset(struct tty_struct *tty, unsigned int set,
1454 unsigned int clear)
1455{
1456 struct usb_serial_port *port = tty->driver_data;
1457
1458 return f81534_update_mctrl(port, set, clear);
1459}
1460
1461static void f81534_dtr_rts(struct usb_serial_port *port, int on)
1462{
1463 if (on)
1464 f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
1465 else
1466 f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
1467}
1468
1469static int f81534_write(struct tty_struct *tty, struct usb_serial_port *port,
1470 const u8 *buf, int count)
1471{
1472 int bytes_out, status;
1473
1474 if (!count)
1475 return 0;
1476
1477 bytes_out = kfifo_in_locked(&port->write_fifo, buf, count,
1478 &port->lock);
1479
1480 status = f81534_submit_writer(port, GFP_ATOMIC);
1481 if (status) {
1482 dev_err(&port->dev, "%s: submit failed\n", __func__);
1483 return status;
1484 }
1485
1486 return bytes_out;
1487}
1488
1489static bool f81534_tx_empty(struct usb_serial_port *port)
1490{
1491 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1492
1493 return test_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1494}
1495
1496static int f81534_resume(struct usb_serial *serial)
1497{
1498 struct f81534_serial_private *serial_priv =
1499 usb_get_serial_data(serial);
1500 struct usb_serial_port *port;
1501 int error = 0;
1502 int status;
1503 size_t i;
1504
1505 /*
1506 * We'll register port 0 bulkin when port had opened, It'll take all
1507 * port received data, MSR register change and TX_EMPTY information.
1508 */
1509 mutex_lock(&serial_priv->urb_mutex);
1510
1511 if (serial_priv->opened_port) {
1512 status = f81534_submit_read_urb(serial, GFP_NOIO);
1513 if (status) {
1514 mutex_unlock(&serial_priv->urb_mutex);
1515 return status;
1516 }
1517 }
1518
1519 mutex_unlock(&serial_priv->urb_mutex);
1520
1521 for (i = 0; i < serial->num_ports; i++) {
1522 port = serial->port[i];
1523 if (!tty_port_initialized(&port->port))
1524 continue;
1525
1526 status = f81534_submit_writer(port, GFP_NOIO);
1527 if (status) {
1528 dev_err(&port->dev, "%s: submit failed\n", __func__);
1529 ++error;
1530 }
1531 }
1532
1533 if (error)
1534 return -EIO;
1535
1536 return 0;
1537}
1538
1539static struct usb_serial_driver f81534_device = {
1540 .driver = {
1541 .name = "f81534",
1542 },
1543 .description = DRIVER_DESC,
1544 .id_table = f81534_id_table,
1545 .num_bulk_in = 1,
1546 .num_bulk_out = 1,
1547 .open = f81534_open,
1548 .close = f81534_close,
1549 .write = f81534_write,
1550 .tx_empty = f81534_tx_empty,
1551 .calc_num_ports = f81534_calc_num_ports,
1552 .port_probe = f81534_port_probe,
1553 .port_remove = f81534_port_remove,
1554 .break_ctl = f81534_break_ctl,
1555 .dtr_rts = f81534_dtr_rts,
1556 .process_read_urb = f81534_process_read_urb,
1557 .get_serial = f81534_get_serial_info,
1558 .tiocmget = f81534_tiocmget,
1559 .tiocmset = f81534_tiocmset,
1560 .write_bulk_callback = f81534_write_usb_callback,
1561 .set_termios = f81534_set_termios,
1562 .resume = f81534_resume,
1563};
1564
1565static struct usb_serial_driver *const serial_drivers[] = {
1566 &f81534_device, NULL
1567};
1568
1569module_usb_serial_driver(serial_drivers, f81534_id_table);
1570
1571MODULE_DEVICE_TABLE(usb, f81534_id_table);
1572MODULE_DESCRIPTION(DRIVER_DESC);
1573MODULE_AUTHOR("Peter Hong <Peter_Hong@fintek.com.tw>");
1574MODULE_AUTHOR("Tom Tsai <Tom_Tsai@fintek.com.tw>");
1575MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * F81532/F81534 USB to Serial Ports Bridge
4 *
5 * F81532 => 2 Serial Ports
6 * F81534 => 4 Serial Ports
7 *
8 * Copyright (C) 2016 Feature Integration Technology Inc., (Fintek)
9 * Copyright (C) 2016 Tom Tsai (Tom_Tsai@fintek.com.tw)
10 * Copyright (C) 2016 Peter Hong (Peter_Hong@fintek.com.tw)
11 *
12 * The F81532/F81534 had 1 control endpoint for setting, 1 endpoint bulk-out
13 * for all serial port TX and 1 endpoint bulk-in for all serial port read in
14 * (Read Data/MSR/LSR).
15 *
16 * Write URB is fixed with 512bytes, per serial port used 128Bytes.
17 * It can be described by f81534_prepare_write_buffer()
18 *
19 * Read URB is 512Bytes max, per serial port used 128Bytes.
20 * It can be described by f81534_process_read_urb() and maybe received with
21 * 128x1,2,3,4 bytes.
22 *
23 */
24#include <linux/slab.h>
25#include <linux/tty.h>
26#include <linux/tty_flip.h>
27#include <linux/usb.h>
28#include <linux/usb/serial.h>
29#include <linux/serial_reg.h>
30#include <linux/module.h>
31#include <linux/uaccess.h>
32
33/* Serial Port register Address */
34#define F81534_UART_BASE_ADDRESS 0x1200
35#define F81534_UART_OFFSET 0x10
36#define F81534_DIVISOR_LSB_REG (0x00 + F81534_UART_BASE_ADDRESS)
37#define F81534_DIVISOR_MSB_REG (0x01 + F81534_UART_BASE_ADDRESS)
38#define F81534_INTERRUPT_ENABLE_REG (0x01 + F81534_UART_BASE_ADDRESS)
39#define F81534_FIFO_CONTROL_REG (0x02 + F81534_UART_BASE_ADDRESS)
40#define F81534_LINE_CONTROL_REG (0x03 + F81534_UART_BASE_ADDRESS)
41#define F81534_MODEM_CONTROL_REG (0x04 + F81534_UART_BASE_ADDRESS)
42#define F81534_LINE_STATUS_REG (0x05 + F81534_UART_BASE_ADDRESS)
43#define F81534_MODEM_STATUS_REG (0x06 + F81534_UART_BASE_ADDRESS)
44#define F81534_CLOCK_REG (0x08 + F81534_UART_BASE_ADDRESS)
45#define F81534_CONFIG1_REG (0x09 + F81534_UART_BASE_ADDRESS)
46
47#define F81534_DEF_CONF_ADDRESS_START 0x3000
48#define F81534_DEF_CONF_SIZE 8
49
50#define F81534_CUSTOM_ADDRESS_START 0x2f00
51#define F81534_CUSTOM_DATA_SIZE 0x10
52#define F81534_CUSTOM_NO_CUSTOM_DATA 0xff
53#define F81534_CUSTOM_VALID_TOKEN 0xf0
54#define F81534_CONF_OFFSET 1
55#define F81534_CONF_GPIO_OFFSET 4
56
57#define F81534_MAX_DATA_BLOCK 64
58#define F81534_MAX_BUS_RETRY 20
59
60/* Default URB timeout for USB operations */
61#define F81534_USB_MAX_RETRY 10
62#define F81534_USB_TIMEOUT 2000
63#define F81534_SET_GET_REGISTER 0xA0
64
65#define F81534_NUM_PORT 4
66#define F81534_UNUSED_PORT 0xff
67#define F81534_WRITE_BUFFER_SIZE 512
68
69#define DRIVER_DESC "Fintek F81532/F81534"
70#define FINTEK_VENDOR_ID_1 0x1934
71#define FINTEK_VENDOR_ID_2 0x2C42
72#define FINTEK_DEVICE_ID 0x1202
73#define F81534_MAX_TX_SIZE 124
74#define F81534_MAX_RX_SIZE 124
75#define F81534_RECEIVE_BLOCK_SIZE 128
76#define F81534_MAX_RECEIVE_BLOCK_SIZE 512
77
78#define F81534_TOKEN_RECEIVE 0x01
79#define F81534_TOKEN_WRITE 0x02
80#define F81534_TOKEN_TX_EMPTY 0x03
81#define F81534_TOKEN_MSR_CHANGE 0x04
82
83/*
84 * We used interal SPI bus to access FLASH section. We must wait the SPI bus to
85 * idle if we performed any command.
86 *
87 * SPI Bus status register: F81534_BUS_REG_STATUS
88 * Bit 0/1 : BUSY
89 * Bit 2 : IDLE
90 */
91#define F81534_BUS_BUSY (BIT(0) | BIT(1))
92#define F81534_BUS_IDLE BIT(2)
93#define F81534_BUS_READ_DATA 0x1004
94#define F81534_BUS_REG_STATUS 0x1003
95#define F81534_BUS_REG_START 0x1002
96#define F81534_BUS_REG_END 0x1001
97
98#define F81534_CMD_READ 0x03
99
100#define F81534_DEFAULT_BAUD_RATE 9600
101
102#define F81534_PORT_CONF_RS232 0
103#define F81534_PORT_CONF_RS485 BIT(0)
104#define F81534_PORT_CONF_RS485_INVERT (BIT(0) | BIT(1))
105#define F81534_PORT_CONF_MODE_MASK GENMASK(1, 0)
106#define F81534_PORT_CONF_DISABLE_PORT BIT(3)
107#define F81534_PORT_CONF_NOT_EXIST_PORT BIT(7)
108#define F81534_PORT_UNAVAILABLE \
109 (F81534_PORT_CONF_DISABLE_PORT | F81534_PORT_CONF_NOT_EXIST_PORT)
110
111
112#define F81534_1X_RXTRIGGER 0xc3
113#define F81534_8X_RXTRIGGER 0xcf
114
115/*
116 * F81532/534 Clock registers (offset +08h)
117 *
118 * Bit0: UART Enable (always on)
119 * Bit2-1: Clock source selector
120 * 00: 1.846MHz.
121 * 01: 18.46MHz.
122 * 10: 24MHz.
123 * 11: 14.77MHz.
124 * Bit4: Auto direction(RTS) control (RTS pin Low when TX)
125 * Bit5: Invert direction(RTS) when Bit4 enabled (RTS pin high when TX)
126 */
127
128#define F81534_UART_EN BIT(0)
129#define F81534_CLK_1_846_MHZ 0
130#define F81534_CLK_18_46_MHZ BIT(1)
131#define F81534_CLK_24_MHZ BIT(2)
132#define F81534_CLK_14_77_MHZ (BIT(1) | BIT(2))
133#define F81534_CLK_MASK GENMASK(2, 1)
134#define F81534_CLK_TX_DELAY_1BIT BIT(3)
135#define F81534_CLK_RS485_MODE BIT(4)
136#define F81534_CLK_RS485_INVERT BIT(5)
137
138static const struct usb_device_id f81534_id_table[] = {
139 { USB_DEVICE(FINTEK_VENDOR_ID_1, FINTEK_DEVICE_ID) },
140 { USB_DEVICE(FINTEK_VENDOR_ID_2, FINTEK_DEVICE_ID) },
141 {} /* Terminating entry */
142};
143
144#define F81534_TX_EMPTY_BIT 0
145
146struct f81534_serial_private {
147 u8 conf_data[F81534_DEF_CONF_SIZE];
148 int tty_idx[F81534_NUM_PORT];
149 u8 setting_idx;
150 int opened_port;
151 struct mutex urb_mutex;
152};
153
154struct f81534_port_private {
155 struct mutex mcr_mutex;
156 struct mutex lcr_mutex;
157 struct work_struct lsr_work;
158 struct usb_serial_port *port;
159 unsigned long tx_empty;
160 spinlock_t msr_lock;
161 u32 baud_base;
162 u8 shadow_mcr;
163 u8 shadow_lcr;
164 u8 shadow_msr;
165 u8 shadow_clk;
166 u8 phy_num;
167};
168
169struct f81534_pin_data {
170 const u16 reg_addr;
171 const u8 reg_mask;
172};
173
174struct f81534_port_out_pin {
175 struct f81534_pin_data pin[3];
176};
177
178/* Pin output value for M2/M1/M0(SD) */
179static const struct f81534_port_out_pin f81534_port_out_pins[] = {
180 { { { 0x2ae8, BIT(7) }, { 0x2a90, BIT(5) }, { 0x2a90, BIT(4) } } },
181 { { { 0x2ae8, BIT(6) }, { 0x2ae8, BIT(0) }, { 0x2ae8, BIT(3) } } },
182 { { { 0x2a90, BIT(0) }, { 0x2ae8, BIT(2) }, { 0x2a80, BIT(6) } } },
183 { { { 0x2a90, BIT(3) }, { 0x2a90, BIT(2) }, { 0x2a90, BIT(1) } } },
184};
185
186static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
187static u8 const clock_table[] = { F81534_CLK_1_846_MHZ, F81534_CLK_14_77_MHZ,
188 F81534_CLK_18_46_MHZ, F81534_CLK_24_MHZ };
189
190static int f81534_logic_to_phy_port(struct usb_serial *serial,
191 struct usb_serial_port *port)
192{
193 struct f81534_serial_private *serial_priv =
194 usb_get_serial_data(port->serial);
195 int count = 0;
196 int i;
197
198 for (i = 0; i < F81534_NUM_PORT; ++i) {
199 if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
200 continue;
201
202 if (port->port_number == count)
203 return i;
204
205 ++count;
206 }
207
208 return -ENODEV;
209}
210
211static int f81534_set_register(struct usb_serial *serial, u16 reg, u8 data)
212{
213 struct usb_interface *interface = serial->interface;
214 struct usb_device *dev = serial->dev;
215 size_t count = F81534_USB_MAX_RETRY;
216 int status;
217 u8 *tmp;
218
219 tmp = kmalloc(sizeof(u8), GFP_KERNEL);
220 if (!tmp)
221 return -ENOMEM;
222
223 *tmp = data;
224
225 /*
226 * Our device maybe not reply when heavily loading, We'll retry for
227 * F81534_USB_MAX_RETRY times.
228 */
229 while (count--) {
230 status = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
231 F81534_SET_GET_REGISTER,
232 USB_TYPE_VENDOR | USB_DIR_OUT,
233 reg, 0, tmp, sizeof(u8),
234 F81534_USB_TIMEOUT);
235 if (status > 0) {
236 status = 0;
237 break;
238 } else if (status == 0) {
239 status = -EIO;
240 }
241 }
242
243 if (status < 0) {
244 dev_err(&interface->dev, "%s: reg: %x data: %x failed: %d\n",
245 __func__, reg, data, status);
246 }
247
248 kfree(tmp);
249 return status;
250}
251
252static int f81534_get_register(struct usb_serial *serial, u16 reg, u8 *data)
253{
254 struct usb_interface *interface = serial->interface;
255 struct usb_device *dev = serial->dev;
256 size_t count = F81534_USB_MAX_RETRY;
257 int status;
258 u8 *tmp;
259
260 tmp = kmalloc(sizeof(u8), GFP_KERNEL);
261 if (!tmp)
262 return -ENOMEM;
263
264 /*
265 * Our device maybe not reply when heavily loading, We'll retry for
266 * F81534_USB_MAX_RETRY times.
267 */
268 while (count--) {
269 status = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
270 F81534_SET_GET_REGISTER,
271 USB_TYPE_VENDOR | USB_DIR_IN,
272 reg, 0, tmp, sizeof(u8),
273 F81534_USB_TIMEOUT);
274 if (status > 0) {
275 status = 0;
276 break;
277 } else if (status == 0) {
278 status = -EIO;
279 }
280 }
281
282 if (status < 0) {
283 dev_err(&interface->dev, "%s: reg: %x failed: %d\n", __func__,
284 reg, status);
285 goto end;
286 }
287
288 *data = *tmp;
289
290end:
291 kfree(tmp);
292 return status;
293}
294
295static int f81534_set_mask_register(struct usb_serial *serial, u16 reg,
296 u8 mask, u8 data)
297{
298 int status;
299 u8 tmp;
300
301 status = f81534_get_register(serial, reg, &tmp);
302 if (status)
303 return status;
304
305 tmp &= ~mask;
306 tmp |= (mask & data);
307
308 return f81534_set_register(serial, reg, tmp);
309}
310
311static int f81534_set_phy_port_register(struct usb_serial *serial, int phy,
312 u16 reg, u8 data)
313{
314 return f81534_set_register(serial, reg + F81534_UART_OFFSET * phy,
315 data);
316}
317
318static int f81534_get_phy_port_register(struct usb_serial *serial, int phy,
319 u16 reg, u8 *data)
320{
321 return f81534_get_register(serial, reg + F81534_UART_OFFSET * phy,
322 data);
323}
324
325static int f81534_set_port_register(struct usb_serial_port *port, u16 reg,
326 u8 data)
327{
328 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
329
330 return f81534_set_register(port->serial,
331 reg + port_priv->phy_num * F81534_UART_OFFSET, data);
332}
333
334static int f81534_get_port_register(struct usb_serial_port *port, u16 reg,
335 u8 *data)
336{
337 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
338
339 return f81534_get_register(port->serial,
340 reg + port_priv->phy_num * F81534_UART_OFFSET, data);
341}
342
343/*
344 * If we try to access the internal flash via SPI bus, we should check the bus
345 * status for every command. e.g., F81534_BUS_REG_START/F81534_BUS_REG_END
346 */
347static int f81534_wait_for_spi_idle(struct usb_serial *serial)
348{
349 size_t count = F81534_MAX_BUS_RETRY;
350 u8 tmp;
351 int status;
352
353 do {
354 status = f81534_get_register(serial, F81534_BUS_REG_STATUS,
355 &tmp);
356 if (status)
357 return status;
358
359 if (tmp & F81534_BUS_BUSY)
360 continue;
361
362 if (tmp & F81534_BUS_IDLE)
363 break;
364
365 } while (--count);
366
367 if (!count) {
368 dev_err(&serial->interface->dev,
369 "%s: timed out waiting for idle SPI bus\n",
370 __func__);
371 return -EIO;
372 }
373
374 return f81534_set_register(serial, F81534_BUS_REG_STATUS,
375 tmp & ~F81534_BUS_IDLE);
376}
377
378static int f81534_get_spi_register(struct usb_serial *serial, u16 reg,
379 u8 *data)
380{
381 int status;
382
383 status = f81534_get_register(serial, reg, data);
384 if (status)
385 return status;
386
387 return f81534_wait_for_spi_idle(serial);
388}
389
390static int f81534_set_spi_register(struct usb_serial *serial, u16 reg, u8 data)
391{
392 int status;
393
394 status = f81534_set_register(serial, reg, data);
395 if (status)
396 return status;
397
398 return f81534_wait_for_spi_idle(serial);
399}
400
401static int f81534_read_flash(struct usb_serial *serial, u32 address,
402 size_t size, u8 *buf)
403{
404 u8 tmp_buf[F81534_MAX_DATA_BLOCK];
405 size_t block = 0;
406 size_t read_size;
407 size_t count;
408 int status;
409 int offset;
410 u16 reg_tmp;
411
412 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
413 F81534_CMD_READ);
414 if (status)
415 return status;
416
417 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
418 (address >> 16) & 0xff);
419 if (status)
420 return status;
421
422 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
423 (address >> 8) & 0xff);
424 if (status)
425 return status;
426
427 status = f81534_set_spi_register(serial, F81534_BUS_REG_START,
428 (address >> 0) & 0xff);
429 if (status)
430 return status;
431
432 /* Continuous read mode */
433 do {
434 read_size = min_t(size_t, F81534_MAX_DATA_BLOCK, size);
435
436 for (count = 0; count < read_size; ++count) {
437 /* To write F81534_BUS_REG_END when final byte */
438 if (size <= F81534_MAX_DATA_BLOCK &&
439 read_size == count + 1)
440 reg_tmp = F81534_BUS_REG_END;
441 else
442 reg_tmp = F81534_BUS_REG_START;
443
444 /*
445 * Dummy code, force IC to generate a read pulse, the
446 * set of value 0xf1 is dont care (any value is ok)
447 */
448 status = f81534_set_spi_register(serial, reg_tmp,
449 0xf1);
450 if (status)
451 return status;
452
453 status = f81534_get_spi_register(serial,
454 F81534_BUS_READ_DATA,
455 &tmp_buf[count]);
456 if (status)
457 return status;
458
459 offset = count + block * F81534_MAX_DATA_BLOCK;
460 buf[offset] = tmp_buf[count];
461 }
462
463 size -= read_size;
464 ++block;
465 } while (size);
466
467 return 0;
468}
469
470static void f81534_prepare_write_buffer(struct usb_serial_port *port, u8 *buf)
471{
472 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
473 int phy_num = port_priv->phy_num;
474 u8 tx_len;
475 int i;
476
477 /*
478 * The block layout is fixed with 4x128 Bytes, per 128 Bytes a port.
479 * index 0: port phy idx (e.g., 0,1,2,3)
480 * index 1: only F81534_TOKEN_WRITE
481 * index 2: serial TX out length
482 * index 3: fix to 0
483 * index 4~127: serial out data block
484 */
485 for (i = 0; i < F81534_NUM_PORT; ++i) {
486 buf[i * F81534_RECEIVE_BLOCK_SIZE] = i;
487 buf[i * F81534_RECEIVE_BLOCK_SIZE + 1] = F81534_TOKEN_WRITE;
488 buf[i * F81534_RECEIVE_BLOCK_SIZE + 2] = 0;
489 buf[i * F81534_RECEIVE_BLOCK_SIZE + 3] = 0;
490 }
491
492 tx_len = kfifo_out_locked(&port->write_fifo,
493 &buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 4],
494 F81534_MAX_TX_SIZE, &port->lock);
495
496 buf[phy_num * F81534_RECEIVE_BLOCK_SIZE + 2] = tx_len;
497}
498
499static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
500{
501 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
502 struct urb *urb;
503 unsigned long flags;
504 int result;
505
506 /* Check is any data in write_fifo */
507 spin_lock_irqsave(&port->lock, flags);
508
509 if (kfifo_is_empty(&port->write_fifo)) {
510 spin_unlock_irqrestore(&port->lock, flags);
511 return 0;
512 }
513
514 spin_unlock_irqrestore(&port->lock, flags);
515
516 /* Check H/W is TXEMPTY */
517 if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
518 return 0;
519
520 urb = port->write_urbs[0];
521 f81534_prepare_write_buffer(port, port->bulk_out_buffers[0]);
522 urb->transfer_buffer_length = F81534_WRITE_BUFFER_SIZE;
523
524 result = usb_submit_urb(urb, mem_flags);
525 if (result) {
526 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
527 dev_err(&port->dev, "%s: submit failed: %d\n", __func__,
528 result);
529 return result;
530 }
531
532 usb_serial_port_softint(port);
533 return 0;
534}
535
536static u32 f81534_calc_baud_divisor(u32 baudrate, u32 clockrate)
537{
538 if (!baudrate)
539 return 0;
540
541 /* Round to nearest divisor */
542 return DIV_ROUND_CLOSEST(clockrate, baudrate);
543}
544
545static int f81534_find_clk(u32 baudrate)
546{
547 int idx;
548
549 for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
550 if (baudrate <= baudrate_table[idx] &&
551 baudrate_table[idx] % baudrate == 0)
552 return idx;
553 }
554
555 return -EINVAL;
556}
557
558static int f81534_set_port_config(struct usb_serial_port *port,
559 struct tty_struct *tty, u32 baudrate, u32 old_baudrate, u8 lcr)
560{
561 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
562 u32 divisor;
563 int status;
564 int i;
565 int idx;
566 u8 value;
567 u32 baud_list[] = {baudrate, old_baudrate, F81534_DEFAULT_BAUD_RATE};
568
569 for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
570 idx = f81534_find_clk(baud_list[i]);
571 if (idx >= 0) {
572 baudrate = baud_list[i];
573 tty_encode_baud_rate(tty, baudrate, baudrate);
574 break;
575 }
576 }
577
578 if (idx < 0)
579 return -EINVAL;
580
581 port_priv->baud_base = baudrate_table[idx];
582 port_priv->shadow_clk &= ~F81534_CLK_MASK;
583 port_priv->shadow_clk |= clock_table[idx];
584
585 status = f81534_set_port_register(port, F81534_CLOCK_REG,
586 port_priv->shadow_clk);
587 if (status) {
588 dev_err(&port->dev, "CLOCK_REG setting failed\n");
589 return status;
590 }
591
592 if (baudrate <= 1200)
593 value = F81534_1X_RXTRIGGER; /* 128 FIFO & TL: 1x */
594 else
595 value = F81534_8X_RXTRIGGER; /* 128 FIFO & TL: 8x */
596
597 status = f81534_set_port_register(port, F81534_CONFIG1_REG, value);
598 if (status) {
599 dev_err(&port->dev, "%s: CONFIG1 setting failed\n", __func__);
600 return status;
601 }
602
603 if (baudrate <= 1200)
604 value = UART_FCR_TRIGGER_1 | UART_FCR_ENABLE_FIFO; /* TL: 1 */
605 else
606 value = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO; /* TL: 8 */
607
608 status = f81534_set_port_register(port, F81534_FIFO_CONTROL_REG,
609 value);
610 if (status) {
611 dev_err(&port->dev, "%s: FCR setting failed\n", __func__);
612 return status;
613 }
614
615 divisor = f81534_calc_baud_divisor(baudrate, port_priv->baud_base);
616
617 mutex_lock(&port_priv->lcr_mutex);
618
619 value = UART_LCR_DLAB;
620 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
621 value);
622 if (status) {
623 dev_err(&port->dev, "%s: set LCR failed\n", __func__);
624 goto out_unlock;
625 }
626
627 value = divisor & 0xff;
628 status = f81534_set_port_register(port, F81534_DIVISOR_LSB_REG, value);
629 if (status) {
630 dev_err(&port->dev, "%s: set DLAB LSB failed\n", __func__);
631 goto out_unlock;
632 }
633
634 value = (divisor >> 8) & 0xff;
635 status = f81534_set_port_register(port, F81534_DIVISOR_MSB_REG, value);
636 if (status) {
637 dev_err(&port->dev, "%s: set DLAB MSB failed\n", __func__);
638 goto out_unlock;
639 }
640
641 value = lcr | (port_priv->shadow_lcr & UART_LCR_SBC);
642 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
643 value);
644 if (status) {
645 dev_err(&port->dev, "%s: set LCR failed\n", __func__);
646 goto out_unlock;
647 }
648
649 port_priv->shadow_lcr = value;
650out_unlock:
651 mutex_unlock(&port_priv->lcr_mutex);
652
653 return status;
654}
655
656static void f81534_break_ctl(struct tty_struct *tty, int break_state)
657{
658 struct usb_serial_port *port = tty->driver_data;
659 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
660 int status;
661
662 mutex_lock(&port_priv->lcr_mutex);
663
664 if (break_state)
665 port_priv->shadow_lcr |= UART_LCR_SBC;
666 else
667 port_priv->shadow_lcr &= ~UART_LCR_SBC;
668
669 status = f81534_set_port_register(port, F81534_LINE_CONTROL_REG,
670 port_priv->shadow_lcr);
671 if (status)
672 dev_err(&port->dev, "set break failed: %d\n", status);
673
674 mutex_unlock(&port_priv->lcr_mutex);
675}
676
677static int f81534_update_mctrl(struct usb_serial_port *port, unsigned int set,
678 unsigned int clear)
679{
680 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
681 int status;
682 u8 tmp;
683
684 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
685 return 0; /* no change */
686
687 mutex_lock(&port_priv->mcr_mutex);
688
689 /* 'Set' takes precedence over 'Clear' */
690 clear &= ~set;
691
692 /* Always enable UART_MCR_OUT2 */
693 tmp = UART_MCR_OUT2 | port_priv->shadow_mcr;
694
695 if (clear & TIOCM_DTR)
696 tmp &= ~UART_MCR_DTR;
697
698 if (clear & TIOCM_RTS)
699 tmp &= ~UART_MCR_RTS;
700
701 if (set & TIOCM_DTR)
702 tmp |= UART_MCR_DTR;
703
704 if (set & TIOCM_RTS)
705 tmp |= UART_MCR_RTS;
706
707 status = f81534_set_port_register(port, F81534_MODEM_CONTROL_REG, tmp);
708 if (status < 0) {
709 dev_err(&port->dev, "%s: MCR write failed\n", __func__);
710 mutex_unlock(&port_priv->mcr_mutex);
711 return status;
712 }
713
714 port_priv->shadow_mcr = tmp;
715 mutex_unlock(&port_priv->mcr_mutex);
716 return 0;
717}
718
719/*
720 * This function will search the data area with token F81534_CUSTOM_VALID_TOKEN
721 * for latest configuration index. If nothing found
722 * (*index = F81534_CUSTOM_NO_CUSTOM_DATA), We'll load default configure in
723 * F81534_DEF_CONF_ADDRESS_START section.
724 *
725 * Due to we only use block0 to save data, so *index should be 0 or
726 * F81534_CUSTOM_NO_CUSTOM_DATA.
727 */
728static int f81534_find_config_idx(struct usb_serial *serial, u8 *index)
729{
730 u8 tmp;
731 int status;
732
733 status = f81534_read_flash(serial, F81534_CUSTOM_ADDRESS_START, 1,
734 &tmp);
735 if (status) {
736 dev_err(&serial->interface->dev, "%s: read failed: %d\n",
737 __func__, status);
738 return status;
739 }
740
741 /* We'll use the custom data when the data is valid. */
742 if (tmp == F81534_CUSTOM_VALID_TOKEN)
743 *index = 0;
744 else
745 *index = F81534_CUSTOM_NO_CUSTOM_DATA;
746
747 return 0;
748}
749
750/*
751 * The F81532/534 will not report serial port to USB serial subsystem when
752 * H/W DCD/DSR/CTS/RI/RX pin connected to ground.
753 *
754 * To detect RX pin status, we'll enable MCR interal loopback, disable it and
755 * delayed for 60ms. It connected to ground If LSR register report UART_LSR_BI.
756 */
757static bool f81534_check_port_hw_disabled(struct usb_serial *serial, int phy)
758{
759 int status;
760 u8 old_mcr;
761 u8 msr;
762 u8 lsr;
763 u8 msr_mask;
764
765 msr_mask = UART_MSR_DCD | UART_MSR_RI | UART_MSR_DSR | UART_MSR_CTS;
766
767 status = f81534_get_phy_port_register(serial, phy,
768 F81534_MODEM_STATUS_REG, &msr);
769 if (status)
770 return false;
771
772 if ((msr & msr_mask) != msr_mask)
773 return false;
774
775 status = f81534_set_phy_port_register(serial, phy,
776 F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
777 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
778 if (status)
779 return false;
780
781 status = f81534_get_phy_port_register(serial, phy,
782 F81534_MODEM_CONTROL_REG, &old_mcr);
783 if (status)
784 return false;
785
786 status = f81534_set_phy_port_register(serial, phy,
787 F81534_MODEM_CONTROL_REG, UART_MCR_LOOP);
788 if (status)
789 return false;
790
791 status = f81534_set_phy_port_register(serial, phy,
792 F81534_MODEM_CONTROL_REG, 0x0);
793 if (status)
794 return false;
795
796 msleep(60);
797
798 status = f81534_get_phy_port_register(serial, phy,
799 F81534_LINE_STATUS_REG, &lsr);
800 if (status)
801 return false;
802
803 status = f81534_set_phy_port_register(serial, phy,
804 F81534_MODEM_CONTROL_REG, old_mcr);
805 if (status)
806 return false;
807
808 if ((lsr & UART_LSR_BI) == UART_LSR_BI)
809 return true;
810
811 return false;
812}
813
814/*
815 * We had 2 generation of F81532/534 IC. All has an internal storage.
816 *
817 * 1st is pure USB-to-TTL RS232 IC and designed for 4 ports only, no any
818 * internal data will used. All mode and gpio control should manually set
819 * by AP or Driver and all storage space value are 0xff. The
820 * f81534_calc_num_ports() will run to final we marked as "oldest version"
821 * for this IC.
822 *
823 * 2rd is designed to more generic to use any transceiver and this is our
824 * mass production type. We'll save data in F81534_CUSTOM_ADDRESS_START
825 * (0x2f00) with 9bytes. The 1st byte is a indicater. If the token is
826 * F81534_CUSTOM_VALID_TOKEN(0xf0), the IC is 2nd gen type, the following
827 * 4bytes save port mode (0:RS232/1:RS485 Invert/2:RS485), and the last
828 * 4bytes save GPIO state(value from 0~7 to represent 3 GPIO output pin).
829 * The f81534_calc_num_ports() will run to "new style" with checking
830 * F81534_PORT_UNAVAILABLE section.
831 */
832static int f81534_calc_num_ports(struct usb_serial *serial,
833 struct usb_serial_endpoints *epds)
834{
835 struct f81534_serial_private *serial_priv;
836 struct device *dev = &serial->interface->dev;
837 int size_bulk_in = usb_endpoint_maxp(epds->bulk_in[0]);
838 int size_bulk_out = usb_endpoint_maxp(epds->bulk_out[0]);
839 u8 num_port = 0;
840 int index = 0;
841 int status;
842 int i;
843
844 if (size_bulk_out != F81534_WRITE_BUFFER_SIZE ||
845 size_bulk_in != F81534_MAX_RECEIVE_BLOCK_SIZE) {
846 dev_err(dev, "unsupported endpoint max packet size\n");
847 return -ENODEV;
848 }
849
850 serial_priv = devm_kzalloc(&serial->interface->dev,
851 sizeof(*serial_priv), GFP_KERNEL);
852 if (!serial_priv)
853 return -ENOMEM;
854
855 usb_set_serial_data(serial, serial_priv);
856 mutex_init(&serial_priv->urb_mutex);
857
858 /* Check had custom setting */
859 status = f81534_find_config_idx(serial, &serial_priv->setting_idx);
860 if (status) {
861 dev_err(&serial->interface->dev, "%s: find idx failed: %d\n",
862 __func__, status);
863 return status;
864 }
865
866 /*
867 * We'll read custom data only when data available, otherwise we'll
868 * read default value instead.
869 */
870 if (serial_priv->setting_idx != F81534_CUSTOM_NO_CUSTOM_DATA) {
871 status = f81534_read_flash(serial,
872 F81534_CUSTOM_ADDRESS_START +
873 F81534_CONF_OFFSET,
874 sizeof(serial_priv->conf_data),
875 serial_priv->conf_data);
876 if (status) {
877 dev_err(&serial->interface->dev,
878 "%s: get custom data failed: %d\n",
879 __func__, status);
880 return status;
881 }
882
883 dev_dbg(&serial->interface->dev,
884 "%s: read config from block: %d\n", __func__,
885 serial_priv->setting_idx);
886 } else {
887 /* Read default board setting */
888 status = f81534_read_flash(serial,
889 F81534_DEF_CONF_ADDRESS_START,
890 sizeof(serial_priv->conf_data),
891 serial_priv->conf_data);
892 if (status) {
893 dev_err(&serial->interface->dev,
894 "%s: read failed: %d\n", __func__,
895 status);
896 return status;
897 }
898
899 dev_dbg(&serial->interface->dev, "%s: read default config\n",
900 __func__);
901 }
902
903 /* New style, find all possible ports */
904 for (i = 0; i < F81534_NUM_PORT; ++i) {
905 if (f81534_check_port_hw_disabled(serial, i))
906 serial_priv->conf_data[i] |= F81534_PORT_UNAVAILABLE;
907
908 if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
909 continue;
910
911 ++num_port;
912 }
913
914 if (!num_port) {
915 dev_warn(&serial->interface->dev,
916 "no config found, assuming 4 ports\n");
917 num_port = 4; /* Nothing found, oldest version IC */
918 }
919
920 /* Assign phy-to-logic mapping */
921 for (i = 0; i < F81534_NUM_PORT; ++i) {
922 if (serial_priv->conf_data[i] & F81534_PORT_UNAVAILABLE)
923 continue;
924
925 serial_priv->tty_idx[i] = index++;
926 dev_dbg(&serial->interface->dev,
927 "%s: phy_num: %d, tty_idx: %d\n", __func__, i,
928 serial_priv->tty_idx[i]);
929 }
930
931 /*
932 * Setup bulk-out endpoint multiplexing. All ports share the same
933 * bulk-out endpoint.
934 */
935 BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < F81534_NUM_PORT);
936
937 for (i = 1; i < num_port; ++i)
938 epds->bulk_out[i] = epds->bulk_out[0];
939
940 epds->num_bulk_out = num_port;
941
942 return num_port;
943}
944
945static void f81534_set_termios(struct tty_struct *tty,
946 struct usb_serial_port *port,
947 struct ktermios *old_termios)
948{
949 u8 new_lcr = 0;
950 int status;
951 u32 baud;
952 u32 old_baud;
953
954 if (C_BAUD(tty) == B0)
955 f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
956 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
957 f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
958
959 if (C_PARENB(tty)) {
960 new_lcr |= UART_LCR_PARITY;
961
962 if (!C_PARODD(tty))
963 new_lcr |= UART_LCR_EPAR;
964
965 if (C_CMSPAR(tty))
966 new_lcr |= UART_LCR_SPAR;
967 }
968
969 if (C_CSTOPB(tty))
970 new_lcr |= UART_LCR_STOP;
971
972 switch (C_CSIZE(tty)) {
973 case CS5:
974 new_lcr |= UART_LCR_WLEN5;
975 break;
976 case CS6:
977 new_lcr |= UART_LCR_WLEN6;
978 break;
979 case CS7:
980 new_lcr |= UART_LCR_WLEN7;
981 break;
982 default:
983 case CS8:
984 new_lcr |= UART_LCR_WLEN8;
985 break;
986 }
987
988 baud = tty_get_baud_rate(tty);
989 if (!baud)
990 return;
991
992 if (old_termios)
993 old_baud = tty_termios_baud_rate(old_termios);
994 else
995 old_baud = F81534_DEFAULT_BAUD_RATE;
996
997 dev_dbg(&port->dev, "%s: baud: %d\n", __func__, baud);
998
999 status = f81534_set_port_config(port, tty, baud, old_baud, new_lcr);
1000 if (status < 0) {
1001 dev_err(&port->dev, "%s: set port config failed: %d\n",
1002 __func__, status);
1003 }
1004}
1005
1006static int f81534_submit_read_urb(struct usb_serial *serial, gfp_t flags)
1007{
1008 return usb_serial_generic_submit_read_urbs(serial->port[0], flags);
1009}
1010
1011static void f81534_msr_changed(struct usb_serial_port *port, u8 msr)
1012{
1013 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1014 struct tty_struct *tty;
1015 unsigned long flags;
1016 u8 old_msr;
1017
1018 if (!(msr & UART_MSR_ANY_DELTA))
1019 return;
1020
1021 spin_lock_irqsave(&port_priv->msr_lock, flags);
1022 old_msr = port_priv->shadow_msr;
1023 port_priv->shadow_msr = msr;
1024 spin_unlock_irqrestore(&port_priv->msr_lock, flags);
1025
1026 dev_dbg(&port->dev, "%s: MSR from %02x to %02x\n", __func__, old_msr,
1027 msr);
1028
1029 /* Update input line counters */
1030 if (msr & UART_MSR_DCTS)
1031 port->icount.cts++;
1032 if (msr & UART_MSR_DDSR)
1033 port->icount.dsr++;
1034 if (msr & UART_MSR_DDCD)
1035 port->icount.dcd++;
1036 if (msr & UART_MSR_TERI)
1037 port->icount.rng++;
1038
1039 wake_up_interruptible(&port->port.delta_msr_wait);
1040
1041 if (!(msr & UART_MSR_DDCD))
1042 return;
1043
1044 dev_dbg(&port->dev, "%s: DCD Changed: phy_num: %d from %x to %x\n",
1045 __func__, port_priv->phy_num, old_msr, msr);
1046
1047 tty = tty_port_tty_get(&port->port);
1048 if (!tty)
1049 return;
1050
1051 usb_serial_handle_dcd_change(port, tty, msr & UART_MSR_DCD);
1052 tty_kref_put(tty);
1053}
1054
1055static int f81534_read_msr(struct usb_serial_port *port)
1056{
1057 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1058 unsigned long flags;
1059 int status;
1060 u8 msr;
1061
1062 /* Get MSR initial value */
1063 status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
1064 if (status)
1065 return status;
1066
1067 /* Force update current state */
1068 spin_lock_irqsave(&port_priv->msr_lock, flags);
1069 port_priv->shadow_msr = msr;
1070 spin_unlock_irqrestore(&port_priv->msr_lock, flags);
1071
1072 return 0;
1073}
1074
1075static int f81534_open(struct tty_struct *tty, struct usb_serial_port *port)
1076{
1077 struct f81534_serial_private *serial_priv =
1078 usb_get_serial_data(port->serial);
1079 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1080 int status;
1081
1082 status = f81534_set_port_register(port,
1083 F81534_FIFO_CONTROL_REG, UART_FCR_ENABLE_FIFO |
1084 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
1085 if (status) {
1086 dev_err(&port->dev, "%s: Clear FIFO failed: %d\n", __func__,
1087 status);
1088 return status;
1089 }
1090
1091 if (tty)
1092 f81534_set_termios(tty, port, NULL);
1093
1094 status = f81534_read_msr(port);
1095 if (status)
1096 return status;
1097
1098 mutex_lock(&serial_priv->urb_mutex);
1099
1100 /* Submit Read URBs for first port opened */
1101 if (!serial_priv->opened_port) {
1102 status = f81534_submit_read_urb(port->serial, GFP_KERNEL);
1103 if (status)
1104 goto exit;
1105 }
1106
1107 serial_priv->opened_port++;
1108
1109exit:
1110 mutex_unlock(&serial_priv->urb_mutex);
1111
1112 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1113 return status;
1114}
1115
1116static void f81534_close(struct usb_serial_port *port)
1117{
1118 struct f81534_serial_private *serial_priv =
1119 usb_get_serial_data(port->serial);
1120 struct usb_serial_port *port0 = port->serial->port[0];
1121 unsigned long flags;
1122 size_t i;
1123
1124 usb_kill_urb(port->write_urbs[0]);
1125
1126 spin_lock_irqsave(&port->lock, flags);
1127 kfifo_reset_out(&port->write_fifo);
1128 spin_unlock_irqrestore(&port->lock, flags);
1129
1130 /* Kill Read URBs when final port closed */
1131 mutex_lock(&serial_priv->urb_mutex);
1132 serial_priv->opened_port--;
1133
1134 if (!serial_priv->opened_port) {
1135 for (i = 0; i < ARRAY_SIZE(port0->read_urbs); ++i)
1136 usb_kill_urb(port0->read_urbs[i]);
1137 }
1138
1139 mutex_unlock(&serial_priv->urb_mutex);
1140}
1141
1142static int f81534_get_serial_info(struct usb_serial_port *port,
1143 struct serial_struct __user *retinfo)
1144{
1145 struct f81534_port_private *port_priv;
1146 struct serial_struct tmp;
1147
1148 port_priv = usb_get_serial_port_data(port);
1149
1150 memset(&tmp, 0, sizeof(tmp));
1151
1152 tmp.type = PORT_16550A;
1153 tmp.port = port->port_number;
1154 tmp.line = port->minor;
1155 tmp.baud_base = port_priv->baud_base;
1156
1157 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1158 return -EFAULT;
1159
1160 return 0;
1161}
1162
1163static int f81534_ioctl(struct tty_struct *tty, unsigned int cmd,
1164 unsigned long arg)
1165{
1166 struct usb_serial_port *port = tty->driver_data;
1167 struct serial_struct __user *buf = (struct serial_struct __user *)arg;
1168
1169 switch (cmd) {
1170 case TIOCGSERIAL:
1171 return f81534_get_serial_info(port, buf);
1172 default:
1173 break;
1174 }
1175
1176 return -ENOIOCTLCMD;
1177}
1178
1179static void f81534_process_per_serial_block(struct usb_serial_port *port,
1180 u8 *data)
1181{
1182 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1183 int phy_num = data[0];
1184 size_t read_size = 0;
1185 size_t i;
1186 char tty_flag;
1187 int status;
1188 u8 lsr;
1189
1190 /*
1191 * The block layout is 128 Bytes
1192 * index 0: port phy idx (e.g., 0,1,2,3),
1193 * index 1: It's could be
1194 * F81534_TOKEN_RECEIVE
1195 * F81534_TOKEN_TX_EMPTY
1196 * F81534_TOKEN_MSR_CHANGE
1197 * index 2: serial in size (data+lsr, must be even)
1198 * meaningful for F81534_TOKEN_RECEIVE only
1199 * index 3: current MSR with this device
1200 * index 4~127: serial in data block (data+lsr, must be even)
1201 */
1202 switch (data[1]) {
1203 case F81534_TOKEN_TX_EMPTY:
1204 set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1205
1206 /* Try to submit writer */
1207 status = f81534_submit_writer(port, GFP_ATOMIC);
1208 if (status)
1209 dev_err(&port->dev, "%s: submit failed\n", __func__);
1210 return;
1211
1212 case F81534_TOKEN_MSR_CHANGE:
1213 f81534_msr_changed(port, data[3]);
1214 return;
1215
1216 case F81534_TOKEN_RECEIVE:
1217 read_size = data[2];
1218 if (read_size > F81534_MAX_RX_SIZE) {
1219 dev_err(&port->dev,
1220 "%s: phy: %d read_size: %zu larger than: %d\n",
1221 __func__, phy_num, read_size,
1222 F81534_MAX_RX_SIZE);
1223 return;
1224 }
1225
1226 break;
1227
1228 default:
1229 dev_warn(&port->dev, "%s: unknown token: %02x\n", __func__,
1230 data[1]);
1231 return;
1232 }
1233
1234 for (i = 4; i < 4 + read_size; i += 2) {
1235 tty_flag = TTY_NORMAL;
1236 lsr = data[i + 1];
1237
1238 if (lsr & UART_LSR_BRK_ERROR_BITS) {
1239 if (lsr & UART_LSR_BI) {
1240 tty_flag = TTY_BREAK;
1241 port->icount.brk++;
1242 usb_serial_handle_break(port);
1243 } else if (lsr & UART_LSR_PE) {
1244 tty_flag = TTY_PARITY;
1245 port->icount.parity++;
1246 } else if (lsr & UART_LSR_FE) {
1247 tty_flag = TTY_FRAME;
1248 port->icount.frame++;
1249 }
1250
1251 if (lsr & UART_LSR_OE) {
1252 port->icount.overrun++;
1253 tty_insert_flip_char(&port->port, 0,
1254 TTY_OVERRUN);
1255 }
1256
1257 schedule_work(&port_priv->lsr_work);
1258 }
1259
1260 if (port->port.console && port->sysrq) {
1261 if (usb_serial_handle_sysrq_char(port, data[i]))
1262 continue;
1263 }
1264
1265 tty_insert_flip_char(&port->port, data[i], tty_flag);
1266 }
1267
1268 tty_flip_buffer_push(&port->port);
1269}
1270
1271static void f81534_process_read_urb(struct urb *urb)
1272{
1273 struct f81534_serial_private *serial_priv;
1274 struct usb_serial_port *port;
1275 struct usb_serial *serial;
1276 u8 *buf;
1277 int phy_port_num;
1278 int tty_port_num;
1279 size_t i;
1280
1281 if (!urb->actual_length ||
1282 urb->actual_length % F81534_RECEIVE_BLOCK_SIZE) {
1283 return;
1284 }
1285
1286 port = urb->context;
1287 serial = port->serial;
1288 buf = urb->transfer_buffer;
1289 serial_priv = usb_get_serial_data(serial);
1290
1291 for (i = 0; i < urb->actual_length; i += F81534_RECEIVE_BLOCK_SIZE) {
1292 phy_port_num = buf[i];
1293 if (phy_port_num >= F81534_NUM_PORT) {
1294 dev_err(&port->dev,
1295 "%s: phy_port_num: %d larger than: %d\n",
1296 __func__, phy_port_num, F81534_NUM_PORT);
1297 continue;
1298 }
1299
1300 tty_port_num = serial_priv->tty_idx[phy_port_num];
1301 port = serial->port[tty_port_num];
1302
1303 if (tty_port_initialized(&port->port))
1304 f81534_process_per_serial_block(port, &buf[i]);
1305 }
1306}
1307
1308static void f81534_write_usb_callback(struct urb *urb)
1309{
1310 struct usb_serial_port *port = urb->context;
1311
1312 switch (urb->status) {
1313 case 0:
1314 break;
1315 case -ENOENT:
1316 case -ECONNRESET:
1317 case -ESHUTDOWN:
1318 dev_dbg(&port->dev, "%s - urb stopped: %d\n",
1319 __func__, urb->status);
1320 return;
1321 case -EPIPE:
1322 dev_err(&port->dev, "%s - urb stopped: %d\n",
1323 __func__, urb->status);
1324 return;
1325 default:
1326 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
1327 __func__, urb->status);
1328 break;
1329 }
1330}
1331
1332static void f81534_lsr_worker(struct work_struct *work)
1333{
1334 struct f81534_port_private *port_priv;
1335 struct usb_serial_port *port;
1336 int status;
1337 u8 tmp;
1338
1339 port_priv = container_of(work, struct f81534_port_private, lsr_work);
1340 port = port_priv->port;
1341
1342 status = f81534_get_port_register(port, F81534_LINE_STATUS_REG, &tmp);
1343 if (status)
1344 dev_warn(&port->dev, "read LSR failed: %d\n", status);
1345}
1346
1347static int f81534_set_port_output_pin(struct usb_serial_port *port)
1348{
1349 struct f81534_serial_private *serial_priv;
1350 struct f81534_port_private *port_priv;
1351 struct usb_serial *serial;
1352 const struct f81534_port_out_pin *pins;
1353 int status;
1354 int i;
1355 u8 value;
1356 u8 idx;
1357
1358 serial = port->serial;
1359 serial_priv = usb_get_serial_data(serial);
1360 port_priv = usb_get_serial_port_data(port);
1361
1362 idx = F81534_CONF_GPIO_OFFSET + port_priv->phy_num;
1363 value = serial_priv->conf_data[idx];
1364 pins = &f81534_port_out_pins[port_priv->phy_num];
1365
1366 for (i = 0; i < ARRAY_SIZE(pins->pin); ++i) {
1367 status = f81534_set_mask_register(serial,
1368 pins->pin[i].reg_addr, pins->pin[i].reg_mask,
1369 value & BIT(i) ? pins->pin[i].reg_mask : 0);
1370 if (status)
1371 return status;
1372 }
1373
1374 dev_dbg(&port->dev, "Output pin (M0/M1/M2): %d\n", value);
1375 return 0;
1376}
1377
1378static int f81534_port_probe(struct usb_serial_port *port)
1379{
1380 struct f81534_serial_private *serial_priv;
1381 struct f81534_port_private *port_priv;
1382 int ret;
1383 u8 value;
1384
1385 serial_priv = usb_get_serial_data(port->serial);
1386 port_priv = devm_kzalloc(&port->dev, sizeof(*port_priv), GFP_KERNEL);
1387 if (!port_priv)
1388 return -ENOMEM;
1389
1390 /*
1391 * We'll make tx frame error when baud rate from 384~500kps. So we'll
1392 * delay all tx data frame with 1bit.
1393 */
1394 port_priv->shadow_clk = F81534_UART_EN | F81534_CLK_TX_DELAY_1BIT;
1395 spin_lock_init(&port_priv->msr_lock);
1396 mutex_init(&port_priv->mcr_mutex);
1397 mutex_init(&port_priv->lcr_mutex);
1398 INIT_WORK(&port_priv->lsr_work, f81534_lsr_worker);
1399
1400 /* Assign logic-to-phy mapping */
1401 ret = f81534_logic_to_phy_port(port->serial, port);
1402 if (ret < 0)
1403 return ret;
1404
1405 port_priv->phy_num = ret;
1406 port_priv->port = port;
1407 usb_set_serial_port_data(port, port_priv);
1408 dev_dbg(&port->dev, "%s: port_number: %d, phy_num: %d\n", __func__,
1409 port->port_number, port_priv->phy_num);
1410
1411 /*
1412 * The F81532/534 will hang-up when enable LSR interrupt in IER and
1413 * occur data overrun. So we'll disable the LSR interrupt in probe()
1414 * and submit the LSR worker to clear LSR state when reported LSR error
1415 * bit with bulk-in data in f81534_process_per_serial_block().
1416 */
1417 ret = f81534_set_port_register(port, F81534_INTERRUPT_ENABLE_REG,
1418 UART_IER_RDI | UART_IER_THRI | UART_IER_MSI);
1419 if (ret)
1420 return ret;
1421
1422 value = serial_priv->conf_data[port_priv->phy_num];
1423 switch (value & F81534_PORT_CONF_MODE_MASK) {
1424 case F81534_PORT_CONF_RS485_INVERT:
1425 port_priv->shadow_clk |= F81534_CLK_RS485_MODE |
1426 F81534_CLK_RS485_INVERT;
1427 dev_dbg(&port->dev, "RS485 invert mode\n");
1428 break;
1429 case F81534_PORT_CONF_RS485:
1430 port_priv->shadow_clk |= F81534_CLK_RS485_MODE;
1431 dev_dbg(&port->dev, "RS485 mode\n");
1432 break;
1433
1434 default:
1435 case F81534_PORT_CONF_RS232:
1436 dev_dbg(&port->dev, "RS232 mode\n");
1437 break;
1438 }
1439
1440 return f81534_set_port_output_pin(port);
1441}
1442
1443static int f81534_port_remove(struct usb_serial_port *port)
1444{
1445 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1446
1447 flush_work(&port_priv->lsr_work);
1448 return 0;
1449}
1450
1451static int f81534_tiocmget(struct tty_struct *tty)
1452{
1453 struct usb_serial_port *port = tty->driver_data;
1454 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1455 int status;
1456 int r;
1457 u8 msr;
1458 u8 mcr;
1459
1460 /* Read current MSR from device */
1461 status = f81534_get_port_register(port, F81534_MODEM_STATUS_REG, &msr);
1462 if (status)
1463 return status;
1464
1465 mutex_lock(&port_priv->mcr_mutex);
1466 mcr = port_priv->shadow_mcr;
1467 mutex_unlock(&port_priv->mcr_mutex);
1468
1469 r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
1470 (mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
1471 (msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
1472 (msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
1473 (msr & UART_MSR_RI ? TIOCM_RI : 0) |
1474 (msr & UART_MSR_DSR ? TIOCM_DSR : 0);
1475
1476 return r;
1477}
1478
1479static int f81534_tiocmset(struct tty_struct *tty, unsigned int set,
1480 unsigned int clear)
1481{
1482 struct usb_serial_port *port = tty->driver_data;
1483
1484 return f81534_update_mctrl(port, set, clear);
1485}
1486
1487static void f81534_dtr_rts(struct usb_serial_port *port, int on)
1488{
1489 if (on)
1490 f81534_update_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
1491 else
1492 f81534_update_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
1493}
1494
1495static int f81534_write(struct tty_struct *tty, struct usb_serial_port *port,
1496 const u8 *buf, int count)
1497{
1498 int bytes_out, status;
1499
1500 if (!count)
1501 return 0;
1502
1503 bytes_out = kfifo_in_locked(&port->write_fifo, buf, count,
1504 &port->lock);
1505
1506 status = f81534_submit_writer(port, GFP_ATOMIC);
1507 if (status) {
1508 dev_err(&port->dev, "%s: submit failed\n", __func__);
1509 return status;
1510 }
1511
1512 return bytes_out;
1513}
1514
1515static bool f81534_tx_empty(struct usb_serial_port *port)
1516{
1517 struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
1518
1519 return test_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
1520}
1521
1522static int f81534_resume(struct usb_serial *serial)
1523{
1524 struct f81534_serial_private *serial_priv =
1525 usb_get_serial_data(serial);
1526 struct usb_serial_port *port;
1527 int error = 0;
1528 int status;
1529 size_t i;
1530
1531 /*
1532 * We'll register port 0 bulkin when port had opened, It'll take all
1533 * port received data, MSR register change and TX_EMPTY information.
1534 */
1535 mutex_lock(&serial_priv->urb_mutex);
1536
1537 if (serial_priv->opened_port) {
1538 status = f81534_submit_read_urb(serial, GFP_NOIO);
1539 if (status) {
1540 mutex_unlock(&serial_priv->urb_mutex);
1541 return status;
1542 }
1543 }
1544
1545 mutex_unlock(&serial_priv->urb_mutex);
1546
1547 for (i = 0; i < serial->num_ports; i++) {
1548 port = serial->port[i];
1549 if (!tty_port_initialized(&port->port))
1550 continue;
1551
1552 status = f81534_submit_writer(port, GFP_NOIO);
1553 if (status) {
1554 dev_err(&port->dev, "%s: submit failed\n", __func__);
1555 ++error;
1556 }
1557 }
1558
1559 if (error)
1560 return -EIO;
1561
1562 return 0;
1563}
1564
1565static struct usb_serial_driver f81534_device = {
1566 .driver = {
1567 .owner = THIS_MODULE,
1568 .name = "f81534",
1569 },
1570 .description = DRIVER_DESC,
1571 .id_table = f81534_id_table,
1572 .num_bulk_in = 1,
1573 .num_bulk_out = 1,
1574 .open = f81534_open,
1575 .close = f81534_close,
1576 .write = f81534_write,
1577 .tx_empty = f81534_tx_empty,
1578 .calc_num_ports = f81534_calc_num_ports,
1579 .port_probe = f81534_port_probe,
1580 .port_remove = f81534_port_remove,
1581 .break_ctl = f81534_break_ctl,
1582 .dtr_rts = f81534_dtr_rts,
1583 .process_read_urb = f81534_process_read_urb,
1584 .ioctl = f81534_ioctl,
1585 .tiocmget = f81534_tiocmget,
1586 .tiocmset = f81534_tiocmset,
1587 .write_bulk_callback = f81534_write_usb_callback,
1588 .set_termios = f81534_set_termios,
1589 .resume = f81534_resume,
1590};
1591
1592static struct usb_serial_driver *const serial_drivers[] = {
1593 &f81534_device, NULL
1594};
1595
1596module_usb_serial_driver(serial_drivers, f81534_id_table);
1597
1598MODULE_DEVICE_TABLE(usb, f81534_id_table);
1599MODULE_DESCRIPTION(DRIVER_DESC);
1600MODULE_AUTHOR("Peter Hong <Peter_Hong@fintek.com.tw>");
1601MODULE_AUTHOR("Tom Tsai <Tom_Tsai@fintek.com.tw>");
1602MODULE_LICENSE("GPL");