blob: bd045678493f63e99b3e2b43f043082a2619c58e [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
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Viresh Kumar067906f2015-08-06 12:44:55 +053011#include <linux/workqueue.h>
Alex Elder30c6d9d2015-05-22 13:02:08 -050012
Viresh Kumarf66427a2015-09-02 21:27:13 +053013#include "greybus.h"
14
Johan Hovoldf6c6c132015-11-11 10:07:08 +010015#define CPORT_FLAGS_E2EFC BIT(0)
16#define CPORT_FLAGS_CSD_N BIT(1)
17#define CPORT_FLAGS_CSV_N BIT(2)
Perry Hung0b226492015-07-24 19:02:34 -040018
Viresh Kumar3ccb1602015-09-03 15:42:22 +053019enum gb_svc_state {
20 GB_SVC_STATE_RESET,
21 GB_SVC_STATE_PROTOCOL_VERSION,
22 GB_SVC_STATE_SVC_HELLO,
23};
24
Viresh Kumarb45864d2015-07-24 15:32:21 +053025struct gb_svc {
26 struct gb_connection *connection;
Viresh Kumar3ccb1602015-09-03 15:42:22 +053027 enum gb_svc_state state;
Johan Hovoldc09db182015-09-15 09:18:08 +020028 struct ida device_id_map;
Viresh Kumarb45864d2015-07-24 15:32:21 +053029};
30
Viresh Kumar067906f2015-08-06 12:44:55 +053031struct svc_hotplug {
32 struct work_struct work;
33 struct gb_connection *connection;
34 struct gb_svc_intf_hotplug_request data;
35};
36
Viresh Kumaread35462015-07-21 17:44:19 +053037
Viresh Kumard3d44842015-07-21 17:44:18 +053038/*
39 * AP's SVC cport is required early to get messages from the SVC. This happens
40 * even before the Endo is created and hence any modules or interfaces.
41 *
42 * This is a temporary connection, used only at initial bootup.
43 */
44struct gb_connection *
Johan Hovold25376362015-11-03 18:03:23 +010045gb_ap_svc_connection_create(struct gb_host_device *hd)
Viresh Kumard3d44842015-07-21 17:44:18 +053046{
47 struct gb_connection *connection;
48
49 connection = gb_connection_create_range(hd, NULL, hd->parent,
50 GB_SVC_CPORT_ID,
51 GREYBUS_PROTOCOL_SVC,
52 GB_SVC_CPORT_ID,
53 GB_SVC_CPORT_ID + 1);
54
55 return connection;
56}
Viresh Kumard3d44842015-07-21 17:44:18 +053057
58/*
59 * We know endo-type and AP's interface id now, lets create a proper svc
60 * connection (and its interface/bundle) now and get rid of the initial
61 * 'partially' initialized one svc connection.
62 */
63static struct gb_interface *
Johan Hovold25376362015-11-03 18:03:23 +010064gb_ap_interface_create(struct gb_host_device *hd,
Viresh Kumard3d44842015-07-21 17:44:18 +053065 struct gb_connection *connection, u8 interface_id)
66{
67 struct gb_interface *intf;
68 struct device *dev = &hd->endo->dev;
Viresh Kumard3d44842015-07-21 17:44:18 +053069
70 intf = gb_interface_create(hd, interface_id);
71 if (!intf) {
72 dev_err(dev, "%s: Failed to create interface with id %hhu\n",
73 __func__, interface_id);
74 return NULL;
75 }
76
77 intf->device_id = GB_DEVICE_ID_AP;
Viresh Kumar67c93ae2015-07-24 15:32:19 +053078 svc_update_connection(intf, connection);
Viresh Kumard3d44842015-07-21 17:44:18 +053079
Viresh Kumardcd05002015-07-24 15:32:20 +053080 /* Its no longer a partially initialized connection */
81 hd->initial_svc_connection = NULL;
82
Viresh Kumard3d44842015-07-21 17:44:18 +053083 return intf;
84}
85
Viresh Kumar505f16c2015-08-31 17:21:07 +053086static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -050087{
88 struct gb_svc_intf_device_id_request request;
89
90 request.intf_id = intf_id;
91 request.device_id = device_id;
92
93 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
94 &request, sizeof(request), NULL, 0);
95}
96
Viresh Kumar3f0e9182015-08-31 17:21:06 +053097int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -050098{
99 struct gb_svc_intf_reset_request request;
100
101 request.intf_id = intf_id;
102
103 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
104 &request, sizeof(request), NULL, 0);
105}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530106EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500107
Viresh Kumar19151c32015-09-09 21:08:29 +0530108int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
109 u32 *value)
110{
111 struct gb_svc_dme_peer_get_request request;
112 struct gb_svc_dme_peer_get_response response;
113 u16 result;
114 int ret;
115
116 request.intf_id = intf_id;
117 request.attr = cpu_to_le16(attr);
118 request.selector = cpu_to_le16(selector);
119
120 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
121 &request, sizeof(request),
122 &response, sizeof(response));
123 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700124 pr_err("failed to get DME attribute (%hhu %hx %hu) %d\n",
125 intf_id, attr, selector, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530126 return ret;
127 }
128
129 result = le16_to_cpu(response.result_code);
130 if (result) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700131 pr_err("Unipro error %hu while getting DME attribute (%hhu %hx %hu)\n",
132 result, intf_id, attr, selector);
Viresh Kumar19151c32015-09-09 21:08:29 +0530133 return -EINVAL;
134 }
135
136 if (value)
137 *value = le32_to_cpu(response.attr_value);
138
139 return 0;
140}
141EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
142
143int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
144 u32 value)
145{
146 struct gb_svc_dme_peer_set_request request;
147 struct gb_svc_dme_peer_set_response response;
148 u16 result;
149 int ret;
150
151 request.intf_id = intf_id;
152 request.attr = cpu_to_le16(attr);
153 request.selector = cpu_to_le16(selector);
154 request.value = cpu_to_le32(value);
155
156 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
157 &request, sizeof(request),
158 &response, sizeof(response));
159 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700160 pr_err("failed to set DME attribute (%hhu %hx %hu %u) %d\n",
161 intf_id, attr, selector, value, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530162 return ret;
163 }
164
165 result = le16_to_cpu(response.result_code);
166 if (result) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700167 pr_err("Unipro error %hu while setting DME attribute (%hhu %hx %hu %u)\n",
168 result, intf_id, attr, selector, value);
Viresh Kumar19151c32015-09-09 21:08:29 +0530169 return -EINVAL;
170 }
171
172 return 0;
173}
174EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
175
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700176/*
177 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
178 * status attribute. AP needs to read and clear it, after reading a non-zero
179 * value from it.
180 *
181 * FIXME: This is module-hardware dependent and needs to be extended for every
182 * type of module we want to support.
183 */
184static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
185{
Johan Hovold25376362015-11-03 18:03:23 +0100186 struct gb_host_device *hd = intf->hd;
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700187 int ret;
188 u32 value;
189
190 /* Read and clear boot status in T_TstSrcIncrement */
191 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id,
192 DME_ATTR_T_TST_SRC_INCREMENT,
193 DME_ATTR_SELECTOR_INDEX, &value);
194
195 if (ret)
196 return ret;
197
198 /*
199 * A nonzero boot status indicates the module has finished
200 * booting. Clear it.
201 */
202 if (!value) {
203 dev_err(&intf->dev, "Module not ready yet\n");
204 return -ENODEV;
205 }
206
Viresh Kumar1575ef12015-10-07 15:40:24 -0400207 /*
208 * Check if the module needs to boot from unipro.
209 * For ES2: We need to check lowest 8 bits of 'value'.
210 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
211 *
212 * FIXME: Add code to find if we are on ES2 or ES3 to have separate
213 * checks.
214 */
215 if (value == DME_TSI_UNIPRO_BOOT_STARTED ||
216 value == DME_TSI_FALLBACK_UNIPRO_BOOT_STARTED)
217 intf->boot_over_unipro = true;
218
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700219 return gb_svc_dme_peer_set(hd->svc, intf->interface_id,
220 DME_ATTR_T_TST_SRC_INCREMENT,
221 DME_ATTR_SELECTOR_INDEX, 0);
222}
223
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530224int gb_svc_connection_create(struct gb_svc *svc,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500225 u8 intf1_id, u16 cport1_id,
Viresh Kumar1575ef12015-10-07 15:40:24 -0400226 u8 intf2_id, u16 cport2_id,
227 bool boot_over_unipro)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500228{
229 struct gb_svc_conn_create_request request;
230
231 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100232 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500233 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100234 request.cport2_id = cpu_to_le16(cport2_id);
Perry Hung0b226492015-07-24 19:02:34 -0400235 /*
236 * XXX: fix connections paramaters to TC0 and all CPort flags
237 * for now.
238 */
239 request.tc = 0;
Viresh Kumar1575ef12015-10-07 15:40:24 -0400240
241 /*
242 * We need to skip setting E2EFC and other flags to the connection
243 * create request, for all cports, on an interface that need to boot
244 * over unipro, i.e. interfaces required to download firmware.
245 */
246 if (boot_over_unipro)
247 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
248 else
249 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500250
251 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
252 &request, sizeof(request), NULL, 0);
253}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530254EXPORT_SYMBOL_GPL(gb_svc_connection_create);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500255
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530256void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
257 u8 intf2_id, u16 cport2_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500258{
259 struct gb_svc_conn_destroy_request request;
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530260 struct gb_connection *connection = svc->connection;
261 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500262
263 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100264 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500265 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100266 request.cport2_id = cpu_to_le16(cport2_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500267
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530268 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
269 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700270 if (ret)
Johan Hovold35a84ba2015-11-11 10:07:07 +0100271 pr_err("failed to destroy connection (%hhu:%hu %hhu:%hu) %d\n",
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700272 intf1_id, cport1_id, intf2_id, cport2_id, ret);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500273}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530274EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500275
Viresh Kumarbb106852015-09-07 16:01:25 +0530276/* Creates bi-directional routes between the devices */
Viresh Kumar505f16c2015-08-31 17:21:07 +0530277static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
278 u8 intf2_id, u8 dev2_id)
Perry Hunge08aaa42015-07-24 19:02:31 -0400279{
280 struct gb_svc_route_create_request request;
281
282 request.intf1_id = intf1_id;
283 request.dev1_id = dev1_id;
284 request.intf2_id = intf2_id;
285 request.dev2_id = dev2_id;
286
287 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
288 &request, sizeof(request), NULL, 0);
289}
Perry Hunge08aaa42015-07-24 19:02:31 -0400290
Viresh Kumar0a020572015-09-07 18:05:26 +0530291/* Destroys bi-directional routes between the devices */
292static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
293{
294 struct gb_svc_route_destroy_request request;
295 int ret;
296
297 request.intf1_id = intf1_id;
298 request.intf2_id = intf2_id;
299
300 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
301 &request, sizeof(request), NULL, 0);
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700302 if (ret)
Johan Hovold35a84ba2015-11-11 10:07:07 +0100303 pr_err("failed to destroy route (%hhu %hhu) %d\n",
Viresh Kumar0a020572015-09-07 18:05:26 +0530304 intf1_id, intf2_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530305}
306
Viresh Kumaread35462015-07-21 17:44:19 +0530307static int gb_svc_version_request(struct gb_operation *op)
308{
309 struct gb_connection *connection = op->connection;
Johan Hovoldcfb16902015-09-15 10:48:01 +0200310 struct gb_protocol_version_request *request;
311 struct gb_protocol_version_response *response;
Viresh Kumaread35462015-07-21 17:44:19 +0530312
Johan Hovold55510842015-11-19 18:28:01 +0100313 if (op->request->payload_size < sizeof(*request)) {
314 pr_err("%d: short version request (%zu < %zu)\n",
315 connection->intf_cport_id,
316 op->request->payload_size,
317 sizeof(*request));
318 return -EINVAL;
319 }
320
Johan Hovoldcfb16902015-09-15 10:48:01 +0200321 request = op->request->payload;
Viresh Kumaread35462015-07-21 17:44:19 +0530322
Johan Hovoldcfb16902015-09-15 10:48:01 +0200323 if (request->major > GB_SVC_VERSION_MAJOR) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700324 pr_err("%d: unsupported major version (%hhu > %hhu)\n",
325 connection->intf_cport_id, request->major,
326 GB_SVC_VERSION_MAJOR);
Viresh Kumaread35462015-07-21 17:44:19 +0530327 return -ENOTSUPP;
328 }
329
Johan Hovoldcfb16902015-09-15 10:48:01 +0200330 connection->module_major = request->major;
331 connection->module_minor = request->minor;
Viresh Kumar3ea959e32015-08-11 07:36:14 +0530332
Johan Hovoldcfb16902015-09-15 10:48:01 +0200333 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700334 pr_err("%d: error allocating response\n",
335 connection->intf_cport_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530336 return -ENOMEM;
337 }
338
Johan Hovoldcfb16902015-09-15 10:48:01 +0200339 response = op->response->payload;
340 response->major = connection->module_major;
341 response->minor = connection->module_minor;
Johan Hovold59832932015-09-15 10:48:00 +0200342
Viresh Kumaread35462015-07-21 17:44:19 +0530343 return 0;
344}
345
346static int gb_svc_hello(struct gb_operation *op)
347{
348 struct gb_connection *connection = op->connection;
Johan Hovold25376362015-11-03 18:03:23 +0100349 struct gb_host_device *hd = connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530350 struct gb_svc_hello_request *hello_request;
Viresh Kumaread35462015-07-21 17:44:19 +0530351 struct gb_interface *intf;
352 u16 endo_id;
353 u8 interface_id;
354 int ret;
355
Viresh Kumaread35462015-07-21 17:44:19 +0530356 /*
357 * SVC sends information about the endo and interface-id on the hello
358 * request, use that to create an endo.
359 */
Viresh Kumar0c32d2a2015-08-11 07:29:19 +0530360 if (op->request->payload_size < sizeof(*hello_request)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700361 pr_err("%d: Illegal size of hello request (%zu < %zu)\n",
362 connection->intf_cport_id, op->request->payload_size,
363 sizeof(*hello_request));
Viresh Kumaread35462015-07-21 17:44:19 +0530364 return -EINVAL;
365 }
366
367 hello_request = op->request->payload;
368 endo_id = le16_to_cpu(hello_request->endo_id);
369 interface_id = hello_request->interface_id;
370
371 /* Setup Endo */
372 ret = greybus_endo_setup(hd, endo_id, interface_id);
373 if (ret)
374 return ret;
375
376 /*
377 * Endo and its modules are ready now, fix AP's partially initialized
378 * svc protocol and its connection.
379 */
380 intf = gb_ap_interface_create(hd, connection, interface_id);
381 if (!intf) {
382 gb_endo_remove(hd->endo);
383 return ret;
384 }
385
386 return 0;
387}
388
Viresh Kumarbbaca712015-09-23 16:48:08 -0700389static void svc_intf_remove(struct gb_connection *connection,
390 struct gb_interface *intf)
391{
Johan Hovold25376362015-11-03 18:03:23 +0100392 struct gb_host_device *hd = connection->hd;
Viresh Kumarbbaca712015-09-23 16:48:08 -0700393 struct gb_svc *svc = connection->private;
394 u8 intf_id = intf->interface_id;
395 u8 device_id;
396
397 device_id = intf->device_id;
Viresh Kumar80d1ede2015-09-23 16:48:10 -0700398 gb_interface_remove(intf);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700399
400 /*
401 * Destroy the two-way route between the AP and the interface.
402 */
403 gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
404
405 ida_simple_remove(&svc->device_id_map, device_id);
406}
407
Viresh Kumar067906f2015-08-06 12:44:55 +0530408/*
409 * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
410 * returns, irrespective of success or Failure in bringing up the module.
411 */
412static void svc_process_hotplug(struct work_struct *work)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500413{
Viresh Kumar067906f2015-08-06 12:44:55 +0530414 struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
415 work);
416 struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
417 struct gb_connection *connection = svc_hotplug->connection;
418 struct gb_svc *svc = connection->private;
Johan Hovold25376362015-11-03 18:03:23 +0100419 struct gb_host_device *hd = connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530420 struct gb_interface *intf;
421 u8 intf_id, device_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530422 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500423
Alex Elder30c6d9d2015-05-22 13:02:08 -0500424 /*
425 * Grab the information we need.
Viresh Kumar7eb89192015-07-01 12:13:50 +0530426 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500427 intf_id = hotplug->intf_id;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500428
Viresh Kumarbbaca712015-09-23 16:48:08 -0700429 intf = gb_interface_find(hd, intf_id);
430 if (intf) {
431 /*
432 * We have received a hotplug request for an interface that
433 * already exists.
434 *
435 * This can happen in cases like:
436 * - bootrom loading the firmware image and booting into that,
437 * which only generates a hotplug event. i.e. no hot-unplug
438 * event.
439 * - Or the firmware on the module crashed and sent hotplug
440 * request again to the SVC, which got propagated to AP.
441 *
442 * Remove the interface and add it again, and let user know
443 * about this with a print message.
444 */
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700445 pr_info("%d: Removed interface (%hhu) to add it again\n",
446 connection->intf_cport_id, intf_id);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700447 svc_intf_remove(connection, intf);
448 }
449
Viresh Kumaread35462015-07-21 17:44:19 +0530450 intf = gb_interface_create(hd, intf_id);
451 if (!intf) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700452 pr_err("%d: Failed to create interface with id %hhu\n",
453 connection->intf_cport_id, intf_id);
Viresh Kumar067906f2015-08-06 12:44:55 +0530454 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530455 }
456
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700457 ret = gb_svc_read_and_clear_module_boot_status(intf);
458 if (ret)
459 goto destroy_interface;
460
Viresh Kumar3944a452015-08-12 09:19:31 +0530461 intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
462 intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
463 intf->ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id);
464 intf->ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id);
465
Viresh Kumaread35462015-07-21 17:44:19 +0530466 /*
467 * Create a device id for the interface:
468 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
469 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
470 *
471 * XXX Do we need to allocate device ID for SVC or the AP here? And what
472 * XXX about an AP with multiple interface blocks?
473 */
Johan Hovoldc09db182015-09-15 09:18:08 +0200474 device_id = ida_simple_get(&svc->device_id_map,
Johan Hovold89f637f2015-09-01 12:25:25 +0200475 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
Viresh Kumaread35462015-07-21 17:44:19 +0530476 if (device_id < 0) {
477 ret = device_id;
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700478 pr_err("%d: Failed to allocate device id for interface with id %hhu (%d)\n",
479 connection->intf_cport_id, intf_id, ret);
Viresh Kumaread35462015-07-21 17:44:19 +0530480 goto destroy_interface;
481 }
482
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530483 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530484 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700485 pr_err("%d: Device id operation failed, interface %hhu device_id %hhu (%d)\n",
486 connection->intf_cport_id, intf_id, device_id, ret);
Viresh Kumaread35462015-07-21 17:44:19 +0530487 goto ida_put;
488 }
489
Perry Hung7e275462015-07-24 19:02:32 -0400490 /*
491 * Create a two-way route between the AP and the new interface
492 */
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530493 ret = gb_svc_route_create(svc, hd->endo->ap_intf_id, GB_DEVICE_ID_AP,
494 intf_id, device_id);
Perry Hung7e275462015-07-24 19:02:32 -0400495 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700496 pr_err("%d: Route create operation failed, interface %hhu device_id %hhu (%d)\n",
497 connection->intf_cport_id, intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530498 goto svc_id_free;
Perry Hung7e275462015-07-24 19:02:32 -0400499 }
500
Viresh Kumaread35462015-07-21 17:44:19 +0530501 ret = gb_interface_init(intf, device_id);
502 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700503 pr_err("%d: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n",
504 connection->intf_cport_id, intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530505 goto destroy_route;
Viresh Kumaread35462015-07-21 17:44:19 +0530506 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500507
Viresh Kumar067906f2015-08-06 12:44:55 +0530508 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530509
Viresh Kumar0a020572015-09-07 18:05:26 +0530510destroy_route:
511 gb_svc_route_destroy(svc, hd->endo->ap_intf_id, intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530512svc_id_free:
513 /*
514 * XXX Should we tell SVC that this id doesn't belong to interface
515 * XXX anymore.
516 */
517ida_put:
Johan Hovoldc09db182015-09-15 09:18:08 +0200518 ida_simple_remove(&svc->device_id_map, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530519destroy_interface:
Viresh Kumar80d1ede2015-09-23 16:48:10 -0700520 gb_interface_remove(intf);
Viresh Kumar067906f2015-08-06 12:44:55 +0530521free_svc_hotplug:
522 kfree(svc_hotplug);
523}
Viresh Kumaread35462015-07-21 17:44:19 +0530524
Viresh Kumar067906f2015-08-06 12:44:55 +0530525/*
526 * Bringing up a module can be time consuming, as that may require lots of
527 * initialization on the module side. Over that, we may also need to download
528 * the firmware first and flash that on the module.
529 *
530 * In order to make other hotplug events to not wait for all this to finish,
531 * handle most of module hotplug stuff outside of the hotplug callback, with
532 * help of a workqueue.
533 */
534static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
535{
536 struct gb_message *request = op->request;
537 struct svc_hotplug *svc_hotplug;
538
539 if (request->payload_size < sizeof(svc_hotplug->data)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700540 pr_err("%d: short hotplug request received (%zu < %zu)\n",
541 op->connection->intf_cport_id, request->payload_size,
542 sizeof(svc_hotplug->data));
Viresh Kumar067906f2015-08-06 12:44:55 +0530543 return -EINVAL;
544 }
545
Johan Hovold287bba82015-09-01 12:25:26 +0200546 svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL);
Viresh Kumar067906f2015-08-06 12:44:55 +0530547 if (!svc_hotplug)
548 return -ENOMEM;
549
550 svc_hotplug->connection = op->connection;
551 memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data));
552
553 INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
554 queue_work(system_unbound_wq, &svc_hotplug->work);
555
556 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500557}
558
559static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
560{
561 struct gb_message *request = op->request;
Viresh Kumaread35462015-07-21 17:44:19 +0530562 struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload;
Johan Hovold25376362015-11-03 18:03:23 +0100563 struct gb_host_device *hd = op->connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530564 struct gb_interface *intf;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500565 u8 intf_id;
566
567 if (request->payload_size < sizeof(*hot_unplug)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700568 pr_err("connection %d: short hot unplug request received (%zu < %zu)\n",
569 op->connection->intf_cport_id, request->payload_size,
570 sizeof(*hot_unplug));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500571 return -EINVAL;
572 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500573
574 intf_id = hot_unplug->intf_id;
575
Viresh Kumaread35462015-07-21 17:44:19 +0530576 intf = gb_interface_find(hd, intf_id);
577 if (!intf) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700578 pr_err("connection %d: Couldn't find interface for id %hhu\n",
579 op->connection->intf_cport_id, intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530580 return -EINVAL;
581 }
582
Viresh Kumarbbaca712015-09-23 16:48:08 -0700583 svc_intf_remove(op->connection, intf);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500584
585 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500586}
587
588static int gb_svc_intf_reset_recv(struct gb_operation *op)
589{
590 struct gb_message *request = op->request;
591 struct gb_svc_intf_reset_request *reset;
592 u8 intf_id;
593
594 if (request->payload_size < sizeof(*reset)) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700595 pr_err("connection %d: short reset request received (%zu < %zu)\n",
596 op->connection->intf_cport_id, request->payload_size,
597 sizeof(*reset));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500598 return -EINVAL;
599 }
600 reset = request->payload;
601
602 intf_id = reset->intf_id;
603
604 /* FIXME Reset the interface here */
605
606 return 0;
607}
608
609static int gb_svc_request_recv(u8 type, struct gb_operation *op)
610{
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530611 struct gb_connection *connection = op->connection;
612 struct gb_svc *svc = connection->private;
613 int ret = 0;
614
615 /*
616 * SVC requests need to follow a specific order (at least initially) and
617 * below code takes care of enforcing that. The expected order is:
618 * - PROTOCOL_VERSION
619 * - SVC_HELLO
620 * - Any other request, but the earlier two.
621 *
622 * Incoming requests are guaranteed to be serialized and so we don't
623 * need to protect 'state' for any races.
624 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500625 switch (type) {
Viresh Kumar0e2462d2015-08-14 07:57:38 +0530626 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530627 if (svc->state != GB_SVC_STATE_RESET)
628 ret = -EINVAL;
629 break;
Viresh Kumaread35462015-07-21 17:44:19 +0530630 case GB_SVC_TYPE_SVC_HELLO:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530631 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
632 ret = -EINVAL;
633 break;
634 default:
635 if (svc->state != GB_SVC_STATE_SVC_HELLO)
636 ret = -EINVAL;
637 break;
638 }
639
640 if (ret) {
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700641 pr_warn("connection %d: unexpected SVC request 0x%02x received (state %u)\n",
642 connection->intf_cport_id, type, svc->state);
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530643 return ret;
644 }
645
646 switch (type) {
647 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
648 ret = gb_svc_version_request(op);
649 if (!ret)
650 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
651 return ret;
652 case GB_SVC_TYPE_SVC_HELLO:
653 ret = gb_svc_hello(op);
654 if (!ret)
655 svc->state = GB_SVC_STATE_SVC_HELLO;
656 return ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500657 case GB_SVC_TYPE_INTF_HOTPLUG:
658 return gb_svc_intf_hotplug_recv(op);
659 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
660 return gb_svc_intf_hot_unplug_recv(op);
661 case GB_SVC_TYPE_INTF_RESET:
662 return gb_svc_intf_reset_recv(op);
663 default:
Greg Kroah-Hartmanb50a24e2015-10-16 16:53:31 -0700664 pr_err("connection %d: unsupported request: %hhu\n",
665 connection->intf_cport_id, type);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500666 return -EINVAL;
667 }
668}
669
Alex Elder30c6d9d2015-05-22 13:02:08 -0500670static int gb_svc_connection_init(struct gb_connection *connection)
671{
672 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500673
674 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
675 if (!svc)
676 return -ENOMEM;
677
Perry Hung75a60ed2015-07-24 19:02:33 -0400678 connection->hd->svc = svc;
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530679 svc->state = GB_SVC_STATE_RESET;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500680 svc->connection = connection;
681 connection->private = svc;
Viresh Kumard3d44842015-07-21 17:44:18 +0530682
Viresh Kumardcd05002015-07-24 15:32:20 +0530683 WARN_ON(connection->hd->initial_svc_connection);
684 connection->hd->initial_svc_connection = connection;
Viresh Kumard3d44842015-07-21 17:44:18 +0530685
Johan Hovoldc09db182015-09-15 09:18:08 +0200686 ida_init(&svc->device_id_map);
Viresh Kumard3d44842015-07-21 17:44:18 +0530687
Viresh Kumar18d777c2015-07-21 17:44:20 +0530688 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500689}
690
691static void gb_svc_connection_exit(struct gb_connection *connection)
692{
693 struct gb_svc *svc = connection->private;
694
Johan Hovoldc09db182015-09-15 09:18:08 +0200695 ida_destroy(&svc->device_id_map);
Perry Hung75a60ed2015-07-24 19:02:33 -0400696 connection->hd->svc = NULL;
Viresh Kumard3d44842015-07-21 17:44:18 +0530697 connection->private = NULL;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500698 kfree(svc);
699}
700
701static struct gb_protocol svc_protocol = {
702 .name = "svc",
703 .id = GREYBUS_PROTOCOL_SVC,
Viresh Kumar06e305f2015-07-01 12:13:51 +0530704 .major = GB_SVC_VERSION_MAJOR,
705 .minor = GB_SVC_VERSION_MINOR,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500706 .connection_init = gb_svc_connection_init,
707 .connection_exit = gb_svc_connection_exit,
708 .request_recv = gb_svc_request_recv,
Viresh Kumar5a5296b2015-09-07 16:01:24 +0530709 .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
710 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
711 GB_PROTOCOL_NO_BUNDLE |
712 GB_PROTOCOL_SKIP_VERSION |
713 GB_PROTOCOL_SKIP_SVC_CONNECTION,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500714};
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530715gb_builtin_protocol_driver(svc_protocol);