device.c 9.8 KB

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