sierra_net.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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_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 = usbnet_get_stats64,
  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 inline int sierra_net_is_valid_addrlen(u8 len)
  298. {
  299. return len == sizeof(struct in_addr);
  300. }
  301. static int sierra_net_parse_lsi(struct usbnet *dev, char *data, int datalen)
  302. {
  303. struct lsi_umts *lsi = (struct lsi_umts *)data;
  304. u32 expected_length;
  305. if (datalen < sizeof(struct lsi_umts_single)) {
  306. netdev_err(dev->net, "%s: Data length %d, exp >= %zu\n",
  307. __func__, datalen, sizeof(struct lsi_umts_single));
  308. return -1;
  309. }
  310. /* Validate the session state */
  311. if (lsi->session_state == SIERRA_NET_SESSION_IDLE) {
  312. netdev_err(dev->net, "Session idle, 0x%02x\n",
  313. lsi->session_state);
  314. return 0;
  315. }
  316. /* Validate the protocol - only support UMTS for now */
  317. if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS) {
  318. struct lsi_umts_single *single = (struct lsi_umts_single *)lsi;
  319. /* Validate the link type */
  320. if (single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV4 &&
  321. single->link_type != SIERRA_NET_AS_LINK_TYPE_IPV6) {
  322. netdev_err(dev->net, "Link type unsupported: 0x%02x\n",
  323. single->link_type);
  324. return -1;
  325. }
  326. expected_length = SIERRA_NET_LSI_UMTS_STATUS_LEN;
  327. } else if (lsi->protocol == SIERRA_NET_PROTOCOL_UMTS_DS) {
  328. expected_length = SIERRA_NET_LSI_UMTS_DS_STATUS_LEN;
  329. } else {
  330. netdev_err(dev->net, "Protocol unsupported, 0x%02x\n",
  331. lsi->protocol);
  332. return -1;
  333. }
  334. if (be16_to_cpu(lsi->length) != expected_length) {
  335. netdev_err(dev->net, "%s: LSI_UMTS_STATUS_LEN %d, exp %u\n",
  336. __func__, be16_to_cpu(lsi->length), expected_length);
  337. return -1;
  338. }
  339. /* Validate the coverage */
  340. if (lsi->coverage == SIERRA_NET_COVERAGE_NONE ||
  341. lsi->coverage == SIERRA_NET_COVERAGE_NOPACKET) {
  342. netdev_err(dev->net, "No coverage, 0x%02x\n", lsi->coverage);
  343. return 0;
  344. }
  345. /* Set link_sense true */
  346. return 1;
  347. }
  348. static void sierra_net_handle_lsi(struct usbnet *dev, char *data,
  349. struct hip_hdr *hh)
  350. {
  351. struct sierra_net_data *priv = sierra_net_get_private(dev);
  352. int link_up;
  353. link_up = sierra_net_parse_lsi(dev, data + hh->hdrlen,
  354. hh->payload_len.word);
  355. if (link_up < 0) {
  356. netdev_err(dev->net, "Invalid LSI\n");
  357. return;
  358. }
  359. if (link_up) {
  360. sierra_net_set_ctx_index(priv, hh->msgspecific.byte);
  361. priv->link_up = 1;
  362. } else {
  363. priv->link_up = 0;
  364. }
  365. usbnet_link_change(dev, link_up, 0);
  366. }
  367. static void sierra_net_dosync(struct usbnet *dev)
  368. {
  369. int status;
  370. struct sierra_net_data *priv = sierra_net_get_private(dev);
  371. dev_dbg(&dev->udev->dev, "%s", __func__);
  372. /* The SIERRA_NET_HIP_MSYNC_ID command appears to request that the
  373. * firmware restart itself. After restarting, the modem will respond
  374. * with the SIERRA_NET_HIP_RESTART_ID indication. The driver continues
  375. * sending MSYNC commands every few seconds until it receives the
  376. * RESTART event from the firmware
  377. */
  378. /* tell modem we are ready */
  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. status = sierra_net_send_sync(dev);
  384. if (status < 0)
  385. netdev_err(dev->net,
  386. "Send SYNC failed, status %d\n", status);
  387. /* Now, start a timer and make sure we get the Restart Indication */
  388. priv->sync_timer.expires = jiffies + SIERRA_NET_SYNCDELAY;
  389. add_timer(&priv->sync_timer);
  390. }
  391. static void sierra_net_kevent(struct work_struct *work)
  392. {
  393. struct sierra_net_data *priv =
  394. container_of(work, struct sierra_net_data, sierra_net_kevent);
  395. struct usbnet *dev = priv->usbnet;
  396. int len;
  397. int err;
  398. u8 *buf;
  399. u8 ifnum;
  400. if (test_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags)) {
  401. clear_bit(SIERRA_NET_EVENT_RESP_AVAIL, &priv->kevent_flags);
  402. /* Query the modem for the LSI message */
  403. buf = kzalloc(SIERRA_NET_USBCTL_BUF_LEN, GFP_KERNEL);
  404. if (!buf)
  405. return;
  406. ifnum = priv->ifnum;
  407. len = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  408. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  409. USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE,
  410. 0, ifnum, buf, SIERRA_NET_USBCTL_BUF_LEN,
  411. USB_CTRL_SET_TIMEOUT);
  412. if (len < 0) {
  413. netdev_err(dev->net,
  414. "usb_control_msg failed, status %d\n", len);
  415. } else {
  416. struct hip_hdr hh;
  417. dev_dbg(&dev->udev->dev, "%s: Received status message,"
  418. " %04x bytes", __func__, len);
  419. err = parse_hip(buf, len, &hh);
  420. if (err) {
  421. netdev_err(dev->net, "%s: Bad packet,"
  422. " parse result %d\n", __func__, err);
  423. kfree(buf);
  424. return;
  425. }
  426. /* Validate packet length */
  427. if (len != hh.hdrlen + hh.payload_len.word) {
  428. netdev_err(dev->net, "%s: Bad packet, received"
  429. " %d, expected %d\n", __func__, len,
  430. hh.hdrlen + hh.payload_len.word);
  431. kfree(buf);
  432. return;
  433. }
  434. /* Switch on received message types */
  435. switch (hh.msgid.byte) {
  436. case SIERRA_NET_HIP_LSI_UMTSID:
  437. dev_dbg(&dev->udev->dev, "LSI for ctx:%d",
  438. hh.msgspecific.byte);
  439. sierra_net_handle_lsi(dev, buf, &hh);
  440. break;
  441. case SIERRA_NET_HIP_RESTART_ID:
  442. dev_dbg(&dev->udev->dev, "Restart reported: %d,"
  443. " stopping sync timer",
  444. hh.msgspecific.byte);
  445. /* Got sync resp - stop timer & clear mask */
  446. del_timer_sync(&priv->sync_timer);
  447. clear_bit(SIERRA_NET_TIMER_EXPIRY,
  448. &priv->kevent_flags);
  449. break;
  450. case SIERRA_NET_HIP_HSYNC_ID:
  451. dev_dbg(&dev->udev->dev, "SYNC received");
  452. err = sierra_net_send_sync(dev);
  453. if (err < 0)
  454. netdev_err(dev->net,
  455. "Send SYNC failed %d\n", err);
  456. break;
  457. case SIERRA_NET_HIP_EXTENDEDID:
  458. netdev_err(dev->net, "Unrecognized HIP msg, "
  459. "extmsgid 0x%04x\n", hh.extmsgid.word);
  460. break;
  461. case SIERRA_NET_HIP_RCGI:
  462. /* Ignored */
  463. break;
  464. default:
  465. netdev_err(dev->net, "Unrecognized HIP msg, "
  466. "msgid 0x%02x\n", hh.msgid.byte);
  467. break;
  468. }
  469. }
  470. kfree(buf);
  471. }
  472. /* The sync timer bit might be set */
  473. if (test_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags)) {
  474. clear_bit(SIERRA_NET_TIMER_EXPIRY, &priv->kevent_flags);
  475. dev_dbg(&dev->udev->dev, "Deferred sync timer expiry");
  476. sierra_net_dosync(priv->usbnet);
  477. }
  478. if (priv->kevent_flags)
  479. dev_dbg(&dev->udev->dev, "sierra_net_kevent done, "
  480. "kevent_flags = 0x%lx", priv->kevent_flags);
  481. }
  482. static void sierra_net_defer_kevent(struct usbnet *dev, int work)
  483. {
  484. struct sierra_net_data *priv = sierra_net_get_private(dev);
  485. set_bit(work, &priv->kevent_flags);
  486. schedule_work(&priv->sierra_net_kevent);
  487. }
  488. /*
  489. * Sync Retransmit Timer Handler. On expiry, kick the work queue
  490. */
  491. static void sierra_sync_timer(struct timer_list *t)
  492. {
  493. struct sierra_net_data *priv = from_timer(priv, t, sync_timer);
  494. struct usbnet *dev = priv->usbnet;
  495. dev_dbg(&dev->udev->dev, "%s", __func__);
  496. /* Kick the tasklet */
  497. sierra_net_defer_kevent(dev, SIERRA_NET_TIMER_EXPIRY);
  498. }
  499. static void sierra_net_status(struct usbnet *dev, struct urb *urb)
  500. {
  501. struct usb_cdc_notification *event;
  502. dev_dbg(&dev->udev->dev, "%s", __func__);
  503. if (urb->actual_length < sizeof *event)
  504. return;
  505. /* Add cases to handle other standard notifications. */
  506. event = urb->transfer_buffer;
  507. switch (event->bNotificationType) {
  508. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  509. case USB_CDC_NOTIFY_SPEED_CHANGE:
  510. /* USB 305 sends those */
  511. break;
  512. case USB_CDC_NOTIFY_RESPONSE_AVAILABLE:
  513. sierra_net_defer_kevent(dev, SIERRA_NET_EVENT_RESP_AVAIL);
  514. break;
  515. default:
  516. netdev_err(dev->net, ": unexpected notification %02x!\n",
  517. event->bNotificationType);
  518. break;
  519. }
  520. }
  521. static void sierra_net_get_drvinfo(struct net_device *net,
  522. struct ethtool_drvinfo *info)
  523. {
  524. /* Inherit standard device info */
  525. usbnet_get_drvinfo(net, info);
  526. strlcpy(info->driver, driver_name, sizeof(info->driver));
  527. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  528. }
  529. static u32 sierra_net_get_link(struct net_device *net)
  530. {
  531. struct usbnet *dev = netdev_priv(net);
  532. /* Report link is down whenever the interface is down */
  533. return sierra_net_get_private(dev)->link_up && netif_running(net);
  534. }
  535. static const struct ethtool_ops sierra_net_ethtool_ops = {
  536. .get_drvinfo = sierra_net_get_drvinfo,
  537. .get_link = sierra_net_get_link,
  538. .get_msglevel = usbnet_get_msglevel,
  539. .set_msglevel = usbnet_set_msglevel,
  540. .nway_reset = usbnet_nway_reset,
  541. .get_link_ksettings = usbnet_get_link_ksettings,
  542. .set_link_ksettings = usbnet_set_link_ksettings,
  543. };
  544. static int sierra_net_get_fw_attr(struct usbnet *dev, u16 *datap)
  545. {
  546. int result = 0;
  547. __le16 attrdata;
  548. result = usbnet_read_cmd(dev,
  549. /* _u8 vendor specific request */
  550. SWI_USB_REQUEST_GET_FW_ATTR,
  551. USB_DIR_IN | USB_TYPE_VENDOR, /* __u8 request type */
  552. 0x0000, /* __u16 value not used */
  553. 0x0000, /* __u16 index not used */
  554. &attrdata, /* char *data */
  555. sizeof(attrdata) /* __u16 size */
  556. );
  557. if (result < 0)
  558. return -EIO;
  559. *datap = le16_to_cpu(attrdata);
  560. return result;
  561. }
  562. /*
  563. * collects the bulk endpoints, the status endpoint.
  564. */
  565. static int sierra_net_bind(struct usbnet *dev, struct usb_interface *intf)
  566. {
  567. u8 ifacenum;
  568. u8 numendpoints;
  569. u16 fwattr = 0;
  570. int status;
  571. struct sierra_net_data *priv;
  572. static const u8 sync_tmplate[sizeof(priv->sync_msg)] = {
  573. 0x00, 0x00, SIERRA_NET_HIP_MSYNC_ID, 0x00};
  574. static const u8 shdwn_tmplate[sizeof(priv->shdwn_msg)] = {
  575. 0x00, 0x00, SIERRA_NET_HIP_SHUTD_ID, 0x00};
  576. dev_dbg(&dev->udev->dev, "%s", __func__);
  577. ifacenum = intf->cur_altsetting->desc.bInterfaceNumber;
  578. numendpoints = intf->cur_altsetting->desc.bNumEndpoints;
  579. /* We have three endpoints, bulk in and out, and a status */
  580. if (numendpoints != 3) {
  581. dev_err(&dev->udev->dev, "Expected 3 endpoints, found: %d",
  582. numendpoints);
  583. return -ENODEV;
  584. }
  585. /* Status endpoint set in usbnet_get_endpoints() */
  586. dev->status = NULL;
  587. status = usbnet_get_endpoints(dev, intf);
  588. if (status < 0) {
  589. dev_err(&dev->udev->dev, "Error in usbnet_get_endpoints (%d)",
  590. status);
  591. return -ENODEV;
  592. }
  593. /* Initialize sierra private data */
  594. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  595. if (!priv)
  596. return -ENOMEM;
  597. priv->usbnet = dev;
  598. priv->ifnum = ifacenum;
  599. dev->net->netdev_ops = &sierra_net_device_ops;
  600. /* change MAC addr to include, ifacenum, and to be unique */
  601. dev->net->dev_addr[ETH_ALEN-2] = atomic_inc_return(&iface_counter);
  602. dev->net->dev_addr[ETH_ALEN-1] = ifacenum;
  603. /* prepare shutdown message template */
  604. memcpy(priv->shdwn_msg, shdwn_tmplate, sizeof(priv->shdwn_msg));
  605. /* set context index initially to 0 - prepares tx hdr template */
  606. sierra_net_set_ctx_index(priv, 0);
  607. /* prepare sync message template */
  608. memcpy(priv->sync_msg, sync_tmplate, sizeof(priv->sync_msg));
  609. /* decrease the rx_urb_size and max_tx_size to 4k on USB 1.1 */
  610. dev->rx_urb_size = SIERRA_NET_RX_URB_SIZE;
  611. if (dev->udev->speed != USB_SPEED_HIGH)
  612. dev->rx_urb_size = min_t(size_t, 4096, SIERRA_NET_RX_URB_SIZE);
  613. dev->net->hard_header_len += SIERRA_NET_HIP_EXT_HDR_LEN;
  614. dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
  615. dev->net->max_mtu = SIERRA_NET_MAX_SUPPORTED_MTU;
  616. /* Set up the netdev */
  617. dev->net->flags |= IFF_NOARP;
  618. dev->net->ethtool_ops = &sierra_net_ethtool_ops;
  619. netif_carrier_off(dev->net);
  620. sierra_net_set_private(dev, priv);
  621. priv->kevent_flags = 0;
  622. /* Use the shared workqueue */
  623. INIT_WORK(&priv->sierra_net_kevent, sierra_net_kevent);
  624. /* Only need to do this once */
  625. timer_setup(&priv->sync_timer, sierra_sync_timer, 0);
  626. /* verify fw attributes */
  627. status = sierra_net_get_fw_attr(dev, &fwattr);
  628. dev_dbg(&dev->udev->dev, "Fw attr: %x\n", fwattr);
  629. /* test whether firmware supports DHCP */
  630. if (!(status == sizeof(fwattr) && (fwattr & SWI_GET_FW_ATTR_MASK))) {
  631. /* found incompatible firmware version */
  632. dev_err(&dev->udev->dev, "Incompatible driver and firmware"
  633. " versions\n");
  634. kfree(priv);
  635. return -ENODEV;
  636. }
  637. return 0;
  638. }
  639. static void sierra_net_unbind(struct usbnet *dev, struct usb_interface *intf)
  640. {
  641. int status;
  642. struct sierra_net_data *priv = sierra_net_get_private(dev);
  643. dev_dbg(&dev->udev->dev, "%s", __func__);
  644. /* kill the timer and work */
  645. del_timer_sync(&priv->sync_timer);
  646. cancel_work_sync(&priv->sierra_net_kevent);
  647. /* tell modem we are going away */
  648. status = sierra_net_send_cmd(dev, priv->shdwn_msg,
  649. sizeof(priv->shdwn_msg), "Shutdown");
  650. if (status < 0)
  651. netdev_err(dev->net,
  652. "usb_control_msg failed, status %d\n", status);
  653. usbnet_status_stop(dev);
  654. sierra_net_set_private(dev, NULL);
  655. kfree(priv);
  656. }
  657. static struct sk_buff *sierra_net_skb_clone(struct usbnet *dev,
  658. struct sk_buff *skb, int len)
  659. {
  660. struct sk_buff *new_skb;
  661. /* clone skb */
  662. new_skb = skb_clone(skb, GFP_ATOMIC);
  663. /* remove len bytes from original */
  664. skb_pull(skb, len);
  665. /* trim next packet to it's length */
  666. if (new_skb) {
  667. skb_trim(new_skb, len);
  668. } else {
  669. if (netif_msg_rx_err(dev))
  670. netdev_err(dev->net, "failed to get skb\n");
  671. dev->net->stats.rx_dropped++;
  672. }
  673. return new_skb;
  674. }
  675. /* ---------------------------- Receive data path ----------------------*/
  676. static int sierra_net_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  677. {
  678. int err;
  679. struct hip_hdr hh;
  680. struct sk_buff *new_skb;
  681. dev_dbg(&dev->udev->dev, "%s", __func__);
  682. /* could contain multiple packets */
  683. while (likely(skb->len)) {
  684. err = parse_hip(skb->data, skb->len, &hh);
  685. if (err) {
  686. if (netif_msg_rx_err(dev))
  687. netdev_err(dev->net, "Invalid HIP header %d\n",
  688. err);
  689. /* dev->net->stats.rx_errors incremented by caller */
  690. dev->net->stats.rx_length_errors++;
  691. return 0;
  692. }
  693. /* Validate Extended HIP header */
  694. if (!hh.extmsgid.is_present
  695. || hh.extmsgid.word != SIERRA_NET_HIP_EXT_IP_IN_ID) {
  696. if (netif_msg_rx_err(dev))
  697. netdev_err(dev->net, "HIP/ETH: Invalid pkt\n");
  698. dev->net->stats.rx_frame_errors++;
  699. /* dev->net->stats.rx_errors incremented by caller */
  700. return 0;
  701. }
  702. skb_pull(skb, hh.hdrlen);
  703. /* We are going to accept this packet, prepare it.
  704. * In case protocol is IPv6, keep it, otherwise force IPv4.
  705. */
  706. skb_reset_mac_header(skb);
  707. if (eth_hdr(skb)->h_proto != cpu_to_be16(ETH_P_IPV6))
  708. eth_hdr(skb)->h_proto = cpu_to_be16(ETH_P_IP);
  709. eth_zero_addr(eth_hdr(skb)->h_source);
  710. memcpy(eth_hdr(skb)->h_dest, dev->net->dev_addr, ETH_ALEN);
  711. /* Last packet in batch handled by usbnet */
  712. if (hh.payload_len.word == skb->len)
  713. return 1;
  714. new_skb = sierra_net_skb_clone(dev, skb, hh.payload_len.word);
  715. if (new_skb)
  716. usbnet_skb_return(dev, new_skb);
  717. } /* while */
  718. return 0;
  719. }
  720. /* ---------------------------- Transmit data path ----------------------*/
  721. static struct sk_buff *sierra_net_tx_fixup(struct usbnet *dev,
  722. struct sk_buff *skb, gfp_t flags)
  723. {
  724. struct sierra_net_data *priv = sierra_net_get_private(dev);
  725. u16 len;
  726. bool need_tail;
  727. BUILD_BUG_ON(FIELD_SIZEOF(struct usbnet, data)
  728. < sizeof(struct cdc_state));
  729. dev_dbg(&dev->udev->dev, "%s", __func__);
  730. if (priv->link_up && check_ethip_packet(skb, dev) && is_ip(skb)) {
  731. /* enough head room as is? */
  732. if (SIERRA_NET_HIP_EXT_HDR_LEN <= skb_headroom(skb)) {
  733. /* Save the Eth/IP length and set up HIP hdr */
  734. len = skb->len;
  735. skb_push(skb, SIERRA_NET_HIP_EXT_HDR_LEN);
  736. /* Handle ZLP issue */
  737. need_tail = ((len + SIERRA_NET_HIP_EXT_HDR_LEN)
  738. % dev->maxpacket == 0);
  739. if (need_tail) {
  740. if (unlikely(skb_tailroom(skb) == 0)) {
  741. netdev_err(dev->net, "tx_fixup:"
  742. "no room for packet\n");
  743. dev_kfree_skb_any(skb);
  744. return NULL;
  745. } else {
  746. skb->data[skb->len] = 0;
  747. __skb_put(skb, 1);
  748. len = len + 1;
  749. }
  750. }
  751. build_hip(skb->data, len, priv);
  752. return skb;
  753. } else {
  754. /*
  755. * compensate in the future if necessary
  756. */
  757. netdev_err(dev->net, "tx_fixup: no room for HIP\n");
  758. } /* headroom */
  759. }
  760. if (!priv->link_up)
  761. dev->net->stats.tx_carrier_errors++;
  762. /* tx_dropped incremented by usbnet */
  763. /* filter the packet out, release it */
  764. dev_kfree_skb_any(skb);
  765. return NULL;
  766. }
  767. static const struct driver_info sierra_net_info_direct_ip = {
  768. .description = "Sierra Wireless USB-to-WWAN Modem",
  769. .flags = FLAG_WWAN | FLAG_SEND_ZLP,
  770. .bind = sierra_net_bind,
  771. .unbind = sierra_net_unbind,
  772. .status = sierra_net_status,
  773. .rx_fixup = sierra_net_rx_fixup,
  774. .tx_fixup = sierra_net_tx_fixup,
  775. };
  776. static int
  777. sierra_net_probe(struct usb_interface *udev, const struct usb_device_id *prod)
  778. {
  779. int ret;
  780. ret = usbnet_probe(udev, prod);
  781. if (ret == 0) {
  782. struct usbnet *dev = usb_get_intfdata(udev);
  783. ret = usbnet_status_start(dev, GFP_KERNEL);
  784. if (ret == 0) {
  785. /* Interrupt URB now set up; initiate sync sequence */
  786. sierra_net_dosync(dev);
  787. }
  788. }
  789. return ret;
  790. }
  791. #define DIRECT_IP_DEVICE(vend, prod) \
  792. {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 7), \
  793. .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
  794. {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 10), \
  795. .driver_info = (unsigned long)&sierra_net_info_direct_ip}, \
  796. {USB_DEVICE_INTERFACE_NUMBER(vend, prod, 11), \
  797. .driver_info = (unsigned long)&sierra_net_info_direct_ip}
  798. static const struct usb_device_id products[] = {
  799. DIRECT_IP_DEVICE(0x1199, 0x68A3), /* Sierra Wireless USB-to-WWAN modem */
  800. DIRECT_IP_DEVICE(0x0F3D, 0x68A3), /* AT&T Direct IP modem */
  801. DIRECT_IP_DEVICE(0x1199, 0x68AA), /* Sierra Wireless Direct IP LTE modem */
  802. DIRECT_IP_DEVICE(0x0F3D, 0x68AA), /* AT&T Direct IP LTE modem */
  803. {}, /* last item */
  804. };
  805. MODULE_DEVICE_TABLE(usb, products);
  806. /* We are based on usbnet, so let it handle the USB driver specifics */
  807. static struct usb_driver sierra_net_driver = {
  808. .name = "sierra_net",
  809. .id_table = products,
  810. .probe = sierra_net_probe,
  811. .disconnect = usbnet_disconnect,
  812. .suspend = usbnet_suspend,
  813. .resume = usbnet_resume,
  814. .no_dynamic_id = 1,
  815. .disable_hub_initiated_lpm = 1,
  816. };
  817. module_usb_driver(sierra_net_driver);
  818. MODULE_AUTHOR(DRIVER_AUTHOR);
  819. MODULE_DESCRIPTION(DRIVER_DESC);
  820. MODULE_VERSION(DRIVER_VERSION);
  821. MODULE_LICENSE("GPL");