Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * mos7720.c
   4 *   Controls the Moschip 7720 usb to dual port serial converter
   5 *
   6 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
   7 *
 
 
 
 
   8 * Developed by:
   9 * 	Vijaya Kumar <vijaykumar.gn@gmail.com>
  10 *	Ajay Kumar <naanuajay@yahoo.com>
  11 *	Gurudeva <ngurudeva@yahoo.com>
  12 *
  13 * Cleaned up from the original by:
  14 *	Greg Kroah-Hartman <gregkh@suse.de>
  15 *
  16 * Originally based on drivers/usb/serial/io_edgeport.c which is:
  17 *	Copyright (C) 2000 Inside Out Networks, All rights reserved.
  18 *	Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  19 */
  20#include <linux/kernel.h>
  21#include <linux/errno.h>
  22#include <linux/slab.h>
  23#include <linux/tty.h>
  24#include <linux/tty_driver.h>
  25#include <linux/tty_flip.h>
  26#include <linux/module.h>
  27#include <linux/spinlock.h>
  28#include <linux/serial.h>
  29#include <linux/serial_reg.h>
  30#include <linux/usb.h>
  31#include <linux/usb/serial.h>
  32#include <linux/uaccess.h>
  33#include <linux/parport.h>
  34
  35#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  36#define DRIVER_DESC "Moschip USB Serial Driver"
  37
  38/* default urb timeout */
  39#define MOS_WDR_TIMEOUT	5000
  40
  41#define MOS_MAX_PORT	0x02
  42#define MOS_WRITE	0x0E
  43#define MOS_READ	0x0D
  44
  45/* Interrupt Routines Defines	*/
  46#define SERIAL_IIR_RLS	0x06
  47#define SERIAL_IIR_RDA	0x04
  48#define SERIAL_IIR_CTI	0x0c
  49#define SERIAL_IIR_THR	0x02
  50#define SERIAL_IIR_MS	0x00
  51
  52#define NUM_URBS			16	/* URB Count */
  53#define URB_TRANSFER_BUFFER_SIZE	32	/* URB Size */
  54
  55/* This structure holds all of the local serial port information */
  56struct moschip_port {
  57	__u8	shadowLCR;		/* last LCR value received */
  58	__u8	shadowMCR;		/* last MCR value received */
  59	__u8	shadowMSR;		/* last MSR value received */
  60	char			open;
  61	struct usb_serial_port	*port;	/* loop back to the owner */
  62	struct urb		*write_urb_pool[NUM_URBS];
  63};
  64
 
 
  65#define USB_VENDOR_ID_MOSCHIP		0x9710
  66#define MOSCHIP_DEVICE_ID_7720		0x7720
  67#define MOSCHIP_DEVICE_ID_7715		0x7715
  68
  69static const struct usb_device_id id_table[] = {
  70	{ USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  71	{ USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
  72	{ } /* terminating entry */
  73};
  74MODULE_DEVICE_TABLE(usb, id_table);
  75
  76#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  77
  78/* initial values for parport regs */
  79#define DCR_INIT_VAL       0x0c	/* SLCTIN, nINIT */
  80#define ECR_INIT_VAL       0x00	/* SPP mode */
  81
 
 
 
 
 
 
 
 
  82enum mos7715_pp_modes {
  83	SPP = 0<<5,
  84	PS2 = 1<<5,      /* moschip calls this 'NIBBLE' mode */
  85	PPF = 2<<5,	 /* moschip calls this 'CB-FIFO mode */
  86};
  87
  88struct mos7715_parport {
  89	struct parport          *pp;	       /* back to containing struct */
  90	struct kref             ref_count;     /* to instance of this struct */
 
 
 
  91	bool                    msg_pending;   /* usb sync call pending */
  92	struct completion       syncmsg_compl; /* usb sync call completed */
  93	struct work_struct      work;          /* restore deferred writes */
  94	struct usb_serial       *serial;       /* back to containing struct */
  95	__u8	                shadowECR;     /* parallel port regs... */
  96	__u8	                shadowDCR;
  97	atomic_t                shadowDSR;     /* updated in int-in callback */
  98};
  99
 100/* lock guards against dereferencing NULL ptr in parport ops callbacks */
 101static DEFINE_SPINLOCK(release_lock);
 102
 103#endif	/* CONFIG_USB_SERIAL_MOS7715_PARPORT */
 104
 105static const unsigned int dummy; /* for clarity in register access fns */
 106
 107enum mos_regs {
 108	MOS7720_THR,		  /* serial port regs */
 109	MOS7720_RHR,
 110	MOS7720_IER,
 111	MOS7720_FCR,
 112	MOS7720_ISR,
 113	MOS7720_LCR,
 114	MOS7720_MCR,
 115	MOS7720_LSR,
 116	MOS7720_MSR,
 117	MOS7720_SPR,
 118	MOS7720_DLL,
 119	MOS7720_DLM,
 120	MOS7720_DPR,		  /* parallel port regs */
 121	MOS7720_DSR,
 122	MOS7720_DCR,
 123	MOS7720_ECR,
 124	MOS7720_SP1_REG,	  /* device control regs */
 125	MOS7720_SP2_REG,	  /* serial port 2 (7720 only) */
 126	MOS7720_PP_REG,
 127	MOS7720_SP_CONTROL_REG,
 128};
 129
 130/*
 131 * Return the correct value for the Windex field of the setup packet
 132 * for a control endpoint message.  See the 7715 datasheet.
 133 */
 134static inline __u16 get_reg_index(enum mos_regs reg)
 135{
 136	static const __u16 mos7715_index_lookup_table[] = {
 137		0x00,		/* MOS7720_THR */
 138		0x00,		/* MOS7720_RHR */
 139		0x01,		/* MOS7720_IER */
 140		0x02,		/* MOS7720_FCR */
 141		0x02,		/* MOS7720_ISR */
 142		0x03,		/* MOS7720_LCR */
 143		0x04,		/* MOS7720_MCR */
 144		0x05,		/* MOS7720_LSR */
 145		0x06,		/* MOS7720_MSR */
 146		0x07,		/* MOS7720_SPR */
 147		0x00,		/* MOS7720_DLL */
 148		0x01,		/* MOS7720_DLM */
 149		0x00,		/* MOS7720_DPR */
 150		0x01,		/* MOS7720_DSR */
 151		0x02,		/* MOS7720_DCR */
 152		0x0a,		/* MOS7720_ECR */
 153		0x01,		/* MOS7720_SP1_REG */
 154		0x02,		/* MOS7720_SP2_REG (7720 only) */
 155		0x04,		/* MOS7720_PP_REG (7715 only) */
 156		0x08,		/* MOS7720_SP_CONTROL_REG */
 157	};
 158	return mos7715_index_lookup_table[reg];
 159}
 160
 161/*
 162 * Return the correct value for the upper byte of the Wvalue field of
 163 * the setup packet for a control endpoint message.
 164 */
 165static inline __u16 get_reg_value(enum mos_regs reg,
 166				  unsigned int serial_portnum)
 167{
 168	if (reg >= MOS7720_SP1_REG)	/* control reg */
 169		return 0x0000;
 170
 171	else if (reg >= MOS7720_DPR)	/* parallel port reg (7715 only) */
 172		return 0x0100;
 173
 174	else			      /* serial port reg */
 175		return (serial_portnum + 2) << 8;
 176}
 177
 178/*
 179 * Write data byte to the specified device register.  The data is embedded in
 180 * the value field of the setup packet. serial_portnum is ignored for registers
 181 * not specific to a particular serial port.
 182 */
 183static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 184			 enum mos_regs reg, __u8 data)
 185{
 186	struct usb_device *usbdev = serial->dev;
 187	unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
 188	__u8 request = (__u8)0x0e;
 189	__u8 requesttype = (__u8)0x40;
 190	__u16 index = get_reg_index(reg);
 191	__u16 value = get_reg_value(reg, serial_portnum) + data;
 192	int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
 193				     index, NULL, 0, MOS_WDR_TIMEOUT);
 194	if (status < 0)
 195		dev_err(&usbdev->dev,
 196			"mos7720: usb_control_msg() failed: %d\n", status);
 197	return status;
 198}
 199
 200/*
 201 * Read data byte from the specified device register.  The data returned by the
 202 * device is embedded in the value field of the setup packet.  serial_portnum is
 203 * ignored for registers that are not specific to a particular serial port.
 204 */
 205static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 206			enum mos_regs reg, __u8 *data)
 207{
 208	struct usb_device *usbdev = serial->dev;
 209	unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
 210	__u8 request = (__u8)0x0d;
 211	__u8 requesttype = (__u8)0xc0;
 212	__u16 index = get_reg_index(reg);
 213	__u16 value = get_reg_value(reg, serial_portnum);
 214	u8 *buf;
 215	int status;
 216
 217	buf = kmalloc(1, GFP_KERNEL);
 218	if (!buf) {
 219		*data = 0;
 220		return -ENOMEM;
 221	}
 222
 223	status = usb_control_msg(usbdev, pipe, request, requesttype, value,
 224				     index, buf, 1, MOS_WDR_TIMEOUT);
 225	if (status == 1) {
 226		*data = *buf;
 227	} else {
 228		dev_err(&usbdev->dev,
 229			"mos7720: usb_control_msg() failed: %d\n", status);
 230		if (status >= 0)
 231			status = -EIO;
 232		*data = 0;
 233	}
 234
 235	kfree(buf);
 236
 237	return status;
 238}
 239
 240#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
 241
 242static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
 243				      enum mos7715_pp_modes mode)
 244{
 245	mos_parport->shadowECR = mode;
 246	write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
 247		      mos_parport->shadowECR);
 248	return 0;
 249}
 250
 251static void destroy_mos_parport(struct kref *kref)
 252{
 253	struct mos7715_parport *mos_parport =
 254		container_of(kref, struct mos7715_parport, ref_count);
 255
 256	kfree(mos_parport);
 257}
 258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 259/*
 260 * This is the common top part of all parallel port callback operations that
 261 * send synchronous messages to the device.  This implements convoluted locking
 262 * that avoids two scenarios: (1) a port operation is called after usbserial
 263 * has called our release function, at which point struct mos7715_parport has
 264 * been destroyed, and (2) the device has been disconnected, but usbserial has
 265 * not called the release function yet because someone has a serial port open.
 266 * The shared release_lock prevents the first, and the mutex and disconnected
 267 * flag maintained by usbserial covers the second.  We also use the msg_pending
 268 * flag to ensure that all synchronous usb message calls have completed before
 269 * our release function can return.
 270 */
 271static int parport_prologue(struct parport *pp)
 272{
 273	struct mos7715_parport *mos_parport;
 274
 275	spin_lock(&release_lock);
 276	mos_parport = pp->private_data;
 277	if (unlikely(mos_parport == NULL)) {
 278		/* release fn called, port struct destroyed */
 279		spin_unlock(&release_lock);
 280		return -1;
 281	}
 282	mos_parport->msg_pending = true;   /* synch usb call pending */
 283	reinit_completion(&mos_parport->syncmsg_compl);
 284	spin_unlock(&release_lock);
 285
 286	/* ensure writes from restore are submitted before new requests */
 287	if (work_pending(&mos_parport->work))
 288		flush_work(&mos_parport->work);
 289
 290	mutex_lock(&mos_parport->serial->disc_mutex);
 291	if (mos_parport->serial->disconnected) {
 292		/* device disconnected */
 293		mutex_unlock(&mos_parport->serial->disc_mutex);
 294		mos_parport->msg_pending = false;
 295		complete(&mos_parport->syncmsg_compl);
 296		return -1;
 297	}
 298
 299	return 0;
 300}
 301
 302/*
 303 * This is the common bottom part of all parallel port functions that send
 304 * synchronous messages to the device.
 305 */
 306static inline void parport_epilogue(struct parport *pp)
 307{
 308	struct mos7715_parport *mos_parport = pp->private_data;
 309	mutex_unlock(&mos_parport->serial->disc_mutex);
 310	mos_parport->msg_pending = false;
 311	complete(&mos_parport->syncmsg_compl);
 312}
 313
 314static void deferred_restore_writes(struct work_struct *work)
 315{
 316	struct mos7715_parport *mos_parport;
 317
 318	mos_parport = container_of(work, struct mos7715_parport, work);
 319
 320	mutex_lock(&mos_parport->serial->disc_mutex);
 321
 322	/* if device disconnected, game over */
 323	if (mos_parport->serial->disconnected)
 324		goto done;
 325
 326	write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
 327		      mos_parport->shadowDCR);
 328	write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
 329		      mos_parport->shadowECR);
 330done:
 331	mutex_unlock(&mos_parport->serial->disc_mutex);
 332}
 333
 334static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
 335{
 336	struct mos7715_parport *mos_parport = pp->private_data;
 337
 338	if (parport_prologue(pp) < 0)
 339		return;
 340	mos7715_change_mode(mos_parport, SPP);
 341	write_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, (__u8)d);
 342	parport_epilogue(pp);
 343}
 344
 345static unsigned char parport_mos7715_read_data(struct parport *pp)
 346{
 347	struct mos7715_parport *mos_parport = pp->private_data;
 348	unsigned char d;
 349
 350	if (parport_prologue(pp) < 0)
 351		return 0;
 352	read_mos_reg(mos_parport->serial, dummy, MOS7720_DPR, &d);
 353	parport_epilogue(pp);
 354	return d;
 355}
 356
 357static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
 358{
 359	struct mos7715_parport *mos_parport = pp->private_data;
 360	__u8 data;
 361
 362	if (parport_prologue(pp) < 0)
 363		return;
 364	data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
 365	write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR, data);
 366	mos_parport->shadowDCR = data;
 367	parport_epilogue(pp);
 368}
 369
 370static unsigned char parport_mos7715_read_control(struct parport *pp)
 371{
 372	struct mos7715_parport *mos_parport;
 373	__u8 dcr;
 374
 375	spin_lock(&release_lock);
 376	mos_parport = pp->private_data;
 377	if (unlikely(mos_parport == NULL)) {
 378		spin_unlock(&release_lock);
 379		return 0;
 380	}
 381	dcr = mos_parport->shadowDCR & 0x0f;
 382	spin_unlock(&release_lock);
 383	return dcr;
 384}
 385
 386static unsigned char parport_mos7715_frob_control(struct parport *pp,
 387						  unsigned char mask,
 388						  unsigned char val)
 389{
 390	struct mos7715_parport *mos_parport = pp->private_data;
 391	__u8 dcr;
 392
 393	mask &= 0x0f;
 394	val &= 0x0f;
 395	if (parport_prologue(pp) < 0)
 396		return 0;
 397	mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
 398	write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
 399		      mos_parport->shadowDCR);
 400	dcr = mos_parport->shadowDCR & 0x0f;
 401	parport_epilogue(pp);
 402	return dcr;
 403}
 404
 405static unsigned char parport_mos7715_read_status(struct parport *pp)
 406{
 407	unsigned char status;
 408	struct mos7715_parport *mos_parport;
 409
 410	spin_lock(&release_lock);
 411	mos_parport = pp->private_data;
 412	if (unlikely(mos_parport == NULL)) {	/* release called */
 413		spin_unlock(&release_lock);
 414		return 0;
 415	}
 416	status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
 417	spin_unlock(&release_lock);
 418	return status;
 419}
 420
 421static void parport_mos7715_enable_irq(struct parport *pp)
 422{
 423}
 424
 425static void parport_mos7715_disable_irq(struct parport *pp)
 426{
 427}
 428
 429static void parport_mos7715_data_forward(struct parport *pp)
 430{
 431	struct mos7715_parport *mos_parport = pp->private_data;
 432
 433	if (parport_prologue(pp) < 0)
 434		return;
 435	mos7715_change_mode(mos_parport, PS2);
 436	mos_parport->shadowDCR &=  ~0x20;
 437	write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
 438		      mos_parport->shadowDCR);
 439	parport_epilogue(pp);
 440}
 441
 442static void parport_mos7715_data_reverse(struct parport *pp)
 443{
 444	struct mos7715_parport *mos_parport = pp->private_data;
 445
 446	if (parport_prologue(pp) < 0)
 447		return;
 448	mos7715_change_mode(mos_parport, PS2);
 449	mos_parport->shadowDCR |= 0x20;
 450	write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
 451		      mos_parport->shadowDCR);
 452	parport_epilogue(pp);
 453}
 454
 455static void parport_mos7715_init_state(struct pardevice *dev,
 456				       struct parport_state *s)
 457{
 458	s->u.pc.ctr = DCR_INIT_VAL;
 459	s->u.pc.ecr = ECR_INIT_VAL;
 460}
 461
 462/* N.B. Parport core code requires that this function not block */
 463static void parport_mos7715_save_state(struct parport *pp,
 464				       struct parport_state *s)
 465{
 466	struct mos7715_parport *mos_parport;
 467
 468	spin_lock(&release_lock);
 469	mos_parport = pp->private_data;
 470	if (unlikely(mos_parport == NULL)) {	/* release called */
 471		spin_unlock(&release_lock);
 472		return;
 473	}
 474	s->u.pc.ctr = mos_parport->shadowDCR;
 475	s->u.pc.ecr = mos_parport->shadowECR;
 476	spin_unlock(&release_lock);
 477}
 478
 479/* N.B. Parport core code requires that this function not block */
 480static void parport_mos7715_restore_state(struct parport *pp,
 481					  struct parport_state *s)
 482{
 483	struct mos7715_parport *mos_parport;
 484
 485	spin_lock(&release_lock);
 486	mos_parport = pp->private_data;
 487	if (unlikely(mos_parport == NULL)) {	/* release called */
 488		spin_unlock(&release_lock);
 489		return;
 490	}
 491	mos_parport->shadowDCR = s->u.pc.ctr;
 492	mos_parport->shadowECR = s->u.pc.ecr;
 493
 494	schedule_work(&mos_parport->work);
 495	spin_unlock(&release_lock);
 496}
 497
 498static size_t parport_mos7715_write_compat(struct parport *pp,
 499					   const void *buffer,
 500					   size_t len, int flags)
 501{
 502	int retval;
 503	struct mos7715_parport *mos_parport = pp->private_data;
 504	int actual_len;
 505
 506	if (parport_prologue(pp) < 0)
 507		return 0;
 508	mos7715_change_mode(mos_parport, PPF);
 509	retval = usb_bulk_msg(mos_parport->serial->dev,
 510			      usb_sndbulkpipe(mos_parport->serial->dev, 2),
 511			      (void *)buffer, len, &actual_len,
 512			      MOS_WDR_TIMEOUT);
 513	parport_epilogue(pp);
 514	if (retval) {
 515		dev_err(&mos_parport->serial->dev->dev,
 516			"mos7720: usb_bulk_msg() failed: %d\n", retval);
 517		return 0;
 518	}
 519	return actual_len;
 520}
 521
 522static struct parport_operations parport_mos7715_ops = {
 523	.owner =		THIS_MODULE,
 524	.write_data =		parport_mos7715_write_data,
 525	.read_data =		parport_mos7715_read_data,
 526
 527	.write_control =	parport_mos7715_write_control,
 528	.read_control =		parport_mos7715_read_control,
 529	.frob_control =		parport_mos7715_frob_control,
 530
 531	.read_status =		parport_mos7715_read_status,
 532
 533	.enable_irq =		parport_mos7715_enable_irq,
 534	.disable_irq =		parport_mos7715_disable_irq,
 535
 536	.data_forward =		parport_mos7715_data_forward,
 537	.data_reverse =		parport_mos7715_data_reverse,
 538
 539	.init_state =		parport_mos7715_init_state,
 540	.save_state =		parport_mos7715_save_state,
 541	.restore_state =	parport_mos7715_restore_state,
 542
 543	.compat_write_data =	parport_mos7715_write_compat,
 544
 545	.nibble_read_data =	parport_ieee1284_read_nibble,
 546	.byte_read_data =	parport_ieee1284_read_byte,
 547};
 548
 549/*
 550 * Allocate and initialize parallel port control struct, initialize
 551 * the parallel port hardware device, and register with the parport subsystem.
 552 */
 553static int mos7715_parport_init(struct usb_serial *serial)
 554{
 555	struct mos7715_parport *mos_parport;
 556
 557	/* allocate and initialize parallel port control struct */
 558	mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
 559	if (!mos_parport)
 560		return -ENOMEM;
 561
 562	mos_parport->msg_pending = false;
 563	kref_init(&mos_parport->ref_count);
 
 
 
 564	usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
 565	mos_parport->serial = serial;
 566	INIT_WORK(&mos_parport->work, deferred_restore_writes);
 
 567	init_completion(&mos_parport->syncmsg_compl);
 568
 569	/* cycle parallel port reset bit */
 570	write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x80);
 571	write_mos_reg(mos_parport->serial, dummy, MOS7720_PP_REG, (__u8)0x00);
 572
 573	/* initialize device registers */
 574	mos_parport->shadowDCR = DCR_INIT_VAL;
 575	write_mos_reg(mos_parport->serial, dummy, MOS7720_DCR,
 576		      mos_parport->shadowDCR);
 577	mos_parport->shadowECR = ECR_INIT_VAL;
 578	write_mos_reg(mos_parport->serial, dummy, MOS7720_ECR,
 579		      mos_parport->shadowECR);
 580
 581	/* register with parport core */
 582	mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
 583						PARPORT_DMA_NONE,
 584						&parport_mos7715_ops);
 585	if (mos_parport->pp == NULL) {
 586		dev_err(&serial->interface->dev,
 587			"Could not register parport\n");
 588		kref_put(&mos_parport->ref_count, destroy_mos_parport);
 589		return -EIO;
 590	}
 591	mos_parport->pp->private_data = mos_parport;
 592	mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
 593	mos_parport->pp->dev = &serial->interface->dev;
 594	parport_announce_port(mos_parport->pp);
 595
 596	return 0;
 597}
 598#endif	/* CONFIG_USB_SERIAL_MOS7715_PARPORT */
 599
 600/*
 601 * mos7720_interrupt_callback
 602 *	this is the callback function for when we have received data on the
 603 *	interrupt endpoint.
 604 */
 605static void mos7720_interrupt_callback(struct urb *urb)
 606{
 607	int result;
 608	int length;
 609	int status = urb->status;
 610	struct device *dev = &urb->dev->dev;
 611	__u8 *data;
 612	__u8 sp1;
 613	__u8 sp2;
 614
 615	switch (status) {
 616	case 0:
 617		/* success */
 618		break;
 619	case -ECONNRESET:
 620	case -ENOENT:
 621	case -ESHUTDOWN:
 622		/* this urb is terminated, clean up */
 623		dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
 624		return;
 625	default:
 626		dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
 627		goto exit;
 628	}
 629
 630	length = urb->actual_length;
 631	data = urb->transfer_buffer;
 632
 633	/* Moschip get 4 bytes
 634	 * Byte 1 IIR Port 1 (port.number is 0)
 635	 * Byte 2 IIR Port 2 (port.number is 1)
 636	 * Byte 3 --------------
 637	 * Byte 4 FIFO status for both */
 638
 639	/* the above description is inverted
 640	 * 	oneukum 2007-03-14 */
 641
 642	if (unlikely(length != 4)) {
 643		dev_dbg(dev, "Wrong data !!!\n");
 644		return;
 645	}
 646
 647	sp1 = data[3];
 648	sp2 = data[2];
 649
 650	if ((sp1 | sp2) & 0x01) {
 651		/* No Interrupt Pending in both the ports */
 652		dev_dbg(dev, "No Interrupt !!!\n");
 653	} else {
 654		switch (sp1 & 0x0f) {
 655		case SERIAL_IIR_RLS:
 656			dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
 657			break;
 658		case SERIAL_IIR_CTI:
 659			dev_dbg(dev, "Serial Port 1: Receiver time out\n");
 660			break;
 661		case SERIAL_IIR_MS:
 662			/* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
 663			break;
 664		}
 665
 666		switch (sp2 & 0x0f) {
 667		case SERIAL_IIR_RLS:
 668			dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
 669			break;
 670		case SERIAL_IIR_CTI:
 671			dev_dbg(dev, "Serial Port 2: Receiver time out\n");
 672			break;
 673		case SERIAL_IIR_MS:
 674			/* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
 675			break;
 676		}
 677	}
 678
 679exit:
 680	result = usb_submit_urb(urb, GFP_ATOMIC);
 681	if (result)
 682		dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
 683}
 684
 685/*
 686 * mos7715_interrupt_callback
 687 *	this is the 7715's callback function for when we have received data on
 688 *	the interrupt endpoint.
 689 */
 690static void mos7715_interrupt_callback(struct urb *urb)
 691{
 692	int result;
 693	int length;
 694	int status = urb->status;
 695	struct device *dev = &urb->dev->dev;
 696	__u8 *data;
 697	__u8 iir;
 698
 699	switch (status) {
 700	case 0:
 701		/* success */
 702		break;
 703	case -ECONNRESET:
 704	case -ENOENT:
 705	case -ESHUTDOWN:
 706	case -ENODEV:
 707		/* this urb is terminated, clean up */
 708		dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
 709		return;
 710	default:
 711		dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
 712		goto exit;
 713	}
 714
 715	length = urb->actual_length;
 716	data = urb->transfer_buffer;
 717
 718	/* Structure of data from 7715 device:
 719	 * Byte 1: IIR serial Port
 720	 * Byte 2: unused
 721	 * Byte 2: DSR parallel port
 722	 * Byte 4: FIFO status for both */
 723
 724	if (unlikely(length != 4)) {
 725		dev_dbg(dev, "Wrong data !!!\n");
 726		return;
 727	}
 728
 729	iir = data[0];
 730	if (!(iir & 0x01)) {	/* serial port interrupt pending */
 731		switch (iir & 0x0f) {
 732		case SERIAL_IIR_RLS:
 733			dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
 734			break;
 735		case SERIAL_IIR_CTI:
 736			dev_dbg(dev, "Serial Port: Receiver time out\n");
 737			break;
 738		case SERIAL_IIR_MS:
 739			/* dev_dbg(dev, "Serial Port: Modem status change\n"); */
 740			break;
 741		}
 742	}
 743
 744#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
 745	{       /* update local copy of DSR reg */
 746		struct usb_serial_port *port = urb->context;
 747		struct mos7715_parport *mos_parport = port->serial->private;
 748		if (unlikely(mos_parport == NULL))
 749			return;
 750		atomic_set(&mos_parport->shadowDSR, data[2]);
 751	}
 752#endif
 753
 754exit:
 755	result = usb_submit_urb(urb, GFP_ATOMIC);
 756	if (result)
 757		dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
 758}
 759
 760/*
 761 * mos7720_bulk_in_callback
 762 *	this is the callback function for when we have received data on the
 763 *	bulk in endpoint.
 764 */
 765static void mos7720_bulk_in_callback(struct urb *urb)
 766{
 767	int retval;
 768	unsigned char *data ;
 769	struct usb_serial_port *port;
 770	int status = urb->status;
 771
 772	if (status) {
 773		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
 774		return;
 775	}
 776
 777	port = urb->context;
 778
 779	dev_dbg(&port->dev, "Entering...%s\n", __func__);
 780
 781	data = urb->transfer_buffer;
 782
 783	if (urb->actual_length) {
 784		tty_insert_flip_string(&port->port, data, urb->actual_length);
 785		tty_flip_buffer_push(&port->port);
 786	}
 787
 788	if (port->read_urb->status != -EINPROGRESS) {
 789		retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
 790		if (retval)
 791			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
 792	}
 793}
 794
 795/*
 796 * mos7720_bulk_out_data_callback
 797 *	this is the callback function for when we have finished sending serial
 798 *	data on the bulk out endpoint.
 799 */
 800static void mos7720_bulk_out_data_callback(struct urb *urb)
 801{
 802	struct moschip_port *mos7720_port;
 803	int status = urb->status;
 804
 805	if (status) {
 806		dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
 807		return;
 808	}
 809
 810	mos7720_port = urb->context;
 811	if (!mos7720_port) {
 812		dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
 813		return ;
 814	}
 815
 816	if (mos7720_port->open)
 817		tty_port_tty_wakeup(&mos7720_port->port->port);
 818}
 819
 820static int mos77xx_calc_num_ports(struct usb_serial *serial,
 821					struct usb_serial_endpoints *epds)
 
 
 
 
 
 
 822{
 823	u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
 
 
 
 
 
 824
 825	if (product == MOSCHIP_DEVICE_ID_7715) {
 826		/*
 827		 * The 7715 uses the first bulk in/out endpoint pair for the
 828		 * parallel port, and the second for the serial port. We swap
 829		 * the endpoint descriptors here so that the first and
 830		 * only registered port structure uses the serial-port
 831		 * endpoints.
 832		 */
 833		swap(epds->bulk_in[0], epds->bulk_in[1]);
 834		swap(epds->bulk_out[0], epds->bulk_out[1]);
 835
 
 
 
 
 836		return 1;
 837	}
 838
 839	return 2;
 840}
 841
 842static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
 843{
 844	struct usb_serial *serial;
 845	struct urb *urb;
 846	struct moschip_port *mos7720_port;
 847	int response;
 848	int port_number;
 849	__u8 data;
 850	int allocated_urbs = 0;
 851	int j;
 852
 853	serial = port->serial;
 854
 855	mos7720_port = usb_get_serial_port_data(port);
 856	if (mos7720_port == NULL)
 857		return -ENODEV;
 858
 859	usb_clear_halt(serial->dev, port->write_urb->pipe);
 860	usb_clear_halt(serial->dev, port->read_urb->pipe);
 861
 862	/* Initialising the write urb pool */
 863	for (j = 0; j < NUM_URBS; ++j) {
 864		urb = usb_alloc_urb(0, GFP_KERNEL);
 865		mos7720_port->write_urb_pool[j] = urb;
 866		if (!urb)
 867			continue;
 868
 869		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
 870					       GFP_KERNEL);
 871		if (!urb->transfer_buffer) {
 872			usb_free_urb(mos7720_port->write_urb_pool[j]);
 873			mos7720_port->write_urb_pool[j] = NULL;
 874			continue;
 875		}
 876		allocated_urbs++;
 877	}
 878
 879	if (!allocated_urbs)
 880		return -ENOMEM;
 881
 882	 /* Initialize MCS7720 -- Write Init values to corresponding Registers
 883	  *
 884	  * Register Index
 885	  * 0 : MOS7720_THR/MOS7720_RHR
 886	  * 1 : MOS7720_IER
 887	  * 2 : MOS7720_FCR
 888	  * 3 : MOS7720_LCR
 889	  * 4 : MOS7720_MCR
 890	  * 5 : MOS7720_LSR
 891	  * 6 : MOS7720_MSR
 892	  * 7 : MOS7720_SPR
 893	  *
 894	  * 0x08 : SP1/2 Control Reg
 895	  */
 896	port_number = port->port_number;
 897	read_mos_reg(serial, port_number, MOS7720_LSR, &data);
 898
 899	dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
 900
 901	write_mos_reg(serial, dummy, MOS7720_SP1_REG, 0x02);
 902	write_mos_reg(serial, dummy, MOS7720_SP2_REG, 0x02);
 903
 904	write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
 905	write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
 906
 907	write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
 908	mos7720_port->shadowLCR = 0x03;
 909	write_mos_reg(serial, port_number, MOS7720_LCR,
 910		      mos7720_port->shadowLCR);
 911	mos7720_port->shadowMCR = 0x0b;
 912	write_mos_reg(serial, port_number, MOS7720_MCR,
 913		      mos7720_port->shadowMCR);
 914
 915	write_mos_reg(serial, port_number, MOS7720_SP_CONTROL_REG, 0x00);
 916	read_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, &data);
 917	data = data | (port->port_number + 1);
 918	write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, data);
 919	mos7720_port->shadowLCR = 0x83;
 920	write_mos_reg(serial, port_number, MOS7720_LCR,
 921		      mos7720_port->shadowLCR);
 922	write_mos_reg(serial, port_number, MOS7720_THR, 0x0c);
 923	write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
 924	mos7720_port->shadowLCR = 0x03;
 925	write_mos_reg(serial, port_number, MOS7720_LCR,
 926		      mos7720_port->shadowLCR);
 927	write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
 928
 929	response = usb_submit_urb(port->read_urb, GFP_KERNEL);
 930	if (response)
 931		dev_err(&port->dev, "%s - Error %d submitting read urb\n",
 932							__func__, response);
 933
 934	/* initialize our port settings */
 935	mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
 936
 937	/* send a open port command */
 938	mos7720_port->open = 1;
 939
 940	return 0;
 941}
 942
 943/*
 944 * mos7720_chars_in_buffer
 945 *	this function is called by the tty driver when it wants to know how many
 946 *	bytes of data we currently have outstanding in the port (data that has
 947 *	been written, but hasn't made it out the port yet)
 
 
 
 948 */
 949static unsigned int mos7720_chars_in_buffer(struct tty_struct *tty)
 950{
 951	struct usb_serial_port *port = tty->driver_data;
 952	struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
 953	int i;
 954	unsigned int chars = 0;
 
 
 
 
 
 955
 956	for (i = 0; i < NUM_URBS; ++i) {
 957		if (mos7720_port->write_urb_pool[i] &&
 958		    mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
 959			chars += URB_TRANSFER_BUFFER_SIZE;
 960	}
 961	dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
 962	return chars;
 963}
 964
 965static void mos7720_close(struct usb_serial_port *port)
 966{
 967	struct usb_serial *serial;
 968	struct moschip_port *mos7720_port;
 969	int j;
 970
 971	serial = port->serial;
 972
 973	mos7720_port = usb_get_serial_port_data(port);
 974	if (mos7720_port == NULL)
 975		return;
 976
 977	for (j = 0; j < NUM_URBS; ++j)
 978		usb_kill_urb(mos7720_port->write_urb_pool[j]);
 979
 980	/* Freeing Write URBs */
 981	for (j = 0; j < NUM_URBS; ++j) {
 982		if (mos7720_port->write_urb_pool[j]) {
 983			kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
 984			usb_free_urb(mos7720_port->write_urb_pool[j]);
 985		}
 986	}
 987
 988	/* While closing port, shutdown all bulk read, write  *
 989	 * and interrupt read if they exists, otherwise nop   */
 990	usb_kill_urb(port->write_urb);
 991	usb_kill_urb(port->read_urb);
 992
 993	write_mos_reg(serial, port->port_number, MOS7720_MCR, 0x00);
 994	write_mos_reg(serial, port->port_number, MOS7720_IER, 0x00);
 995
 996	mos7720_port->open = 0;
 997}
 998
 999static int mos7720_break(struct tty_struct *tty, int break_state)
1000{
1001	struct usb_serial_port *port = tty->driver_data;
1002	unsigned char data;
1003	struct usb_serial *serial;
1004	struct moschip_port *mos7720_port;
1005
1006	serial = port->serial;
1007
1008	mos7720_port = usb_get_serial_port_data(port);
1009	if (mos7720_port == NULL)
1010		return -ENODEV;
1011
1012	if (break_state == -1)
1013		data = mos7720_port->shadowLCR | UART_LCR_SBC;
1014	else
1015		data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1016
1017	mos7720_port->shadowLCR  = data;
1018
1019	return write_mos_reg(serial, port->port_number, MOS7720_LCR,
1020			     mos7720_port->shadowLCR);
1021}
1022
1023/*
1024 * mos7720_write_room
1025 *	this function is called by the tty driver when it wants to know how many
1026 *	bytes of data we can accept for a specific port.
 
 
1027 */
1028static unsigned int mos7720_write_room(struct tty_struct *tty)
1029{
1030	struct usb_serial_port *port = tty->driver_data;
1031	struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1032	unsigned int room = 0;
1033	int i;
1034
 
 
 
 
1035	/* FIXME: Locking */
1036	for (i = 0; i < NUM_URBS; ++i) {
1037		if (mos7720_port->write_urb_pool[i] &&
1038		    mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1039			room += URB_TRANSFER_BUFFER_SIZE;
1040	}
1041
1042	dev_dbg(&port->dev, "%s - returns %u\n", __func__, room);
1043	return room;
1044}
1045
1046static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1047				 const unsigned char *data, int count)
1048{
1049	int status;
1050	int i;
1051	int bytes_sent = 0;
1052	int transfer_size;
1053
1054	struct moschip_port *mos7720_port;
1055	struct usb_serial *serial;
1056	struct urb    *urb;
1057	const unsigned char *current_position = data;
1058
1059	serial = port->serial;
1060
1061	mos7720_port = usb_get_serial_port_data(port);
1062	if (mos7720_port == NULL)
1063		return -ENODEV;
1064
1065	/* try to find a free urb in the list */
1066	urb = NULL;
1067
1068	for (i = 0; i < NUM_URBS; ++i) {
1069		if (mos7720_port->write_urb_pool[i] &&
1070		    mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1071			urb = mos7720_port->write_urb_pool[i];
1072			dev_dbg(&port->dev, "URB:%d\n", i);
1073			break;
1074		}
1075	}
1076
1077	if (urb == NULL) {
1078		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1079		goto exit;
1080	}
1081
1082	if (urb->transfer_buffer == NULL) {
1083		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1084					       GFP_ATOMIC);
1085		if (!urb->transfer_buffer) {
1086			bytes_sent = -ENOMEM;
1087			goto exit;
1088		}
1089	}
1090	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1091
1092	memcpy(urb->transfer_buffer, current_position, transfer_size);
1093	usb_serial_debug_data(&port->dev, __func__, transfer_size,
1094			      urb->transfer_buffer);
1095
1096	/* fill urb with data and submit  */
1097	usb_fill_bulk_urb(urb, serial->dev,
1098			  usb_sndbulkpipe(serial->dev,
1099					port->bulk_out_endpointAddress),
1100			  urb->transfer_buffer, transfer_size,
1101			  mos7720_bulk_out_data_callback, mos7720_port);
1102
1103	/* send it down the pipe */
1104	status = usb_submit_urb(urb, GFP_ATOMIC);
1105	if (status) {
1106		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1107			"with status = %d\n", __func__, status);
1108		bytes_sent = status;
1109		goto exit;
1110	}
1111	bytes_sent = transfer_size;
1112
1113exit:
1114	return bytes_sent;
1115}
1116
1117static void mos7720_throttle(struct tty_struct *tty)
1118{
1119	struct usb_serial_port *port = tty->driver_data;
1120	struct moschip_port *mos7720_port;
1121	int status;
1122
1123	mos7720_port = usb_get_serial_port_data(port);
1124
1125	if (mos7720_port == NULL)
1126		return;
1127
1128	if (!mos7720_port->open) {
1129		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1130		return;
1131	}
1132
1133	/* if we are implementing XON/XOFF, send the stop character */
1134	if (I_IXOFF(tty)) {
1135		unsigned char stop_char = STOP_CHAR(tty);
1136		status = mos7720_write(tty, port, &stop_char, 1);
1137		if (status <= 0)
1138			return;
1139	}
1140
1141	/* if we are implementing RTS/CTS, toggle that line */
1142	if (C_CRTSCTS(tty)) {
1143		mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1144		write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1145			      mos7720_port->shadowMCR);
 
 
1146	}
1147}
1148
1149static void mos7720_unthrottle(struct tty_struct *tty)
1150{
1151	struct usb_serial_port *port = tty->driver_data;
1152	struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1153	int status;
1154
1155	if (mos7720_port == NULL)
1156		return;
1157
1158	if (!mos7720_port->open) {
1159		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1160		return;
1161	}
1162
1163	/* if we are implementing XON/XOFF, send the start character */
1164	if (I_IXOFF(tty)) {
1165		unsigned char start_char = START_CHAR(tty);
1166		status = mos7720_write(tty, port, &start_char, 1);
1167		if (status <= 0)
1168			return;
1169	}
1170
1171	/* if we are implementing RTS/CTS, toggle that line */
1172	if (C_CRTSCTS(tty)) {
1173		mos7720_port->shadowMCR |= UART_MCR_RTS;
1174		write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1175			      mos7720_port->shadowMCR);
 
 
1176	}
1177}
1178
1179/* FIXME: this function does not work */
1180static int set_higher_rates(struct moschip_port *mos7720_port,
1181			    unsigned int baud)
1182{
1183	struct usb_serial_port *port;
1184	struct usb_serial *serial;
1185	int port_number;
1186	enum mos_regs sp_reg;
1187	if (mos7720_port == NULL)
1188		return -EINVAL;
1189
1190	port = mos7720_port->port;
1191	serial = port->serial;
1192
1193	 /***********************************************
1194	 *      Init Sequence for higher rates
1195	 ***********************************************/
1196	dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
1197	port_number = port->port_number;
1198
1199	write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1200	write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1201	write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1202	mos7720_port->shadowMCR = 0x0b;
1203	write_mos_reg(serial, port_number, MOS7720_MCR,
1204		      mos7720_port->shadowMCR);
1205	write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x00);
1206
1207	/***********************************************
1208	 *              Set for higher rates           *
1209	 ***********************************************/
1210	/* writing baud rate verbatum into uart clock field clearly not right */
1211	if (port_number == 0)
1212		sp_reg = MOS7720_SP1_REG;
1213	else
1214		sp_reg = MOS7720_SP2_REG;
1215	write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1216	write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG, 0x03);
1217	mos7720_port->shadowMCR = 0x2b;
1218	write_mos_reg(serial, port_number, MOS7720_MCR,
1219		      mos7720_port->shadowMCR);
1220
1221	/***********************************************
1222	 *              Set DLL/DLM
1223	 ***********************************************/
1224	mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1225	write_mos_reg(serial, port_number, MOS7720_LCR,
1226		      mos7720_port->shadowLCR);
1227	write_mos_reg(serial, port_number, MOS7720_DLL, 0x01);
1228	write_mos_reg(serial, port_number, MOS7720_DLM, 0x00);
1229	mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1230	write_mos_reg(serial, port_number, MOS7720_LCR,
1231		      mos7720_port->shadowLCR);
1232
1233	return 0;
1234}
1235
1236/* baud rate information */
1237struct divisor_table_entry {
1238	__u32  baudrate;
1239	__u16  divisor;
1240};
1241
1242/* Define table of divisors for moschip 7720 hardware	   *
1243 * These assume a 3.6864MHz crystal, the standard /16, and *
1244 * MCR.7 = 0.						   */
1245static const struct divisor_table_entry divisor_table[] = {
1246	{   50,		2304},
1247	{   110,	1047},	/* 2094.545455 => 230450   => .0217 % over */
1248	{   134,	857},	/* 1713.011152 => 230398.5 => .00065% under */
1249	{   150,	768},
1250	{   300,	384},
1251	{   600,	192},
1252	{   1200,	96},
1253	{   1800,	64},
1254	{   2400,	48},
1255	{   4800,	24},
1256	{   7200,	16},
1257	{   9600,	12},
1258	{   19200,	6},
1259	{   38400,	3},
1260	{   57600,	2},
1261	{   115200,	1},
1262};
1263
1264/*****************************************************************************
1265 * calc_baud_rate_divisor
1266 *	this function calculates the proper baud rate divisor for the specified
1267 *	baud rate.
1268 *****************************************************************************/
1269static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1270{
1271	int i;
1272	__u16 custom;
1273	__u16 round1;
1274	__u16 round;
1275
1276
1277	dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
1278
1279	for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1280		if (divisor_table[i].baudrate == baudrate) {
1281			*divisor = divisor_table[i].divisor;
1282			return 0;
1283		}
1284	}
1285
1286	/* After trying for all the standard baud rates    *
1287	 * Try calculating the divisor for this baud rate  */
1288	if (baudrate > 75 &&  baudrate < 230400) {
1289		/* get the divisor */
1290		custom = (__u16)(230400L  / baudrate);
1291
1292		/* Check for round off */
1293		round1 = (__u16)(2304000L / baudrate);
1294		round = (__u16)(round1 - (custom * 10));
1295		if (round > 4)
1296			custom++;
1297		*divisor = custom;
1298
1299		dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1300		return 0;
1301	}
1302
1303	dev_dbg(&port->dev, "Baud calculation Failed...\n");
1304	return -EINVAL;
1305}
1306
1307/*
1308 * send_cmd_write_baud_rate
1309 *	this function sends the proper command to change the baud rate of the
1310 *	specified port.
1311 */
1312static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1313				    int baudrate)
1314{
1315	struct usb_serial_port *port;
1316	struct usb_serial *serial;
1317	int divisor;
1318	int status;
1319	unsigned char number;
1320
1321	if (mos7720_port == NULL)
1322		return -1;
1323
1324	port = mos7720_port->port;
1325	serial = port->serial;
1326
1327	number = port->port_number;
1328	dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
1329
1330	/* Calculate the Divisor */
1331	status = calc_baud_rate_divisor(port, baudrate, &divisor);
1332	if (status) {
1333		dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1334		return status;
1335	}
1336
1337	/* Enable access to divisor latch */
1338	mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1339	write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1340
1341	/* Write the divisor */
1342	write_mos_reg(serial, number, MOS7720_DLL, (__u8)(divisor & 0xff));
1343	write_mos_reg(serial, number, MOS7720_DLM,
1344		      (__u8)((divisor & 0xff00) >> 8));
1345
1346	/* Disable access to divisor latch */
1347	mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1348	write_mos_reg(serial, number, MOS7720_LCR, mos7720_port->shadowLCR);
1349
1350	return status;
1351}
1352
1353/*
1354 * change_port_settings
1355 *	This routine is called to set the UART on the device to match
1356 *      the specified new settings.
1357 */
1358static void change_port_settings(struct tty_struct *tty,
1359				 struct moschip_port *mos7720_port,
1360				 const struct ktermios *old_termios)
1361{
1362	struct usb_serial_port *port;
1363	struct usb_serial *serial;
1364	int baud;
1365	unsigned cflag;
 
 
1366	__u8 lData;
1367	__u8 lParity;
1368	__u8 lStop;
1369	int status;
1370	int port_number;
1371
1372	if (mos7720_port == NULL)
1373		return ;
1374
1375	port = mos7720_port->port;
1376	serial = port->serial;
1377	port_number = port->port_number;
1378
1379	if (!mos7720_port->open) {
1380		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1381		return;
1382	}
1383
 
1384	lStop = 0x00;	/* 1 stop bit */
1385	lParity = 0x00;	/* No parity */
1386
1387	cflag = tty->termios.c_cflag;
 
1388
1389	lData = UART_LCR_WLEN(tty_get_char_size(cflag));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1390
1391	/* Change the Parity bit */
1392	if (cflag & PARENB) {
1393		if (cflag & PARODD) {
1394			lParity = UART_LCR_PARITY;
1395			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1396		} else {
1397			lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1398			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1399		}
1400
1401	} else {
1402		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1403	}
1404
1405	if (cflag & CMSPAR)
1406		lParity = lParity | 0x20;
1407
1408	/* Change the Stop bit */
1409	if (cflag & CSTOPB) {
1410		lStop = UART_LCR_STOP;
1411		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1412	} else {
1413		lStop = 0x00;
1414		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1415	}
1416
1417#define LCR_BITS_MASK		0x03	/* Mask for bits/char field */
1418#define LCR_STOP_MASK		0x04	/* Mask for stop bits field */
1419#define LCR_PAR_MASK		0x38	/* Mask for parity field */
1420
1421	/* Update the LCR with the correct value */
1422	mos7720_port->shadowLCR &=
1423		~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1424	mos7720_port->shadowLCR |= (lData | lParity | lStop);
1425
1426
1427	/* Disable Interrupts */
1428	write_mos_reg(serial, port_number, MOS7720_IER, 0x00);
1429	write_mos_reg(serial, port_number, MOS7720_FCR, 0x00);
1430	write_mos_reg(serial, port_number, MOS7720_FCR, 0xcf);
1431
1432	/* Send the updated LCR value to the mos7720 */
1433	write_mos_reg(serial, port_number, MOS7720_LCR,
1434		      mos7720_port->shadowLCR);
1435	mos7720_port->shadowMCR = 0x0b;
1436	write_mos_reg(serial, port_number, MOS7720_MCR,
1437		      mos7720_port->shadowMCR);
1438
1439	/* set up the MCR register and send it to the mos7720 */
1440	mos7720_port->shadowMCR = UART_MCR_OUT2;
1441	if (cflag & CBAUD)
1442		mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1443
1444	if (cflag & CRTSCTS) {
1445		mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1446		/* To set hardware flow control to the specified *
1447		 * serial port, in SP1/2_CONTROL_REG             */
1448		if (port_number)
1449			write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1450				      0x01);
1451		else
1452			write_mos_reg(serial, dummy, MOS7720_SP_CONTROL_REG,
1453				      0x02);
1454
1455	} else
1456		mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1457
1458	write_mos_reg(serial, port_number, MOS7720_MCR,
1459		      mos7720_port->shadowMCR);
1460
1461	/* Determine divisor based on baud rate */
1462	baud = tty_get_baud_rate(tty);
1463	if (!baud) {
1464		/* pick a default, any default... */
1465		dev_dbg(&port->dev, "Picked default baud...\n");
1466		baud = 9600;
1467	}
1468
1469	if (baud >= 230400) {
1470		set_higher_rates(mos7720_port, baud);
1471		/* Enable Interrupts */
1472		write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1473		return;
1474	}
1475
1476	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1477	status = send_cmd_write_baud_rate(mos7720_port, baud);
1478	/* FIXME: needs to write actual resulting baud back not just
1479	   blindly do so */
1480	if (cflag & CBAUD)
1481		tty_encode_baud_rate(tty, baud, baud);
1482	/* Enable Interrupts */
1483	write_mos_reg(serial, port_number, MOS7720_IER, 0x0c);
1484
1485	if (port->read_urb->status != -EINPROGRESS) {
1486		status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1487		if (status)
1488			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1489	}
1490}
1491
1492/*
1493 * mos7720_set_termios
1494 *	this function is called by the tty driver when it wants to change the
1495 *	termios structure.
1496 */
1497static void mos7720_set_termios(struct tty_struct *tty,
1498				struct usb_serial_port *port,
1499				const struct ktermios *old_termios)
1500{
1501	int status;
 
 
1502	struct moschip_port *mos7720_port;
1503
 
 
1504	mos7720_port = usb_get_serial_port_data(port);
1505
1506	if (mos7720_port == NULL)
1507		return;
1508
1509	if (!mos7720_port->open) {
1510		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1511		return;
1512	}
1513
 
 
 
 
 
 
 
 
 
 
1514	/* change the port settings to the new ones specified */
1515	change_port_settings(tty, mos7720_port, old_termios);
1516
1517	if (port->read_urb->status != -EINPROGRESS) {
1518		status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1519		if (status)
1520			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1521	}
1522}
1523
1524/*
1525 * get_lsr_info - get line status register info
1526 *
1527 * Purpose: Let user call ioctl() to get info when the UART physically
1528 * 	    is emptied.  On bus types like RS485, the transmitter must
1529 * 	    release the bus after transmitting. This must be done when
1530 * 	    the transmit shift register is empty, not be done when the
1531 * 	    transmit holding register is empty.  This functionality
1532 * 	    allows an RS485 driver to be written in user space.
1533 */
1534static int get_lsr_info(struct tty_struct *tty,
1535		struct moschip_port *mos7720_port, unsigned int __user *value)
1536{
1537	struct usb_serial_port *port = tty->driver_data;
1538	unsigned int result = 0;
1539	unsigned char data = 0;
1540	int port_number = port->port_number;
1541	int count;
1542
1543	count = mos7720_chars_in_buffer(tty);
1544	if (count == 0) {
1545		read_mos_reg(port->serial, port_number, MOS7720_LSR, &data);
1546		if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1547					== (UART_LSR_TEMT | UART_LSR_THRE)) {
1548			dev_dbg(&port->dev, "%s -- Empty\n", __func__);
1549			result = TIOCSER_TEMT;
1550		}
1551	}
1552	if (copy_to_user(value, &result, sizeof(int)))
1553		return -EFAULT;
1554	return 0;
1555}
1556
1557static int mos7720_tiocmget(struct tty_struct *tty)
1558{
1559	struct usb_serial_port *port = tty->driver_data;
1560	struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1561	unsigned int result = 0;
1562	unsigned int mcr ;
1563	unsigned int msr ;
1564
1565	mcr = mos7720_port->shadowMCR;
1566	msr = mos7720_port->shadowMSR;
1567
1568	result = ((mcr & UART_MCR_DTR)  ? TIOCM_DTR : 0)   /* 0x002 */
1569	  | ((mcr & UART_MCR_RTS)   ? TIOCM_RTS : 0)   /* 0x004 */
1570	  | ((msr & UART_MSR_CTS)   ? TIOCM_CTS : 0)   /* 0x020 */
1571	  | ((msr & UART_MSR_DCD)   ? TIOCM_CAR : 0)   /* 0x040 */
1572	  | ((msr & UART_MSR_RI)    ? TIOCM_RI :  0)   /* 0x080 */
1573	  | ((msr & UART_MSR_DSR)   ? TIOCM_DSR : 0);  /* 0x100 */
1574
1575	return result;
1576}
1577
1578static int mos7720_tiocmset(struct tty_struct *tty,
1579			    unsigned int set, unsigned int clear)
1580{
1581	struct usb_serial_port *port = tty->driver_data;
1582	struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1583	unsigned int mcr ;
1584
1585	mcr = mos7720_port->shadowMCR;
1586
1587	if (set & TIOCM_RTS)
1588		mcr |= UART_MCR_RTS;
1589	if (set & TIOCM_DTR)
1590		mcr |= UART_MCR_DTR;
1591	if (set & TIOCM_LOOP)
1592		mcr |= UART_MCR_LOOP;
1593
1594	if (clear & TIOCM_RTS)
1595		mcr &= ~UART_MCR_RTS;
1596	if (clear & TIOCM_DTR)
1597		mcr &= ~UART_MCR_DTR;
1598	if (clear & TIOCM_LOOP)
1599		mcr &= ~UART_MCR_LOOP;
1600
1601	mos7720_port->shadowMCR = mcr;
1602	write_mos_reg(port->serial, port->port_number, MOS7720_MCR,
1603		      mos7720_port->shadowMCR);
1604
1605	return 0;
1606}
1607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1608static int mos7720_ioctl(struct tty_struct *tty,
1609			 unsigned int cmd, unsigned long arg)
1610{
1611	struct usb_serial_port *port = tty->driver_data;
1612	struct moschip_port *mos7720_port;
1613
1614	mos7720_port = usb_get_serial_port_data(port);
1615	if (mos7720_port == NULL)
1616		return -ENODEV;
1617
1618	switch (cmd) {
1619	case TIOCSERGETLSR:
1620		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1621		return get_lsr_info(tty, mos7720_port,
1622					(unsigned int __user *)arg);
 
 
 
 
 
 
 
 
 
 
 
 
1623	}
1624
1625	return -ENOIOCTLCMD;
1626}
1627
1628static int mos7720_startup(struct usb_serial *serial)
1629{
1630	struct usb_device *dev;
1631	char data;
1632	u16 product;
1633	int ret_val;
1634
1635	product = le16_to_cpu(serial->dev->descriptor.idProduct);
1636	dev = serial->dev;
1637
 
 
 
 
 
 
 
 
 
 
 
1638	if (product == MOSCHIP_DEVICE_ID_7715) {
1639		struct urb *urb = serial->port[0]->interrupt_in_urb;
 
 
 
 
 
 
 
 
 
1640
1641		urb->complete = mos7715_interrupt_callback;
 
 
 
 
 
 
 
 
 
1642
1643#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
 
1644		ret_val = mos7715_parport_init(serial);
1645		if (ret_val < 0)
1646			return ret_val;
1647#endif
1648	}
1649	/* start the interrupt urb */
1650	ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1651	if (ret_val) {
1652		dev_err(&dev->dev, "failed to submit interrupt urb: %d\n",
1653			ret_val);
1654	}
1655
1656	/* LSR For Port 1 */
1657	read_mos_reg(serial, 0, MOS7720_LSR, &data);
1658	dev_dbg(&dev->dev, "LSR:%x\n", data);
1659
1660	return 0;
1661}
1662
1663static void mos7720_release(struct usb_serial *serial)
1664{
1665	usb_kill_urb(serial->port[0]->interrupt_in_urb);
1666
1667#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1668	/* close the parallel port */
1669
1670	if (le16_to_cpu(serial->dev->descriptor.idProduct)
1671	    == MOSCHIP_DEVICE_ID_7715) {
 
 
1672		struct mos7715_parport *mos_parport =
1673			usb_get_serial_data(serial);
1674
1675		/* prevent NULL ptr dereference in port callbacks */
1676		spin_lock(&release_lock);
1677		mos_parport->pp->private_data = NULL;
1678		spin_unlock(&release_lock);
1679
1680		/* wait for synchronous usb calls to return */
1681		if (mos_parport->msg_pending)
1682			wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1683					    msecs_to_jiffies(MOS_WDR_TIMEOUT));
1684		/*
1685		 * If delayed work is currently scheduled, wait for it to
1686		 * complete. This also implies barriers that ensure the
1687		 * below serial clearing is not hoisted above the ->work.
1688		 */
1689		cancel_work_sync(&mos_parport->work);
1690
1691		parport_remove_port(mos_parport->pp);
1692		usb_set_serial_data(serial, NULL);
1693		mos_parport->serial = NULL;
1694
1695		parport_del_port(mos_parport->pp);
 
 
 
 
 
 
 
 
 
1696
1697		kref_put(&mos_parport->ref_count, destroy_mos_parport);
1698	}
1699#endif
1700}
1701
1702static int mos7720_port_probe(struct usb_serial_port *port)
1703{
1704	struct moschip_port *mos7720_port;
1705
1706	mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
1707	if (!mos7720_port)
1708		return -ENOMEM;
1709
 
 
 
 
 
1710	mos7720_port->port = port;
1711
1712	usb_set_serial_port_data(port, mos7720_port);
1713
1714	return 0;
1715}
1716
1717static void mos7720_port_remove(struct usb_serial_port *port)
1718{
1719	struct moschip_port *mos7720_port;
1720
1721	mos7720_port = usb_get_serial_port_data(port);
1722	kfree(mos7720_port);
 
 
1723}
1724
1725static struct usb_serial_driver moschip7720_2port_driver = {
1726	.driver = {
1727		.owner =	THIS_MODULE,
1728		.name =		"moschip7720",
1729	},
1730	.description		= "Moschip 2 port adapter",
1731	.id_table		= id_table,
1732	.num_bulk_in		= 2,
1733	.num_bulk_out		= 2,
1734	.num_interrupt_in	= 1,
1735	.calc_num_ports		= mos77xx_calc_num_ports,
1736	.open			= mos7720_open,
1737	.close			= mos7720_close,
1738	.throttle		= mos7720_throttle,
1739	.unthrottle		= mos7720_unthrottle,
 
1740	.attach			= mos7720_startup,
1741	.release		= mos7720_release,
1742	.port_probe		= mos7720_port_probe,
1743	.port_remove		= mos7720_port_remove,
1744	.ioctl			= mos7720_ioctl,
1745	.tiocmget		= mos7720_tiocmget,
1746	.tiocmset		= mos7720_tiocmset,
1747	.set_termios		= mos7720_set_termios,
1748	.write			= mos7720_write,
1749	.write_room		= mos7720_write_room,
1750	.chars_in_buffer	= mos7720_chars_in_buffer,
1751	.break_ctl		= mos7720_break,
1752	.read_bulk_callback	= mos7720_bulk_in_callback,
1753	.read_int_callback	= mos7720_interrupt_callback,
1754};
1755
1756static struct usb_serial_driver * const serial_drivers[] = {
1757	&moschip7720_2port_driver, NULL
1758};
1759
1760module_usb_serial_driver(serial_drivers, id_table);
1761
1762MODULE_AUTHOR(DRIVER_AUTHOR);
1763MODULE_DESCRIPTION(DRIVER_DESC);
1764MODULE_LICENSE("GPL v2");
v3.15
 
   1/*
   2 * mos7720.c
   3 *   Controls the Moschip 7720 usb to dual port serial converter
   4 *
   5 * Copyright 2006 Moschip Semiconductor Tech. Ltd.
   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, version 2 of the License.
  10 *
  11 * Developed by:
  12 * 	Vijaya Kumar <vijaykumar.gn@gmail.com>
  13 *	Ajay Kumar <naanuajay@yahoo.com>
  14 *	Gurudeva <ngurudeva@yahoo.com>
  15 *
  16 * Cleaned up from the original by:
  17 *	Greg Kroah-Hartman <gregkh@suse.de>
  18 *
  19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20 *	Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21 *	Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22 */
  23#include <linux/kernel.h>
  24#include <linux/errno.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/module.h>
  30#include <linux/spinlock.h>
  31#include <linux/serial.h>
  32#include <linux/serial_reg.h>
  33#include <linux/usb.h>
  34#include <linux/usb/serial.h>
  35#include <linux/uaccess.h>
  36#include <linux/parport.h>
  37
  38#define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  39#define DRIVER_DESC "Moschip USB Serial Driver"
  40
  41/* default urb timeout */
  42#define MOS_WDR_TIMEOUT	5000
  43
  44#define MOS_MAX_PORT	0x02
  45#define MOS_WRITE	0x0E
  46#define MOS_READ	0x0D
  47
  48/* Interrupt Routines Defines	*/
  49#define SERIAL_IIR_RLS	0x06
  50#define SERIAL_IIR_RDA	0x04
  51#define SERIAL_IIR_CTI	0x0c
  52#define SERIAL_IIR_THR	0x02
  53#define SERIAL_IIR_MS	0x00
  54
  55#define NUM_URBS			16	/* URB Count */
  56#define URB_TRANSFER_BUFFER_SIZE	32	/* URB Size */
  57
  58/* This structure holds all of the local serial port information */
  59struct moschip_port {
  60	__u8	shadowLCR;		/* last LCR value received */
  61	__u8	shadowMCR;		/* last MCR value received */
  62	__u8	shadowMSR;		/* last MSR value received */
  63	char			open;
  64	struct usb_serial_port	*port;	/* loop back to the owner */
  65	struct urb		*write_urb_pool[NUM_URBS];
  66};
  67
  68static struct usb_serial_driver moschip7720_2port_driver;
  69
  70#define USB_VENDOR_ID_MOSCHIP		0x9710
  71#define MOSCHIP_DEVICE_ID_7720		0x7720
  72#define MOSCHIP_DEVICE_ID_7715		0x7715
  73
  74static const struct usb_device_id id_table[] = {
  75	{ USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  76	{ USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
  77	{ } /* terminating entry */
  78};
  79MODULE_DEVICE_TABLE(usb, id_table);
  80
  81#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
  82
  83/* initial values for parport regs */
  84#define DCR_INIT_VAL       0x0c	/* SLCTIN, nINIT */
  85#define ECR_INIT_VAL       0x00	/* SPP mode */
  86
  87struct urbtracker {
  88	struct mos7715_parport  *mos_parport;
  89	struct list_head        urblist_entry;
  90	struct kref             ref_count;
  91	struct urb              *urb;
  92	struct usb_ctrlrequest	*setup;
  93};
  94
  95enum mos7715_pp_modes {
  96	SPP = 0<<5,
  97	PS2 = 1<<5,      /* moschip calls this 'NIBBLE' mode */
  98	PPF = 2<<5,	 /* moschip calls this 'CB-FIFO mode */
  99};
 100
 101struct mos7715_parport {
 102	struct parport          *pp;	       /* back to containing struct */
 103	struct kref             ref_count;     /* to instance of this struct */
 104	struct list_head        deferred_urbs; /* list deferred async urbs */
 105	struct list_head        active_urbs;   /* list async urbs in flight */
 106	spinlock_t              listlock;      /* protects list access */
 107	bool                    msg_pending;   /* usb sync call pending */
 108	struct completion       syncmsg_compl; /* usb sync call completed */
 109	struct tasklet_struct   urb_tasklet;   /* for sending deferred urbs */
 110	struct usb_serial       *serial;       /* back to containing struct */
 111	__u8	                shadowECR;     /* parallel port regs... */
 112	__u8	                shadowDCR;
 113	atomic_t                shadowDSR;     /* updated in int-in callback */
 114};
 115
 116/* lock guards against dereferencing NULL ptr in parport ops callbacks */
 117static DEFINE_SPINLOCK(release_lock);
 118
 119#endif	/* CONFIG_USB_SERIAL_MOS7715_PARPORT */
 120
 121static const unsigned int dummy; /* for clarity in register access fns */
 122
 123enum mos_regs {
 124	THR,	          /* serial port regs */
 125	RHR,
 126	IER,
 127	FCR,
 128	ISR,
 129	LCR,
 130	MCR,
 131	LSR,
 132	MSR,
 133	SPR,
 134	DLL,
 135	DLM,
 136	DPR,              /* parallel port regs */
 137	DSR,
 138	DCR,
 139	ECR,
 140	SP1_REG,          /* device control regs */
 141	SP2_REG,          /* serial port 2 (7720 only) */
 142	PP_REG,
 143	SP_CONTROL_REG,
 144};
 145
 146/*
 147 * Return the correct value for the Windex field of the setup packet
 148 * for a control endpoint message.  See the 7715 datasheet.
 149 */
 150static inline __u16 get_reg_index(enum mos_regs reg)
 151{
 152	static const __u16 mos7715_index_lookup_table[] = {
 153		0x00,		/* THR */
 154		0x00,		/* RHR */
 155		0x01,		/* IER */
 156		0x02,		/* FCR */
 157		0x02,		/* ISR */
 158		0x03,		/* LCR */
 159		0x04,		/* MCR */
 160		0x05,		/* LSR */
 161		0x06,		/* MSR */
 162		0x07,		/* SPR */
 163		0x00,		/* DLL */
 164		0x01,		/* DLM */
 165		0x00,		/* DPR */
 166		0x01,		/* DSR */
 167		0x02,		/* DCR */
 168		0x0a,		/* ECR */
 169		0x01,		/* SP1_REG */
 170		0x02,		/* SP2_REG (7720 only) */
 171		0x04,		/* PP_REG (7715 only) */
 172		0x08,		/* SP_CONTROL_REG */
 173	};
 174	return mos7715_index_lookup_table[reg];
 175}
 176
 177/*
 178 * Return the correct value for the upper byte of the Wvalue field of
 179 * the setup packet for a control endpoint message.
 180 */
 181static inline __u16 get_reg_value(enum mos_regs reg,
 182				  unsigned int serial_portnum)
 183{
 184	if (reg >= SP1_REG)	      /* control reg */
 185		return 0x0000;
 186
 187	else if (reg >= DPR)	      /* parallel port reg (7715 only) */
 188		return 0x0100;
 189
 190	else			      /* serial port reg */
 191		return (serial_portnum + 2) << 8;
 192}
 193
 194/*
 195 * Write data byte to the specified device register.  The data is embedded in
 196 * the value field of the setup packet. serial_portnum is ignored for registers
 197 * not specific to a particular serial port.
 198 */
 199static int write_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 200			 enum mos_regs reg, __u8 data)
 201{
 202	struct usb_device *usbdev = serial->dev;
 203	unsigned int pipe = usb_sndctrlpipe(usbdev, 0);
 204	__u8 request = (__u8)0x0e;
 205	__u8 requesttype = (__u8)0x40;
 206	__u16 index = get_reg_index(reg);
 207	__u16 value = get_reg_value(reg, serial_portnum) + data;
 208	int status = usb_control_msg(usbdev, pipe, request, requesttype, value,
 209				     index, NULL, 0, MOS_WDR_TIMEOUT);
 210	if (status < 0)
 211		dev_err(&usbdev->dev,
 212			"mos7720: usb_control_msg() failed: %d\n", status);
 213	return status;
 214}
 215
 216/*
 217 * Read data byte from the specified device register.  The data returned by the
 218 * device is embedded in the value field of the setup packet.  serial_portnum is
 219 * ignored for registers that are not specific to a particular serial port.
 220 */
 221static int read_mos_reg(struct usb_serial *serial, unsigned int serial_portnum,
 222			enum mos_regs reg, __u8 *data)
 223{
 224	struct usb_device *usbdev = serial->dev;
 225	unsigned int pipe = usb_rcvctrlpipe(usbdev, 0);
 226	__u8 request = (__u8)0x0d;
 227	__u8 requesttype = (__u8)0xc0;
 228	__u16 index = get_reg_index(reg);
 229	__u16 value = get_reg_value(reg, serial_portnum);
 230	u8 *buf;
 231	int status;
 232
 233	buf = kmalloc(1, GFP_KERNEL);
 234	if (!buf)
 
 235		return -ENOMEM;
 
 236
 237	status = usb_control_msg(usbdev, pipe, request, requesttype, value,
 238				     index, buf, 1, MOS_WDR_TIMEOUT);
 239	if (status == 1)
 240		*data = *buf;
 241	else if (status < 0)
 242		dev_err(&usbdev->dev,
 243			"mos7720: usb_control_msg() failed: %d\n", status);
 
 
 
 
 
 244	kfree(buf);
 245
 246	return status;
 247}
 248
 249#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
 250
 251static inline int mos7715_change_mode(struct mos7715_parport *mos_parport,
 252				      enum mos7715_pp_modes mode)
 253{
 254	mos_parport->shadowECR = mode;
 255	write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
 
 256	return 0;
 257}
 258
 259static void destroy_mos_parport(struct kref *kref)
 260{
 261	struct mos7715_parport *mos_parport =
 262		container_of(kref, struct mos7715_parport, ref_count);
 263
 264	kfree(mos_parport);
 265}
 266
 267static void destroy_urbtracker(struct kref *kref)
 268{
 269	struct urbtracker *urbtrack =
 270		container_of(kref, struct urbtracker, ref_count);
 271	struct mos7715_parport *mos_parport = urbtrack->mos_parport;
 272
 273	usb_free_urb(urbtrack->urb);
 274	kfree(urbtrack->setup);
 275	kfree(urbtrack);
 276	kref_put(&mos_parport->ref_count, destroy_mos_parport);
 277}
 278
 279/*
 280 * This runs as a tasklet when sending an urb in a non-blocking parallel
 281 * port callback had to be deferred because the disconnect mutex could not be
 282 * obtained at the time.
 283 */
 284static void send_deferred_urbs(unsigned long _mos_parport)
 285{
 286	int ret_val;
 287	unsigned long flags;
 288	struct mos7715_parport *mos_parport = (void *)_mos_parport;
 289	struct urbtracker *urbtrack, *tmp;
 290	struct list_head *cursor, *next;
 291	struct device *dev;
 292
 293	/* if release function ran, game over */
 294	if (unlikely(mos_parport->serial == NULL))
 295		return;
 296
 297	dev = &mos_parport->serial->dev->dev;
 298
 299	/* try again to get the mutex */
 300	if (!mutex_trylock(&mos_parport->serial->disc_mutex)) {
 301		dev_dbg(dev, "%s: rescheduling tasklet\n", __func__);
 302		tasklet_schedule(&mos_parport->urb_tasklet);
 303		return;
 304	}
 305
 306	/* if device disconnected, game over */
 307	if (unlikely(mos_parport->serial->disconnected)) {
 308		mutex_unlock(&mos_parport->serial->disc_mutex);
 309		return;
 310	}
 311
 312	spin_lock_irqsave(&mos_parport->listlock, flags);
 313	if (list_empty(&mos_parport->deferred_urbs)) {
 314		spin_unlock_irqrestore(&mos_parport->listlock, flags);
 315		mutex_unlock(&mos_parport->serial->disc_mutex);
 316		dev_dbg(dev, "%s: deferred_urbs list empty\n", __func__);
 317		return;
 318	}
 319
 320	/* move contents of deferred_urbs list to active_urbs list and submit */
 321	list_for_each_safe(cursor, next, &mos_parport->deferred_urbs)
 322		list_move_tail(cursor, &mos_parport->active_urbs);
 323	list_for_each_entry_safe(urbtrack, tmp, &mos_parport->active_urbs,
 324			    urblist_entry) {
 325		ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
 326		dev_dbg(dev, "%s: urb submitted\n", __func__);
 327		if (ret_val) {
 328			dev_err(dev, "usb_submit_urb() failed: %d\n", ret_val);
 329			list_del(&urbtrack->urblist_entry);
 330			kref_put(&urbtrack->ref_count, destroy_urbtracker);
 331		}
 332	}
 333	spin_unlock_irqrestore(&mos_parport->listlock, flags);
 334	mutex_unlock(&mos_parport->serial->disc_mutex);
 335}
 336
 337/* callback for parallel port control urbs submitted asynchronously */
 338static void async_complete(struct urb *urb)
 339{
 340	struct urbtracker *urbtrack = urb->context;
 341	int status = urb->status;
 342
 343	if (unlikely(status))
 344		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
 345
 346	/* remove the urbtracker from the active_urbs list */
 347	spin_lock(&urbtrack->mos_parport->listlock);
 348	list_del(&urbtrack->urblist_entry);
 349	spin_unlock(&urbtrack->mos_parport->listlock);
 350	kref_put(&urbtrack->ref_count, destroy_urbtracker);
 351}
 352
 353static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
 354				      enum mos_regs reg, __u8 data)
 355{
 356	struct urbtracker *urbtrack;
 357	int ret_val;
 358	unsigned long flags;
 359	struct usb_serial *serial = mos_parport->serial;
 360	struct usb_device *usbdev = serial->dev;
 361
 362	/* create and initialize the control urb and containing urbtracker */
 363	urbtrack = kmalloc(sizeof(struct urbtracker), GFP_ATOMIC);
 364	if (!urbtrack)
 365		return -ENOMEM;
 366
 367	kref_get(&mos_parport->ref_count);
 368	urbtrack->mos_parport = mos_parport;
 369	urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
 370	if (!urbtrack->urb) {
 371		kfree(urbtrack);
 372		return -ENOMEM;
 373	}
 374	urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
 375	if (!urbtrack->setup) {
 376		usb_free_urb(urbtrack->urb);
 377		kfree(urbtrack);
 378		return -ENOMEM;
 379	}
 380	urbtrack->setup->bRequestType = (__u8)0x40;
 381	urbtrack->setup->bRequest = (__u8)0x0e;
 382	urbtrack->setup->wValue = cpu_to_le16(get_reg_value(reg, dummy));
 383	urbtrack->setup->wIndex = cpu_to_le16(get_reg_index(reg));
 384	urbtrack->setup->wLength = 0;
 385	usb_fill_control_urb(urbtrack->urb, usbdev,
 386			     usb_sndctrlpipe(usbdev, 0),
 387			     (unsigned char *)urbtrack->setup,
 388			     NULL, 0, async_complete, urbtrack);
 389	kref_init(&urbtrack->ref_count);
 390	INIT_LIST_HEAD(&urbtrack->urblist_entry);
 391
 392	/*
 393	 * get the disconnect mutex, or add tracker to the deferred_urbs list
 394	 * and schedule a tasklet to try again later
 395	 */
 396	if (!mutex_trylock(&serial->disc_mutex)) {
 397		spin_lock_irqsave(&mos_parport->listlock, flags);
 398		list_add_tail(&urbtrack->urblist_entry,
 399			      &mos_parport->deferred_urbs);
 400		spin_unlock_irqrestore(&mos_parport->listlock, flags);
 401		tasklet_schedule(&mos_parport->urb_tasklet);
 402		dev_dbg(&usbdev->dev, "tasklet scheduled\n");
 403		return 0;
 404	}
 405
 406	/* bail if device disconnected */
 407	if (serial->disconnected) {
 408		kref_put(&urbtrack->ref_count, destroy_urbtracker);
 409		mutex_unlock(&serial->disc_mutex);
 410		return -ENODEV;
 411	}
 412
 413	/* add the tracker to the active_urbs list and submit */
 414	spin_lock_irqsave(&mos_parport->listlock, flags);
 415	list_add_tail(&urbtrack->urblist_entry, &mos_parport->active_urbs);
 416	spin_unlock_irqrestore(&mos_parport->listlock, flags);
 417	ret_val = usb_submit_urb(urbtrack->urb, GFP_ATOMIC);
 418	mutex_unlock(&serial->disc_mutex);
 419	if (ret_val) {
 420		dev_err(&usbdev->dev,
 421			"%s: submit_urb() failed: %d\n", __func__, ret_val);
 422		spin_lock_irqsave(&mos_parport->listlock, flags);
 423		list_del(&urbtrack->urblist_entry);
 424		spin_unlock_irqrestore(&mos_parport->listlock, flags);
 425		kref_put(&urbtrack->ref_count, destroy_urbtracker);
 426		return ret_val;
 427	}
 428	return 0;
 429}
 430
 431/*
 432 * This is the the common top part of all parallel port callback operations that
 433 * send synchronous messages to the device.  This implements convoluted locking
 434 * that avoids two scenarios: (1) a port operation is called after usbserial
 435 * has called our release function, at which point struct mos7715_parport has
 436 * been destroyed, and (2) the device has been disconnected, but usbserial has
 437 * not called the release function yet because someone has a serial port open.
 438 * The shared release_lock prevents the first, and the mutex and disconnected
 439 * flag maintained by usbserial covers the second.  We also use the msg_pending
 440 * flag to ensure that all synchronous usb message calls have completed before
 441 * our release function can return.
 442 */
 443static int parport_prologue(struct parport *pp)
 444{
 445	struct mos7715_parport *mos_parport;
 446
 447	spin_lock(&release_lock);
 448	mos_parport = pp->private_data;
 449	if (unlikely(mos_parport == NULL)) {
 450		/* release fn called, port struct destroyed */
 451		spin_unlock(&release_lock);
 452		return -1;
 453	}
 454	mos_parport->msg_pending = true;   /* synch usb call pending */
 455	reinit_completion(&mos_parport->syncmsg_compl);
 456	spin_unlock(&release_lock);
 457
 
 
 
 
 458	mutex_lock(&mos_parport->serial->disc_mutex);
 459	if (mos_parport->serial->disconnected) {
 460		/* device disconnected */
 461		mutex_unlock(&mos_parport->serial->disc_mutex);
 462		mos_parport->msg_pending = false;
 463		complete(&mos_parport->syncmsg_compl);
 464		return -1;
 465	}
 466
 467	return 0;
 468}
 469
 470/*
 471 * This is the common bottom part of all parallel port functions that send
 472 * synchronous messages to the device.
 473 */
 474static inline void parport_epilogue(struct parport *pp)
 475{
 476	struct mos7715_parport *mos_parport = pp->private_data;
 477	mutex_unlock(&mos_parport->serial->disc_mutex);
 478	mos_parport->msg_pending = false;
 479	complete(&mos_parport->syncmsg_compl);
 480}
 481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 482static void parport_mos7715_write_data(struct parport *pp, unsigned char d)
 483{
 484	struct mos7715_parport *mos_parport = pp->private_data;
 485
 486	if (parport_prologue(pp) < 0)
 487		return;
 488	mos7715_change_mode(mos_parport, SPP);
 489	write_mos_reg(mos_parport->serial, dummy, DPR, (__u8)d);
 490	parport_epilogue(pp);
 491}
 492
 493static unsigned char parport_mos7715_read_data(struct parport *pp)
 494{
 495	struct mos7715_parport *mos_parport = pp->private_data;
 496	unsigned char d;
 497
 498	if (parport_prologue(pp) < 0)
 499		return 0;
 500	read_mos_reg(mos_parport->serial, dummy, DPR, &d);
 501	parport_epilogue(pp);
 502	return d;
 503}
 504
 505static void parport_mos7715_write_control(struct parport *pp, unsigned char d)
 506{
 507	struct mos7715_parport *mos_parport = pp->private_data;
 508	__u8 data;
 509
 510	if (parport_prologue(pp) < 0)
 511		return;
 512	data = ((__u8)d & 0x0f) | (mos_parport->shadowDCR & 0xf0);
 513	write_mos_reg(mos_parport->serial, dummy, DCR, data);
 514	mos_parport->shadowDCR = data;
 515	parport_epilogue(pp);
 516}
 517
 518static unsigned char parport_mos7715_read_control(struct parport *pp)
 519{
 520	struct mos7715_parport *mos_parport = pp->private_data;
 521	__u8 dcr;
 522
 523	spin_lock(&release_lock);
 524	mos_parport = pp->private_data;
 525	if (unlikely(mos_parport == NULL)) {
 526		spin_unlock(&release_lock);
 527		return 0;
 528	}
 529	dcr = mos_parport->shadowDCR & 0x0f;
 530	spin_unlock(&release_lock);
 531	return dcr;
 532}
 533
 534static unsigned char parport_mos7715_frob_control(struct parport *pp,
 535						  unsigned char mask,
 536						  unsigned char val)
 537{
 538	struct mos7715_parport *mos_parport = pp->private_data;
 539	__u8 dcr;
 540
 541	mask &= 0x0f;
 542	val &= 0x0f;
 543	if (parport_prologue(pp) < 0)
 544		return 0;
 545	mos_parport->shadowDCR = (mos_parport->shadowDCR & (~mask)) ^ val;
 546	write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
 
 547	dcr = mos_parport->shadowDCR & 0x0f;
 548	parport_epilogue(pp);
 549	return dcr;
 550}
 551
 552static unsigned char parport_mos7715_read_status(struct parport *pp)
 553{
 554	unsigned char status;
 555	struct mos7715_parport *mos_parport = pp->private_data;
 556
 557	spin_lock(&release_lock);
 558	mos_parport = pp->private_data;
 559	if (unlikely(mos_parport == NULL)) {	/* release called */
 560		spin_unlock(&release_lock);
 561		return 0;
 562	}
 563	status = atomic_read(&mos_parport->shadowDSR) & 0xf8;
 564	spin_unlock(&release_lock);
 565	return status;
 566}
 567
 568static void parport_mos7715_enable_irq(struct parport *pp)
 569{
 570}
 571
 572static void parport_mos7715_disable_irq(struct parport *pp)
 573{
 574}
 575
 576static void parport_mos7715_data_forward(struct parport *pp)
 577{
 578	struct mos7715_parport *mos_parport = pp->private_data;
 579
 580	if (parport_prologue(pp) < 0)
 581		return;
 582	mos7715_change_mode(mos_parport, PS2);
 583	mos_parport->shadowDCR &=  ~0x20;
 584	write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
 
 585	parport_epilogue(pp);
 586}
 587
 588static void parport_mos7715_data_reverse(struct parport *pp)
 589{
 590	struct mos7715_parport *mos_parport = pp->private_data;
 591
 592	if (parport_prologue(pp) < 0)
 593		return;
 594	mos7715_change_mode(mos_parport, PS2);
 595	mos_parport->shadowDCR |= 0x20;
 596	write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
 
 597	parport_epilogue(pp);
 598}
 599
 600static void parport_mos7715_init_state(struct pardevice *dev,
 601				       struct parport_state *s)
 602{
 603	s->u.pc.ctr = DCR_INIT_VAL;
 604	s->u.pc.ecr = ECR_INIT_VAL;
 605}
 606
 607/* N.B. Parport core code requires that this function not block */
 608static void parport_mos7715_save_state(struct parport *pp,
 609				       struct parport_state *s)
 610{
 611	struct mos7715_parport *mos_parport;
 612
 613	spin_lock(&release_lock);
 614	mos_parport = pp->private_data;
 615	if (unlikely(mos_parport == NULL)) {	/* release called */
 616		spin_unlock(&release_lock);
 617		return;
 618	}
 619	s->u.pc.ctr = mos_parport->shadowDCR;
 620	s->u.pc.ecr = mos_parport->shadowECR;
 621	spin_unlock(&release_lock);
 622}
 623
 624/* N.B. Parport core code requires that this function not block */
 625static void parport_mos7715_restore_state(struct parport *pp,
 626					  struct parport_state *s)
 627{
 628	struct mos7715_parport *mos_parport;
 629
 630	spin_lock(&release_lock);
 631	mos_parport = pp->private_data;
 632	if (unlikely(mos_parport == NULL)) {	/* release called */
 633		spin_unlock(&release_lock);
 634		return;
 635	}
 636	write_parport_reg_nonblock(mos_parport, DCR, mos_parport->shadowDCR);
 637	write_parport_reg_nonblock(mos_parport, ECR, mos_parport->shadowECR);
 
 
 638	spin_unlock(&release_lock);
 639}
 640
 641static size_t parport_mos7715_write_compat(struct parport *pp,
 642					   const void *buffer,
 643					   size_t len, int flags)
 644{
 645	int retval;
 646	struct mos7715_parport *mos_parport = pp->private_data;
 647	int actual_len;
 648
 649	if (parport_prologue(pp) < 0)
 650		return 0;
 651	mos7715_change_mode(mos_parport, PPF);
 652	retval = usb_bulk_msg(mos_parport->serial->dev,
 653			      usb_sndbulkpipe(mos_parport->serial->dev, 2),
 654			      (void *)buffer, len, &actual_len,
 655			      MOS_WDR_TIMEOUT);
 656	parport_epilogue(pp);
 657	if (retval) {
 658		dev_err(&mos_parport->serial->dev->dev,
 659			"mos7720: usb_bulk_msg() failed: %d\n", retval);
 660		return 0;
 661	}
 662	return actual_len;
 663}
 664
 665static struct parport_operations parport_mos7715_ops = {
 666	.owner =		THIS_MODULE,
 667	.write_data =		parport_mos7715_write_data,
 668	.read_data =		parport_mos7715_read_data,
 669
 670	.write_control =	parport_mos7715_write_control,
 671	.read_control =		parport_mos7715_read_control,
 672	.frob_control =		parport_mos7715_frob_control,
 673
 674	.read_status =		parport_mos7715_read_status,
 675
 676	.enable_irq =		parport_mos7715_enable_irq,
 677	.disable_irq =		parport_mos7715_disable_irq,
 678
 679	.data_forward =		parport_mos7715_data_forward,
 680	.data_reverse =		parport_mos7715_data_reverse,
 681
 682	.init_state =		parport_mos7715_init_state,
 683	.save_state =		parport_mos7715_save_state,
 684	.restore_state =	parport_mos7715_restore_state,
 685
 686	.compat_write_data =	parport_mos7715_write_compat,
 687
 688	.nibble_read_data =	parport_ieee1284_read_nibble,
 689	.byte_read_data =	parport_ieee1284_read_byte,
 690};
 691
 692/*
 693 * Allocate and initialize parallel port control struct, initialize
 694 * the parallel port hardware device, and register with the parport subsystem.
 695 */
 696static int mos7715_parport_init(struct usb_serial *serial)
 697{
 698	struct mos7715_parport *mos_parport;
 699
 700	/* allocate and initialize parallel port control struct */
 701	mos_parport = kzalloc(sizeof(struct mos7715_parport), GFP_KERNEL);
 702	if (!mos_parport)
 703		return -ENOMEM;
 704
 705	mos_parport->msg_pending = false;
 706	kref_init(&mos_parport->ref_count);
 707	spin_lock_init(&mos_parport->listlock);
 708	INIT_LIST_HEAD(&mos_parport->active_urbs);
 709	INIT_LIST_HEAD(&mos_parport->deferred_urbs);
 710	usb_set_serial_data(serial, mos_parport); /* hijack private pointer */
 711	mos_parport->serial = serial;
 712	tasklet_init(&mos_parport->urb_tasklet, send_deferred_urbs,
 713		     (unsigned long) mos_parport);
 714	init_completion(&mos_parport->syncmsg_compl);
 715
 716	/* cycle parallel port reset bit */
 717	write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x80);
 718	write_mos_reg(mos_parport->serial, dummy, PP_REG, (__u8)0x00);
 719
 720	/* initialize device registers */
 721	mos_parport->shadowDCR = DCR_INIT_VAL;
 722	write_mos_reg(mos_parport->serial, dummy, DCR, mos_parport->shadowDCR);
 
 723	mos_parport->shadowECR = ECR_INIT_VAL;
 724	write_mos_reg(mos_parport->serial, dummy, ECR, mos_parport->shadowECR);
 
 725
 726	/* register with parport core */
 727	mos_parport->pp = parport_register_port(0, PARPORT_IRQ_NONE,
 728						PARPORT_DMA_NONE,
 729						&parport_mos7715_ops);
 730	if (mos_parport->pp == NULL) {
 731		dev_err(&serial->interface->dev,
 732			"Could not register parport\n");
 733		kref_put(&mos_parport->ref_count, destroy_mos_parport);
 734		return -EIO;
 735	}
 736	mos_parport->pp->private_data = mos_parport;
 737	mos_parport->pp->modes = PARPORT_MODE_COMPAT | PARPORT_MODE_PCSPP;
 738	mos_parport->pp->dev = &serial->interface->dev;
 739	parport_announce_port(mos_parport->pp);
 740
 741	return 0;
 742}
 743#endif	/* CONFIG_USB_SERIAL_MOS7715_PARPORT */
 744
 745/*
 746 * mos7720_interrupt_callback
 747 *	this is the callback function for when we have received data on the
 748 *	interrupt endpoint.
 749 */
 750static void mos7720_interrupt_callback(struct urb *urb)
 751{
 752	int result;
 753	int length;
 754	int status = urb->status;
 755	struct device *dev = &urb->dev->dev;
 756	__u8 *data;
 757	__u8 sp1;
 758	__u8 sp2;
 759
 760	switch (status) {
 761	case 0:
 762		/* success */
 763		break;
 764	case -ECONNRESET:
 765	case -ENOENT:
 766	case -ESHUTDOWN:
 767		/* this urb is terminated, clean up */
 768		dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
 769		return;
 770	default:
 771		dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
 772		goto exit;
 773	}
 774
 775	length = urb->actual_length;
 776	data = urb->transfer_buffer;
 777
 778	/* Moschip get 4 bytes
 779	 * Byte 1 IIR Port 1 (port.number is 0)
 780	 * Byte 2 IIR Port 2 (port.number is 1)
 781	 * Byte 3 --------------
 782	 * Byte 4 FIFO status for both */
 783
 784	/* the above description is inverted
 785	 * 	oneukum 2007-03-14 */
 786
 787	if (unlikely(length != 4)) {
 788		dev_dbg(dev, "Wrong data !!!\n");
 789		return;
 790	}
 791
 792	sp1 = data[3];
 793	sp2 = data[2];
 794
 795	if ((sp1 | sp2) & 0x01) {
 796		/* No Interrupt Pending in both the ports */
 797		dev_dbg(dev, "No Interrupt !!!\n");
 798	} else {
 799		switch (sp1 & 0x0f) {
 800		case SERIAL_IIR_RLS:
 801			dev_dbg(dev, "Serial Port 1: Receiver status error or address bit detected in 9-bit mode\n");
 802			break;
 803		case SERIAL_IIR_CTI:
 804			dev_dbg(dev, "Serial Port 1: Receiver time out\n");
 805			break;
 806		case SERIAL_IIR_MS:
 807			/* dev_dbg(dev, "Serial Port 1: Modem status change\n"); */
 808			break;
 809		}
 810
 811		switch (sp2 & 0x0f) {
 812		case SERIAL_IIR_RLS:
 813			dev_dbg(dev, "Serial Port 2: Receiver status error or address bit detected in 9-bit mode\n");
 814			break;
 815		case SERIAL_IIR_CTI:
 816			dev_dbg(dev, "Serial Port 2: Receiver time out\n");
 817			break;
 818		case SERIAL_IIR_MS:
 819			/* dev_dbg(dev, "Serial Port 2: Modem status change\n"); */
 820			break;
 821		}
 822	}
 823
 824exit:
 825	result = usb_submit_urb(urb, GFP_ATOMIC);
 826	if (result)
 827		dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
 828}
 829
 830/*
 831 * mos7715_interrupt_callback
 832 *	this is the 7715's callback function for when we have received data on
 833 *	the interrupt endpoint.
 834 */
 835static void mos7715_interrupt_callback(struct urb *urb)
 836{
 837	int result;
 838	int length;
 839	int status = urb->status;
 840	struct device *dev = &urb->dev->dev;
 841	__u8 *data;
 842	__u8 iir;
 843
 844	switch (status) {
 845	case 0:
 846		/* success */
 847		break;
 848	case -ECONNRESET:
 849	case -ENOENT:
 850	case -ESHUTDOWN:
 851	case -ENODEV:
 852		/* this urb is terminated, clean up */
 853		dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
 854		return;
 855	default:
 856		dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
 857		goto exit;
 858	}
 859
 860	length = urb->actual_length;
 861	data = urb->transfer_buffer;
 862
 863	/* Structure of data from 7715 device:
 864	 * Byte 1: IIR serial Port
 865	 * Byte 2: unused
 866	 * Byte 2: DSR parallel port
 867	 * Byte 4: FIFO status for both */
 868
 869	if (unlikely(length != 4)) {
 870		dev_dbg(dev, "Wrong data !!!\n");
 871		return;
 872	}
 873
 874	iir = data[0];
 875	if (!(iir & 0x01)) {	/* serial port interrupt pending */
 876		switch (iir & 0x0f) {
 877		case SERIAL_IIR_RLS:
 878			dev_dbg(dev, "Serial Port: Receiver status error or address bit detected in 9-bit mode\n");
 879			break;
 880		case SERIAL_IIR_CTI:
 881			dev_dbg(dev, "Serial Port: Receiver time out\n");
 882			break;
 883		case SERIAL_IIR_MS:
 884			/* dev_dbg(dev, "Serial Port: Modem status change\n"); */
 885			break;
 886		}
 887	}
 888
 889#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
 890	{       /* update local copy of DSR reg */
 891		struct usb_serial_port *port = urb->context;
 892		struct mos7715_parport *mos_parport = port->serial->private;
 893		if (unlikely(mos_parport == NULL))
 894			return;
 895		atomic_set(&mos_parport->shadowDSR, data[2]);
 896	}
 897#endif
 898
 899exit:
 900	result = usb_submit_urb(urb, GFP_ATOMIC);
 901	if (result)
 902		dev_err(dev, "%s - Error %d submitting control urb\n", __func__, result);
 903}
 904
 905/*
 906 * mos7720_bulk_in_callback
 907 *	this is the callback function for when we have received data on the
 908 *	bulk in endpoint.
 909 */
 910static void mos7720_bulk_in_callback(struct urb *urb)
 911{
 912	int retval;
 913	unsigned char *data ;
 914	struct usb_serial_port *port;
 915	int status = urb->status;
 916
 917	if (status) {
 918		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
 919		return;
 920	}
 921
 922	port = urb->context;
 923
 924	dev_dbg(&port->dev, "Entering...%s\n", __func__);
 925
 926	data = urb->transfer_buffer;
 927
 928	if (urb->actual_length) {
 929		tty_insert_flip_string(&port->port, data, urb->actual_length);
 930		tty_flip_buffer_push(&port->port);
 931	}
 932
 933	if (port->read_urb->status != -EINPROGRESS) {
 934		retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
 935		if (retval)
 936			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
 937	}
 938}
 939
 940/*
 941 * mos7720_bulk_out_data_callback
 942 *	this is the callback function for when we have finished sending serial
 943 *	data on the bulk out endpoint.
 944 */
 945static void mos7720_bulk_out_data_callback(struct urb *urb)
 946{
 947	struct moschip_port *mos7720_port;
 948	int status = urb->status;
 949
 950	if (status) {
 951		dev_dbg(&urb->dev->dev, "nonzero write bulk status received:%d\n", status);
 952		return;
 953	}
 954
 955	mos7720_port = urb->context;
 956	if (!mos7720_port) {
 957		dev_dbg(&urb->dev->dev, "NULL mos7720_port pointer\n");
 958		return ;
 959	}
 960
 961	if (mos7720_port->open)
 962		tty_port_tty_wakeup(&mos7720_port->port->port);
 963}
 964
 965/*
 966 * mos77xx_probe
 967 *	this function installs the appropriate read interrupt endpoint callback
 968 *	depending on whether the device is a 7720 or 7715, thus avoiding costly
 969 *	run-time checks in the high-frequency callback routine itself.
 970 */
 971static int mos77xx_probe(struct usb_serial *serial,
 972			 const struct usb_device_id *id)
 973{
 974	if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
 975		moschip7720_2port_driver.read_int_callback =
 976			mos7715_interrupt_callback;
 977	else
 978		moschip7720_2port_driver.read_int_callback =
 979			mos7720_interrupt_callback;
 980
 981	return 0;
 982}
 
 
 
 
 
 
 
 
 983
 984static int mos77xx_calc_num_ports(struct usb_serial *serial)
 985{
 986	u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
 987	if (product == MOSCHIP_DEVICE_ID_7715)
 988		return 1;
 
 989
 990	return 2;
 991}
 992
 993static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
 994{
 995	struct usb_serial *serial;
 996	struct urb *urb;
 997	struct moschip_port *mos7720_port;
 998	int response;
 999	int port_number;
1000	__u8 data;
1001	int allocated_urbs = 0;
1002	int j;
1003
1004	serial = port->serial;
1005
1006	mos7720_port = usb_get_serial_port_data(port);
1007	if (mos7720_port == NULL)
1008		return -ENODEV;
1009
1010	usb_clear_halt(serial->dev, port->write_urb->pipe);
1011	usb_clear_halt(serial->dev, port->read_urb->pipe);
1012
1013	/* Initialising the write urb pool */
1014	for (j = 0; j < NUM_URBS; ++j) {
1015		urb = usb_alloc_urb(0, GFP_KERNEL);
1016		mos7720_port->write_urb_pool[j] = urb;
1017		if (!urb)
1018			continue;
1019
1020		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1021					       GFP_KERNEL);
1022		if (!urb->transfer_buffer) {
1023			usb_free_urb(mos7720_port->write_urb_pool[j]);
1024			mos7720_port->write_urb_pool[j] = NULL;
1025			continue;
1026		}
1027		allocated_urbs++;
1028	}
1029
1030	if (!allocated_urbs)
1031		return -ENOMEM;
1032
1033	 /* Initialize MCS7720 -- Write Init values to corresponding Registers
1034	  *
1035	  * Register Index
1036	  * 0 : THR/RHR
1037	  * 1 : IER
1038	  * 2 : FCR
1039	  * 3 : LCR
1040	  * 4 : MCR
1041	  * 5 : LSR
1042	  * 6 : MSR
1043	  * 7 : SPR
1044	  *
1045	  * 0x08 : SP1/2 Control Reg
1046	  */
1047	port_number = port->port_number;
1048	read_mos_reg(serial, port_number, LSR, &data);
1049
1050	dev_dbg(&port->dev, "SS::%p LSR:%x\n", mos7720_port, data);
1051
1052	write_mos_reg(serial, dummy, SP1_REG, 0x02);
1053	write_mos_reg(serial, dummy, SP2_REG, 0x02);
1054
1055	write_mos_reg(serial, port_number, IER, 0x00);
1056	write_mos_reg(serial, port_number, FCR, 0x00);
1057
1058	write_mos_reg(serial, port_number, FCR, 0xcf);
1059	mos7720_port->shadowLCR = 0x03;
1060	write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
 
