industrialio-core.c 24 KB

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