sierra_net.c 27 KB

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