device.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /* device is a standalone device and opened */
  109. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
  110. (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  111. return -RT_EBUSY;
  112. /* call device open interface */
  113. open = dev->open;
  114. if (open != RT_NULL)
  115. {
  116. result = open(dev, oflag);
  117. }
  118. else
  119. {
  120. /* no this interface in device driver */
  121. result = -RT_ENOSYS;
  122. }
  123. /* set open flag */
  124. if (result == RT_EOK || result == -RT_ENOSYS)
  125. dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
  126. return result;
  127. }
  128. /**
  129. * This function will close a device
  130. *
  131. * @param dev the pointer of device driver structure
  132. *
  133. * @return the result
  134. */
  135. rt_err_t rt_device_close(rt_device_t dev)
  136. {
  137. rt_err_t result;
  138. rt_err_t (*close)(rt_device_t dev);
  139. RT_ASSERT(dev != RT_NULL);
  140. /* call device close interface */
  141. close = dev->close;
  142. if (close != RT_NULL)
  143. {
  144. result = close(dev);
  145. }
  146. else
  147. {
  148. /* no this interface in device driver */
  149. result = -RT_ENOSYS;
  150. }
  151. /* set open flag */
  152. if (result == RT_EOK || result == -RT_ENOSYS)
  153. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  154. return result;
  155. }
  156. /**
  157. * This function will read some data from a device.
  158. *
  159. * @param dev the pointer of device driver structure
  160. * @param pos the position of reading
  161. * @param buffer the data buffer to save read data
  162. * @param size the size of buffer
  163. *
  164. * @return the actually read size on successful, otherwise negative returned.
  165. */
  166. rt_size_t rt_device_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  167. {
  168. rt_size_t (*read)(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size);
  169. RT_ASSERT(dev != RT_NULL);
  170. /* call device read interface */
  171. read = dev->read;
  172. if (read != RT_NULL)
  173. {
  174. return read(dev, pos, buffer, size);
  175. }
  176. /* set error code */
  177. rt_set_errno(-RT_ENOSYS);
  178. return 0;
  179. }
  180. /**
  181. * This function will write some data to a device.
  182. *
  183. * @param dev the pointer of device driver structure
  184. * @param pos the position of written
  185. * @param buffer the data buffer to be written to device
  186. * @param size the size of buffer
  187. *
  188. * @return the actually written size on successful, otherwise negative returned.
  189. */
  190. rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  191. {
  192. rt_size_t (*write)(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size);
  193. RT_ASSERT(dev != RT_NULL);
  194. /* call device write interface */
  195. write = dev->write;
  196. if (write != RT_NULL)
  197. {
  198. return write(dev, pos, buffer, size);
  199. }
  200. /* set error code */
  201. rt_set_errno(-RT_ENOSYS);
  202. return 0;
  203. }
  204. /**
  205. * This function will perform a variety of control functions on devices.
  206. *
  207. * @param dev the pointer of device driver structure
  208. * @param cmd the command sent to device
  209. * @param arg the argument of command
  210. *
  211. * @return the result
  212. */
  213. rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void* arg)
  214. {
  215. rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void* arg);
  216. RT_ASSERT(dev != RT_NULL);
  217. /* call device write interface */
  218. control = dev->control;
  219. if (control != RT_NULL)
  220. {
  221. return control(dev, cmd, arg);
  222. }
  223. return -RT_ENOSYS;
  224. }
  225. 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))
  226. {
  227. RT_ASSERT(dev != RT_NULL);
  228. dev->rx_indicate = rx_ind;
  229. return RT_EOK;
  230. }
  231. rt_err_t rt_device_set_tx_complete(rt_device_t dev, rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  232. {
  233. RT_ASSERT(dev != RT_NULL);
  234. dev->tx_complete = tx_done;
  235. return RT_EOK;
  236. }
  237. #endif