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 static void gb_hd_release(struct device *dev)
22 struct gb_host_device *hd = to_gb_host_device(dev);
24 ida_simple_remove(&gb_hd_bus_id_map, hd->bus_id);
25 ida_destroy(&hd->cport_id_map);
29 struct device_type greybus_hd_type = {
30 .name = "greybus_host_device",
31 .release = gb_hd_release,
34 struct gb_host_device *gb_hd_create(struct gb_hd_driver *driver,
35 struct device *parent,
36 size_t buffer_size_max,
39 struct gb_host_device *hd;
43 * Validate that the driver implements all of the callbacks
44 * so that we don't have to every time we make them.
46 if ((!driver->message_send) || (!driver->message_cancel)) {
47 pr_err("Must implement all gb_hd_driver callbacks!\n");
48 return ERR_PTR(-EINVAL);
51 if (buffer_size_max < GB_OPERATION_MESSAGE_SIZE_MIN) {
52 dev_err(parent, "greybus host-device buffers too small\n");
53 return ERR_PTR(-EINVAL);
56 if (num_cports == 0 || num_cports > CPORT_ID_MAX + 1) {
57 dev_err(parent, "Invalid number of CPorts: %zu\n", num_cports);
58 return ERR_PTR(-EINVAL);
62 * Make sure to never allocate messages larger than what the Greybus
65 if (buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) {
66 dev_warn(parent, "limiting buffer size to %u\n",
67 GB_OPERATION_MESSAGE_SIZE_MAX);
68 buffer_size_max = GB_OPERATION_MESSAGE_SIZE_MAX;
71 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
73 return ERR_PTR(-ENOMEM);
75 ret = ida_simple_get(&gb_hd_bus_id_map, 1, 0, GFP_KERNEL);
83 INIT_LIST_HEAD(&hd->interfaces);
84 INIT_LIST_HEAD(&hd->connections);
85 ida_init(&hd->cport_id_map);
86 hd->buffer_size_max = buffer_size_max;
87 hd->num_cports = num_cports;
89 hd->dev.parent = parent;
90 hd->dev.bus = &greybus_bus_type;
91 hd->dev.type = &greybus_hd_type;
92 hd->dev.dma_mask = hd->dev.parent->dma_mask;
93 device_initialize(&hd->dev);
94 dev_set_name(&hd->dev, "greybus%d", hd->bus_id);
98 EXPORT_SYMBOL_GPL(gb_hd_create);
100 static int gb_hd_create_svc_connection(struct gb_host_device *hd)
102 hd->svc_connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
103 GREYBUS_PROTOCOL_SVC);
104 if (!hd->svc_connection) {
105 dev_err(&hd->dev, "failed to create svc connection\n");
112 int gb_hd_add(struct gb_host_device *hd)
116 ret = device_add(&hd->dev);
120 ret = gb_hd_create_svc_connection(hd);
122 device_del(&hd->dev);
126 ret = gb_connection_init(hd->svc_connection);
128 gb_connection_destroy(hd->svc_connection);
129 device_del(&hd->dev);
135 EXPORT_SYMBOL_GPL(gb_hd_add);
137 void gb_hd_del(struct gb_host_device *hd)
139 gb_interfaces_remove(hd);
141 gb_connection_destroy(hd->svc_connection);
143 device_del(&hd->dev);
145 EXPORT_SYMBOL_GPL(gb_hd_del);
147 void gb_hd_put(struct gb_host_device *hd)
149 put_device(&hd->dev);
151 EXPORT_SYMBOL_GPL(gb_hd_put);
153 int __init gb_hd_init(void)
155 ida_init(&gb_hd_bus_id_map);
160 void gb_hd_exit(void)
162 ida_destroy(&gb_hd_bus_id_map);