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