industrialio-core.c 24 KB

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