xevilstar

qmi_wwan_simcom.c

Jun 30th, 2022
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.83 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2012  Bjørn Mork <[email protected]>
  3.  *
  4.  * The probing code is heavily inspired by cdc_ether, which is:
  5.  * Copyright (C) 2003-2005 by David Brownell
  6.  * Copyright (C) 2006 by Ole Andre Vadla Ravnas (ActiveSync)
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * version 2 as published by the Free Software Foundation.
  11.  */
  12.  
  13. #include <linux/module.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/mii.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/cdc.h>
  20. #include <linux/usb/usbnet.h>
  21. #include <linux/usb/cdc-wdm.h>
  22.  
  23. /* This driver supports wwan (3G/LTE/?) devices using a vendor
  24.  * specific management protocol called Qualcomm MSM Interface (QMI) -
  25.  * in addition to the more common AT commands over serial interface
  26.  * management
  27.  *
  28.  * QMI is wrapped in CDC, using CDC encapsulated commands on the
  29.  * control ("master") interface of a two-interface CDC Union
  30.  * resembling standard CDC ECM.  The devices do not use the control
  31.  * interface for any other CDC messages.  Most likely because the
  32.  * management protocol is used in place of the standard CDC
  33.  * notifications NOTIFY_NETWORK_CONNECTION and NOTIFY_SPEED_CHANGE
  34.  *
  35.  * Alternatively, control and data functions can be combined in a
  36.  * single USB interface.
  37.  *
  38.  * Handling a protocol like QMI is out of the scope for any driver.
  39.  * It is exported as a character device using the cdc-wdm driver as
  40.  * a subdriver, enabling userspace applications ("modem managers") to
  41.  * handle it.
  42.  *
  43.  * These devices may alternatively/additionally be configured using AT
  44.  * commands on a serial interface
  45.  */
  46.  
  47. /* driver specific data */
  48. struct qmi_wwan_state {
  49.     struct usb_driver *subdriver;
  50.     atomic_t pmcount;
  51.     unsigned long unused;
  52.     struct usb_interface *control;
  53.     struct usb_interface *data;
  54. };
  55.  
  56. /* default ethernet address used by the modem */
  57. static const u8 default_modem_addr[ETH_ALEN] = {0x02, 0x50, 0xf3};
  58.  
  59. /* Make up an ethernet header if the packet doesn't have one.
  60.  *
  61.  * A firmware bug common among several devices cause them to send raw
  62.  * IP packets under some circumstances.  There is no way for the
  63.  * driver/host to know when this will happen.  And even when the bug
  64.  * hits, some packets will still arrive with an intact header.
  65.  *
  66.  * The supported devices are only capably of sending IPv4, IPv6 and
  67.  * ARP packets on a point-to-point link. Any packet with an ethernet
  68.  * header will have either our address or a broadcast/multicast
  69.  * address as destination.  ARP packets will always have a header.
  70.  *
  71.  * This means that this function will reliably add the appropriate
  72.  * header iff necessary, provided our hardware address does not start
  73.  * with 4 or 6.
  74.  *
  75.  * Another common firmware bug results in all packets being addressed
  76.  * to 00:a0:c6:00:00:00 despite the host address being different.
  77.  * This function will also fixup such packets.
  78.  */
  79. static int qmi_wwan_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  80. {
  81.     __be16 proto;
  82.  
  83.     /* This check is no longer done by usbnet */
  84.     if (skb->len < dev->net->hard_header_len)
  85.         return 0;
  86.  
  87.     switch (skb->data[0] & 0xf0) {
  88.     case 0x40:
  89.         proto = htons(ETH_P_IP);
  90.         break;
  91.     case 0x60:
  92.         proto = htons(ETH_P_IPV6);
  93.         break;
  94.     case 0x00:
  95.         if (is_multicast_ether_addr(skb->data))
  96.             return 1;
  97.         /* possibly bogus destination - rewrite just in case */
  98.         skb_reset_mac_header(skb);
  99.         goto fix_dest;
  100.     default:
  101.         /* pass along other packets without modifications */
  102.         return 1;
  103.     }
  104.     if (skb_headroom(skb) < ETH_HLEN)
  105.         return 0;
  106.     skb_push(skb, ETH_HLEN);
  107.     skb_reset_mac_header(skb);
  108.     eth_hdr(skb)->h_proto = proto;
  109.     memset(eth_hdr(skb)->h_source, 0, ETH_ALEN);
  110. fix_dest:
  111.     memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
  112.     return 1;
  113. }
  114.  
  115. struct sk_buff *qmi_wwan_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
  116. {
  117.     if (dev->udev->descriptor.idVendor != cpu_to_le16(0x1e0e))
  118.         return skb;
  119.     //skip ethernet header
  120.     if (skb_pull(skb, ETH_HLEN))
  121.     {
  122.         return skb;
  123.     }
  124.     else
  125.     {
  126.         dev_err(&dev->intf->dev, "Packet Dropped\n");
  127.     }
  128.  
  129.     if (skb != NULL)
  130.         dev_kfree_skb_any(skb);
  131.  
  132.     return NULL;
  133. }
  134.  
  135. /* very simplistic detection of IPv4 or IPv6 headers */
  136. static bool possibly_iphdr(const char *data)
  137. {
  138.     return (data[0] & 0xd0) == 0x40;
  139. }
  140.  
  141. /* disallow addresses which may be confused with IP headers */
  142. static int qmi_wwan_mac_addr(struct net_device *dev, void *p)
  143. {
  144.     int ret;
  145.     struct sockaddr *addr = p;
  146.  
  147.     ret = eth_prepare_mac_addr_change(dev, p);
  148.     if (ret < 0)
  149.         return ret;
  150.     if (possibly_iphdr(addr->sa_data))
  151.         return -EADDRNOTAVAIL;
  152.     eth_commit_mac_addr_change(dev, p);
  153.     return 0;
  154. }
  155.  
  156. static const struct net_device_ops qmi_wwan_netdev_ops = {
  157.     .ndo_open       = usbnet_open,
  158.     .ndo_stop       = usbnet_stop,
  159.     .ndo_start_xmit     = usbnet_start_xmit,
  160.     .ndo_tx_timeout     = usbnet_tx_timeout,
  161.     .ndo_change_mtu     = usbnet_change_mtu,
  162.     .ndo_set_mac_address    = qmi_wwan_mac_addr,
  163.     .ndo_validate_addr  = eth_validate_addr,
  164. };
  165.  
  166. /* using a counter to merge subdriver requests with our own into a
  167.  * combined state
  168.  */
  169. static int qmi_wwan_manage_power(struct usbnet *dev, int on)
  170. {
  171.     struct qmi_wwan_state *info = (void *)&dev->data;
  172.     int rv;
  173.  
  174.     dev_dbg(&dev->intf->dev, "%s() pmcount=%d, on=%d\n", __func__,
  175.         atomic_read(&info->pmcount), on);
  176.  
  177.     if ((on && atomic_add_return(1, &info->pmcount) == 1) ||
  178.         (!on && atomic_dec_and_test(&info->pmcount))) {
  179.         /* need autopm_get/put here to ensure the usbcore sees
  180.          * the new value
  181.          */
  182.         rv = usb_autopm_get_interface(dev->intf);
  183.         dev->intf->needs_remote_wakeup = on;
  184.         if (!rv)
  185.             usb_autopm_put_interface(dev->intf);
  186.     }
  187.     return 0;
  188. }
  189.  
  190. static int qmi_wwan_cdc_wdm_manage_power(struct usb_interface *intf, int on)
  191. {
  192.     struct usbnet *dev = usb_get_intfdata(intf);
  193.  
  194.     /* can be called while disconnecting */
  195.     if (!dev)
  196.         return 0;
  197.     return qmi_wwan_manage_power(dev, on);
  198. }
  199.  
  200. /* collect all three endpoints and register subdriver */
  201. static int qmi_wwan_register_subdriver(struct usbnet *dev)
  202. {
  203.     int rv;
  204.     struct usb_driver *subdriver = NULL;
  205.     struct qmi_wwan_state *info = (void *)&dev->data;
  206.  
  207.     /* collect bulk endpoints */
  208.     rv = usbnet_get_endpoints(dev, info->data);
  209.     if (rv < 0)
  210.         goto err;
  211.  
  212.     /* update status endpoint if separate control interface */
  213.     if (info->control != info->data)
  214.         dev->status = &info->control->cur_altsetting->endpoint[0];
  215.  
  216.     /* require interrupt endpoint for subdriver */
  217.     if (!dev->status) {
  218.         rv = -EINVAL;
  219.         goto err;
  220.     }
  221.  
  222.     /* for subdriver power management */
  223.     atomic_set(&info->pmcount, 0);
  224.  
  225.     /* register subdriver */
  226.     subdriver = usb_cdc_wdm_register(info->control, &dev->status->desc,
  227.                      4096, &qmi_wwan_cdc_wdm_manage_power);
  228.     if (IS_ERR(subdriver)) {
  229.         dev_err(&info->control->dev, "subdriver registration failed\n");
  230.         rv = PTR_ERR(subdriver);
  231.         goto err;
  232.     }
  233.  
  234.     /* prevent usbnet from using status endpoint */
  235.     dev->status = NULL;
  236.  
  237.     /* save subdriver struct for suspend/resume wrappers */
  238.     info->subdriver = subdriver;
  239.  
  240. err:
  241.     return rv;
  242. }
  243.  
  244. static int qmi_wwan_bind(struct usbnet *dev, struct usb_interface *intf)
  245. {
  246.     int status = -1;
  247.     u8 *buf = intf->cur_altsetting->extra;
  248.     int len = intf->cur_altsetting->extralen;
  249.     struct usb_interface_descriptor *desc = &intf->cur_altsetting->desc;
  250.     struct usb_cdc_union_desc *cdc_union = NULL;
  251.     struct usb_cdc_ether_desc *cdc_ether = NULL;
  252.     u32 found = 0;
  253.     struct usb_driver *driver = driver_of(intf);
  254.     struct qmi_wwan_state *info = (void *)&dev->data;
  255.  
  256.     BUILD_BUG_ON((sizeof(((struct usbnet *)0)->data) <
  257.               sizeof(struct qmi_wwan_state)));
  258.  
  259.     /* set up initial state */
  260.     info->control = intf;
  261.     info->data = intf;
  262.  
  263.     /* and a number of CDC descriptors */
  264.     while (len > 3) {
  265.         struct usb_descriptor_header *h = (void *)buf;
  266.  
  267.         /* ignore any misplaced descriptors */
  268.         if (h->bDescriptorType != USB_DT_CS_INTERFACE)
  269.             goto next_desc;
  270.  
  271.         /* buf[2] is CDC descriptor subtype */
  272.         switch (buf[2]) {
  273.         case USB_CDC_HEADER_TYPE:
  274.             if (found & 1 << USB_CDC_HEADER_TYPE) {
  275.                 dev_dbg(&intf->dev, "extra CDC header\n");
  276.                 goto err;
  277.             }
  278.             if (h->bLength != sizeof(struct usb_cdc_header_desc)) {
  279.                 dev_dbg(&intf->dev, "CDC header len %u\n",
  280.                     h->bLength);
  281.                 goto err;
  282.             }
  283.             break;
  284.         case USB_CDC_UNION_TYPE:
  285.             if (found & 1 << USB_CDC_UNION_TYPE) {
  286.                 dev_dbg(&intf->dev, "extra CDC union\n");
  287.                 goto err;
  288.             }
  289.             if (h->bLength != sizeof(struct usb_cdc_union_desc)) {
  290.                 dev_dbg(&intf->dev, "CDC union len %u\n",
  291.                     h->bLength);
  292.                 goto err;
  293.             }
  294.             cdc_union = (struct usb_cdc_union_desc *)buf;
  295.             break;
  296.         case USB_CDC_ETHERNET_TYPE:
  297.             if (found & 1 << USB_CDC_ETHERNET_TYPE) {
  298.                 dev_dbg(&intf->dev, "extra CDC ether\n");
  299.                 goto err;
  300.             }
  301.             if (h->bLength != sizeof(struct usb_cdc_ether_desc)) {
  302.                 dev_dbg(&intf->dev, "CDC ether len %u\n",
  303.                     h->bLength);
  304.                 goto err;
  305.             }
  306.             cdc_ether = (struct usb_cdc_ether_desc *)buf;
  307.             break;
  308.         }
  309.  
  310.         /* Remember which CDC functional descriptors we've seen.  Works
  311.          * for all types we care about, of which USB_CDC_ETHERNET_TYPE
  312.          * (0x0f) is the highest numbered
  313.          */
  314.         if (buf[2] < 32)
  315.             found |= 1 << buf[2];
  316.  
  317. next_desc:
  318.         len -= h->bLength;
  319.         buf += h->bLength;
  320.     }
  321.  
  322.     /* Use separate control and data interfaces if we found a CDC Union */
  323.     if (cdc_union) {
  324.         info->data = usb_ifnum_to_if(dev->udev,
  325.                          cdc_union->bSlaveInterface0);
  326.         if (desc->bInterfaceNumber != cdc_union->bMasterInterface0 ||
  327.             !info->data) {
  328.             dev_err(&intf->dev,
  329.                 "bogus CDC Union: master=%u, slave=%u\n",
  330.                 cdc_union->bMasterInterface0,
  331.                 cdc_union->bSlaveInterface0);
  332.             goto err;
  333.         }
  334.     }
  335.  
  336.     /* errors aren't fatal - we can live with the dynamic address */
  337.     if (cdc_ether && cdc_ether->wMaxSegmentSize) {
  338.         dev->hard_mtu = le16_to_cpu(cdc_ether->wMaxSegmentSize);
  339.         usbnet_get_ethernet_addr(dev, cdc_ether->iMACAddress);
  340.     }
  341.  
  342.     /* claim data interface and set it up */
  343.     if (info->control != info->data) {
  344.         status = usb_driver_claim_interface(driver, info->data, dev);
  345.         if (status < 0)
  346.             goto err;
  347.     }
  348.  
  349.     status = qmi_wwan_register_subdriver(dev);
  350.     if (status < 0 && info->control != info->data) {
  351.         usb_set_intfdata(info->data, NULL);
  352.         usb_driver_release_interface(driver, info->data);
  353.     }
  354.  
  355.     /* Never use the same address on both ends of the link, even
  356.      * if the buggy firmware told us to.
  357.      */
  358.     if (ether_addr_equal(dev->net->dev_addr, default_modem_addr))
  359.         eth_hw_addr_random(dev->net);
  360.  
  361.     /* make MAC addr easily distinguishable from an IP header */
  362.     if (possibly_iphdr(dev->net->dev_addr)) {
  363.         dev->net->dev_addr[0] |= 0x02;  /* set local assignment bit */
  364.         dev->net->dev_addr[0] &= 0xbf;  /* clear "IP" bit */
  365.     }
  366.     dev->net->netdev_ops = &qmi_wwan_netdev_ops;
  367.  
  368. #if 1 //Added by Simcom
  369.     if (dev->udev->descriptor.idVendor == cpu_to_le16(0x1e0e)) {       
  370.         dev_info(&intf->dev, "SIMCom 8200 work on RawIP mode\n");
  371.         dev->net->flags |= IFF_NOARP;
  372.         usb_control_msg(
  373.             interface_to_usbdev(intf),
  374.             usb_sndctrlpipe(interface_to_usbdev(intf), 0),
  375.             0x22, //USB_CDC_REQ_SET_CONTROL_LINE_STATE
  376.             0x21, //USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE
  377.             1, //active CDC DTR
  378.             intf->cur_altsetting->desc.bInterfaceNumber,
  379.             NULL,
  380.             0,
  381.             100
  382.             );
  383.     }
  384. #endif
  385.  
  386. err:
  387.     return status;
  388. }
  389.  
  390. static void qmi_wwan_unbind(struct usbnet *dev, struct usb_interface *intf)
  391. {
  392.     struct qmi_wwan_state *info = (void *)&dev->data;
  393.     struct usb_driver *driver = driver_of(intf);
  394.     struct usb_interface *other;
  395.  
  396.     if (info->subdriver && info->subdriver->disconnect)
  397.         info->subdriver->disconnect(info->control);
  398.  
  399.     /* allow user to unbind using either control or data */
  400.     if (intf == info->control)
  401.         other = info->data;
  402.     else
  403.         other = info->control;
  404.  
  405.     /* only if not shared */
  406.     if (other && intf != other) {
  407.         usb_set_intfdata(other, NULL);
  408.         usb_driver_release_interface(driver, other);
  409.     }
  410.  
  411.     info->subdriver = NULL;
  412.     info->data = NULL;
  413.     info->control = NULL;
  414. }
  415.  
  416. /* suspend/resume wrappers calling both usbnet and the cdc-wdm
  417.  * subdriver if present.
  418.  *
  419.  * NOTE: cdc-wdm also supports pre/post_reset, but we cannot provide
  420.  * wrappers for those without adding usbnet reset support first.
  421.  */
  422. static int qmi_wwan_suspend(struct usb_interface *intf, pm_message_t message)
  423. {
  424.     struct usbnet *dev = usb_get_intfdata(intf);
  425.     struct qmi_wwan_state *info = (void *)&dev->data;
  426.     int ret;
  427.  
  428.     /* Both usbnet_suspend() and subdriver->suspend() MUST return 0
  429.      * in system sleep context, otherwise, the resume callback has
  430.      * to recover device from previous suspend failure.
  431.      */
  432.     ret = usbnet_suspend(intf, message);
  433.     if (ret < 0)
  434.         goto err;
  435.  
  436.     if (intf == info->control && info->subdriver &&
  437.         info->subdriver->suspend)
  438.         ret = info->subdriver->suspend(intf, message);
  439.     if (ret < 0)
  440.         usbnet_resume(intf);
  441. err:
  442.     return ret;
  443. }
  444.  
  445. static int qmi_wwan_resume(struct usb_interface *intf)
  446. {
  447.     struct usbnet *dev = usb_get_intfdata(intf);
  448.     struct qmi_wwan_state *info = (void *)&dev->data;
  449.     int ret = 0;
  450.     bool callsub = (intf == info->control && info->subdriver &&
  451.             info->subdriver->resume);
  452.  
  453.     if (callsub)
  454.         ret = info->subdriver->resume(intf);
  455.     if (ret < 0)
  456.         goto err;
  457.     ret = usbnet_resume(intf);
  458.     if (ret < 0 && callsub)
  459.         info->subdriver->suspend(intf, PMSG_SUSPEND);
  460. err:
  461.     return ret;
  462. }
  463.  
  464. static const struct driver_info qmi_wwan_info = {
  465.     .description    = "WWAN/QMI device",
  466.     .flags      = FLAG_WWAN,
  467.     .bind       = qmi_wwan_bind,
  468.     .unbind     = qmi_wwan_unbind,
  469.     .manage_power   = qmi_wwan_manage_power,
  470.     .rx_fixup       = qmi_wwan_rx_fixup,
  471. };
  472.  
  473. static const struct driver_info qmi_wwan_raw_ip_info = {
  474.     .description    = "WWAN/QMI device",
  475.     .flags      = FLAG_WWAN,
  476.     .bind       = qmi_wwan_bind,
  477.     .unbind     = qmi_wwan_unbind,
  478.     .manage_power   = qmi_wwan_manage_power,
  479.     .rx_fixup       = qmi_wwan_rx_fixup,
  480.     .tx_fixup       = qmi_wwan_tx_fixup,
  481. };
  482.  
  483. #define HUAWEI_VENDOR_ID    0x12D1
  484.  
  485. /* map QMI/wwan function by a fixed interface number */
  486. #define QMI_FIXED_INTF(vend, prod, num) \
  487.     USB_DEVICE_INTERFACE_NUMBER(vend, prod, num), \
  488.     .driver_info = (unsigned long)&qmi_wwan_info
  489.  
  490. #define QMI_FIXED_RAWIP_INTF(vend, prod, num) \
  491.     USB_DEVICE_INTERFACE_NUMBER(vend, prod, num), \
  492.     .driver_info = (unsigned long)&qmi_wwan_raw_ip_info
  493.  
  494. /* Gobi 1000 QMI/wwan interface number is 3 according to qcserial */
  495. #define QMI_GOBI1K_DEVICE(vend, prod) \
  496.     QMI_FIXED_INTF(vend, prod, 3)
  497.  
  498. /* Gobi 2000/3000 QMI/wwan interface number is 0 according to qcserial */
  499. #define QMI_GOBI_DEVICE(vend, prod) \
  500.     QMI_FIXED_INTF(vend, prod, 0)
  501.  
  502. static const struct usb_device_id products[] = {
  503.     {QMI_FIXED_RAWIP_INTF(0x1e0e, 0x9001, 5)},  /* SIMCOM 8200 */
  504.     { }                 /* END */
  505. };
  506. MODULE_DEVICE_TABLE(usb, products);
  507.  
  508. static bool quectel_ec20_detected(struct usb_interface *intf)
  509. {
  510.     struct usb_device *dev = interface_to_usbdev(intf);
  511.  
  512.     if (dev->actconfig &&
  513.         le16_to_cpu(dev->descriptor.idVendor) == 0x05c6 &&
  514.         le16_to_cpu(dev->descriptor.idProduct) == 0x9215 &&
  515.         dev->actconfig->desc.bNumInterfaces == 5)
  516.         return true;
  517.  
  518.     return false;
  519. }
  520.  
  521. static int qmi_wwan_probe(struct usb_interface *intf,
  522.               const struct usb_device_id *prod)
  523. {
  524.     struct usb_device_id *id = (struct usb_device_id *)prod;
  525.     struct usb_interface_descriptor *desc = &intf->cur_altsetting->desc;
  526.  
  527.     /* Workaround to enable dynamic IDs.  This disables usbnet
  528.      * blacklisting functionality.  Which, if required, can be
  529.      * reimplemented here by using a magic "blacklist" value
  530.      * instead of 0 in the static device id table
  531.      */
  532.     if (!id->driver_info) {
  533.         dev_dbg(&intf->dev, "setting defaults for dynamic device id\n");
  534.         id->driver_info = (unsigned long)&qmi_wwan_info;
  535.     }
  536.  
  537.     /* There are devices where the same interface number can be
  538.      * configured as different functions. We should only bind to
  539.      * vendor specific functions when matching on interface number
  540.      */
  541.     if (id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER &&
  542.         desc->bInterfaceClass != USB_CLASS_VENDOR_SPEC) {
  543.         dev_dbg(&intf->dev,
  544.             "Rejecting interface number match for class %02x\n",
  545.             desc->bInterfaceClass);
  546.         return -ENODEV;
  547.     }
  548.  
  549.     /* Quectel EC20 quirk where we've QMI on interface 4 instead of 0 */
  550.     if (quectel_ec20_detected(intf) && desc->bInterfaceNumber == 0) {
  551.         dev_dbg(&intf->dev, "Quectel EC20 quirk, skipping interface 0\n");
  552.         return -ENODEV;
  553.     }
  554.  
  555.     return usbnet_probe(intf, id);
  556. }
  557.  
  558. static struct usb_driver qmi_wwan_driver = {
  559.     .name             = "qmi_wwan_simcom",
  560.     .id_table         = products,
  561.     .probe            = qmi_wwan_probe,
  562.     .disconnect       = usbnet_disconnect,
  563.     .suspend          = qmi_wwan_suspend,
  564.     .resume           = qmi_wwan_resume,
  565.     .reset_resume         = qmi_wwan_resume,
  566.     .supports_autosuspend = 1,
  567.     .disable_hub_initiated_lpm = 1,
  568. };
  569.  
  570. module_usb_driver(qmi_wwan_driver);
  571.  
  572. MODULE_AUTHOR("Bjørn Mork <[email protected]>");
  573. MODULE_DESCRIPTION("Qualcomm MSM Interface (QMI) WWAN driver");
  574. MODULE_LICENSE("GPL");
  575. MODULE_VERSION("Simcom_Linux_QMI_WWAN_Driver_V1.0");
Advertisement
Add Comment
Please, Sign In to add comment