2 * Greybus interface code
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
13 #define GB_INTERFACE_DEVICE_ID_BAD 0xff
15 /* Don't-care selector index */
16 #define DME_SELECTOR_INDEX_NULL 0
19 /* FIXME: remove ES2 support and DME_T_TST_SRC_INCREMENT */
20 #define DME_T_TST_SRC_INCREMENT 0x4083
22 #define DME_DDBL1_MANUFACTURERID 0x5003
23 #define DME_DDBL1_PRODUCTID 0x5004
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
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
38 static int gb_interface_dme_attr_get(struct gb_interface *intf,
41 return gb_svc_dme_peer_get(intf->hd->svc, intf->interface_id,
42 attr, DME_SELECTOR_INDEX_NULL, val);
45 static int gb_interface_read_ara_dme(struct gb_interface *intf)
51 * Unless this is a Toshiba bridge, bail out until we have defined
52 * standard Ara attributes.
54 if (intf->ddbl1_manufacturer_id != TOSHIBA_DMID) {
55 dev_err(&intf->dev, "unknown manufacturer %08x\n",
56 intf->ddbl1_manufacturer_id);
60 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_VID,
65 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_PID,
70 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN0, &sn0);
74 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_ARA_SN1, &sn1);
78 intf->serial_number = (u64)sn1 << 32 | sn0;
83 static int gb_interface_read_dme(struct gb_interface *intf)
87 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_MANUFACTURERID,
88 &intf->ddbl1_manufacturer_id);
92 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_PRODUCTID,
93 &intf->ddbl1_product_id);
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;
103 return gb_interface_read_ara_dme(intf);
106 static int gb_interface_route_create(struct gb_interface *intf)
108 struct gb_svc *svc = intf->hd->svc;
109 u8 intf_id = intf->interface_id;
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,
118 dev_err(&intf->dev, "failed to allocate device id: %d\n", ret);
123 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
125 dev_err(&intf->dev, "failed to set device id %u: %d\n",
130 /* FIXME: Hard-coded AP device id. */
131 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_SVC_DEVICE_ID_AP,
134 dev_err(&intf->dev, "failed to create route: %d\n", ret);
135 goto err_svc_id_free;
138 intf->device_id = device_id;
144 * XXX Should we tell SVC that this id doesn't belong to interface
148 ida_simple_remove(&svc->device_id_map, device_id);
153 static void gb_interface_route_destroy(struct gb_interface *intf)
155 struct gb_svc *svc = intf->hd->svc;
157 if (intf->device_id == GB_INTERFACE_DEVICE_ID_BAD)
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;
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.
170 * FIXME: This is module-hardware dependent and needs to be extended for every
171 * type of module we want to support.
173 static int gb_interface_read_and_clear_init_status(struct gb_interface *intf)
175 struct gb_host_device *hd = intf->hd;
182 * ES2 bridges use T_TstSrcIncrement for the init status.
184 * FIXME: Remove ES2 support
186 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
187 attr = DME_T_TST_SRC_INCREMENT;
189 attr = DME_TOSHIBA_ARA_INIT_STATUS;
191 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
192 DME_SELECTOR_INDEX_NULL, &value);
197 * A nonzero init status indicates the module has finished
201 dev_err(&intf->dev, "invalid init status\n");
206 * Extract the init status.
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'.
211 * FIXME: Remove ES2 support
213 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
214 init_status = value & 0xff;
216 init_status = value >> 24;
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.
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;
231 /* Clear the init status. */
232 return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
233 DME_SELECTOR_INDEX_NULL, 0);
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, \
242 struct gb_interface *intf = to_gb_interface(dev); \
243 return scnprintf(buf, PAGE_SIZE, type"\n", intf->field); \
245 static DEVICE_ATTR_RO(field)
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");
256 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
259 struct gb_interface *intf = to_gb_interface(dev);
261 return scnprintf(buf, PAGE_SIZE, "%u.%u\n", intf->version_major,
262 intf->version_minor);
264 static DEVICE_ATTR_RO(version);
266 static ssize_t voltage_now_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 struct gb_interface *intf = to_gb_interface(dev);
273 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
274 GB_SVC_PWRMON_TYPE_VOL,
277 dev_err(&intf->dev, "failed to get voltage sample (%d)\n", ret);
281 return sprintf(buf, "%u\n", measurement);
283 static DEVICE_ATTR_RO(voltage_now);
285 static ssize_t current_now_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct gb_interface *intf = to_gb_interface(dev);
292 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
293 GB_SVC_PWRMON_TYPE_CURR,
296 dev_err(&intf->dev, "failed to get current sample (%d)\n", ret);
300 return sprintf(buf, "%u\n", measurement);
302 static DEVICE_ATTR_RO(current_now);
304 static ssize_t power_now_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 struct gb_interface *intf = to_gb_interface(dev);
311 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
312 GB_SVC_PWRMON_TYPE_PWR,
315 dev_err(&intf->dev, "failed to get power sample (%d)\n", ret);
319 return sprintf(buf, "%u\n", measurement);
321 static DEVICE_ATTR_RO(power_now);
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,
338 ATTRIBUTE_GROUPS(interface);
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,
346 struct gb_interface *intf;
348 list_for_each_entry(intf, &hd->interfaces, links)
349 if (intf->interface_id == interface_id)
355 static void gb_interface_release(struct device *dev)
357 struct gb_interface *intf = to_gb_interface(dev);
359 kfree(intf->product_string);
360 kfree(intf->vendor_string);
363 gb_control_put(intf->control);
368 struct device_type greybus_interface_type = {
369 .name = "greybus_interface",
370 .release = gb_interface_release,
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.
378 * Create a gb_interface structure to represent a discovered interface.
379 * The position of interface within the Endo is encoded in "interface_id"
382 * Returns a pointer to the new interfce or a null pointer if a
383 * failure occurs due to memory exhaustion.
385 * Locking: Caller ensures serialisation with gb_interface_remove and
388 struct gb_interface *gb_interface_create(struct gb_host_device *hd,
391 struct gb_interface *intf;
393 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
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);
402 /* Invalid device id to start with */
403 intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
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);
413 intf->control = gb_control_create(intf);
414 if (!intf->control) {
415 put_device(&intf->dev);
419 list_add(&intf->links, &hd->interfaces);
424 int gb_interface_activate(struct gb_interface *intf)
428 ret = gb_interface_read_dme(intf);
432 ret = gb_interface_route_create(intf);
439 void gb_interface_deactivate(struct gb_interface *intf)
441 gb_interface_route_destroy(intf);
445 * Enable an interface by enabling its control connection and fetching the
446 * manifest and other information over it.
448 int gb_interface_enable(struct gb_interface *intf)
450 struct gb_bundle *bundle, *tmp;
454 ret = gb_interface_read_and_clear_init_status(intf);
456 dev_err(&intf->dev, "failed to clear init status: %d\n", ret);
460 /* Establish control connection */
461 ret = gb_control_enable(intf->control);
465 /* Get manifest size using control protocol on CPort */
466 size = gb_control_get_manifest_size_operation(intf);
468 dev_err(&intf->dev, "failed to get manifest size: %d\n", size);
475 goto err_disable_control;
478 manifest = kmalloc(size, GFP_KERNEL);
481 goto err_disable_control;
484 /* Get manifest using control protocol on CPort */
485 ret = gb_control_get_manifest_operation(intf, manifest, size);
487 dev_err(&intf->dev, "failed to get manifest: %d\n", ret);
488 goto err_free_manifest;
492 * Parse the manifest and build up our data structures representing
495 if (!gb_manifest_parse(intf, manifest, size)) {
496 dev_err(&intf->dev, "failed to parse manifest\n");
498 goto err_destroy_bundles;
501 ret = gb_control_get_interface_version_operation(intf);
503 goto err_destroy_bundles;
505 ret = gb_control_get_bundle_versions(intf->control);
507 goto err_destroy_bundles;
514 list_for_each_entry_safe(bundle, tmp, &intf->bundles, links)
515 gb_bundle_destroy(bundle);
519 gb_control_disable(intf->control);
524 /* Disable an interface and destroy its bundles. */
525 void gb_interface_disable(struct gb_interface *intf)
527 struct gb_bundle *bundle;
528 struct gb_bundle *next;
531 * Disable the control-connection early to avoid operation timeouts
532 * when the interface is already gone.
534 if (intf->disconnected)
535 gb_control_disable(intf->control);
537 list_for_each_entry_safe(bundle, next, &intf->bundles, links)
538 gb_bundle_destroy(bundle);
540 gb_control_disable(intf->control);
543 /* Register an interface and its bundles. */
544 int gb_interface_add(struct gb_interface *intf)
546 struct gb_bundle *bundle, *tmp;
549 ret = device_add(&intf->dev);
551 dev_err(&intf->dev, "failed to register interface: %d\n", ret);
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);
560 list_for_each_entry_safe_reverse(bundle, tmp, &intf->bundles, links) {
561 ret = gb_bundle_add(bundle);
563 gb_bundle_destroy(bundle);
571 /* Deregister an interface and drop its reference. */
572 void gb_interface_remove(struct gb_interface *intf)
574 if (device_is_registered(&intf->dev)) {
575 device_del(&intf->dev);
576 dev_info(&intf->dev, "Interface removed\n");
579 list_del(&intf->links);
581 put_device(&intf->dev);