industrialio-core.c 34 KB

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