device.c 12 KB

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