Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 *	mxuport.c - MOXA UPort series driver
   4 *
   5 *	Copyright (c) 2006 Moxa Technologies Co., Ltd.
   6 *	Copyright (c) 2013 Andrew Lunn <andrew@lunn.ch>
   7 *
 
 
 
 
 
   8 *	Supports the following Moxa USB to serial converters:
   9 *	 2 ports : UPort 1250, UPort 1250I
  10 *	 4 ports : UPort 1410, UPort 1450, UPort 1450I
  11 *	 8 ports : UPort 1610-8, UPort 1650-8
  12 *	16 ports : UPort 1610-16, UPort 1650-16
  13 */
  14
  15#include <linux/kernel.h>
  16#include <linux/module.h>
  17#include <linux/firmware.h>
  18#include <linux/jiffies.h>
  19#include <linux/serial.h>
  20#include <linux/serial_reg.h>
  21#include <linux/slab.h>
  22#include <linux/tty.h>
  23#include <linux/tty_driver.h>
  24#include <linux/tty_flip.h>
  25#include <linux/uaccess.h>
  26#include <linux/usb.h>
  27#include <linux/usb/serial.h>
  28#include <asm/unaligned.h>
  29
  30/* Definitions for the vendor ID and device ID */
  31#define MX_USBSERIAL_VID	0x110A
  32#define MX_UPORT1250_PID	0x1250
  33#define MX_UPORT1251_PID	0x1251
  34#define MX_UPORT1410_PID	0x1410
  35#define MX_UPORT1450_PID	0x1450
  36#define MX_UPORT1451_PID	0x1451
  37#define MX_UPORT1618_PID	0x1618
  38#define MX_UPORT1658_PID	0x1658
  39#define MX_UPORT1613_PID	0x1613
  40#define MX_UPORT1653_PID	0x1653
  41
  42/* Definitions for USB info */
  43#define HEADER_SIZE		4
  44#define EVENT_LENGTH		8
  45#define DOWN_BLOCK_SIZE		64
  46
  47/* Definitions for firmware info */
  48#define VER_ADDR_1		0x20
  49#define VER_ADDR_2		0x24
  50#define VER_ADDR_3		0x28
  51
  52/* Definitions for USB vendor request */
  53#define RQ_VENDOR_NONE			0x00
  54#define RQ_VENDOR_SET_BAUD		0x01 /* Set baud rate */
  55#define RQ_VENDOR_SET_LINE		0x02 /* Set line status */
  56#define RQ_VENDOR_SET_CHARS		0x03 /* Set Xon/Xoff chars */
  57#define RQ_VENDOR_SET_RTS		0x04 /* Set RTS */
  58#define RQ_VENDOR_SET_DTR		0x05 /* Set DTR */
  59#define RQ_VENDOR_SET_XONXOFF		0x06 /* Set auto Xon/Xoff */
  60#define RQ_VENDOR_SET_RX_HOST_EN	0x07 /* Set RX host enable */
  61#define RQ_VENDOR_SET_OPEN		0x08 /* Set open/close port */
  62#define RQ_VENDOR_PURGE			0x09 /* Purge Rx/Tx buffer */
  63#define RQ_VENDOR_SET_MCR		0x0A /* Set MCR register */
  64#define RQ_VENDOR_SET_BREAK		0x0B /* Set Break signal */
  65
  66#define RQ_VENDOR_START_FW_DOWN		0x0C /* Start firmware download */
  67#define RQ_VENDOR_STOP_FW_DOWN		0x0D /* Stop firmware download */
  68#define RQ_VENDOR_QUERY_FW_READY	0x0E /* Query if new firmware ready */
  69
  70#define RQ_VENDOR_SET_FIFO_DISABLE	0x0F /* Set fifo disable */
  71#define RQ_VENDOR_SET_INTERFACE		0x10 /* Set interface */
  72#define RQ_VENDOR_SET_HIGH_PERFOR	0x11 /* Set hi-performance */
  73
  74#define RQ_VENDOR_ERASE_BLOCK		0x12 /* Erase flash block */
  75#define RQ_VENDOR_WRITE_PAGE		0x13 /* Write flash page */
  76#define RQ_VENDOR_PREPARE_WRITE		0x14 /* Prepare write flash */
  77#define RQ_VENDOR_CONFIRM_WRITE		0x15 /* Confirm write flash */
  78#define RQ_VENDOR_LOCATE		0x16 /* Locate the device */
  79
  80#define RQ_VENDOR_START_ROM_DOWN	0x17 /* Start firmware download */
  81#define RQ_VENDOR_ROM_DATA		0x18 /* Rom file data */
  82#define RQ_VENDOR_STOP_ROM_DOWN		0x19 /* Stop firmware download */
  83#define RQ_VENDOR_FW_DATA		0x20 /* Firmware data */
  84
  85#define RQ_VENDOR_RESET_DEVICE		0x23 /* Try to reset the device */
  86#define RQ_VENDOR_QUERY_FW_CONFIG	0x24
  87
  88#define RQ_VENDOR_GET_VERSION		0x81 /* Get firmware version */
  89#define RQ_VENDOR_GET_PAGE		0x82 /* Read flash page */
  90#define RQ_VENDOR_GET_ROM_PROC		0x83 /* Get ROM process state */
  91
  92#define RQ_VENDOR_GET_INQUEUE		0x84 /* Data in input buffer */
  93#define RQ_VENDOR_GET_OUTQUEUE		0x85 /* Data in output buffer */
  94
  95#define RQ_VENDOR_GET_MSR		0x86 /* Get modem status register */
  96
  97/* Definitions for UPort event type */
  98#define UPORT_EVENT_NONE		0 /* None */
  99#define UPORT_EVENT_TXBUF_THRESHOLD	1 /* Tx buffer threshold */
 100#define UPORT_EVENT_SEND_NEXT		2 /* Send next */
 101#define UPORT_EVENT_MSR			3 /* Modem status */
 102#define UPORT_EVENT_LSR			4 /* Line status */
 103#define UPORT_EVENT_MCR			5 /* Modem control */
 104
 105/* Definitions for serial event type */
 106#define SERIAL_EV_CTS			0x0008	/* CTS changed state */
 107#define SERIAL_EV_DSR			0x0010	/* DSR changed state */
 108#define SERIAL_EV_RLSD			0x0020	/* RLSD changed state */
 109
 110/* Definitions for modem control event type */
 111#define SERIAL_EV_XOFF			0x40	/* XOFF received */
 112
 113/* Definitions for line control of communication */
 114#define MX_WORDLENGTH_5			5
 115#define MX_WORDLENGTH_6			6
 116#define MX_WORDLENGTH_7			7
 117#define MX_WORDLENGTH_8			8
 118
 119#define MX_PARITY_NONE			0
 120#define MX_PARITY_ODD			1
 121#define MX_PARITY_EVEN			2
 122#define MX_PARITY_MARK			3
 123#define MX_PARITY_SPACE			4
 124
 125#define MX_STOP_BITS_1			0
 126#define MX_STOP_BITS_1_5		1
 127#define MX_STOP_BITS_2			2
 128
 129#define MX_RTS_DISABLE			0x0
 130#define MX_RTS_ENABLE			0x1
 131#define MX_RTS_HW			0x2
 132#define MX_RTS_NO_CHANGE		0x3 /* Flag, not valid register value*/
 133
 134#define MX_INT_RS232			0
 135#define MX_INT_2W_RS485			1
 136#define MX_INT_RS422			2
 137#define MX_INT_4W_RS485			3
 138
 139/* Definitions for holding reason */
 140#define MX_WAIT_FOR_CTS			0x0001
 141#define MX_WAIT_FOR_DSR			0x0002
 142#define MX_WAIT_FOR_DCD			0x0004
 143#define MX_WAIT_FOR_XON			0x0008
 144#define MX_WAIT_FOR_START_TX		0x0010
 145#define MX_WAIT_FOR_UNTHROTTLE		0x0020
 146#define MX_WAIT_FOR_LOW_WATER		0x0040
 147#define MX_WAIT_FOR_SEND_NEXT		0x0080
 148
 149#define MX_UPORT_2_PORT			BIT(0)
 150#define MX_UPORT_4_PORT			BIT(1)
 151#define MX_UPORT_8_PORT			BIT(2)
 152#define MX_UPORT_16_PORT		BIT(3)
 153
 154/* This structure holds all of the local port information */
 155struct mxuport_port {
 156	u8 mcr_state;		/* Last MCR state */
 157	u8 msr_state;		/* Last MSR state */
 158	struct mutex mutex;	/* Protects mcr_state */
 159	spinlock_t spinlock;	/* Protects msr_state */
 160};
 161
 162/* Table of devices that work with this driver */
 163static const struct usb_device_id mxuport_idtable[] = {
 164	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1250_PID),
 165	  .driver_info = MX_UPORT_2_PORT },
 166	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1251_PID),
 167	  .driver_info = MX_UPORT_2_PORT },
 168	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1410_PID),
 169	  .driver_info = MX_UPORT_4_PORT },
 170	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1450_PID),
 171	  .driver_info = MX_UPORT_4_PORT },
 172	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1451_PID),
 173	  .driver_info = MX_UPORT_4_PORT },
 174	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1618_PID),
 175	  .driver_info = MX_UPORT_8_PORT },
 176	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1658_PID),
 177	  .driver_info = MX_UPORT_8_PORT },
 178	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1613_PID),
 179	  .driver_info = MX_UPORT_16_PORT },
 180	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1653_PID),
 181	  .driver_info = MX_UPORT_16_PORT },
 182	{}			/* Terminating entry */
 183};
 184
 185MODULE_DEVICE_TABLE(usb, mxuport_idtable);
 186
 187/*
 188 * Add a four byte header containing the port number and the number of
 189 * bytes of data in the message. Return the number of bytes in the
 190 * buffer.
 191 */
 192static int mxuport_prepare_write_buffer(struct usb_serial_port *port,
 193					void *dest, size_t size)
 194{
 195	u8 *buf = dest;
 196	int count;
 197
 198	count = kfifo_out_locked(&port->write_fifo, buf + HEADER_SIZE,
 199				 size - HEADER_SIZE,
 200				 &port->lock);
 201
 202	put_unaligned_be16(port->port_number, buf);
 203	put_unaligned_be16(count, buf + 2);
 204
 205	dev_dbg(&port->dev, "%s - size %zd count %d\n", __func__,
 206		size, count);
 207
 208	return count + HEADER_SIZE;
 209}
 210
 211/* Read the given buffer in from the control pipe. */
 212static int mxuport_recv_ctrl_urb(struct usb_serial *serial,
 213				 u8 request, u16 value, u16 index,
 214				 u8 *data, size_t size)
 215{
 216	int status;
 217
 218	status = usb_control_msg(serial->dev,
 219				 usb_rcvctrlpipe(serial->dev, 0),
 220				 request,
 221				 (USB_DIR_IN | USB_TYPE_VENDOR |
 222				  USB_RECIP_DEVICE), value, index,
 223				 data, size,
 224				 USB_CTRL_GET_TIMEOUT);
 225	if (status < 0) {
 226		dev_err(&serial->interface->dev,
 227			"%s - usb_control_msg failed (%d)\n",
 228			__func__, status);
 229		return status;
 230	}
 231
 232	if (status != size) {
 233		dev_err(&serial->interface->dev,
 234			"%s - short read (%d / %zd)\n",
 235			__func__, status, size);
 236		return -EIO;
 237	}
 238
 239	return status;
 240}
 241
 242/* Write the given buffer out to the control pipe.  */
 243static int mxuport_send_ctrl_data_urb(struct usb_serial *serial,
 244				      u8 request,
 245				      u16 value, u16 index,
 246				      u8 *data, size_t size)
 247{
 248	int status;
 249
 250	status = usb_control_msg(serial->dev,
 251				 usb_sndctrlpipe(serial->dev, 0),
 252				 request,
 253				 (USB_DIR_OUT | USB_TYPE_VENDOR |
 254				  USB_RECIP_DEVICE), value, index,
 255				 data, size,
 256				 USB_CTRL_SET_TIMEOUT);
 257	if (status < 0) {
 258		dev_err(&serial->interface->dev,
 259			"%s - usb_control_msg failed (%d)\n",
 260			__func__, status);
 261		return status;
 262	}
 263
 264	if (status != size) {
 265		dev_err(&serial->interface->dev,
 266			"%s - short write (%d / %zd)\n",
 267			__func__, status, size);
 268		return -EIO;
 269	}
 270
 271	return 0;
 272}
 273
 274/* Send a vendor request without any data */
 275static int mxuport_send_ctrl_urb(struct usb_serial *serial,
 276				 u8 request, u16 value, u16 index)
 277{
 278	return mxuport_send_ctrl_data_urb(serial, request, value, index,
 279					  NULL, 0);
 280}
 281
 282/*
 283 * mxuport_throttle - throttle function of driver
 284 *
 285 * This function is called by the tty driver when it wants to stop the
 286 * data being read from the port. Since all the data comes over one
 287 * bulk in endpoint, we cannot stop submitting urbs by setting
 288 * port->throttle. Instead tell the device to stop sending us data for
 289 * the port.
 290 */
 291static void mxuport_throttle(struct tty_struct *tty)
 292{
 293	struct usb_serial_port *port = tty->driver_data;
 294	struct usb_serial *serial = port->serial;
 295
 296	dev_dbg(&port->dev, "%s\n", __func__);
 297
 298	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
 299			      0, port->port_number);
 300}
 301
 302/*
 303 * mxuport_unthrottle - unthrottle function of driver
 304 *
 305 * This function is called by the tty driver when it wants to resume
 306 * the data being read from the port. Tell the device it can resume
 307 * sending us received data from the port.
 308 */
 309static void mxuport_unthrottle(struct tty_struct *tty)
 310{
 311
 312	struct usb_serial_port *port = tty->driver_data;
 313	struct usb_serial *serial = port->serial;
 314
 315	dev_dbg(&port->dev, "%s\n", __func__);
 316
 317	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
 318			      1, port->port_number);
 319}
 320
 321/*
 322 * Processes one chunk of data received for a port.  Mostly a copy of
 323 * usb_serial_generic_process_read_urb().
 324 */
 325static void mxuport_process_read_urb_data(struct usb_serial_port *port,
 326					  char *data, int size)
 327{
 328	int i;
 329
 330	if (port->sysrq) {
 
 
 331		for (i = 0; i < size; i++, data++) {
 332			if (!usb_serial_handle_sysrq_char(port, *data))
 333				tty_insert_flip_char(&port->port, *data,
 334						     TTY_NORMAL);
 335		}
 336	} else {
 337		tty_insert_flip_string(&port->port, data, size);
 338	}
 339	tty_flip_buffer_push(&port->port);
 340}
 341
 342static void mxuport_msr_event(struct usb_serial_port *port, u8 buf[4])
 343{
 344	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 345	u8 rcv_msr_hold = buf[2] & 0xF0;
 346	u16 rcv_msr_event = get_unaligned_be16(buf);
 347	unsigned long flags;
 348
 349	if (rcv_msr_event == 0)
 350		return;
 351
 352	/* Update MSR status */
 353	spin_lock_irqsave(&mxport->spinlock, flags);
 354
 355	dev_dbg(&port->dev, "%s - current MSR status = 0x%x\n",
 356		__func__, mxport->msr_state);
 357
 358	if (rcv_msr_hold & UART_MSR_CTS) {
 359		mxport->msr_state |= UART_MSR_CTS;
 360		dev_dbg(&port->dev, "%s - CTS high\n", __func__);
 361	} else {
 362		mxport->msr_state &= ~UART_MSR_CTS;
 363		dev_dbg(&port->dev, "%s - CTS low\n", __func__);
 364	}
 365
 366	if (rcv_msr_hold & UART_MSR_DSR) {
 367		mxport->msr_state |= UART_MSR_DSR;
 368		dev_dbg(&port->dev, "%s - DSR high\n", __func__);
 369	} else {
 370		mxport->msr_state &= ~UART_MSR_DSR;
 371		dev_dbg(&port->dev, "%s - DSR low\n", __func__);
 372	}
 373
 374	if (rcv_msr_hold & UART_MSR_DCD) {
 375		mxport->msr_state |= UART_MSR_DCD;
 376		dev_dbg(&port->dev, "%s - DCD high\n", __func__);
 377	} else {
 378		mxport->msr_state &= ~UART_MSR_DCD;
 379		dev_dbg(&port->dev, "%s - DCD low\n", __func__);
 380	}
 381	spin_unlock_irqrestore(&mxport->spinlock, flags);
 382
 383	if (rcv_msr_event &
 384	    (SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RLSD)) {
 385
 386		if (rcv_msr_event & SERIAL_EV_CTS) {
 387			port->icount.cts++;
 388			dev_dbg(&port->dev, "%s - CTS change\n", __func__);
 389		}
 390
 391		if (rcv_msr_event & SERIAL_EV_DSR) {
 392			port->icount.dsr++;
 393			dev_dbg(&port->dev, "%s - DSR change\n", __func__);
 394		}
 395
 396		if (rcv_msr_event & SERIAL_EV_RLSD) {
 397			port->icount.dcd++;
 398			dev_dbg(&port->dev, "%s - DCD change\n", __func__);
 399		}
 400		wake_up_interruptible(&port->port.delta_msr_wait);
 401	}
 402}
 403
 404static void mxuport_lsr_event(struct usb_serial_port *port, u8 buf[4])
 405{
 406	u8 lsr_event = buf[2];
 407
 408	if (lsr_event & UART_LSR_BI) {
 409		port->icount.brk++;
 410		dev_dbg(&port->dev, "%s - break error\n", __func__);
 411	}
 412
 413	if (lsr_event & UART_LSR_FE) {
 414		port->icount.frame++;
 415		dev_dbg(&port->dev, "%s - frame error\n", __func__);
 416	}
 417
 418	if (lsr_event & UART_LSR_PE) {
 419		port->icount.parity++;
 420		dev_dbg(&port->dev, "%s - parity error\n", __func__);
 421	}
 422
 423	if (lsr_event & UART_LSR_OE) {
 424		port->icount.overrun++;
 425		dev_dbg(&port->dev, "%s - overrun error\n", __func__);
 426	}
 427}
 428
 429/*
 430 * When something interesting happens, modem control lines XON/XOFF
 431 * etc, the device sends an event. Process these events.
 432 */
 433static void mxuport_process_read_urb_event(struct usb_serial_port *port,
 434					   u8 buf[4], u32 event)
 435{
 436	dev_dbg(&port->dev, "%s - receive event : %04x\n", __func__, event);
 437
 438	switch (event) {
 439	case UPORT_EVENT_SEND_NEXT:
 440		/*
 441		 * Sent as part of the flow control on device buffers.
 442		 * Not currently used.
 443		 */
 444		break;
 445	case UPORT_EVENT_MSR:
 446		mxuport_msr_event(port, buf);
 447		break;
 448	case UPORT_EVENT_LSR:
 449		mxuport_lsr_event(port, buf);
 450		break;
 451	case UPORT_EVENT_MCR:
 452		/*
 453		 * Event to indicate a change in XON/XOFF from the
 454		 * peer.  Currently not used. We just continue
 455		 * sending the device data and it will buffer it if
 456		 * needed. This event could be used for flow control
 457		 * between the host and the device.
 458		 */
 459		break;
 460	default:
 461		dev_dbg(&port->dev, "Unexpected event\n");
 462		break;
 463	}
 464}
 465
 466/*
 467 * One URB can contain data for multiple ports. Demultiplex the data,
 468 * checking the port exists, is opened and the message is valid.
 469 */
 470static void mxuport_process_read_urb_demux_data(struct urb *urb)
 471{
 472	struct usb_serial_port *port = urb->context;
 473	struct usb_serial *serial = port->serial;
 474	u8 *data = urb->transfer_buffer;
 475	u8 *end = data + urb->actual_length;
 476	struct usb_serial_port *demux_port;
 477	u8 *ch;
 478	u16 rcv_port;
 479	u16 rcv_len;
 480
 481	while (data < end) {
 482		if (data + HEADER_SIZE > end) {
 483			dev_warn(&port->dev, "%s - message with short header\n",
 484				 __func__);
 485			return;
 486		}
 487
 488		rcv_port = get_unaligned_be16(data);
 489		if (rcv_port >= serial->num_ports) {
 490			dev_warn(&port->dev, "%s - message for invalid port\n",
 491				 __func__);
 492			return;
 493		}
 494
 495		demux_port = serial->port[rcv_port];
 496		rcv_len = get_unaligned_be16(data + 2);
 497		if (!rcv_len || data + HEADER_SIZE + rcv_len > end) {
 498			dev_warn(&port->dev, "%s - short data\n", __func__);
 499			return;
 500		}
 501
 502		if (tty_port_initialized(&demux_port->port)) {
 503			ch = data + HEADER_SIZE;
 504			mxuport_process_read_urb_data(demux_port, ch, rcv_len);
 505		} else {
 506			dev_dbg(&demux_port->dev, "%s - data for closed port\n",
 507				__func__);
 508		}
 509		data += HEADER_SIZE + rcv_len;
 510	}
 511}
 512
 513/*
 514 * One URB can contain events for multiple ports. Demultiplex the event,
 515 * checking the port exists, and is opened.
 516 */
 517static void mxuport_process_read_urb_demux_event(struct urb *urb)
 518{
 519	struct usb_serial_port *port = urb->context;
 520	struct usb_serial *serial = port->serial;
 521	u8 *data = urb->transfer_buffer;
 522	u8 *end = data + urb->actual_length;
 523	struct usb_serial_port *demux_port;
 524	u8 *ch;
 525	u16 rcv_port;
 526	u16 rcv_event;
 527
 528	while (data < end) {
 529		if (data + EVENT_LENGTH > end) {
 530			dev_warn(&port->dev, "%s - message with short event\n",
 531				 __func__);
 532			return;
 533		}
 534
 535		rcv_port = get_unaligned_be16(data);
 536		if (rcv_port >= serial->num_ports) {
 537			dev_warn(&port->dev, "%s - message for invalid port\n",
 538				 __func__);
 539			return;
 540		}
 541
 542		demux_port = serial->port[rcv_port];
 543		if (tty_port_initialized(&demux_port->port)) {
 544			ch = data + HEADER_SIZE;
 545			rcv_event = get_unaligned_be16(data + 2);
 546			mxuport_process_read_urb_event(demux_port, ch,
 547						       rcv_event);
 548		} else {
 549			dev_dbg(&demux_port->dev,
 550				"%s - event for closed port\n", __func__);
 551		}
 552		data += EVENT_LENGTH;
 553	}
 554}
 555
 556/*
 557 * This is called when we have received data on the bulk in
 558 * endpoint. Depending on which port it was received on, it can
 559 * contain serial data or events.
 560 */
 561static void mxuport_process_read_urb(struct urb *urb)
 562{
 563	struct usb_serial_port *port = urb->context;
 564	struct usb_serial *serial = port->serial;
 565
 566	if (port == serial->port[0])
 567		mxuport_process_read_urb_demux_data(urb);
 568
 569	if (port == serial->port[1])
 570		mxuport_process_read_urb_demux_event(urb);
 571}
 572
 573/*
 574 * Ask the device how many bytes it has queued to be sent out. If
 575 * there are none, return true.
 576 */
 577static bool mxuport_tx_empty(struct usb_serial_port *port)
 578{
 579	struct usb_serial *serial = port->serial;
 580	bool is_empty = true;
 581	u32 txlen;
 582	u8 *len_buf;
 583	int err;
 584
 585	len_buf = kzalloc(4, GFP_KERNEL);
 586	if (!len_buf)
 587		goto out;
 588
 589	err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_OUTQUEUE, 0,
 590				    port->port_number, len_buf, 4);
 591	if (err < 0)
 592		goto out;
 593
 594	txlen = get_unaligned_be32(len_buf);
 595	dev_dbg(&port->dev, "%s - tx len = %u\n", __func__, txlen);
 596
 597	if (txlen != 0)
 598		is_empty = false;
 599
 600out:
 601	kfree(len_buf);
 602	return is_empty;
 603}
 604
 605static int mxuport_set_mcr(struct usb_serial_port *port, u8 mcr_state)
 606{
 607	struct usb_serial *serial = port->serial;
 608	int err;
 609
 610	dev_dbg(&port->dev, "%s - %02x\n", __func__, mcr_state);
 611
 612	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_MCR,
 613				    mcr_state, port->port_number);
 614	if (err)
 615		dev_err(&port->dev, "%s - failed to change MCR\n", __func__);
 616
 617	return err;
 618}
 619
 620static int mxuport_set_dtr(struct usb_serial_port *port, int on)
 621{
 622	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 623	struct usb_serial *serial = port->serial;
 624	int err;
 625
 626	mutex_lock(&mxport->mutex);
 627
 628	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_DTR,
 629				    !!on, port->port_number);
 630	if (!err) {
 631		if (on)
 632			mxport->mcr_state |= UART_MCR_DTR;
 633		else
 634			mxport->mcr_state &= ~UART_MCR_DTR;
 635	}
 636
 637	mutex_unlock(&mxport->mutex);
 638
 639	return err;
 640}
 641
 642static int mxuport_set_rts(struct usb_serial_port *port, u8 state)
 643{
 644	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 645	struct usb_serial *serial = port->serial;
 646	int err;
 647	u8 mcr_state;
 648
 649	mutex_lock(&mxport->mutex);
 650	mcr_state = mxport->mcr_state;
 651
 652	switch (state) {
 653	case MX_RTS_DISABLE:
 654		mcr_state &= ~UART_MCR_RTS;
 655		break;
 656	case MX_RTS_ENABLE:
 657		mcr_state |= UART_MCR_RTS;
 658		break;
 659	case MX_RTS_HW:
 660		/*
 661		 * Do not update mxport->mcr_state when doing hardware
 662		 * flow control.
 663		 */
 664		break;
 665	default:
 666		/*
 667		 * Should not happen, but somebody might try passing
 668		 * MX_RTS_NO_CHANGE, which is not valid.
 669		 */
 670		err = -EINVAL;
 671		goto out;
 672	}
 673	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RTS,
 674				    state, port->port_number);
 675	if (!err)
 676		mxport->mcr_state = mcr_state;
 677
 678out:
 679	mutex_unlock(&mxport->mutex);
 680
 681	return err;
 682}
 683
 684static void mxuport_dtr_rts(struct usb_serial_port *port, int on)
 685{
 686	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 687	u8 mcr_state;
 688	int err;
 689
 690	mutex_lock(&mxport->mutex);
 691	mcr_state = mxport->mcr_state;
 692
 693	if (on)
 694		mcr_state |= (UART_MCR_RTS | UART_MCR_DTR);
 695	else
 696		mcr_state &= ~(UART_MCR_RTS | UART_MCR_DTR);
 697
 698	err = mxuport_set_mcr(port, mcr_state);
 699	if (!err)
 700		mxport->mcr_state = mcr_state;
 701
 702	mutex_unlock(&mxport->mutex);
 703}
 704
 705static int mxuport_tiocmset(struct tty_struct *tty, unsigned int set,
 706			    unsigned int clear)
 707{
 708	struct usb_serial_port *port = tty->driver_data;
 709	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 710	int err;
 711	u8 mcr_state;
 712
 713	mutex_lock(&mxport->mutex);
 714	mcr_state = mxport->mcr_state;
 715
 716	if (set & TIOCM_RTS)
 717		mcr_state |= UART_MCR_RTS;
 718
 719	if (set & TIOCM_DTR)
 720		mcr_state |= UART_MCR_DTR;
 721
 722	if (clear & TIOCM_RTS)
 723		mcr_state &= ~UART_MCR_RTS;
 724
 725	if (clear & TIOCM_DTR)
 726		mcr_state &= ~UART_MCR_DTR;
 727
 728	err = mxuport_set_mcr(port, mcr_state);
 729	if (!err)
 730		mxport->mcr_state = mcr_state;
 731
 732	mutex_unlock(&mxport->mutex);
 733
 734	return err;
 735}
 736
 737static int mxuport_tiocmget(struct tty_struct *tty)
 738{
 739	struct mxuport_port *mxport;
 740	struct usb_serial_port *port = tty->driver_data;
 741	unsigned int result;
 742	unsigned long flags;
 743	unsigned int msr;
 744	unsigned int mcr;
 745
 746	mxport = usb_get_serial_port_data(port);
 747
 748	mutex_lock(&mxport->mutex);
 749	spin_lock_irqsave(&mxport->spinlock, flags);
 750
 751	msr = mxport->msr_state;
 752	mcr = mxport->mcr_state;
 753
 754	spin_unlock_irqrestore(&mxport->spinlock, flags);
 755	mutex_unlock(&mxport->mutex);
 756
 757	result = (((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) |	/* 0x002 */
 758		  ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) |	/* 0x004 */
 759		  ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) |	/* 0x020 */
 760		  ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) |	/* 0x040 */
 761		  ((msr & UART_MSR_RI) ? TIOCM_RI : 0) |	/* 0x080 */
 762		  ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0));	/* 0x100 */
 763
 764	dev_dbg(&port->dev, "%s - 0x%04x\n", __func__, result);
 765
 766	return result;
 767}
 768
 769static int mxuport_set_termios_flow(struct tty_struct *tty,
 770				    struct ktermios *old_termios,
 771				    struct usb_serial_port *port,
 772				    struct usb_serial *serial)
 773{
 774	u8 xon = START_CHAR(tty);
 775	u8 xoff = STOP_CHAR(tty);
 776	int enable;
 777	int err;
 778	u8 *buf;
 779	u8 rts;
 780
 781	buf = kmalloc(2, GFP_KERNEL);
 782	if (!buf)
 783		return -ENOMEM;
 784
 785	/* S/W flow control settings */
 786	if (I_IXOFF(tty) || I_IXON(tty)) {
 787		enable = 1;
 788		buf[0] = xon;
 789		buf[1] = xoff;
 790
 791		err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_CHARS,
 792						 0, port->port_number,
 793						 buf, 2);
 794		if (err)
 795			goto out;
 796
 797		dev_dbg(&port->dev, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
 798			__func__, xon, xoff);
 799	} else {
 800		enable = 0;
 801	}
 802
 803	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_XONXOFF,
 804				    enable, port->port_number);
 805	if (err)
 806		goto out;
 807
 808	rts = MX_RTS_NO_CHANGE;
 809
 810	/* H/W flow control settings */
 811	if (!old_termios ||
 812	    C_CRTSCTS(tty) != (old_termios->c_cflag & CRTSCTS)) {
 813		if (C_CRTSCTS(tty))
 814			rts = MX_RTS_HW;
 815		else
 816			rts = MX_RTS_ENABLE;
 817	}
 818
 819	if (C_BAUD(tty)) {
 820		if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
 821			/* Raise DTR and RTS */
 822			if (C_CRTSCTS(tty))
 823				rts = MX_RTS_HW;
 824			else
 825				rts = MX_RTS_ENABLE;
 826			mxuport_set_dtr(port, 1);
 827		}
 828	} else {
 829		/* Drop DTR and RTS */
 830		rts = MX_RTS_DISABLE;
 831		mxuport_set_dtr(port, 0);
 832	}
 833
 834	if (rts != MX_RTS_NO_CHANGE)
 835		err = mxuport_set_rts(port, rts);
 836
 837out:
 838	kfree(buf);
 839	return err;
 840}
 841
 842static void mxuport_set_termios(struct tty_struct *tty,
 843				struct usb_serial_port *port,
 844				struct ktermios *old_termios)
 845{
 846	struct usb_serial *serial = port->serial;
 847	u8 *buf;
 848	u8 data_bits;
 849	u8 stop_bits;
 850	u8 parity;
 851	int baud;
 852	int err;
 853
 854	if (old_termios &&
 855	    !tty_termios_hw_change(&tty->termios, old_termios) &&
 856	    tty->termios.c_iflag == old_termios->c_iflag) {
 857		dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
 858		return;
 859	}
 860
 861	buf = kmalloc(4, GFP_KERNEL);
 862	if (!buf)
 863		return;
 864
 865	/* Set data bit of termios */
 866	switch (C_CSIZE(tty)) {
 867	case CS5:
 868		data_bits = MX_WORDLENGTH_5;
 869		break;
 870	case CS6:
 871		data_bits = MX_WORDLENGTH_6;
 872		break;
 873	case CS7:
 874		data_bits = MX_WORDLENGTH_7;
 875		break;
 876	case CS8:
 877	default:
 878		data_bits = MX_WORDLENGTH_8;
 879		break;
 880	}
 881
 882	/* Set parity of termios */
 883	if (C_PARENB(tty)) {
 884		if (C_CMSPAR(tty)) {
 885			if (C_PARODD(tty))
 886				parity = MX_PARITY_MARK;
 887			else
 888				parity = MX_PARITY_SPACE;
 889		} else {
 890			if (C_PARODD(tty))
 891				parity = MX_PARITY_ODD;
 892			else
 893				parity = MX_PARITY_EVEN;
 894		}
 895	} else {
 896		parity = MX_PARITY_NONE;
 897	}
 898
 899	/* Set stop bit of termios */
 900	if (C_CSTOPB(tty))
 901		stop_bits = MX_STOP_BITS_2;
 902	else
 903		stop_bits = MX_STOP_BITS_1;
 904
 905	buf[0] = data_bits;
 906	buf[1] = parity;
 907	buf[2] = stop_bits;
 908	buf[3] = 0;
 909
 910	err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_LINE,
 911					 0, port->port_number, buf, 4);
 912	if (err)
 913		goto out;
 914
 915	err = mxuport_set_termios_flow(tty, old_termios, port, serial);
 916	if (err)
 917		goto out;
 918
 919	baud = tty_get_baud_rate(tty);
 920	if (!baud)
 921		baud = 9600;
 922
 923	/* Note: Little Endian */
 924	put_unaligned_le32(baud, buf);
 925
 926	err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_BAUD,
 927					 0, port->port_number,
 928					 buf, 4);
 929	if (err)
 930		goto out;
 931
 932	dev_dbg(&port->dev, "baud_rate	: %d\n", baud);
 933	dev_dbg(&port->dev, "data_bits	: %d\n", data_bits);
 934	dev_dbg(&port->dev, "parity	: %d\n", parity);
 935	dev_dbg(&port->dev, "stop_bits	: %d\n", stop_bits);
 936
 937out:
 938	kfree(buf);
 939}
 940
 941/*
 942 * Determine how many ports this device has dynamically.  It will be
 943 * called after the probe() callback is called, but before attach().
 944 */
 945static int mxuport_calc_num_ports(struct usb_serial *serial,
 946					struct usb_serial_endpoints *epds)
 947{
 948	unsigned long features = (unsigned long)usb_get_serial_data(serial);
 949	int num_ports;
 950	int i;
 951
 952	if (features & MX_UPORT_2_PORT) {
 953		num_ports = 2;
 954	} else if (features & MX_UPORT_4_PORT) {
 955		num_ports = 4;
 956	} else if (features & MX_UPORT_8_PORT) {
 957		num_ports = 8;
 958	} else if (features & MX_UPORT_16_PORT) {
 959		num_ports = 16;
 960	} else {
 961		dev_warn(&serial->interface->dev,
 962				"unknown device, assuming two ports\n");
 963		num_ports = 2;
 964	}
 965
 966	/*
 967	 * Setup bulk-out endpoint multiplexing. All ports share the same
 968	 * bulk-out endpoint.
 969	 */
 970	BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < 16);
 971
 972	for (i = 1; i < num_ports; ++i)
 973		epds->bulk_out[i] = epds->bulk_out[0];
 
 
 
 
 
 
 974
 975	epds->num_bulk_out = num_ports;
 976
 977	return num_ports;
 978}
 979
 980/* Get the version of the firmware currently running. */
 981static int mxuport_get_fw_version(struct usb_serial *serial, u32 *version)
 982{
 983	u8 *ver_buf;
 984	int err;
 985
 986	ver_buf = kzalloc(4, GFP_KERNEL);
 987	if (!ver_buf)
 988		return -ENOMEM;
 989
 990	/* Get firmware version from SDRAM */
 991	err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_VERSION, 0, 0,
 992				    ver_buf, 4);
 993	if (err != 4) {
 994		err = -EIO;
 995		goto out;
 996	}
 997
 998	*version = (ver_buf[0] << 16) | (ver_buf[1] << 8) | ver_buf[2];
 999	err = 0;
