inkern.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* The industrial I/O core in kernel channel mapping
  2. *
  3. * Copyright (c) 2011 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/err.h>
  10. #include <linux/export.h>
  11. #include <linux/slab.h>
  12. #include <linux/mutex.h>
  13. #include <linux/iio/iio.h>
  14. #include "iio_core.h"
  15. #include <linux/iio/machine.h>
  16. #include <linux/iio/driver.h>
  17. #include <linux/iio/consumer.h>
  18. struct iio_map_internal {
  19. struct iio_dev *indio_dev;
  20. struct iio_map *map;
  21. struct list_head l;
  22. };
  23. static LIST_HEAD(iio_map_list);
  24. static DEFINE_MUTEX(iio_map_list_lock);
  25. int iio_map_array_register(struct iio_dev *indio_dev, struct iio_map *maps)
  26. {
  27. int i = 0, ret = 0;
  28. struct iio_map_internal *mapi;
  29. if (maps == NULL)
  30. return 0;
  31. mutex_lock(&iio_map_list_lock);
  32. while (maps[i].consumer_dev_name != NULL) {
  33. mapi = kzalloc(sizeof(*mapi), GFP_KERNEL);
  34. if (mapi == NULL) {
  35. ret = -ENOMEM;
  36. goto error_ret;
  37. }
  38. mapi->map = &maps[i];
  39. mapi->indio_dev = indio_dev;
  40. list_add(&mapi->l, &iio_map_list);
  41. i++;
  42. }
  43. error_ret:
  44. mutex_unlock(&iio_map_list_lock);
  45. return ret;
  46. }
  47. EXPORT_SYMBOL_GPL(iio_map_array_register);
  48. /* Assumes the exact same array (e.g. memory locations)
  49. * used at unregistration as used at registration rather than
  50. * more complex checking of contents.
  51. */
  52. int iio_map_array_unregister(struct iio_dev *indio_dev,
  53. struct iio_map *maps)
  54. {
  55. int i = 0, ret = 0;
  56. bool found_it;
  57. struct iio_map_internal *mapi;
  58. if (maps == NULL)
  59. return 0;
  60. mutex_lock(&iio_map_list_lock);
  61. while (maps[i].consumer_dev_name != NULL) {
  62. found_it = false;
  63. list_for_each_entry(mapi, &iio_map_list, l)
  64. if (&maps[i] == mapi->map) {
  65. list_del(&mapi->l);
  66. kfree(mapi);
  67. found_it = true;
  68. break;
  69. }
  70. if (found_it == false) {
  71. ret = -ENODEV;
  72. goto error_ret;
  73. }
  74. i++;
  75. }
  76. error_ret:
  77. mutex_unlock(&iio_map_list_lock);
  78. return ret;
  79. }
  80. EXPORT_SYMBOL_GPL(iio_map_array_unregister);
  81. static const struct iio_chan_spec
  82. *iio_chan_spec_from_name(const struct iio_dev *indio_dev,
  83. const char *name)
  84. {
  85. int i;
  86. const struct iio_chan_spec *chan = NULL;
  87. for (i = 0; i < indio_dev->num_channels; i++)
  88. if (indio_dev->channels[i].datasheet_name &&
  89. strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
  90. chan = &indio_dev->channels[i];
  91. break;
  92. }
  93. return chan;
  94. }
  95. struct iio_channel *iio_st_channel_get(const char *name,
  96. const char *channel_name)
  97. {
  98. struct iio_map_internal *c_i = NULL, *c = NULL;
  99. struct iio_channel *channel;
  100. if (name == NULL && channel_name == NULL)
  101. return ERR_PTR(-ENODEV);
  102. /* first find matching entry the channel map */
  103. mutex_lock(&iio_map_list_lock);
  104. list_for_each_entry(c_i, &iio_map_list, l) {
  105. if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
  106. (channel_name &&
  107. strcmp(channel_name, c_i->map->consumer_channel) != 0))
  108. continue;
  109. c = c_i;
  110. get_device(&c->indio_dev->dev);
  111. break;
  112. }
  113. mutex_unlock(&iio_map_list_lock);
  114. if (c == NULL)
  115. return ERR_PTR(-ENODEV);
  116. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  117. if (channel == NULL)
  118. return ERR_PTR(-ENOMEM);
  119. channel->indio_dev = c->indio_dev;
  120. if (c->map->adc_channel_label)
  121. channel->channel =
  122. iio_chan_spec_from_name(channel->indio_dev,
  123. c->map->adc_channel_label);
  124. return channel;
  125. }
  126. EXPORT_SYMBOL_GPL(iio_st_channel_get);
  127. void iio_st_channel_release(struct iio_channel *channel)
  128. {
  129. put_device(&channel->indio_dev->dev);
  130. kfree(channel);
  131. }
  132. EXPORT_SYMBOL_GPL(iio_st_channel_release);
  133. struct iio_channel *iio_st_channel_get_all(const char *name)
  134. {
  135. struct iio_channel *chans;
  136. struct iio_map_internal *c = NULL;
  137. int nummaps = 0;
  138. int mapind = 0;
  139. int i, ret;
  140. if (name == NULL)
  141. return ERR_PTR(-EINVAL);
  142. mutex_lock(&iio_map_list_lock);
  143. /* first count the matching maps */
  144. list_for_each_entry(c, &iio_map_list, l)
  145. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  146. continue;
  147. else
  148. nummaps++;
  149. if (nummaps == 0) {
  150. ret = -ENODEV;
  151. goto error_ret;
  152. }
  153. /* NULL terminated array to save passing size */
  154. chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
  155. if (chans == NULL) {
  156. ret = -ENOMEM;
  157. goto error_ret;
  158. }
  159. /* for each map fill in the chans element */
  160. list_for_each_entry(c, &iio_map_list, l) {
  161. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  162. continue;
  163. chans[mapind].indio_dev = c->indio_dev;
  164. chans[mapind].channel =
  165. iio_chan_spec_from_name(chans[mapind].indio_dev,
  166. c->map->adc_channel_label);
  167. if (chans[mapind].channel == NULL) {
  168. ret = -EINVAL;
  169. put_device(&chans[mapind].indio_dev->dev);
  170. goto error_free_chans;
  171. }
  172. get_device(&chans[mapind].indio_dev->dev);
  173. mapind++;
  174. }
  175. mutex_unlock(&iio_map_list_lock);
  176. if (mapind == 0) {
  177. ret = -ENODEV;
  178. goto error_free_chans;
  179. }
  180. return chans;
  181. error_free_chans:
  182. for (i = 0; i < nummaps; i++)
  183. if (chans[i].indio_dev)
  184. put_device(&chans[i].indio_dev->dev);
  185. kfree(chans);
  186. error_ret:
  187. mutex_unlock(&iio_map_list_lock);
  188. return ERR_PTR(ret);
  189. }
  190. EXPORT_SYMBOL_GPL(iio_st_channel_get_all);
  191. void iio_st_channel_release_all(struct iio_channel *channels)
  192. {
  193. struct iio_channel *chan = &channels[0];
  194. while (chan->indio_dev) {
  195. put_device(&chan->indio_dev->dev);
  196. chan++;
  197. }
  198. kfree(channels);
  199. }
  200. EXPORT_SYMBOL_GPL(iio_st_channel_release_all);
  201. int iio_st_read_channel_raw(struct iio_channel *chan, int *val)
  202. {
  203. int val2, ret;
  204. mutex_lock(&chan->indio_dev->info_exist_lock);
  205. if (chan->indio_dev->info == NULL) {
  206. ret = -ENODEV;
  207. goto err_unlock;
  208. }
  209. ret = chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
  210. val, &val2, 0);
  211. err_unlock:
  212. mutex_unlock(&chan->indio_dev->info_exist_lock);
  213. return ret;
  214. }
  215. EXPORT_SYMBOL_GPL(iio_st_read_channel_raw);
  216. int iio_st_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
  217. {
  218. int ret;
  219. mutex_lock(&chan->indio_dev->info_exist_lock);
  220. if (chan->indio_dev->info == NULL) {
  221. ret = -ENODEV;
  222. goto err_unlock;
  223. }
  224. ret = chan->indio_dev->info->read_raw(chan->indio_dev,
  225. chan->channel,
  226. val, val2,
  227. IIO_CHAN_INFO_SCALE);
  228. err_unlock:
  229. mutex_unlock(&chan->indio_dev->info_exist_lock);
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(iio_st_read_channel_scale);
  233. int iio_st_get_channel_type(struct iio_channel *chan,
  234. enum iio_chan_type *type)
  235. {
  236. int ret = 0;
  237. /* Need to verify underlying driver has not gone away */
  238. mutex_lock(&chan->indio_dev->info_exist_lock);
  239. if (chan->indio_dev->info == NULL) {
  240. ret = -ENODEV;
  241. goto err_unlock;
  242. }
  243. *type = chan->channel->type;
  244. err_unlock:
  245. mutex_unlock(&chan->indio_dev->info_exist_lock);
  246. return ret;
  247. }
  248. EXPORT_SYMBOL_GPL(iio_st_get_channel_type);