dhd_cdc.c 12 KB

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