sierra_net.c 28 KB

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