dhd_cdc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <brcmu_utils.h>
  24. #include <brcmu_wifi.h>
  25. #include "dhd.h"
  26. #include "dhd_proto.h"
  27. #include "dhd_bus.h"
  28. #include "dhd_dbg.h"
  29. struct brcmf_proto_cdc_dcmd {
  30. __le32 cmd; /* dongle command value */
  31. __le32 len; /* lower 16: output buflen;
  32. * upper 16: input buflen (excludes header) */
  33. __le32 flags; /* flag defns given below */
  34. __le32 status; /* status code returned from the device */
  35. };
  36. /* Max valid buffer size that can be sent to the dongle */
  37. #define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN)
  38. /* CDC flag definitions */
  39. #define CDC_DCMD_ERROR 0x01 /* 1=cmd failed */
  40. #define CDC_DCMD_SET 0x02 /* 0=get, 1=set cmd */
  41. #define CDC_DCMD_IF_MASK 0xF000 /* I/F index */
  42. #define CDC_DCMD_IF_SHIFT 12
  43. #define CDC_DCMD_ID_MASK 0xFFFF0000 /* id an cmd pairing */
  44. #define CDC_DCMD_ID_SHIFT 16 /* ID Mask shift bits */
  45. #define CDC_DCMD_ID(flags) \
  46. (((flags) & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_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 2 /* 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 data_offset;
  70. };
  71. #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
  72. #define BUS_HEADER_LEN (16+64) /* Must be atleast SDPCM_RESERVE
  73. * (amount of header tha might be added)
  74. * plus any space that might be needed
  75. * for bus alignment padding.
  76. */
  77. #define ROUND_UP_MARGIN 2048 /* Biggest bus block size possible for
  78. * round off at the end of buffer
  79. * Currently is SDIO
  80. */
  81. struct brcmf_proto {
  82. u16 reqid;
  83. u8 pending;
  84. u32 lastcmd;
  85. u8 bus_header[BUS_HEADER_LEN];
  86. struct brcmf_proto_cdc_dcmd msg;
  87. unsigned char buf[BRCMF_DCMD_MAXLEN + ROUND_UP_MARGIN];
  88. };
  89. static int brcmf_proto_cdc_msg(struct brcmf_pub *drvr)
  90. {
  91. struct brcmf_proto *prot = drvr->prot;
  92. int len = le32_to_cpu(prot->msg.len) +
  93. sizeof(struct brcmf_proto_cdc_dcmd);
  94. brcmf_dbg(TRACE, "Enter\n");
  95. /* NOTE : cdc->msg.len holds the desired length of the buffer to be
  96. * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
  97. * is actually sent to the dongle
  98. */
  99. if (len > CDC_MAX_MSG_SIZE)
  100. len = CDC_MAX_MSG_SIZE;
  101. /* Send request */
  102. return brcmf_bus_txctl(drvr->bus_if, (unsigned char *)&prot->msg, 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_dbg(TRACE, "Enter\n");
  109. len += sizeof(struct brcmf_proto_cdc_dcmd);
  110. do {
  111. ret = brcmf_bus_rxctl(drvr->bus_if, (unsigned char *)&prot->msg,
  112. len);
  113. if (ret < 0)
  114. break;
  115. } while (CDC_DCMD_ID(le32_to_cpu(prot->msg.flags)) != id);
  116. return ret;
  117. }
  118. int
  119. brcmf_proto_cdc_query_dcmd(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_dcmd *msg = &prot->msg;
  124. void *info;
  125. int ret = 0, retries = 0;
  126. u32 id, flags;
  127. brcmf_dbg(TRACE, "Enter\n");
  128. brcmf_dbg(CTL, "cmd %d len %d\n", 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_dcmd));
  141. msg->cmd = cpu_to_le32(cmd);
  142. msg->len = cpu_to_le32(len);
  143. flags = (++prot->reqid << CDC_DCMD_ID_SHIFT);
  144. flags = (flags & ~CDC_DCMD_IF_MASK) |
  145. (ifidx << CDC_DCMD_IF_SHIFT);
  146. msg->flags = cpu_to_le32(flags);
  147. if (buf)
  148. memcpy(prot->buf, buf, len);
  149. ret = brcmf_proto_cdc_msg(drvr);
  150. if (ret < 0) {
  151. brcmf_err("brcmf_proto_cdc_msg failed w/status %d\n",
  152. ret);
  153. goto done;
  154. }
  155. retry:
  156. /* wait for interrupt and get first fragment */
  157. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  158. if (ret < 0)
  159. goto done;
  160. flags = le32_to_cpu(msg->flags);
  161. id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
  162. if ((id < prot->reqid) && (++retries < RETRIES))
  163. goto retry;
  164. if (id != prot->reqid) {
  165. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  166. brcmf_ifname(drvr, ifidx), id, 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 & CDC_DCMD_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_dcmd(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_dcmd *msg = &prot->msg;
  192. int ret = 0;
  193. u32 flags, id;
  194. brcmf_dbg(TRACE, "Enter\n");
  195. brcmf_dbg(CTL, "cmd %d len %d\n", cmd, len);
  196. memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd));
  197. msg->cmd = cpu_to_le32(cmd);
  198. msg->len = cpu_to_le32(len);
  199. flags = (++prot->reqid << CDC_DCMD_ID_SHIFT) | CDC_DCMD_SET;
  200. flags = (flags & ~CDC_DCMD_IF_MASK) |
  201. (ifidx << CDC_DCMD_IF_SHIFT);
  202. msg->flags = cpu_to_le32(flags);
  203. if (buf)
  204. memcpy(prot->buf, buf, len);
  205. ret = brcmf_proto_cdc_msg(drvr);
  206. if (ret < 0)
  207. goto done;
  208. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  209. if (ret < 0)
  210. goto done;
  211. flags = le32_to_cpu(msg->flags);
  212. id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
  213. if (id != prot->reqid) {
  214. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  215. brcmf_ifname(drvr, ifidx), id, prot->reqid);
  216. ret = -EINVAL;
  217. goto done;
  218. }
  219. /* Check the ERROR flag */
  220. if (flags & CDC_DCMD_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. static bool pkt_sum_needed(struct sk_buff *skb)
  229. {
  230. return skb->ip_summed == CHECKSUM_PARTIAL;
  231. }
  232. static void pkt_set_sum_good(struct sk_buff *skb, bool x)
  233. {
  234. skb->ip_summed = (x ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
  235. }
  236. void brcmf_proto_hdrpush(struct brcmf_pub *drvr, int ifidx,
  237. struct sk_buff *pktbuf)
  238. {
  239. struct brcmf_proto_bdc_header *h;
  240. brcmf_dbg(TRACE, "Enter\n");
  241. /* Push BDC header used to convey priority for buses that don't */
  242. skb_push(pktbuf, BDC_HEADER_LEN);
  243. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  244. h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
  245. if (pkt_sum_needed(pktbuf))
  246. h->flags |= BDC_FLAG_SUM_NEEDED;
  247. h->priority = (pktbuf->priority & BDC_PRIORITY_MASK);
  248. h->flags2 = 0;
  249. h->data_offset = 0;
  250. BDC_SET_IF_IDX(h, ifidx);
  251. }
  252. int brcmf_proto_hdrpull(struct device *dev, int *ifidx,
  253. struct sk_buff *pktbuf)
  254. {
  255. struct brcmf_proto_bdc_header *h;
  256. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  257. struct brcmf_pub *drvr = bus_if->drvr;
  258. brcmf_dbg(TRACE, "Enter\n");
  259. /* Pop BDC header used to convey priority for buses that don't */
  260. if (pktbuf->len < BDC_HEADER_LEN) {
  261. brcmf_err("rx data too short (%d < %d)\n",
  262. pktbuf->len, BDC_HEADER_LEN);
  263. return -EBADE;
  264. }
  265. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  266. *ifidx = BDC_GET_IF_IDX(h);
  267. if (*ifidx >= BRCMF_MAX_IFS) {
  268. brcmf_err("rx data ifnum out of range (%d)\n", *ifidx);
  269. return -EBADE;
  270. }
  271. if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
  272. BDC_PROTO_VER) {
  273. brcmf_err("%s: non-BDC packet received, flags 0x%x\n",
  274. brcmf_ifname(drvr, *ifidx), h->flags);
  275. return -EBADE;
  276. }
  277. if (h->flags & BDC_FLAG_SUM_GOOD) {
  278. brcmf_dbg(INFO, "%s: BDC packet received with good rx-csum, flags 0x%x\n",
  279. brcmf_ifname(drvr, *ifidx), h->flags);
  280. pkt_set_sum_good(pktbuf, true);
  281. }
  282. pktbuf->priority = h->priority & BDC_PRIORITY_MASK;
  283. skb_pull(pktbuf, BDC_HEADER_LEN);
  284. skb_pull(pktbuf, h->data_offset << 2);
  285. return 0;
  286. }
  287. int brcmf_proto_attach(struct brcmf_pub *drvr)
  288. {
  289. struct brcmf_proto *cdc;
  290. cdc = kzalloc(sizeof(struct brcmf_proto), GFP_ATOMIC);
  291. if (!cdc)
  292. goto fail;
  293. /* ensure that the msg buf directly follows the cdc msg struct */
  294. if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
  295. brcmf_err("struct brcmf_proto is not correctly defined\n");
  296. goto fail;
  297. }
  298. drvr->prot = cdc;
  299. drvr->hdrlen += BDC_HEADER_LEN;
  300. drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
  301. sizeof(struct brcmf_proto_cdc_dcmd) + ROUND_UP_MARGIN;
  302. return 0;
  303. fail:
  304. kfree(cdc);
  305. return -ENOMEM;
  306. }
  307. /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
  308. void brcmf_proto_detach(struct brcmf_pub *drvr)
  309. {
  310. kfree(drvr->prot);
  311. drvr->prot = NULL;
  312. }
  313. void brcmf_proto_stop(struct brcmf_pub *drvr)
  314. {
  315. /* Nothing to do for CDC */
  316. }