industrialio-core.c 24 KB

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