1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2016 Google Inc.
6 * Copyright 2016 Linaro Ltd.
9 #include <linux/greybus.h>
10 #include "greybus_trace.h"
12 static ssize_t
eject_store(struct device
*dev
,
13 struct device_attribute
*attr
,
14 const char *buf
, size_t len
)
16 struct gb_module
*module
= to_gb_module(dev
);
17 struct gb_interface
*intf
;
22 ret
= kstrtol(buf
, 0, &val
);
29 for (i
= 0; i
< module
->num_interfaces
; ++i
) {
30 intf
= module
->interfaces
[i
];
32 mutex_lock(&intf
->mutex
);
33 /* Set flag to prevent concurrent activation. */
35 gb_interface_disable(intf
);
36 gb_interface_deactivate(intf
);
37 mutex_unlock(&intf
->mutex
);
40 /* Tell the SVC to eject the primary interface. */
41 ret
= gb_svc_intf_eject(module
->hd
->svc
, module
->module_id
);
47 static DEVICE_ATTR_WO(eject
);
49 static ssize_t
module_id_show(struct device
*dev
,
50 struct device_attribute
*attr
, char *buf
)
52 struct gb_module
*module
= to_gb_module(dev
);
54 return sprintf(buf
, "%u\n", module
->module_id
);
56 static DEVICE_ATTR_RO(module_id
);
58 static ssize_t
num_interfaces_show(struct device
*dev
,
59 struct device_attribute
*attr
, char *buf
)
61 struct gb_module
*module
= to_gb_module(dev
);
63 return sprintf(buf
, "%zu\n", module
->num_interfaces
);
65 static DEVICE_ATTR_RO(num_interfaces
);
67 static struct attribute
*module_attrs
[] = {
69 &dev_attr_module_id
.attr
,
70 &dev_attr_num_interfaces
.attr
,
73 ATTRIBUTE_GROUPS(module
);
75 static void gb_module_release(struct device
*dev
)
77 struct gb_module
*module
= to_gb_module(dev
);
79 trace_gb_module_release(module
);
84 struct device_type greybus_module_type
= {
85 .name
= "greybus_module",
86 .release
= gb_module_release
,
89 struct gb_module
*gb_module_create(struct gb_host_device
*hd
, u8 module_id
,
90 size_t num_interfaces
)
92 struct gb_interface
*intf
;
93 struct gb_module
*module
;
96 module
= kzalloc(struct_size(module
, interfaces
, num_interfaces
),
102 module
->module_id
= module_id
;
103 module
->num_interfaces
= num_interfaces
;
105 module
->dev
.parent
= &hd
->dev
;
106 module
->dev
.bus
= &greybus_bus_type
;
107 module
->dev
.type
= &greybus_module_type
;
108 module
->dev
.groups
= module_groups
;
109 module
->dev
.dma_mask
= hd
->dev
.dma_mask
;
110 device_initialize(&module
->dev
);
111 dev_set_name(&module
->dev
, "%d-%u", hd
->bus_id
, module_id
);
113 trace_gb_module_create(module
);
115 for (i
= 0; i
< num_interfaces
; ++i
) {
116 intf
= gb_interface_create(module
, module_id
+ i
);
118 dev_err(&module
->dev
, "failed to create interface %u\n",
120 goto err_put_interfaces
;
122 module
->interfaces
[i
] = intf
;
128 for (--i
; i
>= 0; --i
)
129 gb_interface_put(module
->interfaces
[i
]);
131 put_device(&module
->dev
);
137 * Register and enable an interface after first attempting to activate it.
139 static void gb_module_register_interface(struct gb_interface
*intf
)
141 struct gb_module
*module
= intf
->module
;
142 u8 intf_id
= intf
->interface_id
;
145 mutex_lock(&intf
->mutex
);
147 ret
= gb_interface_activate(intf
);
149 if (intf
->type
!= GB_INTERFACE_TYPE_DUMMY
) {
150 dev_err(&module
->dev
,
151 "failed to activate interface %u: %d\n",
155 gb_interface_add(intf
);
159 ret
= gb_interface_add(intf
);
161 goto err_interface_deactivate
;
163 ret
= gb_interface_enable(intf
);
165 dev_err(&module
->dev
, "failed to enable interface %u: %d\n",
167 goto err_interface_deactivate
;
170 mutex_unlock(&intf
->mutex
);
174 err_interface_deactivate
:
175 gb_interface_deactivate(intf
);
177 mutex_unlock(&intf
->mutex
);
180 static void gb_module_deregister_interface(struct gb_interface
*intf
)
182 /* Mark as disconnected to prevent I/O during disable. */
183 if (intf
->module
->disconnected
)
184 intf
->disconnected
= true;
186 mutex_lock(&intf
->mutex
);
187 intf
->removed
= true;
188 gb_interface_disable(intf
);
189 gb_interface_deactivate(intf
);
190 mutex_unlock(&intf
->mutex
);
192 gb_interface_del(intf
);
195 /* Register a module and its interfaces. */
196 int gb_module_add(struct gb_module
*module
)
201 ret
= device_add(&module
->dev
);
203 dev_err(&module
->dev
, "failed to register module: %d\n", ret
);
207 trace_gb_module_add(module
);
209 for (i
= 0; i
< module
->num_interfaces
; ++i
)
210 gb_module_register_interface(module
->interfaces
[i
]);
215 /* Deregister a module and its interfaces. */
216 void gb_module_del(struct gb_module
*module
)
220 for (i
= 0; i
< module
->num_interfaces
; ++i
)
221 gb_module_deregister_interface(module
->interfaces
[i
]);
223 trace_gb_module_del(module
);
225 device_del(&module
->dev
);
228 void gb_module_put(struct gb_module
*module
)
232 for (i
= 0; i
< module
->num_interfaces
; ++i
)
233 gb_interface_put(module
->interfaces
[i
]);
235 put_device(&module
->dev
);