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