device.c 12 KB

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