1061	mos7720_port->shadowMCR = 0x0b;
1062	write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
 
1063
1064	write_mos_reg(serial, port_number, SP_CONTROL_REG, 0x00);
1065	read_mos_reg(serial, dummy, SP_CONTROL_REG, &data);
1066	data = data | (port->port_number + 1);
1067	write_mos_reg(serial, dummy, SP_CONTROL_REG, data);
1068	mos7720_port->shadowLCR = 0x83;
1069	write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1070	write_mos_reg(serial, port_number, THR, 0x0c);
1071	write_mos_reg(serial, port_number, IER, 0x00);
 
1072	mos7720_port->shadowLCR = 0x03;
1073	write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1074	write_mos_reg(serial, port_number, IER, 0x0c);
 
1075
1076	response = usb_submit_urb(port->read_urb, GFP_KERNEL);
1077	if (response)
1078		dev_err(&port->dev, "%s - Error %d submitting read urb\n",
1079							__func__, response);
1080
1081	/* initialize our port settings */
1082	mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
1083
1084	/* send a open port command */
1085	mos7720_port->open = 1;
1086
1087	return 0;
1088}
1089
1090/*
1091 * mos7720_chars_in_buffer
1092 *	this function is called by the tty driver when it wants to know how many
1093 *	bytes of data we currently have outstanding in the port (data that has
1094 *	been written, but hasn't made it out the port yet)
1095 *	If successful, we return the number of bytes left to be written in the
1096 *	system,
1097 *	Otherwise we return a negative error number.
1098 */
1099static int mos7720_chars_in_buffer(struct tty_struct *tty)
1100{
1101	struct usb_serial_port *port = tty->driver_data;
 
1102	int i;
1103	int chars = 0;
1104	struct moschip_port *mos7720_port;
1105
1106	mos7720_port = usb_get_serial_port_data(port);
1107	if (mos7720_port == NULL)
1108		return 0;
1109
1110	for (i = 0; i < NUM_URBS; ++i) {
1111		if (mos7720_port->write_urb_pool[i] &&
1112		    mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
1113			chars += URB_TRANSFER_BUFFER_SIZE;
1114	}
1115	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1116	return chars;
1117}
1118
1119static void mos7720_close(struct usb_serial_port *port)
1120{
1121	struct usb_serial *serial;
1122	struct moschip_port *mos7720_port;
1123	int j;
1124
1125	serial = port->serial;
1126
1127	mos7720_port = usb_get_serial_port_data(port);
1128	if (mos7720_port == NULL)
1129		return;
1130
1131	for (j = 0; j < NUM_URBS; ++j)
1132		usb_kill_urb(mos7720_port->write_urb_pool[j]);
1133
1134	/* Freeing Write URBs */
1135	for (j = 0; j < NUM_URBS; ++j) {
1136		if (mos7720_port->write_urb_pool[j]) {
1137			kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
1138			usb_free_urb(mos7720_port->write_urb_pool[j]);
1139		}
1140	}
1141
1142	/* While closing port, shutdown all bulk read, write  *
1143	 * and interrupt read if they exists, otherwise nop   */
1144	usb_kill_urb(port->write_urb);
1145	usb_kill_urb(port->read_urb);
1146
1147	write_mos_reg(serial, port->port_number, MCR, 0x00);
1148	write_mos_reg(serial, port->port_number, IER, 0x00);
1149
1150	mos7720_port->open = 0;
1151}
1152
1153static void mos7720_break(struct tty_struct *tty, int break_state)
1154{
1155	struct usb_serial_port *port = tty->driver_data;
1156	unsigned char data;
1157	struct usb_serial *serial;
1158	struct moschip_port *mos7720_port;
1159
1160	serial = port->serial;
1161
1162	mos7720_port = usb_get_serial_port_data(port);
1163	if (mos7720_port == NULL)
1164		return;
1165
1166	if (break_state == -1)
1167		data = mos7720_port->shadowLCR | UART_LCR_SBC;
1168	else
1169		data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
1170
1171	mos7720_port->shadowLCR  = data;
1172	write_mos_reg(serial, port->port_number, LCR, mos7720_port->shadowLCR);
 
 
1173}
1174
1175/*
1176 * mos7720_write_room
1177 *	this function is called by the tty driver when it wants to know how many
1178 *	bytes of data we can accept for a specific port.
1179 *	If successful, we return the amount of room that we have for this port
1180 *	Otherwise we return a negative error number.
1181 */
1182static int mos7720_write_room(struct tty_struct *tty)
1183{
1184	struct usb_serial_port *port = tty->driver_data;
1185	struct moschip_port *mos7720_port;
1186	int room = 0;
1187	int i;
1188
1189	mos7720_port = usb_get_serial_port_data(port);
1190	if (mos7720_port == NULL)
1191		return -ENODEV;
1192
1193	/* FIXME: Locking */
1194	for (i = 0; i < NUM_URBS; ++i) {
1195		if (mos7720_port->write_urb_pool[i] &&
1196		    mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
1197			room += URB_TRANSFER_BUFFER_SIZE;
1198	}
1199
1200	dev_dbg(&port->dev, "%s - returns %d\n", __func__, room);
1201	return room;
1202}
1203
1204static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
1205				 const unsigned char *data, int count)
1206{
1207	int status;
1208	int i;
1209	int bytes_sent = 0;
1210	int transfer_size;
1211
1212	struct moschip_port *mos7720_port;
1213	struct usb_serial *serial;
1214	struct urb    *urb;
1215	const unsigned char *current_position = data;
1216
1217	serial = port->serial;
1218
1219	mos7720_port = usb_get_serial_port_data(port);
1220	if (mos7720_port == NULL)
1221		return -ENODEV;
1222
1223	/* try to find a free urb in the list */
1224	urb = NULL;
1225
1226	for (i = 0; i < NUM_URBS; ++i) {
1227		if (mos7720_port->write_urb_pool[i] &&
1228		    mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
1229			urb = mos7720_port->write_urb_pool[i];
1230			dev_dbg(&port->dev, "URB:%d\n", i);
1231			break;
1232		}
1233	}
1234
1235	if (urb == NULL) {
1236		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1237		goto exit;
1238	}
1239
1240	if (urb->transfer_buffer == NULL) {
1241		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1242					       GFP_KERNEL);
1243		if (!urb->transfer_buffer)
 
1244			goto exit;
 
1245	}
1246	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1247
1248	memcpy(urb->transfer_buffer, current_position, transfer_size);
1249	usb_serial_debug_data(&port->dev, __func__, transfer_size,
1250			      urb->transfer_buffer);
1251
1252	/* fill urb with data and submit  */
1253	usb_fill_bulk_urb(urb, serial->dev,
1254			  usb_sndbulkpipe(serial->dev,
1255					port->bulk_out_endpointAddress),
1256			  urb->transfer_buffer, transfer_size,
1257			  mos7720_bulk_out_data_callback, mos7720_port);
1258
1259	/* send it down the pipe */
1260	status = usb_submit_urb(urb, GFP_ATOMIC);
1261	if (status) {
1262		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1263			"with status = %d\n", __func__, status);
1264		bytes_sent = status;
1265		goto exit;
1266	}
1267	bytes_sent = transfer_size;
1268
1269exit:
1270	return bytes_sent;
1271}
1272
1273static void mos7720_throttle(struct tty_struct *tty)
1274{
1275	struct usb_serial_port *port = tty->driver_data;
1276	struct moschip_port *mos7720_port;
1277	int status;
1278
1279	mos7720_port = usb_get_serial_port_data(port);
1280
1281	if (mos7720_port == NULL)
1282		return;
1283
1284	if (!mos7720_port->open) {
1285		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1286		return;
1287	}
1288
1289	/* if we are implementing XON/XOFF, send the stop character */
1290	if (I_IXOFF(tty)) {
1291		unsigned char stop_char = STOP_CHAR(tty);
1292		status = mos7720_write(tty, port, &stop_char, 1);
1293		if (status <= 0)
1294			return;
1295	}
1296
1297	/* if we are implementing RTS/CTS, toggle that line */
1298	if (tty->termios.c_cflag & CRTSCTS) {
1299		mos7720_port->shadowMCR &= ~UART_MCR_RTS;
1300		write_mos_reg(port->serial, port->port_number, MCR,
1301			      mos7720_port->shadowMCR);
1302		if (status != 0)
1303			return;
1304	}
1305}
1306
1307static void mos7720_unthrottle(struct tty_struct *tty)
1308{
1309	struct usb_serial_port *port = tty->driver_data;
1310	struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1311	int status;
1312
1313	if (mos7720_port == NULL)
1314		return;
1315
1316	if (!mos7720_port->open) {
1317		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1318		return;
1319	}
1320
1321	/* if we are implementing XON/XOFF, send the start character */
1322	if (I_IXOFF(tty)) {
1323		unsigned char start_char = START_CHAR(tty);
1324		status = mos7720_write(tty, port, &start_char, 1);
1325		if (status <= 0)
1326			return;
1327	}
1328
1329	/* if we are implementing RTS/CTS, toggle that line */
1330	if (tty->termios.c_cflag & CRTSCTS) {
1331		mos7720_port->shadowMCR |= UART_MCR_RTS;
1332		write_mos_reg(port->serial, port->port_number, MCR,
1333			      mos7720_port->shadowMCR);
1334		if (status != 0)
1335			return;
1336	}
1337}
1338
1339/* FIXME: this function does not work */
1340static int set_higher_rates(struct moschip_port *mos7720_port,
1341			    unsigned int baud)
1342{
1343	struct usb_serial_port *port;
1344	struct usb_serial *serial;
1345	int port_number;
1346	enum mos_regs sp_reg;
1347	if (mos7720_port == NULL)
1348		return -EINVAL;
1349
1350	port = mos7720_port->port;
1351	serial = port->serial;
1352
1353	 /***********************************************
1354	 *      Init Sequence for higher rates
1355	 ***********************************************/
1356	dev_dbg(&port->dev, "Sending Setting Commands ..........\n");
1357	port_number = port->port_number;
1358
1359	write_mos_reg(serial, port_number, IER, 0x00);
1360	write_mos_reg(serial, port_number, FCR, 0x00);
1361	write_mos_reg(serial, port_number, FCR, 0xcf);
1362	mos7720_port->shadowMCR = 0x0b;
1363	write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
1364	write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x00);
 
