industrialio-core.c 24 KB

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