device.c 11 KB

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