industrialio-core.c 22 KB

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