greybus: es2: clean up CDSI CPort reservation
[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 {
60         struct ida *id_map = &hd->cport_id_map;
61         int ida_start, ida_end;
62
63         if (cport_id < 0) {
64                 ida_start = 0;
65                 ida_end = hd->num_cports;
66         } else if (cport_id < hd->num_cports) {
67                 ida_start = cport_id;
68                 ida_end = cport_id + 1;
69         } else {
70                 dev_err(&hd->dev, "cport %d not available\n", cport_id);
71                 return -EINVAL;
72         }
73
74         return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
75 }
76
77 /* Locking: Caller guarantees serialisation */
78 void gb_hd_cport_release(struct gb_host_device *hd, u16 cport_id)
79 {
80         ida_simple_remove(&hd->cport_id_map, cport_id);
81 }
82
83 static void gb_hd_release(struct device *dev)
84 {
85         struct gb_host_device *hd = to_gb_host_device(dev);
86
87         if (hd->svc)
88                 gb_svc_put(hd->svc);
89         ida_simple_remove(&gb_hd_bus_id_map, hd->bus_id);
90         ida_destroy(&hd->cport_id_map);
91         kfree(hd);
92 }
93
94 struct device_type greybus_hd_type = {
95         .name           = "greybus_host_device",
96         .release        = gb_hd_release,
97 };
98
99 struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver,
100                                         struct device *parent,
101                                         size_t buffer_size_max,
102                                         size_t num_cports)
103 {
104         struct gb_host_device *hd;
105         int ret;
106
107         /*
108          * Validate that the driver implements all of the callbacks
109          * so that we don't have to every time we make them.
110          */
111         if ((!driver->message_send) || (!driver->message_cancel)) {
112                 dev_err(parent, "mandatory hd-callbacks missing\n");
113                 return ERR_PTR(-EINVAL);
114         }
115
116         if (buffer_size_max < GB_OPERATION_MESSAGE_SIZE_MIN) {
117                 dev_err(parent, "greybus host-device buffers too small\n");
118                 return ERR_PTR(-EINVAL);
119         }
120
121         if (num_cports == 0 || num_cports > CPORT_ID_MAX + 1) {
122                 dev_err(parent, "Invalid number of CPorts: %zu\n", num_cports);
123                 return ERR_PTR(-EINVAL);
124         }
125
126         /*
127          * Make sure to never allocate messages larger than what the Greybus
128          * protocol supports.
129          */
130         if (buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) {
131                 dev_warn(parent, "limiting buffer size to %u\n",
132                          GB_OPERATION_MESSAGE_SIZE_MAX);
133                 buffer_size_max = GB_OPERATION_MESSAGE_SIZE_MAX;
134         }
135
136         hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
137         if (!hd)
138                 return ERR_PTR(-ENOMEM);
139
140         ret = ida_simple_get(&gb_hd_bus_id_map, 1, 0, GFP_KERNEL);
141         if (ret < 0) {
142                 kfree(hd);
143                 return ERR_PTR(ret);
144         }
145         hd->bus_id = ret;
146
147         hd->driver = driver;
148         INIT_LIST_HEAD(&hd->modules);
149         INIT_LIST_HEAD(&hd->connections);
150         ida_init(&hd->cport_id_map);
151         hd->buffer_size_max = buffer_size_max;
152         hd->num_cports = num_cports;
153
154         hd->dev.parent = parent;
155         hd->dev.bus = &greybus_bus_type;
156         hd->dev.type = &greybus_hd_type;
157         hd->dev.groups = bus_groups;
158         hd->dev.dma_mask = hd->dev.parent->dma_mask;
159         device_initialize(&hd->dev);
160         dev_set_name(&hd->dev, "greybus%d", hd->bus_id);
161
162         hd->svc = gb_svc_create(hd);
163         if (!hd->svc) {
164                 dev_err(&hd->dev, "failed to create svc\n");
165                 put_device(&hd->dev);
166                 return ERR_PTR(-ENOMEM);
167         }
168
169         return hd;
170 }
171 EXPORT_SYMBOL_GPL(gb_hd_create);
172
173 int gb_hd_add(struct gb_host_device *hd)
174 {
175         int ret;
176
177         ret = device_add(&hd->dev);
178         if (ret)
179                 return ret;
180
181         ret = gb_svc_add(hd->svc);
182         if (ret) {
183                 device_del(&hd->dev);
184                 return ret;
185         }
186
187         return 0;
188 }
189 EXPORT_SYMBOL_GPL(gb_hd_add);
190
191 void gb_hd_del(struct gb_host_device *hd)
192 {
193         /*
194          * Tear down the svc and flush any on-going hotplug processing before
195          * removing the remaining interfaces.
196          */
197         gb_svc_del(hd->svc);
198
199         device_del(&hd->dev);
200 }
201 EXPORT_SYMBOL_GPL(gb_hd_del);
202
203 void gb_hd_put(struct gb_host_device *hd)
204 {
205         put_device(&hd->dev);
206 }
207 EXPORT_SYMBOL_GPL(gb_hd_put);
208
209 int __init gb_hd_init(void)
210 {
211         ida_init(&gb_hd_bus_id_map);
212
213         return 0;
214 }
215
216 void gb_hd_exit(void)
217 {
218         ida_destroy(&gb_hd_bus_id_map);
219 }