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