xref: /freebsd/sys/compat/linuxkpi/common/include/linux/device.h (revision 13464e4a44fc58490a03bb8bfc7e3c972e9c30b2)
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 int
363 device_register(struct device *dev)
364 {
365 	device_t bsddev = NULL;
366 	int unit = -1;
367 
368 	if (dev->bsddev != NULL)
369 		goto done;
370 
371 	if (dev->devt) {
372 		unit = MINOR(dev->devt);
373 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
374 		dev->bsddev_attached_here = false;
375 	} else if (dev->parent == NULL) {
376 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
377 		dev->bsddev_attached_here = false;
378 	} else {
379 		dev->bsddev_attached_here = true;
380 	}
381 	if (bsddev == NULL && dev->parent != NULL) {
382 		bsddev = device_add_child(dev->parent->bsddev,
383 		    dev->class->kobj.name, unit);
384 	}
385 	if (bsddev != NULL) {
386 		if (dev->devt == 0)
387 			dev->devt = makedev(0, device_get_unit(bsddev));
388 		device_set_softc(bsddev, dev);
389 	}
390 	dev->bsddev = bsddev;
391 done:
392 	kobject_init(&dev->kobj, &linux_dev_ktype);
393 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
394 
395 	return (0);
396 }
397 
398 static inline void
399 device_unregister(struct device *dev)
400 {
401 	device_t bsddev;
402 
403 	bsddev = dev->bsddev;
404 	dev->bsddev = NULL;
405 
406 	if (bsddev != NULL && dev->bsddev_attached_here) {
407 		mtx_lock(&Giant);
408 		device_delete_child(device_get_parent(bsddev), bsddev);
409 		mtx_unlock(&Giant);
410 	}
411 	put_device(dev);
412 }
413 
414 static inline void
415 device_del(struct device *dev)
416 {
417 	device_t bsddev;
418 
419 	bsddev = dev->bsddev;
420 	dev->bsddev = NULL;
421 
422 	if (bsddev != NULL && dev->bsddev_attached_here) {
423 		mtx_lock(&Giant);
424 		device_delete_child(device_get_parent(bsddev), bsddev);
425 		mtx_unlock(&Giant);
426 	}
427 }
428 
429 struct device *device_create(struct class *class, struct device *parent,
430 	    dev_t devt, void *drvdata, const char *fmt, ...);
431 
432 static inline void
433 device_destroy(struct class *class, dev_t devt)
434 {
435 	device_t bsddev;
436 	int unit;
437 
438 	unit = MINOR(devt);
439 	bsddev = devclass_get_device(class->bsdclass, unit);
440 	if (bsddev != NULL)
441 		device_unregister(device_get_softc(bsddev));
442 }
443 
444 static inline void
445 linux_class_kfree(struct class *class)
446 {
447 
448 	kfree(class);
449 }
450 
451 static inline struct class *
452 class_create(struct module *owner, const char *name)
453 {
454 	struct class *class;
455 	int error;
456 
457 	class = kzalloc(sizeof(*class), M_WAITOK);
458 	class->owner = owner;
459 	class->name = name;
460 	class->class_release = linux_class_kfree;
461 	error = class_register(class);
462 	if (error) {
463 		kfree(class);
464 		return (NULL);
465 	}
466 
467 	return (class);
468 }
469 
470 static inline void
471 class_destroy(struct class *class)
472 {
473 
474 	if (class == NULL)
475 		return;
476 	class_unregister(class);
477 }
478 
479 static inline int
480 device_create_file(struct device *dev, const struct device_attribute *attr)
481 {
482 
483 	if (dev)
484 		return sysfs_create_file(&dev->kobj, &attr->attr);
485 	return -EINVAL;
486 }
487 
488 static inline void
489 device_remove_file(struct device *dev, const struct device_attribute *attr)
490 {
491 
492 	if (dev)
493 		sysfs_remove_file(&dev->kobj, &attr->attr);
494 }
495 
496 static inline int
497 class_create_file(struct class *class, const struct class_attribute *attr)
498 {
499 
500 	if (class)
501 		return sysfs_create_file(&class->kobj, &attr->attr);
502 	return -EINVAL;
503 }
504 
505 static inline void
506 class_remove_file(struct class *class, const struct class_attribute *attr)
507 {
508 
509 	if (class)
510 		sysfs_remove_file(&class->kobj, &attr->attr);
511 }
512 
513 static inline int
514 dev_to_node(struct device *dev)
515 {
516                 return -1;
517 }
518 
519 char *kvasprintf(gfp_t, const char *, va_list);
520 char *kasprintf(gfp_t, const char *, ...);
521 
522 #endif	/* _LINUX_DEVICE_H_ */
523