industrialio-core.c 31 KB

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