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