industrialio-core.c 36 KB

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