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>
38 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
41 * 16C50 UART register defines
44 #define LCR_BITS_5 0x00 /* 5 bits/char */
45 #define LCR_BITS_6 0x01 /* 6 bits/char */
46 #define LCR_BITS_7 0x02 /* 7 bits/char */
47 #define LCR_BITS_8 0x03 /* 8 bits/char */
48 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
50 #define LCR_STOP_1 0x00 /* 1 stop bit */
51 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
52 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
53 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
55 #define LCR_PAR_NONE 0x00 /* No parity */
56 #define LCR_PAR_ODD 0x08 /* Odd parity */
57 #define LCR_PAR_EVEN 0x18 /* Even parity */
58 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
59 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
60 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
62 #define LCR_SET_BREAK 0x40 /* Set Break condition */
63 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
65 #define MCR_DTR 0x01 /* Assert DTR */
66 #define MCR_RTS 0x02 /* Assert RTS */
67 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
68 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
69 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
70 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
72 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
73 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
74 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
75 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
78 * Defines used for sending commands to port
81 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
83 #define MOS_PORT1 0x0200
84 #define MOS_PORT2 0x0300
85 #define MOS_VENREG 0x0000
86 #define MOS_MAX_PORT 0x02
87 #define MOS_WRITE 0x0E
91 #define MCS_RD_RTYPE 0xC0
92 #define MCS_WR_RTYPE 0x40
93 #define MCS_RDREQ 0x0D
94 #define MCS_WRREQ 0x0E
95 #define MCS_CTRL_TIMEOUT 500
96 #define VENDOR_READ_LENGTH (0x01)
98 #define MAX_NAME_LEN 64
100 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
101 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
103 /* For higher baud Rates use TIOCEXBAUD */
104 #define TIOCEXBAUD 0x5462
106 /* vendor id and device id defines */
108 /* The native mos7840/7820 component */
109 #define USB_VENDOR_ID_MOSCHIP 0x9710
110 #define MOSCHIP_DEVICE_ID_7840 0x7840
111 #define MOSCHIP_DEVICE_ID_7820 0x7820
112 #define MOSCHIP_DEVICE_ID_7810 0x7810
113 /* The native component can have its vendor/device id's overridden
114 * in vendor-specific implementations. Such devices can be handled
115 * by making a change here, in id_table.
117 #define USB_VENDOR_ID_BANDB 0x0856
118 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
119 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
120 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
121 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
122 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
123 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
124 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
125 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
126 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
127 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
128 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
129 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
130 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
132 /* This driver also supports
133 * ATEN UC2324 device using Moschip MCS7840
134 * ATEN UC2322 device using Moschip MCS7820
136 #define USB_VENDOR_ID_ATENINTL 0x0557
137 #define ATENINTL_DEVICE_ID_UC2324 0x2011
138 #define ATENINTL_DEVICE_ID_UC2322 0x7820
140 /* Interrupt Routine Defines */
142 #define SERIAL_IIR_RLS 0x06
143 #define SERIAL_IIR_MS 0x00
146 * Emulation of the bit mask on the LINE STATUS REGISTER.
148 #define SERIAL_LSR_DR 0x0001
149 #define SERIAL_LSR_OE 0x0002
150 #define SERIAL_LSR_PE 0x0004
151 #define SERIAL_LSR_FE 0x0008
152 #define SERIAL_LSR_BI 0x0010
154 #define MOS_MSR_DELTA_CTS 0x10
155 #define MOS_MSR_DELTA_DSR 0x20
156 #define MOS_MSR_DELTA_RI 0x40
157 #define MOS_MSR_DELTA_CD 0x80
159 /* Serial Port register Address */
160 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
161 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
162 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
163 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
164 #define LINE_STATUS_REGISTER ((__u16)(0x05))
165 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
166 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
167 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
168 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
170 #define CLK_MULTI_REGISTER ((__u16)(0x02))
171 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
172 #define GPIO_REGISTER ((__u16)(0x07))
174 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
177 * URB POOL related defines
179 #define NUM_URBS 16 /* URB Count */
180 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
182 /* LED on/off milliseconds*/
183 #define LED_ON_MS 500
184 #define LED_OFF_MS 500
187 MOS7840_FLAG_CTRL_BUSY,
188 MOS7840_FLAG_LED_BUSY,
191 static const struct usb_device_id id_table[] = {
192 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
194 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
207 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
208 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
209 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
210 {} /* terminating entry */
212 MODULE_DEVICE_TABLE(usb, id_table);
214 /* This structure holds all of the local port information */
216 struct moschip_port {
217 int port_num; /*Actual port number in the device(1,2,etc) */
218 struct urb *write_urb; /* write URB for this port */
219 struct urb *read_urb; /* read URB for this port */
220 __u8 shadowLCR; /* last LCR value received */
221 __u8 shadowMCR; /* last MCR value received */
224 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
225 struct usb_serial_port *port; /* loop back to the owner of this object */
229 __u8 ControlRegOffset;
231 /* for processing control URBS in interrupt context */
232 struct urb *control_urb;
233 struct usb_ctrlrequest *dr;
237 spinlock_t pool_lock;
238 struct urb *write_urb_pool[NUM_URBS];
242 /* For device(s) with LED indicator */
244 struct timer_list led_timer1; /* Timer for LED on */
245 struct timer_list led_timer2; /* Timer for LED off */
247 struct usb_ctrlrequest *led_dr;
253 * mos7840_set_reg_sync
254 * To set the Control register by calling usb_fill_control_urb function
255 * by passing usb_sndctrlpipe function as parameter.
258 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
261 struct usb_device *dev = port->serial->dev;
263 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
265 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
266 MCS_WR_RTYPE, val, reg, NULL, 0,
271 * mos7840_get_reg_sync
272 * To set the Uart register by calling usb_fill_control_urb function by
273 * passing usb_rcvctrlpipe function as parameter.
276 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
279 struct usb_device *dev = port->serial->dev;
283 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
287 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
288 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
291 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
298 * mos7840_set_uart_reg
299 * To set the Uart register by calling usb_fill_control_urb function by
300 * passing usb_sndctrlpipe function as parameter.
303 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
307 struct usb_device *dev = port->serial->dev;
309 /* For the UART control registers, the application number need
311 if (port->serial->num_ports == 4) {
312 val |= (((__u16) port->number -
313 (__u16) (port->serial->minor)) + 1) << 8;
315 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
316 val |= (((__u16) port->number -
317 (__u16) (port->serial->minor)) + 1) << 8;
319 val |= (((__u16) port->number -
320 (__u16) (port->serial->minor)) + 2) << 8;
323 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
324 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
325 MCS_WR_RTYPE, val, reg, NULL, 0,
331 * mos7840_get_uart_reg
332 * To set the Control register by calling usb_fill_control_urb function
333 * by passing usb_rcvctrlpipe function as parameter.
335 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
338 struct usb_device *dev = port->serial->dev;
343 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
347 /* Wval is same as application number */
348 if (port->serial->num_ports == 4) {
350 (((__u16) port->number - (__u16) (port->serial->minor)) +
353 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
354 Wval = (((__u16) port->number -
355 (__u16) (port->serial->minor)) + 1) << 8;
357 Wval = (((__u16) port->number -
358 (__u16) (port->serial->minor)) + 2) << 8;
361 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
362 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
363 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
371 static void mos7840_dump_serial_port(struct usb_serial_port *port,
372 struct moschip_port *mos7840_port)
375 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
376 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
377 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
381 /************************************************************************/
382 /************************************************************************/
383 /* I N T E R F A C E F U N C T I O N S */
384 /* I N T E R F A C E F U N C T I O N S */
385 /************************************************************************/
386 /************************************************************************/
388 static inline void mos7840_set_port_private(struct usb_serial_port *port,
389 struct moschip_port *data)
391 usb_set_serial_port_data(port, (void *)data);
394 static inline struct moschip_port *mos7840_get_port_private(struct
398 return (struct moschip_port *)usb_get_serial_port_data(port);
401 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
403 struct moschip_port *mos7840_port;
404 struct async_icount *icount;
407 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
409 icount = &mos7840_port->port->icount;
411 /* update input line counters */
412 if (new_msr & MOS_MSR_DELTA_CTS)
414 if (new_msr & MOS_MSR_DELTA_DSR)
416 if (new_msr & MOS_MSR_DELTA_CD)
418 if (new_msr & MOS_MSR_DELTA_RI)
421 wake_up_interruptible(&port->port->port.delta_msr_wait);
425 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
427 struct async_icount *icount;
429 if (new_lsr & SERIAL_LSR_BI) {
431 * Parity and Framing errors only count if they
432 * occur exclusive of a break being
435 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
438 /* update input line counters */
439 icount = &port->port->icount;
440 if (new_lsr & SERIAL_LSR_BI)
442 if (new_lsr & SERIAL_LSR_OE)
444 if (new_lsr & SERIAL_LSR_PE)
446 if (new_lsr & SERIAL_LSR_FE)
450 /************************************************************************/
451 /************************************************************************/
452 /* U S B C A L L B A C K F U N C T I O N S */
453 /* U S B C A L L B A C K F U N C T I O N S */
454 /************************************************************************/
455 /************************************************************************/
457 static void mos7840_control_callback(struct urb *urb)
460 struct moschip_port *mos7840_port;
461 struct device *dev = &urb->dev->dev;
463 int status = urb->status;
465 mos7840_port = urb->context;
474 /* this urb is terminated, clean up */
475 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
478 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
482 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
483 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
484 mos7840_port->MsrLsr, mos7840_port->port_num);
485 data = urb->transfer_buffer;
486 regval = (__u8) data[0];
487 dev_dbg(dev, "%s data is %x\n", __func__, regval);
488 if (mos7840_port->MsrLsr == 0)
489 mos7840_handle_new_msr(mos7840_port, regval);
490 else if (mos7840_port->MsrLsr == 1)
491 mos7840_handle_new_lsr(mos7840_port, regval);
493 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
496 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
499 struct usb_device *dev = mcs->port->serial->dev;
500 struct usb_ctrlrequest *dr = mcs->dr;
501 unsigned char *buffer = mcs->ctrl_buf;
504 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
507 dr->bRequestType = MCS_RD_RTYPE;
508 dr->bRequest = MCS_RDREQ;
509 dr->wValue = cpu_to_le16(Wval); /* 0 */
510 dr->wIndex = cpu_to_le16(reg);
511 dr->wLength = cpu_to_le16(2);
513 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
514 (unsigned char *)dr, buffer, 2,
515 mos7840_control_callback, mcs);
516 mcs->control_urb->transfer_buffer_length = 2;
517 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
519 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
524 static void mos7840_set_led_callback(struct urb *urb)
526 switch (urb->status) {
533 /* This urb is terminated, clean up */
534 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d",
535 __func__, urb->status);
538 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d",
539 __func__, urb->status);
543 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
546 struct usb_device *dev = mcs->port->serial->dev;
547 struct usb_ctrlrequest *dr = mcs->led_dr;
549 dr->bRequestType = MCS_WR_RTYPE;
550 dr->bRequest = MCS_WRREQ;
551 dr->wValue = cpu_to_le16(wval);
552 dr->wIndex = cpu_to_le16(reg);
553 dr->wLength = cpu_to_le16(0);
555 usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
556 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
558 usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
561 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
564 struct usb_device *dev = port->serial->dev;
566 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
567 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
570 static void mos7840_led_off(unsigned long arg)
572 struct moschip_port *mcs = (struct moschip_port *) arg;
575 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
576 mod_timer(&mcs->led_timer2,
577 jiffies + msecs_to_jiffies(LED_OFF_MS));
580 static void mos7840_led_flag_off(unsigned long arg)
582 struct moschip_port *mcs = (struct moschip_port *) arg;
584 clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
587 static void mos7840_led_activity(struct usb_serial_port *port)
589 struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
591 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
594 mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
595 mod_timer(&mos7840_port->led_timer1,
596 jiffies + msecs_to_jiffies(LED_ON_MS));
599 /*****************************************************************************
600 * mos7840_interrupt_callback
601 * this is the callback function for when we have received data on the
602 * interrupt endpoint.
603 *****************************************************************************/
605 static void mos7840_interrupt_callback(struct urb *urb)
609 struct moschip_port *mos7840_port;
610 struct usb_serial *serial;
615 __u16 wval, wreg = 0;
616 int status = urb->status;
625 /* this urb is terminated, clean up */
626 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
630 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
635 length = urb->actual_length;
636 data = urb->transfer_buffer;
638 serial = urb->context;
640 /* Moschip get 5 bytes
641 * Byte 1 IIR Port 1 (port.number is 0)
642 * Byte 2 IIR Port 2 (port.number is 1)
643 * Byte 3 IIR Port 3 (port.number is 2)
644 * Byte 4 IIR Port 4 (port.number is 3)
645 * Byte 5 FIFO status for both */
647 if (length && length > 5) {
648 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
652 sp[0] = (__u8) data[0];
653 sp[1] = (__u8) data[1];
654 sp[2] = (__u8) data[2];
655 sp[3] = (__u8) data[3];
658 for (i = 0; i < serial->num_ports; i++) {
659 mos7840_port = mos7840_get_port_private(serial->port[i]);
661 (((__u16) serial->port[i]->number -
662 (__u16) (serial->minor)) + 1) << 8;
663 if (mos7840_port->open) {
665 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
667 switch (sp[i] & 0x0f) {
669 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
670 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
671 mos7840_port->MsrLsr = 1;
672 wreg = LINE_STATUS_REGISTER;
675 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
676 mos7840_port->MsrLsr = 0;
677 wreg = MODEM_STATUS_REGISTER;
680 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
685 /* the completion handler for the control urb will resubmit */
688 result = usb_submit_urb(urb, GFP_ATOMIC);
690 dev_err(&urb->dev->dev,
691 "%s - Error %d submitting interrupt urb\n",
696 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
697 const char *function)
700 pr_debug("%s - port == NULL\n", function);
704 pr_debug("%s - port->serial == NULL\n", function);
711 /* Inline functions to check the sanity of a pointer that is passed to us */
712 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
713 const char *function)
716 pr_debug("%s - serial == NULL\n", function);
720 pr_debug("%s - serial->type == NULL!\n", function);
727 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
728 const char *function)
730 /* if no port was specified, or it fails a paranoia check */
732 mos7840_port_paranoia_check(port, function) ||
733 mos7840_serial_paranoia_check(port->serial, function)) {
734 /* then say that we don't have a valid usb_serial thing,
735 * which will end up genrating -ENODEV return values */
742 /*****************************************************************************
743 * mos7840_bulk_in_callback
744 * this is the callback function for when we have received data on the
746 *****************************************************************************/
748 static void mos7840_bulk_in_callback(struct urb *urb)
752 struct usb_serial *serial;
753 struct usb_serial_port *port;
754 struct moschip_port *mos7840_port;
755 int status = urb->status;
757 mos7840_port = urb->context;
762 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
763 mos7840_port->read_urb_busy = false;
767 port = mos7840_port->port;
768 if (mos7840_port_paranoia_check(port, __func__)) {
769 mos7840_port->read_urb_busy = false;
773 serial = mos7840_get_usb_serial(port, __func__);
775 mos7840_port->read_urb_busy = false;
779 data = urb->transfer_buffer;
780 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
782 if (urb->actual_length) {
783 struct tty_port *tport = &mos7840_port->port->port;
784 tty_insert_flip_string(tport, data, urb->actual_length);
785 tty_flip_buffer_push(tport);
786 port->icount.rx += urb->actual_length;
787 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
790 if (!mos7840_port->read_urb) {
791 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
792 mos7840_port->read_urb_busy = false;
796 if (mos7840_port->has_led)
797 mos7840_led_activity(port);
799 mos7840_port->read_urb_busy = true;
800 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
803 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
804 mos7840_port->read_urb_busy = false;
808 /*****************************************************************************
809 * mos7840_bulk_out_data_callback
810 * this is the callback function for when we have finished sending
811 * serial data on the bulk out endpoint.
812 *****************************************************************************/
814 static void mos7840_bulk_out_data_callback(struct urb *urb)
816 struct moschip_port *mos7840_port;
817 struct usb_serial_port *port;
818 int status = urb->status;
821 mos7840_port = urb->context;
822 port = mos7840_port->port;
823 spin_lock(&mos7840_port->pool_lock);
824 for (i = 0; i < NUM_URBS; i++) {
825 if (urb == mos7840_port->write_urb_pool[i]) {
826 mos7840_port->busy[i] = 0;
830 spin_unlock(&mos7840_port->pool_lock);
833 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
837 if (mos7840_port_paranoia_check(port, __func__))
840 if (mos7840_port->open)
841 tty_port_tty_wakeup(&port->port);
845 /************************************************************************/
846 /* 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 */
847 /************************************************************************/
849 /*****************************************************************************
851 * this function is called by the tty driver when a port is opened
852 * If successful, we return 0
853 * Otherwise we return a negative error number.
854 *****************************************************************************/
856 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
860 struct usb_serial *serial;
864 struct moschip_port *mos7840_port;
865 struct moschip_port *port0;
867 if (mos7840_port_paranoia_check(port, __func__))
870 serial = port->serial;
872 if (mos7840_serial_paranoia_check(serial, __func__))
875 mos7840_port = mos7840_get_port_private(port);
876 port0 = mos7840_get_port_private(serial->port[0]);
878 if (mos7840_port == NULL || port0 == NULL)
881 usb_clear_halt(serial->dev, port->write_urb->pipe);
882 usb_clear_halt(serial->dev, port->read_urb->pipe);
885 /* Initialising the write urb pool */
886 for (j = 0; j < NUM_URBS; ++j) {
887 urb = usb_alloc_urb(0, GFP_KERNEL);
888 mos7840_port->write_urb_pool[j] = urb;
891 dev_err(&port->dev, "No more urbs???\n");
895 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
897 if (!urb->transfer_buffer) {
899 mos7840_port->write_urb_pool[j] = NULL;
901 "%s-out of memory for urb buffers.\n",
907 /*****************************************************************************
908 * Initialize MCS7840 -- Write Init values to corresponding Registers
916 * 0x08 : SP1/2 Control Reg
917 *****************************************************************************/
919 /* NEED to check the following Block */
922 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
924 dev_dbg(&port->dev, "Reading Spreg failed\n");
928 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
930 dev_dbg(&port->dev, "writing Spreg failed\n");
935 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
937 dev_dbg(&port->dev, "writing Spreg failed\n");
940 /* End of block to be checked */
943 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
946 dev_dbg(&port->dev, "Reading Controlreg failed\n");
949 Data |= 0x08; /* Driver done bit */
950 Data |= 0x20; /* rx_disable */
951 status = mos7840_set_reg_sync(port,
952 mos7840_port->ControlRegOffset, Data);
954 dev_dbg(&port->dev, "writing Controlreg failed\n");
957 /* do register settings here */
958 /* Set all regs to the device default values. */
959 /***********************************
960 * First Disable all interrupts.
961 ***********************************/
963 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
965 dev_dbg(&port->dev, "disabling interrupts failed\n");
968 /* Set FIFO_CONTROL_REGISTER to the default value */
970 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
972 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
977 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
979 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
984 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
985 mos7840_port->shadowLCR = Data;
988 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
989 mos7840_port->shadowMCR = Data;
992 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
993 mos7840_port->shadowLCR = Data;
995 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
996 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
999 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1002 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1005 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1007 Data = Data & ~SERIAL_LCR_DLAB;
1008 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1009 mos7840_port->shadowLCR = Data;
1011 /* clearing Bulkin and Bulkout Fifo */
1013 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
1016 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1018 Data = Data & ~0x0c;
1019 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1020 /* Finally enable all interrupts */
1022 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1024 /* clearing rx_disable */
1026 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1028 Data = Data & ~0x20;
1029 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1034 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1037 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1040 /* Check to see if we've set up our endpoint info yet *
1041 * (can't set it up in mos7840_startup as the structures *
1042 * were not set up at that time.) */
1043 if (port0->open_ports == 1) {
1044 if (serial->port[0]->interrupt_in_buffer == NULL) {
1045 /* set up interrupt urb */
1046 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1048 usb_rcvintpipe(serial->dev,
1049 serial->port[0]->interrupt_in_endpointAddress),
1050 serial->port[0]->interrupt_in_buffer,
1051 serial->port[0]->interrupt_in_urb->
1052 transfer_buffer_length,
1053 mos7840_interrupt_callback,
1055 serial->port[0]->interrupt_in_urb->interval);
1057 /* start interrupt read for mos7840 *
1058 * will continue as long as mos7840 is connected */
1061 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1064 dev_err(&port->dev, "%s - Error %d submitting "
1065 "interrupt urb\n", __func__, response);
1072 /* see if we've set up our endpoint info yet *
1073 * (can't set it up in mos7840_startup as the *
1074 * structures were not set up at that time.) */
1076 dev_dbg(&port->dev, "port number is %d\n", port->number);
1077 dev_dbg(&port->dev, "serial number is %d\n", port->serial->minor);
1078 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1079 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1080 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1081 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1082 mos7840_port->read_urb = port->read_urb;
1084 /* set up our bulk in urb */
1085 if ((serial->num_ports == 2)
1086 && ((((__u16)port->number -
1087 (__u16)(port->serial->minor)) % 2) != 0)) {
1088 usb_fill_bulk_urb(mos7840_port->read_urb,
1090 usb_rcvbulkpipe(serial->dev,
1091 (port->bulk_in_endpointAddress) + 2),
1092 port->bulk_in_buffer,
1093 mos7840_port->read_urb->transfer_buffer_length,
1094 mos7840_bulk_in_callback, mos7840_port);
1096 usb_fill_bulk_urb(mos7840_port->read_urb,
1098 usb_rcvbulkpipe(serial->dev,
1099 port->bulk_in_endpointAddress),
1100 port->bulk_in_buffer,
1101 mos7840_port->read_urb->transfer_buffer_length,
1102 mos7840_bulk_in_callback, mos7840_port);
1105 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1106 mos7840_port->read_urb_busy = true;
1107 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1109 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1110 __func__, response);
1111 mos7840_port->read_urb_busy = false;
1114 /* initialize our wait queues */
1115 init_waitqueue_head(&mos7840_port->wait_chase);
1117 /* initialize our port settings */
1118 /* Must set to enable ints! */
1119 mos7840_port->shadowMCR = MCR_MASTER_IE;
1120 /* send a open port command */
1121 mos7840_port->open = 1;
1122 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1126 for (j = 0; j < NUM_URBS; ++j) {
1127 urb = mos7840_port->write_urb_pool[j];
1130 kfree(urb->transfer_buffer);
1136 /*****************************************************************************
1137 * mos7840_chars_in_buffer
1138 * this function is called by the tty driver when it wants to know how many
1139 * bytes of data we currently have outstanding in the port (data that has
1140 * been written, but hasn't made it out the port yet)
1141 * If successful, we return the number of bytes left to be written in the
1143 * Otherwise we return zero.
1144 *****************************************************************************/
1146 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1148 struct usb_serial_port *port = tty->driver_data;
1151 unsigned long flags;
1152 struct moschip_port *mos7840_port;
1154 if (mos7840_port_paranoia_check(port, __func__))
1157 mos7840_port = mos7840_get_port_private(port);
1158 if (mos7840_port == NULL)
1161 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1162 for (i = 0; i < NUM_URBS; ++i) {
1163 if (mos7840_port->busy[i]) {
1164 struct urb *urb = mos7840_port->write_urb_pool[i];
1165 chars += urb->transfer_buffer_length;
1168 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1169 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1174 /*****************************************************************************
1176 * this function is called by the tty driver when a port is closed
1177 *****************************************************************************/
1179 static void mos7840_close(struct usb_serial_port *port)
1181 struct usb_serial *serial;
1182 struct moschip_port *mos7840_port;
1183 struct moschip_port *port0;
1187 if (mos7840_port_paranoia_check(port, __func__))
1190 serial = mos7840_get_usb_serial(port, __func__);
1194 mos7840_port = mos7840_get_port_private(port);
1195 port0 = mos7840_get_port_private(serial->port[0]);
1197 if (mos7840_port == NULL || port0 == NULL)
1200 for (j = 0; j < NUM_URBS; ++j)
1201 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1203 /* Freeing Write URBs */
1204 for (j = 0; j < NUM_URBS; ++j) {
1205 if (mos7840_port->write_urb_pool[j]) {
1206 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1207 kfree(mos7840_port->write_urb_pool[j]->
1210 usb_free_urb(mos7840_port->write_urb_pool[j]);
1214 usb_kill_urb(mos7840_port->write_urb);
1215 usb_kill_urb(mos7840_port->read_urb);
1216 mos7840_port->read_urb_busy = false;
1218 port0->open_ports--;
1219 dev_dbg(&port->dev, "%s in close%d:in port%d\n", __func__, port0->open_ports, port->number);
1220 if (port0->open_ports == 0) {
1221 if (serial->port[0]->interrupt_in_urb) {
1222 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1223 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1227 if (mos7840_port->write_urb) {
1228 /* if this urb had a transfer buffer already (old tx) free it */
1229 kfree(mos7840_port->write_urb->transfer_buffer);
1230 usb_free_urb(mos7840_port->write_urb);
1234 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1237 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1239 mos7840_port->open = 0;
1242 /************************************************************************
1244 * mos7840_block_until_chase_response
1246 * This function will block the close until one of the following:
1247 * 1. Response to our Chase comes from mos7840
1248 * 2. A timeout of 10 seconds without activity has expired
1249 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1251 ************************************************************************/
1253 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1254 struct moschip_port *mos7840_port)
1256 int timeout = msecs_to_jiffies(1000);
1261 count = mos7840_chars_in_buffer(tty);
1263 /* Check for Buffer status */
1267 /* Block the thread for a while */
1268 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1270 /* No activity.. count down section */
1273 dev_dbg(&mos7840_port->port->dev, "%s - TIMEOUT\n", __func__);
1276 /* Reset timeout value back to seconds */
1283 /*****************************************************************************
1285 * this function sends a break to the port
1286 *****************************************************************************/
1287 static void mos7840_break(struct tty_struct *tty, int break_state)
1289 struct usb_serial_port *port = tty->driver_data;
1291 struct usb_serial *serial;
1292 struct moschip_port *mos7840_port;
1294 if (mos7840_port_paranoia_check(port, __func__))
1297 serial = mos7840_get_usb_serial(port, __func__);
1301 mos7840_port = mos7840_get_port_private(port);
1303 if (mos7840_port == NULL)
1306 /* flush and block until tx is empty */
1307 mos7840_block_until_chase_response(tty, mos7840_port);
1309 if (break_state == -1)
1310 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1312 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1314 /* FIXME: no locking on shadowLCR anywhere in driver */
1315 mos7840_port->shadowLCR = data;
1316 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1317 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1318 mos7840_port->shadowLCR);
1321 /*****************************************************************************
1322 * mos7840_write_room
1323 * this function is called by the tty driver when it wants to know how many
1324 * bytes of data we can accept for a specific port.
1325 * If successful, we return the amount of room that we have for this port
1326 * Otherwise we return a negative error number.
1327 *****************************************************************************/
1329 static int mos7840_write_room(struct tty_struct *tty)
1331 struct usb_serial_port *port = tty->driver_data;
1334 unsigned long flags;
1335 struct moschip_port *mos7840_port;
1337 if (mos7840_port_paranoia_check(port, __func__))
1340 mos7840_port = mos7840_get_port_private(port);
1341 if (mos7840_port == NULL)
1344 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1345 for (i = 0; i < NUM_URBS; ++i) {
1346 if (!mos7840_port->busy[i])
1347 room += URB_TRANSFER_BUFFER_SIZE;
1349 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1351 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1352 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1357 /*****************************************************************************
1359 * this function is called by the tty driver when data should be written to
1361 * If successful, we return the number of bytes written, otherwise we
1362 * return a negative error number.
1363 *****************************************************************************/
1365 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1366 const unsigned char *data, int count)
1372 unsigned long flags;
1374 struct moschip_port *mos7840_port;
1375 struct usb_serial *serial;
1378 const unsigned char *current_position = data;
1379 unsigned char *data1;
1383 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1384 mos7840_port->shadowLCR = Data;
1385 dev_dbg(&port->dev, "%s: LINE_CONTROL_REGISTER is %x\n", __func__, Data);
1386 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1389 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1390 /* mos7840_port->shadowLCR=Data;//Need to add later */
1392 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1393 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1396 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1398 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1399 dev_dbg(&port->dev, "%s: DLL value is %x\n", __func__, Data);
1402 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1403 dev_dbg(&port->dev, "%s: DLM value is %x\n", __func__, Data);
1405 Data = Data & ~SERIAL_LCR_DLAB;
1406 dev_dbg(&port->dev, "%s: mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1407 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1410 if (mos7840_port_paranoia_check(port, __func__))
1413 serial = port->serial;
1414 if (mos7840_serial_paranoia_check(serial, __func__))
1417 mos7840_port = mos7840_get_port_private(port);
1418 if (mos7840_port == NULL)
1421 /* try to find a free urb in the list */
1424 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1425 for (i = 0; i < NUM_URBS; ++i) {
1426 if (!mos7840_port->busy[i]) {
1427 mos7840_port->busy[i] = 1;
1428 urb = mos7840_port->write_urb_pool[i];
1429 dev_dbg(&port->dev, "URB:%d\n", i);
1433 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1436 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1440 if (urb->transfer_buffer == NULL) {
1441 urb->transfer_buffer =
1442 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1444 if (urb->transfer_buffer == NULL) {
1445 dev_err_console(port, "%s no more kernel memory...\n",
1450 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1452 memcpy(urb->transfer_buffer, current_position, transfer_size);
1454 /* fill urb with data and submit */
1455 if ((serial->num_ports == 2)
1456 && ((((__u16)port->number -
1457 (__u16)(port->serial->minor)) % 2) != 0)) {
1458 usb_fill_bulk_urb(urb,
1460 usb_sndbulkpipe(serial->dev,
1461 (port->bulk_out_endpointAddress) + 2),
1462 urb->transfer_buffer,
1464 mos7840_bulk_out_data_callback, mos7840_port);
1466 usb_fill_bulk_urb(urb,
1468 usb_sndbulkpipe(serial->dev,
1469 port->bulk_out_endpointAddress),
1470 urb->transfer_buffer,
1472 mos7840_bulk_out_data_callback, mos7840_port);
1475 data1 = urb->transfer_buffer;
1476 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1478 if (mos7840_port->has_led)
1479 mos7840_led_activity(port);
1481 /* send it down the pipe */
1482 status = usb_submit_urb(urb, GFP_ATOMIC);
1485 mos7840_port->busy[i] = 0;
1486 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1487 "with status = %d\n", __func__, status);
1488 bytes_sent = status;
1491 bytes_sent = transfer_size;
1492 port->icount.tx += transfer_size;
1493 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
1499 /*****************************************************************************
1501 * this function is called by the tty driver when it wants to stop the data
1502 * being read from the port.
1503 *****************************************************************************/
1505 static void mos7840_throttle(struct tty_struct *tty)
1507 struct usb_serial_port *port = tty->driver_data;
1508 struct moschip_port *mos7840_port;
1511 if (mos7840_port_paranoia_check(port, __func__))
1514 mos7840_port = mos7840_get_port_private(port);
1516 if (mos7840_port == NULL)
1519 if (!mos7840_port->open) {
1520 dev_dbg(&port->dev, "%s", "port not opened\n");
1524 /* if we are implementing XON/XOFF, send the stop character */
1526 unsigned char stop_char = STOP_CHAR(tty);
1527 status = mos7840_write(tty, port, &stop_char, 1);
1531 /* if we are implementing RTS/CTS, toggle that line */
1532 if (tty->termios.c_cflag & CRTSCTS) {
1533 mos7840_port->shadowMCR &= ~MCR_RTS;
1534 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1535 mos7840_port->shadowMCR);
1541 /*****************************************************************************
1542 * mos7840_unthrottle
1543 * this function is called by the tty driver when it wants to resume
1544 * the data being read from the port (called after mos7840_throttle is
1546 *****************************************************************************/
1547 static void mos7840_unthrottle(struct tty_struct *tty)
1549 struct usb_serial_port *port = tty->driver_data;
1551 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1553 if (mos7840_port_paranoia_check(port, __func__))
1556 if (mos7840_port == NULL)
1559 if (!mos7840_port->open) {
1560 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1564 /* if we are implementing XON/XOFF, send the start character */
1566 unsigned char start_char = START_CHAR(tty);
1567 status = mos7840_write(tty, port, &start_char, 1);
1572 /* if we are implementing RTS/CTS, toggle that line */
1573 if (tty->termios.c_cflag & CRTSCTS) {
1574 mos7840_port->shadowMCR |= MCR_RTS;
1575 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1576 mos7840_port->shadowMCR);
1582 static int mos7840_tiocmget(struct tty_struct *tty)
1584 struct usb_serial_port *port = tty->driver_data;
1585 struct moschip_port *mos7840_port;
1586 unsigned int result;
1590 mos7840_port = mos7840_get_port_private(port);
1592 if (mos7840_port == NULL)
1595 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1598 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1601 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1602 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1603 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1604 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1605 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1606 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1607 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1609 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1614 static int mos7840_tiocmset(struct tty_struct *tty,
1615 unsigned int set, unsigned int clear)
1617 struct usb_serial_port *port = tty->driver_data;
1618 struct moschip_port *mos7840_port;
1622 mos7840_port = mos7840_get_port_private(port);
1624 if (mos7840_port == NULL)
1627 /* FIXME: What locks the port registers ? */
1628 mcr = mos7840_port->shadowMCR;
1629 if (clear & TIOCM_RTS)
1631 if (clear & TIOCM_DTR)
1633 if (clear & TIOCM_LOOP)
1634 mcr &= ~MCR_LOOPBACK;
1636 if (set & TIOCM_RTS)
1638 if (set & TIOCM_DTR)
1640 if (set & TIOCM_LOOP)
1641 mcr |= MCR_LOOPBACK;
1643 mos7840_port->shadowMCR = mcr;
1645 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1647 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1654 /*****************************************************************************
1655 * mos7840_calc_baud_rate_divisor
1656 * this function calculates the proper baud rate divisor for the specified
1658 *****************************************************************************/
1659 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1660 int baudRate, int *divisor,
1663 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1665 if (baudRate <= 115200) {
1666 *divisor = 115200 / baudRate;
1669 if ((baudRate > 115200) && (baudRate <= 230400)) {
1670 *divisor = 230400 / baudRate;
1671 *clk_sel_val = 0x10;
1672 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1673 *divisor = 403200 / baudRate;
1674 *clk_sel_val = 0x20;
1675 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1676 *divisor = 460800 / baudRate;
1677 *clk_sel_val = 0x30;
1678 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1679 *divisor = 806400 / baudRate;
1680 *clk_sel_val = 0x40;
1681 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1682 *divisor = 921600 / baudRate;
1683 *clk_sel_val = 0x50;
1684 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1685 *divisor = 1572864 / baudRate;
1686 *clk_sel_val = 0x60;
1687 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1688 *divisor = 3145728 / baudRate;
1689 *clk_sel_val = 0x70;
1695 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1696 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1697 *divisor = mos7840_divisor_table[i].Divisor;
1702 /* After trying for all the standard baud rates *
1703 * Try calculating the divisor for this baud rate */
1705 if (baudrate > 75 && baudrate < 230400) {
1706 /* get the divisor */
1707 custom = (__u16) (230400L / baudrate);
1709 /* Check for round off */
1710 round1 = (__u16) (2304000L / baudrate);
1711 round = (__u16) (round1 - (custom * 10));
1716 dev_dbg(&port->dev, " Baud %d = %d\n", baudrate, custom);
1720 dev_dbg(&port->dev, "%s", " Baud calculation Failed...\n");
1725 /*****************************************************************************
1726 * mos7840_send_cmd_write_baud_rate
1727 * this function sends the proper command to change the baud rate of the
1729 *****************************************************************************/
1731 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1737 unsigned char number;
1739 struct usb_serial_port *port;
1741 if (mos7840_port == NULL)
1744 port = mos7840_port->port;
1745 if (mos7840_port_paranoia_check(port, __func__))
1748 if (mos7840_serial_paranoia_check(port->serial, __func__))
1751 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1753 dev_dbg(&port->dev, "%s - port = %d, baud = %d\n", __func__,
1754 mos7840_port->port->number, baudRate);
1755 /* reset clk_uart_sel in spregOffset */
1756 if (baudRate > 115200) {
1757 #ifdef HW_flow_control
1758 /* NOTE: need to see the pther register to modify */
1759 /* setting h/w flow control bit to 1 */
1761 mos7840_port->shadowMCR = Data;
1762 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1765 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1771 #ifdef HW_flow_control
1772 /* setting h/w flow control bit to 0 */
1774 mos7840_port->shadowMCR = Data;
1775 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1778 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1785 if (1) { /* baudRate <= 115200) */
1788 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1790 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1793 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1796 Data = (Data & 0x8f) | clk_sel_val;
1797 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1800 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1803 /* Calculate the Divisor */
1806 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1809 /* Enable access to divisor latch */
1810 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1811 mos7840_port->shadowLCR = Data;
1812 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1814 /* Write the divisor */
1815 Data = (unsigned char)(divisor & 0xff);
1816 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1817 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1819 Data = (unsigned char)((divisor & 0xff00) >> 8);
1820 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1821 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1823 /* Disable access to divisor latch */
1824 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1825 mos7840_port->shadowLCR = Data;
1826 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1832 /*****************************************************************************
1833 * mos7840_change_port_settings
1834 * This routine is called to set the UART on the device to match
1835 * the specified new settings.
1836 *****************************************************************************/
1838 static void mos7840_change_port_settings(struct tty_struct *tty,
1839 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1849 struct usb_serial_port *port;
1850 struct usb_serial *serial;
1852 if (mos7840_port == NULL)
1855 port = mos7840_port->port;
1857 if (mos7840_port_paranoia_check(port, __func__))
1860 if (mos7840_serial_paranoia_check(port->serial, __func__))
1863 serial = port->serial;
1865 if (!mos7840_port->open) {
1866 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1872 lParity = LCR_PAR_NONE;
1874 cflag = tty->termios.c_cflag;
1875 iflag = tty->termios.c_iflag;
1877 /* Change the number of bits */
1878 if (cflag & CSIZE) {
1879 switch (cflag & CSIZE) {
1897 /* Change the Parity bit */
1898 if (cflag & PARENB) {
1899 if (cflag & PARODD) {
1900 lParity = LCR_PAR_ODD;
1901 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1903 lParity = LCR_PAR_EVEN;
1904 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1908 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1912 lParity = lParity | 0x20;
1914 /* Change the Stop bit */
1915 if (cflag & CSTOPB) {
1917 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1920 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1923 /* Update the LCR with the correct value */
1924 mos7840_port->shadowLCR &=
1925 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1926 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1928 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1929 mos7840_port->shadowLCR);
1930 /* Disable Interrupts */
1932 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1935 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1938 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1940 /* Send the updated LCR value to the mos7840 */
1941 Data = mos7840_port->shadowLCR;
1943 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1946 mos7840_port->shadowMCR = Data;
1947 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1949 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1951 /* set up the MCR register and send it to the mos7840 */
1953 mos7840_port->shadowMCR = MCR_MASTER_IE;
1955 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
1957 if (cflag & CRTSCTS)
1958 mos7840_port->shadowMCR |= (MCR_XON_ANY);
1960 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
1962 Data = mos7840_port->shadowMCR;
1963 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1965 /* Determine divisor based on baud rate */
1966 baud = tty_get_baud_rate(tty);
1969 /* pick a default, any default... */
1970 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
1974 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1975 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1977 /* Enable Interrupts */
1979 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1981 if (mos7840_port->read_urb_busy == false) {
1982 mos7840_port->read_urb_busy = true;
1983 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
1985 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1987 mos7840_port->read_urb_busy = false;
1990 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1991 mos7840_port->shadowLCR);
1994 /*****************************************************************************
1995 * mos7840_set_termios
1996 * this function is called by the tty driver when it wants to change
1997 * the termios structure
1998 *****************************************************************************/
2000 static void mos7840_set_termios(struct tty_struct *tty,
2001 struct usb_serial_port *port,
2002 struct ktermios *old_termios)
2006 struct usb_serial *serial;
2007 struct moschip_port *mos7840_port;
2009 if (mos7840_port_paranoia_check(port, __func__))
2012 serial = port->serial;
2014 if (mos7840_serial_paranoia_check(serial, __func__))
2017 mos7840_port = mos7840_get_port_private(port);
2019 if (mos7840_port == NULL)
2022 if (!mos7840_port->open) {
2023 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
2027 dev_dbg(&port->dev, "%s", "setting termios - \n");
2029 cflag = tty->termios.c_cflag;
2031 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
2032 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
2033 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
2034 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2035 dev_dbg(&port->dev, "%s - port %d\n", __func__, port->number);
2037 /* change the port settings to the new ones specified */
2039 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2041 if (!mos7840_port->read_urb) {
2042 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
2046 if (mos7840_port->read_urb_busy == false) {
2047 mos7840_port->read_urb_busy = true;
2048 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2050 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
2052 mos7840_port->read_urb_busy = false;
2057 /*****************************************************************************
2058 * mos7840_get_lsr_info - get line status register info
2060 * Purpose: Let user call ioctl() to get info when the UART physically
2061 * is emptied. On bus types like RS485, the transmitter must
2062 * release the bus after transmitting. This must be done when
2063 * the transmit shift register is empty, not be done when the
2064 * transmit holding register is empty. This functionality
2065 * allows an RS485 driver to be written in user space.
2066 *****************************************************************************/
2068 static int mos7840_get_lsr_info(struct tty_struct *tty,
2069 unsigned int __user *value)
2072 unsigned int result = 0;
2074 count = mos7840_chars_in_buffer(tty);
2076 result = TIOCSER_TEMT;
2078 if (copy_to_user(value, &result, sizeof(int)))
2083 /*****************************************************************************
2084 * mos7840_get_serial_info
2085 * function to get information about serial port
2086 *****************************************************************************/
2088 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2089 struct serial_struct __user *retinfo)
2091 struct serial_struct tmp;
2093 if (mos7840_port == NULL)
2099 memset(&tmp, 0, sizeof(tmp));
2101 tmp.type = PORT_16550A;
2102 tmp.line = mos7840_port->port->serial->minor;
2103 tmp.port = mos7840_port->port->number;
2105 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2106 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2107 tmp.baud_base = 9600;
2108 tmp.close_delay = 5 * HZ;
2109 tmp.closing_wait = 30 * HZ;
2111 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2116 /*****************************************************************************
2118 * this function handles any ioctl calls to the driver
2119 *****************************************************************************/
2121 static int mos7840_ioctl(struct tty_struct *tty,
2122 unsigned int cmd, unsigned long arg)
2124 struct usb_serial_port *port = tty->driver_data;
2125 void __user *argp = (void __user *)arg;
2126 struct moschip_port *mos7840_port;
2128 if (mos7840_port_paranoia_check(port, __func__))
2131 mos7840_port = mos7840_get_port_private(port);
2133 if (mos7840_port == NULL)
2136 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
2139 /* return number of bytes available */
2142 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
2143 return mos7840_get_lsr_info(tty, argp);
2146 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
2147 return mos7840_get_serial_info(mos7840_port, argp);
2150 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
2155 return -ENOIOCTLCMD;
2158 static int mos7810_check(struct usb_serial *serial)
2160 int i, pass_count = 0;
2162 __u16 data = 0, mcr_data = 0;
2163 __u16 test_pattern = 0x55AA;
2166 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2168 return 0; /* failed to identify 7810 */
2170 /* Store MCR setting */
2171 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2172 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2173 buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2174 if (res == VENDOR_READ_LENGTH)
2177 for (i = 0; i < 16; i++) {
2178 /* Send the 1-bit test pattern out to MCS7810 test pin */
2179 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2180 MCS_WRREQ, MCS_WR_RTYPE,
2181 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2182 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2184 /* Read the test pattern back */
2185 res = usb_control_msg(serial->dev,
2186 usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
2187 MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2188 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2189 if (res == VENDOR_READ_LENGTH)
2192 /* If this is a MCS7810 device, both test patterns must match */
2193 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2199 /* Restore MCR setting */
2200 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2201 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2202 0, MOS_WDR_TIMEOUT);
2206 if (pass_count == 16)
2212 static int mos7840_probe(struct usb_serial *serial,
2213 const struct usb_device_id *id)
2215 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
2219 if (product == MOSCHIP_DEVICE_ID_7810 ||
2220 product == MOSCHIP_DEVICE_ID_7820) {
2221 device_type = product;
2225 buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2229 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2230 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2231 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2233 /* For a MCS7840 device GPIO0 must be set to 1 */
2235 device_type = MOSCHIP_DEVICE_ID_7840;
2236 else if (mos7810_check(serial))
2237 device_type = MOSCHIP_DEVICE_ID_7810;
2239 device_type = MOSCHIP_DEVICE_ID_7820;
2243 usb_set_serial_data(serial, (void *)(unsigned long)device_type);
2248 static int mos7840_calc_num_ports(struct usb_serial *serial)
2250 int device_type = (unsigned long)usb_get_serial_data(serial);
2251 int mos7840_num_ports;
2253 mos7840_num_ports = (device_type >> 4) & 0x000F;
2255 return mos7840_num_ports;
2258 static int mos7840_port_probe(struct usb_serial_port *port)
2260 struct usb_serial *serial = port->serial;
2261 int device_type = (unsigned long)usb_get_serial_data(serial);
2262 struct moschip_port *mos7840_port;
2267 /* we set up the pointers to the endpoints in the mos7840_open *
2268 * function, as the structures aren't created yet. */
2270 pnum = port->number - serial->minor;
2272 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2273 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2274 if (mos7840_port == NULL) {
2275 dev_err(&port->dev, "%s - Out of memory\n", __func__);
2279 /* Initialize all port interrupt end point to port 0 int
2280 * endpoint. Our device has only one interrupt end point
2281 * common to all port */
2283 mos7840_port->port = port;
2284 mos7840_set_port_private(port, mos7840_port);
2285 spin_lock_init(&mos7840_port->pool_lock);
2287 /* minor is not initialised until later by
2288 * usb-serial.c:get_free_serial() and cannot therefore be used
2289 * to index device instances */
2290 mos7840_port->port_num = pnum + 1;
2291 dev_dbg(&port->dev, "port->number = %d\n", port->number);
2292 dev_dbg(&port->dev, "port->serial->minor = %d\n", port->serial->minor);
2293 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2294 dev_dbg(&port->dev, "serial->minor = %d\n", serial->minor);
2296 if (mos7840_port->port_num == 1) {
2297 mos7840_port->SpRegOffset = 0x0;
2298 mos7840_port->ControlRegOffset = 0x1;
2299 mos7840_port->DcrRegOffset = 0x4;
2300 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2301 mos7840_port->SpRegOffset = 0x8;
2302 mos7840_port->ControlRegOffset = 0x9;
2303 mos7840_port->DcrRegOffset = 0x16;
2304 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2305 mos7840_port->SpRegOffset = 0xa;
2306 mos7840_port->ControlRegOffset = 0xb;
2307 mos7840_port->DcrRegOffset = 0x19;
2308 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2309 mos7840_port->SpRegOffset = 0xa;
2310 mos7840_port->ControlRegOffset = 0xb;
2311 mos7840_port->DcrRegOffset = 0x19;
2312 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2313 mos7840_port->SpRegOffset = 0xc;
2314 mos7840_port->ControlRegOffset = 0xd;
2315 mos7840_port->DcrRegOffset = 0x1c;
2317 mos7840_dump_serial_port(port, mos7840_port);
2318 mos7840_set_port_private(port, mos7840_port);
2320 /* enable rx_disable bit in control register */
2321 status = mos7840_get_reg_sync(port,
2322 mos7840_port->ControlRegOffset, &Data);
2324 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2327 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2328 Data |= 0x08; /* setting driver done bit */
2329 Data |= 0x04; /* sp1_bit to have cts change reflect in
2332 /* Data |= 0x20; //rx_disable bit */
2333 status = mos7840_set_reg_sync(port,
2334 mos7840_port->ControlRegOffset, Data);
2336 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2339 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2341 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2344 status = mos7840_set_reg_sync(port,
2345 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2347 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2350 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2353 status = mos7840_set_reg_sync(port,
2354 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2356 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2359 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2362 status = mos7840_set_reg_sync(port,
2363 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2365 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2368 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2370 /* write values in clkstart0x0 and clkmulti 0x20 */
2372 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2374 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2377 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2380 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2382 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2385 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2387 /* write value 0x0 to scratchpad register */
2389 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2391 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2394 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2396 /* Zero Length flag register */
2397 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2399 status = mos7840_set_reg_sync(port,
2401 ((__u16)mos7840_port->port_num)), Data);
2402 dev_dbg(&port->dev, "ZLIP offset %x\n",
2403 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2405 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2408 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2411 status = mos7840_set_reg_sync(port,
2413 ((__u16)mos7840_port->port_num) - 0x1), Data);
2414 dev_dbg(&port->dev, "ZLIP offset %x\n",
2415 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2417 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2420 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
2423 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2424 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2425 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2427 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2428 !mos7840_port->dr) {
2433 mos7840_port->has_led = false;
2435 /* Initialize LED timers */
2436 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2437 mos7840_port->has_led = true;
2439 mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
2440 mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
2442 if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
2447 init_timer(&mos7840_port->led_timer1);
2448 mos7840_port->led_timer1.function = mos7840_led_off;
2449 mos7840_port->led_timer1.expires =
2450 jiffies + msecs_to_jiffies(LED_ON_MS);
2451 mos7840_port->led_timer1.data = (unsigned long)mos7840_port;
2453 init_timer(&mos7840_port->led_timer2);
2454 mos7840_port->led_timer2.function = mos7840_led_flag_off;
2455 mos7840_port->led_timer2.expires =
2456 jiffies + msecs_to_jiffies(LED_OFF_MS);
2457 mos7840_port->led_timer2.data = (unsigned long)mos7840_port;
2460 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2463 if (pnum == serial->num_ports - 1) {
2464 /* Zero Length flag enable */
2466 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2468 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2471 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2473 /* setting configuration feature to one */
2474 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2475 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2480 kfree(mos7840_port->led_dr);
2481 usb_free_urb(mos7840_port->led_urb);
2482 kfree(mos7840_port->dr);
2483 kfree(mos7840_port->ctrl_buf);
2484 usb_free_urb(mos7840_port->control_urb);
2485 kfree(mos7840_port);
2490 static int mos7840_port_remove(struct usb_serial_port *port)
2492 struct moschip_port *mos7840_port;
2494 mos7840_port = mos7840_get_port_private(port);
2496 if (mos7840_port->has_led) {
2498 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2500 del_timer_sync(&mos7840_port->led_timer1);
2501 del_timer_sync(&mos7840_port->led_timer2);
2503 usb_kill_urb(mos7840_port->led_urb);
2504 usb_free_urb(mos7840_port->led_urb);
2505 kfree(mos7840_port->led_dr);
2507 usb_kill_urb(mos7840_port->control_urb);
2508 usb_free_urb(mos7840_port->control_urb);
2509 kfree(mos7840_port->ctrl_buf);
2510 kfree(mos7840_port->dr);
2511 kfree(mos7840_port);
2516 static struct usb_serial_driver moschip7840_4port_device = {
2518 .owner = THIS_MODULE,
2521 .description = DRIVER_DESC,
2522 .id_table = id_table,
2524 .open = mos7840_open,
2525 .close = mos7840_close,
2526 .write = mos7840_write,
2527 .write_room = mos7840_write_room,
2528 .chars_in_buffer = mos7840_chars_in_buffer,
2529 .throttle = mos7840_throttle,
2530 .unthrottle = mos7840_unthrottle,
2531 .calc_num_ports = mos7840_calc_num_ports,
2532 .probe = mos7840_probe,
2533 .ioctl = mos7840_ioctl,
2534 .set_termios = mos7840_set_termios,
2535 .break_ctl = mos7840_break,
2536 .tiocmget = mos7840_tiocmget,
2537 .tiocmset = mos7840_tiocmset,
2538 .tiocmiwait = usb_serial_generic_tiocmiwait,
2539 .get_icount = usb_serial_generic_get_icount,
2540 .port_probe = mos7840_port_probe,
2541 .port_remove = mos7840_port_remove,
2542 .read_bulk_callback = mos7840_bulk_in_callback,
2543 .read_int_callback = mos7840_interrupt_callback,
2546 static struct usb_serial_driver * const serial_drivers[] = {
2547 &moschip7840_4port_device, NULL
2550 module_usb_serial_driver(serial_drivers, id_table);
2552 MODULE_DESCRIPTION(DRIVER_DESC);
2553 MODULE_LICENSE("GPL");