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

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

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