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 <asm/uaccess.h>
41 #define DRIVER_VERSION "1.3.1"
42 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
45 * 16C50 UART register defines
48 #define LCR_BITS_5 0x00 /* 5 bits/char */
49 #define LCR_BITS_6 0x01 /* 6 bits/char */
50 #define LCR_BITS_7 0x02 /* 7 bits/char */
51 #define LCR_BITS_8 0x03 /* 8 bits/char */
52 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
54 #define LCR_STOP_1 0x00 /* 1 stop bit */
55 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
56 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
57 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
59 #define LCR_PAR_NONE 0x00 /* No parity */
60 #define LCR_PAR_ODD 0x08 /* Odd parity */
61 #define LCR_PAR_EVEN 0x18 /* Even parity */
62 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
63 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
64 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
66 #define LCR_SET_BREAK 0x40 /* Set Break condition */
67 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
69 #define MCR_DTR 0x01 /* Assert DTR */
70 #define MCR_RTS 0x02 /* Assert RTS */
71 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
72 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
73 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
74 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
76 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
77 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
78 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
79 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
82 * Defines used for sending commands to port
85 #define WAIT_FOR_EVER (HZ * 0 ) /* timeout urb is wait for ever */
86 #define MOS_WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
88 #define MOS_PORT1 0x0200
89 #define MOS_PORT2 0x0300
90 #define MOS_VENREG 0x0000
91 #define MOS_MAX_PORT 0x02
92 #define MOS_WRITE 0x0E
96 #define MCS_RD_RTYPE 0xC0
97 #define MCS_WR_RTYPE 0x40
98 #define MCS_RDREQ 0x0D
99 #define MCS_WRREQ 0x0E
100 #define MCS_CTRL_TIMEOUT 500
101 #define VENDOR_READ_LENGTH (0x01)
103 #define MAX_NAME_LEN 64
105 #define ZLP_REG1 0x3A //Zero_Flag_Reg1 58
106 #define ZLP_REG5 0x3E //Zero_Flag_Reg5 62
108 /* For higher baud Rates use TIOCEXBAUD */
109 #define TIOCEXBAUD 0x5462
111 /* vendor id and device id defines */
113 <<<<<<< HEAD
:drivers
/usb
/serial
/mos7840
.c
115 /* The native mos7840/7820 component */
116 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/usb
/serial
/mos7840
.c
117 #define USB_VENDOR_ID_MOSCHIP 0x9710
118 #define MOSCHIP_DEVICE_ID_7840 0x7840
119 #define MOSCHIP_DEVICE_ID_7820 0x7820
120 <<<<<<< HEAD
:drivers
/usb
/serial
/mos7840
.c
122 /* The native component can have its vendor/device id's overridden
123 * in vendor-specific implementations. Such devices can be handled
124 * by making a change here, in moschip_port_id_table, and in
125 * moschip_id_table_combined
127 #define USB_VENDOR_ID_BANDB 0x0856
128 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
129 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
130 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/usb
/serial
/mos7840
.c
132 <<<<<<< HEAD
:drivers
/usb
/serial
/mos7840
.c
133 /* Interrupt Rotinue Defines */
135 /* Interrupt Routine Defines */
136 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/usb
/serial
/mos7840
.c
138 #define SERIAL_IIR_RLS 0x06
139 #define SERIAL_IIR_MS 0x00
142 * Emulation of the bit mask on the LINE STATUS REGISTER.
144 #define SERIAL_LSR_DR 0x0001
145 #define SERIAL_LSR_OE 0x0002
146 #define SERIAL_LSR_PE 0x0004
147 #define SERIAL_LSR_FE 0x0008
148 #define SERIAL_LSR_BI 0x0010
150 #define MOS_MSR_DELTA_CTS 0x10
151 #define MOS_MSR_DELTA_DSR 0x20
152 #define MOS_MSR_DELTA_RI 0x40
153 #define MOS_MSR_DELTA_CD 0x80
155 // Serial Port register Address
156 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
157 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
158 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
159 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
160 #define LINE_STATUS_REGISTER ((__u16)(0x05))
161 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
162 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
163 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
164 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
166 #define CLK_MULTI_REGISTER ((__u16)(0x02))
167 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
169 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
172 * URB POOL related defines
174 #define NUM_URBS 16 /* URB Count */
175 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
178 static struct usb_device_id moschip_port_id_table
[] = {
179 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
180 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
181 <<<<<<< HEAD
:drivers
/usb
/serial
/mos7840
.c
183 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
184 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
185 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/usb
/serial
/mos7840
.c
186 {} /* terminating entry */
189 static __devinitdata
struct usb_device_id moschip_id_table_combined
[] = {
190 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
192 <<<<<<< HEAD
:drivers
/usb
/serial
/mos7840
.c
194 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
196 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/usb
/serial
/mos7840
.c
197 {} /* terminating entry */
200 MODULE_DEVICE_TABLE(usb
, moschip_id_table_combined
);
202 /* This structure holds all of the local port information */
204 struct moschip_port
{
205 int port_num
; /*Actual port number in the device(1,2,etc) */
206 struct urb
*write_urb
; /* write URB for this port */
207 struct urb
*read_urb
; /* read URB for this port */
209 __u8 shadowLCR
; /* last LCR value received */
210 __u8 shadowMCR
; /* last MCR value received */
214 wait_queue_head_t wait_chase
; /* for handling sleeping while waiting for chase to finish */
215 wait_queue_head_t delta_msr_wait
; /* for handling sleeping while waiting for msr change to happen */
217 struct async_icount icount
;
218 struct usb_serial_port
*port
; /* loop back to the owner of this object */
222 __u8 ControlRegOffset
;
224 //for processing control URBS in interrupt context
225 struct urb
*control_urb
;
226 struct usb_ctrlrequest
*dr
;
230 spinlock_t pool_lock
;
231 struct urb
*write_urb_pool
[NUM_URBS
];
239 * mos7840_set_reg_sync
240 * To set the Control register by calling usb_fill_control_urb function
241 * by passing usb_sndctrlpipe function as parameter.
244 static int mos7840_set_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
247 struct usb_device
*dev
= port
->serial
->dev
;
249 dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg
, val
);
251 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
252 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
257 * mos7840_get_reg_sync
258 * To set the Uart register by calling usb_fill_control_urb function by
259 * passing usb_rcvctrlpipe function as parameter.
262 static int mos7840_get_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
265 struct usb_device
*dev
= port
->serial
->dev
;
268 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
269 MCS_RD_RTYPE
, 0, reg
, val
, VENDOR_READ_LENGTH
,
271 dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg
, *val
);
272 *val
= (*val
) & 0x00ff;
277 * mos7840_set_uart_reg
278 * To set the Uart register by calling usb_fill_control_urb function by
279 * passing usb_sndctrlpipe function as parameter.
282 static int mos7840_set_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
286 struct usb_device
*dev
= port
->serial
->dev
;
288 // For the UART control registers, the application number need to be Or'ed
289 if (port
->serial
->num_ports
== 4) {
291 (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) +
293 dbg("mos7840_set_uart_reg application number is %x\n", val
);
295 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
297 (((__u16
) port
->number
-
298 (__u16
) (port
->serial
->minor
)) + 1) << 8;
299 dbg("mos7840_set_uart_reg application number is %x\n",
303 (((__u16
) port
->number
-
304 (__u16
) (port
->serial
->minor
)) + 2) << 8;
305 dbg("mos7840_set_uart_reg application number is %x\n",
309 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
310 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
316 * mos7840_get_uart_reg
317 * To set the Control register by calling usb_fill_control_urb function
318 * by passing usb_rcvctrlpipe function as parameter.
320 static int mos7840_get_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
323 struct usb_device
*dev
= port
->serial
->dev
;
327 //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
328 /*Wval is same as application number */
329 if (port
->serial
->num_ports
== 4) {
331 (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) +
333 dbg("mos7840_get_uart_reg application number is %x\n", Wval
);
335 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
337 (((__u16
) port
->number
-
338 (__u16
) (port
->serial
->minor
)) + 1) << 8;
339 dbg("mos7840_get_uart_reg application number is %x\n",
343 (((__u16
) port
->number
-
344 (__u16
) (port
->serial
->minor
)) + 2) << 8;
345 dbg("mos7840_get_uart_reg application number is %x\n",
349 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
350 MCS_RD_RTYPE
, Wval
, reg
, val
, VENDOR_READ_LENGTH
,
352 *val
= (*val
) & 0x00ff;
356 static void mos7840_dump_serial_port(struct moschip_port
*mos7840_port
)
359 dbg("***************************************\n");
360 dbg("SpRegOffset is %2x\n", mos7840_port
->SpRegOffset
);
361 dbg("ControlRegOffset is %2x \n", mos7840_port
->ControlRegOffset
);
362 dbg("DCRRegOffset is %2x \n", mos7840_port
->DcrRegOffset
);
363 dbg("***************************************\n");
367 /************************************************************************/
368 /************************************************************************/
369 /* I N T E R F A C E F U N C T I O N S */
370 /* I N T E R F A C E F U N C T I O N S */
371 /************************************************************************/
372 /************************************************************************/
374 static inline void mos7840_set_port_private(struct usb_serial_port
*port
,
375 struct moschip_port
*data
)
377 usb_set_serial_port_data(port
, (void *)data
);
380 static inline struct moschip_port
*mos7840_get_port_private(struct
384 return (struct moschip_port
*)usb_get_serial_port_data(port
);
387 static void mos7840_handle_new_msr(struct moschip_port
*port
, __u8 new_msr
)
389 struct moschip_port
*mos7840_port
;
390 struct async_icount
*icount
;
392 icount
= &mos7840_port
->icount
;
394 (MOS_MSR_DELTA_CTS
| MOS_MSR_DELTA_DSR
| MOS_MSR_DELTA_RI
|
396 icount
= &mos7840_port
->icount
;
398 /* update input line counters */
399 if (new_msr
& MOS_MSR_DELTA_CTS
) {
403 if (new_msr
& MOS_MSR_DELTA_DSR
) {
407 if (new_msr
& MOS_MSR_DELTA_CD
) {
411 if (new_msr
& MOS_MSR_DELTA_RI
) {
418 static void mos7840_handle_new_lsr(struct moschip_port
*port
, __u8 new_lsr
)
420 struct async_icount
*icount
;
422 dbg("%s - %02x", __FUNCTION__
, new_lsr
);
424 if (new_lsr
& SERIAL_LSR_BI
) {
426 // Parity and Framing errors only count if they
427 // occur exclusive of a break being
430 new_lsr
&= (__u8
) (SERIAL_LSR_OE
| SERIAL_LSR_BI
);
433 /* update input line counters */
434 icount
= &port
->icount
;
435 if (new_lsr
& SERIAL_LSR_BI
) {
439 if (new_lsr
& SERIAL_LSR_OE
) {
443 if (new_lsr
& SERIAL_LSR_PE
) {
447 if (new_lsr
& SERIAL_LSR_FE
) {
453 /************************************************************************/
454 /************************************************************************/
455 /* U S B C A L L B A C K F U N C T I O N S */
456 /* U S B C A L L B A C K F U N C T I O N S */
457 /************************************************************************/
458 /************************************************************************/
460 static void mos7840_control_callback(struct urb
*urb
)
463 struct moschip_port
*mos7840_port
;
466 int status
= urb
->status
;
468 mos7840_port
= (struct moschip_port
*)urb
->context
;
477 /* this urb is terminated, clean up */
478 dbg("%s - urb shutting down with status: %d", __FUNCTION__
,
482 dbg("%s - nonzero urb status received: %d", __FUNCTION__
,
487 dbg("%s urb buffer size is %d\n", __FUNCTION__
, urb
->actual_length
);
488 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__
,
489 mos7840_port
->MsrLsr
, mos7840_port
->port_num
);
490 data
= urb
->transfer_buffer
;
491 regval
= (__u8
) data
[0];
492 dbg("%s data is %x\n", __FUNCTION__
, 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
);
499 spin_lock(&mos7840_port
->pool_lock
);
500 if (!mos7840_port
->zombie
)
501 result
= usb_submit_urb(mos7840_port
->int_urb
, GFP_ATOMIC
);
502 spin_unlock(&mos7840_port
->pool_lock
);
504 dev_err(&urb
->dev
->dev
,
505 "%s - Error %d submitting interrupt urb\n",
506 __FUNCTION__
, result
);
510 static int mos7840_get_reg(struct moschip_port
*mcs
, __u16 Wval
, __u16 reg
,
513 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
514 struct usb_ctrlrequest
*dr
= mcs
->dr
;
515 unsigned char *buffer
= mcs
->ctrl_buf
;
518 dr
->bRequestType
= MCS_RD_RTYPE
;
519 dr
->bRequest
= MCS_RDREQ
;
520 dr
->wValue
= cpu_to_le16(Wval
); //0;
521 dr
->wIndex
= cpu_to_le16(reg
);
522 dr
->wLength
= cpu_to_le16(2);
524 usb_fill_control_urb(mcs
->control_urb
, dev
, usb_rcvctrlpipe(dev
, 0),
525 (unsigned char *)dr
, buffer
, 2,
526 mos7840_control_callback
, mcs
);
527 mcs
->control_urb
->transfer_buffer_length
= 2;
528 ret
= usb_submit_urb(mcs
->control_urb
, GFP_ATOMIC
);
532 /*****************************************************************************
533 * mos7840_interrupt_callback
534 * this is the callback function for when we have received data on the
535 * interrupt endpoint.
536 *****************************************************************************/
538 static void mos7840_interrupt_callback(struct urb
*urb
)
542 struct moschip_port
*mos7840_port
;
543 struct usb_serial
*serial
;
548 __u16 wval
, wreg
= 0;
549 int status
= urb
->status
;
551 dbg("%s", " : Entering\n");
560 /* this urb is terminated, clean up */
561 dbg("%s - urb shutting down with status: %d", __FUNCTION__
,
565 dbg("%s - nonzero urb status received: %d", __FUNCTION__
,
570 length
= urb
->actual_length
;
571 data
= urb
->transfer_buffer
;
573 serial
= (struct usb_serial
*)urb
->context
;
575 /* Moschip get 5 bytes
576 * Byte 1 IIR Port 1 (port.number is 0)
577 * Byte 2 IIR Port 2 (port.number is 1)
578 * Byte 3 IIR Port 3 (port.number is 2)
579 * Byte 4 IIR Port 4 (port.number is 3)
580 * Byte 5 FIFO status for both */
582 if (length
&& length
> 5) {
583 dbg("%s \n", "Wrong data !!!");
587 sp
[0] = (__u8
) data
[0];
588 sp
[1] = (__u8
) data
[1];
589 sp
[2] = (__u8
) data
[2];
590 sp
[3] = (__u8
) data
[3];
593 for (i
= 0; i
< serial
->num_ports
; i
++) {
594 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
596 (((__u16
) serial
->port
[i
]->number
-
597 (__u16
) (serial
->minor
)) + 1) << 8;
598 if (mos7840_port
->open
) {
600 dbg("SP%d No Interrupt !!!\n", i
);
602 switch (sp
[i
] & 0x0f) {
604 dbg("Serial Port %d: Receiver status error or ", i
);
605 dbg("address bit detected in 9-bit mode\n");
606 mos7840_port
->MsrLsr
= 1;
607 wreg
= LINE_STATUS_REGISTER
;
610 dbg("Serial Port %d: Modem status change\n", i
);
611 mos7840_port
->MsrLsr
= 0;
612 wreg
= MODEM_STATUS_REGISTER
;
615 spin_lock(&mos7840_port
->pool_lock
);
616 if (!mos7840_port
->zombie
) {
617 rv
= mos7840_get_reg(mos7840_port
, wval
, wreg
, &Data
);
619 spin_unlock(&mos7840_port
->pool_lock
);
622 spin_unlock(&mos7840_port
->pool_lock
);
626 if (!(rv
< 0)) /* the completion handler for the control urb will resubmit */
629 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
631 dev_err(&urb
->dev
->dev
,
632 "%s - Error %d submitting interrupt urb\n",
633 __FUNCTION__
, result
);
637 static int mos7840_port_paranoia_check(struct usb_serial_port
*port
,
638 const char *function
)
641 dbg("%s - port == NULL", function
);
645 dbg("%s - port->serial == NULL", function
);
652 /* Inline functions to check the sanity of a pointer that is passed to us */
653 static int mos7840_serial_paranoia_check(struct usb_serial
*serial
,
654 const char *function
)
657 dbg("%s - serial == NULL", function
);
661 dbg("%s - serial->type == NULL!", function
);
668 static struct usb_serial
*mos7840_get_usb_serial(struct usb_serial_port
*port
,
669 const char *function
)
671 /* if no port was specified, or it fails a paranoia check */
673 mos7840_port_paranoia_check(port
, function
) ||
674 mos7840_serial_paranoia_check(port
->serial
, function
)) {
675 /* then say that we don't have a valid usb_serial thing, which will
676 * end up genrating -ENODEV return values */
683 /*****************************************************************************
684 * mos7840_bulk_in_callback
685 * this is the callback function for when we have received data on the
687 *****************************************************************************/
689 static void mos7840_bulk_in_callback(struct urb
*urb
)
693 struct usb_serial
*serial
;
694 struct usb_serial_port
*port
;
695 struct moschip_port
*mos7840_port
;
696 struct tty_struct
*tty
;
697 int status
= urb
->status
;
700 dbg("nonzero read bulk status received: %d", status
);
704 mos7840_port
= (struct moschip_port
*)urb
->context
;
706 dbg("%s", "NULL mos7840_port pointer \n");
710 port
= (struct usb_serial_port
*)mos7840_port
->port
;
711 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
712 dbg("%s", "Port Paranoia failed \n");
716 serial
= mos7840_get_usb_serial(port
, __FUNCTION__
);
718 dbg("%s\n", "Bad serial pointer ");
722 dbg("%s\n", "Entering... \n");
724 data
= urb
->transfer_buffer
;
726 dbg("%s", "Entering ........... \n");
728 if (urb
->actual_length
) {
729 tty
= mos7840_port
->port
->tty
;
731 tty_buffer_request_room(tty
, urb
->actual_length
);
732 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
734 tty_flip_buffer_push(tty
);
736 mos7840_port
->icount
.rx
+= urb
->actual_length
;
738 dbg("mos7840_port->icount.rx is %d:\n",
739 mos7840_port
->icount
.rx
);
742 if (!mos7840_port
->read_urb
) {
743 dbg("%s", "URB KILLED !!!\n");
748 mos7840_port
->read_urb
->dev
= serial
->dev
;
750 retval
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
753 dbg(" usb_submit_urb(read bulk) failed, retval = %d",
758 /*****************************************************************************
759 * mos7840_bulk_out_data_callback
760 * this is the callback function for when we have finished sending serial data
761 * on the bulk out endpoint.
762 *****************************************************************************/
764 static void mos7840_bulk_out_data_callback(struct urb
*urb
)
766 struct moschip_port
*mos7840_port
;
767 struct tty_struct
*tty
;
768 int status
= urb
->status
;
771 mos7840_port
= (struct moschip_port
*)urb
->context
;
772 spin_lock(&mos7840_port
->pool_lock
);
773 for (i
= 0; i
< NUM_URBS
; i
++) {
774 if (urb
== mos7840_port
->write_urb_pool
[i
]) {
775 mos7840_port
->busy
[i
] = 0;
779 spin_unlock(&mos7840_port
->pool_lock
);
782 dbg("nonzero write bulk status received:%d\n", status
);
786 if (mos7840_port_paranoia_check(mos7840_port
->port
, __FUNCTION__
)) {
787 dbg("%s", "Port Paranoia failed \n");
791 dbg("%s \n", "Entering .........");
793 tty
= mos7840_port
->port
->tty
;
795 if (tty
&& mos7840_port
->open
)
800 /************************************************************************/
801 /* 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 */
802 /************************************************************************/
803 #ifdef MCSSerialProbe
804 static int mos7840_serial_probe(struct usb_serial
*serial
,
805 const struct usb_device_id
*id
)
808 /*need to implement the mode_reg reading and updating\
809 structures usb_serial_ device_type\
810 (i.e num_ports, num_bulkin,bulkout etc) */
811 /* Also we can update the changes attach */
816 /*****************************************************************************
818 * this function is called by the tty driver when a port is opened
819 * If successful, we return 0
820 * Otherwise we return a negative error number.
821 *****************************************************************************/
823 static int mos7840_open(struct usb_serial_port
*port
, struct file
*filp
)
827 struct usb_serial
*serial
;
831 struct moschip_port
*mos7840_port
;
832 struct moschip_port
*port0
;
834 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
835 dbg("%s", "Port Paranoia failed \n");
839 serial
= port
->serial
;
841 if (mos7840_serial_paranoia_check(serial
, __FUNCTION__
)) {
842 dbg("%s", "Serial Paranoia failed \n");
846 mos7840_port
= mos7840_get_port_private(port
);
847 port0
= mos7840_get_port_private(serial
->port
[0]);
849 if (mos7840_port
== NULL
|| port0
== NULL
)
852 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
853 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
856 /* Initialising the write urb pool */
857 for (j
= 0; j
< NUM_URBS
; ++j
) {
858 urb
= usb_alloc_urb(0, GFP_KERNEL
);
859 mos7840_port
->write_urb_pool
[j
] = urb
;
862 err("No more urbs???");
866 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
, GFP_KERNEL
);
867 if (!urb
->transfer_buffer
) {
869 mos7840_port
->write_urb_pool
[j
] = NULL
;
870 err("%s-out of memory for urb buffers.", __FUNCTION__
);
875 /*****************************************************************************
876 * Initialize MCS7840 -- Write Init values to corresponding Registers
884 * 0x08 : SP1/2 Control Reg
885 *****************************************************************************/
887 //NEED to check the following Block
891 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
893 dbg("Reading Spreg failed\n");
897 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
899 dbg("writing Spreg failed\n");
904 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
906 dbg("writing Spreg failed\n");
909 //End of block to be checked
914 mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
, &Data
);
916 dbg("Reading Controlreg failed\n");
919 Data
|= 0x08; //Driver done bit
920 Data
|= 0x20; //rx_disable
921 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
, Data
);
923 dbg("writing Controlreg failed\n");
926 //do register settings here
927 // Set all regs to the device default values.
928 ////////////////////////////////////
929 // First Disable all interrupts.
930 ////////////////////////////////////
933 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
935 dbg("disableing interrupts failed\n");
938 // Set FIFO_CONTROL_REGISTER to the default value
940 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
942 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
947 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
949 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
954 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
955 mos7840_port
->shadowLCR
= Data
;
958 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
959 mos7840_port
->shadowMCR
= Data
;
962 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
963 mos7840_port
->shadowLCR
= Data
;
965 Data
|= SERIAL_LCR_DLAB
; //data latch enable in LCR 0x80
966 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
970 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
974 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
978 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
980 Data
= Data
& ~SERIAL_LCR_DLAB
;
982 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
983 mos7840_port
->shadowLCR
= Data
;
985 //clearing Bulkin and Bulkout Fifo
988 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
992 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
996 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
997 //Finally enable all interrupts
1001 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1003 //clearing rx_disable
1007 mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
, &Data
);
1008 Data
= Data
& ~0x20;
1011 mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
, Data
);
1017 mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
, &Data
);
1021 mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
, Data
);
1023 /* force low_latency on so that our tty_push actually forces *
1024 * the data through,otherwise it is scheduled, and with *
1025 * high data rates (like with OHCI) data can get lost. */
1028 port
->tty
->low_latency
= 1;
1029 /* Check to see if we've set up our endpoint info yet *
1030 * (can't set it up in mos7840_startup as the structures *
1031 * were not set up at that time.) */
1032 if (port0
->open_ports
== 1) {
1033 if (serial
->port
[0]->interrupt_in_buffer
== NULL
) {
1035 /* set up interrupt urb */
1037 usb_fill_int_urb(serial
->port
[0]->interrupt_in_urb
,
1039 usb_rcvintpipe(serial
->dev
,
1041 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
->
1050 /* start interrupt read for mos7840 *
1051 * will continue as long as mos7840 is connected */
1054 usb_submit_urb(serial
->port
[0]->interrupt_in_urb
,
1057 err("%s - Error %d submitting interrupt urb",
1058 __FUNCTION__
, response
);
1065 /* see if we've set up our endpoint info yet *
1066 * (can't set it up in mos7840_startup as the *
1067 * structures were not set up at that time.) */
1069 dbg("port number is %d \n", port
->number
);
1070 dbg("serial number is %d \n", port
->serial
->minor
);
1071 dbg("Bulkin endpoint is %d \n", port
->bulk_in_endpointAddress
);
1072 dbg("BulkOut endpoint is %d \n", port
->bulk_out_endpointAddress
);
1073 dbg("Interrupt endpoint is %d \n", port
->interrupt_in_endpointAddress
);
1074 dbg("port's number in the device is %d\n", mos7840_port
->port_num
);
1075 mos7840_port
->read_urb
= port
->read_urb
;
1077 /* set up our bulk in urb */
1079 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1081 usb_rcvbulkpipe(serial
->dev
,
1082 port
->bulk_in_endpointAddress
),
1083 port
->bulk_in_buffer
,
1084 mos7840_port
->read_urb
->transfer_buffer_length
,
1085 mos7840_bulk_in_callback
, mos7840_port
);
1087 dbg("mos7840_open: bulkin endpoint is %d\n",
1088 port
->bulk_in_endpointAddress
);
1089 response
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1091 err("%s - Error %d submitting control urb", __FUNCTION__
,
1095 /* initialize our wait queues */
1096 init_waitqueue_head(&mos7840_port
->wait_chase
);
1097 init_waitqueue_head(&mos7840_port
->delta_msr_wait
);
1099 /* initialize our icount structure */
1100 memset(&(mos7840_port
->icount
), 0x00, sizeof(mos7840_port
->icount
));
1102 /* initialize our port settings */
1103 mos7840_port
->shadowMCR
= MCR_MASTER_IE
; /* Must set to enable ints! */
1104 /* send a open port command */
1105 mos7840_port
->open
= 1;
1106 //mos7840_change_port_settings(mos7840_port,old_termios);
1107 mos7840_port
->icount
.tx
= 0;
1108 mos7840_port
->icount
.rx
= 0;
1110 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial
, mos7840_port
, port
);
1116 /*****************************************************************************
1117 * mos7840_chars_in_buffer
1118 * this function is called by the tty driver when it wants to know how many
1119 * bytes of data we currently have outstanding in the port (data that has
1120 * been written, but hasn't made it out the port yet)
1121 * If successful, we return the number of bytes left to be written in the
1123 * Otherwise we return a negative error number.
1124 *****************************************************************************/
1126 static int mos7840_chars_in_buffer(struct usb_serial_port
*port
)
1130 unsigned long flags
;
1131 struct moschip_port
*mos7840_port
;
1133 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
1135 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
1136 dbg("%s", "Invalid port \n");
1140 mos7840_port
= mos7840_get_port_private(port
);
1141 if (mos7840_port
== NULL
) {
1142 dbg("%s \n", "mos7840_break:leaving ...........");
1146 spin_lock_irqsave(&mos7840_port
->pool_lock
,flags
);
1147 for (i
= 0; i
< NUM_URBS
; ++i
) {
1148 if (mos7840_port
->busy
[i
]) {
1149 chars
+= URB_TRANSFER_BUFFER_SIZE
;
1152 spin_unlock_irqrestore(&mos7840_port
->pool_lock
,flags
);
1153 dbg("%s - returns %d", __FUNCTION__
, chars
);
1158 /************************************************************************
1160 * mos7840_block_until_tx_empty
1162 * This function will block the close until one of the following:
1164 * 2. The mos7840 has stopped
1165 * 3. A timeout of 3 seconds without activity has expired
1167 ************************************************************************/
1168 static void mos7840_block_until_tx_empty(struct moschip_port
*mos7840_port
)
1170 int timeout
= HZ
/ 10;
1176 count
= mos7840_chars_in_buffer(mos7840_port
->port
);
1178 /* Check for Buffer status */
1183 /* Block the thread for a while */
1184 interruptible_sleep_on_timeout(&mos7840_port
->wait_chase
,
1187 /* No activity.. count down section */
1190 dbg("%s - TIMEOUT", __FUNCTION__
);
1193 /* Reset timeout value back to seconds */
1199 /*****************************************************************************
1201 * this function is called by the tty driver when a port is closed
1202 *****************************************************************************/
1204 static void mos7840_close(struct usb_serial_port
*port
, struct file
*filp
)
1206 struct usb_serial
*serial
;
1207 struct moschip_port
*mos7840_port
;
1208 struct moschip_port
*port0
;
1212 dbg("%s\n", "mos7840_close:entering...");
1214 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
1215 dbg("%s", "Port Paranoia failed \n");
1219 serial
= mos7840_get_usb_serial(port
, __FUNCTION__
);
1221 dbg("%s", "Serial Paranoia failed \n");
1225 mos7840_port
= mos7840_get_port_private(port
);
1226 port0
= mos7840_get_port_private(serial
->port
[0]);
1228 if (mos7840_port
== NULL
|| port0
== NULL
)
1231 for (j
= 0; j
< NUM_URBS
; ++j
)
1232 usb_kill_urb(mos7840_port
->write_urb_pool
[j
]);
1234 /* Freeing Write URBs */
1235 for (j
= 0; j
< NUM_URBS
; ++j
) {
1236 if (mos7840_port
->write_urb_pool
[j
]) {
1237 if (mos7840_port
->write_urb_pool
[j
]->transfer_buffer
)
1238 kfree(mos7840_port
->write_urb_pool
[j
]->
1241 usb_free_urb(mos7840_port
->write_urb_pool
[j
]);
1246 /* flush and block until tx is empty */
1247 mos7840_block_until_tx_empty(mos7840_port
);
1250 /* While closing port, shutdown all bulk read, write *
1251 * and interrupt read if they exists */
1254 if (mos7840_port
->write_urb
) {
1255 dbg("%s", "Shutdown bulk write\n");
1256 usb_kill_urb(mos7840_port
->write_urb
);
1259 if (mos7840_port
->read_urb
) {
1260 dbg("%s", "Shutdown bulk read\n");
1261 usb_kill_urb(mos7840_port
->read_urb
);
1263 if ((&mos7840_port
->control_urb
)) {
1264 dbg("%s", "Shutdown control read\n");
1265 // usb_kill_urb (mos7840_port->control_urb);
1269 // if(mos7840_port->ctrl_buf != NULL)
1270 // kfree(mos7840_port->ctrl_buf);
1271 port0
->open_ports
--;
1272 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1273 port0
->open_ports
, port
->number
);
1274 if (port0
->open_ports
== 0) {
1275 if (serial
->port
[0]->interrupt_in_urb
) {
1276 dbg("%s", "Shutdown interrupt_in_urb\n");
1277 usb_kill_urb(serial
->port
[0]->interrupt_in_urb
);
1281 if (mos7840_port
->write_urb
) {
1282 /* if this urb had a transfer buffer already (old tx) free it */
1284 if (mos7840_port
->write_urb
->transfer_buffer
!= NULL
) {
1285 kfree(mos7840_port
->write_urb
->transfer_buffer
);
1287 usb_free_urb(mos7840_port
->write_urb
);
1291 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1294 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1296 mos7840_port
->open
= 0;
1298 dbg("%s \n", "Leaving ............");
1301 /************************************************************************
1303 * mos7840_block_until_chase_response
1305 * This function will block the close until one of the following:
1306 * 1. Response to our Chase comes from mos7840
1307 * 2. A timeout of 10 seconds without activity has expired
1308 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1310 ************************************************************************/
1312 static void mos7840_block_until_chase_response(struct moschip_port
1315 int timeout
= 1 * HZ
;
1320 count
= mos7840_chars_in_buffer(mos7840_port
->port
);
1322 /* Check for Buffer status */
1327 /* Block the thread for a while */
1328 interruptible_sleep_on_timeout(&mos7840_port
->wait_chase
,
1330 /* No activity.. count down section */
1333 dbg("%s - TIMEOUT", __FUNCTION__
);
1336 /* Reset timeout value back to seconds */
1343 /*****************************************************************************
1345 * this function sends a break to the port
1346 *****************************************************************************/
1347 static void mos7840_break(struct usb_serial_port
*port
, int break_state
)
1350 struct usb_serial
*serial
;
1351 struct moschip_port
*mos7840_port
;
1353 dbg("%s \n", "Entering ...........");
1354 dbg("mos7840_break: Start\n");
1356 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
1357 dbg("%s", "Port Paranoia failed \n");
1361 serial
= mos7840_get_usb_serial(port
, __FUNCTION__
);
1363 dbg("%s", "Serial Paranoia failed \n");
1367 mos7840_port
= mos7840_get_port_private(port
);
1369 if (mos7840_port
== NULL
) {
1375 /* flush and block until tx is empty */
1376 mos7840_block_until_chase_response(mos7840_port
);
1379 if (break_state
== -1) {
1380 data
= mos7840_port
->shadowLCR
| LCR_SET_BREAK
;
1382 data
= mos7840_port
->shadowLCR
& ~LCR_SET_BREAK
;
1385 mos7840_port
->shadowLCR
= data
;
1386 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1387 mos7840_port
->shadowLCR
);
1388 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
,
1389 mos7840_port
->shadowLCR
);
1394 /*****************************************************************************
1395 * mos7840_write_room
1396 * this function is called by the tty driver when it wants to know how many
1397 * bytes of data we can accept for a specific port.
1398 * If successful, we return the amount of room that we have for this port
1399 * Otherwise we return a negative error number.
1400 *****************************************************************************/
1402 static int mos7840_write_room(struct usb_serial_port
*port
)
1406 unsigned long flags
;
1407 struct moschip_port
*mos7840_port
;
1409 dbg("%s \n", " mos7840_write_room:entering ...........");
1411 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
1412 dbg("%s", "Invalid port \n");
1413 dbg("%s \n", " mos7840_write_room:leaving ...........");
1417 mos7840_port
= mos7840_get_port_private(port
);
1418 if (mos7840_port
== NULL
) {
1419 dbg("%s \n", "mos7840_break:leaving ...........");
1423 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1424 for (i
= 0; i
< NUM_URBS
; ++i
) {
1425 if (!mos7840_port
->busy
[i
]) {
1426 room
+= URB_TRANSFER_BUFFER_SIZE
;
1429 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1431 room
= (room
== 0) ? 0 : room
- URB_TRANSFER_BUFFER_SIZE
+ 1;
1432 dbg("%s - returns %d", __FUNCTION__
, room
);
1437 /*****************************************************************************
1439 * this function is called by the tty driver when data should be written to
1441 * If successful, we return the number of bytes written, otherwise we
1442 * return a negative error number.
1443 *****************************************************************************/
1445 static int mos7840_write(struct usb_serial_port
*port
,
1446 const unsigned char *data
, int count
)
1452 unsigned long flags
;
1454 struct moschip_port
*mos7840_port
;
1455 struct usb_serial
*serial
;
1458 const unsigned char *current_position
= data
;
1459 unsigned char *data1
;
1460 dbg("%s \n", "entering ...........");
1461 //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
1466 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
1467 mos7840_port
->shadowLCR
= Data
;
1468 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data
);
1469 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1470 mos7840_port
->shadowLCR
);
1473 //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
1474 //mos7840_port->shadowLCR=Data;//Need to add later
1476 Data
|= SERIAL_LCR_DLAB
; //data latch enable in LCR 0x80
1478 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1481 //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
1484 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_LSB
, &Data
);
1485 dbg("mos7840_write:DLL value is %x\n", Data
);
1489 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_MSB
, &Data
);
1490 dbg("mos7840_write:DLM value is %x\n", Data
);
1492 Data
= Data
& ~SERIAL_LCR_DLAB
;
1493 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1494 mos7840_port
->shadowLCR
);
1496 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1499 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
1500 dbg("%s", "Port Paranoia failed \n");
1504 serial
= port
->serial
;
1505 if (mos7840_serial_paranoia_check(serial
, __FUNCTION__
)) {
1506 dbg("%s", "Serial Paranoia failed \n");
1510 mos7840_port
= mos7840_get_port_private(port
);
1511 if (mos7840_port
== NULL
) {
1512 dbg("%s", "mos7840_port is NULL\n");
1516 /* try to find a free urb in the list */
1519 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1520 for (i
= 0; i
< NUM_URBS
; ++i
) {
1521 if (!mos7840_port
->busy
[i
]) {
1522 mos7840_port
->busy
[i
] = 1;
1523 urb
= mos7840_port
->write_urb_pool
[i
];
1528 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1531 dbg("%s - no more free urbs", __FUNCTION__
);
1535 if (urb
->transfer_buffer
== NULL
) {
1536 urb
->transfer_buffer
=
1537 kmalloc(URB_TRANSFER_BUFFER_SIZE
, GFP_KERNEL
);
1539 if (urb
->transfer_buffer
== NULL
) {
1540 err("%s no more kernel memory...", __FUNCTION__
);
1544 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1546 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1548 /* fill urb with data and submit */
1549 usb_fill_bulk_urb(urb
,
1551 usb_sndbulkpipe(serial
->dev
,
1552 port
->bulk_out_endpointAddress
),
1553 urb
->transfer_buffer
,
1555 mos7840_bulk_out_data_callback
, mos7840_port
);
1557 data1
= urb
->transfer_buffer
;
1558 dbg("\nbulkout endpoint is %d", port
->bulk_out_endpointAddress
);
1560 /* send it down the pipe */
1561 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1564 mos7840_port
->busy
[i
] = 0;
1565 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1566 __FUNCTION__
, status
);
1567 bytes_sent
= status
;
1570 bytes_sent
= transfer_size
;
1571 mos7840_port
->icount
.tx
+= transfer_size
;
1573 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port
->icount
.tx
);
1580 /*****************************************************************************
1582 * this function is called by the tty driver when it wants to stop the data
1583 * being read from the port.
1584 *****************************************************************************/
1586 static void mos7840_throttle(struct usb_serial_port
*port
)
1588 struct moschip_port
*mos7840_port
;
1589 struct tty_struct
*tty
;
1592 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
1593 dbg("%s", "Invalid port \n");
1597 dbg("- port %d\n", port
->number
);
1599 mos7840_port
= mos7840_get_port_private(port
);
1601 if (mos7840_port
== NULL
)
1604 if (!mos7840_port
->open
) {
1605 dbg("%s\n", "port not opened");
1609 dbg("%s", "Entering .......... \n");
1613 dbg("%s - no tty available", __FUNCTION__
);
1617 /* if we are implementing XON/XOFF, send the stop character */
1619 unsigned char stop_char
= STOP_CHAR(tty
);
1620 status
= mos7840_write(port
, &stop_char
, 1);
1626 /* if we are implementing RTS/CTS, toggle that line */
1627 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1628 mos7840_port
->shadowMCR
&= ~MCR_RTS
;
1631 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1632 mos7840_port
->shadowMCR
);
1642 /*****************************************************************************
1643 * mos7840_unthrottle
1644 * this function is called by the tty driver when it wants to resume the data
1645 * being read from the port (called after SerialThrottle is called)
1646 *****************************************************************************/
1647 static void mos7840_unthrottle(struct usb_serial_port
*port
)
1649 struct tty_struct
*tty
;
1651 struct moschip_port
*mos7840_port
= mos7840_get_port_private(port
);
1653 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
1654 dbg("%s", "Invalid port \n");
1658 if (mos7840_port
== NULL
)
1661 if (!mos7840_port
->open
) {
1662 dbg("%s - port not opened", __FUNCTION__
);
1666 dbg("%s", "Entering .......... \n");
1670 dbg("%s - no tty available", __FUNCTION__
);
1674 /* if we are implementing XON/XOFF, send the start character */
1676 unsigned char start_char
= START_CHAR(tty
);
1677 status
= mos7840_write(port
, &start_char
, 1);
1683 /* if we are implementing RTS/CTS, toggle that line */
1684 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1685 mos7840_port
->shadowMCR
|= MCR_RTS
;
1688 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1689 mos7840_port
->shadowMCR
);
1698 static int mos7840_tiocmget(struct usb_serial_port
*port
, struct file
*file
)
1700 struct moschip_port
*mos7840_port
;
1701 unsigned int result
;
1705 mos7840_port
= mos7840_get_port_private(port
);
1707 dbg("%s - port %d", __FUNCTION__
, port
->number
);
1709 if (mos7840_port
== NULL
)
1712 status
= mos7840_get_uart_reg(port
, MODEM_STATUS_REGISTER
, &msr
);
1713 status
= mos7840_get_uart_reg(port
, MODEM_CONTROL_REGISTER
, &mcr
);
1714 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0)
1715 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0)
1716 | ((mcr
& MCR_LOOPBACK
) ? TIOCM_LOOP
: 0)
1717 | ((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0)
1718 | ((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0)
1719 | ((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0)
1720 | ((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0);
1722 dbg("%s - 0x%04X", __FUNCTION__
, result
);
1727 static int mos7840_tiocmset(struct usb_serial_port
*port
, struct file
*file
,
1728 unsigned int set
, unsigned int clear
)
1730 struct moschip_port
*mos7840_port
;
1732 unsigned int status
;
1734 dbg("%s - port %d", __FUNCTION__
, port
->number
);
1736 mos7840_port
= mos7840_get_port_private(port
);
1738 if (mos7840_port
== NULL
)
1741 mcr
= mos7840_port
->shadowMCR
;
1742 if (clear
& TIOCM_RTS
)
1744 if (clear
& TIOCM_DTR
)
1746 if (clear
& TIOCM_LOOP
)
1747 mcr
&= ~MCR_LOOPBACK
;
1749 if (set
& TIOCM_RTS
)
1751 if (set
& TIOCM_DTR
)
1753 if (set
& TIOCM_LOOP
)
1754 mcr
|= MCR_LOOPBACK
;
1756 mos7840_port
->shadowMCR
= mcr
;
1759 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, mcr
);
1761 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
1768 /*****************************************************************************
1769 * mos7840_calc_baud_rate_divisor
1770 * this function calculates the proper baud rate divisor for the specified
1772 *****************************************************************************/
1773 static int mos7840_calc_baud_rate_divisor(int baudRate
, int *divisor
,
1774 __u16
* clk_sel_val
)
1777 dbg("%s - %d", __FUNCTION__
, baudRate
);
1779 if (baudRate
<= 115200) {
1780 *divisor
= 115200 / baudRate
;
1783 if ((baudRate
> 115200) && (baudRate
<= 230400)) {
1784 *divisor
= 230400 / baudRate
;
1785 *clk_sel_val
= 0x10;
1786 } else if ((baudRate
> 230400) && (baudRate
<= 403200)) {
1787 *divisor
= 403200 / baudRate
;
1788 *clk_sel_val
= 0x20;
1789 } else if ((baudRate
> 403200) && (baudRate
<= 460800)) {
1790 *divisor
= 460800 / baudRate
;
1791 *clk_sel_val
= 0x30;
1792 } else if ((baudRate
> 460800) && (baudRate
<= 806400)) {
1793 *divisor
= 806400 / baudRate
;
1794 *clk_sel_val
= 0x40;
1795 } else if ((baudRate
> 806400) && (baudRate
<= 921600)) {
1796 *divisor
= 921600 / baudRate
;
1797 *clk_sel_val
= 0x50;
1798 } else if ((baudRate
> 921600) && (baudRate
<= 1572864)) {
1799 *divisor
= 1572864 / baudRate
;
1800 *clk_sel_val
= 0x60;
1801 } else if ((baudRate
> 1572864) && (baudRate
<= 3145728)) {
1802 *divisor
= 3145728 / baudRate
;
1803 *clk_sel_val
= 0x70;
1809 for (i
= 0; i
< ARRAY_SIZE(mos7840_divisor_table
); i
++) {
1810 if (mos7840_divisor_table
[i
].BaudRate
== baudrate
) {
1811 *divisor
= mos7840_divisor_table
[i
].Divisor
;
1816 /* After trying for all the standard baud rates *
1817 * Try calculating the divisor for this baud rate */
1819 if (baudrate
> 75 && baudrate
< 230400) {
1820 /* get the divisor */
1821 custom
= (__u16
) (230400L / baudrate
);
1823 /* Check for round off */
1824 round1
= (__u16
) (2304000L / baudrate
);
1825 round
= (__u16
) (round1
- (custom
* 10));
1831 dbg(" Baud %d = %d\n", baudrate
, custom
);
1835 dbg("%s\n", " Baud calculation Failed...");
1840 /*****************************************************************************
1841 * mos7840_send_cmd_write_baud_rate
1842 * this function sends the proper command to change the baud rate of the
1844 *****************************************************************************/
1846 static int mos7840_send_cmd_write_baud_rate(struct moschip_port
*mos7840_port
,
1852 unsigned char number
;
1854 struct usb_serial_port
*port
;
1856 if (mos7840_port
== NULL
)
1859 port
= (struct usb_serial_port
*)mos7840_port
->port
;
1860 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
1861 dbg("%s", "Invalid port \n");
1865 if (mos7840_serial_paranoia_check(port
->serial
, __FUNCTION__
)) {
1866 dbg("%s", "Invalid Serial \n");
1870 dbg("%s", "Entering .......... \n");
1872 number
= mos7840_port
->port
->number
- mos7840_port
->port
->serial
->minor
;
1874 dbg("%s - port = %d, baud = %d", __FUNCTION__
,
1875 mos7840_port
->port
->number
, baudRate
);
1876 //reset clk_uart_sel in spregOffset
1877 if (baudRate
> 115200) {
1878 #ifdef HW_flow_control
1879 //NOTE: need to see the pther register to modify
1880 //setting h/w flow control bit to 1;
1883 mos7840_port
->shadowMCR
= Data
;
1885 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1887 dbg("Writing spreg failed in set_serial_baud\n");
1893 #ifdef HW_flow_control
1894 //setting h/w flow control bit to 0;
1897 mos7840_port
->shadowMCR
= Data
;
1899 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1901 dbg("Writing spreg failed in set_serial_baud\n");
1908 if (1) //baudRate <= 115200)
1914 mos7840_calc_baud_rate_divisor(baudRate
, &divisor
,
1917 mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
,
1920 dbg("reading spreg failed in set_serial_baud\n");
1923 Data
= (Data
& 0x8f) | clk_sel_val
;
1926 mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1928 dbg("Writing spreg failed in set_serial_baud\n");
1931 /* Calculate the Divisor */
1934 err("%s - bad baud rate", __FUNCTION__
);
1935 dbg("%s\n", "bad baud rate");
1938 /* Enable access to divisor latch */
1939 Data
= mos7840_port
->shadowLCR
| SERIAL_LCR_DLAB
;
1940 mos7840_port
->shadowLCR
= Data
;
1941 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1943 /* Write the divisor */
1944 Data
= (unsigned char)(divisor
& 0xff);
1945 dbg("set_serial_baud Value to write DLL is %x\n", Data
);
1946 mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
1948 Data
= (unsigned char)((divisor
& 0xff00) >> 8);
1949 dbg("set_serial_baud Value to write DLM is %x\n", Data
);
1950 mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
1952 /* Disable access to divisor latch */
1953 Data
= mos7840_port
->shadowLCR
& ~SERIAL_LCR_DLAB
;
1954 mos7840_port
->shadowLCR
= Data
;
1955 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1962 /*****************************************************************************
1963 * mos7840_change_port_settings
1964 * This routine is called to set the UART on the device to match
1965 * the specified new settings.
1966 *****************************************************************************/
1968 static void mos7840_change_port_settings(struct moschip_port
*mos7840_port
,
1969 struct ktermios
*old_termios
)
1971 struct tty_struct
*tty
;
1980 struct usb_serial_port
*port
;
1981 struct usb_serial
*serial
;
1983 if (mos7840_port
== NULL
)
1986 port
= (struct usb_serial_port
*)mos7840_port
->port
;
1988 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
1989 dbg("%s", "Invalid port \n");
1993 if (mos7840_serial_paranoia_check(port
->serial
, __FUNCTION__
)) {
1994 dbg("%s", "Invalid Serial \n");
1998 serial
= port
->serial
;
2000 dbg("%s - port %d", __FUNCTION__
, mos7840_port
->port
->number
);
2002 if (!mos7840_port
->open
) {
2003 dbg("%s - port not opened", __FUNCTION__
);
2007 tty
= mos7840_port
->port
->tty
;
2009 dbg("%s", "Entering .......... \n");
2013 lParity
= LCR_PAR_NONE
;
2015 cflag
= tty
->termios
->c_cflag
;
2016 iflag
= tty
->termios
->c_iflag
;
2018 /* Change the number of bits */
2019 if (cflag
& CSIZE
) {
2020 switch (cflag
& CSIZE
) {
2038 /* Change the Parity bit */
2039 if (cflag
& PARENB
) {
2040 if (cflag
& PARODD
) {
2041 lParity
= LCR_PAR_ODD
;
2042 dbg("%s - parity = odd", __FUNCTION__
);
2044 lParity
= LCR_PAR_EVEN
;
2045 dbg("%s - parity = even", __FUNCTION__
);
2049 dbg("%s - parity = none", __FUNCTION__
);
2052 if (cflag
& CMSPAR
) {
2053 lParity
= lParity
| 0x20;
2056 /* Change the Stop bit */
2057 if (cflag
& CSTOPB
) {
2059 dbg("%s - stop bits = 2", __FUNCTION__
);
2062 dbg("%s - stop bits = 1", __FUNCTION__
);
2065 /* Update the LCR with the correct value */
2066 mos7840_port
->shadowLCR
&=
2067 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
2068 mos7840_port
->shadowLCR
|= (lData
| lParity
| lStop
);
2070 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2071 mos7840_port
->shadowLCR
);
2072 /* Disable Interrupts */
2074 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
2077 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
2080 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
2082 /* Send the updated LCR value to the mos7840 */
2083 Data
= mos7840_port
->shadowLCR
;
2085 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
2088 mos7840_port
->shadowMCR
= Data
;
2089 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2091 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2093 /* set up the MCR register and send it to the mos7840 */
2095 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
2096 if (cflag
& CBAUD
) {
2097 mos7840_port
->shadowMCR
|= (MCR_DTR
| MCR_RTS
);
2100 if (cflag
& CRTSCTS
) {
2101 mos7840_port
->shadowMCR
|= (MCR_XON_ANY
);
2104 mos7840_port
->shadowMCR
&= ~(MCR_XON_ANY
);
2107 Data
= mos7840_port
->shadowMCR
;
2108 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2110 /* Determine divisor based on baud rate */
2111 baud
= tty_get_baud_rate(tty
);
2114 /* pick a default, any default... */
2115 dbg("%s\n", "Picked default baud...");
2119 dbg("%s - baud rate = %d", __FUNCTION__
, baud
);
2120 status
= mos7840_send_cmd_write_baud_rate(mos7840_port
, baud
);
2122 /* Enable Interrupts */
2124 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
2126 if (mos7840_port
->read_urb
->status
!= -EINPROGRESS
) {
2127 mos7840_port
->read_urb
->dev
= serial
->dev
;
2129 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
2132 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2136 wake_up(&mos7840_port
->delta_msr_wait
);
2137 mos7840_port
->delta_msr_cond
= 1;
2138 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2139 mos7840_port
->shadowLCR
);
2144 /*****************************************************************************
2145 * mos7840_set_termios
2146 * this function is called by the tty driver when it wants to change
2147 * the termios structure
2148 *****************************************************************************/
2150 static void mos7840_set_termios(struct usb_serial_port
*port
,
2151 struct ktermios
*old_termios
)
2155 struct usb_serial
*serial
;
2156 struct moschip_port
*mos7840_port
;
2157 struct tty_struct
*tty
;
2158 dbg("mos7840_set_termios: START\n");
2159 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
2160 dbg("%s", "Invalid port \n");
2164 serial
= port
->serial
;
2166 if (mos7840_serial_paranoia_check(serial
, __FUNCTION__
)) {
2167 dbg("%s", "Invalid Serial \n");
2171 mos7840_port
= mos7840_get_port_private(port
);
2173 if (mos7840_port
== NULL
)
2178 if (!mos7840_port
->open
) {
2179 dbg("%s - port not opened", __FUNCTION__
);
2183 dbg("%s\n", "setting termios - ");
2185 cflag
= tty
->termios
->c_cflag
;
2187 dbg("%s - clfag %08x iflag %08x", __FUNCTION__
,
2188 tty
->termios
->c_cflag
, RELEVANT_IFLAG(tty
->termios
->c_iflag
));
2189 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__
,
2190 old_termios
->c_cflag
, RELEVANT_IFLAG(old_termios
->c_iflag
));
2191 dbg("%s - port %d", __FUNCTION__
, port
->number
);
2193 /* change the port settings to the new ones specified */
2195 mos7840_change_port_settings(mos7840_port
, old_termios
);
2197 if (!mos7840_port
->read_urb
) {
2198 dbg("%s", "URB KILLED !!!!!\n");
2202 if (mos7840_port
->read_urb
->status
!= -EINPROGRESS
) {
2203 mos7840_port
->read_urb
->dev
= serial
->dev
;
2204 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
2206 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2213 /*****************************************************************************
2214 * mos7840_get_lsr_info - get line status register info
2216 * Purpose: Let user call ioctl() to get info when the UART physically
2217 * is emptied. On bus types like RS485, the transmitter must
2218 * release the bus after transmitting. This must be done when
2219 * the transmit shift register is empty, not be done when the
2220 * transmit holding register is empty. This functionality
2221 * allows an RS485 driver to be written in user space.
2222 *****************************************************************************/
2224 static int mos7840_get_lsr_info(struct moschip_port
*mos7840_port
,
2225 unsigned int __user
*value
)
2228 unsigned int result
= 0;
2230 count
= mos7840_chars_in_buffer(mos7840_port
->port
);
2232 dbg("%s -- Empty", __FUNCTION__
);
2233 result
= TIOCSER_TEMT
;
2236 if (copy_to_user(value
, &result
, sizeof(int)))
2241 /*****************************************************************************
2242 * mos7840_set_modem_info
2243 * function to set modem info
2244 *****************************************************************************/
2246 static int mos7840_set_modem_info(struct moschip_port
*mos7840_port
,
2247 unsigned int cmd
, unsigned int __user
*value
)
2253 struct usb_serial_port
*port
;
2255 if (mos7840_port
== NULL
)
2258 port
= (struct usb_serial_port
*)mos7840_port
->port
;
2259 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
2260 dbg("%s", "Invalid port \n");
2264 mcr
= mos7840_port
->shadowMCR
;
2266 if (copy_from_user(&arg
, value
, sizeof(int)))
2271 if (arg
& TIOCM_RTS
)
2273 if (arg
& TIOCM_DTR
)
2275 if (arg
& TIOCM_LOOP
)
2276 mcr
|= MCR_LOOPBACK
;
2280 if (arg
& TIOCM_RTS
)
2282 if (arg
& TIOCM_DTR
)
2284 if (arg
& TIOCM_LOOP
)
2285 mcr
&= ~MCR_LOOPBACK
;
2289 /* turn off the RTS and DTR and LOOPBACK
2290 * and then only turn on what was asked to */
2291 mcr
&= ~(MCR_RTS
| MCR_DTR
| MCR_LOOPBACK
);
2292 mcr
|= ((arg
& TIOCM_RTS
) ? MCR_RTS
: 0);
2293 mcr
|= ((arg
& TIOCM_DTR
) ? MCR_DTR
: 0);
2294 mcr
|= ((arg
& TIOCM_LOOP
) ? MCR_LOOPBACK
: 0);
2298 mos7840_port
->shadowMCR
= mcr
;
2300 Data
= mos7840_port
->shadowMCR
;
2302 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2304 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2311 /*****************************************************************************
2312 * mos7840_get_modem_info
2313 * function to get modem info
2314 *****************************************************************************/
2316 static int mos7840_get_modem_info(struct moschip_port
*mos7840_port
,
2317 unsigned int __user
*value
)
2319 unsigned int result
= 0;
2321 unsigned int mcr
= mos7840_port
->shadowMCR
;
2324 mos7840_get_uart_reg(mos7840_port
->port
, MODEM_STATUS_REGISTER
,
2326 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0) /* 0x002 */
2327 |((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0) /* 0x004 */
2328 |((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0) /* 0x020 */
2329 |((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0) /* 0x040 */
2330 |((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0) /* 0x080 */
2331 |((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0); /* 0x100 */
2333 dbg("%s -- %x", __FUNCTION__
, result
);
2335 if (copy_to_user(value
, &result
, sizeof(int)))
2340 /*****************************************************************************
2341 * mos7840_get_serial_info
2342 * function to get information about serial port
2343 *****************************************************************************/
2345 static int mos7840_get_serial_info(struct moschip_port
*mos7840_port
,
2346 struct serial_struct __user
*retinfo
)
2348 struct serial_struct tmp
;
2350 if (mos7840_port
== NULL
)
2356 memset(&tmp
, 0, sizeof(tmp
));
2358 tmp
.type
= PORT_16550A
;
2359 tmp
.line
= mos7840_port
->port
->serial
->minor
;
2360 tmp
.port
= mos7840_port
->port
->number
;
2362 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
2363 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
2364 tmp
.baud_base
= 9600;
2365 tmp
.close_delay
= 5 * HZ
;
2366 tmp
.closing_wait
= 30 * HZ
;
2368 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2373 /*****************************************************************************
2375 * this function handles any ioctl calls to the driver
2376 *****************************************************************************/
2378 static int mos7840_ioctl(struct usb_serial_port
*port
, struct file
*file
,
2379 unsigned int cmd
, unsigned long arg
)
2381 void __user
*argp
= (void __user
*)arg
;
2382 struct moschip_port
*mos7840_port
;
2383 struct tty_struct
*tty
;
2385 struct async_icount cnow
;
2386 struct async_icount cprev
;
2387 struct serial_icounter_struct icount
;
2390 if (mos7840_port_paranoia_check(port
, __FUNCTION__
)) {
2391 dbg("%s", "Invalid port \n");
2395 mos7840_port
= mos7840_get_port_private(port
);
2397 if (mos7840_port
== NULL
)
2400 tty
= mos7840_port
->port
->tty
;
2402 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__
, port
->number
, cmd
);
2405 /* return number of bytes available */
2408 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__
, port
->number
);
2409 return mos7840_get_lsr_info(mos7840_port
, argp
);
2415 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__
,
2418 mos7840_set_modem_info(mos7840_port
, cmd
, argp
);
2422 dbg("%s (%d) TIOCMGET", __FUNCTION__
, port
->number
);
2423 return mos7840_get_modem_info(mos7840_port
, argp
);
2426 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__
, port
->number
);
2427 return mos7840_get_serial_info(mos7840_port
, argp
);
2430 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__
, port
->number
);
2434 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__
, port
->number
);
2435 cprev
= mos7840_port
->icount
;
2437 //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
2438 mos7840_port
->delta_msr_cond
= 0;
2439 wait_event_interruptible(mos7840_port
->delta_msr_wait
,
2441 delta_msr_cond
== 1));
2443 /* see if a signal did it */
2444 if (signal_pending(current
))
2445 return -ERESTARTSYS
;
2446 cnow
= mos7840_port
->icount
;
2448 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
2449 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
2450 return -EIO
; /* no change => error */
2451 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
2452 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
2453 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
2454 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
2463 cnow
= mos7840_port
->icount
;
2465 icount
.cts
= cnow
.cts
;
2466 icount
.dsr
= cnow
.dsr
;
2467 icount
.rng
= cnow
.rng
;
2468 icount
.dcd
= cnow
.dcd
;
2469 icount
.rx
= cnow
.rx
;
2470 icount
.tx
= cnow
.tx
;
2471 icount
.frame
= cnow
.frame
;
2472 icount
.overrun
= cnow
.overrun
;
2473 icount
.parity
= cnow
.parity
;
2474 icount
.brk
= cnow
.brk
;
2475 icount
.buf_overrun
= cnow
.buf_overrun
;
2477 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__
,
2478 port
->number
, icount
.rx
, icount
.tx
);
2479 if (copy_to_user(argp
, &icount
, sizeof(icount
)))
2489 return -ENOIOCTLCMD
;
2492 static int mos7840_calc_num_ports(struct usb_serial
*serial
)
2494 int mos7840_num_ports
= 0;
2496 dbg("numberofendpoints: %d \n",
2497 (int)serial
->interface
->cur_altsetting
->desc
.bNumEndpoints
);
2498 dbg("numberofendpoints: %d \n",
2499 (int)serial
->interface
->altsetting
->desc
.bNumEndpoints
);
2500 if (serial
->interface
->cur_altsetting
->desc
.bNumEndpoints
== 5) {
2501 mos7840_num_ports
= serial
->num_ports
= 2;
2502 } else if (serial
->interface
->cur_altsetting
->desc
.bNumEndpoints
== 9) {
2503 serial
->num_bulk_in
= 4;
2504 serial
->num_bulk_out
= 4;
2505 mos7840_num_ports
= serial
->num_ports
= 4;
2508 return mos7840_num_ports
;
2511 /****************************************************************************
2513 ****************************************************************************/
2515 static int mos7840_startup(struct usb_serial
*serial
)
2517 struct moschip_port
*mos7840_port
;
2518 struct usb_device
*dev
;
2522 dbg("%s \n", " mos7840_startup :entering..........");
2525 dbg("%s\n", "Invalid Handler");
2531 dbg("%s\n", "Entering...");
2533 /* we set up the pointers to the endpoints in the mos7840_open *
2534 * function, as the structures aren't created yet. */
2536 /* set up port private structures */
2537 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2538 mos7840_port
= kzalloc(sizeof(struct moschip_port
), GFP_KERNEL
);
2539 if (mos7840_port
== NULL
) {
2540 err("%s - Out of memory", __FUNCTION__
);
2542 i
--; /* don't follow NULL pointer cleaning up */
2546 /* Initialize all port interrupt end point to port 0 int endpoint *
2547 * Our device has only one interrupt end point comman to all port */
2549 mos7840_port
->port
= serial
->port
[i
];
2550 mos7840_set_port_private(serial
->port
[i
], mos7840_port
);
2551 spin_lock_init(&mos7840_port
->pool_lock
);
2553 mos7840_port
->port_num
= ((serial
->port
[i
]->number
-
2554 (serial
->port
[i
]->serial
->minor
)) +
2557 if (mos7840_port
->port_num
== 1) {
2558 mos7840_port
->SpRegOffset
= 0x0;
2559 mos7840_port
->ControlRegOffset
= 0x1;
2560 mos7840_port
->DcrRegOffset
= 0x4;
2561 } else if ((mos7840_port
->port_num
== 2)
2562 && (serial
->num_ports
== 4)) {
2563 mos7840_port
->SpRegOffset
= 0x8;
2564 mos7840_port
->ControlRegOffset
= 0x9;
2565 mos7840_port
->DcrRegOffset
= 0x16;
2566 } else if ((mos7840_port
->port_num
== 2)
2567 && (serial
->num_ports
== 2)) {
2568 mos7840_port
->SpRegOffset
= 0xa;
2569 mos7840_port
->ControlRegOffset
= 0xb;
2570 mos7840_port
->DcrRegOffset
= 0x19;
2571 } else if ((mos7840_port
->port_num
== 3)
2572 && (serial
->num_ports
== 4)) {
2573 mos7840_port
->SpRegOffset
= 0xa;
2574 mos7840_port
->ControlRegOffset
= 0xb;
2575 mos7840_port
->DcrRegOffset
= 0x19;
2576 } else if ((mos7840_port
->port_num
== 4)
2577 && (serial
->num_ports
== 4)) {
2578 mos7840_port
->SpRegOffset
= 0xc;
2579 mos7840_port
->ControlRegOffset
= 0xd;
2580 mos7840_port
->DcrRegOffset
= 0x1c;
2582 mos7840_dump_serial_port(mos7840_port
);
2584 mos7840_set_port_private(serial
->port
[i
], mos7840_port
);
2586 //enable rx_disable bit in control register
2589 mos7840_get_reg_sync(serial
->port
[i
],
2590 mos7840_port
->ControlRegOffset
, &Data
);
2592 dbg("Reading ControlReg failed status-0x%x\n", status
);
2595 dbg("ControlReg Reading success val is %x, status%d\n",
2597 Data
|= 0x08; //setting driver done bit
2598 Data
|= 0x04; //sp1_bit to have cts change reflect in modem status reg
2600 //Data |= 0x20; //rx_disable bit
2603 mos7840_set_reg_sync(serial
->port
[i
],
2604 mos7840_port
->ControlRegOffset
, Data
);
2606 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status
);
2609 dbg("ControlReg Writing success(rx_disable) status%d\n",
2612 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2616 mos7840_set_reg_sync(serial
->port
[i
],
2617 (__u16
) (mos7840_port
->DcrRegOffset
+
2620 dbg("Writing DCR0 failed status-0x%x\n", status
);
2623 dbg("DCR0 Writing success status%d\n", status
);
2628 mos7840_set_reg_sync(serial
->port
[i
],
2629 (__u16
) (mos7840_port
->DcrRegOffset
+
2632 dbg("Writing DCR1 failed status-0x%x\n", status
);
2635 dbg("DCR1 Writing success status%d\n", status
);
2640 mos7840_set_reg_sync(serial
->port
[i
],
2641 (__u16
) (mos7840_port
->DcrRegOffset
+
2644 dbg("Writing DCR2 failed status-0x%x\n", status
);
2647 dbg("DCR2 Writing success status%d\n", status
);
2649 // write values in clkstart0x0 and clkmulti 0x20
2653 mos7840_set_reg_sync(serial
->port
[i
],
2654 CLK_START_VALUE_REGISTER
, Data
);
2656 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status
);
2659 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status
);
2663 mos7840_set_reg_sync(serial
->port
[i
], CLK_MULTI_REGISTER
,
2666 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2670 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2673 //write value 0x0 to scratchpad register
2676 mos7840_set_uart_reg(serial
->port
[i
], SCRATCH_PAD_REGISTER
,
2679 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2683 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2686 //Zero Length flag register
2687 if ((mos7840_port
->port_num
!= 1)
2688 && (serial
->num_ports
== 2)) {
2692 status
= mos7840_set_reg_sync(serial
->port
[i
],
2698 dbg("ZLIP offset%x\n",
2700 ((__u16
) mos7840_port
->port_num
)));
2702 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2706 dbg("ZLP_REG%d Writing success status%d\n",
2711 status
= mos7840_set_reg_sync(serial
->port
[i
],
2717 dbg("ZLIP offset%x\n",
2719 ((__u16
) mos7840_port
->port_num
) - 0x1));
2721 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2725 dbg("ZLP_REG%d Writing success status%d\n",
2729 mos7840_port
->control_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2730 mos7840_port
->ctrl_buf
= kmalloc(16, GFP_KERNEL
);
2731 mos7840_port
->dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
2732 if (!mos7840_port
->control_urb
|| !mos7840_port
->ctrl_buf
|| !mos7840_port
->dr
) {
2738 //Zero Length flag enable
2740 status
= mos7840_set_reg_sync(serial
->port
[0], ZLP_REG5
, Data
);
2742 dbg("Writing ZLP_REG5 failed status-0x%x\n", status
);
2745 dbg("ZLP_REG5 Writing success status%d\n", status
);
2747 /* setting configuration feature to one */
2748 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2749 (__u8
) 0x03, 0x00, 0x01, 0x00, NULL
, 0x00, 5 * HZ
);
2752 for (/* nothing */; i
>= 0; i
--) {
2753 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
2755 kfree(mos7840_port
->dr
);
2756 kfree(mos7840_port
->ctrl_buf
);
2757 usb_free_urb(mos7840_port
->control_urb
);
2758 kfree(mos7840_port
);
2759 serial
->port
[i
] = NULL
;
2764 /****************************************************************************
2766 * This function is called whenever the device is removed from the usb bus.
2767 ****************************************************************************/
2769 static void mos7840_shutdown(struct usb_serial
*serial
)
2772 unsigned long flags
;
2773 struct moschip_port
*mos7840_port
;
2774 dbg("%s \n", " shutdown :entering..........");
2777 dbg("%s", "Invalid Handler \n");
2781 /* check for the ports to be closed,close the ports and disconnect */
2783 /* free private structure allocated for serial port *
2784 * stop reads and writes on all ports */
2786 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2787 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
2788 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
2789 mos7840_port
->zombie
= 1;
2790 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
2791 usb_kill_urb(mos7840_port
->control_urb
);
2792 kfree(mos7840_port
->ctrl_buf
);
2793 kfree(mos7840_port
->dr
);
2794 kfree(mos7840_port
);
2795 mos7840_set_port_private(serial
->port
[i
], NULL
);
2798 dbg("%s\n", "Thank u :: ");
2802 static struct usb_driver io_driver
= {
2804 .probe
= usb_serial_probe
,
2805 .disconnect
= usb_serial_disconnect
,
2806 .id_table
= moschip_id_table_combined
,
2810 static struct usb_serial_driver moschip7840_4port_device
= {
2812 .owner
= THIS_MODULE
,
2815 .description
= DRIVER_DESC
,
2816 .usb_driver
= &io_driver
,
2817 .id_table
= moschip_port_id_table
,
2818 .num_interrupt_in
= 1, //NUM_DONT_CARE,//1,
2824 .open
= mos7840_open
,
2825 .close
= mos7840_close
,
2826 .write
= mos7840_write
,
2827 .write_room
= mos7840_write_room
,
2828 .chars_in_buffer
= mos7840_chars_in_buffer
,
2829 .throttle
= mos7840_throttle
,
2830 .unthrottle
= mos7840_unthrottle
,
2831 .calc_num_ports
= mos7840_calc_num_ports
,
2832 #ifdef MCSSerialProbe
2833 .probe
= mos7840_serial_probe
,
2835 .ioctl
= mos7840_ioctl
,
2836 .set_termios
= mos7840_set_termios
,
2837 .break_ctl
= mos7840_break
,
2838 .tiocmget
= mos7840_tiocmget
,
2839 .tiocmset
= mos7840_tiocmset
,
2840 .attach
= mos7840_startup
,
2841 .shutdown
= mos7840_shutdown
,
2842 .read_bulk_callback
= mos7840_bulk_in_callback
,
2843 .read_int_callback
= mos7840_interrupt_callback
,
2846 /****************************************************************************
2848 * This is called by the module subsystem, or on startup to initialize us
2849 ****************************************************************************/
2850 static int __init
moschip7840_init(void)
2854 dbg("%s \n", " mos7840_init :entering..........");
2856 /* Register with the usb serial */
2857 retval
= usb_serial_register(&moschip7840_4port_device
);
2860 goto failed_port_device_register
;
2862 dbg("%s\n", "Entring...");
2863 info(DRIVER_DESC
" " DRIVER_VERSION
);
2865 /* Register with the usb */
2866 retval
= usb_register(&io_driver
);
2869 goto failed_usb_register
;
2872 dbg("%s\n", "Leaving...");
2876 failed_usb_register
:
2877 usb_serial_deregister(&moschip7840_4port_device
);
2879 failed_port_device_register
:
2884 /****************************************************************************
2886 * Called when the driver is about to be unloaded.
2887 ****************************************************************************/
2888 static void __exit
moschip7840_exit(void)
2891 dbg("%s \n", " mos7840_exit :entering..........");
2893 usb_deregister(&io_driver
);
2895 usb_serial_deregister(&moschip7840_4port_device
);
2897 dbg("%s\n", "Entring...");
2900 module_init(moschip7840_init
);
2901 module_exit(moschip7840_exit
);
2903 /* Module information */
2904 MODULE_DESCRIPTION(DRIVER_DESC
);
2905 MODULE_LICENSE("GPL");
2907 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
2908 MODULE_PARM_DESC(debug
, "Debug enabled or not");