File Index
Files and directories
Sign in
FILE

mos7840.c

File preview with syntax highlighting.

linux-6.19.14/drivers/usb/serial/mos7840.c C
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Clean ups from Moschip version and a few ioctl implementations by:
   4 *	Paul B Schroeder <pschroeder "at" uplogix "dot" com>
   5 *
   6 * Originally based on drivers/usb/serial/io_edgeport.c which is:
   7 *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
   8 *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
   9 *
  10 */
  11
  12#include <linux/kernel.h>
  13#include <linux/errno.h>
  14#include <linux/slab.h>
  15#include <linux/tty.h>
  16#include <linux/tty_driver.h>
  17#include <linux/tty_flip.h>
  18#include <linux/module.h>
  19#include <linux/serial.h>
  20#include <linux/usb.h>
  21#include <linux/usb/serial.h>
  22#include <linux/uaccess.h>
  23
  24#define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
  25
  26/*
  27 * 16C50 UART register defines
  28 */
  29
  30#define LCR_BITS_5             0x00	/* 5 bits/char */
  31#define LCR_BITS_6             0x01	/* 6 bits/char */
  32#define LCR_BITS_7             0x02	/* 7 bits/char */
  33#define LCR_BITS_8             0x03	/* 8 bits/char */
  34#define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
  35
  36#define LCR_STOP_1             0x00	/* 1 stop bit */
  37#define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
  38#define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
  39#define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
  40
  41#define LCR_PAR_NONE           0x00	/* No parity */
  42#define LCR_PAR_ODD            0x08	/* Odd parity */
  43#define LCR_PAR_EVEN           0x18	/* Even parity */
  44#define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
  45#define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
  46#define LCR_PAR_MASK           0x38	/* Mask for parity field */
  47
  48#define LCR_SET_BREAK          0x40	/* Set Break condition */
  49#define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
  50
  51#define MCR_DTR                0x01	/* Assert DTR */
  52#define MCR_RTS                0x02	/* Assert RTS */
  53#define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
  54#define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
  55#define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
  56#define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
  57
  58#define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
  59#define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
  60#define MOS7840_MSR_RI         0x40	/* Current state of RI */
  61#define MOS7840_MSR_CD         0x80	/* Current state of CD */
  62
  63/*
  64 * Defines used for sending commands to port
  65 */
  66
  67#define MOS_WDR_TIMEOUT		5000	/* default urb timeout */
  68
  69/* Requests */
  70#define MCS_RD_RTYPE    0xC0
  71#define MCS_WR_RTYPE    0x40
  72#define MCS_RDREQ       0x0D
  73#define MCS_WRREQ       0x0E
  74#define VENDOR_READ_LENGTH      (0x01)
  75
  76#define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
  77#define ZLP_REG5  0x3E		/* Zero_Flag_Reg5    62 */
  78
  79/*
  80 * Vendor id and device id defines
  81 *
  82 * NOTE: Do not add new defines, add entries directly to the id_table instead.
  83 */
  84#define USB_VENDOR_ID_BANDB              0x0856
  85#define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
  86#define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
  87#define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
  88#define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
  89#define BANDB_DEVICE_ID_US9ML2_2         0xAC29
  90#define BANDB_DEVICE_ID_US9ML2_4         0xAC30
  91#define BANDB_DEVICE_ID_USPTL4_2         0xAC31
  92#define BANDB_DEVICE_ID_USPTL4_4         0xAC32
  93#define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
  94#define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
  95#define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
  96#define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
  97
  98/* Interrupt Routine Defines    */
  99
 100#define SERIAL_IIR_RLS      0x06
 101#define SERIAL_IIR_MS       0x00
 102
 103/*
 104 *  Emulation of the bit mask on the LINE STATUS REGISTER.
 105 */
 106#define SERIAL_LSR_DR       0x0001
 107#define SERIAL_LSR_OE       0x0002
 108#define SERIAL_LSR_PE       0x0004
 109#define SERIAL_LSR_FE       0x0008
 110#define SERIAL_LSR_BI       0x0010
 111
 112#define MOS_MSR_DELTA_CTS   0x10
 113#define MOS_MSR_DELTA_DSR   0x20
 114#define MOS_MSR_DELTA_RI    0x40
 115#define MOS_MSR_DELTA_CD    0x80
 116
 117/* Serial Port register Address */
 118#define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
 119#define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
 120#define LINE_CONTROL_REGISTER      ((__u16)(0x03))
 121#define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
 122#define LINE_STATUS_REGISTER       ((__u16)(0x05))
 123#define MODEM_STATUS_REGISTER      ((__u16)(0x06))
 124#define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
 125#define DIVISOR_LATCH_LSB          ((__u16)(0x00))
 126#define DIVISOR_LATCH_MSB          ((__u16)(0x01))
 127
 128#define CLK_MULTI_REGISTER         ((__u16)(0x02))
 129#define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
 130#define GPIO_REGISTER              ((__u16)(0x07))
 131
 132#define SERIAL_LCR_DLAB            ((__u16)(0x0080))
 133
 134/*
 135 * URB POOL related defines
 136 */
 137#define NUM_URBS                        16	/* URB Count */
 138#define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
 139
 140/* LED on/off milliseconds*/
 141#define LED_ON_MS	500
 142#define LED_OFF_MS	500
 143
 144enum mos7840_flag {
 145	MOS7840_FLAG_LED_BUSY,
 146};
 147
 148#define MCS_PORT_MASK	GENMASK(2, 0)
 149#define MCS_PORTS(nr)	((nr) & MCS_PORT_MASK)
 150#define MCS_LED		BIT(3)
 151
 152#define MCS_DEVICE(vid, pid, flags) \
 153		USB_DEVICE((vid), (pid)), .driver_info = (flags)
 154
 155static const struct usb_device_id id_table[] = {
 156	{ MCS_DEVICE(0x0557, 0x2011, MCS_PORTS(4)) },	/* ATEN UC2324 */
 157	{ MCS_DEVICE(0x0557, 0x7820, MCS_PORTS(2)) },	/* ATEN UC2322 */
 158	{ MCS_DEVICE(0x110a, 0x2210, MCS_PORTS(2)) },	/* Moxa UPort 2210 */
 159	{ MCS_DEVICE(0x9710, 0x7810, MCS_PORTS(1) | MCS_LED) }, /* ASIX MCS7810 */
 160	{ MCS_DEVICE(0x9710, 0x7820, MCS_PORTS(2)) },	/* MosChip MCS7820 */
 161	{ MCS_DEVICE(0x9710, 0x7840, MCS_PORTS(4)) },	/* MosChip MCS7840 */
 162	{ MCS_DEVICE(0x9710, 0x7843, MCS_PORTS(3)) },	/* ASIX MCS7840 3 port */
 163	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2) },
 164	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P) },
 165	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4) },
 166	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P) },
 167	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2) },
 168	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4) },
 169	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2) },
 170	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4) },
 171	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2) },
 172	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P) },
 173	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4) },
 174	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P) },
 175	{}			/* terminating entry */
 176};
 177MODULE_DEVICE_TABLE(usb, id_table);
 178
 179/* This structure holds all of the local port information */
 180
 181struct moschip_port {
 182	int port_num;		/*Actual port number in the device(1,2,etc) */
 183	struct urb *read_urb;	/* read URB for this port */
 184	__u8 shadowLCR;		/* last LCR value received */
 185	__u8 shadowMCR;		/* last MCR value received */
 186	struct usb_serial_port *port;	/* loop back to the owner of this object */
 187
 188	/* Offsets */
 189	__u8 SpRegOffset;
 190	__u8 ControlRegOffset;
 191	__u8 DcrRegOffset;
 192
 193	spinlock_t pool_lock;
 194	struct urb *write_urb_pool[NUM_URBS];
 195	char busy[NUM_URBS];
 196	bool read_urb_busy;
 197
 198	/* For device(s) with LED indicator */
 199	bool has_led;
 200	struct timer_list led_timer1;	/* Timer for LED on */
 201	struct timer_list led_timer2;	/* Timer for LED off */
 202	struct urb *led_urb;
 203	struct usb_ctrlrequest *led_dr;
 204
 205	unsigned long flags;
 206};
 207
 208/*
 209 * mos7840_set_reg_sync
 210 * 	To set the Control register by calling usb_fill_control_urb function
 211 *	by passing usb_sndctrlpipe function as parameter.
 212 */
 213
 214static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
 215				__u16 val)
 216{
 217	struct usb_device *dev = port->serial->dev;
 218	val = val & 0x00ff;
 219	dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
 220
 221	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
 222			       MCS_WR_RTYPE, val, reg, NULL, 0,
 223			       MOS_WDR_TIMEOUT);
 224}
 225
 226/*
 227 * mos7840_get_reg_sync
 228 * 	To set the Uart register by calling usb_fill_control_urb function by
 229 *	passing usb_rcvctrlpipe function as parameter.
 230 */
 231
 232static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
 233				__u16 *val)
 234{
 235	struct usb_device *dev = port->serial->dev;
 236	int ret = 0;
 237	u8 *buf;
 238
 239	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
 240	if (!buf)
 241		return -ENOMEM;
 242
 243	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
 244			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
 245			      MOS_WDR_TIMEOUT);
 246	if (ret < VENDOR_READ_LENGTH) {
 247		if (ret >= 0)
 248			ret = -EIO;
 249		goto out;
 250	}
 251
 252	*val = buf[0];
 253	dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
 254out:
 255	kfree(buf);
 256	return ret;
 257}
 258
 259/*
 260 * mos7840_set_uart_reg
 261 *	To set the Uart register by calling usb_fill_control_urb function by
 262 *	passing usb_sndctrlpipe function as parameter.
 263 */
 264
 265static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
 266				__u16 val)
 267{
 268
 269	struct usb_device *dev = port->serial->dev;
 270	val = val & 0x00ff;
 271	/* For the UART control registers, the application number need
 272	   to be Or'ed */
 273	if (port->serial->num_ports == 2 && port->port_number != 0)
 274		val |= ((__u16)port->port_number + 2) << 8;
 275	else
 276		val |= ((__u16)port->port_number + 1) << 8;
 277	dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
 278	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
 279			       MCS_WR_RTYPE, val, reg, NULL, 0,
 280			       MOS_WDR_TIMEOUT);
 281
 282}
 283
 284/*
 285 * mos7840_get_uart_reg
 286 *	To set the Control register by calling usb_fill_control_urb function
 287 *	by passing usb_rcvctrlpipe function as parameter.
 288 */
 289static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
 290				__u16 *val)
 291{
 292	struct usb_device *dev = port->serial->dev;
 293	int ret = 0;
 294	__u16 Wval;
 295	u8 *buf;
 296
 297	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
 298	if (!buf)
 299		return -ENOMEM;
 300
 301	/* Wval  is same as application number */
 302	if (port->serial->num_ports == 2 && port->port_number != 0)
 303		Wval = ((__u16)port->port_number + 2) << 8;
 304	else
 305		Wval = ((__u16)port->port_number + 1) << 8;
 306	dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
 307	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
 308			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
 309			      MOS_WDR_TIMEOUT);
 310	if (ret < VENDOR_READ_LENGTH) {
 311		if (ret >= 0)
 312			ret = -EIO;
 313		goto out;
 314	}
 315	*val = buf[0];
 316out:
 317	kfree(buf);
 318	return ret;
 319}
 320
 321static void mos7840_dump_serial_port(struct usb_serial_port *port,
 322				     struct moschip_port *mos7840_port)
 323{
 324
 325	dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
 326	dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
 327	dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
 328
 329}
 330
 331/************************************************************************/
 332/************************************************************************/
 333/*            U S B  C A L L B A C K   F U N C T I O N S                */
 334/*            U S B  C A L L B A C K   F U N C T I O N S                */
 335/************************************************************************/
 336/************************************************************************/
 337
 338static void mos7840_set_led_callback(struct urb *urb)
 339{
 340	switch (urb->status) {
 341	case 0:
 342		/* Success */
 343		break;
 344	case -ECONNRESET:
 345	case -ENOENT:
 346	case -ESHUTDOWN:
 347		/* This urb is terminated, clean up */
 348		dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
 349			__func__, urb->status);
 350		break;
 351	default:
 352		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
 353			__func__, urb->status);
 354	}
 355}
 356
 357static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
 358				__u16 reg)
 359{
 360	struct usb_device *dev = mcs->port->serial->dev;
 361	struct usb_ctrlrequest *dr = mcs->led_dr;
 362
 363	dr->bRequestType = MCS_WR_RTYPE;
 364	dr->bRequest = MCS_WRREQ;
 365	dr->wValue = cpu_to_le16(wval);
 366	dr->wIndex = cpu_to_le16(reg);
 367	dr->wLength = cpu_to_le16(0);
 368
 369	usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
 370		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
 371
 372	usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
 373}
 374
 375static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
 376				__u16 val)
 377{
 378	struct usb_device *dev = port->serial->dev;
 379
 380	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
 381			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
 382}
 383
 384static void mos7840_led_off(struct timer_list *t)
 385{
 386	struct moschip_port *mcs = timer_container_of(mcs, t, led_timer1);
 387
 388	/* Turn off LED */
 389	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
 390	mod_timer(&mcs->led_timer2,
 391				jiffies + msecs_to_jiffies(LED_OFF_MS));
 392}
 393
 394static void mos7840_led_flag_off(struct timer_list *t)
 395{
 396	struct moschip_port *mcs = timer_container_of(mcs, t, led_timer2);
 397
 398	clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
 399}
 400
 401static void mos7840_led_activity(struct usb_serial_port *port)
 402{
 403	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
 404
 405	if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
 406		return;
 407
 408	mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
 409	mod_timer(&mos7840_port->led_timer1,
 410				jiffies + msecs_to_jiffies(LED_ON_MS));
 411}
 412
 413/*****************************************************************************
 414 * mos7840_bulk_in_callback
 415 *	this is the callback function for when we have received data on the
 416 *	bulk in endpoint.
 417 *****************************************************************************/
 418
 419static void mos7840_bulk_in_callback(struct urb *urb)
 420{
 421	struct moschip_port *mos7840_port = urb->context;
 422	struct usb_serial_port *port = mos7840_port->port;
 423	int retval;
 424	unsigned char *data;
 425	int status = urb->status;
 426
 427	if (status) {
 428		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
 429		mos7840_port->read_urb_busy = false;
 430		return;
 431	}
 432
 433	data = urb->transfer_buffer;
 434	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
 435
 436	if (urb->actual_length) {
 437		struct tty_port *tport = &mos7840_port->port->port;
 438		tty_insert_flip_string(tport, data, urb->actual_length);
 439		tty_flip_buffer_push(tport);
 440		port->icount.rx += urb->actual_length;
 441		dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
 442	}
 443
 444	if (mos7840_port->has_led)
 445		mos7840_led_activity(port);
 446
 447	mos7840_port->read_urb_busy = true;
 448	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
 449
 450	if (retval) {
 451		dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
 452		mos7840_port->read_urb_busy = false;
 453	}
 454}
 455
 456/*****************************************************************************
 457 * mos7840_bulk_out_data_callback
 458 *	this is the callback function for when we have finished sending
 459 *	serial data on the bulk out endpoint.
 460 *****************************************************************************/
 461
 462static void mos7840_bulk_out_data_callback(struct urb *urb)
 463{
 464	struct moschip_port *mos7840_port = urb->context;
 465	struct usb_serial_port *port = mos7840_port->port;
 466	int status = urb->status;
 467	unsigned long flags;
 468	int i;
 469
 470	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
 471	for (i = 0; i < NUM_URBS; i++) {
 472		if (urb == mos7840_port->write_urb_pool[i]) {
 473			mos7840_port->busy[i] = 0;
 474			break;
 475		}
 476	}
 477	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
 478
 479	if (status) {
 480		dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
 481		return;
 482	}
 483
 484	tty_port_tty_wakeup(&port->port);
 485
 486}
 487
 488/************************************************************************/
 489/*       D R I V E R  T T Y  I N T E R F A C E  F U N C T I O N S       */
 490/************************************************************************/
 491
 492/*****************************************************************************
 493 * mos7840_open
 494 *	this function is called by the tty driver when a port is opened
 495 *	If successful, we return 0
 496 *	Otherwise we return a negative error number.
 497 *****************************************************************************/
 498
 499static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
 500{
 501	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
 502	struct usb_serial *serial = port->serial;
 503	int response;
 504	int j;
 505	struct urb *urb;
 506	__u16 Data;
 507	int status;
 508
 509	usb_clear_halt(serial->dev, port->write_urb->pipe);
 510	usb_clear_halt(serial->dev, port->read_urb->pipe);
 511
 512	/* Initialising the write urb pool */
 513	for (j = 0; j < NUM_URBS; ++j) {
 514		urb = usb_alloc_urb(0, GFP_KERNEL);
 515		mos7840_port->write_urb_pool[j] = urb;
 516		if (!urb)
 517			continue;
 518
 519		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
 520								GFP_KERNEL);
 521		if (!urb->transfer_buffer) {
 522			usb_free_urb(urb);
 523			mos7840_port->write_urb_pool[j] = NULL;
 524			continue;
 525		}
 526	}
 527
 528/*****************************************************************************
 529 * Initialize MCS7840 -- Write Init values to corresponding Registers
 530 *
 531 * Register Index
 532 * 1 : IER
 533 * 2 : FCR
 534 * 3 : LCR
 535 * 4 : MCR
 536 *
 537 * 0x08 : SP1/2 Control Reg
 538 *****************************************************************************/
 539
 540	/* NEED to check the following Block */
 541
 542	Data = 0x0;
 543	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
 544	if (status < 0) {
 545		dev_dbg(&port->dev, "Reading Spreg failed\n");
 546		goto err;
 547	}
 548	Data |= 0x80;
 549	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
 550	if (status < 0) {
 551		dev_dbg(&port->dev, "writing Spreg failed\n");
 552		goto err;
 553	}
 554
 555	Data &= ~0x80;
 556	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
 557	if (status < 0) {
 558		dev_dbg(&port->dev, "writing Spreg failed\n");
 559		goto err;
 560	}
 561	/* End of block to be checked */
 562
 563	Data = 0x0;
 564	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
 565									&Data);
 566	if (status < 0) {
 567		dev_dbg(&port->dev, "Reading Controlreg failed\n");
 568		goto err;
 569	}
 570	Data |= 0x08;		/* Driver done bit */
 571	Data |= 0x20;		/* rx_disable */
 572	status = mos7840_set_reg_sync(port,
 573				mos7840_port->ControlRegOffset, Data);
 574	if (status < 0) {
 575		dev_dbg(&port->dev, "writing Controlreg failed\n");
 576		goto err;
 577	}
 578	/* do register settings here */
 579	/* Set all regs to the device default values. */
 580	/***********************************
 581	 * First Disable all interrupts.
 582	 ***********************************/
 583	Data = 0x00;
 584	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
 585	if (status < 0) {
 586		dev_dbg(&port->dev, "disabling interrupts failed\n");
 587		goto err;
 588	}
 589	/* Set FIFO_CONTROL_REGISTER to the default value */
 590	Data = 0x00;
 591	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
 592	if (status < 0) {
 593		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
 594		goto err;
 595	}
 596
 597	Data = 0xcf;
 598	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
 599	if (status < 0) {
 600		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
 601		goto err;
 602	}
 603
 604	Data = 0x03;
 605	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
 606	mos7840_port->shadowLCR = Data;
 607
 608	Data = 0x0b;
 609	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
 610	mos7840_port->shadowMCR = Data;
 611
 612	Data = 0x00;
 613	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
 614	mos7840_port->shadowLCR = Data;
 615
 616	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
 617	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
 618
 619	Data = 0x0c;
 620	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
 621
 622	Data = 0x0;
 623	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
 624
 625	Data = 0x00;
 626	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
 627
 628	Data = Data & ~SERIAL_LCR_DLAB;
 629	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
 630	mos7840_port->shadowLCR = Data;
 631
 632	/* clearing Bulkin and Bulkout Fifo */
 633	Data = 0x0;
 634	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
 635
 636	Data = Data | 0x0c;
 637	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
 638
 639	Data = Data & ~0x0c;
 640	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
 641	/* Finally enable all interrupts */
 642	Data = 0x0c;
 643	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
 644
 645	/* clearing rx_disable */
 646	Data = 0x0;
 647	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
 648									&Data);
 649	Data = Data & ~0x20;
 650	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
 651									Data);
 652
 653	/* rx_negate */
 654	Data = 0x0;
 655	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
 656									&Data);
 657	Data = Data | 0x10;
 658	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
 659									Data);
 660
 661	dev_dbg(&port->dev, "port number is %d\n", port->port_number);
 662	dev_dbg(&port->dev, "minor number is %d\n", port->minor);
 663	dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
 664	dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
 665	dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
 666	dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
 667	mos7840_port->read_urb = port->read_urb;
 668
 669	/* set up our bulk in urb */
 670	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
 671		usb_fill_bulk_urb(mos7840_port->read_urb,
 672			serial->dev,
 673			usb_rcvbulkpipe(serial->dev,
 674				(port->bulk_in_endpointAddress) + 2),
 675			port->bulk_in_buffer,
 676			mos7840_port->read_urb->transfer_buffer_length,
 677			mos7840_bulk_in_callback, mos7840_port);
 678	} else {
 679		usb_fill_bulk_urb(mos7840_port->read_urb,
 680			serial->dev,
 681			usb_rcvbulkpipe(serial->dev,
 682				port->bulk_in_endpointAddress),
 683			port->bulk_in_buffer,
 684			mos7840_port->read_urb->transfer_buffer_length,
 685			mos7840_bulk_in_callback, mos7840_port);
 686	}
 687
 688	dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
 689	mos7840_port->read_urb_busy = true;
 690	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
 691	if (response) {
 692		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
 693			__func__, response);
 694		mos7840_port->read_urb_busy = false;
 695	}
 696
 697	/* initialize our port settings */
 698	/* Must set to enable ints! */
 699	mos7840_port->shadowMCR = MCR_MASTER_IE;
 700
 701	return 0;
 702err:
 703	for (j = 0; j < NUM_URBS; ++j) {
 704		urb = mos7840_port->write_urb_pool[j];
 705		if (!urb)
 706			continue;
 707		kfree(urb->transfer_buffer);
 708		usb_free_urb(urb);
 709	}
 710	return status;
 711}
 712
 713/*****************************************************************************
 714 * mos7840_chars_in_buffer
 715 *	this function is called by the tty driver when it wants to know how many
 716 *	bytes of data we currently have outstanding in the port (data that has
 717 *	been written, but hasn't made it out the port yet)
 718 *****************************************************************************/
 719
 720static unsigned int mos7840_chars_in_buffer(struct tty_struct *tty)
 721{
 722	struct usb_serial_port *port = tty->driver_data;
 723	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
 724	int i;
 725	unsigned int chars = 0;
 726	unsigned long flags;
 727
 728	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
 729	for (i = 0; i < NUM_URBS; ++i) {
 730		if (mos7840_port->busy[i]) {
 731			struct urb *urb = mos7840_port->write_urb_pool[i];
 732			chars += urb->transfer_buffer_length;
 733		}
 734	}
 735	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
 736	dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
 737	return chars;
 738
 739}
 740
 741/*****************************************************************************
 742 * mos7840_close
 743 *	this function is called by the tty driver when a port is closed
 744 *****************************************************************************/
 745
 746static void mos7840_close(struct usb_serial_port *port)
 747{
 748	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
 749	int j;
 750	__u16 Data;
 751
 752	for (j = 0; j < NUM_URBS; ++j)
 753		usb_kill_urb(mos7840_port->write_urb_pool[j]);
 754
 755	/* Freeing Write URBs */
 756	for (j = 0; j < NUM_URBS; ++j) {
 757		if (mos7840_port->write_urb_pool[j]) {
 758			kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
 759			usb_free_urb(mos7840_port->write_urb_pool[j]);
 760		}
 761	}
 762
 763	usb_kill_urb(mos7840_port->read_urb);
 764	mos7840_port->read_urb_busy = false;
 765
 766	Data = 0x0;
 767	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
 768
 769	Data = 0x00;
 770	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
 771}
 772
 773/*****************************************************************************
 774 * mos7840_break
 775 *	this function sends a break to the port
 776 *****************************************************************************/
 777static int mos7840_break(struct tty_struct *tty, int break_state)
 778{
 779	struct usb_serial_port *port = tty->driver_data;
 780	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
 781	unsigned char data;
 782
 783	if (break_state == -1)
 784		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
 785	else
 786		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
 787
 788	/* FIXME: no locking on shadowLCR anywhere in driver */
 789	mos7840_port->shadowLCR = data;
 790	dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
 791
 792	return mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
 793				    mos7840_port->shadowLCR);
 794}
 795
 796/*****************************************************************************
 797 * mos7840_write_room
 798 *	this function is called by the tty driver when it wants to know how many
 799 *	bytes of data we can accept for a specific port.
 800 *****************************************************************************/
 801
 802static unsigned int mos7840_write_room(struct tty_struct *tty)
 803{
 804	struct usb_serial_port *port = tty->driver_data;
 805	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
 806	int i;
 807	unsigned int room = 0;
 808	unsigned long flags;
 809
 810	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
 811	for (i = 0; i < NUM_URBS; ++i) {
 812		if (!mos7840_port->busy[i])
 813			room += URB_TRANSFER_BUFFER_SIZE;
 814	}
 815	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
 816
 817	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
 818	dev_dbg(&mos7840_port->port->dev, "%s - returns %u\n", __func__, room);
 819	return room;
 820
 821}
 822
 823/*****************************************************************************
 824 * mos7840_write
 825 *	this function is called by the tty driver when data should be written to
 826 *	the port.
 827 *	If successful, we return the number of bytes written, otherwise we
 828 *      return a negative error number.
 829 *****************************************************************************/
 830
 831static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
 832			 const unsigned char *data, int count)
 833{
 834	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
 835	struct usb_serial *serial = port->serial;
 836	int status;
 837	int i;
 838	int bytes_sent = 0;
 839	int transfer_size;
 840	unsigned long flags;
 841	struct urb *urb;
 842	/* __u16 Data; */
 843	const unsigned char *current_position = data;
 844
 845	/* try to find a free urb in the list */
 846	urb = NULL;
 847
 848	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
 849	for (i = 0; i < NUM_URBS; ++i) {
 850		if (!mos7840_port->busy[i]) {
 851			mos7840_port->busy[i] = 1;
 852			urb = mos7840_port->write_urb_pool[i];
 853			dev_dbg(&port->dev, "URB:%d\n", i);
 854			break;
 855		}
 856	}
 857	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
 858
 859	if (urb == NULL) {
 860		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
 861		goto exit;
 862	}
 863
 864	if (urb->transfer_buffer == NULL) {
 865		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
 866					       GFP_ATOMIC);
 867		if (!urb->transfer_buffer) {
 868			bytes_sent = -ENOMEM;
 869			goto exit;
 870		}
 871	}
 872	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
 873
 874	memcpy(urb->transfer_buffer, current_position, transfer_size);
 875
 876	/* fill urb with data and submit  */
 877	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
 878		usb_fill_bulk_urb(urb,
 879			serial->dev,
 880			usb_sndbulkpipe(serial->dev,
 881				(port->bulk_out_endpointAddress) + 2),
 882			urb->transfer_buffer,
 883			transfer_size,
 884			mos7840_bulk_out_data_callback, mos7840_port);
 885	} else {
 886		usb_fill_bulk_urb(urb,
 887			serial->dev,
 888			usb_sndbulkpipe(serial->dev,
 889				port->bulk_out_endpointAddress),
 890			urb->transfer_buffer,
 891			transfer_size,
 892			mos7840_bulk_out_data_callback, mos7840_port);
 893	}
 894
 895	dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
 896
 897	if (mos7840_port->has_led)
 898		mos7840_led_activity(port);
 899
 900	/* send it down the pipe */
 901	status = usb_submit_urb(urb, GFP_ATOMIC);
 902
 903	if (status) {
 904		mos7840_port->busy[i] = 0;
 905		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
 906			"with status = %d\n", __func__, status);
 907		bytes_sent = status;
 908		goto exit;
 909	}
 910	bytes_sent = transfer_size;
 911	port->icount.tx += transfer_size;
 912	dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
 913exit:
 914	return bytes_sent;
 915
 916}
 917
 918/*****************************************************************************
 919 * mos7840_throttle
 920 *	this function is called by the tty driver when it wants to stop the data
 921 *	being read from the port.
 922 *****************************************************************************/
 923
 924static void mos7840_throttle(struct tty_struct *tty)
 925{
 926	struct usb_serial_port *port = tty->driver_data;
 927	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
 928	int status;
 929
 930	/* if we are implementing XON/XOFF, send the stop character */
 931	if (I_IXOFF(tty)) {
 932		unsigned char stop_char = STOP_CHAR(tty);
 933		status = mos7840_write(tty, port, &stop_char, 1);
 934		if (status <= 0)
 935			return;
 936	}
 937	/* if we are implementing RTS/CTS, toggle that line */
 938	if (C_CRTSCTS(tty)) {
 939		mos7840_port->shadowMCR &= ~MCR_RTS;
 940		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
 941					 mos7840_port->shadowMCR);
 942		if (status < 0)
 943			return;
 944	}
 945}
 946
 947/*****************************************************************************
 948 * mos7840_unthrottle
 949 *	this function is called by the tty driver when it wants to resume
 950 *	the data being read from the port (called after mos7840_throttle is
 951 *	called)
 952 *****************************************************************************/
 953static void mos7840_unthrottle(struct tty_struct *tty)
 954{
 955	struct usb_serial_port *port = tty->driver_data;
 956	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
 957	int status;
 958
 959	/* if we are implementing XON/XOFF, send the start character */
 960	if (I_IXOFF(tty)) {
 961		unsigned char start_char = START_CHAR(tty);
 962		status = mos7840_write(tty, port, &start_char, 1);
 963		if (status <= 0)
 964			return;
 965	}
 966
 967	/* if we are implementing RTS/CTS, toggle that line */
 968	if (C_CRTSCTS(tty)) {
 969		mos7840_port->shadowMCR |= MCR_RTS;
 970		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
 971					 mos7840_port->shadowMCR);
 972		if (status < 0)
 973			return;
 974	}
 975}
 976
 977static int mos7840_tiocmget(struct tty_struct *tty)
 978{
 979	struct usb_serial_port *port = tty->driver_data;
 980	unsigned int result;
 981	__u16 msr;
 982	__u16 mcr;
 983	int status;
 984
 985	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
 986	if (status < 0)
 987		return -EIO;
 988	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
 989	if (status < 0)
 990		return -EIO;
 991	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
 992	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
 993	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
 994	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
 995	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
 996	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
 997	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
 998
 999	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1000
1001	return result;
1002}
1003
1004static int mos7840_tiocmset(struct tty_struct *tty,
1005			    unsigned int set, unsigned int clear)
1006{
1007	struct usb_serial_port *port = tty->driver_data;
1008	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
1009	unsigned int mcr;
1010	int status;
1011
1012	/* FIXME: What locks the port registers ? */
1013	mcr = mos7840_port->shadowMCR;
1014	if (clear & TIOCM_RTS)
1015		mcr &= ~MCR_RTS;
1016	if (clear & TIOCM_DTR)
1017		mcr &= ~MCR_DTR;
1018	if (clear & TIOCM_LOOP)
1019		mcr &= ~MCR_LOOPBACK;
1020
1021	if (set & TIOCM_RTS)
1022		mcr |= MCR_RTS;
1023	if (set & TIOCM_DTR)
1024		mcr |= MCR_DTR;
1025	if (set & TIOCM_LOOP)
1026		mcr |= MCR_LOOPBACK;
1027
1028	mos7840_port->shadowMCR = mcr;
1029
1030	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1031	if (status < 0) {
1032		dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1033		return status;
1034	}
1035
1036	return 0;
1037}
1038
1039/*****************************************************************************
1040 * mos7840_calc_baud_rate_divisor
1041 *	this function calculates the proper baud rate divisor for the specified
1042 *	baud rate.
1043 *****************************************************************************/
1044static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1045					  int baudRate, int *divisor,
1046					  __u16 *clk_sel_val)
1047{
1048	dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1049
1050	if (baudRate <= 115200) {
1051		*divisor = 115200 / baudRate;
1052		*clk_sel_val = 0x0;
1053	}
1054	if ((baudRate > 115200) && (baudRate <= 230400)) {
1055		*divisor = 230400 / baudRate;
1056		*clk_sel_val = 0x10;
1057	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
1058		*divisor = 403200 / baudRate;
1059		*clk_sel_val = 0x20;
1060	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
1061		*divisor = 460800 / baudRate;
1062		*clk_sel_val = 0x30;
1063	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
1064		*divisor = 806400 / baudRate;
1065		*clk_sel_val = 0x40;
1066	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
1067		*divisor = 921600 / baudRate;
1068		*clk_sel_val = 0x50;
1069	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1070		*divisor = 1572864 / baudRate;
1071		*clk_sel_val = 0x60;
1072	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1073		*divisor = 3145728 / baudRate;
1074		*clk_sel_val = 0x70;
1075	}
1076	return 0;
1077}
1078
1079/*****************************************************************************
1080 * mos7840_send_cmd_write_baud_rate
1081 *	this function sends the proper command to change the baud rate of the
1082 *	specified port.
1083 *****************************************************************************/
1084
1085static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1086					    int baudRate)
1087{
1088	struct usb_serial_port *port = mos7840_port->port;
1089	int divisor = 0;
1090	int status;
1091	__u16 Data;
1092	__u16 clk_sel_val;
1093
1094	dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1095	/* reset clk_uart_sel in spregOffset */
1096	if (baudRate > 115200) {
1097#ifdef HW_flow_control
1098		/* NOTE: need to see the pther register to modify */
1099		/* setting h/w flow control bit to 1 */
1100		Data = 0x2b;
1101		mos7840_port->shadowMCR = Data;
1102		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1103									Data);
1104		if (status < 0) {
1105			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1106			return -1;
1107		}
1108#endif
1109
1110	} else {
1111#ifdef HW_flow_control
1112		/* setting h/w flow control bit to 0 */
1113		Data = 0xb;
1114		mos7840_port->shadowMCR = Data;
1115		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1116									Data);
1117		if (status < 0) {
1118			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1119			return -1;
1120		}
1121#endif
1122
1123	}
1124
1125	if (1) {		/* baudRate <= 115200) */
1126		clk_sel_val = 0x0;
1127		Data = 0x0;
1128		status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1129						   &clk_sel_val);
1130		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1131								 &Data);
1132		if (status < 0) {
1133			dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1134			return -1;
1135		}
1136		Data = (Data & 0x8f) | clk_sel_val;
1137		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1138								Data);
1139		if (status < 0) {
1140			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1141			return -1;
1142		}
1143		/* Calculate the Divisor */
1144
1145		if (status) {
1146			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1147			return status;
1148		}
1149		/* Enable access to divisor latch */
1150		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1151		mos7840_port->shadowLCR = Data;
1152		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1153
1154		/* Write the divisor */
1155		Data = (unsigned char)(divisor & 0xff);
1156		dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1157		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1158
1159		Data = (unsigned char)((divisor & 0xff00) >> 8);
1160		dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1161		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1162
1163		/* Disable access to divisor latch */
1164		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1165		mos7840_port->shadowLCR = Data;
1166		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1167
1168	}
1169	return status;
1170}
1171
1172/*****************************************************************************
1173 * mos7840_change_port_settings
1174 *	This routine is called to set the UART on the device to match
1175 *      the specified new settings.
1176 *****************************************************************************/
1177
1178static void mos7840_change_port_settings(struct tty_struct *tty,
1179					 struct moschip_port *mos7840_port,
1180					 const struct ktermios *old_termios)
1181{
1182	struct usb_serial_port *port = mos7840_port->port;
1183	int baud;
1184	unsigned cflag;
1185	__u8 lData;
1186	__u8 lParity;
1187	__u8 lStop;
1188	int status;
1189	__u16 Data;
1190
1191	lData = LCR_BITS_8;
1192	lStop = LCR_STOP_1;
1193	lParity = LCR_PAR_NONE;
1194
1195	cflag = tty->termios.c_cflag;
1196
1197	/* Change the number of bits */
1198	switch (cflag & CSIZE) {
1199	case CS5:
1200		lData = LCR_BITS_5;
1201		break;
1202
1203	case CS6:
1204		lData = LCR_BITS_6;
1205		break;
1206
1207	case CS7:
1208		lData = LCR_BITS_7;
1209		break;
1210
1211	default:
1212	case CS8:
1213		lData = LCR_BITS_8;
1214		break;
1215	}
1216
1217	/* Change the Parity bit */
1218	if (cflag & PARENB) {
1219		if (cflag & PARODD) {
1220			lParity = LCR_PAR_ODD;
1221			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1222		} else {
1223			lParity = LCR_PAR_EVEN;
1224			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1225		}
1226
1227	} else {
1228		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1229	}
1230
1231	if (cflag & CMSPAR)
1232		lParity = lParity | 0x20;
1233
1234	/* Change the Stop bit */
1235	if (cflag & CSTOPB) {
1236		lStop = LCR_STOP_2;
1237		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1238	} else {
1239		lStop = LCR_STOP_1;
1240		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1241	}
1242
1243	/* Update the LCR with the correct value */
1244	mos7840_port->shadowLCR &=
1245	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1246	mos7840_port->shadowLCR |= (lData | lParity | lStop);
1247
1248	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1249		mos7840_port->shadowLCR);
1250	/* Disable Interrupts */
1251	Data = 0x00;
1252	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1253
1254	Data = 0x00;
1255	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1256
1257	Data = 0xcf;
1258	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1259
1260	/* Send the updated LCR value to the mos7840 */
1261	Data = mos7840_port->shadowLCR;
1262
1263	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1264
1265	Data = 0x00b;
1266	mos7840_port->shadowMCR = Data;
1267	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1268	Data = 0x00b;
1269	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1270
1271	/* set up the MCR register and send it to the mos7840 */
1272
1273	mos7840_port->shadowMCR = MCR_MASTER_IE;
1274	if (cflag & CBAUD)
1275		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
1276
1277	if (cflag & CRTSCTS)
1278		mos7840_port->shadowMCR |= (MCR_XON_ANY);
1279	else
1280		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
1281
1282	Data = mos7840_port->shadowMCR;
1283	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1284
1285	/* Determine divisor based on baud rate */
1286	baud = tty_get_baud_rate(tty);
1287
1288	if (!baud) {
1289		/* pick a default, any default... */
1290		dev_dbg(&port->dev, "%s", "Picked default baud...\n");
1291		baud = 9600;
1292	}
1293
1294	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1295	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1296
1297	/* Enable Interrupts */
1298	Data = 0x0c;
1299	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1300
1301	if (!mos7840_port->read_urb_busy) {
1302		mos7840_port->read_urb_busy = true;
1303		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1304		if (status) {
1305			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1306			    status);
1307			mos7840_port->read_urb_busy = false;
1308		}
1309	}
1310	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1311		mos7840_port->shadowLCR);
1312}
1313
1314/*****************************************************************************
1315 * mos7840_set_termios
1316 *	this function is called by the tty driver when it wants to change
1317 *	the termios structure
1318 *****************************************************************************/
1319
1320static void mos7840_set_termios(struct tty_struct *tty,
1321				struct usb_serial_port *port,
1322				const struct ktermios *old_termios)
1323{
1324	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
1325	int status;
1326
1327	/* change the port settings to the new ones specified */
1328
1329	mos7840_change_port_settings(tty, mos7840_port, old_termios);
1330
1331	if (!mos7840_port->read_urb_busy) {
1332		mos7840_port->read_urb_busy = true;
1333		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1334		if (status) {
1335			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1336			    status);
1337			mos7840_port->read_urb_busy = false;
1338		}
1339	}
1340}
1341
1342/*****************************************************************************
1343 * mos7840_get_lsr_info - get line status register info
1344 *
1345 * Purpose: Let user call ioctl() to get info when the UART physically
1346 * 	    is emptied.  On bus types like RS485, the transmitter must
1347 * 	    release the bus after transmitting. This must be done when
1348 * 	    the transmit shift register is empty, not be done when the
1349 * 	    transmit holding register is empty.  This functionality
1350 * 	    allows an RS485 driver to be written in user space.
1351 *****************************************************************************/
1352
1353static int mos7840_get_lsr_info(struct tty_struct *tty,
1354				unsigned int __user *value)
1355{
1356	int count;
1357	unsigned int result = 0;
1358
1359	count = mos7840_chars_in_buffer(tty);
1360	if (count == 0)
1361		result = TIOCSER_TEMT;
1362
1363	if (copy_to_user(value, &result, sizeof(int)))
1364		return -EFAULT;
1365	return 0;
1366}
1367
1368/*****************************************************************************
1369 * SerialIoctl
1370 *	this function handles any ioctl calls to the driver
1371 *****************************************************************************/
1372
1373static int mos7840_ioctl(struct tty_struct *tty,
1374			 unsigned int cmd, unsigned long arg)
1375{
1376	struct usb_serial_port *port = tty->driver_data;
1377	void __user *argp = (void __user *)arg;
1378
1379	switch (cmd) {
1380		/* return number of bytes available */
1381
1382	case TIOCSERGETLSR:
1383		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1384		return mos7840_get_lsr_info(tty, argp);
1385
1386	default:
1387		break;
1388	}
1389	return -ENOIOCTLCMD;
1390}
1391
1392/*
1393 * Check if GPO (pin 42) is connected to GPI (pin 33) as recommended by ASIX
1394 * for MCS7810 by bit-banging a 16-bit word.
1395 *
1396 * Note that GPO is really RTS of the third port so this will toggle RTS of
1397 * port two or three on two- and four-port devices.
1398 */
1399static int mos7810_check(struct usb_serial *serial)
1400{
1401	int i, pass_count = 0;
1402	u8 *buf;
1403	__u16 data = 0, mcr_data = 0;
1404	__u16 test_pattern = 0x55AA;
1405	int res;
1406
1407	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
1408	if (!buf)
1409		return 0;	/* failed to identify 7810 */
1410
1411	/* Store MCR setting */
1412	res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1413		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
1414		buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
1415	if (res == VENDOR_READ_LENGTH)
1416		mcr_data = *buf;
1417
1418	for (i = 0; i < 16; i++) {
1419		/* Send the 1-bit test pattern out to MCS7810 test pin */
1420		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1421			MCS_WRREQ, MCS_WR_RTYPE,
1422			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
1423			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
1424
1425		/* Read the test pattern back */
1426		res = usb_control_msg(serial->dev,
1427				usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
1428				MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
1429				VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
1430		if (res == VENDOR_READ_LENGTH)
1431			data = *buf;
1432
1433		/* If this is a MCS7810 device, both test patterns must match */
1434		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
1435			break;
1436
1437		pass_count++;
1438	}
1439
1440	/* Restore MCR setting */
1441	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
1442		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
1443		0, MOS_WDR_TIMEOUT);
1444
1445	kfree(buf);
1446
1447	if (pass_count == 16)
1448		return 1;
1449
1450	return 0;
1451}
1452
1453static int mos7840_probe(struct usb_serial *serial,
1454				const struct usb_device_id *id)
1455{
1456	unsigned long device_flags = id->driver_info;
1457	u8 *buf;
1458
1459	/* Skip device-type detection if we already have device flags. */
1460	if (device_flags)
1461		goto out;
1462
1463	buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
1464	if (!buf)
1465		return -ENOMEM;
1466
1467	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1468			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
1469			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
1470
1471	/* For a MCS7840 device GPIO0 must be set to 1 */
1472	if (buf[0] & 0x01)
1473		device_flags = MCS_PORTS(4);
1474	else if (mos7810_check(serial))
1475		device_flags = MCS_PORTS(1) | MCS_LED;
1476	else
1477		device_flags = MCS_PORTS(2);
1478
1479	kfree(buf);
1480out:
1481	usb_set_serial_data(serial, (void *)device_flags);
1482
1483	return 0;
1484}
1485
1486static int mos7840_calc_num_ports(struct usb_serial *serial,
1487					struct usb_serial_endpoints *epds)
1488{
1489	unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
1490	int num_ports = MCS_PORTS(device_flags);
1491
1492	if (num_ports == 0 || num_ports > 4)
1493		return -ENODEV;
1494
1495	if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
1496		dev_err(&serial->interface->dev, "missing endpoints\n");
1497		return -ENODEV;
1498	}
1499
1500	return num_ports;
1501}
1502
1503static int mos7840_attach(struct usb_serial *serial)
1504{
1505	struct device *dev = &serial->interface->dev;
1506	int status;
1507	u16 val;
1508
1509	/* Zero Length flag enable */
1510	val = 0x0f;
1511	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, val);
1512	if (status < 0)
1513		dev_dbg(dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
1514	else
1515		dev_dbg(dev, "ZLP_REG5 Writing success status%d\n", status);
1516
1517	return status;
1518}
1519
1520static int mos7840_port_probe(struct usb_serial_port *port)
1521{
1522	struct usb_serial *serial = port->serial;
1523	unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
1524	struct moschip_port *mos7840_port;
1525	int status;
1526	int pnum;
1527	__u16 Data;
1528
1529	/* we set up the pointers to the endpoints in the mos7840_open *
1530	 * function, as the structures aren't created yet.             */
1531
1532	pnum = port->port_number;
1533
1534	dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
1535	mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
1536	if (!mos7840_port)
1537		return -ENOMEM;
1538
1539	/* Initialize all port interrupt end point to port 0 int
1540	 * endpoint. Our device has only one interrupt end point
1541	 * common to all port */
1542
1543	mos7840_port->port = port;
1544	spin_lock_init(&mos7840_port->pool_lock);
1545
1546	/* minor is not initialised until later by
1547	 * usb-serial.c:get_free_serial() and cannot therefore be used
1548	 * to index device instances */
1549	mos7840_port->port_num = pnum + 1;
1550	dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
1551	dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
1552
1553	if (mos7840_port->port_num == 1) {
1554		mos7840_port->SpRegOffset = 0x0;
1555		mos7840_port->ControlRegOffset = 0x1;
1556		mos7840_port->DcrRegOffset = 0x4;
1557	} else {
1558		u8 phy_num = mos7840_port->port_num;
1559
1560		/* Port 2 in the 2-port case uses registers of port 3 */
1561		if (serial->num_ports == 2)
1562			phy_num = 3;
1563
1564		mos7840_port->SpRegOffset = 0x8 + 2 * (phy_num - 2);
1565		mos7840_port->ControlRegOffset = 0x9 + 2 * (phy_num - 2);
1566		mos7840_port->DcrRegOffset = 0x16 + 3 * (phy_num - 2);
1567	}
1568	mos7840_dump_serial_port(port, mos7840_port);
1569	usb_set_serial_port_data(port, mos7840_port);
1570
1571	/* enable rx_disable bit in control register */
1572	status = mos7840_get_reg_sync(port,
1573			mos7840_port->ControlRegOffset, &Data);
1574	if (status < 0) {
1575		dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
1576		goto error;
1577	} else
1578		dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
1579	Data |= 0x08;	/* setting driver done bit */
1580	Data |= 0x04;	/* sp1_bit to have cts change reflect in
1581			   modem status reg */
1582
1583	/* Data |= 0x20; //rx_disable bit */
1584	status = mos7840_set_reg_sync(port,
1585			mos7840_port->ControlRegOffset, Data);
1586	if (status < 0) {
1587		dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
1588		goto error;
1589	} else
1590		dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
1591
1592	/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
1593	   and 0x24 in DCR3 */
1594	Data = 0x01;
1595	status = mos7840_set_reg_sync(port,
1596			(__u16) (mos7840_port->DcrRegOffset + 0), Data);
1597	if (status < 0) {
1598		dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
1599		goto error;
1600	} else
1601		dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
1602
1603	Data = 0x05;
1604	status = mos7840_set_reg_sync(port,
1605			(__u16) (mos7840_port->DcrRegOffset + 1), Data);
1606	if (status < 0) {
1607		dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
1608		goto error;
1609	} else
1610		dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
1611
1612	Data = 0x24;
1613	status = mos7840_set_reg_sync(port,
1614			(__u16) (mos7840_port->DcrRegOffset + 2), Data);
1615	if (status < 0) {
1616		dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
1617		goto error;
1618	} else
1619		dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
1620
1621	/* write values in clkstart0x0 and clkmulti 0x20 */
1622	Data = 0x0;
1623	status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
1624	if (status < 0) {
1625		dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
1626		goto error;
1627	} else
1628		dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
1629
1630	Data = 0x20;
1631	status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
1632	if (status < 0) {
1633		dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
1634		goto error;
1635	} else
1636		dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
1637
1638	/* write value 0x0 to scratchpad register */
1639	Data = 0x00;
1640	status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
1641	if (status < 0) {
1642		dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
1643		goto error;
1644	} else
1645		dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
1646
1647	/* Zero Length flag register */
1648	if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
1649		Data = 0xff;
1650		status = mos7840_set_reg_sync(port,
1651				(__u16) (ZLP_REG1 +
1652					((__u16)mos7840_port->port_num)), Data);
1653		dev_dbg(&port->dev, "ZLIP offset %x\n",
1654				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
1655		if (status < 0) {
1656			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
1657			goto error;
1658		} else
1659			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
1660	} else {
1661		Data = 0xff;
1662		status = mos7840_set_reg_sync(port,
1663				(__u16) (ZLP_REG1 +
1664					((__u16)mos7840_port->port_num) - 0x1), Data);
1665		dev_dbg(&port->dev, "ZLIP offset %x\n",
1666				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
1667		if (status < 0) {
1668			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
1669			goto error;
1670		} else
1671			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
1672
1673	}
1674
1675	mos7840_port->has_led = device_flags & MCS_LED;
1676
1677	/* Initialize LED timers */
1678	if (mos7840_port->has_led) {
1679		mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
1680		mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
1681								GFP_KERNEL);
1682		if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
1683			status = -ENOMEM;
1684			goto error;
1685		}
1686
1687		timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
1688		mos7840_port->led_timer1.expires =
1689			jiffies + msecs_to_jiffies(LED_ON_MS);
1690		timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
1691			    0);
1692		mos7840_port->led_timer2.expires =
1693			jiffies + msecs_to_jiffies(LED_OFF_MS);
1694
1695		/* Turn off LED */
1696		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
1697	}
1698
1699	return 0;
1700error:
1701	kfree(mos7840_port->led_dr);
1702	usb_free_urb(mos7840_port->led_urb);
1703	kfree(mos7840_port);
1704
1705	return status;
1706}
1707
1708static void mos7840_port_remove(struct usb_serial_port *port)
1709{
1710	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
1711
1712	if (mos7840_port->has_led) {
1713		/* Turn off LED */
1714		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
1715
1716		timer_shutdown_sync(&mos7840_port->led_timer1);
1717		timer_shutdown_sync(&mos7840_port->led_timer2);
1718
1719		usb_kill_urb(mos7840_port->led_urb);
1720		usb_free_urb(mos7840_port->led_urb);
1721		kfree(mos7840_port->led_dr);
1722	}
1723
1724	kfree(mos7840_port);
1725}
1726
1727static int mos7840_suspend(struct usb_serial *serial, pm_message_t message)
1728{
1729	struct moschip_port *mos7840_port;
1730	struct usb_serial_port *port;
1731	int i;
1732
1733	for (i = 0; i < serial->num_ports; ++i) {
1734		port = serial->port[i];
1735		if (!tty_port_initialized(&port->port))
1736			continue;
1737
1738		mos7840_port = usb_get_serial_port_data(port);
1739
1740		usb_kill_urb(mos7840_port->read_urb);
1741		mos7840_port->read_urb_busy = false;
1742	}
1743
1744	return 0;
1745}
1746
1747static int mos7840_resume(struct usb_serial *serial)
1748{
1749	struct moschip_port *mos7840_port;
1750	struct usb_serial_port *port;
1751	int res;
1752	int i;
1753
1754	for (i = 0; i < serial->num_ports; ++i) {
1755		port = serial->port[i];
1756		if (!tty_port_initialized(&port->port))
1757			continue;
1758
1759		mos7840_port = usb_get_serial_port_data(port);
1760
1761		mos7840_port->read_urb_busy = true;
1762		res = usb_submit_urb(mos7840_port->read_urb, GFP_NOIO);
1763		if (res)
1764			mos7840_port->read_urb_busy = false;
1765	}
1766
1767	return 0;
1768}
1769
1770static struct usb_serial_driver moschip7840_4port_device = {
1771	.driver = {
1772		   .name = "mos7840",
1773		   },
1774	.description = DRIVER_DESC,
1775	.id_table = id_table,
1776	.num_interrupt_in = 1,
1777	.open = mos7840_open,
1778	.close = mos7840_close,
1779	.write = mos7840_write,
1780	.write_room = mos7840_write_room,
1781	.chars_in_buffer = mos7840_chars_in_buffer,
1782	.throttle = mos7840_throttle,
1783	.unthrottle = mos7840_unthrottle,
1784	.calc_num_ports = mos7840_calc_num_ports,
1785	.probe = mos7840_probe,
1786	.attach = mos7840_attach,
1787	.ioctl = mos7840_ioctl,
1788	.set_termios = mos7840_set_termios,
1789	.break_ctl = mos7840_break,
1790	.tiocmget = mos7840_tiocmget,
1791	.tiocmset = mos7840_tiocmset,
1792	.get_icount = usb_serial_generic_get_icount,
1793	.port_probe = mos7840_port_probe,
1794	.port_remove = mos7840_port_remove,
1795	.read_bulk_callback = mos7840_bulk_in_callback,
1796	.suspend = mos7840_suspend,
1797	.resume = mos7840_resume,
1798};
1799
1800static struct usb_serial_driver * const serial_drivers[] = {
1801	&moschip7840_4port_device, NULL
1802};
1803
1804module_usb_serial_driver(serial_drivers, id_table);
1805
1806MODULE_DESCRIPTION(DRIVER_DESC);
1807MODULE_LICENSE("GPL");
~/ Read-only file preview.