dhd_cdc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 <brcmu_utils.h>
  23. #include <brcmu_wifi.h>
  24. #include "dhd.h"
  25. #include "dhd_proto.h"
  26. #include "dhd_bus.h"
  27. #include "dhd_dbg.h"
  28. struct brcmf_proto_cdc_dcmd {
  29. __le32 cmd; /* dongle command value */
  30. __le32 len; /* lower 16: output buflen;
  31. * upper 16: input buflen (excludes header) */
  32. __le32 flags; /* flag defns given below */
  33. __le32 status; /* status code returned from the device */
  34. };
  35. /* Max valid buffer size that can be sent to the dongle */
  36. #define CDC_MAX_MSG_SIZE (ETH_FRAME_LEN+ETH_FCS_LEN)
  37. /* CDC flag definitions */
  38. #define CDC_DCMD_ERROR 0x01 /* 1=cmd failed */
  39. #define CDC_DCMD_SET 0x02 /* 0=get, 1=set cmd */
  40. #define CDC_DCMD_IF_MASK 0xF000 /* I/F index */
  41. #define CDC_DCMD_IF_SHIFT 12
  42. #define CDC_DCMD_ID_MASK 0xFFFF0000 /* id an cmd pairing */
  43. #define CDC_DCMD_ID_SHIFT 16 /* ID Mask shift bits */
  44. #define CDC_DCMD_ID(flags) \
  45. (((flags) & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT)
  46. /*
  47. * BDC header - Broadcom specific extension of CDC.
  48. * Used on data packets to convey priority across USB.
  49. */
  50. #define BDC_HEADER_LEN 4
  51. #define BDC_PROTO_VER 2 /* Protocol version */
  52. #define BDC_FLAG_VER_MASK 0xf0 /* Protocol version mask */
  53. #define BDC_FLAG_VER_SHIFT 4 /* Protocol version shift */
  54. #define BDC_FLAG_SUM_GOOD 0x04 /* Good RX checksums */
  55. #define BDC_FLAG_SUM_NEEDED 0x08 /* Dongle needs to do TX checksums */
  56. #define BDC_PRIORITY_MASK 0x7
  57. #define BDC_FLAG2_IF_MASK 0x0f /* packet rx interface in APSTA */
  58. #define BDC_FLAG2_IF_SHIFT 0
  59. #define BDC_GET_IF_IDX(hdr) \
  60. ((int)((((hdr)->flags2) & BDC_FLAG2_IF_MASK) >> BDC_FLAG2_IF_SHIFT))
  61. #define BDC_SET_IF_IDX(hdr, idx) \
  62. ((hdr)->flags2 = (((hdr)->flags2 & ~BDC_FLAG2_IF_MASK) | \
  63. ((idx) << BDC_FLAG2_IF_SHIFT)))
  64. struct brcmf_proto_bdc_header {
  65. u8 flags;
  66. u8 priority; /* 802.1d Priority, 4:7 flow control info for usb */
  67. u8 flags2;
  68. u8 data_offset;
  69. };
  70. #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
  71. #define BUS_HEADER_LEN (16+64) /* Must be atleast SDPCM_RESERVE
  72. * (amount of header tha might be added)
  73. * plus any space that might be needed
  74. * for bus alignment padding.
  75. */
  76. #define ROUND_UP_MARGIN 2048 /* Biggest bus block size possible for
  77. * round off at the end of buffer
  78. * Currently is SDIO
  79. */
  80. struct brcmf_proto {
  81. u16 reqid;
  82. u8 bus_header[BUS_HEADER_LEN];
  83. struct brcmf_proto_cdc_dcmd msg;
  84. unsigned char buf[BRCMF_DCMD_MAXLEN + ROUND_UP_MARGIN];
  85. };
  86. static int brcmf_proto_cdc_msg(struct brcmf_pub *drvr)
  87. {
  88. struct brcmf_proto *prot = drvr->prot;
  89. int len = le32_to_cpu(prot->msg.len) +
  90. sizeof(struct brcmf_proto_cdc_dcmd);
  91. brcmf_dbg(CDC, "Enter\n");
  92. /* NOTE : cdc->msg.len holds the desired length of the buffer to be
  93. * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
  94. * is actually sent to the dongle
  95. */
  96. if (len > CDC_MAX_MSG_SIZE)
  97. len = CDC_MAX_MSG_SIZE;
  98. /* Send request */
  99. return brcmf_bus_txctl(drvr->bus_if, (unsigned char *)&prot->msg, len);
  100. }
  101. static int brcmf_proto_cdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len)
  102. {
  103. int ret;
  104. struct brcmf_proto *prot = drvr->prot;
  105. brcmf_dbg(CDC, "Enter\n");
  106. len += sizeof(struct brcmf_proto_cdc_dcmd);
  107. do {
  108. ret = brcmf_bus_rxctl(drvr->bus_if, (unsigned char *)&prot->msg,
  109. len);
  110. if (ret < 0)
  111. break;
  112. } while (CDC_DCMD_ID(le32_to_cpu(prot->msg.flags)) != id);
  113. return ret;
  114. }
  115. int
  116. brcmf_proto_cdc_query_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  117. void *buf, uint len)
  118. {
  119. struct brcmf_proto *prot = drvr->prot;
  120. struct brcmf_proto_cdc_dcmd *msg = &prot->msg;
  121. void *info;
  122. int ret = 0, retries = 0;
  123. u32 id, flags;
  124. brcmf_dbg(CDC, "Enter, cmd %d len %d\n", cmd, len);
  125. memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd));
  126. msg->cmd = cpu_to_le32(cmd);
  127. msg->len = cpu_to_le32(len);
  128. flags = (++prot->reqid << CDC_DCMD_ID_SHIFT);
  129. flags = (flags & ~CDC_DCMD_IF_MASK) |
  130. (ifidx << CDC_DCMD_IF_SHIFT);
  131. msg->flags = cpu_to_le32(flags);
  132. if (buf)
  133. memcpy(prot->buf, buf, len);
  134. ret = brcmf_proto_cdc_msg(drvr);
  135. if (ret < 0) {
  136. brcmf_err("brcmf_proto_cdc_msg failed w/status %d\n",
  137. ret);
  138. goto done;
  139. }
  140. retry:
  141. /* wait for interrupt and get first fragment */
  142. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  143. if (ret < 0)
  144. goto done;
  145. flags = le32_to_cpu(msg->flags);
  146. id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
  147. if ((id < prot->reqid) && (++retries < RETRIES))
  148. goto retry;
  149. if (id != prot->reqid) {
  150. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  151. brcmf_ifname(drvr, ifidx), id, prot->reqid);
  152. ret = -EINVAL;
  153. goto done;
  154. }
  155. /* Check info buffer */
  156. info = (void *)&msg[1];
  157. /* Copy info buffer */
  158. if (buf) {
  159. if (ret < (int)len)
  160. len = ret;
  161. memcpy(buf, info, len);
  162. }
  163. /* Check the ERROR flag */
  164. if (flags & CDC_DCMD_ERROR)
  165. ret = le32_to_cpu(msg->status);
  166. done:
  167. return ret;
  168. }
  169. int brcmf_proto_cdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  170. void *buf, uint len)
  171. {
  172. struct brcmf_proto *prot = drvr->prot;
  173. struct brcmf_proto_cdc_dcmd *msg = &prot->msg;
  174. int ret = 0;
  175. u32 flags, id;
  176. brcmf_dbg(CDC, "Enter, cmd %d len %d\n", cmd, len);
  177. memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd));
  178. msg->cmd = cpu_to_le32(cmd);
  179. msg->len = cpu_to_le32(len);
  180. flags = (++prot->reqid << CDC_DCMD_ID_SHIFT) | CDC_DCMD_SET;
  181. flags = (flags & ~CDC_DCMD_IF_MASK) |
  182. (ifidx << CDC_DCMD_IF_SHIFT);
  183. msg->flags = cpu_to_le32(flags);
  184. if (buf)
  185. memcpy(prot->buf, buf, len);
  186. ret = brcmf_proto_cdc_msg(drvr);
  187. if (ret < 0)
  188. goto done;
  189. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  190. if (ret < 0)
  191. goto done;
  192. flags = le32_to_cpu(msg->flags);
  193. id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
  194. if (id != prot->reqid) {
  195. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  196. brcmf_ifname(drvr, ifidx), id, prot->reqid);
  197. ret = -EINVAL;
  198. goto done;
  199. }
  200. /* Check the ERROR flag */
  201. if (flags & CDC_DCMD_ERROR)
  202. ret = le32_to_cpu(msg->status);
  203. done:
  204. return ret;
  205. }
  206. static bool pkt_sum_needed(struct sk_buff *skb)
  207. {
  208. return skb->ip_summed == CHECKSUM_PARTIAL;
  209. }
  210. static void pkt_set_sum_good(struct sk_buff *skb, bool x)
  211. {
  212. skb->ip_summed = (x ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
  213. }
  214. void brcmf_proto_hdrpush(struct brcmf_pub *drvr, int ifidx,
  215. struct sk_buff *pktbuf)
  216. {
  217. struct brcmf_proto_bdc_header *h;
  218. brcmf_dbg(CDC, "Enter\n");
  219. /* Push BDC header used to convey priority for buses that don't */
  220. skb_push(pktbuf, BDC_HEADER_LEN);
  221. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  222. h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
  223. if (pkt_sum_needed(pktbuf))
  224. h->flags |= BDC_FLAG_SUM_NEEDED;
  225. h->priority = (pktbuf->priority & BDC_PRIORITY_MASK);
  226. h->flags2 = 0;
  227. h->data_offset = 0;
  228. BDC_SET_IF_IDX(h, ifidx);
  229. }
  230. int brcmf_proto_hdrpull(struct brcmf_pub *drvr, u8 *ifidx,
  231. struct sk_buff *pktbuf)
  232. {
  233. struct brcmf_proto_bdc_header *h;
  234. brcmf_dbg(CDC, "Enter\n");
  235. /* Pop BDC header used to convey priority for buses that don't */
  236. if (pktbuf->len < BDC_HEADER_LEN) {
  237. brcmf_err("rx data too short (%d < %d)\n",
  238. pktbuf->len, BDC_HEADER_LEN);
  239. return -EBADE;
  240. }
  241. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  242. *ifidx = BDC_GET_IF_IDX(h);
  243. if (*ifidx >= BRCMF_MAX_IFS) {
  244. brcmf_err("rx data ifnum out of range (%d)\n", *ifidx);
  245. return -EBADE;
  246. }
  247. /* The ifidx is the idx to map to matching netdev/ifp. When receiving
  248. * events this is easy because it contains the bssidx which maps
  249. * 1-on-1 to the netdev/ifp. But for data frames the ifidx is rcvd.
  250. * bssidx 1 is used for p2p0 and no data can be received or
  251. * transmitted on it. Therefor bssidx is ifidx + 1 if ifidx > 0
  252. */
  253. if (*ifidx)
  254. (*ifidx)++;
  255. if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
  256. BDC_PROTO_VER) {
  257. brcmf_err("%s: non-BDC packet received, flags 0x%x\n",
  258. brcmf_ifname(drvr, *ifidx), h->flags);
  259. return -EBADE;
  260. }
  261. if (h->flags & BDC_FLAG_SUM_GOOD) {
  262. brcmf_dbg(CDC, "%s: BDC rcv, good checksum, flags 0x%x\n",
  263. brcmf_ifname(drvr, *ifidx), h->flags);
  264. pkt_set_sum_good(pktbuf, true);
  265. }
  266. pktbuf->priority = h->priority & BDC_PRIORITY_MASK;
  267. skb_pull(pktbuf, BDC_HEADER_LEN);
  268. skb_pull(pktbuf, h->data_offset << 2);
  269. if (pktbuf->len == 0)
  270. return -ENODATA;
  271. return 0;
  272. }
  273. int brcmf_proto_attach(struct brcmf_pub *drvr)
  274. {
  275. struct brcmf_proto *cdc;
  276. cdc = kzalloc(sizeof(struct brcmf_proto), GFP_ATOMIC);
  277. if (!cdc)
  278. goto fail;
  279. /* ensure that the msg buf directly follows the cdc msg struct */
  280. if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
  281. brcmf_err("struct brcmf_proto is not correctly defined\n");
  282. goto fail;
  283. }
  284. drvr->prot = cdc;
  285. drvr->hdrlen += BDC_HEADER_LEN;
  286. drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
  287. sizeof(struct brcmf_proto_cdc_dcmd) + ROUND_UP_MARGIN;
  288. return 0;
  289. fail:
  290. kfree(cdc);
  291. return -ENOMEM;
  292. }
  293. /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
  294. void brcmf_proto_detach(struct brcmf_pub *drvr)
  295. {
  296. kfree(drvr->prot);
  297. drvr->prot = NULL;
  298. }
  299. void brcmf_proto_stop(struct brcmf_pub *drvr)
  300. {
  301. /* Nothing to do for CDC */
  302. }