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 WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
86 #define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
88 #define MOS_PORT1 0x0200
89 #define MOS_PORT2 0x0300
90 #define MOS_VENREG 0x0000
91 #define MOS_MAX_PORT 0x02
92 #define MOS_WRITE 0x0E
96 #define MCS_RD_RTYPE 0xC0
97 #define MCS_WR_RTYPE 0x40
98 #define MCS_RDREQ 0x0D
99 #define MCS_WRREQ 0x0E
100 #define MCS_CTRL_TIMEOUT 500
101 #define VENDOR_READ_LENGTH (0x01)
103 #define MAX_NAME_LEN 64
105 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
106 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
108 /* For higher baud Rates use TIOCEXBAUD */
109 #define TIOCEXBAUD 0x5462
111 /* vendor id and device id defines */
113 /* The native mos7840/7820 component */
114 #define USB_VENDOR_ID_MOSCHIP 0x9710
115 #define MOSCHIP_DEVICE_ID_7840 0x7840
116 #define MOSCHIP_DEVICE_ID_7820 0x7820
117 #define MOSCHIP_DEVICE_ID_7810 0x7810
118 /* The native component can have its vendor/device id's overridden
119 * in vendor-specific implementations. Such devices can be handled
120 * by making a change here, in moschip_port_id_table, and in
121 * moschip_id_table_combined
123 #define USB_VENDOR_ID_BANDB 0x0856
124 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
125 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
126 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
127 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
128 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
129 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
130 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
131 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
132 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
133 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
134 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
135 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
136 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
138 /* This driver also supports
139 * ATEN UC2324 device using Moschip MCS7840
140 * ATEN UC2322 device using Moschip MCS7820
142 #define USB_VENDOR_ID_ATENINTL 0x0557
143 #define ATENINTL_DEVICE_ID_UC2324 0x2011
144 #define ATENINTL_DEVICE_ID_UC2322 0x7820
146 /* Interrupt Routine Defines */
148 #define SERIAL_IIR_RLS 0x06
149 #define SERIAL_IIR_MS 0x00
152 * Emulation of the bit mask on the LINE STATUS REGISTER.
154 #define SERIAL_LSR_DR 0x0001
155 #define SERIAL_LSR_OE 0x0002
156 #define SERIAL_LSR_PE 0x0004
157 #define SERIAL_LSR_FE 0x0008
158 #define SERIAL_LSR_BI 0x0010
160 #define MOS_MSR_DELTA_CTS 0x10
161 #define MOS_MSR_DELTA_DSR 0x20
162 #define MOS_MSR_DELTA_RI 0x40
163 #define MOS_MSR_DELTA_CD 0x80
165 /* Serial Port register Address */
166 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
167 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
168 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
169 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
170 #define LINE_STATUS_REGISTER ((__u16)(0x05))
171 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
172 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
173 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
174 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
176 #define CLK_MULTI_REGISTER ((__u16)(0x02))
177 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
178 #define GPIO_REGISTER ((__u16)(0x07))
180 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
183 * URB POOL related defines
185 #define NUM_URBS 16 /* URB Count */
186 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
188 /* LED on/off milliseconds*/
189 #define LED_ON_MS 500
190 #define LED_OFF_MS 500
192 static int device_type;
194 static const struct usb_device_id moschip_port_id_table[] = {
195 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
196 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
197 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
208 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
209 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
210 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
211 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
212 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
213 {} /* terminating entry */
216 static const struct usb_device_id moschip_id_table_combined[] __devinitconst = {
217 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
218 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
219 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
220 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
221 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
222 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
223 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
224 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
225 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
226 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
227 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
228 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
229 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
230 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
231 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
232 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
233 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
234 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
235 {} /* terminating entry */
238 MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
240 /* This structure holds all of the local port information */
242 struct moschip_port {
243 int port_num; /*Actual port number in the device(1,2,etc) */
244 struct urb *write_urb; /* write URB for this port */
245 struct urb *read_urb; /* read URB for this port */
247 __u8 shadowLCR; /* last LCR value received */
248 __u8 shadowMCR; /* last MCR value received */
252 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
253 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
255 struct async_icount icount;
256 struct usb_serial_port *port; /* loop back to the owner of this object */
260 __u8 ControlRegOffset;
262 /* for processing control URBS in interrupt context */
263 struct urb *control_urb;
264 struct usb_ctrlrequest *dr;
268 spinlock_t pool_lock;
269 struct urb *write_urb_pool[NUM_URBS];
273 /* For device(s) with LED indicator */
276 struct timer_list led_timer1; /* Timer for LED on */
277 struct timer_list led_timer2; /* Timer for LED off */
283 * mos7840_set_reg_sync
284 * To set the Control register by calling usb_fill_control_urb function
285 * by passing usb_sndctrlpipe function as parameter.
288 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
291 struct usb_device *dev = port->serial->dev;
293 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
295 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
296 MCS_WR_RTYPE, val, reg, NULL, 0,
301 * mos7840_get_reg_sync
302 * To set the Uart register by calling usb_fill_control_urb function by
303 * passing usb_rcvctrlpipe function as parameter.
306 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
309 struct usb_device *dev = port->serial->dev;
313 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
317 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
318 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
321 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
328 * mos7840_set_uart_reg
329 * To set the Uart register by calling usb_fill_control_urb function by
330 * passing usb_sndctrlpipe function as parameter.
333 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
337 struct usb_device *dev = port->serial->dev;
339 /* For the UART control registers, the application number need
341 if (port->serial->num_ports == 4) {
342 val |= (((__u16) port->number -
343 (__u16) (port->serial->minor)) + 1) << 8;
344 dbg("mos7840_set_uart_reg application number is %x", val);
346 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
347 val |= (((__u16) port->number -
348 (__u16) (port->serial->minor)) + 1) << 8;
349 dbg("mos7840_set_uart_reg application number is %x",
353 (((__u16) port->number -
354 (__u16) (port->serial->minor)) + 2) << 8;
355 dbg("mos7840_set_uart_reg application number is %x",
359 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
360 MCS_WR_RTYPE, val, reg, NULL, 0,
366 * mos7840_get_uart_reg
367 * To set the Control register by calling usb_fill_control_urb function
368 * by passing usb_rcvctrlpipe function as parameter.
370 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
373 struct usb_device *dev = port->serial->dev;
378 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
382 /* dbg("application number is %4x",
383 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
384 /* Wval is same as application number */
385 if (port->serial->num_ports == 4) {
387 (((__u16) port->number - (__u16) (port->serial->minor)) +
389 dbg("mos7840_get_uart_reg application number is %x", Wval);
391 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
392 Wval = (((__u16) port->number -
393 (__u16) (port->serial->minor)) + 1) << 8;
394 dbg("mos7840_get_uart_reg application number is %x",
397 Wval = (((__u16) port->number -
398 (__u16) (port->serial->minor)) + 2) << 8;
399 dbg("mos7840_get_uart_reg application number is %x",
403 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
404 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
412 static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
415 dbg("***************************************");
416 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
417 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
418 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
419 dbg("***************************************");
423 /************************************************************************/
424 /************************************************************************/
425 /* I N T E R F A C E F U N C T I O N S */
426 /* I N T E R F A C E F U N C T I O N S */
427 /************************************************************************/
428 /************************************************************************/
430 static inline void mos7840_set_port_private(struct usb_serial_port *port,
431 struct moschip_port *data)
433 usb_set_serial_port_data(port, (void *)data);
436 static inline struct moschip_port *mos7840_get_port_private(struct
440 return (struct moschip_port *)usb_get_serial_port_data(port);
443 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
445 struct moschip_port *mos7840_port;
446 struct async_icount *icount;
448 icount = &mos7840_port->icount;
450 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
452 icount = &mos7840_port->icount;
454 /* update input line counters */
455 if (new_msr & MOS_MSR_DELTA_CTS) {
459 if (new_msr & MOS_MSR_DELTA_DSR) {
463 if (new_msr & MOS_MSR_DELTA_CD) {
467 if (new_msr & MOS_MSR_DELTA_RI) {
474 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
476 struct async_icount *icount;
478 dbg("%s - %02x", __func__, new_lsr);
480 if (new_lsr & SERIAL_LSR_BI) {
482 * Parity and Framing errors only count if they
483 * occur exclusive of a break being
486 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
489 /* update input line counters */
490 icount = &port->icount;
491 if (new_lsr & SERIAL_LSR_BI) {
495 if (new_lsr & SERIAL_LSR_OE) {
499 if (new_lsr & SERIAL_LSR_PE) {
503 if (new_lsr & SERIAL_LSR_FE) {
509 /************************************************************************/
510 /************************************************************************/
511 /* U S B C A L L B A C K F U N C T I O N S */
512 /* U S B C A L L B A C K F U N C T I O N S */
513 /************************************************************************/
514 /************************************************************************/
516 static void mos7840_control_callback(struct urb *urb)
519 struct moschip_port *mos7840_port;
522 int status = urb->status;
524 mos7840_port = urb->context;
533 /* this urb is terminated, clean up */
534 dbg("%s - urb shutting down with status: %d", __func__,
538 dbg("%s - nonzero urb status received: %d", __func__,
543 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
544 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
545 mos7840_port->MsrLsr, mos7840_port->port_num);
546 data = urb->transfer_buffer;
547 regval = (__u8) data[0];
548 dbg("%s data is %x", __func__, regval);
549 if (mos7840_port->MsrLsr == 0)
550 mos7840_handle_new_msr(mos7840_port, regval);
551 else if (mos7840_port->MsrLsr == 1)
552 mos7840_handle_new_lsr(mos7840_port, regval);
555 spin_lock(&mos7840_port->pool_lock);
556 if (!mos7840_port->zombie)
557 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
558 spin_unlock(&mos7840_port->pool_lock);
560 dev_err(&urb->dev->dev,
561 "%s - Error %d submitting interrupt urb\n",
566 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
569 struct usb_device *dev = mcs->port->serial->dev;
570 struct usb_ctrlrequest *dr = mcs->dr;
571 unsigned char *buffer = mcs->ctrl_buf;
574 dr->bRequestType = MCS_RD_RTYPE;
575 dr->bRequest = MCS_RDREQ;
576 dr->wValue = cpu_to_le16(Wval); /* 0 */
577 dr->wIndex = cpu_to_le16(reg);
578 dr->wLength = cpu_to_le16(2);
580 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
581 (unsigned char *)dr, buffer, 2,
582 mos7840_control_callback, mcs);
583 mcs->control_urb->transfer_buffer_length = 2;
584 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
588 static void mos7840_set_led_callback(struct urb *urb)
590 switch (urb->status) {
597 /* This urb is terminated, clean up */
598 dbg("%s - urb shutting down with status: %d", __func__,
602 dbg("%s - nonzero urb status received: %d", __func__,
607 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
610 struct usb_device *dev = mcs->port->serial->dev;
611 struct usb_ctrlrequest *dr = mcs->dr;
613 dr->bRequestType = MCS_WR_RTYPE;
614 dr->bRequest = MCS_WRREQ;
615 dr->wValue = cpu_to_le16(wval);
616 dr->wIndex = cpu_to_le16(reg);
617 dr->wLength = cpu_to_le16(0);
619 usb_fill_control_urb(mcs->control_urb, dev, usb_sndctrlpipe(dev, 0),
620 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
622 usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
625 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
628 struct usb_device *dev = port->serial->dev;
630 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
631 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
634 static void mos7840_led_off(unsigned long arg)
636 struct moschip_port *mcs = (struct moschip_port *) arg;
639 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
640 mod_timer(&mcs->led_timer2,
641 jiffies + msecs_to_jiffies(LED_OFF_MS));
644 static void mos7840_led_flag_off(unsigned long arg)
646 struct moschip_port *mcs = (struct moschip_port *) arg;
648 mcs->led_flag = false;
651 /*****************************************************************************
652 * mos7840_interrupt_callback
653 * this is the callback function for when we have received data on the
654 * interrupt endpoint.
655 *****************************************************************************/
657 static void mos7840_interrupt_callback(struct urb *urb)
661 struct moschip_port *mos7840_port;
662 struct usb_serial *serial;
667 __u16 wval, wreg = 0;
668 int status = urb->status;
677 /* this urb is terminated, clean up */
678 dbg("%s - urb shutting down with status: %d", __func__,
682 dbg("%s - nonzero urb status received: %d", __func__,
687 length = urb->actual_length;
688 data = urb->transfer_buffer;
690 serial = urb->context;
692 /* Moschip get 5 bytes
693 * Byte 1 IIR Port 1 (port.number is 0)
694 * Byte 2 IIR Port 2 (port.number is 1)
695 * Byte 3 IIR Port 3 (port.number is 2)
696 * Byte 4 IIR Port 4 (port.number is 3)
697 * Byte 5 FIFO status for both */
699 if (length && length > 5) {
700 dbg("%s", "Wrong data !!!");
704 sp[0] = (__u8) data[0];
705 sp[1] = (__u8) data[1];
706 sp[2] = (__u8) data[2];
707 sp[3] = (__u8) data[3];
710 for (i = 0; i < serial->num_ports; i++) {
711 mos7840_port = mos7840_get_port_private(serial->port[i]);
713 (((__u16) serial->port[i]->number -
714 (__u16) (serial->minor)) + 1) << 8;
715 if (mos7840_port->open) {
717 dbg("SP%d No Interrupt !!!", i);
719 switch (sp[i] & 0x0f) {
721 dbg("Serial Port %d: Receiver status error or ", i);
722 dbg("address bit detected in 9-bit mode");
723 mos7840_port->MsrLsr = 1;
724 wreg = LINE_STATUS_REGISTER;
727 dbg("Serial Port %d: Modem status change", i);
728 mos7840_port->MsrLsr = 0;
729 wreg = MODEM_STATUS_REGISTER;
732 spin_lock(&mos7840_port->pool_lock);
733 if (!mos7840_port->zombie) {
734 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
736 spin_unlock(&mos7840_port->pool_lock);
739 spin_unlock(&mos7840_port->pool_lock);
744 /* the completion handler for the control urb will resubmit */
747 result = usb_submit_urb(urb, GFP_ATOMIC);
749 dev_err(&urb->dev->dev,
750 "%s - Error %d submitting interrupt urb\n",
755 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
756 const char *function)
759 dbg("%s - port == NULL", function);
763 dbg("%s - port->serial == NULL", function);
770 /* Inline functions to check the sanity of a pointer that is passed to us */
771 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
772 const char *function)
775 dbg("%s - serial == NULL", function);
779 dbg("%s - serial->type == NULL!", function);
786 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
787 const char *function)
789 /* if no port was specified, or it fails a paranoia check */
791 mos7840_port_paranoia_check(port, function) ||
792 mos7840_serial_paranoia_check(port->serial, function)) {
793 /* then say that we don't have a valid usb_serial thing,
794 * which will end up genrating -ENODEV return values */
801 /*****************************************************************************
802 * mos7840_bulk_in_callback
803 * this is the callback function for when we have received data on the
805 *****************************************************************************/
807 static void mos7840_bulk_in_callback(struct urb *urb)
811 struct usb_serial *serial;
812 struct usb_serial_port *port;
813 struct moschip_port *mos7840_port;
814 struct tty_struct *tty;
815 int status = urb->status;
817 mos7840_port = urb->context;
819 dbg("%s", "NULL mos7840_port pointer");
824 dbg("nonzero read bulk status received: %d", status);
825 mos7840_port->read_urb_busy = false;
829 port = (struct usb_serial_port *)mos7840_port->port;
830 if (mos7840_port_paranoia_check(port, __func__)) {
831 dbg("%s", "Port Paranoia failed");
832 mos7840_port->read_urb_busy = false;
836 serial = mos7840_get_usb_serial(port, __func__);
838 dbg("%s", "Bad serial pointer");
839 mos7840_port->read_urb_busy = false;
843 data = urb->transfer_buffer;
845 if (urb->actual_length) {
846 tty = tty_port_tty_get(&mos7840_port->port->port);
848 tty_insert_flip_string(tty, data, urb->actual_length);
850 tty_flip_buffer_push(tty);
853 mos7840_port->icount.rx += urb->actual_length;
855 dbg("mos7840_port->icount.rx is %d:",
856 mos7840_port->icount.rx);
859 if (!mos7840_port->read_urb) {
860 dbg("%s", "URB KILLED !!!");
861 mos7840_port->read_urb_busy = false;
866 if (mos7840_port->has_led && !mos7840_port->led_flag) {
867 mos7840_port->led_flag = true;
868 mos7840_set_led_async(mos7840_port, 0x0301,
869 MODEM_CONTROL_REGISTER);
870 mod_timer(&mos7840_port->led_timer1,
871 jiffies + msecs_to_jiffies(LED_ON_MS));
874 mos7840_port->read_urb_busy = true;
875 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
878 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
879 mos7840_port->read_urb_busy = false;
883 /*****************************************************************************
884 * mos7840_bulk_out_data_callback
885 * this is the callback function for when we have finished sending
886 * serial data on the bulk out endpoint.
887 *****************************************************************************/
889 static void mos7840_bulk_out_data_callback(struct urb *urb)
891 struct moschip_port *mos7840_port;
892 struct tty_struct *tty;
893 int status = urb->status;
896 mos7840_port = urb->context;
897 spin_lock(&mos7840_port->pool_lock);
898 for (i = 0; i < NUM_URBS; i++) {
899 if (urb == mos7840_port->write_urb_pool[i]) {
900 mos7840_port->busy[i] = 0;
904 spin_unlock(&mos7840_port->pool_lock);
907 dbg("nonzero write bulk status received:%d", status);
911 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
912 dbg("%s", "Port Paranoia failed");
916 tty = tty_port_tty_get(&mos7840_port->port->port);
917 if (tty && mos7840_port->open)
923 /************************************************************************/
924 /* 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 */
925 /************************************************************************/
926 #ifdef MCSSerialProbe
927 static int mos7840_serial_probe(struct usb_serial *serial,
928 const struct usb_device_id *id)
931 /*need to implement the mode_reg reading and updating\
932 structures usb_serial_ device_type\
933 (i.e num_ports, num_bulkin,bulkout etc) */
934 /* Also we can update the changes attach */
939 /*****************************************************************************
941 * this function is called by the tty driver when a port is opened
942 * If successful, we return 0
943 * Otherwise we return a negative error number.
944 *****************************************************************************/
946 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
950 struct usb_serial *serial;
954 struct moschip_port *mos7840_port;
955 struct moschip_port *port0;
957 if (mos7840_port_paranoia_check(port, __func__)) {
958 dbg("%s", "Port Paranoia failed");
962 serial = port->serial;
964 if (mos7840_serial_paranoia_check(serial, __func__)) {
965 dbg("%s", "Serial Paranoia failed");
969 mos7840_port = mos7840_get_port_private(port);
970 port0 = mos7840_get_port_private(serial->port[0]);
972 if (mos7840_port == NULL || port0 == NULL)
975 usb_clear_halt(serial->dev, port->write_urb->pipe);
976 usb_clear_halt(serial->dev, port->read_urb->pipe);
979 /* Initialising the write urb pool */
980 for (j = 0; j < NUM_URBS; ++j) {
981 urb = usb_alloc_urb(0, GFP_KERNEL);
982 mos7840_port->write_urb_pool[j] = urb;
985 dev_err(&port->dev, "No more urbs???\n");
989 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
991 if (!urb->transfer_buffer) {
993 mos7840_port->write_urb_pool[j] = NULL;
995 "%s-out of memory for urb buffers.\n",
1001 /*****************************************************************************
1002 * Initialize MCS7840 -- Write Init values to corresponding Registers
1010 * 0x08 : SP1/2 Control Reg
1011 *****************************************************************************/
1013 /* NEED to check the following Block */
1016 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1018 dbg("Reading Spreg failed");
1022 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1024 dbg("writing Spreg failed");
1029 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1031 dbg("writing Spreg failed");
1034 /* End of block to be checked */
1037 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1040 dbg("Reading Controlreg failed");
1043 Data |= 0x08; /* Driver done bit */
1044 Data |= 0x20; /* rx_disable */
1045 status = mos7840_set_reg_sync(port,
1046 mos7840_port->ControlRegOffset, Data);
1048 dbg("writing Controlreg failed");
1051 /* do register settings here */
1052 /* Set all regs to the device default values. */
1053 /***********************************
1054 * First Disable all interrupts.
1055 ***********************************/
1057 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1059 dbg("disabling interrupts failed");
1062 /* Set FIFO_CONTROL_REGISTER to the default value */
1064 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1066 dbg("Writing FIFO_CONTROL_REGISTER failed");
1071 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1073 dbg("Writing FIFO_CONTROL_REGISTER failed");
1078 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1079 mos7840_port->shadowLCR = Data;
1082 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1083 mos7840_port->shadowMCR = Data;
1086 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1087 mos7840_port->shadowLCR = Data;
1089 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1090 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1093 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1096 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1099 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1101 Data = Data & ~SERIAL_LCR_DLAB;
1102 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1103 mos7840_port->shadowLCR = Data;
1105 /* clearing Bulkin and Bulkout Fifo */
1107 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1110 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1112 Data = Data & ~0x0c;
1113 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1114 /* Finally enable all interrupts */
1116 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1118 /* clearing rx_disable */
1120 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1122 Data = Data & ~0x20;
1123 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1128 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1131 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1134 /* Check to see if we've set up our endpoint info yet *
1135 * (can't set it up in mos7840_startup as the structures *
1136 * were not set up at that time.) */
1137 if (port0->open_ports == 1) {
1138 if (serial->port[0]->interrupt_in_buffer == NULL) {
1139 /* set up interrupt urb */
1140 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1142 usb_rcvintpipe(serial->dev,
1143 serial->port[0]->interrupt_in_endpointAddress),
1144 serial->port[0]->interrupt_in_buffer,
1145 serial->port[0]->interrupt_in_urb->
1146 transfer_buffer_length,
1147 mos7840_interrupt_callback,
1149 serial->port[0]->interrupt_in_urb->interval);
1151 /* start interrupt read for mos7840 *
1152 * will continue as long as mos7840 is connected */
1155 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1158 dev_err(&port->dev, "%s - Error %d submitting "
1159 "interrupt urb\n", __func__, response);
1166 /* see if we've set up our endpoint info yet *
1167 * (can't set it up in mos7840_startup as the *
1168 * structures were not set up at that time.) */
1170 dbg("port number is %d", port->number);
1171 dbg("serial number is %d", port->serial->minor);
1172 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1173 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1174 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1175 dbg("port's number in the device is %d", mos7840_port->port_num);
1176 mos7840_port->read_urb = port->read_urb;
1178 /* set up our bulk in urb */
1179 if ((serial->num_ports == 2)
1180 && ((((__u16)port->number -
1181 (__u16)(port->serial->minor)) % 2) != 0)) {
1182 usb_fill_bulk_urb(mos7840_port->read_urb,
1184 usb_rcvbulkpipe(serial->dev,
1185 (port->bulk_in_endpointAddress) + 2),
1186 port->bulk_in_buffer,
1187 mos7840_port->read_urb->transfer_buffer_length,
1188 mos7840_bulk_in_callback, mos7840_port);
1190 usb_fill_bulk_urb(mos7840_port->read_urb,
1192 usb_rcvbulkpipe(serial->dev,
1193 port->bulk_in_endpointAddress),
1194 port->bulk_in_buffer,
1195 mos7840_port->read_urb->transfer_buffer_length,
1196 mos7840_bulk_in_callback, mos7840_port);
1199 dbg("mos7840_open: bulkin endpoint is %d",
1200 port->bulk_in_endpointAddress);
1201 mos7840_port->read_urb_busy = true;
1202 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1204 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1205 __func__, response);
1206 mos7840_port->read_urb_busy = false;
1209 /* initialize our wait queues */
1210 init_waitqueue_head(&mos7840_port->wait_chase);
1211 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1213 /* initialize our icount structure */
1214 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1216 /* initialize our port settings */
1217 /* Must set to enable ints! */
1218 mos7840_port->shadowMCR = MCR_MASTER_IE;
1219 /* send a open port command */
1220 mos7840_port->open = 1;
1221 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1222 mos7840_port->icount.tx = 0;
1223 mos7840_port->icount.rx = 0;
1225 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
1226 serial, mos7840_port, port);
1231 /*****************************************************************************
1232 * mos7840_chars_in_buffer
1233 * this function is called by the tty driver when it wants to know how many
1234 * bytes of data we currently have outstanding in the port (data that has
1235 * been written, but hasn't made it out the port yet)
1236 * If successful, we return the number of bytes left to be written in the
1238 * Otherwise we return zero.
1239 *****************************************************************************/
1241 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1243 struct usb_serial_port *port = tty->driver_data;
1246 unsigned long flags;
1247 struct moschip_port *mos7840_port;
1249 if (mos7840_port_paranoia_check(port, __func__)) {
1250 dbg("%s", "Invalid port");
1254 mos7840_port = mos7840_get_port_private(port);
1255 if (mos7840_port == NULL)
1258 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1259 for (i = 0; i < NUM_URBS; ++i)
1260 if (mos7840_port->busy[i])
1261 chars += URB_TRANSFER_BUFFER_SIZE;
1262 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1263 dbg("%s - returns %d", __func__, chars);
1268 /*****************************************************************************
1270 * this function is called by the tty driver when a port is closed
1271 *****************************************************************************/
1273 static void mos7840_close(struct usb_serial_port *port)
1275 struct usb_serial *serial;
1276 struct moschip_port *mos7840_port;
1277 struct moschip_port *port0;
1281 if (mos7840_port_paranoia_check(port, __func__)) {
1282 dbg("%s", "Port Paranoia failed");
1286 serial = mos7840_get_usb_serial(port, __func__);
1288 dbg("%s", "Serial Paranoia failed");
1292 mos7840_port = mos7840_get_port_private(port);
1293 port0 = mos7840_get_port_private(serial->port[0]);
1295 if (mos7840_port == NULL || port0 == NULL)
1298 for (j = 0; j < NUM_URBS; ++j)
1299 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1301 /* Freeing Write URBs */
1302 for (j = 0; j < NUM_URBS; ++j) {
1303 if (mos7840_port->write_urb_pool[j]) {
1304 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1305 kfree(mos7840_port->write_urb_pool[j]->
1308 usb_free_urb(mos7840_port->write_urb_pool[j]);
1312 /* While closing port, shutdown all bulk read, write *
1313 * and interrupt read if they exists */
1315 if (mos7840_port->write_urb) {
1316 dbg("%s", "Shutdown bulk write");
1317 usb_kill_urb(mos7840_port->write_urb);
1319 if (mos7840_port->read_urb) {
1320 dbg("%s", "Shutdown bulk read");
1321 usb_kill_urb(mos7840_port->read_urb);
1322 mos7840_port->read_urb_busy = false;
1324 if ((&mos7840_port->control_urb)) {
1325 dbg("%s", "Shutdown control read");
1326 /*/ usb_kill_urb (mos7840_port->control_urb); */
1329 /* if(mos7840_port->ctrl_buf != NULL) */
1330 /* kfree(mos7840_port->ctrl_buf); */
1331 port0->open_ports--;
1332 dbg("mos7840_num_open_ports in close%d:in port%d",
1333 port0->open_ports, port->number);
1334 if (port0->open_ports == 0) {
1335 if (serial->port[0]->interrupt_in_urb) {
1336 dbg("%s", "Shutdown interrupt_in_urb");
1337 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1341 if (mos7840_port->write_urb) {
1342 /* if this urb had a transfer buffer already (old tx) free it */
1343 if (mos7840_port->write_urb->transfer_buffer != NULL)
1344 kfree(mos7840_port->write_urb->transfer_buffer);
1345 usb_free_urb(mos7840_port->write_urb);
1349 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1352 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1354 mos7840_port->open = 0;
1357 /************************************************************************
1359 * mos7840_block_until_chase_response
1361 * This function will block the close until one of the following:
1362 * 1. Response to our Chase comes from mos7840
1363 * 2. A timeout of 10 seconds without activity has expired
1364 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1366 ************************************************************************/
1368 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1369 struct moschip_port *mos7840_port)
1371 int timeout = 1 * HZ;
1376 count = mos7840_chars_in_buffer(tty);
1378 /* Check for Buffer status */
1382 /* Block the thread for a while */
1383 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1385 /* No activity.. count down section */
1388 dbg("%s - TIMEOUT", __func__);
1391 /* Reset timeout value back to seconds */
1398 /*****************************************************************************
1400 * this function sends a break to the port
1401 *****************************************************************************/
1402 static void mos7840_break(struct tty_struct *tty, int break_state)
1404 struct usb_serial_port *port = tty->driver_data;
1406 struct usb_serial *serial;
1407 struct moschip_port *mos7840_port;
1409 if (mos7840_port_paranoia_check(port, __func__)) {
1410 dbg("%s", "Port Paranoia failed");
1414 serial = mos7840_get_usb_serial(port, __func__);
1416 dbg("%s", "Serial Paranoia failed");
1420 mos7840_port = mos7840_get_port_private(port);
1422 if (mos7840_port == NULL)
1426 /* flush and block until tx is empty */
1427 mos7840_block_until_chase_response(tty, mos7840_port);
1429 if (break_state == -1)
1430 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1432 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1434 /* FIXME: no locking on shadowLCR anywhere in driver */
1435 mos7840_port->shadowLCR = data;
1436 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
1437 mos7840_port->shadowLCR);
1438 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1439 mos7840_port->shadowLCR);
1442 /*****************************************************************************
1443 * mos7840_write_room
1444 * this function is called by the tty driver when it wants to know how many
1445 * bytes of data we can accept for a specific port.
1446 * If successful, we return the amount of room that we have for this port
1447 * Otherwise we return a negative error number.
1448 *****************************************************************************/
1450 static int mos7840_write_room(struct tty_struct *tty)
1452 struct usb_serial_port *port = tty->driver_data;
1455 unsigned long flags;
1456 struct moschip_port *mos7840_port;
1458 if (mos7840_port_paranoia_check(port, __func__)) {
1459 dbg("%s", "Invalid port");
1460 dbg("%s", " mos7840_write_room:leaving ...........");
1464 mos7840_port = mos7840_get_port_private(port);
1465 if (mos7840_port == NULL) {
1466 dbg("%s", "mos7840_break:leaving ...........");
1470 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1471 for (i = 0; i < NUM_URBS; ++i) {
1472 if (!mos7840_port->busy[i])
1473 room += URB_TRANSFER_BUFFER_SIZE;
1475 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1477 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1478 dbg("%s - returns %d", __func__, room);
1483 /*****************************************************************************
1485 * this function is called by the tty driver when data should be written to
1487 * If successful, we return the number of bytes written, otherwise we
1488 * return a negative error number.
1489 *****************************************************************************/
1491 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1492 const unsigned char *data, int count)
1498 unsigned long flags;
1500 struct moschip_port *mos7840_port;
1501 struct usb_serial *serial;
1504 const unsigned char *current_position = data;
1505 unsigned char *data1;
1509 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1510 mos7840_port->shadowLCR = Data;
1511 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1512 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1513 mos7840_port->shadowLCR);
1516 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1517 /* mos7840_port->shadowLCR=Data;//Need to add later */
1519 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1520 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1523 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1525 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1526 dbg("mos7840_write:DLL value is %x", Data);
1529 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1530 dbg("mos7840_write:DLM value is %x", Data);
1532 Data = Data & ~SERIAL_LCR_DLAB;
1533 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1534 mos7840_port->shadowLCR);
1535 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1538 if (mos7840_port_paranoia_check(port, __func__)) {
1539 dbg("%s", "Port Paranoia failed");
1543 serial = port->serial;
1544 if (mos7840_serial_paranoia_check(serial, __func__)) {
1545 dbg("%s", "Serial Paranoia failed");
1549 mos7840_port = mos7840_get_port_private(port);
1550 if (mos7840_port == NULL) {
1551 dbg("%s", "mos7840_port is NULL");
1555 /* try to find a free urb in the list */
1558 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1559 for (i = 0; i < NUM_URBS; ++i) {
1560 if (!mos7840_port->busy[i]) {
1561 mos7840_port->busy[i] = 1;
1562 urb = mos7840_port->write_urb_pool[i];
1567 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1570 dbg("%s - no more free urbs", __func__);
1574 if (urb->transfer_buffer == NULL) {
1575 urb->transfer_buffer =
1576 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1578 if (urb->transfer_buffer == NULL) {
1579 dev_err_console(port, "%s no more kernel memory...\n",
1584 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1586 memcpy(urb->transfer_buffer, current_position, transfer_size);
1588 /* fill urb with data and submit */
1589 if ((serial->num_ports == 2)
1590 && ((((__u16)port->number -
1591 (__u16)(port->serial->minor)) % 2) != 0)) {
1592 usb_fill_bulk_urb(urb,
1594 usb_sndbulkpipe(serial->dev,
1595 (port->bulk_out_endpointAddress) + 2),
1596 urb->transfer_buffer,
1598 mos7840_bulk_out_data_callback, mos7840_port);
1600 usb_fill_bulk_urb(urb,
1602 usb_sndbulkpipe(serial->dev,
1603 port->bulk_out_endpointAddress),
1604 urb->transfer_buffer,
1606 mos7840_bulk_out_data_callback, mos7840_port);
1609 data1 = urb->transfer_buffer;
1610 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
1613 if (mos7840_port->has_led && !mos7840_port->led_flag) {
1614 mos7840_port->led_flag = true;
1615 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0301);
1616 mod_timer(&mos7840_port->led_timer1,
1617 jiffies + msecs_to_jiffies(LED_ON_MS));
1620 /* send it down the pipe */
1621 status = usb_submit_urb(urb, GFP_ATOMIC);
1624 mos7840_port->busy[i] = 0;
1625 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1626 "with status = %d\n", __func__, status);
1627 bytes_sent = status;
1630 bytes_sent = transfer_size;
1631 mos7840_port->icount.tx += transfer_size;
1633 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
1639 /*****************************************************************************
1641 * this function is called by the tty driver when it wants to stop the data
1642 * being read from the port.
1643 *****************************************************************************/
1645 static void mos7840_throttle(struct tty_struct *tty)
1647 struct usb_serial_port *port = tty->driver_data;
1648 struct moschip_port *mos7840_port;
1651 if (mos7840_port_paranoia_check(port, __func__)) {
1652 dbg("%s", "Invalid port");
1656 dbg("- port %d", port->number);
1658 mos7840_port = mos7840_get_port_private(port);
1660 if (mos7840_port == NULL)
1663 if (!mos7840_port->open) {
1664 dbg("%s", "port not opened");
1668 /* if we are implementing XON/XOFF, send the stop character */
1670 unsigned char stop_char = STOP_CHAR(tty);
1671 status = mos7840_write(tty, port, &stop_char, 1);
1675 /* if we are implementing RTS/CTS, toggle that line */
1676 if (tty->termios->c_cflag & CRTSCTS) {
1677 mos7840_port->shadowMCR &= ~MCR_RTS;
1678 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1679 mos7840_port->shadowMCR);
1685 /*****************************************************************************
1686 * mos7840_unthrottle
1687 * this function is called by the tty driver when it wants to resume
1688 * the data being read from the port (called after mos7840_throttle is
1690 *****************************************************************************/
1691 static void mos7840_unthrottle(struct tty_struct *tty)
1693 struct usb_serial_port *port = tty->driver_data;
1695 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1697 if (mos7840_port_paranoia_check(port, __func__)) {
1698 dbg("%s", "Invalid port");
1702 if (mos7840_port == NULL)
1705 if (!mos7840_port->open) {
1706 dbg("%s - port not opened", __func__);
1710 /* if we are implementing XON/XOFF, send the start character */
1712 unsigned char start_char = START_CHAR(tty);
1713 status = mos7840_write(tty, port, &start_char, 1);
1718 /* if we are implementing RTS/CTS, toggle that line */
1719 if (tty->termios->c_cflag & CRTSCTS) {
1720 mos7840_port->shadowMCR |= MCR_RTS;
1721 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1722 mos7840_port->shadowMCR);
1728 static int mos7840_tiocmget(struct tty_struct *tty)
1730 struct usb_serial_port *port = tty->driver_data;
1731 struct moschip_port *mos7840_port;
1732 unsigned int result;
1736 mos7840_port = mos7840_get_port_private(port);
1738 if (mos7840_port == NULL)
1741 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1742 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1743 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1744 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1745 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1746 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1747 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1748 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1749 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1751 dbg("%s - 0x%04X", __func__, result);
1756 static int mos7840_tiocmset(struct tty_struct *tty,
1757 unsigned int set, unsigned int clear)
1759 struct usb_serial_port *port = tty->driver_data;
1760 struct moschip_port *mos7840_port;
1764 mos7840_port = mos7840_get_port_private(port);
1766 if (mos7840_port == NULL)
1769 /* FIXME: What locks the port registers ? */
1770 mcr = mos7840_port->shadowMCR;
1771 if (clear & TIOCM_RTS)
1773 if (clear & TIOCM_DTR)
1775 if (clear & TIOCM_LOOP)
1776 mcr &= ~MCR_LOOPBACK;
1778 if (set & TIOCM_RTS)
1780 if (set & TIOCM_DTR)
1782 if (set & TIOCM_LOOP)
1783 mcr |= MCR_LOOPBACK;
1785 mos7840_port->shadowMCR = mcr;
1787 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1789 dbg("setting MODEM_CONTROL_REGISTER Failed");
1796 /*****************************************************************************
1797 * mos7840_calc_baud_rate_divisor
1798 * this function calculates the proper baud rate divisor for the specified
1800 *****************************************************************************/
1801 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1804 dbg("%s - %d", __func__, baudRate);
1806 if (baudRate <= 115200) {
1807 *divisor = 115200 / baudRate;
1810 if ((baudRate > 115200) && (baudRate <= 230400)) {
1811 *divisor = 230400 / baudRate;
1812 *clk_sel_val = 0x10;
1813 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1814 *divisor = 403200 / baudRate;
1815 *clk_sel_val = 0x20;
1816 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1817 *divisor = 460800 / baudRate;
1818 *clk_sel_val = 0x30;
1819 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1820 *divisor = 806400 / baudRate;
1821 *clk_sel_val = 0x40;
1822 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1823 *divisor = 921600 / baudRate;
1824 *clk_sel_val = 0x50;
1825 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1826 *divisor = 1572864 / baudRate;
1827 *clk_sel_val = 0x60;
1828 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1829 *divisor = 3145728 / baudRate;
1830 *clk_sel_val = 0x70;
1836 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1837 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1838 *divisor = mos7840_divisor_table[i].Divisor;
1843 /* After trying for all the standard baud rates *
1844 * Try calculating the divisor for this baud rate */
1846 if (baudrate > 75 && baudrate < 230400) {
1847 /* get the divisor */
1848 custom = (__u16) (230400L / baudrate);
1850 /* Check for round off */
1851 round1 = (__u16) (2304000L / baudrate);
1852 round = (__u16) (round1 - (custom * 10));
1857 dbg(" Baud %d = %d", baudrate, custom);
1861 dbg("%s", " Baud calculation Failed...");
1866 /*****************************************************************************
1867 * mos7840_send_cmd_write_baud_rate
1868 * this function sends the proper command to change the baud rate of the
1870 *****************************************************************************/
1872 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1878 unsigned char number;
1880 struct usb_serial_port *port;
1882 if (mos7840_port == NULL)
1885 port = (struct usb_serial_port *)mos7840_port->port;
1886 if (mos7840_port_paranoia_check(port, __func__)) {
1887 dbg("%s", "Invalid port");
1891 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1892 dbg("%s", "Invalid Serial");
1896 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1898 dbg("%s - port = %d, baud = %d", __func__,
1899 mos7840_port->port->number, baudRate);
1900 /* reset clk_uart_sel in spregOffset */
1901 if (baudRate > 115200) {
1902 #ifdef HW_flow_control
1903 /* NOTE: need to see the pther register to modify */
1904 /* setting h/w flow control bit to 1 */
1906 mos7840_port->shadowMCR = Data;
1907 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1910 dbg("Writing spreg failed in set_serial_baud");
1916 #ifdef HW_flow_control
1917 /* setting h/w flow control bit to 0 */
1919 mos7840_port->shadowMCR = Data;
1920 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1923 dbg("Writing spreg failed in set_serial_baud");
1930 if (1) { /* baudRate <= 115200) */
1933 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1935 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1938 dbg("reading spreg failed in set_serial_baud");
1941 Data = (Data & 0x8f) | clk_sel_val;
1942 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1945 dbg("Writing spreg failed in set_serial_baud");
1948 /* Calculate the Divisor */
1951 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1954 /* Enable access to divisor latch */
1955 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1956 mos7840_port->shadowLCR = Data;
1957 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1959 /* Write the divisor */
1960 Data = (unsigned char)(divisor & 0xff);
1961 dbg("set_serial_baud Value to write DLL is %x", Data);
1962 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1964 Data = (unsigned char)((divisor & 0xff00) >> 8);
1965 dbg("set_serial_baud Value to write DLM is %x", Data);
1966 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1968 /* Disable access to divisor latch */
1969 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1970 mos7840_port->shadowLCR = Data;
1971 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1977 /*****************************************************************************
1978 * mos7840_change_port_settings
1979 * This routine is called to set the UART on the device to match
1980 * the specified new settings.
1981 *****************************************************************************/
1983 static void mos7840_change_port_settings(struct tty_struct *tty,
1984 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1994 struct usb_serial_port *port;
1995 struct usb_serial *serial;
1997 if (mos7840_port == NULL)
2000 port = (struct usb_serial_port *)mos7840_port->port;
2002 if (mos7840_port_paranoia_check(port, __func__)) {
2003 dbg("%s", "Invalid port");
2007 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
2008 dbg("%s", "Invalid Serial");
2012 serial = port->serial;
2014 dbg("%s - port %d", __func__, mos7840_port->port->number);
2016 if (!mos7840_port->open) {
2017 dbg("%s - port not opened", __func__);
2023 lParity = LCR_PAR_NONE;
2025 cflag = tty->termios->c_cflag;
2026 iflag = tty->termios->c_iflag;
2028 /* Change the number of bits */
2029 if (cflag & CSIZE) {
2030 switch (cflag & CSIZE) {
2048 /* Change the Parity bit */
2049 if (cflag & PARENB) {
2050 if (cflag & PARODD) {
2051 lParity = LCR_PAR_ODD;
2052 dbg("%s - parity = odd", __func__);
2054 lParity = LCR_PAR_EVEN;
2055 dbg("%s - parity = even", __func__);
2059 dbg("%s - parity = none", __func__);
2063 lParity = lParity | 0x20;
2065 /* Change the Stop bit */
2066 if (cflag & CSTOPB) {
2068 dbg("%s - stop bits = 2", __func__);
2071 dbg("%s - stop bits = 1", __func__);
2074 /* Update the LCR with the correct value */
2075 mos7840_port->shadowLCR &=
2076 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2077 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2079 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
2080 mos7840_port->shadowLCR);
2081 /* Disable Interrupts */
2083 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2086 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2089 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2091 /* Send the updated LCR value to the mos7840 */
2092 Data = mos7840_port->shadowLCR;
2094 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2097 mos7840_port->shadowMCR = Data;
2098 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2100 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2102 /* set up the MCR register and send it to the mos7840 */
2104 mos7840_port->shadowMCR = MCR_MASTER_IE;
2106 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2108 if (cflag & CRTSCTS)
2109 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2111 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2113 Data = mos7840_port->shadowMCR;
2114 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2116 /* Determine divisor based on baud rate */
2117 baud = tty_get_baud_rate(tty);
2120 /* pick a default, any default... */
2121 dbg("%s", "Picked default baud...");
2125 dbg("%s - baud rate = %d", __func__, baud);
2126 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2128 /* Enable Interrupts */
2130 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2132 if (mos7840_port->read_urb_busy == false) {
2133 mos7840_port->read_urb_busy = true;
2134 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2136 dbg("usb_submit_urb(read bulk) failed, status = %d",
2138 mos7840_port->read_urb_busy = false;
2141 wake_up(&mos7840_port->delta_msr_wait);
2142 mos7840_port->delta_msr_cond = 1;
2143 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
2144 mos7840_port->shadowLCR);
2147 /*****************************************************************************
2148 * mos7840_set_termios
2149 * this function is called by the tty driver when it wants to change
2150 * the termios structure
2151 *****************************************************************************/
2153 static void mos7840_set_termios(struct tty_struct *tty,
2154 struct usb_serial_port *port,
2155 struct ktermios *old_termios)
2159 struct usb_serial *serial;
2160 struct moschip_port *mos7840_port;
2162 if (mos7840_port_paranoia_check(port, __func__)) {
2163 dbg("%s", "Invalid port");
2167 serial = port->serial;
2169 if (mos7840_serial_paranoia_check(serial, __func__)) {
2170 dbg("%s", "Invalid Serial");
2174 mos7840_port = mos7840_get_port_private(port);
2176 if (mos7840_port == NULL)
2179 if (!mos7840_port->open) {
2180 dbg("%s - port not opened", __func__);
2184 dbg("%s", "setting termios - ");
2186 cflag = tty->termios->c_cflag;
2188 dbg("%s - clfag %08x iflag %08x", __func__,
2189 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2190 dbg("%s - old clfag %08x old iflag %08x", __func__,
2191 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2192 dbg("%s - port %d", __func__, port->number);
2194 /* change the port settings to the new ones specified */
2196 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2198 if (!mos7840_port->read_urb) {
2199 dbg("%s", "URB KILLED !!!!!");
2203 if (mos7840_port->read_urb_busy == false) {
2204 mos7840_port->read_urb_busy = true;
2205 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2207 dbg("usb_submit_urb(read bulk) failed, status = %d",
2209 mos7840_port->read_urb_busy = false;
2214 /*****************************************************************************
2215 * mos7840_get_lsr_info - get line status register info
2217 * Purpose: Let user call ioctl() to get info when the UART physically
2218 * is emptied. On bus types like RS485, the transmitter must
2219 * release the bus after transmitting. This must be done when
2220 * the transmit shift register is empty, not be done when the
2221 * transmit holding register is empty. This functionality
2222 * allows an RS485 driver to be written in user space.
2223 *****************************************************************************/
2225 static int mos7840_get_lsr_info(struct tty_struct *tty,
2226 unsigned int __user *value)
2229 unsigned int result = 0;
2231 count = mos7840_chars_in_buffer(tty);
2233 dbg("%s -- Empty", __func__);
2234 result = TIOCSER_TEMT;
2237 if (copy_to_user(value, &result, sizeof(int)))
2242 /*****************************************************************************
2243 * mos7840_get_serial_info
2244 * function to get information about serial port
2245 *****************************************************************************/
2247 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2248 struct serial_struct __user *retinfo)
2250 struct serial_struct tmp;
2252 if (mos7840_port == NULL)
2258 memset(&tmp, 0, sizeof(tmp));
2260 tmp.type = PORT_16550A;
2261 tmp.line = mos7840_port->port->serial->minor;
2262 tmp.port = mos7840_port->port->number;
2264 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2265 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2266 tmp.baud_base = 9600;
2267 tmp.close_delay = 5 * HZ;
2268 tmp.closing_wait = 30 * HZ;
2270 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2275 static int mos7840_get_icount(struct tty_struct *tty,
2276 struct serial_icounter_struct *icount)
2278 struct usb_serial_port *port = tty->driver_data;
2279 struct moschip_port *mos7840_port;
2280 struct async_icount cnow;
2282 mos7840_port = mos7840_get_port_private(port);
2283 cnow = mos7840_port->icount;
2286 icount->cts = cnow.cts;
2287 icount->dsr = cnow.dsr;
2288 icount->rng = cnow.rng;
2289 icount->dcd = cnow.dcd;
2290 icount->rx = cnow.rx;
2291 icount->tx = cnow.tx;
2292 icount->frame = cnow.frame;
2293 icount->overrun = cnow.overrun;
2294 icount->parity = cnow.parity;
2295 icount->brk = cnow.brk;
2296 icount->buf_overrun = cnow.buf_overrun;
2298 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2299 port->number, icount->rx, icount->tx);
2303 /*****************************************************************************
2305 * this function handles any ioctl calls to the driver
2306 *****************************************************************************/
2308 static int mos7840_ioctl(struct tty_struct *tty,
2309 unsigned int cmd, unsigned long arg)
2311 struct usb_serial_port *port = tty->driver_data;
2312 void __user *argp = (void __user *)arg;
2313 struct moschip_port *mos7840_port;
2315 struct async_icount cnow;
2316 struct async_icount cprev;
2318 if (mos7840_port_paranoia_check(port, __func__)) {
2319 dbg("%s", "Invalid port");
2323 mos7840_port = mos7840_get_port_private(port);
2325 if (mos7840_port == NULL)
2328 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2331 /* return number of bytes available */
2334 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2335 return mos7840_get_lsr_info(tty, argp);
2338 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
2339 return mos7840_get_serial_info(mos7840_port, argp);
2342 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
2346 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
2347 cprev = mos7840_port->icount;
2349 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2350 mos7840_port->delta_msr_cond = 0;
2351 wait_event_interruptible(mos7840_port->delta_msr_wait,
2353 delta_msr_cond == 1));
2355 /* see if a signal did it */
2356 if (signal_pending(current))
2357 return -ERESTARTSYS;
2358 cnow = mos7840_port->icount;
2360 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2361 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2362 return -EIO; /* no change => error */
2363 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2364 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2365 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2366 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2377 return -ENOIOCTLCMD;
2380 static int mos7810_check(struct usb_serial *serial)
2382 int i, pass_count = 0;
2383 __u16 data = 0, mcr_data = 0;
2384 __u16 test_pattern = 0x55AA;
2386 /* Store MCR setting */
2387 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2388 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2389 &mcr_data, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2391 for (i = 0; i < 16; i++) {
2392 /* Send the 1-bit test pattern out to MCS7810 test pin */
2393 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2394 MCS_WRREQ, MCS_WR_RTYPE,
2395 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2396 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2398 /* Read the test pattern back */
2399 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2400 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2401 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2403 /* If this is a MCS7810 device, both test patterns must match */
2404 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2410 /* Restore MCR setting */
2411 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2412 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2413 0, MOS_WDR_TIMEOUT);
2415 if (pass_count == 16)
2421 static int mos7840_calc_num_ports(struct usb_serial *serial)
2424 int mos7840_num_ports;
2426 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2427 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &data,
2428 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2430 if (serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7810 ||
2431 serial->dev->descriptor.idProduct == MOSCHIP_DEVICE_ID_7820) {
2432 device_type = serial->dev->descriptor.idProduct;
2434 /* For a MCS7840 device GPIO0 must be set to 1 */
2435 if ((data & 0x01) == 1)
2436 device_type = MOSCHIP_DEVICE_ID_7840;
2437 else if (mos7810_check(serial))
2438 device_type = MOSCHIP_DEVICE_ID_7810;
2440 device_type = MOSCHIP_DEVICE_ID_7820;
2443 mos7840_num_ports = (device_type >> 4) & 0x000F;
2444 serial->num_bulk_in = mos7840_num_ports;
2445 serial->num_bulk_out = mos7840_num_ports;
2446 serial->num_ports = mos7840_num_ports;
2448 return mos7840_num_ports;
2451 /****************************************************************************
2453 ****************************************************************************/
2455 static int mos7840_startup(struct usb_serial *serial)
2457 struct moschip_port *mos7840_port;
2458 struct usb_device *dev;
2463 dbg("%s", "Invalid Handler");
2469 /* we set up the pointers to the endpoints in the mos7840_open *
2470 * function, as the structures aren't created yet. */
2472 /* set up port private structures */
2473 for (i = 0; i < serial->num_ports; ++i) {
2474 dbg ("mos7840_startup: configuring port %d............", i);
2475 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2476 if (mos7840_port == NULL) {
2477 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2479 i--; /* don't follow NULL pointer cleaning up */
2483 /* Initialize all port interrupt end point to port 0 int
2484 * endpoint. Our device has only one interrupt end point
2485 * common to all port */
2487 mos7840_port->port = serial->port[i];
2488 mos7840_set_port_private(serial->port[i], mos7840_port);
2489 spin_lock_init(&mos7840_port->pool_lock);
2491 /* minor is not initialised until later by
2492 * usb-serial.c:get_free_serial() and cannot therefore be used
2493 * to index device instances */
2494 mos7840_port->port_num = i + 1;
2495 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2496 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2497 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2498 dbg ("serial->minor = %d", serial->minor);
2500 if (mos7840_port->port_num == 1) {
2501 mos7840_port->SpRegOffset = 0x0;
2502 mos7840_port->ControlRegOffset = 0x1;
2503 mos7840_port->DcrRegOffset = 0x4;
2504 } else if ((mos7840_port->port_num == 2)
2505 && (serial->num_ports == 4)) {
2506 mos7840_port->SpRegOffset = 0x8;
2507 mos7840_port->ControlRegOffset = 0x9;
2508 mos7840_port->DcrRegOffset = 0x16;
2509 } else if ((mos7840_port->port_num == 2)
2510 && (serial->num_ports == 2)) {
2511 mos7840_port->SpRegOffset = 0xa;
2512 mos7840_port->ControlRegOffset = 0xb;
2513 mos7840_port->DcrRegOffset = 0x19;
2514 } else if ((mos7840_port->port_num == 3)
2515 && (serial->num_ports == 4)) {
2516 mos7840_port->SpRegOffset = 0xa;
2517 mos7840_port->ControlRegOffset = 0xb;
2518 mos7840_port->DcrRegOffset = 0x19;
2519 } else if ((mos7840_port->port_num == 4)
2520 && (serial->num_ports == 4)) {
2521 mos7840_port->SpRegOffset = 0xc;
2522 mos7840_port->ControlRegOffset = 0xd;
2523 mos7840_port->DcrRegOffset = 0x1c;
2525 mos7840_dump_serial_port(mos7840_port);
2526 mos7840_set_port_private(serial->port[i], mos7840_port);
2528 /* enable rx_disable bit in control register */
2529 status = mos7840_get_reg_sync(serial->port[i],
2530 mos7840_port->ControlRegOffset, &Data);
2532 dbg("Reading ControlReg failed status-0x%x", status);
2535 dbg("ControlReg Reading success val is %x, status%d",
2537 Data |= 0x08; /* setting driver done bit */
2538 Data |= 0x04; /* sp1_bit to have cts change reflect in
2541 /* Data |= 0x20; //rx_disable bit */
2542 status = mos7840_set_reg_sync(serial->port[i],
2543 mos7840_port->ControlRegOffset, Data);
2545 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
2548 dbg("ControlReg Writing success(rx_disable) status%d",
2551 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2554 status = mos7840_set_reg_sync(serial->port[i],
2555 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2557 dbg("Writing DCR0 failed status-0x%x", status);
2560 dbg("DCR0 Writing success status%d", status);
2563 status = mos7840_set_reg_sync(serial->port[i],
2564 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2566 dbg("Writing DCR1 failed status-0x%x", status);
2569 dbg("DCR1 Writing success status%d", status);
2572 status = mos7840_set_reg_sync(serial->port[i],
2573 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2575 dbg("Writing DCR2 failed status-0x%x", status);
2578 dbg("DCR2 Writing success status%d", status);
2580 /* write values in clkstart0x0 and clkmulti 0x20 */
2582 status = mos7840_set_reg_sync(serial->port[i],
2583 CLK_START_VALUE_REGISTER, Data);
2585 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
2588 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
2591 status = mos7840_set_reg_sync(serial->port[i],
2592 CLK_MULTI_REGISTER, Data);
2594 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
2598 dbg("CLK_MULTI_REGISTER Writing success status%d",
2601 /* write value 0x0 to scratchpad register */
2603 status = mos7840_set_uart_reg(serial->port[i],
2604 SCRATCH_PAD_REGISTER, Data);
2606 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
2610 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
2613 /* Zero Length flag register */
2614 if ((mos7840_port->port_num != 1)
2615 && (serial->num_ports == 2)) {
2618 status = mos7840_set_reg_sync(serial->port[i],
2620 ((__u16)mos7840_port->port_num)), Data);
2621 dbg("ZLIP offset %x",
2623 ((__u16) mos7840_port->port_num)));
2625 dbg("Writing ZLP_REG%d failed status-0x%x",
2629 dbg("ZLP_REG%d Writing success status%d",
2633 status = mos7840_set_reg_sync(serial->port[i],
2635 ((__u16)mos7840_port->port_num) - 0x1), Data);
2636 dbg("ZLIP offset %x",
2638 ((__u16) mos7840_port->port_num) - 0x1));
2640 dbg("Writing ZLP_REG%d failed status-0x%x",
2644 dbg("ZLP_REG%d Writing success status%d",
2648 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2649 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2650 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2652 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2653 !mos7840_port->dr) {
2658 mos7840_port->has_led = false;
2660 /* Initialize LED timers */
2661 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2662 mos7840_port->has_led = true;
2664 init_timer(&mos7840_port->led_timer1);
2665 mos7840_port->led_timer1.function = mos7840_led_off;
2666 mos7840_port->led_timer1.expires =
2667 jiffies + msecs_to_jiffies(LED_ON_MS);
2668 mos7840_port->led_timer1.data =
2669 (unsigned long)mos7840_port;
2671 init_timer(&mos7840_port->led_timer2);
2672 mos7840_port->led_timer2.function =
2673 mos7840_led_flag_off;
2674 mos7840_port->led_timer2.expires =
2675 jiffies + msecs_to_jiffies(LED_OFF_MS);
2676 mos7840_port->led_timer2.data =
2677 (unsigned long)mos7840_port;
2679 mos7840_port->led_flag = false;
2682 mos7840_set_led_sync(serial->port[i],
2683 MODEM_CONTROL_REGISTER, 0x0300);
2686 dbg ("mos7840_startup: all ports configured...........");
2688 /* Zero Length flag enable */
2690 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2692 dbg("Writing ZLP_REG5 failed status-0x%x", status);
2695 dbg("ZLP_REG5 Writing success status%d", status);
2697 /* setting configuration feature to one */
2698 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2699 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2702 for (/* nothing */; i >= 0; i--) {
2703 mos7840_port = mos7840_get_port_private(serial->port[i]);
2705 kfree(mos7840_port->dr);
2706 kfree(mos7840_port->ctrl_buf);
2707 usb_free_urb(mos7840_port->control_urb);
2708 kfree(mos7840_port);
2709 serial->port[i] = NULL;
2714 /****************************************************************************
2715 * mos7840_disconnect
2716 * This function is called whenever the device is removed from the usb bus.
2717 ****************************************************************************/
2719 static void mos7840_disconnect(struct usb_serial *serial)
2722 unsigned long flags;
2723 struct moschip_port *mos7840_port;
2726 dbg("%s", "Invalid Handler");
2730 /* check for the ports to be closed,close the ports and disconnect */
2732 /* free private structure allocated for serial port *
2733 * stop reads and writes on all ports */
2735 for (i = 0; i < serial->num_ports; ++i) {
2736 mos7840_port = mos7840_get_port_private(serial->port[i]);
2737 dbg ("mos7840_port %d = %p", i, mos7840_port);
2739 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2740 mos7840_port->zombie = 1;
2741 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2742 usb_kill_urb(mos7840_port->control_urb);
2747 /****************************************************************************
2749 * This function is called when the usb_serial structure is freed.
2750 ****************************************************************************/
2752 static void mos7840_release(struct usb_serial *serial)
2755 struct moschip_port *mos7840_port;
2758 dbg("%s", "Invalid Handler");
2762 /* check for the ports to be closed,close the ports and disconnect */
2764 /* free private structure allocated for serial port *
2765 * stop reads and writes on all ports */
2767 for (i = 0; i < serial->num_ports; ++i) {
2768 mos7840_port = mos7840_get_port_private(serial->port[i]);
2769 dbg("mos7840_port %d = %p", i, mos7840_port);
2771 if (mos7840_port->has_led) {
2773 mos7840_set_led_sync(mos7840_port->port,
2774 MODEM_CONTROL_REGISTER, 0x0300);
2776 del_timer_sync(&mos7840_port->led_timer1);
2777 del_timer_sync(&mos7840_port->led_timer2);
2779 kfree(mos7840_port->ctrl_buf);
2780 kfree(mos7840_port->dr);
2781 kfree(mos7840_port);
2786 static struct usb_driver io_driver = {
2788 .disconnect = usb_serial_disconnect,
2789 .id_table = moschip_id_table_combined,
2792 static struct usb_serial_driver moschip7840_4port_device = {
2794 .owner = THIS_MODULE,
2797 .description = DRIVER_DESC,
2798 .id_table = moschip_port_id_table,
2800 .open = mos7840_open,
2801 .close = mos7840_close,
2802 .write = mos7840_write,
2803 .write_room = mos7840_write_room,
2804 .chars_in_buffer = mos7840_chars_in_buffer,
2805 .throttle = mos7840_throttle,
2806 .unthrottle = mos7840_unthrottle,
2807 .calc_num_ports = mos7840_calc_num_ports,
2808 #ifdef MCSSerialProbe
2809 .probe = mos7840_serial_probe,
2811 .ioctl = mos7840_ioctl,
2812 .set_termios = mos7840_set_termios,
2813 .break_ctl = mos7840_break,
2814 .tiocmget = mos7840_tiocmget,
2815 .tiocmset = mos7840_tiocmset,
2816 .get_icount = mos7840_get_icount,
2817 .attach = mos7840_startup,
2818 .disconnect = mos7840_disconnect,
2819 .release = mos7840_release,
2820 .read_bulk_callback = mos7840_bulk_in_callback,
2821 .read_int_callback = mos7840_interrupt_callback,
2824 static struct usb_serial_driver * const serial_drivers[] = {
2825 &moschip7840_4port_device, NULL
2828 module_usb_serial_driver(io_driver, serial_drivers);
2830 MODULE_DESCRIPTION(DRIVER_DESC);
2831 MODULE_LICENSE("GPL");
2833 module_param(debug, bool, S_IRUGO | S_IWUSR);
2834 MODULE_PARM_DESC(debug, "Debug enabled or not");