1 // SPDX-License-Identifier: GPL-2.0+
3 * Clean ups from Moschip version and a few ioctl implementations by:
4 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
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>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/tty.h>
16 #include <linux/tty_driver.h>
17 #include <linux/tty_flip.h>
18 #include <linux/module.h>
19 #include <linux/serial.h>
20 #include <linux/usb.h>
21 #include <linux/usb/serial.h>
22 #include <linux/uaccess.h>
24 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
27 * 16C50 UART register defines
30 #define LCR_BITS_5 0x00 /* 5 bits/char */
31 #define LCR_BITS_6 0x01 /* 6 bits/char */
32 #define LCR_BITS_7 0x02 /* 7 bits/char */
33 #define LCR_BITS_8 0x03 /* 8 bits/char */
34 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
36 #define LCR_STOP_1 0x00 /* 1 stop bit */
37 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
38 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
39 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
41 #define LCR_PAR_NONE 0x00 /* No parity */
42 #define LCR_PAR_ODD 0x08 /* Odd parity */
43 #define LCR_PAR_EVEN 0x18 /* Even parity */
44 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
45 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
46 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
48 #define LCR_SET_BREAK 0x40 /* Set Break condition */
49 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
51 #define MCR_DTR 0x01 /* Assert DTR */
52 #define MCR_RTS 0x02 /* Assert RTS */
53 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
54 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
55 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
56 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
58 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
59 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
60 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
61 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
64 * Defines used for sending commands to port
67 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
69 #define MOS_PORT1 0x0200
70 #define MOS_PORT2 0x0300
71 #define MOS_VENREG 0x0000
72 #define MOS_MAX_PORT 0x02
73 #define MOS_WRITE 0x0E
77 #define MCS_RD_RTYPE 0xC0
78 #define MCS_WR_RTYPE 0x40
79 #define MCS_RDREQ 0x0D
80 #define MCS_WRREQ 0x0E
81 #define MCS_CTRL_TIMEOUT 500
82 #define VENDOR_READ_LENGTH (0x01)
84 #define MAX_NAME_LEN 64
86 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
87 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
89 /* For higher baud Rates use TIOCEXBAUD */
90 #define TIOCEXBAUD 0x5462
92 /* vendor id and device id defines */
94 /* The native mos7840/7820 component */
95 #define USB_VENDOR_ID_MOSCHIP 0x9710
96 #define MOSCHIP_DEVICE_ID_7840 0x7840
97 #define MOSCHIP_DEVICE_ID_7820 0x7820
98 #define MOSCHIP_DEVICE_ID_7810 0x7810
99 /* The native component can have its vendor/device id's overridden
100 * in vendor-specific implementations. Such devices can be handled
101 * by making a change here, in id_table.
103 #define USB_VENDOR_ID_BANDB 0x0856
104 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
105 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
106 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
107 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
108 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
109 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
110 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
111 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
112 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
113 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
114 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
115 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
116 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
118 /* This driver also supports
119 * ATEN UC2324 device using Moschip MCS7840
120 * ATEN UC2322 device using Moschip MCS7820
122 #define USB_VENDOR_ID_ATENINTL 0x0557
123 #define ATENINTL_DEVICE_ID_UC2324 0x2011
124 #define ATENINTL_DEVICE_ID_UC2322 0x7820
126 /* Interrupt Routine Defines */
128 #define SERIAL_IIR_RLS 0x06
129 #define SERIAL_IIR_MS 0x00
132 * Emulation of the bit mask on the LINE STATUS REGISTER.
134 #define SERIAL_LSR_DR 0x0001
135 #define SERIAL_LSR_OE 0x0002
136 #define SERIAL_LSR_PE 0x0004
137 #define SERIAL_LSR_FE 0x0008
138 #define SERIAL_LSR_BI 0x0010
140 #define MOS_MSR_DELTA_CTS 0x10
141 #define MOS_MSR_DELTA_DSR 0x20
142 #define MOS_MSR_DELTA_RI 0x40
143 #define MOS_MSR_DELTA_CD 0x80
145 /* Serial Port register Address */
146 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
147 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
148 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
149 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
150 #define LINE_STATUS_REGISTER ((__u16)(0x05))
151 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
152 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
153 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
154 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
156 #define CLK_MULTI_REGISTER ((__u16)(0x02))
157 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
158 #define GPIO_REGISTER ((__u16)(0x07))
160 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
163 * URB POOL related defines
165 #define NUM_URBS 16 /* URB Count */
166 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
168 /* LED on/off milliseconds*/
169 #define LED_ON_MS 500
170 #define LED_OFF_MS 500
173 MOS7840_FLAG_CTRL_BUSY
,
174 MOS7840_FLAG_LED_BUSY
,
177 static const struct usb_device_id id_table
[] = {
178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
179 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
180 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7810
)},
181 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_2
)},
182 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_2P
)},
183 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_4
)},
184 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_4P
)},
185 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_US9ML2_2
)},
186 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_US9ML2_4
)},
187 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USPTL4_2
)},
188 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USPTL4_4
)},
189 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
190 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2P
)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
192 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4P
)},
193 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL2_4
)},
194 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2324
)},
195 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2322
)},
196 {} /* terminating entry */
198 MODULE_DEVICE_TABLE(usb
, id_table
);
200 /* This structure holds all of the local port information */
202 struct moschip_port
{
203 int port_num
; /*Actual port number in the device(1,2,etc) */
204 struct urb
*read_urb
; /* read URB for this port */
205 __u8 shadowLCR
; /* last LCR value received */
206 __u8 shadowMCR
; /* last MCR value received */
209 struct usb_serial_port
*port
; /* loop back to the owner of this object */
213 __u8 ControlRegOffset
;
215 /* for processing control URBS in interrupt context */
216 struct urb
*control_urb
;
217 struct usb_ctrlrequest
*dr
;
221 spinlock_t pool_lock
;
222 struct urb
*write_urb_pool
[NUM_URBS
];
226 /* For device(s) with LED indicator */
228 struct timer_list led_timer1
; /* Timer for LED on */
229 struct timer_list led_timer2
; /* Timer for LED off */
231 struct usb_ctrlrequest
*led_dr
;
237 * mos7840_set_reg_sync
238 * To set the Control register by calling usb_fill_control_urb function
239 * by passing usb_sndctrlpipe function as parameter.
242 static int mos7840_set_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
245 struct usb_device
*dev
= port
->serial
->dev
;
247 dev_dbg(&port
->dev
, "mos7840_set_reg_sync offset is %x, value %x\n", reg
, val
);
249 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
250 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
255 * mos7840_get_reg_sync
256 * To set the Uart register by calling usb_fill_control_urb function by
257 * passing usb_rcvctrlpipe function as parameter.
260 static int mos7840_get_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
263 struct usb_device
*dev
= port
->serial
->dev
;
267 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
271 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
272 MCS_RD_RTYPE
, 0, reg
, buf
, VENDOR_READ_LENGTH
,
274 if (ret
< VENDOR_READ_LENGTH
) {
281 dev_dbg(&port
->dev
, "%s offset is %x, return val %x\n", __func__
, reg
, *val
);
288 * mos7840_set_uart_reg
289 * To set the Uart register by calling usb_fill_control_urb function by
290 * passing usb_sndctrlpipe function as parameter.
293 static int mos7840_set_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
297 struct usb_device
*dev
= port
->serial
->dev
;
299 /* For the UART control registers, the application number need
301 if (port
->serial
->num_ports
== 4) {
302 val
|= ((__u16
)port
->port_number
+ 1) << 8;
304 if (port
->port_number
== 0) {
305 val
|= ((__u16
)port
->port_number
+ 1) << 8;
307 val
|= ((__u16
)port
->port_number
+ 2) << 8;
310 dev_dbg(&port
->dev
, "%s application number is %x\n", __func__
, val
);
311 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
312 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
318 * mos7840_get_uart_reg
319 * To set the Control register by calling usb_fill_control_urb function
320 * by passing usb_rcvctrlpipe function as parameter.
322 static int mos7840_get_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
325 struct usb_device
*dev
= port
->serial
->dev
;
330 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
334 /* Wval is same as application number */
335 if (port
->serial
->num_ports
== 4) {
336 Wval
= ((__u16
)port
->port_number
+ 1) << 8;
338 if (port
->port_number
== 0) {
339 Wval
= ((__u16
)port
->port_number
+ 1) << 8;
341 Wval
= ((__u16
)port
->port_number
+ 2) << 8;
344 dev_dbg(&port
->dev
, "%s application number is %x\n", __func__
, Wval
);
345 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
346 MCS_RD_RTYPE
, Wval
, reg
, buf
, VENDOR_READ_LENGTH
,
348 if (ret
< VENDOR_READ_LENGTH
) {
359 static void mos7840_dump_serial_port(struct usb_serial_port
*port
,
360 struct moschip_port
*mos7840_port
)
363 dev_dbg(&port
->dev
, "SpRegOffset is %2x\n", mos7840_port
->SpRegOffset
);
364 dev_dbg(&port
->dev
, "ControlRegOffset is %2x\n", mos7840_port
->ControlRegOffset
);
365 dev_dbg(&port
->dev
, "DCRRegOffset is %2x\n", mos7840_port
->DcrRegOffset
);
369 /************************************************************************/
370 /************************************************************************/
371 /* I N T E R F A C E F U N C T I O N S */
372 /* I N T E R F A C E F U N C T I O N S */
373 /************************************************************************/
374 /************************************************************************/
376 static inline void mos7840_set_port_private(struct usb_serial_port
*port
,
377 struct moschip_port
*data
)
379 usb_set_serial_port_data(port
, (void *)data
);
382 static inline struct moschip_port
*mos7840_get_port_private(struct
386 return (struct moschip_port
*)usb_get_serial_port_data(port
);
389 static void mos7840_handle_new_msr(struct moschip_port
*port
, __u8 new_msr
)
391 struct moschip_port
*mos7840_port
;
392 struct async_icount
*icount
;
395 (MOS_MSR_DELTA_CTS
| MOS_MSR_DELTA_DSR
| MOS_MSR_DELTA_RI
|
397 icount
= &mos7840_port
->port
->icount
;
399 /* update input line counters */
400 if (new_msr
& MOS_MSR_DELTA_CTS
)
402 if (new_msr
& MOS_MSR_DELTA_DSR
)
404 if (new_msr
& MOS_MSR_DELTA_CD
)
406 if (new_msr
& MOS_MSR_DELTA_RI
)
409 wake_up_interruptible(&port
->port
->port
.delta_msr_wait
);
413 static void mos7840_handle_new_lsr(struct moschip_port
*port
, __u8 new_lsr
)
415 struct async_icount
*icount
;
417 if (new_lsr
& SERIAL_LSR_BI
) {
419 * Parity and Framing errors only count if they
420 * occur exclusive of a break being
423 new_lsr
&= (__u8
) (SERIAL_LSR_OE
| SERIAL_LSR_BI
);
426 /* update input line counters */
427 icount
= &port
->port
->icount
;
428 if (new_lsr
& SERIAL_LSR_BI
)
430 if (new_lsr
& SERIAL_LSR_OE
)
432 if (new_lsr
& SERIAL_LSR_PE
)
434 if (new_lsr
& SERIAL_LSR_FE
)
438 /************************************************************************/
439 /************************************************************************/
440 /* U S B C A L L B A C K F U N C T I O N S */
441 /* U S B C A L L B A C K F U N C T I O N S */
442 /************************************************************************/
443 /************************************************************************/
445 static void mos7840_control_callback(struct urb
*urb
)
448 struct moschip_port
*mos7840_port
;
449 struct device
*dev
= &urb
->dev
->dev
;
451 int status
= urb
->status
;
453 mos7840_port
= urb
->context
;
462 /* this urb is terminated, clean up */
463 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
466 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
470 dev_dbg(dev
, "%s urb buffer size is %d\n", __func__
, urb
->actual_length
);
471 if (urb
->actual_length
< 1)
474 dev_dbg(dev
, "%s mos7840_port->MsrLsr is %d port %d\n", __func__
,
475 mos7840_port
->MsrLsr
, mos7840_port
->port_num
);
476 data
= urb
->transfer_buffer
;
477 regval
= (__u8
) data
[0];
478 dev_dbg(dev
, "%s data is %x\n", __func__
, regval
);
479 if (mos7840_port
->MsrLsr
== 0)
480 mos7840_handle_new_msr(mos7840_port
, regval
);
481 else if (mos7840_port
->MsrLsr
== 1)
482 mos7840_handle_new_lsr(mos7840_port
, regval
);
484 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY
, &mos7840_port
->flags
);
487 static int mos7840_get_reg(struct moschip_port
*mcs
, __u16 Wval
, __u16 reg
,
490 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
491 struct usb_ctrlrequest
*dr
= mcs
->dr
;
492 unsigned char *buffer
= mcs
->ctrl_buf
;
495 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY
, &mcs
->flags
))
498 dr
->bRequestType
= MCS_RD_RTYPE
;
499 dr
->bRequest
= MCS_RDREQ
;
500 dr
->wValue
= cpu_to_le16(Wval
); /* 0 */
501 dr
->wIndex
= cpu_to_le16(reg
);
502 dr
->wLength
= cpu_to_le16(2);
504 usb_fill_control_urb(mcs
->control_urb
, dev
, usb_rcvctrlpipe(dev
, 0),
505 (unsigned char *)dr
, buffer
, 2,
506 mos7840_control_callback
, mcs
);
507 mcs
->control_urb
->transfer_buffer_length
= 2;
508 ret
= usb_submit_urb(mcs
->control_urb
, GFP_ATOMIC
);
510 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY
, &mcs
->flags
);
515 static void mos7840_set_led_callback(struct urb
*urb
)
517 switch (urb
->status
) {
524 /* This urb is terminated, clean up */
525 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down: %d\n",
526 __func__
, urb
->status
);
529 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status: %d\n",
530 __func__
, urb
->status
);
534 static void mos7840_set_led_async(struct moschip_port
*mcs
, __u16 wval
,
537 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
538 struct usb_ctrlrequest
*dr
= mcs
->led_dr
;
540 dr
->bRequestType
= MCS_WR_RTYPE
;
541 dr
->bRequest
= MCS_WRREQ
;
542 dr
->wValue
= cpu_to_le16(wval
);
543 dr
->wIndex
= cpu_to_le16(reg
);
544 dr
->wLength
= cpu_to_le16(0);
546 usb_fill_control_urb(mcs
->led_urb
, dev
, usb_sndctrlpipe(dev
, 0),
547 (unsigned char *)dr
, NULL
, 0, mos7840_set_led_callback
, NULL
);
549 usb_submit_urb(mcs
->led_urb
, GFP_ATOMIC
);
552 static void mos7840_set_led_sync(struct usb_serial_port
*port
, __u16 reg
,
555 struct usb_device
*dev
= port
->serial
->dev
;
557 usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
, MCS_WR_RTYPE
,
558 val
, reg
, NULL
, 0, MOS_WDR_TIMEOUT
);
561 static void mos7840_led_off(struct timer_list
*t
)
563 struct moschip_port
*mcs
= from_timer(mcs
, t
, led_timer1
);
566 mos7840_set_led_async(mcs
, 0x0300, MODEM_CONTROL_REGISTER
);
567 mod_timer(&mcs
->led_timer2
,
568 jiffies
+ msecs_to_jiffies(LED_OFF_MS
));
571 static void mos7840_led_flag_off(struct timer_list
*t
)
573 struct moschip_port
*mcs
= from_timer(mcs
, t
, led_timer2
);
575 clear_bit_unlock(MOS7840_FLAG_LED_BUSY
, &mcs
->flags
);
578 static void mos7840_led_activity(struct usb_serial_port
*port
)
580 struct moschip_port
*mos7840_port
= usb_get_serial_port_data(port
);
582 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY
, &mos7840_port
->flags
))
585 mos7840_set_led_async(mos7840_port
, 0x0301, MODEM_CONTROL_REGISTER
);
586 mod_timer(&mos7840_port
->led_timer1
,
587 jiffies
+ msecs_to_jiffies(LED_ON_MS
));
590 /*****************************************************************************
591 * mos7840_interrupt_callback
592 * this is the callback function for when we have received data on the
593 * interrupt endpoint.
594 *****************************************************************************/
596 static void mos7840_interrupt_callback(struct urb
*urb
)
600 struct moschip_port
*mos7840_port
;
601 struct usb_serial
*serial
;
606 __u16 wval
, wreg
= 0;
607 int status
= urb
->status
;
616 /* this urb is terminated, clean up */
617 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d\n",
621 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n",
626 length
= urb
->actual_length
;
627 data
= urb
->transfer_buffer
;
629 serial
= urb
->context
;
631 /* Moschip get 5 bytes
632 * Byte 1 IIR Port 1 (port.number is 0)
633 * Byte 2 IIR Port 2 (port.number is 1)
634 * Byte 3 IIR Port 3 (port.number is 2)
635 * Byte 4 IIR Port 4 (port.number is 3)
636 * Byte 5 FIFO status for both */
639 dev_dbg(&urb
->dev
->dev
, "%s", "Wrong data !!!\n");
643 sp
[0] = (__u8
) data
[0];
644 sp
[1] = (__u8
) data
[1];
645 sp
[2] = (__u8
) data
[2];
646 sp
[3] = (__u8
) data
[3];
649 for (i
= 0; i
< serial
->num_ports
; i
++) {
650 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
651 wval
= ((__u16
)serial
->port
[i
]->port_number
+ 1) << 8;
652 if (mos7840_port
->open
) {
654 dev_dbg(&urb
->dev
->dev
, "SP%d No Interrupt !!!\n", i
);
656 switch (sp
[i
] & 0x0f) {
658 dev_dbg(&urb
->dev
->dev
, "Serial Port %d: Receiver status error or \n", i
);
659 dev_dbg(&urb
->dev
->dev
, "address bit detected in 9-bit mode\n");
660 mos7840_port
->MsrLsr
= 1;
661 wreg
= LINE_STATUS_REGISTER
;
664 dev_dbg(&urb
->dev
->dev
, "Serial Port %d: Modem status change\n", i
);
665 mos7840_port
->MsrLsr
= 0;
666 wreg
= MODEM_STATUS_REGISTER
;
669 rv
= mos7840_get_reg(mos7840_port
, wval
, wreg
, &Data
);
674 /* the completion handler for the control urb will resubmit */
677 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
679 dev_err(&urb
->dev
->dev
,
680 "%s - Error %d submitting interrupt urb\n",
685 static int mos7840_port_paranoia_check(struct usb_serial_port
*port
,
686 const char *function
)
689 pr_debug("%s - port == NULL\n", function
);
693 pr_debug("%s - port->serial == NULL\n", function
);
700 /* Inline functions to check the sanity of a pointer that is passed to us */
701 static int mos7840_serial_paranoia_check(struct usb_serial
*serial
,
702 const char *function
)
705 pr_debug("%s - serial == NULL\n", function
);
709 pr_debug("%s - serial->type == NULL!\n", function
);
716 static struct usb_serial
*mos7840_get_usb_serial(struct usb_serial_port
*port
,
717 const char *function
)
719 /* if no port was specified, or it fails a paranoia check */
721 mos7840_port_paranoia_check(port
, function
) ||
722 mos7840_serial_paranoia_check(port
->serial
, function
)) {
723 /* then say that we don't have a valid usb_serial thing,
724 * which will end up genrating -ENODEV return values */
731 /*****************************************************************************
732 * mos7840_bulk_in_callback
733 * this is the callback function for when we have received data on the
735 *****************************************************************************/
737 static void mos7840_bulk_in_callback(struct urb
*urb
)
741 struct usb_serial
*serial
;
742 struct usb_serial_port
*port
;
743 struct moschip_port
*mos7840_port
;
744 int status
= urb
->status
;
746 mos7840_port
= urb
->context
;
751 dev_dbg(&urb
->dev
->dev
, "nonzero read bulk status received: %d\n", status
);
752 mos7840_port
->read_urb_busy
= false;
756 port
= mos7840_port
->port
;
757 if (mos7840_port_paranoia_check(port
, __func__
)) {
758 mos7840_port
->read_urb_busy
= false;
762 serial
= mos7840_get_usb_serial(port
, __func__
);
764 mos7840_port
->read_urb_busy
= false;
768 data
= urb
->transfer_buffer
;
769 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
771 if (urb
->actual_length
) {
772 struct tty_port
*tport
= &mos7840_port
->port
->port
;
773 tty_insert_flip_string(tport
, data
, urb
->actual_length
);
774 tty_flip_buffer_push(tport
);
775 port
->icount
.rx
+= urb
->actual_length
;
776 dev_dbg(&port
->dev
, "icount.rx is %d:\n", port
->icount
.rx
);
779 if (!mos7840_port
->read_urb
) {
780 dev_dbg(&port
->dev
, "%s", "URB KILLED !!!\n");
781 mos7840_port
->read_urb_busy
= false;
785 if (mos7840_port
->has_led
)
786 mos7840_led_activity(port
);
788 mos7840_port
->read_urb_busy
= true;
789 retval
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
792 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, retval = %d\n", retval
);
793 mos7840_port
->read_urb_busy
= false;
797 /*****************************************************************************
798 * mos7840_bulk_out_data_callback
799 * this is the callback function for when we have finished sending
800 * serial data on the bulk out endpoint.
801 *****************************************************************************/
803 static void mos7840_bulk_out_data_callback(struct urb
*urb
)
805 struct moschip_port
*mos7840_port
;
806 struct usb_serial_port
*port
;
807 int status
= urb
->status
;
811 mos7840_port
= urb
->context
;
812 port
= mos7840_port
->port
;
813 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
814 for (i
= 0; i
< NUM_URBS
; i
++) {
815 if (urb
== mos7840_port
->write_urb_pool
[i
]) {
816 mos7840_port
->busy
[i
] = 0;
820 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
823 dev_dbg(&port
->dev
, "nonzero write bulk status received:%d\n", status
);
827 if (mos7840_port_paranoia_check(port
, __func__
))
830 if (mos7840_port
->open
)
831 tty_port_tty_wakeup(&port
->port
);
835 /************************************************************************/
836 /* D R I V E R T T Y I N T E R F A C E F U N C T I O N S */
837 /************************************************************************/
839 /*****************************************************************************
841 * this function is called by the tty driver when a port is opened
842 * If successful, we return 0
843 * Otherwise we return a negative error number.
844 *****************************************************************************/
846 static int mos7840_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
850 struct usb_serial
*serial
;
854 struct moschip_port
*mos7840_port
;
855 struct moschip_port
*port0
;
857 if (mos7840_port_paranoia_check(port
, __func__
))
860 serial
= port
->serial
;
862 if (mos7840_serial_paranoia_check(serial
, __func__
))
865 mos7840_port
= mos7840_get_port_private(port
);
866 port0
= mos7840_get_port_private(serial
->port
[0]);
868 if (mos7840_port
== NULL
|| port0
== NULL
)
871 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
872 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
875 /* Initialising the write urb pool */
876 for (j
= 0; j
< NUM_URBS
; ++j
) {
877 urb
= usb_alloc_urb(0, GFP_KERNEL
);
878 mos7840_port
->write_urb_pool
[j
] = urb
;
882 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
884 if (!urb
->transfer_buffer
) {
886 mos7840_port
->write_urb_pool
[j
] = NULL
;
891 /*****************************************************************************
892 * Initialize MCS7840 -- Write Init values to corresponding Registers
900 * 0x08 : SP1/2 Control Reg
901 *****************************************************************************/
903 /* NEED to check the following Block */
906 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
908 dev_dbg(&port
->dev
, "Reading Spreg failed\n");
912 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
914 dev_dbg(&port
->dev
, "writing Spreg failed\n");
919 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
921 dev_dbg(&port
->dev
, "writing Spreg failed\n");
924 /* End of block to be checked */
927 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
930 dev_dbg(&port
->dev
, "Reading Controlreg failed\n");
933 Data
|= 0x08; /* Driver done bit */
934 Data
|= 0x20; /* rx_disable */
935 status
= mos7840_set_reg_sync(port
,
936 mos7840_port
->ControlRegOffset
, Data
);
938 dev_dbg(&port
->dev
, "writing Controlreg failed\n");
941 /* do register settings here */
942 /* Set all regs to the device default values. */
943 /***********************************
944 * First Disable all interrupts.
945 ***********************************/
947 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
949 dev_dbg(&port
->dev
, "disabling interrupts failed\n");
952 /* Set FIFO_CONTROL_REGISTER to the default value */
954 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
956 dev_dbg(&port
->dev
, "Writing FIFO_CONTROL_REGISTER failed\n");
961 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
963 dev_dbg(&port
->dev
, "Writing FIFO_CONTROL_REGISTER failed\n");
968 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
969 mos7840_port
->shadowLCR
= Data
;
972 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
973 mos7840_port
->shadowMCR
= Data
;
976 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
977 mos7840_port
->shadowLCR
= Data
;
979 Data
|= SERIAL_LCR_DLAB
; /* data latch enable in LCR 0x80 */
980 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
983 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
986 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
989 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
991 Data
= Data
& ~SERIAL_LCR_DLAB
;
992 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
993 mos7840_port
->shadowLCR
= Data
;
995 /* clearing Bulkin and Bulkout Fifo */
997 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
1000 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1002 Data
= Data
& ~0x0c;
1003 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1004 /* Finally enable all interrupts */
1006 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1008 /* clearing rx_disable */
1010 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1012 Data
= Data
& ~0x20;
1013 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1018 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1021 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1024 /* Check to see if we've set up our endpoint info yet *
1025 * (can't set it up in mos7840_startup as the structures *
1026 * were not set up at that time.) */
1027 if (port0
->open_ports
== 1) {
1028 /* FIXME: Buffer never NULL, so URB is not submitted. */
1029 if (serial
->port
[0]->interrupt_in_buffer
== NULL
) {
1030 /* set up interrupt urb */
1031 usb_fill_int_urb(serial
->port
[0]->interrupt_in_urb
,
1033 usb_rcvintpipe(serial
->dev
,
1034 serial
->port
[0]->interrupt_in_endpointAddress
),
1035 serial
->port
[0]->interrupt_in_buffer
,
1036 serial
->port
[0]->interrupt_in_urb
->
1037 transfer_buffer_length
,
1038 mos7840_interrupt_callback
,
1040 serial
->port
[0]->interrupt_in_urb
->interval
);
1042 /* start interrupt read for mos7840 */
1044 usb_submit_urb(serial
->port
[0]->interrupt_in_urb
,
1047 dev_err(&port
->dev
, "%s - Error %d submitting "
1048 "interrupt urb\n", __func__
, response
);
1055 /* see if we've set up our endpoint info yet *
1056 * (can't set it up in mos7840_startup as the *
1057 * structures were not set up at that time.) */
1059 dev_dbg(&port
->dev
, "port number is %d\n", port
->port_number
);
1060 dev_dbg(&port
->dev
, "minor number is %d\n", port
->minor
);
1061 dev_dbg(&port
->dev
, "Bulkin endpoint is %d\n", port
->bulk_in_endpointAddress
);
1062 dev_dbg(&port
->dev
, "BulkOut endpoint is %d\n", port
->bulk_out_endpointAddress
);
1063 dev_dbg(&port
->dev
, "Interrupt endpoint is %d\n", port
->interrupt_in_endpointAddress
);
1064 dev_dbg(&port
->dev
, "port's number in the device is %d\n", mos7840_port
->port_num
);
1065 mos7840_port
->read_urb
= port
->read_urb
;
1067 /* set up our bulk in urb */
1068 if ((serial
->num_ports
== 2) && (((__u16
)port
->port_number
% 2) != 0)) {
1069 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1071 usb_rcvbulkpipe(serial
->dev
,
1072 (port
->bulk_in_endpointAddress
) + 2),
1073 port
->bulk_in_buffer
,
1074 mos7840_port
->read_urb
->transfer_buffer_length
,
1075 mos7840_bulk_in_callback
, mos7840_port
);
1077 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1079 usb_rcvbulkpipe(serial
->dev
,
1080 port
->bulk_in_endpointAddress
),
1081 port
->bulk_in_buffer
,
1082 mos7840_port
->read_urb
->transfer_buffer_length
,
1083 mos7840_bulk_in_callback
, mos7840_port
);
1086 dev_dbg(&port
->dev
, "%s: bulkin endpoint is %d\n", __func__
, port
->bulk_in_endpointAddress
);
1087 mos7840_port
->read_urb_busy
= true;
1088 response
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1090 dev_err(&port
->dev
, "%s - Error %d submitting control urb\n",
1091 __func__
, response
);
1092 mos7840_port
->read_urb_busy
= false;
1095 /* initialize our port settings */
1096 /* Must set to enable ints! */
1097 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
1098 /* send a open port command */
1099 mos7840_port
->open
= 1;
1100 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1104 for (j
= 0; j
< NUM_URBS
; ++j
) {
1105 urb
= mos7840_port
->write_urb_pool
[j
];
1108 kfree(urb
->transfer_buffer
);
1114 /*****************************************************************************
1115 * mos7840_chars_in_buffer
1116 * this function is called by the tty driver when it wants to know how many
1117 * bytes of data we currently have outstanding in the port (data that has
1118 * been written, but hasn't made it out the port yet)
1119 * If successful, we return the number of bytes left to be written in the
1121 * Otherwise we return zero.
1122 *****************************************************************************/
1124 static int mos7840_chars_in_buffer(struct tty_struct
*tty
)
1126 struct usb_serial_port
*port
= tty
->driver_data
;
1129 unsigned long flags
;
1130 struct moschip_port
*mos7840_port
;
1132 if (mos7840_port_paranoia_check(port
, __func__
))
1135 mos7840_port
= mos7840_get_port_private(port
);
1136 if (mos7840_port
== NULL
)
1139 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1140 for (i
= 0; i
< NUM_URBS
; ++i
) {
1141 if (mos7840_port
->busy
[i
]) {
1142 struct urb
*urb
= mos7840_port
->write_urb_pool
[i
];
1143 chars
+= urb
->transfer_buffer_length
;
1146 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1147 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
1152 /*****************************************************************************
1154 * this function is called by the tty driver when a port is closed
1155 *****************************************************************************/
1157 static void mos7840_close(struct usb_serial_port
*port
)
1159 struct usb_serial
*serial
;
1160 struct moschip_port
*mos7840_port
;
1161 struct moschip_port
*port0
;
1165 if (mos7840_port_paranoia_check(port
, __func__
))
1168 serial
= mos7840_get_usb_serial(port
, __func__
);
1172 mos7840_port
= mos7840_get_port_private(port
);
1173 port0
= mos7840_get_port_private(serial
->port
[0]);
1175 if (mos7840_port
== NULL
|| port0
== NULL
)
1178 for (j
= 0; j
< NUM_URBS
; ++j
)
1179 usb_kill_urb(mos7840_port
->write_urb_pool
[j
]);
1181 /* Freeing Write URBs */
1182 for (j
= 0; j
< NUM_URBS
; ++j
) {
1183 if (mos7840_port
->write_urb_pool
[j
]) {
1184 kfree(mos7840_port
->write_urb_pool
[j
]->transfer_buffer
);
1185 usb_free_urb(mos7840_port
->write_urb_pool
[j
]);
1189 usb_kill_urb(mos7840_port
->read_urb
);
1190 mos7840_port
->read_urb_busy
= false;
1192 port0
->open_ports
--;
1193 dev_dbg(&port
->dev
, "%s in close%d\n", __func__
, port0
->open_ports
);
1194 if (port0
->open_ports
== 0) {
1195 if (serial
->port
[0]->interrupt_in_urb
) {
1196 dev_dbg(&port
->dev
, "Shutdown interrupt_in_urb\n");
1197 usb_kill_urb(serial
->port
[0]->interrupt_in_urb
);
1202 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1205 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1207 mos7840_port
->open
= 0;
1210 /*****************************************************************************
1212 * this function sends a break to the port
1213 *****************************************************************************/
1214 static void mos7840_break(struct tty_struct
*tty
, int break_state
)
1216 struct usb_serial_port
*port
= tty
->driver_data
;
1218 struct usb_serial
*serial
;
1219 struct moschip_port
*mos7840_port
;
1221 if (mos7840_port_paranoia_check(port
, __func__
))
1224 serial
= mos7840_get_usb_serial(port
, __func__
);
1228 mos7840_port
= mos7840_get_port_private(port
);
1230 if (mos7840_port
== NULL
)
1233 if (break_state
== -1)
1234 data
= mos7840_port
->shadowLCR
| LCR_SET_BREAK
;
1236 data
= mos7840_port
->shadowLCR
& ~LCR_SET_BREAK
;
1238 /* FIXME: no locking on shadowLCR anywhere in driver */
1239 mos7840_port
->shadowLCR
= data
;
1240 dev_dbg(&port
->dev
, "%s mos7840_port->shadowLCR is %x\n", __func__
, mos7840_port
->shadowLCR
);
1241 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
,
1242 mos7840_port
->shadowLCR
);
1245 /*****************************************************************************
1246 * mos7840_write_room
1247 * this function is called by the tty driver when it wants to know how many
1248 * bytes of data we can accept for a specific port.
1249 * If successful, we return the amount of room that we have for this port
1250 * Otherwise we return a negative error number.
1251 *****************************************************************************/
1253 static int mos7840_write_room(struct tty_struct
*tty
)
1255 struct usb_serial_port
*port
= tty
->driver_data
;
1258 unsigned long flags
;
1259 struct moschip_port
*mos7840_port
;
1261 if (mos7840_port_paranoia_check(port
, __func__
))
1264 mos7840_port
= mos7840_get_port_private(port
);
1265 if (mos7840_port
== NULL
)
1268 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1269 for (i
= 0; i
< NUM_URBS
; ++i
) {
1270 if (!mos7840_port
->busy
[i
])
1271 room
+= URB_TRANSFER_BUFFER_SIZE
;
1273 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1275 room
= (room
== 0) ? 0 : room
- URB_TRANSFER_BUFFER_SIZE
+ 1;
1276 dev_dbg(&mos7840_port
->port
->dev
, "%s - returns %d\n", __func__
, room
);
1281 /*****************************************************************************
1283 * this function is called by the tty driver when data should be written to
1285 * If successful, we return the number of bytes written, otherwise we
1286 * return a negative error number.
1287 *****************************************************************************/
1289 static int mos7840_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1290 const unsigned char *data
, int count
)
1296 unsigned long flags
;
1298 struct moschip_port
*mos7840_port
;
1299 struct usb_serial
*serial
;
1302 const unsigned char *current_position
= data
;
1303 unsigned char *data1
;
1305 if (mos7840_port_paranoia_check(port
, __func__
))
1308 serial
= port
->serial
;
1309 if (mos7840_serial_paranoia_check(serial
, __func__
))
1312 mos7840_port
= mos7840_get_port_private(port
);
1313 if (mos7840_port
== NULL
)
1316 /* try to find a free urb in the list */
1319 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1320 for (i
= 0; i
< NUM_URBS
; ++i
) {
1321 if (!mos7840_port
->busy
[i
]) {
1322 mos7840_port
->busy
[i
] = 1;
1323 urb
= mos7840_port
->write_urb_pool
[i
];
1324 dev_dbg(&port
->dev
, "URB:%d\n", i
);
1328 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1331 dev_dbg(&port
->dev
, "%s - no more free urbs\n", __func__
);
1335 if (urb
->transfer_buffer
== NULL
) {
1336 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
1338 if (!urb
->transfer_buffer
)
1341 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1343 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1345 /* fill urb with data and submit */
1346 if ((serial
->num_ports
== 2) && (((__u16
)port
->port_number
% 2) != 0)) {
1347 usb_fill_bulk_urb(urb
,
1349 usb_sndbulkpipe(serial
->dev
,
1350 (port
->bulk_out_endpointAddress
) + 2),
1351 urb
->transfer_buffer
,
1353 mos7840_bulk_out_data_callback
, mos7840_port
);
1355 usb_fill_bulk_urb(urb
,
1357 usb_sndbulkpipe(serial
->dev
,
1358 port
->bulk_out_endpointAddress
),
1359 urb
->transfer_buffer
,
1361 mos7840_bulk_out_data_callback
, mos7840_port
);
1364 data1
= urb
->transfer_buffer
;
1365 dev_dbg(&port
->dev
, "bulkout endpoint is %d\n", port
->bulk_out_endpointAddress
);
1367 if (mos7840_port
->has_led
)
1368 mos7840_led_activity(port
);
1370 /* send it down the pipe */
1371 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1374 mos7840_port
->busy
[i
] = 0;
1375 dev_err_console(port
, "%s - usb_submit_urb(write bulk) failed "
1376 "with status = %d\n", __func__
, status
);
1377 bytes_sent
= status
;
1380 bytes_sent
= transfer_size
;
1381 port
->icount
.tx
+= transfer_size
;
1382 dev_dbg(&port
->dev
, "icount.tx is %d:\n", port
->icount
.tx
);
1388 /*****************************************************************************
1390 * this function is called by the tty driver when it wants to stop the data
1391 * being read from the port.
1392 *****************************************************************************/
1394 static void mos7840_throttle(struct tty_struct
*tty
)
1396 struct usb_serial_port
*port
= tty
->driver_data
;
1397 struct moschip_port
*mos7840_port
;
1400 if (mos7840_port_paranoia_check(port
, __func__
))
1403 mos7840_port
= mos7840_get_port_private(port
);
1405 if (mos7840_port
== NULL
)
1408 if (!mos7840_port
->open
) {
1409 dev_dbg(&port
->dev
, "%s", "port not opened\n");
1413 /* if we are implementing XON/XOFF, send the stop character */
1415 unsigned char stop_char
= STOP_CHAR(tty
);
1416 status
= mos7840_write(tty
, port
, &stop_char
, 1);
1420 /* if we are implementing RTS/CTS, toggle that line */
1421 if (C_CRTSCTS(tty
)) {
1422 mos7840_port
->shadowMCR
&= ~MCR_RTS
;
1423 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1424 mos7840_port
->shadowMCR
);
1430 /*****************************************************************************
1431 * mos7840_unthrottle
1432 * this function is called by the tty driver when it wants to resume
1433 * the data being read from the port (called after mos7840_throttle is
1435 *****************************************************************************/
1436 static void mos7840_unthrottle(struct tty_struct
*tty
)
1438 struct usb_serial_port
*port
= tty
->driver_data
;
1440 struct moschip_port
*mos7840_port
= mos7840_get_port_private(port
);
1442 if (mos7840_port_paranoia_check(port
, __func__
))
1445 if (mos7840_port
== NULL
)
1448 if (!mos7840_port
->open
) {
1449 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1453 /* if we are implementing XON/XOFF, send the start character */
1455 unsigned char start_char
= START_CHAR(tty
);
1456 status
= mos7840_write(tty
, port
, &start_char
, 1);
1461 /* if we are implementing RTS/CTS, toggle that line */
1462 if (C_CRTSCTS(tty
)) {
1463 mos7840_port
->shadowMCR
|= MCR_RTS
;
1464 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1465 mos7840_port
->shadowMCR
);
1471 static int mos7840_tiocmget(struct tty_struct
*tty
)
1473 struct usb_serial_port
*port
= tty
->driver_data
;
1474 struct moschip_port
*mos7840_port
;
1475 unsigned int result
;
1479 mos7840_port
= mos7840_get_port_private(port
);
1481 if (mos7840_port
== NULL
)
1484 status
= mos7840_get_uart_reg(port
, MODEM_STATUS_REGISTER
, &msr
);
1487 status
= mos7840_get_uart_reg(port
, MODEM_CONTROL_REGISTER
, &mcr
);
1490 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0)
1491 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0)
1492 | ((mcr
& MCR_LOOPBACK
) ? TIOCM_LOOP
: 0)
1493 | ((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0)
1494 | ((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0)
1495 | ((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0)
1496 | ((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0);
1498 dev_dbg(&port
->dev
, "%s - 0x%04X\n", __func__
, result
);
1503 static int mos7840_tiocmset(struct tty_struct
*tty
,
1504 unsigned int set
, unsigned int clear
)
1506 struct usb_serial_port
*port
= tty
->driver_data
;
1507 struct moschip_port
*mos7840_port
;
1511 mos7840_port
= mos7840_get_port_private(port
);
1513 if (mos7840_port
== NULL
)
1516 /* FIXME: What locks the port registers ? */
1517 mcr
= mos7840_port
->shadowMCR
;
1518 if (clear
& TIOCM_RTS
)
1520 if (clear
& TIOCM_DTR
)
1522 if (clear
& TIOCM_LOOP
)
1523 mcr
&= ~MCR_LOOPBACK
;
1525 if (set
& TIOCM_RTS
)
1527 if (set
& TIOCM_DTR
)
1529 if (set
& TIOCM_LOOP
)
1530 mcr
|= MCR_LOOPBACK
;
1532 mos7840_port
->shadowMCR
= mcr
;
1534 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, mcr
);
1536 dev_dbg(&port
->dev
, "setting MODEM_CONTROL_REGISTER Failed\n");
1543 /*****************************************************************************
1544 * mos7840_calc_baud_rate_divisor
1545 * this function calculates the proper baud rate divisor for the specified
1547 *****************************************************************************/
1548 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port
*port
,
1549 int baudRate
, int *divisor
,
1552 dev_dbg(&port
->dev
, "%s - %d\n", __func__
, baudRate
);
1554 if (baudRate
<= 115200) {
1555 *divisor
= 115200 / baudRate
;
1558 if ((baudRate
> 115200) && (baudRate
<= 230400)) {
1559 *divisor
= 230400 / baudRate
;
1560 *clk_sel_val
= 0x10;
1561 } else if ((baudRate
> 230400) && (baudRate
<= 403200)) {
1562 *divisor
= 403200 / baudRate
;
1563 *clk_sel_val
= 0x20;
1564 } else if ((baudRate
> 403200) && (baudRate
<= 460800)) {
1565 *divisor
= 460800 / baudRate
;
1566 *clk_sel_val
= 0x30;
1567 } else if ((baudRate
> 460800) && (baudRate
<= 806400)) {
1568 *divisor
= 806400 / baudRate
;
1569 *clk_sel_val
= 0x40;
1570 } else if ((baudRate
> 806400) && (baudRate
<= 921600)) {
1571 *divisor
= 921600 / baudRate
;
1572 *clk_sel_val
= 0x50;
1573 } else if ((baudRate
> 921600) && (baudRate
<= 1572864)) {
1574 *divisor
= 1572864 / baudRate
;
1575 *clk_sel_val
= 0x60;
1576 } else if ((baudRate
> 1572864) && (baudRate
<= 3145728)) {
1577 *divisor
= 3145728 / baudRate
;
1578 *clk_sel_val
= 0x70;
1583 /*****************************************************************************
1584 * mos7840_send_cmd_write_baud_rate
1585 * this function sends the proper command to change the baud rate of the
1587 *****************************************************************************/
1589 static int mos7840_send_cmd_write_baud_rate(struct moschip_port
*mos7840_port
,
1595 unsigned char number
;
1597 struct usb_serial_port
*port
;
1599 if (mos7840_port
== NULL
)
1602 port
= mos7840_port
->port
;
1603 if (mos7840_port_paranoia_check(port
, __func__
))
1606 if (mos7840_serial_paranoia_check(port
->serial
, __func__
))
1609 number
= mos7840_port
->port
->port_number
;
1611 dev_dbg(&port
->dev
, "%s - baud = %d\n", __func__
, baudRate
);
1612 /* reset clk_uart_sel in spregOffset */
1613 if (baudRate
> 115200) {
1614 #ifdef HW_flow_control
1615 /* NOTE: need to see the pther register to modify */
1616 /* setting h/w flow control bit to 1 */
1618 mos7840_port
->shadowMCR
= Data
;
1619 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1622 dev_dbg(&port
->dev
, "Writing spreg failed in set_serial_baud\n");
1628 #ifdef HW_flow_control
1629 /* setting h/w flow control bit to 0 */
1631 mos7840_port
->shadowMCR
= Data
;
1632 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1635 dev_dbg(&port
->dev
, "Writing spreg failed in set_serial_baud\n");
1642 if (1) { /* baudRate <= 115200) */
1645 status
= mos7840_calc_baud_rate_divisor(port
, baudRate
, &divisor
,
1647 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
,
1650 dev_dbg(&port
->dev
, "reading spreg failed in set_serial_baud\n");
1653 Data
= (Data
& 0x8f) | clk_sel_val
;
1654 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
,
1657 dev_dbg(&port
->dev
, "Writing spreg failed in set_serial_baud\n");
1660 /* Calculate the Divisor */
1663 dev_err(&port
->dev
, "%s - bad baud rate\n", __func__
);
1666 /* Enable access to divisor latch */
1667 Data
= mos7840_port
->shadowLCR
| SERIAL_LCR_DLAB
;
1668 mos7840_port
->shadowLCR
= Data
;
1669 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1671 /* Write the divisor */
1672 Data
= (unsigned char)(divisor
& 0xff);
1673 dev_dbg(&port
->dev
, "set_serial_baud Value to write DLL is %x\n", Data
);
1674 mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
1676 Data
= (unsigned char)((divisor
& 0xff00) >> 8);
1677 dev_dbg(&port
->dev
, "set_serial_baud Value to write DLM is %x\n", Data
);
1678 mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
1680 /* Disable access to divisor latch */
1681 Data
= mos7840_port
->shadowLCR
& ~SERIAL_LCR_DLAB
;
1682 mos7840_port
->shadowLCR
= Data
;
1683 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1689 /*****************************************************************************
1690 * mos7840_change_port_settings
1691 * This routine is called to set the UART on the device to match
1692 * the specified new settings.
1693 *****************************************************************************/
1695 static void mos7840_change_port_settings(struct tty_struct
*tty
,
1696 struct moschip_port
*mos7840_port
, struct ktermios
*old_termios
)
1706 struct usb_serial_port
*port
;
1707 struct usb_serial
*serial
;
1709 if (mos7840_port
== NULL
)
1712 port
= mos7840_port
->port
;
1714 if (mos7840_port_paranoia_check(port
, __func__
))
1717 if (mos7840_serial_paranoia_check(port
->serial
, __func__
))
1720 serial
= port
->serial
;
1722 if (!mos7840_port
->open
) {
1723 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1729 lParity
= LCR_PAR_NONE
;
1731 cflag
= tty
->termios
.c_cflag
;
1732 iflag
= tty
->termios
.c_iflag
;
1734 /* Change the number of bits */
1735 switch (cflag
& CSIZE
) {
1754 /* Change the Parity bit */
1755 if (cflag
& PARENB
) {
1756 if (cflag
& PARODD
) {
1757 lParity
= LCR_PAR_ODD
;
1758 dev_dbg(&port
->dev
, "%s - parity = odd\n", __func__
);
1760 lParity
= LCR_PAR_EVEN
;
1761 dev_dbg(&port
->dev
, "%s - parity = even\n", __func__
);
1765 dev_dbg(&port
->dev
, "%s - parity = none\n", __func__
);
1769 lParity
= lParity
| 0x20;
1771 /* Change the Stop bit */
1772 if (cflag
& CSTOPB
) {
1774 dev_dbg(&port
->dev
, "%s - stop bits = 2\n", __func__
);
1777 dev_dbg(&port
->dev
, "%s - stop bits = 1\n", __func__
);
1780 /* Update the LCR with the correct value */
1781 mos7840_port
->shadowLCR
&=
1782 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
1783 mos7840_port
->shadowLCR
|= (lData
| lParity
| lStop
);
1785 dev_dbg(&port
->dev
, "%s - mos7840_port->shadowLCR is %x\n", __func__
,
1786 mos7840_port
->shadowLCR
);
1787 /* Disable Interrupts */
1789 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1792 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1795 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1797 /* Send the updated LCR value to the mos7840 */
1798 Data
= mos7840_port
->shadowLCR
;
1800 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1803 mos7840_port
->shadowMCR
= Data
;
1804 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1806 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1808 /* set up the MCR register and send it to the mos7840 */
1810 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
1812 mos7840_port
->shadowMCR
|= (MCR_DTR
| MCR_RTS
);
1814 if (cflag
& CRTSCTS
)
1815 mos7840_port
->shadowMCR
|= (MCR_XON_ANY
);
1817 mos7840_port
->shadowMCR
&= ~(MCR_XON_ANY
);
1819 Data
= mos7840_port
->shadowMCR
;
1820 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1822 /* Determine divisor based on baud rate */
1823 baud
= tty_get_baud_rate(tty
);
1826 /* pick a default, any default... */
1827 dev_dbg(&port
->dev
, "%s", "Picked default baud...\n");
1831 dev_dbg(&port
->dev
, "%s - baud rate = %d\n", __func__
, baud
);
1832 status
= mos7840_send_cmd_write_baud_rate(mos7840_port
, baud
);
1834 /* Enable Interrupts */
1836 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1838 if (!mos7840_port
->read_urb_busy
) {
1839 mos7840_port
->read_urb_busy
= true;
1840 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1842 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n",
1844 mos7840_port
->read_urb_busy
= false;
1847 dev_dbg(&port
->dev
, "%s - mos7840_port->shadowLCR is End %x\n", __func__
,
1848 mos7840_port
->shadowLCR
);
1851 /*****************************************************************************
1852 * mos7840_set_termios
1853 * this function is called by the tty driver when it wants to change
1854 * the termios structure
1855 *****************************************************************************/
1857 static void mos7840_set_termios(struct tty_struct
*tty
,
1858 struct usb_serial_port
*port
,
1859 struct ktermios
*old_termios
)
1862 struct usb_serial
*serial
;
1863 struct moschip_port
*mos7840_port
;
1865 if (mos7840_port_paranoia_check(port
, __func__
))
1868 serial
= port
->serial
;
1870 if (mos7840_serial_paranoia_check(serial
, __func__
))
1873 mos7840_port
= mos7840_get_port_private(port
);
1875 if (mos7840_port
== NULL
)
1878 if (!mos7840_port
->open
) {
1879 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1883 /* change the port settings to the new ones specified */
1885 mos7840_change_port_settings(tty
, mos7840_port
, old_termios
);
1887 if (!mos7840_port
->read_urb
) {
1888 dev_dbg(&port
->dev
, "%s", "URB KILLED !!!!!\n");
1892 if (!mos7840_port
->read_urb_busy
) {
1893 mos7840_port
->read_urb_busy
= true;
1894 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1896 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n",
1898 mos7840_port
->read_urb_busy
= false;
1903 /*****************************************************************************
1904 * mos7840_get_lsr_info - get line status register info
1906 * Purpose: Let user call ioctl() to get info when the UART physically
1907 * is emptied. On bus types like RS485, the transmitter must
1908 * release the bus after transmitting. This must be done when
1909 * the transmit shift register is empty, not be done when the
1910 * transmit holding register is empty. This functionality
1911 * allows an RS485 driver to be written in user space.
1912 *****************************************************************************/
1914 static int mos7840_get_lsr_info(struct tty_struct
*tty
,
1915 unsigned int __user
*value
)
1918 unsigned int result
= 0;
1920 count
= mos7840_chars_in_buffer(tty
);
1922 result
= TIOCSER_TEMT
;
1924 if (copy_to_user(value
, &result
, sizeof(int)))
1929 /*****************************************************************************
1930 * mos7840_get_serial_info
1931 * function to get information about serial port
1932 *****************************************************************************/
1934 static int mos7840_get_serial_info(struct moschip_port
*mos7840_port
,
1935 struct serial_struct __user
*retinfo
)
1937 struct serial_struct tmp
;
1939 if (mos7840_port
== NULL
)
1942 memset(&tmp
, 0, sizeof(tmp
));
1944 tmp
.type
= PORT_16550A
;
1945 tmp
.line
= mos7840_port
->port
->minor
;
1946 tmp
.port
= mos7840_port
->port
->port_number
;
1948 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
1949 tmp
.baud_base
= 9600;
1950 tmp
.close_delay
= 5 * HZ
;
1951 tmp
.closing_wait
= 30 * HZ
;
1953 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
1958 /*****************************************************************************
1960 * this function handles any ioctl calls to the driver
1961 *****************************************************************************/
1963 static int mos7840_ioctl(struct tty_struct
*tty
,
1964 unsigned int cmd
, unsigned long arg
)
1966 struct usb_serial_port
*port
= tty
->driver_data
;
1967 void __user
*argp
= (void __user
*)arg
;
1968 struct moschip_port
*mos7840_port
;
1970 if (mos7840_port_paranoia_check(port
, __func__
))
1973 mos7840_port
= mos7840_get_port_private(port
);
1975 if (mos7840_port
== NULL
)
1979 /* return number of bytes available */
1982 dev_dbg(&port
->dev
, "%s TIOCSERGETLSR\n", __func__
);
1983 return mos7840_get_lsr_info(tty
, argp
);
1986 dev_dbg(&port
->dev
, "%s TIOCGSERIAL\n", __func__
);
1987 return mos7840_get_serial_info(mos7840_port
, argp
);
1990 dev_dbg(&port
->dev
, "%s TIOCSSERIAL\n", __func__
);
1995 return -ENOIOCTLCMD
;
1998 static int mos7810_check(struct usb_serial
*serial
)
2000 int i
, pass_count
= 0;
2002 __u16 data
= 0, mcr_data
= 0;
2003 __u16 test_pattern
= 0x55AA;
2006 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
2008 return 0; /* failed to identify 7810 */
2010 /* Store MCR setting */
2011 res
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2012 MCS_RDREQ
, MCS_RD_RTYPE
, 0x0300, MODEM_CONTROL_REGISTER
,
2013 buf
, VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2014 if (res
== VENDOR_READ_LENGTH
)
2017 for (i
= 0; i
< 16; i
++) {
2018 /* Send the 1-bit test pattern out to MCS7810 test pin */
2019 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2020 MCS_WRREQ
, MCS_WR_RTYPE
,
2021 (0x0300 | (((test_pattern
>> i
) & 0x0001) << 1)),
2022 MODEM_CONTROL_REGISTER
, NULL
, 0, MOS_WDR_TIMEOUT
);
2024 /* Read the test pattern back */
2025 res
= usb_control_msg(serial
->dev
,
2026 usb_rcvctrlpipe(serial
->dev
, 0), MCS_RDREQ
,
2027 MCS_RD_RTYPE
, 0, GPIO_REGISTER
, buf
,
2028 VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2029 if (res
== VENDOR_READ_LENGTH
)
2032 /* If this is a MCS7810 device, both test patterns must match */
2033 if (((test_pattern
>> i
) ^ (~data
>> 1)) & 0x0001)
2039 /* Restore MCR setting */
2040 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0), MCS_WRREQ
,
2041 MCS_WR_RTYPE
, 0x0300 | mcr_data
, MODEM_CONTROL_REGISTER
, NULL
,
2042 0, MOS_WDR_TIMEOUT
);
2046 if (pass_count
== 16)
2052 static int mos7840_probe(struct usb_serial
*serial
,
2053 const struct usb_device_id
*id
)
2055 u16 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
2059 if (product
== MOSCHIP_DEVICE_ID_7810
||
2060 product
== MOSCHIP_DEVICE_ID_7820
) {
2061 device_type
= product
;
2065 buf
= kzalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
2069 usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2070 MCS_RDREQ
, MCS_RD_RTYPE
, 0, GPIO_REGISTER
, buf
,
2071 VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2073 /* For a MCS7840 device GPIO0 must be set to 1 */
2075 device_type
= MOSCHIP_DEVICE_ID_7840
;
2076 else if (mos7810_check(serial
))
2077 device_type
= MOSCHIP_DEVICE_ID_7810
;
2079 device_type
= MOSCHIP_DEVICE_ID_7820
;
2083 usb_set_serial_data(serial
, (void *)(unsigned long)device_type
);
2088 static int mos7840_calc_num_ports(struct usb_serial
*serial
,
2089 struct usb_serial_endpoints
*epds
)
2091 int device_type
= (unsigned long)usb_get_serial_data(serial
);
2094 num_ports
= (device_type
>> 4) & 0x000F;
2097 * num_ports is currently never zero as device_type is one of
2098 * MOSCHIP_DEVICE_ID_78{1,2,4}0.
2103 if (epds
->num_bulk_in
< num_ports
|| epds
->num_bulk_out
< num_ports
) {
2104 dev_err(&serial
->interface
->dev
, "missing endpoints\n");
2111 static int mos7840_port_probe(struct usb_serial_port
*port
)
2113 struct usb_serial
*serial
= port
->serial
;
2114 int device_type
= (unsigned long)usb_get_serial_data(serial
);
2115 struct moschip_port
*mos7840_port
;
2120 /* we set up the pointers to the endpoints in the mos7840_open *
2121 * function, as the structures aren't created yet. */
2123 pnum
= port
->port_number
;
2125 dev_dbg(&port
->dev
, "mos7840_startup: configuring port %d\n", pnum
);
2126 mos7840_port
= kzalloc(sizeof(struct moschip_port
), GFP_KERNEL
);
2130 /* Initialize all port interrupt end point to port 0 int
2131 * endpoint. Our device has only one interrupt end point
2132 * common to all port */
2134 mos7840_port
->port
= port
;
2135 mos7840_set_port_private(port
, mos7840_port
);
2136 spin_lock_init(&mos7840_port
->pool_lock
);
2138 /* minor is not initialised until later by
2139 * usb-serial.c:get_free_serial() and cannot therefore be used
2140 * to index device instances */
2141 mos7840_port
->port_num
= pnum
+ 1;
2142 dev_dbg(&port
->dev
, "port->minor = %d\n", port
->minor
);
2143 dev_dbg(&port
->dev
, "mos7840_port->port_num = %d\n", mos7840_port
->port_num
);
2145 if (mos7840_port
->port_num
== 1) {
2146 mos7840_port
->SpRegOffset
= 0x0;
2147 mos7840_port
->ControlRegOffset
= 0x1;
2148 mos7840_port
->DcrRegOffset
= 0x4;
2149 } else if ((mos7840_port
->port_num
== 2) && (serial
->num_ports
== 4)) {
2150 mos7840_port
->SpRegOffset
= 0x8;
2151 mos7840_port
->ControlRegOffset
= 0x9;
2152 mos7840_port
->DcrRegOffset
= 0x16;
2153 } else if ((mos7840_port
->port_num
== 2) && (serial
->num_ports
== 2)) {
2154 mos7840_port
->SpRegOffset
= 0xa;
2155 mos7840_port
->ControlRegOffset
= 0xb;
2156 mos7840_port
->DcrRegOffset
= 0x19;
2157 } else if ((mos7840_port
->port_num
== 3) && (serial
->num_ports
== 4)) {
2158 mos7840_port
->SpRegOffset
= 0xa;
2159 mos7840_port
->ControlRegOffset
= 0xb;
2160 mos7840_port
->DcrRegOffset
= 0x19;
2161 } else if ((mos7840_port
->port_num
== 4) && (serial
->num_ports
== 4)) {
2162 mos7840_port
->SpRegOffset
= 0xc;
2163 mos7840_port
->ControlRegOffset
= 0xd;
2164 mos7840_port
->DcrRegOffset
= 0x1c;
2166 mos7840_dump_serial_port(port
, mos7840_port
);
2167 mos7840_set_port_private(port
, mos7840_port
);
2169 /* enable rx_disable bit in control register */
2170 status
= mos7840_get_reg_sync(port
,
2171 mos7840_port
->ControlRegOffset
, &Data
);
2173 dev_dbg(&port
->dev
, "Reading ControlReg failed status-0x%x\n", status
);
2176 dev_dbg(&port
->dev
, "ControlReg Reading success val is %x, status%d\n", Data
, status
);
2177 Data
|= 0x08; /* setting driver done bit */
2178 Data
|= 0x04; /* sp1_bit to have cts change reflect in
2181 /* Data |= 0x20; //rx_disable bit */
2182 status
= mos7840_set_reg_sync(port
,
2183 mos7840_port
->ControlRegOffset
, Data
);
2185 dev_dbg(&port
->dev
, "Writing ControlReg failed(rx_disable) status-0x%x\n", status
);
2188 dev_dbg(&port
->dev
, "ControlReg Writing success(rx_disable) status%d\n", status
);
2190 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2193 status
= mos7840_set_reg_sync(port
,
2194 (__u16
) (mos7840_port
->DcrRegOffset
+ 0), Data
);
2196 dev_dbg(&port
->dev
, "Writing DCR0 failed status-0x%x\n", status
);
2199 dev_dbg(&port
->dev
, "DCR0 Writing success status%d\n", status
);
2202 status
= mos7840_set_reg_sync(port
,
2203 (__u16
) (mos7840_port
->DcrRegOffset
+ 1), Data
);
2205 dev_dbg(&port
->dev
, "Writing DCR1 failed status-0x%x\n", status
);
2208 dev_dbg(&port
->dev
, "DCR1 Writing success status%d\n", status
);
2211 status
= mos7840_set_reg_sync(port
,
2212 (__u16
) (mos7840_port
->DcrRegOffset
+ 2), Data
);
2214 dev_dbg(&port
->dev
, "Writing DCR2 failed status-0x%x\n", status
);
2217 dev_dbg(&port
->dev
, "DCR2 Writing success status%d\n", status
);
2219 /* write values in clkstart0x0 and clkmulti 0x20 */
2221 status
= mos7840_set_reg_sync(port
, CLK_START_VALUE_REGISTER
, Data
);
2223 dev_dbg(&port
->dev
, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status
);
2226 dev_dbg(&port
->dev
, "CLK_START_VALUE_REGISTER Writing success status%d\n", status
);
2229 status
= mos7840_set_reg_sync(port
, CLK_MULTI_REGISTER
, Data
);
2231 dev_dbg(&port
->dev
, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status
);
2234 dev_dbg(&port
->dev
, "CLK_MULTI_REGISTER Writing success status%d\n", status
);
2236 /* write value 0x0 to scratchpad register */
2238 status
= mos7840_set_uart_reg(port
, SCRATCH_PAD_REGISTER
, Data
);
2240 dev_dbg(&port
->dev
, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status
);
2243 dev_dbg(&port
->dev
, "SCRATCH_PAD_REGISTER Writing success status%d\n", status
);
2245 /* Zero Length flag register */
2246 if ((mos7840_port
->port_num
!= 1) && (serial
->num_ports
== 2)) {
2248 status
= mos7840_set_reg_sync(port
,
2250 ((__u16
)mos7840_port
->port_num
)), Data
);
2251 dev_dbg(&port
->dev
, "ZLIP offset %x\n",
2252 (__u16
)(ZLP_REG1
+ ((__u16
) mos7840_port
->port_num
)));
2254 dev_dbg(&port
->dev
, "Writing ZLP_REG%d failed status-0x%x\n", pnum
+ 2, status
);
2257 dev_dbg(&port
->dev
, "ZLP_REG%d Writing success status%d\n", pnum
+ 2, status
);
2260 status
= mos7840_set_reg_sync(port
,
2262 ((__u16
)mos7840_port
->port_num
) - 0x1), Data
);
2263 dev_dbg(&port
->dev
, "ZLIP offset %x\n",
2264 (__u16
)(ZLP_REG1
+ ((__u16
) mos7840_port
->port_num
) - 0x1));
2266 dev_dbg(&port
->dev
, "Writing ZLP_REG%d failed status-0x%x\n", pnum
+ 1, status
);
2269 dev_dbg(&port
->dev
, "ZLP_REG%d Writing success status%d\n", pnum
+ 1, status
);
2272 mos7840_port
->control_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2273 mos7840_port
->ctrl_buf
= kmalloc(16, GFP_KERNEL
);
2274 mos7840_port
->dr
= kmalloc(sizeof(struct usb_ctrlrequest
),
2276 if (!mos7840_port
->control_urb
|| !mos7840_port
->ctrl_buf
||
2277 !mos7840_port
->dr
) {
2282 mos7840_port
->has_led
= false;
2284 /* Initialize LED timers */
2285 if (device_type
== MOSCHIP_DEVICE_ID_7810
) {
2286 mos7840_port
->has_led
= true;
2288 mos7840_port
->led_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2289 mos7840_port
->led_dr
= kmalloc(sizeof(*mos7840_port
->led_dr
),
2291 if (!mos7840_port
->led_urb
|| !mos7840_port
->led_dr
) {
2296 timer_setup(&mos7840_port
->led_timer1
, mos7840_led_off
, 0);
2297 mos7840_port
->led_timer1
.expires
=
2298 jiffies
+ msecs_to_jiffies(LED_ON_MS
);
2299 timer_setup(&mos7840_port
->led_timer2
, mos7840_led_flag_off
,
2301 mos7840_port
->led_timer2
.expires
=
2302 jiffies
+ msecs_to_jiffies(LED_OFF_MS
);
2305 mos7840_set_led_sync(port
, MODEM_CONTROL_REGISTER
, 0x0300);
2308 if (pnum
== serial
->num_ports
- 1) {
2309 /* Zero Length flag enable */
2311 status
= mos7840_set_reg_sync(serial
->port
[0], ZLP_REG5
, Data
);
2313 dev_dbg(&port
->dev
, "Writing ZLP_REG5 failed status-0x%x\n", status
);
2316 dev_dbg(&port
->dev
, "ZLP_REG5 Writing success status%d\n", status
);
2318 /* setting configuration feature to one */
2319 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2320 0x03, 0x00, 0x01, 0x00, NULL
, 0x00,
2325 kfree(mos7840_port
->led_dr
);
2326 usb_free_urb(mos7840_port
->led_urb
);
2327 kfree(mos7840_port
->dr
);
2328 kfree(mos7840_port
->ctrl_buf
);
2329 usb_free_urb(mos7840_port
->control_urb
);
2330 kfree(mos7840_port
);
2335 static int mos7840_port_remove(struct usb_serial_port
*port
)
2337 struct moschip_port
*mos7840_port
;
2339 mos7840_port
= mos7840_get_port_private(port
);
2341 if (mos7840_port
->has_led
) {
2343 mos7840_set_led_sync(port
, MODEM_CONTROL_REGISTER
, 0x0300);
2345 del_timer_sync(&mos7840_port
->led_timer1
);
2346 del_timer_sync(&mos7840_port
->led_timer2
);
2348 usb_kill_urb(mos7840_port
->led_urb
);
2349 usb_free_urb(mos7840_port
->led_urb
);
2350 kfree(mos7840_port
->led_dr
);
2352 usb_kill_urb(mos7840_port
->control_urb
);
2353 usb_free_urb(mos7840_port
->control_urb
);
2354 kfree(mos7840_port
->ctrl_buf
);
2355 kfree(mos7840_port
->dr
);
2356 kfree(mos7840_port
);
2361 static struct usb_serial_driver moschip7840_4port_device
= {
2363 .owner
= THIS_MODULE
,
2366 .description
= DRIVER_DESC
,
2367 .id_table
= id_table
,
2368 .num_interrupt_in
= 1,
2369 .open
= mos7840_open
,
2370 .close
= mos7840_close
,
2371 .write
= mos7840_write
,
2372 .write_room
= mos7840_write_room
,
2373 .chars_in_buffer
= mos7840_chars_in_buffer
,
2374 .throttle
= mos7840_throttle
,
2375 .unthrottle
= mos7840_unthrottle
,
2376 .calc_num_ports
= mos7840_calc_num_ports
,
2377 .probe
= mos7840_probe
,
2378 .ioctl
= mos7840_ioctl
,
2379 .set_termios
= mos7840_set_termios
,
2380 .break_ctl
= mos7840_break
,
2381 .tiocmget
= mos7840_tiocmget
,
2382 .tiocmset
= mos7840_tiocmset
,
2383 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
2384 .get_icount
= usb_serial_generic_get_icount
,
2385 .port_probe
= mos7840_port_probe
,
2386 .port_remove
= mos7840_port_remove
,
2387 .read_bulk_callback
= mos7840_bulk_in_callback
,
2388 .read_int_callback
= mos7840_interrupt_callback
,
2391 static struct usb_serial_driver
* const serial_drivers
[] = {
2392 &moschip7840_4port_device
, NULL
2395 module_usb_serial_driver(serial_drivers
, id_table
);
2397 MODULE_DESCRIPTION(DRIVER_DESC
);
2398 MODULE_LICENSE("GPL");