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 #define USB_VENDOR_ID_MOSCHIP 0x9710
114 #define MOSCHIP_DEVICE_ID_7840 0x7840
115 #define MOSCHIP_DEVICE_ID_7820 0x7820
117 /* Interrupt Rotinue Defines */
119 #define SERIAL_IIR_RLS 0x06
120 #define SERIAL_IIR_MS 0x00
123 * Emulation of the bit mask on the LINE STATUS REGISTER.
125 #define SERIAL_LSR_DR 0x0001
126 #define SERIAL_LSR_OE 0x0002
127 #define SERIAL_LSR_PE 0x0004
128 #define SERIAL_LSR_FE 0x0008
129 #define SERIAL_LSR_BI 0x0010
131 #define MOS_MSR_DELTA_CTS 0x10
132 #define MOS_MSR_DELTA_DSR 0x20
133 #define MOS_MSR_DELTA_RI 0x40
134 #define MOS_MSR_DELTA_CD 0x80
136 // Serial Port register Address
137 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
138 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
139 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
140 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
141 #define LINE_STATUS_REGISTER ((__u16)(0x05))
142 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
143 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
144 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
145 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
147 #define CLK_MULTI_REGISTER ((__u16)(0x02))
148 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
150 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
153 * URB POOL related defines
155 #define NUM_URBS 16 /* URB Count */
156 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
159 static struct usb_device_id moschip_port_id_table[] = {
160 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
161 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
162 {} /* terminating entry */
165 static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
166 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
167 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
168 {} /* terminating entry */
171 MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
173 /* This structure holds all of the local port information */
175 struct moschip_port {
176 int port_num; /*Actual port number in the device(1,2,etc) */
177 struct urb *write_urb; /* write URB for this port */
178 struct urb *read_urb; /* read URB for this port */
180 __u8 shadowLCR; /* last LCR value received */
181 __u8 shadowMCR; /* last MCR value received */
185 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
186 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
188 struct async_icount icount;
189 struct usb_serial_port *port; /* loop back to the owner of this object */
193 __u8 ControlRegOffset;
195 //for processing control URBS in interrupt context
196 struct urb *control_urb;
197 struct usb_ctrlrequest *dr;
201 spinlock_t pool_lock;
202 struct urb *write_urb_pool[NUM_URBS];
210 * mos7840_set_reg_sync
211 * To set the Control register by calling usb_fill_control_urb function
212 * by passing usb_sndctrlpipe function as parameter.
215 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
218 struct usb_device *dev = port->serial->dev;
220 dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
222 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
223 MCS_WR_RTYPE, val, reg, NULL, 0,
228 * mos7840_get_reg_sync
229 * To set the Uart register by calling usb_fill_control_urb function by
230 * passing usb_rcvctrlpipe function as parameter.
233 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
236 struct usb_device *dev = port->serial->dev;
239 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
240 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
242 dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
243 *val = (*val) & 0x00ff;
248 * mos7840_set_uart_reg
249 * To set the Uart register by calling usb_fill_control_urb function by
250 * passing usb_sndctrlpipe function as parameter.
253 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
257 struct usb_device *dev = port->serial->dev;
259 // For the UART control registers, the application number need to be Or'ed
260 if (port->serial->num_ports == 4) {
262 (((__u16) port->number - (__u16) (port->serial->minor)) +
264 dbg("mos7840_set_uart_reg application number is %x\n", val);
266 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
268 (((__u16) port->number -
269 (__u16) (port->serial->minor)) + 1) << 8;
270 dbg("mos7840_set_uart_reg application number is %x\n",
274 (((__u16) port->number -
275 (__u16) (port->serial->minor)) + 2) << 8;
276 dbg("mos7840_set_uart_reg application number is %x\n",
280 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
281 MCS_WR_RTYPE, val, reg, NULL, 0,
287 * mos7840_get_uart_reg
288 * To set the Control register by calling usb_fill_control_urb function
289 * by passing usb_rcvctrlpipe function as parameter.
291 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
294 struct usb_device *dev = port->serial->dev;
298 //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
299 /*Wval is same as application number */
300 if (port->serial->num_ports == 4) {
302 (((__u16) port->number - (__u16) (port->serial->minor)) +
304 dbg("mos7840_get_uart_reg application number is %x\n", Wval);
306 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
308 (((__u16) port->number -
309 (__u16) (port->serial->minor)) + 1) << 8;
310 dbg("mos7840_get_uart_reg application number is %x\n",
314 (((__u16) port->number -
315 (__u16) (port->serial->minor)) + 2) << 8;
316 dbg("mos7840_get_uart_reg application number is %x\n",
320 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
321 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
323 *val = (*val) & 0x00ff;
327 static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
330 dbg("***************************************\n");
331 dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
332 dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
333 dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
334 dbg("***************************************\n");
338 /************************************************************************/
339 /************************************************************************/
340 /* I N T E R F A C E F U N C T I O N S */
341 /* I N T E R F A C E F U N C T I O N S */
342 /************************************************************************/
343 /************************************************************************/
345 static inline void mos7840_set_port_private(struct usb_serial_port *port,
346 struct moschip_port *data)
348 usb_set_serial_port_data(port, (void *)data);
351 static inline struct moschip_port *mos7840_get_port_private(struct
355 return (struct moschip_port *)usb_get_serial_port_data(port);
358 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
360 struct moschip_port *mos7840_port;
361 struct async_icount *icount;
363 icount = &mos7840_port->icount;
365 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
367 icount = &mos7840_port->icount;
369 /* update input line counters */
370 if (new_msr & MOS_MSR_DELTA_CTS) {
374 if (new_msr & MOS_MSR_DELTA_DSR) {
378 if (new_msr & MOS_MSR_DELTA_CD) {
382 if (new_msr & MOS_MSR_DELTA_RI) {
389 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
391 struct async_icount *icount;
393 dbg("%s - %02x", __FUNCTION__, new_lsr);
395 if (new_lsr & SERIAL_LSR_BI) {
397 // Parity and Framing errors only count if they
398 // occur exclusive of a break being
401 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
404 /* update input line counters */
405 icount = &port->icount;
406 if (new_lsr & SERIAL_LSR_BI) {
410 if (new_lsr & SERIAL_LSR_OE) {
414 if (new_lsr & SERIAL_LSR_PE) {
418 if (new_lsr & SERIAL_LSR_FE) {
424 /************************************************************************/
425 /************************************************************************/
426 /* U S B C A L L B A C K F U N C T I O N S */
427 /* U S B C A L L B A C K F U N C T I O N S */
428 /************************************************************************/
429 /************************************************************************/
431 static void mos7840_control_callback(struct urb *urb)
434 struct moschip_port *mos7840_port;
439 dbg("%s", "Invalid Pointer !!!!:\n");
443 mos7840_port = (struct moschip_port *)urb->context;
445 switch (urb->status) {
452 /* this urb is terminated, clean up */
453 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
457 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
462 dbg("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
463 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__,
464 mos7840_port->MsrLsr, mos7840_port->port_num);
465 data = urb->transfer_buffer;
466 regval = (__u8) data[0];
467 dbg("%s data is %x\n", __FUNCTION__, regval);
468 if (mos7840_port->MsrLsr == 0)
469 mos7840_handle_new_msr(mos7840_port, regval);
470 else if (mos7840_port->MsrLsr == 1)
471 mos7840_handle_new_lsr(mos7840_port, regval);
474 spin_lock(&mos7840_port->pool_lock);
475 if (!mos7840_port->zombie)
476 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
477 spin_unlock(&mos7840_port->pool_lock);
479 dev_err(&urb->dev->dev,
480 "%s - Error %d submitting interrupt urb\n",
481 __FUNCTION__, result);
485 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
488 struct usb_device *dev = mcs->port->serial->dev;
489 struct usb_ctrlrequest *dr = mcs->dr;
490 unsigned char *buffer = mcs->ctrl_buf;
493 dr->bRequestType = MCS_RD_RTYPE;
494 dr->bRequest = MCS_RDREQ;
495 dr->wValue = cpu_to_le16(Wval); //0;
496 dr->wIndex = cpu_to_le16(reg);
497 dr->wLength = cpu_to_le16(2);
499 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
500 (unsigned char *)dr, buffer, 2,
501 mos7840_control_callback, mcs);
502 mcs->control_urb->transfer_buffer_length = 2;
503 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
507 /*****************************************************************************
508 * mos7840_interrupt_callback
509 * this is the callback function for when we have received data on the
510 * interrupt endpoint.
511 *****************************************************************************/
513 static void mos7840_interrupt_callback(struct urb *urb)
517 struct moschip_port *mos7840_port;
518 struct usb_serial *serial;
523 __u16 wval, wreg = 0;
525 dbg("%s", " : Entering\n");
527 dbg("%s", "Invalid Pointer !!!!:\n");
531 switch (urb->status) {
538 /* this urb is terminated, clean up */
539 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
543 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
548 length = urb->actual_length;
549 data = urb->transfer_buffer;
551 serial = (struct usb_serial *)urb->context;
553 /* Moschip get 5 bytes
554 * Byte 1 IIR Port 1 (port.number is 0)
555 * Byte 2 IIR Port 2 (port.number is 1)
556 * Byte 3 IIR Port 3 (port.number is 2)
557 * Byte 4 IIR Port 4 (port.number is 3)
558 * Byte 5 FIFO status for both */
560 if (length && length > 5) {
561 dbg("%s \n", "Wrong data !!!");
565 sp[0] = (__u8) data[0];
566 sp[1] = (__u8) data[1];
567 sp[2] = (__u8) data[2];
568 sp[3] = (__u8) data[3];
571 for (i = 0; i < serial->num_ports; i++) {
572 mos7840_port = mos7840_get_port_private(serial->port[i]);
574 (((__u16) serial->port[i]->number -
575 (__u16) (serial->minor)) + 1) << 8;
576 if (mos7840_port->open) {
578 dbg("SP%d No Interrupt !!!\n", i);
580 switch (sp[i] & 0x0f) {
582 dbg("Serial Port %d: Receiver status error or ", i);
583 dbg("address bit detected in 9-bit mode\n");
584 mos7840_port->MsrLsr = 1;
585 wreg = LINE_STATUS_REGISTER;
588 dbg("Serial Port %d: Modem status change\n", i);
589 mos7840_port->MsrLsr = 0;
590 wreg = MODEM_STATUS_REGISTER;
593 spin_lock(&mos7840_port->pool_lock);
594 if (!mos7840_port->zombie) {
595 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
597 spin_unlock(&mos7840_port->pool_lock);
600 spin_unlock(&mos7840_port->pool_lock);
604 if (!(rv < 0)) /* the completion handler for the control urb will resubmit */
607 result = usb_submit_urb(urb, GFP_ATOMIC);
609 dev_err(&urb->dev->dev,
610 "%s - Error %d submitting interrupt urb\n",
611 __FUNCTION__, result);
615 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
616 const char *function)
619 dbg("%s - port == NULL", function);
623 dbg("%s - port->serial == NULL", function);
630 /* Inline functions to check the sanity of a pointer that is passed to us */
631 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
632 const char *function)
635 dbg("%s - serial == NULL", function);
639 dbg("%s - serial->type == NULL!", function);
646 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
647 const char *function)
649 /* if no port was specified, or it fails a paranoia check */
651 mos7840_port_paranoia_check(port, function) ||
652 mos7840_serial_paranoia_check(port->serial, function)) {
653 /* then say that we don't have a valid usb_serial thing, which will
654 * end up genrating -ENODEV return values */
661 /*****************************************************************************
662 * mos7840_bulk_in_callback
663 * this is the callback function for when we have received data on the
665 *****************************************************************************/
667 static void mos7840_bulk_in_callback(struct urb *urb)
671 struct usb_serial *serial;
672 struct usb_serial_port *port;
673 struct moschip_port *mos7840_port;
674 struct tty_struct *tty;
677 dbg("%s", "Invalid Pointer !!!!:\n");
682 dbg("nonzero read bulk status received: %d", urb->status);
686 mos7840_port = (struct moschip_port *)urb->context;
688 dbg("%s", "NULL mos7840_port pointer \n");
692 port = (struct usb_serial_port *)mos7840_port->port;
693 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
694 dbg("%s", "Port Paranoia failed \n");
698 serial = mos7840_get_usb_serial(port, __FUNCTION__);
700 dbg("%s\n", "Bad serial pointer ");
704 dbg("%s\n", "Entering... \n");
706 data = urb->transfer_buffer;
708 dbg("%s", "Entering ........... \n");
710 if (urb->actual_length) {
711 tty = mos7840_port->port->tty;
713 tty_buffer_request_room(tty, urb->actual_length);
714 tty_insert_flip_string(tty, data, urb->actual_length);
716 tty_flip_buffer_push(tty);
718 mos7840_port->icount.rx += urb->actual_length;
720 dbg("mos7840_port->icount.rx is %d:\n",
721 mos7840_port->icount.rx);
724 if (!mos7840_port->read_urb) {
725 dbg("%s", "URB KILLED !!!\n");
730 mos7840_port->read_urb->dev = serial->dev;
732 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
735 dbg(" usb_submit_urb(read bulk) failed, status = %d",
740 /*****************************************************************************
741 * mos7840_bulk_out_data_callback
742 * this is the callback function for when we have finished sending serial data
743 * on the bulk out endpoint.
744 *****************************************************************************/
746 static void mos7840_bulk_out_data_callback(struct urb *urb)
748 struct moschip_port *mos7840_port;
749 struct tty_struct *tty;
753 dbg("%s", "Invalid Pointer !!!!:\n");
757 mos7840_port = (struct moschip_port *)urb->context;
758 spin_lock(&mos7840_port->pool_lock);
759 for (i = 0; i < NUM_URBS; i++) {
760 if (urb == mos7840_port->write_urb_pool[i]) {
761 mos7840_port->busy[i] = 0;
765 spin_unlock(&mos7840_port->pool_lock);
768 dbg("nonzero write bulk status received:%d\n", urb->status);
772 if (mos7840_port_paranoia_check(mos7840_port->port, __FUNCTION__)) {
773 dbg("%s", "Port Paranoia failed \n");
777 dbg("%s \n", "Entering .........");
779 tty = mos7840_port->port->tty;
781 if (tty && mos7840_port->open)
786 /************************************************************************/
787 /* 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 */
788 /************************************************************************/
789 #ifdef MCSSerialProbe
790 static int mos7840_serial_probe(struct usb_serial *serial,
791 const struct usb_device_id *id)
794 /*need to implement the mode_reg reading and updating\
795 structures usb_serial_ device_type\
796 (i.e num_ports, num_bulkin,bulkout etc) */
797 /* Also we can update the changes attach */
802 /*****************************************************************************
804 * this function is called by the tty driver when a port is opened
805 * If successful, we return 0
806 * Otherwise we return a negative error number.
807 *****************************************************************************/
809 static int mos7840_open(struct usb_serial_port *port, struct file *filp)
813 struct usb_serial *serial;
817 struct moschip_port *mos7840_port;
818 struct moschip_port *port0;
820 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
821 dbg("%s", "Port Paranoia failed \n");
825 serial = port->serial;
827 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
828 dbg("%s", "Serial Paranoia failed \n");
832 mos7840_port = mos7840_get_port_private(port);
833 port0 = mos7840_get_port_private(serial->port[0]);
835 if (mos7840_port == NULL || port0 == NULL)
838 usb_clear_halt(serial->dev, port->write_urb->pipe);
839 usb_clear_halt(serial->dev, port->read_urb->pipe);
842 /* Initialising the write urb pool */
843 for (j = 0; j < NUM_URBS; ++j) {
844 urb = usb_alloc_urb(0, GFP_KERNEL);
845 mos7840_port->write_urb_pool[j] = urb;
848 err("No more urbs???");
852 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
853 if (!urb->transfer_buffer) {
855 mos7840_port->write_urb_pool[j] = NULL;
856 err("%s-out of memory for urb buffers.", __FUNCTION__);
861 /*****************************************************************************
862 * Initialize MCS7840 -- Write Init values to corresponding Registers
870 * 0x08 : SP1/2 Control Reg
871 *****************************************************************************/
873 //NEED to check the following Block
877 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
879 dbg("Reading Spreg failed\n");
883 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
885 dbg("writing Spreg failed\n");
890 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
892 dbg("writing Spreg failed\n");
895 //End of block to be checked
900 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
902 dbg("Reading Controlreg failed\n");
905 Data |= 0x08; //Driver done bit
906 Data |= 0x20; //rx_disable
907 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
909 dbg("writing Controlreg failed\n");
912 //do register settings here
913 // Set all regs to the device default values.
914 ////////////////////////////////////
915 // First Disable all interrupts.
916 ////////////////////////////////////
919 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
921 dbg("disableing interrupts failed\n");
924 // Set FIFO_CONTROL_REGISTER to the default value
926 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
928 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
933 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
935 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
940 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
941 mos7840_port->shadowLCR = Data;
944 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
945 mos7840_port->shadowMCR = Data;
948 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
949 mos7840_port->shadowLCR = Data;
951 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
952 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
956 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
960 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
964 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
966 Data = Data & ~SERIAL_LCR_DLAB;
968 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
969 mos7840_port->shadowLCR = Data;
971 //clearing Bulkin and Bulkout Fifo
974 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
978 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
982 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
983 //Finally enable all interrupts
987 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
989 //clearing rx_disable
993 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
997 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1003 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
1007 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1009 /* force low_latency on so that our tty_push actually forces *
1010 * the data through,otherwise it is scheduled, and with *
1011 * high data rates (like with OHCI) data can get lost. */
1014 port->tty->low_latency = 1;
1015 /* Check to see if we've set up our endpoint info yet *
1016 * (can't set it up in mos7840_startup as the structures *
1017 * were not set up at that time.) */
1018 if (port0->open_ports == 1) {
1019 if (serial->port[0]->interrupt_in_buffer == NULL) {
1021 /* set up interrupt urb */
1023 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1025 usb_rcvintpipe(serial->dev,
1027 interrupt_in_endpointAddress),
1028 serial->port[0]->interrupt_in_buffer,
1029 serial->port[0]->interrupt_in_urb->
1030 transfer_buffer_length,
1031 mos7840_interrupt_callback,
1033 serial->port[0]->interrupt_in_urb->
1036 /* start interrupt read for mos7840 *
1037 * will continue as long as mos7840 is connected */
1040 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1043 err("%s - Error %d submitting interrupt urb",
1044 __FUNCTION__, response);
1051 /* see if we've set up our endpoint info yet *
1052 * (can't set it up in mos7840_startup as the *
1053 * structures were not set up at that time.) */
1055 dbg("port number is %d \n", port->number);
1056 dbg("serial number is %d \n", port->serial->minor);
1057 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1058 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1059 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
1060 dbg("port's number in the device is %d\n", mos7840_port->port_num);
1061 mos7840_port->read_urb = port->read_urb;
1063 /* set up our bulk in urb */
1065 usb_fill_bulk_urb(mos7840_port->read_urb,
1067 usb_rcvbulkpipe(serial->dev,
1068 port->bulk_in_endpointAddress),
1069 port->bulk_in_buffer,
1070 mos7840_port->read_urb->transfer_buffer_length,
1071 mos7840_bulk_in_callback, mos7840_port);
1073 dbg("mos7840_open: bulkin endpoint is %d\n",
1074 port->bulk_in_endpointAddress);
1075 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1077 err("%s - Error %d submitting control urb", __FUNCTION__,
1081 /* initialize our wait queues */
1082 init_waitqueue_head(&mos7840_port->wait_chase);
1083 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1085 /* initialize our icount structure */
1086 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1088 /* initialize our port settings */
1089 mos7840_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
1090 /* send a open port command */
1091 mos7840_port->open = 1;
1092 //mos7840_change_port_settings(mos7840_port,old_termios);
1093 mos7840_port->icount.tx = 0;
1094 mos7840_port->icount.rx = 0;
1096 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial, mos7840_port, port);
1102 /*****************************************************************************
1103 * mos7840_chars_in_buffer
1104 * this function is called by the tty driver when it wants to know how many
1105 * bytes of data we currently have outstanding in the port (data that has
1106 * been written, but hasn't made it out the port yet)
1107 * If successful, we return the number of bytes left to be written in the
1109 * Otherwise we return a negative error number.
1110 *****************************************************************************/
1112 static int mos7840_chars_in_buffer(struct usb_serial_port *port)
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, __FUNCTION__)) {
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;
1138 spin_unlock_irqrestore(&mos7840_port->pool_lock,flags);
1139 dbg("%s - returns %d", __FUNCTION__, chars);
1144 /************************************************************************
1146 * mos7840_block_until_tx_empty
1148 * This function will block the close until one of the following:
1150 * 2. The mos7840 has stopped
1151 * 3. A timout of 3 seconds without activity has expired
1153 ************************************************************************/
1154 static void mos7840_block_until_tx_empty(struct moschip_port *mos7840_port)
1156 int timeout = HZ / 10;
1162 count = mos7840_chars_in_buffer(mos7840_port->port);
1164 /* Check for Buffer status */
1169 /* Block the thread for a while */
1170 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1173 /* No activity.. count down section */
1176 dbg("%s - TIMEOUT", __FUNCTION__);
1179 /* Reset timout value back to seconds */
1185 /*****************************************************************************
1187 * this function is called by the tty driver when a port is closed
1188 *****************************************************************************/
1190 static void mos7840_close(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, __FUNCTION__)) {
1201 dbg("%s", "Port Paranoia failed \n");
1205 serial = mos7840_get_usb_serial(port, __FUNCTION__);
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(mos7840_port);
1236 /* While closing port, shutdown all bulk read, write *
1237 * and interrupt read if they exists */
1240 if (mos7840_port->write_urb) {
1241 dbg("%s", "Shutdown bulk write\n");
1242 usb_kill_urb(mos7840_port->write_urb);
1245 if (mos7840_port->read_urb) {
1246 dbg("%s", "Shutdown bulk read\n");
1247 usb_kill_urb(mos7840_port->read_urb);
1249 if ((&mos7840_port->control_urb)) {
1250 dbg("%s", "Shutdown control read\n");
1251 // usb_kill_urb (mos7840_port->control_urb);
1255 // if(mos7840_port->ctrl_buf != NULL)
1256 // kfree(mos7840_port->ctrl_buf);
1257 port0->open_ports--;
1258 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1259 port0->open_ports, port->number);
1260 if (port0->open_ports == 0) {
1261 if (serial->port[0]->interrupt_in_urb) {
1262 dbg("%s", "Shutdown interrupt_in_urb\n");
1263 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1267 if (mos7840_port->write_urb) {
1268 /* if this urb had a transfer buffer already (old tx) free it */
1270 if (mos7840_port->write_urb->transfer_buffer != NULL) {
1271 kfree(mos7840_port->write_urb->transfer_buffer);
1273 usb_free_urb(mos7840_port->write_urb);
1277 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1280 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1282 mos7840_port->open = 0;
1284 dbg("%s \n", "Leaving ............");
1287 /************************************************************************
1289 * mos7840_block_until_chase_response
1291 * This function will block the close until one of the following:
1292 * 1. Response to our Chase comes from mos7840
1293 * 2. A timout of 10 seconds without activity has expired
1294 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1296 ************************************************************************/
1298 static void mos7840_block_until_chase_response(struct moschip_port
1301 int timeout = 1 * HZ;
1306 count = mos7840_chars_in_buffer(mos7840_port->port);
1308 /* Check for Buffer status */
1313 /* Block the thread for a while */
1314 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1316 /* No activity.. count down section */
1319 dbg("%s - TIMEOUT", __FUNCTION__);
1322 /* Reset timout value back to seconds */
1329 /*****************************************************************************
1331 * this function sends a break to the port
1332 *****************************************************************************/
1333 static void mos7840_break(struct usb_serial_port *port, int break_state)
1336 struct usb_serial *serial;
1337 struct moschip_port *mos7840_port;
1339 dbg("%s \n", "Entering ...........");
1340 dbg("mos7840_break: Start\n");
1342 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1343 dbg("%s", "Port Paranoia failed \n");
1347 serial = mos7840_get_usb_serial(port, __FUNCTION__);
1349 dbg("%s", "Serial Paranoia failed \n");
1353 mos7840_port = mos7840_get_port_private(port);
1355 if (mos7840_port == NULL) {
1361 /* flush and block until tx is empty */
1362 mos7840_block_until_chase_response(mos7840_port);
1365 if (break_state == -1) {
1366 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1368 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1371 mos7840_port->shadowLCR = data;
1372 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1373 mos7840_port->shadowLCR);
1374 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1375 mos7840_port->shadowLCR);
1380 /*****************************************************************************
1381 * mos7840_write_room
1382 * this function is called by the tty driver when it wants to know how many
1383 * bytes of data we can accept for a specific port.
1384 * If successful, we return the amount of room that we have for this port
1385 * Otherwise we return a negative error number.
1386 *****************************************************************************/
1388 static int mos7840_write_room(struct usb_serial_port *port)
1392 unsigned long flags;
1393 struct moschip_port *mos7840_port;
1395 dbg("%s \n", " mos7840_write_room:entering ...........");
1397 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1398 dbg("%s", "Invalid port \n");
1399 dbg("%s \n", " mos7840_write_room:leaving ...........");
1403 mos7840_port = mos7840_get_port_private(port);
1404 if (mos7840_port == NULL) {
1405 dbg("%s \n", "mos7840_break:leaving ...........");
1409 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1410 for (i = 0; i < NUM_URBS; ++i) {
1411 if (!mos7840_port->busy[i]) {
1412 room += URB_TRANSFER_BUFFER_SIZE;
1415 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1417 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1418 dbg("%s - returns %d", __FUNCTION__, room);
1423 /*****************************************************************************
1425 * this function is called by the tty driver when data should be written to
1427 * If successful, we return the number of bytes written, otherwise we
1428 * return a negative error number.
1429 *****************************************************************************/
1431 static int mos7840_write(struct usb_serial_port *port,
1432 const unsigned char *data, int count)
1438 unsigned long flags;
1440 struct moschip_port *mos7840_port;
1441 struct usb_serial *serial;
1444 const unsigned char *current_position = data;
1445 unsigned char *data1;
1446 dbg("%s \n", "entering ...........");
1447 //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
1452 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1453 mos7840_port->shadowLCR = Data;
1454 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
1455 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1456 mos7840_port->shadowLCR);
1459 //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
1460 //mos7840_port->shadowLCR=Data;//Need to add later
1462 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1464 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1467 //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
1470 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1471 dbg("mos7840_write:DLL value is %x\n", Data);
1475 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1476 dbg("mos7840_write:DLM value is %x\n", Data);
1478 Data = Data & ~SERIAL_LCR_DLAB;
1479 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1480 mos7840_port->shadowLCR);
1482 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1485 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1486 dbg("%s", "Port Paranoia failed \n");
1490 serial = port->serial;
1491 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
1492 dbg("%s", "Serial Paranoia failed \n");
1496 mos7840_port = mos7840_get_port_private(port);
1497 if (mos7840_port == NULL) {
1498 dbg("%s", "mos7840_port is NULL\n");
1502 /* try to find a free urb in the list */
1505 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1506 for (i = 0; i < NUM_URBS; ++i) {
1507 if (!mos7840_port->busy[i]) {
1508 mos7840_port->busy[i] = 1;
1509 urb = mos7840_port->write_urb_pool[i];
1514 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1517 dbg("%s - no more free urbs", __FUNCTION__);
1521 if (urb->transfer_buffer == NULL) {
1522 urb->transfer_buffer =
1523 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1525 if (urb->transfer_buffer == NULL) {
1526 err("%s no more kernel memory...", __FUNCTION__);
1530 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1532 memcpy(urb->transfer_buffer, current_position, transfer_size);
1534 /* fill urb with data and submit */
1535 usb_fill_bulk_urb(urb,
1537 usb_sndbulkpipe(serial->dev,
1538 port->bulk_out_endpointAddress),
1539 urb->transfer_buffer,
1541 mos7840_bulk_out_data_callback, mos7840_port);
1543 data1 = urb->transfer_buffer;
1544 dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1546 /* send it down the pipe */
1547 status = usb_submit_urb(urb, GFP_ATOMIC);
1550 mos7840_port->busy[i] = 0;
1551 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1552 __FUNCTION__, status);
1553 bytes_sent = status;
1556 bytes_sent = transfer_size;
1557 mos7840_port->icount.tx += transfer_size;
1559 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1566 /*****************************************************************************
1568 * this function is called by the tty driver when it wants to stop the data
1569 * being read from the port.
1570 *****************************************************************************/
1572 static void mos7840_throttle(struct usb_serial_port *port)
1574 struct moschip_port *mos7840_port;
1575 struct tty_struct *tty;
1578 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1579 dbg("%s", "Invalid port \n");
1583 dbg("- port %d\n", port->number);
1585 mos7840_port = mos7840_get_port_private(port);
1587 if (mos7840_port == NULL)
1590 if (!mos7840_port->open) {
1591 dbg("%s\n", "port not opened");
1595 dbg("%s", "Entering .......... \n");
1599 dbg("%s - no tty available", __FUNCTION__);
1603 /* if we are implementing XON/XOFF, send the stop character */
1605 unsigned char stop_char = STOP_CHAR(tty);
1606 status = mos7840_write(port, &stop_char, 1);
1612 /* if we are implementing RTS/CTS, toggle that line */
1613 if (tty->termios->c_cflag & CRTSCTS) {
1614 mos7840_port->shadowMCR &= ~MCR_RTS;
1617 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1618 mos7840_port->shadowMCR);
1628 /*****************************************************************************
1629 * mos7840_unthrottle
1630 * this function is called by the tty driver when it wants to resume the data
1631 * being read from the port (called after SerialThrottle is called)
1632 *****************************************************************************/
1633 static void mos7840_unthrottle(struct usb_serial_port *port)
1635 struct tty_struct *tty;
1637 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1639 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1640 dbg("%s", "Invalid port \n");
1644 if (mos7840_port == NULL)
1647 if (!mos7840_port->open) {
1648 dbg("%s - port not opened", __FUNCTION__);
1652 dbg("%s", "Entering .......... \n");
1656 dbg("%s - no tty available", __FUNCTION__);
1660 /* if we are implementing XON/XOFF, send the start character */
1662 unsigned char start_char = START_CHAR(tty);
1663 status = mos7840_write(port, &start_char, 1);
1669 /* if we are implementing RTS/CTS, toggle that line */
1670 if (tty->termios->c_cflag & CRTSCTS) {
1671 mos7840_port->shadowMCR |= MCR_RTS;
1674 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1675 mos7840_port->shadowMCR);
1684 static int mos7840_tiocmget(struct usb_serial_port *port, struct file *file)
1686 struct moschip_port *mos7840_port;
1687 unsigned int result;
1691 mos7840_port = mos7840_get_port_private(port);
1693 dbg("%s - port %d", __FUNCTION__, port->number);
1695 if (mos7840_port == NULL)
1698 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1699 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1700 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1701 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1702 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1703 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1704 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1705 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1706 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1708 dbg("%s - 0x%04X", __FUNCTION__, result);
1713 static int mos7840_tiocmset(struct usb_serial_port *port, struct file *file,
1714 unsigned int set, unsigned int clear)
1716 struct moschip_port *mos7840_port;
1718 unsigned int status;
1720 dbg("%s - port %d", __FUNCTION__, port->number);
1722 mos7840_port = mos7840_get_port_private(port);
1724 if (mos7840_port == NULL)
1727 mcr = mos7840_port->shadowMCR;
1728 if (clear & TIOCM_RTS)
1730 if (clear & TIOCM_DTR)
1732 if (clear & TIOCM_LOOP)
1733 mcr &= ~MCR_LOOPBACK;
1735 if (set & TIOCM_RTS)
1737 if (set & TIOCM_DTR)
1739 if (set & TIOCM_LOOP)
1740 mcr |= MCR_LOOPBACK;
1742 mos7840_port->shadowMCR = mcr;
1745 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1747 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
1754 /*****************************************************************************
1755 * mos7840_calc_baud_rate_divisor
1756 * this function calculates the proper baud rate divisor for the specified
1758 *****************************************************************************/
1759 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1760 __u16 * clk_sel_val)
1763 dbg("%s - %d", __FUNCTION__, baudRate);
1765 if (baudRate <= 115200) {
1766 *divisor = 115200 / baudRate;
1769 if ((baudRate > 115200) && (baudRate <= 230400)) {
1770 *divisor = 230400 / baudRate;
1771 *clk_sel_val = 0x10;
1772 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1773 *divisor = 403200 / baudRate;
1774 *clk_sel_val = 0x20;
1775 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1776 *divisor = 460800 / baudRate;
1777 *clk_sel_val = 0x30;
1778 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1779 *divisor = 806400 / baudRate;
1780 *clk_sel_val = 0x40;
1781 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1782 *divisor = 921600 / baudRate;
1783 *clk_sel_val = 0x50;
1784 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1785 *divisor = 1572864 / baudRate;
1786 *clk_sel_val = 0x60;
1787 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1788 *divisor = 3145728 / baudRate;
1789 *clk_sel_val = 0x70;
1795 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1796 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1797 *divisor = mos7840_divisor_table[i].Divisor;
1802 /* After trying for all the standard baud rates *
1803 * Try calculating the divisor for this baud rate */
1805 if (baudrate > 75 && baudrate < 230400) {
1806 /* get the divisor */
1807 custom = (__u16) (230400L / baudrate);
1809 /* Check for round off */
1810 round1 = (__u16) (2304000L / baudrate);
1811 round = (__u16) (round1 - (custom * 10));
1817 dbg(" Baud %d = %d\n", baudrate, custom);
1821 dbg("%s\n", " Baud calculation Failed...");
1826 /*****************************************************************************
1827 * mos7840_send_cmd_write_baud_rate
1828 * this function sends the proper command to change the baud rate of the
1830 *****************************************************************************/
1832 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1838 unsigned char number;
1840 struct usb_serial_port *port;
1842 if (mos7840_port == NULL)
1845 port = (struct usb_serial_port *)mos7840_port->port;
1846 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1847 dbg("%s", "Invalid port \n");
1851 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1852 dbg("%s", "Invalid Serial \n");
1856 dbg("%s", "Entering .......... \n");
1858 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1860 dbg("%s - port = %d, baud = %d", __FUNCTION__,
1861 mos7840_port->port->number, baudRate);
1862 //reset clk_uart_sel in spregOffset
1863 if (baudRate > 115200) {
1864 #ifdef HW_flow_control
1865 //NOTE: need to see the pther register to modify
1866 //setting h/w flow control bit to 1;
1869 mos7840_port->shadowMCR = Data;
1871 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1873 dbg("Writing spreg failed in set_serial_baud\n");
1879 #ifdef HW_flow_control
1880 //setting h/w flow control bit to 0;
1883 mos7840_port->shadowMCR = Data;
1885 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1887 dbg("Writing spreg failed in set_serial_baud\n");
1894 if (1) //baudRate <= 115200)
1900 mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1903 mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1906 dbg("reading spreg failed in set_serial_baud\n");
1909 Data = (Data & 0x8f) | clk_sel_val;
1912 mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1914 dbg("Writing spreg failed in set_serial_baud\n");
1917 /* Calculate the Divisor */
1920 err("%s - bad baud rate", __FUNCTION__);
1921 dbg("%s\n", "bad baud rate");
1924 /* Enable access to divisor latch */
1925 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1926 mos7840_port->shadowLCR = Data;
1927 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1929 /* Write the divisor */
1930 Data = (unsigned char)(divisor & 0xff);
1931 dbg("set_serial_baud Value to write DLL is %x\n", Data);
1932 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1934 Data = (unsigned char)((divisor & 0xff00) >> 8);
1935 dbg("set_serial_baud Value to write DLM is %x\n", Data);
1936 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1938 /* Disable access to divisor latch */
1939 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1940 mos7840_port->shadowLCR = Data;
1941 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1948 /*****************************************************************************
1949 * mos7840_change_port_settings
1950 * This routine is called to set the UART on the device to match
1951 * the specified new settings.
1952 *****************************************************************************/
1954 static void mos7840_change_port_settings(struct moschip_port *mos7840_port,
1955 struct ktermios *old_termios)
1957 struct tty_struct *tty;
1966 struct usb_serial_port *port;
1967 struct usb_serial *serial;
1969 if (mos7840_port == NULL)
1972 port = (struct usb_serial_port *)mos7840_port->port;
1974 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1975 dbg("%s", "Invalid port \n");
1979 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1980 dbg("%s", "Invalid Serial \n");
1984 serial = port->serial;
1986 dbg("%s - port %d", __FUNCTION__, mos7840_port->port->number);
1988 if (!mos7840_port->open) {
1989 dbg("%s - port not opened", __FUNCTION__);
1993 tty = mos7840_port->port->tty;
1995 if ((!tty) || (!tty->termios)) {
1996 dbg("%s - no tty structures", __FUNCTION__);
2000 dbg("%s", "Entering .......... \n");
2004 lParity = LCR_PAR_NONE;
2006 cflag = tty->termios->c_cflag;
2007 iflag = tty->termios->c_iflag;
2009 /* Change the number of bits */
2010 if (cflag & CSIZE) {
2011 switch (cflag & CSIZE) {
2029 /* Change the Parity bit */
2030 if (cflag & PARENB) {
2031 if (cflag & PARODD) {
2032 lParity = LCR_PAR_ODD;
2033 dbg("%s - parity = odd", __FUNCTION__);
2035 lParity = LCR_PAR_EVEN;
2036 dbg("%s - parity = even", __FUNCTION__);
2040 dbg("%s - parity = none", __FUNCTION__);
2043 if (cflag & CMSPAR) {
2044 lParity = lParity | 0x20;
2047 /* Change the Stop bit */
2048 if (cflag & CSTOPB) {
2050 dbg("%s - stop bits = 2", __FUNCTION__);
2053 dbg("%s - stop bits = 1", __FUNCTION__);
2056 /* Update the LCR with the correct value */
2057 mos7840_port->shadowLCR &=
2058 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2059 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2061 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2062 mos7840_port->shadowLCR);
2063 /* Disable Interrupts */
2065 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2068 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2071 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2073 /* Send the updated LCR value to the mos7840 */
2074 Data = mos7840_port->shadowLCR;
2076 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2079 mos7840_port->shadowMCR = Data;
2080 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2082 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2084 /* set up the MCR register and send it to the mos7840 */
2086 mos7840_port->shadowMCR = MCR_MASTER_IE;
2087 if (cflag & CBAUD) {
2088 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2091 if (cflag & CRTSCTS) {
2092 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2095 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2098 Data = mos7840_port->shadowMCR;
2099 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2101 /* Determine divisor based on baud rate */
2102 baud = tty_get_baud_rate(tty);
2105 /* pick a default, any default... */
2106 dbg("%s\n", "Picked default baud...");
2110 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2111 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2113 /* Enable Interrupts */
2115 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2117 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2118 mos7840_port->read_urb->dev = serial->dev;
2120 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2123 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2127 wake_up(&mos7840_port->delta_msr_wait);
2128 mos7840_port->delta_msr_cond = 1;
2129 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2130 mos7840_port->shadowLCR);
2135 /*****************************************************************************
2136 * mos7840_set_termios
2137 * this function is called by the tty driver when it wants to change
2138 * the termios structure
2139 *****************************************************************************/
2141 static void mos7840_set_termios(struct usb_serial_port *port,
2142 struct ktermios *old_termios)
2146 struct usb_serial *serial;
2147 struct moschip_port *mos7840_port;
2148 struct tty_struct *tty;
2149 dbg("mos7840_set_termios: START\n");
2150 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2151 dbg("%s", "Invalid port \n");
2155 serial = port->serial;
2157 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
2158 dbg("%s", "Invalid Serial \n");
2162 mos7840_port = mos7840_get_port_private(port);
2164 if (mos7840_port == NULL)
2169 if (!port->tty || !port->tty->termios) {
2170 dbg("%s - no tty or termios", __FUNCTION__);
2174 if (!mos7840_port->open) {
2175 dbg("%s - port not opened", __FUNCTION__);
2179 dbg("%s\n", "setting termios - ");
2181 cflag = tty->termios->c_cflag;
2184 dbg("%s %s\n", __FUNCTION__, "cflag is NULL");
2188 /* check that they really want us to change something */
2190 if ((cflag == old_termios->c_cflag) &&
2191 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
2192 RELEVANT_IFLAG(old_termios->c_iflag))) {
2193 dbg("%s\n", "Nothing to change");
2198 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2199 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2202 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2203 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2206 dbg("%s - port %d", __FUNCTION__, port->number);
2208 /* change the port settings to the new ones specified */
2210 mos7840_change_port_settings(mos7840_port, old_termios);
2212 if (!mos7840_port->read_urb) {
2213 dbg("%s", "URB KILLED !!!!!\n");
2217 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2218 mos7840_port->read_urb->dev = serial->dev;
2219 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2221 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2228 /*****************************************************************************
2229 * mos7840_get_lsr_info - get line status register info
2231 * Purpose: Let user call ioctl() to get info when the UART physically
2232 * is emptied. On bus types like RS485, the transmitter must
2233 * release the bus after transmitting. This must be done when
2234 * the transmit shift register is empty, not be done when the
2235 * transmit holding register is empty. This functionality
2236 * allows an RS485 driver to be written in user space.
2237 *****************************************************************************/
2239 static int mos7840_get_lsr_info(struct moschip_port *mos7840_port,
2240 unsigned int __user *value)
2243 unsigned int result = 0;
2245 count = mos7840_chars_in_buffer(mos7840_port->port);
2247 dbg("%s -- Empty", __FUNCTION__);
2248 result = TIOCSER_TEMT;
2251 if (copy_to_user(value, &result, sizeof(int)))
2256 /*****************************************************************************
2257 * mos7840_get_bytes_avail - get number of bytes available
2259 * Purpose: Let user call ioctl to get the count of number of bytes available.
2260 *****************************************************************************/
2262 static int mos7840_get_bytes_avail(struct moschip_port *mos7840_port,
2263 unsigned int __user *value)
2265 unsigned int result = 0;
2266 struct tty_struct *tty = mos7840_port->port->tty;
2269 return -ENOIOCTLCMD;
2271 result = tty->read_cnt;
2273 dbg("%s(%d) = %d", __FUNCTION__, mos7840_port->port->number, result);
2274 if (copy_to_user(value, &result, sizeof(int)))
2277 return -ENOIOCTLCMD;
2280 /*****************************************************************************
2281 * mos7840_set_modem_info
2282 * function to set modem info
2283 *****************************************************************************/
2285 static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2286 unsigned int cmd, unsigned int __user *value)
2292 struct usb_serial_port *port;
2294 if (mos7840_port == NULL)
2297 port = (struct usb_serial_port *)mos7840_port->port;
2298 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2299 dbg("%s", "Invalid port \n");
2303 mcr = mos7840_port->shadowMCR;
2305 if (copy_from_user(&arg, value, sizeof(int)))
2310 if (arg & TIOCM_RTS)
2312 if (arg & TIOCM_DTR)
2314 if (arg & TIOCM_LOOP)
2315 mcr |= MCR_LOOPBACK;
2319 if (arg & TIOCM_RTS)
2321 if (arg & TIOCM_DTR)
2323 if (arg & TIOCM_LOOP)
2324 mcr &= ~MCR_LOOPBACK;
2328 /* turn off the RTS and DTR and LOOPBACK
2329 * and then only turn on what was asked to */
2330 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2331 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2332 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2333 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2337 mos7840_port->shadowMCR = mcr;
2339 Data = mos7840_port->shadowMCR;
2341 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2343 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2350 /*****************************************************************************
2351 * mos7840_get_modem_info
2352 * function to get modem info
2353 *****************************************************************************/
2355 static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
2356 unsigned int __user *value)
2358 unsigned int result = 0;
2360 unsigned int mcr = mos7840_port->shadowMCR;
2363 mos7840_get_uart_reg(mos7840_port->port, MODEM_STATUS_REGISTER,
2365 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2366 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2367 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2368 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2369 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2370 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2372 dbg("%s -- %x", __FUNCTION__, result);
2374 if (copy_to_user(value, &result, sizeof(int)))
2379 /*****************************************************************************
2380 * mos7840_get_serial_info
2381 * function to get information about serial port
2382 *****************************************************************************/
2384 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2385 struct serial_struct __user *retinfo)
2387 struct serial_struct tmp;
2389 if (mos7840_port == NULL)
2395 memset(&tmp, 0, sizeof(tmp));
2397 tmp.type = PORT_16550A;
2398 tmp.line = mos7840_port->port->serial->minor;
2399 tmp.port = mos7840_port->port->number;
2401 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2402 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2403 tmp.baud_base = 9600;
2404 tmp.close_delay = 5 * HZ;
2405 tmp.closing_wait = 30 * HZ;
2407 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2412 /*****************************************************************************
2414 * this function handles any ioctl calls to the driver
2415 *****************************************************************************/
2417 static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
2418 unsigned int cmd, unsigned long arg)
2420 void __user *argp = (void __user *)arg;
2421 struct moschip_port *mos7840_port;
2422 struct tty_struct *tty;
2424 struct async_icount cnow;
2425 struct async_icount cprev;
2426 struct serial_icounter_struct icount;
2429 struct tty_ldisc *ld;
2431 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2432 dbg("%s", "Invalid port \n");
2436 mos7840_port = mos7840_get_port_private(port);
2438 if (mos7840_port == NULL)
2441 tty = mos7840_port->port->tty;
2443 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2446 /* return number of bytes available */
2449 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
2450 return mos7840_get_bytes_avail(mos7840_port, argp);
2453 dbg("%s (%d) TIOCOUTQ", __FUNCTION__, port->number);
2454 return put_user(tty->driver->chars_in_buffer ?
2455 tty->driver->chars_in_buffer(tty) : 0,
2459 retval = tty_check_change(tty);
2463 ld = tty_ldisc_ref(tty);
2466 if (ld && ld->flush_buffer)
2467 ld->flush_buffer(tty);
2470 if (ld && ld->flush_buffer)
2471 ld->flush_buffer(tty);
2474 if (tty->driver->flush_buffer)
2475 tty->driver->flush_buffer(tty);
2478 tty_ldisc_deref(ld);
2481 tty_ldisc_deref(ld);
2485 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2486 return mos7840_get_lsr_info(mos7840_port, argp);
2492 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
2495 mos7840_set_modem_info(mos7840_port, cmd, argp);
2499 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
2500 return mos7840_get_modem_info(mos7840_port, argp);
2503 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2504 return mos7840_get_serial_info(mos7840_port, argp);
2507 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2511 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2512 cprev = mos7840_port->icount;
2514 //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
2515 mos7840_port->delta_msr_cond = 0;
2516 wait_event_interruptible(mos7840_port->delta_msr_wait,
2518 delta_msr_cond == 1));
2520 /* see if a signal did it */
2521 if (signal_pending(current))
2522 return -ERESTARTSYS;
2523 cnow = mos7840_port->icount;
2525 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2526 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2527 return -EIO; /* no change => error */
2528 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2529 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2530 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2531 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2540 cnow = mos7840_port->icount;
2542 icount.cts = cnow.cts;
2543 icount.dsr = cnow.dsr;
2544 icount.rng = cnow.rng;
2545 icount.dcd = cnow.dcd;
2546 icount.rx = cnow.rx;
2547 icount.tx = cnow.tx;
2548 icount.frame = cnow.frame;
2549 icount.overrun = cnow.overrun;
2550 icount.parity = cnow.parity;
2551 icount.brk = cnow.brk;
2552 icount.buf_overrun = cnow.buf_overrun;
2554 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2555 port->number, icount.rx, icount.tx);
2556 if (copy_to_user(argp, &icount, sizeof(icount)))
2566 return -ENOIOCTLCMD;
2569 static int mos7840_calc_num_ports(struct usb_serial *serial)
2571 int mos7840_num_ports = 0;
2573 dbg("numberofendpoints: %d \n",
2574 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
2575 dbg("numberofendpoints: %d \n",
2576 (int)serial->interface->altsetting->desc.bNumEndpoints);
2577 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2578 mos7840_num_ports = serial->num_ports = 2;
2579 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
2580 serial->num_bulk_in = 4;
2581 serial->num_bulk_out = 4;
2582 mos7840_num_ports = serial->num_ports = 4;
2585 return mos7840_num_ports;
2588 /****************************************************************************
2590 ****************************************************************************/
2592 static int mos7840_startup(struct usb_serial *serial)
2594 struct moschip_port *mos7840_port;
2595 struct usb_device *dev;
2599 dbg("%s \n", " mos7840_startup :entering..........");
2602 dbg("%s\n", "Invalid Handler");
2608 dbg("%s\n", "Entering...");
2610 /* we set up the pointers to the endpoints in the mos7840_open *
2611 * function, as the structures aren't created yet. */
2613 /* set up port private structures */
2614 for (i = 0; i < serial->num_ports; ++i) {
2615 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2616 if (mos7840_port == NULL) {
2617 err("%s - Out of memory", __FUNCTION__);
2619 i--; /* don't follow NULL pointer cleaning up */
2623 /* Initialize all port interrupt end point to port 0 int endpoint *
2624 * Our device has only one interrupt end point comman to all port */
2626 mos7840_port->port = serial->port[i];
2627 mos7840_set_port_private(serial->port[i], mos7840_port);
2628 spin_lock_init(&mos7840_port->pool_lock);
2630 mos7840_port->port_num = ((serial->port[i]->number -
2631 (serial->port[i]->serial->minor)) +
2634 if (mos7840_port->port_num == 1) {
2635 mos7840_port->SpRegOffset = 0x0;
2636 mos7840_port->ControlRegOffset = 0x1;
2637 mos7840_port->DcrRegOffset = 0x4;
2638 } else if ((mos7840_port->port_num == 2)
2639 && (serial->num_ports == 4)) {
2640 mos7840_port->SpRegOffset = 0x8;
2641 mos7840_port->ControlRegOffset = 0x9;
2642 mos7840_port->DcrRegOffset = 0x16;
2643 } else if ((mos7840_port->port_num == 2)
2644 && (serial->num_ports == 2)) {
2645 mos7840_port->SpRegOffset = 0xa;
2646 mos7840_port->ControlRegOffset = 0xb;
2647 mos7840_port->DcrRegOffset = 0x19;
2648 } else if ((mos7840_port->port_num == 3)
2649 && (serial->num_ports == 4)) {
2650 mos7840_port->SpRegOffset = 0xa;
2651 mos7840_port->ControlRegOffset = 0xb;
2652 mos7840_port->DcrRegOffset = 0x19;
2653 } else if ((mos7840_port->port_num == 4)
2654 && (serial->num_ports == 4)) {
2655 mos7840_port->SpRegOffset = 0xc;
2656 mos7840_port->ControlRegOffset = 0xd;
2657 mos7840_port->DcrRegOffset = 0x1c;
2659 mos7840_dump_serial_port(mos7840_port);
2661 mos7840_set_port_private(serial->port[i], mos7840_port);
2663 //enable rx_disable bit in control register
2666 mos7840_get_reg_sync(serial->port[i],
2667 mos7840_port->ControlRegOffset, &Data);
2669 dbg("Reading ControlReg failed status-0x%x\n", status);
2672 dbg("ControlReg Reading success val is %x, status%d\n",
2674 Data |= 0x08; //setting driver done bit
2675 Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
2677 //Data |= 0x20; //rx_disable bit
2680 mos7840_set_reg_sync(serial->port[i],
2681 mos7840_port->ControlRegOffset, Data);
2683 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2686 dbg("ControlReg Writing success(rx_disable) status%d\n",
2689 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2693 mos7840_set_reg_sync(serial->port[i],
2694 (__u16) (mos7840_port->DcrRegOffset +
2697 dbg("Writing DCR0 failed status-0x%x\n", status);
2700 dbg("DCR0 Writing success status%d\n", status);
2705 mos7840_set_reg_sync(serial->port[i],
2706 (__u16) (mos7840_port->DcrRegOffset +
2709 dbg("Writing DCR1 failed status-0x%x\n", status);
2712 dbg("DCR1 Writing success status%d\n", status);
2717 mos7840_set_reg_sync(serial->port[i],
2718 (__u16) (mos7840_port->DcrRegOffset +
2721 dbg("Writing DCR2 failed status-0x%x\n", status);
2724 dbg("DCR2 Writing success status%d\n", status);
2726 // write values in clkstart0x0 and clkmulti 0x20
2730 mos7840_set_reg_sync(serial->port[i],
2731 CLK_START_VALUE_REGISTER, Data);
2733 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2736 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2740 mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2743 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2747 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2750 //write value 0x0 to scratchpad register
2753 mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
2756 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2760 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2763 //Zero Length flag register
2764 if ((mos7840_port->port_num != 1)
2765 && (serial->num_ports == 2)) {
2769 status = mos7840_set_reg_sync(serial->port[i],
2775 dbg("ZLIP offset%x\n",
2777 ((__u16) mos7840_port->port_num)));
2779 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2783 dbg("ZLP_REG%d Writing success status%d\n",
2788 status = mos7840_set_reg_sync(serial->port[i],
2794 dbg("ZLIP offset%x\n",
2796 ((__u16) mos7840_port->port_num) - 0x1));
2798 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2802 dbg("ZLP_REG%d Writing success status%d\n",
2806 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2807 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2808 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
2809 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || !mos7840_port->dr) {
2815 //Zero Length flag enable
2817 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2819 dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
2822 dbg("ZLP_REG5 Writing success status%d\n", status);
2824 /* setting configuration feature to one */
2825 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2826 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2829 for (/* nothing */; i >= 0; i--) {
2830 mos7840_port = mos7840_get_port_private(serial->port[i]);
2832 kfree(mos7840_port->dr);
2833 kfree(mos7840_port->ctrl_buf);
2834 usb_free_urb(mos7840_port->control_urb);
2835 kfree(mos7840_port);
2836 serial->port[i] = NULL;
2841 /****************************************************************************
2843 * This function is called whenever the device is removed from the usb bus.
2844 ****************************************************************************/
2846 static void mos7840_shutdown(struct usb_serial *serial)
2849 unsigned long flags;
2850 struct moschip_port *mos7840_port;
2851 dbg("%s \n", " shutdown :entering..........");
2854 dbg("%s", "Invalid Handler \n");
2858 /* check for the ports to be closed,close the ports and disconnect */
2860 /* free private structure allocated for serial port *
2861 * stop reads and writes on all ports */
2863 for (i = 0; i < serial->num_ports; ++i) {
2864 mos7840_port = mos7840_get_port_private(serial->port[i]);
2865 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2866 mos7840_port->zombie = 1;
2867 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2868 usb_kill_urb(mos7840_port->control_urb);
2869 kfree(mos7840_port->ctrl_buf);
2870 kfree(mos7840_port->dr);
2871 kfree(mos7840_port);
2872 mos7840_set_port_private(serial->port[i], NULL);
2875 dbg("%s\n", "Thank u :: ");
2879 static struct usb_driver io_driver = {
2881 .probe = usb_serial_probe,
2882 .disconnect = usb_serial_disconnect,
2883 .id_table = moschip_id_table_combined,
2887 static struct usb_serial_driver moschip7840_4port_device = {
2889 .owner = THIS_MODULE,
2892 .description = DRIVER_DESC,
2893 .usb_driver = &io_driver,
2894 .id_table = moschip_port_id_table,
2895 .num_interrupt_in = 1, //NUM_DONT_CARE,//1,
2901 .open = mos7840_open,
2902 .close = mos7840_close,
2903 .write = mos7840_write,
2904 .write_room = mos7840_write_room,
2905 .chars_in_buffer = mos7840_chars_in_buffer,
2906 .throttle = mos7840_throttle,
2907 .unthrottle = mos7840_unthrottle,
2908 .calc_num_ports = mos7840_calc_num_ports,
2909 #ifdef MCSSerialProbe
2910 .probe = mos7840_serial_probe,
2912 .ioctl = mos7840_ioctl,
2913 .set_termios = mos7840_set_termios,
2914 .break_ctl = mos7840_break,
2915 .tiocmget = mos7840_tiocmget,
2916 .tiocmset = mos7840_tiocmset,
2917 .attach = mos7840_startup,
2918 .shutdown = mos7840_shutdown,
2919 .read_bulk_callback = mos7840_bulk_in_callback,
2920 .read_int_callback = mos7840_interrupt_callback,
2923 /****************************************************************************
2925 * This is called by the module subsystem, or on startup to initialize us
2926 ****************************************************************************/
2927 static int __init moschip7840_init(void)
2931 dbg("%s \n", " mos7840_init :entering..........");
2933 /* Register with the usb serial */
2934 retval = usb_serial_register(&moschip7840_4port_device);
2937 goto failed_port_device_register;
2939 dbg("%s\n", "Entring...");
2940 info(DRIVER_DESC " " DRIVER_VERSION);
2942 /* Register with the usb */
2943 retval = usb_register(&io_driver);
2946 goto failed_usb_register;
2949 dbg("%s\n", "Leaving...");
2953 failed_usb_register:
2954 usb_serial_deregister(&moschip7840_4port_device);
2956 failed_port_device_register:
2961 /****************************************************************************
2963 * Called when the driver is about to be unloaded.
2964 ****************************************************************************/
2965 static void __exit moschip7840_exit(void)
2968 dbg("%s \n", " mos7840_exit :entering..........");
2970 usb_deregister(&io_driver);
2972 usb_serial_deregister(&moschip7840_4port_device);
2974 dbg("%s\n", "Entring...");
2977 module_init(moschip7840_init);
2978 module_exit(moschip7840_exit);
2980 /* Module information */
2981 MODULE_DESCRIPTION(DRIVER_DESC);
2982 MODULE_LICENSE("GPL");
2984 module_param(debug, bool, S_IRUGO | S_IWUSR);
2985 MODULE_PARM_DESC(debug, "Debug enabled or not");