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