greybus: host: provide "generic" apbridge output calls
[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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14
15 #include "greybus.h"
16
17
18 static struct ida gb_hd_bus_id_map;
19
20 int gb_hd_output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
21                  bool async)
22 {
23         if (!hd || !hd->driver || !hd->driver->output)
24                 return -EINVAL;
25         return hd->driver->output(hd, req, size, cmd, async);
26 }
27 EXPORT_SYMBOL_GPL(gb_hd_output);
28
29 static void gb_hd_release(struct device *dev)
30 {
31         struct gb_host_device *hd = to_gb_host_device(dev);
32
33         if (hd->svc)
34                 gb_svc_put(hd->svc);
35         ida_simple_remove(&gb_hd_bus_id_map, hd->bus_id);
36         ida_destroy(&hd->cport_id_map);
37         kfree(hd);
38 }
39
40 struct device_type greybus_hd_type = {
41         .name           = "greybus_host_device",
42         .release        = gb_hd_release,
43 };
44
45 struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver,
46                                         struct device *parent,
47                                         size_t buffer_size_max,
48                                         size_t num_cports)
49 {
50         struct gb_host_device *hd;
51         int ret;
52
53         /*
54          * Validate that the driver implements all of the callbacks
55          * so that we don't have to every time we make them.
56          */
57         if ((!driver->message_send) || (!driver->message_cancel)) {
58                 pr_err("Must implement all gb_hd_driver callbacks!\n");
59                 return ERR_PTR(-EINVAL);
60         }
61
62         if (buffer_size_max < GB_OPERATION_MESSAGE_SIZE_MIN) {
63                 dev_err(parent, "greybus host-device buffers too small\n");
64                 return ERR_PTR(-EINVAL);
65         }
66
67         if (num_cports == 0 || num_cports > CPORT_ID_MAX + 1) {
68                 dev_err(parent, "Invalid number of CPorts: %zu\n", num_cports);
69                 return ERR_PTR(-EINVAL);
70         }
71
72         /*
73          * Make sure to never allocate messages larger than what the Greybus
74          * protocol supports.
75          */
76         if (buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) {
77                 dev_warn(parent, "limiting buffer size to %u\n",
78                          GB_OPERATION_MESSAGE_SIZE_MAX);
79                 buffer_size_max = GB_OPERATION_MESSAGE_SIZE_MAX;
80         }
81
82         hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
83         if (!hd)
84                 return ERR_PTR(-ENOMEM);
85
86         ret = ida_simple_get(&gb_hd_bus_id_map, 1, 0, GFP_KERNEL);
87         if (ret < 0) {
88                 kfree(hd);
89                 return ERR_PTR(ret);
90         }
91         hd->bus_id = ret;
92
93         hd->driver = driver;
94         INIT_LIST_HEAD(&hd->interfaces);
95         INIT_LIST_HEAD(&hd->connections);
96         ida_init(&hd->cport_id_map);
97         hd->buffer_size_max = buffer_size_max;
98         hd->num_cports = num_cports;
99
100         hd->dev.parent = parent;
101         hd->dev.bus = &greybus_bus_type;
102         hd->dev.type = &greybus_hd_type;
103         hd->dev.dma_mask = hd->dev.parent->dma_mask;
104         device_initialize(&hd->dev);
105         dev_set_name(&hd->dev, "greybus%d", hd->bus_id);
106
107         hd->svc = gb_svc_create(hd);
108         if (!hd->svc) {
109                 dev_err(&hd->dev, "failed to create svc\n");
110                 put_device(&hd->dev);
111                 return ERR_PTR(-ENOMEM);
112         }
113
114         return hd;
115 }
116 EXPORT_SYMBOL_GPL(gb_hd_create);
117
118 int gb_hd_add(struct gb_host_device *hd)
119 {
120         int ret;
121
122         ret = device_add(&hd->dev);
123         if (ret)
124                 return ret;
125
126         ret = gb_svc_add(hd->svc);
127         if (ret) {
128                 device_del(&hd->dev);
129                 return ret;
130         }
131
132         return 0;
133 }
134 EXPORT_SYMBOL_GPL(gb_hd_add);
135
136 void gb_hd_del(struct gb_host_device *hd)
137 {
138         gb_interfaces_remove(hd);
139
140         gb_svc_del(hd->svc);
141
142         device_del(&hd->dev);
143 }
144 EXPORT_SYMBOL_GPL(gb_hd_del);
145
146 void gb_hd_put(struct gb_host_device *hd)
147 {
148         put_device(&hd->dev);
149 }
150 EXPORT_SYMBOL_GPL(gb_hd_put);
151
152 int __init gb_hd_init(void)
153 {
154         ida_init(&gb_hd_bus_id_map);
155
156         return 0;
157 }
158
159 void gb_hd_exit(void)
160 {
161         ida_destroy(&gb_hd_bus_id_map);
162 }