device.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * File : device.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2007-01-21 Bernard the first version
  23. * 2010-05-04 Bernard add rt_device_init implementation
  24. * 2012-10-20 Bernard add device check in register function,
  25. * provided by Rob <rdent@iinet.net.au>
  26. * 2012-12-25 Bernard return RT_EOK if the device interface not exist.
  27. * 2013-07-09 Grissiom add ref_count support
  28. * 2016-04-02 Bernard fix the open_flag initialization issue.
  29. */
  30. #include <rtthread.h>
  31. #ifdef RT_USING_DEVICE
  32. /**
  33. * This function registers a device driver with specified name.
  34. *
  35. * @param dev the pointer of device driver structure
  36. * @param name the device driver's name
  37. * @param flags the flag of device
  38. *
  39. * @return the error code, RT_EOK on initialization successfully.
  40. */
  41. rt_err_t rt_device_register(rt_device_t dev,
  42. const char *name,
  43. rt_uint16_t flags)
  44. {
  45. if (dev == RT_NULL)
  46. return -RT_ERROR;
  47. if (rt_device_find(name) != RT_NULL)
  48. return -RT_ERROR;
  49. rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
  50. dev->flag = flags;
  51. dev->ref_count = 0;
  52. dev->open_flag = 0;
  53. return RT_EOK;
  54. }
  55. RTM_EXPORT(rt_device_register);
  56. /**
  57. * This function removes a previously registered device driver
  58. *
  59. * @param dev the pointer of device driver structure
  60. *
  61. * @return the error code, RT_EOK on successfully.
  62. */
  63. rt_err_t rt_device_unregister(rt_device_t dev)
  64. {
  65. RT_ASSERT(dev != RT_NULL);
  66. rt_object_detach(&(dev->parent));
  67. return RT_EOK;
  68. }
  69. RTM_EXPORT(rt_device_unregister);
  70. /**
  71. * This function initializes all registered device driver
  72. *
  73. * @return the error code, RT_EOK on successfully.
  74. *
  75. * @deprecated since 1.2.x, this function is not needed because the initialization
  76. * of a device is performed when applicaiton opens it.
  77. */
  78. rt_err_t rt_device_init_all(void)
  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. struct rt_object *object;
  92. struct rt_list_node *node;
  93. struct rt_object_information *information;
  94. extern struct rt_object_information rt_object_container[];
  95. /* enter critical */
  96. if (rt_thread_self() != RT_NULL)
  97. rt_enter_critical();
  98. /* try to find device object */
  99. information = &rt_object_container[RT_Object_Class_Device];
  100. for (node = information->object_list.next;
  101. node != &(information->object_list);
  102. node = node->next)
  103. {
  104. object = rt_list_entry(node, struct rt_object, list);
  105. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  106. {
  107. /* leave critical */
  108. if (rt_thread_self() != RT_NULL)
  109. rt_exit_critical();
  110. return (rt_device_t)object;
  111. }
  112. }
  113. /* leave critical */
  114. if (rt_thread_self() != RT_NULL)
  115. rt_exit_critical();
  116. /* not found */
  117. return RT_NULL;
  118. }
  119. RTM_EXPORT(rt_device_find);
  120. /**
  121. * This function will initialize the specified device
  122. *
  123. * @param dev the pointer of device driver structure
  124. *
  125. * @return the result
  126. */
  127. rt_err_t rt_device_init(rt_device_t dev)
  128. {
  129. rt_err_t result = RT_EOK;
  130. RT_ASSERT(dev != RT_NULL);
  131. /* get device init handler */
  132. if (dev->init != RT_NULL)
  133. {
  134. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  135. {
  136. result = dev->init(dev);
  137. if (result != RT_EOK)
  138. {
  139. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  140. dev->parent.name, result);
  141. }
  142. else
  143. {
  144. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  145. }
  146. }
  147. }
  148. return result;
  149. }
  150. /**
  151. * This function will open a device
  152. *
  153. * @param dev the pointer of device driver structure
  154. * @param oflag the flags for device open
  155. *
  156. * @return the result
  157. */
  158. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  159. {
  160. rt_err_t result = RT_EOK;
  161. RT_ASSERT(dev != RT_NULL);
  162. /* if device is not initialized, initialize it. */
  163. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  164. {
  165. if (dev->init != RT_NULL)
  166. {
  167. result = dev->init(dev);
  168. if (result != RT_EOK)
  169. {
  170. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  171. dev->parent.name, result);
  172. return result;
  173. }
  174. }
  175. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  176. }
  177. /* device is a stand alone device and opened */
  178. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
  179. (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  180. {
  181. return -RT_EBUSY;
  182. }
  183. /* call device open interface */
  184. if (dev->open != RT_NULL)
  185. {
  186. result = dev->open(dev, oflag);
  187. }
  188. /* set open flag */
  189. if (result == RT_EOK || result == -RT_ENOSYS)
  190. {
  191. dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
  192. dev->ref_count++;
  193. /* don't let bad things happen silently. If you are bitten by this assert,
  194. * please set the ref_count to a bigger type. */
  195. RT_ASSERT(dev->ref_count != 0);
  196. }
  197. return result;
  198. }
  199. RTM_EXPORT(rt_device_open);
  200. /**
  201. * This function will close a device
  202. *
  203. * @param dev the pointer of device driver structure
  204. *
  205. * @return the result
  206. */
  207. rt_err_t rt_device_close(rt_device_t dev)
  208. {
  209. rt_err_t result = RT_EOK;
  210. RT_ASSERT(dev != RT_NULL);
  211. if (dev->ref_count == 0)
  212. return -RT_ERROR;
  213. dev->ref_count--;
  214. if (dev->ref_count != 0)
  215. return RT_EOK;
  216. /* call device close interface */
  217. if (dev->close != RT_NULL)
  218. {
  219. result = dev->close(dev);
  220. }
  221. /* set open flag */
  222. if (result == RT_EOK || result == -RT_ENOSYS)
  223. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  224. return result;
  225. }
  226. RTM_EXPORT(rt_device_close);
  227. /**
  228. * This function will read some data from a device.
  229. *
  230. * @param dev the pointer of device driver structure
  231. * @param pos the position of reading
  232. * @param buffer the data buffer to save read data
  233. * @param size the size of buffer
  234. *
  235. * @return the actually read size on successful, otherwise negative returned.
  236. *
  237. * @note since 0.4.0, the unit of size/pos is a block for block device.
  238. */
  239. rt_size_t rt_device_read(rt_device_t dev,
  240. rt_off_t pos,
  241. void *buffer,
  242. rt_size_t size)
  243. {
  244. RT_ASSERT(dev != RT_NULL);
  245. if (dev->ref_count == 0)
  246. {
  247. rt_set_errno(-RT_ERROR);
  248. return 0;
  249. }
  250. /* call device read interface */
  251. if (dev->read != RT_NULL)
  252. {
  253. return dev->read(dev, pos, buffer, size);
  254. }
  255. /* set error code */
  256. rt_set_errno(-RT_ENOSYS);
  257. return 0;
  258. }
  259. RTM_EXPORT(rt_device_read);
  260. /**
  261. * This function will write some data to a device.
  262. *
  263. * @param dev the pointer of device driver structure
  264. * @param pos the position of written
  265. * @param buffer the data buffer to be written to device
  266. * @param size the size of buffer
  267. *
  268. * @return the actually written size on successful, otherwise negative returned.
  269. *
  270. * @note since 0.4.0, the unit of size/pos is a block for block device.
  271. */
  272. rt_size_t rt_device_write(rt_device_t dev,
  273. rt_off_t pos,
  274. const void *buffer,
  275. rt_size_t size)
  276. {
  277. RT_ASSERT(dev != RT_NULL);
  278. if (dev->ref_count == 0)
  279. {
  280. rt_set_errno(-RT_ERROR);
  281. return 0;
  282. }
  283. /* call device write interface */
  284. if (dev->write != RT_NULL)
  285. {
  286. return dev->write(dev, pos, buffer, size);
  287. }
  288. /* set error code */
  289. rt_set_errno(-RT_ENOSYS);
  290. return 0;
  291. }
  292. RTM_EXPORT(rt_device_write);
  293. /**
  294. * This function will perform a variety of control functions on devices.
  295. *
  296. * @param dev the pointer of device driver structure
  297. * @param cmd the command sent to device
  298. * @param arg the argument of command
  299. *
  300. * @return the result
  301. */
  302. rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
  303. {
  304. RT_ASSERT(dev != RT_NULL);
  305. /* call device write interface */
  306. if (dev->control != RT_NULL)
  307. {
  308. return dev->control(dev, cmd, arg);
  309. }
  310. return RT_EOK;
  311. }
  312. RTM_EXPORT(rt_device_control);
  313. /**
  314. * This function will set the reception indication callback function. This callback function
  315. * is invoked when this device receives data.
  316. *
  317. * @param dev the pointer of device driver structure
  318. * @param rx_ind the indication callback function
  319. *
  320. * @return RT_EOK
  321. */
  322. rt_err_t
  323. rt_device_set_rx_indicate(rt_device_t dev,
  324. rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
  325. {
  326. RT_ASSERT(dev != RT_NULL);
  327. dev->rx_indicate = rx_ind;
  328. return RT_EOK;
  329. }
  330. RTM_EXPORT(rt_device_set_rx_indicate);
  331. /**
  332. * This function will set the indication callback function when device has
  333. * written data to physical hardware.
  334. *
  335. * @param dev the pointer of device driver structure
  336. * @param tx_done the indication callback function
  337. *
  338. * @return RT_EOK
  339. */
  340. rt_err_t
  341. rt_device_set_tx_complete(rt_device_t dev,
  342. rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  343. {
  344. RT_ASSERT(dev != RT_NULL);
  345. dev->tx_complete = tx_done;
  346. return RT_EOK;
  347. }
  348. RTM_EXPORT(rt_device_set_tx_complete);
  349. #endif