device.c 8.6 KB

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