industrialio-core.c 23 KB

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