device.c 13 KB

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