PageRenderTime 30ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/iio/industrialio-core.c

https://gitlab.com/buktemirlnk/mirrors
C | 1375 lines | 1055 code | 180 blank | 140 comment | 156 complexity | e7dc75a47ab6ab3c0c2044998d5bf43a MD5 | raw file
  1. /* The industrial I/O core
  2. *
  3. * Copyright (c) 2008 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. * Based on elements of hwmon and input subsystems.
  10. */
  11. #define pr_fmt(fmt) "iio-core: " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/idr.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/err.h>
  17. #include <linux/device.h>
  18. #include <linux/fs.h>
  19. #include <linux/poll.h>
  20. #include <linux/sched.h>
  21. #include <linux/wait.h>
  22. #include <linux/cdev.h>
  23. #include <linux/slab.h>
  24. #include <linux/anon_inodes.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/iio/iio.h>
  27. #include "iio_core.h"
  28. #include "iio_core_trigger.h"
  29. #include <linux/iio/sysfs.h>
  30. #include <linux/iio/events.h>
  31. #include <linux/iio/buffer.h>
  32. /* IDA to assign each registered device a unique id */
  33. static DEFINE_IDA(iio_ida);
  34. static dev_t iio_devt;
  35. #define IIO_DEV_MAX 256
  36. struct bus_type iio_bus_type = {
  37. .name = "iio",
  38. };
  39. EXPORT_SYMBOL(iio_bus_type);
  40. static struct dentry *iio_debugfs_dentry;
  41. static const char * const iio_direction[] = {
  42. [0] = "in",
  43. [1] = "out",
  44. };
  45. static const char * const iio_chan_type_name_spec[] = {
  46. [IIO_VOLTAGE] = "voltage",
  47. [IIO_CURRENT] = "current",
  48. [IIO_POWER] = "power",
  49. [IIO_ACCEL] = "accel",
  50. [IIO_ANGL_VEL] = "anglvel",
  51. [IIO_MAGN] = "magn",
  52. [IIO_LIGHT] = "illuminance",
  53. [IIO_INTENSITY] = "intensity",
  54. [IIO_PROXIMITY] = "proximity",
  55. [IIO_TEMP] = "temp",
  56. [IIO_INCLI] = "incli",
  57. [IIO_ROT] = "rot",
  58. [IIO_ANGL] = "angl",
  59. [IIO_TIMESTAMP] = "timestamp",
  60. [IIO_CAPACITANCE] = "capacitance",
  61. [IIO_ALTVOLTAGE] = "altvoltage",
  62. [IIO_CCT] = "cct",
  63. [IIO_PRESSURE] = "pressure",
  64. [IIO_HUMIDITYRELATIVE] = "humidityrelative",
  65. [IIO_ACTIVITY] = "activity",
  66. [IIO_STEPS] = "steps",
  67. [IIO_ENERGY] = "energy",
  68. [IIO_DISTANCE] = "distance",
  69. [IIO_VELOCITY] = "velocity",
  70. [IIO_CONCENTRATION] = "concentration",
  71. [IIO_RESISTANCE] = "resistance",
  72. [IIO_QUATERNION] = "quaternion",
  73. };
  74. static const char * const iio_modifier_names[] = {
  75. [IIO_MOD_X] = "x",
  76. [IIO_MOD_Y] = "y",
  77. [IIO_MOD_Z] = "z",
  78. [IIO_MOD_X_AND_Y] = "x&y",
  79. [IIO_MOD_X_AND_Z] = "x&z",
  80. [IIO_MOD_Y_AND_Z] = "y&z",
  81. [IIO_MOD_X_AND_Y_AND_Z] = "x&y&z",
  82. [IIO_MOD_X_OR_Y] = "x|y",
  83. [IIO_MOD_X_OR_Z] = "x|z",
  84. [IIO_MOD_Y_OR_Z] = "y|z",
  85. [IIO_MOD_X_OR_Y_OR_Z] = "x|y|z",
  86. [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
  87. [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
  88. [IIO_MOD_LIGHT_BOTH] = "both",
  89. [IIO_MOD_LIGHT_IR] = "ir",
  90. [IIO_MOD_LIGHT_CLEAR] = "clear",
  91. [IIO_MOD_LIGHT_RED] = "red",
  92. [IIO_MOD_LIGHT_GREEN] = "green",
  93. [IIO_MOD_LIGHT_BLUE] = "blue",
  94. [IIO_MOD_QUATERNION] = "quaternion",
  95. [IIO_MOD_TEMP_AMBIENT] = "ambient",
  96. [IIO_MOD_TEMP_OBJECT] = "object",
  97. [IIO_MOD_NORTH_MAGN] = "from_north_magnetic",
  98. [IIO_MOD_NORTH_TRUE] = "from_north_true",
  99. [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
  100. [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
  101. [IIO_MOD_RUNNING] = "running",
  102. [IIO_MOD_JOGGING] = "jogging",
  103. [IIO_MOD_WALKING] = "walking",
  104. [IIO_MOD_STILL] = "still",
  105. [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
  106. [IIO_MOD_I] = "i",
  107. [IIO_MOD_Q] = "q",
  108. [IIO_MOD_CO2] = "co2",
  109. [IIO_MOD_VOC] = "voc",
  110. [IIO_MOD_R] = "r",
  111. };
  112. /* relies on pairs of these shared then separate */
  113. static const char * const iio_chan_info_postfix[] = {
  114. [IIO_CHAN_INFO_RAW] = "raw",
  115. [IIO_CHAN_INFO_PROCESSED] = "input",
  116. [IIO_CHAN_INFO_SCALE] = "scale",
  117. [IIO_CHAN_INFO_OFFSET] = "offset",
  118. [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
  119. [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
  120. [IIO_CHAN_INFO_PEAK] = "peak_raw",
  121. [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
  122. [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
  123. [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
  124. [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
  125. = "filter_low_pass_3db_frequency",
  126. [IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY]
  127. = "filter_high_pass_3db_frequency",
  128. [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
  129. [IIO_CHAN_INFO_FREQUENCY] = "frequency",
  130. [IIO_CHAN_INFO_PHASE] = "phase",
  131. [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
  132. [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
  133. [IIO_CHAN_INFO_INT_TIME] = "integration_time",
  134. [IIO_CHAN_INFO_ENABLE] = "en",
  135. [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
  136. [IIO_CHAN_INFO_CALIBWEIGHT] = "calibweight",
  137. [IIO_CHAN_INFO_DEBOUNCE_COUNT] = "debounce_count",
  138. [IIO_CHAN_INFO_DEBOUNCE_TIME] = "debounce_time",
  139. [IIO_CHAN_INFO_CALIBEMISSIVITY] = "calibemissivity",
  140. [IIO_CHAN_INFO_OVERSAMPLING_RATIO] = "oversampling_ratio",
  141. };
  142. /**
  143. * iio_find_channel_from_si() - get channel from its scan index
  144. * @indio_dev: device
  145. * @si: scan index to match
  146. */
  147. const struct iio_chan_spec
  148. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
  149. {
  150. int i;
  151. for (i = 0; i < indio_dev->num_channels; i++)
  152. if (indio_dev->channels[i].scan_index == si)
  153. return &indio_dev->channels[i];
  154. return NULL;
  155. }
  156. /* This turns up an awful lot */
  157. ssize_t iio_read_const_attr(struct device *dev,
  158. struct device_attribute *attr,
  159. char *buf)
  160. {
  161. return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
  162. }
  163. EXPORT_SYMBOL(iio_read_const_attr);
  164. static int __init iio_init(void)
  165. {
  166. int ret;
  167. /* Register sysfs bus */
  168. ret = bus_register(&iio_bus_type);
  169. if (ret < 0) {
  170. pr_err("could not register bus type\n");
  171. goto error_nothing;
  172. }
  173. ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
  174. if (ret < 0) {
  175. pr_err("failed to allocate char dev region\n");
  176. goto error_unregister_bus_type;
  177. }
  178. iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
  179. return 0;
  180. error_unregister_bus_type:
  181. bus_unregister(&iio_bus_type);
  182. error_nothing:
  183. return ret;
  184. }
  185. static void __exit iio_exit(void)
  186. {
  187. if (iio_devt)
  188. unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
  189. bus_unregister(&iio_bus_type);
  190. debugfs_remove(iio_debugfs_dentry);
  191. }
  192. #if defined(CONFIG_DEBUG_FS)
  193. static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
  194. size_t count, loff_t *ppos)
  195. {
  196. struct iio_dev *indio_dev = file->private_data;
  197. char buf[20];
  198. unsigned val = 0;
  199. ssize_t len;
  200. int ret;
  201. ret = indio_dev->info->debugfs_reg_access(indio_dev,
  202. indio_dev->cached_reg_addr,
  203. 0, &val);
  204. if (ret) {
  205. dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
  206. return ret;
  207. }
  208. len = snprintf(buf, sizeof(buf), "0x%X\n", val);
  209. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  210. }
  211. static ssize_t iio_debugfs_write_reg(struct file *file,
  212. const char __user *userbuf, size_t count, loff_t *ppos)
  213. {
  214. struct iio_dev *indio_dev = file->private_data;
  215. unsigned reg, val;
  216. char buf[80];
  217. int ret;
  218. count = min_t(size_t, count, (sizeof(buf)-1));
  219. if (copy_from_user(buf, userbuf, count))
  220. return -EFAULT;
  221. buf[count] = 0;
  222. ret = sscanf(buf, "%i %i", &reg, &val);
  223. switch (ret) {
  224. case 1:
  225. indio_dev->cached_reg_addr = reg;
  226. break;
  227. case 2:
  228. indio_dev->cached_reg_addr = reg;
  229. ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
  230. val, NULL);
  231. if (ret) {
  232. dev_err(indio_dev->dev.parent, "%s: write failed\n",
  233. __func__);
  234. return ret;
  235. }
  236. break;
  237. default:
  238. return -EINVAL;
  239. }
  240. return count;
  241. }
  242. static const struct file_operations iio_debugfs_reg_fops = {
  243. .open = simple_open,
  244. .read = iio_debugfs_read_reg,
  245. .write = iio_debugfs_write_reg,
  246. };
  247. static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
  248. {
  249. debugfs_remove_recursive(indio_dev->debugfs_dentry);
  250. }
  251. static int iio_device_register_debugfs(struct iio_dev *indio_dev)
  252. {
  253. struct dentry *d;
  254. if (indio_dev->info->debugfs_reg_access == NULL)
  255. return 0;
  256. if (!iio_debugfs_dentry)
  257. return 0;
  258. indio_dev->debugfs_dentry =
  259. debugfs_create_dir(dev_name(&indio_dev->dev),
  260. iio_debugfs_dentry);
  261. if (indio_dev->debugfs_dentry == NULL) {
  262. dev_warn(indio_dev->dev.parent,
  263. "Failed to create debugfs directory\n");
  264. return -EFAULT;
  265. }
  266. d = debugfs_create_file("direct_reg_access", 0644,
  267. indio_dev->debugfs_dentry,
  268. indio_dev, &iio_debugfs_reg_fops);
  269. if (!d) {
  270. iio_device_unregister_debugfs(indio_dev);
  271. return -ENOMEM;
  272. }
  273. return 0;
  274. }
  275. #else
  276. static int iio_device_register_debugfs(struct iio_dev *indio_dev)
  277. {
  278. return 0;
  279. }
  280. static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
  281. {
  282. }
  283. #endif /* CONFIG_DEBUG_FS */
  284. static ssize_t iio_read_channel_ext_info(struct device *dev,
  285. struct device_attribute *attr,
  286. char *buf)
  287. {
  288. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  289. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  290. const struct iio_chan_spec_ext_info *ext_info;
  291. ext_info = &this_attr->c->ext_info[this_attr->address];
  292. return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
  293. }
  294. static ssize_t iio_write_channel_ext_info(struct device *dev,
  295. struct device_attribute *attr,
  296. const char *buf,
  297. size_t len)
  298. {
  299. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  300. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  301. const struct iio_chan_spec_ext_info *ext_info;
  302. ext_info = &this_attr->c->ext_info[this_attr->address];
  303. return ext_info->write(indio_dev, ext_info->private,
  304. this_attr->c, buf, len);
  305. }
  306. ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
  307. uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
  308. {
  309. const struct iio_enum *e = (const struct iio_enum *)priv;
  310. unsigned int i;
  311. size_t len = 0;
  312. if (!e->num_items)
  313. return 0;
  314. for (i = 0; i < e->num_items; ++i)
  315. len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
  316. /* replace last space with a newline */
  317. buf[len - 1] = '\n';
  318. return len;
  319. }
  320. EXPORT_SYMBOL_GPL(iio_enum_available_read);
  321. ssize_t iio_enum_read(struct iio_dev *indio_dev,
  322. uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
  323. {
  324. const struct iio_enum *e = (const struct iio_enum *)priv;
  325. int i;
  326. if (!e->get)
  327. return -EINVAL;
  328. i = e->get(indio_dev, chan);
  329. if (i < 0)
  330. return i;
  331. else if (i >= e->num_items)
  332. return -EINVAL;
  333. return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]);
  334. }
  335. EXPORT_SYMBOL_GPL(iio_enum_read);
  336. ssize_t iio_enum_write(struct iio_dev *indio_dev,
  337. uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
  338. size_t len)
  339. {
  340. const struct iio_enum *e = (const struct iio_enum *)priv;
  341. unsigned int i;
  342. int ret;
  343. if (!e->set)
  344. return -EINVAL;
  345. for (i = 0; i < e->num_items; i++) {
  346. if (sysfs_streq(buf, e->items[i]))
  347. break;
  348. }
  349. if (i == e->num_items)
  350. return -EINVAL;
  351. ret = e->set(indio_dev, chan, i);
  352. return ret ? ret : len;
  353. }
  354. EXPORT_SYMBOL_GPL(iio_enum_write);
  355. /**
  356. * iio_format_value() - Formats a IIO value into its string representation
  357. * @buf: The buffer to which the formatted value gets written
  358. * @type: One of the IIO_VAL_... constants. This decides how the val
  359. * and val2 parameters are formatted.
  360. * @size: Number of IIO value entries contained in vals
  361. * @vals: Pointer to the values, exact meaning depends on the
  362. * type parameter.
  363. *
  364. * Return: 0 by default, a negative number on failure or the
  365. * total number of characters written for a type that belongs
  366. * to the IIO_VAL_... constant.
  367. */
  368. ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals)
  369. {
  370. unsigned long long tmp;
  371. bool scale_db = false;
  372. switch (type) {
  373. case IIO_VAL_INT:
  374. return sprintf(buf, "%d\n", vals[0]);
  375. case IIO_VAL_INT_PLUS_MICRO_DB:
  376. scale_db = true;
  377. case IIO_VAL_INT_PLUS_MICRO:
  378. if (vals[1] < 0)
  379. return sprintf(buf, "-%d.%06u%s\n", abs(vals[0]),
  380. -vals[1], scale_db ? " dB" : "");
  381. else
  382. return sprintf(buf, "%d.%06u%s\n", vals[0], vals[1],
  383. scale_db ? " dB" : "");
  384. case IIO_VAL_INT_PLUS_NANO:
  385. if (vals[1] < 0)
  386. return sprintf(buf, "-%d.%09u\n", abs(vals[0]),
  387. -vals[1]);
  388. else
  389. return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
  390. case IIO_VAL_FRACTIONAL:
  391. tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]);
  392. vals[0] = (int)div_s64_rem(tmp, 1000000000, &vals[1]);
  393. return sprintf(buf, "%d.%09u\n", vals[0], abs(vals[1]));
  394. case IIO_VAL_FRACTIONAL_LOG2:
  395. tmp = (s64)vals[0] * 1000000000LL >> vals[1];
  396. vals[1] = do_div(tmp, 1000000000LL);
  397. vals[0] = tmp;
  398. return sprintf(buf, "%d.%09u\n", vals[0], vals[1]);
  399. case IIO_VAL_INT_MULTIPLE:
  400. {
  401. int i;
  402. int len = 0;
  403. for (i = 0; i < size; ++i)
  404. len += snprintf(&buf[len], PAGE_SIZE - len, "%d ",
  405. vals[i]);
  406. len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
  407. return len;
  408. }
  409. default:
  410. return 0;
  411. }
  412. }
  413. static ssize_t iio_read_channel_info(struct device *dev,
  414. struct device_attribute *attr,
  415. char *buf)
  416. {
  417. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  418. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  419. int vals[INDIO_MAX_RAW_ELEMENTS];
  420. int ret;
  421. int val_len = 2;
  422. if (indio_dev->info->read_raw_multi)
  423. ret = indio_dev->info->read_raw_multi(indio_dev, this_attr->c,
  424. INDIO_MAX_RAW_ELEMENTS,
  425. vals, &val_len,
  426. this_attr->address);
  427. else
  428. ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
  429. &vals[0], &vals[1], this_attr->address);
  430. if (ret < 0)
  431. return ret;
  432. return iio_format_value(buf, ret, val_len, vals);
  433. }
  434. /**
  435. * iio_str_to_fixpoint() - Parse a fixed-point number from a string
  436. * @str: The string to parse
  437. * @fract_mult: Multiplier for the first decimal place, should be a power of 10
  438. * @integer: The integer part of the number
  439. * @fract: The fractional part of the number
  440. *
  441. * Returns 0 on success, or a negative error code if the string could not be
  442. * parsed.
  443. */
  444. int iio_str_to_fixpoint(const char *str, int fract_mult,
  445. int *integer, int *fract)
  446. {
  447. int i = 0, f = 0;
  448. bool integer_part = true, negative = false;
  449. if (str[0] == '-') {
  450. negative = true;
  451. str++;
  452. } else if (str[0] == '+') {
  453. str++;
  454. }
  455. while (*str) {
  456. if ('0' <= *str && *str <= '9') {
  457. if (integer_part) {
  458. i = i * 10 + *str - '0';
  459. } else {
  460. f += fract_mult * (*str - '0');
  461. fract_mult /= 10;
  462. }
  463. } else if (*str == '\n') {
  464. if (*(str + 1) == '\0')
  465. break;
  466. else
  467. return -EINVAL;
  468. } else if (*str == '.' && integer_part) {
  469. integer_part = false;
  470. } else {
  471. return -EINVAL;
  472. }
  473. str++;
  474. }
  475. if (negative) {
  476. if (i)
  477. i = -i;
  478. else
  479. f = -f;
  480. }
  481. *integer = i;
  482. *fract = f;
  483. return 0;
  484. }
  485. EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
  486. static ssize_t iio_write_channel_info(struct device *dev,
  487. struct device_attribute *attr,
  488. const char *buf,
  489. size_t len)
  490. {
  491. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  492. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  493. int ret, fract_mult = 100000;
  494. int integer, fract;
  495. /* Assumes decimal - precision based on number of digits */
  496. if (!indio_dev->info->write_raw)
  497. return -EINVAL;
  498. if (indio_dev->info->write_raw_get_fmt)
  499. switch (indio_dev->info->write_raw_get_fmt(indio_dev,
  500. this_attr->c, this_attr->address)) {
  501. case IIO_VAL_INT_PLUS_MICRO:
  502. fract_mult = 100000;
  503. break;
  504. case IIO_VAL_INT_PLUS_NANO:
  505. fract_mult = 100000000;
  506. break;
  507. default:
  508. return -EINVAL;
  509. }
  510. ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
  511. if (ret)
  512. return ret;
  513. ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
  514. integer, fract, this_attr->address);
  515. if (ret)
  516. return ret;
  517. return len;
  518. }
  519. static
  520. int __iio_device_attr_init(struct device_attribute *dev_attr,
  521. const char *postfix,
  522. struct iio_chan_spec const *chan,
  523. ssize_t (*readfunc)(struct device *dev,
  524. struct device_attribute *attr,
  525. char *buf),
  526. ssize_t (*writefunc)(struct device *dev,
  527. struct device_attribute *attr,
  528. const char *buf,
  529. size_t len),
  530. enum iio_shared_by shared_by)
  531. {
  532. int ret = 0;
  533. char *name = NULL;
  534. char *full_postfix;
  535. sysfs_attr_init(&dev_attr->attr);
  536. /* Build up postfix of <extend_name>_<modifier>_postfix */
  537. if (chan->modified && (shared_by == IIO_SEPARATE)) {
  538. if (chan->extend_name)
  539. full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  540. iio_modifier_names[chan
  541. ->channel2],
  542. chan->extend_name,
  543. postfix);
  544. else
  545. full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
  546. iio_modifier_names[chan
  547. ->channel2],
  548. postfix);
  549. } else {
  550. if (chan->extend_name == NULL || shared_by != IIO_SEPARATE)
  551. full_postfix = kstrdup(postfix, GFP_KERNEL);
  552. else
  553. full_postfix = kasprintf(GFP_KERNEL,
  554. "%s_%s",
  555. chan->extend_name,
  556. postfix);
  557. }
  558. if (full_postfix == NULL)
  559. return -ENOMEM;
  560. if (chan->differential) { /* Differential can not have modifier */
  561. switch (shared_by) {
  562. case IIO_SHARED_BY_ALL:
  563. name = kasprintf(GFP_KERNEL, "%s", full_postfix);
  564. break;
  565. case IIO_SHARED_BY_DIR:
  566. name = kasprintf(GFP_KERNEL, "%s_%s",
  567. iio_direction[chan->output],
  568. full_postfix);
  569. break;
  570. case IIO_SHARED_BY_TYPE:
  571. name = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
  572. iio_direction[chan->output],
  573. iio_chan_type_name_spec[chan->type],
  574. iio_chan_type_name_spec[chan->type],
  575. full_postfix);
  576. break;
  577. case IIO_SEPARATE:
  578. if (!chan->indexed) {
  579. WARN(1, "Differential channels must be indexed\n");
  580. ret = -EINVAL;
  581. goto error_free_full_postfix;
  582. }
  583. name = kasprintf(GFP_KERNEL,
  584. "%s_%s%d-%s%d_%s",
  585. iio_direction[chan->output],
  586. iio_chan_type_name_spec[chan->type],
  587. chan->channel,
  588. iio_chan_type_name_spec[chan->type],
  589. chan->channel2,
  590. full_postfix);
  591. break;
  592. }
  593. } else { /* Single ended */
  594. switch (shared_by) {
  595. case IIO_SHARED_BY_ALL:
  596. name = kasprintf(GFP_KERNEL, "%s", full_postfix);
  597. break;
  598. case IIO_SHARED_BY_DIR:
  599. name = kasprintf(GFP_KERNEL, "%s_%s",
  600. iio_direction[chan->output],
  601. full_postfix);
  602. break;
  603. case IIO_SHARED_BY_TYPE:
  604. name = kasprintf(GFP_KERNEL, "%s_%s_%s",
  605. iio_direction[chan->output],
  606. iio_chan_type_name_spec[chan->type],
  607. full_postfix);
  608. break;
  609. case IIO_SEPARATE:
  610. if (chan->indexed)
  611. name = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
  612. iio_direction[chan->output],
  613. iio_chan_type_name_spec[chan->type],
  614. chan->channel,
  615. full_postfix);
  616. else
  617. name = kasprintf(GFP_KERNEL, "%s_%s_%s",
  618. iio_direction[chan->output],
  619. iio_chan_type_name_spec[chan->type],
  620. full_postfix);
  621. break;
  622. }
  623. }
  624. if (name == NULL) {
  625. ret = -ENOMEM;
  626. goto error_free_full_postfix;
  627. }
  628. dev_attr->attr.name = name;
  629. if (readfunc) {
  630. dev_attr->attr.mode |= S_IRUGO;
  631. dev_attr->show = readfunc;
  632. }
  633. if (writefunc) {
  634. dev_attr->attr.mode |= S_IWUSR;
  635. dev_attr->store = writefunc;
  636. }
  637. error_free_full_postfix:
  638. kfree(full_postfix);
  639. return ret;
  640. }
  641. static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
  642. {
  643. kfree(dev_attr->attr.name);
  644. }
  645. int __iio_add_chan_devattr(const char *postfix,
  646. struct iio_chan_spec const *chan,
  647. ssize_t (*readfunc)(struct device *dev,
  648. struct device_attribute *attr,
  649. char *buf),
  650. ssize_t (*writefunc)(struct device *dev,
  651. struct device_attribute *attr,
  652. const char *buf,
  653. size_t len),
  654. u64 mask,
  655. enum iio_shared_by shared_by,
  656. struct device *dev,
  657. struct list_head *attr_list)
  658. {
  659. int ret;
  660. struct iio_dev_attr *iio_attr, *t;
  661. iio_attr = kzalloc(sizeof(*iio_attr), GFP_KERNEL);
  662. if (iio_attr == NULL)
  663. return -ENOMEM;
  664. ret = __iio_device_attr_init(&iio_attr->dev_attr,
  665. postfix, chan,
  666. readfunc, writefunc, shared_by);
  667. if (ret)
  668. goto error_iio_dev_attr_free;
  669. iio_attr->c = chan;
  670. iio_attr->address = mask;
  671. list_for_each_entry(t, attr_list, l)
  672. if (strcmp(t->dev_attr.attr.name,
  673. iio_attr->dev_attr.attr.name) == 0) {
  674. if (shared_by == IIO_SEPARATE)
  675. dev_err(dev, "tried to double register : %s\n",
  676. t->dev_attr.attr.name);
  677. ret = -EBUSY;
  678. goto error_device_attr_deinit;
  679. }
  680. list_add(&iio_attr->l, attr_list);
  681. return 0;
  682. error_device_attr_deinit:
  683. __iio_device_attr_deinit(&iio_attr->dev_attr);
  684. error_iio_dev_attr_free:
  685. kfree(iio_attr);
  686. return ret;
  687. }
  688. static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
  689. struct iio_chan_spec const *chan,
  690. enum iio_shared_by shared_by,
  691. const long *infomask)
  692. {
  693. int i, ret, attrcount = 0;
  694. for_each_set_bit(i, infomask, sizeof(infomask)*8) {
  695. if (i >= ARRAY_SIZE(iio_chan_info_postfix))
  696. return -EINVAL;
  697. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  698. chan,
  699. &iio_read_channel_info,
  700. &iio_write_channel_info,
  701. i,
  702. shared_by,
  703. &indio_dev->dev,
  704. &indio_dev->channel_attr_list);
  705. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  706. continue;
  707. else if (ret < 0)
  708. return ret;
  709. attrcount++;
  710. }
  711. return attrcount;
  712. }
  713. static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
  714. struct iio_chan_spec const *chan)
  715. {
  716. int ret, attrcount = 0;
  717. const struct iio_chan_spec_ext_info *ext_info;
  718. if (chan->channel < 0)
  719. return 0;
  720. ret = iio_device_add_info_mask_type(indio_dev, chan,
  721. IIO_SEPARATE,
  722. &chan->info_mask_separate);
  723. if (ret < 0)
  724. return ret;
  725. attrcount += ret;
  726. ret = iio_device_add_info_mask_type(indio_dev, chan,
  727. IIO_SHARED_BY_TYPE,
  728. &chan->info_mask_shared_by_type);
  729. if (ret < 0)
  730. return ret;
  731. attrcount += ret;
  732. ret = iio_device_add_info_mask_type(indio_dev, chan,
  733. IIO_SHARED_BY_DIR,
  734. &chan->info_mask_shared_by_dir);
  735. if (ret < 0)
  736. return ret;
  737. attrcount += ret;
  738. ret = iio_device_add_info_mask_type(indio_dev, chan,
  739. IIO_SHARED_BY_ALL,
  740. &chan->info_mask_shared_by_all);
  741. if (ret < 0)
  742. return ret;
  743. attrcount += ret;
  744. if (chan->ext_info) {
  745. unsigned int i = 0;
  746. for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
  747. ret = __iio_add_chan_devattr(ext_info->name,
  748. chan,
  749. ext_info->read ?
  750. &iio_read_channel_ext_info : NULL,
  751. ext_info->write ?
  752. &iio_write_channel_ext_info : NULL,
  753. i,
  754. ext_info->shared,
  755. &indio_dev->dev,
  756. &indio_dev->channel_attr_list);
  757. i++;
  758. if (ret == -EBUSY && ext_info->shared)
  759. continue;
  760. if (ret)
  761. return ret;
  762. attrcount++;
  763. }
  764. }
  765. return attrcount;
  766. }
  767. /**
  768. * iio_free_chan_devattr_list() - Free a list of IIO device attributes
  769. * @attr_list: List of IIO device attributes
  770. *
  771. * This function frees the memory allocated for each of the IIO device
  772. * attributes in the list.
  773. */
  774. void iio_free_chan_devattr_list(struct list_head *attr_list)
  775. {
  776. struct iio_dev_attr *p, *n;
  777. list_for_each_entry_safe(p, n, attr_list, l) {
  778. kfree(p->dev_attr.attr.name);
  779. list_del(&p->l);
  780. kfree(p);
  781. }
  782. }
  783. static ssize_t iio_show_dev_name(struct device *dev,
  784. struct device_attribute *attr,
  785. char *buf)
  786. {
  787. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  788. return snprintf(buf, PAGE_SIZE, "%s\n", indio_dev->name);
  789. }
  790. static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
  791. static int iio_device_register_sysfs(struct iio_dev *indio_dev)
  792. {
  793. int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
  794. struct iio_dev_attr *p;
  795. struct attribute **attr;
  796. /* First count elements in any existing group */
  797. if (indio_dev->info->attrs) {
  798. attr = indio_dev->info->attrs->attrs;
  799. while (*attr++ != NULL)
  800. attrcount_orig++;
  801. }
  802. attrcount = attrcount_orig;
  803. /*
  804. * New channel registration method - relies on the fact a group does
  805. * not need to be initialized if its name is NULL.
  806. */
  807. if (indio_dev->channels)
  808. for (i = 0; i < indio_dev->num_channels; i++) {
  809. ret = iio_device_add_channel_sysfs(indio_dev,
  810. &indio_dev
  811. ->channels[i]);
  812. if (ret < 0)
  813. goto error_clear_attrs;
  814. attrcount += ret;
  815. }
  816. if (indio_dev->name)
  817. attrcount++;
  818. indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
  819. sizeof(indio_dev->chan_attr_group.attrs[0]),
  820. GFP_KERNEL);
  821. if (indio_dev->chan_attr_group.attrs == NULL) {
  822. ret = -ENOMEM;
  823. goto error_clear_attrs;
  824. }
  825. /* Copy across original attributes */
  826. if (indio_dev->info->attrs)
  827. memcpy(indio_dev->chan_attr_group.attrs,
  828. indio_dev->info->attrs->attrs,
  829. sizeof(indio_dev->chan_attr_group.attrs[0])
  830. *attrcount_orig);
  831. attrn = attrcount_orig;
  832. /* Add all elements from the list. */
  833. list_for_each_entry(p, &indio_dev->channel_attr_list, l)
  834. indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
  835. if (indio_dev->name)
  836. indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
  837. indio_dev->groups[indio_dev->groupcounter++] =
  838. &indio_dev->chan_attr_group;
  839. return 0;
  840. error_clear_attrs:
  841. iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
  842. return ret;
  843. }
  844. static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
  845. {
  846. iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
  847. kfree(indio_dev->chan_attr_group.attrs);
  848. indio_dev->chan_attr_group.attrs = NULL;
  849. }
  850. static void iio_dev_release(struct device *device)
  851. {
  852. struct iio_dev *indio_dev = dev_to_iio_dev(device);
  853. if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
  854. iio_device_unregister_trigger_consumer(indio_dev);
  855. iio_device_unregister_eventset(indio_dev);
  856. iio_device_unregister_sysfs(indio_dev);
  857. iio_buffer_put(indio_dev->buffer);
  858. ida_simple_remove(&iio_ida, indio_dev->id);
  859. kfree(indio_dev);
  860. }
  861. struct device_type iio_device_type = {
  862. .name = "iio_device",
  863. .release = iio_dev_release,
  864. };
  865. /**
  866. * iio_device_alloc() - allocate an iio_dev from a driver
  867. * @sizeof_priv: Space to allocate for private structure.
  868. **/
  869. struct iio_dev *iio_device_alloc(int sizeof_priv)
  870. {
  871. struct iio_dev *dev;
  872. size_t alloc_size;
  873. alloc_size = sizeof(struct iio_dev);
  874. if (sizeof_priv) {
  875. alloc_size = ALIGN(alloc_size, IIO_ALIGN);
  876. alloc_size += sizeof_priv;
  877. }
  878. /* ensure 32-byte alignment of whole construct ? */
  879. alloc_size += IIO_ALIGN - 1;
  880. dev = kzalloc(alloc_size, GFP_KERNEL);
  881. if (dev) {
  882. dev->dev.groups = dev->groups;
  883. dev->dev.type = &iio_device_type;
  884. dev->dev.bus = &iio_bus_type;
  885. device_initialize(&dev->dev);
  886. dev_set_drvdata(&dev->dev, (void *)dev);
  887. mutex_init(&dev->mlock);
  888. mutex_init(&dev->info_exist_lock);
  889. INIT_LIST_HEAD(&dev->channel_attr_list);
  890. dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
  891. if (dev->id < 0) {
  892. /* cannot use a dev_err as the name isn't available */
  893. pr_err("failed to get device id\n");
  894. kfree(dev);
  895. return NULL;
  896. }
  897. dev_set_name(&dev->dev, "iio:device%d", dev->id);
  898. INIT_LIST_HEAD(&dev->buffer_list);
  899. }
  900. return dev;
  901. }
  902. EXPORT_SYMBOL(iio_device_alloc);
  903. /**
  904. * iio_device_free() - free an iio_dev from a driver
  905. * @dev: the iio_dev associated with the device
  906. **/
  907. void iio_device_free(struct iio_dev *dev)
  908. {
  909. if (dev)
  910. put_device(&dev->dev);
  911. }
  912. EXPORT_SYMBOL(iio_device_free);
  913. static void devm_iio_device_release(struct device *dev, void *res)
  914. {
  915. iio_device_free(*(struct iio_dev **)res);
  916. }
  917. static int devm_iio_device_match(struct device *dev, void *res, void *data)
  918. {
  919. struct iio_dev **r = res;
  920. if (!r || !*r) {
  921. WARN_ON(!r || !*r);
  922. return 0;
  923. }
  924. return *r == data;
  925. }
  926. /**
  927. * devm_iio_device_alloc - Resource-managed iio_device_alloc()
  928. * @dev: Device to allocate iio_dev for
  929. * @sizeof_priv: Space to allocate for private structure.
  930. *
  931. * Managed iio_device_alloc. iio_dev allocated with this function is
  932. * automatically freed on driver detach.
  933. *
  934. * If an iio_dev allocated with this function needs to be freed separately,
  935. * devm_iio_device_free() must be used.
  936. *
  937. * RETURNS:
  938. * Pointer to allocated iio_dev on success, NULL on failure.
  939. */
  940. struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
  941. {
  942. struct iio_dev **ptr, *iio_dev;
  943. ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
  944. GFP_KERNEL);
  945. if (!ptr)
  946. return NULL;
  947. iio_dev = iio_device_alloc(sizeof_priv);
  948. if (iio_dev) {
  949. *ptr = iio_dev;
  950. devres_add(dev, ptr);
  951. } else {
  952. devres_free(ptr);
  953. }
  954. return iio_dev;
  955. }
  956. EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
  957. /**
  958. * devm_iio_device_free - Resource-managed iio_device_free()
  959. * @dev: Device this iio_dev belongs to
  960. * @iio_dev: the iio_dev associated with the device
  961. *
  962. * Free iio_dev allocated with devm_iio_device_alloc().
  963. */
  964. void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
  965. {
  966. int rc;
  967. rc = devres_release(dev, devm_iio_device_release,
  968. devm_iio_device_match, iio_dev);
  969. WARN_ON(rc);
  970. }
  971. EXPORT_SYMBOL_GPL(devm_iio_device_free);
  972. /**
  973. * iio_chrdev_open() - chrdev file open for buffer access and ioctls
  974. * @inode: Inode structure for identifying the device in the file system
  975. * @filp: File structure for iio device used to keep and later access
  976. * private data
  977. *
  978. * Return: 0 on success or -EBUSY if the device is already opened
  979. **/
  980. static int iio_chrdev_open(struct inode *inode, struct file *filp)
  981. {
  982. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  983. struct iio_dev, chrdev);
  984. if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
  985. return -EBUSY;
  986. iio_device_get(indio_dev);
  987. filp->private_data = indio_dev;
  988. return 0;
  989. }
  990. /**
  991. * iio_chrdev_release() - chrdev file close buffer access and ioctls
  992. * @inode: Inode structure pointer for the char device
  993. * @filp: File structure pointer for the char device
  994. *
  995. * Return: 0 for successful release
  996. */
  997. static int iio_chrdev_release(struct inode *inode, struct file *filp)
  998. {
  999. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  1000. struct iio_dev, chrdev);
  1001. clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
  1002. iio_device_put(indio_dev);
  1003. return 0;
  1004. }
  1005. /* Somewhat of a cross file organization violation - ioctls here are actually
  1006. * event related */
  1007. static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  1008. {
  1009. struct iio_dev *indio_dev = filp->private_data;
  1010. int __user *ip = (int __user *)arg;
  1011. int fd;
  1012. if (!indio_dev->info)
  1013. return -ENODEV;
  1014. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  1015. fd = iio_event_getfd(indio_dev);
  1016. if (fd < 0)
  1017. return fd;
  1018. if (copy_to_user(ip, &fd, sizeof(fd)))
  1019. return -EFAULT;
  1020. return 0;
  1021. }
  1022. return -EINVAL;
  1023. }
  1024. static const struct file_operations iio_buffer_fileops = {
  1025. .read = iio_buffer_read_first_n_outer_addr,
  1026. .release = iio_chrdev_release,
  1027. .open = iio_chrdev_open,
  1028. .poll = iio_buffer_poll_addr,
  1029. .owner = THIS_MODULE,
  1030. .llseek = noop_llseek,
  1031. .unlocked_ioctl = iio_ioctl,
  1032. .compat_ioctl = iio_ioctl,
  1033. };
  1034. static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
  1035. {
  1036. int i, j;
  1037. const struct iio_chan_spec *channels = indio_dev->channels;
  1038. if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
  1039. return 0;
  1040. for (i = 0; i < indio_dev->num_channels - 1; i++) {
  1041. if (channels[i].scan_index < 0)
  1042. continue;
  1043. for (j = i + 1; j < indio_dev->num_channels; j++)
  1044. if (channels[i].scan_index == channels[j].scan_index) {
  1045. dev_err(&indio_dev->dev,
  1046. "Duplicate scan index %d\n",
  1047. channels[i].scan_index);
  1048. return -EINVAL;
  1049. }
  1050. }
  1051. return 0;
  1052. }
  1053. static const struct iio_buffer_setup_ops noop_ring_setup_ops;
  1054. /**
  1055. * iio_device_register() - register a device with the IIO subsystem
  1056. * @indio_dev: Device structure filled by the device driver
  1057. **/
  1058. int iio_device_register(struct iio_dev *indio_dev)
  1059. {
  1060. int ret;
  1061. /* If the calling driver did not initialize of_node, do it here */
  1062. if (!indio_dev->dev.of_node && indio_dev->dev.parent)
  1063. indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
  1064. ret = iio_check_unique_scan_index(indio_dev);
  1065. if (ret < 0)
  1066. return ret;
  1067. /* configure elements for the chrdev */
  1068. indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
  1069. ret = iio_device_register_debugfs(indio_dev);
  1070. if (ret) {
  1071. dev_err(indio_dev->dev.parent,
  1072. "Failed to register debugfs interfaces\n");
  1073. return ret;
  1074. }
  1075. ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
  1076. if (ret) {
  1077. dev_err(indio_dev->dev.parent,
  1078. "Failed to create buffer sysfs interfaces\n");
  1079. goto error_unreg_debugfs;
  1080. }
  1081. ret = iio_device_register_sysfs(indio_dev);
  1082. if (ret) {
  1083. dev_err(indio_dev->dev.parent,
  1084. "Failed to register sysfs interfaces\n");
  1085. goto error_buffer_free_sysfs;
  1086. }
  1087. ret = iio_device_register_eventset(indio_dev);
  1088. if (ret) {
  1089. dev_err(indio_dev->dev.parent,
  1090. "Failed to register event set\n");
  1091. goto error_free_sysfs;
  1092. }
  1093. if (indio_dev->modes & (INDIO_BUFFER_TRIGGERED | INDIO_EVENT_TRIGGERED))
  1094. iio_device_register_trigger_consumer(indio_dev);
  1095. if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
  1096. indio_dev->setup_ops == NULL)
  1097. indio_dev->setup_ops = &noop_ring_setup_ops;
  1098. cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
  1099. indio_dev->chrdev.owner = indio_dev->info->driver_module;
  1100. indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
  1101. ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
  1102. if (ret < 0)
  1103. goto error_unreg_eventset;
  1104. ret = device_add(&indio_dev->dev);
  1105. if (ret < 0)
  1106. goto error_cdev_del;
  1107. return 0;
  1108. error_cdev_del:
  1109. cdev_del(&indio_dev->chrdev);
  1110. error_unreg_eventset:
  1111. iio_device_unregister_eventset(indio_dev);
  1112. error_free_sysfs:
  1113. iio_device_unregister_sysfs(indio_dev);
  1114. error_buffer_free_sysfs:
  1115. iio_buffer_free_sysfs_and_mask(indio_dev);
  1116. error_unreg_debugfs:
  1117. iio_device_unregister_debugfs(indio_dev);
  1118. return ret;
  1119. }
  1120. EXPORT_SYMBOL(iio_device_register);
  1121. /**
  1122. * iio_device_unregister() - unregister a device from the IIO subsystem
  1123. * @indio_dev: Device structure representing the device.
  1124. **/
  1125. void iio_device_unregister(struct iio_dev *indio_dev)
  1126. {
  1127. mutex_lock(&indio_dev->info_exist_lock);
  1128. device_del(&indio_dev->dev);
  1129. if (indio_dev->chrdev.dev)
  1130. cdev_del(&indio_dev->chrdev);
  1131. iio_device_unregister_debugfs(indio_dev);
  1132. iio_disable_all_buffers(indio_dev);
  1133. indio_dev->info = NULL;
  1134. iio_device_wakeup_eventset(indio_dev);
  1135. iio_buffer_wakeup_poll(indio_dev);
  1136. mutex_unlock(&indio_dev->info_exist_lock);
  1137. iio_buffer_free_sysfs_and_mask(indio_dev);
  1138. }
  1139. EXPORT_SYMBOL(iio_device_unregister);
  1140. static void devm_iio_device_unreg(struct device *dev, void *res)
  1141. {
  1142. iio_device_unregister(*(struct iio_dev **)res);
  1143. }
  1144. /**
  1145. * devm_iio_device_register - Resource-managed iio_device_register()
  1146. * @dev: Device to allocate iio_dev for
  1147. * @indio_dev: Device structure filled by the device driver
  1148. *
  1149. * Managed iio_device_register. The IIO device registered with this
  1150. * function is automatically unregistered on driver detach. This function
  1151. * calls iio_device_register() internally. Refer to that function for more
  1152. * information.
  1153. *
  1154. * If an iio_dev registered with this function needs to be unregistered
  1155. * separately, devm_iio_device_unregister() must be used.
  1156. *
  1157. * RETURNS:
  1158. * 0 on success, negative error number on failure.
  1159. */
  1160. int devm_iio_device_register(struct device *dev, struct iio_dev *indio_dev)
  1161. {
  1162. struct iio_dev **ptr;
  1163. int ret;
  1164. ptr = devres_alloc(devm_iio_device_unreg, sizeof(*ptr), GFP_KERNEL);
  1165. if (!ptr)
  1166. return -ENOMEM;
  1167. *ptr = indio_dev;
  1168. ret = iio_device_register(indio_dev);
  1169. if (!ret)
  1170. devres_add(dev, ptr);
  1171. else
  1172. devres_free(ptr);
  1173. return ret;
  1174. }
  1175. EXPORT_SYMBOL_GPL(devm_iio_device_register);
  1176. /**
  1177. * devm_iio_device_unregister - Resource-managed iio_device_unregister()
  1178. * @dev: Device this iio_dev belongs to
  1179. * @indio_dev: the iio_dev associated with the device
  1180. *
  1181. * Unregister iio_dev registered with devm_iio_device_register().
  1182. */
  1183. void devm_iio_device_unregister(struct device *dev, struct iio_dev *indio_dev)
  1184. {
  1185. int rc;
  1186. rc = devres_release(dev, devm_iio_device_unreg,
  1187. devm_iio_device_match, indio_dev);
  1188. WARN_ON(rc);
  1189. }
  1190. EXPORT_SYMBOL_GPL(devm_iio_device_unregister);
  1191. subsys_initcall(iio_init);
  1192. module_exit(iio_exit);
  1193. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  1194. MODULE_DESCRIPTION("Industrial I/O core");
  1195. MODULE_LICENSE("GPL");