Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / drivers / net / usb / cdc_ncm.c
index 0ed03b1ba39a0b5b1dfa0371350e340dbb223aa3..d38bc20a60e2e63fd2d056b9070cdce57f524090 100644 (file)
@@ -82,16 +82,15 @@ static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
        u16 ntb_fmt_supported;
        u32 min_dgram_size;
        u32 min_hdr_size;
+       struct usbnet *dev = netdev_priv(ctx->netdev);
 
        iface_no = ctx->control->cur_altsetting->desc.bInterfaceNumber;
 
-       err = usb_control_msg(ctx->udev,
-                               usb_rcvctrlpipe(ctx->udev, 0),
-                               USB_CDC_GET_NTB_PARAMETERS,
-                               USB_TYPE_CLASS | USB_DIR_IN
-                                | USB_RECIP_INTERFACE,
-                               0, iface_no, &ctx->ncm_parm,
-                               sizeof(ctx->ncm_parm), 10000);
+       err = usbnet_read_cmd(dev, USB_CDC_GET_NTB_PARAMETERS,
+                             USB_TYPE_CLASS | USB_DIR_IN
+                             |USB_RECIP_INTERFACE,
+                             0, iface_no, &ctx->ncm_parm,
+                             sizeof(ctx->ncm_parm));
        if (err < 0) {
                pr_debug("failed GET_NTB_PARAMETERS\n");
                return 1;
@@ -147,22 +146,12 @@ static u8 cdc_ncm_setup(struct cdc_ncm_ctx *ctx)
 
        /* inform device about NTB input size changes */
        if (ctx->rx_max != le32_to_cpu(ctx->ncm_parm.dwNtbInMaxSize)) {
-               __le32 *dwNtbInMaxSize;
+               __le32 dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
 
-               dwNtbInMaxSize = kzalloc(sizeof(*dwNtbInMaxSize), GFP_KERNEL);
-               if (!dwNtbInMaxSize) {
-                       err = -ENOMEM;
-                       goto size_err;
-               }
-               *dwNtbInMaxSize = cpu_to_le32(ctx->rx_max);
-               err = usb_control_msg(ctx->udev,
-                                     usb_sndctrlpipe(ctx->udev, 0),
-                                     USB_CDC_SET_NTB_INPUT_SIZE,
-                                     USB_TYPE_CLASS | USB_DIR_OUT
-                                     | USB_RECIP_INTERFACE,
-                                     0, iface_no, dwNtbInMaxSize, 4, 1000);
-               kfree(dwNtbInMaxSize);
-size_err:
+               err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_INPUT_SIZE,
+                                      USB_TYPE_CLASS | USB_DIR_OUT
+                                      | USB_RECIP_INTERFACE,
+                                      0, iface_no, &dwNtbInMaxSize, 4);
                if (err < 0)
                        pr_debug("Setting NTB Input Size failed\n");
        }
@@ -218,23 +207,22 @@ size_err:
 
        /* set CRC Mode */
        if (flags & USB_CDC_NCM_NCAP_CRC_MODE) {
-               err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
-                               USB_CDC_SET_CRC_MODE,
-                               USB_TYPE_CLASS | USB_DIR_OUT
-                                | USB_RECIP_INTERFACE,
-                               USB_CDC_NCM_CRC_NOT_APPENDED,
-                               iface_no, NULL, 0, 1000);
+               err = usbnet_write_cmd(dev, USB_CDC_SET_CRC_MODE,
+                                      USB_TYPE_CLASS | USB_DIR_OUT
+                                      | USB_RECIP_INTERFACE,
+                                      USB_CDC_NCM_CRC_NOT_APPENDED,
+                                      iface_no, NULL, 0);
                if (err < 0)
                        pr_debug("Setting CRC mode off failed\n");
        }
 
        /* set NTB format, if both formats are supported */
        if (ntb_fmt_supported & USB_CDC_NCM_NTH32_SIGN) {
-               err = usb_control_msg(ctx->udev, usb_sndctrlpipe(ctx->udev, 0),
-                               USB_CDC_SET_NTB_FORMAT, USB_TYPE_CLASS
-                                | USB_DIR_OUT | USB_RECIP_INTERFACE,
-                               USB_CDC_NCM_NTB16_FORMAT,
-                               iface_no, NULL, 0, 1000);
+               err = usbnet_write_cmd(dev, USB_CDC_SET_NTB_FORMAT,
+                                      USB_TYPE_CLASS | USB_DIR_OUT
+                                      | USB_RECIP_INTERFACE,
+                                      USB_CDC_NCM_NTB16_FORMAT,
+                                      iface_no, NULL, 0);
                if (err < 0)
                        pr_debug("Setting NTB format to 16-bit failed\n");
        }
