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/slab.h>
28 #include <linux/tty.h>
29 #include <linux/tty_driver.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/serial.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include <linux/uaccess.h>
37 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
40 * 16C50 UART register defines
43 #define LCR_BITS_5 0x00 /* 5 bits/char */
44 #define LCR_BITS_6 0x01 /* 6 bits/char */
45 #define LCR_BITS_7 0x02 /* 7 bits/char */
46 #define LCR_BITS_8 0x03 /* 8 bits/char */
47 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
49 #define LCR_STOP_1 0x00 /* 1 stop bit */
50 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
51 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
52 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
54 #define LCR_PAR_NONE 0x00 /* No parity */
55 #define LCR_PAR_ODD 0x08 /* Odd parity */
56 #define LCR_PAR_EVEN 0x18 /* Even parity */
57 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
58 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
59 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
61 #define LCR_SET_BREAK 0x40 /* Set Break condition */
62 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
64 #define MCR_DTR 0x01 /* Assert DTR */
65 #define MCR_RTS 0x02 /* Assert RTS */
66 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
67 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
68 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
69 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
71 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
72 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
73 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
74 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
77 * Defines used for sending commands to port
80 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
82 #define MOS_PORT1 0x0200
83 #define MOS_PORT2 0x0300
84 #define MOS_VENREG 0x0000
85 #define MOS_MAX_PORT 0x02
86 #define MOS_WRITE 0x0E
90 #define MCS_RD_RTYPE 0xC0
91 #define MCS_WR_RTYPE 0x40
92 #define MCS_RDREQ 0x0D
93 #define MCS_WRREQ 0x0E
94 #define MCS_CTRL_TIMEOUT 500
95 #define VENDOR_READ_LENGTH (0x01)
97 #define MAX_NAME_LEN 64
99 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
100 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
102 /* For higher baud Rates use TIOCEXBAUD */
103 #define TIOCEXBAUD 0x5462
105 /* vendor id and device id defines */
107 /* The native mos7840/7820 component */
108 #define USB_VENDOR_ID_MOSCHIP 0x9710
109 #define MOSCHIP_DEVICE_ID_7840 0x7840
110 #define MOSCHIP_DEVICE_ID_7820 0x7820
111 #define MOSCHIP_DEVICE_ID_7810 0x7810
112 /* The native component can have its vendor/device id's overridden
113 * in vendor-specific implementations. Such devices can be handled
114 * by making a change here, in id_table.
116 #define USB_VENDOR_ID_BANDB 0x0856
117 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
118 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
119 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
120 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
121 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
122 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
123 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
124 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
125 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
126 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
127 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
128 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
129 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
131 /* This driver also supports
132 * ATEN UC2324 device using Moschip MCS7840
133 * ATEN UC2322 device using Moschip MCS7820
135 #define USB_VENDOR_ID_ATENINTL 0x0557
136 #define ATENINTL_DEVICE_ID_UC2324 0x2011
137 #define ATENINTL_DEVICE_ID_UC2322 0x7820
139 /* Interrupt Routine Defines */
141 #define SERIAL_IIR_RLS 0x06
142 #define SERIAL_IIR_MS 0x00
145 * Emulation of the bit mask on the LINE STATUS REGISTER.
147 #define SERIAL_LSR_DR 0x0001
148 #define SERIAL_LSR_OE 0x0002
149 #define SERIAL_LSR_PE 0x0004
150 #define SERIAL_LSR_FE 0x0008
151 #define SERIAL_LSR_BI 0x0010
153 #define MOS_MSR_DELTA_CTS 0x10
154 #define MOS_MSR_DELTA_DSR 0x20
155 #define MOS_MSR_DELTA_RI 0x40
156 #define MOS_MSR_DELTA_CD 0x80
158 /* Serial Port register Address */
159 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
160 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
161 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
162 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
163 #define LINE_STATUS_REGISTER ((__u16)(0x05))
164 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
165 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
166 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
167 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
169 #define CLK_MULTI_REGISTER ((__u16)(0x02))
170 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
171 #define GPIO_REGISTER ((__u16)(0x07))
173 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
176 * URB POOL related defines
178 #define NUM_URBS 16 /* URB Count */
179 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
181 /* LED on/off milliseconds*/
182 #define LED_ON_MS 500
183 #define LED_OFF_MS 500
186 MOS7840_FLAG_CTRL_BUSY
,
187 MOS7840_FLAG_LED_BUSY
,
190 static const struct usb_device_id id_table
[] = {
191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
192 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7810
)},
194 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_2
)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_2P
)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_4
)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USO9ML2_4P
)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_US9ML2_2
)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_US9ML2_4
)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USPTL4_2
)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USPTL4_4
)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2P
)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4P
)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL2_4
)},
207 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2324
)},
208 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2322
)},
209 {} /* terminating entry */
211 MODULE_DEVICE_TABLE(usb
, id_table
);
213 /* This structure holds all of the local port information */
215 struct moschip_port
{
216 int port_num
; /*Actual port number in the device(1,2,etc) */
217 struct urb
*write_urb
; /* write URB for this port */
218 struct urb
*read_urb
; /* read URB for this port */
219 __u8 shadowLCR
; /* last LCR value received */
220 __u8 shadowMCR
; /* last MCR value received */
223 struct usb_serial_port
*port
; /* loop back to the owner of this object */
227 __u8 ControlRegOffset
;
229 /* for processing control URBS in interrupt context */
230 struct urb
*control_urb
;
231 struct usb_ctrlrequest
*dr
;
235 spinlock_t pool_lock
;
236 struct urb
*write_urb_pool
[NUM_URBS
];
240 /* 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 */
245 struct usb_ctrlrequest
*led_dr
;
251 * mos7840_set_reg_sync
252 * To set the Control register by calling usb_fill_control_urb function
253 * by passing usb_sndctrlpipe function as parameter.
256 static int mos7840_set_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
259 struct usb_device
*dev
= port
->serial
->dev
;
261 dev_dbg(&port
->dev
, "mos7840_set_reg_sync offset is %x, value %x\n", reg
, val
);
263 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
264 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
269 * mos7840_get_reg_sync
270 * To set the Uart register by calling usb_fill_control_urb function by
271 * passing usb_rcvctrlpipe function as parameter.
274 static int mos7840_get_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
277 struct usb_device
*dev
= port
->serial
->dev
;
281 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
285 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
286 MCS_RD_RTYPE
, 0, reg
, buf
, VENDOR_READ_LENGTH
,
288 if (ret
< VENDOR_READ_LENGTH
) {
295 dev_dbg(&port
->dev
, "%s offset is %x, return val %x\n", __func__
, reg
, *val
);
302 * mos7840_set_uart_reg
303 * To set the Uart register by calling usb_fill_control_urb function by
304 * passing usb_sndctrlpipe function as parameter.
307 static int mos7840_set_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
311 struct usb_device
*dev
= port
->serial
->dev
;
313 /* For the UART control registers, the application number need
315 if (port
->serial
->num_ports
== 4) {
316 val
|= ((__u16
)port
->port_number
+ 1) << 8;
318 if (port
->port_number
== 0) {
319 val
|= ((__u16
)port
->port_number
+ 1) << 8;
321 val
|= ((__u16
)port
->port_number
+ 2) << 8;
324 dev_dbg(&port
->dev
, "%s application number is %x\n", __func__
, val
);
325 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
326 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
332 * mos7840_get_uart_reg
333 * To set the Control register by calling usb_fill_control_urb function
334 * by passing usb_rcvctrlpipe function as parameter.
336 static int mos7840_get_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
339 struct usb_device
*dev
= port
->serial
->dev
;
344 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
348 /* Wval is same as application number */
349 if (port
->serial
->num_ports
== 4) {
350 Wval
= ((__u16
)port
->port_number
+ 1) << 8;
352 if (port
->port_number
== 0) {
353 Wval
= ((__u16
)port
->port_number
+ 1) << 8;
355 Wval
= ((__u16
)port
->port_number
+ 2) << 8;
358 dev_dbg(&port
->dev
, "%s application number is %x\n", __func__
, Wval
);
359 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
360 MCS_RD_RTYPE
, Wval
, reg
, buf
, VENDOR_READ_LENGTH
,
362 if (ret
< VENDOR_READ_LENGTH
) {
373 static void mos7840_dump_serial_port(struct usb_serial_port
*port
,
374 struct moschip_port
*mos7840_port
)
377 dev_dbg(&port
->dev
, "SpRegOffset is %2x\n", mos7840_port
->SpRegOffset
);
378 dev_dbg(&port
->dev
, "ControlRegOffset is %2x\n", mos7840_port
->ControlRegOffset
);
379 dev_dbg(&port
->dev
, "DCRRegOffset is %2x\n", mos7840_port
->DcrRegOffset
);
383 /************************************************************************/
384 /************************************************************************/
385 /* I N T E R F A C E F U N C T I O N S */
386 /* I N T E R F A C E F U N C T I O N S */
387 /************************************************************************/
388 /************************************************************************/
390 static inline void mos7840_set_port_private(struct usb_serial_port
*port
,
391 struct moschip_port
*data
)
393 usb_set_serial_port_data(port
, (void *)data
);
396 static inline struct moschip_port
*mos7840_get_port_private(struct
400 return (struct moschip_port
*)usb_get_serial_port_data(port
);
403 static void mos7840_handle_new_msr(struct moschip_port
*port
, __u8 new_msr
)
405 struct moschip_port
*mos7840_port
;
406 struct async_icount
*icount
;
409 (MOS_MSR_DELTA_CTS
| MOS_MSR_DELTA_DSR
| MOS_MSR_DELTA_RI
|
411 icount
= &mos7840_port
->port
->icount
;
413 /* update input line counters */
414 if (new_msr
& MOS_MSR_DELTA_CTS
)
416 if (new_msr
& MOS_MSR_DELTA_DSR
)
418 if (new_msr
& MOS_MSR_DELTA_CD
)
420 if (new_msr
& MOS_MSR_DELTA_RI
)
423 wake_up_interruptible(&port
->port
->port
.delta_msr_wait
);
427 static void mos7840_handle_new_lsr(struct moschip_port
*port
, __u8 new_lsr
)
429 struct async_icount
*icount
;
431 if (new_lsr
& SERIAL_LSR_BI
) {
433 * Parity and Framing errors only count if they
434 * occur exclusive of a break being
437 new_lsr
&= (__u8
) (SERIAL_LSR_OE
| SERIAL_LSR_BI
);
440 /* update input line counters */
441 icount
= &port
->port
->icount
;
442 if (new_lsr
& SERIAL_LSR_BI
)
444 if (new_lsr
& SERIAL_LSR_OE
)
446 if (new_lsr
& SERIAL_LSR_PE
)
448 if (new_lsr
& SERIAL_LSR_FE
)
452 /************************************************************************/
453 /************************************************************************/
454 /* U S B C A L L B A C K F U N C T I O N S */
455 /* U S B C A L L B A C K F U N C T I O N S */
456 /************************************************************************/
457 /************************************************************************/
459 static void mos7840_control_callback(struct urb
*urb
)
462 struct moschip_port
*mos7840_port
;
463 struct device
*dev
= &urb
->dev
->dev
;
465 int status
= urb
->status
;
467 mos7840_port
= urb
->context
;
476 /* this urb is terminated, clean up */
477 dev_dbg(dev
, "%s - urb shutting down with status: %d\n", __func__
, status
);
480 dev_dbg(dev
, "%s - nonzero urb status received: %d\n", __func__
, status
);
484 dev_dbg(dev
, "%s urb buffer size is %d\n", __func__
, urb
->actual_length
);
485 if (urb
->actual_length
< 1)
488 dev_dbg(dev
, "%s mos7840_port->MsrLsr is %d port %d\n", __func__
,
489 mos7840_port
->MsrLsr
, mos7840_port
->port_num
);
490 data
= urb
->transfer_buffer
;
491 regval
= (__u8
) data
[0];
492 dev_dbg(dev
, "%s data is %x\n", __func__
, regval
);
493 if (mos7840_port
->MsrLsr
== 0)
494 mos7840_handle_new_msr(mos7840_port
, regval
);
495 else if (mos7840_port
->MsrLsr
== 1)
496 mos7840_handle_new_lsr(mos7840_port
, regval
);
498 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY
, &mos7840_port
->flags
);
501 static int mos7840_get_reg(struct moschip_port
*mcs
, __u16 Wval
, __u16 reg
,
504 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
505 struct usb_ctrlrequest
*dr
= mcs
->dr
;
506 unsigned char *buffer
= mcs
->ctrl_buf
;
509 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY
, &mcs
->flags
))
512 dr
->bRequestType
= MCS_RD_RTYPE
;
513 dr
->bRequest
= MCS_RDREQ
;
514 dr
->wValue
= cpu_to_le16(Wval
); /* 0 */
515 dr
->wIndex
= cpu_to_le16(reg
);
516 dr
->wLength
= cpu_to_le16(2);
518 usb_fill_control_urb(mcs
->control_urb
, dev
, usb_rcvctrlpipe(dev
, 0),
519 (unsigned char *)dr
, buffer
, 2,
520 mos7840_control_callback
, mcs
);
521 mcs
->control_urb
->transfer_buffer_length
= 2;
522 ret
= usb_submit_urb(mcs
->control_urb
, GFP_ATOMIC
);
524 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY
, &mcs
->flags
);
529 static void mos7840_set_led_callback(struct urb
*urb
)
531 switch (urb
->status
) {
538 /* This urb is terminated, clean up */
539 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down: %d\n",
540 __func__
, urb
->status
);
543 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status: %d\n",
544 __func__
, urb
->status
);
548 static void mos7840_set_led_async(struct moschip_port
*mcs
, __u16 wval
,
551 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
552 struct usb_ctrlrequest
*dr
= mcs
->led_dr
;
554 dr
->bRequestType
= MCS_WR_RTYPE
;
555 dr
->bRequest
= MCS_WRREQ
;
556 dr
->wValue
= cpu_to_le16(wval
);
557 dr
->wIndex
= cpu_to_le16(reg
);
558 dr
->wLength
= cpu_to_le16(0);
560 usb_fill_control_urb(mcs
->led_urb
, dev
, usb_sndctrlpipe(dev
, 0),
561 (unsigned char *)dr
, NULL
, 0, mos7840_set_led_callback
, NULL
);
563 usb_submit_urb(mcs
->led_urb
, GFP_ATOMIC
);
566 static void mos7840_set_led_sync(struct usb_serial_port
*port
, __u16 reg
,
569 struct usb_device
*dev
= port
->serial
->dev
;
571 usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
, MCS_WR_RTYPE
,
572 val
, reg
, NULL
, 0, MOS_WDR_TIMEOUT
);
575 static void mos7840_led_off(unsigned long arg
)
577 struct moschip_port
*mcs
= (struct moschip_port
*) arg
;
580 mos7840_set_led_async(mcs
, 0x0300, MODEM_CONTROL_REGISTER
);
581 mod_timer(&mcs
->led_timer2
,
582 jiffies
+ msecs_to_jiffies(LED_OFF_MS
));
585 static void mos7840_led_flag_off(unsigned long arg
)
587 struct moschip_port
*mcs
= (struct moschip_port
*) arg
;
589 clear_bit_unlock(MOS7840_FLAG_LED_BUSY
, &mcs
->flags
);
592 static void mos7840_led_activity(struct usb_serial_port
*port
)
594 struct moschip_port
*mos7840_port
= usb_get_serial_port_data(port
);
596 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY
, &mos7840_port
->flags
))
599 mos7840_set_led_async(mos7840_port
, 0x0301, MODEM_CONTROL_REGISTER
);
600 mod_timer(&mos7840_port
->led_timer1
,
601 jiffies
+ msecs_to_jiffies(LED_ON_MS
));
604 /*****************************************************************************
605 * mos7840_interrupt_callback
606 * this is the callback function for when we have received data on the
607 * interrupt endpoint.
608 *****************************************************************************/
610 static void mos7840_interrupt_callback(struct urb
*urb
)
614 struct moschip_port
*mos7840_port
;
615 struct usb_serial
*serial
;
620 __u16 wval
, wreg
= 0;
621 int status
= urb
->status
;
630 /* this urb is terminated, clean up */
631 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d\n",
635 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n",
640 length
= urb
->actual_length
;
641 data
= urb
->transfer_buffer
;
643 serial
= urb
->context
;
645 /* Moschip get 5 bytes
646 * Byte 1 IIR Port 1 (port.number is 0)
647 * Byte 2 IIR Port 2 (port.number is 1)
648 * Byte 3 IIR Port 3 (port.number is 2)
649 * Byte 4 IIR Port 4 (port.number is 3)
650 * Byte 5 FIFO status for both */
653 dev_dbg(&urb
->dev
->dev
, "%s", "Wrong data !!!\n");
657 sp
[0] = (__u8
) data
[0];
658 sp
[1] = (__u8
) data
[1];
659 sp
[2] = (__u8
) data
[2];
660 sp
[3] = (__u8
) data
[3];
663 for (i
= 0; i
< serial
->num_ports
; i
++) {
664 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
665 wval
= ((__u16
)serial
->port
[i
]->port_number
+ 1) << 8;
666 if (mos7840_port
->open
) {
668 dev_dbg(&urb
->dev
->dev
, "SP%d No Interrupt !!!\n", i
);
670 switch (sp
[i
] & 0x0f) {
672 dev_dbg(&urb
->dev
->dev
, "Serial Port %d: Receiver status error or \n", i
);
673 dev_dbg(&urb
->dev
->dev
, "address bit detected in 9-bit mode\n");
674 mos7840_port
->MsrLsr
= 1;
675 wreg
= LINE_STATUS_REGISTER
;
678 dev_dbg(&urb
->dev
->dev
, "Serial Port %d: Modem status change\n", i
);
679 mos7840_port
->MsrLsr
= 0;
680 wreg
= MODEM_STATUS_REGISTER
;
683 rv
= mos7840_get_reg(mos7840_port
, wval
, wreg
, &Data
);
688 /* the completion handler for the control urb will resubmit */
691 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
693 dev_err(&urb
->dev
->dev
,
694 "%s - Error %d submitting interrupt urb\n",
699 static int mos7840_port_paranoia_check(struct usb_serial_port
*port
,
700 const char *function
)
703 pr_debug("%s - port == NULL\n", function
);
707 pr_debug("%s - port->serial == NULL\n", function
);
714 /* Inline functions to check the sanity of a pointer that is passed to us */
715 static int mos7840_serial_paranoia_check(struct usb_serial
*serial
,
716 const char *function
)
719 pr_debug("%s - serial == NULL\n", function
);
723 pr_debug("%s - serial->type == NULL!\n", function
);
730 static struct usb_serial
*mos7840_get_usb_serial(struct usb_serial_port
*port
,
731 const char *function
)
733 /* if no port was specified, or it fails a paranoia check */
735 mos7840_port_paranoia_check(port
, function
) ||
736 mos7840_serial_paranoia_check(port
->serial
, function
)) {
737 /* then say that we don't have a valid usb_serial thing,
738 * which will end up genrating -ENODEV return values */
745 /*****************************************************************************
746 * mos7840_bulk_in_callback
747 * this is the callback function for when we have received data on the
749 *****************************************************************************/
751 static void mos7840_bulk_in_callback(struct urb
*urb
)
755 struct usb_serial
*serial
;
756 struct usb_serial_port
*port
;
757 struct moschip_port
*mos7840_port
;
758 int status
= urb
->status
;
760 mos7840_port
= urb
->context
;
765 dev_dbg(&urb
->dev
->dev
, "nonzero read bulk status received: %d\n", status
);
766 mos7840_port
->read_urb_busy
= false;
770 port
= mos7840_port
->port
;
771 if (mos7840_port_paranoia_check(port
, __func__
)) {
772 mos7840_port
->read_urb_busy
= false;
776 serial
= mos7840_get_usb_serial(port
, __func__
);
778 mos7840_port
->read_urb_busy
= false;
782 data
= urb
->transfer_buffer
;
783 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
785 if (urb
->actual_length
) {
786 struct tty_port
*tport
= &mos7840_port
->port
->port
;
787 tty_insert_flip_string(tport
, data
, urb
->actual_length
);
788 tty_flip_buffer_push(tport
);
789 port
->icount
.rx
+= urb
->actual_length
;
790 dev_dbg(&port
->dev
, "icount.rx is %d:\n", port
->icount
.rx
);
793 if (!mos7840_port
->read_urb
) {
794 dev_dbg(&port
->dev
, "%s", "URB KILLED !!!\n");
795 mos7840_port
->read_urb_busy
= false;
799 if (mos7840_port
->has_led
)
800 mos7840_led_activity(port
);
802 mos7840_port
->read_urb_busy
= true;
803 retval
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
806 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, retval = %d\n", retval
);
807 mos7840_port
->read_urb_busy
= false;
811 /*****************************************************************************
812 * mos7840_bulk_out_data_callback
813 * this is the callback function for when we have finished sending
814 * serial data on the bulk out endpoint.
815 *****************************************************************************/
817 static void mos7840_bulk_out_data_callback(struct urb
*urb
)
819 struct moschip_port
*mos7840_port
;
820 struct usb_serial_port
*port
;
821 int status
= urb
->status
;
824 mos7840_port
= urb
->context
;
825 port
= mos7840_port
->port
;
826 spin_lock(&mos7840_port
->pool_lock
);
827 for (i
= 0; i
< NUM_URBS
; i
++) {
828 if (urb
== mos7840_port
->write_urb_pool
[i
]) {
829 mos7840_port
->busy
[i
] = 0;
833 spin_unlock(&mos7840_port
->pool_lock
);
836 dev_dbg(&port
->dev
, "nonzero write bulk status received:%d\n", status
);
840 if (mos7840_port_paranoia_check(port
, __func__
))
843 if (mos7840_port
->open
)
844 tty_port_tty_wakeup(&port
->port
);
848 /************************************************************************/
849 /* 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 */
850 /************************************************************************/
852 /*****************************************************************************
854 * this function is called by the tty driver when a port is opened
855 * If successful, we return 0
856 * Otherwise we return a negative error number.
857 *****************************************************************************/
859 static int mos7840_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
863 struct usb_serial
*serial
;
867 struct moschip_port
*mos7840_port
;
868 struct moschip_port
*port0
;
870 if (mos7840_port_paranoia_check(port
, __func__
))
873 serial
= port
->serial
;
875 if (mos7840_serial_paranoia_check(serial
, __func__
))
878 mos7840_port
= mos7840_get_port_private(port
);
879 port0
= mos7840_get_port_private(serial
->port
[0]);
881 if (mos7840_port
== NULL
|| port0
== NULL
)
884 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
885 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
888 /* Initialising the write urb pool */
889 for (j
= 0; j
< NUM_URBS
; ++j
) {
890 urb
= usb_alloc_urb(0, GFP_KERNEL
);
891 mos7840_port
->write_urb_pool
[j
] = urb
;
895 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
897 if (!urb
->transfer_buffer
) {
899 mos7840_port
->write_urb_pool
[j
] = NULL
;
904 /*****************************************************************************
905 * Initialize MCS7840 -- Write Init values to corresponding Registers
913 * 0x08 : SP1/2 Control Reg
914 *****************************************************************************/
916 /* NEED to check the following Block */
919 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
921 dev_dbg(&port
->dev
, "Reading Spreg failed\n");
925 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
927 dev_dbg(&port
->dev
, "writing Spreg failed\n");
932 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
934 dev_dbg(&port
->dev
, "writing Spreg failed\n");
937 /* End of block to be checked */
940 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
943 dev_dbg(&port
->dev
, "Reading Controlreg failed\n");
946 Data
|= 0x08; /* Driver done bit */
947 Data
|= 0x20; /* rx_disable */
948 status
= mos7840_set_reg_sync(port
,
949 mos7840_port
->ControlRegOffset
, Data
);
951 dev_dbg(&port
->dev
, "writing Controlreg failed\n");
954 /* do register settings here */
955 /* Set all regs to the device default values. */
956 /***********************************
957 * First Disable all interrupts.
958 ***********************************/
960 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
962 dev_dbg(&port
->dev
, "disabling interrupts failed\n");
965 /* Set FIFO_CONTROL_REGISTER to the default value */
967 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
969 dev_dbg(&port
->dev
, "Writing FIFO_CONTROL_REGISTER failed\n");
974 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
976 dev_dbg(&port
->dev
, "Writing FIFO_CONTROL_REGISTER failed\n");
981 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
982 mos7840_port
->shadowLCR
= Data
;
985 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
986 mos7840_port
->shadowMCR
= Data
;
989 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
990 mos7840_port
->shadowLCR
= Data
;
992 Data
|= SERIAL_LCR_DLAB
; /* data latch enable in LCR 0x80 */
993 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
996 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
999 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
1002 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
1004 Data
= Data
& ~SERIAL_LCR_DLAB
;
1005 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1006 mos7840_port
->shadowLCR
= Data
;
1008 /* clearing Bulkin and Bulkout Fifo */
1010 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
1013 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1015 Data
= Data
& ~0x0c;
1016 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1017 /* Finally enable all interrupts */
1019 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1021 /* clearing rx_disable */
1023 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1025 Data
= Data
& ~0x20;
1026 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1031 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1034 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1037 /* Check to see if we've set up our endpoint info yet *
1038 * (can't set it up in mos7840_startup as the structures *
1039 * were not set up at that time.) */
1040 if (port0
->open_ports
== 1) {
1041 /* FIXME: Buffer never NULL, so URB is not submitted. */
1042 if (serial
->port
[0]->interrupt_in_buffer
== NULL
) {
1043 /* set up interrupt urb */
1044 usb_fill_int_urb(serial
->port
[0]->interrupt_in_urb
,
1046 usb_rcvintpipe(serial
->dev
,
1047 serial
->port
[0]->interrupt_in_endpointAddress
),
1048 serial
->port
[0]->interrupt_in_buffer
,
1049 serial
->port
[0]->interrupt_in_urb
->
1050 transfer_buffer_length
,
1051 mos7840_interrupt_callback
,
1053 serial
->port
[0]->interrupt_in_urb
->interval
);
1055 /* start interrupt read for mos7840 *
1056 * will continue as long as mos7840 is connected */
1059 usb_submit_urb(serial
->port
[0]->interrupt_in_urb
,
1062 dev_err(&port
->dev
, "%s - Error %d submitting "
1063 "interrupt urb\n", __func__
, response
);
1070 /* see if we've set up our endpoint info yet *
1071 * (can't set it up in mos7840_startup as the *
1072 * structures were not set up at that time.) */
1074 dev_dbg(&port
->dev
, "port number is %d\n", port
->port_number
);
1075 dev_dbg(&port
->dev
, "minor number is %d\n", port
->minor
);
1076 dev_dbg(&port
->dev
, "Bulkin endpoint is %d\n", port
->bulk_in_endpointAddress
);
1077 dev_dbg(&port
->dev
, "BulkOut endpoint is %d\n", port
->bulk_out_endpointAddress
);
1078 dev_dbg(&port
->dev
, "Interrupt endpoint is %d\n", port
->interrupt_in_endpointAddress
);
1079 dev_dbg(&port
->dev
, "port's number in the device is %d\n", mos7840_port
->port_num
);
1080 mos7840_port
->read_urb
= port
->read_urb
;
1082 /* set up our bulk in urb */
1083 if ((serial
->num_ports
== 2) && (((__u16
)port
->port_number
% 2) != 0)) {
1084 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1086 usb_rcvbulkpipe(serial
->dev
,
1087 (port
->bulk_in_endpointAddress
) + 2),
1088 port
->bulk_in_buffer
,
1089 mos7840_port
->read_urb
->transfer_buffer_length
,
1090 mos7840_bulk_in_callback
, mos7840_port
);
1092 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1094 usb_rcvbulkpipe(serial
->dev
,
1095 port
->bulk_in_endpointAddress
),
1096 port
->bulk_in_buffer
,
1097 mos7840_port
->read_urb
->transfer_buffer_length
,
1098 mos7840_bulk_in_callback
, mos7840_port
);
1101 dev_dbg(&port
->dev
, "%s: bulkin endpoint is %d\n", __func__
, port
->bulk_in_endpointAddress
);
1102 mos7840_port
->read_urb_busy
= true;
1103 response
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1105 dev_err(&port
->dev
, "%s - Error %d submitting control urb\n",
1106 __func__
, response
);
1107 mos7840_port
->read_urb_busy
= false;
1110 /* initialize our port settings */
1111 /* Must set to enable ints! */
1112 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
1113 /* send a open port command */
1114 mos7840_port
->open
= 1;
1115 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1119 for (j
= 0; j
< NUM_URBS
; ++j
) {
1120 urb
= mos7840_port
->write_urb_pool
[j
];
1123 kfree(urb
->transfer_buffer
);
1129 /*****************************************************************************
1130 * mos7840_chars_in_buffer
1131 * this function is called by the tty driver when it wants to know how many
1132 * bytes of data we currently have outstanding in the port (data that has
1133 * been written, but hasn't made it out the port yet)
1134 * If successful, we return the number of bytes left to be written in the
1136 * Otherwise we return zero.
1137 *****************************************************************************/
1139 static int mos7840_chars_in_buffer(struct tty_struct
*tty
)
1141 struct usb_serial_port
*port
= tty
->driver_data
;
1144 unsigned long flags
;
1145 struct moschip_port
*mos7840_port
;
1147 if (mos7840_port_paranoia_check(port
, __func__
))
1150 mos7840_port
= mos7840_get_port_private(port
);
1151 if (mos7840_port
== NULL
)
1154 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1155 for (i
= 0; i
< NUM_URBS
; ++i
) {
1156 if (mos7840_port
->busy
[i
]) {
1157 struct urb
*urb
= mos7840_port
->write_urb_pool
[i
];
1158 chars
+= urb
->transfer_buffer_length
;
1161 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1162 dev_dbg(&port
->dev
, "%s - returns %d\n", __func__
, chars
);
1167 /*****************************************************************************
1169 * this function is called by the tty driver when a port is closed
1170 *****************************************************************************/
1172 static void mos7840_close(struct usb_serial_port
*port
)
1174 struct usb_serial
*serial
;
1175 struct moschip_port
*mos7840_port
;
1176 struct moschip_port
*port0
;
1180 if (mos7840_port_paranoia_check(port
, __func__
))
1183 serial
= mos7840_get_usb_serial(port
, __func__
);
1187 mos7840_port
= mos7840_get_port_private(port
);
1188 port0
= mos7840_get_port_private(serial
->port
[0]);
1190 if (mos7840_port
== NULL
|| port0
== NULL
)
1193 for (j
= 0; j
< NUM_URBS
; ++j
)
1194 usb_kill_urb(mos7840_port
->write_urb_pool
[j
]);
1196 /* Freeing Write URBs */
1197 for (j
= 0; j
< NUM_URBS
; ++j
) {
1198 if (mos7840_port
->write_urb_pool
[j
]) {
1199 kfree(mos7840_port
->write_urb_pool
[j
]->transfer_buffer
);
1200 usb_free_urb(mos7840_port
->write_urb_pool
[j
]);
1204 usb_kill_urb(mos7840_port
->write_urb
);
1205 usb_kill_urb(mos7840_port
->read_urb
);
1206 mos7840_port
->read_urb_busy
= false;
1208 port0
->open_ports
--;
1209 dev_dbg(&port
->dev
, "%s in close%d\n", __func__
, port0
->open_ports
);
1210 if (port0
->open_ports
== 0) {
1211 if (serial
->port
[0]->interrupt_in_urb
) {
1212 dev_dbg(&port
->dev
, "Shutdown interrupt_in_urb\n");
1213 usb_kill_urb(serial
->port
[0]->interrupt_in_urb
);
1217 if (mos7840_port
->write_urb
) {
1218 /* if this urb had a transfer buffer already (old tx) free it */
1219 kfree(mos7840_port
->write_urb
->transfer_buffer
);
1220 usb_free_urb(mos7840_port
->write_urb
);
1224 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1227 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1229 mos7840_port
->open
= 0;
1232 /*****************************************************************************
1234 * this function sends a break to the port
1235 *****************************************************************************/
1236 static void mos7840_break(struct tty_struct
*tty
, int break_state
)
1238 struct usb_serial_port
*port
= tty
->driver_data
;
1240 struct usb_serial
*serial
;
1241 struct moschip_port
*mos7840_port
;
1243 if (mos7840_port_paranoia_check(port
, __func__
))
1246 serial
= mos7840_get_usb_serial(port
, __func__
);
1250 mos7840_port
= mos7840_get_port_private(port
);
1252 if (mos7840_port
== NULL
)
1255 if (break_state
== -1)
1256 data
= mos7840_port
->shadowLCR
| LCR_SET_BREAK
;
1258 data
= mos7840_port
->shadowLCR
& ~LCR_SET_BREAK
;
1260 /* FIXME: no locking on shadowLCR anywhere in driver */
1261 mos7840_port
->shadowLCR
= data
;
1262 dev_dbg(&port
->dev
, "%s mos7840_port->shadowLCR is %x\n", __func__
, mos7840_port
->shadowLCR
);
1263 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
,
1264 mos7840_port
->shadowLCR
);
1267 /*****************************************************************************
1268 * mos7840_write_room
1269 * this function is called by the tty driver when it wants to know how many
1270 * bytes of data we can accept for a specific port.
1271 * If successful, we return the amount of room that we have for this port
1272 * Otherwise we return a negative error number.
1273 *****************************************************************************/
1275 static int mos7840_write_room(struct tty_struct
*tty
)
1277 struct usb_serial_port
*port
= tty
->driver_data
;
1280 unsigned long flags
;
1281 struct moschip_port
*mos7840_port
;
1283 if (mos7840_port_paranoia_check(port
, __func__
))
1286 mos7840_port
= mos7840_get_port_private(port
);
1287 if (mos7840_port
== NULL
)
1290 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1291 for (i
= 0; i
< NUM_URBS
; ++i
) {
1292 if (!mos7840_port
->busy
[i
])
1293 room
+= URB_TRANSFER_BUFFER_SIZE
;
1295 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1297 room
= (room
== 0) ? 0 : room
- URB_TRANSFER_BUFFER_SIZE
+ 1;
1298 dev_dbg(&mos7840_port
->port
->dev
, "%s - returns %d\n", __func__
, room
);
1303 /*****************************************************************************
1305 * this function is called by the tty driver when data should be written to
1307 * If successful, we return the number of bytes written, otherwise we
1308 * return a negative error number.
1309 *****************************************************************************/
1311 static int mos7840_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1312 const unsigned char *data
, int count
)
1318 unsigned long flags
;
1320 struct moschip_port
*mos7840_port
;
1321 struct usb_serial
*serial
;
1324 const unsigned char *current_position
= data
;
1325 unsigned char *data1
;
1327 if (mos7840_port_paranoia_check(port
, __func__
))
1330 serial
= port
->serial
;
1331 if (mos7840_serial_paranoia_check(serial
, __func__
))
1334 mos7840_port
= mos7840_get_port_private(port
);
1335 if (mos7840_port
== NULL
)
1338 /* try to find a free urb in the list */
1341 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1342 for (i
= 0; i
< NUM_URBS
; ++i
) {
1343 if (!mos7840_port
->busy
[i
]) {
1344 mos7840_port
->busy
[i
] = 1;
1345 urb
= mos7840_port
->write_urb_pool
[i
];
1346 dev_dbg(&port
->dev
, "URB:%d\n", i
);
1350 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1353 dev_dbg(&port
->dev
, "%s - no more free urbs\n", __func__
);
1357 if (urb
->transfer_buffer
== NULL
) {
1358 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
1360 if (!urb
->transfer_buffer
)
1363 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1365 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1367 /* fill urb with data and submit */
1368 if ((serial
->num_ports
== 2) && (((__u16
)port
->port_number
% 2) != 0)) {
1369 usb_fill_bulk_urb(urb
,
1371 usb_sndbulkpipe(serial
->dev
,
1372 (port
->bulk_out_endpointAddress
) + 2),
1373 urb
->transfer_buffer
,
1375 mos7840_bulk_out_data_callback
, mos7840_port
);
1377 usb_fill_bulk_urb(urb
,
1379 usb_sndbulkpipe(serial
->dev
,
1380 port
->bulk_out_endpointAddress
),
1381 urb
->transfer_buffer
,
1383 mos7840_bulk_out_data_callback
, mos7840_port
);
1386 data1
= urb
->transfer_buffer
;
1387 dev_dbg(&port
->dev
, "bulkout endpoint is %d\n", port
->bulk_out_endpointAddress
);
1389 if (mos7840_port
->has_led
)
1390 mos7840_led_activity(port
);
1392 /* send it down the pipe */
1393 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1396 mos7840_port
->busy
[i
] = 0;
1397 dev_err_console(port
, "%s - usb_submit_urb(write bulk) failed "
1398 "with status = %d\n", __func__
, status
);
1399 bytes_sent
= status
;
1402 bytes_sent
= transfer_size
;
1403 port
->icount
.tx
+= transfer_size
;
1404 dev_dbg(&port
->dev
, "icount.tx is %d:\n", port
->icount
.tx
);
1410 /*****************************************************************************
1412 * this function is called by the tty driver when it wants to stop the data
1413 * being read from the port.
1414 *****************************************************************************/
1416 static void mos7840_throttle(struct tty_struct
*tty
)
1418 struct usb_serial_port
*port
= tty
->driver_data
;
1419 struct moschip_port
*mos7840_port
;
1422 if (mos7840_port_paranoia_check(port
, __func__
))
1425 mos7840_port
= mos7840_get_port_private(port
);
1427 if (mos7840_port
== NULL
)
1430 if (!mos7840_port
->open
) {
1431 dev_dbg(&port
->dev
, "%s", "port not opened\n");
1435 /* if we are implementing XON/XOFF, send the stop character */
1437 unsigned char stop_char
= STOP_CHAR(tty
);
1438 status
= mos7840_write(tty
, port
, &stop_char
, 1);
1442 /* if we are implementing RTS/CTS, toggle that line */
1443 if (C_CRTSCTS(tty
)) {
1444 mos7840_port
->shadowMCR
&= ~MCR_RTS
;
1445 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1446 mos7840_port
->shadowMCR
);
1452 /*****************************************************************************
1453 * mos7840_unthrottle
1454 * this function is called by the tty driver when it wants to resume
1455 * the data being read from the port (called after mos7840_throttle is
1457 *****************************************************************************/
1458 static void mos7840_unthrottle(struct tty_struct
*tty
)
1460 struct usb_serial_port
*port
= tty
->driver_data
;
1462 struct moschip_port
*mos7840_port
= mos7840_get_port_private(port
);
1464 if (mos7840_port_paranoia_check(port
, __func__
))
1467 if (mos7840_port
== NULL
)
1470 if (!mos7840_port
->open
) {
1471 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1475 /* if we are implementing XON/XOFF, send the start character */
1477 unsigned char start_char
= START_CHAR(tty
);
1478 status
= mos7840_write(tty
, port
, &start_char
, 1);
1483 /* if we are implementing RTS/CTS, toggle that line */
1484 if (C_CRTSCTS(tty
)) {
1485 mos7840_port
->shadowMCR
|= MCR_RTS
;
1486 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1487 mos7840_port
->shadowMCR
);
1493 static int mos7840_tiocmget(struct tty_struct
*tty
)
1495 struct usb_serial_port
*port
= tty
->driver_data
;
1496 struct moschip_port
*mos7840_port
;
1497 unsigned int result
;
1501 mos7840_port
= mos7840_get_port_private(port
);
1503 if (mos7840_port
== NULL
)
1506 status
= mos7840_get_uart_reg(port
, MODEM_STATUS_REGISTER
, &msr
);
1509 status
= mos7840_get_uart_reg(port
, MODEM_CONTROL_REGISTER
, &mcr
);
1512 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0)
1513 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0)
1514 | ((mcr
& MCR_LOOPBACK
) ? TIOCM_LOOP
: 0)
1515 | ((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0)
1516 | ((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0)
1517 | ((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0)
1518 | ((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0);
1520 dev_dbg(&port
->dev
, "%s - 0x%04X\n", __func__
, result
);
1525 static int mos7840_tiocmset(struct tty_struct
*tty
,
1526 unsigned int set
, unsigned int clear
)
1528 struct usb_serial_port
*port
= tty
->driver_data
;
1529 struct moschip_port
*mos7840_port
;
1533 mos7840_port
= mos7840_get_port_private(port
);
1535 if (mos7840_port
== NULL
)
1538 /* FIXME: What locks the port registers ? */
1539 mcr
= mos7840_port
->shadowMCR
;
1540 if (clear
& TIOCM_RTS
)
1542 if (clear
& TIOCM_DTR
)
1544 if (clear
& TIOCM_LOOP
)
1545 mcr
&= ~MCR_LOOPBACK
;
1547 if (set
& TIOCM_RTS
)
1549 if (set
& TIOCM_DTR
)
1551 if (set
& TIOCM_LOOP
)
1552 mcr
|= MCR_LOOPBACK
;
1554 mos7840_port
->shadowMCR
= mcr
;
1556 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, mcr
);
1558 dev_dbg(&port
->dev
, "setting MODEM_CONTROL_REGISTER Failed\n");
1565 /*****************************************************************************
1566 * mos7840_calc_baud_rate_divisor
1567 * this function calculates the proper baud rate divisor for the specified
1569 *****************************************************************************/
1570 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port
*port
,
1571 int baudRate
, int *divisor
,
1574 dev_dbg(&port
->dev
, "%s - %d\n", __func__
, baudRate
);
1576 if (baudRate
<= 115200) {
1577 *divisor
= 115200 / baudRate
;
1580 if ((baudRate
> 115200) && (baudRate
<= 230400)) {
1581 *divisor
= 230400 / baudRate
;
1582 *clk_sel_val
= 0x10;
1583 } else if ((baudRate
> 230400) && (baudRate
<= 403200)) {
1584 *divisor
= 403200 / baudRate
;
1585 *clk_sel_val
= 0x20;
1586 } else if ((baudRate
> 403200) && (baudRate
<= 460800)) {
1587 *divisor
= 460800 / baudRate
;
1588 *clk_sel_val
= 0x30;
1589 } else if ((baudRate
> 460800) && (baudRate
<= 806400)) {
1590 *divisor
= 806400 / baudRate
;
1591 *clk_sel_val
= 0x40;
1592 } else if ((baudRate
> 806400) && (baudRate
<= 921600)) {
1593 *divisor
= 921600 / baudRate
;
1594 *clk_sel_val
= 0x50;
1595 } else if ((baudRate
> 921600) && (baudRate
<= 1572864)) {
1596 *divisor
= 1572864 / baudRate
;
1597 *clk_sel_val
= 0x60;
1598 } else if ((baudRate
> 1572864) && (baudRate
<= 3145728)) {
1599 *divisor
= 3145728 / baudRate
;
1600 *clk_sel_val
= 0x70;
1605 /*****************************************************************************
1606 * mos7840_send_cmd_write_baud_rate
1607 * this function sends the proper command to change the baud rate of the
1609 *****************************************************************************/
1611 static int mos7840_send_cmd_write_baud_rate(struct moschip_port
*mos7840_port
,
1617 unsigned char number
;
1619 struct usb_serial_port
*port
;
1621 if (mos7840_port
== NULL
)
1624 port
= mos7840_port
->port
;
1625 if (mos7840_port_paranoia_check(port
, __func__
))
1628 if (mos7840_serial_paranoia_check(port
->serial
, __func__
))
1631 number
= mos7840_port
->port
->port_number
;
1633 dev_dbg(&port
->dev
, "%s - baud = %d\n", __func__
, baudRate
);
1634 /* reset clk_uart_sel in spregOffset */
1635 if (baudRate
> 115200) {
1636 #ifdef HW_flow_control
1637 /* NOTE: need to see the pther register to modify */
1638 /* setting h/w flow control bit to 1 */
1640 mos7840_port
->shadowMCR
= Data
;
1641 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1644 dev_dbg(&port
->dev
, "Writing spreg failed in set_serial_baud\n");
1650 #ifdef HW_flow_control
1651 /* setting h/w flow control bit to 0 */
1653 mos7840_port
->shadowMCR
= Data
;
1654 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1657 dev_dbg(&port
->dev
, "Writing spreg failed in set_serial_baud\n");
1664 if (1) { /* baudRate <= 115200) */
1667 status
= mos7840_calc_baud_rate_divisor(port
, baudRate
, &divisor
,
1669 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
,
1672 dev_dbg(&port
->dev
, "reading spreg failed in set_serial_baud\n");
1675 Data
= (Data
& 0x8f) | clk_sel_val
;
1676 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
,
1679 dev_dbg(&port
->dev
, "Writing spreg failed in set_serial_baud\n");
1682 /* Calculate the Divisor */
1685 dev_err(&port
->dev
, "%s - bad baud rate\n", __func__
);
1688 /* Enable access to divisor latch */
1689 Data
= mos7840_port
->shadowLCR
| SERIAL_LCR_DLAB
;
1690 mos7840_port
->shadowLCR
= Data
;
1691 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1693 /* Write the divisor */
1694 Data
= (unsigned char)(divisor
& 0xff);
1695 dev_dbg(&port
->dev
, "set_serial_baud Value to write DLL is %x\n", Data
);
1696 mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
1698 Data
= (unsigned char)((divisor
& 0xff00) >> 8);
1699 dev_dbg(&port
->dev
, "set_serial_baud Value to write DLM is %x\n", Data
);
1700 mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
1702 /* Disable access to divisor latch */
1703 Data
= mos7840_port
->shadowLCR
& ~SERIAL_LCR_DLAB
;
1704 mos7840_port
->shadowLCR
= Data
;
1705 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1711 /*****************************************************************************
1712 * mos7840_change_port_settings
1713 * This routine is called to set the UART on the device to match
1714 * the specified new settings.
1715 *****************************************************************************/
1717 static void mos7840_change_port_settings(struct tty_struct
*tty
,
1718 struct moschip_port
*mos7840_port
, struct ktermios
*old_termios
)
1728 struct usb_serial_port
*port
;
1729 struct usb_serial
*serial
;
1731 if (mos7840_port
== NULL
)
1734 port
= mos7840_port
->port
;
1736 if (mos7840_port_paranoia_check(port
, __func__
))
1739 if (mos7840_serial_paranoia_check(port
->serial
, __func__
))
1742 serial
= port
->serial
;
1744 if (!mos7840_port
->open
) {
1745 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1751 lParity
= LCR_PAR_NONE
;
1753 cflag
= tty
->termios
.c_cflag
;
1754 iflag
= tty
->termios
.c_iflag
;
1756 /* Change the number of bits */
1757 switch (cflag
& CSIZE
) {
1776 /* Change the Parity bit */
1777 if (cflag
& PARENB
) {
1778 if (cflag
& PARODD
) {
1779 lParity
= LCR_PAR_ODD
;
1780 dev_dbg(&port
->dev
, "%s - parity = odd\n", __func__
);
1782 lParity
= LCR_PAR_EVEN
;
1783 dev_dbg(&port
->dev
, "%s - parity = even\n", __func__
);
1787 dev_dbg(&port
->dev
, "%s - parity = none\n", __func__
);
1791 lParity
= lParity
| 0x20;
1793 /* Change the Stop bit */
1794 if (cflag
& CSTOPB
) {
1796 dev_dbg(&port
->dev
, "%s - stop bits = 2\n", __func__
);
1799 dev_dbg(&port
->dev
, "%s - stop bits = 1\n", __func__
);
1802 /* Update the LCR with the correct value */
1803 mos7840_port
->shadowLCR
&=
1804 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
1805 mos7840_port
->shadowLCR
|= (lData
| lParity
| lStop
);
1807 dev_dbg(&port
->dev
, "%s - mos7840_port->shadowLCR is %x\n", __func__
,
1808 mos7840_port
->shadowLCR
);
1809 /* Disable Interrupts */
1811 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1814 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1817 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1819 /* Send the updated LCR value to the mos7840 */
1820 Data
= mos7840_port
->shadowLCR
;
1822 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1825 mos7840_port
->shadowMCR
= Data
;
1826 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1828 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1830 /* set up the MCR register and send it to the mos7840 */
1832 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
1834 mos7840_port
->shadowMCR
|= (MCR_DTR
| MCR_RTS
);
1836 if (cflag
& CRTSCTS
)
1837 mos7840_port
->shadowMCR
|= (MCR_XON_ANY
);
1839 mos7840_port
->shadowMCR
&= ~(MCR_XON_ANY
);
1841 Data
= mos7840_port
->shadowMCR
;
1842 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1844 /* Determine divisor based on baud rate */
1845 baud
= tty_get_baud_rate(tty
);
1848 /* pick a default, any default... */
1849 dev_dbg(&port
->dev
, "%s", "Picked default baud...\n");
1853 dev_dbg(&port
->dev
, "%s - baud rate = %d\n", __func__
, baud
);
1854 status
= mos7840_send_cmd_write_baud_rate(mos7840_port
, baud
);
1856 /* Enable Interrupts */
1858 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1860 if (!mos7840_port
->read_urb_busy
) {
1861 mos7840_port
->read_urb_busy
= true;
1862 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1864 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n",
1866 mos7840_port
->read_urb_busy
= false;
1869 dev_dbg(&port
->dev
, "%s - mos7840_port->shadowLCR is End %x\n", __func__
,
1870 mos7840_port
->shadowLCR
);
1873 /*****************************************************************************
1874 * mos7840_set_termios
1875 * this function is called by the tty driver when it wants to change
1876 * the termios structure
1877 *****************************************************************************/
1879 static void mos7840_set_termios(struct tty_struct
*tty
,
1880 struct usb_serial_port
*port
,
1881 struct ktermios
*old_termios
)
1885 struct usb_serial
*serial
;
1886 struct moschip_port
*mos7840_port
;
1888 if (mos7840_port_paranoia_check(port
, __func__
))
1891 serial
= port
->serial
;
1893 if (mos7840_serial_paranoia_check(serial
, __func__
))
1896 mos7840_port
= mos7840_get_port_private(port
);
1898 if (mos7840_port
== NULL
)
1901 if (!mos7840_port
->open
) {
1902 dev_dbg(&port
->dev
, "%s - port not opened\n", __func__
);
1906 dev_dbg(&port
->dev
, "%s", "setting termios - \n");
1908 cflag
= tty
->termios
.c_cflag
;
1910 dev_dbg(&port
->dev
, "%s - clfag %08x iflag %08x\n", __func__
,
1911 tty
->termios
.c_cflag
, RELEVANT_IFLAG(tty
->termios
.c_iflag
));
1912 dev_dbg(&port
->dev
, "%s - old clfag %08x old iflag %08x\n", __func__
,
1913 old_termios
->c_cflag
, RELEVANT_IFLAG(old_termios
->c_iflag
));
1915 /* change the port settings to the new ones specified */
1917 mos7840_change_port_settings(tty
, mos7840_port
, old_termios
);
1919 if (!mos7840_port
->read_urb
) {
1920 dev_dbg(&port
->dev
, "%s", "URB KILLED !!!!!\n");
1924 if (!mos7840_port
->read_urb_busy
) {
1925 mos7840_port
->read_urb_busy
= true;
1926 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1928 dev_dbg(&port
->dev
, "usb_submit_urb(read bulk) failed, status = %d\n",
1930 mos7840_port
->read_urb_busy
= false;
1935 /*****************************************************************************
1936 * mos7840_get_lsr_info - get line status register info
1938 * Purpose: Let user call ioctl() to get info when the UART physically
1939 * is emptied. On bus types like RS485, the transmitter must
1940 * release the bus after transmitting. This must be done when
1941 * the transmit shift register is empty, not be done when the
1942 * transmit holding register is empty. This functionality
1943 * allows an RS485 driver to be written in user space.
1944 *****************************************************************************/
1946 static int mos7840_get_lsr_info(struct tty_struct
*tty
,
1947 unsigned int __user
*value
)
1950 unsigned int result
= 0;
1952 count
= mos7840_chars_in_buffer(tty
);
1954 result
= TIOCSER_TEMT
;
1956 if (copy_to_user(value
, &result
, sizeof(int)))
1961 /*****************************************************************************
1962 * mos7840_get_serial_info
1963 * function to get information about serial port
1964 *****************************************************************************/
1966 static int mos7840_get_serial_info(struct moschip_port
*mos7840_port
,
1967 struct serial_struct __user
*retinfo
)
1969 struct serial_struct tmp
;
1971 if (mos7840_port
== NULL
)
1977 memset(&tmp
, 0, sizeof(tmp
));
1979 tmp
.type
= PORT_16550A
;
1980 tmp
.line
= mos7840_port
->port
->minor
;
1981 tmp
.port
= mos7840_port
->port
->port_number
;
1983 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
1984 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
1985 tmp
.baud_base
= 9600;
1986 tmp
.close_delay
= 5 * HZ
;
1987 tmp
.closing_wait
= 30 * HZ
;
1989 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
1994 /*****************************************************************************
1996 * this function handles any ioctl calls to the driver
1997 *****************************************************************************/
1999 static int mos7840_ioctl(struct tty_struct
*tty
,
2000 unsigned int cmd
, unsigned long arg
)
2002 struct usb_serial_port
*port
= tty
->driver_data
;
2003 void __user
*argp
= (void __user
*)arg
;
2004 struct moschip_port
*mos7840_port
;
2006 if (mos7840_port_paranoia_check(port
, __func__
))
2009 mos7840_port
= mos7840_get_port_private(port
);
2011 if (mos7840_port
== NULL
)
2015 /* return number of bytes available */
2018 dev_dbg(&port
->dev
, "%s TIOCSERGETLSR\n", __func__
);
2019 return mos7840_get_lsr_info(tty
, argp
);
2022 dev_dbg(&port
->dev
, "%s TIOCGSERIAL\n", __func__
);
2023 return mos7840_get_serial_info(mos7840_port
, argp
);
2026 dev_dbg(&port
->dev
, "%s TIOCSSERIAL\n", __func__
);
2031 return -ENOIOCTLCMD
;
2034 static int mos7810_check(struct usb_serial
*serial
)
2036 int i
, pass_count
= 0;
2038 __u16 data
= 0, mcr_data
= 0;
2039 __u16 test_pattern
= 0x55AA;
2042 buf
= kmalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
2044 return 0; /* failed to identify 7810 */
2046 /* Store MCR setting */
2047 res
= usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2048 MCS_RDREQ
, MCS_RD_RTYPE
, 0x0300, MODEM_CONTROL_REGISTER
,
2049 buf
, VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2050 if (res
== VENDOR_READ_LENGTH
)
2053 for (i
= 0; i
< 16; i
++) {
2054 /* Send the 1-bit test pattern out to MCS7810 test pin */
2055 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2056 MCS_WRREQ
, MCS_WR_RTYPE
,
2057 (0x0300 | (((test_pattern
>> i
) & 0x0001) << 1)),
2058 MODEM_CONTROL_REGISTER
, NULL
, 0, MOS_WDR_TIMEOUT
);
2060 /* Read the test pattern back */
2061 res
= usb_control_msg(serial
->dev
,
2062 usb_rcvctrlpipe(serial
->dev
, 0), MCS_RDREQ
,
2063 MCS_RD_RTYPE
, 0, GPIO_REGISTER
, buf
,
2064 VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2065 if (res
== VENDOR_READ_LENGTH
)
2068 /* If this is a MCS7810 device, both test patterns must match */
2069 if (((test_pattern
>> i
) ^ (~data
>> 1)) & 0x0001)
2075 /* Restore MCR setting */
2076 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0), MCS_WRREQ
,
2077 MCS_WR_RTYPE
, 0x0300 | mcr_data
, MODEM_CONTROL_REGISTER
, NULL
,
2078 0, MOS_WDR_TIMEOUT
);
2082 if (pass_count
== 16)
2088 static int mos7840_probe(struct usb_serial
*serial
,
2089 const struct usb_device_id
*id
)
2091 u16 product
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
2095 if (product
== MOSCHIP_DEVICE_ID_7810
||
2096 product
== MOSCHIP_DEVICE_ID_7820
) {
2097 device_type
= product
;
2101 buf
= kzalloc(VENDOR_READ_LENGTH
, GFP_KERNEL
);
2105 usb_control_msg(serial
->dev
, usb_rcvctrlpipe(serial
->dev
, 0),
2106 MCS_RDREQ
, MCS_RD_RTYPE
, 0, GPIO_REGISTER
, buf
,
2107 VENDOR_READ_LENGTH
, MOS_WDR_TIMEOUT
);
2109 /* For a MCS7840 device GPIO0 must be set to 1 */
2111 device_type
= MOSCHIP_DEVICE_ID_7840
;
2112 else if (mos7810_check(serial
))
2113 device_type
= MOSCHIP_DEVICE_ID_7810
;
2115 device_type
= MOSCHIP_DEVICE_ID_7820
;
2119 usb_set_serial_data(serial
, (void *)(unsigned long)device_type
);
2124 static int mos7840_calc_num_ports(struct usb_serial
*serial
)
2126 int device_type
= (unsigned long)usb_get_serial_data(serial
);
2127 int mos7840_num_ports
;
2129 mos7840_num_ports
= (device_type
>> 4) & 0x000F;
2131 return mos7840_num_ports
;
2134 static int mos7840_attach(struct usb_serial
*serial
)
2136 if (serial
->num_bulk_in
< serial
->num_ports
||
2137 serial
->num_bulk_out
< serial
->num_ports
||
2138 serial
->num_interrupt_in
< 1) {
2139 dev_err(&serial
->interface
->dev
, "missing endpoints\n");
2146 static int mos7840_port_probe(struct usb_serial_port
*port
)
2148 struct usb_serial
*serial
= port
->serial
;
2149 int device_type
= (unsigned long)usb_get_serial_data(serial
);
2150 struct moschip_port
*mos7840_port
;
2155 /* we set up the pointers to the endpoints in the mos7840_open *
2156 * function, as the structures aren't created yet. */
2158 pnum
= port
->port_number
;
2160 dev_dbg(&port
->dev
, "mos7840_startup: configuring port %d\n", pnum
);
2161 mos7840_port
= kzalloc(sizeof(struct moschip_port
), GFP_KERNEL
);
2165 /* Initialize all port interrupt end point to port 0 int
2166 * endpoint. Our device has only one interrupt end point
2167 * common to all port */
2169 mos7840_port
->port
= port
;
2170 mos7840_set_port_private(port
, mos7840_port
);
2171 spin_lock_init(&mos7840_port
->pool_lock
);
2173 /* minor is not initialised until later by
2174 * usb-serial.c:get_free_serial() and cannot therefore be used
2175 * to index device instances */
2176 mos7840_port
->port_num
= pnum
+ 1;
2177 dev_dbg(&port
->dev
, "port->minor = %d\n", port
->minor
);
2178 dev_dbg(&port
->dev
, "mos7840_port->port_num = %d\n", mos7840_port
->port_num
);
2180 if (mos7840_port
->port_num
== 1) {
2181 mos7840_port
->SpRegOffset
= 0x0;
2182 mos7840_port
->ControlRegOffset
= 0x1;
2183 mos7840_port
->DcrRegOffset
= 0x4;
2184 } else if ((mos7840_port
->port_num
== 2) && (serial
->num_ports
== 4)) {
2185 mos7840_port
->SpRegOffset
= 0x8;
2186 mos7840_port
->ControlRegOffset
= 0x9;
2187 mos7840_port
->DcrRegOffset
= 0x16;
2188 } else if ((mos7840_port
->port_num
== 2) && (serial
->num_ports
== 2)) {
2189 mos7840_port
->SpRegOffset
= 0xa;
2190 mos7840_port
->ControlRegOffset
= 0xb;
2191 mos7840_port
->DcrRegOffset
= 0x19;
2192 } else if ((mos7840_port
->port_num
== 3) && (serial
->num_ports
== 4)) {
2193 mos7840_port
->SpRegOffset
= 0xa;
2194 mos7840_port
->ControlRegOffset
= 0xb;
2195 mos7840_port
->DcrRegOffset
= 0x19;
2196 } else if ((mos7840_port
->port_num
== 4) && (serial
->num_ports
== 4)) {
2197 mos7840_port
->SpRegOffset
= 0xc;
2198 mos7840_port
->ControlRegOffset
= 0xd;
2199 mos7840_port
->DcrRegOffset
= 0x1c;
2201 mos7840_dump_serial_port(port
, mos7840_port
);
2202 mos7840_set_port_private(port
, mos7840_port
);
2204 /* enable rx_disable bit in control register */
2205 status
= mos7840_get_reg_sync(port
,
2206 mos7840_port
->ControlRegOffset
, &Data
);
2208 dev_dbg(&port
->dev
, "Reading ControlReg failed status-0x%x\n", status
);
2211 dev_dbg(&port
->dev
, "ControlReg Reading success val is %x, status%d\n", Data
, status
);
2212 Data
|= 0x08; /* setting driver done bit */
2213 Data
|= 0x04; /* sp1_bit to have cts change reflect in
2216 /* Data |= 0x20; //rx_disable bit */
2217 status
= mos7840_set_reg_sync(port
,
2218 mos7840_port
->ControlRegOffset
, Data
);
2220 dev_dbg(&port
->dev
, "Writing ControlReg failed(rx_disable) status-0x%x\n", status
);
2223 dev_dbg(&port
->dev
, "ControlReg Writing success(rx_disable) status%d\n", status
);
2225 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2228 status
= mos7840_set_reg_sync(port
,
2229 (__u16
) (mos7840_port
->DcrRegOffset
+ 0), Data
);
2231 dev_dbg(&port
->dev
, "Writing DCR0 failed status-0x%x\n", status
);
2234 dev_dbg(&port
->dev
, "DCR0 Writing success status%d\n", status
);
2237 status
= mos7840_set_reg_sync(port
,
2238 (__u16
) (mos7840_port
->DcrRegOffset
+ 1), Data
);
2240 dev_dbg(&port
->dev
, "Writing DCR1 failed status-0x%x\n", status
);
2243 dev_dbg(&port
->dev
, "DCR1 Writing success status%d\n", status
);
2246 status
= mos7840_set_reg_sync(port
,
2247 (__u16
) (mos7840_port
->DcrRegOffset
+ 2), Data
);
2249 dev_dbg(&port
->dev
, "Writing DCR2 failed status-0x%x\n", status
);
2252 dev_dbg(&port
->dev
, "DCR2 Writing success status%d\n", status
);
2254 /* write values in clkstart0x0 and clkmulti 0x20 */
2256 status
= mos7840_set_reg_sync(port
, CLK_START_VALUE_REGISTER
, Data
);
2258 dev_dbg(&port
->dev
, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status
);
2261 dev_dbg(&port
->dev
, "CLK_START_VALUE_REGISTER Writing success status%d\n", status
);
2264 status
= mos7840_set_reg_sync(port
, CLK_MULTI_REGISTER
, Data
);
2266 dev_dbg(&port
->dev
, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status
);
2269 dev_dbg(&port
->dev
, "CLK_MULTI_REGISTER Writing success status%d\n", status
);
2271 /* write value 0x0 to scratchpad register */
2273 status
= mos7840_set_uart_reg(port
, SCRATCH_PAD_REGISTER
, Data
);
2275 dev_dbg(&port
->dev
, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status
);
2278 dev_dbg(&port
->dev
, "SCRATCH_PAD_REGISTER Writing success status%d\n", status
);
2280 /* Zero Length flag register */
2281 if ((mos7840_port
->port_num
!= 1) && (serial
->num_ports
== 2)) {
2283 status
= mos7840_set_reg_sync(port
,
2285 ((__u16
)mos7840_port
->port_num
)), Data
);
2286 dev_dbg(&port
->dev
, "ZLIP offset %x\n",
2287 (__u16
)(ZLP_REG1
+ ((__u16
) mos7840_port
->port_num
)));
2289 dev_dbg(&port
->dev
, "Writing ZLP_REG%d failed status-0x%x\n", pnum
+ 2, status
);
2292 dev_dbg(&port
->dev
, "ZLP_REG%d Writing success status%d\n", pnum
+ 2, status
);
2295 status
= mos7840_set_reg_sync(port
,
2297 ((__u16
)mos7840_port
->port_num
) - 0x1), Data
);
2298 dev_dbg(&port
->dev
, "ZLIP offset %x\n",
2299 (__u16
)(ZLP_REG1
+ ((__u16
) mos7840_port
->port_num
) - 0x1));
2301 dev_dbg(&port
->dev
, "Writing ZLP_REG%d failed status-0x%x\n", pnum
+ 1, status
);
2304 dev_dbg(&port
->dev
, "ZLP_REG%d Writing success status%d\n", pnum
+ 1, status
);
2307 mos7840_port
->control_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2308 mos7840_port
->ctrl_buf
= kmalloc(16, GFP_KERNEL
);
2309 mos7840_port
->dr
= kmalloc(sizeof(struct usb_ctrlrequest
),
2311 if (!mos7840_port
->control_urb
|| !mos7840_port
->ctrl_buf
||
2312 !mos7840_port
->dr
) {
2317 mos7840_port
->has_led
= false;
2319 /* Initialize LED timers */
2320 if (device_type
== MOSCHIP_DEVICE_ID_7810
) {
2321 mos7840_port
->has_led
= true;
2323 mos7840_port
->led_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2324 mos7840_port
->led_dr
= kmalloc(sizeof(*mos7840_port
->led_dr
),
2326 if (!mos7840_port
->led_urb
|| !mos7840_port
->led_dr
) {
2331 setup_timer(&mos7840_port
->led_timer1
, mos7840_led_off
,
2332 (unsigned long)mos7840_port
);
2333 mos7840_port
->led_timer1
.expires
=
2334 jiffies
+ msecs_to_jiffies(LED_ON_MS
);
2335 setup_timer(&mos7840_port
->led_timer2
, mos7840_led_flag_off
,
2336 (unsigned long)mos7840_port
);
2337 mos7840_port
->led_timer2
.expires
=
2338 jiffies
+ msecs_to_jiffies(LED_OFF_MS
);
2341 mos7840_set_led_sync(port
, MODEM_CONTROL_REGISTER
, 0x0300);
2344 if (pnum
== serial
->num_ports
- 1) {
2345 /* Zero Length flag enable */
2347 status
= mos7840_set_reg_sync(serial
->port
[0], ZLP_REG5
, Data
);
2349 dev_dbg(&port
->dev
, "Writing ZLP_REG5 failed status-0x%x\n", status
);
2352 dev_dbg(&port
->dev
, "ZLP_REG5 Writing success status%d\n", status
);
2354 /* setting configuration feature to one */
2355 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2356 0x03, 0x00, 0x01, 0x00, NULL
, 0x00,
2361 kfree(mos7840_port
->led_dr
);
2362 usb_free_urb(mos7840_port
->led_urb
);
2363 kfree(mos7840_port
->dr
);
2364 kfree(mos7840_port
->ctrl_buf
);
2365 usb_free_urb(mos7840_port
->control_urb
);
2366 kfree(mos7840_port
);
2371 static int mos7840_port_remove(struct usb_serial_port
*port
)
2373 struct moschip_port
*mos7840_port
;
2375 mos7840_port
= mos7840_get_port_private(port
);
2377 if (mos7840_port
->has_led
) {
2379 mos7840_set_led_sync(port
, MODEM_CONTROL_REGISTER
, 0x0300);
2381 del_timer_sync(&mos7840_port
->led_timer1
);
2382 del_timer_sync(&mos7840_port
->led_timer2
);
2384 usb_kill_urb(mos7840_port
->led_urb
);
2385 usb_free_urb(mos7840_port
->led_urb
);
2386 kfree(mos7840_port
->led_dr
);
2388 usb_kill_urb(mos7840_port
->control_urb
);
2389 usb_free_urb(mos7840_port
->control_urb
);
2390 kfree(mos7840_port
->ctrl_buf
);
2391 kfree(mos7840_port
->dr
);
2392 kfree(mos7840_port
);
2397 static struct usb_serial_driver moschip7840_4port_device
= {
2399 .owner
= THIS_MODULE
,
2402 .description
= DRIVER_DESC
,
2403 .id_table
= id_table
,
2405 .open
= mos7840_open
,
2406 .close
= mos7840_close
,
2407 .write
= mos7840_write
,
2408 .write_room
= mos7840_write_room
,
2409 .chars_in_buffer
= mos7840_chars_in_buffer
,
2410 .throttle
= mos7840_throttle
,
2411 .unthrottle
= mos7840_unthrottle
,
2412 .calc_num_ports
= mos7840_calc_num_ports
,
2413 .probe
= mos7840_probe
,
2414 .ioctl
= mos7840_ioctl
,
2415 .set_termios
= mos7840_set_termios
,
2416 .break_ctl
= mos7840_break
,
2417 .tiocmget
= mos7840_tiocmget
,
2418 .tiocmset
= mos7840_tiocmset
,
2419 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
2420 .get_icount
= usb_serial_generic_get_icount
,
2421 .attach
= mos7840_attach
,
2422 .port_probe
= mos7840_port_probe
,
2423 .port_remove
= mos7840_port_remove
,
2424 .read_bulk_callback
= mos7840_bulk_in_callback
,
2425 .read_int_callback
= mos7840_interrupt_callback
,
2428 static struct usb_serial_driver
* const serial_drivers
[] = {
2429 &moschip7840_4port_device
, NULL
2432 module_usb_serial_driver(serial_drivers
, id_table
);
2434 MODULE_DESCRIPTION(DRIVER_DESC
);
2435 MODULE_LICENSE("GPL");