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
192
193static void sierra_sync_timer(unsigned long syncdata);
194
195
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_get_stats64 = usbnet_get_stats64,
203 .ndo_set_mac_address = eth_mac_addr,
204 .ndo_validate_addr = eth_validate_addr,
205};
206
207
208static inline struct sierra_net_data *sierra_net_get_private(struct usbnet *dev)
209{
210 return (struct sierra_net_data *)dev->data[0];
211}
212
213
214static inline void sierra_net_set_private(struct usbnet *dev,
215 struct sierra_net_data *priv)
216{
217 dev->data[0] = (unsigned long)priv;
218}
219
220
221static inline int is_ip(struct sk_buff *skb)
222{
223 return skb->protocol == cpu_to_be16(ETH_P_IP) ||
224 skb->protocol == cpu_to_be16(ETH_P_IPV6);
225}
226
227
228
229
230
231
232static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
233{
234 skb_reset_mac_header(skb);
235
236 if (skb_is_nonlinear(skb)) {
237 netdev_err(dev->net, "Non linear buffer-dropping\n");
238 return 0;
239 }
240
241 if (!pskb_may_pull(skb, ETH_HLEN))
242 return 0;
243 skb->protocol = eth_hdr(skb)->h_proto;
244
245 return 1;
246}
247
248static const u8 *save16bit(struct param *p, const u8 *datap)
249{
250 p->is_present = 1;
251 p->word = get_unaligned_be16(datap);
252 return datap + sizeof(p->word);
253}
254
255static const u8 *save8bit(struct param *p, const u8 *datap)
256{
257 p->is_present = 1;
258 p->byte = *datap;
259 return datap + sizeof(p->byte);
260}
261
262
263
264
265
266#define SIERRA_NET_HIP_HDR_LEN 4
267
268#define SIERRA_NET_HIP_EXT_HDR_LEN 6
269
270struct hip_hdr {
271 int hdrlen;
272 struct param payload_len;
273 struct param msgid;
274 struct param msgspecific;
275 struct param extmsgid;
276};
277
278static int parse_hip(const u8 *buf, const u32 buflen, struct hip_hdr *hh)
279{
280 const u8 *curp = buf;
281 int padded;
282
283 if (buflen < SIERRA_NET_HIP_HDR_LEN)
284 return -EPROTO;
285
286 curp = save16bit(&hh->payload_len, curp);
287 curp = save8bit(&hh->msgid, curp);
288 curp = save8bit(&hh->msgspecific, curp);
289
290 padded = hh->msgid.byte & 0x80;
291 hh->msgid.byte &= 0x7F;
292
293 hh->extmsgid.is_present = (hh->msgid.byte == SIERRA_NET_HIP_EXTENDEDID);
294 if (hh->extmsgid.is_present) {
295 if (buflen < SIERRA_NET_HIP_EXT_HDR_LEN)
296 return -EPROTO;
297
298 hh->payload_len.word &= 0x3FFF;
299
300 curp = save16bit(&hh->extmsgid, curp);
301 hh->extmsgid.word &= 0x03FF;
302
303 hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
304 } else {
305 hh->payload_len.word &= 0x07FF;
306 hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
307 }
308
309 if (padded) {
310 hh->hdrlen++;
311 hh->payload_len.word--;
312 }
313
314
315 if (buflen < (hh->hdrlen + hh->payload_len.word))
316 return -EINVAL;
317
318 return 0;
319}
320
321static void build_hip(u8 *buf, const u16 payloadlen,
322 struct sierra_net_data *priv)
323{
324
325
326
327 put_unaligned_be16(payloadlen, buf);
328 memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
329}
330
331
332
333
334static int sierra_net_send_cmd(struct usbnet *dev,
335 u8 *cmd, int cmdlen, const char * cmd_name)
336{
337 struct sierra_net_data *priv = sierra_net_get_private(dev);
338 int status;
339
340 status = usbnet_write_cmd(dev, USB_CDC_SEND_ENCAPSULATED_COMMAND,
341 USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
342 0, priv->ifnum, cmd, cmdlen);
343
344 if (status != cmdlen && status != -ENODEV)
345 netdev_err(dev->net, "Submit %s failed %d\n", cmd_name, status);
346
347 return status;
348}
349
350static int sierra_net_send_sync(struct usbnet *dev)
351{
352 int status;
353 struct sierra_net_data *priv = sierra_net_get_private(dev);
354
355 dev_dbg(&dev->udev->dev, "%s", __func__);
356
357 status = sierra_net_send_cmd(dev, priv->sync_msg,
358 sizeof(priv->sync_msg), "SYNC");
359
360 return status;
361}
362
363static void sierra_net_set_ctx_index(struct sierra_net_data *priv, u8 ctx_ix)
364{
365 dev_dbg(&(priv->usbnet->udev->dev), "%s %d", __func__, ctx_ix);
366 priv->tx_hdr_template[0] = 0x3F;
367 priv->tx_hdr_template[1] = ctx_ix;
368 *((__be16 *)&priv->tx_hdr_template[2]) =
369 cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID);
370}
371
372static inline int sierra_net_is_valid_addrlen(u8 len)
373{
374 return len == sizeof(struct in_addr);
375}
376
377static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen)
378{
379 struct lsi_umts *lsi = (struct lsi_umts *)data;
380 u32 expected_length;
381
382 if (datalen < sizeof(struct lsi_umts_single)) {
383 netdev_err(dev->net, "%s: Data length %d, exp >= %zu\n",
384 __func__, datalen, sizeof(struct lsi_umts_single));
385 return -1;
386 }
387
388
389 if (lsi->session_state == SIERRA_NET_SESSION_IDLE) {
390 netdev_err(dev->net, "Session idle, 0x%02x\n",
391 lsi->session_state);
392 return 0;
393 }
394
395
396 if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS) {
397 struct lsi_umts_single *single = (struct lsi_umts_single *)lsi;
398
399
400 if (single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV4 &&
401 single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV6) {
402 netdev_err(dev->net, "Link type unsupported: 0x%02x\n",
403 single->link_type);
404 return -1;
405 }
406 expected_length = SIERRA_NET_LSI_UMTS_STATUS_LEN;
407 } else if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS_DS) {
408 expected_length = SIERRA_NET_LSI_UMTS_DS_STATUS_LEN;
409 } else {
410 netdev_err(dev->net, "Protocol unsupported, 0x%02x\n",
411 lsi->protocol);
412 return -1;
413 }
414
415 if (be16_to_cpu(lsi->length) != expected_length) {
416 netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
417 __func__, be16_to_cpu(lsi->length), expected_length);
418 return -1;
419 }
420
421
422 if (lsi->coverage == SIERRA_NET_COVERAGE_NONE ||
423 lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) {
424 netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage);
425 return 0;
426 }
427
428
429 return 1;
430}
431
432static void sierra_net_handle_lsi(struct usbnet *dev, char *data,
433 struct hip_hdr *hh)
434{
435 struct sierra_net_data *priv = sierra_net_get_private(dev);
436 int link_up;
437
438 link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen,
439 hh->payload_len.word);
440 if (link_up < 0) {
441 netdev_err(dev->net, "Invalid LSI\n");
442 return;
443 }
444 if (link_up) {
445 sierra_net_set_ctx_index(priv, hh->msgspecific.byte);
446 priv->link_up = 1;
447 } else {
448 priv->link_up = 0;
449 }
450 usbnet_link_change(dev, link_up, 0);
451}
452
453static void sierra_net_dosync(struct usbnet *dev)
454{
455 int status;
456 struct sierra_net_data *priv = sierra_net_get_private(dev);
457
458 dev_dbg(&dev->udev->dev, "%s", __func__);
459
460
461
462
463
464
465
466
467
468 status = sierra_net_send_sync(dev);
469 if (status < 0)
470 netdev_err(dev->net,
471 "Send SYNC failed, status %d\n", status);
472 status = sierra_net_send_sync(dev);
473 if (status < 0)
474 netdev_err(dev->net,
475 "Send SYNC failed, status %d\n", status);
476
477
478 priv->sync_timer.function = sierra_sync_timer;
479 priv->sync_timer.data = (unsigned long) dev;
480 priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
481 add_timer(&priv->sync_timer);
482}
483
484static void sierra_net_kevent(struct work_struct *work)
485{
486 struct sierra_net_data *priv =
487 container_of(work, struct sierra_net_data, sierra_net_kevent);
488 struct usbnet *dev = priv->usbnet;
489 int len;
490 int err;
491 u8 *buf;
492 u8 ifnum;
493
494 if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
495 clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
496
497
498 buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
499 if (!buf)
500 return;
501
502 ifnum = priv->ifnum;
503 len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
504 USB_CDC_GET_ENCAPSULATED_RESPONSE,
505 USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
506 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
507 USB_CTRL_SET_TIMEOUT);
508
509 if (len < 0) {
510 netdev_err(dev->net,
511 "usb_control_msg failed, status %d\n", len);
512 } else {
513 struct hip_hdr hh;
514
515 dev_dbg(&dev->udev->dev, "%s: Received status message,"
516 " %04x bytes", __func__, len);
517
518 err = parse_hip(buf, len, &hh);
519 if (err) {
520 netdev_err(dev->net, "%s: Bad packet,"
521 " parse result %d\n", __func__, err);
522 kfree(buf);
523 return;
524 }
525
526
527 if (len != hh.hdrlen + hh.payload_len.word) {
528 netdev_err(dev->net, "%s: Bad packet, received"
529 " %d, expected %d\n", __func__, len,
530 hh.hdrlen + hh.payload_len.word);
531 kfree(buf);
532 return;
533 }
534
535
536 switch (hh.msgid.byte) {
537 case SIERRA_NET_HIP_LSI_UMTSID:
538 dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
539 hh.msgspecific.byte);
540 sierra_net_handle_lsi(dev, buf, &hh);
541 break;
542 case SIERRA_NET_HIP_RESTART_ID:
543 dev_dbg(&dev->udev->dev, "Restart reported: %d,"
544 " stopping sync timer",
545 hh.msgspecific.byte);
546
547 del_timer_sync(&priv->sync_timer);
548 clear_bit(SIERRA_NET_TIMER_EXPIRY,
549 &priv->kevent_flags);
550 break;
551 case SIERRA_NET_HIP_HSYNC_ID:
552 dev_dbg(&dev->udev->dev, "SYNC received");
553 err = sierra_net_send_sync(dev);
554 if (err < 0)
555 netdev_err(dev->net,
556 "Send SYNC failed %d\n", err);
557 break;
558 case SIERRA_NET_HIP_EXTENDEDID:
559 netdev_err(dev->net, "Unrecognized HIP msg, "
560 "extmsgid 0x%04x\n", hh.extmsgid.word);
561 break;
562 case SIERRA_NET_HIP_RCGI:
563
564 break;
565 default:
566 netdev_err(dev->net, "Unrecognized HIP msg, "
567 "msgid 0x%02x\n", hh.msgid.byte);
568 break;
569 }
570 }
571 kfree(buf);
572 }
573
574 if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
575 clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
576 dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
577 sierra_net_dosync(priv->usbnet);
578 }
579
580 if (priv->kevent_flags)
581 dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
582 "kevent_flags = 0x%lx", priv->kevent_flags);
583}
584
585static void sierra_net_defer_kevent(struct usbnet *dev, int work)
586{
587 struct sierra_net_data *priv = sierra_net_get_private(dev);
588
589 set_bit(work, &priv->kevent_flags);
590 schedule_work(&priv->sierra_net_kevent);
591}
592
593
594
595
596static void sierra_sync_timer(unsigned long syncdata)
597{
598 struct usbnet *dev = (struct usbnet *)syncdata;
599
600 dev_dbg(&dev->udev->dev, "%s", __func__);
601
602 sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
603}
604
605static void sierra_net_status(struct usbnet *dev, struct urb *urb)
606{
607 struct usb_cdc_notification *event;
608
609 dev_dbg(&dev->udev->dev, "%s", __func__);
610
611 if (urb->actual_length < sizeof *event)
612 return;
613
614
615 event = urb->transfer_buffer;
616 switch (event->bNotificationType) {
617 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
618 case USB_CDC_NOTIFY_SPEED_CHANGE:
619
620 break;
621 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
622 sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
623 break;
624 default:
625 netdev_err(dev->net, ": unexpected notification %02x!\n",
626 event->bNotificationType);
627 break;
628 }
629}
630
631static void sierra_net_get_drvinfo(struct net_device *net,
632 struct ethtool_drvinfo *info)
633{
634
635 usbnet_get_drvinfo(net, info);
636 strlcpy(info->driver, driver_name, sizeof(info->driver));
637 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
638}
639
640static u32 sierra_net_get_link(struct net_device *net)
641{
642 struct usbnet *dev = netdev_priv(net);
643
644 return sierra_net_get_private(dev)->link_up && netif_running(net);
645}
646
647static const struct ethtool_ops sierra_net_ethtool_ops = {
648 .get_drvinfo = sierra_net_get_drvinfo,
649 .get_link = sierra_net_get_link,
650 .get_msglevel = usbnet_get_msglevel,
651 .set_msglevel = usbnet_set_msglevel,
652 .nway_reset = usbnet_nway_reset,
653 .get_link_ksettings = usbnet_get_link_ksettings,
654 .set_link_ksettings = usbnet_set_link_ksettings,
655};
656
657static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
658{
659 int result = 0;
660 __le16 attrdata;
661
662 result = usbnet_read_cmd(dev,
663
664 SWI_USB_REQUEST_GET_FW_ATTR,
665 USB_DIR_IN | USB_TYPE_VENDOR,
666 0x0000,
667 0x0000,
668 &attrdata,
669 sizeof(attrdata)
670 );
671
672 if (result < 0)
673 return -EIO;
674
675 *datap = le16_to_cpu(attrdata);
676 return result;
677}
678
679
680
681
682static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
683{
684 u8 ifacenum;
685 u8 numendpoints;
686 u16 fwattr = 0;
687 int status;
688 struct sierra_net_data *priv;
689 static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
690 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
691 static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
692 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
693
694 dev_dbg(&dev->udev->dev, "%s", __func__);
695
696 ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
697 numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
698
699 if (numendpoints != 3) {
700 dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
701 numendpoints);
702 return -ENODEV;
703 }
704
705 dev->status = NULL;
706 status = usbnet_get_endpoints(dev, intf);
707 if (status < 0) {
708 dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
709 status);
710 return -ENODEV;
711 }
712
713 priv = kzalloc(sizeof *priv, GFP_KERNEL);
714 if (!priv)
715 return -ENOMEM;
716
717 priv->usbnet = dev;
718 priv->ifnum = ifacenum;
719 dev->net->netdev_ops = &sierra_net_device_ops;
720
721
722 dev->net->dev_addr[ETH_ALEN-2] = atomic_inc_return(&iface_counter);
723 dev->net->dev_addr[ETH_ALEN-1] = ifacenum;
724
725
726 memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
727
728 sierra_net_set_ctx_index(priv, 0);
729
730
731 memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
732
733
734 dev->rx_urb_size = SIERRA_NET_RX_URB_SIZE;
735 if (dev->udev->speed != USB_SPEED_HIGH)
736 dev->rx_urb_size = min_t(size_t, 4096, SIERRA_NET_RX_URB_SIZE);
737
738 dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
739 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
740 dev->net->max_mtu = SIERRA_NET_MAX_SUPPORTED_MTU;
741
742
743 dev->net->flags |= IFF_NOARP;
744 dev->net->ethtool_ops = &sierra_net_ethtool_ops;
745 netif_carrier_off(dev->net);
746
747 sierra_net_set_private(dev, priv);
748
749 priv->kevent_flags = 0;
750
751
752 INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
753
754
755 init_timer(&priv->sync_timer);
756
757
758 status = sierra_net_get_fw_attr(dev, &fwattr);
759 dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
760
761
762 if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
763
764 dev_err(&dev->udev->dev, "Incompatible driver and firmware"
765 " versions\n");
766 kfree(priv);
767 return -ENODEV;
768 }
769
770 return 0;
771}
772
773static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
774{
775 int status;
776 struct sierra_net_data *priv = sierra_net_get_private(dev);
777
778 dev_dbg(&dev->udev->dev, "%s", __func__);
779
780
781 del_timer_sync(&priv->sync_timer);
782 cancel_work_sync(&priv->sierra_net_kevent);
783
784
785 status = sierra_net_send_cmd(dev, priv->shdwn_msg,
786 sizeof(priv->shdwn_msg), "Shutdown");
787 if (status < 0)
788 netdev_err(dev->net,
789 "usb_control_msg failed, status %d\n", status);
790
791 usbnet_status_stop(dev);
792
793 sierra_net_set_private(dev, NULL);
794 kfree(priv);
795}
796
797static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
798 struct sk_buff *skb, int len)
799{
800 struct sk_buff *new_skb;
801
802
803 new_skb = skb_clone(skb, GFP_ATOMIC);
804
805
806 skb_pull(skb, len);
807
808
809 if (new_skb) {
810 skb_trim(new_skb, len);
811 } else {
812 if (netif_msg_rx_err(dev))
813 netdev_err(dev->net, "failed to get skb\n");
814 dev->net->stats.rx_dropped++;
815 }
816
817 return new_skb;
818}
819
820
821static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
822{
823 int err;
824 struct hip_hdr hh;
825 struct sk_buff *new_skb;
826
827 dev_dbg(&dev->udev->dev, "%s", __func__);
828
829
830 while (likely(skb->len)) {
831 err = parse_hip(skb->data, skb->len, &hh);
832 if (err) {
833 if (netif_msg_rx_err(dev))
834 netdev_err(dev->net, "Invalid HIP header %d\n",
835 err);
836
837 dev->net->stats.rx_length_errors++;
838 return 0;
839 }
840
841
842 if (!hh.extmsgid.is_present
843 || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
844 if (netif_msg_rx_err(dev))
845 netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
846
847 dev->net->stats.rx_frame_errors++;
848
849 return 0;
850 }
851
852 skb_pull(skb, hh.hdrlen);
853
854
855
856
857 skb_reset_mac_header(skb);
858 if (eth_hdr(skb)->h_proto != cpu_to_be16(ETH_P_IPV6))
859 eth_hdr(skb)->h_proto = cpu_to_be16(ETH_P_IP);
860 eth_zero_addr(eth_hdr(skb)->h_source);
861 memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
862
863
864 if (hh.payload_len.word == skb->len)
865 return 1;
866
867 new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
868 if (new_skb)
869 usbnet_skb_return(dev, new_skb);
870
871 }
872
873 return 0;
874}
875
876
877static struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev,
878 struct sk_buff *skb, gfp_t flags)
879{
880 struct sierra_net_data *priv = sierra_net_get_private(dev);
881 u16 len;
882 bool need_tail;
883
884 BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet, data)
885 < sizeof(struct cdc_state));
886
887 dev_dbg(&dev->udev->dev, "%s", __func__);
888 if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
889
890 if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
891
892 len = skb->len;
893 skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
894
895 need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
896 % dev->maxpacket == 0);
897 if (need_tail) {
898 if (unlikely(skb_tailroom(skb) == 0)) {
899 netdev_err(dev->net, "tx_fixup:"
900 "no room for packet\n");
901 dev_kfree_skb_any(skb);
902 return NULL;
903 } else {
904 skb->data[skb->len] = 0;
905 __skb_put(skb, 1);
906 len = len + 1;
907 }
908 }
909 build_hip(skb->data, len, priv);
910 return skb;
911 } else {
912
913
914
915 netdev_err(dev->net, "tx_fixup: no room for HIP\n");
916 }
917 }
918
919 if (!priv->link_up)
920 dev->net->stats.tx_carrier_errors++;
921
922
923
924
925 dev_kfree_skb_any(skb);
926 return NULL;
927}
928
929static const struct driver_info sierra_net_info_direct_ip = {
930 .description = "Sierra Wireless USB-to-WWAN Modem",
931 .flags = FLAG_WWAN | FLAG_SEND_ZLP,
932 .bind = sierra_net_bind,
933 .unbind = sierra_net_unbind,
934 .status = sierra_net_status,
935 .rx_fixup = sierra_net_rx_fixup,
936 .tx_fixup = sierra_net_tx_fixup,
937};
938
939static int
940sierra_net_probe(struct usb_interface *udev, const struct usb_device_id *prod)
941{
942 int ret;
943
944 ret = usbnet_probe(udev, prod);
945 if (ret == 0) {
946 struct usbnet *dev = usb_get_intfdata(udev);
947
948 ret = usbnet_status_start(dev, GFP_KERNEL);
949 if (ret == 0) {
950
951 sierra_net_dosync(dev);
952 }
953 }
954 return ret;
955}
956
957#define DIRECT_IP_DEVICE(vend, prod) \
958 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 7), \
959 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
960 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 10), \
961 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
962 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 11), \
963 .driver_info = (unsigned long)&sierra_net_info_direct_ip}
964
965static const struct usb_device_id products[] = {
966 DIRECT_IP_DEVICE(0x1199, 0x68A3),
967 DIRECT_IP_DEVICE(0x0F3D, 0x68A3),
968 DIRECT_IP_DEVICE(0x1199, 0x68AA),
969 DIRECT_IP_DEVICE(0x0F3D, 0x68AA),
970
971 {},
972};
973MODULE_DEVICE_TABLE(usb, products);
974
975
976static struct usb_driver sierra_net_driver = {
977 .name = "sierra_net",
978 .id_table = products,
979 .probe = sierra_net_probe,
980 .disconnect = usbnet_disconnect,
981 .suspend = usbnet_suspend,
982 .resume = usbnet_resume,
983 .no_dynamic_id = 1,
984 .disable_hub_initiated_lpm = 1,
985};
986
987module_usb_driver(sierra_net_driver);
988
989MODULE_AUTHOR(DRIVER_AUTHOR);
990MODULE_DESCRIPTION(DRIVER_DESC);
991MODULE_VERSION(DRIVER_VERSION);
992MODULE_LICENSE("GPL");
993