device.c 12 KB

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