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 <linux/uaccess.h>
41 #define DRIVER_VERSION "1.3.1"
42 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
45 * 16C50 UART register defines
48 #define LCR_BITS_5 0x00 /* 5 bits/char */
49 #define LCR_BITS_6 0x01 /* 6 bits/char */
50 #define LCR_BITS_7 0x02 /* 7 bits/char */
51 #define LCR_BITS_8 0x03 /* 8 bits/char */
52 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
54 #define LCR_STOP_1 0x00 /* 1 stop bit */
55 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
56 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
57 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
59 #define LCR_PAR_NONE 0x00 /* No parity */
60 #define LCR_PAR_ODD 0x08 /* Odd parity */
61 #define LCR_PAR_EVEN 0x18 /* Even parity */
62 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
63 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
64 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
66 #define LCR_SET_BREAK 0x40 /* Set Break condition */
67 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
69 #define MCR_DTR 0x01 /* Assert DTR */
70 #define MCR_RTS 0x02 /* Assert RTS */
71 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
72 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
73 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
74 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
76 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
77 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
78 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
79 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
82 * Defines used for sending commands to port
85 #define WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
86 #define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
88 #define MOS_PORT1 0x0200
89 #define MOS_PORT2 0x0300
90 #define MOS_VENREG 0x0000
91 #define MOS_MAX_PORT 0x02
92 #define MOS_WRITE 0x0E
96 #define MCS_RD_RTYPE 0xC0
97 #define MCS_WR_RTYPE 0x40
98 #define MCS_RDREQ 0x0D
99 #define MCS_WRREQ 0x0E
100 #define MCS_CTRL_TIMEOUT 500
101 #define VENDOR_READ_LENGTH (0x01)
103 #define MAX_NAME_LEN 64
105 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
106 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
108 /* For higher baud Rates use TIOCEXBAUD */
109 #define TIOCEXBAUD 0x5462
111 /* vendor id and device id defines */
113 /* The native mos7840/7820 component */
114 #define USB_VENDOR_ID_MOSCHIP 0x9710
115 #define MOSCHIP_DEVICE_ID_7840 0x7840
116 #define MOSCHIP_DEVICE_ID_7820 0x7820
117 /* The native component can have its vendor/device id's overridden
118 * in vendor-specific implementations. Such devices can be handled
119 * by making a change here, in moschip_port_id_table, and in
120 * moschip_id_table_combined
122 #define USB_VENDOR_ID_BANDB 0x0856
123 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
124 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
126 /* Interrupt Routine Defines */
128 #define SERIAL_IIR_RLS 0x06
129 #define SERIAL_IIR_MS 0x00
132 * Emulation of the bit mask on the LINE STATUS REGISTER.
134 #define SERIAL_LSR_DR 0x0001
135 #define SERIAL_LSR_OE 0x0002
136 #define SERIAL_LSR_PE 0x0004
137 #define SERIAL_LSR_FE 0x0008
138 #define SERIAL_LSR_BI 0x0010
140 #define MOS_MSR_DELTA_CTS 0x10
141 #define MOS_MSR_DELTA_DSR 0x20
142 #define MOS_MSR_DELTA_RI 0x40
143 #define MOS_MSR_DELTA_CD 0x80
145 /* Serial Port register Address */
146 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
147 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
148 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
149 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
150 #define LINE_STATUS_REGISTER ((__u16)(0x05))
151 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
152 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
153 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
154 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
156 #define CLK_MULTI_REGISTER ((__u16)(0x02))
157 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
159 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
162 * URB POOL related defines
164 #define NUM_URBS 16 /* URB Count */
165 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
168 static struct usb_device_id moschip_port_id_table[] = {
169 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
170 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
171 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
172 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
173 {} /* terminating entry */
176 static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
177 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
178 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
179 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
180 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
181 {} /* terminating entry */
184 MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
186 /* This structure holds all of the local port information */
188 struct moschip_port {
189 int port_num; /*Actual port number in the device(1,2,etc) */
190 struct urb *write_urb; /* write URB for this port */
191 struct urb *read_urb; /* read URB for this port */
193 __u8 shadowLCR; /* last LCR value received */
194 __u8 shadowMCR; /* last MCR value received */
198 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
199 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
201 struct async_icount icount;
202 struct usb_serial_port *port; /* loop back to the owner of this object */
206 __u8 ControlRegOffset;
208 /* for processing control URBS in interrupt context */
209 struct urb *control_urb;
210 struct usb_ctrlrequest *dr;
214 spinlock_t pool_lock;
215 struct urb *write_urb_pool[NUM_URBS];
224 * mos7840_set_reg_sync
225 * To set the Control register by calling usb_fill_control_urb function
226 * by passing usb_sndctrlpipe function as parameter.
229 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
232 struct usb_device *dev = port->serial->dev;
234 dbg("mos7840_set_reg_sync offset is %x, value %x\n", reg, val);
236 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
237 MCS_WR_RTYPE, val, reg, NULL, 0,
242 * mos7840_get_reg_sync
243 * To set the Uart register by calling usb_fill_control_urb function by
244 * passing usb_rcvctrlpipe function as parameter.
247 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
250 struct usb_device *dev = port->serial->dev;
253 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
254 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
256 dbg("mos7840_get_reg_sync offset is %x, return val %x\n", reg, *val);
257 *val = (*val) & 0x00ff;
262 * mos7840_set_uart_reg
263 * To set the Uart register by calling usb_fill_control_urb function by
264 * passing usb_sndctrlpipe function as parameter.
267 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
271 struct usb_device *dev = port->serial->dev;
273 /* For the UART control registers, the application number need
275 if (port->serial->num_ports == 4) {
276 val |= (((__u16) port->number -
277 (__u16) (port->serial->minor)) + 1) << 8;
278 dbg("mos7840_set_uart_reg application number is %x\n", val);
280 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
281 val |= (((__u16) port->number -
282 (__u16) (port->serial->minor)) + 1) << 8;
283 dbg("mos7840_set_uart_reg application number is %x\n",
287 (((__u16) port->number -
288 (__u16) (port->serial->minor)) + 2) << 8;
289 dbg("mos7840_set_uart_reg application number is %x\n",
293 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
294 MCS_WR_RTYPE, val, reg, NULL, 0,
300 * mos7840_get_uart_reg
301 * To set the Control register by calling usb_fill_control_urb function
302 * by passing usb_rcvctrlpipe function as parameter.
304 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
307 struct usb_device *dev = port->serial->dev;
311 /* dbg("application number is %4x \n",
312 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
313 /* Wval is same as application number */
314 if (port->serial->num_ports == 4) {
316 (((__u16) port->number - (__u16) (port->serial->minor)) +
318 dbg("mos7840_get_uart_reg application number is %x\n", Wval);
320 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
321 Wval = (((__u16) port->number -
322 (__u16) (port->serial->minor)) + 1) << 8;
323 dbg("mos7840_get_uart_reg application number is %x\n",
326 Wval = (((__u16) port->number -
327 (__u16) (port->serial->minor)) + 2) << 8;
328 dbg("mos7840_get_uart_reg application number is %x\n",
332 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
333 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
335 *val = (*val) & 0x00ff;
339 static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
342 dbg("***************************************\n");
343 dbg("SpRegOffset is %2x\n", mos7840_port->SpRegOffset);
344 dbg("ControlRegOffset is %2x \n", mos7840_port->ControlRegOffset);
345 dbg("DCRRegOffset is %2x \n", mos7840_port->DcrRegOffset);
346 dbg("***************************************\n");
350 /************************************************************************/
351 /************************************************************************/
352 /* I N T E R F A C E F U N C T I O N S */
353 /* I N T E R F A C E F U N C T I O N S */
354 /************************************************************************/
355 /************************************************************************/
357 static inline void mos7840_set_port_private(struct usb_serial_port *port,
358 struct moschip_port *data)
360 usb_set_serial_port_data(port, (void *)data);
363 static inline struct moschip_port *mos7840_get_port_private(struct
367 return (struct moschip_port *)usb_get_serial_port_data(port);
370 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
372 struct moschip_port *mos7840_port;
373 struct async_icount *icount;
375 icount = &mos7840_port->icount;
377 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
379 icount = &mos7840_port->icount;
381 /* update input line counters */
382 if (new_msr & MOS_MSR_DELTA_CTS) {
386 if (new_msr & MOS_MSR_DELTA_DSR) {
390 if (new_msr & MOS_MSR_DELTA_CD) {
394 if (new_msr & MOS_MSR_DELTA_RI) {
401 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
403 struct async_icount *icount;
405 dbg("%s - %02x", __func__, new_lsr);
407 if (new_lsr & SERIAL_LSR_BI) {
409 * Parity and Framing errors only count if they
410 * occur exclusive of a break being
413 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
416 /* update input line counters */
417 icount = &port->icount;
418 if (new_lsr & SERIAL_LSR_BI) {
422 if (new_lsr & SERIAL_LSR_OE) {
426 if (new_lsr & SERIAL_LSR_PE) {
430 if (new_lsr & SERIAL_LSR_FE) {
436 /************************************************************************/
437 /************************************************************************/
438 /* U S B C A L L B A C K F U N C T I O N S */
439 /* U S B C A L L B A C K F U N C T I O N S */
440 /************************************************************************/
441 /************************************************************************/
443 static void mos7840_control_callback(struct urb *urb)
446 struct moschip_port *mos7840_port;
449 int status = urb->status;
451 mos7840_port = urb->context;
460 /* this urb is terminated, clean up */
461 dbg("%s - urb shutting down with status: %d", __func__,
465 dbg("%s - nonzero urb status received: %d", __func__,
470 dbg("%s urb buffer size is %d\n", __func__, urb->actual_length);
471 dbg("%s mos7840_port->MsrLsr is %d port %d\n", __func__,
472 mos7840_port->MsrLsr, mos7840_port->port_num);
473 data = urb->transfer_buffer;
474 regval = (__u8) data[0];
475 dbg("%s data is %x\n", __func__, regval);
476 if (mos7840_port->MsrLsr == 0)
477 mos7840_handle_new_msr(mos7840_port, regval);
478 else if (mos7840_port->MsrLsr == 1)
479 mos7840_handle_new_lsr(mos7840_port, regval);
482 spin_lock(&mos7840_port->pool_lock);
483 if (!mos7840_port->zombie)
484 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
485 spin_unlock(&mos7840_port->pool_lock);
487 dev_err(&urb->dev->dev,
488 "%s - Error %d submitting interrupt urb\n",
493 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
496 struct usb_device *dev = mcs->port->serial->dev;
497 struct usb_ctrlrequest *dr = mcs->dr;
498 unsigned char *buffer = mcs->ctrl_buf;
501 dr->bRequestType = MCS_RD_RTYPE;
502 dr->bRequest = MCS_RDREQ;
503 dr->wValue = cpu_to_le16(Wval); /* 0 */
504 dr->wIndex = cpu_to_le16(reg);
505 dr->wLength = cpu_to_le16(2);
507 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
508 (unsigned char *)dr, buffer, 2,
509 mos7840_control_callback, mcs);
510 mcs->control_urb->transfer_buffer_length = 2;
511 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
515 /*****************************************************************************
516 * mos7840_interrupt_callback
517 * this is the callback function for when we have received data on the
518 * interrupt endpoint.
519 *****************************************************************************/
521 static void mos7840_interrupt_callback(struct urb *urb)
525 struct moschip_port *mos7840_port;
526 struct usb_serial *serial;
531 __u16 wval, wreg = 0;
532 int status = urb->status;
534 dbg("%s", " : Entering\n");
543 /* this urb is terminated, clean up */
544 dbg("%s - urb shutting down with status: %d", __func__,
548 dbg("%s - nonzero urb status received: %d", __func__,
553 length = urb->actual_length;
554 data = urb->transfer_buffer;
556 serial = urb->context;
558 /* Moschip get 5 bytes
559 * Byte 1 IIR Port 1 (port.number is 0)
560 * Byte 2 IIR Port 2 (port.number is 1)
561 * Byte 3 IIR Port 3 (port.number is 2)
562 * Byte 4 IIR Port 4 (port.number is 3)
563 * Byte 5 FIFO status for both */
565 if (length && length > 5) {
566 dbg("%s \n", "Wrong data !!!");
570 sp[0] = (__u8) data[0];
571 sp[1] = (__u8) data[1];
572 sp[2] = (__u8) data[2];
573 sp[3] = (__u8) data[3];
576 for (i = 0; i < serial->num_ports; i++) {
577 mos7840_port = mos7840_get_port_private(serial->port[i]);
579 (((__u16) serial->port[i]->number -
580 (__u16) (serial->minor)) + 1) << 8;
581 if (mos7840_port->open) {
583 dbg("SP%d No Interrupt !!!\n", i);
585 switch (sp[i] & 0x0f) {
587 dbg("Serial Port %d: Receiver status error or ", i);
588 dbg("address bit detected in 9-bit mode\n");
589 mos7840_port->MsrLsr = 1;
590 wreg = LINE_STATUS_REGISTER;
593 dbg("Serial Port %d: Modem status change\n", i);
594 mos7840_port->MsrLsr = 0;
595 wreg = MODEM_STATUS_REGISTER;
598 spin_lock(&mos7840_port->pool_lock);
599 if (!mos7840_port->zombie) {
600 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
602 spin_unlock(&mos7840_port->pool_lock);
605 spin_unlock(&mos7840_port->pool_lock);
610 /* the completion handler for the control urb will resubmit */
613 result = usb_submit_urb(urb, GFP_ATOMIC);
615 dev_err(&urb->dev->dev,
616 "%s - Error %d submitting interrupt urb\n",
621 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
622 const char *function)
625 dbg("%s - port == NULL", function);
629 dbg("%s - port->serial == NULL", function);
636 /* Inline functions to check the sanity of a pointer that is passed to us */
637 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
638 const char *function)
641 dbg("%s - serial == NULL", function);
645 dbg("%s - serial->type == NULL!", function);
652 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
653 const char *function)
655 /* if no port was specified, or it fails a paranoia check */
657 mos7840_port_paranoia_check(port, function) ||
658 mos7840_serial_paranoia_check(port->serial, function)) {
659 /* then say that we don't have a valid usb_serial thing,
660 * which will end up genrating -ENODEV return values */
667 /*****************************************************************************
668 * mos7840_bulk_in_callback
669 * this is the callback function for when we have received data on the
671 *****************************************************************************/
673 static void mos7840_bulk_in_callback(struct urb *urb)
677 struct usb_serial *serial;
678 struct usb_serial_port *port;
679 struct moschip_port *mos7840_port;
680 struct tty_struct *tty;
681 int status = urb->status;
683 mos7840_port = urb->context;
685 dbg("%s", "NULL mos7840_port pointer \n");
686 mos7840_port->read_urb_busy = false;
691 dbg("nonzero read bulk status received: %d", status);
692 mos7840_port->read_urb_busy = false;
696 port = (struct usb_serial_port *)mos7840_port->port;
697 if (mos7840_port_paranoia_check(port, __func__)) {
698 dbg("%s", "Port Paranoia failed \n");
699 mos7840_port->read_urb_busy = false;
703 serial = mos7840_get_usb_serial(port, __func__);
705 dbg("%s\n", "Bad serial pointer ");
706 mos7840_port->read_urb_busy = false;
710 dbg("%s\n", "Entering... \n");
712 data = urb->transfer_buffer;
714 dbg("%s", "Entering ........... \n");
716 if (urb->actual_length) {
717 tty = tty_port_tty_get(&mos7840_port->port->port);
719 tty_buffer_request_room(tty, urb->actual_length);
720 tty_insert_flip_string(tty, data, urb->actual_length);
722 tty_flip_buffer_push(tty);
725 mos7840_port->icount.rx += urb->actual_length;
727 dbg("mos7840_port->icount.rx is %d:\n",
728 mos7840_port->icount.rx);
731 if (!mos7840_port->read_urb) {
732 dbg("%s", "URB KILLED !!!\n");
733 mos7840_port->read_urb_busy = false;
738 mos7840_port->read_urb->dev = serial->dev;
740 mos7840_port->read_urb_busy = true;
741 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
744 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
745 mos7840_port->read_urb_busy = false;
749 /*****************************************************************************
750 * mos7840_bulk_out_data_callback
751 * this is the callback function for when we have finished sending
752 * serial data on the bulk out endpoint.
753 *****************************************************************************/
755 static void mos7840_bulk_out_data_callback(struct urb *urb)
757 struct moschip_port *mos7840_port;
758 struct tty_struct *tty;
759 int status = urb->status;
762 mos7840_port = urb->context;
763 spin_lock(&mos7840_port->pool_lock);
764 for (i = 0; i < NUM_URBS; i++) {
765 if (urb == mos7840_port->write_urb_pool[i]) {
766 mos7840_port->busy[i] = 0;
770 spin_unlock(&mos7840_port->pool_lock);
773 dbg("nonzero write bulk status received:%d\n", status);
777 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
778 dbg("%s", "Port Paranoia failed \n");
782 dbg("%s \n", "Entering .........");
784 tty = tty_port_tty_get(&mos7840_port->port->port);
785 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 tty_struct *tty,
815 struct usb_serial_port *port, struct file *filp)
819 struct usb_serial *serial;
823 struct moschip_port *mos7840_port;
824 struct moschip_port *port0;
826 if (mos7840_port_paranoia_check(port, __func__)) {
827 dbg("%s", "Port Paranoia failed \n");
831 serial = port->serial;
833 if (mos7840_serial_paranoia_check(serial, __func__)) {
834 dbg("%s", "Serial Paranoia failed \n");
838 mos7840_port = mos7840_get_port_private(port);
839 port0 = mos7840_get_port_private(serial->port[0]);
841 if (mos7840_port == NULL || port0 == NULL)
844 usb_clear_halt(serial->dev, port->write_urb->pipe);
845 usb_clear_halt(serial->dev, port->read_urb->pipe);
848 /* Initialising the write urb pool */
849 for (j = 0; j < NUM_URBS; ++j) {
850 urb = usb_alloc_urb(0, GFP_KERNEL);
851 mos7840_port->write_urb_pool[j] = urb;
854 dev_err(&port->dev, "No more urbs???\n");
858 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
860 if (!urb->transfer_buffer) {
862 mos7840_port->write_urb_pool[j] = NULL;
864 "%s-out of memory for urb buffers.\n",
870 /*****************************************************************************
871 * Initialize MCS7840 -- Write Init values to corresponding Registers
879 * 0x08 : SP1/2 Control Reg
880 *****************************************************************************/
882 /* NEED to check the following Block */
885 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
887 dbg("Reading Spreg failed\n");
891 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
893 dbg("writing Spreg failed\n");
898 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
900 dbg("writing Spreg failed\n");
903 /* End of block to be checked */
906 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
909 dbg("Reading Controlreg failed\n");
912 Data |= 0x08; /* Driver done bit */
913 Data |= 0x20; /* rx_disable */
914 status = mos7840_set_reg_sync(port,
915 mos7840_port->ControlRegOffset, Data);
917 dbg("writing Controlreg failed\n");
920 /* do register settings here */
921 /* Set all regs to the device default values. */
922 /***********************************
923 * First Disable all interrupts.
924 ***********************************/
926 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
928 dbg("disableing interrupts failed\n");
931 /* Set FIFO_CONTROL_REGISTER to the default value */
933 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
935 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
940 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
942 dbg("Writing FIFO_CONTROL_REGISTER failed\n");
947 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
948 mos7840_port->shadowLCR = Data;
951 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
952 mos7840_port->shadowMCR = Data;
955 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
956 mos7840_port->shadowLCR = Data;
958 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
959 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
962 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
965 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
968 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
970 Data = Data & ~SERIAL_LCR_DLAB;
971 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
972 mos7840_port->shadowLCR = Data;
974 /* clearing Bulkin and Bulkout Fifo */
976 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
979 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
982 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
983 /* Finally enable all interrupts */
985 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
987 /* clearing rx_disable */
989 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
992 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
997 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1000 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1003 /* force low_latency on so that our tty_push actually forces *
1004 * the data through,otherwise it is scheduled, and with *
1005 * high data rates (like with OHCI) data can get lost. */
1007 tty->low_latency = 1;
1009 /* Check to see if we've set up our endpoint info yet *
1010 * (can't set it up in mos7840_startup as the structures *
1011 * were not set up at that time.) */
1012 if (port0->open_ports == 1) {
1013 if (serial->port[0]->interrupt_in_buffer == NULL) {
1014 /* set up interrupt urb */
1015 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1017 usb_rcvintpipe(serial->dev,
1018 serial->port[0]->interrupt_in_endpointAddress),
1019 serial->port[0]->interrupt_in_buffer,
1020 serial->port[0]->interrupt_in_urb->
1021 transfer_buffer_length,
1022 mos7840_interrupt_callback,
1024 serial->port[0]->interrupt_in_urb->interval);
1026 /* start interrupt read for mos7840 *
1027 * will continue as long as mos7840 is connected */
1030 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1033 dev_err(&port->dev, "%s - Error %d submitting "
1034 "interrupt urb\n", __func__, response);
1041 /* see if we've set up our endpoint info yet *
1042 * (can't set it up in mos7840_startup as the *
1043 * structures were not set up at that time.) */
1045 dbg("port number is %d \n", port->number);
1046 dbg("serial number is %d \n", port->serial->minor);
1047 dbg("Bulkin endpoint is %d \n", port->bulk_in_endpointAddress);
1048 dbg("BulkOut endpoint is %d \n", port->bulk_out_endpointAddress);
1049 dbg("Interrupt endpoint is %d \n", port->interrupt_in_endpointAddress);
1050 dbg("port's number in the device is %d\n", mos7840_port->port_num);
1051 mos7840_port->read_urb = port->read_urb;
1053 /* set up our bulk in urb */
1055 usb_fill_bulk_urb(mos7840_port->read_urb,
1057 usb_rcvbulkpipe(serial->dev,
1058 port->bulk_in_endpointAddress),
1059 port->bulk_in_buffer,
1060 mos7840_port->read_urb->transfer_buffer_length,
1061 mos7840_bulk_in_callback, mos7840_port);
1063 dbg("mos7840_open: bulkin endpoint is %d\n",
1064 port->bulk_in_endpointAddress);
1065 mos7840_port->read_urb_busy = true;
1066 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1068 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1069 __func__, response);
1070 mos7840_port->read_urb_busy = false;
1073 /* initialize our wait queues */
1074 init_waitqueue_head(&mos7840_port->wait_chase);
1075 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1077 /* initialize our icount structure */
1078 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1080 /* initialize our port settings */
1081 /* Must set to enable ints! */
1082 mos7840_port->shadowMCR = MCR_MASTER_IE;
1083 /* send a open port command */
1084 mos7840_port->open = 1;
1085 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1086 mos7840_port->icount.tx = 0;
1087 mos7840_port->icount.rx = 0;
1089 dbg("\n\nusb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p\n\n",
1090 serial, mos7840_port, port);
1096 /*****************************************************************************
1097 * mos7840_chars_in_buffer
1098 * this function is called by the tty driver when it wants to know how many
1099 * bytes of data we currently have outstanding in the port (data that has
1100 * been written, but hasn't made it out the port yet)
1101 * If successful, we return the number of bytes left to be written in the
1103 * Otherwise we return zero.
1104 *****************************************************************************/
1106 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1108 struct usb_serial_port *port = tty->driver_data;
1111 unsigned long flags;
1112 struct moschip_port *mos7840_port;
1114 dbg("%s \n", " mos7840_chars_in_buffer:entering ...........");
1116 if (mos7840_port_paranoia_check(port, __func__)) {
1117 dbg("%s", "Invalid port \n");
1121 mos7840_port = mos7840_get_port_private(port);
1122 if (mos7840_port == NULL) {
1123 dbg("%s \n", "mos7840_break:leaving ...........");
1127 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1128 for (i = 0; i < NUM_URBS; ++i)
1129 if (mos7840_port->busy[i])
1130 chars += URB_TRANSFER_BUFFER_SIZE;
1131 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1132 dbg("%s - returns %d", __func__, chars);
1137 /************************************************************************
1139 * mos7840_block_until_tx_empty
1141 * This function will block the close until one of the following:
1143 * 2. The mos7840 has stopped
1144 * 3. A timeout of 3 seconds without activity has expired
1146 ************************************************************************/
1147 static void mos7840_block_until_tx_empty(struct tty_struct *tty,
1148 struct moschip_port *mos7840_port)
1150 int timeout = HZ / 10;
1156 count = mos7840_chars_in_buffer(tty);
1158 /* Check for Buffer status */
1162 /* Block the thread for a while */
1163 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1166 /* No activity.. count down section */
1169 dbg("%s - TIMEOUT", __func__);
1172 /* Reset timeout value back to seconds */
1178 /*****************************************************************************
1180 * this function is called by the tty driver when a port is closed
1181 *****************************************************************************/
1183 static void mos7840_close(struct tty_struct *tty,
1184 struct usb_serial_port *port, struct file *filp)
1186 struct usb_serial *serial;
1187 struct moschip_port *mos7840_port;
1188 struct moschip_port *port0;
1192 dbg("%s\n", "mos7840_close:entering...");
1194 if (mos7840_port_paranoia_check(port, __func__)) {
1195 dbg("%s", "Port Paranoia failed \n");
1199 serial = mos7840_get_usb_serial(port, __func__);
1201 dbg("%s", "Serial Paranoia failed \n");
1205 mos7840_port = mos7840_get_port_private(port);
1206 port0 = mos7840_get_port_private(serial->port[0]);
1208 if (mos7840_port == NULL || port0 == NULL)
1211 for (j = 0; j < NUM_URBS; ++j)
1212 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1214 /* Freeing Write URBs */
1215 for (j = 0; j < NUM_URBS; ++j) {
1216 if (mos7840_port->write_urb_pool[j]) {
1217 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1218 kfree(mos7840_port->write_urb_pool[j]->
1221 usb_free_urb(mos7840_port->write_urb_pool[j]);
1226 /* flush and block until tx is empty */
1227 mos7840_block_until_tx_empty(tty, mos7840_port);
1229 /* While closing port, shutdown all bulk read, write *
1230 * and interrupt read if they exists */
1232 if (mos7840_port->write_urb) {
1233 dbg("%s", "Shutdown bulk write\n");
1234 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);
1239 mos7840_port->read_urb_busy = false;
1241 if ((&mos7840_port->control_urb)) {
1242 dbg("%s", "Shutdown control read\n");
1243 /*/ usb_kill_urb (mos7840_port->control_urb); */
1246 /* if(mos7840_port->ctrl_buf != NULL) */
1247 /* kfree(mos7840_port->ctrl_buf); */
1248 port0->open_ports--;
1249 dbg("mos7840_num_open_ports in close%d:in port%d\n",
1250 port0->open_ports, port->number);
1251 if (port0->open_ports == 0) {
1252 if (serial->port[0]->interrupt_in_urb) {
1253 dbg("%s", "Shutdown interrupt_in_urb\n");
1254 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1258 if (mos7840_port->write_urb) {
1259 /* 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);
1262 usb_free_urb(mos7840_port->write_urb);
1266 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1269 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1271 mos7840_port->open = 0;
1273 dbg("%s \n", "Leaving ............");
1276 /************************************************************************
1278 * mos7840_block_until_chase_response
1280 * This function will block the close until one of the following:
1281 * 1. Response to our Chase comes from mos7840
1282 * 2. A timeout of 10 seconds without activity has expired
1283 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1285 ************************************************************************/
1287 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1288 struct moschip_port *mos7840_port)
1290 int timeout = 1 * HZ;
1295 count = mos7840_chars_in_buffer(tty);
1297 /* Check for Buffer status */
1301 /* Block the thread for a while */
1302 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1304 /* No activity.. count down section */
1307 dbg("%s - TIMEOUT", __func__);
1310 /* Reset timeout value back to seconds */
1317 /*****************************************************************************
1319 * this function sends a break to the port
1320 *****************************************************************************/
1321 static void mos7840_break(struct tty_struct *tty, int break_state)
1323 struct usb_serial_port *port = tty->driver_data;
1325 struct usb_serial *serial;
1326 struct moschip_port *mos7840_port;
1328 dbg("%s \n", "Entering ...........");
1329 dbg("mos7840_break: Start\n");
1331 if (mos7840_port_paranoia_check(port, __func__)) {
1332 dbg("%s", "Port Paranoia failed \n");
1336 serial = mos7840_get_usb_serial(port, __func__);
1338 dbg("%s", "Serial Paranoia failed \n");
1342 mos7840_port = mos7840_get_port_private(port);
1344 if (mos7840_port == NULL)
1348 /* flush and block until tx is empty */
1349 mos7840_block_until_chase_response(tty, mos7840_port);
1351 if (break_state == -1)
1352 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1354 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1356 /* FIXME: no locking on shadowLCR anywhere in driver */
1357 mos7840_port->shadowLCR = data;
1358 dbg("mcs7840_break mos7840_port->shadowLCR is %x\n",
1359 mos7840_port->shadowLCR);
1360 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1361 mos7840_port->shadowLCR);
1366 /*****************************************************************************
1367 * mos7840_write_room
1368 * this function is called by the tty driver when it wants to know how many
1369 * bytes of data we can accept for a specific port.
1370 * If successful, we return the amount of room that we have for this port
1371 * Otherwise we return a negative error number.
1372 *****************************************************************************/
1374 static int mos7840_write_room(struct tty_struct *tty)
1376 struct usb_serial_port *port = tty->driver_data;
1379 unsigned long flags;
1380 struct moschip_port *mos7840_port;
1382 dbg("%s \n", " mos7840_write_room:entering ...........");
1384 if (mos7840_port_paranoia_check(port, __func__)) {
1385 dbg("%s", "Invalid port \n");
1386 dbg("%s \n", " mos7840_write_room:leaving ...........");
1390 mos7840_port = mos7840_get_port_private(port);
1391 if (mos7840_port == NULL) {
1392 dbg("%s \n", "mos7840_break:leaving ...........");
1396 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1397 for (i = 0; i < NUM_URBS; ++i) {
1398 if (!mos7840_port->busy[i])
1399 room += URB_TRANSFER_BUFFER_SIZE;
1401 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1403 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1404 dbg("%s - returns %d", __func__, 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 tty_struct *tty, struct usb_serial_port *port,
1418 const unsigned char *data, int count)
1424 unsigned long flags;
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",
1434 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 */
1449 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1452 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1454 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1455 dbg("mos7840_write:DLL value is %x\n", Data);
1458 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1459 dbg("mos7840_write:DLM value is %x\n", Data);
1461 Data = Data & ~SERIAL_LCR_DLAB;
1462 dbg("mos7840_write: mos7840_port->shadowLCR is %x\n",
1463 mos7840_port->shadowLCR);
1464 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1467 if (mos7840_port_paranoia_check(port, __func__)) {
1468 dbg("%s", "Port Paranoia failed \n");
1472 serial = port->serial;
1473 if (mos7840_serial_paranoia_check(serial, __func__)) {
1474 dbg("%s", "Serial Paranoia failed \n");
1478 mos7840_port = mos7840_get_port_private(port);
1479 if (mos7840_port == NULL) {
1480 dbg("%s", "mos7840_port is NULL\n");
1484 /* try to find a free urb in the list */
1487 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1488 for (i = 0; i < NUM_URBS; ++i) {
1489 if (!mos7840_port->busy[i]) {
1490 mos7840_port->busy[i] = 1;
1491 urb = mos7840_port->write_urb_pool[i];
1496 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1499 dbg("%s - no more free urbs", __func__);
1503 if (urb->transfer_buffer == NULL) {
1504 urb->transfer_buffer =
1505 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1507 if (urb->transfer_buffer == NULL) {
1508 dev_err(&port->dev, "%s no more kernel memory...\n",
1513 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1515 memcpy(urb->transfer_buffer, current_position, transfer_size);
1517 /* fill urb with data and submit */
1518 usb_fill_bulk_urb(urb,
1520 usb_sndbulkpipe(serial->dev,
1521 port->bulk_out_endpointAddress),
1522 urb->transfer_buffer,
1524 mos7840_bulk_out_data_callback, mos7840_port);
1526 data1 = urb->transfer_buffer;
1527 dbg("\nbulkout endpoint is %d", port->bulk_out_endpointAddress);
1529 /* send it down the pipe */
1530 status = usb_submit_urb(urb, GFP_ATOMIC);
1533 mos7840_port->busy[i] = 0;
1534 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1535 "with status = %d\n", __func__, status);
1536 bytes_sent = status;
1539 bytes_sent = transfer_size;
1540 mos7840_port->icount.tx += transfer_size;
1542 dbg("mos7840_port->icount.tx is %d:\n", mos7840_port->icount.tx);
1548 /*****************************************************************************
1550 * this function is called by the tty driver when it wants to stop the data
1551 * being read from the port.
1552 *****************************************************************************/
1554 static void mos7840_throttle(struct tty_struct *tty)
1556 struct usb_serial_port *port = tty->driver_data;
1557 struct moschip_port *mos7840_port;
1560 if (mos7840_port_paranoia_check(port, __func__)) {
1561 dbg("%s", "Invalid port \n");
1565 dbg("- port %d\n", port->number);
1567 mos7840_port = mos7840_get_port_private(port);
1569 if (mos7840_port == NULL)
1572 if (!mos7840_port->open) {
1573 dbg("%s\n", "port not opened");
1577 dbg("%s", "Entering .......... \n");
1579 /* if we are implementing XON/XOFF, send the stop character */
1581 unsigned char stop_char = STOP_CHAR(tty);
1582 status = mos7840_write(tty, port, &stop_char, 1);
1586 /* if we are implementing RTS/CTS, toggle that line */
1587 if (tty->termios->c_cflag & CRTSCTS) {
1588 mos7840_port->shadowMCR &= ~MCR_RTS;
1589 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1590 mos7840_port->shadowMCR);
1598 /*****************************************************************************
1599 * mos7840_unthrottle
1600 * this function is called by the tty driver when it wants to resume
1601 * the data being read from the port (called after mos7840_throttle is
1603 *****************************************************************************/
1604 static void mos7840_unthrottle(struct tty_struct *tty)
1606 struct usb_serial_port *port = tty->driver_data;
1608 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1610 if (mos7840_port_paranoia_check(port, __func__)) {
1611 dbg("%s", "Invalid port \n");
1615 if (mos7840_port == NULL)
1618 if (!mos7840_port->open) {
1619 dbg("%s - port not opened", __func__);
1623 dbg("%s", "Entering .......... \n");
1625 /* if we are implementing XON/XOFF, send the start character */
1627 unsigned char start_char = START_CHAR(tty);
1628 status = mos7840_write(tty, port, &start_char, 1);
1633 /* if we are implementing RTS/CTS, toggle that line */
1634 if (tty->termios->c_cflag & CRTSCTS) {
1635 mos7840_port->shadowMCR |= MCR_RTS;
1636 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1637 mos7840_port->shadowMCR);
1643 static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
1645 struct usb_serial_port *port = tty->driver_data;
1646 struct moschip_port *mos7840_port;
1647 unsigned int result;
1651 mos7840_port = mos7840_get_port_private(port);
1653 dbg("%s - port %d", __func__, port->number);
1655 if (mos7840_port == NULL)
1658 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1659 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1660 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1661 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1662 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1663 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1664 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1665 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1666 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1668 dbg("%s - 0x%04X", __func__, result);
1673 static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
1674 unsigned int set, unsigned int clear)
1676 struct usb_serial_port *port = tty->driver_data;
1677 struct moschip_port *mos7840_port;
1681 dbg("%s - port %d", __func__, port->number);
1683 mos7840_port = mos7840_get_port_private(port);
1685 if (mos7840_port == NULL)
1688 /* FIXME: What locks the port registers ? */
1689 mcr = mos7840_port->shadowMCR;
1690 if (clear & TIOCM_RTS)
1692 if (clear & TIOCM_DTR)
1694 if (clear & TIOCM_LOOP)
1695 mcr &= ~MCR_LOOPBACK;
1697 if (set & TIOCM_RTS)
1699 if (set & TIOCM_DTR)
1701 if (set & TIOCM_LOOP)
1702 mcr |= MCR_LOOPBACK;
1704 mos7840_port->shadowMCR = mcr;
1706 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1708 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
1715 /*****************************************************************************
1716 * mos7840_calc_baud_rate_divisor
1717 * this function calculates the proper baud rate divisor for the specified
1719 *****************************************************************************/
1720 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1724 dbg("%s - %d", __func__, baudRate);
1726 if (baudRate <= 115200) {
1727 *divisor = 115200 / baudRate;
1730 if ((baudRate > 115200) && (baudRate <= 230400)) {
1731 *divisor = 230400 / baudRate;
1732 *clk_sel_val = 0x10;
1733 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1734 *divisor = 403200 / baudRate;
1735 *clk_sel_val = 0x20;
1736 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1737 *divisor = 460800 / baudRate;
1738 *clk_sel_val = 0x30;
1739 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1740 *divisor = 806400 / baudRate;
1741 *clk_sel_val = 0x40;
1742 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1743 *divisor = 921600 / baudRate;
1744 *clk_sel_val = 0x50;
1745 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1746 *divisor = 1572864 / baudRate;
1747 *clk_sel_val = 0x60;
1748 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1749 *divisor = 3145728 / baudRate;
1750 *clk_sel_val = 0x70;
1756 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1757 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1758 *divisor = mos7840_divisor_table[i].Divisor;
1763 /* After trying for all the standard baud rates *
1764 * Try calculating the divisor for this baud rate */
1766 if (baudrate > 75 && baudrate < 230400) {
1767 /* get the divisor */
1768 custom = (__u16) (230400L / baudrate);
1770 /* Check for round off */
1771 round1 = (__u16) (2304000L / baudrate);
1772 round = (__u16) (round1 - (custom * 10));
1777 dbg(" Baud %d = %d\n", baudrate, custom);
1781 dbg("%s\n", " Baud calculation Failed...");
1786 /*****************************************************************************
1787 * mos7840_send_cmd_write_baud_rate
1788 * this function sends the proper command to change the baud rate of the
1790 *****************************************************************************/
1792 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1798 unsigned char number;
1800 struct usb_serial_port *port;
1802 if (mos7840_port == NULL)
1805 port = (struct usb_serial_port *)mos7840_port->port;
1806 if (mos7840_port_paranoia_check(port, __func__)) {
1807 dbg("%s", "Invalid port \n");
1811 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1812 dbg("%s", "Invalid Serial \n");
1816 dbg("%s", "Entering .......... \n");
1818 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1820 dbg("%s - port = %d, baud = %d", __func__,
1821 mos7840_port->port->number, baudRate);
1822 /* reset clk_uart_sel in spregOffset */
1823 if (baudRate > 115200) {
1824 #ifdef HW_flow_control
1825 /* NOTE: need to see the pther register to modify */
1826 /* setting h/w flow control bit to 1 */
1828 mos7840_port->shadowMCR = Data;
1829 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1832 dbg("Writing spreg failed in set_serial_baud\n");
1838 #ifdef HW_flow_control
1839 / *setting h/w flow control bit to 0 */
1841 mos7840_port->shadowMCR = Data;
1842 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1845 dbg("Writing spreg failed in set_serial_baud\n");
1852 if (1) { /* baudRate <= 115200) */
1855 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1857 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1860 dbg("reading spreg failed in set_serial_baud\n");
1863 Data = (Data & 0x8f) | clk_sel_val;
1864 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1867 dbg("Writing spreg failed in set_serial_baud\n");
1870 /* Calculate the Divisor */
1873 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1876 /* Enable access to divisor latch */
1877 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1878 mos7840_port->shadowLCR = Data;
1879 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1881 /* Write the divisor */
1882 Data = (unsigned char)(divisor & 0xff);
1883 dbg("set_serial_baud Value to write DLL is %x\n", Data);
1884 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1886 Data = (unsigned char)((divisor & 0xff00) >> 8);
1887 dbg("set_serial_baud Value to write DLM is %x\n", Data);
1888 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1890 /* Disable access to divisor latch */
1891 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1892 mos7840_port->shadowLCR = Data;
1893 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1899 /*****************************************************************************
1900 * mos7840_change_port_settings
1901 * This routine is called to set the UART on the device to match
1902 * the specified new settings.
1903 *****************************************************************************/
1905 static void mos7840_change_port_settings(struct tty_struct *tty,
1906 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1916 struct usb_serial_port *port;
1917 struct usb_serial *serial;
1919 if (mos7840_port == NULL)
1922 port = (struct usb_serial_port *)mos7840_port->port;
1924 if (mos7840_port_paranoia_check(port, __func__)) {
1925 dbg("%s", "Invalid port \n");
1929 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1930 dbg("%s", "Invalid Serial \n");
1934 serial = port->serial;
1936 dbg("%s - port %d", __func__, mos7840_port->port->number);
1938 if (!mos7840_port->open) {
1939 dbg("%s - port not opened", __func__);
1943 dbg("%s", "Entering .......... \n");
1947 lParity = LCR_PAR_NONE;
1949 cflag = tty->termios->c_cflag;
1950 iflag = tty->termios->c_iflag;
1952 /* Change the number of bits */
1953 if (cflag & CSIZE) {
1954 switch (cflag & CSIZE) {
1972 /* Change the Parity bit */
1973 if (cflag & PARENB) {
1974 if (cflag & PARODD) {
1975 lParity = LCR_PAR_ODD;
1976 dbg("%s - parity = odd", __func__);
1978 lParity = LCR_PAR_EVEN;
1979 dbg("%s - parity = even", __func__);
1983 dbg("%s - parity = none", __func__);
1987 lParity = lParity | 0x20;
1989 /* Change the Stop bit */
1990 if (cflag & CSTOPB) {
1992 dbg("%s - stop bits = 2", __func__);
1995 dbg("%s - stop bits = 1", __func__);
1998 /* Update the LCR with the correct value */
1999 mos7840_port->shadowLCR &=
2000 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
2001 mos7840_port->shadowLCR |= (lData | lParity | lStop);
2003 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x\n",
2004 mos7840_port->shadowLCR);
2005 /* Disable Interrupts */
2007 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2010 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2013 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
2015 /* Send the updated LCR value to the mos7840 */
2016 Data = mos7840_port->shadowLCR;
2018 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
2021 mos7840_port->shadowMCR = Data;
2022 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2024 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2026 /* set up the MCR register and send it to the mos7840 */
2028 mos7840_port->shadowMCR = MCR_MASTER_IE;
2030 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
2032 if (cflag & CRTSCTS)
2033 mos7840_port->shadowMCR |= (MCR_XON_ANY);
2035 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
2037 Data = mos7840_port->shadowMCR;
2038 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2040 /* Determine divisor based on baud rate */
2041 baud = tty_get_baud_rate(tty);
2044 /* pick a default, any default... */
2045 dbg("%s\n", "Picked default baud...");
2049 dbg("%s - baud rate = %d", __func__, baud);
2050 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2052 /* Enable Interrupts */
2054 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2056 if (mos7840_port->read_urb_busy == false) {
2057 mos7840_port->read_urb->dev = serial->dev;
2058 mos7840_port->read_urb_busy = true;
2059 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2061 dbg("usb_submit_urb(read bulk) failed, status = %d",
2063 mos7840_port->read_urb_busy = false;
2066 wake_up(&mos7840_port->delta_msr_wait);
2067 mos7840_port->delta_msr_cond = 1;
2068 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x\n",
2069 mos7840_port->shadowLCR);
2074 /*****************************************************************************
2075 * mos7840_set_termios
2076 * this function is called by the tty driver when it wants to change
2077 * the termios structure
2078 *****************************************************************************/
2080 static void mos7840_set_termios(struct tty_struct *tty,
2081 struct usb_serial_port *port,
2082 struct ktermios *old_termios)
2086 struct usb_serial *serial;
2087 struct moschip_port *mos7840_port;
2088 dbg("mos7840_set_termios: START\n");
2089 if (mos7840_port_paranoia_check(port, __func__)) {
2090 dbg("%s", "Invalid port \n");
2094 serial = port->serial;
2096 if (mos7840_serial_paranoia_check(serial, __func__)) {
2097 dbg("%s", "Invalid Serial \n");
2101 mos7840_port = mos7840_get_port_private(port);
2103 if (mos7840_port == NULL)
2106 if (!mos7840_port->open) {
2107 dbg("%s - port not opened", __func__);
2111 dbg("%s\n", "setting termios - ");
2113 cflag = tty->termios->c_cflag;
2115 dbg("%s - clfag %08x iflag %08x", __func__,
2116 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2117 dbg("%s - old clfag %08x old iflag %08x", __func__,
2118 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2119 dbg("%s - port %d", __func__, port->number);
2121 /* change the port settings to the new ones specified */
2123 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2125 if (!mos7840_port->read_urb) {
2126 dbg("%s", "URB KILLED !!!!!\n");
2130 if (mos7840_port->read_urb_busy == false) {
2131 mos7840_port->read_urb->dev = serial->dev;
2132 mos7840_port->read_urb_busy = true;
2133 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2135 dbg("usb_submit_urb(read bulk) failed, status = %d",
2137 mos7840_port->read_urb_busy = false;
2143 /*****************************************************************************
2144 * mos7840_get_lsr_info - get line status register info
2146 * Purpose: Let user call ioctl() to get info when the UART physically
2147 * is emptied. On bus types like RS485, the transmitter must
2148 * release the bus after transmitting. This must be done when
2149 * the transmit shift register is empty, not be done when the
2150 * transmit holding register is empty. This functionality
2151 * allows an RS485 driver to be written in user space.
2152 *****************************************************************************/
2154 static int mos7840_get_lsr_info(struct tty_struct *tty,
2155 unsigned int __user *value)
2158 unsigned int result = 0;
2160 count = mos7840_chars_in_buffer(tty);
2162 dbg("%s -- Empty", __func__);
2163 result = TIOCSER_TEMT;
2166 if (copy_to_user(value, &result, sizeof(int)))
2171 /*****************************************************************************
2172 * mos7840_set_modem_info
2173 * function to set modem info
2174 *****************************************************************************/
2176 /* FIXME: Should be using the model control hooks */
2178 static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2179 unsigned int cmd, unsigned int __user *value)
2185 struct usb_serial_port *port;
2187 if (mos7840_port == NULL)
2190 port = (struct usb_serial_port *)mos7840_port->port;
2191 if (mos7840_port_paranoia_check(port, __func__)) {
2192 dbg("%s", "Invalid port \n");
2196 mcr = mos7840_port->shadowMCR;
2198 if (copy_from_user(&arg, value, sizeof(int)))
2203 if (arg & TIOCM_RTS)
2205 if (arg & TIOCM_DTR)
2207 if (arg & TIOCM_LOOP)
2208 mcr |= MCR_LOOPBACK;
2212 if (arg & TIOCM_RTS)
2214 if (arg & TIOCM_DTR)
2216 if (arg & TIOCM_LOOP)
2217 mcr &= ~MCR_LOOPBACK;
2221 /* turn off the RTS and DTR and LOOPBACK
2222 * and then only turn on what was asked to */
2223 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2224 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2225 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2226 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2231 mos7840_port->shadowMCR = mcr;
2233 Data = mos7840_port->shadowMCR;
2234 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2237 dbg("setting MODEM_CONTROL_REGISTER Failed\n");
2244 /*****************************************************************************
2245 * mos7840_get_modem_info
2246 * function to get modem info
2247 *****************************************************************************/
2249 static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
2250 unsigned int __user *value)
2252 unsigned int result = 0;
2254 unsigned int mcr = mos7840_port->shadowMCR;
2255 mos7840_get_uart_reg(mos7840_port->port,
2256 MODEM_STATUS_REGISTER, &msr);
2257 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2258 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2259 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2260 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2261 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2262 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2264 dbg("%s -- %x", __func__, result);
2266 if (copy_to_user(value, &result, sizeof(int)))
2271 /*****************************************************************************
2272 * mos7840_get_serial_info
2273 * function to get information about serial port
2274 *****************************************************************************/
2276 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2277 struct serial_struct __user *retinfo)
2279 struct serial_struct tmp;
2281 if (mos7840_port == NULL)
2287 memset(&tmp, 0, sizeof(tmp));
2289 tmp.type = PORT_16550A;
2290 tmp.line = mos7840_port->port->serial->minor;
2291 tmp.port = mos7840_port->port->number;
2293 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2294 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2295 tmp.baud_base = 9600;
2296 tmp.close_delay = 5 * HZ;
2297 tmp.closing_wait = 30 * HZ;
2299 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2304 /*****************************************************************************
2306 * this function handles any ioctl calls to the driver
2307 *****************************************************************************/
2309 static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
2310 unsigned int cmd, unsigned long arg)
2312 struct usb_serial_port *port = tty->driver_data;
2313 void __user *argp = (void __user *)arg;
2314 struct moschip_port *mos7840_port;
2316 struct async_icount cnow;
2317 struct async_icount cprev;
2318 struct serial_icounter_struct icount;
2321 if (mos7840_port_paranoia_check(port, __func__)) {
2322 dbg("%s", "Invalid port \n");
2326 mos7840_port = mos7840_get_port_private(port);
2328 if (mos7840_port == NULL)
2331 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2334 /* return number of bytes available */
2337 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2338 return mos7840_get_lsr_info(tty, argp);
2341 /* FIXME: use the modem hooks and remove this */
2345 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
2348 mos7840_set_modem_info(mos7840_port, cmd, argp);
2352 dbg("%s (%d) TIOCMGET", __func__, port->number);
2353 return mos7840_get_modem_info(mos7840_port, argp);
2356 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
2357 return mos7840_get_serial_info(mos7840_port, argp);
2360 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
2364 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
2365 cprev = mos7840_port->icount;
2367 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2368 mos7840_port->delta_msr_cond = 0;
2369 wait_event_interruptible(mos7840_port->delta_msr_wait,
2371 delta_msr_cond == 1));
2373 /* see if a signal did it */
2374 if (signal_pending(current))
2375 return -ERESTARTSYS;
2376 cnow = mos7840_port->icount;
2378 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2379 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2380 return -EIO; /* no change => error */
2381 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2382 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2383 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2384 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2393 cnow = mos7840_port->icount;
2395 icount.cts = cnow.cts;
2396 icount.dsr = cnow.dsr;
2397 icount.rng = cnow.rng;
2398 icount.dcd = cnow.dcd;
2399 icount.rx = cnow.rx;
2400 icount.tx = cnow.tx;
2401 icount.frame = cnow.frame;
2402 icount.overrun = cnow.overrun;
2403 icount.parity = cnow.parity;
2404 icount.brk = cnow.brk;
2405 icount.buf_overrun = cnow.buf_overrun;
2407 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2408 port->number, icount.rx, icount.tx);
2409 if (copy_to_user(argp, &icount, sizeof(icount)))
2415 return -ENOIOCTLCMD;
2418 static int mos7840_calc_num_ports(struct usb_serial *serial)
2420 int mos7840_num_ports = 0;
2422 dbg("numberofendpoints: %d \n",
2423 (int)serial->interface->cur_altsetting->desc.bNumEndpoints);
2424 dbg("numberofendpoints: %d \n",
2425 (int)serial->interface->altsetting->desc.bNumEndpoints);
2426 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2427 mos7840_num_ports = serial->num_ports = 2;
2428 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
2429 serial->num_bulk_in = 4;
2430 serial->num_bulk_out = 4;
2431 mos7840_num_ports = serial->num_ports = 4;
2434 return mos7840_num_ports;
2437 /****************************************************************************
2439 ****************************************************************************/
2441 static int mos7840_startup(struct usb_serial *serial)
2443 struct moschip_port *mos7840_port;
2444 struct usb_device *dev;
2448 dbg("%s \n", " mos7840_startup :entering..........");
2451 dbg("%s\n", "Invalid Handler");
2457 dbg("%s\n", "Entering...");
2459 /* we set up the pointers to the endpoints in the mos7840_open *
2460 * function, as the structures aren't created yet. */
2462 /* set up port private structures */
2463 for (i = 0; i < serial->num_ports; ++i) {
2464 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2465 if (mos7840_port == NULL) {
2466 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2468 i--; /* don't follow NULL pointer cleaning up */
2472 /* Initialize all port interrupt end point to port 0 int
2473 * endpoint. Our device has only one interrupt end point
2474 * common to all port */
2476 mos7840_port->port = serial->port[i];
2477 mos7840_set_port_private(serial->port[i], mos7840_port);
2478 spin_lock_init(&mos7840_port->pool_lock);
2480 mos7840_port->port_num = ((serial->port[i]->number -
2481 (serial->port[i]->serial->minor)) +
2484 if (mos7840_port->port_num == 1) {
2485 mos7840_port->SpRegOffset = 0x0;
2486 mos7840_port->ControlRegOffset = 0x1;
2487 mos7840_port->DcrRegOffset = 0x4;
2488 } else if ((mos7840_port->port_num == 2)
2489 && (serial->num_ports == 4)) {
2490 mos7840_port->SpRegOffset = 0x8;
2491 mos7840_port->ControlRegOffset = 0x9;
2492 mos7840_port->DcrRegOffset = 0x16;
2493 } else if ((mos7840_port->port_num == 2)
2494 && (serial->num_ports == 2)) {
2495 mos7840_port->SpRegOffset = 0xa;
2496 mos7840_port->ControlRegOffset = 0xb;
2497 mos7840_port->DcrRegOffset = 0x19;
2498 } else if ((mos7840_port->port_num == 3)
2499 && (serial->num_ports == 4)) {
2500 mos7840_port->SpRegOffset = 0xa;
2501 mos7840_port->ControlRegOffset = 0xb;
2502 mos7840_port->DcrRegOffset = 0x19;
2503 } else if ((mos7840_port->port_num == 4)
2504 && (serial->num_ports == 4)) {
2505 mos7840_port->SpRegOffset = 0xc;
2506 mos7840_port->ControlRegOffset = 0xd;
2507 mos7840_port->DcrRegOffset = 0x1c;
2509 mos7840_dump_serial_port(mos7840_port);
2510 mos7840_set_port_private(serial->port[i], mos7840_port);
2512 /* enable rx_disable bit in control register */
2513 status = mos7840_get_reg_sync(serial->port[i],
2514 mos7840_port->ControlRegOffset, &Data);
2516 dbg("Reading ControlReg failed status-0x%x\n", status);
2519 dbg("ControlReg Reading success val is %x, status%d\n",
2521 Data |= 0x08; /* setting driver done bit */
2522 Data |= 0x04; /* sp1_bit to have cts change reflect in
2525 /* Data |= 0x20; //rx_disable bit */
2526 status = mos7840_set_reg_sync(serial->port[i],
2527 mos7840_port->ControlRegOffset, Data);
2529 dbg("Writing ControlReg failed(rx_disable) status-0x%x\n", status);
2532 dbg("ControlReg Writing success(rx_disable) status%d\n",
2535 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2538 status = mos7840_set_reg_sync(serial->port[i],
2539 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2541 dbg("Writing DCR0 failed status-0x%x\n", status);
2544 dbg("DCR0 Writing success status%d\n", status);
2547 status = mos7840_set_reg_sync(serial->port[i],
2548 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2550 dbg("Writing DCR1 failed status-0x%x\n", status);
2553 dbg("DCR1 Writing success status%d\n", status);
2556 status = mos7840_set_reg_sync(serial->port[i],
2557 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2559 dbg("Writing DCR2 failed status-0x%x\n", status);
2562 dbg("DCR2 Writing success status%d\n", status);
2564 /* write values in clkstart0x0 and clkmulti 0x20 */
2566 status = mos7840_set_reg_sync(serial->port[i],
2567 CLK_START_VALUE_REGISTER, Data);
2569 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x\n", status);
2572 dbg("CLK_START_VALUE_REGISTER Writing success status%d\n", status);
2575 status = mos7840_set_reg_sync(serial->port[i],
2576 CLK_MULTI_REGISTER, Data);
2578 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x\n",
2582 dbg("CLK_MULTI_REGISTER Writing success status%d\n",
2585 /* write value 0x0 to scratchpad register */
2587 status = mos7840_set_uart_reg(serial->port[i],
2588 SCRATCH_PAD_REGISTER, Data);
2590 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x\n",
2594 dbg("SCRATCH_PAD_REGISTER Writing success status%d\n",
2597 /* Zero Length flag register */
2598 if ((mos7840_port->port_num != 1)
2599 && (serial->num_ports == 2)) {
2602 status = mos7840_set_reg_sync(serial->port[i],
2604 ((__u16)mos7840_port->port_num)), Data);
2605 dbg("ZLIP offset%x\n",
2607 ((__u16) mos7840_port->port_num)));
2609 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2613 dbg("ZLP_REG%d Writing success status%d\n",
2617 status = mos7840_set_reg_sync(serial->port[i],
2619 ((__u16)mos7840_port->port_num) - 0x1), Data);
2620 dbg("ZLIP offset%x\n",
2622 ((__u16) mos7840_port->port_num) - 0x1));
2624 dbg("Writing ZLP_REG%d failed status-0x%x\n",
2628 dbg("ZLP_REG%d Writing success status%d\n",
2632 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2633 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2634 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2636 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2637 !mos7840_port->dr) {
2643 /* Zero Length flag enable */
2645 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2647 dbg("Writing ZLP_REG5 failed status-0x%x\n", status);
2650 dbg("ZLP_REG5 Writing success status%d\n", status);
2652 /* setting configuration feature to one */
2653 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2654 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2657 for (/* nothing */; i >= 0; i--) {
2658 mos7840_port = mos7840_get_port_private(serial->port[i]);
2660 kfree(mos7840_port->dr);
2661 kfree(mos7840_port->ctrl_buf);
2662 usb_free_urb(mos7840_port->control_urb);
2663 kfree(mos7840_port);
2664 serial->port[i] = NULL;
2669 /****************************************************************************
2671 * This function is called whenever the device is removed from the usb bus.
2672 ****************************************************************************/
2674 static void mos7840_shutdown(struct usb_serial *serial)
2677 unsigned long flags;
2678 struct moschip_port *mos7840_port;
2679 dbg("%s \n", " shutdown :entering..........");
2682 dbg("%s", "Invalid Handler \n");
2686 /* check for the ports to be closed,close the ports and disconnect */
2688 /* free private structure allocated for serial port *
2689 * stop reads and writes on all ports */
2691 for (i = 0; i < serial->num_ports; ++i) {
2692 mos7840_port = mos7840_get_port_private(serial->port[i]);
2693 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2694 mos7840_port->zombie = 1;
2695 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2696 usb_kill_urb(mos7840_port->control_urb);
2697 kfree(mos7840_port->ctrl_buf);
2698 kfree(mos7840_port->dr);
2699 kfree(mos7840_port);
2700 mos7840_set_port_private(serial->port[i], NULL);
2703 dbg("%s\n", "Thank u :: ");
2707 static struct usb_driver io_driver = {
2709 .probe = usb_serial_probe,
2710 .disconnect = usb_serial_disconnect,
2711 .id_table = moschip_id_table_combined,
2715 static struct usb_serial_driver moschip7840_4port_device = {
2717 .owner = THIS_MODULE,
2720 .description = DRIVER_DESC,
2721 .usb_driver = &io_driver,
2722 .id_table = moschip_port_id_table,
2724 .open = mos7840_open,
2725 .close = mos7840_close,
2726 .write = mos7840_write,
2727 .write_room = mos7840_write_room,
2728 .chars_in_buffer = mos7840_chars_in_buffer,
2729 .throttle = mos7840_throttle,
2730 .unthrottle = mos7840_unthrottle,
2731 .calc_num_ports = mos7840_calc_num_ports,
2732 #ifdef MCSSerialProbe
2733 .probe = mos7840_serial_probe,
2735 .ioctl = mos7840_ioctl,
2736 .set_termios = mos7840_set_termios,
2737 .break_ctl = mos7840_break,
2738 .tiocmget = mos7840_tiocmget,
2739 .tiocmset = mos7840_tiocmset,
2740 .attach = mos7840_startup,
2741 .shutdown = mos7840_shutdown,
2742 .read_bulk_callback = mos7840_bulk_in_callback,
2743 .read_int_callback = mos7840_interrupt_callback,
2746 /****************************************************************************
2748 * This is called by the module subsystem, or on startup to initialize us
2749 ****************************************************************************/
2750 static int __init moschip7840_init(void)
2754 dbg("%s \n", " mos7840_init :entering..........");
2756 /* Register with the usb serial */
2757 retval = usb_serial_register(&moschip7840_4port_device);
2760 goto failed_port_device_register;
2762 dbg("%s\n", "Entring...");
2763 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2766 /* Register with the usb */
2767 retval = usb_register(&io_driver);
2769 dbg("%s\n", "Leaving...");
2772 usb_serial_deregister(&moschip7840_4port_device);
2773 failed_port_device_register:
2777 /****************************************************************************
2779 * Called when the driver is about to be unloaded.
2780 ****************************************************************************/
2781 static void __exit moschip7840_exit(void)
2784 dbg("%s \n", " mos7840_exit :entering..........");
2786 usb_deregister(&io_driver);
2788 usb_serial_deregister(&moschip7840_4port_device);
2790 dbg("%s\n", "Entring...");
2793 module_init(moschip7840_init);
2794 module_exit(moschip7840_exit);
2796 /* Module information */
2797 MODULE_DESCRIPTION(DRIVER_DESC);
2798 MODULE_LICENSE("GPL");
2800 module_param(debug, bool, S_IRUGO | S_IWUSR);
2801 MODULE_PARM_DESC(debug, "Debug enabled or not");