device.c 13 KB

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