industrialio-core.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/idr.h>
  14. #include <linux/kdev_t.h>
  15. #include <linux/err.h>
  16. #include <linux/device.h>
  17. #include <linux/fs.h>
  18. #include <linux/poll.h>
  19. #include <linux/sched.h>
  20. #include <linux/wait.h>
  21. #include <linux/cdev.h>
  22. #include <linux/slab.h>
  23. #include <linux/anon_inodes.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/iio/iio.h>
  26. #include "iio_core.h"
  27. #include "iio_core_trigger.h"
  28. #include <linux/iio/sysfs.h>
  29. #include <linux/iio/events.h>
  30. #include <linux/iio/buffer.h>
  31. /* IDA to assign each registered device a unique id */
  32. static DEFINE_IDA(iio_ida);
  33. static dev_t iio_devt;
  34. #define IIO_DEV_MAX 256
  35. struct bus_type iio_bus_type = {
  36. .name = "iio",
  37. };
  38. EXPORT_SYMBOL(iio_bus_type);
  39. static struct dentry *iio_debugfs_dentry;
  40. static const char * const iio_direction[] = {
  41. [0] = "in",
  42. [1] = "out",
  43. };
  44. static const char * const iio_chan_type_name_spec[] = {
  45. [IIO_VOLTAGE] = "voltage",
  46. [IIO_CURRENT] = "current",
  47. [IIO_POWER] = "power",
  48. [IIO_ACCEL] = "accel",
  49. [IIO_ANGL_VEL] = "anglvel",
  50. [IIO_MAGN] = "magn",
  51. [IIO_LIGHT] = "illuminance",
  52. [IIO_INTENSITY] = "intensity",
  53. [IIO_PROXIMITY] = "proximity",
  54. [IIO_TEMP] = "temp",
  55. [IIO_INCLI] = "incli",
  56. [IIO_ROT] = "rot",
  57. [IIO_ANGL] = "angl",
  58. [IIO_TIMESTAMP] = "timestamp",
  59. [IIO_CAPACITANCE] = "capacitance",
  60. [IIO_ALTVOLTAGE] = "altvoltage",
  61. [IIO_CCT] = "cct",
  62. [IIO_PRESSURE] = "pressure",
  63. };
  64. static const char * const iio_modifier_names[] = {
  65. [IIO_MOD_X] = "x",
  66. [IIO_MOD_Y] = "y",
  67. [IIO_MOD_Z] = "z",
  68. [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
  69. [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
  70. [IIO_MOD_LIGHT_BOTH] = "both",
  71. [IIO_MOD_LIGHT_IR] = "ir",
  72. [IIO_MOD_LIGHT_CLEAR] = "clear",
  73. [IIO_MOD_LIGHT_RED] = "red",
  74. [IIO_MOD_LIGHT_GREEN] = "green",
  75. [IIO_MOD_LIGHT_BLUE] = "blue",
  76. };
  77. /* relies on pairs of these shared then separate */
  78. static const char * const iio_chan_info_postfix[] = {
  79. [IIO_CHAN_INFO_RAW] = "raw",
  80. [IIO_CHAN_INFO_PROCESSED] = "input",
  81. [IIO_CHAN_INFO_SCALE] = "scale",
  82. [IIO_CHAN_INFO_OFFSET] = "offset",
  83. [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
  84. [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
  85. [IIO_CHAN_INFO_PEAK] = "peak_raw",
  86. [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
  87. [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
  88. [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
  89. [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
  90. = "filter_low_pass_3db_frequency",
  91. [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
  92. [IIO_CHAN_INFO_FREQUENCY] = "frequency",
  93. [IIO_CHAN_INFO_PHASE] = "phase",
  94. [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
  95. [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
  96. [IIO_CHAN_INFO_INT_TIME] = "integration_time",
  97. };
  98. const struct iio_chan_spec
  99. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
  100. {
  101. int i;
  102. for (i = 0; i < indio_dev->num_channels; i++)
  103. if (indio_dev->channels[i].scan_index == si)
  104. return &indio_dev->channels[i];
  105. return NULL;
  106. }
  107. /* This turns up an awful lot */
  108. ssize_t iio_read_const_attr(struct device *dev,
  109. struct device_attribute *attr,
  110. char *buf)
  111. {
  112. return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
  113. }
  114. EXPORT_SYMBOL(iio_read_const_attr);
  115. static int __init iio_init(void)
  116. {
  117. int ret;
  118. /* Register sysfs bus */
  119. ret = bus_register(&iio_bus_type);
  120. if (ret < 0) {
  121. printk(KERN_ERR
  122. "%s could not register bus type\n",
  123. __FILE__);
  124. goto error_nothing;
  125. }
  126. ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
  127. if (ret < 0) {
  128. printk(KERN_ERR "%s: failed to allocate char dev region\n",
  129. __FILE__);
  130. goto error_unregister_bus_type;
  131. }
  132. iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
  133. return 0;
  134. error_unregister_bus_type:
  135. bus_unregister(&iio_bus_type);
  136. error_nothing:
  137. return ret;
  138. }
  139. static void __exit iio_exit(void)
  140. {
  141. if (iio_devt)
  142. unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
  143. bus_unregister(&iio_bus_type);
  144. debugfs_remove(iio_debugfs_dentry);
  145. }
  146. #if defined(CONFIG_DEBUG_FS)
  147. static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
  148. size_t count, loff_t *ppos)
  149. {
  150. struct iio_dev *indio_dev = file->private_data;
  151. char buf[20];
  152. unsigned val = 0;
  153. ssize_t len;
  154. int ret;
  155. ret = indio_dev->info->debugfs_reg_access(indio_dev,
  156. indio_dev->cached_reg_addr,
  157. 0, &val);
  158. if (ret)
  159. dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
  160. len = snprintf(buf, sizeof(buf), "0x%X\n", val);
  161. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  162. }
  163. static ssize_t iio_debugfs_write_reg(struct file *file,
  164. const char __user *userbuf, size_t count, loff_t *ppos)
  165. {
  166. struct iio_dev *indio_dev = file->private_data;
  167. unsigned reg, val;
  168. char buf[80];
  169. int ret;
  170. count = min_t(size_t, count, (sizeof(buf)-1));
  171. if (copy_from_user(buf, userbuf, count))
  172. return -EFAULT;
  173. buf[count] = 0;
  174. ret = sscanf(buf, "%i %i", &reg, &val);
  175. switch (ret) {
  176. case 1:
  177. indio_dev->cached_reg_addr = reg;
  178. break;
  179. case 2:
  180. indio_dev->cached_reg_addr = reg;
  181. ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
  182. val, NULL);
  183. if (ret) {
  184. dev_err(indio_dev->dev.parent, "%s: write failed\n",
  185. __func__);
  186. return ret;
  187. }
  188. break;
  189. default:
  190. return -EINVAL;
  191. }
  192. return count;
  193. }
  194. static const struct file_operations iio_debugfs_reg_fops = {
  195. .open = simple_open,
  196. .read = iio_debugfs_read_reg,
  197. .write = iio_debugfs_write_reg,
  198. };
  199. static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
  200. {
  201. debugfs_remove_recursive(indio_dev->debugfs_dentry);
  202. }
  203. static int iio_device_register_debugfs(struct iio_dev *indio_dev)
  204. {
  205. struct dentry *d;
  206. if (indio_dev->info->debugfs_reg_access == NULL)
  207. return 0;
  208. if (!iio_debugfs_dentry)
  209. return 0;
  210. indio_dev->debugfs_dentry =
  211. debugfs_create_dir(dev_name(&indio_dev->dev),
  212. iio_debugfs_dentry);
  213. if (indio_dev->debugfs_dentry == NULL) {
  214. dev_warn(indio_dev->dev.parent,
  215. "Failed to create debugfs directory\n");
  216. return -EFAULT;
  217. }
  218. d = debugfs_create_file("direct_reg_access", 0644,
  219. indio_dev->debugfs_dentry,
  220. indio_dev, &iio_debugfs_reg_fops);
  221. if (!d) {
  222. iio_device_unregister_debugfs(indio_dev);
  223. return -ENOMEM;
  224. }
  225. return 0;
  226. }
  227. #else
  228. static int iio_device_register_debugfs(struct iio_dev *indio_dev)
  229. {
  230. return 0;
  231. }
  232. static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
  233. {
  234. }
  235. #endif /* CONFIG_DEBUG_FS */
  236. static ssize_t iio_read_channel_ext_info(struct device *dev,
  237. struct device_attribute *attr,
  238. char *buf)
  239. {
  240. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  241. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  242. const struct iio_chan_spec_ext_info *ext_info;
  243. ext_info = &this_attr->c->ext_info[this_attr->address];
  244. return ext_info->read(indio_dev, ext_info->private, this_attr->c, buf);
  245. }
  246. static ssize_t iio_write_channel_ext_info(struct device *dev,
  247. struct device_attribute *attr,
  248. const char *buf,
  249. size_t len)
  250. {
  251. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  252. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  253. const struct iio_chan_spec_ext_info *ext_info;
  254. ext_info = &this_attr->c->ext_info[this_attr->address];
  255. return ext_info->write(indio_dev, ext_info->private,
  256. this_attr->c, buf, len);
  257. }
  258. ssize_t iio_enum_available_read(struct iio_dev *indio_dev,
  259. uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
  260. {
  261. const struct iio_enum *e = (const struct iio_enum *)priv;
  262. unsigned int i;
  263. size_t len = 0;
  264. if (!e->num_items)
  265. return 0;
  266. for (i = 0; i < e->num_items; ++i)
  267. len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]);
  268. /* replace last space with a newline */
  269. buf[len - 1] = '\n';
  270. return len;
  271. }
  272. EXPORT_SYMBOL_GPL(iio_enum_available_read);
  273. ssize_t iio_enum_read(struct iio_dev *indio_dev,
  274. uintptr_t priv, const struct iio_chan_spec *chan, char *buf)
  275. {
  276. const struct iio_enum *e = (const struct iio_enum *)priv;
  277. int i;
  278. if (!e->get)
  279. return -EINVAL;
  280. i = e->get(indio_dev, chan);
  281. if (i < 0)
  282. return i;
  283. else if (i >= e->num_items)
  284. return -EINVAL;
  285. return sprintf(buf, "%s\n", e->items[i]);
  286. }
  287. EXPORT_SYMBOL_GPL(iio_enum_read);
  288. ssize_t iio_enum_write(struct iio_dev *indio_dev,
  289. uintptr_t priv, const struct iio_chan_spec *chan, const char *buf,
  290. size_t len)
  291. {
  292. const struct iio_enum *e = (const struct iio_enum *)priv;
  293. unsigned int i;
  294. int ret;
  295. if (!e->set)
  296. return -EINVAL;
  297. for (i = 0; i < e->num_items; i++) {
  298. if (sysfs_streq(buf, e->items[i]))
  299. break;
  300. }
  301. if (i == e->num_items)
  302. return -EINVAL;
  303. ret = e->set(indio_dev, chan, i);
  304. return ret ? ret : len;
  305. }
  306. EXPORT_SYMBOL_GPL(iio_enum_write);
  307. static ssize_t iio_read_channel_info(struct device *dev,
  308. struct device_attribute *attr,
  309. char *buf)
  310. {
  311. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  312. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  313. unsigned long long tmp;
  314. int val, val2;
  315. bool scale_db = false;
  316. int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
  317. &val, &val2, this_attr->address);
  318. if (ret < 0)
  319. return ret;
  320. switch (ret) {
  321. case IIO_VAL_INT:
  322. return sprintf(buf, "%d\n", val);
  323. case IIO_VAL_INT_PLUS_MICRO_DB:
  324. scale_db = true;
  325. case IIO_VAL_INT_PLUS_MICRO:
  326. if (val2 < 0)
  327. return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
  328. scale_db ? " dB" : "");
  329. else
  330. return sprintf(buf, "%d.%06u%s\n", val, val2,
  331. scale_db ? " dB" : "");
  332. case IIO_VAL_INT_PLUS_NANO:
  333. if (val2 < 0)
  334. return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
  335. else
  336. return sprintf(buf, "%d.%09u\n", val, val2);
  337. case IIO_VAL_FRACTIONAL:
  338. tmp = div_s64((s64)val * 1000000000LL, val2);
  339. val2 = do_div(tmp, 1000000000LL);
  340. val = tmp;
  341. return sprintf(buf, "%d.%09u\n", val, val2);
  342. case IIO_VAL_FRACTIONAL_LOG2:
  343. tmp = (s64)val * 1000000000LL >> val2;
  344. val2 = do_div(tmp, 1000000000LL);
  345. val = tmp;
  346. return sprintf(buf, "%d.%09u\n", val, val2);
  347. default:
  348. return 0;
  349. }
  350. }
  351. /**
  352. * iio_str_to_fixpoint() - Parse a fixed-point number from a string
  353. * @str: The string to parse
  354. * @fract_mult: Multiplier for the first decimal place, should be a power of 10
  355. * @integer: The integer part of the number
  356. * @fract: The fractional part of the number
  357. *
  358. * Returns 0 on success, or a negative error code if the string could not be
  359. * parsed.
  360. */
  361. int iio_str_to_fixpoint(const char *str, int fract_mult,
  362. int *integer, int *fract)
  363. {
  364. int i = 0, f = 0;
  365. bool integer_part = true, negative = false;
  366. if (str[0] == '-') {
  367. negative = true;
  368. str++;
  369. } else if (str[0] == '+') {
  370. str++;
  371. }
  372. while (*str) {
  373. if ('0' <= *str && *str <= '9') {
  374. if (integer_part) {
  375. i = i * 10 + *str - '0';
  376. } else {
  377. f += fract_mult * (*str - '0');
  378. fract_mult /= 10;
  379. }
  380. } else if (*str == '\n') {
  381. if (*(str + 1) == '\0')
  382. break;
  383. else
  384. return -EINVAL;
  385. } else if (*str == '.' && integer_part) {
  386. integer_part = false;
  387. } else {
  388. return -EINVAL;
  389. }
  390. str++;
  391. }
  392. if (negative) {
  393. if (i)
  394. i = -i;
  395. else
  396. f = -f;
  397. }
  398. *integer = i;
  399. *fract = f;
  400. return 0;
  401. }
  402. EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
  403. static ssize_t iio_write_channel_info(struct device *dev,
  404. struct device_attribute *attr,
  405. const char *buf,
  406. size_t len)
  407. {
  408. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  409. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  410. int ret, fract_mult = 100000;
  411. int integer, fract;
  412. /* Assumes decimal - precision based on number of digits */
  413. if (!indio_dev->info->write_raw)
  414. return -EINVAL;
  415. if (indio_dev->info->write_raw_get_fmt)
  416. switch (indio_dev->info->write_raw_get_fmt(indio_dev,
  417. this_attr->c, this_attr->address)) {
  418. case IIO_VAL_INT_PLUS_MICRO:
  419. fract_mult = 100000;
  420. break;
  421. case IIO_VAL_INT_PLUS_NANO:
  422. fract_mult = 100000000;
  423. break;
  424. default:
  425. return -EINVAL;
  426. }
  427. ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
  428. if (ret)
  429. return ret;
  430. ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
  431. integer, fract, this_attr->address);
  432. if (ret)
  433. return ret;
  434. return len;
  435. }
  436. static
  437. int __iio_device_attr_init(struct device_attribute *dev_attr,
  438. const char *postfix,
  439. struct iio_chan_spec const *chan,
  440. ssize_t (*readfunc)(struct device *dev,
  441. struct device_attribute *attr,
  442. char *buf),
  443. ssize_t (*writefunc)(struct device *dev,
  444. struct device_attribute *attr,
  445. const char *buf,
  446. size_t len),
  447. enum iio_shared_by shared_by)
  448. {
  449. int ret = 0;
  450. char *name_format = NULL;
  451. char *full_postfix;
  452. sysfs_attr_init(&dev_attr->attr);
  453. /* Build up postfix of <extend_name>_<modifier>_postfix */
  454. if (chan->modified && (shared_by == IIO_SEPARATE)) {
  455. if (chan->extend_name)
  456. full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  457. iio_modifier_names[chan
  458. ->channel2],
  459. chan->extend_name,
  460. postfix);
  461. else
  462. full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
  463. iio_modifier_names[chan
  464. ->channel2],
  465. postfix);
  466. } else {
  467. if (chan->extend_name == NULL)
  468. full_postfix = kstrdup(postfix, GFP_KERNEL);
  469. else
  470. full_postfix = kasprintf(GFP_KERNEL,
  471. "%s_%s",
  472. chan->extend_name,
  473. postfix);
  474. }
  475. if (full_postfix == NULL)
  476. return -ENOMEM;
  477. if (chan->differential) { /* Differential can not have modifier */
  478. switch (shared_by) {
  479. case IIO_SHARED_BY_ALL:
  480. name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
  481. break;
  482. case IIO_SHARED_BY_DIR:
  483. name_format = kasprintf(GFP_KERNEL, "%s_%s",
  484. iio_direction[chan->output],
  485. full_postfix);
  486. break;
  487. case IIO_SHARED_BY_TYPE:
  488. name_format
  489. = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
  490. iio_direction[chan->output],
  491. iio_chan_type_name_spec[chan->type],
  492. iio_chan_type_name_spec[chan->type],
  493. full_postfix);
  494. break;
  495. case IIO_SEPARATE:
  496. if (!chan->indexed) {
  497. WARN_ON("Differential channels must be indexed\n");
  498. ret = -EINVAL;
  499. goto error_free_full_postfix;
  500. }
  501. name_format
  502. = kasprintf(GFP_KERNEL,
  503. "%s_%s%d-%s%d_%s",
  504. iio_direction[chan->output],
  505. iio_chan_type_name_spec[chan->type],
  506. chan->channel,
  507. iio_chan_type_name_spec[chan->type],
  508. chan->channel2,
  509. full_postfix);
  510. break;
  511. }
  512. } else { /* Single ended */
  513. switch (shared_by) {
  514. case IIO_SHARED_BY_ALL:
  515. name_format = kasprintf(GFP_KERNEL, "%s", full_postfix);
  516. break;
  517. case IIO_SHARED_BY_DIR:
  518. name_format = kasprintf(GFP_KERNEL, "%s_%s",
  519. iio_direction[chan->output],
  520. full_postfix);
  521. break;
  522. case IIO_SHARED_BY_TYPE:
  523. name_format
  524. = kasprintf(GFP_KERNEL, "%s_%s_%s",
  525. iio_direction[chan->output],
  526. iio_chan_type_name_spec[chan->type],
  527. full_postfix);
  528. break;
  529. case IIO_SEPARATE:
  530. if (chan->indexed)
  531. name_format
  532. = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
  533. iio_direction[chan->output],
  534. iio_chan_type_name_spec[chan->type],
  535. chan->channel,
  536. full_postfix);
  537. else
  538. name_format
  539. = kasprintf(GFP_KERNEL, "%s_%s_%s",
  540. iio_direction[chan->output],
  541. iio_chan_type_name_spec[chan->type],
  542. full_postfix);
  543. break;
  544. }
  545. }
  546. if (name_format == NULL) {
  547. ret = -ENOMEM;
  548. goto error_free_full_postfix;
  549. }
  550. dev_attr->attr.name = kasprintf(GFP_KERNEL,
  551. name_format,
  552. chan->channel,
  553. chan->channel2);
  554. if (dev_attr->attr.name == NULL) {
  555. ret = -ENOMEM;
  556. goto error_free_name_format;
  557. }
  558. if (readfunc) {
  559. dev_attr->attr.mode |= S_IRUGO;
  560. dev_attr->show = readfunc;
  561. }
  562. if (writefunc) {
  563. dev_attr->attr.mode |= S_IWUSR;
  564. dev_attr->store = writefunc;
  565. }
  566. error_free_name_format:
  567. kfree(name_format);
  568. error_free_full_postfix:
  569. kfree(full_postfix);
  570. return ret;
  571. }
  572. static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
  573. {
  574. kfree(dev_attr->attr.name);
  575. }
  576. int __iio_add_chan_devattr(const char *postfix,
  577. struct iio_chan_spec const *chan,
  578. ssize_t (*readfunc)(struct device *dev,
  579. struct device_attribute *attr,
  580. char *buf),
  581. ssize_t (*writefunc)(struct device *dev,
  582. struct device_attribute *attr,
  583. const char *buf,
  584. size_t len),
  585. u64 mask,
  586. enum iio_shared_by shared_by,
  587. struct device *dev,
  588. struct list_head *attr_list)
  589. {
  590. int ret;
  591. struct iio_dev_attr *iio_attr, *t;
  592. iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
  593. if (iio_attr == NULL) {
  594. ret = -ENOMEM;
  595. goto error_ret;
  596. }
  597. ret = __iio_device_attr_init(&iio_attr->dev_attr,
  598. postfix, chan,
  599. readfunc, writefunc, shared_by);
  600. if (ret)
  601. goto error_iio_dev_attr_free;
  602. iio_attr->c = chan;
  603. iio_attr->address = mask;
  604. list_for_each_entry(t, attr_list, l)
  605. if (strcmp(t->dev_attr.attr.name,
  606. iio_attr->dev_attr.attr.name) == 0) {
  607. if (shared_by == IIO_SEPARATE)
  608. dev_err(dev, "tried to double register : %s\n",
  609. t->dev_attr.attr.name);
  610. ret = -EBUSY;
  611. goto error_device_attr_deinit;
  612. }
  613. list_add(&iio_attr->l, attr_list);
  614. return 0;
  615. error_device_attr_deinit:
  616. __iio_device_attr_deinit(&iio_attr->dev_attr);
  617. error_iio_dev_attr_free:
  618. kfree(iio_attr);
  619. error_ret:
  620. return ret;
  621. }
  622. static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
  623. struct iio_chan_spec const *chan,
  624. enum iio_shared_by shared_by,
  625. const long *infomask)
  626. {
  627. int i, ret, attrcount = 0;
  628. for_each_set_bit(i, infomask, sizeof(infomask)*8) {
  629. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  630. chan,
  631. &iio_read_channel_info,
  632. &iio_write_channel_info,
  633. i,
  634. shared_by,
  635. &indio_dev->dev,
  636. &indio_dev->channel_attr_list);
  637. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  638. continue;
  639. else if (ret < 0)
  640. return ret;
  641. attrcount++;
  642. }
  643. return attrcount;
  644. }
  645. static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
  646. struct iio_chan_spec const *chan)
  647. {
  648. int ret, attrcount = 0;
  649. const struct iio_chan_spec_ext_info *ext_info;
  650. if (chan->channel < 0)
  651. return 0;
  652. ret = iio_device_add_info_mask_type(indio_dev, chan,
  653. IIO_SEPARATE,
  654. &chan->info_mask_separate);
  655. if (ret < 0)
  656. return ret;
  657. attrcount += ret;
  658. ret = iio_device_add_info_mask_type(indio_dev, chan,
  659. IIO_SHARED_BY_TYPE,
  660. &chan->info_mask_shared_by_type);
  661. if (ret < 0)
  662. return ret;
  663. attrcount += ret;
  664. ret = iio_device_add_info_mask_type(indio_dev, chan,
  665. IIO_SHARED_BY_DIR,
  666. &chan->info_mask_shared_by_dir);
  667. if (ret < 0)
  668. return ret;
  669. attrcount += ret;
  670. ret = iio_device_add_info_mask_type(indio_dev, chan,
  671. IIO_SHARED_BY_ALL,
  672. &chan->info_mask_shared_by_all);
  673. if (ret < 0)
  674. return ret;
  675. attrcount += ret;
  676. if (chan->ext_info) {
  677. unsigned int i = 0;
  678. for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
  679. ret = __iio_add_chan_devattr(ext_info->name,
  680. chan,
  681. ext_info->read ?
  682. &iio_read_channel_ext_info : NULL,
  683. ext_info->write ?
  684. &iio_write_channel_ext_info : NULL,
  685. i,
  686. ext_info->shared,
  687. &indio_dev->dev,
  688. &indio_dev->channel_attr_list);
  689. i++;
  690. if (ret == -EBUSY && ext_info->shared)
  691. continue;
  692. if (ret)
  693. return ret;
  694. attrcount++;
  695. }
  696. }
  697. return attrcount;
  698. }
  699. static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
  700. struct iio_dev_attr *p)
  701. {
  702. kfree(p->dev_attr.attr.name);
  703. kfree(p);
  704. }
  705. static ssize_t iio_show_dev_name(struct device *dev,
  706. struct device_attribute *attr,
  707. char *buf)
  708. {
  709. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  710. return sprintf(buf, "%s\n", indio_dev->name);
  711. }
  712. static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
  713. static int iio_device_register_sysfs(struct iio_dev *indio_dev)
  714. {
  715. int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
  716. struct iio_dev_attr *p, *n;
  717. struct attribute **attr;
  718. /* First count elements in any existing group */
  719. if (indio_dev->info->attrs) {
  720. attr = indio_dev->info->attrs->attrs;
  721. while (*attr++ != NULL)
  722. attrcount_orig++;
  723. }
  724. attrcount = attrcount_orig;
  725. /*
  726. * New channel registration method - relies on the fact a group does
  727. * not need to be initialized if its name is NULL.
  728. */
  729. if (indio_dev->channels)
  730. for (i = 0; i < indio_dev->num_channels; i++) {
  731. ret = iio_device_add_channel_sysfs(indio_dev,
  732. &indio_dev
  733. ->channels[i]);
  734. if (ret < 0)
  735. goto error_clear_attrs;
  736. attrcount += ret;
  737. }
  738. if (indio_dev->name)
  739. attrcount++;
  740. indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
  741. sizeof(indio_dev->chan_attr_group.attrs[0]),
  742. GFP_KERNEL);
  743. if (indio_dev->chan_attr_group.attrs == NULL) {
  744. ret = -ENOMEM;
  745. goto error_clear_attrs;
  746. }
  747. /* Copy across original attributes */
  748. if (indio_dev->info->attrs)
  749. memcpy(indio_dev->chan_attr_group.attrs,
  750. indio_dev->info->attrs->attrs,
  751. sizeof(indio_dev->chan_attr_group.attrs[0])
  752. *attrcount_orig);
  753. attrn = attrcount_orig;
  754. /* Add all elements from the list. */
  755. list_for_each_entry(p, &indio_dev->channel_attr_list, l)
  756. indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
  757. if (indio_dev->name)
  758. indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
  759. indio_dev->groups[indio_dev->groupcounter++] =
  760. &indio_dev->chan_attr_group;
  761. return 0;
  762. error_clear_attrs:
  763. list_for_each_entry_safe(p, n,
  764. &indio_dev->channel_attr_list, l) {
  765. list_del(&p->l);
  766. iio_device_remove_and_free_read_attr(indio_dev, p);
  767. }
  768. return ret;
  769. }
  770. static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
  771. {
  772. struct iio_dev_attr *p, *n;
  773. list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
  774. list_del(&p->l);
  775. iio_device_remove_and_free_read_attr(indio_dev, p);
  776. }
  777. kfree(indio_dev->chan_attr_group.attrs);
  778. }
  779. static void iio_dev_release(struct device *device)
  780. {
  781. struct iio_dev *indio_dev = dev_to_iio_dev(device);
  782. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  783. iio_device_unregister_trigger_consumer(indio_dev);
  784. iio_device_unregister_eventset(indio_dev);
  785. iio_device_unregister_sysfs(indio_dev);
  786. iio_buffer_put(indio_dev->buffer);
  787. ida_simple_remove(&iio_ida, indio_dev->id);
  788. kfree(indio_dev);
  789. }
  790. struct device_type iio_device_type = {
  791. .name = "iio_device",
  792. .release = iio_dev_release,
  793. };
  794. struct iio_dev *iio_device_alloc(int sizeof_priv)
  795. {
  796. struct iio_dev *dev;
  797. size_t alloc_size;
  798. alloc_size = sizeof(struct iio_dev);
  799. if (sizeof_priv) {
  800. alloc_size = ALIGN(alloc_size, IIO_ALIGN);
  801. alloc_size += sizeof_priv;
  802. }
  803. /* ensure 32-byte alignment of whole construct ? */
  804. alloc_size += IIO_ALIGN - 1;
  805. dev = kzalloc(alloc_size, GFP_KERNEL);
  806. if (dev) {
  807. dev->dev.groups = dev->groups;
  808. dev->dev.type = &iio_device_type;
  809. dev->dev.bus = &iio_bus_type;
  810. device_initialize(&dev->dev);
  811. dev_set_drvdata(&dev->dev, (void *)dev);
  812. mutex_init(&dev->mlock);
  813. mutex_init(&dev->info_exist_lock);
  814. INIT_LIST_HEAD(&dev->channel_attr_list);
  815. dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
  816. if (dev->id < 0) {
  817. /* cannot use a dev_err as the name isn't available */
  818. printk(KERN_ERR "Failed to get id\n");
  819. kfree(dev);
  820. return NULL;
  821. }
  822. dev_set_name(&dev->dev, "iio:device%d", dev->id);
  823. INIT_LIST_HEAD(&dev->buffer_list);
  824. }
  825. return dev;
  826. }
  827. EXPORT_SYMBOL(iio_device_alloc);
  828. void iio_device_free(struct iio_dev *dev)
  829. {
  830. if (dev)
  831. put_device(&dev->dev);
  832. }
  833. EXPORT_SYMBOL(iio_device_free);
  834. static void devm_iio_device_release(struct device *dev, void *res)
  835. {
  836. iio_device_free(*(struct iio_dev **)res);
  837. }
  838. static int devm_iio_device_match(struct device *dev, void *res, void *data)
  839. {
  840. struct iio_dev **r = res;
  841. if (!r || !*r) {
  842. WARN_ON(!r || !*r);
  843. return 0;
  844. }
  845. return *r == data;
  846. }
  847. struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
  848. {
  849. struct iio_dev **ptr, *iio_dev;
  850. ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
  851. GFP_KERNEL);
  852. if (!ptr)
  853. return NULL;
  854. /* use raw alloc_dr for kmalloc caller tracing */
  855. iio_dev = iio_device_alloc(sizeof_priv);
  856. if (iio_dev) {
  857. *ptr = iio_dev;
  858. devres_add(dev, ptr);
  859. } else {
  860. devres_free(ptr);
  861. }
  862. return iio_dev;
  863. }
  864. EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
  865. void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
  866. {
  867. int rc;
  868. rc = devres_release(dev, devm_iio_device_release,
  869. devm_iio_device_match, iio_dev);
  870. WARN_ON(rc);
  871. }
  872. EXPORT_SYMBOL_GPL(devm_iio_device_free);
  873. /**
  874. * iio_chrdev_open() - chrdev file open for buffer access and ioctls
  875. **/
  876. static int iio_chrdev_open(struct inode *inode, struct file *filp)
  877. {
  878. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  879. struct iio_dev, chrdev);
  880. if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
  881. return -EBUSY;
  882. iio_device_get(indio_dev);
  883. filp->private_data = indio_dev;
  884. return 0;
  885. }
  886. /**
  887. * iio_chrdev_release() - chrdev file close buffer access and ioctls
  888. **/
  889. static int iio_chrdev_release(struct inode *inode, struct file *filp)
  890. {
  891. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  892. struct iio_dev, chrdev);
  893. clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
  894. iio_device_put(indio_dev);
  895. return 0;
  896. }
  897. /* Somewhat of a cross file organization violation - ioctls here are actually
  898. * event related */
  899. static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  900. {
  901. struct iio_dev *indio_dev = filp->private_data;
  902. int __user *ip = (int __user *)arg;
  903. int fd;
  904. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  905. fd = iio_event_getfd(indio_dev);
  906. if (copy_to_user(ip, &fd, sizeof(fd)))
  907. return -EFAULT;
  908. return 0;
  909. }
  910. return -EINVAL;
  911. }
  912. static const struct file_operations iio_buffer_fileops = {
  913. .read = iio_buffer_read_first_n_outer_addr,
  914. .release = iio_chrdev_release,
  915. .open = iio_chrdev_open,
  916. .poll = iio_buffer_poll_addr,
  917. .owner = THIS_MODULE,
  918. .llseek = noop_llseek,
  919. .unlocked_ioctl = iio_ioctl,
  920. .compat_ioctl = iio_ioctl,
  921. };
  922. static const struct iio_buffer_setup_ops noop_ring_setup_ops;
  923. int iio_device_register(struct iio_dev *indio_dev)
  924. {
  925. int ret;
  926. /* If the calling driver did not initialize of_node, do it here */
  927. if (!indio_dev->dev.of_node && indio_dev->dev.parent)
  928. indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
  929. /* configure elements for the chrdev */
  930. indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
  931. ret = iio_device_register_debugfs(indio_dev);
  932. if (ret) {
  933. dev_err(indio_dev->dev.parent,
  934. "Failed to register debugfs interfaces\n");
  935. goto error_ret;
  936. }
  937. ret = iio_device_register_sysfs(indio_dev);
  938. if (ret) {
  939. dev_err(indio_dev->dev.parent,
  940. "Failed to register sysfs interfaces\n");
  941. goto error_unreg_debugfs;
  942. }
  943. ret = iio_device_register_eventset(indio_dev);
  944. if (ret) {
  945. dev_err(indio_dev->dev.parent,
  946. "Failed to register event set\n");
  947. goto error_free_sysfs;
  948. }
  949. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  950. iio_device_register_trigger_consumer(indio_dev);
  951. if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
  952. indio_dev->setup_ops == NULL)
  953. indio_dev->setup_ops = &noop_ring_setup_ops;
  954. cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
  955. indio_dev->chrdev.owner = indio_dev->info->driver_module;
  956. indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
  957. ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
  958. if (ret < 0)
  959. goto error_unreg_eventset;
  960. ret = device_add(&indio_dev->dev);
  961. if (ret < 0)
  962. goto error_cdev_del;
  963. return 0;
  964. error_cdev_del:
  965. cdev_del(&indio_dev->chrdev);
  966. error_unreg_eventset:
  967. iio_device_unregister_eventset(indio_dev);
  968. error_free_sysfs:
  969. iio_device_unregister_sysfs(indio_dev);
  970. error_unreg_debugfs:
  971. iio_device_unregister_debugfs(indio_dev);
  972. error_ret:
  973. return ret;
  974. }
  975. EXPORT_SYMBOL(iio_device_register);
  976. void iio_device_unregister(struct iio_dev *indio_dev)
  977. {
  978. mutex_lock(&indio_dev->info_exist_lock);
  979. device_del(&indio_dev->dev);
  980. if (indio_dev->chrdev.dev)
  981. cdev_del(&indio_dev->chrdev);
  982. iio_device_unregister_debugfs(indio_dev);
  983. iio_disable_all_buffers(indio_dev);
  984. indio_dev->info = NULL;
  985. mutex_unlock(&indio_dev->info_exist_lock);
  986. }
  987. EXPORT_SYMBOL(iio_device_unregister);
  988. subsys_initcall(iio_init);
  989. module_exit(iio_exit);
  990. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  991. MODULE_DESCRIPTION("Industrial I/O core");
  992. MODULE_LICENSE("GPL");