sierra_net.c 27 KB

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