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;
437 int status = urb->status;
440 dbg("%s", "Invalid Pointer !!!!:\n");
444 mos7840_port = (struct moschip_port *)urb->context;
453 /* this urb is terminated, clean up */
454 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
458 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
463 dbg("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
464 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__,
465 mos7840_port->MsrLsr, mos7840_port->port_num);
466 data = urb->transfer_buffer;
467 regval = (__u8) data[0];
468 dbg("%s data is %x\n", __FUNCTION__, regval);
469 if (mos7840_port->MsrLsr == 0)
470 mos7840_handle_new_msr(mos7840_port, regval);
471 else if (mos7840_port->MsrLsr == 1)
472 mos7840_handle_new_lsr(mos7840_port, regval);
475 spin_lock(&mos7840_port->pool_lock);
476 if (!mos7840_port->zombie)
477 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
478 spin_unlock(&mos7840_port->pool_lock);
480 dev_err(&urb->dev->dev,
481 "%s - Error %d submitting interrupt urb\n",
482 __FUNCTION__, result);
486 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
489 struct usb_device *dev = mcs->port->serial->dev;
490 struct usb_ctrlrequest *dr = mcs->dr;
491 unsigned char *buffer = mcs->ctrl_buf;
494 dr->bRequestType = MCS_RD_RTYPE;
495 dr->bRequest = MCS_RDREQ;
496 dr->wValue = cpu_to_le16(Wval); //0;
497 dr->wIndex = cpu_to_le16(reg);
498 dr->wLength = cpu_to_le16(2);
500 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
501 (unsigned char *)dr, buffer, 2,
502 mos7840_control_callback, mcs);
503 mcs->control_urb->transfer_buffer_length = 2;
504 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
508 /*****************************************************************************
509 * mos7840_interrupt_callback
510 * this is the callback function for when we have received data on the
511 * interrupt endpoint.
512 *****************************************************************************/
514 static void mos7840_interrupt_callback(struct urb *urb)
518 struct moschip_port *mos7840_port;
519 struct usb_serial *serial;
524 __u16 wval, wreg = 0;
525 int status = urb->status;
527 dbg("%s", " : Entering\n");
529 dbg("%s", "Invalid Pointer !!!!:\n");
540 /* this urb is terminated, clean up */
541 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
545 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
550 length = urb->actual_length;
551 data = urb->transfer_buffer;
553 serial = (struct usb_serial *)urb->context;
555 /* Moschip get 5 bytes
556 * Byte 1 IIR Port 1 (port.number is 0)
557 * Byte 2 IIR Port 2 (port.number is 1)
558 * Byte 3 IIR Port 3 (port.number is 2)
559 * Byte 4 IIR Port 4 (port.number is 3)
560 * Byte 5 FIFO status for both */
562 if (length && length > 5) {
563 dbg("%s \n", "Wrong data !!!");
567 sp[0] = (__u8) data[0];
568 sp[1] = (__u8) data[1];
569 sp[2] = (__u8) data[2];
570 sp[3] = (__u8) data[3];
573 for (i = 0; i < serial->num_ports; i++) {
574 mos7840_port = mos7840_get_port_private(serial->port[i]);
576 (((__u16) serial->port[i]->number -
577 (__u16) (serial->minor)) + 1) << 8;
578 if (mos7840_port->open) {
580 dbg("SP%d No Interrupt !!!\n", i);
582 switch (sp[i] & 0x0f) {
584 dbg("Serial Port %d: Receiver status error or ", i);
585 dbg("address bit detected in 9-bit mode\n");
586 mos7840_port->MsrLsr = 1;
587 wreg = LINE_STATUS_REGISTER;
590 dbg("Serial Port %d: Modem status change\n", i);
591 mos7840_port->MsrLsr = 0;
592 wreg = MODEM_STATUS_REGISTER;
595 spin_lock(&mos7840_port->pool_lock);
596 if (!mos7840_port->zombie) {
597 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
599 spin_unlock(&mos7840_port->pool_lock);
602 spin_unlock(&mos7840_port->pool_lock);
606 if (!(rv < 0)) /* the completion handler for the control urb will resubmit */
609 result = usb_submit_urb(urb, GFP_ATOMIC);
611 dev_err(&urb->dev->dev,
612 "%s - Error %d submitting interrupt urb\n",
613 __FUNCTION__, result);
617 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
618 const char *function)
621 dbg("%s - port == NULL", function);
625 dbg("%s - port->serial == NULL", function);
632 /* Inline functions to check the sanity of a pointer that is passed to us */
633 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
634 const char *function)
637 dbg("%s - serial == NULL", function);
641 dbg("%s - serial->type == NULL!", function);
648 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
649 const char *function)
651 /* if no port was specified, or it fails a paranoia check */
653 mos7840_port_paranoia_check(port, function) ||
654 mos7840_serial_paranoia_check(port->serial, function)) {
655 /* then say that we don't have a valid usb_serial thing, which will
656 * end up genrating -ENODEV return values */
663 /*****************************************************************************
664 * mos7840_bulk_in_callback
665 * this is the callback function for when we have received data on the
667 *****************************************************************************/
669 static void mos7840_bulk_in_callback(struct urb *urb)
673 struct usb_serial *serial;
674 struct usb_serial_port *port;
675 struct moschip_port *mos7840_port;
676 struct tty_struct *tty;
677 int status = urb->status;
680 dbg("%s", "Invalid Pointer !!!!:\n");
685 dbg("nonzero read bulk status received: %d", status);
689 mos7840_port = (struct moschip_port *)urb->context;
691 dbg("%s", "NULL mos7840_port pointer \n");
695 port = (struct usb_serial_port *)mos7840_port->port;
696 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
697 dbg("%s", "Port Paranoia failed \n");
701 serial = mos7840_get_usb_serial(port, __FUNCTION__);
703 dbg("%s\n", "Bad serial pointer ");
707 dbg("%s\n", "Entering... \n");
709 data = urb->transfer_buffer;
711 dbg("%s", "Entering ........... \n");
713 if (urb->actual_length) {
714 tty = mos7840_port->port->tty;
716 tty_buffer_request_room(tty, urb->actual_length);
717 tty_insert_flip_string(tty, data, urb->actual_length);
719 tty_flip_buffer_push(tty);
721 mos7840_port->icount.rx += urb->actual_length;
723 dbg("mos7840_port->icount.rx is %d:\n",
724 mos7840_port->icount.rx);
727 if (!mos7840_port->read_urb) {
728 dbg("%s", "URB KILLED !!!\n");
733 mos7840_port->read_urb->dev = serial->dev;
735 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
738 dbg(" usb_submit_urb(read bulk) failed, retval = %d",
743 /*****************************************************************************
744 * mos7840_bulk_out_data_callback
745 * this is the callback function for when we have finished sending serial data
746 * on the bulk out endpoint.
747 *****************************************************************************/
749 static void mos7840_bulk_out_data_callback(struct urb *urb)
751 struct moschip_port *mos7840_port;
752 struct tty_struct *tty;
753 int status = urb->status;
757 dbg("%s", "Invalid Pointer !!!!:\n");
761 mos7840_port = (struct moschip_port *)urb->context;
762 spin_lock(&mos7840_port->pool_lock);
763 for (i = 0; i < NUM_URBS; i++) {
764 if (urb == mos7840_port->write_urb_pool[i]) {
765 mos7840_port->busy[i] = 0;
769 spin_unlock(&mos7840_port->pool_lock);
772 dbg("nonzero write bulk status received:%d\n", status);
776 if (mos7840_port_paranoia_check(mos7840_port->port, __FUNCTION__)) {
777 dbg("%s", "Port Paranoia failed \n");
781 dbg("%s \n", "Entering .........");
783 tty = mos7840_port->port->tty;
785 if (tty && mos7840_port->open)
790 /************************************************************************/
791 /* 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 */
792 /************************************************************************/
793 #ifdef MCSSerialProbe
794 static int mos7840_serial_probe(struct usb_serial *serial,
795 const struct usb_device_id *id)
798 /*need to implement the mode_reg reading and updating\
799 structures usb_serial_ device_type\
800 (i.e num_ports, num_bulkin,bulkout etc) */
801 /* Also we can update the changes attach */
806 /*****************************************************************************
808 * this function is called by the tty driver when a port is opened
809 * If successful, we return 0
810 * Otherwise we return a negative error number.
811 *****************************************************************************/
813 static int mos7840_open(struct usb_serial_port *port, struct file *filp)
817 struct usb_serial *serial;
821 struct moschip_port *mos7840_port;
822 struct moschip_port *port0;
824 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
825 dbg("%s", "Port Paranoia failed \n");
829 serial = port->serial;
831 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
832 dbg("%s", "Serial Paranoia failed \n");
836 mos7840_port = mos7840_get_port_private(port);
837 port0 = mos7840_get_port_private(serial->port[0]);
839 if (mos7840_port == NULL || port0 == NULL)
842 usb_clear_halt(serial->dev, port->write_urb->pipe);
843 usb_clear_halt(serial->dev, port->read_urb->pipe);
846 /* Initialising the write urb pool */
847 for (j = 0; j < NUM_URBS; ++j) {
848 urb = usb_alloc_urb(0, GFP_KERNEL);
849 mos7840_port->write_urb_pool[j] = urb;
852 err("No more urbs???");
856 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
857 if (!urb->transfer_buffer) {
859 mos7840_port->write_urb_pool[j] = NULL;
860 err("%s-out of memory for urb buffers.", __FUNCTION__);
865 /*****************************************************************************
866 * Initialize MCS7840 -- Write Init values to corresponding Registers
874 * 0x08 : SP1/2 Control Reg
875 *****************************************************************************/
877 //NEED to check the following Block
881 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
883 dbg("Reading Spreg failed\n");
887 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
889 dbg("writing Spreg failed\n");
894 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
896 dbg("writing Spreg failed\n");
899 //End of block to be checked
904 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
906 dbg("Reading Controlreg failed\n");
909 Data |= 0x08; //Driver done bit
910 Data |= 0x20; //rx_disable
911 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
913 dbg("writing Controlreg failed\n");
916 //do register settings here
917 // Set all regs to the device default values.
918 ////////////////////////////////////
919 // First Disable all interrupts.
920 ////////////////////////////////////
923 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
925 dbg("disableing interrupts failed\n");
928 // Set FIFO_CONTROL_REGISTER to the default value
930 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
932 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
937 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
939 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
944 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
945 mos7840_port->shadowLCR = Data;
948 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
949 mos7840_port->shadowMCR = Data;
952 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
953 mos7840_port->shadowLCR = Data;
955 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
956 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
960 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
964 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
968 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
970 Data = Data & ~SERIAL_LCR_DLAB;
972 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
973 mos7840_port->shadowLCR = Data;
975 //clearing Bulkin and Bulkout Fifo
978 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
982 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
986 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
987 //Finally enable all interrupts
991 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
993 //clearing rx_disable
997 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
1001 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1007 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
1011 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1013 /* force low_latency on so that our tty_push actually forces *
1014 * the data through,otherwise it is scheduled, and with *
1015 * high data rates (like with OHCI) data can get lost. */
1018 port->tty->low_latency = 1;
1019 /* Check to see if we've set up our endpoint info yet *
1020 * (can't set it up in mos7840_startup as the structures *
1021 * were not set up at that time.) */
1022 if (port0->open_ports == 1) {
1023 if (serial->port[0]->interrupt_in_buffer == NULL) {
1025 /* set up interrupt urb */
1027 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1029 usb_rcvintpipe(serial->dev,
1031 interrupt_in_endpointAddress),
1032 serial->port[0]->interrupt_in_buffer,
1033 serial->port[0]->interrupt_in_urb->
1034 transfer_buffer_length,
1035 mos7840_interrupt_callback,
1037 serial->port[0]->interrupt_in_urb->
1040 /* start interrupt read for mos7840 *
1041 * will continue as long as mos7840 is connected */
1044 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1047 err("%s - Error %d submitting interrupt urb",
1048 __FUNCTION__, response);
1055 /* see if we've set up our endpoint info yet *
1056 * (can't set it up in mos7840_startup as the *
1057 * structures were not set up at that time.) */
1059 dbg("port number is %d \n", port->number);
1060 dbg("serial number is %d \n", port->serial->minor);
1061 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1062 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1063 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
1064 dbg("port's number in the device is %d\n", mos7840_port->port_num);
1065 mos7840_port->read_urb = port->read_urb;
1067 /* set up our bulk in urb */
1069 usb_fill_bulk_urb(mos7840_port->read_urb,
1071 usb_rcvbulkpipe(serial->dev,
1072 port->bulk_in_endpointAddress),
1073 port->bulk_in_buffer,
1074 mos7840_port->read_urb->transfer_buffer_length,
1075 mos7840_bulk_in_callback, mos7840_port);
1077 dbg("mos7840_open: bulkin endpoint is %d\n",
1078 port->bulk_in_endpointAddress);
1079 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1081 err("%s - Error %d submitting control urb", __FUNCTION__,
1085 /* initialize our wait queues */
1086 init_waitqueue_head(&mos7840_port->wait_chase);
1087 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1089 /* initialize our icount structure */
1090 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1092 /* initialize our port settings */
1093 mos7840_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
1094 /* send a open port command */
1095 mos7840_port->open = 1;
1096 //mos7840_change_port_settings(mos7840_port,old_termios);
1097 mos7840_port->icount.tx = 0;
1098 mos7840_port->icount.rx = 0;
1100 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial, mos7840_port, port);
1106 /*****************************************************************************
1107 * mos7840_chars_in_buffer
1108 * this function is called by the tty driver when it wants to know how many
1109 * bytes of data we currently have outstanding in the port (data that has
1110 * been written, but hasn't made it out the port yet)
1111 * If successful, we return the number of bytes left to be written in the
1113 * Otherwise we return a negative error number.
1114 *****************************************************************************/
1116 static int mos7840_chars_in_buffer(struct usb_serial_port *port)
1120 unsigned long flags;
1121 struct moschip_port *mos7840_port;
1123 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
1125 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1126 dbg("%s", "Invalid port \n");
1130 mos7840_port = mos7840_get_port_private(port);
1131 if (mos7840_port == NULL) {
1132 dbg("%s \n", "mos7840_break:leaving ...........");
1136 spin_lock_irqsave(&mos7840_port->pool_lock,flags);
1137 for (i = 0; i < NUM_URBS; ++i) {
1138 if (mos7840_port->busy[i]) {
1139 chars += URB_TRANSFER_BUFFER_SIZE;
1142 spin_unlock_irqrestore(&mos7840_port->pool_lock,flags);
1143 dbg("%s - returns %d", __FUNCTION__, chars);
1148 /************************************************************************
1150 * mos7840_block_until_tx_empty
1152 * This function will block the close until one of the following:
1154 * 2. The mos7840 has stopped
1155 * 3. A timout of 3 seconds without activity has expired
1157 ************************************************************************/
1158 static void mos7840_block_until_tx_empty(struct moschip_port *mos7840_port)
1160 int timeout = HZ / 10;
1166 count = mos7840_chars_in_buffer(mos7840_port->port);
1168 /* Check for Buffer status */
1173 /* Block the thread for a while */
1174 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1177 /* No activity.. count down section */
1180 dbg("%s - TIMEOUT", __FUNCTION__);
1183 /* Reset timout value back to seconds */
1189 /*****************************************************************************
1191 * this function is called by the tty driver when a port is closed
1192 *****************************************************************************/
1194 static void mos7840_close(struct usb_serial_port *port, struct file *filp)
1196 struct usb_serial *serial;
1197 struct moschip_port *mos7840_port;
1198 struct moschip_port *port0;
1202 dbg("%s\n", "mos7840_close:entering...");
1204 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1205 dbg("%s", "Port Paranoia failed \n");
1209 serial = mos7840_get_usb_serial(port, __FUNCTION__);
1211 dbg("%s", "Serial Paranoia failed \n");
1215 mos7840_port = mos7840_get_port_private(port);
1216 port0 = mos7840_get_port_private(serial->port[0]);
1218 if (mos7840_port == NULL || port0 == NULL)
1221 for (j = 0; j < NUM_URBS; ++j)
1222 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1224 /* Freeing Write URBs */
1225 for (j = 0; j < NUM_URBS; ++j) {
1226 if (mos7840_port->write_urb_pool[j]) {
1227 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1228 kfree(mos7840_port->write_urb_pool[j]->
1231 usb_free_urb(mos7840_port->write_urb_pool[j]);
1236 /* flush and block until tx is empty */
1237 mos7840_block_until_tx_empty(mos7840_port);
1240 /* While closing port, shutdown all bulk read, write *
1241 * and interrupt read if they exists */
1244 if (mos7840_port->write_urb) {
1245 dbg("%s", "Shutdown bulk write\n");
1246 usb_kill_urb(mos7840_port->write_urb);
1249 if (mos7840_port->read_urb) {
1250 dbg("%s", "Shutdown bulk read\n");
1251 usb_kill_urb(mos7840_port->read_urb);
1253 if ((&mos7840_port->control_urb)) {
1254 dbg("%s", "Shutdown control read\n");
1255 // usb_kill_urb (mos7840_port->control_urb);
1259 // if(mos7840_port->ctrl_buf != NULL)
1260 // kfree(mos7840_port->ctrl_buf);
1261 port0->open_ports--;
1262 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1263 port0->open_ports, port->number);
1264 if (port0->open_ports == 0) {
1265 if (serial->port[0]->interrupt_in_urb) {
1266 dbg("%s", "Shutdown interrupt_in_urb\n");
1267 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1271 if (mos7840_port->write_urb) {
1272 /* if this urb had a transfer buffer already (old tx) free it */
1274 if (mos7840_port->write_urb->transfer_buffer != NULL) {
1275 kfree(mos7840_port->write_urb->transfer_buffer);
1277 usb_free_urb(mos7840_port->write_urb);
1281 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1284 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1286 mos7840_port->open = 0;
1288 dbg("%s \n", "Leaving ............");
1291 /************************************************************************
1293 * mos7840_block_until_chase_response
1295 * This function will block the close until one of the following:
1296 * 1. Response to our Chase comes from mos7840
1297 * 2. A timout of 10 seconds without activity has expired
1298 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1300 ************************************************************************/
1302 static void mos7840_block_until_chase_response(struct moschip_port
1305 int timeout = 1 * HZ;
1310 count = mos7840_chars_in_buffer(mos7840_port->port);
1312 /* Check for Buffer status */
1317 /* Block the thread for a while */
1318 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1320 /* No activity.. count down section */
1323 dbg("%s - TIMEOUT", __FUNCTION__);
1326 /* Reset timout value back to seconds */
1333 /*****************************************************************************
1335 * this function sends a break to the port
1336 *****************************************************************************/
1337 static void mos7840_break(struct usb_serial_port *port, int break_state)
1340 struct usb_serial *serial;
1341 struct moschip_port *mos7840_port;
1343 dbg("%s \n", "Entering ...........");
1344 dbg("mos7840_break: Start\n");
1346 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1347 dbg("%s", "Port Paranoia failed \n");
1351 serial = mos7840_get_usb_serial(port, __FUNCTION__);
1353 dbg("%s", "Serial Paranoia failed \n");
1357 mos7840_port = mos7840_get_port_private(port);
1359 if (mos7840_port == NULL) {
1365 /* flush and block until tx is empty */
1366 mos7840_block_until_chase_response(mos7840_port);
1369 if (break_state == -1) {
1370 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1372 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1375 mos7840_port->shadowLCR = data;
1376 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1377 mos7840_port->shadowLCR);
1378 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1379 mos7840_port->shadowLCR);
1384 /*****************************************************************************
1385 * mos7840_write_room
1386 * this function is called by the tty driver when it wants to know how many
1387 * bytes of data we can accept for a specific port.
1388 * If successful, we return the amount of room that we have for this port
1389 * Otherwise we return a negative error number.
1390 *****************************************************************************/
1392 static int mos7840_write_room(struct usb_serial_port *port)
1396 unsigned long flags;
1397 struct moschip_port *mos7840_port;
1399 dbg("%s \n", " mos7840_write_room:entering ...........");
1401 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1402 dbg("%s", "Invalid port \n");
1403 dbg("%s \n", " mos7840_write_room:leaving ...........");
1407 mos7840_port = mos7840_get_port_private(port);
1408 if (mos7840_port == NULL) {
1409 dbg("%s \n", "mos7840_break:leaving ...........");
1413 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1414 for (i = 0; i < NUM_URBS; ++i) {
1415 if (!mos7840_port->busy[i]) {
1416 room += URB_TRANSFER_BUFFER_SIZE;
1419 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1421 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1422 dbg("%s - returns %d", __FUNCTION__, room);
1427 /*****************************************************************************
1429 * this function is called by the tty driver when data should be written to
1431 * If successful, we return the number of bytes written, otherwise we
1432 * return a negative error number.
1433 *****************************************************************************/
1435 static int mos7840_write(struct usb_serial_port *port,
1436 const unsigned char *data, int count)
1442 unsigned long flags;
1444 struct moschip_port *mos7840_port;
1445 struct usb_serial *serial;
1448 const unsigned char *current_position = data;
1449 unsigned char *data1;
1450 dbg("%s \n", "entering ...........");
1451 //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
1456 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1457 mos7840_port->shadowLCR = Data;
1458 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
1459 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1460 mos7840_port->shadowLCR);
1463 //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
1464 //mos7840_port->shadowLCR=Data;//Need to add later
1466 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1468 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1471 //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
1474 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1475 dbg("mos7840_write:DLL value is %x\n", Data);
1479 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1480 dbg("mos7840_write:DLM value is %x\n", Data);
1482 Data = Data & ~SERIAL_LCR_DLAB;
1483 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1484 mos7840_port->shadowLCR);
1486 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1489 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1490 dbg("%s", "Port Paranoia failed \n");
1494 serial = port->serial;
1495 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
1496 dbg("%s", "Serial Paranoia failed \n");
1500 mos7840_port = mos7840_get_port_private(port);
1501 if (mos7840_port == NULL) {
1502 dbg("%s", "mos7840_port is NULL\n");
1506 /* try to find a free urb in the list */
1509 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1510 for (i = 0; i < NUM_URBS; ++i) {
1511 if (!mos7840_port->busy[i]) {
1512 mos7840_port->busy[i] = 1;
1513 urb = mos7840_port->write_urb_pool[i];
1518 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1521 dbg("%s - no more free urbs", __FUNCTION__);
1525 if (urb->transfer_buffer == NULL) {
1526 urb->transfer_buffer =
1527 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1529 if (urb->transfer_buffer == NULL) {
1530 err("%s no more kernel memory...", __FUNCTION__);
1534 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1536 memcpy(urb->transfer_buffer, current_position, transfer_size);
1538 /* fill urb with data and submit */
1539 usb_fill_bulk_urb(urb,
1541 usb_sndbulkpipe(serial->dev,
1542 port->bulk_out_endpointAddress),
1543 urb->transfer_buffer,
1545 mos7840_bulk_out_data_callback, mos7840_port);
1547 data1 = urb->transfer_buffer;
1548 dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1550 /* send it down the pipe */
1551 status = usb_submit_urb(urb, GFP_ATOMIC);
1554 mos7840_port->busy[i] = 0;
1555 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1556 __FUNCTION__, status);
1557 bytes_sent = status;
1560 bytes_sent = transfer_size;
1561 mos7840_port->icount.tx += transfer_size;
1563 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1570 /*****************************************************************************
1572 * this function is called by the tty driver when it wants to stop the data
1573 * being read from the port.
1574 *****************************************************************************/
1576 static void mos7840_throttle(struct usb_serial_port *port)
1578 struct moschip_port *mos7840_port;
1579 struct tty_struct *tty;
1582 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1583 dbg("%s", "Invalid port \n");
1587 dbg("- port %d\n", port->number);
1589 mos7840_port = mos7840_get_port_private(port);
1591 if (mos7840_port == NULL)
1594 if (!mos7840_port->open) {
1595 dbg("%s\n", "port not opened");
1599 dbg("%s", "Entering .......... \n");
1603 dbg("%s - no tty available", __FUNCTION__);
1607 /* if we are implementing XON/XOFF, send the stop character */
1609 unsigned char stop_char = STOP_CHAR(tty);
1610 status = mos7840_write(port, &stop_char, 1);
1616 /* if we are implementing RTS/CTS, toggle that line */
1617 if (tty->termios->c_cflag & CRTSCTS) {
1618 mos7840_port->shadowMCR &= ~MCR_RTS;
1621 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1622 mos7840_port->shadowMCR);
1632 /*****************************************************************************
1633 * mos7840_unthrottle
1634 * this function is called by the tty driver when it wants to resume the data
1635 * being read from the port (called after SerialThrottle is called)
1636 *****************************************************************************/
1637 static void mos7840_unthrottle(struct usb_serial_port *port)
1639 struct tty_struct *tty;
1641 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1643 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1644 dbg("%s", "Invalid port \n");
1648 if (mos7840_port == NULL)
1651 if (!mos7840_port->open) {
1652 dbg("%s - port not opened", __FUNCTION__);
1656 dbg("%s", "Entering .......... \n");
1660 dbg("%s - no tty available", __FUNCTION__);
1664 /* if we are implementing XON/XOFF, send the start character */
1666 unsigned char start_char = START_CHAR(tty);
1667 status = mos7840_write(port, &start_char, 1);
1673 /* if we are implementing RTS/CTS, toggle that line */
1674 if (tty->termios->c_cflag & CRTSCTS) {
1675 mos7840_port->shadowMCR |= MCR_RTS;
1678 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1679 mos7840_port->shadowMCR);
1688 static int mos7840_tiocmget(struct usb_serial_port *port, struct file *file)
1690 struct moschip_port *mos7840_port;
1691 unsigned int result;
1695 mos7840_port = mos7840_get_port_private(port);
1697 dbg("%s - port %d", __FUNCTION__, port->number);
1699 if (mos7840_port == NULL)
1702 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1703 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1704 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1705 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1706 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1707 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1708 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1709 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1710 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1712 dbg("%s - 0x%04X", __FUNCTION__, result);
1717 static int mos7840_tiocmset(struct usb_serial_port *port, struct file *file,
1718 unsigned int set, unsigned int clear)
1720 struct moschip_port *mos7840_port;
1722 unsigned int status;
1724 dbg("%s - port %d", __FUNCTION__, port->number);
1726 mos7840_port = mos7840_get_port_private(port);
1728 if (mos7840_port == NULL)
1731 mcr = mos7840_port->shadowMCR;
1732 if (clear & TIOCM_RTS)
1734 if (clear & TIOCM_DTR)
1736 if (clear & TIOCM_LOOP)
1737 mcr &= ~MCR_LOOPBACK;
1739 if (set & TIOCM_RTS)
1741 if (set & TIOCM_DTR)
1743 if (set & TIOCM_LOOP)
1744 mcr |= MCR_LOOPBACK;
1746 mos7840_port->shadowMCR = mcr;
1749 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1751 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
1758 /*****************************************************************************
1759 * mos7840_calc_baud_rate_divisor
1760 * this function calculates the proper baud rate divisor for the specified
1762 *****************************************************************************/
1763 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1764 __u16 * clk_sel_val)
1767 dbg("%s - %d", __FUNCTION__, baudRate);
1769 if (baudRate <= 115200) {
1770 *divisor = 115200 / baudRate;
1773 if ((baudRate > 115200) && (baudRate <= 230400)) {
1774 *divisor = 230400 / baudRate;
1775 *clk_sel_val = 0x10;
1776 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1777 *divisor = 403200 / baudRate;
1778 *clk_sel_val = 0x20;
1779 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1780 *divisor = 460800 / baudRate;
1781 *clk_sel_val = 0x30;
1782 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1783 *divisor = 806400 / baudRate;
1784 *clk_sel_val = 0x40;
1785 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1786 *divisor = 921600 / baudRate;
1787 *clk_sel_val = 0x50;
1788 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1789 *divisor = 1572864 / baudRate;
1790 *clk_sel_val = 0x60;
1791 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1792 *divisor = 3145728 / baudRate;
1793 *clk_sel_val = 0x70;
1799 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1800 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1801 *divisor = mos7840_divisor_table[i].Divisor;
1806 /* After trying for all the standard baud rates *
1807 * Try calculating the divisor for this baud rate */
1809 if (baudrate > 75 && baudrate < 230400) {
1810 /* get the divisor */
1811 custom = (__u16) (230400L / baudrate);
1813 /* Check for round off */
1814 round1 = (__u16) (2304000L / baudrate);
1815 round = (__u16) (round1 - (custom * 10));
1821 dbg(" Baud %d = %d\n", baudrate, custom);
1825 dbg("%s\n", " Baud calculation Failed...");
1830 /*****************************************************************************
1831 * mos7840_send_cmd_write_baud_rate
1832 * this function sends the proper command to change the baud rate of the
1834 *****************************************************************************/
1836 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1842 unsigned char number;
1844 struct usb_serial_port *port;
1846 if (mos7840_port == NULL)
1849 port = (struct usb_serial_port *)mos7840_port->port;
1850 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1851 dbg("%s", "Invalid port \n");
1855 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1856 dbg("%s", "Invalid Serial \n");
1860 dbg("%s", "Entering .......... \n");
1862 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1864 dbg("%s - port = %d, baud = %d", __FUNCTION__,
1865 mos7840_port->port->number, baudRate);
1866 //reset clk_uart_sel in spregOffset
1867 if (baudRate > 115200) {
1868 #ifdef HW_flow_control
1869 //NOTE: need to see the pther register to modify
1870 //setting h/w flow control bit to 1;
1873 mos7840_port->shadowMCR = Data;
1875 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1877 dbg("Writing spreg failed in set_serial_baud\n");
1883 #ifdef HW_flow_control
1884 //setting h/w flow control bit to 0;
1887 mos7840_port->shadowMCR = Data;
1889 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1891 dbg("Writing spreg failed in set_serial_baud\n");
1898 if (1) //baudRate <= 115200)
1904 mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1907 mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1910 dbg("reading spreg failed in set_serial_baud\n");
1913 Data = (Data & 0x8f) | clk_sel_val;
1916 mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1918 dbg("Writing spreg failed in set_serial_baud\n");
1921 /* Calculate the Divisor */
1924 err("%s - bad baud rate", __FUNCTION__);
1925 dbg("%s\n", "bad baud rate");
1928 /* Enable access to divisor latch */
1929 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1930 mos7840_port->shadowLCR = Data;
1931 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1933 /* Write the divisor */
1934 Data = (unsigned char)(divisor & 0xff);
1935 dbg("set_serial_baud Value to write DLL is %x\n", Data);
1936 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1938 Data = (unsigned char)((divisor & 0xff00) >> 8);
1939 dbg("set_serial_baud Value to write DLM is %x\n", Data);
1940 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1942 /* Disable access to divisor latch */
1943 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1944 mos7840_port->shadowLCR = Data;
1945 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1952 /*****************************************************************************
1953 * mos7840_change_port_settings
1954 * This routine is called to set the UART on the device to match
1955 * the specified new settings.
1956 *****************************************************************************/
1958 static void mos7840_change_port_settings(struct moschip_port *mos7840_port,
1959 struct ktermios *old_termios)
1961 struct tty_struct *tty;
1970 struct usb_serial_port *port;
1971 struct usb_serial *serial;
1973 if (mos7840_port == NULL)
1976 port = (struct usb_serial_port *)mos7840_port->port;
1978 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1979 dbg("%s", "Invalid port \n");
1983 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1984 dbg("%s", "Invalid Serial \n");
1988 serial = port->serial;
1990 dbg("%s - port %d", __FUNCTION__, mos7840_port->port->number);
1992 if (!mos7840_port->open) {
1993 dbg("%s - port not opened", __FUNCTION__);
1997 tty = mos7840_port->port->tty;
1999 if ((!tty) || (!tty->termios)) {
2000 dbg("%s - no tty structures", __FUNCTION__);
2004 dbg("%s", "Entering .......... \n");
2008 lParity = LCR_PAR_NONE;
2010 cflag = tty->termios->c_cflag;
2011 iflag = tty->termios->c_iflag;
2013 /* Change the number of bits */
2014 if (cflag & CSIZE) {
2015 switch (cflag & CSIZE) {
2033 /* Change the Parity bit */
2034 if (cflag & PARENB) {
2035 if (cflag & PARODD) {
2036 lParity = LCR_PAR_ODD;
2037 dbg("%s - parity = odd", __FUNCTION__);
2039 lParity = LCR_PAR_EVEN;
2040 dbg("%s - parity = even", __FUNCTION__);
2044 dbg("%s - parity = none", __FUNCTION__);
2047 if (cflag & CMSPAR) {
2048 lParity = lParity | 0x20;
2051 /* Change the Stop bit */
2052 if (cflag & CSTOPB) {
2054 dbg("%s - stop bits = 2", __FUNCTION__);
2057 dbg("%s - stop bits = 1", __FUNCTION__);
2060 /* Update the LCR with the correct value */
2061 mos7840_port->shadowLCR &=
2062 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2063 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2065 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2066 mos7840_port->shadowLCR);
2067 /* Disable Interrupts */
2069 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2072 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2075 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2077 /* Send the updated LCR value to the mos7840 */
2078 Data = mos7840_port->shadowLCR;
2080 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2083 mos7840_port->shadowMCR = Data;
2084 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2086 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2088 /* set up the MCR register and send it to the mos7840 */
2090 mos7840_port->shadowMCR = MCR_MASTER_IE;
2091 if (cflag & CBAUD) {
2092 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2095 if (cflag & CRTSCTS) {
2096 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2099 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2102 Data = mos7840_port->shadowMCR;
2103 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2105 /* Determine divisor based on baud rate */
2106 baud = tty_get_baud_rate(tty);
2109 /* pick a default, any default... */
2110 dbg("%s\n", "Picked default baud...");
2114 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2115 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2117 /* Enable Interrupts */
2119 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2121 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2122 mos7840_port->read_urb->dev = serial->dev;
2124 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2127 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2131 wake_up(&mos7840_port->delta_msr_wait);
2132 mos7840_port->delta_msr_cond = 1;
2133 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2134 mos7840_port->shadowLCR);
2139 /*****************************************************************************
2140 * mos7840_set_termios
2141 * this function is called by the tty driver when it wants to change
2142 * the termios structure
2143 *****************************************************************************/
2145 static void mos7840_set_termios(struct usb_serial_port *port,
2146 struct ktermios *old_termios)
2150 struct usb_serial *serial;
2151 struct moschip_port *mos7840_port;
2152 struct tty_struct *tty;
2153 dbg("mos7840_set_termios: START\n");
2154 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2155 dbg("%s", "Invalid port \n");
2159 serial = port->serial;
2161 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
2162 dbg("%s", "Invalid Serial \n");
2166 mos7840_port = mos7840_get_port_private(port);
2168 if (mos7840_port == NULL)
2173 if (!port->tty || !port->tty->termios) {
2174 dbg("%s - no tty or termios", __FUNCTION__);
2178 if (!mos7840_port->open) {
2179 dbg("%s - port not opened", __FUNCTION__);
2183 dbg("%s\n", "setting termios - ");
2185 cflag = tty->termios->c_cflag;
2188 dbg("%s %s\n", __FUNCTION__, "cflag is NULL");
2192 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2193 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2196 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2197 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2200 dbg("%s - port %d", __FUNCTION__, port->number);
2202 /* change the port settings to the new ones specified */
2204 mos7840_change_port_settings(mos7840_port, old_termios);
2206 if (!mos7840_port->read_urb) {
2207 dbg("%s", "URB KILLED !!!!!\n");
2211 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2212 mos7840_port->read_urb->dev = serial->dev;
2213 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2215 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2222 /*****************************************************************************
2223 * mos7840_get_lsr_info - get line status register info
2225 * Purpose: Let user call ioctl() to get info when the UART physically
2226 * is emptied. On bus types like RS485, the transmitter must
2227 * release the bus after transmitting. This must be done when
2228 * the transmit shift register is empty, not be done when the
2229 * transmit holding register is empty. This functionality
2230 * allows an RS485 driver to be written in user space.
2231 *****************************************************************************/
2233 static int mos7840_get_lsr_info(struct moschip_port *mos7840_port,
2234 unsigned int __user *value)
2237 unsigned int result = 0;
2239 count = mos7840_chars_in_buffer(mos7840_port->port);
2241 dbg("%s -- Empty", __FUNCTION__);
2242 result = TIOCSER_TEMT;
2245 if (copy_to_user(value, &result, sizeof(int)))
2250 /*****************************************************************************
2251 * mos7840_set_modem_info
2252 * function to set modem info
2253 *****************************************************************************/
2255 static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2256 unsigned int cmd, unsigned int __user *value)
2262 struct usb_serial_port *port;
2264 if (mos7840_port == NULL)
2267 port = (struct usb_serial_port *)mos7840_port->port;
2268 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2269 dbg("%s", "Invalid port \n");
2273 mcr = mos7840_port->shadowMCR;
2275 if (copy_from_user(&arg, value, sizeof(int)))
2280 if (arg & TIOCM_RTS)
2282 if (arg & TIOCM_DTR)
2284 if (arg & TIOCM_LOOP)
2285 mcr |= MCR_LOOPBACK;
2289 if (arg & TIOCM_RTS)
2291 if (arg & TIOCM_DTR)
2293 if (arg & TIOCM_LOOP)
2294 mcr &= ~MCR_LOOPBACK;
2298 /* turn off the RTS and DTR and LOOPBACK
2299 * and then only turn on what was asked to */
2300 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2301 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2302 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2303 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2307 mos7840_port->shadowMCR = mcr;
2309 Data = mos7840_port->shadowMCR;
2311 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2313 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2320 /*****************************************************************************
2321 * mos7840_get_modem_info
2322 * function to get modem info
2323 *****************************************************************************/
2325 static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
2326 unsigned int __user *value)
2328 unsigned int result = 0;
2330 unsigned int mcr = mos7840_port->shadowMCR;
2333 mos7840_get_uart_reg(mos7840_port->port, MODEM_STATUS_REGISTER,
2335 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2336 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2337 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2338 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2339 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2340 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2342 dbg("%s -- %x", __FUNCTION__, result);
2344 if (copy_to_user(value, &result, sizeof(int)))
2349 /*****************************************************************************
2350 * mos7840_get_serial_info
2351 * function to get information about serial port
2352 *****************************************************************************/
2354 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2355 struct serial_struct __user *retinfo)
2357 struct serial_struct tmp;
2359 if (mos7840_port == NULL)
2365 memset(&tmp, 0, sizeof(tmp));
2367 tmp.type = PORT_16550A;
2368 tmp.line = mos7840_port->port->serial->minor;
2369 tmp.port = mos7840_port->port->number;
2371 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2372 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2373 tmp.baud_base = 9600;
2374 tmp.close_delay = 5 * HZ;
2375 tmp.closing_wait = 30 * HZ;
2377 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2382 /*****************************************************************************
2384 * this function handles any ioctl calls to the driver
2385 *****************************************************************************/
2387 static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
2388 unsigned int cmd, unsigned long arg)
2390 void __user *argp = (void __user *)arg;
2391 struct moschip_port *mos7840_port;
2392 struct tty_struct *tty;
2394 struct async_icount cnow;
2395 struct async_icount cprev;
2396 struct serial_icounter_struct icount;
2399 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2400 dbg("%s", "Invalid port \n");
2404 mos7840_port = mos7840_get_port_private(port);
2406 if (mos7840_port == NULL)
2409 tty = mos7840_port->port->tty;
2411 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2414 /* return number of bytes available */
2417 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2418 return mos7840_get_lsr_info(mos7840_port, argp);
2424 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
2427 mos7840_set_modem_info(mos7840_port, cmd, argp);
2431 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
2432 return mos7840_get_modem_info(mos7840_port, argp);
2435 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2436 return mos7840_get_serial_info(mos7840_port, argp);
2439 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2443 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2444 cprev = mos7840_port->icount;
2446 //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
2447 mos7840_port->delta_msr_cond = 0;
2448 wait_event_interruptible(mos7840_port->delta_msr_wait,
2450 delta_msr_cond == 1));
2452 /* see if a signal did it */
2453 if (signal_pending(current))
2454 return -ERESTARTSYS;
2455 cnow = mos7840_port->icount;
2457 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2458 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2459 return -EIO; /* no change => error */
2460 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2461 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2462 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2463 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2472 cnow = mos7840_port->icount;
2474 icount.cts = cnow.cts;
2475 icount.dsr = cnow.dsr;
2476 icount.rng = cnow.rng;
2477 icount.dcd = cnow.dcd;
2478 icount.rx = cnow.rx;
2479 icount.tx = cnow.tx;
2480 icount.frame = cnow.frame;
2481 icount.overrun = cnow.overrun;
2482 icount.parity = cnow.parity;
2483 icount.brk = cnow.brk;
2484 icount.buf_overrun = cnow.buf_overrun;
2486 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2487 port->number, icount.rx, icount.tx);
2488 if (copy_to_user(argp, &icount, sizeof(icount)))
2498 return -ENOIOCTLCMD;
2501 static int mos7840_calc_num_ports(struct usb_serial *serial)
2503 int mos7840_num_ports = 0;
2505 dbg("numberofendpoints: %d \n",
2506 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
2507 dbg("numberofendpoints: %d \n",
2508 (int)serial->interface->altsetting->desc.bNumEndpoints);
2509 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2510 mos7840_num_ports = serial->num_ports = 2;
2511 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
2512 serial->num_bulk_in = 4;
2513 serial->num_bulk_out = 4;
2514 mos7840_num_ports = serial->num_ports = 4;
2517 return mos7840_num_ports;
2520 /****************************************************************************
2522 ****************************************************************************/
2524 static int mos7840_startup(struct usb_serial *serial)
2526 struct moschip_port *mos7840_port;
2527 struct usb_device *dev;
2531 dbg("%s \n", " mos7840_startup :entering..........");
2534 dbg("%s\n", "Invalid Handler");
2540 dbg("%s\n", "Entering...");
2542 /* we set up the pointers to the endpoints in the mos7840_open *
2543 * function, as the structures aren't created yet. */
2545 /* set up port private structures */
2546 for (i = 0; i < serial->num_ports; ++i) {
2547 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2548 if (mos7840_port == NULL) {
2549 err("%s - Out of memory", __FUNCTION__);
2551 i--; /* don't follow NULL pointer cleaning up */
2555 /* Initialize all port interrupt end point to port 0 int endpoint *
2556 * Our device has only one interrupt end point comman to all port */
2558 mos7840_port->port = serial->port[i];
2559 mos7840_set_port_private(serial->port[i], mos7840_port);
2560 spin_lock_init(&mos7840_port->pool_lock);
2562 mos7840_port->port_num = ((serial->port[i]->number -
2563 (serial->port[i]->serial->minor)) +
2566 if (mos7840_port->port_num == 1) {
2567 mos7840_port->SpRegOffset = 0x0;
2568 mos7840_port->ControlRegOffset = 0x1;
2569 mos7840_port->DcrRegOffset = 0x4;
2570 } else if ((mos7840_port->port_num == 2)
2571 && (serial->num_ports == 4)) {
2572 mos7840_port->SpRegOffset = 0x8;
2573 mos7840_port->ControlRegOffset = 0x9;
2574 mos7840_port->DcrRegOffset = 0x16;
2575 } else if ((mos7840_port->port_num == 2)
2576 && (serial->num_ports == 2)) {
2577 mos7840_port->SpRegOffset = 0xa;
2578 mos7840_port->ControlRegOffset = 0xb;
2579 mos7840_port->DcrRegOffset = 0x19;
2580 } else if ((mos7840_port->port_num == 3)
2581 && (serial->num_ports == 4)) {
2582 mos7840_port->SpRegOffset = 0xa;
2583 mos7840_port->ControlRegOffset = 0xb;
2584 mos7840_port->DcrRegOffset = 0x19;
2585 } else if ((mos7840_port->port_num == 4)
2586 && (serial->num_ports == 4)) {
2587 mos7840_port->SpRegOffset = 0xc;
2588 mos7840_port->ControlRegOffset = 0xd;
2589 mos7840_port->DcrRegOffset = 0x1c;
2591 mos7840_dump_serial_port(mos7840_port);
2593 mos7840_set_port_private(serial->port[i], mos7840_port);
2595 //enable rx_disable bit in control register
2598 mos7840_get_reg_sync(serial->port[i],
2599 mos7840_port->ControlRegOffset, &Data);
2601 dbg("Reading ControlReg failed status-0x%x\n", status);
2604 dbg("ControlReg Reading success val is %x, status%d\n",
2606 Data |= 0x08; //setting driver done bit
2607 Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
2609 //Data |= 0x20; //rx_disable bit
2612 mos7840_set_reg_sync(serial->port[i],
2613 mos7840_port->ControlRegOffset, Data);
2615 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2618 dbg("ControlReg Writing success(rx_disable) status%d\n",
2621 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2625 mos7840_set_reg_sync(serial->port[i],
2626 (__u16) (mos7840_port->DcrRegOffset +
2629 dbg("Writing DCR0 failed status-0x%x\n", status);
2632 dbg("DCR0 Writing success status%d\n", status);
2637 mos7840_set_reg_sync(serial->port[i],
2638 (__u16) (mos7840_port->DcrRegOffset +
2641 dbg("Writing DCR1 failed status-0x%x\n", status);
2644 dbg("DCR1 Writing success status%d\n", status);
2649 mos7840_set_reg_sync(serial->port[i],
2650 (__u16) (mos7840_port->DcrRegOffset +
2653 dbg("Writing DCR2 failed status-0x%x\n", status);
2656 dbg("DCR2 Writing success status%d\n", status);
2658 // write values in clkstart0x0 and clkmulti 0x20
2662 mos7840_set_reg_sync(serial->port[i],
2663 CLK_START_VALUE_REGISTER, Data);
2665 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2668 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2672 mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2675 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2679 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2682 //write value 0x0 to scratchpad register
2685 mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
2688 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2692 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2695 //Zero Length flag register
2696 if ((mos7840_port->port_num != 1)
2697 && (serial->num_ports == 2)) {
2701 status = mos7840_set_reg_sync(serial->port[i],
2707 dbg("ZLIP offset%x\n",
2709 ((__u16) mos7840_port->port_num)));
2711 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2715 dbg("ZLP_REG%d Writing success status%d\n",
2720 status = mos7840_set_reg_sync(serial->port[i],
2726 dbg("ZLIP offset%x\n",
2728 ((__u16) mos7840_port->port_num) - 0x1));
2730 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2734 dbg("ZLP_REG%d Writing success status%d\n",
2738 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2739 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2740 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
2741 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || !mos7840_port->dr) {
2747 //Zero Length flag enable
2749 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2751 dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
2754 dbg("ZLP_REG5 Writing success status%d\n", status);
2756 /* setting configuration feature to one */
2757 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2758 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2761 for (/* nothing */; i >= 0; i--) {
2762 mos7840_port = mos7840_get_port_private(serial->port[i]);
2764 kfree(mos7840_port->dr);
2765 kfree(mos7840_port->ctrl_buf);
2766 usb_free_urb(mos7840_port->control_urb);
2767 kfree(mos7840_port);
2768 serial->port[i] = NULL;
2773 /****************************************************************************
2775 * This function is called whenever the device is removed from the usb bus.
2776 ****************************************************************************/
2778 static void mos7840_shutdown(struct usb_serial *serial)
2781 unsigned long flags;
2782 struct moschip_port *mos7840_port;
2783 dbg("%s \n", " shutdown :entering..........");
2786 dbg("%s", "Invalid Handler \n");
2790 /* check for the ports to be closed,close the ports and disconnect */
2792 /* free private structure allocated for serial port *
2793 * stop reads and writes on all ports */
2795 for (i = 0; i < serial->num_ports; ++i) {
2796 mos7840_port = mos7840_get_port_private(serial->port[i]);
2797 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2798 mos7840_port->zombie = 1;
2799 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2800 usb_kill_urb(mos7840_port->control_urb);
2801 kfree(mos7840_port->ctrl_buf);
2802 kfree(mos7840_port->dr);
2803 kfree(mos7840_port);
2804 mos7840_set_port_private(serial->port[i], NULL);
2807 dbg("%s\n", "Thank u :: ");
2811 static struct usb_driver io_driver = {
2813 .probe = usb_serial_probe,
2814 .disconnect = usb_serial_disconnect,
2815 .id_table = moschip_id_table_combined,
2819 static struct usb_serial_driver moschip7840_4port_device = {
2821 .owner = THIS_MODULE,
2824 .description = DRIVER_DESC,
2825 .usb_driver = &io_driver,
2826 .id_table = moschip_port_id_table,
2827 .num_interrupt_in = 1, //NUM_DONT_CARE,//1,
2833 .open = mos7840_open,
2834 .close = mos7840_close,
2835 .write = mos7840_write,
2836 .write_room = mos7840_write_room,
2837 .chars_in_buffer = mos7840_chars_in_buffer,
2838 .throttle = mos7840_throttle,
2839 .unthrottle = mos7840_unthrottle,
2840 .calc_num_ports = mos7840_calc_num_ports,
2841 #ifdef MCSSerialProbe
2842 .probe = mos7840_serial_probe,
2844 .ioctl = mos7840_ioctl,
2845 .set_termios = mos7840_set_termios,
2846 .break_ctl = mos7840_break,
2847 .tiocmget = mos7840_tiocmget,
2848 .tiocmset = mos7840_tiocmset,
2849 .attach = mos7840_startup,
2850 .shutdown = mos7840_shutdown,
2851 .read_bulk_callback = mos7840_bulk_in_callback,
2852 .read_int_callback = mos7840_interrupt_callback,
2855 /****************************************************************************
2857 * This is called by the module subsystem, or on startup to initialize us
2858 ****************************************************************************/
2859 static int __init moschip7840_init(void)
2863 dbg("%s \n", " mos7840_init :entering..........");
2865 /* Register with the usb serial */
2866 retval = usb_serial_register(&moschip7840_4port_device);
2869 goto failed_port_device_register;
2871 dbg("%s\n", "Entring...");
2872 info(DRIVER_DESC " " DRIVER_VERSION);
2874 /* Register with the usb */
2875 retval = usb_register(&io_driver);
2878 goto failed_usb_register;
2881 dbg("%s\n", "Leaving...");
2885 failed_usb_register:
2886 usb_serial_deregister(&moschip7840_4port_device);
2888 failed_port_device_register:
2893 /****************************************************************************
2895 * Called when the driver is about to be unloaded.
2896 ****************************************************************************/
2897 static void __exit moschip7840_exit(void)
2900 dbg("%s \n", " mos7840_exit :entering..........");
2902 usb_deregister(&io_driver);
2904 usb_serial_deregister(&moschip7840_4port_device);
2906 dbg("%s\n", "Entring...");
2909 module_init(moschip7840_init);
2910 module_exit(moschip7840_exit);
2912 /* Module information */
2913 MODULE_DESCRIPTION(DRIVER_DESC);
2914 MODULE_LICENSE("GPL");
2916 module_param(debug, bool, S_IRUGO | S_IWUSR);
2917 MODULE_PARM_DESC(debug, "Debug enabled or not");