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 | |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 10 | #include "greybus.h" |
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 | |
Perry Hung | 0b22649 | 2015-07-24 19:02:34 -0400 | [diff] [blame] | 13 | #define CPORT_FLAGS_E2EFC (1) |
| 14 | #define CPORT_FLAGS_CSD_N (2) |
| 15 | #define CPORT_FLAGS_CSV_N (4) |
| 16 | |
Viresh Kumar | b45864d | 2015-07-24 15:32:21 +0530 | [diff] [blame] | 17 | struct gb_svc { |
| 18 | struct gb_connection *connection; |
| 19 | u8 version_major; |
| 20 | u8 version_minor; |
| 21 | }; |
| 22 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame^] | 23 | struct svc_hotplug { |
| 24 | struct work_struct work; |
| 25 | struct gb_connection *connection; |
| 26 | struct gb_svc_intf_hotplug_request data; |
| 27 | }; |
| 28 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 29 | static struct ida greybus_svc_device_id_map; |
| 30 | |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 31 | /* |
| 32 | * AP's SVC cport is required early to get messages from the SVC. This happens |
| 33 | * even before the Endo is created and hence any modules or interfaces. |
| 34 | * |
| 35 | * This is a temporary connection, used only at initial bootup. |
| 36 | */ |
| 37 | struct gb_connection * |
| 38 | gb_ap_svc_connection_create(struct greybus_host_device *hd) |
| 39 | { |
| 40 | struct gb_connection *connection; |
| 41 | |
| 42 | connection = gb_connection_create_range(hd, NULL, hd->parent, |
| 43 | GB_SVC_CPORT_ID, |
| 44 | GREYBUS_PROTOCOL_SVC, |
| 45 | GB_SVC_CPORT_ID, |
| 46 | GB_SVC_CPORT_ID + 1); |
| 47 | |
| 48 | return connection; |
| 49 | } |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * We know endo-type and AP's interface id now, lets create a proper svc |
| 53 | * connection (and its interface/bundle) now and get rid of the initial |
| 54 | * 'partially' initialized one svc connection. |
| 55 | */ |
| 56 | static struct gb_interface * |
| 57 | gb_ap_interface_create(struct greybus_host_device *hd, |
| 58 | struct gb_connection *connection, u8 interface_id) |
| 59 | { |
| 60 | struct gb_interface *intf; |
| 61 | struct device *dev = &hd->endo->dev; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 62 | |
| 63 | intf = gb_interface_create(hd, interface_id); |
| 64 | if (!intf) { |
| 65 | dev_err(dev, "%s: Failed to create interface with id %hhu\n", |
| 66 | __func__, interface_id); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | intf->device_id = GB_DEVICE_ID_AP; |
Viresh Kumar | 67c93ae | 2015-07-24 15:32:19 +0530 | [diff] [blame] | 71 | svc_update_connection(intf, connection); |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 72 | |
Viresh Kumar | dcd0500 | 2015-07-24 15:32:20 +0530 | [diff] [blame] | 73 | /* Its no longer a partially initialized connection */ |
| 74 | hd->initial_svc_connection = NULL; |
| 75 | |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 76 | return intf; |
| 77 | } |
| 78 | |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 79 | static int intf_device_id_operation(struct gb_svc *svc, |
| 80 | u8 intf_id, u8 device_id) |
| 81 | { |
| 82 | struct gb_svc_intf_device_id_request request; |
| 83 | |
| 84 | request.intf_id = intf_id; |
| 85 | request.device_id = device_id; |
| 86 | |
| 87 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID, |
| 88 | &request, sizeof(request), NULL, 0); |
| 89 | } |
| 90 | |
| 91 | static int intf_reset_operation(struct gb_svc *svc, u8 intf_id) |
| 92 | { |
| 93 | struct gb_svc_intf_reset_request request; |
| 94 | |
| 95 | request.intf_id = intf_id; |
| 96 | |
| 97 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET, |
| 98 | &request, sizeof(request), NULL, 0); |
| 99 | } |
| 100 | |
| 101 | static int connection_create_operation(struct gb_svc *svc, |
| 102 | u8 intf1_id, u16 cport1_id, |
| 103 | u8 intf2_id, u16 cport2_id) |
| 104 | { |
| 105 | struct gb_svc_conn_create_request request; |
| 106 | |
| 107 | request.intf1_id = intf1_id; |
| 108 | request.cport1_id = cport1_id; |
| 109 | request.intf2_id = intf2_id; |
| 110 | request.cport2_id = cport2_id; |
Perry Hung | 0b22649 | 2015-07-24 19:02:34 -0400 | [diff] [blame] | 111 | /* |
| 112 | * XXX: fix connections paramaters to TC0 and all CPort flags |
| 113 | * for now. |
| 114 | */ |
| 115 | request.tc = 0; |
| 116 | request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 117 | |
| 118 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE, |
| 119 | &request, sizeof(request), NULL, 0); |
| 120 | } |
| 121 | |
| 122 | static int connection_destroy_operation(struct gb_svc *svc, |
| 123 | u8 intf1_id, u16 cport1_id, |
| 124 | u8 intf2_id, u16 cport2_id) |
| 125 | { |
| 126 | struct gb_svc_conn_destroy_request request; |
| 127 | |
| 128 | request.intf1_id = intf1_id; |
| 129 | request.cport1_id = cport1_id; |
| 130 | request.intf2_id = intf2_id; |
| 131 | request.cport2_id = cport2_id; |
| 132 | |
| 133 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_DESTROY, |
| 134 | &request, sizeof(request), NULL, 0); |
| 135 | } |
| 136 | |
Perry Hung | e08aaa4 | 2015-07-24 19:02:31 -0400 | [diff] [blame] | 137 | static int route_create_operation(struct gb_svc *svc, u8 intf1_id, u8 dev1_id, |
| 138 | u8 intf2_id, u8 dev2_id) |
| 139 | { |
| 140 | struct gb_svc_route_create_request request; |
| 141 | |
| 142 | request.intf1_id = intf1_id; |
| 143 | request.dev1_id = dev1_id; |
| 144 | request.intf2_id = intf2_id; |
| 145 | request.dev2_id = dev2_id; |
| 146 | |
| 147 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE, |
| 148 | &request, sizeof(request), NULL, 0); |
| 149 | } |
| 150 | |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 151 | int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id) |
| 152 | { |
| 153 | return intf_device_id_operation(svc, intf_id, device_id); |
| 154 | } |
| 155 | EXPORT_SYMBOL_GPL(gb_svc_intf_device_id); |
| 156 | |
| 157 | int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id) |
| 158 | { |
| 159 | return intf_reset_operation(svc, intf_id); |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(gb_svc_intf_reset); |
| 162 | |
| 163 | int gb_svc_connection_create(struct gb_svc *svc, |
| 164 | u8 intf1_id, u16 cport1_id, |
| 165 | u8 intf2_id, u16 cport2_id) |
| 166 | { |
| 167 | return connection_create_operation(svc, intf1_id, cport1_id, |
| 168 | intf2_id, cport2_id); |
| 169 | } |
| 170 | EXPORT_SYMBOL_GPL(gb_svc_connection_create); |
| 171 | |
| 172 | int gb_svc_connection_destroy(struct gb_svc *svc, |
| 173 | u8 intf1_id, u16 cport1_id, |
| 174 | u8 intf2_id, u16 cport2_id) |
| 175 | { |
| 176 | return connection_destroy_operation(svc, intf1_id, cport1_id, |
| 177 | intf2_id, cport2_id); |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(gb_svc_connection_destroy); |
| 180 | |
Perry Hung | e08aaa4 | 2015-07-24 19:02:31 -0400 | [diff] [blame] | 181 | int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id, |
Viresh Kumar | 03cb4fa | 2015-07-29 11:44:08 +0530 | [diff] [blame] | 182 | u8 intf2_id, u8 dev2_id) |
| 183 | { |
Perry Hung | e08aaa4 | 2015-07-24 19:02:31 -0400 | [diff] [blame] | 184 | return route_create_operation(svc, intf1_id, dev1_id, |
| 185 | intf2_id, dev2_id); |
| 186 | } |
| 187 | EXPORT_SYMBOL_GPL(gb_svc_route_create); |
| 188 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 189 | static int gb_svc_version_request(struct gb_operation *op) |
| 190 | { |
| 191 | struct gb_connection *connection = op->connection; |
| 192 | struct gb_protocol_version_response *version; |
| 193 | struct device *dev = &connection->dev; |
| 194 | |
| 195 | version = op->request->payload; |
| 196 | |
| 197 | if (version->major > GB_SVC_VERSION_MAJOR) { |
| 198 | dev_err(&connection->dev, |
| 199 | "unsupported major version (%hhu > %hhu)\n", |
| 200 | version->major, GB_SVC_VERSION_MAJOR); |
| 201 | return -ENOTSUPP; |
| 202 | } |
| 203 | |
| 204 | if (!gb_operation_response_alloc(op, sizeof(*version), GFP_KERNEL)) { |
| 205 | dev_err(dev, "%s: error allocating response\n", |
| 206 | __func__); |
| 207 | return -ENOMEM; |
| 208 | } |
| 209 | |
| 210 | version = op->response->payload; |
| 211 | version->major = GB_SVC_VERSION_MAJOR; |
| 212 | version->minor = GB_SVC_VERSION_MINOR; |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int gb_svc_hello(struct gb_operation *op) |
| 217 | { |
| 218 | struct gb_connection *connection = op->connection; |
| 219 | struct greybus_host_device *hd = connection->hd; |
| 220 | struct gb_svc_hello_request *hello_request; |
| 221 | struct device *dev = &connection->dev; |
| 222 | struct gb_interface *intf; |
| 223 | u16 endo_id; |
| 224 | u8 interface_id; |
| 225 | int ret; |
| 226 | |
| 227 | /* Hello message should be received only during early bootup */ |
| 228 | WARN_ON(hd->initial_svc_connection != connection); |
| 229 | |
| 230 | /* |
| 231 | * SVC sends information about the endo and interface-id on the hello |
| 232 | * request, use that to create an endo. |
| 233 | */ |
| 234 | if (op->request->payload_size != sizeof(*hello_request)) { |
Viresh Kumar | 155b9f1 | 2015-07-23 10:30:47 +0530 | [diff] [blame] | 235 | dev_err(dev, "%s: Illegal size of hello request (%zu %zu)\n", |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 236 | __func__, op->request->payload_size, |
| 237 | sizeof(*hello_request)); |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | |
| 241 | hello_request = op->request->payload; |
| 242 | endo_id = le16_to_cpu(hello_request->endo_id); |
| 243 | interface_id = hello_request->interface_id; |
| 244 | |
| 245 | /* Setup Endo */ |
| 246 | ret = greybus_endo_setup(hd, endo_id, interface_id); |
| 247 | if (ret) |
| 248 | return ret; |
| 249 | |
| 250 | /* |
| 251 | * Endo and its modules are ready now, fix AP's partially initialized |
| 252 | * svc protocol and its connection. |
| 253 | */ |
| 254 | intf = gb_ap_interface_create(hd, connection, interface_id); |
| 255 | if (!intf) { |
| 256 | gb_endo_remove(hd->endo); |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame^] | 263 | /* |
| 264 | * 'struct svc_hotplug' should be freed by svc_process_hotplug() before it |
| 265 | * returns, irrespective of success or Failure in bringing up the module. |
| 266 | */ |
| 267 | static void svc_process_hotplug(struct work_struct *work) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 268 | { |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame^] | 269 | struct svc_hotplug *svc_hotplug = container_of(work, struct svc_hotplug, |
| 270 | work); |
| 271 | struct gb_svc_intf_hotplug_request *hotplug = &svc_hotplug->data; |
| 272 | struct gb_connection *connection = svc_hotplug->connection; |
| 273 | struct gb_svc *svc = connection->private; |
| 274 | struct greybus_host_device *hd = connection->bundle->intf->hd; |
| 275 | struct device *dev = &connection->dev; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 276 | struct gb_interface *intf; |
| 277 | u8 intf_id, device_id; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 278 | u32 unipro_mfg_id; |
| 279 | u32 unipro_prod_id; |
| 280 | u32 ara_vend_id; |
| 281 | u32 ara_prod_id; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 282 | int ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 283 | |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 284 | /* |
| 285 | * Grab the information we need. |
Viresh Kumar | 7eb8919 | 2015-07-01 12:13:50 +0530 | [diff] [blame] | 286 | */ |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 287 | intf_id = hotplug->intf_id; |
Phong Tran | ea15a40 | 2015-05-27 21:31:02 +0700 | [diff] [blame] | 288 | unipro_mfg_id = le32_to_cpu(hotplug->data.unipro_mfg_id); |
| 289 | unipro_prod_id = le32_to_cpu(hotplug->data.unipro_prod_id); |
| 290 | ara_vend_id = le32_to_cpu(hotplug->data.ara_vend_id); |
| 291 | ara_prod_id = le32_to_cpu(hotplug->data.ara_prod_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 292 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 293 | // FIXME May require firmware download |
| 294 | intf = gb_interface_create(hd, intf_id); |
| 295 | if (!intf) { |
| 296 | dev_err(dev, "%s: Failed to create interface with id %hhu\n", |
| 297 | __func__, intf_id); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame^] | 298 | goto free_svc_hotplug; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Create a device id for the interface: |
| 303 | * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC |
| 304 | * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP |
| 305 | * |
| 306 | * XXX Do we need to allocate device ID for SVC or the AP here? And what |
| 307 | * XXX about an AP with multiple interface blocks? |
| 308 | */ |
| 309 | device_id = ida_simple_get(&greybus_svc_device_id_map, |
| 310 | GB_DEVICE_ID_MODULES_START, 0, GFP_ATOMIC); |
| 311 | if (device_id < 0) { |
| 312 | ret = device_id; |
| 313 | dev_err(dev, "%s: Failed to allocate device id for interface with id %hhu (%d)\n", |
| 314 | __func__, intf_id, ret); |
| 315 | goto destroy_interface; |
| 316 | } |
| 317 | |
| 318 | ret = intf_device_id_operation(svc, intf_id, device_id); |
| 319 | if (ret) { |
| 320 | dev_err(dev, "%s: Device id operation failed, interface %hhu device_id %hhu (%d)\n", |
| 321 | __func__, intf_id, device_id, ret); |
| 322 | goto ida_put; |
| 323 | } |
| 324 | |
Perry Hung | 7e27546 | 2015-07-24 19:02:32 -0400 | [diff] [blame] | 325 | /* |
| 326 | * Create a two-way route between the AP and the new interface |
| 327 | */ |
| 328 | ret = route_create_operation(svc, hd->endo->ap_intf_id, |
| 329 | GB_DEVICE_ID_AP, intf_id, device_id); |
| 330 | if (ret) { |
| 331 | dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n", |
| 332 | __func__, intf_id, device_id, ret); |
| 333 | goto ida_put; |
| 334 | } |
| 335 | |
| 336 | ret = route_create_operation(svc, intf_id, device_id, |
| 337 | hd->endo->ap_intf_id, GB_DEVICE_ID_AP); |
| 338 | if (ret) { |
| 339 | dev_err(dev, "%s: Route create operation failed, interface %hhu device_id %hhu (%d)\n", |
| 340 | __func__, intf_id, device_id, ret); |
| 341 | goto ida_put; |
| 342 | } |
| 343 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 344 | ret = gb_interface_init(intf, device_id); |
| 345 | if (ret) { |
| 346 | dev_err(dev, "%s: Failed to initialize interface, interface %hhu device_id %hhu (%d)\n", |
| 347 | __func__, intf_id, device_id, ret); |
| 348 | goto svc_id_free; |
| 349 | } |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 350 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame^] | 351 | goto free_svc_hotplug; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 352 | |
| 353 | svc_id_free: |
| 354 | /* |
| 355 | * XXX Should we tell SVC that this id doesn't belong to interface |
| 356 | * XXX anymore. |
| 357 | */ |
| 358 | ida_put: |
| 359 | ida_simple_remove(&greybus_svc_device_id_map, device_id); |
| 360 | destroy_interface: |
| 361 | gb_interface_remove(hd, intf_id); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame^] | 362 | free_svc_hotplug: |
| 363 | kfree(svc_hotplug); |
| 364 | } |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 365 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame^] | 366 | /* |
| 367 | * Bringing up a module can be time consuming, as that may require lots of |
| 368 | * initialization on the module side. Over that, we may also need to download |
| 369 | * the firmware first and flash that on the module. |
| 370 | * |
| 371 | * In order to make other hotplug events to not wait for all this to finish, |
| 372 | * handle most of module hotplug stuff outside of the hotplug callback, with |
| 373 | * help of a workqueue. |
| 374 | */ |
| 375 | static int gb_svc_intf_hotplug_recv(struct gb_operation *op) |
| 376 | { |
| 377 | struct gb_message *request = op->request; |
| 378 | struct svc_hotplug *svc_hotplug; |
| 379 | |
| 380 | if (request->payload_size < sizeof(svc_hotplug->data)) { |
| 381 | dev_err(&op->connection->dev, |
| 382 | "%s: short hotplug request received (%zu < %zu)\n", |
| 383 | __func__, request->payload_size, |
| 384 | sizeof(svc_hotplug->data)); |
| 385 | return -EINVAL; |
| 386 | } |
| 387 | |
| 388 | svc_hotplug = kmalloc(sizeof(*svc_hotplug), GFP_ATOMIC); |
| 389 | if (!svc_hotplug) |
| 390 | return -ENOMEM; |
| 391 | |
| 392 | svc_hotplug->connection = op->connection; |
| 393 | memcpy(&svc_hotplug->data, op->request->payload, sizeof(svc_hotplug->data)); |
| 394 | |
| 395 | INIT_WORK(&svc_hotplug->work, svc_process_hotplug); |
| 396 | queue_work(system_unbound_wq, &svc_hotplug->work); |
| 397 | |
| 398 | return 0; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op) |
| 402 | { |
| 403 | struct gb_message *request = op->request; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 404 | struct gb_svc_intf_hot_unplug_request *hot_unplug = request->payload; |
| 405 | struct greybus_host_device *hd = op->connection->bundle->intf->hd; |
| 406 | struct device *dev = &op->connection->dev; |
| 407 | u8 device_id; |
| 408 | struct gb_interface *intf; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 409 | u8 intf_id; |
| 410 | |
| 411 | if (request->payload_size < sizeof(*hot_unplug)) { |
| 412 | dev_err(&op->connection->dev, |
Viresh Kumar | 6d05ad3 | 2015-08-06 12:44:54 +0530 | [diff] [blame] | 413 | "short hot unplug request received (%zu < %zu)\n", |
| 414 | request->payload_size, sizeof(*hot_unplug)); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 415 | return -EINVAL; |
| 416 | } |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 417 | |
| 418 | intf_id = hot_unplug->intf_id; |
| 419 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 420 | intf = gb_interface_find(hd, intf_id); |
| 421 | if (!intf) { |
| 422 | dev_err(dev, "%s: Couldn't find interface for id %hhu\n", |
| 423 | __func__, intf_id); |
| 424 | return -EINVAL; |
| 425 | } |
| 426 | |
| 427 | device_id = intf->device_id; |
| 428 | gb_interface_remove(hd, intf_id); |
| 429 | ida_simple_remove(&greybus_svc_device_id_map, device_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 430 | |
| 431 | return 0; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static int gb_svc_intf_reset_recv(struct gb_operation *op) |
| 435 | { |
| 436 | struct gb_message *request = op->request; |
| 437 | struct gb_svc_intf_reset_request *reset; |
| 438 | u8 intf_id; |
| 439 | |
| 440 | if (request->payload_size < sizeof(*reset)) { |
| 441 | dev_err(&op->connection->dev, |
Viresh Kumar | 6d05ad3 | 2015-08-06 12:44:54 +0530 | [diff] [blame] | 442 | "short reset request received (%zu < %zu)\n", |
| 443 | request->payload_size, sizeof(*reset)); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 444 | return -EINVAL; |
| 445 | } |
| 446 | reset = request->payload; |
| 447 | |
| 448 | intf_id = reset->intf_id; |
| 449 | |
| 450 | /* FIXME Reset the interface here */ |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int gb_svc_request_recv(u8 type, struct gb_operation *op) |
| 456 | { |
| 457 | switch (type) { |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 458 | case GB_SVC_TYPE_PROTOCOL_VERSION: |
| 459 | return gb_svc_version_request(op); |
| 460 | case GB_SVC_TYPE_SVC_HELLO: |
| 461 | return gb_svc_hello(op); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 462 | case GB_SVC_TYPE_INTF_HOTPLUG: |
| 463 | return gb_svc_intf_hotplug_recv(op); |
| 464 | case GB_SVC_TYPE_INTF_HOT_UNPLUG: |
| 465 | return gb_svc_intf_hot_unplug_recv(op); |
| 466 | case GB_SVC_TYPE_INTF_RESET: |
| 467 | return gb_svc_intf_reset_recv(op); |
| 468 | default: |
| 469 | dev_err(&op->connection->dev, |
| 470 | "unsupported request: %hhu\n", type); |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | } |
| 474 | |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 475 | static int gb_svc_connection_init(struct gb_connection *connection) |
| 476 | { |
| 477 | struct gb_svc *svc; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 478 | |
| 479 | svc = kzalloc(sizeof(*svc), GFP_KERNEL); |
| 480 | if (!svc) |
| 481 | return -ENOMEM; |
| 482 | |
Perry Hung | 75a60ed | 2015-07-24 19:02:33 -0400 | [diff] [blame] | 483 | connection->hd->svc = svc; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 484 | svc->connection = connection; |
| 485 | connection->private = svc; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 486 | |
Viresh Kumar | dcd0500 | 2015-07-24 15:32:20 +0530 | [diff] [blame] | 487 | WARN_ON(connection->hd->initial_svc_connection); |
| 488 | connection->hd->initial_svc_connection = connection; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 489 | |
| 490 | ida_init(&greybus_svc_device_id_map); |
| 491 | |
Viresh Kumar | 18d777c | 2015-07-21 17:44:20 +0530 | [diff] [blame] | 492 | return 0; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static void gb_svc_connection_exit(struct gb_connection *connection) |
| 496 | { |
| 497 | struct gb_svc *svc = connection->private; |
| 498 | |
Perry Hung | 75a60ed | 2015-07-24 19:02:33 -0400 | [diff] [blame] | 499 | connection->hd->svc = NULL; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 500 | connection->private = NULL; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 501 | kfree(svc); |
| 502 | } |
| 503 | |
| 504 | static struct gb_protocol svc_protocol = { |
| 505 | .name = "svc", |
| 506 | .id = GREYBUS_PROTOCOL_SVC, |
Viresh Kumar | 06e305f | 2015-07-01 12:13:51 +0530 | [diff] [blame] | 507 | .major = GB_SVC_VERSION_MAJOR, |
| 508 | .minor = GB_SVC_VERSION_MINOR, |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 509 | .connection_init = gb_svc_connection_init, |
| 510 | .connection_exit = gb_svc_connection_exit, |
| 511 | .request_recv = gb_svc_request_recv, |
| 512 | }; |
Viresh Kumar | ab69c4c | 2015-07-03 17:00:29 +0530 | [diff] [blame] | 513 | gb_builtin_protocol_driver(svc_protocol); |