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