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