[scripts] env: colorize git output
[openwrt.git] / package / sierra-directip / src / sierra_net.c
1 /*
2 * USB-to-WWAN Driver for Sierra Wireless modems
3 *
4 * Copyright (C) 2008 - 2011 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, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28 #define DRIVER_VERSION "v.3.2"
29 #define DRIVER_AUTHOR "Paxton Smith, Matthew Safar, Rory Filer"
30 #define DRIVER_DESC "USB-to-WWAN Driver for Sierra Wireless modems"
31 static const char driver_name[] = "sierra_net";
32
33 /* if defined debug messages enabled */
34 /*#define DEBUG*/
35 /* more debug messages */
36 /*#define VERBOSE*/
37 /* Uncomment to force power level set to auto when attaching a device */
38 /*#define POWER_LEVEL_AUTO*/
39
40 #include <linux/module.h>
41 #include <linux/etherdevice.h>
42 #include <linux/ethtool.h>
43 #include <linux/mii.h>
44 #include <linux/sched.h>
45 #include <linux/timer.h>
46 #include <linux/usb.h>
47 #include <linux/usb/cdc.h>
48 #include <net/ip.h>
49 #include <net/udp.h>
50 #include <asm/unaligned.h>
51 #include <linux/usb/usbnet.h>
52
53 #define SWI_USB_REQUEST_GET_FW_ATTR 0x06
54 #define SWI_GET_FW_ATTR_MASK 0x08
55 #define SWI_GET_FW_ATTR_APM 0x2
56
57 /* atomic counter partially included in MAC address to make sure 2 devices
58 * do not end up with the same MAC - concept breaks in case of > 255 ifaces
59 */
60 static atomic_t iface_counter = ATOMIC_INIT(0);
61
62 /*
63 * SYNC Timer Delay definition used to set the expiry time
64 */
65 #define SIERRA_NET_SYNCDELAY (2*HZ)
66
67 /* Max. MTU supported. The modem buffers are limited to 1500 */
68 #define SIERRA_NET_MAX_SUPPORTED_MTU 1500
69
70 /* The SIERRA_NET_USBCTL_BUF_LEN defines a buffer size allocated for control
71 * message reception ... and thus the max. received packet.
72 * (May be the cause for parse_hip returning -EINVAL)
73 */
74 #define SIERRA_NET_USBCTL_BUF_LEN 1024
75
76 /* The SIERRA_NET_RX_URB_SZ defines the receive urb size
77 * for optimal throughput.
78 */
79 #define SIERRA_NET_RX_URB_SZ 1540
80
81 /* list of interface numbers - used for constructing interface lists */
82 struct sierra_net_iface_info {
83 const u32 infolen; /* number of interface numbers on list */
84 const u8 *ifaceinfo; /* pointer to the array holding the numbers */
85 };
86
87 struct sierra_net_info_data {
88 u16 rx_urb_size;
89 struct sierra_net_iface_info whitelist;
90 };
91
92 /* Private data structure */
93 struct sierra_net_data {
94
95 u8 ethr_hdr_tmpl[ETH_HLEN]; /* ethernet header template for rx'd pkts */
96
97 u16 link_up; /* air link up or down */
98 u8 tx_hdr_template[4]; /* part of HIP hdr for tx'd packets */
99
100 u8 sync_msg[4]; /* SYNC message */
101 u8 shdwn_msg[4]; /* Shutdown message */
102
103 /* Backpointer to the container */
104 struct usbnet *usbnet;
105
106 u8 ifnum; /* interface number */
107
108 /* Bit masks, must be a power of 2 */
109 #define SIERRA_NET_EVENT_RESP_AVAIL 0x01
110 #define SIERRA_NET_TIMER_EXPIRY 0x02
111 unsigned long kevent_flags;
112 struct work_struct sierra_net_kevent;
113 struct timer_list sync_timer; /* For retrying SYNC sequence */
114 };
115
116 struct param {
117 int is_present;
118 union {
119 void *ptr;
120 u32 dword;
121 u16 word;
122 u8 byte;
123 };
124 };
125
126 /* HIP message type */
127 #define SIERRA_NET_HIP_EXTENDEDID 0x7F
128 #define SIERRA_NET_HIP_HSYNC_ID 0x60 /* Modem -> host */
129 #define SIERRA_NET_HIP_RESTART_ID 0x62 /* Modem -> host */
130 #define SIERRA_NET_HIP_MSYNC_ID 0x20 /* Host -> modem */
131 #define SIERRA_NET_HIP_SHUTD_ID 0x26 /* Host -> modem */
132
133 #define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
134 #define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
135
136 /* 3G UMTS Link Sense Indication definitions */
137 #define SIERRA_NET_HIP_LSI_UMTSID 0x78
138
139 /* Reverse Channel Grant Indication HIP message */
140 #define SIERRA_NET_HIP_RCGI 0x64
141
142 /* LSI Protocol types */
143 #define SIERRA_NET_PROTOCOL_UMTS 0x01
144 /* LSI Coverage */
145 #define SIERRA_NET_COVERAGE_NONE 0x00
146 #define SIERRA_NET_COVERAGE_NOPACKET 0x01
147
148 /* LSI Session */
149 #define SIERRA_NET_SESSION_IDLE 0x00
150 /* LSI Link types */
151 #define SIERRA_NET_AS_LINK_TYPE_IPv4 0x00
152 #define SIERRA_NET_AS_LINK_TYPE_IPv6 0x02
153
154 struct lsi_umts {
155 u8 protocol;
156 u8 unused1;
157 __be16 length;
158 /* eventually use a union for the rest - assume umts for now */
159 u8 coverage;
160 u8 unused2[41];
161 u8 session_state;
162 u8 unused3[33];
163 u8 link_type;
164 u8 pdp_addr_len; /* NW-supplied PDP address len */
165 u8 pdp_addr[16]; /* NW-supplied PDP address (bigendian)) */
166 u8 unused4[23];
167 u8 dns1_addr_len; /* NW-supplied 1st DNS address len (bigendian) */
168 u8 dns1_addr[16]; /* NW-supplied 1st DNS address */
169 u8 dns2_addr_len; /* NW-supplied 2nd DNS address len */
170 u8 dns2_addr[16]; /* NW-supplied 2nd DNS address (bigendian)*/
171 u8 wins1_addr_len; /* NW-supplied 1st Wins address len */
172 u8 wins1_addr[16]; /* NW-supplied 1st Wins address (bigendian)*/
173 u8 wins2_addr_len; /* NW-supplied 2nd Wins address len */
174 u8 wins2_addr[16]; /* NW-supplied 2nd Wins address (bigendian) */
175 u8 unused5[4];
176 u8 gw_addr_len; /* NW-supplied GW address len */
177 u8 gw_addr[16]; /* NW-supplied GW address (bigendian) */
178 u8 reserved[8];
179 } __packed;
180
181 #define SIERRA_NET_LSI_COMMON_LEN 4
182 #define SIERRA_NET_LSI_UMTS_LEN (sizeof(struct lsi_umts))
183 #define SIERRA_NET_LSI_UMTS_STATUS_LEN \
184 (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN)
185
186 /* Forward definitions */
187 static void sierra_sync_timer(unsigned long syncdata);
188 static int sierra_net_change_mtu(struct net_device *net, int new_mtu);
189
190 /* Our own net device operations structure */
191 static const struct net_device_ops sierra_net_device_ops = {
192 .ndo_open = usbnet_open,
193 .ndo_stop = usbnet_stop,
194 .ndo_start_xmit = usbnet_start_xmit,
195 .ndo_tx_timeout = usbnet_tx_timeout,
196 .ndo_change_mtu = sierra_net_change_mtu,
197 .ndo_set_mac_address = eth_mac_addr,
198 .ndo_validate_addr = eth_validate_addr,
199 };
200
201 /* get private data associated with passed in usbnet device */
202 static inline struct sierra_net_data *sierra_net_get_private(struct usbnet *dev)
203 {
204 return (struct sierra_net_data *)dev->data[0];
205 }
206
207 /* set private data associated with passed in usbnet device */
208 static inline void sierra_net_set_private(struct usbnet *dev,
209 struct sierra_net_data *priv)
210 {
211 dev->data[0] = (unsigned long)priv;
212 }
213
214 /* is packet IP */
215 static inline int is_ip(struct sk_buff *skb)
216 {
217 return ((skb->protocol == cpu_to_be16(ETH_P_IP)) ||
218 (skb->protocol == cpu_to_be16(ETH_P_IPV6)));
219 }
220
221 /*
222 * check passed in packet and make sure that:
223 * - it is linear (no scatter/gather)
224 * - it is ethernet (mac_header properly set)
225 */
226 static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
227 {
228 skb_reset_mac_header(skb); /* ethernet header */
229
230 if (skb_is_nonlinear(skb)) {
231 netdev_err(dev->net, "Non linear buffer-dropping\n");
232 return 0;
233 }
234
235 if (!pskb_may_pull(skb, ETH_HLEN))
236 return 0;
237 skb->protocol = eth_hdr(skb)->h_proto;
238
239 return 1;
240 }
241
242 static const u8 *save16bit(struct param *p, const u8 *datap)
243 {
244 p->is_present = 1;
245 p->word = get_unaligned_be16(datap);
246 return datap + sizeof(p->word);
247 }
248
249 static const u8 *save8bit(struct param *p, const u8 *datap)
250 {
251 p->is_present = 1;
252 p->byte = *datap;
253 return datap + sizeof(p->byte);
254 }
255
256 /*----------------------------------------------------------------------------*
257 * BEGIN HIP *
258 *----------------------------------------------------------------------------*/
259 /* HIP header */
260 #define SIERRA_NET_HIP_HDR_LEN 4
261 /* Extended HIP header */
262 #define SIERRA_NET_HIP_EXT_HDR_LEN 6
263
264 struct hip_hdr {
265 int hdrlen;
266 struct param payload_len;
267 struct param msgid;
268 struct param msgspecific;
269 struct param extmsgid;
270 };
271
272 static int parse_hip(const u8 *buf, const u32 buflen, struct hip_hdr *hh)
273 {
274 const u8 *curp = buf;
275 int padded;
276
277 if (buflen < SIERRA_NET_HIP_HDR_LEN)
278 return -EPROTO;
279
280 curp = save16bit(&hh->payload_len, curp);
281 curp = save8bit(&hh->msgid, curp);
282 curp = save8bit(&hh->msgspecific, curp);
283
284 padded = hh->msgid.byte & 0x80;
285 hh->msgid.byte &= 0x7F; /* 7 bits */
286
287 hh->extmsgid.is_present = (hh->msgid.byte == SIERRA_NET_HIP_EXTENDEDID);
288 if (hh->extmsgid.is_present) {
289 if (buflen < SIERRA_NET_HIP_EXT_HDR_LEN)
290 return -EPROTO;
291
292 hh->payload_len.word &= 0x3FFF; /* 14 bits */
293
294 curp = save16bit(&hh->extmsgid, curp);
295 hh->extmsgid.word &= 0x03FF; /* 10 bits */
296
297 hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
298 } else {
299 hh->payload_len.word &= 0x07FF; /* 11 bits */
300 hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
301 }
302
303 if (padded) {
304 hh->hdrlen++;
305 hh->payload_len.word--;
306 }
307
308 /* if real packet shorter than the claimed length */
309 if (buflen < (hh->hdrlen + hh->payload_len.word))
310 return -EINVAL;
311
312 return 0;
313 }
314
315 static void build_hip(u8 *buf, const u16 payloadlen,
316 struct sierra_net_data *priv)
317 {
318 /* the following doesn't have the full functionality. We
319 * currently build only one kind of header, so it is faster this way
320 */
321 put_unaligned_be16(payloadlen, buf);
322 memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
323 }
324 /*----------------------------------------------------------------------------*
325 * END HIP *
326 *----------------------------------------------------------------------------*/
327 /* This should come out before going to kernel.org */
328 static void sierra_net_printk_buf(u8 *buf, u16 len)
329 {
330 #ifdef VERBOSE
331 u16 i;
332 u16 k;
333
334 for (i = k = 0; i < len / 4; i++, k += 4) {
335 printk(KERN_DEBUG "%02x%02x%02x%02x ",
336 buf[k+0], buf[k+1], buf[k+2], buf[k+3]);
337 }
338
339 for (; k < len; k++)
340 printk(KERN_DEBUG "%02x", buf[k]);
341
342 printk("\n");
343 #endif
344 }
345
346 static int sierra_net_send_cmd(struct usbnet *dev,
347 u8 *cmd, int cmdlen, const char * cmd_name)
348 {
349 struct sierra_net_data *priv = sierra_net_get_private(dev);
350 int status;
351
352 usb_autopm_get_interface(dev->intf);
353 status = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
354 USB_CDC_SEND_ENCAPSULATED_COMMAND,
355 USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE, 0,
356 priv->ifnum, cmd, cmdlen, USB_CTRL_SET_TIMEOUT);
357 usb_autopm_put_interface(dev->intf);
358
359 if (status != cmdlen && status != -ENODEV)
360 netdev_err(dev->net, "Submit %s failed %d\n", cmd_name, status);
361
362 return status;
363 }
364
365 static int sierra_net_send_sync(struct usbnet *dev)
366 {
367 int status;
368 struct sierra_net_data *priv = sierra_net_get_private(dev);
369
370 dev_dbg(&dev->udev->dev, "%s", __func__);
371
372 status = sierra_net_send_cmd(dev, priv->sync_msg,
373 sizeof(priv->sync_msg), "SYNC");
374 return status;
375 }
376
377 static void sierra_net_send_shutdown(struct usbnet *dev)
378 {
379 struct sierra_net_data *priv = sierra_net_get_private(dev);
380
381 dev_dbg(&dev->udev->dev, "%s", __func__);
382
383 sierra_net_send_cmd(dev, priv->shdwn_msg,
384 sizeof(priv->shdwn_msg), "Shutdown");
385 }
386
387 static void sierra_net_set_ctx_index(struct sierra_net_data *priv, u8 ctx_ix)
388 {
389 dev_dbg(&(priv->usbnet->udev->dev), "%s %d", __func__, ctx_ix);
390 priv->tx_hdr_template[0] = 0x3F;
391 priv->tx_hdr_template[1] = ctx_ix;
392 *((u16 *)&priv->tx_hdr_template[2]) =
393 cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID);
394 }
395
396 static inline int sierra_net_is_valid_addrlen(u8 len)
397 {
398 return (len == sizeof(struct in_addr));
399 }
400
401 static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen)
402 {
403 struct lsi_umts *lsi = (struct lsi_umts *)data;
404
405 if (datalen < sizeof(struct lsi_umts)) {
406 netdev_err(dev->net, "%s: Data length %d, exp %Zu\n",
407 __func__, datalen,
408 sizeof(struct lsi_umts));
409 return -1;
410 }
411
412 if (lsi->length != cpu_to_be16(SIERRA_NET_LSI_UMTS_STATUS_LEN)) {
413 netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
414 __func__, be16_to_cpu(lsi->length),
415 (u32)SIERRA_NET_LSI_UMTS_STATUS_LEN);
416 return -1;
417 }
418
419 /* Validate the protocol - only support UMTS for now */
420 if (lsi->protocol != SIERRA_NET_PROTOCOL_UMTS) {
421 netdev_err(dev->net, "Protocol unsupported, 0x%02x\n",
422 lsi->protocol);
423 return -1;
424 }
425
426 /* Validate the link type */
427 if ((lsi->link_type != SIERRA_NET_AS_LINK_TYPE_IPv4) &&
428 (lsi->link_type != SIERRA_NET_AS_LINK_TYPE_IPv6)) {
429 netdev_err(dev->net, "Link unavailable: 0x%02x",
430 lsi->link_type);
431 return 0;
432 }
433
434 /* Validate the coverage */
435 if (lsi->coverage == SIERRA_NET_COVERAGE_NONE
436 || lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) {
437 netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage);
438 return 0;
439 }
440
441 /* Validate the session state */
442 if (lsi->session_state == SIERRA_NET_SESSION_IDLE) {
443 netdev_err(dev->net, "Session idle, 0x%02x\n",
444 lsi->session_state);
445 return 0;
446 }
447
448 /* Set link_sense true */
449 return 1;
450 }
451
452 static void sierra_net_handle_lsi(struct usbnet *dev, char *data,
453 struct hip_hdr *hh)
454 {
455 struct sierra_net_data *priv = sierra_net_get_private(dev);
456 int link_up;
457
458 link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen,
459 hh->payload_len.word);
460 if (link_up < 0) {
461 netdev_err(dev->net, "Invalid LSI\n");
462 return;
463 }
464 if (link_up) {
465 sierra_net_set_ctx_index(priv, hh->msgspecific.byte);
466 priv->link_up = 1;
467 netif_carrier_on(dev->net);
468 } else {
469 priv->link_up = 0;
470 netif_carrier_off(dev->net);
471 }
472 }
473
474 static void sierra_net_dosync(struct usbnet *dev)
475 {
476 int status;
477 struct sierra_net_data *priv = sierra_net_get_private(dev);
478
479 dev_dbg(&dev->udev->dev, "%s", __func__);
480
481 /* tell modem we are ready */
482 status = sierra_net_send_sync(dev);
483 if (status < 0)
484 netdev_err(dev->net,
485 "Send SYNC failed, status %d\n", status);
486 status = sierra_net_send_sync(dev);
487 if (status < 0)
488 netdev_err(dev->net,
489 "Send SYNC failed, status %d\n", status);
490
491 /* Now, start a timer and make sure we get the Restart Indication */
492 priv->sync_timer.function = sierra_sync_timer;
493 priv->sync_timer.data = (unsigned long) dev;
494 priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
495 add_timer(&priv->sync_timer);
496 }
497
498 static void sierra_net_kevent(struct work_struct *work)
499 {
500 struct sierra_net_data *priv =
501 container_of(work, struct sierra_net_data, sierra_net_kevent);
502 struct usbnet *dev = priv->usbnet;
503 int len;
504 int err;
505 u8 *buf;
506 u8 ifnum;
507
508 if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
509 clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
510
511 /* Query the modem for the LSI message */
512 buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
513 if (!buf) {
514 netdev_err(dev->net,
515 "failed to allocate buf for LS msg\n");
516 return;
517 }
518 ifnum = priv->ifnum;
519 usb_autopm_get_interface(dev->intf);
520 len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
521 USB_CDC_GET_ENCAPSULATED_RESPONSE,
522 USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
523 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
524 USB_CTRL_SET_TIMEOUT);
525 usb_autopm_put_interface(dev->intf);
526
527 if (unlikely(len < 0)) {
528 netdev_err(dev->net,
529 "usb_control_msg failed, status %d\n", len);
530 } else {
531 struct hip_hdr hh;
532
533 dev_dbg(&dev->udev->dev, "%s: Received status message,"
534 " %04x bytes", __func__, len);
535 sierra_net_printk_buf(buf, len);
536
537 err = parse_hip(buf, len, &hh);
538 if (err) {
539 netdev_err(dev->net, "%s: Bad packet,"
540 " parse result %d\n", __func__, err);
541 kfree(buf);
542 return;
543 }
544
545 /* Validate packet length */
546 if (len != hh.hdrlen + hh.payload_len.word) {
547 netdev_err(dev->net, "%s: Bad packet, received"
548 " %d, expected %d\n", __func__, len,
549 hh.hdrlen + hh.payload_len.word);
550 kfree(buf);
551 return;
552 }
553
554 /* Switch on received message types */
555 switch (hh.msgid.byte) {
556 case SIERRA_NET_HIP_LSI_UMTSID:
557 dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
558 hh.msgspecific.byte);
559 sierra_net_handle_lsi(dev, buf, &hh);
560 break;
561 case SIERRA_NET_HIP_RESTART_ID:
562 dev_dbg(&dev->udev->dev, "Restart reported: %d,"
563 " stopping sync timer",
564 hh.msgspecific.byte);
565 /* Got sync resp - stop timer & clear mask */
566 del_timer_sync(&priv->sync_timer);
567 clear_bit(SIERRA_NET_TIMER_EXPIRY,
568 &priv->kevent_flags);
569 break;
570 case SIERRA_NET_HIP_HSYNC_ID:
571 dev_dbg(&dev->udev->dev, "SYNC received");
572 err = sierra_net_send_sync(dev);
573 if (err < 0)
574 netdev_err(dev->net,
575 "Send SYNC failed %d\n", err);
576 break;
577 case SIERRA_NET_HIP_EXTENDEDID:
578 netdev_err(dev->net, "Unrecognized HIP msg, "
579 "extmsgid 0x%04x\n", hh.extmsgid.word);
580 break;
581 case SIERRA_NET_HIP_RCGI:
582 /* Ignored. It is a firmware
583 * workaround to solve a Windows driver bug and
584 * may be removed in the future.
585 */
586 break;
587 default:
588 netdev_err(dev->net, "Unrecognized HIP msg, "
589 "msgid 0x%02x\n", hh.msgid.byte);
590 break;
591 }
592 }
593 kfree(buf);
594 }
595 /* The sync timer bit might be set */
596 if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
597 clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
598 dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
599 sierra_net_dosync(priv->usbnet);
600 }
601
602 if (priv->kevent_flags)
603 dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
604 "kevent_flags = 0x%lx", priv->kevent_flags);
605 }
606
607 static void sierra_net_defer_kevent(struct usbnet *dev, int work)
608 {
609 struct sierra_net_data *priv = sierra_net_get_private(dev);
610
611 set_bit(work, &priv->kevent_flags);
612 if (!schedule_work(&priv->sierra_net_kevent))
613 dev_dbg(&dev->udev->dev, "sierra_net_kevent %d may have been dropped", work);
614 else
615 dev_dbg(&dev->udev->dev, "sierra_net_kevent %d scheduled", work);
616 }
617
618 /*
619 * Sync Retransmit Timer Handler. On expiry, kick the work queue
620 */
621 void sierra_sync_timer(unsigned long syncdata)
622 {
623 struct usbnet *dev = (struct usbnet *)syncdata;
624
625 dev_dbg(&dev->udev->dev, "%s", __func__);
626 /* Kick the tasklet */
627 sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
628 }
629
630 static void sierra_net_status(struct usbnet *dev, struct urb *urb)
631 {
632 struct usb_cdc_notification *event;
633
634 dev_dbg(&dev->udev->dev, "%s", __func__);
635 sierra_net_printk_buf(urb->transfer_buffer, urb->actual_length);
636
637 if (urb->actual_length < sizeof *event)
638 return;
639
640 /* Add cases to handle other standard notifications. */
641 event = urb->transfer_buffer;
642 switch (event->bNotificationType) {
643 case USB_CDC_NOTIFY_NETWORK_CONNECTION:
644 case USB_CDC_NOTIFY_SPEED_CHANGE:
645 /* USB 305 sends those */
646 break;
647 case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
648 sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
649 break;
650 default:
651 netdev_err(dev->net, ": unexpected notification %02x!\n",
652 event->bNotificationType);
653 break;
654 }
655 }
656
657 static void sierra_net_get_drvinfo(struct net_device *net,
658 struct ethtool_drvinfo *info)
659 {
660 /* Inherit standard device info */
661 usbnet_get_drvinfo(net, info);
662 strncpy(info->driver, driver_name, sizeof info->driver);
663 strncpy(info->version, DRIVER_VERSION, sizeof info->version);
664 }
665
666 static u32 sierra_net_get_link(struct net_device *net)
667 {
668 struct usbnet *dev = netdev_priv(net);
669 /* Report link is down whenever the interface is down */
670 return sierra_net_get_private(dev)->link_up && netif_running(net);
671 }
672
673 static struct ethtool_ops sierra_net_ethtool_ops = {
674 .get_drvinfo = sierra_net_get_drvinfo,
675 .get_link = sierra_net_get_link,
676 .get_msglevel = usbnet_get_msglevel,
677 .set_msglevel = usbnet_set_msglevel,
678 .get_settings = usbnet_get_settings,
679 .set_settings = usbnet_set_settings,
680 .nway_reset = usbnet_nway_reset,
681 };
682
683 /* MTU can not be more than 1500 bytes, enforce it. */
684 static int sierra_net_change_mtu(struct net_device *net, int new_mtu)
685 {
686 if (new_mtu > SIERRA_NET_MAX_SUPPORTED_MTU)
687 return -EINVAL;
688
689 return usbnet_change_mtu(net, new_mtu);
690 }
691
692 static int is_whitelisted(const u8 ifnum,
693 const struct sierra_net_iface_info *whitelist)
694 {
695 if (whitelist) {
696 const u8 *list = whitelist->ifaceinfo;
697 int i;
698
699 for (i = 0; i < whitelist->infolen; i++) {
700 if (list[i] == ifnum)
701 return 1;
702 }
703 }
704 return 0;
705 }
706
707 static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
708 {
709 int result = 0;
710 u16 *attrdata;
711
712 attrdata = kmalloc(sizeof(*attrdata), GFP_KERNEL);
713 if (!attrdata)
714 return -ENOMEM;
715
716 usb_autopm_get_interface(dev->intf);
717 result = usb_control_msg(
718 dev->udev,
719 usb_rcvctrlpipe(dev->udev, 0),
720 /* _u8 vendor specific request */
721 SWI_USB_REQUEST_GET_FW_ATTR,
722 USB_DIR_IN | USB_TYPE_VENDOR, /* __u8 request type */
723 0x0000, /* __u16 value not used */
724 0x0000, /* __u16 index not used */
725 attrdata, /* char *data */
726 sizeof(*attrdata), /* __u16 size */
727 USB_CTRL_SET_TIMEOUT); /* int timeout */
728 usb_autopm_put_interface(dev->intf);
729
730 if (result < 0) {
731 kfree(attrdata);
732 return -EIO;
733 }
734
735 *datap = *attrdata;
736
737 kfree(attrdata);
738 return result;
739 }
740
741 static int sierra_net_manage_power(struct usbnet *dev, int on)
742 {
743 dev->intf->needs_remote_wakeup = on;
744 return 0;
745 }
746
747 /*
748 * collects the bulk endpoints, the status endpoint.
749 */
750 static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
751 {
752 u8 ifacenum;
753 u8 numendpoints;
754 u16 fwattr = 0;
755 int status;
756 struct ethhdr *eth;
757 struct sierra_net_data *priv;
758 static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
759 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
760 static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
761 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
762
763 struct sierra_net_info_data *data =
764 (struct sierra_net_info_data *)dev->driver_info->data;
765
766 dev_dbg(&dev->udev->dev, "%s", __func__);
767
768 ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
769 /* We only accept certain interfaces */
770 if (!is_whitelisted(ifacenum, &data->whitelist)) {
771 dev_dbg(&dev->udev->dev, "Ignoring interface: %d", ifacenum);
772 return -ENODEV;
773 }
774 numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
775 /* We have three endpoints, bulk in and out, and a status */
776 if (numendpoints != 3) {
777 dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
778 numendpoints);
779 return -ENODEV;
780 }
781 /* Status endpoint set in usbnet_get_endpoints() */
782 dev->status = NULL;
783 status = usbnet_get_endpoints(dev, intf);
784 if (status < 0) {
785 dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
786 status);
787 return -ENODEV;
788 }
789 /* Initialize sierra private data */
790 priv = kzalloc(sizeof *priv, GFP_KERNEL);
791 if (!priv) {
792 dev_err(&dev->udev->dev, "No memory");
793 return -ENOMEM;
794 }
795
796 priv->usbnet = dev;
797 priv->ifnum = ifacenum;
798 /* override change_mtu with our own routine - need to bound check */
799 /* replace structure to our own structure where we have our own
800 * routine - need to bound check
801 */
802 dev->net->netdev_ops = &sierra_net_device_ops;
803
804 /* change MAC addr to include, ifacenum, and to be unique */
805 dev->net->dev_addr[ETH_ALEN-2] = atomic_inc_return(&iface_counter);
806 dev->net->dev_addr[ETH_ALEN-1] = ifacenum;
807
808 /* we will have to manufacture ethernet headers, prepare template */
809 eth = (struct ethhdr *)priv->ethr_hdr_tmpl;
810 memcpy(&eth->h_dest, dev->net->dev_addr, ETH_ALEN);
811 eth->h_proto = cpu_to_be16(ETH_P_IP);
812
813 /* prepare shutdown message template */
814 memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
815 /* set context index initially to 0 - prepares tx hdr template */
816 sierra_net_set_ctx_index(priv, 0);
817
818 /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */
819 dev->rx_urb_size = data->rx_urb_size;
820 if (dev->udev->speed != USB_SPEED_HIGH)
821 dev->rx_urb_size = min_t(size_t, 4096, data->rx_urb_size);
822
823 dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
824 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
825
826 /* Set up the netdev */
827 dev->net->flags |= IFF_NOARP;
828 dev->net->flags |= IFF_MULTICAST;
829 dev->net->ethtool_ops = &sierra_net_ethtool_ops;
830 netif_carrier_off(dev->net);
831
832 sierra_net_set_private(dev, priv);
833
834 priv->kevent_flags = 0;
835
836 /* Use the shared workqueue */
837 INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
838
839 /* Only need to do this once */
840 init_timer(&priv->sync_timer);
841 /* verify fw attributes */
842 status = sierra_net_get_fw_attr(dev, &fwattr);
843 dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
844 if (status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_APM)) {
845 /*******************************************************************************
846 * If you want the default /sys/bus/usb/devices/.../.../power/level to be forced
847 * to auto, the following needs to be compiled in.
848 */
849 #ifdef POWER_LEVEL_AUTO
850 /* make power level default be 'auto' */
851 dev_dbg(&dev->udev->dev, "Enabling APM");
852 usb_enable_autosuspend(dev->udev);
853 #endif
854 } else {
855 dev_info(&intf->dev, "Disabling APM - not supported");
856 usb_disable_autosuspend(dev->udev);
857 }
858 /* test whether firmware supports DHCP */
859 if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
860 /* found incompatible firmware version */
861 dev_err(&dev->udev->dev, "Incompatible driver and firmware"
862 " versions\n");
863 kfree(priv);
864 return -ENODEV;
865 }
866 /* prepare sync message from template */
867 memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
868
869 return 0;
870 }
871
872 static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
873 {
874 struct sierra_net_data *priv = sierra_net_get_private(dev);
875
876 dev_dbg(&dev->udev->dev, "%s", __func__);
877
878 sierra_net_set_private(dev, NULL);
879
880 kfree(priv);
881 }
882
883 static int sierra_net_open(struct usbnet *dev)
884 {
885 dev_dbg(&dev->udev->dev, "%s", __func__);
886
887 /* initiate the sync sequence */
888 sierra_net_dosync(dev);
889
890 return 0;
891 }
892
893 static int sierra_net_stop(struct usbnet *dev)
894 {
895 struct sierra_net_data *priv = sierra_net_get_private(dev);
896
897 dev_dbg(&dev->udev->dev, "%s", __func__);
898
899 /* Kill the timer then flush the work queue */
900 del_timer_sync(&priv->sync_timer);
901
902 cancel_work_sync(&priv->sierra_net_kevent);
903
904 /* tell modem we are going away */
905 sierra_net_send_shutdown(dev);
906
907 return 0;
908 }
909
910 static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
911 struct sk_buff *skb, int len)
912 {
913 struct sk_buff *new_skb;
914
915 /* clone skb */
916 new_skb = skb_clone(skb, GFP_ATOMIC);
917
918 /* remove len bytes from original */
919 skb_pull(skb, len);
920
921 /* trim next packet to it's length */
922 if (new_skb) {
923 skb_trim(new_skb, len);
924 } else {
925 if (netif_msg_rx_err(dev))
926 netdev_err(dev->net, "failed to get skb\n");
927 dev->net->stats.rx_dropped++;
928 }
929
930 return new_skb;
931 }
932
933 /* ---------------------------- Receive data path ----------------------*/
934 static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
935 {
936 int err;
937 struct hip_hdr hh;
938 struct sk_buff *new_skb;
939 struct ethhdr *eth;
940 struct iphdr *ip;
941
942 dev_dbg(&dev->udev->dev, "%s", __func__);
943
944 sierra_net_printk_buf(skb->data, skb->len);
945
946 /* could contain multiple packets */
947 while (likely(skb->len)) {
948 err = parse_hip(skb->data, skb->len, &hh);
949 if (err) {
950 if (netif_msg_rx_err(dev))
951 netdev_err(dev->net, "Invalid HIP header %d\n",
952 err);
953 /* dev->net->stats.rx_errors incremented by caller */
954 dev->net->stats.rx_length_errors++;
955 return 0;
956 }
957
958 /* Validate Extended HIP header */
959 if (!hh.extmsgid.is_present
960 || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
961 if (netif_msg_rx_err(dev))
962 netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
963
964 dev->net->stats.rx_frame_errors++;
965 /* dev->net->stats.rx_errors incremented by caller */;
966 return 0;
967 }
968
969 skb_pull(skb, hh.hdrlen);
970
971 /* We are going to accept this packet, prepare it */
972 memcpy(skb->data, sierra_net_get_private(dev)->ethr_hdr_tmpl,
973 ETH_HLEN);
974
975 ip = (struct iphdr *)((char *)skb->data + ETH_HLEN);
976 if(ip->version == 6) {
977 eth = (struct ethhdr *)skb->data;
978 eth->h_proto = cpu_to_be16(ETH_P_IPV6);
979 }
980
981 /* Last packet in batch handled by usbnet */
982 if (hh.payload_len.word == skb->len)
983 return 1;
984
985 new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
986 if (new_skb)
987 usbnet_skb_return(dev, new_skb);
988
989 } /* while */
990
991 return 0;
992 }
993
994 /* ---------------------------- Transmit data path ----------------------*/
995 struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
996 gfp_t flags)
997 {
998 struct sierra_net_data *priv = sierra_net_get_private(dev);
999 u16 len;
1000 bool need_tail;
1001
1002 dev_dbg(&dev->udev->dev, "%s", __func__);
1003 if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
1004 /* enough head room as is? */
1005 if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
1006 /* Save the Eth/IP length and set up HIP hdr */
1007 len = skb->len;
1008 skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
1009 /* Handle ZLP issue */
1010 need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
1011 % dev->maxpacket == 0);
1012 if (need_tail) {
1013 if (unlikely(skb_tailroom(skb) == 0)) {
1014 netdev_err(dev->net, "tx_fixup:"
1015 "no room for packet\n");
1016 dev_kfree_skb_any(skb);
1017 return NULL;
1018 } else {
1019 skb->data[skb->len] = 0;
1020 __skb_put(skb, 1);
1021 len = len + 1;
1022 }
1023 }
1024 build_hip(skb->data, len, priv);
1025
1026 sierra_net_printk_buf(skb->data, skb->len);
1027 return skb;
1028 } else {
1029 /*
1030 * compensate in the future if necessary
1031 */
1032 netdev_err(dev->net, "tx_fixup: no room for HIP\n");
1033 } /* headroom */
1034 }
1035
1036 if (!priv->link_up)
1037 dev->net->stats.tx_carrier_errors++;
1038
1039 /* tx_dropped incremented by usbnet */
1040
1041 /* filter the packet out, release it */
1042 dev_kfree_skb_any(skb);
1043 return NULL;
1044 }
1045
1046 static int sierra_net_reset_resume(struct usb_interface *intf)
1047 {
1048 struct usbnet *dev = usb_get_intfdata(intf);
1049 netdev_err(dev->net, "%s\n", __func__);
1050 return usbnet_resume(intf);
1051 }
1052
1053 static const u8 sierra_net_ifnum_list[] = { 7, 10, 11 };
1054 static const struct sierra_net_info_data sierra_net_info_data_direct_ip = {
1055 /* .rx_urb_size = 8 * 1024, */
1056 .rx_urb_size = SIERRA_NET_RX_URB_SZ,
1057 .whitelist = {
1058 .infolen = ARRAY_SIZE(sierra_net_ifnum_list),
1059 .ifaceinfo = sierra_net_ifnum_list
1060 }
1061 };
1062
1063 static const struct driver_info sierra_net_info_direct_ip = {
1064 .description = "Sierra Wireless USB-to-WWAN Modem",
1065 .flags = FLAG_WWAN | FLAG_SEND_ZLP,
1066 .bind = sierra_net_bind,
1067 .unbind = sierra_net_unbind,
1068 .status = sierra_net_status,
1069 .rx_fixup = sierra_net_rx_fixup,
1070 .tx_fixup = sierra_net_tx_fixup,
1071 .manage_power = sierra_net_manage_power,
1072 .stop = sierra_net_stop,
1073 .check_connect = sierra_net_open,
1074 .data = (unsigned long)&sierra_net_info_data_direct_ip,
1075 };
1076
1077 static const struct usb_device_id products[] = {
1078 {USB_DEVICE(0x1199, 0x68A3), /* Sierra Wireless Direct IP modem */
1079 .driver_info = (unsigned long) &sierra_net_info_direct_ip},
1080 {USB_DEVICE(0xF3D, 0x68A3), /* AT&T Direct IP modem */
1081 .driver_info = (unsigned long) &sierra_net_info_direct_ip},
1082 {USB_DEVICE(0x1199, 0x68AA), /* Sierra Wireless Direct IP LTE modem */
1083 .driver_info = (unsigned long) &sierra_net_info_direct_ip},
1084 {USB_DEVICE(0xF3D, 0x68AA), /* AT&T Direct IP LTE modem */
1085 .driver_info = (unsigned long) &sierra_net_info_direct_ip},
1086
1087 {}, /* last item */
1088 };
1089 MODULE_DEVICE_TABLE(usb, products);
1090
1091 /* We are based on usbnet, so let it handle the USB driver specifics */
1092 static struct usb_driver sierra_net_driver = {
1093 .name = "sierra_net",
1094 .id_table = products,
1095 .probe = usbnet_probe,
1096 .disconnect = usbnet_disconnect,
1097 .suspend = usbnet_suspend,
1098 .resume = usbnet_resume,
1099 .reset_resume = sierra_net_reset_resume,
1100 .no_dynamic_id = 1,
1101 .supports_autosuspend = 1,
1102 };
1103
1104 static int __init sierra_net_init(void)
1105 {
1106 BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet, data)
1107 < sizeof(struct cdc_state));
1108
1109 return usb_register(&sierra_net_driver);
1110 }
1111
1112 static void __exit sierra_net_exit(void)
1113 {
1114 usb_deregister(&sierra_net_driver);
1115 }
1116
1117 module_exit(sierra_net_exit);
1118 module_init(sierra_net_init);
1119
1120 MODULE_AUTHOR(DRIVER_AUTHOR);
1121 MODULE_DESCRIPTION(DRIVER_DESC);
1122 MODULE_VERSION(DRIVER_VERSION);
1123 MODULE_LICENSE("GPL");
This page took 0.123885 seconds and 5 git commands to generate.