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