2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * Clean ups from Moschip version and a few ioctl implementations by:
17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/tty.h>
30 #include <linux/tty_driver.h>
31 #include <linux/tty_flip.h>
32 #include <linux/module.h>
33 #include <linux/serial.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
36 #include <linux/uaccess.h>
41 #define DRIVER_VERSION "1.3.2"
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 MOS_WDR_TIMEOUT 5000 /* default urb timeout */
87 #define MOS_PORT1 0x0200
88 #define MOS_PORT2 0x0300
89 #define MOS_VENREG 0x0000
90 #define MOS_MAX_PORT 0x02
91 #define MOS_WRITE 0x0E
95 #define MCS_RD_RTYPE 0xC0
96 #define MCS_WR_RTYPE 0x40
97 #define MCS_RDREQ 0x0D
98 #define MCS_WRREQ 0x0E
99 #define MCS_CTRL_TIMEOUT 500
100 #define VENDOR_READ_LENGTH (0x01)
102 #define MAX_NAME_LEN 64
104 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
105 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
107 /* For higher baud Rates use TIOCEXBAUD */
108 #define TIOCEXBAUD 0x5462
110 /* vendor id and device id defines */
112 /* The native mos7840/7820 component */
113 #define USB_VENDOR_ID_MOSCHIP 0x9710
114 #define MOSCHIP_DEVICE_ID_7840 0x7840
115 #define MOSCHIP_DEVICE_ID_7820 0x7820
116 #define MOSCHIP_DEVICE_ID_7810 0x7810
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 id_table.
121 #define USB_VENDOR_ID_BANDB 0x0856
122 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
123 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
124 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
125 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
126 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
127 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
128 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
129 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
130 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
131 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
132 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
133 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
134 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
136 /* This driver also supports
137 * ATEN UC2324 device using Moschip MCS7840
138 * ATEN UC2322 device using Moschip MCS7820
140 #define USB_VENDOR_ID_ATENINTL 0x0557
141 #define ATENINTL_DEVICE_ID_UC2324 0x2011
142 #define ATENINTL_DEVICE_ID_UC2322 0x7820
144 /* Interrupt Routine Defines */
146 #define SERIAL_IIR_RLS 0x06
147 #define SERIAL_IIR_MS 0x00
150 * Emulation of the bit mask on the LINE STATUS REGISTER.
152 #define SERIAL_LSR_DR 0x0001
153 #define SERIAL_LSR_OE 0x0002
154 #define SERIAL_LSR_PE 0x0004
155 #define SERIAL_LSR_FE 0x0008
156 #define SERIAL_LSR_BI 0x0010
158 #define MOS_MSR_DELTA_CTS 0x10
159 #define MOS_MSR_DELTA_DSR 0x20
160 #define MOS_MSR_DELTA_RI 0x40
161 #define MOS_MSR_DELTA_CD 0x80
163 /* Serial Port register Address */
164 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
165 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
166 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
167 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
168 #define LINE_STATUS_REGISTER ((__u16)(0x05))
169 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
170 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
171 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
172 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
174 #define CLK_MULTI_REGISTER ((__u16)(0x02))
175 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
176 #define GPIO_REGISTER ((__u16)(0x07))
178 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
181 * URB POOL related defines
183 #define NUM_URBS 16 /* URB Count */
184 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
186 /* LED on/off milliseconds*/
187 #define LED_ON_MS 500
188 #define LED_OFF_MS 500
190 static int device_type;
192 static const struct usb_device_id id_table[] = {
193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
194 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
195 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
209 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
210 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
211 {} /* terminating entry */
213 MODULE_DEVICE_TABLE(usb, id_table);
215 /* This structure holds all of the local port information */
217 struct moschip_port {
218 int port_num; /*Actual port number in the device(1,2,etc) */
219 struct urb *write_urb; /* write URB for this port */
220 struct urb *read_urb; /* read URB for this port */
222 __u8 shadowLCR; /* last LCR value received */
223 __u8 shadowMCR; /* last MCR value received */
227 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
228 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
230 struct async_icount icount;
231 struct usb_serial_port *port; /* loop back to the owner of this object */
235 __u8 ControlRegOffset;
237 /* for processing control URBS in interrupt context */
238 struct urb *control_urb;
239 struct usb_ctrlrequest *dr;
243 spinlock_t pool_lock;
244 struct urb *write_urb_pool[NUM_URBS];
248 /* For device(s) with LED indicator */
251 struct timer_list led_timer1; /* Timer for LED on */
252 struct timer_list led_timer2; /* Timer for LED off */
258 * mos7840_set_reg_sync
259 * To set the Control register by calling usb_fill_control_urb function
260 * by passing usb_sndctrlpipe function as parameter.
263 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
266 struct usb_device *dev = port->serial->dev;
268 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
270 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
271 MCS_WR_RTYPE, val, reg, NULL, 0,
276 * mos7840_get_reg_sync
277 * To set the Uart register by calling usb_fill_control_urb function by
278 * passing usb_rcvctrlpipe function as parameter.
281 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
284 struct usb_device *dev = port->serial->dev;
288 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
292 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
293 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
296 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
303 * mos7840_set_uart_reg
304 * To set the Uart register by calling usb_fill_control_urb function by
305 * passing usb_sndctrlpipe function as parameter.
308 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
312 struct usb_device *dev = port->serial->dev;
314 /* For the UART control registers, the application number need
316 if (port->serial->num_ports == 4) {
317 val |= (((__u16) port->number -
318 (__u16) (port->serial->minor)) + 1) << 8;
319 dbg("mos7840_set_uart_reg application number is %x", val);
321 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
322 val |= (((__u16) port->number -
323 (__u16) (port->serial->minor)) + 1) << 8;
324 dbg("mos7840_set_uart_reg application number is %x",
328 (((__u16) port->number -
329 (__u16) (port->serial->minor)) + 2) << 8;
330 dbg("mos7840_set_uart_reg application number is %x",
334 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
335 MCS_WR_RTYPE, val, reg, NULL, 0,
341 * mos7840_get_uart_reg
342 * To set the Control register by calling usb_fill_control_urb function
343 * by passing usb_rcvctrlpipe function as parameter.
345 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
348 struct usb_device *dev = port->serial->dev;
353 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
357 /* dbg("application number is %4x",
358 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
359 /* Wval is same as application number */
360 if (port->serial->num_ports == 4) {
362 (((__u16) port->number - (__u16) (port->serial->minor)) +
364 dbg("mos7840_get_uart_reg application number is %x", Wval);
366 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
367 Wval = (((__u16) port->number -
368 (__u16) (port->serial->minor)) + 1) << 8;
369 dbg("mos7840_get_uart_reg application number is %x",
372 Wval = (((__u16) port->number -
373 (__u16) (port->serial->minor)) + 2) << 8;
374 dbg("mos7840_get_uart_reg application number is %x",
378 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
379 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
387 static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
390 dbg("***************************************");
391 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
392 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
393 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
394 dbg("***************************************");
398 /************************************************************************/
399 /************************************************************************/
400 /* I N T E R F A C E F U N C T I O N S */
401 /* I N T E R F A C E F U N C T I O N S */
402 /************************************************************************/
403 /************************************************************************/
405 static inline void mos7840_set_port_private(struct usb_serial_port *port,
406 struct moschip_port *data)
408 usb_set_serial_port_data(port, (void *)data);
411 static inline struct moschip_port *mos7840_get_port_private(struct
415 return (struct moschip_port *)usb_get_serial_port_data(port);
418 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
420 struct moschip_port *mos7840_port;
421 struct async_icount *icount;
423 icount = &mos7840_port->icount;
425 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
427 icount = &mos7840_port->icount;
429 /* update input line counters */
430 if (new_msr & MOS_MSR_DELTA_CTS) {
434 if (new_msr & MOS_MSR_DELTA_DSR) {
438 if (new_msr & MOS_MSR_DELTA_CD) {
442 if (new_msr & MOS_MSR_DELTA_RI) {
449 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
451 struct async_icount *icount;
453 dbg("%s - %02x", __func__, new_lsr);
455 if (new_lsr & SERIAL_LSR_BI) {
457 * Parity and Framing errors only count if they
458 * occur exclusive of a break being
461 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
464 /* update input line counters */
465 icount = &port->icount;
466 if (new_lsr & SERIAL_LSR_BI) {
470 if (new_lsr & SERIAL_LSR_OE) {
474 if (new_lsr & SERIAL_LSR_PE) {
478 if (new_lsr & SERIAL_LSR_FE) {
484 /************************************************************************/
485 /************************************************************************/
486 /* U S B C A L L B A C K F U N C T I O N S */
487 /* U S B C A L L B A C K F U N C T I O N S */
488 /************************************************************************/
489 /************************************************************************/
491 static void mos7840_control_callback(struct urb *urb)
494 struct moschip_port *mos7840_port;
497 int status = urb->status;
499 mos7840_port = urb->context;
508 /* this urb is terminated, clean up */
509 dbg("%s - urb shutting down with status: %d", __func__,
513 dbg("%s - nonzero urb status received: %d", __func__,
518 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
519 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
520 mos7840_port->MsrLsr, mos7840_port->port_num);
521 data = urb->transfer_buffer;
522 regval = (__u8) data[0];
523 dbg("%s data is %x", __func__, regval);
524 if (mos7840_port->MsrLsr == 0)
525 mos7840_handle_new_msr(mos7840_port, regval);
526 else if (mos7840_port->MsrLsr == 1)
527 mos7840_handle_new_lsr(mos7840_port, regval);
530 spin_lock(&mos7840_port->pool_lock);
531 if (!mos7840_port->zombie)
532 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
533 spin_unlock(&mos7840_port->pool_lock);
535 dev_err(&urb->dev->dev,
536 "%s - Error %d submitting interrupt urb\n",
541 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
544 struct usb_device *dev = mcs->port->serial->dev;
545 struct usb_ctrlrequest *dr = mcs->dr;
546 unsigned char *buffer = mcs->ctrl_buf;
549 dr->bRequestType = MCS_RD_RTYPE;
550 dr->bRequest = MCS_RDREQ;
551 dr->wValue = cpu_to_le16(Wval); /* 0 */
552 dr->wIndex = cpu_to_le16(reg);
553 dr->wLength = cpu_to_le16(2);
555 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
556 (unsigned char *)dr, buffer, 2,
557 mos7840_control_callback, mcs);
558 mcs->control_urb->transfer_buffer_length = 2;
559 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
563 static void mos7840_set_led_callback(struct urb *urb)
565 switch (urb->status) {
572 /* This urb is terminated, clean up */
573 dbg("%s - urb shutting down with status: %d", __func__,
577 dbg("%s - nonzero urb status received: %d", __func__,
582 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
585 struct usb_device *dev = mcs->port->serial->dev;
586 struct usb_ctrlrequest *dr = mcs->dr;
588 dr->bRequestType = MCS_WR_RTYPE;
589 dr->bRequest = MCS_WRREQ;
590 dr->wValue = cpu_to_le16(wval);
591 dr->wIndex = cpu_to_le16(reg);
592 dr->wLength = cpu_to_le16(0);
594 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
595 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
597 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
600 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
603 struct usb_device *dev = port->serial->dev;
605 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
606 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
609 static void mos7840_led_off(unsigned long arg)
611 struct moschip_port *mcs = (struct moschip_port *) arg;
614 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
615 mod_timer(&mcs->led_timer2,
616 jiffies + msecs_to_jiffies(LED_OFF_MS));
619 static void mos7840_led_flag_off(unsigned long arg)
621 struct moschip_port *mcs = (struct moschip_port *) arg;
623 mcs->led_flag = false;
626 /*****************************************************************************
627 * mos7840_interrupt_callback
628 * this is the callback function for when we have received data on the
629 * interrupt endpoint.
630 *****************************************************************************/
632 static void mos7840_interrupt_callback(struct urb *urb)
636 struct moschip_port *mos7840_port;
637 struct usb_serial *serial;
642 __u16 wval, wreg = 0;
643 int status = urb->status;
652 /* this urb is terminated, clean up */
653 dbg("%s - urb shutting down with status: %d", __func__,
657 dbg("%s - nonzero urb status received: %d", __func__,
662 length = urb->actual_length;
663 data = urb->transfer_buffer;
665 serial = urb->context;
667 /* Moschip get 5 bytes
668 * Byte 1 IIR Port 1 (port.number is 0)
669 * Byte 2 IIR Port 2 (port.number is 1)
670 * Byte 3 IIR Port 3 (port.number is 2)
671 * Byte 4 IIR Port 4 (port.number is 3)
672 * Byte 5 FIFO status for both */
674 if (length && length > 5) {
675 dbg("%s", "Wrong data !!!");
679 sp[0] = (__u8) data[0];
680 sp[1] = (__u8) data[1];
681 sp[2] = (__u8) data[2];
682 sp[3] = (__u8) data[3];
685 for (i = 0; i < serial->num_ports; i++) {
686 mos7840_port = mos7840_get_port_private(serial->port[i]);
688 (((__u16) serial->port[i]->number -
689 (__u16) (serial->minor)) + 1) << 8;
690 if (mos7840_port->open) {
692 dbg("SP%d No Interrupt !!!", i);
694 switch (sp[i] & 0x0f) {
696 dbg("Serial Port %d: Receiver status error or ", i);
697 dbg("address bit detected in 9-bit mode");
698 mos7840_port->MsrLsr = 1;
699 wreg = LINE_STATUS_REGISTER;
702 dbg("Serial Port %d: Modem status change", i);
703 mos7840_port->MsrLsr = 0;
704 wreg = MODEM_STATUS_REGISTER;
707 spin_lock(&mos7840_port->pool_lock);
708 if (!mos7840_port->zombie) {
709 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
711 spin_unlock(&mos7840_port->pool_lock);
714 spin_unlock(&mos7840_port->pool_lock);
719 /* the completion handler for the control urb will resubmit */
722 result = usb_submit_urb(urb, GFP_ATOMIC);
724 dev_err(&urb->dev->dev,
725 "%s - Error %d submitting interrupt urb\n",
730 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
731 const char *function)
734 dbg("%s - port == NULL", function);
738 dbg("%s - port->serial == NULL", function);
745 /* Inline functions to check the sanity of a pointer that is passed to us */
746 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
747 const char *function)
750 dbg("%s - serial == NULL", function);
754 dbg("%s - serial->type == NULL!", function);
761 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
762 const char *function)
764 /* if no port was specified, or it fails a paranoia check */
766 mos7840_port_paranoia_check(port, function) ||
767 mos7840_serial_paranoia_check(port->serial, function)) {
768 /* then say that we don't have a valid usb_serial thing,
769 * which will end up genrating -ENODEV return values */
776 /*****************************************************************************
777 * mos7840_bulk_in_callback
778 * this is the callback function for when we have received data on the
780 *****************************************************************************/
782 static void mos7840_bulk_in_callback(struct urb *urb)
786 struct usb_serial *serial;
787 struct usb_serial_port *port;
788 struct moschip_port *mos7840_port;
789 struct tty_struct *tty;
790 int status = urb->status;
792 mos7840_port = urb->context;
794 dbg("%s", "NULL mos7840_port pointer");
799 dbg("nonzero read bulk status received: %d", status);
800 mos7840_port->read_urb_busy = false;
804 port = (struct usb_serial_port *)mos7840_port->port;
805 if (mos7840_port_paranoia_check(port, __func__)) {
806 dbg("%s", "Port Paranoia failed");
807 mos7840_port->read_urb_busy = false;
811 serial = mos7840_get_usb_serial(port, __func__);
813 dbg("%s", "Bad serial pointer");
814 mos7840_port->read_urb_busy = false;
818 data = urb->transfer_buffer;
820 if (urb->actual_length) {
821 tty = tty_port_tty_get(&mos7840_port->port->port);
823 tty_insert_flip_string(tty, data, urb->actual_length);
825 tty_flip_buffer_push(tty);
828 mos7840_port->icount.rx += urb->actual_length;
830 dbg("mos7840_port->icount.rx is %d:",
831 mos7840_port->icount.rx);
834 if (!mos7840_port->read_urb) {
835 dbg("%s", "URB KILLED !!!");
836 mos7840_port->read_urb_busy = false;
841 if (mos7840_port->has_led && !mos7840_port->led_flag) {
842 mos7840_port->led_flag = true;
843 mos7840_set_led_async(mos7840_port, 0x0301,
844 MODEM_CONTROL_REGISTER);
845 mod_timer(&mos7840_port->led_timer1,
846 jiffies + msecs_to_jiffies(LED_ON_MS));
849 mos7840_port->read_urb_busy = true;
850 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
853 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
854 mos7840_port->read_urb_busy = false;
858 /*****************************************************************************
859 * mos7840_bulk_out_data_callback
860 * this is the callback function for when we have finished sending
861 * serial data on the bulk out endpoint.
862 *****************************************************************************/
864 static void mos7840_bulk_out_data_callback(struct urb *urb)
866 struct moschip_port *mos7840_port;
867 struct tty_struct *tty;
868 int status = urb->status;
871 mos7840_port = urb->context;
872 spin_lock(&mos7840_port->pool_lock);
873 for (i = 0; i < NUM_URBS; i++) {
874 if (urb == mos7840_port->write_urb_pool[i]) {
875 mos7840_port->busy[i] = 0;
879 spin_unlock(&mos7840_port->pool_lock);
882 dbg("nonzero write bulk status received:%d", status);
886 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
887 dbg("%s", "Port Paranoia failed");
891 tty = tty_port_tty_get(&mos7840_port->port->port);
892 if (tty && mos7840_port->open)
898 /************************************************************************/
899 /* 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 */
900 /************************************************************************/
901 #ifdef MCSSerialProbe
902 static int mos7840_serial_probe(struct usb_serial *serial,
903 const struct usb_device_id *id)
906 /*need to implement the mode_reg reading and updating\
907 structures usb_serial_ device_type\
908 (i.e num_ports, num_bulkin,bulkout etc) */
909 /* Also we can update the changes attach */
914 /*****************************************************************************
916 * this function is called by the tty driver when a port is opened
917 * If successful, we return 0
918 * Otherwise we return a negative error number.
919 *****************************************************************************/
921 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
925 struct usb_serial *serial;
929 struct moschip_port *mos7840_port;
930 struct moschip_port *port0;
932 if (mos7840_port_paranoia_check(port, __func__)) {
933 dbg("%s", "Port Paranoia failed");
937 serial = port->serial;
939 if (mos7840_serial_paranoia_check(serial, __func__)) {
940 dbg("%s", "Serial Paranoia failed");
944 mos7840_port = mos7840_get_port_private(port);
945 port0 = mos7840_get_port_private(serial->port[0]);
947 if (mos7840_port == NULL || port0 == NULL)
950 usb_clear_halt(serial->dev, port->write_urb->pipe);
951 usb_clear_halt(serial->dev, port->read_urb->pipe);
954 /* Initialising the write urb pool */
955 for (j = 0; j < NUM_URBS; ++j) {
956 urb = usb_alloc_urb(0, GFP_KERNEL);
957 mos7840_port->write_urb_pool[j] = urb;
960 dev_err(&port->dev, "No more urbs???\n");
964 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
966 if (!urb->transfer_buffer) {
968 mos7840_port->write_urb_pool[j] = NULL;
970 "%s-out of memory for urb buffers.\n",
976 /*****************************************************************************
977 * Initialize MCS7840 -- Write Init values to corresponding Registers
985 * 0x08 : SP1/2 Control Reg
986 *****************************************************************************/
988 /* NEED to check the following Block */
991 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
993 dbg("Reading Spreg failed");
997 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
999 dbg("writing Spreg failed");
1004 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1006 dbg("writing Spreg failed");
1009 /* End of block to be checked */
1012 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1015 dbg("Reading Controlreg failed");
1018 Data |= 0x08; /* Driver done bit */
1019 Data |= 0x20; /* rx_disable */
1020 status = mos7840_set_reg_sync(port,
1021 mos7840_port->ControlRegOffset, Data);
1023 dbg("writing Controlreg failed");
1026 /* do register settings here */
1027 /* Set all regs to the device default values. */
1028 /***********************************
1029 * First Disable all interrupts.
1030 ***********************************/
1032 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1034 dbg("disabling interrupts failed");
1037 /* Set FIFO_CONTROL_REGISTER to the default value */
1039 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1041 dbg("Writing FIFO_CONTROL_REGISTER failed");
1046 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1048 dbg("Writing FIFO_CONTROL_REGISTER failed");
1053 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1054 mos7840_port->shadowLCR = Data;
1057 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1058 mos7840_port->shadowMCR = Data;
1061 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1062 mos7840_port->shadowLCR = Data;
1064 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1065 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1068 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1071 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1074 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1076 Data = Data & ~SERIAL_LCR_DLAB;
1077 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1078 mos7840_port->shadowLCR = Data;
1080 /* clearing Bulkin and Bulkout Fifo */
1082 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1085 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1087 Data = Data & ~0x0c;
1088 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1089 /* Finally enable all interrupts */
1091 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1093 /* clearing rx_disable */
1095 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1097 Data = Data & ~0x20;
1098 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1103 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1106 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1109 /* Check to see if we've set up our endpoint info yet *
1110 * (can't set it up in mos7840_startup as the structures *
1111 * were not set up at that time.) */
1112 if (port0->open_ports == 1) {
1113 if (serial->port[0]->interrupt_in_buffer == NULL) {
1114 /* set up interrupt urb */
1115 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1117 usb_rcvintpipe(serial->dev,
1118 serial->port[0]->interrupt_in_endpointAddress),
1119 serial->port[0]->interrupt_in_buffer,
1120 serial->port[0]->interrupt_in_urb->
1121 transfer_buffer_length,
1122 mos7840_interrupt_callback,
1124 serial->port[0]->interrupt_in_urb->interval);
1126 /* start interrupt read for mos7840 *
1127 * will continue as long as mos7840 is connected */
1130 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1133 dev_err(&port->dev, "%s - Error %d submitting "
1134 "interrupt urb\n", __func__, response);
1141 /* see if we've set up our endpoint info yet *
1142 * (can't set it up in mos7840_startup as the *
1143 * structures were not set up at that time.) */
1145 dbg("port number is %d", port->number);
1146 dbg("serial number is %d", port->serial->minor);
1147 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1148 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1149 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1150 dbg("port's number in the device is %d", mos7840_port->port_num);
1151 mos7840_port->read_urb = port->read_urb;
1153 /* set up our bulk in urb */
1154 if ((serial->num_ports == 2)
1155 && ((((__u16)port->number -
1156 (__u16)(port->serial->minor)) % 2) != 0)) {
1157 usb_fill_bulk_urb(mos7840_port->read_urb,
1159 usb_rcvbulkpipe(serial->dev,
1160 (port->bulk_in_endpointAddress) + 2),
1161 port->bulk_in_buffer,
1162 mos7840_port->read_urb->transfer_buffer_length,
1163 mos7840_bulk_in_callback, mos7840_port);
1165 usb_fill_bulk_urb(mos7840_port->read_urb,
1167 usb_rcvbulkpipe(serial->dev,
1168 port->bulk_in_endpointAddress),
1169 port->bulk_in_buffer,
1170 mos7840_port->read_urb->transfer_buffer_length,
1171 mos7840_bulk_in_callback, mos7840_port);
1174 dbg("mos7840_open: bulkin endpoint is %d",
1175 port->bulk_in_endpointAddress);
1176 mos7840_port->read_urb_busy = true;
1177 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1179 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1180 __func__, response);
1181 mos7840_port->read_urb_busy = false;
1184 /* initialize our wait queues */
1185 init_waitqueue_head(&mos7840_port->wait_chase);
1186 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1188 /* initialize our icount structure */
1189 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1191 /* initialize our port settings */
1192 /* Must set to enable ints! */
1193 mos7840_port->shadowMCR = MCR_MASTER_IE;
1194 /* send a open port command */
1195 mos7840_port->open = 1;
1196 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1197 mos7840_port->icount.tx = 0;
1198 mos7840_port->icount.rx = 0;
1200 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
1201 serial, mos7840_port, port);
1206 /*****************************************************************************
1207 * mos7840_chars_in_buffer
1208 * this function is called by the tty driver when it wants to know how many
1209 * bytes of data we currently have outstanding in the port (data that has
1210 * been written, but hasn't made it out the port yet)
1211 * If successful, we return the number of bytes left to be written in the
1213 * Otherwise we return zero.
1214 *****************************************************************************/
1216 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1218 struct usb_serial_port *port = tty->driver_data;
1221 unsigned long flags;
1222 struct moschip_port *mos7840_port;
1224 if (mos7840_port_paranoia_check(port, __func__)) {
1225 dbg("%s", "Invalid port");
1229 mos7840_port = mos7840_get_port_private(port);
1230 if (mos7840_port == NULL)
1233 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1234 for (i = 0; i < NUM_URBS; ++i) {
1235 if (mos7840_port->busy[i]) {
1236 struct urb *urb = mos7840_port->write_urb_pool[i];
1237 chars += urb->transfer_buffer_length;
1240 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1241 dbg("%s - returns %d", __func__, chars);
1246 /*****************************************************************************
1248 * this function is called by the tty driver when a port is closed
1249 *****************************************************************************/
1251 static void mos7840_close(struct usb_serial_port *port)
1253 struct usb_serial *serial;
1254 struct moschip_port *mos7840_port;
1255 struct moschip_port *port0;
1259 if (mos7840_port_paranoia_check(port, __func__)) {
1260 dbg("%s", "Port Paranoia failed");
1264 serial = mos7840_get_usb_serial(port, __func__);
1266 dbg("%s", "Serial Paranoia failed");
1270 mos7840_port = mos7840_get_port_private(port);
1271 port0 = mos7840_get_port_private(serial->port[0]);
1273 if (mos7840_port == NULL || port0 == NULL)
1276 for (j = 0; j < NUM_URBS; ++j)
1277 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1279 /* Freeing Write URBs */
1280 for (j = 0; j < NUM_URBS; ++j) {
1281 if (mos7840_port->write_urb_pool[j]) {
1282 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1283 kfree(mos7840_port->write_urb_pool[j]->
1286 usb_free_urb(mos7840_port->write_urb_pool[j]);
1290 /* While closing port, shutdown all bulk read, write *
1291 * and interrupt read if they exists */
1293 if (mos7840_port->write_urb) {
1294 dbg("%s", "Shutdown bulk write");
1295 usb_kill_urb(mos7840_port->write_urb);
1297 if (mos7840_port->read_urb) {
1298 dbg("%s", "Shutdown bulk read");
1299 usb_kill_urb(mos7840_port->read_urb);
1300 mos7840_port->read_urb_busy = false;
1302 if ((&mos7840_port->control_urb)) {
1303 dbg("%s", "Shutdown control read");
1304 /*/ usb_kill_urb (mos7840_port->control_urb); */
1307 /* if(mos7840_port->ctrl_buf != NULL) */
1308 /* kfree(mos7840_port->ctrl_buf); */
1309 port0->open_ports--;
1310 dbg("mos7840_num_open_ports in close%d:in port%d",
1311 port0->open_ports, port->number);
1312 if (port0->open_ports == 0) {
1313 if (serial->port[0]->interrupt_in_urb) {
1314 dbg("%s", "Shutdown interrupt_in_urb");
1315 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1319 if (mos7840_port->write_urb) {
1320 /* if this urb had a transfer buffer already (old tx) free it */
1321 if (mos7840_port->write_urb->transfer_buffer != NULL)
1322 kfree(mos7840_port->write_urb->transfer_buffer);
1323 usb_free_urb(mos7840_port->write_urb);
1327 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1330 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1332 mos7840_port->open = 0;
1335 /************************************************************************
1337 * mos7840_block_until_chase_response
1339 * This function will block the close until one of the following:
1340 * 1. Response to our Chase comes from mos7840
1341 * 2. A timeout of 10 seconds without activity has expired
1342 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1344 ************************************************************************/
1346 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1347 struct moschip_port *mos7840_port)
1349 int timeout = msecs_to_jiffies(1000);
1354 count = mos7840_chars_in_buffer(tty);
1356 /* Check for Buffer status */
1360 /* Block the thread for a while */
1361 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1363 /* No activity.. count down section */
1366 dbg("%s - TIMEOUT", __func__);
1369 /* Reset timeout value back to seconds */
1376 /*****************************************************************************
1378 * this function sends a break to the port
1379 *****************************************************************************/
1380 static void mos7840_break(struct tty_struct *tty, int break_state)
1382 struct usb_serial_port *port = tty->driver_data;
1384 struct usb_serial *serial;
1385 struct moschip_port *mos7840_port;
1387 if (mos7840_port_paranoia_check(port, __func__)) {
1388 dbg("%s", "Port Paranoia failed");
1392 serial = mos7840_get_usb_serial(port, __func__);
1394 dbg("%s", "Serial Paranoia failed");
1398 mos7840_port = mos7840_get_port_private(port);
1400 if (mos7840_port == NULL)
1404 /* flush and block until tx is empty */
1405 mos7840_block_until_chase_response(tty, mos7840_port);
1407 if (break_state == -1)
1408 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1410 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1412 /* FIXME: no locking on shadowLCR anywhere in driver */
1413 mos7840_port->shadowLCR = data;
1414 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
1415 mos7840_port->shadowLCR);
1416 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1417 mos7840_port->shadowLCR);
1420 /*****************************************************************************
1421 * mos7840_write_room
1422 * this function is called by the tty driver when it wants to know how many
1423 * bytes of data we can accept for a specific port.
1424 * If successful, we return the amount of room that we have for this port
1425 * Otherwise we return a negative error number.
1426 *****************************************************************************/
1428 static int mos7840_write_room(struct tty_struct *tty)
1430 struct usb_serial_port *port = tty->driver_data;
1433 unsigned long flags;
1434 struct moschip_port *mos7840_port;
1436 if (mos7840_port_paranoia_check(port, __func__)) {
1437 dbg("%s", "Invalid port");
1438 dbg("%s", " mos7840_write_room:leaving ...........");
1442 mos7840_port = mos7840_get_port_private(port);
1443 if (mos7840_port == NULL) {
1444 dbg("%s", "mos7840_break:leaving ...........");
1448 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1449 for (i = 0; i < NUM_URBS; ++i) {
1450 if (!mos7840_port->busy[i])
1451 room += URB_TRANSFER_BUFFER_SIZE;
1453 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1455 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1456 dbg("%s - returns %d", __func__, room);
1461 /*****************************************************************************
1463 * this function is called by the tty driver when data should be written to
1465 * If successful, we return the number of bytes written, otherwise we
1466 * return a negative error number.
1467 *****************************************************************************/
1469 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1470 const unsigned char *data, int count)
1476 unsigned long flags;
1478 struct moschip_port *mos7840_port;
1479 struct usb_serial *serial;
1482 const unsigned char *current_position = data;
1483 unsigned char *data1;
1487 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1488 mos7840_port->shadowLCR = Data;
1489 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1490 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1491 mos7840_port->shadowLCR);
1494 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1495 /* mos7840_port->shadowLCR=Data;//Need to add later */
1497 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1498 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1501 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1503 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1504 dbg("mos7840_write:DLL value is %x", Data);
1507 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1508 dbg("mos7840_write:DLM value is %x", Data);
1510 Data = Data & ~SERIAL_LCR_DLAB;
1511 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1512 mos7840_port->shadowLCR);
1513 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1516 if (mos7840_port_paranoia_check(port, __func__)) {
1517 dbg("%s", "Port Paranoia failed");
1521 serial = port->serial;
1522 if (mos7840_serial_paranoia_check(serial, __func__)) {
1523 dbg("%s", "Serial Paranoia failed");
1527 mos7840_port = mos7840_get_port_private(port);
1528 if (mos7840_port == NULL) {
1529 dbg("%s", "mos7840_port is NULL");
1533 /* try to find a free urb in the list */
1536 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1537 for (i = 0; i < NUM_URBS; ++i) {
1538 if (!mos7840_port->busy[i]) {
1539 mos7840_port->busy[i] = 1;
1540 urb = mos7840_port->write_urb_pool[i];
1545 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1548 dbg("%s - no more free urbs", __func__);
1552 if (urb->transfer_buffer == NULL) {
1553 urb->transfer_buffer =
1554 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1556 if (urb->transfer_buffer == NULL) {
1557 dev_err_console(port, "%s no more kernel memory...\n",
1562 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1564 memcpy(urb->transfer_buffer, current_position, transfer_size);
1566 /* fill urb with data and submit */
1567 if ((serial->num_ports == 2)
1568 && ((((__u16)port->number -
1569 (__u16)(port->serial->minor)) % 2) != 0)) {
1570 usb_fill_bulk_urb(urb,
1572 usb_sndbulkpipe(serial->dev,
1573 (port->bulk_out_endpointAddress) + 2),
1574 urb->transfer_buffer,
1576 mos7840_bulk_out_data_callback, mos7840_port);
1578 usb_fill_bulk_urb(urb,
1580 usb_sndbulkpipe(serial->dev,
1581 port->bulk_out_endpointAddress),
1582 urb->transfer_buffer,
1584 mos7840_bulk_out_data_callback, mos7840_port);
1587 data1 = urb->transfer_buffer;
1588 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
1591 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1592 mos7840_port->led_flag = true;
1593 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1594 mod_timer(&mos7840_port->led_timer1,
1595 jiffies + msecs_to_jiffies(LED_ON_MS));
1598 /* send it down the pipe */
1599 status = usb_submit_urb(urb, GFP_ATOMIC);
1602 mos7840_port->busy[i] = 0;
1603 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1604 "with status = %d\n", __func__, status);
1605 bytes_sent = status;
1608 bytes_sent = transfer_size;
1609 mos7840_port->icount.tx += transfer_size;
1611 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
1617 /*****************************************************************************
1619 * this function is called by the tty driver when it wants to stop the data
1620 * being read from the port.
1621 *****************************************************************************/
1623 static void mos7840_throttle(struct tty_struct *tty)
1625 struct usb_serial_port *port = tty->driver_data;
1626 struct moschip_port *mos7840_port;
1629 if (mos7840_port_paranoia_check(port, __func__)) {
1630 dbg("%s", "Invalid port");
1634 dbg("- port %d", port->number);
1636 mos7840_port = mos7840_get_port_private(port);
1638 if (mos7840_port == NULL)
1641 if (!mos7840_port->open) {
1642 dbg("%s", "port not opened");
1646 /* if we are implementing XON/XOFF, send the stop character */
1648 unsigned char stop_char = STOP_CHAR(tty);
1649 status = mos7840_write(tty, port, &stop_char, 1);
1653 /* if we are implementing RTS/CTS, toggle that line */
1654 if (tty->termios->c_cflag & CRTSCTS) {
1655 mos7840_port->shadowMCR &= ~MCR_RTS;
1656 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1657 mos7840_port->shadowMCR);
1663 /*****************************************************************************
1664 * mos7840_unthrottle
1665 * this function is called by the tty driver when it wants to resume
1666 * the data being read from the port (called after mos7840_throttle is
1668 *****************************************************************************/
1669 static void mos7840_unthrottle(struct tty_struct *tty)
1671 struct usb_serial_port *port = tty->driver_data;
1673 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1675 if (mos7840_port_paranoia_check(port, __func__)) {
1676 dbg("%s", "Invalid port");
1680 if (mos7840_port == NULL)
1683 if (!mos7840_port->open) {
1684 dbg("%s - port not opened", __func__);
1688 /* if we are implementing XON/XOFF, send the start character */
1690 unsigned char start_char = START_CHAR(tty);
1691 status = mos7840_write(tty, port, &start_char, 1);
1696 /* if we are implementing RTS/CTS, toggle that line */
1697 if (tty->termios->c_cflag & CRTSCTS) {
1698 mos7840_port->shadowMCR |= MCR_RTS;
1699 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1700 mos7840_port->shadowMCR);
1706 static int mos7840_tiocmget(struct tty_struct *tty)
1708 struct usb_serial_port *port = tty->driver_data;
1709 struct moschip_port *mos7840_port;
1710 unsigned int result;
1714 mos7840_port = mos7840_get_port_private(port);
1716 if (mos7840_port == NULL)
1719 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1720 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1721 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1722 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1723 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1724 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1725 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1726 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1727 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1729 dbg("%s - 0x%04X", __func__, result);
1734 static int mos7840_tiocmset(struct tty_struct *tty,
1735 unsigned int set, unsigned int clear)
1737 struct usb_serial_port *port = tty->driver_data;
1738 struct moschip_port *mos7840_port;
1742 mos7840_port = mos7840_get_port_private(port);
1744 if (mos7840_port == NULL)
1747 /* FIXME: What locks the port registers ? */
1748 mcr = mos7840_port->shadowMCR;
1749 if (clear & TIOCM_RTS)
1751 if (clear & TIOCM_DTR)
1753 if (clear & TIOCM_LOOP)
1754 mcr &= ~MCR_LOOPBACK;
1756 if (set & TIOCM_RTS)
1758 if (set & TIOCM_DTR)
1760 if (set & TIOCM_LOOP)
1761 mcr |= MCR_LOOPBACK;
1763 mos7840_port->shadowMCR = mcr;
1765 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1767 dbg("setting MODEM_CONTROL_REGISTER Failed");
1774 /*****************************************************************************
1775 * mos7840_calc_baud_rate_divisor
1776 * this function calculates the proper baud rate divisor for the specified
1778 *****************************************************************************/
1779 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1782 dbg("%s - %d", __func__, baudRate);
1784 if (baudRate <= 115200) {
1785 *divisor = 115200 / baudRate;
1788 if ((baudRate > 115200) && (baudRate <= 230400)) {
1789 *divisor = 230400 / baudRate;
1790 *clk_sel_val = 0x10;
1791 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1792 *divisor = 403200 / baudRate;
1793 *clk_sel_val = 0x20;
1794 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1795 *divisor = 460800 / baudRate;
1796 *clk_sel_val = 0x30;
1797 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1798 *divisor = 806400 / baudRate;
1799 *clk_sel_val = 0x40;
1800 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1801 *divisor = 921600 / baudRate;
1802 *clk_sel_val = 0x50;
1803 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1804 *divisor = 1572864 / baudRate;
1805 *clk_sel_val = 0x60;
1806 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1807 *divisor = 3145728 / baudRate;
1808 *clk_sel_val = 0x70;
1814 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1815 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1816 *divisor = mos7840_divisor_table[i].Divisor;
1821 /* After trying for all the standard baud rates *
1822 * Try calculating the divisor for this baud rate */
1824 if (baudrate > 75 && baudrate < 230400) {
1825 /* get the divisor */
1826 custom = (__u16) (230400L / baudrate);
1828 /* Check for round off */
1829 round1 = (__u16) (2304000L / baudrate);
1830 round = (__u16) (round1 - (custom * 10));
1835 dbg(" Baud %d = %d", baudrate, custom);
1839 dbg("%s", " Baud calculation Failed...");
1844 /*****************************************************************************
1845 * mos7840_send_cmd_write_baud_rate
1846 * this function sends the proper command to change the baud rate of the
1848 *****************************************************************************/
1850 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1856 unsigned char number;
1858 struct usb_serial_port *port;
1860 if (mos7840_port == NULL)
1863 port = (struct usb_serial_port *)mos7840_port->port;
1864 if (mos7840_port_paranoia_check(port, __func__)) {
1865 dbg("%s", "Invalid port");
1869 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1870 dbg("%s", "Invalid Serial");
1874 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1876 dbg("%s - port = %d, baud = %d", __func__,
1877 mos7840_port->port->number, baudRate);
1878 /* reset clk_uart_sel in spregOffset */
1879 if (baudRate > 115200) {
1880 #ifdef HW_flow_control
1881 /* NOTE: need to see the pther register to modify */
1882 /* setting h/w flow control bit to 1 */
1884 mos7840_port->shadowMCR = Data;
1885 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1888 dbg("Writing spreg failed in set_serial_baud");
1894 #ifdef HW_flow_control
1895 /* setting h/w flow control bit to 0 */
1897 mos7840_port->shadowMCR = Data;
1898 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1901 dbg("Writing spreg failed in set_serial_baud");
1908 if (1) { /* baudRate <= 115200) */
1911 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1913 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1916 dbg("reading spreg failed in set_serial_baud");
1919 Data = (Data & 0x8f) | clk_sel_val;
1920 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1923 dbg("Writing spreg failed in set_serial_baud");
1926 /* Calculate the Divisor */
1929 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1932 /* Enable access to divisor latch */
1933 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1934 mos7840_port->shadowLCR = Data;
1935 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1937 /* Write the divisor */
1938 Data = (unsigned char)(divisor & 0xff);
1939 dbg("set_serial_baud Value to write DLL is %x", Data);
1940 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1942 Data = (unsigned char)((divisor & 0xff00) >> 8);
1943 dbg("set_serial_baud Value to write DLM is %x", Data);
1944 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1946 /* Disable access to divisor latch */
1947 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1948 mos7840_port->shadowLCR = Data;
1949 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1955 /*****************************************************************************
1956 * mos7840_change_port_settings
1957 * This routine is called to set the UART on the device to match
1958 * the specified new settings.
1959 *****************************************************************************/
1961 static void mos7840_change_port_settings(struct tty_struct *tty,
1962 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1972 struct usb_serial_port *port;
1973 struct usb_serial *serial;
1975 if (mos7840_port == NULL)
1978 port = (struct usb_serial_port *)mos7840_port->port;
1980 if (mos7840_port_paranoia_check(port, __func__)) {
1981 dbg("%s", "Invalid port");
1985 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1986 dbg("%s", "Invalid Serial");
1990 serial = port->serial;
1992 dbg("%s - port %d", __func__, mos7840_port->port->number);
1994 if (!mos7840_port->open) {
1995 dbg("%s - port not opened", __func__);
2001 lParity = LCR_PAR_NONE;
2003 cflag = tty->termios->c_cflag;
2004 iflag = tty->termios->c_iflag;
2006 /* Change the number of bits */
2007 if (cflag & CSIZE) {
2008 switch (cflag & CSIZE) {
2026 /* Change the Parity bit */
2027 if (cflag & PARENB) {
2028 if (cflag & PARODD) {
2029 lParity = LCR_PAR_ODD;
2030 dbg("%s - parity = odd", __func__);
2032 lParity = LCR_PAR_EVEN;
2033 dbg("%s - parity = even", __func__);
2037 dbg("%s - parity = none", __func__);
2041 lParity = lParity | 0x20;
2043 /* Change the Stop bit */
2044 if (cflag & CSTOPB) {
2046 dbg("%s - stop bits = 2", __func__);
2049 dbg("%s - stop bits = 1", __func__);
2052 /* Update the LCR with the correct value */
2053 mos7840_port->shadowLCR &=
2054 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2055 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2057 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
2058 mos7840_port->shadowLCR);
2059 /* Disable Interrupts */
2061 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2064 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2067 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2069 /* Send the updated LCR value to the mos7840 */
2070 Data = mos7840_port->shadowLCR;
2072 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2075 mos7840_port->shadowMCR = Data;
2076 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2078 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2080 /* set up the MCR register and send it to the mos7840 */
2082 mos7840_port->shadowMCR = MCR_MASTER_IE;
2084 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2086 if (cflag & CRTSCTS)
2087 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2089 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", "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_busy == false) {
2111 mos7840_port->read_urb_busy = true;
2112 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2114 dbg("usb_submit_urb(read bulk) failed, status = %d",
2116 mos7840_port->read_urb_busy = false;
2119 wake_up(&mos7840_port->delta_msr_wait);
2120 mos7840_port->delta_msr_cond = 1;
2121 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
2122 mos7840_port->shadowLCR);
2125 /*****************************************************************************
2126 * mos7840_set_termios
2127 * this function is called by the tty driver when it wants to change
2128 * the termios structure
2129 *****************************************************************************/
2131 static void mos7840_set_termios(struct tty_struct *tty,
2132 struct usb_serial_port *port,
2133 struct ktermios *old_termios)
2137 struct usb_serial *serial;
2138 struct moschip_port *mos7840_port;
2140 if (mos7840_port_paranoia_check(port, __func__)) {
2141 dbg("%s", "Invalid port");
2145 serial = port->serial;
2147 if (mos7840_serial_paranoia_check(serial, __func__)) {
2148 dbg("%s", "Invalid Serial");
2152 mos7840_port = mos7840_get_port_private(port);
2154 if (mos7840_port == NULL)
2157 if (!mos7840_port->open) {
2158 dbg("%s - port not opened", __func__);
2162 dbg("%s", "setting termios - ");
2164 cflag = tty->termios->c_cflag;
2166 dbg("%s - clfag %08x iflag %08x", __func__,
2167 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2168 dbg("%s - old clfag %08x old iflag %08x", __func__,
2169 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2170 dbg("%s - port %d", __func__, port->number);
2172 /* change the port settings to the new ones specified */
2174 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2176 if (!mos7840_port->read_urb) {
2177 dbg("%s", "URB KILLED !!!!!");
2181 if (mos7840_port->read_urb_busy == false) {
2182 mos7840_port->read_urb_busy = true;
2183 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2185 dbg("usb_submit_urb(read bulk) failed, status = %d",
2187 mos7840_port->read_urb_busy = false;
2192 /*****************************************************************************
2193 * mos7840_get_lsr_info - get line status register info
2195 * Purpose: Let user call ioctl() to get info when the UART physically
2196 * is emptied. On bus types like RS485, the transmitter must
2197 * release the bus after transmitting. This must be done when
2198 * the transmit shift register is empty, not be done when the
2199 * transmit holding register is empty. This functionality
2200 * allows an RS485 driver to be written in user space.
2201 *****************************************************************************/
2203 static int mos7840_get_lsr_info(struct tty_struct *tty,
2204 unsigned int __user *value)
2207 unsigned int result = 0;
2209 count = mos7840_chars_in_buffer(tty);
2211 dbg("%s -- Empty", __func__);
2212 result = TIOCSER_TEMT;
2215 if (copy_to_user(value, &result, sizeof(int)))
2220 /*****************************************************************************
2221 * mos7840_get_serial_info
2222 * function to get information about serial port
2223 *****************************************************************************/
2225 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2226 struct serial_struct __user *retinfo)
2228 struct serial_struct tmp;
2230 if (mos7840_port == NULL)
2236 memset(&tmp, 0, sizeof(tmp));
2238 tmp.type = PORT_16550A;
2239 tmp.line = mos7840_port->port->serial->minor;
2240 tmp.port = mos7840_port->port->number;
2242 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2243 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2244 tmp.baud_base = 9600;
2245 tmp.close_delay = 5 * HZ;
2246 tmp.closing_wait = 30 * HZ;
2248 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2253 static int mos7840_get_icount(struct tty_struct *tty,
2254 struct serial_icounter_struct *icount)
2256 struct usb_serial_port *port = tty->driver_data;
2257 struct moschip_port *mos7840_port;
2258 struct async_icount cnow;
2260 mos7840_port = mos7840_get_port_private(port);
2261 cnow = mos7840_port->icount;
2264 icount->cts = cnow.cts;
2265 icount->dsr = cnow.dsr;
2266 icount->rng = cnow.rng;
2267 icount->dcd = cnow.dcd;
2268 icount->rx = cnow.rx;
2269 icount->tx = cnow.tx;
2270 icount->frame = cnow.frame;
2271 icount->overrun = cnow.overrun;
2272 icount->parity = cnow.parity;
2273 icount->brk = cnow.brk;
2274 icount->buf_overrun = cnow.buf_overrun;
2276 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2277 port->number, icount->rx, icount->tx);
2281 /*****************************************************************************
2283 * this function handles any ioctl calls to the driver
2284 *****************************************************************************/
2286 static int mos7840_ioctl(struct tty_struct *tty,
2287 unsigned int cmd, unsigned long arg)
2289 struct usb_serial_port *port = tty->driver_data;
2290 void __user *argp = (void __user *)arg;
2291 struct moschip_port *mos7840_port;
2293 struct async_icount cnow;
2294 struct async_icount cprev;
2296 if (mos7840_port_paranoia_check(port, __func__)) {
2297 dbg("%s", "Invalid port");
2301 mos7840_port = mos7840_get_port_private(port);
2303 if (mos7840_port == NULL)
2306 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2309 /* return number of bytes available */
2312 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2313 return mos7840_get_lsr_info(tty, argp);
2316 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
2317 return mos7840_get_serial_info(mos7840_port, argp);
2320 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
2324 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
2325 cprev = mos7840_port->icount;
2327 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2328 mos7840_port->delta_msr_cond = 0;
2329 wait_event_interruptible(mos7840_port->delta_msr_wait,
2331 delta_msr_cond == 1));
2333 /* see if a signal did it */
2334 if (signal_pending(current))
2335 return -ERESTARTSYS;
2336 cnow = mos7840_port->icount;
2338 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2339 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2340 return -EIO; /* no change => error */
2341 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2342 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2343 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2344 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2355 return -ENOIOCTLCMD;
2358 static int mos7810_check(struct usb_serial *serial)
2360 int i, pass_count = 0;
2361 __u16 data = 0, mcr_data = 0;
2362 __u16 test_pattern = 0x55AA;
2364 /* Store MCR setting */
2365 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2366 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2367 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2369 for (i = 0; i < 16; i++) {
2370 /* Send the 1-bit test pattern out to MCS7810 test pin */
2371 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2372 MCS_WRREQ, MCS_WR_RTYPE,
2373 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2374 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2376 /* Read the test pattern back */
2377 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2378 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2379 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2381 /* If this is a MCS7810 device, both test patterns must match */
2382 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2388 /* Restore MCR setting */
2389 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2390 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2391 0, MOS_WDR_TIMEOUT);
2393 if (pass_count == 16)
2399 static int mos7840_calc_num_ports(struct usb_serial *serial)
2402 int mos7840_num_ports;
2404 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2405 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2406 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2408 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2409 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2410 device_type = serial->dev->descriptor.idProduct;
2412 /* For a MCS7840 device GPIO0 must be set to 1 */
2413 if ((data & 0x01) == 1)
2414 device_type = MOSCHIP_DEVICE_ID_7840;
2415 else if (mos7810_check(serial))
2416 device_type = MOSCHIP_DEVICE_ID_7810;
2418 device_type = MOSCHIP_DEVICE_ID_7820;
2421 mos7840_num_ports = (device_type >> 4) & 0x000F;
2422 serial->num_bulk_in = mos7840_num_ports;
2423 serial->num_bulk_out = mos7840_num_ports;
2424 serial->num_ports = mos7840_num_ports;
2426 return mos7840_num_ports;
2429 /****************************************************************************
2431 ****************************************************************************/
2433 static int mos7840_startup(struct usb_serial *serial)
2435 struct moschip_port *mos7840_port;
2436 struct usb_device *dev;
2441 dbg("%s", "Invalid Handler");
2447 /* we set up the pointers to the endpoints in the mos7840_open *
2448 * function, as the structures aren't created yet. */
2450 /* set up port private structures */
2451 for (i = 0; i < serial->num_ports; ++i) {
2452 dbg ("mos7840_startup: configuring port %d............", i);
2453 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2454 if (mos7840_port == NULL) {
2455 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2457 i--; /* don't follow NULL pointer cleaning up */
2461 /* Initialize all port interrupt end point to port 0 int
2462 * endpoint. Our device has only one interrupt end point
2463 * common to all port */
2465 mos7840_port->port = serial->port[i];
2466 mos7840_set_port_private(serial->port[i], mos7840_port);
2467 spin_lock_init(&mos7840_port->pool_lock);
2469 /* minor is not initialised until later by
2470 * usb-serial.c:get_free_serial() and cannot therefore be used
2471 * to index device instances */
2472 mos7840_port->port_num = i + 1;
2473 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2474 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2475 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2476 dbg ("serial->minor = %d", serial->minor);
2478 if (mos7840_port->port_num == 1) {
2479 mos7840_port->SpRegOffset = 0x0;
2480 mos7840_port->ControlRegOffset = 0x1;
2481 mos7840_port->DcrRegOffset = 0x4;
2482 } else if ((mos7840_port->port_num == 2)
2483 && (serial->num_ports == 4)) {
2484 mos7840_port->SpRegOffset = 0x8;
2485 mos7840_port->ControlRegOffset = 0x9;
2486 mos7840_port->DcrRegOffset = 0x16;
2487 } else if ((mos7840_port->port_num == 2)
2488 && (serial->num_ports == 2)) {
2489 mos7840_port->SpRegOffset = 0xa;
2490 mos7840_port->ControlRegOffset = 0xb;
2491 mos7840_port->DcrRegOffset = 0x19;
2492 } else if ((mos7840_port->port_num == 3)
2493 && (serial->num_ports == 4)) {
2494 mos7840_port->SpRegOffset = 0xa;
2495 mos7840_port->ControlRegOffset = 0xb;
2496 mos7840_port->DcrRegOffset = 0x19;
2497 } else if ((mos7840_port->port_num == 4)
2498 && (serial->num_ports == 4)) {
2499 mos7840_port->SpRegOffset = 0xc;
2500 mos7840_port->ControlRegOffset = 0xd;
2501 mos7840_port->DcrRegOffset = 0x1c;
2503 mos7840_dump_serial_port(mos7840_port);
2504 mos7840_set_port_private(serial->port[i], mos7840_port);
2506 /* enable rx_disable bit in control register */
2507 status = mos7840_get_reg_sync(serial->port[i],
2508 mos7840_port->ControlRegOffset, &Data);
2510 dbg("Reading ControlReg failed status-0x%x", status);
2513 dbg("ControlReg Reading success val is %x, status%d",
2515 Data |= 0x08; /* setting driver done bit */
2516 Data |= 0x04; /* sp1_bit to have cts change reflect in
2519 /* Data |= 0x20; //rx_disable bit */
2520 status = mos7840_set_reg_sync(serial->port[i],
2521 mos7840_port->ControlRegOffset, Data);
2523 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
2526 dbg("ControlReg Writing success(rx_disable) status%d",
2529 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2532 status = mos7840_set_reg_sync(serial->port[i],
2533 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2535 dbg("Writing DCR0 failed status-0x%x", status);
2538 dbg("DCR0 Writing success status%d", status);
2541 status = mos7840_set_reg_sync(serial->port[i],
2542 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2544 dbg("Writing DCR1 failed status-0x%x", status);
2547 dbg("DCR1 Writing success status%d", status);
2550 status = mos7840_set_reg_sync(serial->port[i],
2551 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2553 dbg("Writing DCR2 failed status-0x%x", status);
2556 dbg("DCR2 Writing success status%d", status);
2558 /* write values in clkstart0x0 and clkmulti 0x20 */
2560 status = mos7840_set_reg_sync(serial->port[i],
2561 CLK_START_VALUE_REGISTER, Data);
2563 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
2566 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
2569 status = mos7840_set_reg_sync(serial->port[i],
2570 CLK_MULTI_REGISTER, Data);
2572 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
2576 dbg("CLK_MULTI_REGISTER Writing success status%d",
2579 /* write value 0x0 to scratchpad register */
2581 status = mos7840_set_uart_reg(serial->port[i],
2582 SCRATCH_PAD_REGISTER, Data);
2584 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
2588 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
2591 /* Zero Length flag register */
2592 if ((mos7840_port->port_num != 1)
2593 && (serial->num_ports == 2)) {
2596 status = mos7840_set_reg_sync(serial->port[i],
2598 ((__u16)mos7840_port->port_num)), Data);
2599 dbg("ZLIP offset %x",
2601 ((__u16) mos7840_port->port_num)));
2603 dbg("Writing ZLP_REG%d failed status-0x%x",
2607 dbg("ZLP_REG%d Writing success status%d",
2611 status = mos7840_set_reg_sync(serial->port[i],
2613 ((__u16)mos7840_port->port_num) - 0x1), Data);
2614 dbg("ZLIP offset %x",
2616 ((__u16) mos7840_port->port_num) - 0x1));
2618 dbg("Writing ZLP_REG%d failed status-0x%x",
2622 dbg("ZLP_REG%d Writing success status%d",
2626 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2627 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2628 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2630 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2631 !mos7840_port->dr) {
2636 mos7840_port->has_led = false;
2638 /* Initialize LED timers */
2639 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2640 mos7840_port->has_led = true;
2642 init_timer(&mos7840_port->led_timer1);
2643 mos7840_port->led_timer1.function = mos7840_led_off;
2644 mos7840_port->led_timer1.expires =
2645 jiffies + msecs_to_jiffies(LED_ON_MS);
2646 mos7840_port->led_timer1.data =
2647 (unsigned long)mos7840_port;
2649 init_timer(&mos7840_port->led_timer2);
2650 mos7840_port->led_timer2.function =
2651 mos7840_led_flag_off;
2652 mos7840_port->led_timer2.expires =
2653 jiffies + msecs_to_jiffies(LED_OFF_MS);
2654 mos7840_port->led_timer2.data =
2655 (unsigned long)mos7840_port;
2657 mos7840_port->led_flag = false;
2660 mos7840_set_led_sync(serial->port[i],
2661 MODEM_CONTROL_REGISTER, 0x0300);
2664 dbg ("mos7840_startup: all ports configured...........");
2666 /* Zero Length flag enable */
2668 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2670 dbg("Writing ZLP_REG5 failed status-0x%x", status);
2673 dbg("ZLP_REG5 Writing success status%d", status);
2675 /* setting configuration feature to one */
2676 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2677 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT);
2680 for (/* nothing */; i >= 0; i--) {
2681 mos7840_port = mos7840_get_port_private(serial->port[i]);
2683 kfree(mos7840_port->dr);
2684 kfree(mos7840_port->ctrl_buf);
2685 usb_free_urb(mos7840_port->control_urb);
2686 kfree(mos7840_port);
2687 serial->port[i] = NULL;
2692 /****************************************************************************
2693 * mos7840_disconnect
2694 * This function is called whenever the device is removed from the usb bus.
2695 ****************************************************************************/
2697 static void mos7840_disconnect(struct usb_serial *serial)
2700 unsigned long flags;
2701 struct moschip_port *mos7840_port;
2704 dbg("%s", "Invalid Handler");
2708 /* check for the ports to be closed,close the ports and disconnect */
2710 /* free private structure allocated for serial port *
2711 * stop reads and writes on all ports */
2713 for (i = 0; i < serial->num_ports; ++i) {
2714 mos7840_port = mos7840_get_port_private(serial->port[i]);
2715 dbg ("mos7840_port %d = %p", i, mos7840_port);
2717 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2718 mos7840_port->zombie = 1;
2719 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2720 usb_kill_urb(mos7840_port->control_urb);
2725 /****************************************************************************
2727 * This function is called when the usb_serial structure is freed.
2728 ****************************************************************************/
2730 static void mos7840_release(struct usb_serial *serial)
2733 struct moschip_port *mos7840_port;
2736 dbg("%s", "Invalid Handler");
2740 /* check for the ports to be closed,close the ports and disconnect */
2742 /* free private structure allocated for serial port *
2743 * stop reads and writes on all ports */
2745 for (i = 0; i < serial->num_ports; ++i) {
2746 mos7840_port = mos7840_get_port_private(serial->port[i]);
2747 dbg("mos7840_port %d = %p", i, mos7840_port);
2749 if (mos7840_port->has_led) {
2751 mos7840_set_led_sync(mos7840_port->port,
2752 MODEM_CONTROL_REGISTER, 0x0300);
2754 del_timer_sync(&mos7840_port->led_timer1);
2755 del_timer_sync(&mos7840_port->led_timer2);
2757 kfree(mos7840_port->ctrl_buf);
2758 kfree(mos7840_port->dr);
2759 kfree(mos7840_port);
2764 static struct usb_serial_driver moschip7840_4port_device = {
2766 .owner = THIS_MODULE,
2769 .description = DRIVER_DESC,
2770 .id_table = id_table,
2772 .open = mos7840_open,
2773 .close = mos7840_close,
2774 .write = mos7840_write,
2775 .write_room = mos7840_write_room,
2776 .chars_in_buffer = mos7840_chars_in_buffer,
2777 .throttle = mos7840_throttle,
2778 .unthrottle = mos7840_unthrottle,
2779 .calc_num_ports = mos7840_calc_num_ports,
2780 #ifdef MCSSerialProbe
2781 .probe = mos7840_serial_probe,
2783 .ioctl = mos7840_ioctl,
2784 .set_termios = mos7840_set_termios,
2785 .break_ctl = mos7840_break,
2786 .tiocmget = mos7840_tiocmget,
2787 .tiocmset = mos7840_tiocmset,
2788 .get_icount = mos7840_get_icount,
2789 .attach = mos7840_startup,
2790 .disconnect = mos7840_disconnect,
2791 .release = mos7840_release,
2792 .read_bulk_callback = mos7840_bulk_in_callback,
2793 .read_int_callback = mos7840_interrupt_callback,
2796 static struct usb_serial_driver * const serial_drivers[] = {
2797 &moschip7840_4port_device, NULL
2800 module_usb_serial_driver(serial_drivers, id_table);
2802 MODULE_DESCRIPTION(DRIVER_DESC);
2803 MODULE_LICENSE("GPL");
2805 module_param(debug, bool, S_IRUGO | S_IWUSR);
2806 MODULE_PARM_DESC(debug, "Debug enabled or not");