device.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. */
  29. #include <rtthread.h>
  30. #ifdef RT_USING_DEVICE
  31. /**
  32. * This function registers a device driver with specified name.
  33. *
  34. * @param dev the pointer of device driver structure
  35. * @param name the device driver's name
  36. * @param flags the flag of device
  37. *
  38. * @return the error code, RT_EOK on initialization successfully.
  39. */
  40. rt_err_t rt_device_register(rt_device_t dev,
  41. const char *name,
  42. rt_uint16_t flags)
  43. {
  44. if (dev == RT_NULL)
  45. return -RT_ERROR;
  46. if (rt_device_find(name) != RT_NULL)
  47. return -RT_ERROR;
  48. rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
  49. dev->flag = flags;
  50. dev->ref_count = 0;
  51. return RT_EOK;
  52. }
  53. RTM_EXPORT(rt_device_register);
  54. /**
  55. * This function removes a previously registered device driver
  56. *
  57. * @param dev the pointer of device driver structure
  58. *
  59. * @return the error code, RT_EOK on successfully.
  60. */
  61. rt_err_t rt_device_unregister(rt_device_t dev)
  62. {
  63. RT_ASSERT(dev != RT_NULL);
  64. rt_object_detach(&(dev->parent));
  65. return RT_EOK;
  66. }
  67. RTM_EXPORT(rt_device_unregister);
  68. /**
  69. * This function initializes all registered device driver
  70. *
  71. * @return the error code, RT_EOK on successfully.
  72. *
  73. * @deprecated since 1.2.x, this function is not needed because the initialization
  74. * of a device is performed when applicaiton opens it.
  75. */
  76. rt_err_t rt_device_init_all(void)
  77. {
  78. return RT_EOK;
  79. }
  80. /**
  81. * This function finds a device driver by specified name.
  82. *
  83. * @param name the device driver's name
  84. *
  85. * @return the registered device driver on successful, or RT_NULL on failure.
  86. */
  87. rt_device_t rt_device_find(const char *name)
  88. {
  89. struct rt_object *object;
  90. struct rt_list_node *node;
  91. struct rt_object_information *information;
  92. extern struct rt_object_information rt_object_container[];
  93. /* enter critical */
  94. if (rt_thread_self() != RT_NULL)
  95. rt_enter_critical();
  96. /* try to find device object */
  97. information = &rt_object_container[RT_Object_Class_Device];
  98. for (node = information->object_list.next;
  99. node != &(information->object_list);
  100. node = node->next)
  101. {
  102. object = rt_list_entry(node, struct rt_object, list);
  103. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  104. {
  105. /* leave critical */
  106. if (rt_thread_self() != RT_NULL)
  107. rt_exit_critical();
  108. return (rt_device_t)object;
  109. }
  110. }
  111. /* leave critical */
  112. if (rt_thread_self() != RT_NULL)
  113. rt_exit_critical();
  114. /* not found */
  115. return RT_NULL;
  116. }
  117. RTM_EXPORT(rt_device_find);
  118. /**
  119. * This function will initialize the specified 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 = RT_EOK;
  128. RT_ASSERT(dev != RT_NULL);
  129. /* get device init handler */
  130. if (dev->init != RT_NULL)
  131. {
  132. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  133. {
  134. result = dev->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. }
  146. return result;
  147. }
  148. /**
  149. * This function will open a device
  150. *
  151. * @param dev the pointer of device driver structure
  152. * @param oflag the flags for device open
  153. *
  154. * @return the result
  155. */
  156. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  157. {
  158. rt_err_t result = RT_EOK;
  159. RT_ASSERT(dev != RT_NULL);
  160. /* if device is not initialized, initialize it. */
  161. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  162. {
  163. if (dev->init != RT_NULL)
  164. {
  165. result = dev->init(dev);
  166. if (result != RT_EOK)
  167. {
  168. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  169. dev->parent.name, result);
  170. return result;
  171. }
  172. }
  173. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  174. }
  175. /* device is a stand alone device and opened */
  176. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
  177. (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  178. {
  179. return -RT_EBUSY;
  180. }
  181. /* call device open interface */
  182. if (dev->open != RT_NULL)
  183. {
  184. result = dev->open(dev, oflag);
  185. }
  186. /* set open flag */
  187. if (result == RT_EOK || result == -RT_ENOSYS)
  188. {
  189. dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
  190. dev->ref_count++;
  191. /* don't let bad things happen silently. If you are bitten by this assert,
  192. * please set the ref_count to a bigger type. */
  193. RT_ASSERT(dev->ref_count != 0);
  194. }
  195. return result;
  196. }
  197. RTM_EXPORT(rt_device_open);
  198. /**
  199. * This function will close a device
  200. *
  201. * @param dev the pointer of device driver structure
  202. *
  203. * @return the result
  204. */
  205. rt_err_t rt_device_close(rt_device_t dev)
  206. {
  207. rt_err_t result = RT_EOK;
  208. RT_ASSERT(dev != RT_NULL);
  209. if (dev->ref_count == 0)
  210. return -RT_ERROR;
  211. dev->ref_count--;
  212. if (dev->ref_count != 0)
  213. return RT_EOK;
  214. /* call device close interface */
  215. if (dev->close != RT_NULL)
  216. {
  217. result = dev->close(dev);
  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. RTM_EXPORT(rt_device_close);
  225. /**
  226. * This function will read some data from a device.
  227. *
  228. * @param dev the pointer of device driver structure
  229. * @param pos the position of reading
  230. * @param buffer the data buffer to save read data
  231. * @param size the size of buffer
  232. *
  233. * @return the actually read size on successful, otherwise negative returned.
  234. *
  235. * @note since 0.4.0, the unit of size/pos is a block for block device.
  236. */
  237. rt_size_t rt_device_read(rt_device_t dev,
  238. rt_off_t pos,
  239. void *buffer,
  240. rt_size_t size)
  241. {
  242. RT_ASSERT(dev != RT_NULL);
  243. if (dev->ref_count == 0)
  244. {
  245. rt_set_errno(-RT_ERROR);
  246. return 0;
  247. }
  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. if (dev->ref_count == 0)
  277. {
  278. rt_set_errno(-RT_ERROR);
  279. return 0;
  280. }
  281. /* call device write interface */
  282. if (dev->write != RT_NULL)
  283. {
  284. return dev->write(dev, pos, buffer, size);
  285. }
  286. /* set error code */
  287. rt_set_errno(-RT_ENOSYS);
  288. return 0;
  289. }
  290. RTM_EXPORT(rt_device_write);
  291. /**
  292. * This function will perform a variety of control functions on devices.
  293. *
  294. * @param dev the pointer of device driver structure
  295. * @param cmd the command sent to device
  296. * @param arg the argument of command
  297. *
  298. * @return the result
  299. */
  300. rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
  301. {
  302. RT_ASSERT(dev != RT_NULL);
  303. /* call device write interface */
  304. if (dev->control != RT_NULL)
  305. {
  306. return dev->control(dev, cmd, arg);
  307. }
  308. return RT_EOK;
  309. }
  310. RTM_EXPORT(rt_device_control);
  311. /**
  312. * This function will set the indication callback function when device receives
  313. * data.
  314. *
  315. * @param dev the pointer of device driver structure
  316. * @param rx_ind the indication callback function
  317. *
  318. * @return RT_EOK
  319. */
  320. rt_err_t
  321. rt_device_set_rx_indicate(rt_device_t dev,
  322. rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
  323. {
  324. RT_ASSERT(dev != RT_NULL);
  325. dev->rx_indicate = rx_ind;
  326. return RT_EOK;
  327. }
  328. RTM_EXPORT(rt_device_set_rx_indicate);
  329. /**
  330. * This function will set the indication callback function when device has
  331. * written data to physical hardware.
  332. *
  333. * @param dev the pointer of device driver structure
  334. * @param tx_done the indication callback function
  335. *
  336. * @return RT_EOK
  337. */
  338. rt_err_t
  339. rt_device_set_tx_complete(rt_device_t dev,
  340. rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  341. {
  342. RT_ASSERT(dev != RT_NULL);
  343. dev->tx_complete = tx_done;
  344. return RT_EOK;
  345. }
  346. RTM_EXPORT(rt_device_set_tx_complete);
  347. #endif