device.c 11 KB

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