blob: 2c2dd8e66d7d98ad50f01edb3b50abdc2a03d0cf [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
Johan Hovoldf6c6c132015-11-11 10:07:08 +010014#define CPORT_FLAGS_E2EFC BIT(0)
15#define CPORT_FLAGS_CSD_N BIT(1)
16#define CPORT_FLAGS_CSV_N BIT(2)
Perry Hung0b226492015-07-24 19:02:34 -040017
Viresh Kumarb45864d2015-07-24 15:32:21 +053018
Viresh Kumar067906f2015-08-06 12:44:55 +053019struct svc_hotplug {
20 struct work_struct work;
21 struct gb_connection *connection;
22 struct gb_svc_intf_hotplug_request data;
23};
24
Viresh Kumaread35462015-07-21 17:44:19 +053025
Johan Hovold66069fb2015-11-25 15:59:09 +010026static ssize_t endo_id_show(struct device *dev,
27 struct device_attribute *attr, char *buf)
28{
29 struct gb_svc *svc = to_gb_svc(dev);
30
31 return sprintf(buf, "0x%04x\n", svc->endo_id);
32}
33static DEVICE_ATTR_RO(endo_id);
34
35static ssize_t ap_intf_id_show(struct device *dev,
36 struct device_attribute *attr, char *buf)
37{
38 struct gb_svc *svc = to_gb_svc(dev);
39
40 return sprintf(buf, "%u\n", svc->ap_intf_id);
41}
42static DEVICE_ATTR_RO(ap_intf_id);
43
44static struct attribute *svc_attrs[] = {
45 &dev_attr_endo_id.attr,
46 &dev_attr_ap_intf_id.attr,
47 NULL,
48};
49ATTRIBUTE_GROUPS(svc);
50
Viresh Kumar505f16c2015-08-31 17:21:07 +053051static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -050052{
53 struct gb_svc_intf_device_id_request request;
54
55 request.intf_id = intf_id;
56 request.device_id = device_id;
57
58 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
59 &request, sizeof(request), NULL, 0);
60}
61
Viresh Kumar3f0e9182015-08-31 17:21:06 +053062int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -050063{
64 struct gb_svc_intf_reset_request request;
65
66 request.intf_id = intf_id;
67
68 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
69 &request, sizeof(request), NULL, 0);
70}
Viresh Kumar3f0e9182015-08-31 17:21:06 +053071EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
Alex Elder30c6d9d2015-05-22 13:02:08 -050072
Viresh Kumar19151c32015-09-09 21:08:29 +053073int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
74 u32 *value)
75{
76 struct gb_svc_dme_peer_get_request request;
77 struct gb_svc_dme_peer_get_response response;
78 u16 result;
79 int ret;
80
81 request.intf_id = intf_id;
82 request.attr = cpu_to_le16(attr);
83 request.selector = cpu_to_le16(selector);
84
85 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
86 &request, sizeof(request),
87 &response, sizeof(response));
88 if (ret) {
Johan Hovold684156a2015-11-25 15:59:19 +010089 dev_err(&svc->dev, "failed to get DME attribute (%hhu %hx %hu): %d\n",
90 intf_id, attr, selector, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +053091 return ret;
92 }
93
94 result = le16_to_cpu(response.result_code);
95 if (result) {
Johan Hovold684156a2015-11-25 15:59:19 +010096 dev_err(&svc->dev, "UniPro error while getting DME attribute (%hhu %hx %hu): %hu\n",
97 intf_id, attr, selector, result);
Viresh Kumar19151c32015-09-09 21:08:29 +053098 return -EINVAL;
99 }
100
101 if (value)
102 *value = le32_to_cpu(response.attr_value);
103
104 return 0;
105}
106EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
107
108int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
109 u32 value)
110{
111 struct gb_svc_dme_peer_set_request request;
112 struct gb_svc_dme_peer_set_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 request.value = cpu_to_le32(value);
120
121 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
122 &request, sizeof(request),
123 &response, sizeof(response));
124 if (ret) {
Johan Hovold684156a2015-11-25 15:59:19 +0100125 dev_err(&svc->dev, "failed to set DME attribute (%hhu %hx %hu %u): %d\n",
126 intf_id, attr, selector, value, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530127 return ret;
128 }
129
130 result = le16_to_cpu(response.result_code);
131 if (result) {
Johan Hovold684156a2015-11-25 15:59:19 +0100132 dev_err(&svc->dev, "UniPro error while setting DME attribute (%hhu %hx %hu %u): %hu\n",
133 intf_id, attr, selector, value, result);
Viresh Kumar19151c32015-09-09 21:08:29 +0530134 return -EINVAL;
135 }
136
137 return 0;
138}
139EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
140
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700141/*
142 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
143 * status attribute. AP needs to read and clear it, after reading a non-zero
144 * value from it.
145 *
146 * FIXME: This is module-hardware dependent and needs to be extended for every
147 * type of module we want to support.
148 */
149static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
150{
Johan Hovold25376362015-11-03 18:03:23 +0100151 struct gb_host_device *hd = intf->hd;
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700152 int ret;
153 u32 value;
154
155 /* Read and clear boot status in T_TstSrcIncrement */
156 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id,
157 DME_ATTR_T_TST_SRC_INCREMENT,
158 DME_ATTR_SELECTOR_INDEX, &value);
159
160 if (ret)
161 return ret;
162
163 /*
164 * A nonzero boot status indicates the module has finished
165 * booting. Clear it.
166 */
167 if (!value) {
168 dev_err(&intf->dev, "Module not ready yet\n");
169 return -ENODEV;
170 }
171
Viresh Kumar1575ef12015-10-07 15:40:24 -0400172 /*
173 * Check if the module needs to boot from unipro.
174 * For ES2: We need to check lowest 8 bits of 'value'.
175 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
176 *
177 * FIXME: Add code to find if we are on ES2 or ES3 to have separate
178 * checks.
179 */
180 if (value == DME_TSI_UNIPRO_BOOT_STARTED ||
181 value == DME_TSI_FALLBACK_UNIPRO_BOOT_STARTED)
182 intf->boot_over_unipro = true;
183
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700184 return gb_svc_dme_peer_set(hd->svc, intf->interface_id,
185 DME_ATTR_T_TST_SRC_INCREMENT,
186 DME_ATTR_SELECTOR_INDEX, 0);
187}
188
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530189int gb_svc_connection_create(struct gb_svc *svc,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500190 u8 intf1_id, u16 cport1_id,
Viresh Kumar1575ef12015-10-07 15:40:24 -0400191 u8 intf2_id, u16 cport2_id,
192 bool boot_over_unipro)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500193{
194 struct gb_svc_conn_create_request request;
195
196 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100197 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500198 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100199 request.cport2_id = cpu_to_le16(cport2_id);
Perry Hung0b226492015-07-24 19:02:34 -0400200 /*
201 * XXX: fix connections paramaters to TC0 and all CPort flags
202 * for now.
203 */
204 request.tc = 0;
Viresh Kumar1575ef12015-10-07 15:40:24 -0400205
206 /*
207 * We need to skip setting E2EFC and other flags to the connection
208 * create request, for all cports, on an interface that need to boot
209 * over unipro, i.e. interfaces required to download firmware.
210 */
211 if (boot_over_unipro)
212 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
213 else
214 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500215
216 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
217 &request, sizeof(request), NULL, 0);
218}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530219EXPORT_SYMBOL_GPL(gb_svc_connection_create);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500220
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530221void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
222 u8 intf2_id, u16 cport2_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500223{
224 struct gb_svc_conn_destroy_request request;
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530225 struct gb_connection *connection = svc->connection;
226 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500227
228 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100229 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500230 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100231 request.cport2_id = cpu_to_le16(cport2_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500232
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530233 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
234 &request, sizeof(request), NULL, 0);
Johan Hovold684156a2015-11-25 15:59:19 +0100235 if (ret) {
236 dev_err(&svc->dev, "failed to destroy connection (%hhu:%hu %hhu:%hu): %d\n",
237 intf1_id, cport1_id, intf2_id, cport2_id, ret);
238 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500239}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530240EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500241
Viresh Kumarbb106852015-09-07 16:01:25 +0530242/* Creates bi-directional routes between the devices */
Viresh Kumar505f16c2015-08-31 17:21:07 +0530243static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
244 u8 intf2_id, u8 dev2_id)
Perry Hunge08aaa42015-07-24 19:02:31 -0400245{
246 struct gb_svc_route_create_request request;
247
248 request.intf1_id = intf1_id;
249 request.dev1_id = dev1_id;
250 request.intf2_id = intf2_id;
251 request.dev2_id = dev2_id;
252
253 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
254 &request, sizeof(request), NULL, 0);
255}
Perry Hunge08aaa42015-07-24 19:02:31 -0400256
Viresh Kumar0a020572015-09-07 18:05:26 +0530257/* Destroys bi-directional routes between the devices */
258static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
259{
260 struct gb_svc_route_destroy_request request;
261 int ret;
262
263 request.intf1_id = intf1_id;
264 request.intf2_id = intf2_id;
265
266 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
267 &request, sizeof(request), NULL, 0);
Johan Hovold684156a2015-11-25 15:59:19 +0100268 if (ret) {
269 dev_err(&svc->dev, "failed to destroy route (%hhu %hhu): %d\n",
270 intf1_id, intf2_id, ret);
271 }
Viresh Kumar0a020572015-09-07 18:05:26 +0530272}
273
Viresh Kumaread35462015-07-21 17:44:19 +0530274static int gb_svc_version_request(struct gb_operation *op)
275{
276 struct gb_connection *connection = op->connection;
Johan Hovold684156a2015-11-25 15:59:19 +0100277 struct gb_svc *svc = connection->private;
Johan Hovoldcfb16902015-09-15 10:48:01 +0200278 struct gb_protocol_version_request *request;
279 struct gb_protocol_version_response *response;
Viresh Kumaread35462015-07-21 17:44:19 +0530280
Johan Hovold55510842015-11-19 18:28:01 +0100281 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100282 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
Johan Hovold55510842015-11-19 18:28:01 +0100283 op->request->payload_size,
284 sizeof(*request));
285 return -EINVAL;
286 }
287
Johan Hovoldcfb16902015-09-15 10:48:01 +0200288 request = op->request->payload;
Viresh Kumaread35462015-07-21 17:44:19 +0530289
Johan Hovoldcfb16902015-09-15 10:48:01 +0200290 if (request->major > GB_SVC_VERSION_MAJOR) {
Johan Hovold684156a2015-11-25 15:59:19 +0100291 dev_warn(&svc->dev, "unsupported major version (%hhu > %hhu)\n",
292 request->major, GB_SVC_VERSION_MAJOR);
Viresh Kumaread35462015-07-21 17:44:19 +0530293 return -ENOTSUPP;
294 }
295
Johan Hovoldcfb16902015-09-15 10:48:01 +0200296 connection->module_major = request->major;
297 connection->module_minor = request->minor;
Viresh Kumar3ea959e32015-08-11 07:36:14 +0530298
Johan Hovold684156a2015-11-25 15:59:19 +0100299 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
Viresh Kumaread35462015-07-21 17:44:19 +0530300 return -ENOMEM;
Viresh Kumaread35462015-07-21 17:44:19 +0530301
Johan Hovoldcfb16902015-09-15 10:48:01 +0200302 response = op->response->payload;
303 response->major = connection->module_major;
304 response->minor = connection->module_minor;
Johan Hovold59832932015-09-15 10:48:00 +0200305
Viresh Kumaread35462015-07-21 17:44:19 +0530306 return 0;
307}
308
309static int gb_svc_hello(struct gb_operation *op)
310{
311 struct gb_connection *connection = op->connection;
Johan Hovold88f7b962015-11-25 15:59:08 +0100312 struct gb_svc *svc = connection->private;
Viresh Kumaread35462015-07-21 17:44:19 +0530313 struct gb_svc_hello_request *hello_request;
Viresh Kumaread35462015-07-21 17:44:19 +0530314 int ret;
315
Viresh Kumar0c32d2a2015-08-11 07:29:19 +0530316 if (op->request->payload_size < sizeof(*hello_request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100317 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
318 op->request->payload_size,
319 sizeof(*hello_request));
Viresh Kumaread35462015-07-21 17:44:19 +0530320 return -EINVAL;
321 }
322
323 hello_request = op->request->payload;
Johan Hovold66069fb2015-11-25 15:59:09 +0100324 svc->endo_id = le16_to_cpu(hello_request->endo_id);
325 svc->ap_intf_id = hello_request->interface_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530326
Johan Hovold88f7b962015-11-25 15:59:08 +0100327 ret = device_add(&svc->dev);
328 if (ret) {
329 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
330 return ret;
331 }
332
Viresh Kumaread35462015-07-21 17:44:19 +0530333 return 0;
334}
335
Viresh Kumarbbaca712015-09-23 16:48:08 -0700336static void svc_intf_remove(struct gb_connection *connection,
337 struct gb_interface *intf)
338{
Viresh Kumarbbaca712015-09-23 16:48:08 -0700339 struct gb_svc *svc = connection->private;
340 u8 intf_id = intf->interface_id;
341 u8 device_id;
342
343 device_id = intf->device_id;
Viresh Kumar80d1ede2015-09-23 16:48:10 -0700344 gb_interface_remove(intf);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700345
346 /*
347 * Destroy the two-way route between the AP and the interface.
348 */
Johan Hovold66069fb2015-11-25 15:59:09 +0100349 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700350
351 ida_simple_remove(&svc->device_id_map, device_id);
352}
353
Viresh Kumar067906f2015-08-06 12:44:55 +0530354/*
355 * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it
356 * returns, irrespective of success or Failure in bringing up the module.
357 */
358static void svc_process_hotplug(struct work_struct *work)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500359{
Viresh Kumar067906f2015-08-06 12:44:55 +0530360 struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug,
361 work);
362 struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data;
363 struct gb_connection *connection = svc_hotplug->connection;
364 struct gb_svc *svc = connection->private;
Johan Hovold25376362015-11-03 18:03:23 +0100365 struct gb_host_device *hd = connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530366 struct gb_interface *intf;
367 u8 intf_id, device_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530368 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500369
Alex Elder30c6d9d2015-05-22 13:02:08 -0500370 /*
371 * Grab the information we need.
Viresh Kumar7eb89192015-07-01 12:13:50 +0530372 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500373 intf_id = hotplug->intf_id;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500374
Viresh Kumarbbaca712015-09-23 16:48:08 -0700375 intf = gb_interface_find(hd, intf_id);
376 if (intf) {
377 /*
378 * We have received a hotplug request for an interface that
379 * already exists.
380 *
381 * This can happen in cases like:
382 * - bootrom loading the firmware image and booting into that,
383 * which only generates a hotplug event. i.e. no hot-unplug
384 * event.
385 * - Or the firmware on the module crashed and sent hotplug
386 * request again to the SVC, which got propagated to AP.
387 *
388 * Remove the interface and add it again, and let user know
389 * about this with a print message.
390 */
Johan Hovold684156a2015-11-25 15:59:19 +0100391 dev_info(&svc->dev, "removing interface %hhu to add it again\n",
392 intf_id);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700393 svc_intf_remove(connection, intf);
394 }
395
Viresh Kumaread35462015-07-21 17:44:19 +0530396 intf = gb_interface_create(hd, intf_id);
397 if (!intf) {
Johan Hovold684156a2015-11-25 15:59:19 +0100398 dev_err(&svc->dev, "failed to create interface %hhu\n",
399 intf_id);
Viresh Kumar067906f2015-08-06 12:44:55 +0530400 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530401 }
402
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700403 ret = gb_svc_read_and_clear_module_boot_status(intf);
404 if (ret)
405 goto destroy_interface;
406
Viresh Kumar3944a452015-08-12 09:19:31 +0530407 intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id);
408 intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id);
Johan Hovold9f592632015-11-25 15:58:56 +0100409 intf->vendor_id = le32_to_cpu(hotplug->data.ara_vend_id);
410 intf->product_id = le32_to_cpu(hotplug->data.ara_prod_id);
Viresh Kumar3944a452015-08-12 09:19:31 +0530411
Viresh Kumaread35462015-07-21 17:44:19 +0530412 /*
413 * Create a device id for the interface:
414 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
415 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
416 *
417 * XXX Do we need to allocate device ID for SVC or the AP here? And what
418 * XXX about an AP with multiple interface blocks?
419 */
Johan Hovoldc09db182015-09-15 09:18:08 +0200420 device_id = ida_simple_get(&svc->device_id_map,
Johan Hovold89f637f2015-09-01 12:25:25 +0200421 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
Viresh Kumaread35462015-07-21 17:44:19 +0530422 if (device_id < 0) {
423 ret = device_id;
Johan Hovold684156a2015-11-25 15:59:19 +0100424 dev_err(&svc->dev, "failed to allocate device id for interface %hhu: %d\n",
425 intf_id, ret);
Viresh Kumaread35462015-07-21 17:44:19 +0530426 goto destroy_interface;
427 }
428
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530429 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530430 if (ret) {
Johan Hovold684156a2015-11-25 15:59:19 +0100431 dev_err(&svc->dev, "failed to set device id %hhu for interface %hhu: %d\n",
432 device_id, intf_id, ret);
Viresh Kumaread35462015-07-21 17:44:19 +0530433 goto ida_put;
434 }
435
Perry Hung7e275462015-07-24 19:02:32 -0400436 /*
437 * Create a two-way route between the AP and the new interface
438 */
Johan Hovold66069fb2015-11-25 15:59:09 +0100439 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530440 intf_id, device_id);
Perry Hung7e275462015-07-24 19:02:32 -0400441 if (ret) {
Johan Hovold684156a2015-11-25 15:59:19 +0100442 dev_err(&svc->dev, "failed to create route to interface %hhu (device id %hhu): %d\n",
443 intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530444 goto svc_id_free;
Perry Hung7e275462015-07-24 19:02:32 -0400445 }
446
Viresh Kumaread35462015-07-21 17:44:19 +0530447 ret = gb_interface_init(intf, device_id);
448 if (ret) {
Johan Hovold684156a2015-11-25 15:59:19 +0100449 dev_err(&svc->dev, "failed to initialize interface %hhu (device id %hhu): %d\n",
450 intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530451 goto destroy_route;
Viresh Kumaread35462015-07-21 17:44:19 +0530452 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500453
Viresh Kumar067906f2015-08-06 12:44:55 +0530454 goto free_svc_hotplug;
Viresh Kumaread35462015-07-21 17:44:19 +0530455
Viresh Kumar0a020572015-09-07 18:05:26 +0530456destroy_route:
Johan Hovold66069fb2015-11-25 15:59:09 +0100457 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530458svc_id_free:
459 /*
460 * XXX Should we tell SVC that this id doesn't belong to interface
461 * XXX anymore.
462 */
463ida_put:
Johan Hovoldc09db182015-09-15 09:18:08 +0200464 ida_simple_remove(&svc->device_id_map, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530465destroy_interface:
Viresh Kumar80d1ede2015-09-23 16:48:10 -0700466 gb_interface_remove(intf);
Viresh Kumar067906f2015-08-06 12:44:55 +0530467free_svc_hotplug:
468 kfree(svc_hotplug);
469}
Viresh Kumaread35462015-07-21 17:44:19 +0530470
Viresh Kumar067906f2015-08-06 12:44:55 +0530471/*
472 * Bringing up a module can be time consuming, as that may require lots of
473 * initialization on the module side. Over that, we may also need to download
474 * the firmware first and flash that on the module.
475 *
476 * In order to make other hotplug events to not wait for all this to finish,
477 * handle most of module hotplug stuff outside of the hotplug callback, with
478 * help of a workqueue.
479 */
480static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
481{
Johan Hovold684156a2015-11-25 15:59:19 +0100482 struct gb_svc *svc = op->connection->private;
Johan Hovoldd34a3642015-12-02 18:23:26 +0100483 struct gb_svc_intf_hotplug_request *request;
Viresh Kumar067906f2015-08-06 12:44:55 +0530484 struct svc_hotplug *svc_hotplug;
485
Johan Hovoldd34a3642015-12-02 18:23:26 +0100486 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100487 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
Johan Hovoldd34a3642015-12-02 18:23:26 +0100488 op->request->payload_size, sizeof(*request));
Viresh Kumar067906f2015-08-06 12:44:55 +0530489 return -EINVAL;
490 }
491
Johan Hovoldd34a3642015-12-02 18:23:26 +0100492 request = op->request->payload;
493
494 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
495
Johan Hovold287bba82015-09-01 12:25:26 +0200496 svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL);
Viresh Kumar067906f2015-08-06 12:44:55 +0530497 if (!svc_hotplug)
498 return -ENOMEM;
499
500 svc_hotplug->connection = op->connection;
Johan Hovoldd34a3642015-12-02 18:23:26 +0100501 memcpy(&svc_hotplug->data, request, sizeof(svc_hotplug->data));
Viresh Kumar067906f2015-08-06 12:44:55 +0530502
503 INIT_WORK(&svc_hotplug->work, svc_process_hotplug);
504 queue_work(system_unbound_wq, &svc_hotplug->work);
505
506 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500507}
508
509static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
510{
Johan Hovold684156a2015-11-25 15:59:19 +0100511 struct gb_svc *svc = op->connection->private;
Johan Hovoldd34a3642015-12-02 18:23:26 +0100512 struct gb_svc_intf_hot_unplug_request *request;
Johan Hovold25376362015-11-03 18:03:23 +0100513 struct gb_host_device *hd = op->connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530514 struct gb_interface *intf;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500515 u8 intf_id;
516
Johan Hovoldd34a3642015-12-02 18:23:26 +0100517 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100518 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
Johan Hovoldd34a3642015-12-02 18:23:26 +0100519 op->request->payload_size, sizeof(*request));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500520 return -EINVAL;
521 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500522
Johan Hovoldd34a3642015-12-02 18:23:26 +0100523 request = op->request->payload;
524 intf_id = request->intf_id;
525
526 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500527
Viresh Kumaread35462015-07-21 17:44:19 +0530528 intf = gb_interface_find(hd, intf_id);
529 if (!intf) {
Johan Hovold684156a2015-11-25 15:59:19 +0100530 dev_warn(&svc->dev, "could not find hot-unplug interface %hhu\n",
531 intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530532 return -EINVAL;
533 }
534
Viresh Kumarbbaca712015-09-23 16:48:08 -0700535 svc_intf_remove(op->connection, intf);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500536
537 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500538}
539
540static int gb_svc_intf_reset_recv(struct gb_operation *op)
541{
Johan Hovold684156a2015-11-25 15:59:19 +0100542 struct gb_svc *svc = op->connection->private;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500543 struct gb_message *request = op->request;
544 struct gb_svc_intf_reset_request *reset;
545 u8 intf_id;
546
547 if (request->payload_size < sizeof(*reset)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100548 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
549 request->payload_size, sizeof(*reset));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500550 return -EINVAL;
551 }
552 reset = request->payload;
553
554 intf_id = reset->intf_id;
555
556 /* FIXME Reset the interface here */
557
558 return 0;
559}
560
561static int gb_svc_request_recv(u8 type, struct gb_operation *op)
562{
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530563 struct gb_connection *connection = op->connection;
564 struct gb_svc *svc = connection->private;
565 int ret = 0;
566
567 /*
568 * SVC requests need to follow a specific order (at least initially) and
569 * below code takes care of enforcing that. The expected order is:
570 * - PROTOCOL_VERSION
571 * - SVC_HELLO
572 * - Any other request, but the earlier two.
573 *
574 * Incoming requests are guaranteed to be serialized and so we don't
575 * need to protect 'state' for any races.
576 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500577 switch (type) {
Viresh Kumar0e2462d2015-08-14 07:57:38 +0530578 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530579 if (svc->state != GB_SVC_STATE_RESET)
580 ret = -EINVAL;
581 break;
Viresh Kumaread35462015-07-21 17:44:19 +0530582 case GB_SVC_TYPE_SVC_HELLO:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530583 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
584 ret = -EINVAL;
585 break;
586 default:
587 if (svc->state != GB_SVC_STATE_SVC_HELLO)
588 ret = -EINVAL;
589 break;
590 }
591
592 if (ret) {
Johan Hovold684156a2015-11-25 15:59:19 +0100593 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
594 type, svc->state);
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530595 return ret;
596 }
597
598 switch (type) {
599 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
600 ret = gb_svc_version_request(op);
601 if (!ret)
602 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
603 return ret;
604 case GB_SVC_TYPE_SVC_HELLO:
605 ret = gb_svc_hello(op);
606 if (!ret)
607 svc->state = GB_SVC_STATE_SVC_HELLO;
608 return ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500609 case GB_SVC_TYPE_INTF_HOTPLUG:
610 return gb_svc_intf_hotplug_recv(op);
611 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
612 return gb_svc_intf_hot_unplug_recv(op);
613 case GB_SVC_TYPE_INTF_RESET:
614 return gb_svc_intf_reset_recv(op);
615 default:
Johan Hovold684156a2015-11-25 15:59:19 +0100616 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500617 return -EINVAL;
618 }
619}
620
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100621static void gb_svc_release(struct device *dev)
622{
Johan Hovold88f7b962015-11-25 15:59:08 +0100623 struct gb_svc *svc = to_gb_svc(dev);
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100624
625 ida_destroy(&svc->device_id_map);
626 kfree(svc);
627}
628
629struct device_type greybus_svc_type = {
630 .name = "greybus_svc",
631 .release = gb_svc_release,
632};
633
Alex Elder30c6d9d2015-05-22 13:02:08 -0500634static int gb_svc_connection_init(struct gb_connection *connection)
635{
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100636 struct gb_host_device *hd = connection->hd;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500637 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500638
639 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
640 if (!svc)
641 return -ENOMEM;
642
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100643 svc->dev.parent = &hd->dev;
644 svc->dev.bus = &greybus_bus_type;
645 svc->dev.type = &greybus_svc_type;
Johan Hovold66069fb2015-11-25 15:59:09 +0100646 svc->dev.groups = svc_groups;
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100647 svc->dev.dma_mask = svc->dev.parent->dma_mask;
648 device_initialize(&svc->dev);
649
650 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
651
Johan Hovold6106e512015-11-25 15:59:07 +0100652 ida_init(&svc->device_id_map);
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530653 svc->state = GB_SVC_STATE_RESET;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500654 svc->connection = connection;
655 connection->private = svc;
Viresh Kumard3d44842015-07-21 17:44:18 +0530656
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100657 hd->svc = svc;
658
Viresh Kumar18d777c2015-07-21 17:44:20 +0530659 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500660}
661
662static void gb_svc_connection_exit(struct gb_connection *connection)
663{
664 struct gb_svc *svc = connection->private;
665
Johan Hovold88f7b962015-11-25 15:59:08 +0100666 if (device_is_registered(&svc->dev))
667 device_del(&svc->dev);
668
Perry Hung75a60ed2015-07-24 19:02:33 -0400669 connection->hd->svc = NULL;
Viresh Kumard3d44842015-07-21 17:44:18 +0530670 connection->private = NULL;
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100671
672 put_device(&svc->dev);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500673}
674
675static struct gb_protocol svc_protocol = {
676 .name = "svc",
677 .id = GREYBUS_PROTOCOL_SVC,
Viresh Kumar06e305f2015-07-01 12:13:51 +0530678 .major = GB_SVC_VERSION_MAJOR,
679 .minor = GB_SVC_VERSION_MINOR,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500680 .connection_init = gb_svc_connection_init,
681 .connection_exit = gb_svc_connection_exit,
682 .request_recv = gb_svc_request_recv,
Viresh Kumar5a5296b2015-09-07 16:01:24 +0530683 .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
684 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
Johan Hovold4ec15742015-11-25 15:59:13 +0100685 GB_PROTOCOL_SKIP_VERSION,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500686};
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530687gb_builtin_protocol_driver(svc_protocol);