2 * Greybus interface code
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
12 /* interface sysfs attributes */
13 #define gb_interface_attr(field, type) \
14 static ssize_t field##_show(struct device *dev, \
15 struct device_attribute *attr, \
18 struct gb_interface *intf = to_gb_interface(dev); \
19 return scnprintf(buf, PAGE_SIZE, "%"#type"\n", intf->field); \
21 static DEVICE_ATTR_RO(field)
23 gb_interface_attr(interface_id, u);
24 gb_interface_attr(vendor_id, x);
25 gb_interface_attr(product_id, x);
26 gb_interface_attr(vendor_string, s);
27 gb_interface_attr(product_string, s);
29 static struct attribute *interface_attrs[] = {
30 &dev_attr_interface_id.attr,
31 &dev_attr_vendor_id.attr,
32 &dev_attr_product_id.attr,
33 &dev_attr_vendor_string.attr,
34 &dev_attr_product_string.attr,
37 ATTRIBUTE_GROUPS(interface);
40 /* XXX This could be per-host device */
41 static DEFINE_SPINLOCK(gb_interfaces_lock);
43 // FIXME, odds are you don't want to call this function, rework the caller to
44 // not need it please.
45 struct gb_interface *gb_interface_find(struct gb_host_device *hd,
48 struct gb_interface *intf;
50 list_for_each_entry(intf, &hd->interfaces, links)
51 if (intf->interface_id == interface_id)
57 static void gb_interface_release(struct device *dev)
59 struct gb_interface *intf = to_gb_interface(dev);
61 kfree(intf->product_string);
62 kfree(intf->vendor_string);
67 struct device_type greybus_interface_type = {
68 .name = "greybus_interface",
69 .release = gb_interface_release,
73 * A Greybus module represents a user-replaceable component on an Ara
74 * phone. An interface is the physical connection on that module. A
75 * module may have more than one interface.
77 * Create a gb_interface structure to represent a discovered interface.
78 * The position of interface within the Endo is encoded in "interface_id"
81 * Returns a pointer to the new interfce or a null pointer if a
82 * failure occurs due to memory exhaustion.
84 struct gb_interface *gb_interface_create(struct gb_host_device *hd,
87 struct gb_interface *intf;
90 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
94 intf->hd = hd; /* XXX refcount? */
95 intf->interface_id = interface_id;
96 INIT_LIST_HEAD(&intf->bundles);
97 INIT_LIST_HEAD(&intf->manifest_descs);
99 /* Invalid device id to start with */
100 intf->device_id = GB_DEVICE_ID_BAD;
102 intf->dev.parent = &hd->dev;
103 intf->dev.bus = &greybus_bus_type;
104 intf->dev.type = &greybus_interface_type;
105 intf->dev.groups = interface_groups;
106 intf->dev.dma_mask = hd->dev.dma_mask;
107 device_initialize(&intf->dev);
108 dev_set_name(&intf->dev, "%d-%d", hd->bus_id, interface_id);
110 retval = device_add(&intf->dev);
112 pr_err("failed to add interface %u\n", interface_id);
116 spin_lock_irq(&gb_interfaces_lock);
117 list_add(&intf->links, &hd->interfaces);
118 spin_unlock_irq(&gb_interfaces_lock);
123 put_device(&intf->dev);
128 * Tear down a previously set up interface.
130 void gb_interface_remove(struct gb_interface *intf)
132 struct gb_bundle *bundle;
133 struct gb_bundle *next;
138 spin_lock_irq(&gb_interfaces_lock);
139 list_del(&intf->links);
140 spin_unlock_irq(&gb_interfaces_lock);
142 list_for_each_entry_safe(bundle, next, &intf->bundles, links)
143 gb_bundle_destroy(bundle);
146 gb_connection_destroy(intf->control->connection);
148 device_unregister(&intf->dev);
151 void gb_interfaces_remove(struct gb_host_device *hd)
153 struct gb_interface *intf, *temp;
155 list_for_each_entry_safe(intf, temp, &hd->interfaces, links)
156 gb_interface_remove(intf);
162 * Create connection for control CPort and then request/parse manifest.
163 * Finally initialize all the bundles to set routes via SVC and initialize all
166 int gb_interface_init(struct gb_interface *intf, u8 device_id)
168 struct gb_connection *connection;
172 intf->device_id = device_id;
174 /* Establish control CPort connection */
175 connection = gb_connection_create_dynamic(intf, NULL,
177 GREYBUS_PROTOCOL_CONTROL);
179 dev_err(&intf->dev, "failed to create control connection\n");
183 /* Get manifest size using control protocol on CPort */
184 size = gb_control_get_manifest_size_operation(intf);
186 dev_err(&intf->dev, "%s: Failed to get manifest size (%d)\n",
194 manifest = kmalloc(size, GFP_KERNEL);
198 /* Get manifest using control protocol on CPort */
199 ret = gb_control_get_manifest_operation(intf, manifest, size);
201 dev_err(&intf->dev, "%s: Failed to get manifest\n", __func__);
206 * Parse the manifest and build up our data structures representing
209 if (!gb_manifest_parse(intf, manifest, size)) {
210 dev_err(&intf->dev, "%s: Failed to parse manifest\n", __func__);
217 * We've successfully parsed the manifest. Now we need to
218 * allocate CPort Id's for connecting to the CPorts found on
219 * other modules. For each of these, establish a connection
220 * between the local and remote CPorts (including
221 * configuring the switch to allow them to communicate).