sierra_net.c 29 KB

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