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/kdev_t.h>
44 #include <linux/backlight.h>
45 #include <asm/atomic.h>
46 
47 #include <sys/bus.h>
48 #include <sys/backlight.h>
49 
50 struct device;
51 struct fwnode_handle;
52 
53 struct class {
54 	const char	*name;
55 	struct module	*owner;
56 	struct kobject	kobj;
57 	devclass_t	bsdclass;
58 	const struct dev_pm_ops *pm;
59 	const struct attribute_group **dev_groups;
60 	void		(*class_release)(struct class *class);
61 	void		(*dev_release)(struct device *dev);
62 	char *		(*devnode)(struct device *dev, umode_t *mode);
63 };
64 
65 struct dev_pm_ops {
66 	int (*prepare)(struct device *dev);
67 	int (*suspend)(struct device *dev);
68 	int (*suspend_late)(struct device *dev);
69 	int (*resume)(struct device *dev);
70 	int (*resume_early)(struct device *dev);
71 	int (*freeze)(struct device *dev);
72 	int (*freeze_late)(struct device *dev);
73 	int (*thaw)(struct device *dev);
74 	int (*thaw_early)(struct device *dev);
75 	int (*poweroff)(struct device *dev);
76 	int (*poweroff_late)(struct device *dev);
77 	int (*restore)(struct device *dev);
78 	int (*restore_early)(struct device *dev);
79 	int (*runtime_suspend)(struct device *dev);
80 	int (*runtime_resume)(struct device *dev);
81 	int (*runtime_idle)(struct device *dev);
82 };
83 
84 struct device_driver {
85 	const char	*name;
86 	const struct dev_pm_ops *pm;
87 };
88 
89 struct device_type {
90 	const char	*name;
91 };
92 
93 struct device {
94 	struct device	*parent;
95 	struct list_head irqents;
96 	device_t	bsddev;
97 	/*
98 	 * The following flag is used to determine if the LinuxKPI is
99 	 * responsible for detaching the BSD device or not. If the
100 	 * LinuxKPI got the BSD device using devclass_get_device(), it
101 	 * must not try to detach or delete it, because it's already
102 	 * done somewhere else.
103 	 */
104 	bool		bsddev_attached_here;
105 	struct device_driver *driver;
106 	struct device_type *type;
107 	dev_t		devt;
108 	struct class	*class;
109 	void		(*release)(struct device *dev);
110 	struct kobject	kobj;
111 	void		*dma_priv;
112 	void		*driver_data;
113 	unsigned int	irq;
114 #define	LINUX_IRQ_INVALID	65535
115 	unsigned int	irq_start;
116 	unsigned int	irq_end;
117 	const struct attribute_group **groups;
118 	struct fwnode_handle *fwnode;
119 	struct cdev	*backlight_dev;
120 	struct backlight_device	*bd;
121 
122 	spinlock_t	devres_lock;
123 	struct list_head devres_head;
124 };
125 
126 extern struct device linux_root_device;
127 extern struct kobject linux_class_root;
128 extern const struct kobj_type linux_dev_ktype;
129 extern const struct kobj_type linux_class_ktype;
130 
131 struct class_attribute {
132 	struct attribute attr;
133 	ssize_t (*show)(struct class *, struct class_attribute *, char *);
134 	ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
135 	const void *(*namespace)(struct class *, const struct class_attribute *);
136 };
137 
138 #define	CLASS_ATTR(_name, _mode, _show, _store)				\
139 	struct class_attribute class_attr_##_name =			\
140 	    { { #_name, NULL, _mode }, _show, _store }
141 
142 struct device_attribute {
143 	struct attribute	attr;
144 	ssize_t			(*show)(struct device *,
145 					struct device_attribute *, char *);
146 	ssize_t			(*store)(struct device *,
147 					struct device_attribute *, const char *,
148 					size_t);
149 };
150 
151 #define	DEVICE_ATTR(_name, _mode, _show, _store)			\
152 	struct device_attribute dev_attr_##_name =			\
153 	    __ATTR(_name, _mode, _show, _store)
154 #define	DEVICE_ATTR_RO(_name)						\
155 	struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
156 #define	DEVICE_ATTR_WO(_name)						\
157 	struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
158 #define	DEVICE_ATTR_RW(_name)						\
159 	struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
160 
161 /* Simple class attribute that is just a static string */
162 struct class_attribute_string {
163 	struct class_attribute attr;
164 	char *str;
165 };
166 
167 static inline ssize_t
168 show_class_attr_string(struct class *class,
169 				struct class_attribute *attr, char *buf)
170 {
171 	struct class_attribute_string *cs;
172 	cs = container_of(attr, struct class_attribute_string, attr);
173 	return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
174 }
175 
176 /* Currently read-only only */
177 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
178 	{ __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
179 #define CLASS_ATTR_STRING(_name, _mode, _str) \
180 	struct class_attribute_string class_attr_##_name = \
181 		_CLASS_ATTR_STRING(_name, _mode, _str)
182 
183 #define	dev_err(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
184 #define	dev_warn(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
185 #define	dev_info(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
186 #define	dev_notice(dev, fmt, ...)	device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
187 #define	dev_dbg(dev, fmt, ...)	do { } while (0)
188 #define	dev_printk(lvl, dev, fmt, ...)					\
189 	    device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
190 
191 #define	dev_err_once(dev, ...) do {		\
192 	static bool __dev_err_once;		\
193 	if (!__dev_err_once) {			\
194 		__dev_err_once = 1;		\
195 		dev_err(dev, __VA_ARGS__);	\
196 	}					\
197 } while (0)
198 
199 #define	dev_err_ratelimited(dev, ...) do {	\
200 	static linux_ratelimit_t __ratelimited;	\
201 	if (linux_ratelimited(&__ratelimited))	\
202 		dev_err(dev, __VA_ARGS__);	\
203 } while (0)
204 
205 #define	dev_warn_ratelimited(dev, ...) do {	\
206 	static linux_ratelimit_t __ratelimited;	\
207 	if (linux_ratelimited(&__ratelimited))	\
208 		dev_warn(dev, __VA_ARGS__);	\
209 } while (0)
210 
211 static inline void *
212 dev_get_drvdata(const struct device *dev)
213 {
214 
215 	return dev->driver_data;
216 }
217 
218 static inline void
219 dev_set_drvdata(struct device *dev, void *data)
220 {
221 
222 	dev->driver_data = data;
223 }
224 
225 static inline struct device *
226 get_device(struct device *dev)
227 {
228 
229 	if (dev)
230 		kobject_get(&dev->kobj);
231 
232 	return (dev);
233 }
234 
235 static inline char *
236 dev_name(const struct device *dev)
237 {
238 
239 	return kobject_name(&dev->kobj);
240 }
241 
242 #define	dev_set_name(_dev, _fmt, ...)					\
243 	kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
244 
245 static inline void
246 put_device(struct device *dev)
247 {
248 
249 	if (dev)
250 		kobject_put(&dev->kobj);
251 }
252 
253 static inline int
254 class_register(struct class *class)
255 {
256 
257 	class->bsdclass = devclass_create(class->name);
258 	kobject_init(&class->kobj, &linux_class_ktype);
259 	kobject_set_name(&class->kobj, class->name);
260 	kobject_add(&class->kobj, &linux_class_root, class->name);
261 
262 	return (0);
263 }
264 
265 static inline void
266 class_unregister(struct class *class)
267 {
268 
269 	kobject_put(&class->kobj);
270 }
271 
272 static inline struct device *kobj_to_dev(struct kobject *kobj)
273 {
274 	return container_of(kobj, struct device, kobj);
275 }
276 
277 /*
278  * Devices are registered and created for exporting to sysfs. Create
279  * implies register and register assumes the device fields have been
280  * setup appropriately before being called.
281  */
282 static inline void
283 device_initialize(struct device *dev)
284 {
285 	device_t bsddev = NULL;
286 	int unit = -1;
287 
288 	if (dev->devt) {
289 		unit = MINOR(dev->devt);
290 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
291 		dev->bsddev_attached_here = false;
292 	} else if (dev->parent == NULL) {
293 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
294 		dev->bsddev_attached_here = false;
295 	} else {
296 		dev->bsddev_attached_here = true;
297 	}
298 
299 	if (bsddev == NULL && dev->parent != NULL) {
300 		bsddev = device_add_child(dev->parent->bsddev,
301 		    dev->class->kobj.name, unit);
302 	}
303 
304 	if (bsddev != NULL)
305 		device_set_softc(bsddev, dev);
306 
307 	dev->bsddev = bsddev;
308 	MPASS(dev->bsddev != NULL);
309 	kobject_init(&dev->kobj, &linux_dev_ktype);
310 
311 	spin_lock_init(&dev->devres_lock);
312 	INIT_LIST_HEAD(&dev->devres_head);
313 }
314 
315 static inline int
316 device_add(struct device *dev)
317 {
318 	if (dev->bsddev != NULL) {
319 		if (dev->devt == 0)
320 			dev->devt = makedev(0, device_get_unit(dev->bsddev));
321 	}
322 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
323 
324 	if (dev->groups)
325 		return (sysfs_create_groups(&dev->kobj, dev->groups));
326 
327 	return (0);
328 }
329 
330 static inline void
331 device_create_release(struct device *dev)
332 {
333 	kfree(dev);
334 }
335 
336 static inline struct device *
337 device_create_groups_vargs(struct class *class, struct device *parent,
338     dev_t devt, void *drvdata, const struct attribute_group **groups,
339     const char *fmt, va_list args)
340 {
341 	struct device *dev = NULL;
342 	int retval = -ENODEV;
343 
344 	if (class == NULL || IS_ERR(class))
345 		goto error;
346 
347 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
348 	if (!dev) {
349 		retval = -ENOMEM;
350 		goto error;
351 	}
352 
353 	dev->devt = devt;
354 	dev->class = class;
355 	dev->parent = parent;
356 	dev->groups = groups;
357 	dev->release = device_create_release;
358 	/* device_initialize() needs the class and parent to be set */
359 	device_initialize(dev);
360 	dev_set_drvdata(dev, drvdata);
361 
362 	retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
363 	if (retval)
364 		goto error;
365 
366 	retval = device_add(dev);
367 	if (retval)
368 		goto error;
369 
370 	return dev;
371 
372 error:
373 	put_device(dev);
374 	return ERR_PTR(retval);
375 }
376 
377 static inline struct device *
378 device_create_with_groups(struct class *class,
379     struct device *parent, dev_t devt, void *drvdata,
380     const struct attribute_group **groups, const char *fmt, ...)
381 {
382 	va_list vargs;
383 	struct device *dev;
384 
385 	va_start(vargs, fmt);
386 	dev = device_create_groups_vargs(class, parent, devt, drvdata,
387 	    groups, fmt, vargs);
388 	va_end(vargs);
389 	return dev;
390 }
391 
392 static inline bool
393 device_is_registered(struct device *dev)
394 {
395 
396 	return (dev->bsddev != NULL);
397 }
398 
399 static inline int
400 device_register(struct device *dev)
401 {
402 	device_t bsddev = NULL;
403 	int unit = -1;
404 
405 	if (device_is_registered(dev))
406 		goto done;
407 
408 	if (dev->devt) {
409 		unit = MINOR(dev->devt);
410 		bsddev = devclass_get_device(dev->class->bsdclass, unit);
411 		dev->bsddev_attached_here = false;
412 	} else if (dev->parent == NULL) {
413 		bsddev = devclass_get_device(dev->class->bsdclass, 0);
414 		dev->bsddev_attached_here = false;
415 	} else {
416 		dev->bsddev_attached_here = true;
417 	}
418 	if (bsddev == NULL && dev->parent != NULL) {
419 		bsddev = device_add_child(dev->parent->bsddev,
420 		    dev->class->kobj.name, unit);
421 	}
422 	if (bsddev != NULL) {
423 		if (dev->devt == 0)
424 			dev->devt = makedev(0, device_get_unit(bsddev));
425 		device_set_softc(bsddev, dev);
426 	}
427 	dev->bsddev = bsddev;
428 done:
429 	kobject_init(&dev->kobj, &linux_dev_ktype);
430 	kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
431 
432 	sysfs_create_groups(&dev->kobj, dev->class->dev_groups);
433 
434 	return (0);
435 }
436 
437 static inline void
438 device_unregister(struct device *dev)
439 {
440 	device_t bsddev;
441 
442 	sysfs_remove_groups(&dev->kobj, dev->class->dev_groups);
443 
444 	bsddev = dev->bsddev;
445 	dev->bsddev = NULL;
446 
447 	if (bsddev != NULL && dev->bsddev_attached_here) {
448 		mtx_lock(&Giant);
449 		device_delete_child(device_get_parent(bsddev), bsddev);
450 		mtx_unlock(&Giant);
451 	}
452 	put_device(dev);
453 }
454 
455 static inline void
456 device_del(struct device *dev)
457 {
458 	device_t bsddev;
459 
460 	bsddev = dev->bsddev;
461 	dev->bsddev = NULL;
462 
463 	if (bsddev != NULL && dev->bsddev_attached_here) {
464 		mtx_lock(&Giant);
465 		device_delete_child(device_get_parent(bsddev), bsddev);
466 		mtx_unlock(&Giant);
467 	}
468 }
469 
470 struct device *device_create(struct class *class, struct device *parent,
471 	    dev_t devt, void *drvdata, const char *fmt, ...);
472 
473 static inline void
474 device_destroy(struct class *class, dev_t devt)
475 {
476 	device_t bsddev;
477 	int unit;
478 
479 	unit = MINOR(devt);
480 	bsddev = devclass_get_device(class->bsdclass, unit);
481 	if (bsddev != NULL)
482 		device_unregister(device_get_softc(bsddev));
483 }
484 
485 #define	dev_pm_set_driver_flags(dev, flags) do { \
486 } while (0)
487 
488 static inline void
489 linux_class_kfree(struct class *class)
490 {
491 
492 	kfree(class);
493 }
494 
495 static inline struct class *
496 class_create(struct module *owner, const char *name)
497 {
498 	struct class *class;
499 	int error;
500 
501 	class = kzalloc(sizeof(*class), M_WAITOK);
502 	class->owner = owner;
503 	class->name = name;
504 	class->class_release = linux_class_kfree;
505 	error = class_register(class);
506 	if (error) {
507 		kfree(class);
508 		return (NULL);
509 	}
510 
511 	return (class);
512 }
513 
514 static inline void
515 class_destroy(struct class *class)
516 {
517 
518 	if (class == NULL)
519 		return;
520 	class_unregister(class);
521 }
522 
523 static inline int
524 device_create_file(struct device *dev, const struct device_attribute *attr)
525 {
526 
527 	if (dev)
528 		return sysfs_create_file(&dev->kobj, &attr->attr);
529 	return -EINVAL;
530 }
531 
532 static inline void
533 device_remove_file(struct device *dev, const struct device_attribute *attr)
534 {
535 
536 	if (dev)
537 		sysfs_remove_file(&dev->kobj, &attr->attr);
538 }
539 
540 static inline int
541 class_create_file(struct class *class, const struct class_attribute *attr)
542 {
543 
544 	if (class)
545 		return sysfs_create_file(&class->kobj, &attr->attr);
546 	return -EINVAL;
547 }
548 
549 static inline void
550 class_remove_file(struct class *class, const struct class_attribute *attr)
551 {
552 
553 	if (class)
554 		sysfs_remove_file(&class->kobj, &attr->attr);
555 }
556 
557 static inline int
558 dev_to_node(struct device *dev)
559 {
560 	return -1;
561 }
562 
563 char *kvasprintf(gfp_t, const char *, va_list);
564 char *kasprintf(gfp_t, const char *, ...);
565 
566 #endif	/* _LINUX_DEVICE_H_ */
567