2 * USB-to-WWAN Driver for Sierra Wireless modems
4 * Copyright (C) 2008 - 2011 Paxton Smith, Matthew Safar, Rory Filer
5 * <linux@sierrawireless.com>
7 * Portions of this based on the cdc_ether driver by David Brownell (2003-2005)
8 * and Ole Andre Vadla Ravnas (ActiveSync) (2006).
10 * IMPORTANT DISCLAIMER: This driver is not commercially supported by
11 * Sierra Wireless. Use at your own risk.
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.
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.
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
28 #define DRIVER_VERSION "v.3.2"
29 #define DRIVER_AUTHOR "Paxton Smith, Matthew Safar, Rory Filer"
30 #define DRIVER_DESC "USB-to-WWAN Driver for Sierra Wireless modems"
31 static const char driver_name[] = "sierra_net";
33 /* if defined debug messages enabled */
35 /* more debug messages */
37 /* Uncomment to force power level set to auto when attaching a device */
38 /*#define POWER_LEVEL_AUTO*/
40 #include <linux/module.h>
41 #include <linux/etherdevice.h>
42 #include <linux/ethtool.h>
43 #include <linux/mii.h>
44 #include <linux/sched.h>
45 #include <linux/timer.h>
46 #include <linux/usb.h>
47 #include <linux/usb/cdc.h>
50 #include <asm/unaligned.h>
51 #include <linux/usb/usbnet.h>
53 #define SWI_USB_REQUEST_GET_FW_ATTR 0x06
54 #define SWI_GET_FW_ATTR_MASK 0x08
55 #define SWI_GET_FW_ATTR_APM 0x2
57 /* atomic counter partially included in MAC address to make sure 2 devices
58 * do not end up with the same MAC - concept breaks in case of > 255 ifaces
60 static atomic_t iface_counter = ATOMIC_INIT(0);
63 * SYNC Timer Delay definition used to set the expiry time
65 #define SIERRA_NET_SYNCDELAY (2*HZ)
67 /* Max. MTU supported. The modem buffers are limited to 1500 */
68 #define SIERRA_NET_MAX_SUPPORTED_MTU 1500
70 /* The SIERRA_NET_USBCTL_BUF_LEN defines a buffer size allocated for control
71 * message reception ... and thus the max. received packet.
72 * (May be the cause for parse_hip returning -EINVAL)
74 #define SIERRA_NET_USBCTL_BUF_LEN 1024
76 /* The SIERRA_NET_RX_URB_SZ defines the receive urb size
77 * for optimal throughput.
79 #define SIERRA_NET_RX_URB_SZ 1540
81 /* list of interface numbers - used for constructing interface lists */
82 struct sierra_net_iface_info {
83 const u32 infolen; /* number of interface numbers on list */
84 const u8 *ifaceinfo; /* pointer to the array holding the numbers */
87 struct sierra_net_info_data {
89 struct sierra_net_iface_info whitelist;
92 /* Private data structure */
93 struct sierra_net_data {
95 u8 ethr_hdr_tmpl[ETH_HLEN]; /* ethernet header template for rx'd pkts */
97 u16 link_up; /* air link up or down */
98 u8 tx_hdr_template[4]; /* part of HIP hdr for tx'd packets */
100 u8 sync_msg[4]; /* SYNC message */
101 u8 shdwn_msg[4]; /* Shutdown message */
103 /* Backpointer to the container */
104 struct usbnet *usbnet;
106 u8 ifnum; /* interface number */
108 /* Bit masks, must be a power of 2 */
109 #define SIERRA_NET_EVENT_RESP_AVAIL 0x01
110 #define SIERRA_NET_TIMER_EXPIRY 0x02
111 unsigned long kevent_flags;
112 struct work_struct sierra_net_kevent;
113 struct timer_list sync_timer; /* For retrying SYNC sequence */
126 /* HIP message type */
127 #define SIERRA_NET_HIP_EXTENDEDID 0x7F
128 #define SIERRA_NET_HIP_HSYNC_ID 0x60 /* Modem -> host */
129 #define SIERRA_NET_HIP_RESTART_ID 0x62 /* Modem -> host */
130 #define SIERRA_NET_HIP_MSYNC_ID 0x20 /* Host -> modem */
131 #define SIERRA_NET_HIP_SHUTD_ID 0x26 /* Host -> modem */
133 #define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
134 #define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
136 /* 3G UMTS Link Sense Indication definitions */
137 #define SIERRA_NET_HIP_LSI_UMTSID 0x78
139 /* Reverse Channel Grant Indication HIP message */
140 #define SIERRA_NET_HIP_RCGI 0x64
142 /* LSI Protocol types */
143 #define SIERRA_NET_PROTOCOL_UMTS 0x01
145 #define SIERRA_NET_COVERAGE_NONE 0x00
146 #define SIERRA_NET_COVERAGE_NOPACKET 0x01
149 #define SIERRA_NET_SESSION_IDLE 0x00
151 #define SIERRA_NET_AS_LINK_TYPE_IPv4 0x00
152 #define SIERRA_NET_AS_LINK_TYPE_IPv6 0x02
158 /* eventually use a union for the rest - assume umts for now */
164 u8 pdp_addr_len; /* NW-supplied PDP address len */
165 u8 pdp_addr[16]; /* NW-supplied PDP address (bigendian)) */
167 u8 dns1_addr_len; /* NW-supplied 1st DNS address len (bigendian) */
168 u8 dns1_addr[16]; /* NW-supplied 1st DNS address */
169 u8 dns2_addr_len; /* NW-supplied 2nd DNS address len */
170 u8 dns2_addr[16]; /* NW-supplied 2nd DNS address (bigendian)*/
171 u8 wins1_addr_len; /* NW-supplied 1st Wins address len */
172 u8 wins1_addr[16]; /* NW-supplied 1st Wins address (bigendian)*/
173 u8 wins2_addr_len; /* NW-supplied 2nd Wins address len */
174 u8 wins2_addr[16]; /* NW-supplied 2nd Wins address (bigendian) */
176 u8 gw_addr_len; /* NW-supplied GW address len */
177 u8 gw_addr[16]; /* NW-supplied GW address (bigendian) */
181 #define SIERRA_NET_LSI_COMMON_LEN 4
182 #define SIERRA_NET_LSI_UMTS_LEN (sizeof(struct lsi_umts))
183 #define SIERRA_NET_LSI_UMTS_STATUS_LEN \
184 (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN)
186 /* Forward definitions */
187 static void sierra_sync_timer(unsigned long syncdata);
188 static int sierra_net_change_mtu(struct net_device *net, int new_mtu);
190 /* Our own net device operations structure */
191 static const struct net_device_ops sierra_net_device_ops = {
192 .ndo_open = usbnet_open,
193 .ndo_stop = usbnet_stop,
194 .ndo_start_xmit = usbnet_start_xmit,
195 .ndo_tx_timeout = usbnet_tx_timeout,
196 .ndo_change_mtu = sierra_net_change_mtu,
197 .ndo_set_mac_address = eth_mac_addr,
198 .ndo_validate_addr = eth_validate_addr,
201 /* get private data associated with passed in usbnet device */
202 static inline struct sierra_net_data *sierra_net_get_private(struct usbnet *dev)
204 return (struct sierra_net_data *)dev->data[0];
207 /* set private data associated with passed in usbnet device */
208 static inline void sierra_net_set_private(struct usbnet *dev,
209 struct sierra_net_data *priv)
211 dev->data[0] = (unsigned long)priv;
215 static inline int is_ip(struct sk_buff *skb)
217 return ((skb->protocol == cpu_to_be16(ETH_P_IP)) ||
218 (skb->protocol == cpu_to_be16(ETH_P_IPV6)));
222 * check passed in packet and make sure that:
223 * - it is linear (no scatter/gather)
224 * - it is ethernet (mac_header properly set)
226 static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
228 skb_reset_mac_header(skb); /* ethernet header */
230 if (skb_is_nonlinear(skb)) {
231 netdev_err(dev->net, "Non linear buffer-dropping\n");
235 if (!pskb_may_pull(skb, ETH_HLEN))
237 skb->protocol = eth_hdr(skb)->h_proto;
242 static const u8 *save16bit(struct param *p, const u8 *datap)
245 p->word = get_unaligned_be16(datap);
246 return datap + sizeof(p->word);
249 static const u8 *save8bit(struct param *p, const u8 *datap)
253 return datap + sizeof(p->byte);
256 /*----------------------------------------------------------------------------*
258 *----------------------------------------------------------------------------*/
260 #define SIERRA_NET_HIP_HDR_LEN 4
261 /* Extended HIP header */
262 #define SIERRA_NET_HIP_EXT_HDR_LEN 6
266 struct param payload_len;
268 struct param msgspecific;
269 struct param extmsgid;
272 static int parse_hip(const u8 *buf, const u32 buflen, struct hip_hdr *hh)
274 const u8 *curp = buf;
277 if (buflen < SIERRA_NET_HIP_HDR_LEN)
280 curp = save16bit(&hh->payload_len, curp);
281 curp = save8bit(&hh->msgid, curp);
282 curp = save8bit(&hh->msgspecific, curp);
284 padded = hh->msgid.byte & 0x80;
285 hh->msgid.byte &= 0x7F; /* 7 bits */
287 hh->extmsgid.is_present = (hh->msgid.byte == SIERRA_NET_HIP_EXTENDEDID);
288 if (hh->extmsgid.is_present) {
289 if (buflen < SIERRA_NET_HIP_EXT_HDR_LEN)
292 hh->payload_len.word &= 0x3FFF; /* 14 bits */
294 curp = save16bit(&hh->extmsgid, curp);
295 hh->extmsgid.word &= 0x03FF; /* 10 bits */
297 hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
299 hh->payload_len.word &= 0x07FF; /* 11 bits */
300 hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
305 hh->payload_len.word--;
308 /* if real packet shorter than the claimed length */
309 if (buflen < (hh->hdrlen + hh->payload_len.word))
315 static void build_hip(u8 *buf, const u16 payloadlen,
316 struct sierra_net_data *priv)
318 /* the following doesn't have the full functionality. We
319 * currently build only one kind of header, so it is faster this way
321 put_unaligned_be16(payloadlen, buf);
322 memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
324 /*----------------------------------------------------------------------------*
326 *----------------------------------------------------------------------------*/
327 /* This should come out before going to kernel.org */
328 static void sierra_net_printk_buf(u8 *buf, u16 len)
334 for (i = k = 0; i < len / 4; i++, k += 4) {
335 printk(KERN_DEBUG "%02x%02x%02x%02x ",
336 buf[k+0], buf[k+1], buf[k+2], buf[k+3]);
340 printk(KERN_DEBUG "%02x", buf[k]);
346 static int sierra_net_send_cmd(struct usbnet *dev,
347 u8 *cmd, int cmdlen, const char * cmd_name)
349 struct sierra_net_data *priv = sierra_net_get_private(dev);
352 usb_autopm_get_interface(dev->intf);
353 status = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
354 USB_CDC_SEND_ENCAPSULATED_COMMAND,
355 USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE, 0,
356 priv->ifnum, cmd, cmdlen, USB_CTRL_SET_TIMEOUT);
357 usb_autopm_put_interface(dev->intf);
359 if (status != cmdlen && status != -ENODEV)
360 netdev_err(dev->net, "Submit %s failed %d\n", cmd_name, status);
365 static int sierra_net_send_sync(struct usbnet *dev)
368 struct sierra_net_data *priv = sierra_net_get_private(dev);
370 dev_dbg(&dev->udev->dev, "%s", __func__);
372 status = sierra_net_send_cmd(dev, priv->sync_msg,
373 sizeof(priv->sync_msg), "SYNC");
377 static void sierra_net_send_shutdown(struct usbnet *dev)
379 struct sierra_net_data *priv = sierra_net_get_private(dev);
381 dev_dbg(&dev->udev->dev, "%s", __func__);
383 sierra_net_send_cmd(dev, priv->shdwn_msg,
384 sizeof(priv->shdwn_msg), "Shutdown");
387 static void sierra_net_set_ctx_index(struct sierra_net_data *priv, u8 ctx_ix)
389 dev_dbg(&(priv->usbnet->udev->dev), "%s %d", __func__, ctx_ix);
390 priv->tx_hdr_template[0] = 0x3F;
391 priv->tx_hdr_template[1] = ctx_ix;
392 *((u16 *)&priv->tx_hdr_template[2]) =
393 cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID);
396 static inline int sierra_net_is_valid_addrlen(u8 len)
398 return (len == sizeof(struct in_addr));
401 static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen)
403 struct lsi_umts *lsi = (struct lsi_umts *)data;
405 if (datalen < sizeof(struct lsi_umts)) {
406 netdev_err(dev->net, "%s: Data length %d, exp %Zu\n",
408 sizeof(struct lsi_umts));
412 if (lsi->length != cpu_to_be16(SIERRA_NET_LSI_UMTS_STATUS_LEN)) {
413 netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
414 __func__, be16_to_cpu(lsi->length),
415 (u32)SIERRA_NET_LSI_UMTS_STATUS_LEN);
419 /* Validate the protocol - only support UMTS for now */
420 if (lsi->protocol != SIERRA_NET_PROTOCOL_UMTS) {
421 netdev_err(dev->net, "Protocol unsupported, 0x%02x\n",
426 /* Validate the link type */
427 if ((lsi->link_type != SIERRA_NET_AS_LINK_TYPE_IPv4) &&
428 (lsi->link_type != SIERRA_NET_AS_LINK_TYPE_IPv6)) {
429 netdev_err(dev->net, "Link unavailable: 0x%02x",
434 /* Validate the coverage */
435 if (lsi->coverage == SIERRA_NET_COVERAGE_NONE
436 || lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) {
437 netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage);
441 /* Validate the session state */
442 if (lsi->session_state == SIERRA_NET_SESSION_IDLE) {
443 netdev_err(dev->net, "Session idle, 0x%02x\n",
448 /* Set link_sense true */
452 static void sierra_net_handle_lsi(struct usbnet *dev, char *data,
455 struct sierra_net_data *priv = sierra_net_get_private(dev);
458 link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen,
459 hh->payload_len.word);
461 netdev_err(dev->net, "Invalid LSI\n");
465 sierra_net_set_ctx_index(priv, hh->msgspecific.byte);
467 netif_carrier_on(dev->net);
470 netif_carrier_off(dev->net);
474 static void sierra_net_dosync(struct usbnet *dev)
477 struct sierra_net_data *priv = sierra_net_get_private(dev);
479 dev_dbg(&dev->udev->dev, "%s", __func__);
481 /* tell modem we are ready */
482 status = sierra_net_send_sync(dev);
485 "Send SYNC failed, status %d\n", status);
486 status = sierra_net_send_sync(dev);
489 "Send SYNC failed, status %d\n", status);
491 /* Now, start a timer and make sure we get the Restart Indication */
492 priv->sync_timer.function = sierra_sync_timer;
493 priv->sync_timer.data = (unsigned long) dev;
494 priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
495 add_timer(&priv->sync_timer);
498 static void sierra_net_kevent(struct work_struct *work)
500 struct sierra_net_data *priv =
501 container_of(work, struct sierra_net_data, sierra_net_kevent);
502 struct usbnet *dev = priv->usbnet;
508 if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
509 clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
511 /* Query the modem for the LSI message */
512 buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
515 "failed to allocate buf for LS msg\n");
519 usb_autopm_get_interface(dev->intf);
520 len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
521 USB_CDC_GET_ENCAPSULATED_RESPONSE,
522 USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
523 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
524 USB_CTRL_SET_TIMEOUT);
525 usb_autopm_put_interface(dev->intf);
527 if (unlikely(len < 0)) {
529 "usb_control_msg failed, status %d\n", len);
533 dev_dbg(&dev->udev->dev, "%s: Received status message,"
534 " %04x bytes", __func__, len);
535 sierra_net_printk_buf(buf, len);
537 err = parse_hip(buf, len, &hh);
539 netdev_err(dev->net, "%s: Bad packet,"
540 " parse result %d\n", __func__, err);
545 /* Validate packet length */
546 if (len != hh.hdrlen + hh.payload_len.word) {
547 netdev_err(dev->net, "%s: Bad packet, received"
548 " %d, expected %d\n", __func__, len,
549 hh.hdrlen + hh.payload_len.word);
554 /* Switch on received message types */
555 switch (hh.msgid.byte) {
556 case SIERRA_NET_HIP_LSI_UMTSID:
557 dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
558 hh.msgspecific.byte);
559 sierra_net_handle_lsi(dev, buf, &hh);
561 case SIERRA_NET_HIP_RESTART_ID:
562 dev_dbg(&dev->udev->dev, "Restart reported: %d,"
563 " stopping sync timer",
564 hh.msgspecific.byte);
565 /* Got sync resp - stop timer & clear mask */
566 del_timer_sync(&priv->sync_timer);
567 clear_bit(SIERRA_NET_TIMER_EXPIRY,
568 &priv->kevent_flags);
570 case SIERRA_NET_HIP_HSYNC_ID:
571 dev_dbg(&dev->udev->dev, "SYNC received");
572 err = sierra_net_send_sync(dev);
575 "Send SYNC failed %d\n", err);
577 case SIERRA_NET_HIP_EXTENDEDID:
578 netdev_err(dev->net, "Unrecognized HIP msg, "
579 "extmsgid 0x%04x\n", hh.extmsgid.word);
581 case SIERRA_NET_HIP_RCGI:
582 /* Ignored. It is a firmware
583 * workaround to solve a Windows driver bug and
584 * may be removed in the future.
588 netdev_err(dev->net, "Unrecognized HIP msg, "
589 "msgid 0x%02x\n", hh.msgid.byte);
595 /* The sync timer bit might be set */
596 if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
597 clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
598 dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
599 sierra_net_dosync(priv->usbnet);
602 if (priv->kevent_flags)
603 dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
604 "kevent_flags = 0x%lx", priv->kevent_flags);
607 static void sierra_net_defer_kevent(struct usbnet *dev, int work)
609 struct sierra_net_data *priv = sierra_net_get_private(dev);
611 set_bit(work, &priv->kevent_flags);
612 if (!schedule_work(&priv->sierra_net_kevent))
613 dev_dbg(&dev->udev->dev, "sierra_net_kevent %d may have been dropped", work);
615 dev_dbg(&dev->udev->dev, "sierra_net_kevent %d scheduled", work);
619 * Sync Retransmit Timer Handler. On expiry, kick the work queue
621 void sierra_sync_timer(unsigned long syncdata)
623 struct usbnet *dev = (struct usbnet *)syncdata;
625 dev_dbg(&dev->udev->dev, "%s", __func__);
626 /* Kick the tasklet */
627 sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
630 static void sierra_net_status(struct usbnet *dev, struct urb *urb)
632 struct usb_cdc_notification *event;
634 dev_dbg(&dev->udev->dev, "%s", __func__);
635 sierra_net_printk_buf(urb->transfer_buffer, urb->actual_length);
637 if (urb->actual_length < sizeof *event)
640 /* Add cases to handle other standard notifications. */
641 event = urb->transfer_buffer;
642 switch (event->bNotificationType) {
643 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
644 case USB_CDC_NOTIFY_SPEED_CHANGE:
645 /* USB 305 sends those */
647 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
648 sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
651 netdev_err(dev->net, ": unexpected notification %02x!\n",
652 event->bNotificationType);
657 static void sierra_net_get_drvinfo(struct net_device *net,
658 struct ethtool_drvinfo *info)
660 /* Inherit standard device info */
661 usbnet_get_drvinfo(net, info);
662 strncpy(info->driver, driver_name, sizeof info->driver);
663 strncpy(info->version, DRIVER_VERSION, sizeof info->version);
666 static u32 sierra_net_get_link(struct net_device *net)
668 struct usbnet *dev = netdev_priv(net);
669 /* Report link is down whenever the interface is down */
670 return sierra_net_get_private(dev)->link_up && netif_running(net);
673 static struct ethtool_ops sierra_net_ethtool_ops = {
674 .get_drvinfo = sierra_net_get_drvinfo,
675 .get_link = sierra_net_get_link,
676 .get_msglevel = usbnet_get_msglevel,
677 .set_msglevel = usbnet_set_msglevel,
678 .get_settings = usbnet_get_settings,
679 .set_settings = usbnet_set_settings,
680 .nway_reset = usbnet_nway_reset,
683 /* MTU can not be more than 1500 bytes, enforce it. */
684 static int sierra_net_change_mtu(struct net_device *net, int new_mtu)
686 if (new_mtu > SIERRA_NET_MAX_SUPPORTED_MTU)
689 return usbnet_change_mtu(net, new_mtu);
692 static int is_whitelisted(const u8 ifnum,
693 const struct sierra_net_iface_info *whitelist)
696 const u8 *list = whitelist->ifaceinfo;
699 for (i = 0; i < whitelist->infolen; i++) {
700 if (list[i] == ifnum)
707 static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
712 attrdata = kmalloc(sizeof(*attrdata), GFP_KERNEL);
716 usb_autopm_get_interface(dev->intf);
717 result = usb_control_msg(
719 usb_rcvctrlpipe(dev->udev, 0),
720 /* _u8 vendor specific request */
721 SWI_USB_REQUEST_GET_FW_ATTR,
722 USB_DIR_IN | USB_TYPE_VENDOR, /* __u8 request type */
723 0x0000, /* __u16 value not used */
724 0x0000, /* __u16 index not used */
725 attrdata, /* char *data */
726 sizeof(*attrdata), /* __u16 size */
727 USB_CTRL_SET_TIMEOUT); /* int timeout */
728 usb_autopm_put_interface(dev->intf);
741 static int sierra_net_manage_power(struct usbnet *dev, int on)
743 dev->intf->needs_remote_wakeup = on;
748 * collects the bulk endpoints, the status endpoint.
750 static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
757 struct sierra_net_data *priv;
758 static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
759 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
760 static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
761 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
763 struct sierra_net_info_data *data =
764 (struct sierra_net_info_data *)dev->driver_info->data;
766 dev_dbg(&dev->udev->dev, "%s", __func__);
768 ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
769 /* We only accept certain interfaces */
770 if (!is_whitelisted(ifacenum, &data->whitelist)) {
771 dev_dbg(&dev->udev->dev, "Ignoring interface: %d", ifacenum);
774 numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
775 /* We have three endpoints, bulk in and out, and a status */
776 if (numendpoints != 3) {
777 dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
781 /* Status endpoint set in usbnet_get_endpoints() */
783 status = usbnet_get_endpoints(dev, intf);
785 dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
789 /* Initialize sierra private data */
790 priv = kzalloc(sizeof *priv, GFP_KERNEL);
792 dev_err(&dev->udev->dev, "No memory");
797 priv->ifnum = ifacenum;
798 /* override change_mtu with our own routine - need to bound check */
799 /* replace structure to our own structure where we have our own
800 * routine - need to bound check
802 dev->net->netdev_ops = &sierra_net_device_ops;
804 /* change MAC addr to include, ifacenum, and to be unique */
805 dev->net->dev_addr[ETH_ALEN-2] = atomic_inc_return(&iface_counter);
806 dev->net->dev_addr[ETH_ALEN-1] = ifacenum;
808 /* we will have to manufacture ethernet headers, prepare template */
809 eth = (struct ethhdr *)priv->ethr_hdr_tmpl;
810 memcpy(ð->h_dest, dev->net->dev_addr, ETH_ALEN);
811 eth->h_proto = cpu_to_be16(ETH_P_IP);
813 /* prepare shutdown message template */
814 memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
815 /* set context index initially to 0 - prepares tx hdr template */
816 sierra_net_set_ctx_index(priv, 0);
818 /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */
819 dev->rx_urb_size = data->rx_urb_size;
820 if (dev->udev->speed != USB_SPEED_HIGH)
821 dev->rx_urb_size = min_t(size_t, 4096, data->rx_urb_size);
823 dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
824 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
826 /* Set up the netdev */
827 dev->net->flags |= IFF_NOARP;
828 dev->net->flags |= IFF_MULTICAST;
829 dev->net->ethtool_ops = &sierra_net_ethtool_ops;
830 netif_carrier_off(dev->net);
832 sierra_net_set_private(dev, priv);
834 priv->kevent_flags = 0;
836 /* Use the shared workqueue */
837 INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
839 /* Only need to do this once */
840 init_timer(&priv->sync_timer);
841 /* verify fw attributes */
842 status = sierra_net_get_fw_attr(dev, &fwattr);
843 dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
844 if (status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_APM)) {
845 /*******************************************************************************
846 * If you want the default /sys/bus/usb/devices/.../.../power/level to be forced
847 * to auto, the following needs to be compiled in.
849 #ifdef POWER_LEVEL_AUTO
850 /* make power level default be 'auto' */
851 dev_dbg(&dev->udev->dev, "Enabling APM");
852 usb_enable_autosuspend(dev->udev);
855 dev_info(&intf->dev, "Disabling APM - not supported");
856 usb_disable_autosuspend(dev->udev);
858 /* test whether firmware supports DHCP */
859 if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
860 /* found incompatible firmware version */
861 dev_err(&dev->udev->dev, "Incompatible driver and firmware"
866 /* prepare sync message from template */
867 memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
872 static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
874 struct sierra_net_data *priv = sierra_net_get_private(dev);
876 dev_dbg(&dev->udev->dev, "%s", __func__);
878 sierra_net_set_private(dev, NULL);
883 static int sierra_net_open(struct usbnet *dev)
885 dev_dbg(&dev->udev->dev, "%s", __func__);
887 /* initiate the sync sequence */
888 sierra_net_dosync(dev);
893 static int sierra_net_stop(struct usbnet *dev)
895 struct sierra_net_data *priv = sierra_net_get_private(dev);
897 dev_dbg(&dev->udev->dev, "%s", __func__);
899 /* Kill the timer then flush the work queue */
900 del_timer_sync(&priv->sync_timer);
902 cancel_work_sync(&priv->sierra_net_kevent);
904 /* tell modem we are going away */
905 sierra_net_send_shutdown(dev);
910 static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
911 struct sk_buff *skb, int len)
913 struct sk_buff *new_skb;
916 new_skb = skb_clone(skb, GFP_ATOMIC);
918 /* remove len bytes from original */
921 /* trim next packet to it's length */
923 skb_trim(new_skb, len);
925 if (netif_msg_rx_err(dev))
926 netdev_err(dev->net, "failed to get skb\n");
927 dev->net->stats.rx_dropped++;
933 /* ---------------------------- Receive data path ----------------------*/
934 static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
938 struct sk_buff *new_skb;
942 dev_dbg(&dev->udev->dev, "%s", __func__);
944 sierra_net_printk_buf(skb->data, skb->len);
946 /* could contain multiple packets */
947 while (likely(skb->len)) {
948 err = parse_hip(skb->data, skb->len, &hh);
950 if (netif_msg_rx_err(dev))
951 netdev_err(dev->net, "Invalid HIP header %d\n",
953 /* dev->net->stats.rx_errors incremented by caller */
954 dev->net->stats.rx_length_errors++;
958 /* Validate Extended HIP header */
959 if (!hh.extmsgid.is_present
960 || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
961 if (netif_msg_rx_err(dev))
962 netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
964 dev->net->stats.rx_frame_errors++;
965 /* dev->net->stats.rx_errors incremented by caller */;
969 skb_pull(skb, hh.hdrlen);
971 /* We are going to accept this packet, prepare it */
972 memcpy(skb->data, sierra_net_get_private(dev)->ethr_hdr_tmpl,
975 ip = (struct iphdr *)((char *)skb->data + ETH_HLEN);
976 if(ip->version == 6) {
977 eth = (struct ethhdr *)skb->data;
978 eth->h_proto = cpu_to_be16(ETH_P_IPV6);
981 /* Last packet in batch handled by usbnet */
982 if (hh.payload_len.word == skb->len)
985 new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
987 usbnet_skb_return(dev, new_skb);
994 /* ---------------------------- Transmit data path ----------------------*/
995 struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
998 struct sierra_net_data *priv = sierra_net_get_private(dev);
1002 dev_dbg(&dev->udev->dev, "%s", __func__);
1003 if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
1004 /* enough head room as is? */
1005 if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
1006 /* Save the Eth/IP length and set up HIP hdr */
1008 skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
1009 /* Handle ZLP issue */
1010 need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
1011 % dev->maxpacket == 0);
1013 if (unlikely(skb_tailroom(skb) == 0)) {
1014 netdev_err(dev->net, "tx_fixup:"
1015 "no room for packet\n");
1016 dev_kfree_skb_any(skb);
1019 skb->data[skb->len] = 0;
1024 build_hip(skb->data, len, priv);
1026 sierra_net_printk_buf(skb->data, skb->len);
1030 * compensate in the future if necessary
1032 netdev_err(dev->net, "tx_fixup: no room for HIP\n");
1037 dev->net->stats.tx_carrier_errors++;
1039 /* tx_dropped incremented by usbnet */
1041 /* filter the packet out, release it */
1042 dev_kfree_skb_any(skb);
1046 static int sierra_net_reset_resume(struct usb_interface *intf)
1048 struct usbnet *dev = usb_get_intfdata(intf);
1049 netdev_err(dev->net, "%s\n", __func__);
1050 return usbnet_resume(intf);
1053 static const u8 sierra_net_ifnum_list[] = { 7, 10, 11 };
1054 static const struct sierra_net_info_data sierra_net_info_data_direct_ip = {
1055 /* .rx_urb_size = 8 * 1024, */
1056 .rx_urb_size = SIERRA_NET_RX_URB_SZ,
1058 .infolen = ARRAY_SIZE(sierra_net_ifnum_list),
1059 .ifaceinfo = sierra_net_ifnum_list
1063 static const struct driver_info sierra_net_info_direct_ip = {
1064 .description = "Sierra Wireless USB-to-WWAN Modem",
1065 .flags = FLAG_WWAN | FLAG_SEND_ZLP,
1066 .bind = sierra_net_bind,
1067 .unbind = sierra_net_unbind,
1068 .status = sierra_net_status,
1069 .rx_fixup = sierra_net_rx_fixup,
1070 .tx_fixup = sierra_net_tx_fixup,
1071 .manage_power = sierra_net_manage_power,
1072 .stop = sierra_net_stop,
1073 .check_connect = sierra_net_open,
1074 .data = (unsigned long)&sierra_net_info_data_direct_ip,
1077 static const struct usb_device_id products[] = {
1078 {USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modem */
1079 .driver_info = (unsigned long) &sierra_net_info_direct_ip},
1080 {USB_DEVICE(0xF3D, 0x68A3), /* AT&T Direct IP modem */
1081 .driver_info = (unsigned long) &sierra_net_info_direct_ip},
1082 {USB_DEVICE(0x1199, 0x68AA), /* Sierra Wireless Direct IP LTE modem */
1083 .driver_info = (unsigned long) &sierra_net_info_direct_ip},
1084 {USB_DEVICE(0xF3D, 0x68AA), /* AT&T Direct IP LTE modem */
1085 .driver_info = (unsigned long) &sierra_net_info_direct_ip},
1089 MODULE_DEVICE_TABLE(usb, products);
1091 /* We are based on usbnet, so let it handle the USB driver specifics */
1092 static struct usb_driver sierra_net_driver = {
1093 .name = "sierra_net",
1094 .id_table = products,
1095 .probe = usbnet_probe,
1096 .disconnect = usbnet_disconnect,
1097 .suspend = usbnet_suspend,
1098 .resume = usbnet_resume,
1099 .reset_resume = sierra_net_reset_resume,
1101 .supports_autosuspend = 1,
1104 static int __init sierra_net_init(void)
1106 BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet, data)
1107 < sizeof(struct cdc_state));
1109 return usb_register(&sierra_net_driver);
1112 static void __exit sierra_net_exit(void)
1114 usb_deregister(&sierra_net_driver);
1117 module_exit(sierra_net_exit);
1118 module_init(sierra_net_init);
1120 MODULE_AUTHOR(DRIVER_AUTHOR);
1121 MODULE_DESCRIPTION(DRIVER_DESC);
1122 MODULE_VERSION(DRIVER_VERSION);
1123 MODULE_LICENSE("GPL");