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 <asm/uaccess.h>
41 #define DRIVER_VERSION "1.3.1"
42 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
45 * 16C50 UART register defines
48 #define LCR_BITS_5 0x00 /* 5 bits/char */
49 #define LCR_BITS_6 0x01 /* 6 bits/char */
50 #define LCR_BITS_7 0x02 /* 7 bits/char */
51 #define LCR_BITS_8 0x03 /* 8 bits/char */
52 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
54 #define LCR_STOP_1 0x00 /* 1 stop bit */
55 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
56 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
57 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
59 #define LCR_PAR_NONE 0x00 /* No parity */
60 #define LCR_PAR_ODD 0x08 /* Odd parity */
61 #define LCR_PAR_EVEN 0x18 /* Even parity */
62 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
63 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
64 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
66 #define LCR_SET_BREAK 0x40 /* Set Break condition */
67 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
69 #define MCR_DTR 0x01 /* Assert DTR */
70 #define MCR_RTS 0x02 /* Assert RTS */
71 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
72 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
73 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
74 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
76 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
77 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
78 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
79 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
82 * Defines used for sending commands to port
85 #define WAIT_FOR_EVER (HZ * 0 ) /* timeout urb is wait for ever */
86 #define MOS_WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
88 #define MOS_PORT1 0x0200
89 #define MOS_PORT2 0x0300
90 #define MOS_VENREG 0x0000
91 #define MOS_MAX_PORT 0x02
92 #define MOS_WRITE 0x0E
96 #define MCS_RD_RTYPE 0xC0
97 #define MCS_WR_RTYPE 0x40
98 #define MCS_RDREQ 0x0D
99 #define MCS_WRREQ 0x0E
100 #define MCS_CTRL_TIMEOUT 500
101 #define VENDOR_READ_LENGTH (0x01)
103 #define MAX_NAME_LEN 64
105 #define ZLP_REG1 0x3A //Zero_Flag_Reg1 58
106 #define ZLP_REG5 0x3E //Zero_Flag_Reg5 62
108 /* For higher baud Rates use TIOCEXBAUD */
109 #define TIOCEXBAUD 0x5462
111 /* vendor id and device id defines */
113 /* The native mos7840/7820 component */
114 #define USB_VENDOR_ID_MOSCHIP 0x9710
115 #define MOSCHIP_DEVICE_ID_7840 0x7840
116 #define MOSCHIP_DEVICE_ID_7820 0x7820
117 /* The native component can have its vendor/device id's overridden
118 * in vendor-specific implementations. Such devices can be handled
119 * by making a change here, in moschip_port_id_table, and in
120 * moschip_id_table_combined
122 #define USB_VENDOR_ID_BANDB 0x0856
123 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
124 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
126 /* Interrupt Routine Defines */
128 #define SERIAL_IIR_RLS 0x06
129 #define SERIAL_IIR_MS 0x00
132 * Emulation of the bit mask on the LINE STATUS REGISTER.
134 #define SERIAL_LSR_DR 0x0001
135 #define SERIAL_LSR_OE 0x0002
136 #define SERIAL_LSR_PE 0x0004
137 #define SERIAL_LSR_FE 0x0008
138 #define SERIAL_LSR_BI 0x0010
140 #define MOS_MSR_DELTA_CTS 0x10
141 #define MOS_MSR_DELTA_DSR 0x20
142 #define MOS_MSR_DELTA_RI 0x40
143 #define MOS_MSR_DELTA_CD 0x80
145 // Serial Port register Address
146 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
147 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
148 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
149 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
150 #define LINE_STATUS_REGISTER ((__u16)(0x05))
151 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
152 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
153 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
154 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
156 #define CLK_MULTI_REGISTER ((__u16)(0x02))
157 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
159 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
162 * URB POOL related defines
164 #define NUM_URBS 16 /* URB Count */
165 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
168 static struct usb_device_id moschip_port_id_table[] = {
169 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
170 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
171 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
172 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
173 {} /* terminating entry */
176 static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
177 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
179 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
180 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
181 {} /* terminating entry */
184 MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
186 /* This structure holds all of the local port information */
188 struct moschip_port {
189 int port_num; /*Actual port number in the device(1,2,etc) */
190 struct urb *write_urb; /* write URB for this port */
191 struct urb *read_urb; /* read URB for this port */
193 __u8 shadowLCR; /* last LCR value received */
194 __u8 shadowMCR; /* last MCR value received */
198 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
199 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
201 struct async_icount icount;
202 struct usb_serial_port *port; /* loop back to the owner of this object */
206 __u8 ControlRegOffset;
208 //for processing control URBS in interrupt context
209 struct urb *control_urb;
210 struct usb_ctrlrequest *dr;
214 spinlock_t pool_lock;
215 struct urb *write_urb_pool[NUM_URBS];
223 * mos7840_set_reg_sync
224 * To set the Control register by calling usb_fill_control_urb function
225 * by passing usb_sndctrlpipe function as parameter.
228 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
231 struct usb_device *dev = port->serial->dev;
233 dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
235 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
236 MCS_WR_RTYPE, val, reg, NULL, 0,
241 * mos7840_get_reg_sync
242 * To set the Uart register by calling usb_fill_control_urb function by
243 * passing usb_rcvctrlpipe function as parameter.
246 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
249 struct usb_device *dev = port->serial->dev;
252 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
253 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
255 dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
256 *val = (*val) & 0x00ff;
261 * mos7840_set_uart_reg
262 * To set the Uart register by calling usb_fill_control_urb function by
263 * passing usb_sndctrlpipe function as parameter.
266 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
270 struct usb_device *dev = port->serial->dev;
272 // For the UART control registers, the application number need to be Or'ed
273 if (port->serial->num_ports == 4) {
275 (((__u16) port->number - (__u16) (port->serial->minor)) +
277 dbg("mos7840_set_uart_reg application number is %x\n", val);
279 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
281 (((__u16) port->number -
282 (__u16) (port->serial->minor)) + 1) << 8;
283 dbg("mos7840_set_uart_reg application number is %x\n",
287 (((__u16) port->number -
288 (__u16) (port->serial->minor)) + 2) << 8;
289 dbg("mos7840_set_uart_reg application number is %x\n",
293 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
294 MCS_WR_RTYPE, val, reg, NULL, 0,
300 * mos7840_get_uart_reg
301 * To set the Control register by calling usb_fill_control_urb function
302 * by passing usb_rcvctrlpipe function as parameter.
304 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
307 struct usb_device *dev = port->serial->dev;
311 //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
312 /*Wval is same as application number */
313 if (port->serial->num_ports == 4) {
315 (((__u16) port->number - (__u16) (port->serial->minor)) +
317 dbg("mos7840_get_uart_reg application number is %x\n", Wval);
319 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
321 (((__u16) port->number -
322 (__u16) (port->serial->minor)) + 1) << 8;
323 dbg("mos7840_get_uart_reg application number is %x\n",
327 (((__u16) port->number -
328 (__u16) (port->serial->minor)) + 2) << 8;
329 dbg("mos7840_get_uart_reg application number is %x\n",
333 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
334 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
336 *val = (*val) & 0x00ff;
340 static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
343 dbg("***************************************\n");
344 dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
345 dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
346 dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
347 dbg("***************************************\n");
351 /************************************************************************/
352 /************************************************************************/
353 /* I N T E R F A C E F U N C T I O N S */
354 /* I N T E R F A C E F U N C T I O N S */
355 /************************************************************************/
356 /************************************************************************/
358 static inline void mos7840_set_port_private(struct usb_serial_port *port,
359 struct moschip_port *data)
361 usb_set_serial_port_data(port, (void *)data);
364 static inline struct moschip_port *mos7840_get_port_private(struct
368 return (struct moschip_port *)usb_get_serial_port_data(port);
371 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
373 struct moschip_port *mos7840_port;
374 struct async_icount *icount;
376 icount = &mos7840_port->icount;
378 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
380 icount = &mos7840_port->icount;
382 /* update input line counters */
383 if (new_msr & MOS_MSR_DELTA_CTS) {
387 if (new_msr & MOS_MSR_DELTA_DSR) {
391 if (new_msr & MOS_MSR_DELTA_CD) {
395 if (new_msr & MOS_MSR_DELTA_RI) {
402 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
404 struct async_icount *icount;
406 dbg("%s - %02x", __func__, new_lsr);
408 if (new_lsr & SERIAL_LSR_BI) {
410 // Parity and Framing errors only count if they
411 // occur exclusive of a break being
414 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
417 /* update input line counters */
418 icount = &port->icount;
419 if (new_lsr & SERIAL_LSR_BI) {
423 if (new_lsr & SERIAL_LSR_OE) {
427 if (new_lsr & SERIAL_LSR_PE) {
431 if (new_lsr & SERIAL_LSR_FE) {
437 /************************************************************************/
438 /************************************************************************/
439 /* U S B C A L L B A C K F U N C T I O N S */
440 /* U S B C A L L B A C K F U N C T I O N S */
441 /************************************************************************/
442 /************************************************************************/
444 static void mos7840_control_callback(struct urb *urb)
447 struct moschip_port *mos7840_port;
450 int status = urb->status;
452 mos7840_port = urb->context;
461 /* this urb is terminated, clean up */
462 dbg("%s - urb shutting down with status: %d", __func__,
466 dbg("%s - nonzero urb status received: %d", __func__,
471 dbg("%s urb buffer size is %d\n", __func__, urb->actual_length);
472 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __func__,
473 mos7840_port->MsrLsr, mos7840_port->port_num);
474 data = urb->transfer_buffer;
475 regval = (__u8) data[0];
476 dbg("%s data is %x\n", __func__, regval);
477 if (mos7840_port->MsrLsr == 0)
478 mos7840_handle_new_msr(mos7840_port, regval);
479 else if (mos7840_port->MsrLsr == 1)
480 mos7840_handle_new_lsr(mos7840_port, regval);
483 spin_lock(&mos7840_port->pool_lock);
484 if (!mos7840_port->zombie)
485 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
486 spin_unlock(&mos7840_port->pool_lock);
488 dev_err(&urb->dev->dev,
489 "%s - Error %d submitting interrupt urb\n",
494 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
497 struct usb_device *dev = mcs->port->serial->dev;
498 struct usb_ctrlrequest *dr = mcs->dr;
499 unsigned char *buffer = mcs->ctrl_buf;
502 dr->bRequestType = MCS_RD_RTYPE;
503 dr->bRequest = MCS_RDREQ;
504 dr->wValue = cpu_to_le16(Wval); //0;
505 dr->wIndex = cpu_to_le16(reg);
506 dr->wLength = cpu_to_le16(2);
508 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
509 (unsigned char *)dr, buffer, 2,
510 mos7840_control_callback, mcs);
511 mcs->control_urb->transfer_buffer_length = 2;
512 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
516 /*****************************************************************************
517 * mos7840_interrupt_callback
518 * this is the callback function for when we have received data on the
519 * interrupt endpoint.
520 *****************************************************************************/
522 static void mos7840_interrupt_callback(struct urb *urb)
526 struct moschip_port *mos7840_port;
527 struct usb_serial *serial;
532 __u16 wval, wreg = 0;
533 int status = urb->status;
535 dbg("%s", " : Entering\n");
544 /* this urb is terminated, clean up */
545 dbg("%s - urb shutting down with status: %d", __func__,
549 dbg("%s - nonzero urb status received: %d", __func__,
554 length = urb->actual_length;
555 data = urb->transfer_buffer;
557 serial = urb->context;
559 /* Moschip get 5 bytes
560 * Byte 1 IIR Port 1 (port.number is 0)
561 * Byte 2 IIR Port 2 (port.number is 1)
562 * Byte 3 IIR Port 3 (port.number is 2)
563 * Byte 4 IIR Port 4 (port.number is 3)
564 * Byte 5 FIFO status for both */
566 if (length && length > 5) {
567 dbg("%s \n", "Wrong data !!!");
571 sp[0] = (__u8) data[0];
572 sp[1] = (__u8) data[1];
573 sp[2] = (__u8) data[2];
574 sp[3] = (__u8) data[3];
577 for (i = 0; i < serial->num_ports; i++) {
578 mos7840_port = mos7840_get_port_private(serial->port[i]);
580 (((__u16) serial->port[i]->number -
581 (__u16) (serial->minor)) + 1) << 8;
582 if (mos7840_port->open) {
584 dbg("SP%d No Interrupt !!!\n", i);
586 switch (sp[i] & 0x0f) {
588 dbg("Serial Port %d: Receiver status error or ", i);
589 dbg("address bit detected in 9-bit mode\n");
590 mos7840_port->MsrLsr = 1;
591 wreg = LINE_STATUS_REGISTER;
594 dbg("Serial Port %d: Modem status change\n", i);
595 mos7840_port->MsrLsr = 0;
596 wreg = MODEM_STATUS_REGISTER;
599 spin_lock(&mos7840_port->pool_lock);
600 if (!mos7840_port->zombie) {
601 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
603 spin_unlock(&mos7840_port->pool_lock);
606 spin_unlock(&mos7840_port->pool_lock);
610 if (!(rv < 0)) /* the completion handler for the control urb will resubmit */
613 result = usb_submit_urb(urb, GFP_ATOMIC);
615 dev_err(&urb->dev->dev,
616 "%s - Error %d submitting interrupt urb\n",
621 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
622 const char *function)
625 dbg("%s - port == NULL", function);
629 dbg("%s - port->serial == NULL", function);
636 /* Inline functions to check the sanity of a pointer that is passed to us */
637 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
638 const char *function)
641 dbg("%s - serial == NULL", function);
645 dbg("%s - serial->type == NULL!", function);
652 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
653 const char *function)
655 /* if no port was specified, or it fails a paranoia check */
657 mos7840_port_paranoia_check(port, function) ||
658 mos7840_serial_paranoia_check(port->serial, function)) {
659 /* then say that we don't have a valid usb_serial thing, which will
660 * end up genrating -ENODEV return values */
667 /*****************************************************************************
668 * mos7840_bulk_in_callback
669 * this is the callback function for when we have received data on the
671 *****************************************************************************/
673 static void mos7840_bulk_in_callback(struct urb *urb)
677 struct usb_serial *serial;
678 struct usb_serial_port *port;
679 struct moschip_port *mos7840_port;
680 struct tty_struct *tty;
681 int status = urb->status;
684 dbg("nonzero read bulk status received: %d", status);
688 mos7840_port = urb->context;
690 dbg("%s", "NULL mos7840_port pointer \n");
694 port = (struct usb_serial_port *)mos7840_port->port;
695 if (mos7840_port_paranoia_check(port, __func__)) {
696 dbg("%s", "Port Paranoia failed \n");
700 serial = mos7840_get_usb_serial(port, __func__);
702 dbg("%s\n", "Bad serial pointer ");
706 dbg("%s\n", "Entering... \n");
708 data = urb->transfer_buffer;
710 dbg("%s", "Entering ........... \n");
712 if (urb->actual_length) {
713 tty = mos7840_port->port->port.tty;
715 tty_buffer_request_room(tty, urb->actual_length);
716 tty_insert_flip_string(tty, data, urb->actual_length);
718 tty_flip_buffer_push(tty);
720 mos7840_port->icount.rx += urb->actual_length;
722 dbg("mos7840_port->icount.rx is %d:\n",
723 mos7840_port->icount.rx);
726 if (!mos7840_port->read_urb) {
727 dbg("%s", "URB KILLED !!!\n");
732 mos7840_port->read_urb->dev = serial->dev;
734 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
737 dbg(" usb_submit_urb(read bulk) failed, retval = %d",
742 /*****************************************************************************
743 * mos7840_bulk_out_data_callback
744 * this is the callback function for when we have finished sending serial data
745 * on the bulk out endpoint.
746 *****************************************************************************/
748 static void mos7840_bulk_out_data_callback(struct urb *urb)
750 struct moschip_port *mos7840_port;
751 struct tty_struct *tty;
752 int status = urb->status;
755 mos7840_port = urb->context;
756 spin_lock(&mos7840_port->pool_lock);
757 for (i = 0; i < NUM_URBS; i++) {
758 if (urb == mos7840_port->write_urb_pool[i]) {
759 mos7840_port->busy[i] = 0;
763 spin_unlock(&mos7840_port->pool_lock);
766 dbg("nonzero write bulk status received:%d\n", status);
770 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
771 dbg("%s", "Port Paranoia failed \n");
775 dbg("%s \n", "Entering .........");
777 tty = mos7840_port->port->port.tty;
779 if (tty && mos7840_port->open)
784 /************************************************************************/
785 /* 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 */
786 /************************************************************************/
787 #ifdef MCSSerialProbe
788 static int mos7840_serial_probe(struct usb_serial *serial,
789 const struct usb_device_id *id)
792 /*need to implement the mode_reg reading and updating\
793 structures usb_serial_ device_type\
794 (i.e num_ports, num_bulkin,bulkout etc) */
795 /* Also we can update the changes attach */
800 /*****************************************************************************
802 * this function is called by the tty driver when a port is opened
803 * If successful, we return 0
804 * Otherwise we return a negative error number.
805 *****************************************************************************/
807 static int mos7840_open(struct tty_struct *tty,
808 struct usb_serial_port *port, struct file *filp)
812 struct usb_serial *serial;
816 struct moschip_port *mos7840_port;
817 struct moschip_port *port0;
819 if (mos7840_port_paranoia_check(port, __func__)) {
820 dbg("%s", "Port Paranoia failed \n");
824 serial = port->serial;
826 if (mos7840_serial_paranoia_check(serial, __func__)) {
827 dbg("%s", "Serial Paranoia failed \n");
831 mos7840_port = mos7840_get_port_private(port);
832 port0 = mos7840_get_port_private(serial->port[0]);
834 if (mos7840_port == NULL || port0 == NULL)
837 usb_clear_halt(serial->dev, port->write_urb->pipe);
838 usb_clear_halt(serial->dev, port->read_urb->pipe);
841 /* Initialising the write urb pool */
842 for (j = 0; j < NUM_URBS; ++j) {
843 urb = usb_alloc_urb(0, GFP_KERNEL);
844 mos7840_port->write_urb_pool[j] = urb;
847 err("No more urbs???");
851 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
852 if (!urb->transfer_buffer) {
854 mos7840_port->write_urb_pool[j] = NULL;
855 err("%s-out of memory for urb buffers.", __func__);
860 /*****************************************************************************
861 * Initialize MCS7840 -- Write Init values to corresponding Registers
869 * 0x08 : SP1/2 Control Reg
870 *****************************************************************************/
872 //NEED to check the following Block
876 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
878 dbg("Reading Spreg failed\n");
882 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
884 dbg("writing Spreg failed\n");
889 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
891 dbg("writing Spreg failed\n");
894 //End of block to be checked
899 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
901 dbg("Reading Controlreg failed\n");
904 Data |= 0x08; //Driver done bit
905 Data |= 0x20; //rx_disable
906 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
908 dbg("writing Controlreg failed\n");
911 //do register settings here
912 // Set all regs to the device default values.
913 ////////////////////////////////////
914 // First Disable all interrupts.
915 ////////////////////////////////////
918 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
920 dbg("disableing interrupts failed\n");
923 // Set FIFO_CONTROL_REGISTER to the default value
925 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
927 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
932 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
934 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
939 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
940 mos7840_port->shadowLCR = Data;
943 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
944 mos7840_port->shadowMCR = Data;
947 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
948 mos7840_port->shadowLCR = Data;
950 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
951 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
955 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
959 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
963 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
965 Data = Data & ~SERIAL_LCR_DLAB;
967 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
968 mos7840_port->shadowLCR = Data;
970 //clearing Bulkin and Bulkout Fifo
973 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
977 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
981 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
982 //Finally enable all interrupts
986 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
988 //clearing rx_disable
992 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
996 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1002 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
1006 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1008 /* force low_latency on so that our tty_push actually forces *
1009 * the data through,otherwise it is scheduled, and with *
1010 * high data rates (like with OHCI) data can get lost. */
1013 tty->low_latency = 1;
1014 /* Check to see if we've set up our endpoint info yet *
1015 * (can't set it up in mos7840_startup as the structures *
1016 * were not set up at that time.) */
1017 if (port0->open_ports == 1) {
1018 if (serial->port[0]->interrupt_in_buffer == NULL) {
1020 /* set up interrupt urb */
1022 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1024 usb_rcvintpipe(serial->dev,
1026 interrupt_in_endpointAddress),
1027 serial->port[0]->interrupt_in_buffer,
1028 serial->port[0]->interrupt_in_urb->
1029 transfer_buffer_length,
1030 mos7840_interrupt_callback,
1032 serial->port[0]->interrupt_in_urb->
1035 /* start interrupt read for mos7840 *
1036 * will continue as long as mos7840 is connected */
1039 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1042 err("%s - Error %d submitting interrupt urb",
1043 __func__, response);
1050 /* see if we've set up our endpoint info yet *
1051 * (can't set it up in mos7840_startup as the *
1052 * structures were not set up at that time.) */
1054 dbg("port number is %d \n", port->number);
1055 dbg("serial number is %d \n", port->serial->minor);
1056 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1057 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1058 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
1059 dbg("port's number in the device is %d\n", mos7840_port->port_num);
1060 mos7840_port->read_urb = port->read_urb;
1062 /* set up our bulk in urb */
1064 usb_fill_bulk_urb(mos7840_port->read_urb,
1066 usb_rcvbulkpipe(serial->dev,
1067 port->bulk_in_endpointAddress),
1068 port->bulk_in_buffer,
1069 mos7840_port->read_urb->transfer_buffer_length,
1070 mos7840_bulk_in_callback, mos7840_port);
1072 dbg("mos7840_open: bulkin endpoint is %d\n",
1073 port->bulk_in_endpointAddress);
1074 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1076 err("%s - Error %d submitting control urb", __func__,
1080 /* initialize our wait queues */
1081 init_waitqueue_head(&mos7840_port->wait_chase);
1082 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1084 /* initialize our icount structure */
1085 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1087 /* initialize our port settings */
1088 mos7840_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
1089 /* send a open port command */
1090 mos7840_port->open = 1;
1091 //mos7840_change_port_settings(mos7840_port,old_termios);
1092 mos7840_port->icount.tx = 0;
1093 mos7840_port->icount.rx = 0;
1095 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial, mos7840_port, port);
1101 /*****************************************************************************
1102 * mos7840_chars_in_buffer
1103 * this function is called by the tty driver when it wants to know how many
1104 * bytes of data we currently have outstanding in the port (data that has
1105 * been written, but hasn't made it out the port yet)
1106 * If successful, we return the number of bytes left to be written in the
1108 * Otherwise we return zero.
1109 *****************************************************************************/
1111 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1113 struct usb_serial_port *port = tty->driver_data;
1116 unsigned long flags;
1117 struct moschip_port *mos7840_port;
1119 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
1121 if (mos7840_port_paranoia_check(port, __func__)) {
1122 dbg("%s", "Invalid port \n");
1126 mos7840_port = mos7840_get_port_private(port);
1127 if (mos7840_port == NULL) {
1128 dbg("%s \n", "mos7840_break:leaving ...........");
1132 spin_lock_irqsave(&mos7840_port->pool_lock,flags);
1133 for (i = 0; i < NUM_URBS; ++i)
1134 if (mos7840_port->busy[i])
1135 chars += URB_TRANSFER_BUFFER_SIZE;
1136 spin_unlock_irqrestore(&mos7840_port->pool_lock,flags);
1137 dbg("%s - returns %d", __func__, chars);
1142 /************************************************************************
1144 * mos7840_block_until_tx_empty
1146 * This function will block the close until one of the following:
1148 * 2. The mos7840 has stopped
1149 * 3. A timeout of 3 seconds without activity has expired
1151 ************************************************************************/
1152 static void mos7840_block_until_tx_empty(struct tty_struct *tty,
1153 struct moschip_port *mos7840_port)
1155 int timeout = HZ / 10;
1161 count = mos7840_chars_in_buffer(tty);
1163 /* Check for Buffer status */
1168 /* Block the thread for a while */
1169 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1172 /* No activity.. count down section */
1175 dbg("%s - TIMEOUT", __func__);
1178 /* Reset timeout value back to seconds */
1184 /*****************************************************************************
1186 * this function is called by the tty driver when a port is closed
1187 *****************************************************************************/
1189 static void mos7840_close(struct tty_struct *tty,
1190 struct usb_serial_port *port, struct file *filp)
1192 struct usb_serial *serial;
1193 struct moschip_port *mos7840_port;
1194 struct moschip_port *port0;
1198 dbg("%s\n", "mos7840_close:entering...");
1200 if (mos7840_port_paranoia_check(port, __func__)) {
1201 dbg("%s", "Port Paranoia failed \n");
1205 serial = mos7840_get_usb_serial(port, __func__);
1207 dbg("%s", "Serial Paranoia failed \n");
1211 mos7840_port = mos7840_get_port_private(port);
1212 port0 = mos7840_get_port_private(serial->port[0]);
1214 if (mos7840_port == NULL || port0 == NULL)
1217 for (j = 0; j < NUM_URBS; ++j)
1218 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1220 /* Freeing Write URBs */
1221 for (j = 0; j < NUM_URBS; ++j) {
1222 if (mos7840_port->write_urb_pool[j]) {
1223 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1224 kfree(mos7840_port->write_urb_pool[j]->
1227 usb_free_urb(mos7840_port->write_urb_pool[j]);
1232 /* flush and block until tx is empty */
1233 mos7840_block_until_tx_empty(tty, mos7840_port);
1235 /* While closing port, shutdown all bulk read, write *
1236 * and interrupt read if they exists */
1238 if (mos7840_port->write_urb) {
1239 dbg("%s", "Shutdown bulk write\n");
1240 usb_kill_urb(mos7840_port->write_urb);
1242 if (mos7840_port->read_urb) {
1243 dbg("%s", "Shutdown bulk read\n");
1244 usb_kill_urb(mos7840_port->read_urb);
1246 if ((&mos7840_port->control_urb)) {
1247 dbg("%s", "Shutdown control read\n");
1248 // usb_kill_urb (mos7840_port->control_urb);
1251 // if(mos7840_port->ctrl_buf != NULL)
1252 // kfree(mos7840_port->ctrl_buf);
1253 port0->open_ports--;
1254 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1255 port0->open_ports, port->number);
1256 if (port0->open_ports == 0) {
1257 if (serial->port[0]->interrupt_in_urb) {
1258 dbg("%s", "Shutdown interrupt_in_urb\n");
1259 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1263 if (mos7840_port->write_urb) {
1264 /* if this urb had a transfer buffer already (old tx) free it */
1266 if (mos7840_port->write_urb->transfer_buffer != NULL) {
1267 kfree(mos7840_port->write_urb->transfer_buffer);
1269 usb_free_urb(mos7840_port->write_urb);
1273 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1276 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1278 mos7840_port->open = 0;
1280 dbg("%s \n", "Leaving ............");
1283 /************************************************************************
1285 * mos7840_block_until_chase_response
1287 * This function will block the close until one of the following:
1288 * 1. Response to our Chase comes from mos7840
1289 * 2. A timeout of 10 seconds without activity has expired
1290 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1292 ************************************************************************/
1294 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1295 struct moschip_port *mos7840_port)
1297 int timeout = 1 * HZ;
1302 count = mos7840_chars_in_buffer(tty);
1304 /* Check for Buffer status */
1309 /* Block the thread for a while */
1310 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1312 /* No activity.. count down section */
1315 dbg("%s - TIMEOUT", __func__);
1318 /* Reset timeout value back to seconds */
1325 /*****************************************************************************
1327 * this function sends a break to the port
1328 *****************************************************************************/
1329 static void mos7840_break(struct tty_struct *tty, int break_state)
1331 struct usb_serial_port *port = tty->driver_data;
1333 struct usb_serial *serial;
1334 struct moschip_port *mos7840_port;
1336 dbg("%s \n", "Entering ...........");
1337 dbg("mos7840_break: Start\n");
1339 if (mos7840_port_paranoia_check(port, __func__)) {
1340 dbg("%s", "Port Paranoia failed \n");
1344 serial = mos7840_get_usb_serial(port, __func__);
1346 dbg("%s", "Serial Paranoia failed \n");
1350 mos7840_port = mos7840_get_port_private(port);
1352 if (mos7840_port == NULL) {
1357 /* flush and block until tx is empty */
1358 mos7840_block_until_chase_response(tty, mos7840_port);
1360 if (break_state == -1)
1361 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1363 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1365 mos7840_port->shadowLCR = data;
1366 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1367 mos7840_port->shadowLCR);
1368 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1369 mos7840_port->shadowLCR);
1374 /*****************************************************************************
1375 * mos7840_write_room
1376 * this function is called by the tty driver when it wants to know how many
1377 * bytes of data we can accept for a specific port.
1378 * If successful, we return the amount of room that we have for this port
1379 * Otherwise we return a negative error number.
1380 *****************************************************************************/
1382 static int mos7840_write_room(struct tty_struct *tty)
1384 struct usb_serial_port *port = tty->driver_data;
1387 unsigned long flags;
1388 struct moschip_port *mos7840_port;
1390 dbg("%s \n", " mos7840_write_room:entering ...........");
1392 if (mos7840_port_paranoia_check(port, __func__)) {
1393 dbg("%s", "Invalid port \n");
1394 dbg("%s \n", " mos7840_write_room:leaving ...........");
1398 mos7840_port = mos7840_get_port_private(port);
1399 if (mos7840_port == NULL) {
1400 dbg("%s \n", "mos7840_break:leaving ...........");
1404 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1405 for (i = 0; i < NUM_URBS; ++i) {
1406 if (!mos7840_port->busy[i]) {
1407 room += URB_TRANSFER_BUFFER_SIZE;
1410 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1412 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1413 dbg("%s - returns %d", __func__, room);
1418 /*****************************************************************************
1420 * this function is called by the tty driver when data should be written to
1422 * If successful, we return the number of bytes written, otherwise we
1423 * return a negative error number.
1424 *****************************************************************************/
1426 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1427 const unsigned char *data, int count)
1433 unsigned long flags;
1435 struct moschip_port *mos7840_port;
1436 struct usb_serial *serial;
1439 const unsigned char *current_position = data;
1440 unsigned char *data1;
1441 dbg("%s \n", "entering ...........");
1442 //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
1447 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1448 mos7840_port->shadowLCR = Data;
1449 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
1450 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1451 mos7840_port->shadowLCR);
1454 //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
1455 //mos7840_port->shadowLCR=Data;//Need to add later
1457 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1459 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1462 //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
1465 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1466 dbg("mos7840_write:DLL value is %x\n", Data);
1470 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1471 dbg("mos7840_write:DLM value is %x\n", Data);
1473 Data = Data & ~SERIAL_LCR_DLAB;
1474 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1475 mos7840_port->shadowLCR);
1477 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1480 if (mos7840_port_paranoia_check(port, __func__)) {
1481 dbg("%s", "Port Paranoia failed \n");
1485 serial = port->serial;
1486 if (mos7840_serial_paranoia_check(serial, __func__)) {
1487 dbg("%s", "Serial Paranoia failed \n");
1491 mos7840_port = mos7840_get_port_private(port);
1492 if (mos7840_port == NULL) {
1493 dbg("%s", "mos7840_port is NULL\n");
1497 /* try to find a free urb in the list */
1500 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1501 for (i = 0; i < NUM_URBS; ++i) {
1502 if (!mos7840_port->busy[i]) {
1503 mos7840_port->busy[i] = 1;
1504 urb = mos7840_port->write_urb_pool[i];
1509 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1512 dbg("%s - no more free urbs", __func__);
1516 if (urb->transfer_buffer == NULL) {
1517 urb->transfer_buffer =
1518 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1520 if (urb->transfer_buffer == NULL) {
1521 err("%s no more kernel memory...", __func__);
1525 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1527 memcpy(urb->transfer_buffer, current_position, transfer_size);
1529 /* fill urb with data and submit */
1530 usb_fill_bulk_urb(urb,
1532 usb_sndbulkpipe(serial->dev,
1533 port->bulk_out_endpointAddress),
1534 urb->transfer_buffer,
1536 mos7840_bulk_out_data_callback, mos7840_port);
1538 data1 = urb->transfer_buffer;
1539 dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1541 /* send it down the pipe */
1542 status = usb_submit_urb(urb, GFP_ATOMIC);
1545 mos7840_port->busy[i] = 0;
1546 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1548 bytes_sent = status;
1551 bytes_sent = transfer_size;
1552 mos7840_port->icount.tx += transfer_size;
1554 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1560 /*****************************************************************************
1562 * this function is called by the tty driver when it wants to stop the data
1563 * being read from the port.
1564 *****************************************************************************/
1566 static void mos7840_throttle(struct tty_struct *tty)
1568 struct usb_serial_port *port = tty->driver_data;
1569 struct moschip_port *mos7840_port;
1572 if (mos7840_port_paranoia_check(port, __func__)) {
1573 dbg("%s", "Invalid port \n");
1577 dbg("- port %d\n", port->number);
1579 mos7840_port = mos7840_get_port_private(port);
1581 if (mos7840_port == NULL)
1584 if (!mos7840_port->open) {
1585 dbg("%s\n", "port not opened");
1589 dbg("%s", "Entering .......... \n");
1591 /* if we are implementing XON/XOFF, send the stop character */
1593 unsigned char stop_char = STOP_CHAR(tty);
1594 status = mos7840_write(tty, port, &stop_char, 1);
1598 /* if we are implementing RTS/CTS, toggle that line */
1599 if (tty->termios->c_cflag & CRTSCTS) {
1600 mos7840_port->shadowMCR &= ~MCR_RTS;
1602 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1603 mos7840_port->shadowMCR);
1611 /*****************************************************************************
1612 * mos7840_unthrottle
1613 * this function is called by the tty driver when it wants to resume the data
1614 * being read from the port (called after SerialThrottle is called)
1615 *****************************************************************************/
1616 static void mos7840_unthrottle(struct tty_struct *tty)
1618 struct usb_serial_port *port = tty->driver_data;
1620 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1622 if (mos7840_port_paranoia_check(port, __func__)) {
1623 dbg("%s", "Invalid port \n");
1627 if (mos7840_port == NULL)
1630 if (!mos7840_port->open) {
1631 dbg("%s - port not opened", __func__);
1635 dbg("%s", "Entering .......... \n");
1637 /* if we are implementing XON/XOFF, send the start character */
1639 unsigned char start_char = START_CHAR(tty);
1640 status = mos7840_write(tty, port, &start_char, 1);
1645 /* if we are implementing RTS/CTS, toggle that line */
1646 if (tty->termios->c_cflag & CRTSCTS) {
1647 mos7840_port->shadowMCR |= MCR_RTS;
1649 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1650 mos7840_port->shadowMCR);
1656 static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
1658 struct usb_serial_port *port = tty->driver_data;
1659 struct moschip_port *mos7840_port;
1660 unsigned int result;
1664 mos7840_port = mos7840_get_port_private(port);
1666 dbg("%s - port %d", __func__, port->number);
1668 if (mos7840_port == NULL)
1671 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1672 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1673 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1674 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1675 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1676 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1677 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1678 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1679 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1681 dbg("%s - 0x%04X", __func__, result);
1686 static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
1687 unsigned int set, unsigned int clear)
1689 struct usb_serial_port *port = tty->driver_data;
1690 struct moschip_port *mos7840_port;
1694 dbg("%s - port %d", __func__, port->number);
1696 mos7840_port = mos7840_get_port_private(port);
1698 if (mos7840_port == NULL)
1701 /* FIXME: What locks the port registers ? */
1702 mcr = mos7840_port->shadowMCR;
1703 if (clear & TIOCM_RTS)
1705 if (clear & TIOCM_DTR)
1707 if (clear & TIOCM_LOOP)
1708 mcr &= ~MCR_LOOPBACK;
1710 if (set & TIOCM_RTS)
1712 if (set & TIOCM_DTR)
1714 if (set & TIOCM_LOOP)
1715 mcr |= MCR_LOOPBACK;
1717 mos7840_port->shadowMCR = mcr;
1719 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1721 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
1728 /*****************************************************************************
1729 * mos7840_calc_baud_rate_divisor
1730 * this function calculates the proper baud rate divisor for the specified
1732 *****************************************************************************/
1733 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1734 __u16 * clk_sel_val)
1737 dbg("%s - %d", __func__, baudRate);
1739 if (baudRate <= 115200) {
1740 *divisor = 115200 / baudRate;
1743 if ((baudRate > 115200) && (baudRate <= 230400)) {
1744 *divisor = 230400 / baudRate;
1745 *clk_sel_val = 0x10;
1746 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1747 *divisor = 403200 / baudRate;
1748 *clk_sel_val = 0x20;
1749 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1750 *divisor = 460800 / baudRate;
1751 *clk_sel_val = 0x30;
1752 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1753 *divisor = 806400 / baudRate;
1754 *clk_sel_val = 0x40;
1755 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1756 *divisor = 921600 / baudRate;
1757 *clk_sel_val = 0x50;
1758 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1759 *divisor = 1572864 / baudRate;
1760 *clk_sel_val = 0x60;
1761 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1762 *divisor = 3145728 / baudRate;
1763 *clk_sel_val = 0x70;
1769 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1770 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1771 *divisor = mos7840_divisor_table[i].Divisor;
1776 /* After trying for all the standard baud rates *
1777 * Try calculating the divisor for this baud rate */
1779 if (baudrate > 75 && baudrate < 230400) {
1780 /* get the divisor */
1781 custom = (__u16) (230400L / baudrate);
1783 /* Check for round off */
1784 round1 = (__u16) (2304000L / baudrate);
1785 round = (__u16) (round1 - (custom * 10));
1791 dbg(" Baud %d = %d\n", baudrate, custom);
1795 dbg("%s\n", " Baud calculation Failed...");
1800 /*****************************************************************************
1801 * mos7840_send_cmd_write_baud_rate
1802 * this function sends the proper command to change the baud rate of the
1804 *****************************************************************************/
1806 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1812 unsigned char number;
1814 struct usb_serial_port *port;
1816 if (mos7840_port == NULL)
1819 port = (struct usb_serial_port *)mos7840_port->port;
1820 if (mos7840_port_paranoia_check(port, __func__)) {
1821 dbg("%s", "Invalid port \n");
1825 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1826 dbg("%s", "Invalid Serial \n");
1830 dbg("%s", "Entering .......... \n");
1832 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1834 dbg("%s - port = %d, baud = %d", __func__,
1835 mos7840_port->port->number, baudRate);
1836 //reset clk_uart_sel in spregOffset
1837 if (baudRate > 115200) {
1838 #ifdef HW_flow_control
1839 //NOTE: need to see the pther register to modify
1840 //setting h/w flow control bit to 1;
1843 mos7840_port->shadowMCR = Data;
1845 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1847 dbg("Writing spreg failed in set_serial_baud\n");
1853 #ifdef HW_flow_control
1854 //setting h/w flow control bit to 0;
1857 mos7840_port->shadowMCR = Data;
1859 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1861 dbg("Writing spreg failed in set_serial_baud\n");
1868 if (1) //baudRate <= 115200)
1874 mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1877 mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1880 dbg("reading spreg failed in set_serial_baud\n");
1883 Data = (Data & 0x8f) | clk_sel_val;
1886 mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1888 dbg("Writing spreg failed in set_serial_baud\n");
1891 /* Calculate the Divisor */
1894 err("%s - bad baud rate", __func__);
1895 dbg("%s\n", "bad baud rate");
1898 /* Enable access to divisor latch */
1899 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1900 mos7840_port->shadowLCR = Data;
1901 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1903 /* Write the divisor */
1904 Data = (unsigned char)(divisor & 0xff);
1905 dbg("set_serial_baud Value to write DLL is %x\n", Data);
1906 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1908 Data = (unsigned char)((divisor & 0xff00) >> 8);
1909 dbg("set_serial_baud Value to write DLM is %x\n", Data);
1910 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1912 /* Disable access to divisor latch */
1913 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1914 mos7840_port->shadowLCR = Data;
1915 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1922 /*****************************************************************************
1923 * mos7840_change_port_settings
1924 * This routine is called to set the UART on the device to match
1925 * the specified new settings.
1926 *****************************************************************************/
1928 static void mos7840_change_port_settings(struct tty_struct *tty,
1929 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1939 struct usb_serial_port *port;
1940 struct usb_serial *serial;
1942 if (mos7840_port == NULL)
1945 port = (struct usb_serial_port *)mos7840_port->port;
1947 if (mos7840_port_paranoia_check(port, __func__)) {
1948 dbg("%s", "Invalid port \n");
1952 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1953 dbg("%s", "Invalid Serial \n");
1957 serial = port->serial;
1959 dbg("%s - port %d", __func__, mos7840_port->port->number);
1961 if (!mos7840_port->open) {
1962 dbg("%s - port not opened", __func__);
1966 dbg("%s", "Entering .......... \n");
1970 lParity = LCR_PAR_NONE;
1972 cflag = tty->termios->c_cflag;
1973 iflag = tty->termios->c_iflag;
1975 /* Change the number of bits */
1976 if (cflag & CSIZE) {
1977 switch (cflag & CSIZE) {
1995 /* Change the Parity bit */
1996 if (cflag & PARENB) {
1997 if (cflag & PARODD) {
1998 lParity = LCR_PAR_ODD;
1999 dbg("%s - parity = odd", __func__);
2001 lParity = LCR_PAR_EVEN;
2002 dbg("%s - parity = even", __func__);
2006 dbg("%s - parity = none", __func__);
2009 if (cflag & CMSPAR) {
2010 lParity = lParity | 0x20;
2013 /* Change the Stop bit */
2014 if (cflag & CSTOPB) {
2016 dbg("%s - stop bits = 2", __func__);
2019 dbg("%s - stop bits = 1", __func__);
2022 /* Update the LCR with the correct value */
2023 mos7840_port->shadowLCR &=
2024 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2025 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2027 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2028 mos7840_port->shadowLCR);
2029 /* Disable Interrupts */
2031 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2034 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2037 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2039 /* Send the updated LCR value to the mos7840 */
2040 Data = mos7840_port->shadowLCR;
2042 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2045 mos7840_port->shadowMCR = Data;
2046 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2048 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2050 /* set up the MCR register and send it to the mos7840 */
2052 mos7840_port->shadowMCR = MCR_MASTER_IE;
2053 if (cflag & CBAUD) {
2054 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2057 if (cflag & CRTSCTS) {
2058 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2061 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2064 Data = mos7840_port->shadowMCR;
2065 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2067 /* Determine divisor based on baud rate */
2068 baud = tty_get_baud_rate(tty);
2071 /* pick a default, any default... */
2072 dbg("%s\n", "Picked default baud...");
2076 dbg("%s - baud rate = %d", __func__, baud);
2077 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2079 /* Enable Interrupts */
2081 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2083 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2084 mos7840_port->read_urb->dev = serial->dev;
2086 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2089 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2093 wake_up(&mos7840_port->delta_msr_wait);
2094 mos7840_port->delta_msr_cond = 1;
2095 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2096 mos7840_port->shadowLCR);
2101 /*****************************************************************************
2102 * mos7840_set_termios
2103 * this function is called by the tty driver when it wants to change
2104 * the termios structure
2105 *****************************************************************************/
2107 static void mos7840_set_termios(struct tty_struct *tty,
2108 struct usb_serial_port *port,
2109 struct ktermios *old_termios)
2113 struct usb_serial *serial;
2114 struct moschip_port *mos7840_port;
2115 dbg("mos7840_set_termios: START\n");
2116 if (mos7840_port_paranoia_check(port, __func__)) {
2117 dbg("%s", "Invalid port \n");
2121 serial = port->serial;
2123 if (mos7840_serial_paranoia_check(serial, __func__)) {
2124 dbg("%s", "Invalid Serial \n");
2128 mos7840_port = mos7840_get_port_private(port);
2130 if (mos7840_port == NULL)
2133 if (!mos7840_port->open) {
2134 dbg("%s - port not opened", __func__);
2138 dbg("%s\n", "setting termios - ");
2140 cflag = tty->termios->c_cflag;
2142 dbg("%s - clfag %08x iflag %08x", __func__,
2143 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2144 dbg("%s - old clfag %08x old iflag %08x", __func__,
2145 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2146 dbg("%s - port %d", __func__, port->number);
2148 /* change the port settings to the new ones specified */
2150 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2152 if (!mos7840_port->read_urb) {
2153 dbg("%s", "URB KILLED !!!!!\n");
2157 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2158 mos7840_port->read_urb->dev = serial->dev;
2159 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2161 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2168 /*****************************************************************************
2169 * mos7840_get_lsr_info - get line status register info
2171 * Purpose: Let user call ioctl() to get info when the UART physically
2172 * is emptied. On bus types like RS485, the transmitter must
2173 * release the bus after transmitting. This must be done when
2174 * the transmit shift register is empty, not be done when the
2175 * transmit holding register is empty. This functionality
2176 * allows an RS485 driver to be written in user space.
2177 *****************************************************************************/
2179 static int mos7840_get_lsr_info(struct tty_struct *tty,
2180 unsigned int __user *value)
2183 unsigned int result = 0;
2185 count = mos7840_chars_in_buffer(tty);
2187 dbg("%s -- Empty", __func__);
2188 result = TIOCSER_TEMT;
2191 if (copy_to_user(value, &result, sizeof(int)))
2196 /*****************************************************************************
2197 * mos7840_set_modem_info
2198 * function to set modem info
2199 *****************************************************************************/
2201 /* FIXME: Should be using the model control hooks */
2203 static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2204 unsigned int cmd, unsigned int __user *value)
2210 struct usb_serial_port *port;
2212 if (mos7840_port == NULL)
2215 port = (struct usb_serial_port *)mos7840_port->port;
2216 if (mos7840_port_paranoia_check(port, __func__)) {
2217 dbg("%s", "Invalid port \n");
2221 mcr = mos7840_port->shadowMCR;
2223 if (copy_from_user(&arg, value, sizeof(int)))
2228 if (arg & TIOCM_RTS)
2230 if (arg & TIOCM_DTR)
2232 if (arg & TIOCM_LOOP)
2233 mcr |= MCR_LOOPBACK;
2237 if (arg & TIOCM_RTS)
2239 if (arg & TIOCM_DTR)
2241 if (arg & TIOCM_LOOP)
2242 mcr &= ~MCR_LOOPBACK;
2246 /* turn off the RTS and DTR and LOOPBACK
2247 * and then only turn on what was asked to */
2248 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2249 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2250 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2251 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2255 mos7840_port->shadowMCR = mcr;
2257 Data = mos7840_port->shadowMCR;
2259 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2261 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2268 /*****************************************************************************
2269 * mos7840_get_modem_info
2270 * function to get modem info
2271 *****************************************************************************/
2273 static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
2274 unsigned int __user *value)
2276 unsigned int result = 0;
2278 unsigned int mcr = mos7840_port->shadowMCR;
2280 status = mos7840_get_uart_reg(mos7840_port->port,
2281 MODEM_STATUS_REGISTER, &msr);
2282 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2283 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2284 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2285 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2286 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2287 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2289 dbg("%s -- %x", __func__, result);
2291 if (copy_to_user(value, &result, sizeof(int)))
2296 /*****************************************************************************
2297 * mos7840_get_serial_info
2298 * function to get information about serial port
2299 *****************************************************************************/
2301 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2302 struct serial_struct __user *retinfo)
2304 struct serial_struct tmp;
2306 if (mos7840_port == NULL)
2312 memset(&tmp, 0, sizeof(tmp));
2314 tmp.type = PORT_16550A;
2315 tmp.line = mos7840_port->port->serial->minor;
2316 tmp.port = mos7840_port->port->number;
2318 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2319 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2320 tmp.baud_base = 9600;
2321 tmp.close_delay = 5 * HZ;
2322 tmp.closing_wait = 30 * HZ;
2324 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2329 /*****************************************************************************
2331 * this function handles any ioctl calls to the driver
2332 *****************************************************************************/
2334 static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
2335 unsigned int cmd, unsigned long arg)
2337 struct usb_serial_port *port = tty->driver_data;
2338 void __user *argp = (void __user *)arg;
2339 struct moschip_port *mos7840_port;
2341 struct async_icount cnow;
2342 struct async_icount cprev;
2343 struct serial_icounter_struct icount;
2346 if (mos7840_port_paranoia_check(port, __func__)) {
2347 dbg("%s", "Invalid port \n");
2351 mos7840_port = mos7840_get_port_private(port);
2353 if (mos7840_port == NULL)
2356 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2359 /* return number of bytes available */
2362 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2363 return mos7840_get_lsr_info(tty, argp);
2366 /* FIXME: use the modem hooks and remove this */
2370 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
2373 mos7840_set_modem_info(mos7840_port, cmd, argp);
2377 dbg("%s (%d) TIOCMGET", __func__, port->number);
2378 return mos7840_get_modem_info(mos7840_port, argp);
2381 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
2382 return mos7840_get_serial_info(mos7840_port, argp);
2385 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
2389 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
2390 cprev = mos7840_port->icount;
2392 //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
2393 mos7840_port->delta_msr_cond = 0;
2394 wait_event_interruptible(mos7840_port->delta_msr_wait,
2396 delta_msr_cond == 1));
2398 /* see if a signal did it */
2399 if (signal_pending(current))
2400 return -ERESTARTSYS;
2401 cnow = mos7840_port->icount;
2403 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2404 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2405 return -EIO; /* no change => error */
2406 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2407 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2408 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2409 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2418 cnow = mos7840_port->icount;
2420 icount.cts = cnow.cts;
2421 icount.dsr = cnow.dsr;
2422 icount.rng = cnow.rng;
2423 icount.dcd = cnow.dcd;
2424 icount.rx = cnow.rx;
2425 icount.tx = cnow.tx;
2426 icount.frame = cnow.frame;
2427 icount.overrun = cnow.overrun;
2428 icount.parity = cnow.parity;
2429 icount.brk = cnow.brk;
2430 icount.buf_overrun = cnow.buf_overrun;
2432 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2433 port->number, icount.rx, icount.tx);
2434 if (copy_to_user(argp, &icount, sizeof(icount)))
2440 return -ENOIOCTLCMD;
2443 static int mos7840_calc_num_ports(struct usb_serial *serial)
2445 int mos7840_num_ports = 0;
2447 dbg("numberofendpoints: %d \n",
2448 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
2449 dbg("numberofendpoints: %d \n",
2450 (int)serial->interface->altsetting->desc.bNumEndpoints);
2451 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2452 mos7840_num_ports = serial->num_ports = 2;
2453 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
2454 serial->num_bulk_in = 4;
2455 serial->num_bulk_out = 4;
2456 mos7840_num_ports = serial->num_ports = 4;
2459 return mos7840_num_ports;
2462 /****************************************************************************
2464 ****************************************************************************/
2466 static int mos7840_startup(struct usb_serial *serial)
2468 struct moschip_port *mos7840_port;
2469 struct usb_device *dev;
2473 dbg("%s \n", " mos7840_startup :entering..........");
2476 dbg("%s\n", "Invalid Handler");
2482 dbg("%s\n", "Entering...");
2484 /* we set up the pointers to the endpoints in the mos7840_open *
2485 * function, as the structures aren't created yet. */
2487 /* set up port private structures */
2488 for (i = 0; i < serial->num_ports; ++i) {
2489 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2490 if (mos7840_port == NULL) {
2491 err("%s - Out of memory", __func__);
2493 i--; /* don't follow NULL pointer cleaning up */
2497 /* Initialize all port interrupt end point to port 0 int endpoint *
2498 * Our device has only one interrupt end point comman to all port */
2500 mos7840_port->port = serial->port[i];
2501 mos7840_set_port_private(serial->port[i], mos7840_port);
2502 spin_lock_init(&mos7840_port->pool_lock);
2504 mos7840_port->port_num = ((serial->port[i]->number -
2505 (serial->port[i]->serial->minor)) +
2508 if (mos7840_port->port_num == 1) {
2509 mos7840_port->SpRegOffset = 0x0;
2510 mos7840_port->ControlRegOffset = 0x1;
2511 mos7840_port->DcrRegOffset = 0x4;
2512 } else if ((mos7840_port->port_num == 2)
2513 && (serial->num_ports == 4)) {
2514 mos7840_port->SpRegOffset = 0x8;
2515 mos7840_port->ControlRegOffset = 0x9;
2516 mos7840_port->DcrRegOffset = 0x16;
2517 } else if ((mos7840_port->port_num == 2)
2518 && (serial->num_ports == 2)) {
2519 mos7840_port->SpRegOffset = 0xa;
2520 mos7840_port->ControlRegOffset = 0xb;
2521 mos7840_port->DcrRegOffset = 0x19;
2522 } else if ((mos7840_port->port_num == 3)
2523 && (serial->num_ports == 4)) {
2524 mos7840_port->SpRegOffset = 0xa;
2525 mos7840_port->ControlRegOffset = 0xb;
2526 mos7840_port->DcrRegOffset = 0x19;
2527 } else if ((mos7840_port->port_num == 4)
2528 && (serial->num_ports == 4)) {
2529 mos7840_port->SpRegOffset = 0xc;
2530 mos7840_port->ControlRegOffset = 0xd;
2531 mos7840_port->DcrRegOffset = 0x1c;
2533 mos7840_dump_serial_port(mos7840_port);
2535 mos7840_set_port_private(serial->port[i], mos7840_port);
2537 //enable rx_disable bit in control register
2540 mos7840_get_reg_sync(serial->port[i],
2541 mos7840_port->ControlRegOffset, &Data);
2543 dbg("Reading ControlReg failed status-0x%x\n", status);
2546 dbg("ControlReg Reading success val is %x, status%d\n",
2548 Data |= 0x08; //setting driver done bit
2549 Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
2551 //Data |= 0x20; //rx_disable bit
2554 mos7840_set_reg_sync(serial->port[i],
2555 mos7840_port->ControlRegOffset, Data);
2557 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2560 dbg("ControlReg Writing success(rx_disable) status%d\n",
2563 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2567 mos7840_set_reg_sync(serial->port[i],
2568 (__u16) (mos7840_port->DcrRegOffset +
2571 dbg("Writing DCR0 failed status-0x%x\n", status);
2574 dbg("DCR0 Writing success status%d\n", status);
2579 mos7840_set_reg_sync(serial->port[i],
2580 (__u16) (mos7840_port->DcrRegOffset +
2583 dbg("Writing DCR1 failed status-0x%x\n", status);
2586 dbg("DCR1 Writing success status%d\n", status);
2591 mos7840_set_reg_sync(serial->port[i],
2592 (__u16) (mos7840_port->DcrRegOffset +
2595 dbg("Writing DCR2 failed status-0x%x\n", status);
2598 dbg("DCR2 Writing success status%d\n", status);
2600 // write values in clkstart0x0 and clkmulti 0x20
2604 mos7840_set_reg_sync(serial->port[i],
2605 CLK_START_VALUE_REGISTER, Data);
2607 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2610 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2614 mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2617 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2621 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2624 //write value 0x0 to scratchpad register
2627 mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
2630 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2634 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2637 //Zero Length flag register
2638 if ((mos7840_port->port_num != 1)
2639 && (serial->num_ports == 2)) {
2643 status = mos7840_set_reg_sync(serial->port[i],
2649 dbg("ZLIP offset%x\n",
2651 ((__u16) mos7840_port->port_num)));
2653 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2657 dbg("ZLP_REG%d Writing success status%d\n",
2662 status = mos7840_set_reg_sync(serial->port[i],
2668 dbg("ZLIP offset%x\n",
2670 ((__u16) mos7840_port->port_num) - 0x1));
2672 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2676 dbg("ZLP_REG%d Writing success status%d\n",
2680 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2681 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2682 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
2683 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || !mos7840_port->dr) {
2689 //Zero Length flag enable
2691 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2693 dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
2696 dbg("ZLP_REG5 Writing success status%d\n", status);
2698 /* setting configuration feature to one */
2699 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2700 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2703 for (/* nothing */; i >= 0; i--) {
2704 mos7840_port = mos7840_get_port_private(serial->port[i]);
2706 kfree(mos7840_port->dr);
2707 kfree(mos7840_port->ctrl_buf);
2708 usb_free_urb(mos7840_port->control_urb);
2709 kfree(mos7840_port);
2710 serial->port[i] = NULL;
2715 /****************************************************************************
2717 * This function is called whenever the device is removed from the usb bus.
2718 ****************************************************************************/
2720 static void mos7840_shutdown(struct usb_serial *serial)
2723 unsigned long flags;
2724 struct moschip_port *mos7840_port;
2725 dbg("%s \n", " shutdown :entering..........");
2728 dbg("%s", "Invalid Handler \n");
2732 /* check for the ports to be closed,close the ports and disconnect */
2734 /* free private structure allocated for serial port *
2735 * stop reads and writes on all ports */
2737 for (i = 0; i < serial->num_ports; ++i) {
2738 mos7840_port = mos7840_get_port_private(serial->port[i]);
2739 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2740 mos7840_port->zombie = 1;
2741 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2742 usb_kill_urb(mos7840_port->control_urb);
2743 kfree(mos7840_port->ctrl_buf);
2744 kfree(mos7840_port->dr);
2745 kfree(mos7840_port);
2746 mos7840_set_port_private(serial->port[i], NULL);
2749 dbg("%s\n", "Thank u :: ");
2753 static struct usb_driver io_driver = {
2755 .probe = usb_serial_probe,
2756 .disconnect = usb_serial_disconnect,
2757 .id_table = moschip_id_table_combined,
2761 static struct usb_serial_driver moschip7840_4port_device = {
2763 .owner = THIS_MODULE,
2766 .description = DRIVER_DESC,
2767 .usb_driver = &io_driver,
2768 .id_table = moschip_port_id_table,
2770 .open = mos7840_open,
2771 .close = mos7840_close,
2772 .write = mos7840_write,
2773 .write_room = mos7840_write_room,
2774 .chars_in_buffer = mos7840_chars_in_buffer,
2775 .throttle = mos7840_throttle,
2776 .unthrottle = mos7840_unthrottle,
2777 .calc_num_ports = mos7840_calc_num_ports,
2778 #ifdef MCSSerialProbe
2779 .probe = mos7840_serial_probe,
2781 .ioctl = mos7840_ioctl,
2782 .set_termios = mos7840_set_termios,
2783 .break_ctl = mos7840_break,
2784 .tiocmget = mos7840_tiocmget,
2785 .tiocmset = mos7840_tiocmset,
2786 .attach = mos7840_startup,
2787 .shutdown = mos7840_shutdown,
2788 .read_bulk_callback = mos7840_bulk_in_callback,
2789 .read_int_callback = mos7840_interrupt_callback,
2792 /****************************************************************************
2794 * This is called by the module subsystem, or on startup to initialize us
2795 ****************************************************************************/
2796 static int __init moschip7840_init(void)
2800 dbg("%s \n", " mos7840_init :entering..........");
2802 /* Register with the usb serial */
2803 retval = usb_serial_register(&moschip7840_4port_device);
2806 goto failed_port_device_register;
2808 dbg("%s\n", "Entring...");
2809 info(DRIVER_DESC " " DRIVER_VERSION);
2811 /* Register with the usb */
2812 retval = usb_register(&io_driver);
2815 goto failed_usb_register;
2818 dbg("%s\n", "Leaving...");
2822 failed_usb_register:
2823 usb_serial_deregister(&moschip7840_4port_device);
2825 failed_port_device_register:
2830 /****************************************************************************
2832 * Called when the driver is about to be unloaded.
2833 ****************************************************************************/
2834 static void __exit moschip7840_exit(void)
2837 dbg("%s \n", " mos7840_exit :entering..........");
2839 usb_deregister(&io_driver);
2841 usb_serial_deregister(&moschip7840_4port_device);
2843 dbg("%s\n", "Entring...");
2846 module_init(moschip7840_init);
2847 module_exit(moschip7840_exit);
2849 /* Module information */
2850 MODULE_DESCRIPTION(DRIVER_DESC);
2851 MODULE_LICENSE("GPL");
2853 module_param(debug, bool, S_IRUGO | S_IWUSR);
2854 MODULE_PARM_DESC(debug, "Debug enabled or not");