xref: /freebsd/sys/compat/linuxkpi/common/include/linux/device.h (revision 97ae05e314279d9a4100575b335baaa7afb4930d)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUX_DEVICE_H_
32 #define	_LINUX_DEVICE_H_
33 
34 #include <linux/err.h>
35 #include <linux/types.h>
36 #include <linux/kobject.h>
37 #include <linux/sysfs.h>
38 #include <linux/list.h>
39 #include <linux/compiler.h>
40 #include <linux/types.h>
41 #include <linux/module.h>
42 #include <linux/workqueue.h>
43 #include <linux/sysfs.h>
44 #include <linux/kdev_t.h>
45 #include <asm/atomic.h>
46 
47 #include <sys/bus.h>
48 
49 enum irqreturn	{ IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
50 typedef enum irqreturn	irqreturn_t;
51 
52 struct device;
53 
54 struct class {
55 	const char	*name;
56 	struct module	*owner;
57 	struct kobject	kobj;
58 	devclass_t	bsdclass;
59 	void		(*class_release)(struct class *class);
60 	void		(*dev_release)(struct device *dev);
61 	char *		(*devnode)(struct device *dev, umode_t *mode);
62 };
63 
64 struct dev_pm_ops {
65 	int (*suspend)(struct device *dev);
66 	int (*suspend_late)(struct device *dev);
67 	int (*resume)(struct device *dev);
68 	int (*resume_early)(struct device *dev);
69 	int (*freeze)(struct device *dev);
70 	int (*freeze_late)(struct device *dev);
71 	int (*thaw)(struct device *dev);
72 	int (*thaw_early)(struct device *dev);
73 	int (*poweroff)(struct device *dev);
74 	int (*poweroff_late)(struct device *dev);
75 	int (*restore)(struct device *dev);
76 	int (*restore_early)(struct device *dev);
77 	int (*runtime_suspend)(struct device *dev);
78 	int (*runtime_resume)(struct device *dev);
79 	int (*runtime_idle)(struct device *dev);
80 };
81 
82 struct device_driver {
83 	const char	*name;
84 	const struct dev_pm_ops *pm;
85 };
86 
87 struct device_type {
88 	const char	*name;
89 };
90 
91 struct device {
92 	struct device	*parent;
93 	struct list_head irqents;
94 	device_t	bsddev;
95 	/*
96 	 * The following flag is used to determine if the LinuxKPI is
97 	 * responsible for detaching the BSD device or not. If the
98 	 * LinuxKPI got the BSD device using devclass_get_device(), it
99 	 * must not try to detach or delete it, because it's already
100 	 * done somewhere else.
101 	 */
102 	bool		bsddev_attached_here;
103 	struct device_driver *driver;
104 	struct device_type *type;
105 	dev_t		devt;
106 	struct class	*class;
107 	void		(*release)(struct device *dev);
108 	struct kobject	kobj;
109 	uint64_t	*dma_mask;
110 	void		*driver_data;
111 	unsigned int	irq;
112 #define	LINUX_IRQ_INVALID	65535
113 	unsigned int	msix;
114 	unsigned int	msix_max;
115 	const struct attribute_group **groups;
116 };
117 
118 extern struct device linux_root_device;
119 extern struct kobject linux_class_root;
120 extern const struct kobj_type linux_dev_ktype;
121 extern const struct kobj_type linux_class_ktype;
122 
123 struct class_attribute {
124         struct attribute attr;
125         ssize_t (*show)(struct class *, struct class_attribute *, char *);
126         ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
127         const void *(*namespace)(struct class *, const struct class_attribute *);
128 };
129 
130 #define	CLASS_ATTR(_name, _mode, _show, _store)				\
131 	struct class_attribute class_attr_##_name =			\
132 	    { { #_name, NULL, _mode }, _show, _store }
133 
134 struct device_attribute {
135 	struct attribute	attr;
136 	ssize_t			(*show)(struct device *,
137 					struct device_attribute *, char *);
138 	ssize_t			(*store)(struct device *,
139 					struct device_attribute *, const char *,
140 					size_t);
141 };
142 
143 #define	DEVICE_ATTR(_name, _mode, _show, _store)			\
144 	struct device_attribute dev_attr_##_name =			\
145 	    { { #_name, NULL, _mode }, _show, _store }
146 
147 /* Simple class attribute that is just a static string */
148 struct class_attribute_string {
149 	struct class_attribute attr;
150 	char *str;
151 };
152 
153 static inline ssize_t
154 show_class_attr_string(struct class *class,
155 				struct class_attribute *attr, char *buf)
156 {
157 	struct class_attribute_string *cs;
158 	cs = container_of(attr, struct class_attribute_string, attr);
159 	return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
160 }
161 
162 /* Currently read-only only */
163 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
164 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
165 #define CLASS_ATTR_STRING(_name, _mode, _str) \
166 	struct class_attribute_string class_attr_##_name = \
167 		_CLASS_ATTR_STRING(_name, _mode, _str)
168 
169 #define	dev_err(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
170 #define	dev_warn(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
171 #define	dev_info(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
172 #define	dev_notice(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
173 #define	dev_printk(lvl, dev, fmt, ...)					\
174 	    device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
175 
176 #define	dev_err_ratelimited(dev, ...) do {	\
177 	static linux_ratelimit_t __ratelimited;	\
178 	if (linux_ratelimited(&__ratelimited))	\
179 		dev_err(dev, __VA_ARGS__);	\
180 } while (0)
181 
182 #define	dev_warn_ratelimited(dev, ...) do {	\
183 	static linux_ratelimit_t __ratelimited;	\
184 	if (linux_ratelimited(&__ratelimited))	\
185 		dev_warn(dev, __VA_ARGS__);	\
186 } while (0)
187 
188 static inline void *
189 dev_get_drvdata(const struct device *dev)
190 {
191 
192 	return dev->driver_data;
193 }
194 
195 static inline void
196 dev_set_drvdata(struct device *dev, void *data)
197 {
198 
199 	dev->driver_data = data;
200 }
201 
202 static inline struct device *
203 get_device(struct device *dev)
204 {
205 
206 	if (dev)
207 		kobject_get(&dev->kobj);
208 
209 	return (dev);
210 }
211 
212 static inline char *
213 dev_name(const struct device *dev)
214 {
215 
216  	return kobject_name(&dev->kobj);
217 }
218 
219 #define	dev_set_name(_dev, _fmt, ...)					\
220 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
221 
222 static inline void
223 put_device(struct device *dev)
224 {
225 
226 	if (dev)
227 		kobject_put(&dev->kobj);
228 }
229 
230 static inline int
231 class_register(struct class *class)
232 {
233 
234 	class->bsdclass = devclass_create(class->name);
235 	kobject_init(&class->kobj, &linux_class_ktype);
236 	kobject_set_name(&class->kobj, class->name);
237 	kobject_add(&class->kobj, &linux_class_root, class->name);
238 
239 	return (0);
240 }
241 
242 static inline void
243 class_unregister(struct class *class)
244 {
245 
246 	kobject_put(&class->kobj);
247 }
248 
249 static inline struct device *kobj_to_dev(struct kobject *kobj)
250 {
251 	return container_of(kobj, struct device, kobj);
252 }
253 
254 /*
255  * Devices are registered and created for exporting to sysfs. Create
256  * implies register and register assumes the device fields have been
257  * setup appropriately before being called.
258  */
259 static inline void
260 device_initialize(struct device *dev)
261 {
262 	device_t bsddev = NULL;
263 	int unit = -1;
264 
265 	if (dev->devt) {
266 		unit = MINOR(dev->devt);
267 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
268 		dev->bsddev_attached_here = false;
269 	} else if (dev->parent == NULL) {
270 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
271 		dev->bsddev_attached_here = false;
272 	} else {
273 		dev->bsddev_attached_here = true;
274 	}
275 
276 	if (bsddev == NULL && dev->parent != NULL) {
277 		bsddev = device_add_child(dev->parent->bsddev,
278 		    dev->class->kobj.name, unit);
279 	}
280 
281 	if (bsddev != NULL)
282 		device_set_softc(bsddev, dev);
283 
284 	dev->bsddev = bsddev;
285 	MPASS(dev->bsddev != NULL);
286 	kobject_init(&dev->kobj, &linux_dev_ktype);
287 }
288 
289 static inline int
290 device_add(struct device *dev)
291 {
292 	if (dev->bsddev != NULL) {
293 		if (dev->devt == 0)
294 			dev->devt = makedev(0, device_get_unit(dev->bsddev));
295 	}
296 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
297 	return (0);
298 }
299 
300 static inline void
301 device_create_release(struct device *dev)
302 {
303 	kfree(dev);
304 }
305 
306 static inline struct device *
307 device_create_groups_vargs(struct class *class, struct device *parent,
308     dev_t devt, void *drvdata, const struct attribute_group **groups,
309     const char *fmt, va_list args)
310 {
311 	struct device *dev = NULL;
312 	int retval = -ENODEV;
313 
314 	if (class == NULL || IS_ERR(class))
315 		goto error;
316 
317 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
318 	if (!dev) {
319 		retval = -ENOMEM;
320 		goto error;
321 	}
322 
323 	dev->devt = devt;
324 	dev->class = class;
325 	dev->parent = parent;
326 	dev->groups = groups;
327 	dev->release = device_create_release;
328 	/* device_initialize() needs the class and parent to be set */
329 	device_initialize(dev);
330 	dev_set_drvdata(dev, drvdata);
331 
332 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
333 	if (retval)
334 		goto error;
335 
336 	retval = device_add(dev);
337 	if (retval)
338 		goto error;
339 
340 	return dev;
341 
342 error:
343 	put_device(dev);
344 	return ERR_PTR(retval);
345 }
346 
347 static inline struct device *
348 device_create_with_groups(struct class *class,
349     struct device *parent, dev_t devt, void *drvdata,
350     const struct attribute_group **groups, const char *fmt, ...)
351 {
352 	va_list vargs;
353 	struct device *dev;
354 
355 	va_start(vargs, fmt);
356 	dev = device_create_groups_vargs(class, parent, devt, drvdata,
357 	    groups, fmt, vargs);
358 	va_end(vargs);
359 	return dev;
360 }
361 
362 static inline bool
363 device_is_registered(struct device *dev)
364 {
365 
366 	return (dev->bsddev != NULL);
367 }
368 
369 static inline int
370 device_register(struct device *dev)
371 {
372 	device_t bsddev = NULL;
373 	int unit = -1;
374 
375 	if (device_is_registered(dev))
376 		goto done;
377 
378 	if (dev->devt) {
379 		unit = MINOR(dev->devt);
380 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
381 		dev->bsddev_attached_here = false;
382 	} else if (dev->parent == NULL) {
383 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
384 		dev->bsddev_attached_here = false;
385 	} else {
386 		dev->bsddev_attached_here = true;
387 	}
388 	if (bsddev == NULL && dev->parent != NULL) {
389 		bsddev = device_add_child(dev->parent->bsddev,
390 		    dev->class->kobj.name, unit);
391 	}
392 	if (bsddev != NULL) {
393 		if (dev->devt == 0)
394 			dev->devt = makedev(0, device_get_unit(bsddev));
395 		device_set_softc(bsddev, dev);
396 	}
397 	dev->bsddev = bsddev;
398 done:
399 	kobject_init(&dev->kobj, &linux_dev_ktype);
400 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
401 
402 	return (0);
403 }
404 
405 static inline void
406 device_unregister(struct device *dev)
407 {
408 	device_t bsddev;
409 
410 	bsddev = dev->bsddev;
411 	dev->bsddev = NULL;
412 
413 	if (bsddev != NULL && dev->bsddev_attached_here) {
414 		mtx_lock(&Giant);
415 		device_delete_child(device_get_parent(bsddev), bsddev);
416 		mtx_unlock(&Giant);
417 	}
418 	put_device(dev);
419 }
420 
421 static inline void
422 device_del(struct device *dev)
423 {
424 	device_t bsddev;
425 
426 	bsddev = dev->bsddev;
427 	dev->bsddev = NULL;
428 
429 	if (bsddev != NULL && dev->bsddev_attached_here) {
430 		mtx_lock(&Giant);
431 		device_delete_child(device_get_parent(bsddev), bsddev);
432 		mtx_unlock(&Giant);
433 	}
434 }
435 
436 struct device *device_create(struct class *class, struct device *parent,
437 	    dev_t devt, void *drvdata, const char *fmt, ...);
438 
439 static inline void
440 device_destroy(struct class *class, dev_t devt)
441 {
442 	device_t bsddev;
443 	int unit;
444 
445 	unit = MINOR(devt);
446 	bsddev = devclass_get_device(class->bsdclass, unit);
447 	if (bsddev != NULL)
448 		device_unregister(device_get_softc(bsddev));
449 }
450 
451 static inline void
452 linux_class_kfree(struct class *class)
453 {
454 
455 	kfree(class);
456 }
457 
458 static inline struct class *
459 class_create(struct module *owner, const char *name)
460 {
461 	struct class *class;
462 	int error;
463 
464 	class = kzalloc(sizeof(*class), M_WAITOK);
465 	class->owner = owner;
466 	class->name = name;
467 	class->class_release = linux_class_kfree;
468 	error = class_register(class);
469 	if (error) {
470 		kfree(class);
471 		return (NULL);
472 	}
473 
474 	return (class);
475 }
476 
477 static inline void
478 class_destroy(struct class *class)
479 {
480 
481 	if (class == NULL)
482 		return;
483 	class_unregister(class);
484 }
485 
486 static inline int
487 device_create_file(struct device *dev, const struct device_attribute *attr)
488 {
489 
490 	if (dev)
491 		return sysfs_create_file(&dev->kobj, &attr->attr);
492 	return -EINVAL;
493 }
494 
495 static inline void
496 device_remove_file(struct device *dev, const struct device_attribute *attr)
497 {
498 
499 	if (dev)
500 		sysfs_remove_file(&dev->kobj, &attr->attr);
501 }
502 
503 static inline int
504 class_create_file(struct class *class, const struct class_attribute *attr)
505 {
506 
507 	if (class)
508 		return sysfs_create_file(&class->kobj, &attr->attr);
509 	return -EINVAL;
510 }
511 
512 static inline void
513 class_remove_file(struct class *class, const struct class_attribute *attr)
514 {
515 
516 	if (class)
517 		sysfs_remove_file(&class->kobj, &attr->attr);
518 }
519 
520 static inline int
521 dev_to_node(struct device *dev)
522 {
523                 return -1;
524 }
525 
526 char *kvasprintf(gfp_t, const char *, va_list);
527 char *kasprintf(gfp_t, const char *, ...);
528 
529 #endif	/* _LINUX_DEVICE_H_ */
530