device.c 13 KB

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