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/smp_lock.h>
30 #include <linux/tty.h>
31 #include <linux/tty_driver.h>
32 #include <linux/tty_flip.h>
33 #include <linux/module.h>
34 #include <linux/serial.h>
35 #include <linux/usb.h>
36 #include <linux/usb/serial.h>
37 #include <linux/uaccess.h>
42 #define DRIVER_VERSION "1.3.2"
43 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
46 * 16C50 UART register defines
49 #define LCR_BITS_5 0x00 /* 5 bits/char */
50 #define LCR_BITS_6 0x01 /* 6 bits/char */
51 #define LCR_BITS_7 0x02 /* 7 bits/char */
52 #define LCR_BITS_8 0x03 /* 8 bits/char */
53 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
55 #define LCR_STOP_1 0x00 /* 1 stop bit */
56 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
57 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
58 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
60 #define LCR_PAR_NONE 0x00 /* No parity */
61 #define LCR_PAR_ODD 0x08 /* Odd parity */
62 #define LCR_PAR_EVEN 0x18 /* Even parity */
63 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
64 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
65 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
67 #define LCR_SET_BREAK 0x40 /* Set Break condition */
68 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
70 #define MCR_DTR 0x01 /* Assert DTR */
71 #define MCR_RTS 0x02 /* Assert RTS */
72 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
73 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
74 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
75 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
77 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
78 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
79 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
80 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
83 * Defines used for sending commands to port
86 #define WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
87 #define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
89 #define MOS_PORT1 0x0200
90 #define MOS_PORT2 0x0300
91 #define MOS_VENREG 0x0000
92 #define MOS_MAX_PORT 0x02
93 #define MOS_WRITE 0x0E
97 #define MCS_RD_RTYPE 0xC0
98 #define MCS_WR_RTYPE 0x40
99 #define MCS_RDREQ 0x0D
100 #define MCS_WRREQ 0x0E
101 #define MCS_CTRL_TIMEOUT 500
102 #define VENDOR_READ_LENGTH (0x01)
104 #define MAX_NAME_LEN 64
106 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
107 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
109 /* For higher baud Rates use TIOCEXBAUD */
110 #define TIOCEXBAUD 0x5462
112 /* vendor id and device id defines */
114 /* The native mos7840/7820 component */
115 #define USB_VENDOR_ID_MOSCHIP 0x9710
116 #define MOSCHIP_DEVICE_ID_7840 0x7840
117 #define MOSCHIP_DEVICE_ID_7820 0x7820
118 /* The native component can have its vendor/device id's overridden
119 * in vendor-specific implementations. Such devices can be handled
120 * by making a change here, in moschip_port_id_table, and in
121 * moschip_id_table_combined
123 #define USB_VENDOR_ID_BANDB 0x0856
124 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
125 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
127 /* This driver also supports
128 * ATEN UC2324 device using Moschip MCS7840
129 * ATEN UC2322 device using Moschip MCS7820
131 #define USB_VENDOR_ID_ATENINTL 0x0557
132 #define ATENINTL_DEVICE_ID_UC2324 0x2011
133 #define ATENINTL_DEVICE_ID_UC2322 0x7820
135 /* Interrupt Routine Defines */
137 #define SERIAL_IIR_RLS 0x06
138 #define SERIAL_IIR_MS 0x00
141 * Emulation of the bit mask on the LINE STATUS REGISTER.
143 #define SERIAL_LSR_DR 0x0001
144 #define SERIAL_LSR_OE 0x0002
145 #define SERIAL_LSR_PE 0x0004
146 #define SERIAL_LSR_FE 0x0008
147 #define SERIAL_LSR_BI 0x0010
149 #define MOS_MSR_DELTA_CTS 0x10
150 #define MOS_MSR_DELTA_DSR 0x20
151 #define MOS_MSR_DELTA_RI 0x40
152 #define MOS_MSR_DELTA_CD 0x80
154 /* Serial Port register Address */
155 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
156 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
157 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
158 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
159 #define LINE_STATUS_REGISTER ((__u16)(0x05))
160 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
161 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
162 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
163 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
165 #define CLK_MULTI_REGISTER ((__u16)(0x02))
166 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
168 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
171 * URB POOL related defines
173 #define NUM_URBS 16 /* URB Count */
174 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
177 static struct usb_device_id moschip_port_id_table
[] = {
178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
179 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
180 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
181 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
182 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2324
)},
183 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2322
)},
184 {} /* terminating entry */
187 static __devinitdata
struct usb_device_id moschip_id_table_combined
[] = {
188 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7840
)},
189 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP
, MOSCHIP_DEVICE_ID_7820
)},
190 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_4
)},
191 {USB_DEVICE(USB_VENDOR_ID_BANDB
, BANDB_DEVICE_ID_USOPTL4_2
)},
192 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2324
)},
193 {USB_DEVICE(USB_VENDOR_ID_ATENINTL
, ATENINTL_DEVICE_ID_UC2322
)},
194 {} /* terminating entry */
197 MODULE_DEVICE_TABLE(usb
, moschip_id_table_combined
);
199 /* This structure holds all of the local port information */
201 struct moschip_port
{
202 int port_num
; /*Actual port number in the device(1,2,etc) */
203 struct urb
*write_urb
; /* write URB for this port */
204 struct urb
*read_urb
; /* read URB for this port */
206 __u8 shadowLCR
; /* last LCR value received */
207 __u8 shadowMCR
; /* last MCR value received */
211 wait_queue_head_t wait_chase
; /* for handling sleeping while waiting for chase to finish */
212 wait_queue_head_t delta_msr_wait
; /* for handling sleeping while waiting for msr change to happen */
214 struct async_icount icount
;
215 struct usb_serial_port
*port
; /* loop back to the owner of this object */
219 __u8 ControlRegOffset
;
221 /* for processing control URBS in interrupt context */
222 struct urb
*control_urb
;
223 struct usb_ctrlrequest
*dr
;
227 spinlock_t pool_lock
;
228 struct urb
*write_urb_pool
[NUM_URBS
];
237 * mos7840_set_reg_sync
238 * To set the Control register by calling usb_fill_control_urb function
239 * by passing usb_sndctrlpipe function as parameter.
242 static int mos7840_set_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
245 struct usb_device
*dev
= port
->serial
->dev
;
247 dbg("mos7840_set_reg_sync offset is %x, value %x", reg
, val
);
249 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
250 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
255 * mos7840_get_reg_sync
256 * To set the Uart register by calling usb_fill_control_urb function by
257 * passing usb_rcvctrlpipe function as parameter.
260 static int mos7840_get_reg_sync(struct usb_serial_port
*port
, __u16 reg
,
263 struct usb_device
*dev
= port
->serial
->dev
;
266 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
267 MCS_RD_RTYPE
, 0, reg
, val
, VENDOR_READ_LENGTH
,
269 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg
, *val
);
270 *val
= (*val
) & 0x00ff;
275 * mos7840_set_uart_reg
276 * To set the Uart register by calling usb_fill_control_urb function by
277 * passing usb_sndctrlpipe function as parameter.
280 static int mos7840_set_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
284 struct usb_device
*dev
= port
->serial
->dev
;
286 /* For the UART control registers, the application number need
288 if (port
->serial
->num_ports
== 4) {
289 val
|= (((__u16
) port
->number
-
290 (__u16
) (port
->serial
->minor
)) + 1) << 8;
291 dbg("mos7840_set_uart_reg application number is %x", val
);
293 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
294 val
|= (((__u16
) port
->number
-
295 (__u16
) (port
->serial
->minor
)) + 1) << 8;
296 dbg("mos7840_set_uart_reg application number is %x",
300 (((__u16
) port
->number
-
301 (__u16
) (port
->serial
->minor
)) + 2) << 8;
302 dbg("mos7840_set_uart_reg application number is %x",
306 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0), MCS_WRREQ
,
307 MCS_WR_RTYPE
, val
, reg
, NULL
, 0,
313 * mos7840_get_uart_reg
314 * To set the Control register by calling usb_fill_control_urb function
315 * by passing usb_rcvctrlpipe function as parameter.
317 static int mos7840_get_uart_reg(struct usb_serial_port
*port
, __u16 reg
,
320 struct usb_device
*dev
= port
->serial
->dev
;
324 /* dbg("application number is %4x",
325 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
326 /* Wval is same as application number */
327 if (port
->serial
->num_ports
== 4) {
329 (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) +
331 dbg("mos7840_get_uart_reg application number is %x", Wval
);
333 if (((__u16
) port
->number
- (__u16
) (port
->serial
->minor
)) == 0) {
334 Wval
= (((__u16
) port
->number
-
335 (__u16
) (port
->serial
->minor
)) + 1) << 8;
336 dbg("mos7840_get_uart_reg application number is %x",
339 Wval
= (((__u16
) port
->number
-
340 (__u16
) (port
->serial
->minor
)) + 2) << 8;
341 dbg("mos7840_get_uart_reg application number is %x",
345 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0), MCS_RDREQ
,
346 MCS_RD_RTYPE
, Wval
, reg
, val
, VENDOR_READ_LENGTH
,
348 *val
= (*val
) & 0x00ff;
352 static void mos7840_dump_serial_port(struct moschip_port
*mos7840_port
)
355 dbg("***************************************");
356 dbg("SpRegOffset is %2x", mos7840_port
->SpRegOffset
);
357 dbg("ControlRegOffset is %2x", mos7840_port
->ControlRegOffset
);
358 dbg("DCRRegOffset is %2x", mos7840_port
->DcrRegOffset
);
359 dbg("***************************************");
363 /************************************************************************/
364 /************************************************************************/
365 /* I N T E R F A C E F U N C T I O N S */
366 /* I N T E R F A C E F U N C T I O N S */
367 /************************************************************************/
368 /************************************************************************/
370 static inline void mos7840_set_port_private(struct usb_serial_port
*port
,
371 struct moschip_port
*data
)
373 usb_set_serial_port_data(port
, (void *)data
);
376 static inline struct moschip_port
*mos7840_get_port_private(struct
380 return (struct moschip_port
*)usb_get_serial_port_data(port
);
383 static void mos7840_handle_new_msr(struct moschip_port
*port
, __u8 new_msr
)
385 struct moschip_port
*mos7840_port
;
386 struct async_icount
*icount
;
388 icount
= &mos7840_port
->icount
;
390 (MOS_MSR_DELTA_CTS
| MOS_MSR_DELTA_DSR
| MOS_MSR_DELTA_RI
|
392 icount
= &mos7840_port
->icount
;
394 /* update input line counters */
395 if (new_msr
& MOS_MSR_DELTA_CTS
) {
399 if (new_msr
& MOS_MSR_DELTA_DSR
) {
403 if (new_msr
& MOS_MSR_DELTA_CD
) {
407 if (new_msr
& MOS_MSR_DELTA_RI
) {
414 static void mos7840_handle_new_lsr(struct moschip_port
*port
, __u8 new_lsr
)
416 struct async_icount
*icount
;
418 dbg("%s - %02x", __func__
, new_lsr
);
420 if (new_lsr
& SERIAL_LSR_BI
) {
422 * Parity and Framing errors only count if they
423 * occur exclusive of a break being
426 new_lsr
&= (__u8
) (SERIAL_LSR_OE
| SERIAL_LSR_BI
);
429 /* update input line counters */
430 icount
= &port
->icount
;
431 if (new_lsr
& SERIAL_LSR_BI
) {
435 if (new_lsr
& SERIAL_LSR_OE
) {
439 if (new_lsr
& SERIAL_LSR_PE
) {
443 if (new_lsr
& SERIAL_LSR_FE
) {
449 /************************************************************************/
450 /************************************************************************/
451 /* U S B C A L L B A C K F U N C T I O N S */
452 /* U S B C A L L B A C K F U N C T I O N S */
453 /************************************************************************/
454 /************************************************************************/
456 static void mos7840_control_callback(struct urb
*urb
)
459 struct moschip_port
*mos7840_port
;
462 int status
= urb
->status
;
464 mos7840_port
= urb
->context
;
473 /* this urb is terminated, clean up */
474 dbg("%s - urb shutting down with status: %d", __func__
,
478 dbg("%s - nonzero urb status received: %d", __func__
,
483 dbg("%s urb buffer size is %d", __func__
, urb
->actual_length
);
484 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__
,
485 mos7840_port
->MsrLsr
, mos7840_port
->port_num
);
486 data
= urb
->transfer_buffer
;
487 regval
= (__u8
) data
[0];
488 dbg("%s data is %x", __func__
, regval
);
489 if (mos7840_port
->MsrLsr
== 0)
490 mos7840_handle_new_msr(mos7840_port
, regval
);
491 else if (mos7840_port
->MsrLsr
== 1)
492 mos7840_handle_new_lsr(mos7840_port
, regval
);
495 spin_lock(&mos7840_port
->pool_lock
);
496 if (!mos7840_port
->zombie
)
497 result
= usb_submit_urb(mos7840_port
->int_urb
, GFP_ATOMIC
);
498 spin_unlock(&mos7840_port
->pool_lock
);
500 dev_err(&urb
->dev
->dev
,
501 "%s - Error %d submitting interrupt urb\n",
506 static int mos7840_get_reg(struct moschip_port
*mcs
, __u16 Wval
, __u16 reg
,
509 struct usb_device
*dev
= mcs
->port
->serial
->dev
;
510 struct usb_ctrlrequest
*dr
= mcs
->dr
;
511 unsigned char *buffer
= mcs
->ctrl_buf
;
514 dr
->bRequestType
= MCS_RD_RTYPE
;
515 dr
->bRequest
= MCS_RDREQ
;
516 dr
->wValue
= cpu_to_le16(Wval
); /* 0 */
517 dr
->wIndex
= cpu_to_le16(reg
);
518 dr
->wLength
= cpu_to_le16(2);
520 usb_fill_control_urb(mcs
->control_urb
, dev
, usb_rcvctrlpipe(dev
, 0),
521 (unsigned char *)dr
, buffer
, 2,
522 mos7840_control_callback
, mcs
);
523 mcs
->control_urb
->transfer_buffer_length
= 2;
524 ret
= usb_submit_urb(mcs
->control_urb
, GFP_ATOMIC
);
528 /*****************************************************************************
529 * mos7840_interrupt_callback
530 * this is the callback function for when we have received data on the
531 * interrupt endpoint.
532 *****************************************************************************/
534 static void mos7840_interrupt_callback(struct urb
*urb
)
538 struct moschip_port
*mos7840_port
;
539 struct usb_serial
*serial
;
544 __u16 wval
, wreg
= 0;
545 int status
= urb
->status
;
547 dbg("%s", " : Entering");
556 /* this urb is terminated, clean up */
557 dbg("%s - urb shutting down with status: %d", __func__
,
561 dbg("%s - nonzero urb status received: %d", __func__
,
566 length
= urb
->actual_length
;
567 data
= urb
->transfer_buffer
;
569 serial
= urb
->context
;
571 /* Moschip get 5 bytes
572 * Byte 1 IIR Port 1 (port.number is 0)
573 * Byte 2 IIR Port 2 (port.number is 1)
574 * Byte 3 IIR Port 3 (port.number is 2)
575 * Byte 4 IIR Port 4 (port.number is 3)
576 * Byte 5 FIFO status for both */
578 if (length
&& length
> 5) {
579 dbg("%s", "Wrong data !!!");
583 sp
[0] = (__u8
) data
[0];
584 sp
[1] = (__u8
) data
[1];
585 sp
[2] = (__u8
) data
[2];
586 sp
[3] = (__u8
) data
[3];
589 for (i
= 0; i
< serial
->num_ports
; i
++) {
590 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
592 (((__u16
) serial
->port
[i
]->number
-
593 (__u16
) (serial
->minor
)) + 1) << 8;
594 if (mos7840_port
->open
) {
596 dbg("SP%d No Interrupt !!!", i
);
598 switch (sp
[i
] & 0x0f) {
600 dbg("Serial Port %d: Receiver status error or ", i
);
601 dbg("address bit detected in 9-bit mode");
602 mos7840_port
->MsrLsr
= 1;
603 wreg
= LINE_STATUS_REGISTER
;
606 dbg("Serial Port %d: Modem status change", i
);
607 mos7840_port
->MsrLsr
= 0;
608 wreg
= MODEM_STATUS_REGISTER
;
611 spin_lock(&mos7840_port
->pool_lock
);
612 if (!mos7840_port
->zombie
) {
613 rv
= mos7840_get_reg(mos7840_port
, wval
, wreg
, &Data
);
615 spin_unlock(&mos7840_port
->pool_lock
);
618 spin_unlock(&mos7840_port
->pool_lock
);
623 /* the completion handler for the control urb will resubmit */
626 result
= usb_submit_urb(urb
, GFP_ATOMIC
);
628 dev_err(&urb
->dev
->dev
,
629 "%s - Error %d submitting interrupt urb\n",
634 static int mos7840_port_paranoia_check(struct usb_serial_port
*port
,
635 const char *function
)
638 dbg("%s - port == NULL", function
);
642 dbg("%s - port->serial == NULL", function
);
649 /* Inline functions to check the sanity of a pointer that is passed to us */
650 static int mos7840_serial_paranoia_check(struct usb_serial
*serial
,
651 const char *function
)
654 dbg("%s - serial == NULL", function
);
658 dbg("%s - serial->type == NULL!", function
);
665 static struct usb_serial
*mos7840_get_usb_serial(struct usb_serial_port
*port
,
666 const char *function
)
668 /* if no port was specified, or it fails a paranoia check */
670 mos7840_port_paranoia_check(port
, function
) ||
671 mos7840_serial_paranoia_check(port
->serial
, function
)) {
672 /* then say that we don't have a valid usb_serial thing,
673 * which will end up genrating -ENODEV return values */
680 /*****************************************************************************
681 * mos7840_bulk_in_callback
682 * this is the callback function for when we have received data on the
684 *****************************************************************************/
686 static void mos7840_bulk_in_callback(struct urb
*urb
)
690 struct usb_serial
*serial
;
691 struct usb_serial_port
*port
;
692 struct moschip_port
*mos7840_port
;
693 struct tty_struct
*tty
;
694 int status
= urb
->status
;
696 mos7840_port
= urb
->context
;
698 dbg("%s", "NULL mos7840_port pointer");
699 mos7840_port
->read_urb_busy
= false;
704 dbg("nonzero read bulk status received: %d", status
);
705 mos7840_port
->read_urb_busy
= false;
709 port
= (struct usb_serial_port
*)mos7840_port
->port
;
710 if (mos7840_port_paranoia_check(port
, __func__
)) {
711 dbg("%s", "Port Paranoia failed");
712 mos7840_port
->read_urb_busy
= false;
716 serial
= mos7840_get_usb_serial(port
, __func__
);
718 dbg("%s", "Bad serial pointer");
719 mos7840_port
->read_urb_busy
= false;
723 dbg("%s", "Entering... ");
725 data
= urb
->transfer_buffer
;
727 dbg("%s", "Entering ...........");
729 if (urb
->actual_length
) {
730 tty
= tty_port_tty_get(&mos7840_port
->port
->port
);
732 tty_buffer_request_room(tty
, urb
->actual_length
);
733 tty_insert_flip_string(tty
, data
, urb
->actual_length
);
735 tty_flip_buffer_push(tty
);
738 mos7840_port
->icount
.rx
+= urb
->actual_length
;
740 dbg("mos7840_port->icount.rx is %d:",
741 mos7840_port
->icount
.rx
);
744 if (!mos7840_port
->read_urb
) {
745 dbg("%s", "URB KILLED !!!");
746 mos7840_port
->read_urb_busy
= false;
751 mos7840_port
->read_urb
->dev
= serial
->dev
;
753 mos7840_port
->read_urb_busy
= true;
754 retval
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
757 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval
);
758 mos7840_port
->read_urb_busy
= false;
762 /*****************************************************************************
763 * mos7840_bulk_out_data_callback
764 * this is the callback function for when we have finished sending
765 * serial data on the bulk out endpoint.
766 *****************************************************************************/
768 static void mos7840_bulk_out_data_callback(struct urb
*urb
)
770 struct moschip_port
*mos7840_port
;
771 struct tty_struct
*tty
;
772 int status
= urb
->status
;
775 mos7840_port
= urb
->context
;
776 spin_lock(&mos7840_port
->pool_lock
);
777 for (i
= 0; i
< NUM_URBS
; i
++) {
778 if (urb
== mos7840_port
->write_urb_pool
[i
]) {
779 mos7840_port
->busy
[i
] = 0;
783 spin_unlock(&mos7840_port
->pool_lock
);
786 dbg("nonzero write bulk status received:%d", status
);
790 if (mos7840_port_paranoia_check(mos7840_port
->port
, __func__
)) {
791 dbg("%s", "Port Paranoia failed");
795 dbg("%s", "Entering .........");
797 tty
= tty_port_tty_get(&mos7840_port
->port
->port
);
798 if (tty
&& mos7840_port
->open
)
804 /************************************************************************/
805 /* 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 */
806 /************************************************************************/
807 #ifdef MCSSerialProbe
808 static int mos7840_serial_probe(struct usb_serial
*serial
,
809 const struct usb_device_id
*id
)
812 /*need to implement the mode_reg reading and updating\
813 structures usb_serial_ device_type\
814 (i.e num_ports, num_bulkin,bulkout etc) */
815 /* Also we can update the changes attach */
820 /*****************************************************************************
822 * this function is called by the tty driver when a port is opened
823 * If successful, we return 0
824 * Otherwise we return a negative error number.
825 *****************************************************************************/
827 static int mos7840_open(struct tty_struct
*tty
,
828 struct usb_serial_port
*port
, struct file
*filp
)
832 struct usb_serial
*serial
;
836 struct moschip_port
*mos7840_port
;
837 struct moschip_port
*port0
;
839 dbg ("%s enter", __func__
);
841 if (mos7840_port_paranoia_check(port
, __func__
)) {
842 dbg("%s", "Port Paranoia failed");
846 serial
= port
->serial
;
848 if (mos7840_serial_paranoia_check(serial
, __func__
)) {
849 dbg("%s", "Serial Paranoia failed");
853 mos7840_port
= mos7840_get_port_private(port
);
854 port0
= mos7840_get_port_private(serial
->port
[0]);
856 if (mos7840_port
== NULL
|| port0
== NULL
)
859 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
860 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
863 /* Initialising the write urb pool */
864 for (j
= 0; j
< NUM_URBS
; ++j
) {
865 urb
= usb_alloc_urb(0, GFP_KERNEL
);
866 mos7840_port
->write_urb_pool
[j
] = urb
;
869 dev_err(&port
->dev
, "No more urbs???\n");
873 urb
->transfer_buffer
= kmalloc(URB_TRANSFER_BUFFER_SIZE
,
875 if (!urb
->transfer_buffer
) {
877 mos7840_port
->write_urb_pool
[j
] = NULL
;
879 "%s-out of memory for urb buffers.\n",
885 /*****************************************************************************
886 * Initialize MCS7840 -- Write Init values to corresponding Registers
894 * 0x08 : SP1/2 Control Reg
895 *****************************************************************************/
897 /* NEED to check the following Block */
900 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
902 dbg("Reading Spreg failed");
906 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
908 dbg("writing Spreg failed");
913 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
915 dbg("writing Spreg failed");
918 /* End of block to be checked */
921 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
924 dbg("Reading Controlreg failed");
927 Data
|= 0x08; /* Driver done bit */
928 Data
|= 0x20; /* rx_disable */
929 status
= mos7840_set_reg_sync(port
,
930 mos7840_port
->ControlRegOffset
, Data
);
932 dbg("writing Controlreg failed");
935 /* do register settings here */
936 /* Set all regs to the device default values. */
937 /***********************************
938 * First Disable all interrupts.
939 ***********************************/
941 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
943 dbg("disabling interrupts failed");
946 /* Set FIFO_CONTROL_REGISTER to the default value */
948 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
950 dbg("Writing FIFO_CONTROL_REGISTER failed");
955 status
= mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
957 dbg("Writing FIFO_CONTROL_REGISTER failed");
962 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
963 mos7840_port
->shadowLCR
= Data
;
966 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
967 mos7840_port
->shadowMCR
= Data
;
970 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
971 mos7840_port
->shadowLCR
= Data
;
973 Data
|= SERIAL_LCR_DLAB
; /* data latch enable in LCR 0x80 */
974 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
977 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
980 status
= mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
983 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
985 Data
= Data
& ~SERIAL_LCR_DLAB
;
986 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
987 mos7840_port
->shadowLCR
= Data
;
989 /* clearing Bulkin and Bulkout Fifo */
991 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
, &Data
);
994 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
997 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
, Data
);
998 /* Finally enable all interrupts */
1000 status
= mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1002 /* clearing rx_disable */
1004 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1006 Data
= Data
& ~0x20;
1007 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1012 status
= mos7840_get_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1015 status
= mos7840_set_reg_sync(port
, mos7840_port
->ControlRegOffset
,
1018 /* Check to see if we've set up our endpoint info yet *
1019 * (can't set it up in mos7840_startup as the structures *
1020 * were not set up at that time.) */
1021 if (port0
->open_ports
== 1) {
1022 if (serial
->port
[0]->interrupt_in_buffer
== NULL
) {
1023 /* set up interrupt urb */
1024 usb_fill_int_urb(serial
->port
[0]->interrupt_in_urb
,
1026 usb_rcvintpipe(serial
->dev
,
1027 serial
->port
[0]->interrupt_in_endpointAddress
),
1028 serial
->port
[0]->interrupt_in_buffer
,
1029 serial
->port
[0]->interrupt_in_urb
->
1030 transfer_buffer_length
,
1031 mos7840_interrupt_callback
,
1033 serial
->port
[0]->interrupt_in_urb
->interval
);
1035 /* start interrupt read for mos7840 *
1036 * will continue as long as mos7840 is connected */
1039 usb_submit_urb(serial
->port
[0]->interrupt_in_urb
,
1042 dev_err(&port
->dev
, "%s - Error %d submitting "
1043 "interrupt urb\n", __func__
, response
);
1050 /* see if we've set up our endpoint info yet *
1051 * (can't set it up in mos7840_startup as the *
1052 * structures were not set up at that time.) */
1054 dbg("port number is %d", port
->number
);
1055 dbg("serial number is %d", port
->serial
->minor
);
1056 dbg("Bulkin endpoint is %d", port
->bulk_in_endpointAddress
);
1057 dbg("BulkOut endpoint is %d", port
->bulk_out_endpointAddress
);
1058 dbg("Interrupt endpoint is %d", port
->interrupt_in_endpointAddress
);
1059 dbg("port's number in the device is %d", mos7840_port
->port_num
);
1060 mos7840_port
->read_urb
= port
->read_urb
;
1062 /* set up our bulk in urb */
1064 usb_fill_bulk_urb(mos7840_port
->read_urb
,
1066 usb_rcvbulkpipe(serial
->dev
,
1067 port
->bulk_in_endpointAddress
),
1068 port
->bulk_in_buffer
,
1069 mos7840_port
->read_urb
->transfer_buffer_length
,
1070 mos7840_bulk_in_callback
, mos7840_port
);
1072 dbg("mos7840_open: bulkin endpoint is %d",
1073 port
->bulk_in_endpointAddress
);
1074 mos7840_port
->read_urb_busy
= true;
1075 response
= usb_submit_urb(mos7840_port
->read_urb
, GFP_KERNEL
);
1077 dev_err(&port
->dev
, "%s - Error %d submitting control urb\n",
1078 __func__
, response
);
1079 mos7840_port
->read_urb_busy
= false;
1082 /* initialize our wait queues */
1083 init_waitqueue_head(&mos7840_port
->wait_chase
);
1084 init_waitqueue_head(&mos7840_port
->delta_msr_wait
);
1086 /* initialize our icount structure */
1087 memset(&(mos7840_port
->icount
), 0x00, sizeof(mos7840_port
->icount
));
1089 /* initialize our port settings */
1090 /* Must set to enable ints! */
1091 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
1092 /* send a open port command */
1093 mos7840_port
->open
= 1;
1094 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1095 mos7840_port
->icount
.tx
= 0;
1096 mos7840_port
->icount
.rx
= 0;
1098 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
1099 serial
, mos7840_port
, port
);
1101 dbg ("%s leave", __func__
);
1107 /*****************************************************************************
1108 * mos7840_chars_in_buffer
1109 * this function is called by the tty driver when it wants to know how many
1110 * bytes of data we currently have outstanding in the port (data that has
1111 * been written, but hasn't made it out the port yet)
1112 * If successful, we return the number of bytes left to be written in the
1114 * Otherwise we return zero.
1115 *****************************************************************************/
1117 static int mos7840_chars_in_buffer(struct tty_struct
*tty
)
1119 struct usb_serial_port
*port
= tty
->driver_data
;
1122 unsigned long flags
;
1123 struct moschip_port
*mos7840_port
;
1125 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
1127 if (mos7840_port_paranoia_check(port
, __func__
)) {
1128 dbg("%s", "Invalid port");
1132 mos7840_port
= mos7840_get_port_private(port
);
1133 if (mos7840_port
== NULL
) {
1134 dbg("%s", "mos7840_break:leaving ...........");
1138 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1139 for (i
= 0; i
< NUM_URBS
; ++i
)
1140 if (mos7840_port
->busy
[i
])
1141 chars
+= URB_TRANSFER_BUFFER_SIZE
;
1142 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1143 dbg("%s - returns %d", __func__
, chars
);
1148 /*****************************************************************************
1150 * this function is called by the tty driver when a port is closed
1151 *****************************************************************************/
1153 static void mos7840_close(struct usb_serial_port
*port
)
1155 struct usb_serial
*serial
;
1156 struct moschip_port
*mos7840_port
;
1157 struct moschip_port
*port0
;
1161 dbg("%s", "mos7840_close:entering...");
1163 if (mos7840_port_paranoia_check(port
, __func__
)) {
1164 dbg("%s", "Port Paranoia failed");
1168 serial
= mos7840_get_usb_serial(port
, __func__
);
1170 dbg("%s", "Serial Paranoia failed");
1174 mos7840_port
= mos7840_get_port_private(port
);
1175 port0
= mos7840_get_port_private(serial
->port
[0]);
1177 if (mos7840_port
== NULL
|| port0
== NULL
)
1180 for (j
= 0; j
< NUM_URBS
; ++j
)
1181 usb_kill_urb(mos7840_port
->write_urb_pool
[j
]);
1183 /* Freeing Write URBs */
1184 for (j
= 0; j
< NUM_URBS
; ++j
) {
1185 if (mos7840_port
->write_urb_pool
[j
]) {
1186 if (mos7840_port
->write_urb_pool
[j
]->transfer_buffer
)
1187 kfree(mos7840_port
->write_urb_pool
[j
]->
1190 usb_free_urb(mos7840_port
->write_urb_pool
[j
]);
1194 /* While closing port, shutdown all bulk read, write *
1195 * and interrupt read if they exists */
1197 if (mos7840_port
->write_urb
) {
1198 dbg("%s", "Shutdown bulk write");
1199 usb_kill_urb(mos7840_port
->write_urb
);
1201 if (mos7840_port
->read_urb
) {
1202 dbg("%s", "Shutdown bulk read");
1203 usb_kill_urb(mos7840_port
->read_urb
);
1204 mos7840_port
->read_urb_busy
= false;
1206 if ((&mos7840_port
->control_urb
)) {
1207 dbg("%s", "Shutdown control read");
1208 /*/ usb_kill_urb (mos7840_port->control_urb); */
1211 /* if(mos7840_port->ctrl_buf != NULL) */
1212 /* kfree(mos7840_port->ctrl_buf); */
1213 port0
->open_ports
--;
1214 dbg("mos7840_num_open_ports in close%d:in port%d",
1215 port0
->open_ports
, port
->number
);
1216 if (port0
->open_ports
== 0) {
1217 if (serial
->port
[0]->interrupt_in_urb
) {
1218 dbg("%s", "Shutdown interrupt_in_urb");
1219 usb_kill_urb(serial
->port
[0]->interrupt_in_urb
);
1223 if (mos7840_port
->write_urb
) {
1224 /* if this urb had a transfer buffer already (old tx) free it */
1225 if (mos7840_port
->write_urb
->transfer_buffer
!= NULL
)
1226 kfree(mos7840_port
->write_urb
->transfer_buffer
);
1227 usb_free_urb(mos7840_port
->write_urb
);
1231 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1234 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1236 mos7840_port
->open
= 0;
1238 dbg("%s", "Leaving ............");
1241 /************************************************************************
1243 * mos7840_block_until_chase_response
1245 * This function will block the close until one of the following:
1246 * 1. Response to our Chase comes from mos7840
1247 * 2. A timeout of 10 seconds without activity has expired
1248 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1250 ************************************************************************/
1252 static void mos7840_block_until_chase_response(struct tty_struct
*tty
,
1253 struct moschip_port
*mos7840_port
)
1255 int timeout
= 1 * HZ
;
1260 count
= mos7840_chars_in_buffer(tty
);
1262 /* Check for Buffer status */
1266 /* Block the thread for a while */
1267 interruptible_sleep_on_timeout(&mos7840_port
->wait_chase
,
1269 /* No activity.. count down section */
1272 dbg("%s - TIMEOUT", __func__
);
1275 /* Reset timeout value back to seconds */
1282 /*****************************************************************************
1284 * this function sends a break to the port
1285 *****************************************************************************/
1286 static void mos7840_break(struct tty_struct
*tty
, int break_state
)
1288 struct usb_serial_port
*port
= tty
->driver_data
;
1290 struct usb_serial
*serial
;
1291 struct moschip_port
*mos7840_port
;
1293 dbg("%s", "Entering ...........");
1294 dbg("mos7840_break: Start");
1296 if (mos7840_port_paranoia_check(port
, __func__
)) {
1297 dbg("%s", "Port Paranoia failed");
1301 serial
= mos7840_get_usb_serial(port
, __func__
);
1303 dbg("%s", "Serial Paranoia failed");
1307 mos7840_port
= mos7840_get_port_private(port
);
1309 if (mos7840_port
== NULL
)
1313 /* flush and block until tx is empty */
1314 mos7840_block_until_chase_response(tty
, mos7840_port
);
1316 if (break_state
== -1)
1317 data
= mos7840_port
->shadowLCR
| LCR_SET_BREAK
;
1319 data
= mos7840_port
->shadowLCR
& ~LCR_SET_BREAK
;
1321 /* FIXME: no locking on shadowLCR anywhere in driver */
1322 mos7840_port
->shadowLCR
= data
;
1323 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
1324 mos7840_port
->shadowLCR
);
1325 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
,
1326 mos7840_port
->shadowLCR
);
1331 /*****************************************************************************
1332 * mos7840_write_room
1333 * this function is called by the tty driver when it wants to know how many
1334 * bytes of data we can accept for a specific port.
1335 * If successful, we return the amount of room that we have for this port
1336 * Otherwise we return a negative error number.
1337 *****************************************************************************/
1339 static int mos7840_write_room(struct tty_struct
*tty
)
1341 struct usb_serial_port
*port
= tty
->driver_data
;
1344 unsigned long flags
;
1345 struct moschip_port
*mos7840_port
;
1347 dbg("%s", " mos7840_write_room:entering ...........");
1349 if (mos7840_port_paranoia_check(port
, __func__
)) {
1350 dbg("%s", "Invalid port");
1351 dbg("%s", " mos7840_write_room:leaving ...........");
1355 mos7840_port
= mos7840_get_port_private(port
);
1356 if (mos7840_port
== NULL
) {
1357 dbg("%s", "mos7840_break:leaving ...........");
1361 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1362 for (i
= 0; i
< NUM_URBS
; ++i
) {
1363 if (!mos7840_port
->busy
[i
])
1364 room
+= URB_TRANSFER_BUFFER_SIZE
;
1366 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1368 room
= (room
== 0) ? 0 : room
- URB_TRANSFER_BUFFER_SIZE
+ 1;
1369 dbg("%s - returns %d", __func__
, room
);
1374 /*****************************************************************************
1376 * this function is called by the tty driver when data should be written to
1378 * If successful, we return the number of bytes written, otherwise we
1379 * return a negative error number.
1380 *****************************************************************************/
1382 static int mos7840_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1383 const unsigned char *data
, int count
)
1389 unsigned long flags
;
1391 struct moschip_port
*mos7840_port
;
1392 struct usb_serial
*serial
;
1395 const unsigned char *current_position
= data
;
1396 unsigned char *data1
;
1397 dbg("%s", "entering ...........");
1398 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1399 mos7840_port->shadowLCR); */
1403 status
= mos7840_get_uart_reg(port
, LINE_CONTROL_REGISTER
, &Data
);
1404 mos7840_port
->shadowLCR
= Data
;
1405 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data
);
1406 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1407 mos7840_port
->shadowLCR
);
1410 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1411 /* mos7840_port->shadowLCR=Data;//Need to add later */
1413 Data
|= SERIAL_LCR_DLAB
; /* data latch enable in LCR 0x80 */
1414 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1417 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1419 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_LSB
, &Data
);
1420 dbg("mos7840_write:DLL value is %x", Data
);
1423 status
= mos7840_get_uart_reg(port
, DIVISOR_LATCH_MSB
, &Data
);
1424 dbg("mos7840_write:DLM value is %x", Data
);
1426 Data
= Data
& ~SERIAL_LCR_DLAB
;
1427 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1428 mos7840_port
->shadowLCR
);
1429 status
= mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1432 if (mos7840_port_paranoia_check(port
, __func__
)) {
1433 dbg("%s", "Port Paranoia failed");
1437 serial
= port
->serial
;
1438 if (mos7840_serial_paranoia_check(serial
, __func__
)) {
1439 dbg("%s", "Serial Paranoia failed");
1443 mos7840_port
= mos7840_get_port_private(port
);
1444 if (mos7840_port
== NULL
) {
1445 dbg("%s", "mos7840_port is NULL");
1449 /* try to find a free urb in the list */
1452 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
1453 for (i
= 0; i
< NUM_URBS
; ++i
) {
1454 if (!mos7840_port
->busy
[i
]) {
1455 mos7840_port
->busy
[i
] = 1;
1456 urb
= mos7840_port
->write_urb_pool
[i
];
1461 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
1464 dbg("%s - no more free urbs", __func__
);
1468 if (urb
->transfer_buffer
== NULL
) {
1469 urb
->transfer_buffer
=
1470 kmalloc(URB_TRANSFER_BUFFER_SIZE
, GFP_KERNEL
);
1472 if (urb
->transfer_buffer
== NULL
) {
1473 dev_err(&port
->dev
, "%s no more kernel memory...\n",
1478 transfer_size
= min(count
, URB_TRANSFER_BUFFER_SIZE
);
1480 memcpy(urb
->transfer_buffer
, current_position
, transfer_size
);
1482 /* fill urb with data and submit */
1483 usb_fill_bulk_urb(urb
,
1485 usb_sndbulkpipe(serial
->dev
,
1486 port
->bulk_out_endpointAddress
),
1487 urb
->transfer_buffer
,
1489 mos7840_bulk_out_data_callback
, mos7840_port
);
1491 data1
= urb
->transfer_buffer
;
1492 dbg("bulkout endpoint is %d", port
->bulk_out_endpointAddress
);
1494 /* send it down the pipe */
1495 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1498 mos7840_port
->busy
[i
] = 0;
1499 dev_err(&port
->dev
, "%s - usb_submit_urb(write bulk) failed "
1500 "with status = %d\n", __func__
, status
);
1501 bytes_sent
= status
;
1504 bytes_sent
= transfer_size
;
1505 mos7840_port
->icount
.tx
+= transfer_size
;
1507 dbg("mos7840_port->icount.tx is %d:", mos7840_port
->icount
.tx
);
1513 /*****************************************************************************
1515 * this function is called by the tty driver when it wants to stop the data
1516 * being read from the port.
1517 *****************************************************************************/
1519 static void mos7840_throttle(struct tty_struct
*tty
)
1521 struct usb_serial_port
*port
= tty
->driver_data
;
1522 struct moschip_port
*mos7840_port
;
1525 if (mos7840_port_paranoia_check(port
, __func__
)) {
1526 dbg("%s", "Invalid port");
1530 dbg("- port %d", port
->number
);
1532 mos7840_port
= mos7840_get_port_private(port
);
1534 if (mos7840_port
== NULL
)
1537 if (!mos7840_port
->open
) {
1538 dbg("%s", "port not opened");
1542 dbg("%s", "Entering ..........");
1544 /* if we are implementing XON/XOFF, send the stop character */
1546 unsigned char stop_char
= STOP_CHAR(tty
);
1547 status
= mos7840_write(tty
, port
, &stop_char
, 1);
1551 /* if we are implementing RTS/CTS, toggle that line */
1552 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1553 mos7840_port
->shadowMCR
&= ~MCR_RTS
;
1554 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1555 mos7840_port
->shadowMCR
);
1563 /*****************************************************************************
1564 * mos7840_unthrottle
1565 * this function is called by the tty driver when it wants to resume
1566 * the data being read from the port (called after mos7840_throttle is
1568 *****************************************************************************/
1569 static void mos7840_unthrottle(struct tty_struct
*tty
)
1571 struct usb_serial_port
*port
= tty
->driver_data
;
1573 struct moschip_port
*mos7840_port
= mos7840_get_port_private(port
);
1575 if (mos7840_port_paranoia_check(port
, __func__
)) {
1576 dbg("%s", "Invalid port");
1580 if (mos7840_port
== NULL
)
1583 if (!mos7840_port
->open
) {
1584 dbg("%s - port not opened", __func__
);
1588 dbg("%s", "Entering ..........");
1590 /* if we are implementing XON/XOFF, send the start character */
1592 unsigned char start_char
= START_CHAR(tty
);
1593 status
= mos7840_write(tty
, port
, &start_char
, 1);
1598 /* if we are implementing RTS/CTS, toggle that line */
1599 if (tty
->termios
->c_cflag
& CRTSCTS
) {
1600 mos7840_port
->shadowMCR
|= MCR_RTS
;
1601 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1602 mos7840_port
->shadowMCR
);
1608 static int mos7840_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1610 struct usb_serial_port
*port
= tty
->driver_data
;
1611 struct moschip_port
*mos7840_port
;
1612 unsigned int result
;
1616 mos7840_port
= mos7840_get_port_private(port
);
1618 dbg("%s - port %d", __func__
, port
->number
);
1620 if (mos7840_port
== NULL
)
1623 status
= mos7840_get_uart_reg(port
, MODEM_STATUS_REGISTER
, &msr
);
1624 status
= mos7840_get_uart_reg(port
, MODEM_CONTROL_REGISTER
, &mcr
);
1625 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0)
1626 | ((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0)
1627 | ((mcr
& MCR_LOOPBACK
) ? TIOCM_LOOP
: 0)
1628 | ((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0)
1629 | ((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0)
1630 | ((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0)
1631 | ((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0);
1633 dbg("%s - 0x%04X", __func__
, result
);
1638 static int mos7840_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1639 unsigned int set
, unsigned int clear
)
1641 struct usb_serial_port
*port
= tty
->driver_data
;
1642 struct moschip_port
*mos7840_port
;
1646 dbg("%s - port %d", __func__
, port
->number
);
1648 mos7840_port
= mos7840_get_port_private(port
);
1650 if (mos7840_port
== NULL
)
1653 /* FIXME: What locks the port registers ? */
1654 mcr
= mos7840_port
->shadowMCR
;
1655 if (clear
& TIOCM_RTS
)
1657 if (clear
& TIOCM_DTR
)
1659 if (clear
& TIOCM_LOOP
)
1660 mcr
&= ~MCR_LOOPBACK
;
1662 if (set
& TIOCM_RTS
)
1664 if (set
& TIOCM_DTR
)
1666 if (set
& TIOCM_LOOP
)
1667 mcr
|= MCR_LOOPBACK
;
1669 mos7840_port
->shadowMCR
= mcr
;
1671 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, mcr
);
1673 dbg("setting MODEM_CONTROL_REGISTER Failed");
1680 /*****************************************************************************
1681 * mos7840_calc_baud_rate_divisor
1682 * this function calculates the proper baud rate divisor for the specified
1684 *****************************************************************************/
1685 static int mos7840_calc_baud_rate_divisor(int baudRate
, int *divisor
,
1689 dbg("%s - %d", __func__
, baudRate
);
1691 if (baudRate
<= 115200) {
1692 *divisor
= 115200 / baudRate
;
1695 if ((baudRate
> 115200) && (baudRate
<= 230400)) {
1696 *divisor
= 230400 / baudRate
;
1697 *clk_sel_val
= 0x10;
1698 } else if ((baudRate
> 230400) && (baudRate
<= 403200)) {
1699 *divisor
= 403200 / baudRate
;
1700 *clk_sel_val
= 0x20;
1701 } else if ((baudRate
> 403200) && (baudRate
<= 460800)) {
1702 *divisor
= 460800 / baudRate
;
1703 *clk_sel_val
= 0x30;
1704 } else if ((baudRate
> 460800) && (baudRate
<= 806400)) {
1705 *divisor
= 806400 / baudRate
;
1706 *clk_sel_val
= 0x40;
1707 } else if ((baudRate
> 806400) && (baudRate
<= 921600)) {
1708 *divisor
= 921600 / baudRate
;
1709 *clk_sel_val
= 0x50;
1710 } else if ((baudRate
> 921600) && (baudRate
<= 1572864)) {
1711 *divisor
= 1572864 / baudRate
;
1712 *clk_sel_val
= 0x60;
1713 } else if ((baudRate
> 1572864) && (baudRate
<= 3145728)) {
1714 *divisor
= 3145728 / baudRate
;
1715 *clk_sel_val
= 0x70;
1721 for (i
= 0; i
< ARRAY_SIZE(mos7840_divisor_table
); i
++) {
1722 if (mos7840_divisor_table
[i
].BaudRate
== baudrate
) {
1723 *divisor
= mos7840_divisor_table
[i
].Divisor
;
1728 /* After trying for all the standard baud rates *
1729 * Try calculating the divisor for this baud rate */
1731 if (baudrate
> 75 && baudrate
< 230400) {
1732 /* get the divisor */
1733 custom
= (__u16
) (230400L / baudrate
);
1735 /* Check for round off */
1736 round1
= (__u16
) (2304000L / baudrate
);
1737 round
= (__u16
) (round1
- (custom
* 10));
1742 dbg(" Baud %d = %d", baudrate
, custom
);
1746 dbg("%s", " Baud calculation Failed...");
1751 /*****************************************************************************
1752 * mos7840_send_cmd_write_baud_rate
1753 * this function sends the proper command to change the baud rate of the
1755 *****************************************************************************/
1757 static int mos7840_send_cmd_write_baud_rate(struct moschip_port
*mos7840_port
,
1763 unsigned char number
;
1765 struct usb_serial_port
*port
;
1767 if (mos7840_port
== NULL
)
1770 port
= (struct usb_serial_port
*)mos7840_port
->port
;
1771 if (mos7840_port_paranoia_check(port
, __func__
)) {
1772 dbg("%s", "Invalid port");
1776 if (mos7840_serial_paranoia_check(port
->serial
, __func__
)) {
1777 dbg("%s", "Invalid Serial");
1781 dbg("%s", "Entering ..........");
1783 number
= mos7840_port
->port
->number
- mos7840_port
->port
->serial
->minor
;
1785 dbg("%s - port = %d, baud = %d", __func__
,
1786 mos7840_port
->port
->number
, baudRate
);
1787 /* reset clk_uart_sel in spregOffset */
1788 if (baudRate
> 115200) {
1789 #ifdef HW_flow_control
1790 /* NOTE: need to see the pther register to modify */
1791 /* setting h/w flow control bit to 1 */
1793 mos7840_port
->shadowMCR
= Data
;
1794 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1797 dbg("Writing spreg failed in set_serial_baud");
1803 #ifdef HW_flow_control
1804 / *setting h
/w flow control bit to
0 */
1806 mos7840_port
->shadowMCR
= Data
;
1807 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
,
1810 dbg("Writing spreg failed in set_serial_baud");
1817 if (1) { /* baudRate <= 115200) */
1820 status
= mos7840_calc_baud_rate_divisor(baudRate
, &divisor
,
1822 status
= mos7840_get_reg_sync(port
, mos7840_port
->SpRegOffset
,
1825 dbg("reading spreg failed in set_serial_baud");
1828 Data
= (Data
& 0x8f) | clk_sel_val
;
1829 status
= mos7840_set_reg_sync(port
, mos7840_port
->SpRegOffset
,
1832 dbg("Writing spreg failed in set_serial_baud");
1835 /* Calculate the Divisor */
1838 dev_err(&port
->dev
, "%s - bad baud rate\n", __func__
);
1841 /* Enable access to divisor latch */
1842 Data
= mos7840_port
->shadowLCR
| SERIAL_LCR_DLAB
;
1843 mos7840_port
->shadowLCR
= Data
;
1844 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1846 /* Write the divisor */
1847 Data
= (unsigned char)(divisor
& 0xff);
1848 dbg("set_serial_baud Value to write DLL is %x", Data
);
1849 mos7840_set_uart_reg(port
, DIVISOR_LATCH_LSB
, Data
);
1851 Data
= (unsigned char)((divisor
& 0xff00) >> 8);
1852 dbg("set_serial_baud Value to write DLM is %x", Data
);
1853 mos7840_set_uart_reg(port
, DIVISOR_LATCH_MSB
, Data
);
1855 /* Disable access to divisor latch */
1856 Data
= mos7840_port
->shadowLCR
& ~SERIAL_LCR_DLAB
;
1857 mos7840_port
->shadowLCR
= Data
;
1858 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1864 /*****************************************************************************
1865 * mos7840_change_port_settings
1866 * This routine is called to set the UART on the device to match
1867 * the specified new settings.
1868 *****************************************************************************/
1870 static void mos7840_change_port_settings(struct tty_struct
*tty
,
1871 struct moschip_port
*mos7840_port
, struct ktermios
*old_termios
)
1881 struct usb_serial_port
*port
;
1882 struct usb_serial
*serial
;
1884 if (mos7840_port
== NULL
)
1887 port
= (struct usb_serial_port
*)mos7840_port
->port
;
1889 if (mos7840_port_paranoia_check(port
, __func__
)) {
1890 dbg("%s", "Invalid port");
1894 if (mos7840_serial_paranoia_check(port
->serial
, __func__
)) {
1895 dbg("%s", "Invalid Serial");
1899 serial
= port
->serial
;
1901 dbg("%s - port %d", __func__
, mos7840_port
->port
->number
);
1903 if (!mos7840_port
->open
) {
1904 dbg("%s - port not opened", __func__
);
1908 dbg("%s", "Entering ..........");
1912 lParity
= LCR_PAR_NONE
;
1914 cflag
= tty
->termios
->c_cflag
;
1915 iflag
= tty
->termios
->c_iflag
;
1917 /* Change the number of bits */
1918 if (cflag
& CSIZE
) {
1919 switch (cflag
& CSIZE
) {
1937 /* Change the Parity bit */
1938 if (cflag
& PARENB
) {
1939 if (cflag
& PARODD
) {
1940 lParity
= LCR_PAR_ODD
;
1941 dbg("%s - parity = odd", __func__
);
1943 lParity
= LCR_PAR_EVEN
;
1944 dbg("%s - parity = even", __func__
);
1948 dbg("%s - parity = none", __func__
);
1952 lParity
= lParity
| 0x20;
1954 /* Change the Stop bit */
1955 if (cflag
& CSTOPB
) {
1957 dbg("%s - stop bits = 2", __func__
);
1960 dbg("%s - stop bits = 1", __func__
);
1963 /* Update the LCR with the correct value */
1964 mos7840_port
->shadowLCR
&=
1965 ~(LCR_BITS_MASK
| LCR_STOP_MASK
| LCR_PAR_MASK
);
1966 mos7840_port
->shadowLCR
|= (lData
| lParity
| lStop
);
1968 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
1969 mos7840_port
->shadowLCR
);
1970 /* Disable Interrupts */
1972 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
1975 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1978 mos7840_set_uart_reg(port
, FIFO_CONTROL_REGISTER
, Data
);
1980 /* Send the updated LCR value to the mos7840 */
1981 Data
= mos7840_port
->shadowLCR
;
1983 mos7840_set_uart_reg(port
, LINE_CONTROL_REGISTER
, Data
);
1986 mos7840_port
->shadowMCR
= Data
;
1987 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1989 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
1991 /* set up the MCR register and send it to the mos7840 */
1993 mos7840_port
->shadowMCR
= MCR_MASTER_IE
;
1995 mos7840_port
->shadowMCR
|= (MCR_DTR
| MCR_RTS
);
1997 if (cflag
& CRTSCTS
)
1998 mos7840_port
->shadowMCR
|= (MCR_XON_ANY
);
2000 mos7840_port
->shadowMCR
&= ~(MCR_XON_ANY
);
2002 Data
= mos7840_port
->shadowMCR
;
2003 mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2005 /* Determine divisor based on baud rate */
2006 baud
= tty_get_baud_rate(tty
);
2009 /* pick a default, any default... */
2010 dbg("%s", "Picked default baud...");
2014 dbg("%s - baud rate = %d", __func__
, baud
);
2015 status
= mos7840_send_cmd_write_baud_rate(mos7840_port
, baud
);
2017 /* Enable Interrupts */
2019 mos7840_set_uart_reg(port
, INTERRUPT_ENABLE_REGISTER
, Data
);
2021 if (mos7840_port
->read_urb_busy
== false) {
2022 mos7840_port
->read_urb
->dev
= serial
->dev
;
2023 mos7840_port
->read_urb_busy
= true;
2024 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
2026 dbg("usb_submit_urb(read bulk) failed, status = %d",
2028 mos7840_port
->read_urb_busy
= false;
2031 wake_up(&mos7840_port
->delta_msr_wait
);
2032 mos7840_port
->delta_msr_cond
= 1;
2033 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
2034 mos7840_port
->shadowLCR
);
2039 /*****************************************************************************
2040 * mos7840_set_termios
2041 * this function is called by the tty driver when it wants to change
2042 * the termios structure
2043 *****************************************************************************/
2045 static void mos7840_set_termios(struct tty_struct
*tty
,
2046 struct usb_serial_port
*port
,
2047 struct ktermios
*old_termios
)
2051 struct usb_serial
*serial
;
2052 struct moschip_port
*mos7840_port
;
2053 dbg("mos7840_set_termios: START");
2054 if (mos7840_port_paranoia_check(port
, __func__
)) {
2055 dbg("%s", "Invalid port");
2059 serial
= port
->serial
;
2061 if (mos7840_serial_paranoia_check(serial
, __func__
)) {
2062 dbg("%s", "Invalid Serial");
2066 mos7840_port
= mos7840_get_port_private(port
);
2068 if (mos7840_port
== NULL
)
2071 if (!mos7840_port
->open
) {
2072 dbg("%s - port not opened", __func__
);
2076 dbg("%s", "setting termios - ");
2078 cflag
= tty
->termios
->c_cflag
;
2080 dbg("%s - clfag %08x iflag %08x", __func__
,
2081 tty
->termios
->c_cflag
, RELEVANT_IFLAG(tty
->termios
->c_iflag
));
2082 dbg("%s - old clfag %08x old iflag %08x", __func__
,
2083 old_termios
->c_cflag
, RELEVANT_IFLAG(old_termios
->c_iflag
));
2084 dbg("%s - port %d", __func__
, port
->number
);
2086 /* change the port settings to the new ones specified */
2088 mos7840_change_port_settings(tty
, mos7840_port
, old_termios
);
2090 if (!mos7840_port
->read_urb
) {
2091 dbg("%s", "URB KILLED !!!!!");
2095 if (mos7840_port
->read_urb_busy
== false) {
2096 mos7840_port
->read_urb
->dev
= serial
->dev
;
2097 mos7840_port
->read_urb_busy
= true;
2098 status
= usb_submit_urb(mos7840_port
->read_urb
, GFP_ATOMIC
);
2100 dbg("usb_submit_urb(read bulk) failed, status = %d",
2102 mos7840_port
->read_urb_busy
= false;
2108 /*****************************************************************************
2109 * mos7840_get_lsr_info - get line status register info
2111 * Purpose: Let user call ioctl() to get info when the UART physically
2112 * is emptied. On bus types like RS485, the transmitter must
2113 * release the bus after transmitting. This must be done when
2114 * the transmit shift register is empty, not be done when the
2115 * transmit holding register is empty. This functionality
2116 * allows an RS485 driver to be written in user space.
2117 *****************************************************************************/
2119 static int mos7840_get_lsr_info(struct tty_struct
*tty
,
2120 unsigned int __user
*value
)
2123 unsigned int result
= 0;
2125 count
= mos7840_chars_in_buffer(tty
);
2127 dbg("%s -- Empty", __func__
);
2128 result
= TIOCSER_TEMT
;
2131 if (copy_to_user(value
, &result
, sizeof(int)))
2136 /*****************************************************************************
2137 * mos7840_set_modem_info
2138 * function to set modem info
2139 *****************************************************************************/
2141 /* FIXME: Should be using the model control hooks */
2143 static int mos7840_set_modem_info(struct moschip_port
*mos7840_port
,
2144 unsigned int cmd
, unsigned int __user
*value
)
2150 struct usb_serial_port
*port
;
2152 if (mos7840_port
== NULL
)
2155 port
= (struct usb_serial_port
*)mos7840_port
->port
;
2156 if (mos7840_port_paranoia_check(port
, __func__
)) {
2157 dbg("%s", "Invalid port");
2161 mcr
= mos7840_port
->shadowMCR
;
2163 if (copy_from_user(&arg
, value
, sizeof(int)))
2168 if (arg
& TIOCM_RTS
)
2170 if (arg
& TIOCM_DTR
)
2172 if (arg
& TIOCM_LOOP
)
2173 mcr
|= MCR_LOOPBACK
;
2177 if (arg
& TIOCM_RTS
)
2179 if (arg
& TIOCM_DTR
)
2181 if (arg
& TIOCM_LOOP
)
2182 mcr
&= ~MCR_LOOPBACK
;
2186 /* turn off the RTS and DTR and LOOPBACK
2187 * and then only turn on what was asked to */
2188 mcr
&= ~(MCR_RTS
| MCR_DTR
| MCR_LOOPBACK
);
2189 mcr
|= ((arg
& TIOCM_RTS
) ? MCR_RTS
: 0);
2190 mcr
|= ((arg
& TIOCM_DTR
) ? MCR_DTR
: 0);
2191 mcr
|= ((arg
& TIOCM_LOOP
) ? MCR_LOOPBACK
: 0);
2196 mos7840_port
->shadowMCR
= mcr
;
2198 Data
= mos7840_port
->shadowMCR
;
2199 status
= mos7840_set_uart_reg(port
, MODEM_CONTROL_REGISTER
, Data
);
2202 dbg("setting MODEM_CONTROL_REGISTER Failed");
2209 /*****************************************************************************
2210 * mos7840_get_modem_info
2211 * function to get modem info
2212 *****************************************************************************/
2214 static int mos7840_get_modem_info(struct moschip_port
*mos7840_port
,
2215 unsigned int __user
*value
)
2217 unsigned int result
= 0;
2219 unsigned int mcr
= mos7840_port
->shadowMCR
;
2220 mos7840_get_uart_reg(mos7840_port
->port
,
2221 MODEM_STATUS_REGISTER
, &msr
);
2222 result
= ((mcr
& MCR_DTR
) ? TIOCM_DTR
: 0) /* 0x002 */
2223 |((mcr
& MCR_RTS
) ? TIOCM_RTS
: 0) /* 0x004 */
2224 |((msr
& MOS7840_MSR_CTS
) ? TIOCM_CTS
: 0) /* 0x020 */
2225 |((msr
& MOS7840_MSR_CD
) ? TIOCM_CAR
: 0) /* 0x040 */
2226 |((msr
& MOS7840_MSR_RI
) ? TIOCM_RI
: 0) /* 0x080 */
2227 |((msr
& MOS7840_MSR_DSR
) ? TIOCM_DSR
: 0); /* 0x100 */
2229 dbg("%s -- %x", __func__
, result
);
2231 if (copy_to_user(value
, &result
, sizeof(int)))
2236 /*****************************************************************************
2237 * mos7840_get_serial_info
2238 * function to get information about serial port
2239 *****************************************************************************/
2241 static int mos7840_get_serial_info(struct moschip_port
*mos7840_port
,
2242 struct serial_struct __user
*retinfo
)
2244 struct serial_struct tmp
;
2246 if (mos7840_port
== NULL
)
2252 memset(&tmp
, 0, sizeof(tmp
));
2254 tmp
.type
= PORT_16550A
;
2255 tmp
.line
= mos7840_port
->port
->serial
->minor
;
2256 tmp
.port
= mos7840_port
->port
->number
;
2258 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
2259 tmp
.xmit_fifo_size
= NUM_URBS
* URB_TRANSFER_BUFFER_SIZE
;
2260 tmp
.baud_base
= 9600;
2261 tmp
.close_delay
= 5 * HZ
;
2262 tmp
.closing_wait
= 30 * HZ
;
2264 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2269 /*****************************************************************************
2271 * this function handles any ioctl calls to the driver
2272 *****************************************************************************/
2274 static int mos7840_ioctl(struct tty_struct
*tty
, struct file
*file
,
2275 unsigned int cmd
, unsigned long arg
)
2277 struct usb_serial_port
*port
= tty
->driver_data
;
2278 void __user
*argp
= (void __user
*)arg
;
2279 struct moschip_port
*mos7840_port
;
2281 struct async_icount cnow
;
2282 struct async_icount cprev
;
2283 struct serial_icounter_struct icount
;
2286 if (mos7840_port_paranoia_check(port
, __func__
)) {
2287 dbg("%s", "Invalid port");
2291 mos7840_port
= mos7840_get_port_private(port
);
2293 if (mos7840_port
== NULL
)
2296 dbg("%s - port %d, cmd = 0x%x", __func__
, port
->number
, cmd
);
2299 /* return number of bytes available */
2302 dbg("%s (%d) TIOCSERGETLSR", __func__
, port
->number
);
2303 return mos7840_get_lsr_info(tty
, argp
);
2306 /* FIXME: use the modem hooks and remove this */
2310 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__
,
2313 mos7840_set_modem_info(mos7840_port
, cmd
, argp
);
2317 dbg("%s (%d) TIOCMGET", __func__
, port
->number
);
2318 return mos7840_get_modem_info(mos7840_port
, argp
);
2321 dbg("%s (%d) TIOCGSERIAL", __func__
, port
->number
);
2322 return mos7840_get_serial_info(mos7840_port
, argp
);
2325 dbg("%s (%d) TIOCSSERIAL", __func__
, port
->number
);
2329 dbg("%s (%d) TIOCMIWAIT", __func__
, port
->number
);
2330 cprev
= mos7840_port
->icount
;
2332 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2333 mos7840_port
->delta_msr_cond
= 0;
2334 wait_event_interruptible(mos7840_port
->delta_msr_wait
,
2336 delta_msr_cond
== 1));
2338 /* see if a signal did it */
2339 if (signal_pending(current
))
2340 return -ERESTARTSYS
;
2341 cnow
= mos7840_port
->icount
;
2343 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
2344 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
2345 return -EIO
; /* no change => error */
2346 if (((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
2347 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
2348 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
2349 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
))) {
2358 cnow
= mos7840_port
->icount
;
2360 icount
.cts
= cnow
.cts
;
2361 icount
.dsr
= cnow
.dsr
;
2362 icount
.rng
= cnow
.rng
;
2363 icount
.dcd
= cnow
.dcd
;
2364 icount
.rx
= cnow
.rx
;
2365 icount
.tx
= cnow
.tx
;
2366 icount
.frame
= cnow
.frame
;
2367 icount
.overrun
= cnow
.overrun
;
2368 icount
.parity
= cnow
.parity
;
2369 icount
.brk
= cnow
.brk
;
2370 icount
.buf_overrun
= cnow
.buf_overrun
;
2372 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__
,
2373 port
->number
, icount
.rx
, icount
.tx
);
2374 if (copy_to_user(argp
, &icount
, sizeof(icount
)))
2380 return -ENOIOCTLCMD
;
2383 static int mos7840_calc_num_ports(struct usb_serial
*serial
)
2385 int mos7840_num_ports
= 0;
2387 dbg("numberofendpoints: cur %d, alt %d",
2388 (int)serial
->interface
->cur_altsetting
->desc
.bNumEndpoints
,
2389 (int)serial
->interface
->altsetting
->desc
.bNumEndpoints
);
2390 if (serial
->interface
->cur_altsetting
->desc
.bNumEndpoints
== 5) {
2391 mos7840_num_ports
= serial
->num_ports
= 2;
2392 } else if (serial
->interface
->cur_altsetting
->desc
.bNumEndpoints
== 9) {
2393 serial
->num_bulk_in
= 4;
2394 serial
->num_bulk_out
= 4;
2395 mos7840_num_ports
= serial
->num_ports
= 4;
2397 dbg ("mos7840_num_ports = %d", mos7840_num_ports
);
2398 return mos7840_num_ports
;
2401 /****************************************************************************
2403 ****************************************************************************/
2405 static int mos7840_startup(struct usb_serial
*serial
)
2407 struct moschip_port
*mos7840_port
;
2408 struct usb_device
*dev
;
2412 dbg("%s", "mos7840_startup :Entering..........");
2415 dbg("%s", "Invalid Handler");
2421 dbg("%s", "Entering...");
2422 dbg ("mos7840_startup: serial = %p", serial
);
2424 /* we set up the pointers to the endpoints in the mos7840_open *
2425 * function, as the structures aren't created yet. */
2427 /* set up port private structures */
2428 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2429 dbg ("mos7840_startup: configuring port %d............", i
);
2430 mos7840_port
= kzalloc(sizeof(struct moschip_port
), GFP_KERNEL
);
2431 if (mos7840_port
== NULL
) {
2432 dev_err(&dev
->dev
, "%s - Out of memory\n", __func__
);
2434 i
--; /* don't follow NULL pointer cleaning up */
2438 /* Initialize all port interrupt end point to port 0 int
2439 * endpoint. Our device has only one interrupt end point
2440 * common to all port */
2442 mos7840_port
->port
= serial
->port
[i
];
2443 mos7840_set_port_private(serial
->port
[i
], mos7840_port
);
2444 spin_lock_init(&mos7840_port
->pool_lock
);
2446 /* minor is not initialised until later by
2447 * usb-serial.c:get_free_serial() and cannot therefore be used
2448 * to index device instances */
2449 mos7840_port
->port_num
= i
+ 1;
2450 dbg ("serial->port[i]->number = %d", serial
->port
[i
]->number
);
2451 dbg ("serial->port[i]->serial->minor = %d", serial
->port
[i
]->serial
->minor
);
2452 dbg ("mos7840_port->port_num = %d", mos7840_port
->port_num
);
2453 dbg ("serial->minor = %d", serial
->minor
);
2455 if (mos7840_port
->port_num
== 1) {
2456 mos7840_port
->SpRegOffset
= 0x0;
2457 mos7840_port
->ControlRegOffset
= 0x1;
2458 mos7840_port
->DcrRegOffset
= 0x4;
2459 } else if ((mos7840_port
->port_num
== 2)
2460 && (serial
->num_ports
== 4)) {
2461 mos7840_port
->SpRegOffset
= 0x8;
2462 mos7840_port
->ControlRegOffset
= 0x9;
2463 mos7840_port
->DcrRegOffset
= 0x16;
2464 } else if ((mos7840_port
->port_num
== 2)
2465 && (serial
->num_ports
== 2)) {
2466 mos7840_port
->SpRegOffset
= 0xa;
2467 mos7840_port
->ControlRegOffset
= 0xb;
2468 mos7840_port
->DcrRegOffset
= 0x19;
2469 } else if ((mos7840_port
->port_num
== 3)
2470 && (serial
->num_ports
== 4)) {
2471 mos7840_port
->SpRegOffset
= 0xa;
2472 mos7840_port
->ControlRegOffset
= 0xb;
2473 mos7840_port
->DcrRegOffset
= 0x19;
2474 } else if ((mos7840_port
->port_num
== 4)
2475 && (serial
->num_ports
== 4)) {
2476 mos7840_port
->SpRegOffset
= 0xc;
2477 mos7840_port
->ControlRegOffset
= 0xd;
2478 mos7840_port
->DcrRegOffset
= 0x1c;
2480 mos7840_dump_serial_port(mos7840_port
);
2481 mos7840_set_port_private(serial
->port
[i
], mos7840_port
);
2483 /* enable rx_disable bit in control register */
2484 status
= mos7840_get_reg_sync(serial
->port
[i
],
2485 mos7840_port
->ControlRegOffset
, &Data
);
2487 dbg("Reading ControlReg failed status-0x%x", status
);
2490 dbg("ControlReg Reading success val is %x, status%d",
2492 Data
|= 0x08; /* setting driver done bit */
2493 Data
|= 0x04; /* sp1_bit to have cts change reflect in
2496 /* Data |= 0x20; //rx_disable bit */
2497 status
= mos7840_set_reg_sync(serial
->port
[i
],
2498 mos7840_port
->ControlRegOffset
, Data
);
2500 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status
);
2503 dbg("ControlReg Writing success(rx_disable) status%d",
2506 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2509 status
= mos7840_set_reg_sync(serial
->port
[i
],
2510 (__u16
) (mos7840_port
->DcrRegOffset
+ 0), Data
);
2512 dbg("Writing DCR0 failed status-0x%x", status
);
2515 dbg("DCR0 Writing success status%d", status
);
2518 status
= mos7840_set_reg_sync(serial
->port
[i
],
2519 (__u16
) (mos7840_port
->DcrRegOffset
+ 1), Data
);
2521 dbg("Writing DCR1 failed status-0x%x", status
);
2524 dbg("DCR1 Writing success status%d", status
);
2527 status
= mos7840_set_reg_sync(serial
->port
[i
],
2528 (__u16
) (mos7840_port
->DcrRegOffset
+ 2), Data
);
2530 dbg("Writing DCR2 failed status-0x%x", status
);
2533 dbg("DCR2 Writing success status%d", status
);
2535 /* write values in clkstart0x0 and clkmulti 0x20 */
2537 status
= mos7840_set_reg_sync(serial
->port
[i
],
2538 CLK_START_VALUE_REGISTER
, Data
);
2540 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status
);
2543 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status
);
2546 status
= mos7840_set_reg_sync(serial
->port
[i
],
2547 CLK_MULTI_REGISTER
, Data
);
2549 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
2553 dbg("CLK_MULTI_REGISTER Writing success status%d",
2556 /* write value 0x0 to scratchpad register */
2558 status
= mos7840_set_uart_reg(serial
->port
[i
],
2559 SCRATCH_PAD_REGISTER
, Data
);
2561 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
2565 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
2568 /* Zero Length flag register */
2569 if ((mos7840_port
->port_num
!= 1)
2570 && (serial
->num_ports
== 2)) {
2573 status
= mos7840_set_reg_sync(serial
->port
[i
],
2575 ((__u16
)mos7840_port
->port_num
)), Data
);
2576 dbg("ZLIP offset %x",
2578 ((__u16
) mos7840_port
->port_num
)));
2580 dbg("Writing ZLP_REG%d failed status-0x%x",
2584 dbg("ZLP_REG%d Writing success status%d",
2588 status
= mos7840_set_reg_sync(serial
->port
[i
],
2590 ((__u16
)mos7840_port
->port_num
) - 0x1), Data
);
2591 dbg("ZLIP offset %x",
2593 ((__u16
) mos7840_port
->port_num
) - 0x1));
2595 dbg("Writing ZLP_REG%d failed status-0x%x",
2599 dbg("ZLP_REG%d Writing success status%d",
2603 mos7840_port
->control_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2604 mos7840_port
->ctrl_buf
= kmalloc(16, GFP_KERNEL
);
2605 mos7840_port
->dr
= kmalloc(sizeof(struct usb_ctrlrequest
),
2607 if (!mos7840_port
->control_urb
|| !mos7840_port
->ctrl_buf
||
2608 !mos7840_port
->dr
) {
2613 dbg ("mos7840_startup: all ports configured...........");
2615 /* Zero Length flag enable */
2617 status
= mos7840_set_reg_sync(serial
->port
[0], ZLP_REG5
, Data
);
2619 dbg("Writing ZLP_REG5 failed status-0x%x", status
);
2622 dbg("ZLP_REG5 Writing success status%d", status
);
2624 /* setting configuration feature to one */
2625 usb_control_msg(serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0),
2626 (__u8
) 0x03, 0x00, 0x01, 0x00, NULL
, 0x00, 5 * HZ
);
2629 for (/* nothing */; i
>= 0; i
--) {
2630 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
2632 kfree(mos7840_port
->dr
);
2633 kfree(mos7840_port
->ctrl_buf
);
2634 usb_free_urb(mos7840_port
->control_urb
);
2635 kfree(mos7840_port
);
2636 serial
->port
[i
] = NULL
;
2641 /****************************************************************************
2642 * mos7840_disconnect
2643 * This function is called whenever the device is removed from the usb bus.
2644 ****************************************************************************/
2646 static void mos7840_disconnect(struct usb_serial
*serial
)
2649 unsigned long flags
;
2650 struct moschip_port
*mos7840_port
;
2651 dbg("%s", " disconnect :entering..........");
2654 dbg("%s", "Invalid Handler");
2658 /* check for the ports to be closed,close the ports and disconnect */
2660 /* free private structure allocated for serial port *
2661 * stop reads and writes on all ports */
2663 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2664 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
2665 dbg ("mos7840_port %d = %p", i
, mos7840_port
);
2667 spin_lock_irqsave(&mos7840_port
->pool_lock
, flags
);
2668 mos7840_port
->zombie
= 1;
2669 spin_unlock_irqrestore(&mos7840_port
->pool_lock
, flags
);
2670 usb_kill_urb(mos7840_port
->control_urb
);
2674 dbg("%s", "Thank u :: ");
2678 /****************************************************************************
2680 * This function is called when the usb_serial structure is freed.
2681 ****************************************************************************/
2683 static void mos7840_release(struct usb_serial
*serial
)
2686 struct moschip_port
*mos7840_port
;
2687 dbg("%s", " release :entering..........");
2690 dbg("%s", "Invalid Handler");
2694 /* check for the ports to be closed,close the ports and disconnect */
2696 /* free private structure allocated for serial port *
2697 * stop reads and writes on all ports */
2699 for (i
= 0; i
< serial
->num_ports
; ++i
) {
2700 mos7840_port
= mos7840_get_port_private(serial
->port
[i
]);
2701 dbg("mos7840_port %d = %p", i
, mos7840_port
);
2703 kfree(mos7840_port
->ctrl_buf
);
2704 kfree(mos7840_port
->dr
);
2705 kfree(mos7840_port
);
2709 dbg("%s", "Thank u :: ");
2713 static struct usb_driver io_driver
= {
2715 .probe
= usb_serial_probe
,
2716 .disconnect
= usb_serial_disconnect
,
2717 .id_table
= moschip_id_table_combined
,
2721 static struct usb_serial_driver moschip7840_4port_device
= {
2723 .owner
= THIS_MODULE
,
2726 .description
= DRIVER_DESC
,
2727 .usb_driver
= &io_driver
,
2728 .id_table
= moschip_port_id_table
,
2730 .open
= mos7840_open
,
2731 .close
= mos7840_close
,
2732 .write
= mos7840_write
,
2733 .write_room
= mos7840_write_room
,
2734 .chars_in_buffer
= mos7840_chars_in_buffer
,
2735 .throttle
= mos7840_throttle
,
2736 .unthrottle
= mos7840_unthrottle
,
2737 .calc_num_ports
= mos7840_calc_num_ports
,
2738 #ifdef MCSSerialProbe
2739 .probe
= mos7840_serial_probe
,
2741 .ioctl
= mos7840_ioctl
,
2742 .set_termios
= mos7840_set_termios
,
2743 .break_ctl
= mos7840_break
,
2744 .tiocmget
= mos7840_tiocmget
,
2745 .tiocmset
= mos7840_tiocmset
,
2746 .attach
= mos7840_startup
,
2747 .disconnect
= mos7840_disconnect
,
2748 .release
= mos7840_release
,
2749 .read_bulk_callback
= mos7840_bulk_in_callback
,
2750 .read_int_callback
= mos7840_interrupt_callback
,
2753 /****************************************************************************
2755 * This is called by the module subsystem, or on startup to initialize us
2756 ****************************************************************************/
2757 static int __init
moschip7840_init(void)
2761 dbg("%s", " mos7840_init :entering..........");
2763 /* Register with the usb serial */
2764 retval
= usb_serial_register(&moschip7840_4port_device
);
2767 goto failed_port_device_register
;
2769 dbg("%s", "Entering...");
2770 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
2773 /* Register with the usb */
2774 retval
= usb_register(&io_driver
);
2776 dbg("%s", "Leaving...");
2779 usb_serial_deregister(&moschip7840_4port_device
);
2780 failed_port_device_register
:
2784 /****************************************************************************
2786 * Called when the driver is about to be unloaded.
2787 ****************************************************************************/
2788 static void __exit
moschip7840_exit(void)
2791 dbg("%s", " mos7840_exit :entering..........");
2793 usb_deregister(&io_driver
);
2795 usb_serial_deregister(&moschip7840_4port_device
);
2797 dbg("%s", "Entering...");
2800 module_init(moschip7840_init
);
2801 module_exit(moschip7840_exit
);
2803 /* Module information */
2804 MODULE_DESCRIPTION(DRIVER_DESC
);
2805 MODULE_LICENSE("GPL");
2807 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
2808 MODULE_PARM_DESC(debug
, "Debug enabled or not");