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>
38 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
41 * 16C50 UART register defines
44 #define LCR_BITS_5 0x00 /* 5 bits/char */
45 #define LCR_BITS_6 0x01 /* 6 bits/char */
46 #define LCR_BITS_7 0x02 /* 7 bits/char */
47 #define LCR_BITS_8 0x03 /* 8 bits/char */
48 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
50 #define LCR_STOP_1 0x00 /* 1 stop bit */
51 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
52 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
53 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
55 #define LCR_PAR_NONE 0x00 /* No parity */
56 #define LCR_PAR_ODD 0x08 /* Odd parity */
57 #define LCR_PAR_EVEN 0x18 /* Even parity */
58 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
59 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
60 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
62 #define LCR_SET_BREAK 0x40 /* Set Break condition */
63 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
65 #define MCR_DTR 0x01 /* Assert DTR */
66 #define MCR_RTS 0x02 /* Assert RTS */
67 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
68 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
69 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
70 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
72 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
73 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
74 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
75 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
78 * Defines used for sending commands to port
81 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
83 #define MOS_PORT1 0x0200
84 #define MOS_PORT2 0x0300
85 #define MOS_VENREG 0x0000
86 #define MOS_MAX_PORT 0x02
87 #define MOS_WRITE 0x0E
91 #define MCS_RD_RTYPE 0xC0
92 #define MCS_WR_RTYPE 0x40
93 #define MCS_RDREQ 0x0D
94 #define MCS_WRREQ 0x0E
95 #define MCS_CTRL_TIMEOUT 500
96 #define VENDOR_READ_LENGTH (0x01)
98 #define MAX_NAME_LEN 64
100 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
101 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
103 /* For higher baud Rates use TIOCEXBAUD */
104 #define TIOCEXBAUD 0x5462
106 /* vendor id and device id defines */
108 /* The native mos7840/7820 component */
109 #define USB_VENDOR_ID_MOSCHIP 0x9710
110 #define MOSCHIP_DEVICE_ID_7840 0x7840
111 #define MOSCHIP_DEVICE_ID_7820 0x7820
112 #define MOSCHIP_DEVICE_ID_7810 0x7810
113 /* The native component can have its vendor/device id's overridden
114 * in vendor-specific implementations. Such devices can be handled
115 * by making a change here, in id_table.
117 #define USB_VENDOR_ID_BANDB 0x0856
118 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
119 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
120 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
121 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
122 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
123 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
124 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
125 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
126 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
127 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
128 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
129 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
130 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
132 /* This driver also supports
133 * ATEN UC2324 device using Moschip MCS7840
134 * ATEN UC2322 device using Moschip MCS7820
136 #define USB_VENDOR_ID_ATENINTL 0x0557
137 #define ATENINTL_DEVICE_ID_UC2324 0x2011
138 #define ATENINTL_DEVICE_ID_UC2322 0x7820
140 /* Interrupt Routine Defines */
142 #define SERIAL_IIR_RLS 0x06
143 #define SERIAL_IIR_MS 0x00
146 * Emulation of the bit mask on the LINE STATUS REGISTER.
148 #define SERIAL_LSR_DR 0x0001
149 #define SERIAL_LSR_OE 0x0002
150 #define SERIAL_LSR_PE 0x0004
151 #define SERIAL_LSR_FE 0x0008
152 #define SERIAL_LSR_BI 0x0010
154 #define MOS_MSR_DELTA_CTS 0x10
155 #define MOS_MSR_DELTA_DSR 0x20
156 #define MOS_MSR_DELTA_RI 0x40
157 #define MOS_MSR_DELTA_CD 0x80
159 /* Serial Port register Address */
160 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
161 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
162 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
163 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
164 #define LINE_STATUS_REGISTER ((__u16)(0x05))
165 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
166 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
167 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
168 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
170 #define CLK_MULTI_REGISTER ((__u16)(0x02))
171 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
172 #define GPIO_REGISTER ((__u16)(0x07))
174 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
177 * URB POOL related defines
179 #define NUM_URBS 16 /* URB Count */
180 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
182 /* LED on/off milliseconds*/
183 #define LED_ON_MS 500
184 #define LED_OFF_MS 500
186 static int device_type
;
188 static const struct usb_device_id id_table
[] = {
189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
190 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7810
)},
192 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_2
)},
193 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_2P
)},
194 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_4
)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_4P
)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_US9ML2_2
)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_US9ML2_4
)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USPTL4_2
)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USPTL4_4
)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2P
)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4P
)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL2_4
)},
205 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2324
)},
206 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2322
)},
207 {} /* terminating entry */
209 MODULE_DEVICE_TABLE(usb
, id_table
);
211 /* This structure holds all of the local port information */
213 struct moschip_port
{
214 int port_num
; /*Actual port number in the device(1,2,etc) */
215 struct urb
*write_urb
; /* write URB for this port */
216 struct urb
*read_urb
; /* read URB for this port */
217 __u8 shadowLCR
; /* last LCR value received */
218 __u8 shadowMCR
; /* last MCR value received */
221 wait_queue_head_t wait_chase
; /* for handling sleeping while waiting for chase to finish */
222 struct usb_serial_port
*port
; /* loop back to the owner of this object */
226 __u8 ControlRegOffset
;
228 /* for processing control URBS in interrupt context */
229 struct urb
*control_urb
;
230 struct usb_ctrlrequest
*dr
;
234 spinlock_t pool_lock
;
235 struct urb
*write_urb_pool
[NUM_URBS
];
239 /* For device(s) with LED indicator */
242 struct timer_list led_timer1
; /* Timer for LED on */
243 struct timer_list led_timer2
; /* Timer for LED off */
247 * mos7840_set_reg_sync
248 * To set the Control register by calling usb_fill_control_urb function
249 * by passing usb_sndctrlpipe function as parameter.
252 static int mos7840_set_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
255 struct usb_device
*dev
= port
->serial
->dev
;
257 dev_dbg(&port
->dev
, "mos7840_set_reg_sync offset is %x, value %x\n", reg
, val
);
259 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
260 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
265 * mos7840_get_reg_sync
266 * To set the Uart register by calling usb_fill_control_urb function by
267 * passing usb_rcvctrlpipe function as parameter.
270 static int mos7840_get_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
273 struct usb_device
*dev
= port
->serial
->dev
;
277 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
281 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
282 MCS_RD_RTYPE
, 0, reg
, buf
, VENDOR_READ_LENGTH
,
285 dev_dbg(&port
->dev
, "%s offset is %x, return val %x\n", __func__
, reg
, *val
);
292 * mos7840_set_uart_reg
293 * To set the Uart register by calling usb_fill_control_urb function by
294 * passing usb_sndctrlpipe function as parameter.
297 static int mos7840_set_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
301 struct usb_device
*dev
= port
->serial
->dev
;
303 /* For the UART control registers, the application number need
305 if (port
->serial
->num_ports
== 4) {
306 val
|= (((__u16
) port
->number
-
307 (__u16
) (port
->serial
->minor
)) + 1) << 8;
309 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
310 val
|= (((__u16
) port
->number
-
311 (__u16
) (port
->serial
->minor
)) + 1) << 8;
313 val
|= (((__u16
) port
->number
-
314 (__u16
) (port
->serial
->minor
)) + 2) << 8;
317 dev_dbg(&port
->dev
, "%s application number is %x\n", __func__
, val
);
318 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
319 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
325 * mos7840_get_uart_reg
326 * To set the Control register by calling usb_fill_control_urb function
327 * by passing usb_rcvctrlpipe function as parameter.
329 static int mos7840_get_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
332 struct usb_device
*dev
= port
->serial
->dev
;
337 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
341 /* Wval is same as application number */
342 if (port
->serial
->num_ports
== 4) {
344 (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) +
347 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
348 Wval
= (((__u16
) port
->number
-
349 (__u16
) (port
->serial
->minor
)) + 1) << 8;
351 Wval
= (((__u16
) port
->number
-
352 (__u16
) (port
->serial
->minor
)) + 2) << 8;
355 dev_dbg(&port
->dev
, "%s application number is %x\n", __func__
, Wval
);
356 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
357 MCS_RD_RTYPE
, Wval
, reg
, buf
, VENDOR_READ_LENGTH
,
365 static void mos7840_dump_serial_port(struct usb_serial_port
*port
,
366 struct moschip_port
*mos7840_port
)
369 dev_dbg(&port
->dev
, "SpRegOffset is %2x\n", mos7840_port
->SpRegOffset
);
370 dev_dbg(&port
->dev
, "ControlRegOffset is %2x\n", mos7840_port
->ControlRegOffset
);
371 dev_dbg(&port
->dev
, "DCRRegOffset is %2x\n", mos7840_port
->DcrRegOffset
);
375 /************************************************************************/
376 /************************************************************************/
377 /* I N T E R F A C E F U N C T I O N S */
378 /* I N T E R F A C E F U N C T I O N S */
379 /************************************************************************/
380 /************************************************************************/
382 static inline void mos7840_set_port_private(struct usb_serial_port
*port
,
383 struct moschip_port
*data
)
385 usb_set_serial_port_data(port
, (void *)data
);
388 static inline struct moschip_port
*mos7840_get_port_private(struct
392 return (struct moschip_port
*)usb_get_serial_port_data(port
);
395 static void mos7840_handle_new_msr(struct moschip_port
*port
, __u8 new_msr
)
397 struct moschip_port
*mos7840_port
;
398 struct async_icount
*icount
;
401 (MOS_MSR_DELTA_CTS
| MOS_MSR_DELTA_DSR
| MOS_MSR_DELTA_RI
|
403 icount
= &mos7840_port
->port
->icount
;
405 /* update input line counters */
406 if (new_msr
& MOS_MSR_DELTA_CTS
)
408 if (new_msr
& MOS_MSR_DELTA_DSR
)
410 if (new_msr
& MOS_MSR_DELTA_CD
)
412 if (new_msr
& MOS_MSR_DELTA_RI
)
415 wake_up_interruptible(&port
->port
->port
.delta_msr_wait
);
419 static void mos7840_handle_new_lsr(struct moschip_port
*port
, __u8 new_lsr
)
421 struct async_icount
*icount
;
423 if (new_lsr
& SERIAL_LSR_BI
) {
425 * Parity and Framing errors only count if they
426 * occur exclusive of a break being
429 new_lsr
&= (__u8
) (SERIAL_LSR_OE
| SERIAL_LSR_BI
);
432 /* update input line counters */
433 icount
= &port
->port
->icount
;
434 if (new_lsr
& SERIAL_LSR_BI
)
436 if (new_lsr
& SERIAL_LSR_OE
)
438 if (new_lsr
& SERIAL_LSR_PE
)
440 if (new_lsr
& SERIAL_LSR_FE
)
444 /************************************************************************/
445 /************************************************************************/
446 /* U S B C A L L B A C K F U N C T I O N S */
447 /* U S B C A L L B A C K F U N C T I O N S */
448 /************************************************************************/
449 /************************************************************************/
451 static void mos7840_control_callback(struct urb
*urb
)
454 struct moschip_port
*mos7840_port
;
455 struct device
*dev
= &urb
->dev
->dev
;
457 int status
= urb
->status
;
459 mos7840_port
= urb
->context
;
468 /* this urb is terminated, clean up */
469 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
472 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
476 dev_dbg(dev
, "%s urb buffer size is %d\n", __func__
, urb
->actual_length
);
477 dev_dbg(dev
, "%s mos7840_port->MsrLsr is %d port %d\n", __func__
,
478 mos7840_port
->MsrLsr
, mos7840_port
->port_num
);
479 data
= urb
->transfer_buffer
;
480 regval
= (__u8
) data
[0];
481 dev_dbg(dev
, "%s data is %x\n", __func__
, regval
);
482 if (mos7840_port
->MsrLsr
== 0)
483 mos7840_handle_new_msr(mos7840_port
, regval
);
484 else if (mos7840_port
->MsrLsr
== 1)
485 mos7840_handle_new_lsr(mos7840_port
, regval
);
488 static int mos7840_get_reg(struct moschip_port
*mcs
, __u16 Wval
, __u16 reg
,
491 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
492 struct usb_ctrlrequest
*dr
= mcs
->dr
;
493 unsigned char *buffer
= mcs
->ctrl_buf
;
496 dr
->bRequestType
= MCS_RD_RTYPE
;
497 dr
->bRequest
= MCS_RDREQ
;
498 dr
->wValue
= cpu_to_le16(Wval
); /* 0 */
499 dr
->wIndex
= cpu_to_le16(reg
);
500 dr
->wLength
= cpu_to_le16(2);
502 usb_fill_control_urb(mcs
->control_urb
, dev
, usb_rcvctrlpipe(dev
, 0),
503 (unsigned char *)dr
, buffer
, 2,
504 mos7840_control_callback
, mcs
);
505 mcs
->control_urb
->transfer_buffer_length
= 2;
506 ret
= usb_submit_urb(mcs
->control_urb
, GFP_ATOMIC
);
510 static void mos7840_set_led_callback(struct urb
*urb
)
512 switch (urb
->status
) {
519 /* This urb is terminated, clean up */
520 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d",
521 __func__
, urb
->status
);
524 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d",
525 __func__
, urb
->status
);
529 static void mos7840_set_led_async(struct moschip_port
*mcs
, __u16 wval
,
532 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
533 struct usb_ctrlrequest
*dr
= mcs
->dr
;
535 dr
->bRequestType
= MCS_WR_RTYPE
;
536 dr
->bRequest
= MCS_WRREQ
;
537 dr
->wValue
= cpu_to_le16(wval
);
538 dr
->wIndex
= cpu_to_le16(reg
);
539 dr
->wLength
= cpu_to_le16(0);
541 usb_fill_control_urb(mcs
->control_urb
, dev
, usb_sndctrlpipe(dev
, 0),
542 (unsigned char *)dr
, NULL
, 0, mos7840_set_led_callback
, NULL
);
544 usb_submit_urb(mcs
->control_urb
, GFP_ATOMIC
);
547 static void mos7840_set_led_sync(struct usb_serial_port
*port
, __u16 reg
,
550 struct usb_device
*dev
= port
->serial
->dev
;
552 usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
, MCS_WR_RTYPE
,
553 val
, reg
, NULL
, 0, MOS_WDR_TIMEOUT
);
556 static void mos7840_led_off(unsigned long arg
)
558 struct moschip_port
*mcs
= (struct moschip_port
*) arg
;
561 mos7840_set_led_async(mcs
, 0x0300, MODEM_CONTROL_REGISTER
);
562 mod_timer(&mcs
->led_timer2
,
563 jiffies
+ msecs_to_jiffies(LED_OFF_MS
));
566 static void mos7840_led_flag_off(unsigned long arg
)
568 struct moschip_port
*mcs
= (struct moschip_port
*) arg
;
570 mcs
->led_flag
= false;
573 /*****************************************************************************
574 * mos7840_interrupt_callback
575 * this is the callback function for when we have received data on the
576 * interrupt endpoint.
577 *****************************************************************************/
579 static void mos7840_interrupt_callback(struct urb
*urb
)
583 struct moschip_port
*mos7840_port
;
584 struct usb_serial
*serial
;
589 __u16 wval
, wreg
= 0;
590 int status
= urb
->status
;
599 /* this urb is terminated, clean up */
600 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d\n",
604 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n",
609 length
= urb
->actual_length
;
610 data
= urb
->transfer_buffer
;
612 serial
= urb
->context
;
614 /* Moschip get 5 bytes
615 * Byte 1 IIR Port 1 (port.number is 0)
616 * Byte 2 IIR Port 2 (port.number is 1)
617 * Byte 3 IIR Port 3 (port.number is 2)
618 * Byte 4 IIR Port 4 (port.number is 3)
619 * Byte 5 FIFO status for both */
621 if (length
&& length
> 5) {
622 dev_dbg(&urb
->dev
->dev
, "%s", "Wrong data !!!\n");
626 sp
[0] = (__u8
) data
[0];
627 sp
[1] = (__u8
) data
[1];
628 sp
[2] = (__u8
) data
[2];
629 sp
[3] = (__u8
) data
[3];
632 for (i
= 0; i
< serial
->num_ports
; i
++) {
633 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
635 (((__u16
) serial
->port
[i
]->number
-
636 (__u16
) (serial
->minor
)) + 1) << 8;
637 if (mos7840_port
->open
) {
639 dev_dbg(&urb
->dev
->dev
, "SP%d No Interrupt !!!\n", i
);
641 switch (sp
[i
] & 0x0f) {
643 dev_dbg(&urb
->dev
->dev
, "Serial Port %d: Receiver status error or \n", i
);
644 dev_dbg(&urb
->dev
->dev
, "address bit detected in 9-bit mode\n");
645 mos7840_port
->MsrLsr
= 1;
646 wreg
= LINE_STATUS_REGISTER
;
649 dev_dbg(&urb
->dev
->dev
, "Serial Port %d: Modem status change\n", i
);
650 mos7840_port
->MsrLsr
= 0;
651 wreg
= MODEM_STATUS_REGISTER
;
654 rv
= mos7840_get_reg(mos7840_port
, wval
, wreg
, &Data
);
659 /* the completion handler for the control urb will resubmit */
662 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
664 dev_err(&urb
->dev
->dev
,
665 "%s - Error %d submitting interrupt urb\n",
670 static int mos7840_port_paranoia_check(struct usb_serial_port
*port
,
671 const char *function
)
674 pr_debug("%s - port == NULL\n", function
);
678 pr_debug("%s - port->serial == NULL\n", function
);
685 /* Inline functions to check the sanity of a pointer that is passed to us */
686 static int mos7840_serial_paranoia_check(struct usb_serial
*serial
,
687 const char *function
)
690 pr_debug("%s - serial == NULL\n", function
);
694 pr_debug("%s - serial->type == NULL!\n", function
);
701 static struct usb_serial
*mos7840_get_usb_serial(struct usb_serial_port
*port
,
702 const char *function
)
704 /* if no port was specified, or it fails a paranoia check */
706 mos7840_port_paranoia_check(port
, function
) ||
707 mos7840_serial_paranoia_check(port
->serial
, function
)) {
708 /* then say that we don't have a valid usb_serial thing,
709 * which will end up genrating -ENODEV return values */
716 /*****************************************************************************
717 * mos7840_bulk_in_callback
718 * this is the callback function for when we have received data on the
720 *****************************************************************************/
722 static void mos7840_bulk_in_callback(struct urb
*urb
)
726 struct usb_serial
*serial
;
727 struct usb_serial_port
*port
;
728 struct moschip_port
*mos7840_port
;
729 int status
= urb
->status
;
731 mos7840_port
= urb
->context
;
736 dev_dbg(&urb
->dev
->dev
, "nonzero read bulk status received: %d\n", status
);
737 mos7840_port
->read_urb_busy
= false;
741 port
= mos7840_port
->port
;
742 if (mos7840_port_paranoia_check(port
, __func__
)) {
743 mos7840_port
->read_urb_busy
= false;
747 serial
= mos7840_get_usb_serial(port
, __func__
);
749 mos7840_port
->read_urb_busy
= false;
753 data
= urb
->transfer_buffer
;
754 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
756 if (urb
->actual_length
) {
757 struct tty_port
*tport
= &mos7840_port
->port
->port
;
758 tty_insert_flip_string(tport
, data
, urb
->actual_length
);
759 tty_flip_buffer_push(tport
);
760 port
->icount
.rx
+= urb
->actual_length
;
761 dev_dbg(&port
->dev
, "icount.rx is %d:\n", port
->icount
.rx
);
764 if (!mos7840_port
->read_urb
) {
765 dev_dbg(&port
->dev
, "%s", "URB KILLED !!!\n");
766 mos7840_port
->read_urb_busy
= false;
771 if (mos7840_port
->has_led
&& !mos7840_port
->led_flag
) {
772 mos7840_port
->led_flag
= true;
773 mos7840_set_led_async(mos7840_port
, 0x0301,
774 MODEM_CONTROL_REGISTER
);
775 mod_timer(&mos7840_port
->led_timer1
,
776 jiffies
+ msecs_to_jiffies(LED_ON_MS
));
779 mos7840_port
->read_urb_busy
= true;
780 retval
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
783 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, retval = %d\n", retval
);
784 mos7840_port
->read_urb_busy
= false;
788 /*****************************************************************************
789 * mos7840_bulk_out_data_callback
790 * this is the callback function for when we have finished sending
791 * serial data on the bulk out endpoint.
792 *****************************************************************************/
794 static void mos7840_bulk_out_data_callback(struct urb
*urb
)
796 struct moschip_port
*mos7840_port
;
797 struct usb_serial_port
*port
;
798 int status
= urb
->status
;
801 mos7840_port
= urb
->context
;
802 port
= mos7840_port
->port
;
803 spin_lock(&mos7840_port
->pool_lock
);
804 for (i
= 0; i
< NUM_URBS
; i
++) {
805 if (urb
== mos7840_port
->write_urb_pool
[i
]) {
806 mos7840_port
->busy
[i
] = 0;
810 spin_unlock(&mos7840_port
->pool_lock
);
813 dev_dbg(&port
->dev
, "nonzero write bulk status received:%d\n", status
);
817 if (mos7840_port_paranoia_check(port
, __func__
))
820 if (mos7840_port
->open
)
821 tty_port_tty_wakeup(&port
->port
);
825 /************************************************************************/
826 /* 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 */
827 /************************************************************************/
828 #ifdef MCSSerialProbe
829 static int mos7840_serial_probe(struct usb_serial
*serial
,
830 const struct usb_device_id
*id
)
833 /*need to implement the mode_reg reading and updating\
834 structures usb_serial_ device_type\
835 (i.e num_ports, num_bulkin,bulkout etc) */
836 /* Also we can update the changes attach */
841 /*****************************************************************************
843 * this function is called by the tty driver when a port is opened
844 * If successful, we return 0
845 * Otherwise we return a negative error number.
846 *****************************************************************************/
848 static int mos7840_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
852 struct usb_serial
*serial
;
856 struct moschip_port
*mos7840_port
;
857 struct moschip_port
*port0
;
859 if (mos7840_port_paranoia_check(port
, __func__
))
862 serial
= port
->serial
;
864 if (mos7840_serial_paranoia_check(serial
, __func__
))
867 mos7840_port
= mos7840_get_port_private(port
);
868 port0
= mos7840_get_port_private(serial
->port
[0]);
870 if (mos7840_port
== NULL
|| port0
== NULL
)
873 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
874 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
877 /* Initialising the write urb pool */
878 for (j
= 0; j
< NUM_URBS
; ++j
) {
879 urb
= usb_alloc_urb(0, GFP_KERNEL
);
880 mos7840_port
->write_urb_pool
[j
] = urb
;
883 dev_err(&port
->dev
, "No more urbs???\n");
887 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
889 if (!urb
->transfer_buffer
) {
891 mos7840_port
->write_urb_pool
[j
] = NULL
;
893 "%s-out of memory for urb buffers.\n",
899 /*****************************************************************************
900 * Initialize MCS7840 -- Write Init values to corresponding Registers
908 * 0x08 : SP1/2 Control Reg
909 *****************************************************************************/
911 /* NEED to check the following Block */
914 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
916 dev_dbg(&port
->dev
, "Reading Spreg failed\n");
920 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
922 dev_dbg(&port
->dev
, "writing Spreg failed\n");
927 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
929 dev_dbg(&port
->dev
, "writing Spreg failed\n");
932 /* End of block to be checked */
935 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
938 dev_dbg(&port
->dev
, "Reading Controlreg failed\n");
941 Data
|= 0x08; /* Driver done bit */
942 Data
|= 0x20; /* rx_disable */
943 status
= mos7840_set_reg_sync(port
,
944 mos7840_port
->ControlRegOffset
, Data
);
946 dev_dbg(&port
->dev
, "writing Controlreg failed\n");
949 /* do register settings here */
950 /* Set all regs to the device default values. */
951 /***********************************
952 * First Disable all interrupts.
953 ***********************************/
955 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
957 dev_dbg(&port
->dev
, "disabling interrupts failed\n");
960 /* Set FIFO_CONTROL_REGISTER to the default value */
962 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
964 dev_dbg(&port
->dev
, "Writing FIFO_CONTROL_REGISTER failed\n");
969 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
971 dev_dbg(&port
->dev
, "Writing FIFO_CONTROL_REGISTER failed\n");
976 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
977 mos7840_port
->shadowLCR
= Data
;
980 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
981 mos7840_port
->shadowMCR
= Data
;
984 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
985 mos7840_port
->shadowLCR
= Data
;
987 Data
|= SERIAL_LCR_DLAB
; /* data latch enable in LCR 0x80 */
988 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
991 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
994 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
997 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
999 Data
= Data
& ~SERIAL_LCR_DLAB
;
1000 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1001 mos7840_port
->shadowLCR
= Data
;
1003 /* clearing Bulkin and Bulkout Fifo */
1005 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
1008 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1010 Data
= Data
& ~0x0c;
1011 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1012 /* Finally enable all interrupts */
1014 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1016 /* clearing rx_disable */
1018 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1020 Data
= Data
& ~0x20;
1021 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1026 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1029 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1032 /* Check to see if we've set up our endpoint info yet *
1033 * (can't set it up in mos7840_startup as the structures *
1034 * were not set up at that time.) */
1035 if (port0
->open_ports
== 1) {
1036 if (serial
->port
[0]->interrupt_in_buffer
== NULL
) {
1037 /* set up interrupt urb */
1038 usb_fill_int_urb(serial
->port
[0]->interrupt_in_urb
,
1040 usb_rcvintpipe(serial
->dev
,
1041 serial
->port
[0]->interrupt_in_endpointAddress
),
1042 serial
->port
[0]->interrupt_in_buffer
,
1043 serial
->port
[0]->interrupt_in_urb
->
1044 transfer_buffer_length
,
1045 mos7840_interrupt_callback
,
1047 serial
->port
[0]->interrupt_in_urb
->interval
);
1049 /* start interrupt read for mos7840 *
1050 * will continue as long as mos7840 is connected */
1053 usb_submit_urb(serial
->port
[0]->interrupt_in_urb
,
1056 dev_err(&port
->dev
, "%s - Error %d submitting "
1057 "interrupt urb\n", __func__
, response
);
1064 /* see if we've set up our endpoint info yet *
1065 * (can't set it up in mos7840_startup as the *
1066 * structures were not set up at that time.) */
1068 dev_dbg(&port
->dev
, "port number is %d\n", port
->number
);
1069 dev_dbg(&port
->dev
, "serial number is %d\n", port
->serial
->minor
);
1070 dev_dbg(&port
->dev
, "Bulkin endpoint is %d\n", port
->bulk_in_endpointAddress
);
1071 dev_dbg(&port
->dev
, "BulkOut endpoint is %d\n", port
->bulk_out_endpointAddress
);
1072 dev_dbg(&port
->dev
, "Interrupt endpoint is %d\n", port
->interrupt_in_endpointAddress
);
1073 dev_dbg(&port
->dev
, "port's number in the device is %d\n", mos7840_port
->port_num
);
1074 mos7840_port
->read_urb
= port
->read_urb
;
1076 /* set up our bulk in urb */
1077 if ((serial
->num_ports
== 2)
1078 && ((((__u16
)port
->number
-
1079 (__u16
)(port
->serial
->minor
)) % 2) != 0)) {
1080 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1082 usb_rcvbulkpipe(serial
->dev
,
1083 (port
->bulk_in_endpointAddress
) + 2),
1084 port
->bulk_in_buffer
,
1085 mos7840_port
->read_urb
->transfer_buffer_length
,
1086 mos7840_bulk_in_callback
, mos7840_port
);
1088 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1090 usb_rcvbulkpipe(serial
->dev
,
1091 port
->bulk_in_endpointAddress
),
1092 port
->bulk_in_buffer
,
1093 mos7840_port
->read_urb
->transfer_buffer_length
,
1094 mos7840_bulk_in_callback
, mos7840_port
);
1097 dev_dbg(&port
->dev
, "%s: bulkin endpoint is %d\n", __func__
, port
->bulk_in_endpointAddress
);
1098 mos7840_port
->read_urb_busy
= true;
1099 response
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1101 dev_err(&port
->dev
, "%s - Error %d submitting control urb\n",
1102 __func__
, response
);
1103 mos7840_port
->read_urb_busy
= false;
1106 /* initialize our wait queues */
1107 init_waitqueue_head(&mos7840_port
->wait_chase
);
1109 /* initialize our port settings */
1110 /* Must set to enable ints! */
1111 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
1112 /* send a open port command */
1113 mos7840_port
->open
= 1;
1114 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1119 /*****************************************************************************
1120 * mos7840_chars_in_buffer
1121 * this function is called by the tty driver when it wants to know how many
1122 * bytes of data we currently have outstanding in the port (data that has
1123 * been written, but hasn't made it out the port yet)
1124 * If successful, we return the number of bytes left to be written in the
1126 * Otherwise we return zero.
1127 *****************************************************************************/
1129 static int mos7840_chars_in_buffer(struct tty_struct
*tty
)
1131 struct usb_serial_port
*port
= tty
->driver_data
;
1134 unsigned long flags
;
1135 struct moschip_port
*mos7840_port
;
1137 if (mos7840_port_paranoia_check(port
, __func__
))
1140 mos7840_port
= mos7840_get_port_private(port
);
1141 if (mos7840_port
== NULL
)
1144 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1145 for (i
= 0; i
< NUM_URBS
; ++i
) {
1146 if (mos7840_port
->busy
[i
]) {
1147 struct urb
*urb
= mos7840_port
->write_urb_pool
[i
];
1148 chars
+= urb
->transfer_buffer_length
;
1151 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1152 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
1157 /*****************************************************************************
1159 * this function is called by the tty driver when a port is closed
1160 *****************************************************************************/
1162 static void mos7840_close(struct usb_serial_port
*port
)
1164 struct usb_serial
*serial
;
1165 struct moschip_port
*mos7840_port
;
1166 struct moschip_port
*port0
;
1170 if (mos7840_port_paranoia_check(port
, __func__
))
1173 serial
= mos7840_get_usb_serial(port
, __func__
);
1177 mos7840_port
= mos7840_get_port_private(port
);
1178 port0
= mos7840_get_port_private(serial
->port
[0]);
1180 if (mos7840_port
== NULL
|| port0
== NULL
)
1183 for (j
= 0; j
< NUM_URBS
; ++j
)
1184 usb_kill_urb(mos7840_port
->write_urb_pool
[j
]);
1186 /* Freeing Write URBs */
1187 for (j
= 0; j
< NUM_URBS
; ++j
) {
1188 if (mos7840_port
->write_urb_pool
[j
]) {
1189 if (mos7840_port
->write_urb_pool
[j
]->transfer_buffer
)
1190 kfree(mos7840_port
->write_urb_pool
[j
]->
1193 usb_free_urb(mos7840_port
->write_urb_pool
[j
]);
1197 usb_kill_urb(mos7840_port
->write_urb
);
1198 usb_kill_urb(mos7840_port
->read_urb
);
1199 mos7840_port
->read_urb_busy
= false;
1201 port0
->open_ports
--;
1202 dev_dbg(&port
->dev
, "%s in close%d:in port%d\n", __func__
, port0
->open_ports
, port
->number
);
1203 if (port0
->open_ports
== 0) {
1204 if (serial
->port
[0]->interrupt_in_urb
) {
1205 dev_dbg(&port
->dev
, "Shutdown interrupt_in_urb\n");
1206 usb_kill_urb(serial
->port
[0]->interrupt_in_urb
);
1210 if (mos7840_port
->write_urb
) {
1211 /* if this urb had a transfer buffer already (old tx) free it */
1212 kfree(mos7840_port
->write_urb
->transfer_buffer
);
1213 usb_free_urb(mos7840_port
->write_urb
);
1217 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1220 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1222 mos7840_port
->open
= 0;
1225 /************************************************************************
1227 * mos7840_block_until_chase_response
1229 * This function will block the close until one of the following:
1230 * 1. Response to our Chase comes from mos7840
1231 * 2. A timeout of 10 seconds without activity has expired
1232 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1234 ************************************************************************/
1236 static void mos7840_block_until_chase_response(struct tty_struct
*tty
,
1237 struct moschip_port
*mos7840_port
)
1239 int timeout
= msecs_to_jiffies(1000);
1244 count
= mos7840_chars_in_buffer(tty
);
1246 /* Check for Buffer status */
1250 /* Block the thread for a while */
1251 interruptible_sleep_on_timeout(&mos7840_port
->wait_chase
,
1253 /* No activity.. count down section */
1256 dev_dbg(&mos7840_port
->port
->dev
, "%s - TIMEOUT\n", __func__
);
1259 /* Reset timeout value back to seconds */
1266 /*****************************************************************************
1268 * this function sends a break to the port
1269 *****************************************************************************/
1270 static void mos7840_break(struct tty_struct
*tty
, int break_state
)
1272 struct usb_serial_port
*port
= tty
->driver_data
;
1274 struct usb_serial
*serial
;
1275 struct moschip_port
*mos7840_port
;
1277 if (mos7840_port_paranoia_check(port
, __func__
))
1280 serial
= mos7840_get_usb_serial(port
, __func__
);
1284 mos7840_port
= mos7840_get_port_private(port
);
1286 if (mos7840_port
== NULL
)
1289 /* flush and block until tx is empty */
1290 mos7840_block_until_chase_response(tty
, mos7840_port
);
1292 if (break_state
== -1)
1293 data
= mos7840_port
->shadowLCR
| LCR_SET_BREAK
;
1295 data
= mos7840_port
->shadowLCR
& ~LCR_SET_BREAK
;
1297 /* FIXME: no locking on shadowLCR anywhere in driver */
1298 mos7840_port
->shadowLCR
= data
;
1299 dev_dbg(&port
->dev
, "%s mos7840_port->shadowLCR is %x\n", __func__
, mos7840_port
->shadowLCR
);
1300 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
,
1301 mos7840_port
->shadowLCR
);
1304 /*****************************************************************************
1305 * mos7840_write_room
1306 * this function is called by the tty driver when it wants to know how many
1307 * bytes of data we can accept for a specific port.
1308 * If successful, we return the amount of room that we have for this port
1309 * Otherwise we return a negative error number.
1310 *****************************************************************************/
1312 static int mos7840_write_room(struct tty_struct
*tty
)
1314 struct usb_serial_port
*port
= tty
->driver_data
;
1317 unsigned long flags
;
1318 struct moschip_port
*mos7840_port
;
1320 if (mos7840_port_paranoia_check(port
, __func__
))
1323 mos7840_port
= mos7840_get_port_private(port
);
1324 if (mos7840_port
== NULL
)
1327 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1328 for (i
= 0; i
< NUM_URBS
; ++i
) {
1329 if (!mos7840_port
->busy
[i
])
1330 room
+= URB_TRANSFER_BUFFER_SIZE
;
1332 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1334 room
= (room
== 0) ? 0 : room
- URB_TRANSFER_BUFFER_SIZE
+ 1;
1335 dev_dbg(&mos7840_port
->port
->dev
, "%s - returns %d\n", __func__
, room
);
1340 /*****************************************************************************
1342 * this function is called by the tty driver when data should be written to
1344 * If successful, we return the number of bytes written, otherwise we
1345 * return a negative error number.
1346 *****************************************************************************/
1348 static int mos7840_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1349 const unsigned char *data
, int count
)
1355 unsigned long flags
;
1357 struct moschip_port
*mos7840_port
;
1358 struct usb_serial
*serial
;
1361 const unsigned char *current_position
= data
;
1362 unsigned char *data1
;
1366 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
1367 mos7840_port
->shadowLCR
= Data
;
1368 dev_dbg(&port
->dev
, "%s: LINE_CONTROL_REGISTER is %x\n", __func__
, Data
);
1369 dev_dbg(&port
->dev
, "%s: mos7840_port->shadowLCR is %x\n", __func__
, mos7840_port
->shadowLCR
);
1372 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1373 /* mos7840_port->shadowLCR=Data;//Need to add later */
1375 Data
|= SERIAL_LCR_DLAB
; /* data latch enable in LCR 0x80 */
1376 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1379 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1381 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_LSB
, &Data
);
1382 dev_dbg(&port
->dev
, "%s: DLL value is %x\n", __func__
, Data
);
1385 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_MSB
, &Data
);
1386 dev_dbg(&port
->dev
, "%s: DLM value is %x\n", __func__
, Data
);
1388 Data
= Data
& ~SERIAL_LCR_DLAB
;
1389 dev_dbg(&port
->dev
, "%s: mos7840_port->shadowLCR is %x\n", __func__
, mos7840_port
->shadowLCR
);
1390 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1393 if (mos7840_port_paranoia_check(port
, __func__
))
1396 serial
= port
->serial
;
1397 if (mos7840_serial_paranoia_check(serial
, __func__
))
1400 mos7840_port
= mos7840_get_port_private(port
);
1401 if (mos7840_port
== NULL
)
1404 /* try to find a free urb in the list */
1407 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1408 for (i
= 0; i
< NUM_URBS
; ++i
) {
1409 if (!mos7840_port
->busy
[i
]) {
1410 mos7840_port
->busy
[i
] = 1;
1411 urb
= mos7840_port
->write_urb_pool
[i
];
1412 dev_dbg(&port
->dev
, "URB:%d\n", i
);
1416 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1419 dev_dbg(&port
->dev
, "%s - no more free urbs\n", __func__
);
1423 if (urb
->transfer_buffer
== NULL
) {
1424 urb
->transfer_buffer
=
1425 kmalloc(URB_TRANSFER_BUFFER_SIZE
, GFP_KERNEL
);
1427 if (urb
->transfer_buffer
== NULL
) {
1428 dev_err_console(port
, "%s no more kernel memory...\n",
1433 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1435 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1437 /* fill urb with data and submit */
1438 if ((serial
->num_ports
== 2)
1439 && ((((__u16
)port
->number
-
1440 (__u16
)(port
->serial
->minor
)) % 2) != 0)) {
1441 usb_fill_bulk_urb(urb
,
1443 usb_sndbulkpipe(serial
->dev
,
1444 (port
->bulk_out_endpointAddress
) + 2),
1445 urb
->transfer_buffer
,
1447 mos7840_bulk_out_data_callback
, mos7840_port
);
1449 usb_fill_bulk_urb(urb
,
1451 usb_sndbulkpipe(serial
->dev
,
1452 port
->bulk_out_endpointAddress
),
1453 urb
->transfer_buffer
,
1455 mos7840_bulk_out_data_callback
, mos7840_port
);
1458 data1
= urb
->transfer_buffer
;
1459 dev_dbg(&port
->dev
, "bulkout endpoint is %d\n", port
->bulk_out_endpointAddress
);
1462 if (mos7840_port
->has_led
&& !mos7840_port
->led_flag
) {
1463 mos7840_port
->led_flag
= true;
1464 mos7840_set_led_sync(port
, MODEM_CONTROL_REGISTER
, 0x0301);
1465 mod_timer(&mos7840_port
->led_timer1
,
1466 jiffies
+ msecs_to_jiffies(LED_ON_MS
));
1469 /* send it down the pipe */
1470 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1473 mos7840_port
->busy
[i
] = 0;
1474 dev_err_console(port
, "%s - usb_submit_urb(write bulk) failed "
1475 "with status = %d\n", __func__
, status
);
1476 bytes_sent
= status
;
1479 bytes_sent
= transfer_size
;
1480 port
->icount
.tx
+= transfer_size
;
1481 dev_dbg(&port
->dev
, "icount.tx is %d:\n", port
->icount
.tx
);
1487 /*****************************************************************************
1489 * this function is called by the tty driver when it wants to stop the data
1490 * being read from the port.
1491 *****************************************************************************/
1493 static void mos7840_throttle(struct tty_struct
*tty
)
1495 struct usb_serial_port
*port
= tty
->driver_data
;
1496 struct moschip_port
*mos7840_port
;
1499 if (mos7840_port_paranoia_check(port
, __func__
))
1502 mos7840_port
= mos7840_get_port_private(port
);
1504 if (mos7840_port
== NULL
)
1507 if (!mos7840_port
->open
) {
1508 dev_dbg(&port
->dev
, "%s", "port not opened\n");
1512 /* if we are implementing XON/XOFF, send the stop character */
1514 unsigned char stop_char
= STOP_CHAR(tty
);
1515 status
= mos7840_write(tty
, port
, &stop_char
, 1);
1519 /* if we are implementing RTS/CTS, toggle that line */
1520 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1521 mos7840_port
->shadowMCR
&= ~MCR_RTS
;
1522 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1523 mos7840_port
->shadowMCR
);
1529 /*****************************************************************************
1530 * mos7840_unthrottle
1531 * this function is called by the tty driver when it wants to resume
1532 * the data being read from the port (called after mos7840_throttle is
1534 *****************************************************************************/
1535 static void mos7840_unthrottle(struct tty_struct
*tty
)
1537 struct usb_serial_port
*port
= tty
->driver_data
;
1539 struct moschip_port
*mos7840_port
= mos7840_get_port_private(port
);
1541 if (mos7840_port_paranoia_check(port
, __func__
))
1544 if (mos7840_port
== NULL
)
1547 if (!mos7840_port
->open
) {
1548 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1552 /* if we are implementing XON/XOFF, send the start character */
1554 unsigned char start_char
= START_CHAR(tty
);
1555 status
= mos7840_write(tty
, port
, &start_char
, 1);
1560 /* if we are implementing RTS/CTS, toggle that line */
1561 if (tty
->termios
.c_cflag
& CRTSCTS
) {
1562 mos7840_port
->shadowMCR
|= MCR_RTS
;
1563 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1564 mos7840_port
->shadowMCR
);
1570 static int mos7840_tiocmget(struct tty_struct
*tty
)
1572 struct usb_serial_port
*port
= tty
->driver_data
;
1573 struct moschip_port
*mos7840_port
;
1574 unsigned int result
;
1578 mos7840_port
= mos7840_get_port_private(port
);
1580 if (mos7840_port
== NULL
)
1583 status
= mos7840_get_uart_reg(port
, MODEM_STATUS_REGISTER
, &msr
);
1584 status
= mos7840_get_uart_reg(port
, MODEM_CONTROL_REGISTER
, &mcr
);
1585 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0)
1586 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0)
1587 | ((mcr
& MCR_LOOPBACK
) ? TIOCM_LOOP
: 0)
1588 | ((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0)
1589 | ((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0)
1590 | ((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0)
1591 | ((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0);
1593 dev_dbg(&port
->dev
, "%s - 0x%04X\n", __func__
, result
);
1598 static int mos7840_tiocmset(struct tty_struct
*tty
,
1599 unsigned int set
, unsigned int clear
)
1601 struct usb_serial_port
*port
= tty
->driver_data
;
1602 struct moschip_port
*mos7840_port
;
1606 mos7840_port
= mos7840_get_port_private(port
);
1608 if (mos7840_port
== NULL
)
1611 /* FIXME: What locks the port registers ? */
1612 mcr
= mos7840_port
->shadowMCR
;
1613 if (clear
& TIOCM_RTS
)
1615 if (clear
& TIOCM_DTR
)
1617 if (clear
& TIOCM_LOOP
)
1618 mcr
&= ~MCR_LOOPBACK
;
1620 if (set
& TIOCM_RTS
)
1622 if (set
& TIOCM_DTR
)
1624 if (set
& TIOCM_LOOP
)
1625 mcr
|= MCR_LOOPBACK
;
1627 mos7840_port
->shadowMCR
= mcr
;
1629 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, mcr
);
1631 dev_dbg(&port
->dev
, "setting MODEM_CONTROL_REGISTER Failed\n");
1638 /*****************************************************************************
1639 * mos7840_calc_baud_rate_divisor
1640 * this function calculates the proper baud rate divisor for the specified
1642 *****************************************************************************/
1643 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port
*port
,
1644 int baudRate
, int *divisor
,
1647 dev_dbg(&port
->dev
, "%s - %d\n", __func__
, baudRate
);
1649 if (baudRate
<= 115200) {
1650 *divisor
= 115200 / baudRate
;
1653 if ((baudRate
> 115200) && (baudRate
<= 230400)) {
1654 *divisor
= 230400 / baudRate
;
1655 *clk_sel_val
= 0x10;
1656 } else if ((baudRate
> 230400) && (baudRate
<= 403200)) {
1657 *divisor
= 403200 / baudRate
;
1658 *clk_sel_val
= 0x20;
1659 } else if ((baudRate
> 403200) && (baudRate
<= 460800)) {
1660 *divisor
= 460800 / baudRate
;
1661 *clk_sel_val
= 0x30;
1662 } else if ((baudRate
> 460800) && (baudRate
<= 806400)) {
1663 *divisor
= 806400 / baudRate
;
1664 *clk_sel_val
= 0x40;
1665 } else if ((baudRate
> 806400) && (baudRate
<= 921600)) {
1666 *divisor
= 921600 / baudRate
;
1667 *clk_sel_val
= 0x50;
1668 } else if ((baudRate
> 921600) && (baudRate
<= 1572864)) {
1669 *divisor
= 1572864 / baudRate
;
1670 *clk_sel_val
= 0x60;
1671 } else if ((baudRate
> 1572864) && (baudRate
<= 3145728)) {
1672 *divisor
= 3145728 / baudRate
;
1673 *clk_sel_val
= 0x70;
1679 for (i
= 0; i
< ARRAY_SIZE(mos7840_divisor_table
); i
++) {
1680 if (mos7840_divisor_table
[i
].BaudRate
== baudrate
) {
1681 *divisor
= mos7840_divisor_table
[i
].Divisor
;
1686 /* After trying for all the standard baud rates *
1687 * Try calculating the divisor for this baud rate */
1689 if (baudrate
> 75 && baudrate
< 230400) {
1690 /* get the divisor */
1691 custom
= (__u16
) (230400L / baudrate
);
1693 /* Check for round off */
1694 round1
= (__u16
) (2304000L / baudrate
);
1695 round
= (__u16
) (round1
- (custom
* 10));
1700 dev_dbg(&port
->dev
, " Baud %d = %d\n", baudrate
, custom
);
1704 dev_dbg(&port
->dev
, "%s", " Baud calculation Failed...\n");
1709 /*****************************************************************************
1710 * mos7840_send_cmd_write_baud_rate
1711 * this function sends the proper command to change the baud rate of the
1713 *****************************************************************************/
1715 static int mos7840_send_cmd_write_baud_rate(struct moschip_port
*mos7840_port
,
1721 unsigned char number
;
1723 struct usb_serial_port
*port
;
1725 if (mos7840_port
== NULL
)
1728 port
= mos7840_port
->port
;
1729 if (mos7840_port_paranoia_check(port
, __func__
))
1732 if (mos7840_serial_paranoia_check(port
->serial
, __func__
))
1735 number
= mos7840_port
->port
->number
- mos7840_port
->port
->serial
->minor
;
1737 dev_dbg(&port
->dev
, "%s - port = %d, baud = %d\n", __func__
,
1738 mos7840_port
->port
->number
, baudRate
);
1739 /* reset clk_uart_sel in spregOffset */
1740 if (baudRate
> 115200) {
1741 #ifdef HW_flow_control
1742 /* NOTE: need to see the pther register to modify */
1743 /* setting h/w flow control bit to 1 */
1745 mos7840_port
->shadowMCR
= Data
;
1746 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1749 dev_dbg(&port
->dev
, "Writing spreg failed in set_serial_baud\n");
1755 #ifdef HW_flow_control
1756 /* setting h/w flow control bit to 0 */
1758 mos7840_port
->shadowMCR
= Data
;
1759 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1762 dev_dbg(&port
->dev
, "Writing spreg failed in set_serial_baud\n");
1769 if (1) { /* baudRate <= 115200) */
1772 status
= mos7840_calc_baud_rate_divisor(port
, baudRate
, &divisor
,
1774 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
,
1777 dev_dbg(&port
->dev
, "reading spreg failed in set_serial_baud\n");
1780 Data
= (Data
& 0x8f) | clk_sel_val
;
1781 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
,
1784 dev_dbg(&port
->dev
, "Writing spreg failed in set_serial_baud\n");
1787 /* Calculate the Divisor */
1790 dev_err(&port
->dev
, "%s - bad baud rate\n", __func__
);
1793 /* Enable access to divisor latch */
1794 Data
= mos7840_port
->shadowLCR
| SERIAL_LCR_DLAB
;
1795 mos7840_port
->shadowLCR
= Data
;
1796 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1798 /* Write the divisor */
1799 Data
= (unsigned char)(divisor
& 0xff);
1800 dev_dbg(&port
->dev
, "set_serial_baud Value to write DLL is %x\n", Data
);
1801 mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
1803 Data
= (unsigned char)((divisor
& 0xff00) >> 8);
1804 dev_dbg(&port
->dev
, "set_serial_baud Value to write DLM is %x\n", Data
);
1805 mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
1807 /* Disable access to divisor latch */
1808 Data
= mos7840_port
->shadowLCR
& ~SERIAL_LCR_DLAB
;
1809 mos7840_port
->shadowLCR
= Data
;
1810 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1816 /*****************************************************************************
1817 * mos7840_change_port_settings
1818 * This routine is called to set the UART on the device to match
1819 * the specified new settings.
1820 *****************************************************************************/
1822 static void mos7840_change_port_settings(struct tty_struct
*tty
,
1823 struct moschip_port
*mos7840_port
, struct ktermios
*old_termios
)
1833 struct usb_serial_port
*port
;
1834 struct usb_serial
*serial
;
1836 if (mos7840_port
== NULL
)
1839 port
= mos7840_port
->port
;
1841 if (mos7840_port_paranoia_check(port
, __func__
))
1844 if (mos7840_serial_paranoia_check(port
->serial
, __func__
))
1847 serial
= port
->serial
;
1849 if (!mos7840_port
->open
) {
1850 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1856 lParity
= LCR_PAR_NONE
;
1858 cflag
= tty
->termios
.c_cflag
;
1859 iflag
= tty
->termios
.c_iflag
;
1861 /* Change the number of bits */
1862 if (cflag
& CSIZE
) {
1863 switch (cflag
& CSIZE
) {
1881 /* Change the Parity bit */
1882 if (cflag
& PARENB
) {
1883 if (cflag
& PARODD
) {
1884 lParity
= LCR_PAR_ODD
;
1885 dev_dbg(&port
->dev
, "%s - parity = odd\n", __func__
);
1887 lParity
= LCR_PAR_EVEN
;
1888 dev_dbg(&port
->dev
, "%s - parity = even\n", __func__
);
1892 dev_dbg(&port
->dev
, "%s - parity = none\n", __func__
);
1896 lParity
= lParity
| 0x20;
1898 /* Change the Stop bit */
1899 if (cflag
& CSTOPB
) {
1901 dev_dbg(&port
->dev
, "%s - stop bits = 2\n", __func__
);
1904 dev_dbg(&port
->dev
, "%s - stop bits = 1\n", __func__
);
1907 /* Update the LCR with the correct value */
1908 mos7840_port
->shadowLCR
&=
1909 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
1910 mos7840_port
->shadowLCR
|= (lData
| lParity
| lStop
);
1912 dev_dbg(&port
->dev
, "%s - mos7840_port->shadowLCR is %x\n", __func__
,
1913 mos7840_port
->shadowLCR
);
1914 /* Disable Interrupts */
1916 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1919 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1922 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1924 /* Send the updated LCR value to the mos7840 */
1925 Data
= mos7840_port
->shadowLCR
;
1927 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1930 mos7840_port
->shadowMCR
= Data
;
1931 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1933 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1935 /* set up the MCR register and send it to the mos7840 */
1937 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
1939 mos7840_port
->shadowMCR
|= (MCR_DTR
| MCR_RTS
);
1941 if (cflag
& CRTSCTS
)
1942 mos7840_port
->shadowMCR
|= (MCR_XON_ANY
);
1944 mos7840_port
->shadowMCR
&= ~(MCR_XON_ANY
);
1946 Data
= mos7840_port
->shadowMCR
;
1947 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1949 /* Determine divisor based on baud rate */
1950 baud
= tty_get_baud_rate(tty
);
1953 /* pick a default, any default... */
1954 dev_dbg(&port
->dev
, "%s", "Picked default baud...\n");
1958 dev_dbg(&port
->dev
, "%s - baud rate = %d\n", __func__
, baud
);
1959 status
= mos7840_send_cmd_write_baud_rate(mos7840_port
, baud
);
1961 /* Enable Interrupts */
1963 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1965 if (mos7840_port
->read_urb_busy
== false) {
1966 mos7840_port
->read_urb_busy
= true;
1967 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
1969 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n",
1971 mos7840_port
->read_urb_busy
= false;
1974 dev_dbg(&port
->dev
, "%s - mos7840_port->shadowLCR is End %x\n", __func__
,
1975 mos7840_port
->shadowLCR
);
1978 /*****************************************************************************
1979 * mos7840_set_termios
1980 * this function is called by the tty driver when it wants to change
1981 * the termios structure
1982 *****************************************************************************/
1984 static void mos7840_set_termios(struct tty_struct
*tty
,
1985 struct usb_serial_port
*port
,
1986 struct ktermios
*old_termios
)
1990 struct usb_serial
*serial
;
1991 struct moschip_port
*mos7840_port
;
1993 if (mos7840_port_paranoia_check(port
, __func__
))
1996 serial
= port
->serial
;
1998 if (mos7840_serial_paranoia_check(serial
, __func__
))
2001 mos7840_port
= mos7840_get_port_private(port
);
2003 if (mos7840_port
== NULL
)
2006 if (!mos7840_port
->open
) {
2007 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
2011 dev_dbg(&port
->dev
, "%s", "setting termios - \n");
2013 cflag
= tty
->termios
.c_cflag
;
2015 dev_dbg(&port
->dev
, "%s - clfag %08x iflag %08x\n", __func__
,
2016 tty
->termios
.c_cflag
, RELEVANT_IFLAG(tty
->termios
.c_iflag
));
2017 dev_dbg(&port
->dev
, "%s - old clfag %08x old iflag %08x\n", __func__
,
2018 old_termios
->c_cflag
, RELEVANT_IFLAG(old_termios
->c_iflag
));
2019 dev_dbg(&port
->dev
, "%s - port %d\n", __func__
, port
->number
);
2021 /* change the port settings to the new ones specified */
2023 mos7840_change_port_settings(tty
, mos7840_port
, old_termios
);
2025 if (!mos7840_port
->read_urb
) {
2026 dev_dbg(&port
->dev
, "%s", "URB KILLED !!!!!\n");
2030 if (mos7840_port
->read_urb_busy
== false) {
2031 mos7840_port
->read_urb_busy
= true;
2032 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
2034 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n",
2036 mos7840_port
->read_urb_busy
= false;
2041 /*****************************************************************************
2042 * mos7840_get_lsr_info - get line status register info
2044 * Purpose: Let user call ioctl() to get info when the UART physically
2045 * is emptied. On bus types like RS485, the transmitter must
2046 * release the bus after transmitting. This must be done when
2047 * the transmit shift register is empty, not be done when the
2048 * transmit holding register is empty. This functionality
2049 * allows an RS485 driver to be written in user space.
2050 *****************************************************************************/
2052 static int mos7840_get_lsr_info(struct tty_struct
*tty
,
2053 unsigned int __user
*value
)
2056 unsigned int result
= 0;
2058 count
= mos7840_chars_in_buffer(tty
);
2060 result
= TIOCSER_TEMT
;
2062 if (copy_to_user(value
, &result
, sizeof(int)))
2067 /*****************************************************************************
2068 * mos7840_get_serial_info
2069 * function to get information about serial port
2070 *****************************************************************************/
2072 static int mos7840_get_serial_info(struct moschip_port
*mos7840_port
,
2073 struct serial_struct __user
*retinfo
)
2075 struct serial_struct tmp
;
2077 if (mos7840_port
== NULL
)
2083 memset(&tmp
, 0, sizeof(tmp
));
2085 tmp
.type
= PORT_16550A
;
2086 tmp
.line
= mos7840_port
->port
->serial
->minor
;
2087 tmp
.port
= mos7840_port
->port
->number
;
2089 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
2090 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
2091 tmp
.baud_base
= 9600;
2092 tmp
.close_delay
= 5 * HZ
;
2093 tmp
.closing_wait
= 30 * HZ
;
2095 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2100 /*****************************************************************************
2102 * this function handles any ioctl calls to the driver
2103 *****************************************************************************/
2105 static int mos7840_ioctl(struct tty_struct
*tty
,
2106 unsigned int cmd
, unsigned long arg
)
2108 struct usb_serial_port
*port
= tty
->driver_data
;
2109 void __user
*argp
= (void __user
*)arg
;
2110 struct moschip_port
*mos7840_port
;
2112 if (mos7840_port_paranoia_check(port
, __func__
))
2115 mos7840_port
= mos7840_get_port_private(port
);
2117 if (mos7840_port
== NULL
)
2120 dev_dbg(&port
->dev
, "%s - cmd = 0x%x\n", __func__
, cmd
);
2123 /* return number of bytes available */
2126 dev_dbg(&port
->dev
, "%s TIOCSERGETLSR\n", __func__
);
2127 return mos7840_get_lsr_info(tty
, argp
);
2130 dev_dbg(&port
->dev
, "%s TIOCGSERIAL\n", __func__
);
2131 return mos7840_get_serial_info(mos7840_port
, argp
);
2134 dev_dbg(&port
->dev
, "%s TIOCSSERIAL\n", __func__
);
2139 return -ENOIOCTLCMD
;
2142 static int mos7810_check(struct usb_serial
*serial
)
2144 int i
, pass_count
= 0;
2145 __u16 data
= 0, mcr_data
= 0;
2146 __u16 test_pattern
= 0x55AA;
2148 /* Store MCR setting */
2149 usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2150 MCS_RDREQ
, MCS_RD_RTYPE
, 0x0300, MODEM_CONTROL_REGISTER
,
2151 &mcr_data
, VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2153 for (i
= 0; i
< 16; i
++) {
2154 /* Send the 1-bit test pattern out to MCS7810 test pin */
2155 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2156 MCS_WRREQ
, MCS_WR_RTYPE
,
2157 (0x0300 | (((test_pattern
>> i
) & 0x0001) << 1)),
2158 MODEM_CONTROL_REGISTER
, NULL
, 0, MOS_WDR_TIMEOUT
);
2160 /* Read the test pattern back */
2161 usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2162 MCS_RDREQ
, MCS_RD_RTYPE
, 0, GPIO_REGISTER
, &data
,
2163 VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2165 /* If this is a MCS7810 device, both test patterns must match */
2166 if (((test_pattern
>> i
) ^ (~data
>> 1)) & 0x0001)
2172 /* Restore MCR setting */
2173 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0), MCS_WRREQ
,
2174 MCS_WR_RTYPE
, 0x0300 | mcr_data
, MODEM_CONTROL_REGISTER
, NULL
,
2175 0, MOS_WDR_TIMEOUT
);
2177 if (pass_count
== 16)
2183 static int mos7840_calc_num_ports(struct usb_serial
*serial
)
2186 int mos7840_num_ports
;
2188 usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2189 MCS_RDREQ
, MCS_RD_RTYPE
, 0, GPIO_REGISTER
, &data
,
2190 VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2192 if (serial
->dev
->descriptor
.idProduct
== MOSCHIP_DEVICE_ID_7810
||
2193 serial
->dev
->descriptor
.idProduct
== MOSCHIP_DEVICE_ID_7820
) {
2194 device_type
= serial
->dev
->descriptor
.idProduct
;
2196 /* For a MCS7840 device GPIO0 must be set to 1 */
2197 if ((data
& 0x01) == 1)
2198 device_type
= MOSCHIP_DEVICE_ID_7840
;
2199 else if (mos7810_check(serial
))
2200 device_type
= MOSCHIP_DEVICE_ID_7810
;
2202 device_type
= MOSCHIP_DEVICE_ID_7820
;
2205 mos7840_num_ports
= (device_type
>> 4) & 0x000F;
2206 serial
->num_bulk_in
= mos7840_num_ports
;
2207 serial
->num_bulk_out
= mos7840_num_ports
;
2208 serial
->num_ports
= mos7840_num_ports
;
2210 return mos7840_num_ports
;
2213 static int mos7840_port_probe(struct usb_serial_port
*port
)
2215 struct usb_serial
*serial
= port
->serial
;
2216 struct moschip_port
*mos7840_port
;
2221 /* we set up the pointers to the endpoints in the mos7840_open *
2222 * function, as the structures aren't created yet. */
2224 pnum
= port
->number
- serial
->minor
;
2226 dev_dbg(&port
->dev
, "mos7840_startup: configuring port %d\n", pnum
);
2227 mos7840_port
= kzalloc(sizeof(struct moschip_port
), GFP_KERNEL
);
2228 if (mos7840_port
== NULL
) {
2229 dev_err(&port
->dev
, "%s - Out of memory\n", __func__
);
2233 /* Initialize all port interrupt end point to port 0 int
2234 * endpoint. Our device has only one interrupt end point
2235 * common to all port */
2237 mos7840_port
->port
= port
;
2238 mos7840_set_port_private(port
, mos7840_port
);
2239 spin_lock_init(&mos7840_port
->pool_lock
);
2241 /* minor is not initialised until later by
2242 * usb-serial.c:get_free_serial() and cannot therefore be used
2243 * to index device instances */
2244 mos7840_port
->port_num
= pnum
+ 1;
2245 dev_dbg(&port
->dev
, "port->number = %d\n", port
->number
);
2246 dev_dbg(&port
->dev
, "port->serial->minor = %d\n", port
->serial
->minor
);
2247 dev_dbg(&port
->dev
, "mos7840_port->port_num = %d\n", mos7840_port
->port_num
);
2248 dev_dbg(&port
->dev
, "serial->minor = %d\n", serial
->minor
);
2250 if (mos7840_port
->port_num
== 1) {
2251 mos7840_port
->SpRegOffset
= 0x0;
2252 mos7840_port
->ControlRegOffset
= 0x1;
2253 mos7840_port
->DcrRegOffset
= 0x4;
2254 } else if ((mos7840_port
->port_num
== 2) && (serial
->num_ports
== 4)) {
2255 mos7840_port
->SpRegOffset
= 0x8;
2256 mos7840_port
->ControlRegOffset
= 0x9;
2257 mos7840_port
->DcrRegOffset
= 0x16;
2258 } else if ((mos7840_port
->port_num
== 2) && (serial
->num_ports
== 2)) {
2259 mos7840_port
->SpRegOffset
= 0xa;
2260 mos7840_port
->ControlRegOffset
= 0xb;
2261 mos7840_port
->DcrRegOffset
= 0x19;
2262 } else if ((mos7840_port
->port_num
== 3) && (serial
->num_ports
== 4)) {
2263 mos7840_port
->SpRegOffset
= 0xa;
2264 mos7840_port
->ControlRegOffset
= 0xb;
2265 mos7840_port
->DcrRegOffset
= 0x19;
2266 } else if ((mos7840_port
->port_num
== 4) && (serial
->num_ports
== 4)) {
2267 mos7840_port
->SpRegOffset
= 0xc;
2268 mos7840_port
->ControlRegOffset
= 0xd;
2269 mos7840_port
->DcrRegOffset
= 0x1c;
2271 mos7840_dump_serial_port(port
, mos7840_port
);
2272 mos7840_set_port_private(port
, mos7840_port
);
2274 /* enable rx_disable bit in control register */
2275 status
= mos7840_get_reg_sync(port
,
2276 mos7840_port
->ControlRegOffset
, &Data
);
2278 dev_dbg(&port
->dev
, "Reading ControlReg failed status-0x%x\n", status
);
2281 dev_dbg(&port
->dev
, "ControlReg Reading success val is %x, status%d\n", Data
, status
);
2282 Data
|= 0x08; /* setting driver done bit */
2283 Data
|= 0x04; /* sp1_bit to have cts change reflect in
2286 /* Data |= 0x20; //rx_disable bit */
2287 status
= mos7840_set_reg_sync(port
,
2288 mos7840_port
->ControlRegOffset
, Data
);
2290 dev_dbg(&port
->dev
, "Writing ControlReg failed(rx_disable) status-0x%x\n", status
);
2293 dev_dbg(&port
->dev
, "ControlReg Writing success(rx_disable) status%d\n", status
);
2295 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2298 status
= mos7840_set_reg_sync(port
,
2299 (__u16
) (mos7840_port
->DcrRegOffset
+ 0), Data
);
2301 dev_dbg(&port
->dev
, "Writing DCR0 failed status-0x%x\n", status
);
2304 dev_dbg(&port
->dev
, "DCR0 Writing success status%d\n", status
);
2307 status
= mos7840_set_reg_sync(port
,
2308 (__u16
) (mos7840_port
->DcrRegOffset
+ 1), Data
);
2310 dev_dbg(&port
->dev
, "Writing DCR1 failed status-0x%x\n", status
);
2313 dev_dbg(&port
->dev
, "DCR1 Writing success status%d\n", status
);
2316 status
= mos7840_set_reg_sync(port
,
2317 (__u16
) (mos7840_port
->DcrRegOffset
+ 2), Data
);
2319 dev_dbg(&port
->dev
, "Writing DCR2 failed status-0x%x\n", status
);
2322 dev_dbg(&port
->dev
, "DCR2 Writing success status%d\n", status
);
2324 /* write values in clkstart0x0 and clkmulti 0x20 */
2326 status
= mos7840_set_reg_sync(port
, CLK_START_VALUE_REGISTER
, Data
);
2328 dev_dbg(&port
->dev
, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status
);
2331 dev_dbg(&port
->dev
, "CLK_START_VALUE_REGISTER Writing success status%d\n", status
);
2334 status
= mos7840_set_reg_sync(port
, CLK_MULTI_REGISTER
, Data
);
2336 dev_dbg(&port
->dev
, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status
);
2339 dev_dbg(&port
->dev
, "CLK_MULTI_REGISTER Writing success status%d\n", status
);
2341 /* write value 0x0 to scratchpad register */
2343 status
= mos7840_set_uart_reg(port
, SCRATCH_PAD_REGISTER
, Data
);
2345 dev_dbg(&port
->dev
, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status
);
2348 dev_dbg(&port
->dev
, "SCRATCH_PAD_REGISTER Writing success status%d\n", status
);
2350 /* Zero Length flag register */
2351 if ((mos7840_port
->port_num
!= 1) && (serial
->num_ports
== 2)) {
2353 status
= mos7840_set_reg_sync(port
,
2355 ((__u16
)mos7840_port
->port_num
)), Data
);
2356 dev_dbg(&port
->dev
, "ZLIP offset %x\n",
2357 (__u16
)(ZLP_REG1
+ ((__u16
) mos7840_port
->port_num
)));
2359 dev_dbg(&port
->dev
, "Writing ZLP_REG%d failed status-0x%x\n", pnum
+ 2, status
);
2362 dev_dbg(&port
->dev
, "ZLP_REG%d Writing success status%d\n", pnum
+ 2, status
);
2365 status
= mos7840_set_reg_sync(port
,
2367 ((__u16
)mos7840_port
->port_num
) - 0x1), Data
);
2368 dev_dbg(&port
->dev
, "ZLIP offset %x\n",
2369 (__u16
)(ZLP_REG1
+ ((__u16
) mos7840_port
->port_num
) - 0x1));
2371 dev_dbg(&port
->dev
, "Writing ZLP_REG%d failed status-0x%x\n", pnum
+ 1, status
);
2374 dev_dbg(&port
->dev
, "ZLP_REG%d Writing success status%d\n", pnum
+ 1, status
);
2377 mos7840_port
->control_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2378 mos7840_port
->ctrl_buf
= kmalloc(16, GFP_KERNEL
);
2379 mos7840_port
->dr
= kmalloc(sizeof(struct usb_ctrlrequest
),
2381 if (!mos7840_port
->control_urb
|| !mos7840_port
->ctrl_buf
||
2382 !mos7840_port
->dr
) {
2387 mos7840_port
->has_led
= false;
2389 /* Initialize LED timers */
2390 if (device_type
== MOSCHIP_DEVICE_ID_7810
) {
2391 mos7840_port
->has_led
= true;
2393 init_timer(&mos7840_port
->led_timer1
);
2394 mos7840_port
->led_timer1
.function
= mos7840_led_off
;
2395 mos7840_port
->led_timer1
.expires
=
2396 jiffies
+ msecs_to_jiffies(LED_ON_MS
);
2397 mos7840_port
->led_timer1
.data
= (unsigned long)mos7840_port
;
2399 init_timer(&mos7840_port
->led_timer2
);
2400 mos7840_port
->led_timer2
.function
= mos7840_led_flag_off
;
2401 mos7840_port
->led_timer2
.expires
=
2402 jiffies
+ msecs_to_jiffies(LED_OFF_MS
);
2403 mos7840_port
->led_timer2
.data
= (unsigned long)mos7840_port
;
2405 mos7840_port
->led_flag
= false;
2408 mos7840_set_led_sync(port
, MODEM_CONTROL_REGISTER
, 0x0300);
2411 if (pnum
== serial
->num_ports
- 1) {
2412 /* Zero Length flag enable */
2414 status
= mos7840_set_reg_sync(serial
->port
[0], ZLP_REG5
, Data
);
2416 dev_dbg(&port
->dev
, "Writing ZLP_REG5 failed status-0x%x\n", status
);
2419 dev_dbg(&port
->dev
, "ZLP_REG5 Writing success status%d\n", status
);
2421 /* setting configuration feature to one */
2422 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2423 0x03, 0x00, 0x01, 0x00, NULL
, 0x00,
2428 kfree(mos7840_port
->dr
);
2429 kfree(mos7840_port
->ctrl_buf
);
2430 usb_free_urb(mos7840_port
->control_urb
);
2431 kfree(mos7840_port
);
2436 static int mos7840_port_remove(struct usb_serial_port
*port
)
2438 struct moschip_port
*mos7840_port
;
2440 mos7840_port
= mos7840_get_port_private(port
);
2442 if (mos7840_port
->has_led
) {
2444 mos7840_set_led_sync(port
, MODEM_CONTROL_REGISTER
, 0x0300);
2446 del_timer_sync(&mos7840_port
->led_timer1
);
2447 del_timer_sync(&mos7840_port
->led_timer2
);
2449 usb_kill_urb(mos7840_port
->control_urb
);
2450 usb_free_urb(mos7840_port
->control_urb
);
2451 kfree(mos7840_port
->ctrl_buf
);
2452 kfree(mos7840_port
->dr
);
2453 kfree(mos7840_port
);
2458 static struct usb_serial_driver moschip7840_4port_device
= {
2460 .owner
= THIS_MODULE
,
2463 .description
= DRIVER_DESC
,
2464 .id_table
= id_table
,
2466 .open
= mos7840_open
,
2467 .close
= mos7840_close
,
2468 .write
= mos7840_write
,
2469 .write_room
= mos7840_write_room
,
2470 .chars_in_buffer
= mos7840_chars_in_buffer
,
2471 .throttle
= mos7840_throttle
,
2472 .unthrottle
= mos7840_unthrottle
,
2473 .calc_num_ports
= mos7840_calc_num_ports
,
2474 #ifdef MCSSerialProbe
2475 .probe
= mos7840_serial_probe
,
2477 .ioctl
= mos7840_ioctl
,
2478 .set_termios
= mos7840_set_termios
,
2479 .break_ctl
= mos7840_break
,
2480 .tiocmget
= mos7840_tiocmget
,
2481 .tiocmset
= mos7840_tiocmset
,
2482 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
2483 .get_icount
= usb_serial_generic_get_icount
,
2484 .port_probe
= mos7840_port_probe
,
2485 .port_remove
= mos7840_port_remove
,
2486 .read_bulk_callback
= mos7840_bulk_in_callback
,
2487 .read_int_callback
= mos7840_interrupt_callback
,
2490 static struct usb_serial_driver
* const serial_drivers
[] = {
2491 &moschip7840_4port_device
, NULL
2494 module_usb_serial_driver(serial_drivers
, id_table
);
2496 MODULE_DESCRIPTION(DRIVER_DESC
);
2497 MODULE_LICENSE("GPL");