1000out:
1001	kfree(ver_buf);
1002	return err;
1003}
1004
1005/* Given a firmware blob, download it to the device. */
1006static int mxuport_download_fw(struct usb_serial *serial,
1007			       const struct firmware *fw_p)
1008{
1009	u8 *fw_buf;
1010	size_t txlen;
1011	size_t fwidx;
1012	int err;
1013
1014	fw_buf = kmalloc(DOWN_BLOCK_SIZE, GFP_KERNEL);
1015	if (!fw_buf)
1016		return -ENOMEM;
1017
1018	dev_dbg(&serial->interface->dev, "Starting firmware download...\n");
1019	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_START_FW_DOWN, 0, 0);
1020	if (err)
1021		goto out;
1022
1023	fwidx = 0;
1024	do {
1025		txlen = min_t(size_t, (fw_p->size - fwidx), DOWN_BLOCK_SIZE);
1026
1027		memcpy(fw_buf, &fw_p->data[fwidx], txlen);
1028		err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_FW_DATA,
1029						 0, 0, fw_buf, txlen);
1030		if (err) {
1031			mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN,
1032					      0, 0);
1033			goto out;
1034		}
1035
1036		fwidx += txlen;
1037		usleep_range(1000, 2000);
1038
1039	} while (fwidx < fw_p->size);
1040
1041	msleep(1000);
1042	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, 0, 0);
1043	if (err)
1044		goto out;
1045
1046	msleep(1000);
1047	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_READY, 0, 0);
1048
1049out:
1050	kfree(fw_buf);
1051	return err;
1052}
1053
1054static int mxuport_probe(struct usb_serial *serial,
1055			 const struct usb_device_id *id)
1056{
1057	u16 productid = le16_to_cpu(serial->dev->descriptor.idProduct);
1058	const struct firmware *fw_p = NULL;
1059	u32 version;
1060	int local_ver;
1061	char buf[32];
1062	int err;
1063
1064	/* Load our firmware */
1065	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_CONFIG, 0, 0);
1066	if (err) {
1067		mxuport_send_ctrl_urb(serial, RQ_VENDOR_RESET_DEVICE, 0, 0);
1068		return err;
1069	}
1070
1071	err = mxuport_get_fw_version(serial, &version);
1072	if (err < 0)
1073		return err;
1074
1075	dev_dbg(&serial->interface->dev, "Device firmware version v%x.%x.%x\n",
1076		(version & 0xff0000) >> 16,
1077		(version & 0xff00) >> 8,
1078		(version & 0xff));
1079
1080	snprintf(buf, sizeof(buf) - 1, "moxa/moxa-%04x.fw", productid);
1081
1082	err = request_firmware(&fw_p, buf, &serial->interface->dev);
1083	if (err) {
1084		dev_warn(&serial->interface->dev, "Firmware %s not found\n",
1085			 buf);
1086
1087		/* Use the firmware already in the device */
1088		err = 0;
1089	} else {
1090		local_ver = ((fw_p->data[VER_ADDR_1] << 16) |
1091			     (fw_p->data[VER_ADDR_2] << 8) |
1092			     fw_p->data[VER_ADDR_3]);
1093		dev_dbg(&serial->interface->dev,
1094			"Available firmware version v%x.%x.%x\n",
1095			fw_p->data[VER_ADDR_1], fw_p->data[VER_ADDR_2],
1096			fw_p->data[VER_ADDR_3]);
1097		if (local_ver > version) {
1098			err = mxuport_download_fw(serial, fw_p);
1099			if (err)
1100				goto out;
1101			err  = mxuport_get_fw_version(serial, &version);
1102			if (err < 0)
1103				goto out;
1104		}
1105	}
1106
1107	dev_info(&serial->interface->dev,
1108		 "Using device firmware version v%x.%x.%x\n",
1109		 (version & 0xff0000) >> 16,
1110		 (version & 0xff00) >> 8,
1111		 (version & 0xff));
1112
1113	/*
1114	 * Contains the features of this hardware. Store away for
1115	 * later use, eg, number of ports.
1116	 */
1117	usb_set_serial_data(serial, (void *)id->driver_info);
1118out:
1119	if (fw_p)
1120		release_firmware(fw_p);
1121	return err;
1122}
1123
1124
1125static int mxuport_port_probe(struct usb_serial_port *port)
1126{
1127	struct usb_serial *serial = port->serial;
1128	struct mxuport_port *mxport;
1129	int err;
1130
1131	mxport = devm_kzalloc(&port->dev, sizeof(struct mxuport_port),
1132			      GFP_KERNEL);
1133	if (!mxport)
1134		return -ENOMEM;
1135
1136	mutex_init(&mxport->mutex);
1137	spin_lock_init(&mxport->spinlock);
1138
1139	/* Set the port private data */
1140	usb_set_serial_port_data(port, mxport);
1141
1142	/* Set FIFO (Enable) */
1143	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_FIFO_DISABLE,
1144				    0, port->port_number);
1145	if (err)
1146		return err;
1147
1148	/* Set transmission mode (Hi-Performance) */
1149	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_HIGH_PERFOR,
1150				    0, port->port_number);
1151	if (err)
1152		return err;
1153
1154	/* Set interface (RS-232) */
1155	return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE,
1156				     MX_INT_RS232,
1157				     port->port_number);
 
 
 
 
1158}
1159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1160static int mxuport_attach(struct usb_serial *serial)
1161{
1162	struct usb_serial_port *port0 = serial->port[0];
1163	struct usb_serial_port *port1 = serial->port[1];
 
1164	int err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1165
1166	/*
1167	 * All data from the ports is received on the first bulk in
1168	 * endpoint, with a multiplex header. The second bulk in is
1169	 * used for events.
1170	 *
1171	 * Start to read from the device.
1172	 */
1173	err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL);
1174	if (err)
1175		return err;
1176
1177	err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL);
1178	if (err) {
1179		usb_serial_generic_close(port0);
1180		return err;
1181	}
1182
1183	return 0;
1184}
1185
1186static void mxuport_release(struct usb_serial *serial)
1187{
1188	struct usb_serial_port *port0 = serial->port[0];
1189	struct usb_serial_port *port1 = serial->port[1];
1190
1191	usb_serial_generic_close(port1);
1192	usb_serial_generic_close(port0);
1193}
1194
1195static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port)
1196{
1197	struct mxuport_port *mxport = usb_get_serial_port_data(port);
1198	struct usb_serial *serial = port->serial;
1199	int err;
1200
1201	/* Set receive host (enable) */
1202	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1203				    1, port->port_number);
1204	if (err)
1205		return err;
1206
1207	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN,
1208				    1, port->port_number);
1209	if (err) {
1210		mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1211				      0, port->port_number);
1212		return err;
1213	}
1214
1215	/* Initial port termios */
1216	if (tty)
1217		mxuport_set_termios(tty, port, NULL);
1218
1219	/*
1220	 * TODO: use RQ_VENDOR_GET_MSR, once we know what it
1221	 * returns.
1222	 */
1223	mxport->msr_state = 0;
1224
1225	return err;
1226}
1227
1228static void mxuport_close(struct usb_serial_port *port)
1229{
1230	struct usb_serial *serial = port->serial;
1231
1232	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0,
1233			      port->port_number);
1234
1235	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0,
1236			      port->port_number);
1237}
1238
1239/* Send a break to the port. */
1240static void mxuport_break_ctl(struct tty_struct *tty, int break_state)
1241{
1242	struct usb_serial_port *port = tty->driver_data;
1243	struct usb_serial *serial = port->serial;
1244	int enable;
1245
1246	if (break_state == -1) {
1247		enable = 1;
1248		dev_dbg(&port->dev, "%s - sending break\n", __func__);
1249	} else {
1250		enable = 0;
1251		dev_dbg(&port->dev, "%s - clearing break\n", __func__);
1252	}
1253
1254	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK,
1255			      enable, port->port_number);
1256}
1257
1258static int mxuport_resume(struct usb_serial *serial)
1259{
1260	struct usb_serial_port *port;
1261	int c = 0;
1262	int i;
1263	int r;
1264
1265	for (i = 0; i < 2; i++) {
1266		port = serial->port[i];
1267
1268		r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO);
1269		if (r < 0)
1270			c++;
1271	}
1272
1273	for (i = 0; i < serial->num_ports; i++) {
1274		port = serial->port[i];
1275		if (!tty_port_initialized(&port->port))
1276			continue;
1277
1278		r = usb_serial_generic_write_start(port, GFP_NOIO);
1279		if (r < 0)
1280			c++;
1281	}
1282
1283	return c ? -EIO : 0;
1284}
1285
1286static struct usb_serial_driver mxuport_device = {
1287	.driver = {
1288		.owner =	THIS_MODULE,
1289		.name =		"mxuport",
1290	},
1291	.description		= "MOXA UPort",
1292	.id_table		= mxuport_idtable,
1293	.num_bulk_in		= 2,
1294	.num_bulk_out		= 1,
1295	.probe			= mxuport_probe,
1296	.port_probe		= mxuport_port_probe,
1297	.attach			= mxuport_attach,
1298	.release		= mxuport_release,
1299	.calc_num_ports		= mxuport_calc_num_ports,
1300	.open			= mxuport_open,
1301	.close			= mxuport_close,
1302	.set_termios		= mxuport_set_termios,
1303	.break_ctl		= mxuport_break_ctl,
1304	.tx_empty		= mxuport_tx_empty,
1305	.tiocmiwait		= usb_serial_generic_tiocmiwait,
1306	.get_icount		= usb_serial_generic_get_icount,
1307	.throttle		= mxuport_throttle,
1308	.unthrottle		= mxuport_unthrottle,
1309	.tiocmget		= mxuport_tiocmget,
1310	.tiocmset		= mxuport_tiocmset,
1311	.dtr_rts		= mxuport_dtr_rts,
1312	.process_read_urb	= mxuport_process_read_urb,
1313	.prepare_write_buffer	= mxuport_prepare_write_buffer,
1314	.resume			= mxuport_resume,
1315};
1316
1317static struct usb_serial_driver *const serial_drivers[] = {
1318	&mxuport_device, NULL
1319};
1320
1321module_usb_serial_driver(serial_drivers, mxuport_idtable);
1322
1323MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
1324MODULE_AUTHOR("<support@moxa.com>");
1325MODULE_LICENSE("GPL");
v3.15
 
   1/*
   2 *	mxuport.c - MOXA UPort series driver
   3 *
   4 *	Copyright (c) 2006 Moxa Technologies Co., Ltd.
   5 *	Copyright (c) 2013 Andrew Lunn <andrew@lunn.ch>
   6 *
   7 *	This program is free software; you can redistribute it and/or modify
   8 *	it under the terms of the GNU General Public License as published by
   9 *	the Free Software Foundation; either version 2 of the License, or
  10 *	(at your option) any later version.
  11 *
  12 *	Supports the following Moxa USB to serial converters:
  13 *	 2 ports : UPort 1250, UPort 1250I
  14 *	 4 ports : UPort 1410, UPort 1450, UPort 1450I
  15 *	 8 ports : UPort 1610-8, UPort 1650-8
  16 *	16 ports : UPort 1610-16, UPort 1650-16
  17 */
  18
  19#include <linux/kernel.h>
  20#include <linux/module.h>
  21#include <linux/firmware.h>
  22#include <linux/jiffies.h>
  23#include <linux/serial.h>
  24#include <linux/serial_reg.h>
  25#include <linux/slab.h>
  26#include <linux/tty.h>
  27#include <linux/tty_driver.h>
  28#include <linux/tty_flip.h>
  29#include <linux/uaccess.h>
  30#include <linux/usb.h>
  31#include <linux/usb/serial.h>
  32#include <asm/unaligned.h>
  33
  34/* Definitions for the vendor ID and device ID */
  35#define MX_USBSERIAL_VID	0x110A
  36#define MX_UPORT1250_PID	0x1250
  37#define MX_UPORT1251_PID	0x1251
  38#define MX_UPORT1410_PID	0x1410
  39#define MX_UPORT1450_PID	0x1450
  40#define MX_UPORT1451_PID	0x1451
  41#define MX_UPORT1618_PID	0x1618
  42#define MX_UPORT1658_PID	0x1658
  43#define MX_UPORT1613_PID	0x1613
  44#define MX_UPORT1653_PID	0x1653
  45
  46/* Definitions for USB info */
  47#define HEADER_SIZE		4
  48#define EVENT_LENGTH		8
  49#define DOWN_BLOCK_SIZE		64
  50
  51/* Definitions for firmware info */
  52#define VER_ADDR_1		0x20
  53#define VER_ADDR_2		0x24
  54#define VER_ADDR_3		0x28
  55
  56/* Definitions for USB vendor request */
  57#define RQ_VENDOR_NONE			0x00
  58#define RQ_VENDOR_SET_BAUD		0x01 /* Set baud rate */
  59#define RQ_VENDOR_SET_LINE		0x02 /* Set line status */
  60#define RQ_VENDOR_SET_CHARS		0x03 /* Set Xon/Xoff chars */
  61#define RQ_VENDOR_SET_RTS		0x04 /* Set RTS */
  62#define RQ_VENDOR_SET_DTR		0x05 /* Set DTR */
  63#define RQ_VENDOR_SET_XONXOFF		0x06 /* Set auto Xon/Xoff */
  64#define RQ_VENDOR_SET_RX_HOST_EN	0x07 /* Set RX host enable */
  65#define RQ_VENDOR_SET_OPEN		0x08 /* Set open/close port */
  66#define RQ_VENDOR_PURGE			0x09 /* Purge Rx/Tx buffer */
  67#define RQ_VENDOR_SET_MCR		0x0A /* Set MCR register */
  68#define RQ_VENDOR_SET_BREAK		0x0B /* Set Break signal */
  69
  70#define RQ_VENDOR_START_FW_DOWN		0x0C /* Start firmware download */
  71#define RQ_VENDOR_STOP_FW_DOWN		0x0D /* Stop firmware download */
  72#define RQ_VENDOR_QUERY_FW_READY	0x0E /* Query if new firmware ready */
  73
  74#define RQ_VENDOR_SET_FIFO_DISABLE	0x0F /* Set fifo disable */
  75#define RQ_VENDOR_SET_INTERFACE		0x10 /* Set interface */
  76#define RQ_VENDOR_SET_HIGH_PERFOR	0x11 /* Set hi-performance */
  77
  78#define RQ_VENDOR_ERASE_BLOCK		0x12 /* Erase flash block */
  79#define RQ_VENDOR_WRITE_PAGE		0x13 /* Write flash page */
  80#define RQ_VENDOR_PREPARE_WRITE		0x14 /* Prepare write flash */
  81#define RQ_VENDOR_CONFIRM_WRITE		0x15 /* Confirm write flash */
  82#define RQ_VENDOR_LOCATE		0x16 /* Locate the device */
  83
  84#define RQ_VENDOR_START_ROM_DOWN	0x17 /* Start firmware download */
  85#define RQ_VENDOR_ROM_DATA		0x18 /* Rom file data */
  86#define RQ_VENDOR_STOP_ROM_DOWN		0x19 /* Stop firmware download */
  87#define RQ_VENDOR_FW_DATA		0x20 /* Firmware data */
  88
  89#define RQ_VENDOR_RESET_DEVICE		0x23 /* Try to reset the device */
  90#define RQ_VENDOR_QUERY_FW_CONFIG	0x24
  91
  92#define RQ_VENDOR_GET_VERSION		0x81 /* Get firmware version */
  93#define RQ_VENDOR_GET_PAGE		0x82 /* Read flash page */
  94#define RQ_VENDOR_GET_ROM_PROC		0x83 /* Get ROM process state */
  95
  96#define RQ_VENDOR_GET_INQUEUE		0x84 /* Data in input buffer */
  97#define RQ_VENDOR_GET_OUTQUEUE		0x85 /* Data in output buffer */
  98
  99#define RQ_VENDOR_GET_MSR		0x86 /* Get modem status register */
 100
 101/* Definitions for UPort event type */
 102#define UPORT_EVENT_NONE		0 /* None */
 103#define UPORT_EVENT_TXBUF_THRESHOLD	1 /* Tx buffer threshold */
 104#define UPORT_EVENT_SEND_NEXT		2 /* Send next */
 105#define UPORT_EVENT_MSR			3 /* Modem status */
 106#define UPORT_EVENT_LSR			4 /* Line status */
 107#define UPORT_EVENT_MCR			5 /* Modem control */
 108
 109/* Definitions for serial event type */
 110#define SERIAL_EV_CTS			0x0008	/* CTS changed state */
 111#define SERIAL_EV_DSR			0x0010	/* DSR changed state */
 112#define SERIAL_EV_RLSD			0x0020	/* RLSD changed state */
 113
 114/* Definitions for modem control event type */
 115#define SERIAL_EV_XOFF			0x40	/* XOFF received */
 116
 117/* Definitions for line control of communication */
 118#define MX_WORDLENGTH_5			5
 119#define MX_WORDLENGTH_6			6
 120#define MX_WORDLENGTH_7			7
 121#define MX_WORDLENGTH_8			8
 122
 123#define MX_PARITY_NONE			0
 124#define MX_PARITY_ODD			1
 125#define MX_PARITY_EVEN			2
 126#define MX_PARITY_MARK			3
 127#define MX_PARITY_SPACE			4
 128
 129#define MX_STOP_BITS_1			0
 130#define MX_STOP_BITS_1_5		1
 131#define MX_STOP_BITS_2			2
 132
 133#define MX_RTS_DISABLE			0x0
 134#define MX_RTS_ENABLE			0x1
 135#define MX_RTS_HW			0x2
 136#define MX_RTS_NO_CHANGE		0x3 /* Flag, not valid register value*/
 137
 138#define MX_INT_RS232			0
 139#define MX_INT_2W_RS485			1
 140#define MX_INT_RS422			2
 141#define MX_INT_4W_RS485			3
 142
 143/* Definitions for holding reason */
 144#define MX_WAIT_FOR_CTS			0x0001
 145#define MX_WAIT_FOR_DSR			0x0002
 146#define MX_WAIT_FOR_DCD			0x0004
 147#define MX_WAIT_FOR_XON			0x0008
 148#define MX_WAIT_FOR_START_TX		0x0010
 149#define MX_WAIT_FOR_UNTHROTTLE		0x0020
 150#define MX_WAIT_FOR_LOW_WATER		0x0040
 151#define MX_WAIT_FOR_SEND_NEXT		0x0080
 152
 153#define MX_UPORT_2_PORT			BIT(0)
 154#define MX_UPORT_4_PORT			BIT(1)
 155#define MX_UPORT_8_PORT			BIT(2)
 156#define MX_UPORT_16_PORT		BIT(3)
 157
 158/* This structure holds all of the local port information */
 159struct mxuport_port {
 160	u8 mcr_state;		/* Last MCR state */
 161	u8 msr_state;		/* Last MSR state */
 162	struct mutex mutex;	/* Protects mcr_state */
 163	spinlock_t spinlock;	/* Protects msr_state */
 164};
 165
 166/* Table of devices that work with this driver */
 167static const struct usb_device_id mxuport_idtable[] = {
 168	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1250_PID),
 169	  .driver_info = MX_UPORT_2_PORT },
 170	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1251_PID),
 171	  .driver_info = MX_UPORT_2_PORT },
 172	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1410_PID),
 173	  .driver_info = MX_UPORT_4_PORT },
 174	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1450_PID),
 175	  .driver_info = MX_UPORT_4_PORT },
 176	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1451_PID),
 177	  .driver_info = MX_UPORT_4_PORT },
 178	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1618_PID),
 179	  .driver_info = MX_UPORT_8_PORT },
 180	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1658_PID),
 181	  .driver_info = MX_UPORT_8_PORT },
 182	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1613_PID),
 183	  .driver_info = MX_UPORT_16_PORT },
 184	{ USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1653_PID),
 185	  .driver_info = MX_UPORT_16_PORT },
 186	{}			/* Terminating entry */
 187};
 188
 189MODULE_DEVICE_TABLE(usb, mxuport_idtable);
 190
 191/*
 192 * Add a four byte header containing the port number and the number of
 193 * bytes of data in the message. Return the number of bytes in the
 194 * buffer.
 195 */
 196static int mxuport_prepare_write_buffer(struct usb_serial_port *port,
 197					void *dest, size_t size)
 198{
 199	u8 *buf = dest;
 200	int count;
 201
 202	count = kfifo_out_locked(&port->write_fifo, buf + HEADER_SIZE,
 203				 size - HEADER_SIZE,
 204				 &port->lock);
 205
 206	put_unaligned_be16(port->port_number, buf);
 207	put_unaligned_be16(count, buf + 2);
 208
 209	dev_dbg(&port->dev, "%s - size %zd count %d\n", __func__,
 210		size, count);
 211
 212	return count + HEADER_SIZE;
 213}
 214
 215/* Read the given buffer in from the control pipe. */
 216static int mxuport_recv_ctrl_urb(struct usb_serial *serial,
 217				 u8 request, u16 value, u16 index,
 218				 u8 *data, size_t size)
 219{
 220	int status;
 221
 222	status = usb_control_msg(serial->dev,
 223				 usb_rcvctrlpipe(serial->dev, 0),
 224				 request,
 225				 (USB_DIR_IN | USB_TYPE_VENDOR |
 226				  USB_RECIP_DEVICE), value, index,
 227				 data, size,
 228				 USB_CTRL_GET_TIMEOUT);
 229	if (status < 0) {
 230		dev_err(&serial->interface->dev,
 231			"%s - usb_control_msg failed (%d)\n",
 232			__func__, status);
 233		return status;
 234	}
 235
 236	if (status != size) {
 237		dev_err(&serial->interface->dev,
 238			"%s - short read (%d / %zd)\n",
 239			__func__, status, size);
 240		return -EIO;
 241	}
 242
 243	return status;
 244}
 245
 246/* Write the given buffer out to the control pipe.  */
 247static int mxuport_send_ctrl_data_urb(struct usb_serial *serial,
 248				      u8 request,
 249				      u16 value, u16 index,
 250				      u8 *data, size_t size)
 251{
 252	int status;
 253
 254	status = usb_control_msg(serial->dev,
 255				 usb_sndctrlpipe(serial->dev, 0),
 256				 request,
 257				 (USB_DIR_OUT | USB_TYPE_VENDOR |
 258				  USB_RECIP_DEVICE), value, index,
 259				 data, size,
 260				 USB_CTRL_SET_TIMEOUT);
 261	if (status < 0) {
 262		dev_err(&serial->interface->dev,
 263			"%s - usb_control_msg failed (%d)\n",
 264			__func__, status);
 265		return status;
 266	}
 267
 268	if (status != size) {
 269		dev_err(&serial->interface->dev,
 270			"%s - short write (%d / %zd)\n",
 271			__func__, status, size);
 272		return -EIO;
 273	}
 274
 275	return 0;
 276}
 277
 278/* Send a vendor request without any data */
 279static int mxuport_send_ctrl_urb(struct usb_serial *serial,
 280				 u8 request, u16 value, u16 index)
 281{
 282	return mxuport_send_ctrl_data_urb(serial, request, value, index,
 283					  NULL, 0);
 284}
 285
 286/*
 287 * mxuport_throttle - throttle function of driver
 288 *
 289 * This function is called by the tty driver when it wants to stop the
 290 * data being read from the port. Since all the data comes over one
 291 * bulk in endpoint, we cannot stop submitting urbs by setting
 292 * port->throttle. Instead tell the device to stop sending us data for
 293 * the port.
 294 */
 295static void mxuport_throttle(struct tty_struct *tty)
 296{
 297	struct usb_serial_port *port = tty->driver_data;
 298	struct usb_serial *serial = port->serial;
 299
 300	dev_dbg(&port->dev, "%s\n", __func__);
 301
 302	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
 303			      0, port->port_number);
 304}
 305
 306/*
 307 * mxuport_unthrottle - unthrottle function of driver
 308 *
 309 * This function is called by the tty driver when it wants to resume
 310 * the data being read from the port. Tell the device it can resume
 311 * sending us received data from the port.
 312 */
 313static void mxuport_unthrottle(struct tty_struct *tty)
 314{
 315
 316	struct usb_serial_port *port = tty->driver_data;
 317	struct usb_serial *serial = port->serial;
 318
 319	dev_dbg(&port->dev, "%s\n", __func__);
 320
 321	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
 322			      1, port->port_number);
 323}
 324
 325/*
 326 * Processes one chunk of data received for a port.  Mostly a copy of
 327 * usb_serial_generic_process_read_urb().
 328 */
 329static void mxuport_process_read_urb_data(struct usb_serial_port *port,
 330					  char *data, int size)
 331{
 332	int i;
 333
 334	if (!port->port.console || !port->sysrq) {
 335		tty_insert_flip_string(&port->port, data, size);
 336	} else {
 337		for (i = 0; i < size; i++, data++) {
 338			if (!usb_serial_handle_sysrq_char(port, *data))
 339				tty_insert_flip_char(&port->port, *data,
 340						     TTY_NORMAL);
 341		}
 
 
 342	}
 343	tty_flip_buffer_push(&port->port);
 344}
 345
 346static void mxuport_msr_event(struct usb_serial_port *port, u8 buf[4])
 347{
 348	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 349	u8 rcv_msr_hold = buf[2] & 0xF0;
 350	u16 rcv_msr_event = get_unaligned_be16(buf);
 351	unsigned long flags;
 352
 353	if (rcv_msr_event == 0)
 354		return;
 355
 356	/* Update MSR status */
 357	spin_lock_irqsave(&mxport->spinlock, flags);
 358
 359	dev_dbg(&port->dev, "%s - current MSR status = 0x%x\n",
 360		__func__, mxport->msr_state);
 361
 362	if (rcv_msr_hold & UART_MSR_CTS) {
 363		mxport->msr_state |= UART_MSR_CTS;
 364		dev_dbg(&port->dev, "%s - CTS high\n", __func__);
 365	} else {
 366		mxport->msr_state &= ~UART_MSR_CTS;
 367		dev_dbg(&port->dev, "%s - CTS low\n", __func__);
 368	}
 369
 370	if (rcv_msr_hold & UART_MSR_DSR) {
 371		mxport->msr_state |= UART_MSR_DSR;
 372		dev_dbg(&port->dev, "%s - DSR high\n", __func__);
 373	} else {
 374		mxport->msr_state &= ~UART_MSR_DSR;
 375		dev_dbg(&port->dev, "%s - DSR low\n", __func__);
 376	}
 377
 378	if (rcv_msr_hold & UART_MSR_DCD) {
 379		mxport->msr_state |= UART_MSR_DCD;
 380		dev_dbg(&port->dev, "%s - DCD high\n", __func__);
 381	} else {
 382		mxport->msr_state &= ~UART_MSR_DCD;
 383		dev_dbg(&port->dev, "%s - DCD low\n", __func__);
 384	}
 385	spin_unlock_irqrestore(&mxport->spinlock, flags);
 386
 387	if (rcv_msr_event &
 388	    (SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RLSD)) {
 389
 390		if (rcv_msr_event & SERIAL_EV_CTS) {
 391			port->icount.cts++;
 392			dev_dbg(&port->dev, "%s - CTS change\n", __func__);
 393		}
 394
 395		if (rcv_msr_event & SERIAL_EV_DSR) {
 396			port->icount.dsr++;
 397			dev_dbg(&port->dev, "%s - DSR change\n", __func__);
 398		}
 399
 400		if (rcv_msr_event & SERIAL_EV_RLSD) {
 401			port->icount.dcd++;
 402			dev_dbg(&port->dev, "%s - DCD change\n", __func__);
 403		}
 404		wake_up_interruptible(&port->port.delta_msr_wait);
 405	}
 406}
 407
 408static void mxuport_lsr_event(struct usb_serial_port *port, u8 buf[4])
 409{
 410	u8 lsr_event = buf[2];
 411
 412	if (lsr_event & UART_LSR_BI) {
 413		port->icount.brk++;
 414		dev_dbg(&port->dev, "%s - break error\n", __func__);
 415	}
 416
 417	if (lsr_event & UART_LSR_FE) {
 418		port->icount.frame++;
 419		dev_dbg(&port->dev, "%s - frame error\n", __func__);
 420	}
 421
 422	if (lsr_event & UART_LSR_PE) {
 423		port->icount.parity++;
 424		dev_dbg(&port->dev, "%s - parity error\n", __func__);
 425	}
 426
 427	if (lsr_event & UART_LSR_OE) {
 428		port->icount.overrun++;
 429		dev_dbg(&port->dev, "%s - overrun error\n", __func__);
 430	}
 431}
 432
 433/*
 434 * When something interesting happens, modem control lines XON/XOFF
 435 * etc, the device sends an event. Process these events.
 436 */
 437static void mxuport_process_read_urb_event(struct usb_serial_port *port,
 438					   u8 buf[4], u32 event)
 439{
 440	dev_dbg(&port->dev, "%s - receive event : %04x\n", __func__, event);
 441
 442	switch (event) {
 443	case UPORT_EVENT_SEND_NEXT:
 444		/*
 445		 * Sent as part of the flow control on device buffers.
 446		 * Not currently used.
 447		 */
 448		break;
 449	case UPORT_EVENT_MSR:
 450		mxuport_msr_event(port, buf);
 451		break;
 452	case UPORT_EVENT_LSR:
 453		mxuport_lsr_event(port, buf);
 454		break;
 455	case UPORT_EVENT_MCR:
 456		/*
 457		 * Event to indicate a change in XON/XOFF from the
 458		 * peer.  Currently not used. We just continue
 459		 * sending the device data and it will buffer it if
 460		 * needed. This event could be used for flow control
 461		 * between the host and the device.
 462		 */
 463		break;
 464	default:
 465		dev_dbg(&port->dev, "Unexpected event\n");
 466		break;
 467	}
 468}
 469
 470/*
 471 * One URB can contain data for multiple ports. Demultiplex the data,
 472 * checking the port exists, is opened and the message is valid.
 473 */
 474static void mxuport_process_read_urb_demux_data(struct urb *urb)
 475{
 476	struct usb_serial_port *port = urb->context;
 477	struct usb_serial *serial = port->serial;
 478	u8 *data = urb->transfer_buffer;
 479	u8 *end = data + urb->actual_length;
 480	struct usb_serial_port *demux_port;
 481	u8 *ch;
 482	u16 rcv_port;
 483	u16 rcv_len;
 484
 485	while (data < end) {
 486		if (data + HEADER_SIZE > end) {
 487			dev_warn(&port->dev, "%s - message with short header\n",
 488				 __func__);
 489			return;
 490		}
 491
 492		rcv_port = get_unaligned_be16(data);
 493		if (rcv_port >= serial->num_ports) {
 494			dev_warn(&port->dev, "%s - message for invalid port\n",
 495				 __func__);
 496			return;
 497		}
 498
 499		demux_port = serial->port[rcv_port];
 500		rcv_len = get_unaligned_be16(data + 2);
 501		if (!rcv_len || data + HEADER_SIZE + rcv_len > end) {
 502			dev_warn(&port->dev, "%s - short data\n", __func__);
 503			return;
 504		}
 505
 506		if (test_bit(ASYNCB_INITIALIZED, &demux_port->port.flags)) {
 507			ch = data + HEADER_SIZE;
 508			mxuport_process_read_urb_data(demux_port, ch, rcv_len);
 509		} else {
 510			dev_dbg(&demux_port->dev, "%s - data for closed port\n",
 511				__func__);
 512		}
 513		data += HEADER_SIZE + rcv_len;
 514	}
 515}
 516
 517/*
 518 * One URB can contain events for multiple ports. Demultiplex the event,
 519 * checking the port exists, and is opened.
 520 */
 521static void mxuport_process_read_urb_demux_event(struct urb *urb)
 522{
 523	struct usb_serial_port *port = urb->context;
 524	struct usb_serial *serial = port->serial;
 525	u8 *data = urb->transfer_buffer;
 526	u8 *end = data + urb->actual_length;
 527	struct usb_serial_port *demux_port;
 528	u8 *ch;
 529	u16 rcv_port;
 530	u16 rcv_event;
 531
 532	while (data < end) {
 533		if (data + EVENT_LENGTH > end) {
 534			dev_warn(&port->dev, "%s - message with short event\n",
 535				 __func__);
 536			return;
 537		}
 538
 539		rcv_port = get_unaligned_be16(data);
 540		if (rcv_port >= serial->num_ports) {
 541			dev_warn(&port->dev, "%s - message for invalid port\n",
 542				 __func__);
 543			return;
 544		}
 545
 546		demux_port = serial->port[rcv_port];
 547		if (test_bit(ASYNCB_INITIALIZED, &demux_port->port.flags)) {
 548			ch = data + HEADER_SIZE;
 549			rcv_event = get_unaligned_be16(data + 2);
 550			mxuport_process_read_urb_event(demux_port, ch,
 551						       rcv_event);
 552		} else {
 553			dev_dbg(&demux_port->dev,
 554				"%s - event for closed port\n", __func__);
 555		}
 556		data += EVENT_LENGTH;
 557	}
 558}
 559
 560/*
 561 * This is called when we have received data on the bulk in
 562 * endpoint. Depending on which port it was received on, it can
 563 * contain serial data or events.
 564 */
 565static void mxuport_process_read_urb(struct urb *urb)
 566{
 567	struct usb_serial_port *port = urb->context;
 568	struct usb_serial *serial = port->serial;
 569
 570	if (port == serial->port[0])
 571		mxuport_process_read_urb_demux_data(urb);
 572
 573	if (port == serial->port[1])
 574		mxuport_process_read_urb_demux_event(urb);
 575}
 576
 577/*
 578 * Ask the device how many bytes it has queued to be sent out. If
 579 * there are none, return true.
 580 */
 581static bool mxuport_tx_empty(struct usb_serial_port *port)
 582{
 583	struct usb_serial *serial = port->serial;
 584	bool is_empty = true;
 585	u32 txlen;
 586	u8 *len_buf;
 587	int err;
 588
 589	len_buf = kzalloc(4, GFP_KERNEL);
 590	if (!len_buf)
 591		goto out;
 592
 593	err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_OUTQUEUE, 0,
 594				    port->port_number, len_buf, 4);
 595	if (err < 0)
 596		goto out;
 597
 598	txlen = get_unaligned_be32(len_buf);
 599	dev_dbg(&port->dev, "%s - tx len = %u\n", __func__, txlen);
 600
 601	if (txlen != 0)
 602		is_empty = false;
 603
 604out:
 605	kfree(len_buf);
 606	return is_empty;
 607}
 608
 609static int mxuport_set_mcr(struct usb_serial_port *port, u8 mcr_state)
 610{
 611	struct usb_serial *serial = port->serial;
 612	int err;
 613
 614	dev_dbg(&port->dev, "%s - %02x\n", __func__, mcr_state);
 615
 616	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_MCR,
 617				    mcr_state, port->port_number);
 618	if (err)
 619		dev_err(&port->dev, "%s - failed to change MCR\n", __func__);
 620
 621	return err;
 622}
 623
 624static int mxuport_set_dtr(struct usb_serial_port *port, int on)
 625{
 626	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 627	struct usb_serial *serial = port->serial;
 628	int err;
 629
 630	mutex_lock(&mxport->mutex);
 631
 632	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_DTR,
 633				    !!on, port->port_number);
 634	if (!err) {
 635		if (on)
 636			mxport->mcr_state |= UART_MCR_DTR;
 637		else
 638			mxport->mcr_state &= ~UART_MCR_DTR;
 639	}
 640
 641	mutex_unlock(&mxport->mutex);
 642
 643	return err;
 644}
 645
 646static int mxuport_set_rts(struct usb_serial_port *port, u8 state)
 647{
 648	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 649	struct usb_serial *serial = port->serial;
 650	int err;
 651	u8 mcr_state;
 652
 653	mutex_lock(&mxport->mutex);
 654	mcr_state = mxport->mcr_state;
 655
 656	switch (state) {
 657	case MX_RTS_DISABLE:
 658		mcr_state &= ~UART_MCR_RTS;
 659		break;
 660	case MX_RTS_ENABLE:
 661		mcr_state |= UART_MCR_RTS;
 662		break;
 663	case MX_RTS_HW:
 664		/*
 665		 * Do not update mxport->mcr_state when doing hardware
 666		 * flow control.
 667		 */
 668		break;
 669	default:
 670		/*
 671		 * Should not happen, but somebody might try passing
 672		 * MX_RTS_NO_CHANGE, which is not valid.
 673		 */
 674		err = -EINVAL;
 675		goto out;
 676	}
 677	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RTS,
 678				    state, port->port_number);
 679	if (!err)
 680		mxport->mcr_state = mcr_state;
 681
 682out:
 683	mutex_unlock(&mxport->mutex);
 684
 685	return err;
 686}
 687
 688static void mxuport_dtr_rts(struct usb_serial_port *port, int on)
 689{
 690	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 691	u8 mcr_state;
 692	int err;
 693
 694	mutex_lock(&mxport->mutex);
 695	mcr_state = mxport->mcr_state;
 696
 697	if (on)
 698		mcr_state |= (UART_MCR_RTS | UART_MCR_DTR);
 699	else
 700		mcr_state &= ~(UART_MCR_RTS | UART_MCR_DTR);
 701
 702	err = mxuport_set_mcr(port, mcr_state);
 703	if (!err)
 704		mxport->mcr_state = mcr_state;
 705
 706	mutex_unlock(&mxport->mutex);
 707}
 708
 709static int mxuport_tiocmset(struct tty_struct *tty, unsigned int set,
 710			    unsigned int clear)
 711{
 712	struct usb_serial_port *port = tty->driver_data;
 713	struct mxuport_port *mxport = usb_get_serial_port_data(port);
 714	int err;
 715	u8 mcr_state;
 716
 717	mutex_lock(&mxport->mutex);
 718	mcr_state = mxport->mcr_state;
 719
 720	if (set & TIOCM_RTS)
 721		mcr_state |= UART_MCR_RTS;
 722
 723	if (set & TIOCM_DTR)
 724		mcr_state |= UART_MCR_DTR;
 725
 726	if (clear & TIOCM_RTS)
 727		mcr_state &= ~UART_MCR_RTS;
 728
 729	if (clear & TIOCM_DTR)
 730		mcr_state &= ~UART_MCR_DTR;
 731
 732	err = mxuport_set_mcr(port, mcr_state);
 733	if (!err)
 734		mxport->mcr_state = mcr_state;
 735
 736	mutex_unlock(&mxport->mutex);
 737
 738	return err;
 739}
 740
 741static int mxuport_tiocmget(struct tty_struct *tty)
 742{
 743	struct mxuport_port *mxport;
 744	struct usb_serial_port *port = tty->driver_data;
 745	unsigned int result;
 746	unsigned long flags;
 747	unsigned int msr;
 748	unsigned int mcr;
 749
 750	mxport = usb_get_serial_port_data(port);
 751
 752	mutex_lock(&mxport->mutex);
 753	spin_lock_irqsave(&mxport->spinlock, flags);
 754
 755	msr = mxport->msr_state;
 756	mcr = mxport->mcr_state;
 757
 758	spin_unlock_irqrestore(&mxport->spinlock, flags);
 759	mutex_unlock(&mxport->mutex);
 760
 761	result = (((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) |	/* 0x002 */
 762		  ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) |	/* 0x004 */
 763		  ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) |	/* 0x020 */
 764		  ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) |	/* 0x040 */
 765		  ((msr & UART_MSR_RI) ? TIOCM_RI : 0) |	/* 0x080 */
 766		  ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0));	/* 0x100 */
 767
 768	dev_dbg(&port->dev, "%s - 0x%04x\n", __func__, result);
 769
 770	return result;
 771}
 772
 773static int mxuport_set_termios_flow(struct tty_struct *tty,
 774				    struct ktermios *old_termios,
 775				    struct usb_serial_port *port,
 776				    struct usb_serial *serial)
 777{
 778	u8 xon = START_CHAR(tty);
 779	u8 xoff = STOP_CHAR(tty);
 780	int enable;
 781	int err;
 782	u8 *buf;
 783	u8 rts;
 784
 785	buf = kmalloc(2, GFP_KERNEL);
 786	if (!buf)
 787		return -ENOMEM;
 788
 789	/* S/W flow control settings */
 790	if (I_IXOFF(tty) || I_IXON(tty)) {
 791		enable = 1;
 792		buf[0] = xon;
 793		buf[1] = xoff;
 794
 795		err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_CHARS,
 796						 0, port->port_number,
 797						 buf, 2);
 798		if (err)
 799			goto out;
 800
 801		dev_dbg(&port->dev, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
 802			__func__, xon, xoff);
 803	} else {
 804		enable = 0;
 805	}
 806
 807	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_XONXOFF,
 808				    enable, port->port_number);
 809	if (err)
 810		goto out;
 811
 812	rts = MX_RTS_NO_CHANGE;
 813
 814	/* H/W flow control settings */
 815	if (!old_termios ||
 816	    C_CRTSCTS(tty) != (old_termios->c_cflag & CRTSCTS)) {
 817		if (C_CRTSCTS(tty))
 818			rts = MX_RTS_HW;
 819		else
 820			rts = MX_RTS_ENABLE;
 821	}
 822
 823	if (C_BAUD(tty)) {
 824		if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
 825			/* Raise DTR and RTS */
 826			if (C_CRTSCTS(tty))
 827				rts = MX_RTS_HW;
 828			else
 829				rts = MX_RTS_ENABLE;
 830			mxuport_set_dtr(port, 1);
 831		}
 832	} else {
 833		/* Drop DTR and RTS */
 834		rts = MX_RTS_DISABLE;
 835		mxuport_set_dtr(port, 0);
 836	}
 837
 838	if (rts != MX_RTS_NO_CHANGE)
 839		err = mxuport_set_rts(port, rts);
 840
 841out:
 842	kfree(buf);
 843	return err;
 844}
 845
 846static void mxuport_set_termios(struct tty_struct *tty,
 847				struct usb_serial_port *port,
 848				struct ktermios *old_termios)
 849{
 850	struct usb_serial *serial = port->serial;
 851	u8 *buf;
 852	u8 data_bits;
 853	u8 stop_bits;
 854	u8 parity;
 855	int baud;
 856	int err;
 857
 858	if (old_termios &&
 859	    !tty_termios_hw_change(&tty->termios, old_termios) &&
 860	    tty->termios.c_iflag == old_termios->c_iflag) {
 861		dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
 862		return;
 863	}
 864
 865	buf = kmalloc(4, GFP_KERNEL);
 866	if (!buf)
 867		return;
 868
 869	/* Set data bit of termios */
 870	switch (C_CSIZE(tty)) {
 871	case CS5:
 872		data_bits = MX_WORDLENGTH_5;
 873		break;
 874	case CS6:
 875		data_bits = MX_WORDLENGTH_6;
 876		break;
 877	case CS7:
 878		data_bits = MX_WORDLENGTH_7;
 879		break;
 880	case CS8:
 881	default:
 882		data_bits = MX_WORDLENGTH_8;
 883		break;
 884	}
 885
 886	/* Set parity of termios */
 887	if (C_PARENB(tty)) {
 888		if (C_CMSPAR(tty)) {
 889			if (C_PARODD(tty))
 890				parity = MX_PARITY_MARK;
 891			else
 892				parity = MX_PARITY_SPACE;
 893		} else {
 894			if (C_PARODD(tty))
 895				parity = MX_PARITY_ODD;
 896			else
 897				parity = MX_PARITY_EVEN;
 898		}
 899	} else {
 900		parity = MX_PARITY_NONE;
 901	}
 902
 903	/* Set stop bit of termios */
 904	if (C_CSTOPB(tty))
 905		stop_bits = MX_STOP_BITS_2;
 906	else
 907		stop_bits = MX_STOP_BITS_1;
 908
 909	buf[0] = data_bits;
 910	buf[1] = parity;
 911	buf[2] = stop_bits;
 912	buf[3] = 0;
 913
 914	err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_LINE,
 915					 0, port->port_number, buf, 4);
 916	if (err)
 917		goto out;
 918
 919	err = mxuport_set_termios_flow(tty, old_termios, port, serial);
 920	if (err)
 921		goto out;
 922
 923	baud = tty_get_baud_rate(tty);
 924	if (!baud)
 925		baud = 9600;
 926
 927	/* Note: Little Endian */
 928	put_unaligned_le32(baud, buf);
 929
 930	err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_BAUD,
 931					 0, port->port_number,
 932					 buf, 4);
 933	if (err)
 934		goto out;
 935
 936	dev_dbg(&port->dev, "baud_rate	: %d\n", baud);
 937	dev_dbg(&port->dev, "data_bits	: %d\n", data_bits);
 938	dev_dbg(&port->dev, "parity	: %d\n", parity);
 939	dev_dbg(&port->dev, "stop_bits	: %d\n", stop_bits);
 940
 941out:
 942	kfree(buf);
 943}
 944
 945/*
 946 * Determine how many ports this device has dynamically.  It will be
 947 * called after the probe() callback is called, but before attach().
 948 */
 949static int mxuport_calc_num_ports(struct usb_serial *serial)
 
 950{
 951	unsigned long features = (unsigned long)usb_get_serial_data(serial);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 952
 953	if (features & MX_UPORT_2_PORT)
 954		return 2;
 955	if (features & MX_UPORT_4_PORT)
 956		return 4;
 957	if (features & MX_UPORT_8_PORT)
 958		return 8;
 959	if (features & MX_UPORT_16_PORT)
 960		return 16;
 961
 962	return 0;
 
 
 963}
 964
 965/* Get the version of the firmware currently running. */
 966static int mxuport_get_fw_version(struct usb_serial *serial, u32 *version)
 967{
 968	u8 *ver_buf;
 969	int err;
 970
 971	ver_buf = kzalloc(4, GFP_KERNEL);
 972	if (!ver_buf)
 973		return -ENOMEM;
 974
 975	/* Get firmware version from SDRAM */
 976	err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_VERSION, 0, 0,
 977				    ver_buf, 4);
 978	if (err != 4) {
 979		err = -EIO;
 980		goto out;
 981	}
 982
 983	*version = (ver_buf[0] << 16) | (ver_buf[1] << 8) | ver_buf[2];
 984	err = 0;
 985out:
 986	kfree(ver_buf);
 987	return err;
 988}
 989
 990/* Given a firmware blob, download it to the device. */
 991static int mxuport_download_fw(struct usb_serial *serial,
 992			       const struct firmware *fw_p)
 993{
 994	u8 *fw_buf;
 995	size_t txlen;
 996	size_t fwidx;
 997	int err;
 998
 999	fw_buf = kmalloc(DOWN_BLOCK_SIZE, GFP_KERNEL);
1000	if (!fw_buf)
1001		return -ENOMEM;
1002
1003	dev_dbg(&serial->interface->dev, "Starting firmware download...\n");
1004	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_START_FW_DOWN, 0, 0);
1005	if (err)
1006		goto out;
1007
1008	fwidx = 0;
1009	do {
1010		txlen = min_t(size_t, (fw_p->size - fwidx), DOWN_BLOCK_SIZE);
1011
1012		memcpy(fw_buf, &fw_p->data[fwidx], txlen);
1013		err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_FW_DATA,
1014						 0, 0, fw_buf, txlen);
1015		if (err) {
1016			mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN,
1017					      0, 0);
1018			goto out;
1019		}
1020
1021		fwidx += txlen;
1022		usleep_range(1000, 2000);
1023
1024	} while (fwidx < fw_p->size);
1025
1026	msleep(1000);
1027	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, 0, 0);
1028	if (err)
1029		goto out;
1030
1031	msleep(1000);
1032	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_READY, 0, 0);
1033
1034out:
1035	kfree(fw_buf);
1036	return err;
1037}
1038
1039static int mxuport_probe(struct usb_serial *serial,
1040			 const struct usb_device_id *id)
1041{
1042	u16 productid = le16_to_cpu(serial->dev->descriptor.idProduct);
1043	const struct firmware *fw_p = NULL;
1044	u32 version;
1045	int local_ver;
1046	char buf[32];
1047	int err;
1048
1049	/* Load our firmware */
1050	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_CONFIG, 0, 0);
1051	if (err) {
1052		mxuport_send_ctrl_urb(serial, RQ_VENDOR_RESET_DEVICE, 0, 0);
1053		return err;
1054	}
1055
1056	err = mxuport_get_fw_version(serial, &version);
1057	if (err < 0)
1058		return err;
1059
1060	dev_dbg(&serial->interface->dev, "Device firmware version v%x.%x.%x\n",
1061		(version & 0xff0000) >> 16,
1062		(version & 0xff00) >> 8,
1063		(version & 0xff));
1064
1065	snprintf(buf, sizeof(buf) - 1, "moxa/moxa-%04x.fw", productid);
1066
1067	err = request_firmware(&fw_p, buf, &serial->interface->dev);
1068	if (err) {
1069		dev_warn(&serial->interface->dev, "Firmware %s not found\n",
1070			 buf);
1071
1072		/* Use the firmware already in the device */
1073		err = 0;
1074	} else {
1075		local_ver = ((fw_p->data[VER_ADDR_1] << 16) |
1076			     (fw_p->data[VER_ADDR_2] << 8) |
1077			     fw_p->data[VER_ADDR_3]);
1078		dev_dbg(&serial->interface->dev,
1079			"Available firmware version v%x.%x.%x\n",
1080			fw_p->data[VER_ADDR_1], fw_p->data[VER_ADDR_2],
1081			fw_p->data[VER_ADDR_3]);
1082		if (local_ver > version) {
1083			err = mxuport_download_fw(serial, fw_p);
1084			if (err)
1085				goto out;
1086			err  = mxuport_get_fw_version(serial, &version);
1087			if (err < 0)
1088				goto out;
1089		}
1090	}
1091
1092	dev_info(&serial->interface->dev,
1093		 "Using device firmware version v%x.%x.%x\n",
1094		 (version & 0xff0000) >> 16,
1095		 (version & 0xff00) >> 8,
1096		 (version & 0xff));
1097
1098	/*
1099	 * Contains the features of this hardware. Store away for
1100	 * later use, eg, number of ports.
1101	 */
1102	usb_set_serial_data(serial, (void *)id->driver_info);
1103out:
1104	if (fw_p)
1105		release_firmware(fw_p);
1106	return err;
1107}
1108
1109
1110static int mxuport_port_probe(struct usb_serial_port *port)
1111{
1112	struct usb_serial *serial = port->serial;
1113	struct mxuport_port *mxport;
1114	int err;
1115
1116	mxport = devm_kzalloc(&port->dev, sizeof(struct mxuport_port),
1117			      GFP_KERNEL);
1118	if (!mxport)
1119		return -ENOMEM;
1120
1121	mutex_init(&mxport->mutex);
1122	spin_lock_init(&mxport->spinlock);
1123
1124	/* Set the port private data */
1125	usb_set_serial_port_data(port, mxport);
1126
1127	/* Set FIFO (Enable) */
1128	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_FIFO_DISABLE,
1129				    0, port->port_number);
1130	if (err)
1131		return err;
1132
1133	/* Set transmission mode (Hi-Performance) */
1134	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_HIGH_PERFOR,
1135				    0, port->port_number);
1136	if (err)
1137		return err;
1138
1139	/* Set interface (RS-232) */
1140	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE,
1141				    MX_INT_RS232,
1142				    port->port_number);
1143	if (err)
1144		return err;
1145
1146	return 0;
1147}
1148
1149static int mxuport_alloc_write_urb(struct usb_serial *serial,
1150				   struct usb_serial_port *port,
1151				   struct usb_serial_port *port0,
1152				   int j)
1153{
1154	struct usb_device *dev = interface_to_usbdev(serial->interface);
1155
1156	set_bit(j, &port->write_urbs_free);
1157	port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
1158	if (!port->write_urbs[j])
1159		return -ENOMEM;
1160
1161	port->bulk_out_buffers[j] = kmalloc(port0->bulk_out_size, GFP_KERNEL);
1162	if (!port->bulk_out_buffers[j])
1163		return -ENOMEM;
1164
1165	usb_fill_bulk_urb(port->write_urbs[j], dev,
1166			  usb_sndbulkpipe(dev, port->bulk_out_endpointAddress),
1167			  port->bulk_out_buffers[j],
1168			  port->bulk_out_size,
1169			  serial->type->write_bulk_callback,
1170			  port);
1171	return 0;
1172}
1173
1174
1175static int mxuport_alloc_write_urbs(struct usb_serial *serial,
1176				    struct usb_serial_port *port,
1177				    struct usb_serial_port *port0)
1178{
1179	int j;
1180	int ret;
1181
1182	for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
1183		ret = mxuport_alloc_write_urb(serial, port, port0, j);
1184		if (ret)
1185			return ret;
1186	}
1187	return 0;
1188}
1189
1190
1191static int mxuport_attach(struct usb_serial *serial)
1192{
1193	struct usb_serial_port *port0 = serial->port[0];
1194	struct usb_serial_port *port1 = serial->port[1];
1195	struct usb_serial_port *port;
1196	int err;
1197	int i;
1198	int j;
1199
1200	/*
1201	 * Throw away all but the first allocated write URBs so we can
1202	 * set them up again to fit the multiplexing scheme.
1203	 */
1204	for (i = 1; i < serial->num_bulk_out; ++i) {
1205		port = serial->port[i];
1206		for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
1207			usb_free_urb(port->write_urbs[j]);
1208			kfree(port->bulk_out_buffers[j]);
1209			port->write_urbs[j] = NULL;
1210			port->bulk_out_buffers[j] = NULL;
1211		}
1212		port->write_urbs_free = 0;
1213	}
1214
1215	/*
1216	 * All write data is sent over the first bulk out endpoint,
1217	 * with an added header to indicate the port. Allocate URBs
1218	 * for each port to the first bulk out endpoint.
1219	 */
1220	for (i = 1; i < serial->num_ports; ++i) {
1221		port = serial->port[i];
1222		port->bulk_out_size = port0->bulk_out_size;
1223		port->bulk_out_endpointAddress =
1224			port0->bulk_out_endpointAddress;
1225
1226		err = mxuport_alloc_write_urbs(serial, port, port0);
1227		if (err)
1228			return err;
1229
1230		port->write_urb = port->write_urbs[0];
1231		port->bulk_out_buffer = port->bulk_out_buffers[0];
1232
1233		/*
1234		 * Ensure each port has a fifo. The framework only
1235		 * allocates a fifo to ports with a bulk out endpoint,
1236		 * where as we need one for every port.
1237		 */
1238		if (!kfifo_initialized(&port->write_fifo)) {
1239			err = kfifo_alloc(&port->write_fifo, PAGE_SIZE,
1240					  GFP_KERNEL);
1241			if (err)
1242				return err;
1243		}
1244	}
1245
1246	/*
1247	 * All data from the ports is received on the first bulk in
1248	 * endpoint, with a multiplex header. The second bulk in is
1249	 * used for events.
1250	 *
1251	 * Start to read from the device.
1252	 */
1253	err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL);
1254	if (err)
1255		return err;
1256
1257	err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL);
1258	if (err) {
1259		usb_serial_generic_close(port0);
1260		return err;
1261	}
1262
1263	return 0;
1264}
1265
 
 
 
 
 
 
 
 
 
