staging: wfx: fix rate control handling
[linux/fpc-iii.git] / drivers / greybus / hd.c
blob72b21bf2d7d36a85cbfd05d261d760fa37f16205
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Greybus Host Device
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
7 */
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/greybus.h>
13 #include "greybus_trace.h"
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);
19 EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_in);
20 EXPORT_TRACEPOINT_SYMBOL_GPL(gb_message_submit);
22 static struct ida gb_hd_bus_id_map;
24 int gb_hd_output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
25 bool async)
27 if (!hd || !hd->driver || !hd->driver->output)
28 return -EINVAL;
29 return hd->driver->output(hd, req, size, cmd, async);
31 EXPORT_SYMBOL_GPL(gb_hd_output);
33 static ssize_t bus_id_show(struct device *dev,
34 struct device_attribute *attr, char *buf)
36 struct gb_host_device *hd = to_gb_host_device(dev);
38 return sprintf(buf, "%d\n", hd->bus_id);
40 static DEVICE_ATTR_RO(bus_id);
42 static struct attribute *bus_attrs[] = {
43 &dev_attr_bus_id.attr,
44 NULL
46 ATTRIBUTE_GROUPS(bus);
48 int gb_hd_cport_reserve(struct gb_host_device *hd, u16 cport_id)
50 struct ida *id_map = &hd->cport_id_map;
51 int ret;
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;
59 return 0;
61 EXPORT_SYMBOL_GPL(gb_hd_cport_reserve);
63 void gb_hd_cport_release_reserved(struct gb_host_device *hd, u16 cport_id)
65 struct ida *id_map = &hd->cport_id_map;
67 ida_simple_remove(id_map, cport_id);
69 EXPORT_SYMBOL_GPL(gb_hd_cport_release_reserved);
71 /* Locking: Caller guarantees serialisation */
72 int gb_hd_cport_allocate(struct gb_host_device *hd, int cport_id,
73 unsigned long flags)
75 struct ida *id_map = &hd->cport_id_map;
76 int ida_start, ida_end;
78 if (hd->driver->cport_allocate)
79 return hd->driver->cport_allocate(hd, cport_id, flags);
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;
92 return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
95 /* Locking: Caller guarantees serialisation */
96 void gb_hd_cport_release(struct gb_host_device *hd, u16 cport_id)
98 if (hd->driver->cport_release) {
99 hd->driver->cport_release(hd, cport_id);
100 return;
103 ida_simple_remove(&hd->cport_id_map, cport_id);
106 static void gb_hd_release(struct device *dev)
108 struct gb_host_device *hd = to_gb_host_device(dev);
110 trace_gb_hd_release(hd);
112 if (hd->svc)
113 gb_svc_put(hd->svc);
114 ida_simple_remove(&gb_hd_bus_id_map, hd->bus_id);
115 ida_destroy(&hd->cport_id_map);
116 kfree(hd);
119 struct device_type greybus_hd_type = {
120 .name = "greybus_host_device",
121 .release = gb_hd_release,
124 struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver,
125 struct device *parent,
126 size_t buffer_size_max,
127 size_t num_cports)
129 struct gb_host_device *hd;
130 int ret;
133 * Validate that the driver implements all of the callbacks
134 * so that we don't have to every time we make them.
136 if ((!driver->message_send) || (!driver->message_cancel)) {
137 dev_err(parent, "mandatory hd-callbacks missing\n");
138 return ERR_PTR(-EINVAL);
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);
146 if (num_cports == 0 || num_cports > CPORT_ID_MAX + 1) {
147 dev_err(parent, "Invalid number of CPorts: %zu\n", num_cports);
148 return ERR_PTR(-EINVAL);
152 * Make sure to never allocate messages larger than what the Greybus
153 * protocol supports.
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;
161 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
162 if (!hd)
163 return ERR_PTR(-ENOMEM);
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);
170 hd->bus_id = ret;
172 hd->driver = driver;
173 INIT_LIST_HEAD(&hd->modules);
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;
179 hd->dev.parent = parent;
180 hd->dev.bus = &greybus_bus_type;
181 hd->dev.type = &greybus_hd_type;
182 hd->dev.groups = bus_groups;
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);
187 trace_gb_hd_create(hd);
189 hd->svc = gb_svc_create(hd);
190 if (!hd->svc) {
191 dev_err(&hd->dev, "failed to create svc\n");
192 put_device(&hd->dev);
193 return ERR_PTR(-ENOMEM);
196 return hd;
198 EXPORT_SYMBOL_GPL(gb_hd_create);
200 int gb_hd_add(struct gb_host_device *hd)
202 int ret;
204 ret = device_add(&hd->dev);
205 if (ret)
206 return ret;
208 ret = gb_svc_add(hd->svc);
209 if (ret) {
210 device_del(&hd->dev);
211 return ret;
214 trace_gb_hd_add(hd);
216 return 0;
218 EXPORT_SYMBOL_GPL(gb_hd_add);
220 void gb_hd_del(struct gb_host_device *hd)
222 trace_gb_hd_del(hd);
225 * Tear down the svc and flush any on-going hotplug processing before
226 * removing the remaining interfaces.
228 gb_svc_del(hd->svc);
230 device_del(&hd->dev);
232 EXPORT_SYMBOL_GPL(gb_hd_del);
234 void gb_hd_shutdown(struct gb_host_device *hd)
236 gb_svc_del(hd->svc);
238 EXPORT_SYMBOL_GPL(gb_hd_shutdown);
240 void gb_hd_put(struct gb_host_device *hd)
242 put_device(&hd->dev);
244 EXPORT_SYMBOL_GPL(gb_hd_put);
246 int __init gb_hd_init(void)
248 ida_init(&gb_hd_bus_id_map);
250 return 0;
253 void gb_hd_exit(void)
255 ida_destroy(&gb_hd_bus_id_map);