Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 1 | /* |
| 2 | * SVC Greybus driver. |
| 3 | * |
| 4 | * Copyright 2015 Google Inc. |
| 5 | * Copyright 2015 Linaro Ltd. |
| 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
| 9 | |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 11 | #include <linux/workqueue.h> |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 12 | |
Viresh Kumar | f66427a | 2015-09-02 21:27:13 +0530 | [diff] [blame] | 13 | #include "greybus.h" |
| 14 | |
Johan Hovold | f6c6c13 | 2015-11-11 10:07:08 +0100 | [diff] [blame] | 15 | #define CPORT_FLAGS_E2EFC BIT(0) |
| 16 | #define CPORT_FLAGS_CSD_N BIT(1) |
| 17 | #define CPORT_FLAGS_CSV_N BIT(2) |
Perry Hung | 0b22649 | 2015-07-24 19:02:34 -0400 | [diff] [blame] | 18 | |
Viresh Kumar | b45864d | 2015-07-24 15:32:21 +0530 | [diff] [blame] | 19 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 20 | struct svc_hotplug { |
| 21 | struct work_struct work; |
| 22 | struct gb_connection *connection; |
| 23 | struct gb_svc_intf_hotplug_request data; |
| 24 | }; |
| 25 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 26 | |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame^] | 27 | static ssize_t endo_id_show(struct device *dev, |
| 28 | struct device_attribute *attr, char *buf) |
| 29 | { |
| 30 | struct gb_svc *svc = to_gb_svc(dev); |
| 31 | |
| 32 | return sprintf(buf, "0x%04x\n", svc->endo_id); |
| 33 | } |
| 34 | static DEVICE_ATTR_RO(endo_id); |
| 35 | |
| 36 | static ssize_t ap_intf_id_show(struct device *dev, |
| 37 | struct device_attribute *attr, char *buf) |
| 38 | { |
| 39 | struct gb_svc *svc = to_gb_svc(dev); |
| 40 | |
| 41 | return sprintf(buf, "%u\n", svc->ap_intf_id); |
| 42 | } |
| 43 | static DEVICE_ATTR_RO(ap_intf_id); |
| 44 | |
| 45 | static struct attribute *svc_attrs[] = { |
| 46 | &dev_attr_endo_id.attr, |
| 47 | &dev_attr_ap_intf_id.attr, |
| 48 | NULL, |
| 49 | }; |
| 50 | ATTRIBUTE_GROUPS(svc); |
| 51 | |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 52 | /* |
| 53 | * AP's SVC cport is required early to get messages from the SVC. This happens |
| 54 | * even before the Endo is created and hence any modules or interfaces. |
| 55 | * |
| 56 | * This is a temporary connection, used only at initial bootup. |
| 57 | */ |
| 58 | struct gb_connection * |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 59 | gb_ap_svc_connection_create(struct gb_host_device *hd) |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 60 | { |
| 61 | struct gb_connection *connection; |
| 62 | |
Johan Hovold | 582b3a1 | 2015-11-25 15:59:03 +0100 | [diff] [blame] | 63 | connection = gb_connection_create_range(hd, NULL, |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 64 | GB_SVC_CPORT_ID, |
| 65 | GREYBUS_PROTOCOL_SVC, |
| 66 | GB_SVC_CPORT_ID, |
| 67 | GB_SVC_CPORT_ID + 1); |
| 68 | |
| 69 | return connection; |
| 70 | } |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * We know endo-type and AP's interface id now, lets create a proper svc |
| 74 | * connection (and its interface/bundle) now and get rid of the initial |
| 75 | * 'partially' initialized one svc connection. |
| 76 | */ |
| 77 | static struct gb_interface * |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 78 | gb_ap_interface_create(struct gb_host_device *hd, |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 79 | struct gb_connection *connection, u8 interface_id) |
| 80 | { |
| 81 | struct gb_interface *intf; |
| 82 | struct device *dev = &hd->endo->dev; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 83 | |
| 84 | intf = gb_interface_create(hd, interface_id); |
| 85 | if (!intf) { |
| 86 | dev_err(dev, "%s: Failed to create interface with id %hhu\n", |
| 87 | __func__, interface_id); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | intf->device_id = GB_DEVICE_ID_AP; |
Viresh Kumar | 67c93ae | 2015-07-24 15:32:19 +0530 | [diff] [blame] | 92 | svc_update_connection(intf, connection); |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 93 | |
Viresh Kumar | dcd0500 | 2015-07-24 15:32:20 +0530 | [diff] [blame] | 94 | /* Its no longer a partially initialized connection */ |
| 95 | hd->initial_svc_connection = NULL; |
| 96 | |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 97 | return intf; |
| 98 | } |
| 99 | |
Viresh Kumar | 505f16c | 2015-08-31 17:21:07 +0530 | [diff] [blame] | 100 | static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 101 | { |
| 102 | struct gb_svc_intf_device_id_request request; |
| 103 | |
| 104 | request.intf_id = intf_id; |
| 105 | request.device_id = device_id; |
| 106 | |
| 107 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID, |
| 108 | &request, sizeof(request), NULL, 0); |
| 109 | } |
| 110 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 111 | int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 112 | { |
| 113 | struct gb_svc_intf_reset_request request; |
| 114 | |
| 115 | request.intf_id = intf_id; |
| 116 | |
| 117 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET, |
| 118 | &request, sizeof(request), NULL, 0); |
| 119 | } |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 120 | EXPORT_SYMBOL_GPL(gb_svc_intf_reset); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 121 | |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 122 | int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector, |
| 123 | u32 *value) |
| 124 | { |
| 125 | struct gb_svc_dme_peer_get_request request; |
| 126 | struct gb_svc_dme_peer_get_response response; |
| 127 | u16 result; |
| 128 | int ret; |
| 129 | |
| 130 | request.intf_id = intf_id; |
| 131 | request.attr = cpu_to_le16(attr); |
| 132 | request.selector = cpu_to_le16(selector); |
| 133 | |
| 134 | ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET, |
| 135 | &request, sizeof(request), |
| 136 | &response, sizeof(response)); |
| 137 | if (ret) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 138 | pr_err("failed to get DME attribute (%hhu %hx %hu) %d\n", |
| 139 | intf_id, attr, selector, ret); |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | result = le16_to_cpu(response.result_code); |
| 144 | if (result) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 145 | pr_err("Unipro error %hu while getting DME attribute (%hhu %hx %hu)\n", |
| 146 | result, intf_id, attr, selector); |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 147 | return -EINVAL; |
| 148 | } |
| 149 | |
| 150 | if (value) |
| 151 | *value = le32_to_cpu(response.attr_value); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get); |
| 156 | |
| 157 | int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector, |
| 158 | u32 value) |
| 159 | { |
| 160 | struct gb_svc_dme_peer_set_request request; |
| 161 | struct gb_svc_dme_peer_set_response response; |
| 162 | u16 result; |
| 163 | int ret; |
| 164 | |
| 165 | request.intf_id = intf_id; |
| 166 | request.attr = cpu_to_le16(attr); |
| 167 | request.selector = cpu_to_le16(selector); |
| 168 | request.value = cpu_to_le32(value); |
| 169 | |
| 170 | ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET, |
| 171 | &request, sizeof(request), |
| 172 | &response, sizeof(response)); |
| 173 | if (ret) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 174 | pr_err("failed to set DME attribute (%hhu %hx %hu %u) %d\n", |
| 175 | intf_id, attr, selector, value, ret); |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | result = le16_to_cpu(response.result_code); |
| 180 | if (result) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 181 | pr_err("Unipro error %hu while setting DME attribute (%hhu %hx %hu %u)\n", |
| 182 | result, intf_id, attr, selector, value); |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 183 | return -EINVAL; |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set); |
| 189 | |
Viresh Kumar | 6bec5c7 | 2015-09-24 14:40:29 -0700 | [diff] [blame] | 190 | /* |
| 191 | * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot |
| 192 | * status attribute. AP needs to read and clear it, after reading a non-zero |
| 193 | * value from it. |
| 194 | * |
| 195 | * FIXME: This is module-hardware dependent and needs to be extended for every |
| 196 | * type of module we want to support. |
| 197 | */ |
| 198 | static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf) |
| 199 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 200 | struct gb_host_device *hd = intf->hd; |
Viresh Kumar | 6bec5c7 | 2015-09-24 14:40:29 -0700 | [diff] [blame] | 201 | int ret; |
| 202 | u32 value; |
| 203 | |
| 204 | /* Read and clear boot status in T_TstSrcIncrement */ |
| 205 | ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, |
| 206 | DME_ATTR_T_TST_SRC_INCREMENT, |
| 207 | DME_ATTR_SELECTOR_INDEX, &value); |
| 208 | |
| 209 | if (ret) |
| 210 | return ret; |
| 211 | |
| 212 | /* |
| 213 | * A nonzero boot status indicates the module has finished |
| 214 | * booting. Clear it. |
| 215 | */ |
| 216 | if (!value) { |
| 217 | dev_err(&intf->dev, "Module not ready yet\n"); |
| 218 | return -ENODEV; |
| 219 | } |
| 220 | |
Viresh Kumar | 1575ef1 | 2015-10-07 15:40:24 -0400 | [diff] [blame] | 221 | /* |
| 222 | * Check if the module needs to boot from unipro. |
| 223 | * For ES2: We need to check lowest 8 bits of 'value'. |
| 224 | * For ES3: We need to check highest 8 bits out of 32 of 'value'. |
| 225 | * |
| 226 | * FIXME: Add code to find if we are on ES2 or ES3 to have separate |
| 227 | * checks. |
| 228 | */ |
| 229 | if (value == DME_TSI_UNIPRO_BOOT_STARTED || |
| 230 | value == DME_TSI_FALLBACK_UNIPRO_BOOT_STARTED) |
| 231 | intf->boot_over_unipro = true; |
| 232 | |
Viresh Kumar | 6bec5c7 | 2015-09-24 14:40:29 -0700 | [diff] [blame] | 233 | return gb_svc_dme_peer_set(hd->svc, intf->interface_id, |
| 234 | DME_ATTR_T_TST_SRC_INCREMENT, |
| 235 | DME_ATTR_SELECTOR_INDEX, 0); |
| 236 | } |
| 237 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 238 | int gb_svc_connection_create(struct gb_svc *svc, |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 239 | u8 intf1_id, u16 cport1_id, |
Viresh Kumar | 1575ef1 | 2015-10-07 15:40:24 -0400 | [diff] [blame] | 240 | u8 intf2_id, u16 cport2_id, |
| 241 | bool boot_over_unipro) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 242 | { |
| 243 | struct gb_svc_conn_create_request request; |
| 244 | |
| 245 | request.intf1_id = intf1_id; |
Rui Miguel Silva | 2498050 | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 246 | request.cport1_id = cpu_to_le16(cport1_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 247 | request.intf2_id = intf2_id; |
Rui Miguel Silva | 2498050 | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 248 | request.cport2_id = cpu_to_le16(cport2_id); |
Perry Hung | 0b22649 | 2015-07-24 19:02:34 -0400 | [diff] [blame] | 249 | /* |
| 250 | * XXX: fix connections paramaters to TC0 and all CPort flags |
| 251 | * for now. |
| 252 | */ |
| 253 | request.tc = 0; |
Viresh Kumar | 1575ef1 | 2015-10-07 15:40:24 -0400 | [diff] [blame] | 254 | |
| 255 | /* |
| 256 | * We need to skip setting E2EFC and other flags to the connection |
| 257 | * create request, for all cports, on an interface that need to boot |
| 258 | * over unipro, i.e. interfaces required to download firmware. |
| 259 | */ |
| 260 | if (boot_over_unipro) |
| 261 | request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N; |
| 262 | else |
| 263 | request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 264 | |
| 265 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE, |
| 266 | &request, sizeof(request), NULL, 0); |
| 267 | } |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 268 | EXPORT_SYMBOL_GPL(gb_svc_connection_create); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 269 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 270 | void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id, |
| 271 | u8 intf2_id, u16 cport2_id) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 272 | { |
| 273 | struct gb_svc_conn_destroy_request request; |
Viresh Kumar | d9fcfff | 2015-08-31 17:21:05 +0530 | [diff] [blame] | 274 | struct gb_connection *connection = svc->connection; |
| 275 | int ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 276 | |
| 277 | request.intf1_id = intf1_id; |
Rui Miguel Silva | 2498050 | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 278 | request.cport1_id = cpu_to_le16(cport1_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 279 | request.intf2_id = intf2_id; |
Rui Miguel Silva | 2498050 | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 280 | request.cport2_id = cpu_to_le16(cport2_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 281 | |
Viresh Kumar | d9fcfff | 2015-08-31 17:21:05 +0530 | [diff] [blame] | 282 | ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY, |
| 283 | &request, sizeof(request), NULL, 0); |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 284 | if (ret) |
Johan Hovold | 35a84ba | 2015-11-11 10:07:07 +0100 | [diff] [blame] | 285 | pr_err("failed to destroy connection (%hhu:%hu %hhu:%hu) %d\n", |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 286 | intf1_id, cport1_id, intf2_id, cport2_id, ret); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 287 | } |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 288 | EXPORT_SYMBOL_GPL(gb_svc_connection_destroy); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 289 | |
Viresh Kumar | bb10685 | 2015-09-07 16:01:25 +0530 | [diff] [blame] | 290 | /* Creates bi-directional routes between the devices */ |
Viresh Kumar | 505f16c | 2015-08-31 17:21:07 +0530 | [diff] [blame] | 291 | static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id, |
| 292 | u8 intf2_id, u8 dev2_id) |
Perry Hung | e08aaa4 | 2015-07-24 19:02:31 -0400 | [diff] [blame] | 293 | { |
| 294 | struct gb_svc_route_create_request request; |
| 295 | |
| 296 | request.intf1_id = intf1_id; |
| 297 | request.dev1_id = dev1_id; |
| 298 | request.intf2_id = intf2_id; |
| 299 | request.dev2_id = dev2_id; |
| 300 | |
| 301 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE, |
| 302 | &request, sizeof(request), NULL, 0); |
| 303 | } |
Perry Hung | e08aaa4 | 2015-07-24 19:02:31 -0400 | [diff] [blame] | 304 | |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 305 | /* Destroys bi-directional routes between the devices */ |
| 306 | static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id) |
| 307 | { |
| 308 | struct gb_svc_route_destroy_request request; |
| 309 | int ret; |
| 310 | |
| 311 | request.intf1_id = intf1_id; |
| 312 | request.intf2_id = intf2_id; |
| 313 | |
| 314 | ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY, |
| 315 | &request, sizeof(request), NULL, 0); |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 316 | if (ret) |
Johan Hovold | 35a84ba | 2015-11-11 10:07:07 +0100 | [diff] [blame] | 317 | pr_err("failed to destroy route (%hhu %hhu) %d\n", |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 318 | intf1_id, intf2_id, ret); |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 319 | } |
| 320 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 321 | static int gb_svc_version_request(struct gb_operation *op) |
| 322 | { |
| 323 | struct gb_connection *connection = op->connection; |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 324 | struct gb_protocol_version_request *request; |
| 325 | struct gb_protocol_version_response *response; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 326 | |
Johan Hovold | 5551084 | 2015-11-19 18:28:01 +0100 | [diff] [blame] | 327 | if (op->request->payload_size < sizeof(*request)) { |
| 328 | pr_err("%d: short version request (%zu < %zu)\n", |
| 329 | connection->intf_cport_id, |
| 330 | op->request->payload_size, |
| 331 | sizeof(*request)); |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 335 | request = op->request->payload; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 336 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 337 | if (request->major > GB_SVC_VERSION_MAJOR) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 338 | pr_err("%d: unsupported major version (%hhu > %hhu)\n", |
| 339 | connection->intf_cport_id, request->major, |
| 340 | GB_SVC_VERSION_MAJOR); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 341 | return -ENOTSUPP; |
| 342 | } |
| 343 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 344 | connection->module_major = request->major; |
| 345 | connection->module_minor = request->minor; |
Viresh Kumar | 3ea959e3 | 2015-08-11 07:36:14 +0530 | [diff] [blame] | 346 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 347 | if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 348 | pr_err("%d: error allocating response\n", |
| 349 | connection->intf_cport_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 350 | return -ENOMEM; |
| 351 | } |
| 352 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 353 | response = op->response->payload; |
| 354 | response->major = connection->module_major; |
| 355 | response->minor = connection->module_minor; |
Johan Hovold | 5983293 | 2015-09-15 10:48:00 +0200 | [diff] [blame] | 356 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int gb_svc_hello(struct gb_operation *op) |
| 361 | { |
| 362 | struct gb_connection *connection = op->connection; |
Johan Hovold | 88f7b96 | 2015-11-25 15:59:08 +0100 | [diff] [blame] | 363 | struct gb_svc *svc = connection->private; |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 364 | struct gb_host_device *hd = connection->hd; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 365 | struct gb_svc_hello_request *hello_request; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 366 | struct gb_interface *intf; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 367 | int ret; |
| 368 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 369 | /* |
| 370 | * SVC sends information about the endo and interface-id on the hello |
| 371 | * request, use that to create an endo. |
| 372 | */ |
Viresh Kumar | 0c32d2a | 2015-08-11 07:29:19 +0530 | [diff] [blame] | 373 | if (op->request->payload_size < sizeof(*hello_request)) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 374 | pr_err("%d: Illegal size of hello request (%zu < %zu)\n", |
| 375 | connection->intf_cport_id, op->request->payload_size, |
| 376 | sizeof(*hello_request)); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 377 | return -EINVAL; |
| 378 | } |
| 379 | |
| 380 | hello_request = op->request->payload; |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame^] | 381 | svc->endo_id = le16_to_cpu(hello_request->endo_id); |
| 382 | svc->ap_intf_id = hello_request->interface_id; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 383 | |
Johan Hovold | 88f7b96 | 2015-11-25 15:59:08 +0100 | [diff] [blame] | 384 | ret = device_add(&svc->dev); |
| 385 | if (ret) { |
| 386 | dev_err(&svc->dev, "failed to register svc device: %d\n", ret); |
| 387 | return ret; |
| 388 | } |
| 389 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 390 | /* Setup Endo */ |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame^] | 391 | ret = greybus_endo_setup(hd, svc->endo_id, svc->ap_intf_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 392 | if (ret) |
| 393 | return ret; |
| 394 | |
| 395 | /* |
| 396 | * Endo and its modules are ready now, fix AP's partially initialized |
| 397 | * svc protocol and its connection. |
| 398 | */ |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame^] | 399 | intf = gb_ap_interface_create(hd, connection, svc->ap_intf_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 400 | if (!intf) { |
| 401 | gb_endo_remove(hd->endo); |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 408 | static void svc_intf_remove(struct gb_connection *connection, |
| 409 | struct gb_interface *intf) |
| 410 | { |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 411 | struct gb_svc *svc = connection->private; |
| 412 | u8 intf_id = intf->interface_id; |
| 413 | u8 device_id; |
| 414 | |
| 415 | device_id = intf->device_id; |
Viresh Kumar | 80d1ede | 2015-09-23 16:48:10 -0700 | [diff] [blame] | 416 | gb_interface_remove(intf); |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * Destroy the two-way route between the AP and the interface. |
| 420 | */ |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame^] | 421 | gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id); |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 422 | |
| 423 | ida_simple_remove(&svc->device_id_map, device_id); |
| 424 | } |
| 425 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 426 | /* |
| 427 | * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it |
| 428 | * returns, irrespective of success or Failure in bringing up the module. |
| 429 | */ |
| 430 | static void svc_process_hotplug(struct work_struct *work) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 431 | { |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 432 | struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug, |
| 433 | work); |
| 434 | struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data; |
| 435 | struct gb_connection *connection = svc_hotplug->connection; |
| 436 | struct gb_svc *svc = connection->private; |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 437 | struct gb_host_device *hd = connection->hd; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 438 | struct gb_interface *intf; |
| 439 | u8 intf_id, device_id; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 440 | int ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 441 | |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 442 | /* |
| 443 | * Grab the information we need. |
Viresh Kumar | 7eb8919 | 2015-07-01 12:13:50 +0530 | [diff] [blame] | 444 | */ |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 445 | intf_id = hotplug->intf_id; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 446 | |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 447 | intf = gb_interface_find(hd, intf_id); |
| 448 | if (intf) { |
| 449 | /* |
| 450 | * We have received a hotplug request for an interface that |
| 451 | * already exists. |
| 452 | * |
| 453 | * This can happen in cases like: |
| 454 | * - bootrom loading the firmware image and booting into that, |
| 455 | * which only generates a hotplug event. i.e. no hot-unplug |
| 456 | * event. |
| 457 | * - Or the firmware on the module crashed and sent hotplug |
| 458 | * request again to the SVC, which got propagated to AP. |
| 459 | * |
| 460 | * Remove the interface and add it again, and let user know |
| 461 | * about this with a print message. |
| 462 | */ |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 463 | pr_info("%d: Removed interface (%hhu) to add it again\n", |
| 464 | connection->intf_cport_id, intf_id); |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 465 | svc_intf_remove(connection, intf); |
| 466 | } |
| 467 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 468 | intf = gb_interface_create(hd, intf_id); |
| 469 | if (!intf) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 470 | pr_err("%d: Failed to create interface with id %hhu\n", |
| 471 | connection->intf_cport_id, intf_id); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 472 | goto free_svc_hotplug; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 473 | } |
| 474 | |
Viresh Kumar | 6bec5c7 | 2015-09-24 14:40:29 -0700 | [diff] [blame] | 475 | ret = gb_svc_read_and_clear_module_boot_status(intf); |
| 476 | if (ret) |
| 477 | goto destroy_interface; |
| 478 | |
Viresh Kumar | 3944a45 | 2015-08-12 09:19:31 +0530 | [diff] [blame] | 479 | intf->unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id); |
| 480 | intf->unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id); |
Johan Hovold | 9f59263 | 2015-11-25 15:58:56 +0100 | [diff] [blame] | 481 | intf->vendor_id = le32_to_cpu(hotplug->data.ara_vend_id); |
| 482 | intf->product_id = le32_to_cpu(hotplug->data.ara_prod_id); |
Viresh Kumar | 3944a45 | 2015-08-12 09:19:31 +0530 | [diff] [blame] | 483 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 484 | /* |
| 485 | * Create a device id for the interface: |
| 486 | * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC |
| 487 | * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP |
| 488 | * |
| 489 | * XXX Do we need to allocate device ID for SVC or the AP here? And what |
| 490 | * XXX about an AP with multiple interface blocks? |
| 491 | */ |
Johan Hovold | c09db18 | 2015-09-15 09:18:08 +0200 | [diff] [blame] | 492 | device_id = ida_simple_get(&svc->device_id_map, |
Johan Hovold | 89f637f | 2015-09-01 12:25:25 +0200 | [diff] [blame] | 493 | GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 494 | if (device_id < 0) { |
| 495 | ret = device_id; |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 496 | pr_err("%d: Failed to allocate device id for interface with id %hhu (%d)\n", |
| 497 | connection->intf_cport_id, intf_id, ret); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 498 | goto destroy_interface; |
| 499 | } |
| 500 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 501 | ret = gb_svc_intf_device_id(svc, intf_id, device_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 502 | if (ret) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 503 | pr_err("%d: Device id operation failed, interface %hhu device_id %hhu (%d)\n", |
| 504 | connection->intf_cport_id, intf_id, device_id, ret); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 505 | goto ida_put; |
| 506 | } |
| 507 | |
Perry Hung | 7e27546 | 2015-07-24 19:02:32 -0400 | [diff] [blame] | 508 | /* |
| 509 | * Create a two-way route between the AP and the new interface |
| 510 | */ |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame^] | 511 | ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP, |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 512 | intf_id, device_id); |
Perry Hung | 7e27546 | 2015-07-24 19:02:32 -0400 | [diff] [blame] | 513 | if (ret) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 514 | pr_err("%d: Route create operation failed, interface %hhu device_id %hhu (%d)\n", |
| 515 | connection->intf_cport_id, intf_id, device_id, ret); |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 516 | goto svc_id_free; |
Perry Hung | 7e27546 | 2015-07-24 19:02:32 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 519 | ret = gb_interface_init(intf, device_id); |
| 520 | if (ret) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 521 | pr_err("%d: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n", |
| 522 | connection->intf_cport_id, intf_id, device_id, ret); |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 523 | goto destroy_route; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 524 | } |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 525 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 526 | goto free_svc_hotplug; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 527 | |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 528 | destroy_route: |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame^] | 529 | gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 530 | svc_id_free: |
| 531 | /* |
| 532 | * XXX Should we tell SVC that this id doesn't belong to interface |
| 533 | * XXX anymore. |
| 534 | */ |
| 535 | ida_put: |
Johan Hovold | c09db18 | 2015-09-15 09:18:08 +0200 | [diff] [blame] | 536 | ida_simple_remove(&svc->device_id_map, device_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 537 | destroy_interface: |
Viresh Kumar | 80d1ede | 2015-09-23 16:48:10 -0700 | [diff] [blame] | 538 | gb_interface_remove(intf); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 539 | free_svc_hotplug: |
| 540 | kfree(svc_hotplug); |
| 541 | } |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 542 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 543 | /* |
| 544 | * Bringing up a module can be time consuming, as that may require lots of |
| 545 | * initialization on the module side. Over that, we may also need to download |
| 546 | * the firmware first and flash that on the module. |
| 547 | * |
| 548 | * In order to make other hotplug events to not wait for all this to finish, |
| 549 | * handle most of module hotplug stuff outside of the hotplug callback, with |
| 550 | * help of a workqueue. |
| 551 | */ |
| 552 | static int gb_svc_intf_hotplug_recv(struct gb_operation *op) |
| 553 | { |
| 554 | struct gb_message *request = op->request; |
| 555 | struct svc_hotplug *svc_hotplug; |
| 556 | |
| 557 | if (request->payload_size < sizeof(svc_hotplug->data)) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 558 | pr_err("%d: short hotplug request received (%zu < %zu)\n", |
| 559 | op->connection->intf_cport_id, request->payload_size, |
| 560 | sizeof(svc_hotplug->data)); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 561 | return -EINVAL; |
| 562 | } |
| 563 | |
Johan Hovold | 287bba8 | 2015-09-01 12:25:26 +0200 | [diff] [blame] | 564 | svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_KERNEL); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 565 | if (!svc_hotplug) |
| 566 | return -ENOMEM; |
| 567 | |
| 568 | svc_hotplug->connection = op->connection; |
| 569 | memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data)); |
| 570 | |
| 571 | INIT_WORK(&svc_hotplug->work, svc_process_hotplug); |
| 572 | queue_work(system_unbound_wq, &svc_hotplug->work); |
| 573 | |
| 574 | return 0; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op) |
| 578 | { |
| 579 | struct gb_message *request = op->request; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 580 | struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload; |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 581 | struct gb_host_device *hd = op->connection->hd; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 582 | struct gb_interface *intf; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 583 | u8 intf_id; |
| 584 | |
| 585 | if (request->payload_size < sizeof(*hot_unplug)) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 586 | pr_err("connection %d: short hot unplug request received (%zu < %zu)\n", |
| 587 | op->connection->intf_cport_id, request->payload_size, |
| 588 | sizeof(*hot_unplug)); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 589 | return -EINVAL; |
| 590 | } |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 591 | |
| 592 | intf_id = hot_unplug->intf_id; |
| 593 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 594 | intf = gb_interface_find(hd, intf_id); |
| 595 | if (!intf) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 596 | pr_err("connection %d: Couldn't find interface for id %hhu\n", |
| 597 | op->connection->intf_cport_id, intf_id); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 598 | return -EINVAL; |
| 599 | } |
| 600 | |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 601 | svc_intf_remove(op->connection, intf); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 602 | |
| 603 | return 0; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static int gb_svc_intf_reset_recv(struct gb_operation *op) |
| 607 | { |
| 608 | struct gb_message *request = op->request; |
| 609 | struct gb_svc_intf_reset_request *reset; |
| 610 | u8 intf_id; |
| 611 | |
| 612 | if (request->payload_size < sizeof(*reset)) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 613 | pr_err("connection %d: short reset request received (%zu < %zu)\n", |
| 614 | op->connection->intf_cport_id, request->payload_size, |
| 615 | sizeof(*reset)); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 616 | return -EINVAL; |
| 617 | } |
| 618 | reset = request->payload; |
| 619 | |
| 620 | intf_id = reset->intf_id; |
| 621 | |
| 622 | /* FIXME Reset the interface here */ |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static int gb_svc_request_recv(u8 type, struct gb_operation *op) |
| 628 | { |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 629 | struct gb_connection *connection = op->connection; |
| 630 | struct gb_svc *svc = connection->private; |
| 631 | int ret = 0; |
| 632 | |
| 633 | /* |
| 634 | * SVC requests need to follow a specific order (at least initially) and |
| 635 | * below code takes care of enforcing that. The expected order is: |
| 636 | * - PROTOCOL_VERSION |
| 637 | * - SVC_HELLO |
| 638 | * - Any other request, but the earlier two. |
| 639 | * |
| 640 | * Incoming requests are guaranteed to be serialized and so we don't |
| 641 | * need to protect 'state' for any races. |
| 642 | */ |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 643 | switch (type) { |
Viresh Kumar | 0e2462d | 2015-08-14 07:57:38 +0530 | [diff] [blame] | 644 | case GB_REQUEST_TYPE_PROTOCOL_VERSION: |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 645 | if (svc->state != GB_SVC_STATE_RESET) |
| 646 | ret = -EINVAL; |
| 647 | break; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 648 | case GB_SVC_TYPE_SVC_HELLO: |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 649 | if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION) |
| 650 | ret = -EINVAL; |
| 651 | break; |
| 652 | default: |
| 653 | if (svc->state != GB_SVC_STATE_SVC_HELLO) |
| 654 | ret = -EINVAL; |
| 655 | break; |
| 656 | } |
| 657 | |
| 658 | if (ret) { |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 659 | pr_warn("connection %d: unexpected SVC request 0x%02x received (state %u)\n", |
| 660 | connection->intf_cport_id, type, svc->state); |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | switch (type) { |
| 665 | case GB_REQUEST_TYPE_PROTOCOL_VERSION: |
| 666 | ret = gb_svc_version_request(op); |
| 667 | if (!ret) |
| 668 | svc->state = GB_SVC_STATE_PROTOCOL_VERSION; |
| 669 | return ret; |
| 670 | case GB_SVC_TYPE_SVC_HELLO: |
| 671 | ret = gb_svc_hello(op); |
| 672 | if (!ret) |
| 673 | svc->state = GB_SVC_STATE_SVC_HELLO; |
| 674 | return ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 675 | case GB_SVC_TYPE_INTF_HOTPLUG: |
| 676 | return gb_svc_intf_hotplug_recv(op); |
| 677 | case GB_SVC_TYPE_INTF_HOT_UNPLUG: |
| 678 | return gb_svc_intf_hot_unplug_recv(op); |
| 679 | case GB_SVC_TYPE_INTF_RESET: |
| 680 | return gb_svc_intf_reset_recv(op); |
| 681 | default: |
Greg Kroah-Hartman | b50a24e | 2015-10-16 16:53:31 -0700 | [diff] [blame] | 682 | pr_err("connection %d: unsupported request: %hhu\n", |
| 683 | connection->intf_cport_id, type); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 684 | return -EINVAL; |
| 685 | } |
| 686 | } |
| 687 | |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 688 | static void gb_svc_release(struct device *dev) |
| 689 | { |
Johan Hovold | 88f7b96 | 2015-11-25 15:59:08 +0100 | [diff] [blame] | 690 | struct gb_svc *svc = to_gb_svc(dev); |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 691 | |
| 692 | ida_destroy(&svc->device_id_map); |
| 693 | kfree(svc); |
| 694 | } |
| 695 | |
| 696 | struct device_type greybus_svc_type = { |
| 697 | .name = "greybus_svc", |
| 698 | .release = gb_svc_release, |
| 699 | }; |
| 700 | |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 701 | static int gb_svc_connection_init(struct gb_connection *connection) |
| 702 | { |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 703 | struct gb_host_device *hd = connection->hd; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 704 | struct gb_svc *svc; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 705 | |
| 706 | svc = kzalloc(sizeof(*svc), GFP_KERNEL); |
| 707 | if (!svc) |
| 708 | return -ENOMEM; |
| 709 | |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 710 | svc->dev.parent = &hd->dev; |
| 711 | svc->dev.bus = &greybus_bus_type; |
| 712 | svc->dev.type = &greybus_svc_type; |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame^] | 713 | svc->dev.groups = svc_groups; |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 714 | svc->dev.dma_mask = svc->dev.parent->dma_mask; |
| 715 | device_initialize(&svc->dev); |
| 716 | |
| 717 | dev_set_name(&svc->dev, "%d-svc", hd->bus_id); |
| 718 | |
Johan Hovold | 6106e51 | 2015-11-25 15:59:07 +0100 | [diff] [blame] | 719 | ida_init(&svc->device_id_map); |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 720 | svc->state = GB_SVC_STATE_RESET; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 721 | svc->connection = connection; |
| 722 | connection->private = svc; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 723 | |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 724 | hd->svc = svc; |
| 725 | |
Viresh Kumar | dcd0500 | 2015-07-24 15:32:20 +0530 | [diff] [blame] | 726 | WARN_ON(connection->hd->initial_svc_connection); |
| 727 | connection->hd->initial_svc_connection = connection; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 728 | |
Viresh Kumar | 18d777c | 2015-07-21 17:44:20 +0530 | [diff] [blame] | 729 | return 0; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | static void gb_svc_connection_exit(struct gb_connection *connection) |
| 733 | { |
| 734 | struct gb_svc *svc = connection->private; |
| 735 | |
Johan Hovold | 88f7b96 | 2015-11-25 15:59:08 +0100 | [diff] [blame] | 736 | if (device_is_registered(&svc->dev)) |
| 737 | device_del(&svc->dev); |
| 738 | |
Perry Hung | 75a60ed | 2015-07-24 19:02:33 -0400 | [diff] [blame] | 739 | connection->hd->svc = NULL; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 740 | connection->private = NULL; |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 741 | |
| 742 | put_device(&svc->dev); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static struct gb_protocol svc_protocol = { |
| 746 | .name = "svc", |
| 747 | .id = GREYBUS_PROTOCOL_SVC, |
Viresh Kumar | 06e305f | 2015-07-01 12:13:51 +0530 | [diff] [blame] | 748 | .major = GB_SVC_VERSION_MAJOR, |
| 749 | .minor = GB_SVC_VERSION_MINOR, |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 750 | .connection_init = gb_svc_connection_init, |
| 751 | .connection_exit = gb_svc_connection_exit, |
| 752 | .request_recv = gb_svc_request_recv, |
Viresh Kumar | 5a5296b | 2015-09-07 16:01:24 +0530 | [diff] [blame] | 753 | .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED | |
| 754 | GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED | |
| 755 | GB_PROTOCOL_NO_BUNDLE | |
| 756 | GB_PROTOCOL_SKIP_VERSION | |
| 757 | GB_PROTOCOL_SKIP_SVC_CONNECTION, |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 758 | }; |
Viresh Kumar | ab69c4c | 2015-07-03 17:00:29 +0530 | [diff] [blame] | 759 | gb_builtin_protocol_driver(svc_protocol); |