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