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 */
179 __u8 shadowLCR; /* last LCR value received */
180 __u8 shadowMCR; /* last MCR value received */
182 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
183 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
185 struct async_icount icount;
186 struct usb_serial_port *port; /* loop back to the owner of this object */
190 __u8 ControlRegOffset;
192 //for processing control URBS in interrupt context
193 struct urb *control_urb;
197 struct urb *write_urb_pool[NUM_URBS];
202 static int mos7840_num_ports; //this says the number of ports in the device
203 static int mos7840_num_open_ports;
207 * mos7840_set_reg_sync
208 * To set the Control register by calling usb_fill_control_urb function
209 * by passing usb_sndctrlpipe function as parameter.
212 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
215 struct usb_device *dev = port->serial->dev;
217 dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
219 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
220 MCS_WR_RTYPE, val, reg, NULL, 0,
225 * mos7840_get_reg_sync
226 * To set the Uart register by calling usb_fill_control_urb function by
227 * passing usb_rcvctrlpipe function as parameter.
230 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
233 struct usb_device *dev = port->serial->dev;
236 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
237 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
239 dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
240 *val = (*val) & 0x00ff;
245 * mos7840_set_uart_reg
246 * To set the Uart register by calling usb_fill_control_urb function by
247 * passing usb_sndctrlpipe function as parameter.
250 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
254 struct usb_device *dev = port->serial->dev;
256 // For the UART control registers, the application number need to be Or'ed
257 if (mos7840_num_ports == 4) {
259 (((__u16) port->number - (__u16) (port->serial->minor)) +
261 dbg("mos7840_set_uart_reg application number is %x\n", val);
263 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
265 (((__u16) port->number -
266 (__u16) (port->serial->minor)) + 1) << 8;
267 dbg("mos7840_set_uart_reg application number is %x\n",
271 (((__u16) port->number -
272 (__u16) (port->serial->minor)) + 2) << 8;
273 dbg("mos7840_set_uart_reg application number is %x\n",
277 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
278 MCS_WR_RTYPE, val, reg, NULL, 0,
284 * mos7840_get_uart_reg
285 * To set the Control register by calling usb_fill_control_urb function
286 * by passing usb_rcvctrlpipe function as parameter.
288 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
291 struct usb_device *dev = port->serial->dev;
295 //dbg("application number is %4x \n",(((__u16)port->number - (__u16)(port->serial->minor))+1)<<8);
296 /*Wval is same as application number */
297 if (mos7840_num_ports == 4) {
299 (((__u16) port->number - (__u16) (port->serial->minor)) +
301 dbg("mos7840_get_uart_reg application number is %x\n", Wval);
303 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
305 (((__u16) port->number -
306 (__u16) (port->serial->minor)) + 1) << 8;
307 dbg("mos7840_get_uart_reg application number is %x\n",
311 (((__u16) port->number -
312 (__u16) (port->serial->minor)) + 2) << 8;
313 dbg("mos7840_get_uart_reg application number is %x\n",
317 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
318 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
320 *val = (*val) & 0x00ff;
324 static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
327 dbg("***************************************\n");
328 dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
329 dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
330 dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
331 dbg("***************************************\n");
335 /************************************************************************/
336 /************************************************************************/
337 /* I N T E R F A C E F U N C T I O N S */
338 /* I N T E R F A C E F U N C T I O N S */
339 /************************************************************************/
340 /************************************************************************/
342 static inline void mos7840_set_port_private(struct usb_serial_port *port,
343 struct moschip_port *data)
345 usb_set_serial_port_data(port, (void *)data);
348 static inline struct moschip_port *mos7840_get_port_private(struct
352 return (struct moschip_port *)usb_get_serial_port_data(port);
355 static int mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
357 struct moschip_port *mos7840_port;
358 struct async_icount *icount;
360 icount = &mos7840_port->icount;
362 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
364 icount = &mos7840_port->icount;
366 /* update input line counters */
367 if (new_msr & MOS_MSR_DELTA_CTS) {
370 if (new_msr & MOS_MSR_DELTA_DSR) {
373 if (new_msr & MOS_MSR_DELTA_CD) {
376 if (new_msr & MOS_MSR_DELTA_RI) {
384 static int mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
386 struct async_icount *icount;
388 dbg("%s - %02x", __FUNCTION__, new_lsr);
390 if (new_lsr & SERIAL_LSR_BI) {
392 // Parity and Framing errors only count if they
393 // occur exclusive of a break being
396 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
399 /* update input line counters */
400 icount = &port->icount;
401 if (new_lsr & SERIAL_LSR_BI) {
404 if (new_lsr & SERIAL_LSR_OE) {
407 if (new_lsr & SERIAL_LSR_PE) {
410 if (new_lsr & SERIAL_LSR_FE) {
417 /************************************************************************/
418 /************************************************************************/
419 /* U S B C A L L B A C K F U N C T I O N S */
420 /* U S B C A L L B A C K F U N C T I O N S */
421 /************************************************************************/
422 /************************************************************************/
424 static void mos7840_control_callback(struct urb *urb, struct pt_regs *regs)
427 struct moschip_port *mos7840_port;
431 dbg("%s", "Invalid Pointer !!!!:\n");
435 switch (urb->status) {
442 /* this urb is terminated, clean up */
443 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
447 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
452 mos7840_port = (struct moschip_port *)urb->context;
454 dbg("%s urb buffer size is %d\n", __FUNCTION__, urb->actual_length);
455 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __FUNCTION__,
456 mos7840_port->MsrLsr, mos7840_port->port_num);
457 data = urb->transfer_buffer;
458 regval = (__u8) data[0];
459 dbg("%s data is %x\n", __FUNCTION__, regval);
460 if (mos7840_port->MsrLsr == 0)
461 mos7840_handle_new_msr(mos7840_port, regval);
462 else if (mos7840_port->MsrLsr == 1)
463 mos7840_handle_new_lsr(mos7840_port, regval);
469 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
472 struct usb_device *dev = mcs->port->serial->dev;
473 struct usb_ctrlrequest *dr = NULL;
474 unsigned char *buffer = NULL;
476 buffer = (__u8 *) mcs->ctrl_buf;
478 // dr=(struct usb_ctrlrequest *)(buffer);
479 dr = (void *)(buffer + 2);
480 dr->bRequestType = MCS_RD_RTYPE;
481 dr->bRequest = MCS_RDREQ;
482 dr->wValue = cpu_to_le16(Wval); //0;
483 dr->wIndex = cpu_to_le16(reg);
484 dr->wLength = cpu_to_le16(2);
486 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
487 (unsigned char *)dr, buffer, 2,
488 mos7840_control_callback, mcs);
489 mcs->control_urb->transfer_buffer_length = 2;
490 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
494 /*****************************************************************************
495 * mos7840_interrupt_callback
496 * this is the callback function for when we have received data on the
497 * interrupt endpoint.
498 *****************************************************************************/
500 static void mos7840_interrupt_callback(struct urb *urb, struct pt_regs *regs)
504 struct moschip_port *mos7840_port;
505 struct usb_serial *serial;
512 dbg("%s", " : Entering\n");
514 dbg("%s", "Invalid Pointer !!!!:\n");
518 switch (urb->status) {
525 /* this urb is terminated, clean up */
526 dbg("%s - urb shutting down with status: %d", __FUNCTION__,
530 dbg("%s - nonzero urb status received: %d", __FUNCTION__,
535 length = urb->actual_length;
536 data = urb->transfer_buffer;
538 serial = (struct usb_serial *)urb->context;
540 /* Moschip get 5 bytes
541 * Byte 1 IIR Port 1 (port.number is 0)
542 * Byte 2 IIR Port 2 (port.number is 1)
543 * Byte 3 IIR Port 3 (port.number is 2)
544 * Byte 4 IIR Port 4 (port.number is 3)
545 * Byte 5 FIFO status for both */
547 if (length && length > 5) {
548 dbg("%s \n", "Wrong data !!!");
552 sp[0] = (__u8) data[0];
553 sp[1] = (__u8) data[1];
554 sp[2] = (__u8) data[2];
555 sp[3] = (__u8) data[3];
558 for (i = 0; i < serial->num_ports; i++) {
559 mos7840_port = mos7840_get_port_private(serial->port[i]);
561 (((__u16) serial->port[i]->number -
562 (__u16) (serial->minor)) + 1) << 8;
563 if (mos7840_port->open) {
565 dbg("SP%d No Interrupt !!!\n", i);
567 switch (sp[i] & 0x0f) {
569 dbg("Serial Port %d: Receiver status error or ", i);
570 dbg("address bit detected in 9-bit mode\n");
571 mos7840_port->MsrLsr = 1;
572 mos7840_get_reg(mos7840_port, wval,
573 LINE_STATUS_REGISTER,
577 dbg("Serial Port %d: Modem status change\n", i);
578 mos7840_port->MsrLsr = 0;
579 mos7840_get_reg(mos7840_port, wval,
580 MODEM_STATUS_REGISTER,
588 result = usb_submit_urb(urb, GFP_ATOMIC);
590 dev_err(&urb->dev->dev,
591 "%s - Error %d submitting interrupt urb\n",
592 __FUNCTION__, result);
599 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
600 const char *function)
603 dbg("%s - port == NULL", function);
607 dbg("%s - port->serial == NULL", function);
614 /* Inline functions to check the sanity of a pointer that is passed to us */
615 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
616 const char *function)
619 dbg("%s - serial == NULL", function);
623 dbg("%s - serial->type == NULL!", function);
630 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
631 const char *function)
633 /* if no port was specified, or it fails a paranoia check */
635 mos7840_port_paranoia_check(port, function) ||
636 mos7840_serial_paranoia_check(port->serial, function)) {
637 /* then say that we don't have a valid usb_serial thing, which will * end up genrating -ENODEV return values */
644 /*****************************************************************************
645 * mos7840_bulk_in_callback
646 * this is the callback function for when we have received data on the
648 *****************************************************************************/
650 static void mos7840_bulk_in_callback(struct urb *urb, struct pt_regs *regs)
654 struct usb_serial *serial;
655 struct usb_serial_port *port;
656 struct moschip_port *mos7840_port;
657 struct tty_struct *tty;
660 dbg("%s", "Invalid Pointer !!!!:\n");
665 dbg("nonzero read bulk status received: %d", urb->status);
669 mos7840_port = (struct moschip_port *)urb->context;
671 dbg("%s", "NULL mos7840_port pointer \n");
675 port = (struct usb_serial_port *)mos7840_port->port;
676 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
677 dbg("%s", "Port Paranoia failed \n");
681 serial = mos7840_get_usb_serial(port, __FUNCTION__);
683 dbg("%s\n", "Bad serial pointer ");
687 dbg("%s\n", "Entering... \n");
689 data = urb->transfer_buffer;
691 dbg("%s", "Entering ........... \n");
693 if (urb->actual_length) {
694 tty = mos7840_port->port->tty;
696 tty_buffer_request_room(tty, urb->actual_length);
697 tty_insert_flip_string(tty, data, urb->actual_length);
699 tty_flip_buffer_push(tty);
701 mos7840_port->icount.rx += urb->actual_length;
702 dbg("mos7840_port->icount.rx is %d:\n",
703 mos7840_port->icount.rx);
706 if (!mos7840_port->read_urb) {
707 dbg("%s", "URB KILLED !!!\n");
711 if (mos7840_port->read_urb->status != -EINPROGRESS) {
712 mos7840_port->read_urb->dev = serial->dev;
714 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
717 dbg(" usb_submit_urb(read bulk) failed, status = %d",
723 /*****************************************************************************
724 * mos7840_bulk_out_data_callback
725 * this is the callback function for when we have finished sending serial data
726 * on the bulk out endpoint.
727 *****************************************************************************/
729 static void mos7840_bulk_out_data_callback(struct urb *urb,
730 struct pt_regs *regs)
732 struct moschip_port *mos7840_port;
733 struct tty_struct *tty;
735 dbg("%s", "Invalid Pointer !!!!:\n");
740 dbg("nonzero write bulk status received:%d\n", urb->status);
744 mos7840_port = (struct moschip_port *)urb->context;
746 dbg("%s", "NULL mos7840_port pointer \n");
750 if (mos7840_port_paranoia_check(mos7840_port->port, __FUNCTION__)) {
751 dbg("%s", "Port Paranoia failed \n");
755 dbg("%s \n", "Entering .........");
757 tty = mos7840_port->port->tty;
759 if (tty && mos7840_port->open) {
760 /* let the tty driver wakeup if it has a special *
761 * write_wakeup function */
763 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
764 && tty->ldisc.write_wakeup) {
765 (tty->ldisc.write_wakeup) (tty);
768 /* tell the tty driver that something has changed */
769 wake_up_interruptible(&tty->write_wait);
774 /************************************************************************/
775 /* 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 */
776 /************************************************************************/
777 #ifdef MCSSerialProbe
778 static int mos7840_serial_probe(struct usb_serial *serial,
779 const struct usb_device_id *id)
782 /*need to implement the mode_reg reading and updating\
783 structures usb_serial_ device_type\
784 (i.e num_ports, num_bulkin,bulkout etc) */
785 /* Also we can update the changes attach */
790 /*****************************************************************************
792 * this function is called by the tty driver when a port is opened
793 * If successful, we return 0
794 * Otherwise we return a negative error number.
795 *****************************************************************************/
797 static int mos7840_open(struct usb_serial_port *port, struct file *filp)
801 struct usb_serial *serial;
805 struct moschip_port *mos7840_port;
807 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
808 dbg("%s", "Port Paranoia failed \n");
812 mos7840_num_open_ports++;
813 serial = port->serial;
815 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
816 dbg("%s", "Serial Paranoia failed \n");
820 mos7840_port = mos7840_get_port_private(port);
822 if (mos7840_port == NULL)
825 usb_clear_halt(serial->dev, port->write_urb->pipe);
826 usb_clear_halt(serial->dev, port->read_urb->pipe);
828 /* Initialising the write urb pool */
829 for (j = 0; j < NUM_URBS; ++j) {
830 urb = usb_alloc_urb(0, SLAB_ATOMIC);
831 mos7840_port->write_urb_pool[j] = urb;
834 err("No more urbs???");
838 urb->transfer_buffer = NULL;
839 urb->transfer_buffer =
840 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
841 if (!urb->transfer_buffer) {
842 err("%s-out of memory for urb buffers.", __FUNCTION__);
847 /*****************************************************************************
848 * Initialize MCS7840 -- Write Init values to corresponding Registers
856 * 0x08 : SP1/2 Control Reg
857 *****************************************************************************/
859 //NEED to check the following Block
863 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
865 dbg("Reading Spreg failed\n");
869 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
871 dbg("writing Spreg failed\n");
876 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
878 dbg("writing Spreg failed\n");
881 //End of block to be checked
886 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
888 dbg("Reading Controlreg failed\n");
891 Data |= 0x08; //Driver done bit
892 Data |= 0x20; //rx_disable
895 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
897 dbg("writing Controlreg failed\n");
900 //do register settings here
901 // Set all regs to the device default values.
902 ////////////////////////////////////
903 // First Disable all interrupts.
904 ////////////////////////////////////
908 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
910 dbg("disableing interrupts failed\n");
913 // Set FIFO_CONTROL_REGISTER to the default value
916 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
918 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
924 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
926 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
932 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
933 mos7840_port->shadowLCR = Data;
937 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
938 mos7840_port->shadowMCR = Data;
942 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
943 mos7840_port->shadowLCR = Data;
945 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
947 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
951 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
955 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
959 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
961 Data = Data & ~SERIAL_LCR_DLAB;
963 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
964 mos7840_port->shadowLCR = Data;
966 //clearing Bulkin and Bulkout Fifo
969 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
973 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
977 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
978 //Finally enable all interrupts
982 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
984 //clearing rx_disable
988 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
992 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
998 mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset, &Data);
1002 mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset, Data);
1004 /* force low_latency on so that our tty_push actually forces *
1005 * the data through,otherwise it is scheduled, and with *
1006 * high data rates (like with OHCI) data can get lost. */
1009 port->tty->low_latency = 1;
1010 /* Check to see if we've set up our endpoint info yet *
1011 * (can't set it up in mos7840_startup as the structures *
1012 * were not set up at that time.) */
1013 if (mos7840_num_open_ports == 1) {
1014 if (serial->port[0]->interrupt_in_buffer == NULL) {
1016 /* set up interrupt urb */
1018 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1020 usb_rcvintpipe(serial->dev,
1022 interrupt_in_endpointAddress),
1023 serial->port[0]->interrupt_in_buffer,
1024 serial->port[0]->interrupt_in_urb->
1025 transfer_buffer_length,
1026 mos7840_interrupt_callback,
1028 serial->port[0]->interrupt_in_urb->
1031 /* start interrupt read for mos7840 *
1032 * will continue as long as mos7840 is connected */
1035 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1038 err("%s - Error %d submitting interrupt urb",
1039 __FUNCTION__, response);
1046 /* see if we've set up our endpoint info yet *
1047 * (can't set it up in mos7840_startup as the *
1048 * structures were not set up at that time.) */
1050 dbg("port number is %d \n", port->number);
1051 dbg("serial number is %d \n", port->serial->minor);
1052 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1053 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1054 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
1055 dbg("port's number in the device is %d\n", mos7840_port->port_num);
1056 mos7840_port->read_urb = port->read_urb;
1058 /* set up our bulk in urb */
1060 usb_fill_bulk_urb(mos7840_port->read_urb,
1062 usb_rcvbulkpipe(serial->dev,
1063 port->bulk_in_endpointAddress),
1064 port->bulk_in_buffer,
1065 mos7840_port->read_urb->transfer_buffer_length,
1066 mos7840_bulk_in_callback, mos7840_port);
1068 dbg("mos7840_open: bulkin endpoint is %d\n",
1069 port->bulk_in_endpointAddress);
1070 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1072 err("%s - Error %d submitting control urb", __FUNCTION__,
1076 /* initialize our wait queues */
1077 init_waitqueue_head(&mos7840_port->wait_chase);
1078 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1080 /* initialize our icount structure */
1081 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1083 /* initialize our port settings */
1084 mos7840_port->shadowMCR = MCR_MASTER_IE; /* Must set to enable ints! */
1085 /* send a open port command */
1086 mos7840_port->open = 1;
1087 //mos7840_change_port_settings(mos7840_port,old_termios);
1088 mos7840_port->icount.tx = 0;
1089 mos7840_port->icount.rx = 0;
1091 dbg("\n\nusb_serial serial:%x mos7840_port:%x\n usb_serial_port port:%x\n\n", (unsigned int)serial, (unsigned int)mos7840_port, (unsigned int)port);
1097 /*****************************************************************************
1098 * mos7840_chars_in_buffer
1099 * this function is called by the tty driver when it wants to know how many
1100 * bytes of data we currently have outstanding in the port (data that has
1101 * been written, but hasn't made it out the port yet)
1102 * If successful, we return the number of bytes left to be written in the
1104 * Otherwise we return a negative error number.
1105 *****************************************************************************/
1107 static int mos7840_chars_in_buffer(struct usb_serial_port *port)
1111 struct moschip_port *mos7840_port;
1113 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
1115 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1116 dbg("%s", "Invalid port \n");
1120 mos7840_port = mos7840_get_port_private(port);
1121 if (mos7840_port == NULL) {
1122 dbg("%s \n", "mos7840_break:leaving ...........");
1126 for (i = 0; i < NUM_URBS; ++i) {
1127 if (mos7840_port->write_urb_pool[i]->status == -EINPROGRESS) {
1128 chars += URB_TRANSFER_BUFFER_SIZE;
1131 dbg("%s - returns %d", __FUNCTION__, chars);
1136 /************************************************************************
1138 * mos7840_block_until_tx_empty
1140 * This function will block the close until one of the following:
1142 * 2. The mos7840 has stopped
1143 * 3. A timout of 3 seconds without activity has expired
1145 ************************************************************************/
1146 static void mos7840_block_until_tx_empty(struct moschip_port *mos7840_port)
1148 int timeout = HZ / 10;
1154 count = mos7840_chars_in_buffer(mos7840_port->port);
1156 /* Check for Buffer status */
1161 /* Block the thread for a while */
1162 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1165 /* No activity.. count down section */
1168 dbg("%s - TIMEOUT", __FUNCTION__);
1171 /* Reset timout value back to seconds */
1177 /*****************************************************************************
1179 * this function is called by the tty driver when a port is closed
1180 *****************************************************************************/
1182 static void mos7840_close(struct usb_serial_port *port, struct file *filp)
1184 struct usb_serial *serial;
1185 struct moschip_port *mos7840_port;
1189 dbg("%s\n", "mos7840_close:entering...");
1191 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1192 dbg("%s", "Port Paranoia failed \n");
1196 serial = mos7840_get_usb_serial(port, __FUNCTION__);
1198 dbg("%s", "Serial Paranoia failed \n");
1202 mos7840_port = mos7840_get_port_private(port);
1204 if (mos7840_port == NULL) {
1208 for (j = 0; j < NUM_URBS; ++j)
1209 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1211 /* Freeing Write URBs */
1212 for (j = 0; j < NUM_URBS; ++j) {
1213 if (mos7840_port->write_urb_pool[j]) {
1214 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1215 kfree(mos7840_port->write_urb_pool[j]->
1218 usb_free_urb(mos7840_port->write_urb_pool[j]);
1223 /* flush and block until tx is empty */
1224 mos7840_block_until_tx_empty(mos7840_port);
1227 /* While closing port, shutdown all bulk read, write *
1228 * and interrupt read if they exists */
1231 if (mos7840_port->write_urb) {
1232 dbg("%s", "Shutdown bulk write\n");
1233 usb_kill_urb(mos7840_port->write_urb);
1236 if (mos7840_port->read_urb) {
1237 dbg("%s", "Shutdown bulk read\n");
1238 usb_kill_urb(mos7840_port->read_urb);
1240 if ((&mos7840_port->control_urb)) {
1241 dbg("%s", "Shutdown control read\n");
1242 // usb_kill_urb (mos7840_port->control_urb);
1246 // if(mos7840_port->ctrl_buf != NULL)
1247 // kfree(mos7840_port->ctrl_buf);
1248 mos7840_num_open_ports--;
1249 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1250 mos7840_num_open_ports, port->number);
1251 if (mos7840_num_open_ports == 0) {
1252 if (serial->port[0]->interrupt_in_urb) {
1253 dbg("%s", "Shutdown interrupt_in_urb\n");
1257 if (mos7840_port->write_urb) {
1258 /* if this urb had a transfer buffer already (old tx) free it */
1260 if (mos7840_port->write_urb->transfer_buffer != NULL) {
1261 kfree(mos7840_port->write_urb->transfer_buffer);
1263 usb_free_urb(mos7840_port->write_urb);
1267 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1270 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1272 mos7840_port->open = 0;
1274 dbg("%s \n", "Leaving ............");
1277 /************************************************************************
1279 * mos7840_block_until_chase_response
1281 * This function will block the close until one of the following:
1282 * 1. Response to our Chase comes from mos7840
1283 * 2. A timout of 10 seconds without activity has expired
1284 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1286 ************************************************************************/
1288 static void mos7840_block_until_chase_response(struct moschip_port
1291 int timeout = 1 * HZ;
1296 count = mos7840_chars_in_buffer(mos7840_port->port);
1298 /* Check for Buffer status */
1303 /* Block the thread for a while */
1304 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1306 /* No activity.. count down section */
1309 dbg("%s - TIMEOUT", __FUNCTION__);
1312 /* Reset timout value back to seconds */
1319 /*****************************************************************************
1321 * this function sends a break to the port
1322 *****************************************************************************/
1323 static void mos7840_break(struct usb_serial_port *port, int break_state)
1326 struct usb_serial *serial;
1327 struct moschip_port *mos7840_port;
1329 dbg("%s \n", "Entering ...........");
1330 dbg("mos7840_break: Start\n");
1332 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1333 dbg("%s", "Port Paranoia failed \n");
1337 serial = mos7840_get_usb_serial(port, __FUNCTION__);
1339 dbg("%s", "Serial Paranoia failed \n");
1343 mos7840_port = mos7840_get_port_private(port);
1345 if (mos7840_port == NULL) {
1351 /* flush and block until tx is empty */
1352 mos7840_block_until_chase_response(mos7840_port);
1355 if (break_state == -1) {
1356 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1358 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1361 mos7840_port->shadowLCR = data;
1362 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1363 mos7840_port->shadowLCR);
1364 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1365 mos7840_port->shadowLCR);
1370 /*****************************************************************************
1371 * mos7840_write_room
1372 * this function is called by the tty driver when it wants to know how many
1373 * bytes of data we can accept for a specific port.
1374 * If successful, we return the amount of room that we have for this port
1375 * Otherwise we return a negative error number.
1376 *****************************************************************************/
1378 static int mos7840_write_room(struct usb_serial_port *port)
1382 struct moschip_port *mos7840_port;
1384 dbg("%s \n", " mos7840_write_room:entering ...........");
1386 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1387 dbg("%s", "Invalid port \n");
1388 dbg("%s \n", " mos7840_write_room:leaving ...........");
1392 mos7840_port = mos7840_get_port_private(port);
1393 if (mos7840_port == NULL) {
1394 dbg("%s \n", "mos7840_break:leaving ...........");
1398 for (i = 0; i < NUM_URBS; ++i) {
1399 if (mos7840_port->write_urb_pool[i]->status != -EINPROGRESS) {
1400 room += URB_TRANSFER_BUFFER_SIZE;
1404 dbg("%s - returns %d", __FUNCTION__, room);
1409 /*****************************************************************************
1411 * this function is called by the tty driver when data should be written to
1413 * If successful, we return the number of bytes written, otherwise we
1414 * return a negative error number.
1415 *****************************************************************************/
1417 static int mos7840_write(struct usb_serial_port *port,
1418 const unsigned char *data, int count)
1426 struct moschip_port *mos7840_port;
1427 struct usb_serial *serial;
1430 const unsigned char *current_position = data;
1431 unsigned char *data1;
1432 dbg("%s \n", "entering ...........");
1433 //dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",mos7840_port->shadowLCR);
1438 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1439 mos7840_port->shadowLCR = Data;
1440 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x\n", Data);
1441 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1442 mos7840_port->shadowLCR);
1445 //status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data);
1446 //mos7840_port->shadowLCR=Data;//Need to add later
1448 Data |= SERIAL_LCR_DLAB; //data latch enable in LCR 0x80
1450 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1453 //status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data);
1456 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1457 dbg("mos7840_write:DLL value is %x\n", Data);
1461 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1462 dbg("mos7840_write:DLM value is %x\n", Data);
1464 Data = Data & ~SERIAL_LCR_DLAB;
1465 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1466 mos7840_port->shadowLCR);
1468 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1471 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1472 dbg("%s", "Port Paranoia failed \n");
1476 serial = port->serial;
1477 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
1478 dbg("%s", "Serial Paranoia failed \n");
1482 mos7840_port = mos7840_get_port_private(port);
1483 if (mos7840_port == NULL) {
1484 dbg("%s", "mos7840_port is NULL\n");
1488 /* try to find a free urb in the list */
1491 for (i = 0; i < NUM_URBS; ++i) {
1492 if (mos7840_port->write_urb_pool[i]->status != -EINPROGRESS) {
1493 urb = mos7840_port->write_urb_pool[i];
1500 dbg("%s - no more free urbs", __FUNCTION__);
1504 if (urb->transfer_buffer == NULL) {
1505 urb->transfer_buffer =
1506 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1508 if (urb->transfer_buffer == NULL) {
1509 err("%s no more kernel memory...", __FUNCTION__);
1513 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1517 (urb->transfer_buffer, current_position, transfer_size)) {
1518 bytes_sent = -EFAULT;
1522 memcpy(urb->transfer_buffer, current_position, transfer_size);
1525 /* fill urb with data and submit */
1526 usb_fill_bulk_urb(urb,
1528 usb_sndbulkpipe(serial->dev,
1529 port->bulk_out_endpointAddress),
1530 urb->transfer_buffer,
1532 mos7840_bulk_out_data_callback, mos7840_port);
1534 data1 = urb->transfer_buffer;
1535 dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1537 /* send it down the pipe */
1538 status = usb_submit_urb(urb, GFP_ATOMIC);
1541 err("%s - usb_submit_urb(write bulk) failed with status = %d",
1542 __FUNCTION__, status);
1543 bytes_sent = status;
1546 bytes_sent = transfer_size;
1547 mos7840_port->icount.tx += transfer_size;
1548 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1555 /*****************************************************************************
1557 * this function is called by the tty driver when it wants to stop the data
1558 * being read from the port.
1559 *****************************************************************************/
1561 static void mos7840_throttle(struct usb_serial_port *port)
1563 struct moschip_port *mos7840_port;
1564 struct tty_struct *tty;
1567 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1568 dbg("%s", "Invalid port \n");
1572 dbg("- port %d\n", port->number);
1574 mos7840_port = mos7840_get_port_private(port);
1576 if (mos7840_port == NULL)
1579 if (!mos7840_port->open) {
1580 dbg("%s\n", "port not opened");
1584 dbg("%s", "Entering .......... \n");
1588 dbg("%s - no tty available", __FUNCTION__);
1592 /* if we are implementing XON/XOFF, send the stop character */
1594 unsigned char stop_char = STOP_CHAR(tty);
1595 status = mos7840_write(port, &stop_char, 1);
1601 /* if we are implementing RTS/CTS, toggle that line */
1602 if (tty->termios->c_cflag & CRTSCTS) {
1603 mos7840_port->shadowMCR &= ~MCR_RTS;
1606 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1607 mos7840_port->shadowMCR);
1617 /*****************************************************************************
1618 * mos7840_unthrottle
1619 * this function is called by the tty driver when it wants to resume the data
1620 * being read from the port (called after SerialThrottle is called)
1621 *****************************************************************************/
1622 static void mos7840_unthrottle(struct usb_serial_port *port)
1624 struct tty_struct *tty;
1626 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1628 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1629 dbg("%s", "Invalid port \n");
1633 if (mos7840_port == NULL)
1636 if (!mos7840_port->open) {
1637 dbg("%s - port not opened", __FUNCTION__);
1641 dbg("%s", "Entering .......... \n");
1645 dbg("%s - no tty available", __FUNCTION__);
1649 /* if we are implementing XON/XOFF, send the start character */
1651 unsigned char start_char = START_CHAR(tty);
1652 status = mos7840_write(port, &start_char, 1);
1658 /* if we are implementing RTS/CTS, toggle that line */
1659 if (tty->termios->c_cflag & CRTSCTS) {
1660 mos7840_port->shadowMCR |= MCR_RTS;
1663 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1664 mos7840_port->shadowMCR);
1673 static int mos7840_tiocmget(struct usb_serial_port *port, struct file *file)
1675 struct moschip_port *mos7840_port;
1676 unsigned int result;
1680 mos7840_port = mos7840_get_port_private(port);
1682 dbg("%s - port %d", __FUNCTION__, port->number);
1684 if (mos7840_port == NULL)
1687 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1688 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1689 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1690 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1691 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1692 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1693 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1694 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1695 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1697 dbg("%s - 0x%04X", __FUNCTION__, result);
1702 static int mos7840_tiocmset(struct usb_serial_port *port, struct file *file,
1703 unsigned int set, unsigned int clear)
1705 struct moschip_port *mos7840_port;
1707 unsigned int status;
1709 dbg("%s - port %d", __FUNCTION__, port->number);
1711 mos7840_port = mos7840_get_port_private(port);
1713 if (mos7840_port == NULL)
1716 mcr = mos7840_port->shadowMCR;
1717 if (clear & TIOCM_RTS)
1719 if (clear & TIOCM_DTR)
1721 if (clear & TIOCM_LOOP)
1722 mcr &= ~MCR_LOOPBACK;
1724 if (set & TIOCM_RTS)
1726 if (set & TIOCM_DTR)
1728 if (set & TIOCM_LOOP)
1729 mcr |= MCR_LOOPBACK;
1731 mos7840_port->shadowMCR = mcr;
1734 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1736 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
1743 /*****************************************************************************
1744 * mos7840_calc_baud_rate_divisor
1745 * this function calculates the proper baud rate divisor for the specified
1747 *****************************************************************************/
1748 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1749 __u16 * clk_sel_val)
1752 dbg("%s - %d", __FUNCTION__, baudRate);
1754 if (baudRate <= 115200) {
1755 *divisor = 115200 / baudRate;
1758 if ((baudRate > 115200) && (baudRate <= 230400)) {
1759 *divisor = 230400 / baudRate;
1760 *clk_sel_val = 0x10;
1761 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1762 *divisor = 403200 / baudRate;
1763 *clk_sel_val = 0x20;
1764 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1765 *divisor = 460800 / baudRate;
1766 *clk_sel_val = 0x30;
1767 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1768 *divisor = 806400 / baudRate;
1769 *clk_sel_val = 0x40;
1770 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1771 *divisor = 921600 / baudRate;
1772 *clk_sel_val = 0x50;
1773 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1774 *divisor = 1572864 / baudRate;
1775 *clk_sel_val = 0x60;
1776 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1777 *divisor = 3145728 / baudRate;
1778 *clk_sel_val = 0x70;
1784 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1785 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1786 *divisor = mos7840_divisor_table[i].Divisor;
1791 /* After trying for all the standard baud rates *
1792 * Try calculating the divisor for this baud rate */
1794 if (baudrate > 75 && baudrate < 230400) {
1795 /* get the divisor */
1796 custom = (__u16) (230400L / baudrate);
1798 /* Check for round off */
1799 round1 = (__u16) (2304000L / baudrate);
1800 round = (__u16) (round1 - (custom * 10));
1806 dbg(" Baud %d = %d\n", baudrate, custom);
1810 dbg("%s\n", " Baud calculation Failed...");
1815 /*****************************************************************************
1816 * mos7840_send_cmd_write_baud_rate
1817 * this function sends the proper command to change the baud rate of the
1819 *****************************************************************************/
1821 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1827 unsigned char number;
1829 struct usb_serial_port *port;
1831 if (mos7840_port == NULL)
1834 port = (struct usb_serial_port *)mos7840_port->port;
1835 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1836 dbg("%s", "Invalid port \n");
1840 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1841 dbg("%s", "Invalid Serial \n");
1845 dbg("%s", "Entering .......... \n");
1847 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1849 dbg("%s - port = %d, baud = %d", __FUNCTION__,
1850 mos7840_port->port->number, baudRate);
1851 //reset clk_uart_sel in spregOffset
1852 if (baudRate > 115200) {
1853 #ifdef HW_flow_control
1854 //NOTE: need to see the pther register to modify
1855 //setting h/w flow control bit to 1;
1858 mos7840_port->shadowMCR = Data;
1860 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1862 dbg("Writing spreg failed in set_serial_baud\n");
1868 #ifdef HW_flow_control
1869 //setting h/w flow control bit to 0;
1872 mos7840_port->shadowMCR = Data;
1874 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1876 dbg("Writing spreg failed in set_serial_baud\n");
1883 if (1) //baudRate <= 115200)
1889 mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1892 mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1895 dbg("reading spreg failed in set_serial_baud\n");
1898 Data = (Data & 0x8f) | clk_sel_val;
1901 mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
1903 dbg("Writing spreg failed in set_serial_baud\n");
1906 /* Calculate the Divisor */
1909 err("%s - bad baud rate", __FUNCTION__);
1910 dbg("%s\n", "bad baud rate");
1913 /* Enable access to divisor latch */
1914 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1915 mos7840_port->shadowLCR = Data;
1916 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1918 /* Write the divisor */
1919 Data = (unsigned char)(divisor & 0xff);
1920 dbg("set_serial_baud Value to write DLL is %x\n", Data);
1921 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1923 Data = (unsigned char)((divisor & 0xff00) >> 8);
1924 dbg("set_serial_baud Value to write DLM is %x\n", Data);
1925 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1927 /* Disable access to divisor latch */
1928 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1929 mos7840_port->shadowLCR = Data;
1930 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1937 /*****************************************************************************
1938 * mos7840_change_port_settings
1939 * This routine is called to set the UART on the device to match
1940 * the specified new settings.
1941 *****************************************************************************/
1943 static void mos7840_change_port_settings(struct moschip_port *mos7840_port,
1944 struct termios *old_termios)
1946 struct tty_struct *tty;
1955 struct usb_serial_port *port;
1956 struct usb_serial *serial;
1958 if (mos7840_port == NULL)
1961 port = (struct usb_serial_port *)mos7840_port->port;
1963 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
1964 dbg("%s", "Invalid port \n");
1968 if (mos7840_serial_paranoia_check(port->serial, __FUNCTION__)) {
1969 dbg("%s", "Invalid Serial \n");
1973 serial = port->serial;
1975 dbg("%s - port %d", __FUNCTION__, mos7840_port->port->number);
1977 if (!mos7840_port->open) {
1978 dbg("%s - port not opened", __FUNCTION__);
1982 tty = mos7840_port->port->tty;
1984 if ((!tty) || (!tty->termios)) {
1985 dbg("%s - no tty structures", __FUNCTION__);
1989 dbg("%s", "Entering .......... \n");
1993 lParity = LCR_PAR_NONE;
1995 cflag = tty->termios->c_cflag;
1996 iflag = tty->termios->c_iflag;
1998 /* Change the number of bits */
1999 if (cflag & CSIZE) {
2000 switch (cflag & CSIZE) {
2018 /* Change the Parity bit */
2019 if (cflag & PARENB) {
2020 if (cflag & PARODD) {
2021 lParity = LCR_PAR_ODD;
2022 dbg("%s - parity = odd", __FUNCTION__);
2024 lParity = LCR_PAR_EVEN;
2025 dbg("%s - parity = even", __FUNCTION__);
2029 dbg("%s - parity = none", __FUNCTION__);
2032 if (cflag & CMSPAR) {
2033 lParity = lParity | 0x20;
2036 /* Change the Stop bit */
2037 if (cflag & CSTOPB) {
2039 dbg("%s - stop bits = 2", __FUNCTION__);
2042 dbg("%s - stop bits = 1", __FUNCTION__);
2045 /* Update the LCR with the correct value */
2046 mos7840_port->shadowLCR &=
2047 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2048 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2050 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2051 mos7840_port->shadowLCR);
2052 /* Disable Interrupts */
2054 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2057 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2060 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2062 /* Send the updated LCR value to the mos7840 */
2063 Data = mos7840_port->shadowLCR;
2065 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2068 mos7840_port->shadowMCR = Data;
2069 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2071 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2073 /* set up the MCR register and send it to the mos7840 */
2075 mos7840_port->shadowMCR = MCR_MASTER_IE;
2076 if (cflag & CBAUD) {
2077 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2080 if (cflag & CRTSCTS) {
2081 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2084 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2087 Data = mos7840_port->shadowMCR;
2088 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2090 /* Determine divisor based on baud rate */
2091 baud = tty_get_baud_rate(tty);
2094 /* pick a default, any default... */
2095 dbg("%s\n", "Picked default baud...");
2099 dbg("%s - baud rate = %d", __FUNCTION__, baud);
2100 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2102 /* Enable Interrupts */
2104 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2106 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2107 mos7840_port->read_urb->dev = serial->dev;
2109 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2112 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2116 wake_up(&mos7840_port->delta_msr_wait);
2117 mos7840_port->delta_msr_cond = 1;
2118 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2119 mos7840_port->shadowLCR);
2124 /*****************************************************************************
2125 * mos7840_set_termios
2126 * this function is called by the tty driver when it wants to change
2127 * the termios structure
2128 *****************************************************************************/
2130 static void mos7840_set_termios(struct usb_serial_port *port,
2131 struct termios *old_termios)
2135 struct usb_serial *serial;
2136 struct moschip_port *mos7840_port;
2137 struct tty_struct *tty;
2138 dbg("mos7840_set_termios: START\n");
2139 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2140 dbg("%s", "Invalid port \n");
2144 serial = port->serial;
2146 if (mos7840_serial_paranoia_check(serial, __FUNCTION__)) {
2147 dbg("%s", "Invalid Serial \n");
2151 mos7840_port = mos7840_get_port_private(port);
2153 if (mos7840_port == NULL)
2158 if (!port->tty || !port->tty->termios) {
2159 dbg("%s - no tty or termios", __FUNCTION__);
2163 if (!mos7840_port->open) {
2164 dbg("%s - port not opened", __FUNCTION__);
2168 dbg("%s\n", "setting termios - ");
2170 cflag = tty->termios->c_cflag;
2173 dbg("%s %s\n", __FUNCTION__, "cflag is NULL");
2177 /* check that they really want us to change something */
2179 if ((cflag == old_termios->c_cflag) &&
2180 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
2181 RELEVANT_IFLAG(old_termios->c_iflag))) {
2182 dbg("%s\n", "Nothing to change");
2187 dbg("%s - clfag %08x iflag %08x", __FUNCTION__,
2188 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2191 dbg("%s - old clfag %08x old iflag %08x", __FUNCTION__,
2192 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2195 dbg("%s - port %d", __FUNCTION__, port->number);
2197 /* change the port settings to the new ones specified */
2199 mos7840_change_port_settings(mos7840_port, old_termios);
2201 if (!mos7840_port->read_urb) {
2202 dbg("%s", "URB KILLED !!!!!\n");
2206 if (mos7840_port->read_urb->status != -EINPROGRESS) {
2207 mos7840_port->read_urb->dev = serial->dev;
2208 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2210 dbg(" usb_submit_urb(read bulk) failed, status = %d",
2217 /*****************************************************************************
2218 * mos7840_get_lsr_info - get line status register info
2220 * Purpose: Let user call ioctl() to get info when the UART physically
2221 * is emptied. On bus types like RS485, the transmitter must
2222 * release the bus after transmitting. This must be done when
2223 * the transmit shift register is empty, not be done when the
2224 * transmit holding register is empty. This functionality
2225 * allows an RS485 driver to be written in user space.
2226 *****************************************************************************/
2228 static int mos7840_get_lsr_info(struct moschip_port *mos7840_port,
2229 unsigned int *value)
2232 unsigned int result = 0;
2234 count = mos7840_chars_in_buffer(mos7840_port->port);
2236 dbg("%s -- Empty", __FUNCTION__);
2237 result = TIOCSER_TEMT;
2240 if (copy_to_user(value, &result, sizeof(int)))
2245 /*****************************************************************************
2246 * mos7840_get_bytes_avail - get number of bytes available
2248 * Purpose: Let user call ioctl to get the count of number of bytes available.
2249 *****************************************************************************/
2251 static int mos7840_get_bytes_avail(struct moschip_port *mos7840_port,
2252 unsigned int *value)
2254 unsigned int result = 0;
2255 struct tty_struct *tty = mos7840_port->port->tty;
2258 return -ENOIOCTLCMD;
2260 result = tty->read_cnt;
2262 dbg("%s(%d) = %d", __FUNCTION__, mos7840_port->port->number, result);
2263 if (copy_to_user(value, &result, sizeof(int)))
2266 return -ENOIOCTLCMD;
2269 /*****************************************************************************
2270 * mos7840_set_modem_info
2271 * function to set modem info
2272 *****************************************************************************/
2274 static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2275 unsigned int cmd, unsigned int *value)
2281 struct usb_serial_port *port;
2283 if (mos7840_port == NULL)
2286 port = (struct usb_serial_port *)mos7840_port->port;
2287 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2288 dbg("%s", "Invalid port \n");
2292 mcr = mos7840_port->shadowMCR;
2294 if (copy_from_user(&arg, value, sizeof(int)))
2299 if (arg & TIOCM_RTS)
2301 if (arg & TIOCM_DTR)
2303 if (arg & TIOCM_LOOP)
2304 mcr |= MCR_LOOPBACK;
2308 if (arg & TIOCM_RTS)
2310 if (arg & TIOCM_DTR)
2312 if (arg & TIOCM_LOOP)
2313 mcr &= ~MCR_LOOPBACK;
2317 /* turn off the RTS and DTR and LOOPBACK
2318 * and then only turn on what was asked to */
2319 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2320 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2321 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2322 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2326 mos7840_port->shadowMCR = mcr;
2328 Data = mos7840_port->shadowMCR;
2330 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2332 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2339 /*****************************************************************************
2340 * mos7840_get_modem_info
2341 * function to get modem info
2342 *****************************************************************************/
2344 static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
2345 unsigned int *value)
2347 unsigned int result = 0;
2349 unsigned int mcr = mos7840_port->shadowMCR;
2352 mos7840_get_uart_reg(mos7840_port->port, MODEM_STATUS_REGISTER,
2354 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2355 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2356 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2357 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2358 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2359 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2361 dbg("%s -- %x", __FUNCTION__, result);
2363 if (copy_to_user(value, &result, sizeof(int)))
2368 /*****************************************************************************
2369 * mos7840_get_serial_info
2370 * function to get information about serial port
2371 *****************************************************************************/
2373 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2374 struct serial_struct *retinfo)
2376 struct serial_struct tmp;
2378 if (mos7840_port == NULL)
2384 memset(&tmp, 0, sizeof(tmp));
2386 tmp.type = PORT_16550A;
2387 tmp.line = mos7840_port->port->serial->minor;
2388 tmp.port = mos7840_port->port->number;
2390 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2391 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2392 tmp.baud_base = 9600;
2393 tmp.close_delay = 5 * HZ;
2394 tmp.closing_wait = 30 * HZ;
2396 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2401 /*****************************************************************************
2403 * this function handles any ioctl calls to the driver
2404 *****************************************************************************/
2406 static int mos7840_ioctl(struct usb_serial_port *port, struct file *file,
2407 unsigned int cmd, unsigned long arg)
2409 struct moschip_port *mos7840_port;
2410 struct tty_struct *tty;
2412 struct async_icount cnow;
2413 struct async_icount cprev;
2414 struct serial_icounter_struct icount;
2417 struct tty_ldisc *ld;
2419 if (mos7840_port_paranoia_check(port, __FUNCTION__)) {
2420 dbg("%s", "Invalid port \n");
2424 mos7840_port = mos7840_get_port_private(port);
2425 tty = mos7840_port->port->tty;
2427 if (mos7840_port == NULL)
2430 dbg("%s - port %d, cmd = 0x%x", __FUNCTION__, port->number, cmd);
2433 /* return number of bytes available */
2436 dbg("%s (%d) TIOCINQ", __FUNCTION__, port->number);
2437 return mos7840_get_bytes_avail(mos7840_port,
2438 (unsigned int *)arg);
2442 dbg("%s (%d) TIOCOUTQ", __FUNCTION__, port->number);
2443 return put_user(tty->driver->chars_in_buffer ?
2444 tty->driver->chars_in_buffer(tty) : 0,
2449 retval = tty_check_change(tty);
2453 ld = tty_ldisc_ref(tty);
2456 if (ld && ld->flush_buffer)
2457 ld->flush_buffer(tty);
2460 if (ld && ld->flush_buffer)
2461 ld->flush_buffer(tty);
2464 if (tty->driver->flush_buffer)
2465 tty->driver->flush_buffer(tty);
2468 tty_ldisc_deref(ld);
2471 tty_ldisc_deref(ld);
2475 if (kernel_termios_to_user_termios
2476 ((struct termios __user *)arg, tty->termios))
2481 dbg("%s (%d) TIOCSERGETLSR", __FUNCTION__, port->number);
2482 return mos7840_get_lsr_info(mos7840_port, (unsigned int *)arg);
2488 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __FUNCTION__,
2491 mos7840_set_modem_info(mos7840_port, cmd,
2492 (unsigned int *)arg);
2496 dbg("%s (%d) TIOCMGET", __FUNCTION__, port->number);
2497 return mos7840_get_modem_info(mos7840_port,
2498 (unsigned int *)arg);
2501 dbg("%s (%d) TIOCGSERIAL", __FUNCTION__, port->number);
2502 return mos7840_get_serial_info(mos7840_port,
2503 (struct serial_struct *)arg);
2506 dbg("%s (%d) TIOCSSERIAL", __FUNCTION__, port->number);
2510 dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
2511 cprev = mos7840_port->icount;
2513 //interruptible_sleep_on(&mos7840_port->delta_msr_wait);
2514 mos7840_port->delta_msr_cond = 0;
2515 wait_event_interruptible(mos7840_port->delta_msr_wait,
2517 delta_msr_cond == 1));
2519 /* see if a signal did it */
2520 if (signal_pending(current))
2521 return -ERESTARTSYS;
2522 cnow = mos7840_port->icount;
2523 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2524 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2525 return -EIO; /* no change => error */
2526 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2527 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2528 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2529 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2538 cnow = mos7840_port->icount;
2539 icount.cts = cnow.cts;
2540 icount.dsr = cnow.dsr;
2541 icount.rng = cnow.rng;
2542 icount.dcd = cnow.dcd;
2543 icount.rx = cnow.rx;
2544 icount.tx = cnow.tx;
2545 icount.frame = cnow.frame;
2546 icount.overrun = cnow.overrun;
2547 icount.parity = cnow.parity;
2548 icount.brk = cnow.brk;
2549 icount.buf_overrun = cnow.buf_overrun;
2551 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __FUNCTION__,
2552 port->number, icount.rx, icount.tx);
2553 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
2563 return -ENOIOCTLCMD;
2566 static int mos7840_calc_num_ports(struct usb_serial *serial)
2569 dbg("numberofendpoints: %d \n",
2570 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
2571 dbg("numberofendpoints: %d \n",
2572 (int)serial->interface->altsetting->desc.bNumEndpoints);
2573 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2574 mos7840_num_ports = 2;
2575 serial->type->num_ports = 2;
2576 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
2577 mos7840_num_ports = 4;
2578 serial->type->num_bulk_in = 4;
2579 serial->type->num_bulk_out = 4;
2580 serial->type->num_ports = 4;
2583 return mos7840_num_ports;
2586 /****************************************************************************
2588 ****************************************************************************/
2590 static int mos7840_startup(struct usb_serial *serial)
2592 struct moschip_port *mos7840_port;
2593 struct usb_device *dev;
2597 dbg("%s \n", " mos7840_startup :entering..........");
2600 dbg("%s\n", "Invalid Handler");
2606 dbg("%s\n", "Entering...");
2608 /* we set up the pointers to the endpoints in the mos7840_open *
2609 * function, as the structures aren't created yet. */
2611 /* set up port private structures */
2612 for (i = 0; i < serial->num_ports; ++i) {
2613 mos7840_port = kmalloc(sizeof(struct moschip_port), GFP_KERNEL);
2614 if (mos7840_port == NULL) {
2615 err("%s - Out of memory", __FUNCTION__);
2618 memset(mos7840_port, 0, sizeof(struct moschip_port));
2620 /* Initialize all port interrupt end point to port 0 int endpoint *
2621 * Our device has only one interrupt end point comman to all port */
2623 mos7840_port->port = serial->port[i];
2624 mos7840_set_port_private(serial->port[i], mos7840_port);
2626 mos7840_port->port_num = ((serial->port[i]->number -
2627 (serial->port[i]->serial->minor)) +
2630 if (mos7840_port->port_num == 1) {
2631 mos7840_port->SpRegOffset = 0x0;
2632 mos7840_port->ControlRegOffset = 0x1;
2633 mos7840_port->DcrRegOffset = 0x4;
2634 } else if ((mos7840_port->port_num == 2)
2635 && (mos7840_num_ports == 4)) {
2636 mos7840_port->SpRegOffset = 0x8;
2637 mos7840_port->ControlRegOffset = 0x9;
2638 mos7840_port->DcrRegOffset = 0x16;
2639 } else if ((mos7840_port->port_num == 2)
2640 && (mos7840_num_ports == 2)) {
2641 mos7840_port->SpRegOffset = 0xa;
2642 mos7840_port->ControlRegOffset = 0xb;
2643 mos7840_port->DcrRegOffset = 0x19;
2644 } else if ((mos7840_port->port_num == 3)
2645 && (mos7840_num_ports == 4)) {
2646 mos7840_port->SpRegOffset = 0xa;
2647 mos7840_port->ControlRegOffset = 0xb;
2648 mos7840_port->DcrRegOffset = 0x19;
2649 } else if ((mos7840_port->port_num == 4)
2650 && (mos7840_num_ports == 4)) {
2651 mos7840_port->SpRegOffset = 0xc;
2652 mos7840_port->ControlRegOffset = 0xd;
2653 mos7840_port->DcrRegOffset = 0x1c;
2655 mos7840_dump_serial_port(mos7840_port);
2657 mos7840_set_port_private(serial->port[i], mos7840_port);
2659 //enable rx_disable bit in control register
2662 mos7840_get_reg_sync(serial->port[i],
2663 mos7840_port->ControlRegOffset, &Data);
2665 dbg("Reading ControlReg failed status-0x%x\n", status);
2668 dbg("ControlReg Reading success val is %x, status%d\n",
2670 Data |= 0x08; //setting driver done bit
2671 Data |= 0x04; //sp1_bit to have cts change reflect in modem status reg
2673 //Data |= 0x20; //rx_disable bit
2676 mos7840_set_reg_sync(serial->port[i],
2677 mos7840_port->ControlRegOffset, Data);
2679 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2682 dbg("ControlReg Writing success(rx_disable) status%d\n",
2685 //Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2 and 0x24 in DCR3
2689 mos7840_set_reg_sync(serial->port[i],
2690 (__u16) (mos7840_port->DcrRegOffset +
2693 dbg("Writing DCR0 failed status-0x%x\n", status);
2696 dbg("DCR0 Writing success status%d\n", status);
2701 mos7840_set_reg_sync(serial->port[i],
2702 (__u16) (mos7840_port->DcrRegOffset +
2705 dbg("Writing DCR1 failed status-0x%x\n", status);
2708 dbg("DCR1 Writing success status%d\n", status);
2713 mos7840_set_reg_sync(serial->port[i],
2714 (__u16) (mos7840_port->DcrRegOffset +
2717 dbg("Writing DCR2 failed status-0x%x\n", status);
2720 dbg("DCR2 Writing success status%d\n", status);
2722 // write values in clkstart0x0 and clkmulti 0x20
2726 mos7840_set_reg_sync(serial->port[i],
2727 CLK_START_VALUE_REGISTER, Data);
2729 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2732 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2737 mos7840_set_reg_sync(serial->port[i], CLK_MULTI_REGISTER,
2740 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2744 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2747 //write value 0x0 to scratchpad register
2751 mos7840_set_uart_reg(serial->port[i], SCRATCH_PAD_REGISTER,
2754 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2758 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2761 //Zero Length flag register
2762 if ((mos7840_port->port_num != 1)
2763 && (mos7840_num_ports == 2)) {
2767 status = mos7840_set_reg_sync(serial->port[i],
2773 dbg("ZLIP offset%x\n",
2775 ((__u16) mos7840_port->port_num)));
2777 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2781 dbg("ZLP_REG%d Writing success status%d\n",
2786 status = mos7840_set_reg_sync(serial->port[i],
2792 dbg("ZLIP offset%x\n",
2794 ((__u16) mos7840_port->port_num) - 0x1));
2796 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2800 dbg("ZLP_REG%d Writing success status%d\n",
2804 mos7840_port->control_urb = usb_alloc_urb(0, SLAB_ATOMIC);
2805 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2809 //Zero Length flag enable
2812 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2814 dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
2817 dbg("ZLP_REG5 Writing success status%d\n", status);
2819 /* setting configuration feature to one */
2820 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2821 (__u8) 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 5 * HZ);
2825 /****************************************************************************
2827 * This function is called whenever the device is removed from the usb bus.
2828 ****************************************************************************/
2830 static void mos7840_shutdown(struct usb_serial *serial)
2833 struct moschip_port *mos7840_port;
2834 dbg("%s \n", " shutdown :entering..........");
2837 dbg("%s", "Invalid Handler \n");
2841 /* check for the ports to be closed,close the ports and disconnect */
2843 /* free private structure allocated for serial port *
2844 * stop reads and writes on all ports */
2846 for (i = 0; i < serial->num_ports; ++i) {
2847 mos7840_port = mos7840_get_port_private(serial->port[i]);
2848 kfree(mos7840_port->ctrl_buf);
2849 usb_kill_urb(mos7840_port->control_urb);
2850 kfree(mos7840_port);
2851 mos7840_set_port_private(serial->port[i], NULL);
2854 dbg("%s\n", "Thank u :: ");
2858 static struct usb_serial_driver moschip7840_4port_device = {
2860 .owner = THIS_MODULE,
2863 .description = DRIVER_DESC,
2864 .id_table = moschip_port_id_table,
2865 .num_interrupt_in = 1, //NUM_DONT_CARE,//1,
2871 .open = mos7840_open,
2872 .close = mos7840_close,
2873 .write = mos7840_write,
2874 .write_room = mos7840_write_room,
2875 .chars_in_buffer = mos7840_chars_in_buffer,
2876 .throttle = mos7840_throttle,
2877 .unthrottle = mos7840_unthrottle,
2878 .calc_num_ports = mos7840_calc_num_ports,
2879 #ifdef MCSSerialProbe
2880 .probe = mos7840_serial_probe,
2882 .ioctl = mos7840_ioctl,
2883 .set_termios = mos7840_set_termios,
2884 .break_ctl = mos7840_break,
2885 .tiocmget = mos7840_tiocmget,
2886 .tiocmset = mos7840_tiocmset,
2887 .attach = mos7840_startup,
2888 .shutdown = mos7840_shutdown,
2889 .read_bulk_callback = mos7840_bulk_in_callback,
2890 .read_int_callback = mos7840_interrupt_callback,
2893 static struct usb_driver io_driver = {
2895 .probe = usb_serial_probe,
2896 .disconnect = usb_serial_disconnect,
2897 .id_table = moschip_id_table_combined,
2900 /****************************************************************************
2902 * This is called by the module subsystem, or on startup to initialize us
2903 ****************************************************************************/
2904 static int __init moschip7840_init(void)
2908 dbg("%s \n", " mos7840_init :entering..........");
2910 /* Register with the usb serial */
2911 retval = usb_serial_register(&moschip7840_4port_device);
2914 goto failed_port_device_register;
2916 dbg("%s\n", "Entring...");
2917 info(DRIVER_DESC " " DRIVER_VERSION);
2919 /* Register with the usb */
2920 retval = usb_register(&io_driver);
2923 goto failed_usb_register;
2926 dbg("%s\n", "Leaving...");
2930 failed_usb_register:
2931 usb_serial_deregister(&moschip7840_4port_device);
2933 failed_port_device_register:
2938 /****************************************************************************
2940 * Called when the driver is about to be unloaded.
2941 ****************************************************************************/
2942 static void __exit moschip7840_exit(void)
2945 dbg("%s \n", " mos7840_exit :entering..........");
2947 usb_deregister(&io_driver);
2949 usb_serial_deregister(&moschip7840_4port_device);
2951 dbg("%s\n", "Entring...");
2954 module_init(moschip7840_init);
2955 module_exit(moschip7840_exit);
2957 /* Module information */
2958 MODULE_DESCRIPTION(DRIVER_DESC);
2959 MODULE_LICENSE("GPL");
2961 module_param(debug, bool, S_IRUGO | S_IWUSR);
2962 MODULE_PARM_DESC(debug, "Debug enabled or not");