2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * Clean ups from Moschip version and a few ioctl implementations by:
17 * Paul B Schroeder <pschroeder "at" uplogix "dot" com>
19 * Originally based on drivers/usb/serial/io_edgeport.c which is:
20 * Copyright (C) 2000 Inside Out Networks, All rights reserved.
21 * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/tty.h>
30 #include <linux/tty_driver.h>
31 #include <linux/tty_flip.h>
32 #include <linux/module.h>
33 #include <linux/serial.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
36 #include <linux/uaccess.h>
41 #define DRIVER_VERSION "1.3.2"
42 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
45 * 16C50 UART register defines
48 #define LCR_BITS_5 0x00 /* 5 bits/char */
49 #define LCR_BITS_6 0x01 /* 6 bits/char */
50 #define LCR_BITS_7 0x02 /* 7 bits/char */
51 #define LCR_BITS_8 0x03 /* 8 bits/char */
52 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
54 #define LCR_STOP_1 0x00 /* 1 stop bit */
55 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
56 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
57 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
59 #define LCR_PAR_NONE 0x00 /* No parity */
60 #define LCR_PAR_ODD 0x08 /* Odd parity */
61 #define LCR_PAR_EVEN 0x18 /* Even parity */
62 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
63 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
64 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
66 #define LCR_SET_BREAK 0x40 /* Set Break condition */
67 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
69 #define MCR_DTR 0x01 /* Assert DTR */
70 #define MCR_RTS 0x02 /* Assert RTS */
71 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
72 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
73 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
74 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
76 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
77 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
78 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
79 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
82 * Defines used for sending commands to port
85 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
87 #define MOS_PORT1 0x0200
88 #define MOS_PORT2 0x0300
89 #define MOS_VENREG 0x0000
90 #define MOS_MAX_PORT 0x02
91 #define MOS_WRITE 0x0E
95 #define MCS_RD_RTYPE 0xC0
96 #define MCS_WR_RTYPE 0x40
97 #define MCS_RDREQ 0x0D
98 #define MCS_WRREQ 0x0E
99 #define MCS_CTRL_TIMEOUT 500
100 #define VENDOR_READ_LENGTH (0x01)
102 #define MAX_NAME_LEN 64
104 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
105 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
107 /* For higher baud Rates use TIOCEXBAUD */
108 #define TIOCEXBAUD 0x5462
110 /* vendor id and device id defines */
112 /* The native mos7840/7820 component */
113 #define USB_VENDOR_ID_MOSCHIP 0x9710
114 #define MOSCHIP_DEVICE_ID_7840 0x7840
115 #define MOSCHIP_DEVICE_ID_7820 0x7820
116 #define MOSCHIP_DEVICE_ID_7810 0x7810
117 /* The native component can have its vendor/device id's overridden
118 * in vendor-specific implementations. Such devices can be handled
119 * by making a change here, in id_table.
121 #define USB_VENDOR_ID_BANDB 0x0856
122 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
123 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
124 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
125 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
126 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
127 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
128 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
129 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
130 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
131 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
132 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
133 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
134 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
136 /* This driver also supports
137 * ATEN UC2324 device using Moschip MCS7840
138 * ATEN UC2322 device using Moschip MCS7820
140 #define USB_VENDOR_ID_ATENINTL 0x0557
141 #define ATENINTL_DEVICE_ID_UC2324 0x2011
142 #define ATENINTL_DEVICE_ID_UC2322 0x7820
144 /* Interrupt Routine Defines */
146 #define SERIAL_IIR_RLS 0x06
147 #define SERIAL_IIR_MS 0x00
150 * Emulation of the bit mask on the LINE STATUS REGISTER.
152 #define SERIAL_LSR_DR 0x0001
153 #define SERIAL_LSR_OE 0x0002
154 #define SERIAL_LSR_PE 0x0004
155 #define SERIAL_LSR_FE 0x0008
156 #define SERIAL_LSR_BI 0x0010
158 #define MOS_MSR_DELTA_CTS 0x10
159 #define MOS_MSR_DELTA_DSR 0x20
160 #define MOS_MSR_DELTA_RI 0x40
161 #define MOS_MSR_DELTA_CD 0x80
163 /* Serial Port register Address */
164 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
165 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
166 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
167 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
168 #define LINE_STATUS_REGISTER ((__u16)(0x05))
169 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
170 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
171 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
172 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
174 #define CLK_MULTI_REGISTER ((__u16)(0x02))
175 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
176 #define GPIO_REGISTER ((__u16)(0x07))
178 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
181 * URB POOL related defines
183 #define NUM_URBS 16 /* URB Count */
184 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
186 /* LED on/off milliseconds*/
187 #define LED_ON_MS 500
188 #define LED_OFF_MS 500
190 static int device_type;
192 static const struct usb_device_id id_table[] = {
193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
194 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
195 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
209 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
210 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
211 {} /* terminating entry */
213 MODULE_DEVICE_TABLE(usb, id_table);
215 /* This structure holds all of the local port information */
217 struct moschip_port {
218 int port_num; /*Actual port number in the device(1,2,etc) */
219 struct urb *write_urb; /* write URB for this port */
220 struct urb *read_urb; /* read URB for this port */
222 __u8 shadowLCR; /* last LCR value received */
223 __u8 shadowMCR; /* last MCR value received */
227 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
228 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
230 struct async_icount icount;
231 struct usb_serial_port *port; /* loop back to the owner of this object */
235 __u8 ControlRegOffset;
237 /* for processing control URBS in interrupt context */
238 struct urb *control_urb;
239 struct usb_ctrlrequest *dr;
243 spinlock_t pool_lock;
244 struct urb *write_urb_pool[NUM_URBS];
248 /* For device(s) with LED indicator */
251 struct timer_list led_timer1; /* Timer for LED on */
252 struct timer_list led_timer2; /* Timer for LED off */
258 * mos7840_set_reg_sync
259 * To set the Control register by calling usb_fill_control_urb function
260 * by passing usb_sndctrlpipe function as parameter.
263 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
266 struct usb_device *dev = port->serial->dev;
268 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
270 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
271 MCS_WR_RTYPE, val, reg, NULL, 0,
276 * mos7840_get_reg_sync
277 * To set the Uart register by calling usb_fill_control_urb function by
278 * passing usb_rcvctrlpipe function as parameter.
281 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
284 struct usb_device *dev = port->serial->dev;
288 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
292 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
293 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
296 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
303 * mos7840_set_uart_reg
304 * To set the Uart register by calling usb_fill_control_urb function by
305 * passing usb_sndctrlpipe function as parameter.
308 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
312 struct usb_device *dev = port->serial->dev;
314 /* For the UART control registers, the application number need
316 if (port->serial->num_ports == 4) {
317 val |= (((__u16) port->number -
318 (__u16) (port->serial->minor)) + 1) << 8;
320 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
321 val |= (((__u16) port->number -
322 (__u16) (port->serial->minor)) + 1) << 8;
324 val |= (((__u16) port->number -
325 (__u16) (port->serial->minor)) + 2) << 8;
328 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
329 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
330 MCS_WR_RTYPE, val, reg, NULL, 0,
336 * mos7840_get_uart_reg
337 * To set the Control register by calling usb_fill_control_urb function
338 * by passing usb_rcvctrlpipe function as parameter.
340 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
343 struct usb_device *dev = port->serial->dev;
348 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
352 /* Wval is same as application number */
353 if (port->serial->num_ports == 4) {
355 (((__u16) port->number - (__u16) (port->serial->minor)) +
358 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
359 Wval = (((__u16) port->number -
360 (__u16) (port->serial->minor)) + 1) << 8;
362 Wval = (((__u16) port->number -
363 (__u16) (port->serial->minor)) + 2) << 8;
366 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
367 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
368 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
376 static void mos7840_dump_serial_port(struct usb_serial_port *port,
377 struct moschip_port *mos7840_port)
380 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
381 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
382 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
386 /************************************************************************/
387 /************************************************************************/
388 /* I N T E R F A C E F U N C T I O N S */
389 /* I N T E R F A C E F U N C T I O N S */
390 /************************************************************************/
391 /************************************************************************/
393 static inline void mos7840_set_port_private(struct usb_serial_port *port,
394 struct moschip_port *data)
396 usb_set_serial_port_data(port, (void *)data);
399 static inline struct moschip_port *mos7840_get_port_private(struct
403 return (struct moschip_port *)usb_get_serial_port_data(port);
406 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
408 struct moschip_port *mos7840_port;
409 struct async_icount *icount;
411 icount = &mos7840_port->icount;
413 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
415 icount = &mos7840_port->icount;
417 /* update input line counters */
418 if (new_msr & MOS_MSR_DELTA_CTS) {
422 if (new_msr & MOS_MSR_DELTA_DSR) {
426 if (new_msr & MOS_MSR_DELTA_CD) {
430 if (new_msr & MOS_MSR_DELTA_RI) {
437 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
439 struct async_icount *icount;
441 if (new_lsr & SERIAL_LSR_BI) {
443 * Parity and Framing errors only count if they
444 * occur exclusive of a break being
447 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
450 /* update input line counters */
451 icount = &port->icount;
452 if (new_lsr & SERIAL_LSR_BI) {
456 if (new_lsr & SERIAL_LSR_OE) {
460 if (new_lsr & SERIAL_LSR_PE) {
464 if (new_lsr & SERIAL_LSR_FE) {
470 /************************************************************************/
471 /************************************************************************/
472 /* U S B C A L L B A C K F U N C T I O N S */
473 /* U S B C A L L B A C K F U N C T I O N S */
474 /************************************************************************/
475 /************************************************************************/
477 static void mos7840_control_callback(struct urb *urb)
480 struct moschip_port *mos7840_port;
481 struct device *dev = &urb->dev->dev;
484 int status = urb->status;
486 mos7840_port = urb->context;
495 /* this urb is terminated, clean up */
496 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
499 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
503 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
504 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
505 mos7840_port->MsrLsr, mos7840_port->port_num);
506 data = urb->transfer_buffer;
507 regval = (__u8) data[0];
508 dev_dbg(dev, "%s data is %x\n", __func__, regval);
509 if (mos7840_port->MsrLsr == 0)
510 mos7840_handle_new_msr(mos7840_port, regval);
511 else if (mos7840_port->MsrLsr == 1)
512 mos7840_handle_new_lsr(mos7840_port, regval);
515 spin_lock(&mos7840_port->pool_lock);
516 if (!mos7840_port->zombie)
517 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
518 spin_unlock(&mos7840_port->pool_lock);
520 dev_err(dev, "%s - Error %d submitting interrupt urb\n",
525 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
528 struct usb_device *dev = mcs->port->serial->dev;
529 struct usb_ctrlrequest *dr = mcs->dr;
530 unsigned char *buffer = mcs->ctrl_buf;
533 dr->bRequestType = MCS_RD_RTYPE;
534 dr->bRequest = MCS_RDREQ;
535 dr->wValue = cpu_to_le16(Wval); /* 0 */
536 dr->wIndex = cpu_to_le16(reg);
537 dr->wLength = cpu_to_le16(2);
539 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
540 (unsigned char *)dr, buffer, 2,
541 mos7840_control_callback, mcs);
542 mcs->control_urb->transfer_buffer_length = 2;
543 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
547 static void mos7840_set_led_callback(struct urb *urb)
549 switch (urb->status) {
556 /* This urb is terminated, clean up */
557 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
558 __func__, urb->status);
561 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
562 __func__, urb->status);
566 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
569 struct usb_device *dev = mcs->port->serial->dev;
570 struct usb_ctrlrequest *dr = mcs->dr;
572 dr->bRequestType = MCS_WR_RTYPE;
573 dr->bRequest = MCS_WRREQ;
574 dr->wValue = cpu_to_le16(wval);
575 dr->wIndex = cpu_to_le16(reg);
576 dr->wLength = cpu_to_le16(0);
578 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
579 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
581 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
584 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
587 struct usb_device *dev = port->serial->dev;
589 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
590 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
593 static void mos7840_led_off(unsigned long arg)
595 struct moschip_port *mcs = (struct moschip_port *) arg;
598 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
599 mod_timer(&mcs->led_timer2,
600 jiffies + msecs_to_jiffies(LED_OFF_MS));
603 static void mos7840_led_flag_off(unsigned long arg)
605 struct moschip_port *mcs = (struct moschip_port *) arg;
607 mcs->led_flag = false;
610 /*****************************************************************************
611 * mos7840_interrupt_callback
612 * this is the callback function for when we have received data on the
613 * interrupt endpoint.
614 *****************************************************************************/
616 static void mos7840_interrupt_callback(struct urb *urb)
620 struct moschip_port *mos7840_port;
621 struct usb_serial *serial;
626 __u16 wval, wreg = 0;
627 int status = urb->status;
636 /* this urb is terminated, clean up */
637 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
641 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
646 length = urb->actual_length;
647 data = urb->transfer_buffer;
649 serial = urb->context;
651 /* Moschip get 5 bytes
652 * Byte 1 IIR Port 1 (port.number is 0)
653 * Byte 2 IIR Port 2 (port.number is 1)
654 * Byte 3 IIR Port 3 (port.number is 2)
655 * Byte 4 IIR Port 4 (port.number is 3)
656 * Byte 5 FIFO status for both */
658 if (length && length > 5) {
659 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
663 sp[0] = (__u8) data[0];
664 sp[1] = (__u8) data[1];
665 sp[2] = (__u8) data[2];
666 sp[3] = (__u8) data[3];
669 for (i = 0; i < serial->num_ports; i++) {
670 mos7840_port = mos7840_get_port_private(serial->port[i]);
672 (((__u16) serial->port[i]->number -
673 (__u16) (serial->minor)) + 1) << 8;
674 if (mos7840_port->open) {
676 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
678 switch (sp[i] & 0x0f) {
680 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
681 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
682 mos7840_port->MsrLsr = 1;
683 wreg = LINE_STATUS_REGISTER;
686 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
687 mos7840_port->MsrLsr = 0;
688 wreg = MODEM_STATUS_REGISTER;
691 spin_lock(&mos7840_port->pool_lock);
692 if (!mos7840_port->zombie) {
693 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
695 spin_unlock(&mos7840_port->pool_lock);
698 spin_unlock(&mos7840_port->pool_lock);
703 /* the completion handler for the control urb will resubmit */
706 result = usb_submit_urb(urb, GFP_ATOMIC);
708 dev_err(&urb->dev->dev,
709 "%s - Error %d submitting interrupt urb\n",
714 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
715 const char *function)
718 pr_debug("%s - port == NULL\n", function);
722 pr_debug("%s - port->serial == NULL\n", function);
729 /* Inline functions to check the sanity of a pointer that is passed to us */
730 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
731 const char *function)
734 pr_debug("%s - serial == NULL\n", function);
738 pr_debug("%s - serial->type == NULL!\n", function);
745 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
746 const char *function)
748 /* if no port was specified, or it fails a paranoia check */
750 mos7840_port_paranoia_check(port, function) ||
751 mos7840_serial_paranoia_check(port->serial, function)) {
752 /* then say that we don't have a valid usb_serial thing,
753 * which will end up genrating -ENODEV return values */
760 /*****************************************************************************
761 * mos7840_bulk_in_callback
762 * this is the callback function for when we have received data on the
764 *****************************************************************************/
766 static void mos7840_bulk_in_callback(struct urb *urb)
770 struct usb_serial *serial;
771 struct usb_serial_port *port;
772 struct moschip_port *mos7840_port;
773 struct tty_struct *tty;
774 int status = urb->status;
776 mos7840_port = urb->context;
781 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
782 mos7840_port->read_urb_busy = false;
786 port = mos7840_port->port;
787 if (mos7840_port_paranoia_check(port, __func__)) {
788 mos7840_port->read_urb_busy = false;
792 serial = mos7840_get_usb_serial(port, __func__);
794 mos7840_port->read_urb_busy = false;
798 data = urb->transfer_buffer;
799 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
801 if (urb->actual_length) {
802 tty = tty_port_tty_get(&mos7840_port->port->port);
804 tty_insert_flip_string(tty, data, urb->actual_length);
805 tty_flip_buffer_push(tty);
808 mos7840_port->icount.rx += urb->actual_length;
810 dev_dbg(&port->dev, "mos7840_port->icount.rx is %d:\n", mos7840_port->icount.rx);
813 if (!mos7840_port->read_urb) {
814 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
815 mos7840_port->read_urb_busy = false;
820 if (mos7840_port->has_led && !mos7840_port->led_flag) {
821 mos7840_port->led_flag = true;
822 mos7840_set_led_async(mos7840_port, 0x0301,
823 MODEM_CONTROL_REGISTER);
824 mod_timer(&mos7840_port->led_timer1,
825 jiffies + msecs_to_jiffies(LED_ON_MS));
828 mos7840_port->read_urb_busy = true;
829 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
832 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
833 mos7840_port->read_urb_busy = false;
837 /*****************************************************************************
838 * mos7840_bulk_out_data_callback
839 * this is the callback function for when we have finished sending
840 * serial data on the bulk out endpoint.
841 *****************************************************************************/
843 static void mos7840_bulk_out_data_callback(struct urb *urb)
845 struct moschip_port *mos7840_port;
846 struct usb_serial_port *port;
847 struct tty_struct *tty;
848 int status = urb->status;
851 mos7840_port = urb->context;
852 port = mos7840_port->port;
853 spin_lock(&mos7840_port->pool_lock);
854 for (i = 0; i < NUM_URBS; i++) {
855 if (urb == mos7840_port->write_urb_pool[i]) {
856 mos7840_port->busy[i] = 0;
860 spin_unlock(&mos7840_port->pool_lock);
863 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
867 if (mos7840_port_paranoia_check(port, __func__))
870 tty = tty_port_tty_get(&port->port);
871 if (tty && mos7840_port->open)
877 /************************************************************************/
878 /* 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 */
879 /************************************************************************/
880 #ifdef MCSSerialProbe
881 static int mos7840_serial_probe(struct usb_serial *serial,
882 const struct usb_device_id *id)
885 /*need to implement the mode_reg reading and updating\
886 structures usb_serial_ device_type\
887 (i.e num_ports, num_bulkin,bulkout etc) */
888 /* Also we can update the changes attach */
893 /*****************************************************************************
895 * this function is called by the tty driver when a port is opened
896 * If successful, we return 0
897 * Otherwise we return a negative error number.
898 *****************************************************************************/
900 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
904 struct usb_serial *serial;
908 struct moschip_port *mos7840_port;
909 struct moschip_port *port0;
911 if (mos7840_port_paranoia_check(port, __func__))
914 serial = port->serial;
916 if (mos7840_serial_paranoia_check(serial, __func__))
919 mos7840_port = mos7840_get_port_private(port);
920 port0 = mos7840_get_port_private(serial->port[0]);
922 if (mos7840_port == NULL || port0 == NULL)
925 usb_clear_halt(serial->dev, port->write_urb->pipe);
926 usb_clear_halt(serial->dev, port->read_urb->pipe);
929 /* Initialising the write urb pool */
930 for (j = 0; j < NUM_URBS; ++j) {
931 urb = usb_alloc_urb(0, GFP_KERNEL);
932 mos7840_port->write_urb_pool[j] = urb;
935 dev_err(&port->dev, "No more urbs???\n");
939 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
941 if (!urb->transfer_buffer) {
943 mos7840_port->write_urb_pool[j] = NULL;
945 "%s-out of memory for urb buffers.\n",
951 /*****************************************************************************
952 * Initialize MCS7840 -- Write Init values to corresponding Registers
960 * 0x08 : SP1/2 Control Reg
961 *****************************************************************************/
963 /* NEED to check the following Block */
966 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
968 dev_dbg(&port->dev, "Reading Spreg failed\n");
972 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
974 dev_dbg(&port->dev, "writing Spreg failed\n");
979 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
981 dev_dbg(&port->dev, "writing Spreg failed\n");
984 /* End of block to be checked */
987 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
990 dev_dbg(&port->dev, "Reading Controlreg failed\n");
993 Data |= 0x08; /* Driver done bit */
994 Data |= 0x20; /* rx_disable */
995 status = mos7840_set_reg_sync(port,
996 mos7840_port->ControlRegOffset, Data);
998 dev_dbg(&port->dev, "writing Controlreg failed\n");
1001 /* do register settings here */
1002 /* Set all regs to the device default values. */
1003 /***********************************
1004 * First Disable all interrupts.
1005 ***********************************/
1007 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1009 dev_dbg(&port->dev, "disabling interrupts failed\n");
1012 /* Set FIFO_CONTROL_REGISTER to the default value */
1014 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1016 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
1021 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1023 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
1028 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1029 mos7840_port->shadowLCR = Data;
1032 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1033 mos7840_port->shadowMCR = Data;
1036 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1037 mos7840_port->shadowLCR = Data;
1039 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1040 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1043 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1046 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1049 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1051 Data = Data & ~SERIAL_LCR_DLAB;
1052 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1053 mos7840_port->shadowLCR = Data;
1055 /* clearing Bulkin and Bulkout Fifo */
1057 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1060 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1062 Data = Data & ~0x0c;
1063 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1064 /* Finally enable all interrupts */
1066 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1068 /* clearing rx_disable */
1070 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1072 Data = Data & ~0x20;
1073 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1078 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1081 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1084 /* Check to see if we've set up our endpoint info yet *
1085 * (can't set it up in mos7840_startup as the structures *
1086 * were not set up at that time.) */
1087 if (port0->open_ports == 1) {
1088 if (serial->port[0]->interrupt_in_buffer == NULL) {
1089 /* set up interrupt urb */
1090 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1092 usb_rcvintpipe(serial->dev,
1093 serial->port[0]->interrupt_in_endpointAddress),
1094 serial->port[0]->interrupt_in_buffer,
1095 serial->port[0]->interrupt_in_urb->
1096 transfer_buffer_length,
1097 mos7840_interrupt_callback,
1099 serial->port[0]->interrupt_in_urb->interval);
1101 /* start interrupt read for mos7840 *
1102 * will continue as long as mos7840 is connected */
1105 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1108 dev_err(&port->dev, "%s - Error %d submitting "
1109 "interrupt urb\n", __func__, response);
1116 /* see if we've set up our endpoint info yet *
1117 * (can't set it up in mos7840_startup as the *
1118 * structures were not set up at that time.) */
1120 dev_dbg(&port->dev, "port number is %d\n", port->number);
1121 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1122 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1123 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1124 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1125 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1126 mos7840_port->read_urb = port->read_urb;
1128 /* set up our bulk in urb */
1129 if ((serial->num_ports == 2)
1130 && ((((__u16)port->number -
1131 (__u16)(port->serial->minor)) % 2) != 0)) {
1132 usb_fill_bulk_urb(mos7840_port->read_urb,
1134 usb_rcvbulkpipe(serial->dev,
1135 (port->bulk_in_endpointAddress) + 2),
1136 port->bulk_in_buffer,
1137 mos7840_port->read_urb->transfer_buffer_length,
1138 mos7840_bulk_in_callback, mos7840_port);
1140 usb_fill_bulk_urb(mos7840_port->read_urb,
1142 usb_rcvbulkpipe(serial->dev,
1143 port->bulk_in_endpointAddress),
1144 port->bulk_in_buffer,
1145 mos7840_port->read_urb->transfer_buffer_length,
1146 mos7840_bulk_in_callback, mos7840_port);
1149 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1150 mos7840_port->read_urb_busy = true;
1151 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1153 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1154 __func__, response);
1155 mos7840_port->read_urb_busy = false;
1158 /* initialize our wait queues */
1159 init_waitqueue_head(&mos7840_port->wait_chase);
1160 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1162 /* initialize our icount structure */
1163 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1165 /* initialize our port settings */
1166 /* Must set to enable ints! */
1167 mos7840_port->shadowMCR = MCR_MASTER_IE;
1168 /* send a open port command */
1169 mos7840_port->open = 1;
1170 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1171 mos7840_port->icount.tx = 0;
1172 mos7840_port->icount.rx = 0;
1177 /*****************************************************************************
1178 * mos7840_chars_in_buffer
1179 * this function is called by the tty driver when it wants to know how many
1180 * bytes of data we currently have outstanding in the port (data that has
1181 * been written, but hasn't made it out the port yet)
1182 * If successful, we return the number of bytes left to be written in the
1184 * Otherwise we return zero.
1185 *****************************************************************************/
1187 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1189 struct usb_serial_port *port = tty->driver_data;
1192 unsigned long flags;
1193 struct moschip_port *mos7840_port;
1195 if (mos7840_port_paranoia_check(port, __func__))
1198 mos7840_port = mos7840_get_port_private(port);
1199 if (mos7840_port == NULL)
1202 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1203 for (i = 0; i < NUM_URBS; ++i) {
1204 if (mos7840_port->busy[i]) {
1205 struct urb *urb = mos7840_port->write_urb_pool[i];
1206 chars += urb->transfer_buffer_length;
1209 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1210 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1215 /*****************************************************************************
1217 * this function is called by the tty driver when a port is closed
1218 *****************************************************************************/
1220 static void mos7840_close(struct usb_serial_port *port)
1222 struct usb_serial *serial;
1223 struct moschip_port *mos7840_port;
1224 struct moschip_port *port0;
1228 if (mos7840_port_paranoia_check(port, __func__))
1231 serial = mos7840_get_usb_serial(port, __func__);
1235 mos7840_port = mos7840_get_port_private(port);
1236 port0 = mos7840_get_port_private(serial->port[0]);
1238 if (mos7840_port == NULL || port0 == NULL)
1241 for (j = 0; j < NUM_URBS; ++j)
1242 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1244 /* Freeing Write URBs */
1245 for (j = 0; j < NUM_URBS; ++j) {
1246 if (mos7840_port->write_urb_pool[j]) {
1247 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1248 kfree(mos7840_port->write_urb_pool[j]->
1251 usb_free_urb(mos7840_port->write_urb_pool[j]);
1255 /* While closing port, shutdown all bulk read, write *
1256 * and interrupt read if they exists */
1258 if (mos7840_port->write_urb) {
1259 dev_dbg(&port->dev, "%s", "Shutdown bulk write\n");
1260 usb_kill_urb(mos7840_port->write_urb);
1262 if (mos7840_port->read_urb) {
1263 dev_dbg(&port->dev, "%s", "Shutdown bulk read\n");
1264 usb_kill_urb(mos7840_port->read_urb);
1265 mos7840_port->read_urb_busy = false;
1267 if ((&mos7840_port->control_urb)) {
1268 dev_dbg(&port->dev, "%s", "Shutdown control read\n");
1269 /*/ usb_kill_urb (mos7840_port->control_urb); */
1272 /* if(mos7840_port->ctrl_buf != NULL) */
1273 /* kfree(mos7840_port->ctrl_buf); */
1274 port0->open_ports--;
1275 dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
1276 if (port0->open_ports == 0) {
1277 if (serial->port[0]->interrupt_in_urb) {
1278 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1279 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1283 if (mos7840_port->write_urb) {
1284 /* if this urb had a transfer buffer already (old tx) free it */
1285 if (mos7840_port->write_urb->transfer_buffer != NULL)
1286 kfree(mos7840_port->write_urb->transfer_buffer);
1287 usb_free_urb(mos7840_port->write_urb);
1291 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1294 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1296 mos7840_port->open = 0;
1299 /************************************************************************
1301 * mos7840_block_until_chase_response
1303 * This function will block the close until one of the following:
1304 * 1. Response to our Chase comes from mos7840
1305 * 2. A timeout of 10 seconds without activity has expired
1306 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1308 ************************************************************************/
1310 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1311 struct moschip_port *mos7840_port)
1313 int timeout = msecs_to_jiffies(1000);
1318 count = mos7840_chars_in_buffer(tty);
1320 /* Check for Buffer status */
1324 /* Block the thread for a while */
1325 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1327 /* No activity.. count down section */
1330 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
1333 /* Reset timeout value back to seconds */
1340 /*****************************************************************************
1342 * this function sends a break to the port
1343 *****************************************************************************/
1344 static void mos7840_break(struct tty_struct *tty, int break_state)
1346 struct usb_serial_port *port = tty->driver_data;
1348 struct usb_serial *serial;
1349 struct moschip_port *mos7840_port;
1351 if (mos7840_port_paranoia_check(port, __func__))
1354 serial = mos7840_get_usb_serial(port, __func__);
1358 mos7840_port = mos7840_get_port_private(port);
1360 if (mos7840_port == NULL)
1364 /* flush and block until tx is empty */
1365 mos7840_block_until_chase_response(tty, mos7840_port);
1367 if (break_state == -1)
1368 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1370 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1372 /* FIXME: no locking on shadowLCR anywhere in driver */
1373 mos7840_port->shadowLCR = data;
1374 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1375 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1376 mos7840_port->shadowLCR);
1379 /*****************************************************************************
1380 * mos7840_write_room
1381 * this function is called by the tty driver when it wants to know how many
1382 * bytes of data we can accept for a specific port.
1383 * If successful, we return the amount of room that we have for this port
1384 * Otherwise we return a negative error number.
1385 *****************************************************************************/
1387 static int mos7840_write_room(struct tty_struct *tty)
1389 struct usb_serial_port *port = tty->driver_data;
1392 unsigned long flags;
1393 struct moschip_port *mos7840_port;
1395 if (mos7840_port_paranoia_check(port, __func__))
1398 mos7840_port = mos7840_get_port_private(port);
1399 if (mos7840_port == NULL)
1402 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1403 for (i = 0; i < NUM_URBS; ++i) {
1404 if (!mos7840_port->busy[i])
1405 room += URB_TRANSFER_BUFFER_SIZE;
1407 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1409 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1410 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1415 /*****************************************************************************
1417 * this function is called by the tty driver when data should be written to
1419 * If successful, we return the number of bytes written, otherwise we
1420 * return a negative error number.
1421 *****************************************************************************/
1423 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1424 const unsigned char *data, int count)
1430 unsigned long flags;
1432 struct moschip_port *mos7840_port;
1433 struct usb_serial *serial;
1436 const unsigned char *current_position = data;
1437 unsigned char *data1;
1441 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1442 mos7840_port->shadowLCR = Data;
1443 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1444 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1447 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1448 /* mos7840_port->shadowLCR=Data;//Need to add later */
1450 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1451 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1454 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1456 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1457 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
1460 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1461 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
1463 Data = Data & ~SERIAL_LCR_DLAB;
1464 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1465 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1468 if (mos7840_port_paranoia_check(port, __func__))
1471 serial = port->serial;
1472 if (mos7840_serial_paranoia_check(serial, __func__))
1475 mos7840_port = mos7840_get_port_private(port);
1476 if (mos7840_port == NULL)
1479 /* try to find a free urb in the list */
1482 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1483 for (i = 0; i < NUM_URBS; ++i) {
1484 if (!mos7840_port->busy[i]) {
1485 mos7840_port->busy[i] = 1;
1486 urb = mos7840_port->write_urb_pool[i];
1487 dev_dbg(&port->dev, "URB:%d\n", i);
1491 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1494 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1498 if (urb->transfer_buffer == NULL) {
1499 urb->transfer_buffer =
1500 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1502 if (urb->transfer_buffer == NULL) {
1503 dev_err_console(port, "%s no more kernel memory...\n",
1508 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1510 memcpy(urb->transfer_buffer, current_position, transfer_size);
1512 /* fill urb with data and submit */
1513 if ((serial->num_ports == 2)
1514 && ((((__u16)port->number -
1515 (__u16)(port->serial->minor)) % 2) != 0)) {
1516 usb_fill_bulk_urb(urb,
1518 usb_sndbulkpipe(serial->dev,
1519 (port->bulk_out_endpointAddress) + 2),
1520 urb->transfer_buffer,
1522 mos7840_bulk_out_data_callback, mos7840_port);
1524 usb_fill_bulk_urb(urb,
1526 usb_sndbulkpipe(serial->dev,
1527 port->bulk_out_endpointAddress),
1528 urb->transfer_buffer,
1530 mos7840_bulk_out_data_callback, mos7840_port);
1533 data1 = urb->transfer_buffer;
1534 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1537 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1538 mos7840_port->led_flag = true;
1539 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1540 mod_timer(&mos7840_port->led_timer1,
1541 jiffies + msecs_to_jiffies(LED_ON_MS));
1544 /* send it down the pipe */
1545 status = usb_submit_urb(urb, GFP_ATOMIC);
1548 mos7840_port->busy[i] = 0;
1549 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1550 "with status = %d\n", __func__, status);
1551 bytes_sent = status;
1554 bytes_sent = transfer_size;
1555 mos7840_port->icount.tx += transfer_size;
1557 dev_dbg(&port->dev, "mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1563 /*****************************************************************************
1565 * this function is called by the tty driver when it wants to stop the data
1566 * being read from the port.
1567 *****************************************************************************/
1569 static void mos7840_throttle(struct tty_struct *tty)
1571 struct usb_serial_port *port = tty->driver_data;
1572 struct moschip_port *mos7840_port;
1575 if (mos7840_port_paranoia_check(port, __func__))
1578 mos7840_port = mos7840_get_port_private(port);
1580 if (mos7840_port == NULL)
1583 if (!mos7840_port->open) {
1584 dev_dbg(&port->dev, "%s", "port not opened\n");
1588 /* if we are implementing XON/XOFF, send the stop character */
1590 unsigned char stop_char = STOP_CHAR(tty);
1591 status = mos7840_write(tty, port, &stop_char, 1);
1595 /* if we are implementing RTS/CTS, toggle that line */
1596 if (tty->termios->c_cflag & CRTSCTS) {
1597 mos7840_port->shadowMCR &= ~MCR_RTS;
1598 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1599 mos7840_port->shadowMCR);
1605 /*****************************************************************************
1606 * mos7840_unthrottle
1607 * this function is called by the tty driver when it wants to resume
1608 * the data being read from the port (called after mos7840_throttle is
1610 *****************************************************************************/
1611 static void mos7840_unthrottle(struct tty_struct *tty)
1613 struct usb_serial_port *port = tty->driver_data;
1615 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1617 if (mos7840_port_paranoia_check(port, __func__))
1620 if (mos7840_port == NULL)
1623 if (!mos7840_port->open) {
1624 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1628 /* if we are implementing XON/XOFF, send the start character */
1630 unsigned char start_char = START_CHAR(tty);
1631 status = mos7840_write(tty, port, &start_char, 1);
1636 /* if we are implementing RTS/CTS, toggle that line */
1637 if (tty->termios->c_cflag & CRTSCTS) {
1638 mos7840_port->shadowMCR |= MCR_RTS;
1639 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1640 mos7840_port->shadowMCR);
1646 static int mos7840_tiocmget(struct tty_struct *tty)
1648 struct usb_serial_port *port = tty->driver_data;
1649 struct moschip_port *mos7840_port;
1650 unsigned int result;
1654 mos7840_port = mos7840_get_port_private(port);
1656 if (mos7840_port == NULL)
1659 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1660 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1661 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1662 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1663 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1664 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1665 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1666 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1667 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1669 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1674 static int mos7840_tiocmset(struct tty_struct *tty,
1675 unsigned int set, unsigned int clear)
1677 struct usb_serial_port *port = tty->driver_data;
1678 struct moschip_port *mos7840_port;
1682 mos7840_port = mos7840_get_port_private(port);
1684 if (mos7840_port == NULL)
1687 /* FIXME: What locks the port registers ? */
1688 mcr = mos7840_port->shadowMCR;
1689 if (clear & TIOCM_RTS)
1691 if (clear & TIOCM_DTR)
1693 if (clear & TIOCM_LOOP)
1694 mcr &= ~MCR_LOOPBACK;
1696 if (set & TIOCM_RTS)
1698 if (set & TIOCM_DTR)
1700 if (set & TIOCM_LOOP)
1701 mcr |= MCR_LOOPBACK;
1703 mos7840_port->shadowMCR = mcr;
1705 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1707 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1714 /*****************************************************************************
1715 * mos7840_calc_baud_rate_divisor
1716 * this function calculates the proper baud rate divisor for the specified
1718 *****************************************************************************/
1719 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1720 int baudRate, int *divisor,
1723 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1725 if (baudRate <= 115200) {
1726 *divisor = 115200 / baudRate;
1729 if ((baudRate > 115200) && (baudRate <= 230400)) {
1730 *divisor = 230400 / baudRate;
1731 *clk_sel_val = 0x10;
1732 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1733 *divisor = 403200 / baudRate;
1734 *clk_sel_val = 0x20;
1735 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1736 *divisor = 460800 / baudRate;
1737 *clk_sel_val = 0x30;
1738 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1739 *divisor = 806400 / baudRate;
1740 *clk_sel_val = 0x40;
1741 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1742 *divisor = 921600 / baudRate;
1743 *clk_sel_val = 0x50;
1744 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1745 *divisor = 1572864 / baudRate;
1746 *clk_sel_val = 0x60;
1747 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1748 *divisor = 3145728 / baudRate;
1749 *clk_sel_val = 0x70;
1755 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1756 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1757 *divisor = mos7840_divisor_table[i].Divisor;
1762 /* After trying for all the standard baud rates *
1763 * Try calculating the divisor for this baud rate */
1765 if (baudrate > 75 && baudrate < 230400) {
1766 /* get the divisor */
1767 custom = (__u16) (230400L / baudrate);
1769 /* Check for round off */
1770 round1 = (__u16) (2304000L / baudrate);
1771 round = (__u16) (round1 - (custom * 10));
1776 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
1780 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
1785 /*****************************************************************************
1786 * mos7840_send_cmd_write_baud_rate
1787 * this function sends the proper command to change the baud rate of the
1789 *****************************************************************************/
1791 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1797 unsigned char number;
1799 struct usb_serial_port *port;
1801 if (mos7840_port == NULL)
1804 port = mos7840_port->port;
1805 if (mos7840_port_paranoia_check(port, __func__))
1808 if (mos7840_serial_paranoia_check(port->serial, __func__))
1811 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1813 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1814 mos7840_port->port->number, baudRate);
1815 /* reset clk_uart_sel in spregOffset */
1816 if (baudRate > 115200) {
1817 #ifdef HW_flow_control
1818 /* NOTE: need to see the pther register to modify */
1819 /* setting h/w flow control bit to 1 */
1821 mos7840_port->shadowMCR = Data;
1822 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1825 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1831 #ifdef HW_flow_control
1832 /* setting h/w flow control bit to 0 */
1834 mos7840_port->shadowMCR = Data;
1835 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1838 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1845 if (1) { /* baudRate <= 115200) */
1848 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1850 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1853 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1856 Data = (Data & 0x8f) | clk_sel_val;
1857 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1860 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1863 /* Calculate the Divisor */
1866 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1869 /* Enable 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);
1874 /* Write the divisor */
1875 Data = (unsigned char)(divisor & 0xff);
1876 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1877 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1879 Data = (unsigned char)((divisor & 0xff00) >> 8);
1880 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1881 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1883 /* Disable access to divisor latch */
1884 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1885 mos7840_port->shadowLCR = Data;
1886 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1892 /*****************************************************************************
1893 * mos7840_change_port_settings
1894 * This routine is called to set the UART on the device to match
1895 * the specified new settings.
1896 *****************************************************************************/
1898 static void mos7840_change_port_settings(struct tty_struct *tty,
1899 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1909 struct usb_serial_port *port;
1910 struct usb_serial *serial;
1912 if (mos7840_port == NULL)
1915 port = mos7840_port->port;
1917 if (mos7840_port_paranoia_check(port, __func__))
1920 if (mos7840_serial_paranoia_check(port->serial, __func__))
1923 serial = port->serial;
1925 if (!mos7840_port->open) {
1926 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1932 lParity = LCR_PAR_NONE;
1934 cflag = tty->termios->c_cflag;
1935 iflag = tty->termios->c_iflag;
1937 /* Change the number of bits */
1938 if (cflag & CSIZE) {
1939 switch (cflag & CSIZE) {
1957 /* Change the Parity bit */
1958 if (cflag & PARENB) {
1959 if (cflag & PARODD) {
1960 lParity = LCR_PAR_ODD;
1961 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1963 lParity = LCR_PAR_EVEN;
1964 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1968 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1972 lParity = lParity | 0x20;
1974 /* Change the Stop bit */
1975 if (cflag & CSTOPB) {
1977 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1980 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1983 /* Update the LCR with the correct value */
1984 mos7840_port->shadowLCR &=
1985 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1986 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1988 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1989 mos7840_port->shadowLCR);
1990 /* Disable Interrupts */
1992 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1995 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1998 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2000 /* Send the updated LCR value to the mos7840 */
2001 Data = mos7840_port->shadowLCR;
2003 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2006 mos7840_port->shadowMCR = Data;
2007 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2009 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2011 /* set up the MCR register and send it to the mos7840 */
2013 mos7840_port->shadowMCR = MCR_MASTER_IE;
2015 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2017 if (cflag & CRTSCTS)
2018 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2020 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2022 Data = mos7840_port->shadowMCR;
2023 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2025 /* Determine divisor based on baud rate */
2026 baud = tty_get_baud_rate(tty);
2029 /* pick a default, any default... */
2030 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
2034 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
2035 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2037 /* Enable Interrupts */
2039 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2041 if (mos7840_port->read_urb_busy == false) {
2042 mos7840_port->read_urb_busy = true;
2043 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2045 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2047 mos7840_port->read_urb_busy = false;
2050 wake_up(&mos7840_port->delta_msr_wait);
2051 mos7840_port->delta_msr_cond = 1;
2052 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
2053 mos7840_port->shadowLCR);
2056 /*****************************************************************************
2057 * mos7840_set_termios
2058 * this function is called by the tty driver when it wants to change
2059 * the termios structure
2060 *****************************************************************************/
2062 static void mos7840_set_termios(struct tty_struct *tty,
2063 struct usb_serial_port *port,
2064 struct ktermios *old_termios)
2068 struct usb_serial *serial;
2069 struct moschip_port *mos7840_port;
2071 if (mos7840_port_paranoia_check(port, __func__))
2074 serial = port->serial;
2076 if (mos7840_serial_paranoia_check(serial, __func__))
2079 mos7840_port = mos7840_get_port_private(port);
2081 if (mos7840_port == NULL)
2084 if (!mos7840_port->open) {
2085 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
2089 dev_dbg(&port->dev, "%s", "setting termios - \n");
2091 cflag = tty->termios->c_cflag;
2093 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2094 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2095 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2096 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2097 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
2099 /* change the port settings to the new ones specified */
2101 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2103 if (!mos7840_port->read_urb) {
2104 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
2108 if (mos7840_port->read_urb_busy == false) {
2109 mos7840_port->read_urb_busy = true;
2110 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2112 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2114 mos7840_port->read_urb_busy = false;
2119 /*****************************************************************************
2120 * mos7840_get_lsr_info - get line status register info
2122 * Purpose: Let user call ioctl() to get info when the UART physically
2123 * is emptied. On bus types like RS485, the transmitter must
2124 * release the bus after transmitting. This must be done when
2125 * the transmit shift register is empty, not be done when the
2126 * transmit holding register is empty. This functionality
2127 * allows an RS485 driver to be written in user space.
2128 *****************************************************************************/
2130 static int mos7840_get_lsr_info(struct tty_struct *tty,
2131 unsigned int __user *value)
2134 unsigned int result = 0;
2136 count = mos7840_chars_in_buffer(tty);
2138 result = TIOCSER_TEMT;
2140 if (copy_to_user(value, &result, sizeof(int)))
2145 /*****************************************************************************
2146 * mos7840_get_serial_info
2147 * function to get information about serial port
2148 *****************************************************************************/
2150 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2151 struct serial_struct __user *retinfo)
2153 struct serial_struct tmp;
2155 if (mos7840_port == NULL)
2161 memset(&tmp, 0, sizeof(tmp));
2163 tmp.type = PORT_16550A;
2164 tmp.line = mos7840_port->port->serial->minor;
2165 tmp.port = mos7840_port->port->number;
2167 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2168 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2169 tmp.baud_base = 9600;
2170 tmp.close_delay = 5 * HZ;
2171 tmp.closing_wait = 30 * HZ;
2173 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2178 static int mos7840_get_icount(struct tty_struct *tty,
2179 struct serial_icounter_struct *icount)
2181 struct usb_serial_port *port = tty->driver_data;
2182 struct moschip_port *mos7840_port;
2183 struct async_icount cnow;
2185 mos7840_port = mos7840_get_port_private(port);
2186 cnow = mos7840_port->icount;
2189 icount->cts = cnow.cts;
2190 icount->dsr = cnow.dsr;
2191 icount->rng = cnow.rng;
2192 icount->dcd = cnow.dcd;
2193 icount->rx = cnow.rx;
2194 icount->tx = cnow.tx;
2195 icount->frame = cnow.frame;
2196 icount->overrun = cnow.overrun;
2197 icount->parity = cnow.parity;
2198 icount->brk = cnow.brk;
2199 icount->buf_overrun = cnow.buf_overrun;
2201 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n", __func__,
2202 icount->rx, icount->tx);
2206 /*****************************************************************************
2208 * this function handles any ioctl calls to the driver
2209 *****************************************************************************/
2211 static int mos7840_ioctl(struct tty_struct *tty,
2212 unsigned int cmd, unsigned long arg)
2214 struct usb_serial_port *port = tty->driver_data;
2215 void __user *argp = (void __user *)arg;
2216 struct moschip_port *mos7840_port;
2218 struct async_icount cnow;
2219 struct async_icount cprev;
2221 if (mos7840_port_paranoia_check(port, __func__))
2224 mos7840_port = mos7840_get_port_private(port);
2226 if (mos7840_port == NULL)
2229 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
2232 /* return number of bytes available */
2235 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
2236 return mos7840_get_lsr_info(tty, argp);
2239 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
2240 return mos7840_get_serial_info(mos7840_port, argp);
2243 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
2247 dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
2248 cprev = mos7840_port->icount;
2250 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2251 mos7840_port->delta_msr_cond = 0;
2252 wait_event_interruptible(mos7840_port->delta_msr_wait,
2254 delta_msr_cond == 1));
2256 /* see if a signal did it */
2257 if (signal_pending(current))
2258 return -ERESTARTSYS;
2259 cnow = mos7840_port->icount;
2261 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2262 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2263 return -EIO; /* no change => error */
2264 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2265 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2266 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2267 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2278 return -ENOIOCTLCMD;
2281 static int mos7810_check(struct usb_serial *serial)
2283 int i, pass_count = 0;
2284 __u16 data = 0, mcr_data = 0;
2285 __u16 test_pattern = 0x55AA;
2287 /* Store MCR setting */
2288 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2289 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2290 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2292 for (i = 0; i < 16; i++) {
2293 /* Send the 1-bit test pattern out to MCS7810 test pin */
2294 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2295 MCS_WRREQ, MCS_WR_RTYPE,
2296 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2297 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2299 /* Read the test pattern back */
2300 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2301 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2302 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2304 /* If this is a MCS7810 device, both test patterns must match */
2305 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2311 /* Restore MCR setting */
2312 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2313 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2314 0, MOS_WDR_TIMEOUT);
2316 if (pass_count == 16)
2322 static int mos7840_calc_num_ports(struct usb_serial *serial)
2325 int mos7840_num_ports;
2327 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2328 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2329 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2331 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2332 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2333 device_type = serial->dev->descriptor.idProduct;
2335 /* For a MCS7840 device GPIO0 must be set to 1 */
2336 if ((data & 0x01) == 1)
2337 device_type = MOSCHIP_DEVICE_ID_7840;
2338 else if (mos7810_check(serial))
2339 device_type = MOSCHIP_DEVICE_ID_7810;
2341 device_type = MOSCHIP_DEVICE_ID_7820;
2344 mos7840_num_ports = (device_type >> 4) & 0x000F;
2345 serial->num_bulk_in = mos7840_num_ports;
2346 serial->num_bulk_out = mos7840_num_ports;
2347 serial->num_ports = mos7840_num_ports;
2349 return mos7840_num_ports;
2352 /****************************************************************************
2354 ****************************************************************************/
2356 static int mos7840_startup(struct usb_serial *serial)
2358 struct moschip_port *mos7840_port;
2359 struct usb_device *dev;
2365 /* we set up the pointers to the endpoints in the mos7840_open *
2366 * function, as the structures aren't created yet. */
2368 /* set up port private structures */
2369 for (i = 0; i < serial->num_ports; ++i) {
2370 dev_dbg(&dev->dev, "mos7840_startup: configuring port %d............\n", i);
2371 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2372 if (mos7840_port == NULL) {
2373 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2375 i--; /* don't follow NULL pointer cleaning up */
2379 /* Initialize all port interrupt end point to port 0 int
2380 * endpoint. Our device has only one interrupt end point
2381 * common to all port */
2383 mos7840_port->port = serial->port[i];
2384 mos7840_set_port_private(serial->port[i], mos7840_port);
2385 spin_lock_init(&mos7840_port->pool_lock);
2387 /* minor is not initialised until later by
2388 * usb-serial.c:get_free_serial() and cannot therefore be used
2389 * to index device instances */
2390 mos7840_port->port_num = i + 1;
2391 dev_dbg(&dev->dev, "serial->port[i]->number = %d\n", serial->port[i]->number);
2392 dev_dbg(&dev->dev, "serial->port[i]->serial->minor = %d\n", serial->port[i]->serial->minor);
2393 dev_dbg(&dev->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2394 dev_dbg(&dev->dev, "serial->minor = %d\n", serial->minor);
2396 if (mos7840_port->port_num == 1) {
2397 mos7840_port->SpRegOffset = 0x0;
2398 mos7840_port->ControlRegOffset = 0x1;
2399 mos7840_port->DcrRegOffset = 0x4;
2400 } else if ((mos7840_port->port_num == 2)
2401 && (serial->num_ports == 4)) {
2402 mos7840_port->SpRegOffset = 0x8;
2403 mos7840_port->ControlRegOffset = 0x9;
2404 mos7840_port->DcrRegOffset = 0x16;
2405 } else if ((mos7840_port->port_num == 2)
2406 && (serial->num_ports == 2)) {
2407 mos7840_port->SpRegOffset = 0xa;
2408 mos7840_port->ControlRegOffset = 0xb;
2409 mos7840_port->DcrRegOffset = 0x19;
2410 } else if ((mos7840_port->port_num == 3)
2411 && (serial->num_ports == 4)) {
2412 mos7840_port->SpRegOffset = 0xa;
2413 mos7840_port->ControlRegOffset = 0xb;
2414 mos7840_port->DcrRegOffset = 0x19;
2415 } else if ((mos7840_port->port_num == 4)
2416 && (serial->num_ports == 4)) {
2417 mos7840_port->SpRegOffset = 0xc;
2418 mos7840_port->ControlRegOffset = 0xd;
2419 mos7840_port->DcrRegOffset = 0x1c;
2421 mos7840_dump_serial_port(serial->port[i], mos7840_port);
2422 mos7840_set_port_private(serial->port[i], mos7840_port);
2424 /* enable rx_disable bit in control register */
2425 status = mos7840_get_reg_sync(serial->port[i],
2426 mos7840_port->ControlRegOffset, &Data);
2428 dev_dbg(&dev->dev, "Reading ControlReg failed status-0x%x\n", status);
2431 dev_dbg(&dev->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2432 Data |= 0x08; /* setting driver done bit */
2433 Data |= 0x04; /* sp1_bit to have cts change reflect in
2436 /* Data |= 0x20; //rx_disable bit */
2437 status = mos7840_set_reg_sync(serial->port[i],
2438 mos7840_port->ControlRegOffset, Data);
2440 dev_dbg(&dev->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2443 dev_dbg(&dev->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2445 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2448 status = mos7840_set_reg_sync(serial->port[i],
2449 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2451 dev_dbg(&dev->dev, "Writing DCR0 failed status-0x%x\n", status);
2454 dev_dbg(&dev->dev, "DCR0 Writing success status%d\n", status);
2457 status = mos7840_set_reg_sync(serial->port[i],
2458 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2460 dev_dbg(&dev->dev, "Writing DCR1 failed status-0x%x\n", status);
2463 dev_dbg(&dev->dev, "DCR1 Writing success status%d\n", status);
2466 status = mos7840_set_reg_sync(serial->port[i],
2467 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2469 dev_dbg(&dev->dev, "Writing DCR2 failed status-0x%x\n", status);
2472 dev_dbg(&dev->dev, "DCR2 Writing success status%d\n", status);
2474 /* write values in clkstart0x0 and clkmulti 0x20 */
2476 status = mos7840_set_reg_sync(serial->port[i],
2477 CLK_START_VALUE_REGISTER, Data);
2479 dev_dbg(&dev->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2482 dev_dbg(&dev->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2485 status = mos7840_set_reg_sync(serial->port[i],
2486 CLK_MULTI_REGISTER, Data);
2488 dev_dbg(&dev->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2491 dev_dbg(&dev->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2493 /* write value 0x0 to scratchpad register */
2495 status = mos7840_set_uart_reg(serial->port[i],
2496 SCRATCH_PAD_REGISTER, Data);
2498 dev_dbg(&dev->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2501 dev_dbg(&dev->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2503 /* Zero Length flag register */
2504 if ((mos7840_port->port_num != 1)
2505 && (serial->num_ports == 2)) {
2508 status = mos7840_set_reg_sync(serial->port[i],
2510 ((__u16)mos7840_port->port_num)), Data);
2511 dev_dbg(&dev->dev, "ZLIP offset %x\n",
2512 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2514 dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 2, status);
2517 dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 2, status);
2520 status = mos7840_set_reg_sync(serial->port[i],
2522 ((__u16)mos7840_port->port_num) - 0x1), Data);
2523 dev_dbg(&dev->dev, "ZLIP offset %x\n",
2524 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2526 dev_dbg(&dev->dev, "Writing ZLP_REG%d failed status-0x%x\n", i + 1, status);
2529 dev_dbg(&dev->dev, "ZLP_REG%d Writing success status%d\n", i + 1, status);
2532 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2533 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2534 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2536 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2537 !mos7840_port->dr) {
2542 mos7840_port->has_led = false;
2544 /* Initialize LED timers */
2545 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2546 mos7840_port->has_led = true;
2548 init_timer(&mos7840_port->led_timer1);
2549 mos7840_port->led_timer1.function = mos7840_led_off;
2550 mos7840_port->led_timer1.expires =
2551 jiffies + msecs_to_jiffies(LED_ON_MS);
2552 mos7840_port->led_timer1.data =
2553 (unsigned long)mos7840_port;
2555 init_timer(&mos7840_port->led_timer2);
2556 mos7840_port->led_timer2.function =
2557 mos7840_led_flag_off;
2558 mos7840_port->led_timer2.expires =
2559 jiffies + msecs_to_jiffies(LED_OFF_MS);
2560 mos7840_port->led_timer2.data =
2561 (unsigned long)mos7840_port;
2563 mos7840_port->led_flag = false;
2566 mos7840_set_led_sync(serial->port[i],
2567 MODEM_CONTROL_REGISTER, 0x0300);
2571 /* Zero Length flag enable */
2573 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2575 dev_dbg(&dev->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2578 dev_dbg(&dev->dev, "ZLP_REG5 Writing success status%d\n", status);
2580 /* setting configuration feature to one */
2581 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2582 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, MOS_WDR_TIMEOUT);
2585 for (/* nothing */; i >= 0; i--) {
2586 mos7840_port = mos7840_get_port_private(serial->port[i]);
2588 kfree(mos7840_port->dr);
2589 kfree(mos7840_port->ctrl_buf);
2590 usb_free_urb(mos7840_port->control_urb);
2591 kfree(mos7840_port);
2592 serial->port[i] = NULL;
2597 /****************************************************************************
2598 * mos7840_disconnect
2599 * This function is called whenever the device is removed from the usb bus.
2600 ****************************************************************************/
2602 static void mos7840_disconnect(struct usb_serial *serial)
2605 unsigned long flags;
2606 struct moschip_port *mos7840_port;
2608 /* check for the ports to be closed,close the ports and disconnect */
2610 /* free private structure allocated for serial port *
2611 * stop reads and writes on all ports */
2613 for (i = 0; i < serial->num_ports; ++i) {
2614 mos7840_port = mos7840_get_port_private(serial->port[i]);
2616 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2617 mos7840_port->zombie = 1;
2618 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2619 usb_kill_urb(mos7840_port->control_urb);
2624 /****************************************************************************
2626 * This function is called when the usb_serial structure is freed.
2627 ****************************************************************************/
2629 static void mos7840_release(struct usb_serial *serial)
2632 struct moschip_port *mos7840_port;
2634 /* check for the ports to be closed,close the ports and disconnect */
2636 /* free private structure allocated for serial port *
2637 * stop reads and writes on all ports */
2639 for (i = 0; i < serial->num_ports; ++i) {
2640 mos7840_port = mos7840_get_port_private(serial->port[i]);
2642 if (mos7840_port->has_led) {
2644 mos7840_set_led_sync(mos7840_port->port,
2645 MODEM_CONTROL_REGISTER, 0x0300);
2647 del_timer_sync(&mos7840_port->led_timer1);
2648 del_timer_sync(&mos7840_port->led_timer2);
2650 kfree(mos7840_port->ctrl_buf);
2651 kfree(mos7840_port->dr);
2652 kfree(mos7840_port);
2657 static struct usb_serial_driver moschip7840_4port_device = {
2659 .owner = THIS_MODULE,
2662 .description = DRIVER_DESC,
2663 .id_table = id_table,
2665 .open = mos7840_open,
2666 .close = mos7840_close,
2667 .write = mos7840_write,
2668 .write_room = mos7840_write_room,
2669 .chars_in_buffer = mos7840_chars_in_buffer,
2670 .throttle = mos7840_throttle,
2671 .unthrottle = mos7840_unthrottle,
2672 .calc_num_ports = mos7840_calc_num_ports,
2673 #ifdef MCSSerialProbe
2674 .probe = mos7840_serial_probe,
2676 .ioctl = mos7840_ioctl,
2677 .set_termios = mos7840_set_termios,
2678 .break_ctl = mos7840_break,
2679 .tiocmget = mos7840_tiocmget,
2680 .tiocmset = mos7840_tiocmset,
2681 .get_icount = mos7840_get_icount,
2682 .attach = mos7840_startup,
2683 .disconnect = mos7840_disconnect,
2684 .release = mos7840_release,
2685 .read_bulk_callback = mos7840_bulk_in_callback,
2686 .read_int_callback = mos7840_interrupt_callback,
2689 static struct usb_serial_driver * const serial_drivers[] = {
2690 &moschip7840_4port_device, NULL
2693 module_usb_serial_driver(serial_drivers, id_table);
2695 MODULE_DESCRIPTION(DRIVER_DESC);
2696 MODULE_LICENSE("GPL");
2698 module_param(debug, bool, S_IRUGO | S_IWUSR);
2699 MODULE_PARM_DESC(debug, "Debug enabled or not");