2 * Greybus CPort control protocol.
4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
15 /* Highest control-protocol version supported */
16 #define GB_CONTROL_VERSION_MAJOR 0
17 #define GB_CONTROL_VERSION_MINOR 1
20 static int gb_control_get_version(struct gb_control
*control
)
22 struct gb_interface
*intf
= control
->connection
->intf
;
23 struct gb_control_version_request request
;
24 struct gb_control_version_response response
;
27 request
.major
= GB_CONTROL_VERSION_MAJOR
;
28 request
.minor
= GB_CONTROL_VERSION_MINOR
;
30 ret
= gb_operation_sync(control
->connection
,
31 GB_CONTROL_TYPE_VERSION
,
32 &request
, sizeof(request
), &response
,
36 "failed to get control-protocol version: %d\n",
41 if (response
.major
> request
.major
) {
43 "unsupported major control-protocol version (%u > %u)\n",
44 response
.major
, request
.major
);
48 control
->protocol_major
= response
.major
;
49 control
->protocol_minor
= response
.minor
;
51 dev_dbg(&intf
->dev
, "%s - %u.%u\n", __func__
, response
.major
,
57 static int gb_control_get_bundle_version(struct gb_control
*control
,
58 struct gb_bundle
*bundle
)
60 struct gb_interface
*intf
= control
->connection
->intf
;
61 struct gb_control_bundle_version_request request
;
62 struct gb_control_bundle_version_response response
;
65 request
.bundle_id
= bundle
->id
;
67 ret
= gb_operation_sync(control
->connection
,
68 GB_CONTROL_TYPE_BUNDLE_VERSION
,
69 &request
, sizeof(request
),
70 &response
, sizeof(response
));
73 "failed to get bundle %u class version: %d\n",
78 bundle
->class_major
= response
.major
;
79 bundle
->class_minor
= response
.minor
;
81 dev_dbg(&intf
->dev
, "%s - %u: %u.%u\n", __func__
, bundle
->id
,
82 response
.major
, response
.minor
);
87 int gb_control_get_bundle_versions(struct gb_control
*control
)
89 struct gb_interface
*intf
= control
->connection
->intf
;
90 struct gb_bundle
*bundle
;
93 if (!control
->has_bundle_version
)
96 list_for_each_entry(bundle
, &intf
->bundles
, links
) {
97 ret
= gb_control_get_bundle_version(control
, bundle
);
105 /* Get Manifest's size from the interface */
106 int gb_control_get_manifest_size_operation(struct gb_interface
*intf
)
108 struct gb_control_get_manifest_size_response response
;
109 struct gb_connection
*connection
= intf
->control
->connection
;
112 ret
= gb_operation_sync(connection
, GB_CONTROL_TYPE_GET_MANIFEST_SIZE
,
113 NULL
, 0, &response
, sizeof(response
));
115 dev_err(&connection
->intf
->dev
,
116 "failed to get manifest size: %d\n", ret
);
120 return le16_to_cpu(response
.size
);
123 /* Reads Manifest from the interface */
124 int gb_control_get_manifest_operation(struct gb_interface
*intf
, void *manifest
,
127 struct gb_connection
*connection
= intf
->control
->connection
;
129 return gb_operation_sync(connection
, GB_CONTROL_TYPE_GET_MANIFEST
,
130 NULL
, 0, manifest
, size
);
133 int gb_control_connected_operation(struct gb_control
*control
, u16 cport_id
)
135 struct gb_control_connected_request request
;
137 request
.cport_id
= cpu_to_le16(cport_id
);
138 return gb_operation_sync(control
->connection
, GB_CONTROL_TYPE_CONNECTED
,
139 &request
, sizeof(request
), NULL
, 0);
142 int gb_control_disconnected_operation(struct gb_control
*control
, u16 cport_id
)
144 struct gb_control_disconnected_request request
;
146 request
.cport_id
= cpu_to_le16(cport_id
);
147 return gb_operation_sync(control
->connection
,
148 GB_CONTROL_TYPE_DISCONNECTED
, &request
,
149 sizeof(request
), NULL
, 0);
152 int gb_control_disconnecting_operation(struct gb_control
*control
,
155 struct gb_control_disconnecting_request
*request
;
156 struct gb_operation
*operation
;
159 operation
= gb_operation_create_core(control
->connection
,
160 GB_CONTROL_TYPE_DISCONNECTING
,
161 sizeof(*request
), 0, 0,
166 request
= operation
->request
->payload
;
167 request
->cport_id
= cpu_to_le16(cport_id
);
169 ret
= gb_operation_request_send_sync(operation
);
171 dev_err(&control
->dev
, "failed to send disconnecting: %d\n",
175 gb_operation_put(operation
);
180 int gb_control_mode_switch_operation(struct gb_control
*control
)
182 struct gb_operation
*operation
;
185 operation
= gb_operation_create_core(control
->connection
,
186 GB_CONTROL_TYPE_MODE_SWITCH
,
187 0, 0, GB_OPERATION_FLAG_UNIDIRECTIONAL
,
192 ret
= gb_operation_request_send_sync(operation
);
194 dev_err(&control
->dev
, "failed to send mode switch: %d\n", ret
);
196 gb_operation_put(operation
);
201 static int gb_control_bundle_pm_status_map(u8 status
)
204 case GB_CONTROL_BUNDLE_PM_INVAL
:
206 case GB_CONTROL_BUNDLE_PM_BUSY
:
208 case GB_CONTROL_BUNDLE_PM_NA
:
210 case GB_CONTROL_BUNDLE_PM_FAIL
:
216 int gb_control_bundle_suspend(struct gb_control
*control
, u8 bundle_id
)
218 struct gb_control_bundle_pm_request request
;
219 struct gb_control_bundle_pm_response response
;
222 request
.bundle_id
= bundle_id
;
223 ret
= gb_operation_sync(control
->connection
,
224 GB_CONTROL_TYPE_BUNDLE_SUSPEND
, &request
,
225 sizeof(request
), &response
, sizeof(response
));
227 dev_err(&control
->dev
, "failed to send bundle %u suspend: %d\n",
232 if (response
.status
!= GB_CONTROL_BUNDLE_PM_OK
) {
233 dev_err(&control
->dev
, "failed to suspend bundle %u: %d\n",
234 bundle_id
, response
.status
);
235 return gb_control_bundle_pm_status_map(response
.status
);
241 int gb_control_bundle_resume(struct gb_control
*control
, u8 bundle_id
)
243 struct gb_control_bundle_pm_request request
;
244 struct gb_control_bundle_pm_response response
;
247 request
.bundle_id
= bundle_id
;
248 ret
= gb_operation_sync(control
->connection
,
249 GB_CONTROL_TYPE_BUNDLE_RESUME
, &request
,
250 sizeof(request
), &response
, sizeof(response
));
252 dev_err(&control
->dev
, "failed to send bundle %u resume: %d\n",
257 if (response
.status
!= GB_CONTROL_BUNDLE_PM_OK
) {
258 dev_err(&control
->dev
, "failed to resume bundle %u: %d\n",
259 bundle_id
, response
.status
);
260 return gb_control_bundle_pm_status_map(response
.status
);
266 int gb_control_bundle_deactivate(struct gb_control
*control
, u8 bundle_id
)
268 struct gb_control_bundle_pm_request request
;
269 struct gb_control_bundle_pm_response response
;
272 request
.bundle_id
= bundle_id
;
273 ret
= gb_operation_sync(control
->connection
,
274 GB_CONTROL_TYPE_BUNDLE_DEACTIVATE
, &request
,
275 sizeof(request
), &response
, sizeof(response
));
277 dev_err(&control
->dev
,
278 "failed to send bundle %u deactivate: %d\n", bundle_id
,
283 if (response
.status
!= GB_CONTROL_BUNDLE_PM_OK
) {
284 dev_err(&control
->dev
, "failed to deactivate bundle %u: %d\n",
285 bundle_id
, response
.status
);
286 return gb_control_bundle_pm_status_map(response
.status
);
292 int gb_control_bundle_activate(struct gb_control
*control
, u8 bundle_id
)
294 struct gb_control_bundle_pm_request request
;
295 struct gb_control_bundle_pm_response response
;
298 if (!control
->has_bundle_activate
)
301 request
.bundle_id
= bundle_id
;
302 ret
= gb_operation_sync(control
->connection
,
303 GB_CONTROL_TYPE_BUNDLE_ACTIVATE
, &request
,
304 sizeof(request
), &response
, sizeof(response
));
306 dev_err(&control
->dev
,
307 "failed to send bundle %u activate: %d\n", bundle_id
,
312 if (response
.status
!= GB_CONTROL_BUNDLE_PM_OK
) {
313 dev_err(&control
->dev
, "failed to activate bundle %u: %d\n",
314 bundle_id
, response
.status
);
315 return gb_control_bundle_pm_status_map(response
.status
);
321 static int gb_control_interface_pm_status_map(u8 status
)
324 case GB_CONTROL_INTF_PM_BUSY
:
326 case GB_CONTROL_INTF_PM_NA
:
333 int gb_control_interface_suspend_prepare(struct gb_control
*control
)
335 struct gb_control_intf_pm_response response
;
338 ret
= gb_operation_sync(control
->connection
,
339 GB_CONTROL_TYPE_INTF_SUSPEND_PREPARE
, NULL
, 0,
340 &response
, sizeof(response
));
342 dev_err(&control
->dev
,
343 "failed to send interface suspend prepare: %d\n", ret
);
347 if (response
.status
!= GB_CONTROL_INTF_PM_OK
) {
348 dev_err(&control
->dev
, "interface error while preparing suspend: %d\n",
350 return gb_control_interface_pm_status_map(response
.status
);
356 int gb_control_interface_deactivate_prepare(struct gb_control
*control
)
358 struct gb_control_intf_pm_response response
;
361 ret
= gb_operation_sync(control
->connection
,
362 GB_CONTROL_TYPE_INTF_DEACTIVATE_PREPARE
, NULL
,
363 0, &response
, sizeof(response
));
365 dev_err(&control
->dev
, "failed to send interface deactivate prepare: %d\n",
370 if (response
.status
!= GB_CONTROL_INTF_PM_OK
) {
371 dev_err(&control
->dev
, "interface error while preparing deactivate: %d\n",
373 return gb_control_interface_pm_status_map(response
.status
);
379 int gb_control_interface_hibernate_abort(struct gb_control
*control
)
381 struct gb_control_intf_pm_response response
;
384 ret
= gb_operation_sync(control
->connection
,
385 GB_CONTROL_TYPE_INTF_HIBERNATE_ABORT
, NULL
, 0,
386 &response
, sizeof(response
));
388 dev_err(&control
->dev
,
389 "failed to send interface aborting hibernate: %d\n",
394 if (response
.status
!= GB_CONTROL_INTF_PM_OK
) {
395 dev_err(&control
->dev
, "interface error while aborting hibernate: %d\n",
397 return gb_control_interface_pm_status_map(response
.status
);
403 static ssize_t
vendor_string_show(struct device
*dev
,
404 struct device_attribute
*attr
, char *buf
)
406 struct gb_control
*control
= to_gb_control(dev
);
408 return scnprintf(buf
, PAGE_SIZE
, "%s\n", control
->vendor_string
);
410 static DEVICE_ATTR_RO(vendor_string
);
412 static ssize_t
product_string_show(struct device
*dev
,
413 struct device_attribute
*attr
, char *buf
)
415 struct gb_control
*control
= to_gb_control(dev
);
417 return scnprintf(buf
, PAGE_SIZE
, "%s\n", control
->product_string
);
419 static DEVICE_ATTR_RO(product_string
);
421 static struct attribute
*control_attrs
[] = {
422 &dev_attr_vendor_string
.attr
,
423 &dev_attr_product_string
.attr
,
426 ATTRIBUTE_GROUPS(control
);
428 static void gb_control_release(struct device
*dev
)
430 struct gb_control
*control
= to_gb_control(dev
);
432 gb_connection_destroy(control
->connection
);
434 kfree(control
->vendor_string
);
435 kfree(control
->product_string
);
440 struct device_type greybus_control_type
= {
441 .name
= "greybus_control",
442 .release
= gb_control_release
,
445 struct gb_control
*gb_control_create(struct gb_interface
*intf
)
447 struct gb_connection
*connection
;
448 struct gb_control
*control
;
450 control
= kzalloc(sizeof(*control
), GFP_KERNEL
);
452 return ERR_PTR(-ENOMEM
);
454 control
->intf
= intf
;
456 connection
= gb_connection_create_control(intf
);
457 if (IS_ERR(connection
)) {
459 "failed to create control connection: %ld\n",
460 PTR_ERR(connection
));
462 return ERR_CAST(connection
);
465 control
->connection
= connection
;
467 control
->dev
.parent
= &intf
->dev
;
468 control
->dev
.bus
= &greybus_bus_type
;
469 control
->dev
.type
= &greybus_control_type
;
470 control
->dev
.groups
= control_groups
;
471 control
->dev
.dma_mask
= intf
->dev
.dma_mask
;
472 device_initialize(&control
->dev
);
473 dev_set_name(&control
->dev
, "%s.ctrl", dev_name(&intf
->dev
));
475 gb_connection_set_data(control
->connection
, control
);
480 int gb_control_enable(struct gb_control
*control
)
484 dev_dbg(&control
->connection
->intf
->dev
, "%s\n", __func__
);
486 ret
= gb_connection_enable_tx(control
->connection
);
488 dev_err(&control
->connection
->intf
->dev
,
489 "failed to enable control connection: %d\n",
494 ret
= gb_control_get_version(control
);
496 goto err_disable_connection
;
498 if (control
->protocol_major
> 0 || control
->protocol_minor
> 1)
499 control
->has_bundle_version
= true;
501 /* FIXME: use protocol version instead */
502 if (!(control
->intf
->quirks
& GB_INTERFACE_QUIRK_NO_BUNDLE_ACTIVATE
))
503 control
->has_bundle_activate
= true;
507 err_disable_connection
:
508 gb_connection_disable(control
->connection
);
513 void gb_control_disable(struct gb_control
*control
)
515 dev_dbg(&control
->connection
->intf
->dev
, "%s\n", __func__
);
517 if (control
->intf
->disconnected
)
518 gb_connection_disable_forced(control
->connection
);
520 gb_connection_disable(control
->connection
);
523 int gb_control_suspend(struct gb_control
*control
)
525 gb_connection_disable(control
->connection
);
530 int gb_control_resume(struct gb_control
*control
)
534 ret
= gb_connection_enable_tx(control
->connection
);
536 dev_err(&control
->connection
->intf
->dev
,
537 "failed to enable control connection: %d\n", ret
);
544 int gb_control_add(struct gb_control
*control
)
548 ret
= device_add(&control
->dev
);
550 dev_err(&control
->dev
,
551 "failed to register control device: %d\n",
559 void gb_control_del(struct gb_control
*control
)
561 if (device_is_registered(&control
->dev
))
562 device_del(&control
->dev
);
565 struct gb_control
*gb_control_get(struct gb_control
*control
)
567 get_device(&control
->dev
);
572 void gb_control_put(struct gb_control
*control
)
574 put_device(&control
->dev
);
577 void gb_control_mode_switch_prepare(struct gb_control
*control
)
579 gb_connection_mode_switch_prepare(control
->connection
);
582 void gb_control_mode_switch_complete(struct gb_control
*control
)
584 gb_connection_mode_switch_complete(control
->connection
);