Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * USB-to-WWAN Driver for Sierra Wireless modems
0004  *
0005  * Copyright (C) 2008, 2009, 2010 Paxton Smith, Matthew Safar, Rory Filer
0006  *                          <linux@sierrawireless.com>
0007  *
0008  * Portions of this based on the cdc_ether driver by David Brownell (2003-2005)
0009  * and Ole Andre Vadla Ravnas (ActiveSync) (2006).
0010  *
0011  * IMPORTANT DISCLAIMER: This driver is not commercially supported by
0012  * Sierra Wireless. Use at your own risk.
0013  */
0014 
0015 #define DRIVER_VERSION "v.2.0"
0016 #define DRIVER_AUTHOR "Paxton Smith, Matthew Safar, Rory Filer"
0017 #define DRIVER_DESC "USB-to-WWAN Driver for Sierra Wireless modems"
0018 static const char driver_name[] = "sierra_net";
0019 
0020 /* if defined debug messages enabled */
0021 /*#define   DEBUG*/
0022 
0023 #include <linux/module.h>
0024 #include <linux/etherdevice.h>
0025 #include <linux/ethtool.h>
0026 #include <linux/mii.h>
0027 #include <linux/sched.h>
0028 #include <linux/timer.h>
0029 #include <linux/usb.h>
0030 #include <linux/usb/cdc.h>
0031 #include <net/ip.h>
0032 #include <net/udp.h>
0033 #include <asm/unaligned.h>
0034 #include <linux/usb/usbnet.h>
0035 
0036 #define SWI_USB_REQUEST_GET_FW_ATTR 0x06
0037 #define SWI_GET_FW_ATTR_MASK        0x08
0038 
0039 /* atomic counter partially included in MAC address to make sure 2 devices
0040  * do not end up with the same MAC - concept breaks in case of > 255 ifaces
0041  */
0042 static  atomic_t iface_counter = ATOMIC_INIT(0);
0043 
0044 /*
0045  * SYNC Timer Delay definition used to set the expiry time
0046  */
0047 #define SIERRA_NET_SYNCDELAY (2*HZ)
0048 
0049 /* Max. MTU supported. The modem buffers are limited to 1500 */
0050 #define SIERRA_NET_MAX_SUPPORTED_MTU    1500
0051 
0052 /* The SIERRA_NET_USBCTL_BUF_LEN defines a buffer size allocated for control
0053  * message reception ... and thus the max. received packet.
0054  * (May be the cause for parse_hip returning -EINVAL)
0055  */
0056 #define SIERRA_NET_USBCTL_BUF_LEN   1024
0057 
0058 /* Overriding the default usbnet rx_urb_size */
0059 #define SIERRA_NET_RX_URB_SIZE      (8 * 1024)
0060 
0061 /* Private data structure */
0062 struct sierra_net_data {
0063 
0064     u16 link_up;        /* air link up or down */
0065     u8 tx_hdr_template[4];  /* part of HIP hdr for tx'd packets */
0066 
0067     u8 sync_msg[4];     /* SYNC message */
0068     u8 shdwn_msg[4];    /* Shutdown message */
0069 
0070     /* Backpointer to the container */
0071     struct usbnet *usbnet;
0072 
0073     u8 ifnum;   /* interface number */
0074 
0075 /* Bit masks, must be a power of 2 */
0076 #define SIERRA_NET_EVENT_RESP_AVAIL    0x01
0077 #define SIERRA_NET_TIMER_EXPIRY        0x02
0078     unsigned long kevent_flags;
0079     struct work_struct sierra_net_kevent;
0080     struct timer_list sync_timer; /* For retrying SYNC sequence */
0081 };
0082 
0083 struct param {
0084     int is_present;
0085     union {
0086         void  *ptr;
0087         u32    dword;
0088         u16    word;
0089         u8     byte;
0090     };
0091 };
0092 
0093 /* HIP message type */
0094 #define SIERRA_NET_HIP_EXTENDEDID   0x7F
0095 #define SIERRA_NET_HIP_HSYNC_ID     0x60    /* Modem -> host */
0096 #define SIERRA_NET_HIP_RESTART_ID   0x62    /* Modem -> host */
0097 #define SIERRA_NET_HIP_MSYNC_ID     0x20    /* Host -> modem */
0098 #define SIERRA_NET_HIP_SHUTD_ID     0x26    /* Host -> modem */
0099 
0100 #define SIERRA_NET_HIP_EXT_IP_IN_ID   0x0202
0101 #define SIERRA_NET_HIP_EXT_IP_OUT_ID  0x0002
0102 
0103 /* 3G UMTS Link Sense Indication definitions */
0104 #define SIERRA_NET_HIP_LSI_UMTSID   0x78
0105 
0106 /* Reverse Channel Grant Indication HIP message */
0107 #define SIERRA_NET_HIP_RCGI     0x64
0108 
0109 /* LSI Protocol types */
0110 #define SIERRA_NET_PROTOCOL_UMTS      0x01
0111 #define SIERRA_NET_PROTOCOL_UMTS_DS   0x04
0112 /* LSI Coverage */
0113 #define SIERRA_NET_COVERAGE_NONE      0x00
0114 #define SIERRA_NET_COVERAGE_NOPACKET  0x01
0115 
0116 /* LSI Session */
0117 #define SIERRA_NET_SESSION_IDLE       0x00
0118 /* LSI Link types */
0119 #define SIERRA_NET_AS_LINK_TYPE_IPV4  0x00
0120 #define SIERRA_NET_AS_LINK_TYPE_IPV6  0x02
0121 
0122 struct lsi_umts {
0123     u8 protocol;
0124     u8 unused1;
0125     __be16 length;
0126     /* eventually use a union for the rest - assume umts for now */
0127     u8 coverage;
0128     u8 network_len; /* network name len */
0129     u8 network[40]; /* network name (UCS2, bigendian) */
0130     u8 session_state;
0131     u8 unused3[33];
0132 } __packed;
0133 
0134 struct lsi_umts_single {
0135     struct lsi_umts lsi;
0136     u8 link_type;
0137     u8 pdp_addr_len; /* NW-supplied PDP address len */
0138     u8 pdp_addr[16]; /* NW-supplied PDP address (bigendian)) */
0139     u8 unused4[23];
0140     u8 dns1_addr_len; /* NW-supplied 1st DNS address len (bigendian) */
0141     u8 dns1_addr[16]; /* NW-supplied 1st DNS address */
0142     u8 dns2_addr_len; /* NW-supplied 2nd DNS address len */
0143     u8 dns2_addr[16]; /* NW-supplied 2nd DNS address (bigendian)*/
0144     u8 wins1_addr_len; /* NW-supplied 1st Wins address len */
0145     u8 wins1_addr[16]; /* NW-supplied 1st Wins address (bigendian)*/
0146     u8 wins2_addr_len; /* NW-supplied 2nd Wins address len */
0147     u8 wins2_addr[16]; /* NW-supplied 2nd Wins address (bigendian) */
0148     u8 unused5[4];
0149     u8 gw_addr_len; /* NW-supplied GW address len */
0150     u8 gw_addr[16]; /* NW-supplied GW address (bigendian) */
0151     u8 reserved[8];
0152 } __packed;
0153 
0154 struct lsi_umts_dual {
0155     struct lsi_umts lsi;
0156     u8 pdp_addr4_len; /* NW-supplied PDP IPv4 address len */
0157     u8 pdp_addr4[4];  /* NW-supplied PDP IPv4 address (bigendian)) */
0158     u8 pdp_addr6_len; /* NW-supplied PDP IPv6 address len */
0159     u8 pdp_addr6[16]; /* NW-supplied PDP IPv6 address (bigendian)) */
0160     u8 unused4[23];
0161     u8 dns1_addr4_len; /* NW-supplied 1st DNS v4 address len (bigendian) */
0162     u8 dns1_addr4[4];  /* NW-supplied 1st DNS v4 address */
0163     u8 dns1_addr6_len; /* NW-supplied 1st DNS v6 address len */
0164     u8 dns1_addr6[16]; /* NW-supplied 1st DNS v6 address (bigendian)*/
0165     u8 dns2_addr4_len; /* NW-supplied 2nd DNS v4 address len (bigendian) */
0166     u8 dns2_addr4[4];  /* NW-supplied 2nd DNS v4 address */
0167     u8 dns2_addr6_len; /* NW-supplied 2nd DNS v6 address len */
0168     u8 dns2_addr6[16]; /* NW-supplied 2nd DNS v6 address (bigendian)*/
0169     u8 unused5[68];
0170 } __packed;
0171 
0172 #define SIERRA_NET_LSI_COMMON_LEN      4
0173 #define SIERRA_NET_LSI_UMTS_LEN        (sizeof(struct lsi_umts_single))
0174 #define SIERRA_NET_LSI_UMTS_STATUS_LEN \
0175     (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN)
0176 #define SIERRA_NET_LSI_UMTS_DS_LEN     (sizeof(struct lsi_umts_dual))
0177 #define SIERRA_NET_LSI_UMTS_DS_STATUS_LEN \
0178     (SIERRA_NET_LSI_UMTS_DS_LEN - SIERRA_NET_LSI_COMMON_LEN)
0179 
0180 /* Our own net device operations structure */
0181 static const struct net_device_ops sierra_net_device_ops = {
0182     .ndo_open               = usbnet_open,
0183     .ndo_stop               = usbnet_stop,
0184     .ndo_start_xmit         = usbnet_start_xmit,
0185     .ndo_tx_timeout         = usbnet_tx_timeout,
0186     .ndo_change_mtu         = usbnet_change_mtu,
0187     .ndo_get_stats64        = dev_get_tstats64,
0188     .ndo_set_mac_address    = eth_mac_addr,
0189     .ndo_validate_addr      = eth_validate_addr,
0190 };
0191 
0192 /* get private data associated with passed in usbnet device */
0193 static inline struct sierra_net_data *sierra_net_get_private(struct usbnet *dev)
0194 {
0195     return (struct sierra_net_data *)dev->data[0];
0196 }
0197 
0198 /* set private data associated with passed in usbnet device */
0199 static inline void sierra_net_set_private(struct usbnet *dev,
0200             struct sierra_net_data *priv)
0201 {
0202     dev->data[0] = (unsigned long)priv;
0203 }
0204 
0205 /* is packet IPv4/IPv6 */
0206 static inline int is_ip(struct sk_buff *skb)
0207 {
0208     return skb->protocol == cpu_to_be16(ETH_P_IP) ||
0209            skb->protocol == cpu_to_be16(ETH_P_IPV6);
0210 }
0211 
0212 /*
0213  * check passed in packet and make sure that:
0214  *  - it is linear (no scatter/gather)
0215  *  - it is ethernet (mac_header properly set)
0216  */
0217 static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
0218 {
0219     skb_reset_mac_header(skb); /* ethernet header */
0220 
0221     if (skb_is_nonlinear(skb)) {
0222         netdev_err(dev->net, "Non linear buffer-dropping\n");
0223         return 0;
0224     }
0225 
0226     if (!pskb_may_pull(skb, ETH_HLEN))
0227         return 0;
0228     skb->protocol = eth_hdr(skb)->h_proto;
0229 
0230     return 1;
0231 }
0232 
0233 static const u8 *save16bit(struct param *p, const u8 *datap)
0234 {
0235     p->is_present = 1;
0236     p->word = get_unaligned_be16(datap);
0237     return datap + sizeof(p->word);
0238 }
0239 
0240 static const u8 *save8bit(struct param *p, const u8 *datap)
0241 {
0242     p->is_present = 1;
0243     p->byte = *datap;
0244     return datap + sizeof(p->byte);
0245 }
0246 
0247 /*----------------------------------------------------------------------------*
0248  *                              BEGIN HIP                                     *
0249  *----------------------------------------------------------------------------*/
0250 /* HIP header */
0251 #define SIERRA_NET_HIP_HDR_LEN 4
0252 /* Extended HIP header */
0253 #define SIERRA_NET_HIP_EXT_HDR_LEN 6
0254 
0255 struct hip_hdr {
0256     int    hdrlen;
0257     struct param payload_len;
0258     struct param msgid;
0259     struct param msgspecific;
0260     struct param extmsgid;
0261 };
0262 
0263 static int parse_hip(const u8 *buf, const u32 buflen, struct hip_hdr *hh)
0264 {
0265     const u8 *curp = buf;
0266     int    padded;
0267 
0268     if (buflen < SIERRA_NET_HIP_HDR_LEN)
0269         return -EPROTO;
0270 
0271     curp = save16bit(&hh->payload_len, curp);
0272     curp = save8bit(&hh->msgid, curp);
0273     curp = save8bit(&hh->msgspecific, curp);
0274 
0275     padded = hh->msgid.byte & 0x80;
0276     hh->msgid.byte &= 0x7F;         /* 7 bits */
0277 
0278     hh->extmsgid.is_present = (hh->msgid.byte == SIERRA_NET_HIP_EXTENDEDID);
0279     if (hh->extmsgid.is_present) {
0280         if (buflen < SIERRA_NET_HIP_EXT_HDR_LEN)
0281             return -EPROTO;
0282 
0283         hh->payload_len.word &= 0x3FFF; /* 14 bits */
0284 
0285         curp = save16bit(&hh->extmsgid, curp);
0286         hh->extmsgid.word &= 0x03FF;    /* 10 bits */
0287 
0288         hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
0289     } else {
0290         hh->payload_len.word &= 0x07FF; /* 11 bits */
0291         hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
0292     }
0293 
0294     if (padded) {
0295         hh->hdrlen++;
0296         hh->payload_len.word--;
0297     }
0298 
0299     /* if real packet shorter than the claimed length */
0300     if (buflen < (hh->hdrlen + hh->payload_len.word))
0301         return -EINVAL;
0302 
0303     return 0;
0304 }
0305 
0306 static void build_hip(u8 *buf, const u16 payloadlen,
0307         struct sierra_net_data *priv)
0308 {
0309     /* the following doesn't have the full functionality. We
0310      * currently build only one kind of header, so it is faster this way
0311      */
0312     put_unaligned_be16(payloadlen, buf);
0313     memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
0314 }
0315 /*----------------------------------------------------------------------------*
0316  *                              END HIP                                       *
0317  *----------------------------------------------------------------------------*/
0318 
0319 static int sierra_net_send_cmd(struct usbnet *dev,
0320         u8 *cmd, int cmdlen, const char * cmd_name)
0321 {
0322     struct sierra_net_data *priv = sierra_net_get_private(dev);
0323     int  status;
0324 
0325     status = usbnet_write_cmd(dev, USB_CDC_SEND_ENCAPSULATED_COMMAND,
0326                   USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
0327                   0, priv->ifnum, cmd, cmdlen);
0328 
0329     if (status != cmdlen && status != -ENODEV)
0330         netdev_err(dev->net, "Submit %s failed %d\n", cmd_name, status);
0331 
0332     return status;
0333 }
0334 
0335 static int sierra_net_send_sync(struct usbnet *dev)
0336 {
0337     int  status;
0338     struct sierra_net_data *priv = sierra_net_get_private(dev);
0339 
0340     dev_dbg(&dev->udev->dev, "%s", __func__);
0341 
0342     status = sierra_net_send_cmd(dev, priv->sync_msg,
0343             sizeof(priv->sync_msg), "SYNC");
0344 
0345     return status;
0346 }
0347 
0348 static void sierra_net_set_ctx_index(struct sierra_net_data *priv, u8 ctx_ix)
0349 {
0350     dev_dbg(&(priv->usbnet->udev->dev), "%s %d", __func__, ctx_ix);
0351     priv->tx_hdr_template[0] = 0x3F;
0352     priv->tx_hdr_template[1] = ctx_ix;
0353     *((__be16 *)&priv->tx_hdr_template[2]) =
0354         cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID);
0355 }
0356 
0357 static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen)
0358 {
0359     struct lsi_umts *lsi = (struct lsi_umts *)data;
0360     u32 expected_length;
0361 
0362     if (datalen < sizeof(struct lsi_umts_single)) {
0363         netdev_err(dev->net, "%s: Data length %d, exp >= %zu\n",
0364                __func__, datalen, sizeof(struct lsi_umts_single));
0365         return -1;
0366     }
0367 
0368     /* Validate the session state */
0369     if (lsi->session_state == SIERRA_NET_SESSION_IDLE) {
0370         netdev_err(dev->net, "Session idle, 0x%02x\n",
0371                lsi->session_state);
0372         return 0;
0373     }
0374 
0375     /* Validate the protocol  - only support UMTS for now */
0376     if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS) {
0377         struct lsi_umts_single *single = (struct lsi_umts_single *)lsi;
0378 
0379         /* Validate the link type */
0380         if (single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV4 &&
0381             single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV6) {
0382             netdev_err(dev->net, "Link type unsupported: 0x%02x\n",
0383                    single->link_type);
0384             return -1;
0385         }
0386         expected_length = SIERRA_NET_LSI_UMTS_STATUS_LEN;
0387     } else if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS_DS) {
0388         expected_length = SIERRA_NET_LSI_UMTS_DS_STATUS_LEN;
0389     } else {
0390         netdev_err(dev->net, "Protocol unsupported, 0x%02x\n",
0391                lsi->protocol);
0392         return -1;
0393     }
0394 
0395     if (be16_to_cpu(lsi->length) != expected_length) {
0396         netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
0397                __func__, be16_to_cpu(lsi->length), expected_length);
0398         return -1;
0399     }
0400 
0401     /* Validate the coverage */
0402     if (lsi->coverage == SIERRA_NET_COVERAGE_NONE ||
0403         lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) {
0404         netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage);
0405         return 0;
0406     }
0407 
0408     /* Set link_sense true */
0409     return 1;
0410 }
0411 
0412 static void sierra_net_handle_lsi(struct usbnet *dev, char *data,
0413         struct hip_hdr  *hh)
0414 {
0415     struct sierra_net_data *priv = sierra_net_get_private(dev);
0416     int link_up;
0417 
0418     link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen,
0419                     hh->payload_len.word);
0420     if (link_up < 0) {
0421         netdev_err(dev->net, "Invalid LSI\n");
0422         return;
0423     }
0424     if (link_up) {
0425         sierra_net_set_ctx_index(priv, hh->msgspecific.byte);
0426         priv->link_up = 1;
0427     } else {
0428         priv->link_up = 0;
0429     }
0430     usbnet_link_change(dev, link_up, 0);
0431 }
0432 
0433 static void sierra_net_dosync(struct usbnet *dev)
0434 {
0435     int status;
0436     struct sierra_net_data *priv = sierra_net_get_private(dev);
0437 
0438     dev_dbg(&dev->udev->dev, "%s", __func__);
0439 
0440     /* The SIERRA_NET_HIP_MSYNC_ID command appears to request that the
0441      * firmware restart itself.  After restarting, the modem will respond
0442      * with the SIERRA_NET_HIP_RESTART_ID indication.  The driver continues
0443      * sending MSYNC commands every few seconds until it receives the
0444      * RESTART event from the firmware
0445      */
0446 
0447     /* tell modem we are ready */
0448     status = sierra_net_send_sync(dev);
0449     if (status < 0)
0450         netdev_err(dev->net,
0451             "Send SYNC failed, status %d\n", status);
0452     status = sierra_net_send_sync(dev);
0453     if (status < 0)
0454         netdev_err(dev->net,
0455             "Send SYNC failed, status %d\n", status);
0456 
0457     /* Now, start a timer and make sure we get the Restart Indication */
0458     priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
0459     add_timer(&priv->sync_timer);
0460 }
0461 
0462 static void sierra_net_kevent(struct work_struct *work)
0463 {
0464     struct sierra_net_data *priv =
0465         container_of(work, struct sierra_net_data, sierra_net_kevent);
0466     struct usbnet *dev = priv->usbnet;
0467     int  len;
0468     int  err;
0469     u8  *buf;
0470     u8   ifnum;
0471 
0472     if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
0473         clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
0474 
0475         /* Query the modem for the LSI message */
0476         buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
0477         if (!buf)
0478             return;
0479 
0480         ifnum = priv->ifnum;
0481         len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
0482                 USB_CDC_GET_ENCAPSULATED_RESPONSE,
0483                 USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
0484                 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
0485                 USB_CTRL_SET_TIMEOUT);
0486 
0487         if (len < 0) {
0488             netdev_err(dev->net,
0489                 "usb_control_msg failed, status %d\n", len);
0490         } else {
0491             struct hip_hdr  hh;
0492 
0493             dev_dbg(&dev->udev->dev, "%s: Received status message,"
0494                 " %04x bytes", __func__, len);
0495 
0496             err = parse_hip(buf, len, &hh);
0497             if (err) {
0498                 netdev_err(dev->net, "%s: Bad packet,"
0499                     " parse result %d\n", __func__, err);
0500                 kfree(buf);
0501                 return;
0502             }
0503 
0504             /* Validate packet length */
0505             if (len != hh.hdrlen + hh.payload_len.word) {
0506                 netdev_err(dev->net, "%s: Bad packet, received"
0507                     " %d, expected %d\n",   __func__, len,
0508                     hh.hdrlen + hh.payload_len.word);
0509                 kfree(buf);
0510                 return;
0511             }
0512 
0513             /* Switch on received message types */
0514             switch (hh.msgid.byte) {
0515             case SIERRA_NET_HIP_LSI_UMTSID:
0516                 dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
0517                     hh.msgspecific.byte);
0518                 sierra_net_handle_lsi(dev, buf, &hh);
0519                 break;
0520             case SIERRA_NET_HIP_RESTART_ID:
0521                 dev_dbg(&dev->udev->dev, "Restart reported: %d,"
0522                         " stopping sync timer",
0523                         hh.msgspecific.byte);
0524                 /* Got sync resp - stop timer & clear mask */
0525                 del_timer_sync(&priv->sync_timer);
0526                 clear_bit(SIERRA_NET_TIMER_EXPIRY,
0527                       &priv->kevent_flags);
0528                 break;
0529             case SIERRA_NET_HIP_HSYNC_ID:
0530                 dev_dbg(&dev->udev->dev, "SYNC received");
0531                 err = sierra_net_send_sync(dev);
0532                 if (err < 0)
0533                     netdev_err(dev->net,
0534                         "Send SYNC failed %d\n", err);
0535                 break;
0536             case SIERRA_NET_HIP_EXTENDEDID:
0537                 netdev_err(dev->net, "Unrecognized HIP msg, "
0538                     "extmsgid 0x%04x\n", hh.extmsgid.word);
0539                 break;
0540             case SIERRA_NET_HIP_RCGI:
0541                 /* Ignored */
0542                 break;
0543             default:
0544                 netdev_err(dev->net, "Unrecognized HIP msg, "
0545                     "msgid 0x%02x\n", hh.msgid.byte);
0546                 break;
0547             }
0548         }
0549         kfree(buf);
0550     }
0551     /* The sync timer bit might be set */
0552     if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
0553         clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
0554         dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
0555         sierra_net_dosync(priv->usbnet);
0556     }
0557 
0558     if (priv->kevent_flags)
0559         dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
0560             "kevent_flags = 0x%lx", priv->kevent_flags);
0561 }
0562 
0563 static void sierra_net_defer_kevent(struct usbnet *dev, int work)
0564 {
0565     struct sierra_net_data *priv = sierra_net_get_private(dev);
0566 
0567     set_bit(work, &priv->kevent_flags);
0568     schedule_work(&priv->sierra_net_kevent);
0569 }
0570 
0571 /*
0572  * Sync Retransmit Timer Handler. On expiry, kick the work queue
0573  */
0574 static void sierra_sync_timer(struct timer_list *t)
0575 {
0576     struct sierra_net_data *priv = from_timer(priv, t, sync_timer);
0577     struct usbnet *dev = priv->usbnet;
0578 
0579     dev_dbg(&dev->udev->dev, "%s", __func__);
0580     /* Kick the tasklet */
0581     sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
0582 }
0583 
0584 static void sierra_net_status(struct usbnet *dev, struct urb *urb)
0585 {
0586     struct usb_cdc_notification *event;
0587 
0588     dev_dbg(&dev->udev->dev, "%s", __func__);
0589 
0590     if (urb->actual_length < sizeof *event)
0591         return;
0592 
0593     /* Add cases to handle other standard notifications. */
0594     event = urb->transfer_buffer;
0595     switch (event->bNotificationType) {
0596     case USB_CDC_NOTIFY_NETWORK_CONNECTION:
0597     case USB_CDC_NOTIFY_SPEED_CHANGE:
0598         /* USB 305 sends those */
0599         break;
0600     case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
0601         sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
0602         break;
0603     default:
0604         netdev_err(dev->net, ": unexpected notification %02x!\n",
0605                 event->bNotificationType);
0606         break;
0607     }
0608 }
0609 
0610 static void sierra_net_get_drvinfo(struct net_device *net,
0611         struct ethtool_drvinfo *info)
0612 {
0613     /* Inherit standard device info */
0614     usbnet_get_drvinfo(net, info);
0615     strlcpy(info->driver, driver_name, sizeof(info->driver));
0616     strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
0617 }
0618 
0619 static u32 sierra_net_get_link(struct net_device *net)
0620 {
0621     struct usbnet *dev = netdev_priv(net);
0622     /* Report link is down whenever the interface is down */
0623     return sierra_net_get_private(dev)->link_up && netif_running(net);
0624 }
0625 
0626 static const struct ethtool_ops sierra_net_ethtool_ops = {
0627     .get_drvinfo = sierra_net_get_drvinfo,
0628     .get_link = sierra_net_get_link,
0629     .get_msglevel = usbnet_get_msglevel,
0630     .set_msglevel = usbnet_set_msglevel,
0631     .nway_reset = usbnet_nway_reset,
0632     .get_link_ksettings = usbnet_get_link_ksettings_mii,
0633     .set_link_ksettings = usbnet_set_link_ksettings_mii,
0634 };
0635 
0636 static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
0637 {
0638     int result = 0;
0639     __le16 attrdata;
0640 
0641     result = usbnet_read_cmd(dev,
0642                 /* _u8 vendor specific request */
0643                 SWI_USB_REQUEST_GET_FW_ATTR,
0644                 USB_DIR_IN | USB_TYPE_VENDOR,   /* __u8 request type */
0645                 0x0000,     /* __u16 value not used */
0646                 0x0000,     /* __u16 index  not used */
0647                 &attrdata,  /* char *data */
0648                 sizeof(attrdata)    /* __u16 size */
0649                 );
0650 
0651     if (result < 0)
0652         return -EIO;
0653 
0654     *datap = le16_to_cpu(attrdata);
0655     return result;
0656 }
0657 
0658 /*
0659  * collects the bulk endpoints, the status endpoint.
0660  */
0661 static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
0662 {
0663     u8  ifacenum;
0664     u8  numendpoints;
0665     u16 fwattr = 0;
0666     int status;
0667     struct sierra_net_data *priv;
0668     static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
0669         0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
0670     static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
0671         0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
0672     u8 mod[2];
0673 
0674     dev_dbg(&dev->udev->dev, "%s", __func__);
0675 
0676     ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
0677     numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
0678     /* We have three endpoints, bulk in and out, and a status */
0679     if (numendpoints != 3) {
0680         dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
0681             numendpoints);
0682         return -ENODEV;
0683     }
0684     /* Status endpoint set in usbnet_get_endpoints() */
0685     dev->status = NULL;
0686     status = usbnet_get_endpoints(dev, intf);
0687     if (status < 0) {
0688         dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
0689             status);
0690         return -ENODEV;
0691     }
0692     /* Initialize sierra private data */
0693     priv = kzalloc(sizeof *priv, GFP_KERNEL);
0694     if (!priv)
0695         return -ENOMEM;
0696 
0697     priv->usbnet = dev;
0698     priv->ifnum = ifacenum;
0699     dev->net->netdev_ops = &sierra_net_device_ops;
0700 
0701     /* change MAC addr to include, ifacenum, and to be unique */
0702     mod[0] = atomic_inc_return(&iface_counter);
0703     mod[1] = ifacenum;
0704     dev_addr_mod(dev->net, ETH_ALEN - 2, mod, 2);
0705 
0706     /* prepare shutdown message template */
0707     memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
0708     /* set context index initially to 0 - prepares tx hdr template */
0709     sierra_net_set_ctx_index(priv, 0);
0710 
0711     /* prepare sync message template */
0712     memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
0713 
0714     /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */
0715     dev->rx_urb_size  = SIERRA_NET_RX_URB_SIZE;
0716     if (dev->udev->speed != USB_SPEED_HIGH)
0717         dev->rx_urb_size  = min_t(size_t, 4096, SIERRA_NET_RX_URB_SIZE);
0718 
0719     dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
0720     dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
0721     dev->net->max_mtu = SIERRA_NET_MAX_SUPPORTED_MTU;
0722 
0723     /* Set up the netdev */
0724     dev->net->flags |= IFF_NOARP;
0725     dev->net->ethtool_ops = &sierra_net_ethtool_ops;
0726     netif_carrier_off(dev->net);
0727 
0728     sierra_net_set_private(dev, priv);
0729 
0730     priv->kevent_flags = 0;
0731 
0732     /* Use the shared workqueue */
0733     INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
0734 
0735     /* Only need to do this once */
0736     timer_setup(&priv->sync_timer, sierra_sync_timer, 0);
0737 
0738     /* verify fw attributes */
0739     status = sierra_net_get_fw_attr(dev, &fwattr);
0740     dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
0741 
0742     /* test whether firmware supports DHCP */
0743     if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
0744         /* found incompatible firmware version */
0745         dev_err(&dev->udev->dev, "Incompatible driver and firmware"
0746             " versions\n");
0747         kfree(priv);
0748         return -ENODEV;
0749     }
0750 
0751     return 0;
0752 }
0753 
0754 static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
0755 {
0756     int status;
0757     struct sierra_net_data *priv = sierra_net_get_private(dev);
0758 
0759     dev_dbg(&dev->udev->dev, "%s", __func__);
0760 
0761     /* kill the timer and work */
0762     del_timer_sync(&priv->sync_timer);
0763     cancel_work_sync(&priv->sierra_net_kevent);
0764 
0765     /* tell modem we are going away */
0766     status = sierra_net_send_cmd(dev, priv->shdwn_msg,
0767             sizeof(priv->shdwn_msg), "Shutdown");
0768     if (status < 0)
0769         netdev_err(dev->net,
0770             "usb_control_msg failed, status %d\n", status);
0771 
0772     usbnet_status_stop(dev);
0773 
0774     sierra_net_set_private(dev, NULL);
0775     kfree(priv);
0776 }
0777 
0778 static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
0779         struct sk_buff *skb, int len)
0780 {
0781     struct sk_buff *new_skb;
0782 
0783     /* clone skb */
0784     new_skb = skb_clone(skb, GFP_ATOMIC);
0785 
0786     /* remove len bytes from original */
0787     skb_pull(skb, len);
0788 
0789     /* trim next packet to it's length */
0790     if (new_skb) {
0791         skb_trim(new_skb, len);
0792     } else {
0793         if (netif_msg_rx_err(dev))
0794             netdev_err(dev->net, "failed to get skb\n");
0795         dev->net->stats.rx_dropped++;
0796     }
0797 
0798     return new_skb;
0799 }
0800 
0801 /* ---------------------------- Receive data path ----------------------*/
0802 static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
0803 {
0804     int err;
0805     struct hip_hdr  hh;
0806     struct sk_buff *new_skb;
0807 
0808     dev_dbg(&dev->udev->dev, "%s", __func__);
0809 
0810     /* could contain multiple packets */
0811     while (likely(skb->len)) {
0812         err = parse_hip(skb->data, skb->len, &hh);
0813         if (err) {
0814             if (netif_msg_rx_err(dev))
0815                 netdev_err(dev->net, "Invalid HIP header %d\n",
0816                     err);
0817             /* dev->net->stats.rx_errors incremented by caller */
0818             dev->net->stats.rx_length_errors++;
0819             return 0;
0820         }
0821 
0822         /* Validate Extended HIP header */
0823         if (!hh.extmsgid.is_present
0824             || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
0825             if (netif_msg_rx_err(dev))
0826                 netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
0827 
0828             dev->net->stats.rx_frame_errors++;
0829             /* dev->net->stats.rx_errors incremented by caller */
0830             return 0;
0831         }
0832 
0833         skb_pull(skb, hh.hdrlen);
0834 
0835         /* We are going to accept this packet, prepare it.
0836          * In case protocol is IPv6, keep it, otherwise force IPv4.
0837          */
0838         skb_reset_mac_header(skb);
0839         if (eth_hdr(skb)->h_proto != cpu_to_be16(ETH_P_IPV6))
0840             eth_hdr(skb)->h_proto = cpu_to_be16(ETH_P_IP);
0841         eth_zero_addr(eth_hdr(skb)->h_source);
0842         memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
0843 
0844         /* Last packet in batch handled by usbnet */
0845         if (hh.payload_len.word == skb->len)
0846             return 1;
0847 
0848         new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
0849         if (new_skb)
0850             usbnet_skb_return(dev, new_skb);
0851 
0852     } /* while */
0853 
0854     return 0;
0855 }
0856 
0857 /* ---------------------------- Transmit data path ----------------------*/
0858 static struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev,
0859                        struct sk_buff *skb, gfp_t flags)
0860 {
0861     struct sierra_net_data *priv = sierra_net_get_private(dev);
0862     u16 len;
0863     bool need_tail;
0864 
0865     BUILD_BUG_ON(sizeof_field(struct usbnet, data)
0866                 < sizeof(struct cdc_state));
0867 
0868     dev_dbg(&dev->udev->dev, "%s", __func__);
0869     if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
0870         /* enough head room as is? */
0871         if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
0872             /* Save the Eth/IP length and set up HIP hdr */
0873             len = skb->len;
0874             skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
0875             /* Handle ZLP issue */
0876             need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
0877                 % dev->maxpacket == 0);
0878             if (need_tail) {
0879                 if (unlikely(skb_tailroom(skb) == 0)) {
0880                     netdev_err(dev->net, "tx_fixup:"
0881                         "no room for packet\n");
0882                     dev_kfree_skb_any(skb);
0883                     return NULL;
0884                 } else {
0885                     skb->data[skb->len] = 0;
0886                     __skb_put(skb, 1);
0887                     len = len + 1;
0888                 }
0889             }
0890             build_hip(skb->data, len, priv);
0891             return skb;
0892         } else {
0893             /*
0894              * compensate in the future if necessary
0895              */
0896             netdev_err(dev->net, "tx_fixup: no room for HIP\n");
0897         } /* headroom */
0898     }
0899 
0900     if (!priv->link_up)
0901         dev->net->stats.tx_carrier_errors++;
0902 
0903     /* tx_dropped incremented by usbnet */
0904 
0905     /* filter the packet out, release it  */
0906     dev_kfree_skb_any(skb);
0907     return NULL;
0908 }
0909 
0910 static const struct driver_info sierra_net_info_direct_ip = {
0911     .description = "Sierra Wireless USB-to-WWAN Modem",
0912     .flags = FLAG_WWAN | FLAG_SEND_ZLP,
0913     .bind = sierra_net_bind,
0914     .unbind = sierra_net_unbind,
0915     .status = sierra_net_status,
0916     .rx_fixup = sierra_net_rx_fixup,
0917     .tx_fixup = sierra_net_tx_fixup,
0918 };
0919 
0920 static int
0921 sierra_net_probe(struct usb_interface *udev, const struct usb_device_id *prod)
0922 {
0923     int ret;
0924 
0925     ret = usbnet_probe(udev, prod);
0926     if (ret == 0) {
0927         struct usbnet *dev = usb_get_intfdata(udev);
0928 
0929         ret = usbnet_status_start(dev, GFP_KERNEL);
0930         if (ret == 0) {
0931             /* Interrupt URB now set up; initiate sync sequence */
0932             sierra_net_dosync(dev);
0933         }
0934     }
0935     return ret;
0936 }
0937 
0938 #define DIRECT_IP_DEVICE(vend, prod) \
0939     {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 7), \
0940     .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
0941     {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 10), \
0942     .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
0943     {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 11), \
0944     .driver_info = (unsigned long)&sierra_net_info_direct_ip}
0945 
0946 static const struct usb_device_id products[] = {
0947     DIRECT_IP_DEVICE(0x1199, 0x68A3), /* Sierra Wireless USB-to-WWAN modem */
0948     DIRECT_IP_DEVICE(0x0F3D, 0x68A3), /* AT&T Direct IP modem */
0949     DIRECT_IP_DEVICE(0x1199, 0x68AA), /* Sierra Wireless Direct IP LTE modem */
0950     DIRECT_IP_DEVICE(0x0F3D, 0x68AA), /* AT&T Direct IP LTE modem */
0951 
0952     {}, /* last item */
0953 };
0954 MODULE_DEVICE_TABLE(usb, products);
0955 
0956 /* We are based on usbnet, so let it handle the USB driver specifics */
0957 static struct usb_driver sierra_net_driver = {
0958     .name = "sierra_net",
0959     .id_table = products,
0960     .probe = sierra_net_probe,
0961     .disconnect = usbnet_disconnect,
0962     .suspend = usbnet_suspend,
0963     .resume = usbnet_resume,
0964     .no_dynamic_id = 1,
0965     .disable_hub_initiated_lpm = 1,
0966 };
0967 
0968 module_usb_driver(sierra_net_driver);
0969 
0970 MODULE_AUTHOR(DRIVER_AUTHOR);
0971 MODULE_DESCRIPTION(DRIVER_DESC);
0972 MODULE_VERSION(DRIVER_VERSION);
0973 MODULE_LICENSE("GPL");