device.c 8.6 KB

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