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