1365
1366	/***********************************************
1367	 *              Set for higher rates           *
1368	 ***********************************************/
1369	/* writing baud rate verbatum into uart clock field clearly not right */
1370	if (port_number == 0)
1371		sp_reg = SP1_REG;
1372	else
1373		sp_reg = SP2_REG;
1374	write_mos_reg(serial, dummy, sp_reg, baud * 0x10);
1375	write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x03);
1376	mos7720_port->shadowMCR = 0x2b;
1377	write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
 
1378
1379	/***********************************************
1380	 *              Set DLL/DLM
1381	 ***********************************************/
1382	mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1383	write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
1384	write_mos_reg(serial, port_number, DLL, 0x01);
1385	write_mos_reg(serial, port_number, DLM, 0x00);
 
1386	mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1387	write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
 
1388
1389	return 0;
1390}
1391
1392/* baud rate information */
1393struct divisor_table_entry {
1394	__u32  baudrate;
1395	__u16  divisor;
1396};
1397
1398/* Define table of divisors for moschip 7720 hardware	   *
1399 * These assume a 3.6864MHz crystal, the standard /16, and *
1400 * MCR.7 = 0.						   */
1401static struct divisor_table_entry divisor_table[] = {
1402	{   50,		2304},
1403	{   110,	1047},	/* 2094.545455 => 230450   => .0217 % over */
1404	{   134,	857},	/* 1713.011152 => 230398.5 => .00065% under */
1405	{   150,	768},
1406	{   300,	384},
1407	{   600,	192},
1408	{   1200,	96},
1409	{   1800,	64},
1410	{   2400,	48},
1411	{   4800,	24},
1412	{   7200,	16},
1413	{   9600,	12},
1414	{   19200,	6},
1415	{   38400,	3},
1416	{   57600,	2},
1417	{   115200,	1},
1418};
1419
1420/*****************************************************************************
1421 * calc_baud_rate_divisor
1422 *	this function calculates the proper baud rate divisor for the specified
1423 *	baud rate.
1424 *****************************************************************************/
1425static int calc_baud_rate_divisor(struct usb_serial_port *port, int baudrate, int *divisor)
1426{
1427	int i;
1428	__u16 custom;
1429	__u16 round1;
1430	__u16 round;
1431
1432
1433	dev_dbg(&port->dev, "%s - %d\n", __func__, baudrate);
1434
1435	for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
1436		if (divisor_table[i].baudrate == baudrate) {
1437			*divisor = divisor_table[i].divisor;
1438			return 0;
1439		}
1440	}
1441
1442	/* After trying for all the standard baud rates    *
1443	 * Try calculating the divisor for this baud rate  */
1444	if (baudrate > 75 &&  baudrate < 230400) {
1445		/* get the divisor */
1446		custom = (__u16)(230400L  / baudrate);
1447
1448		/* Check for round off */
1449		round1 = (__u16)(2304000L / baudrate);
1450		round = (__u16)(round1 - (custom * 10));
1451		if (round > 4)
1452			custom++;
1453		*divisor = custom;
1454
1455		dev_dbg(&port->dev, "Baud %d = %d\n", baudrate, custom);
1456		return 0;
1457	}
1458
1459	dev_dbg(&port->dev, "Baud calculation Failed...\n");
1460	return -EINVAL;
1461}
1462
1463/*
1464 * send_cmd_write_baud_rate
1465 *	this function sends the proper command to change the baud rate of the
1466 *	specified port.
1467 */
1468static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
1469				    int baudrate)
1470{
1471	struct usb_serial_port *port;
1472	struct usb_serial *serial;
1473	int divisor;
1474	int status;
1475	unsigned char number;
1476
1477	if (mos7720_port == NULL)
1478		return -1;
1479
1480	port = mos7720_port->port;
1481	serial = port->serial;
1482
1483	number = port->port_number;
1484	dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudrate);
1485
1486	/* Calculate the Divisor */
1487	status = calc_baud_rate_divisor(port, baudrate, &divisor);
1488	if (status) {
1489		dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1490		return status;
1491	}
1492
1493	/* Enable access to divisor latch */
1494	mos7720_port->shadowLCR = mos7720_port->shadowLCR | UART_LCR_DLAB;
1495	write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1496
1497	/* Write the divisor */
1498	write_mos_reg(serial, number, DLL, (__u8)(divisor & 0xff));
1499	write_mos_reg(serial, number, DLM, (__u8)((divisor & 0xff00) >> 8));
 
