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