blob: 72b21bf2d7d36a85cbfd05d261d760fa37f16205 [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Johan Hovold7bc6faa2015-11-03 18:03:22 +01002/*
3 * Greybus Host Device
4 *
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
Johan Hovold7bc6faa2015-11-03 18:03:22 +01007 */
8
Johan Hovold7bc6faa2015-11-03 18:03:22 +01009#include <linux/kernel.h>
10#include <linux/slab.h>
Greg Kroah-Hartmanec0ad862019-08-25 07:54:27 +020011#include <linux/greybus.h>
Johan Hovold7bc6faa2015-11-03 18:03:22 +010012
Alex Elder1f790462016-05-23 23:05:30 -050013#include "greybus_trace.h"
Johan Hovold7bc6faa2015-11-03 18:03:22 +010014
Viresh Kumar56278c72016-06-09 16:34:40 +053015EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_create);
16EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_release);
17EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_add);
18EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_del);
Alex Elder495787a2016-06-03 15:55:38 -050019EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_in);
Viresh Kumar56278c72016-06-09 16:34:40 +053020EXPORT_TRACEPOINT_SYMBOL_GPL(gb_message_submit);
Alex Elder495787a2016-06-03 15:55:38 -050021
Johan Hovold2adaefb2015-11-25 15:59:02 +010022static struct ida gb_hd_bus_id_map;
Johan Hovold7bc6faa2015-11-03 18:03:22 +010023
Greg Kroah-Hartmaned4596e92015-12-22 18:21:51 -080024int 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}
31EXPORT_SYMBOL_GPL(gb_hd_output);
32
Sandeep Patilfbbd2b72016-04-29 07:13:23 -070033static ssize_t bus_id_show(struct device *dev,
Greg Kroah-Hartmana11ac9e2019-08-25 07:54:23 +020034 struct device_attribute *attr, char *buf)
Sandeep Patilfbbd2b72016-04-29 07:13:23 -070035{
36 struct gb_host_device *hd = to_gb_host_device(dev);
37
38 return sprintf(buf, "%d\n", hd->bus_id);
39}
40static DEVICE_ATTR_RO(bus_id);
41
42static struct attribute *bus_attrs[] = {
43 &dev_attr_bus_id.attr,
44 NULL
45};
46ATTRIBUTE_GROUPS(bus);
47
Johan Hovold05061502016-05-11 10:18:01 +020048int 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}
61EXPORT_SYMBOL_GPL(gb_hd_cport_reserve);
62
Vaibhav Agarwal29a822b2016-05-27 10:49:23 +053063void 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}
69EXPORT_SYMBOL_GPL(gb_hd_cport_release_reserved);
70
Johan Hovold74a5d932016-05-11 10:17:59 +020071/* Locking: Caller guarantees serialisation */
Johan Hovoldf2aae1c2016-05-11 10:18:02 +020072int gb_hd_cport_allocate(struct gb_host_device *hd, int cport_id,
Greg Kroah-Hartmana11ac9e2019-08-25 07:54:23 +020073 unsigned long flags)
Johan Hovold74a5d932016-05-11 10:17:59 +020074{
75 struct ida *id_map = &hd->cport_id_map;
76 int ida_start, ida_end;
77
Johan Hovoldf2aae1c2016-05-11 10:18:02 +020078 if (hd->driver->cport_allocate)
79 return hd->driver->cport_allocate(hd, cport_id, flags);
80
Johan Hovold74a5d932016-05-11 10:17:59 +020081 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 */
96void gb_hd_cport_release(struct gb_host_device *hd, u16 cport_id)
97{
Johan Hovoldf2aae1c2016-05-11 10:18:02 +020098 if (hd->driver->cport_release) {
99 hd->driver->cport_release(hd, cport_id);
100 return;
101 }
102
Johan Hovold74a5d932016-05-11 10:17:59 +0200103 ida_simple_remove(&hd->cport_id_map, cport_id);
104}
105
Johan Hovold2adaefb2015-11-25 15:59:02 +0100106static void gb_hd_release(struct device *dev)
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100107{
Johan Hovold2adaefb2015-11-25 15:59:02 +0100108 struct gb_host_device *hd = to_gb_host_device(dev);
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100109
Johan Hovold12823172016-05-27 18:23:01 +0200110 trace_gb_hd_release(hd);
111
Johan Hovold7adeaae72015-12-07 15:05:37 +0100112 if (hd->svc)
113 gb_svc_put(hd->svc);
Johan Hovold2adaefb2015-11-25 15:59:02 +0100114 ida_simple_remove(&gb_hd_bus_id_map, hd->bus_id);
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100115 ida_destroy(&hd->cport_id_map);
116 kfree(hd);
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100117}
118
Johan Hovold2adaefb2015-11-25 15:59:02 +0100119struct device_type greybus_hd_type = {
120 .name = "greybus_host_device",
121 .release = gb_hd_release,
122};
123
Johan Hovoldd6e139b2015-11-03 18:03:25 +0100124struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver,
Greg Kroah-Hartmana11ac9e2019-08-25 07:54:23 +0200125 struct device *parent,
126 size_t buffer_size_max,
127 size_t num_cports)
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100128{
Johan Hovold25376362015-11-03 18:03:23 +0100129 struct gb_host_device *hd;
Johan Hovold2adaefb2015-11-25 15:59:02 +0100130 int ret;
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100131
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 Hovoldb4275722016-02-11 13:52:47 +0100137 dev_err(parent, "mandatory hd-callbacks missing\n");
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100138 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 Hovoldbfe2c992015-11-22 10:12:58 +0100146 if (num_cports == 0 || num_cports > CPORT_ID_MAX + 1) {
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100147 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 Hovold2adaefb2015-11-25 15:59:02 +0100165 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 Hovold2adaefb2015-11-25 15:59:02 +0100170 hd->bus_id = ret;
Johan Hovold2adaefb2015-11-25 15:59:02 +0100171
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100172 hd->driver = driver;
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200173 INIT_LIST_HEAD(&hd->modules);
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100174 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 Hovoldd4c80ba2015-12-07 15:05:35 +0100179 hd->dev.parent = parent;
180 hd->dev.bus = &greybus_bus_type;
181 hd->dev.type = &greybus_hd_type;
Sandeep Patilfbbd2b72016-04-29 07:13:23 -0700182 hd->dev.groups = bus_groups;
Johan Hovoldd4c80ba2015-12-07 15:05:35 +0100183 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 Elder1f790462016-05-23 23:05:30 -0500187 trace_gb_hd_create(hd);
188
Johan Hovold7adeaae72015-12-07 15:05:37 +0100189 hd->svc = gb_svc_create(hd);
190 if (!hd->svc) {
191 dev_err(&hd->dev, "failed to create svc\n");
Johan Hovold2c848942015-12-07 15:05:36 +0100192 put_device(&hd->dev);
193 return ERR_PTR(-ENOMEM);
Johan Hovold5ef32382015-11-25 15:59:18 +0100194 }
195
Johan Hovold2c848942015-12-07 15:05:36 +0100196 return hd;
Johan Hovold5ef32382015-11-25 15:59:18 +0100197}
Johan Hovold2c848942015-12-07 15:05:36 +0100198EXPORT_SYMBOL_GPL(gb_hd_create);
Johan Hovold5ef32382015-11-25 15:59:18 +0100199
Johan Hovoldc1700472015-11-04 18:55:22 +0100200int gb_hd_add(struct gb_host_device *hd)
201{
Johan Hovold2adaefb2015-11-25 15:59:02 +0100202 int ret;
203
204 ret = device_add(&hd->dev);
205 if (ret)
206 return ret;
207
Johan Hovold7adeaae72015-12-07 15:05:37 +0100208 ret = gb_svc_add(hd->svc);
Johan Hovold0bf1f242015-12-07 15:05:34 +0100209 if (ret) {
Johan Hovold0bf1f242015-12-07 15:05:34 +0100210 device_del(&hd->dev);
211 return ret;
212 }
213
Alex Elder1f790462016-05-23 23:05:30 -0500214 trace_gb_hd_add(hd);
215
Johan Hovoldc1700472015-11-04 18:55:22 +0100216 return 0;
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100217}
Johan Hovoldc1700472015-11-04 18:55:22 +0100218EXPORT_SYMBOL_GPL(gb_hd_add);
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100219
Johan Hovoldc1700472015-11-04 18:55:22 +0100220void gb_hd_del(struct gb_host_device *hd)
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100221{
Alex Elder1f790462016-05-23 23:05:30 -0500222 trace_gb_hd_del(hd);
223
Johan Hovold24988d3a2016-01-28 12:43:29 +0100224 /*
225 * Tear down the svc and flush any on-going hotplug processing before
226 * removing the remaining interfaces.
227 */
Johan Hovold7adeaae72015-12-07 15:05:37 +0100228 gb_svc_del(hd->svc);
Johan Hovold2adaefb2015-11-25 15:59:02 +0100229
230 device_del(&hd->dev);
Johan Hovoldc1700472015-11-04 18:55:22 +0100231}
232EXPORT_SYMBOL_GPL(gb_hd_del);
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100233
David Lin1f77b362016-07-12 17:41:21 -0700234void gb_hd_shutdown(struct gb_host_device *hd)
235{
236 gb_svc_del(hd->svc);
237}
238EXPORT_SYMBOL_GPL(gb_hd_shutdown);
239
Johan Hovoldc1700472015-11-04 18:55:22 +0100240void gb_hd_put(struct gb_host_device *hd)
241{
Johan Hovold2adaefb2015-11-25 15:59:02 +0100242 put_device(&hd->dev);
Johan Hovold7bc6faa2015-11-03 18:03:22 +0100243}
Johan Hovoldc1700472015-11-04 18:55:22 +0100244EXPORT_SYMBOL_GPL(gb_hd_put);
Johan Hovold2adaefb2015-11-25 15:59:02 +0100245
246int __init gb_hd_init(void)
247{
248 ida_init(&gb_hd_bus_id_map);
249
250 return 0;
251}
252
253void gb_hd_exit(void)
254{
255 ida_destroy(&gb_hd_bus_id_map);
256}