industrialio-core.c 24 KB

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