source: rtems-libbsd/rtemsbsd/powerpc/include/linux/device.h @ 28ee86a

55-freebsd-126-freebsd-12
Last change on this file since 28ee86a was 28ee86a, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/16 at 09:58:19

Import DPAA driver snapshot

Imported from Freescale Linux repository

git://git.freescale.com/ppc/upstream/linux.git

commit 2774c204cd8bfc56a200ff4dcdfc9cdf5b6fc161.

Linux compatibility layer is partly from FreeBSD.

  • Property mode set to 100644
File size: 11.6 KB
Line 
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, 2014 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/types.h>
35#include <linux/kobject.h>
36#include <linux/list.h>
37#include <linux/compiler.h>
38#include <linux/types.h>
39#include <linux/module.h>
40#include <linux/workqueue.h>
41#include <linux/sysfs.h>
42#include <linux/kdev_t.h>
43#include <linux/slab.h>
44#include <asm/atomic.h>
45
46#include <sys/bus.h>
47
48enum irqreturn  { IRQ_NONE = 0, IRQ_HANDLED, IRQ_WAKE_THREAD, };
49typedef enum irqreturn  irqreturn_t;
50
51#include <stdio.h>
52
53#include <linux/ioport.h>
54#include <linux/of.h>
55
56struct device {
57        struct device_node *of_node;
58        uintptr_t base;
59        void *driver_data;
60};
61
62static inline void *
63devm_kmalloc(struct device *dev, size_t size, gfp_t gfp)
64{
65
66        (void)dev;
67        return (kmalloc(size, gfp));
68}
69
70static inline void *
71devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
72{
73
74        (void)dev;
75        return (kzalloc(size, gfp));
76}
77
78static inline void __iomem *
79devm_ioremap(struct device *dev, resource_size_t offset, resource_size_t size)
80{
81
82        return (void __iomem *)(dev->base + (uintptr_t)offset);
83}
84
85#define devm_alloc_percpu(dev, type) \
86    devm_kzalloc(dev, sizeof(type) * rtems_get_processor_count(), GFP_KERNEL)
87
88#define dev_err(dev, fmt, ...) \
89    do { (void)dev; fprintf(stderr, fmt, ##__VA_ARGS__); } while (0)
90
91#define dev_warn(dev, fmt, ...) \
92    do { (void)dev; fprintf(stderr, fmt, ##__VA_ARGS__); } while (0)
93
94#define dev_info(dev, fmt, ...) \
95    do { (void)dev; fprintf(stderr, fmt, ##__VA_ARGS__); } while (0)
96
97static inline struct device *
98get_device(struct device *dev)
99{
100
101        return (dev);
102}
103
104static inline void
105put_device(struct device *dev)
106{
107
108        (void)dev;
109}
110
111static inline void *
112dev_get_drvdata(const struct device *dev)
113{
114
115        return (dev->driver_data);
116}
117
118static inline void
119dev_set_drvdata(struct device *dev, void *drvdata)
120{
121
122        dev->driver_data = drvdata;
123}
124
125#ifndef __rtems__
126struct class {
127        const char      *name;
128        struct module   *owner;
129        struct kobject  kobj;
130        devclass_t      bsdclass;
131        void            (*class_release)(struct class *class);
132        void            (*dev_release)(struct device *dev);
133        char *          (*devnode)(struct device *dev, umode_t *mode);
134};
135
136struct device {
137        struct device   *parent;
138        struct list_head irqents;
139        device_t        bsddev;
140        dev_t           devt;
141        struct class    *class;
142        void            (*release)(struct device *dev);
143        struct kobject  kobj;
144        uint64_t        *dma_mask;
145        void            *driver_data;
146        unsigned int    irq;
147        unsigned int    msix;
148        unsigned int    msix_max;
149};
150
151extern struct device linux_rootdev;
152extern struct kobject class_root;
153
154struct class_attribute {
155        struct attribute attr;
156        ssize_t (*show)(struct class *, struct class_attribute *, char *);
157        ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t);
158        const void *(*namespace)(struct class *, const struct class_attribute *);
159};
160
161#define CLASS_ATTR(_name, _mode, _show, _store)                         \
162        struct class_attribute class_attr_##_name =                     \
163            { { #_name, NULL, _mode }, _show, _store }
164
165struct device_attribute {
166        struct attribute        attr;
167        ssize_t                 (*show)(struct device *,
168                                        struct device_attribute *, char *);
169        ssize_t                 (*store)(struct device *,
170                                        struct device_attribute *, const char *,
171                                        size_t);
172};
173
174#define DEVICE_ATTR(_name, _mode, _show, _store)                        \
175        struct device_attribute dev_attr_##_name =                      \
176            { { #_name, NULL, _mode }, _show, _store }
177
178/* Simple class attribute that is just a static string */
179struct class_attribute_string {
180        struct class_attribute attr;
181        char *str;
182};
183
184static inline ssize_t
185show_class_attr_string(struct class *class,
186                                struct class_attribute *attr, char *buf)
187{
188        struct class_attribute_string *cs;
189        cs = container_of(attr, struct class_attribute_string, attr);
190        return snprintf(buf, PAGE_SIZE, "%s\n", cs->str);
191}
192
193/* Currently read-only only */
194#define _CLASS_ATTR_STRING(_name, _mode, _str) \
195        { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
196#define CLASS_ATTR_STRING(_name, _mode, _str) \
197        struct class_attribute_string class_attr_##_name = \
198                _CLASS_ATTR_STRING(_name, _mode, _str)
199
200#define dev_err(dev, fmt, ...)  device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
201#define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
202#define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
203#define dev_printk(lvl, dev, fmt, ...)                                  \
204            device_printf((dev)->bsddev, fmt, ##__VA_ARGS__)
205
206static inline void *
207dev_get_drvdata(struct device *dev)
208{
209
210        return dev->driver_data;
211}
212
213static inline void
214dev_set_drvdata(struct device *dev, void *data)
215{
216
217        dev->driver_data = data;
218}
219
220static inline struct device *
221get_device(struct device *dev)
222{
223
224        if (dev)
225                kobject_get(&dev->kobj);
226
227        return (dev);
228}
229
230static inline char *
231dev_name(const struct device *dev)
232{
233
234        return kobject_name(&dev->kobj);
235}
236
237#define dev_set_name(_dev, _fmt, ...)                                   \
238        kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__)
239
240static inline void
241put_device(struct device *dev)
242{
243
244        if (dev)
245                kobject_put(&dev->kobj);
246}
247
248static inline ssize_t
249class_show(struct kobject *kobj, struct attribute *attr, char *buf)
250{
251        struct class_attribute *dattr;
252        ssize_t error;
253
254        dattr = container_of(attr, struct class_attribute, attr);
255        error = -EIO;
256        if (dattr->show)
257                error = dattr->show(container_of(kobj, struct class, kobj),
258                    dattr, buf);
259        return (error);
260}
261
262static inline ssize_t
263class_store(struct kobject *kobj, struct attribute *attr, const char *buf,
264    size_t count)
265{
266        struct class_attribute *dattr;
267        ssize_t error;
268
269        dattr = container_of(attr, struct class_attribute, attr);
270        error = -EIO;
271        if (dattr->store)
272                error = dattr->store(container_of(kobj, struct class, kobj),
273                    dattr, buf, count);
274        return (error);
275}
276
277static inline void
278class_release(struct kobject *kobj)
279{
280        struct class *class;
281
282        class = container_of(kobj, struct class, kobj);
283        if (class->class_release)
284                class->class_release(class);
285}
286
287static struct sysfs_ops class_sysfs = {
288        .show  = class_show,
289        .store = class_store,
290};
291static struct kobj_type class_ktype = {
292        .release = class_release,
293        .sysfs_ops = &class_sysfs
294};
295
296static inline int
297class_register(struct class *class)
298{
299
300        class->bsdclass = devclass_create(class->name);
301        kobject_init(&class->kobj, &class_ktype);
302        kobject_set_name(&class->kobj, class->name);
303        kobject_add(&class->kobj, &class_root, class->name);
304
305        return (0);
306}
307
308static inline void
309class_unregister(struct class *class)
310{
311
312        kobject_put(&class->kobj);
313}
314
315static inline void
316device_release(struct kobject *kobj)
317{
318        struct device *dev;
319
320        dev = container_of(kobj, struct device, kobj);
321        /* This is the precedence defined by linux. */
322        if (dev->release)
323                dev->release(dev);
324        else if (dev->class && dev->class->dev_release)
325                dev->class->dev_release(dev);
326}
327
328static inline ssize_t
329dev_show(struct kobject *kobj, struct attribute *attr, char *buf)
330{
331        struct device_attribute *dattr;
332        ssize_t error;
333
334        dattr = container_of(attr, struct device_attribute, attr);
335        error = -EIO;
336        if (dattr->show)
337                error = dattr->show(container_of(kobj, struct device, kobj),
338                    dattr, buf);
339        return (error);
340}
341
342static inline ssize_t
343dev_store(struct kobject *kobj, struct attribute *attr, const char *buf,
344    size_t count)
345{
346        struct device_attribute *dattr;
347        ssize_t error;
348
349        dattr = container_of(attr, struct device_attribute, attr);
350        error = -EIO;
351        if (dattr->store)
352                error = dattr->store(container_of(kobj, struct device, kobj),
353                    dattr, buf, count);
354        return (error);
355}
356
357static struct sysfs_ops dev_sysfs = { .show  = dev_show, .store = dev_store, };
358static struct kobj_type dev_ktype = {
359        .release = device_release,
360        .sysfs_ops = &dev_sysfs
361};
362
363/*
364 * Devices are registered and created for exporting to sysfs.  create
365 * implies register and register assumes the device fields have been
366 * setup appropriately before being called.
367 */
368static inline int
369device_register(struct device *dev)
370{
371        device_t bsddev;
372        int unit;
373
374        bsddev = NULL;
375        if (dev->devt) {
376                unit = MINOR(dev->devt);
377                bsddev = devclass_get_device(dev->class->bsdclass, unit);
378        } else
379                unit = -1;
380        if (bsddev == NULL)
381                bsddev = device_add_child(dev->parent->bsddev,
382                    dev->class->kobj.name, unit);
383        if (bsddev) {
384                if (dev->devt == 0)
385                        dev->devt = makedev(0, device_get_unit(bsddev));
386                device_set_softc(bsddev, dev);
387        }
388        dev->bsddev = bsddev;
389        kobject_init(&dev->kobj, &dev_ktype);
390        kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev));
391
392        return (0);
393}
394
395static inline void
396device_unregister(struct device *dev)
397{
398        device_t bsddev;
399
400        bsddev = dev->bsddev;
401        mtx_lock(&Giant);
402        if (bsddev)
403                device_delete_child(device_get_parent(bsddev), bsddev);
404        mtx_unlock(&Giant);
405        put_device(dev);
406}
407
408struct device *device_create(struct class *class, struct device *parent,
409            dev_t devt, void *drvdata, const char *fmt, ...);
410
411static inline void
412device_destroy(struct class *class, dev_t devt)
413{
414        device_t bsddev;
415        int unit;
416
417        unit = MINOR(devt);
418        bsddev = devclass_get_device(class->bsdclass, unit);
419        if (bsddev)
420                device_unregister(device_get_softc(bsddev));
421}
422
423static inline void
424class_kfree(struct class *class)
425{
426
427        kfree(class);
428}
429
430static inline struct class *
431class_create(struct module *owner, const char *name)
432{
433        struct class *class;
434        int error;
435
436        class = kzalloc(sizeof(*class), M_WAITOK);
437        class->owner = owner;
438        class->name= name;
439        class->class_release = class_kfree;
440        error = class_register(class);
441        if (error) {
442                kfree(class);
443                return (NULL);
444        }
445
446        return (class);
447}
448
449static inline void
450class_destroy(struct class *class)
451{
452
453        if (class == NULL)
454                return;
455        class_unregister(class);
456}
457
458static inline int
459device_create_file(struct device *dev, const struct device_attribute *attr)
460{
461
462        if (dev)
463                return sysfs_create_file(&dev->kobj, &attr->attr);
464        return -EINVAL;
465}
466
467static inline void
468device_remove_file(struct device *dev, const struct device_attribute *attr)
469{
470
471        if (dev)
472                sysfs_remove_file(&dev->kobj, &attr->attr);
473}
474
475static inline int
476class_create_file(struct class *class, const struct class_attribute *attr)
477{
478
479        if (class)
480                return sysfs_create_file(&class->kobj, &attr->attr);
481        return -EINVAL;
482}
483
484static inline void
485class_remove_file(struct class *class, const struct class_attribute *attr)
486{
487
488        if (class)
489                sysfs_remove_file(&class->kobj, &attr->attr);
490}
491
492static inline int dev_to_node(struct device *dev)
493{
494                return -1;
495}
496
497char *kvasprintf(gfp_t, const char *, va_list);
498char *kasprintf(gfp_t, const char *, ...);
499#endif /* __rtems__ */
500
501#endif  /* _LINUX_DEVICE_H_ */
Note: See TracBrowser for help on using the repository browser.