xref: /linux/drivers/usb/serial/mos7840.c (revision 08ec212c0f92cbf30e3ecc7349f18151714041d6)
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  *
16  * Clean ups from Moschip version and a few ioctl implementations by:
17  *	Paul B Schroeder <pschroeder "at" uplogix "dot" com>
18  *
19  * Originally based on drivers/usb/serial/io_edgeport.c which is:
20  *      Copyright (C) 2000 Inside Out Networks, All rights reserved.
21  *      Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
22  *
23  */
24 
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/tty.h>
30 #include <linux/tty_driver.h>
31 #include <linux/tty_flip.h>
32 #include <linux/module.h>
33 #include <linux/serial.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
36 #include <linux/uaccess.h>
37 
38 /*
39  * Version Information
40  */
41 #define DRIVER_VERSION "1.3.2"
42 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
43 
44 /*
45  * 16C50 UART register defines
46  */
47 
48 #define LCR_BITS_5             0x00	/* 5 bits/char */
49 #define LCR_BITS_6             0x01	/* 6 bits/char */
50 #define LCR_BITS_7             0x02	/* 7 bits/char */
51 #define LCR_BITS_8             0x03	/* 8 bits/char */
52 #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
53 
54 #define LCR_STOP_1             0x00	/* 1 stop bit */
55 #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
56 #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
57 #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
58 
59 #define LCR_PAR_NONE           0x00	/* No parity */
60 #define LCR_PAR_ODD            0x08	/* Odd parity */
61 #define LCR_PAR_EVEN           0x18	/* Even parity */
62 #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
63 #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
64 #define LCR_PAR_MASK           0x38	/* Mask for parity field */
65 
66 #define LCR_SET_BREAK          0x40	/* Set Break condition */
67 #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
68 
69 #define MCR_DTR                0x01	/* Assert DTR */
70 #define MCR_RTS                0x02	/* Assert RTS */
71 #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
72 #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
73 #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
74 #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
75 
76 #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
77 #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
78 #define MOS7840_MSR_RI         0x40	/* Current state of RI */
79 #define MOS7840_MSR_CD         0x80	/* Current state of CD */
80 
81 /*
82  * Defines used for sending commands to port
83  */
84 
85 #define MOS_WDR_TIMEOUT		5000	/* default urb timeout */
86 
87 #define MOS_PORT1       0x0200
88 #define MOS_PORT2       0x0300
89 #define MOS_VENREG      0x0000
90 #define MOS_MAX_PORT	0x02
91 #define MOS_WRITE       0x0E
92 #define MOS_READ        0x0D
93 
94 /* Requests */
95 #define MCS_RD_RTYPE    0xC0
96 #define MCS_WR_RTYPE    0x40
97 #define MCS_RDREQ       0x0D
98 #define MCS_WRREQ       0x0E
99 #define MCS_CTRL_TIMEOUT        500
100 #define VENDOR_READ_LENGTH      (0x01)
101 
102 #define MAX_NAME_LEN    64
103 
104 #define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
105 #define ZLP_REG5  0x3E		/* Zero_Flag_Reg5    62 */
106 
107 /* For higher baud Rates use TIOCEXBAUD */
108 #define TIOCEXBAUD     0x5462
109 
110 /* vendor id and device id defines */
111 
112 /* The native mos7840/7820 component */
113 #define USB_VENDOR_ID_MOSCHIP           0x9710
114 #define MOSCHIP_DEVICE_ID_7840          0x7840
115 #define MOSCHIP_DEVICE_ID_7820          0x7820
116 #define MOSCHIP_DEVICE_ID_7810          0x7810
117 /* The native component can have its vendor/device id's overridden
118  * in vendor-specific implementations.  Such devices can be handled
119  * by making a change here, in id_table.
120  */
121 #define USB_VENDOR_ID_BANDB              0x0856
122 #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
123 #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
124 #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
125 #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
126 #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
127 #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
128 #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
129 #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
130 #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
131 #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
132 #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
133 #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
134 #define BANDB_DEVICE_ID_USOPTL2_4        0xAC24
135 
136 /* This driver also supports
137  * ATEN UC2324 device using Moschip MCS7840
138  * ATEN UC2322 device using Moschip MCS7820
139  */
140 #define USB_VENDOR_ID_ATENINTL		0x0557
141 #define ATENINTL_DEVICE_ID_UC2324	0x2011
142 #define ATENINTL_DEVICE_ID_UC2322	0x7820
143 
144 /* Interrupt Routine Defines    */
145 
146 #define SERIAL_IIR_RLS      0x06
147 #define SERIAL_IIR_MS       0x00
148 
149 /*
150  *  Emulation of the bit mask on the LINE STATUS REGISTER.
151  */
152 #define SERIAL_LSR_DR       0x0001
153 #define SERIAL_LSR_OE       0x0002
154 #define SERIAL_LSR_PE       0x0004
155 #define SERIAL_LSR_FE       0x0008
156 #define SERIAL_LSR_BI       0x0010
157 
158 #define MOS_MSR_DELTA_CTS   0x10
159 #define MOS_MSR_DELTA_DSR   0x20
160 #define MOS_MSR_DELTA_RI    0x40
161 #define MOS_MSR_DELTA_CD    0x80
162 
163 /* Serial Port register Address */
164 #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
165 #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
166 #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
167 #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
168 #define LINE_STATUS_REGISTER       ((__u16)(0x05))
169 #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
170 #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
171 #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
172 #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
173 
174 #define CLK_MULTI_REGISTER         ((__u16)(0x02))
175 #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
176 #define GPIO_REGISTER              ((__u16)(0x07))
177 
178 #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
179 
180 /*
181  * URB POOL related defines
182  */
183 #define NUM_URBS                        16	/* URB Count */
184 #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
185 
186 /* LED on/off milliseconds*/
187 #define LED_ON_MS	500
188 #define LED_OFF_MS	500
189 
190 static int device_type;
191 
192 static const struct usb_device_id id_table[] = {
193 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
194 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
195 	{USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
196 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
197 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
198 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
199 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
200 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
201 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
202 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
203 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
204 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
205 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
206 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
207 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
208 	{USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
209 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
210 	{USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
211 	{}			/* terminating entry */
212 };
213 MODULE_DEVICE_TABLE(usb, id_table);
214 
215 /* This structure holds all of the local port information */
216 
217 struct moschip_port {
218 	int port_num;		/*Actual port number in the device(1,2,etc) */
219 	struct urb *write_urb;	/* write URB for this port */
220 	struct urb *read_urb;	/* read URB for this port */
221 	struct urb *int_urb;
222 	__u8 shadowLCR;		/* last LCR value received */
223 	__u8 shadowMCR;		/* last MCR value received */
224 	char open;
225 	char open_ports;
226 	char zombie;
227 	wait_queue_head_t wait_chase;	/* for handling sleeping while waiting for chase to finish */
228 	wait_queue_head_t delta_msr_wait;	/* for handling sleeping while waiting for msr change to happen */
229 	int delta_msr_cond;
230 	struct async_icount icount;
231 	struct usb_serial_port *port;	/* loop back to the owner of this object */
232 
233 	/* Offsets */
234 	__u8 SpRegOffset;
235 	__u8 ControlRegOffset;
236 	__u8 DcrRegOffset;
237 	/* for processing control URBS in interrupt context */
238 	struct urb *control_urb;
239 	struct usb_ctrlrequest *dr;
240 	char *ctrl_buf;
241 	int MsrLsr;
242 
243 	spinlock_t pool_lock;
244 	struct urb *write_urb_pool[NUM_URBS];
245 	char busy[NUM_URBS];
246 	bool read_urb_busy;
247 
248 	/* For device(s) with LED indicator */
249 	bool has_led;
250 	bool led_flag;
251 	struct timer_list led_timer1;	/* Timer for LED on */
252 	struct timer_list led_timer2;	/* Timer for LED off */
253 };
254 
255 /*
256  * mos7840_set_reg_sync
257  * 	To set the Control register by calling usb_fill_control_urb function
258  *	by passing usb_sndctrlpipe function as parameter.
259  */
260 
261 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
262 				__u16 val)
263 {
264 	struct usb_device *dev = port->serial->dev;
265 	val = val & 0x00ff;
266 	dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
267 
268 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
269 			       MCS_WR_RTYPE, val, reg, NULL, 0,
270 			       MOS_WDR_TIMEOUT);
271 }
272 
273 /*
274  * mos7840_get_reg_sync
275  * 	To set the Uart register by calling usb_fill_control_urb function by
276  *	passing usb_rcvctrlpipe function as parameter.
277  */
278 
279 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
280 				__u16 *val)
281 {
282 	struct usb_device *dev = port->serial->dev;
283 	int ret = 0;
284 	u8 *buf;
285 
286 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
287 	if (!buf)
288 		return -ENOMEM;
289 
290 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
291 			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
292 			      MOS_WDR_TIMEOUT);
293 	*val = buf[0];
294 	dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
295 
296 	kfree(buf);
297 	return ret;
298 }
299 
300 /*
301  * mos7840_set_uart_reg
302  *	To set the Uart register by calling usb_fill_control_urb function by
303  *	passing usb_sndctrlpipe function as parameter.
304  */
305 
306 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
307 				__u16 val)
308 {
309 
310 	struct usb_device *dev = port->serial->dev;
311 	val = val & 0x00ff;
312 	/* For the UART control registers, the application number need
313 	   to be Or'ed */
314 	if (port->serial->num_ports == 4) {
315 		val |= (((__u16) port->number -
316 				(__u16) (port->serial->minor)) + 1) << 8;
317 	} else {
318 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
319 			val |= (((__u16) port->number -
320 			      (__u16) (port->serial->minor)) + 1) << 8;
321 		} else {
322 			val |= (((__u16) port->number -
323 			      (__u16) (port->serial->minor)) + 2) << 8;
324 		}
325 	}
326 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
327 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
328 			       MCS_WR_RTYPE, val, reg, NULL, 0,
329 			       MOS_WDR_TIMEOUT);
330 
331 }
332 
333 /*
334  * mos7840_get_uart_reg
335  *	To set the Control register by calling usb_fill_control_urb function
336  *	by passing usb_rcvctrlpipe function as parameter.
337  */
338 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
339 				__u16 *val)
340 {
341 	struct usb_device *dev = port->serial->dev;
342 	int ret = 0;
343 	__u16 Wval;
344 	u8 *buf;
345 
346 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
347 	if (!buf)
348 		return -ENOMEM;
349 
350 	/* Wval  is same as application number */
351 	if (port->serial->num_ports == 4) {
352 		Wval =
353 		    (((__u16) port->number - (__u16) (port->serial->minor)) +
354 		     1) << 8;
355 	} else {
356 		if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
357 			Wval = (((__u16) port->number -
358 			      (__u16) (port->serial->minor)) + 1) << 8;
359 		} else {
360 			Wval = (((__u16) port->number -
361 			      (__u16) (port->serial->minor)) + 2) << 8;
362 		}
363 	}
364 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
365 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
366 			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
367 			      MOS_WDR_TIMEOUT);
368 	*val = buf[0];
369 
370 	kfree(buf);
371 	return ret;
372 }
373 
374 static void mos7840_dump_serial_port(struct usb_serial_port *port,
375 				     struct moschip_port *mos7840_port)
376 {
377 
378 	dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
379 	dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
380 	dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
381 
382 }
383 
384 /************************************************************************/
385 /************************************************************************/
386 /*             I N T E R F A C E   F U N C T I O N S			*/
387 /*             I N T E R F A C E   F U N C T I O N S			*/
388 /************************************************************************/
389 /************************************************************************/
390 
391 static inline void mos7840_set_port_private(struct usb_serial_port *port,
392 					    struct moschip_port *data)
393 {
394 	usb_set_serial_port_data(port, (void *)data);
395 }
396 
397 static inline struct moschip_port *mos7840_get_port_private(struct
398 							    usb_serial_port
399 							    *port)
400 {
401 	return (struct moschip_port *)usb_get_serial_port_data(port);
402 }
403 
404 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
405 {
406 	struct moschip_port *mos7840_port;
407 	struct async_icount *icount;
408 	mos7840_port = port;
409 	icount = &mos7840_port->icount;
410 	if (new_msr &
411 	    (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
412 	     MOS_MSR_DELTA_CD)) {
413 		icount = &mos7840_port->icount;
414 
415 		/* update input line counters */
416 		if (new_msr & MOS_MSR_DELTA_CTS) {
417 			icount->cts++;
418 			smp_wmb();
419 		}
420 		if (new_msr & MOS_MSR_DELTA_DSR) {
421 			icount->dsr++;
422 			smp_wmb();
423 		}
424 		if (new_msr & MOS_MSR_DELTA_CD) {
425 			icount->dcd++;
426 			smp_wmb();
427 		}
428 		if (new_msr & MOS_MSR_DELTA_RI) {
429 			icount->rng++;
430 			smp_wmb();
431 		}
432 	}
433 }
434 
435 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
436 {
437 	struct async_icount *icount;
438 
439 	if (new_lsr & SERIAL_LSR_BI) {
440 		/*
441 		 * Parity and Framing errors only count if they
442 		 * occur exclusive of a break being
443 		 * received.
444 		 */
445 		new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
446 	}
447 
448 	/* update input line counters */
449 	icount = &port->icount;
450 	if (new_lsr & SERIAL_LSR_BI) {
451 		icount->brk++;
452 		smp_wmb();
453 	}
454 	if (new_lsr & SERIAL_LSR_OE) {
455 		icount->overrun++;
456 		smp_wmb();
457 	}
458 	if (new_lsr & SERIAL_LSR_PE) {
459 		icount->parity++;
460 		smp_wmb();
461 	}
462 	if (new_lsr & SERIAL_LSR_FE) {
463 		icount->frame++;
464 		smp_wmb();
465 	}
466 }
467 
468 /************************************************************************/
469 /************************************************************************/
470 /*            U S B  C A L L B A C K   F U N C T I O N S                */
471 /*            U S B  C A L L B A C K   F U N C T I O N S                */
472 /************************************************************************/
473 /************************************************************************/
474 
475 static void mos7840_control_callback(struct urb *urb)
476 {
477 	unsigned char *data;
478 	struct moschip_port *mos7840_port;
479 	struct device *dev = &urb->dev->dev;
480 	__u8 regval = 0x0;
481 	int result = 0;
482 	int status = urb->status;
483 
484 	mos7840_port = urb->context;
485 
486 	switch (status) {
487 	case 0:
488 		/* success */
489 		break;
490 	case -ECONNRESET:
491 	case -ENOENT:
492 	case -ESHUTDOWN:
493 		/* this urb is terminated, clean up */
494 		dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
495 		return;
496 	default:
497 		dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
498 		goto exit;
499 	}
500 
501 	dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
502 	dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
503 		mos7840_port->MsrLsr, mos7840_port->port_num);
504 	data = urb->transfer_buffer;
505 	regval = (__u8) data[0];
506 	dev_dbg(dev, "%s data is %x\n", __func__, regval);
507 	if (mos7840_port->MsrLsr == 0)
508 		mos7840_handle_new_msr(mos7840_port, regval);
509 	else if (mos7840_port->MsrLsr == 1)
510 		mos7840_handle_new_lsr(mos7840_port, regval);
511 
512 exit:
513 	spin_lock(&mos7840_port->pool_lock);
514 	if (!mos7840_port->zombie)
515 		result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
516 	spin_unlock(&mos7840_port->pool_lock);
517 	if (result) {
518 		dev_err(dev, "%s - Error %d submitting interrupt urb\n",
519 			__func__, result);
520 	}
521 }
522 
523 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
524 			   __u16 *val)
525 {
526 	struct usb_device *dev = mcs->port->serial->dev;
527 	struct usb_ctrlrequest *dr = mcs->dr;
528 	unsigned char *buffer = mcs->ctrl_buf;
529 	int ret;
530 
531 	dr->bRequestType = MCS_RD_RTYPE;
532 	dr->bRequest = MCS_RDREQ;
533 	dr->wValue = cpu_to_le16(Wval);	/* 0 */
534 	dr->wIndex = cpu_to_le16(reg);
535 	dr->wLength = cpu_to_le16(2);
536 
537 	usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
538 			     (unsigned char *)dr, buffer, 2,
539 			     mos7840_control_callback, mcs);
540 	mcs->control_urb->transfer_buffer_length = 2;
541 	ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
542 	return ret;
543 }
544 
545 static void mos7840_set_led_callback(struct urb *urb)
546 {
547 	switch (urb->status) {
548 	case 0:
549 		/* Success */
550 		break;
551 	case -ECONNRESET:
552 	case -ENOENT:
553 	case -ESHUTDOWN:
554 		/* This urb is terminated, clean up */
555 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
556 			__func__, urb->status);
557 		break;
558 	default:
559 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
560 			__func__, urb->status);
561 	}
562 }
563 
564 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
565 				__u16 reg)
566 {
567 	struct usb_device *dev = mcs->port->serial->dev;
568 	struct usb_ctrlrequest *dr = mcs->dr;
569 
570 	dr->bRequestType = MCS_WR_RTYPE;
571 	dr->bRequest = MCS_WRREQ;
572 	dr->wValue = cpu_to_le16(wval);
573 	dr->wIndex = cpu_to_le16(reg);
574 	dr->wLength = cpu_to_le16(0);
575 
576 	usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
577 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
578 
579 	usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
580 }
581 
582 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
583 				__u16 val)
584 {
585 	struct usb_device *dev = port->serial->dev;
586 
587 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
588 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
589 }
590 
591 static void mos7840_led_off(unsigned long arg)
592 {
593 	struct moschip_port *mcs = (struct moschip_port *) arg;
594 
595 	/* Turn off LED */
596 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
597 	mod_timer(&mcs->led_timer2,
598 				jiffies + msecs_to_jiffies(LED_OFF_MS));
599 }
600 
601 static void mos7840_led_flag_off(unsigned long arg)
602 {
603 	struct moschip_port *mcs = (struct moschip_port *) arg;
604 
605 	mcs->led_flag = false;
606 }
607 
608 /*****************************************************************************
609  * mos7840_interrupt_callback
610  *	this is the callback function for when we have received data on the
611  *	interrupt endpoint.
612  *****************************************************************************/
613 
614 static void mos7840_interrupt_callback(struct urb *urb)
615 {
616 	int result;
617 	int length;
618 	struct moschip_port *mos7840_port;
619 	struct usb_serial *serial;
620 	__u16 Data;
621 	unsigned char *data;
622 	__u8 sp[5], st;
623 	int i, rv = 0;
624 	__u16 wval, wreg = 0;
625 	int status = urb->status;
626 
627 	switch (status) {
628 	case 0:
629 		/* success */
630 		break;
631 	case -ECONNRESET:
632 	case -ENOENT:
633 	case -ESHUTDOWN:
634 		/* this urb is terminated, clean up */
635 		dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
636 			__func__, status);
637 		return;
638 	default:
639 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
640 			__func__, status);
641 		goto exit;
642 	}
643 
644 	length = urb->actual_length;
645 	data = urb->transfer_buffer;
646 
647 	serial = urb->context;
648 
649 	/* Moschip get 5 bytes
650 	 * Byte 1 IIR Port 1 (port.number is 0)
651 	 * Byte 2 IIR Port 2 (port.number is 1)
652 	 * Byte 3 IIR Port 3 (port.number is 2)
653 	 * Byte 4 IIR Port 4 (port.number is 3)
654 	 * Byte 5 FIFO status for both */
655 
656 	if (length && length > 5) {
657 		dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
658 		return;
659 	}
660 
661 	sp[0] = (__u8) data[0];
662 	sp[1] = (__u8) data[1];
663 	sp[2] = (__u8) data[2];
664 	sp[3] = (__u8) data[3];
665 	st = (__u8) data[4];
666 
667 	for (i = 0; i < serial->num_ports; i++) {
668 		mos7840_port = mos7840_get_port_private(serial->port[i]);
669 		wval =
670 		    (((__u16) serial->port[i]->number -
671 		      (__u16) (serial->minor)) + 1) << 8;
672 		if (mos7840_port->open) {
673 			if (sp[i] & 0x01) {
674 				dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
675 			} else {
676 				switch (sp[i] & 0x0f) {
677 				case SERIAL_IIR_RLS:
678 					dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
679 					dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
680 					mos7840_port->MsrLsr = 1;
681 					wreg = LINE_STATUS_REGISTER;
682 					break;
683 				case SERIAL_IIR_MS:
684 					dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
685 					mos7840_port->MsrLsr = 0;
686 					wreg = MODEM_STATUS_REGISTER;
687 					break;
688 				}
689 				spin_lock(&mos7840_port->pool_lock);
690 				if (!mos7840_port->zombie) {
691 					rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
692 				} else {
693 					spin_unlock(&mos7840_port->pool_lock);
694 					return;
695 				}
696 				spin_unlock(&mos7840_port->pool_lock);
697 			}
698 		}
699 	}
700 	if (!(rv < 0))
701 		/* the completion handler for the control urb will resubmit */
702 		return;
703 exit:
704 	result = usb_submit_urb(urb, GFP_ATOMIC);
705 	if (result) {
706 		dev_err(&urb->dev->dev,
707 			"%s - Error %d submitting interrupt urb\n",
708 			__func__, result);
709 	}
710 }
711 
712 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
713 				       const char *function)
714 {
715 	if (!port) {
716 		pr_debug("%s - port == NULL\n", function);
717 		return -1;
718 	}
719 	if (!port->serial) {
720 		pr_debug("%s - port->serial == NULL\n", function);
721 		return -1;
722 	}
723 
724 	return 0;
725 }
726 
727 /* Inline functions to check the sanity of a pointer that is passed to us */
728 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
729 					 const char *function)
730 {
731 	if (!serial) {
732 		pr_debug("%s - serial == NULL\n", function);
733 		return -1;
734 	}
735 	if (!serial->type) {
736 		pr_debug("%s - serial->type == NULL!\n", function);
737 		return -1;
738 	}
739 
740 	return 0;
741 }
742 
743 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
744 						 const char *function)
745 {
746 	/* if no port was specified, or it fails a paranoia check */
747 	if (!port ||
748 	    mos7840_port_paranoia_check(port, function) ||
749 	    mos7840_serial_paranoia_check(port->serial, function)) {
750 		/* then say that we don't have a valid usb_serial thing,
751 		 * which will end up genrating -ENODEV return values */
752 		return NULL;
753 	}
754 
755 	return port->serial;
756 }
757 
758 /*****************************************************************************
759  * mos7840_bulk_in_callback
760  *	this is the callback function for when we have received data on the
761  *	bulk in endpoint.
762  *****************************************************************************/
763 
764 static void mos7840_bulk_in_callback(struct urb *urb)
765 {
766 	int retval;
767 	unsigned char *data;
768 	struct usb_serial *serial;
769 	struct usb_serial_port *port;
770 	struct moschip_port *mos7840_port;
771 	struct tty_struct *tty;
772 	int status = urb->status;
773 
774 	mos7840_port = urb->context;
775 	if (!mos7840_port)
776 		return;
777 
778 	if (status) {
779 		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
780 		mos7840_port->read_urb_busy = false;
781 		return;
782 	}
783 
784 	port = mos7840_port->port;
785 	if (mos7840_port_paranoia_check(port, __func__)) {
786 		mos7840_port->read_urb_busy = false;
787 		return;
788 	}
789 
790 	serial = mos7840_get_usb_serial(port, __func__);
791 	if (!serial) {
792 		mos7840_port->read_urb_busy = false;
793 		return;
794 	}
795 
796 	data = urb->transfer_buffer;
797 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
798 
799 	if (urb->actual_length) {
800 		tty = tty_port_tty_get(&mos7840_port->port->port);
801 		if (tty) {
802 			tty_insert_flip_string(tty, data, urb->actual_length);
803 			tty_flip_buffer_push(tty);
804 			tty_kref_put(tty);
805 		}
806 		mos7840_port->icount.rx += urb->actual_length;
807 		smp_wmb();
808 		dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx);
809 	}
810 
811 	if (!mos7840_port->read_urb) {
812 		dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
813 		mos7840_port->read_urb_busy = false;
814 		return;
815 	}
816 
817 	/* Turn on LED */
818 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
819 		mos7840_port->led_flag = true;
820 		mos7840_set_led_async(mos7840_port, 0x0301,
821 					MODEM_CONTROL_REGISTER);
822 		mod_timer(&mos7840_port->led_timer1,
823 				jiffies + msecs_to_jiffies(LED_ON_MS));
824 	}
825 
826 	mos7840_port->read_urb_busy = true;
827 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
828 
829 	if (retval) {
830 		dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
831 		mos7840_port->read_urb_busy = false;
832 	}
833 }
834 
835 /*****************************************************************************
836  * mos7840_bulk_out_data_callback
837  *	this is the callback function for when we have finished sending
838  *	serial data on the bulk out endpoint.
839  *****************************************************************************/
840 
841 static void mos7840_bulk_out_data_callback(struct urb *urb)
842 {
843 	struct moschip_port *mos7840_port;
844 	struct usb_serial_port *port;
845 	struct tty_struct *tty;
846 	int status = urb->status;
847 	int i;
848 
849 	mos7840_port = urb->context;
850 	port = mos7840_port->port;
851 	spin_lock(&mos7840_port->pool_lock);
852 	for (i = 0; i < NUM_URBS; i++) {
853 		if (urb == mos7840_port->write_urb_pool[i]) {
854 			mos7840_port->busy[i] = 0;
855 			break;
856 		}
857 	}
858 	spin_unlock(&mos7840_port->pool_lock);
859 
860 	if (status) {
861 		dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
862 		return;
863 	}
864 
865 	if (mos7840_port_paranoia_check(port, __func__))
866 		return;
867 
868 	tty = tty_port_tty_get(&port->port);
869 	if (tty && mos7840_port->open)
870 		tty_wakeup(tty);
871 	tty_kref_put(tty);
872 
873 }
874 
875 /************************************************************************/
876 /*       D R I V E R  T T Y  I N T E R F A C E  F U N C T I O N S       */
877 /************************************************************************/
878 #ifdef MCSSerialProbe
879 static int mos7840_serial_probe(struct usb_serial *serial,
880 				const struct usb_device_id *id)
881 {
882 
883 	/*need to implement the mode_reg reading and updating\
884 	   structures usb_serial_ device_type\
885 	   (i.e num_ports, num_bulkin,bulkout etc) */
886 	/* Also we can update the changes  attach */
887 	return 1;
888 }
889 #endif
890 
891 /*****************************************************************************
892  * mos7840_open
893  *	this function is called by the tty driver when a port is opened
894  *	If successful, we return 0
895  *	Otherwise we return a negative error number.
896  *****************************************************************************/
897 
898 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
899 {
900 	int response;
901 	int j;
902 	struct usb_serial *serial;
903 	struct urb *urb;
904 	__u16 Data;
905 	int status;
906 	struct moschip_port *mos7840_port;
907 	struct moschip_port *port0;
908 
909 	if (mos7840_port_paranoia_check(port, __func__))
910 		return -ENODEV;
911 
912 	serial = port->serial;
913 
914 	if (mos7840_serial_paranoia_check(serial, __func__))
915 		return -ENODEV;
916 
917 	mos7840_port = mos7840_get_port_private(port);
918 	port0 = mos7840_get_port_private(serial->port[0]);
919 
920 	if (mos7840_port == NULL || port0 == NULL)
921 		return -ENODEV;
922 
923 	usb_clear_halt(serial->dev, port->write_urb->pipe);
924 	usb_clear_halt(serial->dev, port->read_urb->pipe);
925 	port0->open_ports++;
926 
927 	/* Initialising the write urb pool */
928 	for (j = 0; j < NUM_URBS; ++j) {
929 		urb = usb_alloc_urb(0, GFP_KERNEL);
930 		mos7840_port->write_urb_pool[j] = urb;
931 
932 		if (urb == NULL) {
933 			dev_err(&port->dev, "No more urbs???\n");
934 			continue;
935 		}
936 
937 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
938 								GFP_KERNEL);
939 		if (!urb->transfer_buffer) {
940 			usb_free_urb(urb);
941 			mos7840_port->write_urb_pool[j] = NULL;
942 			dev_err(&port->dev,
943 				"%s-out of memory for urb buffers.\n",
944 				__func__);
945 			continue;
946 		}
947 	}
948 
949 /*****************************************************************************
950  * Initialize MCS7840 -- Write Init values to corresponding Registers
951  *
952  * Register Index
953  * 1 : IER
954  * 2 : FCR
955  * 3 : LCR
956  * 4 : MCR
957  *
958  * 0x08 : SP1/2 Control Reg
959  *****************************************************************************/
960 
961 	/* NEED to check the following Block */
962 
963 	Data = 0x0;
964 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
965 	if (status < 0) {
966 		dev_dbg(&port->dev, "Reading Spreg failed\n");
967 		return -1;
968 	}
969 	Data |= 0x80;
970 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
971 	if (status < 0) {
972 		dev_dbg(&port->dev, "writing Spreg failed\n");
973 		return -1;
974 	}
975 
976 	Data &= ~0x80;
977 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
978 	if (status < 0) {
979 		dev_dbg(&port->dev, "writing Spreg failed\n");
980 		return -1;
981 	}
982 	/* End of block to be checked */
983 
984 	Data = 0x0;
985 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
986 									&Data);
987 	if (status < 0) {
988 		dev_dbg(&port->dev, "Reading Controlreg failed\n");
989 		return -1;
990 	}
991 	Data |= 0x08;		/* Driver done bit */
992 	Data |= 0x20;		/* rx_disable */
993 	status = mos7840_set_reg_sync(port,
994 				mos7840_port->ControlRegOffset, Data);
995 	if (status < 0) {
996 		dev_dbg(&port->dev, "writing Controlreg failed\n");
997 		return -1;
998 	}
999 	/* do register settings here */
1000 	/* Set all regs to the device default values. */
1001 	/***********************************
1002 	 * First Disable all interrupts.
1003 	 ***********************************/
1004 	Data = 0x00;
1005 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1006 	if (status < 0) {
1007 		dev_dbg(&port->dev, "disabling interrupts failed\n");
1008 		return -1;
1009 	}
1010 	/* Set FIFO_CONTROL_REGISTER to the default value */
1011 	Data = 0x00;
1012 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1013 	if (status < 0) {
1014 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
1015 		return -1;
1016 	}
1017 
1018 	Data = 0xcf;
1019 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1020 	if (status < 0) {
1021 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
1022 		return -1;
1023 	}
1024 
1025 	Data = 0x03;
1026 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1027 	mos7840_port->shadowLCR = Data;
1028 
1029 	Data = 0x0b;
1030 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1031 	mos7840_port->shadowMCR = Data;
1032 
1033 	Data = 0x00;
1034 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1035 	mos7840_port->shadowLCR = Data;
1036 
1037 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
1038 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1039 
1040 	Data = 0x0c;
1041 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1042 
1043 	Data = 0x0;
1044 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1045 
1046 	Data = 0x00;
1047 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1048 
1049 	Data = Data & ~SERIAL_LCR_DLAB;
1050 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1051 	mos7840_port->shadowLCR = Data;
1052 
1053 	/* clearing Bulkin and Bulkout Fifo */
1054 	Data = 0x0;
1055 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1056 
1057 	Data = Data | 0x0c;
1058 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1059 
1060 	Data = Data & ~0x0c;
1061 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1062 	/* Finally enable all interrupts */
1063 	Data = 0x0c;
1064 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1065 
1066 	/* clearing rx_disable */
1067 	Data = 0x0;
1068 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1069 									&Data);
1070 	Data = Data & ~0x20;
1071 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1072 									Data);
1073 
1074 	/* rx_negate */
1075 	Data = 0x0;
1076 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1077 									&Data);
1078 	Data = Data | 0x10;
1079 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1080 									Data);
1081 
1082 	/* Check to see if we've set up our endpoint info yet    *
1083 	 * (can't set it up in mos7840_startup as the structures *
1084 	 * were not set up at that time.)                        */
1085 	if (port0->open_ports == 1) {
1086 		if (serial->port[0]->interrupt_in_buffer == NULL) {
1087 			/* set up interrupt urb */
1088 			usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1089 				serial->dev,
1090 				usb_rcvintpipe(serial->dev,
1091 				serial->port[0]->interrupt_in_endpointAddress),
1092 				serial->port[0]->interrupt_in_buffer,
1093 				serial->port[0]->interrupt_in_urb->
1094 				transfer_buffer_length,
1095 				mos7840_interrupt_callback,
1096 				serial,
1097 				serial->port[0]->interrupt_in_urb->interval);
1098 
1099 			/* start interrupt read for mos7840               *
1100 			 * will continue as long as mos7840 is connected  */
1101 
1102 			response =
1103 			    usb_submit_urb(serial->port[0]->interrupt_in_urb,
1104 					   GFP_KERNEL);
1105 			if (response) {
1106 				dev_err(&port->dev, "%s - Error %d submitting "
1107 					"interrupt urb\n", __func__, response);
1108 			}
1109 
1110 		}
1111 
1112 	}
1113 
1114 	/* see if we've set up our endpoint info yet   *
1115 	 * (can't set it up in mos7840_startup as the  *
1116 	 * structures were not set up at that time.)   */
1117 
1118 	dev_dbg(&port->dev, "port number is %d\n", port->number);
1119 	dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1120 	dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1121 	dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1122 	dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1123 	dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1124 	mos7840_port->read_urb = port->read_urb;
1125 
1126 	/* set up our bulk in urb */
1127 	if ((serial->num_ports == 2)
1128 		&& ((((__u16)port->number -
1129 			(__u16)(port->serial->minor)) % 2) != 0)) {
1130 		usb_fill_bulk_urb(mos7840_port->read_urb,
1131 			serial->dev,
1132 			usb_rcvbulkpipe(serial->dev,
1133 				(port->bulk_in_endpointAddress) + 2),
1134 			port->bulk_in_buffer,
1135 			mos7840_port->read_urb->transfer_buffer_length,
1136 			mos7840_bulk_in_callback, mos7840_port);
1137 	} else {
1138 		usb_fill_bulk_urb(mos7840_port->read_urb,
1139 			serial->dev,
1140 			usb_rcvbulkpipe(serial->dev,
1141 				port->bulk_in_endpointAddress),
1142 			port->bulk_in_buffer,
1143 			mos7840_port->read_urb->transfer_buffer_length,
1144 			mos7840_bulk_in_callback, mos7840_port);
1145 	}
1146 
1147 	dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1148 	mos7840_port->read_urb_busy = true;
1149 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1150 	if (response) {
1151 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1152 			__func__, response);
1153 		mos7840_port->read_urb_busy = false;
1154 	}
1155 
1156 	/* initialize our wait queues */
1157 	init_waitqueue_head(&mos7840_port->wait_chase);
1158 	init_waitqueue_head(&mos7840_port->delta_msr_wait);
1159 
1160 	/* initialize our icount structure */
1161 	memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1162 
1163 	/* initialize our port settings */
1164 	/* Must set to enable ints! */
1165 	mos7840_port->shadowMCR = MCR_MASTER_IE;
1166 	/* send a open port command */
1167 	mos7840_port->open = 1;
1168 	/* mos7840_change_port_settings(mos7840_port,old_termios); */
1169 	mos7840_port->icount.tx = 0;
1170 	mos7840_port->icount.rx = 0;
1171 
1172 	return 0;
1173 }
1174 
1175 /*****************************************************************************
1176  * mos7840_chars_in_buffer
1177  *	this function is called by the tty driver when it wants to know how many
1178  *	bytes of data we currently have outstanding in the port (data that has
1179  *	been written, but hasn't made it out the port yet)
1180  *	If successful, we return the number of bytes left to be written in the
1181  *	system,
1182  *	Otherwise we return zero.
1183  *****************************************************************************/
1184 
1185 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1186 {
1187 	struct usb_serial_port *port = tty->driver_data;
1188 	int i;
1189 	int chars = 0;
1190 	unsigned long flags;
1191 	struct moschip_port *mos7840_port;
1192 
1193 	if (mos7840_port_paranoia_check(port, __func__))
1194 		return 0;
1195 
1196 	mos7840_port = mos7840_get_port_private(port);
1197 	if (mos7840_port == NULL)
1198 		return 0;
1199 
1200 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1201 	for (i = 0; i < NUM_URBS; ++i) {
1202 		if (mos7840_port->busy[i]) {
1203 			struct urb *urb = mos7840_port->write_urb_pool[i];
1204 			chars += urb->transfer_buffer_length;
1205 		}
1206 	}
1207 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1208 	dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1209 	return chars;
1210 
1211 }
1212 
1213 /*****************************************************************************
1214  * mos7840_close
1215  *	this function is called by the tty driver when a port is closed
1216  *****************************************************************************/
1217 
1218 static void mos7840_close(struct usb_serial_port *port)
1219 {
1220 	struct usb_serial *serial;
1221 	struct moschip_port *mos7840_port;
1222 	struct moschip_port *port0;
1223 	int j;
1224 	__u16 Data;
1225 
1226 	if (mos7840_port_paranoia_check(port, __func__))
1227 		return;
1228 
1229 	serial = mos7840_get_usb_serial(port, __func__);
1230 	if (!serial)
1231 		return;
1232 
1233 	mos7840_port = mos7840_get_port_private(port);
1234 	port0 = mos7840_get_port_private(serial->port[0]);
1235 
1236 	if (mos7840_port == NULL || port0 == NULL)
1237 		return;
1238 
1239 	for (j = 0; j < NUM_URBS; ++j)
1240 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
1241 
1242 	/* Freeing Write URBs */
1243 	for (j = 0; j < NUM_URBS; ++j) {
1244 		if (mos7840_port->write_urb_pool[j]) {
1245 			if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1246 				kfree(mos7840_port->write_urb_pool[j]->
1247 				      transfer_buffer);
1248 
1249 			usb_free_urb(mos7840_port->write_urb_pool[j]);
1250 		}
1251 	}
1252 
1253 	/* While closing port, shutdown all bulk read, write  *
1254 	 * and interrupt read if they exists                  */
1255 	if (serial->dev) {
1256 		if (mos7840_port->write_urb) {
1257 			dev_dbg(&port->dev, "%s", "Shutdown bulk write\n");
1258 			usb_kill_urb(mos7840_port->write_urb);
1259 		}
1260 		if (mos7840_port->read_urb) {
1261 			dev_dbg(&port->dev, "%s", "Shutdown bulk read\n");
1262 			usb_kill_urb(mos7840_port->read_urb);
1263 			mos7840_port->read_urb_busy = false;
1264 		}
1265 		if ((&mos7840_port->control_urb)) {
1266 			dev_dbg(&port->dev, "%s", "Shutdown control read\n");
1267 			/*/      usb_kill_urb (mos7840_port->control_urb); */
1268 		}
1269 	}
1270 /*      if(mos7840_port->ctrl_buf != NULL) */
1271 /*              kfree(mos7840_port->ctrl_buf); */
1272 	port0->open_ports--;
1273 	dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
1274 	if (port0->open_ports == 0) {
1275 		if (serial->port[0]->interrupt_in_urb) {
1276 			dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1277 			usb_kill_urb(serial->port[0]->interrupt_in_urb);
1278 		}
1279 	}
1280 
1281 	if (mos7840_port->write_urb) {
1282 		/* if this urb had a transfer buffer already (old tx) free it */
1283 		if (mos7840_port->write_urb->transfer_buffer != NULL)
1284 			kfree(mos7840_port->write_urb->transfer_buffer);
1285 		usb_free_urb(mos7840_port->write_urb);
1286 	}
1287 
1288 	Data = 0x0;
1289 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1290 
1291 	Data = 0x00;
1292 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1293 
1294 	mos7840_port->open = 0;
1295 }
1296 
1297 /************************************************************************
1298  *
1299  * mos7840_block_until_chase_response
1300  *
1301  *	This function will block the close until one of the following:
1302  *		1. Response to our Chase comes from mos7840
1303  *		2. A timeout of 10 seconds without activity has expired
1304  *		   (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1305  *
1306  ************************************************************************/
1307 
1308 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1309 					struct moschip_port *mos7840_port)
1310 {
1311 	int timeout = msecs_to_jiffies(1000);
1312 	int wait = 10;
1313 	int count;
1314 
1315 	while (1) {
1316 		count = mos7840_chars_in_buffer(tty);
1317 
1318 		/* Check for Buffer status */
1319 		if (count <= 0)
1320 			return;
1321 
1322 		/* Block the thread for a while */
1323 		interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1324 					       timeout);
1325 		/* No activity.. count down section */
1326 		wait--;
1327 		if (wait == 0) {
1328 			dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
1329 			return;
1330 		} else {
1331 			/* Reset timeout value back to seconds */
1332 			wait = 10;
1333 		}
1334 	}
1335 
1336 }
1337 
1338 /*****************************************************************************
1339  * mos7840_break
1340  *	this function sends a break to the port
1341  *****************************************************************************/
1342 static void mos7840_break(struct tty_struct *tty, int break_state)
1343 {
1344 	struct usb_serial_port *port = tty->driver_data;
1345 	unsigned char data;
1346 	struct usb_serial *serial;
1347 	struct moschip_port *mos7840_port;
1348 
1349 	if (mos7840_port_paranoia_check(port, __func__))
1350 		return;
1351 
1352 	serial = mos7840_get_usb_serial(port, __func__);
1353 	if (!serial)
1354 		return;
1355 
1356 	mos7840_port = mos7840_get_port_private(port);
1357 
1358 	if (mos7840_port == NULL)
1359 		return;
1360 
1361 	if (serial->dev)
1362 		/* flush and block until tx is empty */
1363 		mos7840_block_until_chase_response(tty, mos7840_port);
1364 
1365 	if (break_state == -1)
1366 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1367 	else
1368 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1369 
1370 	/* FIXME: no locking on shadowLCR anywhere in driver */
1371 	mos7840_port->shadowLCR = data;
1372 	dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1373 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1374 			     mos7840_port->shadowLCR);
1375 }
1376 
1377 /*****************************************************************************
1378  * mos7840_write_room
1379  *	this function is called by the tty driver when it wants to know how many
1380  *	bytes of data we can accept for a specific port.
1381  *	If successful, we return the amount of room that we have for this port
1382  *	Otherwise we return a negative error number.
1383  *****************************************************************************/
1384 
1385 static int mos7840_write_room(struct tty_struct *tty)
1386 {
1387 	struct usb_serial_port *port = tty->driver_data;
1388 	int i;
1389 	int room = 0;
1390 	unsigned long flags;
1391 	struct moschip_port *mos7840_port;
1392 
1393 	if (mos7840_port_paranoia_check(port, __func__))
1394 		return -1;
1395 
1396 	mos7840_port = mos7840_get_port_private(port);
1397 	if (mos7840_port == NULL)
1398 		return -1;
1399 
1400 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1401 	for (i = 0; i < NUM_URBS; ++i) {
1402 		if (!mos7840_port->busy[i])
1403 			room += URB_TRANSFER_BUFFER_SIZE;
1404 	}
1405 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1406 
1407 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1408 	dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1409 	return room;
1410 
1411 }
1412 
1413 /*****************************************************************************
1414  * mos7840_write
1415  *	this function is called by the tty driver when data should be written to
1416  *	the port.
1417  *	If successful, we return the number of bytes written, otherwise we
1418  *      return a negative error number.
1419  *****************************************************************************/
1420 
1421 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1422 			 const unsigned char *data, int count)
1423 {
1424 	int status;
1425 	int i;
1426 	int bytes_sent = 0;
1427 	int transfer_size;
1428 	unsigned long flags;
1429 
1430 	struct moschip_port *mos7840_port;
1431 	struct usb_serial *serial;
1432 	struct urb *urb;
1433 	/* __u16 Data; */
1434 	const unsigned char *current_position = data;
1435 	unsigned char *data1;
1436 
1437 #ifdef NOTMOS7840
1438 	Data = 0x00;
1439 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1440 	mos7840_port->shadowLCR = Data;
1441 	dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1442 	dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1443 
1444 	/* Data = 0x03; */
1445 	/* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1446 	/* mos7840_port->shadowLCR=Data;//Need to add later */
1447 
1448 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
1449 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1450 
1451 	/* Data = 0x0c; */
1452 	/* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1453 	Data = 0x00;
1454 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1455 	dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
1456 
1457 	Data = 0x0;
1458 	status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1459 	dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
1460 
1461 	Data = Data & ~SERIAL_LCR_DLAB;
1462 	dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1463 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1464 #endif
1465 
1466 	if (mos7840_port_paranoia_check(port, __func__))
1467 		return -1;
1468 
1469 	serial = port->serial;
1470 	if (mos7840_serial_paranoia_check(serial, __func__))
1471 		return -1;
1472 
1473 	mos7840_port = mos7840_get_port_private(port);
1474 	if (mos7840_port == NULL)
1475 		return -1;
1476 
1477 	/* try to find a free urb in the list */
1478 	urb = NULL;
1479 
1480 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1481 	for (i = 0; i < NUM_URBS; ++i) {
1482 		if (!mos7840_port->busy[i]) {
1483 			mos7840_port->busy[i] = 1;
1484 			urb = mos7840_port->write_urb_pool[i];
1485 			dev_dbg(&port->dev, "URB:%d\n", i);
1486 			break;
1487 		}
1488 	}
1489 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1490 
1491 	if (urb == NULL) {
1492 		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1493 		goto exit;
1494 	}
1495 
1496 	if (urb->transfer_buffer == NULL) {
1497 		urb->transfer_buffer =
1498 		    kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1499 
1500 		if (urb->transfer_buffer == NULL) {
1501 			dev_err_console(port, "%s no more kernel memory...\n",
1502 				__func__);
1503 			goto exit;
1504 		}
1505 	}
1506 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1507 
1508 	memcpy(urb->transfer_buffer, current_position, transfer_size);
1509 
1510 	/* fill urb with data and submit  */
1511 	if ((serial->num_ports == 2)
1512 		&& ((((__u16)port->number -
1513 			(__u16)(port->serial->minor)) % 2) != 0)) {
1514 		usb_fill_bulk_urb(urb,
1515 			serial->dev,
1516 			usb_sndbulkpipe(serial->dev,
1517 				(port->bulk_out_endpointAddress) + 2),
1518 			urb->transfer_buffer,
1519 			transfer_size,
1520 			mos7840_bulk_out_data_callback, mos7840_port);
1521 	} else {
1522 		usb_fill_bulk_urb(urb,
1523 			serial->dev,
1524 			usb_sndbulkpipe(serial->dev,
1525 				port->bulk_out_endpointAddress),
1526 			urb->transfer_buffer,
1527 			transfer_size,
1528 			mos7840_bulk_out_data_callback, mos7840_port);
1529 	}
1530 
1531 	data1 = urb->transfer_buffer;
1532 	dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1533 
1534 	/* Turn on LED */
1535 	if (mos7840_port->has_led && !mos7840_port->led_flag) {
1536 		mos7840_port->led_flag = true;
1537 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1538 		mod_timer(&mos7840_port->led_timer1,
1539 				jiffies + msecs_to_jiffies(LED_ON_MS));
1540 	}
1541 
1542 	/* send it down the pipe */
1543 	status = usb_submit_urb(urb, GFP_ATOMIC);
1544 
1545 	if (status) {
1546 		mos7840_port->busy[i] = 0;
1547 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1548 			"with status = %d\n", __func__, status);
1549 		bytes_sent = status;
1550 		goto exit;
1551 	}
1552 	bytes_sent = transfer_size;
1553 	mos7840_port->icount.tx += transfer_size;
1554 	smp_wmb();
1555 	dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1556 exit:
1557 	return bytes_sent;
1558 
1559 }
1560 
1561 /*****************************************************************************
1562  * mos7840_throttle
1563  *	this function is called by the tty driver when it wants to stop the data
1564  *	being read from the port.
1565  *****************************************************************************/
1566 
1567 static void mos7840_throttle(struct tty_struct *tty)
1568 {
1569 	struct usb_serial_port *port = tty->driver_data;
1570 	struct moschip_port *mos7840_port;
1571 	int status;
1572 
1573 	if (mos7840_port_paranoia_check(port, __func__))
1574 		return;
1575 
1576 	mos7840_port = mos7840_get_port_private(port);
1577 
1578 	if (mos7840_port == NULL)
1579 		return;
1580 
1581 	if (!mos7840_port->open) {
1582 		dev_dbg(&port->dev, "%s", "port not opened\n");
1583 		return;
1584 	}
1585 
1586 	/* if we are implementing XON/XOFF, send the stop character */
1587 	if (I_IXOFF(tty)) {
1588 		unsigned char stop_char = STOP_CHAR(tty);
1589 		status = mos7840_write(tty, port, &stop_char, 1);
1590 		if (status <= 0)
1591 			return;
1592 	}
1593 	/* if we are implementing RTS/CTS, toggle that line */
1594 	if (tty->termios.c_cflag & CRTSCTS) {
1595 		mos7840_port->shadowMCR &= ~MCR_RTS;
1596 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1597 					 mos7840_port->shadowMCR);
1598 		if (status < 0)
1599 			return;
1600 	}
1601 }
1602 
1603 /*****************************************************************************
1604  * mos7840_unthrottle
1605  *	this function is called by the tty driver when it wants to resume
1606  *	the data being read from the port (called after mos7840_throttle is
1607  *	called)
1608  *****************************************************************************/
1609 static void mos7840_unthrottle(struct tty_struct *tty)
1610 {
1611 	struct usb_serial_port *port = tty->driver_data;
1612 	int status;
1613 	struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1614 
1615 	if (mos7840_port_paranoia_check(port, __func__))
1616 		return;
1617 
1618 	if (mos7840_port == NULL)
1619 		return;
1620 
1621 	if (!mos7840_port->open) {
1622 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1623 		return;
1624 	}
1625 
1626 	/* if we are implementing XON/XOFF, send the start character */
1627 	if (I_IXOFF(tty)) {
1628 		unsigned char start_char = START_CHAR(tty);
1629 		status = mos7840_write(tty, port, &start_char, 1);
1630 		if (status <= 0)
1631 			return;
1632 	}
1633 
1634 	/* if we are implementing RTS/CTS, toggle that line */
1635 	if (tty->termios.c_cflag & CRTSCTS) {
1636 		mos7840_port->shadowMCR |= MCR_RTS;
1637 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1638 					 mos7840_port->shadowMCR);
1639 		if (status < 0)
1640 			return;
1641 	}
1642 }
1643 
1644 static int mos7840_tiocmget(struct tty_struct *tty)
1645 {
1646 	struct usb_serial_port *port = tty->driver_data;
1647 	struct moschip_port *mos7840_port;
1648 	unsigned int result;
1649 	__u16 msr;
1650 	__u16 mcr;
1651 	int status;
1652 	mos7840_port = mos7840_get_port_private(port);
1653 
1654 	if (mos7840_port == NULL)
1655 		return -ENODEV;
1656 
1657 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1658 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1659 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1660 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1661 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1662 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1663 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1664 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1665 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1666 
1667 	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1668 
1669 	return result;
1670 }
1671 
1672 static int mos7840_tiocmset(struct tty_struct *tty,
1673 			    unsigned int set, unsigned int clear)
1674 {
1675 	struct usb_serial_port *port = tty->driver_data;
1676 	struct moschip_port *mos7840_port;
1677 	unsigned int mcr;
1678 	int status;
1679 
1680 	mos7840_port = mos7840_get_port_private(port);
1681 
1682 	if (mos7840_port == NULL)
1683 		return -ENODEV;
1684 
1685 	/* FIXME: What locks the port registers ? */
1686 	mcr = mos7840_port->shadowMCR;
1687 	if (clear & TIOCM_RTS)
1688 		mcr &= ~MCR_RTS;
1689 	if (clear & TIOCM_DTR)
1690 		mcr &= ~MCR_DTR;
1691 	if (clear & TIOCM_LOOP)
1692 		mcr &= ~MCR_LOOPBACK;
1693 
1694 	if (set & TIOCM_RTS)
1695 		mcr |= MCR_RTS;
1696 	if (set & TIOCM_DTR)
1697 		mcr |= MCR_DTR;
1698 	if (set & TIOCM_LOOP)
1699 		mcr |= MCR_LOOPBACK;
1700 
1701 	mos7840_port->shadowMCR = mcr;
1702 
1703 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1704 	if (status < 0) {
1705 		dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1706 		return status;
1707 	}
1708 
1709 	return 0;
1710 }
1711 
1712 /*****************************************************************************
1713  * mos7840_calc_baud_rate_divisor
1714  *	this function calculates the proper baud rate divisor for the specified
1715  *	baud rate.
1716  *****************************************************************************/
1717 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1718 					  int baudRate, int *divisor,
1719 					  __u16 *clk_sel_val)
1720 {
1721 	dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1722 
1723 	if (baudRate <= 115200) {
1724 		*divisor = 115200 / baudRate;
1725 		*clk_sel_val = 0x0;
1726 	}
1727 	if ((baudRate > 115200) && (baudRate <= 230400)) {
1728 		*divisor = 230400 / baudRate;
1729 		*clk_sel_val = 0x10;
1730 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
1731 		*divisor = 403200 / baudRate;
1732 		*clk_sel_val = 0x20;
1733 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
1734 		*divisor = 460800 / baudRate;
1735 		*clk_sel_val = 0x30;
1736 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
1737 		*divisor = 806400 / baudRate;
1738 		*clk_sel_val = 0x40;
1739 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
1740 		*divisor = 921600 / baudRate;
1741 		*clk_sel_val = 0x50;
1742 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1743 		*divisor = 1572864 / baudRate;
1744 		*clk_sel_val = 0x60;
1745 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1746 		*divisor = 3145728 / baudRate;
1747 		*clk_sel_val = 0x70;
1748 	}
1749 	return 0;
1750 
1751 #ifdef NOTMCS7840
1752 
1753 	for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1754 		if (mos7840_divisor_table[i].BaudRate == baudrate) {
1755 			*divisor = mos7840_divisor_table[i].Divisor;
1756 			return 0;
1757 		}
1758 	}
1759 
1760 	/* After trying for all the standard baud rates    *
1761 	 * Try calculating the divisor for this baud rate  */
1762 
1763 	if (baudrate > 75 && baudrate < 230400) {
1764 		/* get the divisor */
1765 		custom = (__u16) (230400L / baudrate);
1766 
1767 		/* Check for round off */
1768 		round1 = (__u16) (2304000L / baudrate);
1769 		round = (__u16) (round1 - (custom * 10));
1770 		if (round > 4)
1771 			custom++;
1772 		*divisor = custom;
1773 
1774 		dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
1775 		return 0;
1776 	}
1777 
1778 	dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
1779 	return -1;
1780 #endif
1781 }
1782 
1783 /*****************************************************************************
1784  * mos7840_send_cmd_write_baud_rate
1785  *	this function sends the proper command to change the baud rate of the
1786  *	specified port.
1787  *****************************************************************************/
1788 
1789 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1790 					    int baudRate)
1791 {
1792 	int divisor = 0;
1793 	int status;
1794 	__u16 Data;
1795 	unsigned char number;
1796 	__u16 clk_sel_val;
1797 	struct usb_serial_port *port;
1798 
1799 	if (mos7840_port == NULL)
1800 		return -1;
1801 
1802 	port = mos7840_port->port;
1803 	if (mos7840_port_paranoia_check(port, __func__))
1804 		return -1;
1805 
1806 	if (mos7840_serial_paranoia_check(port->serial, __func__))
1807 		return -1;
1808 
1809 	number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1810 
1811 	dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1812 		mos7840_port->port->number, baudRate);
1813 	/* reset clk_uart_sel in spregOffset */
1814 	if (baudRate > 115200) {
1815 #ifdef HW_flow_control
1816 		/* NOTE: need to see the pther register to modify */
1817 		/* setting h/w flow control bit to 1 */
1818 		Data = 0x2b;
1819 		mos7840_port->shadowMCR = Data;
1820 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1821 									Data);
1822 		if (status < 0) {
1823 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1824 			return -1;
1825 		}
1826 #endif
1827 
1828 	} else {
1829 #ifdef HW_flow_control
1830 		/* setting h/w flow control bit to 0 */
1831 		Data = 0xb;
1832 		mos7840_port->shadowMCR = Data;
1833 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1834 									Data);
1835 		if (status < 0) {
1836 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1837 			return -1;
1838 		}
1839 #endif
1840 
1841 	}
1842 
1843 	if (1) {		/* baudRate <= 115200) */
1844 		clk_sel_val = 0x0;
1845 		Data = 0x0;
1846 		status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1847 						   &clk_sel_val);
1848 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1849 								 &Data);
1850 		if (status < 0) {
1851 			dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1852 			return -1;
1853 		}
1854 		Data = (Data & 0x8f) | clk_sel_val;
1855 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1856 								Data);
1857 		if (status < 0) {
1858 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1859 			return -1;
1860 		}
1861 		/* Calculate the Divisor */
1862 
1863 		if (status) {
1864 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1865 			return status;
1866 		}
1867 		/* Enable access to divisor latch */
1868 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1869 		mos7840_port->shadowLCR = Data;
1870 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1871 
1872 		/* Write the divisor */
1873 		Data = (unsigned char)(divisor & 0xff);
1874 		dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1875 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1876 
1877 		Data = (unsigned char)((divisor & 0xff00) >> 8);
1878 		dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1879 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1880 
1881 		/* Disable access to divisor latch */
1882 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1883 		mos7840_port->shadowLCR = Data;
1884 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1885 
1886 	}
1887 	return status;
1888 }
1889 
1890 /*****************************************************************************
1891  * mos7840_change_port_settings
1892  *	This routine is called to set the UART on the device to match
1893  *      the specified new settings.
1894  *****************************************************************************/
1895 
1896 static void mos7840_change_port_settings(struct tty_struct *tty,
1897 	struct moschip_port *mos7840_port, struct ktermios *old_termios)
1898 {
1899 	int baud;
1900 	unsigned cflag;
1901 	unsigned iflag;
1902 	__u8 lData;
1903 	__u8 lParity;
1904 	__u8 lStop;
1905 	int status;
1906 	__u16 Data;
1907 	struct usb_serial_port *port;
1908 	struct usb_serial *serial;
1909 
1910 	if (mos7840_port == NULL)
1911 		return;
1912 
1913 	port = mos7840_port->port;
1914 
1915 	if (mos7840_port_paranoia_check(port, __func__))
1916 		return;
1917 
1918 	if (mos7840_serial_paranoia_check(port->serial, __func__))
1919 		return;
1920 
1921 	serial = port->serial;
1922 
1923 	if (!mos7840_port->open) {
1924 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1925 		return;
1926 	}
1927 
1928 	lData = LCR_BITS_8;
1929 	lStop = LCR_STOP_1;
1930 	lParity = LCR_PAR_NONE;
1931 
1932 	cflag = tty->termios.c_cflag;
1933 	iflag = tty->termios.c_iflag;
1934 
1935 	/* Change the number of bits */
1936 	if (cflag & CSIZE) {
1937 		switch (cflag & CSIZE) {
1938 		case CS5:
1939 			lData = LCR_BITS_5;
1940 			break;
1941 
1942 		case CS6:
1943 			lData = LCR_BITS_6;
1944 			break;
1945 
1946 		case CS7:
1947 			lData = LCR_BITS_7;
1948 			break;
1949 		default:
1950 		case CS8:
1951 			lData = LCR_BITS_8;
1952 			break;
1953 		}
1954 	}
1955 	/* Change the Parity bit */
1956 	if (cflag & PARENB) {
1957 		if (cflag & PARODD) {
1958 			lParity = LCR_PAR_ODD;
1959 			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1960 		} else {
1961 			lParity = LCR_PAR_EVEN;
1962 			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1963 		}
1964 
1965 	} else {
1966 		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1967 	}
1968 
1969 	if (cflag & CMSPAR)
1970 		lParity = lParity | 0x20;
1971 
1972 	/* Change the Stop bit */
1973 	if (cflag & CSTOPB) {
1974 		lStop = LCR_STOP_2;
1975 		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1976 	} else {
1977 		lStop = LCR_STOP_1;
1978 		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1979 	}
1980 
1981 	/* Update the LCR with the correct value */
1982 	mos7840_port->shadowLCR &=
1983 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1984 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
1985 
1986 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1987 		mos7840_port->shadowLCR);
1988 	/* Disable Interrupts */
1989 	Data = 0x00;
1990 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1991 
1992 	Data = 0x00;
1993 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1994 
1995 	Data = 0xcf;
1996 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1997 
1998 	/* Send the updated LCR value to the mos7840 */
1999 	Data = mos7840_port->shadowLCR;
2000 
2001 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2002 
2003 	Data = 0x00b;
2004 	mos7840_port->shadowMCR = Data;
2005 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2006 	Data = 0x00b;
2007 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2008 
2009 	/* set up the MCR register and send it to the mos7840 */
2010 
2011 	mos7840_port->shadowMCR = MCR_MASTER_IE;
2012 	if (cflag & CBAUD)
2013 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2014 
2015 	if (cflag & CRTSCTS)
2016 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
2017 	else
2018 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2019 
2020 	Data = mos7840_port->shadowMCR;
2021 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2022 
2023 	/* Determine divisor based on baud rate */
2024 	baud = tty_get_baud_rate(tty);
2025 
2026 	if (!baud) {
2027 		/* pick a default, any default... */
2028 		dev_dbg(&port->dev, "%s", "Picked default baud...\n");
2029 		baud = 9600;
2030 	}
2031 
2032 	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
2033 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2034 
2035 	/* Enable Interrupts */
2036 	Data = 0x0c;
2037 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2038 
2039 	if (mos7840_port->read_urb_busy == false) {
2040 		mos7840_port->read_urb_busy = true;
2041 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2042 		if (status) {
2043 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2044 			    status);
2045 			mos7840_port->read_urb_busy = false;
2046 		}
2047 	}
2048 	wake_up(&mos7840_port->delta_msr_wait);
2049 	mos7840_port->delta_msr_cond = 1;
2050 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
2051 		mos7840_port->shadowLCR);
2052 }
2053 
2054 /*****************************************************************************
2055  * mos7840_set_termios
2056  *	this function is called by the tty driver when it wants to change
2057  *	the termios structure
2058  *****************************************************************************/
2059 
2060 static void mos7840_set_termios(struct tty_struct *tty,
2061 				struct usb_serial_port *port,
2062 				struct ktermios *old_termios)
2063 {
2064 	int status;
2065 	unsigned int cflag;
2066 	struct usb_serial *serial;
2067 	struct moschip_port *mos7840_port;
2068 
2069 	if (mos7840_port_paranoia_check(port, __func__))
2070 		return;
2071 
2072 	serial = port->serial;
2073 
2074 	if (mos7840_serial_paranoia_check(serial, __func__))
2075 		return;
2076 
2077 	mos7840_port = mos7840_get_port_private(port);
2078 
2079 	if (mos7840_port == NULL)
2080 		return;
2081 
2082 	if (!mos7840_port->open) {
2083 		dev_dbg(&port->dev, "%s - port not opened\n", __func__);
2084 		return;
2085 	}
2086 
2087 	dev_dbg(&port->dev, "%s", "setting termios - \n");
2088 
2089 	cflag = tty->termios.c_cflag;
2090 
2091 	dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2092 		tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
2093 	dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2094 		old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2095 	dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
2096 
2097 	/* change the port settings to the new ones specified */
2098 
2099 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
2100 
2101 	if (!mos7840_port->read_urb) {
2102 		dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
2103 		return;
2104 	}
2105 
2106 	if (mos7840_port->read_urb_busy == false) {
2107 		mos7840_port->read_urb_busy = true;
2108 		status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2109 		if (status) {
2110 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2111 			    status);
2112 			mos7840_port->read_urb_busy = false;
2113 		}
2114 	}
2115 }
2116 
2117 /*****************************************************************************
2118  * mos7840_get_lsr_info - get line status register info
2119  *
2120  * Purpose: Let user call ioctl() to get info when the UART physically
2121  * 	    is emptied.  On bus types like RS485, the transmitter must
2122  * 	    release the bus after transmitting. This must be done when
2123  * 	    the transmit shift register is empty, not be done when the
2124  * 	    transmit holding register is empty.  This functionality
2125  * 	    allows an RS485 driver to be written in user space.
2126  *****************************************************************************/
2127 
2128 static int mos7840_get_lsr_info(struct tty_struct *tty,
2129 				unsigned int __user *value)
2130 {
2131 	int count;
2132 	unsigned int result = 0;
2133 
2134 	count = mos7840_chars_in_buffer(tty);
2135 	if (count == 0)
2136 		result = TIOCSER_TEMT;
2137 
2138 	if (copy_to_user(value, &result, sizeof(int)))
2139 		return -EFAULT;
2140 	return 0;
2141 }
2142 
2143 /*****************************************************************************
2144  * mos7840_get_serial_info
2145  *      function to get information about serial port
2146  *****************************************************************************/
2147 
2148 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2149 				   struct serial_struct __user *retinfo)
2150 {
2151 	struct serial_struct tmp;
2152 
2153 	if (mos7840_port == NULL)
2154 		return -1;
2155 
2156 	if (!retinfo)
2157 		return -EFAULT;
2158 
2159 	memset(&tmp, 0, sizeof(tmp));
2160 
2161 	tmp.type = PORT_16550A;
2162 	tmp.line = mos7840_port->port->serial->minor;
2163 	tmp.port = mos7840_port->port->number;
2164 	tmp.irq = 0;
2165 	tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2166 	tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2167 	tmp.baud_base = 9600;
2168 	tmp.close_delay = 5 * HZ;
2169 	tmp.closing_wait = 30 * HZ;
2170 
2171 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2172 		return -EFAULT;
2173 	return 0;
2174 }
2175 
2176 static int mos7840_get_icount(struct tty_struct *tty,
2177 			struct serial_icounter_struct *icount)
2178 {
2179 	struct usb_serial_port *port = tty->driver_data;
2180 	struct moschip_port *mos7840_port;
2181 	struct async_icount cnow;
2182 
2183 	mos7840_port = mos7840_get_port_private(port);
2184 	cnow = mos7840_port->icount;
2185 
2186 	smp_rmb();
2187 	icount->cts = cnow.cts;
2188 	icount->dsr = cnow.dsr;
2189 	icount->rng = cnow.rng;
2190 	icount->dcd = cnow.dcd;
2191 	icount->rx = cnow.rx;
2192 	icount->tx = cnow.tx;
2193 	icount->frame = cnow.frame;
2194 	icount->overrun = cnow.overrun;
2195 	icount->parity = cnow.parity;
2196 	icount->brk = cnow.brk;
2197 	icount->buf_overrun = cnow.buf_overrun;
2198 
2199 	dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
2200 		icount->rx, icount->tx);
2201 	return 0;
2202 }
2203 
2204 /*****************************************************************************
2205  * SerialIoctl
2206  *	this function handles any ioctl calls to the driver
2207  *****************************************************************************/
2208 
2209 static int mos7840_ioctl(struct tty_struct *tty,
2210 			 unsigned int cmd, unsigned long arg)
2211 {
2212 	struct usb_serial_port *port = tty->driver_data;
2213 	void __user *argp = (void __user *)arg;
2214 	struct moschip_port *mos7840_port;
2215 
2216 	struct async_icount cnow;
2217 	struct async_icount cprev;
2218 
2219 	if (mos7840_port_paranoia_check(port, __func__))
2220 		return -1;
2221 
2222 	mos7840_port = mos7840_get_port_private(port);
2223 
2224 	if (mos7840_port == NULL)
2225 		return -1;
2226 
2227 	dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
2228 
2229 	switch (cmd) {
2230 		/* return number of bytes available */
2231 
2232 	case TIOCSERGETLSR:
2233 		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
2234 		return mos7840_get_lsr_info(tty, argp);
2235 
2236 	case TIOCGSERIAL:
2237 		dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
2238 		return mos7840_get_serial_info(mos7840_port, argp);
2239 
2240 	case TIOCSSERIAL:
2241 		dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
2242 		break;
2243 
2244 	case TIOCMIWAIT:
2245 		dev_dbg(&port->dev, "%s  TIOCMIWAIT\n", __func__);
2246 		cprev = mos7840_port->icount;
2247 		while (1) {
2248 			/* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2249 			mos7840_port->delta_msr_cond = 0;
2250 			wait_event_interruptible(mos7840_port->delta_msr_wait,
2251 						 (mos7840_port->
2252 						  delta_msr_cond == 1));
2253 
2254 			/* see if a signal did it */
2255 			if (signal_pending(current))
2256 				return -ERESTARTSYS;
2257 			cnow = mos7840_port->icount;
2258 			smp_rmb();
2259 			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2260 			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2261 				return -EIO;	/* no change => error */
2262 			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2263 			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2264 			    ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2265 			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2266 				return 0;
2267 			}
2268 			cprev = cnow;
2269 		}
2270 		/* NOTREACHED */
2271 		break;
2272 
2273 	default:
2274 		break;
2275 	}
2276 	return -ENOIOCTLCMD;
2277 }
2278 
2279 static int mos7810_check(struct usb_serial *serial)
2280 {
2281 	int i, pass_count = 0;
2282 	__u16 data = 0, mcr_data = 0;
2283 	__u16 test_pattern = 0x55AA;
2284 
2285 	/* Store MCR setting */
2286 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2287 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2288 		&mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2289 
2290 	for (i = 0; i < 16; i++) {
2291 		/* Send the 1-bit test pattern out to MCS7810 test pin */
2292 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2293 			MCS_WRREQ, MCS_WR_RTYPE,
2294 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2295 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2296 
2297 		/* Read the test pattern back */
2298 		usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2299 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2300 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2301 
2302 		/* If this is a MCS7810 device, both test patterns must match */
2303 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2304 			break;
2305 
2306 		pass_count++;
2307 	}
2308 
2309 	/* Restore MCR setting */
2310 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2311 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2312 		0, MOS_WDR_TIMEOUT);
2313 
2314 	if (pass_count == 16)
2315 		return 1;
2316 
2317 	return 0;
2318 }
2319 
2320 static int mos7840_calc_num_ports(struct usb_serial *serial)
2321 {
2322 	__u16 data = 0x00;
2323 	int mos7840_num_ports;
2324 
2325 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2326 		MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2327 		VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2328 
2329 	if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2330 		serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2331 		device_type = serial->dev->descriptor.idProduct;
2332 	} else {
2333 		/* For a MCS7840 device GPIO0 must be set to 1 */
2334 		if ((data & 0x01) == 1)
2335 			device_type = MOSCHIP_DEVICE_ID_7840;
2336 		else if (mos7810_check(serial))
2337 			device_type = MOSCHIP_DEVICE_ID_7810;
2338 		else
2339 			device_type = MOSCHIP_DEVICE_ID_7820;
2340 	}
2341 
2342 	mos7840_num_ports = (device_type >> 4) & 0x000F;
2343 	serial->num_bulk_in = mos7840_num_ports;
2344 	serial->num_bulk_out = mos7840_num_ports;
2345 	serial->num_ports = mos7840_num_ports;
2346 
2347 	return mos7840_num_ports;
2348 }
2349 
2350 /****************************************************************************
2351  * mos7840_startup
2352  ****************************************************************************/
2353 
2354 static int mos7840_startup(struct usb_serial *serial)
2355 {
2356 	struct moschip_port *mos7840_port;
2357 	struct usb_device *dev;
2358 	int i, status;
2359 	__u16 Data;
2360 
2361 	dev = serial->dev;
2362 
2363 	/* we set up the pointers to the endpoints in the mos7840_open *
2364 	 * function, as the structures aren't created yet.             */
2365 
2366 	/* set up port private structures */
2367 	for (i = 0; i < serial->num_ports; ++i) {
2368 		dev_dbg(&dev->dev, "mos7840_startup: configuring port %d............\n", i);
2369 		mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2370 		if (mos7840_port == NULL) {
2371 			dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2372 			status = -ENOMEM;
2373 			i--; /* don't follow NULL pointer cleaning up */
2374 			goto error;
2375 		}
2376 
2377 		/* Initialize all port interrupt end point to port 0 int
2378 		 * endpoint. Our device has only one interrupt end point
2379 		 * common to all port */
2380 
2381 		mos7840_port->port = serial->port[i];
2382 		mos7840_set_port_private(serial->port[i], mos7840_port);
2383 		spin_lock_init(&mos7840_port->pool_lock);
2384 
2385 		/* minor is not initialised until later by
2386 		 * usb-serial.c:get_free_serial() and cannot therefore be used
2387 		 * to index device instances */
2388 		mos7840_port->port_num = i + 1;
2389 		dev_dbg(&dev->dev, "serial->port[i]->number = %d\n", serial->port[i]->number);
2390 		dev_dbg(&dev->dev, "serial->port[i]->serial->minor = %d\n", serial->port[i]->serial->minor);
2391 		dev_dbg(&dev->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2392 		dev_dbg(&dev->dev, "serial->minor = %d\n", serial->minor);
2393 
2394 		if (mos7840_port->port_num == 1) {
2395 			mos7840_port->SpRegOffset = 0x0;
2396 			mos7840_port->ControlRegOffset = 0x1;
2397 			mos7840_port->DcrRegOffset = 0x4;
2398 		} else if ((mos7840_port->port_num == 2)
2399 			   && (serial->num_ports == 4)) {
2400 			mos7840_port->SpRegOffset = 0x8;
2401 			mos7840_port->ControlRegOffset = 0x9;
2402 			mos7840_port->DcrRegOffset = 0x16;
2403 		} else if ((mos7840_port->port_num == 2)
2404 			   && (serial->num_ports == 2)) {
2405 			mos7840_port->SpRegOffset = 0xa;
2406 			mos7840_port->ControlRegOffset = 0xb;
2407 			mos7840_port->DcrRegOffset = 0x19;
2408 		} else if ((mos7840_port->port_num == 3)
2409 			   && (serial->num_ports == 4)) {
2410 			mos7840_port->SpRegOffset = 0xa;
2411 			mos7840_port->ControlRegOffset = 0xb;
2412 			mos7840_port->DcrRegOffset = 0x19;
2413 		} else if ((mos7840_port->port_num == 4)
2414 			   && (serial->num_ports == 4)) {
2415 			mos7840_port->SpRegOffset = 0xc;
2416 			mos7840_port->ControlRegOffset = 0xd;
2417 			mos7840_port->DcrRegOffset = 0x1c;
2418 		}
2419 		mos7840_dump_serial_port(serial->port[i], mos7840_port);
2420 		mos7840_set_port_private(serial->port[i], mos7840_port);
2421 
2422 		/* enable rx_disable bit in control register */
2423 		status = mos7840_get_reg_sync(serial->port[i],
2424 				 mos7840_port->ControlRegOffset, &Data);
2425 		if (status < 0) {
2426 			dev_dbg(&dev->dev, "Reading ControlReg failed status-0x%x\n", status);
2427 			break;
2428 		} else
2429 			dev_dbg(&dev->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2430 		Data |= 0x08;	/* setting driver done bit */
2431 		Data |= 0x04;	/* sp1_bit to have cts change reflect in
2432 				   modem status reg */
2433 
2434 		/* Data |= 0x20; //rx_disable bit */
2435 		status = mos7840_set_reg_sync(serial->port[i],
2436 					 mos7840_port->ControlRegOffset, Data);
2437 		if (status < 0) {
2438 			dev_dbg(&dev->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2439 			break;
2440 		} else
2441 			dev_dbg(&dev->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2442 
2443 		/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2444 		   and 0x24 in DCR3 */
2445 		Data = 0x01;
2446 		status = mos7840_set_reg_sync(serial->port[i],
2447 			 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2448 		if (status < 0) {
2449 			dev_dbg(&dev->dev, "Writing DCR0 failed status-0x%x\n", status);
2450 			break;
2451 		} else
2452 			dev_dbg(&dev->dev, "DCR0 Writing success status%d\n", status);
2453 
2454 		Data = 0x05;
2455 		status = mos7840_set_reg_sync(serial->port[i],
2456 			 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2457 		if (status < 0) {
2458 			dev_dbg(&dev->dev, "Writing DCR1 failed status-0x%x\n", status);
2459 			break;
2460 		} else
2461 			dev_dbg(&dev->dev, "DCR1 Writing success status%d\n", status);
2462 
2463 		Data = 0x24;
2464 		status = mos7840_set_reg_sync(serial->port[i],
2465 			 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2466 		if (status < 0) {
2467 			dev_dbg(&dev->dev, "Writing DCR2 failed status-0x%x\n", status);
2468 			break;
2469 		} else
2470 			dev_dbg(&dev->dev, "DCR2 Writing success status%d\n", status);
2471 
2472 		/* write values in clkstart0x0 and clkmulti 0x20 */
2473 		Data = 0x0;
2474 		status = mos7840_set_reg_sync(serial->port[i],
2475 					 CLK_START_VALUE_REGISTER, Data);
2476 		if (status < 0) {
2477 			dev_dbg(&dev->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2478 			break;
2479 		} else
2480 			dev_dbg(&dev->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2481 
2482 		Data = 0x20;
2483 		status = mos7840_set_reg_sync(serial->port[i],
2484 					CLK_MULTI_REGISTER, Data);
2485 		if (status < 0) {
2486 			dev_dbg(&dev->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2487 			goto error;
2488 		} else
2489 			dev_dbg(&dev->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2490 
2491 		/* write value 0x0 to scratchpad register */
2492 		Data = 0x00;
2493 		status = mos7840_set_uart_reg(serial->port[i],
2494 						SCRATCH_PAD_REGISTER, Data);
2495 		if (status < 0) {
2496 			dev_dbg(&dev->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2497 			break;
2498 		} else
2499 			dev_dbg(&dev->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2500 
2501 		/* Zero Length flag register */
2502 		if ((mos7840_port->port_num != 1)
2503 		    && (serial->num_ports == 2)) {
2504 
2505 			Data = 0xff;
2506 			status = mos7840_set_reg_sync(serial->port[i],
2507 				      (__u16) (ZLP_REG1 +
2508 				      ((__u16)mos7840_port->port_num)), Data);
2509 			dev_dbg(&dev->dev, "ZLIP offset %x\n",
2510 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2511 			if (status < 0) {
2512 				dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 2, status);
2513 				break;
2514 			} else
2515 				dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 2, status);
2516 		} else {
2517 			Data = 0xff;
2518 			status = mos7840_set_reg_sync(serial->port[i],
2519 			      (__u16) (ZLP_REG1 +
2520 			      ((__u16)mos7840_port->port_num) - 0x1), Data);
2521 			dev_dbg(&dev->dev, "ZLIP offset %x\n",
2522 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2523 			if (status < 0) {
2524 				dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 1, status);
2525 				break;
2526 			} else
2527 				dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 1, status);
2528 
2529 		}
2530 		mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2531 		mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2532 		mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2533 								GFP_KERNEL);
2534 		if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2535 							!mos7840_port->dr) {
2536 			status = -ENOMEM;
2537 			goto error;
2538 		}
2539 
2540 		mos7840_port->has_led = false;
2541 
2542 		/* Initialize LED timers */
2543 		if (device_type == MOSCHIP_DEVICE_ID_7810) {
2544 			mos7840_port->has_led = true;
2545 
2546 			init_timer(&mos7840_port->led_timer1);
2547 			mos7840_port->led_timer1.function = mos7840_led_off;
2548 			mos7840_port->led_timer1.expires =
2549 					jiffies + msecs_to_jiffies(LED_ON_MS);
2550 			mos7840_port->led_timer1.data =
2551 						(unsigned long)mos7840_port;
2552 
2553 			init_timer(&mos7840_port->led_timer2);
2554 			mos7840_port->led_timer2.function =
2555 						mos7840_led_flag_off;
2556 			mos7840_port->led_timer2.expires =
2557 					jiffies + msecs_to_jiffies(LED_OFF_MS);
2558 			mos7840_port->led_timer2.data =
2559 						(unsigned long)mos7840_port;
2560 
2561 			mos7840_port->led_flag = false;
2562 
2563 			/* Turn off LED */
2564 			mos7840_set_led_sync(serial->port[i],
2565 						MODEM_CONTROL_REGISTER, 0x0300);
2566 		}
2567 	}
2568 
2569 	/* Zero Length flag enable */
2570 	Data = 0x0f;
2571 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2572 	if (status < 0) {
2573 		dev_dbg(&dev->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2574 		goto error;
2575 	} else
2576 		dev_dbg(&dev->dev, "ZLP_REG5 Writing success status%d\n", status);
2577 
2578 	/* setting configuration feature to one */
2579 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2580 			(__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT);
2581 	return 0;
2582 error:
2583 	for (/* nothing */; i >= 0; i--) {
2584 		mos7840_port = mos7840_get_port_private(serial->port[i]);
2585 
2586 		kfree(mos7840_port->dr);
2587 		kfree(mos7840_port->ctrl_buf);
2588 		usb_free_urb(mos7840_port->control_urb);
2589 		kfree(mos7840_port);
2590 		serial->port[i] = NULL;
2591 	}
2592 	return status;
2593 }
2594 
2595 /****************************************************************************
2596  * mos7840_disconnect
2597  *	This function is called whenever the device is removed from the usb bus.
2598  ****************************************************************************/
2599 
2600 static void mos7840_disconnect(struct usb_serial *serial)
2601 {
2602 	int i;
2603 	unsigned long flags;
2604 	struct moschip_port *mos7840_port;
2605 
2606 	/* check for the ports to be closed,close the ports and disconnect */
2607 
2608 	/* free private structure allocated for serial port  *
2609 	 * stop reads and writes on all ports                */
2610 
2611 	for (i = 0; i < serial->num_ports; ++i) {
2612 		mos7840_port = mos7840_get_port_private(serial->port[i]);
2613 		if (mos7840_port) {
2614 			spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2615 			mos7840_port->zombie = 1;
2616 			spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2617 			usb_kill_urb(mos7840_port->control_urb);
2618 		}
2619 	}
2620 }
2621 
2622 /****************************************************************************
2623  * mos7840_release
2624  *	This function is called when the usb_serial structure is freed.
2625  ****************************************************************************/
2626 
2627 static void mos7840_release(struct usb_serial *serial)
2628 {
2629 	int i;
2630 	struct moschip_port *mos7840_port;
2631 
2632 	/* check for the ports to be closed,close the ports and disconnect */
2633 
2634 	/* free private structure allocated for serial port  *
2635 	 * stop reads and writes on all ports                */
2636 
2637 	for (i = 0; i < serial->num_ports; ++i) {
2638 		mos7840_port = mos7840_get_port_private(serial->port[i]);
2639 		if (mos7840_port) {
2640 			if (mos7840_port->has_led) {
2641 				/* Turn off LED */
2642 				mos7840_set_led_sync(mos7840_port->port,
2643 						MODEM_CONTROL_REGISTER, 0x0300);
2644 
2645 				del_timer_sync(&mos7840_port->led_timer1);
2646 				del_timer_sync(&mos7840_port->led_timer2);
2647 			}
2648 			kfree(mos7840_port->ctrl_buf);
2649 			kfree(mos7840_port->dr);
2650 			kfree(mos7840_port);
2651 		}
2652 	}
2653 }
2654 
2655 static struct usb_serial_driver moschip7840_4port_device = {
2656 	.driver = {
2657 		   .owner = THIS_MODULE,
2658 		   .name = "mos7840",
2659 		   },
2660 	.description = DRIVER_DESC,
2661 	.id_table = id_table,
2662 	.num_ports = 4,
2663 	.open = mos7840_open,
2664 	.close = mos7840_close,
2665 	.write = mos7840_write,
2666 	.write_room = mos7840_write_room,
2667 	.chars_in_buffer = mos7840_chars_in_buffer,
2668 	.throttle = mos7840_throttle,
2669 	.unthrottle = mos7840_unthrottle,
2670 	.calc_num_ports = mos7840_calc_num_ports,
2671 #ifdef MCSSerialProbe
2672 	.probe = mos7840_serial_probe,
2673 #endif
2674 	.ioctl = mos7840_ioctl,
2675 	.set_termios = mos7840_set_termios,
2676 	.break_ctl = mos7840_break,
2677 	.tiocmget = mos7840_tiocmget,
2678 	.tiocmset = mos7840_tiocmset,
2679 	.get_icount = mos7840_get_icount,
2680 	.attach = mos7840_startup,
2681 	.disconnect = mos7840_disconnect,
2682 	.release = mos7840_release,
2683 	.read_bulk_callback = mos7840_bulk_in_callback,
2684 	.read_int_callback = mos7840_interrupt_callback,
2685 };
2686 
2687 static struct usb_serial_driver * const serial_drivers[] = {
2688 	&moschip7840_4port_device, NULL
2689 };
2690 
2691 module_usb_serial_driver(serial_drivers, id_table);
2692 
2693 MODULE_DESCRIPTION(DRIVER_DESC);
2694 MODULE_LICENSE("GPL");
2695