PageRenderTime 28ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

/sc7731_kernel/drivers/iio/industrialio-core.c

https://gitlab.com/envieidoc/sprd_project
C | 1046 lines | 867 code | 133 blank | 46 comment | 130 complexity | 8e74e4c807dc53482ff50120e9571b07 MD5 | raw file
  1. /* The industrial I/O core
  2. *
  3. * Copyright (c) 2008 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * Based on elements of hwmon and input subsystems.
  10. */
  11. #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. [IIO_QUATERNION] = "quaternion",
  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. [IIO_MOD_R] = "r",
  77. };
  78. /* relies on pairs of these shared then separate */
  79. static const char * const iio_chan_info_postfix[] = {
  80. [IIO_CHAN_INFO_RAW] = "raw",
  81. [IIO_CHAN_INFO_PROCESSED] = "input",
  82. [IIO_CHAN_INFO_SCALE] = "scale",
  83. [IIO_CHAN_INFO_OFFSET] = "offset",
  84. [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
  85. [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
  86. [IIO_CHAN_INFO_PEAK] = "peak_raw",
  87. [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
  88. [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
  89. [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
  90. [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
  91. = "filter_low_pass_3db_frequency",
  92. [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
  93. [IIO_CHAN_INFO_FREQUENCY] = "frequency",
  94. [IIO_CHAN_INFO_PHASE] = "phase",
  95. [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
  96. [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
  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, "-%d.%06u%s\n", 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, "-%d.%09u\n", 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. bool generic)
  448. {
  449. int ret;
  450. char *name_format, *full_postfix;
  451. sysfs_attr_init(&dev_attr->attr);
  452. /* Build up postfix of <extend_name>_<modifier>_postfix */
  453. if (chan->modified && !generic) {
  454. if (chan->extend_name)
  455. full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  456. iio_modifier_names[chan
  457. ->channel2],
  458. chan->extend_name,
  459. postfix);
  460. else
  461. full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
  462. iio_modifier_names[chan
  463. ->channel2],
  464. postfix);
  465. } else {
  466. if (chan->extend_name == NULL)
  467. full_postfix = kstrdup(postfix, GFP_KERNEL);
  468. else
  469. full_postfix = kasprintf(GFP_KERNEL,
  470. "%s_%s",
  471. chan->extend_name,
  472. postfix);
  473. }
  474. if (full_postfix == NULL) {
  475. ret = -ENOMEM;
  476. goto error_ret;
  477. }
  478. if (chan->differential) { /* Differential can not have modifier */
  479. if (generic)
  480. name_format
  481. = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
  482. iio_direction[chan->output],
  483. iio_chan_type_name_spec[chan->type],
  484. iio_chan_type_name_spec[chan->type],
  485. full_postfix);
  486. else if (chan->indexed)
  487. name_format
  488. = kasprintf(GFP_KERNEL, "%s_%s%d-%s%d_%s",
  489. iio_direction[chan->output],
  490. iio_chan_type_name_spec[chan->type],
  491. chan->channel,
  492. iio_chan_type_name_spec[chan->type],
  493. chan->channel2,
  494. full_postfix);
  495. else {
  496. WARN_ON("Differential channels must be indexed\n");
  497. ret = -EINVAL;
  498. goto error_free_full_postfix;
  499. }
  500. } else { /* Single ended */
  501. if (generic)
  502. name_format
  503. = kasprintf(GFP_KERNEL, "%s_%s_%s",
  504. iio_direction[chan->output],
  505. iio_chan_type_name_spec[chan->type],
  506. full_postfix);
  507. else if (chan->indexed)
  508. name_format
  509. = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
  510. iio_direction[chan->output],
  511. iio_chan_type_name_spec[chan->type],
  512. chan->channel,
  513. full_postfix);
  514. else
  515. name_format
  516. = kasprintf(GFP_KERNEL, "%s_%s_%s",
  517. iio_direction[chan->output],
  518. iio_chan_type_name_spec[chan->type],
  519. full_postfix);
  520. }
  521. if (name_format == NULL) {
  522. ret = -ENOMEM;
  523. goto error_free_full_postfix;
  524. }
  525. dev_attr->attr.name = kasprintf(GFP_KERNEL,
  526. name_format,
  527. chan->channel,
  528. chan->channel2);
  529. if (dev_attr->attr.name == NULL) {
  530. ret = -ENOMEM;
  531. goto error_free_name_format;
  532. }
  533. if (readfunc) {
  534. dev_attr->attr.mode |= S_IRUGO;
  535. dev_attr->show = readfunc;
  536. }
  537. if (writefunc) {
  538. dev_attr->attr.mode |= S_IWUSR;
  539. dev_attr->store = writefunc;
  540. }
  541. kfree(name_format);
  542. kfree(full_postfix);
  543. return 0;
  544. error_free_name_format:
  545. kfree(name_format);
  546. error_free_full_postfix:
  547. kfree(full_postfix);
  548. error_ret:
  549. return ret;
  550. }
  551. static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
  552. {
  553. kfree(dev_attr->attr.name);
  554. }
  555. int __iio_add_chan_devattr(const char *postfix,
  556. struct iio_chan_spec const *chan,
  557. ssize_t (*readfunc)(struct device *dev,
  558. struct device_attribute *attr,
  559. char *buf),
  560. ssize_t (*writefunc)(struct device *dev,
  561. struct device_attribute *attr,
  562. const char *buf,
  563. size_t len),
  564. u64 mask,
  565. bool generic,
  566. struct device *dev,
  567. struct list_head *attr_list)
  568. {
  569. int ret;
  570. struct iio_dev_attr *iio_attr, *t;
  571. iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
  572. if (iio_attr == NULL) {
  573. ret = -ENOMEM;
  574. goto error_ret;
  575. }
  576. ret = __iio_device_attr_init(&iio_attr->dev_attr,
  577. postfix, chan,
  578. readfunc, writefunc, generic);
  579. if (ret)
  580. goto error_iio_dev_attr_free;
  581. iio_attr->c = chan;
  582. iio_attr->address = mask;
  583. list_for_each_entry(t, attr_list, l)
  584. if (strcmp(t->dev_attr.attr.name,
  585. iio_attr->dev_attr.attr.name) == 0) {
  586. if (!generic)
  587. dev_err(dev, "tried to double register : %s\n",
  588. t->dev_attr.attr.name);
  589. ret = -EBUSY;
  590. goto error_device_attr_deinit;
  591. }
  592. list_add(&iio_attr->l, attr_list);
  593. return 0;
  594. error_device_attr_deinit:
  595. __iio_device_attr_deinit(&iio_attr->dev_attr);
  596. error_iio_dev_attr_free:
  597. kfree(iio_attr);
  598. error_ret:
  599. return ret;
  600. }
  601. static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
  602. struct iio_chan_spec const *chan)
  603. {
  604. int ret, attrcount = 0;
  605. int i;
  606. const struct iio_chan_spec_ext_info *ext_info;
  607. if (chan->channel < 0)
  608. return 0;
  609. for_each_set_bit(i, &chan->info_mask_separate, sizeof(long)*8) {
  610. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  611. chan,
  612. &iio_read_channel_info,
  613. &iio_write_channel_info,
  614. i,
  615. 0,
  616. &indio_dev->dev,
  617. &indio_dev->channel_attr_list);
  618. if (ret < 0)
  619. goto error_ret;
  620. attrcount++;
  621. }
  622. for_each_set_bit(i, &chan->info_mask_shared_by_type, sizeof(long)*8) {
  623. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  624. chan,
  625. &iio_read_channel_info,
  626. &iio_write_channel_info,
  627. i,
  628. 1,
  629. &indio_dev->dev,
  630. &indio_dev->channel_attr_list);
  631. if (ret == -EBUSY) {
  632. ret = 0;
  633. continue;
  634. } else if (ret < 0) {
  635. goto error_ret;
  636. }
  637. attrcount++;
  638. }
  639. if (chan->ext_info) {
  640. unsigned int i = 0;
  641. for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
  642. ret = __iio_add_chan_devattr(ext_info->name,
  643. chan,
  644. ext_info->read ?
  645. &iio_read_channel_ext_info : NULL,
  646. ext_info->write ?
  647. &iio_write_channel_ext_info : NULL,
  648. i,
  649. ext_info->shared,
  650. &indio_dev->dev,
  651. &indio_dev->channel_attr_list);
  652. i++;
  653. if (ret == -EBUSY && ext_info->shared)
  654. continue;
  655. if (ret)
  656. goto error_ret;
  657. attrcount++;
  658. }
  659. }
  660. ret = attrcount;
  661. error_ret:
  662. return ret;
  663. }
  664. static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
  665. struct iio_dev_attr *p)
  666. {
  667. kfree(p->dev_attr.attr.name);
  668. kfree(p);
  669. }
  670. static ssize_t iio_show_dev_name(struct device *dev,
  671. struct device_attribute *attr,
  672. char *buf)
  673. {
  674. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  675. return sprintf(buf, "%s\n", indio_dev->name);
  676. }
  677. static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
  678. static int iio_device_register_sysfs(struct iio_dev *indio_dev)
  679. {
  680. int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
  681. struct iio_dev_attr *p, *n;
  682. struct attribute **attr;
  683. /* First count elements in any existing group */
  684. if (indio_dev->info->attrs) {
  685. attr = indio_dev->info->attrs->attrs;
  686. while (*attr++ != NULL)
  687. attrcount_orig++;
  688. }
  689. attrcount = attrcount_orig;
  690. /*
  691. * New channel registration method - relies on the fact a group does
  692. * not need to be initialized if its name is NULL.
  693. */
  694. if (indio_dev->channels)
  695. for (i = 0; i < indio_dev->num_channels; i++) {
  696. ret = iio_device_add_channel_sysfs(indio_dev,
  697. &indio_dev
  698. ->channels[i]);
  699. if (ret < 0)
  700. goto error_clear_attrs;
  701. attrcount += ret;
  702. }
  703. if (indio_dev->name)
  704. attrcount++;
  705. indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
  706. sizeof(indio_dev->chan_attr_group.attrs[0]),
  707. GFP_KERNEL);
  708. if (indio_dev->chan_attr_group.attrs == NULL) {
  709. ret = -ENOMEM;
  710. goto error_clear_attrs;
  711. }
  712. /* Copy across original attributes */
  713. if (indio_dev->info->attrs)
  714. memcpy(indio_dev->chan_attr_group.attrs,
  715. indio_dev->info->attrs->attrs,
  716. sizeof(indio_dev->chan_attr_group.attrs[0])
  717. *attrcount_orig);
  718. attrn = attrcount_orig;
  719. /* Add all elements from the list. */
  720. list_for_each_entry(p, &indio_dev->channel_attr_list, l)
  721. indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
  722. if (indio_dev->name)
  723. indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
  724. indio_dev->groups[indio_dev->groupcounter++] =
  725. &indio_dev->chan_attr_group;
  726. return 0;
  727. error_clear_attrs:
  728. list_for_each_entry_safe(p, n,
  729. &indio_dev->channel_attr_list, l) {
  730. list_del(&p->l);
  731. iio_device_remove_and_free_read_attr(indio_dev, p);
  732. }
  733. return ret;
  734. }
  735. static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
  736. {
  737. struct iio_dev_attr *p, *n;
  738. list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
  739. list_del(&p->l);
  740. iio_device_remove_and_free_read_attr(indio_dev, p);
  741. }
  742. kfree(indio_dev->chan_attr_group.attrs);
  743. }
  744. static void iio_dev_release(struct device *device)
  745. {
  746. struct iio_dev *indio_dev = dev_to_iio_dev(device);
  747. if (indio_dev->chrdev.dev)
  748. cdev_del(&indio_dev->chrdev);
  749. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  750. iio_device_unregister_trigger_consumer(indio_dev);
  751. iio_device_unregister_eventset(indio_dev);
  752. iio_device_unregister_sysfs(indio_dev);
  753. iio_device_unregister_debugfs(indio_dev);
  754. ida_simple_remove(&iio_ida, indio_dev->id);
  755. kfree(indio_dev);
  756. }
  757. struct device_type iio_device_type = {
  758. .name = "iio_device",
  759. .release = iio_dev_release,
  760. };
  761. struct iio_dev *iio_device_alloc(int sizeof_priv)
  762. {
  763. struct iio_dev *dev;
  764. size_t alloc_size;
  765. alloc_size = sizeof(struct iio_dev);
  766. if (sizeof_priv) {
  767. alloc_size = ALIGN(alloc_size, IIO_ALIGN);
  768. alloc_size += sizeof_priv;
  769. }
  770. /* ensure 32-byte alignment of whole construct ? */
  771. alloc_size += IIO_ALIGN - 1;
  772. dev = kzalloc(alloc_size, GFP_KERNEL);
  773. if (dev) {
  774. dev->dev.groups = dev->groups;
  775. dev->dev.type = &iio_device_type;
  776. dev->dev.bus = &iio_bus_type;
  777. device_initialize(&dev->dev);
  778. dev_set_drvdata(&dev->dev, (void *)dev);
  779. mutex_init(&dev->mlock);
  780. mutex_init(&dev->info_exist_lock);
  781. INIT_LIST_HEAD(&dev->channel_attr_list);
  782. dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
  783. if (dev->id < 0) {
  784. /* cannot use a dev_err as the name isn't available */
  785. printk(KERN_ERR "Failed to get id\n");
  786. kfree(dev);
  787. return NULL;
  788. }
  789. dev_set_name(&dev->dev, "iio:device%d", dev->id);
  790. INIT_LIST_HEAD(&dev->buffer_list);
  791. }
  792. return dev;
  793. }
  794. EXPORT_SYMBOL(iio_device_alloc);
  795. void iio_device_free(struct iio_dev *dev)
  796. {
  797. if (dev)
  798. put_device(&dev->dev);
  799. }
  800. EXPORT_SYMBOL(iio_device_free);
  801. /**
  802. * iio_chrdev_open() - chrdev file open for buffer access and ioctls
  803. **/
  804. static int iio_chrdev_open(struct inode *inode, struct file *filp)
  805. {
  806. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  807. struct iio_dev, chrdev);
  808. if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
  809. return -EBUSY;
  810. filp->private_data = indio_dev;
  811. return 0;
  812. }
  813. /**
  814. * iio_chrdev_release() - chrdev file close buffer access and ioctls
  815. **/
  816. static int iio_chrdev_release(struct inode *inode, struct file *filp)
  817. {
  818. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  819. struct iio_dev, chrdev);
  820. clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
  821. return 0;
  822. }
  823. /* Somewhat of a cross file organization violation - ioctls here are actually
  824. * event related */
  825. static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  826. {
  827. struct iio_dev *indio_dev = filp->private_data;
  828. int __user *ip = (int __user *)arg;
  829. int fd;
  830. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  831. fd = iio_event_getfd(indio_dev);
  832. if (copy_to_user(ip, &fd, sizeof(fd)))
  833. return -EFAULT;
  834. return 0;
  835. }
  836. return -EINVAL;
  837. }
  838. static const struct file_operations iio_buffer_fileops = {
  839. .read = iio_buffer_read_first_n_outer_addr,
  840. .release = iio_chrdev_release,
  841. .open = iio_chrdev_open,
  842. .poll = iio_buffer_poll_addr,
  843. .owner = THIS_MODULE,
  844. .llseek = noop_llseek,
  845. .unlocked_ioctl = iio_ioctl,
  846. .compat_ioctl = iio_ioctl,
  847. };
  848. static const struct iio_buffer_setup_ops noop_ring_setup_ops;
  849. int iio_device_register(struct iio_dev *indio_dev)
  850. {
  851. int ret;
  852. /* If the calling driver did not initialize of_node, do it here */
  853. if (!indio_dev->dev.of_node && indio_dev->dev.parent)
  854. indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
  855. /* configure elements for the chrdev */
  856. indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
  857. ret = iio_device_register_debugfs(indio_dev);
  858. if (ret) {
  859. dev_err(indio_dev->dev.parent,
  860. "Failed to register debugfs interfaces\n");
  861. goto error_ret;
  862. }
  863. ret = iio_device_register_sysfs(indio_dev);
  864. if (ret) {
  865. dev_err(indio_dev->dev.parent,
  866. "Failed to register sysfs interfaces\n");
  867. goto error_unreg_debugfs;
  868. }
  869. ret = iio_device_register_eventset(indio_dev);
  870. if (ret) {
  871. dev_err(indio_dev->dev.parent,
  872. "Failed to register event set\n");
  873. goto error_free_sysfs;
  874. }
  875. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  876. iio_device_register_trigger_consumer(indio_dev);
  877. if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
  878. indio_dev->setup_ops == NULL)
  879. indio_dev->setup_ops = &noop_ring_setup_ops;
  880. ret = device_add(&indio_dev->dev);
  881. if (ret < 0)
  882. goto error_unreg_eventset;
  883. cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
  884. indio_dev->chrdev.owner = indio_dev->info->driver_module;
  885. ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
  886. if (ret < 0)
  887. goto error_del_device;
  888. return 0;
  889. error_del_device:
  890. device_del(&indio_dev->dev);
  891. error_unreg_eventset:
  892. iio_device_unregister_eventset(indio_dev);
  893. error_free_sysfs:
  894. iio_device_unregister_sysfs(indio_dev);
  895. error_unreg_debugfs:
  896. iio_device_unregister_debugfs(indio_dev);
  897. error_ret:
  898. return ret;
  899. }
  900. EXPORT_SYMBOL(iio_device_register);
  901. void iio_device_unregister(struct iio_dev *indio_dev)
  902. {
  903. mutex_lock(&indio_dev->info_exist_lock);
  904. indio_dev->info = NULL;
  905. mutex_unlock(&indio_dev->info_exist_lock);
  906. device_del(&indio_dev->dev);
  907. }
  908. EXPORT_SYMBOL(iio_device_unregister);
  909. subsys_initcall(iio_init);
  910. module_exit(iio_exit);
  911. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  912. MODULE_DESCRIPTION("Industrial I/O core");
  913. MODULE_LICENSE("GPL");