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;
157 request.cport_id = cpu_to_le16(cport_id);
159 return gb_operation_sync(control->connection,
160 GB_CONTROL_TYPE_DISCONNECTING, &request,
161 sizeof(request), NULL, 0);
164 int gb_control_mode_switch_operation(struct gb_control *control)
168 ret = gb_operation_unidirectional(control->connection,
169 GB_CONTROL_TYPE_MODE_SWITCH,
172 dev_err(&control->dev, "failed to send mode switch: %d\n",
180 int gb_control_timesync_enable(struct gb_control *control, u8 count,
181 u64 frame_time, u32 strobe_delay, u32 refclk)
183 struct gb_control_timesync_enable_request request;
185 request.count = count;
186 request.frame_time = cpu_to_le64(frame_time);
187 request.strobe_delay = cpu_to_le32(strobe_delay);
188 request.refclk = cpu_to_le32(refclk);
189 return gb_operation_sync(control->connection,
190 GB_CONTROL_TYPE_TIMESYNC_ENABLE, &request,
191 sizeof(request), NULL, 0);
194 int gb_control_timesync_disable(struct gb_control *control)
196 return gb_operation_sync(control->connection,
197 GB_CONTROL_TYPE_TIMESYNC_DISABLE, NULL, 0,
201 int gb_control_timesync_get_last_event(struct gb_control *control,
204 struct gb_control_timesync_get_last_event_response response;
207 ret = gb_operation_sync(control->connection,
208 GB_CONTROL_TYPE_TIMESYNC_GET_LAST_EVENT,
209 NULL, 0, &response, sizeof(response));
211 *frame_time = le64_to_cpu(response.frame_time);
215 int gb_control_timesync_authoritative(struct gb_control *control,
218 struct gb_control_timesync_authoritative_request request;
221 for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
222 request.frame_time[i] = cpu_to_le64(frame_time[i]);
224 return gb_operation_sync(control->connection,
225 GB_CONTROL_TYPE_TIMESYNC_AUTHORITATIVE,
226 &request, sizeof(request),
230 static ssize_t vendor_string_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
233 struct gb_control *control = to_gb_control(dev);
235 return scnprintf(buf, PAGE_SIZE, "%s\n", control->vendor_string);
237 static DEVICE_ATTR_RO(vendor_string);
239 static ssize_t product_string_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 struct gb_control *control = to_gb_control(dev);
244 return scnprintf(buf, PAGE_SIZE, "%s\n", control->product_string);
246 static DEVICE_ATTR_RO(product_string);
248 static struct attribute *control_attrs[] = {
249 &dev_attr_vendor_string.attr,
250 &dev_attr_product_string.attr,
253 ATTRIBUTE_GROUPS(control);
255 static void gb_control_release(struct device *dev)
257 struct gb_control *control = to_gb_control(dev);
259 gb_connection_destroy(control->connection);
261 kfree(control->vendor_string);
262 kfree(control->product_string);
267 struct device_type greybus_control_type = {
268 .name = "greybus_control",
269 .release = gb_control_release,
272 struct gb_control *gb_control_create(struct gb_interface *intf)
274 struct gb_connection *connection;
275 struct gb_control *control;
277 control = kzalloc(sizeof(*control), GFP_KERNEL);
279 return ERR_PTR(-ENOMEM);
281 control->intf = intf;
283 connection = gb_connection_create_control(intf);
284 if (IS_ERR(connection)) {
286 "failed to create control connection: %ld\n",
287 PTR_ERR(connection));
289 return ERR_CAST(connection);
292 control->connection = connection;
294 control->dev.parent = &intf->dev;
295 control->dev.bus = &greybus_bus_type;
296 control->dev.type = &greybus_control_type;
297 control->dev.groups = control_groups;
298 control->dev.dma_mask = intf->dev.dma_mask;
299 device_initialize(&control->dev);
300 dev_set_name(&control->dev, "%s.ctrl", dev_name(&intf->dev));
302 gb_connection_set_data(control->connection, control);
307 int gb_control_enable(struct gb_control *control)
311 dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
313 ret = gb_connection_enable_tx(control->connection);
315 dev_err(&control->connection->intf->dev,
316 "failed to enable control connection: %d\n",
321 ret = gb_control_get_version(control);
323 goto err_disable_connection;
325 if (control->protocol_major > 0 || control->protocol_minor > 1)
326 control->has_bundle_version = true;
330 err_disable_connection:
331 gb_connection_disable(control->connection);
336 void gb_control_disable(struct gb_control *control)
338 dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
340 if (control->intf->disconnected)
341 gb_connection_disable_forced(control->connection);
343 gb_connection_disable(control->connection);
346 int gb_control_add(struct gb_control *control)
350 ret = device_add(&control->dev);
352 dev_err(&control->dev,
353 "failed to register control device: %d\n",
361 void gb_control_del(struct gb_control *control)
363 if (device_is_registered(&control->dev))
364 device_del(&control->dev);
367 struct gb_control *gb_control_get(struct gb_control *control)
369 get_device(&control->dev);
374 void gb_control_put(struct gb_control *control)
376 put_device(&control->dev);
379 void gb_control_mode_switch_prepare(struct gb_control *control)
381 gb_connection_mode_switch_prepare(control->connection);
384 void gb_control_mode_switch_complete(struct gb_control *control)
386 gb_connection_mode_switch_complete(control->connection);