industrialio-core.c 24 KB

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