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.
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.
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
16 * Clean ups from Moschip version and a few ioctl implementations by:
17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
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>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/tty.h>
30 #include <linux/tty_driver.h>
31 #include <linux/tty_flip.h>
32 #include <linux/module.h>
33 #include <linux/serial.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
36 #include <linux/uaccess.h>
41 #define DRIVER_VERSION "1.3.2"
42 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
45 * 16C50 UART register defines
48 #define LCR_BITS_5 0x00 /* 5 bits/char */
49 #define LCR_BITS_6 0x01 /* 6 bits/char */
50 #define LCR_BITS_7 0x02 /* 7 bits/char */
51 #define LCR_BITS_8 0x03 /* 8 bits/char */
52 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
54 #define LCR_STOP_1 0x00 /* 1 stop bit */
55 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
56 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
57 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
59 #define LCR_PAR_NONE 0x00 /* No parity */
60 #define LCR_PAR_ODD 0x08 /* Odd parity */
61 #define LCR_PAR_EVEN 0x18 /* Even parity */
62 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
63 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
64 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
66 #define LCR_SET_BREAK 0x40 /* Set Break condition */
67 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
69 #define MCR_DTR 0x01 /* Assert DTR */
70 #define MCR_RTS 0x02 /* Assert RTS */
71 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
72 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
73 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
74 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
76 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
77 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
78 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
79 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
82 * Defines used for sending commands to port
85 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
87 #define MOS_PORT1 0x0200
88 #define MOS_PORT2 0x0300
89 #define MOS_VENREG 0x0000
90 #define MOS_MAX_PORT 0x02
91 #define MOS_WRITE 0x0E
95 #define MCS_RD_RTYPE 0xC0
96 #define MCS_WR_RTYPE 0x40
97 #define MCS_RDREQ 0x0D
98 #define MCS_WRREQ 0x0E
99 #define MCS_CTRL_TIMEOUT 500
100 #define VENDOR_READ_LENGTH (0x01)
102 #define MAX_NAME_LEN 64
104 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
105 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
107 /* For higher baud Rates use TIOCEXBAUD */
108 #define TIOCEXBAUD 0x5462
110 /* vendor id and device id defines */
112 /* The native mos7840/7820 component */
113 #define USB_VENDOR_ID_MOSCHIP 0x9710
114 #define MOSCHIP_DEVICE_ID_7840 0x7840
115 #define MOSCHIP_DEVICE_ID_7820 0x7820
116 #define MOSCHIP_DEVICE_ID_7810 0x7810
117 /* The native component can have its vendor/device id's overridden
118 * in vendor-specific implementations. Such devices can be handled
119 * by making a change here, in id_table.
121 #define USB_VENDOR_ID_BANDB 0x0856
122 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
123 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
124 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
125 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
126 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
127 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
128 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
129 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
130 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
131 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
132 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
133 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
134 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
136 /* This driver also supports
137 * ATEN UC2324 device using Moschip MCS7840
138 * ATEN UC2322 device using Moschip MCS7820
140 #define USB_VENDOR_ID_ATENINTL 0x0557
141 #define ATENINTL_DEVICE_ID_UC2324 0x2011
142 #define ATENINTL_DEVICE_ID_UC2322 0x7820
144 /* Interrupt Routine Defines */
146 #define SERIAL_IIR_RLS 0x06
147 #define SERIAL_IIR_MS 0x00
150 * Emulation of the bit mask on the LINE STATUS REGISTER.
152 #define SERIAL_LSR_DR 0x0001
153 #define SERIAL_LSR_OE 0x0002
154 #define SERIAL_LSR_PE 0x0004
155 #define SERIAL_LSR_FE 0x0008
156 #define SERIAL_LSR_BI 0x0010
158 #define MOS_MSR_DELTA_CTS 0x10
159 #define MOS_MSR_DELTA_DSR 0x20
160 #define MOS_MSR_DELTA_RI 0x40
161 #define MOS_MSR_DELTA_CD 0x80
163 /* Serial Port register Address */
164 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
165 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
166 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
167 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
168 #define LINE_STATUS_REGISTER ((__u16)(0x05))
169 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
170 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
171 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
172 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
174 #define CLK_MULTI_REGISTER ((__u16)(0x02))
175 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
176 #define GPIO_REGISTER ((__u16)(0x07))
178 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
181 * URB POOL related defines
183 #define NUM_URBS 16 /* URB Count */
184 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
186 /* LED on/off milliseconds*/
187 #define LED_ON_MS 500
188 #define LED_OFF_MS 500
190 static int device_type
;
192 static const struct usb_device_id id_table
[] = {
193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
194 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
195 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7810
)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_2
)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_2P
)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_4
)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_4P
)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_US9ML2_2
)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_US9ML2_4
)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USPTL4_2
)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USPTL4_4
)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2P
)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
207 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4P
)},
208 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL2_4
)},
209 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2324
)},
210 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2322
)},
211 {} /* terminating entry */
213 MODULE_DEVICE_TABLE(usb
, id_table
);
215 /* This structure holds all of the local port information */
217 struct moschip_port
{
218 int port_num
; /*Actual port number in the device(1,2,etc) */
219 struct urb
*write_urb
; /* write URB for this port */
220 struct urb
*read_urb
; /* read URB for this port */
221 __u8 shadowLCR
; /* last LCR value received */
222 __u8 shadowMCR
; /* last MCR value received */
225 wait_queue_head_t wait_chase
; /* for handling sleeping while waiting for chase to finish */
226 wait_queue_head_t delta_msr_wait
; /* for handling sleeping while waiting for msr change to happen */
228 struct async_icount icount
;
229 struct usb_serial_port
*port
; /* loop back to the owner of this object */
233 __u8 ControlRegOffset
;
235 /* for processing control URBS in interrupt context */
236 struct urb
*control_urb
;
237 struct usb_ctrlrequest
*dr
;
241 spinlock_t pool_lock
;
242 struct urb
*write_urb_pool
[NUM_URBS
];
246 /* For device(s) with LED indicator */
249 struct timer_list led_timer1
; /* Timer for LED on */
250 struct timer_list led_timer2
; /* Timer for LED off */
256 * mos7840_set_reg_sync
257 * To set the Control register by calling usb_fill_control_urb function
258 * by passing usb_sndctrlpipe function as parameter.
261 static int mos7840_set_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
264 struct usb_device
*dev
= port
->serial
->dev
;
266 dbg("mos7840_set_reg_sync offset is %x, value %x", reg
, val
);
268 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
269 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
274 * mos7840_get_reg_sync
275 * To set the Uart register by calling usb_fill_control_urb function by
276 * passing usb_rcvctrlpipe function as parameter.
279 static int mos7840_get_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
282 struct usb_device
*dev
= port
->serial
->dev
;
286 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
290 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
291 MCS_RD_RTYPE
, 0, reg
, buf
, VENDOR_READ_LENGTH
,
294 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg
, *val
);
301 * mos7840_set_uart_reg
302 * To set the Uart register by calling usb_fill_control_urb function by
303 * passing usb_sndctrlpipe function as parameter.
306 static int mos7840_set_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
310 struct usb_device
*dev
= port
->serial
->dev
;
312 /* For the UART control registers, the application number need
314 if (port
->serial
->num_ports
== 4) {
315 val
|= (((__u16
) port
->number
-
316 (__u16
) (port
->serial
->minor
)) + 1) << 8;
317 dbg("mos7840_set_uart_reg application number is %x", val
);
319 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
320 val
|= (((__u16
) port
->number
-
321 (__u16
) (port
->serial
->minor
)) + 1) << 8;
322 dbg("mos7840_set_uart_reg application number is %x",
326 (((__u16
) port
->number
-
327 (__u16
) (port
->serial
->minor
)) + 2) << 8;
328 dbg("mos7840_set_uart_reg application number is %x",
332 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
333 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
339 * mos7840_get_uart_reg
340 * To set the Control register by calling usb_fill_control_urb function
341 * by passing usb_rcvctrlpipe function as parameter.
343 static int mos7840_get_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
346 struct usb_device
*dev
= port
->serial
->dev
;
351 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
355 /* dbg("application number is %4x",
356 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
357 /* Wval is same as application number */
358 if (port
->serial
->num_ports
== 4) {
360 (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) +
362 dbg("mos7840_get_uart_reg application number is %x", Wval
);
364 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
365 Wval
= (((__u16
) port
->number
-
366 (__u16
) (port
->serial
->minor
)) + 1) << 8;
367 dbg("mos7840_get_uart_reg application number is %x",
370 Wval
= (((__u16
) port
->number
-
371 (__u16
) (port
->serial
->minor
)) + 2) << 8;
372 dbg("mos7840_get_uart_reg application number is %x",
376 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
377 MCS_RD_RTYPE
, Wval
, reg
, buf
, VENDOR_READ_LENGTH
,
385 static void mos7840_dump_serial_port(struct moschip_port
*mos7840_port
)
388 dbg("***************************************");
389 dbg("SpRegOffset is %2x", mos7840_port
->SpRegOffset
);
390 dbg("ControlRegOffset is %2x", mos7840_port
->ControlRegOffset
);
391 dbg("DCRRegOffset is %2x", mos7840_port
->DcrRegOffset
);
392 dbg("***************************************");
396 /************************************************************************/
397 /************************************************************************/
398 /* I N T E R F A C E F U N C T I O N S */
399 /* I N T E R F A C E F U N C T I O N S */
400 /************************************************************************/
401 /************************************************************************/
403 static inline void mos7840_set_port_private(struct usb_serial_port
*port
,
404 struct moschip_port
*data
)
406 usb_set_serial_port_data(port
, (void *)data
);
409 static inline struct moschip_port
*mos7840_get_port_private(struct
413 return (struct moschip_port
*)usb_get_serial_port_data(port
);
416 static void mos7840_handle_new_msr(struct moschip_port
*port
, __u8 new_msr
)
418 struct moschip_port
*mos7840_port
;
419 struct async_icount
*icount
;
421 icount
= &mos7840_port
->icount
;
423 (MOS_MSR_DELTA_CTS
| MOS_MSR_DELTA_DSR
| MOS_MSR_DELTA_RI
|
425 icount
= &mos7840_port
->icount
;
427 /* update input line counters */
428 if (new_msr
& MOS_MSR_DELTA_CTS
) {
432 if (new_msr
& MOS_MSR_DELTA_DSR
) {
436 if (new_msr
& MOS_MSR_DELTA_CD
) {
440 if (new_msr
& MOS_MSR_DELTA_RI
) {
447 static void mos7840_handle_new_lsr(struct moschip_port
*port
, __u8 new_lsr
)
449 struct async_icount
*icount
;
451 dbg("%s - %02x", __func__
, new_lsr
);
453 if (new_lsr
& SERIAL_LSR_BI
) {
455 * Parity and Framing errors only count if they
456 * occur exclusive of a break being
459 new_lsr
&= (__u8
) (SERIAL_LSR_OE
| SERIAL_LSR_BI
);
462 /* update input line counters */
463 icount
= &port
->icount
;
464 if (new_lsr
& SERIAL_LSR_BI
) {
468 if (new_lsr
& SERIAL_LSR_OE
) {
472 if (new_lsr
& SERIAL_LSR_PE
) {
476 if (new_lsr
& SERIAL_LSR_FE
) {
482 /************************************************************************/
483 /************************************************************************/
484 /* U S B C A L L B A C K F U N C T I O N S */
485 /* U S B C A L L B A C K F U N C T I O N S */
486 /************************************************************************/
487 /************************************************************************/
489 static void mos7840_control_callback(struct urb
*urb
)
492 struct moschip_port
*mos7840_port
;
494 int status
= urb
->status
;
496 mos7840_port
= urb
->context
;
505 /* this urb is terminated, clean up */
506 dbg("%s - urb shutting down with status: %d", __func__
,
510 dbg("%s - nonzero urb status received: %d", __func__
,
515 dbg("%s urb buffer size is %d", __func__
, urb
->actual_length
);
516 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__
,
517 mos7840_port
->MsrLsr
, mos7840_port
->port_num
);
518 data
= urb
->transfer_buffer
;
519 regval
= (__u8
) data
[0];
520 dbg("%s data is %x", __func__
, regval
);
521 if (mos7840_port
->MsrLsr
== 0)
522 mos7840_handle_new_msr(mos7840_port
, regval
);
523 else if (mos7840_port
->MsrLsr
== 1)
524 mos7840_handle_new_lsr(mos7840_port
, regval
);
527 static int mos7840_get_reg(struct moschip_port
*mcs
, __u16 Wval
, __u16 reg
,
530 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
531 struct usb_ctrlrequest
*dr
= mcs
->dr
;
532 unsigned char *buffer
= mcs
->ctrl_buf
;
535 dr
->bRequestType
= MCS_RD_RTYPE
;
536 dr
->bRequest
= MCS_RDREQ
;
537 dr
->wValue
= cpu_to_le16(Wval
); /* 0 */
538 dr
->wIndex
= cpu_to_le16(reg
);
539 dr
->wLength
= cpu_to_le16(2);
541 usb_fill_control_urb(mcs
->control_urb
, dev
, usb_rcvctrlpipe(dev
, 0),
542 (unsigned char *)dr
, buffer
, 2,
543 mos7840_control_callback
, mcs
);
544 mcs
->control_urb
->transfer_buffer_length
= 2;
545 ret
= usb_submit_urb(mcs
->control_urb
, GFP_ATOMIC
);
549 static void mos7840_set_led_callback(struct urb
*urb
)
551 switch (urb
->status
) {
558 /* This urb is terminated, clean up */
559 dbg("%s - urb shutting down with status: %d", __func__
,
563 dbg("%s - nonzero urb status received: %d", __func__
,
568 static void mos7840_set_led_async(struct moschip_port
*mcs
, __u16 wval
,
571 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
572 struct usb_ctrlrequest
*dr
= mcs
->dr
;
574 dr
->bRequestType
= MCS_WR_RTYPE
;
575 dr
->bRequest
= MCS_WRREQ
;
576 dr
->wValue
= cpu_to_le16(wval
);
577 dr
->wIndex
= cpu_to_le16(reg
);
578 dr
->wLength
= cpu_to_le16(0);
580 usb_fill_control_urb(mcs
->control_urb
, dev
, usb_sndctrlpipe(dev
, 0),
581 (unsigned char *)dr
, NULL
, 0, mos7840_set_led_callback
, NULL
);
583 usb_submit_urb(mcs
->control_urb
, GFP_ATOMIC
);
586 static void mos7840_set_led_sync(struct usb_serial_port
*port
, __u16 reg
,
589 struct usb_device
*dev
= port
->serial
->dev
;
591 usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
, MCS_WR_RTYPE
,
592 val
, reg
, NULL
, 0, MOS_WDR_TIMEOUT
);
595 static void mos7840_led_off(unsigned long arg
)
597 struct moschip_port
*mcs
= (struct moschip_port
*) arg
;
600 mos7840_set_led_async(mcs
, 0x0300, MODEM_CONTROL_REGISTER
);
601 mod_timer(&mcs
->led_timer2
,
602 jiffies
+ msecs_to_jiffies(LED_OFF_MS
));
605 static void mos7840_led_flag_off(unsigned long arg
)
607 struct moschip_port
*mcs
= (struct moschip_port
*) arg
;
609 mcs
->led_flag
= false;
612 /*****************************************************************************
613 * mos7840_interrupt_callback
614 * this is the callback function for when we have received data on the
615 * interrupt endpoint.
616 *****************************************************************************/
618 static void mos7840_interrupt_callback(struct urb
*urb
)
622 struct moschip_port
*mos7840_port
;
623 struct usb_serial
*serial
;
628 __u16 wval
, wreg
= 0;
629 int status
= urb
->status
;
638 /* this urb is terminated, clean up */
639 dbg("%s - urb shutting down with status: %d", __func__
,
643 dbg("%s - nonzero urb status received: %d", __func__
,
648 length
= urb
->actual_length
;
649 data
= urb
->transfer_buffer
;
651 serial
= urb
->context
;
653 /* Moschip get 5 bytes
654 * Byte 1 IIR Port 1 (port.number is 0)
655 * Byte 2 IIR Port 2 (port.number is 1)
656 * Byte 3 IIR Port 3 (port.number is 2)
657 * Byte 4 IIR Port 4 (port.number is 3)
658 * Byte 5 FIFO status for both */
660 if (length
&& length
> 5) {
661 dbg("%s", "Wrong data !!!");
665 sp
[0] = (__u8
) data
[0];
666 sp
[1] = (__u8
) data
[1];
667 sp
[2] = (__u8
) data
[2];
668 sp
[3] = (__u8
) data
[3];
671 for (i
= 0; i
< serial
->num_ports
; i
++) {
672 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
674 (((__u16
) serial
->port
[i
]->number
-
675 (__u16
) (serial
->minor
)) + 1) << 8;
676 if (mos7840_port
->open
) {
678 dbg("SP%d No Interrupt !!!", i
);
680 switch (sp
[i
] & 0x0f) {
682 dbg("Serial Port %d: Receiver status error or ", i
);
683 dbg("address bit detected in 9-bit mode");
684 mos7840_port
->MsrLsr
= 1;
685 wreg
= LINE_STATUS_REGISTER
;
688 dbg("Serial Port %d: Modem status change", i
);
689 mos7840_port
->MsrLsr
= 0;
690 wreg
= MODEM_STATUS_REGISTER
;
693 rv
= mos7840_get_reg(mos7840_port
, wval
, wreg
, &Data
);
698 /* the completion handler for the control urb will resubmit */
701 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
703 dev_err(&urb
->dev
->dev
,
704 "%s - Error %d submitting interrupt urb\n",
709 static int mos7840_port_paranoia_check(struct usb_serial_port
*port
,
710 const char *function
)
713 dbg("%s - port == NULL", function
);
717 dbg("%s - port->serial == NULL", function
);
724 /* Inline functions to check the sanity of a pointer that is passed to us */
725 static int mos7840_serial_paranoia_check(struct usb_serial
*serial
,
726 const char *function
)
729 dbg("%s - serial == NULL", function
);
733 dbg("%s - serial->type == NULL!", function
);
740 static struct usb_serial
*mos7840_get_usb_serial(struct usb_serial_port
*port
,
741 const char *function
)
743 /* if no port was specified, or it fails a paranoia check */
745 mos7840_port_paranoia_check(port
, function
) ||
746 mos7840_serial_paranoia_check(port
->serial
, function
)) {
747 /* then say that we don't have a valid usb_serial thing,
748 * which will end up genrating -ENODEV return values */
755 /*****************************************************************************
756 * mos7840_bulk_in_callback
757 * this is the callback function for when we have received data on the
759 *****************************************************************************/
761 static void mos7840_bulk_in_callback(struct urb
*urb
)
765 struct usb_serial
*serial
;
766 struct usb_serial_port
*port
;
767 struct moschip_port
*mos7840_port
;
768 struct tty_struct
*tty
;
769 int status
= urb
->status
;
771 mos7840_port
= urb
->context
;
773 dbg("%s", "NULL mos7840_port pointer");
778 dbg("nonzero read bulk status received: %d", status
);
779 mos7840_port
->read_urb_busy
= false;
783 port
= (struct usb_serial_port
*)mos7840_port
->port
;
784 if (mos7840_port_paranoia_check(port
, __func__
)) {
785 dbg("%s", "Port Paranoia failed");
786 mos7840_port
->read_urb_busy
= false;
790 serial
= mos7840_get_usb_serial(port
, __func__
);
792 dbg("%s", "Bad serial pointer");
793 mos7840_port
->read_urb_busy
= false;
797 data
= urb
->transfer_buffer
;
799 if (urb
->actual_length
) {
800 tty
= tty_port_tty_get(&mos7840_port
->port
->port
);
802 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
804 tty_flip_buffer_push(tty
);
807 mos7840_port
->icount
.rx
+= urb
->actual_length
;
809 dbg("mos7840_port->icount.rx is %d:",
810 mos7840_port
->icount
.rx
);
813 if (!mos7840_port
->read_urb
) {
814 dbg("%s", "URB KILLED !!!");
815 mos7840_port
->read_urb_busy
= false;
820 if (mos7840_port
->has_led
&& !mos7840_port
->led_flag
) {
821 mos7840_port
->led_flag
= true;
822 mos7840_set_led_async(mos7840_port
, 0x0301,
823 MODEM_CONTROL_REGISTER
);
824 mod_timer(&mos7840_port
->led_timer1
,
825 jiffies
+ msecs_to_jiffies(LED_ON_MS
));
828 mos7840_port
->read_urb_busy
= true;
829 retval
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
832 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval
);
833 mos7840_port
->read_urb_busy
= false;
837 /*****************************************************************************
838 * mos7840_bulk_out_data_callback
839 * this is the callback function for when we have finished sending
840 * serial data on the bulk out endpoint.
841 *****************************************************************************/
843 static void mos7840_bulk_out_data_callback(struct urb
*urb
)
845 struct moschip_port
*mos7840_port
;
846 struct tty_struct
*tty
;
847 int status
= urb
->status
;
850 mos7840_port
= urb
->context
;
851 spin_lock(&mos7840_port
->pool_lock
);
852 for (i
= 0; i
< NUM_URBS
; i
++) {
853 if (urb
== mos7840_port
->write_urb_pool
[i
]) {
854 mos7840_port
->busy
[i
] = 0;
858 spin_unlock(&mos7840_port
->pool_lock
);
861 dbg("nonzero write bulk status received:%d", status
);
865 if (mos7840_port_paranoia_check(mos7840_port
->port
, __func__
)) {
866 dbg("%s", "Port Paranoia failed");
870 tty
= tty_port_tty_get(&mos7840_port
->port
->port
);
871 if (tty
&& mos7840_port
->open
)
877 /************************************************************************/
878 /* 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 */
879 /************************************************************************/
880 #ifdef MCSSerialProbe
881 static int mos7840_serial_probe(struct usb_serial
*serial
,
882 const struct usb_device_id
*id
)
885 /*need to implement the mode_reg reading and updating\
886 structures usb_serial_ device_type\
887 (i.e num_ports, num_bulkin,bulkout etc) */
888 /* Also we can update the changes attach */
893 /*****************************************************************************
895 * this function is called by the tty driver when a port is opened
896 * If successful, we return 0
897 * Otherwise we return a negative error number.
898 *****************************************************************************/
900 static int mos7840_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
904 struct usb_serial
*serial
;
908 struct moschip_port
*mos7840_port
;
909 struct moschip_port
*port0
;
911 if (mos7840_port_paranoia_check(port
, __func__
)) {
912 dbg("%s", "Port Paranoia failed");
916 serial
= port
->serial
;
918 if (mos7840_serial_paranoia_check(serial
, __func__
)) {
919 dbg("%s", "Serial Paranoia failed");
923 mos7840_port
= mos7840_get_port_private(port
);
924 port0
= mos7840_get_port_private(serial
->port
[0]);
926 if (mos7840_port
== NULL
|| port0
== NULL
)
929 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
930 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
933 /* Initialising the write urb pool */
934 for (j
= 0; j
< NUM_URBS
; ++j
) {
935 urb
= usb_alloc_urb(0, GFP_KERNEL
);
936 mos7840_port
->write_urb_pool
[j
] = urb
;
939 dev_err(&port
->dev
, "No more urbs???\n");
943 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
945 if (!urb
->transfer_buffer
) {
947 mos7840_port
->write_urb_pool
[j
] = NULL
;
949 "%s-out of memory for urb buffers.\n",
955 /*****************************************************************************
956 * Initialize MCS7840 -- Write Init values to corresponding Registers
964 * 0x08 : SP1/2 Control Reg
965 *****************************************************************************/
967 /* NEED to check the following Block */
970 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
972 dbg("Reading Spreg failed");
976 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
978 dbg("writing Spreg failed");
983 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
985 dbg("writing Spreg failed");
988 /* End of block to be checked */
991 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
994 dbg("Reading Controlreg failed");
997 Data
|= 0x08; /* Driver done bit */
998 Data
|= 0x20; /* rx_disable */
999 status
= mos7840_set_reg_sync(port
,
1000 mos7840_port
->ControlRegOffset
, Data
);
1002 dbg("writing Controlreg failed");
1005 /* do register settings here */
1006 /* Set all regs to the device default values. */
1007 /***********************************
1008 * First Disable all interrupts.
1009 ***********************************/
1011 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1013 dbg("disabling interrupts failed");
1016 /* Set FIFO_CONTROL_REGISTER to the default value */
1018 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1020 dbg("Writing FIFO_CONTROL_REGISTER failed");
1025 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1027 dbg("Writing FIFO_CONTROL_REGISTER failed");
1032 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1033 mos7840_port
->shadowLCR
= Data
;
1036 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1037 mos7840_port
->shadowMCR
= Data
;
1040 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
1041 mos7840_port
->shadowLCR
= Data
;
1043 Data
|= SERIAL_LCR_DLAB
; /* data latch enable in LCR 0x80 */
1044 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1047 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
1050 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
1053 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
1055 Data
= Data
& ~SERIAL_LCR_DLAB
;
1056 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1057 mos7840_port
->shadowLCR
= Data
;
1059 /* clearing Bulkin and Bulkout Fifo */
1061 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
1064 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1066 Data
= Data
& ~0x0c;
1067 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1068 /* Finally enable all interrupts */
1070 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1072 /* clearing rx_disable */
1074 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1076 Data
= Data
& ~0x20;
1077 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1082 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1085 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1088 /* Check to see if we've set up our endpoint info yet *
1089 * (can't set it up in mos7840_startup as the structures *
1090 * were not set up at that time.) */
1091 if (port0
->open_ports
== 1) {
1092 if (serial
->port
[0]->interrupt_in_buffer
== NULL
) {
1093 /* set up interrupt urb */
1094 usb_fill_int_urb(serial
->port
[0]->interrupt_in_urb
,
1096 usb_rcvintpipe(serial
->dev
,
1097 serial
->port
[0]->interrupt_in_endpointAddress
),
1098 serial
->port
[0]->interrupt_in_buffer
,
1099 serial
->port
[0]->interrupt_in_urb
->
1100 transfer_buffer_length
,
1101 mos7840_interrupt_callback
,
1103 serial
->port
[0]->interrupt_in_urb
->interval
);
1105 /* start interrupt read for mos7840 *
1106 * will continue as long as mos7840 is connected */
1109 usb_submit_urb(serial
->port
[0]->interrupt_in_urb
,
1112 dev_err(&port
->dev
, "%s - Error %d submitting "
1113 "interrupt urb\n", __func__
, response
);
1120 /* see if we've set up our endpoint info yet *
1121 * (can't set it up in mos7840_startup as the *
1122 * structures were not set up at that time.) */
1124 dbg("port number is %d", port
->number
);
1125 dbg("serial number is %d", port
->serial
->minor
);
1126 dbg("Bulkin endpoint is %d", port
->bulk_in_endpointAddress
);
1127 dbg("BulkOut endpoint is %d", port
->bulk_out_endpointAddress
);
1128 dbg("Interrupt endpoint is %d", port
->interrupt_in_endpointAddress
);
1129 dbg("port's number in the device is %d", mos7840_port
->port_num
);
1130 mos7840_port
->read_urb
= port
->read_urb
;
1132 /* set up our bulk in urb */
1133 if ((serial
->num_ports
== 2)
1134 && ((((__u16
)port
->number
-
1135 (__u16
)(port
->serial
->minor
)) % 2) != 0)) {
1136 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1138 usb_rcvbulkpipe(serial
->dev
,
1139 (port
->bulk_in_endpointAddress
) + 2),
1140 port
->bulk_in_buffer
,
1141 mos7840_port
->read_urb
->transfer_buffer_length
,
1142 mos7840_bulk_in_callback
, mos7840_port
);
1144 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1146 usb_rcvbulkpipe(serial
->dev
,
1147 port
->bulk_in_endpointAddress
),
1148 port
->bulk_in_buffer
,
1149 mos7840_port
->read_urb
->transfer_buffer_length
,
1150 mos7840_bulk_in_callback
, mos7840_port
);
1153 dbg("mos7840_open: bulkin endpoint is %d",
1154 port
->bulk_in_endpointAddress
);
1155 mos7840_port
->read_urb_busy
= true;
1156 response
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1158 dev_err(&port
->dev
, "%s - Error %d submitting control urb\n",
1159 __func__
, response
);
1160 mos7840_port
->read_urb_busy
= false;
1163 /* initialize our wait queues */
1164 init_waitqueue_head(&mos7840_port
->wait_chase
);
1165 init_waitqueue_head(&mos7840_port
->delta_msr_wait
);
1167 /* initialize our icount structure */
1168 memset(&(mos7840_port
->icount
), 0x00, sizeof(mos7840_port
->icount
));
1170 /* initialize our port settings */
1171 /* Must set to enable ints! */
1172 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
1173 /* send a open port command */
1174 mos7840_port
->open
= 1;
1175 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1176 mos7840_port
->icount
.tx
= 0;
1177 mos7840_port
->icount
.rx
= 0;
1179 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
1180 serial
, mos7840_port
, port
);
1185 /*****************************************************************************
1186 * mos7840_chars_in_buffer
1187 * this function is called by the tty driver when it wants to know how many
1188 * bytes of data we currently have outstanding in the port (data that has
1189 * been written, but hasn't made it out the port yet)
1190 * If successful, we return the number of bytes left to be written in the
1192 * Otherwise we return zero.
1193 *****************************************************************************/
1195 static int mos7840_chars_in_buffer(struct tty_struct
*tty
)
1197 struct usb_serial_port
*port
= tty
->driver_data
;
1200 unsigned long flags
;
1201 struct moschip_port
*mos7840_port
;
1203 if (mos7840_port_paranoia_check(port
, __func__
)) {
1204 dbg("%s", "Invalid port");
1208 mos7840_port
= mos7840_get_port_private(port
);
1209 if (mos7840_port
== NULL
)
1212 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1213 for (i
= 0; i
< NUM_URBS
; ++i
) {
1214 if (mos7840_port
->busy
[i
]) {
1215 struct urb
*urb
= mos7840_port
->write_urb_pool
[i
];
1216 chars
+= urb
->transfer_buffer_length
;
1219 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1220 dbg("%s - returns %d", __func__
, chars
);
1225 /*****************************************************************************
1227 * this function is called by the tty driver when a port is closed
1228 *****************************************************************************/
1230 static void mos7840_close(struct usb_serial_port
*port
)
1232 struct usb_serial
*serial
;
1233 struct moschip_port
*mos7840_port
;
1234 struct moschip_port
*port0
;
1238 if (mos7840_port_paranoia_check(port
, __func__
)) {
1239 dbg("%s", "Port Paranoia failed");
1243 serial
= mos7840_get_usb_serial(port
, __func__
);
1245 dbg("%s", "Serial Paranoia failed");
1249 mos7840_port
= mos7840_get_port_private(port
);
1250 port0
= mos7840_get_port_private(serial
->port
[0]);
1252 if (mos7840_port
== NULL
|| port0
== NULL
)
1255 for (j
= 0; j
< NUM_URBS
; ++j
)
1256 usb_kill_urb(mos7840_port
->write_urb_pool
[j
]);
1258 /* Freeing Write URBs */
1259 for (j
= 0; j
< NUM_URBS
; ++j
) {
1260 if (mos7840_port
->write_urb_pool
[j
]) {
1261 if (mos7840_port
->write_urb_pool
[j
]->transfer_buffer
)
1262 kfree(mos7840_port
->write_urb_pool
[j
]->
1265 usb_free_urb(mos7840_port
->write_urb_pool
[j
]);
1269 /* While closing port, shutdown all bulk read, write *
1270 * and interrupt read if they exists */
1272 if (mos7840_port
->write_urb
) {
1273 dbg("%s", "Shutdown bulk write");
1274 usb_kill_urb(mos7840_port
->write_urb
);
1276 if (mos7840_port
->read_urb
) {
1277 dbg("%s", "Shutdown bulk read");
1278 usb_kill_urb(mos7840_port
->read_urb
);
1279 mos7840_port
->read_urb_busy
= false;
1281 if ((&mos7840_port
->control_urb
)) {
1282 dbg("%s", "Shutdown control read");
1283 /*/ usb_kill_urb (mos7840_port->control_urb); */
1286 /* if(mos7840_port->ctrl_buf != NULL) */
1287 /* kfree(mos7840_port->ctrl_buf); */
1288 port0
->open_ports
--;
1289 dbg("mos7840_num_open_ports in close%d:in port%d",
1290 port0
->open_ports
, port
->number
);
1291 if (port0
->open_ports
== 0) {
1292 if (serial
->port
[0]->interrupt_in_urb
) {
1293 dbg("%s", "Shutdown interrupt_in_urb");
1294 usb_kill_urb(serial
->port
[0]->interrupt_in_urb
);
1298 if (mos7840_port
->write_urb
) {
1299 /* if this urb had a transfer buffer already (old tx) free it */
1300 if (mos7840_port
->write_urb
->transfer_buffer
!= NULL
)
1301 kfree(mos7840_port
->write_urb
->transfer_buffer
);
1302 usb_free_urb(mos7840_port
->write_urb
);
1306 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1309 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1311 mos7840_port
->open
= 0;
1314 /************************************************************************
1316 * mos7840_block_until_chase_response
1318 * This function will block the close until one of the following:
1319 * 1. Response to our Chase comes from mos7840
1320 * 2. A timeout of 10 seconds without activity has expired
1321 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1323 ************************************************************************/
1325 static void mos7840_block_until_chase_response(struct tty_struct
*tty
,
1326 struct moschip_port
*mos7840_port
)
1328 int timeout
= msecs_to_jiffies(1000);
1333 count
= mos7840_chars_in_buffer(tty
);
1335 /* Check for Buffer status */
1339 /* Block the thread for a while */
1340 interruptible_sleep_on_timeout(&mos7840_port
->wait_chase
,
1342 /* No activity.. count down section */
1345 dbg("%s - TIMEOUT", __func__
);
1348 /* Reset timeout value back to seconds */
1355 /*****************************************************************************
1357 * this function sends a break to the port
1358 *****************************************************************************/
1359 static void mos7840_break(struct tty_struct
*tty
, int break_state
)
1361 struct usb_serial_port
*port
= tty
->driver_data
;
1363 struct usb_serial
*serial
;
1364 struct moschip_port
*mos7840_port
;
1366 if (mos7840_port_paranoia_check(port
, __func__
)) {
1367 dbg("%s", "Port Paranoia failed");
1371 serial
= mos7840_get_usb_serial(port
, __func__
);
1373 dbg("%s", "Serial Paranoia failed");
1377 mos7840_port
= mos7840_get_port_private(port
);
1379 if (mos7840_port
== NULL
)
1383 /* flush and block until tx is empty */
1384 mos7840_block_until_chase_response(tty
, mos7840_port
);
1386 if (break_state
== -1)
1387 data
= mos7840_port
->shadowLCR
| LCR_SET_BREAK
;
1389 data
= mos7840_port
->shadowLCR
& ~LCR_SET_BREAK
;
1391 /* FIXME: no locking on shadowLCR anywhere in driver */
1392 mos7840_port
->shadowLCR
= data
;
1393 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
1394 mos7840_port
->shadowLCR
);
1395 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
,
1396 mos7840_port
->shadowLCR
);
1399 /*****************************************************************************
1400 * mos7840_write_room
1401 * this function is called by the tty driver when it wants to know how many
1402 * bytes of data we can accept for a specific port.
1403 * If successful, we return the amount of room that we have for this port
1404 * Otherwise we return a negative error number.
1405 *****************************************************************************/
1407 static int mos7840_write_room(struct tty_struct
*tty
)
1409 struct usb_serial_port
*port
= tty
->driver_data
;
1412 unsigned long flags
;
1413 struct moschip_port
*mos7840_port
;
1415 if (mos7840_port_paranoia_check(port
, __func__
)) {
1416 dbg("%s", "Invalid port");
1417 dbg("%s", " mos7840_write_room:leaving ...........");
1421 mos7840_port
= mos7840_get_port_private(port
);
1422 if (mos7840_port
== NULL
) {
1423 dbg("%s", "mos7840_break:leaving ...........");
1427 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1428 for (i
= 0; i
< NUM_URBS
; ++i
) {
1429 if (!mos7840_port
->busy
[i
])
1430 room
+= URB_TRANSFER_BUFFER_SIZE
;
1432 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1434 room
= (room
== 0) ? 0 : room
- URB_TRANSFER_BUFFER_SIZE
+ 1;
1435 dbg("%s - returns %d", __func__
, room
);
1440 /*****************************************************************************
1442 * this function is called by the tty driver when data should be written to
1444 * If successful, we return the number of bytes written, otherwise we
1445 * return a negative error number.
1446 *****************************************************************************/
1448 static int mos7840_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1449 const unsigned char *data
, int count
)
1455 unsigned long flags
;
1457 struct moschip_port
*mos7840_port
;
1458 struct usb_serial
*serial
;
1461 const unsigned char *current_position
= data
;
1462 unsigned char *data1
;
1466 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
1467 mos7840_port
->shadowLCR
= Data
;
1468 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data
);
1469 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1470 mos7840_port
->shadowLCR
);
1473 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1474 /* mos7840_port->shadowLCR=Data;//Need to add later */
1476 Data
|= SERIAL_LCR_DLAB
; /* data latch enable in LCR 0x80 */
1477 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1480 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1482 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_LSB
, &Data
);
1483 dbg("mos7840_write:DLL value is %x", Data
);
1486 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_MSB
, &Data
);
1487 dbg("mos7840_write:DLM value is %x", Data
);
1489 Data
= Data
& ~SERIAL_LCR_DLAB
;
1490 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1491 mos7840_port
->shadowLCR
);
1492 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1495 if (mos7840_port_paranoia_check(port
, __func__
)) {
1496 dbg("%s", "Port Paranoia failed");
1500 serial
= port
->serial
;
1501 if (mos7840_serial_paranoia_check(serial
, __func__
)) {
1502 dbg("%s", "Serial Paranoia failed");
1506 mos7840_port
= mos7840_get_port_private(port
);
1507 if (mos7840_port
== NULL
) {
1508 dbg("%s", "mos7840_port is NULL");
1512 /* try to find a free urb in the list */
1515 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1516 for (i
= 0; i
< NUM_URBS
; ++i
) {
1517 if (!mos7840_port
->busy
[i
]) {
1518 mos7840_port
->busy
[i
] = 1;
1519 urb
= mos7840_port
->write_urb_pool
[i
];
1524 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1527 dbg("%s - no more free urbs", __func__
);
1531 if (urb
->transfer_buffer
== NULL
) {
1532 urb
->transfer_buffer
=
1533 kmalloc(URB_TRANSFER_BUFFER_SIZE
, GFP_KERNEL
);
1535 if (urb
->transfer_buffer
== NULL
) {
1536 dev_err_console(port
, "%s no more kernel memory...\n",
1541 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1543 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1545 /* fill urb with data and submit */
1546 if ((serial
->num_ports
== 2)
1547 && ((((__u16
)port
->number
-
1548 (__u16
)(port
->serial
->minor
)) % 2) != 0)) {
1549 usb_fill_bulk_urb(urb
,
1551 usb_sndbulkpipe(serial
->dev
,
1552 (port
->bulk_out_endpointAddress
) + 2),
1553 urb
->transfer_buffer
,
1555 mos7840_bulk_out_data_callback
, mos7840_port
);
1557 usb_fill_bulk_urb(urb
,
1559 usb_sndbulkpipe(serial
->dev
,
1560 port
->bulk_out_endpointAddress
),
1561 urb
->transfer_buffer
,
1563 mos7840_bulk_out_data_callback
, mos7840_port
);
1566 data1
= urb
->transfer_buffer
;
1567 dbg("bulkout endpoint is %d", port
->bulk_out_endpointAddress
);
1570 if (mos7840_port
->has_led
&& !mos7840_port
->led_flag
) {
1571 mos7840_port
->led_flag
= true;
1572 mos7840_set_led_sync(port
, MODEM_CONTROL_REGISTER
, 0x0301);
1573 mod_timer(&mos7840_port
->led_timer1
,
1574 jiffies
+ msecs_to_jiffies(LED_ON_MS
));
1577 /* send it down the pipe */
1578 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1581 mos7840_port
->busy
[i
] = 0;
1582 dev_err_console(port
, "%s - usb_submit_urb(write bulk) failed "
1583 "with status = %d\n", __func__
, status
);
1584 bytes_sent
= status
;
1587 bytes_sent
= transfer_size
;
1588 mos7840_port
->icount
.tx
+= transfer_size
;
1590 dbg("mos7840_port->icount.tx is %d:", mos7840_port
->icount
.tx
);
1596 /*****************************************************************************
1598 * this function is called by the tty driver when it wants to stop the data
1599 * being read from the port.
1600 *****************************************************************************/
1602 static void mos7840_throttle(struct tty_struct
*tty
)
1604 struct usb_serial_port
*port
= tty
->driver_data
;
1605 struct moschip_port
*mos7840_port
;
1608 if (mos7840_port_paranoia_check(port
, __func__
)) {
1609 dbg("%s", "Invalid port");
1613 dbg("- port %d", port
->number
);
1615 mos7840_port
= mos7840_get_port_private(port
);
1617 if (mos7840_port
== NULL
)
1620 if (!mos7840_port
->open
) {
1621 dbg("%s", "port not opened");
1625 /* if we are implementing XON/XOFF, send the stop character */
1627 unsigned char stop_char
= STOP_CHAR(tty
);
1628 status
= mos7840_write(tty
, port
, &stop_char
, 1);
1632 /* if we are implementing RTS/CTS, toggle that line */
1633 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1634 mos7840_port
->shadowMCR
&= ~MCR_RTS
;
1635 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1636 mos7840_port
->shadowMCR
);
1642 /*****************************************************************************
1643 * mos7840_unthrottle
1644 * this function is called by the tty driver when it wants to resume
1645 * the data being read from the port (called after mos7840_throttle is
1647 *****************************************************************************/
1648 static void mos7840_unthrottle(struct tty_struct
*tty
)
1650 struct usb_serial_port
*port
= tty
->driver_data
;
1652 struct moschip_port
*mos7840_port
= mos7840_get_port_private(port
);
1654 if (mos7840_port_paranoia_check(port
, __func__
)) {
1655 dbg("%s", "Invalid port");
1659 if (mos7840_port
== NULL
)
1662 if (!mos7840_port
->open
) {
1663 dbg("%s - port not opened", __func__
);
1667 /* if we are implementing XON/XOFF, send the start character */
1669 unsigned char start_char
= START_CHAR(tty
);
1670 status
= mos7840_write(tty
, port
, &start_char
, 1);
1675 /* if we are implementing RTS/CTS, toggle that line */
1676 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1677 mos7840_port
->shadowMCR
|= MCR_RTS
;
1678 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1679 mos7840_port
->shadowMCR
);
1685 static int mos7840_tiocmget(struct tty_struct
*tty
)
1687 struct usb_serial_port
*port
= tty
->driver_data
;
1688 struct moschip_port
*mos7840_port
;
1689 unsigned int result
;
1693 mos7840_port
= mos7840_get_port_private(port
);
1695 if (mos7840_port
== NULL
)
1698 status
= mos7840_get_uart_reg(port
, MODEM_STATUS_REGISTER
, &msr
);
1699 status
= mos7840_get_uart_reg(port
, MODEM_CONTROL_REGISTER
, &mcr
);
1700 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0)
1701 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0)
1702 | ((mcr
& MCR_LOOPBACK
) ? TIOCM_LOOP
: 0)
1703 | ((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0)
1704 | ((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0)
1705 | ((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0)
1706 | ((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0);
1708 dbg("%s - 0x%04X", __func__
, result
);
1713 static int mos7840_tiocmset(struct tty_struct
*tty
,
1714 unsigned int set
, unsigned int clear
)
1716 struct usb_serial_port
*port
= tty
->driver_data
;
1717 struct moschip_port
*mos7840_port
;
1721 mos7840_port
= mos7840_get_port_private(port
);
1723 if (mos7840_port
== NULL
)
1726 /* FIXME: What locks the port registers ? */
1727 mcr
= mos7840_port
->shadowMCR
;
1728 if (clear
& TIOCM_RTS
)
1730 if (clear
& TIOCM_DTR
)
1732 if (clear
& TIOCM_LOOP
)
1733 mcr
&= ~MCR_LOOPBACK
;
1735 if (set
& TIOCM_RTS
)
1737 if (set
& TIOCM_DTR
)
1739 if (set
& TIOCM_LOOP
)
1740 mcr
|= MCR_LOOPBACK
;
1742 mos7840_port
->shadowMCR
= mcr
;
1744 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, mcr
);
1746 dbg("setting MODEM_CONTROL_REGISTER Failed");
1753 /*****************************************************************************
1754 * mos7840_calc_baud_rate_divisor
1755 * this function calculates the proper baud rate divisor for the specified
1757 *****************************************************************************/
1758 static int mos7840_calc_baud_rate_divisor(int baudRate
, int *divisor
,
1761 dbg("%s - %d", __func__
, baudRate
);
1763 if (baudRate
<= 115200) {
1764 *divisor
= 115200 / baudRate
;
1767 if ((baudRate
> 115200) && (baudRate
<= 230400)) {
1768 *divisor
= 230400 / baudRate
;
1769 *clk_sel_val
= 0x10;
1770 } else if ((baudRate
> 230400) && (baudRate
<= 403200)) {
1771 *divisor
= 403200 / baudRate
;
1772 *clk_sel_val
= 0x20;
1773 } else if ((baudRate
> 403200) && (baudRate
<= 460800)) {
1774 *divisor
= 460800 / baudRate
;
1775 *clk_sel_val
= 0x30;
1776 } else if ((baudRate
> 460800) && (baudRate
<= 806400)) {
1777 *divisor
= 806400 / baudRate
;
1778 *clk_sel_val
= 0x40;
1779 } else if ((baudRate
> 806400) && (baudRate
<= 921600)) {
1780 *divisor
= 921600 / baudRate
;
1781 *clk_sel_val
= 0x50;
1782 } else if ((baudRate
> 921600) && (baudRate
<= 1572864)) {
1783 *divisor
= 1572864 / baudRate
;
1784 *clk_sel_val
= 0x60;
1785 } else if ((baudRate
> 1572864) && (baudRate
<= 3145728)) {
1786 *divisor
= 3145728 / baudRate
;
1787 *clk_sel_val
= 0x70;
1793 for (i
= 0; i
< ARRAY_SIZE(mos7840_divisor_table
); i
++) {
1794 if (mos7840_divisor_table
[i
].BaudRate
== baudrate
) {
1795 *divisor
= mos7840_divisor_table
[i
].Divisor
;
1800 /* After trying for all the standard baud rates *
1801 * Try calculating the divisor for this baud rate */
1803 if (baudrate
> 75 && baudrate
< 230400) {
1804 /* get the divisor */
1805 custom
= (__u16
) (230400L / baudrate
);
1807 /* Check for round off */
1808 round1
= (__u16
) (2304000L / baudrate
);
1809 round
= (__u16
) (round1
- (custom
* 10));
1814 dbg(" Baud %d = %d", baudrate
, custom
);
1818 dbg("%s", " Baud calculation Failed...");
1823 /*****************************************************************************
1824 * mos7840_send_cmd_write_baud_rate
1825 * this function sends the proper command to change the baud rate of the
1827 *****************************************************************************/
1829 static int mos7840_send_cmd_write_baud_rate(struct moschip_port
*mos7840_port
,
1835 unsigned char number
;
1837 struct usb_serial_port
*port
;
1839 if (mos7840_port
== NULL
)
1842 port
= (struct usb_serial_port
*)mos7840_port
->port
;
1843 if (mos7840_port_paranoia_check(port
, __func__
)) {
1844 dbg("%s", "Invalid port");
1848 if (mos7840_serial_paranoia_check(port
->serial
, __func__
)) {
1849 dbg("%s", "Invalid Serial");
1853 number
= mos7840_port
->port
->number
- mos7840_port
->port
->serial
->minor
;
1855 dbg("%s - port = %d, baud = %d", __func__
,
1856 mos7840_port
->port
->number
, baudRate
);
1857 /* reset clk_uart_sel in spregOffset */
1858 if (baudRate
> 115200) {
1859 #ifdef HW_flow_control
1860 /* NOTE: need to see the pther register to modify */
1861 /* setting h/w flow control bit to 1 */
1863 mos7840_port
->shadowMCR
= Data
;
1864 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1867 dbg("Writing spreg failed in set_serial_baud");
1873 #ifdef HW_flow_control
1874 /* setting h/w flow control bit to 0 */
1876 mos7840_port
->shadowMCR
= Data
;
1877 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1880 dbg("Writing spreg failed in set_serial_baud");
1887 if (1) { /* baudRate <= 115200) */
1890 status
= mos7840_calc_baud_rate_divisor(baudRate
, &divisor
,
1892 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
,
1895 dbg("reading spreg failed in set_serial_baud");
1898 Data
= (Data
& 0x8f) | clk_sel_val
;
1899 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
,
1902 dbg("Writing spreg failed in set_serial_baud");
1905 /* Calculate the Divisor */
1908 dev_err(&port
->dev
, "%s - bad baud rate\n", __func__
);
1911 /* Enable access to divisor latch */
1912 Data
= mos7840_port
->shadowLCR
| SERIAL_LCR_DLAB
;
1913 mos7840_port
->shadowLCR
= Data
;
1914 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1916 /* Write the divisor */
1917 Data
= (unsigned char)(divisor
& 0xff);
1918 dbg("set_serial_baud Value to write DLL is %x", Data
);
1919 mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
1921 Data
= (unsigned char)((divisor
& 0xff00) >> 8);
1922 dbg("set_serial_baud Value to write DLM is %x", Data
);
1923 mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
1925 /* Disable access to divisor latch */
1926 Data
= mos7840_port
->shadowLCR
& ~SERIAL_LCR_DLAB
;
1927 mos7840_port
->shadowLCR
= Data
;
1928 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1934 /*****************************************************************************
1935 * mos7840_change_port_settings
1936 * This routine is called to set the UART on the device to match
1937 * the specified new settings.
1938 *****************************************************************************/
1940 static void mos7840_change_port_settings(struct tty_struct
*tty
,
1941 struct moschip_port
*mos7840_port
, struct ktermios
*old_termios
)
1951 struct usb_serial_port
*port
;
1952 struct usb_serial
*serial
;
1954 if (mos7840_port
== NULL
)
1957 port
= (struct usb_serial_port
*)mos7840_port
->port
;
1959 if (mos7840_port_paranoia_check(port
, __func__
)) {
1960 dbg("%s", "Invalid port");
1964 if (mos7840_serial_paranoia_check(port
->serial
, __func__
)) {
1965 dbg("%s", "Invalid Serial");
1969 serial
= port
->serial
;
1971 dbg("%s - port %d", __func__
, mos7840_port
->port
->number
);
1973 if (!mos7840_port
->open
) {
1974 dbg("%s - port not opened", __func__
);
1980 lParity
= LCR_PAR_NONE
;
1982 cflag
= tty
->termios
->c_cflag
;
1983 iflag
= tty
->termios
->c_iflag
;
1985 /* Change the number of bits */
1986 if (cflag
& CSIZE
) {
1987 switch (cflag
& CSIZE
) {
2005 /* Change the Parity bit */
2006 if (cflag
& PARENB
) {
2007 if (cflag
& PARODD
) {
2008 lParity
= LCR_PAR_ODD
;
2009 dbg("%s - parity = odd", __func__
);
2011 lParity
= LCR_PAR_EVEN
;
2012 dbg("%s - parity = even", __func__
);
2016 dbg("%s - parity = none", __func__
);
2020 lParity
= lParity
| 0x20;
2022 /* Change the Stop bit */
2023 if (cflag
& CSTOPB
) {
2025 dbg("%s - stop bits = 2", __func__
);
2028 dbg("%s - stop bits = 1", __func__
);
2031 /* Update the LCR with the correct value */
2032 mos7840_port
->shadowLCR
&=
2033 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
2034 mos7840_port
->shadowLCR
|= (lData
| lParity
| lStop
);
2036 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
2037 mos7840_port
->shadowLCR
);
2038 /* Disable Interrupts */
2040 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
2043 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
2046 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
2048 /* Send the updated LCR value to the mos7840 */
2049 Data
= mos7840_port
->shadowLCR
;
2051 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
2054 mos7840_port
->shadowMCR
= Data
;
2055 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2057 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2059 /* set up the MCR register and send it to the mos7840 */
2061 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
2063 mos7840_port
->shadowMCR
|= (MCR_DTR
| MCR_RTS
);
2065 if (cflag
& CRTSCTS
)
2066 mos7840_port
->shadowMCR
|= (MCR_XON_ANY
);
2068 mos7840_port
->shadowMCR
&= ~(MCR_XON_ANY
);
2070 Data
= mos7840_port
->shadowMCR
;
2071 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2073 /* Determine divisor based on baud rate */
2074 baud
= tty_get_baud_rate(tty
);
2077 /* pick a default, any default... */
2078 dbg("%s", "Picked default baud...");
2082 dbg("%s - baud rate = %d", __func__
, baud
);
2083 status
= mos7840_send_cmd_write_baud_rate(mos7840_port
, baud
);
2085 /* Enable Interrupts */
2087 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
2089 if (mos7840_port
->read_urb_busy
== false) {
2090 mos7840_port
->read_urb_busy
= true;
2091 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
2093 dbg("usb_submit_urb(read bulk) failed, status = %d",
2095 mos7840_port
->read_urb_busy
= false;
2098 wake_up(&mos7840_port
->delta_msr_wait
);
2099 mos7840_port
->delta_msr_cond
= 1;
2100 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
2101 mos7840_port
->shadowLCR
);
2104 /*****************************************************************************
2105 * mos7840_set_termios
2106 * this function is called by the tty driver when it wants to change
2107 * the termios structure
2108 *****************************************************************************/
2110 static void mos7840_set_termios(struct tty_struct
*tty
,
2111 struct usb_serial_port
*port
,
2112 struct ktermios
*old_termios
)
2116 struct usb_serial
*serial
;
2117 struct moschip_port
*mos7840_port
;
2119 if (mos7840_port_paranoia_check(port
, __func__
)) {
2120 dbg("%s", "Invalid port");
2124 serial
= port
->serial
;
2126 if (mos7840_serial_paranoia_check(serial
, __func__
)) {
2127 dbg("%s", "Invalid Serial");
2131 mos7840_port
= mos7840_get_port_private(port
);
2133 if (mos7840_port
== NULL
)
2136 if (!mos7840_port
->open
) {
2137 dbg("%s - port not opened", __func__
);
2141 dbg("%s", "setting termios - ");
2143 cflag
= tty
->termios
->c_cflag
;
2145 dbg("%s - clfag %08x iflag %08x", __func__
,
2146 tty
->termios
->c_cflag
, RELEVANT_IFLAG(tty
->termios
->c_iflag
));
2147 dbg("%s - old clfag %08x old iflag %08x", __func__
,
2148 old_termios
->c_cflag
, RELEVANT_IFLAG(old_termios
->c_iflag
));
2149 dbg("%s - port %d", __func__
, port
->number
);
2151 /* change the port settings to the new ones specified */
2153 mos7840_change_port_settings(tty
, mos7840_port
, old_termios
);
2155 if (!mos7840_port
->read_urb
) {
2156 dbg("%s", "URB KILLED !!!!!");
2160 if (mos7840_port
->read_urb_busy
== false) {
2161 mos7840_port
->read_urb_busy
= true;
2162 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
2164 dbg("usb_submit_urb(read bulk) failed, status = %d",
2166 mos7840_port
->read_urb_busy
= false;
2171 /*****************************************************************************
2172 * mos7840_get_lsr_info - get line status register info
2174 * Purpose: Let user call ioctl() to get info when the UART physically
2175 * is emptied. On bus types like RS485, the transmitter must
2176 * release the bus after transmitting. This must be done when
2177 * the transmit shift register is empty, not be done when the
2178 * transmit holding register is empty. This functionality
2179 * allows an RS485 driver to be written in user space.
2180 *****************************************************************************/
2182 static int mos7840_get_lsr_info(struct tty_struct
*tty
,
2183 unsigned int __user
*value
)
2186 unsigned int result
= 0;
2188 count
= mos7840_chars_in_buffer(tty
);
2190 dbg("%s -- Empty", __func__
);
2191 result
= TIOCSER_TEMT
;
2194 if (copy_to_user(value
, &result
, sizeof(int)))
2199 /*****************************************************************************
2200 * mos7840_get_serial_info
2201 * function to get information about serial port
2202 *****************************************************************************/
2204 static int mos7840_get_serial_info(struct moschip_port
*mos7840_port
,
2205 struct serial_struct __user
*retinfo
)
2207 struct serial_struct tmp
;
2209 if (mos7840_port
== NULL
)
2215 memset(&tmp
, 0, sizeof(tmp
));
2217 tmp
.type
= PORT_16550A
;
2218 tmp
.line
= mos7840_port
->port
->serial
->minor
;
2219 tmp
.port
= mos7840_port
->port
->number
;
2221 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
2222 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
2223 tmp
.baud_base
= 9600;
2224 tmp
.close_delay
= 5 * HZ
;
2225 tmp
.closing_wait
= 30 * HZ
;
2227 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2232 static int mos7840_get_icount(struct tty_struct
*tty
,
2233 struct serial_icounter_struct
*icount
)
2235 struct usb_serial_port
*port
= tty
->driver_data
;
2236 struct moschip_port
*mos7840_port
;
2237 struct async_icount cnow
;
2239 mos7840_port
= mos7840_get_port_private(port
);
2240 cnow
= mos7840_port
->icount
;
2243 icount
->cts
= cnow
.cts
;
2244 icount
->dsr
= cnow
.dsr
;
2245 icount
->rng
= cnow
.rng
;
2246 icount
->dcd
= cnow
.dcd
;
2247 icount
->rx
= cnow
.rx
;
2248 icount
->tx
= cnow
.tx
;
2249 icount
->frame
= cnow
.frame
;
2250 icount
->overrun
= cnow
.overrun
;
2251 icount
->parity
= cnow
.parity
;
2252 icount
->brk
= cnow
.brk
;
2253 icount
->buf_overrun
= cnow
.buf_overrun
;
2255 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__
,
2256 port
->number
, icount
->rx
, icount
->tx
);
2260 /*****************************************************************************
2262 * this function handles any ioctl calls to the driver
2263 *****************************************************************************/
2265 static int mos7840_ioctl(struct tty_struct
*tty
,
2266 unsigned int cmd
, unsigned long arg
)
2268 struct usb_serial_port
*port
= tty
->driver_data
;
2269 void __user
*argp
= (void __user
*)arg
;
2270 struct moschip_port
*mos7840_port
;
2272 struct async_icount cnow
;
2273 struct async_icount cprev
;
2275 if (mos7840_port_paranoia_check(port
, __func__
)) {
2276 dbg("%s", "Invalid port");
2280 mos7840_port
= mos7840_get_port_private(port
);
2282 if (mos7840_port
== NULL
)
2285 dbg("%s - port %d, cmd = 0x%x", __func__
, port
->number
, cmd
);
2288 /* return number of bytes available */
2291 dbg("%s (%d) TIOCSERGETLSR", __func__
, port
->number
);
2292 return mos7840_get_lsr_info(tty
, argp
);
2295 dbg("%s (%d) TIOCGSERIAL", __func__
, port
->number
);
2296 return mos7840_get_serial_info(mos7840_port
, argp
);
2299 dbg("%s (%d) TIOCSSERIAL", __func__
, port
->number
);
2303 dbg("%s (%d) TIOCMIWAIT", __func__
, port
->number
);
2304 cprev
= mos7840_port
->icount
;
2306 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2307 mos7840_port
->delta_msr_cond
= 0;
2308 wait_event_interruptible(mos7840_port
->delta_msr_wait
,
2310 delta_msr_cond
== 1));
2312 /* see if a signal did it */
2313 if (signal_pending(current
))
2314 return -ERESTARTSYS
;
2315 cnow
= mos7840_port
->icount
;
2317 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
2318 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
2319 return -EIO
; /* no change => error */
2320 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
2321 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
2322 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
2323 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
2334 return -ENOIOCTLCMD
;
2337 static int mos7810_check(struct usb_serial
*serial
)
2339 int i
, pass_count
= 0;
2340 __u16 data
= 0, mcr_data
= 0;
2341 __u16 test_pattern
= 0x55AA;
2343 /* Store MCR setting */
2344 usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2345 MCS_RDREQ
, MCS_RD_RTYPE
, 0x0300, MODEM_CONTROL_REGISTER
,
2346 &mcr_data
, VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2348 for (i
= 0; i
< 16; i
++) {
2349 /* Send the 1-bit test pattern out to MCS7810 test pin */
2350 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2351 MCS_WRREQ
, MCS_WR_RTYPE
,
2352 (0x0300 | (((test_pattern
>> i
) & 0x0001) << 1)),
2353 MODEM_CONTROL_REGISTER
, NULL
, 0, MOS_WDR_TIMEOUT
);
2355 /* Read the test pattern back */
2356 usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2357 MCS_RDREQ
, MCS_RD_RTYPE
, 0, GPIO_REGISTER
, &data
,
2358 VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2360 /* If this is a MCS7810 device, both test patterns must match */
2361 if (((test_pattern
>> i
) ^ (~data
>> 1)) & 0x0001)
2367 /* Restore MCR setting */
2368 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0), MCS_WRREQ
,
2369 MCS_WR_RTYPE
, 0x0300 | mcr_data
, MODEM_CONTROL_REGISTER
, NULL
,
2370 0, MOS_WDR_TIMEOUT
);
2372 if (pass_count
== 16)
2378 static int mos7840_calc_num_ports(struct usb_serial
*serial
)
2381 int mos7840_num_ports
;
2383 usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2384 MCS_RDREQ
, MCS_RD_RTYPE
, 0, GPIO_REGISTER
, &data
,
2385 VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2387 if (serial
->dev
->descriptor
.idProduct
== MOSCHIP_DEVICE_ID_7810
||
2388 serial
->dev
->descriptor
.idProduct
== MOSCHIP_DEVICE_ID_7820
) {
2389 device_type
= serial
->dev
->descriptor
.idProduct
;
2391 /* For a MCS7840 device GPIO0 must be set to 1 */
2392 if ((data
& 0x01) == 1)
2393 device_type
= MOSCHIP_DEVICE_ID_7840
;
2394 else if (mos7810_check(serial
))
2395 device_type
= MOSCHIP_DEVICE_ID_7810
;
2397 device_type
= MOSCHIP_DEVICE_ID_7820
;
2400 mos7840_num_ports
= (device_type
>> 4) & 0x000F;
2401 serial
->num_bulk_in
= mos7840_num_ports
;
2402 serial
->num_bulk_out
= mos7840_num_ports
;
2403 serial
->num_ports
= mos7840_num_ports
;
2405 return mos7840_num_ports
;
2408 static int mos7840_port_probe(struct usb_serial_port
*port
)
2410 struct usb_serial
*serial
= port
->serial
;
2411 struct moschip_port
*mos7840_port
;
2416 /* we set up the pointers to the endpoints in the mos7840_open *
2417 * function, as the structures aren't created yet. */
2419 pnum
= port
->number
- serial
->minor
;
2421 /* FIXME: remove do-while(0) loop used to keep stable patch minimal.
2424 dbg("mos7840_startup: configuring port %d............", pnum
);
2425 mos7840_port
= kzalloc(sizeof(struct moschip_port
), GFP_KERNEL
);
2426 if (mos7840_port
== NULL
) {
2427 dev_err(&port
->dev
, "%s - Out of memory\n", __func__
);
2431 /* Initialize all port interrupt end point to port 0 int
2432 * endpoint. Our device has only one interrupt end point
2433 * common to all port */
2435 mos7840_port
->port
= port
;
2436 mos7840_set_port_private(port
, mos7840_port
);
2437 spin_lock_init(&mos7840_port
->pool_lock
);
2439 /* minor is not initialised until later by
2440 * usb-serial.c:get_free_serial() and cannot therefore be used
2441 * to index device instances */
2442 mos7840_port
->port_num
= pnum
+ 1;
2443 dbg("port->number = %d", port
->number
);
2444 dbg("port->serial->minor = %d", port
->serial
->minor
);
2445 dbg ("mos7840_port->port_num = %d", mos7840_port
->port_num
);
2446 dbg ("serial->minor = %d", serial
->minor
);
2448 if (mos7840_port
->port_num
== 1) {
2449 mos7840_port
->SpRegOffset
= 0x0;
2450 mos7840_port
->ControlRegOffset
= 0x1;
2451 mos7840_port
->DcrRegOffset
= 0x4;
2452 } else if ((mos7840_port
->port_num
== 2)
2453 && (serial
->num_ports
== 4)) {
2454 mos7840_port
->SpRegOffset
= 0x8;
2455 mos7840_port
->ControlRegOffset
= 0x9;
2456 mos7840_port
->DcrRegOffset
= 0x16;
2457 } else if ((mos7840_port
->port_num
== 2)
2458 && (serial
->num_ports
== 2)) {
2459 mos7840_port
->SpRegOffset
= 0xa;
2460 mos7840_port
->ControlRegOffset
= 0xb;
2461 mos7840_port
->DcrRegOffset
= 0x19;
2462 } else if ((mos7840_port
->port_num
== 3)
2463 && (serial
->num_ports
== 4)) {
2464 mos7840_port
->SpRegOffset
= 0xa;
2465 mos7840_port
->ControlRegOffset
= 0xb;
2466 mos7840_port
->DcrRegOffset
= 0x19;
2467 } else if ((mos7840_port
->port_num
== 4)
2468 && (serial
->num_ports
== 4)) {
2469 mos7840_port
->SpRegOffset
= 0xc;
2470 mos7840_port
->ControlRegOffset
= 0xd;
2471 mos7840_port
->DcrRegOffset
= 0x1c;
2473 mos7840_dump_serial_port(mos7840_port
);
2474 mos7840_set_port_private(port
, mos7840_port
);
2476 /* enable rx_disable bit in control register */
2477 status
= mos7840_get_reg_sync(port
,
2478 mos7840_port
->ControlRegOffset
, &Data
);
2480 dbg("Reading ControlReg failed status-0x%x", status
);
2483 dbg("ControlReg Reading success val is %x, status%d",
2486 Data
|= 0x08; /* setting driver done bit */
2487 Data
|= 0x04; /* sp1_bit to have cts change reflect in
2490 /* Data |= 0x20; //rx_disable bit */
2491 status
= mos7840_set_reg_sync(port
,
2492 mos7840_port
->ControlRegOffset
, Data
);
2494 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status
);
2497 dbg("ControlReg Writing success(rx_disable) status%d",
2500 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2503 status
= mos7840_set_reg_sync(port
,
2504 (__u16
) (mos7840_port
->DcrRegOffset
+ 0), Data
);
2506 dbg("Writing DCR0 failed status-0x%x", status
);
2509 dbg("DCR0 Writing success status%d", status
);
2512 status
= mos7840_set_reg_sync(port
,
2513 (__u16
) (mos7840_port
->DcrRegOffset
+ 1), Data
);
2515 dbg("Writing DCR1 failed status-0x%x", status
);
2518 dbg("DCR1 Writing success status%d", status
);
2521 status
= mos7840_set_reg_sync(port
,
2522 (__u16
) (mos7840_port
->DcrRegOffset
+ 2), Data
);
2524 dbg("Writing DCR2 failed status-0x%x", status
);
2527 dbg("DCR2 Writing success status%d", status
);
2529 /* write values in clkstart0x0 and clkmulti 0x20 */
2531 status
= mos7840_set_reg_sync(port
,
2532 CLK_START_VALUE_REGISTER
, Data
);
2534 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status
);
2537 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status
);
2540 status
= mos7840_set_reg_sync(port
,
2541 CLK_MULTI_REGISTER
, Data
);
2543 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
2547 dbg("CLK_MULTI_REGISTER Writing success status%d",
2550 /* write value 0x0 to scratchpad register */
2552 status
= mos7840_set_uart_reg(port
,
2553 SCRATCH_PAD_REGISTER
, Data
);
2555 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
2559 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
2562 /* Zero Length flag register */
2563 if ((mos7840_port
->port_num
!= 1)
2564 && (serial
->num_ports
== 2)) {
2567 status
= mos7840_set_reg_sync(port
,
2569 ((__u16
)mos7840_port
->port_num
)), Data
);
2570 dbg("ZLIP offset %x",
2572 ((__u16
) mos7840_port
->port_num
)));
2574 dbg("Writing ZLP_REG%d failed status-0x%x",
2578 dbg("ZLP_REG%d Writing success status%d",
2582 status
= mos7840_set_reg_sync(port
,
2584 ((__u16
)mos7840_port
->port_num
) - 0x1), Data
);
2585 dbg("ZLIP offset %x",
2587 ((__u16
) mos7840_port
->port_num
) - 0x1));
2589 dbg("Writing ZLP_REG%d failed status-0x%x",
2593 dbg("ZLP_REG%d Writing success status%d",
2597 mos7840_port
->control_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2598 mos7840_port
->ctrl_buf
= kmalloc(16, GFP_KERNEL
);
2599 mos7840_port
->dr
= kmalloc(sizeof(struct usb_ctrlrequest
),
2601 if (!mos7840_port
->control_urb
|| !mos7840_port
->ctrl_buf
||
2602 !mos7840_port
->dr
) {
2607 mos7840_port
->has_led
= false;
2609 /* Initialize LED timers */
2610 if (device_type
== MOSCHIP_DEVICE_ID_7810
) {
2611 mos7840_port
->has_led
= true;
2613 init_timer(&mos7840_port
->led_timer1
);
2614 mos7840_port
->led_timer1
.function
= mos7840_led_off
;
2615 mos7840_port
->led_timer1
.expires
=
2616 jiffies
+ msecs_to_jiffies(LED_ON_MS
);
2617 mos7840_port
->led_timer1
.data
=
2618 (unsigned long)mos7840_port
;
2620 init_timer(&mos7840_port
->led_timer2
);
2621 mos7840_port
->led_timer2
.function
=
2622 mos7840_led_flag_off
;
2623 mos7840_port
->led_timer2
.expires
=
2624 jiffies
+ msecs_to_jiffies(LED_OFF_MS
);
2625 mos7840_port
->led_timer2
.data
=
2626 (unsigned long)mos7840_port
;
2628 mos7840_port
->led_flag
= false;
2631 mos7840_set_led_sync(port
,
2632 MODEM_CONTROL_REGISTER
, 0x0300);
2636 if (pnum
== serial
->num_ports
- 1) {
2637 dbg("mos7840_startup: all ports configured...........");
2639 /* Zero Length flag enable */
2641 status
= mos7840_set_reg_sync(serial
->port
[0], ZLP_REG5
, Data
);
2643 dbg("Writing ZLP_REG5 failed status-0x%x", status
);
2646 dbg("ZLP_REG5 Writing success status%d", status
);
2648 /* setting configuration feature to one */
2649 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2650 0x03, 0x00, 0x01, 0x00, NULL
, 0x00,
2655 kfree(mos7840_port
->dr
);
2656 kfree(mos7840_port
->ctrl_buf
);
2657 usb_free_urb(mos7840_port
->control_urb
);
2658 kfree(mos7840_port
);
2663 static int mos7840_port_remove(struct usb_serial_port
*port
)
2665 struct moschip_port
*mos7840_port
;
2667 mos7840_port
= mos7840_get_port_private(port
);
2669 if (mos7840_port
->has_led
) {
2671 mos7840_set_led_sync(port
, MODEM_CONTROL_REGISTER
, 0x0300);
2673 del_timer_sync(&mos7840_port
->led_timer1
);
2674 del_timer_sync(&mos7840_port
->led_timer2
);
2676 usb_kill_urb(mos7840_port
->control_urb
);
2677 usb_free_urb(mos7840_port
->control_urb
);
2678 kfree(mos7840_port
->ctrl_buf
);
2679 kfree(mos7840_port
->dr
);
2680 kfree(mos7840_port
);
2685 static struct usb_serial_driver moschip7840_4port_device
= {
2687 .owner
= THIS_MODULE
,
2690 .description
= DRIVER_DESC
,
2691 .id_table
= id_table
,
2693 .open
= mos7840_open
,
2694 .close
= mos7840_close
,
2695 .write
= mos7840_write
,
2696 .write_room
= mos7840_write_room
,
2697 .chars_in_buffer
= mos7840_chars_in_buffer
,
2698 .throttle
= mos7840_throttle
,
2699 .unthrottle
= mos7840_unthrottle
,
2700 .calc_num_ports
= mos7840_calc_num_ports
,
2701 #ifdef MCSSerialProbe
2702 .probe
= mos7840_serial_probe
,
2704 .ioctl
= mos7840_ioctl
,
2705 .set_termios
= mos7840_set_termios
,
2706 .break_ctl
= mos7840_break
,
2707 .tiocmget
= mos7840_tiocmget
,
2708 .tiocmset
= mos7840_tiocmset
,
2709 .get_icount
= mos7840_get_icount
,
2710 .port_probe
= mos7840_port_probe
,
2711 .port_remove
= mos7840_port_remove
,
2712 .read_bulk_callback
= mos7840_bulk_in_callback
,
2713 .read_int_callback
= mos7840_interrupt_callback
,
2716 static struct usb_serial_driver
* const serial_drivers
[] = {
2717 &moschip7840_4port_device
, NULL
2720 module_usb_serial_driver(serial_drivers
, id_table
);
2722 MODULE_DESCRIPTION(DRIVER_DESC
);
2723 MODULE_LICENSE("GPL");
2725 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
2726 MODULE_PARM_DESC(debug
, "Debug enabled or not");