industrialio-core.c 25 KB

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