industrialio-core.c 28 KB

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