1500
1501	/* Disable access to divisor latch */
1502	mos7720_port->shadowLCR = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
1503	write_mos_reg(serial, number, LCR, mos7720_port->shadowLCR);
1504
1505	return status;
1506}
1507
1508/*
1509 * change_port_settings
1510 *	This routine is called to set the UART on the device to match
1511 *      the specified new settings.
1512 */
1513static void change_port_settings(struct tty_struct *tty,
1514				 struct moschip_port *mos7720_port,
1515				 struct ktermios *old_termios)
1516{
1517	struct usb_serial_port *port;
1518	struct usb_serial *serial;
1519	int baud;
1520	unsigned cflag;
1521	unsigned iflag;
1522	__u8 mask = 0xff;
1523	__u8 lData;
1524	__u8 lParity;
1525	__u8 lStop;
1526	int status;
1527	int port_number;
1528
1529	if (mos7720_port == NULL)
1530		return ;
1531
1532	port = mos7720_port->port;
1533	serial = port->serial;
1534	port_number = port->port_number;
1535
1536	if (!mos7720_port->open) {
1537		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1538		return;
1539	}
1540
1541	lData = UART_LCR_WLEN8;
1542	lStop = 0x00;	/* 1 stop bit */
1543	lParity = 0x00;	/* No parity */
1544
1545	cflag = tty->termios.c_cflag;
1546	iflag = tty->termios.c_iflag;
1547
1548	/* Change the number of bits */
1549	switch (cflag & CSIZE) {
1550	case CS5:
1551		lData = UART_LCR_WLEN5;
1552		mask = 0x1f;
1553		break;
1554
1555	case CS6:
1556		lData = UART_LCR_WLEN6;
1557		mask = 0x3f;
1558		break;
1559
1560	case CS7:
1561		lData = UART_LCR_WLEN7;
1562		mask = 0x7f;
1563		break;
1564	default:
1565	case CS8:
1566		lData = UART_LCR_WLEN8;
1567		break;
1568	}
1569
1570	/* Change the Parity bit */
1571	if (cflag & PARENB) {
1572		if (cflag & PARODD) {
1573			lParity = UART_LCR_PARITY;
1574			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1575		} else {
1576			lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
1577			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1578		}
1579
1580	} else {
1581		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1582	}
1583
1584	if (cflag & CMSPAR)
1585		lParity = lParity | 0x20;
1586
1587	/* Change the Stop bit */
1588	if (cflag & CSTOPB) {
1589		lStop = UART_LCR_STOP;
1590		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1591	} else {
1592		lStop = 0x00;
1593		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1594	}
1595
1596#define LCR_BITS_MASK		0x03	/* Mask for bits/char field */
1597#define LCR_STOP_MASK		0x04	/* Mask for stop bits field */
1598#define LCR_PAR_MASK		0x38	/* Mask for parity field */
1599
1600	/* Update the LCR with the correct value */
1601	mos7720_port->shadowLCR &=
1602		~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1603	mos7720_port->shadowLCR |= (lData | lParity | lStop);
1604
1605
1606	/* Disable Interrupts */
1607	write_mos_reg(serial, port_number, IER, 0x00);
1608	write_mos_reg(serial, port_number, FCR, 0x00);
1609	write_mos_reg(serial, port_number, FCR, 0xcf);
1610
1611	/* Send the updated LCR value to the mos7720 */
1612	write_mos_reg(serial, port_number, LCR, mos7720_port->shadowLCR);
 
1613	mos7720_port->shadowMCR = 0x0b;
1614	write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
 
1615
1616	/* set up the MCR register and send it to the mos7720 */
1617	mos7720_port->shadowMCR = UART_MCR_OUT2;
1618	if (cflag & CBAUD)
1619		mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
1620
1621	if (cflag & CRTSCTS) {
1622		mos7720_port->shadowMCR |= (UART_MCR_XONANY);
1623		/* To set hardware flow control to the specified *
1624		 * serial port, in SP1/2_CONTROL_REG             */
1625		if (port_number)
1626			write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x01);
 
1627		else
1628			write_mos_reg(serial, dummy, SP_CONTROL_REG, 0x02);
 
1629
1630	} else
1631		mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
1632
1633	write_mos_reg(serial, port_number, MCR, mos7720_port->shadowMCR);
 
