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/slab.h>
28 #include <linux/tty.h>
29 #include <linux/tty_driver.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/serial.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include <linux/uaccess.h>
37 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
40 * 16C50 UART register defines
43 #define LCR_BITS_5 0x00 /* 5 bits/char */
44 #define LCR_BITS_6 0x01 /* 6 bits/char */
45 #define LCR_BITS_7 0x02 /* 7 bits/char */
46 #define LCR_BITS_8 0x03 /* 8 bits/char */
47 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
49 #define LCR_STOP_1 0x00 /* 1 stop bit */
50 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
51 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
52 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
54 #define LCR_PAR_NONE 0x00 /* No parity */
55 #define LCR_PAR_ODD 0x08 /* Odd parity */
56 #define LCR_PAR_EVEN 0x18 /* Even parity */
57 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
58 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
59 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
61 #define LCR_SET_BREAK 0x40 /* Set Break condition */
62 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
64 #define MCR_DTR 0x01 /* Assert DTR */
65 #define MCR_RTS 0x02 /* Assert RTS */
66 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
67 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
68 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
69 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
71 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
72 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
73 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
74 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
77 * Defines used for sending commands to port
80 #define MOS_WDR_TIMEOUT 5000 /* default urb timeout */
82 #define MOS_PORT1 0x0200
83 #define MOS_PORT2 0x0300
84 #define MOS_VENREG 0x0000
85 #define MOS_MAX_PORT 0x02
86 #define MOS_WRITE 0x0E
90 #define MCS_RD_RTYPE 0xC0
91 #define MCS_WR_RTYPE 0x40
92 #define MCS_RDREQ 0x0D
93 #define MCS_WRREQ 0x0E
94 #define MCS_CTRL_TIMEOUT 500
95 #define VENDOR_READ_LENGTH (0x01)
97 #define MAX_NAME_LEN 64
99 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
100 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
102 /* For higher baud Rates use TIOCEXBAUD */
103 #define TIOCEXBAUD 0x5462
105 /* vendor id and device id defines */
107 /* The native mos7840/7820 component */
108 #define USB_VENDOR_ID_MOSCHIP 0x9710
109 #define MOSCHIP_DEVICE_ID_7840 0x7840
110 #define MOSCHIP_DEVICE_ID_7820 0x7820
111 #define MOSCHIP_DEVICE_ID_7810 0x7810
112 /* The native component can have its vendor/device id's overridden
113 * in vendor-specific implementations. Such devices can be handled
114 * by making a change here, in id_table.
116 #define USB_VENDOR_ID_BANDB 0x0856
117 #define BANDB_DEVICE_ID_USO9ML2_2 0xAC22
118 #define BANDB_DEVICE_ID_USO9ML2_2P 0xBC00
119 #define BANDB_DEVICE_ID_USO9ML2_4 0xAC24
120 #define BANDB_DEVICE_ID_USO9ML2_4P 0xBC01
121 #define BANDB_DEVICE_ID_US9ML2_2 0xAC29
122 #define BANDB_DEVICE_ID_US9ML2_4 0xAC30
123 #define BANDB_DEVICE_ID_USPTL4_2 0xAC31
124 #define BANDB_DEVICE_ID_USPTL4_4 0xAC32
125 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
126 #define BANDB_DEVICE_ID_USOPTL4_2P 0xBC02
127 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
128 #define BANDB_DEVICE_ID_USOPTL4_4P 0xBC03
129 #define BANDB_DEVICE_ID_USOPTL2_4 0xAC24
131 /* This driver also supports
132 * ATEN UC2324 device using Moschip MCS7840
133 * ATEN UC2322 device using Moschip MCS7820
135 #define USB_VENDOR_ID_ATENINTL 0x0557
136 #define ATENINTL_DEVICE_ID_UC2324 0x2011
137 #define ATENINTL_DEVICE_ID_UC2322 0x7820
139 /* Interrupt Routine Defines */
141 #define SERIAL_IIR_RLS 0x06
142 #define SERIAL_IIR_MS 0x00
145 * Emulation of the bit mask on the LINE STATUS REGISTER.
147 #define SERIAL_LSR_DR 0x0001
148 #define SERIAL_LSR_OE 0x0002
149 #define SERIAL_LSR_PE 0x0004
150 #define SERIAL_LSR_FE 0x0008
151 #define SERIAL_LSR_BI 0x0010
153 #define MOS_MSR_DELTA_CTS 0x10
154 #define MOS_MSR_DELTA_DSR 0x20
155 #define MOS_MSR_DELTA_RI 0x40
156 #define MOS_MSR_DELTA_CD 0x80
158 /* Serial Port register Address */
159 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
160 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
161 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
162 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
163 #define LINE_STATUS_REGISTER ((__u16)(0x05))
164 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
165 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
166 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
167 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
169 #define CLK_MULTI_REGISTER ((__u16)(0x02))
170 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
171 #define GPIO_REGISTER ((__u16)(0x07))
173 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
176 * URB POOL related defines
178 #define NUM_URBS 16 /* URB Count */
179 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
181 /* LED on/off milliseconds*/
182 #define LED_ON_MS 500
183 #define LED_OFF_MS 500
186 MOS7840_FLAG_CTRL_BUSY,
187 MOS7840_FLAG_LED_BUSY,
190 static const struct usb_device_id id_table[] = {
191 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
192 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
193 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7810)},
194 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2)},
195 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_2P)},
196 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4)},
197 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USO9ML2_4P)},
198 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_2)},
199 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_US9ML2_4)},
200 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_2)},
201 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USPTL4_4)},
202 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
203 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2P)},
204 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
205 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4P)},
206 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL2_4)},
207 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
208 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2322)},
209 {} /* terminating entry */
211 MODULE_DEVICE_TABLE(usb, id_table);
213 /* This structure holds all of the local port information */
215 struct moschip_port {
216 int port_num; /*Actual port number in the device(1,2,etc) */
217 struct urb *read_urb; /* read URB for this port */
218 __u8 shadowLCR; /* last LCR value received */
219 __u8 shadowMCR; /* last MCR value received */
222 struct usb_serial_port *port; /* loop back to the owner of this object */
226 __u8 ControlRegOffset;
228 /* for processing control URBS in interrupt context */
229 struct urb *control_urb;
230 struct usb_ctrlrequest *dr;
234 spinlock_t pool_lock;
235 struct urb *write_urb_pool[NUM_URBS];
239 /* For device(s) with LED indicator */
241 struct timer_list led_timer1; /* Timer for LED on */
242 struct timer_list led_timer2; /* Timer for LED off */
244 struct usb_ctrlrequest *led_dr;
250 * mos7840_set_reg_sync
251 * To set the Control register by calling usb_fill_control_urb function
252 * by passing usb_sndctrlpipe function as parameter.
255 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
258 struct usb_device *dev = port->serial->dev;
260 dev_dbg(&port->dev, "mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
262 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
263 MCS_WR_RTYPE, val, reg, NULL, 0,
268 * mos7840_get_reg_sync
269 * To set the Uart register by calling usb_fill_control_urb function by
270 * passing usb_rcvctrlpipe function as parameter.
273 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
276 struct usb_device *dev = port->serial->dev;
280 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
284 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
285 MCS_RD_RTYPE, 0, reg, buf, VENDOR_READ_LENGTH,
288 dev_dbg(&port->dev, "%s offset is %x, return val %x\n", __func__, reg, *val);
295 * mos7840_set_uart_reg
296 * To set the Uart register by calling usb_fill_control_urb function by
297 * passing usb_sndctrlpipe function as parameter.
300 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
304 struct usb_device *dev = port->serial->dev;
306 /* For the UART control registers, the application number need
308 if (port->serial->num_ports == 4) {
309 val |= ((__u16)port->port_number + 1) << 8;
311 if (port->port_number == 0) {
312 val |= ((__u16)port->port_number + 1) << 8;
314 val |= ((__u16)port->port_number + 2) << 8;
317 dev_dbg(&port->dev, "%s application number is %x\n", __func__, val);
318 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
319 MCS_WR_RTYPE, val, reg, NULL, 0,
325 * mos7840_get_uart_reg
326 * To set the Control register by calling usb_fill_control_urb function
327 * by passing usb_rcvctrlpipe function as parameter.
329 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
332 struct usb_device *dev = port->serial->dev;
337 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
341 /* Wval is same as application number */
342 if (port->serial->num_ports == 4) {
343 Wval = ((__u16)port->port_number + 1) << 8;
345 if (port->port_number == 0) {
346 Wval = ((__u16)port->port_number + 1) << 8;
348 Wval = ((__u16)port->port_number + 2) << 8;
351 dev_dbg(&port->dev, "%s application number is %x\n", __func__, Wval);
352 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
353 MCS_RD_RTYPE, Wval, reg, buf, VENDOR_READ_LENGTH,
361 static void mos7840_dump_serial_port(struct usb_serial_port *port,
362 struct moschip_port *mos7840_port)
365 dev_dbg(&port->dev, "SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
366 dev_dbg(&port->dev, "ControlRegOffset is %2x\n", mos7840_port->ControlRegOffset);
367 dev_dbg(&port->dev, "DCRRegOffset is %2x\n", mos7840_port->DcrRegOffset);
371 /************************************************************************/
372 /************************************************************************/
373 /* I N T E R F A C E F U N C T I O N S */
374 /* I N T E R F A C E F U N C T I O N S */
375 /************************************************************************/
376 /************************************************************************/
378 static inline void mos7840_set_port_private(struct usb_serial_port *port,
379 struct moschip_port *data)
381 usb_set_serial_port_data(port, (void *)data);
384 static inline struct moschip_port *mos7840_get_port_private(struct
388 return (struct moschip_port *)usb_get_serial_port_data(port);
391 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
393 struct moschip_port *mos7840_port;
394 struct async_icount *icount;
397 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
399 icount = &mos7840_port->port->icount;
401 /* update input line counters */
402 if (new_msr & MOS_MSR_DELTA_CTS)
404 if (new_msr & MOS_MSR_DELTA_DSR)
406 if (new_msr & MOS_MSR_DELTA_CD)
408 if (new_msr & MOS_MSR_DELTA_RI)
411 wake_up_interruptible(&port->port->port.delta_msr_wait);
415 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
417 struct async_icount *icount;
419 if (new_lsr & SERIAL_LSR_BI) {
421 * Parity and Framing errors only count if they
422 * occur exclusive of a break being
425 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
428 /* update input line counters */
429 icount = &port->port->icount;
430 if (new_lsr & SERIAL_LSR_BI)
432 if (new_lsr & SERIAL_LSR_OE)
434 if (new_lsr & SERIAL_LSR_PE)
436 if (new_lsr & SERIAL_LSR_FE)
440 /************************************************************************/
441 /************************************************************************/
442 /* U S B C A L L B A C K F U N C T I O N S */
443 /* U S B C A L L B A C K F U N C T I O N S */
444 /************************************************************************/
445 /************************************************************************/
447 static void mos7840_control_callback(struct urb *urb)
450 struct moschip_port *mos7840_port;
451 struct device *dev = &urb->dev->dev;
453 int status = urb->status;
455 mos7840_port = urb->context;
464 /* this urb is terminated, clean up */
465 dev_dbg(dev, "%s - urb shutting down with status: %d\n", __func__, status);
468 dev_dbg(dev, "%s - nonzero urb status received: %d\n", __func__, status);
472 dev_dbg(dev, "%s urb buffer size is %d\n", __func__, urb->actual_length);
473 dev_dbg(dev, "%s mos7840_port->MsrLsr is %d port %d\n", __func__,
474 mos7840_port->MsrLsr, mos7840_port->port_num);
475 data = urb->transfer_buffer;
476 regval = (__u8) data[0];
477 dev_dbg(dev, "%s data is %x\n", __func__, regval);
478 if (mos7840_port->MsrLsr == 0)
479 mos7840_handle_new_msr(mos7840_port, regval);
480 else if (mos7840_port->MsrLsr == 1)
481 mos7840_handle_new_lsr(mos7840_port, regval);
483 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mos7840_port->flags);
486 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
489 struct usb_device *dev = mcs->port->serial->dev;
490 struct usb_ctrlrequest *dr = mcs->dr;
491 unsigned char *buffer = mcs->ctrl_buf;
494 if (test_and_set_bit_lock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags))
497 dr->bRequestType = MCS_RD_RTYPE;
498 dr->bRequest = MCS_RDREQ;
499 dr->wValue = cpu_to_le16(Wval); /* 0 */
500 dr->wIndex = cpu_to_le16(reg);
501 dr->wLength = cpu_to_le16(2);
503 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
504 (unsigned char *)dr, buffer, 2,
505 mos7840_control_callback, mcs);
506 mcs->control_urb->transfer_buffer_length = 2;
507 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
509 clear_bit_unlock(MOS7840_FLAG_CTRL_BUSY, &mcs->flags);
514 static void mos7840_set_led_callback(struct urb *urb)
516 switch (urb->status) {
523 /* This urb is terminated, clean up */
524 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
525 __func__, urb->status);
528 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
529 __func__, urb->status);
533 static void mos7840_set_led_async(struct moschip_port *mcs, __u16 wval,
536 struct usb_device *dev = mcs->port->serial->dev;
537 struct usb_ctrlrequest *dr = mcs->led_dr;
539 dr->bRequestType = MCS_WR_RTYPE;
540 dr->bRequest = MCS_WRREQ;
541 dr->wValue = cpu_to_le16(wval);
542 dr->wIndex = cpu_to_le16(reg);
543 dr->wLength = cpu_to_le16(0);
545 usb_fill_control_urb(mcs->led_urb, dev, usb_sndctrlpipe(dev, 0),
546 (unsigned char *)dr, NULL, 0, mos7840_set_led_callback, NULL);
548 usb_submit_urb(mcs->led_urb, GFP_ATOMIC);
551 static void mos7840_set_led_sync(struct usb_serial_port *port, __u16 reg,
554 struct usb_device *dev = port->serial->dev;
556 usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ, MCS_WR_RTYPE,
557 val, reg, NULL, 0, MOS_WDR_TIMEOUT);
560 static void mos7840_led_off(unsigned long arg)
562 struct moschip_port *mcs = (struct moschip_port *) arg;
565 mos7840_set_led_async(mcs, 0x0300, MODEM_CONTROL_REGISTER);
566 mod_timer(&mcs->led_timer2,
567 jiffies + msecs_to_jiffies(LED_OFF_MS));
570 static void mos7840_led_flag_off(unsigned long arg)
572 struct moschip_port *mcs = (struct moschip_port *) arg;
574 clear_bit_unlock(MOS7840_FLAG_LED_BUSY, &mcs->flags);
577 static void mos7840_led_activity(struct usb_serial_port *port)
579 struct moschip_port *mos7840_port = usb_get_serial_port_data(port);
581 if (test_and_set_bit_lock(MOS7840_FLAG_LED_BUSY, &mos7840_port->flags))
584 mos7840_set_led_async(mos7840_port, 0x0301, MODEM_CONTROL_REGISTER);
585 mod_timer(&mos7840_port->led_timer1,
586 jiffies + msecs_to_jiffies(LED_ON_MS));
589 /*****************************************************************************
590 * mos7840_interrupt_callback
591 * this is the callback function for when we have received data on the
592 * interrupt endpoint.
593 *****************************************************************************/
595 static void mos7840_interrupt_callback(struct urb *urb)
599 struct moschip_port *mos7840_port;
600 struct usb_serial *serial;
605 __u16 wval, wreg = 0;
606 int status = urb->status;
615 /* this urb is terminated, clean up */
616 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
620 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
625 length = urb->actual_length;
626 data = urb->transfer_buffer;
628 serial = urb->context;
630 /* Moschip get 5 bytes
631 * Byte 1 IIR Port 1 (port.number is 0)
632 * Byte 2 IIR Port 2 (port.number is 1)
633 * Byte 3 IIR Port 3 (port.number is 2)
634 * Byte 4 IIR Port 4 (port.number is 3)
635 * Byte 5 FIFO status for both */
638 dev_dbg(&urb->dev->dev, "%s", "Wrong data !!!\n");
642 sp[0] = (__u8) data[0];
643 sp[1] = (__u8) data[1];
644 sp[2] = (__u8) data[2];
645 sp[3] = (__u8) data[3];
648 for (i = 0; i < serial->num_ports; i++) {
649 mos7840_port = mos7840_get_port_private(serial->port[i]);
650 wval = ((__u16)serial->port[i]->port_number + 1) << 8;
651 if (mos7840_port->open) {
653 dev_dbg(&urb->dev->dev, "SP%d No Interrupt !!!\n", i);
655 switch (sp[i] & 0x0f) {
657 dev_dbg(&urb->dev->dev, "Serial Port %d: Receiver status error or \n", i);
658 dev_dbg(&urb->dev->dev, "address bit detected in 9-bit mode\n");
659 mos7840_port->MsrLsr = 1;
660 wreg = LINE_STATUS_REGISTER;
663 dev_dbg(&urb->dev->dev, "Serial Port %d: Modem status change\n", i);
664 mos7840_port->MsrLsr = 0;
665 wreg = MODEM_STATUS_REGISTER;
668 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
673 /* the completion handler for the control urb will resubmit */
676 result = usb_submit_urb(urb, GFP_ATOMIC);
678 dev_err(&urb->dev->dev,
679 "%s - Error %d submitting interrupt urb\n",
684 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
685 const char *function)
688 pr_debug("%s - port == NULL\n", function);
692 pr_debug("%s - port->serial == NULL\n", function);
699 /* Inline functions to check the sanity of a pointer that is passed to us */
700 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
701 const char *function)
704 pr_debug("%s - serial == NULL\n", function);
708 pr_debug("%s - serial->type == NULL!\n", function);
715 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
716 const char *function)
718 /* if no port was specified, or it fails a paranoia check */
720 mos7840_port_paranoia_check(port, function) ||
721 mos7840_serial_paranoia_check(port->serial, function)) {
722 /* then say that we don't have a valid usb_serial thing,
723 * which will end up genrating -ENODEV return values */
730 /*****************************************************************************
731 * mos7840_bulk_in_callback
732 * this is the callback function for when we have received data on the
734 *****************************************************************************/
736 static void mos7840_bulk_in_callback(struct urb *urb)
740 struct usb_serial *serial;
741 struct usb_serial_port *port;
742 struct moschip_port *mos7840_port;
743 int status = urb->status;
745 mos7840_port = urb->context;
750 dev_dbg(&urb->dev->dev, "nonzero read bulk status received: %d\n", status);
751 mos7840_port->read_urb_busy = false;
755 port = mos7840_port->port;
756 if (mos7840_port_paranoia_check(port, __func__)) {
757 mos7840_port->read_urb_busy = false;
761 serial = mos7840_get_usb_serial(port, __func__);
763 mos7840_port->read_urb_busy = false;
767 data = urb->transfer_buffer;
768 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
770 if (urb->actual_length) {
771 struct tty_port *tport = &mos7840_port->port->port;
772 tty_insert_flip_string(tport, data, urb->actual_length);
773 tty_flip_buffer_push(tport);
774 port->icount.rx += urb->actual_length;
775 dev_dbg(&port->dev, "icount.rx is %d:\n", port->icount.rx);
778 if (!mos7840_port->read_urb) {
779 dev_dbg(&port->dev, "%s", "URB KILLED !!!\n");
780 mos7840_port->read_urb_busy = false;
784 if (mos7840_port->has_led)
785 mos7840_led_activity(port);
787 mos7840_port->read_urb_busy = true;
788 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
791 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, retval = %d\n", retval);
792 mos7840_port->read_urb_busy = false;
796 /*****************************************************************************
797 * mos7840_bulk_out_data_callback
798 * this is the callback function for when we have finished sending
799 * serial data on the bulk out endpoint.
800 *****************************************************************************/
802 static void mos7840_bulk_out_data_callback(struct urb *urb)
804 struct moschip_port *mos7840_port;
805 struct usb_serial_port *port;
806 int status = urb->status;
809 mos7840_port = urb->context;
810 port = mos7840_port->port;
811 spin_lock(&mos7840_port->pool_lock);
812 for (i = 0; i < NUM_URBS; i++) {
813 if (urb == mos7840_port->write_urb_pool[i]) {
814 mos7840_port->busy[i] = 0;
818 spin_unlock(&mos7840_port->pool_lock);
821 dev_dbg(&port->dev, "nonzero write bulk status received:%d\n", status);
825 if (mos7840_port_paranoia_check(port, __func__))
828 if (mos7840_port->open)
829 tty_port_tty_wakeup(&port->port);
833 /************************************************************************/
834 /* 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 */
835 /************************************************************************/
837 /*****************************************************************************
839 * this function is called by the tty driver when a port is opened
840 * If successful, we return 0
841 * Otherwise we return a negative error number.
842 *****************************************************************************/
844 static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
848 struct usb_serial *serial;
852 struct moschip_port *mos7840_port;
853 struct moschip_port *port0;
855 if (mos7840_port_paranoia_check(port, __func__))
858 serial = port->serial;
860 if (mos7840_serial_paranoia_check(serial, __func__))
863 mos7840_port = mos7840_get_port_private(port);
864 port0 = mos7840_get_port_private(serial->port[0]);
866 if (mos7840_port == NULL || port0 == NULL)
869 usb_clear_halt(serial->dev, port->write_urb->pipe);
870 usb_clear_halt(serial->dev, port->read_urb->pipe);
873 /* Initialising the write urb pool */
874 for (j = 0; j < NUM_URBS; ++j) {
875 urb = usb_alloc_urb(0, GFP_KERNEL);
876 mos7840_port->write_urb_pool[j] = urb;
880 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
882 if (!urb->transfer_buffer) {
884 mos7840_port->write_urb_pool[j] = NULL;
889 /*****************************************************************************
890 * Initialize MCS7840 -- Write Init values to corresponding Registers
898 * 0x08 : SP1/2 Control Reg
899 *****************************************************************************/
901 /* NEED to check the following Block */
904 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
906 dev_dbg(&port->dev, "Reading Spreg failed\n");
910 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
912 dev_dbg(&port->dev, "writing Spreg failed\n");
917 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
919 dev_dbg(&port->dev, "writing Spreg failed\n");
922 /* End of block to be checked */
925 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
928 dev_dbg(&port->dev, "Reading Controlreg failed\n");
931 Data |= 0x08; /* Driver done bit */
932 Data |= 0x20; /* rx_disable */
933 status = mos7840_set_reg_sync(port,
934 mos7840_port->ControlRegOffset, Data);
936 dev_dbg(&port->dev, "writing Controlreg failed\n");
939 /* do register settings here */
940 /* Set all regs to the device default values. */
941 /***********************************
942 * First Disable all interrupts.
943 ***********************************/
945 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
947 dev_dbg(&port->dev, "disabling interrupts failed\n");
950 /* Set FIFO_CONTROL_REGISTER to the default value */
952 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
954 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
959 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
961 dev_dbg(&port->dev, "Writing FIFO_CONTROL_REGISTER failed\n");
966 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
967 mos7840_port->shadowLCR = Data;
970 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
971 mos7840_port->shadowMCR = Data;
974 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
975 mos7840_port->shadowLCR = Data;
977 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
978 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
981 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
984 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
987 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
989 Data = Data & ~SERIAL_LCR_DLAB;
990 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
991 mos7840_port->shadowLCR = Data;
993 /* clearing Bulkin and Bulkout Fifo */
995 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
998 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1000 Data = Data & ~0x0c;
1001 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1002 /* Finally enable all interrupts */
1004 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1006 /* clearing rx_disable */
1008 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1010 Data = Data & ~0x20;
1011 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1016 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1019 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1022 /* Check to see if we've set up our endpoint info yet *
1023 * (can't set it up in mos7840_startup as the structures *
1024 * were not set up at that time.) */
1025 if (port0->open_ports == 1) {
1026 if (serial->port[0]->interrupt_in_buffer == NULL) {
1027 /* set up interrupt urb */
1028 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1030 usb_rcvintpipe(serial->dev,
1031 serial->port[0]->interrupt_in_endpointAddress),
1032 serial->port[0]->interrupt_in_buffer,
1033 serial->port[0]->interrupt_in_urb->
1034 transfer_buffer_length,
1035 mos7840_interrupt_callback,
1037 serial->port[0]->interrupt_in_urb->interval);
1039 /* start interrupt read for mos7840 */
1041 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1044 dev_err(&port->dev, "%s - Error %d submitting "
1045 "interrupt urb\n", __func__, response);
1052 /* see if we've set up our endpoint info yet *
1053 * (can't set it up in mos7840_startup as the *
1054 * structures were not set up at that time.) */
1056 dev_dbg(&port->dev, "port number is %d\n", port->port_number);
1057 dev_dbg(&port->dev, "minor number is %d\n", port->minor);
1058 dev_dbg(&port->dev, "Bulkin endpoint is %d\n", port->bulk_in_endpointAddress);
1059 dev_dbg(&port->dev, "BulkOut endpoint is %d\n", port->bulk_out_endpointAddress);
1060 dev_dbg(&port->dev, "Interrupt endpoint is %d\n", port->interrupt_in_endpointAddress);
1061 dev_dbg(&port->dev, "port's number in the device is %d\n", mos7840_port->port_num);
1062 mos7840_port->read_urb = port->read_urb;
1064 /* set up our bulk in urb */
1065 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1066 usb_fill_bulk_urb(mos7840_port->read_urb,
1068 usb_rcvbulkpipe(serial->dev,
1069 (port->bulk_in_endpointAddress) + 2),
1070 port->bulk_in_buffer,
1071 mos7840_port->read_urb->transfer_buffer_length,
1072 mos7840_bulk_in_callback, mos7840_port);
1074 usb_fill_bulk_urb(mos7840_port->read_urb,
1076 usb_rcvbulkpipe(serial->dev,
1077 port->bulk_in_endpointAddress),
1078 port->bulk_in_buffer,
1079 mos7840_port->read_urb->transfer_buffer_length,
1080 mos7840_bulk_in_callback, mos7840_port);
1083 dev_dbg(&port->dev, "%s: bulkin endpoint is %d\n", __func__, port->bulk_in_endpointAddress);
1084 mos7840_port->read_urb_busy = true;
1085 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1087 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1088 __func__, response);
1089 mos7840_port->read_urb_busy = false;
1092 /* initialize our port settings */
1093 /* Must set to enable ints! */
1094 mos7840_port->shadowMCR = MCR_MASTER_IE;
1095 /* send a open port command */
1096 mos7840_port->open = 1;
1097 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1101 for (j = 0; j < NUM_URBS; ++j) {
1102 urb = mos7840_port->write_urb_pool[j];
1105 kfree(urb->transfer_buffer);
1111 /*****************************************************************************
1112 * mos7840_chars_in_buffer
1113 * this function is called by the tty driver when it wants to know how many
1114 * bytes of data we currently have outstanding in the port (data that has
1115 * been written, but hasn't made it out the port yet)
1116 * If successful, we return the number of bytes left to be written in the
1118 * Otherwise we return zero.
1119 *****************************************************************************/
1121 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1123 struct usb_serial_port *port = tty->driver_data;
1126 unsigned long flags;
1127 struct moschip_port *mos7840_port;
1129 if (mos7840_port_paranoia_check(port, __func__))
1132 mos7840_port = mos7840_get_port_private(port);
1133 if (mos7840_port == NULL)
1136 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1137 for (i = 0; i < NUM_URBS; ++i) {
1138 if (mos7840_port->busy[i]) {
1139 struct urb *urb = mos7840_port->write_urb_pool[i];
1140 chars += urb->transfer_buffer_length;
1143 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1144 dev_dbg(&port->dev, "%s - returns %d\n", __func__, chars);
1149 /*****************************************************************************
1151 * this function is called by the tty driver when a port is closed
1152 *****************************************************************************/
1154 static void mos7840_close(struct usb_serial_port *port)
1156 struct usb_serial *serial;
1157 struct moschip_port *mos7840_port;
1158 struct moschip_port *port0;
1162 if (mos7840_port_paranoia_check(port, __func__))
1165 serial = mos7840_get_usb_serial(port, __func__);
1169 mos7840_port = mos7840_get_port_private(port);
1170 port0 = mos7840_get_port_private(serial->port[0]);
1172 if (mos7840_port == NULL || port0 == NULL)
1175 for (j = 0; j < NUM_URBS; ++j)
1176 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1178 /* Freeing Write URBs */
1179 for (j = 0; j < NUM_URBS; ++j) {
1180 if (mos7840_port->write_urb_pool[j]) {
1181 kfree(mos7840_port->write_urb_pool[j]->transfer_buffer);
1182 usb_free_urb(mos7840_port->write_urb_pool[j]);
1186 usb_kill_urb(mos7840_port->read_urb);
1187 mos7840_port->read_urb_busy = false;
1189 port0->open_ports--;
1190 dev_dbg(&port->dev, "%s in close%d\n", __func__, port0->open_ports);
1191 if (port0->open_ports == 0) {
1192 if (serial->port[0]->interrupt_in_urb) {
1193 dev_dbg(&port->dev, "Shutdown interrupt_in_urb\n");
1194 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1199 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1202 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1204 mos7840_port->open = 0;
1207 /*****************************************************************************
1209 * this function sends a break to the port
1210 *****************************************************************************/
1211 static void mos7840_break(struct tty_struct *tty, int break_state)
1213 struct usb_serial_port *port = tty->driver_data;
1215 struct usb_serial *serial;
1216 struct moschip_port *mos7840_port;
1218 if (mos7840_port_paranoia_check(port, __func__))
1221 serial = mos7840_get_usb_serial(port, __func__);
1225 mos7840_port = mos7840_get_port_private(port);
1227 if (mos7840_port == NULL)
1230 if (break_state == -1)
1231 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1233 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1235 /* FIXME: no locking on shadowLCR anywhere in driver */
1236 mos7840_port->shadowLCR = data;
1237 dev_dbg(&port->dev, "%s mos7840_port->shadowLCR is %x\n", __func__, mos7840_port->shadowLCR);
1238 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1239 mos7840_port->shadowLCR);
1242 /*****************************************************************************
1243 * mos7840_write_room
1244 * this function is called by the tty driver when it wants to know how many
1245 * bytes of data we can accept for a specific port.
1246 * If successful, we return the amount of room that we have for this port
1247 * Otherwise we return a negative error number.
1248 *****************************************************************************/
1250 static int mos7840_write_room(struct tty_struct *tty)
1252 struct usb_serial_port *port = tty->driver_data;
1255 unsigned long flags;
1256 struct moschip_port *mos7840_port;
1258 if (mos7840_port_paranoia_check(port, __func__))
1261 mos7840_port = mos7840_get_port_private(port);
1262 if (mos7840_port == NULL)
1265 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1266 for (i = 0; i < NUM_URBS; ++i) {
1267 if (!mos7840_port->busy[i])
1268 room += URB_TRANSFER_BUFFER_SIZE;
1270 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1272 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1273 dev_dbg(&mos7840_port->port->dev, "%s - returns %d\n", __func__, room);
1278 /*****************************************************************************
1280 * this function is called by the tty driver when data should be written to
1282 * If successful, we return the number of bytes written, otherwise we
1283 * return a negative error number.
1284 *****************************************************************************/
1286 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1287 const unsigned char *data, int count)
1293 unsigned long flags;
1295 struct moschip_port *mos7840_port;
1296 struct usb_serial *serial;
1299 const unsigned char *current_position = data;
1300 unsigned char *data1;
1302 if (mos7840_port_paranoia_check(port, __func__))
1305 serial = port->serial;
1306 if (mos7840_serial_paranoia_check(serial, __func__))
1309 mos7840_port = mos7840_get_port_private(port);
1310 if (mos7840_port == NULL)
1313 /* try to find a free urb in the list */
1316 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1317 for (i = 0; i < NUM_URBS; ++i) {
1318 if (!mos7840_port->busy[i]) {
1319 mos7840_port->busy[i] = 1;
1320 urb = mos7840_port->write_urb_pool[i];
1321 dev_dbg(&port->dev, "URB:%d\n", i);
1325 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1328 dev_dbg(&port->dev, "%s - no more free urbs\n", __func__);
1332 if (urb->transfer_buffer == NULL) {
1333 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
1335 if (!urb->transfer_buffer)
1338 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1340 memcpy(urb->transfer_buffer, current_position, transfer_size);
1342 /* fill urb with data and submit */
1343 if ((serial->num_ports == 2) && (((__u16)port->port_number % 2) != 0)) {
1344 usb_fill_bulk_urb(urb,
1346 usb_sndbulkpipe(serial->dev,
1347 (port->bulk_out_endpointAddress) + 2),
1348 urb->transfer_buffer,
1350 mos7840_bulk_out_data_callback, mos7840_port);
1352 usb_fill_bulk_urb(urb,
1354 usb_sndbulkpipe(serial->dev,
1355 port->bulk_out_endpointAddress),
1356 urb->transfer_buffer,
1358 mos7840_bulk_out_data_callback, mos7840_port);
1361 data1 = urb->transfer_buffer;
1362 dev_dbg(&port->dev, "bulkout endpoint is %d\n", port->bulk_out_endpointAddress);
1364 if (mos7840_port->has_led)
1365 mos7840_led_activity(port);
1367 /* send it down the pipe */
1368 status = usb_submit_urb(urb, GFP_ATOMIC);
1371 mos7840_port->busy[i] = 0;
1372 dev_err_console(port, "%s - usb_submit_urb(write bulk) failed "
1373 "with status = %d\n", __func__, status);
1374 bytes_sent = status;
1377 bytes_sent = transfer_size;
1378 port->icount.tx += transfer_size;
1379 dev_dbg(&port->dev, "icount.tx is %d:\n", port->icount.tx);
1385 /*****************************************************************************
1387 * this function is called by the tty driver when it wants to stop the data
1388 * being read from the port.
1389 *****************************************************************************/
1391 static void mos7840_throttle(struct tty_struct *tty)
1393 struct usb_serial_port *port = tty->driver_data;
1394 struct moschip_port *mos7840_port;
1397 if (mos7840_port_paranoia_check(port, __func__))
1400 mos7840_port = mos7840_get_port_private(port);
1402 if (mos7840_port == NULL)
1405 if (!mos7840_port->open) {
1406 dev_dbg(&port->dev, "%s", "port not opened\n");
1410 /* if we are implementing XON/XOFF, send the stop character */
1412 unsigned char stop_char = STOP_CHAR(tty);
1413 status = mos7840_write(tty, port, &stop_char, 1);
1417 /* if we are implementing RTS/CTS, toggle that line */
1418 if (C_CRTSCTS(tty)) {
1419 mos7840_port->shadowMCR &= ~MCR_RTS;
1420 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1421 mos7840_port->shadowMCR);
1427 /*****************************************************************************
1428 * mos7840_unthrottle
1429 * this function is called by the tty driver when it wants to resume
1430 * the data being read from the port (called after mos7840_throttle is
1432 *****************************************************************************/
1433 static void mos7840_unthrottle(struct tty_struct *tty)
1435 struct usb_serial_port *port = tty->driver_data;
1437 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1439 if (mos7840_port_paranoia_check(port, __func__))
1442 if (mos7840_port == NULL)
1445 if (!mos7840_port->open) {
1446 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1450 /* if we are implementing XON/XOFF, send the start character */
1452 unsigned char start_char = START_CHAR(tty);
1453 status = mos7840_write(tty, port, &start_char, 1);
1458 /* if we are implementing RTS/CTS, toggle that line */
1459 if (C_CRTSCTS(tty)) {
1460 mos7840_port->shadowMCR |= MCR_RTS;
1461 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1462 mos7840_port->shadowMCR);
1468 static int mos7840_tiocmget(struct tty_struct *tty)
1470 struct usb_serial_port *port = tty->driver_data;
1471 struct moschip_port *mos7840_port;
1472 unsigned int result;
1476 mos7840_port = mos7840_get_port_private(port);
1478 if (mos7840_port == NULL)
1481 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1484 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1487 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1488 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1489 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1490 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1491 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1492 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1493 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1495 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
1500 static int mos7840_tiocmset(struct tty_struct *tty,
1501 unsigned int set, unsigned int clear)
1503 struct usb_serial_port *port = tty->driver_data;
1504 struct moschip_port *mos7840_port;
1508 mos7840_port = mos7840_get_port_private(port);
1510 if (mos7840_port == NULL)
1513 /* FIXME: What locks the port registers ? */
1514 mcr = mos7840_port->shadowMCR;
1515 if (clear & TIOCM_RTS)
1517 if (clear & TIOCM_DTR)
1519 if (clear & TIOCM_LOOP)
1520 mcr &= ~MCR_LOOPBACK;
1522 if (set & TIOCM_RTS)
1524 if (set & TIOCM_DTR)
1526 if (set & TIOCM_LOOP)
1527 mcr |= MCR_LOOPBACK;
1529 mos7840_port->shadowMCR = mcr;
1531 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1533 dev_dbg(&port->dev, "setting MODEM_CONTROL_REGISTER Failed\n");
1540 /*****************************************************************************
1541 * mos7840_calc_baud_rate_divisor
1542 * this function calculates the proper baud rate divisor for the specified
1544 *****************************************************************************/
1545 static int mos7840_calc_baud_rate_divisor(struct usb_serial_port *port,
1546 int baudRate, int *divisor,
1549 dev_dbg(&port->dev, "%s - %d\n", __func__, baudRate);
1551 if (baudRate <= 115200) {
1552 *divisor = 115200 / baudRate;
1555 if ((baudRate > 115200) && (baudRate <= 230400)) {
1556 *divisor = 230400 / baudRate;
1557 *clk_sel_val = 0x10;
1558 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1559 *divisor = 403200 / baudRate;
1560 *clk_sel_val = 0x20;
1561 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1562 *divisor = 460800 / baudRate;
1563 *clk_sel_val = 0x30;
1564 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1565 *divisor = 806400 / baudRate;
1566 *clk_sel_val = 0x40;
1567 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1568 *divisor = 921600 / baudRate;
1569 *clk_sel_val = 0x50;
1570 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1571 *divisor = 1572864 / baudRate;
1572 *clk_sel_val = 0x60;
1573 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1574 *divisor = 3145728 / baudRate;
1575 *clk_sel_val = 0x70;
1580 /*****************************************************************************
1581 * mos7840_send_cmd_write_baud_rate
1582 * this function sends the proper command to change the baud rate of the
1584 *****************************************************************************/
1586 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1592 unsigned char number;
1594 struct usb_serial_port *port;
1596 if (mos7840_port == NULL)
1599 port = mos7840_port->port;
1600 if (mos7840_port_paranoia_check(port, __func__))
1603 if (mos7840_serial_paranoia_check(port->serial, __func__))
1606 number = mos7840_port->port->port_number;
1608 dev_dbg(&port->dev, "%s - baud = %d\n", __func__, baudRate);
1609 /* reset clk_uart_sel in spregOffset */
1610 if (baudRate > 115200) {
1611 #ifdef HW_flow_control
1612 /* NOTE: need to see the pther register to modify */
1613 /* setting h/w flow control bit to 1 */
1615 mos7840_port->shadowMCR = Data;
1616 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1619 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1625 #ifdef HW_flow_control
1626 /* setting h/w flow control bit to 0 */
1628 mos7840_port->shadowMCR = Data;
1629 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1632 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1639 if (1) { /* baudRate <= 115200) */
1642 status = mos7840_calc_baud_rate_divisor(port, baudRate, &divisor,
1644 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1647 dev_dbg(&port->dev, "reading spreg failed in set_serial_baud\n");
1650 Data = (Data & 0x8f) | clk_sel_val;
1651 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1654 dev_dbg(&port->dev, "Writing spreg failed in set_serial_baud\n");
1657 /* Calculate the Divisor */
1660 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1663 /* Enable access to divisor latch */
1664 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1665 mos7840_port->shadowLCR = Data;
1666 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1668 /* Write the divisor */
1669 Data = (unsigned char)(divisor & 0xff);
1670 dev_dbg(&port->dev, "set_serial_baud Value to write DLL is %x\n", Data);
1671 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1673 Data = (unsigned char)((divisor & 0xff00) >> 8);
1674 dev_dbg(&port->dev, "set_serial_baud Value to write DLM is %x\n", Data);
1675 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1677 /* Disable access to divisor latch */
1678 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1679 mos7840_port->shadowLCR = Data;
1680 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1686 /*****************************************************************************
1687 * mos7840_change_port_settings
1688 * This routine is called to set the UART on the device to match
1689 * the specified new settings.
1690 *****************************************************************************/
1692 static void mos7840_change_port_settings(struct tty_struct *tty,
1693 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1703 struct usb_serial_port *port;
1704 struct usb_serial *serial;
1706 if (mos7840_port == NULL)
1709 port = mos7840_port->port;
1711 if (mos7840_port_paranoia_check(port, __func__))
1714 if (mos7840_serial_paranoia_check(port->serial, __func__))
1717 serial = port->serial;
1719 if (!mos7840_port->open) {
1720 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1726 lParity = LCR_PAR_NONE;
1728 cflag = tty->termios.c_cflag;
1729 iflag = tty->termios.c_iflag;
1731 /* Change the number of bits */
1732 switch (cflag & CSIZE) {
1751 /* Change the Parity bit */
1752 if (cflag & PARENB) {
1753 if (cflag & PARODD) {
1754 lParity = LCR_PAR_ODD;
1755 dev_dbg(&port->dev, "%s - parity = odd\n", __func__);
1757 lParity = LCR_PAR_EVEN;
1758 dev_dbg(&port->dev, "%s - parity = even\n", __func__);
1762 dev_dbg(&port->dev, "%s - parity = none\n", __func__);
1766 lParity = lParity | 0x20;
1768 /* Change the Stop bit */
1769 if (cflag & CSTOPB) {
1771 dev_dbg(&port->dev, "%s - stop bits = 2\n", __func__);
1774 dev_dbg(&port->dev, "%s - stop bits = 1\n", __func__);
1777 /* Update the LCR with the correct value */
1778 mos7840_port->shadowLCR &=
1779 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1780 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1782 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is %x\n", __func__,
1783 mos7840_port->shadowLCR);
1784 /* Disable Interrupts */
1786 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1789 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1792 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1794 /* Send the updated LCR value to the mos7840 */
1795 Data = mos7840_port->shadowLCR;
1797 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1800 mos7840_port->shadowMCR = Data;
1801 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1803 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1805 /* set up the MCR register and send it to the mos7840 */
1807 mos7840_port->shadowMCR = MCR_MASTER_IE;
1809 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
1811 if (cflag & CRTSCTS)
1812 mos7840_port->shadowMCR |= (MCR_XON_ANY);
1814 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
1816 Data = mos7840_port->shadowMCR;
1817 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1819 /* Determine divisor based on baud rate */
1820 baud = tty_get_baud_rate(tty);
1823 /* pick a default, any default... */
1824 dev_dbg(&port->dev, "%s", "Picked default baud...\n");
1828 dev_dbg(&port->dev, "%s - baud rate = %d\n", __func__, baud);
1829 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
1831 /* Enable Interrupts */
1833 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1835 if (!mos7840_port->read_urb_busy) {
1836 mos7840_port->read_urb_busy = true;
1837 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1839 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1841 mos7840_port->read_urb_busy = false;
1844 dev_dbg(&port->dev, "%s - mos7840_port->shadowLCR is End %x\n", __func__,
1845 mos7840_port->shadowLCR);
1848 /*****************************************************************************
1849 * mos7840_set_termios
1850 * this function is called by the tty driver when it wants to change
1851 * the termios structure
1852 *****************************************************************************/
1854 static void mos7840_set_termios(struct tty_struct *tty,
1855 struct usb_serial_port *port,
1856 struct ktermios *old_termios)
1860 struct usb_serial *serial;
1861 struct moschip_port *mos7840_port;
1863 if (mos7840_port_paranoia_check(port, __func__))
1866 serial = port->serial;
1868 if (mos7840_serial_paranoia_check(serial, __func__))
1871 mos7840_port = mos7840_get_port_private(port);
1873 if (mos7840_port == NULL)
1876 if (!mos7840_port->open) {
1877 dev_dbg(&port->dev, "%s - port not opened\n", __func__);
1881 dev_dbg(&port->dev, "%s", "setting termios - \n");
1883 cflag = tty->termios.c_cflag;
1885 dev_dbg(&port->dev, "%s - clfag %08x iflag %08x\n", __func__,
1886 tty->termios.c_cflag, RELEVANT_IFLAG(tty->termios.c_iflag));
1887 dev_dbg(&port->dev, "%s - old clfag %08x old iflag %08x\n", __func__,
1888 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
1890 /* change the port settings to the new ones specified */
1892 mos7840_change_port_settings(tty, mos7840_port, old_termios);
1894 if (!mos7840_port->read_urb) {
1895 dev_dbg(&port->dev, "%s", "URB KILLED !!!!!\n");
1899 if (!mos7840_port->read_urb_busy) {
1900 mos7840_port->read_urb_busy = true;
1901 status = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1903 dev_dbg(&port->dev, "usb_submit_urb(read bulk) failed, status = %d\n",
1905 mos7840_port->read_urb_busy = false;
1910 /*****************************************************************************
1911 * mos7840_get_lsr_info - get line status register info
1913 * Purpose: Let user call ioctl() to get info when the UART physically
1914 * is emptied. On bus types like RS485, the transmitter must
1915 * release the bus after transmitting. This must be done when
1916 * the transmit shift register is empty, not be done when the
1917 * transmit holding register is empty. This functionality
1918 * allows an RS485 driver to be written in user space.
1919 *****************************************************************************/
1921 static int mos7840_get_lsr_info(struct tty_struct *tty,
1922 unsigned int __user *value)
1925 unsigned int result = 0;
1927 count = mos7840_chars_in_buffer(tty);
1929 result = TIOCSER_TEMT;
1931 if (copy_to_user(value, &result, sizeof(int)))
1936 /*****************************************************************************
1937 * mos7840_get_serial_info
1938 * function to get information about serial port
1939 *****************************************************************************/
1941 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
1942 struct serial_struct __user *retinfo)
1944 struct serial_struct tmp;
1946 if (mos7840_port == NULL)
1949 memset(&tmp, 0, sizeof(tmp));
1951 tmp.type = PORT_16550A;
1952 tmp.line = mos7840_port->port->minor;
1953 tmp.port = mos7840_port->port->port_number;
1955 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
1956 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
1957 tmp.baud_base = 9600;
1958 tmp.close_delay = 5 * HZ;
1959 tmp.closing_wait = 30 * HZ;
1961 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1966 /*****************************************************************************
1968 * this function handles any ioctl calls to the driver
1969 *****************************************************************************/
1971 static int mos7840_ioctl(struct tty_struct *tty,
1972 unsigned int cmd, unsigned long arg)
1974 struct usb_serial_port *port = tty->driver_data;
1975 void __user *argp = (void __user *)arg;
1976 struct moschip_port *mos7840_port;
1978 if (mos7840_port_paranoia_check(port, __func__))
1981 mos7840_port = mos7840_get_port_private(port);
1983 if (mos7840_port == NULL)
1987 /* return number of bytes available */
1990 dev_dbg(&port->dev, "%s TIOCSERGETLSR\n", __func__);
1991 return mos7840_get_lsr_info(tty, argp);
1994 dev_dbg(&port->dev, "%s TIOCGSERIAL\n", __func__);
1995 return mos7840_get_serial_info(mos7840_port, argp);
1998 dev_dbg(&port->dev, "%s TIOCSSERIAL\n", __func__);
2003 return -ENOIOCTLCMD;
2006 static int mos7810_check(struct usb_serial *serial)
2008 int i, pass_count = 0;
2010 __u16 data = 0, mcr_data = 0;
2011 __u16 test_pattern = 0x55AA;
2014 buf = kmalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2016 return 0; /* failed to identify 7810 */
2018 /* Store MCR setting */
2019 res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2020 MCS_RDREQ, MCS_RD_RTYPE, 0x0300, MODEM_CONTROL_REGISTER,
2021 buf, VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2022 if (res == VENDOR_READ_LENGTH)
2025 for (i = 0; i < 16; i++) {
2026 /* Send the 1-bit test pattern out to MCS7810 test pin */
2027 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2028 MCS_WRREQ, MCS_WR_RTYPE,
2029 (0x0300 | (((test_pattern >> i) & 0x0001) << 1)),
2030 MODEM_CONTROL_REGISTER, NULL, 0, MOS_WDR_TIMEOUT);
2032 /* Read the test pattern back */
2033 res = usb_control_msg(serial->dev,
2034 usb_rcvctrlpipe(serial->dev, 0), MCS_RDREQ,
2035 MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2036 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2037 if (res == VENDOR_READ_LENGTH)
2040 /* If this is a MCS7810 device, both test patterns must match */
2041 if (((test_pattern >> i) ^ (~data >> 1)) & 0x0001)
2047 /* Restore MCR setting */
2048 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), MCS_WRREQ,
2049 MCS_WR_RTYPE, 0x0300 | mcr_data, MODEM_CONTROL_REGISTER, NULL,
2050 0, MOS_WDR_TIMEOUT);
2054 if (pass_count == 16)
2060 static int mos7840_probe(struct usb_serial *serial,
2061 const struct usb_device_id *id)
2063 u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
2067 if (product == MOSCHIP_DEVICE_ID_7810 ||
2068 product == MOSCHIP_DEVICE_ID_7820) {
2069 device_type = product;
2073 buf = kzalloc(VENDOR_READ_LENGTH, GFP_KERNEL);
2077 usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
2078 MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, buf,
2079 VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
2081 /* For a MCS7840 device GPIO0 must be set to 1 */
2083 device_type = MOSCHIP_DEVICE_ID_7840;
2084 else if (mos7810_check(serial))
2085 device_type = MOSCHIP_DEVICE_ID_7810;
2087 device_type = MOSCHIP_DEVICE_ID_7820;
2091 usb_set_serial_data(serial, (void *)(unsigned long)device_type);
2096 static int mos7840_calc_num_ports(struct usb_serial *serial)
2098 int device_type = (unsigned long)usb_get_serial_data(serial);
2099 int mos7840_num_ports;
2101 mos7840_num_ports = (device_type >> 4) & 0x000F;
2103 return mos7840_num_ports;
2106 static int mos7840_attach(struct usb_serial *serial)
2108 if (serial->num_bulk_in < serial->num_ports ||
2109 serial->num_bulk_out < serial->num_ports) {
2110 dev_err(&serial->interface->dev, "missing endpoints\n");
2117 static int mos7840_port_probe(struct usb_serial_port *port)
2119 struct usb_serial *serial = port->serial;
2120 int device_type = (unsigned long)usb_get_serial_data(serial);
2121 struct moschip_port *mos7840_port;
2126 /* we set up the pointers to the endpoints in the mos7840_open *
2127 * function, as the structures aren't created yet. */
2129 pnum = port->port_number;
2131 dev_dbg(&port->dev, "mos7840_startup: configuring port %d\n", pnum);
2132 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2136 /* Initialize all port interrupt end point to port 0 int
2137 * endpoint. Our device has only one interrupt end point
2138 * common to all port */
2140 mos7840_port->port = port;
2141 mos7840_set_port_private(port, mos7840_port);
2142 spin_lock_init(&mos7840_port->pool_lock);
2144 /* minor is not initialised until later by
2145 * usb-serial.c:get_free_serial() and cannot therefore be used
2146 * to index device instances */
2147 mos7840_port->port_num = pnum + 1;
2148 dev_dbg(&port->dev, "port->minor = %d\n", port->minor);
2149 dev_dbg(&port->dev, "mos7840_port->port_num = %d\n", mos7840_port->port_num);
2151 if (mos7840_port->port_num == 1) {
2152 mos7840_port->SpRegOffset = 0x0;
2153 mos7840_port->ControlRegOffset = 0x1;
2154 mos7840_port->DcrRegOffset = 0x4;
2155 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 4)) {
2156 mos7840_port->SpRegOffset = 0x8;
2157 mos7840_port->ControlRegOffset = 0x9;
2158 mos7840_port->DcrRegOffset = 0x16;
2159 } else if ((mos7840_port->port_num == 2) && (serial->num_ports == 2)) {
2160 mos7840_port->SpRegOffset = 0xa;
2161 mos7840_port->ControlRegOffset = 0xb;
2162 mos7840_port->DcrRegOffset = 0x19;
2163 } else if ((mos7840_port->port_num == 3) && (serial->num_ports == 4)) {
2164 mos7840_port->SpRegOffset = 0xa;
2165 mos7840_port->ControlRegOffset = 0xb;
2166 mos7840_port->DcrRegOffset = 0x19;
2167 } else if ((mos7840_port->port_num == 4) && (serial->num_ports == 4)) {
2168 mos7840_port->SpRegOffset = 0xc;
2169 mos7840_port->ControlRegOffset = 0xd;
2170 mos7840_port->DcrRegOffset = 0x1c;
2172 mos7840_dump_serial_port(port, mos7840_port);
2173 mos7840_set_port_private(port, mos7840_port);
2175 /* enable rx_disable bit in control register */
2176 status = mos7840_get_reg_sync(port,
2177 mos7840_port->ControlRegOffset, &Data);
2179 dev_dbg(&port->dev, "Reading ControlReg failed status-0x%x\n", status);
2182 dev_dbg(&port->dev, "ControlReg Reading success val is %x, status%d\n", Data, status);
2183 Data |= 0x08; /* setting driver done bit */
2184 Data |= 0x04; /* sp1_bit to have cts change reflect in
2187 /* Data |= 0x20; //rx_disable bit */
2188 status = mos7840_set_reg_sync(port,
2189 mos7840_port->ControlRegOffset, Data);
2191 dev_dbg(&port->dev, "Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2194 dev_dbg(&port->dev, "ControlReg Writing success(rx_disable) status%d\n", status);
2196 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2199 status = mos7840_set_reg_sync(port,
2200 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2202 dev_dbg(&port->dev, "Writing DCR0 failed status-0x%x\n", status);
2205 dev_dbg(&port->dev, "DCR0 Writing success status%d\n", status);
2208 status = mos7840_set_reg_sync(port,
2209 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2211 dev_dbg(&port->dev, "Writing DCR1 failed status-0x%x\n", status);
2214 dev_dbg(&port->dev, "DCR1 Writing success status%d\n", status);
2217 status = mos7840_set_reg_sync(port,
2218 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2220 dev_dbg(&port->dev, "Writing DCR2 failed status-0x%x\n", status);
2223 dev_dbg(&port->dev, "DCR2 Writing success status%d\n", status);
2225 /* write values in clkstart0x0 and clkmulti 0x20 */
2227 status = mos7840_set_reg_sync(port, CLK_START_VALUE_REGISTER, Data);
2229 dev_dbg(&port->dev, "Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2232 dev_dbg(&port->dev, "CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2235 status = mos7840_set_reg_sync(port, CLK_MULTI_REGISTER, Data);
2237 dev_dbg(&port->dev, "Writing CLK_MULTI_REGISTER failed status-0x%x\n", status);
2240 dev_dbg(&port->dev, "CLK_MULTI_REGISTER Writing success status%d\n", status);
2242 /* write value 0x0 to scratchpad register */
2244 status = mos7840_set_uart_reg(port, SCRATCH_PAD_REGISTER, Data);
2246 dev_dbg(&port->dev, "Writing SCRATCH_PAD_REGISTER failed status-0x%x\n", status);
2249 dev_dbg(&port->dev, "SCRATCH_PAD_REGISTER Writing success status%d\n", status);
2251 /* Zero Length flag register */
2252 if ((mos7840_port->port_num != 1) && (serial->num_ports == 2)) {
2254 status = mos7840_set_reg_sync(port,
2256 ((__u16)mos7840_port->port_num)), Data);
2257 dev_dbg(&port->dev, "ZLIP offset %x\n",
2258 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num)));
2260 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 2, status);
2263 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 2, status);
2266 status = mos7840_set_reg_sync(port,
2268 ((__u16)mos7840_port->port_num) - 0x1), Data);
2269 dev_dbg(&port->dev, "ZLIP offset %x\n",
2270 (__u16)(ZLP_REG1 + ((__u16) mos7840_port->port_num) - 0x1));
2272 dev_dbg(&port->dev, "Writing ZLP_REG%d failed status-0x%x\n", pnum + 1, status);
2275 dev_dbg(&port->dev, "ZLP_REG%d Writing success status%d\n", pnum + 1, status);
2278 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2279 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2280 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2282 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2283 !mos7840_port->dr) {
2288 mos7840_port->has_led = false;
2290 /* Initialize LED timers */
2291 if (device_type == MOSCHIP_DEVICE_ID_7810) {
2292 mos7840_port->has_led = true;
2294 mos7840_port->led_urb = usb_alloc_urb(0, GFP_KERNEL);
2295 mos7840_port->led_dr = kmalloc(sizeof(*mos7840_port->led_dr),
2297 if (!mos7840_port->led_urb || !mos7840_port->led_dr) {
2302 setup_timer(&mos7840_port->led_timer1, mos7840_led_off,
2303 (unsigned long)mos7840_port);
2304 mos7840_port->led_timer1.expires =
2305 jiffies + msecs_to_jiffies(LED_ON_MS);
2306 setup_timer(&mos7840_port->led_timer2, mos7840_led_flag_off,
2307 (unsigned long)mos7840_port);
2308 mos7840_port->led_timer2.expires =
2309 jiffies + msecs_to_jiffies(LED_OFF_MS);
2312 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2315 if (pnum == serial->num_ports - 1) {
2316 /* Zero Length flag enable */
2318 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2320 dev_dbg(&port->dev, "Writing ZLP_REG5 failed status-0x%x\n", status);
2323 dev_dbg(&port->dev, "ZLP_REG5 Writing success status%d\n", status);
2325 /* setting configuration feature to one */
2326 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2327 0x03, 0x00, 0x01, 0x00, NULL, 0x00,
2332 kfree(mos7840_port->led_dr);
2333 usb_free_urb(mos7840_port->led_urb);
2334 kfree(mos7840_port->dr);
2335 kfree(mos7840_port->ctrl_buf);
2336 usb_free_urb(mos7840_port->control_urb);
2337 kfree(mos7840_port);
2342 static int mos7840_port_remove(struct usb_serial_port *port)
2344 struct moschip_port *mos7840_port;
2346 mos7840_port = mos7840_get_port_private(port);
2348 if (mos7840_port->has_led) {
2350 mos7840_set_led_sync(port, MODEM_CONTROL_REGISTER, 0x0300);
2352 del_timer_sync(&mos7840_port->led_timer1);
2353 del_timer_sync(&mos7840_port->led_timer2);
2355 usb_kill_urb(mos7840_port->led_urb);
2356 usb_free_urb(mos7840_port->led_urb);
2357 kfree(mos7840_port->led_dr);
2359 usb_kill_urb(mos7840_port->control_urb);
2360 usb_free_urb(mos7840_port->control_urb);
2361 kfree(mos7840_port->ctrl_buf);
2362 kfree(mos7840_port->dr);
2363 kfree(mos7840_port);
2368 static struct usb_serial_driver moschip7840_4port_device = {
2370 .owner = THIS_MODULE,
2373 .description = DRIVER_DESC,
2374 .id_table = id_table,
2376 .open = mos7840_open,
2377 .close = mos7840_close,
2378 .write = mos7840_write,
2379 .write_room = mos7840_write_room,
2380 .chars_in_buffer = mos7840_chars_in_buffer,
2381 .throttle = mos7840_throttle,
2382 .unthrottle = mos7840_unthrottle,
2383 .calc_num_ports = mos7840_calc_num_ports,
2384 .probe = mos7840_probe,
2385 .ioctl = mos7840_ioctl,
2386 .set_termios = mos7840_set_termios,
2387 .break_ctl = mos7840_break,
2388 .tiocmget = mos7840_tiocmget,
2389 .tiocmset = mos7840_tiocmset,
2390 .tiocmiwait = usb_serial_generic_tiocmiwait,
2391 .get_icount = usb_serial_generic_get_icount,
2392 .attach = mos7840_attach,
2393 .port_probe = mos7840_port_probe,
2394 .port_remove = mos7840_port_remove,
2395 .read_bulk_callback = mos7840_bulk_in_callback,
2396 .read_int_callback = mos7840_interrupt_callback,
2399 static struct usb_serial_driver * const serial_drivers[] = {
2400 &moschip7840_4port_device, NULL
2403 module_usb_serial_driver(serial_drivers, id_table);
2405 MODULE_DESCRIPTION(DRIVER_DESC);
2406 MODULE_LICENSE("GPL");