4 * Copyright 2016 Google Inc.
5 * Copyright 2016 Linaro Ltd.
7 * Released under the GPLv2 only.
11 #include "greybus_trace.h"
14 static ssize_t
eject_store(struct device
*dev
,
15 struct device_attribute
*attr
,
16 const char *buf
, size_t len
)
18 struct gb_module
*module
= to_gb_module(dev
);
19 struct gb_interface
*intf
;
24 ret
= kstrtol(buf
, 0, &val
);
31 for (i
= 0; i
< module
->num_interfaces
; ++i
) {
32 intf
= module
->interfaces
[i
];
34 mutex_lock(&intf
->mutex
);
35 /* Set flag to prevent concurrent activation. */
37 gb_interface_disable(intf
);
38 gb_interface_deactivate(intf
);
39 mutex_unlock(&intf
->mutex
);
42 /* Tell the SVC to eject the primary interface. */
43 ret
= gb_svc_intf_eject(module
->hd
->svc
, module
->module_id
);
49 static DEVICE_ATTR_WO(eject
);
51 static ssize_t
module_id_show(struct device
*dev
,
52 struct device_attribute
*attr
, char *buf
)
54 struct gb_module
*module
= to_gb_module(dev
);
56 return sprintf(buf
, "%u\n", module
->module_id
);
58 static DEVICE_ATTR_RO(module_id
);
60 static ssize_t
num_interfaces_show(struct device
*dev
,
61 struct device_attribute
*attr
, char *buf
)
63 struct gb_module
*module
= to_gb_module(dev
);
65 return sprintf(buf
, "%zu\n", module
->num_interfaces
);
67 static DEVICE_ATTR_RO(num_interfaces
);
69 static struct attribute
*module_attrs
[] = {
71 &dev_attr_module_id
.attr
,
72 &dev_attr_num_interfaces
.attr
,
75 ATTRIBUTE_GROUPS(module
);
77 static void gb_module_release(struct device
*dev
)
79 struct gb_module
*module
= to_gb_module(dev
);
81 trace_gb_module_release(module
);
86 struct device_type greybus_module_type
= {
87 .name
= "greybus_module",
88 .release
= gb_module_release
,
91 struct gb_module
*gb_module_create(struct gb_host_device
*hd
, u8 module_id
,
92 size_t num_interfaces
)
94 struct gb_interface
*intf
;
95 struct gb_module
*module
;
98 module
= kzalloc(sizeof(*module
) + num_interfaces
* sizeof(intf
),
104 module
->module_id
= module_id
;
105 module
->num_interfaces
= num_interfaces
;
107 module
->dev
.parent
= &hd
->dev
;
108 module
->dev
.bus
= &greybus_bus_type
;
109 module
->dev
.type
= &greybus_module_type
;
110 module
->dev
.groups
= module_groups
;
111 module
->dev
.dma_mask
= hd
->dev
.dma_mask
;
112 device_initialize(&module
->dev
);
113 dev_set_name(&module
->dev
, "%d-%u", hd
->bus_id
, module_id
);
115 trace_gb_module_create(module
);
117 for (i
= 0; i
< num_interfaces
; ++i
) {
118 intf
= gb_interface_create(module
, module_id
+ i
);
120 dev_err(&module
->dev
, "failed to create interface %u\n",
122 goto err_put_interfaces
;
124 module
->interfaces
[i
] = intf
;
130 for (--i
; i
>= 0; --i
)
131 gb_interface_put(module
->interfaces
[i
]);
133 put_device(&module
->dev
);
139 * Register and enable an interface after first attempting to activate it.
141 static void gb_module_register_interface(struct gb_interface
*intf
)
143 struct gb_module
*module
= intf
->module
;
144 u8 intf_id
= intf
->interface_id
;
147 mutex_lock(&intf
->mutex
);
149 ret
= gb_interface_activate(intf
);
151 if (intf
->type
!= GB_INTERFACE_TYPE_DUMMY
) {
152 dev_err(&module
->dev
,
153 "failed to activate interface %u: %d\n",
157 gb_interface_add(intf
);
161 ret
= gb_interface_add(intf
);
163 goto err_interface_deactivate
;
165 ret
= gb_interface_enable(intf
);
167 dev_err(&module
->dev
, "failed to enable interface %u: %d\n",
169 goto err_interface_deactivate
;
172 mutex_unlock(&intf
->mutex
);
176 err_interface_deactivate
:
177 gb_interface_deactivate(intf
);
179 mutex_unlock(&intf
->mutex
);
182 static void gb_module_deregister_interface(struct gb_interface
*intf
)
184 /* Mark as disconnected to prevent I/O during disable. */
185 if (intf
->module
->disconnected
)
186 intf
->disconnected
= true;
188 mutex_lock(&intf
->mutex
);
189 intf
->removed
= true;
190 gb_interface_disable(intf
);
191 gb_interface_deactivate(intf
);
192 mutex_unlock(&intf
->mutex
);
194 gb_interface_del(intf
);
197 /* Register a module and its interfaces. */
198 int gb_module_add(struct gb_module
*module
)
203 ret
= device_add(&module
->dev
);
205 dev_err(&module
->dev
, "failed to register module: %d\n", ret
);
209 trace_gb_module_add(module
);
211 for (i
= 0; i
< module
->num_interfaces
; ++i
)
212 gb_module_register_interface(module
->interfaces
[i
]);
217 /* Deregister a module and its interfaces. */
218 void gb_module_del(struct gb_module
*module
)
222 for (i
= 0; i
< module
->num_interfaces
; ++i
)
223 gb_module_deregister_interface(module
->interfaces
[i
]);
225 trace_gb_module_del(module
);
227 device_del(&module
->dev
);
230 void gb_module_put(struct gb_module
*module
)
234 for (i
= 0; i
< module
->num_interfaces
; ++i
)
235 gb_interface_put(module
->interfaces
[i
]);
237 put_device(&module
->dev
);