greybus: svc: Set interface's hotplug attributes before using them
[cascardo/linux.git] / drivers / staging / greybus / svc.c
1 /*
2  * SVC Greybus driver.
3  *
4  * Copyright 2015 Google Inc.
5  * Copyright 2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/workqueue.h>
11
12 #include "greybus.h"
13
14 #define CPORT_FLAGS_E2EFC       BIT(0)
15 #define CPORT_FLAGS_CSD_N       BIT(1)
16 #define CPORT_FLAGS_CSV_N       BIT(2)
17
18
19 struct gb_svc_deferred_request {
20         struct work_struct work;
21         struct gb_operation *operation;
22 };
23
24
25 static ssize_t endo_id_show(struct device *dev,
26                         struct device_attribute *attr, char *buf)
27 {
28         struct gb_svc *svc = to_gb_svc(dev);
29
30         return sprintf(buf, "0x%04x\n", svc->endo_id);
31 }
32 static DEVICE_ATTR_RO(endo_id);
33
34 static ssize_t ap_intf_id_show(struct device *dev,
35                         struct device_attribute *attr, char *buf)
36 {
37         struct gb_svc *svc = to_gb_svc(dev);
38
39         return sprintf(buf, "%u\n", svc->ap_intf_id);
40 }
41 static DEVICE_ATTR_RO(ap_intf_id);
42
43 static struct attribute *svc_attrs[] = {
44         &dev_attr_endo_id.attr,
45         &dev_attr_ap_intf_id.attr,
46         NULL,
47 };
48 ATTRIBUTE_GROUPS(svc);
49
50 static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
51 {
52         struct gb_svc_intf_device_id_request request;
53
54         request.intf_id = intf_id;
55         request.device_id = device_id;
56
57         return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
58                                  &request, sizeof(request), NULL, 0);
59 }
60
61 int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
62 {
63         struct gb_svc_intf_reset_request request;
64
65         request.intf_id = intf_id;
66
67         return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
68                                  &request, sizeof(request), NULL, 0);
69 }
70 EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
71
72 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
73                         u32 *value)
74 {
75         struct gb_svc_dme_peer_get_request request;
76         struct gb_svc_dme_peer_get_response response;
77         u16 result;
78         int ret;
79
80         request.intf_id = intf_id;
81         request.attr = cpu_to_le16(attr);
82         request.selector = cpu_to_le16(selector);
83
84         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
85                                 &request, sizeof(request),
86                                 &response, sizeof(response));
87         if (ret) {
88                 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
89                                 intf_id, attr, selector, ret);
90                 return ret;
91         }
92
93         result = le16_to_cpu(response.result_code);
94         if (result) {
95                 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
96                                 intf_id, attr, selector, result);
97                 return -EIO;
98         }
99
100         if (value)
101                 *value = le32_to_cpu(response.attr_value);
102
103         return 0;
104 }
105 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
106
107 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
108                         u32 value)
109 {
110         struct gb_svc_dme_peer_set_request request;
111         struct gb_svc_dme_peer_set_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         request.value = cpu_to_le32(value);
119
120         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
121                                 &request, sizeof(request),
122                                 &response, sizeof(response));
123         if (ret) {
124                 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
125                                 intf_id, attr, selector, value, ret);
126                 return ret;
127         }
128
129         result = le16_to_cpu(response.result_code);
130         if (result) {
131                 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
132                                 intf_id, attr, selector, value, result);
133                 return -EIO;
134         }
135
136         return 0;
137 }
138 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
139
140 /*
141  * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
142  * status attribute ES3_INIT_STATUS. AP needs to read and clear it, after
143  * reading a non-zero value from it.
144  *
145  * FIXME: This is module-hardware dependent and needs to be extended for every
146  * type of module we want to support.
147  */
148 static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
149 {
150         struct gb_host_device *hd = intf->hd;
151         int ret;
152         u32 value;
153         u16 attr;
154         u8 init_status;
155
156         /*
157          * Check if the module is ES2 or ES3, and choose attr number
158          * appropriately.
159          * FIXME: Remove ES2 support from the kernel entirely.
160          */
161         if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
162                                 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID)
163                 attr = DME_ATTR_T_TST_SRC_INCREMENT;
164         else
165                 attr = DME_ATTR_ES3_INIT_STATUS;
166
167         /* Read and clear boot status in ES3_INIT_STATUS */
168         ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
169                                   DME_ATTR_SELECTOR_INDEX, &value);
170
171         if (ret)
172                 return ret;
173
174         /*
175          * A nonzero boot status indicates the module has finished
176          * booting. Clear it.
177          */
178         if (!value) {
179                 dev_err(&intf->dev, "Module not ready yet\n");
180                 return -ENODEV;
181         }
182
183         /*
184          * Check if the module needs to boot from UniPro.
185          * For ES2: We need to check lowest 8 bits of 'value'.
186          * For ES3: We need to check highest 8 bits out of 32 of 'value'.
187          * FIXME: Remove ES2 support from the kernel entirely.
188          */
189         if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
190                                 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID)
191                 init_status = value;
192         else
193                 init_status = value >> 24;
194
195         if (init_status == DME_DIS_UNIPRO_BOOT_STARTED ||
196                                 init_status == DME_DIS_FALLBACK_UNIPRO_BOOT_STARTED)
197                 intf->boot_over_unipro = true;
198
199         return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
200                                    DME_ATTR_SELECTOR_INDEX, 0);
201 }
202
203 int gb_svc_connection_create(struct gb_svc *svc,
204                                 u8 intf1_id, u16 cport1_id,
205                                 u8 intf2_id, u16 cport2_id,
206                                 bool boot_over_unipro)
207 {
208         struct gb_svc_conn_create_request request;
209
210         request.intf1_id = intf1_id;
211         request.cport1_id = cpu_to_le16(cport1_id);
212         request.intf2_id = intf2_id;
213         request.cport2_id = cpu_to_le16(cport2_id);
214         /*
215          * XXX: fix connections paramaters to TC0 and all CPort flags
216          * for now.
217          */
218         request.tc = 0;
219
220         /*
221          * We need to skip setting E2EFC and other flags to the connection
222          * create request, for all cports, on an interface that need to boot
223          * over unipro, i.e. interfaces required to download firmware.
224          */
225         if (boot_over_unipro)
226                 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
227         else
228                 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
229
230         return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
231                                  &request, sizeof(request), NULL, 0);
232 }
233 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
234
235 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
236                                u8 intf2_id, u16 cport2_id)
237 {
238         struct gb_svc_conn_destroy_request request;
239         struct gb_connection *connection = svc->connection;
240         int ret;
241
242         request.intf1_id = intf1_id;
243         request.cport1_id = cpu_to_le16(cport1_id);
244         request.intf2_id = intf2_id;
245         request.cport2_id = cpu_to_le16(cport2_id);
246
247         ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
248                                 &request, sizeof(request), NULL, 0);
249         if (ret) {
250                 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
251                                 intf1_id, cport1_id, intf2_id, cport2_id, ret);
252         }
253 }
254 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
255
256 /* Creates bi-directional routes between the devices */
257 static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
258                                u8 intf2_id, u8 dev2_id)
259 {
260         struct gb_svc_route_create_request request;
261
262         request.intf1_id = intf1_id;
263         request.dev1_id = dev1_id;
264         request.intf2_id = intf2_id;
265         request.dev2_id = dev2_id;
266
267         return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
268                                  &request, sizeof(request), NULL, 0);
269 }
270
271 /* Destroys bi-directional routes between the devices */
272 static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
273 {
274         struct gb_svc_route_destroy_request request;
275         int ret;
276
277         request.intf1_id = intf1_id;
278         request.intf2_id = intf2_id;
279
280         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
281                                 &request, sizeof(request), NULL, 0);
282         if (ret) {
283                 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
284                                 intf1_id, intf2_id, ret);
285         }
286 }
287
288 int gb_svc_link_config(struct gb_svc *svc, u8 intf_id,
289                        unsigned int burst, unsigned int gear,
290                        unsigned int nlanes, unsigned int flags)
291 {
292         struct gb_svc_link_config_request request;
293
294         request.intf_id = intf_id;
295         request.burst = burst;
296         request.gear = gear;
297         request.nlanes = nlanes;
298         request.flags = flags;
299
300         return gb_operation_sync(svc->connection, GB_SVC_TYPE_LINK_CONFIG,
301                                  &request, sizeof(request), NULL, 0);
302 }
303 EXPORT_SYMBOL_GPL(gb_svc_link_config);
304
305 static int gb_svc_version_request(struct gb_operation *op)
306 {
307         struct gb_connection *connection = op->connection;
308         struct gb_svc *svc = connection->private;
309         struct gb_protocol_version_request *request;
310         struct gb_protocol_version_response *response;
311
312         if (op->request->payload_size < sizeof(*request)) {
313                 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
314                                 op->request->payload_size,
315                                 sizeof(*request));
316                 return -EINVAL;
317         }
318
319         request = op->request->payload;
320
321         if (request->major > GB_SVC_VERSION_MAJOR) {
322                 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
323                                 request->major, GB_SVC_VERSION_MAJOR);
324                 return -ENOTSUPP;
325         }
326
327         connection->module_major = request->major;
328         connection->module_minor = request->minor;
329
330         if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
331                 return -ENOMEM;
332
333         response = op->response->payload;
334         response->major = connection->module_major;
335         response->minor = connection->module_minor;
336
337         return 0;
338 }
339
340 static int gb_svc_hello(struct gb_operation *op)
341 {
342         struct gb_connection *connection = op->connection;
343         struct gb_svc *svc = connection->private;
344         struct gb_svc_hello_request *hello_request;
345         int ret;
346
347         if (op->request->payload_size < sizeof(*hello_request)) {
348                 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
349                                 op->request->payload_size,
350                                 sizeof(*hello_request));
351                 return -EINVAL;
352         }
353
354         hello_request = op->request->payload;
355         svc->endo_id = le16_to_cpu(hello_request->endo_id);
356         svc->ap_intf_id = hello_request->interface_id;
357
358         ret = device_add(&svc->dev);
359         if (ret) {
360                 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
361                 return ret;
362         }
363
364         return 0;
365 }
366
367 static void gb_svc_intf_remove(struct gb_svc *svc, struct gb_interface *intf)
368 {
369         u8 intf_id = intf->interface_id;
370         u8 device_id = intf->device_id;
371
372         intf->disconnected = true;
373
374         gb_interface_remove(intf);
375
376         /*
377          * Destroy the two-way route between the AP and the interface.
378          */
379         gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
380
381         ida_simple_remove(&svc->device_id_map, device_id);
382 }
383
384 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
385 {
386         struct gb_svc_intf_hotplug_request *request;
387         struct gb_connection *connection = operation->connection;
388         struct gb_svc *svc = connection->private;
389         struct gb_host_device *hd = connection->hd;
390         struct gb_interface *intf;
391         u8 intf_id, device_id;
392         int ret;
393
394         /* The request message size has already been verified. */
395         request = operation->request->payload;
396         intf_id = request->intf_id;
397
398         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
399
400         intf = gb_interface_find(hd, intf_id);
401         if (intf) {
402                 /*
403                  * We have received a hotplug request for an interface that
404                  * already exists.
405                  *
406                  * This can happen in cases like:
407                  * - bootrom loading the firmware image and booting into that,
408                  *   which only generates a hotplug event. i.e. no hot-unplug
409                  *   event.
410                  * - Or the firmware on the module crashed and sent hotplug
411                  *   request again to the SVC, which got propagated to AP.
412                  *
413                  * Remove the interface and add it again, and let user know
414                  * about this with a print message.
415                  */
416                 dev_info(&svc->dev, "removing interface %u to add it again\n",
417                                 intf_id);
418                 gb_svc_intf_remove(svc, intf);
419         }
420
421         intf = gb_interface_create(hd, intf_id);
422         if (!intf) {
423                 dev_err(&svc->dev, "failed to create interface %u\n",
424                                 intf_id);
425                 return;
426         }
427
428         intf->ddbl1_manufacturer_id = le32_to_cpu(request->data.ddbl1_mfr_id);
429         intf->ddbl1_product_id = le32_to_cpu(request->data.ddbl1_prod_id);
430         intf->vendor_id = le32_to_cpu(request->data.ara_vend_id);
431         intf->product_id = le32_to_cpu(request->data.ara_prod_id);
432
433         ret = gb_svc_read_and_clear_module_boot_status(intf);
434         if (ret) {
435                 dev_err(&svc->dev, "failed to clear boot status of interface %u: %d\n",
436                                 intf_id, ret);
437                 goto destroy_interface;
438         }
439
440         /*
441          * Create a device id for the interface:
442          * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
443          * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
444          *
445          * XXX Do we need to allocate device ID for SVC or the AP here? And what
446          * XXX about an AP with multiple interface blocks?
447          */
448         device_id = ida_simple_get(&svc->device_id_map,
449                                    GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
450         if (device_id < 0) {
451                 ret = device_id;
452                 dev_err(&svc->dev, "failed to allocate device id for interface %u: %d\n",
453                                 intf_id, ret);
454                 goto destroy_interface;
455         }
456
457         ret = gb_svc_intf_device_id(svc, intf_id, device_id);
458         if (ret) {
459                 dev_err(&svc->dev, "failed to set device id %u for interface %u: %d\n",
460                                 device_id, intf_id, ret);
461                 goto ida_put;
462         }
463
464         /*
465          * Create a two-way route between the AP and the new interface
466          */
467         ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
468                                   intf_id, device_id);
469         if (ret) {
470                 dev_err(&svc->dev, "failed to create route to interface %u (device id %u): %d\n",
471                                 intf_id, device_id, ret);
472                 goto svc_id_free;
473         }
474
475         ret = gb_interface_init(intf, device_id);
476         if (ret) {
477                 dev_err(&svc->dev, "failed to initialize interface %u (device id %u): %d\n",
478                                 intf_id, device_id, ret);
479                 goto destroy_route;
480         }
481
482         return;
483
484 destroy_route:
485         gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
486 svc_id_free:
487         /*
488          * XXX Should we tell SVC that this id doesn't belong to interface
489          * XXX anymore.
490          */
491 ida_put:
492         ida_simple_remove(&svc->device_id_map, device_id);
493 destroy_interface:
494         gb_interface_remove(intf);
495 }
496
497 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
498 {
499         struct gb_svc *svc = operation->connection->private;
500         struct gb_svc_intf_hot_unplug_request *request;
501         struct gb_host_device *hd = operation->connection->hd;
502         struct gb_interface *intf;
503         u8 intf_id;
504
505         /* The request message size has already been verified. */
506         request = operation->request->payload;
507         intf_id = request->intf_id;
508
509         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
510
511         intf = gb_interface_find(hd, intf_id);
512         if (!intf) {
513                 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
514                                 intf_id);
515                 return;
516         }
517
518         gb_svc_intf_remove(svc, intf);
519 }
520
521 static void gb_svc_process_deferred_request(struct work_struct *work)
522 {
523         struct gb_svc_deferred_request *dr;
524         struct gb_operation *operation;
525         struct gb_svc *svc;
526         u8 type;
527
528         dr = container_of(work, struct gb_svc_deferred_request, work);
529         operation = dr->operation;
530         svc = operation->connection->private;
531         type = operation->request->header->type;
532
533         switch (type) {
534         case GB_SVC_TYPE_INTF_HOTPLUG:
535                 gb_svc_process_intf_hotplug(operation);
536                 break;
537         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
538                 gb_svc_process_intf_hot_unplug(operation);
539                 break;
540         default:
541                 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
542         }
543
544         gb_operation_put(operation);
545         kfree(dr);
546 }
547
548 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
549 {
550         struct gb_svc *svc = operation->connection->private;
551         struct gb_svc_deferred_request *dr;
552
553         dr = kmalloc(sizeof(*dr), GFP_KERNEL);
554         if (!dr)
555                 return -ENOMEM;
556
557         gb_operation_get(operation);
558
559         dr->operation = operation;
560         INIT_WORK(&dr->work, gb_svc_process_deferred_request);
561
562         queue_work(svc->wq, &dr->work);
563
564         return 0;
565 }
566
567 /*
568  * Bringing up a module can be time consuming, as that may require lots of
569  * initialization on the module side. Over that, we may also need to download
570  * the firmware first and flash that on the module.
571  *
572  * In order not to make other svc events wait for all this to finish,
573  * handle most of module hotplug stuff outside of the hotplug callback, with
574  * help of a workqueue.
575  */
576 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
577 {
578         struct gb_svc *svc = op->connection->private;
579         struct gb_svc_intf_hotplug_request *request;
580
581         if (op->request->payload_size < sizeof(*request)) {
582                 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
583                                 op->request->payload_size, sizeof(*request));
584                 return -EINVAL;
585         }
586
587         request = op->request->payload;
588
589         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
590
591         return gb_svc_queue_deferred_request(op);
592 }
593
594 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
595 {
596         struct gb_svc *svc = op->connection->private;
597         struct gb_svc_intf_hot_unplug_request *request;
598
599         if (op->request->payload_size < sizeof(*request)) {
600                 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
601                                 op->request->payload_size, sizeof(*request));
602                 return -EINVAL;
603         }
604
605         request = op->request->payload;
606
607         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
608
609         return gb_svc_queue_deferred_request(op);
610 }
611
612 static int gb_svc_intf_reset_recv(struct gb_operation *op)
613 {
614         struct gb_svc *svc = op->connection->private;
615         struct gb_message *request = op->request;
616         struct gb_svc_intf_reset_request *reset;
617         u8 intf_id;
618
619         if (request->payload_size < sizeof(*reset)) {
620                 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
621                                 request->payload_size, sizeof(*reset));
622                 return -EINVAL;
623         }
624         reset = request->payload;
625
626         intf_id = reset->intf_id;
627
628         /* FIXME Reset the interface here */
629
630         return 0;
631 }
632
633 static int gb_svc_request_recv(u8 type, struct gb_operation *op)
634 {
635         struct gb_connection *connection = op->connection;
636         struct gb_svc *svc = connection->private;
637         int ret = 0;
638
639         /*
640          * SVC requests need to follow a specific order (at least initially) and
641          * below code takes care of enforcing that. The expected order is:
642          * - PROTOCOL_VERSION
643          * - SVC_HELLO
644          * - Any other request, but the earlier two.
645          *
646          * Incoming requests are guaranteed to be serialized and so we don't
647          * need to protect 'state' for any races.
648          */
649         switch (type) {
650         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
651                 if (svc->state != GB_SVC_STATE_RESET)
652                         ret = -EINVAL;
653                 break;
654         case GB_SVC_TYPE_SVC_HELLO:
655                 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
656                         ret = -EINVAL;
657                 break;
658         default:
659                 if (svc->state != GB_SVC_STATE_SVC_HELLO)
660                         ret = -EINVAL;
661                 break;
662         }
663
664         if (ret) {
665                 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
666                                 type, svc->state);
667                 return ret;
668         }
669
670         switch (type) {
671         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
672                 ret = gb_svc_version_request(op);
673                 if (!ret)
674                         svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
675                 return ret;
676         case GB_SVC_TYPE_SVC_HELLO:
677                 ret = gb_svc_hello(op);
678                 if (!ret)
679                         svc->state = GB_SVC_STATE_SVC_HELLO;
680                 return ret;
681         case GB_SVC_TYPE_INTF_HOTPLUG:
682                 return gb_svc_intf_hotplug_recv(op);
683         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
684                 return gb_svc_intf_hot_unplug_recv(op);
685         case GB_SVC_TYPE_INTF_RESET:
686                 return gb_svc_intf_reset_recv(op);
687         default:
688                 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
689                 return -EINVAL;
690         }
691 }
692
693 static void gb_svc_release(struct device *dev)
694 {
695         struct gb_svc *svc = to_gb_svc(dev);
696
697         if (svc->connection)
698                 gb_connection_destroy(svc->connection);
699         ida_destroy(&svc->device_id_map);
700         destroy_workqueue(svc->wq);
701         kfree(svc);
702 }
703
704 struct device_type greybus_svc_type = {
705         .name           = "greybus_svc",
706         .release        = gb_svc_release,
707 };
708
709 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
710 {
711         struct gb_svc *svc;
712
713         svc = kzalloc(sizeof(*svc), GFP_KERNEL);
714         if (!svc)
715                 return NULL;
716
717         svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
718         if (!svc->wq) {
719                 kfree(svc);
720                 return NULL;
721         }
722
723         svc->dev.parent = &hd->dev;
724         svc->dev.bus = &greybus_bus_type;
725         svc->dev.type = &greybus_svc_type;
726         svc->dev.groups = svc_groups;
727         svc->dev.dma_mask = svc->dev.parent->dma_mask;
728         device_initialize(&svc->dev);
729
730         dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
731
732         ida_init(&svc->device_id_map);
733         svc->state = GB_SVC_STATE_RESET;
734         svc->hd = hd;
735
736         svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
737                                                         GREYBUS_PROTOCOL_SVC);
738         if (!svc->connection) {
739                 dev_err(&svc->dev, "failed to create connection\n");
740                 put_device(&svc->dev);
741                 return NULL;
742         }
743
744         svc->connection->private = svc;
745
746         return svc;
747 }
748
749 int gb_svc_add(struct gb_svc *svc)
750 {
751         int ret;
752
753         /*
754          * The SVC protocol is currently driven by the SVC, so the SVC device
755          * is added from the connection request handler when enough
756          * information has been received.
757          */
758         ret = gb_connection_init(svc->connection);
759         if (ret)
760                 return ret;
761
762         return 0;
763 }
764
765 void gb_svc_del(struct gb_svc *svc)
766 {
767         /*
768          * The SVC device may have been registered from the request handler.
769          */
770         if (device_is_registered(&svc->dev))
771                 device_del(&svc->dev);
772
773         gb_connection_exit(svc->connection);
774
775         flush_workqueue(svc->wq);
776 }
777
778 void gb_svc_put(struct gb_svc *svc)
779 {
780         put_device(&svc->dev);
781 }
782
783 static int gb_svc_connection_init(struct gb_connection *connection)
784 {
785         struct gb_svc *svc = connection->private;
786
787         dev_dbg(&svc->dev, "%s\n", __func__);
788
789         return 0;
790 }
791
792 static void gb_svc_connection_exit(struct gb_connection *connection)
793 {
794         struct gb_svc *svc = connection->private;
795
796         dev_dbg(&svc->dev, "%s\n", __func__);
797 }
798
799 static struct gb_protocol svc_protocol = {
800         .name                   = "svc",
801         .id                     = GREYBUS_PROTOCOL_SVC,
802         .major                  = GB_SVC_VERSION_MAJOR,
803         .minor                  = GB_SVC_VERSION_MINOR,
804         .connection_init        = gb_svc_connection_init,
805         .connection_exit        = gb_svc_connection_exit,
806         .request_recv           = gb_svc_request_recv,
807         .flags                  = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
808                                   GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
809                                   GB_PROTOCOL_SKIP_VERSION,
810 };
811 gb_builtin_protocol_driver(svc_protocol);