Merge tag 'staging-3.9-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux/fpc-iii.git] / drivers / net / usb / sierra_net.c
blob79ab2435d9d3906a321332f8e72e961e35c3f1e0
1 /*
2 * USB-to-WWAN Driver for Sierra Wireless modems
4 * Copyright (C) 2008, 2009, 2010 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.2.0"
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 */
34 /*#define DEBUG*/
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>
49 #define SWI_USB_REQUEST_GET_FW_ATTR 0x06
50 #define SWI_GET_FW_ATTR_MASK 0x08
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
55 static atomic_t iface_counter = ATOMIC_INIT(0);
58 * SYNC Timer Delay definition used to set the expiry time
60 #define SIERRA_NET_SYNCDELAY (2*HZ)
62 /* Max. MTU supported. The modem buffers are limited to 1500 */
63 #define SIERRA_NET_MAX_SUPPORTED_MTU 1500
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)
69 #define SIERRA_NET_USBCTL_BUF_LEN 1024
71 /* Overriding the default usbnet rx_urb_size */
72 #define SIERRA_NET_RX_URB_SIZE (8 * 1024)
74 /* Private data structure */
75 struct sierra_net_data {
77 u8 ethr_hdr_tmpl[ETH_HLEN]; /* ethernet header template for rx'd pkts */
79 u16 link_up; /* air link up or down */
80 u8 tx_hdr_template[4]; /* part of HIP hdr for tx'd packets */
82 u8 sync_msg[4]; /* SYNC message */
83 u8 shdwn_msg[4]; /* Shutdown message */
85 /* Backpointer to the container */
86 struct usbnet *usbnet;
88 u8 ifnum; /* interface number */
90 /* Bit masks, must be a power of 2 */
91 #define SIERRA_NET_EVENT_RESP_AVAIL 0x01
92 #define SIERRA_NET_TIMER_EXPIRY 0x02
93 unsigned long kevent_flags;
94 struct work_struct sierra_net_kevent;
95 struct timer_list sync_timer; /* For retrying SYNC sequence */
98 struct param {
99 int is_present;
100 union {
101 void *ptr;
102 u32 dword;
103 u16 word;
104 u8 byte;
108 /* HIP message type */
109 #define SIERRA_NET_HIP_EXTENDEDID 0x7F
110 #define SIERRA_NET_HIP_HSYNC_ID 0x60 /* Modem -> host */
111 #define SIERRA_NET_HIP_RESTART_ID 0x62 /* Modem -> host */
112 #define SIERRA_NET_HIP_MSYNC_ID 0x20 /* Host -> modem */
113 #define SIERRA_NET_HIP_SHUTD_ID 0x26 /* Host -> modem */
115 #define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
116 #define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
118 /* 3G UMTS Link Sense Indication definitions */
119 #define SIERRA_NET_HIP_LSI_UMTSID 0x78
121 /* Reverse Channel Grant Indication HIP message */
122 #define SIERRA_NET_HIP_RCGI 0x64
124 /* LSI Protocol types */
125 #define SIERRA_NET_PROTOCOL_UMTS 0x01
126 /* LSI Coverage */
127 #define SIERRA_NET_COVERAGE_NONE 0x00
128 #define SIERRA_NET_COVERAGE_NOPACKET 0x01
130 /* LSI Session */
131 #define SIERRA_NET_SESSION_IDLE 0x00
132 /* LSI Link types */
133 #define SIERRA_NET_AS_LINK_TYPE_IPv4 0x00
135 struct lsi_umts {
136 u8 protocol;
137 u8 unused1;
138 __be16 length;
139 /* eventually use a union for the rest - assume umts for now */
140 u8 coverage;
141 u8 unused2[41];
142 u8 session_state;
143 u8 unused3[33];
144 u8 link_type;
145 u8 pdp_addr_len; /* NW-supplied PDP address len */
146 u8 pdp_addr[16]; /* NW-supplied PDP address (bigendian)) */
147 u8 unused4[23];
148 u8 dns1_addr_len; /* NW-supplied 1st DNS address len (bigendian) */
149 u8 dns1_addr[16]; /* NW-supplied 1st DNS address */
150 u8 dns2_addr_len; /* NW-supplied 2nd DNS address len */
151 u8 dns2_addr[16]; /* NW-supplied 2nd DNS address (bigendian)*/
152 u8 wins1_addr_len; /* NW-supplied 1st Wins address len */
153 u8 wins1_addr[16]; /* NW-supplied 1st Wins address (bigendian)*/
154 u8 wins2_addr_len; /* NW-supplied 2nd Wins address len */
155 u8 wins2_addr[16]; /* NW-supplied 2nd Wins address (bigendian) */
156 u8 unused5[4];
157 u8 gw_addr_len; /* NW-supplied GW address len */
158 u8 gw_addr[16]; /* NW-supplied GW address (bigendian) */
159 u8 reserved[8];
160 } __packed;
162 #define SIERRA_NET_LSI_COMMON_LEN 4
163 #define SIERRA_NET_LSI_UMTS_LEN (sizeof(struct lsi_umts))
164 #define SIERRA_NET_LSI_UMTS_STATUS_LEN \
165 (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN)
167 /* Forward definitions */
168 static void sierra_sync_timer(unsigned long syncdata);
169 static int sierra_net_change_mtu(struct net_device *net, int new_mtu);
171 /* Our own net device operations structure */
172 static const struct net_device_ops sierra_net_device_ops = {
173 .ndo_open = usbnet_open,
174 .ndo_stop = usbnet_stop,
175 .ndo_start_xmit = usbnet_start_xmit,
176 .ndo_tx_timeout = usbnet_tx_timeout,
177 .ndo_change_mtu = sierra_net_change_mtu,
178 .ndo_set_mac_address = eth_mac_addr,
179 .ndo_validate_addr = eth_validate_addr,
182 /* get private data associated with passed in usbnet device */
183 static inline struct sierra_net_data *sierra_net_get_private(struct usbnet *dev)
185 return (struct sierra_net_data *)dev->data[0];
188 /* set private data associated with passed in usbnet device */
189 static inline void sierra_net_set_private(struct usbnet *dev,
190 struct sierra_net_data *priv)
192 dev->data[0] = (unsigned long)priv;
195 /* is packet IPv4 */
196 static inline int is_ip(struct sk_buff *skb)
198 return skb->protocol == cpu_to_be16(ETH_P_IP);
202 * check passed in packet and make sure that:
203 * - it is linear (no scatter/gather)
204 * - it is ethernet (mac_header properly set)
206 static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
208 skb_reset_mac_header(skb); /* ethernet header */
210 if (skb_is_nonlinear(skb)) {
211 netdev_err(dev->net, "Non linear buffer-dropping\n");
212 return 0;
215 if (!pskb_may_pull(skb, ETH_HLEN))
216 return 0;
217 skb->protocol = eth_hdr(skb)->h_proto;
219 return 1;
222 static const u8 *save16bit(struct param *p, const u8 *datap)
224 p->is_present = 1;
225 p->word = get_unaligned_be16(datap);
226 return datap + sizeof(p->word);
229 static const u8 *save8bit(struct param *p, const u8 *datap)
231 p->is_present = 1;
232 p->byte = *datap;
233 return datap + sizeof(p->byte);
236 /*----------------------------------------------------------------------------*
237 * BEGIN HIP *
238 *----------------------------------------------------------------------------*/
239 /* HIP header */
240 #define SIERRA_NET_HIP_HDR_LEN 4
241 /* Extended HIP header */
242 #define SIERRA_NET_HIP_EXT_HDR_LEN 6
244 struct hip_hdr {
245 int hdrlen;
246 struct param payload_len;
247 struct param msgid;
248 struct param msgspecific;
249 struct param extmsgid;
252 static int parse_hip(const u8 *buf, const u32 buflen, struct hip_hdr *hh)
254 const u8 *curp = buf;
255 int padded;
257 if (buflen < SIERRA_NET_HIP_HDR_LEN)
258 return -EPROTO;
260 curp = save16bit(&hh->payload_len, curp);
261 curp = save8bit(&hh->msgid, curp);
262 curp = save8bit(&hh->msgspecific, curp);
264 padded = hh->msgid.byte & 0x80;
265 hh->msgid.byte &= 0x7F; /* 7 bits */
267 hh->extmsgid.is_present = (hh->msgid.byte == SIERRA_NET_HIP_EXTENDEDID);
268 if (hh->extmsgid.is_present) {
269 if (buflen < SIERRA_NET_HIP_EXT_HDR_LEN)
270 return -EPROTO;
272 hh->payload_len.word &= 0x3FFF; /* 14 bits */
274 curp = save16bit(&hh->extmsgid, curp);
275 hh->extmsgid.word &= 0x03FF; /* 10 bits */
277 hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
278 } else {
279 hh->payload_len.word &= 0x07FF; /* 11 bits */
280 hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
283 if (padded) {
284 hh->hdrlen++;
285 hh->payload_len.word--;
288 /* if real packet shorter than the claimed length */
289 if (buflen < (hh->hdrlen + hh->payload_len.word))
290 return -EINVAL;
292 return 0;
295 static void build_hip(u8 *buf, const u16 payloadlen,
296 struct sierra_net_data *priv)
298 /* the following doesn't have the full functionality. We
299 * currently build only one kind of header, so it is faster this way
301 put_unaligned_be16(payloadlen, buf);
302 memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
304 /*----------------------------------------------------------------------------*
305 * END HIP *
306 *----------------------------------------------------------------------------*/
308 static int sierra_net_send_cmd(struct usbnet *dev,
309 u8 *cmd, int cmdlen, const char * cmd_name)
311 struct sierra_net_data *priv = sierra_net_get_private(dev);
312 int status;
314 status = usbnet_write_cmd(dev, USB_CDC_SEND_ENCAPSULATED_COMMAND,
315 USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
316 0, priv->ifnum, cmd, cmdlen);
318 if (status != cmdlen && status != -ENODEV)
319 netdev_err(dev->net, "Submit %s failed %d\n", cmd_name, status);
321 return status;
324 static int sierra_net_send_sync(struct usbnet *dev)
326 int status;
327 struct sierra_net_data *priv = sierra_net_get_private(dev);
329 dev_dbg(&dev->udev->dev, "%s", __func__);
331 status = sierra_net_send_cmd(dev, priv->sync_msg,
332 sizeof(priv->sync_msg), "SYNC");
334 return status;
337 static void sierra_net_set_ctx_index(struct sierra_net_data *priv, u8 ctx_ix)
339 dev_dbg(&(priv->usbnet->udev->dev), "%s %d", __func__, ctx_ix);
340 priv->tx_hdr_template[0] = 0x3F;
341 priv->tx_hdr_template[1] = ctx_ix;
342 *((__be16 *)&priv->tx_hdr_template[2]) =
343 cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID);
346 static inline int sierra_net_is_valid_addrlen(u8 len)
348 return len == sizeof(struct in_addr);
351 static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen)
353 struct lsi_umts *lsi = (struct lsi_umts *)data;
355 if (datalen < sizeof(struct lsi_umts)) {
356 netdev_err(dev->net, "%s: Data length %d, exp %Zu\n",
357 __func__, datalen,
358 sizeof(struct lsi_umts));
359 return -1;
362 if (lsi->length != cpu_to_be16(SIERRA_NET_LSI_UMTS_STATUS_LEN)) {
363 netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
364 __func__, be16_to_cpu(lsi->length),
365 (u32)SIERRA_NET_LSI_UMTS_STATUS_LEN);
366 return -1;
369 /* Validate the protocol - only support UMTS for now */
370 if (lsi->protocol != SIERRA_NET_PROTOCOL_UMTS) {
371 netdev_err(dev->net, "Protocol unsupported, 0x%02x\n",
372 lsi->protocol);
373 return -1;
376 /* Validate the link type */
377 if (lsi->link_type != SIERRA_NET_AS_LINK_TYPE_IPv4) {
378 netdev_err(dev->net, "Link type unsupported: 0x%02x\n",
379 lsi->link_type);
380 return -1;
383 /* Validate the coverage */
384 if (lsi->coverage == SIERRA_NET_COVERAGE_NONE
385 || lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) {
386 netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage);
387 return 0;
390 /* Validate the session state */
391 if (lsi->session_state == SIERRA_NET_SESSION_IDLE) {
392 netdev_err(dev->net, "Session idle, 0x%02x\n",
393 lsi->session_state);
394 return 0;
397 /* Set link_sense true */
398 return 1;
401 static void sierra_net_handle_lsi(struct usbnet *dev, char *data,
402 struct hip_hdr *hh)
404 struct sierra_net_data *priv = sierra_net_get_private(dev);
405 int link_up;
407 link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen,
408 hh->payload_len.word);
409 if (link_up < 0) {
410 netdev_err(dev->net, "Invalid LSI\n");
411 return;
413 if (link_up) {
414 sierra_net_set_ctx_index(priv, hh->msgspecific.byte);
415 priv->link_up = 1;
416 netif_carrier_on(dev->net);
417 } else {
418 priv->link_up = 0;
419 netif_carrier_off(dev->net);
423 static void sierra_net_dosync(struct usbnet *dev)
425 int status;
426 struct sierra_net_data *priv = sierra_net_get_private(dev);
428 dev_dbg(&dev->udev->dev, "%s", __func__);
430 /* tell modem we are ready */
431 status = sierra_net_send_sync(dev);
432 if (status < 0)
433 netdev_err(dev->net,
434 "Send SYNC failed, status %d\n", status);
435 status = sierra_net_send_sync(dev);
436 if (status < 0)
437 netdev_err(dev->net,
438 "Send SYNC failed, status %d\n", status);
440 /* Now, start a timer and make sure we get the Restart Indication */
441 priv->sync_timer.function = sierra_sync_timer;
442 priv->sync_timer.data = (unsigned long) dev;
443 priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
444 add_timer(&priv->sync_timer);
447 static void sierra_net_kevent(struct work_struct *work)
449 struct sierra_net_data *priv =
450 container_of(work, struct sierra_net_data, sierra_net_kevent);
451 struct usbnet *dev = priv->usbnet;
452 int len;
453 int err;
454 u8 *buf;
455 u8 ifnum;
457 if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
458 clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
460 /* Query the modem for the LSI message */
461 buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
462 if (!buf)
463 return;
465 ifnum = priv->ifnum;
466 len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
467 USB_CDC_GET_ENCAPSULATED_RESPONSE,
468 USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
469 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
470 USB_CTRL_SET_TIMEOUT);
472 if (len < 0) {
473 netdev_err(dev->net,
474 "usb_control_msg failed, status %d\n", len);
475 } else {
476 struct hip_hdr hh;
478 dev_dbg(&dev->udev->dev, "%s: Received status message,"
479 " %04x bytes", __func__, len);
481 err = parse_hip(buf, len, &hh);
482 if (err) {
483 netdev_err(dev->net, "%s: Bad packet,"
484 " parse result %d\n", __func__, err);
485 kfree(buf);
486 return;
489 /* Validate packet length */
490 if (len != hh.hdrlen + hh.payload_len.word) {
491 netdev_err(dev->net, "%s: Bad packet, received"
492 " %d, expected %d\n", __func__, len,
493 hh.hdrlen + hh.payload_len.word);
494 kfree(buf);
495 return;
498 /* Switch on received message types */
499 switch (hh.msgid.byte) {
500 case SIERRA_NET_HIP_LSI_UMTSID:
501 dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
502 hh.msgspecific.byte);
503 sierra_net_handle_lsi(dev, buf, &hh);
504 break;
505 case SIERRA_NET_HIP_RESTART_ID:
506 dev_dbg(&dev->udev->dev, "Restart reported: %d,"
507 " stopping sync timer",
508 hh.msgspecific.byte);
509 /* Got sync resp - stop timer & clear mask */
510 del_timer_sync(&priv->sync_timer);
511 clear_bit(SIERRA_NET_TIMER_EXPIRY,
512 &priv->kevent_flags);
513 break;
514 case SIERRA_NET_HIP_HSYNC_ID:
515 dev_dbg(&dev->udev->dev, "SYNC received");
516 err = sierra_net_send_sync(dev);
517 if (err < 0)
518 netdev_err(dev->net,
519 "Send SYNC failed %d\n", err);
520 break;
521 case SIERRA_NET_HIP_EXTENDEDID:
522 netdev_err(dev->net, "Unrecognized HIP msg, "
523 "extmsgid 0x%04x\n", hh.extmsgid.word);
524 break;
525 case SIERRA_NET_HIP_RCGI:
526 /* Ignored */
527 break;
528 default:
529 netdev_err(dev->net, "Unrecognized HIP msg, "
530 "msgid 0x%02x\n", hh.msgid.byte);
531 break;
534 kfree(buf);
536 /* The sync timer bit might be set */
537 if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
538 clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
539 dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
540 sierra_net_dosync(priv->usbnet);
543 if (priv->kevent_flags)
544 dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
545 "kevent_flags = 0x%lx", priv->kevent_flags);
548 static void sierra_net_defer_kevent(struct usbnet *dev, int work)
550 struct sierra_net_data *priv = sierra_net_get_private(dev);
552 set_bit(work, &priv->kevent_flags);
553 schedule_work(&priv->sierra_net_kevent);
557 * Sync Retransmit Timer Handler. On expiry, kick the work queue
559 static void sierra_sync_timer(unsigned long syncdata)
561 struct usbnet *dev = (struct usbnet *)syncdata;
563 dev_dbg(&dev->udev->dev, "%s", __func__);
564 /* Kick the tasklet */
565 sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
568 static void sierra_net_status(struct usbnet *dev, struct urb *urb)
570 struct usb_cdc_notification *event;
572 dev_dbg(&dev->udev->dev, "%s", __func__);
574 if (urb->actual_length < sizeof *event)
575 return;
577 /* Add cases to handle other standard notifications. */
578 event = urb->transfer_buffer;
579 switch (event->bNotificationType) {
580 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
581 case USB_CDC_NOTIFY_SPEED_CHANGE:
582 /* USB 305 sends those */
583 break;
584 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
585 sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
586 break;
587 default:
588 netdev_err(dev->net, ": unexpected notification %02x!\n",
589 event->bNotificationType);
590 break;
594 static void sierra_net_get_drvinfo(struct net_device *net,
595 struct ethtool_drvinfo *info)
597 /* Inherit standard device info */
598 usbnet_get_drvinfo(net, info);
599 strlcpy(info->driver, driver_name, sizeof(info->driver));
600 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
603 static u32 sierra_net_get_link(struct net_device *net)
605 struct usbnet *dev = netdev_priv(net);
606 /* Report link is down whenever the interface is down */
607 return sierra_net_get_private(dev)->link_up && netif_running(net);
610 static const struct ethtool_ops sierra_net_ethtool_ops = {
611 .get_drvinfo = sierra_net_get_drvinfo,
612 .get_link = sierra_net_get_link,
613 .get_msglevel = usbnet_get_msglevel,
614 .set_msglevel = usbnet_set_msglevel,
615 .get_settings = usbnet_get_settings,
616 .set_settings = usbnet_set_settings,
617 .nway_reset = usbnet_nway_reset,
620 /* MTU can not be more than 1500 bytes, enforce it. */
621 static int sierra_net_change_mtu(struct net_device *net, int new_mtu)
623 if (new_mtu > SIERRA_NET_MAX_SUPPORTED_MTU)
624 return -EINVAL;
626 return usbnet_change_mtu(net, new_mtu);
629 static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
631 int result = 0;
632 __le16 attrdata;
634 result = usbnet_read_cmd(dev,
635 /* _u8 vendor specific request */
636 SWI_USB_REQUEST_GET_FW_ATTR,
637 USB_DIR_IN | USB_TYPE_VENDOR, /* __u8 request type */
638 0x0000, /* __u16 value not used */
639 0x0000, /* __u16 index not used */
640 &attrdata, /* char *data */
641 sizeof(attrdata) /* __u16 size */
644 if (result < 0)
645 return -EIO;
647 *datap = le16_to_cpu(attrdata);
648 return result;
652 * collects the bulk endpoints, the status endpoint.
654 static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
656 u8 ifacenum;
657 u8 numendpoints;
658 u16 fwattr = 0;
659 int status;
660 struct ethhdr *eth;
661 struct sierra_net_data *priv;
662 static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
663 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
664 static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
665 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
667 dev_dbg(&dev->udev->dev, "%s", __func__);
669 ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
670 numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
671 /* We have three endpoints, bulk in and out, and a status */
672 if (numendpoints != 3) {
673 dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
674 numendpoints);
675 return -ENODEV;
677 /* Status endpoint set in usbnet_get_endpoints() */
678 dev->status = NULL;
679 status = usbnet_get_endpoints(dev, intf);
680 if (status < 0) {
681 dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
682 status);
683 return -ENODEV;
685 /* Initialize sierra private data */
686 priv = kzalloc(sizeof *priv, GFP_KERNEL);
687 if (!priv)
688 return -ENOMEM;
690 priv->usbnet = dev;
691 priv->ifnum = ifacenum;
692 dev->net->netdev_ops = &sierra_net_device_ops;
694 /* change MAC addr to include, ifacenum, and to be unique */
695 dev->net->dev_addr[ETH_ALEN-2] = atomic_inc_return(&iface_counter);
696 dev->net->dev_addr[ETH_ALEN-1] = ifacenum;
698 /* we will have to manufacture ethernet headers, prepare template */
699 eth = (struct ethhdr *)priv->ethr_hdr_tmpl;
700 memcpy(&eth->h_dest, dev->net->dev_addr, ETH_ALEN);
701 eth->h_proto = cpu_to_be16(ETH_P_IP);
703 /* prepare shutdown message template */
704 memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
705 /* set context index initially to 0 - prepares tx hdr template */
706 sierra_net_set_ctx_index(priv, 0);
708 /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */
709 dev->rx_urb_size = SIERRA_NET_RX_URB_SIZE;
710 if (dev->udev->speed != USB_SPEED_HIGH)
711 dev->rx_urb_size = min_t(size_t, 4096, SIERRA_NET_RX_URB_SIZE);
713 dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
714 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
716 /* Set up the netdev */
717 dev->net->flags |= IFF_NOARP;
718 dev->net->ethtool_ops = &sierra_net_ethtool_ops;
719 netif_carrier_off(dev->net);
721 sierra_net_set_private(dev, priv);
723 priv->kevent_flags = 0;
725 /* Use the shared workqueue */
726 INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
728 /* Only need to do this once */
729 init_timer(&priv->sync_timer);
731 /* verify fw attributes */
732 status = sierra_net_get_fw_attr(dev, &fwattr);
733 dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
735 /* test whether firmware supports DHCP */
736 if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
737 /* found incompatible firmware version */
738 dev_err(&dev->udev->dev, "Incompatible driver and firmware"
739 " versions\n");
740 kfree(priv);
741 return -ENODEV;
743 /* prepare sync message from template */
744 memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
746 /* initiate the sync sequence */
747 sierra_net_dosync(dev);
749 return 0;
752 static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
754 int status;
755 struct sierra_net_data *priv = sierra_net_get_private(dev);
757 dev_dbg(&dev->udev->dev, "%s", __func__);
759 /* kill the timer and work */
760 del_timer_sync(&priv->sync_timer);
761 cancel_work_sync(&priv->sierra_net_kevent);
763 /* tell modem we are going away */
764 status = sierra_net_send_cmd(dev, priv->shdwn_msg,
765 sizeof(priv->shdwn_msg), "Shutdown");
766 if (status < 0)
767 netdev_err(dev->net,
768 "usb_control_msg failed, status %d\n", status);
770 sierra_net_set_private(dev, NULL);
772 kfree(priv);
775 static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
776 struct sk_buff *skb, int len)
778 struct sk_buff *new_skb;
780 /* clone skb */
781 new_skb = skb_clone(skb, GFP_ATOMIC);
783 /* remove len bytes from original */
784 skb_pull(skb, len);
786 /* trim next packet to it's length */
787 if (new_skb) {
788 skb_trim(new_skb, len);
789 } else {
790 if (netif_msg_rx_err(dev))
791 netdev_err(dev->net, "failed to get skb\n");
792 dev->net->stats.rx_dropped++;
795 return new_skb;
798 /* ---------------------------- Receive data path ----------------------*/
799 static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
801 int err;
802 struct hip_hdr hh;
803 struct sk_buff *new_skb;
805 dev_dbg(&dev->udev->dev, "%s", __func__);
807 /* could contain multiple packets */
808 while (likely(skb->len)) {
809 err = parse_hip(skb->data, skb->len, &hh);
810 if (err) {
811 if (netif_msg_rx_err(dev))
812 netdev_err(dev->net, "Invalid HIP header %d\n",
813 err);
814 /* dev->net->stats.rx_errors incremented by caller */
815 dev->net->stats.rx_length_errors++;
816 return 0;
819 /* Validate Extended HIP header */
820 if (!hh.extmsgid.is_present
821 || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
822 if (netif_msg_rx_err(dev))
823 netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
825 dev->net->stats.rx_frame_errors++;
826 /* dev->net->stats.rx_errors incremented by caller */
827 return 0;
830 skb_pull(skb, hh.hdrlen);
832 /* We are going to accept this packet, prepare it */
833 memcpy(skb->data, sierra_net_get_private(dev)->ethr_hdr_tmpl,
834 ETH_HLEN);
836 /* Last packet in batch handled by usbnet */
837 if (hh.payload_len.word == skb->len)
838 return 1;
840 new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
841 if (new_skb)
842 usbnet_skb_return(dev, new_skb);
844 } /* while */
846 return 0;
849 /* ---------------------------- Transmit data path ----------------------*/
850 static struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev,
851 struct sk_buff *skb, gfp_t flags)
853 struct sierra_net_data *priv = sierra_net_get_private(dev);
854 u16 len;
855 bool need_tail;
857 BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet, data)
858 < sizeof(struct cdc_state));
860 dev_dbg(&dev->udev->dev, "%s", __func__);
861 if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
862 /* enough head room as is? */
863 if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
864 /* Save the Eth/IP length and set up HIP hdr */
865 len = skb->len;
866 skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
867 /* Handle ZLP issue */
868 need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
869 % dev->maxpacket == 0);
870 if (need_tail) {
871 if (unlikely(skb_tailroom(skb) == 0)) {
872 netdev_err(dev->net, "tx_fixup:"
873 "no room for packet\n");
874 dev_kfree_skb_any(skb);
875 return NULL;
876 } else {
877 skb->data[skb->len] = 0;
878 __skb_put(skb, 1);
879 len = len + 1;
882 build_hip(skb->data, len, priv);
883 return skb;
884 } else {
886 * compensate in the future if necessary
888 netdev_err(dev->net, "tx_fixup: no room for HIP\n");
889 } /* headroom */
892 if (!priv->link_up)
893 dev->net->stats.tx_carrier_errors++;
895 /* tx_dropped incremented by usbnet */
897 /* filter the packet out, release it */
898 dev_kfree_skb_any(skb);
899 return NULL;
902 static const struct driver_info sierra_net_info_direct_ip = {
903 .description = "Sierra Wireless USB-to-WWAN Modem",
904 .flags = FLAG_WWAN | FLAG_SEND_ZLP,
905 .bind = sierra_net_bind,
906 .unbind = sierra_net_unbind,
907 .status = sierra_net_status,
908 .rx_fixup = sierra_net_rx_fixup,
909 .tx_fixup = sierra_net_tx_fixup,
912 #define DIRECT_IP_DEVICE(vend, prod) \
913 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 7), \
914 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
915 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 10), \
916 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
917 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 11), \
918 .driver_info = (unsigned long)&sierra_net_info_direct_ip}
920 static const struct usb_device_id products[] = {
921 DIRECT_IP_DEVICE(0x1199, 0x68A3), /* Sierra Wireless USB-to-WWAN modem */
922 DIRECT_IP_DEVICE(0x0F3D, 0x68A3), /* AT&T Direct IP modem */
923 DIRECT_IP_DEVICE(0x1199, 0x68AA), /* Sierra Wireless Direct IP LTE modem */
924 DIRECT_IP_DEVICE(0x0F3D, 0x68AA), /* AT&T Direct IP LTE modem */
926 {}, /* last item */
928 MODULE_DEVICE_TABLE(usb, products);
930 /* We are based on usbnet, so let it handle the USB driver specifics */
931 static struct usb_driver sierra_net_driver = {
932 .name = "sierra_net",
933 .id_table = products,
934 .probe = usbnet_probe,
935 .disconnect = usbnet_disconnect,
936 .suspend = usbnet_suspend,
937 .resume = usbnet_resume,
938 .no_dynamic_id = 1,
939 .disable_hub_initiated_lpm = 1,
942 module_usb_driver(sierra_net_driver);
944 MODULE_AUTHOR(DRIVER_AUTHOR);
945 MODULE_DESCRIPTION(DRIVER_DESC);
946 MODULE_VERSION(DRIVER_VERSION);
947 MODULE_LICENSE("GPL");