PageRenderTime 73ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/iio/industrialio-core.c

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