industrialio-core.c 35 KB

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