@@ -243,7 +231,7 @@ size_err:
 
        /* set Max Datagram Size (MTU) */
        if (flags & USB_CDC_NCM_NCAP_MAX_DATAGRAM_SIZE) {
-               __le16 *max_datagram_size;
+               __le16 max_datagram_size;
                u16 eth_max_sz;
                if (ctx->ether_desc != NULL)
                        eth_max_sz = le16_to_cpu(ctx->ether_desc->wMaxSegmentSize);
@@ -252,25 +240,16 @@ size_err:
                else
                        goto max_dgram_err;
 
-               max_datagram_size = kzalloc(sizeof(*max_datagram_size),
-                               GFP_KERNEL);
-               if (!max_datagram_size) {
-                       err = -ENOMEM;
-                       goto max_dgram_err;
-               }
-
-               err = usb_control_msg(ctx->udev, usb_rcvctrlpipe(ctx->udev, 0),
-                               USB_CDC_GET_MAX_DATAGRAM_SIZE,
-                               USB_TYPE_CLASS | USB_DIR_IN
-                                | USB_RECIP_INTERFACE,
-                               0, iface_no, max_datagram_size,
-                               2, 1000);
+               err = usbnet_read_cmd(dev, USB_CDC_GET_MAX_DATAGRAM_SIZE,
+                                     USB_TYPE_CLASS | USB_DIR_IN
+                                     | USB_RECIP_INTERFACE,
+                                     0, iface_no, &max_datagram_size, 2);
                if (err < 0) {
                        pr_debug("GET_MAX_DATAGRAM_SIZE failed, use size=%u\n",
                                 min_dgram_size);
                } else {
                        ctx->max_datagram_size =
-                               le16_to_cpu(*max_datagram_size);
+                               le16_to_cpu(max_datagram_size);
                        /* Check Eth descriptor value */
                        if (ctx->max_datagram_size > eth_max_sz)
                                        ctx->max_datagram_size = eth_max_sz;
@@ -283,20 +262,18 @@ size_err:
 
                        /* if value changed, update device */
                        if (ctx->max_datagram_size !=
-                                       le16_to_cpu(*max_datagram_size)) {
-                               err = usb_control_msg(ctx->udev,
-                                               usb_sndctrlpipe(ctx->udev, 0),
+                                       le16_to_cpu(max_datagram_size)) {
+                               err = usbnet_write_cmd(dev,
                                                USB_CDC_SET_MAX_DATAGRAM_SIZE,
                                                USB_TYPE_CLASS | USB_DIR_OUT
                                                 | USB_RECIP_INTERFACE,
                                                0,
-                                               iface_no, max_datagram_size,
-                                               2, 1000);
+                                               iface_no, &max_datagram_size,
+                                               2);
                                if (err < 0)
                                        pr_debug("SET_MAX_DGRAM_SIZE failed\n");
                        }
                }
-               kfree(max_datagram_size);
        }
 
 max_dgram_err:
@@ -463,10 +440,12 @@ advance:
            ((!ctx->mbim_desc) && ((ctx->ether_desc == NULL) || (ctx->control != intf))))
                goto error;
 
-       /* claim interfaces, if any */
-       temp = usb_driver_claim_interface(driver, ctx->data, dev);
-       if (temp)
-               goto error;
+       /* claim data interface, if different from control */
+       if (ctx->data != ctx->control) {
+               temp = usb_driver_claim_interface(driver, ctx->data, dev);
+               if (temp)
+                       goto error;
+       }
 
        iface_no = ctx->data->cur_altsetting->desc.bInterfaceNumber;
 
@@ -542,6 +521,10 @@ void cdc_ncm_unbind(struct usbnet *dev, struct usb_interface *intf)
 
        tasklet_kill(&ctx->bh);
 
+       /* handle devices with combined control and data interface */
+       if (ctx->control == ctx->data)
+               ctx->data = NULL;
+
        /* disconnect master --> disconnect slave */
        if (intf == ctx->control && ctx->data) {
                usb_set_intfdata(ctx->data, NULL);
@@ -842,6 +825,8 @@ static void cdc_ncm_txpath_bh(unsigned long param)
                netif_tx_lock_bh(ctx->netdev);
                usbnet_start_xmit(NULL, ctx->netdev);
                netif_tx_unlock_bh(ctx->netdev);
+       } else {
+               spin_unlock_bh(&ctx->mtx);
        }
 }
 
@@ -1087,7 +1072,7 @@ static void cdc_ncm_status(struct usbnet *dev, struct urb *urb)
                 * USB_CDC_NOTIFY_NETWORK_CONNECTION notification shall be
                 * sent by device after USB_CDC_NOTIFY_SPEED_CHANGE.
                 */
-               ctx->connected = event->wValue;
+               ctx->connected = le16_to_cpu(event->wValue);
 
                printk(KERN_INFO KBUILD_MODNAME ": %s: network connection:"
                        " %sconnected\n",
@@ -1207,6 +1192,14 @@ static const struct usb_device_id cdc_devs[] = {
          .driver_info = (unsigned long) &wwan_info,
        },
 
+       /* Huawei NCM devices disguised as vendor specific */
+       { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x16),
+         .driver_info = (unsigned long)&wwan_info,
+       },
+       { USB_VENDOR_AND_INTERFACE_INFO(0x12d1, 0xff, 0x02, 0x46),
+         .driver_info = (unsigned long)&wwan_info,
+       },
+
        /* Generic CDC-NCM devices */
        { USB_INTERFACE_INFO(USB_CLASS_COMM,
                USB_CDC_SUBCLASS_NCM, USB_CDC_PROTO_NONE),