Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Fintek F81232 USB to serial adaptor driver
   4 * Fintek F81532A/534A/535/536 USB to 2/4/8/12 serial adaptor driver
   5 *
   6 * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
   7 * Copyright (C) 2012 Linux Foundation
   8 */
   9
  10#include <linux/kernel.h>
  11#include <linux/errno.h>
  12#include <linux/slab.h>
  13#include <linux/tty.h>
  14#include <linux/tty_driver.h>
  15#include <linux/tty_flip.h>
  16#include <linux/serial.h>
  17#include <linux/module.h>
  18#include <linux/moduleparam.h>
  19#include <linux/mutex.h>
  20#include <linux/uaccess.h>
  21#include <linux/usb.h>
  22#include <linux/usb/serial.h>
  23#include <linux/serial_reg.h>
  24
  25#define F81232_ID		\
  26	{ USB_DEVICE(0x1934, 0x0706) }	/* 1 port UART device */
  27
  28#define F81534A_SERIES_ID	\
  29	{ USB_DEVICE(0x2c42, 0x1602) },	/* In-Box 2 port UART device */	\
  30	{ USB_DEVICE(0x2c42, 0x1604) },	/* In-Box 4 port UART device */	\
  31	{ USB_DEVICE(0x2c42, 0x1605) },	/* In-Box 8 port UART device */	\
  32	{ USB_DEVICE(0x2c42, 0x1606) },	/* In-Box 12 port UART device */ \
  33	{ USB_DEVICE(0x2c42, 0x1608) },	/* Non-Flash type */ \
  34	{ USB_DEVICE(0x2c42, 0x1632) },	/* 2 port UART device */ \
  35	{ USB_DEVICE(0x2c42, 0x1634) },	/* 4 port UART device */ \
  36	{ USB_DEVICE(0x2c42, 0x1635) },	/* 8 port UART device */ \
  37	{ USB_DEVICE(0x2c42, 0x1636) }	/* 12 port UART device */
  38
  39#define F81534A_CTRL_ID		\
  40	{ USB_DEVICE(0x2c42, 0x16f8) }	/* Global control device */
  41
  42static const struct usb_device_id f81232_id_table[] = {
  43	F81232_ID,
  44	{ }					/* Terminating entry */
  45};
  46
  47static const struct usb_device_id f81534a_id_table[] = {
  48	F81534A_SERIES_ID,
  49	{ }					/* Terminating entry */
  50};
  51
  52static const struct usb_device_id f81534a_ctrl_id_table[] = {
  53	F81534A_CTRL_ID,
  54	{ }					/* Terminating entry */
  55};
  56
  57static const struct usb_device_id combined_id_table[] = {
  58	F81232_ID,
  59	F81534A_SERIES_ID,
  60	F81534A_CTRL_ID,
  61	{ }					/* Terminating entry */
  62};
  63MODULE_DEVICE_TABLE(usb, combined_id_table);
  64
  65/* Maximum baudrate for F81232 */
  66#define F81232_MAX_BAUDRATE		1500000
  67#define F81232_DEF_BAUDRATE		9600
  68
  69/* USB Control EP parameter */
  70#define F81232_REGISTER_REQUEST		0xa0
  71#define F81232_GET_REGISTER		0xc0
  72#define F81232_SET_REGISTER		0x40
  73#define F81534A_ACCESS_REG_RETRY	2
  74
  75#define SERIAL_BASE_ADDRESS		0x0120
  76#define RECEIVE_BUFFER_REGISTER		(0x00 + SERIAL_BASE_ADDRESS)
  77#define INTERRUPT_ENABLE_REGISTER	(0x01 + SERIAL_BASE_ADDRESS)
  78#define FIFO_CONTROL_REGISTER		(0x02 + SERIAL_BASE_ADDRESS)
  79#define LINE_CONTROL_REGISTER		(0x03 + SERIAL_BASE_ADDRESS)
  80#define MODEM_CONTROL_REGISTER		(0x04 + SERIAL_BASE_ADDRESS)
  81#define LINE_STATUS_REGISTER		(0x05 + SERIAL_BASE_ADDRESS)
  82#define MODEM_STATUS_REGISTER		(0x06 + SERIAL_BASE_ADDRESS)
  83
  84/*
  85 * F81232 Clock registers (106h)
  86 *
  87 * Bit1-0:	Clock source selector
  88 *			00: 1.846MHz.
  89 *			01: 18.46MHz.
  90 *			10: 24MHz.
  91 *			11: 14.77MHz.
  92 */
  93#define F81232_CLK_REGISTER		0x106
  94#define F81232_CLK_1_846_MHZ		0
  95#define F81232_CLK_18_46_MHZ		BIT(0)
  96#define F81232_CLK_24_MHZ		BIT(1)
  97#define F81232_CLK_14_77_MHZ		(BIT(1) | BIT(0))
  98#define F81232_CLK_MASK			GENMASK(1, 0)
  99
 100#define F81534A_MODE_REG		0x107
 101#define F81534A_TRIGGER_MASK		GENMASK(3, 2)
 102#define F81534A_TRIGGER_MULTIPLE_4X	BIT(3)
 103#define F81534A_FIFO_128BYTE		(BIT(1) | BIT(0))
 104
 105/* Serial port self GPIO control, 2bytes [control&output data][input data] */
 106#define F81534A_GPIO_REG		0x10e
 107#define F81534A_GPIO_MODE2_DIR		BIT(6) /* 1: input, 0: output */
 108#define F81534A_GPIO_MODE1_DIR		BIT(5)
 109#define F81534A_GPIO_MODE0_DIR		BIT(4)
 110#define F81534A_GPIO_MODE2_OUTPUT	BIT(2)
 111#define F81534A_GPIO_MODE1_OUTPUT	BIT(1)
 112#define F81534A_GPIO_MODE0_OUTPUT	BIT(0)
 113
 114#define F81534A_CTRL_CMD_ENABLE_PORT	0x116
 115
 116struct f81232_private {
 117	struct mutex lock;
 118	u8 modem_control;
 119	u8 modem_status;
 120	u8 shadow_lcr;
 121	speed_t baud_base;
 122	struct work_struct lsr_work;
 123	struct work_struct interrupt_work;
 124	struct usb_serial_port *port;
 125};
 126
 127static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
 128static u8 const clock_table[] = { F81232_CLK_1_846_MHZ, F81232_CLK_14_77_MHZ,
 129				F81232_CLK_18_46_MHZ, F81232_CLK_24_MHZ };
 130
 131static int calc_baud_divisor(speed_t baudrate, speed_t clockrate)
 132{
 
 
 
 133	return DIV_ROUND_CLOSEST(clockrate, baudrate);
 134}
 135
 136static int f81232_get_register(struct usb_serial_port *port, u16 reg, u8 *val)
 137{
 138	int status;
 
 139	struct usb_device *dev = port->serial->dev;
 140
 141	status = usb_control_msg_recv(dev,
 142				      0,
 143				      F81232_REGISTER_REQUEST,
 144				      F81232_GET_REGISTER,
 145				      reg,
 146				      0,
 147				      val,
 148				      sizeof(*val),
 149				      USB_CTRL_GET_TIMEOUT,
 150				      GFP_KERNEL);
 151	if (status) {
 
 
 
 152		dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
 153		status = usb_translate_errors(status);
 
 
 
 
 
 
 
 154	}
 155
 
 156	return status;
 157}
 158
 159static int f81232_set_register(struct usb_serial_port *port, u16 reg, u8 val)
 160{
 161	int status;
 
 162	struct usb_device *dev = port->serial->dev;
 163
 164	status = usb_control_msg_send(dev,
 165				      0,
 166				      F81232_REGISTER_REQUEST,
 167				      F81232_SET_REGISTER,
 168				      reg,
 169				      0,
 170				      &val,
 171				      sizeof(val),
 172				      USB_CTRL_SET_TIMEOUT,
 173				      GFP_KERNEL);
 174	if (status) {
 
 
 
 
 
 175		dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
 176		status = usb_translate_errors(status);
 
 
 
 
 
 
 177	}
 178
 
 179	return status;
 180}
 181
 182static int f81232_set_mask_register(struct usb_serial_port *port, u16 reg,
 183					u8 mask, u8 val)
 184{
 185	int status;
 186	u8 tmp;
 187
 188	status = f81232_get_register(port, reg, &tmp);
 189	if (status)
 190		return status;
 191
 192	tmp = (tmp & ~mask) | (val & mask);
 193
 194	return f81232_set_register(port, reg, tmp);
 195}
 196
 197static void f81232_read_msr(struct usb_serial_port *port)
 198{
 199	int status;
 200	u8 current_msr;
 201	struct tty_struct *tty;
 202	struct f81232_private *priv = usb_get_serial_port_data(port);
 203
 204	mutex_lock(&priv->lock);
 205	status = f81232_get_register(port, MODEM_STATUS_REGISTER,
 206			&current_msr);
 207	if (status) {
 208		dev_err(&port->dev, "%s fail, status: %d\n", __func__, status);
 209		mutex_unlock(&priv->lock);
 210		return;
 211	}
 212
 213	if (!(current_msr & UART_MSR_ANY_DELTA)) {
 214		mutex_unlock(&priv->lock);
 215		return;
 216	}
 217
 218	priv->modem_status = current_msr;
 219
 220	if (current_msr & UART_MSR_DCTS)
 221		port->icount.cts++;
 222	if (current_msr & UART_MSR_DDSR)
 223		port->icount.dsr++;
 224	if (current_msr & UART_MSR_TERI)
 225		port->icount.rng++;
 226	if (current_msr & UART_MSR_DDCD) {
 227		port->icount.dcd++;
 228		tty = tty_port_tty_get(&port->port);
 229		if (tty) {
 230			usb_serial_handle_dcd_change(port, tty,
 231					current_msr & UART_MSR_DCD);
 232
 233			tty_kref_put(tty);
 234		}
 235	}
 236
 237	wake_up_interruptible(&port->port.delta_msr_wait);
 238	mutex_unlock(&priv->lock);
 239}
 240
 241static int f81232_set_mctrl(struct usb_serial_port *port,
 242					   unsigned int set, unsigned int clear)
 243{
 244	u8 val;
 245	int status;
 246	struct f81232_private *priv = usb_get_serial_port_data(port);
 247
 248	if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
 249		return 0;	/* no change */
 250
 251	/* 'set' takes precedence over 'clear' */
 252	clear &= ~set;
 253
 254	/* force enable interrupt with OUT2 */
 255	mutex_lock(&priv->lock);
 256	val = UART_MCR_OUT2 | priv->modem_control;
 257
 258	if (clear & TIOCM_DTR)
 259		val &= ~UART_MCR_DTR;
 260
 261	if (clear & TIOCM_RTS)
 262		val &= ~UART_MCR_RTS;
 263
 264	if (set & TIOCM_DTR)
 265		val |= UART_MCR_DTR;
 266
 267	if (set & TIOCM_RTS)
 268		val |= UART_MCR_RTS;
 269
 270	dev_dbg(&port->dev, "%s new:%02x old:%02x\n", __func__,
 271			val, priv->modem_control);
 272
 273	status = f81232_set_register(port, MODEM_CONTROL_REGISTER, val);
 274	if (status) {
 275		dev_err(&port->dev, "%s set MCR status < 0\n", __func__);
 276		mutex_unlock(&priv->lock);
 277		return status;
 278	}
 279
 280	priv->modem_control = val;
 281	mutex_unlock(&priv->lock);
 282
 283	return 0;
 284}
 285
 286static void f81232_update_line_status(struct usb_serial_port *port,
 287				      unsigned char *data,
 288				      size_t actual_length)
 289{
 290	struct f81232_private *priv = usb_get_serial_port_data(port);
 291
 292	if (!actual_length)
 293		return;
 294
 295	switch (data[0] & 0x07) {
 296	case 0x00: /* msr change */
 297		dev_dbg(&port->dev, "IIR: MSR Change: %02x\n", data[0]);
 298		schedule_work(&priv->interrupt_work);
 299		break;
 300	case 0x02: /* tx-empty */
 301		break;
 302	case 0x04: /* rx data available */
 303		break;
 304	case 0x06: /* lsr change */
 305		/* we can forget it. the LSR will read from bulk-in */
 306		dev_dbg(&port->dev, "IIR: LSR Change: %02x\n", data[0]);
 307		break;
 308	}
 309}
 310
 311static void f81232_read_int_callback(struct urb *urb)
 312{
 313	struct usb_serial_port *port =  urb->context;
 314	unsigned char *data = urb->transfer_buffer;
 315	unsigned int actual_length = urb->actual_length;
 316	int status = urb->status;
 317	int retval;
 318
 319	switch (status) {
 320	case 0:
 321		/* success */
 322		break;
 323	case -ECONNRESET:
 324	case -ENOENT:
 325	case -ESHUTDOWN:
 326		/* this urb is terminated, clean up */
 327		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
 328			__func__, status);
 329		return;
 330	default:
 331		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
 332			__func__, status);
 333		goto exit;
 334	}
 335
 336	usb_serial_debug_data(&port->dev, __func__,
 337			      urb->actual_length, urb->transfer_buffer);
 338
 339	f81232_update_line_status(port, data, actual_length);
 340
 341exit:
 342	retval = usb_submit_urb(urb, GFP_ATOMIC);
 343	if (retval)
 344		dev_err(&urb->dev->dev,
 345			"%s - usb_submit_urb failed with result %d\n",
 346			__func__, retval);
 347}
 348
 349static char f81232_handle_lsr(struct usb_serial_port *port, u8 lsr)
 350{
 351	struct f81232_private *priv = usb_get_serial_port_data(port);
 352	char tty_flag = TTY_NORMAL;
 353
 354	if (!(lsr & UART_LSR_BRK_ERROR_BITS))
 355		return tty_flag;
 356
 357	if (lsr & UART_LSR_BI) {
 358		tty_flag = TTY_BREAK;
 359		port->icount.brk++;
 360		usb_serial_handle_break(port);
 361	} else if (lsr & UART_LSR_PE) {
 362		tty_flag = TTY_PARITY;
 363		port->icount.parity++;
 364	} else if (lsr & UART_LSR_FE) {
 365		tty_flag = TTY_FRAME;
 366		port->icount.frame++;
 367	}
 368
 369	if (lsr & UART_LSR_OE) {
 370		port->icount.overrun++;
 371		schedule_work(&priv->lsr_work);
 372		tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
 373	}
 374
 375	return tty_flag;
 376}
 377
 378static void f81232_process_read_urb(struct urb *urb)
 379{
 380	struct usb_serial_port *port = urb->context;
 
 381	unsigned char *data = urb->transfer_buffer;
 382	char tty_flag;
 383	unsigned int i;
 384	u8 lsr;
 385
 386	/*
 387	 * When opening the port we get a 1-byte packet with the current LSR,
 388	 * which we discard.
 389	 */
 390	if ((urb->actual_length < 2) || (urb->actual_length % 2))
 391		return;
 392
 393	/* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
 394
 395	for (i = 0; i < urb->actual_length; i += 2) {
 
 396		lsr = data[i];
 397		tty_flag = f81232_handle_lsr(port, lsr);
 398
 399		if (port->sysrq) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 400			if (usb_serial_handle_sysrq_char(port, data[i + 1]))
 401				continue;
 402		}
 403
 404		tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
 405	}
 406
 407	tty_flip_buffer_push(&port->port);
 408}
 409
 410static void f81534a_process_read_urb(struct urb *urb)
 411{
 412	struct usb_serial_port *port = urb->context;
 413	unsigned char *data = urb->transfer_buffer;
 414	char tty_flag;
 415	unsigned int i;
 416	u8 lsr;
 417	u8 len;
 418
 419	if (urb->actual_length < 3) {
 420		dev_err(&port->dev, "short message received: %d\n",
 421				urb->actual_length);
 422		return;
 423	}
 424
 425	len = data[0];
 426	if (len != urb->actual_length) {
 427		dev_err(&port->dev, "malformed message received: %d (%d)\n",
 428				urb->actual_length, len);
 429		return;
 430	}
 431
 432	/* bulk-in data: [LEN][Data.....][LSR] */
 433	lsr = data[len - 1];
 434	tty_flag = f81232_handle_lsr(port, lsr);
 435
 436	if (port->sysrq) {
 437		for (i = 1; i < len - 1; ++i) {
 438			if (!usb_serial_handle_sysrq_char(port, data[i])) {
 439				tty_insert_flip_char(&port->port, data[i],
 440						tty_flag);
 441			}
 442		}
 443	} else {
 444		tty_insert_flip_string_fixed_flag(&port->port, &data[1],
 445							tty_flag, len - 2);
 446	}
 447
 448	tty_flip_buffer_push(&port->port);
 449}
 450
 451static void f81232_break_ctl(struct tty_struct *tty, int break_state)
 452{
 453	struct usb_serial_port *port = tty->driver_data;
 454	struct f81232_private *priv = usb_get_serial_port_data(port);
 455	int status;
 456
 457	mutex_lock(&priv->lock);
 458
 459	if (break_state)
 460		priv->shadow_lcr |= UART_LCR_SBC;
 461	else
 462		priv->shadow_lcr &= ~UART_LCR_SBC;
 463
 464	status = f81232_set_register(port, LINE_CONTROL_REGISTER,
 465					priv->shadow_lcr);
 466	if (status)
 467		dev_err(&port->dev, "set break failed: %d\n", status);
 468
 469	mutex_unlock(&priv->lock);
 470}
 471
 472static int f81232_find_clk(speed_t baudrate)
 473{
 474	int idx;
 475
 476	for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
 477		if (baudrate <= baudrate_table[idx] &&
 478				baudrate_table[idx] % baudrate == 0)
 479			return idx;
 480	}
 481
 482	return -EINVAL;
 483}
 484
 485static void f81232_set_baudrate(struct tty_struct *tty,
 486				struct usb_serial_port *port, speed_t baudrate,
 487				speed_t old_baudrate)
 488{
 489	struct f81232_private *priv = usb_get_serial_port_data(port);
 490	u8 lcr;
 491	int divisor;
 492	int status = 0;
 493	int i;
 494	int idx;
 495	speed_t baud_list[] = { baudrate, old_baudrate, F81232_DEF_BAUDRATE };
 496
 497	for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
 498		baudrate = baud_list[i];
 499		if (baudrate == 0) {
 500			tty_encode_baud_rate(tty, 0, 0);
 501			return;
 502		}
 503
 504		idx = f81232_find_clk(baudrate);
 505		if (idx >= 0) {
 
 506			tty_encode_baud_rate(tty, baudrate, baudrate);
 507			break;
 508		}
 509	}
 510
 511	if (idx < 0)
 512		return;
 513
 514	priv->baud_base = baudrate_table[idx];
 515	divisor = calc_baud_divisor(baudrate, priv->baud_base);
 516
 517	status = f81232_set_mask_register(port, F81232_CLK_REGISTER,
 518			F81232_CLK_MASK, clock_table[idx]);
 519	if (status) {
 520		dev_err(&port->dev, "%s failed to set CLK_REG: %d\n",
 521			__func__, status);
 522		return;
 523	}
 524
 525	status = f81232_get_register(port, LINE_CONTROL_REGISTER,
 526			 &lcr); /* get LCR */
 527	if (status) {
 528		dev_err(&port->dev, "%s failed to get LCR: %d\n",
 529			__func__, status);
 530		return;
 531	}
 532
 533	status = f81232_set_register(port, LINE_CONTROL_REGISTER,
 534			 lcr | UART_LCR_DLAB); /* Enable DLAB */
 535	if (status) {
 536		dev_err(&port->dev, "%s failed to set DLAB: %d\n",
 537			__func__, status);
 538		return;
 539	}
 540
 541	status = f81232_set_register(port, RECEIVE_BUFFER_REGISTER,
 542			 divisor & 0x00ff); /* low */
 543	if (status) {
 544		dev_err(&port->dev, "%s failed to set baudrate MSB: %d\n",
 545			__func__, status);
 546		goto reapply_lcr;
 547	}
 548
 549	status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
 550			 (divisor & 0xff00) >> 8); /* high */
 551	if (status) {
 552		dev_err(&port->dev, "%s failed to set baudrate LSB: %d\n",
 553			__func__, status);
 554	}
 555
 556reapply_lcr:
 557	status = f81232_set_register(port, LINE_CONTROL_REGISTER,
 558			lcr & ~UART_LCR_DLAB);
 559	if (status) {
 560		dev_err(&port->dev, "%s failed to set DLAB: %d\n",
 561			__func__, status);
 562	}
 563}
 564
 565static int f81232_port_enable(struct usb_serial_port *port)
 566{
 567	u8 val;
 568	int status;
 569
 570	/* fifo on, trigger8, clear TX/RX*/
 571	val = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
 572			UART_FCR_CLEAR_XMIT;
 573
 574	status = f81232_set_register(port, FIFO_CONTROL_REGISTER, val);
 575	if (status) {
 576		dev_err(&port->dev, "%s failed to set FCR: %d\n",
 577			__func__, status);
 578		return status;
 579	}
 580
 581	/* MSR Interrupt only, LSR will read from Bulk-in odd byte */
 582	status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
 583			UART_IER_MSI);
 584	if (status) {
 585		dev_err(&port->dev, "%s failed to set IER: %d\n",
 586			__func__, status);
 587		return status;
 588	}
 589
 590	return 0;
 591}
 592
 593static int f81232_port_disable(struct usb_serial_port *port)
 594{
 595	int status;
 596
 597	status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER, 0);
 598	if (status) {
 599		dev_err(&port->dev, "%s failed to set IER: %d\n",
 600			__func__, status);
 601		return status;
 602	}
 603
 604	return 0;
 605}
 606
 607static void f81232_set_termios(struct tty_struct *tty,
 608			       struct usb_serial_port *port,
 609			       const struct ktermios *old_termios)
 610{
 611	struct f81232_private *priv = usb_get_serial_port_data(port);
 612	u8 new_lcr = 0;
 613	int status = 0;
 614	speed_t baudrate;
 615	speed_t old_baud;
 616
 617	/* Don't change anything if nothing has changed */
 618	if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
 619		return;
 620
 621	if (C_BAUD(tty) == B0)
 622		f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
 623	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
 624		f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
 625
 626	baudrate = tty_get_baud_rate(tty);
 627	if (baudrate > 0) {
 628		if (old_termios)
 629			old_baud = tty_termios_baud_rate(old_termios);
 630		else
 631			old_baud = F81232_DEF_BAUDRATE;
 632
 633		f81232_set_baudrate(tty, port, baudrate, old_baud);
 634	}
 635
 636	if (C_PARENB(tty)) {
 637		new_lcr |= UART_LCR_PARITY;
 638
 639		if (!C_PARODD(tty))
 640			new_lcr |= UART_LCR_EPAR;
 641
 642		if (C_CMSPAR(tty))
 643			new_lcr |= UART_LCR_SPAR;
 644	}
 645
 646	if (C_CSTOPB(tty))
 647		new_lcr |= UART_LCR_STOP;
 648
 649	new_lcr |= UART_LCR_WLEN(tty_get_char_size(tty->termios.c_cflag));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 650
 651	mutex_lock(&priv->lock);
 652
 653	new_lcr |= (priv->shadow_lcr & UART_LCR_SBC);
 654	status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
 655	if (status) {
 656		dev_err(&port->dev, "%s failed to set LCR: %d\n",
 657			__func__, status);
 658	}
 659
 660	priv->shadow_lcr = new_lcr;
 661
 662	mutex_unlock(&priv->lock);
 663}
 664
 665static int f81232_tiocmget(struct tty_struct *tty)
 666{
 667	int r;
 668	struct usb_serial_port *port = tty->driver_data;
 669	struct f81232_private *port_priv = usb_get_serial_port_data(port);
 670	u8 mcr, msr;
 671
 672	/* force get current MSR changed state */
 673	f81232_read_msr(port);
 674
 675	mutex_lock(&port_priv->lock);
 676	mcr = port_priv->modem_control;
 677	msr = port_priv->modem_status;
 678	mutex_unlock(&port_priv->lock);
 679
 680	r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
 681		(mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
 682		(msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
 683		(msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
 684		(msr & UART_MSR_RI ? TIOCM_RI : 0) |
 685		(msr & UART_MSR_DSR ? TIOCM_DSR : 0);
 686
 687	return r;
 688}
 689
 690static int f81232_tiocmset(struct tty_struct *tty,
 691			unsigned int set, unsigned int clear)
 692{
 693	struct usb_serial_port *port = tty->driver_data;
 694
 695	return f81232_set_mctrl(port, set, clear);
 696}
 697
 698static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
 699{
 700	int result;
 701
 702	result = f81232_port_enable(port);
 703	if (result)
 704		return result;
 705
 706	/* Setup termios */
 707	if (tty)
 708		f81232_set_termios(tty, port, NULL);
 709
 710	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
 711	if (result) {
 712		dev_err(&port->dev, "%s - failed submitting interrupt urb,"
 713			" error %d\n", __func__, result);
 714		return result;
 715	}
 716
 717	result = usb_serial_generic_open(tty, port);
 718	if (result) {
 719		usb_kill_urb(port->interrupt_in_urb);
 720		return result;
 721	}
 722
 723	return 0;
 724}
 725
 726static int f81534a_open(struct tty_struct *tty, struct usb_serial_port *port)
 727{
 728	int status;
 729	u8 mask;
 730	u8 val;
 731
 732	val = F81534A_TRIGGER_MULTIPLE_4X | F81534A_FIFO_128BYTE;
 733	mask = F81534A_TRIGGER_MASK | F81534A_FIFO_128BYTE;
 734
 735	status = f81232_set_mask_register(port, F81534A_MODE_REG, mask, val);
 736	if (status) {
 737		dev_err(&port->dev, "failed to set MODE_REG: %d\n", status);
 738		return status;
 739	}
 740
 741	return f81232_open(tty, port);
 742}
 743
 744static void f81232_close(struct usb_serial_port *port)
 745{
 746	struct f81232_private *port_priv = usb_get_serial_port_data(port);
 747
 748	f81232_port_disable(port);
 749	usb_serial_generic_close(port);
 750	usb_kill_urb(port->interrupt_in_urb);
 751	flush_work(&port_priv->interrupt_work);
 752	flush_work(&port_priv->lsr_work);
 753}
 754
 755static void f81232_dtr_rts(struct usb_serial_port *port, int on)
 756{
 757	if (on)
 758		f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
 759	else
 760		f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
 761}
 762
 763static bool f81232_tx_empty(struct usb_serial_port *port)
 764{
 765	int status;
 766	u8 tmp;
 767
 768	status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
 769	if (!status) {
 770		if ((tmp & UART_LSR_TEMT) != UART_LSR_TEMT)
 771			return false;
 772	}
 773
 774	return true;
 775}
 776
 777static int f81232_carrier_raised(struct usb_serial_port *port)
 778{
 779	u8 msr;
 780	struct f81232_private *priv = usb_get_serial_port_data(port);
 781
 782	mutex_lock(&priv->lock);
 783	msr = priv->modem_status;
 784	mutex_unlock(&priv->lock);
 785
 786	if (msr & UART_MSR_DCD)
 787		return 1;
 788	return 0;
 789}
 790
 791static void f81232_get_serial(struct tty_struct *tty, struct serial_struct *ss)
 
 792{
 793	struct usb_serial_port *port = tty->driver_data;
 794	struct f81232_private *priv = usb_get_serial_port_data(port);
 795
 
 
 
 796	ss->baud_base = priv->baud_base;
 
 797}
 798
 799static void  f81232_interrupt_work(struct work_struct *work)
 800{
 801	struct f81232_private *priv =
 802		container_of(work, struct f81232_private, interrupt_work);
 803
 804	f81232_read_msr(priv->port);
 805}
 806
 807static void f81232_lsr_worker(struct work_struct *work)
 808{
 809	struct f81232_private *priv;
 810	struct usb_serial_port *port;
 811	int status;
 812	u8 tmp;
 813
 814	priv = container_of(work, struct f81232_private, lsr_work);
 815	port = priv->port;
 816
 817	status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
 818	if (status)
 819		dev_warn(&port->dev, "read LSR failed: %d\n", status);
 820}
 821
 822static int f81534a_ctrl_set_register(struct usb_interface *intf, u16 reg,
 823					u16 size, void *val)
 824{
 825	struct usb_device *dev = interface_to_usbdev(intf);
 826	int retry = F81534A_ACCESS_REG_RETRY;
 827	int status;
 828
 829	while (retry--) {
 830		status = usb_control_msg_send(dev,
 831					      0,
 832					      F81232_REGISTER_REQUEST,
 833					      F81232_SET_REGISTER,
 834					      reg,
 835					      0,
 836					      val,
 837					      size,
 838					      USB_CTRL_SET_TIMEOUT,
 839					      GFP_KERNEL);
 840		if (status) {
 841			status = usb_translate_errors(status);
 842			if (status == -EIO)
 843				continue;
 844		}
 845
 846		break;
 847	}
 848
 849	if (status) {
 850		dev_err(&intf->dev, "failed to set register 0x%x: %d\n",
 851				reg, status);
 852	}
 853
 854	return status;
 855}
 856
 857static int f81534a_ctrl_enable_all_ports(struct usb_interface *intf, bool en)
 858{
 859	unsigned char enable[2] = {0};
 860	int status;
 861
 862	/*
 863	 * Enable all available serial ports, define as following:
 864	 * bit 15	: Reset behavior (when HUB got soft reset)
 865	 *			0: maintain all serial port enabled state.
 866	 *			1: disable all serial port.
 867	 * bit 0~11	: Serial port enable bit.
 868	 */
 869	if (en) {
 870		enable[0] = 0xff;
 871		enable[1] = 0x8f;
 872	}
 873
 874	status = f81534a_ctrl_set_register(intf, F81534A_CTRL_CMD_ENABLE_PORT,
 875			sizeof(enable), enable);
 876	if (status)
 877		dev_err(&intf->dev, "failed to enable ports: %d\n", status);
 878
 879	return status;
 880}
 881
 882static int f81534a_ctrl_probe(struct usb_interface *intf,
 883				const struct usb_device_id *id)
 884{
 885	return f81534a_ctrl_enable_all_ports(intf, true);
 886}
 887
 888static void f81534a_ctrl_disconnect(struct usb_interface *intf)
 889{
 890	f81534a_ctrl_enable_all_ports(intf, false);
 891}
 892
 893static int f81534a_ctrl_resume(struct usb_interface *intf)
 894{
 895	return f81534a_ctrl_enable_all_ports(intf, true);
 896}
 897
 898static int f81232_port_probe(struct usb_serial_port *port)
 899{
 900	struct f81232_private *priv;
 901
 902	priv = devm_kzalloc(&port->dev, sizeof(*priv), GFP_KERNEL);
 903	if (!priv)
 904		return -ENOMEM;
 905
 906	mutex_init(&priv->lock);
 907	INIT_WORK(&priv->interrupt_work,  f81232_interrupt_work);
 908	INIT_WORK(&priv->lsr_work, f81232_lsr_worker);
 909
 910	usb_set_serial_port_data(port, priv);
 911
 
 912	priv->port = port;
 913
 914	return 0;
 915}
 916
 917static int f81534a_port_probe(struct usb_serial_port *port)
 918{
 919	int status;
 920
 921	/* tri-state with pull-high, default RS232 Mode */
 922	status = f81232_set_register(port, F81534A_GPIO_REG,
 923					F81534A_GPIO_MODE2_DIR);
 924	if (status)
 925		return status;
 926
 927	return f81232_port_probe(port);
 928}
 929
 930static int f81232_suspend(struct usb_serial *serial, pm_message_t message)
 931{
 932	struct usb_serial_port *port = serial->port[0];
 933	struct f81232_private *port_priv = usb_get_serial_port_data(port);
 934	int i;
 935
 936	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
 937		usb_kill_urb(port->read_urbs[i]);
 938
 939	usb_kill_urb(port->interrupt_in_urb);
 940
 941	if (port_priv) {
 942		flush_work(&port_priv->interrupt_work);
 943		flush_work(&port_priv->lsr_work);
 944	}
 945
 946	return 0;
 947}
 948
 949static int f81232_resume(struct usb_serial *serial)
 950{
 951	struct usb_serial_port *port = serial->port[0];
 952	int result;
 953
 954	if (tty_port_initialized(&port->port)) {
 955		result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
 956		if (result) {
 957			dev_err(&port->dev, "submit interrupt urb failed: %d\n",
 958					result);
 959			return result;
 960		}
 961	}
 962
 963	return usb_serial_generic_resume(serial);
 964}
 965
 966static struct usb_serial_driver f81232_device = {
 967	.driver = {
 968		.owner =	THIS_MODULE,
 969		.name =		"f81232",
 970	},
 971	.id_table =		f81232_id_table,
 972	.num_ports =		1,
 973	.bulk_in_size =		256,
 974	.bulk_out_size =	256,
 975	.open =			f81232_open,
 976	.close =		f81232_close,
 977	.dtr_rts =		f81232_dtr_rts,
 978	.carrier_raised =	f81232_carrier_raised,
 979	.get_serial =		f81232_get_serial,
 980	.break_ctl =		f81232_break_ctl,
 981	.set_termios =		f81232_set_termios,
 982	.tiocmget =		f81232_tiocmget,
 983	.tiocmset =		f81232_tiocmset,
 984	.tiocmiwait =		usb_serial_generic_tiocmiwait,
 985	.tx_empty =		f81232_tx_empty,
 986	.process_read_urb =	f81232_process_read_urb,
 987	.read_int_callback =	f81232_read_int_callback,
 988	.port_probe =		f81232_port_probe,
 989	.suspend =		f81232_suspend,
 990	.resume =		f81232_resume,
 991};
 992
 993static struct usb_serial_driver f81534a_device = {
 994	.driver = {
 995		.owner =	THIS_MODULE,
 996		.name =		"f81534a",
 997	},
 998	.id_table =		f81534a_id_table,
 999	.num_ports =		1,
1000	.open =			f81534a_open,
1001	.close =		f81232_close,
1002	.dtr_rts =		f81232_dtr_rts,
1003	.carrier_raised =	f81232_carrier_raised,
1004	.get_serial =		f81232_get_serial,
1005	.break_ctl =		f81232_break_ctl,
1006	.set_termios =		f81232_set_termios,
1007	.tiocmget =		f81232_tiocmget,
1008	.tiocmset =		f81232_tiocmset,
1009	.tiocmiwait =		usb_serial_generic_tiocmiwait,
1010	.tx_empty =		f81232_tx_empty,
1011	.process_read_urb =	f81534a_process_read_urb,
1012	.read_int_callback =	f81232_read_int_callback,
1013	.port_probe =		f81534a_port_probe,
1014	.suspend =		f81232_suspend,
1015	.resume =		f81232_resume,
1016};
1017
1018static struct usb_serial_driver * const serial_drivers[] = {
1019	&f81232_device,
1020	&f81534a_device,
1021	NULL,
1022};
1023
1024static struct usb_driver f81534a_ctrl_driver = {
1025	.name =		"f81534a_ctrl",
1026	.id_table =	f81534a_ctrl_id_table,
1027	.probe =	f81534a_ctrl_probe,
1028	.disconnect =	f81534a_ctrl_disconnect,
1029	.resume =	f81534a_ctrl_resume,
1030};
1031
1032static int __init f81232_init(void)
1033{
1034	int status;
1035
1036	status = usb_register_driver(&f81534a_ctrl_driver, THIS_MODULE,
1037			KBUILD_MODNAME);
1038	if (status)
1039		return status;
1040
1041	status = usb_serial_register_drivers(serial_drivers, KBUILD_MODNAME,
1042			combined_id_table);
1043	if (status) {
1044		usb_deregister(&f81534a_ctrl_driver);
1045		return status;
1046	}
1047
1048	return 0;
1049}
1050
1051static void __exit f81232_exit(void)
1052{
1053	usb_serial_deregister_drivers(serial_drivers);
1054	usb_deregister(&f81534a_ctrl_driver);
1055}
1056
1057module_init(f81232_init);
1058module_exit(f81232_exit);
1059
1060MODULE_DESCRIPTION("Fintek F81232/532A/534A/535/536 USB to serial driver");
1061MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
1062MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
1063MODULE_LICENSE("GPL v2");
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  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
  7 */
  8
  9#include <linux/kernel.h>
 10#include <linux/errno.h>
 11#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>
 18#include <linux/mutex.h>
 19#include <linux/uaccess.h>
 20#include <linux/usb.h>
 21#include <linux/usb/serial.h>
 22#include <linux/serial_reg.h>
 23
 24static const struct usb_device_id id_table[] = {
 25	{ USB_DEVICE(0x1934, 0x0706) },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 26	{ }					/* Terminating entry */
 27};
 28MODULE_DEVICE_TABLE(usb, id_table);
 29
 30/* Maximum baudrate for F81232 */
 31#define F81232_MAX_BAUDRATE		1500000
 32#define F81232_DEF_BAUDRATE		9600
 33
 34/* USB Control EP parameter */
 35#define F81232_REGISTER_REQUEST		0xa0
 36#define F81232_GET_REGISTER		0xc0
 37#define F81232_SET_REGISTER		0x40
 
 38
 39#define SERIAL_BASE_ADDRESS		0x0120
 40#define RECEIVE_BUFFER_REGISTER		(0x00 + SERIAL_BASE_ADDRESS)
 41#define INTERRUPT_ENABLE_REGISTER	(0x01 + SERIAL_BASE_ADDRESS)
 42#define FIFO_CONTROL_REGISTER		(0x02 + SERIAL_BASE_ADDRESS)
 43#define LINE_CONTROL_REGISTER		(0x03 + SERIAL_BASE_ADDRESS)
 44#define MODEM_CONTROL_REGISTER		(0x04 + SERIAL_BASE_ADDRESS)
 45#define LINE_STATUS_REGISTER		(0x05 + SERIAL_BASE_ADDRESS)
 46#define MODEM_STATUS_REGISTER		(0x06 + SERIAL_BASE_ADDRESS)
 47
 48/*
 49 * F81232 Clock registers (106h)
 50 *
 51 * Bit1-0:	Clock source selector
 52 *			00: 1.846MHz.
 53 *			01: 18.46MHz.
 54 *			10: 24MHz.
 55 *			11: 14.77MHz.
 56 */
 57#define F81232_CLK_REGISTER		0x106
 58#define F81232_CLK_1_846_MHZ		0
 59#define F81232_CLK_18_46_MHZ		BIT(0)
 60#define F81232_CLK_24_MHZ		BIT(1)
 61#define F81232_CLK_14_77_MHZ		(BIT(1) | BIT(0))
 62#define F81232_CLK_MASK			GENMASK(1, 0)
 63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 64struct f81232_private {
 65	struct mutex lock;
 66	u8 modem_control;
 67	u8 modem_status;
 68	u8 shadow_lcr;
 69	speed_t baud_base;
 70	struct work_struct lsr_work;
 71	struct work_struct interrupt_work;
 72	struct usb_serial_port *port;
 73};
 74
 75static u32 const baudrate_table[] = { 115200, 921600, 1152000, 1500000 };
 76static u8 const clock_table[] = { F81232_CLK_1_846_MHZ, F81232_CLK_14_77_MHZ,
 77				F81232_CLK_18_46_MHZ, F81232_CLK_24_MHZ };
 78
 79static int calc_baud_divisor(speed_t baudrate, speed_t clockrate)
 80{
 81	if (!baudrate)
 82		return 0;
 83
 84	return DIV_ROUND_CLOSEST(clockrate, baudrate);
 85}
 86
 87static int f81232_get_register(struct usb_serial_port *port, u16 reg, u8 *val)
 88{
 89	int status;
 90	u8 *tmp;
 91	struct usb_device *dev = port->serial->dev;
 92
 93	tmp = kmalloc(sizeof(*val), GFP_KERNEL);
 94	if (!tmp)
 95		return -ENOMEM;
 96
 97	status = usb_control_msg(dev,
 98				usb_rcvctrlpipe(dev, 0),
 99				F81232_REGISTER_REQUEST,
100				F81232_GET_REGISTER,
101				reg,
102				0,
103				tmp,
104				sizeof(*val),
105				USB_CTRL_GET_TIMEOUT);
106	if (status != sizeof(*val)) {
107		dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
108
109		if (status < 0)
110			status = usb_translate_errors(status);
111		else
112			status = -EIO;
113	} else {
114		status = 0;
115		*val = *tmp;
116	}
117
118	kfree(tmp);
119	return status;
120}
121
122static int f81232_set_register(struct usb_serial_port *port, u16 reg, u8 val)
123{
124	int status;
125	u8 *tmp;
126	struct usb_device *dev = port->serial->dev;
127
128	tmp = kmalloc(sizeof(val), GFP_KERNEL);
129	if (!tmp)
130		return -ENOMEM;
131
132	*tmp = val;
133
134	status = usb_control_msg(dev,
135				usb_sndctrlpipe(dev, 0),
136				F81232_REGISTER_REQUEST,
137				F81232_SET_REGISTER,
138				reg,
139				0,
140				tmp,
141				sizeof(val),
142				USB_CTRL_SET_TIMEOUT);
143	if (status != sizeof(val)) {
144		dev_err(&port->dev, "%s failed status: %d\n", __func__, status);
145
146		if (status < 0)
147			status = usb_translate_errors(status);
148		else
149			status = -EIO;
150	} else {
151		status = 0;
152	}
153
154	kfree(tmp);
155	return status;
156}
157
158static int f81232_set_mask_register(struct usb_serial_port *port, u16 reg,
159					u8 mask, u8 val)
160{
161	int status;
162	u8 tmp;
163
164	status = f81232_get_register(port, reg, &tmp);
165	if (status)
166		return status;
167
168	tmp = (tmp & ~mask) | (val & mask);
169
170	return f81232_set_register(port, reg, tmp);
171}
172
173static void f81232_read_msr(struct usb_serial_port *port)
174{
175	int status;
176	u8 current_msr;
177	struct tty_struct *tty;
178	struct f81232_private *priv = usb_get_serial_port_data(port);
179
180	mutex_lock(&priv->lock);
181	status = f81232_get_register(port, MODEM_STATUS_REGISTER,
182			&current_msr);
183	if (status) {
184		dev_err(&port->dev, "%s fail, status: %d\n", __func__, status);
185		mutex_unlock(&priv->lock);
186		return;
187	}
188
189	if (!(current_msr & UART_MSR_ANY_DELTA)) {
190		mutex_unlock(&priv->lock);
191		return;
192	}
193
194	priv->modem_status = current_msr;
195
196	if (current_msr & UART_MSR_DCTS)
197		port->icount.cts++;
198	if (current_msr & UART_MSR_DDSR)
199		port->icount.dsr++;
200	if (current_msr & UART_MSR_TERI)
201		port->icount.rng++;
202	if (current_msr & UART_MSR_DDCD) {
203		port->icount.dcd++;
204		tty = tty_port_tty_get(&port->port);
205		if (tty) {
206			usb_serial_handle_dcd_change(port, tty,
207					current_msr & UART_MSR_DCD);
208
209			tty_kref_put(tty);
210		}
211	}
212
213	wake_up_interruptible(&port->port.delta_msr_wait);
214	mutex_unlock(&priv->lock);
215}
216
217static int f81232_set_mctrl(struct usb_serial_port *port,
218					   unsigned int set, unsigned int clear)
219{
220	u8 val;
221	int status;
222	struct f81232_private *priv = usb_get_serial_port_data(port);
223
224	if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0)
225		return 0;	/* no change */
226
227	/* 'set' takes precedence over 'clear' */
228	clear &= ~set;
229
230	/* force enable interrupt with OUT2 */
231	mutex_lock(&priv->lock);
232	val = UART_MCR_OUT2 | priv->modem_control;
233
234	if (clear & TIOCM_DTR)
235		val &= ~UART_MCR_DTR;
236
237	if (clear & TIOCM_RTS)
238		val &= ~UART_MCR_RTS;
239
240	if (set & TIOCM_DTR)
241		val |= UART_MCR_DTR;
242
243	if (set & TIOCM_RTS)
244		val |= UART_MCR_RTS;
245
246	dev_dbg(&port->dev, "%s new:%02x old:%02x\n", __func__,
247			val, priv->modem_control);
248
249	status = f81232_set_register(port, MODEM_CONTROL_REGISTER, val);
250	if (status) {
251		dev_err(&port->dev, "%s set MCR status < 0\n", __func__);
252		mutex_unlock(&priv->lock);
253		return status;
254	}
255
256	priv->modem_control = val;
257	mutex_unlock(&priv->lock);
258
259	return 0;
260}
261
262static void f81232_update_line_status(struct usb_serial_port *port,
263				      unsigned char *data,
264				      size_t actual_length)
265{
266	struct f81232_private *priv = usb_get_serial_port_data(port);
267
268	if (!actual_length)
269		return;
270
271	switch (data[0] & 0x07) {
272	case 0x00: /* msr change */
273		dev_dbg(&port->dev, "IIR: MSR Change: %02x\n", data[0]);
274		schedule_work(&priv->interrupt_work);
275		break;
276	case 0x02: /* tx-empty */
277		break;
278	case 0x04: /* rx data available */
279		break;
280	case 0x06: /* lsr change */
281		/* we can forget it. the LSR will read from bulk-in */
282		dev_dbg(&port->dev, "IIR: LSR Change: %02x\n", data[0]);
283		break;
284	}
285}
286
287static void f81232_read_int_callback(struct urb *urb)
288{
289	struct usb_serial_port *port =  urb->context;
290	unsigned char *data = urb->transfer_buffer;
291	unsigned int actual_length = urb->actual_length;
292	int status = urb->status;
293	int retval;
294
295	switch (status) {
296	case 0:
297		/* success */
298		break;
299	case -ECONNRESET:
300	case -ENOENT:
301	case -ESHUTDOWN:
302		/* this urb is terminated, clean up */
303		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
304			__func__, status);
305		return;
306	default:
307		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
308			__func__, status);
309		goto exit;
310	}
311
312	usb_serial_debug_data(&port->dev, __func__,
313			      urb->actual_length, urb->transfer_buffer);
314
315	f81232_update_line_status(port, data, actual_length);
316
317exit:
318	retval = usb_submit_urb(urb, GFP_ATOMIC);
319	if (retval)
320		dev_err(&urb->dev->dev,
321			"%s - usb_submit_urb failed with result %d\n",
322			__func__, retval);
323}
324
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325static void f81232_process_read_urb(struct urb *urb)
326{
327	struct usb_serial_port *port = urb->context;
328	struct f81232_private *priv = usb_get_serial_port_data(port);
329	unsigned char *data = urb->transfer_buffer;
330	char tty_flag;
331	unsigned int i;
332	u8 lsr;
333
334	/*
335	 * When opening the port we get a 1-byte packet with the current LSR,
336	 * which we discard.
337	 */
338	if ((urb->actual_length < 2) || (urb->actual_length % 2))
339		return;
340
341	/* bulk-in data: [LSR(1Byte)+DATA(1Byte)][LSR(1Byte)+DATA(1Byte)]... */
342
343	for (i = 0; i < urb->actual_length; i += 2) {
344		tty_flag = TTY_NORMAL;
345		lsr = data[i];
 
346
347		if (lsr & UART_LSR_BRK_ERROR_BITS) {
348			if (lsr & UART_LSR_BI) {
349				tty_flag = TTY_BREAK;
350				port->icount.brk++;
351				usb_serial_handle_break(port);
352			} else if (lsr & UART_LSR_PE) {
353				tty_flag = TTY_PARITY;
354				port->icount.parity++;
355			} else if (lsr & UART_LSR_FE) {
356				tty_flag = TTY_FRAME;
357				port->icount.frame++;
358			}
359
360			if (lsr & UART_LSR_OE) {
361				port->icount.overrun++;
362				schedule_work(&priv->lsr_work);
363				tty_insert_flip_char(&port->port, 0,
364						TTY_OVERRUN);
365			}
366		}
367
368		if (port->port.console && port->sysrq) {
369			if (usb_serial_handle_sysrq_char(port, data[i + 1]))
370				continue;
371		}
372
373		tty_insert_flip_char(&port->port, data[i + 1], tty_flag);
374	}
375
376	tty_flip_buffer_push(&port->port);
377}
378
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379static void f81232_break_ctl(struct tty_struct *tty, int break_state)
380{
381	struct usb_serial_port *port = tty->driver_data;
382	struct f81232_private *priv = usb_get_serial_port_data(port);
383	int status;
384
385	mutex_lock(&priv->lock);
386
387	if (break_state)
388		priv->shadow_lcr |= UART_LCR_SBC;
389	else
390		priv->shadow_lcr &= ~UART_LCR_SBC;
391
392	status = f81232_set_register(port, LINE_CONTROL_REGISTER,
393					priv->shadow_lcr);
394	if (status)
395		dev_err(&port->dev, "set break failed: %d\n", status);
396
397	mutex_unlock(&priv->lock);
398}
399
400static int f81232_find_clk(speed_t baudrate)
401{
402	int idx;
403
404	for (idx = 0; idx < ARRAY_SIZE(baudrate_table); ++idx) {
405		if (baudrate <= baudrate_table[idx] &&
406				baudrate_table[idx] % baudrate == 0)
407			return idx;
408	}
409
410	return -EINVAL;
411}
412
413static void f81232_set_baudrate(struct tty_struct *tty,
414				struct usb_serial_port *port, speed_t baudrate,
415				speed_t old_baudrate)
416{
417	struct f81232_private *priv = usb_get_serial_port_data(port);
418	u8 lcr;
419	int divisor;
420	int status = 0;
421	int i;
422	int idx;
423	speed_t baud_list[] = { baudrate, old_baudrate, F81232_DEF_BAUDRATE };
424
425	for (i = 0; i < ARRAY_SIZE(baud_list); ++i) {
426		idx = f81232_find_clk(baud_list[i]);
 
 
 
 
 
 
427		if (idx >= 0) {
428			baudrate = baud_list[i];
429			tty_encode_baud_rate(tty, baudrate, baudrate);
430			break;
431		}
432	}
433
434	if (idx < 0)
435		return;
436
437	priv->baud_base = baudrate_table[idx];
438	divisor = calc_baud_divisor(baudrate, priv->baud_base);
439
440	status = f81232_set_mask_register(port, F81232_CLK_REGISTER,
441			F81232_CLK_MASK, clock_table[idx]);
442	if (status) {
443		dev_err(&port->dev, "%s failed to set CLK_REG: %d\n",
444			__func__, status);
445		return;
446	}
447
448	status = f81232_get_register(port, LINE_CONTROL_REGISTER,
449			 &lcr); /* get LCR */
450	if (status) {
451		dev_err(&port->dev, "%s failed to get LCR: %d\n",
452			__func__, status);
453		return;
454	}
455
456	status = f81232_set_register(port, LINE_CONTROL_REGISTER,
457			 lcr | UART_LCR_DLAB); /* Enable DLAB */
458	if (status) {
459		dev_err(&port->dev, "%s failed to set DLAB: %d\n",
460			__func__, status);
461		return;
462	}
463
464	status = f81232_set_register(port, RECEIVE_BUFFER_REGISTER,
465			 divisor & 0x00ff); /* low */
466	if (status) {
467		dev_err(&port->dev, "%s failed to set baudrate MSB: %d\n",
468			__func__, status);
469		goto reapply_lcr;
470	}
471
472	status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
473			 (divisor & 0xff00) >> 8); /* high */
474	if (status) {
475		dev_err(&port->dev, "%s failed to set baudrate LSB: %d\n",
476			__func__, status);
477	}
478
479reapply_lcr:
480	status = f81232_set_register(port, LINE_CONTROL_REGISTER,
481			lcr & ~UART_LCR_DLAB);
482	if (status) {
483		dev_err(&port->dev, "%s failed to set DLAB: %d\n",
484			__func__, status);
485	}
486}
487
488static int f81232_port_enable(struct usb_serial_port *port)
489{
490	u8 val;
491	int status;
492
493	/* fifo on, trigger8, clear TX/RX*/
494	val = UART_FCR_TRIGGER_8 | UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
495			UART_FCR_CLEAR_XMIT;
496
497	status = f81232_set_register(port, FIFO_CONTROL_REGISTER, val);
498	if (status) {
499		dev_err(&port->dev, "%s failed to set FCR: %d\n",
500			__func__, status);
501		return status;
502	}
503
504	/* MSR Interrupt only, LSR will read from Bulk-in odd byte */
505	status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER,
506			UART_IER_MSI);
507	if (status) {
508		dev_err(&port->dev, "%s failed to set IER: %d\n",
509			__func__, status);
510		return status;
511	}
512
513	return 0;
514}
515
516static int f81232_port_disable(struct usb_serial_port *port)
517{
518	int status;
519
520	status = f81232_set_register(port, INTERRUPT_ENABLE_REGISTER, 0);
521	if (status) {
522		dev_err(&port->dev, "%s failed to set IER: %d\n",
523			__func__, status);
524		return status;
525	}
526
527	return 0;
528}
529
530static void f81232_set_termios(struct tty_struct *tty,
531		struct usb_serial_port *port, struct ktermios *old_termios)
 
