Linux Audio

Check our new training course

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