4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
16 static struct ida gb_hd_bus_id_map;
18 int gb_hd_output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
21 if (!hd || !hd->driver || !hd->driver->output)
23 return hd->driver->output(hd, req, size, cmd, async);
25 EXPORT_SYMBOL_GPL(gb_hd_output);
27 static ssize_t bus_id_show(struct device *dev,
28 struct device_attribute *attr, char *buf)
30 struct gb_host_device *hd = to_gb_host_device(dev);
32 return sprintf(buf, "%d\n", hd->bus_id);
34 static DEVICE_ATTR_RO(bus_id);
36 static struct attribute *bus_attrs[] = {
37 &dev_attr_bus_id.attr,
40 ATTRIBUTE_GROUPS(bus);
42 int gb_hd_cport_reserve(struct gb_host_device *hd, u16 cport_id)
44 struct ida *id_map = &hd->cport_id_map;
47 ret = ida_simple_get(id_map, cport_id, cport_id + 1, GFP_KERNEL);
49 dev_err(&hd->dev, "failed to reserve cport %u\n", cport_id);
55 EXPORT_SYMBOL_GPL(gb_hd_cport_reserve);
57 /* Locking: Caller guarantees serialisation */
58 int gb_hd_cport_allocate(struct gb_host_device *hd, int cport_id,
61 struct ida *id_map = &hd->cport_id_map;
62 int ida_start, ida_end;
64 if (hd->driver->cport_allocate)
65 return hd->driver->cport_allocate(hd, cport_id, flags);
69 ida_end = hd->num_cports;
70 } else if (cport_id < hd->num_cports) {
72 ida_end = cport_id + 1;
74 dev_err(&hd->dev, "cport %d not available\n", cport_id);
78 return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
81 /* Locking: Caller guarantees serialisation */
82 void gb_hd_cport_release(struct gb_host_device *hd, u16 cport_id)
84 if (hd->driver->cport_release) {
85 hd->driver->cport_release(hd, cport_id);
89 ida_simple_remove(&hd->cport_id_map, cport_id);
92 static void gb_hd_release(struct device *dev)
94 struct gb_host_device *hd = to_gb_host_device(dev);
98 ida_simple_remove(&gb_hd_bus_id_map, hd->bus_id);
99 ida_destroy(&hd->cport_id_map);
103 struct device_type greybus_hd_type = {
104 .name = "greybus_host_device",
105 .release = gb_hd_release,
108 struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver,
109 struct device *parent,
110 size_t buffer_size_max,
113 struct gb_host_device *hd;
117 * Validate that the driver implements all of the callbacks
118 * so that we don't have to every time we make them.
120 if ((!driver->message_send) || (!driver->message_cancel)) {
121 dev_err(parent, "mandatory hd-callbacks missing\n");
122 return ERR_PTR(-EINVAL);
125 if (buffer_size_max < GB_OPERATION_MESSAGE_SIZE_MIN) {
126 dev_err(parent, "greybus host-device buffers too small\n");
127 return ERR_PTR(-EINVAL);
130 if (num_cports == 0 || num_cports > CPORT_ID_MAX + 1) {
131 dev_err(parent, "Invalid number of CPorts: %zu\n", num_cports);
132 return ERR_PTR(-EINVAL);
136 * Make sure to never allocate messages larger than what the Greybus
139 if (buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) {
140 dev_warn(parent, "limiting buffer size to %u\n",
141 GB_OPERATION_MESSAGE_SIZE_MAX);
142 buffer_size_max = GB_OPERATION_MESSAGE_SIZE_MAX;
145 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
147 return ERR_PTR(-ENOMEM);
149 ret = ida_simple_get(&gb_hd_bus_id_map, 1, 0, GFP_KERNEL);
157 INIT_LIST_HEAD(&hd->modules);
158 INIT_LIST_HEAD(&hd->connections);
159 ida_init(&hd->cport_id_map);
160 hd->buffer_size_max = buffer_size_max;
161 hd->num_cports = num_cports;
163 hd->dev.parent = parent;
164 hd->dev.bus = &greybus_bus_type;
165 hd->dev.type = &greybus_hd_type;
166 hd->dev.groups = bus_groups;
167 hd->dev.dma_mask = hd->dev.parent->dma_mask;
168 device_initialize(&hd->dev);
169 dev_set_name(&hd->dev, "greybus%d", hd->bus_id);
171 hd->svc = gb_svc_create(hd);
173 dev_err(&hd->dev, "failed to create svc\n");
174 put_device(&hd->dev);
175 return ERR_PTR(-ENOMEM);
180 EXPORT_SYMBOL_GPL(gb_hd_create);
182 int gb_hd_add(struct gb_host_device *hd)
186 ret = device_add(&hd->dev);
190 ret = gb_svc_add(hd->svc);
192 device_del(&hd->dev);
198 EXPORT_SYMBOL_GPL(gb_hd_add);
200 void gb_hd_del(struct gb_host_device *hd)
203 * Tear down the svc and flush any on-going hotplug processing before
204 * removing the remaining interfaces.
208 device_del(&hd->dev);
210 EXPORT_SYMBOL_GPL(gb_hd_del);
212 void gb_hd_put(struct gb_host_device *hd)
214 put_device(&hd->dev);
216 EXPORT_SYMBOL_GPL(gb_hd_put);
218 int __init gb_hd_init(void)
220 ida_init(&gb_hd_bus_id_map);
225 void gb_hd_exit(void)
227 ida_destroy(&gb_hd_bus_id_map);