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 /* The native mos7840/7820 component */
114 #define USB_VENDOR_ID_MOSCHIP 0x9710
115 #define MOSCHIP_DEVICE_ID_7840 0x7840
116 #define MOSCHIP_DEVICE_ID_7820 0x7820
117 /* The native component can have its vendor/device id's overridden
118 * in vendor-specific implementations. Such devices can be handled
119 * by making a change here, in moschip_port_id_table, and in
120 * moschip_id_table_combined
122 #define USB_VENDOR_ID_BANDB 0x0856
123 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
124 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
126 /* Interrupt Routine Defines */
128 #define SERIAL_IIR_RLS 0x06
129 #define SERIAL_IIR_MS 0x00
132 * Emulation of the bit mask on the LINE STATUS REGISTER.
134 #define SERIAL_LSR_DR 0x0001
135 #define SERIAL_LSR_OE 0x0002
136 #define SERIAL_LSR_PE 0x0004
137 #define SERIAL_LSR_FE 0x0008
138 #define SERIAL_LSR_BI 0x0010
140 #define MOS_MSR_DELTA_CTS 0x10
141 #define MOS_MSR_DELTA_DSR 0x20
142 #define MOS_MSR_DELTA_RI 0x40
143 #define MOS_MSR_DELTA_CD 0x80
145 // Serial Port register Address
146 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
147 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
148 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
149 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
150 #define LINE_STATUS_REGISTER ((__u16)(0x05))
151 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
152 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
153 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
154 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
156 #define CLK_MULTI_REGISTER ((__u16)(0x02))
157 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
159 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
162 * URB POOL related defines
164 #define NUM_URBS 16 /* URB Count */
165 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
168 static struct usb_device_id moschip_port_id_table
[] = {
169 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
170 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
171 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
172 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
173 {} /* terminating entry */
176 static __devinitdata
struct usb_device_id moschip_id_table_combined
[] = {
177 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
179 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
180 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
181 {} /* terminating entry */
184 MODULE_DEVICE_TABLE(usb
, moschip_id_table_combined
);
186 /* This structure holds all of the local port information */
188 struct moschip_port
{
189 int port_num
; /*Actual port number in the device(1,2,etc) */
190 struct urb
*write_urb
; /* write URB for this port */
191 struct urb
*read_urb
; /* read URB for this port */
193 __u8 shadowLCR
; /* last LCR value received */
194 __u8 shadowMCR
; /* last MCR value received */
198 wait_queue_head_t wait_chase
; /* for handling sleeping while waiting for chase to finish */
199 wait_queue_head_t delta_msr_wait
; /* for handling sleeping while waiting for msr change to happen */
201 struct async_icount icount
;
202 struct usb_serial_port
*port
; /* loop back to the owner of this object */
206 __u8 ControlRegOffset
;
208 //for processing control URBS in interrupt context
209 struct urb
*control_urb
;
210 struct usb_ctrlrequest
*dr
;
214 spinlock_t pool_lock
;
215 struct urb
*write_urb_pool
[NUM_URBS
];
223 * mos7840_set_reg_sync
224 * To set the Control register by calling usb_fill_control_urb function
225 * by passing usb_sndctrlpipe function as parameter.
228 static int mos7840_set_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
231 struct usb_device
*dev
= port
->serial
->dev
;
233 dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg
, val
);
235 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
236 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
241 * mos7840_get_reg_sync
242 * To set the Uart register by calling usb_fill_control_urb function by
243 * passing usb_rcvctrlpipe function as parameter.
246 static int mos7840_get_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
249 struct usb_device
*dev
= port
->serial
->dev
;
252 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
253 MCS_RD_RTYPE
, 0, reg
, val
, VENDOR_READ_LENGTH
,
255 dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg
, *val
);
256 *val
= (*val
) & 0x00ff;
261 * mos7840_set_uart_reg
262 * To set the Uart register by calling usb_fill_control_urb function by
263 * passing usb_sndctrlpipe function as parameter.
266 static int mos7840_set_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
270 struct usb_device
*dev
= port
->serial
->dev
;
272 // For the UART control registers, the application number need to be Or'ed
273 if (port
->serial
->num_ports
== 4) {
275 (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) +
277 dbg("mos7840_set_uart_reg application number is %x\n", val
);
279 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
281 (((__u16
) port
->number
-
282 (__u16
) (port
->serial
->minor
)) + 1) << 8;
283 dbg("mos7840_set_uart_reg application number is %x\n",
287 (((__u16
) port
->number
-
288 (__u16
) (port
->serial
->minor
)) + 2) << 8;
289 dbg("mos7840_set_uart_reg application number is %x\n",
293 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
294 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
300 * mos7840_get_uart_reg
301 * To set the Control register by calling usb_fill_control_urb function
302 * by passing usb_rcvctrlpipe function as parameter.
304 static int mos7840_get_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
307 struct usb_device
*dev
= port
->serial
->dev
;
311 //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
312 /*Wval is same as application number */
313 if (port
->serial
->num_ports
== 4) {
315 (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) +
317 dbg("mos7840_get_uart_reg application number is %x\n", Wval
);
319 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
321 (((__u16
) port
->number
-
322 (__u16
) (port
->serial
->minor
)) + 1) << 8;
323 dbg("mos7840_get_uart_reg application number is %x\n",
327 (((__u16
) port
->number
-
328 (__u16
) (port
->serial
->minor
)) + 2) << 8;
329 dbg("mos7840_get_uart_reg application number is %x\n",
333 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
334 MCS_RD_RTYPE
, Wval
, reg
, val
, VENDOR_READ_LENGTH
,
336 *val
= (*val
) & 0x00ff;
340 static void mos7840_dump_serial_port(struct moschip_port
*mos7840_port
)
343 dbg("***************************************\n");
344 dbg("SpRegOffset is %2x\n", mos7840_port
->SpRegOffset
);
345 dbg("ControlRegOffset is %2x \n", mos7840_port
->ControlRegOffset
);
346 dbg("DCRRegOffset is %2x \n", mos7840_port
->DcrRegOffset
);
347 dbg("***************************************\n");
351 /************************************************************************/
352 /************************************************************************/
353 /* I N T E R F A C E F U N C T I O N S */
354 /* I N T E R F A C E F U N C T I O N S */
355 /************************************************************************/
356 /************************************************************************/
358 static inline void mos7840_set_port_private(struct usb_serial_port
*port
,
359 struct moschip_port
*data
)
361 usb_set_serial_port_data(port
, (void *)data
);
364 static inline struct moschip_port
*mos7840_get_port_private(struct
368 return (struct moschip_port
*)usb_get_serial_port_data(port
);
371 static void mos7840_handle_new_msr(struct moschip_port
*port
, __u8 new_msr
)
373 struct moschip_port
*mos7840_port
;
374 struct async_icount
*icount
;
376 icount
= &mos7840_port
->icount
;
378 (MOS_MSR_DELTA_CTS
| MOS_MSR_DELTA_DSR
| MOS_MSR_DELTA_RI
|
380 icount
= &mos7840_port
->icount
;
382 /* update input line counters */
383 if (new_msr
& MOS_MSR_DELTA_CTS
) {
387 if (new_msr
& MOS_MSR_DELTA_DSR
) {
391 if (new_msr
& MOS_MSR_DELTA_CD
) {
395 if (new_msr
& MOS_MSR_DELTA_RI
) {
402 static void mos7840_handle_new_lsr(struct moschip_port
*port
, __u8 new_lsr
)
404 struct async_icount
*icount
;
406 dbg("%s - %02x", __func__
, new_lsr
);
408 if (new_lsr
& SERIAL_LSR_BI
) {
410 // Parity and Framing errors only count if they
411 // occur exclusive of a break being
414 new_lsr
&= (__u8
) (SERIAL_LSR_OE
| SERIAL_LSR_BI
);
417 /* update input line counters */
418 icount
= &port
->icount
;
419 if (new_lsr
& SERIAL_LSR_BI
) {
423 if (new_lsr
& SERIAL_LSR_OE
) {
427 if (new_lsr
& SERIAL_LSR_PE
) {
431 if (new_lsr
& SERIAL_LSR_FE
) {
437 /************************************************************************/
438 /************************************************************************/
439 /* U S B C A L L B A C K F U N C T I O N S */
440 /* U S B C A L L B A C K F U N C T I O N S */
441 /************************************************************************/
442 /************************************************************************/
444 static void mos7840_control_callback(struct urb
*urb
)
447 struct moschip_port
*mos7840_port
;
450 int status
= urb
->status
;
452 mos7840_port
= urb
->context
;
461 /* this urb is terminated, clean up */
462 dbg("%s - urb shutting down with status: %d", __func__
,
466 dbg("%s - nonzero urb status received: %d", __func__
,
471 dbg("%s urb buffer size is %d\n", __func__
, urb
->actual_length
);
472 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __func__
,
473 mos7840_port
->MsrLsr
, mos7840_port
->port_num
);
474 data
= urb
->transfer_buffer
;
475 regval
= (__u8
) data
[0];
476 dbg("%s data is %x\n", __func__
, regval
);
477 if (mos7840_port
->MsrLsr
== 0)
478 mos7840_handle_new_msr(mos7840_port
, regval
);
479 else if (mos7840_port
->MsrLsr
== 1)
480 mos7840_handle_new_lsr(mos7840_port
, regval
);
483 spin_lock(&mos7840_port
->pool_lock
);
484 if (!mos7840_port
->zombie
)
485 result
= usb_submit_urb(mos7840_port
->int_urb
, GFP_ATOMIC
);
486 spin_unlock(&mos7840_port
->pool_lock
);
488 dev_err(&urb
->dev
->dev
,
489 "%s - Error %d submitting interrupt urb\n",
494 static int mos7840_get_reg(struct moschip_port
*mcs
, __u16 Wval
, __u16 reg
,
497 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
498 struct usb_ctrlrequest
*dr
= mcs
->dr
;
499 unsigned char *buffer
= mcs
->ctrl_buf
;
502 dr
->bRequestType
= MCS_RD_RTYPE
;
503 dr
->bRequest
= MCS_RDREQ
;
504 dr
->wValue
= cpu_to_le16(Wval
); //0;
505 dr
->wIndex
= cpu_to_le16(reg
);
506 dr
->wLength
= cpu_to_le16(2);
508 usb_fill_control_urb(mcs
->control_urb
, dev
, usb_rcvctrlpipe(dev
, 0),
509 (unsigned char *)dr
, buffer
, 2,
510 mos7840_control_callback
, mcs
);
511 mcs
->control_urb
->transfer_buffer_length
= 2;
512 ret
= usb_submit_urb(mcs
->control_urb
, GFP_ATOMIC
);
516 /*****************************************************************************
517 * mos7840_interrupt_callback
518 * this is the callback function for when we have received data on the
519 * interrupt endpoint.
520 *****************************************************************************/
522 static void mos7840_interrupt_callback(struct urb
*urb
)
526 struct moschip_port
*mos7840_port
;
527 struct usb_serial
*serial
;
532 __u16 wval
, wreg
= 0;
533 int status
= urb
->status
;
535 dbg("%s", " : Entering\n");
544 /* this urb is terminated, clean up */
545 dbg("%s - urb shutting down with status: %d", __func__
,
549 dbg("%s - nonzero urb status received: %d", __func__
,
554 length
= urb
->actual_length
;
555 data
= urb
->transfer_buffer
;
557 serial
= urb
->context
;
559 /* Moschip get 5 bytes
560 * Byte 1 IIR Port 1 (port.number is 0)
561 * Byte 2 IIR Port 2 (port.number is 1)
562 * Byte 3 IIR Port 3 (port.number is 2)
563 * Byte 4 IIR Port 4 (port.number is 3)
564 * Byte 5 FIFO status for both */
566 if (length
&& length
> 5) {
567 dbg("%s \n", "Wrong data !!!");
571 sp
[0] = (__u8
) data
[0];
572 sp
[1] = (__u8
) data
[1];
573 sp
[2] = (__u8
) data
[2];
574 sp
[3] = (__u8
) data
[3];
577 for (i
= 0; i
< serial
->num_ports
; i
++) {
578 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
580 (((__u16
) serial
->port
[i
]->number
-
581 (__u16
) (serial
->minor
)) + 1) << 8;
582 if (mos7840_port
->open
) {
584 dbg("SP%d No Interrupt !!!\n", i
);
586 switch (sp
[i
] & 0x0f) {
588 dbg("Serial Port %d: Receiver status error or ", i
);
589 dbg("address bit detected in 9-bit mode\n");
590 mos7840_port
->MsrLsr
= 1;
591 wreg
= LINE_STATUS_REGISTER
;
594 dbg("Serial Port %d: Modem status change\n", i
);
595 mos7840_port
->MsrLsr
= 0;
596 wreg
= MODEM_STATUS_REGISTER
;
599 spin_lock(&mos7840_port
->pool_lock
);
600 if (!mos7840_port
->zombie
) {
601 rv
= mos7840_get_reg(mos7840_port
, wval
, wreg
, &Data
);
603 spin_unlock(&mos7840_port
->pool_lock
);
606 spin_unlock(&mos7840_port
->pool_lock
);
610 if (!(rv
< 0)) /* the completion handler for the control urb will resubmit */
613 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
615 dev_err(&urb
->dev
->dev
,
616 "%s - Error %d submitting interrupt urb\n",
621 static int mos7840_port_paranoia_check(struct usb_serial_port
*port
,
622 const char *function
)
625 dbg("%s - port == NULL", function
);
629 dbg("%s - port->serial == NULL", function
);
636 /* Inline functions to check the sanity of a pointer that is passed to us */
637 static int mos7840_serial_paranoia_check(struct usb_serial
*serial
,
638 const char *function
)
641 dbg("%s - serial == NULL", function
);
645 dbg("%s - serial->type == NULL!", function
);
652 static struct usb_serial
*mos7840_get_usb_serial(struct usb_serial_port
*port
,
653 const char *function
)
655 /* if no port was specified, or it fails a paranoia check */
657 mos7840_port_paranoia_check(port
, function
) ||
658 mos7840_serial_paranoia_check(port
->serial
, function
)) {
659 /* then say that we don't have a valid usb_serial thing, which will
660 * end up genrating -ENODEV return values */
667 /*****************************************************************************
668 * mos7840_bulk_in_callback
669 * this is the callback function for when we have received data on the
671 *****************************************************************************/
673 static void mos7840_bulk_in_callback(struct urb
*urb
)
677 struct usb_serial
*serial
;
678 struct usb_serial_port
*port
;
679 struct moschip_port
*mos7840_port
;
680 struct tty_struct
*tty
;
681 int status
= urb
->status
;
684 dbg("nonzero read bulk status received: %d", status
);
688 mos7840_port
= urb
->context
;
690 dbg("%s", "NULL mos7840_port pointer \n");
694 port
= (struct usb_serial_port
*)mos7840_port
->port
;
695 if (mos7840_port_paranoia_check(port
, __func__
)) {
696 dbg("%s", "Port Paranoia failed \n");
700 serial
= mos7840_get_usb_serial(port
, __func__
);
702 dbg("%s\n", "Bad serial pointer ");
706 dbg("%s\n", "Entering... \n");
708 data
= urb
->transfer_buffer
;
710 dbg("%s", "Entering ........... \n");
712 if (urb
->actual_length
) {
713 tty
= mos7840_port
->port
->tty
;
715 tty_buffer_request_room(tty
, urb
->actual_length
);
716 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
718 tty_flip_buffer_push(tty
);
720 mos7840_port
->icount
.rx
+= urb
->actual_length
;
722 dbg("mos7840_port->icount.rx is %d:\n",
723 mos7840_port
->icount
.rx
);
726 if (!mos7840_port
->read_urb
) {
727 dbg("%s", "URB KILLED !!!\n");
732 mos7840_port
->read_urb
->dev
= serial
->dev
;
734 retval
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
737 dbg(" usb_submit_urb(read bulk) failed, retval = %d",
742 /*****************************************************************************
743 * mos7840_bulk_out_data_callback
744 * this is the callback function for when we have finished sending serial data
745 * on the bulk out endpoint.
746 *****************************************************************************/
748 static void mos7840_bulk_out_data_callback(struct urb
*urb
)
750 struct moschip_port
*mos7840_port
;
751 struct tty_struct
*tty
;
752 int status
= urb
->status
;
755 mos7840_port
= urb
->context
;
756 spin_lock(&mos7840_port
->pool_lock
);
757 for (i
= 0; i
< NUM_URBS
; i
++) {
758 if (urb
== mos7840_port
->write_urb_pool
[i
]) {
759 mos7840_port
->busy
[i
] = 0;
763 spin_unlock(&mos7840_port
->pool_lock
);
766 dbg("nonzero write bulk status received:%d\n", status
);
770 if (mos7840_port_paranoia_check(mos7840_port
->port
, __func__
)) {
771 dbg("%s", "Port Paranoia failed \n");
775 dbg("%s \n", "Entering .........");
777 tty
= mos7840_port
->port
->tty
;
779 if (tty
&& mos7840_port
->open
)
784 /************************************************************************/
785 /* 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 */
786 /************************************************************************/
787 #ifdef MCSSerialProbe
788 static int mos7840_serial_probe(struct usb_serial
*serial
,
789 const struct usb_device_id
*id
)
792 /*need to implement the mode_reg reading and updating\
793 structures usb_serial_ device_type\
794 (i.e num_ports, num_bulkin,bulkout etc) */
795 /* Also we can update the changes attach */
800 /*****************************************************************************
802 * this function is called by the tty driver when a port is opened
803 * If successful, we return 0
804 * Otherwise we return a negative error number.
805 *****************************************************************************/
807 static int mos7840_open(struct usb_serial_port
*port
, struct file
*filp
)
811 struct usb_serial
*serial
;
815 struct moschip_port
*mos7840_port
;
816 struct moschip_port
*port0
;
818 if (mos7840_port_paranoia_check(port
, __func__
)) {
819 dbg("%s", "Port Paranoia failed \n");
823 serial
= port
->serial
;
825 if (mos7840_serial_paranoia_check(serial
, __func__
)) {
826 dbg("%s", "Serial Paranoia failed \n");
830 mos7840_port
= mos7840_get_port_private(port
);
831 port0
= mos7840_get_port_private(serial
->port
[0]);
833 if (mos7840_port
== NULL
|| port0
== NULL
)
836 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
837 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
840 /* Initialising the write urb pool */
841 for (j
= 0; j
< NUM_URBS
; ++j
) {
842 urb
= usb_alloc_urb(0, GFP_KERNEL
);
843 mos7840_port
->write_urb_pool
[j
] = urb
;
846 err("No more urbs???");
850 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
, GFP_KERNEL
);
851 if (!urb
->transfer_buffer
) {
853 mos7840_port
->write_urb_pool
[j
] = NULL
;
854 err("%s-out of memory for urb buffers.", __func__
);
859 /*****************************************************************************
860 * Initialize MCS7840 -- Write Init values to corresponding Registers
868 * 0x08 : SP1/2 Control Reg
869 *****************************************************************************/
871 //NEED to check the following Block
875 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
877 dbg("Reading Spreg failed\n");
881 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
883 dbg("writing Spreg failed\n");
888 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
890 dbg("writing Spreg failed\n");
893 //End of block to be checked
898 mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
, &Data
);
900 dbg("Reading Controlreg failed\n");
903 Data
|= 0x08; //Driver done bit
904 Data
|= 0x20; //rx_disable
905 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
, Data
);
907 dbg("writing Controlreg failed\n");
910 //do register settings here
911 // Set all regs to the device default values.
912 ////////////////////////////////////
913 // First Disable all interrupts.
914 ////////////////////////////////////
917 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
919 dbg("disableing interrupts failed\n");
922 // Set FIFO_CONTROL_REGISTER to the default value
924 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
926 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
931 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
933 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
938 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
939 mos7840_port
->shadowLCR
= Data
;
942 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
943 mos7840_port
->shadowMCR
= Data
;
946 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
947 mos7840_port
->shadowLCR
= Data
;
949 Data
|= SERIAL_LCR_DLAB
; //data latch enable in LCR 0x80
950 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
954 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
958 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
962 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
964 Data
= Data
& ~SERIAL_LCR_DLAB
;
966 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
967 mos7840_port
->shadowLCR
= Data
;
969 //clearing Bulkin and Bulkout Fifo
972 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
976 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
980 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
981 //Finally enable all interrupts
985 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
987 //clearing rx_disable
991 mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
, &Data
);
995 mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
, Data
);
1001 mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
, &Data
);
1005 mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
, Data
);
1007 /* force low_latency on so that our tty_push actually forces *
1008 * the data through,otherwise it is scheduled, and with *
1009 * high data rates (like with OHCI) data can get lost. */
1012 port
->tty
->low_latency
= 1;
1013 /* Check to see if we've set up our endpoint info yet *
1014 * (can't set it up in mos7840_startup as the structures *
1015 * were not set up at that time.) */
1016 if (port0
->open_ports
== 1) {
1017 if (serial
->port
[0]->interrupt_in_buffer
== NULL
) {
1019 /* set up interrupt urb */
1021 usb_fill_int_urb(serial
->port
[0]->interrupt_in_urb
,
1023 usb_rcvintpipe(serial
->dev
,
1025 interrupt_in_endpointAddress
),
1026 serial
->port
[0]->interrupt_in_buffer
,
1027 serial
->port
[0]->interrupt_in_urb
->
1028 transfer_buffer_length
,
1029 mos7840_interrupt_callback
,
1031 serial
->port
[0]->interrupt_in_urb
->
1034 /* start interrupt read for mos7840 *
1035 * will continue as long as mos7840 is connected */
1038 usb_submit_urb(serial
->port
[0]->interrupt_in_urb
,
1041 err("%s - Error %d submitting interrupt urb",
1042 __func__
, response
);
1049 /* see if we've set up our endpoint info yet *
1050 * (can't set it up in mos7840_startup as the *
1051 * structures were not set up at that time.) */
1053 dbg("port number is %d \n", port
->number
);
1054 dbg("serial number is %d \n", port
->serial
->minor
);
1055 dbg("Bulkin endpoint is %d \n", port
->bulk_in_endpointAddress
);
1056 dbg("BulkOut endpoint is %d \n", port
->bulk_out_endpointAddress
);
1057 dbg("Interrupt endpoint is %d \n", port
->interrupt_in_endpointAddress
);
1058 dbg("port's number in the device is %d\n", mos7840_port
->port_num
);
1059 mos7840_port
->read_urb
= port
->read_urb
;
1061 /* set up our bulk in urb */
1063 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1065 usb_rcvbulkpipe(serial
->dev
,
1066 port
->bulk_in_endpointAddress
),
1067 port
->bulk_in_buffer
,
1068 mos7840_port
->read_urb
->transfer_buffer_length
,
1069 mos7840_bulk_in_callback
, mos7840_port
);
1071 dbg("mos7840_open: bulkin endpoint is %d\n",
1072 port
->bulk_in_endpointAddress
);
1073 response
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1075 err("%s - Error %d submitting control urb", __func__
,
1079 /* initialize our wait queues */
1080 init_waitqueue_head(&mos7840_port
->wait_chase
);
1081 init_waitqueue_head(&mos7840_port
->delta_msr_wait
);
1083 /* initialize our icount structure */
1084 memset(&(mos7840_port
->icount
), 0x00, sizeof(mos7840_port
->icount
));
1086 /* initialize our port settings */
1087 mos7840_port
->shadowMCR
= MCR_MASTER_IE
; /* Must set to enable ints! */
1088 /* send a open port command */
1089 mos7840_port
->open
= 1;
1090 //mos7840_change_port_settings(mos7840_port,old_termios);
1091 mos7840_port
->icount
.tx
= 0;
1092 mos7840_port
->icount
.rx
= 0;
1094 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial
, mos7840_port
, port
);
1100 /*****************************************************************************
1101 * mos7840_chars_in_buffer
1102 * this function is called by the tty driver when it wants to know how many
1103 * bytes of data we currently have outstanding in the port (data that has
1104 * been written, but hasn't made it out the port yet)
1105 * If successful, we return the number of bytes left to be written in the
1107 * Otherwise we return a negative error number.
1108 *****************************************************************************/
1110 static int mos7840_chars_in_buffer(struct usb_serial_port
*port
)
1114 unsigned long flags
;
1115 struct moschip_port
*mos7840_port
;
1117 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
1119 if (mos7840_port_paranoia_check(port
, __func__
)) {
1120 dbg("%s", "Invalid port \n");
1124 mos7840_port
= mos7840_get_port_private(port
);
1125 if (mos7840_port
== NULL
) {
1126 dbg("%s \n", "mos7840_break:leaving ...........");
1130 spin_lock_irqsave(&mos7840_port
->pool_lock
,flags
);
1131 for (i
= 0; i
< NUM_URBS
; ++i
) {
1132 if (mos7840_port
->busy
[i
]) {
1133 chars
+= URB_TRANSFER_BUFFER_SIZE
;
1136 spin_unlock_irqrestore(&mos7840_port
->pool_lock
,flags
);
1137 dbg("%s - returns %d", __func__
, chars
);
1142 /************************************************************************
1144 * mos7840_block_until_tx_empty
1146 * This function will block the close until one of the following:
1148 * 2. The mos7840 has stopped
1149 * 3. A timeout of 3 seconds without activity has expired
1151 ************************************************************************/
1152 static void mos7840_block_until_tx_empty(struct moschip_port
*mos7840_port
)
1154 int timeout
= HZ
/ 10;
1160 count
= mos7840_chars_in_buffer(mos7840_port
->port
);
1162 /* Check for Buffer status */
1167 /* Block the thread for a while */
1168 interruptible_sleep_on_timeout(&mos7840_port
->wait_chase
,
1171 /* No activity.. count down section */
1174 dbg("%s - TIMEOUT", __func__
);
1177 /* Reset timeout value back to seconds */
1183 /*****************************************************************************
1185 * this function is called by the tty driver when a port is closed
1186 *****************************************************************************/
1188 static void mos7840_close(struct usb_serial_port
*port
, struct file
*filp
)
1190 struct usb_serial
*serial
;
1191 struct moschip_port
*mos7840_port
;
1192 struct moschip_port
*port0
;
1196 dbg("%s\n", "mos7840_close:entering...");
1198 if (mos7840_port_paranoia_check(port
, __func__
)) {
1199 dbg("%s", "Port Paranoia failed \n");
1203 serial
= mos7840_get_usb_serial(port
, __func__
);
1205 dbg("%s", "Serial Paranoia failed \n");
1209 mos7840_port
= mos7840_get_port_private(port
);
1210 port0
= mos7840_get_port_private(serial
->port
[0]);
1212 if (mos7840_port
== NULL
|| port0
== NULL
)
1215 for (j
= 0; j
< NUM_URBS
; ++j
)
1216 usb_kill_urb(mos7840_port
->write_urb_pool
[j
]);
1218 /* Freeing Write URBs */
1219 for (j
= 0; j
< NUM_URBS
; ++j
) {
1220 if (mos7840_port
->write_urb_pool
[j
]) {
1221 if (mos7840_port
->write_urb_pool
[j
]->transfer_buffer
)
1222 kfree(mos7840_port
->write_urb_pool
[j
]->
1225 usb_free_urb(mos7840_port
->write_urb_pool
[j
]);
1230 /* flush and block until tx is empty */
1231 mos7840_block_until_tx_empty(mos7840_port
);
1234 /* While closing port, shutdown all bulk read, write *
1235 * and interrupt read if they exists */
1238 if (mos7840_port
->write_urb
) {
1239 dbg("%s", "Shutdown bulk write\n");
1240 usb_kill_urb(mos7840_port
->write_urb
);
1243 if (mos7840_port
->read_urb
) {
1244 dbg("%s", "Shutdown bulk read\n");
1245 usb_kill_urb(mos7840_port
->read_urb
);
1247 if ((&mos7840_port
->control_urb
)) {
1248 dbg("%s", "Shutdown control read\n");
1249 // usb_kill_urb (mos7840_port->control_urb);
1253 // if(mos7840_port->ctrl_buf != NULL)
1254 // kfree(mos7840_port->ctrl_buf);
1255 port0
->open_ports
--;
1256 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1257 port0
->open_ports
, port
->number
);
1258 if (port0
->open_ports
== 0) {
1259 if (serial
->port
[0]->interrupt_in_urb
) {
1260 dbg("%s", "Shutdown interrupt_in_urb\n");
1261 usb_kill_urb(serial
->port
[0]->interrupt_in_urb
);
1265 if (mos7840_port
->write_urb
) {
1266 /* if this urb had a transfer buffer already (old tx) free it */
1268 if (mos7840_port
->write_urb
->transfer_buffer
!= NULL
) {
1269 kfree(mos7840_port
->write_urb
->transfer_buffer
);
1271 usb_free_urb(mos7840_port
->write_urb
);
1275 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1278 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1280 mos7840_port
->open
= 0;
1282 dbg("%s \n", "Leaving ............");
1285 /************************************************************************
1287 * mos7840_block_until_chase_response
1289 * This function will block the close until one of the following:
1290 * 1. Response to our Chase comes from mos7840
1291 * 2. A timeout of 10 seconds without activity has expired
1292 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1294 ************************************************************************/
1296 static void mos7840_block_until_chase_response(struct moschip_port
1299 int timeout
= 1 * HZ
;
1304 count
= mos7840_chars_in_buffer(mos7840_port
->port
);
1306 /* Check for Buffer status */
1311 /* Block the thread for a while */
1312 interruptible_sleep_on_timeout(&mos7840_port
->wait_chase
,
1314 /* No activity.. count down section */
1317 dbg("%s - TIMEOUT", __func__
);
1320 /* Reset timeout value back to seconds */
1327 /*****************************************************************************
1329 * this function sends a break to the port
1330 *****************************************************************************/
1331 static void mos7840_break(struct usb_serial_port
*port
, int break_state
)
1334 struct usb_serial
*serial
;
1335 struct moschip_port
*mos7840_port
;
1337 dbg("%s \n", "Entering ...........");
1338 dbg("mos7840_break: Start\n");
1340 if (mos7840_port_paranoia_check(port
, __func__
)) {
1341 dbg("%s", "Port Paranoia failed \n");
1345 serial
= mos7840_get_usb_serial(port
, __func__
);
1347 dbg("%s", "Serial Paranoia failed \n");
1351 mos7840_port
= mos7840_get_port_private(port
);
1353 if (mos7840_port
== NULL
) {
1359 /* flush and block until tx is empty */
1360 mos7840_block_until_chase_response(mos7840_port
);
1363 if (break_state
== -1) {
1364 data
= mos7840_port
->shadowLCR
| LCR_SET_BREAK
;
1366 data
= mos7840_port
->shadowLCR
& ~LCR_SET_BREAK
;
1369 mos7840_port
->shadowLCR
= data
;
1370 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1371 mos7840_port
->shadowLCR
);
1372 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
,
1373 mos7840_port
->shadowLCR
);
1378 /*****************************************************************************
1379 * mos7840_write_room
1380 * this function is called by the tty driver when it wants to know how many
1381 * bytes of data we can accept for a specific port.
1382 * If successful, we return the amount of room that we have for this port
1383 * Otherwise we return a negative error number.
1384 *****************************************************************************/
1386 static int mos7840_write_room(struct usb_serial_port
*port
)
1390 unsigned long flags
;
1391 struct moschip_port
*mos7840_port
;
1393 dbg("%s \n", " mos7840_write_room:entering ...........");
1395 if (mos7840_port_paranoia_check(port
, __func__
)) {
1396 dbg("%s", "Invalid port \n");
1397 dbg("%s \n", " mos7840_write_room:leaving ...........");
1401 mos7840_port
= mos7840_get_port_private(port
);
1402 if (mos7840_port
== NULL
) {
1403 dbg("%s \n", "mos7840_break:leaving ...........");
1407 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1408 for (i
= 0; i
< NUM_URBS
; ++i
) {
1409 if (!mos7840_port
->busy
[i
]) {
1410 room
+= URB_TRANSFER_BUFFER_SIZE
;
1413 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1415 room
= (room
== 0) ? 0 : room
- URB_TRANSFER_BUFFER_SIZE
+ 1;
1416 dbg("%s - returns %d", __func__
, room
);
1421 /*****************************************************************************
1423 * this function is called by the tty driver when data should be written to
1425 * If successful, we return the number of bytes written, otherwise we
1426 * return a negative error number.
1427 *****************************************************************************/
1429 static int mos7840_write(struct usb_serial_port
*port
,
1430 const unsigned char *data
, int count
)
1436 unsigned long flags
;
1438 struct moschip_port
*mos7840_port
;
1439 struct usb_serial
*serial
;
1442 const unsigned char *current_position
= data
;
1443 unsigned char *data1
;
1444 dbg("%s \n", "entering ...........");
1445 //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
1450 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
1451 mos7840_port
->shadowLCR
= Data
;
1452 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data
);
1453 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1454 mos7840_port
->shadowLCR
);
1457 //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
1458 //mos7840_port->shadowLCR=Data;//Need to add later
1460 Data
|= SERIAL_LCR_DLAB
; //data latch enable in LCR 0x80
1462 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1465 //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
1468 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_LSB
, &Data
);
1469 dbg("mos7840_write:DLL value is %x\n", Data
);
1473 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_MSB
, &Data
);
1474 dbg("mos7840_write:DLM value is %x\n", Data
);
1476 Data
= Data
& ~SERIAL_LCR_DLAB
;
1477 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1478 mos7840_port
->shadowLCR
);
1480 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1483 if (mos7840_port_paranoia_check(port
, __func__
)) {
1484 dbg("%s", "Port Paranoia failed \n");
1488 serial
= port
->serial
;
1489 if (mos7840_serial_paranoia_check(serial
, __func__
)) {
1490 dbg("%s", "Serial Paranoia failed \n");
1494 mos7840_port
= mos7840_get_port_private(port
);
1495 if (mos7840_port
== NULL
) {
1496 dbg("%s", "mos7840_port is NULL\n");
1500 /* try to find a free urb in the list */
1503 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1504 for (i
= 0; i
< NUM_URBS
; ++i
) {
1505 if (!mos7840_port
->busy
[i
]) {
1506 mos7840_port
->busy
[i
] = 1;
1507 urb
= mos7840_port
->write_urb_pool
[i
];
1512 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1515 dbg("%s - no more free urbs", __func__
);
1519 if (urb
->transfer_buffer
== NULL
) {
1520 urb
->transfer_buffer
=
1521 kmalloc(URB_TRANSFER_BUFFER_SIZE
, GFP_KERNEL
);
1523 if (urb
->transfer_buffer
== NULL
) {
1524 err("%s no more kernel memory...", __func__
);
1528 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1530 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1532 /* fill urb with data and submit */
1533 usb_fill_bulk_urb(urb
,
1535 usb_sndbulkpipe(serial
->dev
,
1536 port
->bulk_out_endpointAddress
),
1537 urb
->transfer_buffer
,
1539 mos7840_bulk_out_data_callback
, mos7840_port
);
1541 data1
= urb
->transfer_buffer
;
1542 dbg("\nbulkout endpoint is %d", port
->bulk_out_endpointAddress
);
1544 /* send it down the pipe */
1545 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1548 mos7840_port
->busy
[i
] = 0;
1549 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1551 bytes_sent
= status
;
1554 bytes_sent
= transfer_size
;
1555 mos7840_port
->icount
.tx
+= transfer_size
;
1557 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port
->icount
.tx
);
1564 /*****************************************************************************
1566 * this function is called by the tty driver when it wants to stop the data
1567 * being read from the port.
1568 *****************************************************************************/
1570 static void mos7840_throttle(struct usb_serial_port
*port
)
1572 struct moschip_port
*mos7840_port
;
1573 struct tty_struct
*tty
;
1576 if (mos7840_port_paranoia_check(port
, __func__
)) {
1577 dbg("%s", "Invalid port \n");
1581 dbg("- port %d\n", port
->number
);
1583 mos7840_port
= mos7840_get_port_private(port
);
1585 if (mos7840_port
== NULL
)
1588 if (!mos7840_port
->open
) {
1589 dbg("%s\n", "port not opened");
1593 dbg("%s", "Entering .......... \n");
1597 dbg("%s - no tty available", __func__
);
1601 /* if we are implementing XON/XOFF, send the stop character */
1603 unsigned char stop_char
= STOP_CHAR(tty
);
1604 status
= mos7840_write(port
, &stop_char
, 1);
1610 /* if we are implementing RTS/CTS, toggle that line */
1611 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1612 mos7840_port
->shadowMCR
&= ~MCR_RTS
;
1615 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1616 mos7840_port
->shadowMCR
);
1626 /*****************************************************************************
1627 * mos7840_unthrottle
1628 * this function is called by the tty driver when it wants to resume the data
1629 * being read from the port (called after SerialThrottle is called)
1630 *****************************************************************************/
1631 static void mos7840_unthrottle(struct usb_serial_port
*port
)
1633 struct tty_struct
*tty
;
1635 struct moschip_port
*mos7840_port
= mos7840_get_port_private(port
);
1637 if (mos7840_port_paranoia_check(port
, __func__
)) {
1638 dbg("%s", "Invalid port \n");
1642 if (mos7840_port
== NULL
)
1645 if (!mos7840_port
->open
) {
1646 dbg("%s - port not opened", __func__
);
1650 dbg("%s", "Entering .......... \n");
1654 dbg("%s - no tty available", __func__
);
1658 /* if we are implementing XON/XOFF, send the start character */
1660 unsigned char start_char
= START_CHAR(tty
);
1661 status
= mos7840_write(port
, &start_char
, 1);
1667 /* if we are implementing RTS/CTS, toggle that line */
1668 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1669 mos7840_port
->shadowMCR
|= MCR_RTS
;
1672 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1673 mos7840_port
->shadowMCR
);
1682 static int mos7840_tiocmget(struct usb_serial_port
*port
, struct file
*file
)
1684 struct moschip_port
*mos7840_port
;
1685 unsigned int result
;
1689 mos7840_port
= mos7840_get_port_private(port
);
1691 dbg("%s - port %d", __func__
, port
->number
);
1693 if (mos7840_port
== NULL
)
1696 status
= mos7840_get_uart_reg(port
, MODEM_STATUS_REGISTER
, &msr
);
1697 status
= mos7840_get_uart_reg(port
, MODEM_CONTROL_REGISTER
, &mcr
);
1698 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0)
1699 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0)
1700 | ((mcr
& MCR_LOOPBACK
) ? TIOCM_LOOP
: 0)
1701 | ((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0)
1702 | ((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0)
1703 | ((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0)
1704 | ((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0);
1706 dbg("%s - 0x%04X", __func__
, result
);
1711 static int mos7840_tiocmset(struct usb_serial_port
*port
, struct file
*file
,
1712 unsigned int set
, unsigned int clear
)
1714 struct moschip_port
*mos7840_port
;
1718 dbg("%s - port %d", __func__
, port
->number
);
1720 mos7840_port
= mos7840_get_port_private(port
);
1722 if (mos7840_port
== NULL
)
1725 /* FIXME: What locks the port registers ? */
1726 mcr
= mos7840_port
->shadowMCR
;
1727 if (clear
& TIOCM_RTS
)
1729 if (clear
& TIOCM_DTR
)
1731 if (clear
& TIOCM_LOOP
)
1732 mcr
&= ~MCR_LOOPBACK
;
1734 if (set
& TIOCM_RTS
)
1736 if (set
& TIOCM_DTR
)
1738 if (set
& TIOCM_LOOP
)
1739 mcr
|= MCR_LOOPBACK
;
1741 mos7840_port
->shadowMCR
= mcr
;
1743 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, mcr
);
1745 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
1752 /*****************************************************************************
1753 * mos7840_calc_baud_rate_divisor
1754 * this function calculates the proper baud rate divisor for the specified
1756 *****************************************************************************/
1757 static int mos7840_calc_baud_rate_divisor(int baudRate
, int *divisor
,
1758 __u16
* clk_sel_val
)
1761 dbg("%s - %d", __func__
, baudRate
);
1763 if (baudRate
<= 115200) {
1764 *divisor
= 115200 / baudRate
;
1767 if ((baudRate
> 115200) && (baudRate
<= 230400)) {
1768 *divisor
= 230400 / baudRate
;
1769 *clk_sel_val
= 0x10;
1770 } else if ((baudRate
> 230400) && (baudRate
<= 403200)) {
1771 *divisor
= 403200 / baudRate
;
1772 *clk_sel_val
= 0x20;
1773 } else if ((baudRate
> 403200) && (baudRate
<= 460800)) {
1774 *divisor
= 460800 / baudRate
;
1775 *clk_sel_val
= 0x30;
1776 } else if ((baudRate
> 460800) && (baudRate
<= 806400)) {
1777 *divisor
= 806400 / baudRate
;
1778 *clk_sel_val
= 0x40;
1779 } else if ((baudRate
> 806400) && (baudRate
<= 921600)) {
1780 *divisor
= 921600 / baudRate
;
1781 *clk_sel_val
= 0x50;
1782 } else if ((baudRate
> 921600) && (baudRate
<= 1572864)) {
1783 *divisor
= 1572864 / baudRate
;
1784 *clk_sel_val
= 0x60;
1785 } else if ((baudRate
> 1572864) && (baudRate
<= 3145728)) {
1786 *divisor
= 3145728 / baudRate
;
1787 *clk_sel_val
= 0x70;
1793 for (i
= 0; i
< ARRAY_SIZE(mos7840_divisor_table
); i
++) {
1794 if (mos7840_divisor_table
[i
].BaudRate
== baudrate
) {
1795 *divisor
= mos7840_divisor_table
[i
].Divisor
;
1800 /* After trying for all the standard baud rates *
1801 * Try calculating the divisor for this baud rate */
1803 if (baudrate
> 75 && baudrate
< 230400) {
1804 /* get the divisor */
1805 custom
= (__u16
) (230400L / baudrate
);
1807 /* Check for round off */
1808 round1
= (__u16
) (2304000L / baudrate
);
1809 round
= (__u16
) (round1
- (custom
* 10));
1815 dbg(" Baud %d = %d\n", baudrate
, custom
);
1819 dbg("%s\n", " Baud calculation Failed...");
1824 /*****************************************************************************
1825 * mos7840_send_cmd_write_baud_rate
1826 * this function sends the proper command to change the baud rate of the
1828 *****************************************************************************/
1830 static int mos7840_send_cmd_write_baud_rate(struct moschip_port
*mos7840_port
,
1836 unsigned char number
;
1838 struct usb_serial_port
*port
;
1840 if (mos7840_port
== NULL
)
1843 port
= (struct usb_serial_port
*)mos7840_port
->port
;
1844 if (mos7840_port_paranoia_check(port
, __func__
)) {
1845 dbg("%s", "Invalid port \n");
1849 if (mos7840_serial_paranoia_check(port
->serial
, __func__
)) {
1850 dbg("%s", "Invalid Serial \n");
1854 dbg("%s", "Entering .......... \n");
1856 number
= mos7840_port
->port
->number
- mos7840_port
->port
->serial
->minor
;
1858 dbg("%s - port = %d, baud = %d", __func__
,
1859 mos7840_port
->port
->number
, baudRate
);
1860 //reset clk_uart_sel in spregOffset
1861 if (baudRate
> 115200) {
1862 #ifdef HW_flow_control
1863 //NOTE: need to see the pther register to modify
1864 //setting h/w flow control bit to 1;
1867 mos7840_port
->shadowMCR
= Data
;
1869 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1871 dbg("Writing spreg failed in set_serial_baud\n");
1877 #ifdef HW_flow_control
1878 //setting h/w flow control bit to 0;
1881 mos7840_port
->shadowMCR
= Data
;
1883 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1885 dbg("Writing spreg failed in set_serial_baud\n");
1892 if (1) //baudRate <= 115200)
1898 mos7840_calc_baud_rate_divisor(baudRate
, &divisor
,
1901 mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
,
1904 dbg("reading spreg failed in set_serial_baud\n");
1907 Data
= (Data
& 0x8f) | clk_sel_val
;
1910 mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
1912 dbg("Writing spreg failed in set_serial_baud\n");
1915 /* Calculate the Divisor */
1918 err("%s - bad baud rate", __func__
);
1919 dbg("%s\n", "bad baud rate");
1922 /* Enable access to divisor latch */
1923 Data
= mos7840_port
->shadowLCR
| SERIAL_LCR_DLAB
;
1924 mos7840_port
->shadowLCR
= Data
;
1925 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1927 /* Write the divisor */
1928 Data
= (unsigned char)(divisor
& 0xff);
1929 dbg("set_serial_baud Value to write DLL is %x\n", Data
);
1930 mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
1932 Data
= (unsigned char)((divisor
& 0xff00) >> 8);
1933 dbg("set_serial_baud Value to write DLM is %x\n", Data
);
1934 mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
1936 /* Disable access to divisor latch */
1937 Data
= mos7840_port
->shadowLCR
& ~SERIAL_LCR_DLAB
;
1938 mos7840_port
->shadowLCR
= Data
;
1939 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1946 /*****************************************************************************
1947 * mos7840_change_port_settings
1948 * This routine is called to set the UART on the device to match
1949 * the specified new settings.
1950 *****************************************************************************/
1952 static void mos7840_change_port_settings(struct moschip_port
*mos7840_port
,
1953 struct ktermios
*old_termios
)
1955 struct tty_struct
*tty
;
1964 struct usb_serial_port
*port
;
1965 struct usb_serial
*serial
;
1967 if (mos7840_port
== NULL
)
1970 port
= (struct usb_serial_port
*)mos7840_port
->port
;
1972 if (mos7840_port_paranoia_check(port
, __func__
)) {
1973 dbg("%s", "Invalid port \n");
1977 if (mos7840_serial_paranoia_check(port
->serial
, __func__
)) {
1978 dbg("%s", "Invalid Serial \n");
1982 serial
= port
->serial
;
1984 dbg("%s - port %d", __func__
, mos7840_port
->port
->number
);
1986 if (!mos7840_port
->open
) {
1987 dbg("%s - port not opened", __func__
);
1991 tty
= mos7840_port
->port
->tty
;
1993 dbg("%s", "Entering .......... \n");
1997 lParity
= LCR_PAR_NONE
;
1999 cflag
= tty
->termios
->c_cflag
;
2000 iflag
= tty
->termios
->c_iflag
;
2002 /* Change the number of bits */
2003 if (cflag
& CSIZE
) {
2004 switch (cflag
& CSIZE
) {
2022 /* Change the Parity bit */
2023 if (cflag
& PARENB
) {
2024 if (cflag
& PARODD
) {
2025 lParity
= LCR_PAR_ODD
;
2026 dbg("%s - parity = odd", __func__
);
2028 lParity
= LCR_PAR_EVEN
;
2029 dbg("%s - parity = even", __func__
);
2033 dbg("%s - parity = none", __func__
);
2036 if (cflag
& CMSPAR
) {
2037 lParity
= lParity
| 0x20;
2040 /* Change the Stop bit */
2041 if (cflag
& CSTOPB
) {
2043 dbg("%s - stop bits = 2", __func__
);
2046 dbg("%s - stop bits = 1", __func__
);
2049 /* Update the LCR with the correct value */
2050 mos7840_port
->shadowLCR
&=
2051 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
2052 mos7840_port
->shadowLCR
|= (lData
| lParity
| lStop
);
2054 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2055 mos7840_port
->shadowLCR
);
2056 /* Disable Interrupts */
2058 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
2061 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
2064 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
2066 /* Send the updated LCR value to the mos7840 */
2067 Data
= mos7840_port
->shadowLCR
;
2069 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
2072 mos7840_port
->shadowMCR
= Data
;
2073 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2075 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2077 /* set up the MCR register and send it to the mos7840 */
2079 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
2080 if (cflag
& CBAUD
) {
2081 mos7840_port
->shadowMCR
|= (MCR_DTR
| MCR_RTS
);
2084 if (cflag
& CRTSCTS
) {
2085 mos7840_port
->shadowMCR
|= (MCR_XON_ANY
);
2088 mos7840_port
->shadowMCR
&= ~(MCR_XON_ANY
);
2091 Data
= mos7840_port
->shadowMCR
;
2092 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2094 /* Determine divisor based on baud rate */
2095 baud
= tty_get_baud_rate(tty
);
2098 /* pick a default, any default... */
2099 dbg("%s\n", "Picked default baud...");
2103 dbg("%s - baud rate = %d", __func__
, baud
);
2104 status
= mos7840_send_cmd_write_baud_rate(mos7840_port
, baud
);
2106 /* Enable Interrupts */
2108 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
2110 if (mos7840_port
->read_urb
->status
!= -EINPROGRESS
) {
2111 mos7840_port
->read_urb
->dev
= serial
->dev
;
2113 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
2116 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2120 wake_up(&mos7840_port
->delta_msr_wait
);
2121 mos7840_port
->delta_msr_cond
= 1;
2122 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2123 mos7840_port
->shadowLCR
);
2128 /*****************************************************************************
2129 * mos7840_set_termios
2130 * this function is called by the tty driver when it wants to change
2131 * the termios structure
2132 *****************************************************************************/
2134 static void mos7840_set_termios(struct usb_serial_port
*port
,
2135 struct ktermios
*old_termios
)
2139 struct usb_serial
*serial
;
2140 struct moschip_port
*mos7840_port
;
2141 struct tty_struct
*tty
;
2142 dbg("mos7840_set_termios: START\n");
2143 if (mos7840_port_paranoia_check(port
, __func__
)) {
2144 dbg("%s", "Invalid port \n");
2148 serial
= port
->serial
;
2150 if (mos7840_serial_paranoia_check(serial
, __func__
)) {
2151 dbg("%s", "Invalid Serial \n");
2155 mos7840_port
= mos7840_get_port_private(port
);
2157 if (mos7840_port
== NULL
)
2162 if (!mos7840_port
->open
) {
2163 dbg("%s - port not opened", __func__
);
2167 dbg("%s\n", "setting termios - ");
2169 cflag
= tty
->termios
->c_cflag
;
2171 dbg("%s - clfag %08x iflag %08x", __func__
,
2172 tty
->termios
->c_cflag
, RELEVANT_IFLAG(tty
->termios
->c_iflag
));
2173 dbg("%s - old clfag %08x old iflag %08x", __func__
,
2174 old_termios
->c_cflag
, RELEVANT_IFLAG(old_termios
->c_iflag
));
2175 dbg("%s - port %d", __func__
, port
->number
);
2177 /* change the port settings to the new ones specified */
2179 mos7840_change_port_settings(mos7840_port
, old_termios
);
2181 if (!mos7840_port
->read_urb
) {
2182 dbg("%s", "URB KILLED !!!!!\n");
2186 if (mos7840_port
->read_urb
->status
!= -EINPROGRESS
) {
2187 mos7840_port
->read_urb
->dev
= serial
->dev
;
2188 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
2190 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2197 /*****************************************************************************
2198 * mos7840_get_lsr_info - get line status register info
2200 * Purpose: Let user call ioctl() to get info when the UART physically
2201 * is emptied. On bus types like RS485, the transmitter must
2202 * release the bus after transmitting. This must be done when
2203 * the transmit shift register is empty, not be done when the
2204 * transmit holding register is empty. This functionality
2205 * allows an RS485 driver to be written in user space.
2206 *****************************************************************************/
2208 static int mos7840_get_lsr_info(struct moschip_port
*mos7840_port
,
2209 unsigned int __user
*value
)
2212 unsigned int result
= 0;
2214 count
= mos7840_chars_in_buffer(mos7840_port
->port
);
2216 dbg("%s -- Empty", __func__
);
2217 result
= TIOCSER_TEMT
;
2220 if (copy_to_user(value
, &result
, sizeof(int)))
2225 /*****************************************************************************
2226 * mos7840_set_modem_info
2227 * function to set modem info
2228 *****************************************************************************/
2230 static int mos7840_set_modem_info(struct moschip_port
*mos7840_port
,
2231 unsigned int cmd
, unsigned int __user
*value
)
2237 struct usb_serial_port
*port
;
2239 if (mos7840_port
== NULL
)
2242 port
= (struct usb_serial_port
*)mos7840_port
->port
;
2243 if (mos7840_port_paranoia_check(port
, __func__
)) {
2244 dbg("%s", "Invalid port \n");
2248 mcr
= mos7840_port
->shadowMCR
;
2250 if (copy_from_user(&arg
, value
, sizeof(int)))
2255 if (arg
& TIOCM_RTS
)
2257 if (arg
& TIOCM_DTR
)
2259 if (arg
& TIOCM_LOOP
)
2260 mcr
|= MCR_LOOPBACK
;
2264 if (arg
& TIOCM_RTS
)
2266 if (arg
& TIOCM_DTR
)
2268 if (arg
& TIOCM_LOOP
)
2269 mcr
&= ~MCR_LOOPBACK
;
2273 /* turn off the RTS and DTR and LOOPBACK
2274 * and then only turn on what was asked to */
2275 mcr
&= ~(MCR_RTS
| MCR_DTR
| MCR_LOOPBACK
);
2276 mcr
|= ((arg
& TIOCM_RTS
) ? MCR_RTS
: 0);
2277 mcr
|= ((arg
& TIOCM_DTR
) ? MCR_DTR
: 0);
2278 mcr
|= ((arg
& TIOCM_LOOP
) ? MCR_LOOPBACK
: 0);
2282 mos7840_port
->shadowMCR
= mcr
;
2284 Data
= mos7840_port
->shadowMCR
;
2286 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2288 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2295 /*****************************************************************************
2296 * mos7840_get_modem_info
2297 * function to get modem info
2298 *****************************************************************************/
2300 static int mos7840_get_modem_info(struct moschip_port
*mos7840_port
,
2301 unsigned int __user
*value
)
2303 unsigned int result
= 0;
2305 unsigned int mcr
= mos7840_port
->shadowMCR
;
2308 mos7840_get_uart_reg(mos7840_port
->port
, MODEM_STATUS_REGISTER
,
2310 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0) /* 0x002 */
2311 |((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0) /* 0x004 */
2312 |((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0) /* 0x020 */
2313 |((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0) /* 0x040 */
2314 |((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0) /* 0x080 */
2315 |((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0); /* 0x100 */
2317 dbg("%s -- %x", __func__
, result
);
2319 if (copy_to_user(value
, &result
, sizeof(int)))
2324 /*****************************************************************************
2325 * mos7840_get_serial_info
2326 * function to get information about serial port
2327 *****************************************************************************/
2329 static int mos7840_get_serial_info(struct moschip_port
*mos7840_port
,
2330 struct serial_struct __user
*retinfo
)
2332 struct serial_struct tmp
;
2334 if (mos7840_port
== NULL
)
2340 memset(&tmp
, 0, sizeof(tmp
));
2342 tmp
.type
= PORT_16550A
;
2343 tmp
.line
= mos7840_port
->port
->serial
->minor
;
2344 tmp
.port
= mos7840_port
->port
->number
;
2346 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
2347 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
2348 tmp
.baud_base
= 9600;
2349 tmp
.close_delay
= 5 * HZ
;
2350 tmp
.closing_wait
= 30 * HZ
;
2352 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2357 /*****************************************************************************
2359 * this function handles any ioctl calls to the driver
2360 *****************************************************************************/
2362 static int mos7840_ioctl(struct usb_serial_port
*port
, struct file
*file
,
2363 unsigned int cmd
, unsigned long arg
)
2365 void __user
*argp
= (void __user
*)arg
;
2366 struct moschip_port
*mos7840_port
;
2367 struct tty_struct
*tty
;
2369 struct async_icount cnow
;
2370 struct async_icount cprev
;
2371 struct serial_icounter_struct icount
;
2374 if (mos7840_port_paranoia_check(port
, __func__
)) {
2375 dbg("%s", "Invalid port \n");
2379 mos7840_port
= mos7840_get_port_private(port
);
2381 if (mos7840_port
== NULL
)
2384 tty
= mos7840_port
->port
->tty
;
2386 dbg("%s - port %d, cmd = 0x%x", __func__
, port
->number
, cmd
);
2389 /* return number of bytes available */
2392 dbg("%s (%d) TIOCSERGETLSR", __func__
, port
->number
);
2393 return mos7840_get_lsr_info(mos7840_port
, argp
);
2399 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__
,
2402 mos7840_set_modem_info(mos7840_port
, cmd
, argp
);
2406 dbg("%s (%d) TIOCMGET", __func__
, port
->number
);
2407 return mos7840_get_modem_info(mos7840_port
, argp
);
2410 dbg("%s (%d) TIOCGSERIAL", __func__
, port
->number
);
2411 return mos7840_get_serial_info(mos7840_port
, argp
);
2414 dbg("%s (%d) TIOCSSERIAL", __func__
, port
->number
);
2418 dbg("%s (%d) TIOCMIWAIT", __func__
, port
->number
);
2419 cprev
= mos7840_port
->icount
;
2421 //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
2422 mos7840_port
->delta_msr_cond
= 0;
2423 wait_event_interruptible(mos7840_port
->delta_msr_wait
,
2425 delta_msr_cond
== 1));
2427 /* see if a signal did it */
2428 if (signal_pending(current
))
2429 return -ERESTARTSYS
;
2430 cnow
= mos7840_port
->icount
;
2432 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
2433 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
2434 return -EIO
; /* no change => error */
2435 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
2436 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
2437 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
2438 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
2447 cnow
= mos7840_port
->icount
;
2449 icount
.cts
= cnow
.cts
;
2450 icount
.dsr
= cnow
.dsr
;
2451 icount
.rng
= cnow
.rng
;
2452 icount
.dcd
= cnow
.dcd
;
2453 icount
.rx
= cnow
.rx
;
2454 icount
.tx
= cnow
.tx
;
2455 icount
.frame
= cnow
.frame
;
2456 icount
.overrun
= cnow
.overrun
;
2457 icount
.parity
= cnow
.parity
;
2458 icount
.brk
= cnow
.brk
;
2459 icount
.buf_overrun
= cnow
.buf_overrun
;
2461 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__
,
2462 port
->number
, icount
.rx
, icount
.tx
);
2463 if (copy_to_user(argp
, &icount
, sizeof(icount
)))
2473 return -ENOIOCTLCMD
;
2476 static int mos7840_calc_num_ports(struct usb_serial
*serial
)
2478 int mos7840_num_ports
= 0;
2480 dbg("numberofendpoints: %d \n",
2481 (int)serial
->interface
->cur_altsetting
->desc
.bNumEndpoints
);
2482 dbg("numberofendpoints: %d \n",
2483 (int)serial
->interface
->altsetting
->desc
.bNumEndpoints
);
2484 if (serial
->interface
->cur_altsetting
->desc
.bNumEndpoints
== 5) {
2485 mos7840_num_ports
= serial
->num_ports
= 2;
2486 } else if (serial
->interface
->cur_altsetting
->desc
.bNumEndpoints
== 9) {
2487 serial
->num_bulk_in
= 4;
2488 serial
->num_bulk_out
= 4;
2489 mos7840_num_ports
= serial
->num_ports
= 4;
2492 return mos7840_num_ports
;
2495 /****************************************************************************
2497 ****************************************************************************/
2499 static int mos7840_startup(struct usb_serial
*serial
)
2501 struct moschip_port
*mos7840_port
;
2502 struct usb_device
*dev
;
2506 dbg("%s \n", " mos7840_startup :entering..........");
2509 dbg("%s\n", "Invalid Handler");
2515 dbg("%s\n", "Entering...");
2517 /* we set up the pointers to the endpoints in the mos7840_open *
2518 * function, as the structures aren't created yet. */
2520 /* set up port private structures */
2521 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2522 mos7840_port
= kzalloc(sizeof(struct moschip_port
), GFP_KERNEL
);
2523 if (mos7840_port
== NULL
) {
2524 err("%s - Out of memory", __func__
);
2526 i
--; /* don't follow NULL pointer cleaning up */
2530 /* Initialize all port interrupt end point to port 0 int endpoint *
2531 * Our device has only one interrupt end point comman to all port */
2533 mos7840_port
->port
= serial
->port
[i
];
2534 mos7840_set_port_private(serial
->port
[i
], mos7840_port
);
2535 spin_lock_init(&mos7840_port
->pool_lock
);
2537 mos7840_port
->port_num
= ((serial
->port
[i
]->number
-
2538 (serial
->port
[i
]->serial
->minor
)) +
2541 if (mos7840_port
->port_num
== 1) {
2542 mos7840_port
->SpRegOffset
= 0x0;
2543 mos7840_port
->ControlRegOffset
= 0x1;
2544 mos7840_port
->DcrRegOffset
= 0x4;
2545 } else if ((mos7840_port
->port_num
== 2)
2546 && (serial
->num_ports
== 4)) {
2547 mos7840_port
->SpRegOffset
= 0x8;
2548 mos7840_port
->ControlRegOffset
= 0x9;
2549 mos7840_port
->DcrRegOffset
= 0x16;
2550 } else if ((mos7840_port
->port_num
== 2)
2551 && (serial
->num_ports
== 2)) {
2552 mos7840_port
->SpRegOffset
= 0xa;
2553 mos7840_port
->ControlRegOffset
= 0xb;
2554 mos7840_port
->DcrRegOffset
= 0x19;
2555 } else if ((mos7840_port
->port_num
== 3)
2556 && (serial
->num_ports
== 4)) {
2557 mos7840_port
->SpRegOffset
= 0xa;
2558 mos7840_port
->ControlRegOffset
= 0xb;
2559 mos7840_port
->DcrRegOffset
= 0x19;
2560 } else if ((mos7840_port
->port_num
== 4)
2561 && (serial
->num_ports
== 4)) {
2562 mos7840_port
->SpRegOffset
= 0xc;
2563 mos7840_port
->ControlRegOffset
= 0xd;
2564 mos7840_port
->DcrRegOffset
= 0x1c;
2566 mos7840_dump_serial_port(mos7840_port
);
2568 mos7840_set_port_private(serial
->port
[i
], mos7840_port
);
2570 //enable rx_disable bit in control register
2573 mos7840_get_reg_sync(serial
->port
[i
],
2574 mos7840_port
->ControlRegOffset
, &Data
);
2576 dbg("Reading ControlReg failed status-0x%x\n", status
);
2579 dbg("ControlReg Reading success val is %x, status%d\n",
2581 Data
|= 0x08; //setting driver done bit
2582 Data
|= 0x04; //sp1_bit to have cts change reflect in modem status reg
2584 //Data |= 0x20; //rx_disable bit
2587 mos7840_set_reg_sync(serial
->port
[i
],
2588 mos7840_port
->ControlRegOffset
, Data
);
2590 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status
);
2593 dbg("ControlReg Writing success(rx_disable) status%d\n",
2596 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2600 mos7840_set_reg_sync(serial
->port
[i
],
2601 (__u16
) (mos7840_port
->DcrRegOffset
+
2604 dbg("Writing DCR0 failed status-0x%x\n", status
);
2607 dbg("DCR0 Writing success status%d\n", status
);
2612 mos7840_set_reg_sync(serial
->port
[i
],
2613 (__u16
) (mos7840_port
->DcrRegOffset
+
2616 dbg("Writing DCR1 failed status-0x%x\n", status
);
2619 dbg("DCR1 Writing success status%d\n", status
);
2624 mos7840_set_reg_sync(serial
->port
[i
],
2625 (__u16
) (mos7840_port
->DcrRegOffset
+
2628 dbg("Writing DCR2 failed status-0x%x\n", status
);
2631 dbg("DCR2 Writing success status%d\n", status
);
2633 // write values in clkstart0x0 and clkmulti 0x20
2637 mos7840_set_reg_sync(serial
->port
[i
],
2638 CLK_START_VALUE_REGISTER
, Data
);
2640 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status
);
2643 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status
);
2647 mos7840_set_reg_sync(serial
->port
[i
], CLK_MULTI_REGISTER
,
2650 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2654 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2657 //write value 0x0 to scratchpad register
2660 mos7840_set_uart_reg(serial
->port
[i
], SCRATCH_PAD_REGISTER
,
2663 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2667 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2670 //Zero Length flag register
2671 if ((mos7840_port
->port_num
!= 1)
2672 && (serial
->num_ports
== 2)) {
2676 status
= mos7840_set_reg_sync(serial
->port
[i
],
2682 dbg("ZLIP offset%x\n",
2684 ((__u16
) mos7840_port
->port_num
)));
2686 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2690 dbg("ZLP_REG%d Writing success status%d\n",
2695 status
= mos7840_set_reg_sync(serial
->port
[i
],
2701 dbg("ZLIP offset%x\n",
2703 ((__u16
) mos7840_port
->port_num
) - 0x1));
2705 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2709 dbg("ZLP_REG%d Writing success status%d\n",
2713 mos7840_port
->control_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2714 mos7840_port
->ctrl_buf
= kmalloc(16, GFP_KERNEL
);
2715 mos7840_port
->dr
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_KERNEL
);
2716 if (!mos7840_port
->control_urb
|| !mos7840_port
->ctrl_buf
|| !mos7840_port
->dr
) {
2722 //Zero Length flag enable
2724 status
= mos7840_set_reg_sync(serial
->port
[0], ZLP_REG5
, Data
);
2726 dbg("Writing ZLP_REG5 failed status-0x%x\n", status
);
2729 dbg("ZLP_REG5 Writing success status%d\n", status
);
2731 /* setting configuration feature to one */
2732 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2733 (__u8
) 0x03, 0x00, 0x01, 0x00, NULL
, 0x00, 5 * HZ
);
2736 for (/* nothing */; i
>= 0; i
--) {
2737 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
2739 kfree(mos7840_port
->dr
);
2740 kfree(mos7840_port
->ctrl_buf
);
2741 usb_free_urb(mos7840_port
->control_urb
);
2742 kfree(mos7840_port
);
2743 serial
->port
[i
] = NULL
;
2748 /****************************************************************************
2750 * This function is called whenever the device is removed from the usb bus.
2751 ****************************************************************************/
2753 static void mos7840_shutdown(struct usb_serial
*serial
)
2756 unsigned long flags
;
2757 struct moschip_port
*mos7840_port
;
2758 dbg("%s \n", " shutdown :entering..........");
2761 dbg("%s", "Invalid Handler \n");
2765 /* check for the ports to be closed,close the ports and disconnect */
2767 /* free private structure allocated for serial port *
2768 * stop reads and writes on all ports */
2770 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2771 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
2772 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
2773 mos7840_port
->zombie
= 1;
2774 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
2775 usb_kill_urb(mos7840_port
->control_urb
);
2776 kfree(mos7840_port
->ctrl_buf
);
2777 kfree(mos7840_port
->dr
);
2778 kfree(mos7840_port
);
2779 mos7840_set_port_private(serial
->port
[i
], NULL
);
2782 dbg("%s\n", "Thank u :: ");
2786 static struct usb_driver io_driver
= {
2788 .probe
= usb_serial_probe
,
2789 .disconnect
= usb_serial_disconnect
,
2790 .id_table
= moschip_id_table_combined
,
2794 static struct usb_serial_driver moschip7840_4port_device
= {
2796 .owner
= THIS_MODULE
,
2799 .description
= DRIVER_DESC
,
2800 .usb_driver
= &io_driver
,
2801 .id_table
= moschip_port_id_table
,
2803 .open
= mos7840_open
,
2804 .close
= mos7840_close
,
2805 .write
= mos7840_write
,
2806 .write_room
= mos7840_write_room
,
2807 .chars_in_buffer
= mos7840_chars_in_buffer
,
2808 .throttle
= mos7840_throttle
,
2809 .unthrottle
= mos7840_unthrottle
,
2810 .calc_num_ports
= mos7840_calc_num_ports
,
2811 #ifdef MCSSerialProbe
2812 .probe
= mos7840_serial_probe
,
2814 .ioctl
= mos7840_ioctl
,
2815 .set_termios
= mos7840_set_termios
,
2816 .break_ctl
= mos7840_break
,
2817 .tiocmget
= mos7840_tiocmget
,
2818 .tiocmset
= mos7840_tiocmset
,
2819 .attach
= mos7840_startup
,
2820 .shutdown
= mos7840_shutdown
,
2821 .read_bulk_callback
= mos7840_bulk_in_callback
,
2822 .read_int_callback
= mos7840_interrupt_callback
,
2825 /****************************************************************************
2827 * This is called by the module subsystem, or on startup to initialize us
2828 ****************************************************************************/
2829 static int __init
moschip7840_init(void)
2833 dbg("%s \n", " mos7840_init :entering..........");
2835 /* Register with the usb serial */
2836 retval
= usb_serial_register(&moschip7840_4port_device
);
2839 goto failed_port_device_register
;
2841 dbg("%s\n", "Entring...");
2842 info(DRIVER_DESC
" " DRIVER_VERSION
);
2844 /* Register with the usb */
2845 retval
= usb_register(&io_driver
);
2848 goto failed_usb_register
;
2851 dbg("%s\n", "Leaving...");
2855 failed_usb_register
:
2856 usb_serial_deregister(&moschip7840_4port_device
);
2858 failed_port_device_register
:
2863 /****************************************************************************
2865 * Called when the driver is about to be unloaded.
2866 ****************************************************************************/
2867 static void __exit
moschip7840_exit(void)
2870 dbg("%s \n", " mos7840_exit :entering..........");
2872 usb_deregister(&io_driver
);
2874 usb_serial_deregister(&moschip7840_4port_device
);
2876 dbg("%s\n", "Entring...");
2879 module_init(moschip7840_init
);
2880 module_exit(moschip7840_exit
);
2882 /* Module information */
2883 MODULE_DESCRIPTION(DRIVER_DESC
);
2884 MODULE_LICENSE("GPL");
2886 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
2887 MODULE_PARM_DESC(debug
, "Debug enabled or not");