industrialio-core.c 27 KB

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