1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
33
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
52
53
54static atomic_t iface_counter = ATOMIC_INIT(0);
55
56
57
58
59#define SIERRA_NET_SYNCDELAY (2*HZ)
60
61
62#define SIERRA_NET_MAX_SUPPORTED_MTU 1500
63
64
65
66
67
68#define SIERRA_NET_USBCTL_BUF_LEN 1024
69
70
71#define SIERRA_NET_RX_URB_SIZE (8 * 1024)
72
73
74struct sierra_net_data {
75
76 u16 link_up;
77 u8 tx_hdr_template[4];
78
79 u8 sync_msg[4];
80 u8 shdwn_msg[4];
81
82
83 struct usbnet *usbnet;
84
85 u8 ifnum;
86
87
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;
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
106#define SIERRA_NET_HIP_EXTENDEDID 0x7F
107#define SIERRA_NET_HIP_HSYNC_ID 0x60
108#define SIERRA_NET_HIP_RESTART_ID 0x62
109#define SIERRA_NET_HIP_MSYNC_ID 0x20
110#define SIERRA_NET_HIP_SHUTD_ID 0x26
111
112#define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
113#define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
114
115
116#define SIERRA_NET_HIP_LSI_UMTSID 0x78
117
118
119#define SIERRA_NET_HIP_RCGI 0x64
120
121
122#define SIERRA_NET_PROTOCOL_UMTS 0x01
123#define SIERRA_NET_PROTOCOL_UMTS_DS 0x04
124
125#define SIERRA_NET_COVERAGE_NONE 0x00
126#define SIERRA_NET_COVERAGE_NOPACKET 0x01
127
128
129#define SIERRA_NET_SESSION_IDLE 0x00
130
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
139 u8 coverage;
140 u8 network_len;
141 u8 network[40];
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;
150 u8 pdp_addr[16];
151 u8 unused4[23];
152 u8 dns1_addr_len;
153 u8 dns1_addr[16];
154 u8 dns2_addr_len;
155 u8 dns2_addr[16];
156 u8 wins1_addr_len;
157 u8 wins1_addr[16];
158 u8 wins2_addr_len;
159 u8 wins2_addr[16];
160 u8 unused5[4];
161 u8 gw_addr_len;
162 u8 gw_addr[16];
163 u8 reserved[8];
164} __packed;
165
166struct lsi_umts_dual {
167 struct lsi_umts lsi;
168 u8 pdp_addr4_len;
169 u8 pdp_addr4[4];
170 u8 pdp_addr6_len;
171 u8 pdp_addr6[16];
172 u8 unused4[23];
173 u8 dns1_addr4_len;
174 u8 dns1_addr4[4];
175 u8 dns1_addr6_len;
176 u8 dns1_addr6[16];
177 u8 dns2_addr4_len;
178 u8 dns2_addr4[4];
179 u8 dns2_addr6_len;
180 u8 dns2_addr6[16];
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
192static int sierra_net_change_mtu(struct net_device *net, int new_mtu);
193
194
195static const struct net_device_ops sierra_net_device_ops = {
196 .ndo_open = usbnet_open,
197 .ndo_stop = usbnet_stop,
198 .ndo_start_xmit = usbnet_start_xmit,
199 .ndo_tx_timeout = usbnet_tx_timeout,
200 .ndo_change_mtu_rh74 = sierra_net_change_mtu,
201 .ndo_get_stats64 = usbnet_get_stats64,
202 .ndo_set_mac_address = eth_mac_addr,
203 .ndo_validate_addr = eth_validate_addr,
204};
205
206
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
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
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
228
229
230
231static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
232{
233 skb_reset_mac_header(skb);
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
263
264
265#define SIERRA_NET_HIP_HDR_LEN 4
266
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;
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;
298
299 curp = save16bit(&hh->extmsgid, curp);
300 hh->extmsgid.word &= 0x03FF;
301
302 hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
303 } else {
304 hh->payload_len.word &= 0x07FF;
305 hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
306 }
307
308 if (padded) {
309 hh->hdrlen++;
310 hh->payload_len.word--;
311 }
312
313
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
324
325
326 put_unaligned_be16(payloadlen, buf);
327 memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
328}
329
330
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
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
395 if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS) {
396 struct lsi_umts_single *single = (struct lsi_umts_single *)lsi;
397
398
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
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
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
460
461
462
463
464
465
466
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
477 priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
478 add_timer(&priv->sync_timer);
479}
480
481static void sierra_net_kevent(struct work_struct *work)
482{
483 struct sierra_net_data *priv =
484 container_of(work, struct sierra_net_data, sierra_net_kevent);
485 struct usbnet *dev = priv->usbnet;
486 int len;
487 int err;
488 u8 *buf;
489 u8 ifnum;
490
491 if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
492 clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
493
494
495 buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
496 if (!buf)
497 return;
498
499 ifnum = priv->ifnum;
500 len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
501 USB_CDC_GET_ENCAPSULATED_RESPONSE,
502 USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
503 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
504 USB_CTRL_SET_TIMEOUT);
505
506 if (len < 0) {
507 netdev_err(dev->net,
508 "usb_control_msg failed, status %d\n", len);
509 } else {
510 struct hip_hdr hh;
511
512 dev_dbg(&dev->udev->dev, "%s: Received status message,"
513 " %04x bytes", __func__, len);
514
515 err = parse_hip(buf, len, &hh);
516 if (err) {
517 netdev_err(dev->net, "%s: Bad packet,"
518 " parse result %d\n", __func__, err);
519 kfree(buf);
520 return;
521 }
522
523
524 if (len != hh.hdrlen + hh.payload_len.word) {
525 netdev_err(dev->net, "%s: Bad packet, received"
526 " %d, expected %d\n", __func__, len,
527 hh.hdrlen + hh.payload_len.word);
528 kfree(buf);
529 return;
530 }
531
532
533 switch (hh.msgid.byte) {
534 case SIERRA_NET_HIP_LSI_UMTSID:
535 dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
536 hh.msgspecific.byte);
537 sierra_net_handle_lsi(dev, buf, &hh);
538 break;
539 case SIERRA_NET_HIP_RESTART_ID:
540 dev_dbg(&dev->udev->dev, "Restart reported: %d,"
541 " stopping sync timer",
542 hh.msgspecific.byte);
543
544 del_timer_sync(&priv->sync_timer);
545 clear_bit(SIERRA_NET_TIMER_EXPIRY,
546 &priv->kevent_flags);
547 break;
548 case SIERRA_NET_HIP_HSYNC_ID:
549 dev_dbg(&dev->udev->dev, "SYNC received");
550 err = sierra_net_send_sync(dev);
551 if (err < 0)
552 netdev_err(dev->net,
553 "Send SYNC failed %d\n", err);
554 break;
555 case SIERRA_NET_HIP_EXTENDEDID:
556 netdev_err(dev->net, "Unrecognized HIP msg, "
557 "extmsgid 0x%04x\n", hh.extmsgid.word);
558 break;
559 case SIERRA_NET_HIP_RCGI:
560
561 break;
562 default:
563 netdev_err(dev->net, "Unrecognized HIP msg, "
564 "msgid 0x%02x\n", hh.msgid.byte);
565 break;
566 }
567 }
568 kfree(buf);
569 }
570
571 if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
572 clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
573 dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
574 sierra_net_dosync(priv->usbnet);
575 }
576
577 if (priv->kevent_flags)
578 dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
579 "kevent_flags = 0x%lx", priv->kevent_flags);
580}
581
582static void sierra_net_defer_kevent(struct usbnet *dev, int work)
583{
584 struct sierra_net_data *priv = sierra_net_get_private(dev);
585
586 set_bit(work, &priv->kevent_flags);
587 schedule_work(&priv->sierra_net_kevent);
588}
589
590
591
592
593static void sierra_sync_timer(struct timer_list *t)
594{
595 struct sierra_net_data *priv = from_timer(priv, t, sync_timer);
596 struct usbnet *dev = priv->usbnet;
597
598 dev_dbg(&dev->udev->dev, "%s", __func__);
599
600 sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
601}
602
603static void sierra_net_status(struct usbnet *dev, struct urb *urb)
604{
605 struct usb_cdc_notification *event;
606
607 dev_dbg(&dev->udev->dev, "%s", __func__);
608
609 if (urb->actual_length < sizeof *event)
610 return;
611
612
613 event = urb->transfer_buffer;
614 switch (event->bNotificationType) {
615 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
616 case USB_CDC_NOTIFY_SPEED_CHANGE:
617
618 break;
619 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
620 sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
621 break;
622 default:
623 netdev_err(dev->net, ": unexpected notification %02x!\n",
624 event->bNotificationType);
625 break;
626 }
627}
628
629static void sierra_net_get_drvinfo(struct net_device *net,
630 struct ethtool_drvinfo *info)
631{
632
633 usbnet_get_drvinfo(net, info);
634 strlcpy(info->driver, driver_name, sizeof(info->driver));
635 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
636}
637
638static u32 sierra_net_get_link(struct net_device *net)
639{
640 struct usbnet *dev = netdev_priv(net);
641
642 return sierra_net_get_private(dev)->link_up && netif_running(net);
643}
644
645static const struct ethtool_ops sierra_net_ethtool_ops = {
646 .get_drvinfo = sierra_net_get_drvinfo,
647 .get_link = sierra_net_get_link,
648 .get_msglevel = usbnet_get_msglevel,
649 .set_msglevel = usbnet_set_msglevel,
650 .nway_reset = usbnet_nway_reset,
651 .get_link_ksettings = usbnet_get_link_ksettings,
652 .set_link_ksettings = usbnet_set_link_ksettings,
653};
654
655
656static int sierra_net_change_mtu(struct net_device *net, int new_mtu)
657{
658 if (new_mtu > SIERRA_NET_MAX_SUPPORTED_MTU)
659 return -EINVAL;
660
661 return usbnet_change_mtu(net, new_mtu);
662}
663
664static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
665{
666 int result = 0;
667 __le16 attrdata;
668
669 result = usbnet_read_cmd(dev,
670
671 SWI_USB_REQUEST_GET_FW_ATTR,
672 USB_DIR_IN | USB_TYPE_VENDOR,
673 0x0000,
674 0x0000,
675 &attrdata,
676 sizeof(attrdata)
677 );
678
679 if (result < 0)
680 return -EIO;
681
682 *datap = le16_to_cpu(attrdata);
683 return result;
684}
685
686
687
688
689static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
690{
691 u8 ifacenum;
692 u8 numendpoints;
693 u16 fwattr = 0;
694 int status;
695 struct sierra_net_data *priv;
696 static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
697 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
698 static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
699 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
700
701 dev_dbg(&dev->udev->dev, "%s", __func__);
702
703 ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
704 numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
705
706 if (numendpoints != 3) {
707 dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
708 numendpoints);
709 return -ENODEV;
710 }
711
712 dev->status = NULL;
713 status = usbnet_get_endpoints(dev, intf);
714 if (status < 0) {
715 dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
716 status);
717 return -ENODEV;
718 }
719
720 priv = kzalloc(sizeof *priv, GFP_KERNEL);
721 if (!priv)
722 return -ENOMEM;
723
724 priv->usbnet = dev;
725 priv->ifnum = ifacenum;
726 dev->net->netdev_ops = &sierra_net_device_ops;
727
728
729 dev->net->dev_addr[ETH_ALEN-2] = atomic_inc_return(&iface_counter);
730 dev->net->dev_addr[ETH_ALEN-1] = ifacenum;
731
732
733 memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
734
735 sierra_net_set_ctx_index(priv, 0);
736
737
738 memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
739
740
741 dev->rx_urb_size = SIERRA_NET_RX_URB_SIZE;
742 if (dev->udev->speed != USB_SPEED_HIGH)
743 dev->rx_urb_size = min_t(size_t, 4096, SIERRA_NET_RX_URB_SIZE);
744
745 dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
746 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
747
748
749 dev->net->flags |= IFF_NOARP;
750 dev->net->ethtool_ops = &sierra_net_ethtool_ops;
751 netif_carrier_off(dev->net);
752
753 sierra_net_set_private(dev, priv);
754
755 priv->kevent_flags = 0;
756
757
758 INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
759
760
761 timer_setup(&priv->sync_timer, sierra_sync_timer, 0);
762
763
764 status = sierra_net_get_fw_attr(dev, &fwattr);
765 dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
766
767
768 if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
769
770 dev_err(&dev->udev->dev, "Incompatible driver and firmware"
771 " versions\n");
772 kfree(priv);
773 return -ENODEV;
774 }
775
776 return 0;
777}
778
779static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
780{
781 int status;
782 struct sierra_net_data *priv = sierra_net_get_private(dev);
783
784 dev_dbg(&dev->udev->dev, "%s", __func__);
785
786
787 del_timer_sync(&priv->sync_timer);
788 cancel_work_sync(&priv->sierra_net_kevent);
789
790
791 status = sierra_net_send_cmd(dev, priv->shdwn_msg,
792 sizeof(priv->shdwn_msg), "Shutdown");
793 if (status < 0)
794 netdev_err(dev->net,
795 "usb_control_msg failed, status %d\n", status);
796
797 usbnet_status_stop(dev);
798
799 sierra_net_set_private(dev, NULL);
800 kfree(priv);
801}
802
803static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
804 struct sk_buff *skb, int len)
805{
806 struct sk_buff *new_skb;
807
808
809 new_skb = skb_clone(skb, GFP_ATOMIC);
810
811
812 skb_pull(skb, len);
813
814
815 if (new_skb) {
816 skb_trim(new_skb, len);
817 } else {
818 if (netif_msg_rx_err(dev))
819 netdev_err(dev->net, "failed to get skb\n");
820 dev->net->stats.rx_dropped++;
821 }
822
823 return new_skb;
824}
825
826
827static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
828{
829 int err;
830 struct hip_hdr hh;
831 struct sk_buff *new_skb;
832
833 dev_dbg(&dev->udev->dev, "%s", __func__);
834
835
836 while (likely(skb->len)) {
837 err = parse_hip(skb->data, skb->len, &hh);
838 if (err) {
839 if (netif_msg_rx_err(dev))
840 netdev_err(dev->net, "Invalid HIP header %d\n",
841 err);
842
843 dev->net->stats.rx_length_errors++;
844 return 0;
845 }
846
847
848 if (!hh.extmsgid.is_present
849 || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
850 if (netif_msg_rx_err(dev))
851 netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
852
853 dev->net->stats.rx_frame_errors++;
854
855 return 0;
856 }
857
858 skb_pull(skb, hh.hdrlen);
859
860
861
862
863 skb_reset_mac_header(skb);
864 if (eth_hdr(skb)->h_proto != cpu_to_be16(ETH_P_IPV6))
865 eth_hdr(skb)->h_proto = cpu_to_be16(ETH_P_IP);
866 eth_zero_addr(eth_hdr(skb)->h_source);
867 memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
868
869
870 if (hh.payload_len.word == skb->len)
871 return 1;
872
873 new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
874 if (new_skb)
875 usbnet_skb_return(dev, new_skb);
876
877 }
878
879 return 0;
880}
881
882
883static struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev,
884 struct sk_buff *skb, gfp_t flags)
885{
886 struct sierra_net_data *priv = sierra_net_get_private(dev);
887 u16 len;
888 bool need_tail;
889
890 BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet, data)
891 < sizeof(struct cdc_state));
892
893 dev_dbg(&dev->udev->dev, "%s", __func__);
894 if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
895
896 if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
897
898 len = skb->len;
899 skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
900
901 need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
902 % dev->maxpacket == 0);
903 if (need_tail) {
904 if (unlikely(skb_tailroom(skb) == 0)) {
905 netdev_err(dev->net, "tx_fixup:"
906 "no room for packet\n");
907 dev_kfree_skb_any(skb);
908 return NULL;
909 } else {
910 skb->data[skb->len] = 0;
911 __skb_put(skb, 1);
912 len = len + 1;
913 }
914 }
915 build_hip(skb->data, len, priv);
916 return skb;
917 } else {
918
919
920
921 netdev_err(dev->net, "tx_fixup: no room for HIP\n");
922 }
923 }
924
925 if (!priv->link_up)
926 dev->net->stats.tx_carrier_errors++;
927
928
929
930
931 dev_kfree_skb_any(skb);
932 return NULL;
933}
934
935static const struct driver_info sierra_net_info_direct_ip = {
936 .description = "Sierra Wireless USB-to-WWAN Modem",
937 .flags = FLAG_WWAN | FLAG_SEND_ZLP,
938 .bind = sierra_net_bind,
939 .unbind = sierra_net_unbind,
940 .status = sierra_net_status,
941 .rx_fixup = sierra_net_rx_fixup,
942 .tx_fixup = sierra_net_tx_fixup,
943};
944
945static int
946sierra_net_probe(struct usb_interface *udev, const struct usb_device_id *prod)
947{
948 int ret;
949
950 ret = usbnet_probe(udev, prod);
951 if (ret == 0) {
952 struct usbnet *dev = usb_get_intfdata(udev);
953
954 ret = usbnet_status_start(dev, GFP_KERNEL);
955 if (ret == 0) {
956
957 sierra_net_dosync(dev);
958 }
959 }
960 return ret;
961}
962
963#define DIRECT_IP_DEVICE(vend, prod) \
964 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 7), \
965 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
966 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 10), \
967 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
968 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 11), \
969 .driver_info = (unsigned long)&sierra_net_info_direct_ip}
970
971static const struct usb_device_id products[] = {
972 DIRECT_IP_DEVICE(0x1199, 0x68A3),
973 DIRECT_IP_DEVICE(0x0F3D, 0x68A3),
974 DIRECT_IP_DEVICE(0x1199, 0x68AA),
975 DIRECT_IP_DEVICE(0x0F3D, 0x68AA),
976
977 {},
978};
979MODULE_DEVICE_TABLE(usb, products);
980
981
982static struct usb_driver sierra_net_driver = {
983 .name = "sierra_net",
984 .id_table = products,
985 .probe = sierra_net_probe,
986 .disconnect = usbnet_disconnect,
987 .suspend = usbnet_suspend,
988 .resume = usbnet_resume,
989 .no_dynamic_id = 1,
990 .disable_hub_initiated_lpm = 1,
991};
992
993module_usb_driver(sierra_net_driver);
994
995MODULE_AUTHOR(DRIVER_AUTHOR);
996MODULE_DESCRIPTION(DRIVER_DESC);
997MODULE_VERSION(DRIVER_VERSION);
998MODULE_LICENSE("GPL");
999