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