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 
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 "iio.h"
27 #include "iio_core.h"
28 #include "iio_core_trigger.h"
29 #include "sysfs.h"
30 #include "events.h"
31 
32 /* IDA to assign each registered device a unique id*/
33 static DEFINE_IDA(iio_ida);
34 
35 static dev_t iio_devt;
36 
37 #define IIO_DEV_MAX 256
38 struct bus_type iio_bus_type = {
39 	.name = "iio",
40 };
41 EXPORT_SYMBOL(iio_bus_type);
42 
43 static struct dentry *iio_debugfs_dentry;
44 
45 static const char * const iio_data_type_name[] = {
46 	[IIO_RAW] = "raw",
47 	[IIO_PROCESSED] = "input",
48 };
49 
50 static const char * const iio_direction[] = {
51 	[0] = "in",
52 	[1] = "out",
53 };
54 
55 static const char * const iio_chan_type_name_spec[] = {
56 	[IIO_VOLTAGE] = "voltage",
57 	[IIO_CURRENT] = "current",
58 	[IIO_POWER] = "power",
59 	[IIO_ACCEL] = "accel",
60 	[IIO_ANGL_VEL] = "anglvel",
61 	[IIO_MAGN] = "magn",
62 	[IIO_LIGHT] = "illuminance",
63 	[IIO_INTENSITY] = "intensity",
64 	[IIO_PROXIMITY] = "proximity",
65 	[IIO_TEMP] = "temp",
66 	[IIO_INCLI] = "incli",
67 	[IIO_ROT] = "rot",
68 	[IIO_ANGL] = "angl",
69 	[IIO_TIMESTAMP] = "timestamp",
70 	[IIO_CAPACITANCE] = "capacitance",
71 };
72 
73 static const char * const iio_modifier_names[] = {
74 	[IIO_MOD_X] = "x",
75 	[IIO_MOD_Y] = "y",
76 	[IIO_MOD_Z] = "z",
77 	[IIO_MOD_LIGHT_BOTH] = "both",
78 	[IIO_MOD_LIGHT_IR] = "ir",
79 };
80 
81 /* relies on pairs of these shared then separate */
82 static const char * const iio_chan_info_postfix[] = {
83 	[IIO_CHAN_INFO_SCALE] = "scale",
84 	[IIO_CHAN_INFO_OFFSET] = "offset",
85 	[IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
86 	[IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
87 	[IIO_CHAN_INFO_PEAK] = "peak_raw",
88 	[IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
89 	[IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
90 	[IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
91 	[IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
92 	= "filter_low_pass_3db_frequency",
93 };
94 
95 const struct iio_chan_spec
iio_find_channel_from_si(struct iio_dev * indio_dev,int si)96 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
97 {
98 	int i;
99 
100 	for (i = 0; i < indio_dev->num_channels; i++)
101 		if (indio_dev->channels[i].scan_index == si)
102 			return &indio_dev->channels[i];
103 	return NULL;
104 }
105 
106 /* This turns up an awful lot */
iio_read_const_attr(struct device * dev,struct device_attribute * attr,char * buf)107 ssize_t iio_read_const_attr(struct device *dev,
108 			    struct device_attribute *attr,
109 			    char *buf)
110 {
111 	return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
112 }
113 EXPORT_SYMBOL(iio_read_const_attr);
114 
iio_init(void)115 static int __init iio_init(void)
116 {
117 	int ret;
118 
119 	/* Register sysfs bus */
120 	ret  = bus_register(&iio_bus_type);
121 	if (ret < 0) {
122 		printk(KERN_ERR
123 		       "%s could not register bus type\n",
124 			__FILE__);
125 		goto error_nothing;
126 	}
127 
128 	ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
129 	if (ret < 0) {
130 		printk(KERN_ERR "%s: failed to allocate char dev region\n",
131 		       __FILE__);
132 		goto error_unregister_bus_type;
133 	}
134 
135 	iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
136 
137 	return 0;
138 
139 error_unregister_bus_type:
140 	bus_unregister(&iio_bus_type);
141 error_nothing:
142 	return ret;
143 }
144 
iio_exit(void)145 static void __exit iio_exit(void)
146 {
147 	if (iio_devt)
148 		unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
149 	bus_unregister(&iio_bus_type);
150 	debugfs_remove(iio_debugfs_dentry);
151 }
152 
153 #if defined(CONFIG_DEBUG_FS)
iio_debugfs_open(struct inode * inode,struct file * file)154 static int iio_debugfs_open(struct inode *inode, struct file *file)
155 {
156 	if (inode->i_private)
157 		file->private_data = inode->i_private;
158 
159 	return 0;
160 }
161 
iio_debugfs_read_reg(struct file * file,char __user * userbuf,size_t count,loff_t * ppos)162 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
163 			      size_t count, loff_t *ppos)
164 {
165 	struct iio_dev *indio_dev = file->private_data;
166 	char buf[20];
167 	unsigned val = 0;
168 	ssize_t len;
169 	int ret;
170 
171 	ret = indio_dev->info->debugfs_reg_access(indio_dev,
172 						  indio_dev->cached_reg_addr,
173 						  0, &val);
174 	if (ret)
175 		dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
176 
177 	len = snprintf(buf, sizeof(buf), "0x%X\n", val);
178 
179 	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
180 }
181 
iio_debugfs_write_reg(struct file * file,const char __user * userbuf,size_t count,loff_t * ppos)182 static ssize_t iio_debugfs_write_reg(struct file *file,
183 		     const char __user *userbuf, size_t count, loff_t *ppos)
184 {
185 	struct iio_dev *indio_dev = file->private_data;
186 	unsigned reg, val;
187 	char buf[80];
188 	int ret;
189 
190 	count = min_t(size_t, count, (sizeof(buf)-1));
191 	if (copy_from_user(buf, userbuf, count))
192 		return -EFAULT;
193 
194 	buf[count] = 0;
195 
196 	ret = sscanf(buf, "%i %i", &reg, &val);
197 
198 	switch (ret) {
199 	case 1:
200 		indio_dev->cached_reg_addr = reg;
201 		break;
202 	case 2:
203 		indio_dev->cached_reg_addr = reg;
204 		ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
205 							  val, NULL);
206 		if (ret) {
207 			dev_err(indio_dev->dev.parent, "%s: write failed\n",
208 				__func__);
209 			return ret;
210 		}
211 		break;
212 	default:
213 		return -EINVAL;
214 	}
215 
216 	return count;
217 }
218 
219 static const struct file_operations iio_debugfs_reg_fops = {
220 	.open = iio_debugfs_open,
221 	.read = iio_debugfs_read_reg,
222 	.write = iio_debugfs_write_reg,
223 };
224 
iio_device_unregister_debugfs(struct iio_dev * indio_dev)225 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
226 {
227 	debugfs_remove_recursive(indio_dev->debugfs_dentry);
228 }
229 
iio_device_register_debugfs(struct iio_dev * indio_dev)230 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
231 {
232 	struct dentry *d;
233 
234 	if (indio_dev->info->debugfs_reg_access == NULL)
235 		return 0;
236 
237 	if (IS_ERR(iio_debugfs_dentry))
238 		return 0;
239 
240 	indio_dev->debugfs_dentry =
241 		debugfs_create_dir(dev_name(&indio_dev->dev),
242 				   iio_debugfs_dentry);
243 	if (IS_ERR(indio_dev->debugfs_dentry))
244 		return PTR_ERR(indio_dev->debugfs_dentry);
245 
246 	if (indio_dev->debugfs_dentry == NULL) {
247 		dev_warn(indio_dev->dev.parent,
248 			 "Failed to create debugfs directory\n");
249 		return -EFAULT;
250 	}
251 
252 	d = debugfs_create_file("direct_reg_access", 0644,
253 				indio_dev->debugfs_dentry,
254 				indio_dev, &iio_debugfs_reg_fops);
255 	if (!d) {
256 		iio_device_unregister_debugfs(indio_dev);
257 		return -ENOMEM;
258 	}
259 
260 	return 0;
261 }
262 #else
iio_device_register_debugfs(struct iio_dev * indio_dev)263 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
264 {
265 	return 0;
266 }
267 
iio_device_unregister_debugfs(struct iio_dev * indio_dev)268 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
269 {
270 }
271 #endif /* CONFIG_DEBUG_FS */
272 
iio_read_channel_ext_info(struct device * dev,struct device_attribute * attr,char * buf)273 static ssize_t iio_read_channel_ext_info(struct device *dev,
274 				     struct device_attribute *attr,
275 				     char *buf)
276 {
277 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
278 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
279 	const struct iio_chan_spec_ext_info *ext_info;
280 
281 	ext_info = &this_attr->c->ext_info[this_attr->address];
282 
283 	return ext_info->read(indio_dev, this_attr->c, buf);
284 }
285 
iio_write_channel_ext_info(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)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_get_drvdata(dev);
292 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
293 	const struct iio_chan_spec_ext_info *ext_info;
294 
295 	ext_info = &this_attr->c->ext_info[this_attr->address];
296 
297 	return ext_info->write(indio_dev, this_attr->c, buf, len);
298 }
299 
iio_read_channel_info(struct device * dev,struct device_attribute * attr,char * buf)300 static ssize_t iio_read_channel_info(struct device *dev,
301 				     struct device_attribute *attr,
302 				     char *buf)
303 {
304 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
305 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
306 	int val, val2;
307 	int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
308 					    &val, &val2, this_attr->address);
309 
310 	if (ret < 0)
311 		return ret;
312 
313 	if (ret == IIO_VAL_INT)
314 		return sprintf(buf, "%d\n", val);
315 	else if (ret == IIO_VAL_INT_PLUS_MICRO) {
316 		if (val2 < 0)
317 			return sprintf(buf, "-%d.%06u\n", val, -val2);
318 		else
319 			return sprintf(buf, "%d.%06u\n", val, val2);
320 	} else if (ret == IIO_VAL_INT_PLUS_NANO) {
321 		if (val2 < 0)
322 			return sprintf(buf, "-%d.%09u\n", val, -val2);
323 		else
324 			return sprintf(buf, "%d.%09u\n", val, val2);
325 	} else
326 		return 0;
327 }
328 
iio_write_channel_info(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)329 static ssize_t iio_write_channel_info(struct device *dev,
330 				      struct device_attribute *attr,
331 				      const char *buf,
332 				      size_t len)
333 {
334 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
335 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
336 	int ret, integer = 0, fract = 0, fract_mult = 100000;
337 	bool integer_part = true, negative = false;
338 
339 	/* Assumes decimal - precision based on number of digits */
340 	if (!indio_dev->info->write_raw)
341 		return -EINVAL;
342 
343 	if (indio_dev->info->write_raw_get_fmt)
344 		switch (indio_dev->info->write_raw_get_fmt(indio_dev,
345 			this_attr->c, this_attr->address)) {
346 		case IIO_VAL_INT_PLUS_MICRO:
347 			fract_mult = 100000;
348 			break;
349 		case IIO_VAL_INT_PLUS_NANO:
350 			fract_mult = 100000000;
351 			break;
352 		default:
353 			return -EINVAL;
354 		}
355 
356 	if (buf[0] == '-') {
357 		negative = true;
358 		buf++;
359 	}
360 
361 	while (*buf) {
362 		if ('0' <= *buf && *buf <= '9') {
363 			if (integer_part)
364 				integer = integer*10 + *buf - '0';
365 			else {
366 				fract += fract_mult*(*buf - '0');
367 				if (fract_mult == 1)
368 					break;
369 				fract_mult /= 10;
370 			}
371 		} else if (*buf == '\n') {
372 			if (*(buf + 1) == '\0')
373 				break;
374 			else
375 				return -EINVAL;
376 		} else if (*buf == '.') {
377 			integer_part = false;
378 		} else {
379 			return -EINVAL;
380 		}
381 		buf++;
382 	}
383 	if (negative) {
384 		if (integer)
385 			integer = -integer;
386 		else
387 			fract = -fract;
388 	}
389 
390 	ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
391 					 integer, fract, this_attr->address);
392 	if (ret)
393 		return ret;
394 
395 	return len;
396 }
397 
398 static
__iio_device_attr_init(struct device_attribute * dev_attr,const char * postfix,struct iio_chan_spec const * chan,ssize_t (* readfunc)(struct device * dev,struct device_attribute * attr,char * buf),ssize_t (* writefunc)(struct device * dev,struct device_attribute * attr,const char * buf,size_t len),bool generic)399 int __iio_device_attr_init(struct device_attribute *dev_attr,
400 			   const char *postfix,
401 			   struct iio_chan_spec const *chan,
402 			   ssize_t (*readfunc)(struct device *dev,
403 					       struct device_attribute *attr,
404 					       char *buf),
405 			   ssize_t (*writefunc)(struct device *dev,
406 						struct device_attribute *attr,
407 						const char *buf,
408 						size_t len),
409 			   bool generic)
410 {
411 	int ret;
412 	char *name_format, *full_postfix;
413 	sysfs_attr_init(&dev_attr->attr);
414 
415 	/* Build up postfix of <extend_name>_<modifier>_postfix */
416 	if (chan->modified && !generic) {
417 		if (chan->extend_name)
418 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
419 						 iio_modifier_names[chan
420 								    ->channel2],
421 						 chan->extend_name,
422 						 postfix);
423 		else
424 			full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
425 						 iio_modifier_names[chan
426 								    ->channel2],
427 						 postfix);
428 	} else {
429 		if (chan->extend_name == NULL)
430 			full_postfix = kstrdup(postfix, GFP_KERNEL);
431 		else
432 			full_postfix = kasprintf(GFP_KERNEL,
433 						 "%s_%s",
434 						 chan->extend_name,
435 						 postfix);
436 	}
437 	if (full_postfix == NULL) {
438 		ret = -ENOMEM;
439 		goto error_ret;
440 	}
441 
442 	if (chan->differential) { /* Differential can not have modifier */
443 		if (generic)
444 			name_format
445 				= kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
446 					    iio_direction[chan->output],
447 					    iio_chan_type_name_spec[chan->type],
448 					    iio_chan_type_name_spec[chan->type],
449 					    full_postfix);
450 		else if (chan->indexed)
451 			name_format
452 				= kasprintf(GFP_KERNEL, "%s_%s%d-%s%d_%s",
453 					    iio_direction[chan->output],
454 					    iio_chan_type_name_spec[chan->type],
455 					    chan->channel,
456 					    iio_chan_type_name_spec[chan->type],
457 					    chan->channel2,
458 					    full_postfix);
459 		else {
460 			WARN_ON("Differential channels must be indexed\n");
461 			ret = -EINVAL;
462 			goto error_free_full_postfix;
463 		}
464 	} else { /* Single ended */
465 		if (generic)
466 			name_format
467 				= kasprintf(GFP_KERNEL, "%s_%s_%s",
468 					    iio_direction[chan->output],
469 					    iio_chan_type_name_spec[chan->type],
470 					    full_postfix);
471 		else if (chan->indexed)
472 			name_format
473 				= kasprintf(GFP_KERNEL, "%s_%s%d_%s",
474 					    iio_direction[chan->output],
475 					    iio_chan_type_name_spec[chan->type],
476 					    chan->channel,
477 					    full_postfix);
478 		else
479 			name_format
480 				= kasprintf(GFP_KERNEL, "%s_%s_%s",
481 					    iio_direction[chan->output],
482 					    iio_chan_type_name_spec[chan->type],
483 					    full_postfix);
484 	}
485 	if (name_format == NULL) {
486 		ret = -ENOMEM;
487 		goto error_free_full_postfix;
488 	}
489 	dev_attr->attr.name = kasprintf(GFP_KERNEL,
490 					name_format,
491 					chan->channel,
492 					chan->channel2);
493 	if (dev_attr->attr.name == NULL) {
494 		ret = -ENOMEM;
495 		goto error_free_name_format;
496 	}
497 
498 	if (readfunc) {
499 		dev_attr->attr.mode |= S_IRUGO;
500 		dev_attr->show = readfunc;
501 	}
502 
503 	if (writefunc) {
504 		dev_attr->attr.mode |= S_IWUSR;
505 		dev_attr->store = writefunc;
506 	}
507 	kfree(name_format);
508 	kfree(full_postfix);
509 
510 	return 0;
511 
512 error_free_name_format:
513 	kfree(name_format);
514 error_free_full_postfix:
515 	kfree(full_postfix);
516 error_ret:
517 	return ret;
518 }
519 
__iio_device_attr_deinit(struct device_attribute * dev_attr)520 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
521 {
522 	kfree(dev_attr->attr.name);
523 }
524 
__iio_add_chan_devattr(const char * postfix,struct iio_chan_spec const * chan,ssize_t (* readfunc)(struct device * dev,struct device_attribute * attr,char * buf),ssize_t (* writefunc)(struct device * dev,struct device_attribute * attr,const char * buf,size_t len),u64 mask,bool generic,struct device * dev,struct list_head * attr_list)525 int __iio_add_chan_devattr(const char *postfix,
526 			   struct iio_chan_spec const *chan,
527 			   ssize_t (*readfunc)(struct device *dev,
528 					       struct device_attribute *attr,
529 					       char *buf),
530 			   ssize_t (*writefunc)(struct device *dev,
531 						struct device_attribute *attr,
532 						const char *buf,
533 						size_t len),
534 			   u64 mask,
535 			   bool generic,
536 			   struct device *dev,
537 			   struct list_head *attr_list)
538 {
539 	int ret;
540 	struct iio_dev_attr *iio_attr, *t;
541 
542 	iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
543 	if (iio_attr == NULL) {
544 		ret = -ENOMEM;
545 		goto error_ret;
546 	}
547 	ret = __iio_device_attr_init(&iio_attr->dev_attr,
548 				     postfix, chan,
549 				     readfunc, writefunc, generic);
550 	if (ret)
551 		goto error_iio_dev_attr_free;
552 	iio_attr->c = chan;
553 	iio_attr->address = mask;
554 	list_for_each_entry(t, attr_list, l)
555 		if (strcmp(t->dev_attr.attr.name,
556 			   iio_attr->dev_attr.attr.name) == 0) {
557 			if (!generic)
558 				dev_err(dev, "tried to double register : %s\n",
559 					t->dev_attr.attr.name);
560 			ret = -EBUSY;
561 			goto error_device_attr_deinit;
562 		}
563 	list_add(&iio_attr->l, attr_list);
564 
565 	return 0;
566 
567 error_device_attr_deinit:
568 	__iio_device_attr_deinit(&iio_attr->dev_attr);
569 error_iio_dev_attr_free:
570 	kfree(iio_attr);
571 error_ret:
572 	return ret;
573 }
574 
iio_device_add_channel_sysfs(struct iio_dev * indio_dev,struct iio_chan_spec const * chan)575 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
576 					struct iio_chan_spec const *chan)
577 {
578 	int ret, i, attrcount = 0;
579 	const struct iio_chan_spec_ext_info *ext_info;
580 
581 	if (chan->channel < 0)
582 		return 0;
583 
584 	ret = __iio_add_chan_devattr(iio_data_type_name[chan->processed_val],
585 				     chan,
586 				     &iio_read_channel_info,
587 				     (chan->output ?
588 				      &iio_write_channel_info : NULL),
589 				     0,
590 				     0,
591 				     &indio_dev->dev,
592 				     &indio_dev->channel_attr_list);
593 	if (ret)
594 		goto error_ret;
595 	attrcount++;
596 
597 	for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
598 		ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
599 					     chan,
600 					     &iio_read_channel_info,
601 					     &iio_write_channel_info,
602 					     i/2,
603 					     !(i%2),
604 					     &indio_dev->dev,
605 					     &indio_dev->channel_attr_list);
606 		if (ret == -EBUSY && (i%2 == 0)) {
607 			ret = 0;
608 			continue;
609 		}
610 		if (ret < 0)
611 			goto error_ret;
612 		attrcount++;
613 	}
614 
615 	if (chan->ext_info) {
616 		unsigned int i = 0;
617 		for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
618 			ret = __iio_add_chan_devattr(ext_info->name,
619 					chan,
620 					ext_info->read ?
621 					    &iio_read_channel_ext_info : NULL,
622 					ext_info->write ?
623 					    &iio_write_channel_ext_info : NULL,
624 					i,
625 					ext_info->shared,
626 					&indio_dev->dev,
627 					&indio_dev->channel_attr_list);
628 			i++;
629 			if (ret == -EBUSY && ext_info->shared)
630 				continue;
631 
632 			if (ret)
633 				goto error_ret;
634 
635 			attrcount++;
636 		}
637 	}
638 
639 	ret = attrcount;
640 error_ret:
641 	return ret;
642 }
643 
iio_device_remove_and_free_read_attr(struct iio_dev * indio_dev,struct iio_dev_attr * p)644 static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
645 						 struct iio_dev_attr *p)
646 {
647 	kfree(p->dev_attr.attr.name);
648 	kfree(p);
649 }
650 
iio_show_dev_name(struct device * dev,struct device_attribute * attr,char * buf)651 static ssize_t iio_show_dev_name(struct device *dev,
652 				 struct device_attribute *attr,
653 				 char *buf)
654 {
655 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
656 	return sprintf(buf, "%s\n", indio_dev->name);
657 }
658 
659 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
660 
iio_device_register_sysfs(struct iio_dev * indio_dev)661 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
662 {
663 	int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
664 	struct iio_dev_attr *p, *n;
665 	struct attribute **attr;
666 
667 	/* First count elements in any existing group */
668 	if (indio_dev->info->attrs) {
669 		attr = indio_dev->info->attrs->attrs;
670 		while (*attr++ != NULL)
671 			attrcount_orig++;
672 	}
673 	attrcount = attrcount_orig;
674 	/*
675 	 * New channel registration method - relies on the fact a group does
676 	 * not need to be initialized if it is name is NULL.
677 	 */
678 	INIT_LIST_HEAD(&indio_dev->channel_attr_list);
679 	if (indio_dev->channels)
680 		for (i = 0; i < indio_dev->num_channels; i++) {
681 			ret = iio_device_add_channel_sysfs(indio_dev,
682 							   &indio_dev
683 							   ->channels[i]);
684 			if (ret < 0)
685 				goto error_clear_attrs;
686 			attrcount += ret;
687 		}
688 
689 	if (indio_dev->name)
690 		attrcount++;
691 
692 	indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
693 						   sizeof(indio_dev->chan_attr_group.attrs[0]),
694 						   GFP_KERNEL);
695 	if (indio_dev->chan_attr_group.attrs == NULL) {
696 		ret = -ENOMEM;
697 		goto error_clear_attrs;
698 	}
699 	/* Copy across original attributes */
700 	if (indio_dev->info->attrs)
701 		memcpy(indio_dev->chan_attr_group.attrs,
702 		       indio_dev->info->attrs->attrs,
703 		       sizeof(indio_dev->chan_attr_group.attrs[0])
704 		       *attrcount_orig);
705 	attrn = attrcount_orig;
706 	/* Add all elements from the list. */
707 	list_for_each_entry(p, &indio_dev->channel_attr_list, l)
708 		indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
709 	if (indio_dev->name)
710 		indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
711 
712 	indio_dev->groups[indio_dev->groupcounter++] =
713 		&indio_dev->chan_attr_group;
714 
715 	return 0;
716 
717 error_clear_attrs:
718 	list_for_each_entry_safe(p, n,
719 				 &indio_dev->channel_attr_list, l) {
720 		list_del(&p->l);
721 		iio_device_remove_and_free_read_attr(indio_dev, p);
722 	}
723 
724 	return ret;
725 }
726 
iio_device_unregister_sysfs(struct iio_dev * indio_dev)727 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
728 {
729 
730 	struct iio_dev_attr *p, *n;
731 
732 	list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
733 		list_del(&p->l);
734 		iio_device_remove_and_free_read_attr(indio_dev, p);
735 	}
736 	kfree(indio_dev->chan_attr_group.attrs);
737 }
738 
iio_dev_release(struct device * device)739 static void iio_dev_release(struct device *device)
740 {
741 	struct iio_dev *indio_dev = container_of(device, struct iio_dev, dev);
742 	cdev_del(&indio_dev->chrdev);
743 	if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
744 		iio_device_unregister_trigger_consumer(indio_dev);
745 	iio_device_unregister_eventset(indio_dev);
746 	iio_device_unregister_sysfs(indio_dev);
747 	iio_device_unregister_debugfs(indio_dev);
748 }
749 
750 static struct device_type iio_dev_type = {
751 	.name = "iio_device",
752 	.release = iio_dev_release,
753 };
754 
iio_allocate_device(int sizeof_priv)755 struct iio_dev *iio_allocate_device(int sizeof_priv)
756 {
757 	struct iio_dev *dev;
758 	size_t alloc_size;
759 
760 	alloc_size = sizeof(struct iio_dev);
761 	if (sizeof_priv) {
762 		alloc_size = ALIGN(alloc_size, IIO_ALIGN);
763 		alloc_size += sizeof_priv;
764 	}
765 	/* ensure 32-byte alignment of whole construct ? */
766 	alloc_size += IIO_ALIGN - 1;
767 
768 	dev = kzalloc(alloc_size, GFP_KERNEL);
769 
770 	if (dev) {
771 		dev->dev.groups = dev->groups;
772 		dev->dev.type = &iio_dev_type;
773 		dev->dev.bus = &iio_bus_type;
774 		device_initialize(&dev->dev);
775 		dev_set_drvdata(&dev->dev, (void *)dev);
776 		mutex_init(&dev->mlock);
777 		mutex_init(&dev->info_exist_lock);
778 
779 		dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
780 		if (dev->id < 0) {
781 			/* cannot use a dev_err as the name isn't available */
782 			printk(KERN_ERR "Failed to get id\n");
783 			kfree(dev);
784 			return NULL;
785 		}
786 		dev_set_name(&dev->dev, "iio:device%d", dev->id);
787 	}
788 
789 	return dev;
790 }
791 EXPORT_SYMBOL(iio_allocate_device);
792 
iio_free_device(struct iio_dev * dev)793 void iio_free_device(struct iio_dev *dev)
794 {
795 	if (dev) {
796 		ida_simple_remove(&iio_ida, dev->id);
797 		kfree(dev);
798 	}
799 }
800 EXPORT_SYMBOL(iio_free_device);
801 
802 /**
803  * iio_chrdev_open() - chrdev file open for buffer access and ioctls
804  **/
iio_chrdev_open(struct inode * inode,struct file * filp)805 static int iio_chrdev_open(struct inode *inode, struct file *filp)
806 {
807 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
808 						struct iio_dev, chrdev);
809 
810 	if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
811 		return -EBUSY;
812 
813 	filp->private_data = indio_dev;
814 
815 	return 0;
816 }
817 
818 /**
819  * iio_chrdev_release() - chrdev file close buffer access and ioctls
820  **/
iio_chrdev_release(struct inode * inode,struct file * filp)821 static int iio_chrdev_release(struct inode *inode, struct file *filp)
822 {
823 	struct iio_dev *indio_dev = container_of(inode->i_cdev,
824 						struct iio_dev, chrdev);
825 	clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
826 	return 0;
827 }
828 
829 /* Somewhat of a cross file organization violation - ioctls here are actually
830  * event related */
iio_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)831 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
832 {
833 	struct iio_dev *indio_dev = filp->private_data;
834 	int __user *ip = (int __user *)arg;
835 	int fd;
836 
837 	if (cmd == IIO_GET_EVENT_FD_IOCTL) {
838 		fd = iio_event_getfd(indio_dev);
839 		if (copy_to_user(ip, &fd, sizeof(fd)))
840 			return -EFAULT;
841 		return 0;
842 	}
843 	return -EINVAL;
844 }
845 
846 static const struct file_operations iio_buffer_fileops = {
847 	.read = iio_buffer_read_first_n_outer_addr,
848 	.release = iio_chrdev_release,
849 	.open = iio_chrdev_open,
850 	.poll = iio_buffer_poll_addr,
851 	.owner = THIS_MODULE,
852 	.llseek = noop_llseek,
853 	.unlocked_ioctl = iio_ioctl,
854 	.compat_ioctl = iio_ioctl,
855 };
856 
857 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
858 
iio_device_register(struct iio_dev * indio_dev)859 int iio_device_register(struct iio_dev *indio_dev)
860 {
861 	int ret;
862 
863 	/* configure elements for the chrdev */
864 	indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
865 
866 	ret = iio_device_register_debugfs(indio_dev);
867 	if (ret) {
868 		dev_err(indio_dev->dev.parent,
869 			"Failed to register debugfs interfaces\n");
870 		goto error_ret;
871 	}
872 	ret = iio_device_register_sysfs(indio_dev);
873 	if (ret) {
874 		dev_err(indio_dev->dev.parent,
875 			"Failed to register sysfs interfaces\n");
876 		goto error_unreg_debugfs;
877 	}
878 	ret = iio_device_register_eventset(indio_dev);
879 	if (ret) {
880 		dev_err(indio_dev->dev.parent,
881 			"Failed to register event set\n");
882 		goto error_free_sysfs;
883 	}
884 	if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
885 		iio_device_register_trigger_consumer(indio_dev);
886 
887 	if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
888 		indio_dev->setup_ops == NULL)
889 		indio_dev->setup_ops = &noop_ring_setup_ops;
890 
891 	ret = device_add(&indio_dev->dev);
892 	if (ret < 0)
893 		goto error_unreg_eventset;
894 	cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
895 	indio_dev->chrdev.owner = indio_dev->info->driver_module;
896 	ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
897 	if (ret < 0)
898 		goto error_del_device;
899 	return 0;
900 
901 error_del_device:
902 	device_del(&indio_dev->dev);
903 error_unreg_eventset:
904 	iio_device_unregister_eventset(indio_dev);
905 error_free_sysfs:
906 	iio_device_unregister_sysfs(indio_dev);
907 error_unreg_debugfs:
908 	iio_device_unregister_debugfs(indio_dev);
909 error_ret:
910 	return ret;
911 }
912 EXPORT_SYMBOL(iio_device_register);
913 
iio_device_unregister(struct iio_dev * indio_dev)914 void iio_device_unregister(struct iio_dev *indio_dev)
915 {
916 	mutex_lock(&indio_dev->info_exist_lock);
917 	indio_dev->info = NULL;
918 	mutex_unlock(&indio_dev->info_exist_lock);
919 	device_unregister(&indio_dev->dev);
920 }
921 EXPORT_SYMBOL(iio_device_unregister);
922 subsys_initcall(iio_init);
923 module_exit(iio_exit);
924 
925 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
926 MODULE_DESCRIPTION("Industrial I/O core");
927 MODULE_LICENSE("GPL");
928