industrialio-core.c 24 KB

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