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