1 // SPDX-License-Identifier: GPL-2.0
3 * Greybus CPort control protocol.
5 * Copyright 2015 Google Inc.
6 * Copyright 2015 Linaro Ltd.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/greybus.h>
14 /* Highest control-protocol version supported */
15 #define GB_CONTROL_VERSION_MAJOR 0
16 #define GB_CONTROL_VERSION_MINOR 1
18 static int gb_control_get_version(struct gb_control
*control
)
20 struct gb_interface
*intf
= control
->connection
->intf
;
21 struct gb_control_version_request request
;
22 struct gb_control_version_response response
;
25 request
.major
= GB_CONTROL_VERSION_MAJOR
;
26 request
.minor
= GB_CONTROL_VERSION_MINOR
;
28 ret
= gb_operation_sync(control
->connection
,
29 GB_CONTROL_TYPE_VERSION
,
30 &request
, sizeof(request
), &response
,
34 "failed to get control-protocol version: %d\n",
39 if (response
.major
> request
.major
) {
41 "unsupported major control-protocol version (%u > %u)\n",
42 response
.major
, request
.major
);
46 control
->protocol_major
= response
.major
;
47 control
->protocol_minor
= response
.minor
;
49 dev_dbg(&intf
->dev
, "%s - %u.%u\n", __func__
, response
.major
,
55 static int gb_control_get_bundle_version(struct gb_control
*control
,
56 struct gb_bundle
*bundle
)
58 struct gb_interface
*intf
= control
->connection
->intf
;
59 struct gb_control_bundle_version_request request
;
60 struct gb_control_bundle_version_response response
;
63 request
.bundle_id
= bundle
->id
;
65 ret
= gb_operation_sync(control
->connection
,
66 GB_CONTROL_TYPE_BUNDLE_VERSION
,
67 &request
, sizeof(request
),
68 &response
, sizeof(response
));
71 "failed to get bundle %u class version: %d\n",
76 bundle
->class_major
= response
.major
;
77 bundle
->class_minor
= response
.minor
;
79 dev_dbg(&intf
->dev
, "%s - %u: %u.%u\n", __func__
, bundle
->id
,
80 response
.major
, response
.minor
);
85 int gb_control_get_bundle_versions(struct gb_control
*control
)
87 struct gb_interface
*intf
= control
->connection
->intf
;
88 struct gb_bundle
*bundle
;
91 if (!control
->has_bundle_version
)
94 list_for_each_entry(bundle
, &intf
->bundles
, links
) {
95 ret
= gb_control_get_bundle_version(control
, bundle
);
103 /* Get Manifest's size from the interface */
104 int gb_control_get_manifest_size_operation(struct gb_interface
*intf
)
106 struct gb_control_get_manifest_size_response response
;
107 struct gb_connection
*connection
= intf
->control
->connection
;
110 ret
= gb_operation_sync(connection
, GB_CONTROL_TYPE_GET_MANIFEST_SIZE
,
111 NULL
, 0, &response
, sizeof(response
));
113 dev_err(&connection
->intf
->dev
,
114 "failed to get manifest size: %d\n", ret
);
118 return le16_to_cpu(response
.size
);
121 /* Reads Manifest from the interface */
122 int gb_control_get_manifest_operation(struct gb_interface
*intf
, void *manifest
,
125 struct gb_connection
*connection
= intf
->control
->connection
;
127 return gb_operation_sync(connection
, GB_CONTROL_TYPE_GET_MANIFEST
,
128 NULL
, 0, manifest
, size
);
131 int gb_control_connected_operation(struct gb_control
*control
, u16 cport_id
)
133 struct gb_control_connected_request request
;
135 request
.cport_id
= cpu_to_le16(cport_id
);
136 return gb_operation_sync(control
->connection
, GB_CONTROL_TYPE_CONNECTED
,
137 &request
, sizeof(request
), NULL
, 0);
140 int gb_control_disconnected_operation(struct gb_control
*control
, u16 cport_id
)
142 struct gb_control_disconnected_request request
;
144 request
.cport_id
= cpu_to_le16(cport_id
);
145 return gb_operation_sync(control
->connection
,
146 GB_CONTROL_TYPE_DISCONNECTED
, &request
,
147 sizeof(request
), NULL
, 0);
150 int gb_control_disconnecting_operation(struct gb_control
*control
,
153 struct gb_control_disconnecting_request
*request
;
154 struct gb_operation
*operation
;
157 operation
= gb_operation_create_core(control
->connection
,
158 GB_CONTROL_TYPE_DISCONNECTING
,
159 sizeof(*request
), 0, 0,
164 request
= operation
->request
->payload
;
165 request
->cport_id
= cpu_to_le16(cport_id
);
167 ret
= gb_operation_request_send_sync(operation
);
169 dev_err(&control
->dev
, "failed to send disconnecting: %d\n",
173 gb_operation_put(operation
);
178 int gb_control_mode_switch_operation(struct gb_control
*control
)
180 struct gb_operation
*operation
;
183 operation
= gb_operation_create_core(control
->connection
,
184 GB_CONTROL_TYPE_MODE_SWITCH
,
186 GB_OPERATION_FLAG_UNIDIRECTIONAL
,
191 ret
= gb_operation_request_send_sync(operation
);
193 dev_err(&control
->dev
, "failed to send mode switch: %d\n", ret
);
195 gb_operation_put(operation
);
200 static int gb_control_bundle_pm_status_map(u8 status
)
203 case GB_CONTROL_BUNDLE_PM_INVAL
:
205 case GB_CONTROL_BUNDLE_PM_BUSY
:
207 case GB_CONTROL_BUNDLE_PM_NA
:
209 case GB_CONTROL_BUNDLE_PM_FAIL
:
215 int gb_control_bundle_suspend(struct gb_control
*control
, u8 bundle_id
)
217 struct gb_control_bundle_pm_request request
;
218 struct gb_control_bundle_pm_response response
;
221 request
.bundle_id
= bundle_id
;
222 ret
= gb_operation_sync(control
->connection
,
223 GB_CONTROL_TYPE_BUNDLE_SUSPEND
, &request
,
224 sizeof(request
), &response
, sizeof(response
));
226 dev_err(&control
->dev
, "failed to send bundle %u suspend: %d\n",
231 if (response
.status
!= GB_CONTROL_BUNDLE_PM_OK
) {
232 dev_err(&control
->dev
, "failed to suspend bundle %u: %d\n",
233 bundle_id
, response
.status
);
234 return gb_control_bundle_pm_status_map(response
.status
);
240 int gb_control_bundle_resume(struct gb_control
*control
, u8 bundle_id
)
242 struct gb_control_bundle_pm_request request
;
243 struct gb_control_bundle_pm_response response
;
246 request
.bundle_id
= bundle_id
;
247 ret
= gb_operation_sync(control
->connection
,
248 GB_CONTROL_TYPE_BUNDLE_RESUME
, &request
,
249 sizeof(request
), &response
, sizeof(response
));
251 dev_err(&control
->dev
, "failed to send bundle %u resume: %d\n",
256 if (response
.status
!= GB_CONTROL_BUNDLE_PM_OK
) {
257 dev_err(&control
->dev
, "failed to resume bundle %u: %d\n",
258 bundle_id
, response
.status
);
259 return gb_control_bundle_pm_status_map(response
.status
);
265 int gb_control_bundle_deactivate(struct gb_control
*control
, u8 bundle_id
)
267 struct gb_control_bundle_pm_request request
;
268 struct gb_control_bundle_pm_response response
;
271 request
.bundle_id
= bundle_id
;
272 ret
= gb_operation_sync(control
->connection
,
273 GB_CONTROL_TYPE_BUNDLE_DEACTIVATE
, &request
,
274 sizeof(request
), &response
, sizeof(response
));
276 dev_err(&control
->dev
,
277 "failed to send bundle %u deactivate: %d\n", bundle_id
,
282 if (response
.status
!= GB_CONTROL_BUNDLE_PM_OK
) {
283 dev_err(&control
->dev
, "failed to deactivate bundle %u: %d\n",
284 bundle_id
, response
.status
);
285 return gb_control_bundle_pm_status_map(response
.status
);
291 int gb_control_bundle_activate(struct gb_control
*control
, u8 bundle_id
)
293 struct gb_control_bundle_pm_request request
;
294 struct gb_control_bundle_pm_response response
;
297 if (!control
->has_bundle_activate
)
300 request
.bundle_id
= bundle_id
;
301 ret
= gb_operation_sync(control
->connection
,
302 GB_CONTROL_TYPE_BUNDLE_ACTIVATE
, &request
,
303 sizeof(request
), &response
, sizeof(response
));
305 dev_err(&control
->dev
,
306 "failed to send bundle %u activate: %d\n", bundle_id
,
311 if (response
.status
!= GB_CONTROL_BUNDLE_PM_OK
) {
312 dev_err(&control
->dev
, "failed to activate bundle %u: %d\n",
313 bundle_id
, response
.status
);
314 return gb_control_bundle_pm_status_map(response
.status
);
320 static int gb_control_interface_pm_status_map(u8 status
)
323 case GB_CONTROL_INTF_PM_BUSY
:
325 case GB_CONTROL_INTF_PM_NA
:
332 int gb_control_interface_suspend_prepare(struct gb_control
*control
)
334 struct gb_control_intf_pm_response response
;
337 ret
= gb_operation_sync(control
->connection
,
338 GB_CONTROL_TYPE_INTF_SUSPEND_PREPARE
, NULL
, 0,
339 &response
, sizeof(response
));
341 dev_err(&control
->dev
,
342 "failed to send interface suspend prepare: %d\n", ret
);
346 if (response
.status
!= GB_CONTROL_INTF_PM_OK
) {
347 dev_err(&control
->dev
, "interface error while preparing suspend: %d\n",
349 return gb_control_interface_pm_status_map(response
.status
);
355 int gb_control_interface_deactivate_prepare(struct gb_control
*control
)
357 struct gb_control_intf_pm_response response
;
360 ret
= gb_operation_sync(control
->connection
,
361 GB_CONTROL_TYPE_INTF_DEACTIVATE_PREPARE
, NULL
,
362 0, &response
, sizeof(response
));
364 dev_err(&control
->dev
, "failed to send interface deactivate prepare: %d\n",
369 if (response
.status
!= GB_CONTROL_INTF_PM_OK
) {
370 dev_err(&control
->dev
, "interface error while preparing deactivate: %d\n",
372 return gb_control_interface_pm_status_map(response
.status
);
378 int gb_control_interface_hibernate_abort(struct gb_control
*control
)
380 struct gb_control_intf_pm_response response
;
383 ret
= gb_operation_sync(control
->connection
,
384 GB_CONTROL_TYPE_INTF_HIBERNATE_ABORT
, NULL
, 0,
385 &response
, sizeof(response
));
387 dev_err(&control
->dev
,
388 "failed to send interface aborting hibernate: %d\n",
393 if (response
.status
!= GB_CONTROL_INTF_PM_OK
) {
394 dev_err(&control
->dev
, "interface error while aborting hibernate: %d\n",
396 return gb_control_interface_pm_status_map(response
.status
);
402 static ssize_t
vendor_string_show(struct device
*dev
,
403 struct device_attribute
*attr
, char *buf
)
405 struct gb_control
*control
= to_gb_control(dev
);
407 return scnprintf(buf
, PAGE_SIZE
, "%s\n", control
->vendor_string
);
409 static DEVICE_ATTR_RO(vendor_string
);
411 static ssize_t
product_string_show(struct device
*dev
,
412 struct device_attribute
*attr
, char *buf
)
414 struct gb_control
*control
= to_gb_control(dev
);
416 return scnprintf(buf
, PAGE_SIZE
, "%s\n", control
->product_string
);
418 static DEVICE_ATTR_RO(product_string
);
420 static struct attribute
*control_attrs
[] = {
421 &dev_attr_vendor_string
.attr
,
422 &dev_attr_product_string
.attr
,
425 ATTRIBUTE_GROUPS(control
);
427 static void gb_control_release(struct device
*dev
)
429 struct gb_control
*control
= to_gb_control(dev
);
431 gb_connection_destroy(control
->connection
);
433 kfree(control
->vendor_string
);
434 kfree(control
->product_string
);
439 struct device_type greybus_control_type
= {
440 .name
= "greybus_control",
441 .release
= gb_control_release
,
444 struct gb_control
*gb_control_create(struct gb_interface
*intf
)
446 struct gb_connection
*connection
;
447 struct gb_control
*control
;
449 control
= kzalloc(sizeof(*control
), GFP_KERNEL
);
451 return ERR_PTR(-ENOMEM
);
453 control
->intf
= intf
;
455 connection
= gb_connection_create_control(intf
);
456 if (IS_ERR(connection
)) {
458 "failed to create control connection: %ld\n",
459 PTR_ERR(connection
));
461 return ERR_CAST(connection
);
464 control
->connection
= connection
;
466 control
->dev
.parent
= &intf
->dev
;
467 control
->dev
.bus
= &greybus_bus_type
;
468 control
->dev
.type
= &greybus_control_type
;
469 control
->dev
.groups
= control_groups
;
470 control
->dev
.dma_mask
= intf
->dev
.dma_mask
;
471 device_initialize(&control
->dev
);
472 dev_set_name(&control
->dev
, "%s.ctrl", dev_name(&intf
->dev
));
474 gb_connection_set_data(control
->connection
, control
);
479 int gb_control_enable(struct gb_control
*control
)
483 dev_dbg(&control
->connection
->intf
->dev
, "%s\n", __func__
);
485 ret
= gb_connection_enable_tx(control
->connection
);
487 dev_err(&control
->connection
->intf
->dev
,
488 "failed to enable control connection: %d\n",
493 ret
= gb_control_get_version(control
);
495 goto err_disable_connection
;
497 if (control
->protocol_major
> 0 || control
->protocol_minor
> 1)
498 control
->has_bundle_version
= true;
500 /* FIXME: use protocol version instead */
501 if (!(control
->intf
->quirks
& GB_INTERFACE_QUIRK_NO_BUNDLE_ACTIVATE
))
502 control
->has_bundle_activate
= true;
506 err_disable_connection
:
507 gb_connection_disable(control
->connection
);
512 void gb_control_disable(struct gb_control
*control
)
514 dev_dbg(&control
->connection
->intf
->dev
, "%s\n", __func__
);
516 if (control
->intf
->disconnected
)
517 gb_connection_disable_forced(control
->connection
);
519 gb_connection_disable(control
->connection
);
522 int gb_control_suspend(struct gb_control
*control
)
524 gb_connection_disable(control
->connection
);
529 int gb_control_resume(struct gb_control
*control
)
533 ret
= gb_connection_enable_tx(control
->connection
);
535 dev_err(&control
->connection
->intf
->dev
,
536 "failed to enable control connection: %d\n", ret
);
543 int gb_control_add(struct gb_control
*control
)
547 ret
= device_add(&control
->dev
);
549 dev_err(&control
->dev
,
550 "failed to register control device: %d\n",
558 void gb_control_del(struct gb_control
*control
)
560 if (device_is_registered(&control
->dev
))
561 device_del(&control
->dev
);
564 struct gb_control
*gb_control_get(struct gb_control
*control
)
566 get_device(&control
->dev
);
571 void gb_control_put(struct gb_control
*control
)
573 put_device(&control
->dev
);
576 void gb_control_mode_switch_prepare(struct gb_control
*control
)
578 gb_connection_mode_switch_prepare(control
->connection
);
581 void gb_control_mode_switch_complete(struct gb_control
*control
)
583 gb_connection_mode_switch_complete(control
->connection
);