industrialio-core.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. };
  96. const struct iio_chan_spec
  97. *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
  98. {
  99. int i;
  100. for (i = 0; i < indio_dev->num_channels; i++)
  101. if (indio_dev->channels[i].scan_index == si)
  102. return &indio_dev->channels[i];
  103. return NULL;
  104. }
  105. /* This turns up an awful lot */
  106. ssize_t iio_read_const_attr(struct device *dev,
  107. struct device_attribute *attr,
  108. char *buf)
  109. {
  110. return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
  111. }
  112. EXPORT_SYMBOL(iio_read_const_attr);
  113. static int __init iio_init(void)
  114. {
  115. int ret;
  116. /* Register sysfs bus */
  117. ret = bus_register(&iio_bus_type);
  118. if (ret < 0) {
  119. printk(KERN_ERR
  120. "%s could not register bus type\n",
  121. __FILE__);
  122. goto error_nothing;
  123. }
  124. ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
  125. if (ret < 0) {
  126. printk(KERN_ERR "%s: failed to allocate char dev region\n",
  127. __FILE__);
  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. static ssize_t iio_read_channel_info(struct device *dev,
  306. struct device_attribute *attr,
  307. char *buf)
  308. {
  309. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  310. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  311. unsigned long long tmp;
  312. int val, val2;
  313. bool scale_db = false;
  314. int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
  315. &val, &val2, this_attr->address);
  316. if (ret < 0)
  317. return ret;
  318. switch (ret) {
  319. case IIO_VAL_INT:
  320. return sprintf(buf, "%d\n", val);
  321. case IIO_VAL_INT_PLUS_MICRO_DB:
  322. scale_db = true;
  323. case IIO_VAL_INT_PLUS_MICRO:
  324. if (val2 < 0)
  325. return sprintf(buf, "-%ld.%06u%s\n", abs(val), -val2,
  326. scale_db ? " dB" : "");
  327. else
  328. return sprintf(buf, "%d.%06u%s\n", val, val2,
  329. scale_db ? " dB" : "");
  330. case IIO_VAL_INT_PLUS_NANO:
  331. if (val2 < 0)
  332. return sprintf(buf, "-%ld.%09u\n", abs(val), -val2);
  333. else
  334. return sprintf(buf, "%d.%09u\n", val, val2);
  335. case IIO_VAL_FRACTIONAL:
  336. tmp = div_s64((s64)val * 1000000000LL, val2);
  337. val2 = do_div(tmp, 1000000000LL);
  338. val = tmp;
  339. return sprintf(buf, "%d.%09u\n", val, val2);
  340. case IIO_VAL_FRACTIONAL_LOG2:
  341. tmp = (s64)val * 1000000000LL >> val2;
  342. val2 = do_div(tmp, 1000000000LL);
  343. val = tmp;
  344. return sprintf(buf, "%d.%09u\n", val, val2);
  345. default:
  346. return 0;
  347. }
  348. }
  349. /**
  350. * iio_str_to_fixpoint() - Parse a fixed-point number from a string
  351. * @str: The string to parse
  352. * @fract_mult: Multiplier for the first decimal place, should be a power of 10
  353. * @integer: The integer part of the number
  354. * @fract: The fractional part of the number
  355. *
  356. * Returns 0 on success, or a negative error code if the string could not be
  357. * parsed.
  358. */
  359. int iio_str_to_fixpoint(const char *str, int fract_mult,
  360. int *integer, int *fract)
  361. {
  362. int i = 0, f = 0;
  363. bool integer_part = true, negative = false;
  364. if (str[0] == '-') {
  365. negative = true;
  366. str++;
  367. } else if (str[0] == '+') {
  368. str++;
  369. }
  370. while (*str) {
  371. if ('0' <= *str && *str <= '9') {
  372. if (integer_part) {
  373. i = i * 10 + *str - '0';
  374. } else {
  375. f += fract_mult * (*str - '0');
  376. fract_mult /= 10;
  377. }
  378. } else if (*str == '\n') {
  379. if (*(str + 1) == '\0')
  380. break;
  381. else
  382. return -EINVAL;
  383. } else if (*str == '.' && integer_part) {
  384. integer_part = false;
  385. } else {
  386. return -EINVAL;
  387. }
  388. str++;
  389. }
  390. if (negative) {
  391. if (i)
  392. i = -i;
  393. else
  394. f = -f;
  395. }
  396. *integer = i;
  397. *fract = f;
  398. return 0;
  399. }
  400. EXPORT_SYMBOL_GPL(iio_str_to_fixpoint);
  401. static ssize_t iio_write_channel_info(struct device *dev,
  402. struct device_attribute *attr,
  403. const char *buf,
  404. size_t len)
  405. {
  406. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  407. struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
  408. int ret, fract_mult = 100000;
  409. int integer, fract;
  410. /* Assumes decimal - precision based on number of digits */
  411. if (!indio_dev->info->write_raw)
  412. return -EINVAL;
  413. if (indio_dev->info->write_raw_get_fmt)
  414. switch (indio_dev->info->write_raw_get_fmt(indio_dev,
  415. this_attr->c, this_attr->address)) {
  416. case IIO_VAL_INT_PLUS_MICRO:
  417. fract_mult = 100000;
  418. break;
  419. case IIO_VAL_INT_PLUS_NANO:
  420. fract_mult = 100000000;
  421. break;
  422. default:
  423. return -EINVAL;
  424. }
  425. ret = iio_str_to_fixpoint(buf, fract_mult, &integer, &fract);
  426. if (ret)
  427. return ret;
  428. ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
  429. integer, fract, this_attr->address);
  430. if (ret)
  431. return ret;
  432. return len;
  433. }
  434. static
  435. int __iio_device_attr_init(struct device_attribute *dev_attr,
  436. const char *postfix,
  437. struct iio_chan_spec const *chan,
  438. ssize_t (*readfunc)(struct device *dev,
  439. struct device_attribute *attr,
  440. char *buf),
  441. ssize_t (*writefunc)(struct device *dev,
  442. struct device_attribute *attr,
  443. const char *buf,
  444. size_t len),
  445. bool generic)
  446. {
  447. int ret;
  448. char *name_format, *full_postfix;
  449. sysfs_attr_init(&dev_attr->attr);
  450. /* Build up postfix of <extend_name>_<modifier>_postfix */
  451. if (chan->modified && !generic) {
  452. if (chan->extend_name)
  453. full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
  454. iio_modifier_names[chan
  455. ->channel2],
  456. chan->extend_name,
  457. postfix);
  458. else
  459. full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
  460. iio_modifier_names[chan
  461. ->channel2],
  462. postfix);
  463. } else {
  464. if (chan->extend_name == NULL)
  465. full_postfix = kstrdup(postfix, GFP_KERNEL);
  466. else
  467. full_postfix = kasprintf(GFP_KERNEL,
  468. "%s_%s",
  469. chan->extend_name,
  470. postfix);
  471. }
  472. if (full_postfix == NULL) {
  473. ret = -ENOMEM;
  474. goto error_ret;
  475. }
  476. if (chan->differential) { /* Differential can not have modifier */
  477. if (generic)
  478. name_format
  479. = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
  480. iio_direction[chan->output],
  481. iio_chan_type_name_spec[chan->type],
  482. iio_chan_type_name_spec[chan->type],
  483. full_postfix);
  484. else if (chan->indexed)
  485. name_format
  486. = kasprintf(GFP_KERNEL, "%s_%s%d-%s%d_%s",
  487. iio_direction[chan->output],
  488. iio_chan_type_name_spec[chan->type],
  489. chan->channel,
  490. iio_chan_type_name_spec[chan->type],
  491. chan->channel2,
  492. full_postfix);
  493. else {
  494. WARN_ON("Differential channels must be indexed\n");
  495. ret = -EINVAL;
  496. goto error_free_full_postfix;
  497. }
  498. } else { /* Single ended */
  499. if (generic)
  500. name_format
  501. = kasprintf(GFP_KERNEL, "%s_%s_%s",
  502. iio_direction[chan->output],
  503. iio_chan_type_name_spec[chan->type],
  504. full_postfix);
  505. else if (chan->indexed)
  506. name_format
  507. = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
  508. iio_direction[chan->output],
  509. iio_chan_type_name_spec[chan->type],
  510. chan->channel,
  511. full_postfix);
  512. else
  513. name_format
  514. = kasprintf(GFP_KERNEL, "%s_%s_%s",
  515. iio_direction[chan->output],
  516. iio_chan_type_name_spec[chan->type],
  517. full_postfix);
  518. }
  519. if (name_format == NULL) {
  520. ret = -ENOMEM;
  521. goto error_free_full_postfix;
  522. }
  523. dev_attr->attr.name = kasprintf(GFP_KERNEL,
  524. name_format,
  525. chan->channel,
  526. chan->channel2);
  527. if (dev_attr->attr.name == NULL) {
  528. ret = -ENOMEM;
  529. goto error_free_name_format;
  530. }
  531. if (readfunc) {
  532. dev_attr->attr.mode |= S_IRUGO;
  533. dev_attr->show = readfunc;
  534. }
  535. if (writefunc) {
  536. dev_attr->attr.mode |= S_IWUSR;
  537. dev_attr->store = writefunc;
  538. }
  539. kfree(name_format);
  540. kfree(full_postfix);
  541. return 0;
  542. error_free_name_format:
  543. kfree(name_format);
  544. error_free_full_postfix:
  545. kfree(full_postfix);
  546. error_ret:
  547. return ret;
  548. }
  549. static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
  550. {
  551. kfree(dev_attr->attr.name);
  552. }
  553. int __iio_add_chan_devattr(const char *postfix,
  554. struct iio_chan_spec const *chan,
  555. ssize_t (*readfunc)(struct device *dev,
  556. struct device_attribute *attr,
  557. char *buf),
  558. ssize_t (*writefunc)(struct device *dev,
  559. struct device_attribute *attr,
  560. const char *buf,
  561. size_t len),
  562. u64 mask,
  563. bool generic,
  564. struct device *dev,
  565. struct list_head *attr_list)
  566. {
  567. int ret;
  568. struct iio_dev_attr *iio_attr, *t;
  569. iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
  570. if (iio_attr == NULL) {
  571. ret = -ENOMEM;
  572. goto error_ret;
  573. }
  574. ret = __iio_device_attr_init(&iio_attr->dev_attr,
  575. postfix, chan,
  576. readfunc, writefunc, generic);
  577. if (ret)
  578. goto error_iio_dev_attr_free;
  579. iio_attr->c = chan;
  580. iio_attr->address = mask;
  581. list_for_each_entry(t, attr_list, l)
  582. if (strcmp(t->dev_attr.attr.name,
  583. iio_attr->dev_attr.attr.name) == 0) {
  584. if (!generic)
  585. dev_err(dev, "tried to double register : %s\n",
  586. t->dev_attr.attr.name);
  587. ret = -EBUSY;
  588. goto error_device_attr_deinit;
  589. }
  590. list_add(&iio_attr->l, attr_list);
  591. return 0;
  592. error_device_attr_deinit:
  593. __iio_device_attr_deinit(&iio_attr->dev_attr);
  594. error_iio_dev_attr_free:
  595. kfree(iio_attr);
  596. error_ret:
  597. return ret;
  598. }
  599. static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
  600. struct iio_chan_spec const *chan)
  601. {
  602. int ret, attrcount = 0;
  603. int i;
  604. const struct iio_chan_spec_ext_info *ext_info;
  605. if (chan->channel < 0)
  606. return 0;
  607. for_each_set_bit(i, &chan->info_mask_separate, sizeof(long)*8) {
  608. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  609. chan,
  610. &iio_read_channel_info,
  611. &iio_write_channel_info,
  612. i,
  613. 0,
  614. &indio_dev->dev,
  615. &indio_dev->channel_attr_list);
  616. if (ret < 0)
  617. goto error_ret;
  618. attrcount++;
  619. }
  620. for_each_set_bit(i, &chan->info_mask_shared_by_type, sizeof(long)*8) {
  621. ret = __iio_add_chan_devattr(iio_chan_info_postfix[i],
  622. chan,
  623. &iio_read_channel_info,
  624. &iio_write_channel_info,
  625. i,
  626. 1,
  627. &indio_dev->dev,
  628. &indio_dev->channel_attr_list);
  629. if (ret == -EBUSY) {
  630. ret = 0;
  631. continue;
  632. } else if (ret < 0) {
  633. goto error_ret;
  634. }
  635. attrcount++;
  636. }
  637. if (chan->ext_info) {
  638. unsigned int i = 0;
  639. for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
  640. ret = __iio_add_chan_devattr(ext_info->name,
  641. chan,
  642. ext_info->read ?
  643. &iio_read_channel_ext_info : NULL,
  644. ext_info->write ?
  645. &iio_write_channel_ext_info : NULL,
  646. i,
  647. ext_info->shared,
  648. &indio_dev->dev,
  649. &indio_dev->channel_attr_list);
  650. i++;
  651. if (ret == -EBUSY && ext_info->shared)
  652. continue;
  653. if (ret)
  654. goto error_ret;
  655. attrcount++;
  656. }
  657. }
  658. ret = attrcount;
  659. error_ret:
  660. return ret;
  661. }
  662. static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
  663. struct iio_dev_attr *p)
  664. {
  665. kfree(p->dev_attr.attr.name);
  666. kfree(p);
  667. }
  668. static ssize_t iio_show_dev_name(struct device *dev,
  669. struct device_attribute *attr,
  670. char *buf)
  671. {
  672. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  673. return sprintf(buf, "%s\n", indio_dev->name);
  674. }
  675. static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
  676. static int iio_device_register_sysfs(struct iio_dev *indio_dev)
  677. {
  678. int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
  679. struct iio_dev_attr *p, *n;
  680. struct attribute **attr;
  681. /* First count elements in any existing group */
  682. if (indio_dev->info->attrs) {
  683. attr = indio_dev->info->attrs->attrs;
  684. while (*attr++ != NULL)
  685. attrcount_orig++;
  686. }
  687. attrcount = attrcount_orig;
  688. /*
  689. * New channel registration method - relies on the fact a group does
  690. * not need to be initialized if its name is NULL.
  691. */
  692. if (indio_dev->channels)
  693. for (i = 0; i < indio_dev->num_channels; i++) {
  694. ret = iio_device_add_channel_sysfs(indio_dev,
  695. &indio_dev
  696. ->channels[i]);
  697. if (ret < 0)
  698. goto error_clear_attrs;
  699. attrcount += ret;
  700. }
  701. if (indio_dev->name)
  702. attrcount++;
  703. indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
  704. sizeof(indio_dev->chan_attr_group.attrs[0]),
  705. GFP_KERNEL);
  706. if (indio_dev->chan_attr_group.attrs == NULL) {
  707. ret = -ENOMEM;
  708. goto error_clear_attrs;
  709. }
  710. /* Copy across original attributes */
  711. if (indio_dev->info->attrs)
  712. memcpy(indio_dev->chan_attr_group.attrs,
  713. indio_dev->info->attrs->attrs,
  714. sizeof(indio_dev->chan_attr_group.attrs[0])
  715. *attrcount_orig);
  716. attrn = attrcount_orig;
  717. /* Add all elements from the list. */
  718. list_for_each_entry(p, &indio_dev->channel_attr_list, l)
  719. indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
  720. if (indio_dev->name)
  721. indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
  722. indio_dev->groups[indio_dev->groupcounter++] =
  723. &indio_dev->chan_attr_group;
  724. return 0;
  725. error_clear_attrs:
  726. list_for_each_entry_safe(p, n,
  727. &indio_dev->channel_attr_list, l) {
  728. list_del(&p->l);
  729. iio_device_remove_and_free_read_attr(indio_dev, p);
  730. }
  731. return ret;
  732. }
  733. static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
  734. {
  735. struct iio_dev_attr *p, *n;
  736. list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
  737. list_del(&p->l);
  738. iio_device_remove_and_free_read_attr(indio_dev, p);
  739. }
  740. kfree(indio_dev->chan_attr_group.attrs);
  741. }
  742. static void iio_dev_release(struct device *device)
  743. {
  744. struct iio_dev *indio_dev = dev_to_iio_dev(device);
  745. if (indio_dev->chrdev.dev)
  746. cdev_del(&indio_dev->chrdev);
  747. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  748. iio_device_unregister_trigger_consumer(indio_dev);
  749. iio_device_unregister_eventset(indio_dev);
  750. iio_device_unregister_sysfs(indio_dev);
  751. iio_device_unregister_debugfs(indio_dev);
  752. ida_simple_remove(&iio_ida, indio_dev->id);
  753. kfree(indio_dev);
  754. }
  755. struct device_type iio_device_type = {
  756. .name = "iio_device",
  757. .release = iio_dev_release,
  758. };
  759. struct iio_dev *iio_device_alloc(int sizeof_priv)
  760. {
  761. struct iio_dev *dev;
  762. size_t alloc_size;
  763. alloc_size = sizeof(struct iio_dev);
  764. if (sizeof_priv) {
  765. alloc_size = ALIGN(alloc_size, IIO_ALIGN);
  766. alloc_size += sizeof_priv;
  767. }
  768. /* ensure 32-byte alignment of whole construct ? */
  769. alloc_size += IIO_ALIGN - 1;
  770. dev = kzalloc(alloc_size, GFP_KERNEL);
  771. if (dev) {
  772. dev->dev.groups = dev->groups;
  773. dev->dev.type = &iio_device_type;
  774. dev->dev.bus = &iio_bus_type;
  775. device_initialize(&dev->dev);
  776. dev_set_drvdata(&dev->dev, (void *)dev);
  777. mutex_init(&dev->mlock);
  778. mutex_init(&dev->info_exist_lock);
  779. INIT_LIST_HEAD(&dev->channel_attr_list);
  780. dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
  781. if (dev->id < 0) {
  782. /* cannot use a dev_err as the name isn't available */
  783. printk(KERN_ERR "Failed to get id\n");
  784. kfree(dev);
  785. return NULL;
  786. }
  787. dev_set_name(&dev->dev, "iio:device%d", dev->id);
  788. INIT_LIST_HEAD(&dev->buffer_list);
  789. }
  790. return dev;
  791. }
  792. EXPORT_SYMBOL(iio_device_alloc);
  793. void iio_device_free(struct iio_dev *dev)
  794. {
  795. if (dev)
  796. put_device(&dev->dev);
  797. }
  798. EXPORT_SYMBOL(iio_device_free);
  799. static void devm_iio_device_release(struct device *dev, void *res)
  800. {
  801. iio_device_free(*(struct iio_dev **)res);
  802. }
  803. static int devm_iio_device_match(struct device *dev, void *res, void *data)
  804. {
  805. struct iio_dev **r = res;
  806. if (!r || !*r) {
  807. WARN_ON(!r || !*r);
  808. return 0;
  809. }
  810. return *r == data;
  811. }
  812. struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
  813. {
  814. struct iio_dev **ptr, *iio_dev;
  815. ptr = devres_alloc(devm_iio_device_release, sizeof(*ptr),
  816. GFP_KERNEL);
  817. if (!ptr)
  818. return NULL;
  819. /* use raw alloc_dr for kmalloc caller tracing */
  820. iio_dev = iio_device_alloc(sizeof_priv);
  821. if (iio_dev) {
  822. *ptr = iio_dev;
  823. devres_add(dev, ptr);
  824. } else {
  825. devres_free(ptr);
  826. }
  827. return iio_dev;
  828. }
  829. EXPORT_SYMBOL_GPL(devm_iio_device_alloc);
  830. void devm_iio_device_free(struct device *dev, struct iio_dev *iio_dev)
  831. {
  832. int rc;
  833. rc = devres_release(dev, devm_iio_device_release,
  834. devm_iio_device_match, iio_dev);
  835. WARN_ON(rc);
  836. }
  837. EXPORT_SYMBOL_GPL(devm_iio_device_free);
  838. /**
  839. * iio_chrdev_open() - chrdev file open for buffer access and ioctls
  840. **/
  841. static int iio_chrdev_open(struct inode *inode, struct file *filp)
  842. {
  843. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  844. struct iio_dev, chrdev);
  845. if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
  846. return -EBUSY;
  847. filp->private_data = indio_dev;
  848. return 0;
  849. }
  850. /**
  851. * iio_chrdev_release() - chrdev file close buffer access and ioctls
  852. **/
  853. static int iio_chrdev_release(struct inode *inode, struct file *filp)
  854. {
  855. struct iio_dev *indio_dev = container_of(inode->i_cdev,
  856. struct iio_dev, chrdev);
  857. clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
  858. return 0;
  859. }
  860. /* Somewhat of a cross file organization violation - ioctls here are actually
  861. * event related */
  862. static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  863. {
  864. struct iio_dev *indio_dev = filp->private_data;
  865. int __user *ip = (int __user *)arg;
  866. int fd;
  867. if (cmd == IIO_GET_EVENT_FD_IOCTL) {
  868. fd = iio_event_getfd(indio_dev);
  869. if (copy_to_user(ip, &fd, sizeof(fd)))
  870. return -EFAULT;
  871. return 0;
  872. }
  873. return -EINVAL;
  874. }
  875. static const struct file_operations iio_buffer_fileops = {
  876. .read = iio_buffer_read_first_n_outer_addr,
  877. .release = iio_chrdev_release,
  878. .open = iio_chrdev_open,
  879. .poll = iio_buffer_poll_addr,
  880. .owner = THIS_MODULE,
  881. .llseek = noop_llseek,
  882. .unlocked_ioctl = iio_ioctl,
  883. .compat_ioctl = iio_ioctl,
  884. };
  885. static const struct iio_buffer_setup_ops noop_ring_setup_ops;
  886. int iio_device_register(struct iio_dev *indio_dev)
  887. {
  888. int ret;
  889. /* If the calling driver did not initialize of_node, do it here */
  890. if (!indio_dev->dev.of_node && indio_dev->dev.parent)
  891. indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
  892. /* configure elements for the chrdev */
  893. indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
  894. ret = iio_device_register_debugfs(indio_dev);
  895. if (ret) {
  896. dev_err(indio_dev->dev.parent,
  897. "Failed to register debugfs interfaces\n");
  898. goto error_ret;
  899. }
  900. ret = iio_device_register_sysfs(indio_dev);
  901. if (ret) {
  902. dev_err(indio_dev->dev.parent,
  903. "Failed to register sysfs interfaces\n");
  904. goto error_unreg_debugfs;
  905. }
  906. ret = iio_device_register_eventset(indio_dev);
  907. if (ret) {
  908. dev_err(indio_dev->dev.parent,
  909. "Failed to register event set\n");
  910. goto error_free_sysfs;
  911. }
  912. if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
  913. iio_device_register_trigger_consumer(indio_dev);
  914. if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
  915. indio_dev->setup_ops == NULL)
  916. indio_dev->setup_ops = &noop_ring_setup_ops;
  917. ret = device_add(&indio_dev->dev);
  918. if (ret < 0)
  919. goto error_unreg_eventset;
  920. cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
  921. indio_dev->chrdev.owner = indio_dev->info->driver_module;
  922. ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
  923. if (ret < 0)
  924. goto error_del_device;
  925. return 0;
  926. error_del_device:
  927. device_del(&indio_dev->dev);
  928. error_unreg_eventset:
  929. iio_device_unregister_eventset(indio_dev);
  930. error_free_sysfs:
  931. iio_device_unregister_sysfs(indio_dev);
  932. error_unreg_debugfs:
  933. iio_device_unregister_debugfs(indio_dev);
  934. error_ret:
  935. return ret;
  936. }
  937. EXPORT_SYMBOL(iio_device_register);
  938. void iio_device_unregister(struct iio_dev *indio_dev)
  939. {
  940. mutex_lock(&indio_dev->info_exist_lock);
  941. indio_dev->info = NULL;
  942. mutex_unlock(&indio_dev->info_exist_lock);
  943. device_del(&indio_dev->dev);
  944. }
  945. EXPORT_SYMBOL(iio_device_unregister);
  946. subsys_initcall(iio_init);
  947. module_exit(iio_exit);
  948. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  949. MODULE_DESCRIPTION("Industrial I/O core");
  950. MODULE_LICENSE("GPL");