Greg Kroah-Hartman | eb50fd3 | 2017-11-07 14:58:41 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Greybus Host Device |
| 4 | * |
| 5 | * Copyright 2014-2015 Google Inc. |
| 6 | * Copyright 2014-2015 Linaro Ltd. |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/slab.h> |
Greg Kroah-Hartman | ec0ad86 | 2019-08-25 07:54:27 +0200 | [diff] [blame] | 11 | #include <linux/greybus.h> |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 12 | |
Alex Elder | 1f79046 | 2016-05-23 23:05:30 -0500 | [diff] [blame] | 13 | #include "greybus_trace.h" |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 14 | |
Viresh Kumar | 56278c7 | 2016-06-09 16:34:40 +0530 | [diff] [blame] | 15 | EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_create); |
| 16 | EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_release); |
| 17 | EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_add); |
| 18 | EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_del); |
Alex Elder | 495787a | 2016-06-03 15:55:38 -0500 | [diff] [blame] | 19 | EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_in); |
Viresh Kumar | 56278c7 | 2016-06-09 16:34:40 +0530 | [diff] [blame] | 20 | EXPORT_TRACEPOINT_SYMBOL_GPL(gb_message_submit); |
Alex Elder | 495787a | 2016-06-03 15:55:38 -0500 | [diff] [blame] | 21 | |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 22 | static struct ida gb_hd_bus_id_map; |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 23 | |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 24 | int gb_hd_output(struct gb_host_device *hd, void *req, u16 size, u8 cmd, |
| 25 | bool async) |
| 26 | { |
| 27 | if (!hd || !hd->driver || !hd->driver->output) |
| 28 | return -EINVAL; |
| 29 | return hd->driver->output(hd, req, size, cmd, async); |
| 30 | } |
| 31 | EXPORT_SYMBOL_GPL(gb_hd_output); |
| 32 | |
Sandeep Patil | fbbd2b7 | 2016-04-29 07:13:23 -0700 | [diff] [blame] | 33 | static ssize_t bus_id_show(struct device *dev, |
Greg Kroah-Hartman | a11ac9e | 2019-08-25 07:54:23 +0200 | [diff] [blame] | 34 | struct device_attribute *attr, char *buf) |
Sandeep Patil | fbbd2b7 | 2016-04-29 07:13:23 -0700 | [diff] [blame] | 35 | { |
| 36 | struct gb_host_device *hd = to_gb_host_device(dev); |
| 37 | |
| 38 | return sprintf(buf, "%d\n", hd->bus_id); |
| 39 | } |
| 40 | static DEVICE_ATTR_RO(bus_id); |
| 41 | |
| 42 | static struct attribute *bus_attrs[] = { |
| 43 | &dev_attr_bus_id.attr, |
| 44 | NULL |
| 45 | }; |
| 46 | ATTRIBUTE_GROUPS(bus); |
| 47 | |
Johan Hovold | 0506150 | 2016-05-11 10:18:01 +0200 | [diff] [blame] | 48 | int gb_hd_cport_reserve(struct gb_host_device *hd, u16 cport_id) |
| 49 | { |
| 50 | struct ida *id_map = &hd->cport_id_map; |
| 51 | int ret; |
| 52 | |
| 53 | ret = ida_simple_get(id_map, cport_id, cport_id + 1, GFP_KERNEL); |
| 54 | if (ret < 0) { |
| 55 | dev_err(&hd->dev, "failed to reserve cport %u\n", cport_id); |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | EXPORT_SYMBOL_GPL(gb_hd_cport_reserve); |
| 62 | |
Vaibhav Agarwal | 29a822b | 2016-05-27 10:49:23 +0530 | [diff] [blame] | 63 | void gb_hd_cport_release_reserved(struct gb_host_device *hd, u16 cport_id) |
| 64 | { |
| 65 | struct ida *id_map = &hd->cport_id_map; |
| 66 | |
| 67 | ida_simple_remove(id_map, cport_id); |
| 68 | } |
| 69 | EXPORT_SYMBOL_GPL(gb_hd_cport_release_reserved); |
| 70 | |
Johan Hovold | 74a5d93 | 2016-05-11 10:17:59 +0200 | [diff] [blame] | 71 | /* Locking: Caller guarantees serialisation */ |
Johan Hovold | f2aae1c | 2016-05-11 10:18:02 +0200 | [diff] [blame] | 72 | int gb_hd_cport_allocate(struct gb_host_device *hd, int cport_id, |
Greg Kroah-Hartman | a11ac9e | 2019-08-25 07:54:23 +0200 | [diff] [blame] | 73 | unsigned long flags) |
Johan Hovold | 74a5d93 | 2016-05-11 10:17:59 +0200 | [diff] [blame] | 74 | { |
| 75 | struct ida *id_map = &hd->cport_id_map; |
| 76 | int ida_start, ida_end; |
| 77 | |
Johan Hovold | f2aae1c | 2016-05-11 10:18:02 +0200 | [diff] [blame] | 78 | if (hd->driver->cport_allocate) |
| 79 | return hd->driver->cport_allocate(hd, cport_id, flags); |
| 80 | |
Johan Hovold | 74a5d93 | 2016-05-11 10:17:59 +0200 | [diff] [blame] | 81 | if (cport_id < 0) { |
| 82 | ida_start = 0; |
| 83 | ida_end = hd->num_cports; |
| 84 | } else if (cport_id < hd->num_cports) { |
| 85 | ida_start = cport_id; |
| 86 | ida_end = cport_id + 1; |
| 87 | } else { |
| 88 | dev_err(&hd->dev, "cport %d not available\n", cport_id); |
| 89 | return -EINVAL; |
| 90 | } |
| 91 | |
| 92 | return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL); |
| 93 | } |
| 94 | |
| 95 | /* Locking: Caller guarantees serialisation */ |
| 96 | void gb_hd_cport_release(struct gb_host_device *hd, u16 cport_id) |
| 97 | { |
Johan Hovold | f2aae1c | 2016-05-11 10:18:02 +0200 | [diff] [blame] | 98 | if (hd->driver->cport_release) { |
| 99 | hd->driver->cport_release(hd, cport_id); |
| 100 | return; |
| 101 | } |
| 102 | |
Johan Hovold | 74a5d93 | 2016-05-11 10:17:59 +0200 | [diff] [blame] | 103 | ida_simple_remove(&hd->cport_id_map, cport_id); |
| 104 | } |
| 105 | |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 106 | static void gb_hd_release(struct device *dev) |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 107 | { |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 108 | struct gb_host_device *hd = to_gb_host_device(dev); |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 109 | |
Johan Hovold | 1282317 | 2016-05-27 18:23:01 +0200 | [diff] [blame] | 110 | trace_gb_hd_release(hd); |
| 111 | |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 112 | if (hd->svc) |
| 113 | gb_svc_put(hd->svc); |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 114 | ida_simple_remove(&gb_hd_bus_id_map, hd->bus_id); |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 115 | ida_destroy(&hd->cport_id_map); |
| 116 | kfree(hd); |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 119 | struct device_type greybus_hd_type = { |
| 120 | .name = "greybus_host_device", |
| 121 | .release = gb_hd_release, |
| 122 | }; |
| 123 | |
Johan Hovold | d6e139b | 2015-11-03 18:03:25 +0100 | [diff] [blame] | 124 | struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver, |
Greg Kroah-Hartman | a11ac9e | 2019-08-25 07:54:23 +0200 | [diff] [blame] | 125 | struct device *parent, |
| 126 | size_t buffer_size_max, |
| 127 | size_t num_cports) |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 128 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 129 | struct gb_host_device *hd; |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 130 | int ret; |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Validate that the driver implements all of the callbacks |
| 134 | * so that we don't have to every time we make them. |
| 135 | */ |
| 136 | if ((!driver->message_send) || (!driver->message_cancel)) { |
Johan Hovold | b427572 | 2016-02-11 13:52:47 +0100 | [diff] [blame] | 137 | dev_err(parent, "mandatory hd-callbacks missing\n"); |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 138 | return ERR_PTR(-EINVAL); |
| 139 | } |
| 140 | |
| 141 | if (buffer_size_max < GB_OPERATION_MESSAGE_SIZE_MIN) { |
| 142 | dev_err(parent, "greybus host-device buffers too small\n"); |
| 143 | return ERR_PTR(-EINVAL); |
| 144 | } |
| 145 | |
Johan Hovold | bfe2c99 | 2015-11-22 10:12:58 +0100 | [diff] [blame] | 146 | if (num_cports == 0 || num_cports > CPORT_ID_MAX + 1) { |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 147 | dev_err(parent, "Invalid number of CPorts: %zu\n", num_cports); |
| 148 | return ERR_PTR(-EINVAL); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Make sure to never allocate messages larger than what the Greybus |
| 153 | * protocol supports. |
| 154 | */ |
| 155 | if (buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) { |
| 156 | dev_warn(parent, "limiting buffer size to %u\n", |
| 157 | GB_OPERATION_MESSAGE_SIZE_MAX); |
| 158 | buffer_size_max = GB_OPERATION_MESSAGE_SIZE_MAX; |
| 159 | } |
| 160 | |
| 161 | hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL); |
| 162 | if (!hd) |
| 163 | return ERR_PTR(-ENOMEM); |
| 164 | |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 165 | ret = ida_simple_get(&gb_hd_bus_id_map, 1, 0, GFP_KERNEL); |
| 166 | if (ret < 0) { |
| 167 | kfree(hd); |
| 168 | return ERR_PTR(ret); |
| 169 | } |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 170 | hd->bus_id = ret; |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 171 | |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 172 | hd->driver = driver; |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 173 | INIT_LIST_HEAD(&hd->modules); |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 174 | INIT_LIST_HEAD(&hd->connections); |
| 175 | ida_init(&hd->cport_id_map); |
| 176 | hd->buffer_size_max = buffer_size_max; |
| 177 | hd->num_cports = num_cports; |
| 178 | |
Johan Hovold | d4c80ba | 2015-12-07 15:05:35 +0100 | [diff] [blame] | 179 | hd->dev.parent = parent; |
| 180 | hd->dev.bus = &greybus_bus_type; |
| 181 | hd->dev.type = &greybus_hd_type; |
Sandeep Patil | fbbd2b7 | 2016-04-29 07:13:23 -0700 | [diff] [blame] | 182 | hd->dev.groups = bus_groups; |
Johan Hovold | d4c80ba | 2015-12-07 15:05:35 +0100 | [diff] [blame] | 183 | hd->dev.dma_mask = hd->dev.parent->dma_mask; |
| 184 | device_initialize(&hd->dev); |
| 185 | dev_set_name(&hd->dev, "greybus%d", hd->bus_id); |
| 186 | |
Alex Elder | 1f79046 | 2016-05-23 23:05:30 -0500 | [diff] [blame] | 187 | trace_gb_hd_create(hd); |
| 188 | |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 189 | hd->svc = gb_svc_create(hd); |
| 190 | if (!hd->svc) { |
| 191 | dev_err(&hd->dev, "failed to create svc\n"); |
Johan Hovold | 2c84894 | 2015-12-07 15:05:36 +0100 | [diff] [blame] | 192 | put_device(&hd->dev); |
| 193 | return ERR_PTR(-ENOMEM); |
Johan Hovold | 5ef3238 | 2015-11-25 15:59:18 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Johan Hovold | 2c84894 | 2015-12-07 15:05:36 +0100 | [diff] [blame] | 196 | return hd; |
Johan Hovold | 5ef3238 | 2015-11-25 15:59:18 +0100 | [diff] [blame] | 197 | } |
Johan Hovold | 2c84894 | 2015-12-07 15:05:36 +0100 | [diff] [blame] | 198 | EXPORT_SYMBOL_GPL(gb_hd_create); |
Johan Hovold | 5ef3238 | 2015-11-25 15:59:18 +0100 | [diff] [blame] | 199 | |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 200 | int gb_hd_add(struct gb_host_device *hd) |
| 201 | { |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 202 | int ret; |
| 203 | |
| 204 | ret = device_add(&hd->dev); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 208 | ret = gb_svc_add(hd->svc); |
Johan Hovold | 0bf1f24 | 2015-12-07 15:05:34 +0100 | [diff] [blame] | 209 | if (ret) { |
Johan Hovold | 0bf1f24 | 2015-12-07 15:05:34 +0100 | [diff] [blame] | 210 | device_del(&hd->dev); |
| 211 | return ret; |
| 212 | } |
| 213 | |
Alex Elder | 1f79046 | 2016-05-23 23:05:30 -0500 | [diff] [blame] | 214 | trace_gb_hd_add(hd); |
| 215 | |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 216 | return 0; |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 217 | } |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 218 | EXPORT_SYMBOL_GPL(gb_hd_add); |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 219 | |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 220 | void gb_hd_del(struct gb_host_device *hd) |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 221 | { |
Alex Elder | 1f79046 | 2016-05-23 23:05:30 -0500 | [diff] [blame] | 222 | trace_gb_hd_del(hd); |
| 223 | |
Johan Hovold | 24988d3a | 2016-01-28 12:43:29 +0100 | [diff] [blame] | 224 | /* |
| 225 | * Tear down the svc and flush any on-going hotplug processing before |
| 226 | * removing the remaining interfaces. |
| 227 | */ |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 228 | gb_svc_del(hd->svc); |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 229 | |
| 230 | device_del(&hd->dev); |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 231 | } |
| 232 | EXPORT_SYMBOL_GPL(gb_hd_del); |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 233 | |
David Lin | 1f77b36 | 2016-07-12 17:41:21 -0700 | [diff] [blame] | 234 | void gb_hd_shutdown(struct gb_host_device *hd) |
| 235 | { |
| 236 | gb_svc_del(hd->svc); |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(gb_hd_shutdown); |
| 239 | |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 240 | void gb_hd_put(struct gb_host_device *hd) |
| 241 | { |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 242 | put_device(&hd->dev); |
Johan Hovold | 7bc6faa | 2015-11-03 18:03:22 +0100 | [diff] [blame] | 243 | } |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 244 | EXPORT_SYMBOL_GPL(gb_hd_put); |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 245 | |
| 246 | int __init gb_hd_init(void) |
| 247 | { |
| 248 | ida_init(&gb_hd_bus_id_map); |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | void gb_hd_exit(void) |
| 254 | { |
| 255 | ida_destroy(&gb_hd_bus_id_map); |
| 256 | } |