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, see <http://www.gnu.org/licenses/>.
27 #define DRIVER_VERSION "v.2.0"
28 #define DRIVER_AUTHOR "Paxton Smith, Matthew Safar, Rory Filer"
29 #define DRIVER_DESC "USB-to-WWAN Driver for Sierra Wireless modems"
30 static const char driver_name
[] = "sierra_net";
32 /* if defined debug messages enabled */
35 #include <linux/module.h>
36 #include <linux/etherdevice.h>
37 #include <linux/ethtool.h>
38 #include <linux/mii.h>
39 #include <linux/sched.h>
40 #include <linux/timer.h>
41 #include <linux/usb.h>
42 #include <linux/usb/cdc.h>
45 #include <asm/unaligned.h>
46 #include <linux/usb/usbnet.h>
48 #define SWI_USB_REQUEST_GET_FW_ATTR 0x06
49 #define SWI_GET_FW_ATTR_MASK 0x08
51 /* atomic counter partially included in MAC address to make sure 2 devices
52 * do not end up with the same MAC - concept breaks in case of > 255 ifaces
54 static atomic_t iface_counter
= ATOMIC_INIT(0);
57 * SYNC Timer Delay definition used to set the expiry time
59 #define SIERRA_NET_SYNCDELAY (2*HZ)
61 /* Max. MTU supported. The modem buffers are limited to 1500 */
62 #define SIERRA_NET_MAX_SUPPORTED_MTU 1500
64 /* The SIERRA_NET_USBCTL_BUF_LEN defines a buffer size allocated for control
65 * message reception ... and thus the max. received packet.
66 * (May be the cause for parse_hip returning -EINVAL)
68 #define SIERRA_NET_USBCTL_BUF_LEN 1024
70 /* Overriding the default usbnet rx_urb_size */
71 #define SIERRA_NET_RX_URB_SIZE (8 * 1024)
73 /* Private data structure */
74 struct sierra_net_data
{
76 u16 link_up
; /* air link up or down */
77 u8 tx_hdr_template
[4]; /* part of HIP hdr for tx'd packets */
79 u8 sync_msg
[4]; /* SYNC message */
80 u8 shdwn_msg
[4]; /* Shutdown message */
82 /* Backpointer to the container */
83 struct usbnet
*usbnet
;
85 u8 ifnum
; /* interface number */
87 /* Bit masks, must be a power of 2 */
88 #define SIERRA_NET_EVENT_RESP_AVAIL 0x01
89 #define SIERRA_NET_TIMER_EXPIRY 0x02
90 unsigned long kevent_flags
;
91 struct work_struct sierra_net_kevent
;
92 struct timer_list sync_timer
; /* For retrying SYNC sequence */
105 /* HIP message type */
106 #define SIERRA_NET_HIP_EXTENDEDID 0x7F
107 #define SIERRA_NET_HIP_HSYNC_ID 0x60 /* Modem -> host */
108 #define SIERRA_NET_HIP_RESTART_ID 0x62 /* Modem -> host */
109 #define SIERRA_NET_HIP_MSYNC_ID 0x20 /* Host -> modem */
110 #define SIERRA_NET_HIP_SHUTD_ID 0x26 /* Host -> modem */
112 #define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
113 #define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
115 /* 3G UMTS Link Sense Indication definitions */
116 #define SIERRA_NET_HIP_LSI_UMTSID 0x78
118 /* Reverse Channel Grant Indication HIP message */
119 #define SIERRA_NET_HIP_RCGI 0x64
121 /* LSI Protocol types */
122 #define SIERRA_NET_PROTOCOL_UMTS 0x01
123 #define SIERRA_NET_PROTOCOL_UMTS_DS 0x04
125 #define SIERRA_NET_COVERAGE_NONE 0x00
126 #define SIERRA_NET_COVERAGE_NOPACKET 0x01
129 #define SIERRA_NET_SESSION_IDLE 0x00
131 #define SIERRA_NET_AS_LINK_TYPE_IPV4 0x00
132 #define SIERRA_NET_AS_LINK_TYPE_IPV6 0x02
138 /* eventually use a union for the rest - assume umts for now */
140 u8 network_len
; /* network name len */
141 u8 network
[40]; /* network name (UCS2, bigendian) */
146 struct lsi_umts_single
{
149 u8 pdp_addr_len
; /* NW-supplied PDP address len */
150 u8 pdp_addr
[16]; /* NW-supplied PDP address (bigendian)) */
152 u8 dns1_addr_len
; /* NW-supplied 1st DNS address len (bigendian) */
153 u8 dns1_addr
[16]; /* NW-supplied 1st DNS address */
154 u8 dns2_addr_len
; /* NW-supplied 2nd DNS address len */
155 u8 dns2_addr
[16]; /* NW-supplied 2nd DNS address (bigendian)*/
156 u8 wins1_addr_len
; /* NW-supplied 1st Wins address len */
157 u8 wins1_addr
[16]; /* NW-supplied 1st Wins address (bigendian)*/
158 u8 wins2_addr_len
; /* NW-supplied 2nd Wins address len */
159 u8 wins2_addr
[16]; /* NW-supplied 2nd Wins address (bigendian) */
161 u8 gw_addr_len
; /* NW-supplied GW address len */
162 u8 gw_addr
[16]; /* NW-supplied GW address (bigendian) */
166 struct lsi_umts_dual
{
168 u8 pdp_addr4_len
; /* NW-supplied PDP IPv4 address len */
169 u8 pdp_addr4
[4]; /* NW-supplied PDP IPv4 address (bigendian)) */
170 u8 pdp_addr6_len
; /* NW-supplied PDP IPv6 address len */
171 u8 pdp_addr6
[16]; /* NW-supplied PDP IPv6 address (bigendian)) */
173 u8 dns1_addr4_len
; /* NW-supplied 1st DNS v4 address len (bigendian) */
174 u8 dns1_addr4
[4]; /* NW-supplied 1st DNS v4 address */
175 u8 dns1_addr6_len
; /* NW-supplied 1st DNS v6 address len */
176 u8 dns1_addr6
[16]; /* NW-supplied 1st DNS v6 address (bigendian)*/
177 u8 dns2_addr4_len
; /* NW-supplied 2nd DNS v4 address len (bigendian) */
178 u8 dns2_addr4
[4]; /* NW-supplied 2nd DNS v4 address */
179 u8 dns2_addr6_len
; /* NW-supplied 2nd DNS v6 address len */
180 u8 dns2_addr6
[16]; /* NW-supplied 2nd DNS v6 address (bigendian)*/
184 #define SIERRA_NET_LSI_COMMON_LEN 4
185 #define SIERRA_NET_LSI_UMTS_LEN (sizeof(struct lsi_umts_single))
186 #define SIERRA_NET_LSI_UMTS_STATUS_LEN \
187 (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN)
188 #define SIERRA_NET_LSI_UMTS_DS_LEN (sizeof(struct lsi_umts_dual))
189 #define SIERRA_NET_LSI_UMTS_DS_STATUS_LEN \
190 (SIERRA_NET_LSI_UMTS_DS_LEN - SIERRA_NET_LSI_COMMON_LEN)
192 /* Forward definitions */
193 static void sierra_sync_timer(unsigned long syncdata
);
195 /* Our own net device operations structure */
196 static const struct net_device_ops sierra_net_device_ops
= {
197 .ndo_open
= usbnet_open
,
198 .ndo_stop
= usbnet_stop
,
199 .ndo_start_xmit
= usbnet_start_xmit
,
200 .ndo_tx_timeout
= usbnet_tx_timeout
,
201 .ndo_change_mtu
= usbnet_change_mtu
,
202 .ndo_set_mac_address
= eth_mac_addr
,
203 .ndo_validate_addr
= eth_validate_addr
,
206 /* get private data associated with passed in usbnet device */
207 static inline struct sierra_net_data
*sierra_net_get_private(struct usbnet
*dev
)
209 return (struct sierra_net_data
*)dev
->data
[0];
212 /* set private data associated with passed in usbnet device */
213 static inline void sierra_net_set_private(struct usbnet
*dev
,
214 struct sierra_net_data
*priv
)
216 dev
->data
[0] = (unsigned long)priv
;
219 /* is packet IPv4/IPv6 */
220 static inline int is_ip(struct sk_buff
*skb
)
222 return skb
->protocol
== cpu_to_be16(ETH_P_IP
) ||
223 skb
->protocol
== cpu_to_be16(ETH_P_IPV6
);
227 * check passed in packet and make sure that:
228 * - it is linear (no scatter/gather)
229 * - it is ethernet (mac_header properly set)
231 static int check_ethip_packet(struct sk_buff
*skb
, struct usbnet
*dev
)
233 skb_reset_mac_header(skb
); /* ethernet header */
235 if (skb_is_nonlinear(skb
)) {
236 netdev_err(dev
->net
, "Non linear buffer-dropping\n");
240 if (!pskb_may_pull(skb
, ETH_HLEN
))
242 skb
->protocol
= eth_hdr(skb
)->h_proto
;
247 static const u8
*save16bit(struct param
*p
, const u8
*datap
)
250 p
->word
= get_unaligned_be16(datap
);
251 return datap
+ sizeof(p
->word
);
254 static const u8
*save8bit(struct param
*p
, const u8
*datap
)
258 return datap
+ sizeof(p
->byte
);
261 /*----------------------------------------------------------------------------*
263 *----------------------------------------------------------------------------*/
265 #define SIERRA_NET_HIP_HDR_LEN 4
266 /* Extended HIP header */
267 #define SIERRA_NET_HIP_EXT_HDR_LEN 6
271 struct param payload_len
;
273 struct param msgspecific
;
274 struct param extmsgid
;
277 static int parse_hip(const u8
*buf
, const u32 buflen
, struct hip_hdr
*hh
)
279 const u8
*curp
= buf
;
282 if (buflen
< SIERRA_NET_HIP_HDR_LEN
)
285 curp
= save16bit(&hh
->payload_len
, curp
);
286 curp
= save8bit(&hh
->msgid
, curp
);
287 curp
= save8bit(&hh
->msgspecific
, curp
);
289 padded
= hh
->msgid
.byte
& 0x80;
290 hh
->msgid
.byte
&= 0x7F; /* 7 bits */
292 hh
->extmsgid
.is_present
= (hh
->msgid
.byte
== SIERRA_NET_HIP_EXTENDEDID
);
293 if (hh
->extmsgid
.is_present
) {
294 if (buflen
< SIERRA_NET_HIP_EXT_HDR_LEN
)
297 hh
->payload_len
.word
&= 0x3FFF; /* 14 bits */
299 curp
= save16bit(&hh
->extmsgid
, curp
);
300 hh
->extmsgid
.word
&= 0x03FF; /* 10 bits */
302 hh
->hdrlen
= SIERRA_NET_HIP_EXT_HDR_LEN
;
304 hh
->payload_len
.word
&= 0x07FF; /* 11 bits */
305 hh
->hdrlen
= SIERRA_NET_HIP_HDR_LEN
;
310 hh
->payload_len
.word
--;
313 /* if real packet shorter than the claimed length */
314 if (buflen
< (hh
->hdrlen
+ hh
->payload_len
.word
))
320 static void build_hip(u8
*buf
, const u16 payloadlen
,
321 struct sierra_net_data
*priv
)
323 /* the following doesn't have the full functionality. We
324 * currently build only one kind of header, so it is faster this way
326 put_unaligned_be16(payloadlen
, buf
);
327 memcpy(buf
+2, priv
->tx_hdr_template
, sizeof(priv
->tx_hdr_template
));
329 /*----------------------------------------------------------------------------*
331 *----------------------------------------------------------------------------*/
333 static int sierra_net_send_cmd(struct usbnet
*dev
,
334 u8
*cmd
, int cmdlen
, const char * cmd_name
)
336 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
339 status
= usbnet_write_cmd(dev
, USB_CDC_SEND_ENCAPSULATED_COMMAND
,
340 USB_DIR_OUT
|USB_TYPE_CLASS
|USB_RECIP_INTERFACE
,
341 0, priv
->ifnum
, cmd
, cmdlen
);
343 if (status
!= cmdlen
&& status
!= -ENODEV
)
344 netdev_err(dev
->net
, "Submit %s failed %d\n", cmd_name
, status
);
349 static int sierra_net_send_sync(struct usbnet
*dev
)
352 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
354 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
356 status
= sierra_net_send_cmd(dev
, priv
->sync_msg
,
357 sizeof(priv
->sync_msg
), "SYNC");
362 static void sierra_net_set_ctx_index(struct sierra_net_data
*priv
, u8 ctx_ix
)
364 dev_dbg(&(priv
->usbnet
->udev
->dev
), "%s %d", __func__
, ctx_ix
);
365 priv
->tx_hdr_template
[0] = 0x3F;
366 priv
->tx_hdr_template
[1] = ctx_ix
;
367 *((__be16
*)&priv
->tx_hdr_template
[2]) =
368 cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID
);
371 static inline int sierra_net_is_valid_addrlen(u8 len
)
373 return len
== sizeof(struct in_addr
);
376 static int sierra_net_parse_lsi(struct usbnet
*dev
, char *data
, int datalen
)
378 struct lsi_umts
*lsi
= (struct lsi_umts
*)data
;
381 if (datalen
< sizeof(struct lsi_umts_single
)) {
382 netdev_err(dev
->net
, "%s: Data length %d, exp >= %Zu\n",
383 __func__
, datalen
, sizeof(struct lsi_umts_single
));
387 /* Validate the session state */
388 if (lsi
->session_state
== SIERRA_NET_SESSION_IDLE
) {
389 netdev_err(dev
->net
, "Session idle, 0x%02x\n",
394 /* Validate the protocol - only support UMTS for now */
395 if (lsi
->protocol
== SIERRA_NET_PROTOCOL_UMTS
) {
396 struct lsi_umts_single
*single
= (struct lsi_umts_single
*)lsi
;
398 /* Validate the link type */
399 if (single
->link_type
!= SIERRA_NET_AS_LINK_TYPE_IPV4
&&
400 single
->link_type
!= SIERRA_NET_AS_LINK_TYPE_IPV6
) {
401 netdev_err(dev
->net
, "Link type unsupported: 0x%02x\n",
405 expected_length
= SIERRA_NET_LSI_UMTS_STATUS_LEN
;
406 } else if (lsi
->protocol
== SIERRA_NET_PROTOCOL_UMTS_DS
) {
407 expected_length
= SIERRA_NET_LSI_UMTS_DS_STATUS_LEN
;
409 netdev_err(dev
->net
, "Protocol unsupported, 0x%02x\n",
414 if (be16_to_cpu(lsi
->length
) != expected_length
) {
415 netdev_err(dev
->net
, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
416 __func__
, be16_to_cpu(lsi
->length
), expected_length
);
420 /* Validate the coverage */
421 if (lsi
->coverage
== SIERRA_NET_COVERAGE_NONE
||
422 lsi
->coverage
== SIERRA_NET_COVERAGE_NOPACKET
) {
423 netdev_err(dev
->net
, "No coverage, 0x%02x\n", lsi
->coverage
);
427 /* Set link_sense true */
431 static void sierra_net_handle_lsi(struct usbnet
*dev
, char *data
,
434 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
437 link_up
= sierra_net_parse_lsi(dev
, data
+ hh
->hdrlen
,
438 hh
->payload_len
.word
);
440 netdev_err(dev
->net
, "Invalid LSI\n");
444 sierra_net_set_ctx_index(priv
, hh
->msgspecific
.byte
);
449 usbnet_link_change(dev
, link_up
, 0);
452 static void sierra_net_dosync(struct usbnet
*dev
)
455 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
457 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
459 /* The SIERRA_NET_HIP_MSYNC_ID command appears to request that the
460 * firmware restart itself. After restarting, the modem will respond
461 * with the SIERRA_NET_HIP_RESTART_ID indication. The driver continues
462 * sending MSYNC commands every few seconds until it receives the
463 * RESTART event from the firmware
466 /* tell modem we are ready */
467 status
= sierra_net_send_sync(dev
);
470 "Send SYNC failed, status %d\n", status
);
471 status
= sierra_net_send_sync(dev
);
474 "Send SYNC failed, status %d\n", status
);
476 /* Now, start a timer and make sure we get the Restart Indication */
477 priv
->sync_timer
.function
= sierra_sync_timer
;
478 priv
->sync_timer
.data
= (unsigned long) dev
;
479 priv
->sync_timer
.expires
= jiffies
+ SIERRA_NET_SYNCDELAY
;
480 add_timer(&priv
->sync_timer
);
483 static void sierra_net_kevent(struct work_struct
*work
)
485 struct sierra_net_data
*priv
=
486 container_of(work
, struct sierra_net_data
, sierra_net_kevent
);
487 struct usbnet
*dev
= priv
->usbnet
;
493 if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL
, &priv
->kevent_flags
)) {
494 clear_bit(SIERRA_NET_EVENT_RESP_AVAIL
, &priv
->kevent_flags
);
496 /* Query the modem for the LSI message */
497 buf
= kzalloc(SIERRA_NET_USBCTL_BUF_LEN
, GFP_KERNEL
);
502 len
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
503 USB_CDC_GET_ENCAPSULATED_RESPONSE
,
504 USB_DIR_IN
|USB_TYPE_CLASS
|USB_RECIP_INTERFACE
,
505 0, ifnum
, buf
, SIERRA_NET_USBCTL_BUF_LEN
,
506 USB_CTRL_SET_TIMEOUT
);
510 "usb_control_msg failed, status %d\n", len
);
514 dev_dbg(&dev
->udev
->dev
, "%s: Received status message,"
515 " %04x bytes", __func__
, len
);
517 err
= parse_hip(buf
, len
, &hh
);
519 netdev_err(dev
->net
, "%s: Bad packet,"
520 " parse result %d\n", __func__
, err
);
525 /* Validate packet length */
526 if (len
!= hh
.hdrlen
+ hh
.payload_len
.word
) {
527 netdev_err(dev
->net
, "%s: Bad packet, received"
528 " %d, expected %d\n", __func__
, len
,
529 hh
.hdrlen
+ hh
.payload_len
.word
);
534 /* Switch on received message types */
535 switch (hh
.msgid
.byte
) {
536 case SIERRA_NET_HIP_LSI_UMTSID
:
537 dev_dbg(&dev
->udev
->dev
, "LSI for ctx:%d",
538 hh
.msgspecific
.byte
);
539 sierra_net_handle_lsi(dev
, buf
, &hh
);
541 case SIERRA_NET_HIP_RESTART_ID
:
542 dev_dbg(&dev
->udev
->dev
, "Restart reported: %d,"
543 " stopping sync timer",
544 hh
.msgspecific
.byte
);
545 /* Got sync resp - stop timer & clear mask */
546 del_timer_sync(&priv
->sync_timer
);
547 clear_bit(SIERRA_NET_TIMER_EXPIRY
,
548 &priv
->kevent_flags
);
550 case SIERRA_NET_HIP_HSYNC_ID
:
551 dev_dbg(&dev
->udev
->dev
, "SYNC received");
552 err
= sierra_net_send_sync(dev
);
555 "Send SYNC failed %d\n", err
);
557 case SIERRA_NET_HIP_EXTENDEDID
:
558 netdev_err(dev
->net
, "Unrecognized HIP msg, "
559 "extmsgid 0x%04x\n", hh
.extmsgid
.word
);
561 case SIERRA_NET_HIP_RCGI
:
565 netdev_err(dev
->net
, "Unrecognized HIP msg, "
566 "msgid 0x%02x\n", hh
.msgid
.byte
);
572 /* The sync timer bit might be set */
573 if (test_bit(SIERRA_NET_TIMER_EXPIRY
, &priv
->kevent_flags
)) {
574 clear_bit(SIERRA_NET_TIMER_EXPIRY
, &priv
->kevent_flags
);
575 dev_dbg(&dev
->udev
->dev
, "Deferred sync timer expiry");
576 sierra_net_dosync(priv
->usbnet
);
579 if (priv
->kevent_flags
)
580 dev_dbg(&dev
->udev
->dev
, "sierra_net_kevent done, "
581 "kevent_flags = 0x%lx", priv
->kevent_flags
);
584 static void sierra_net_defer_kevent(struct usbnet
*dev
, int work
)
586 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
588 set_bit(work
, &priv
->kevent_flags
);
589 schedule_work(&priv
->sierra_net_kevent
);
593 * Sync Retransmit Timer Handler. On expiry, kick the work queue
595 static void sierra_sync_timer(unsigned long syncdata
)
597 struct usbnet
*dev
= (struct usbnet
*)syncdata
;
599 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
600 /* Kick the tasklet */
601 sierra_net_defer_kevent(dev
, SIERRA_NET_TIMER_EXPIRY
);
604 static void sierra_net_status(struct usbnet
*dev
, struct urb
*urb
)
606 struct usb_cdc_notification
*event
;
608 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
610 if (urb
->actual_length
< sizeof *event
)
613 /* Add cases to handle other standard notifications. */
614 event
= urb
->transfer_buffer
;
615 switch (event
->bNotificationType
) {
616 case USB_CDC_NOTIFY_NETWORK_CONNECTION
:
617 case USB_CDC_NOTIFY_SPEED_CHANGE
:
618 /* USB 305 sends those */
620 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE
:
621 sierra_net_defer_kevent(dev
, SIERRA_NET_EVENT_RESP_AVAIL
);
624 netdev_err(dev
->net
, ": unexpected notification %02x!\n",
625 event
->bNotificationType
);
630 static void sierra_net_get_drvinfo(struct net_device
*net
,
631 struct ethtool_drvinfo
*info
)
633 /* Inherit standard device info */
634 usbnet_get_drvinfo(net
, info
);
635 strlcpy(info
->driver
, driver_name
, sizeof(info
->driver
));
636 strlcpy(info
->version
, DRIVER_VERSION
, sizeof(info
->version
));
639 static u32
sierra_net_get_link(struct net_device
*net
)
641 struct usbnet
*dev
= netdev_priv(net
);
642 /* Report link is down whenever the interface is down */
643 return sierra_net_get_private(dev
)->link_up
&& netif_running(net
);
646 static const struct ethtool_ops sierra_net_ethtool_ops
= {
647 .get_drvinfo
= sierra_net_get_drvinfo
,
648 .get_link
= sierra_net_get_link
,
649 .get_msglevel
= usbnet_get_msglevel
,
650 .set_msglevel
= usbnet_set_msglevel
,
651 .get_settings
= usbnet_get_settings
,
652 .set_settings
= usbnet_set_settings
,
653 .nway_reset
= usbnet_nway_reset
,
656 static int sierra_net_get_fw_attr(struct usbnet
*dev
, u16
*datap
)
661 result
= usbnet_read_cmd(dev
,
662 /* _u8 vendor specific request */
663 SWI_USB_REQUEST_GET_FW_ATTR
,
664 USB_DIR_IN
| USB_TYPE_VENDOR
, /* __u8 request type */
665 0x0000, /* __u16 value not used */
666 0x0000, /* __u16 index not used */
667 &attrdata
, /* char *data */
668 sizeof(attrdata
) /* __u16 size */
674 *datap
= le16_to_cpu(attrdata
);
679 * collects the bulk endpoints, the status endpoint.
681 static int sierra_net_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
687 struct sierra_net_data
*priv
;
688 static const u8 sync_tmplate
[sizeof(priv
->sync_msg
)] = {
689 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID
, 0x00};
690 static const u8 shdwn_tmplate
[sizeof(priv
->shdwn_msg
)] = {
691 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID
, 0x00};
693 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
695 ifacenum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
696 numendpoints
= intf
->cur_altsetting
->desc
.bNumEndpoints
;
697 /* We have three endpoints, bulk in and out, and a status */
698 if (numendpoints
!= 3) {
699 dev_err(&dev
->udev
->dev
, "Expected 3 endpoints, found: %d",
703 /* Status endpoint set in usbnet_get_endpoints() */
705 status
= usbnet_get_endpoints(dev
, intf
);
707 dev_err(&dev
->udev
->dev
, "Error in usbnet_get_endpoints (%d)",
711 /* Initialize sierra private data */
712 priv
= kzalloc(sizeof *priv
, GFP_KERNEL
);
717 priv
->ifnum
= ifacenum
;
718 dev
->net
->netdev_ops
= &sierra_net_device_ops
;
720 /* change MAC addr to include, ifacenum, and to be unique */
721 dev
->net
->dev_addr
[ETH_ALEN
-2] = atomic_inc_return(&iface_counter
);
722 dev
->net
->dev_addr
[ETH_ALEN
-1] = ifacenum
;
724 /* prepare shutdown message template */
725 memcpy(priv
->shdwn_msg
, shdwn_tmplate
, sizeof(priv
->shdwn_msg
));
726 /* set context index initially to 0 - prepares tx hdr template */
727 sierra_net_set_ctx_index(priv
, 0);
729 /* prepare sync message template */
730 memcpy(priv
->sync_msg
, sync_tmplate
, sizeof(priv
->sync_msg
));
732 /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */
733 dev
->rx_urb_size
= SIERRA_NET_RX_URB_SIZE
;
734 if (dev
->udev
->speed
!= USB_SPEED_HIGH
)
735 dev
->rx_urb_size
= min_t(size_t, 4096, SIERRA_NET_RX_URB_SIZE
);
737 dev
->net
->hard_header_len
+= SIERRA_NET_HIP_EXT_HDR_LEN
;
738 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
739 dev
->net
->max_mtu
= SIERRA_NET_MAX_SUPPORTED_MTU
;
741 /* Set up the netdev */
742 dev
->net
->flags
|= IFF_NOARP
;
743 dev
->net
->ethtool_ops
= &sierra_net_ethtool_ops
;
744 netif_carrier_off(dev
->net
);
746 sierra_net_set_private(dev
, priv
);
748 priv
->kevent_flags
= 0;
750 /* Use the shared workqueue */
751 INIT_WORK(&priv
->sierra_net_kevent
, sierra_net_kevent
);
753 /* Only need to do this once */
754 init_timer(&priv
->sync_timer
);
756 /* verify fw attributes */
757 status
= sierra_net_get_fw_attr(dev
, &fwattr
);
758 dev_dbg(&dev
->udev
->dev
, "Fw attr: %x\n", fwattr
);
760 /* test whether firmware supports DHCP */
761 if (!(status
== sizeof(fwattr
) && (fwattr
& SWI_GET_FW_ATTR_MASK
))) {
762 /* found incompatible firmware version */
763 dev_err(&dev
->udev
->dev
, "Incompatible driver and firmware"
772 static void sierra_net_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
775 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
777 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
779 /* kill the timer and work */
780 del_timer_sync(&priv
->sync_timer
);
781 cancel_work_sync(&priv
->sierra_net_kevent
);
783 /* tell modem we are going away */
784 status
= sierra_net_send_cmd(dev
, priv
->shdwn_msg
,
785 sizeof(priv
->shdwn_msg
), "Shutdown");
788 "usb_control_msg failed, status %d\n", status
);
790 usbnet_status_stop(dev
);
792 sierra_net_set_private(dev
, NULL
);
796 static struct sk_buff
*sierra_net_skb_clone(struct usbnet
*dev
,
797 struct sk_buff
*skb
, int len
)
799 struct sk_buff
*new_skb
;
802 new_skb
= skb_clone(skb
, GFP_ATOMIC
);
804 /* remove len bytes from original */
807 /* trim next packet to it's length */
809 skb_trim(new_skb
, len
);
811 if (netif_msg_rx_err(dev
))
812 netdev_err(dev
->net
, "failed to get skb\n");
813 dev
->net
->stats
.rx_dropped
++;
819 /* ---------------------------- Receive data path ----------------------*/
820 static int sierra_net_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
824 struct sk_buff
*new_skb
;
826 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
828 /* could contain multiple packets */
829 while (likely(skb
->len
)) {
830 err
= parse_hip(skb
->data
, skb
->len
, &hh
);
832 if (netif_msg_rx_err(dev
))
833 netdev_err(dev
->net
, "Invalid HIP header %d\n",
835 /* dev->net->stats.rx_errors incremented by caller */
836 dev
->net
->stats
.rx_length_errors
++;
840 /* Validate Extended HIP header */
841 if (!hh
.extmsgid
.is_present
842 || hh
.extmsgid
.word
!= SIERRA_NET_HIP_EXT_IP_IN_ID
) {
843 if (netif_msg_rx_err(dev
))
844 netdev_err(dev
->net
, "HIP/ETH: Invalid pkt\n");
846 dev
->net
->stats
.rx_frame_errors
++;
847 /* dev->net->stats.rx_errors incremented by caller */
851 skb_pull(skb
, hh
.hdrlen
);
853 /* We are going to accept this packet, prepare it.
854 * In case protocol is IPv6, keep it, otherwise force IPv4.
856 skb_reset_mac_header(skb
);
857 if (eth_hdr(skb
)->h_proto
!= cpu_to_be16(ETH_P_IPV6
))
858 eth_hdr(skb
)->h_proto
= cpu_to_be16(ETH_P_IP
);
859 eth_zero_addr(eth_hdr(skb
)->h_source
);
860 memcpy(eth_hdr(skb
)->h_dest
, dev
->net
->dev_addr
, ETH_ALEN
);
862 /* Last packet in batch handled by usbnet */
863 if (hh
.payload_len
.word
== skb
->len
)
866 new_skb
= sierra_net_skb_clone(dev
, skb
, hh
.payload_len
.word
);
868 usbnet_skb_return(dev
, new_skb
);
875 /* ---------------------------- Transmit data path ----------------------*/
876 static struct sk_buff
*sierra_net_tx_fixup(struct usbnet
*dev
,
877 struct sk_buff
*skb
, gfp_t flags
)
879 struct sierra_net_data
*priv
= sierra_net_get_private(dev
);
883 BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet
, data
)
884 < sizeof(struct cdc_state
));
886 dev_dbg(&dev
->udev
->dev
, "%s", __func__
);
887 if (priv
->link_up
&& check_ethip_packet(skb
, dev
) && is_ip(skb
)) {
888 /* enough head room as is? */
889 if (SIERRA_NET_HIP_EXT_HDR_LEN
<= skb_headroom(skb
)) {
890 /* Save the Eth/IP length and set up HIP hdr */
892 skb_push(skb
, SIERRA_NET_HIP_EXT_HDR_LEN
);
893 /* Handle ZLP issue */
894 need_tail
= ((len
+ SIERRA_NET_HIP_EXT_HDR_LEN
)
895 % dev
->maxpacket
== 0);
897 if (unlikely(skb_tailroom(skb
) == 0)) {
898 netdev_err(dev
->net
, "tx_fixup:"
899 "no room for packet\n");
900 dev_kfree_skb_any(skb
);
903 skb
->data
[skb
->len
] = 0;
908 build_hip(skb
->data
, len
, priv
);
912 * compensate in the future if necessary
914 netdev_err(dev
->net
, "tx_fixup: no room for HIP\n");
919 dev
->net
->stats
.tx_carrier_errors
++;
921 /* tx_dropped incremented by usbnet */
923 /* filter the packet out, release it */
924 dev_kfree_skb_any(skb
);
928 static const struct driver_info sierra_net_info_direct_ip
= {
929 .description
= "Sierra Wireless USB-to-WWAN Modem",
930 .flags
= FLAG_WWAN
| FLAG_SEND_ZLP
,
931 .bind
= sierra_net_bind
,
932 .unbind
= sierra_net_unbind
,
933 .status
= sierra_net_status
,
934 .rx_fixup
= sierra_net_rx_fixup
,
935 .tx_fixup
= sierra_net_tx_fixup
,
939 sierra_net_probe(struct usb_interface
*udev
, const struct usb_device_id
*prod
)
943 ret
= usbnet_probe(udev
, prod
);
945 struct usbnet
*dev
= usb_get_intfdata(udev
);
947 ret
= usbnet_status_start(dev
, GFP_KERNEL
);
949 /* Interrupt URB now set up; initiate sync sequence */
950 sierra_net_dosync(dev
);
956 #define DIRECT_IP_DEVICE(vend, prod) \
957 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 7), \
958 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
959 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 10), \
960 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
961 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 11), \
962 .driver_info = (unsigned long)&sierra_net_info_direct_ip}
964 static const struct usb_device_id products
[] = {
965 DIRECT_IP_DEVICE(0x1199, 0x68A3), /* Sierra Wireless USB-to-WWAN modem */
966 DIRECT_IP_DEVICE(0x0F3D, 0x68A3), /* AT&T Direct IP modem */
967 DIRECT_IP_DEVICE(0x1199, 0x68AA), /* Sierra Wireless Direct IP LTE modem */
968 DIRECT_IP_DEVICE(0x0F3D, 0x68AA), /* AT&T Direct IP LTE modem */
972 MODULE_DEVICE_TABLE(usb
, products
);
974 /* We are based on usbnet, so let it handle the USB driver specifics */
975 static struct usb_driver sierra_net_driver
= {
976 .name
= "sierra_net",
977 .id_table
= products
,
978 .probe
= sierra_net_probe
,
979 .disconnect
= usbnet_disconnect
,
980 .suspend
= usbnet_suspend
,
981 .resume
= usbnet_resume
,
983 .disable_hub_initiated_lpm
= 1,
986 module_usb_driver(sierra_net_driver
);
988 MODULE_AUTHOR(DRIVER_AUTHOR
);
989 MODULE_DESCRIPTION(DRIVER_DESC
);
990 MODULE_VERSION(DRIVER_VERSION
);
991 MODULE_LICENSE("GPL");