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 */
256 * mos7840_set_reg_sync
257 * To set the Control register by calling usb_fill_control_urb function
258 * by passing usb_sndctrlpipe function as parameter.
261 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
264 struct usb_device *dev = port->serial->dev;
266 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
268 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
269 MCS_WR_RTYPE, val, reg, NULL, 0,
274 * mos7840_get_reg_sync
275 * To set the Uart register by calling usb_fill_control_urb function by
276 * passing usb_rcvctrlpipe function as parameter.
279 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
282 struct usb_device *dev = port->serial->dev;
286 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
290 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
291 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
294 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
301 * mos7840_set_uart_reg
302 * To set the Uart register by calling usb_fill_control_urb function by
303 * passing usb_sndctrlpipe function as parameter.
306 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
310 struct usb_device *dev = port->serial->dev;
312 /* For the UART control registers, the application number need
314 if (port->serial->num_ports == 4) {
315 val |= (((__u16) port->number -
316 (__u16) (port->serial->minor)) + 1) << 8;
318 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
319 val |= (((__u16) port->number -
320 (__u16) (port->serial->minor)) + 1) << 8;
322 val |= (((__u16) port->number -
323 (__u16) (port->serial->minor)) + 2) << 8;
326 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
327 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
328 MCS_WR_RTYPE, val, reg, NULL, 0,
334 * mos7840_get_uart_reg
335 * To set the Control register by calling usb_fill_control_urb function
336 * by passing usb_rcvctrlpipe function as parameter.
338 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
341 struct usb_device *dev = port->serial->dev;
346 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
350 /* Wval is same as application number */
351 if (port->serial->num_ports == 4) {
353 (((__u16) port->number - (__u16) (port->serial->minor)) +
356 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
357 Wval = (((__u16) port->number -
358 (__u16) (port->serial->minor)) + 1) << 8;
360 Wval = (((__u16) port->number -
361 (__u16) (port->serial->minor)) + 2) << 8;
364 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
365 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
366 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
374 static void mos7840_dump_serial_port(struct usb_serial_port *port,
375 struct moschip_port *mos7840_port)
378 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
379 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
380 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
384 /************************************************************************/
385 /************************************************************************/
386 /* I N T E R F A C E F U N C T I O N S */
387 /* I N T E R F A C E F U N C T I O N S */
388 /************************************************************************/
389 /************************************************************************/
391 static inline void mos7840_set_port_private(struct usb_serial_port *port,
392 struct moschip_port *data)
394 usb_set_serial_port_data(port, (void *)data);
397 static inline struct moschip_port *mos7840_get_port_private(struct
401 return (struct moschip_port *)usb_get_serial_port_data(port);
404 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
406 struct moschip_port *mos7840_port;
407 struct async_icount *icount;
409 icount = &mos7840_port->icount;
411 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
413 icount = &mos7840_port->icount;
415 /* update input line counters */
416 if (new_msr & MOS_MSR_DELTA_CTS) {
420 if (new_msr & MOS_MSR_DELTA_DSR) {
424 if (new_msr & MOS_MSR_DELTA_CD) {
428 if (new_msr & MOS_MSR_DELTA_RI) {
435 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
437 struct async_icount *icount;
439 if (new_lsr & SERIAL_LSR_BI) {
441 * Parity and Framing errors only count if they
442 * occur exclusive of a break being
445 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
448 /* update input line counters */
449 icount = &port->icount;
450 if (new_lsr & SERIAL_LSR_BI) {
454 if (new_lsr & SERIAL_LSR_OE) {
458 if (new_lsr & SERIAL_LSR_PE) {
462 if (new_lsr & SERIAL_LSR_FE) {
468 /************************************************************************/
469 /************************************************************************/
470 /* U S B C A L L B A C K F U N C T I O N S */
471 /* U S B C A L L B A C K F U N C T I O N S */
472 /************************************************************************/
473 /************************************************************************/
475 static void mos7840_control_callback(struct urb *urb)
478 struct moschip_port *mos7840_port;
479 struct device *dev = &urb->dev->dev;
482 int status = urb->status;
484 mos7840_port = urb->context;
493 /* this urb is terminated, clean up */
494 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
497 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
501 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
502 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
503 mos7840_port->MsrLsr, mos7840_port->port_num);
504 data = urb->transfer_buffer;
505 regval = (__u8) data[0];
506 dev_dbg(dev, "%s data is %x\n", __func__, regval);
507 if (mos7840_port->MsrLsr == 0)
508 mos7840_handle_new_msr(mos7840_port, regval);
509 else if (mos7840_port->MsrLsr == 1)
510 mos7840_handle_new_lsr(mos7840_port, regval);
513 spin_lock(&mos7840_port->pool_lock);
514 if (!mos7840_port->zombie)
515 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
516 spin_unlock(&mos7840_port->pool_lock);
518 dev_err(dev, "%s - Error %d submitting interrupt urb\n",
523 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
526 struct usb_device *dev = mcs->port->serial->dev;
527 struct usb_ctrlrequest *dr = mcs->dr;
528 unsigned char *buffer = mcs->ctrl_buf;
531 dr->bRequestType = MCS_RD_RTYPE;
532 dr->bRequest = MCS_RDREQ;
533 dr->wValue = cpu_to_le16(Wval); /* 0 */
534 dr->wIndex = cpu_to_le16(reg);
535 dr->wLength = cpu_to_le16(2);
537 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
538 (unsigned char *)dr, buffer, 2,
539 mos7840_control_callback, mcs);
540 mcs->control_urb->transfer_buffer_length = 2;
541 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
545 static void mos7840_set_led_callback(struct urb *urb)
547 switch (urb->status) {
554 /* This urb is terminated, clean up */
555 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
556 __func__, urb->status);
559 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
560 __func__, urb->status);
564 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
567 struct usb_device *dev = mcs->port->serial->dev;
568 struct usb_ctrlrequest *dr = mcs->dr;
570 dr->bRequestType = MCS_WR_RTYPE;
571 dr->bRequest = MCS_WRREQ;
572 dr->wValue = cpu_to_le16(wval);
573 dr->wIndex = cpu_to_le16(reg);
574 dr->wLength = cpu_to_le16(0);
576 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
577 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
579 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
582 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
585 struct usb_device *dev = port->serial->dev;
587 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
588 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
591 static void mos7840_led_off(unsigned long arg)
593 struct moschip_port *mcs = (struct moschip_port *) arg;
596 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
597 mod_timer(&mcs->led_timer2,
598 jiffies + msecs_to_jiffies(LED_OFF_MS));
601 static void mos7840_led_flag_off(unsigned long arg)
603 struct moschip_port *mcs = (struct moschip_port *) arg;
605 mcs->led_flag = false;
608 /*****************************************************************************
609 * mos7840_interrupt_callback
610 * this is the callback function for when we have received data on the
611 * interrupt endpoint.
612 *****************************************************************************/
614 static void mos7840_interrupt_callback(struct urb *urb)
618 struct moschip_port *mos7840_port;
619 struct usb_serial *serial;
624 __u16 wval, wreg = 0;
625 int status = urb->status;
634 /* this urb is terminated, clean up */
635 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
639 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
644 length = urb->actual_length;
645 data = urb->transfer_buffer;
647 serial = urb->context;
649 /* Moschip get 5 bytes
650 * Byte 1 IIR Port 1 (port.number is 0)
651 * Byte 2 IIR Port 2 (port.number is 1)
652 * Byte 3 IIR Port 3 (port.number is 2)
653 * Byte 4 IIR Port 4 (port.number is 3)
654 * Byte 5 FIFO status for both */
656 if (length && length > 5) {
657 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
661 sp[0] = (__u8) data[0];
662 sp[1] = (__u8) data[1];
663 sp[2] = (__u8) data[2];
664 sp[3] = (__u8) data[3];
667 for (i = 0; i < serial->num_ports; i++) {
668 mos7840_port = mos7840_get_port_private(serial->port[i]);
670 (((__u16) serial->port[i]->number -
671 (__u16) (serial->minor)) + 1) << 8;
672 if (mos7840_port->open) {
674 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
676 switch (sp[i] & 0x0f) {
678 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
679 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
680 mos7840_port->MsrLsr = 1;
681 wreg = LINE_STATUS_REGISTER;
684 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
685 mos7840_port->MsrLsr = 0;
686 wreg = MODEM_STATUS_REGISTER;
689 spin_lock(&mos7840_port->pool_lock);
690 if (!mos7840_port->zombie) {
691 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
693 spin_unlock(&mos7840_port->pool_lock);
696 spin_unlock(&mos7840_port->pool_lock);
701 /* the completion handler for the control urb will resubmit */
704 result = usb_submit_urb(urb, GFP_ATOMIC);
706 dev_err(&urb->dev->dev,
707 "%s - Error %d submitting interrupt urb\n",
712 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
713 const char *function)
716 pr_debug("%s - port == NULL\n", function);
720 pr_debug("%s - port->serial == NULL\n", function);
727 /* Inline functions to check the sanity of a pointer that is passed to us */
728 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
729 const char *function)
732 pr_debug("%s - serial == NULL\n", function);
736 pr_debug("%s - serial->type == NULL!\n", function);
743 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
744 const char *function)
746 /* if no port was specified, or it fails a paranoia check */
748 mos7840_port_paranoia_check(port, function) ||
749 mos7840_serial_paranoia_check(port->serial, function)) {
750 /* then say that we don't have a valid usb_serial thing,
751 * which will end up genrating -ENODEV return values */
758 /*****************************************************************************
759 * mos7840_bulk_in_callback
760 * this is the callback function for when we have received data on the
762 *****************************************************************************/
764 static void mos7840_bulk_in_callback(struct urb *urb)
768 struct usb_serial *serial;
769 struct usb_serial_port *port;
770 struct moschip_port *mos7840_port;
771 struct tty_struct *tty;
772 int status = urb->status;
774 mos7840_port = urb->context;
779 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
780 mos7840_port->read_urb_busy = false;
784 port = mos7840_port->port;
785 if (mos7840_port_paranoia_check(port, __func__)) {
786 mos7840_port->read_urb_busy = false;
790 serial = mos7840_get_usb_serial(port, __func__);
792 mos7840_port->read_urb_busy = false;
796 data = urb->transfer_buffer;
797 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
799 if (urb->actual_length) {
800 tty = tty_port_tty_get(&mos7840_port->port->port);
802 tty_insert_flip_string(tty, data, urb->actual_length);
803 tty_flip_buffer_push(tty);
806 mos7840_port->icount.rx += urb->actual_length;
808 dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx);
811 if (!mos7840_port->read_urb) {
812 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
813 mos7840_port->read_urb_busy = false;
818 if (mos7840_port->has_led && !mos7840_port->led_flag) {
819 mos7840_port->led_flag = true;
820 mos7840_set_led_async(mos7840_port, 0x0301,
821 MODEM_CONTROL_REGISTER);
822 mod_timer(&mos7840_port->led_timer1,
823 jiffies + msecs_to_jiffies(LED_ON_MS));
826 mos7840_port->read_urb_busy = true;
827 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
830 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
831 mos7840_port->read_urb_busy = false;
835 /*****************************************************************************
836 * mos7840_bulk_out_data_callback
837 * this is the callback function for when we have finished sending
838 * serial data on the bulk out endpoint.
839 *****************************************************************************/
841 static void mos7840_bulk_out_data_callback(struct urb *urb)
843 struct moschip_port *mos7840_port;
844 struct usb_serial_port *port;
845 struct tty_struct *tty;
846 int status = urb->status;
849 mos7840_port = urb->context;
850 port = mos7840_port->port;
851 spin_lock(&mos7840_port->pool_lock);
852 for (i = 0; i < NUM_URBS; i++) {
853 if (urb == mos7840_port->write_urb_pool[i]) {
854 mos7840_port->busy[i] = 0;
858 spin_unlock(&mos7840_port->pool_lock);
861 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
865 if (mos7840_port_paranoia_check(port, __func__))
868 tty = tty_port_tty_get(&port->port);
869 if (tty && mos7840_port->open)
875 /************************************************************************/
876 /* 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 */
877 /************************************************************************/
878 #ifdef MCSSerialProbe
879 static int mos7840_serial_probe(struct usb_serial *serial,
880 const struct usb_device_id *id)
883 /*need to implement the mode_reg reading and updating\
884 structures usb_serial_ device_type\
885 (i.e num_ports, num_bulkin,bulkout etc) */
886 /* Also we can update the changes attach */
891 /*****************************************************************************
893 * this function is called by the tty driver when a port is opened
894 * If successful, we return 0
895 * Otherwise we return a negative error number.
896 *****************************************************************************/
898 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
902 struct usb_serial *serial;
906 struct moschip_port *mos7840_port;
907 struct moschip_port *port0;
909 if (mos7840_port_paranoia_check(port, __func__))
912 serial = port->serial;
914 if (mos7840_serial_paranoia_check(serial, __func__))
917 mos7840_port = mos7840_get_port_private(port);
918 port0 = mos7840_get_port_private(serial->port[0]);
920 if (mos7840_port == NULL || port0 == NULL)
923 usb_clear_halt(serial->dev, port->write_urb->pipe);
924 usb_clear_halt(serial->dev, port->read_urb->pipe);
927 /* Initialising the write urb pool */
928 for (j = 0; j < NUM_URBS; ++j) {
929 urb = usb_alloc_urb(0, GFP_KERNEL);
930 mos7840_port->write_urb_pool[j] = urb;
933 dev_err(&port->dev, "No more urbs???\n");
937 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
939 if (!urb->transfer_buffer) {
941 mos7840_port->write_urb_pool[j] = NULL;
943 "%s-out of memory for urb buffers.\n",
949 /*****************************************************************************
950 * Initialize MCS7840 -- Write Init values to corresponding Registers
958 * 0x08 : SP1/2 Control Reg
959 *****************************************************************************/
961 /* NEED to check the following Block */
964 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
966 dev_dbg(&port->dev, "Reading Spreg failed\n");
970 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
972 dev_dbg(&port->dev, "writing Spreg failed\n");
977 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
979 dev_dbg(&port->dev, "writing Spreg failed\n");
982 /* End of block to be checked */
985 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
988 dev_dbg(&port->dev, "Reading Controlreg failed\n");
991 Data |= 0x08; /* Driver done bit */
992 Data |= 0x20; /* rx_disable */
993 status = mos7840_set_reg_sync(port,
994 mos7840_port->ControlRegOffset, Data);
996 dev_dbg(&port->dev, "writing Controlreg failed\n");
999 /* do register settings here */
1000 /* Set all regs to the device default values. */
1001 /***********************************
1002 * First Disable all interrupts.
1003 ***********************************/
1005 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1007 dev_dbg(&port->dev, "disabling interrupts failed\n");
1010 /* Set FIFO_CONTROL_REGISTER to the default value */
1012 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1014 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
1019 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1021 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
1026 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1027 mos7840_port->shadowLCR = Data;
1030 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1031 mos7840_port->shadowMCR = Data;
1034 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1035 mos7840_port->shadowLCR = Data;
1037 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1038 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1041 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1044 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1047 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1049 Data = Data & ~SERIAL_LCR_DLAB;
1050 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1051 mos7840_port->shadowLCR = Data;
1053 /* clearing Bulkin and Bulkout Fifo */
1055 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1058 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1060 Data = Data & ~0x0c;
1061 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1062 /* Finally enable all interrupts */
1064 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1066 /* clearing rx_disable */
1068 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1070 Data = Data & ~0x20;
1071 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1076 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1079 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1082 /* Check to see if we've set up our endpoint info yet *
1083 * (can't set it up in mos7840_startup as the structures *
1084 * were not set up at that time.) */
1085 if (port0->open_ports == 1) {
1086 if (serial->port[0]->interrupt_in_buffer == NULL) {
1087 /* set up interrupt urb */
1088 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1090 usb_rcvintpipe(serial->dev,
1091 serial->port[0]->interrupt_in_endpointAddress),
1092 serial->port[0]->interrupt_in_buffer,
1093 serial->port[0]->interrupt_in_urb->
1094 transfer_buffer_length,
1095 mos7840_interrupt_callback,
1097 serial->port[0]->interrupt_in_urb->interval);
1099 /* start interrupt read for mos7840 *
1100 * will continue as long as mos7840 is connected */
1103 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1106 dev_err(&port->dev, "%s - Error %d submitting "
1107 "interrupt urb\n", __func__, response);
1114 /* see if we've set up our endpoint info yet *
1115 * (can't set it up in mos7840_startup as the *
1116 * structures were not set up at that time.) */
1118 dev_dbg(&port->dev, "port number is %d\n", port->number);
1119 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1120 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1121 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1122 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1123 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1124 mos7840_port->read_urb = port->read_urb;
1126 /* set up our bulk in urb */
1127 if ((serial->num_ports == 2)
1128 && ((((__u16)port->number -
1129 (__u16)(port->serial->minor)) % 2) != 0)) {
1130 usb_fill_bulk_urb(mos7840_port->read_urb,
1132 usb_rcvbulkpipe(serial->dev,
1133 (port->bulk_in_endpointAddress) + 2),
1134 port->bulk_in_buffer,
1135 mos7840_port->read_urb->transfer_buffer_length,
1136 mos7840_bulk_in_callback, mos7840_port);
1138 usb_fill_bulk_urb(mos7840_port->read_urb,
1140 usb_rcvbulkpipe(serial->dev,
1141 port->bulk_in_endpointAddress),
1142 port->bulk_in_buffer,
1143 mos7840_port->read_urb->transfer_buffer_length,
1144 mos7840_bulk_in_callback, mos7840_port);
1147 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1148 mos7840_port->read_urb_busy = true;
1149 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1151 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1152 __func__, response);
1153 mos7840_port->read_urb_busy = false;
1156 /* initialize our wait queues */
1157 init_waitqueue_head(&mos7840_port->wait_chase);
1158 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1160 /* initialize our icount structure */
1161 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1163 /* initialize our port settings */
1164 /* Must set to enable ints! */
1165 mos7840_port->shadowMCR = MCR_MASTER_IE;
1166 /* send a open port command */
1167 mos7840_port->open = 1;
1168 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1169 mos7840_port->icount.tx = 0;
1170 mos7840_port->icount.rx = 0;
1175 /*****************************************************************************
1176 * mos7840_chars_in_buffer
1177 * this function is called by the tty driver when it wants to know how many
1178 * bytes of data we currently have outstanding in the port (data that has
1179 * been written, but hasn't made it out the port yet)
1180 * If successful, we return the number of bytes left to be written in the
1182 * Otherwise we return zero.
1183 *****************************************************************************/
1185 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1187 struct usb_serial_port *port = tty->driver_data;
1190 unsigned long flags;
1191 struct moschip_port *mos7840_port;
1193 if (mos7840_port_paranoia_check(port, __func__))
1196 mos7840_port = mos7840_get_port_private(port);
1197 if (mos7840_port == NULL)
1200 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1201 for (i = 0; i < NUM_URBS; ++i) {
1202 if (mos7840_port->busy[i]) {
1203 struct urb *urb = mos7840_port->write_urb_pool[i];
1204 chars += urb->transfer_buffer_length;
1207 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1208 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1213 /*****************************************************************************
1215 * this function is called by the tty driver when a port is closed
1216 *****************************************************************************/
1218 static void mos7840_close(struct usb_serial_port *port)
1220 struct usb_serial *serial;
1221 struct moschip_port *mos7840_port;
1222 struct moschip_port *port0;
1226 if (mos7840_port_paranoia_check(port, __func__))
1229 serial = mos7840_get_usb_serial(port, __func__);
1233 mos7840_port = mos7840_get_port_private(port);
1234 port0 = mos7840_get_port_private(serial->port[0]);
1236 if (mos7840_port == NULL || port0 == NULL)
1239 for (j = 0; j < NUM_URBS; ++j)
1240 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1242 /* Freeing Write URBs */
1243 for (j = 0; j < NUM_URBS; ++j) {
1244 if (mos7840_port->write_urb_pool[j]) {
1245 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1246 kfree(mos7840_port->write_urb_pool[j]->
1249 usb_free_urb(mos7840_port->write_urb_pool[j]);
1253 /* While closing port, shutdown all bulk read, write *
1254 * and interrupt read if they exists */
1256 if (mos7840_port->write_urb) {
1257 dev_dbg(&port->dev, "%s", "Shutdown bulk write\n");
1258 usb_kill_urb(mos7840_port->write_urb);
1260 if (mos7840_port->read_urb) {
1261 dev_dbg(&port->dev, "%s", "Shutdown bulk read\n");
1262 usb_kill_urb(mos7840_port->read_urb);
1263 mos7840_port->read_urb_busy = false;
1265 if ((&mos7840_port->control_urb)) {
1266 dev_dbg(&port->dev, "%s", "Shutdown control read\n");
1267 /*/ usb_kill_urb (mos7840_port->control_urb); */
1270 /* if(mos7840_port->ctrl_buf != NULL) */
1271 /* kfree(mos7840_port->ctrl_buf); */
1272 port0->open_ports--;
1273 dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
1274 if (port0->open_ports == 0) {
1275 if (serial->port[0]->interrupt_in_urb) {
1276 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1277 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1281 if (mos7840_port->write_urb) {
1282 /* if this urb had a transfer buffer already (old tx) free it */
1283 if (mos7840_port->write_urb->transfer_buffer != NULL)
1284 kfree(mos7840_port->write_urb->transfer_buffer);
1285 usb_free_urb(mos7840_port->write_urb);
1289 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1292 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1294 mos7840_port->open = 0;
1297 /************************************************************************
1299 * mos7840_block_until_chase_response
1301 * This function will block the close until one of the following:
1302 * 1. Response to our Chase comes from mos7840
1303 * 2. A timeout of 10 seconds without activity has expired
1304 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1306 ************************************************************************/
1308 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1309 struct moschip_port *mos7840_port)
1311 int timeout = msecs_to_jiffies(1000);
1316 count = mos7840_chars_in_buffer(tty);
1318 /* Check for Buffer status */
1322 /* Block the thread for a while */
1323 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1325 /* No activity.. count down section */
1328 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
1331 /* Reset timeout value back to seconds */
1338 /*****************************************************************************
1340 * this function sends a break to the port
1341 *****************************************************************************/
1342 static void mos7840_break(struct tty_struct *tty, int break_state)
1344 struct usb_serial_port *port = tty->driver_data;
1346 struct usb_serial *serial;
1347 struct moschip_port *mos7840_port;
1349 if (mos7840_port_paranoia_check(port, __func__))
1352 serial = mos7840_get_usb_serial(port, __func__);
1356 mos7840_port = mos7840_get_port_private(port);
1358 if (mos7840_port == NULL)
1362 /* flush and block until tx is empty */
1363 mos7840_block_until_chase_response(tty, mos7840_port);
1365 if (break_state == -1)
1366 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1368 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1370 /* FIXME: no locking on shadowLCR anywhere in driver */
1371 mos7840_port->shadowLCR = data;
1372 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1373 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1374 mos7840_port->shadowLCR);
1377 /*****************************************************************************
1378 * mos7840_write_room
1379 * this function is called by the tty driver when it wants to know how many
1380 * bytes of data we can accept for a specific port.
1381 * If successful, we return the amount of room that we have for this port
1382 * Otherwise we return a negative error number.
1383 *****************************************************************************/
1385 static int mos7840_write_room(struct tty_struct *tty)
1387 struct usb_serial_port *port = tty->driver_data;
1390 unsigned long flags;
1391 struct moschip_port *mos7840_port;
1393 if (mos7840_port_paranoia_check(port, __func__))
1396 mos7840_port = mos7840_get_port_private(port);
1397 if (mos7840_port == NULL)
1400 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1401 for (i = 0; i < NUM_URBS; ++i) {
1402 if (!mos7840_port->busy[i])
1403 room += URB_TRANSFER_BUFFER_SIZE;
1405 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1407 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1408 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1413 /*****************************************************************************
1415 * this function is called by the tty driver when data should be written to
1417 * If successful, we return the number of bytes written, otherwise we
1418 * return a negative error number.
1419 *****************************************************************************/
1421 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1422 const unsigned char *data, int count)
1428 unsigned long flags;
1430 struct moschip_port *mos7840_port;
1431 struct usb_serial *serial;
1434 const unsigned char *current_position = data;
1435 unsigned char *data1;
1439 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1440 mos7840_port->shadowLCR = Data;
1441 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1442 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1445 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1446 /* mos7840_port->shadowLCR=Data;//Need to add later */
1448 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1449 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1452 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1454 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1455 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
1458 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1459 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
1461 Data = Data & ~SERIAL_LCR_DLAB;
1462 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1463 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1466 if (mos7840_port_paranoia_check(port, __func__))
1469 serial = port->serial;
1470 if (mos7840_serial_paranoia_check(serial, __func__))
1473 mos7840_port = mos7840_get_port_private(port);
1474 if (mos7840_port == NULL)
1477 /* try to find a free urb in the list */
1480 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1481 for (i = 0; i < NUM_URBS; ++i) {
1482 if (!mos7840_port->busy[i]) {
1483 mos7840_port->busy[i] = 1;
1484 urb = mos7840_port->write_urb_pool[i];
1485 dev_dbg(&port->dev, "URB:%d\n", i);
1489 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1492 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1496 if (urb->transfer_buffer == NULL) {
1497 urb->transfer_buffer =
1498 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1500 if (urb->transfer_buffer == NULL) {
1501 dev_err_console(port, "%s no more kernel memory...\n",
1506 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1508 memcpy(urb->transfer_buffer, current_position, transfer_size);
1510 /* fill urb with data and submit */
1511 if ((serial->num_ports == 2)
1512 && ((((__u16)port->number -
1513 (__u16)(port->serial->minor)) % 2) != 0)) {
1514 usb_fill_bulk_urb(urb,
1516 usb_sndbulkpipe(serial->dev,
1517 (port->bulk_out_endpointAddress) + 2),
1518 urb->transfer_buffer,
1520 mos7840_bulk_out_data_callback, mos7840_port);
1522 usb_fill_bulk_urb(urb,
1524 usb_sndbulkpipe(serial->dev,
1525 port->bulk_out_endpointAddress),
1526 urb->transfer_buffer,
1528 mos7840_bulk_out_data_callback, mos7840_port);
1531 data1 = urb->transfer_buffer;
1532 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1535 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1536 mos7840_port->led_flag = true;
1537 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1538 mod_timer(&mos7840_port->led_timer1,
1539 jiffies + msecs_to_jiffies(LED_ON_MS));
1542 /* send it down the pipe */
1543 status = usb_submit_urb(urb, GFP_ATOMIC);
1546 mos7840_port->busy[i] = 0;
1547 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1548 "with status = %d\n", __func__, status);
1549 bytes_sent = status;
1552 bytes_sent = transfer_size;
1553 mos7840_port->icount.tx += transfer_size;
1555 dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1561 /*****************************************************************************
1563 * this function is called by the tty driver when it wants to stop the data
1564 * being read from the port.
1565 *****************************************************************************/
1567 static void mos7840_throttle(struct tty_struct *tty)
1569 struct usb_serial_port *port = tty->driver_data;
1570 struct moschip_port *mos7840_port;
1573 if (mos7840_port_paranoia_check(port, __func__))
1576 mos7840_port = mos7840_get_port_private(port);
1578 if (mos7840_port == NULL)
1581 if (!mos7840_port->open) {
1582 dev_dbg(&port->dev, "%s", "port not opened\n");
1586 /* if we are implementing XON/XOFF, send the stop character */
1588 unsigned char stop_char = STOP_CHAR(tty);
1589 status = mos7840_write(tty, port, &stop_char, 1);
1593 /* if we are implementing RTS/CTS, toggle that line */
1594 if (tty->termios.c_cflag & CRTSCTS) {
1595 mos7840_port->shadowMCR &= ~MCR_RTS;
1596 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1597 mos7840_port->shadowMCR);
1603 /*****************************************************************************
1604 * mos7840_unthrottle
1605 * this function is called by the tty driver when it wants to resume
1606 * the data being read from the port (called after mos7840_throttle is
1608 *****************************************************************************/
1609 static void mos7840_unthrottle(struct tty_struct *tty)
1611 struct usb_serial_port *port = tty->driver_data;
1613 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1615 if (mos7840_port_paranoia_check(port, __func__))
1618 if (mos7840_port == NULL)
1621 if (!mos7840_port->open) {
1622 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1626 /* if we are implementing XON/XOFF, send the start character */
1628 unsigned char start_char = START_CHAR(tty);
1629 status = mos7840_write(tty, port, &start_char, 1);
1634 /* if we are implementing RTS/CTS, toggle that line */
1635 if (tty->termios.c_cflag & CRTSCTS) {
1636 mos7840_port->shadowMCR |= MCR_RTS;
1637 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1638 mos7840_port->shadowMCR);
1644 static int mos7840_tiocmget(struct tty_struct *tty)
1646 struct usb_serial_port *port = tty->driver_data;
1647 struct moschip_port *mos7840_port;
1648 unsigned int result;
1652 mos7840_port = mos7840_get_port_private(port);
1654 if (mos7840_port == NULL)
1657 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1658 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1659 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1660 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1661 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1662 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1663 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1664 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1665 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1667 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1672 static int mos7840_tiocmset(struct tty_struct *tty,
1673 unsigned int set, unsigned int clear)
1675 struct usb_serial_port *port = tty->driver_data;
1676 struct moschip_port *mos7840_port;
1680 mos7840_port = mos7840_get_port_private(port);
1682 if (mos7840_port == NULL)
1685 /* FIXME: What locks the port registers ? */
1686 mcr = mos7840_port->shadowMCR;
1687 if (clear & TIOCM_RTS)
1689 if (clear & TIOCM_DTR)
1691 if (clear & TIOCM_LOOP)
1692 mcr &= ~MCR_LOOPBACK;
1694 if (set & TIOCM_RTS)
1696 if (set & TIOCM_DTR)
1698 if (set & TIOCM_LOOP)
1699 mcr |= MCR_LOOPBACK;
1701 mos7840_port->shadowMCR = mcr;
1703 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1705 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1712 /*****************************************************************************
1713 * mos7840_calc_baud_rate_divisor
1714 * this function calculates the proper baud rate divisor for the specified
1716 *****************************************************************************/
1717 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1718 int baudRate, int *divisor,
1721 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1723 if (baudRate <= 115200) {
1724 *divisor = 115200 / baudRate;
1727 if ((baudRate > 115200) && (baudRate <= 230400)) {
1728 *divisor = 230400 / baudRate;
1729 *clk_sel_val = 0x10;
1730 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1731 *divisor = 403200 / baudRate;
1732 *clk_sel_val = 0x20;
1733 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1734 *divisor = 460800 / baudRate;
1735 *clk_sel_val = 0x30;
1736 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1737 *divisor = 806400 / baudRate;
1738 *clk_sel_val = 0x40;
1739 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1740 *divisor = 921600 / baudRate;
1741 *clk_sel_val = 0x50;
1742 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1743 *divisor = 1572864 / baudRate;
1744 *clk_sel_val = 0x60;
1745 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1746 *divisor = 3145728 / baudRate;
1747 *clk_sel_val = 0x70;
1753 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1754 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1755 *divisor = mos7840_divisor_table[i].Divisor;
1760 /* After trying for all the standard baud rates *
1761 * Try calculating the divisor for this baud rate */
1763 if (baudrate > 75 && baudrate < 230400) {
1764 /* get the divisor */
1765 custom = (__u16) (230400L / baudrate);
1767 /* Check for round off */
1768 round1 = (__u16) (2304000L / baudrate);
1769 round = (__u16) (round1 - (custom * 10));
1774 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
1778 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
1783 /*****************************************************************************
1784 * mos7840_send_cmd_write_baud_rate
1785 * this function sends the proper command to change the baud rate of the
1787 *****************************************************************************/
1789 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1795 unsigned char number;
1797 struct usb_serial_port *port;
1799 if (mos7840_port == NULL)
1802 port = mos7840_port->port;
1803 if (mos7840_port_paranoia_check(port, __func__))
1806 if (mos7840_serial_paranoia_check(port->serial, __func__))
1809 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1811 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1812 mos7840_port->port->number, baudRate);
1813 /* reset clk_uart_sel in spregOffset */
1814 if (baudRate > 115200) {
1815 #ifdef HW_flow_control
1816 /* NOTE: need to see the pther register to modify */
1817 /* setting h/w flow control bit to 1 */
1819 mos7840_port->shadowMCR = Data;
1820 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1823 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1829 #ifdef HW_flow_control
1830 /* setting h/w flow control bit to 0 */
1832 mos7840_port->shadowMCR = Data;
1833 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1836 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1843 if (1) { /* baudRate <= 115200) */
1846 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1848 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1851 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1854 Data = (Data & 0x8f) | clk_sel_val;
1855 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1858 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1861 /* Calculate the Divisor */
1864 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1867 /* Enable access to divisor latch */
1868 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1869 mos7840_port->shadowLCR = Data;
1870 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1872 /* Write the divisor */
1873 Data = (unsigned char)(divisor & 0xff);
1874 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1875 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1877 Data = (unsigned char)((divisor & 0xff00) >> 8);
1878 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1879 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1881 /* Disable access to divisor latch */
1882 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1883 mos7840_port->shadowLCR = Data;
1884 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1890 /*****************************************************************************
1891 * mos7840_change_port_settings
1892 * This routine is called to set the UART on the device to match
1893 * the specified new settings.
1894 *****************************************************************************/
1896 static void mos7840_change_port_settings(struct tty_struct *tty,
1897 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1907 struct usb_serial_port *port;
1908 struct usb_serial *serial;
1910 if (mos7840_port == NULL)
1913 port = mos7840_port->port;
1915 if (mos7840_port_paranoia_check(port, __func__))
1918 if (mos7840_serial_paranoia_check(port->serial, __func__))
1921 serial = port->serial;
1923 if (!mos7840_port->open) {
1924 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1930 lParity = LCR_PAR_NONE;
1932 cflag = tty->termios.c_cflag;
1933 iflag = tty->termios.c_iflag;
1935 /* Change the number of bits */
1936 if (cflag & CSIZE) {
1937 switch (cflag & CSIZE) {
1955 /* Change the Parity bit */
1956 if (cflag & PARENB) {
1957 if (cflag & PARODD) {
1958 lParity = LCR_PAR_ODD;
1959 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1961 lParity = LCR_PAR_EVEN;
1962 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1966 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1970 lParity = lParity | 0x20;
1972 /* Change the Stop bit */
1973 if (cflag & CSTOPB) {
1975 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1978 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1981 /* Update the LCR with the correct value */
1982 mos7840_port->shadowLCR &=
1983 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1984 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1986 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1987 mos7840_port->shadowLCR);
1988 /* Disable Interrupts */
1990 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1993 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1996 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1998 /* Send the updated LCR value to the mos7840 */
1999 Data = mos7840_port->shadowLCR;
2001 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2004 mos7840_port->shadowMCR = Data;
2005 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2007 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2009 /* set up the MCR register and send it to the mos7840 */
2011 mos7840_port->shadowMCR = MCR_MASTER_IE;
2013 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2015 if (cflag & CRTSCTS)
2016 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2018 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2020 Data = mos7840_port->shadowMCR;
2021 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2023 /* Determine divisor based on baud rate */
2024 baud = tty_get_baud_rate(tty);
2027 /* pick a default, any default... */
2028 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
2032 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
2033 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2035 /* Enable Interrupts */
2037 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2039 if (mos7840_port->read_urb_busy == false) {
2040 mos7840_port->read_urb_busy = true;
2041 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2043 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2045 mos7840_port->read_urb_busy = false;
2048 wake_up(&mos7840_port->delta_msr_wait);
2049 mos7840_port->delta_msr_cond = 1;
2050 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
2051 mos7840_port->shadowLCR);
2054 /*****************************************************************************
2055 * mos7840_set_termios
2056 * this function is called by the tty driver when it wants to change
2057 * the termios structure
2058 *****************************************************************************/
2060 static void mos7840_set_termios(struct tty_struct *tty,
2061 struct usb_serial_port *port,
2062 struct ktermios *old_termios)
2066 struct usb_serial *serial;
2067 struct moschip_port *mos7840_port;
2069 if (mos7840_port_paranoia_check(port, __func__))
2072 serial = port->serial;
2074 if (mos7840_serial_paranoia_check(serial, __func__))
2077 mos7840_port = mos7840_get_port_private(port);
2079 if (mos7840_port == NULL)
2082 if (!mos7840_port->open) {
2083 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
2087 dev_dbg(&port->dev, "%s", "setting termios - \n");
2089 cflag = tty->termios.c_cflag;
2091 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2092 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
2093 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2094 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2095 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
2097 /* change the port settings to the new ones specified */
2099 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2101 if (!mos7840_port->read_urb) {
2102 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
2106 if (mos7840_port->read_urb_busy == false) {
2107 mos7840_port->read_urb_busy = true;
2108 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2110 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2112 mos7840_port->read_urb_busy = false;
2117 /*****************************************************************************
2118 * mos7840_get_lsr_info - get line status register info
2120 * Purpose: Let user call ioctl() to get info when the UART physically
2121 * is emptied. On bus types like RS485, the transmitter must
2122 * release the bus after transmitting. This must be done when
2123 * the transmit shift register is empty, not be done when the
2124 * transmit holding register is empty. This functionality
2125 * allows an RS485 driver to be written in user space.
2126 *****************************************************************************/
2128 static int mos7840_get_lsr_info(struct tty_struct *tty,
2129 unsigned int __user *value)
2132 unsigned int result = 0;
2134 count = mos7840_chars_in_buffer(tty);
2136 result = TIOCSER_TEMT;
2138 if (copy_to_user(value, &result, sizeof(int)))
2143 /*****************************************************************************
2144 * mos7840_get_serial_info
2145 * function to get information about serial port
2146 *****************************************************************************/
2148 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2149 struct serial_struct __user *retinfo)
2151 struct serial_struct tmp;
2153 if (mos7840_port == NULL)
2159 memset(&tmp, 0, sizeof(tmp));
2161 tmp.type = PORT_16550A;
2162 tmp.line = mos7840_port->port->serial->minor;
2163 tmp.port = mos7840_port->port->number;
2165 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2166 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2167 tmp.baud_base = 9600;
2168 tmp.close_delay = 5 * HZ;
2169 tmp.closing_wait = 30 * HZ;
2171 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2176 static int mos7840_get_icount(struct tty_struct *tty,
2177 struct serial_icounter_struct *icount)
2179 struct usb_serial_port *port = tty->driver_data;
2180 struct moschip_port *mos7840_port;
2181 struct async_icount cnow;
2183 mos7840_port = mos7840_get_port_private(port);
2184 cnow = mos7840_port->icount;
2187 icount->cts = cnow.cts;
2188 icount->dsr = cnow.dsr;
2189 icount->rng = cnow.rng;
2190 icount->dcd = cnow.dcd;
2191 icount->rx = cnow.rx;
2192 icount->tx = cnow.tx;
2193 icount->frame = cnow.frame;
2194 icount->overrun = cnow.overrun;
2195 icount->parity = cnow.parity;
2196 icount->brk = cnow.brk;
2197 icount->buf_overrun = cnow.buf_overrun;
2199 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
2200 icount->rx, icount->tx);
2204 /*****************************************************************************
2206 * this function handles any ioctl calls to the driver
2207 *****************************************************************************/
2209 static int mos7840_ioctl(struct tty_struct *tty,
2210 unsigned int cmd, unsigned long arg)
2212 struct usb_serial_port *port = tty->driver_data;
2213 void __user *argp = (void __user *)arg;
2214 struct moschip_port *mos7840_port;
2216 struct async_icount cnow;
2217 struct async_icount cprev;
2219 if (mos7840_port_paranoia_check(port, __func__))
2222 mos7840_port = mos7840_get_port_private(port);
2224 if (mos7840_port == NULL)
2227 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
2230 /* return number of bytes available */
2233 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
2234 return mos7840_get_lsr_info(tty, argp);
2237 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
2238 return mos7840_get_serial_info(mos7840_port, argp);
2241 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
2245 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
2246 cprev = mos7840_port->icount;
2248 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2249 mos7840_port->delta_msr_cond = 0;
2250 wait_event_interruptible(mos7840_port->delta_msr_wait,
2252 delta_msr_cond == 1));
2254 /* see if a signal did it */
2255 if (signal_pending(current))
2256 return -ERESTARTSYS;
2257 cnow = mos7840_port->icount;
2259 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2260 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2261 return -EIO; /* no change => error */
2262 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2263 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2264 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2265 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2276 return -ENOIOCTLCMD;
2279 static int mos7810_check(struct usb_serial *serial)
2281 int i, pass_count = 0;
2282 __u16 data = 0, mcr_data = 0;
2283 __u16 test_pattern = 0x55AA;
2285 /* Store MCR setting */
2286 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2287 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2288 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2290 for (i = 0; i < 16; i++) {
2291 /* Send the 1-bit test pattern out to MCS7810 test pin */
2292 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2293 MCS_WRREQ, MCS_WR_RTYPE,
2294 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2295 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2297 /* Read the test pattern back */
2298 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2299 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2300 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2302 /* If this is a MCS7810 device, both test patterns must match */
2303 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2309 /* Restore MCR setting */
2310 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2311 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2312 0, MOS_WDR_TIMEOUT);
2314 if (pass_count == 16)
2320 static int mos7840_calc_num_ports(struct usb_serial *serial)
2323 int mos7840_num_ports;
2325 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2326 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2327 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2329 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2330 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2331 device_type = serial->dev->descriptor.idProduct;
2333 /* For a MCS7840 device GPIO0 must be set to 1 */
2334 if ((data & 0x01) == 1)
2335 device_type = MOSCHIP_DEVICE_ID_7840;
2336 else if (mos7810_check(serial))
2337 device_type = MOSCHIP_DEVICE_ID_7810;
2339 device_type = MOSCHIP_DEVICE_ID_7820;
2342 mos7840_num_ports = (device_type >> 4) & 0x000F;
2343 serial->num_bulk_in = mos7840_num_ports;
2344 serial->num_bulk_out = mos7840_num_ports;
2345 serial->num_ports = mos7840_num_ports;
2347 return mos7840_num_ports;
2350 /****************************************************************************
2352 ****************************************************************************/
2354 static int mos7840_startup(struct usb_serial *serial)
2356 struct moschip_port *mos7840_port;
2357 struct usb_device *dev;
2363 /* we set up the pointers to the endpoints in the mos7840_open *
2364 * function, as the structures aren't created yet. */
2366 /* set up port private structures */
2367 for (i = 0; i < serial->num_ports; ++i) {
2368 dev_dbg(&dev->dev, "mos7840_startup: configuring port %d............\n", i);
2369 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2370 if (mos7840_port == NULL) {
2371 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2373 i--; /* don't follow NULL pointer cleaning up */
2377 /* Initialize all port interrupt end point to port 0 int
2378 * endpoint. Our device has only one interrupt end point
2379 * common to all port */
2381 mos7840_port->port = serial->port[i];
2382 mos7840_set_port_private(serial->port[i], mos7840_port);
2383 spin_lock_init(&mos7840_port->pool_lock);
2385 /* minor is not initialised until later by
2386 * usb-serial.c:get_free_serial() and cannot therefore be used
2387 * to index device instances */
2388 mos7840_port->port_num = i + 1;
2389 dev_dbg(&dev->dev, "serial->port[i]->number = %d\n", serial->port[i]->number);
2390 dev_dbg(&dev->dev, "serial->port[i]->serial->minor = %d\n", serial->port[i]->serial->minor);
2391 dev_dbg(&dev->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2392 dev_dbg(&dev->dev, "serial->minor = %d\n", serial->minor);
2394 if (mos7840_port->port_num == 1) {
2395 mos7840_port->SpRegOffset = 0x0;
2396 mos7840_port->ControlRegOffset = 0x1;
2397 mos7840_port->DcrRegOffset = 0x4;
2398 } else if ((mos7840_port->port_num == 2)
2399 && (serial->num_ports == 4)) {
2400 mos7840_port->SpRegOffset = 0x8;
2401 mos7840_port->ControlRegOffset = 0x9;
2402 mos7840_port->DcrRegOffset = 0x16;
2403 } else if ((mos7840_port->port_num == 2)
2404 && (serial->num_ports == 2)) {
2405 mos7840_port->SpRegOffset = 0xa;
2406 mos7840_port->ControlRegOffset = 0xb;
2407 mos7840_port->DcrRegOffset = 0x19;
2408 } else if ((mos7840_port->port_num == 3)
2409 && (serial->num_ports == 4)) {
2410 mos7840_port->SpRegOffset = 0xa;
2411 mos7840_port->ControlRegOffset = 0xb;
2412 mos7840_port->DcrRegOffset = 0x19;
2413 } else if ((mos7840_port->port_num == 4)
2414 && (serial->num_ports == 4)) {
2415 mos7840_port->SpRegOffset = 0xc;
2416 mos7840_port->ControlRegOffset = 0xd;
2417 mos7840_port->DcrRegOffset = 0x1c;
2419 mos7840_dump_serial_port(serial->port[i], mos7840_port);
2420 mos7840_set_port_private(serial->port[i], mos7840_port);
2422 /* enable rx_disable bit in control register */
2423 status = mos7840_get_reg_sync(serial->port[i],
2424 mos7840_port->ControlRegOffset, &Data);
2426 dev_dbg(&dev->dev, "Reading ControlReg failed status-0x%x\n", status);
2429 dev_dbg(&dev->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2430 Data |= 0x08; /* setting driver done bit */
2431 Data |= 0x04; /* sp1_bit to have cts change reflect in
2434 /* Data |= 0x20; //rx_disable bit */
2435 status = mos7840_set_reg_sync(serial->port[i],
2436 mos7840_port->ControlRegOffset, Data);
2438 dev_dbg(&dev->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2441 dev_dbg(&dev->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2443 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2446 status = mos7840_set_reg_sync(serial->port[i],
2447 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2449 dev_dbg(&dev->dev, "Writing DCR0 failed status-0x%x\n", status);
2452 dev_dbg(&dev->dev, "DCR0 Writing success status%d\n", status);
2455 status = mos7840_set_reg_sync(serial->port[i],
2456 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2458 dev_dbg(&dev->dev, "Writing DCR1 failed status-0x%x\n", status);
2461 dev_dbg(&dev->dev, "DCR1 Writing success status%d\n", status);
2464 status = mos7840_set_reg_sync(serial->port[i],
2465 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2467 dev_dbg(&dev->dev, "Writing DCR2 failed status-0x%x\n", status);
2470 dev_dbg(&dev->dev, "DCR2 Writing success status%d\n", status);
2472 /* write values in clkstart0x0 and clkmulti 0x20 */
2474 status = mos7840_set_reg_sync(serial->port[i],
2475 CLK_START_VALUE_REGISTER, Data);
2477 dev_dbg(&dev->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2480 dev_dbg(&dev->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2483 status = mos7840_set_reg_sync(serial->port[i],
2484 CLK_MULTI_REGISTER, Data);
2486 dev_dbg(&dev->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2489 dev_dbg(&dev->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2491 /* write value 0x0 to scratchpad register */
2493 status = mos7840_set_uart_reg(serial->port[i],
2494 SCRATCH_PAD_REGISTER, Data);
2496 dev_dbg(&dev->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2499 dev_dbg(&dev->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2501 /* Zero Length flag register */
2502 if ((mos7840_port->port_num != 1)
2503 && (serial->num_ports == 2)) {
2506 status = mos7840_set_reg_sync(serial->port[i],
2508 ((__u16)mos7840_port->port_num)), Data);
2509 dev_dbg(&dev->dev, "ZLIP offset %x\n",
2510 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2512 dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 2, status);
2515 dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 2, status);
2518 status = mos7840_set_reg_sync(serial->port[i],
2520 ((__u16)mos7840_port->port_num) - 0x1), Data);
2521 dev_dbg(&dev->dev, "ZLIP offset %x\n",
2522 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2524 dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 1, status);
2527 dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 1, status);
2530 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2531 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2532 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2534 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2535 !mos7840_port->dr) {
2540 mos7840_port->has_led = false;
2542 /* Initialize LED timers */
2543 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2544 mos7840_port->has_led = true;
2546 init_timer(&mos7840_port->led_timer1);
2547 mos7840_port->led_timer1.function = mos7840_led_off;
2548 mos7840_port->led_timer1.expires =
2549 jiffies + msecs_to_jiffies(LED_ON_MS);
2550 mos7840_port->led_timer1.data =
2551 (unsigned long)mos7840_port;
2553 init_timer(&mos7840_port->led_timer2);
2554 mos7840_port->led_timer2.function =
2555 mos7840_led_flag_off;
2556 mos7840_port->led_timer2.expires =
2557 jiffies + msecs_to_jiffies(LED_OFF_MS);
2558 mos7840_port->led_timer2.data =
2559 (unsigned long)mos7840_port;
2561 mos7840_port->led_flag = false;
2564 mos7840_set_led_sync(serial->port[i],
2565 MODEM_CONTROL_REGISTER, 0x0300);
2569 /* Zero Length flag enable */
2571 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2573 dev_dbg(&dev->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2576 dev_dbg(&dev->dev, "ZLP_REG5 Writing success status%d\n", status);
2578 /* setting configuration feature to one */
2579 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2580 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT);
2583 for (/* nothing */; i >= 0; i--) {
2584 mos7840_port = mos7840_get_port_private(serial->port[i]);
2586 kfree(mos7840_port->dr);
2587 kfree(mos7840_port->ctrl_buf);
2588 usb_free_urb(mos7840_port->control_urb);
2589 kfree(mos7840_port);
2590 serial->port[i] = NULL;
2595 /****************************************************************************
2596 * mos7840_disconnect
2597 * This function is called whenever the device is removed from the usb bus.
2598 ****************************************************************************/
2600 static void mos7840_disconnect(struct usb_serial *serial)
2603 unsigned long flags;
2604 struct moschip_port *mos7840_port;
2606 /* check for the ports to be closed,close the ports and disconnect */
2608 /* free private structure allocated for serial port *
2609 * stop reads and writes on all ports */
2611 for (i = 0; i < serial->num_ports; ++i) {
2612 mos7840_port = mos7840_get_port_private(serial->port[i]);
2614 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2615 mos7840_port->zombie = 1;
2616 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2617 usb_kill_urb(mos7840_port->control_urb);
2622 /****************************************************************************
2624 * This function is called when the usb_serial structure is freed.
2625 ****************************************************************************/
2627 static void mos7840_release(struct usb_serial *serial)
2630 struct moschip_port *mos7840_port;
2632 /* check for the ports to be closed,close the ports and disconnect */
2634 /* free private structure allocated for serial port *
2635 * stop reads and writes on all ports */
2637 for (i = 0; i < serial->num_ports; ++i) {
2638 mos7840_port = mos7840_get_port_private(serial->port[i]);
2640 if (mos7840_port->has_led) {
2642 mos7840_set_led_sync(mos7840_port->port,
2643 MODEM_CONTROL_REGISTER, 0x0300);
2645 del_timer_sync(&mos7840_port->led_timer1);
2646 del_timer_sync(&mos7840_port->led_timer2);
2648 kfree(mos7840_port->ctrl_buf);
2649 kfree(mos7840_port->dr);
2650 kfree(mos7840_port);
2655 static struct usb_serial_driver moschip7840_4port_device = {
2657 .owner = THIS_MODULE,
2660 .description = DRIVER_DESC,
2661 .id_table = id_table,
2663 .open = mos7840_open,
2664 .close = mos7840_close,
2665 .write = mos7840_write,
2666 .write_room = mos7840_write_room,
2667 .chars_in_buffer = mos7840_chars_in_buffer,
2668 .throttle = mos7840_throttle,
2669 .unthrottle = mos7840_unthrottle,
2670 .calc_num_ports = mos7840_calc_num_ports,
2671 #ifdef MCSSerialProbe
2672 .probe = mos7840_serial_probe,
2674 .ioctl = mos7840_ioctl,
2675 .set_termios = mos7840_set_termios,
2676 .break_ctl = mos7840_break,
2677 .tiocmget = mos7840_tiocmget,
2678 .tiocmset = mos7840_tiocmset,
2679 .get_icount = mos7840_get_icount,
2680 .attach = mos7840_startup,
2681 .disconnect = mos7840_disconnect,
2682 .release = mos7840_release,
2683 .read_bulk_callback = mos7840_bulk_in_callback,
2684 .read_int_callback = mos7840_interrupt_callback,
2687 static struct usb_serial_driver * const serial_drivers[] = {
2688 &moschip7840_4port_device, NULL
2691 module_usb_serial_driver(serial_drivers, id_table);
2693 MODULE_DESCRIPTION(DRIVER_DESC);
2694 MODULE_LICENSE("GPL");