USB: serial: mos7840: Fixed MCS7820 device attach problem
authorDonald Lee <donald@asix.com.tw>
Wed, 14 Mar 2012 07:26:33 +0000 (15:26 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 14 Mar 2012 19:25:26 +0000 (12:25 -0700)
A MCS7820 device supports two serial ports and a MCS7840 device supports
four serial ports. Both devices use the same driver, but the attach function
in driver was unable to correctly handle the port numbers for MCS7820
device. This problem has been fixed in this patch and this fix has been
verified on x86 Linux kernel 3.2.9 with both MCS7820 and MCS7840 devices.

Signed-off-by: Donald Lee <donald@asix.com.tw>
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/serial/mos7840.c

index f7e6e30f53ee03af764e41860bba36d2507a02c8..c526550694a06270acf60b056274ec764a75e5bc 100644 (file)
 
 #define CLK_MULTI_REGISTER         ((__u16)(0x02))
 #define CLK_START_VALUE_REGISTER   ((__u16)(0x03))
+#define GPIO_REGISTER              ((__u16)(0x07))
 
 #define SERIAL_LCR_DLAB            ((__u16)(0x0080))
 
@@ -1101,14 +1102,25 @@ static int mos7840_open(struct tty_struct *tty, struct usb_serial_port *port)
        mos7840_port->read_urb = port->read_urb;
 
        /* set up our bulk in urb */
-
-       usb_fill_bulk_urb(mos7840_port->read_urb,
-                         serial->dev,
-                         usb_rcvbulkpipe(serial->dev,
-                                         port->bulk_in_endpointAddress),
-                         port->bulk_in_buffer,
-                         mos7840_port->read_urb->transfer_buffer_length,
-                         mos7840_bulk_in_callback, mos7840_port);
+       if ((serial->num_ports == 2)
+               && ((((__u16)port->number -
+                       (__u16)(port->serial->minor)) % 2) != 0)) {
+               usb_fill_bulk_urb(mos7840_port->read_urb,
+                       serial->dev,
+                       usb_rcvbulkpipe(serial->dev,
+                               (port->bulk_in_endpointAddress) + 2),
+                       port->bulk_in_buffer,
+                       mos7840_port->read_urb->transfer_buffer_length,
+                       mos7840_bulk_in_callback, mos7840_port);
+       } else {
+               usb_fill_bulk_urb(mos7840_port->read_urb,
+                       serial->dev,
+                       usb_rcvbulkpipe(serial->dev,
+                               port->bulk_in_endpointAddress),
+                       port->bulk_in_buffer,
+                       mos7840_port->read_urb->transfer_buffer_length,
+                       mos7840_bulk_in_callback, mos7840_port);
+       }
 
        dbg("mos7840_open: bulkin endpoint is %d",
            port->bulk_in_endpointAddress);
@@ -1519,13 +1531,25 @@ static int mos7840_write(struct tty_struct *tty, struct usb_serial_port *port,
        memcpy(urb->transfer_buffer, current_position, transfer_size);
 
        /* fill urb with data and submit  */
-       usb_fill_bulk_urb(urb,
-                         serial->dev,
-                         usb_sndbulkpipe(serial->dev,
-                                         port->bulk_out_endpointAddress),
-                         urb->transfer_buffer,
-                         transfer_size,
-                         mos7840_bulk_out_data_callback, mos7840_port);
+       if ((serial->num_ports == 2)
+               && ((((__u16)port->number -
+                       (__u16)(port->serial->minor)) % 2) != 0)) {
+               usb_fill_bulk_urb(urb,
+                       serial->dev,
+                       usb_sndbulkpipe(serial->dev,
+                               (port->bulk_out_endpointAddress) + 2),
+                       urb->transfer_buffer,
+                       transfer_size,
+                       mos7840_bulk_out_data_callback, mos7840_port);
+       } else {
+               usb_fill_bulk_urb(urb,
+                       serial->dev,
+                       usb_sndbulkpipe(serial->dev,
+                               port->bulk_out_endpointAddress),
+                       urb->transfer_buffer,
+                       transfer_size,
+                       mos7840_bulk_out_data_callback, mos7840_port);
+       }
 
        data1 = urb->transfer_buffer;
        dbg("bulkout endpoint is %d", port->bulk_out_endpointAddress);
@@ -1838,7 +1862,7 @@ static int mos7840_send_cmd_write_baud_rate(struct moschip_port *mos7840_port,
 
        } else {
 #ifdef HW_flow_control
-               / *setting h/w flow control bit to 0 */
+               /setting h/w flow control bit to 0 */
                Data = 0xb;
                mos7840_port->shadowMCR = Data;
                status = mos7840_set_uart_reg(port, MODEM_CONTROL_REGISTER,
@@ -2305,19 +2329,26 @@ static int mos7840_ioctl(struct tty_struct *tty,
 
 static int mos7840_calc_num_ports(struct usb_serial *serial)
 {
-       int mos7840_num_ports = 0;
-
-       dbg("numberofendpoints: cur %d, alt %d",
-           (int)serial->interface->cur_altsetting->desc.bNumEndpoints,
-           (int)serial->interface->altsetting->desc.bNumEndpoints);
-       if (serial->interface->cur_altsetting->desc.bNumEndpoints == 5) {
-               mos7840_num_ports = serial->num_ports = 2;
-       } else if (serial->interface->cur_altsetting->desc.bNumEndpoints == 9) {
+       __u16 Data = 0x00;
+       int ret = 0;
+       int mos7840_num_ports;
+
+       ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
+               MCS_RDREQ, MCS_RD_RTYPE, 0, GPIO_REGISTER, &Data,
+               VENDOR_READ_LENGTH, MOS_WDR_TIMEOUT);
+
+       if ((Data & 0x01) == 0) {
+               mos7840_num_ports = 2;
+               serial->num_bulk_in = 2;
+               serial->num_bulk_out = 2;
+               serial->num_ports = 2;
+       } else {
+               mos7840_num_ports = 4;
                serial->num_bulk_in = 4;
                serial->num_bulk_out = 4;
-               mos7840_num_ports = serial->num_ports = 4;
+               serial->num_ports = 4;
        }
-       dbg ("mos7840_num_ports = %d", mos7840_num_ports);
+
        return mos7840_num_ports;
 }