2cfb5a46e7d45b94c275f8177a1d20d2ad4a4bd0
[cascardo/linux.git] / drivers / staging / greybus / interface.c
1 /*
2  * Greybus interface code
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include "greybus.h"
11
12 #include "greybus_trace.h"
13
14 #define GB_INTERFACE_DEVICE_ID_BAD      0xff
15
16 /* Don't-care selector index */
17 #define DME_SELECTOR_INDEX_NULL         0
18
19 /* DME attributes */
20 /* FIXME: remove ES2 support and DME_T_TST_SRC_INCREMENT */
21 #define DME_T_TST_SRC_INCREMENT         0x4083
22
23 #define DME_DDBL1_MANUFACTURERID        0x5003
24 #define DME_DDBL1_PRODUCTID             0x5004
25
26 #define DME_TOSHIBA_ARA_VID             0x6000
27 #define DME_TOSHIBA_ARA_PID             0x6001
28 #define DME_TOSHIBA_ARA_SN0             0x6002
29 #define DME_TOSHIBA_ARA_SN1             0x6003
30 #define DME_TOSHIBA_ARA_INIT_STATUS     0x6101
31
32 /* DDBL1 Manufacturer and Product ids */
33 #define TOSHIBA_DMID                    0x0126
34 #define TOSHIBA_ES2_BRIDGE_DPID         0x1000
35 #define TOSHIBA_ES3_APBRIDGE_DPID       0x1001
36 #define TOSHIBA_ES3_GBPHY_DPID  0x1002
37
38
39 static int gb_interface_dme_attr_get(struct gb_interface *intf,
40                                                         u16 attr, u32 *val)
41 {
42         return gb_svc_dme_peer_get(intf->hd->svc, intf->interface_id,
43                                         attr, DME_SELECTOR_INDEX_NULL, val);
44 }
45
46 static int gb_interface_read_ara_dme(struct gb_interface *intf)
47 {
48         u32 sn0, sn1;
49         int ret;
50
51         /*
52          * Unless this is a Toshiba bridge, bail out until we have defined
53          * standard Ara attributes.
54          */
55         if (intf->ddbl1_manufacturer_id != TOSHIBA_DMID) {
56                 dev_err(&intf->dev, "unknown manufacturer %08x\n",
57                                 intf->ddbl1_manufacturer_id);
58                 return -ENODEV;
59         }
60
61         ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_VID,
62                                         &intf->vendor_id);
63         if (ret)
64                 return ret;
65
66         ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_PID,
67                                         &intf->product_id);
68         if (ret)
69                 return ret;
70
71         ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN0, &sn0);
72         if (ret)
73                 return ret;
74
75         ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN1, &sn1);
76         if (ret)
77                 return ret;
78
79         intf->serial_number = (u64)sn1 << 32 | sn0;
80
81         return 0;
82 }
83
84 static int gb_interface_read_dme(struct gb_interface *intf)
85 {
86         int ret;
87
88         ret = gb_interface_dme_attr_get(intf, DME_DDBL1_MANUFACTURERID,
89                                         &intf->ddbl1_manufacturer_id);
90         if (ret)
91                 return ret;
92
93         ret = gb_interface_dme_attr_get(intf, DME_DDBL1_PRODUCTID,
94                                         &intf->ddbl1_product_id);
95         if (ret)
96                 return ret;
97
98         if (intf->ddbl1_manufacturer_id == TOSHIBA_DMID &&
99                         intf->ddbl1_product_id == TOSHIBA_ES2_BRIDGE_DPID) {
100                 intf->quirks |= GB_INTERFACE_QUIRK_NO_ARA_IDS;
101                 intf->quirks |= GB_INTERFACE_QUIRK_NO_INIT_STATUS;
102         }
103
104         return gb_interface_read_ara_dme(intf);
105 }
106
107 static int gb_interface_route_create(struct gb_interface *intf)
108 {
109         struct gb_svc *svc = intf->hd->svc;
110         u8 intf_id = intf->interface_id;
111         u8 device_id;
112         int ret;
113
114         /* Allocate an interface device id. */
115         ret = ida_simple_get(&svc->device_id_map,
116                              GB_SVC_DEVICE_ID_MIN, GB_SVC_DEVICE_ID_MAX + 1,
117                              GFP_KERNEL);
118         if (ret < 0) {
119                 dev_err(&intf->dev, "failed to allocate device id: %d\n", ret);
120                 return ret;
121         }
122         device_id = ret;
123
124         ret = gb_svc_intf_device_id(svc, intf_id, device_id);
125         if (ret) {
126                 dev_err(&intf->dev, "failed to set device id %u: %d\n",
127                                 device_id, ret);
128                 goto err_ida_remove;
129         }
130
131         /* FIXME: Hard-coded AP device id. */
132         ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_SVC_DEVICE_ID_AP,
133                                   intf_id, device_id);
134         if (ret) {
135                 dev_err(&intf->dev, "failed to create route: %d\n", ret);
136                 goto err_svc_id_free;
137         }
138
139         intf->device_id = device_id;
140
141         return 0;
142
143 err_svc_id_free:
144         /*
145          * XXX Should we tell SVC that this id doesn't belong to interface
146          * XXX anymore.
147          */
148 err_ida_remove:
149         ida_simple_remove(&svc->device_id_map, device_id);
150
151         return ret;
152 }
153
154 static void gb_interface_route_destroy(struct gb_interface *intf)
155 {
156         struct gb_svc *svc = intf->hd->svc;
157
158         if (intf->device_id == GB_INTERFACE_DEVICE_ID_BAD)
159                 return;
160
161         gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id);
162         ida_simple_remove(&svc->device_id_map, intf->device_id);
163         intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
164 }
165
166 /*
167  * T_TstSrcIncrement is written by the module on ES2 as a stand-in for the
168  * init-status attribute DME_TOSHIBA_INIT_STATUS. The AP needs to read and
169  * clear it after reading a non-zero value from it.
170  *
171  * FIXME: This is module-hardware dependent and needs to be extended for every
172  * type of module we want to support.
173  */
174 static int gb_interface_read_and_clear_init_status(struct gb_interface *intf)
175 {
176         struct gb_host_device *hd = intf->hd;
177         unsigned long bootrom_quirks;
178         int ret;
179         u32 value;
180         u16 attr;
181         u8 init_status;
182
183         /*
184          * ES2 bridges use T_TstSrcIncrement for the init status.
185          *
186          * FIXME: Remove ES2 support
187          */
188         if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
189                 attr = DME_T_TST_SRC_INCREMENT;
190         else
191                 attr = DME_TOSHIBA_ARA_INIT_STATUS;
192
193         ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
194                                   DME_SELECTOR_INDEX_NULL, &value);
195         if (ret)
196                 return ret;
197
198         /*
199          * A nonzero init status indicates the module has finished
200          * initializing.
201          */
202         if (!value) {
203                 dev_err(&intf->dev, "invalid init status\n");
204                 return -ENODEV;
205         }
206
207         /*
208          * Extract the init status.
209          *
210          * For ES2: We need to check lowest 8 bits of 'value'.
211          * For ES3: We need to check highest 8 bits out of 32 of 'value'.
212          *
213          * FIXME: Remove ES2 support
214          */
215         if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
216                 init_status = value & 0xff;
217         else
218                 init_status = value >> 24;
219
220         /*
221          * Check if the interface is executing the quirky ES3 bootrom that,
222          * for example, requires E2EFC, CSD and CSV to be disabled.
223          */
224         bootrom_quirks = GB_INTERFACE_QUIRK_NO_CPORT_FEATURES |
225                                 GB_INTERFACE_QUIRK_FORCED_DISABLE;
226         switch (init_status) {
227         case GB_INIT_BOOTROM_UNIPRO_BOOT_STARTED:
228         case GB_INIT_BOOTROM_FALLBACK_UNIPRO_BOOT_STARTED:
229                 intf->quirks |= bootrom_quirks;
230                 break;
231         default:
232                 intf->quirks &= ~bootrom_quirks;
233         }
234
235         /* Clear the init status. */
236         return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
237                                    DME_SELECTOR_INDEX_NULL, 0);
238 }
239
240 /* interface sysfs attributes */
241 #define gb_interface_attr(field, type)                                  \
242 static ssize_t field##_show(struct device *dev,                         \
243                             struct device_attribute *attr,              \
244                             char *buf)                                  \
245 {                                                                       \
246         struct gb_interface *intf = to_gb_interface(dev);               \
247         return scnprintf(buf, PAGE_SIZE, type"\n", intf->field);        \
248 }                                                                       \
249 static DEVICE_ATTR_RO(field)
250
251 gb_interface_attr(ddbl1_manufacturer_id, "0x%08x");
252 gb_interface_attr(ddbl1_product_id, "0x%08x");
253 gb_interface_attr(interface_id, "%u");
254 gb_interface_attr(vendor_id, "0x%08x");
255 gb_interface_attr(product_id, "0x%08x");
256 gb_interface_attr(serial_number, "0x%016llx");
257
258 static ssize_t voltage_now_show(struct device *dev,
259                                 struct device_attribute *attr, char *buf)
260 {
261         struct gb_interface *intf = to_gb_interface(dev);
262         int ret;
263         u32 measurement;
264
265         ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
266                                             GB_SVC_PWRMON_TYPE_VOL,
267                                             &measurement);
268         if (ret) {
269                 dev_err(&intf->dev, "failed to get voltage sample (%d)\n", ret);
270                 return ret;
271         }
272
273         return sprintf(buf, "%u\n", measurement);
274 }
275 static DEVICE_ATTR_RO(voltage_now);
276
277 static ssize_t current_now_show(struct device *dev,
278                                 struct device_attribute *attr, char *buf)
279 {
280         struct gb_interface *intf = to_gb_interface(dev);
281         int ret;
282         u32 measurement;
283
284         ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
285                                             GB_SVC_PWRMON_TYPE_CURR,
286                                             &measurement);
287         if (ret) {
288                 dev_err(&intf->dev, "failed to get current sample (%d)\n", ret);
289                 return ret;
290         }
291
292         return sprintf(buf, "%u\n", measurement);
293 }
294 static DEVICE_ATTR_RO(current_now);
295
296 static ssize_t power_now_show(struct device *dev,
297                               struct device_attribute *attr, char *buf)
298 {
299         struct gb_interface *intf = to_gb_interface(dev);
300         int ret;
301         u32 measurement;
302
303         ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
304                                             GB_SVC_PWRMON_TYPE_PWR,
305                                             &measurement);
306         if (ret) {
307                 dev_err(&intf->dev, "failed to get power sample (%d)\n", ret);
308                 return ret;
309         }
310
311         return sprintf(buf, "%u\n", measurement);
312 }
313 static DEVICE_ATTR_RO(power_now);
314
315 static struct attribute *interface_attrs[] = {
316         &dev_attr_ddbl1_manufacturer_id.attr,
317         &dev_attr_ddbl1_product_id.attr,
318         &dev_attr_interface_id.attr,
319         &dev_attr_vendor_id.attr,
320         &dev_attr_product_id.attr,
321         &dev_attr_serial_number.attr,
322         &dev_attr_voltage_now.attr,
323         &dev_attr_current_now.attr,
324         &dev_attr_power_now.attr,
325         NULL,
326 };
327 ATTRIBUTE_GROUPS(interface);
328
329 static void gb_interface_release(struct device *dev)
330 {
331         struct gb_interface *intf = to_gb_interface(dev);
332
333         trace_gb_interface_release(intf);
334
335         kfree(intf);
336 }
337
338 struct device_type greybus_interface_type = {
339         .name =         "greybus_interface",
340         .release =      gb_interface_release,
341 };
342
343 /*
344  * A Greybus module represents a user-replaceable component on an Ara
345  * phone.  An interface is the physical connection on that module.  A
346  * module may have more than one interface.
347  *
348  * Create a gb_interface structure to represent a discovered interface.
349  * The position of interface within the Endo is encoded in "interface_id"
350  * argument.
351  *
352  * Returns a pointer to the new interfce or a null pointer if a
353  * failure occurs due to memory exhaustion.
354  */
355 struct gb_interface *gb_interface_create(struct gb_module *module,
356                                          u8 interface_id)
357 {
358         struct gb_host_device *hd = module->hd;
359         struct gb_interface *intf;
360
361         intf = kzalloc(sizeof(*intf), GFP_KERNEL);
362         if (!intf)
363                 return NULL;
364
365         intf->hd = hd;          /* XXX refcount? */
366         intf->module = module;
367         intf->interface_id = interface_id;
368         INIT_LIST_HEAD(&intf->bundles);
369         INIT_LIST_HEAD(&intf->manifest_descs);
370         mutex_init(&intf->mutex);
371
372         /* Invalid device id to start with */
373         intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
374
375         intf->dev.parent = &module->dev;
376         intf->dev.bus = &greybus_bus_type;
377         intf->dev.type = &greybus_interface_type;
378         intf->dev.groups = interface_groups;
379         intf->dev.dma_mask = module->dev.dma_mask;
380         device_initialize(&intf->dev);
381         dev_set_name(&intf->dev, "%s.%u", dev_name(&module->dev),
382                         interface_id);
383
384         trace_gb_interface_create(intf);
385
386         return intf;
387 }
388
389 static int gb_interface_vsys_set(struct gb_interface *intf, bool enable)
390 {
391         struct gb_svc *svc = intf->hd->svc;
392         int ret;
393
394         dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
395
396         ret = gb_svc_intf_vsys_set(svc, intf->interface_id, enable);
397         if (ret) {
398                 dev_err(&intf->dev, "failed to set v_sys: %d\n", ret);
399                 return ret;
400         }
401
402         return 0;
403 }
404
405 static int gb_interface_refclk_set(struct gb_interface *intf, bool enable)
406 {
407         struct gb_svc *svc = intf->hd->svc;
408         int ret;
409
410         dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
411
412         ret = gb_svc_intf_refclk_set(svc, intf->interface_id, enable);
413         if (ret) {
414                 dev_err(&intf->dev, "failed to set refclk: %d\n", ret);
415                 return ret;
416         }
417
418         return 0;
419 }
420
421 static int gb_interface_unipro_set(struct gb_interface *intf, bool enable)
422 {
423         struct gb_svc *svc = intf->hd->svc;
424         int ret;
425
426         dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
427
428         ret = gb_svc_intf_unipro_set(svc, intf->interface_id, enable);
429         if (ret) {
430                 dev_err(&intf->dev, "failed to set UniPro: %d\n", ret);
431                 return ret;
432         }
433
434         return 0;
435 }
436
437 static int gb_interface_activate_operation(struct gb_interface *intf)
438 {
439         struct gb_svc *svc = intf->hd->svc;
440         u8 type;
441         int ret;
442
443         dev_dbg(&intf->dev, "%s\n", __func__);
444
445         ret = gb_svc_intf_activate(svc, intf->interface_id, &type);
446         if (ret) {
447                 dev_err(&intf->dev, "failed to activate: %d\n", ret);
448                 return ret;
449         }
450
451         switch (type) {
452         case GB_SVC_INTF_TYPE_DUMMY:
453                 dev_info(&intf->dev, "dummy interface detected\n");
454                 /* FIXME: handle as an error for now */
455                 return -ENODEV;
456         case GB_SVC_INTF_TYPE_UNIPRO:
457                 dev_err(&intf->dev, "interface type UniPro not supported\n");
458                 /* FIXME: check if this is a Toshiba bridge before retrying? */
459                 return -EAGAIN;
460         case GB_SVC_INTF_TYPE_GREYBUS:
461                 break;
462         default:
463                 dev_err(&intf->dev, "unknown interface type: %u\n", type);
464                 return -ENODEV;
465         }
466
467         return 0;
468 }
469
470 static int gb_interface_hibernate_link(struct gb_interface *intf)
471 {
472         dev_dbg(&intf->dev, "%s\n", __func__);
473
474         /* FIXME: implement */
475
476         return 0;
477 }
478
479 /*
480  * Activate an interface.
481  *
482  * Locking: Caller holds the interface mutex.
483  */
484 int gb_interface_activate(struct gb_interface *intf)
485 {
486         int ret;
487
488         if (intf->ejected)
489                 return -ENODEV;
490
491         ret = gb_interface_vsys_set(intf, true);
492         if (ret)
493                 return ret;
494
495         ret = gb_interface_refclk_set(intf, true);
496         if (ret)
497                 goto err_vsys_disable;
498
499         ret = gb_interface_unipro_set(intf, true);
500         if (ret)
501                 goto err_refclk_disable;
502
503         ret = gb_interface_activate_operation(intf);
504         if (ret)
505                 goto err_unipro_disable;
506
507         ret = gb_interface_read_dme(intf);
508         if (ret)
509                 goto err_hibernate_link;
510
511         ret = gb_interface_route_create(intf);
512         if (ret)
513                 goto err_hibernate_link;
514
515         intf->active = true;
516
517         trace_gb_interface_activate(intf);
518
519         return 0;
520
521 err_hibernate_link:
522         gb_interface_hibernate_link(intf);
523 err_unipro_disable:
524         gb_interface_unipro_set(intf, false);
525 err_refclk_disable:
526         gb_interface_refclk_set(intf, false);
527 err_vsys_disable:
528         gb_interface_vsys_set(intf, false);
529
530         return ret;
531 }
532
533 /*
534  * Deactivate an interface.
535  *
536  * Locking: Caller holds the interface mutex.
537  */
538 void gb_interface_deactivate(struct gb_interface *intf)
539 {
540         if (!intf->active)
541                 return;
542
543         trace_gb_interface_deactivate(intf);
544
545         gb_interface_route_destroy(intf);
546         gb_interface_hibernate_link(intf);
547         gb_interface_unipro_set(intf, false);
548         gb_interface_refclk_set(intf, false);
549         gb_interface_vsys_set(intf, false);
550
551         intf->active = false;
552 }
553
554 /*
555  * Enable an interface by enabling its control connection, fetching the
556  * manifest and other information over it, and finally registering its child
557  * devices.
558  *
559  * Locking: Caller holds the interface mutex.
560  */
561 int gb_interface_enable(struct gb_interface *intf)
562 {
563         struct gb_control *control;
564         struct gb_bundle *bundle, *tmp;
565         int ret, size;
566         void *manifest;
567
568         ret = gb_interface_read_and_clear_init_status(intf);
569         if (ret) {
570                 dev_err(&intf->dev, "failed to clear init status: %d\n", ret);
571                 return ret;
572         }
573
574         /* Establish control connection */
575         control = gb_control_create(intf);
576         if (IS_ERR(control)) {
577                 dev_err(&intf->dev, "failed to create control device: %ld\n",
578                                 PTR_ERR(control));
579                 return PTR_ERR(control);
580         }
581         intf->control = control;
582
583         ret = gb_control_enable(intf->control);
584         if (ret)
585                 goto err_put_control;
586
587         /* Get manifest size using control protocol on CPort */
588         size = gb_control_get_manifest_size_operation(intf);
589         if (size <= 0) {
590                 dev_err(&intf->dev, "failed to get manifest size: %d\n", size);
591
592                 if (size)
593                         ret = size;
594                 else
595                         ret =  -EINVAL;
596
597                 goto err_disable_control;
598         }
599
600         manifest = kmalloc(size, GFP_KERNEL);
601         if (!manifest) {
602                 ret = -ENOMEM;
603                 goto err_disable_control;
604         }
605
606         /* Get manifest using control protocol on CPort */
607         ret = gb_control_get_manifest_operation(intf, manifest, size);
608         if (ret) {
609                 dev_err(&intf->dev, "failed to get manifest: %d\n", ret);
610                 goto err_free_manifest;
611         }
612
613         /*
614          * Parse the manifest and build up our data structures representing
615          * what's in it.
616          */
617         if (!gb_manifest_parse(intf, manifest, size)) {
618                 dev_err(&intf->dev, "failed to parse manifest\n");
619                 ret = -EINVAL;
620                 goto err_destroy_bundles;
621         }
622
623         ret = gb_control_get_bundle_versions(intf->control);
624         if (ret)
625                 goto err_destroy_bundles;
626
627         /* Register the control device and any bundles */
628         ret = gb_control_add(intf->control);
629         if (ret)
630                 goto err_destroy_bundles;
631
632         list_for_each_entry_safe_reverse(bundle, tmp, &intf->bundles, links) {
633                 ret = gb_bundle_add(bundle);
634                 if (ret) {
635                         gb_bundle_destroy(bundle);
636                         continue;
637                 }
638         }
639
640         kfree(manifest);
641
642         intf->enabled = true;
643
644         trace_gb_interface_enable(intf);
645
646         return 0;
647
648 err_destroy_bundles:
649         list_for_each_entry_safe(bundle, tmp, &intf->bundles, links)
650                 gb_bundle_destroy(bundle);
651 err_free_manifest:
652         kfree(manifest);
653 err_disable_control:
654         gb_control_disable(intf->control);
655 err_put_control:
656         gb_control_put(intf->control);
657         intf->control = NULL;
658
659         return ret;
660 }
661
662 /*
663  * Disable an interface and destroy its bundles.
664  *
665  * Locking: Caller holds the interface mutex.
666  */
667 void gb_interface_disable(struct gb_interface *intf)
668 {
669         struct gb_bundle *bundle;
670         struct gb_bundle *next;
671
672         if (!intf->enabled)
673                 return;
674
675         trace_gb_interface_disable(intf);
676
677         /* Set disconnected flag to avoid I/O during connection tear down. */
678         if (intf->quirks & GB_INTERFACE_QUIRK_FORCED_DISABLE)
679                 intf->disconnected = true;
680
681         list_for_each_entry_safe(bundle, next, &intf->bundles, links)
682                 gb_bundle_destroy(bundle);
683
684         gb_control_del(intf->control);
685         gb_control_disable(intf->control);
686         gb_control_put(intf->control);
687         intf->control = NULL;
688
689         intf->enabled = false;
690 }
691
692 /* Register an interface. */
693 int gb_interface_add(struct gb_interface *intf)
694 {
695         int ret;
696
697         ret = device_add(&intf->dev);
698         if (ret) {
699                 dev_err(&intf->dev, "failed to register interface: %d\n", ret);
700                 return ret;
701         }
702
703         trace_gb_interface_add(intf);
704
705         dev_info(&intf->dev, "Interface added: VID=0x%08x, PID=0x%08x\n",
706                  intf->vendor_id, intf->product_id);
707         dev_info(&intf->dev, "DDBL1 Manufacturer=0x%08x, Product=0x%08x\n",
708                  intf->ddbl1_manufacturer_id, intf->ddbl1_product_id);
709
710         return 0;
711 }
712
713 /* Deregister an interface. */
714 void gb_interface_del(struct gb_interface *intf)
715 {
716         if (device_is_registered(&intf->dev)) {
717                 trace_gb_interface_del(intf);
718
719                 device_del(&intf->dev);
720                 dev_info(&intf->dev, "Interface removed\n");
721         }
722 }
723
724 void gb_interface_put(struct gb_interface *intf)
725 {
726         put_device(&intf->dev);
727 }