device.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * File : device.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, 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://openlab.rt-thread.com/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("init device:%s error:\n", device->parent.name);
  72. rt_kprintf("error code:%d\n", result);
  73. }
  74. }
  75. }
  76. return RT_EOK;
  77. }
  78. /**
  79. * This function finds a device driver by specified name.
  80. *
  81. * @param name the device driver's name
  82. *
  83. * @return the registered device driver on successful, or RT_NULL on failure.
  84. */
  85. rt_device_t rt_device_find(const char* name)
  86. {
  87. /* try to find device object */
  88. return (struct rt_device*) rt_object_find (RT_Object_Class_Device,
  89. name);
  90. }
  91. /**
  92. * This function will open a device
  93. *
  94. * @param dev the pointer of device driver structure
  95. *
  96. * @return the result
  97. */
  98. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  99. {
  100. rt_err_t result;
  101. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  102. RT_ASSERT(dev != RT_NULL);
  103. result = RT_EOK;
  104. /* device is a standalone device and opened */
  105. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
  106. (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  107. return -RT_EBUSY;
  108. /* call device open interface */
  109. open = dev->open;
  110. if (open != RT_NULL)
  111. {
  112. result = open(dev, oflag);
  113. }
  114. else
  115. {
  116. /* no this interface in device driver */
  117. result = -RT_ENOSYS;
  118. }
  119. /* set open flag */
  120. if (result == RT_EOK || result == -RT_ENOSYS)
  121. dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
  122. return result;
  123. }
  124. /**
  125. * This function will close a device
  126. *
  127. * @param dev the pointer of device driver structure
  128. *
  129. * @return the result
  130. */
  131. rt_err_t rt_device_close(rt_device_t dev)
  132. {
  133. rt_err_t result;
  134. rt_err_t (*close)(rt_device_t dev);
  135. RT_ASSERT(dev != RT_NULL);
  136. /* call device close interface */
  137. close = dev->close;
  138. if (close != RT_NULL)
  139. {
  140. result = close(dev);
  141. }
  142. else
  143. {
  144. /* no this interface in device driver */
  145. result = -RT_ENOSYS;
  146. }
  147. /* set open flag */
  148. if (result == RT_EOK || result == -RT_ENOSYS)
  149. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  150. return result;
  151. }
  152. /**
  153. * This function will read some data from a device.
  154. *
  155. * @param dev the pointer of device driver structure
  156. * @param pos the position of reading
  157. * @param buffer the data buffer to save read data
  158. * @param size the size of buffer
  159. *
  160. * @return the actually read size on successful, otherwise negative returned.
  161. */
  162. rt_size_t rt_device_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  163. {
  164. rt_size_t (*read)(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size);
  165. RT_ASSERT(dev != RT_NULL);
  166. /* call device read interface */
  167. read = dev->read;
  168. if (read != RT_NULL)
  169. {
  170. return read(dev, pos, buffer, size);
  171. }
  172. /* set error code */
  173. rt_set_errno(-RT_ENOSYS);
  174. return 0;
  175. }
  176. /**
  177. * This function will write some data to a device.
  178. *
  179. * @param dev the pointer of device driver structure
  180. * @param pos the position of written
  181. * @param buffer the data buffer to be written to device
  182. * @param size the size of buffer
  183. *
  184. * @return the actually written size on successful, otherwise negative returned.
  185. */
  186. rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  187. {
  188. rt_size_t (*write)(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size);
  189. RT_ASSERT(dev != RT_NULL);
  190. /* call device write interface */
  191. write = dev->write;
  192. if (write != RT_NULL)
  193. {
  194. return write(dev, pos, buffer, size);
  195. }
  196. /* set error code */
  197. rt_set_errno(-RT_ENOSYS);
  198. return 0;
  199. }
  200. /**
  201. * This function will perform a variety of control functions on devices.
  202. *
  203. * @param dev the pointer of device driver structure
  204. * @param cmd the command sent to device
  205. * @param arg the argument of command
  206. *
  207. * @return the result
  208. */
  209. rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void* arg)
  210. {
  211. rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void* arg);
  212. RT_ASSERT(dev != RT_NULL);
  213. /* call device write interface */
  214. control = dev->control;
  215. if (control != RT_NULL)
  216. {
  217. return control(dev, cmd, arg);
  218. }
  219. return -RT_ENOSYS;
  220. }
  221. 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))
  222. {
  223. RT_ASSERT(dev != RT_NULL);
  224. dev->rx_indicate = rx_ind;
  225. return RT_EOK;
  226. }
  227. rt_err_t rt_device_set_tx_complete(rt_device_t dev, rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  228. {
  229. RT_ASSERT(dev != RT_NULL);
  230. dev->tx_complete = tx_done;
  231. return RT_EOK;
  232. }
  233. #endif