PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/iio/industrialio-core.c

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