greybus: hd: generalise cport allocation
[cascardo/linux.git] / drivers / staging / greybus / hd.c
1 /*
2  * Greybus Host Device
3  *
4  * Copyright 2014-2015 Google Inc.
5  * Copyright 2014-2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12
13 #include "greybus.h"
14
15
16 static struct ida gb_hd_bus_id_map;
17
18 int gb_hd_output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
19                  bool async)
20 {
21         if (!hd || !hd->driver || !hd->driver->output)
22                 return -EINVAL;
23         return hd->driver->output(hd, req, size, cmd, async);
24 }
25 EXPORT_SYMBOL_GPL(gb_hd_output);
26
27 static ssize_t bus_id_show(struct device *dev,
28                                 struct device_attribute *attr, char *buf)
29 {
30         struct gb_host_device *hd = to_gb_host_device(dev);
31
32         return sprintf(buf, "%d\n", hd->bus_id);
33 }
34 static DEVICE_ATTR_RO(bus_id);
35
36 static struct attribute *bus_attrs[] = {
37         &dev_attr_bus_id.attr,
38         NULL
39 };
40 ATTRIBUTE_GROUPS(bus);
41
42 int gb_hd_cport_reserve(struct gb_host_device *hd, u16 cport_id)
43 {
44         struct ida *id_map = &hd->cport_id_map;
45         int ret;
46
47         ret = ida_simple_get(id_map, cport_id, cport_id + 1, GFP_KERNEL);
48         if (ret < 0) {
49                 dev_err(&hd->dev, "failed to reserve cport %u\n", cport_id);
50                 return ret;
51         }
52
53         return 0;
54 }
55 EXPORT_SYMBOL_GPL(gb_hd_cport_reserve);
56
57 /* Locking: Caller guarantees serialisation */
58 int gb_hd_cport_allocate(struct gb_host_device *hd, int cport_id,
59                                 unsigned long flags)
60 {
61         struct ida *id_map = &hd->cport_id_map;
62         int ida_start, ida_end;
63
64         if (hd->driver->cport_allocate)
65                 return hd->driver->cport_allocate(hd, cport_id, flags);
66
67         if (cport_id < 0) {
68                 ida_start = 0;
69                 ida_end = hd->num_cports;
70         } else if (cport_id < hd->num_cports) {
71                 ida_start = cport_id;
72                 ida_end = cport_id + 1;
73         } else {
74                 dev_err(&hd->dev, "cport %d not available\n", cport_id);
75                 return -EINVAL;
76         }
77
78         return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
79 }
80
81 /* Locking: Caller guarantees serialisation */
82 void gb_hd_cport_release(struct gb_host_device *hd, u16 cport_id)
83 {
84         if (hd->driver->cport_release) {
85                 hd->driver->cport_release(hd, cport_id);
86                 return;
87         }
88
89         ida_simple_remove(&hd->cport_id_map, cport_id);
90 }
91
92 static void gb_hd_release(struct device *dev)
93 {
94         struct gb_host_device *hd = to_gb_host_device(dev);
95
96         if (hd->svc)
97                 gb_svc_put(hd->svc);
98         ida_simple_remove(&gb_hd_bus_id_map, hd->bus_id);
99         ida_destroy(&hd->cport_id_map);
100         kfree(hd);
101 }
102
103 struct device_type greybus_hd_type = {
104         .name           = "greybus_host_device",
105         .release        = gb_hd_release,
106 };
107
108 struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver,
109                                         struct device *parent,
110                                         size_t buffer_size_max,
111                                         size_t num_cports)
112 {
113         struct gb_host_device *hd;
114         int ret;
115
116         /*
117          * Validate that the driver implements all of the callbacks
118          * so that we don't have to every time we make them.
119          */
120         if ((!driver->message_send) || (!driver->message_cancel)) {
121                 dev_err(parent, "mandatory hd-callbacks missing\n");
122                 return ERR_PTR(-EINVAL);
123         }
124
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);
128         }
129
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);
133         }
134
135         /*
136          * Make sure to never allocate messages larger than what the Greybus
137          * protocol supports.
138          */
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;
143         }
144
145         hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
146         if (!hd)
147                 return ERR_PTR(-ENOMEM);
148
149         ret = ida_simple_get(&gb_hd_bus_id_map, 1, 0, GFP_KERNEL);
150         if (ret < 0) {
151                 kfree(hd);
152                 return ERR_PTR(ret);
153         }
154         hd->bus_id = ret;
155
156         hd->driver = driver;
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;
162
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);
170
171         hd->svc = gb_svc_create(hd);
172         if (!hd->svc) {
173                 dev_err(&hd->dev, "failed to create svc\n");
174                 put_device(&hd->dev);
175                 return ERR_PTR(-ENOMEM);
176         }
177
178         return hd;
179 }
180 EXPORT_SYMBOL_GPL(gb_hd_create);
181
182 int gb_hd_add(struct gb_host_device *hd)
183 {
184         int ret;
185
186         ret = device_add(&hd->dev);
187         if (ret)
188                 return ret;
189
190         ret = gb_svc_add(hd->svc);
191         if (ret) {
192                 device_del(&hd->dev);
193                 return ret;
194         }
195
196         return 0;
197 }
198 EXPORT_SYMBOL_GPL(gb_hd_add);
199
200 void gb_hd_del(struct gb_host_device *hd)
201 {
202         /*
203          * Tear down the svc and flush any on-going hotplug processing before
204          * removing the remaining interfaces.
205          */
206         gb_svc_del(hd->svc);
207
208         device_del(&hd->dev);
209 }
210 EXPORT_SYMBOL_GPL(gb_hd_del);
211
212 void gb_hd_put(struct gb_host_device *hd)
213 {
214         put_device(&hd->dev);
215 }
216 EXPORT_SYMBOL_GPL(gb_hd_put);
217
218 int __init gb_hd_init(void)
219 {
220         ida_init(&gb_hd_bus_id_map);
221
222         return 0;
223 }
224
225 void gb_hd_exit(void)
226 {
227         ida_destroy(&gb_hd_bus_id_map);
228 }