industrialio-core.c 34 KB

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