4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
18 static struct ida gb_hd_bus_id_map;
20 int gb_hd_output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
23 if (!hd || !hd->driver || !hd->driver->output)
25 return hd->driver->output(hd, req, size, cmd, async);
27 EXPORT_SYMBOL_GPL(gb_hd_output);
29 static void gb_hd_release(struct device *dev)
31 struct gb_host_device *hd = to_gb_host_device(dev);
35 ida_simple_remove(&gb_hd_bus_id_map, hd->bus_id);
36 ida_destroy(&hd->cport_id_map);
40 struct device_type greybus_hd_type = {
41 .name = "greybus_host_device",
42 .release = gb_hd_release,
45 struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver,
46 struct device *parent,
47 size_t buffer_size_max,
50 struct gb_host_device *hd;
54 * Validate that the driver implements all of the callbacks
55 * so that we don't have to every time we make them.
57 if ((!driver->message_send) || (!driver->message_cancel)) {
58 pr_err("Must implement all gb_hd_driver callbacks!\n");
59 return ERR_PTR(-EINVAL);
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);
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);
73 * Make sure to never allocate messages larger than what the Greybus
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;
82 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
84 return ERR_PTR(-ENOMEM);
86 ret = ida_simple_get(&gb_hd_bus_id_map, 1, 0, GFP_KERNEL);
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;
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);
107 hd->svc = gb_svc_create(hd);
109 dev_err(&hd->dev, "failed to create svc\n");
110 put_device(&hd->dev);
111 return ERR_PTR(-ENOMEM);
116 EXPORT_SYMBOL_GPL(gb_hd_create);
118 int gb_hd_add(struct gb_host_device *hd)
122 ret = device_add(&hd->dev);
126 ret = gb_svc_add(hd->svc);
128 device_del(&hd->dev);
134 EXPORT_SYMBOL_GPL(gb_hd_add);
136 void gb_hd_del(struct gb_host_device *hd)
138 gb_interfaces_remove(hd);
142 device_del(&hd->dev);
144 EXPORT_SYMBOL_GPL(gb_hd_del);
146 void gb_hd_put(struct gb_host_device *hd)
148 put_device(&hd->dev);
150 EXPORT_SYMBOL_GPL(gb_hd_put);
152 int __init gb_hd_init(void)
154 ida_init(&gb_hd_bus_id_map);
159 void gb_hd_exit(void)
161 ida_destroy(&gb_hd_bus_id_map);