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 */
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>
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 /* list of interface numbers - used for constructing interface lists */
72 struct sierra_net_iface_info
{
73 const u32 infolen
; /* number of interface numbers on list */
74 const u8
*ifaceinfo
; /* pointer to the array holding the numbers */
77 struct sierra_net_info_data
{
79 struct sierra_net_iface_info whitelist
;
82 /* Private data structure */
83 struct sierra_net_data
{
85 u8 ethr_hdr_tmpl
[ETH_HLEN
]; /* ethernet header template for rx'd pkts */
87 u16 link_up
; /* air link up or down */
88 u8 tx_hdr_template
[4]; /* part of HIP hdr for tx'd packets */
90 u8 sync_msg
[4]; /* SYNC message */
91 u8 shdwn_msg
[4]; /* Shutdown message */
93 /* Backpointer to the container */
94 struct usbnet
*usbnet
;
96 u8 ifnum
; /* interface number */
98 /* Bit masks, must be a power of 2 */
99 #define SIERRA_NET_EVENT_RESP_AVAIL 0x01
100 #define SIERRA_NET_TIMER_EXPIRY 0x02
101 unsigned long kevent_flags
;
102 struct work_struct sierra_net_kevent
;
103 struct timer_list sync_timer
; /* For retrying SYNC sequence */
116 /* HIP message type */
117 #define SIERRA_NET_HIP_EXTENDEDID 0x7F
118 #define SIERRA_NET_HIP_HSYNC_ID 0x60 /* Modem -> host */
119 #define SIERRA_NET_HIP_RESTART_ID 0x62 /* Modem -> host */
120 #define SIERRA_NET_HIP_MSYNC_ID 0x20 /* Host -> modem */
121 #define SIERRA_NET_HIP_SHUTD_ID 0x26 /* Host -> modem */
123 #define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
124 #define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
126 /* 3G UMTS Link Sense Indication definitions */
127 #define SIERRA_NET_HIP_LSI_UMTSID 0x78
129 /* Reverse Channel Grant Indication HIP message */
130 #define SIERRA_NET_HIP_RCGI 0x64
132 /* LSI Protocol types */
133 #define SIERRA_NET_PROTOCOL_UMTS 0x01
135 #define SIERRA_NET_COVERAGE_NONE 0x00
136 #define SIERRA_NET_COVERAGE_NOPACKET 0x01
139 #define SIERRA_NET_SESSION_IDLE 0x00
141 #define SIERRA_NET_AS_LINK_TYPE_IPv4 0x00
147 /* eventually use a union for the rest - assume umts for now */
153 u8 pdp_addr_len
; /* NW-supplied PDP address len */
154 u8 pdp_addr
[16]; /* NW-supplied PDP address (bigendian)) */
156 u8 dns1_addr_len
; /* NW-supplied 1st DNS address len (bigendian) */
157 u8 dns1_addr
[16]; /* NW-supplied 1st DNS address */
158 u8 dns2_addr_len
; /* NW-supplied 2nd DNS address len */
159 u8 dns2_addr
[16]; /* NW-supplied 2nd DNS address (bigendian)*/
160 u8 wins1_addr_len
; /* NW-supplied 1st Wins address len */
161 u8 wins1_addr
[16]; /* NW-supplied 1st Wins address (bigendian)*/
162 u8 wins2_addr_len
; /* NW-supplied 2nd Wins address len */
163 u8 wins2_addr
[16]; /* NW-supplied 2nd Wins address (bigendian) */
165 u8 gw_addr_len
; /* NW-supplied GW address len */
166 u8 gw_addr
[16]; /* NW-supplied GW address (bigendian) */
170 #define SIERRA_NET_LSI_COMMON_LEN 4
171 #define SIERRA_NET_LSI_UMTS_LEN (sizeof(struct lsi_umts))
172 #define SIERRA_NET_LSI_UMTS_STATUS_LEN \
173 (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN)
175 /* Forward definitions */
176 static void sierra_sync_timer(unsigned long syncdata
);
177 static int sierra_net_change_mtu(struct net_device
*net
, int new_mtu
);
179 /* Our own net device operations structure */
180 static const struct net_device_ops sierra_net_device_ops
= {
181 .ndo_open
= usbnet_open
,
182 .ndo_stop
= usbnet_stop
,
183 .ndo_start_xmit
= usbnet_start_xmit
,
184 .ndo_tx_timeout
= usbnet_tx_timeout
,
185 .ndo_change_mtu
= sierra_net_change_mtu
,
186 .ndo_set_mac_address
= eth_mac_addr
,
187 .ndo_validate_addr
= eth_validate_addr
,
190 /* get private data associated with passed in usbnet device */
191 static inline struct sierra_net_data
*sierra_net_get_private(struct usbnet
*dev
)
193 return (struct sierra_net_data
*)dev
->data
[0];
196 /* set private data associated with passed in usbnet device */
197 static inline void sierra_net_set_private(struct usbnet
*dev
,
198 struct sierra_net_data
*priv
)
200 dev
->data
[0] = (unsigned long)priv
;
204 static inline int is_ip(struct sk_buff
*skb
)
206 return skb
->protocol
== cpu_to_be16(ETH_P_IP
);
210 * check passed in packet and make sure that:
211 * - it is linear (no scatter/gather)
212 * - it is ethernet (mac_header properly set)
214 static int check_ethip_packet(struct sk_buff
*skb
, struct usbnet
*dev
)
216 skb_reset_mac_header(skb
); /* ethernet header */
218 if (skb_is_nonlinear(skb
)) {
219 netdev_err(dev
->net
, "Non linear buffer-dropping\n");
223 if (!pskb_may_pull(skb
, ETH_HLEN
))
225 skb
->protocol
= eth_hdr(skb
)->h_proto
;
230 static const u8
*save16bit(struct param
*p
, const u8
*datap
)
233 p
->word
= get_unaligned_be16(datap
);
234 return datap
+ sizeof(p
->word
);
237 static const u8
*save8bit(struct param
*p
, const u8
*datap
)
241 return datap
+ sizeof(p
->byte
);
244 /*----------------------------------------------------------------------------*
246 *----------------------------------------------------------------------------*/
248 #define SIERRA_NET_HIP_HDR_LEN 4
249 /* Extended HIP header */
250 #define SIERRA_NET_HIP_EXT_HDR_LEN 6
254 struct param payload_len
;
256 struct param msgspecific
;
257 struct param extmsgid
;
260 static int parse_hip(const u8
*buf
, const u32 buflen
, struct hip_hdr
*hh
)
262 const u8
*curp
= buf
;
265 if (buflen
< SIERRA_NET_HIP_HDR_LEN
)
268 curp
= save16bit(&hh
->payload_len
, curp
);
269 curp
= save8bit(&hh
->msgid
, curp
);
270 curp
= save8bit(&hh
->msgspecific
, curp
);
272 padded
= hh
->msgid
.byte
& 0x80;
273 hh
->msgid
.byte
&= 0x7F; /* 7 bits */
275 hh
->extmsgid
.is_present
= (hh
->msgid
.byte
== SIERRA_NET_HIP_EXTENDEDID
);
276 if (hh
->extmsgid
.is_present
) {
277 if (buflen
< SIERRA_NET_HIP_EXT_HDR_LEN
)
280 hh
->payload_len
.word
&= 0x3FFF; /* 14 bits */
282 curp
= save16bit(&hh
->extmsgid
, curp
);
283 hh
->extmsgid
.word
&= 0x03FF; /* 10 bits */
285 hh
->hdrlen
= SIERRA_NET_HIP_EXT_HDR_LEN
;
287 hh
->payload_len
.word
&= 0x07FF; /* 11 bits */
288 hh
->hdrlen
= SIERRA_NET_HIP_HDR_LEN
;
293 hh
->payload_len
.word
--;
296 /* if real packet shorter than the claimed length */
297 if (buflen
< (hh
->hdrlen
+ hh
->payload_len
.word
))
303 static void build_hip(u8
*buf
, const u16 payloadlen
,
304 struct sierra_net_data
*priv
)
306 /* the following doesn't have the full functionality. We
307 * currently build only one kind of header, so it is faster this way
309 put_unaligned_be16(payloadlen
, buf
);
310 memcpy(buf
+2, priv
->tx_hdr_template
, sizeof(priv
->tx_hdr_template
));
312 /*----------------------------------------------------------------------------*
314 *----------------------------------------------------------------------------*/
316 static int sierra_net_send_cmd(struct usbnet
*dev
,
317 u8
*cmd
, int cmdlen
, const char * cmd_name
)
319 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
322 status
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
323 USB_CDC_SEND_ENCAPSULATED_COMMAND
,
324 USB_DIR_OUT
|USB_TYPE_CLASS
|USB_RECIP_INTERFACE
, 0,
325 priv
->ifnum
, cmd
, cmdlen
, USB_CTRL_SET_TIMEOUT
);
327 if (status
!= cmdlen
&& status
!= -ENODEV
)
328 netdev_err(dev
->net
, "Submit %s failed %d\n", cmd_name
, status
);
333 static int sierra_net_send_sync(struct usbnet
*dev
)
336 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
338 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
340 status
= sierra_net_send_cmd(dev
, priv
->sync_msg
,
341 sizeof(priv
->sync_msg
), "SYNC");
346 static void sierra_net_set_ctx_index(struct sierra_net_data
*priv
, u8 ctx_ix
)
348 dev_dbg(&(priv
->usbnet
->udev
->dev
), "%s %d", __func__
, ctx_ix
);
349 priv
->tx_hdr_template
[0] = 0x3F;
350 priv
->tx_hdr_template
[1] = ctx_ix
;
351 *((u16
*)&priv
->tx_hdr_template
[2]) =
352 cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID
);
355 static inline int sierra_net_is_valid_addrlen(u8 len
)
357 return len
== sizeof(struct in_addr
);
360 static int sierra_net_parse_lsi(struct usbnet
*dev
, char *data
, int datalen
)
362 struct lsi_umts
*lsi
= (struct lsi_umts
*)data
;
364 if (datalen
< sizeof(struct lsi_umts
)) {
365 netdev_err(dev
->net
, "%s: Data length %d, exp %Zu\n",
367 sizeof(struct lsi_umts
));
371 if (lsi
->length
!= cpu_to_be16(SIERRA_NET_LSI_UMTS_STATUS_LEN
)) {
372 netdev_err(dev
->net
, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
373 __func__
, be16_to_cpu(lsi
->length
),
374 (u32
)SIERRA_NET_LSI_UMTS_STATUS_LEN
);
378 /* Validate the protocol - only support UMTS for now */
379 if (lsi
->protocol
!= SIERRA_NET_PROTOCOL_UMTS
) {
380 netdev_err(dev
->net
, "Protocol unsupported, 0x%02x\n",
385 /* Validate the link type */
386 if (lsi
->link_type
!= SIERRA_NET_AS_LINK_TYPE_IPv4
) {
387 netdev_err(dev
->net
, "Link type unsupported: 0x%02x\n",
392 /* Validate the coverage */
393 if (lsi
->coverage
== SIERRA_NET_COVERAGE_NONE
394 || lsi
->coverage
== SIERRA_NET_COVERAGE_NOPACKET
) {
395 netdev_err(dev
->net
, "No coverage, 0x%02x\n", lsi
->coverage
);
399 /* Validate the session state */
400 if (lsi
->session_state
== SIERRA_NET_SESSION_IDLE
) {
401 netdev_err(dev
->net
, "Session idle, 0x%02x\n",
406 /* Set link_sense true */
410 static void sierra_net_handle_lsi(struct usbnet
*dev
, char *data
,
413 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
416 link_up
= sierra_net_parse_lsi(dev
, data
+ hh
->hdrlen
,
417 hh
->payload_len
.word
);
419 netdev_err(dev
->net
, "Invalid LSI\n");
423 sierra_net_set_ctx_index(priv
, hh
->msgspecific
.byte
);
425 netif_carrier_on(dev
->net
);
428 netif_carrier_off(dev
->net
);
432 static void sierra_net_dosync(struct usbnet
*dev
)
435 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
437 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
439 /* tell modem we are ready */
440 status
= sierra_net_send_sync(dev
);
443 "Send SYNC failed, status %d\n", status
);
444 status
= sierra_net_send_sync(dev
);
447 "Send SYNC failed, status %d\n", status
);
449 /* Now, start a timer and make sure we get the Restart Indication */
450 priv
->sync_timer
.function
= sierra_sync_timer
;
451 priv
->sync_timer
.data
= (unsigned long) dev
;
452 priv
->sync_timer
.expires
= jiffies
+ SIERRA_NET_SYNCDELAY
;
453 add_timer(&priv
->sync_timer
);
456 static void sierra_net_kevent(struct work_struct
*work
)
458 struct sierra_net_data
*priv
=
459 container_of(work
, struct sierra_net_data
, sierra_net_kevent
);
460 struct usbnet
*dev
= priv
->usbnet
;
466 if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL
, &priv
->kevent_flags
)) {
467 clear_bit(SIERRA_NET_EVENT_RESP_AVAIL
, &priv
->kevent_flags
);
469 /* Query the modem for the LSI message */
470 buf
= kzalloc(SIERRA_NET_USBCTL_BUF_LEN
, GFP_KERNEL
);
473 "failed to allocate buf for LS msg\n");
477 len
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
478 USB_CDC_GET_ENCAPSULATED_RESPONSE
,
479 USB_DIR_IN
|USB_TYPE_CLASS
|USB_RECIP_INTERFACE
,
480 0, ifnum
, buf
, SIERRA_NET_USBCTL_BUF_LEN
,
481 USB_CTRL_SET_TIMEOUT
);
485 "usb_control_msg failed, status %d\n", len
);
489 dev_dbg(&dev
->udev
->dev
, "%s: Received status message,"
490 " %04x bytes", __func__
, len
);
492 err
= parse_hip(buf
, len
, &hh
);
494 netdev_err(dev
->net
, "%s: Bad packet,"
495 " parse result %d\n", __func__
, err
);
500 /* Validate packet length */
501 if (len
!= hh
.hdrlen
+ hh
.payload_len
.word
) {
502 netdev_err(dev
->net
, "%s: Bad packet, received"
503 " %d, expected %d\n", __func__
, len
,
504 hh
.hdrlen
+ hh
.payload_len
.word
);
509 /* Switch on received message types */
510 switch (hh
.msgid
.byte
) {
511 case SIERRA_NET_HIP_LSI_UMTSID
:
512 dev_dbg(&dev
->udev
->dev
, "LSI for ctx:%d",
513 hh
.msgspecific
.byte
);
514 sierra_net_handle_lsi(dev
, buf
, &hh
);
516 case SIERRA_NET_HIP_RESTART_ID
:
517 dev_dbg(&dev
->udev
->dev
, "Restart reported: %d,"
518 " stopping sync timer",
519 hh
.msgspecific
.byte
);
520 /* Got sync resp - stop timer & clear mask */
521 del_timer_sync(&priv
->sync_timer
);
522 clear_bit(SIERRA_NET_TIMER_EXPIRY
,
523 &priv
->kevent_flags
);
525 case SIERRA_NET_HIP_HSYNC_ID
:
526 dev_dbg(&dev
->udev
->dev
, "SYNC received");
527 err
= sierra_net_send_sync(dev
);
530 "Send SYNC failed %d\n", err
);
532 case SIERRA_NET_HIP_EXTENDEDID
:
533 netdev_err(dev
->net
, "Unrecognized HIP msg, "
534 "extmsgid 0x%04x\n", hh
.extmsgid
.word
);
536 case SIERRA_NET_HIP_RCGI
:
540 netdev_err(dev
->net
, "Unrecognized HIP msg, "
541 "msgid 0x%02x\n", hh
.msgid
.byte
);
547 /* The sync timer bit might be set */
548 if (test_bit(SIERRA_NET_TIMER_EXPIRY
, &priv
->kevent_flags
)) {
549 clear_bit(SIERRA_NET_TIMER_EXPIRY
, &priv
->kevent_flags
);
550 dev_dbg(&dev
->udev
->dev
, "Deferred sync timer expiry");
551 sierra_net_dosync(priv
->usbnet
);
554 if (priv
->kevent_flags
)
555 dev_dbg(&dev
->udev
->dev
, "sierra_net_kevent done, "
556 "kevent_flags = 0x%lx", priv
->kevent_flags
);
559 static void sierra_net_defer_kevent(struct usbnet
*dev
, int work
)
561 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
563 set_bit(work
, &priv
->kevent_flags
);
564 schedule_work(&priv
->sierra_net_kevent
);
568 * Sync Retransmit Timer Handler. On expiry, kick the work queue
570 void sierra_sync_timer(unsigned long syncdata
)
572 struct usbnet
*dev
= (struct usbnet
*)syncdata
;
574 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
575 /* Kick the tasklet */
576 sierra_net_defer_kevent(dev
, SIERRA_NET_TIMER_EXPIRY
);
579 static void sierra_net_status(struct usbnet
*dev
, struct urb
*urb
)
581 struct usb_cdc_notification
*event
;
583 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
585 if (urb
->actual_length
< sizeof *event
)
588 /* Add cases to handle other standard notifications. */
589 event
= urb
->transfer_buffer
;
590 switch (event
->bNotificationType
) {
591 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
592 case USB_CDC_NOTIFY_SPEED_CHANGE
:
593 /* USB 305 sends those */
595 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
596 sierra_net_defer_kevent(dev
, SIERRA_NET_EVENT_RESP_AVAIL
);
599 netdev_err(dev
->net
, ": unexpected notification %02x!\n",
600 event
->bNotificationType
);
605 static void sierra_net_get_drvinfo(struct net_device
*net
,
606 struct ethtool_drvinfo
*info
)
608 /* Inherit standard device info */
609 usbnet_get_drvinfo(net
, info
);
610 strncpy(info
->driver
, driver_name
, sizeof info
->driver
);
611 strncpy(info
->version
, DRIVER_VERSION
, sizeof info
->version
);
614 static u32
sierra_net_get_link(struct net_device
*net
)
616 struct usbnet
*dev
= netdev_priv(net
);
617 /* Report link is down whenever the interface is down */
618 return sierra_net_get_private(dev
)->link_up
&& netif_running(net
);
621 static struct ethtool_ops sierra_net_ethtool_ops
= {
622 .get_drvinfo
= sierra_net_get_drvinfo
,
623 .get_link
= sierra_net_get_link
,
624 .get_msglevel
= usbnet_get_msglevel
,
625 .set_msglevel
= usbnet_set_msglevel
,
626 .get_settings
= usbnet_get_settings
,
627 .set_settings
= usbnet_set_settings
,
628 .nway_reset
= usbnet_nway_reset
,
631 /* MTU can not be more than 1500 bytes, enforce it. */
632 static int sierra_net_change_mtu(struct net_device
*net
, int new_mtu
)
634 if (new_mtu
> SIERRA_NET_MAX_SUPPORTED_MTU
)
637 return usbnet_change_mtu(net
, new_mtu
);
640 static int is_whitelisted(const u8 ifnum
,
641 const struct sierra_net_iface_info
*whitelist
)
644 const u8
*list
= whitelist
->ifaceinfo
;
647 for (i
= 0; i
< whitelist
->infolen
; i
++) {
648 if (list
[i
] == ifnum
)
655 static int sierra_net_get_fw_attr(struct usbnet
*dev
, u16
*datap
)
660 attrdata
= kmalloc(sizeof(*attrdata
), GFP_KERNEL
);
664 result
= usb_control_msg(
666 usb_rcvctrlpipe(dev
->udev
, 0),
667 /* _u8 vendor specific request */
668 SWI_USB_REQUEST_GET_FW_ATTR
,
669 USB_DIR_IN
| USB_TYPE_VENDOR
, /* __u8 request type */
670 0x0000, /* __u16 value not used */
671 0x0000, /* __u16 index not used */
672 attrdata
, /* char *data */
673 sizeof(*attrdata
), /* __u16 size */
674 USB_CTRL_SET_TIMEOUT
); /* int timeout */
688 * collects the bulk endpoints, the status endpoint.
690 static int sierra_net_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
697 struct sierra_net_data
*priv
;
698 static const u8 sync_tmplate
[sizeof(priv
->sync_msg
)] = {
699 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID
, 0x00};
700 static const u8 shdwn_tmplate
[sizeof(priv
->shdwn_msg
)] = {
701 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID
, 0x00};
703 struct sierra_net_info_data
*data
=
704 (struct sierra_net_info_data
*)dev
->driver_info
->data
;
706 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
708 ifacenum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
709 /* We only accept certain interfaces */
710 if (!is_whitelisted(ifacenum
, &data
->whitelist
)) {
711 dev_dbg(&dev
->udev
->dev
, "Ignoring interface: %d", ifacenum
);
714 numendpoints
= intf
->cur_altsetting
->desc
.bNumEndpoints
;
715 /* We have three endpoints, bulk in and out, and a status */
716 if (numendpoints
!= 3) {
717 dev_err(&dev
->udev
->dev
, "Expected 3 endpoints, found: %d",
721 /* Status endpoint set in usbnet_get_endpoints() */
723 status
= usbnet_get_endpoints(dev
, intf
);
725 dev_err(&dev
->udev
->dev
, "Error in usbnet_get_endpoints (%d)",
729 /* Initialize sierra private data */
730 priv
= kzalloc(sizeof *priv
, GFP_KERNEL
);
732 dev_err(&dev
->udev
->dev
, "No memory");
737 priv
->ifnum
= ifacenum
;
738 dev
->net
->netdev_ops
= &sierra_net_device_ops
;
740 /* change MAC addr to include, ifacenum, and to be unique */
741 dev
->net
->dev_addr
[ETH_ALEN
-2] = atomic_inc_return(&iface_counter
);
742 dev
->net
->dev_addr
[ETH_ALEN
-1] = ifacenum
;
744 /* we will have to manufacture ethernet headers, prepare template */
745 eth
= (struct ethhdr
*)priv
->ethr_hdr_tmpl
;
746 memcpy(ð
->h_dest
, dev
->net
->dev_addr
, ETH_ALEN
);
747 eth
->h_proto
= cpu_to_be16(ETH_P_IP
);
749 /* prepare shutdown message template */
750 memcpy(priv
->shdwn_msg
, shdwn_tmplate
, sizeof(priv
->shdwn_msg
));
751 /* set context index initially to 0 - prepares tx hdr template */
752 sierra_net_set_ctx_index(priv
, 0);
754 /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */
755 dev
->rx_urb_size
= data
->rx_urb_size
;
756 if (dev
->udev
->speed
!= USB_SPEED_HIGH
)
757 dev
->rx_urb_size
= min_t(size_t, 4096, data
->rx_urb_size
);
759 dev
->net
->hard_header_len
+= SIERRA_NET_HIP_EXT_HDR_LEN
;
760 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
762 /* Set up the netdev */
763 dev
->net
->flags
|= IFF_NOARP
;
764 dev
->net
->ethtool_ops
= &sierra_net_ethtool_ops
;
765 netif_carrier_off(dev
->net
);
767 sierra_net_set_private(dev
, priv
);
769 priv
->kevent_flags
= 0;
771 /* Use the shared workqueue */
772 INIT_WORK(&priv
->sierra_net_kevent
, sierra_net_kevent
);
774 /* Only need to do this once */
775 init_timer(&priv
->sync_timer
);
777 /* verify fw attributes */
778 status
= sierra_net_get_fw_attr(dev
, &fwattr
);
779 dev_dbg(&dev
->udev
->dev
, "Fw attr: %x\n", fwattr
);
781 /* test whether firmware supports DHCP */
782 if (!(status
== sizeof(fwattr
) && (fwattr
& SWI_GET_FW_ATTR_MASK
))) {
783 /* found incompatible firmware version */
784 dev_err(&dev
->udev
->dev
, "Incompatible driver and firmware"
789 /* prepare sync message from template */
790 memcpy(priv
->sync_msg
, sync_tmplate
, sizeof(priv
->sync_msg
));
792 /* initiate the sync sequence */
793 sierra_net_dosync(dev
);
798 static void sierra_net_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
801 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
803 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
805 /* kill the timer and work */
806 del_timer_sync(&priv
->sync_timer
);
807 cancel_work_sync(&priv
->sierra_net_kevent
);
809 /* tell modem we are going away */
810 status
= sierra_net_send_cmd(dev
, priv
->shdwn_msg
,
811 sizeof(priv
->shdwn_msg
), "Shutdown");
814 "usb_control_msg failed, status %d\n", status
);
816 sierra_net_set_private(dev
, NULL
);
821 static struct sk_buff
*sierra_net_skb_clone(struct usbnet
*dev
,
822 struct sk_buff
*skb
, int len
)
824 struct sk_buff
*new_skb
;
827 new_skb
= skb_clone(skb
, GFP_ATOMIC
);
829 /* remove len bytes from original */
832 /* trim next packet to it's length */
834 skb_trim(new_skb
, len
);
836 if (netif_msg_rx_err(dev
))
837 netdev_err(dev
->net
, "failed to get skb\n");
838 dev
->net
->stats
.rx_dropped
++;
844 /* ---------------------------- Receive data path ----------------------*/
845 static int sierra_net_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
849 struct sk_buff
*new_skb
;
851 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
853 /* could contain multiple packets */
854 while (likely(skb
->len
)) {
855 err
= parse_hip(skb
->data
, skb
->len
, &hh
);
857 if (netif_msg_rx_err(dev
))
858 netdev_err(dev
->net
, "Invalid HIP header %d\n",
860 /* dev->net->stats.rx_errors incremented by caller */
861 dev
->net
->stats
.rx_length_errors
++;
865 /* Validate Extended HIP header */
866 if (!hh
.extmsgid
.is_present
867 || hh
.extmsgid
.word
!= SIERRA_NET_HIP_EXT_IP_IN_ID
) {
868 if (netif_msg_rx_err(dev
))
869 netdev_err(dev
->net
, "HIP/ETH: Invalid pkt\n");
871 dev
->net
->stats
.rx_frame_errors
++;
872 /* dev->net->stats.rx_errors incremented by caller */;
876 skb_pull(skb
, hh
.hdrlen
);
878 /* We are going to accept this packet, prepare it */
879 memcpy(skb
->data
, sierra_net_get_private(dev
)->ethr_hdr_tmpl
,
882 /* Last packet in batch handled by usbnet */
883 if (hh
.payload_len
.word
== skb
->len
)
886 new_skb
= sierra_net_skb_clone(dev
, skb
, hh
.payload_len
.word
);
888 usbnet_skb_return(dev
, new_skb
);
895 /* ---------------------------- Transmit data path ----------------------*/
896 struct sk_buff
*sierra_net_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
,
899 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
903 BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet
, data
)
904 < sizeof(struct cdc_state
));
906 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
907 if (priv
->link_up
&& check_ethip_packet(skb
, dev
) && is_ip(skb
)) {
908 /* enough head room as is? */
909 if (SIERRA_NET_HIP_EXT_HDR_LEN
<= skb_headroom(skb
)) {
910 /* Save the Eth/IP length and set up HIP hdr */
912 skb_push(skb
, SIERRA_NET_HIP_EXT_HDR_LEN
);
913 /* Handle ZLP issue */
914 need_tail
= ((len
+ SIERRA_NET_HIP_EXT_HDR_LEN
)
915 % dev
->maxpacket
== 0);
917 if (unlikely(skb_tailroom(skb
) == 0)) {
918 netdev_err(dev
->net
, "tx_fixup:"
919 "no room for packet\n");
920 dev_kfree_skb_any(skb
);
923 skb
->data
[skb
->len
] = 0;
928 build_hip(skb
->data
, len
, priv
);
932 * compensate in the future if necessary
934 netdev_err(dev
->net
, "tx_fixup: no room for HIP\n");
939 dev
->net
->stats
.tx_carrier_errors
++;
941 /* tx_dropped incremented by usbnet */
943 /* filter the packet out, release it */
944 dev_kfree_skb_any(skb
);
948 static const u8 sierra_net_ifnum_list
[] = { 7, 10, 11 };
949 static const struct sierra_net_info_data sierra_net_info_data_68A3
= {
950 .rx_urb_size
= 8 * 1024,
952 .infolen
= ARRAY_SIZE(sierra_net_ifnum_list
),
953 .ifaceinfo
= sierra_net_ifnum_list
957 static const struct driver_info sierra_net_info_68A3
= {
958 .description
= "Sierra Wireless USB-to-WWAN Modem",
959 .flags
= FLAG_WWAN
| FLAG_SEND_ZLP
,
960 .bind
= sierra_net_bind
,
961 .unbind
= sierra_net_unbind
,
962 .status
= sierra_net_status
,
963 .rx_fixup
= sierra_net_rx_fixup
,
964 .tx_fixup
= sierra_net_tx_fixup
,
965 .data
= (unsigned long)&sierra_net_info_data_68A3
,
968 static const struct usb_device_id products
[] = {
969 {USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless USB-to-WWAN modem */
970 .driver_info
= (unsigned long) &sierra_net_info_68A3
},
974 MODULE_DEVICE_TABLE(usb
, products
);
976 /* We are based on usbnet, so let it handle the USB driver specifics */
977 static struct usb_driver sierra_net_driver
= {
978 .name
= "sierra_net",
979 .id_table
= products
,
980 .probe
= usbnet_probe
,
981 .disconnect
= usbnet_disconnect
,
982 .suspend
= usbnet_suspend
,
983 .resume
= usbnet_resume
,
987 module_usb_driver(sierra_net_driver
);
989 MODULE_AUTHOR(DRIVER_AUTHOR
);
990 MODULE_DESCRIPTION(DRIVER_DESC
);
991 MODULE_VERSION(DRIVER_VERSION
);
992 MODULE_LICENSE("GPL");