device.c 10 KB

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