device.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. * 2010-05-04 Bernard add rt_device_init implementation
  14. */
  15. #include <rtthread.h>
  16. #include "kservice.h"
  17. #ifdef RT_USING_DEVICE
  18. /**
  19. * This function registers a device driver with specified name.
  20. *
  21. * @param dev the pointer of device driver structure
  22. * @param name the device driver's name
  23. * @param flags the flag of device
  24. *
  25. * @return the error code, RT_EOK on initialization successfully.
  26. */
  27. rt_err_t rt_device_register(rt_device_t dev, const char* name, rt_uint16_t flags)
  28. {
  29. if (dev == RT_NULL) return -RT_ERROR;
  30. rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
  31. dev->flag = flags;
  32. return RT_EOK;
  33. }
  34. /**
  35. * This function removes a previouly registered device driver
  36. *
  37. * @param dev the pointer of device driver structure
  38. *
  39. * @return the error code, RT_EOK on successfully.
  40. */
  41. rt_err_t rt_device_unregister(rt_device_t dev)
  42. {
  43. RT_ASSERT(dev != RT_NULL);
  44. rt_object_detach(&(dev->parent));
  45. return RT_EOK;
  46. }
  47. /**
  48. * This function initializes all registered device driver
  49. *
  50. * @return the error code, RT_EOK on successfully.
  51. */
  52. rt_err_t rt_device_init_all()
  53. {
  54. struct rt_device* device;
  55. struct rt_list_node* node;
  56. struct rt_object_information *information;
  57. register rt_err_t result;
  58. extern struct rt_object_information rt_object_container[];
  59. information = &rt_object_container[RT_Object_Class_Device];
  60. /* for each device */
  61. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  62. {
  63. rt_err_t (*init)(rt_device_t dev);
  64. device = (struct rt_device*)rt_list_entry(node, struct rt_object, list);
  65. /* get device init handler */
  66. init = device->init;
  67. if (init != RT_NULL && !(device->flag & RT_DEVICE_FLAG_ACTIVATED))
  68. {
  69. result = init(device);
  70. if (result != RT_EOK)
  71. {
  72. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  73. device->parent.name, result);
  74. }
  75. else
  76. {
  77. device->flag |= RT_DEVICE_FLAG_ACTIVATED;
  78. }
  79. }
  80. }
  81. return RT_EOK;
  82. }
  83. /**
  84. * This function finds a device driver by specified name.
  85. *
  86. * @param name the device driver's name
  87. *
  88. * @return the registered device driver on successful, or RT_NULL on failure.
  89. */
  90. rt_device_t rt_device_find(const char* name)
  91. {
  92. struct rt_object* object;
  93. struct rt_list_node* node;
  94. struct rt_object_information *information;
  95. extern struct rt_object_information rt_object_container[];
  96. /* enter critical */
  97. if (rt_thread_self() != RT_NULL)
  98. rt_enter_critical();
  99. /* try to find device object */
  100. information = &rt_object_container[RT_Object_Class_Device];
  101. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  102. {
  103. object = rt_list_entry(node, struct rt_object, list);
  104. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  105. {
  106. /* leave critical */
  107. if (rt_thread_self() != RT_NULL)
  108. rt_exit_critical();
  109. return (rt_device_t)object;
  110. }
  111. }
  112. /* leave critical */
  113. if (rt_thread_self() != RT_NULL)
  114. rt_exit_critical();
  115. /* not found */
  116. return RT_NULL;
  117. }
  118. /**
  119. * This function will initialize the speicial device
  120. *
  121. * @param dev the pointer of device driver structure
  122. *
  123. * @return the result
  124. */
  125. rt_err_t rt_device_init(rt_device_t dev)
  126. {
  127. rt_err_t result;
  128. rt_err_t (*init)(rt_device_t dev);
  129. RT_ASSERT(dev != RT_NULL);
  130. /* get device init handler */
  131. init = dev->init;
  132. if (init != RT_NULL && !(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  133. {
  134. result = init(dev);
  135. if (result != RT_EOK)
  136. {
  137. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  138. dev->parent.name, result);
  139. }
  140. else
  141. {
  142. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  143. }
  144. }
  145. else result = -RT_ENOSYS;
  146. return result;
  147. }
  148. /**
  149. * This function will open a device
  150. *
  151. * @param dev the pointer of device driver structure
  152. *
  153. * @return the result
  154. */
  155. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  156. {
  157. rt_err_t result;
  158. rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag);
  159. RT_ASSERT(dev != RT_NULL);
  160. result = RT_EOK;
  161. /* if device is not initialized, initialize it. */
  162. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  163. {
  164. result = dev->init(dev);
  165. if (result != RT_EOK)
  166. {
  167. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  168. dev->parent.name, result);
  169. return result;
  170. }
  171. else
  172. {
  173. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  174. }
  175. }
  176. /* device is a standalone device and opened */
  177. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
  178. (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  179. return -RT_EBUSY;
  180. /* call device open interface */
  181. open = dev->open;
  182. if (open != RT_NULL)
  183. {
  184. result = open(dev, oflag);
  185. }
  186. else
  187. {
  188. /* no this interface in device driver */
  189. result = -RT_ENOSYS;
  190. }
  191. /* set open flag */
  192. if (result == RT_EOK || result == -RT_ENOSYS)
  193. dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
  194. return result;
  195. }
  196. /**
  197. * This function will close a device
  198. *
  199. * @param dev the pointer of device driver structure
  200. *
  201. * @return the result
  202. */
  203. rt_err_t rt_device_close(rt_device_t dev)
  204. {
  205. rt_err_t result;
  206. rt_err_t (*close)(rt_device_t dev);
  207. RT_ASSERT(dev != RT_NULL);
  208. /* call device close interface */
  209. close = dev->close;
  210. if (close != RT_NULL)
  211. {
  212. result = close(dev);
  213. }
  214. else
  215. {
  216. /* no this interface in device driver */
  217. result = -RT_ENOSYS;
  218. }
  219. /* set open flag */
  220. if (result == RT_EOK || result == -RT_ENOSYS)
  221. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  222. return result;
  223. }
  224. /**
  225. * This function will read some data from a device.
  226. *
  227. * @param dev the pointer of device driver structure
  228. * @param pos the position of reading
  229. * @param buffer the data buffer to save read data
  230. * @param size the size of buffer
  231. *
  232. * @return the actually read size on successful, otherwise negative returned.
  233. */
  234. rt_size_t rt_device_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  235. {
  236. rt_size_t (*read)(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size);
  237. RT_ASSERT(dev != RT_NULL);
  238. /* call device read interface */
  239. read = dev->read;
  240. if (read != RT_NULL)
  241. {
  242. return read(dev, pos, buffer, size);
  243. }
  244. /* set error code */
  245. rt_set_errno(-RT_ENOSYS);
  246. return 0;
  247. }
  248. /**
  249. * This function will write some data to a device.
  250. *
  251. * @param dev the pointer of device driver structure
  252. * @param pos the position of written
  253. * @param buffer the data buffer to be written to device
  254. * @param size the size of buffer
  255. *
  256. * @return the actually written size on successful, otherwise negative returned.
  257. */
  258. rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  259. {
  260. rt_size_t (*write)(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size);
  261. RT_ASSERT(dev != RT_NULL);
  262. /* call device write interface */
  263. write = dev->write;
  264. if (write != RT_NULL)
  265. {
  266. return write(dev, pos, buffer, size);
  267. }
  268. /* set error code */
  269. rt_set_errno(-RT_ENOSYS);
  270. return 0;
  271. }
  272. /**
  273. * This function will perform a variety of control functions on devices.
  274. *
  275. * @param dev the pointer of device driver structure
  276. * @param cmd the command sent to device
  277. * @param arg the argument of command
  278. *
  279. * @return the result
  280. */
  281. rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void* arg)
  282. {
  283. rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void* arg);
  284. RT_ASSERT(dev != RT_NULL);
  285. /* call device write interface */
  286. control = dev->control;
  287. if (control != RT_NULL)
  288. {
  289. return control(dev, cmd, arg);
  290. }
  291. return -RT_ENOSYS;
  292. }
  293. 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))
  294. {
  295. RT_ASSERT(dev != RT_NULL);
  296. dev->rx_indicate = rx_ind;
  297. return RT_EOK;
  298. }
  299. rt_err_t rt_device_set_tx_complete(rt_device_t dev, rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  300. {
  301. RT_ASSERT(dev != RT_NULL);
  302. dev->tx_complete = tx_done;
  303. return RT_EOK;
  304. }
  305. #endif