device.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2007-01-21 Bernard the first version
  9. * 2010-05-04 Bernard add rt_device_init implementation
  10. * 2012-10-20 Bernard add device check in register function,
  11. * provided by Rob <rdent@iinet.net.au>
  12. * 2012-12-25 Bernard return RT_EOK if the device interface not exist.
  13. * 2013-07-09 Grissiom add ref_count support
  14. * 2016-04-02 Bernard fix the open_flag initialization issue.
  15. * 2021-03-19 Meco Man remove rt_device_init_all()
  16. */
  17. #include <rtthread.h>
  18. #define DBG_TAG "kernel.device"
  19. #ifdef RT_DEBUG_DEVICE
  20. #define DBG_LVL DBG_LOG
  21. #else
  22. #define DBG_LVL DBG_WARNING
  23. #endif /* defined (RT_DEBUG_DEVICE) */
  24. #include <rtdbg.h>
  25. #ifdef RT_USING_POSIX_DEVIO
  26. #include <rtdevice.h> /* for wqueue_init */
  27. #endif /* RT_USING_POSIX_DEVIO */
  28. #ifdef RT_USING_DFS_V2
  29. #include <devfs.h>
  30. #endif /* RT_USING_DFS_V2 */
  31. #ifdef RT_USING_DEVICE
  32. #ifdef RT_USING_DEVICE_OPS
  33. #define device_init (dev->ops ? dev->ops->init : RT_NULL)
  34. #define device_open (dev->ops ? dev->ops->open : RT_NULL)
  35. #define device_close (dev->ops ? dev->ops->close : RT_NULL)
  36. #define device_read (dev->ops ? dev->ops->read : RT_NULL)
  37. #define device_write (dev->ops ? dev->ops->write : RT_NULL)
  38. #define device_control (dev->ops ? dev->ops->control : RT_NULL)
  39. #else
  40. #define device_init (dev->init)
  41. #define device_open (dev->open)
  42. #define device_close (dev->close)
  43. #define device_read (dev->read)
  44. #define device_write (dev->write)
  45. #define device_control (dev->control)
  46. #endif /* RT_USING_DEVICE_OPS */
  47. /**
  48. * @brief This function registers a device driver with a specified name.
  49. *
  50. * @param dev is the pointer of device driver structure.
  51. *
  52. * @param name is the device driver's name.
  53. *
  54. * @param flags is the capabilities flag of device.
  55. *
  56. * @return the error code, RT_EOK on initialization successfully.
  57. */
  58. rt_err_t rt_device_register(rt_device_t dev,
  59. const char *name,
  60. rt_uint16_t flags)
  61. {
  62. if (dev == RT_NULL)
  63. return -RT_ERROR;
  64. if (rt_device_find(name) != RT_NULL)
  65. return -RT_ERROR;
  66. rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
  67. dev->flag = flags;
  68. dev->ref_count = 0;
  69. dev->open_flag = 0;
  70. #ifdef RT_USING_POSIX_DEVIO
  71. dev->fops = RT_NULL;
  72. rt_wqueue_init(&(dev->wait_queue));
  73. #endif /* RT_USING_POSIX_DEVIO */
  74. #ifdef RT_USING_DFS_V2
  75. dfs_devfs_device_add(dev);
  76. #endif /* RT_USING_DFS_V2 */
  77. return RT_EOK;
  78. }
  79. RTM_EXPORT(rt_device_register);
  80. /**
  81. * @brief This function removes a previously registered device driver.
  82. *
  83. * @param dev is the pointer of device driver structure.
  84. *
  85. * @return the error code, RT_EOK on successfully.
  86. */
  87. rt_err_t rt_device_unregister(rt_device_t dev)
  88. {
  89. /* parameter check */
  90. RT_ASSERT(dev != RT_NULL);
  91. RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
  92. RT_ASSERT(rt_object_is_systemobject(&dev->parent));
  93. rt_object_detach(&(dev->parent));
  94. return RT_EOK;
  95. }
  96. RTM_EXPORT(rt_device_unregister);
  97. /**
  98. * @brief This function finds a device driver by specified name.
  99. *
  100. * @param name is the device driver's name.
  101. *
  102. * @return the registered device driver on successful, or RT_NULL on failure.
  103. */
  104. rt_device_t rt_device_find(const char *name)
  105. {
  106. return (rt_device_t)rt_object_find(name, RT_Object_Class_Device);
  107. }
  108. RTM_EXPORT(rt_device_find);
  109. #ifdef RT_USING_HEAP
  110. /**
  111. * @brief This function creates a device object with user data size.
  112. *
  113. * @param type is the type of the device object.
  114. *
  115. * @param attach_size is the size of user data.
  116. *
  117. * @return the allocated device object, or RT_NULL when failed.
  118. */
  119. rt_device_t rt_device_create(int type, int attach_size)
  120. {
  121. int size;
  122. rt_device_t device;
  123. size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE);
  124. attach_size = RT_ALIGN(attach_size, RT_ALIGN_SIZE);
  125. /* use the total size */
  126. size += attach_size;
  127. device = (rt_device_t)rt_malloc(size);
  128. if (device)
  129. {
  130. rt_memset(device, 0x0, sizeof(struct rt_device));
  131. device->type = (enum rt_device_class_type)type;
  132. }
  133. return device;
  134. }
  135. RTM_EXPORT(rt_device_create);
  136. /**
  137. * @brief This function destroy the specific device object.
  138. *
  139. * @param dev is a specific device object.
  140. */
  141. void rt_device_destroy(rt_device_t dev)
  142. {
  143. /* parameter check */
  144. RT_ASSERT(dev != RT_NULL);
  145. RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
  146. RT_ASSERT(rt_object_is_systemobject(&dev->parent) == RT_FALSE);
  147. rt_object_detach(&(dev->parent));
  148. /* release this device object */
  149. rt_free(dev);
  150. }
  151. RTM_EXPORT(rt_device_destroy);
  152. #endif /* RT_USING_HEAP */
  153. /**
  154. * @brief This function will initialize the specified device.
  155. *
  156. * @param dev is the pointer of device driver structure.
  157. *
  158. * @return the result, RT_EOK on successfully.
  159. */
  160. rt_err_t rt_device_init(rt_device_t dev)
  161. {
  162. rt_err_t result = RT_EOK;
  163. RT_ASSERT(dev != RT_NULL);
  164. /* get device_init handler */
  165. if (device_init != RT_NULL)
  166. {
  167. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  168. {
  169. result = device_init(dev);
  170. if (result != RT_EOK)
  171. {
  172. LOG_E("To initialize device:%s failed. The error code is %d",
  173. dev->parent.name, result);
  174. }
  175. else
  176. {
  177. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  178. }
  179. }
  180. }
  181. return result;
  182. }
  183. /**
  184. * @brief This function will open a device.
  185. *
  186. * @param dev is the pointer of device driver structure.
  187. *
  188. * @param oflag is the flags for device open.
  189. *
  190. * @return the result, RT_EOK on successfully.
  191. */
  192. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  193. {
  194. rt_err_t result = RT_EOK;
  195. /* parameter check */
  196. RT_ASSERT(dev != RT_NULL);
  197. RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
  198. /* if device is not initialized, initialize it. */
  199. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  200. {
  201. if (device_init != RT_NULL)
  202. {
  203. result = device_init(dev);
  204. if (result != RT_EOK)
  205. {
  206. LOG_E("To initialize device:%s failed. The error code is %d",
  207. dev->parent.name, result);
  208. return result;
  209. }
  210. }
  211. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  212. }
  213. /* device is a stand alone device and opened */
  214. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
  215. (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  216. {
  217. return -RT_EBUSY;
  218. }
  219. /* device is not opened or opened by other oflag, call device_open interface */
  220. if (!(dev->open_flag & RT_DEVICE_OFLAG_OPEN) ||
  221. ((dev->open_flag & RT_DEVICE_OFLAG_MASK) != (oflag & RT_DEVICE_OFLAG_MASK)))
  222. {
  223. if (device_open != RT_NULL)
  224. {
  225. result = device_open(dev, oflag);
  226. }
  227. else
  228. {
  229. /* set open flag */
  230. dev->open_flag = (oflag & RT_DEVICE_OFLAG_MASK);
  231. }
  232. }
  233. /* set open flag */
  234. if (result == RT_EOK || result == -RT_ENOSYS)
  235. {
  236. dev->open_flag |= RT_DEVICE_OFLAG_OPEN;
  237. dev->ref_count++;
  238. /* don't let bad things happen silently. If you are bitten by this assert,
  239. * please set the ref_count to a bigger type. */
  240. RT_ASSERT(dev->ref_count != 0);
  241. }
  242. return result;
  243. }
  244. RTM_EXPORT(rt_device_open);
  245. /**
  246. * @brief This function will close a device.
  247. *
  248. * @param dev is the pointer of device driver structure.
  249. *
  250. * @return the result, RT_EOK on successfully.
  251. */
  252. rt_err_t rt_device_close(rt_device_t dev)
  253. {
  254. rt_err_t result = RT_EOK;
  255. /* parameter check */
  256. RT_ASSERT(dev != RT_NULL);
  257. RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
  258. if (dev->ref_count == 0)
  259. return -RT_ERROR;
  260. dev->ref_count--;
  261. if (dev->ref_count != 0)
  262. return RT_EOK;
  263. /* call device_close interface */
  264. if (device_close != RT_NULL)
  265. {
  266. result = device_close(dev);
  267. }
  268. /* set open flag */
  269. if (result == RT_EOK || result == -RT_ENOSYS)
  270. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  271. return result;
  272. }
  273. RTM_EXPORT(rt_device_close);
  274. /**
  275. * @brief This function will read some data from a device.
  276. *
  277. * @param dev is the pointer of device driver structure.
  278. *
  279. * @param pos is the position when reading.
  280. *
  281. * @param buffer is a data buffer to save the read data.
  282. *
  283. * @param size is the size of buffer.
  284. *
  285. * @return the actually read size on successful, otherwise 0 will be returned.
  286. *
  287. * @note the unit of size/pos is a block for block device.
  288. */
  289. rt_ssize_t rt_device_read(rt_device_t dev,
  290. rt_off_t pos,
  291. void *buffer,
  292. rt_size_t size)
  293. {
  294. /* parameter check */
  295. RT_ASSERT(dev != RT_NULL);
  296. RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
  297. if (dev->ref_count == 0)
  298. {
  299. rt_set_errno(-RT_ERROR);
  300. return 0;
  301. }
  302. /* call device_read interface */
  303. if (device_read != RT_NULL)
  304. {
  305. return device_read(dev, pos, buffer, size);
  306. }
  307. /* set error code */
  308. rt_set_errno(-RT_ENOSYS);
  309. return 0;
  310. }
  311. RTM_EXPORT(rt_device_read);
  312. /**
  313. * @brief This function will write some data to a device.
  314. *
  315. * @param dev is the pointer of device driver structure.
  316. *
  317. * @param pos is the position when writing.
  318. *
  319. * @param buffer is the data buffer to be written to device.
  320. *
  321. * @param size is the size of buffer.
  322. *
  323. * @return the actually written size on successful, otherwise 0 will be returned.
  324. *
  325. * @note the unit of size/pos is a block for block device.
  326. */
  327. rt_ssize_t rt_device_write(rt_device_t dev,
  328. rt_off_t pos,
  329. const void *buffer,
  330. rt_size_t size)
  331. {
  332. /* parameter check */
  333. RT_ASSERT(dev != RT_NULL);
  334. RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
  335. if (dev->ref_count == 0)
  336. {
  337. rt_set_errno(-RT_ERROR);
  338. return 0;
  339. }
  340. /* call device_write interface */
  341. if (device_write != RT_NULL)
  342. {
  343. return device_write(dev, pos, buffer, size);
  344. }
  345. /* set error code */
  346. rt_set_errno(-RT_ENOSYS);
  347. return 0;
  348. }
  349. RTM_EXPORT(rt_device_write);
  350. /**
  351. * @brief This function will perform a variety of control functions on devices.
  352. *
  353. * @param dev is the pointer of device driver structure.
  354. *
  355. * @param cmd is the command sent to device.
  356. *
  357. * @param arg is the argument of command.
  358. *
  359. * @return the result, -RT_ENOSYS for failed.
  360. */
  361. rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg)
  362. {
  363. /* parameter check */
  364. RT_ASSERT(dev != RT_NULL);
  365. RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
  366. /* call device_write interface */
  367. if (device_control != RT_NULL)
  368. {
  369. return device_control(dev, cmd, arg);
  370. }
  371. return -RT_ENOSYS;
  372. }
  373. RTM_EXPORT(rt_device_control);
  374. /**
  375. * @brief This function will set the reception indication callback function. This callback function
  376. * is invoked when this device receives data.
  377. *
  378. * @param dev is the pointer of device driver structure.
  379. *
  380. * @param rx_ind is the indication callback function.
  381. *
  382. * @return RT_EOK
  383. */
  384. rt_err_t rt_device_set_rx_indicate(rt_device_t dev,
  385. rt_err_t (*rx_ind)(rt_device_t dev,
  386. rt_size_t size))
  387. {
  388. /* parameter check */
  389. RT_ASSERT(dev != RT_NULL);
  390. RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
  391. dev->rx_indicate = rx_ind;
  392. return RT_EOK;
  393. }
  394. RTM_EXPORT(rt_device_set_rx_indicate);
  395. /**
  396. * @brief This function will set a callback function. The callback function
  397. * will be called when device has written data to physical hardware.
  398. *
  399. * @param dev is the pointer of device driver structure.
  400. *
  401. * @param tx_done is the indication callback function.
  402. *
  403. * @return RT_EOK
  404. */
  405. rt_err_t rt_device_set_tx_complete(rt_device_t dev,
  406. rt_err_t (*tx_done)(rt_device_t dev,
  407. void *buffer))
  408. {
  409. /* parameter check */
  410. RT_ASSERT(dev != RT_NULL);
  411. RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
  412. dev->tx_complete = tx_done;
  413. return RT_EOK;
  414. }
  415. RTM_EXPORT(rt_device_set_tx_complete);
  416. #endif /* RT_USING_DEVICE */