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