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
28#define DRIVER_VERSION "v.2.0"
29#define DRIVER_AUTHOR "Paxton Smith, Matthew Safar, Rory Filer"
30#define DRIVER_DESC "USB-to-WWAN Driver for Sierra Wireless modems"
31static const char driver_name[] = "sierra_net";
32
33
34
35
36#include <linux/module.h>
37#include <linux/etherdevice.h>
38#include <linux/ethtool.h>
39#include <linux/mii.h>
40#include <linux/sched.h>
41#include <linux/timer.h>
42#include <linux/usb.h>
43#include <linux/usb/cdc.h>
44#include <net/ip.h>
45#include <net/udp.h>
46#include <asm/unaligned.h>
47#include <linux/usb/usbnet.h>
48
49#define SWI_USB_REQUEST_GET_FW_ATTR 0x06
50#define SWI_GET_FW_ATTR_MASK 0x08
51
52
53
54
55static atomic_t iface_counter = ATOMIC_INIT(0);
56
57
58
59
60#define SIERRA_NET_SYNCDELAY (2*HZ)
61
62
63#define SIERRA_NET_MAX_SUPPORTED_MTU 1500
64
65
66
67
68
69#define SIERRA_NET_USBCTL_BUF_LEN 1024
70
71
72#define SIERRA_NET_RX_URB_SIZE (8 * 1024)
73
74
75struct sierra_net_data {
76
77 u8 ethr_hdr_tmpl[ETH_HLEN];
78
79 u16 link_up;
80 u8 tx_hdr_template[4];
81
82 u8 sync_msg[4];
83 u8 shdwn_msg[4];
84
85
86 struct usbnet *usbnet;
87
88 u8 ifnum;
89
90
91#define SIERRA_NET_EVENT_RESP_AVAIL 0x01
92#define SIERRA_NET_TIMER_EXPIRY 0x02
93 unsigned long kevent_flags;
94 struct work_struct sierra_net_kevent;
95 struct timer_list sync_timer;
96};
97
98struct param {
99 int is_present;
100 union {
101 void *ptr;
102 u32 dword;
103 u16 word;
104 u8 byte;
105 };
106};
107
108
109#define SIERRA_NET_HIP_EXTENDEDID 0x7F
110#define SIERRA_NET_HIP_HSYNC_ID 0x60
111#define SIERRA_NET_HIP_RESTART_ID 0x62
112#define SIERRA_NET_HIP_MSYNC_ID 0x20
113#define SIERRA_NET_HIP_SHUTD_ID 0x26
114
115#define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
116#define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
117
118
119#define SIERRA_NET_HIP_LSI_UMTSID 0x78
120
121
122#define SIERRA_NET_HIP_RCGI 0x64
123
124
125#define SIERRA_NET_PROTOCOL_UMTS 0x01
126
127#define SIERRA_NET_COVERAGE_NONE 0x00
128#define SIERRA_NET_COVERAGE_NOPACKET 0x01
129
130
131#define SIERRA_NET_SESSION_IDLE 0x00
132
133#define SIERRA_NET_AS_LINK_TYPE_IPv4 0x00
134
135struct lsi_umts {
136 u8 protocol;
137 u8 unused1;
138 __be16 length;
139
140 u8 coverage;
141 u8 unused2[41];
142 u8 session_state;
143 u8 unused3[33];
144 u8 link_type;
145 u8 pdp_addr_len;
146 u8 pdp_addr[16];
147 u8 unused4[23];
148 u8 dns1_addr_len;
149 u8 dns1_addr[16];
150 u8 dns2_addr_len;
151 u8 dns2_addr[16];
152 u8 wins1_addr_len;
153 u8 wins1_addr[16];
154 u8 wins2_addr_len;
155 u8 wins2_addr[16];
156 u8 unused5[4];
157 u8 gw_addr_len;
158 u8 gw_addr[16];
159 u8 reserved[8];
160} __packed;
161
162#define SIERRA_NET_LSI_COMMON_LEN 4
163#define SIERRA_NET_LSI_UMTS_LEN (sizeof(struct lsi_umts))
164#define SIERRA_NET_LSI_UMTS_STATUS_LEN \
165 (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN)
166
167
168static void sierra_sync_timer(unsigned long syncdata);
169static int sierra_net_change_mtu(struct net_device *net, int new_mtu);
170
171
172static const struct net_device_ops sierra_net_device_ops = {
173 .ndo_open = usbnet_open,
174 .ndo_stop = usbnet_stop,
175 .ndo_start_xmit = usbnet_start_xmit,
176 .ndo_tx_timeout = usbnet_tx_timeout,
177 .ndo_change_mtu = sierra_net_change_mtu,
178 .ndo_set_mac_address = eth_mac_addr,
179 .ndo_validate_addr = eth_validate_addr,
180};
181
182
183static inline struct sierra_net_data *sierra_net_get_private(struct usbnet *dev)
184{
185 return (struct sierra_net_data *)dev->data[0];
186}
187
188
189static inline void sierra_net_set_private(struct usbnet *dev,
190 struct sierra_net_data *priv)
191{
192 dev->data[0] = (unsigned long)priv;
193}
194
195
196static inline int is_ip(struct sk_buff *skb)
197{
198 return skb->protocol == cpu_to_be16(ETH_P_IP);
199}
200
201
202
203
204
205
206static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
207{
208 skb_reset_mac_header(skb);
209
210 if (skb_is_nonlinear(skb)) {
211 netdev_err(dev->net, "Non linear buffer-dropping\n");
212 return 0;
213 }
214
215 if (!pskb_may_pull(skb, ETH_HLEN))
216 return 0;
217 skb->protocol = eth_hdr(skb)->h_proto;
218
219 return 1;
220}
221
222static const u8 *save16bit(struct param *p, const u8 *datap)
223{
224 p->is_present = 1;
225 p->word = get_unaligned_be16(datap);
226 return datap + sizeof(p->word);
227}
228
229static const u8 *save8bit(struct param *p, const u8 *datap)
230{
231 p->is_present = 1;
232 p->byte = *datap;
233 return datap + sizeof(p->byte);
234}
235
236
237
238
239
240#define SIERRA_NET_HIP_HDR_LEN 4
241
242#define SIERRA_NET_HIP_EXT_HDR_LEN 6
243
244struct hip_hdr {
245 int hdrlen;
246 struct param payload_len;
247 struct param msgid;
248 struct param msgspecific;
249 struct param extmsgid;
250};
251
252static int parse_hip(const u8 *buf, const u32 buflen, struct hip_hdr *hh)
253{
254 const u8 *curp = buf;
255 int padded;
256
257 if (buflen < SIERRA_NET_HIP_HDR_LEN)
258 return -EPROTO;
259
260 curp = save16bit(&hh->payload_len, curp);
261 curp = save8bit(&hh->msgid, curp);
262 curp = save8bit(&hh->msgspecific, curp);
263
264 padded = hh->msgid.byte & 0x80;
265 hh->msgid.byte &= 0x7F;
266
267 hh->extmsgid.is_present = (hh->msgid.byte == SIERRA_NET_HIP_EXTENDEDID);
268 if (hh->extmsgid.is_present) {
269 if (buflen < SIERRA_NET_HIP_EXT_HDR_LEN)
270 return -EPROTO;
271
272 hh->payload_len.word &= 0x3FFF;
273
274 curp = save16bit(&hh->extmsgid, curp);
275 hh->extmsgid.word &= 0x03FF;
276
277 hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
278 } else {
279 hh->payload_len.word &= 0x07FF;
280 hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
281 }
282
283 if (padded) {
284 hh->hdrlen++;
285 hh->payload_len.word--;
286 }
287
288
289 if (buflen < (hh->hdrlen + hh->payload_len.word))
290 return -EINVAL;
291
292 return 0;
293}
294
295static void build_hip(u8 *buf, const u16 payloadlen,
296 struct sierra_net_data *priv)
297{
298
299
300
301 put_unaligned_be16(payloadlen, buf);
302 memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
303}
304
305
306
307
308static int sierra_net_send_cmd(struct usbnet *dev,
309 u8 *cmd, int cmdlen, const char * cmd_name)
310{
311 struct sierra_net_data *priv = sierra_net_get_private(dev);
312 int status;
313
314 status = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
315 USB_CDC_SEND_ENCAPSULATED_COMMAND,
316 USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE, 0,
317 priv->ifnum, cmd, cmdlen, USB_CTRL_SET_TIMEOUT);
318
319 if (status != cmdlen && status != -ENODEV)
320 netdev_err(dev->net, "Submit %s failed %d\n", cmd_name, status);
321
322 return status;
323}
324
325static int sierra_net_send_sync(struct usbnet *dev)
326{
327 int status;
328 struct sierra_net_data *priv = sierra_net_get_private(dev);
329
330 dev_dbg(&dev->udev->dev, "%s", __func__);
331
332 status = sierra_net_send_cmd(dev, priv->sync_msg,
333 sizeof(priv->sync_msg), "SYNC");
334
335 return status;
336}
337
338static void sierra_net_set_ctx_index(struct sierra_net_data *priv, u8 ctx_ix)
339{
340 dev_dbg(&(priv->usbnet->udev->dev), "%s %d", __func__, ctx_ix);
341 priv->tx_hdr_template[0] = 0x3F;
342 priv->tx_hdr_template[1] = ctx_ix;
343 *((u16 *)&priv->tx_hdr_template[2]) =
344 cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID);
345}
346
347static inline int sierra_net_is_valid_addrlen(u8 len)
348{
349 return len == sizeof(struct in_addr);
350}
351
352static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen)
353{
354 struct lsi_umts *lsi = (struct lsi_umts *)data;
355
356 if (datalen < sizeof(struct lsi_umts)) {
357 netdev_err(dev->net, "%s: Data length %d, exp %Zu\n",
358 __func__, datalen,
359 sizeof(struct lsi_umts));
360 return -1;
361 }
362
363 if (lsi->length != cpu_to_be16(SIERRA_NET_LSI_UMTS_STATUS_LEN)) {
364 netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
365 __func__, be16_to_cpu(lsi->length),
366 (u32)SIERRA_NET_LSI_UMTS_STATUS_LEN);
367 return -1;
368 }
369
370
371 if (lsi->protocol != SIERRA_NET_PROTOCOL_UMTS) {
372 netdev_err(dev->net, "Protocol unsupported, 0x%02x\n",
373 lsi->protocol);
374 return -1;
375 }
376
377
378 if (lsi->link_type != SIERRA_NET_AS_LINK_TYPE_IPv4) {
379 netdev_err(dev->net, "Link type unsupported: 0x%02x\n",
380 lsi->link_type);
381 return -1;
382 }
383
384
385 if (lsi->coverage == SIERRA_NET_COVERAGE_NONE
386 || lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) {
387 netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage);
388 return 0;
389 }
390
391
392 if (lsi->session_state == SIERRA_NET_SESSION_IDLE) {
393 netdev_err(dev->net, "Session idle, 0x%02x\n",
394 lsi->session_state);
395 return 0;
396 }
397
398
399 return 1;
400}
401
402static void sierra_net_handle_lsi(struct usbnet *dev, char *data,
403 struct hip_hdr *hh)
404{
405 struct sierra_net_data *priv = sierra_net_get_private(dev);
406 int link_up;
407
408 link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen,
409 hh->payload_len.word);
410 if (link_up < 0) {
411 netdev_err(dev->net, "Invalid LSI\n");
412 return;
413 }
414 if (link_up) {
415 sierra_net_set_ctx_index(priv, hh->msgspecific.byte);
416 priv->link_up = 1;
417 netif_carrier_on(dev->net);
418 } else {
419 priv->link_up = 0;
420 netif_carrier_off(dev->net);
421 }
422}
423
424static void sierra_net_dosync(struct usbnet *dev)
425{
426 int status;
427 struct sierra_net_data *priv = sierra_net_get_private(dev);
428
429 dev_dbg(&dev->udev->dev, "%s", __func__);
430
431
432 status = sierra_net_send_sync(dev);
433 if (status < 0)
434 netdev_err(dev->net,
435 "Send SYNC failed, status %d\n", status);
436 status = sierra_net_send_sync(dev);
437 if (status < 0)
438 netdev_err(dev->net,
439 "Send SYNC failed, status %d\n", status);
440
441
442 priv->sync_timer.function = sierra_sync_timer;
443 priv->sync_timer.data = (unsigned long) dev;
444 priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
445 add_timer(&priv->sync_timer);
446}
447
448static void sierra_net_kevent(struct work_struct *work)
449{
450 struct sierra_net_data *priv =
451 container_of(work, struct sierra_net_data, sierra_net_kevent);
452 struct usbnet *dev = priv->usbnet;
453 int len;
454 int err;
455 u8 *buf;
456 u8 ifnum;
457
458 if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
459 clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
460
461
462 buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
463 if (!buf) {
464 netdev_err(dev->net,
465 "failed to allocate buf for LS msg\n");
466 return;
467 }
468 ifnum = priv->ifnum;
469 len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
470 USB_CDC_GET_ENCAPSULATED_RESPONSE,
471 USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
472 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
473 USB_CTRL_SET_TIMEOUT);
474
475 if (len < 0) {
476 netdev_err(dev->net,
477 "usb_control_msg failed, status %d\n", len);
478 } else {
479 struct hip_hdr hh;
480
481 dev_dbg(&dev->udev->dev, "%s: Received status message,"
482 " %04x bytes", __func__, len);
483
484 err = parse_hip(buf, len, &hh);
485 if (err) {
486 netdev_err(dev->net, "%s: Bad packet,"
487 " parse result %d\n", __func__, err);
488 kfree(buf);
489 return;
490 }
491
492
493 if (len != hh.hdrlen + hh.payload_len.word) {
494 netdev_err(dev->net, "%s: Bad packet, received"
495 " %d, expected %d\n", __func__, len,
496 hh.hdrlen + hh.payload_len.word);
497 kfree(buf);
498 return;
499 }
500
501
502 switch (hh.msgid.byte) {
503 case SIERRA_NET_HIP_LSI_UMTSID:
504 dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
505 hh.msgspecific.byte);
506 sierra_net_handle_lsi(dev, buf, &hh);
507 break;
508 case SIERRA_NET_HIP_RESTART_ID:
509 dev_dbg(&dev->udev->dev, "Restart reported: %d,"
510 " stopping sync timer",
511 hh.msgspecific.byte);
512
513 del_timer_sync(&priv->sync_timer);
514 clear_bit(SIERRA_NET_TIMER_EXPIRY,
515 &priv->kevent_flags);
516 break;
517 case SIERRA_NET_HIP_HSYNC_ID:
518 dev_dbg(&dev->udev->dev, "SYNC received");
519 err = sierra_net_send_sync(dev);
520 if (err < 0)
521 netdev_err(dev->net,
522 "Send SYNC failed %d\n", err);
523 break;
524 case SIERRA_NET_HIP_EXTENDEDID:
525 netdev_err(dev->net, "Unrecognized HIP msg, "
526 "extmsgid 0x%04x\n", hh.extmsgid.word);
527 break;
528 case SIERRA_NET_HIP_RCGI:
529
530 break;
531 default:
532 netdev_err(dev->net, "Unrecognized HIP msg, "
533 "msgid 0x%02x\n", hh.msgid.byte);
534 break;
535 }
536 }
537 kfree(buf);
538 }
539
540 if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
541 clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
542 dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
543 sierra_net_dosync(priv->usbnet);
544 }
545
546 if (priv->kevent_flags)
547 dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
548 "kevent_flags = 0x%lx", priv->kevent_flags);
549}
550
551static void sierra_net_defer_kevent(struct usbnet *dev, int work)
552{
553 struct sierra_net_data *priv = sierra_net_get_private(dev);
554
555 set_bit(work, &priv->kevent_flags);
556 schedule_work(&priv->sierra_net_kevent);
557}
558
559
560
561
562static void sierra_sync_timer(unsigned long syncdata)
563{
564 struct usbnet *dev = (struct usbnet *)syncdata;
565
566 dev_dbg(&dev->udev->dev, "%s", __func__);
567
568 sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
569}
570
571static void sierra_net_status(struct usbnet *dev, struct urb *urb)
572{
573 struct usb_cdc_notification *event;
574
575 dev_dbg(&dev->udev->dev, "%s", __func__);
576
577 if (urb->actual_length < sizeof *event)
578 return;
579
580
581 event = urb->transfer_buffer;
582 switch (event->bNotificationType) {
583 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
584 case USB_CDC_NOTIFY_SPEED_CHANGE:
585
586 break;
587 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
588 sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
589 break;
590 default:
591 netdev_err(dev->net, ": unexpected notification %02x!\n",
592 event->bNotificationType);
593 break;
594 }
595}
596
597static void sierra_net_get_drvinfo(struct net_device *net,
598 struct ethtool_drvinfo *info)
599{
600
601 usbnet_get_drvinfo(net, info);
602 strncpy(info->driver, driver_name, sizeof info->driver);
603 strncpy(info->version, DRIVER_VERSION, sizeof info->version);
604}
605
606static u32 sierra_net_get_link(struct net_device *net)
607{
608 struct usbnet *dev = netdev_priv(net);
609
610 return sierra_net_get_private(dev)->link_up && netif_running(net);
611}
612
613static const struct ethtool_ops sierra_net_ethtool_ops = {
614 .get_drvinfo = sierra_net_get_drvinfo,
615 .get_link = sierra_net_get_link,
616 .get_msglevel = usbnet_get_msglevel,
617 .set_msglevel = usbnet_set_msglevel,
618 .get_settings = usbnet_get_settings,
619 .set_settings = usbnet_set_settings,
620 .nway_reset = usbnet_nway_reset,
621};
622
623
624static int sierra_net_change_mtu(struct net_device *net, int new_mtu)
625{
626 if (new_mtu > SIERRA_NET_MAX_SUPPORTED_MTU)
627 return -EINVAL;
628
629 return usbnet_change_mtu(net, new_mtu);
630}
631
632static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
633{
634 int result = 0;
635 u16 *attrdata;
636
637 attrdata = kmalloc(sizeof(*attrdata), GFP_KERNEL);
638 if (!attrdata)
639 return -ENOMEM;
640
641 result = usb_control_msg(
642 dev->udev,
643 usb_rcvctrlpipe(dev->udev, 0),
644
645 SWI_USB_REQUEST_GET_FW_ATTR,
646 USB_DIR_IN | USB_TYPE_VENDOR,
647 0x0000,
648 0x0000,
649 attrdata,
650 sizeof(*attrdata),
651 USB_CTRL_SET_TIMEOUT);
652
653 if (result < 0) {
654 kfree(attrdata);
655 return -EIO;
656 }
657
658 *datap = le16_to_cpu(*attrdata);
659
660 kfree(attrdata);
661 return result;
662}
663
664
665
666
667static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
668{
669 u8 ifacenum;
670 u8 numendpoints;
671 u16 fwattr = 0;
672 int status;
673 struct ethhdr *eth;
674 struct sierra_net_data *priv;
675 static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
676 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
677 static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
678 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
679
680 dev_dbg(&dev->udev->dev, "%s", __func__);
681
682 ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
683 numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
684
685 if (numendpoints != 3) {
686 dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
687 numendpoints);
688 return -ENODEV;
689 }
690
691 dev->status = NULL;
692 status = usbnet_get_endpoints(dev, intf);
693 if (status < 0) {
694 dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
695 status);
696 return -ENODEV;
697 }
698
699 priv = kzalloc(sizeof *priv, GFP_KERNEL);
700 if (!priv) {
701 dev_err(&dev->udev->dev, "No memory");
702 return -ENOMEM;
703 }
704
705 priv->usbnet = dev;
706 priv->ifnum = ifacenum;
707 dev->net->netdev_ops = &sierra_net_device_ops;
708
709
710 dev->net->dev_addr[ETH_ALEN-2] = atomic_inc_return(&iface_counter);
711 dev->net->dev_addr[ETH_ALEN-1] = ifacenum;
712
713
714 eth = (struct ethhdr *)priv->ethr_hdr_tmpl;
715 memcpy(ð->h_dest, dev->net->dev_addr, ETH_ALEN);
716 eth->h_proto = cpu_to_be16(ETH_P_IP);
717
718
719 memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
720
721 sierra_net_set_ctx_index(priv, 0);
722
723
724 dev->rx_urb_size = SIERRA_NET_RX_URB_SIZE;
725 if (dev->udev->speed != USB_SPEED_HIGH)
726 dev->rx_urb_size = min_t(size_t, 4096, SIERRA_NET_RX_URB_SIZE);
727
728 dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
729 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
730
731
732 dev->net->flags |= IFF_NOARP;
733 dev->net->ethtool_ops = &sierra_net_ethtool_ops;
734 netif_carrier_off(dev->net);
735
736 sierra_net_set_private(dev, priv);
737
738 priv->kevent_flags = 0;
739
740
741 INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
742
743
744 init_timer(&priv->sync_timer);
745
746
747 status = sierra_net_get_fw_attr(dev, &fwattr);
748 dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
749
750
751 if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
752
753 dev_err(&dev->udev->dev, "Incompatible driver and firmware"
754 " versions\n");
755 kfree(priv);
756 return -ENODEV;
757 }
758
759 memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
760
761
762 sierra_net_dosync(dev);
763
764 return 0;
765}
766
767static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
768{
769 int status;
770 struct sierra_net_data *priv = sierra_net_get_private(dev);
771
772 dev_dbg(&dev->udev->dev, "%s", __func__);
773
774
775 del_timer_sync(&priv->sync_timer);
776 cancel_work_sync(&priv->sierra_net_kevent);
777
778
779 status = sierra_net_send_cmd(dev, priv->shdwn_msg,
780 sizeof(priv->shdwn_msg), "Shutdown");
781 if (status < 0)
782 netdev_err(dev->net,
783 "usb_control_msg failed, status %d\n", status);
784
785 sierra_net_set_private(dev, NULL);
786
787 kfree(priv);
788}
789
790static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
791 struct sk_buff *skb, int len)
792{
793 struct sk_buff *new_skb;
794
795
796 new_skb = skb_clone(skb, GFP_ATOMIC);
797
798
799 skb_pull(skb, len);
800
801
802 if (new_skb) {
803 skb_trim(new_skb, len);
804 } else {
805 if (netif_msg_rx_err(dev))
806 netdev_err(dev->net, "failed to get skb\n");
807 dev->net->stats.rx_dropped++;
808 }
809
810 return new_skb;
811}
812
813
814static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
815{
816 int err;
817 struct hip_hdr hh;
818 struct sk_buff *new_skb;
819
820 dev_dbg(&dev->udev->dev, "%s", __func__);
821
822
823 while (likely(skb->len)) {
824 err = parse_hip(skb->data, skb->len, &hh);
825 if (err) {
826 if (netif_msg_rx_err(dev))
827 netdev_err(dev->net, "Invalid HIP header %d\n",
828 err);
829
830 dev->net->stats.rx_length_errors++;
831 return 0;
832 }
833
834
835 if (!hh.extmsgid.is_present
836 || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
837 if (netif_msg_rx_err(dev))
838 netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
839
840 dev->net->stats.rx_frame_errors++;
841
842 return 0;
843 }
844
845 skb_pull(skb, hh.hdrlen);
846
847
848 memcpy(skb->data, sierra_net_get_private(dev)->ethr_hdr_tmpl,
849 ETH_HLEN);
850
851
852 if (hh.payload_len.word == skb->len)
853 return 1;
854
855 new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
856 if (new_skb)
857 usbnet_skb_return(dev, new_skb);
858
859 }
860
861 return 0;
862}
863
864
865static struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev,
866 struct sk_buff *skb, gfp_t flags)
867{
868 struct sierra_net_data *priv = sierra_net_get_private(dev);
869 u16 len;
870 bool need_tail;
871
872 BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet, data)
873 < sizeof(struct cdc_state));
874
875 dev_dbg(&dev->udev->dev, "%s", __func__);
876 if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
877
878 if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
879
880 len = skb->len;
881 skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
882
883 need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
884 % dev->maxpacket == 0);
885 if (need_tail) {
886 if (unlikely(skb_tailroom(skb) == 0)) {
887 netdev_err(dev->net, "tx_fixup:"
888 "no room for packet\n");
889 dev_kfree_skb_any(skb);
890 return NULL;
891 } else {
892 skb->data[skb->len] = 0;
893 __skb_put(skb, 1);
894 len = len + 1;
895 }
896 }
897 build_hip(skb->data, len, priv);
898 return skb;
899 } else {
900
901
902
903 netdev_err(dev->net, "tx_fixup: no room for HIP\n");
904 }
905 }
906
907 if (!priv->link_up)
908 dev->net->stats.tx_carrier_errors++;
909
910
911
912
913 dev_kfree_skb_any(skb);
914 return NULL;
915}
916
917static const struct driver_info sierra_net_info_direct_ip = {
918 .description = "Sierra Wireless USB-to-WWAN Modem",
919 .flags = FLAG_WWAN | FLAG_SEND_ZLP,
920 .bind = sierra_net_bind,
921 .unbind = sierra_net_unbind,
922 .status = sierra_net_status,
923 .rx_fixup = sierra_net_rx_fixup,
924 .tx_fixup = sierra_net_tx_fixup,
925};
926
927#define DIRECT_IP_DEVICE(vend, prod) \
928 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 7), \
929 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
930 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 10), \
931 .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
932 {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 11), \
933 .driver_info = (unsigned long)&sierra_net_info_direct_ip}
934
935static const struct usb_device_id products[] = {
936 DIRECT_IP_DEVICE(0x1199, 0x68A3),
937 DIRECT_IP_DEVICE(0x0F3D, 0x68A3),
938 DIRECT_IP_DEVICE(0x1199, 0x68AA),
939 DIRECT_IP_DEVICE(0x0F3D, 0x68AA),
940
941 {},
942};
943MODULE_DEVICE_TABLE(usb, products);
944
945
946static struct usb_driver sierra_net_driver = {
947 .name = "sierra_net",
948 .id_table = products,
949 .probe = usbnet_probe,
950 .disconnect = usbnet_disconnect,
951 .suspend = usbnet_suspend,
952 .resume = usbnet_resume,
953 .no_dynamic_id = 1,
954 .disable_hub_initiated_lpm = 1,
955};
956
957module_usb_driver(sierra_net_driver);
958
959MODULE_AUTHOR(DRIVER_AUTHOR);
960MODULE_DESCRIPTION(DRIVER_DESC);
961MODULE_VERSION(DRIVER_VERSION);
962MODULE_LICENSE("GPL");
963