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