bcmf_netdev.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /****************************************************************************
  2. * drivers/wireless/bcmf_netdev.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  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. #if defined(CONFIG_NET) && defined(CONFIG_IEEE80211_BROADCOM_FULLMAC)
  40. #include <stdint.h>
  41. #include <stdbool.h>
  42. #include <time.h>
  43. #include <string.h>
  44. #include <errno.h>
  45. #include <assert.h>
  46. #include <debug.h>
  47. #include <arpa/inet.h>
  48. #include <nuttx/arch.h>
  49. #include <nuttx/irq.h>
  50. #include <nuttx/wdog.h>
  51. #include <nuttx/wqueue.h>
  52. #include <nuttx/net/arp.h>
  53. #include <nuttx/net/netdev.h>
  54. #include <nuttx/wireless/wireless.h>
  55. #ifdef CONFIG_NET_PKT
  56. # include <nuttx/net/pkt.h>
  57. #endif
  58. #include "bcmf_driver.h"
  59. #include "bcmf_cdc.h"
  60. #include "bcmf_bdc.h"
  61. #include "bcmf_ioctl.h"
  62. /****************************************************************************
  63. * Pre-processor Definitions
  64. ****************************************************************************/
  65. /* If processing is not done at the interrupt level, then work queue support
  66. * is required.
  67. */
  68. #if !defined(CONFIG_SCHED_WORKQUEUE)
  69. # error Work queue support is required in this configuration (CONFIG_SCHED_WORKQUEUE)
  70. #else
  71. /* Use the selected work queue */
  72. # if defined(CONFIG_IEEE80211_BROADCOM_HPWORK)
  73. # define BCMFWORK HPWORK
  74. # elif defined(CONFIG_IEEE80211_BROADCOM_HLWORK)
  75. # define BCMFWORK LPWORK
  76. # else
  77. # error Neither CONFIG_IEEE80211_BROADCOM_HPWORK nor CONFIG_IEEE80211_BROADCOM_HLWORK defined
  78. # endif
  79. #endif
  80. /* CONFIG_IEEE80211_BROADCOM_NINTERFACES determines the number of physical interfaces
  81. * that will be supported.
  82. */
  83. #ifndef CONFIG_IEEE80211_BROADCOM_NINTERFACES
  84. # define CONFIG_IEEE80211_BROADCOM_NINTERFACES 1
  85. #endif
  86. /* TX poll delay = 1 seconds. CLK_TCK is the number of clock ticks per second */
  87. #define BCMF_WDDELAY (1*CLK_TCK)
  88. /* TX timeout = 1 minute */
  89. #define BCMF_TXTIMEOUT (60*CLK_TCK)
  90. /* This is a helper pointer for accessing the contents of the Ethernet header */
  91. #define BUF ((struct eth_hdr_s *)priv->bc_dev.d_buf)
  92. /****************************************************************************
  93. * Private Types
  94. ****************************************************************************/
  95. /****************************************************************************
  96. * Private Data
  97. ****************************************************************************/
  98. /****************************************************************************
  99. * Private Function Prototypes
  100. ****************************************************************************/
  101. /* Common TX logic */
  102. static int bcmf_transmit(FAR struct bcmf_dev_s *priv,
  103. struct bcmf_frame_s *frame);
  104. static void bcmf_receive(FAR struct bcmf_dev_s *priv);
  105. static int bcmf_txpoll(FAR struct net_driver_s *dev);
  106. static void bcmf_rxpoll(FAR void *arg);
  107. /* Watchdog timer expirations */
  108. static void bcmf_poll_work(FAR void *arg);
  109. static void bcmf_poll_expiry(int argc, wdparm_t arg, ...);
  110. /* NuttX callback functions */
  111. static int bcmf_ifup(FAR struct net_driver_s *dev);
  112. static int bcmf_ifdown(FAR struct net_driver_s *dev);
  113. static void bcmf_txavail_work(FAR void *arg);
  114. static int bcmf_txavail(FAR struct net_driver_s *dev);
  115. #if defined(CONFIG_NET_IGMP) || defined(CONFIG_NET_ICMPv6)
  116. static int bcmf_addmac(FAR struct net_driver_s *dev,
  117. FAR const uint8_t *mac);
  118. #ifdef CONFIG_NET_IGMP
  119. static int bcmf_rmmac(FAR struct net_driver_s *dev,
  120. FAR const uint8_t *mac);
  121. #endif
  122. #ifdef CONFIG_NET_ICMPv6
  123. static void bcmf_ipv6multicast(FAR struct bcmf_dev_s *priv);
  124. #endif
  125. #endif
  126. #ifdef CONFIG_NETDEV_IOCTL
  127. static int bcmf_ioctl(FAR struct net_driver_s *dev, int cmd,
  128. unsigned long arg);
  129. #endif
  130. /****************************************************************************
  131. * Private Functions
  132. ****************************************************************************/
  133. int bcmf_netdev_alloc_tx_frame(FAR struct bcmf_dev_s *priv)
  134. {
  135. if (priv->cur_tx_frame != NULL)
  136. {
  137. /* Frame available */
  138. return OK;
  139. }
  140. /* Allocate frame for TX */
  141. priv->cur_tx_frame = bcmf_bdc_allocate_frame(priv, MAX_NET_DEV_MTU, true);
  142. if (!priv->cur_tx_frame)
  143. {
  144. wlerr("ERROR: Cannot allocate TX frame\n");
  145. return -ENOMEM;
  146. }
  147. return OK;
  148. }
  149. /****************************************************************************
  150. * Name: bcmf_transmit
  151. *
  152. * Description:
  153. * Start hardware transmission. Called either from the txdone interrupt
  154. * handling or from watchdog based polling.
  155. *
  156. * Parameters:
  157. * priv - Reference to the driver state structure
  158. *
  159. * Returned Value:
  160. * OK on success; a negated errno on failure
  161. *
  162. * Assumptions:
  163. * May or may not be called from an interrupt handler. In either case,
  164. * the network is locked.
  165. *
  166. ****************************************************************************/
  167. static int bcmf_transmit(FAR struct bcmf_dev_s *priv,
  168. struct bcmf_frame_s *frame)
  169. {
  170. int ret;
  171. frame->len = priv->bc_dev.d_len +
  172. (unsigned int)(frame->data - frame->base);
  173. ret = bcmf_bdc_transmit_frame(priv, frame);
  174. if (ret)
  175. {
  176. wlerr("ERROR: Failed to transmit frame\n");
  177. return -EIO;
  178. }
  179. NETDEV_TXPACKETS(priv->bc_dev);
  180. return OK;
  181. }
  182. /****************************************************************************
  183. * Name: bcmf_receive
  184. *
  185. * Description:
  186. * An interrupt was received indicating the availability of a new RX packet
  187. *
  188. * Parameters:
  189. * priv - Reference to the driver state structure
  190. *
  191. * Returned Value:
  192. * None
  193. *
  194. * Assumptions:
  195. * The network is locked.
  196. *
  197. ****************************************************************************/
  198. static void bcmf_receive(FAR struct bcmf_dev_s *priv)
  199. {
  200. struct bcmf_frame_s *frame;
  201. // wlinfo("Entry\n");
  202. do
  203. {
  204. /* Request frame buffer from bus interface */
  205. frame = bcmf_bdc_rx_frame(priv);
  206. if (frame == NULL)
  207. {
  208. /* No more frame to process */
  209. break;
  210. }
  211. if (!priv->bc_bifup)
  212. {
  213. /* Interface down, drop frame */
  214. priv->bus->free_frame(priv, frame);
  215. continue;
  216. }
  217. priv->bc_dev.d_buf = frame->data;
  218. priv->bc_dev.d_len = frame->len - (uint32_t)(frame->data - frame->base);
  219. wlinfo("Got frame %p %d\n", frame, priv->bc_dev.d_len);
  220. #ifdef CONFIG_NET_PKT
  221. /* When packet sockets are enabled, feed the frame into the packet tap */
  222. pkt_input(&priv->bc_dev);
  223. #endif
  224. /* We only accept IP packets of the configured type and ARP packets */
  225. #ifdef CONFIG_NET_IPv4
  226. if (BUF->type == HTONS(ETHTYPE_IP))
  227. {
  228. ninfo("IPv4 frame\n");
  229. NETDEV_RXIPV4(&priv->bc_dev);
  230. /* Handle ARP on input then give the IPv4 packet to the network
  231. * layer
  232. */
  233. arp_ipin(&priv->bc_dev);
  234. ipv4_input(&priv->bc_dev);
  235. /* If the above function invocation resulted in data that should be
  236. * sent out on the network, the field d_len will set to a value > 0.
  237. */
  238. if (priv->bc_dev.d_len > 0)
  239. {
  240. /* Update the Ethernet header with the correct MAC address */
  241. #ifdef CONFIG_NET_IPv6
  242. if (IFF_IS_IPv4(priv->bc_dev.d_flags))
  243. #endif
  244. {
  245. arp_out(&priv->bc_dev);
  246. }
  247. #ifdef CONFIG_NET_IPv6
  248. else
  249. {
  250. neighbor_out(&kel->bc_dev);
  251. }
  252. #endif
  253. /* And send the packet */
  254. bcmf_transmit(priv, frame);
  255. }
  256. else
  257. {
  258. /* Release RX frame buffer */
  259. priv->bus->free_frame(priv, frame);
  260. }
  261. }
  262. else
  263. #endif
  264. #ifdef CONFIG_NET_IPv6
  265. if (BUF->type == HTONS(ETHTYPE_IP6))
  266. {
  267. ninfo("Iv6 frame\n");
  268. NETDEV_RXIPV6(&priv->bc_dev);
  269. /* Give the IPv6 packet to the network layer */
  270. ipv6_input(&priv->bc_dev);
  271. /* If the above function invocation resulted in data that should be
  272. * sent out on the network, the field d_len will set to a value > 0.
  273. */
  274. if (priv->bc_dev.d_len > 0)
  275. {
  276. /* Update the Ethernet header with the correct MAC address */
  277. #ifdef CONFIG_NET_IPv4
  278. if (IFF_IS_IPv4(priv->bc_dev.d_flags))
  279. {
  280. arp_out(&priv->bc_dev);
  281. }
  282. else
  283. #endif
  284. #ifdef CONFIG_NET_IPv6
  285. {
  286. neighbor_out(&priv->bc_dev);
  287. }
  288. #endif
  289. /* And send the packet */
  290. bcmf_transmit(priv, frame);
  291. }
  292. else
  293. {
  294. /* Release RX frame buffer */
  295. priv->bus->free_frame(priv, frame);
  296. }
  297. }
  298. else
  299. #endif
  300. #ifdef CONFIG_NET_ARP
  301. if (BUF->type == htons(ETHTYPE_ARP))
  302. {
  303. arp_arpin(&priv->bc_dev);
  304. NETDEV_RXARP(&priv->bc_dev);
  305. /* If the above function invocation resulted in data that should be
  306. * sent out on the network, the field d_len will set to a value > 0.
  307. */
  308. if (priv->bc_dev.d_len > 0)
  309. {
  310. bcmf_transmit(priv, frame);
  311. }
  312. else
  313. {
  314. /* Release RX frame buffer */
  315. priv->bus->free_frame(priv, frame);
  316. }
  317. }
  318. else
  319. #endif
  320. {
  321. wlerr("ERROR: RX dropped\n");
  322. NETDEV_RXDROPPED(&priv->bc_dev);
  323. priv->bus->free_frame(priv, frame);
  324. }
  325. }
  326. while (1); /* While there are more packets to be processed */
  327. }
  328. /****************************************************************************
  329. * Name: bcmf_txpoll
  330. *
  331. * Description:
  332. * The transmitter is available, check if the network has any outgoing
  333. * packets ready to send. This is a callback from devif_poll().
  334. * devif_poll() may be called:
  335. *
  336. * 1. When the preceding TX packet send is complete,
  337. * 2. When the preceding TX packet send timesout and the interface is reset
  338. * 3. During normal TX polling
  339. *
  340. * Parameters:
  341. * dev - Reference to the NuttX driver state structure
  342. *
  343. * Returned Value:
  344. * OK on success; a negated errno on failure
  345. *
  346. * Assumptions:
  347. * May or may not be called from an interrupt handler. In either case,
  348. * the network is locked.
  349. *
  350. ****************************************************************************/
  351. static int bcmf_txpoll(FAR struct net_driver_s *dev)
  352. {
  353. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)dev->d_private;
  354. wlinfo("Entry\n");
  355. /* If the polling resulted in data that should be sent out on the network,
  356. * the field d_len is set to a value > 0.
  357. */
  358. if (priv->bc_dev.d_len > 0)
  359. {
  360. /* Look up the destination MAC address and add it to the Ethernet
  361. * header.
  362. */
  363. #ifdef CONFIG_NET_IPv4
  364. #ifdef CONFIG_NET_IPv6
  365. if (IFF_IS_IPv4(priv->bc_dev.d_flags))
  366. #endif
  367. {
  368. arp_out(&priv->bc_dev);
  369. }
  370. #endif /* CONFIG_NET_IPv4 */
  371. #ifdef CONFIG_NET_IPv6
  372. #ifdef CONFIG_NET_IPv4
  373. else
  374. #endif
  375. {
  376. neighbor_out(&priv->bc_dev);
  377. }
  378. #endif /* CONFIG_NET_IPv6 */
  379. /* Send the packet */
  380. bcmf_transmit(priv, priv->cur_tx_frame);
  381. /* Check if there is room in the device to hold another packet. If not,
  382. * return a non-zero value to terminate the poll.
  383. */
  384. // TODO
  385. priv->cur_tx_frame = NULL;
  386. return 1;
  387. }
  388. /* If zero is returned, the polling will continue until all connections have
  389. * been examined.
  390. */
  391. return 0;
  392. }
  393. /****************************************************************************
  394. * Name: bcmf_rxpoll
  395. *
  396. * Description:
  397. * Process RX frames
  398. *
  399. * Parameters:
  400. * arg - context of device to use
  401. *
  402. * Returned Value:
  403. * OK on success
  404. *
  405. * Assumptions:
  406. * The network is locked.
  407. *
  408. ****************************************************************************/
  409. static void bcmf_rxpoll(FAR void *arg)
  410. {
  411. // wlinfo("Entry\n");
  412. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)arg;
  413. /* Lock the network and serialize driver operations if necessary.
  414. * NOTE: Serialization is only required in the case where the driver work
  415. * is performed on an LP worker thread and where more than one LP worker
  416. * thread has been configured.
  417. */
  418. net_lock();
  419. bcmf_receive(priv);
  420. /* Check if a packet transmission just completed. If so, call bcmf_txdone.
  421. * This may disable further Tx interrupts if there are no pending
  422. * transmissions.
  423. */
  424. // bcmf_txdone(priv);
  425. net_unlock();
  426. }
  427. /****************************************************************************
  428. * Name: bcmf_netdev_notify_tx_done
  429. *
  430. * Description:
  431. * Notify callback called when TX frame is sent and freed.
  432. *
  433. * Assumptions:
  434. *
  435. ****************************************************************************/
  436. void bcmf_netdev_notify_tx_done(FAR struct bcmf_dev_s *priv)
  437. {
  438. // wlinfo("Entry\n");
  439. /* TODO TX buffer may be available, try to send again if needed */
  440. }
  441. /****************************************************************************
  442. * Name: bcmf_netdev_notify_rx
  443. *
  444. * Description:
  445. * Notify callback called when RX frame is available
  446. *
  447. * Assumptions:
  448. *
  449. ****************************************************************************/
  450. void bcmf_netdev_notify_rx(FAR struct bcmf_dev_s *priv)
  451. {
  452. /* Queue a job to process RX frames */
  453. work_queue(BCMFWORK, &priv->bc_pollwork, bcmf_rxpoll, priv, 0);
  454. }
  455. /****************************************************************************
  456. * Name: bcmf_poll_work
  457. *
  458. * Description:
  459. * Perform periodic polling from the worker thread
  460. *
  461. * Parameters:
  462. * arg - The argument passed when work_queue() as called.
  463. *
  464. * Returned Value:
  465. * OK on success
  466. *
  467. * Assumptions:
  468. * The network is locked.
  469. *
  470. ****************************************************************************/
  471. static void bcmf_poll_work(FAR void *arg)
  472. {
  473. // wlinfo("Entry\n");
  474. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)arg;
  475. /* Lock the network and serialize driver operations if necessary.
  476. * NOTE: Serialization is only required in the case where the driver work
  477. * is performed on an LP worker thread and where more than one LP worker
  478. * thread has been configured.
  479. */
  480. net_lock();
  481. /* Perform the poll */
  482. /* Check if there is room in the send another TX packet. We cannot perform
  483. * the TX poll if he are unable to accept another packet for transmission.
  484. */
  485. if (bcmf_netdev_alloc_tx_frame(priv))
  486. {
  487. goto exit_unlock;
  488. }
  489. /* If so, update TCP timing states and poll the network for new XMIT data.
  490. * Hmmm.. might be bug here. Does this mean if there is a transmit in
  491. * progress, we will missing TCP time state updates?
  492. */
  493. priv->bc_dev.d_buf = priv->cur_tx_frame->data;
  494. priv->bc_dev.d_len = 0;
  495. (void)devif_timer(&priv->bc_dev, bcmf_txpoll);
  496. /* Setup the watchdog poll timer again */
  497. (void)wd_start(priv->bc_txpoll, BCMF_WDDELAY, bcmf_poll_expiry, 1,
  498. (wdparm_t)priv);
  499. exit_unlock:
  500. net_unlock();
  501. }
  502. /****************************************************************************
  503. * Name: bcmf_poll_expiry
  504. *
  505. * Description:
  506. * Periodic timer handler. Called from the timer interrupt handler.
  507. *
  508. * Parameters:
  509. * argc - The number of available arguments
  510. * arg - The first argument
  511. *
  512. * Returned Value:
  513. * None
  514. *
  515. * Assumptions:
  516. * Global interrupts are disabled by the watchdog logic.
  517. *
  518. ****************************************************************************/
  519. static void bcmf_poll_expiry(int argc, wdparm_t arg, ...)
  520. {
  521. // wlinfo("Entry\n");
  522. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)arg;
  523. /* Schedule to perform the interrupt processing on the worker thread. */
  524. work_queue(BCMFWORK, &priv->bc_pollwork, bcmf_poll_work, priv, 0);
  525. }
  526. /****************************************************************************
  527. * Name: bcmf_ifup
  528. *
  529. * Description:
  530. * NuttX Callback: Bring up the Ethernet interface when an IP address is
  531. * provided
  532. *
  533. * Parameters:
  534. * dev - Reference to the NuttX driver state structure
  535. *
  536. * Returned Value:
  537. * None
  538. *
  539. * Assumptions:
  540. *
  541. ****************************************************************************/
  542. static int bcmf_ifup(FAR struct net_driver_s *dev)
  543. {
  544. wlinfo("Entry\n");
  545. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)dev->d_private;
  546. #ifdef CONFIG_NET_IPv4
  547. ninfo("Bringing up: %d.%d.%d.%d\n",
  548. dev->d_ipaddr & 0xff, (dev->d_ipaddr >> 8) & 0xff,
  549. (dev->d_ipaddr >> 16) & 0xff, dev->d_ipaddr >> 24);
  550. #endif
  551. #ifdef CONFIG_NET_IPv6
  552. ninfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  553. dev->d_ipv6addr[0], dev->d_ipv6addr[1], dev->d_ipv6addr[2],
  554. dev->d_ipv6addr[3], dev->d_ipv6addr[4], dev->d_ipv6addr[5],
  555. dev->d_ipv6addr[6], dev->d_ipv6addr[7]);
  556. #endif
  557. /* Initialize PHYs, the Ethernet interface, and setup up Ethernet interrupts */
  558. /* Instantiate the MAC address from priv->bc_dev.d_mac.ether.ether_addr_octet */
  559. #ifdef CONFIG_NET_ICMPv6
  560. /* Set up IPv6 multicast address filtering */
  561. bcmf_ipv6multicast(priv);
  562. #endif
  563. /* Set and activate a timer process */
  564. (void)wd_start(priv->bc_txpoll, BCMF_WDDELAY, bcmf_poll_expiry, 1,
  565. (wdparm_t)priv);
  566. /* Enable the hardware interrupt */
  567. priv->bc_bifup = true;
  568. return OK;
  569. }
  570. /****************************************************************************
  571. * Name: bcmf_ifdown
  572. *
  573. * Description:
  574. * NuttX Callback: Stop the interface.
  575. *
  576. * Parameters:
  577. * dev - Reference to the NuttX driver state structure
  578. *
  579. * Returned Value:
  580. * None
  581. *
  582. * Assumptions:
  583. *
  584. ****************************************************************************/
  585. static int bcmf_ifdown(FAR struct net_driver_s *dev)
  586. {
  587. wlinfo("Entry\n");
  588. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)dev->d_private;
  589. irqstate_t flags;
  590. /* Disable the hardware interrupt */
  591. flags = enter_critical_section();
  592. #warning Missing logic
  593. /* Cancel the TX poll timer */
  594. wd_cancel(priv->bc_txpoll);
  595. /* Put the EMAC in its reset, non-operational state. This should be
  596. * a known configuration that will guarantee the bcmf_ifup() always
  597. * successfully brings the interface back up.
  598. */
  599. /* Mark the device "down" */
  600. priv->bc_bifup = false;
  601. leave_critical_section(flags);
  602. return OK;
  603. }
  604. /****************************************************************************
  605. * Name: bcmf_txavail_work
  606. *
  607. * Description:
  608. * Perform an out-of-cycle poll on the worker thread.
  609. *
  610. * Parameters:
  611. * arg - Reference to the NuttX driver state structure (cast to void*)
  612. *
  613. * Returned Value:
  614. * None
  615. *
  616. * Assumptions:
  617. * Called on the higher priority worker thread.
  618. *
  619. ****************************************************************************/
  620. static void bcmf_txavail_work(FAR void *arg)
  621. {
  622. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)arg;
  623. // wlinfo("Entry\n");
  624. /* Lock the network and serialize driver operations if necessary.
  625. * NOTE: Serialization is only required in the case where the driver work
  626. * is performed on an LP worker thread and where more than one LP worker
  627. * thread has been configured.
  628. */
  629. net_lock();
  630. /* Ignore the notification if the interface is not yet up */
  631. if (priv->bc_bifup)
  632. {
  633. /* Check if there is room in the hardware to hold another outgoing packet. */
  634. if (bcmf_netdev_alloc_tx_frame(priv))
  635. {
  636. goto exit_unlock;
  637. }
  638. /* If so, then poll the network for new XMIT data */
  639. priv->bc_dev.d_buf = priv->cur_tx_frame->data;
  640. priv->bc_dev.d_len = 0;
  641. (void)devif_poll(&priv->bc_dev, bcmf_txpoll);
  642. }
  643. exit_unlock:
  644. net_unlock();
  645. }
  646. /****************************************************************************
  647. * Name: bcmf_txavail
  648. *
  649. * Description:
  650. * Driver callback invoked when new TX data is available. This is a
  651. * stimulus perform an out-of-cycle poll and, thereby, reduce the TX
  652. * latency.
  653. *
  654. * Parameters:
  655. * dev - Reference to the NuttX driver state structure
  656. *
  657. * Returned Value:
  658. * None
  659. *
  660. * Assumptions:
  661. * Called in normal user mode
  662. *
  663. ****************************************************************************/
  664. static int bcmf_txavail(FAR struct net_driver_s *dev)
  665. {
  666. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)dev->d_private;
  667. /* Is our single work structure available? It may not be if there are
  668. * pending interrupt actions and we will have to ignore the Tx
  669. * availability action.
  670. */
  671. if (work_available(&priv->bc_pollwork))
  672. {
  673. /* Schedule to serialize the poll on the worker thread. */
  674. work_queue(BCMFWORK, &priv->bc_pollwork, bcmf_txavail_work, priv, 0);
  675. }
  676. return OK;
  677. }
  678. /****************************************************************************
  679. * Name: bcmf_addmac
  680. *
  681. * Description:
  682. * NuttX Callback: Add the specified MAC address to the hardware multicast
  683. * address filtering
  684. *
  685. * Parameters:
  686. * dev - Reference to the NuttX driver state structure
  687. * mac - The MAC address to be added
  688. *
  689. * Returned Value:
  690. * None
  691. *
  692. * Assumptions:
  693. *
  694. ****************************************************************************/
  695. #if defined(CONFIG_NET_IGMP) || defined(CONFIG_NET_ICMPv6)
  696. static int bcmf_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac)
  697. {
  698. wlinfo("Entry\n");
  699. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)dev->d_private;
  700. /* Add the MAC address to the hardware multicast routing table */
  701. return OK;
  702. }
  703. #endif
  704. /****************************************************************************
  705. * Name: bcmf_rmmac
  706. *
  707. * Description:
  708. * NuttX Callback: Remove the specified MAC address from the hardware multicast
  709. * address filtering
  710. *
  711. * Parameters:
  712. * dev - Reference to the NuttX driver state structure
  713. * mac - The MAC address to be removed
  714. *
  715. * Returned Value:
  716. * None
  717. *
  718. * Assumptions:
  719. *
  720. ****************************************************************************/
  721. #ifdef CONFIG_NET_IGMP
  722. static int bcmf_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac)
  723. {
  724. wlinfo("Entry\n");
  725. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)dev->d_private;
  726. /* Add the MAC address to the hardware multicast routing table */
  727. return OK;
  728. }
  729. #endif
  730. /****************************************************************************
  731. * Name: bcmf_ipv6multicast
  732. *
  733. * Description:
  734. * Configure the IPv6 multicast MAC address.
  735. *
  736. * Parameters:
  737. * priv - A reference to the private driver state structure
  738. *
  739. * Returned Value:
  740. * OK on success; Negated errno on failure.
  741. *
  742. * Assumptions:
  743. *
  744. ****************************************************************************/
  745. #ifdef CONFIG_NET_ICMPv6
  746. static void bcmf_ipv6multicast(FAR struct bcmf_dev_s *priv)
  747. {
  748. wlinfo("Entry\n");
  749. FAR struct net_driver_s *dev;
  750. uint16_t tmp16;
  751. uint8_t mac[6];
  752. /* For ICMPv6, we need to add the IPv6 multicast address
  753. *
  754. * For IPv6 multicast addresses, the Ethernet MAC is derived by
  755. * the four low-order octets OR'ed with the MAC 33:33:00:00:00:00,
  756. * so for example the IPv6 address FF02:DEAD:BEEF::1:3 would map
  757. * to the Ethernet MAC address 33:33:00:01:00:03.
  758. *
  759. * NOTES: This appears correct for the ICMPv6 Router Solicitation
  760. * Message, but the ICMPv6 Neighbor Solicitation message seems to
  761. * use 33:33:ff:01:00:03.
  762. */
  763. mac[0] = 0x33;
  764. mac[1] = 0x33;
  765. dev = &priv->dev;
  766. tmp16 = dev->d_ipv6addr[6];
  767. mac[2] = 0xff;
  768. mac[3] = tmp16 >> 8;
  769. tmp16 = dev->d_ipv6addr[7];
  770. mac[4] = tmp16 & 0xff;
  771. mac[5] = tmp16 >> 8;
  772. ninfo("IPv6 Multicast: %02x:%02x:%02x:%02x:%02x:%02x\n",
  773. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  774. (void)bcmf_addmac(dev, mac);
  775. #ifdef CONFIG_NET_ICMPv6_AUTOCONF
  776. /* Add the IPv6 all link-local nodes Ethernet address. This is the
  777. * address that we expect to receive ICMPv6 Router Advertisement
  778. * packets.
  779. */
  780. (void)bcmf_addmac(dev, g_ipv6_ethallnodes.ether_addr_octet);
  781. #endif /* CONFIG_NET_ICMPv6_AUTOCONF */
  782. #ifdef CONFIG_NET_ICMPv6_ROUTER
  783. /* Add the IPv6 all link-local routers Ethernet address. This is the
  784. * address that we expect to receive ICMPv6 Router Solicitation
  785. * packets.
  786. */
  787. (void)bcmf_addmac(dev, g_ipv6_ethallrouters.ether_addr_octet);
  788. #endif /* CONFIG_NET_ICMPv6_ROUTER */
  789. }
  790. #endif /* CONFIG_NET_ICMPv6 */
  791. /****************************************************************************
  792. * Name: bcmf_ioctl
  793. *
  794. * Description:
  795. * Handle network IOCTL commands directed to this device.
  796. *
  797. * Parameters:
  798. * dev - Reference to the NuttX driver state structure
  799. * cmd - The IOCTL command
  800. * arg - The argument for the IOCTL command
  801. *
  802. * Returned Value:
  803. * OK on success; Negated errno on failure.
  804. *
  805. * Assumptions:
  806. *
  807. ****************************************************************************/
  808. #ifdef CONFIG_NETDEV_IOCTL
  809. static int bcmf_ioctl(FAR struct net_driver_s *dev, int cmd,
  810. unsigned long arg)
  811. {
  812. int ret;
  813. FAR struct bcmf_dev_s *priv = (FAR struct bcmf_dev_s *)dev->d_private;
  814. /* Decode and dispatch the driver-specific IOCTL command */
  815. switch (cmd)
  816. {
  817. case SIOCSIWSCAN:
  818. ret = bcmf_wl_start_scan(priv, (struct iwreq *)arg);
  819. break;
  820. case SIOCGIWSCAN:
  821. ret = bcmf_wl_get_scan_results(priv, (struct iwreq *)arg);
  822. break;
  823. case SIOCSIFHWADDR: /* Set device MAC address */
  824. ret = bcmf_wl_set_mac_address(priv, (struct ifreq *)arg);
  825. break;
  826. case SIOCSIWAUTH:
  827. ret = bcmf_wl_set_auth_param(priv, (struct iwreq *)arg);
  828. break;
  829. case SIOCSIWENCODEEXT:
  830. ret = bcmf_wl_set_encode_ext(priv, (struct iwreq *)arg);
  831. break;
  832. case SIOCSIWFREQ: /* Set channel/frequency (Hz) */
  833. wlwarn("WARNING: SIOCSIWFREQ not implemented\n");
  834. ret = -ENOSYS;
  835. break;
  836. case SIOCGIWFREQ: /* Get channel/frequency (Hz) */
  837. wlwarn("WARNING: SIOCGIWFREQ not implemented\n");
  838. ret = -ENOSYS;
  839. break;
  840. case SIOCSIWMODE: /* Set operation mode */
  841. ret = bcmf_wl_set_mode(priv, (struct iwreq *)arg);
  842. break;
  843. case SIOCGIWMODE: /* Get operation mode */
  844. wlwarn("WARNING: SIOCGIWMODE not implemented\n");
  845. ret = -ENOSYS;
  846. break;
  847. case SIOCSIWAP: /* Set access point MAC addresses */
  848. wlwarn("WARNING: SIOCSIWAP not implemented\n");
  849. ret = -ENOSYS;
  850. break;
  851. case SIOCGIWAP: /* Get access point MAC addresses */
  852. wlwarn("WARNING: SIOCGIWAP not implemented\n");
  853. ret = -ENOSYS;
  854. break;
  855. case SIOCSIWESSID: /* Set ESSID (network name) */
  856. ret = bcmf_wl_set_ssid(priv, (struct iwreq *)arg);
  857. break;
  858. case SIOCGIWESSID: /* Get ESSID */
  859. wlwarn("WARNING: SIOCGIWESSID not implemented\n");
  860. ret = -ENOSYS;
  861. break;
  862. case SIOCSIWRATE: /* Set default bit rate (bps) */
  863. wlwarn("WARNING: SIOCSIWRATE not implemented\n");
  864. ret = -ENOSYS;
  865. break;
  866. case SIOCGIWRATE: /* Get default bit rate (bps) */
  867. wlwarn("WARNING: SIOCGIWRATE not implemented\n");
  868. ret = -ENOSYS;
  869. break;
  870. case SIOCSIWTXPOW: /* Set transmit power (dBm) */
  871. wlwarn("WARNING: SIOCSIWTXPOW not implemented\n");
  872. ret = -ENOSYS;
  873. break;
  874. case SIOCGIWTXPOW: /* Get transmit power (dBm) */
  875. wlwarn("WARNING: SIOCGIWTXPOW not implemented\n");
  876. ret = -ENOSYS;
  877. break;
  878. default:
  879. nerr("ERROR: Unrecognized IOCTL command: %d\n", cmd);
  880. ret = -ENOTTY; /* Special return value for this case */
  881. break;
  882. }
  883. return ret;
  884. }
  885. #endif
  886. /****************************************************************************
  887. * Public Functions
  888. ****************************************************************************/
  889. /****************************************************************************
  890. * Name: bcmf_netdev_register
  891. *
  892. * Description:
  893. * Register a network driver and set Broadcom chip in a proper state
  894. *
  895. * Parameters:
  896. * priv - Broadcom driver device
  897. *
  898. * Returned Value:
  899. * OK on success; Negated errno on failure.
  900. *
  901. * Assumptions:
  902. *
  903. ****************************************************************************/
  904. int bcmf_netdev_register(FAR struct bcmf_dev_s *priv)
  905. {
  906. uint32_t out_len;
  907. /* Initialize network driver structure */
  908. memset(&priv->bc_dev, 0, sizeof(priv->bc_dev));
  909. priv->bc_dev.d_ifup = bcmf_ifup; /* I/F up (new IP address) callback */
  910. priv->bc_dev.d_ifdown = bcmf_ifdown; /* I/F down callback */
  911. priv->bc_dev.d_txavail = bcmf_txavail; /* New TX data callback */
  912. #ifdef CONFIG_NET_IGMP
  913. priv->bc_dev.d_addmac = bcmf_addmac; /* Add multicast MAC address */
  914. priv->bc_dev.d_rmmac = bcmf_rmmac; /* Remove multicast MAC address */
  915. #endif
  916. #ifdef CONFIG_NETDEV_IOCTL
  917. priv->bc_dev.d_ioctl = bcmf_ioctl; /* Handle network IOCTL commands */
  918. #endif
  919. priv->bc_dev.d_private = (FAR void *)priv; /* Used to recover private state from dev */
  920. /* Create a watchdog for timing polling */
  921. priv->bc_txpoll = wd_create(); /* Create periodic poll timer */
  922. DEBUGASSERT(priv->bc_txpoll != NULL && priv->bc_txtimeout != NULL);
  923. /* Initialize network stack interface buffer */
  924. priv->cur_tx_frame = NULL;
  925. priv->bc_dev.d_buf = NULL;
  926. /* Put the interface in the down state. This usually amounts to resetting
  927. * the device and/or calling bcmf_ifdown().
  928. */
  929. /* Enable chip */
  930. if (bcmf_wl_enable(priv, true) != OK)
  931. {
  932. return -EIO;
  933. }
  934. /* Query MAC address */
  935. out_len = ETHER_ADDR_LEN;
  936. if (bcmf_cdc_iovar_request(priv, CHIP_STA_INTERFACE, false,
  937. IOVAR_STR_CUR_ETHERADDR,
  938. priv->bc_dev.d_mac.ether.ether_addr_octet,
  939. &out_len) != OK)
  940. {
  941. return -EIO;
  942. }
  943. /* Register the device with the OS so that socket IOCTLs can be performed */
  944. (void)netdev_register(&priv->bc_dev, NET_LL_IEEE80211);
  945. return OK;
  946. }
  947. #endif /* CONFIG_NET && CONFIG_IEEE80211_BROADCOM_FULLMAC */