1266static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port)
1267{
1268	struct mxuport_port *mxport = usb_get_serial_port_data(port);
1269	struct usb_serial *serial = port->serial;
1270	int err;
1271
1272	/* Set receive host (enable) */
1273	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1274				    1, port->port_number);
1275	if (err)
1276		return err;
1277
1278	err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN,
1279				    1, port->port_number);
1280	if (err) {
1281		mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1282				      0, port->port_number);
1283		return err;
1284	}
1285
1286	/* Initial port termios */
1287	mxuport_set_termios(tty, port, NULL);
 
1288
1289	/*
1290	 * TODO: use RQ_VENDOR_GET_MSR, once we know what it
1291	 * returns.
1292	 */
1293	mxport->msr_state = 0;
1294
1295	return err;
1296}
1297
1298static void mxuport_close(struct usb_serial_port *port)
1299{
1300	struct usb_serial *serial = port->serial;
1301
1302	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0,
1303			      port->port_number);
1304
1305	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0,
1306			      port->port_number);
1307}
1308
1309/* Send a break to the port. */
1310static void mxuport_break_ctl(struct tty_struct *tty, int break_state)
1311{
1312	struct usb_serial_port *port = tty->driver_data;
1313	struct usb_serial *serial = port->serial;
1314	int enable;
1315
1316	if (break_state == -1) {
1317		enable = 1;
1318		dev_dbg(&port->dev, "%s - sending break\n", __func__);
1319	} else {
1320		enable = 0;
1321		dev_dbg(&port->dev, "%s - clearing break\n", __func__);
1322	}
1323
1324	mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK,
1325			      enable, port->port_number);
1326}
1327
1328static int mxuport_resume(struct usb_serial *serial)
1329{
1330	struct usb_serial_port *port;
1331	int c = 0;
1332	int i;
1333	int r;
1334
1335	for (i = 0; i < 2; i++) {
1336		port = serial->port[i];
1337
1338		r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO);
1339		if (r < 0)
1340			c++;
1341	}
1342
1343	for (i = 0; i < serial->num_ports; i++) {
1344		port = serial->port[i];
1345		if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
1346			continue;
1347
1348		r = usb_serial_generic_write_start(port, GFP_NOIO);
1349		if (r < 0)
1350			c++;
1351	}
1352
1353	return c ? -EIO : 0;
1354}
1355
1356static struct usb_serial_driver mxuport_device = {
1357	.driver = {
1358		.owner =	THIS_MODULE,
1359		.name =		"mxuport",
1360	},
1361	.description		= "MOXA UPort",
1362	.id_table		= mxuport_idtable,
1363	.num_ports		= 0,
 
1364	.probe			= mxuport_probe,
1365	.port_probe		= mxuport_port_probe,
1366	.attach			= mxuport_attach,
 
1367	.calc_num_ports		= mxuport_calc_num_ports,
1368	.open			= mxuport_open,
1369	.close			= mxuport_close,
1370	.set_termios		= mxuport_set_termios,
1371	.break_ctl		= mxuport_break_ctl,
1372	.tx_empty		= mxuport_tx_empty,
1373	.tiocmiwait		= usb_serial_generic_tiocmiwait,
1374	.get_icount		= usb_serial_generic_get_icount,
1375	.throttle		= mxuport_throttle,
1376	.unthrottle		= mxuport_unthrottle,
1377	.tiocmget		= mxuport_tiocmget,
1378	.tiocmset		= mxuport_tiocmset,
1379	.dtr_rts		= mxuport_dtr_rts,
1380	.process_read_urb	= mxuport_process_read_urb,
1381	.prepare_write_buffer	= mxuport_prepare_write_buffer,
1382	.resume			= mxuport_resume,
1383};
1384
1385static struct usb_serial_driver *const serial_drivers[] = {
1386	&mxuport_device, NULL
1387};
1388
1389module_usb_serial_driver(serial_drivers, mxuport_idtable);
1390
1391MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
1392MODULE_AUTHOR("<support@moxa.com>");
1393MODULE_LICENSE("GPL");