industrialio-core.c 22 KB

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