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);
773 dbg("%s", "NULL mos7840_port pointer \n");
777 if (mos7840_port_paranoia_check(mos7840_port->port, __FUNCTION__)) {
778 dbg("%s", "Port Paranoia failed \n");
782 dbg("%s \n", "Entering .........");
784 tty = mos7840_port->port->tty;
786 if (tty && mos7840_port->open)
791 /************************************************************************/
792 /* 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 */
793 /************************************************************************/
794 #ifdef MCSSerialProbe
795 static int mos7840_serial_probe(struct usb_serial *serial,
796 const struct usb_device_id *id)
799 /*need to implement the mode_reg reading and updating\
800 structures usb_serial_ device_type\
801 (i.e num_ports, num_bulkin,bulkout etc) */
802 /* Also we can update the changes attach */
807 /*****************************************************************************
809 * this function is called by the tty driver when a port is opened
810 * If successful, we return 0
811 * Otherwise we return a negative error number.
812 *****************************************************************************/
814 static int mos7840_open(struct usb_serial_port *port, struct file *filp)
818 struct usb_serial *serial;
822 struct moschip_port *mos7840_port;
823 struct moschip_port *port0;
825 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
826 dbg("%s", "Port Paranoia failed \n");
830 serial = port->serial;
832 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
833 dbg("%s", "Serial Paranoia failed \n");
837 mos7840_port = mos7840_get_port_private(port);
838 port0 = mos7840_get_port_private(serial->port[0]);
840 if (mos7840_port == NULL || port0 == NULL)
843 usb_clear_halt(serial->dev, port->write_urb->pipe);
844 usb_clear_halt(serial->dev, port->read_urb->pipe);
847 /* Initialising the write urb pool */
848 for (j = 0; j < NUM_URBS; ++j) {
849 urb = usb_alloc_urb(0, GFP_KERNEL);
850 mos7840_port->write_urb_pool[j] = urb;
853 err("No more urbs???");
857 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
858 if (!urb->transfer_buffer) {
860 mos7840_port->write_urb_pool[j] = NULL;
861 err("%s-out of memory for urb buffers.", __FUNCTION__);
866 /*****************************************************************************
867 * Initialize MCS7840 -- Write Init values to corresponding Registers
875 * 0x08 : SP1/2 Control Reg
876 *****************************************************************************/
878 //NEED to check the following Block
882 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
884 dbg("Reading Spreg failed\n");
888 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
890 dbg("writing Spreg failed\n");
895 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
897 dbg("writing Spreg failed\n");
900 //End of block to be checked
905 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
907 dbg("Reading Controlreg failed\n");
910 Data |= 0x08; //Driver done bit
911 Data |= 0x20; //rx_disable
912 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
914 dbg("writing Controlreg failed\n");
917 //do register settings here
918 // Set all regs to the device default values.
919 ////////////////////////////////////
920 // First Disable all interrupts.
921 ////////////////////////////////////
924 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
926 dbg("disableing interrupts failed\n");
929 // Set FIFO_CONTROL_REGISTER to the default value
931 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
933 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
938 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
940 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
945 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
946 mos7840_port->shadowLCR = Data;
949 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
950 mos7840_port->shadowMCR = Data;
953 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
954 mos7840_port->shadowLCR = Data;
956 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
957 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
961 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
965 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
969 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
971 Data = Data & ~SERIAL_LCR_DLAB;
973 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
974 mos7840_port->shadowLCR = Data;
976 //clearing Bulkin and Bulkout Fifo
979 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
983 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
987 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
988 //Finally enable all interrupts
992 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
994 //clearing rx_disable
998 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
1002 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1008 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
1012 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1014 /* force low_latency on so that our tty_push actually forces *
1015 * the data through,otherwise it is scheduled, and with *
1016 * high data rates (like with OHCI) data can get lost. */
1019 port->tty->low_latency = 1;
1020 /* Check to see if we've set up our endpoint info yet *
1021 * (can't set it up in mos7840_startup as the structures *
1022 * were not set up at that time.) */
1023 if (port0->open_ports == 1) {
1024 if (serial->port[0]->interrupt_in_buffer == NULL) {
1026 /* set up interrupt urb */
1028 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1030 usb_rcvintpipe(serial->dev,
1032 interrupt_in_endpointAddress),
1033 serial->port[0]->interrupt_in_buffer,
1034 serial->port[0]->interrupt_in_urb->
1035 transfer_buffer_length,
1036 mos7840_interrupt_callback,
1038 serial->port[0]->interrupt_in_urb->
1041 /* start interrupt read for mos7840 *
1042 * will continue as long as mos7840 is connected */
1045 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1048 err("%s - Error %d submitting interrupt urb",
1049 __FUNCTION__, response);
1056 /* see if we've set up our endpoint info yet *
1057 * (can't set it up in mos7840_startup as the *
1058 * structures were not set up at that time.) */
1060 dbg("port number is %d \n", port->number);
1061 dbg("serial number is %d \n", port->serial->minor);
1062 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1063 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1064 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
1065 dbg("port's number in the device is %d\n", mos7840_port->port_num);
1066 mos7840_port->read_urb = port->read_urb;
1068 /* set up our bulk in urb */
1070 usb_fill_bulk_urb(mos7840_port->read_urb,
1072 usb_rcvbulkpipe(serial->dev,
1073 port->bulk_in_endpointAddress),
1074 port->bulk_in_buffer,
1075 mos7840_port->read_urb->transfer_buffer_length,
1076 mos7840_bulk_in_callback, mos7840_port);
1078 dbg("mos7840_open: bulkin endpoint is %d\n",
1079 port->bulk_in_endpointAddress);
1080 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1082 err("%s - Error %d submitting control urb", __FUNCTION__,
1086 /* initialize our wait queues */
1087 init_waitqueue_head(&mos7840_port->wait_chase);
1088 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1090 /* initialize our icount structure */
1091 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1093 /* initialize our port settings */
1094 mos7840_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
1095 /* send a open port command */
1096 mos7840_port->open = 1;
1097 //mos7840_change_port_settings(mos7840_port,old_termios);
1098 mos7840_port->icount.tx = 0;
1099 mos7840_port->icount.rx = 0;
1101 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n", serial, mos7840_port, port);
1107 /*****************************************************************************
1108 * mos7840_chars_in_buffer
1109 * this function is called by the tty driver when it wants to know how many
1110 * bytes of data we currently have outstanding in the port (data that has
1111 * been written, but hasn't made it out the port yet)
1112 * If successful, we return the number of bytes left to be written in the
1114 * Otherwise we return a negative error number.
1115 *****************************************************************************/
1117 static int mos7840_chars_in_buffer(struct usb_serial_port *port)
1121 unsigned long flags;
1122 struct moschip_port *mos7840_port;
1124 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
1126 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1127 dbg("%s", "Invalid port \n");
1131 mos7840_port = mos7840_get_port_private(port);
1132 if (mos7840_port == NULL) {
1133 dbg("%s \n", "mos7840_break:leaving ...........");
1137 spin_lock_irqsave(&mos7840_port->pool_lock,flags);
1138 for (i = 0; i < NUM_URBS; ++i) {
1139 if (mos7840_port->busy[i]) {
1140 chars += URB_TRANSFER_BUFFER_SIZE;
1143 spin_unlock_irqrestore(&mos7840_port->pool_lock,flags);
1144 dbg("%s - returns %d", __FUNCTION__, chars);
1149 /************************************************************************
1151 * mos7840_block_until_tx_empty
1153 * This function will block the close until one of the following:
1155 * 2. The mos7840 has stopped
1156 * 3. A timout of 3 seconds without activity has expired
1158 ************************************************************************/
1159 static void mos7840_block_until_tx_empty(struct moschip_port *mos7840_port)
1161 int timeout = HZ / 10;
1167 count = mos7840_chars_in_buffer(mos7840_port->port);
1169 /* Check for Buffer status */
1174 /* Block the thread for a while */
1175 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1178 /* No activity.. count down section */
1181 dbg("%s - TIMEOUT", __FUNCTION__);
1184 /* Reset timout value back to seconds */
1190 /*****************************************************************************
1192 * this function is called by the tty driver when a port is closed
1193 *****************************************************************************/
1195 static void mos7840_close(struct usb_serial_port *port, struct file *filp)
1197 struct usb_serial *serial;
1198 struct moschip_port *mos7840_port;
1199 struct moschip_port *port0;
1203 dbg("%s\n", "mos7840_close:entering...");
1205 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1206 dbg("%s", "Port Paranoia failed \n");
1210 serial = mos7840_get_usb_serial(port, __FUNCTION__);
1212 dbg("%s", "Serial Paranoia failed \n");
1216 mos7840_port = mos7840_get_port_private(port);
1217 port0 = mos7840_get_port_private(serial->port[0]);
1219 if (mos7840_port == NULL || port0 == NULL)
1222 for (j = 0; j < NUM_URBS; ++j)
1223 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1225 /* Freeing Write URBs */
1226 for (j = 0; j < NUM_URBS; ++j) {
1227 if (mos7840_port->write_urb_pool[j]) {
1228 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1229 kfree(mos7840_port->write_urb_pool[j]->
1232 usb_free_urb(mos7840_port->write_urb_pool[j]);
1237 /* flush and block until tx is empty */
1238 mos7840_block_until_tx_empty(mos7840_port);
1241 /* While closing port, shutdown all bulk read, write *
1242 * and interrupt read if they exists */
1245 if (mos7840_port->write_urb) {
1246 dbg("%s", "Shutdown bulk write\n");
1247 usb_kill_urb(mos7840_port->write_urb);
1250 if (mos7840_port->read_urb) {
1251 dbg("%s", "Shutdown bulk read\n");
1252 usb_kill_urb(mos7840_port->read_urb);
1254 if ((&mos7840_port->control_urb)) {
1255 dbg("%s", "Shutdown control read\n");
1256 // usb_kill_urb (mos7840_port->control_urb);
1260 // if(mos7840_port->ctrl_buf != NULL)
1261 // kfree(mos7840_port->ctrl_buf);
1262 port0->open_ports--;
1263 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1264 port0->open_ports, port->number);
1265 if (port0->open_ports == 0) {
1266 if (serial->port[0]->interrupt_in_urb) {
1267 dbg("%s", "Shutdown interrupt_in_urb\n");
1268 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1272 if (mos7840_port->write_urb) {
1273 /* if this urb had a transfer buffer already (old tx) free it */
1275 if (mos7840_port->write_urb->transfer_buffer != NULL) {
1276 kfree(mos7840_port->write_urb->transfer_buffer);
1278 usb_free_urb(mos7840_port->write_urb);
1282 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1285 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1287 mos7840_port->open = 0;
1289 dbg("%s \n", "Leaving ............");
1292 /************************************************************************
1294 * mos7840_block_until_chase_response
1296 * This function will block the close until one of the following:
1297 * 1. Response to our Chase comes from mos7840
1298 * 2. A timout of 10 seconds without activity has expired
1299 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1301 ************************************************************************/
1303 static void mos7840_block_until_chase_response(struct moschip_port
1306 int timeout = 1 * HZ;
1311 count = mos7840_chars_in_buffer(mos7840_port->port);
1313 /* Check for Buffer status */
1318 /* Block the thread for a while */
1319 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1321 /* No activity.. count down section */
1324 dbg("%s - TIMEOUT", __FUNCTION__);
1327 /* Reset timout value back to seconds */
1334 /*****************************************************************************
1336 * this function sends a break to the port
1337 *****************************************************************************/
1338 static void mos7840_break(struct usb_serial_port *port, int break_state)
1341 struct usb_serial *serial;
1342 struct moschip_port *mos7840_port;
1344 dbg("%s \n", "Entering ...........");
1345 dbg("mos7840_break: Start\n");
1347 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1348 dbg("%s", "Port Paranoia failed \n");
1352 serial = mos7840_get_usb_serial(port, __FUNCTION__);
1354 dbg("%s", "Serial Paranoia failed \n");
1358 mos7840_port = mos7840_get_port_private(port);
1360 if (mos7840_port == NULL) {
1366 /* flush and block until tx is empty */
1367 mos7840_block_until_chase_response(mos7840_port);
1370 if (break_state == -1) {
1371 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1373 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1376 mos7840_port->shadowLCR = data;
1377 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1378 mos7840_port->shadowLCR);
1379 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1380 mos7840_port->shadowLCR);
1385 /*****************************************************************************
1386 * mos7840_write_room
1387 * this function is called by the tty driver when it wants to know how many
1388 * bytes of data we can accept for a specific port.
1389 * If successful, we return the amount of room that we have for this port
1390 * Otherwise we return a negative error number.
1391 *****************************************************************************/
1393 static int mos7840_write_room(struct usb_serial_port *port)
1397 unsigned long flags;
1398 struct moschip_port *mos7840_port;
1400 dbg("%s \n", " mos7840_write_room:entering ...........");
1402 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1403 dbg("%s", "Invalid port \n");
1404 dbg("%s \n", " mos7840_write_room:leaving ...........");
1408 mos7840_port = mos7840_get_port_private(port);
1409 if (mos7840_port == NULL) {
1410 dbg("%s \n", "mos7840_break:leaving ...........");
1414 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1415 for (i = 0; i < NUM_URBS; ++i) {
1416 if (!mos7840_port->busy[i]) {
1417 room += URB_TRANSFER_BUFFER_SIZE;
1420 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1422 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1423 dbg("%s - returns %d", __FUNCTION__, room);
1428 /*****************************************************************************
1430 * this function is called by the tty driver when data should be written to
1432 * If successful, we return the number of bytes written, otherwise we
1433 * return a negative error number.
1434 *****************************************************************************/
1436 static int mos7840_write(struct usb_serial_port *port,
1437 const unsigned char *data, int count)
1443 unsigned long flags;
1445 struct moschip_port *mos7840_port;
1446 struct usb_serial *serial;
1449 const unsigned char *current_position = data;
1450 unsigned char *data1;
1451 dbg("%s \n", "entering ...........");
1452 //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
1457 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1458 mos7840_port->shadowLCR = Data;
1459 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
1460 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1461 mos7840_port->shadowLCR);
1464 //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
1465 //mos7840_port->shadowLCR=Data;//Need to add later
1467 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1469 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1472 //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
1475 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1476 dbg("mos7840_write:DLL value is %x\n", Data);
1480 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1481 dbg("mos7840_write:DLM value is %x\n", Data);
1483 Data = Data & ~SERIAL_LCR_DLAB;
1484 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1485 mos7840_port->shadowLCR);
1487 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1490 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1491 dbg("%s", "Port Paranoia failed \n");
1495 serial = port->serial;
1496 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
1497 dbg("%s", "Serial Paranoia failed \n");
1501 mos7840_port = mos7840_get_port_private(port);
1502 if (mos7840_port == NULL) {
1503 dbg("%s", "mos7840_port is NULL\n");
1507 /* try to find a free urb in the list */
1510 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1511 for (i = 0; i < NUM_URBS; ++i) {
1512 if (!mos7840_port->busy[i]) {
1513 mos7840_port->busy[i] = 1;
1514 urb = mos7840_port->write_urb_pool[i];
1519 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1522 dbg("%s - no more free urbs", __FUNCTION__);
1526 if (urb->transfer_buffer == NULL) {
1527 urb->transfer_buffer =
1528 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1530 if (urb->transfer_buffer == NULL) {
1531 err("%s no more kernel memory...", __FUNCTION__);
1535 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1537 memcpy(urb->transfer_buffer, current_position, transfer_size);
1539 /* fill urb with data and submit */
1540 usb_fill_bulk_urb(urb,
1542 usb_sndbulkpipe(serial->dev,
1543 port->bulk_out_endpointAddress),
1544 urb->transfer_buffer,
1546 mos7840_bulk_out_data_callback, mos7840_port);
1548 data1 = urb->transfer_buffer;
1549 dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1551 /* send it down the pipe */
1552 status = usb_submit_urb(urb, GFP_ATOMIC);
1555 mos7840_port->busy[i] = 0;
1556 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1557 __FUNCTION__, status);
1558 bytes_sent = status;
1561 bytes_sent = transfer_size;
1562 mos7840_port->icount.tx += transfer_size;
1564 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1571 /*****************************************************************************
1573 * this function is called by the tty driver when it wants to stop the data
1574 * being read from the port.
1575 *****************************************************************************/
1577 static void mos7840_throttle(struct usb_serial_port *port)
1579 struct moschip_port *mos7840_port;
1580 struct tty_struct *tty;
1583 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1584 dbg("%s", "Invalid port \n");
1588 dbg("- port %d\n", port->number);
1590 mos7840_port = mos7840_get_port_private(port);
1592 if (mos7840_port == NULL)
1595 if (!mos7840_port->open) {
1596 dbg("%s\n", "port not opened");
1600 dbg("%s", "Entering .......... \n");
1604 dbg("%s - no tty available", __FUNCTION__);
1608 /* if we are implementing XON/XOFF, send the stop character */
1610 unsigned char stop_char = STOP_CHAR(tty);
1611 status = mos7840_write(port, &stop_char, 1);
1617 /* if we are implementing RTS/CTS, toggle that line */
1618 if (tty->termios->c_cflag & CRTSCTS) {
1619 mos7840_port->shadowMCR &= ~MCR_RTS;
1622 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1623 mos7840_port->shadowMCR);
1633 /*****************************************************************************
1634 * mos7840_unthrottle
1635 * this function is called by the tty driver when it wants to resume the data
1636 * being read from the port (called after SerialThrottle is called)
1637 *****************************************************************************/
1638 static void mos7840_unthrottle(struct usb_serial_port *port)
1640 struct tty_struct *tty;
1642 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1644 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1645 dbg("%s", "Invalid port \n");
1649 if (mos7840_port == NULL)
1652 if (!mos7840_port->open) {
1653 dbg("%s - port not opened", __FUNCTION__);
1657 dbg("%s", "Entering .......... \n");
1661 dbg("%s - no tty available", __FUNCTION__);
1665 /* if we are implementing XON/XOFF, send the start character */
1667 unsigned char start_char = START_CHAR(tty);
1668 status = mos7840_write(port, &start_char, 1);
1674 /* if we are implementing RTS/CTS, toggle that line */
1675 if (tty->termios->c_cflag & CRTSCTS) {
1676 mos7840_port->shadowMCR |= MCR_RTS;
1679 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1680 mos7840_port->shadowMCR);
1689 static int mos7840_tiocmget(struct usb_serial_port *port, struct file *file)
1691 struct moschip_port *mos7840_port;
1692 unsigned int result;
1696 mos7840_port = mos7840_get_port_private(port);
1698 dbg("%s - port %d", __FUNCTION__, port->number);
1700 if (mos7840_port == NULL)
1703 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1704 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1705 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1706 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1707 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1708 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1709 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1710 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1711 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1713 dbg("%s - 0x%04X", __FUNCTION__, result);
1718 static int mos7840_tiocmset(struct usb_serial_port *port, struct file *file,
1719 unsigned int set, unsigned int clear)
1721 struct moschip_port *mos7840_port;
1723 unsigned int status;
1725 dbg("%s - port %d", __FUNCTION__, port->number);
1727 mos7840_port = mos7840_get_port_private(port);
1729 if (mos7840_port == NULL)
1732 mcr = mos7840_port->shadowMCR;
1733 if (clear & TIOCM_RTS)
1735 if (clear & TIOCM_DTR)
1737 if (clear & TIOCM_LOOP)
1738 mcr &= ~MCR_LOOPBACK;
1740 if (set & TIOCM_RTS)
1742 if (set & TIOCM_DTR)
1744 if (set & TIOCM_LOOP)
1745 mcr |= MCR_LOOPBACK;
1747 mos7840_port->shadowMCR = mcr;
1750 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1752 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
1759 /*****************************************************************************
1760 * mos7840_calc_baud_rate_divisor
1761 * this function calculates the proper baud rate divisor for the specified
1763 *****************************************************************************/
1764 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1765 __u16 * clk_sel_val)
1768 dbg("%s - %d", __FUNCTION__, baudRate);
1770 if (baudRate <= 115200) {
1771 *divisor = 115200 / baudRate;
1774 if ((baudRate > 115200) && (baudRate <= 230400)) {
1775 *divisor = 230400 / baudRate;
1776 *clk_sel_val = 0x10;
1777 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1778 *divisor = 403200 / baudRate;
1779 *clk_sel_val = 0x20;
1780 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1781 *divisor = 460800 / baudRate;
1782 *clk_sel_val = 0x30;
1783 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1784 *divisor = 806400 / baudRate;
1785 *clk_sel_val = 0x40;
1786 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1787 *divisor = 921600 / baudRate;
1788 *clk_sel_val = 0x50;
1789 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1790 *divisor = 1572864 / baudRate;
1791 *clk_sel_val = 0x60;
1792 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1793 *divisor = 3145728 / baudRate;
1794 *clk_sel_val = 0x70;
1800 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1801 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1802 *divisor = mos7840_divisor_table[i].Divisor;
1807 /* After trying for all the standard baud rates *
1808 * Try calculating the divisor for this baud rate */
1810 if (baudrate > 75 && baudrate < 230400) {
1811 /* get the divisor */
1812 custom = (__u16) (230400L / baudrate);
1814 /* Check for round off */
1815 round1 = (__u16) (2304000L / baudrate);
1816 round = (__u16) (round1 - (custom * 10));
1822 dbg(" Baud %d = %d\n", baudrate, custom);
1826 dbg("%s\n", " Baud calculation Failed...");
1831 /*****************************************************************************
1832 * mos7840_send_cmd_write_baud_rate
1833 * this function sends the proper command to change the baud rate of the
1835 *****************************************************************************/
1837 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1843 unsigned char number;
1845 struct usb_serial_port *port;
1847 if (mos7840_port == NULL)
1850 port = (struct usb_serial_port *)mos7840_port->port;
1851 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1852 dbg("%s", "Invalid port \n");
1856 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1857 dbg("%s", "Invalid Serial \n");
1861 dbg("%s", "Entering .......... \n");
1863 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1865 dbg("%s - port = %d, baud = %d", __FUNCTION__,
1866 mos7840_port->port->number, baudRate);
1867 //reset clk_uart_sel in spregOffset
1868 if (baudRate > 115200) {
1869 #ifdef HW_flow_control
1870 //NOTE: need to see the pther register to modify
1871 //setting h/w flow control bit to 1;
1874 mos7840_port->shadowMCR = Data;
1876 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1878 dbg("Writing spreg failed in set_serial_baud\n");
1884 #ifdef HW_flow_control
1885 //setting h/w flow control bit to 0;
1888 mos7840_port->shadowMCR = Data;
1890 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1892 dbg("Writing spreg failed in set_serial_baud\n");
1899 if (1) //baudRate <= 115200)
1905 mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1908 mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1911 dbg("reading spreg failed in set_serial_baud\n");
1914 Data = (Data & 0x8f) | clk_sel_val;
1917 mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1919 dbg("Writing spreg failed in set_serial_baud\n");
1922 /* Calculate the Divisor */
1925 err("%s - bad baud rate", __FUNCTION__);
1926 dbg("%s\n", "bad baud rate");
1929 /* Enable access to divisor latch */
1930 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1931 mos7840_port->shadowLCR = Data;
1932 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1934 /* Write the divisor */
1935 Data = (unsigned char)(divisor & 0xff);
1936 dbg("set_serial_baud Value to write DLL is %x\n", Data);
1937 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1939 Data = (unsigned char)((divisor & 0xff00) >> 8);
1940 dbg("set_serial_baud Value to write DLM is %x\n", Data);
1941 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1943 /* Disable access to divisor latch */
1944 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1945 mos7840_port->shadowLCR = Data;
1946 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1953 /*****************************************************************************
1954 * mos7840_change_port_settings
1955 * This routine is called to set the UART on the device to match
1956 * the specified new settings.
1957 *****************************************************************************/
1959 static void mos7840_change_port_settings(struct moschip_port *mos7840_port,
1960 struct ktermios *old_termios)
1962 struct tty_struct *tty;
1971 struct usb_serial_port *port;
1972 struct usb_serial *serial;
1974 if (mos7840_port == NULL)
1977 port = (struct usb_serial_port *)mos7840_port->port;
1979 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1980 dbg("%s", "Invalid port \n");
1984 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1985 dbg("%s", "Invalid Serial \n");
1989 serial = port->serial;
1991 dbg("%s - port %d", __FUNCTION__, mos7840_port->port->number);
1993 if (!mos7840_port->open) {
1994 dbg("%s - port not opened", __FUNCTION__);
1998 tty = mos7840_port->port->tty;
2000 if ((!tty) || (!tty->termios)) {
2001 dbg("%s - no tty structures", __FUNCTION__);
2005 dbg("%s", "Entering .......... \n");
2009 lParity = LCR_PAR_NONE;
2011 cflag = tty->termios->c_cflag;
2012 iflag = tty->termios->c_iflag;
2014 /* Change the number of bits */
2015 if (cflag & CSIZE) {
2016 switch (cflag & CSIZE) {
2034 /* Change the Parity bit */
2035 if (cflag & PARENB) {
2036 if (cflag & PARODD) {
2037 lParity = LCR_PAR_ODD;
2038 dbg("%s - parity = odd", __FUNCTION__);
2040 lParity = LCR_PAR_EVEN;
2041 dbg("%s - parity = even", __FUNCTION__);
2045 dbg("%s - parity = none", __FUNCTION__);
2048 if (cflag & CMSPAR) {
2049 lParity = lParity | 0x20;
2052 /* Change the Stop bit */
2053 if (cflag & CSTOPB) {
2055 dbg("%s - stop bits = 2", __FUNCTION__);
2058 dbg("%s - stop bits = 1", __FUNCTION__);
2061 /* Update the LCR with the correct value */
2062 mos7840_port->shadowLCR &=
2063 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2064 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2066 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2067 mos7840_port->shadowLCR);
2068 /* Disable Interrupts */
2070 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2073 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2076 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2078 /* Send the updated LCR value to the mos7840 */
2079 Data = mos7840_port->shadowLCR;
2081 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2084 mos7840_port->shadowMCR = Data;
2085 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2087 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2089 /* set up the MCR register and send it to the mos7840 */
2091 mos7840_port->shadowMCR = MCR_MASTER_IE;
2092 if (cflag & CBAUD) {
2093 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2096 if (cflag & CRTSCTS) {
2097 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2100 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2103 Data = mos7840_port->shadowMCR;
2104 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2106 /* Determine divisor based on baud rate */
2107 baud = tty_get_baud_rate(tty);
2110 /* pick a default, any default... */
2111 dbg("%s\n", "Picked default baud...");
2115 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2116 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2118 /* Enable Interrupts */
2120 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2122 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2123 mos7840_port->read_urb->dev = serial->dev;
2125 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2128 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2132 wake_up(&mos7840_port->delta_msr_wait);
2133 mos7840_port->delta_msr_cond = 1;
2134 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2135 mos7840_port->shadowLCR);
2140 /*****************************************************************************
2141 * mos7840_set_termios
2142 * this function is called by the tty driver when it wants to change
2143 * the termios structure
2144 *****************************************************************************/
2146 static void mos7840_set_termios(struct usb_serial_port *port,
2147 struct ktermios *old_termios)
2151 struct usb_serial *serial;
2152 struct moschip_port *mos7840_port;
2153 struct tty_struct *tty;
2154 dbg("mos7840_set_termios: START\n");
2155 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2156 dbg("%s", "Invalid port \n");
2160 serial = port->serial;
2162 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
2163 dbg("%s", "Invalid Serial \n");
2167 mos7840_port = mos7840_get_port_private(port);
2169 if (mos7840_port == NULL)
2174 if (!port->tty || !port->tty->termios) {
2175 dbg("%s - no tty or termios", __FUNCTION__);
2179 if (!mos7840_port->open) {
2180 dbg("%s - port not opened", __FUNCTION__);
2184 dbg("%s\n", "setting termios - ");
2186 cflag = tty->termios->c_cflag;
2189 dbg("%s %s\n", __FUNCTION__, "cflag is NULL");
2193 /* check that they really want us to change something */
2195 if ((cflag == old_termios->c_cflag) &&
2196 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
2197 RELEVANT_IFLAG(old_termios->c_iflag))) {
2198 dbg("%s\n", "Nothing to change");
2203 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2204 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2207 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2208 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2211 dbg("%s - port %d", __FUNCTION__, port->number);
2213 /* change the port settings to the new ones specified */
2215 mos7840_change_port_settings(mos7840_port, old_termios);
2217 if (!mos7840_port->read_urb) {
2218 dbg("%s", "URB KILLED !!!!!\n");
2222 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2223 mos7840_port->read_urb->dev = serial->dev;
2224 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2226 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2233 /*****************************************************************************
2234 * mos7840_get_lsr_info - get line status register info
2236 * Purpose: Let user call ioctl() to get info when the UART physically
2237 * is emptied. On bus types like RS485, the transmitter must
2238 * release the bus after transmitting. This must be done when
2239 * the transmit shift register is empty, not be done when the
2240 * transmit holding register is empty. This functionality
2241 * allows an RS485 driver to be written in user space.
2242 *****************************************************************************/
2244 static int mos7840_get_lsr_info(struct moschip_port *mos7840_port,
2245 unsigned int __user *value)
2248 unsigned int result = 0;
2250 count = mos7840_chars_in_buffer(mos7840_port->port);
2252 dbg("%s -- Empty", __FUNCTION__);
2253 result = TIOCSER_TEMT;
2256 if (copy_to_user(value, &result, sizeof(int)))
2261 /*****************************************************************************
2262 * mos7840_get_bytes_avail - get number of bytes available
2264 * Purpose: Let user call ioctl to get the count of number of bytes available.
2265 *****************************************************************************/
2267 static int mos7840_get_bytes_avail(struct moschip_port *mos7840_port,
2268 unsigned int __user *value)
2270 unsigned int result = 0;
2271 struct tty_struct *tty = mos7840_port->port->tty;
2274 return -ENOIOCTLCMD;
2276 result = tty->read_cnt;
2278 dbg("%s(%d) = %d", __FUNCTION__, mos7840_port->port->number, result);
2279 if (copy_to_user(value, &result, sizeof(int)))
2282 return -ENOIOCTLCMD;
2285 /*****************************************************************************
2286 * mos7840_set_modem_info
2287 * function to set modem info
2288 *****************************************************************************/
2290 static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2291 unsigned int cmd, unsigned int __user *value)
2297 struct usb_serial_port *port;
2299 if (mos7840_port == NULL)
2302 port = (struct usb_serial_port *)mos7840_port->port;
2303 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2304 dbg("%s", "Invalid port \n");
2308 mcr = mos7840_port->shadowMCR;
2310 if (copy_from_user(&arg, value, sizeof(int)))
2315 if (arg & TIOCM_RTS)
2317 if (arg & TIOCM_DTR)
2319 if (arg & TIOCM_LOOP)
2320 mcr |= MCR_LOOPBACK;
2324 if (arg & TIOCM_RTS)
2326 if (arg & TIOCM_DTR)
2328 if (arg & TIOCM_LOOP)
2329 mcr &= ~MCR_LOOPBACK;
2333 /* turn off the RTS and DTR and LOOPBACK
2334 * and then only turn on what was asked to */
2335 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2336 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2337 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2338 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2342 mos7840_port->shadowMCR = mcr;
2344 Data = mos7840_port->shadowMCR;
2346 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2348 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2355 /*****************************************************************************
2356 * mos7840_get_modem_info
2357 * function to get modem info
2358 *****************************************************************************/
2360 static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
2361 unsigned int __user *value)
2363 unsigned int result = 0;
2365 unsigned int mcr = mos7840_port->shadowMCR;
2368 mos7840_get_uart_reg(mos7840_port->port, MODEM_STATUS_REGISTER,
2370 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2371 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2372 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2373 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2374 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2375 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2377 dbg("%s -- %x", __FUNCTION__, result);
2379 if (copy_to_user(value, &result, sizeof(int)))
2384 /*****************************************************************************
2385 * mos7840_get_serial_info
2386 * function to get information about serial port
2387 *****************************************************************************/
2389 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2390 struct serial_struct __user *retinfo)
2392 struct serial_struct tmp;
2394 if (mos7840_port == NULL)
2400 memset(&tmp, 0, sizeof(tmp));
2402 tmp.type = PORT_16550A;
2403 tmp.line = mos7840_port->port->serial->minor;
2404 tmp.port = mos7840_port->port->number;
2406 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2407 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2408 tmp.baud_base = 9600;
2409 tmp.close_delay = 5 * HZ;
2410 tmp.closing_wait = 30 * HZ;
2412 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2417 /*****************************************************************************
2419 * this function handles any ioctl calls to the driver
2420 *****************************************************************************/
2422 static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
2423 unsigned int cmd, unsigned long arg)
2425 void __user *argp = (void __user *)arg;
2426 struct moschip_port *mos7840_port;
2427 struct tty_struct *tty;
2429 struct async_icount cnow;
2430 struct async_icount cprev;
2431 struct serial_icounter_struct icount;
2434 struct tty_ldisc *ld;
2436 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2437 dbg("%s", "Invalid port \n");
2441 mos7840_port = mos7840_get_port_private(port);
2443 if (mos7840_port == NULL)
2446 tty = mos7840_port->port->tty;
2448 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2451 /* return number of bytes available */
2454 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
2455 return mos7840_get_bytes_avail(mos7840_port, argp);
2458 dbg("%s (%d) TIOCOUTQ", __FUNCTION__, port->number);
2459 return put_user(tty->driver->chars_in_buffer ?
2460 tty->driver->chars_in_buffer(tty) : 0,
2464 retval = tty_check_change(tty);
2468 ld = tty_ldisc_ref(tty);
2471 if (ld && ld->flush_buffer)
2472 ld->flush_buffer(tty);
2475 if (ld && ld->flush_buffer)
2476 ld->flush_buffer(tty);
2479 if (tty->driver->flush_buffer)
2480 tty->driver->flush_buffer(tty);
2483 tty_ldisc_deref(ld);
2486 tty_ldisc_deref(ld);
2490 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2491 return mos7840_get_lsr_info(mos7840_port, argp);
2497 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
2500 mos7840_set_modem_info(mos7840_port, cmd, argp);
2504 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
2505 return mos7840_get_modem_info(mos7840_port, argp);
2508 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2509 return mos7840_get_serial_info(mos7840_port, argp);
2512 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2516 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2517 cprev = mos7840_port->icount;
2519 //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
2520 mos7840_port->delta_msr_cond = 0;
2521 wait_event_interruptible(mos7840_port->delta_msr_wait,
2523 delta_msr_cond == 1));
2525 /* see if a signal did it */
2526 if (signal_pending(current))
2527 return -ERESTARTSYS;
2528 cnow = mos7840_port->icount;
2530 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2531 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2532 return -EIO; /* no change => error */
2533 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2534 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2535 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2536 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2545 cnow = mos7840_port->icount;
2547 icount.cts = cnow.cts;
2548 icount.dsr = cnow.dsr;
2549 icount.rng = cnow.rng;
2550 icount.dcd = cnow.dcd;
2551 icount.rx = cnow.rx;
2552 icount.tx = cnow.tx;
2553 icount.frame = cnow.frame;
2554 icount.overrun = cnow.overrun;
2555 icount.parity = cnow.parity;
2556 icount.brk = cnow.brk;
2557 icount.buf_overrun = cnow.buf_overrun;
2559 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2560 port->number, icount.rx, icount.tx);
2561 if (copy_to_user(argp, &icount, sizeof(icount)))
2571 return -ENOIOCTLCMD;
2574 static int mos7840_calc_num_ports(struct usb_serial *serial)
2576 int mos7840_num_ports = 0;
2578 dbg("numberofendpoints: %d \n",
2579 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
2580 dbg("numberofendpoints: %d \n",
2581 (int)serial->interface->altsetting->desc.bNumEndpoints);
2582 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2583 mos7840_num_ports = serial->num_ports = 2;
2584 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
2585 serial->num_bulk_in = 4;
2586 serial->num_bulk_out = 4;
2587 mos7840_num_ports = serial->num_ports = 4;
2590 return mos7840_num_ports;
2593 /****************************************************************************
2595 ****************************************************************************/
2597 static int mos7840_startup(struct usb_serial *serial)
2599 struct moschip_port *mos7840_port;
2600 struct usb_device *dev;
2604 dbg("%s \n", " mos7840_startup :entering..........");
2607 dbg("%s\n", "Invalid Handler");
2613 dbg("%s\n", "Entering...");
2615 /* we set up the pointers to the endpoints in the mos7840_open *
2616 * function, as the structures aren't created yet. */
2618 /* set up port private structures */
2619 for (i = 0; i < serial->num_ports; ++i) {
2620 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2621 if (mos7840_port == NULL) {
2622 err("%s - Out of memory", __FUNCTION__);
2624 i--; /* don't follow NULL pointer cleaning up */
2628 /* Initialize all port interrupt end point to port 0 int endpoint *
2629 * Our device has only one interrupt end point comman to all port */
2631 mos7840_port->port = serial->port[i];
2632 mos7840_set_port_private(serial->port[i], mos7840_port);
2633 spin_lock_init(&mos7840_port->pool_lock);
2635 mos7840_port->port_num = ((serial->port[i]->number -
2636 (serial->port[i]->serial->minor)) +
2639 if (mos7840_port->port_num == 1) {
2640 mos7840_port->SpRegOffset = 0x0;
2641 mos7840_port->ControlRegOffset = 0x1;
2642 mos7840_port->DcrRegOffset = 0x4;
2643 } else if ((mos7840_port->port_num == 2)
2644 && (serial->num_ports == 4)) {
2645 mos7840_port->SpRegOffset = 0x8;
2646 mos7840_port->ControlRegOffset = 0x9;
2647 mos7840_port->DcrRegOffset = 0x16;
2648 } else if ((mos7840_port->port_num == 2)
2649 && (serial->num_ports == 2)) {
2650 mos7840_port->SpRegOffset = 0xa;
2651 mos7840_port->ControlRegOffset = 0xb;
2652 mos7840_port->DcrRegOffset = 0x19;
2653 } else if ((mos7840_port->port_num == 3)
2654 && (serial->num_ports == 4)) {
2655 mos7840_port->SpRegOffset = 0xa;
2656 mos7840_port->ControlRegOffset = 0xb;
2657 mos7840_port->DcrRegOffset = 0x19;
2658 } else if ((mos7840_port->port_num == 4)
2659 && (serial->num_ports == 4)) {
2660 mos7840_port->SpRegOffset = 0xc;
2661 mos7840_port->ControlRegOffset = 0xd;
2662 mos7840_port->DcrRegOffset = 0x1c;
2664 mos7840_dump_serial_port(mos7840_port);
2666 mos7840_set_port_private(serial->port[i], mos7840_port);
2668 //enable rx_disable bit in control register
2671 mos7840_get_reg_sync(serial->port[i],
2672 mos7840_port->ControlRegOffset, &Data);
2674 dbg("Reading ControlReg failed status-0x%x\n", status);
2677 dbg("ControlReg Reading success val is %x, status%d\n",
2679 Data |= 0x08; //setting driver done bit
2680 Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
2682 //Data |= 0x20; //rx_disable bit
2685 mos7840_set_reg_sync(serial->port[i],
2686 mos7840_port->ControlRegOffset, Data);
2688 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2691 dbg("ControlReg Writing success(rx_disable) status%d\n",
2694 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2698 mos7840_set_reg_sync(serial->port[i],
2699 (__u16) (mos7840_port->DcrRegOffset +
2702 dbg("Writing DCR0 failed status-0x%x\n", status);
2705 dbg("DCR0 Writing success status%d\n", status);
2710 mos7840_set_reg_sync(serial->port[i],
2711 (__u16) (mos7840_port->DcrRegOffset +
2714 dbg("Writing DCR1 failed status-0x%x\n", status);
2717 dbg("DCR1 Writing success status%d\n", status);
2722 mos7840_set_reg_sync(serial->port[i],
2723 (__u16) (mos7840_port->DcrRegOffset +
2726 dbg("Writing DCR2 failed status-0x%x\n", status);
2729 dbg("DCR2 Writing success status%d\n", status);
2731 // write values in clkstart0x0 and clkmulti 0x20
2735 mos7840_set_reg_sync(serial->port[i],
2736 CLK_START_VALUE_REGISTER, Data);
2738 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2741 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2745 mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2748 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2752 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2755 //write value 0x0 to scratchpad register
2758 mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
2761 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2765 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2768 //Zero Length flag register
2769 if ((mos7840_port->port_num != 1)
2770 && (serial->num_ports == 2)) {
2774 status = mos7840_set_reg_sync(serial->port[i],
2780 dbg("ZLIP offset%x\n",
2782 ((__u16) mos7840_port->port_num)));
2784 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2788 dbg("ZLP_REG%d Writing success status%d\n",
2793 status = mos7840_set_reg_sync(serial->port[i],
2799 dbg("ZLIP offset%x\n",
2801 ((__u16) mos7840_port->port_num) - 0x1));
2803 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2807 dbg("ZLP_REG%d Writing success status%d\n",
2811 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2812 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2813 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
2814 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf || !mos7840_port->dr) {
2820 //Zero Length flag enable
2822 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2824 dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
2827 dbg("ZLP_REG5 Writing success status%d\n", status);
2829 /* setting configuration feature to one */
2830 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2831 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2834 for (/* nothing */; i >= 0; i--) {
2835 mos7840_port = mos7840_get_port_private(serial->port[i]);
2837 kfree(mos7840_port->dr);
2838 kfree(mos7840_port->ctrl_buf);
2839 usb_free_urb(mos7840_port->control_urb);
2840 kfree(mos7840_port);
2841 serial->port[i] = NULL;
2846 /****************************************************************************
2848 * This function is called whenever the device is removed from the usb bus.
2849 ****************************************************************************/
2851 static void mos7840_shutdown(struct usb_serial *serial)
2854 unsigned long flags;
2855 struct moschip_port *mos7840_port;
2856 dbg("%s \n", " shutdown :entering..........");
2859 dbg("%s", "Invalid Handler \n");
2863 /* check for the ports to be closed,close the ports and disconnect */
2865 /* free private structure allocated for serial port *
2866 * stop reads and writes on all ports */
2868 for (i = 0; i < serial->num_ports; ++i) {
2869 mos7840_port = mos7840_get_port_private(serial->port[i]);
2870 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2871 mos7840_port->zombie = 1;
2872 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2873 usb_kill_urb(mos7840_port->control_urb);
2874 kfree(mos7840_port->ctrl_buf);
2875 kfree(mos7840_port->dr);
2876 kfree(mos7840_port);
2877 mos7840_set_port_private(serial->port[i], NULL);
2880 dbg("%s\n", "Thank u :: ");
2884 static struct usb_driver io_driver = {
2886 .probe = usb_serial_probe,
2887 .disconnect = usb_serial_disconnect,
2888 .id_table = moschip_id_table_combined,
2892 static struct usb_serial_driver moschip7840_4port_device = {
2894 .owner = THIS_MODULE,
2897 .description = DRIVER_DESC,
2898 .usb_driver = &io_driver,
2899 .id_table = moschip_port_id_table,
2900 .num_interrupt_in = 1, //NUM_DONT_CARE,//1,
2906 .open = mos7840_open,
2907 .close = mos7840_close,
2908 .write = mos7840_write,
2909 .write_room = mos7840_write_room,
2910 .chars_in_buffer = mos7840_chars_in_buffer,
2911 .throttle = mos7840_throttle,
2912 .unthrottle = mos7840_unthrottle,
2913 .calc_num_ports = mos7840_calc_num_ports,
2914 #ifdef MCSSerialProbe
2915 .probe = mos7840_serial_probe,
2917 .ioctl = mos7840_ioctl,
2918 .set_termios = mos7840_set_termios,
2919 .break_ctl = mos7840_break,
2920 .tiocmget = mos7840_tiocmget,
2921 .tiocmset = mos7840_tiocmset,
2922 .attach = mos7840_startup,
2923 .shutdown = mos7840_shutdown,
2924 .read_bulk_callback = mos7840_bulk_in_callback,
2925 .read_int_callback = mos7840_interrupt_callback,
2928 /****************************************************************************
2930 * This is called by the module subsystem, or on startup to initialize us
2931 ****************************************************************************/
2932 static int __init moschip7840_init(void)
2936 dbg("%s \n", " mos7840_init :entering..........");
2938 /* Register with the usb serial */
2939 retval = usb_serial_register(&moschip7840_4port_device);
2942 goto failed_port_device_register;
2944 dbg("%s\n", "Entring...");
2945 info(DRIVER_DESC " " DRIVER_VERSION);
2947 /* Register with the usb */
2948 retval = usb_register(&io_driver);
2951 goto failed_usb_register;
2954 dbg("%s\n", "Leaving...");
2958 failed_usb_register:
2959 usb_serial_deregister(&moschip7840_4port_device);
2961 failed_port_device_register:
2966 /****************************************************************************
2968 * Called when the driver is about to be unloaded.
2969 ****************************************************************************/
2970 static void __exit moschip7840_exit(void)
2973 dbg("%s \n", " mos7840_exit :entering..........");
2975 usb_deregister(&io_driver);
2977 usb_serial_deregister(&moschip7840_4port_device);
2979 dbg("%s\n", "Entring...");
2982 module_init(moschip7840_init);
2983 module_exit(moschip7840_exit);
2985 /* Module information */
2986 MODULE_DESCRIPTION(DRIVER_DESC);
2987 MODULE_LICENSE("GPL");
2989 module_param(debug, bool, S_IRUGO | S_IWUSR);
2990 MODULE_PARM_DESC(debug, "Debug enabled or not");