device.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * File : device.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2007-01-21 Bernard the first version
  13. */
  14. #include <rtthread.h>
  15. #include "kservice.h"
  16. #ifdef RT_USING_DEVICE
  17. /**
  18. * This function registers a device driver with specified name.
  19. *
  20. * @param dev the pointer of device driver structure
  21. * @param name the device driver's name
  22. * @param flags the flag of device
  23. *
  24. * @return the error code, RT_EOK on initialization successfully.
  25. */
  26. rt_err_t rt_device_register(rt_device_t dev, const char* name, rt_uint16_t flags)
  27. {
  28. if (dev == RT_NULL) return -RT_ERROR;
  29. rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
  30. dev->flag = flags;
  31. return RT_EOK;
  32. }
  33. /**
  34. * This function removes a previouly registered device driver
  35. *
  36. * @param dev the pointer of device driver structure
  37. *
  38. * @return the error code, RT_EOK on successfully.
  39. */
  40. rt_err_t rt_device_unregister(rt_device_t dev)
  41. {
  42. RT_ASSERT(dev != RT_NULL);
  43. rt_object_detach(&(dev->parent));
  44. return RT_EOK;
  45. }
  46. /**
  47. * This function initializes all registered device driver
  48. *
  49. * @return the error code, RT_EOK on successfully.
  50. */
  51. rt_err_t rt_device_init_all()
  52. {
  53. struct rt_device* device;
  54. struct rt_list_node* node;
  55. struct rt_object_information *information;
  56. register rt_err_t result;
  57. extern struct rt_object_information rt_object_container[];
  58. information = &rt_object_container[RT_Object_Class_Device];
  59. /* for each device */
  60. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  61. {
  62. rt_err_t (*init)(rt_device_t dev);
  63. device = (struct rt_device*)rt_list_entry(node, struct rt_object, list);
  64. /* get device init handler */
  65. init = device->init;
  66. if (init != RT_NULL && !(device->flag & RT_DEVICE_FLAG_ACTIVATED))
  67. {
  68. result = init(device);
  69. if (result != RT_EOK)
  70. {
  71. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  72. device->parent.name, result);
  73. }
  74. else
  75. {
  76. device->flag |= RT_DEVICE_FLAG_ACTIVATED;
  77. }
  78. }
  79. }
  80. return RT_EOK;
  81. }
  82. /**
  83. * This function finds a device driver by specified name.
  84. *
  85. * @param name the device driver's name
  86. *
  87. * @return the registered device driver on successful, or RT_NULL on failure.
  88. */
  89. rt_device_t rt_device_find(const char* name)
  90. {
  91. /* try to find device object */
  92. return (struct rt_device*) rt_object_find (RT_Object_Class_Device,
  93. name);
  94. }
  95. /**
  96. * This function will open a device
  97. *
  98. * @param dev the pointer of device driver structure
  99. *
  100. * @return the result
  101. */
  102. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  103. {
  104. rt_err_t result;
  105. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  106. RT_ASSERT(dev != RT_NULL);
  107. result = RT_EOK;
  108. /* if device is not initialized, initialize it. */
  109. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  110. {
  111. result = dev->init(dev);
  112. if (result != RT_EOK)
  113. {
  114. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  115. dev->parent.name, result);
  116. return result;
  117. }
  118. else
  119. {
  120. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  121. }
  122. }
  123. /* device is a standalone device and opened */
  124. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
  125. (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  126. return -RT_EBUSY;
  127. /* call device open interface */
  128. open = dev->open;
  129. if (open != RT_NULL)
  130. {
  131. result = open(dev, oflag);
  132. }
  133. else
  134. {
  135. /* no this interface in device driver */
  136. result = -RT_ENOSYS;
  137. }
  138. /* set open flag */
  139. if (result == RT_EOK || result == -RT_ENOSYS)
  140. dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
  141. return result;
  142. }
  143. /**
  144. * This function will close a device
  145. *
  146. * @param dev the pointer of device driver structure
  147. *
  148. * @return the result
  149. */
  150. rt_err_t rt_device_close(rt_device_t dev)
  151. {
  152. rt_err_t result;
  153. rt_err_t (*close)(rt_device_t dev);
  154. RT_ASSERT(dev != RT_NULL);
  155. /* call device close interface */
  156. close = dev->close;
  157. if (close != RT_NULL)
  158. {
  159. result = close(dev);
  160. }
  161. else
  162. {
  163. /* no this interface in device driver */
  164. result = -RT_ENOSYS;
  165. }
  166. /* set open flag */
  167. if (result == RT_EOK || result == -RT_ENOSYS)
  168. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  169. return result;
  170. }
  171. /**
  172. * This function will read some data from a device.
  173. *
  174. * @param dev the pointer of device driver structure
  175. * @param pos the position of reading
  176. * @param buffer the data buffer to save read data
  177. * @param size the size of buffer
  178. *
  179. * @return the actually read size on successful, otherwise negative returned.
  180. */
  181. rt_size_t rt_device_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  182. {
  183. rt_size_t (*read)(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size);
  184. RT_ASSERT(dev != RT_NULL);
  185. /* call device read interface */
  186. read = dev->read;
  187. if (read != RT_NULL)
  188. {
  189. return read(dev, pos, buffer, size);
  190. }
  191. /* set error code */
  192. rt_set_errno(-RT_ENOSYS);
  193. return 0;
  194. }
  195. /**
  196. * This function will write some data to a device.
  197. *
  198. * @param dev the pointer of device driver structure
  199. * @param pos the position of written
  200. * @param buffer the data buffer to be written to device
  201. * @param size the size of buffer
  202. *
  203. * @return the actually written size on successful, otherwise negative returned.
  204. */
  205. rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  206. {
  207. rt_size_t (*write)(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size);
  208. RT_ASSERT(dev != RT_NULL);
  209. /* call device write interface */
  210. write = dev->write;
  211. if (write != RT_NULL)
  212. {
  213. return write(dev, pos, buffer, size);
  214. }
  215. /* set error code */
  216. rt_set_errno(-RT_ENOSYS);
  217. return 0;
  218. }
  219. /**
  220. * This function will perform a variety of control functions on devices.
  221. *
  222. * @param dev the pointer of device driver structure
  223. * @param cmd the command sent to device
  224. * @param arg the argument of command
  225. *
  226. * @return the result
  227. */
  228. rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void* arg)
  229. {
  230. rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void* arg);
  231. RT_ASSERT(dev != RT_NULL);
  232. /* call device write interface */
  233. control = dev->control;
  234. if (control != RT_NULL)
  235. {
  236. return control(dev, cmd, arg);
  237. }
  238. return -RT_ENOSYS;
  239. }
  240. rt_err_t rt_device_set_rx_indicate(rt_device_t dev, rt_err_t (*rx_ind )(rt_device_t dev, rt_size_t size))
  241. {
  242. RT_ASSERT(dev != RT_NULL);
  243. dev->rx_indicate = rx_ind;
  244. return RT_EOK;
  245. }
  246. rt_err_t rt_device_set_tx_complete(rt_device_t dev, rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  247. {
  248. RT_ASSERT(dev != RT_NULL);
  249. dev->tx_complete = tx_done;
  250. return RT_EOK;
  251. }
  252. #endif