device.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * File : device.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2011, 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. #include "kservice.h"
  17. #ifdef RT_USING_DEVICE
  18. /**
  19. * This function registers a device driver with specified name.
  20. *
  21. * @param dev the pointer of device driver structure
  22. * @param name the device driver's name
  23. * @param flags the flag of device
  24. *
  25. * @return the error code, RT_EOK on initialization successfully.
  26. */
  27. rt_err_t rt_device_register(rt_device_t dev, const char *name, rt_uint16_t flags)
  28. {
  29. if (dev == RT_NULL) 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 result = -RT_ENOSYS;
  149. return result;
  150. }
  151. /**
  152. * This function will open a device
  153. *
  154. * @param dev the pointer of device driver structure
  155. * @param oflag the flags for device open
  156. *
  157. * @return the result
  158. */
  159. rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
  160. {
  161. rt_err_t result;
  162. rt_err_t (*open)(rt_device_t dev, rt_uint16_t oflag);
  163. RT_ASSERT(dev != RT_NULL);
  164. result = RT_EOK;
  165. /* if device is not initialized, initialize it. */
  166. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  167. {
  168. result = dev->init(dev);
  169. if (result != RT_EOK)
  170. {
  171. rt_kprintf("To initialize device:%s failed. The error code is %d\n",
  172. dev->parent.name, result);
  173. return result;
  174. }
  175. else
  176. {
  177. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  178. }
  179. }
  180. /* device is a stand alone device and opened */
  181. if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) && (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
  182. return -RT_EBUSY;
  183. /* call device open interface */
  184. open = dev->open;
  185. if (open != RT_NULL)
  186. {
  187. result = open(dev, oflag);
  188. }
  189. else
  190. {
  191. /* no this interface in device driver */
  192. result = -RT_ENOSYS;
  193. }
  194. /* set open flag */
  195. if (result == RT_EOK || result == -RT_ENOSYS)
  196. dev->open_flag = oflag | RT_DEVICE_OFLAG_OPEN;
  197. return result;
  198. }
  199. /**
  200. * This function will close a device
  201. *
  202. * @param dev the pointer of device driver structure
  203. *
  204. * @return the result
  205. */
  206. rt_err_t rt_device_close(rt_device_t dev)
  207. {
  208. rt_err_t result;
  209. rt_err_t (*close)(rt_device_t dev);
  210. RT_ASSERT(dev != RT_NULL);
  211. /* call device close interface */
  212. close = dev->close;
  213. if (close != RT_NULL)
  214. {
  215. result = close(dev);
  216. }
  217. else
  218. {
  219. /* no this interface in device driver */
  220. result = -RT_ENOSYS;
  221. }
  222. /* set open flag */
  223. if (result == RT_EOK || result == -RT_ENOSYS)
  224. dev->open_flag = RT_DEVICE_OFLAG_CLOSE;
  225. return result;
  226. }
  227. /**
  228. * This function will read some data from a device.
  229. *
  230. * @param dev the pointer of device driver structure
  231. * @param pos the position of reading
  232. * @param buffer the data buffer to save read data
  233. * @param size the size of buffer
  234. *
  235. * @return the actually read size on successful, otherwise negative returned.
  236. *
  237. * @note since 0.4.0, the unit of size/pos is a block for block device.
  238. */
  239. rt_size_t rt_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  240. {
  241. rt_size_t (*read)(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
  242. RT_ASSERT(dev != RT_NULL);
  243. /* call device read interface */
  244. read = dev->read;
  245. if (read != RT_NULL)
  246. {
  247. return read(dev, pos, buffer, size);
  248. }
  249. /* set error code */
  250. rt_set_errno(-RT_ENOSYS);
  251. return 0;
  252. }
  253. /**
  254. * This function will write some data to a device.
  255. *
  256. * @param dev the pointer of device driver structure
  257. * @param pos the position of written
  258. * @param buffer the data buffer to be written to device
  259. * @param size the size of buffer
  260. *
  261. * @return the actually written size on successful, otherwise negative returned.
  262. *
  263. * @note since 0.4.0, the unit of size/pos is a block for block device.
  264. */
  265. rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  266. {
  267. rt_size_t (*write)(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
  268. RT_ASSERT(dev != RT_NULL);
  269. /* call device write interface */
  270. write = dev->write;
  271. if (write != RT_NULL)
  272. {
  273. return write(dev, pos, buffer, size);
  274. }
  275. /* set error code */
  276. rt_set_errno(-RT_ENOSYS);
  277. return 0;
  278. }
  279. /**
  280. * This function will perform a variety of control functions on devices.
  281. *
  282. * @param dev the pointer of device driver structure
  283. * @param cmd the command sent to device
  284. * @param arg the argument of command
  285. *
  286. * @return the result
  287. */
  288. rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
  289. {
  290. rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void *arg);
  291. RT_ASSERT(dev != RT_NULL);
  292. /* call device write interface */
  293. control = dev->control;
  294. if (control != RT_NULL)
  295. {
  296. return control(dev, cmd, arg);
  297. }
  298. return -RT_ENOSYS;
  299. }
  300. /**
  301. * This function will set the indication callback function when device receives
  302. * data.
  303. *
  304. * @param dev the pointer of device driver structure
  305. * @param rx_ind the indication callback function
  306. *
  307. * @return RT_EOK
  308. */
  309. 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))
  310. {
  311. RT_ASSERT(dev != RT_NULL);
  312. dev->rx_indicate = rx_ind;
  313. return RT_EOK;
  314. }
  315. /**
  316. * This function will set the indication callback function when device has written
  317. * data to physical hardware.
  318. *
  319. * @param dev the pointer of device driver structure
  320. * @param tx_done the indication callback function
  321. *
  322. * @return RT_EOK
  323. */
  324. rt_err_t rt_device_set_tx_complete(rt_device_t dev, rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
  325. {
  326. RT_ASSERT(dev != RT_NULL);
  327. dev->tx_complete = tx_done;
  328. return RT_EOK;
  329. }
  330. #endif