bcmf_cdc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /****************************************************************************
  2. * drivers/wireless/ieee80211/bcmf_cdc.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Simon Piriou <spiriou31@gmail.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <nuttx/compiler.h>
  40. #include <debug.h>
  41. #include <errno.h>
  42. #include <nuttx/arch.h>
  43. #include <stddef.h>
  44. #include <string.h>
  45. #include <semaphore.h>
  46. #include "bcmf_driver.h"
  47. #include "bcmf_ioctl.h"
  48. #include "bcmf_utils.h"
  49. /****************************************************************************
  50. * Pre-processor Definitions
  51. ****************************************************************************/
  52. /* Control header flags */
  53. #define BCMF_CONTROL_ERROR 0x01 /* Command failure flag */
  54. #define BCMF_CONTROL_SET 0x02 /* Command type: SET = 1, GET = 0 */
  55. #define BCMF_CONTROL_INTERFACE_SHIFT 12
  56. #define BCMF_CONTROL_REQID_SHIFT 16
  57. #define BCMF_CONTROL_TIMEOUT_MS 1000
  58. /****************************************************************************
  59. * Private Types
  60. ****************************************************************************/
  61. struct __attribute__((packed)) bcmf_cdc_header {
  62. uint32_t cmd; /* Command to be sent */
  63. uint32_t len; /* Size of command data */
  64. uint32_t flags; /* cdc request flags, see above */
  65. uint32_t status; /* Returned status code from chip */
  66. };
  67. /****************************************************************************
  68. * Private Function Prototypes
  69. ****************************************************************************/
  70. static struct bcmf_frame_s *bcmf_cdc_allocate_frame(
  71. FAR struct bcmf_dev_s *priv, char *name,
  72. uint8_t *data, uint32_t len);
  73. static int bcmf_cdc_sendframe(FAR struct bcmf_dev_s *priv, uint32_t cmd,
  74. int ifidx, bool set, struct bcmf_frame_s *frame);
  75. static int bcmf_cdc_control_request(FAR struct bcmf_dev_s *priv,
  76. uint32_t ifidx, bool set, uint32_t cmd,
  77. char *name, uint8_t *data, uint32_t *len);
  78. static int bcmf_cdc_control_request_unsafe(FAR struct bcmf_dev_s *priv,
  79. uint32_t ifidx, bool set, uint32_t cmd,
  80. char *name, uint8_t *data, uint32_t *len);
  81. /****************************************************************************
  82. * Private Functions
  83. ****************************************************************************/
  84. struct bcmf_frame_s *bcmf_cdc_allocate_frame(FAR struct bcmf_dev_s *priv,
  85. char *name, uint8_t *data,
  86. uint32_t len)
  87. {
  88. uint32_t data_len;
  89. uint16_t name_len;
  90. struct bcmf_frame_s *frame;
  91. if (name)
  92. {
  93. name_len = strlen(name) + 1;
  94. }
  95. else
  96. {
  97. name_len = 0;
  98. }
  99. if (data)
  100. {
  101. data_len = len;
  102. }
  103. else
  104. {
  105. data_len = 0;
  106. }
  107. /* Allocate control frame */
  108. frame = priv->bus->allocate_frame(priv,
  109. sizeof(struct bcmf_cdc_header) + data_len + name_len,
  110. true, true);
  111. if (!frame)
  112. {
  113. return NULL;
  114. }
  115. /* Copy name string and data */
  116. memcpy(frame->data + sizeof(struct bcmf_cdc_header), name, name_len);
  117. memcpy(frame->data + sizeof(struct bcmf_cdc_header)
  118. + name_len, data, data_len);
  119. return frame;
  120. }
  121. int bcmf_cdc_sendframe(FAR struct bcmf_dev_s *priv, uint32_t cmd,
  122. int ifidx, bool set, struct bcmf_frame_s *frame)
  123. {
  124. struct bcmf_cdc_header *header =
  125. (struct bcmf_cdc_header *)frame->data;
  126. /* Setup control frame header */
  127. uint32_t cdc_data_len = frame->len - (uint32_t)(frame->data-frame->base);
  128. header->cmd = cmd;
  129. header->len = cdc_data_len-sizeof(struct bcmf_cdc_header);
  130. header->status = 0;
  131. header->flags = ++priv->control_reqid << BCMF_CONTROL_REQID_SHIFT;
  132. header->flags |= ifidx << BCMF_CONTROL_INTERFACE_SHIFT;
  133. if (set)
  134. {
  135. header->flags |= BCMF_CONTROL_SET;
  136. }
  137. /* Send frame */
  138. return priv->bus->txframe(priv, frame, true);
  139. }
  140. int bcmf_cdc_control_request(FAR struct bcmf_dev_s *priv,
  141. uint32_t ifidx, bool set, uint32_t cmd,
  142. char *name, uint8_t *data, uint32_t *len)
  143. {
  144. int ret;
  145. /* Take device control mutex */
  146. if ((ret = sem_wait(&priv->control_mutex)) != OK)
  147. {
  148. return ret;
  149. }
  150. ret = bcmf_cdc_control_request_unsafe(priv, ifidx, set, cmd,
  151. name, data, len);
  152. sem_post(&priv->control_mutex);
  153. return ret;
  154. }
  155. int bcmf_cdc_control_request_unsafe(FAR struct bcmf_dev_s *priv,
  156. uint32_t ifidx, bool set, uint32_t cmd,
  157. char *name, uint8_t *data, uint32_t *len)
  158. {
  159. int ret;
  160. struct bcmf_frame_s *frame;
  161. uint32_t out_len = *len;
  162. *len = 0;
  163. /* Prepare control frame */
  164. frame = bcmf_cdc_allocate_frame(priv, name, data, out_len);
  165. if (!frame)
  166. {
  167. wlerr("Cannot allocate cdc frame\n");
  168. return -ENOMEM;
  169. }
  170. /* Setup buffer to store response */
  171. priv->control_rxdata = set ? NULL : data;
  172. priv->control_rxdata_len = out_len;
  173. /* Send control frame. iovar buffer is freed when sent */
  174. ret = bcmf_cdc_sendframe(priv, cmd, ifidx, set, frame);
  175. if (ret != OK)
  176. {
  177. /* Free allocated iovar buffer */
  178. priv->bus->free_frame(priv, frame);
  179. return ret;
  180. }
  181. ret = bcmf_sem_wait(&priv->control_timeout, BCMF_CONTROL_TIMEOUT_MS);
  182. if (ret != OK)
  183. {
  184. wlerr("Error while waiting for control response %d\n", ret);
  185. return ret;
  186. }
  187. *len = priv->control_rxdata_len;
  188. /* Check frame status */
  189. if (priv->control_status != 0)
  190. {
  191. wlerr("Invalid cdc status 0x%x\n", priv->control_status);
  192. return -EINVAL;
  193. }
  194. return OK;
  195. }
  196. /****************************************************************************
  197. * Public Functions
  198. ****************************************************************************/
  199. int bcmf_cdc_iovar_request(FAR struct bcmf_dev_s *priv,
  200. uint32_t ifidx, bool set, char *name,
  201. uint8_t *data, uint32_t *len)
  202. {
  203. return bcmf_cdc_control_request(priv, ifidx, set,
  204. set ? WLC_SET_VAR : WLC_GET_VAR, name,
  205. data, len);
  206. }
  207. int bcmf_cdc_iovar_request_unsafe(FAR struct bcmf_dev_s *priv,
  208. uint32_t ifidx, bool set, char *name,
  209. uint8_t *data, uint32_t *len)
  210. {
  211. return bcmf_cdc_control_request_unsafe(priv, ifidx, set,
  212. set ? WLC_SET_VAR : WLC_GET_VAR, name,
  213. data, len);
  214. }
  215. int bcmf_cdc_ioctl(FAR struct bcmf_dev_s *priv,
  216. uint32_t ifidx, bool set, uint32_t cmd,
  217. uint8_t *data, uint32_t *len)
  218. {
  219. return bcmf_cdc_control_request(priv, ifidx, set, cmd, NULL, data, len);
  220. }
  221. int bcmf_cdc_process_control_frame(FAR struct bcmf_dev_s *priv,
  222. struct bcmf_frame_s *frame)
  223. {
  224. unsigned int data_size;
  225. struct bcmf_cdc_header *cdc_header;
  226. /* Check frame */
  227. data_size = frame->len - (unsigned int)(frame->data - frame->base);
  228. if (data_size < sizeof(struct bcmf_cdc_header))
  229. {
  230. wlerr("Control frame too small\n");
  231. return -EINVAL;
  232. }
  233. cdc_header = (struct bcmf_cdc_header *)frame->data;
  234. if (data_size < cdc_header->len ||
  235. data_size < sizeof(struct bcmf_cdc_header) + cdc_header->len)
  236. {
  237. wlerr("Invalid control frame size\n");
  238. return -EINVAL;
  239. }
  240. // TODO check interface ?
  241. if (cdc_header->flags >> BCMF_CONTROL_REQID_SHIFT == priv->control_reqid)
  242. {
  243. /* Expected frame received, send it back to user */
  244. priv->control_status = cdc_header->status;
  245. if (priv->control_rxdata)
  246. {
  247. if (priv->control_rxdata_len > cdc_header->len)
  248. {
  249. wlerr("Not enough data %d %d\n",
  250. priv->control_rxdata_len, cdc_header->len);
  251. priv->control_rxdata_len = cdc_header->len;
  252. }
  253. memcpy(priv->control_rxdata, (uint8_t *)&cdc_header[1],
  254. priv->control_rxdata_len);
  255. }
  256. sem_post(&priv->control_timeout);
  257. return OK;
  258. }
  259. wlinfo("Got unexpected control frame\n");
  260. return -EINVAL;
  261. }