industrialio-core.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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. enum iio_shared_by shared_by)
  447. {
  448. int ret = 0;
  449. char *name_format = NULL;
  450. char *full_postfix;
  451. sysfs_attr_init(&dev_attr->attr);
  452. /* Build up postfix of <extend_name>_<modifier>_postfix */
  453. if (chan->modified && (shared_by == IIO_SEPARATE)) {
  454. if (chan->extend_name)
  455. full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  456. iio_modifier_names[chan
  457. ->channel2],
  458. chan->extend_name,
  459. postfix);
  460. else
  461. full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
  462. iio_modifier_names[chan
  463. ->channel2],
  464. postfix);
  465. } else {
  466. if (chan->extend_name == NULL)
  467. full_postfix = kstrdup(postfix, GFP_KERNEL);
  468. else
  469. full_postfix = kasprintf(GFP_KERNEL,
  470. "%s_%s",
  471. chan->extend_name,
  472. postfix);
  473. }
  474. if (full_postfix == NULL)
  475. return -ENOMEM;
  476. if (chan->differential) { /* Differential can not have modifier */
  477. switch (shared_by) {
  478. case IIO_SHARED_BY_TYPE:
  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. break;
  486. case IIO_SEPARATE:
  487. if (!chan->indexed) {
  488. WARN_ON("Differential channels must be indexed\n");
  489. ret = -EINVAL;
  490. goto error_free_full_postfix;
  491. }
  492. name_format
  493. = kasprintf(GFP_KERNEL,
  494. "%s_%s%d-%s%d_%s",
  495. iio_direction[chan->output],
  496. iio_chan_type_name_spec[chan->type],
  497. chan->channel,
  498. iio_chan_type_name_spec[chan->type],
  499. chan->channel2,
  500. full_postfix);
  501. break;
  502. }
  503. } else { /* Single ended */
  504. switch (shared_by) {
  505. case IIO_SHARED_BY_TYPE:
  506. name_format
  507. = kasprintf(GFP_KERNEL, "%s_%s_%s",
  508. iio_direction[chan->output],
  509. iio_chan_type_name_spec[chan->type],
  510. full_postfix);
  511. break;
  512. case IIO_SEPARATE:
  513. if (chan->indexed)
  514. name_format
  515. = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
  516. iio_direction[chan->output],
  517. iio_chan_type_name_spec[chan->type],
  518. chan->channel,
  519. full_postfix);
  520. else
  521. name_format
  522. = kasprintf(GFP_KERNEL, "%s_%s_%s",
  523. iio_direction[chan->output],
  524. iio_chan_type_name_spec[chan->type],
  525. full_postfix);
  526. break;
  527. }
  528. }
  529. if (name_format == NULL) {
  530. ret = -ENOMEM;
  531. goto error_free_full_postfix;
  532. }
  533. dev_attr->attr.name = kasprintf(GFP_KERNEL,
  534. name_format,
  535. chan->channel,
  536. chan->channel2);
  537. if (dev_attr->attr.name == NULL) {
  538. ret = -ENOMEM;
  539. goto error_free_name_format;
  540. }
  541. if (readfunc) {
  542. dev_attr->attr.mode |= S_IRUGO;
  543. dev_attr->show = readfunc;
  544. }
  545. if (writefunc) {
  546. dev_attr->attr.mode |= S_IWUSR;
  547. dev_attr->store = writefunc;
  548. }
  549. error_free_name_format:
  550. kfree(name_format);
  551. error_free_full_postfix:
  552. kfree(full_postfix);
  553. return ret;
  554. }
  555. static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
  556. {
  557. kfree(dev_attr->attr.name);
  558. }
  559. int __iio_add_chan_devattr(const char *postfix,
  560. struct iio_chan_spec const *chan,
  561. ssize_t (*readfunc)(struct device *dev,
  562. struct device_attribute *attr,
  563. char *buf),
  564. ssize_t (*writefunc)(struct device *dev,
  565. struct device_attribute *attr,
  566. const char *buf,
  567. size_t len),
  568. u64 mask,
  569. enum iio_shared_by shared_by,
  570. struct device *dev,
  571. struct list_head *attr_list)
  572. {
  573. int ret;
  574. struct iio_dev_attr *iio_attr, *t;
  575. iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
  576. if (iio_attr == NULL) {
  577. ret = -ENOMEM;
  578. goto error_ret;
  579. }
  580. ret = __iio_device_attr_init(&iio_attr->dev_attr,
  581. postfix, chan,
  582. readfunc, writefunc, shared_by);
  583. if (ret)
  584. goto error_iio_dev_attr_free;
  585. iio_attr->c = chan;
  586. iio_attr->address = mask;
  587. list_for_each_entry(t, attr_list, l)
  588. if (strcmp(t->dev_attr.attr.name,
  589. iio_attr->dev_attr.attr.name) == 0) {
  590. if (shared_by == IIO_SEPARATE)
  591. dev_err(dev, "tried to double register : %s\n",
  592. t->dev_attr.attr.name);
  593. ret = -EBUSY;
  594. goto error_device_attr_deinit;
  595. }
  596. list_add(&iio_attr->l, attr_list);
  597. return 0;
  598. error_device_attr_deinit:
  599. __iio_device_attr_deinit(&iio_attr->dev_attr);
  600. error_iio_dev_attr_free:
  601. kfree(iio_attr);
  602. error_ret:
  603. return ret;
  604. }
  605. static int iio_device_add_info_mask_type(struct iio_dev *indio_dev,
  606. struct iio_chan_spec const *chan,
  607. enum iio_shared_by shared_by,
  608. const long *infomask)
  609. {
  610. int i, ret, attrcount = 0;
  611. for_each_set_bit(i, infomask, sizeof(infomask)*8) {
  612. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  613. chan,
  614. &iio_read_channel_info,
  615. &iio_write_channel_info,
  616. i,
  617. shared_by,
  618. &indio_dev->dev,
  619. &indio_dev->channel_attr_list);
  620. if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
  621. continue;
  622. else if (ret < 0)
  623. return ret;
  624. attrcount++;
  625. }
  626. return attrcount;
  627. }
  628. static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
  629. struct iio_chan_spec const *chan)
  630. {
  631. int ret, attrcount = 0;
  632. const struct iio_chan_spec_ext_info *ext_info;
  633. if (chan->channel < 0)
  634. return 0;
  635. ret = iio_device_add_info_mask_type(indio_dev, chan,
  636. IIO_SEPARATE,
  637. &chan->info_mask_separate);
  638. if (ret < 0)
  639. return ret;
  640. attrcount += ret;
  641. ret = iio_device_add_info_mask_type(indio_dev, chan,
  642. IIO_SHARED_BY_TYPE,
  643. &chan->info_mask_shared_by_type);
  644. if (ret < 0)
  645. return ret;
  646. attrcount += ret;
  647. if (chan->ext_info) {
  648. unsigned int i = 0;
  649. for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
  650. ret = __iio_add_chan_devattr(ext_info->name,
  651. chan,
  652. ext_info->read ?
  653. &iio_read_channel_ext_info : NULL,
  654. ext_info->write ?
  655. &iio_write_channel_ext_info : NULL,
  656. i,
  657. ext_info->shared,
  658. &indio_dev->dev,
  659. &indio_dev->channel_attr_list);
  660. i++;
  661. if (ret == -EBUSY && ext_info->shared)
  662. continue;
  663. if (ret)
  664. return ret;
  665. attrcount++;
  666. }
  667. }
  668. return attrcount;
  669. }
  670. static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
  671. struct iio_dev_attr *p)
  672. {
  673. kfree(p->dev_attr.attr.name);
  674. kfree(p);
  675. }
  676. static ssize_t iio_show_dev_name(struct device *dev,
  677. struct device_attribute *attr,
  678. char *buf)
  679. {
  680. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  681. return sprintf(buf, "%s\n", indio_dev->name);
  682. }
  683. static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
  684. static int iio_device_register_sysfs(struct iio_dev *indio_dev)
  685. {
  686. int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
  687. struct iio_dev_attr *p, *n;
  688. struct attribute **attr;
  689. /* First count elements in any existing group */
  690. if (indio_dev->info->attrs) {
  691. attr = indio_dev->info->attrs->attrs;
  692. while (*attr++ != NULL)
  693. attrcount_orig++;
  694. }
  695. attrcount = attrcount_orig;
  696. /*
  697. * New channel registration method - relies on the fact a group does
  698. * not need to be initialized if its name is NULL.
  699. */
  700. if (indio_dev->channels)
  701. for (i = 0; i < indio_dev->num_channels; i++) {
  702. ret = iio_device_add_channel_sysfs(indio_dev,
  703. &indio_dev
  704. ->channels[i]);
  705. if (ret < 0)
  706. goto error_clear_attrs;
  707. attrcount += ret;
  708. }
  709. if (indio_dev->name)
  710. attrcount++;
  711. indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
  712. sizeof(indio_dev->chan_attr_group.attrs[0]),
  713. GFP_KERNEL);
  714. if (indio_dev->chan_attr_group.attrs == NULL) {
  715. ret = -ENOMEM;
  716. goto error_clear_attrs;
  717. }
  718. /* Copy across original attributes */
  719. if (indio_dev->info->attrs)
  720. memcpy(indio_dev->chan_attr_group.attrs,
  721. indio_dev->info->attrs->attrs,
  722. sizeof(indio_dev->chan_attr_group.attrs[0])
  723. *attrcount_orig);
  724. attrn = attrcount_orig;
  725. /* Add all elements from the list. */
  726. list_for_each_entry(p, &indio_dev->channel_attr_list, l)
  727. indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
  728. if (indio_dev->name)
  729. indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
  730. indio_dev->groups[indio_dev->groupcounter++] =
  731. &indio_dev->chan_attr_group;
  732. return 0;
  733. error_clear_attrs:
  734. list_for_each_entry_safe(p, n,
  735. &indio_dev->channel_attr_list, l) {
  736. list_del(&p->l);
  737. iio_device_remove_and_free_read_attr(indio_dev, p);
  738. }
  739. return ret;
  740. }
  741. static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
  742. {
  743. struct iio_dev_attr *p, *n;
  744. list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
  745. list_del(&p->l);
  746. iio_device_remove_and_free_read_attr(indio_dev, p);
  747. }
  748. kfree(indio_dev->chan_attr_group.attrs);
  749. }
  750. static void iio_dev_release(struct device *device)
  751. {
  752. struct iio_dev *indio_dev = dev_to_iio_dev(device);
  753. if (indio_dev->chrdev.dev)
  754. cdev_del(&indio_dev->chrdev);
  755. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  756. iio_device_unregister_trigger_consumer(indio_dev);
  757. iio_device_unregister_eventset(indio_dev);
  758. iio_device_unregister_sysfs(indio_dev);
  759. iio_device_unregister_debugfs(indio_dev);
  760. ida_simple_remove(&iio_ida, indio_dev->id);
  761. kfree(indio_dev);
  762. }
  763. struct device_type iio_device_type = {
  764. .name = "iio_device",
  765. .release = iio_dev_release,
  766. };
  767. struct iio_dev *iio_device_alloc(int sizeof_priv)
  768. {
  769. struct iio_dev *dev;
  770. size_t alloc_size;
  771. alloc_size = sizeof(struct iio_dev);
  772. if (sizeof_priv) {
  773. alloc_size = ALIGN(alloc_size, IIO_ALIGN);
  774. alloc_size += sizeof_priv;
  775. }
  776. /* ensure 32-byte alignment of whole construct ? */
  777. alloc_size += IIO_ALIGN - 1;
  778. dev = kzalloc(alloc_size, GFP_KERNEL);
  779. if (dev) {
  780. dev->dev.groups = dev->groups;
  781. dev->dev.type = &iio_device_type;
  782. dev->dev.bus = &iio_bus_type;
  783. device_initialize(&dev->dev);
  784. dev_set_drvdata(&dev->dev, (void *)dev);
  785. mutex_init(&dev->mlock);
  786. mutex_init(&dev->info_exist_lock);
  787. INIT_LIST_HEAD(&dev->channel_attr_list);
  788. dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
  789. if (dev->id < 0) {
  790. /* cannot use a dev_err as the name isn't available */
  791. printk(KERN_ERR "Failed to get id\n");
  792. kfree(dev);
  793. return NULL;
  794. }
  795. dev_set_name(&dev->dev, "iio:device%d", dev->id);
  796. INIT_LIST_HEAD(&dev->buffer_list);
  797. }
  798. return dev;
  799. }
  800. EXPORT_SYMBOL(iio_device_alloc);
  801. void iio_device_free(struct iio_dev *dev)
  802. {
  803. if (dev)
  804. put_device(&dev->dev);
  805. }
  806. EXPORT_SYMBOL(iio_device_free);
  807. static void devm_iio_device_release(struct device *dev, void *res)
  808. {
  809. iio_device_free(*(struct iio_dev **)res);
  810. }
  811. static int devm_iio_device_match(struct device *dev, void *res, void *data)
  812. {
  813. struct iio_dev **r = res;
  814. if (!r || !*r) {
  815. WARN_ON(!r || !*r);
  816. return 0;
  817. }
  818. return *r == data;
  819. }
  820. struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
  821. {
  822. struct iio_dev **ptr, *iio_dev;
  823. ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
  824. GFP_KERNEL);
  825. if (!ptr)
  826. return NULL;
  827. /* use raw alloc_dr for kmalloc caller tracing */
  828. iio_dev = iio_device_alloc(sizeof_priv);
  829. if (iio_dev) {
  830. *ptr = iio_dev;
  831. devres_add(dev, ptr);
  832. } else {
  833. devres_free(ptr);
  834. }
  835. return iio_dev;
  836. }
  837. EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
  838. void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
  839. {
  840. int rc;
  841. rc = devres_release(dev, devm_iio_device_release,
  842. devm_iio_device_match, iio_dev);
  843. WARN_ON(rc);
  844. }
  845. EXPORT_SYMBOL_GPL(devm_iio_device_free);
  846. /**
  847. * iio_chrdev_open() - chrdev file open for buffer access and ioctls
  848. **/
  849. static int iio_chrdev_open(struct inode *inode, struct file *filp)
  850. {
  851. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  852. struct iio_dev, chrdev);
  853. if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
  854. return -EBUSY;
  855. filp->private_data = indio_dev;
  856. return 0;
  857. }
  858. /**
  859. * iio_chrdev_release() - chrdev file close buffer access and ioctls
  860. **/
  861. static int iio_chrdev_release(struct inode *inode, struct file *filp)
  862. {
  863. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  864. struct iio_dev, chrdev);
  865. clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
  866. return 0;
  867. }
  868. /* Somewhat of a cross file organization violation - ioctls here are actually
  869. * event related */
  870. static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  871. {
  872. struct iio_dev *indio_dev = filp->private_data;
  873. int __user *ip = (int __user *)arg;
  874. int fd;
  875. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  876. fd = iio_event_getfd(indio_dev);
  877. if (copy_to_user(ip, &fd, sizeof(fd)))
  878. return -EFAULT;
  879. return 0;
  880. }
  881. return -EINVAL;
  882. }
  883. static const struct file_operations iio_buffer_fileops = {
  884. .read = iio_buffer_read_first_n_outer_addr,
  885. .release = iio_chrdev_release,
  886. .open = iio_chrdev_open,
  887. .poll = iio_buffer_poll_addr,
  888. .owner = THIS_MODULE,
  889. .llseek = noop_llseek,
  890. .unlocked_ioctl = iio_ioctl,
  891. .compat_ioctl = iio_ioctl,
  892. };
  893. static const struct iio_buffer_setup_ops noop_ring_setup_ops;
  894. int iio_device_register(struct iio_dev *indio_dev)
  895. {
  896. int ret;
  897. /* If the calling driver did not initialize of_node, do it here */
  898. if (!indio_dev->dev.of_node && indio_dev->dev.parent)
  899. indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
  900. /* configure elements for the chrdev */
  901. indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
  902. ret = iio_device_register_debugfs(indio_dev);
  903. if (ret) {
  904. dev_err(indio_dev->dev.parent,
  905. "Failed to register debugfs interfaces\n");
  906. goto error_ret;
  907. }
  908. ret = iio_device_register_sysfs(indio_dev);
  909. if (ret) {
  910. dev_err(indio_dev->dev.parent,
  911. "Failed to register sysfs interfaces\n");
  912. goto error_unreg_debugfs;
  913. }
  914. ret = iio_device_register_eventset(indio_dev);
  915. if (ret) {
  916. dev_err(indio_dev->dev.parent,
  917. "Failed to register event set\n");
  918. goto error_free_sysfs;
  919. }
  920. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  921. iio_device_register_trigger_consumer(indio_dev);
  922. if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
  923. indio_dev->setup_ops == NULL)
  924. indio_dev->setup_ops = &noop_ring_setup_ops;
  925. ret = device_add(&indio_dev->dev);
  926. if (ret < 0)
  927. goto error_unreg_eventset;
  928. cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
  929. indio_dev->chrdev.owner = indio_dev->info->driver_module;
  930. ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
  931. if (ret < 0)
  932. goto error_del_device;
  933. return 0;
  934. error_del_device:
  935. device_del(&indio_dev->dev);
  936. error_unreg_eventset:
  937. iio_device_unregister_eventset(indio_dev);
  938. error_free_sysfs:
  939. iio_device_unregister_sysfs(indio_dev);
  940. error_unreg_debugfs:
  941. iio_device_unregister_debugfs(indio_dev);
  942. error_ret:
  943. return ret;
  944. }
  945. EXPORT_SYMBOL(iio_device_register);
  946. void iio_device_unregister(struct iio_dev *indio_dev)
  947. {
  948. mutex_lock(&indio_dev->info_exist_lock);
  949. indio_dev->info = NULL;
  950. mutex_unlock(&indio_dev->info_exist_lock);
  951. device_del(&indio_dev->dev);
  952. }
  953. EXPORT_SYMBOL(iio_device_unregister);
  954. subsys_initcall(iio_init);
  955. module_exit(iio_exit);
  956. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  957. MODULE_DESCRIPTION("Industrial I/O core");
  958. MODULE_LICENSE("GPL");