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