1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2016 Google Inc.
6 * Copyright 2016 Linaro Ltd.
10 #include "greybus_trace.h"
13 static ssize_t
eject_store(struct device
*dev
,
14 struct device_attribute
*attr
,
15 const char *buf
, size_t len
)
17 struct gb_module
*module
= to_gb_module(dev
);
18 struct gb_interface
*intf
;
23 ret
= kstrtol(buf
, 0, &val
);
30 for (i
= 0; i
< module
->num_interfaces
; ++i
) {
31 intf
= module
->interfaces
[i
];
33 mutex_lock(&intf
->mutex
);
34 /* Set flag to prevent concurrent activation. */
36 gb_interface_disable(intf
);
37 gb_interface_deactivate(intf
);
38 mutex_unlock(&intf
->mutex
);
41 /* Tell the SVC to eject the primary interface. */
42 ret
= gb_svc_intf_eject(module
->hd
->svc
, module
->module_id
);
48 static DEVICE_ATTR_WO(eject
);
50 static ssize_t
module_id_show(struct device
*dev
,
51 struct device_attribute
*attr
, char *buf
)
53 struct gb_module
*module
= to_gb_module(dev
);
55 return sprintf(buf
, "%u\n", module
->module_id
);
57 static DEVICE_ATTR_RO(module_id
);
59 static ssize_t
num_interfaces_show(struct device
*dev
,
60 struct device_attribute
*attr
, char *buf
)
62 struct gb_module
*module
= to_gb_module(dev
);
64 return sprintf(buf
, "%zu\n", module
->num_interfaces
);
66 static DEVICE_ATTR_RO(num_interfaces
);
68 static struct attribute
*module_attrs
[] = {
70 &dev_attr_module_id
.attr
,
71 &dev_attr_num_interfaces
.attr
,
74 ATTRIBUTE_GROUPS(module
);
76 static void gb_module_release(struct device
*dev
)
78 struct gb_module
*module
= to_gb_module(dev
);
80 trace_gb_module_release(module
);
85 struct device_type greybus_module_type
= {
86 .name
= "greybus_module",
87 .release
= gb_module_release
,
90 struct gb_module
*gb_module_create(struct gb_host_device
*hd
, u8 module_id
,
91 size_t num_interfaces
)
93 struct gb_interface
*intf
;
94 struct gb_module
*module
;
97 module
= kzalloc(struct_size(module
, interfaces
, num_interfaces
),
103 module
->module_id
= module_id
;
104 module
->num_interfaces
= num_interfaces
;
106 module
->dev
.parent
= &hd
->dev
;
107 module
->dev
.bus
= &greybus_bus_type
;
108 module
->dev
.type
= &greybus_module_type
;
109 module
->dev
.groups
= module_groups
;
110 module
->dev
.dma_mask
= hd
->dev
.dma_mask
;
111 device_initialize(&module
->dev
);
112 dev_set_name(&module
->dev
, "%d-%u", hd
->bus_id
, module_id
);
114 trace_gb_module_create(module
);
116 for (i
= 0; i
< num_interfaces
; ++i
) {
117 intf
= gb_interface_create(module
, module_id
+ i
);
119 dev_err(&module
->dev
, "failed to create interface %u\n",
121 goto err_put_interfaces
;
123 module
->interfaces
[i
] = intf
;
129 for (--i
; i
>= 0; --i
)
130 gb_interface_put(module
->interfaces
[i
]);
132 put_device(&module
->dev
);
138 * Register and enable an interface after first attempting to activate it.
140 static void gb_module_register_interface(struct gb_interface
*intf
)
142 struct gb_module
*module
= intf
->module
;
143 u8 intf_id
= intf
->interface_id
;
146 mutex_lock(&intf
->mutex
);
148 ret
= gb_interface_activate(intf
);
150 if (intf
->type
!= GB_INTERFACE_TYPE_DUMMY
) {
151 dev_err(&module
->dev
,
152 "failed to activate interface %u: %d\n",
156 gb_interface_add(intf
);
160 ret
= gb_interface_add(intf
);
162 goto err_interface_deactivate
;
164 ret
= gb_interface_enable(intf
);
166 dev_err(&module
->dev
, "failed to enable interface %u: %d\n",
168 goto err_interface_deactivate
;
171 mutex_unlock(&intf
->mutex
);
175 err_interface_deactivate
:
176 gb_interface_deactivate(intf
);
178 mutex_unlock(&intf
->mutex
);
181 static void gb_module_deregister_interface(struct gb_interface
*intf
)
183 /* Mark as disconnected to prevent I/O during disable. */
184 if (intf
->module
->disconnected
)
185 intf
->disconnected
= true;
187 mutex_lock(&intf
->mutex
);
188 intf
->removed
= true;
189 gb_interface_disable(intf
);
190 gb_interface_deactivate(intf
);
191 mutex_unlock(&intf
->mutex
);
193 gb_interface_del(intf
);
196 /* Register a module and its interfaces. */
197 int gb_module_add(struct gb_module
*module
)
202 ret
= device_add(&module
->dev
);
204 dev_err(&module
->dev
, "failed to register module: %d\n", ret
);
208 trace_gb_module_add(module
);
210 for (i
= 0; i
< module
->num_interfaces
; ++i
)
211 gb_module_register_interface(module
->interfaces
[i
]);
216 /* Deregister a module and its interfaces. */
217 void gb_module_del(struct gb_module
*module
)
221 for (i
= 0; i
< module
->num_interfaces
; ++i
)
222 gb_module_deregister_interface(module
->interfaces
[i
]);
224 trace_gb_module_del(module
);
226 device_del(&module
->dev
);
229 void gb_module_put(struct gb_module
*module
)
233 for (i
= 0; i
< module
->num_interfaces
; ++i
)
234 gb_interface_put(module
->interfaces
[i
]);
236 put_device(&module
->dev
);