device.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * File : device.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2007-01-21 Bernard the first version
  13. * 2010-05-04 Bernard add rt_device_init implementation
  14. * 2012-10-20 Bernard add device check in register function,
  15. * provided by Rob <rdent@iinet.net.au>
  16. */
  17. #include <rtthread.h>
  18. #ifdef RT_USING_DEVICE
  19. /**
  20. * This function registers a device driver with specified name.
  21. *
  22. * @param dev the pointer of device driver structure
  23. * @param name the device driver's name
  24. * @param flags the flag of device
  25. *
  26. * @return the error code, RT_EOK on initialization successfully.
  27. */
  28. rt_err_t rt_device_register(rt_device_t dev,
  29. const char *name,
  30. rt_uint16_t flags)
  31. {
  32. if (dev == RT_NULL)
  33. return -RT_ERROR;
  34. if (rt_device_find(name) != RT_NULL)
  35. return -RT_ERROR;
  36. rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
  37. dev->flag = flags;
  38. return RT_EOK;
  39. }
  40. RTM_EXPORT(rt_device_register);
  41. /**
  42. * This function removes a previously registered device driver
  43. *
  44. * @param dev the pointer of device driver structure
  45. *
  46. * @return the error code, RT_EOK on successfully.
  47. */
  48. rt_err_t rt_device_unregister(rt_device_t dev)
  49. {
  50. RT_ASSERT(dev != RT_NULL);
  51. rt_object_detach(&(dev->parent));
  52. return RT_EOK;
  53. }
  54. RTM_EXPORT(rt_device_unregister);
  55. /**
  56. * This function initializes all registered device driver
  57. *
  58. * @return the error code, RT_EOK on successfully.
  59. */
  60. rt_err_t rt_device_init_all(void)
  61. {
  62. struct rt_device *device;
  63. struct rt_list_node *node;
  64. struct rt_object_information *information;
  65. register rt_err_t result;
  66. extern struct rt_object_information rt_object_container[];
  67. information = &rt_object_container[RT_Object_Class_Device];
  68. /* for each device */
  69. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  70. {
  71. rt_err_t (*init)(rt_device_t dev);
  72. device = (struct rt_device *)rt_list_entry(node, struct rt_object, list);
  73. /* get device init handler */
  74. init = device->init;
  75. if (init != RT_NULL && !(device->flag & RT_DEVICE_FLAG_ACTIVATED))
  76. {
  77. result = init(device);
  78. if (result != RT_EOK)
  79. {
  80. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  81. device->parent.name, result);
  82. }
  83. else
  84. {
  85. device->flag |= RT_DEVICE_FLAG_ACTIVATED;
  86. }
  87. }
  88. }
  89. return RT_EOK;
  90. }
  91. /**
  92. * This function finds a device driver by specified name.
  93. *
  94. * @param name the device driver's name
  95. *
  96. * @return the registered device driver on successful, or RT_NULL on failure.
  97. */
  98. rt_device_t rt_device_find(const char *name)
  99. {
  100. struct rt_object *object;
  101. struct rt_list_node *node;
  102. struct rt_object_information *information;
  103. extern struct rt_object_information rt_object_container[];
  104. /* enter critical */
  105. if (rt_thread_self() != RT_NULL)
  106. rt_enter_critical();
  107. /* try to find device object */
  108. information = &rt_object_container[RT_Object_Class_Device];
  109. for (node = information->object_list.next; node != &(information->object_list); node = node->next)
  110. {
  111. object = rt_list_entry(node, struct rt_object, list);
  112. if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
  113. {
  114. /* leave critical */
  115. if (rt_thread_self() != RT_NULL)
  116. rt_exit_critical();
  117. return (rt_device_t)object;
  118. }
  119. }
  120. /* leave critical */
  121. if (rt_thread_self() != RT_NULL)
  122. rt_exit_critical();
  123. /* not found */
  124. return RT_NULL;
  125. }
  126. RTM_EXPORT(rt_device_find);
  127. /**
  128. * This function will initialize the specified device
  129. *
  130. * @param dev the pointer of device driver structure
  131. *
  132. * @return the result
  133. */
  134. rt_err_t rt_device_init(rt_device_t dev)
  135. {
  136. rt_err_t result = RT_EOK;
  137. rt_err_t (*init)(rt_device_t dev);
  138. RT_ASSERT(dev != RT_NULL);
  139. /* get device init handler */
  140. init = dev->init;
  141. if (init != RT_NULL)
  142. {
  143. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  144. {
  145. result = init(dev);
  146. if (result != RT_EOK)
  147. {
  148. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  149. dev->parent.name, result);
  150. }
  151. else
  152. {
  153. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  154. }
  155. }
  156. }
  157. else
  158. result = -RT_ENOSYS;
  159. return result;
  160. }
  161. /**
  162. * This function will open a device
  163. *
  164. * @param dev the pointer of device driver structure
  165. * @param oflag the flags for device open
  166. *
  167. * @return the result
  168. */
  169. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  170. {
  171. rt_err_t result;
  172. rt_err_t (*open)(rt_device_t dev, rt_uint16_t oflag);
  173. RT_ASSERT(dev != RT_NULL);
  174. result = RT_EOK;
  175. /* if device is not initialized, initialize it. */
  176. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  177. {
  178. if (dev->init != RT_NULL)
  179. {
  180. result = dev->init(dev);
  181. if (result != RT_EOK)
  182. {
  183. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  184. dev->parent.name, result);
  185. return result;
  186. }
  187. }
  188. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  189. }
  190. /* device is a stand alone device and opened */
  191. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) && (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  192. return -RT_EBUSY;
  193. /* call device open interface */
  194. open = dev->open;
  195. if (open != RT_NULL)
  196. {
  197. result = open(dev, oflag);
  198. }
  199. else
  200. {
  201. /* no this interface in device driver */
  202. /* result = -RT_ENOSYS; not set errno */
  203. }
  204. /* set open flag */
  205. if (result == RT_EOK || result == -RT_ENOSYS)
  206. dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
  207. return result;
  208. }
  209. RTM_EXPORT(rt_device_open);
  210. /**
  211. * This function will close a device
  212. *
  213. * @param dev the pointer of device driver structure
  214. *
  215. * @return the result
  216. */
  217. rt_err_t rt_device_close(rt_device_t dev)
  218. {
  219. rt_err_t result;
  220. rt_err_t (*close)(rt_device_t dev);
  221. RT_ASSERT(dev != RT_NULL);
  222. /* call device close interface */
  223. close = dev->close;
  224. if (close != RT_NULL)
  225. {
  226. result = close(dev);
  227. }
  228. else
  229. {
  230. /* no this interface in device driver */
  231. /* result = -RT_ENOSYS; not set errno */
  232. }
  233. /* set open flag */
  234. if (result == RT_EOK || result == -RT_ENOSYS)
  235. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  236. return result;
  237. }
  238. RTM_EXPORT(rt_device_close);
  239. /**
  240. * This function will read some data from a device.
  241. *
  242. * @param dev the pointer of device driver structure
  243. * @param pos the position of reading
  244. * @param buffer the data buffer to save read data
  245. * @param size the size of buffer
  246. *
  247. * @return the actually read size on successful, otherwise negative returned.
  248. *
  249. * @note since 0.4.0, the unit of size/pos is a block for block device.
  250. */
  251. rt_size_t rt_device_read(rt_device_t dev,
  252. rt_off_t pos,
  253. void *buffer,
  254. rt_size_t size)
  255. {
  256. rt_size_t (*read)(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  257. RT_ASSERT(dev != RT_NULL);
  258. /* call device read interface */
  259. read = dev->read;
  260. if (read != RT_NULL)
  261. {
  262. return read(dev, pos, buffer, size);
  263. }
  264. /* set error code */
  265. rt_set_errno(-RT_ENOSYS);
  266. return 0;
  267. }
  268. RTM_EXPORT(rt_device_read);
  269. /**
  270. * This function will write some data to a device.
  271. *
  272. * @param dev the pointer of device driver structure
  273. * @param pos the position of written
  274. * @param buffer the data buffer to be written to device
  275. * @param size the size of buffer
  276. *
  277. * @return the actually written size on successful, otherwise negative returned.
  278. *
  279. * @note since 0.4.0, the unit of size/pos is a block for block device.
  280. */
  281. rt_size_t rt_device_write(rt_device_t dev,
  282. rt_off_t pos,
  283. const void *buffer,
  284. rt_size_t size)
  285. {
  286. rt_size_t (*write)(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  287. RT_ASSERT(dev != RT_NULL);
  288. /* call device write interface */
  289. write = dev->write;
  290. if (write != RT_NULL)
  291. {
  292. return write(dev, pos, buffer, size);
  293. }
  294. /* set error code */
  295. rt_set_errno(-RT_ENOSYS);
  296. return 0;
  297. }
  298. RTM_EXPORT(rt_device_write);
  299. /**
  300. * This function will perform a variety of control functions on devices.
  301. *
  302. * @param dev the pointer of device driver structure
  303. * @param cmd the command sent to device
  304. * @param arg the argument of command
  305. *
  306. * @return the result
  307. */
  308. rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
  309. {
  310. rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void *arg);
  311. RT_ASSERT(dev != RT_NULL);
  312. /* call device write interface */
  313. control = dev->control;
  314. if (control != RT_NULL)
  315. {
  316. return control(dev, cmd, arg);
  317. }
  318. return -RT_ENOSYS;
  319. }
  320. RTM_EXPORT(rt_device_control);
  321. /**
  322. * This function will set the indication callback function when device receives
  323. * data.
  324. *
  325. * @param dev the pointer of device driver structure
  326. * @param rx_ind the indication callback function
  327. *
  328. * @return RT_EOK
  329. */
  330. rt_err_t
  331. rt_device_set_rx_indicate(rt_device_t dev,
  332. rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
  333. {
  334. RT_ASSERT(dev != RT_NULL);
  335. dev->rx_indicate = rx_ind;
  336. return RT_EOK;
  337. }
  338. RTM_EXPORT(rt_device_set_rx_indicate);
  339. /**
  340. * This function will set the indication callback function when device has
  341. * written data to physical hardware.
  342. *
  343. * @param dev the pointer of device driver structure
  344. * @param tx_done the indication callback function
  345. *
  346. * @return RT_EOK
  347. */
  348. rt_err_t
  349. rt_device_set_tx_complete(rt_device_t dev,
  350. rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  351. {
  352. RT_ASSERT(dev != RT_NULL);
  353. dev->tx_complete = tx_done;
  354. return RT_EOK;
  355. }
  356. RTM_EXPORT(rt_device_set_tx_complete);
  357. #endif