1634
1635	/* Determine divisor based on baud rate */
1636	baud = tty_get_baud_rate(tty);
1637	if (!baud) {
1638		/* pick a default, any default... */
1639		dev_dbg(&port->dev, "Picked default baud...\n");
1640		baud = 9600;
1641	}
1642
1643	if (baud >= 230400) {
1644		set_higher_rates(mos7720_port, baud);
1645		/* Enable Interrupts */
1646		write_mos_reg(serial, port_number, IER, 0x0c);
1647		return;
1648	}
1649
1650	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1651	status = send_cmd_write_baud_rate(mos7720_port, baud);
1652	/* FIXME: needs to write actual resulting baud back not just
1653	   blindly do so */
1654	if (cflag & CBAUD)
1655		tty_encode_baud_rate(tty, baud, baud);
1656	/* Enable Interrupts */
1657	write_mos_reg(serial, port_number, IER, 0x0c);
1658
1659	if (port->read_urb->status != -EINPROGRESS) {
1660		status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1661		if (status)
1662			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1663	}
1664}
1665
1666/*
1667 * mos7720_set_termios
1668 *	this function is called by the tty driver when it wants to change the
1669 *	termios structure.
1670 */
1671static void mos7720_set_termios(struct tty_struct *tty,
1672		struct usb_serial_port *port, struct ktermios *old_termios)
 
