PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/staging/brcm80211/brcmfmac/dhd_cdc.c

https://github.com/mstsirkin/linux
C | 502 lines | 356 code | 94 blank | 52 comment | 53 complexity | 076a9c808c873bbbcc8b8500aab90a0d MD5 | raw file
  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/sched.h>
  19. #include <defs.h>
  20. #include <brcmu_utils.h>
  21. #include <brcmu_wifi.h>
  22. #include "dhd.h"
  23. #include "dhd_proto.h"
  24. #include "dhd_bus.h"
  25. #include "dhd_dbg.h"
  26. struct brcmf_proto_cdc_ioctl {
  27. u32 cmd; /* ioctl command value */
  28. u32 len; /* lower 16: output buflen;
  29. * upper 16: input buflen (excludes header) */
  30. u32 flags; /* flag defns given below */
  31. u32 status; /* status code returned from the device */
  32. };
  33. /* Max valid buffer size that can be sent to the dongle */
  34. #define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN)
  35. /* CDC flag definitions */
  36. #define CDCF_IOC_ERROR 0x01 /* 1=ioctl cmd failed */
  37. #define CDCF_IOC_SET 0x02 /* 0=get, 1=set cmd */
  38. #define CDCF_IOC_IF_MASK 0xF000 /* I/F index */
  39. #define CDCF_IOC_IF_SHIFT 12
  40. #define CDCF_IOC_ID_MASK 0xFFFF0000 /* id an ioctl pairing */
  41. #define CDCF_IOC_ID_SHIFT 16 /* ID Mask shift bits */
  42. #define CDC_IOC_ID(flags) \
  43. (((flags) & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT)
  44. #define CDC_SET_IF_IDX(hdr, idx) \
  45. ((hdr)->flags = (((hdr)->flags & ~CDCF_IOC_IF_MASK) | \
  46. ((idx) << CDCF_IOC_IF_SHIFT)))
  47. /*
  48. * BDC header - Broadcom specific extension of CDC.
  49. * Used on data packets to convey priority across USB.
  50. */
  51. #define BDC_HEADER_LEN 4
  52. #define BDC_PROTO_VER 1 /* Protocol version */
  53. #define BDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */
  54. #define BDC_FLAG_VER_SHIFT 4 /* Protocol version shift */
  55. #define BDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */
  56. #define BDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */
  57. #define BDC_PRIORITY_MASK 0x7
  58. #define BDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */
  59. #define BDC_FLAG2_IF_SHIFT 0
  60. #define BDC_GET_IF_IDX(hdr) \
  61. ((int)((((hdr)->flags2) & BDC_FLAG2_IF_MASK) >> BDC_FLAG2_IF_SHIFT))
  62. #define BDC_SET_IF_IDX(hdr, idx) \
  63. ((hdr)->flags2 = (((hdr)->flags2 & ~BDC_FLAG2_IF_MASK) | \
  64. ((idx) << BDC_FLAG2_IF_SHIFT)))
  65. struct brcmf_proto_bdc_header {
  66. u8 flags;
  67. u8 priority; /* 802.1d Priority, 4:7 flow control info for usb */
  68. u8 flags2;
  69. u8 rssi;
  70. };
  71. #define RETRIES 2 /* # of retries to retrieve matching ioctl response */
  72. #define BUS_HEADER_LEN (16+BRCMF_SDALIGN) /* Must be atleast SDPCM_RESERVE
  73. * (amount of header tha might be added)
  74. * plus any space that might be needed
  75. * for alignment padding.
  76. */
  77. #define ROUND_UP_MARGIN 2048 /* Biggest SDIO block size possible for
  78. * round off at the end of buffer
  79. */
  80. struct brcmf_proto {
  81. u16 reqid;
  82. u8 pending;
  83. u32 lastcmd;
  84. u8 bus_header[BUS_HEADER_LEN];
  85. struct brcmf_proto_cdc_ioctl msg;
  86. unsigned char buf[BRCMF_C_IOCTL_MAXLEN + ROUND_UP_MARGIN];
  87. };
  88. static int brcmf_proto_cdc_msg(struct brcmf_pub *drvr)
  89. {
  90. struct brcmf_proto *prot = drvr->prot;
  91. int len = le32_to_cpu(prot->msg.len) +
  92. sizeof(struct brcmf_proto_cdc_ioctl);
  93. BRCMF_TRACE(("%s: Enter\n", __func__));
  94. /* NOTE : cdc->msg.len holds the desired length of the buffer to be
  95. * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
  96. * is actually sent to the dongle
  97. */
  98. if (len > CDC_MAX_MSG_SIZE)
  99. len = CDC_MAX_MSG_SIZE;
  100. /* Send request */
  101. return brcmf_sdbrcm_bus_txctl(drvr->bus, (unsigned char *)&prot->msg,
  102. len);
  103. }
  104. static int brcmf_proto_cdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len)
  105. {
  106. int ret;
  107. struct brcmf_proto *prot = drvr->prot;
  108. BRCMF_TRACE(("%s: Enter\n", __func__));
  109. do {
  110. ret = brcmf_sdbrcm_bus_rxctl(drvr->bus,
  111. (unsigned char *)&prot->msg,
  112. len + sizeof(struct brcmf_proto_cdc_ioctl));
  113. if (ret < 0)
  114. break;
  115. } while (CDC_IOC_ID(le32_to_cpu(prot->msg.flags)) != id);
  116. return ret;
  117. }
  118. int
  119. brcmf_proto_cdc_query_ioctl(struct brcmf_pub *drvr, int ifidx, uint cmd,
  120. void *buf, uint len)
  121. {
  122. struct brcmf_proto *prot = drvr->prot;
  123. struct brcmf_proto_cdc_ioctl *msg = &prot->msg;
  124. void *info;
  125. int ret = 0, retries = 0;
  126. u32 id, flags = 0;
  127. BRCMF_TRACE(("%s: Enter\n", __func__));
  128. BRCMF_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
  129. /* Respond "bcmerror" and "bcmerrorstr" with local cache */
  130. if (cmd == BRCMF_C_GET_VAR && buf) {
  131. if (!strcmp((char *)buf, "bcmerrorstr")) {
  132. strncpy((char *)buf, "bcm_error",
  133. BCME_STRLEN);
  134. goto done;
  135. } else if (!strcmp((char *)buf, "bcmerror")) {
  136. *(int *)buf = drvr->dongle_error;
  137. goto done;
  138. }
  139. }
  140. memset(msg, 0, sizeof(struct brcmf_proto_cdc_ioctl));
  141. msg->cmd = cpu_to_le32(cmd);
  142. msg->len = cpu_to_le32(len);
  143. msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT);
  144. CDC_SET_IF_IDX(msg, ifidx);
  145. msg->flags = cpu_to_le32(msg->flags);
  146. if (buf)
  147. memcpy(prot->buf, buf, len);
  148. ret = brcmf_proto_cdc_msg(drvr);
  149. if (ret < 0) {
  150. BRCMF_ERROR(("brcmf_proto_cdc_query_ioctl: brcmf_proto_cdc_msg "
  151. "failed w/status %d\n", ret));
  152. goto done;
  153. }
  154. retry:
  155. /* wait for interrupt and get first fragment */
  156. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  157. if (ret < 0)
  158. goto done;
  159. flags = le32_to_cpu(msg->flags);
  160. id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
  161. if ((id < prot->reqid) && (++retries < RETRIES))
  162. goto retry;
  163. if (id != prot->reqid) {
  164. BRCMF_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
  165. brcmf_ifname(drvr, ifidx), __func__, id,
  166. prot->reqid));
  167. ret = -EINVAL;
  168. goto done;
  169. }
  170. /* Check info buffer */
  171. info = (void *)&msg[1];
  172. /* Copy info buffer */
  173. if (buf) {
  174. if (ret < (int)len)
  175. len = ret;
  176. memcpy(buf, info, len);
  177. }
  178. /* Check the ERROR flag */
  179. if (flags & CDCF_IOC_ERROR) {
  180. ret = le32_to_cpu(msg->status);
  181. /* Cache error from dongle */
  182. drvr->dongle_error = ret;
  183. }
  184. done:
  185. return ret;
  186. }
  187. int brcmf_proto_cdc_set_ioctl(struct brcmf_pub *drvr, int ifidx, uint cmd,
  188. void *buf, uint len)
  189. {
  190. struct brcmf_proto *prot = drvr->prot;
  191. struct brcmf_proto_cdc_ioctl *msg = &prot->msg;
  192. int ret = 0;
  193. u32 flags, id;
  194. BRCMF_TRACE(("%s: Enter\n", __func__));
  195. BRCMF_CTL(("%s: cmd %d len %d\n", __func__, cmd, len));
  196. memset(msg, 0, sizeof(struct brcmf_proto_cdc_ioctl));
  197. msg->cmd = cpu_to_le32(cmd);
  198. msg->len = cpu_to_le32(len);
  199. msg->flags = (++prot->reqid << CDCF_IOC_ID_SHIFT) | CDCF_IOC_SET;
  200. CDC_SET_IF_IDX(msg, ifidx);
  201. msg->flags = cpu_to_le32(msg->flags);
  202. if (buf)
  203. memcpy(prot->buf, buf, len);
  204. ret = brcmf_proto_cdc_msg(drvr);
  205. if (ret < 0)
  206. goto done;
  207. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  208. if (ret < 0)
  209. goto done;
  210. flags = le32_to_cpu(msg->flags);
  211. id = (flags & CDCF_IOC_ID_MASK) >> CDCF_IOC_ID_SHIFT;
  212. if (id != prot->reqid) {
  213. BRCMF_ERROR(("%s: %s: unexpected request id %d (expected %d)\n",
  214. brcmf_ifname(drvr, ifidx), __func__, id,
  215. prot->reqid));
  216. ret = -EINVAL;
  217. goto done;
  218. }
  219. /* Check the ERROR flag */
  220. if (flags & CDCF_IOC_ERROR) {
  221. ret = le32_to_cpu(msg->status);
  222. /* Cache error from dongle */
  223. drvr->dongle_error = ret;
  224. }
  225. done:
  226. return ret;
  227. }
  228. int
  229. brcmf_proto_ioctl(struct brcmf_pub *drvr, int ifidx, struct brcmf_ioctl *ioc,
  230. void *buf, int len)
  231. {
  232. struct brcmf_proto *prot = drvr->prot;
  233. int ret = -1;
  234. if (drvr->busstate == BRCMF_BUS_DOWN) {
  235. BRCMF_ERROR(("%s : bus is down. we have nothing to do\n",
  236. __func__));
  237. return ret;
  238. }
  239. brcmf_os_proto_block(drvr);
  240. BRCMF_TRACE(("%s: Enter\n", __func__));
  241. if (len > BRCMF_C_IOCTL_MAXLEN)
  242. goto done;
  243. if (prot->pending == true) {
  244. BRCMF_TRACE(("CDC packet is pending!!!! cmd=0x%x (%lu) "
  245. "lastcmd=0x%x (%lu)\n",
  246. ioc->cmd, (unsigned long)ioc->cmd, prot->lastcmd,
  247. (unsigned long)prot->lastcmd));
  248. if ((ioc->cmd == BRCMF_C_SET_VAR) ||
  249. (ioc->cmd == BRCMF_C_GET_VAR))
  250. BRCMF_TRACE(("iovar cmd=%s\n", (char *)buf));
  251. goto done;
  252. }
  253. prot->pending = true;
  254. prot->lastcmd = ioc->cmd;
  255. if (ioc->set)
  256. ret = brcmf_proto_cdc_set_ioctl(drvr, ifidx, ioc->cmd,
  257. buf, len);
  258. else {
  259. ret = brcmf_proto_cdc_query_ioctl(drvr, ifidx, ioc->cmd,
  260. buf, len);
  261. if (ret > 0)
  262. ioc->used = ret - sizeof(struct brcmf_proto_cdc_ioctl);
  263. }
  264. /* Too many programs assume ioctl() returns 0 on success */
  265. if (ret >= 0)
  266. ret = 0;
  267. else {
  268. struct brcmf_proto_cdc_ioctl *msg = &prot->msg;
  269. /* len == needed when set/query fails from dongle */
  270. ioc->needed = le32_to_cpu(msg->len);
  271. }
  272. /* Intercept the wme_dp ioctl here */
  273. if (!ret && ioc->cmd == BRCMF_C_SET_VAR &&
  274. !strcmp(buf, "wme_dp")) {
  275. int slen, val = 0;
  276. slen = strlen("wme_dp") + 1;
  277. if (len >= (int)(slen + sizeof(int)))
  278. memcpy(&val, (char *)buf + slen, sizeof(int));
  279. drvr->wme_dp = (u8) le32_to_cpu(val);
  280. }
  281. prot->pending = false;
  282. done:
  283. brcmf_os_proto_unblock(drvr);
  284. return ret;
  285. }
  286. #define PKTSUMNEEDED(skb) \
  287. (((struct sk_buff *)(skb))->ip_summed == CHECKSUM_PARTIAL)
  288. #define PKTSETSUMGOOD(skb, x) \
  289. (((struct sk_buff *)(skb))->ip_summed = \
  290. ((x) ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE))
  291. void brcmf_proto_dump(struct brcmf_pub *drvr, struct brcmu_strbuf *strbuf)
  292. {
  293. brcmu_bprintf(strbuf, "Protocol CDC: reqid %d\n", drvr->prot->reqid);
  294. }
  295. void brcmf_proto_hdrpush(struct brcmf_pub *drvr, int ifidx,
  296. struct sk_buff *pktbuf)
  297. {
  298. struct brcmf_proto_bdc_header *h;
  299. BRCMF_TRACE(("%s: Enter\n", __func__));
  300. /* Push BDC header used to convey priority for buses that don't */
  301. skb_push(pktbuf, BDC_HEADER_LEN);
  302. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  303. h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
  304. if (PKTSUMNEEDED(pktbuf))
  305. h->flags |= BDC_FLAG_SUM_NEEDED;
  306. h->priority = (pktbuf->priority & BDC_PRIORITY_MASK);
  307. h->flags2 = 0;
  308. h->rssi = 0;
  309. BDC_SET_IF_IDX(h, ifidx);
  310. }
  311. int brcmf_proto_hdrpull(struct brcmf_pub *drvr, int *ifidx,
  312. struct sk_buff *pktbuf)
  313. {
  314. struct brcmf_proto_bdc_header *h;
  315. BRCMF_TRACE(("%s: Enter\n", __func__));
  316. /* Pop BDC header used to convey priority for buses that don't */
  317. if (pktbuf->len < BDC_HEADER_LEN) {
  318. BRCMF_ERROR(("%s: rx data too short (%d < %d)\n", __func__,
  319. pktbuf->len, BDC_HEADER_LEN));
  320. return -EBADE;
  321. }
  322. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  323. *ifidx = BDC_GET_IF_IDX(h);
  324. if (*ifidx >= BRCMF_MAX_IFS) {
  325. BRCMF_ERROR(("%s: rx data ifnum out of range (%d)\n",
  326. __func__, *ifidx));
  327. return -EBADE;
  328. }
  329. if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
  330. BDC_PROTO_VER) {
  331. BRCMF_ERROR(("%s: non-BDC packet received, flags 0x%x\n",
  332. brcmf_ifname(drvr, *ifidx), h->flags));
  333. return -EBADE;
  334. }
  335. if (h->flags & BDC_FLAG_SUM_GOOD) {
  336. BRCMF_INFO(("%s: BDC packet received with good rx-csum, "
  337. "flags 0x%x\n",
  338. brcmf_ifname(drvr, *ifidx), h->flags));
  339. PKTSETSUMGOOD(pktbuf, true);
  340. }
  341. pktbuf->priority = h->priority & BDC_PRIORITY_MASK;
  342. skb_pull(pktbuf, BDC_HEADER_LEN);
  343. return 0;
  344. }
  345. int brcmf_proto_attach(struct brcmf_pub *drvr)
  346. {
  347. struct brcmf_proto *cdc;
  348. cdc = kzalloc(sizeof(struct brcmf_proto), GFP_ATOMIC);
  349. if (!cdc) {
  350. BRCMF_ERROR(("%s: kmalloc failed\n", __func__));
  351. goto fail;
  352. }
  353. /* ensure that the msg buf directly follows the cdc msg struct */
  354. if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
  355. BRCMF_ERROR(("struct brcmf_proto is not correctly defined\n"));
  356. goto fail;
  357. }
  358. drvr->prot = cdc;
  359. drvr->hdrlen += BDC_HEADER_LEN;
  360. drvr->maxctl = BRCMF_C_IOCTL_MAXLEN +
  361. sizeof(struct brcmf_proto_cdc_ioctl) + ROUND_UP_MARGIN;
  362. return 0;
  363. fail:
  364. kfree(cdc);
  365. return -ENOMEM;
  366. }
  367. /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
  368. void brcmf_proto_detach(struct brcmf_pub *drvr)
  369. {
  370. kfree(drvr->prot);
  371. drvr->prot = NULL;
  372. }
  373. void brcmf_proto_dstats(struct brcmf_pub *drvr)
  374. {
  375. /* No stats from dongle added yet, copy bus stats */
  376. drvr->dstats.tx_packets = drvr->tx_packets;
  377. drvr->dstats.tx_errors = drvr->tx_errors;
  378. drvr->dstats.rx_packets = drvr->rx_packets;
  379. drvr->dstats.rx_errors = drvr->rx_errors;
  380. drvr->dstats.rx_dropped = drvr->rx_dropped;
  381. drvr->dstats.multicast = drvr->rx_multicast;
  382. return;
  383. }
  384. int brcmf_proto_init(struct brcmf_pub *drvr)
  385. {
  386. int ret = 0;
  387. char buf[128];
  388. BRCMF_TRACE(("%s: Enter\n", __func__));
  389. brcmf_os_proto_block(drvr);
  390. /* Get the device MAC address */
  391. strcpy(buf, "cur_etheraddr");
  392. ret = brcmf_proto_cdc_query_ioctl(drvr, 0, BRCMF_C_GET_VAR,
  393. buf, sizeof(buf));
  394. if (ret < 0) {
  395. brcmf_os_proto_unblock(drvr);
  396. return ret;
  397. }
  398. memcpy(drvr->mac, buf, ETH_ALEN);
  399. brcmf_os_proto_unblock(drvr);
  400. ret = brcmf_c_preinit_ioctls(drvr);
  401. /* Always assumes wl for now */
  402. drvr->iswl = true;
  403. return ret;
  404. }
  405. void brcmf_proto_stop(struct brcmf_pub *drvr)
  406. {
  407. /* Nothing to do for CDC */
  408. }