inkern.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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, const char *name)
  83. {
  84. int i;
  85. const struct iio_chan_spec *chan = NULL;
  86. for (i = 0; i < indio_dev->num_channels; i++)
  87. if (indio_dev->channels[i].datasheet_name &&
  88. strcmp(name, indio_dev->channels[i].datasheet_name) == 0) {
  89. chan = &indio_dev->channels[i];
  90. break;
  91. }
  92. return chan;
  93. }
  94. struct iio_channel *iio_channel_get(const char *name, const char *channel_name)
  95. {
  96. struct iio_map_internal *c_i = NULL, *c = NULL;
  97. struct iio_channel *channel;
  98. if (name == NULL && channel_name == NULL)
  99. return ERR_PTR(-ENODEV);
  100. /* first find matching entry the channel map */
  101. mutex_lock(&iio_map_list_lock);
  102. list_for_each_entry(c_i, &iio_map_list, l) {
  103. if ((name && strcmp(name, c_i->map->consumer_dev_name) != 0) ||
  104. (channel_name &&
  105. strcmp(channel_name, c_i->map->consumer_channel) != 0))
  106. continue;
  107. c = c_i;
  108. iio_device_get(c->indio_dev);
  109. break;
  110. }
  111. mutex_unlock(&iio_map_list_lock);
  112. if (c == NULL)
  113. return ERR_PTR(-ENODEV);
  114. channel = kmalloc(sizeof(*channel), GFP_KERNEL);
  115. if (channel == NULL)
  116. return ERR_PTR(-ENOMEM);
  117. channel->indio_dev = c->indio_dev;
  118. if (c->map->adc_channel_label)
  119. channel->channel =
  120. iio_chan_spec_from_name(channel->indio_dev,
  121. c->map->adc_channel_label);
  122. return channel;
  123. }
  124. EXPORT_SYMBOL_GPL(iio_channel_get);
  125. void iio_channel_release(struct iio_channel *channel)
  126. {
  127. iio_device_put(channel->indio_dev);
  128. kfree(channel);
  129. }
  130. EXPORT_SYMBOL_GPL(iio_channel_release);
  131. struct iio_channel *iio_channel_get_all(const char *name)
  132. {
  133. struct iio_channel *chans;
  134. struct iio_map_internal *c = NULL;
  135. int nummaps = 0;
  136. int mapind = 0;
  137. int i, ret;
  138. if (name == NULL)
  139. return ERR_PTR(-EINVAL);
  140. mutex_lock(&iio_map_list_lock);
  141. /* first count the matching maps */
  142. list_for_each_entry(c, &iio_map_list, l)
  143. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  144. continue;
  145. else
  146. nummaps++;
  147. if (nummaps == 0) {
  148. ret = -ENODEV;
  149. goto error_ret;
  150. }
  151. /* NULL terminated array to save passing size */
  152. chans = kzalloc(sizeof(*chans)*(nummaps + 1), GFP_KERNEL);
  153. if (chans == NULL) {
  154. ret = -ENOMEM;
  155. goto error_ret;
  156. }
  157. /* for each map fill in the chans element */
  158. list_for_each_entry(c, &iio_map_list, l) {
  159. if (name && strcmp(name, c->map->consumer_dev_name) != 0)
  160. continue;
  161. chans[mapind].indio_dev = c->indio_dev;
  162. chans[mapind].channel =
  163. iio_chan_spec_from_name(chans[mapind].indio_dev,
  164. c->map->adc_channel_label);
  165. if (chans[mapind].channel == NULL) {
  166. ret = -EINVAL;
  167. goto error_free_chans;
  168. }
  169. iio_device_get(chans[mapind].indio_dev);
  170. mapind++;
  171. }
  172. if (mapind == 0) {
  173. ret = -ENODEV;
  174. goto error_free_chans;
  175. }
  176. mutex_unlock(&iio_map_list_lock);
  177. return chans;
  178. error_free_chans:
  179. for (i = 0; i < nummaps; i++)
  180. iio_device_put(chans[i].indio_dev);
  181. kfree(chans);
  182. error_ret:
  183. mutex_unlock(&iio_map_list_lock);
  184. return ERR_PTR(ret);
  185. }
  186. EXPORT_SYMBOL_GPL(iio_channel_get_all);
  187. void iio_channel_release_all(struct iio_channel *channels)
  188. {
  189. struct iio_channel *chan = &channels[0];
  190. while (chan->indio_dev) {
  191. iio_device_put(chan->indio_dev);
  192. chan++;
  193. }
  194. kfree(channels);
  195. }
  196. EXPORT_SYMBOL_GPL(iio_channel_release_all);
  197. int iio_read_channel_raw(struct iio_channel *chan, int *val)
  198. {
  199. int val2, ret;
  200. mutex_lock(&chan->indio_dev->info_exist_lock);
  201. if (chan->indio_dev->info == NULL) {
  202. ret = -ENODEV;
  203. goto err_unlock;
  204. }
  205. ret = chan->indio_dev->info->read_raw(chan->indio_dev, chan->channel,
  206. val, &val2, 0);
  207. err_unlock:
  208. mutex_unlock(&chan->indio_dev->info_exist_lock);
  209. return ret;
  210. }
  211. EXPORT_SYMBOL_GPL(iio_read_channel_raw);
  212. int iio_read_channel_scale(struct iio_channel *chan, int *val, int *val2)
  213. {
  214. int ret;
  215. mutex_lock(&chan->indio_dev->info_exist_lock);
  216. if (chan->indio_dev->info == NULL) {
  217. ret = -ENODEV;
  218. goto err_unlock;
  219. }
  220. ret = chan->indio_dev->info->read_raw(chan->indio_dev,
  221. chan->channel,
  222. val, val2,
  223. IIO_CHAN_INFO_SCALE);
  224. err_unlock:
  225. mutex_unlock(&chan->indio_dev->info_exist_lock);
  226. return ret;
  227. }
  228. EXPORT_SYMBOL_GPL(iio_read_channel_scale);
  229. int iio_get_channel_type(struct iio_channel *chan, enum iio_chan_type *type)
  230. {
  231. int ret = 0;
  232. /* Need to verify underlying driver has not gone away */
  233. mutex_lock(&chan->indio_dev->info_exist_lock);
  234. if (chan->indio_dev->info == NULL) {
  235. ret = -ENODEV;
  236. goto err_unlock;
  237. }
  238. *type = chan->channel->type;
  239. err_unlock:
  240. mutex_unlock(&chan->indio_dev->info_exist_lock);
  241. return ret;
  242. }
  243. EXPORT_SYMBOL_GPL(iio_get_channel_type);