industrialio-core.c 33 KB

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