dhd_cdc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. /**
  65. * struct brcmf_proto_bdc_header - BDC header format
  66. *
  67. * @flags: flags contain protocol and checksum info.
  68. * @priority: 802.1d priority and USB flow control info (bit 4:7).
  69. * @flags2: additional flags containing dongle interface index.
  70. * @data_offset: start of packet data. header is following by firmware signals.
  71. */
  72. struct brcmf_proto_bdc_header {
  73. u8 flags;
  74. u8 priority;
  75. u8 flags2;
  76. u8 data_offset;
  77. };
  78. /*
  79. * maximum length of firmware signal data between
  80. * the BDC header and packet data in the tx path.
  81. */
  82. #define BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES 12
  83. #define RETRIES 2 /* # of retries to retrieve matching dcmd response */
  84. #define BUS_HEADER_LEN (16+64) /* Must be atleast SDPCM_RESERVE
  85. * (amount of header tha might be added)
  86. * plus any space that might be needed
  87. * for bus alignment padding.
  88. */
  89. #define ROUND_UP_MARGIN 2048 /* Biggest bus block size possible for
  90. * round off at the end of buffer
  91. * Currently is SDIO
  92. */
  93. struct brcmf_proto {
  94. u16 reqid;
  95. u8 bus_header[BUS_HEADER_LEN];
  96. struct brcmf_proto_cdc_dcmd msg;
  97. unsigned char buf[BRCMF_DCMD_MAXLEN + ROUND_UP_MARGIN];
  98. };
  99. static int brcmf_proto_cdc_msg(struct brcmf_pub *drvr)
  100. {
  101. struct brcmf_proto *prot = drvr->prot;
  102. int len = le32_to_cpu(prot->msg.len) +
  103. sizeof(struct brcmf_proto_cdc_dcmd);
  104. brcmf_dbg(CDC, "Enter\n");
  105. /* NOTE : cdc->msg.len holds the desired length of the buffer to be
  106. * returned. Only up to CDC_MAX_MSG_SIZE of this buffer area
  107. * is actually sent to the dongle
  108. */
  109. if (len > CDC_MAX_MSG_SIZE)
  110. len = CDC_MAX_MSG_SIZE;
  111. /* Send request */
  112. return brcmf_bus_txctl(drvr->bus_if, (unsigned char *)&prot->msg, len);
  113. }
  114. static int brcmf_proto_cdc_cmplt(struct brcmf_pub *drvr, u32 id, u32 len)
  115. {
  116. int ret;
  117. struct brcmf_proto *prot = drvr->prot;
  118. brcmf_dbg(CDC, "Enter\n");
  119. len += sizeof(struct brcmf_proto_cdc_dcmd);
  120. do {
  121. ret = brcmf_bus_rxctl(drvr->bus_if, (unsigned char *)&prot->msg,
  122. len);
  123. if (ret < 0)
  124. break;
  125. } while (CDC_DCMD_ID(le32_to_cpu(prot->msg.flags)) != id);
  126. return ret;
  127. }
  128. int
  129. brcmf_proto_cdc_query_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  130. void *buf, uint len)
  131. {
  132. struct brcmf_proto *prot = drvr->prot;
  133. struct brcmf_proto_cdc_dcmd *msg = &prot->msg;
  134. void *info;
  135. int ret = 0, retries = 0;
  136. u32 id, flags;
  137. brcmf_dbg(CDC, "Enter, cmd %d len %d\n", cmd, len);
  138. memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd));
  139. msg->cmd = cpu_to_le32(cmd);
  140. msg->len = cpu_to_le32(len);
  141. flags = (++prot->reqid << CDC_DCMD_ID_SHIFT);
  142. flags = (flags & ~CDC_DCMD_IF_MASK) |
  143. (ifidx << CDC_DCMD_IF_SHIFT);
  144. msg->flags = cpu_to_le32(flags);
  145. if (buf)
  146. memcpy(prot->buf, buf, len);
  147. ret = brcmf_proto_cdc_msg(drvr);
  148. if (ret < 0) {
  149. brcmf_err("brcmf_proto_cdc_msg failed w/status %d\n",
  150. ret);
  151. goto done;
  152. }
  153. retry:
  154. /* wait for interrupt and get first fragment */
  155. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  156. if (ret < 0)
  157. goto done;
  158. flags = le32_to_cpu(msg->flags);
  159. id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
  160. if ((id < prot->reqid) && (++retries < RETRIES))
  161. goto retry;
  162. if (id != prot->reqid) {
  163. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  164. brcmf_ifname(drvr, ifidx), id, prot->reqid);
  165. ret = -EINVAL;
  166. goto done;
  167. }
  168. /* Check info buffer */
  169. info = (void *)&msg[1];
  170. /* Copy info buffer */
  171. if (buf) {
  172. if (ret < (int)len)
  173. len = ret;
  174. memcpy(buf, info, len);
  175. }
  176. /* Check the ERROR flag */
  177. if (flags & CDC_DCMD_ERROR)
  178. ret = le32_to_cpu(msg->status);
  179. done:
  180. return ret;
  181. }
  182. int brcmf_proto_cdc_set_dcmd(struct brcmf_pub *drvr, int ifidx, uint cmd,
  183. void *buf, uint len)
  184. {
  185. struct brcmf_proto *prot = drvr->prot;
  186. struct brcmf_proto_cdc_dcmd *msg = &prot->msg;
  187. int ret = 0;
  188. u32 flags, id;
  189. brcmf_dbg(CDC, "Enter, cmd %d len %d\n", cmd, len);
  190. memset(msg, 0, sizeof(struct brcmf_proto_cdc_dcmd));
  191. msg->cmd = cpu_to_le32(cmd);
  192. msg->len = cpu_to_le32(len);
  193. flags = (++prot->reqid << CDC_DCMD_ID_SHIFT) | CDC_DCMD_SET;
  194. flags = (flags & ~CDC_DCMD_IF_MASK) |
  195. (ifidx << CDC_DCMD_IF_SHIFT);
  196. msg->flags = cpu_to_le32(flags);
  197. if (buf)
  198. memcpy(prot->buf, buf, len);
  199. ret = brcmf_proto_cdc_msg(drvr);
  200. if (ret < 0)
  201. goto done;
  202. ret = brcmf_proto_cdc_cmplt(drvr, prot->reqid, len);
  203. if (ret < 0)
  204. goto done;
  205. flags = le32_to_cpu(msg->flags);
  206. id = (flags & CDC_DCMD_ID_MASK) >> CDC_DCMD_ID_SHIFT;
  207. if (id != prot->reqid) {
  208. brcmf_err("%s: unexpected request id %d (expected %d)\n",
  209. brcmf_ifname(drvr, ifidx), id, prot->reqid);
  210. ret = -EINVAL;
  211. goto done;
  212. }
  213. /* Check the ERROR flag */
  214. if (flags & CDC_DCMD_ERROR)
  215. ret = le32_to_cpu(msg->status);
  216. done:
  217. return ret;
  218. }
  219. static bool pkt_sum_needed(struct sk_buff *skb)
  220. {
  221. return skb->ip_summed == CHECKSUM_PARTIAL;
  222. }
  223. static void pkt_set_sum_good(struct sk_buff *skb, bool x)
  224. {
  225. skb->ip_summed = (x ? CHECKSUM_UNNECESSARY : CHECKSUM_NONE);
  226. }
  227. void brcmf_proto_hdrpush(struct brcmf_pub *drvr, int ifidx,
  228. struct sk_buff *pktbuf)
  229. {
  230. struct brcmf_proto_bdc_header *h;
  231. brcmf_dbg(CDC, "Enter\n");
  232. /* Push BDC header used to convey priority for buses that don't */
  233. skb_push(pktbuf, BDC_HEADER_LEN);
  234. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  235. h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
  236. if (pkt_sum_needed(pktbuf))
  237. h->flags |= BDC_FLAG_SUM_NEEDED;
  238. h->priority = (pktbuf->priority & BDC_PRIORITY_MASK);
  239. h->flags2 = 0;
  240. h->data_offset = 0;
  241. BDC_SET_IF_IDX(h, ifidx);
  242. }
  243. int brcmf_proto_hdrpull(struct brcmf_pub *drvr, u8 *ifidx,
  244. struct sk_buff *pktbuf)
  245. {
  246. struct brcmf_proto_bdc_header *h;
  247. brcmf_dbg(CDC, "Enter\n");
  248. /* Pop BDC header used to convey priority for buses that don't */
  249. if (pktbuf->len < BDC_HEADER_LEN) {
  250. brcmf_err("rx data too short (%d < %d)\n",
  251. pktbuf->len, BDC_HEADER_LEN);
  252. return -EBADE;
  253. }
  254. h = (struct brcmf_proto_bdc_header *)(pktbuf->data);
  255. *ifidx = BDC_GET_IF_IDX(h);
  256. if (*ifidx >= BRCMF_MAX_IFS) {
  257. brcmf_err("rx data ifnum out of range (%d)\n", *ifidx);
  258. return -EBADE;
  259. }
  260. /* The ifidx is the idx to map to matching netdev/ifp. When receiving
  261. * events this is easy because it contains the bssidx which maps
  262. * 1-on-1 to the netdev/ifp. But for data frames the ifidx is rcvd.
  263. * bssidx 1 is used for p2p0 and no data can be received or
  264. * transmitted on it. Therefor bssidx is ifidx + 1 if ifidx > 0
  265. */
  266. if (*ifidx)
  267. (*ifidx)++;
  268. if (((h->flags & BDC_FLAG_VER_MASK) >> BDC_FLAG_VER_SHIFT) !=
  269. BDC_PROTO_VER) {
  270. brcmf_err("%s: non-BDC packet received, flags 0x%x\n",
  271. brcmf_ifname(drvr, *ifidx), h->flags);
  272. return -EBADE;
  273. }
  274. if (h->flags & BDC_FLAG_SUM_GOOD) {
  275. brcmf_dbg(CDC, "%s: BDC rcv, good checksum, flags 0x%x\n",
  276. brcmf_ifname(drvr, *ifidx), h->flags);
  277. pkt_set_sum_good(pktbuf, true);
  278. }
  279. pktbuf->priority = h->priority & BDC_PRIORITY_MASK;
  280. skb_pull(pktbuf, BDC_HEADER_LEN);
  281. skb_pull(pktbuf, h->data_offset << 2);
  282. if (pktbuf->len == 0)
  283. return -ENODATA;
  284. return 0;
  285. }
  286. int brcmf_proto_attach(struct brcmf_pub *drvr)
  287. {
  288. struct brcmf_proto *cdc;
  289. cdc = kzalloc(sizeof(struct brcmf_proto), GFP_ATOMIC);
  290. if (!cdc)
  291. goto fail;
  292. /* ensure that the msg buf directly follows the cdc msg struct */
  293. if ((unsigned long)(&cdc->msg + 1) != (unsigned long)cdc->buf) {
  294. brcmf_err("struct brcmf_proto is not correctly defined\n");
  295. goto fail;
  296. }
  297. drvr->prot = cdc;
  298. drvr->hdrlen += BDC_HEADER_LEN + BRCMF_PROT_FW_SIGNAL_MAX_TXBYTES;
  299. drvr->bus_if->maxctl = BRCMF_DCMD_MAXLEN +
  300. sizeof(struct brcmf_proto_cdc_dcmd) + ROUND_UP_MARGIN;
  301. return 0;
  302. fail:
  303. kfree(cdc);
  304. return -ENOMEM;
  305. }
  306. /* ~NOTE~ What if another thread is waiting on the semaphore? Holding it? */
  307. void brcmf_proto_detach(struct brcmf_pub *drvr)
  308. {
  309. kfree(drvr->prot);
  310. drvr->prot = NULL;
  311. }
  312. void brcmf_proto_stop(struct brcmf_pub *drvr)
  313. {
  314. /* Nothing to do for CDC */
  315. }