532{
533	struct f81232_private *priv = usb_get_serial_port_data(port);
534	u8 new_lcr = 0;
535	int status = 0;
536	speed_t baudrate;
537	speed_t old_baud;
538
539	/* Don't change anything if nothing has changed */
540	if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
541		return;
542
543	if (C_BAUD(tty) == B0)
544		f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
545	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
546		f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
547
548	baudrate = tty_get_baud_rate(tty);
549	if (baudrate > 0) {
550		if (old_termios)
551			old_baud = tty_termios_baud_rate(old_termios);
552		else
553			old_baud = F81232_DEF_BAUDRATE;
554
555		f81232_set_baudrate(tty, port, baudrate, old_baud);
556	}
557
558	if (C_PARENB(tty)) {
559		new_lcr |= UART_LCR_PARITY;
560
561		if (!C_PARODD(tty))
562			new_lcr |= UART_LCR_EPAR;
563
564		if (C_CMSPAR(tty))
565			new_lcr |= UART_LCR_SPAR;
566	}
567
568	if (C_CSTOPB(tty))
569		new_lcr |= UART_LCR_STOP;
570
571	switch (C_CSIZE(tty)) {
572	case CS5:
573		new_lcr |= UART_LCR_WLEN5;
574		break;
575	case CS6:
576		new_lcr |= UART_LCR_WLEN6;
577		break;
578	case CS7:
579		new_lcr |= UART_LCR_WLEN7;
580		break;
581	default:
582	case CS8:
583		new_lcr |= UART_LCR_WLEN8;
584		break;
585	}
586
587	mutex_lock(&priv->lock);
588
589	new_lcr |= (priv->shadow_lcr & UART_LCR_SBC);
590	status = f81232_set_register(port, LINE_CONTROL_REGISTER, new_lcr);
591	if (status) {
592		dev_err(&port->dev, "%s failed to set LCR: %d\n",
593			__func__, status);
594	}
595
596	priv->shadow_lcr = new_lcr;
597
598	mutex_unlock(&priv->lock);
599}
600
601static int f81232_tiocmget(struct tty_struct *tty)
602{
603	int r;
604	struct usb_serial_port *port = tty->driver_data;
605	struct f81232_private *port_priv = usb_get_serial_port_data(port);
606	u8 mcr, msr;
607
608	/* force get current MSR changed state */
609	f81232_read_msr(port);
610
611	mutex_lock(&port_priv->lock);
612	mcr = port_priv->modem_control;
613	msr = port_priv->modem_status;
614	mutex_unlock(&port_priv->lock);
615
616	r = (mcr & UART_MCR_DTR ? TIOCM_DTR : 0) |
617		(mcr & UART_MCR_RTS ? TIOCM_RTS : 0) |
618		(msr & UART_MSR_CTS ? TIOCM_CTS : 0) |
619		(msr & UART_MSR_DCD ? TIOCM_CAR : 0) |
620		(msr & UART_MSR_RI ? TIOCM_RI : 0) |
621		(msr & UART_MSR_DSR ? TIOCM_DSR : 0);
622
623	return r;
624}
625
626static int f81232_tiocmset(struct tty_struct *tty,
627			unsigned int set, unsigned int clear)
628{
629	struct usb_serial_port *port = tty->driver_data;
630
631	return f81232_set_mctrl(port, set, clear);
632}
633
634static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
635{
636	int result;
637
638	result = f81232_port_enable(port);
639	if (result)
640		return result;
641
642	/* Setup termios */
643	if (tty)
644		f81232_set_termios(tty, port, NULL);
645
646	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
647	if (result) {
648		dev_err(&port->dev, "%s - failed submitting interrupt urb,"
649			" error %d\n", __func__, result);
650		return result;
651	}
652
653	result = usb_serial_generic_open(tty, port);
654	if (result) {
655		usb_kill_urb(port->interrupt_in_urb);
656		return result;
657	}
658
659	return 0;
660}
661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662static void f81232_close(struct usb_serial_port *port)
663{
664	struct f81232_private *port_priv = usb_get_serial_port_data(port);
665
666	f81232_port_disable(port);
667	usb_serial_generic_close(port);
668	usb_kill_urb(port->interrupt_in_urb);
669	flush_work(&port_priv->interrupt_work);
670	flush_work(&port_priv->lsr_work);
671}
672
673static void f81232_dtr_rts(struct usb_serial_port *port, int on)
674{
675	if (on)
676		f81232_set_mctrl(port, TIOCM_DTR | TIOCM_RTS, 0);
677	else
678		f81232_set_mctrl(port, 0, TIOCM_DTR | TIOCM_RTS);
679}
680
 
 
 
 
 
 
 
 
 
 
 
 
 
 
681static int f81232_carrier_raised(struct usb_serial_port *port)
682{
683	u8 msr;
684	struct f81232_private *priv = usb_get_serial_port_data(port);
685
686	mutex_lock(&priv->lock);
687	msr = priv->modem_status;
688	mutex_unlock(&priv->lock);
689
690	if (msr & UART_MSR_DCD)
691		return 1;
692	return 0;
693}
694
695static int f81232_get_serial_info(struct tty_struct *tty,
696		struct serial_struct *ss)
697{
698	struct usb_serial_port *port = tty->driver_data;
699	struct f81232_private *priv = usb_get_serial_port_data(port);
700
701	ss->type = PORT_16550A;
702	ss->line = port->minor;
703	ss->port = port->port_number;
704	ss->baud_base = priv->baud_base;
705	return 0;
706}
707
708static void  f81232_interrupt_work(struct work_struct *work)
709{
710	struct f81232_private *priv =
711		container_of(work, struct f81232_private, interrupt_work);
712
713	f81232_read_msr(priv->port);
714}
715
716static void f81232_lsr_worker(struct work_struct *work)
717{
718	struct f81232_private *priv;
719	struct usb_serial_port *port;
720	int status;
721	u8 tmp;
722
723	priv = container_of(work, struct f81232_private, lsr_work);
724	port = priv->port;
725
726	status = f81232_get_register(port, LINE_STATUS_REGISTER, &tmp);
727	if (status)
728		dev_warn(&port->dev, "read LSR failed: %d\n", status);
729}
730
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
731static int f81232_port_probe(struct usb_serial_port *port)
732{
733	struct f81232_private *priv;
734
735	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
736	if (!priv)
737		return -ENOMEM;
738
739	mutex_init(&priv->lock);
740	INIT_WORK(&priv->interrupt_work,  f81232_interrupt_work);
741	INIT_WORK(&priv->lsr_work, f81232_lsr_worker);
742
743	usb_set_serial_port_data(port, priv);
744
745	port->port.drain_delay = 256;
746	priv->port = port;
747
748	return 0;
749}
750
751static int f81232_port_remove(struct usb_serial_port *port)
752{
753	struct f81232_private *priv;
754
755	priv = usb_get_serial_port_data(port);
756	kfree(priv);
 
 
 
757
758	return 0;
759}
760
761static int f81232_suspend(struct usb_serial *serial, pm_message_t message)
762{
763	struct usb_serial_port *port = serial->port[0];
764	struct f81232_private *port_priv = usb_get_serial_port_data(port);
765	int i;
766
767	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i)
768		usb_kill_urb(port->read_urbs[i]);
769
770	usb_kill_urb(port->interrupt_in_urb);
771
772	if (port_priv) {
773		flush_work(&port_priv->interrupt_work);
774		flush_work(&port_priv->lsr_work);
775	}
776
777	return 0;
778}
779
780static int f81232_resume(struct usb_serial *serial)
781{
782	struct usb_serial_port *port = serial->port[0];
783	int result;
784
785	if (tty_port_initialized(&port->port)) {
786		result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
787		if (result) {
788			dev_err(&port->dev, "submit interrupt urb failed: %d\n",
789					result);
790			return result;
791		}
792	}
793
794	return usb_serial_generic_resume(serial);
795}
796
797static struct usb_serial_driver f81232_device = {
798	.driver = {
799		.owner =	THIS_MODULE,
800		.name =		"f81232",
801	},
802	.id_table =		id_table,
803	.num_ports =		1,
804	.bulk_in_size =		256,
805	.bulk_out_size =	256,
806	.open =			f81232_open,
807	.close =		f81232_close,
808	.dtr_rts =		f81232_dtr_rts,
809	.carrier_raised =	f81232_carrier_raised,
810	.get_serial =		f81232_get_serial_info,
811	.break_ctl =		f81232_break_ctl,
812	.set_termios =		f81232_set_termios,
813	.tiocmget =		f81232_tiocmget,
814	.tiocmset =		f81232_tiocmset,
815	.tiocmiwait =		usb_serial_generic_tiocmiwait,
 
816	.process_read_urb =	f81232_process_read_urb,
817	.read_int_callback =	f81232_read_int_callback,
818	.port_probe =		f81232_port_probe,
819	.port_remove =		f81232_port_remove,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
820	.suspend =		f81232_suspend,
821	.resume =		f81232_resume,
822};
823
824static struct usb_serial_driver * const serial_drivers[] = {
825	&f81232_device,
 
826	NULL,
827};
828
829module_usb_serial_driver(serial_drivers, id_table);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
830
831MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
832MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
833MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
834MODULE_LICENSE("GPL v2");