blob: 35e2fb525dfae211c9ef9e50c6afe2107dd4c76a [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
Viresh Kumar067906f2015-08-06 12:44:55 +053010#include <linux/workqueue.h>
Alex Elder30c6d9d2015-05-22 13:02:08 -050011
Viresh Kumarf66427a2015-09-02 21:27:13 +053012#include "greybus.h"
13
Perry Hung0b226492015-07-24 19:02:34 -040014#define CPORT_FLAGS_E2EFC (1)
15#define CPORT_FLAGS_CSD_N (2)
16#define CPORT_FLAGS_CSV_N (4)
17
Viresh Kumar3ccb1602015-09-03 15:42:22 +053018enum gb_svc_state {
19 GB_SVC_STATE_RESET,
20 GB_SVC_STATE_PROTOCOL_VERSION,
21 GB_SVC_STATE_SVC_HELLO,
22};
23
Viresh Kumarb45864d2015-07-24 15:32:21 +053024struct gb_svc {
25 struct gb_connection *connection;
Viresh Kumar3ccb1602015-09-03 15:42:22 +053026 enum gb_svc_state state;
Johan Hovoldc09db182015-09-15 09:18:08 +020027 struct ida device_id_map;
Viresh Kumarb45864d2015-07-24 15:32:21 +053028};
29
Viresh Kumar067906f2015-08-06 12:44:55 +053030struct svc_hotplug {
31 struct work_struct work;
32 struct gb_connection *connection;
33 struct gb_svc_intf_hotplug_request data;
34};
35
Viresh Kumaread35462015-07-21 17:44:19 +053036
Viresh Kumard3d44842015-07-21 17:44:18 +053037/*
38 * AP's SVC cport is required early to get messages from the SVC. This happens
39 * even before the Endo is created and hence any modules or interfaces.
40 *
41 * This is a temporary connection, used only at initial bootup.
42 */
43struct gb_connection *
44gb_ap_svc_connection_create(struct greybus_host_device *hd)
45{
46 struct gb_connection *connection;
47
48 connection = gb_connection_create_range(hd, NULL, hd->parent,
49 GB_SVC_CPORT_ID,
50 GREYBUS_PROTOCOL_SVC,
51 GB_SVC_CPORT_ID,
52 GB_SVC_CPORT_ID + 1);
53
54 return connection;
55}
Viresh Kumard3d44842015-07-21 17:44:18 +053056
57/*
58 * We know endo-type and AP's interface id now, lets create a proper svc
59 * connection (and its interface/bundle) now and get rid of the initial
60 * 'partially' initialized one svc connection.
61 */
62static struct gb_interface *
63gb_ap_interface_create(struct greybus_host_device *hd,
64 struct gb_connection *connection, u8 interface_id)
65{
66 struct gb_interface *intf;
67 struct device *dev = &hd->endo->dev;
Viresh Kumard3d44842015-07-21 17:44:18 +053068
69 intf = gb_interface_create(hd, interface_id);
70 if (!intf) {
71 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
72 __func__, interface_id);
73 return NULL;
74 }
75
76 intf->device_id = GB_DEVICE_ID_AP;
Viresh Kumar67c93ae2015-07-24 15:32:19 +053077 svc_update_connection(intf, connection);
Viresh Kumard3d44842015-07-21 17:44:18 +053078
Viresh Kumardcd05002015-07-24 15:32:20 +053079 /* Its no longer a partially initialized connection */
80 hd->initial_svc_connection = NULL;
81
Viresh Kumard3d44842015-07-21 17:44:18 +053082 return intf;
83}
84
Viresh Kumar505f16c2015-08-31 17:21:07 +053085static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -050086{
87 struct gb_svc_intf_device_id_request request;
88
89 request.intf_id = intf_id;
90 request.device_id = device_id;
91
92 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
93 &request, sizeof(request), NULL, 0);
94}
95
Viresh Kumar3f0e9182015-08-31 17:21:06 +053096int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -050097{
98 struct gb_svc_intf_reset_request request;
99
100 request.intf_id = intf_id;
101
102 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
103 &request, sizeof(request), NULL, 0);
104}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530105EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500106
Viresh Kumar19151c32015-09-09 21:08:29 +0530107int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
108 u32 *value)
109{
110 struct gb_svc_dme_peer_get_request request;
111 struct gb_svc_dme_peer_get_response response;
112 u16 result;
113 int ret;
114
115 request.intf_id = intf_id;
116 request.attr = cpu_to_le16(attr);
117 request.selector = cpu_to_le16(selector);
118
119 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
120 &request, sizeof(request),
121 &response, sizeof(response));
122 if (ret) {
123 dev_err(&svc->connection->dev,
124 "failed to get DME attribute (%hhu %hx %hu) %d\n",
125 intf_id, attr, selector, ret);
126 return ret;
127 }
128
129 result = le16_to_cpu(response.result_code);
130 if (result) {
131 dev_err(&svc->connection->dev,
132 "Unipro error %hu while getting DME attribute (%hhu %hx %hu)\n",
133 result, intf_id, attr, selector);
134 return -EINVAL;
135 }
136
137 if (value)
138 *value = le32_to_cpu(response.attr_value);
139
140 return 0;
141}
142EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
143
144int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
145 u32 value)
146{
147 struct gb_svc_dme_peer_set_request request;
148 struct gb_svc_dme_peer_set_response response;
149 u16 result;
150 int ret;
151
152 request.intf_id = intf_id;
153 request.attr = cpu_to_le16(attr);
154 request.selector = cpu_to_le16(selector);
155 request.value = cpu_to_le32(value);
156
157 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
158 &request, sizeof(request),
159 &response, sizeof(response));
160 if (ret) {
161 dev_err(&svc->connection->dev,
162 "failed to set DME attribute (%hhu %hx %hu %u) %d\n",
163 intf_id, attr, selector, value, ret);
164 return ret;
165 }
166
167 result = le16_to_cpu(response.result_code);
168 if (result) {
169 dev_err(&svc->connection->dev,
170 "Unipro error %hu while setting DME attribute (%hhu %hx %hu %u)\n",
171 result, intf_id, attr, selector, value);
172 return -EINVAL;
173 }
174
175 return 0;
176}
177EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
178
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530179int gb_svc_connection_create(struct gb_svc *svc,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500180 u8 intf1_id, u16 cport1_id,
181 u8 intf2_id, u16 cport2_id)
182{
183 struct gb_svc_conn_create_request request;
184
185 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100186 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500187 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100188 request.cport2_id = cpu_to_le16(cport2_id);
Perry Hung0b226492015-07-24 19:02:34 -0400189 /*
190 * XXX: fix connections paramaters to TC0 and all CPort flags
191 * for now.
192 */
193 request.tc = 0;
194 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500195
196 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
197 &request, sizeof(request), NULL, 0);
198}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530199EXPORT_SYMBOL_GPL(gb_svc_connection_create);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500200
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530201void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
202 u8 intf2_id, u16 cport2_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500203{
204 struct gb_svc_conn_destroy_request request;
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530205 struct gb_connection *connection = svc->connection;
206 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500207
208 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100209 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500210 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100211 request.cport2_id = cpu_to_le16(cport2_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500212
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530213 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
214 &request, sizeof(request), NULL, 0);
215 if (ret) {
216 dev_err(&connection->dev,
217 "failed to destroy connection (%hhx:%hx %hhx:%hx) %d\n",
218 intf1_id, cport1_id, intf2_id, cport2_id, ret);
219 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500220}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530221EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500222
Viresh Kumarbb106852015-09-07 16:01:25 +0530223/* Creates bi-directional routes between the devices */
Viresh Kumar505f16c2015-08-31 17:21:07 +0530224static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
225 u8 intf2_id, u8 dev2_id)
Perry Hunge08aaa42015-07-24 19:02:31 -0400226{
227 struct gb_svc_route_create_request request;
228
229 request.intf1_id = intf1_id;
230 request.dev1_id = dev1_id;
231 request.intf2_id = intf2_id;
232 request.dev2_id = dev2_id;
233
234 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
235 &request, sizeof(request), NULL, 0);
236}
Perry Hunge08aaa42015-07-24 19:02:31 -0400237
Viresh Kumar0a020572015-09-07 18:05:26 +0530238/* Destroys bi-directional routes between the devices */
239static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
240{
241 struct gb_svc_route_destroy_request request;
242 int ret;
243
244 request.intf1_id = intf1_id;
245 request.intf2_id = intf2_id;
246
247 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
248 &request, sizeof(request), NULL, 0);
249 if (ret) {
250 dev_err(&svc->connection->dev,
251 "failed to destroy route (%hhx %hhx) %d\n",
252 intf1_id, intf2_id, ret);
253 }
254}
255
Viresh Kumaread35462015-07-21 17:44:19 +0530256static int gb_svc_version_request(struct gb_operation *op)
257{
258 struct gb_connection *connection = op->connection;
Johan Hovoldcfb16902015-09-15 10:48:01 +0200259 struct gb_protocol_version_request *request;
260 struct gb_protocol_version_response *response;
Viresh Kumaread35462015-07-21 17:44:19 +0530261 struct device *dev = &connection->dev;
262
Johan Hovoldcfb16902015-09-15 10:48:01 +0200263 request = op->request->payload;
Viresh Kumaread35462015-07-21 17:44:19 +0530264
Johan Hovoldcfb16902015-09-15 10:48:01 +0200265 if (request->major > GB_SVC_VERSION_MAJOR) {
Viresh Kumaread35462015-07-21 17:44:19 +0530266 dev_err(&connection->dev,
267 "unsupported major version (%hhu > %hhu)\n",
Johan Hovoldcfb16902015-09-15 10:48:01 +0200268 request->major, GB_SVC_VERSION_MAJOR);
Viresh Kumaread35462015-07-21 17:44:19 +0530269 return -ENOTSUPP;
270 }
271
Johan Hovoldcfb16902015-09-15 10:48:01 +0200272 connection->module_major = request->major;
273 connection->module_minor = request->minor;
Viresh Kumar3ea959e32015-08-11 07:36:14 +0530274
Johan Hovoldcfb16902015-09-15 10:48:01 +0200275 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) {
Viresh Kumaread35462015-07-21 17:44:19 +0530276 dev_err(dev, "%s: error allocating response\n",
277 __func__);
278 return -ENOMEM;
279 }
280
Johan Hovoldcfb16902015-09-15 10:48:01 +0200281 response = op->response->payload;
282 response->major = connection->module_major;
283 response->minor = connection->module_minor;
Johan Hovold59832932015-09-15 10:48:00 +0200284
Viresh Kumaread35462015-07-21 17:44:19 +0530285 return 0;
286}
287
288static int gb_svc_hello(struct gb_operation *op)
289{
290 struct gb_connection *connection = op->connection;
291 struct greybus_host_device *hd = connection->hd;
292 struct gb_svc_hello_request *hello_request;
293 struct device *dev = &connection->dev;
294 struct gb_interface *intf;
295 u16 endo_id;
296 u8 interface_id;
297 int ret;
298
Viresh Kumaread35462015-07-21 17:44:19 +0530299 /*
300 * SVC sends information about the endo and interface-id on the hello
301 * request, use that to create an endo.
302 */
Viresh Kumar0c32d2a2015-08-11 07:29:19 +0530303 if (op->request->payload_size < sizeof(*hello_request)) {
304 dev_err(dev, "%s: Illegal size of hello request (%zu < %zu)\n",
Viresh Kumaread35462015-07-21 17:44:19 +0530305 __func__, op->request->payload_size,
306 sizeof(*hello_request));
307 return -EINVAL;
308 }
309
310 hello_request = op->request->payload;
311 endo_id = le16_to_cpu(hello_request->endo_id);
312 interface_id = hello_request->interface_id;
313
314 /* Setup Endo */
315 ret = greybus_endo_setup(hd, endo_id, interface_id);
316 if (ret)
317 return ret;
318
319 /*
320 * Endo and its modules are ready now, fix AP's partially initialized
321 * svc protocol and its connection.
322 */
323 intf = gb_ap_interface_create(hd, connection, interface_id);
324 if (!intf) {
325 gb_endo_remove(hd->endo);
326 return ret;
327 }
328
329 return 0;
330}
331
Viresh Kumar067906f2015-08-06 12:44:55 +0530332/*
333 * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
334 * returns, irrespective of success or Failure in bringing up the module.
335 */
336static void svc_process_hotplug(struct work_struct *work)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500337{
Viresh Kumar067906f2015-08-06 12:44:55 +0530338 struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
339 work);
340 struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
341 struct gb_connection *connection = svc_hotplug->connection;
342 struct gb_svc *svc = connection->private;
Viresh Kumarb9fb7042015-09-01 17:16:16 +0530343 struct greybus_host_device *hd = connection->hd;
Viresh Kumar067906f2015-08-06 12:44:55 +0530344 struct device *dev = &connection->dev;
Viresh Kumaread35462015-07-21 17:44:19 +0530345 struct gb_interface *intf;
346 u8 intf_id, device_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530347 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500348
Alex Elder30c6d9d2015-05-22 13:02:08 -0500349 /*
350 * Grab the information we need.
Viresh Kumar7eb89192015-07-01 12:13:50 +0530351 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500352 intf_id = hotplug->intf_id;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500353
Viresh Kumaread35462015-07-21 17:44:19 +0530354 intf = gb_interface_create(hd, intf_id);
355 if (!intf) {
356 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
357 __func__, intf_id);
Viresh Kumar067906f2015-08-06 12:44:55 +0530358 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530359 }
360
Viresh Kumar3944a452015-08-12 09:19:31 +0530361 intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
362 intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
363 intf->ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id);
364 intf->ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id);
365
Viresh Kumaread35462015-07-21 17:44:19 +0530366 /*
367 * Create a device id for the interface:
368 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
369 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
370 *
371 * XXX Do we need to allocate device ID for SVC or the AP here? And what
372 * XXX about an AP with multiple interface blocks?
373 */
Johan Hovoldc09db182015-09-15 09:18:08 +0200374 device_id = ida_simple_get(&svc->device_id_map,
Johan Hovold89f637f2015-09-01 12:25:25 +0200375 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
Viresh Kumaread35462015-07-21 17:44:19 +0530376 if (device_id < 0) {
377 ret = device_id;
378 dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n",
379 __func__, intf_id, ret);
380 goto destroy_interface;
381 }
382
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530383 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530384 if (ret) {
385 dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
386 __func__, intf_id, device_id, ret);
387 goto ida_put;
388 }
389
Perry Hung7e275462015-07-24 19:02:32 -0400390 /*
391 * Create a two-way route between the AP and the new interface
392 */
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530393 ret = gb_svc_route_create(svc, hd->endo->ap_intf_id, GB_DEVICE_ID_AP,
394 intf_id, device_id);
Perry Hung7e275462015-07-24 19:02:32 -0400395 if (ret) {
396 dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
397 __func__, intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530398 goto svc_id_free;
Perry Hung7e275462015-07-24 19:02:32 -0400399 }
400
Viresh Kumaread35462015-07-21 17:44:19 +0530401 ret = gb_interface_init(intf, device_id);
402 if (ret) {
403 dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
404 __func__, intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530405 goto destroy_route;
Viresh Kumaread35462015-07-21 17:44:19 +0530406 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500407
Viresh Kumar067906f2015-08-06 12:44:55 +0530408 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530409
Viresh Kumar0a020572015-09-07 18:05:26 +0530410destroy_route:
411 gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530412svc_id_free:
413 /*
414 * XXX Should we tell SVC that this id doesn't belong to interface
415 * XXX anymore.
416 */
417ida_put:
Johan Hovoldc09db182015-09-15 09:18:08 +0200418 ida_simple_remove(&svc->device_id_map, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530419destroy_interface:
420 gb_interface_remove(hd, intf_id);
Viresh Kumar067906f2015-08-06 12:44:55 +0530421free_svc_hotplug:
422 kfree(svc_hotplug);
423}
Viresh Kumaread35462015-07-21 17:44:19 +0530424
Viresh Kumar067906f2015-08-06 12:44:55 +0530425/*
426 * Bringing up a module can be time consuming, as that may require lots of
427 * initialization on the module side. Over that, we may also need to download
428 * the firmware first and flash that on the module.
429 *
430 * In order to make other hotplug events to not wait for all this to finish,
431 * handle most of module hotplug stuff outside of the hotplug callback, with
432 * help of a workqueue.
433 */
434static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
435{
436 struct gb_message *request = op->request;
437 struct svc_hotplug *svc_hotplug;
438
439 if (request->payload_size < sizeof(svc_hotplug->data)) {
440 dev_err(&op->connection->dev,
441 "%s: short hotplug request received (%zu < %zu)\n",
442 __func__, request->payload_size,
443 sizeof(svc_hotplug->data));
444 return -EINVAL;
445 }
446
Johan Hovold287bba82015-09-01 12:25:26 +0200447 svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL);
Viresh Kumar067906f2015-08-06 12:44:55 +0530448 if (!svc_hotplug)
449 return -ENOMEM;
450
451 svc_hotplug->connection = op->connection;
452 memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data));
453
454 INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
455 queue_work(system_unbound_wq, &svc_hotplug->work);
456
457 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500458}
459
460static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
461{
462 struct gb_message *request = op->request;
Viresh Kumaread35462015-07-21 17:44:19 +0530463 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
Viresh Kumarb9fb7042015-09-01 17:16:16 +0530464 struct greybus_host_device *hd = op->connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530465 struct device *dev = &op->connection->dev;
Viresh Kumar0a020572015-09-07 18:05:26 +0530466 struct gb_svc *svc = op->connection->private;
Viresh Kumaread35462015-07-21 17:44:19 +0530467 u8 device_id;
468 struct gb_interface *intf;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500469 u8 intf_id;
470
471 if (request->payload_size < sizeof(*hot_unplug)) {
472 dev_err(&op->connection->dev,
Viresh Kumar6d05ad32015-08-06 12:44:54 +0530473 "short hot unplug request received (%zu < %zu)\n",
474 request->payload_size, sizeof(*hot_unplug));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500475 return -EINVAL;
476 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500477
478 intf_id = hot_unplug->intf_id;
479
Viresh Kumaread35462015-07-21 17:44:19 +0530480 intf = gb_interface_find(hd, intf_id);
481 if (!intf) {
482 dev_err(dev, "%s: Couldn't find interface for id %hhu\n",
483 __func__, intf_id);
484 return -EINVAL;
485 }
486
487 device_id = intf->device_id;
488 gb_interface_remove(hd, intf_id);
Viresh Kumar0a020572015-09-07 18:05:26 +0530489
490 /*
491 * Destroy the two-way route between the AP and the interface.
492 */
493 gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
494
Johan Hovoldc09db182015-09-15 09:18:08 +0200495 ida_simple_remove(&svc->device_id_map, device_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500496
497 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500498}
499
500static int gb_svc_intf_reset_recv(struct gb_operation *op)
501{
502 struct gb_message *request = op->request;
503 struct gb_svc_intf_reset_request *reset;
504 u8 intf_id;
505
506 if (request->payload_size < sizeof(*reset)) {
507 dev_err(&op->connection->dev,
Viresh Kumar6d05ad32015-08-06 12:44:54 +0530508 "short reset request received (%zu < %zu)\n",
509 request->payload_size, sizeof(*reset));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500510 return -EINVAL;
511 }
512 reset = request->payload;
513
514 intf_id = reset->intf_id;
515
516 /* FIXME Reset the interface here */
517
518 return 0;
519}
520
521static int gb_svc_request_recv(u8 type, struct gb_operation *op)
522{
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530523 struct gb_connection *connection = op->connection;
524 struct gb_svc *svc = connection->private;
525 int ret = 0;
526
527 /*
528 * SVC requests need to follow a specific order (at least initially) and
529 * below code takes care of enforcing that. The expected order is:
530 * - PROTOCOL_VERSION
531 * - SVC_HELLO
532 * - Any other request, but the earlier two.
533 *
534 * Incoming requests are guaranteed to be serialized and so we don't
535 * need to protect 'state' for any races.
536 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500537 switch (type) {
Viresh Kumar0e2462d2015-08-14 07:57:38 +0530538 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530539 if (svc->state != GB_SVC_STATE_RESET)
540 ret = -EINVAL;
541 break;
Viresh Kumaread35462015-07-21 17:44:19 +0530542 case GB_SVC_TYPE_SVC_HELLO:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530543 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
544 ret = -EINVAL;
545 break;
546 default:
547 if (svc->state != GB_SVC_STATE_SVC_HELLO)
548 ret = -EINVAL;
549 break;
550 }
551
552 if (ret) {
553 dev_warn(&connection->dev,
554 "unexpected SVC request 0x%02x received (state %u)\n",
555 type, svc->state);
556 return ret;
557 }
558
559 switch (type) {
560 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
561 ret = gb_svc_version_request(op);
562 if (!ret)
563 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
564 return ret;
565 case GB_SVC_TYPE_SVC_HELLO:
566 ret = gb_svc_hello(op);
567 if (!ret)
568 svc->state = GB_SVC_STATE_SVC_HELLO;
569 return ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500570 case GB_SVC_TYPE_INTF_HOTPLUG:
571 return gb_svc_intf_hotplug_recv(op);
572 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
573 return gb_svc_intf_hot_unplug_recv(op);
574 case GB_SVC_TYPE_INTF_RESET:
575 return gb_svc_intf_reset_recv(op);
576 default:
577 dev_err(&op->connection->dev,
578 "unsupported request: %hhu\n", type);
579 return -EINVAL;
580 }
581}
582
Alex Elder30c6d9d2015-05-22 13:02:08 -0500583static int gb_svc_connection_init(struct gb_connection *connection)
584{
585 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500586
587 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
588 if (!svc)
589 return -ENOMEM;
590
Perry Hung75a60ed2015-07-24 19:02:33 -0400591 connection->hd->svc = svc;
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530592 svc->state = GB_SVC_STATE_RESET;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500593 svc->connection = connection;
594 connection->private = svc;
Viresh Kumard3d44842015-07-21 17:44:18 +0530595
Viresh Kumardcd05002015-07-24 15:32:20 +0530596 WARN_ON(connection->hd->initial_svc_connection);
597 connection->hd->initial_svc_connection = connection;
Viresh Kumard3d44842015-07-21 17:44:18 +0530598
Johan Hovoldc09db182015-09-15 09:18:08 +0200599 ida_init(&svc->device_id_map);
Viresh Kumard3d44842015-07-21 17:44:18 +0530600
Viresh Kumar18d777c2015-07-21 17:44:20 +0530601 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500602}
603
604static void gb_svc_connection_exit(struct gb_connection *connection)
605{
606 struct gb_svc *svc = connection->private;
607
Johan Hovoldc09db182015-09-15 09:18:08 +0200608 ida_destroy(&svc->device_id_map);
Perry Hung75a60ed2015-07-24 19:02:33 -0400609 connection->hd->svc = NULL;
Viresh Kumard3d44842015-07-21 17:44:18 +0530610 connection->private = NULL;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500611 kfree(svc);
612}
613
614static struct gb_protocol svc_protocol = {
615 .name = "svc",
616 .id = GREYBUS_PROTOCOL_SVC,
Viresh Kumar06e305f2015-07-01 12:13:51 +0530617 .major = GB_SVC_VERSION_MAJOR,
618 .minor = GB_SVC_VERSION_MINOR,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500619 .connection_init = gb_svc_connection_init,
620 .connection_exit = gb_svc_connection_exit,
621 .request_recv = gb_svc_request_recv,
Viresh Kumar5a5296b2015-09-07 16:01:24 +0530622 .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
623 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
624 GB_PROTOCOL_NO_BUNDLE |
625 GB_PROTOCOL_SKIP_VERSION |
626 GB_PROTOCOL_SKIP_SVC_CONNECTION,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500627};
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530628gb_builtin_protocol_driver(svc_protocol);