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