3e02bbb5abfe89a2c8a78f562268f83c199d9ca7
[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_disconnecting_operation(struct gb_control *control,
153                                         u16 cport_id)
154 {
155         struct gb_control_disconnecting_request request;
156
157         request.cport_id = cpu_to_le16(cport_id);
158
159         return gb_operation_sync(control->connection,
160                                  GB_CONTROL_TYPE_DISCONNECTING, &request,
161                                  sizeof(request), NULL, 0);
162 }
163
164 int gb_control_mode_switch_operation(struct gb_control *control)
165 {
166         int ret;
167
168         ret = gb_operation_unidirectional(control->connection,
169                                                 GB_CONTROL_TYPE_MODE_SWITCH,
170                                                 NULL, 0);
171         if (ret) {
172                 dev_err(&control->dev, "failed to send mode switch: %d\n",
173                                 ret);
174                 return ret;
175         }
176
177         return 0;
178 }
179
180 int gb_control_timesync_enable(struct gb_control *control, u8 count,
181                                u64 frame_time, u32 strobe_delay, u32 refclk)
182 {
183         struct gb_control_timesync_enable_request request;
184
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);
192 }
193
194 int gb_control_timesync_disable(struct gb_control *control)
195 {
196         return gb_operation_sync(control->connection,
197                                  GB_CONTROL_TYPE_TIMESYNC_DISABLE, NULL, 0,
198                                  NULL, 0);
199 }
200
201 int gb_control_timesync_get_last_event(struct gb_control *control,
202                                        u64 *frame_time)
203 {
204         struct gb_control_timesync_get_last_event_response response;
205         int ret;
206
207         ret = gb_operation_sync(control->connection,
208                                 GB_CONTROL_TYPE_TIMESYNC_GET_LAST_EVENT,
209                                 NULL, 0, &response, sizeof(response));
210         if (!ret)
211                 *frame_time = le64_to_cpu(response.frame_time);
212         return ret;
213 }
214
215 int gb_control_timesync_authoritative(struct gb_control *control,
216                                       u64 *frame_time)
217 {
218         struct gb_control_timesync_authoritative_request request;
219         int i;
220
221         for (i = 0; i < GB_TIMESYNC_MAX_STROBES; i++)
222                 request.frame_time[i] = cpu_to_le64(frame_time[i]);
223
224         return gb_operation_sync(control->connection,
225                                  GB_CONTROL_TYPE_TIMESYNC_AUTHORITATIVE,
226                                  &request, sizeof(request),
227                                  NULL, 0);
228 }
229
230 static ssize_t vendor_string_show(struct device *dev,
231                         struct device_attribute *attr, char *buf)
232 {
233         struct gb_control *control = to_gb_control(dev);
234
235         return scnprintf(buf, PAGE_SIZE, "%s\n", control->vendor_string);
236 }
237 static DEVICE_ATTR_RO(vendor_string);
238
239 static ssize_t product_string_show(struct device *dev,
240                         struct device_attribute *attr, char *buf)
241 {
242         struct gb_control *control = to_gb_control(dev);
243
244         return scnprintf(buf, PAGE_SIZE, "%s\n", control->product_string);
245 }
246 static DEVICE_ATTR_RO(product_string);
247
248 static struct attribute *control_attrs[] = {
249         &dev_attr_vendor_string.attr,
250         &dev_attr_product_string.attr,
251         NULL,
252 };
253 ATTRIBUTE_GROUPS(control);
254
255 static void gb_control_release(struct device *dev)
256 {
257         struct gb_control *control = to_gb_control(dev);
258
259         gb_connection_destroy(control->connection);
260
261         kfree(control->vendor_string);
262         kfree(control->product_string);
263
264         kfree(control);
265 }
266
267 struct device_type greybus_control_type = {
268         .name =         "greybus_control",
269         .release =      gb_control_release,
270 };
271
272 struct gb_control *gb_control_create(struct gb_interface *intf)
273 {
274         struct gb_connection *connection;
275         struct gb_control *control;
276
277         control = kzalloc(sizeof(*control), GFP_KERNEL);
278         if (!control)
279                 return ERR_PTR(-ENOMEM);
280
281         control->intf = intf;
282
283         connection = gb_connection_create_control(intf);
284         if (IS_ERR(connection)) {
285                 dev_err(&intf->dev,
286                                 "failed to create control connection: %ld\n",
287                                 PTR_ERR(connection));
288                 kfree(control);
289                 return ERR_CAST(connection);
290         }
291
292         control->connection = connection;
293
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));
301
302         gb_connection_set_data(control->connection, control);
303
304         return control;
305 }
306
307 int gb_control_enable(struct gb_control *control)
308 {
309         int ret;
310
311         dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
312
313         ret = gb_connection_enable_tx(control->connection);
314         if (ret) {
315                 dev_err(&control->connection->intf->dev,
316                                 "failed to enable control connection: %d\n",
317                                 ret);
318                 return ret;
319         }
320
321         ret = gb_control_get_version(control);
322         if (ret)
323                 goto err_disable_connection;
324
325         if (control->protocol_major > 0 || control->protocol_minor > 1)
326                 control->has_bundle_version = true;
327
328         return 0;
329
330 err_disable_connection:
331         gb_connection_disable(control->connection);
332
333         return ret;
334 }
335
336 void gb_control_disable(struct gb_control *control)
337 {
338         dev_dbg(&control->connection->intf->dev, "%s\n", __func__);
339
340         if (control->intf->disconnected)
341                 gb_connection_disable_forced(control->connection);
342         else
343                 gb_connection_disable(control->connection);
344 }
345
346 int gb_control_add(struct gb_control *control)
347 {
348         int ret;
349
350         ret = device_add(&control->dev);
351         if (ret) {
352                 dev_err(&control->dev,
353                                 "failed to register control device: %d\n",
354                                 ret);
355                 return ret;
356         }
357
358         return 0;
359 }
360
361 void gb_control_del(struct gb_control *control)
362 {
363         if (device_is_registered(&control->dev))
364                 device_del(&control->dev);
365 }
366
367 void gb_control_put(struct gb_control *control)
368 {
369         put_device(&control->dev);
370 }