1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * USB-to-WWAN Driver for Sierra Wireless modems 4 * 5 * Copyright (C) 2008, 2009, 2010 Paxton Smith, Matthew Safar, Rory Filer 6 * <linux@sierrawireless.com> 7 * 8 * Portions of this based on the cdc_ether driver by David Brownell (2003-2005) 9 * and Ole Andre Vadla Ravnas (ActiveSync) (2006). 10 * 11 * IMPORTANT DISCLAIMER: This driver is not commercially supported by 12 * Sierra Wireless. Use at your own risk. 13 */ 14 15 #define DRIVER_AUTHOR "Paxton Smith, Matthew Safar, Rory Filer" 16 #define DRIVER_DESC "USB-to-WWAN Driver for Sierra Wireless modems" 17 18 /* if defined debug messages enabled */ 19 /*#define DEBUG*/ 20 21 #include <linux/module.h> 22 #include <linux/etherdevice.h> 23 #include <linux/ethtool.h> 24 #include <linux/mii.h> 25 #include <linux/sched.h> 26 #include <linux/timer.h> 27 #include <linux/usb.h> 28 #include <linux/usb/cdc.h> 29 #include <net/ip.h> 30 #include <net/udp.h> 31 #include <linux/unaligned.h> 32 #include <linux/usb/usbnet.h> 33 34 #define SWI_USB_REQUEST_GET_FW_ATTR 0x06 35 #define SWI_GET_FW_ATTR_MASK 0x08 36 37 /* atomic counter partially included in MAC address to make sure 2 devices 38 * do not end up with the same MAC - concept breaks in case of > 255 ifaces 39 */ 40 static atomic_t iface_counter = ATOMIC_INIT(0); 41 42 /* 43 * SYNC Timer Delay definition used to set the expiry time 44 */ 45 #define SIERRA_NET_SYNCDELAY (2*HZ) 46 47 /* Max. MTU supported. The modem buffers are limited to 1500 */ 48 #define SIERRA_NET_MAX_SUPPORTED_MTU 1500 49 50 /* The SIERRA_NET_USBCTL_BUF_LEN defines a buffer size allocated for control 51 * message reception ... and thus the max. received packet. 52 * (May be the cause for parse_hip returning -EINVAL) 53 */ 54 #define SIERRA_NET_USBCTL_BUF_LEN 1024 55 56 /* Overriding the default usbnet rx_urb_size */ 57 #define SIERRA_NET_RX_URB_SIZE (8 * 1024) 58 59 /* Private data structure */ 60 struct sierra_net_data { 61 62 u16 link_up; /* air link up or down */ 63 u8 tx_hdr_template[4]; /* part of HIP hdr for tx'd packets */ 64 65 u8 sync_msg[4]; /* SYNC message */ 66 u8 shdwn_msg[4]; /* Shutdown message */ 67 68 /* Backpointer to the container */ 69 struct usbnet *usbnet; 70 71 u8 ifnum; /* interface number */ 72 73 /* Bit masks, must be a power of 2 */ 74 #define SIERRA_NET_EVENT_RESP_AVAIL 0x01 75 #define SIERRA_NET_TIMER_EXPIRY 0x02 76 unsigned long kevent_flags; 77 struct work_struct sierra_net_kevent; 78 struct timer_list sync_timer; /* For retrying SYNC sequence */ 79 }; 80 81 struct param { 82 int is_present; 83 union { 84 void *ptr; 85 u32 dword; 86 u16 word; 87 u8 byte; 88 }; 89 }; 90 91 /* HIP message type */ 92 #define SIERRA_NET_HIP_EXTENDEDID 0x7F 93 #define SIERRA_NET_HIP_HSYNC_ID 0x60 /* Modem -> host */ 94 #define SIERRA_NET_HIP_RESTART_ID 0x62 /* Modem -> host */ 95 #define SIERRA_NET_HIP_MSYNC_ID 0x20 /* Host -> modem */ 96 #define SIERRA_NET_HIP_SHUTD_ID 0x26 /* Host -> modem */ 97 98 #define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202 99 #define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002 100 101 /* 3G UMTS Link Sense Indication definitions */ 102 #define SIERRA_NET_HIP_LSI_UMTSID 0x78 103 104 /* Reverse Channel Grant Indication HIP message */ 105 #define SIERRA_NET_HIP_RCGI 0x64 106 107 /* LSI Protocol types */ 108 #define SIERRA_NET_PROTOCOL_UMTS 0x01 109 #define SIERRA_NET_PROTOCOL_UMTS_DS 0x04 110 /* LSI Coverage */ 111 #define SIERRA_NET_COVERAGE_NONE 0x00 112 #define SIERRA_NET_COVERAGE_NOPACKET 0x01 113 114 /* LSI Session */ 115 #define SIERRA_NET_SESSION_IDLE 0x00 116 /* LSI Link types */ 117 #define SIERRA_NET_AS_LINK_TYPE_IPV4 0x00 118 #define SIERRA_NET_AS_LINK_TYPE_IPV6 0x02 119 120 struct lsi_umts { 121 u8 protocol; 122 u8 unused1; 123 __be16 length; 124 /* eventually use a union for the rest - assume umts for now */ 125 u8 coverage; 126 u8 network_len; /* network name len */ 127 u8 network[40]; /* network name (UCS2, bigendian) */ 128 u8 session_state; 129 u8 unused3[33]; 130 } __packed; 131 132 struct lsi_umts_single { 133 struct lsi_umts lsi; 134 u8 link_type; 135 u8 pdp_addr_len; /* NW-supplied PDP address len */ 136 u8 pdp_addr[16]; /* NW-supplied PDP address (bigendian)) */ 137 u8 unused4[23]; 138 u8 dns1_addr_len; /* NW-supplied 1st DNS address len (bigendian) */ 139 u8 dns1_addr[16]; /* NW-supplied 1st DNS address */ 140 u8 dns2_addr_len; /* NW-supplied 2nd DNS address len */ 141 u8 dns2_addr[16]; /* NW-supplied 2nd DNS address (bigendian)*/ 142 u8 wins1_addr_len; /* NW-supplied 1st Wins address len */ 143 u8 wins1_addr[16]; /* NW-supplied 1st Wins address (bigendian)*/ 144 u8 wins2_addr_len; /* NW-supplied 2nd Wins address len */ 145 u8 wins2_addr[16]; /* NW-supplied 2nd Wins address (bigendian) */ 146 u8 unused5[4]; 147 u8 gw_addr_len; /* NW-supplied GW address len */ 148 u8 gw_addr[16]; /* NW-supplied GW address (bigendian) */ 149 u8 reserved[8]; 150 } __packed; 151 152 struct lsi_umts_dual { 153 struct lsi_umts lsi; 154 u8 pdp_addr4_len; /* NW-supplied PDP IPv4 address len */ 155 u8 pdp_addr4[4]; /* NW-supplied PDP IPv4 address (bigendian)) */ 156 u8 pdp_addr6_len; /* NW-supplied PDP IPv6 address len */ 157 u8 pdp_addr6[16]; /* NW-supplied PDP IPv6 address (bigendian)) */ 158 u8 unused4[23]; 159 u8 dns1_addr4_len; /* NW-supplied 1st DNS v4 address len (bigendian) */ 160 u8 dns1_addr4[4]; /* NW-supplied 1st DNS v4 address */ 161 u8 dns1_addr6_len; /* NW-supplied 1st DNS v6 address len */ 162 u8 dns1_addr6[16]; /* NW-supplied 1st DNS v6 address (bigendian)*/ 163 u8 dns2_addr4_len; /* NW-supplied 2nd DNS v4 address len (bigendian) */ 164 u8 dns2_addr4[4]; /* NW-supplied 2nd DNS v4 address */ 165 u8 dns2_addr6_len; /* NW-supplied 2nd DNS v6 address len */ 166 u8 dns2_addr6[16]; /* NW-supplied 2nd DNS v6 address (bigendian)*/ 167 u8 unused5[68]; 168 } __packed; 169 170 #define SIERRA_NET_LSI_COMMON_LEN 4 171 #define SIERRA_NET_LSI_UMTS_LEN (sizeof(struct lsi_umts_single)) 172 #define SIERRA_NET_LSI_UMTS_STATUS_LEN \ 173 (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN) 174 #define SIERRA_NET_LSI_UMTS_DS_LEN (sizeof(struct lsi_umts_dual)) 175 #define SIERRA_NET_LSI_UMTS_DS_STATUS_LEN \ 176 (SIERRA_NET_LSI_UMTS_DS_LEN - SIERRA_NET_LSI_COMMON_LEN) 177 178 /* Our own net device operations structure */ 179 static const struct net_device_ops sierra_net_device_ops = { 180 .ndo_open = usbnet_open, 181 .ndo_stop = usbnet_stop, 182 .ndo_start_xmit = usbnet_start_xmit, 183 .ndo_tx_timeout = usbnet_tx_timeout, 184 .ndo_change_mtu = usbnet_change_mtu, 185 .ndo_get_stats64 = dev_get_tstats64, 186 .ndo_set_mac_address = eth_mac_addr, 187 .ndo_validate_addr = eth_validate_addr, 188 }; 189 190 /* get private data associated with passed in usbnet device */ 191 static inline struct sierra_net_data *sierra_net_get_private(struct usbnet *dev) 192 { 193 return (struct sierra_net_data *)dev->data[0]; 194 } 195 196 /* set private data associated with passed in usbnet device */ 197 static inline void sierra_net_set_private(struct usbnet *dev, 198 struct sierra_net_data *priv) 199 { 200 dev->data[0] = (unsigned long)priv; 201 } 202 203 /* is packet IPv4/IPv6 */ 204 static inline int is_ip(struct sk_buff *skb) 205 { 206 return skb->protocol == cpu_to_be16(ETH_P_IP) || 207 skb->protocol == cpu_to_be16(ETH_P_IPV6); 208 } 209 210 /* 211 * check passed in packet and make sure that: 212 * - it is linear (no scatter/gather) 213 * - it is ethernet (mac_header properly set) 214 */ 215 static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev) 216 { 217 skb_reset_mac_header(skb); /* ethernet header */ 218 219 if (skb_is_nonlinear(skb)) { 220 netdev_err(dev->net, "Non linear buffer-dropping\n"); 221 return 0; 222 } 223 224 if (!pskb_may_pull(skb, ETH_HLEN)) 225 return 0; 226 skb->protocol = eth_hdr(skb)->h_proto; 227 228 return 1; 229 } 230 231 static const u8 *save16bit(struct param *p, const u8 *datap) 232 { 233 p->is_present = 1; 234 p->word = get_unaligned_be16(datap); 235 return datap + sizeof(p->word); 236 } 237 238 static const u8 *save8bit(struct param *p, const u8 *datap) 239 { 240 p->is_present = 1; 241 p->byte = *datap; 242 return datap + sizeof(p->byte); 243 } 244 245 /*----------------------------------------------------------------------------* 246 * BEGIN HIP * 247 *----------------------------------------------------------------------------*/ 248 /* HIP header */ 249 #define SIERRA_NET_HIP_HDR_LEN 4 250 /* Extended HIP header */ 251 #define SIERRA_NET_HIP_EXT_HDR_LEN 6 252 253 struct hip_hdr { 254 int hdrlen; 255 struct param payload_len; 256 struct param msgid; 257 struct param msgspecific; 258 struct param extmsgid; 259 }; 260 261 static int parse_hip(const u8 *buf, const u32 buflen, struct hip_hdr *hh) 262 { 263 const u8 *curp = buf; 264 int padded; 265 266 if (buflen < SIERRA_NET_HIP_HDR_LEN) 267 return -EPROTO; 268 269 curp = save16bit(&hh->payload_len, curp); 270 curp = save8bit(&hh->msgid, curp); 271 curp = save8bit(&hh->msgspecific, curp); 272 273 padded = hh->msgid.byte & 0x80; 274 hh->msgid.byte &= 0x7F; /* 7 bits */ 275 276 hh->extmsgid.is_present = (hh->msgid.byte == SIERRA_NET_HIP_EXTENDEDID); 277 if (hh->extmsgid.is_present) { 278 if (buflen < SIERRA_NET_HIP_EXT_HDR_LEN) 279 return -EPROTO; 280 281 hh->payload_len.word &= 0x3FFF; /* 14 bits */ 282 283 curp = save16bit(&hh->extmsgid, curp); 284 hh->extmsgid.word &= 0x03FF; /* 10 bits */ 285 286 hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN; 287 } else { 288 hh->payload_len.word &= 0x07FF; /* 11 bits */ 289 hh->hdrlen = SIERRA_NET_HIP_HDR_LEN; 290 } 291 292 if (padded) { 293 hh->hdrlen++; 294 hh->payload_len.word--; 295 } 296 297 /* if real packet shorter than the claimed length */ 298 if (buflen < (hh->hdrlen + hh->payload_len.word)) 299 return -EINVAL; 300 301 return 0; 302 } 303 304 static void build_hip(u8 *buf, const u16 payloadlen, 305 struct sierra_net_data *priv) 306 { 307 /* the following doesn't have the full functionality. We 308 * currently build only one kind of header, so it is faster this way 309 */ 310 put_unaligned_be16(payloadlen, buf); 311 memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template)); 312 } 313 /*----------------------------------------------------------------------------* 314 * END HIP * 315 *----------------------------------------------------------------------------*/ 316 317 static int sierra_net_send_cmd(struct usbnet *dev, 318 u8 *cmd, int cmdlen, const char * cmd_name) 319 { 320 struct sierra_net_data *priv = sierra_net_get_private(dev); 321 int status; 322 323 status = usbnet_write_cmd(dev, USB_CDC_SEND_ENCAPSULATED_COMMAND, 324 USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE, 325 0, priv->ifnum, cmd, cmdlen); 326 327 if (status != cmdlen && status != -ENODEV) 328 netdev_err(dev->net, "Submit %s failed %d\n", cmd_name, status); 329 330 return status; 331 } 332 333 static int sierra_net_send_sync(struct usbnet *dev) 334 { 335 int status; 336 struct sierra_net_data *priv = sierra_net_get_private(dev); 337 338 dev_dbg(&dev->udev->dev, "%s", __func__); 339 340 status = sierra_net_send_cmd(dev, priv->sync_msg, 341 sizeof(priv->sync_msg), "SYNC"); 342 343 return status; 344 } 345 346 static void sierra_net_set_ctx_index(struct sierra_net_data *priv, u8 ctx_ix) 347 { 348 dev_dbg(&(priv->usbnet->udev->dev), "%s %d", __func__, ctx_ix); 349 priv->tx_hdr_template[0] = 0x3F; 350 priv->tx_hdr_template[1] = ctx_ix; 351 *((__be16 *)&priv->tx_hdr_template[2]) = 352 cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID); 353 } 354 355 static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen) 356 { 357 struct lsi_umts *lsi = (struct lsi_umts *)data; 358 u32 expected_length; 359 360 if (datalen < sizeof(struct lsi_umts_single)) { 361 netdev_err(dev->net, "%s: Data length %d, exp >= %zu\n", 362 __func__, datalen, sizeof(struct lsi_umts_single)); 363 return -1; 364 } 365 366 /* Validate the session state */ 367 if (lsi->session_state == SIERRA_NET_SESSION_IDLE) { 368 netdev_err(dev->net, "Session idle, 0x%02x\n", 369 lsi->session_state); 370 return 0; 371 } 372 373 /* Validate the protocol - only support UMTS for now */ 374 if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS) { 375 struct lsi_umts_single *single = (struct lsi_umts_single *)lsi; 376 377 /* Validate the link type */ 378 if (single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV4 && 379 single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV6) { 380 netdev_err(dev->net, "Link type unsupported: 0x%02x\n", 381 single->link_type); 382 return -1; 383 } 384 expected_length = SIERRA_NET_LSI_UMTS_STATUS_LEN; 385 } else if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS_DS) { 386 expected_length = SIERRA_NET_LSI_UMTS_DS_STATUS_LEN; 387 } else { 388 netdev_err(dev->net, "Protocol unsupported, 0x%02x\n", 389 lsi->protocol); 390 return -1; 391 } 392 393 if (be16_to_cpu(lsi->length) != expected_length) { 394 netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n", 395 __func__, be16_to_cpu(lsi->length), expected_length); 396 return -1; 397 } 398 399 /* Validate the coverage */ 400 if (lsi->coverage == SIERRA_NET_COVERAGE_NONE || 401 lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) { 402 netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage); 403 return 0; 404 } 405 406 /* Set link_sense true */ 407 return 1; 408 } 409 410 static void sierra_net_handle_lsi(struct usbnet *dev, char *data, 411 struct hip_hdr *hh) 412 { 413 struct sierra_net_data *priv = sierra_net_get_private(dev); 414 int link_up; 415 416 link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen, 417 hh->payload_len.word); 418 if (link_up < 0) { 419 netdev_err(dev->net, "Invalid LSI\n"); 420 return; 421 } 422 if (link_up) { 423 sierra_net_set_ctx_index(priv, hh->msgspecific.byte); 424 priv->link_up = 1; 425 } else { 426 priv->link_up = 0; 427 } 428 usbnet_link_change(dev, link_up, 0); 429 } 430 431 static void sierra_net_dosync(struct usbnet *dev) 432 { 433 int status; 434 struct sierra_net_data *priv = sierra_net_get_private(dev); 435 436 dev_dbg(&dev->udev->dev, "%s", __func__); 437 438 /* The SIERRA_NET_HIP_MSYNC_ID command appears to request that the 439 * firmware restart itself. After restarting, the modem will respond 440 * with the SIERRA_NET_HIP_RESTART_ID indication. The driver continues 441 * sending MSYNC commands every few seconds until it receives the 442 * RESTART event from the firmware 443 */ 444 445 /* tell modem we are ready */ 446 status = sierra_net_send_sync(dev); 447 if (status < 0) 448 netdev_err(dev->net, 449 "Send SYNC failed, status %d\n", status); 450 status = sierra_net_send_sync(dev); 451 if (status < 0) 452 netdev_err(dev->net, 453 "Send SYNC failed, status %d\n", status); 454 455 /* Now, start a timer and make sure we get the Restart Indication */ 456 priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY; 457 add_timer(&priv->sync_timer); 458 } 459 460 static void sierra_net_kevent(struct work_struct *work) 461 { 462 struct sierra_net_data *priv = 463 container_of(work, struct sierra_net_data, sierra_net_kevent); 464 struct usbnet *dev = priv->usbnet; 465 int len; 466 int err; 467 u8 *buf; 468 u8 ifnum; 469 470 if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) { 471 clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags); 472 473 /* Query the modem for the LSI message */ 474 buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL); 475 if (!buf) 476 return; 477 478 ifnum = priv->ifnum; 479 len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), 480 USB_CDC_GET_ENCAPSULATED_RESPONSE, 481 USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE, 482 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN, 483 USB_CTRL_SET_TIMEOUT); 484 485 if (len < 0) { 486 netdev_err(dev->net, 487 "usb_control_msg failed, status %d\n", len); 488 } else { 489 struct hip_hdr hh; 490 491 dev_dbg(&dev->udev->dev, "%s: Received status message," 492 " %04x bytes", __func__, len); 493 494 err = parse_hip(buf, len, &hh); 495 if (err) { 496 netdev_err(dev->net, "%s: Bad packet," 497 " parse result %d\n", __func__, err); 498 kfree(buf); 499 return; 500 } 501 502 /* Validate packet length */ 503 if (len != hh.hdrlen + hh.payload_len.word) { 504 netdev_err(dev->net, "%s: Bad packet, received" 505 " %d, expected %d\n", __func__, len, 506 hh.hdrlen + hh.payload_len.word); 507 kfree(buf); 508 return; 509 } 510 511 /* Switch on received message types */ 512 switch (hh.msgid.byte) { 513 case SIERRA_NET_HIP_LSI_UMTSID: 514 dev_dbg(&dev->udev->dev, "LSI for ctx:%d", 515 hh.msgspecific.byte); 516 sierra_net_handle_lsi(dev, buf, &hh); 517 break; 518 case SIERRA_NET_HIP_RESTART_ID: 519 dev_dbg(&dev->udev->dev, "Restart reported: %d," 520 " stopping sync timer", 521 hh.msgspecific.byte); 522 /* Got sync resp - stop timer & clear mask */ 523 timer_delete_sync(&priv->sync_timer); 524 clear_bit(SIERRA_NET_TIMER_EXPIRY, 525 &priv->kevent_flags); 526 break; 527 case SIERRA_NET_HIP_HSYNC_ID: 528 dev_dbg(&dev->udev->dev, "SYNC received"); 529 err = sierra_net_send_sync(dev); 530 if (err < 0) 531 netdev_err(dev->net, 532 "Send SYNC failed %d\n", err); 533 break; 534 case SIERRA_NET_HIP_EXTENDEDID: 535 netdev_err(dev->net, "Unrecognized HIP msg, " 536 "extmsgid 0x%04x\n", hh.extmsgid.word); 537 break; 538 case SIERRA_NET_HIP_RCGI: 539 /* Ignored */ 540 break; 541 default: 542 netdev_err(dev->net, "Unrecognized HIP msg, " 543 "msgid 0x%02x\n", hh.msgid.byte); 544 break; 545 } 546 } 547 kfree(buf); 548 } 549 /* The sync timer bit might be set */ 550 if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) { 551 clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags); 552 dev_dbg(&dev->udev->dev, "Deferred sync timer expiry"); 553 sierra_net_dosync(priv->usbnet); 554 } 555 556 if (priv->kevent_flags) 557 dev_dbg(&dev->udev->dev, "sierra_net_kevent done, " 558 "kevent_flags = 0x%lx", priv->kevent_flags); 559 } 560 561 static void sierra_net_defer_kevent(struct usbnet *dev, int work) 562 { 563 struct sierra_net_data *priv = sierra_net_get_private(dev); 564 565 set_bit(work, &priv->kevent_flags); 566 schedule_work(&priv->sierra_net_kevent); 567 } 568 569 /* 570 * Sync Retransmit Timer Handler. On expiry, kick the work queue 571 */ 572 static void sierra_sync_timer(struct timer_list *t) 573 { 574 struct sierra_net_data *priv = timer_container_of(priv, t, sync_timer); 575 struct usbnet *dev = priv->usbnet; 576 577 dev_dbg(&dev->udev->dev, "%s", __func__); 578 /* Kick the tasklet */ 579 sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY); 580 } 581 582 static void sierra_net_status(struct usbnet *dev, struct urb *urb) 583 { 584 struct usb_cdc_notification *event; 585 586 dev_dbg(&dev->udev->dev, "%s", __func__); 587 588 if (urb->actual_length < sizeof *event) 589 return; 590 591 /* Add cases to handle other standard notifications. */ 592 event = urb->transfer_buffer; 593 switch (event->bNotificationType) { 594 case USB_CDC_NOTIFY_NETWORK_CONNECTION: 595 case USB_CDC_NOTIFY_SPEED_CHANGE: 596 /* USB 305 sends those */ 597 break; 598 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: 599 sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL); 600 break; 601 default: 602 netdev_err(dev->net, ": unexpected notification %02x!\n", 603 event->bNotificationType); 604 break; 605 } 606 } 607 608 static u32 sierra_net_get_link(struct net_device *net) 609 { 610 struct usbnet *dev = netdev_priv(net); 611 /* Report link is down whenever the interface is down */ 612 return sierra_net_get_private(dev)->link_up && netif_running(net); 613 } 614 615 static const struct ethtool_ops sierra_net_ethtool_ops = { 616 .get_drvinfo = usbnet_get_drvinfo, 617 .get_link = sierra_net_get_link, 618 .get_msglevel = usbnet_get_msglevel, 619 .set_msglevel = usbnet_set_msglevel, 620 .nway_reset = usbnet_nway_reset, 621 .get_link_ksettings = usbnet_get_link_ksettings_mii, 622 .set_link_ksettings = usbnet_set_link_ksettings_mii, 623 }; 624 625 static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap) 626 { 627 int result = 0; 628 __le16 attrdata; 629 630 result = usbnet_read_cmd(dev, 631 /* _u8 vendor specific request */ 632 SWI_USB_REQUEST_GET_FW_ATTR, 633 USB_DIR_IN | USB_TYPE_VENDOR, /* __u8 request type */ 634 0x0000, /* __u16 value not used */ 635 0x0000, /* __u16 index not used */ 636 &attrdata, /* char *data */ 637 sizeof(attrdata) /* __u16 size */ 638 ); 639 640 if (result < 0) 641 return -EIO; 642 643 *datap = le16_to_cpu(attrdata); 644 return result; 645 } 646 647 /* 648 * collects the bulk endpoints, the status endpoint. 649 */ 650 static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf) 651 { 652 u8 ifacenum; 653 u8 numendpoints; 654 u16 fwattr = 0; 655 int status; 656 struct sierra_net_data *priv; 657 static const u8 sync_tmplate[sizeof(priv->sync_msg)] = { 658 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00}; 659 static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = { 660 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00}; 661 u8 mod[2]; 662 663 dev_dbg(&dev->udev->dev, "%s", __func__); 664 665 ifacenum = intf->cur_altsetting->desc.bInterfaceNumber; 666 numendpoints = intf->cur_altsetting->desc.bNumEndpoints; 667 /* We have three endpoints, bulk in and out, and a status */ 668 if (numendpoints != 3) { 669 dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d", 670 numendpoints); 671 return -ENODEV; 672 } 673 /* Status endpoint set in usbnet_get_endpoints() */ 674 dev->status = NULL; 675 status = usbnet_get_endpoints(dev, intf); 676 if (status < 0) { 677 dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)", 678 status); 679 return -ENODEV; 680 } 681 if (!dev->status) { 682 dev_err(&dev->udev->dev, "No status endpoint found"); 683 return -ENODEV; 684 } 685 /* Initialize sierra private data */ 686 priv = kzalloc(sizeof *priv, GFP_KERNEL); 687 if (!priv) 688 return -ENOMEM; 689 690 priv->usbnet = dev; 691 priv->ifnum = ifacenum; 692 dev->net->netdev_ops = &sierra_net_device_ops; 693 694 /* change MAC addr to include, ifacenum, and to be unique */ 695 mod[0] = atomic_inc_return(&iface_counter); 696 mod[1] = ifacenum; 697 dev_addr_mod(dev->net, ETH_ALEN - 2, mod, 2); 698 699 /* prepare shutdown message template */ 700 memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg)); 701 /* set context index initially to 0 - prepares tx hdr template */ 702 sierra_net_set_ctx_index(priv, 0); 703 704 /* prepare sync message template */ 705 memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg)); 706 707 /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */ 708 dev->rx_urb_size = SIERRA_NET_RX_URB_SIZE; 709 if (dev->udev->speed != USB_SPEED_HIGH) 710 dev->rx_urb_size = min_t(size_t, 4096, SIERRA_NET_RX_URB_SIZE); 711 712 dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN; 713 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len; 714 dev->net->max_mtu = SIERRA_NET_MAX_SUPPORTED_MTU; 715 716 /* Set up the netdev */ 717 dev->net->flags |= IFF_NOARP; 718 dev->net->ethtool_ops = &sierra_net_ethtool_ops; 719 netif_carrier_off(dev->net); 720 721 sierra_net_set_private(dev, priv); 722 723 priv->kevent_flags = 0; 724 725 /* Use the shared workqueue */ 726 INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent); 727 728 /* Only need to do this once */ 729 timer_setup(&priv->sync_timer, sierra_sync_timer, 0); 730 731 /* verify fw attributes */ 732 status = sierra_net_get_fw_attr(dev, &fwattr); 733 dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr); 734 735 /* test whether firmware supports DHCP */ 736 if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) { 737 /* found incompatible firmware version */ 738 dev_err(&dev->udev->dev, "Incompatible driver and firmware" 739 " versions\n"); 740 kfree(priv); 741 return -ENODEV; 742 } 743 744 return 0; 745 } 746 747 static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf) 748 { 749 int status; 750 struct sierra_net_data *priv = sierra_net_get_private(dev); 751 752 dev_dbg(&dev->udev->dev, "%s", __func__); 753 754 /* kill the timer and work */ 755 timer_shutdown_sync(&priv->sync_timer); 756 cancel_work_sync(&priv->sierra_net_kevent); 757 758 /* tell modem we are going away */ 759 status = sierra_net_send_cmd(dev, priv->shdwn_msg, 760 sizeof(priv->shdwn_msg), "Shutdown"); 761 if (status < 0) 762 netdev_err(dev->net, 763 "usb_control_msg failed, status %d\n", status); 764 765 usbnet_status_stop(dev); 766 767 sierra_net_set_private(dev, NULL); 768 kfree(priv); 769 } 770 771 static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev, 772 struct sk_buff *skb, int len) 773 { 774 struct sk_buff *new_skb; 775 776 /* clone skb */ 777 new_skb = skb_clone(skb, GFP_ATOMIC); 778 779 /* remove len bytes from original */ 780 skb_pull(skb, len); 781 782 /* trim next packet to it's length */ 783 if (new_skb) { 784 skb_trim(new_skb, len); 785 } else { 786 if (netif_msg_rx_err(dev)) 787 netdev_err(dev->net, "failed to get skb\n"); 788 dev->net->stats.rx_dropped++; 789 } 790 791 return new_skb; 792 } 793 794 /* ---------------------------- Receive data path ----------------------*/ 795 static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 796 { 797 int err; 798 struct hip_hdr hh; 799 struct sk_buff *new_skb; 800 801 dev_dbg(&dev->udev->dev, "%s", __func__); 802 803 /* could contain multiple packets */ 804 while (likely(skb->len)) { 805 err = parse_hip(skb->data, skb->len, &hh); 806 if (err) { 807 if (netif_msg_rx_err(dev)) 808 netdev_err(dev->net, "Invalid HIP header %d\n", 809 err); 810 /* dev->net->stats.rx_errors incremented by caller */ 811 dev->net->stats.rx_length_errors++; 812 return 0; 813 } 814 815 /* Validate Extended HIP header */ 816 if (!hh.extmsgid.is_present 817 || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) { 818 if (netif_msg_rx_err(dev)) 819 netdev_err(dev->net, "HIP/ETH: Invalid pkt\n"); 820 821 dev->net->stats.rx_frame_errors++; 822 /* dev->net->stats.rx_errors incremented by caller */ 823 return 0; 824 } 825 826 skb_pull(skb, hh.hdrlen); 827 828 /* We are going to accept this packet, prepare it. 829 * In case protocol is IPv6, keep it, otherwise force IPv4. 830 */ 831 skb_reset_mac_header(skb); 832 if (eth_hdr(skb)->h_proto != cpu_to_be16(ETH_P_IPV6)) 833 eth_hdr(skb)->h_proto = cpu_to_be16(ETH_P_IP); 834 eth_zero_addr(eth_hdr(skb)->h_source); 835 memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN); 836 837 /* Last packet in batch handled by usbnet */ 838 if (hh.payload_len.word == skb->len) 839 return 1; 840 841 new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word); 842 if (new_skb) 843 usbnet_skb_return(dev, new_skb); 844 845 } /* while */ 846 847 return 0; 848 } 849 850 /* ---------------------------- Transmit data path ----------------------*/ 851 static struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev, 852 struct sk_buff *skb, gfp_t flags) 853 { 854 struct sierra_net_data *priv = sierra_net_get_private(dev); 855 u16 len; 856 bool need_tail; 857 858 BUILD_BUG_ON(sizeof_field(struct usbnet, data) 859 < sizeof(struct cdc_state)); 860 861 dev_dbg(&dev->udev->dev, "%s", __func__); 862 if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) { 863 /* enough head room as is? */ 864 if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) { 865 /* Save the Eth/IP length and set up HIP hdr */ 866 len = skb->len; 867 skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN); 868 /* Handle ZLP issue */ 869 need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN) 870 % dev->maxpacket == 0); 871 if (need_tail) { 872 if (unlikely(skb_tailroom(skb) == 0)) { 873 netdev_err(dev->net, "tx_fixup:" 874 "no room for packet\n"); 875 dev_kfree_skb_any(skb); 876 return NULL; 877 } else { 878 skb->data[skb->len] = 0; 879 __skb_put(skb, 1); 880 len = len + 1; 881 } 882 } 883 build_hip(skb->data, len, priv); 884 return skb; 885 } else { 886 /* 887 * compensate in the future if necessary 888 */ 889 netdev_err(dev->net, "tx_fixup: no room for HIP\n"); 890 } /* headroom */ 891 } 892 893 if (!priv->link_up) 894 dev->net->stats.tx_carrier_errors++; 895 896 /* tx_dropped incremented by usbnet */ 897 898 /* filter the packet out, release it */ 899 dev_kfree_skb_any(skb); 900 return NULL; 901 } 902 903 static const struct driver_info sierra_net_info_direct_ip = { 904 .description = "Sierra Wireless USB-to-WWAN Modem", 905 .flags = FLAG_WWAN | FLAG_SEND_ZLP, 906 .bind = sierra_net_bind, 907 .unbind = sierra_net_unbind, 908 .status = sierra_net_status, 909 .rx_fixup = sierra_net_rx_fixup, 910 .tx_fixup = sierra_net_tx_fixup, 911 }; 912 913 static int 914 sierra_net_probe(struct usb_interface *udev, const struct usb_device_id *prod) 915 { 916 int ret; 917 918 ret = usbnet_probe(udev, prod); 919 if (ret == 0) { 920 struct usbnet *dev = usb_get_intfdata(udev); 921 922 ret = usbnet_status_start(dev, GFP_KERNEL); 923 if (ret == 0) { 924 /* Interrupt URB now set up; initiate sync sequence */ 925 sierra_net_dosync(dev); 926 } 927 } 928 return ret; 929 } 930 931 #define DIRECT_IP_DEVICE(vend, prod) \ 932 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 7), \ 933 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \ 934 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 10), \ 935 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \ 936 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 11), \ 937 .driver_info = (unsigned long)&sierra_net_info_direct_ip} 938 939 static const struct usb_device_id products[] = { 940 DIRECT_IP_DEVICE(0x1199, 0x68A3), /* Sierra Wireless USB-to-WWAN modem */ 941 DIRECT_IP_DEVICE(0x0F3D, 0x68A3), /* AT&T Direct IP modem */ 942 DIRECT_IP_DEVICE(0x1199, 0x68AA), /* Sierra Wireless Direct IP LTE modem */ 943 DIRECT_IP_DEVICE(0x0F3D, 0x68AA), /* AT&T Direct IP LTE modem */ 944 945 {}, /* last item */ 946 }; 947 MODULE_DEVICE_TABLE(usb, products); 948 949 /* We are based on usbnet, so let it handle the USB driver specifics */ 950 static struct usb_driver sierra_net_driver = { 951 .name = "sierra_net", 952 .id_table = products, 953 .probe = sierra_net_probe, 954 .disconnect = usbnet_disconnect, 955 .suspend = usbnet_suspend, 956 .resume = usbnet_resume, 957 .no_dynamic_id = 1, 958 .disable_hub_initiated_lpm = 1, 959 }; 960 961 module_usb_driver(sierra_net_driver); 962 963 MODULE_AUTHOR(DRIVER_AUTHOR); 964 MODULE_DESCRIPTION(DRIVER_DESC); 965 MODULE_LICENSE("GPL"); 966