blob: 2efc271a96fa6bcaa3ec9506a927b67aa080a36e [file] [log] [blame]
Thomas Gleixner82664962019-06-01 10:08:51 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * transport_class.h - a generic container for all transport classes
4 *
5 * Copyright (c) 2005 - James Bottomley <[email protected]>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#ifndef _TRANSPORT_CLASS_H_
9#define _TRANSPORT_CLASS_H_
10
11#include <linux/device.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050012#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/attribute_container.h>
14
James Bottomleyd0a7e5742005-08-14 17:09:01 -050015struct transport_container;
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017struct transport_class {
18 struct class class;
James Bottomleyd0a7e5742005-08-14 17:09:01 -050019 int (*setup)(struct transport_container *, struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +010020 struct device *);
James Bottomleyd0a7e5742005-08-14 17:09:01 -050021 int (*configure)(struct transport_container *, struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +010022 struct device *);
James Bottomleyd0a7e5742005-08-14 17:09:01 -050023 int (*remove)(struct transport_container *, struct device *,
Tony Jonesee959b02008-02-22 00:13:36 +010024 struct device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
27#define DECLARE_TRANSPORT_CLASS(cls, nm, su, rm, cfg) \
28struct transport_class cls = { \
29 .class = { \
30 .name = nm, \
31 }, \
32 .setup = su, \
33 .remove = rm, \
34 .configure = cfg, \
35}
36
37
38struct anon_transport_class {
39 struct transport_class tclass;
40 struct attribute_container container;
41};
42
43#define DECLARE_ANON_TRANSPORT_CLASS(cls, mtch, cfg) \
44struct anon_transport_class cls = { \
45 .tclass = { \
46 .configure = cfg, \
47 }, \
48 . container = { \
49 .match = mtch, \
50 }, \
51}
52
53#define class_to_transport_class(x) \
54 container_of(x, struct transport_class, class)
55
56struct transport_container {
57 struct attribute_container ac;
David Brownella4dbd672009-06-24 10:06:31 -070058 const struct attribute_group *statistics;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61#define attribute_container_to_transport_container(x) \
62 container_of(x, struct transport_container, ac)
63
64void transport_remove_device(struct device *);
Gabriel Krisman Bertazicd7ea702020-01-06 13:58:16 -050065int transport_add_device(struct device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066void transport_setup_device(struct device *);
67void transport_configure_device(struct device *);
68void transport_destroy_device(struct device *);
69
Gabriel Krisman Bertazicd7ea702020-01-06 13:58:16 -050070static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -070071transport_register_device(struct device *dev)
72{
Yang Yingliange7774562022-11-10 18:23:07 +080073 int ret;
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 transport_setup_device(dev);
Yang Yingliange7774562022-11-10 18:23:07 +080076 ret = transport_add_device(dev);
77 if (ret)
78 transport_destroy_device(dev);
79
80 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83static inline void
84transport_unregister_device(struct device *dev)
85{
86 transport_remove_device(dev);
87 transport_destroy_device(dev);
88}
89
90static inline int transport_container_register(struct transport_container *tc)
91{
92 return attribute_container_register(&tc->ac);
93}
94
James Bottomley2f3edc692008-04-02 10:05:48 -050095static inline void transport_container_unregister(struct transport_container *tc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
James Bottomley2f3edc692008-04-02 10:05:48 -050097 if (unlikely(attribute_container_unregister(&tc->ac)))
98 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101int transport_class_register(struct transport_class *);
102int anon_transport_class_register(struct anon_transport_class *);
103void transport_class_unregister(struct transport_class *);
104void anon_transport_class_unregister(struct anon_transport_class *);
105
106
107#endif