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 int gb_control_timesync_enable(struct gb_control *control, u8 count,
202 u64 frame_time, u32 strobe_delay, u32 refclk)
204 struct gb_control_timesync_enable_request request;
206 request.count = count;
207 request.frame_time = cpu_to_le64(frame_time);
208 request.strobe_delay = cpu_to_le32(strobe_delay);
209 request.refclk = cpu_to_le32(refclk);
210 return gb_operation_sync(control->connection,
211 GB_CONTROL_TYPE_TIMESYNC_ENABLE, &request,
212 sizeof(request), NULL, 0);
215 int gb_control_timesync_disable(struct gb_control *control)
217 return gb_operation_sync(control->connection,
218 GB_CONTROL_TYPE_TIMESYNC_DISABLE, NULL, 0,
222 int gb_control_timesync_get_last_event(struct gb_control *control,
225 struct gb_control_timesync_get_last_event_response response;
228 ret = gb_operation_sync(control->connection,
229 GB_CONTROL_TYPE_TIMESYNC_GET_LAST_EVENT,
230 NULL, 0, &response, sizeof(response));
232 *frame_time = le64_to_cpu(response.frame_time);
236 int gb_control_timesync_authoritative(struct gb_control *control,
239 struct gb_control_timesync_authoritative_request request;
242 for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
243 request.frame_time[i] = cpu_to_le64(frame_time[i]);
245 return gb_operation_sync(control->connection,
246 GB_CONTROL_TYPE_TIMESYNC_AUTHORITATIVE,
247 &request, sizeof(request),
251 static int gb_control_bundle_pm_status_map(u8 status)
254 case GB_CONTROL_BUNDLE_PM_INVAL:
256 case GB_CONTROL_BUNDLE_PM_BUSY:
258 case GB_CONTROL_BUNDLE_PM_NA:
260 case GB_CONTROL_BUNDLE_PM_FAIL:
266 int gb_control_bundle_suspend(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_SUSPEND, &request,
275 sizeof(request), &response, sizeof(response));
277 dev_err(&control->dev, "failed to send bundle %u suspend: %d\n",
282 if (response.status != GB_CONTROL_BUNDLE_PM_OK) {
283 dev_err(&control->dev, "failed to suspend bundle %u: %d\n",
284 bundle_id, response.status);
285 return gb_control_bundle_pm_status_map(response.status);
291 int gb_control_bundle_resume(struct gb_control *control, u8 bundle_id)
293 struct gb_control_bundle_pm_request request;
294 struct gb_control_bundle_pm_response response;
297 request.bundle_id = bundle_id;
298 ret = gb_operation_sync(control->connection,
299 GB_CONTROL_TYPE_BUNDLE_RESUME, &request,
300 sizeof(request), &response, sizeof(response));
302 dev_err(&control->dev, "failed to send bundle %u resume: %d\n",
307 if (response.status != GB_CONTROL_BUNDLE_PM_OK) {
308 dev_err(&control->dev, "failed to resume bundle %u: %d\n",
309 bundle_id, response.status);
310 return gb_control_bundle_pm_status_map(response.status);
316 int gb_control_bundle_deactivate(struct gb_control *control, u8 bundle_id)
318 struct gb_control_bundle_pm_request request;
319 struct gb_control_bundle_pm_response response;
322 request.bundle_id = bundle_id;
323 ret = gb_operation_sync(control->connection,
324 GB_CONTROL_TYPE_BUNDLE_DEACTIVATE, &request,
325 sizeof(request), &response, sizeof(response));
327 dev_err(&control->dev,
328 "failed to send bundle %u deactivate: %d\n", bundle_id,
333 if (response.status != GB_CONTROL_BUNDLE_PM_OK) {
334 dev_err(&control->dev, "failed to deactivate bundle %u: %d\n",
335 bundle_id, response.status);
336 return gb_control_bundle_pm_status_map(response.status);
342 int gb_control_bundle_activate(struct gb_control *control, u8 bundle_id)
344 struct gb_control_bundle_pm_request request;
345 struct gb_control_bundle_pm_response response;
348 if (!control->has_bundle_activate)
351 request.bundle_id = bundle_id;
352 ret = gb_operation_sync(control->connection,
353 GB_CONTROL_TYPE_BUNDLE_ACTIVATE, &request,
354 sizeof(request), &response, sizeof(response));
356 dev_err(&control->dev,
357 "failed to send bundle %u activate: %d\n", bundle_id,
362 if (response.status != GB_CONTROL_BUNDLE_PM_OK) {
363 dev_err(&control->dev, "failed to activate bundle %u: %d\n",
364 bundle_id, response.status);
365 return gb_control_bundle_pm_status_map(response.status);
371 static int gb_control_interface_pm_status_map(u8 status)
374 case GB_CONTROL_INTF_PM_BUSY:
376 case GB_CONTROL_INTF_PM_NA:
383 int gb_control_interface_suspend_prepare(struct gb_control *control)
385 struct gb_control_intf_pm_response response;
388 ret = gb_operation_sync(control->connection,
389 GB_CONTROL_TYPE_INTF_SUSPEND_PREPARE, NULL, 0,
390 &response, sizeof(response));
392 dev_err(&control->dev,
393 "failed to send interface suspend prepare: %d\n", ret);
397 if (response.status != GB_CONTROL_INTF_PM_OK) {
398 dev_err(&control->dev, "interface error while preparing suspend: %d\n",
400 return gb_control_interface_pm_status_map(response.status);
406 int gb_control_interface_deactivate_prepare(struct gb_control *control)
408 struct gb_control_intf_pm_response response;
411 ret = gb_operation_sync(control->connection,
412 GB_CONTROL_TYPE_INTF_DEACTIVATE_PREPARE, NULL,
413 0, &response, sizeof(response));
415 dev_err(&control->dev, "failed to send interface deactivate prepare: %d\n",
420 if (response.status != GB_CONTROL_INTF_PM_OK) {
421 dev_err(&control->dev, "interface error while preparing deactivate: %d\n",
423 return gb_control_interface_pm_status_map(response.status);
429 int gb_control_interface_hibernate_abort(struct gb_control *control)
431 struct gb_control_intf_pm_response response;
434 ret = gb_operation_sync(control->connection,
435 GB_CONTROL_TYPE_INTF_HIBERNATE_ABORT, NULL, 0,
436 &response, sizeof(response));
438 dev_err(&control->dev,
439 "failed to send interface aborting hibernate: %d\n",
444 if (response.status != GB_CONTROL_INTF_PM_OK) {
445 dev_err(&control->dev, "interface error while aborting hibernate: %d\n",
447 return gb_control_interface_pm_status_map(response.status);
453 static ssize_t vendor_string_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
456 struct gb_control *control = to_gb_control(dev);
458 return scnprintf(buf, PAGE_SIZE, "%s\n", control->vendor_string);
460 static DEVICE_ATTR_RO(vendor_string);
462 static ssize_t product_string_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
465 struct gb_control *control = to_gb_control(dev);
467 return scnprintf(buf, PAGE_SIZE, "%s\n", control->product_string);
469 static DEVICE_ATTR_RO(product_string);
471 static struct attribute *control_attrs[] = {
472 &dev_attr_vendor_string.attr,
473 &dev_attr_product_string.attr,
476 ATTRIBUTE_GROUPS(control);
478 static void gb_control_release(struct device *dev)
480 struct gb_control *control = to_gb_control(dev);
482 gb_connection_destroy(control->connection);
484 kfree(control->vendor_string);
485 kfree(control->product_string);
490 struct device_type greybus_control_type = {
491 .name = "greybus_control",
492 .release = gb_control_release,
495 struct gb_control *gb_control_create(struct gb_interface *intf)
497 struct gb_connection *connection;
498 struct gb_control *control;
500 control = kzalloc(sizeof(*control), GFP_KERNEL);
502 return ERR_PTR(-ENOMEM);
504 control->intf = intf;
506 connection = gb_connection_create_control(intf);
507 if (IS_ERR(connection)) {
509 "failed to create control connection: %ld\n",
510 PTR_ERR(connection));
512 return ERR_CAST(connection);
515 control->connection = connection;
517 control->dev.parent = &intf->dev;
518 control->dev.bus = &greybus_bus_type;
519 control->dev.type = &greybus_control_type;
520 control->dev.groups = control_groups;
521 control->dev.dma_mask = intf->dev.dma_mask;
522 device_initialize(&control->dev);
523 dev_set_name(&control->dev, "%s.ctrl", dev_name(&intf->dev));
525 gb_connection_set_data(control->connection, control);
530 int gb_control_enable(struct gb_control *control)
534 dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
536 ret = gb_connection_enable_tx(control->connection);
538 dev_err(&control->connection->intf->dev,
539 "failed to enable control connection: %d\n",
544 ret = gb_control_get_version(control);
546 goto err_disable_connection;
548 if (control->protocol_major > 0 || control->protocol_minor > 1)
549 control->has_bundle_version = true;
551 /* FIXME: use protocol version instead */
552 if (!(control->intf->quirks & GB_INTERFACE_QUIRK_NO_BUNDLE_ACTIVATE))
553 control->has_bundle_activate = true;
557 err_disable_connection:
558 gb_connection_disable(control->connection);
563 void gb_control_disable(struct gb_control *control)
565 dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
567 if (control->intf->disconnected)
568 gb_connection_disable_forced(control->connection);
570 gb_connection_disable(control->connection);
573 int gb_control_suspend(struct gb_control *control)
575 gb_connection_disable(control->connection);
580 int gb_control_resume(struct gb_control *control)
584 ret = gb_connection_enable_tx(control->connection);
586 dev_err(&control->connection->intf->dev,
587 "failed to enable control connection: %d\n", ret);
594 int gb_control_add(struct gb_control *control)
598 ret = device_add(&control->dev);
600 dev_err(&control->dev,
601 "failed to register control device: %d\n",
609 void gb_control_del(struct gb_control *control)
611 if (device_is_registered(&control->dev))
612 device_del(&control->dev);
615 struct gb_control *gb_control_get(struct gb_control *control)
617 get_device(&control->dev);
622 void gb_control_put(struct gb_control *control)
624 put_device(&control->dev);
627 void gb_control_mode_switch_prepare(struct gb_control *control)
629 gb_connection_mode_switch_prepare(control->connection);
632 void gb_control_mode_switch_complete(struct gb_control *control)
634 gb_connection_mode_switch_complete(control->connection);