blob: 17ffb1353fb48a706493fc8279c27083035bff15 [file] [log] [blame]
Alex Elder30c6d9d2015-05-22 13:02:08 -05001/*
2 * SVC Greybus driver.
3 *
4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
6 *
7 * Released under the GPLv2 only.
8 */
9
Alex Elder30c6d9d2015-05-22 13:02:08 -050010#include "greybus.h"
Alex Elder30c6d9d2015-05-22 13:02:08 -050011
Viresh Kumarb45864d2015-07-24 15:32:21 +053012struct gb_svc {
13 struct gb_connection *connection;
14 u8 version_major;
15 u8 version_minor;
16};
17
Viresh Kumaread35462015-07-21 17:44:19 +053018static struct ida greybus_svc_device_id_map;
19
Viresh Kumard3d44842015-07-21 17:44:18 +053020/*
21 * AP's SVC cport is required early to get messages from the SVC. This happens
22 * even before the Endo is created and hence any modules or interfaces.
23 *
24 * This is a temporary connection, used only at initial bootup.
25 */
26struct gb_connection *
27gb_ap_svc_connection_create(struct greybus_host_device *hd)
28{
29 struct gb_connection *connection;
30
31 connection = gb_connection_create_range(hd, NULL, hd->parent,
32 GB_SVC_CPORT_ID,
33 GREYBUS_PROTOCOL_SVC,
34 GB_SVC_CPORT_ID,
35 GB_SVC_CPORT_ID + 1);
36
37 return connection;
38}
Greg Kroah-Hartman3eac8852015-07-29 10:05:09 -070039EXPORT_SYMBOL_GPL(gb_ap_svc_connection_create);
Viresh Kumard3d44842015-07-21 17:44:18 +053040
41/*
42 * We know endo-type and AP's interface id now, lets create a proper svc
43 * connection (and its interface/bundle) now and get rid of the initial
44 * 'partially' initialized one svc connection.
45 */
46static struct gb_interface *
47gb_ap_interface_create(struct greybus_host_device *hd,
48 struct gb_connection *connection, u8 interface_id)
49{
50 struct gb_interface *intf;
51 struct device *dev = &hd->endo->dev;
Viresh Kumard3d44842015-07-21 17:44:18 +053052
53 intf = gb_interface_create(hd, interface_id);
54 if (!intf) {
55 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
56 __func__, interface_id);
57 return NULL;
58 }
59
60 intf->device_id = GB_DEVICE_ID_AP;
Viresh Kumar67c93ae2015-07-24 15:32:19 +053061 svc_update_connection(intf, connection);
Viresh Kumard3d44842015-07-21 17:44:18 +053062
Viresh Kumardcd05002015-07-24 15:32:20 +053063 /* Its no longer a partially initialized connection */
64 hd->initial_svc_connection = NULL;
65
Viresh Kumard3d44842015-07-21 17:44:18 +053066 return intf;
67}
68
Alex Elder30c6d9d2015-05-22 13:02:08 -050069static int intf_device_id_operation(struct gb_svc *svc,
70 u8 intf_id, u8 device_id)
71{
72 struct gb_svc_intf_device_id_request request;
73
74 request.intf_id = intf_id;
75 request.device_id = device_id;
76
77 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
78 &request, sizeof(request), NULL, 0);
79}
80
81static int intf_reset_operation(struct gb_svc *svc, u8 intf_id)
82{
83 struct gb_svc_intf_reset_request request;
84
85 request.intf_id = intf_id;
86
87 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
88 &request, sizeof(request), NULL, 0);
89}
90
91static int connection_create_operation(struct gb_svc *svc,
92 u8 intf1_id, u16 cport1_id,
93 u8 intf2_id, u16 cport2_id)
94{
95 struct gb_svc_conn_create_request request;
96
97 request.intf1_id = intf1_id;
98 request.cport1_id = cport1_id;
99 request.intf2_id = intf2_id;
100 request.cport2_id = cport2_id;
101
102 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
103 &request, sizeof(request), NULL, 0);
104}
105
106static int connection_destroy_operation(struct gb_svc *svc,
107 u8 intf1_id, u16 cport1_id,
108 u8 intf2_id, u16 cport2_id)
109{
110 struct gb_svc_conn_destroy_request request;
111
112 request.intf1_id = intf1_id;
113 request.cport1_id = cport1_id;
114 request.intf2_id = intf2_id;
115 request.cport2_id = cport2_id;
116
117 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_DESTROY,
118 &request, sizeof(request), NULL, 0);
119}
120
121int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
122{
123 return intf_device_id_operation(svc, intf_id, device_id);
124}
125EXPORT_SYMBOL_GPL(gb_svc_intf_device_id);
126
127int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
128{
129 return intf_reset_operation(svc, intf_id);
130}
131EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
132
133int gb_svc_connection_create(struct gb_svc *svc,
134 u8 intf1_id, u16 cport1_id,
135 u8 intf2_id, u16 cport2_id)
136{
137 return connection_create_operation(svc, intf1_id, cport1_id,
138 intf2_id, cport2_id);
139}
140EXPORT_SYMBOL_GPL(gb_svc_connection_create);
141
142int gb_svc_connection_destroy(struct gb_svc *svc,
143 u8 intf1_id, u16 cport1_id,
144 u8 intf2_id, u16 cport2_id)
145{
146 return connection_destroy_operation(svc, intf1_id, cport1_id,
147 intf2_id, cport2_id);
148}
149EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
150
Viresh Kumaread35462015-07-21 17:44:19 +0530151static int gb_svc_version_request(struct gb_operation *op)
152{
153 struct gb_connection *connection = op->connection;
154 struct gb_protocol_version_response *version;
155 struct device *dev = &connection->dev;
156
157 version = op->request->payload;
158
159 if (version->major > GB_SVC_VERSION_MAJOR) {
160 dev_err(&connection->dev,
161 "unsupported major version (%hhu > %hhu)\n",
162 version->major, GB_SVC_VERSION_MAJOR);
163 return -ENOTSUPP;
164 }
165
166 if (!gb_operation_response_alloc(op, sizeof(*version), GFP_KERNEL)) {
167 dev_err(dev, "%s: error allocating response\n",
168 __func__);
169 return -ENOMEM;
170 }
171
172 version = op->response->payload;
173 version->major = GB_SVC_VERSION_MAJOR;
174 version->minor = GB_SVC_VERSION_MINOR;
175 return 0;
176}
177
178static int gb_svc_hello(struct gb_operation *op)
179{
180 struct gb_connection *connection = op->connection;
181 struct greybus_host_device *hd = connection->hd;
182 struct gb_svc_hello_request *hello_request;
183 struct device *dev = &connection->dev;
184 struct gb_interface *intf;
185 u16 endo_id;
186 u8 interface_id;
187 int ret;
188
189 /* Hello message should be received only during early bootup */
190 WARN_ON(hd->initial_svc_connection != connection);
191
192 /*
193 * SVC sends information about the endo and interface-id on the hello
194 * request, use that to create an endo.
195 */
Viresh Kumar0c32d2a2015-08-11 07:29:19 +0530196 if (op->request->payload_size < sizeof(*hello_request)) {
197 dev_err(dev, "%s: Illegal size of hello request (%zu < %zu)\n",
Viresh Kumaread35462015-07-21 17:44:19 +0530198 __func__, op->request->payload_size,
199 sizeof(*hello_request));
200 return -EINVAL;
201 }
202
203 hello_request = op->request->payload;
204 endo_id = le16_to_cpu(hello_request->endo_id);
205 interface_id = hello_request->interface_id;
206
207 /* Setup Endo */
208 ret = greybus_endo_setup(hd, endo_id, interface_id);
209 if (ret)
210 return ret;
211
212 /*
213 * Endo and its modules are ready now, fix AP's partially initialized
214 * svc protocol and its connection.
215 */
216 intf = gb_ap_interface_create(hd, connection, interface_id);
217 if (!intf) {
218 gb_endo_remove(hd->endo);
219 return ret;
220 }
221
222 return 0;
223}
224
Alex Elder30c6d9d2015-05-22 13:02:08 -0500225static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
226{
227 struct gb_message *request = op->request;
Viresh Kumaread35462015-07-21 17:44:19 +0530228 struct gb_svc_intf_hotplug_request *hotplug = request->payload;
229 struct gb_svc *svc = op->connection->private;
230 struct greybus_host_device *hd = op->connection->bundle->intf->hd;
231 struct device *dev = &op->connection->dev;
232 struct gb_interface *intf;
233 u8 intf_id, device_id;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500234 u32 unipro_mfg_id;
235 u32 unipro_prod_id;
236 u32 ara_vend_id;
237 u32 ara_prod_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530238 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500239
240 if (request->payload_size < sizeof(*hotplug)) {
Viresh Kumaread35462015-07-21 17:44:19 +0530241 dev_err(dev, "%s: short hotplug request received\n", __func__);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500242 return -EINVAL;
243 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500244
245 /*
246 * Grab the information we need.
247 *
248 * XXX I'd really like to acknowledge receipt, and then
249 * XXX continue processing the request. There's no need
250 * XXX for the SVC to wait. In fact, it might be best to
251 * XXX have the SVC get acknowledgement before we proceed.
Viresh Kumar7eb89192015-07-01 12:13:50 +0530252 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500253 intf_id = hotplug->intf_id;
Phong Tranea15a402015-05-27 21:31:02 +0700254 unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
255 unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
256 ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id);
257 ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500258
Viresh Kumaread35462015-07-21 17:44:19 +0530259 intf = gb_interface_create(hd, intf_id);
260 if (!intf) {
261 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
262 __func__, intf_id);
263 return -EINVAL;
264 }
265
266 /*
267 * Create a device id for the interface:
268 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
269 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
270 *
271 * XXX Do we need to allocate device ID for SVC or the AP here? And what
272 * XXX about an AP with multiple interface blocks?
273 */
274 device_id = ida_simple_get(&greybus_svc_device_id_map,
275 GB_DEVICE_ID_MODULES_START, 0, GFP_ATOMIC);
276 if (device_id < 0) {
277 ret = device_id;
278 dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n",
279 __func__, intf_id, ret);
280 goto destroy_interface;
281 }
282
283 ret = intf_device_id_operation(svc, intf_id, device_id);
284 if (ret) {
285 dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
286 __func__, intf_id, device_id, ret);
287 goto ida_put;
288 }
289
290 ret = gb_interface_init(intf, device_id);
291 if (ret) {
292 dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
293 __func__, intf_id, device_id, ret);
294 goto svc_id_free;
295 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500296
297 return 0;
Viresh Kumaread35462015-07-21 17:44:19 +0530298
299svc_id_free:
300 /*
301 * XXX Should we tell SVC that this id doesn't belong to interface
302 * XXX anymore.
303 */
304ida_put:
305 ida_simple_remove(&greybus_svc_device_id_map, device_id);
306destroy_interface:
307 gb_interface_remove(hd, intf_id);
308
309 return ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500310}
311
312static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
313{
314 struct gb_message *request = op->request;
Viresh Kumaread35462015-07-21 17:44:19 +0530315 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
316 struct greybus_host_device *hd = op->connection->bundle->intf->hd;
317 struct device *dev = &op->connection->dev;
318 u8 device_id;
319 struct gb_interface *intf;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500320 u8 intf_id;
321
322 if (request->payload_size < sizeof(*hot_unplug)) {
323 dev_err(&op->connection->dev,
324 "short hot unplug request received\n");
325 return -EINVAL;
326 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500327
328 intf_id = hot_unplug->intf_id;
329
Viresh Kumaread35462015-07-21 17:44:19 +0530330 intf = gb_interface_find(hd, intf_id);
331 if (!intf) {
332 dev_err(dev, "%s: Couldn't find interface for id %hhu\n",
333 __func__, intf_id);
334 return -EINVAL;
335 }
336
337 device_id = intf->device_id;
338 gb_interface_remove(hd, intf_id);
339 ida_simple_remove(&greybus_svc_device_id_map, device_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500340
341 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500342}
343
344static int gb_svc_intf_reset_recv(struct gb_operation *op)
345{
346 struct gb_message *request = op->request;
347 struct gb_svc_intf_reset_request *reset;
348 u8 intf_id;
349
350 if (request->payload_size < sizeof(*reset)) {
351 dev_err(&op->connection->dev,
352 "short reset request received\n");
353 return -EINVAL;
354 }
355 reset = request->payload;
356
357 intf_id = reset->intf_id;
358
359 /* FIXME Reset the interface here */
360
361 return 0;
362}
363
364static int gb_svc_request_recv(u8 type, struct gb_operation *op)
365{
366 switch (type) {
Viresh Kumaread35462015-07-21 17:44:19 +0530367 case GB_SVC_TYPE_PROTOCOL_VERSION:
368 return gb_svc_version_request(op);
369 case GB_SVC_TYPE_SVC_HELLO:
370 return gb_svc_hello(op);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500371 case GB_SVC_TYPE_INTF_HOTPLUG:
372 return gb_svc_intf_hotplug_recv(op);
373 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
374 return gb_svc_intf_hot_unplug_recv(op);
375 case GB_SVC_TYPE_INTF_RESET:
376 return gb_svc_intf_reset_recv(op);
377 default:
378 dev_err(&op->connection->dev,
379 "unsupported request: %hhu\n", type);
380 return -EINVAL;
381 }
382}
383
Alex Elder30c6d9d2015-05-22 13:02:08 -0500384static int gb_svc_connection_init(struct gb_connection *connection)
385{
386 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500387
388 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
389 if (!svc)
390 return -ENOMEM;
391
392 svc->connection = connection;
393 connection->private = svc;
Viresh Kumard3d44842015-07-21 17:44:18 +0530394
Viresh Kumardcd05002015-07-24 15:32:20 +0530395 WARN_ON(connection->hd->initial_svc_connection);
396 connection->hd->initial_svc_connection = connection;
Viresh Kumard3d44842015-07-21 17:44:18 +0530397
398 ida_init(&greybus_svc_device_id_map);
399
Viresh Kumar18d777c2015-07-21 17:44:20 +0530400 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500401}
402
403static void gb_svc_connection_exit(struct gb_connection *connection)
404{
405 struct gb_svc *svc = connection->private;
406
Viresh Kumard3d44842015-07-21 17:44:18 +0530407 connection->private = NULL;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500408 kfree(svc);
409}
410
411static struct gb_protocol svc_protocol = {
412 .name = "svc",
413 .id = GREYBUS_PROTOCOL_SVC,
Viresh Kumar06e305f2015-07-01 12:13:51 +0530414 .major = GB_SVC_VERSION_MAJOR,
415 .minor = GB_SVC_VERSION_MINOR,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500416 .connection_init = gb_svc_connection_init,
417 .connection_exit = gb_svc_connection_exit,
418 .request_recv = gb_svc_request_recv,
419};
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530420gb_builtin_protocol_driver(svc_protocol);