device.c 14 KB

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