device.c 12 KB

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