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 */
221 __u8 shadowLCR; /* last LCR value received */
222 __u8 shadowMCR; /* last MCR value received */
226 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
227 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
229 struct async_icount icount;
230 struct usb_serial_port *port; /* loop back to the owner of this object */
234 __u8 ControlRegOffset;
236 /* for processing control URBS in interrupt context */
237 struct urb *control_urb;
238 struct usb_ctrlrequest *dr;
242 spinlock_t pool_lock;
243 struct urb *write_urb_pool[NUM_URBS];
247 /* For device(s) with LED indicator */
250 struct timer_list led_timer1; /* Timer for LED on */
251 struct timer_list led_timer2; /* Timer for LED off */
255 * mos7840_set_reg_sync
256 * To set the Control register by calling usb_fill_control_urb function
257 * by passing usb_sndctrlpipe function as parameter.
260 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
263 struct usb_device *dev = port->serial->dev;
265 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
267 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
268 MCS_WR_RTYPE, val, reg, NULL, 0,
273 * mos7840_get_reg_sync
274 * To set the Uart register by calling usb_fill_control_urb function by
275 * passing usb_rcvctrlpipe function as parameter.
278 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
281 struct usb_device *dev = port->serial->dev;
285 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
289 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
290 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
293 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
300 * mos7840_set_uart_reg
301 * To set the Uart register by calling usb_fill_control_urb function by
302 * passing usb_sndctrlpipe function as parameter.
305 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
309 struct usb_device *dev = port->serial->dev;
311 /* For the UART control registers, the application number need
313 if (port->serial->num_ports == 4) {
314 val |= (((__u16) port->number -
315 (__u16) (port->serial->minor)) + 1) << 8;
317 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
318 val |= (((__u16) port->number -
319 (__u16) (port->serial->minor)) + 1) << 8;
321 val |= (((__u16) port->number -
322 (__u16) (port->serial->minor)) + 2) << 8;
325 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
326 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
327 MCS_WR_RTYPE, val, reg, NULL, 0,
333 * mos7840_get_uart_reg
334 * To set the Control register by calling usb_fill_control_urb function
335 * by passing usb_rcvctrlpipe function as parameter.
337 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
340 struct usb_device *dev = port->serial->dev;
345 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
349 /* Wval is same as application number */
350 if (port->serial->num_ports == 4) {
352 (((__u16) port->number - (__u16) (port->serial->minor)) +
355 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
356 Wval = (((__u16) port->number -
357 (__u16) (port->serial->minor)) + 1) << 8;
359 Wval = (((__u16) port->number -
360 (__u16) (port->serial->minor)) + 2) << 8;
363 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
364 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
365 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
373 static void mos7840_dump_serial_port(struct usb_serial_port *port,
374 struct moschip_port *mos7840_port)
377 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
378 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
379 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
383 /************************************************************************/
384 /************************************************************************/
385 /* I N T E R F A C E F U N C T I O N S */
386 /* I N T E R F A C E F U N C T I O N S */
387 /************************************************************************/
388 /************************************************************************/
390 static inline void mos7840_set_port_private(struct usb_serial_port *port,
391 struct moschip_port *data)
393 usb_set_serial_port_data(port, (void *)data);
396 static inline struct moschip_port *mos7840_get_port_private(struct
400 return (struct moschip_port *)usb_get_serial_port_data(port);
403 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
405 struct moschip_port *mos7840_port;
406 struct async_icount *icount;
408 icount = &mos7840_port->icount;
410 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
412 icount = &mos7840_port->icount;
414 /* update input line counters */
415 if (new_msr & MOS_MSR_DELTA_CTS) {
419 if (new_msr & MOS_MSR_DELTA_DSR) {
423 if (new_msr & MOS_MSR_DELTA_CD) {
427 if (new_msr & MOS_MSR_DELTA_RI) {
434 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
436 struct async_icount *icount;
438 if (new_lsr & SERIAL_LSR_BI) {
440 * Parity and Framing errors only count if they
441 * occur exclusive of a break being
444 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
447 /* update input line counters */
448 icount = &port->icount;
449 if (new_lsr & SERIAL_LSR_BI) {
453 if (new_lsr & SERIAL_LSR_OE) {
457 if (new_lsr & SERIAL_LSR_PE) {
461 if (new_lsr & SERIAL_LSR_FE) {
467 /************************************************************************/
468 /************************************************************************/
469 /* U S B C A L L B A C K F U N C T I O N S */
470 /* U S B C A L L B A C K F U N C T I O N S */
471 /************************************************************************/
472 /************************************************************************/
474 static void mos7840_control_callback(struct urb *urb)
477 struct moschip_port *mos7840_port;
478 struct device *dev = &urb->dev->dev;
480 int status = urb->status;
482 mos7840_port = urb->context;
491 /* this urb is terminated, clean up */
492 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
495 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
499 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
500 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
501 mos7840_port->MsrLsr, mos7840_port->port_num);
502 data = urb->transfer_buffer;
503 regval = (__u8) data[0];
504 dev_dbg(dev, "%s data is %x\n", __func__, regval);
505 if (mos7840_port->MsrLsr == 0)
506 mos7840_handle_new_msr(mos7840_port, regval);
507 else if (mos7840_port->MsrLsr == 1)
508 mos7840_handle_new_lsr(mos7840_port, regval);
511 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
514 struct usb_device *dev = mcs->port->serial->dev;
515 struct usb_ctrlrequest *dr = mcs->dr;
516 unsigned char *buffer = mcs->ctrl_buf;
519 dr->bRequestType = MCS_RD_RTYPE;
520 dr->bRequest = MCS_RDREQ;
521 dr->wValue = cpu_to_le16(Wval); /* 0 */
522 dr->wIndex = cpu_to_le16(reg);
523 dr->wLength = cpu_to_le16(2);
525 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
526 (unsigned char *)dr, buffer, 2,
527 mos7840_control_callback, mcs);
528 mcs->control_urb->transfer_buffer_length = 2;
529 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
533 static void mos7840_set_led_callback(struct urb *urb)
535 switch (urb->status) {
542 /* This urb is terminated, clean up */
543 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
544 __func__, urb->status);
547 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
548 __func__, urb->status);
552 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
555 struct usb_device *dev = mcs->port->serial->dev;
556 struct usb_ctrlrequest *dr = mcs->dr;
558 dr->bRequestType = MCS_WR_RTYPE;
559 dr->bRequest = MCS_WRREQ;
560 dr->wValue = cpu_to_le16(wval);
561 dr->wIndex = cpu_to_le16(reg);
562 dr->wLength = cpu_to_le16(0);
564 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
565 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
567 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
570 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
573 struct usb_device *dev = port->serial->dev;
575 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
576 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
579 static void mos7840_led_off(unsigned long arg)
581 struct moschip_port *mcs = (struct moschip_port *) arg;
584 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
585 mod_timer(&mcs->led_timer2,
586 jiffies + msecs_to_jiffies(LED_OFF_MS));
589 static void mos7840_led_flag_off(unsigned long arg)
591 struct moschip_port *mcs = (struct moschip_port *) arg;
593 mcs->led_flag = false;
596 /*****************************************************************************
597 * mos7840_interrupt_callback
598 * this is the callback function for when we have received data on the
599 * interrupt endpoint.
600 *****************************************************************************/
602 static void mos7840_interrupt_callback(struct urb *urb)
606 struct moschip_port *mos7840_port;
607 struct usb_serial *serial;
612 __u16 wval, wreg = 0;
613 int status = urb->status;
622 /* this urb is terminated, clean up */
623 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
627 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
632 length = urb->actual_length;
633 data = urb->transfer_buffer;
635 serial = urb->context;
637 /* Moschip get 5 bytes
638 * Byte 1 IIR Port 1 (port.number is 0)
639 * Byte 2 IIR Port 2 (port.number is 1)
640 * Byte 3 IIR Port 3 (port.number is 2)
641 * Byte 4 IIR Port 4 (port.number is 3)
642 * Byte 5 FIFO status for both */
644 if (length && length > 5) {
645 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
649 sp[0] = (__u8) data[0];
650 sp[1] = (__u8) data[1];
651 sp[2] = (__u8) data[2];
652 sp[3] = (__u8) data[3];
655 for (i = 0; i < serial->num_ports; i++) {
656 mos7840_port = mos7840_get_port_private(serial->port[i]);
658 (((__u16) serial->port[i]->number -
659 (__u16) (serial->minor)) + 1) << 8;
660 if (mos7840_port->open) {
662 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
664 switch (sp[i] & 0x0f) {
666 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
667 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
668 mos7840_port->MsrLsr = 1;
669 wreg = LINE_STATUS_REGISTER;
672 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
673 mos7840_port->MsrLsr = 0;
674 wreg = MODEM_STATUS_REGISTER;
677 spin_lock(&mos7840_port->pool_lock);
678 if (!mos7840_port->zombie) {
679 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
681 spin_unlock(&mos7840_port->pool_lock);
684 spin_unlock(&mos7840_port->pool_lock);
689 /* the completion handler for the control urb will resubmit */
692 result = usb_submit_urb(urb, GFP_ATOMIC);
694 dev_err(&urb->dev->dev,
695 "%s - Error %d submitting interrupt urb\n",
700 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
701 const char *function)
704 pr_debug("%s - port == NULL\n", function);
708 pr_debug("%s - port->serial == NULL\n", function);
715 /* Inline functions to check the sanity of a pointer that is passed to us */
716 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
717 const char *function)
720 pr_debug("%s - serial == NULL\n", function);
724 pr_debug("%s - serial->type == NULL!\n", function);
731 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
732 const char *function)
734 /* if no port was specified, or it fails a paranoia check */
736 mos7840_port_paranoia_check(port, function) ||
737 mos7840_serial_paranoia_check(port->serial, function)) {
738 /* then say that we don't have a valid usb_serial thing,
739 * which will end up genrating -ENODEV return values */
746 /*****************************************************************************
747 * mos7840_bulk_in_callback
748 * this is the callback function for when we have received data on the
750 *****************************************************************************/
752 static void mos7840_bulk_in_callback(struct urb *urb)
756 struct usb_serial *serial;
757 struct usb_serial_port *port;
758 struct moschip_port *mos7840_port;
759 struct tty_struct *tty;
760 int status = urb->status;
762 mos7840_port = urb->context;
767 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
768 mos7840_port->read_urb_busy = false;
772 port = mos7840_port->port;
773 if (mos7840_port_paranoia_check(port, __func__)) {
774 mos7840_port->read_urb_busy = false;
778 serial = mos7840_get_usb_serial(port, __func__);
780 mos7840_port->read_urb_busy = false;
784 data = urb->transfer_buffer;
785 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
787 if (urb->actual_length) {
788 tty = tty_port_tty_get(&mos7840_port->port->port);
790 tty_insert_flip_string(tty, data, urb->actual_length);
791 tty_flip_buffer_push(tty);
794 mos7840_port->icount.rx += urb->actual_length;
796 dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx);
799 if (!mos7840_port->read_urb) {
800 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
801 mos7840_port->read_urb_busy = false;
806 if (mos7840_port->has_led && !mos7840_port->led_flag) {
807 mos7840_port->led_flag = true;
808 mos7840_set_led_async(mos7840_port, 0x0301,
809 MODEM_CONTROL_REGISTER);
810 mod_timer(&mos7840_port->led_timer1,
811 jiffies + msecs_to_jiffies(LED_ON_MS));
814 mos7840_port->read_urb_busy = true;
815 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
818 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
819 mos7840_port->read_urb_busy = false;
823 /*****************************************************************************
824 * mos7840_bulk_out_data_callback
825 * this is the callback function for when we have finished sending
826 * serial data on the bulk out endpoint.
827 *****************************************************************************/
829 static void mos7840_bulk_out_data_callback(struct urb *urb)
831 struct moschip_port *mos7840_port;
832 struct usb_serial_port *port;
833 struct tty_struct *tty;
834 int status = urb->status;
837 mos7840_port = urb->context;
838 port = mos7840_port->port;
839 spin_lock(&mos7840_port->pool_lock);
840 for (i = 0; i < NUM_URBS; i++) {
841 if (urb == mos7840_port->write_urb_pool[i]) {
842 mos7840_port->busy[i] = 0;
846 spin_unlock(&mos7840_port->pool_lock);
849 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
853 if (mos7840_port_paranoia_check(port, __func__))
856 tty = tty_port_tty_get(&port->port);
857 if (tty && mos7840_port->open)
863 /************************************************************************/
864 /* 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 */
865 /************************************************************************/
866 #ifdef MCSSerialProbe
867 static int mos7840_serial_probe(struct usb_serial *serial,
868 const struct usb_device_id *id)
871 /*need to implement the mode_reg reading and updating\
872 structures usb_serial_ device_type\
873 (i.e num_ports, num_bulkin,bulkout etc) */
874 /* Also we can update the changes attach */
879 /*****************************************************************************
881 * this function is called by the tty driver when a port is opened
882 * If successful, we return 0
883 * Otherwise we return a negative error number.
884 *****************************************************************************/
886 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
890 struct usb_serial *serial;
894 struct moschip_port *mos7840_port;
895 struct moschip_port *port0;
897 if (mos7840_port_paranoia_check(port, __func__))
900 serial = port->serial;
902 if (mos7840_serial_paranoia_check(serial, __func__))
905 mos7840_port = mos7840_get_port_private(port);
906 port0 = mos7840_get_port_private(serial->port[0]);
908 if (mos7840_port == NULL || port0 == NULL)
911 usb_clear_halt(serial->dev, port->write_urb->pipe);
912 usb_clear_halt(serial->dev, port->read_urb->pipe);
915 /* Initialising the write urb pool */
916 for (j = 0; j < NUM_URBS; ++j) {
917 urb = usb_alloc_urb(0, GFP_KERNEL);
918 mos7840_port->write_urb_pool[j] = urb;
921 dev_err(&port->dev, "No more urbs???\n");
925 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
927 if (!urb->transfer_buffer) {
929 mos7840_port->write_urb_pool[j] = NULL;
931 "%s-out of memory for urb buffers.\n",
937 /*****************************************************************************
938 * Initialize MCS7840 -- Write Init values to corresponding Registers
946 * 0x08 : SP1/2 Control Reg
947 *****************************************************************************/
949 /* NEED to check the following Block */
952 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
954 dev_dbg(&port->dev, "Reading Spreg failed\n");
958 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
960 dev_dbg(&port->dev, "writing Spreg failed\n");
965 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
967 dev_dbg(&port->dev, "writing Spreg failed\n");
970 /* End of block to be checked */
973 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
976 dev_dbg(&port->dev, "Reading Controlreg failed\n");
979 Data |= 0x08; /* Driver done bit */
980 Data |= 0x20; /* rx_disable */
981 status = mos7840_set_reg_sync(port,
982 mos7840_port->ControlRegOffset, Data);
984 dev_dbg(&port->dev, "writing Controlreg failed\n");
987 /* do register settings here */
988 /* Set all regs to the device default values. */
989 /***********************************
990 * First Disable all interrupts.
991 ***********************************/
993 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
995 dev_dbg(&port->dev, "disabling interrupts failed\n");
998 /* Set FIFO_CONTROL_REGISTER to the default value */
1000 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1002 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
1007 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1009 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
1014 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1015 mos7840_port->shadowLCR = Data;
1018 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1019 mos7840_port->shadowMCR = Data;
1022 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1023 mos7840_port->shadowLCR = Data;
1025 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1026 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1029 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1032 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1035 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1037 Data = Data & ~SERIAL_LCR_DLAB;
1038 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1039 mos7840_port->shadowLCR = Data;
1041 /* clearing Bulkin and Bulkout Fifo */
1043 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1046 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1048 Data = Data & ~0x0c;
1049 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1050 /* Finally enable all interrupts */
1052 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1054 /* clearing rx_disable */
1056 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1058 Data = Data & ~0x20;
1059 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1064 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1067 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1070 /* Check to see if we've set up our endpoint info yet *
1071 * (can't set it up in mos7840_startup as the structures *
1072 * were not set up at that time.) */
1073 if (port0->open_ports == 1) {
1074 if (serial->port[0]->interrupt_in_buffer == NULL) {
1075 /* set up interrupt urb */
1076 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1078 usb_rcvintpipe(serial->dev,
1079 serial->port[0]->interrupt_in_endpointAddress),
1080 serial->port[0]->interrupt_in_buffer,
1081 serial->port[0]->interrupt_in_urb->
1082 transfer_buffer_length,
1083 mos7840_interrupt_callback,
1085 serial->port[0]->interrupt_in_urb->interval);
1087 /* start interrupt read for mos7840 *
1088 * will continue as long as mos7840 is connected */
1091 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1094 dev_err(&port->dev, "%s - Error %d submitting "
1095 "interrupt urb\n", __func__, response);
1102 /* see if we've set up our endpoint info yet *
1103 * (can't set it up in mos7840_startup as the *
1104 * structures were not set up at that time.) */
1106 dev_dbg(&port->dev, "port number is %d\n", port->number);
1107 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1108 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1109 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1110 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1111 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1112 mos7840_port->read_urb = port->read_urb;
1114 /* set up our bulk in urb */
1115 if ((serial->num_ports == 2)
1116 && ((((__u16)port->number -
1117 (__u16)(port->serial->minor)) % 2) != 0)) {
1118 usb_fill_bulk_urb(mos7840_port->read_urb,
1120 usb_rcvbulkpipe(serial->dev,
1121 (port->bulk_in_endpointAddress) + 2),
1122 port->bulk_in_buffer,
1123 mos7840_port->read_urb->transfer_buffer_length,
1124 mos7840_bulk_in_callback, mos7840_port);
1126 usb_fill_bulk_urb(mos7840_port->read_urb,
1128 usb_rcvbulkpipe(serial->dev,
1129 port->bulk_in_endpointAddress),
1130 port->bulk_in_buffer,
1131 mos7840_port->read_urb->transfer_buffer_length,
1132 mos7840_bulk_in_callback, mos7840_port);
1135 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1136 mos7840_port->read_urb_busy = true;
1137 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1139 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1140 __func__, response);
1141 mos7840_port->read_urb_busy = false;
1144 /* initialize our wait queues */
1145 init_waitqueue_head(&mos7840_port->wait_chase);
1146 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1148 /* initialize our icount structure */
1149 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1151 /* initialize our port settings */
1152 /* Must set to enable ints! */
1153 mos7840_port->shadowMCR = MCR_MASTER_IE;
1154 /* send a open port command */
1155 mos7840_port->open = 1;
1156 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1157 mos7840_port->icount.tx = 0;
1158 mos7840_port->icount.rx = 0;
1163 /*****************************************************************************
1164 * mos7840_chars_in_buffer
1165 * this function is called by the tty driver when it wants to know how many
1166 * bytes of data we currently have outstanding in the port (data that has
1167 * been written, but hasn't made it out the port yet)
1168 * If successful, we return the number of bytes left to be written in the
1170 * Otherwise we return zero.
1171 *****************************************************************************/
1173 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1175 struct usb_serial_port *port = tty->driver_data;
1178 unsigned long flags;
1179 struct moschip_port *mos7840_port;
1181 if (mos7840_port_paranoia_check(port, __func__))
1184 mos7840_port = mos7840_get_port_private(port);
1185 if (mos7840_port == NULL)
1188 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1189 for (i = 0; i < NUM_URBS; ++i) {
1190 if (mos7840_port->busy[i]) {
1191 struct urb *urb = mos7840_port->write_urb_pool[i];
1192 chars += urb->transfer_buffer_length;
1195 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1196 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1201 /*****************************************************************************
1203 * this function is called by the tty driver when a port is closed
1204 *****************************************************************************/
1206 static void mos7840_close(struct usb_serial_port *port)
1208 struct usb_serial *serial;
1209 struct moschip_port *mos7840_port;
1210 struct moschip_port *port0;
1214 if (mos7840_port_paranoia_check(port, __func__))
1217 serial = mos7840_get_usb_serial(port, __func__);
1221 mos7840_port = mos7840_get_port_private(port);
1222 port0 = mos7840_get_port_private(serial->port[0]);
1224 if (mos7840_port == NULL || port0 == NULL)
1227 for (j = 0; j < NUM_URBS; ++j)
1228 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1230 /* Freeing Write URBs */
1231 for (j = 0; j < NUM_URBS; ++j) {
1232 if (mos7840_port->write_urb_pool[j]) {
1233 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1234 kfree(mos7840_port->write_urb_pool[j]->
1237 usb_free_urb(mos7840_port->write_urb_pool[j]);
1241 /* While closing port, shutdown all bulk read, write *
1242 * and interrupt read if they exists */
1244 if (mos7840_port->write_urb) {
1245 dev_dbg(&port->dev, "%s", "Shutdown bulk write\n");
1246 usb_kill_urb(mos7840_port->write_urb);
1248 if (mos7840_port->read_urb) {
1249 dev_dbg(&port->dev, "%s", "Shutdown bulk read\n");
1250 usb_kill_urb(mos7840_port->read_urb);
1251 mos7840_port->read_urb_busy = false;
1253 if ((&mos7840_port->control_urb)) {
1254 dev_dbg(&port->dev, "%s", "Shutdown control read\n");
1255 /*/ usb_kill_urb (mos7840_port->control_urb); */
1258 /* if(mos7840_port->ctrl_buf != NULL) */
1259 /* kfree(mos7840_port->ctrl_buf); */
1260 port0->open_ports--;
1261 dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
1262 if (port0->open_ports == 0) {
1263 if (serial->port[0]->interrupt_in_urb) {
1264 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1265 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1269 if (mos7840_port->write_urb) {
1270 /* if this urb had a transfer buffer already (old tx) free it */
1271 if (mos7840_port->write_urb->transfer_buffer != NULL)
1272 kfree(mos7840_port->write_urb->transfer_buffer);
1273 usb_free_urb(mos7840_port->write_urb);
1277 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1280 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1282 mos7840_port->open = 0;
1285 /************************************************************************
1287 * mos7840_block_until_chase_response
1289 * This function will block the close until one of the following:
1290 * 1. Response to our Chase comes from mos7840
1291 * 2. A timeout of 10 seconds without activity has expired
1292 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1294 ************************************************************************/
1296 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1297 struct moschip_port *mos7840_port)
1299 int timeout = msecs_to_jiffies(1000);
1304 count = mos7840_chars_in_buffer(tty);
1306 /* Check for Buffer status */
1310 /* Block the thread for a while */
1311 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1313 /* No activity.. count down section */
1316 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
1319 /* Reset timeout value back to seconds */
1326 /*****************************************************************************
1328 * this function sends a break to the port
1329 *****************************************************************************/
1330 static void mos7840_break(struct tty_struct *tty, int break_state)
1332 struct usb_serial_port *port = tty->driver_data;
1334 struct usb_serial *serial;
1335 struct moschip_port *mos7840_port;
1337 if (mos7840_port_paranoia_check(port, __func__))
1340 serial = mos7840_get_usb_serial(port, __func__);
1344 mos7840_port = mos7840_get_port_private(port);
1346 if (mos7840_port == NULL)
1350 /* flush and block until tx is empty */
1351 mos7840_block_until_chase_response(tty, mos7840_port);
1353 if (break_state == -1)
1354 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1356 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1358 /* FIXME: no locking on shadowLCR anywhere in driver */
1359 mos7840_port->shadowLCR = data;
1360 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1361 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1362 mos7840_port->shadowLCR);
1365 /*****************************************************************************
1366 * mos7840_write_room
1367 * this function is called by the tty driver when it wants to know how many
1368 * bytes of data we can accept for a specific port.
1369 * If successful, we return the amount of room that we have for this port
1370 * Otherwise we return a negative error number.
1371 *****************************************************************************/
1373 static int mos7840_write_room(struct tty_struct *tty)
1375 struct usb_serial_port *port = tty->driver_data;
1378 unsigned long flags;
1379 struct moschip_port *mos7840_port;
1381 if (mos7840_port_paranoia_check(port, __func__))
1384 mos7840_port = mos7840_get_port_private(port);
1385 if (mos7840_port == NULL)
1388 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1389 for (i = 0; i < NUM_URBS; ++i) {
1390 if (!mos7840_port->busy[i])
1391 room += URB_TRANSFER_BUFFER_SIZE;
1393 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1395 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1396 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1401 /*****************************************************************************
1403 * this function is called by the tty driver when data should be written to
1405 * If successful, we return the number of bytes written, otherwise we
1406 * return a negative error number.
1407 *****************************************************************************/
1409 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1410 const unsigned char *data, int count)
1416 unsigned long flags;
1418 struct moschip_port *mos7840_port;
1419 struct usb_serial *serial;
1422 const unsigned char *current_position = data;
1423 unsigned char *data1;
1427 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1428 mos7840_port->shadowLCR = Data;
1429 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1430 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1433 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1434 /* mos7840_port->shadowLCR=Data;//Need to add later */
1436 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1437 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1440 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1442 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1443 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
1446 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1447 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
1449 Data = Data & ~SERIAL_LCR_DLAB;
1450 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1451 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1454 if (mos7840_port_paranoia_check(port, __func__))
1457 serial = port->serial;
1458 if (mos7840_serial_paranoia_check(serial, __func__))
1461 mos7840_port = mos7840_get_port_private(port);
1462 if (mos7840_port == NULL)
1465 /* try to find a free urb in the list */
1468 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1469 for (i = 0; i < NUM_URBS; ++i) {
1470 if (!mos7840_port->busy[i]) {
1471 mos7840_port->busy[i] = 1;
1472 urb = mos7840_port->write_urb_pool[i];
1473 dev_dbg(&port->dev, "URB:%d\n", i);
1477 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1480 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1484 if (urb->transfer_buffer == NULL) {
1485 urb->transfer_buffer =
1486 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1488 if (urb->transfer_buffer == NULL) {
1489 dev_err_console(port, "%s no more kernel memory...\n",
1494 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1496 memcpy(urb->transfer_buffer, current_position, transfer_size);
1498 /* fill urb with data and submit */
1499 if ((serial->num_ports == 2)
1500 && ((((__u16)port->number -
1501 (__u16)(port->serial->minor)) % 2) != 0)) {
1502 usb_fill_bulk_urb(urb,
1504 usb_sndbulkpipe(serial->dev,
1505 (port->bulk_out_endpointAddress) + 2),
1506 urb->transfer_buffer,
1508 mos7840_bulk_out_data_callback, mos7840_port);
1510 usb_fill_bulk_urb(urb,
1512 usb_sndbulkpipe(serial->dev,
1513 port->bulk_out_endpointAddress),
1514 urb->transfer_buffer,
1516 mos7840_bulk_out_data_callback, mos7840_port);
1519 data1 = urb->transfer_buffer;
1520 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1523 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1524 mos7840_port->led_flag = true;
1525 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1526 mod_timer(&mos7840_port->led_timer1,
1527 jiffies + msecs_to_jiffies(LED_ON_MS));
1530 /* send it down the pipe */
1531 status = usb_submit_urb(urb, GFP_ATOMIC);
1534 mos7840_port->busy[i] = 0;
1535 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1536 "with status = %d\n", __func__, status);
1537 bytes_sent = status;
1540 bytes_sent = transfer_size;
1541 mos7840_port->icount.tx += transfer_size;
1543 dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1549 /*****************************************************************************
1551 * this function is called by the tty driver when it wants to stop the data
1552 * being read from the port.
1553 *****************************************************************************/
1555 static void mos7840_throttle(struct tty_struct *tty)
1557 struct usb_serial_port *port = tty->driver_data;
1558 struct moschip_port *mos7840_port;
1561 if (mos7840_port_paranoia_check(port, __func__))
1564 mos7840_port = mos7840_get_port_private(port);
1566 if (mos7840_port == NULL)
1569 if (!mos7840_port->open) {
1570 dev_dbg(&port->dev, "%s", "port not opened\n");
1574 /* if we are implementing XON/XOFF, send the stop character */
1576 unsigned char stop_char = STOP_CHAR(tty);
1577 status = mos7840_write(tty, port, &stop_char, 1);
1581 /* if we are implementing RTS/CTS, toggle that line */
1582 if (tty->termios.c_cflag & CRTSCTS) {
1583 mos7840_port->shadowMCR &= ~MCR_RTS;
1584 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1585 mos7840_port->shadowMCR);
1591 /*****************************************************************************
1592 * mos7840_unthrottle
1593 * this function is called by the tty driver when it wants to resume
1594 * the data being read from the port (called after mos7840_throttle is
1596 *****************************************************************************/
1597 static void mos7840_unthrottle(struct tty_struct *tty)
1599 struct usb_serial_port *port = tty->driver_data;
1601 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1603 if (mos7840_port_paranoia_check(port, __func__))
1606 if (mos7840_port == NULL)
1609 if (!mos7840_port->open) {
1610 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1614 /* if we are implementing XON/XOFF, send the start character */
1616 unsigned char start_char = START_CHAR(tty);
1617 status = mos7840_write(tty, port, &start_char, 1);
1622 /* if we are implementing RTS/CTS, toggle that line */
1623 if (tty->termios.c_cflag & CRTSCTS) {
1624 mos7840_port->shadowMCR |= MCR_RTS;
1625 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1626 mos7840_port->shadowMCR);
1632 static int mos7840_tiocmget(struct tty_struct *tty)
1634 struct usb_serial_port *port = tty->driver_data;
1635 struct moschip_port *mos7840_port;
1636 unsigned int result;
1640 mos7840_port = mos7840_get_port_private(port);
1642 if (mos7840_port == NULL)
1645 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1646 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1647 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1648 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1649 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1650 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1651 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1652 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1653 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1655 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1660 static int mos7840_tiocmset(struct tty_struct *tty,
1661 unsigned int set, unsigned int clear)
1663 struct usb_serial_port *port = tty->driver_data;
1664 struct moschip_port *mos7840_port;
1668 mos7840_port = mos7840_get_port_private(port);
1670 if (mos7840_port == NULL)
1673 /* FIXME: What locks the port registers ? */
1674 mcr = mos7840_port->shadowMCR;
1675 if (clear & TIOCM_RTS)
1677 if (clear & TIOCM_DTR)
1679 if (clear & TIOCM_LOOP)
1680 mcr &= ~MCR_LOOPBACK;
1682 if (set & TIOCM_RTS)
1684 if (set & TIOCM_DTR)
1686 if (set & TIOCM_LOOP)
1687 mcr |= MCR_LOOPBACK;
1689 mos7840_port->shadowMCR = mcr;
1691 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1693 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1700 /*****************************************************************************
1701 * mos7840_calc_baud_rate_divisor
1702 * this function calculates the proper baud rate divisor for the specified
1704 *****************************************************************************/
1705 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1706 int baudRate, int *divisor,
1709 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1711 if (baudRate <= 115200) {
1712 *divisor = 115200 / baudRate;
1715 if ((baudRate > 115200) && (baudRate <= 230400)) {
1716 *divisor = 230400 / baudRate;
1717 *clk_sel_val = 0x10;
1718 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1719 *divisor = 403200 / baudRate;
1720 *clk_sel_val = 0x20;
1721 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1722 *divisor = 460800 / baudRate;
1723 *clk_sel_val = 0x30;
1724 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1725 *divisor = 806400 / baudRate;
1726 *clk_sel_val = 0x40;
1727 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1728 *divisor = 921600 / baudRate;
1729 *clk_sel_val = 0x50;
1730 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1731 *divisor = 1572864 / baudRate;
1732 *clk_sel_val = 0x60;
1733 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1734 *divisor = 3145728 / baudRate;
1735 *clk_sel_val = 0x70;
1741 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1742 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1743 *divisor = mos7840_divisor_table[i].Divisor;
1748 /* After trying for all the standard baud rates *
1749 * Try calculating the divisor for this baud rate */
1751 if (baudrate > 75 && baudrate < 230400) {
1752 /* get the divisor */
1753 custom = (__u16) (230400L / baudrate);
1755 /* Check for round off */
1756 round1 = (__u16) (2304000L / baudrate);
1757 round = (__u16) (round1 - (custom * 10));
1762 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
1766 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
1771 /*****************************************************************************
1772 * mos7840_send_cmd_write_baud_rate
1773 * this function sends the proper command to change the baud rate of the
1775 *****************************************************************************/
1777 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1783 unsigned char number;
1785 struct usb_serial_port *port;
1787 if (mos7840_port == NULL)
1790 port = mos7840_port->port;
1791 if (mos7840_port_paranoia_check(port, __func__))
1794 if (mos7840_serial_paranoia_check(port->serial, __func__))
1797 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1799 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1800 mos7840_port->port->number, baudRate);
1801 /* reset clk_uart_sel in spregOffset */
1802 if (baudRate > 115200) {
1803 #ifdef HW_flow_control
1804 /* NOTE: need to see the pther register to modify */
1805 /* setting h/w flow control bit to 1 */
1807 mos7840_port->shadowMCR = Data;
1808 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1811 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1817 #ifdef HW_flow_control
1818 /* setting h/w flow control bit to 0 */
1820 mos7840_port->shadowMCR = Data;
1821 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1824 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1831 if (1) { /* baudRate <= 115200) */
1834 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1836 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1839 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1842 Data = (Data & 0x8f) | clk_sel_val;
1843 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1846 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1849 /* Calculate the Divisor */
1852 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1855 /* Enable 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);
1860 /* Write the divisor */
1861 Data = (unsigned char)(divisor & 0xff);
1862 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1863 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1865 Data = (unsigned char)((divisor & 0xff00) >> 8);
1866 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1867 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1869 /* Disable access to divisor latch */
1870 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1871 mos7840_port->shadowLCR = Data;
1872 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1878 /*****************************************************************************
1879 * mos7840_change_port_settings
1880 * This routine is called to set the UART on the device to match
1881 * the specified new settings.
1882 *****************************************************************************/
1884 static void mos7840_change_port_settings(struct tty_struct *tty,
1885 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1895 struct usb_serial_port *port;
1896 struct usb_serial *serial;
1898 if (mos7840_port == NULL)
1901 port = mos7840_port->port;
1903 if (mos7840_port_paranoia_check(port, __func__))
1906 if (mos7840_serial_paranoia_check(port->serial, __func__))
1909 serial = port->serial;
1911 if (!mos7840_port->open) {
1912 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1918 lParity = LCR_PAR_NONE;
1920 cflag = tty->termios.c_cflag;
1921 iflag = tty->termios.c_iflag;
1923 /* Change the number of bits */
1924 if (cflag & CSIZE) {
1925 switch (cflag & CSIZE) {
1943 /* Change the Parity bit */
1944 if (cflag & PARENB) {
1945 if (cflag & PARODD) {
1946 lParity = LCR_PAR_ODD;
1947 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1949 lParity = LCR_PAR_EVEN;
1950 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1954 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1958 lParity = lParity | 0x20;
1960 /* Change the Stop bit */
1961 if (cflag & CSTOPB) {
1963 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1966 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1969 /* Update the LCR with the correct value */
1970 mos7840_port->shadowLCR &=
1971 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1972 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1974 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1975 mos7840_port->shadowLCR);
1976 /* Disable Interrupts */
1978 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1981 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1984 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1986 /* Send the updated LCR value to the mos7840 */
1987 Data = mos7840_port->shadowLCR;
1989 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1992 mos7840_port->shadowMCR = Data;
1993 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1995 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1997 /* set up the MCR register and send it to the mos7840 */
1999 mos7840_port->shadowMCR = MCR_MASTER_IE;
2001 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2003 if (cflag & CRTSCTS)
2004 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2006 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2008 Data = mos7840_port->shadowMCR;
2009 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2011 /* Determine divisor based on baud rate */
2012 baud = tty_get_baud_rate(tty);
2015 /* pick a default, any default... */
2016 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
2020 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
2021 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2023 /* Enable Interrupts */
2025 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2027 if (mos7840_port->read_urb_busy == false) {
2028 mos7840_port->read_urb_busy = true;
2029 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2031 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2033 mos7840_port->read_urb_busy = false;
2036 wake_up(&mos7840_port->delta_msr_wait);
2037 mos7840_port->delta_msr_cond = 1;
2038 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
2039 mos7840_port->shadowLCR);
2042 /*****************************************************************************
2043 * mos7840_set_termios
2044 * this function is called by the tty driver when it wants to change
2045 * the termios structure
2046 *****************************************************************************/
2048 static void mos7840_set_termios(struct tty_struct *tty,
2049 struct usb_serial_port *port,
2050 struct ktermios *old_termios)
2054 struct usb_serial *serial;
2055 struct moschip_port *mos7840_port;
2057 if (mos7840_port_paranoia_check(port, __func__))
2060 serial = port->serial;
2062 if (mos7840_serial_paranoia_check(serial, __func__))
2065 mos7840_port = mos7840_get_port_private(port);
2067 if (mos7840_port == NULL)
2070 if (!mos7840_port->open) {
2071 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
2075 dev_dbg(&port->dev, "%s", "setting termios - \n");
2077 cflag = tty->termios.c_cflag;
2079 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2080 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
2081 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2082 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2083 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
2085 /* change the port settings to the new ones specified */
2087 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2089 if (!mos7840_port->read_urb) {
2090 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
2094 if (mos7840_port->read_urb_busy == false) {
2095 mos7840_port->read_urb_busy = true;
2096 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2098 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2100 mos7840_port->read_urb_busy = false;
2105 /*****************************************************************************
2106 * mos7840_get_lsr_info - get line status register info
2108 * Purpose: Let user call ioctl() to get info when the UART physically
2109 * is emptied. On bus types like RS485, the transmitter must
2110 * release the bus after transmitting. This must be done when
2111 * the transmit shift register is empty, not be done when the
2112 * transmit holding register is empty. This functionality
2113 * allows an RS485 driver to be written in user space.
2114 *****************************************************************************/
2116 static int mos7840_get_lsr_info(struct tty_struct *tty,
2117 unsigned int __user *value)
2120 unsigned int result = 0;
2122 count = mos7840_chars_in_buffer(tty);
2124 result = TIOCSER_TEMT;
2126 if (copy_to_user(value, &result, sizeof(int)))
2131 /*****************************************************************************
2132 * mos7840_get_serial_info
2133 * function to get information about serial port
2134 *****************************************************************************/
2136 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2137 struct serial_struct __user *retinfo)
2139 struct serial_struct tmp;
2141 if (mos7840_port == NULL)
2147 memset(&tmp, 0, sizeof(tmp));
2149 tmp.type = PORT_16550A;
2150 tmp.line = mos7840_port->port->serial->minor;
2151 tmp.port = mos7840_port->port->number;
2153 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2154 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2155 tmp.baud_base = 9600;
2156 tmp.close_delay = 5 * HZ;
2157 tmp.closing_wait = 30 * HZ;
2159 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2164 static int mos7840_get_icount(struct tty_struct *tty,
2165 struct serial_icounter_struct *icount)
2167 struct usb_serial_port *port = tty->driver_data;
2168 struct moschip_port *mos7840_port;
2169 struct async_icount cnow;
2171 mos7840_port = mos7840_get_port_private(port);
2172 cnow = mos7840_port->icount;
2175 icount->cts = cnow.cts;
2176 icount->dsr = cnow.dsr;
2177 icount->rng = cnow.rng;
2178 icount->dcd = cnow.dcd;
2179 icount->rx = cnow.rx;
2180 icount->tx = cnow.tx;
2181 icount->frame = cnow.frame;
2182 icount->overrun = cnow.overrun;
2183 icount->parity = cnow.parity;
2184 icount->brk = cnow.brk;
2185 icount->buf_overrun = cnow.buf_overrun;
2187 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
2188 icount->rx, icount->tx);
2192 /*****************************************************************************
2194 * this function handles any ioctl calls to the driver
2195 *****************************************************************************/
2197 static int mos7840_ioctl(struct tty_struct *tty,
2198 unsigned int cmd, unsigned long arg)
2200 struct usb_serial_port *port = tty->driver_data;
2201 void __user *argp = (void __user *)arg;
2202 struct moschip_port *mos7840_port;
2204 struct async_icount cnow;
2205 struct async_icount cprev;
2207 if (mos7840_port_paranoia_check(port, __func__))
2210 mos7840_port = mos7840_get_port_private(port);
2212 if (mos7840_port == NULL)
2215 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
2218 /* return number of bytes available */
2221 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
2222 return mos7840_get_lsr_info(tty, argp);
2225 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
2226 return mos7840_get_serial_info(mos7840_port, argp);
2229 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
2233 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
2234 cprev = mos7840_port->icount;
2236 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2237 mos7840_port->delta_msr_cond = 0;
2238 wait_event_interruptible(mos7840_port->delta_msr_wait,
2240 delta_msr_cond == 1));
2242 /* see if a signal did it */
2243 if (signal_pending(current))
2244 return -ERESTARTSYS;
2245 cnow = mos7840_port->icount;
2247 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2248 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2249 return -EIO; /* no change => error */
2250 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2251 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2252 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2253 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2264 return -ENOIOCTLCMD;
2267 static int mos7810_check(struct usb_serial *serial)
2269 int i, pass_count = 0;
2270 __u16 data = 0, mcr_data = 0;
2271 __u16 test_pattern = 0x55AA;
2273 /* Store MCR setting */
2274 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2275 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2276 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2278 for (i = 0; i < 16; i++) {
2279 /* Send the 1-bit test pattern out to MCS7810 test pin */
2280 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2281 MCS_WRREQ, MCS_WR_RTYPE,
2282 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2283 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2285 /* Read the test pattern back */
2286 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2287 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2288 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2290 /* If this is a MCS7810 device, both test patterns must match */
2291 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2297 /* Restore MCR setting */
2298 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2299 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2300 0, MOS_WDR_TIMEOUT);
2302 if (pass_count == 16)
2308 static int mos7840_calc_num_ports(struct usb_serial *serial)
2311 int mos7840_num_ports;
2313 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2314 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2315 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2317 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2318 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2319 device_type = serial->dev->descriptor.idProduct;
2321 /* For a MCS7840 device GPIO0 must be set to 1 */
2322 if ((data & 0x01) == 1)
2323 device_type = MOSCHIP_DEVICE_ID_7840;
2324 else if (mos7810_check(serial))
2325 device_type = MOSCHIP_DEVICE_ID_7810;
2327 device_type = MOSCHIP_DEVICE_ID_7820;
2330 mos7840_num_ports = (device_type >> 4) & 0x000F;
2331 serial->num_bulk_in = mos7840_num_ports;
2332 serial->num_bulk_out = mos7840_num_ports;
2333 serial->num_ports = mos7840_num_ports;
2335 return mos7840_num_ports;
2338 /****************************************************************************
2340 ****************************************************************************/
2342 static int mos7840_startup(struct usb_serial *serial)
2344 struct moschip_port *mos7840_port;
2345 struct usb_device *dev;
2351 /* we set up the pointers to the endpoints in the mos7840_open *
2352 * function, as the structures aren't created yet. */
2354 /* set up port private structures */
2355 for (i = 0; i < serial->num_ports; ++i) {
2356 dev_dbg(&dev->dev, "mos7840_startup: configuring port %d............\n", i);
2357 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2358 if (mos7840_port == NULL) {
2359 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2361 i--; /* don't follow NULL pointer cleaning up */
2365 /* Initialize all port interrupt end point to port 0 int
2366 * endpoint. Our device has only one interrupt end point
2367 * common to all port */
2369 mos7840_port->port = serial->port[i];
2370 mos7840_set_port_private(serial->port[i], mos7840_port);
2371 spin_lock_init(&mos7840_port->pool_lock);
2373 /* minor is not initialised until later by
2374 * usb-serial.c:get_free_serial() and cannot therefore be used
2375 * to index device instances */
2376 mos7840_port->port_num = i + 1;
2377 dev_dbg(&dev->dev, "serial->port[i]->number = %d\n", serial->port[i]->number);
2378 dev_dbg(&dev->dev, "serial->port[i]->serial->minor = %d\n", serial->port[i]->serial->minor);
2379 dev_dbg(&dev->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2380 dev_dbg(&dev->dev, "serial->minor = %d\n", serial->minor);
2382 if (mos7840_port->port_num == 1) {
2383 mos7840_port->SpRegOffset = 0x0;
2384 mos7840_port->ControlRegOffset = 0x1;
2385 mos7840_port->DcrRegOffset = 0x4;
2386 } else if ((mos7840_port->port_num == 2)
2387 && (serial->num_ports == 4)) {
2388 mos7840_port->SpRegOffset = 0x8;
2389 mos7840_port->ControlRegOffset = 0x9;
2390 mos7840_port->DcrRegOffset = 0x16;
2391 } else if ((mos7840_port->port_num == 2)
2392 && (serial->num_ports == 2)) {
2393 mos7840_port->SpRegOffset = 0xa;
2394 mos7840_port->ControlRegOffset = 0xb;
2395 mos7840_port->DcrRegOffset = 0x19;
2396 } else if ((mos7840_port->port_num == 3)
2397 && (serial->num_ports == 4)) {
2398 mos7840_port->SpRegOffset = 0xa;
2399 mos7840_port->ControlRegOffset = 0xb;
2400 mos7840_port->DcrRegOffset = 0x19;
2401 } else if ((mos7840_port->port_num == 4)
2402 && (serial->num_ports == 4)) {
2403 mos7840_port->SpRegOffset = 0xc;
2404 mos7840_port->ControlRegOffset = 0xd;
2405 mos7840_port->DcrRegOffset = 0x1c;
2407 mos7840_dump_serial_port(serial->port[i], mos7840_port);
2408 mos7840_set_port_private(serial->port[i], mos7840_port);
2410 /* enable rx_disable bit in control register */
2411 status = mos7840_get_reg_sync(serial->port[i],
2412 mos7840_port->ControlRegOffset, &Data);
2414 dev_dbg(&dev->dev, "Reading ControlReg failed status-0x%x\n", status);
2417 dev_dbg(&dev->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2418 Data |= 0x08; /* setting driver done bit */
2419 Data |= 0x04; /* sp1_bit to have cts change reflect in
2422 /* Data |= 0x20; //rx_disable bit */
2423 status = mos7840_set_reg_sync(serial->port[i],
2424 mos7840_port->ControlRegOffset, Data);
2426 dev_dbg(&dev->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2429 dev_dbg(&dev->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2431 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2434 status = mos7840_set_reg_sync(serial->port[i],
2435 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2437 dev_dbg(&dev->dev, "Writing DCR0 failed status-0x%x\n", status);
2440 dev_dbg(&dev->dev, "DCR0 Writing success status%d\n", status);
2443 status = mos7840_set_reg_sync(serial->port[i],
2444 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2446 dev_dbg(&dev->dev, "Writing DCR1 failed status-0x%x\n", status);
2449 dev_dbg(&dev->dev, "DCR1 Writing success status%d\n", status);
2452 status = mos7840_set_reg_sync(serial->port[i],
2453 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2455 dev_dbg(&dev->dev, "Writing DCR2 failed status-0x%x\n", status);
2458 dev_dbg(&dev->dev, "DCR2 Writing success status%d\n", status);
2460 /* write values in clkstart0x0 and clkmulti 0x20 */
2462 status = mos7840_set_reg_sync(serial->port[i],
2463 CLK_START_VALUE_REGISTER, Data);
2465 dev_dbg(&dev->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2468 dev_dbg(&dev->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2471 status = mos7840_set_reg_sync(serial->port[i],
2472 CLK_MULTI_REGISTER, Data);
2474 dev_dbg(&dev->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2477 dev_dbg(&dev->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2479 /* write value 0x0 to scratchpad register */
2481 status = mos7840_set_uart_reg(serial->port[i],
2482 SCRATCH_PAD_REGISTER, Data);
2484 dev_dbg(&dev->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2487 dev_dbg(&dev->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2489 /* Zero Length flag register */
2490 if ((mos7840_port->port_num != 1)
2491 && (serial->num_ports == 2)) {
2494 status = mos7840_set_reg_sync(serial->port[i],
2496 ((__u16)mos7840_port->port_num)), Data);
2497 dev_dbg(&dev->dev, "ZLIP offset %x\n",
2498 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2500 dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 2, status);
2503 dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 2, status);
2506 status = mos7840_set_reg_sync(serial->port[i],
2508 ((__u16)mos7840_port->port_num) - 0x1), Data);
2509 dev_dbg(&dev->dev, "ZLIP offset %x\n",
2510 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2512 dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 1, status);
2515 dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 1, status);
2518 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2519 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2520 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2522 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2523 !mos7840_port->dr) {
2528 mos7840_port->has_led = false;
2530 /* Initialize LED timers */
2531 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2532 mos7840_port->has_led = true;
2534 init_timer(&mos7840_port->led_timer1);
2535 mos7840_port->led_timer1.function = mos7840_led_off;
2536 mos7840_port->led_timer1.expires =
2537 jiffies + msecs_to_jiffies(LED_ON_MS);
2538 mos7840_port->led_timer1.data =
2539 (unsigned long)mos7840_port;
2541 init_timer(&mos7840_port->led_timer2);
2542 mos7840_port->led_timer2.function =
2543 mos7840_led_flag_off;
2544 mos7840_port->led_timer2.expires =
2545 jiffies + msecs_to_jiffies(LED_OFF_MS);
2546 mos7840_port->led_timer2.data =
2547 (unsigned long)mos7840_port;
2549 mos7840_port->led_flag = false;
2552 mos7840_set_led_sync(serial->port[i],
2553 MODEM_CONTROL_REGISTER, 0x0300);
2557 /* Zero Length flag enable */
2559 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2561 dev_dbg(&dev->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2564 dev_dbg(&dev->dev, "ZLP_REG5 Writing success status%d\n", status);
2566 /* setting configuration feature to one */
2567 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2568 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT);
2571 for (/* nothing */; i >= 0; i--) {
2572 mos7840_port = mos7840_get_port_private(serial->port[i]);
2574 kfree(mos7840_port->dr);
2575 kfree(mos7840_port->ctrl_buf);
2576 usb_free_urb(mos7840_port->control_urb);
2577 kfree(mos7840_port);
2582 /****************************************************************************
2583 * mos7840_disconnect
2584 * This function is called whenever the device is removed from the usb bus.
2585 ****************************************************************************/
2587 static void mos7840_disconnect(struct usb_serial *serial)
2590 unsigned long flags;
2591 struct moschip_port *mos7840_port;
2593 /* check for the ports to be closed,close the ports and disconnect */
2595 /* free private structure allocated for serial port *
2596 * stop reads and writes on all ports */
2598 for (i = 0; i < serial->num_ports; ++i) {
2599 mos7840_port = mos7840_get_port_private(serial->port[i]);
2601 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2602 mos7840_port->zombie = 1;
2603 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2604 usb_kill_urb(mos7840_port->control_urb);
2609 /****************************************************************************
2611 * This function is called when the usb_serial structure is freed.
2612 ****************************************************************************/
2614 static void mos7840_release(struct usb_serial *serial)
2617 struct moschip_port *mos7840_port;
2619 /* check for the ports to be closed,close the ports and disconnect */
2621 /* free private structure allocated for serial port *
2622 * stop reads and writes on all ports */
2624 for (i = 0; i < serial->num_ports; ++i) {
2625 mos7840_port = mos7840_get_port_private(serial->port[i]);
2627 if (mos7840_port->has_led) {
2629 mos7840_set_led_sync(mos7840_port->port,
2630 MODEM_CONTROL_REGISTER, 0x0300);
2632 del_timer_sync(&mos7840_port->led_timer1);
2633 del_timer_sync(&mos7840_port->led_timer2);
2635 usb_free_urb(mos7840_port->control_urb);
2636 kfree(mos7840_port->ctrl_buf);
2637 kfree(mos7840_port->dr);
2638 kfree(mos7840_port);
2643 static struct usb_serial_driver moschip7840_4port_device = {
2645 .owner = THIS_MODULE,
2648 .description = DRIVER_DESC,
2649 .id_table = id_table,
2651 .open = mos7840_open,
2652 .close = mos7840_close,
2653 .write = mos7840_write,
2654 .write_room = mos7840_write_room,
2655 .chars_in_buffer = mos7840_chars_in_buffer,
2656 .throttle = mos7840_throttle,
2657 .unthrottle = mos7840_unthrottle,
2658 .calc_num_ports = mos7840_calc_num_ports,
2659 #ifdef MCSSerialProbe
2660 .probe = mos7840_serial_probe,
2662 .ioctl = mos7840_ioctl,
2663 .set_termios = mos7840_set_termios,
2664 .break_ctl = mos7840_break,
2665 .tiocmget = mos7840_tiocmget,
2666 .tiocmset = mos7840_tiocmset,
2667 .get_icount = mos7840_get_icount,
2668 .attach = mos7840_startup,
2669 .disconnect = mos7840_disconnect,
2670 .release = mos7840_release,
2671 .read_bulk_callback = mos7840_bulk_in_callback,
2672 .read_int_callback = mos7840_interrupt_callback,
2675 static struct usb_serial_driver * const serial_drivers[] = {
2676 &moschip7840_4port_device, NULL
2679 module_usb_serial_driver(serial_drivers, id_table);
2681 MODULE_DESCRIPTION(DRIVER_DESC);
2682 MODULE_LICENSE("GPL");