1673{
1674	int status;
1675	unsigned int cflag;
1676	struct usb_serial *serial;
1677	struct moschip_port *mos7720_port;
1678
1679	serial = port->serial;
1680
1681	mos7720_port = usb_get_serial_port_data(port);
1682
1683	if (mos7720_port == NULL)
1684		return;
1685
1686	if (!mos7720_port->open) {
1687		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1688		return;
1689	}
1690
1691	dev_dbg(&port->dev, "setting termios - ASPIRE\n");
1692
1693	cflag = tty->termios.c_cflag;
1694
1695	dev_dbg(&port->dev, "%s - cflag %08x iflag %08x\n", __func__,
1696		tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
1697
1698	dev_dbg(&port->dev, "%s - old cflag %08x old iflag %08x\n", __func__,
1699		old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
1700
1701	/* change the port settings to the new ones specified */
1702	change_port_settings(tty, mos7720_port, old_termios);
1703
1704	if (port->read_urb->status != -EINPROGRESS) {
1705		status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1706		if (status)
1707			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n", status);
1708	}
1709}
1710
1711/*
1712 * get_lsr_info - get line status register info
1713 *
1714 * Purpose: Let user call ioctl() to get info when the UART physically
1715 * 	    is emptied.  On bus types like RS485, the transmitter must
1716 * 	    release the bus after transmitting. This must be done when
1717 * 	    the transmit shift register is empty, not be done when the
1718 * 	    transmit holding register is empty.  This functionality
1719 * 	    allows an RS485 driver to be written in user space.
1720 */
1721static int get_lsr_info(struct tty_struct *tty,
1722		struct moschip_port *mos7720_port, unsigned int __user *value)
1723{
1724	struct usb_serial_port *port = tty->driver_data;
1725	unsigned int result = 0;
1726	unsigned char data = 0;
1727	int port_number = port->port_number;
1728	int count;
1729
1730	count = mos7720_chars_in_buffer(tty);
1731	if (count == 0) {
1732		read_mos_reg(port->serial, port_number, LSR, &data);
1733		if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
1734					== (UART_LSR_TEMT | UART_LSR_THRE)) {
1735			dev_dbg(&port->dev, "%s -- Empty\n", __func__);
1736			result = TIOCSER_TEMT;
1737		}
1738	}
1739	if (copy_to_user(value, &result, sizeof(int)))
1740		return -EFAULT;
1741	return 0;
1742}
1743
1744static int mos7720_tiocmget(struct tty_struct *tty)
1745{
1746	struct usb_serial_port *port = tty->driver_data;
1747	struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1748	unsigned int result = 0;
1749	unsigned int mcr ;
1750	unsigned int msr ;
1751
1752	mcr = mos7720_port->shadowMCR;
1753	msr = mos7720_port->shadowMSR;
1754
1755	result = ((mcr & UART_MCR_DTR)  ? TIOCM_DTR : 0)   /* 0x002 */
1756	  | ((mcr & UART_MCR_RTS)   ? TIOCM_RTS : 0)   /* 0x004 */
1757	  | ((msr & UART_MSR_CTS)   ? TIOCM_CTS : 0)   /* 0x020 */
1758	  | ((msr & UART_MSR_DCD)   ? TIOCM_CAR : 0)   /* 0x040 */
1759	  | ((msr & UART_MSR_RI)    ? TIOCM_RI :  0)   /* 0x080 */
1760	  | ((msr & UART_MSR_DSR)   ? TIOCM_DSR : 0);  /* 0x100 */
1761
1762	return result;
1763}
1764
1765static int mos7720_tiocmset(struct tty_struct *tty,
1766			    unsigned int set, unsigned int clear)
1767{
1768	struct usb_serial_port *port = tty->driver_data;
1769	struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
1770	unsigned int mcr ;
1771
1772	mcr = mos7720_port->shadowMCR;
1773
1774	if (set & TIOCM_RTS)
1775		mcr |= UART_MCR_RTS;
1776	if (set & TIOCM_DTR)
1777		mcr |= UART_MCR_DTR;
1778	if (set & TIOCM_LOOP)
1779		mcr |= UART_MCR_LOOP;
1780
1781	if (clear & TIOCM_RTS)
1782		mcr &= ~UART_MCR_RTS;
1783	if (clear & TIOCM_DTR)
1784		mcr &= ~UART_MCR_DTR;
1785	if (clear & TIOCM_LOOP)
1786		mcr &= ~UART_MCR_LOOP;
1787
1788	mos7720_port->shadowMCR = mcr;
1789	write_mos_reg(port->serial, port->port_number, MCR,
1790		      mos7720_port->shadowMCR);
1791
1792	return 0;
1793}
1794
1795static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
1796			  unsigned int __user *value)
1797{
1798	unsigned int mcr;
1799	unsigned int arg;
1800
1801	struct usb_serial_port *port;
1802
1803	if (mos7720_port == NULL)
1804		return -1;
1805
1806	port = (struct usb_serial_port *)mos7720_port->port;
1807	mcr = mos7720_port->shadowMCR;
1808
1809	if (copy_from_user(&arg, value, sizeof(int)))
1810		return -EFAULT;
1811
1812	switch (cmd) {
1813	case TIOCMBIS:
1814		if (arg & TIOCM_RTS)
1815			mcr |= UART_MCR_RTS;
1816		if (arg & TIOCM_DTR)
1817			mcr |= UART_MCR_RTS;
1818		if (arg & TIOCM_LOOP)
1819			mcr |= UART_MCR_LOOP;
1820		break;
1821
1822	case TIOCMBIC:
1823		if (arg & TIOCM_RTS)
1824			mcr &= ~UART_MCR_RTS;
1825		if (arg & TIOCM_DTR)
1826			mcr &= ~UART_MCR_RTS;
1827		if (arg & TIOCM_LOOP)
1828			mcr &= ~UART_MCR_LOOP;
1829		break;
1830
1831	}
1832
1833	mos7720_port->shadowMCR = mcr;
1834	write_mos_reg(port->serial, port->port_number, MCR,
1835		      mos7720_port->shadowMCR);
1836
1837	return 0;
1838}
1839
1840static int get_serial_info(struct moschip_port *mos7720_port,
1841			   struct serial_struct __user *retinfo)
1842{
1843	struct serial_struct tmp;
1844
1845	if (!retinfo)
1846		return -EFAULT;
1847
1848	memset(&tmp, 0, sizeof(tmp));
1849
1850	tmp.type		= PORT_16550A;
1851	tmp.line		= mos7720_port->port->minor;
1852	tmp.port		= mos7720_port->port->port_number;
1853	tmp.irq			= 0;
1854	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1855	tmp.xmit_fifo_size	= NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1856	tmp.baud_base		= 9600;
1857	tmp.close_delay		= 5*HZ;
1858	tmp.closing_wait	= 30*HZ;
1859
1860	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1861		return -EFAULT;
1862	return 0;
1863}
1864
1865static int mos7720_ioctl(struct tty_struct *tty,
1866			 unsigned int cmd, unsigned long arg)
1867{
1868	struct usb_serial_port *port = tty->driver_data;
1869	struct moschip_port *mos7720_port;
1870
1871	mos7720_port = usb_get_serial_port_data(port);
1872	if (mos7720_port == NULL)
1873		return -ENODEV;
1874
1875	switch (cmd) {
1876	case TIOCSERGETLSR:
1877		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1878		return get_lsr_info(tty, mos7720_port,
1879					(unsigned int __user *)arg);
1880
1881	/* FIXME: These should be using the mode methods */
1882	case TIOCMBIS:
1883	case TIOCMBIC:
1884		dev_dbg(&port->dev, "%s TIOCMSET/TIOCMBIC/TIOCMSET\n", __func__);
1885		return set_modem_info(mos7720_port, cmd,
1886				      (unsigned int __user *)arg);
1887
1888	case TIOCGSERIAL:
1889		dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
1890		return get_serial_info(mos7720_port,
1891				       (struct serial_struct __user *)arg);
1892	}
1893
1894	return -ENOIOCTLCMD;
1895}
1896
1897static int mos7720_startup(struct usb_serial *serial)
1898{
1899	struct usb_device *dev;
1900	char data;
1901	u16 product;
1902	int ret_val;
1903
1904	product = le16_to_cpu(serial->dev->descriptor.idProduct);
1905	dev = serial->dev;
1906
1907	/*
1908	 * The 7715 uses the first bulk in/out endpoint pair for the parallel
1909	 * port, and the second for the serial port.  Because the usbserial core
1910	 * assumes both pairs are serial ports, we must engage in a bit of
1911	 * subterfuge and swap the pointers for ports 0 and 1 in order to make
1912	 * port 0 point to the serial port.  However, both moschip devices use a
1913	 * single interrupt-in endpoint for both ports (as mentioned a little
1914	 * further down), and this endpoint was assigned to port 0.  So after
1915	 * the swap, we must copy the interrupt endpoint elements from port 1
1916	 * (as newly assigned) to port 0, and null out port 1 pointers.
1917	 */
1918	if (product == MOSCHIP_DEVICE_ID_7715) {
1919		struct usb_serial_port *tmp = serial->port[0];
1920		serial->port[0] = serial->port[1];
1921		serial->port[1] = tmp;
1922		serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
1923		serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
1924		serial->port[0]->interrupt_in_endpointAddress =
1925			tmp->interrupt_in_endpointAddress;
1926		serial->port[1]->interrupt_in_urb = NULL;
1927		serial->port[1]->interrupt_in_buffer = NULL;
1928	}
1929
1930	/* setting configuration feature to one */
1931	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1932			(__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5000);
1933
1934	/* start the interrupt urb */
1935	ret_val = usb_submit_urb(serial->port[0]->interrupt_in_urb, GFP_KERNEL);
1936	if (ret_val)
1937		dev_err(&dev->dev,
1938			"%s - Error %d submitting control urb\n",
1939			__func__, ret_val);
1940
1941#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1942	if (product == MOSCHIP_DEVICE_ID_7715) {
1943		ret_val = mos7715_parport_init(serial);
1944		if (ret_val < 0)
1945			return ret_val;
 
 
 
 
 
 
 
1946	}
1947#endif
1948	/* LSR For Port 1 */
1949	read_mos_reg(serial, 0, LSR, &data);
1950	dev_dbg(&dev->dev, "LSR:%x\n", data);
1951
1952	return 0;
1953}
1954
1955static void mos7720_release(struct usb_serial *serial)
1956{
 
 
1957#ifdef CONFIG_USB_SERIAL_MOS7715_PARPORT
1958	/* close the parallel port */
1959
1960	if (le16_to_cpu(serial->dev->descriptor.idProduct)
1961	    == MOSCHIP_DEVICE_ID_7715) {
1962		struct urbtracker *urbtrack;
1963		unsigned long flags;
1964		struct mos7715_parport *mos_parport =
1965			usb_get_serial_data(serial);
1966
1967		/* prevent NULL ptr dereference in port callbacks */
1968		spin_lock(&release_lock);
1969		mos_parport->pp->private_data = NULL;
1970		spin_unlock(&release_lock);
1971
1972		/* wait for synchronous usb calls to return */
1973		if (mos_parport->msg_pending)
1974			wait_for_completion_timeout(&mos_parport->syncmsg_compl,
1975					    msecs_to_jiffies(MOS_WDR_TIMEOUT));
 
 
 
 
 
 
1976
1977		parport_remove_port(mos_parport->pp);
1978		usb_set_serial_data(serial, NULL);
1979		mos_parport->serial = NULL;
1980
1981		/* if tasklet currently scheduled, wait for it to complete */
1982		tasklet_kill(&mos_parport->urb_tasklet);
1983
1984		/* unlink any urbs sent by the tasklet  */
1985		spin_lock_irqsave(&mos_parport->listlock, flags);
1986		list_for_each_entry(urbtrack,
1987				    &mos_parport->active_urbs,
1988				    urblist_entry)
1989			usb_unlink_urb(urbtrack->urb);
1990		spin_unlock_irqrestore(&mos_parport->listlock, flags);
1991
1992		kref_put(&mos_parport->ref_count, destroy_mos_parport);
1993	}
1994#endif
1995}
1996
1997static int mos7720_port_probe(struct usb_serial_port *port)
1998{
1999	struct moschip_port *mos7720_port;
2000
2001	mos7720_port = kzalloc(sizeof(*mos7720_port), GFP_KERNEL);
2002	if (!mos7720_port)
2003		return -ENOMEM;
2004
2005	/* Initialize all port interrupt end point to port 0 int endpoint.
2006	 * Our device has only one interrupt endpoint common to all ports.
2007	 */
2008	port->interrupt_in_endpointAddress =
2009		port->serial->port[0]->interrupt_in_endpointAddress;
2010	mos7720_port->port = port;
2011
2012	usb_set_serial_port_data(port, mos7720_port);
2013
2014	return 0;
2015}
2016
2017static int mos7720_port_remove(struct usb_serial_port *port)
2018{
2019	struct moschip_port *mos7720_port;
2020
2021	mos7720_port = usb_get_serial_port_data(port);
2022	kfree(mos7720_port);
2023
2024	return 0;
2025}
2026
2027static struct usb_serial_driver moschip7720_2port_driver = {
2028	.driver = {
2029		.owner =	THIS_MODULE,
2030		.name =		"moschip7720",
2031	},
2032	.description		= "Moschip 2 port adapter",
2033	.id_table		= id_table,
 
 
 
2034	.calc_num_ports		= mos77xx_calc_num_ports,
2035	.open			= mos7720_open,
2036	.close			= mos7720_close,
2037	.throttle		= mos7720_throttle,
2038	.unthrottle		= mos7720_unthrottle,
2039	.probe			= mos77xx_probe,
2040	.attach			= mos7720_startup,
2041	.release		= mos7720_release,
2042	.port_probe		= mos7720_port_probe,
2043	.port_remove		= mos7720_port_remove,
2044	.ioctl			= mos7720_ioctl,
2045	.tiocmget		= mos7720_tiocmget,
2046	.tiocmset		= mos7720_tiocmset,
2047	.set_termios		= mos7720_set_termios,
2048	.write			= mos7720_write,
2049	.write_room		= mos7720_write_room,
2050	.chars_in_buffer	= mos7720_chars_in_buffer,
2051	.break_ctl		= mos7720_break,
2052	.read_bulk_callback	= mos7720_bulk_in_callback,
2053	.read_int_callback	= NULL  /* dynamically assigned in probe() */
2054};
2055
2056static struct usb_serial_driver * const serial_drivers[] = {
2057	&moschip7720_2port_driver, NULL
2058};
2059
2060module_usb_serial_driver(serial_drivers, id_table);
2061
2062MODULE_AUTHOR(DRIVER_AUTHOR);
2063MODULE_DESCRIPTION(DRIVER_DESC);
2064MODULE_LICENSE("GPL");