56a320732ef9ca7ce5e78cfa0b4585ccb7029c2c
[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
13 #define GB_INTERFACE_DEVICE_ID_BAD      0xff
14
15 /* Don't-care selector index */
16 #define DME_SELECTOR_INDEX_NULL         0
17
18 /* DME attributes */
19 /* FIXME: remove ES2 support and DME_T_TST_SRC_INCREMENT */
20 #define DME_T_TST_SRC_INCREMENT         0x4083
21
22 #define DME_DDBL1_MANUFACTURERID        0x5003
23 #define DME_DDBL1_PRODUCTID             0x5004
24
25 #define DME_TOSHIBA_ARA_VID             0x6000
26 #define DME_TOSHIBA_ARA_PID             0x6001
27 #define DME_TOSHIBA_ARA_SN0             0x6002
28 #define DME_TOSHIBA_ARA_SN1             0x6003
29 #define DME_TOSHIBA_ARA_INIT_STATUS     0x6101
30
31 /* DDBL1 Manufacturer and Product ids */
32 #define TOSHIBA_DMID                    0x0126
33 #define TOSHIBA_ES2_BRIDGE_DPID         0x1000
34 #define TOSHIBA_ES3_APBRIDGE_DPID       0x1001
35 #define TOSHIBA_ES3_GPBRIDGE_DPID       0x1002
36
37
38 static int gb_interface_dme_attr_get(struct gb_interface *intf,
39                                                         u16 attr, u32 *val)
40 {
41         return gb_svc_dme_peer_get(intf->hd->svc, intf->interface_id,
42                                         attr, DME_SELECTOR_INDEX_NULL, val);
43 }
44
45 static int gb_interface_read_ara_dme(struct gb_interface *intf)
46 {
47         u32 sn0, sn1;
48         int ret;
49
50         /*
51          * Unless this is a Toshiba bridge, bail out until we have defined
52          * standard Ara attributes.
53          */
54         if (intf->ddbl1_manufacturer_id != TOSHIBA_DMID) {
55                 dev_err(&intf->dev, "unknown manufacturer %08x\n",
56                                 intf->ddbl1_manufacturer_id);
57                 return -ENODEV;
58         }
59
60         ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_VID,
61                                         &intf->vendor_id);
62         if (ret)
63                 return ret;
64
65         ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_PID,
66                                         &intf->product_id);
67         if (ret)
68                 return ret;
69
70         ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN0, &sn0);
71         if (ret)
72                 return ret;
73
74         ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN1, &sn1);
75         if (ret)
76                 return ret;
77
78         intf->serial_number = (u64)sn1 << 32 | sn0;
79
80         return 0;
81 }
82
83 static int gb_interface_read_dme(struct gb_interface *intf)
84 {
85         int ret;
86
87         ret = gb_interface_dme_attr_get(intf, DME_DDBL1_MANUFACTURERID,
88                                         &intf->ddbl1_manufacturer_id);
89         if (ret)
90                 return ret;
91
92         ret = gb_interface_dme_attr_get(intf, DME_DDBL1_PRODUCTID,
93                                         &intf->ddbl1_product_id);
94         if (ret)
95                 return ret;
96
97         if (intf->ddbl1_manufacturer_id == TOSHIBA_DMID &&
98                         intf->ddbl1_product_id == TOSHIBA_ES2_BRIDGE_DPID) {
99                 intf->quirks |= GB_INTERFACE_QUIRK_NO_ARA_IDS;
100                 intf->quirks |= GB_INTERFACE_QUIRK_NO_INIT_STATUS;
101         }
102
103         return gb_interface_read_ara_dme(intf);
104 }
105
106 static int gb_interface_route_create(struct gb_interface *intf)
107 {
108         struct gb_svc *svc = intf->hd->svc;
109         u8 intf_id = intf->interface_id;
110         u8 device_id;
111         int ret;
112
113         /* Allocate an interface device id. */
114         ret = ida_simple_get(&svc->device_id_map,
115                              GB_SVC_DEVICE_ID_MIN, GB_SVC_DEVICE_ID_MAX + 1,
116                              GFP_KERNEL);
117         if (ret < 0) {
118                 dev_err(&intf->dev, "failed to allocate device id: %d\n", ret);
119                 return ret;
120         }
121         device_id = ret;
122
123         ret = gb_svc_intf_device_id(svc, intf_id, device_id);
124         if (ret) {
125                 dev_err(&intf->dev, "failed to set device id %u: %d\n",
126                                 device_id, ret);
127                 goto err_ida_remove;
128         }
129
130         /* FIXME: Hard-coded AP device id. */
131         ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_SVC_DEVICE_ID_AP,
132                                   intf_id, device_id);
133         if (ret) {
134                 dev_err(&intf->dev, "failed to create route: %d\n", ret);
135                 goto err_svc_id_free;
136         }
137
138         intf->device_id = device_id;
139
140         return 0;
141
142 err_svc_id_free:
143         /*
144          * XXX Should we tell SVC that this id doesn't belong to interface
145          * XXX anymore.
146          */
147 err_ida_remove:
148         ida_simple_remove(&svc->device_id_map, device_id);
149
150         return ret;
151 }
152
153 static void gb_interface_route_destroy(struct gb_interface *intf)
154 {
155         struct gb_svc *svc = intf->hd->svc;
156
157         if (intf->device_id == GB_INTERFACE_DEVICE_ID_BAD)
158                 return;
159
160         gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id);
161         ida_simple_remove(&svc->device_id_map, intf->device_id);
162         intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
163 }
164
165 /*
166  * T_TstSrcIncrement is written by the module on ES2 as a stand-in for the
167  * init-status attribute DME_TOSHIBA_INIT_STATUS. The AP needs to read and
168  * clear it after reading a non-zero value from it.
169  *
170  * FIXME: This is module-hardware dependent and needs to be extended for every
171  * type of module we want to support.
172  */
173 static int gb_interface_read_and_clear_init_status(struct gb_interface *intf)
174 {
175         struct gb_host_device *hd = intf->hd;
176         int ret;
177         u32 value;
178         u16 attr;
179         u8 init_status;
180
181         /*
182          * ES2 bridges use T_TstSrcIncrement for the init status.
183          *
184          * FIXME: Remove ES2 support
185          */
186         if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
187                 attr = DME_T_TST_SRC_INCREMENT;
188         else
189                 attr = DME_TOSHIBA_ARA_INIT_STATUS;
190
191         ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
192                                   DME_SELECTOR_INDEX_NULL, &value);
193         if (ret)
194                 return ret;
195
196         /*
197          * A nonzero init status indicates the module has finished
198          * initializing.
199          */
200         if (!value) {
201                 dev_err(&intf->dev, "invalid init status\n");
202                 return -ENODEV;
203         }
204
205         /*
206          * Extract the init status.
207          *
208          * For ES2: We need to check lowest 8 bits of 'value'.
209          * For ES3: We need to check highest 8 bits out of 32 of 'value'.
210          *
211          * FIXME: Remove ES2 support
212          */
213         if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
214                 init_status = value & 0xff;
215         else
216                 init_status = value >> 24;
217
218         /*
219          * Check if the interface is executing the quirky ES3 bootrom that
220          * requires E2EFC, CSD and CSV to be disabled and that does not
221          * support the interface-version request.
222          */
223         switch (init_status) {
224         case GB_INIT_BOOTROM_UNIPRO_BOOT_STARTED:
225         case GB_INIT_BOOTROM_FALLBACK_UNIPRO_BOOT_STARTED:
226                 intf->quirks |= GB_INTERFACE_QUIRK_NO_CPORT_FEATURES;
227                 intf->quirks |= GB_INTERFACE_QUIRK_NO_INTERFACE_VERSION;
228                 break;
229         }
230
231         /* Clear the init status. */
232         return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
233                                    DME_SELECTOR_INDEX_NULL, 0);
234 }
235
236 /* interface sysfs attributes */
237 #define gb_interface_attr(field, type)                                  \
238 static ssize_t field##_show(struct device *dev,                         \
239                             struct device_attribute *attr,              \
240                             char *buf)                                  \
241 {                                                                       \
242         struct gb_interface *intf = to_gb_interface(dev);               \
243         return scnprintf(buf, PAGE_SIZE, type"\n", intf->field);        \
244 }                                                                       \
245 static DEVICE_ATTR_RO(field)
246
247 gb_interface_attr(ddbl1_manufacturer_id, "0x%08x");
248 gb_interface_attr(ddbl1_product_id, "0x%08x");
249 gb_interface_attr(interface_id, "%u");
250 gb_interface_attr(vendor_id, "0x%08x");
251 gb_interface_attr(product_id, "0x%08x");
252 gb_interface_attr(vendor_string, "%s");
253 gb_interface_attr(product_string, "%s");
254 gb_interface_attr(serial_number, "0x%016llx");
255
256 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
257                             char *buf)
258 {
259         struct gb_interface *intf = to_gb_interface(dev);
260
261         return scnprintf(buf, PAGE_SIZE, "%u.%u\n", intf->version_major,
262                          intf->version_minor);
263 }
264 static DEVICE_ATTR_RO(version);
265
266 static ssize_t voltage_now_show(struct device *dev,
267                                 struct device_attribute *attr, char *buf)
268 {
269         struct gb_interface *intf = to_gb_interface(dev);
270         int ret;
271         u32 measurement;
272
273         ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
274                                             GB_SVC_PWRMON_TYPE_VOL,
275                                             &measurement);
276         if (ret) {
277                 dev_err(&intf->dev, "failed to get voltage sample (%d)\n", ret);
278                 return ret;
279         }
280
281         return sprintf(buf, "%u\n", measurement);
282 }
283 static DEVICE_ATTR_RO(voltage_now);
284
285 static ssize_t current_now_show(struct device *dev,
286                                 struct device_attribute *attr, char *buf)
287 {
288         struct gb_interface *intf = to_gb_interface(dev);
289         int ret;
290         u32 measurement;
291
292         ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
293                                             GB_SVC_PWRMON_TYPE_CURR,
294                                             &measurement);
295         if (ret) {
296                 dev_err(&intf->dev, "failed to get current sample (%d)\n", ret);
297                 return ret;
298         }
299
300         return sprintf(buf, "%u\n", measurement);
301 }
302 static DEVICE_ATTR_RO(current_now);
303
304 static ssize_t power_now_show(struct device *dev,
305                               struct device_attribute *attr, char *buf)
306 {
307         struct gb_interface *intf = to_gb_interface(dev);
308         int ret;
309         u32 measurement;
310
311         ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
312                                             GB_SVC_PWRMON_TYPE_PWR,
313                                             &measurement);
314         if (ret) {
315                 dev_err(&intf->dev, "failed to get power sample (%d)\n", ret);
316                 return ret;
317         }
318
319         return sprintf(buf, "%u\n", measurement);
320 }
321 static DEVICE_ATTR_RO(power_now);
322
323 static struct attribute *interface_attrs[] = {
324         &dev_attr_ddbl1_manufacturer_id.attr,
325         &dev_attr_ddbl1_product_id.attr,
326         &dev_attr_interface_id.attr,
327         &dev_attr_vendor_id.attr,
328         &dev_attr_product_id.attr,
329         &dev_attr_vendor_string.attr,
330         &dev_attr_product_string.attr,
331         &dev_attr_serial_number.attr,
332         &dev_attr_version.attr,
333         &dev_attr_voltage_now.attr,
334         &dev_attr_current_now.attr,
335         &dev_attr_power_now.attr,
336         NULL,
337 };
338 ATTRIBUTE_GROUPS(interface);
339
340
341 // FIXME, odds are you don't want to call this function, rework the caller to
342 // not need it please.
343 struct gb_interface *gb_interface_find(struct gb_host_device *hd,
344                                        u8 interface_id)
345 {
346         struct gb_interface *intf;
347
348         list_for_each_entry(intf, &hd->interfaces, links)
349                 if (intf->interface_id == interface_id)
350                         return intf;
351
352         return NULL;
353 }
354
355 static void gb_interface_release(struct device *dev)
356 {
357         struct gb_interface *intf = to_gb_interface(dev);
358
359         kfree(intf->product_string);
360         kfree(intf->vendor_string);
361
362         if (intf->control)
363                 gb_control_put(intf->control);
364
365         kfree(intf);
366 }
367
368 struct device_type greybus_interface_type = {
369         .name =         "greybus_interface",
370         .release =      gb_interface_release,
371 };
372
373 /*
374  * A Greybus module represents a user-replaceable component on an Ara
375  * phone.  An interface is the physical connection on that module.  A
376  * module may have more than one interface.
377  *
378  * Create a gb_interface structure to represent a discovered interface.
379  * The position of interface within the Endo is encoded in "interface_id"
380  * argument.
381  *
382  * Returns a pointer to the new interfce or a null pointer if a
383  * failure occurs due to memory exhaustion.
384  *
385  * Locking: Caller ensures serialisation with gb_interface_remove and
386  * gb_interface_find.
387  */
388 struct gb_interface *gb_interface_create(struct gb_host_device *hd,
389                                          u8 interface_id)
390 {
391         struct gb_interface *intf;
392
393         intf = kzalloc(sizeof(*intf), GFP_KERNEL);
394         if (!intf)
395                 return NULL;
396
397         intf->hd = hd;          /* XXX refcount? */
398         intf->interface_id = interface_id;
399         INIT_LIST_HEAD(&intf->bundles);
400         INIT_LIST_HEAD(&intf->manifest_descs);
401
402         /* Invalid device id to start with */
403         intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
404
405         intf->dev.parent = &hd->dev;
406         intf->dev.bus = &greybus_bus_type;
407         intf->dev.type = &greybus_interface_type;
408         intf->dev.groups = interface_groups;
409         intf->dev.dma_mask = hd->dev.dma_mask;
410         device_initialize(&intf->dev);
411         dev_set_name(&intf->dev, "%d-%d", hd->bus_id, interface_id);
412
413         intf->control = gb_control_create(intf);
414         if (!intf->control) {
415                 put_device(&intf->dev);
416                 return NULL;
417         }
418
419         list_add(&intf->links, &hd->interfaces);
420
421         return intf;
422 }
423
424 int gb_interface_activate(struct gb_interface *intf)
425 {
426         int ret;
427
428         ret = gb_interface_read_dme(intf);
429         if (ret)
430                 return ret;
431
432         ret = gb_interface_route_create(intf);
433         if (ret)
434                 return ret;
435
436         return 0;
437 }
438
439 void gb_interface_deactivate(struct gb_interface *intf)
440 {
441         gb_interface_route_destroy(intf);
442 }
443
444 /*
445  * Enable an interface by enabling its control connection and fetching the
446  * manifest and other information over it.
447  */
448 int gb_interface_enable(struct gb_interface *intf)
449 {
450         struct gb_bundle *bundle, *tmp;
451         int ret, size;
452         void *manifest;
453
454         ret = gb_interface_read_and_clear_init_status(intf);
455         if (ret) {
456                 dev_err(&intf->dev, "failed to clear init status: %d\n", ret);
457                 return ret;
458         }
459
460         /* Establish control connection */
461         ret = gb_control_enable(intf->control);
462         if (ret)
463                 return ret;
464
465         /* Get manifest size using control protocol on CPort */
466         size = gb_control_get_manifest_size_operation(intf);
467         if (size <= 0) {
468                 dev_err(&intf->dev, "failed to get manifest size: %d\n", size);
469
470                 if (size)
471                         ret = size;
472                 else
473                         ret =  -EINVAL;
474
475                 goto err_disable_control;
476         }
477
478         manifest = kmalloc(size, GFP_KERNEL);
479         if (!manifest) {
480                 ret = -ENOMEM;
481                 goto err_disable_control;
482         }
483
484         /* Get manifest using control protocol on CPort */
485         ret = gb_control_get_manifest_operation(intf, manifest, size);
486         if (ret) {
487                 dev_err(&intf->dev, "failed to get manifest: %d\n", ret);
488                 goto err_free_manifest;
489         }
490
491         /*
492          * Parse the manifest and build up our data structures representing
493          * what's in it.
494          */
495         if (!gb_manifest_parse(intf, manifest, size)) {
496                 dev_err(&intf->dev, "failed to parse manifest\n");
497                 ret = -EINVAL;
498                 goto err_destroy_bundles;
499         }
500
501         ret = gb_control_get_interface_version_operation(intf);
502         if (ret)
503                 goto err_destroy_bundles;
504
505         ret = gb_control_get_bundle_versions(intf->control);
506         if (ret)
507                 goto err_destroy_bundles;
508
509         kfree(manifest);
510
511         return 0;
512
513 err_destroy_bundles:
514         list_for_each_entry_safe(bundle, tmp, &intf->bundles, links)
515                 gb_bundle_destroy(bundle);
516 err_free_manifest:
517         kfree(manifest);
518 err_disable_control:
519         gb_control_disable(intf->control);
520
521         return ret;
522 }
523
524 /* Disable an interface and destroy its bundles. */
525 void gb_interface_disable(struct gb_interface *intf)
526 {
527         struct gb_bundle *bundle;
528         struct gb_bundle *next;
529
530         /*
531          * Disable the control-connection early to avoid operation timeouts
532          * when the interface is already gone.
533          */
534         if (intf->disconnected)
535                 gb_control_disable(intf->control);
536
537         list_for_each_entry_safe(bundle, next, &intf->bundles, links)
538                 gb_bundle_destroy(bundle);
539
540         gb_control_disable(intf->control);
541 }
542
543 /* Register an interface and its bundles. */
544 int gb_interface_add(struct gb_interface *intf)
545 {
546         struct gb_bundle *bundle, *tmp;
547         int ret;
548
549         ret = device_add(&intf->dev);
550         if (ret) {
551                 dev_err(&intf->dev, "failed to register interface: %d\n", ret);
552                 return ret;
553         }
554
555         dev_info(&intf->dev, "Interface added: VID=0x%08x, PID=0x%08x\n",
556                  intf->vendor_id, intf->product_id);
557         dev_info(&intf->dev, "DDBL1 Manufacturer=0x%08x, Product=0x%08x\n",
558                  intf->ddbl1_manufacturer_id, intf->ddbl1_product_id);
559
560         list_for_each_entry_safe_reverse(bundle, tmp, &intf->bundles, links) {
561                 ret = gb_bundle_add(bundle);
562                 if (ret) {
563                         gb_bundle_destroy(bundle);
564                         continue;
565                 }
566         }
567
568         return 0;
569 }
570
571 /* Deregister an interface and drop its reference. */
572 void gb_interface_remove(struct gb_interface *intf)
573 {
574         if (device_is_registered(&intf->dev)) {
575                 device_del(&intf->dev);
576                 dev_info(&intf->dev, "Interface removed\n");
577         }
578
579         list_del(&intf->links);
580
581         put_device(&intf->dev);
582 }