sierra_net.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * USB-to-WWAN Driver for Sierra Wireless modems
  4. *
  5. * Copyright (C) 2008, 2009, 2010 Paxton Smith, Matthew Safar, Rory Filer
  6. * <linux@sierrawireless.com>
  7. *
  8. * Portions of this based on the cdc_ether driver by David Brownell (2003-2005)
  9. * and Ole Andre Vadla Ravnas (ActiveSync) (2006).
  10. *
  11. * IMPORTANT DISCLAIMER: This driver is not commercially supported by
  12. * Sierra Wireless. Use at your own risk.
  13. */
  14. #define DRIVER_AUTHOR "Paxton Smith, Matthew Safar, Rory Filer"
  15. #define DRIVER_DESC "USB-to-WWAN Driver for Sierra Wireless modems"
  16. /* if defined debug messages enabled */
  17. /*#define DEBUG*/
  18. #include <linux/module.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/ethtool.h>
  21. #include <linux/mii.h>
  22. #include <linux/sched.h>
  23. #include <linux/timer.h>
  24. #include <linux/usb.h>
  25. #include <linux/usb/cdc.h>
  26. #include <net/ip.h>
  27. #include <net/udp.h>
  28. #include <linux/unaligned.h>
  29. #include <linux/usb/usbnet.h>
  30. #define SWI_USB_REQUEST_GET_FW_ATTR 0x06
  31. #define SWI_GET_FW_ATTR_MASK 0x08
  32. /* atomic counter partially included in MAC address to make sure 2 devices
  33. * do not end up with the same MAC - concept breaks in case of > 255 ifaces
  34. */
  35. static atomic_t iface_counter = ATOMIC_INIT(0);
  36. /*
  37. * SYNC Timer Delay definition used to set the expiry time
  38. */
  39. #define SIERRA_NET_SYNCDELAY (2*HZ)
  40. /* Max. MTU supported. The modem buffers are limited to 1500 */
  41. #define SIERRA_NET_MAX_SUPPORTED_MTU 1500
  42. /* The SIERRA_NET_USBCTL_BUF_LEN defines a buffer size allocated for control
  43. * message reception ... and thus the max. received packet.
  44. * (May be the cause for parse_hip returning -EINVAL)
  45. */
  46. #define SIERRA_NET_USBCTL_BUF_LEN 1024
  47. /* Overriding the default usbnet rx_urb_size */
  48. #define SIERRA_NET_RX_URB_SIZE (8 * 1024)
  49. /* Private data structure */
  50. struct sierra_net_data {
  51. u16 link_up; /* air link up or down */
  52. u8 tx_hdr_template[4]; /* part of HIP hdr for tx'd packets */
  53. u8 sync_msg[4]; /* SYNC message */
  54. u8 shdwn_msg[4]; /* Shutdown message */
  55. /* Backpointer to the container */
  56. struct usbnet *usbnet;
  57. u8 ifnum; /* interface number */
  58. /* Bit masks, must be a power of 2 */
  59. #define SIERRA_NET_EVENT_RESP_AVAIL 0x01
  60. #define SIERRA_NET_TIMER_EXPIRY 0x02
  61. unsigned long kevent_flags;
  62. struct work_struct sierra_net_kevent;
  63. struct timer_list sync_timer; /* For retrying SYNC sequence */
  64. };
  65. struct param {
  66. int is_present;
  67. union {
  68. void *ptr;
  69. u32 dword;
  70. u16 word;
  71. u8 byte;
  72. };
  73. };
  74. /* HIP message type */
  75. #define SIERRA_NET_HIP_EXTENDEDID 0x7F
  76. #define SIERRA_NET_HIP_HSYNC_ID 0x60 /* Modem -> host */
  77. #define SIERRA_NET_HIP_RESTART_ID 0x62 /* Modem -> host */
  78. #define SIERRA_NET_HIP_MSYNC_ID 0x20 /* Host -> modem */
  79. #define SIERRA_NET_HIP_SHUTD_ID 0x26 /* Host -> modem */
  80. #define SIERRA_NET_HIP_EXT_IP_IN_ID 0x0202
  81. #define SIERRA_NET_HIP_EXT_IP_OUT_ID 0x0002
  82. /* 3G UMTS Link Sense Indication definitions */
  83. #define SIERRA_NET_HIP_LSI_UMTSID 0x78
  84. /* Reverse Channel Grant Indication HIP message */
  85. #define SIERRA_NET_HIP_RCGI 0x64
  86. /* LSI Protocol types */
  87. #define SIERRA_NET_PROTOCOL_UMTS 0x01
  88. #define SIERRA_NET_PROTOCOL_UMTS_DS 0x04
  89. /* LSI Coverage */
  90. #define SIERRA_NET_COVERAGE_NONE 0x00
  91. #define SIERRA_NET_COVERAGE_NOPACKET 0x01
  92. /* LSI Session */
  93. #define SIERRA_NET_SESSION_IDLE 0x00
  94. /* LSI Link types */
  95. #define SIERRA_NET_AS_LINK_TYPE_IPV4 0x00
  96. #define SIERRA_NET_AS_LINK_TYPE_IPV6 0x02
  97. struct lsi_umts {
  98. u8 protocol;
  99. u8 unused1;
  100. __be16 length;
  101. /* eventually use a union for the rest - assume umts for now */
  102. u8 coverage;
  103. u8 network_len; /* network name len */
  104. u8 network[40]; /* network name (UCS2, bigendian) */
  105. u8 session_state;
  106. u8 unused3[33];
  107. } __packed;
  108. struct lsi_umts_single {
  109. struct lsi_umts lsi;
  110. u8 link_type;
  111. u8 pdp_addr_len; /* NW-supplied PDP address len */
  112. u8 pdp_addr[16]; /* NW-supplied PDP address (bigendian)) */
  113. u8 unused4[23];
  114. u8 dns1_addr_len; /* NW-supplied 1st DNS address len (bigendian) */
  115. u8 dns1_addr[16]; /* NW-supplied 1st DNS address */
  116. u8 dns2_addr_len; /* NW-supplied 2nd DNS address len */
  117. u8 dns2_addr[16]; /* NW-supplied 2nd DNS address (bigendian)*/
  118. u8 wins1_addr_len; /* NW-supplied 1st Wins address len */
  119. u8 wins1_addr[16]; /* NW-supplied 1st Wins address (bigendian)*/
  120. u8 wins2_addr_len; /* NW-supplied 2nd Wins address len */
  121. u8 wins2_addr[16]; /* NW-supplied 2nd Wins address (bigendian) */
  122. u8 unused5[4];
  123. u8 gw_addr_len; /* NW-supplied GW address len */
  124. u8 gw_addr[16]; /* NW-supplied GW address (bigendian) */
  125. u8 reserved[8];
  126. } __packed;
  127. struct lsi_umts_dual {
  128. struct lsi_umts lsi;
  129. u8 pdp_addr4_len; /* NW-supplied PDP IPv4 address len */
  130. u8 pdp_addr4[4]; /* NW-supplied PDP IPv4 address (bigendian)) */
  131. u8 pdp_addr6_len; /* NW-supplied PDP IPv6 address len */
  132. u8 pdp_addr6[16]; /* NW-supplied PDP IPv6 address (bigendian)) */
  133. u8 unused4[23];
  134. u8 dns1_addr4_len; /* NW-supplied 1st DNS v4 address len (bigendian) */
  135. u8 dns1_addr4[4]; /* NW-supplied 1st DNS v4 address */
  136. u8 dns1_addr6_len; /* NW-supplied 1st DNS v6 address len */
  137. u8 dns1_addr6[16]; /* NW-supplied 1st DNS v6 address (bigendian)*/
  138. u8 dns2_addr4_len; /* NW-supplied 2nd DNS v4 address len (bigendian) */
  139. u8 dns2_addr4[4]; /* NW-supplied 2nd DNS v4 address */
  140. u8 dns2_addr6_len; /* NW-supplied 2nd DNS v6 address len */
  141. u8 dns2_addr6[16]; /* NW-supplied 2nd DNS v6 address (bigendian)*/
  142. u8 unused5[68];
  143. } __packed;
  144. #define SIERRA_NET_LSI_COMMON_LEN 4
  145. #define SIERRA_NET_LSI_UMTS_LEN (sizeof(struct lsi_umts_single))
  146. #define SIERRA_NET_LSI_UMTS_STATUS_LEN \
  147. (SIERRA_NET_LSI_UMTS_LEN - SIERRA_NET_LSI_COMMON_LEN)
  148. #define SIERRA_NET_LSI_UMTS_DS_LEN (sizeof(struct lsi_umts_dual))
  149. #define SIERRA_NET_LSI_UMTS_DS_STATUS_LEN \
  150. (SIERRA_NET_LSI_UMTS_DS_LEN - SIERRA_NET_LSI_COMMON_LEN)
  151. /* Our own net device operations structure */
  152. static const struct net_device_ops sierra_net_device_ops = {
  153. .ndo_open = usbnet_open,
  154. .ndo_stop = usbnet_stop,
  155. .ndo_start_xmit = usbnet_start_xmit,
  156. .ndo_tx_timeout = usbnet_tx_timeout,
  157. .ndo_change_mtu = usbnet_change_mtu,
  158. .ndo_get_stats64 = dev_get_tstats64,
  159. .ndo_set_mac_address = eth_mac_addr,
  160. .ndo_validate_addr = eth_validate_addr,
  161. };
  162. /* get private data associated with passed in usbnet device */
  163. static inline struct sierra_net_data *sierra_net_get_private(struct usbnet *dev)
  164. {
  165. return (struct sierra_net_data *)dev->data[0];
  166. }
  167. /* set private data associated with passed in usbnet device */
  168. static inline void sierra_net_set_private(struct usbnet *dev,
  169. struct sierra_net_data *priv)
  170. {
  171. dev->data[0] = (unsigned long)priv;
  172. }
  173. /* is packet IPv4/IPv6 */
  174. static inline int is_ip(struct sk_buff *skb)
  175. {
  176. return skb->protocol == cpu_to_be16(ETH_P_IP) ||
  177. skb->protocol == cpu_to_be16(ETH_P_IPV6);
  178. }
  179. /*
  180. * check passed in packet and make sure that:
  181. * - it is linear (no scatter/gather)
  182. * - it is ethernet (mac_header properly set)
  183. */
  184. static int check_ethip_packet(struct sk_buff *skb, struct usbnet *dev)
  185. {
  186. skb_reset_mac_header(skb); /* ethernet header */
  187. if (skb_is_nonlinear(skb)) {
  188. netdev_err(dev->net, "Non linear buffer-dropping\n");
  189. return 0;
  190. }
  191. if (!pskb_may_pull(skb, ETH_HLEN))
  192. return 0;
  193. skb->protocol = eth_hdr(skb)->h_proto;
  194. return 1;
  195. }
  196. static const u8 *save16bit(struct param *p, const u8 *datap)
  197. {
  198. p->is_present = 1;
  199. p->word = get_unaligned_be16(datap);
  200. return datap + sizeof(p->word);
  201. }
  202. static const u8 *save8bit(struct param *p, const u8 *datap)
  203. {
  204. p->is_present = 1;
  205. p->byte = *datap;
  206. return datap + sizeof(p->byte);
  207. }
  208. /*----------------------------------------------------------------------------*
  209. * BEGIN HIP *
  210. *----------------------------------------------------------------------------*/
  211. /* HIP header */
  212. #define SIERRA_NET_HIP_HDR_LEN 4
  213. /* Extended HIP header */
  214. #define SIERRA_NET_HIP_EXT_HDR_LEN 6
  215. struct hip_hdr {
  216. int hdrlen;
  217. struct param payload_len;
  218. struct param msgid;
  219. struct param msgspecific;
  220. struct param extmsgid;
  221. };
  222. static int parse_hip(const u8 *buf, const u32 buflen, struct hip_hdr *hh)
  223. {
  224. const u8 *curp = buf;
  225. int padded;
  226. if (buflen < SIERRA_NET_HIP_HDR_LEN)
  227. return -EPROTO;
  228. curp = save16bit(&hh->payload_len, curp);
  229. curp = save8bit(&hh->msgid, curp);
  230. curp = save8bit(&hh->msgspecific, curp);
  231. padded = hh->msgid.byte & 0x80;
  232. hh->msgid.byte &= 0x7F; /* 7 bits */
  233. hh->extmsgid.is_present = (hh->msgid.byte == SIERRA_NET_HIP_EXTENDEDID);
  234. if (hh->extmsgid.is_present) {
  235. if (buflen < SIERRA_NET_HIP_EXT_HDR_LEN)
  236. return -EPROTO;
  237. hh->payload_len.word &= 0x3FFF; /* 14 bits */
  238. curp = save16bit(&hh->extmsgid, curp);
  239. hh->extmsgid.word &= 0x03FF; /* 10 bits */
  240. hh->hdrlen = SIERRA_NET_HIP_EXT_HDR_LEN;
  241. } else {
  242. hh->payload_len.word &= 0x07FF; /* 11 bits */
  243. hh->hdrlen = SIERRA_NET_HIP_HDR_LEN;
  244. }
  245. if (padded) {
  246. hh->hdrlen++;
  247. hh->payload_len.word--;
  248. }
  249. /* if real packet shorter than the claimed length */
  250. if (buflen < (hh->hdrlen + hh->payload_len.word))
  251. return -EINVAL;
  252. return 0;
  253. }
  254. static void build_hip(u8 *buf, const u16 payloadlen,
  255. struct sierra_net_data *priv)
  256. {
  257. /* the following doesn't have the full functionality. We
  258. * currently build only one kind of header, so it is faster this way
  259. */
  260. put_unaligned_be16(payloadlen, buf);
  261. memcpy(buf+2, priv->tx_hdr_template, sizeof(priv->tx_hdr_template));
  262. }
  263. /*----------------------------------------------------------------------------*
  264. * END HIP *
  265. *----------------------------------------------------------------------------*/
  266. static int sierra_net_send_cmd(struct usbnet *dev,
  267. u8 *cmd, int cmdlen, const char * cmd_name)
  268. {
  269. struct sierra_net_data *priv = sierra_net_get_private(dev);
  270. int status;
  271. status = usbnet_write_cmd(dev, USB_CDC_SEND_ENCAPSULATED_COMMAND,
  272. USB_DIR_OUT|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
  273. 0, priv->ifnum, cmd, cmdlen);
  274. if (status != cmdlen && status != -ENODEV)
  275. netdev_err(dev->net, "Submit %s failed %d\n", cmd_name, status);
  276. return status;
  277. }
  278. static int sierra_net_send_sync(struct usbnet *dev)
  279. {
  280. int status;
  281. struct sierra_net_data *priv = sierra_net_get_private(dev);
  282. dev_dbg(&dev->udev->dev, "%s", __func__);
  283. status = sierra_net_send_cmd(dev, priv->sync_msg,
  284. sizeof(priv->sync_msg), "SYNC");
  285. return status;
  286. }
  287. static void sierra_net_set_ctx_index(struct sierra_net_data *priv, u8 ctx_ix)
  288. {
  289. dev_dbg(&(priv->usbnet->udev->dev), "%s %d", __func__, ctx_ix);
  290. priv->tx_hdr_template[0] = 0x3F;
  291. priv->tx_hdr_template[1] = ctx_ix;
  292. *((__be16 *)&priv->tx_hdr_template[2]) =
  293. cpu_to_be16(SIERRA_NET_HIP_EXT_IP_OUT_ID);
  294. }
  295. static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen)
  296. {
  297. struct lsi_umts *lsi = (struct lsi_umts *)data;
  298. u32 expected_length;
  299. if (datalen < sizeof(struct lsi_umts_single)) {
  300. netdev_err(dev->net, "%s: Data length %d, exp >= %zu\n",
  301. __func__, datalen, sizeof(struct lsi_umts_single));
  302. return -1;
  303. }
  304. /* Validate the session state */
  305. if (lsi->session_state == SIERRA_NET_SESSION_IDLE) {
  306. netdev_err(dev->net, "Session idle, 0x%02x\n",
  307. lsi->session_state);
  308. return 0;
  309. }
  310. /* Validate the protocol - only support UMTS for now */
  311. if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS) {
  312. struct lsi_umts_single *single = (struct lsi_umts_single *)lsi;
  313. /* Validate the link type */
  314. if (single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV4 &&
  315. single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV6) {
  316. netdev_err(dev->net, "Link type unsupported: 0x%02x\n",
  317. single->link_type);
  318. return -1;
  319. }
  320. expected_length = SIERRA_NET_LSI_UMTS_STATUS_LEN;
  321. } else if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS_DS) {
  322. expected_length = SIERRA_NET_LSI_UMTS_DS_STATUS_LEN;
  323. } else {
  324. netdev_err(dev->net, "Protocol unsupported, 0x%02x\n",
  325. lsi->protocol);
  326. return -1;
  327. }
  328. if (be16_to_cpu(lsi->length) != expected_length) {
  329. netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
  330. __func__, be16_to_cpu(lsi->length), expected_length);
  331. return -1;
  332. }
  333. /* Validate the coverage */
  334. if (lsi->coverage == SIERRA_NET_COVERAGE_NONE ||
  335. lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) {
  336. netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage);
  337. return 0;
  338. }
  339. /* Set link_sense true */
  340. return 1;
  341. }
  342. static void sierra_net_handle_lsi(struct usbnet *dev, char *data,
  343. struct hip_hdr *hh)
  344. {
  345. struct sierra_net_data *priv = sierra_net_get_private(dev);
  346. int link_up;
  347. link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen,
  348. hh->payload_len.word);
  349. if (link_up < 0) {
  350. netdev_err(dev->net, "Invalid LSI\n");
  351. return;
  352. }
  353. if (link_up) {
  354. sierra_net_set_ctx_index(priv, hh->msgspecific.byte);
  355. priv->link_up = 1;
  356. } else {
  357. priv->link_up = 0;
  358. }
  359. usbnet_link_change(dev, link_up, 0);
  360. }
  361. static void sierra_net_dosync(struct usbnet *dev)
  362. {
  363. int status;
  364. struct sierra_net_data *priv = sierra_net_get_private(dev);
  365. dev_dbg(&dev->udev->dev, "%s", __func__);
  366. /* The SIERRA_NET_HIP_MSYNC_ID command appears to request that the
  367. * firmware restart itself. After restarting, the modem will respond
  368. * with the SIERRA_NET_HIP_RESTART_ID indication. The driver continues
  369. * sending MSYNC commands every few seconds until it receives the
  370. * RESTART event from the firmware
  371. */
  372. /* tell modem we are ready */
  373. status = sierra_net_send_sync(dev);
  374. if (status < 0)
  375. netdev_err(dev->net,
  376. "Send SYNC failed, status %d\n", status);
  377. status = sierra_net_send_sync(dev);
  378. if (status < 0)
  379. netdev_err(dev->net,
  380. "Send SYNC failed, status %d\n", status);
  381. /* Now, start a timer and make sure we get the Restart Indication */
  382. priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
  383. add_timer(&priv->sync_timer);
  384. }
  385. static void sierra_net_kevent(struct work_struct *work)
  386. {
  387. struct sierra_net_data *priv =
  388. container_of(work, struct sierra_net_data, sierra_net_kevent);
  389. struct usbnet *dev = priv->usbnet;
  390. int len;
  391. int err;
  392. u8 *buf;
  393. u8 ifnum;
  394. if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
  395. clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
  396. /* Query the modem for the LSI message */
  397. buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
  398. if (!buf)
  399. return;
  400. ifnum = priv->ifnum;
  401. len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  402. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  403. USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
  404. 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
  405. USB_CTRL_SET_TIMEOUT);
  406. if (len < 0) {
  407. netdev_err(dev->net,
  408. "usb_control_msg failed, status %d\n", len);
  409. } else {
  410. struct hip_hdr hh;
  411. dev_dbg(&dev->udev->dev, "%s: Received status message,"
  412. " %04x bytes", __func__, len);
  413. err = parse_hip(buf, len, &hh);
  414. if (err) {
  415. netdev_err(dev->net, "%s: Bad packet,"
  416. " parse result %d\n", __func__, err);
  417. kfree(buf);
  418. return;
  419. }
  420. /* Validate packet length */
  421. if (len != hh.hdrlen + hh.payload_len.word) {
  422. netdev_err(dev->net, "%s: Bad packet, received"
  423. " %d, expected %d\n", __func__, len,
  424. hh.hdrlen + hh.payload_len.word);
  425. kfree(buf);
  426. return;
  427. }
  428. /* Switch on received message types */
  429. switch (hh.msgid.byte) {
  430. case SIERRA_NET_HIP_LSI_UMTSID:
  431. dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
  432. hh.msgspecific.byte);
  433. sierra_net_handle_lsi(dev, buf, &hh);
  434. break;
  435. case SIERRA_NET_HIP_RESTART_ID:
  436. dev_dbg(&dev->udev->dev, "Restart reported: %d,"
  437. " stopping sync timer",
  438. hh.msgspecific.byte);
  439. /* Got sync resp - stop timer & clear mask */
  440. timer_delete_sync(&priv->sync_timer);
  441. clear_bit(SIERRA_NET_TIMER_EXPIRY,
  442. &priv->kevent_flags);
  443. break;
  444. case SIERRA_NET_HIP_HSYNC_ID:
  445. dev_dbg(&dev->udev->dev, "SYNC received");
  446. err = sierra_net_send_sync(dev);
  447. if (err < 0)
  448. netdev_err(dev->net,
  449. "Send SYNC failed %d\n", err);
  450. break;
  451. case SIERRA_NET_HIP_EXTENDEDID:
  452. netdev_err(dev->net, "Unrecognized HIP msg, "
  453. "extmsgid 0x%04x\n", hh.extmsgid.word);
  454. break;
  455. case SIERRA_NET_HIP_RCGI:
  456. /* Ignored */
  457. break;
  458. default:
  459. netdev_err(dev->net, "Unrecognized HIP msg, "
  460. "msgid 0x%02x\n", hh.msgid.byte);
  461. break;
  462. }
  463. }
  464. kfree(buf);
  465. }
  466. /* The sync timer bit might be set */
  467. if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
  468. clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
  469. dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
  470. sierra_net_dosync(priv->usbnet);
  471. }
  472. if (priv->kevent_flags)
  473. dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
  474. "kevent_flags = 0x%lx", priv->kevent_flags);
  475. }
  476. static void sierra_net_defer_kevent(struct usbnet *dev, int work)
  477. {
  478. struct sierra_net_data *priv = sierra_net_get_private(dev);
  479. set_bit(work, &priv->kevent_flags);
  480. schedule_work(&priv->sierra_net_kevent);
  481. }
  482. /*
  483. * Sync Retransmit Timer Handler. On expiry, kick the work queue
  484. */
  485. static void sierra_sync_timer(struct timer_list *t)
  486. {
  487. struct sierra_net_data *priv = timer_container_of(priv, t, sync_timer);
  488. struct usbnet *dev = priv->usbnet;
  489. dev_dbg(&dev->udev->dev, "%s", __func__);
  490. /* Kick the tasklet */
  491. sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
  492. }
  493. static void sierra_net_status(struct usbnet *dev, struct urb *urb)
  494. {
  495. struct usb_cdc_notification *event;
  496. dev_dbg(&dev->udev->dev, "%s", __func__);
  497. if (urb->actual_length < sizeof *event)
  498. return;
  499. /* Add cases to handle other standard notifications. */
  500. event = urb->transfer_buffer;
  501. switch (event->bNotificationType) {
  502. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  503. case USB_CDC_NOTIFY_SPEED_CHANGE:
  504. /* USB 305 sends those */
  505. break;
  506. case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
  507. sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
  508. break;
  509. default:
  510. netdev_err(dev->net, ": unexpected notification %02x!\n",
  511. event->bNotificationType);
  512. break;
  513. }
  514. }
  515. static u32 sierra_net_get_link(struct net_device *net)
  516. {
  517. struct usbnet *dev = netdev_priv(net);
  518. /* Report link is down whenever the interface is down */
  519. return sierra_net_get_private(dev)->link_up && netif_running(net);
  520. }
  521. static const struct ethtool_ops sierra_net_ethtool_ops = {
  522. .get_drvinfo = usbnet_get_drvinfo,
  523. .get_link = sierra_net_get_link,
  524. .get_msglevel = usbnet_get_msglevel,
  525. .set_msglevel = usbnet_set_msglevel,
  526. .nway_reset = usbnet_nway_reset,
  527. .get_link_ksettings = usbnet_get_link_ksettings_mii,
  528. .set_link_ksettings = usbnet_set_link_ksettings_mii,
  529. };
  530. static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
  531. {
  532. int result = 0;
  533. __le16 attrdata;
  534. result = usbnet_read_cmd(dev,
  535. /* _u8 vendor specific request */
  536. SWI_USB_REQUEST_GET_FW_ATTR,
  537. USB_DIR_IN | USB_TYPE_VENDOR, /* __u8 request type */
  538. 0x0000, /* __u16 value not used */
  539. 0x0000, /* __u16 index not used */
  540. &attrdata, /* char *data */
  541. sizeof(attrdata) /* __u16 size */
  542. );
  543. if (result < 0)
  544. return -EIO;
  545. *datap = le16_to_cpu(attrdata);
  546. return result;
  547. }
  548. /*
  549. * collects the bulk endpoints, the status endpoint.
  550. */
  551. static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
  552. {
  553. u8 ifacenum;
  554. u8 numendpoints;
  555. u16 fwattr = 0;
  556. int status;
  557. struct sierra_net_data *priv;
  558. static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
  559. 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
  560. static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
  561. 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
  562. u8 mod[2];
  563. dev_dbg(&dev->udev->dev, "%s", __func__);
  564. ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
  565. numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
  566. /* We have three endpoints, bulk in and out, and a status */
  567. if (numendpoints != 3) {
  568. dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
  569. numendpoints);
  570. return -ENODEV;
  571. }
  572. /* Status endpoint set in usbnet_get_endpoints() */
  573. dev->status = NULL;
  574. status = usbnet_get_endpoints(dev, intf);
  575. if (status < 0) {
  576. dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
  577. status);
  578. return -ENODEV;
  579. }
  580. if (!dev->status) {
  581. dev_err(&dev->udev->dev, "No status endpoint found");
  582. return -ENODEV;
  583. }
  584. /* Initialize sierra private data */
  585. priv = kzalloc_obj(*priv);
  586. if (!priv)
  587. return -ENOMEM;
  588. priv->usbnet = dev;
  589. priv->ifnum = ifacenum;
  590. dev->net->netdev_ops = &sierra_net_device_ops;
  591. /* change MAC addr to include, ifacenum, and to be unique */
  592. mod[0] = atomic_inc_return(&iface_counter);
  593. mod[1] = ifacenum;
  594. dev_addr_mod(dev->net, ETH_ALEN - 2, mod, 2);
  595. /* prepare shutdown message template */
  596. memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
  597. /* set context index initially to 0 - prepares tx hdr template */
  598. sierra_net_set_ctx_index(priv, 0);
  599. /* prepare sync message template */
  600. memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
  601. /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */
  602. dev->rx_urb_size = SIERRA_NET_RX_URB_SIZE;
  603. if (dev->udev->speed != USB_SPEED_HIGH)
  604. dev->rx_urb_size = min_t(size_t, 4096, SIERRA_NET_RX_URB_SIZE);
  605. dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
  606. dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
  607. dev->net->max_mtu = SIERRA_NET_MAX_SUPPORTED_MTU;
  608. /* Set up the netdev */
  609. dev->net->flags |= IFF_NOARP;
  610. dev->net->ethtool_ops = &sierra_net_ethtool_ops;
  611. netif_carrier_off(dev->net);
  612. sierra_net_set_private(dev, priv);
  613. priv->kevent_flags = 0;
  614. /* Use the shared workqueue */
  615. INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
  616. /* Only need to do this once */
  617. timer_setup(&priv->sync_timer, sierra_sync_timer, 0);
  618. /* verify fw attributes */
  619. status = sierra_net_get_fw_attr(dev, &fwattr);
  620. dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
  621. /* test whether firmware supports DHCP */
  622. if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
  623. /* found incompatible firmware version */
  624. dev_err(&dev->udev->dev, "Incompatible driver and firmware"
  625. " versions\n");
  626. kfree(priv);
  627. return -ENODEV;
  628. }
  629. return 0;
  630. }
  631. static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
  632. {
  633. int status;
  634. struct sierra_net_data *priv = sierra_net_get_private(dev);
  635. dev_dbg(&dev->udev->dev, "%s", __func__);
  636. /* kill the timer and work */
  637. timer_shutdown_sync(&priv->sync_timer);
  638. cancel_work_sync(&priv->sierra_net_kevent);
  639. /* tell modem we are going away */
  640. status = sierra_net_send_cmd(dev, priv->shdwn_msg,
  641. sizeof(priv->shdwn_msg), "Shutdown");
  642. if (status < 0)
  643. netdev_err(dev->net,
  644. "usb_control_msg failed, status %d\n", status);
  645. usbnet_status_stop(dev);
  646. sierra_net_set_private(dev, NULL);
  647. kfree(priv);
  648. }
  649. static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
  650. struct sk_buff *skb, int len)
  651. {
  652. struct sk_buff *new_skb;
  653. /* clone skb */
  654. new_skb = skb_clone(skb, GFP_ATOMIC);
  655. /* remove len bytes from original */
  656. skb_pull(skb, len);
  657. /* trim next packet to it's length */
  658. if (new_skb) {
  659. skb_trim(new_skb, len);
  660. } else {
  661. if (netif_msg_rx_err(dev))
  662. netdev_err(dev->net, "failed to get skb\n");
  663. dev->net->stats.rx_dropped++;
  664. }
  665. return new_skb;
  666. }
  667. /* ---------------------------- Receive data path ----------------------*/
  668. static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  669. {
  670. int err;
  671. struct hip_hdr hh;
  672. struct sk_buff *new_skb;
  673. dev_dbg(&dev->udev->dev, "%s", __func__);
  674. /* could contain multiple packets */
  675. while (likely(skb->len)) {
  676. err = parse_hip(skb->data, skb->len, &hh);
  677. if (err) {
  678. if (netif_msg_rx_err(dev))
  679. netdev_err(dev->net, "Invalid HIP header %d\n",
  680. err);
  681. /* dev->net->stats.rx_errors incremented by caller */
  682. dev->net->stats.rx_length_errors++;
  683. return 0;
  684. }
  685. /* Validate Extended HIP header */
  686. if (!hh.extmsgid.is_present
  687. || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
  688. if (netif_msg_rx_err(dev))
  689. netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
  690. dev->net->stats.rx_frame_errors++;
  691. /* dev->net->stats.rx_errors incremented by caller */
  692. return 0;
  693. }
  694. skb_pull(skb, hh.hdrlen);
  695. /* We are going to accept this packet, prepare it.
  696. * In case protocol is IPv6, keep it, otherwise force IPv4.
  697. */
  698. skb_reset_mac_header(skb);
  699. if (eth_hdr(skb)->h_proto != cpu_to_be16(ETH_P_IPV6))
  700. eth_hdr(skb)->h_proto = cpu_to_be16(ETH_P_IP);
  701. eth_zero_addr(eth_hdr(skb)->h_source);
  702. memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
  703. /* Last packet in batch handled by usbnet */
  704. if (hh.payload_len.word == skb->len)
  705. return 1;
  706. new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
  707. if (new_skb)
  708. usbnet_skb_return(dev, new_skb);
  709. } /* while */
  710. return 0;
  711. }
  712. /* ---------------------------- Transmit data path ----------------------*/
  713. static struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev,
  714. struct sk_buff *skb, gfp_t flags)
  715. {
  716. struct sierra_net_data *priv = sierra_net_get_private(dev);
  717. u16 len;
  718. bool need_tail;
  719. BUILD_BUG_ON(sizeof_field(struct usbnet, data)
  720. < sizeof(struct cdc_state));
  721. dev_dbg(&dev->udev->dev, "%s", __func__);
  722. if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
  723. /* enough head room as is? */
  724. if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
  725. /* Save the Eth/IP length and set up HIP hdr */
  726. len = skb->len;
  727. skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
  728. /* Handle ZLP issue */
  729. need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
  730. % dev->maxpacket == 0);
  731. if (need_tail) {
  732. if (unlikely(skb_tailroom(skb) == 0)) {
  733. netdev_err(dev->net, "tx_fixup:"
  734. "no room for packet\n");
  735. dev_kfree_skb_any(skb);
  736. return NULL;
  737. } else {
  738. skb->data[skb->len] = 0;
  739. __skb_put(skb, 1);
  740. len = len + 1;
  741. }
  742. }
  743. build_hip(skb->data, len, priv);
  744. return skb;
  745. } else {
  746. /*
  747. * compensate in the future if necessary
  748. */
  749. netdev_err(dev->net, "tx_fixup: no room for HIP\n");
  750. } /* headroom */
  751. }
  752. if (!priv->link_up)
  753. dev->net->stats.tx_carrier_errors++;
  754. /* tx_dropped incremented by usbnet */
  755. /* filter the packet out, release it */
  756. dev_kfree_skb_any(skb);
  757. return NULL;
  758. }
  759. static const struct driver_info sierra_net_info_direct_ip = {
  760. .description = "Sierra Wireless USB-to-WWAN Modem",
  761. .flags = FLAG_WWAN | FLAG_SEND_ZLP,
  762. .bind = sierra_net_bind,
  763. .unbind = sierra_net_unbind,
  764. .status = sierra_net_status,
  765. .rx_fixup = sierra_net_rx_fixup,
  766. .tx_fixup = sierra_net_tx_fixup,
  767. };
  768. static int
  769. sierra_net_probe(struct usb_interface *udev, const struct usb_device_id *prod)
  770. {
  771. int ret;
  772. ret = usbnet_probe(udev, prod);
  773. if (ret == 0) {
  774. struct usbnet *dev = usb_get_intfdata(udev);
  775. ret = usbnet_status_start(dev, GFP_KERNEL);
  776. if (ret == 0) {
  777. /* Interrupt URB now set up; initiate sync sequence */
  778. sierra_net_dosync(dev);
  779. }
  780. }
  781. return ret;
  782. }
  783. #define DIRECT_IP_DEVICE(vend, prod) \
  784. {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 7), \
  785. .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
  786. {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 10), \
  787. .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
  788. {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 11), \
  789. .driver_info = (unsigned long)&sierra_net_info_direct_ip}
  790. static const struct usb_device_id products[] = {
  791. DIRECT_IP_DEVICE(0x1199, 0x68A3), /* Sierra Wireless USB-to-WWAN modem */
  792. DIRECT_IP_DEVICE(0x0F3D, 0x68A3), /* AT&T Direct IP modem */
  793. DIRECT_IP_DEVICE(0x1199, 0x68AA), /* Sierra Wireless Direct IP LTE modem */
  794. DIRECT_IP_DEVICE(0x0F3D, 0x68AA), /* AT&T Direct IP LTE modem */
  795. {}, /* last item */
  796. };
  797. MODULE_DEVICE_TABLE(usb, products);
  798. /* We are based on usbnet, so let it handle the USB driver specifics */
  799. static struct usb_driver sierra_net_driver = {
  800. .name = "sierra_net",
  801. .id_table = products,
  802. .probe = sierra_net_probe,
  803. .disconnect = usbnet_disconnect,
  804. .suspend = usbnet_suspend,
  805. .resume = usbnet_resume,
  806. .no_dynamic_id = 1,
  807. .disable_hub_initiated_lpm = 1,
  808. };
  809. module_usb_driver(sierra_net_driver);
  810. MODULE_AUTHOR(DRIVER_AUTHOR);
  811. MODULE_DESCRIPTION(DRIVER_DESC);
  812. MODULE_LICENSE("GPL");