a5effcf09f566ca4e5a5d8f786c0e4dca41f66a4
[cascardo/linux.git] / drivers / staging / greybus / control.c
1 /*
2  * Greybus CPort control protocol.
3  *
4  * Copyright 2015 Google Inc.
5  * Copyright 2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include "greybus.h"
14
15 /* Highest control-protocol version supported */
16 #define GB_CONTROL_VERSION_MAJOR        0
17 #define GB_CONTROL_VERSION_MINOR        1
18
19
20 static int gb_control_get_version(struct gb_control *control)
21 {
22         struct gb_interface *intf = control->connection->intf;
23         struct gb_control_version_request request;
24         struct gb_control_version_response response;
25         int ret;
26
27         request.major = GB_CONTROL_VERSION_MAJOR;
28         request.minor = GB_CONTROL_VERSION_MINOR;
29
30         ret = gb_operation_sync(control->connection,
31                                 GB_CONTROL_TYPE_VERSION,
32                                 &request, sizeof(request), &response,
33                                 sizeof(response));
34         if (ret) {
35                 dev_err(&intf->dev,
36                                 "failed to get control-protocol version: %d\n",
37                                 ret);
38                 return ret;
39         }
40
41         if (response.major > request.major) {
42                 dev_err(&intf->dev,
43                                 "unsupported major control-protocol version (%u > %u)\n",
44                                 response.major, request.major);
45                 return -ENOTSUPP;
46         }
47
48         control->protocol_major = response.major;
49         control->protocol_minor = response.minor;
50
51         dev_dbg(&intf->dev, "%s - %u.%u\n", __func__, response.major,
52                         response.minor);
53
54         return 0;
55 }
56
57 static int gb_control_get_bundle_version(struct gb_control *control,
58                                                 struct gb_bundle *bundle)
59 {
60         struct gb_interface *intf = control->connection->intf;
61         struct gb_control_bundle_version_request request;
62         struct gb_control_bundle_version_response response;
63         int ret;
64
65         request.bundle_id = bundle->id;
66
67         ret = gb_operation_sync(control->connection,
68                                 GB_CONTROL_TYPE_BUNDLE_VERSION,
69                                 &request, sizeof(request),
70                                 &response, sizeof(response));
71         if (ret) {
72                 dev_err(&intf->dev,
73                                 "failed to get bundle %u class version: %d\n",
74                                 bundle->id, ret);
75                 return ret;
76         }
77
78         bundle->class_major = response.major;
79         bundle->class_minor = response.minor;
80
81         dev_dbg(&intf->dev, "%s - %u: %u.%u\n", __func__, bundle->id,
82                         response.major, response.minor);
83
84         return 0;
85 }
86
87 int gb_control_get_bundle_versions(struct gb_control *control)
88 {
89         struct gb_interface *intf = control->connection->intf;
90         struct gb_bundle *bundle;
91         int ret;
92
93         if (!control->has_bundle_version)
94                 return 0;
95
96         list_for_each_entry(bundle, &intf->bundles, links) {
97                 ret = gb_control_get_bundle_version(control, bundle);
98                 if (ret)
99                         return ret;
100         }
101
102         return 0;
103 }
104
105 /* Get Manifest's size from the interface */
106 int gb_control_get_manifest_size_operation(struct gb_interface *intf)
107 {
108         struct gb_control_get_manifest_size_response response;
109         struct gb_connection *connection = intf->control->connection;
110         int ret;
111
112         ret = gb_operation_sync(connection, GB_CONTROL_TYPE_GET_MANIFEST_SIZE,
113                                 NULL, 0, &response, sizeof(response));
114         if (ret) {
115                 dev_err(&connection->intf->dev,
116                                 "failed to get manifest size: %d\n", ret);
117                 return ret;
118         }
119
120         return le16_to_cpu(response.size);
121 }
122
123 /* Reads Manifest from the interface */
124 int gb_control_get_manifest_operation(struct gb_interface *intf, void *manifest,
125                                       size_t size)
126 {
127         struct gb_connection *connection = intf->control->connection;
128
129         return gb_operation_sync(connection, GB_CONTROL_TYPE_GET_MANIFEST,
130                                 NULL, 0, manifest, size);
131 }
132
133 int gb_control_connected_operation(struct gb_control *control, u16 cport_id)
134 {
135         struct gb_control_connected_request request;
136
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);
140 }
141
142 int gb_control_disconnected_operation(struct gb_control *control, u16 cport_id)
143 {
144         struct gb_control_disconnected_request request;
145
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);
150 }
151
152 int gb_control_mode_switch_operation(struct gb_control *control)
153 {
154         return gb_operation_unidirectional(control->connection,
155                                                 GB_CONTROL_TYPE_MODE_SWITCH,
156                                                 NULL, 0);
157 }
158
159 int gb_control_timesync_enable(struct gb_control *control, u8 count,
160                                u64 frame_time, u32 strobe_delay, u32 refclk)
161 {
162         struct gb_control_timesync_enable_request request;
163
164         request.count = count;
165         request.frame_time = cpu_to_le64(frame_time);
166         request.strobe_delay = cpu_to_le32(strobe_delay);
167         request.refclk = cpu_to_le32(refclk);
168         return gb_operation_sync(control->connection,
169                                  GB_CONTROL_TYPE_TIMESYNC_ENABLE, &request,
170                                  sizeof(request), NULL, 0);
171 }
172
173 int gb_control_timesync_disable(struct gb_control *control)
174 {
175         return gb_operation_sync(control->connection,
176                                  GB_CONTROL_TYPE_TIMESYNC_DISABLE, NULL, 0,
177                                  NULL, 0);
178 }
179
180 int gb_control_timesync_get_last_event(struct gb_control *control,
181                                        u64 *frame_time)
182 {
183         struct gb_control_timesync_get_last_event_response response;
184         int ret;
185
186         ret = gb_operation_sync(control->connection,
187                                 GB_CONTROL_TYPE_TIMESYNC_GET_LAST_EVENT,
188                                 NULL, 0, &response, sizeof(response));
189         if (!ret)
190                 *frame_time = le64_to_cpu(response.frame_time);
191         return ret;
192 }
193
194 int gb_control_timesync_authoritative(struct gb_control *control,
195                                       u64 *frame_time)
196 {
197         struct gb_control_timesync_authoritative_request request;
198         int i;
199
200         for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
201                 request.frame_time[i] = cpu_to_le64(frame_time[i]);
202
203         return gb_operation_sync(control->connection,
204                                  GB_CONTROL_TYPE_TIMESYNC_AUTHORITATIVE,
205                                  &request, sizeof(request),
206                                  NULL, 0);
207 }
208
209 static ssize_t vendor_string_show(struct device *dev,
210                         struct device_attribute *attr, char *buf)
211 {
212         struct gb_control *control = to_gb_control(dev);
213
214         return scnprintf(buf, PAGE_SIZE, "%s\n", control->vendor_string);
215 }
216 static DEVICE_ATTR_RO(vendor_string);
217
218 static ssize_t product_string_show(struct device *dev,
219                         struct device_attribute *attr, char *buf)
220 {
221         struct gb_control *control = to_gb_control(dev);
222
223         return scnprintf(buf, PAGE_SIZE, "%s\n", control->product_string);
224 }
225 static DEVICE_ATTR_RO(product_string);
226
227 static struct attribute *control_attrs[] = {
228         &dev_attr_vendor_string.attr,
229         &dev_attr_product_string.attr,
230         NULL,
231 };
232 ATTRIBUTE_GROUPS(control);
233
234 static void gb_control_release(struct device *dev)
235 {
236         struct gb_control *control = to_gb_control(dev);
237
238         gb_connection_destroy(control->connection);
239
240         kfree(control->vendor_string);
241         kfree(control->product_string);
242
243         kfree(control);
244 }
245
246 struct device_type greybus_control_type = {
247         .name =         "greybus_control",
248         .release =      gb_control_release,
249 };
250
251 struct gb_control *gb_control_create(struct gb_interface *intf)
252 {
253         struct gb_connection *connection;
254         struct gb_control *control;
255
256         control = kzalloc(sizeof(*control), GFP_KERNEL);
257         if (!control)
258                 return ERR_PTR(-ENOMEM);
259
260         control->intf = intf;
261
262         connection = gb_connection_create_control(intf);
263         if (IS_ERR(connection)) {
264                 dev_err(&intf->dev,
265                                 "failed to create control connection: %ld\n",
266                                 PTR_ERR(connection));
267                 kfree(control);
268                 return ERR_CAST(connection);
269         }
270
271         control->connection = connection;
272
273         control->dev.parent = &intf->dev;
274         control->dev.bus = &greybus_bus_type;
275         control->dev.type = &greybus_control_type;
276         control->dev.groups = control_groups;
277         control->dev.dma_mask = intf->dev.dma_mask;
278         device_initialize(&control->dev);
279         dev_set_name(&control->dev, "%s.ctrl", dev_name(&intf->dev));
280
281         gb_connection_set_data(control->connection, control);
282
283         return control;
284 }
285
286 int gb_control_enable(struct gb_control *control)
287 {
288         int ret;
289
290         dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
291
292         ret = gb_connection_enable_tx(control->connection);
293         if (ret) {
294                 dev_err(&control->connection->intf->dev,
295                                 "failed to enable control connection: %d\n",
296                                 ret);
297                 return ret;
298         }
299
300         ret = gb_control_get_version(control);
301         if (ret)
302                 goto err_disable_connection;
303
304         if (control->protocol_major > 0 || control->protocol_minor > 1)
305                 control->has_bundle_version = true;
306
307         return 0;
308
309 err_disable_connection:
310         gb_connection_disable(control->connection);
311
312         return ret;
313 }
314
315 void gb_control_disable(struct gb_control *control)
316 {
317         dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
318
319         if (control->intf->disconnected)
320                 gb_connection_disable_forced(control->connection);
321         else
322                 gb_connection_disable(control->connection);
323 }
324
325 int gb_control_add(struct gb_control *control)
326 {
327         int ret;
328
329         ret = device_add(&control->dev);
330         if (ret) {
331                 dev_err(&control->dev,
332                                 "failed to register control device: %d\n",
333                                 ret);
334                 return ret;
335         }
336
337         return 0;
338 }
339
340 void gb_control_del(struct gb_control *control)
341 {
342         if (device_is_registered(&control->dev))
343                 device_del(&control->dev);
344 }
345
346 void gb_control_put(struct gb_control *control)
347 {
348         put_device(&control->dev);
349 }