]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/include/linux/device.h
Update lld to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / include / linux / device.h
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 class {
53         const char      *name;
54         struct module   *owner;
55         struct kobject  kobj;
56         devclass_t      bsdclass;
57         void            (*class_release)(struct class *class);
58         void            (*dev_release)(struct device *dev);
59         char *          (*devnode)(struct device *dev, umode_t *mode);
60 };
61
62 struct device {
63         struct device   *parent;
64         struct list_head irqents;
65         device_t        bsddev;
66         /*
67          * The following flag is used to determine if the LinuxKPI is
68          * responsible for detaching the BSD device or not. If the
69          * LinuxKPI got the BSD device using devclass_get_device(), it
70          * must not try to detach or delete it, because it's already
71          * done somewhere else.
72          */
73         bool            bsddev_attached_here;
74         dev_t           devt;
75         struct class    *class;
76         void            (*release)(struct device *dev);
77         struct kobject  kobj;
78         uint64_t        *dma_mask;
79         void            *driver_data;
80         unsigned int    irq;
81 #define LINUX_IRQ_INVALID       65535
82         unsigned int    msix;
83         unsigned int    msix_max;
84         const struct attribute_group **groups;
85 };
86
87 extern struct device linux_root_device;
88 extern struct kobject linux_class_root;
89 extern const struct kobj_type linux_dev_ktype;
90 extern const struct kobj_type linux_class_ktype;
91
92 struct class_attribute {
93         struct attribute attr;
94         ssize_t (*show)(struct class *, struct class_attribute *, char *);
95         ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
96         const void *(*namespace)(struct class *, const struct class_attribute *);
97 };
98
99 #define CLASS_ATTR(_name, _mode, _show, _store)                         \
100         struct class_attribute class_attr_##_name =                     \
101             { { #_name, NULL, _mode }, _show, _store }
102
103 struct device_attribute {
104         struct attribute        attr;
105         ssize_t                 (*show)(struct device *,
106                                         struct device_attribute *, char *);
107         ssize_t                 (*store)(struct device *,
108                                         struct device_attribute *, const char *,
109                                         size_t);
110 };
111
112 #define DEVICE_ATTR(_name, _mode, _show, _store)                        \
113         struct device_attribute dev_attr_##_name =                      \
114             { { #_name, NULL, _mode }, _show, _store }
115
116 /* Simple class attribute that is just a static string */
117 struct class_attribute_string {
118         struct class_attribute attr;
119         char *str;
120 };
121
122 static inline ssize_t
123 show_class_attr_string(struct class *class,
124                                 struct class_attribute *attr, char *buf)
125 {
126         struct class_attribute_string *cs;
127         cs = container_of(attr, struct class_attribute_string, attr);
128         return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
129 }
130
131 /* Currently read-only only */
132 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
133         { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
134 #define CLASS_ATTR_STRING(_name, _mode, _str) \
135         struct class_attribute_string class_attr_##_name = \
136                 _CLASS_ATTR_STRING(_name, _mode, _str)
137
138 #define dev_err(dev, fmt, ...)  device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
139 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
140 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
141 #define dev_notice(dev, fmt, ...)       device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
142 #define dev_printk(lvl, dev, fmt, ...)                                  \
143             device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
144
145 static inline void *
146 dev_get_drvdata(const struct device *dev)
147 {
148
149         return dev->driver_data;
150 }
151
152 static inline void
153 dev_set_drvdata(struct device *dev, void *data)
154 {
155
156         dev->driver_data = data;
157 }
158
159 static inline struct device *
160 get_device(struct device *dev)
161 {
162
163         if (dev)
164                 kobject_get(&dev->kobj);
165
166         return (dev);
167 }
168
169 static inline char *
170 dev_name(const struct device *dev)
171 {
172
173         return kobject_name(&dev->kobj);
174 }
175
176 #define dev_set_name(_dev, _fmt, ...)                                   \
177         kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
178
179 static inline void
180 put_device(struct device *dev)
181 {
182
183         if (dev)
184                 kobject_put(&dev->kobj);
185 }
186
187 static inline int
188 class_register(struct class *class)
189 {
190
191         class->bsdclass = devclass_create(class->name);
192         kobject_init(&class->kobj, &linux_class_ktype);
193         kobject_set_name(&class->kobj, class->name);
194         kobject_add(&class->kobj, &linux_class_root, class->name);
195
196         return (0);
197 }
198
199 static inline void
200 class_unregister(struct class *class)
201 {
202
203         kobject_put(&class->kobj);
204 }
205
206 static inline struct device *kobj_to_dev(struct kobject *kobj)
207 {
208         return container_of(kobj, struct device, kobj);
209 }
210
211 /*
212  * Devices are registered and created for exporting to sysfs. Create
213  * implies register and register assumes the device fields have been
214  * setup appropriately before being called.
215  */
216 static inline void
217 device_initialize(struct device *dev)
218 {
219         device_t bsddev = NULL;
220         int unit = -1;
221
222         if (dev->devt) {
223                 unit = MINOR(dev->devt);
224                 bsddev = devclass_get_device(dev->class->bsdclass, unit);
225                 dev->bsddev_attached_here = false;
226         } else if (dev->parent == NULL) {
227                 bsddev = devclass_get_device(dev->class->bsdclass, 0);
228                 dev->bsddev_attached_here = false;
229         } else {
230                 dev->bsddev_attached_here = true;
231         }
232
233         if (bsddev == NULL && dev->parent != NULL) {
234                 bsddev = device_add_child(dev->parent->bsddev,
235                     dev->class->kobj.name, unit);
236         }
237
238         if (bsddev != NULL)
239                 device_set_softc(bsddev, dev);
240
241         dev->bsddev = bsddev;
242         MPASS(dev->bsddev != NULL);
243         kobject_init(&dev->kobj, &linux_dev_ktype);
244 }
245
246 static inline int
247 device_add(struct device *dev)
248 {
249         if (dev->bsddev != NULL) {
250                 if (dev->devt == 0)
251                         dev->devt = makedev(0, device_get_unit(dev->bsddev));
252         }
253         kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
254         return (0);
255 }
256
257 static inline void
258 device_create_release(struct device *dev)
259 {
260         kfree(dev);
261 }
262
263 static inline struct device *
264 device_create_groups_vargs(struct class *class, struct device *parent,
265     dev_t devt, void *drvdata, const struct attribute_group **groups,
266     const char *fmt, va_list args)
267 {
268         struct device *dev = NULL;
269         int retval = -ENODEV;
270
271         if (class == NULL || IS_ERR(class))
272                 goto error;
273
274         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
275         if (!dev) {
276                 retval = -ENOMEM;
277                 goto error;
278         }
279
280         dev->devt = devt;
281         dev->class = class;
282         dev->parent = parent;
283         dev->groups = groups;
284         dev->release = device_create_release;
285         /* device_initialize() needs the class and parent to be set */
286         device_initialize(dev);
287         dev_set_drvdata(dev, drvdata);
288
289         retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
290         if (retval)
291                 goto error;
292
293         retval = device_add(dev);
294         if (retval)
295                 goto error;
296
297         return dev;
298
299 error:
300         put_device(dev);
301         return ERR_PTR(retval);
302 }
303
304 static inline struct device *
305 device_create_with_groups(struct class *class,
306     struct device *parent, dev_t devt, void *drvdata,
307     const struct attribute_group **groups, const char *fmt, ...)
308 {
309         va_list vargs;
310         struct device *dev;
311
312         va_start(vargs, fmt);
313         dev = device_create_groups_vargs(class, parent, devt, drvdata,
314             groups, fmt, vargs);
315         va_end(vargs);
316         return dev;
317 }
318
319 static inline int
320 device_register(struct device *dev)
321 {
322         device_t bsddev = NULL;
323         int unit = -1;
324
325         if (dev->bsddev != NULL)
326                 goto done;
327
328         if (dev->devt) {
329                 unit = MINOR(dev->devt);
330                 bsddev = devclass_get_device(dev->class->bsdclass, unit);
331                 dev->bsddev_attached_here = false;
332         } else if (dev->parent == NULL) {
333                 bsddev = devclass_get_device(dev->class->bsdclass, 0);
334                 dev->bsddev_attached_here = false;
335         } else {
336                 dev->bsddev_attached_here = true;
337         }
338         if (bsddev == NULL && dev->parent != NULL) {
339                 bsddev = device_add_child(dev->parent->bsddev,
340                     dev->class->kobj.name, unit);
341         }
342         if (bsddev != NULL) {
343                 if (dev->devt == 0)
344                         dev->devt = makedev(0, device_get_unit(bsddev));
345                 device_set_softc(bsddev, dev);
346         }
347         dev->bsddev = bsddev;
348 done:
349         kobject_init(&dev->kobj, &linux_dev_ktype);
350         kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
351
352         return (0);
353 }
354
355 static inline void
356 device_unregister(struct device *dev)
357 {
358         device_t bsddev;
359
360         bsddev = dev->bsddev;
361         dev->bsddev = NULL;
362
363         if (bsddev != NULL && dev->bsddev_attached_here) {
364                 mtx_lock(&Giant);
365                 device_delete_child(device_get_parent(bsddev), bsddev);
366                 mtx_unlock(&Giant);
367         }
368         put_device(dev);
369 }
370
371 static inline void
372 device_del(struct device *dev)
373 {
374         device_t bsddev;
375
376         bsddev = dev->bsddev;
377         dev->bsddev = NULL;
378
379         if (bsddev != NULL && dev->bsddev_attached_here) {
380                 mtx_lock(&Giant);
381                 device_delete_child(device_get_parent(bsddev), bsddev);
382                 mtx_unlock(&Giant);
383         }
384 }
385
386 struct device *device_create(struct class *class, struct device *parent,
387             dev_t devt, void *drvdata, const char *fmt, ...);
388
389 static inline void
390 device_destroy(struct class *class, dev_t devt)
391 {
392         device_t bsddev;
393         int unit;
394
395         unit = MINOR(devt);
396         bsddev = devclass_get_device(class->bsdclass, unit);
397         if (bsddev != NULL)
398                 device_unregister(device_get_softc(bsddev));
399 }
400
401 static inline void
402 linux_class_kfree(struct class *class)
403 {
404
405         kfree(class);
406 }
407
408 static inline struct class *
409 class_create(struct module *owner, const char *name)
410 {
411         struct class *class;
412         int error;
413
414         class = kzalloc(sizeof(*class), M_WAITOK);
415         class->owner = owner;
416         class->name= name;
417         class->class_release = linux_class_kfree;
418         error = class_register(class);
419         if (error) {
420                 kfree(class);
421                 return (NULL);
422         }
423
424         return (class);
425 }
426
427 static inline void
428 class_destroy(struct class *class)
429 {
430
431         if (class == NULL)
432                 return;
433         class_unregister(class);
434 }
435
436 static inline int
437 device_create_file(struct device *dev, const struct device_attribute *attr)
438 {
439
440         if (dev)
441                 return sysfs_create_file(&dev->kobj, &attr->attr);
442         return -EINVAL;
443 }
444
445 static inline void
446 device_remove_file(struct device *dev, const struct device_attribute *attr)
447 {
448
449         if (dev)
450                 sysfs_remove_file(&dev->kobj, &attr->attr);
451 }
452
453 static inline int
454 class_create_file(struct class *class, const struct class_attribute *attr)
455 {
456
457         if (class)
458                 return sysfs_create_file(&class->kobj, &attr->attr);
459         return -EINVAL;
460 }
461
462 static inline void
463 class_remove_file(struct class *class, const struct class_attribute *attr)
464 {
465
466         if (class)
467                 sysfs_remove_file(&class->kobj, &attr->attr);
468 }
469
470 static inline int
471 dev_to_node(struct device *dev)
472 {
473                 return -1;
474 }
475
476 char *kvasprintf(gfp_t, const char *, va_list);
477 char *kasprintf(gfp_t, const char *, ...);
478
479 #endif  /* _LINUX_DEVICE_H_ */