sierra_net.c 28 KB

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