industrialio-core.c 22 KB

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