Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 1 | /* |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 2 | * Greybus bundles |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 3 | * |
Greg Kroah-Hartman | 75052a5 | 2015-04-13 19:48:37 +0200 | [diff] [blame] | 4 | * Copyright 2014-2015 Google Inc. |
| 5 | * Copyright 2014-2015 Linaro Ltd. |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
| 10 | #include "greybus.h" |
| 11 | |
Johan Hovold | 4396c00 | 2015-11-25 15:58:59 +0100 | [diff] [blame] | 12 | static ssize_t bundle_class_show(struct device *dev, |
| 13 | struct device_attribute *attr, char *buf) |
Viresh Kumar | 9f5f30e712 | 2015-04-01 20:32:04 +0530 | [diff] [blame] | 14 | { |
| 15 | struct gb_bundle *bundle = to_gb_bundle(dev); |
| 16 | |
Johan Hovold | 2b14dab | 2015-11-25 15:59:00 +0100 | [diff] [blame] | 17 | return sprintf(buf, "0x%02x\n", bundle->class); |
Viresh Kumar | 9f5f30e712 | 2015-04-01 20:32:04 +0530 | [diff] [blame] | 18 | } |
Johan Hovold | 4396c00 | 2015-11-25 15:58:59 +0100 | [diff] [blame] | 19 | static DEVICE_ATTR_RO(bundle_class); |
Viresh Kumar | 9f5f30e712 | 2015-04-01 20:32:04 +0530 | [diff] [blame] | 20 | |
Johan Hovold | a97015c | 2015-11-25 15:59:01 +0100 | [diff] [blame] | 21 | static ssize_t bundle_id_show(struct device *dev, |
| 22 | struct device_attribute *attr, char *buf) |
| 23 | { |
| 24 | struct gb_bundle *bundle = to_gb_bundle(dev); |
| 25 | |
| 26 | return sprintf(buf, "%u\n", bundle->id); |
| 27 | } |
| 28 | static DEVICE_ATTR_RO(bundle_id); |
| 29 | |
Greg Kroah-Hartman | 75052a5 | 2015-04-13 19:48:37 +0200 | [diff] [blame] | 30 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 31 | char *buf) |
| 32 | { |
| 33 | struct gb_bundle *bundle = to_gb_bundle(dev); |
| 34 | |
| 35 | if (bundle->state == NULL) |
| 36 | return sprintf(buf, "\n"); |
| 37 | |
| 38 | return sprintf(buf, "%s\n", bundle->state); |
| 39 | } |
| 40 | |
| 41 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
| 42 | const char *buf, size_t size) |
| 43 | { |
| 44 | struct gb_bundle *bundle = to_gb_bundle(dev); |
| 45 | |
| 46 | kfree(bundle->state); |
Alex Elder | 22fd2a8 | 2015-04-17 14:41:47 -0500 | [diff] [blame] | 47 | bundle->state = kstrdup(buf, GFP_KERNEL); |
Greg Kroah-Hartman | 75052a5 | 2015-04-13 19:48:37 +0200 | [diff] [blame] | 48 | if (!bundle->state) |
| 49 | return -ENOMEM; |
| 50 | |
Greg Kroah-Hartman | 75052a5 | 2015-04-13 19:48:37 +0200 | [diff] [blame] | 51 | /* Tell userspace that the file contents changed */ |
| 52 | sysfs_notify(&bundle->dev.kobj, NULL, "state"); |
| 53 | |
| 54 | return size; |
| 55 | } |
| 56 | static DEVICE_ATTR_RW(state); |
| 57 | |
| 58 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 59 | static struct attribute *bundle_attrs[] = { |
Johan Hovold | 4396c00 | 2015-11-25 15:58:59 +0100 | [diff] [blame] | 60 | &dev_attr_bundle_class.attr, |
Johan Hovold | a97015c | 2015-11-25 15:59:01 +0100 | [diff] [blame] | 61 | &dev_attr_bundle_id.attr, |
Greg Kroah-Hartman | 75052a5 | 2015-04-13 19:48:37 +0200 | [diff] [blame] | 62 | &dev_attr_state.attr, |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 63 | NULL, |
| 64 | }; |
| 65 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 66 | ATTRIBUTE_GROUPS(bundle); |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 67 | |
Johan Hovold | 1db1b24 | 2015-12-07 15:05:40 +0100 | [diff] [blame] | 68 | static struct gb_bundle *gb_bundle_find(struct gb_interface *intf, |
| 69 | u8 bundle_id) |
| 70 | { |
| 71 | struct gb_bundle *bundle; |
| 72 | |
| 73 | list_for_each_entry(bundle, &intf->bundles, links) { |
| 74 | if (bundle->id == bundle_id) |
| 75 | return bundle; |
| 76 | } |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 81 | static void gb_bundle_release(struct device *dev) |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 82 | { |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 83 | struct gb_bundle *bundle = to_gb_bundle(dev); |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 84 | |
Greg Kroah-Hartman | 75052a5 | 2015-04-13 19:48:37 +0200 | [diff] [blame] | 85 | kfree(bundle->state); |
Johan Hovold | 98fdf5a | 2016-01-21 17:34:09 +0100 | [diff] [blame] | 86 | kfree(bundle->cport_desc); |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 87 | kfree(bundle); |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 88 | } |
| 89 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 90 | struct device_type greybus_bundle_type = { |
| 91 | .name = "greybus_bundle", |
| 92 | .release = gb_bundle_release, |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 93 | }; |
| 94 | |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 95 | /* |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 96 | * Create a gb_bundle structure to represent a discovered |
| 97 | * bundle. Returns a pointer to the new bundle or a null |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 98 | * pointer if a failure occurs due to memory exhaustion. |
| 99 | */ |
Viresh Kumar | 7c183f7 | 2015-04-01 20:32:00 +0530 | [diff] [blame] | 100 | struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id, |
Viresh Kumar | 88e6d37 | 2015-04-06 15:49:36 +0530 | [diff] [blame] | 101 | u8 class) |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 102 | { |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 103 | struct gb_bundle *bundle; |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 104 | |
Alex Elder | 8267616b | 2015-06-09 17:42:57 -0500 | [diff] [blame] | 105 | /* |
| 106 | * Reject any attempt to reuse a bundle id. We initialize |
| 107 | * these serially, so there's no need to worry about keeping |
| 108 | * the interface bundle list locked here. |
| 109 | */ |
| 110 | if (gb_bundle_find(intf, bundle_id)) { |
Johan Hovold | a234792 | 2015-12-07 15:05:41 +0100 | [diff] [blame] | 111 | dev_err(&intf->dev, "duplicate bundle id %u\n", bundle_id); |
Alex Elder | 8267616b | 2015-06-09 17:42:57 -0500 | [diff] [blame] | 112 | return NULL; |
| 113 | } |
| 114 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 115 | bundle = kzalloc(sizeof(*bundle), GFP_KERNEL); |
| 116 | if (!bundle) |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 117 | return NULL; |
| 118 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 119 | bundle->intf = intf; |
Viresh Kumar | 7c183f7 | 2015-04-01 20:32:00 +0530 | [diff] [blame] | 120 | bundle->id = bundle_id; |
Viresh Kumar | 88e6d37 | 2015-04-06 15:49:36 +0530 | [diff] [blame] | 121 | bundle->class = class; |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 122 | INIT_LIST_HEAD(&bundle->connections); |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 123 | |
Greg Kroah-Hartman | 4ab9b3c | 2014-12-19 14:56:31 -0800 | [diff] [blame] | 124 | bundle->dev.parent = &intf->dev; |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 125 | bundle->dev.bus = &greybus_bus_type; |
| 126 | bundle->dev.type = &greybus_bundle_type; |
| 127 | bundle->dev.groups = bundle_groups; |
| 128 | device_initialize(&bundle->dev); |
Johan Hovold | f0172c7 | 2015-11-25 15:59:05 +0100 | [diff] [blame] | 129 | dev_set_name(&bundle->dev, "%s.%d", dev_name(&intf->dev), bundle_id); |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 130 | |
Viresh Kumar | 928f2ab | 2015-06-04 18:16:45 +0530 | [diff] [blame] | 131 | list_add(&bundle->links, &intf->bundles); |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 132 | |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 133 | return bundle; |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Johan Hovold | a7e36d0 | 2015-12-07 15:05:43 +0100 | [diff] [blame] | 136 | int gb_bundle_add(struct gb_bundle *bundle) |
| 137 | { |
| 138 | int ret; |
| 139 | |
| 140 | ret = device_add(&bundle->dev); |
| 141 | if (ret) { |
| 142 | dev_err(&bundle->dev, "failed to register bundle: %d\n", ret); |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 149 | /* |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 150 | * Tear down a previously set up bundle. |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 151 | */ |
Alex Elder | fe53b45 | 2015-06-12 10:21:11 -0500 | [diff] [blame] | 152 | void gb_bundle_destroy(struct gb_bundle *bundle) |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 153 | { |
Johan Hovold | a7e36d0 | 2015-12-07 15:05:43 +0100 | [diff] [blame] | 154 | if (device_is_registered(&bundle->dev)) |
| 155 | device_del(&bundle->dev); |
| 156 | |
Alex Elder | fe53b45 | 2015-06-12 10:21:11 -0500 | [diff] [blame] | 157 | list_del(&bundle->links); |
Johan Hovold | 48d7077 | 2015-02-13 11:28:09 +0800 | [diff] [blame] | 158 | |
Johan Hovold | a7e36d0 | 2015-12-07 15:05:43 +0100 | [diff] [blame] | 159 | put_device(&bundle->dev); |
Alex Elder | 8c12cde | 2014-10-01 21:54:12 -0500 | [diff] [blame] | 160 | } |