0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
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
0021
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
0040
0041
0042 static atomic_t iface_counter = ATOMIC_INIT(0);
0043
0044
0045
0046
0047 #define SIERRA_NET_SYNCDELAY (2*HZ)
0048
0049
0050 #define SIERRA_NET_MAX_SUPPORTED_MTU 1500
0051
0052
0053
0054
0055
0056 #define SIERRA_NET_USBCTL_BUF_LEN 1024
0057
0058
0059 #define SIERRA_NET_RX_URB_SIZE (8 * 1024)
0060
0061
0062 struct sierra_net_data {
0063
0064 u16 link_up;
0065 u8 tx_hdr_template[4];
0066
0067 u8 sync_msg[4];
0068 u8 shdwn_msg[4];
0069
0070
0071 struct usbnet *usbnet;
0072
0073 u8 ifnum;
0074
0075
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;
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
0094 #define SIERRA_NET_HIP_EXTENDEDID 0x7F
0095 #define SIERRA_NET_HIP_HSYNC_ID 0x60
0096 #define SIERRA_NET_HIP_RESTART_ID 0x62
0097 #define SIERRA_NET_HIP_MSYNC_ID 0x20
0098 #define SIERRA_NET_HIP_SHUTD_ID 0x26
0099
0100 #define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
0101 #define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
0102
0103
0104 #define SIERRA_NET_HIP_LSI_UMTSID 0x78
0105
0106
0107 #define SIERRA_NET_HIP_RCGI 0x64
0108
0109
0110 #define SIERRA_NET_PROTOCOL_UMTS 0x01
0111 #define SIERRA_NET_PROTOCOL_UMTS_DS 0x04
0112
0113 #define SIERRA_NET_COVERAGE_NONE 0x00
0114 #define SIERRA_NET_COVERAGE_NOPACKET 0x01
0115
0116
0117 #define SIERRA_NET_SESSION_IDLE 0x00
0118
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
0127 u8 coverage;
0128 u8 network_len;
0129 u8 network[40];
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;
0138 u8 pdp_addr[16];
0139 u8 unused4[23];
0140 u8 dns1_addr_len;
0141 u8 dns1_addr[16];
0142 u8 dns2_addr_len;
0143 u8 dns2_addr[16];
0144 u8 wins1_addr_len;
0145 u8 wins1_addr[16];
0146 u8 wins2_addr_len;
0147 u8 wins2_addr[16];
0148 u8 unused5[4];
0149 u8 gw_addr_len;
0150 u8 gw_addr[16];
0151 u8 reserved[8];
0152 } __packed;
0153
0154 struct lsi_umts_dual {
0155 struct lsi_umts lsi;
0156 u8 pdp_addr4_len;
0157 u8 pdp_addr4[4];
0158 u8 pdp_addr6_len;
0159 u8 pdp_addr6[16];
0160 u8 unused4[23];
0161 u8 dns1_addr4_len;
0162 u8 dns1_addr4[4];
0163 u8 dns1_addr6_len;
0164 u8 dns1_addr6[16];
0165 u8 dns2_addr4_len;
0166 u8 dns2_addr4[4];
0167 u8 dns2_addr6_len;
0168 u8 dns2_addr6[16];
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
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
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
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
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
0214
0215
0216
0217 static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
0218 {
0219 skb_reset_mac_header(skb);
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
0249
0250
0251 #define SIERRA_NET_HIP_HDR_LEN 4
0252
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;
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;
0284
0285 curp = save16bit(&hh->extmsgid, curp);
0286 hh->extmsgid.word &= 0x03FF;
0287
0288 hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
0289 } else {
0290 hh->payload_len.word &= 0x07FF;
0291 hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
0292 }
0293
0294 if (padded) {
0295 hh->hdrlen++;
0296 hh->payload_len.word--;
0297 }
0298
0299
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
0310
0311
0312 put_unaligned_be16(payloadlen, buf);
0313 memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
0314 }
0315
0316
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
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
0376 if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS) {
0377 struct lsi_umts_single *single = (struct lsi_umts_single *)lsi;
0378
0379
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
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
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
0441
0442
0443
0444
0445
0446
0447
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
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
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
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
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
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
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
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
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
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
0594 event = urb->transfer_buffer;
0595 switch (event->bNotificationType) {
0596 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
0597 case USB_CDC_NOTIFY_SPEED_CHANGE:
0598
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
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
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
0643 SWI_USB_REQUEST_GET_FW_ATTR,
0644 USB_DIR_IN | USB_TYPE_VENDOR,
0645 0x0000,
0646 0x0000,
0647 &attrdata,
0648 sizeof(attrdata)
0649 );
0650
0651 if (result < 0)
0652 return -EIO;
0653
0654 *datap = le16_to_cpu(attrdata);
0655 return result;
0656 }
0657
0658
0659
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
0679 if (numendpoints != 3) {
0680 dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
0681 numendpoints);
0682 return -ENODEV;
0683 }
0684
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
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
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
0707 memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
0708
0709 sierra_net_set_ctx_index(priv, 0);
0710
0711
0712 memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
0713
0714
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
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
0733 INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
0734
0735
0736 timer_setup(&priv->sync_timer, sierra_sync_timer, 0);
0737
0738
0739 status = sierra_net_get_fw_attr(dev, &fwattr);
0740 dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
0741
0742
0743 if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
0744
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
0762 del_timer_sync(&priv->sync_timer);
0763 cancel_work_sync(&priv->sierra_net_kevent);
0764
0765
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
0784 new_skb = skb_clone(skb, GFP_ATOMIC);
0785
0786
0787 skb_pull(skb, len);
0788
0789
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
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
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
0818 dev->net->stats.rx_length_errors++;
0819 return 0;
0820 }
0821
0822
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
0830 return 0;
0831 }
0832
0833 skb_pull(skb, hh.hdrlen);
0834
0835
0836
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
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 }
0853
0854 return 0;
0855 }
0856
0857
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
0871 if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
0872
0873 len = skb->len;
0874 skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
0875
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
0895
0896 netdev_err(dev->net, "tx_fixup: no room for HIP\n");
0897 }
0898 }
0899
0900 if (!priv->link_up)
0901 dev->net->stats.tx_carrier_errors++;
0902
0903
0904
0905
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
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),
0948 DIRECT_IP_DEVICE(0x0F3D, 0x68A3),
0949 DIRECT_IP_DEVICE(0x1199, 0x68AA),
0950 DIRECT_IP_DEVICE(0x0F3D, 0x68AA),
0951
0952 {},
0953 };
0954 MODULE_DEVICE_TABLE(usb, products);
0955
0956
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");