device.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2007-01-21 Bernard the first version
  13. * 2010-05-04 Bernard add rt_device_init implementation
  14. * 2012-10-20 Bernard add device check in register function,
  15. * provided by Rob <rdent@iinet.net.au>
  16. */
  17. #include <rtthread.h>
  18. #ifdef RT_USING_DEVICE
  19. /**
  20. * This function registers a device driver with specified name.
  21. *
  22. * @param dev the pointer of device driver structure
  23. * @param name the device driver's name
  24. * @param flags the flag of device
  25. *
  26. * @return the error code, RT_EOK on initialization successfully.
  27. */
  28. rt_err_t rt_device_register(rt_device_t dev, const char *name, rt_uint16_t flags)
  29. {
  30. if (dev == RT_NULL)
  31. return -RT_ERROR;
  32. if (rt_device_find(name) != RT_NULL)
  33. return -RT_ERROR;
  34. rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
  35. dev->flag = flags;
  36. return RT_EOK;
  37. }
  38. RTM_EXPORT(rt_device_register);
  39. /**
  40. * This function removes a previously registered device driver
  41. *
  42. * @param dev the pointer of device driver structure
  43. *
  44. * @return the error code, RT_EOK on successfully.
  45. */
  46. rt_err_t rt_device_unregister(rt_device_t dev)
  47. {
  48. RT_ASSERT(dev != RT_NULL);
  49. rt_object_detach(&(dev->parent));
  50. return RT_EOK;
  51. }
  52. RTM_EXPORT(rt_device_unregister);
  53. /**
  54. * This function initializes all registered device driver
  55. *
  56. * @return the error code, RT_EOK on successfully.
  57. */
  58. rt_err_t rt_device_init_all(void)
  59. {
  60. struct rt_device *device;
  61. struct rt_list_node *node;
  62. struct rt_object_information *information;
  63. register rt_err_t result;
  64. extern struct rt_object_information rt_object_container[];
  65. information = &rt_object_container[RT_Object_Class_Device];
  66. /* for each device */
  67. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  68. {
  69. rt_err_t (*init)(rt_device_t dev);
  70. device = (struct rt_device *)rt_list_entry(node, struct rt_object, list);
  71. /* get device init handler */
  72. init = device->init;
  73. if (init != RT_NULL && !(device->flag & RT_DEVICE_FLAG_ACTIVATED))
  74. {
  75. result = init(device);
  76. if (result != RT_EOK)
  77. {
  78. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  79. device->parent.name, result);
  80. }
  81. else
  82. {
  83. device->flag |= RT_DEVICE_FLAG_ACTIVATED;
  84. }
  85. }
  86. }
  87. return RT_EOK;
  88. }
  89. /**
  90. * This function finds a device driver by specified name.
  91. *
  92. * @param name the device driver's name
  93. *
  94. * @return the registered device driver on successful, or RT_NULL on failure.
  95. */
  96. rt_device_t rt_device_find(const char *name)
  97. {
  98. struct rt_object *object;
  99. struct rt_list_node *node;
  100. struct rt_object_information *information;
  101. extern struct rt_object_information rt_object_container[];
  102. /* enter critical */
  103. if (rt_thread_self() != RT_NULL)
  104. rt_enter_critical();
  105. /* try to find device object */
  106. information = &rt_object_container[RT_Object_Class_Device];
  107. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  108. {
  109. object = rt_list_entry(node, struct rt_object, list);
  110. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  111. {
  112. /* leave critical */
  113. if (rt_thread_self() != RT_NULL)
  114. rt_exit_critical();
  115. return (rt_device_t)object;
  116. }
  117. }
  118. /* leave critical */
  119. if (rt_thread_self() != RT_NULL)
  120. rt_exit_critical();
  121. /* not found */
  122. return RT_NULL;
  123. }
  124. RTM_EXPORT(rt_device_find);
  125. /**
  126. * This function will initialize the specified device
  127. *
  128. * @param dev the pointer of device driver structure
  129. *
  130. * @return the result
  131. */
  132. rt_err_t rt_device_init(rt_device_t dev)
  133. {
  134. rt_err_t result = RT_EOK;
  135. rt_err_t (*init)(rt_device_t dev);
  136. RT_ASSERT(dev != RT_NULL);
  137. /* get device init handler */
  138. init = dev->init;
  139. if (init != RT_NULL)
  140. {
  141. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  142. {
  143. result = init(dev);
  144. if (result != RT_EOK)
  145. {
  146. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  147. dev->parent.name, result);
  148. }
  149. else
  150. {
  151. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  152. }
  153. }
  154. }
  155. else
  156. result = -RT_ENOSYS;
  157. return result;
  158. }
  159. /**
  160. * This function will open a device
  161. *
  162. * @param dev the pointer of device driver structure
  163. * @param oflag the flags for device open
  164. *
  165. * @return the result
  166. */
  167. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  168. {
  169. rt_err_t result;
  170. rt_err_t (*open)(rt_device_t dev, rt_uint16_t oflag);
  171. RT_ASSERT(dev != RT_NULL);
  172. result = RT_EOK;
  173. /* if device is not initialized, initialize it. */
  174. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  175. {
  176. if (dev->init != RT_NULL )
  177. {
  178. result = dev->init(dev);
  179. if (result != RT_EOK)
  180. {
  181. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  182. dev->parent.name, result);
  183. return result;
  184. }
  185. }
  186. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  187. }
  188. /* device is a stand alone device and opened */
  189. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) && (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  190. return -RT_EBUSY;
  191. /* call device open interface */
  192. open = dev->open;
  193. if (open != RT_NULL)
  194. {
  195. result = open(dev, oflag);
  196. }
  197. else
  198. {
  199. /* no this interface in device driver */
  200. /* result = -RT_ENOSYS; not set errno */
  201. }
  202. /* set open flag */
  203. if (result == RT_EOK || result == -RT_ENOSYS)
  204. dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
  205. return result;
  206. }
  207. RTM_EXPORT(rt_device_open);
  208. /**
  209. * This function will close a device
  210. *
  211. * @param dev the pointer of device driver structure
  212. *
  213. * @return the result
  214. */
  215. rt_err_t rt_device_close(rt_device_t dev)
  216. {
  217. rt_err_t result;
  218. rt_err_t (*close)(rt_device_t dev);
  219. RT_ASSERT(dev != RT_NULL);
  220. /* call device close interface */
  221. close = dev->close;
  222. if (close != RT_NULL)
  223. {
  224. result = close(dev);
  225. }
  226. else
  227. {
  228. /* no this interface in device driver */
  229. /* result = -RT_ENOSYS; not set errno */
  230. }
  231. /* set open flag */
  232. if (result == RT_EOK || result == -RT_ENOSYS)
  233. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  234. return result;
  235. }
  236. RTM_EXPORT(rt_device_close);
  237. /**
  238. * This function will read some data from a device.
  239. *
  240. * @param dev the pointer of device driver structure
  241. * @param pos the position of reading
  242. * @param buffer the data buffer to save read data
  243. * @param size the size of buffer
  244. *
  245. * @return the actually read size on successful, otherwise negative returned.
  246. *
  247. * @note since 0.4.0, the unit of size/pos is a block for block device.
  248. */
  249. rt_size_t rt_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  250. {
  251. rt_size_t (*read)(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  252. RT_ASSERT(dev != RT_NULL);
  253. /* call device read interface */
  254. read = dev->read;
  255. if (read != RT_NULL)
  256. {
  257. return read(dev, pos, buffer, size);
  258. }
  259. /* set error code */
  260. rt_set_errno(-RT_ENOSYS);
  261. return 0;
  262. }
  263. RTM_EXPORT(rt_device_read);
  264. /**
  265. * This function will write some data to a device.
  266. *
  267. * @param dev the pointer of device driver structure
  268. * @param pos the position of written
  269. * @param buffer the data buffer to be written to device
  270. * @param size the size of buffer
  271. *
  272. * @return the actually written size on successful, otherwise negative returned.
  273. *
  274. * @note since 0.4.0, the unit of size/pos is a block for block device.
  275. */
  276. rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  277. {
  278. rt_size_t (*write)(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  279. RT_ASSERT(dev != RT_NULL);
  280. /* call device write interface */
  281. write = dev->write;
  282. if (write != RT_NULL)
  283. {
  284. return 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_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void *arg);
  303. RT_ASSERT(dev != RT_NULL);
  304. /* call device write interface */
  305. control = dev->control;
  306. if (control != RT_NULL)
  307. {
  308. return control(dev, cmd, arg);
  309. }
  310. return -RT_ENOSYS;
  311. }
  312. RTM_EXPORT(rt_device_control);
  313. /**
  314. * This function will set the indication callback function when device receives
  315. * 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 rt_device_set_rx_indicate(rt_device_t dev, 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 written
  331. * 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 rt_device_set_tx_complete(rt_device_t dev, rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  339. {
  340. RT_ASSERT(dev != RT_NULL);
  341. dev->tx_complete = tx_done;
  342. return RT_EOK;
  343. }
  344. RTM_EXPORT(rt_device_set_tx_complete);
  345. #endif