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