device.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #if defined(RT_USING_POSIX)
  54. dev->fops = RT_NULL;
  55. rt_list_init(&(dev->wait_queue));
  56. #endif
  57. return RT_EOK;
  58. }
  59. RTM_EXPORT(rt_device_register);
  60. /**
  61. * This function removes a previously registered device driver
  62. *
  63. * @param dev the pointer of device driver structure
  64. *
  65. * @return the error code, RT_EOK on successfully.
  66. */
  67. rt_err_t rt_device_unregister(rt_device_t dev)
  68. {
  69. RT_ASSERT(dev != RT_NULL);
  70. rt_object_detach(&(dev->parent));
  71. return RT_EOK;
  72. }
  73. RTM_EXPORT(rt_device_unregister);
  74. /**
  75. * This function initializes all registered device driver
  76. *
  77. * @return the error code, RT_EOK on successfully.
  78. *
  79. * @deprecated since 1.2.x, this function is not needed because the initialization
  80. * of a device is performed when applicaiton opens it.
  81. */
  82. rt_err_t rt_device_init_all(void)
  83. {
  84. return RT_EOK;
  85. }
  86. /**
  87. * This function finds a device driver by specified name.
  88. *
  89. * @param name the device driver's name
  90. *
  91. * @return the registered device driver on successful, or RT_NULL on failure.
  92. */
  93. rt_device_t rt_device_find(const char *name)
  94. {
  95. struct rt_object *object;
  96. struct rt_list_node *node;
  97. struct rt_object_information *information;
  98. extern struct rt_object_information rt_object_container[];
  99. /* enter critical */
  100. if (rt_thread_self() != RT_NULL)
  101. rt_enter_critical();
  102. /* try to find device object */
  103. information = &rt_object_container[RT_Object_Class_Device];
  104. for (node = information->object_list.next;
  105. node != &(information->object_list);
  106. node = node->next)
  107. {
  108. object = rt_list_entry(node, struct rt_object, list);
  109. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  110. {
  111. /* leave critical */
  112. if (rt_thread_self() != RT_NULL)
  113. rt_exit_critical();
  114. return (rt_device_t)object;
  115. }
  116. }
  117. /* leave critical */
  118. if (rt_thread_self() != RT_NULL)
  119. rt_exit_critical();
  120. /* not found */
  121. return RT_NULL;
  122. }
  123. RTM_EXPORT(rt_device_find);
  124. /**
  125. * This function will initialize the specified device
  126. *
  127. * @param dev the pointer of device driver structure
  128. *
  129. * @return the result
  130. */
  131. rt_err_t rt_device_init(rt_device_t dev)
  132. {
  133. rt_err_t result = RT_EOK;
  134. RT_ASSERT(dev != RT_NULL);
  135. /* get device init handler */
  136. if (dev->init != RT_NULL)
  137. {
  138. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  139. {
  140. result = dev->init(dev);
  141. if (result != RT_EOK)
  142. {
  143. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  144. dev->parent.name, result);
  145. }
  146. else
  147. {
  148. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  149. }
  150. }
  151. }
  152. return result;
  153. }
  154. /**
  155. * This function will open a device
  156. *
  157. * @param dev the pointer of device driver structure
  158. * @param oflag the flags for device open
  159. *
  160. * @return the result
  161. */
  162. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  163. {
  164. rt_err_t result = RT_EOK;
  165. RT_ASSERT(dev != RT_NULL);
  166. /* if device is not initialized, initialize it. */
  167. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  168. {
  169. if (dev->init != RT_NULL)
  170. {
  171. result = dev->init(dev);
  172. if (result != RT_EOK)
  173. {
  174. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  175. dev->parent.name, result);
  176. return result;
  177. }
  178. }
  179. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  180. }
  181. /* device is a stand alone device and opened */
  182. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
  183. (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  184. {
  185. return -RT_EBUSY;
  186. }
  187. /* call device open interface */
  188. if (dev->open != RT_NULL)
  189. {
  190. result = dev->open(dev, oflag);
  191. }
  192. else
  193. {
  194. /* set open flag */
  195. dev->open_flag = (oflag & RT_DEVICE_OFLAG_MASK);
  196. }
  197. /* set open flag */
  198. if (result == RT_EOK || result == -RT_ENOSYS)
  199. {
  200. dev->open_flag |= RT_DEVICE_OFLAG_OPEN;
  201. dev->ref_count++;
  202. /* don't let bad things happen silently. If you are bitten by this assert,
  203. * please set the ref_count to a bigger type. */
  204. RT_ASSERT(dev->ref_count != 0);
  205. }
  206. return result;
  207. }
  208. RTM_EXPORT(rt_device_open);
  209. /**
  210. * This function will close a device
  211. *
  212. * @param dev the pointer of device driver structure
  213. *
  214. * @return the result
  215. */
  216. rt_err_t rt_device_close(rt_device_t dev)
  217. {
  218. rt_err_t result = RT_EOK;
  219. RT_ASSERT(dev != RT_NULL);
  220. if (dev->ref_count == 0)
  221. return -RT_ERROR;
  222. dev->ref_count--;
  223. if (dev->ref_count != 0)
  224. return RT_EOK;
  225. /* call device close interface */
  226. if (dev->close != RT_NULL)
  227. {
  228. result = dev->close(dev);
  229. }
  230. /* set open flag */
  231. if (result == RT_EOK || result == -RT_ENOSYS)
  232. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  233. return result;
  234. }
  235. RTM_EXPORT(rt_device_close);
  236. /**
  237. * This function will read some data from a device.
  238. *
  239. * @param dev the pointer of device driver structure
  240. * @param pos the position of reading
  241. * @param buffer the data buffer to save read data
  242. * @param size the size of buffer
  243. *
  244. * @return the actually read size on successful, otherwise negative returned.
  245. *
  246. * @note since 0.4.0, the unit of size/pos is a block for block device.
  247. */
  248. rt_size_t rt_device_read(rt_device_t dev,
  249. rt_off_t pos,
  250. void *buffer,
  251. rt_size_t size)
  252. {
  253. RT_ASSERT(dev != RT_NULL);
  254. if (dev->ref_count == 0)
  255. {
  256. rt_set_errno(-RT_ERROR);
  257. return 0;
  258. }
  259. /* call device read interface */
  260. if (dev->read != RT_NULL)
  261. {
  262. return dev->read(dev, pos, buffer, size);
  263. }
  264. /* set error code */
  265. rt_set_errno(-RT_ENOSYS);
  266. return 0;
  267. }
  268. RTM_EXPORT(rt_device_read);
  269. /**
  270. * This function will write some data to a device.
  271. *
  272. * @param dev the pointer of device driver structure
  273. * @param pos the position of written
  274. * @param buffer the data buffer to be written to device
  275. * @param size the size of buffer
  276. *
  277. * @return the actually written size on successful, otherwise negative returned.
  278. *
  279. * @note since 0.4.0, the unit of size/pos is a block for block device.
  280. */
  281. rt_size_t rt_device_write(rt_device_t dev,
  282. rt_off_t pos,
  283. const void *buffer,
  284. rt_size_t size)
  285. {
  286. RT_ASSERT(dev != RT_NULL);
  287. if (dev->ref_count == 0)
  288. {
  289. rt_set_errno(-RT_ERROR);
  290. return 0;
  291. }
  292. /* call device write interface */
  293. if (dev->write != RT_NULL)
  294. {
  295. return dev->write(dev, pos, buffer, size);
  296. }
  297. /* set error code */
  298. rt_set_errno(-RT_ENOSYS);
  299. return 0;
  300. }
  301. RTM_EXPORT(rt_device_write);
  302. /**
  303. * This function will perform a variety of control functions on devices.
  304. *
  305. * @param dev the pointer of device driver structure
  306. * @param cmd the command sent to device
  307. * @param arg the argument of command
  308. *
  309. * @return the result
  310. */
  311. rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg)
  312. {
  313. RT_ASSERT(dev != RT_NULL);
  314. /* call device write interface */
  315. if (dev->control != RT_NULL)
  316. {
  317. return dev->control(dev, cmd, arg);
  318. }
  319. return -RT_ENOSYS;
  320. }
  321. RTM_EXPORT(rt_device_control);
  322. /**
  323. * This function will set the reception indication callback function. This callback function
  324. * is invoked when this device receives data.
  325. *
  326. * @param dev the pointer of device driver structure
  327. * @param rx_ind the indication callback function
  328. *
  329. * @return RT_EOK
  330. */
  331. rt_err_t
  332. rt_device_set_rx_indicate(rt_device_t dev,
  333. rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
  334. {
  335. RT_ASSERT(dev != RT_NULL);
  336. dev->rx_indicate = rx_ind;
  337. return RT_EOK;
  338. }
  339. RTM_EXPORT(rt_device_set_rx_indicate);
  340. /**
  341. * This function will set the indication callback function when device has
  342. * written data to physical hardware.
  343. *
  344. * @param dev the pointer of device driver structure
  345. * @param tx_done the indication callback function
  346. *
  347. * @return RT_EOK
  348. */
  349. rt_err_t
  350. rt_device_set_tx_complete(rt_device_t dev,
  351. rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  352. {
  353. RT_ASSERT(dev != RT_NULL);
  354. dev->tx_complete = tx_done;
  355. return RT_EOK;
  356. }
  357. RTM_EXPORT(rt_device_set_tx_complete);
  358. #endif