sierra_net.c 26 KB

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