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/smp_lock.h>
30 #include <linux/tty.h>
31 #include <linux/tty_driver.h>
32 #include <linux/tty_flip.h>
33 #include <linux/module.h>
34 #include <linux/serial.h>
35 #include <linux/usb.h>
36 #include <linux/usb/serial.h>
37 #include <linux/uaccess.h>
42 #define DRIVER_VERSION "1.3.2"
43 #define DRIVER_DESC "Moschip 7840/7820 USB Serial Driver"
46 * 16C50 UART register defines
49 #define LCR_BITS_5 0x00 /* 5 bits/char */
50 #define LCR_BITS_6 0x01 /* 6 bits/char */
51 #define LCR_BITS_7 0x02 /* 7 bits/char */
52 #define LCR_BITS_8 0x03 /* 8 bits/char */
53 #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
55 #define LCR_STOP_1 0x00 /* 1 stop bit */
56 #define LCR_STOP_1_5 0x04 /* 1.5 stop bits (if 5 bits/char) */
57 #define LCR_STOP_2 0x04 /* 2 stop bits (if 6-8 bits/char) */
58 #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
60 #define LCR_PAR_NONE 0x00 /* No parity */
61 #define LCR_PAR_ODD 0x08 /* Odd parity */
62 #define LCR_PAR_EVEN 0x18 /* Even parity */
63 #define LCR_PAR_MARK 0x28 /* Force parity bit to 1 */
64 #define LCR_PAR_SPACE 0x38 /* Force parity bit to 0 */
65 #define LCR_PAR_MASK 0x38 /* Mask for parity field */
67 #define LCR_SET_BREAK 0x40 /* Set Break condition */
68 #define LCR_DL_ENABLE 0x80 /* Enable access to divisor latch */
70 #define MCR_DTR 0x01 /* Assert DTR */
71 #define MCR_RTS 0x02 /* Assert RTS */
72 #define MCR_OUT1 0x04 /* Loopback only: Sets state of RI */
73 #define MCR_MASTER_IE 0x08 /* Enable interrupt outputs */
74 #define MCR_LOOPBACK 0x10 /* Set internal (digital) loopback mode */
75 #define MCR_XON_ANY 0x20 /* Enable any char to exit XOFF mode */
77 #define MOS7840_MSR_CTS 0x10 /* Current state of CTS */
78 #define MOS7840_MSR_DSR 0x20 /* Current state of DSR */
79 #define MOS7840_MSR_RI 0x40 /* Current state of RI */
80 #define MOS7840_MSR_CD 0x80 /* Current state of CD */
83 * Defines used for sending commands to port
86 #define WAIT_FOR_EVER (HZ * 0) /* timeout urb is wait for ever */
87 #define MOS_WDR_TIMEOUT (HZ * 5) /* default urb timeout */
89 #define MOS_PORT1 0x0200
90 #define MOS_PORT2 0x0300
91 #define MOS_VENREG 0x0000
92 #define MOS_MAX_PORT 0x02
93 #define MOS_WRITE 0x0E
97 #define MCS_RD_RTYPE 0xC0
98 #define MCS_WR_RTYPE 0x40
99 #define MCS_RDREQ 0x0D
100 #define MCS_WRREQ 0x0E
101 #define MCS_CTRL_TIMEOUT 500
102 #define VENDOR_READ_LENGTH (0x01)
104 #define MAX_NAME_LEN 64
106 #define ZLP_REG1 0x3A /* Zero_Flag_Reg1 58 */
107 #define ZLP_REG5 0x3E /* Zero_Flag_Reg5 62 */
109 /* For higher baud Rates use TIOCEXBAUD */
110 #define TIOCEXBAUD 0x5462
112 /* vendor id and device id defines */
114 /* The native mos7840/7820 component */
115 #define USB_VENDOR_ID_MOSCHIP 0x9710
116 #define MOSCHIP_DEVICE_ID_7840 0x7840
117 #define MOSCHIP_DEVICE_ID_7820 0x7820
118 /* The native component can have its vendor/device id's overridden
119 * in vendor-specific implementations. Such devices can be handled
120 * by making a change here, in moschip_port_id_table, and in
121 * moschip_id_table_combined
123 #define USB_VENDOR_ID_BANDB 0x0856
124 #define BANDB_DEVICE_ID_USOPTL4_4 0xAC44
125 #define BANDB_DEVICE_ID_USOPTL4_2 0xAC42
127 /* This driver also supports the ATEN UC2324 device since it is mos7840 based
128 * - if I knew the device id it would also support the ATEN UC2322 */
129 #define USB_VENDOR_ID_ATENINTL 0x0557
130 #define ATENINTL_DEVICE_ID_UC2324 0x2011
132 /* Interrupt Routine Defines */
134 #define SERIAL_IIR_RLS 0x06
135 #define SERIAL_IIR_MS 0x00
138 * Emulation of the bit mask on the LINE STATUS REGISTER.
140 #define SERIAL_LSR_DR 0x0001
141 #define SERIAL_LSR_OE 0x0002
142 #define SERIAL_LSR_PE 0x0004
143 #define SERIAL_LSR_FE 0x0008
144 #define SERIAL_LSR_BI 0x0010
146 #define MOS_MSR_DELTA_CTS 0x10
147 #define MOS_MSR_DELTA_DSR 0x20
148 #define MOS_MSR_DELTA_RI 0x40
149 #define MOS_MSR_DELTA_CD 0x80
151 /* Serial Port register Address */
152 #define INTERRUPT_ENABLE_REGISTER ((__u16)(0x01))
153 #define FIFO_CONTROL_REGISTER ((__u16)(0x02))
154 #define LINE_CONTROL_REGISTER ((__u16)(0x03))
155 #define MODEM_CONTROL_REGISTER ((__u16)(0x04))
156 #define LINE_STATUS_REGISTER ((__u16)(0x05))
157 #define MODEM_STATUS_REGISTER ((__u16)(0x06))
158 #define SCRATCH_PAD_REGISTER ((__u16)(0x07))
159 #define DIVISOR_LATCH_LSB ((__u16)(0x00))
160 #define DIVISOR_LATCH_MSB ((__u16)(0x01))
162 #define CLK_MULTI_REGISTER ((__u16)(0x02))
163 #define CLK_START_VALUE_REGISTER ((__u16)(0x03))
165 #define SERIAL_LCR_DLAB ((__u16)(0x0080))
168 * URB POOL related defines
170 #define NUM_URBS 16 /* URB Count */
171 #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
174 static struct usb_device_id moschip_port_id_table[] = {
175 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
176 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
177 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
178 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
179 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
180 {} /* terminating entry */
183 static __devinitdata struct usb_device_id moschip_id_table_combined[] = {
184 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7840)},
185 {USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7820)},
186 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_4)},
187 {USB_DEVICE(USB_VENDOR_ID_BANDB, BANDB_DEVICE_ID_USOPTL4_2)},
188 {USB_DEVICE(USB_VENDOR_ID_ATENINTL, ATENINTL_DEVICE_ID_UC2324)},
189 {} /* terminating entry */
192 MODULE_DEVICE_TABLE(usb, moschip_id_table_combined);
194 /* This structure holds all of the local port information */
196 struct moschip_port {
197 int port_num; /*Actual port number in the device(1,2,etc) */
198 struct urb *write_urb; /* write URB for this port */
199 struct urb *read_urb; /* read URB for this port */
201 __u8 shadowLCR; /* last LCR value received */
202 __u8 shadowMCR; /* last MCR value received */
206 wait_queue_head_t wait_chase; /* for handling sleeping while waiting for chase to finish */
207 wait_queue_head_t delta_msr_wait; /* for handling sleeping while waiting for msr change to happen */
209 struct async_icount icount;
210 struct usb_serial_port *port; /* loop back to the owner of this object */
214 __u8 ControlRegOffset;
216 /* for processing control URBS in interrupt context */
217 struct urb *control_urb;
218 struct usb_ctrlrequest *dr;
222 spinlock_t pool_lock;
223 struct urb *write_urb_pool[NUM_URBS];
232 * mos7840_set_reg_sync
233 * To set the Control register by calling usb_fill_control_urb function
234 * by passing usb_sndctrlpipe function as parameter.
237 static int mos7840_set_reg_sync(struct usb_serial_port *port, __u16 reg,
240 struct usb_device *dev = port->serial->dev;
242 dbg("mos7840_set_reg_sync offset is %x, value %x", reg, val);
244 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
245 MCS_WR_RTYPE, val, reg, NULL, 0,
250 * mos7840_get_reg_sync
251 * To set the Uart register by calling usb_fill_control_urb function by
252 * passing usb_rcvctrlpipe function as parameter.
255 static int mos7840_get_reg_sync(struct usb_serial_port *port, __u16 reg,
258 struct usb_device *dev = port->serial->dev;
261 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
262 MCS_RD_RTYPE, 0, reg, val, VENDOR_READ_LENGTH,
264 dbg("mos7840_get_reg_sync offset is %x, return val %x", reg, *val);
265 *val = (*val) & 0x00ff;
270 * mos7840_set_uart_reg
271 * To set the Uart register by calling usb_fill_control_urb function by
272 * passing usb_sndctrlpipe function as parameter.
275 static int mos7840_set_uart_reg(struct usb_serial_port *port, __u16 reg,
279 struct usb_device *dev = port->serial->dev;
281 /* For the UART control registers, the application number need
283 if (port->serial->num_ports == 4) {
284 val |= (((__u16) port->number -
285 (__u16) (port->serial->minor)) + 1) << 8;
286 dbg("mos7840_set_uart_reg application number is %x", val);
288 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
289 val |= (((__u16) port->number -
290 (__u16) (port->serial->minor)) + 1) << 8;
291 dbg("mos7840_set_uart_reg application number is %x",
295 (((__u16) port->number -
296 (__u16) (port->serial->minor)) + 2) << 8;
297 dbg("mos7840_set_uart_reg application number is %x",
301 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WRREQ,
302 MCS_WR_RTYPE, val, reg, NULL, 0,
308 * mos7840_get_uart_reg
309 * To set the Control register by calling usb_fill_control_urb function
310 * by passing usb_rcvctrlpipe function as parameter.
312 static int mos7840_get_uart_reg(struct usb_serial_port *port, __u16 reg,
315 struct usb_device *dev = port->serial->dev;
319 /* dbg("application number is %4x",
320 (((__u16)port->number - (__u16)(port->serial->minor))+1)<<8); */
321 /* Wval is same as application number */
322 if (port->serial->num_ports == 4) {
324 (((__u16) port->number - (__u16) (port->serial->minor)) +
326 dbg("mos7840_get_uart_reg application number is %x", Wval);
328 if (((__u16) port->number - (__u16) (port->serial->minor)) == 0) {
329 Wval = (((__u16) port->number -
330 (__u16) (port->serial->minor)) + 1) << 8;
331 dbg("mos7840_get_uart_reg application number is %x",
334 Wval = (((__u16) port->number -
335 (__u16) (port->serial->minor)) + 2) << 8;
336 dbg("mos7840_get_uart_reg application number is %x",
340 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RDREQ,
341 MCS_RD_RTYPE, Wval, reg, val, VENDOR_READ_LENGTH,
343 *val = (*val) & 0x00ff;
347 static void mos7840_dump_serial_port(struct moschip_port *mos7840_port)
350 dbg("***************************************");
351 dbg("SpRegOffset is %2x", mos7840_port->SpRegOffset);
352 dbg("ControlRegOffset is %2x", mos7840_port->ControlRegOffset);
353 dbg("DCRRegOffset is %2x", mos7840_port->DcrRegOffset);
354 dbg("***************************************");
358 /************************************************************************/
359 /************************************************************************/
360 /* I N T E R F A C E F U N C T I O N S */
361 /* I N T E R F A C E F U N C T I O N S */
362 /************************************************************************/
363 /************************************************************************/
365 static inline void mos7840_set_port_private(struct usb_serial_port *port,
366 struct moschip_port *data)
368 usb_set_serial_port_data(port, (void *)data);
371 static inline struct moschip_port *mos7840_get_port_private(struct
375 return (struct moschip_port *)usb_get_serial_port_data(port);
378 static void mos7840_handle_new_msr(struct moschip_port *port, __u8 new_msr)
380 struct moschip_port *mos7840_port;
381 struct async_icount *icount;
383 icount = &mos7840_port->icount;
385 (MOS_MSR_DELTA_CTS | MOS_MSR_DELTA_DSR | MOS_MSR_DELTA_RI |
387 icount = &mos7840_port->icount;
389 /* update input line counters */
390 if (new_msr & MOS_MSR_DELTA_CTS) {
394 if (new_msr & MOS_MSR_DELTA_DSR) {
398 if (new_msr & MOS_MSR_DELTA_CD) {
402 if (new_msr & MOS_MSR_DELTA_RI) {
409 static void mos7840_handle_new_lsr(struct moschip_port *port, __u8 new_lsr)
411 struct async_icount *icount;
413 dbg("%s - %02x", __func__, new_lsr);
415 if (new_lsr & SERIAL_LSR_BI) {
417 * Parity and Framing errors only count if they
418 * occur exclusive of a break being
421 new_lsr &= (__u8) (SERIAL_LSR_OE | SERIAL_LSR_BI);
424 /* update input line counters */
425 icount = &port->icount;
426 if (new_lsr & SERIAL_LSR_BI) {
430 if (new_lsr & SERIAL_LSR_OE) {
434 if (new_lsr & SERIAL_LSR_PE) {
438 if (new_lsr & SERIAL_LSR_FE) {
444 /************************************************************************/
445 /************************************************************************/
446 /* U S B C A L L B A C K F U N C T I O N S */
447 /* U S B C A L L B A C K F U N C T I O N S */
448 /************************************************************************/
449 /************************************************************************/
451 static void mos7840_control_callback(struct urb *urb)
454 struct moschip_port *mos7840_port;
457 int status = urb->status;
459 mos7840_port = urb->context;
468 /* this urb is terminated, clean up */
469 dbg("%s - urb shutting down with status: %d", __func__,
473 dbg("%s - nonzero urb status received: %d", __func__,
478 dbg("%s urb buffer size is %d", __func__, urb->actual_length);
479 dbg("%s mos7840_port->MsrLsr is %d port %d", __func__,
480 mos7840_port->MsrLsr, mos7840_port->port_num);
481 data = urb->transfer_buffer;
482 regval = (__u8) data[0];
483 dbg("%s data is %x", __func__, regval);
484 if (mos7840_port->MsrLsr == 0)
485 mos7840_handle_new_msr(mos7840_port, regval);
486 else if (mos7840_port->MsrLsr == 1)
487 mos7840_handle_new_lsr(mos7840_port, regval);
490 spin_lock(&mos7840_port->pool_lock);
491 if (!mos7840_port->zombie)
492 result = usb_submit_urb(mos7840_port->int_urb, GFP_ATOMIC);
493 spin_unlock(&mos7840_port->pool_lock);
495 dev_err(&urb->dev->dev,
496 "%s - Error %d submitting interrupt urb\n",
501 static int mos7840_get_reg(struct moschip_port *mcs, __u16 Wval, __u16 reg,
504 struct usb_device *dev = mcs->port->serial->dev;
505 struct usb_ctrlrequest *dr = mcs->dr;
506 unsigned char *buffer = mcs->ctrl_buf;
509 dr->bRequestType = MCS_RD_RTYPE;
510 dr->bRequest = MCS_RDREQ;
511 dr->wValue = cpu_to_le16(Wval); /* 0 */
512 dr->wIndex = cpu_to_le16(reg);
513 dr->wLength = cpu_to_le16(2);
515 usb_fill_control_urb(mcs->control_urb, dev, usb_rcvctrlpipe(dev, 0),
516 (unsigned char *)dr, buffer, 2,
517 mos7840_control_callback, mcs);
518 mcs->control_urb->transfer_buffer_length = 2;
519 ret = usb_submit_urb(mcs->control_urb, GFP_ATOMIC);
523 /*****************************************************************************
524 * mos7840_interrupt_callback
525 * this is the callback function for when we have received data on the
526 * interrupt endpoint.
527 *****************************************************************************/
529 static void mos7840_interrupt_callback(struct urb *urb)
533 struct moschip_port *mos7840_port;
534 struct usb_serial *serial;
539 __u16 wval, wreg = 0;
540 int status = urb->status;
542 dbg("%s", " : Entering");
551 /* this urb is terminated, clean up */
552 dbg("%s - urb shutting down with status: %d", __func__,
556 dbg("%s - nonzero urb status received: %d", __func__,
561 length = urb->actual_length;
562 data = urb->transfer_buffer;
564 serial = urb->context;
566 /* Moschip get 5 bytes
567 * Byte 1 IIR Port 1 (port.number is 0)
568 * Byte 2 IIR Port 2 (port.number is 1)
569 * Byte 3 IIR Port 3 (port.number is 2)
570 * Byte 4 IIR Port 4 (port.number is 3)
571 * Byte 5 FIFO status for both */
573 if (length && length > 5) {
574 dbg("%s", "Wrong data !!!");
578 sp[0] = (__u8) data[0];
579 sp[1] = (__u8) data[1];
580 sp[2] = (__u8) data[2];
581 sp[3] = (__u8) data[3];
584 for (i = 0; i < serial->num_ports; i++) {
585 mos7840_port = mos7840_get_port_private(serial->port[i]);
587 (((__u16) serial->port[i]->number -
588 (__u16) (serial->minor)) + 1) << 8;
589 if (mos7840_port->open) {
591 dbg("SP%d No Interrupt !!!", i);
593 switch (sp[i] & 0x0f) {
595 dbg("Serial Port %d: Receiver status error or ", i);
596 dbg("address bit detected in 9-bit mode");
597 mos7840_port->MsrLsr = 1;
598 wreg = LINE_STATUS_REGISTER;
601 dbg("Serial Port %d: Modem status change", i);
602 mos7840_port->MsrLsr = 0;
603 wreg = MODEM_STATUS_REGISTER;
606 spin_lock(&mos7840_port->pool_lock);
607 if (!mos7840_port->zombie) {
608 rv = mos7840_get_reg(mos7840_port, wval, wreg, &Data);
610 spin_unlock(&mos7840_port->pool_lock);
613 spin_unlock(&mos7840_port->pool_lock);
618 /* the completion handler for the control urb will resubmit */
621 result = usb_submit_urb(urb, GFP_ATOMIC);
623 dev_err(&urb->dev->dev,
624 "%s - Error %d submitting interrupt urb\n",
629 static int mos7840_port_paranoia_check(struct usb_serial_port *port,
630 const char *function)
633 dbg("%s - port == NULL", function);
637 dbg("%s - port->serial == NULL", function);
644 /* Inline functions to check the sanity of a pointer that is passed to us */
645 static int mos7840_serial_paranoia_check(struct usb_serial *serial,
646 const char *function)
649 dbg("%s - serial == NULL", function);
653 dbg("%s - serial->type == NULL!", function);
660 static struct usb_serial *mos7840_get_usb_serial(struct usb_serial_port *port,
661 const char *function)
663 /* if no port was specified, or it fails a paranoia check */
665 mos7840_port_paranoia_check(port, function) ||
666 mos7840_serial_paranoia_check(port->serial, function)) {
667 /* then say that we don't have a valid usb_serial thing,
668 * which will end up genrating -ENODEV return values */
675 /*****************************************************************************
676 * mos7840_bulk_in_callback
677 * this is the callback function for when we have received data on the
679 *****************************************************************************/
681 static void mos7840_bulk_in_callback(struct urb *urb)
685 struct usb_serial *serial;
686 struct usb_serial_port *port;
687 struct moschip_port *mos7840_port;
688 struct tty_struct *tty;
689 int status = urb->status;
691 mos7840_port = urb->context;
693 dbg("%s", "NULL mos7840_port pointer");
694 mos7840_port->read_urb_busy = false;
699 dbg("nonzero read bulk status received: %d", status);
700 mos7840_port->read_urb_busy = false;
704 port = (struct usb_serial_port *)mos7840_port->port;
705 if (mos7840_port_paranoia_check(port, __func__)) {
706 dbg("%s", "Port Paranoia failed");
707 mos7840_port->read_urb_busy = false;
711 serial = mos7840_get_usb_serial(port, __func__);
713 dbg("%s", "Bad serial pointer");
714 mos7840_port->read_urb_busy = false;
718 dbg("%s", "Entering... ");
720 data = urb->transfer_buffer;
722 dbg("%s", "Entering ...........");
724 if (urb->actual_length) {
725 tty = tty_port_tty_get(&mos7840_port->port->port);
727 tty_buffer_request_room(tty, urb->actual_length);
728 tty_insert_flip_string(tty, data, urb->actual_length);
730 tty_flip_buffer_push(tty);
733 mos7840_port->icount.rx += urb->actual_length;
735 dbg("mos7840_port->icount.rx is %d:",
736 mos7840_port->icount.rx);
739 if (!mos7840_port->read_urb) {
740 dbg("%s", "URB KILLED !!!");
741 mos7840_port->read_urb_busy = false;
746 mos7840_port->read_urb->dev = serial->dev;
748 mos7840_port->read_urb_busy = true;
749 retval = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
752 dbg("usb_submit_urb(read bulk) failed, retval = %d", retval);
753 mos7840_port->read_urb_busy = false;
757 /*****************************************************************************
758 * mos7840_bulk_out_data_callback
759 * this is the callback function for when we have finished sending
760 * serial data on the bulk out endpoint.
761 *****************************************************************************/
763 static void mos7840_bulk_out_data_callback(struct urb *urb)
765 struct moschip_port *mos7840_port;
766 struct tty_struct *tty;
767 int status = urb->status;
770 mos7840_port = urb->context;
771 spin_lock(&mos7840_port->pool_lock);
772 for (i = 0; i < NUM_URBS; i++) {
773 if (urb == mos7840_port->write_urb_pool[i]) {
774 mos7840_port->busy[i] = 0;
778 spin_unlock(&mos7840_port->pool_lock);
781 dbg("nonzero write bulk status received:%d", status);
785 if (mos7840_port_paranoia_check(mos7840_port->port, __func__)) {
786 dbg("%s", "Port Paranoia failed");
790 dbg("%s", "Entering .........");
792 tty = tty_port_tty_get(&mos7840_port->port->port);
793 if (tty && mos7840_port->open)
799 /************************************************************************/
800 /* 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 */
801 /************************************************************************/
802 #ifdef MCSSerialProbe
803 static int mos7840_serial_probe(struct usb_serial *serial,
804 const struct usb_device_id *id)
807 /*need to implement the mode_reg reading and updating\
808 structures usb_serial_ device_type\
809 (i.e num_ports, num_bulkin,bulkout etc) */
810 /* Also we can update the changes attach */
815 /*****************************************************************************
817 * this function is called by the tty driver when a port is opened
818 * If successful, we return 0
819 * Otherwise we return a negative error number.
820 *****************************************************************************/
822 static int mos7840_open(struct tty_struct *tty,
823 struct usb_serial_port *port, struct file *filp)
827 struct usb_serial *serial;
831 struct moschip_port *mos7840_port;
832 struct moschip_port *port0;
834 dbg ("%s enter", __func__);
836 if (mos7840_port_paranoia_check(port, __func__)) {
837 dbg("%s", "Port Paranoia failed");
841 serial = port->serial;
843 if (mos7840_serial_paranoia_check(serial, __func__)) {
844 dbg("%s", "Serial Paranoia failed");
848 mos7840_port = mos7840_get_port_private(port);
849 port0 = mos7840_get_port_private(serial->port[0]);
851 if (mos7840_port == NULL || port0 == NULL)
854 usb_clear_halt(serial->dev, port->write_urb->pipe);
855 usb_clear_halt(serial->dev, port->read_urb->pipe);
858 /* Initialising the write urb pool */
859 for (j = 0; j < NUM_URBS; ++j) {
860 urb = usb_alloc_urb(0, GFP_KERNEL);
861 mos7840_port->write_urb_pool[j] = urb;
864 dev_err(&port->dev, "No more urbs???\n");
868 urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
870 if (!urb->transfer_buffer) {
872 mos7840_port->write_urb_pool[j] = NULL;
874 "%s-out of memory for urb buffers.\n",
880 /*****************************************************************************
881 * Initialize MCS7840 -- Write Init values to corresponding Registers
889 * 0x08 : SP1/2 Control Reg
890 *****************************************************************************/
892 /* NEED to check the following Block */
895 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
897 dbg("Reading Spreg failed");
901 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
903 dbg("writing Spreg failed");
908 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
910 dbg("writing Spreg failed");
913 /* End of block to be checked */
916 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
919 dbg("Reading Controlreg failed");
922 Data |= 0x08; /* Driver done bit */
923 Data |= 0x20; /* rx_disable */
924 status = mos7840_set_reg_sync(port,
925 mos7840_port->ControlRegOffset, Data);
927 dbg("writing Controlreg failed");
930 /* do register settings here */
931 /* Set all regs to the device default values. */
932 /***********************************
933 * First Disable all interrupts.
934 ***********************************/
936 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
938 dbg("disabling interrupts failed");
941 /* Set FIFO_CONTROL_REGISTER to the default value */
943 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
945 dbg("Writing FIFO_CONTROL_REGISTER failed");
950 status = mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
952 dbg("Writing FIFO_CONTROL_REGISTER failed");
957 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
958 mos7840_port->shadowLCR = Data;
961 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
962 mos7840_port->shadowMCR = Data;
965 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
966 mos7840_port->shadowLCR = Data;
968 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
969 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
972 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
975 status = mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
978 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
980 Data = Data & ~SERIAL_LCR_DLAB;
981 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
982 mos7840_port->shadowLCR = Data;
984 /* clearing Bulkin and Bulkout Fifo */
986 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset, &Data);
989 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
992 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset, Data);
993 /* Finally enable all interrupts */
995 status = mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
997 /* clearing rx_disable */
999 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1001 Data = Data & ~0x20;
1002 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1007 status = mos7840_get_reg_sync(port, mos7840_port->ControlRegOffset,
1010 status = mos7840_set_reg_sync(port, mos7840_port->ControlRegOffset,
1013 /* Check to see if we've set up our endpoint info yet *
1014 * (can't set it up in mos7840_startup as the structures *
1015 * were not set up at that time.) */
1016 if (port0->open_ports == 1) {
1017 if (serial->port[0]->interrupt_in_buffer == NULL) {
1018 /* set up interrupt urb */
1019 usb_fill_int_urb(serial->port[0]->interrupt_in_urb,
1021 usb_rcvintpipe(serial->dev,
1022 serial->port[0]->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->interval);
1030 /* start interrupt read for mos7840 *
1031 * will continue as long as mos7840 is connected */
1034 usb_submit_urb(serial->port[0]->interrupt_in_urb,
1037 dev_err(&port->dev, "%s - Error %d submitting "
1038 "interrupt urb\n", __func__, response);
1045 /* see if we've set up our endpoint info yet *
1046 * (can't set it up in mos7840_startup as the *
1047 * structures were not set up at that time.) */
1049 dbg("port number is %d", port->number);
1050 dbg("serial number is %d", port->serial->minor);
1051 dbg("Bulkin endpoint is %d", port->bulk_in_endpointAddress);
1052 dbg("BulkOut endpoint is %d", port->bulk_out_endpointAddress);
1053 dbg("Interrupt endpoint is %d", port->interrupt_in_endpointAddress);
1054 dbg("port's number in the device is %d", mos7840_port->port_num);
1055 mos7840_port->read_urb = port->read_urb;
1057 /* set up our bulk in urb */
1059 usb_fill_bulk_urb(mos7840_port->read_urb,
1061 usb_rcvbulkpipe(serial->dev,
1062 port->bulk_in_endpointAddress),
1063 port->bulk_in_buffer,
1064 mos7840_port->read_urb->transfer_buffer_length,
1065 mos7840_bulk_in_callback, mos7840_port);
1067 dbg("mos7840_open: bulkin endpoint is %d",
1068 port->bulk_in_endpointAddress);
1069 mos7840_port->read_urb_busy = true;
1070 response = usb_submit_urb(mos7840_port->read_urb, GFP_KERNEL);
1072 dev_err(&port->dev, "%s - Error %d submitting control urb\n",
1073 __func__, response);
1074 mos7840_port->read_urb_busy = false;
1077 /* initialize our wait queues */
1078 init_waitqueue_head(&mos7840_port->wait_chase);
1079 init_waitqueue_head(&mos7840_port->delta_msr_wait);
1081 /* initialize our icount structure */
1082 memset(&(mos7840_port->icount), 0x00, sizeof(mos7840_port->icount));
1084 /* initialize our port settings */
1085 /* Must set to enable ints! */
1086 mos7840_port->shadowMCR = MCR_MASTER_IE;
1087 /* send a open port command */
1088 mos7840_port->open = 1;
1089 /* mos7840_change_port_settings(mos7840_port,old_termios); */
1090 mos7840_port->icount.tx = 0;
1091 mos7840_port->icount.rx = 0;
1093 dbg("usb_serial serial:%p mos7840_port:%p\n usb_serial_port port:%p",
1094 serial, mos7840_port, port);
1096 dbg ("%s leave", __func__);
1102 /*****************************************************************************
1103 * mos7840_chars_in_buffer
1104 * this function is called by the tty driver when it wants to know how many
1105 * bytes of data we currently have outstanding in the port (data that has
1106 * been written, but hasn't made it out the port yet)
1107 * If successful, we return the number of bytes left to be written in the
1109 * Otherwise we return zero.
1110 *****************************************************************************/
1112 static int mos7840_chars_in_buffer(struct tty_struct *tty)
1114 struct usb_serial_port *port = tty->driver_data;
1117 unsigned long flags;
1118 struct moschip_port *mos7840_port;
1120 dbg("%s", " mos7840_chars_in_buffer:entering ...........");
1122 if (mos7840_port_paranoia_check(port, __func__)) {
1123 dbg("%s", "Invalid port");
1127 mos7840_port = mos7840_get_port_private(port);
1128 if (mos7840_port == NULL) {
1129 dbg("%s", "mos7840_break:leaving ...........");
1133 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1134 for (i = 0; i < NUM_URBS; ++i)
1135 if (mos7840_port->busy[i])
1136 chars += URB_TRANSFER_BUFFER_SIZE;
1137 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1138 dbg("%s - returns %d", __func__, chars);
1143 /*****************************************************************************
1145 * this function is called by the tty driver when a port is closed
1146 *****************************************************************************/
1148 static void mos7840_close(struct usb_serial_port *port)
1150 struct usb_serial *serial;
1151 struct moschip_port *mos7840_port;
1152 struct moschip_port *port0;
1156 dbg("%s", "mos7840_close:entering...");
1158 if (mos7840_port_paranoia_check(port, __func__)) {
1159 dbg("%s", "Port Paranoia failed");
1163 serial = mos7840_get_usb_serial(port, __func__);
1165 dbg("%s", "Serial Paranoia failed");
1169 mos7840_port = mos7840_get_port_private(port);
1170 port0 = mos7840_get_port_private(serial->port[0]);
1172 if (mos7840_port == NULL || port0 == NULL)
1175 for (j = 0; j < NUM_URBS; ++j)
1176 usb_kill_urb(mos7840_port->write_urb_pool[j]);
1178 /* Freeing Write URBs */
1179 for (j = 0; j < NUM_URBS; ++j) {
1180 if (mos7840_port->write_urb_pool[j]) {
1181 if (mos7840_port->write_urb_pool[j]->transfer_buffer)
1182 kfree(mos7840_port->write_urb_pool[j]->
1185 usb_free_urb(mos7840_port->write_urb_pool[j]);
1189 /* While closing port, shutdown all bulk read, write *
1190 * and interrupt read if they exists */
1192 if (mos7840_port->write_urb) {
1193 dbg("%s", "Shutdown bulk write");
1194 usb_kill_urb(mos7840_port->write_urb);
1196 if (mos7840_port->read_urb) {
1197 dbg("%s", "Shutdown bulk read");
1198 usb_kill_urb(mos7840_port->read_urb);
1199 mos7840_port->read_urb_busy = false;
1201 if ((&mos7840_port->control_urb)) {
1202 dbg("%s", "Shutdown control read");
1203 /*/ usb_kill_urb (mos7840_port->control_urb); */
1206 /* if(mos7840_port->ctrl_buf != NULL) */
1207 /* kfree(mos7840_port->ctrl_buf); */
1208 port0->open_ports--;
1209 dbg("mos7840_num_open_ports in close%d:in port%d",
1210 port0->open_ports, port->number);
1211 if (port0->open_ports == 0) {
1212 if (serial->port[0]->interrupt_in_urb) {
1213 dbg("%s", "Shutdown interrupt_in_urb");
1214 usb_kill_urb(serial->port[0]->interrupt_in_urb);
1218 if (mos7840_port->write_urb) {
1219 /* if this urb had a transfer buffer already (old tx) free it */
1220 if (mos7840_port->write_urb->transfer_buffer != NULL)
1221 kfree(mos7840_port->write_urb->transfer_buffer);
1222 usb_free_urb(mos7840_port->write_urb);
1226 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1229 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1231 mos7840_port->open = 0;
1233 dbg("%s", "Leaving ............");
1236 /************************************************************************
1238 * mos7840_block_until_chase_response
1240 * This function will block the close until one of the following:
1241 * 1. Response to our Chase comes from mos7840
1242 * 2. A timeout of 10 seconds without activity has expired
1243 * (1K of mos7840 data @ 2400 baud ==> 4 sec to empty)
1245 ************************************************************************/
1247 static void mos7840_block_until_chase_response(struct tty_struct *tty,
1248 struct moschip_port *mos7840_port)
1250 int timeout = 1 * HZ;
1255 count = mos7840_chars_in_buffer(tty);
1257 /* Check for Buffer status */
1261 /* Block the thread for a while */
1262 interruptible_sleep_on_timeout(&mos7840_port->wait_chase,
1264 /* No activity.. count down section */
1267 dbg("%s - TIMEOUT", __func__);
1270 /* Reset timeout value back to seconds */
1277 /*****************************************************************************
1279 * this function sends a break to the port
1280 *****************************************************************************/
1281 static void mos7840_break(struct tty_struct *tty, int break_state)
1283 struct usb_serial_port *port = tty->driver_data;
1285 struct usb_serial *serial;
1286 struct moschip_port *mos7840_port;
1288 dbg("%s", "Entering ...........");
1289 dbg("mos7840_break: Start");
1291 if (mos7840_port_paranoia_check(port, __func__)) {
1292 dbg("%s", "Port Paranoia failed");
1296 serial = mos7840_get_usb_serial(port, __func__);
1298 dbg("%s", "Serial Paranoia failed");
1302 mos7840_port = mos7840_get_port_private(port);
1304 if (mos7840_port == NULL)
1308 /* flush and block until tx is empty */
1309 mos7840_block_until_chase_response(tty, mos7840_port);
1311 if (break_state == -1)
1312 data = mos7840_port->shadowLCR | LCR_SET_BREAK;
1314 data = mos7840_port->shadowLCR & ~LCR_SET_BREAK;
1316 /* FIXME: no locking on shadowLCR anywhere in driver */
1317 mos7840_port->shadowLCR = data;
1318 dbg("mcs7840_break mos7840_port->shadowLCR is %x",
1319 mos7840_port->shadowLCR);
1320 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER,
1321 mos7840_port->shadowLCR);
1326 /*****************************************************************************
1327 * mos7840_write_room
1328 * this function is called by the tty driver when it wants to know how many
1329 * bytes of data we can accept for a specific port.
1330 * If successful, we return the amount of room that we have for this port
1331 * Otherwise we return a negative error number.
1332 *****************************************************************************/
1334 static int mos7840_write_room(struct tty_struct *tty)
1336 struct usb_serial_port *port = tty->driver_data;
1339 unsigned long flags;
1340 struct moschip_port *mos7840_port;
1342 dbg("%s", " mos7840_write_room:entering ...........");
1344 if (mos7840_port_paranoia_check(port, __func__)) {
1345 dbg("%s", "Invalid port");
1346 dbg("%s", " mos7840_write_room:leaving ...........");
1350 mos7840_port = mos7840_get_port_private(port);
1351 if (mos7840_port == NULL) {
1352 dbg("%s", "mos7840_break:leaving ...........");
1356 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1357 for (i = 0; i < NUM_URBS; ++i) {
1358 if (!mos7840_port->busy[i])
1359 room += URB_TRANSFER_BUFFER_SIZE;
1361 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1363 room = (room == 0) ? 0 : room - URB_TRANSFER_BUFFER_SIZE + 1;
1364 dbg("%s - returns %d", __func__, room);
1369 /*****************************************************************************
1371 * this function is called by the tty driver when data should be written to
1373 * If successful, we return the number of bytes written, otherwise we
1374 * return a negative error number.
1375 *****************************************************************************/
1377 static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
1378 const unsigned char *data, int count)
1384 unsigned long flags;
1386 struct moschip_port *mos7840_port;
1387 struct usb_serial *serial;
1390 const unsigned char *current_position = data;
1391 unsigned char *data1;
1392 dbg("%s", "entering ...........");
1393 /* dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1394 mos7840_port->shadowLCR); */
1398 status = mos7840_get_uart_reg(port, LINE_CONTROL_REGISTER, &Data);
1399 mos7840_port->shadowLCR = Data;
1400 dbg("mos7840_write: LINE_CONTROL_REGISTER is %x", Data);
1401 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1402 mos7840_port->shadowLCR);
1405 /* status = mos7840_set_uart_reg(port,LINE_CONTROL_REGISTER,Data); */
1406 /* mos7840_port->shadowLCR=Data;//Need to add later */
1408 Data |= SERIAL_LCR_DLAB; /* data latch enable in LCR 0x80 */
1409 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1412 /* status = mos7840_set_uart_reg(port,DIVISOR_LATCH_LSB,Data); */
1414 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_LSB, &Data);
1415 dbg("mos7840_write:DLL value is %x", Data);
1418 status = mos7840_get_uart_reg(port, DIVISOR_LATCH_MSB, &Data);
1419 dbg("mos7840_write:DLM value is %x", Data);
1421 Data = Data & ~SERIAL_LCR_DLAB;
1422 dbg("mos7840_write: mos7840_port->shadowLCR is %x",
1423 mos7840_port->shadowLCR);
1424 status = mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1427 if (mos7840_port_paranoia_check(port, __func__)) {
1428 dbg("%s", "Port Paranoia failed");
1432 serial = port->serial;
1433 if (mos7840_serial_paranoia_check(serial, __func__)) {
1434 dbg("%s", "Serial Paranoia failed");
1438 mos7840_port = mos7840_get_port_private(port);
1439 if (mos7840_port == NULL) {
1440 dbg("%s", "mos7840_port is NULL");
1444 /* try to find a free urb in the list */
1447 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
1448 for (i = 0; i < NUM_URBS; ++i) {
1449 if (!mos7840_port->busy[i]) {
1450 mos7840_port->busy[i] = 1;
1451 urb = mos7840_port->write_urb_pool[i];
1456 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
1459 dbg("%s - no more free urbs", __func__);
1463 if (urb->transfer_buffer == NULL) {
1464 urb->transfer_buffer =
1465 kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_KERNEL);
1467 if (urb->transfer_buffer == NULL) {
1468 dev_err(&port->dev, "%s no more kernel memory...\n",
1473 transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
1475 memcpy(urb->transfer_buffer, current_position, transfer_size);
1477 /* fill urb with data and submit */
1478 usb_fill_bulk_urb(urb,
1480 usb_sndbulkpipe(serial->dev,
1481 port->bulk_out_endpointAddress),
1482 urb->transfer_buffer,
1484 mos7840_bulk_out_data_callback, mos7840_port);
1486 data1 = urb->transfer_buffer;
1487 dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
1489 /* send it down the pipe */
1490 status = usb_submit_urb(urb, GFP_ATOMIC);
1493 mos7840_port->busy[i] = 0;
1494 dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
1495 "with status = %d\n", __func__, status);
1496 bytes_sent = status;
1499 bytes_sent = transfer_size;
1500 mos7840_port->icount.tx += transfer_size;
1502 dbg("mos7840_port->icount.tx is %d:", mos7840_port->icount.tx);
1508 /*****************************************************************************
1510 * this function is called by the tty driver when it wants to stop the data
1511 * being read from the port.
1512 *****************************************************************************/
1514 static void mos7840_throttle(struct tty_struct *tty)
1516 struct usb_serial_port *port = tty->driver_data;
1517 struct moschip_port *mos7840_port;
1520 if (mos7840_port_paranoia_check(port, __func__)) {
1521 dbg("%s", "Invalid port");
1525 dbg("- port %d", port->number);
1527 mos7840_port = mos7840_get_port_private(port);
1529 if (mos7840_port == NULL)
1532 if (!mos7840_port->open) {
1533 dbg("%s", "port not opened");
1537 dbg("%s", "Entering ..........");
1539 /* if we are implementing XON/XOFF, send the stop character */
1541 unsigned char stop_char = STOP_CHAR(tty);
1542 status = mos7840_write(tty, port, &stop_char, 1);
1546 /* if we are implementing RTS/CTS, toggle that line */
1547 if (tty->termios->c_cflag & CRTSCTS) {
1548 mos7840_port->shadowMCR &= ~MCR_RTS;
1549 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1550 mos7840_port->shadowMCR);
1558 /*****************************************************************************
1559 * mos7840_unthrottle
1560 * this function is called by the tty driver when it wants to resume
1561 * the data being read from the port (called after mos7840_throttle is
1563 *****************************************************************************/
1564 static void mos7840_unthrottle(struct tty_struct *tty)
1566 struct usb_serial_port *port = tty->driver_data;
1568 struct moschip_port *mos7840_port = mos7840_get_port_private(port);
1570 if (mos7840_port_paranoia_check(port, __func__)) {
1571 dbg("%s", "Invalid port");
1575 if (mos7840_port == NULL)
1578 if (!mos7840_port->open) {
1579 dbg("%s - port not opened", __func__);
1583 dbg("%s", "Entering ..........");
1585 /* if we are implementing XON/XOFF, send the start character */
1587 unsigned char start_char = START_CHAR(tty);
1588 status = mos7840_write(tty, port, &start_char, 1);
1593 /* if we are implementing RTS/CTS, toggle that line */
1594 if (tty->termios->c_cflag & CRTSCTS) {
1595 mos7840_port->shadowMCR |= MCR_RTS;
1596 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1597 mos7840_port->shadowMCR);
1603 static int mos7840_tiocmget(struct tty_struct *tty, struct file *file)
1605 struct usb_serial_port *port = tty->driver_data;
1606 struct moschip_port *mos7840_port;
1607 unsigned int result;
1611 mos7840_port = mos7840_get_port_private(port);
1613 dbg("%s - port %d", __func__, port->number);
1615 if (mos7840_port == NULL)
1618 status = mos7840_get_uart_reg(port, MODEM_STATUS_REGISTER, &msr);
1619 status = mos7840_get_uart_reg(port, MODEM_CONTROL_REGISTER, &mcr);
1620 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
1621 | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
1622 | ((mcr & MCR_LOOPBACK) ? TIOCM_LOOP : 0)
1623 | ((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0)
1624 | ((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0)
1625 | ((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0)
1626 | ((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0);
1628 dbg("%s - 0x%04X", __func__, result);
1633 static int mos7840_tiocmset(struct tty_struct *tty, struct file *file,
1634 unsigned int set, unsigned int clear)
1636 struct usb_serial_port *port = tty->driver_data;
1637 struct moschip_port *mos7840_port;
1641 dbg("%s - port %d", __func__, port->number);
1643 mos7840_port = mos7840_get_port_private(port);
1645 if (mos7840_port == NULL)
1648 /* FIXME: What locks the port registers ? */
1649 mcr = mos7840_port->shadowMCR;
1650 if (clear & TIOCM_RTS)
1652 if (clear & TIOCM_DTR)
1654 if (clear & TIOCM_LOOP)
1655 mcr &= ~MCR_LOOPBACK;
1657 if (set & TIOCM_RTS)
1659 if (set & TIOCM_DTR)
1661 if (set & TIOCM_LOOP)
1662 mcr |= MCR_LOOPBACK;
1664 mos7840_port->shadowMCR = mcr;
1666 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, mcr);
1668 dbg("setting MODEM_CONTROL_REGISTER Failed");
1675 /*****************************************************************************
1676 * mos7840_calc_baud_rate_divisor
1677 * this function calculates the proper baud rate divisor for the specified
1679 *****************************************************************************/
1680 static int mos7840_calc_baud_rate_divisor(int baudRate, int *divisor,
1684 dbg("%s - %d", __func__, baudRate);
1686 if (baudRate <= 115200) {
1687 *divisor = 115200 / baudRate;
1690 if ((baudRate > 115200) && (baudRate <= 230400)) {
1691 *divisor = 230400 / baudRate;
1692 *clk_sel_val = 0x10;
1693 } else if ((baudRate > 230400) && (baudRate <= 403200)) {
1694 *divisor = 403200 / baudRate;
1695 *clk_sel_val = 0x20;
1696 } else if ((baudRate > 403200) && (baudRate <= 460800)) {
1697 *divisor = 460800 / baudRate;
1698 *clk_sel_val = 0x30;
1699 } else if ((baudRate > 460800) && (baudRate <= 806400)) {
1700 *divisor = 806400 / baudRate;
1701 *clk_sel_val = 0x40;
1702 } else if ((baudRate > 806400) && (baudRate <= 921600)) {
1703 *divisor = 921600 / baudRate;
1704 *clk_sel_val = 0x50;
1705 } else if ((baudRate > 921600) && (baudRate <= 1572864)) {
1706 *divisor = 1572864 / baudRate;
1707 *clk_sel_val = 0x60;
1708 } else if ((baudRate > 1572864) && (baudRate <= 3145728)) {
1709 *divisor = 3145728 / baudRate;
1710 *clk_sel_val = 0x70;
1716 for (i = 0; i < ARRAY_SIZE(mos7840_divisor_table); i++) {
1717 if (mos7840_divisor_table[i].BaudRate == baudrate) {
1718 *divisor = mos7840_divisor_table[i].Divisor;
1723 /* After trying for all the standard baud rates *
1724 * Try calculating the divisor for this baud rate */
1726 if (baudrate > 75 && baudrate < 230400) {
1727 /* get the divisor */
1728 custom = (__u16) (230400L / baudrate);
1730 /* Check for round off */
1731 round1 = (__u16) (2304000L / baudrate);
1732 round = (__u16) (round1 - (custom * 10));
1737 dbg(" Baud %d = %d", baudrate, custom);
1741 dbg("%s", " Baud calculation Failed...");
1746 /*****************************************************************************
1747 * mos7840_send_cmd_write_baud_rate
1748 * this function sends the proper command to change the baud rate of the
1750 *****************************************************************************/
1752 static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
1758 unsigned char number;
1760 struct usb_serial_port *port;
1762 if (mos7840_port == NULL)
1765 port = (struct usb_serial_port *)mos7840_port->port;
1766 if (mos7840_port_paranoia_check(port, __func__)) {
1767 dbg("%s", "Invalid port");
1771 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1772 dbg("%s", "Invalid Serial");
1776 dbg("%s", "Entering ..........");
1778 number = mos7840_port->port->number - mos7840_port->port->serial->minor;
1780 dbg("%s - port = %d, baud = %d", __func__,
1781 mos7840_port->port->number, baudRate);
1782 /* reset clk_uart_sel in spregOffset */
1783 if (baudRate > 115200) {
1784 #ifdef HW_flow_control
1785 /* NOTE: need to see the pther register to modify */
1786 /* setting h/w flow control bit to 1 */
1788 mos7840_port->shadowMCR = Data;
1789 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1792 dbg("Writing spreg failed in set_serial_baud");
1798 #ifdef HW_flow_control
1799 / *setting h/w flow control bit to 0 */
1801 mos7840_port->shadowMCR = Data;
1802 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
1805 dbg("Writing spreg failed in set_serial_baud");
1812 if (1) { /* baudRate <= 115200) */
1815 status = mos7840_calc_baud_rate_divisor(baudRate, &divisor,
1817 status = mos7840_get_reg_sync(port, mos7840_port->SpRegOffset,
1820 dbg("reading spreg failed in set_serial_baud");
1823 Data = (Data & 0x8f) | clk_sel_val;
1824 status = mos7840_set_reg_sync(port, mos7840_port->SpRegOffset,
1827 dbg("Writing spreg failed in set_serial_baud");
1830 /* Calculate the Divisor */
1833 dev_err(&port->dev, "%s - bad baud rate\n", __func__);
1836 /* Enable access to divisor latch */
1837 Data = mos7840_port->shadowLCR | SERIAL_LCR_DLAB;
1838 mos7840_port->shadowLCR = Data;
1839 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1841 /* Write the divisor */
1842 Data = (unsigned char)(divisor & 0xff);
1843 dbg("set_serial_baud Value to write DLL is %x", Data);
1844 mos7840_set_uart_reg(port, DIVISOR_LATCH_LSB, Data);
1846 Data = (unsigned char)((divisor & 0xff00) >> 8);
1847 dbg("set_serial_baud Value to write DLM is %x", Data);
1848 mos7840_set_uart_reg(port, DIVISOR_LATCH_MSB, Data);
1850 /* Disable access to divisor latch */
1851 Data = mos7840_port->shadowLCR & ~SERIAL_LCR_DLAB;
1852 mos7840_port->shadowLCR = Data;
1853 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1859 /*****************************************************************************
1860 * mos7840_change_port_settings
1861 * This routine is called to set the UART on the device to match
1862 * the specified new settings.
1863 *****************************************************************************/
1865 static void mos7840_change_port_settings(struct tty_struct *tty,
1866 struct moschip_port *mos7840_port, struct ktermios *old_termios)
1876 struct usb_serial_port *port;
1877 struct usb_serial *serial;
1879 if (mos7840_port == NULL)
1882 port = (struct usb_serial_port *)mos7840_port->port;
1884 if (mos7840_port_paranoia_check(port, __func__)) {
1885 dbg("%s", "Invalid port");
1889 if (mos7840_serial_paranoia_check(port->serial, __func__)) {
1890 dbg("%s", "Invalid Serial");
1894 serial = port->serial;
1896 dbg("%s - port %d", __func__, mos7840_port->port->number);
1898 if (!mos7840_port->open) {
1899 dbg("%s - port not opened", __func__);
1903 dbg("%s", "Entering ..........");
1907 lParity = LCR_PAR_NONE;
1909 cflag = tty->termios->c_cflag;
1910 iflag = tty->termios->c_iflag;
1912 /* Change the number of bits */
1913 if (cflag & CSIZE) {
1914 switch (cflag & CSIZE) {
1932 /* Change the Parity bit */
1933 if (cflag & PARENB) {
1934 if (cflag & PARODD) {
1935 lParity = LCR_PAR_ODD;
1936 dbg("%s - parity = odd", __func__);
1938 lParity = LCR_PAR_EVEN;
1939 dbg("%s - parity = even", __func__);
1943 dbg("%s - parity = none", __func__);
1947 lParity = lParity | 0x20;
1949 /* Change the Stop bit */
1950 if (cflag & CSTOPB) {
1952 dbg("%s - stop bits = 2", __func__);
1955 dbg("%s - stop bits = 1", __func__);
1958 /* Update the LCR with the correct value */
1959 mos7840_port->shadowLCR &=
1960 ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
1961 mos7840_port->shadowLCR |= (lData | lParity | lStop);
1963 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is %x",
1964 mos7840_port->shadowLCR);
1965 /* Disable Interrupts */
1967 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
1970 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1973 mos7840_set_uart_reg(port, FIFO_CONTROL_REGISTER, Data);
1975 /* Send the updated LCR value to the mos7840 */
1976 Data = mos7840_port->shadowLCR;
1978 mos7840_set_uart_reg(port, LINE_CONTROL_REGISTER, Data);
1981 mos7840_port->shadowMCR = Data;
1982 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1984 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
1986 /* set up the MCR register and send it to the mos7840 */
1988 mos7840_port->shadowMCR = MCR_MASTER_IE;
1990 mos7840_port->shadowMCR |= (MCR_DTR | MCR_RTS);
1992 if (cflag & CRTSCTS)
1993 mos7840_port->shadowMCR |= (MCR_XON_ANY);
1995 mos7840_port->shadowMCR &= ~(MCR_XON_ANY);
1997 Data = mos7840_port->shadowMCR;
1998 mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2000 /* Determine divisor based on baud rate */
2001 baud = tty_get_baud_rate(tty);
2004 /* pick a default, any default... */
2005 dbg("%s", "Picked default baud...");
2009 dbg("%s - baud rate = %d", __func__, baud);
2010 status = mos7840_send_cmd_write_baud_rate(mos7840_port, baud);
2012 /* Enable Interrupts */
2014 mos7840_set_uart_reg(port, INTERRUPT_ENABLE_REGISTER, Data);
2016 if (mos7840_port->read_urb_busy == false) {
2017 mos7840_port->read_urb->dev = serial->dev;
2018 mos7840_port->read_urb_busy = true;
2019 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2021 dbg("usb_submit_urb(read bulk) failed, status = %d",
2023 mos7840_port->read_urb_busy = false;
2026 wake_up(&mos7840_port->delta_msr_wait);
2027 mos7840_port->delta_msr_cond = 1;
2028 dbg("mos7840_change_port_settings mos7840_port->shadowLCR is End %x",
2029 mos7840_port->shadowLCR);
2034 /*****************************************************************************
2035 * mos7840_set_termios
2036 * this function is called by the tty driver when it wants to change
2037 * the termios structure
2038 *****************************************************************************/
2040 static void mos7840_set_termios(struct tty_struct *tty,
2041 struct usb_serial_port *port,
2042 struct ktermios *old_termios)
2046 struct usb_serial *serial;
2047 struct moschip_port *mos7840_port;
2048 dbg("mos7840_set_termios: START");
2049 if (mos7840_port_paranoia_check(port, __func__)) {
2050 dbg("%s", "Invalid port");
2054 serial = port->serial;
2056 if (mos7840_serial_paranoia_check(serial, __func__)) {
2057 dbg("%s", "Invalid Serial");
2061 mos7840_port = mos7840_get_port_private(port);
2063 if (mos7840_port == NULL)
2066 if (!mos7840_port->open) {
2067 dbg("%s - port not opened", __func__);
2071 dbg("%s", "setting termios - ");
2073 cflag = tty->termios->c_cflag;
2075 dbg("%s - clfag %08x iflag %08x", __func__,
2076 tty->termios->c_cflag, RELEVANT_IFLAG(tty->termios->c_iflag));
2077 dbg("%s - old clfag %08x old iflag %08x", __func__,
2078 old_termios->c_cflag, RELEVANT_IFLAG(old_termios->c_iflag));
2079 dbg("%s - port %d", __func__, port->number);
2081 /* change the port settings to the new ones specified */
2083 mos7840_change_port_settings(tty, mos7840_port, old_termios);
2085 if (!mos7840_port->read_urb) {
2086 dbg("%s", "URB KILLED !!!!!");
2090 if (mos7840_port->read_urb_busy == false) {
2091 mos7840_port->read_urb->dev = serial->dev;
2092 mos7840_port->read_urb_busy = true;
2093 status = usb_submit_urb(mos7840_port->read_urb, GFP_ATOMIC);
2095 dbg("usb_submit_urb(read bulk) failed, status = %d",
2097 mos7840_port->read_urb_busy = false;
2103 /*****************************************************************************
2104 * mos7840_get_lsr_info - get line status register info
2106 * Purpose: Let user call ioctl() to get info when the UART physically
2107 * is emptied. On bus types like RS485, the transmitter must
2108 * release the bus after transmitting. This must be done when
2109 * the transmit shift register is empty, not be done when the
2110 * transmit holding register is empty. This functionality
2111 * allows an RS485 driver to be written in user space.
2112 *****************************************************************************/
2114 static int mos7840_get_lsr_info(struct tty_struct *tty,
2115 unsigned int __user *value)
2118 unsigned int result = 0;
2120 count = mos7840_chars_in_buffer(tty);
2122 dbg("%s -- Empty", __func__);
2123 result = TIOCSER_TEMT;
2126 if (copy_to_user(value, &result, sizeof(int)))
2131 /*****************************************************************************
2132 * mos7840_set_modem_info
2133 * function to set modem info
2134 *****************************************************************************/
2136 /* FIXME: Should be using the model control hooks */
2138 static int mos7840_set_modem_info(struct moschip_port *mos7840_port,
2139 unsigned int cmd, unsigned int __user *value)
2145 struct usb_serial_port *port;
2147 if (mos7840_port == NULL)
2150 port = (struct usb_serial_port *)mos7840_port->port;
2151 if (mos7840_port_paranoia_check(port, __func__)) {
2152 dbg("%s", "Invalid port");
2156 mcr = mos7840_port->shadowMCR;
2158 if (copy_from_user(&arg, value, sizeof(int)))
2163 if (arg & TIOCM_RTS)
2165 if (arg & TIOCM_DTR)
2167 if (arg & TIOCM_LOOP)
2168 mcr |= MCR_LOOPBACK;
2172 if (arg & TIOCM_RTS)
2174 if (arg & TIOCM_DTR)
2176 if (arg & TIOCM_LOOP)
2177 mcr &= ~MCR_LOOPBACK;
2181 /* turn off the RTS and DTR and LOOPBACK
2182 * and then only turn on what was asked to */
2183 mcr &= ~(MCR_RTS | MCR_DTR | MCR_LOOPBACK);
2184 mcr |= ((arg & TIOCM_RTS) ? MCR_RTS : 0);
2185 mcr |= ((arg & TIOCM_DTR) ? MCR_DTR : 0);
2186 mcr |= ((arg & TIOCM_LOOP) ? MCR_LOOPBACK : 0);
2191 mos7840_port->shadowMCR = mcr;
2193 Data = mos7840_port->shadowMCR;
2194 status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER, Data);
2197 dbg("setting MODEM_CONTROL_REGISTER Failed");
2204 /*****************************************************************************
2205 * mos7840_get_modem_info
2206 * function to get modem info
2207 *****************************************************************************/
2209 static int mos7840_get_modem_info(struct moschip_port *mos7840_port,
2210 unsigned int __user *value)
2212 unsigned int result = 0;
2214 unsigned int mcr = mos7840_port->shadowMCR;
2215 mos7840_get_uart_reg(mos7840_port->port,
2216 MODEM_STATUS_REGISTER, &msr);
2217 result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
2218 |((mcr & MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
2219 |((msr & MOS7840_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
2220 |((msr & MOS7840_MSR_CD) ? TIOCM_CAR : 0) /* 0x040 */
2221 |((msr & MOS7840_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
2222 |((msr & MOS7840_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
2224 dbg("%s -- %x", __func__, result);
2226 if (copy_to_user(value, &result, sizeof(int)))
2231 /*****************************************************************************
2232 * mos7840_get_serial_info
2233 * function to get information about serial port
2234 *****************************************************************************/
2236 static int mos7840_get_serial_info(struct moschip_port *mos7840_port,
2237 struct serial_struct __user *retinfo)
2239 struct serial_struct tmp;
2241 if (mos7840_port == NULL)
2247 memset(&tmp, 0, sizeof(tmp));
2249 tmp.type = PORT_16550A;
2250 tmp.line = mos7840_port->port->serial->minor;
2251 tmp.port = mos7840_port->port->number;
2253 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
2254 tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
2255 tmp.baud_base = 9600;
2256 tmp.close_delay = 5 * HZ;
2257 tmp.closing_wait = 30 * HZ;
2259 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2264 /*****************************************************************************
2266 * this function handles any ioctl calls to the driver
2267 *****************************************************************************/
2269 static int mos7840_ioctl(struct tty_struct *tty, struct file *file,
2270 unsigned int cmd, unsigned long arg)
2272 struct usb_serial_port *port = tty->driver_data;
2273 void __user *argp = (void __user *)arg;
2274 struct moschip_port *mos7840_port;
2276 struct async_icount cnow;
2277 struct async_icount cprev;
2278 struct serial_icounter_struct icount;
2281 if (mos7840_port_paranoia_check(port, __func__)) {
2282 dbg("%s", "Invalid port");
2286 mos7840_port = mos7840_get_port_private(port);
2288 if (mos7840_port == NULL)
2291 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
2294 /* return number of bytes available */
2297 dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
2298 return mos7840_get_lsr_info(tty, argp);
2301 /* FIXME: use the modem hooks and remove this */
2305 dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET", __func__,
2308 mos7840_set_modem_info(mos7840_port, cmd, argp);
2312 dbg("%s (%d) TIOCMGET", __func__, port->number);
2313 return mos7840_get_modem_info(mos7840_port, argp);
2316 dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
2317 return mos7840_get_serial_info(mos7840_port, argp);
2320 dbg("%s (%d) TIOCSSERIAL", __func__, port->number);
2324 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
2325 cprev = mos7840_port->icount;
2327 /* interruptible_sleep_on(&mos7840_port->delta_msr_wait); */
2328 mos7840_port->delta_msr_cond = 0;
2329 wait_event_interruptible(mos7840_port->delta_msr_wait,
2331 delta_msr_cond == 1));
2333 /* see if a signal did it */
2334 if (signal_pending(current))
2335 return -ERESTARTSYS;
2336 cnow = mos7840_port->icount;
2338 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2339 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2340 return -EIO; /* no change => error */
2341 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2342 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2343 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2344 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
2353 cnow = mos7840_port->icount;
2355 icount.cts = cnow.cts;
2356 icount.dsr = cnow.dsr;
2357 icount.rng = cnow.rng;
2358 icount.dcd = cnow.dcd;
2359 icount.rx = cnow.rx;
2360 icount.tx = cnow.tx;
2361 icount.frame = cnow.frame;
2362 icount.overrun = cnow.overrun;
2363 icount.parity = cnow.parity;
2364 icount.brk = cnow.brk;
2365 icount.buf_overrun = cnow.buf_overrun;
2367 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
2368 port->number, icount.rx, icount.tx);
2369 if (copy_to_user(argp, &icount, sizeof(icount)))
2375 return -ENOIOCTLCMD;
2378 static int mos7840_calc_num_ports(struct usb_serial *serial)
2380 int mos7840_num_ports = 0;
2382 dbg("numberofendpoints: cur %d, alt %d",
2383 (int)serial->interface->cur_altsetting->desc.bNumEndpoints,
2384 (int)serial->interface->altsetting->desc.bNumEndpoints);
2385 if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
2386 mos7840_num_ports = serial->num_ports = 2;
2387 } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
2388 serial->num_bulk_in = 4;
2389 serial->num_bulk_out = 4;
2390 mos7840_num_ports = serial->num_ports = 4;
2392 dbg ("mos7840_num_ports = %d", mos7840_num_ports);
2393 return mos7840_num_ports;
2396 /****************************************************************************
2398 ****************************************************************************/
2400 static int mos7840_startup(struct usb_serial *serial)
2402 struct moschip_port *mos7840_port;
2403 struct usb_device *dev;
2407 dbg("%s", "mos7840_startup :Entering..........");
2410 dbg("%s", "Invalid Handler");
2416 dbg("%s", "Entering...");
2417 dbg ("mos7840_startup: serial = %p", serial);
2419 /* we set up the pointers to the endpoints in the mos7840_open *
2420 * function, as the structures aren't created yet. */
2422 /* set up port private structures */
2423 for (i = 0; i < serial->num_ports; ++i) {
2424 dbg ("mos7840_startup: configuring port %d............", i);
2425 mos7840_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
2426 if (mos7840_port == NULL) {
2427 dev_err(&dev->dev, "%s - Out of memory\n", __func__);
2429 i--; /* don't follow NULL pointer cleaning up */
2433 /* Initialize all port interrupt end point to port 0 int
2434 * endpoint. Our device has only one interrupt end point
2435 * common to all port */
2437 mos7840_port->port = serial->port[i];
2438 mos7840_set_port_private(serial->port[i], mos7840_port);
2439 spin_lock_init(&mos7840_port->pool_lock);
2441 /* minor is not initialised until later by
2442 * usb-serial.c:get_free_serial() and cannot therefore be used
2443 * to index device instances */
2444 mos7840_port->port_num = i + 1;
2445 dbg ("serial->port[i]->number = %d", serial->port[i]->number);
2446 dbg ("serial->port[i]->serial->minor = %d", serial->port[i]->serial->minor);
2447 dbg ("mos7840_port->port_num = %d", mos7840_port->port_num);
2448 dbg ("serial->minor = %d", serial->minor);
2450 if (mos7840_port->port_num == 1) {
2451 mos7840_port->SpRegOffset = 0x0;
2452 mos7840_port->ControlRegOffset = 0x1;
2453 mos7840_port->DcrRegOffset = 0x4;
2454 } else if ((mos7840_port->port_num == 2)
2455 && (serial->num_ports == 4)) {
2456 mos7840_port->SpRegOffset = 0x8;
2457 mos7840_port->ControlRegOffset = 0x9;
2458 mos7840_port->DcrRegOffset = 0x16;
2459 } else if ((mos7840_port->port_num == 2)
2460 && (serial->num_ports == 2)) {
2461 mos7840_port->SpRegOffset = 0xa;
2462 mos7840_port->ControlRegOffset = 0xb;
2463 mos7840_port->DcrRegOffset = 0x19;
2464 } else if ((mos7840_port->port_num == 3)
2465 && (serial->num_ports == 4)) {
2466 mos7840_port->SpRegOffset = 0xa;
2467 mos7840_port->ControlRegOffset = 0xb;
2468 mos7840_port->DcrRegOffset = 0x19;
2469 } else if ((mos7840_port->port_num == 4)
2470 && (serial->num_ports == 4)) {
2471 mos7840_port->SpRegOffset = 0xc;
2472 mos7840_port->ControlRegOffset = 0xd;
2473 mos7840_port->DcrRegOffset = 0x1c;
2475 mos7840_dump_serial_port(mos7840_port);
2476 mos7840_set_port_private(serial->port[i], mos7840_port);
2478 /* enable rx_disable bit in control register */
2479 status = mos7840_get_reg_sync(serial->port[i],
2480 mos7840_port->ControlRegOffset, &Data);
2482 dbg("Reading ControlReg failed status-0x%x", status);
2485 dbg("ControlReg Reading success val is %x, status%d",
2487 Data |= 0x08; /* setting driver done bit */
2488 Data |= 0x04; /* sp1_bit to have cts change reflect in
2491 /* Data |= 0x20; //rx_disable bit */
2492 status = mos7840_set_reg_sync(serial->port[i],
2493 mos7840_port->ControlRegOffset, Data);
2495 dbg("Writing ControlReg failed(rx_disable) status-0x%x", status);
2498 dbg("ControlReg Writing success(rx_disable) status%d",
2501 /* Write default values in DCR (i.e 0x01 in DCR0, 0x05 in DCR2
2504 status = mos7840_set_reg_sync(serial->port[i],
2505 (__u16) (mos7840_port->DcrRegOffset + 0), Data);
2507 dbg("Writing DCR0 failed status-0x%x", status);
2510 dbg("DCR0 Writing success status%d", status);
2513 status = mos7840_set_reg_sync(serial->port[i],
2514 (__u16) (mos7840_port->DcrRegOffset + 1), Data);
2516 dbg("Writing DCR1 failed status-0x%x", status);
2519 dbg("DCR1 Writing success status%d", status);
2522 status = mos7840_set_reg_sync(serial->port[i],
2523 (__u16) (mos7840_port->DcrRegOffset + 2), Data);
2525 dbg("Writing DCR2 failed status-0x%x", status);
2528 dbg("DCR2 Writing success status%d", status);
2530 /* write values in clkstart0x0 and clkmulti 0x20 */
2532 status = mos7840_set_reg_sync(serial->port[i],
2533 CLK_START_VALUE_REGISTER, Data);
2535 dbg("Writing CLK_START_VALUE_REGISTER failed status-0x%x", status);
2538 dbg("CLK_START_VALUE_REGISTER Writing success status%d", status);
2541 status = mos7840_set_reg_sync(serial->port[i],
2542 CLK_MULTI_REGISTER, Data);
2544 dbg("Writing CLK_MULTI_REGISTER failed status-0x%x",
2548 dbg("CLK_MULTI_REGISTER Writing success status%d",
2551 /* write value 0x0 to scratchpad register */
2553 status = mos7840_set_uart_reg(serial->port[i],
2554 SCRATCH_PAD_REGISTER, Data);
2556 dbg("Writing SCRATCH_PAD_REGISTER failed status-0x%x",
2560 dbg("SCRATCH_PAD_REGISTER Writing success status%d",
2563 /* Zero Length flag register */
2564 if ((mos7840_port->port_num != 1)
2565 && (serial->num_ports == 2)) {
2568 status = mos7840_set_reg_sync(serial->port[i],
2570 ((__u16)mos7840_port->port_num)), Data);
2571 dbg("ZLIP offset %x",
2573 ((__u16) mos7840_port->port_num)));
2575 dbg("Writing ZLP_REG%d failed status-0x%x",
2579 dbg("ZLP_REG%d Writing success status%d",
2583 status = mos7840_set_reg_sync(serial->port[i],
2585 ((__u16)mos7840_port->port_num) - 0x1), Data);
2586 dbg("ZLIP offset %x",
2588 ((__u16) mos7840_port->port_num) - 0x1));
2590 dbg("Writing ZLP_REG%d failed status-0x%x",
2594 dbg("ZLP_REG%d Writing success status%d",
2598 mos7840_port->control_urb = usb_alloc_urb(0, GFP_KERNEL);
2599 mos7840_port->ctrl_buf = kmalloc(16, GFP_KERNEL);
2600 mos7840_port->dr = kmalloc(sizeof(struct usb_ctrlrequest),
2602 if (!mos7840_port->control_urb || !mos7840_port->ctrl_buf ||
2603 !mos7840_port->dr) {
2608 dbg ("mos7840_startup: all ports configured...........");
2610 /* Zero Length flag enable */
2612 status = mos7840_set_reg_sync(serial->port[0], ZLP_REG5, Data);
2614 dbg("Writing ZLP_REG5 failed status-0x%x", status);
2617 dbg("ZLP_REG5 Writing success status%d", status);
2619 /* setting configuration feature to one */
2620 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
2621 (__u8) 0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5 * HZ);
2624 for (/* nothing */; i >= 0; i--) {
2625 mos7840_port = mos7840_get_port_private(serial->port[i]);
2627 kfree(mos7840_port->dr);
2628 kfree(mos7840_port->ctrl_buf);
2629 usb_free_urb(mos7840_port->control_urb);
2630 kfree(mos7840_port);
2631 serial->port[i] = NULL;
2636 /****************************************************************************
2637 * mos7840_disconnect
2638 * This function is called whenever the device is removed from the usb bus.
2639 ****************************************************************************/
2641 static void mos7840_disconnect(struct usb_serial *serial)
2644 unsigned long flags;
2645 struct moschip_port *mos7840_port;
2646 dbg("%s", " disconnect :entering..........");
2649 dbg("%s", "Invalid Handler");
2653 /* check for the ports to be closed,close the ports and disconnect */
2655 /* free private structure allocated for serial port *
2656 * stop reads and writes on all ports */
2658 for (i = 0; i < serial->num_ports; ++i) {
2659 mos7840_port = mos7840_get_port_private(serial->port[i]);
2660 dbg ("mos7840_port %d = %p", i, mos7840_port);
2662 spin_lock_irqsave(&mos7840_port->pool_lock, flags);
2663 mos7840_port->zombie = 1;
2664 spin_unlock_irqrestore(&mos7840_port->pool_lock, flags);
2665 usb_kill_urb(mos7840_port->control_urb);
2669 dbg("%s", "Thank u :: ");
2673 /****************************************************************************
2675 * This function is called when the usb_serial structure is freed.
2676 ****************************************************************************/
2678 static void mos7840_release(struct usb_serial *serial)
2681 struct moschip_port *mos7840_port;
2682 dbg("%s", " release :entering..........");
2685 dbg("%s", "Invalid Handler");
2689 /* check for the ports to be closed,close the ports and disconnect */
2691 /* free private structure allocated for serial port *
2692 * stop reads and writes on all ports */
2694 for (i = 0; i < serial->num_ports; ++i) {
2695 mos7840_port = mos7840_get_port_private(serial->port[i]);
2696 dbg("mos7840_port %d = %p", i, mos7840_port);
2698 kfree(mos7840_port->ctrl_buf);
2699 kfree(mos7840_port->dr);
2700 kfree(mos7840_port);
2704 dbg("%s", "Thank u :: ");
2708 static struct usb_driver io_driver = {
2710 .probe = usb_serial_probe,
2711 .disconnect = usb_serial_disconnect,
2712 .id_table = moschip_id_table_combined,
2716 static struct usb_serial_driver moschip7840_4port_device = {
2718 .owner = THIS_MODULE,
2721 .description = DRIVER_DESC,
2722 .usb_driver = &io_driver,
2723 .id_table = moschip_port_id_table,
2725 .open = mos7840_open,
2726 .close = mos7840_close,
2727 .write = mos7840_write,
2728 .write_room = mos7840_write_room,
2729 .chars_in_buffer = mos7840_chars_in_buffer,
2730 .throttle = mos7840_throttle,
2731 .unthrottle = mos7840_unthrottle,
2732 .calc_num_ports = mos7840_calc_num_ports,
2733 #ifdef MCSSerialProbe
2734 .probe = mos7840_serial_probe,
2736 .ioctl = mos7840_ioctl,
2737 .set_termios = mos7840_set_termios,
2738 .break_ctl = mos7840_break,
2739 .tiocmget = mos7840_tiocmget,
2740 .tiocmset = mos7840_tiocmset,
2741 .attach = mos7840_startup,
2742 .disconnect = mos7840_disconnect,
2743 .release = mos7840_release,
2744 .read_bulk_callback = mos7840_bulk_in_callback,
2745 .read_int_callback = mos7840_interrupt_callback,
2748 /****************************************************************************
2750 * This is called by the module subsystem, or on startup to initialize us
2751 ****************************************************************************/
2752 static int __init moschip7840_init(void)
2756 dbg("%s", " mos7840_init :entering..........");
2758 /* Register with the usb serial */
2759 retval = usb_serial_register(&moschip7840_4port_device);
2762 goto failed_port_device_register;
2764 dbg("%s", "Entering...");
2765 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
2768 /* Register with the usb */
2769 retval = usb_register(&io_driver);
2771 dbg("%s", "Leaving...");
2774 usb_serial_deregister(&moschip7840_4port_device);
2775 failed_port_device_register:
2779 /****************************************************************************
2781 * Called when the driver is about to be unloaded.
2782 ****************************************************************************/
2783 static void __exit moschip7840_exit(void)
2786 dbg("%s", " mos7840_exit :entering..........");
2788 usb_deregister(&io_driver);
2790 usb_serial_deregister(&moschip7840_4port_device);
2792 dbg("%s", "Entering...");
2795 module_init(moschip7840_init);
2796 module_exit(moschip7840_exit);
2798 /* Module information */
2799 MODULE_DESCRIPTION(DRIVER_DESC);
2800 MODULE_LICENSE("GPL");
2802 module_param(debug, bool, S_IRUGO | S_IWUSR);
2803 MODULE_PARM_DESC(debug, "Debug enabled or not");