xref: /linux/drivers/usb/serial/mos7840.c (revision 7a19b158f3c9bfd5cfbb3ed9ac99e2ec6895dfb3)
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_flip.h>
17 #include <linux/module.h>
18 #include <linux/serial.h>
19 #include <linux/usb.h>
20 #include <linux/usb/serial.h>
21 #include <linux/uaccess.h>
22 
23 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
24 
25 /*
26  * 16C50 UART register defines
27  */
28 
29 #define LCR_BITS_5             0x00	/* 5 bits/char */
30 #define LCR_BITS_6             0x01	/* 6 bits/char */
31 #define LCR_BITS_7             0x02	/* 7 bits/char */
32 #define LCR_BITS_8             0x03	/* 8 bits/char */
33 #define LCR_BITS_MASK          0x03	/* Mask for bits/char field */
34 
35 #define LCR_STOP_1             0x00	/* 1 stop bit */
36 #define LCR_STOP_1_5           0x04	/* 1.5 stop bits (if 5   bits/char) */
37 #define LCR_STOP_2             0x04	/* 2 stop bits   (if 6-8 bits/char) */
38 #define LCR_STOP_MASK          0x04	/* Mask for stop bits field */
39 
40 #define LCR_PAR_NONE           0x00	/* No parity */
41 #define LCR_PAR_ODD            0x08	/* Odd parity */
42 #define LCR_PAR_EVEN           0x18	/* Even parity */
43 #define LCR_PAR_MARK           0x28	/* Force parity bit to 1 */
44 #define LCR_PAR_SPACE          0x38	/* Force parity bit to 0 */
45 #define LCR_PAR_MASK           0x38	/* Mask for parity field */
46 
47 #define LCR_SET_BREAK          0x40	/* Set Break condition */
48 #define LCR_DL_ENABLE          0x80	/* Enable access to divisor latch */
49 
50 #define MCR_DTR                0x01	/* Assert DTR */
51 #define MCR_RTS                0x02	/* Assert RTS */
52 #define MCR_OUT1               0x04	/* Loopback only: Sets state of RI */
53 #define MCR_MASTER_IE          0x08	/* Enable interrupt outputs */
54 #define MCR_LOOPBACK           0x10	/* Set internal (digital) loopback mode */
55 #define MCR_XON_ANY            0x20	/* Enable any char to exit XOFF mode */
56 
57 #define MOS7840_MSR_CTS        0x10	/* Current state of CTS */
58 #define MOS7840_MSR_DSR        0x20	/* Current state of DSR */
59 #define MOS7840_MSR_RI         0x40	/* Current state of RI */
60 #define MOS7840_MSR_CD         0x80	/* Current state of CD */
61 
62 /*
63  * Defines used for sending commands to port
64  */
65 
66 #define MOS_WDR_TIMEOUT		5000	/* default urb timeout */
67 
68 /* Requests */
69 #define MCS_RD_RTYPE    0xC0
70 #define MCS_WR_RTYPE    0x40
71 #define MCS_RDREQ       0x0D
72 #define MCS_WRREQ       0x0E
73 #define VENDOR_READ_LENGTH      (0x01)
74 
75 #define ZLP_REG1  0x3A		/* Zero_Flag_Reg1    58 */
76 #define ZLP_REG5  0x3E		/* Zero_Flag_Reg5    62 */
77 
78 /*
79  * Vendor id and device id defines
80  *
81  * NOTE: Do not add new defines, add entries directly to the id_table instead.
82  */
83 #define USB_VENDOR_ID_BANDB              0x0856
84 #define BANDB_DEVICE_ID_USO9ML2_2        0xAC22
85 #define BANDB_DEVICE_ID_USO9ML2_2P       0xBC00
86 #define BANDB_DEVICE_ID_USO9ML2_4        0xAC24
87 #define BANDB_DEVICE_ID_USO9ML2_4P       0xBC01
88 #define BANDB_DEVICE_ID_US9ML2_2         0xAC29
89 #define BANDB_DEVICE_ID_US9ML2_4         0xAC30
90 #define BANDB_DEVICE_ID_USPTL4_2         0xAC31
91 #define BANDB_DEVICE_ID_USPTL4_4         0xAC32
92 #define BANDB_DEVICE_ID_USOPTL4_2        0xAC42
93 #define BANDB_DEVICE_ID_USOPTL4_2P       0xBC02
94 #define BANDB_DEVICE_ID_USOPTL4_4        0xAC44
95 #define BANDB_DEVICE_ID_USOPTL4_4P       0xBC03
96 
97 /* Interrupt Routine Defines    */
98 
99 #define SERIAL_IIR_RLS      0x06
100 #define SERIAL_IIR_MS       0x00
101 
102 /*
103  *  Emulation of the bit mask on the LINE STATUS REGISTER.
104  */
105 #define SERIAL_LSR_DR       0x0001
106 #define SERIAL_LSR_OE       0x0002
107 #define SERIAL_LSR_PE       0x0004
108 #define SERIAL_LSR_FE       0x0008
109 #define SERIAL_LSR_BI       0x0010
110 
111 #define MOS_MSR_DELTA_CTS   0x10
112 #define MOS_MSR_DELTA_DSR   0x20
113 #define MOS_MSR_DELTA_RI    0x40
114 #define MOS_MSR_DELTA_CD    0x80
115 
116 /* Serial Port register Address */
117 #define INTERRUPT_ENABLE_REGISTER  ((__u16)(0x01))
118 #define FIFO_CONTROL_REGISTER      ((__u16)(0x02))
119 #define LINE_CONTROL_REGISTER      ((__u16)(0x03))
120 #define MODEM_CONTROL_REGISTER     ((__u16)(0x04))
121 #define LINE_STATUS_REGISTER       ((__u16)(0x05))
122 #define MODEM_STATUS_REGISTER      ((__u16)(0x06))
123 #define SCRATCH_PAD_REGISTER       ((__u16)(0x07))
124 #define DIVISOR_LATCH_LSB          ((__u16)(0x00))
125 #define DIVISOR_LATCH_MSB          ((__u16)(0x01))
126 
127 #define CLK_MULTI_REGISTER         ((__u16)(0x02))
128 #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
129 #define GPIO_REGISTER              ((__u16)(0x07))
130 
131 #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
132 
133 /*
134  * URB POOL related defines
135  */
136 #define NUM_URBS                        16	/* URB Count */
137 #define URB_TRANSFER_BUFFER_SIZE        32	/* URB Size  */
138 
139 /* LED on/off milliseconds*/
140 #define LED_ON_MS	500
141 #define LED_OFF_MS	500
142 
143 enum mos7840_flag {
144 	MOS7840_FLAG_LED_BUSY,
145 };
146 
147 #define MCS_PORT_MASK	GENMASK(2, 0)
148 #define MCS_PORTS(nr)	((nr) & MCS_PORT_MASK)
149 #define MCS_LED		BIT(3)
150 
151 #define MCS_DEVICE(vid, pid, flags) \
152 		USB_DEVICE((vid), (pid)), .driver_info = (flags)
153 
154 static const struct usb_device_id id_table[] = {
155 	{ MCS_DEVICE(0x0557, 0x2011, MCS_PORTS(4)) },	/* ATEN UC2324 */
156 	{ MCS_DEVICE(0x0557, 0x7820, MCS_PORTS(2)) },	/* ATEN UC2322 */
157 	{ MCS_DEVICE(0x110a, 0x2210, MCS_PORTS(2)) },	/* Moxa UPort 2210 */
158 	{ MCS_DEVICE(0x9710, 0x7810, MCS_PORTS(1) | MCS_LED) }, /* ASIX MCS7810 */
159 	{ MCS_DEVICE(0x9710, 0x7820, MCS_PORTS(2)) },	/* MosChip MCS7820 */
160 	{ MCS_DEVICE(0x9710, 0x7840, MCS_PORTS(4)) },	/* MosChip MCS7840 */
161 	{ MCS_DEVICE(0x9710, 0x7843, MCS_PORTS(3)) },	/* ASIX MCS7840 3 port */
162 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2) },
163 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P) },
164 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4) },
165 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P) },
166 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2) },
167 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4) },
168 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2) },
169 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4) },
170 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2) },
171 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P) },
172 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4) },
173 	{ USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P) },
174 	{}			/* terminating entry */
175 };
176 MODULE_DEVICE_TABLE(usb, id_table);
177 
178 /* This structure holds all of the local port information */
179 
180 struct moschip_port {
181 	int port_num;		/*Actual port number in the device(1,2,etc) */
182 	struct urb *read_urb;	/* read URB for this port */
183 	__u8 shadowLCR;		/* last LCR value received */
184 	__u8 shadowMCR;		/* last MCR value received */
185 	struct usb_serial_port *port;	/* loop back to the owner of this object */
186 
187 	/* Offsets */
188 	__u8 SpRegOffset;
189 	__u8 ControlRegOffset;
190 	__u8 DcrRegOffset;
191 
192 	spinlock_t pool_lock;
193 	struct urb *write_urb_pool[NUM_URBS];
194 	char busy[NUM_URBS];
195 	bool read_urb_busy;
196 
197 	/* For device(s) with LED indicator */
198 	bool has_led;
199 	struct timer_list led_timer1;	/* Timer for LED on */
200 	struct timer_list led_timer2;	/* Timer for LED off */
201 	struct urb *led_urb;
202 	struct usb_ctrlrequest *led_dr;
203 
204 	unsigned long flags;
205 };
206 
207 /*
208  * mos7840_set_reg_sync
209  * 	To set the Control register by calling usb_fill_control_urb function
210  *	by passing usb_sndctrlpipe function as parameter.
211  */
212 
213 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
214 				__u16 val)
215 {
216 	struct usb_device *dev = port->serial->dev;
217 	val = val & 0x00ff;
218 	dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
219 
220 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
221 			       MCS_WR_RTYPE, val, reg, NULL, 0,
222 			       MOS_WDR_TIMEOUT);
223 }
224 
225 /*
226  * mos7840_get_reg_sync
227  * 	To set the Uart register by calling usb_fill_control_urb function by
228  *	passing usb_rcvctrlpipe function as parameter.
229  */
230 
231 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
232 				__u16 *val)
233 {
234 	struct usb_device *dev = port->serial->dev;
235 	int ret = 0;
236 	u8 *buf;
237 
238 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
239 	if (!buf)
240 		return -ENOMEM;
241 
242 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
243 			      MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
244 			      MOS_WDR_TIMEOUT);
245 	if (ret < VENDOR_READ_LENGTH) {
246 		if (ret >= 0)
247 			ret = -EIO;
248 		goto out;
249 	}
250 
251 	*val = buf[0];
252 	dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
253 out:
254 	kfree(buf);
255 	return ret;
256 }
257 
258 /*
259  * mos7840_set_uart_reg
260  *	To set the Uart register by calling usb_fill_control_urb function by
261  *	passing usb_sndctrlpipe function as parameter.
262  */
263 
264 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
265 				__u16 val)
266 {
267 
268 	struct usb_device *dev = port->serial->dev;
269 	val = val & 0x00ff;
270 	/* For the UART control registers, the application number need
271 	   to be Or'ed */
272 	if (port->serial->num_ports == 2 && port->port_number != 0)
273 		val |= ((__u16)port->port_number + 2) << 8;
274 	else
275 		val |= ((__u16)port->port_number + 1) << 8;
276 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
277 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
278 			       MCS_WR_RTYPE, val, reg, NULL, 0,
279 			       MOS_WDR_TIMEOUT);
280 
281 }
282 
283 /*
284  * mos7840_get_uart_reg
285  *	To set the Control register by calling usb_fill_control_urb function
286  *	by passing usb_rcvctrlpipe function as parameter.
287  */
288 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
289 				__u16 *val)
290 {
291 	struct usb_device *dev = port->serial->dev;
292 	int ret = 0;
293 	__u16 Wval;
294 	u8 *buf;
295 
296 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
297 	if (!buf)
298 		return -ENOMEM;
299 
300 	/* Wval  is same as application number */
301 	if (port->serial->num_ports == 2 && port->port_number != 0)
302 		Wval = ((__u16)port->port_number + 2) << 8;
303 	else
304 		Wval = ((__u16)port->port_number + 1) << 8;
305 	dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
306 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
307 			      MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
308 			      MOS_WDR_TIMEOUT);
309 	if (ret < VENDOR_READ_LENGTH) {
310 		if (ret >= 0)
311 			ret = -EIO;
312 		goto out;
313 	}
314 	*val = buf[0];
315 out:
316 	kfree(buf);
317 	return ret;
318 }
319 
320 static void mos7840_dump_serial_port(struct usb_serial_port *port,
321 				     struct moschip_port *mos7840_port)
322 {
323 
324 	dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
325 	dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
326 	dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
327 
328 }
329 
330 /************************************************************************/
331 /************************************************************************/
332 /*            U S B  C A L L B A C K   F U N C T I O N S                */
333 /*            U S B  C A L L B A C K   F U N C T I O N S                */
334 /************************************************************************/
335 /************************************************************************/
336 
337 static void mos7840_set_led_callback(struct urb *urb)
338 {
339 	switch (urb->status) {
340 	case 0:
341 		/* Success */
342 		break;
343 	case -ECONNRESET:
344 	case -ENOENT:
345 	case -ESHUTDOWN:
346 		/* This urb is terminated, clean up */
347 		dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
348 			__func__, urb->status);
349 		break;
350 	default:
351 		dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
352 			__func__, urb->status);
353 	}
354 }
355 
356 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
357 				__u16 reg)
358 {
359 	struct usb_device *dev = mcs->port->serial->dev;
360 	struct usb_ctrlrequest *dr = mcs->led_dr;
361 
362 	dr->bRequestType = MCS_WR_RTYPE;
363 	dr->bRequest = MCS_WRREQ;
364 	dr->wValue = cpu_to_le16(wval);
365 	dr->wIndex = cpu_to_le16(reg);
366 	dr->wLength = cpu_to_le16(0);
367 
368 	usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
369 		(unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
370 
371 	usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
372 }
373 
374 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
375 				__u16 val)
376 {
377 	struct usb_device *dev = port->serial->dev;
378 
379 	usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
380 			val, reg, NULL, 0, MOS_WDR_TIMEOUT);
381 }
382 
383 static void mos7840_led_off(struct timer_list *t)
384 {
385 	struct moschip_port *mcs = timer_container_of(mcs, t, led_timer1);
386 
387 	/* Turn off LED */
388 	mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
389 	mod_timer(&mcs->led_timer2,
390 				jiffies + msecs_to_jiffies(LED_OFF_MS));
391 }
392 
393 static void mos7840_led_flag_off(struct timer_list *t)
394 {
395 	struct moschip_port *mcs = timer_container_of(mcs, t, led_timer2);
396 
397 	clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
398 }
399 
400 static void mos7840_led_activity(struct usb_serial_port *port)
401 {
402 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
403 
404 	if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
405 		return;
406 
407 	mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
408 	mod_timer(&mos7840_port->led_timer1,
409 				jiffies + msecs_to_jiffies(LED_ON_MS));
410 }
411 
412 /*****************************************************************************
413  * mos7840_bulk_in_callback
414  *	this is the callback function for when we have received data on the
415  *	bulk in endpoint.
416  *****************************************************************************/
417 
418 static void mos7840_bulk_in_callback(struct urb *urb)
419 {
420 	struct moschip_port *mos7840_port = urb->context;
421 	struct usb_serial_port *port = mos7840_port->port;
422 	int retval;
423 	unsigned char *data;
424 	int status = urb->status;
425 
426 	if (status) {
427 		dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
428 		mos7840_port->read_urb_busy = false;
429 		return;
430 	}
431 
432 	data = urb->transfer_buffer;
433 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
434 
435 	if (urb->actual_length) {
436 		struct tty_port *tport = &mos7840_port->port->port;
437 		tty_insert_flip_string(tport, data, urb->actual_length);
438 		tty_flip_buffer_push(tport);
439 		port->icount.rx += urb->actual_length;
440 		dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
441 	}
442 
443 	if (mos7840_port->has_led)
444 		mos7840_led_activity(port);
445 
446 	mos7840_port->read_urb_busy = true;
447 	retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
448 
449 	if (retval) {
450 		dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
451 		mos7840_port->read_urb_busy = false;
452 	}
453 }
454 
455 /*****************************************************************************
456  * mos7840_bulk_out_data_callback
457  *	this is the callback function for when we have finished sending
458  *	serial data on the bulk out endpoint.
459  *****************************************************************************/
460 
461 static void mos7840_bulk_out_data_callback(struct urb *urb)
462 {
463 	struct moschip_port *mos7840_port = urb->context;
464 	struct usb_serial_port *port = mos7840_port->port;
465 	int status = urb->status;
466 	unsigned long flags;
467 	int i;
468 
469 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
470 	for (i = 0; i < NUM_URBS; i++) {
471 		if (urb == mos7840_port->write_urb_pool[i]) {
472 			mos7840_port->busy[i] = 0;
473 			break;
474 		}
475 	}
476 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
477 
478 	if (status) {
479 		dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
480 		return;
481 	}
482 
483 	tty_port_tty_wakeup(&port->port);
484 
485 }
486 
487 /************************************************************************/
488 /*       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       */
489 /************************************************************************/
490 
491 /*****************************************************************************
492  * mos7840_open
493  *	this function is called by the tty driver when a port is opened
494  *	If successful, we return 0
495  *	Otherwise we return a negative error number.
496  *****************************************************************************/
497 
498 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
499 {
500 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
501 	struct usb_serial *serial = port->serial;
502 	int response;
503 	int j;
504 	struct urb *urb;
505 	__u16 Data;
506 	int status;
507 
508 	usb_clear_halt(serial->dev, port->write_urb->pipe);
509 	usb_clear_halt(serial->dev, port->read_urb->pipe);
510 
511 	/* Initialising the write urb pool */
512 	for (j = 0; j < NUM_URBS; ++j) {
513 		urb = usb_alloc_urb(0, GFP_KERNEL);
514 		mos7840_port->write_urb_pool[j] = urb;
515 		if (!urb)
516 			continue;
517 
518 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
519 								GFP_KERNEL);
520 		if (!urb->transfer_buffer) {
521 			usb_free_urb(urb);
522 			mos7840_port->write_urb_pool[j] = NULL;
523 			continue;
524 		}
525 	}
526 
527 /*****************************************************************************
528  * Initialize MCS7840 -- Write Init values to corresponding Registers
529  *
530  * Register Index
531  * 1 : IER
532  * 2 : FCR
533  * 3 : LCR
534  * 4 : MCR
535  *
536  * 0x08 : SP1/2 Control Reg
537  *****************************************************************************/
538 
539 	/* NEED to check the following Block */
540 
541 	Data = 0x0;
542 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
543 	if (status < 0) {
544 		dev_dbg(&port->dev, "Reading Spreg failed\n");
545 		goto err;
546 	}
547 	Data |= 0x80;
548 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
549 	if (status < 0) {
550 		dev_dbg(&port->dev, "writing Spreg failed\n");
551 		goto err;
552 	}
553 
554 	Data &= ~0x80;
555 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
556 	if (status < 0) {
557 		dev_dbg(&port->dev, "writing Spreg failed\n");
558 		goto err;
559 	}
560 	/* End of block to be checked */
561 
562 	Data = 0x0;
563 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
564 									&Data);
565 	if (status < 0) {
566 		dev_dbg(&port->dev, "Reading Controlreg failed\n");
567 		goto err;
568 	}
569 	Data |= 0x08;		/* Driver done bit */
570 	Data |= 0x20;		/* rx_disable */
571 	status = mos7840_set_reg_sync(port,
572 				mos7840_port->ControlRegOffset, Data);
573 	if (status < 0) {
574 		dev_dbg(&port->dev, "writing Controlreg failed\n");
575 		goto err;
576 	}
577 	/* do register settings here */
578 	/* Set all regs to the device default values. */
579 	/***********************************
580 	 * First Disable all interrupts.
581 	 ***********************************/
582 	Data = 0x00;
583 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
584 	if (status < 0) {
585 		dev_dbg(&port->dev, "disabling interrupts failed\n");
586 		goto err;
587 	}
588 	/* Set FIFO_CONTROL_REGISTER to the default value */
589 	Data = 0x00;
590 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
591 	if (status < 0) {
592 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
593 		goto err;
594 	}
595 
596 	Data = 0xcf;
597 	status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
598 	if (status < 0) {
599 		dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER  failed\n");
600 		goto err;
601 	}
602 
603 	Data = 0x03;
604 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
605 	mos7840_port->shadowLCR = Data;
606 
607 	Data = 0x0b;
608 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
609 	mos7840_port->shadowMCR = Data;
610 
611 	Data = 0x00;
612 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
613 	mos7840_port->shadowLCR = Data;
614 
615 	Data |= SERIAL_LCR_DLAB;	/* data latch enable in LCR 0x80 */
616 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
617 
618 	Data = 0x0c;
619 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
620 
621 	Data = 0x0;
622 	status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
623 
624 	Data = 0x00;
625 	status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
626 
627 	Data = Data & ~SERIAL_LCR_DLAB;
628 	status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
629 	mos7840_port->shadowLCR = Data;
630 
631 	/* clearing Bulkin and Bulkout Fifo */
632 	Data = 0x0;
633 	status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
634 
635 	Data = Data | 0x0c;
636 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
637 
638 	Data = Data & ~0x0c;
639 	status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
640 	/* Finally enable all interrupts */
641 	Data = 0x0c;
642 	status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
643 
644 	/* clearing rx_disable */
645 	Data = 0x0;
646 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
647 									&Data);
648 	Data = Data & ~0x20;
649 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
650 									Data);
651 
652 	/* rx_negate */
653 	Data = 0x0;
654 	status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
655 									&Data);
656 	Data = Data | 0x10;
657 	status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
658 									Data);
659 
660 	dev_dbg(&port->dev, "port number is %d\n", port->port_number);
661 	dev_dbg(&port->dev, "minor number is %d\n", port->minor);
662 	dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
663 	dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
664 	dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
665 	dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
666 	mos7840_port->read_urb = port->read_urb;
667 
668 	/* set up our bulk in urb */
669 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
670 		usb_fill_bulk_urb(mos7840_port->read_urb,
671 			serial->dev,
672 			usb_rcvbulkpipe(serial->dev,
673 				(port->bulk_in_endpointAddress) + 2),
674 			port->bulk_in_buffer,
675 			mos7840_port->read_urb->transfer_buffer_length,
676 			mos7840_bulk_in_callback, mos7840_port);
677 	} else {
678 		usb_fill_bulk_urb(mos7840_port->read_urb,
679 			serial->dev,
680 			usb_rcvbulkpipe(serial->dev,
681 				port->bulk_in_endpointAddress),
682 			port->bulk_in_buffer,
683 			mos7840_port->read_urb->transfer_buffer_length,
684 			mos7840_bulk_in_callback, mos7840_port);
685 	}
686 
687 	dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
688 	mos7840_port->read_urb_busy = true;
689 	response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
690 	if (response) {
691 		dev_err(&port->dev, "%s - Error %d submitting control urb\n",
692 			__func__, response);
693 		mos7840_port->read_urb_busy = false;
694 	}
695 
696 	/* initialize our port settings */
697 	/* Must set to enable ints! */
698 	mos7840_port->shadowMCR = MCR_MASTER_IE;
699 
700 	return 0;
701 err:
702 	for (j = 0; j < NUM_URBS; ++j) {
703 		urb = mos7840_port->write_urb_pool[j];
704 		if (!urb)
705 			continue;
706 		kfree(urb->transfer_buffer);
707 		usb_free_urb(urb);
708 	}
709 	return status;
710 }
711 
712 /*****************************************************************************
713  * mos7840_chars_in_buffer
714  *	this function is called by the tty driver when it wants to know how many
715  *	bytes of data we currently have outstanding in the port (data that has
716  *	been written, but hasn't made it out the port yet)
717  *****************************************************************************/
718 
719 static unsigned int mos7840_chars_in_buffer(struct tty_struct *tty)
720 {
721 	struct usb_serial_port *port = tty->driver_data;
722 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
723 	int i;
724 	unsigned int chars = 0;
725 	unsigned long flags;
726 
727 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
728 	for (i = 0; i < NUM_URBS; ++i) {
729 		if (mos7840_port->busy[i]) {
730 			struct urb *urb = mos7840_port->write_urb_pool[i];
731 			chars += urb->transfer_buffer_length;
732 		}
733 	}
734 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
735 	dev_dbg(&port->dev, "%s - returns %u\n", __func__, chars);
736 	return chars;
737 
738 }
739 
740 /*****************************************************************************
741  * mos7840_close
742  *	this function is called by the tty driver when a port is closed
743  *****************************************************************************/
744 
745 static void mos7840_close(struct usb_serial_port *port)
746 {
747 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
748 	int j;
749 	__u16 Data;
750 
751 	for (j = 0; j < NUM_URBS; ++j)
752 		usb_kill_urb(mos7840_port->write_urb_pool[j]);
753 
754 	/* Freeing Write URBs */
755 	for (j = 0; j < NUM_URBS; ++j) {
756 		if (mos7840_port->write_urb_pool[j]) {
757 			kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
758 			usb_free_urb(mos7840_port->write_urb_pool[j]);
759 		}
760 	}
761 
762 	usb_kill_urb(mos7840_port->read_urb);
763 	mos7840_port->read_urb_busy = false;
764 
765 	Data = 0x0;
766 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
767 
768 	Data = 0x00;
769 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
770 }
771 
772 /*****************************************************************************
773  * mos7840_break
774  *	this function sends a break to the port
775  *****************************************************************************/
776 static int mos7840_break(struct tty_struct *tty, int break_state)
777 {
778 	struct usb_serial_port *port = tty->driver_data;
779 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
780 	unsigned char data;
781 
782 	if (break_state == -1)
783 		data = mos7840_port->shadowLCR | LCR_SET_BREAK;
784 	else
785 		data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
786 
787 	/* FIXME: no locking on shadowLCR anywhere in driver */
788 	mos7840_port->shadowLCR = data;
789 	dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
790 
791 	return mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
792 				    mos7840_port->shadowLCR);
793 }
794 
795 /*****************************************************************************
796  * mos7840_write_room
797  *	this function is called by the tty driver when it wants to know how many
798  *	bytes of data we can accept for a specific port.
799  *****************************************************************************/
800 
801 static unsigned int mos7840_write_room(struct tty_struct *tty)
802 {
803 	struct usb_serial_port *port = tty->driver_data;
804 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
805 	int i;
806 	unsigned int room = 0;
807 	unsigned long flags;
808 
809 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
810 	for (i = 0; i < NUM_URBS; ++i) {
811 		if (!mos7840_port->busy[i])
812 			room += URB_TRANSFER_BUFFER_SIZE;
813 	}
814 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
815 
816 	room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
817 	dev_dbg(&mos7840_port->port->dev, "%s - returns %u\n", __func__, room);
818 	return room;
819 
820 }
821 
822 /*****************************************************************************
823  * mos7840_write
824  *	this function is called by the tty driver when data should be written to
825  *	the port.
826  *	If successful, we return the number of bytes written, otherwise we
827  *      return a negative error number.
828  *****************************************************************************/
829 
830 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
831 			 const unsigned char *data, int count)
832 {
833 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
834 	struct usb_serial *serial = port->serial;
835 	int status;
836 	int i;
837 	int bytes_sent = 0;
838 	int transfer_size;
839 	unsigned long flags;
840 	struct urb *urb;
841 	/* __u16 Data; */
842 	const unsigned char *current_position = data;
843 
844 	/* try to find a free urb in the list */
845 	urb = NULL;
846 
847 	spin_lock_irqsave(&mos7840_port->pool_lock, flags);
848 	for (i = 0; i < NUM_URBS; ++i) {
849 		if (!mos7840_port->busy[i]) {
850 			mos7840_port->busy[i] = 1;
851 			urb = mos7840_port->write_urb_pool[i];
852 			dev_dbg(&port->dev, "URB:%d\n", i);
853 			break;
854 		}
855 	}
856 	spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
857 
858 	if (urb == NULL) {
859 		dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
860 		goto exit;
861 	}
862 
863 	if (urb->transfer_buffer == NULL) {
864 		urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
865 					       GFP_ATOMIC);
866 		if (!urb->transfer_buffer) {
867 			bytes_sent = -ENOMEM;
868 			goto exit;
869 		}
870 	}
871 	transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
872 
873 	memcpy(urb->transfer_buffer, current_position, transfer_size);
874 
875 	/* fill urb with data and submit  */
876 	if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
877 		usb_fill_bulk_urb(urb,
878 			serial->dev,
879 			usb_sndbulkpipe(serial->dev,
880 				(port->bulk_out_endpointAddress) + 2),
881 			urb->transfer_buffer,
882 			transfer_size,
883 			mos7840_bulk_out_data_callback, mos7840_port);
884 	} else {
885 		usb_fill_bulk_urb(urb,
886 			serial->dev,
887 			usb_sndbulkpipe(serial->dev,
888 				port->bulk_out_endpointAddress),
889 			urb->transfer_buffer,
890 			transfer_size,
891 			mos7840_bulk_out_data_callback, mos7840_port);
892 	}
893 
894 	dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
895 
896 	if (mos7840_port->has_led)
897 		mos7840_led_activity(port);
898 
899 	/* send it down the pipe */
900 	status = usb_submit_urb(urb, GFP_ATOMIC);
901 
902 	if (status) {
903 		mos7840_port->busy[i] = 0;
904 		dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
905 			"with status = %d\n", __func__, status);
906 		bytes_sent = status;
907 		goto exit;
908 	}
909 	bytes_sent = transfer_size;
910 	port->icount.tx += transfer_size;
911 	dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
912 exit:
913 	return bytes_sent;
914 
915 }
916 
917 /*****************************************************************************
918  * mos7840_throttle
919  *	this function is called by the tty driver when it wants to stop the data
920  *	being read from the port.
921  *****************************************************************************/
922 
923 static void mos7840_throttle(struct tty_struct *tty)
924 {
925 	struct usb_serial_port *port = tty->driver_data;
926 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
927 	int status;
928 
929 	/* if we are implementing XON/XOFF, send the stop character */
930 	if (I_IXOFF(tty)) {
931 		unsigned char stop_char = STOP_CHAR(tty);
932 		status = mos7840_write(tty, port, &stop_char, 1);
933 		if (status <= 0)
934 			return;
935 	}
936 	/* if we are implementing RTS/CTS, toggle that line */
937 	if (C_CRTSCTS(tty)) {
938 		mos7840_port->shadowMCR &= ~MCR_RTS;
939 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
940 					 mos7840_port->shadowMCR);
941 		if (status < 0)
942 			return;
943 	}
944 }
945 
946 /*****************************************************************************
947  * mos7840_unthrottle
948  *	this function is called by the tty driver when it wants to resume
949  *	the data being read from the port (called after mos7840_throttle is
950  *	called)
951  *****************************************************************************/
952 static void mos7840_unthrottle(struct tty_struct *tty)
953 {
954 	struct usb_serial_port *port = tty->driver_data;
955 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
956 	int status;
957 
958 	/* if we are implementing XON/XOFF, send the start character */
959 	if (I_IXOFF(tty)) {
960 		unsigned char start_char = START_CHAR(tty);
961 		status = mos7840_write(tty, port, &start_char, 1);
962 		if (status <= 0)
963 			return;
964 	}
965 
966 	/* if we are implementing RTS/CTS, toggle that line */
967 	if (C_CRTSCTS(tty)) {
968 		mos7840_port->shadowMCR |= MCR_RTS;
969 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
970 					 mos7840_port->shadowMCR);
971 		if (status < 0)
972 			return;
973 	}
974 }
975 
976 static int mos7840_tiocmget(struct tty_struct *tty)
977 {
978 	struct usb_serial_port *port = tty->driver_data;
979 	unsigned int result;
980 	__u16 msr;
981 	__u16 mcr;
982 	int status;
983 
984 	status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
985 	if (status < 0)
986 		return -EIO;
987 	status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
988 	if (status < 0)
989 		return -EIO;
990 	result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
991 	    | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
992 	    | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
993 	    | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
994 	    | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
995 	    | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
996 	    | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
997 
998 	dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
999 
1000 	return result;
1001 }
1002 
1003 static int mos7840_tiocmset(struct tty_struct *tty,
1004 			    unsigned int set, unsigned int clear)
1005 {
1006 	struct usb_serial_port *port = tty->driver_data;
1007 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
1008 	unsigned int mcr;
1009 	int status;
1010 
1011 	/* FIXME: What locks the port registers ? */
1012 	mcr = mos7840_port->shadowMCR;
1013 	if (clear & TIOCM_RTS)
1014 		mcr &= ~MCR_RTS;
1015 	if (clear & TIOCM_DTR)
1016 		mcr &= ~MCR_DTR;
1017 	if (clear & TIOCM_LOOP)
1018 		mcr &= ~MCR_LOOPBACK;
1019 
1020 	if (set & TIOCM_RTS)
1021 		mcr |= MCR_RTS;
1022 	if (set & TIOCM_DTR)
1023 		mcr |= MCR_DTR;
1024 	if (set & TIOCM_LOOP)
1025 		mcr |= MCR_LOOPBACK;
1026 
1027 	mos7840_port->shadowMCR = mcr;
1028 
1029 	status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1030 	if (status < 0) {
1031 		dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1032 		return status;
1033 	}
1034 
1035 	return 0;
1036 }
1037 
1038 /*****************************************************************************
1039  * mos7840_calc_baud_rate_divisor
1040  *	this function calculates the proper baud rate divisor for the specified
1041  *	baud rate.
1042  *****************************************************************************/
1043 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1044 					  int baudRate, int *divisor,
1045 					  __u16 *clk_sel_val)
1046 {
1047 	dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1048 
1049 	if (baudRate <= 115200) {
1050 		*divisor = 115200 / baudRate;
1051 		*clk_sel_val = 0x0;
1052 	}
1053 	if ((baudRate > 115200) && (baudRate <= 230400)) {
1054 		*divisor = 230400 / baudRate;
1055 		*clk_sel_val = 0x10;
1056 	} else if ((baudRate > 230400) && (baudRate <= 403200)) {
1057 		*divisor = 403200 / baudRate;
1058 		*clk_sel_val = 0x20;
1059 	} else if ((baudRate > 403200) && (baudRate <= 460800)) {
1060 		*divisor = 460800 / baudRate;
1061 		*clk_sel_val = 0x30;
1062 	} else if ((baudRate > 460800) && (baudRate <= 806400)) {
1063 		*divisor = 806400 / baudRate;
1064 		*clk_sel_val = 0x40;
1065 	} else if ((baudRate > 806400) && (baudRate <= 921600)) {
1066 		*divisor = 921600 / baudRate;
1067 		*clk_sel_val = 0x50;
1068 	} else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1069 		*divisor = 1572864 / baudRate;
1070 		*clk_sel_val = 0x60;
1071 	} else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1072 		*divisor = 3145728 / baudRate;
1073 		*clk_sel_val = 0x70;
1074 	}
1075 	return 0;
1076 }
1077 
1078 /*****************************************************************************
1079  * mos7840_send_cmd_write_baud_rate
1080  *	this function sends the proper command to change the baud rate of the
1081  *	specified port.
1082  *****************************************************************************/
1083 
1084 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1085 					    int baudRate)
1086 {
1087 	struct usb_serial_port *port = mos7840_port->port;
1088 	int divisor = 0;
1089 	int status;
1090 	__u16 Data;
1091 	__u16 clk_sel_val;
1092 
1093 	dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1094 	/* reset clk_uart_sel in spregOffset */
1095 	if (baudRate > 115200) {
1096 #ifdef HW_flow_control
1097 		/* NOTE: need to see the pther register to modify */
1098 		/* setting h/w flow control bit to 1 */
1099 		Data = 0x2b;
1100 		mos7840_port->shadowMCR = Data;
1101 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1102 									Data);
1103 		if (status < 0) {
1104 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1105 			return -1;
1106 		}
1107 #endif
1108 
1109 	} else {
1110 #ifdef HW_flow_control
1111 		/* setting h/w flow control bit to 0 */
1112 		Data = 0xb;
1113 		mos7840_port->shadowMCR = Data;
1114 		status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1115 									Data);
1116 		if (status < 0) {
1117 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1118 			return -1;
1119 		}
1120 #endif
1121 
1122 	}
1123 
1124 	if (1) {		/* baudRate <= 115200) */
1125 		clk_sel_val = 0x0;
1126 		Data = 0x0;
1127 		status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1128 						   &clk_sel_val);
1129 		status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1130 								 &Data);
1131 		if (status < 0) {
1132 			dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1133 			return -1;
1134 		}
1135 		Data = (Data & 0x8f) | clk_sel_val;
1136 		status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1137 								Data);
1138 		if (status < 0) {
1139 			dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1140 			return -1;
1141 		}
1142 		/* Calculate the Divisor */
1143 
1144 		if (status) {
1145 			dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1146 			return status;
1147 		}
1148 		/* Enable access to divisor latch */
1149 		Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1150 		mos7840_port->shadowLCR = Data;
1151 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1152 
1153 		/* Write the divisor */
1154 		Data = (unsigned char)(divisor & 0xff);
1155 		dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1156 		mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1157 
1158 		Data = (unsigned char)((divisor & 0xff00) >> 8);
1159 		dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1160 		mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1161 
1162 		/* Disable access to divisor latch */
1163 		Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1164 		mos7840_port->shadowLCR = Data;
1165 		mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1166 
1167 	}
1168 	return status;
1169 }
1170 
1171 /*****************************************************************************
1172  * mos7840_change_port_settings
1173  *	This routine is called to set the UART on the device to match
1174  *      the specified new settings.
1175  *****************************************************************************/
1176 
1177 static void mos7840_change_port_settings(struct tty_struct *tty,
1178 					 struct moschip_port *mos7840_port,
1179 					 const struct ktermios *old_termios)
1180 {
1181 	struct usb_serial_port *port = mos7840_port->port;
1182 	int baud;
1183 	unsigned cflag;
1184 	__u8 lData;
1185 	__u8 lParity;
1186 	__u8 lStop;
1187 	int status;
1188 	__u16 Data;
1189 
1190 	lData = LCR_BITS_8;
1191 	lStop = LCR_STOP_1;
1192 	lParity = LCR_PAR_NONE;
1193 
1194 	cflag = tty->termios.c_cflag;
1195 
1196 	/* Change the number of bits */
1197 	switch (cflag & CSIZE) {
1198 	case CS5:
1199 		lData = LCR_BITS_5;
1200 		break;
1201 
1202 	case CS6:
1203 		lData = LCR_BITS_6;
1204 		break;
1205 
1206 	case CS7:
1207 		lData = LCR_BITS_7;
1208 		break;
1209 
1210 	default:
1211 	case CS8:
1212 		lData = LCR_BITS_8;
1213 		break;
1214 	}
1215 
1216 	/* Change the Parity bit */
1217 	if (cflag & PARENB) {
1218 		if (cflag & PARODD) {
1219 			lParity = LCR_PAR_ODD;
1220 			dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1221 		} else {
1222 			lParity = LCR_PAR_EVEN;
1223 			dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1224 		}
1225 
1226 	} else {
1227 		dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1228 	}
1229 
1230 	if (cflag & CMSPAR)
1231 		lParity = lParity | 0x20;
1232 
1233 	/* Change the Stop bit */
1234 	if (cflag & CSTOPB) {
1235 		lStop = LCR_STOP_2;
1236 		dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1237 	} else {
1238 		lStop = LCR_STOP_1;
1239 		dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1240 	}
1241 
1242 	/* Update the LCR with the correct value */
1243 	mos7840_port->shadowLCR &=
1244 	    ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1245 	mos7840_port->shadowLCR |= (lData | lParity | lStop);
1246 
1247 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1248 		mos7840_port->shadowLCR);
1249 	/* Disable Interrupts */
1250 	Data = 0x00;
1251 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1252 
1253 	Data = 0x00;
1254 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1255 
1256 	Data = 0xcf;
1257 	mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1258 
1259 	/* Send the updated LCR value to the mos7840 */
1260 	Data = mos7840_port->shadowLCR;
1261 
1262 	mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1263 
1264 	Data = 0x00b;
1265 	mos7840_port->shadowMCR = Data;
1266 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1267 	Data = 0x00b;
1268 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1269 
1270 	/* set up the MCR register and send it to the mos7840 */
1271 
1272 	mos7840_port->shadowMCR = MCR_MASTER_IE;
1273 	if (cflag & CBAUD)
1274 		mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
1275 
1276 	if (cflag & CRTSCTS)
1277 		mos7840_port->shadowMCR |= (MCR_XON_ANY);
1278 	else
1279 		mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
1280 
1281 	Data = mos7840_port->shadowMCR;
1282 	mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1283 
1284 	/* Determine divisor based on baud rate */
1285 	baud = tty_get_baud_rate(tty);
1286 
1287 	if (!baud) {
1288 		/* pick a default, any default... */
1289 		dev_dbg(&port->dev, "%s", "Picked default baud...\n");
1290 		baud = 9600;
1291 	}
1292 
1293 	dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1294 	status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1295 
1296 	/* Enable Interrupts */
1297 	Data = 0x0c;
1298 	mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1299 
1300 	if (!mos7840_port->read_urb_busy) {
1301 		mos7840_port->read_urb_busy = true;
1302 		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1303 		if (status) {
1304 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1305 			    status);
1306 			mos7840_port->read_urb_busy = false;
1307 		}
1308 	}
1309 	dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1310 		mos7840_port->shadowLCR);
1311 }
1312 
1313 /*****************************************************************************
1314  * mos7840_set_termios
1315  *	this function is called by the tty driver when it wants to change
1316  *	the termios structure
1317  *****************************************************************************/
1318 
1319 static void mos7840_set_termios(struct tty_struct *tty,
1320 				struct usb_serial_port *port,
1321 				const struct ktermios *old_termios)
1322 {
1323 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
1324 	int status;
1325 
1326 	/* change the port settings to the new ones specified */
1327 
1328 	mos7840_change_port_settings(tty, mos7840_port, old_termios);
1329 
1330 	if (!mos7840_port->read_urb_busy) {
1331 		mos7840_port->read_urb_busy = true;
1332 		status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1333 		if (status) {
1334 			dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1335 			    status);
1336 			mos7840_port->read_urb_busy = false;
1337 		}
1338 	}
1339 }
1340 
1341 /*****************************************************************************
1342  * mos7840_get_lsr_info - get line status register info
1343  *
1344  * Purpose: Let user call ioctl() to get info when the UART physically
1345  * 	    is emptied.  On bus types like RS485, the transmitter must
1346  * 	    release the bus after transmitting. This must be done when
1347  * 	    the transmit shift register is empty, not be done when the
1348  * 	    transmit holding register is empty.  This functionality
1349  * 	    allows an RS485 driver to be written in user space.
1350  *****************************************************************************/
1351 
1352 static int mos7840_get_lsr_info(struct tty_struct *tty,
1353 				unsigned int __user *value)
1354 {
1355 	int count;
1356 	unsigned int result = 0;
1357 
1358 	count = mos7840_chars_in_buffer(tty);
1359 	if (count == 0)
1360 		result = TIOCSER_TEMT;
1361 
1362 	if (copy_to_user(value, &result, sizeof(int)))
1363 		return -EFAULT;
1364 	return 0;
1365 }
1366 
1367 /*****************************************************************************
1368  * SerialIoctl
1369  *	this function handles any ioctl calls to the driver
1370  *****************************************************************************/
1371 
1372 static int mos7840_ioctl(struct tty_struct *tty,
1373 			 unsigned int cmd, unsigned long arg)
1374 {
1375 	struct usb_serial_port *port = tty->driver_data;
1376 	void __user *argp = (void __user *)arg;
1377 
1378 	switch (cmd) {
1379 		/* return number of bytes available */
1380 
1381 	case TIOCSERGETLSR:
1382 		dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1383 		return mos7840_get_lsr_info(tty, argp);
1384 
1385 	default:
1386 		break;
1387 	}
1388 	return -ENOIOCTLCMD;
1389 }
1390 
1391 /*
1392  * Check if GPO (pin 42) is connected to GPI (pin 33) as recommended by ASIX
1393  * for MCS7810 by bit-banging a 16-bit word.
1394  *
1395  * Note that GPO is really RTS of the third port so this will toggle RTS of
1396  * port two or three on two- and four-port devices.
1397  */
1398 static int mos7810_check(struct usb_serial *serial)
1399 {
1400 	int i, pass_count = 0;
1401 	u8 *buf;
1402 	__u16 data = 0, mcr_data = 0;
1403 	__u16 test_pattern = 0x55AA;
1404 	int res;
1405 
1406 	buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
1407 	if (!buf)
1408 		return 0;	/* failed to identify 7810 */
1409 
1410 	/* Store MCR setting */
1411 	res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1412 		MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
1413 		buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
1414 	if (res == VENDOR_READ_LENGTH)
1415 		mcr_data = *buf;
1416 
1417 	for (i = 0; i < 16; i++) {
1418 		/* Send the 1-bit test pattern out to MCS7810 test pin */
1419 		usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1420 			MCS_WRREQ, MCS_WR_RTYPE,
1421 			(0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
1422 			MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
1423 
1424 		/* Read the test pattern back */
1425 		res = usb_control_msg(serial->dev,
1426 				usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
1427 				MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
1428 				VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
1429 		if (res == VENDOR_READ_LENGTH)
1430 			data = *buf;
1431 
1432 		/* If this is a MCS7810 device, both test patterns must match */
1433 		if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
1434 			break;
1435 
1436 		pass_count++;
1437 	}
1438 
1439 	/* Restore MCR setting */
1440 	usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
1441 		MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
1442 		0, MOS_WDR_TIMEOUT);
1443 
1444 	kfree(buf);
1445 
1446 	if (pass_count == 16)
1447 		return 1;
1448 
1449 	return 0;
1450 }
1451 
1452 static int mos7840_probe(struct usb_serial *serial,
1453 				const struct usb_device_id *id)
1454 {
1455 	unsigned long device_flags = id->driver_info;
1456 	u8 *buf;
1457 
1458 	/* Skip device-type detection if we already have device flags. */
1459 	if (device_flags)
1460 		goto out;
1461 
1462 	buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
1463 	if (!buf)
1464 		return -ENOMEM;
1465 
1466 	usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
1467 			MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
1468 			VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
1469 
1470 	/* For a MCS7840 device GPIO0 must be set to 1 */
1471 	if (buf[0] & 0x01)
1472 		device_flags = MCS_PORTS(4);
1473 	else if (mos7810_check(serial))
1474 		device_flags = MCS_PORTS(1) | MCS_LED;
1475 	else
1476 		device_flags = MCS_PORTS(2);
1477 
1478 	kfree(buf);
1479 out:
1480 	usb_set_serial_data(serial, (void *)device_flags);
1481 
1482 	return 0;
1483 }
1484 
1485 static int mos7840_calc_num_ports(struct usb_serial *serial,
1486 					struct usb_serial_endpoints *epds)
1487 {
1488 	unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
1489 	int num_ports = MCS_PORTS(device_flags);
1490 
1491 	if (num_ports == 0 || num_ports > 4)
1492 		return -ENODEV;
1493 
1494 	if (epds->num_bulk_in < num_ports || epds->num_bulk_out < num_ports) {
1495 		dev_err(&serial->interface->dev, "missing endpoints\n");
1496 		return -ENODEV;
1497 	}
1498 
1499 	return num_ports;
1500 }
1501 
1502 static int mos7840_attach(struct usb_serial *serial)
1503 {
1504 	struct device *dev = &serial->interface->dev;
1505 	int status;
1506 	u16 val;
1507 
1508 	/* Zero Length flag enable */
1509 	val = 0x0f;
1510 	status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, val);
1511 	if (status < 0)
1512 		dev_dbg(dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
1513 	else
1514 		dev_dbg(dev, "ZLP_REG5 Writing success status%d\n", status);
1515 
1516 	return status;
1517 }
1518 
1519 static int mos7840_port_probe(struct usb_serial_port *port)
1520 {
1521 	struct usb_serial *serial = port->serial;
1522 	unsigned long device_flags = (unsigned long)usb_get_serial_data(serial);
1523 	struct moschip_port *mos7840_port;
1524 	int status;
1525 	int pnum;
1526 	__u16 Data;
1527 
1528 	/* we set up the pointers to the endpoints in the mos7840_open *
1529 	 * function, as the structures aren't created yet.             */
1530 
1531 	pnum = port->port_number;
1532 
1533 	dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
1534 	mos7840_port = kzalloc_obj(struct moschip_port);
1535 	if (!mos7840_port)
1536 		return -ENOMEM;
1537 
1538 	/* Initialize all port interrupt end point to port 0 int
1539 	 * endpoint. Our device has only one interrupt end point
1540 	 * common to all port */
1541 
1542 	mos7840_port->port = port;
1543 	spin_lock_init(&mos7840_port->pool_lock);
1544 
1545 	/* minor is not initialised until later by
1546 	 * usb-serial.c:get_free_serial() and cannot therefore be used
1547 	 * to index device instances */
1548 	mos7840_port->port_num = pnum + 1;
1549 	dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
1550 	dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
1551 
1552 	if (mos7840_port->port_num == 1) {
1553 		mos7840_port->SpRegOffset = 0x0;
1554 		mos7840_port->ControlRegOffset = 0x1;
1555 		mos7840_port->DcrRegOffset = 0x4;
1556 	} else {
1557 		u8 phy_num = mos7840_port->port_num;
1558 
1559 		/* Port 2 in the 2-port case uses registers of port 3 */
1560 		if (serial->num_ports == 2)
1561 			phy_num = 3;
1562 
1563 		mos7840_port->SpRegOffset = 0x8 + 2 * (phy_num - 2);
1564 		mos7840_port->ControlRegOffset = 0x9 + 2 * (phy_num - 2);
1565 		mos7840_port->DcrRegOffset = 0x16 + 3 * (phy_num - 2);
1566 	}
1567 	mos7840_dump_serial_port(port, mos7840_port);
1568 	usb_set_serial_port_data(port, mos7840_port);
1569 
1570 	/* enable rx_disable bit in control register */
1571 	status = mos7840_get_reg_sync(port,
1572 			mos7840_port->ControlRegOffset, &Data);
1573 	if (status < 0) {
1574 		dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
1575 		goto error;
1576 	} else
1577 		dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
1578 	Data |= 0x08;	/* setting driver done bit */
1579 	Data |= 0x04;	/* sp1_bit to have cts change reflect in
1580 			   modem status reg */
1581 
1582 	/* Data |= 0x20; //rx_disable bit */
1583 	status = mos7840_set_reg_sync(port,
1584 			mos7840_port->ControlRegOffset, Data);
1585 	if (status < 0) {
1586 		dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
1587 		goto error;
1588 	} else
1589 		dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
1590 
1591 	/* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
1592 	   and 0x24 in DCR3 */
1593 	Data = 0x01;
1594 	status = mos7840_set_reg_sync(port,
1595 			(__u16) (mos7840_port->DcrRegOffset + 0), Data);
1596 	if (status < 0) {
1597 		dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
1598 		goto error;
1599 	} else
1600 		dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
1601 
1602 	Data = 0x05;
1603 	status = mos7840_set_reg_sync(port,
1604 			(__u16) (mos7840_port->DcrRegOffset + 1), Data);
1605 	if (status < 0) {
1606 		dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
1607 		goto error;
1608 	} else
1609 		dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
1610 
1611 	Data = 0x24;
1612 	status = mos7840_set_reg_sync(port,
1613 			(__u16) (mos7840_port->DcrRegOffset + 2), Data);
1614 	if (status < 0) {
1615 		dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
1616 		goto error;
1617 	} else
1618 		dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
1619 
1620 	/* write values in clkstart0x0 and clkmulti 0x20 */
1621 	Data = 0x0;
1622 	status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
1623 	if (status < 0) {
1624 		dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
1625 		goto error;
1626 	} else
1627 		dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
1628 
1629 	Data = 0x20;
1630 	status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
1631 	if (status < 0) {
1632 		dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
1633 		goto error;
1634 	} else
1635 		dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
1636 
1637 	/* write value 0x0 to scratchpad register */
1638 	Data = 0x00;
1639 	status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
1640 	if (status < 0) {
1641 		dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
1642 		goto error;
1643 	} else
1644 		dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
1645 
1646 	/* Zero Length flag register */
1647 	if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
1648 		Data = 0xff;
1649 		status = mos7840_set_reg_sync(port,
1650 				(__u16) (ZLP_REG1 +
1651 					((__u16)mos7840_port->port_num)), Data);
1652 		dev_dbg(&port->dev, "ZLIP offset %x\n",
1653 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
1654 		if (status < 0) {
1655 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
1656 			goto error;
1657 		} else
1658 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
1659 	} else {
1660 		Data = 0xff;
1661 		status = mos7840_set_reg_sync(port,
1662 				(__u16) (ZLP_REG1 +
1663 					((__u16)mos7840_port->port_num) - 0x1), Data);
1664 		dev_dbg(&port->dev, "ZLIP offset %x\n",
1665 				(__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
1666 		if (status < 0) {
1667 			dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
1668 			goto error;
1669 		} else
1670 			dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
1671 
1672 	}
1673 
1674 	mos7840_port->has_led = device_flags & MCS_LED;
1675 
1676 	/* Initialize LED timers */
1677 	if (mos7840_port->has_led) {
1678 		mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
1679 		mos7840_port->led_dr = kmalloc_obj(*mos7840_port->led_dr);
1680 		if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
1681 			status = -ENOMEM;
1682 			goto error;
1683 		}
1684 
1685 		timer_setup(&mos7840_port->led_timer1, mos7840_led_off, 0);
1686 		mos7840_port->led_timer1.expires =
1687 			jiffies + msecs_to_jiffies(LED_ON_MS);
1688 		timer_setup(&mos7840_port->led_timer2, mos7840_led_flag_off,
1689 			    0);
1690 		mos7840_port->led_timer2.expires =
1691 			jiffies + msecs_to_jiffies(LED_OFF_MS);
1692 
1693 		/* Turn off LED */
1694 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
1695 	}
1696 
1697 	return 0;
1698 error:
1699 	kfree(mos7840_port->led_dr);
1700 	usb_free_urb(mos7840_port->led_urb);
1701 	kfree(mos7840_port);
1702 
1703 	return status;
1704 }
1705 
1706 static void mos7840_port_remove(struct usb_serial_port *port)
1707 {
1708 	struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
1709 
1710 	if (mos7840_port->has_led) {
1711 		/* Turn off LED */
1712 		mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
1713 
1714 		timer_shutdown_sync(&mos7840_port->led_timer1);
1715 		timer_shutdown_sync(&mos7840_port->led_timer2);
1716 
1717 		usb_kill_urb(mos7840_port->led_urb);
1718 		usb_free_urb(mos7840_port->led_urb);
1719 		kfree(mos7840_port->led_dr);
1720 	}
1721 
1722 	kfree(mos7840_port);
1723 }
1724 
1725 static int mos7840_suspend(struct usb_serial *serial, pm_message_t message)
1726 {
1727 	struct moschip_port *mos7840_port;
1728 	struct usb_serial_port *port;
1729 	int i;
1730 
1731 	for (i = 0; i < serial->num_ports; ++i) {
1732 		port = serial->port[i];
1733 		if (!tty_port_initialized(&port->port))
1734 			continue;
1735 
1736 		mos7840_port = usb_get_serial_port_data(port);
1737 
1738 		usb_kill_urb(mos7840_port->read_urb);
1739 		mos7840_port->read_urb_busy = false;
1740 	}
1741 
1742 	return 0;
1743 }
1744 
1745 static int mos7840_resume(struct usb_serial *serial)
1746 {
1747 	struct moschip_port *mos7840_port;
1748 	struct usb_serial_port *port;
1749 	int res;
1750 	int i;
1751 
1752 	for (i = 0; i < serial->num_ports; ++i) {
1753 		port = serial->port[i];
1754 		if (!tty_port_initialized(&port->port))
1755 			continue;
1756 
1757 		mos7840_port = usb_get_serial_port_data(port);
1758 
1759 		mos7840_port->read_urb_busy = true;
1760 		res = usb_submit_urb(mos7840_port->read_urb, GFP_NOIO);
1761 		if (res)
1762 			mos7840_port->read_urb_busy = false;
1763 	}
1764 
1765 	return 0;
1766 }
1767 
1768 static struct usb_serial_driver moschip7840_4port_device = {
1769 	.driver = {
1770 		   .name = "mos7840",
1771 		   },
1772 	.description = DRIVER_DESC,
1773 	.id_table = id_table,
1774 	.num_interrupt_in = 1,
1775 	.open = mos7840_open,
1776 	.close = mos7840_close,
1777 	.write = mos7840_write,
1778 	.write_room = mos7840_write_room,
1779 	.chars_in_buffer = mos7840_chars_in_buffer,
1780 	.throttle = mos7840_throttle,
1781 	.unthrottle = mos7840_unthrottle,
1782 	.calc_num_ports = mos7840_calc_num_ports,
1783 	.probe = mos7840_probe,
1784 	.attach = mos7840_attach,
1785 	.ioctl = mos7840_ioctl,
1786 	.set_termios = mos7840_set_termios,
1787 	.break_ctl = mos7840_break,
1788 	.tiocmget = mos7840_tiocmget,
1789 	.tiocmset = mos7840_tiocmset,
1790 	.get_icount = usb_serial_generic_get_icount,
1791 	.port_probe = mos7840_port_probe,
1792 	.port_remove = mos7840_port_remove,
1793 	.read_bulk_callback = mos7840_bulk_in_callback,
1794 	.suspend = mos7840_suspend,
1795 	.resume = mos7840_resume,
1796 };
1797 
1798 static struct usb_serial_driver * const serial_drivers[] = {
1799 	&moschip7840_4port_device, NULL
1800 };
1801 
1802 module_usb_serial_driver(serial_drivers, id_table);
1803 
1804 MODULE_DESCRIPTION(DRIVER_DESC);
1805 MODULE_LICENSE("GPL");
1806