industrialio-core.c 26 KB

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