wlan.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /****************************************************************************
  2. * This file is part of the WLAN-Ethernut device driver.
  3. *
  4. * Copyright (c) 2004 by Michael Fischer. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the author nor the names of its contributors may
  16. * be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  23. * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. ****************************************************************************
  33. * History:
  34. *
  35. * 27.01.04 mifi First Version
  36. ****************************************************************************/
  37. #define __WLAN_C__
  38. #include <compiler.h>
  39. #include <stdio.h>
  40. #include <stddef.h>
  41. #include <string.h>
  42. #include <sys/heap.h>
  43. #include <sys/timer.h>
  44. #include <netinet/if_ether.h>
  45. #include <net/ether.h>
  46. #include <net/if_var.h>
  47. #include <dev/wlantypes.h>
  48. #include <dev/wlan.h>
  49. #include <dev/wlandrv.h>
  50. /*==========================================================*/
  51. /* DEFINE: All Structures and Common Constants */
  52. /*==========================================================*/
  53. #define ERROR -1
  54. #define OK 0
  55. /*==========================================================*/
  56. /* DEFINE: Prototypes */
  57. /*==========================================================*/
  58. /*==========================================================*/
  59. /* DEFINE: Prototypes */
  60. /*==========================================================*/
  61. /*
  62. * Network interface information structure.
  63. *
  64. * Used to call.
  65. */
  66. static IFNET ifn_eth0 = {
  67. IFT_ETHER, /*!< \brief Interface type. */
  68. 0, /*!< \brief Interface flags, if_flags. */
  69. {0, 0, 0, 0, 0, 0}, /*!< \brief Hardware net address. */
  70. 0, /*!< \brief IP address. */
  71. 0, /*!< \brief Remote IP address for point to point. */
  72. 0, /*!< \brief IP network mask. */
  73. ETHERMTU, /*!< \brief Maximum size of a transmission unit. */
  74. 0, /*!< \brief Packet identifier. */
  75. 0, /*!< \brief Linked list of arp entries. */
  76. 0, /*!< \brief Linked list of multicast address entries, if_mcast. */
  77. NutEtherInput, /*!< \brief Routine to pass received data to, if_recv(). */
  78. WlanOutput, /*!< \brief Driver output routine, if_send(). */
  79. NutEtherOutput, /*!< \brief Media output routine, if_output(). */
  80. NULL /*!< \brief Interface specific control function, if_ioctl(). */
  81. #ifdef NUT_PERFMON
  82. , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  83. #endif
  84. };
  85. /*==========================================================*/
  86. /* DEFINE: Definition of all global Data */
  87. /*==========================================================*/
  88. /*
  89. * Device information structure.
  90. *
  91. * A pointer to this structure must be passed to NutRegisterDevice()
  92. * to bind this Ethernet device driver to the Nut/OS kernel.
  93. * An application may then call NutNetIfConfig() with the name \em eth0
  94. * of this driver to initialize the network interface.
  95. *
  96. */
  97. NUTDEVICE devWlan = {
  98. 0, /* Pointer to next device. */
  99. {'w', 'l', 'a', 'n', '0', 0, 0, 0, 0}, /* Unique device name. */
  100. IFTYP_NET, /* Type of device. */
  101. 0, /* Base address. */
  102. 0, /* First interrupt number. */
  103. &ifn_eth0, /* Interface control block. */
  104. 0, /* Driver control block. */
  105. WlanInit, /* Driver initialization routine. */
  106. WlanIOCtl, /* Driver specific control function. */
  107. 0, /* Read from device. */
  108. 0, /* Write to device. */
  109. 0, /* Write from program space data to device. */
  110. 0, /* Open a device or file. */
  111. 0, /* Close a device or file. */
  112. 0 /* Request file size. */
  113. 0, /* Select function, optional, not yet implemented */
  114. };
  115. /*==========================================================*/
  116. /* DEFINE: Definition of all local Procedures */
  117. /*==========================================================*/
  118. /*==========================================================*/
  119. /* DEFINE: All code exported */
  120. /*==========================================================*/
  121. /************************************************************/
  122. /* WlanInit */
  123. /* */
  124. /* Initialize Ethernet hardware. */
  125. /* */
  126. /* Resets the WLAN Ethernet controller, */
  127. /* initializes all required hardware registers and starts */
  128. /* a background thread for incoming Ethernet traffic. */
  129. /* */
  130. /* Applications should do not directly call this function. */
  131. /* It is automatically executed during device registration */
  132. /* by NutRegisterDevice(). */
  133. /* */
  134. /* In : dev Identifies the device to initialize */
  135. /* Out : none */
  136. /* Return: 0 on success, -1 in case of any errors */
  137. /************************************************************/
  138. int WlanInit(NUTDEVICE * dev)
  139. {
  140. int nError = ERROR;
  141. /*
  142. * Check if this is the first call.
  143. * Only one init allowed.
  144. */
  145. if (dev->dev_dcb == NULL) {
  146. dev->dev_dcb = NutHeapAlloc(sizeof(WI_SOFTC));
  147. /*
  148. * Have we got the memory ?
  149. */
  150. if (dev->dev_dcb != NULL) {
  151. /*
  152. * Clear NICINFO structure.
  153. */
  154. memset(dev->dev_dcb, 0, sizeof(WI_SOFTC));
  155. /*
  156. * Check if a card is available
  157. */
  158. nError = wlandrv_ProbeDevice();
  159. if (nError == OK) {
  160. /*
  161. * Check if we can attach the card
  162. */
  163. if (wlandrv_Attach(dev) != OK) {
  164. nError = ERROR;
  165. } else {
  166. /*
  167. * Now we have all the information,
  168. * and can start the device.
  169. */
  170. wlandrv_Init(dev);
  171. } /* endif wi_attach(dev->dev_dcb) != OK */
  172. } /* HardwareReset() */
  173. } /* endif dev->dev_dcb != NULL */
  174. } /* check first call, dev->dev_dcb == NULL */
  175. return (nError);
  176. }
  177. /************************************************************/
  178. /* WlanIOCtl */
  179. /* */
  180. /* Perform WLAN control functions. */
  181. /* */
  182. /* reg May be set to one of the following constants: */
  183. /* */
  184. /* In : dev Identifies the device to use. */
  185. /* reg Requested control function. */
  186. /* conf Points to a buffer that contains any data */
  187. /* required for the given control function or */
  188. /* receives data from that function. */
  189. /* Out : none */
  190. /* Return: 0 on success, -1 in case of any errors */
  191. /************************************************************/
  192. int WlanIOCtl(NUTDEVICE * dev, int req, void *conf)
  193. {
  194. int nError;
  195. nError = wlandrv_IOCTL(dev, req, conf);
  196. return (nError);
  197. }
  198. /************************************************************/
  199. /* WlanOutput */
  200. /* */
  201. /* Send Ethernet packet. */
  202. /* */
  203. /* In : dev Identifies the device to use. */
  204. /* nb Network buffer structure containing the */
  205. /* packet to be sent. The structure must have */
  206. /* been allocated by a previous call */
  207. /* NutNetBufAlloc(). */
  208. /* Out : none */
  209. /* Return: 0 on success, -1 in case of any errors */
  210. /************************************************************/
  211. int WlanOutput(NUTDEVICE * dev, NETBUF * nb)
  212. {
  213. int nError = ERROR;
  214. nError = wlandrv_PutPacket(dev, nb);
  215. return (nError);
  216. }