dhd_cdc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. /*******************************************************************************
  17. * Communicates with the dongle by using dcmd codes.
  18. * For certain dcmd codes, the dongle interprets string data from the host.
  19. ******************************************************************************/
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/types.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/sched.h>
  24. #include <defs.h>
  25. #include <brcmu_utils.h>
  26. #include <brcmu_wifi.h>
  27. #include "dhd.h"
  28. #include "dhd_proto.h"
  29. #include "dhd_bus.h"
  30. #include "dhd_dbg.h"
  31. struct brcmf_proto_cdc_dcmd {
  32. __le32 cmd; /* dongle command value */
  33. __le32 len; /* lower 16: output buflen;
  34. * upper 16: input buflen (excludes header) */
  35. __le32 flags; /* flag defns given below */
  36. __le32 status; /* status code returned from the device */
  37. };
  38. /* Max valid buffer size that can be sent to the dongle */
  39. #define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN)
  40. /* CDC flag definitions */
  41. #define CDC_DCMD_ERROR 0x01 /* 1=cmd failed */
  42. #define CDC_DCMD_SET 0x02 /* 0=get, 1=set cmd */
  43. #define CDC_DCMD_IF_MASK 0xF000 /* I/F index */
  44. #define CDC_DCMD_IF_SHIFT 12
  45. #define CDC_DCMD_ID_MASK 0xFFFF0000 /* id an cmd pairing */
  46. #define CDC_DCMD_ID_SHIFT 16 /* ID Mask shift bits */
  47. #define CDC_DCMD_ID(flags) \
  48. (((flags) & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT)
  49. /*
  50. * BDC header - Broadcom specific extension of CDC.
  51. * Used on data packets to convey priority across USB.
  52. */
  53. #define BDC_HEADER_LEN 4
  54. #define BDC_PROTO_VER 2 /* Protocol version */
  55. #define BDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */
  56. #define BDC_FLAG_VER_SHIFT 4 /* Protocol version shift */
  57. #define BDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */
  58. #define BDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */
  59. #define BDC_PRIORITY_MASK 0x7
  60. #define BDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */
  61. #define BDC_FLAG2_IF_SHIFT 0
  62. #define BDC_GET_IF_IDX(hdr) \
  63. ((int)((((hdr)->flags2) & BDC_FLAG2_IF_MASK) >> BDC_FLAG2_IF_SHIFT))
  64. #define BDC_SET_IF_IDX(hdr, idx) \
  65. ((hdr)->flags2 = (((hdr)->flags2 & ~BDC_FLAG2_IF_MASK) | \
  66. ((idx) << BDC_FLAG2_IF_SHIFT)))
  67. struct brcmf_proto_bdc_header {
  68. u8 flags;
  69. u8 priority; /* 802.1d Priority, 4:7 flow control info for usb */
  70. u8 flags2;
  71. u8 data_offset;
  72. };
  73. #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
  74. #define BUS_HEADER_LEN (16+64) /* Must be atleast SDPCM_RESERVE
  75. * (amount of header tha might be added)
  76. * plus any space that might be needed
  77. * for bus alignment padding.
  78. */
  79. #define ROUND_UP_MARGIN 2048 /* Biggest bus block size possible for
  80. * round off at the end of buffer
  81. * Currently is SDIO
  82. */
  83. struct brcmf_proto {
  84. u16 reqid;
  85. u8 pending;
  86. u32 lastcmd;
  87. u8 bus_header[BUS_HEADER_LEN];
  88. struct brcmf_proto_cdc_dcmd msg;
  89. unsigned char buf[BRCMF_DCMD_MAXLEN + ROUND_UP_MARGIN];
  90. };
  91. static int brcmf_proto_cdc_msg(struct brcmf_pub *drvr)
  92. {
  93. struct brcmf_proto *prot = drvr->prot;
  94. int len = le32_to_cpu(prot->msg.len) +
  95. sizeof(struct brcmf_proto_cdc_dcmd);
  96. brcmf_dbg(TRACE, "Enter\n");
  97. /* NOTE : cdc->msg.len holds the desired length of the buffer to be
  98. * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
  99. * is actually sent to the dongle
  100. */
  101. if (len > CDC_MAX_MSG_SIZE)
  102. len = CDC_MAX_MSG_SIZE;
  103. /* Send request */
  104. return drvr->bus_if->brcmf_bus_txctl(drvr->dev,
  105. (unsigned char *)&prot->msg,
  106. len);
  107. }
  108. static int brcmf_proto_cdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len)
  109. {
  110. int ret;
  111. struct brcmf_proto *prot = drvr->prot;
  112. brcmf_dbg(TRACE, "Enter\n");
  113. do {
  114. ret = drvr->bus_if->brcmf_bus_rxctl(drvr->dev,
  115. (unsigned char *)&prot->msg,
  116. len + sizeof(struct brcmf_proto_cdc_dcmd));
  117. if (ret < 0)
  118. break;
  119. } while (CDC_DCMD_ID(le32_to_cpu(prot->msg.flags)) != id);
  120. return ret;
  121. }
  122. int
  123. brcmf_proto_cdc_query_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  124. void *buf, uint len)
  125. {
  126. struct brcmf_proto *prot = drvr->prot;
  127. struct brcmf_proto_cdc_dcmd *msg = &prot->msg;
  128. void *info;
  129. int ret = 0, retries = 0;
  130. u32 id, flags;
  131. brcmf_dbg(TRACE, "Enter\n");
  132. brcmf_dbg(CTL, "cmd %d len %d\n", cmd, len);
  133. /* Respond "bcmerror" and "bcmerrorstr" with local cache */
  134. if (cmd == BRCMF_C_GET_VAR && buf) {
  135. if (!strcmp((char *)buf, "bcmerrorstr")) {
  136. strncpy((char *)buf, "bcm_error",
  137. BCME_STRLEN);
  138. goto done;
  139. } else if (!strcmp((char *)buf, "bcmerror")) {
  140. *(int *)buf = drvr->dongle_error;
  141. goto done;
  142. }
  143. }
  144. memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd));
  145. msg->cmd = cpu_to_le32(cmd);
  146. msg->len = cpu_to_le32(len);
  147. flags = (++prot->reqid << CDC_DCMD_ID_SHIFT);
  148. flags = (flags & ~CDC_DCMD_IF_MASK) |
  149. (ifidx << CDC_DCMD_IF_SHIFT);
  150. msg->flags = cpu_to_le32(flags);
  151. if (buf)
  152. memcpy(prot->buf, buf, len);
  153. ret = brcmf_proto_cdc_msg(drvr);
  154. if (ret < 0) {
  155. brcmf_dbg(ERROR, "brcmf_proto_cdc_msg failed w/status %d\n",
  156. ret);
  157. goto done;
  158. }
  159. retry:
  160. /* wait for interrupt and get first fragment */
  161. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  162. if (ret < 0)
  163. goto done;
  164. flags = le32_to_cpu(msg->flags);
  165. id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
  166. if ((id < prot->reqid) && (++retries < RETRIES))
  167. goto retry;
  168. if (id != prot->reqid) {
  169. brcmf_dbg(ERROR, "%s: unexpected request id %d (expected %d)\n",
  170. brcmf_ifname(drvr, ifidx), id, prot->reqid);
  171. ret = -EINVAL;
  172. goto done;
  173. }
  174. /* Check info buffer */
  175. info = (void *)&msg[1];
  176. /* Copy info buffer */
  177. if (buf) {
  178. if (ret < (int)len)
  179. len = ret;
  180. memcpy(buf, info, len);
  181. }
  182. /* Check the ERROR flag */
  183. if (flags & CDC_DCMD_ERROR) {
  184. ret = le32_to_cpu(msg->status);
  185. /* Cache error from dongle */
  186. drvr->dongle_error = ret;
  187. }
  188. done:
  189. return ret;
  190. }
  191. int brcmf_proto_cdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  192. void *buf, uint len)
  193. {
  194. struct brcmf_proto *prot = drvr->prot;
  195. struct brcmf_proto_cdc_dcmd *msg = &prot->msg;
  196. int ret = 0;
  197. u32 flags, id;
  198. brcmf_dbg(TRACE, "Enter\n");
  199. brcmf_dbg(CTL, "cmd %d len %d\n", cmd, len);
  200. memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd));
  201. msg->cmd = cpu_to_le32(cmd);
  202. msg->len = cpu_to_le32(len);
  203. flags = (++prot->reqid << CDC_DCMD_ID_SHIFT) | CDC_DCMD_SET;
  204. flags = (flags & ~CDC_DCMD_IF_MASK) |
  205. (ifidx << CDC_DCMD_IF_SHIFT);
  206. msg->flags = cpu_to_le32(flags);
  207. if (buf)
  208. memcpy(prot->buf, buf, len);
  209. ret = brcmf_proto_cdc_msg(drvr);
  210. if (ret < 0)
  211. goto done;
  212. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  213. if (ret < 0)
  214. goto done;
  215. flags = le32_to_cpu(msg->flags);
  216. id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
  217. if (id != prot->reqid) {
  218. brcmf_dbg(ERROR, "%s: unexpected request id %d (expected %d)\n",
  219. brcmf_ifname(drvr, ifidx), id, prot->reqid);
  220. ret = -EINVAL;
  221. goto done;
  222. }
  223. /* Check the ERROR flag */
  224. if (flags & CDC_DCMD_ERROR) {
  225. ret = le32_to_cpu(msg->status);
  226. /* Cache error from dongle */
  227. drvr->dongle_error = ret;
  228. }
  229. done:
  230. return ret;
  231. }
  232. int
  233. brcmf_proto_dcmd(struct brcmf_pub *drvr, int ifidx, struct brcmf_dcmd *dcmd,
  234. int len)
  235. {
  236. struct brcmf_proto *prot = drvr->prot;
  237. int ret = -1;
  238. if (drvr->bus_if->state == BRCMF_BUS_DOWN) {
  239. brcmf_dbg(ERROR, "bus is down. we have nothing to do.\n");
  240. return ret;
  241. }
  242. mutex_lock(&drvr->proto_block);
  243. brcmf_dbg(TRACE, "Enter\n");
  244. if (len > BRCMF_DCMD_MAXLEN)
  245. goto done;
  246. if (prot->pending == true) {
  247. brcmf_dbg(TRACE, "CDC packet is pending!!!! cmd=0x%x (%lu) lastcmd=0x%x (%lu)\n",
  248. dcmd->cmd, (unsigned long)dcmd->cmd, prot->lastcmd,
  249. (unsigned long)prot->lastcmd);
  250. if (dcmd->cmd == BRCMF_C_SET_VAR ||
  251. dcmd->cmd == BRCMF_C_GET_VAR)
  252. brcmf_dbg(TRACE, "iovar cmd=%s\n", (char *)dcmd->buf);
  253. goto done;
  254. }
  255. prot->pending = true;
  256. prot->lastcmd = dcmd->cmd;
  257. if (dcmd->set)
  258. ret = brcmf_proto_cdc_set_dcmd(drvr, ifidx, dcmd->cmd,
  259. dcmd->buf, len);
  260. else {
  261. ret = brcmf_proto_cdc_query_dcmd(drvr, ifidx, dcmd->cmd,
  262. dcmd->buf, len);
  263. if (ret > 0)
  264. dcmd->used = ret -
  265. sizeof(struct brcmf_proto_cdc_dcmd);
  266. }
  267. if (ret >= 0)
  268. ret = 0;
  269. else {
  270. struct brcmf_proto_cdc_dcmd *msg = &prot->msg;
  271. /* len == needed when set/query fails from dongle */
  272. dcmd->needed = le32_to_cpu(msg->len);
  273. }
  274. /* Intercept the wme_dp dongle cmd here */
  275. if (!ret && dcmd->cmd == BRCMF_C_SET_VAR &&
  276. !strcmp(dcmd->buf, "wme_dp")) {
  277. int slen;
  278. __le32 val = 0;
  279. slen = strlen("wme_dp") + 1;
  280. if (len >= (int)(slen + sizeof(int)))
  281. memcpy(&val, (char *)dcmd->buf + slen, sizeof(int));
  282. drvr->wme_dp = (u8) le32_to_cpu(val);
  283. }
  284. prot->pending = false;
  285. done:
  286. mutex_unlock(&drvr->proto_block);
  287. return ret;
  288. }
  289. static bool pkt_sum_needed(struct sk_buff *skb)
  290. {
  291. return skb->ip_summed == CHECKSUM_PARTIAL;
  292. }
  293. static void pkt_set_sum_good(struct sk_buff *skb, bool x)
  294. {
  295. skb->ip_summed = (x ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
  296. }
  297. void brcmf_proto_hdrpush(struct brcmf_pub *drvr, int ifidx,
  298. struct sk_buff *pktbuf)
  299. {
  300. struct brcmf_proto_bdc_header *h;
  301. brcmf_dbg(TRACE, "Enter\n");
  302. /* Push BDC header used to convey priority for buses that don't */
  303. skb_push(pktbuf, BDC_HEADER_LEN);
  304. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  305. h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
  306. if (pkt_sum_needed(pktbuf))
  307. h->flags |= BDC_FLAG_SUM_NEEDED;
  308. h->priority = (pktbuf->priority & BDC_PRIORITY_MASK);
  309. h->flags2 = 0;
  310. h->data_offset = 0;
  311. BDC_SET_IF_IDX(h, ifidx);
  312. }
  313. int brcmf_proto_hdrpull(struct device *dev, int *ifidx,
  314. struct sk_buff *pktbuf)
  315. {
  316. struct brcmf_proto_bdc_header *h;
  317. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  318. struct brcmf_pub *drvr = bus_if->drvr;
  319. brcmf_dbg(TRACE, "Enter\n");
  320. /* Pop BDC header used to convey priority for buses that don't */
  321. if (pktbuf->len < BDC_HEADER_LEN) {
  322. brcmf_dbg(ERROR, "rx data too short (%d < %d)\n",
  323. pktbuf->len, BDC_HEADER_LEN);
  324. return -EBADE;
  325. }
  326. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  327. *ifidx = BDC_GET_IF_IDX(h);
  328. if (*ifidx >= BRCMF_MAX_IFS) {
  329. brcmf_dbg(ERROR, "rx data ifnum out of range (%d)\n", *ifidx);
  330. return -EBADE;
  331. }
  332. if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
  333. BDC_PROTO_VER) {
  334. brcmf_dbg(ERROR, "%s: non-BDC packet received, flags 0x%x\n",
  335. brcmf_ifname(drvr, *ifidx), h->flags);
  336. return -EBADE;
  337. }
  338. if (h->flags & BDC_FLAG_SUM_GOOD) {
  339. brcmf_dbg(INFO, "%s: BDC packet received with good rx-csum, flags 0x%x\n",
  340. brcmf_ifname(drvr, *ifidx), h->flags);
  341. pkt_set_sum_good(pktbuf, true);
  342. }
  343. pktbuf->priority = h->priority & BDC_PRIORITY_MASK;
  344. skb_pull(pktbuf, BDC_HEADER_LEN);
  345. return 0;
  346. }
  347. int brcmf_proto_attach(struct brcmf_pub *drvr)
  348. {
  349. struct brcmf_proto *cdc;
  350. cdc = kzalloc(sizeof(struct brcmf_proto), GFP_ATOMIC);
  351. if (!cdc)
  352. goto fail;
  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_dbg(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->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
  361. sizeof(struct brcmf_proto_cdc_dcmd) + 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. int brcmf_proto_init(struct brcmf_pub *drvr)
  374. {
  375. int ret = 0;
  376. char buf[128];
  377. brcmf_dbg(TRACE, "Enter\n");
  378. mutex_lock(&drvr->proto_block);
  379. /* Get the device MAC address */
  380. strcpy(buf, "cur_etheraddr");
  381. ret = brcmf_proto_cdc_query_dcmd(drvr, 0, BRCMF_C_GET_VAR,
  382. buf, sizeof(buf));
  383. if (ret < 0) {
  384. mutex_unlock(&drvr->proto_block);
  385. return ret;
  386. }
  387. memcpy(drvr->mac, buf, ETH_ALEN);
  388. mutex_unlock(&drvr->proto_block);
  389. ret = brcmf_c_preinit_dcmds(drvr);
  390. /* Always assumes wl for now */
  391. drvr->iswl = true;
  392. return ret;
  393. }
  394. void brcmf_proto_stop(struct brcmf_pub *drvr)
  395. {
  396. /* Nothing to do for CDC */
  397. }