device.c 11 KB

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