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 | |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 10 | #include <linux/debugfs.h> |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 11 | #include <linux/input.h> |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 12 | #include <linux/workqueue.h> |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 13 | |
Viresh Kumar | f66427a | 2015-09-02 21:27:13 +0530 | [diff] [blame] | 14 | #include "greybus.h" |
| 15 | |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 16 | #define SVC_KEY_ARA_BUTTON KEY_A |
Viresh Kumar | b45864d | 2015-07-24 15:32:21 +0530 | [diff] [blame] | 17 | |
Johan Hovold | d18da86 | 2016-03-09 12:20:46 +0100 | [diff] [blame] | 18 | #define SVC_INTF_EJECT_TIMEOUT 9000 |
| 19 | |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 20 | struct gb_svc_deferred_request { |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 21 | struct work_struct work; |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 22 | struct gb_operation *operation; |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 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 | |
Rui Miguel Silva | 2c92bd5 | 2016-01-11 13:46:33 +0000 | [diff] [blame] | 44 | |
| 45 | // FIXME |
| 46 | // This is a hack, we need to do this "right" and clean the interface up |
| 47 | // properly, not just forcibly yank the thing out of the system and hope for the |
| 48 | // best. But for now, people want their modules to come out without having to |
| 49 | // throw the thing to the ground or get out a screwdriver. |
| 50 | static ssize_t intf_eject_store(struct device *dev, |
| 51 | struct device_attribute *attr, const char *buf, |
| 52 | size_t len) |
| 53 | { |
| 54 | struct gb_svc *svc = to_gb_svc(dev); |
| 55 | unsigned short intf_id; |
| 56 | int ret; |
| 57 | |
| 58 | ret = kstrtou16(buf, 10, &intf_id); |
| 59 | if (ret < 0) |
| 60 | return ret; |
| 61 | |
| 62 | dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id); |
| 63 | |
| 64 | ret = gb_svc_intf_eject(svc, intf_id); |
| 65 | if (ret < 0) |
| 66 | return ret; |
| 67 | |
| 68 | return len; |
| 69 | } |
| 70 | static DEVICE_ATTR_WO(intf_eject); |
| 71 | |
Greg Kroah-Hartman | d562853 | 2016-01-26 15:17:08 -0800 | [diff] [blame] | 72 | static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr, |
| 73 | char *buf) |
| 74 | { |
| 75 | struct gb_svc *svc = to_gb_svc(dev); |
| 76 | |
| 77 | return sprintf(buf, "%s\n", |
| 78 | gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled"); |
| 79 | } |
| 80 | |
| 81 | static ssize_t watchdog_store(struct device *dev, |
| 82 | struct device_attribute *attr, const char *buf, |
| 83 | size_t len) |
| 84 | { |
| 85 | struct gb_svc *svc = to_gb_svc(dev); |
| 86 | int retval; |
| 87 | bool user_request; |
| 88 | |
| 89 | retval = strtobool(buf, &user_request); |
| 90 | if (retval) |
| 91 | return retval; |
| 92 | |
| 93 | if (user_request) |
| 94 | retval = gb_svc_watchdog_enable(svc); |
| 95 | else |
| 96 | retval = gb_svc_watchdog_disable(svc); |
| 97 | if (retval) |
| 98 | return retval; |
| 99 | return len; |
| 100 | } |
| 101 | static DEVICE_ATTR_RW(watchdog); |
| 102 | |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 103 | static int gb_svc_pwrmon_rail_count_get(struct gb_svc *svc, u8 *value) |
| 104 | { |
| 105 | struct gb_svc_pwrmon_rail_count_get_response response; |
| 106 | int ret; |
| 107 | |
| 108 | ret = gb_operation_sync(svc->connection, |
| 109 | GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET, NULL, 0, |
| 110 | &response, sizeof(response)); |
| 111 | if (ret) { |
Johan Hovold | 89f2df4 | 2016-04-21 11:43:36 +0200 | [diff] [blame] | 112 | dev_err(&svc->dev, "failed to get rail count: %d\n", ret); |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | *value = response.rail_count; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int gb_svc_pwrmon_rail_names_get(struct gb_svc *svc, |
Johan Hovold | f35fdb2 | 2016-04-21 11:43:38 +0200 | [diff] [blame] | 122 | struct gb_svc_pwrmon_rail_names_get_response *response, |
| 123 | size_t bufsize) |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 124 | { |
| 125 | int ret; |
| 126 | |
| 127 | ret = gb_operation_sync(svc->connection, |
| 128 | GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET, NULL, 0, |
| 129 | response, bufsize); |
| 130 | if (ret) { |
Johan Hovold | 89f2df4 | 2016-04-21 11:43:36 +0200 | [diff] [blame] | 131 | dev_err(&svc->dev, "failed to get rail names: %d\n", ret); |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int gb_svc_pwrmon_sample_get(struct gb_svc *svc, u8 rail_id, |
| 139 | u8 measurement_type, u32 *value) |
| 140 | { |
| 141 | struct gb_svc_pwrmon_sample_get_request request; |
| 142 | struct gb_svc_pwrmon_sample_get_response response; |
| 143 | int ret; |
| 144 | |
| 145 | request.rail_id = rail_id; |
| 146 | request.measurement_type = measurement_type; |
| 147 | |
| 148 | ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_PWRMON_SAMPLE_GET, |
| 149 | &request, sizeof(request), |
| 150 | &response, sizeof(response)); |
| 151 | if (ret) { |
Johan Hovold | 89f2df4 | 2016-04-21 11:43:36 +0200 | [diff] [blame] | 152 | dev_err(&svc->dev, "failed to get rail sample: %d\n", ret); |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | if (response.result) { |
| 157 | dev_err(&svc->dev, |
| 158 | "UniPro error while getting rail power sample (%d %d): %d\n", |
| 159 | rail_id, measurement_type, response.result); |
| 160 | switch (response.result) { |
| 161 | case GB_SVC_PWRMON_GET_SAMPLE_INVAL: |
| 162 | return -EINVAL; |
| 163 | case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP: |
Johan Hovold | 5b35ef9 | 2016-04-21 11:43:37 +0200 | [diff] [blame] | 164 | return -ENOMSG; |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 165 | default: |
| 166 | return -EIO; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | *value = le32_to_cpu(response.measurement); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
David Lin | ddb10c8 | 2016-04-07 20:15:30 -0700 | [diff] [blame] | 175 | int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id, |
| 176 | u8 measurement_type, u32 *value) |
| 177 | { |
| 178 | struct gb_svc_pwrmon_intf_sample_get_request request; |
| 179 | struct gb_svc_pwrmon_intf_sample_get_response response; |
| 180 | int ret; |
| 181 | |
| 182 | request.intf_id = intf_id; |
| 183 | request.measurement_type = measurement_type; |
| 184 | |
| 185 | ret = gb_operation_sync(svc->connection, |
| 186 | GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET, |
| 187 | &request, sizeof(request), |
| 188 | &response, sizeof(response)); |
| 189 | if (ret) { |
Johan Hovold | 89f2df4 | 2016-04-21 11:43:36 +0200 | [diff] [blame] | 190 | dev_err(&svc->dev, "failed to get intf sample: %d\n", ret); |
David Lin | ddb10c8 | 2016-04-07 20:15:30 -0700 | [diff] [blame] | 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | if (response.result) { |
| 195 | dev_err(&svc->dev, |
| 196 | "UniPro error while getting intf power sample (%d %d): %d\n", |
| 197 | intf_id, measurement_type, response.result); |
| 198 | switch (response.result) { |
| 199 | case GB_SVC_PWRMON_GET_SAMPLE_INVAL: |
| 200 | return -EINVAL; |
| 201 | case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP: |
| 202 | return -ENOSYS; |
| 203 | default: |
| 204 | return -EIO; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | *value = le32_to_cpu(response.measurement); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame] | 213 | static struct attribute *svc_attrs[] = { |
| 214 | &dev_attr_endo_id.attr, |
| 215 | &dev_attr_ap_intf_id.attr, |
Rui Miguel Silva | 2c92bd5 | 2016-01-11 13:46:33 +0000 | [diff] [blame] | 216 | &dev_attr_intf_eject.attr, |
Greg Kroah-Hartman | d562853 | 2016-01-26 15:17:08 -0800 | [diff] [blame] | 217 | &dev_attr_watchdog.attr, |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame] | 218 | NULL, |
| 219 | }; |
| 220 | ATTRIBUTE_GROUPS(svc); |
| 221 | |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 222 | 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] | 223 | { |
| 224 | struct gb_svc_intf_device_id_request request; |
| 225 | |
| 226 | request.intf_id = intf_id; |
| 227 | request.device_id = device_id; |
| 228 | |
| 229 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID, |
| 230 | &request, sizeof(request), NULL, 0); |
| 231 | } |
| 232 | |
Rui Miguel Silva | c5d55fb | 2016-01-11 13:46:31 +0000 | [diff] [blame] | 233 | int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id) |
| 234 | { |
| 235 | struct gb_svc_intf_eject_request request; |
Johan Hovold | e676ccd | 2016-03-09 12:20:45 +0100 | [diff] [blame] | 236 | int ret; |
Rui Miguel Silva | c5d55fb | 2016-01-11 13:46:31 +0000 | [diff] [blame] | 237 | |
| 238 | request.intf_id = intf_id; |
| 239 | |
| 240 | /* |
| 241 | * The pulse width for module release in svc is long so we need to |
| 242 | * increase the timeout so the operation will not return to soon. |
| 243 | */ |
Johan Hovold | e676ccd | 2016-03-09 12:20:45 +0100 | [diff] [blame] | 244 | ret = gb_operation_sync_timeout(svc->connection, |
| 245 | GB_SVC_TYPE_INTF_EJECT, &request, |
| 246 | sizeof(request), NULL, 0, |
Johan Hovold | d18da86 | 2016-03-09 12:20:46 +0100 | [diff] [blame] | 247 | SVC_INTF_EJECT_TIMEOUT); |
Johan Hovold | e676ccd | 2016-03-09 12:20:45 +0100 | [diff] [blame] | 248 | if (ret) { |
| 249 | dev_err(&svc->dev, "failed to eject interface %u\n", intf_id); |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | return 0; |
Rui Miguel Silva | c5d55fb | 2016-01-11 13:46:31 +0000 | [diff] [blame] | 254 | } |
Rui Miguel Silva | c5d55fb | 2016-01-11 13:46:31 +0000 | [diff] [blame] | 255 | |
Johan Hovold | 017482b | 2016-04-23 18:47:27 +0200 | [diff] [blame] | 256 | int gb_svc_intf_vsys_set(struct gb_svc *svc, u8 intf_id, bool enable) |
| 257 | { |
| 258 | /* FIXME: implement */ |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | int gb_svc_intf_refclk_set(struct gb_svc *svc, u8 intf_id, bool enable) |
| 264 | { |
| 265 | /* FIXME: implement */ |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | int gb_svc_intf_unipro_set(struct gb_svc *svc, u8 intf_id, bool enable) |
| 271 | { |
| 272 | /* FIXME: implement */ |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Johan Hovold | 1e8e22b | 2016-04-23 18:47:28 +0200 | [diff] [blame^] | 277 | int gb_svc_intf_activate(struct gb_svc *svc, u8 intf_id, u8 *intf_type) |
| 278 | { |
| 279 | /* FIXME: implement */ |
| 280 | |
| 281 | *intf_type = GB_SVC_INTF_TYPE_GREYBUS; |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 286 | int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector, |
| 287 | u32 *value) |
| 288 | { |
| 289 | struct gb_svc_dme_peer_get_request request; |
| 290 | struct gb_svc_dme_peer_get_response response; |
| 291 | u16 result; |
| 292 | int ret; |
| 293 | |
| 294 | request.intf_id = intf_id; |
| 295 | request.attr = cpu_to_le16(attr); |
| 296 | request.selector = cpu_to_le16(selector); |
| 297 | |
| 298 | ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET, |
| 299 | &request, sizeof(request), |
| 300 | &response, sizeof(response)); |
| 301 | if (ret) { |
Viresh Kumar | b933fa4 | 2015-12-04 21:30:10 +0530 | [diff] [blame] | 302 | 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] | 303 | intf_id, attr, selector, ret); |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | result = le16_to_cpu(response.result_code); |
| 308 | if (result) { |
Viresh Kumar | b933fa4 | 2015-12-04 21:30:10 +0530 | [diff] [blame] | 309 | 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] | 310 | intf_id, attr, selector, result); |
Viresh Kumar | 4aac6c5 | 2015-12-04 21:30:08 +0530 | [diff] [blame] | 311 | return -EIO; |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (value) |
| 315 | *value = le32_to_cpu(response.attr_value); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get); |
| 320 | |
| 321 | int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector, |
| 322 | u32 value) |
| 323 | { |
| 324 | struct gb_svc_dme_peer_set_request request; |
| 325 | struct gb_svc_dme_peer_set_response response; |
| 326 | u16 result; |
| 327 | int ret; |
| 328 | |
| 329 | request.intf_id = intf_id; |
| 330 | request.attr = cpu_to_le16(attr); |
| 331 | request.selector = cpu_to_le16(selector); |
| 332 | request.value = cpu_to_le32(value); |
| 333 | |
| 334 | ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET, |
| 335 | &request, sizeof(request), |
| 336 | &response, sizeof(response)); |
| 337 | if (ret) { |
Viresh Kumar | b933fa4 | 2015-12-04 21:30:10 +0530 | [diff] [blame] | 338 | 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] | 339 | intf_id, attr, selector, value, ret); |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | result = le16_to_cpu(response.result_code); |
| 344 | if (result) { |
Viresh Kumar | b933fa4 | 2015-12-04 21:30:10 +0530 | [diff] [blame] | 345 | 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] | 346 | intf_id, attr, selector, value, result); |
Viresh Kumar | 4aac6c5 | 2015-12-04 21:30:08 +0530 | [diff] [blame] | 347 | return -EIO; |
Viresh Kumar | 19151c3 | 2015-09-09 21:08:29 +0530 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set); |
| 353 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 354 | int gb_svc_connection_create(struct gb_svc *svc, |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 355 | u8 intf1_id, u16 cport1_id, |
Viresh Kumar | 1575ef18 | 2015-10-07 15:40:24 -0400 | [diff] [blame] | 356 | u8 intf2_id, u16 cport2_id, |
Johan Hovold | 27f25c1 | 2016-03-03 13:34:38 +0100 | [diff] [blame] | 357 | u8 cport_flags) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 358 | { |
| 359 | struct gb_svc_conn_create_request request; |
| 360 | |
| 361 | request.intf1_id = intf1_id; |
Rui Miguel Silva | 2498050b | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 362 | request.cport1_id = cpu_to_le16(cport1_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 363 | request.intf2_id = intf2_id; |
Rui Miguel Silva | 2498050b | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 364 | request.cport2_id = cpu_to_le16(cport2_id); |
Johan Hovold | 34145b6 | 2016-03-03 13:34:37 +0100 | [diff] [blame] | 365 | request.tc = 0; /* TC0 */ |
Johan Hovold | 27f25c1 | 2016-03-03 13:34:38 +0100 | [diff] [blame] | 366 | request.flags = cport_flags; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 367 | |
| 368 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE, |
| 369 | &request, sizeof(request), NULL, 0); |
| 370 | } |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 371 | EXPORT_SYMBOL_GPL(gb_svc_connection_create); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 372 | |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 373 | void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id, |
| 374 | u8 intf2_id, u16 cport2_id) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 375 | { |
| 376 | struct gb_svc_conn_destroy_request request; |
Viresh Kumar | d9fcfff | 2015-08-31 17:21:05 +0530 | [diff] [blame] | 377 | struct gb_connection *connection = svc->connection; |
| 378 | int ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 379 | |
| 380 | request.intf1_id = intf1_id; |
Rui Miguel Silva | 2498050b | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 381 | request.cport1_id = cpu_to_le16(cport1_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 382 | request.intf2_id = intf2_id; |
Rui Miguel Silva | 2498050b | 2015-09-15 15:33:51 +0100 | [diff] [blame] | 383 | request.cport2_id = cpu_to_le16(cport2_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 384 | |
Viresh Kumar | d9fcfff | 2015-08-31 17:21:05 +0530 | [diff] [blame] | 385 | ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY, |
| 386 | &request, sizeof(request), NULL, 0); |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 387 | if (ret) { |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 388 | 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] | 389 | intf1_id, cport1_id, intf2_id, cport2_id, ret); |
| 390 | } |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 391 | } |
Viresh Kumar | 3f0e918 | 2015-08-31 17:21:06 +0530 | [diff] [blame] | 392 | EXPORT_SYMBOL_GPL(gb_svc_connection_destroy); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 393 | |
Viresh Kumar | bb10685 | 2015-09-07 16:01:25 +0530 | [diff] [blame] | 394 | /* Creates bi-directional routes between the devices */ |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 395 | int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id, |
Viresh Kumar | 505f16c | 2015-08-31 17:21:07 +0530 | [diff] [blame] | 396 | u8 intf2_id, u8 dev2_id) |
Perry Hung | e08aaa4 | 2015-07-24 19:02:31 -0400 | [diff] [blame] | 397 | { |
| 398 | struct gb_svc_route_create_request request; |
| 399 | |
| 400 | request.intf1_id = intf1_id; |
| 401 | request.dev1_id = dev1_id; |
| 402 | request.intf2_id = intf2_id; |
| 403 | request.dev2_id = dev2_id; |
| 404 | |
| 405 | return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE, |
| 406 | &request, sizeof(request), NULL, 0); |
| 407 | } |
Perry Hung | e08aaa4 | 2015-07-24 19:02:31 -0400 | [diff] [blame] | 408 | |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 409 | /* Destroys bi-directional routes between the devices */ |
Johan Hovold | 4d5f621 | 2016-03-29 18:56:04 -0400 | [diff] [blame] | 410 | void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id) |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 411 | { |
| 412 | struct gb_svc_route_destroy_request request; |
| 413 | int ret; |
| 414 | |
| 415 | request.intf1_id = intf1_id; |
| 416 | request.intf2_id = intf2_id; |
| 417 | |
| 418 | ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY, |
| 419 | &request, sizeof(request), NULL, 0); |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 420 | if (ret) { |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 421 | dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n", |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 422 | intf1_id, intf2_id, ret); |
| 423 | } |
Viresh Kumar | 0a02057 | 2015-09-07 18:05:26 +0530 | [diff] [blame] | 424 | } |
| 425 | |
Laurent Pinchart | aab4a1a | 2016-01-06 16:16:46 +0200 | [diff] [blame] | 426 | int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series, |
| 427 | u8 tx_mode, u8 tx_gear, u8 tx_nlanes, |
| 428 | u8 rx_mode, u8 rx_gear, u8 rx_nlanes, |
| 429 | u8 flags, u32 quirks) |
Laurent Pinchart | 784f876 | 2015-12-18 21:23:22 +0200 | [diff] [blame] | 430 | { |
Laurent Pinchart | aab4a1a | 2016-01-06 16:16:46 +0200 | [diff] [blame] | 431 | struct gb_svc_intf_set_pwrm_request request; |
| 432 | struct gb_svc_intf_set_pwrm_response response; |
| 433 | int ret; |
Laurent Pinchart | 784f876 | 2015-12-18 21:23:22 +0200 | [diff] [blame] | 434 | |
| 435 | request.intf_id = intf_id; |
Laurent Pinchart | aab4a1a | 2016-01-06 16:16:46 +0200 | [diff] [blame] | 436 | request.hs_series = hs_series; |
| 437 | request.tx_mode = tx_mode; |
| 438 | request.tx_gear = tx_gear; |
| 439 | request.tx_nlanes = tx_nlanes; |
| 440 | request.rx_mode = rx_mode; |
| 441 | request.rx_gear = rx_gear; |
| 442 | request.rx_nlanes = rx_nlanes; |
Laurent Pinchart | 784f876 | 2015-12-18 21:23:22 +0200 | [diff] [blame] | 443 | request.flags = flags; |
Laurent Pinchart | aab4a1a | 2016-01-06 16:16:46 +0200 | [diff] [blame] | 444 | request.quirks = cpu_to_le32(quirks); |
Laurent Pinchart | 784f876 | 2015-12-18 21:23:22 +0200 | [diff] [blame] | 445 | |
Laurent Pinchart | aab4a1a | 2016-01-06 16:16:46 +0200 | [diff] [blame] | 446 | ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM, |
| 447 | &request, sizeof(request), |
| 448 | &response, sizeof(response)); |
| 449 | if (ret < 0) |
| 450 | return ret; |
| 451 | |
| 452 | return le16_to_cpu(response.result_code); |
Laurent Pinchart | 784f876 | 2015-12-18 21:23:22 +0200 | [diff] [blame] | 453 | } |
Laurent Pinchart | aab4a1a | 2016-01-06 16:16:46 +0200 | [diff] [blame] | 454 | EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode); |
Laurent Pinchart | 784f876 | 2015-12-18 21:23:22 +0200 | [diff] [blame] | 455 | |
Greg Kroah-Hartman | 55ec09e | 2016-01-19 23:30:42 -0800 | [diff] [blame] | 456 | int gb_svc_ping(struct gb_svc *svc) |
| 457 | { |
Greg Kroah-Hartman | 839ac5b | 2016-01-26 08:57:50 -0800 | [diff] [blame] | 458 | return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING, |
| 459 | NULL, 0, NULL, 0, |
| 460 | GB_OPERATION_TIMEOUT_DEFAULT * 2); |
Greg Kroah-Hartman | 55ec09e | 2016-01-19 23:30:42 -0800 | [diff] [blame] | 461 | } |
| 462 | EXPORT_SYMBOL_GPL(gb_svc_ping); |
| 463 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 464 | static int gb_svc_version_request(struct gb_operation *op) |
| 465 | { |
| 466 | struct gb_connection *connection = op->connection; |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 467 | struct gb_svc *svc = gb_connection_get_data(connection); |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 468 | struct gb_protocol_version_request *request; |
| 469 | struct gb_protocol_version_response *response; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 470 | |
Johan Hovold | 5551084 | 2015-11-19 18:28:01 +0100 | [diff] [blame] | 471 | if (op->request->payload_size < sizeof(*request)) { |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 472 | dev_err(&svc->dev, "short version request (%zu < %zu)\n", |
Johan Hovold | 5551084 | 2015-11-19 18:28:01 +0100 | [diff] [blame] | 473 | op->request->payload_size, |
| 474 | sizeof(*request)); |
| 475 | return -EINVAL; |
| 476 | } |
| 477 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 478 | request = op->request->payload; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 479 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 480 | if (request->major > GB_SVC_VERSION_MAJOR) { |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 481 | dev_warn(&svc->dev, "unsupported major version (%u > %u)\n", |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 482 | request->major, GB_SVC_VERSION_MAJOR); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 483 | return -ENOTSUPP; |
| 484 | } |
| 485 | |
Johan Hovold | 357de00 | 2016-01-19 12:51:19 +0100 | [diff] [blame] | 486 | svc->protocol_major = request->major; |
| 487 | svc->protocol_minor = request->minor; |
Viresh Kumar | 3ea959e3 | 2015-08-11 07:36:14 +0530 | [diff] [blame] | 488 | |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 489 | if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL)) |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 490 | return -ENOMEM; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 491 | |
Johan Hovold | cfb1690 | 2015-09-15 10:48:01 +0200 | [diff] [blame] | 492 | response = op->response->payload; |
Johan Hovold | 357de00 | 2016-01-19 12:51:19 +0100 | [diff] [blame] | 493 | response->major = svc->protocol_major; |
| 494 | response->minor = svc->protocol_minor; |
Johan Hovold | 5983293 | 2015-09-15 10:48:00 +0200 | [diff] [blame] | 495 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 499 | static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf, |
| 500 | size_t len, loff_t *offset) |
| 501 | { |
| 502 | struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private; |
| 503 | struct gb_svc *svc = pwrmon_rails->svc; |
| 504 | int ret, desc; |
| 505 | u32 value; |
| 506 | char buff[16]; |
| 507 | |
| 508 | ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id, |
| 509 | GB_SVC_PWRMON_TYPE_VOL, &value); |
| 510 | if (ret) { |
| 511 | dev_err(&svc->dev, |
Johan Hovold | 89f2df4 | 2016-04-21 11:43:36 +0200 | [diff] [blame] | 512 | "failed to get voltage sample %u: %d\n", |
| 513 | pwrmon_rails->id, ret); |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | desc = scnprintf(buff, sizeof(buff), "%u\n", value); |
| 518 | |
| 519 | return simple_read_from_buffer(buf, len, offset, buff, desc); |
| 520 | } |
| 521 | |
| 522 | static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf, |
| 523 | size_t len, loff_t *offset) |
| 524 | { |
| 525 | struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private; |
| 526 | struct gb_svc *svc = pwrmon_rails->svc; |
| 527 | int ret, desc; |
| 528 | u32 value; |
| 529 | char buff[16]; |
| 530 | |
| 531 | ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id, |
| 532 | GB_SVC_PWRMON_TYPE_CURR, &value); |
| 533 | if (ret) { |
| 534 | dev_err(&svc->dev, |
Johan Hovold | 89f2df4 | 2016-04-21 11:43:36 +0200 | [diff] [blame] | 535 | "failed to get current sample %u: %d\n", |
| 536 | pwrmon_rails->id, ret); |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | desc = scnprintf(buff, sizeof(buff), "%u\n", value); |
| 541 | |
| 542 | return simple_read_from_buffer(buf, len, offset, buff, desc); |
| 543 | } |
| 544 | |
| 545 | static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf, |
| 546 | size_t len, loff_t *offset) |
| 547 | { |
| 548 | struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private; |
| 549 | struct gb_svc *svc = pwrmon_rails->svc; |
| 550 | int ret, desc; |
| 551 | u32 value; |
| 552 | char buff[16]; |
| 553 | |
| 554 | ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id, |
| 555 | GB_SVC_PWRMON_TYPE_PWR, &value); |
| 556 | if (ret) { |
Johan Hovold | 89f2df4 | 2016-04-21 11:43:36 +0200 | [diff] [blame] | 557 | dev_err(&svc->dev, "failed to get power sample %u: %d\n", |
| 558 | pwrmon_rails->id, ret); |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | desc = scnprintf(buff, sizeof(buff), "%u\n", value); |
| 563 | |
| 564 | return simple_read_from_buffer(buf, len, offset, buff, desc); |
| 565 | } |
| 566 | |
| 567 | static const struct file_operations pwrmon_debugfs_voltage_fops = { |
| 568 | .read = pwr_debugfs_voltage_read, |
| 569 | }; |
| 570 | |
| 571 | static const struct file_operations pwrmon_debugfs_current_fops = { |
| 572 | .read = pwr_debugfs_current_read, |
| 573 | }; |
| 574 | |
| 575 | static const struct file_operations pwrmon_debugfs_power_fops = { |
| 576 | .read = pwr_debugfs_power_read, |
| 577 | }; |
| 578 | |
Johan Hovold | 1218519 | 2016-04-23 18:47:20 +0200 | [diff] [blame] | 579 | static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc) |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 580 | { |
| 581 | int i; |
| 582 | size_t bufsize; |
| 583 | struct dentry *dent; |
| 584 | |
| 585 | dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry); |
| 586 | if (IS_ERR_OR_NULL(dent)) |
| 587 | return; |
| 588 | |
| 589 | if (gb_svc_pwrmon_rail_count_get(svc, &svc->rail_count)) |
| 590 | goto err_pwrmon_debugfs; |
| 591 | |
| 592 | if (!svc->rail_count || svc->rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT) |
| 593 | goto err_pwrmon_debugfs; |
| 594 | |
| 595 | bufsize = GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * svc->rail_count; |
| 596 | |
| 597 | svc->rail_names = kzalloc(bufsize, GFP_KERNEL); |
| 598 | if (!svc->rail_names) |
| 599 | goto err_pwrmon_debugfs; |
| 600 | |
| 601 | svc->pwrmon_rails = kcalloc(svc->rail_count, sizeof(*svc->pwrmon_rails), |
| 602 | GFP_KERNEL); |
| 603 | if (!svc->pwrmon_rails) |
| 604 | goto err_pwrmon_debugfs_free; |
| 605 | |
| 606 | if (gb_svc_pwrmon_rail_names_get(svc, svc->rail_names, bufsize)) |
| 607 | goto err_pwrmon_debugfs_free; |
| 608 | |
| 609 | for (i = 0; i < svc->rail_count; i++) { |
| 610 | struct dentry *dir; |
| 611 | struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i]; |
| 612 | char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE]; |
| 613 | |
| 614 | snprintf(fname, sizeof(fname), "%s", |
| 615 | (char *)&svc->rail_names->name[i]); |
| 616 | |
| 617 | rail->id = i; |
| 618 | rail->svc = svc; |
| 619 | |
| 620 | dir = debugfs_create_dir(fname, dent); |
| 621 | debugfs_create_file("voltage_now", S_IRUGO, dir, rail, |
| 622 | &pwrmon_debugfs_voltage_fops); |
| 623 | debugfs_create_file("current_now", S_IRUGO, dir, rail, |
| 624 | &pwrmon_debugfs_current_fops); |
| 625 | debugfs_create_file("power_now", S_IRUGO, dir, rail, |
| 626 | &pwrmon_debugfs_power_fops); |
| 627 | }; |
| 628 | return; |
| 629 | |
| 630 | err_pwrmon_debugfs_free: |
| 631 | kfree(svc->rail_names); |
| 632 | svc->rail_names = NULL; |
| 633 | |
| 634 | kfree(svc->pwrmon_rails); |
| 635 | svc->pwrmon_rails = NULL; |
| 636 | |
| 637 | err_pwrmon_debugfs: |
| 638 | debugfs_remove(dent); |
| 639 | } |
| 640 | |
Johan Hovold | 1218519 | 2016-04-23 18:47:20 +0200 | [diff] [blame] | 641 | static void gb_svc_debugfs_init(struct gb_svc *svc) |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 642 | { |
| 643 | svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev), |
| 644 | gb_debugfs_get()); |
Johan Hovold | 1218519 | 2016-04-23 18:47:20 +0200 | [diff] [blame] | 645 | gb_svc_pwrmon_debugfs_init(svc); |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Johan Hovold | 1218519 | 2016-04-23 18:47:20 +0200 | [diff] [blame] | 648 | static void gb_svc_debugfs_exit(struct gb_svc *svc) |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 649 | { |
| 650 | debugfs_remove_recursive(svc->debugfs_dentry); |
| 651 | kfree(svc->rail_names); |
| 652 | } |
| 653 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 654 | static int gb_svc_hello(struct gb_operation *op) |
| 655 | { |
| 656 | struct gb_connection *connection = op->connection; |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 657 | struct gb_svc *svc = gb_connection_get_data(connection); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 658 | struct gb_svc_hello_request *hello_request; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 659 | int ret; |
| 660 | |
Viresh Kumar | 0c32d2a | 2015-08-11 07:29:19 +0530 | [diff] [blame] | 661 | if (op->request->payload_size < sizeof(*hello_request)) { |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 662 | dev_warn(&svc->dev, "short hello request (%zu < %zu)\n", |
| 663 | op->request->payload_size, |
| 664 | sizeof(*hello_request)); |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 665 | return -EINVAL; |
| 666 | } |
| 667 | |
| 668 | hello_request = op->request->payload; |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame] | 669 | svc->endo_id = le16_to_cpu(hello_request->endo_id); |
| 670 | svc->ap_intf_id = hello_request->interface_id; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 671 | |
Johan Hovold | 88f7b96 | 2015-11-25 15:59:08 +0100 | [diff] [blame] | 672 | ret = device_add(&svc->dev); |
| 673 | if (ret) { |
| 674 | dev_err(&svc->dev, "failed to register svc device: %d\n", ret); |
| 675 | return ret; |
| 676 | } |
| 677 | |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 678 | ret = input_register_device(svc->input); |
| 679 | if (ret) { |
| 680 | dev_err(&svc->dev, "failed to register input: %d\n", ret); |
| 681 | device_del(&svc->dev); |
| 682 | return ret; |
| 683 | } |
| 684 | |
Greg Kroah-Hartman | ed7279a | 2016-01-20 22:51:49 -0800 | [diff] [blame] | 685 | ret = gb_svc_watchdog_create(svc); |
| 686 | if (ret) { |
| 687 | dev_err(&svc->dev, "failed to create watchdog: %d\n", ret); |
| 688 | input_unregister_device(svc->input); |
| 689 | device_del(&svc->dev); |
Greg Kroah-Hartman | 539d6e1 | 2016-01-23 17:36:00 -0800 | [diff] [blame] | 690 | return ret; |
Greg Kroah-Hartman | ed7279a | 2016-01-20 22:51:49 -0800 | [diff] [blame] | 691 | } |
| 692 | |
Johan Hovold | 1218519 | 2016-04-23 18:47:20 +0200 | [diff] [blame] | 693 | gb_svc_debugfs_init(svc); |
David Lin | 9504677 | 2016-04-20 16:55:08 -0700 | [diff] [blame] | 694 | |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 695 | return 0; |
| 696 | } |
| 697 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 698 | static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id) |
| 699 | { |
| 700 | struct gb_host_device *hd = svc->hd; |
| 701 | struct gb_module *module; |
| 702 | |
| 703 | list_for_each_entry(module, &hd->modules, hd_node) { |
| 704 | if (module->module_id == module_id) |
| 705 | return module; |
| 706 | } |
| 707 | |
| 708 | return NULL; |
| 709 | } |
| 710 | |
Johan Hovold | 844fcbf | 2016-04-23 18:47:23 +0200 | [diff] [blame] | 711 | static void gb_svc_intf_reenable(struct gb_svc *svc, struct gb_interface *intf) |
| 712 | { |
| 713 | int ret; |
| 714 | |
Johan Hovold | 36602a2 | 2016-04-23 18:47:25 +0200 | [diff] [blame] | 715 | mutex_lock(&intf->mutex); |
| 716 | |
Johan Hovold | 844fcbf | 2016-04-23 18:47:23 +0200 | [diff] [blame] | 717 | /* Mark as disconnected to prevent I/O during disable. */ |
| 718 | intf->disconnected = true; |
| 719 | gb_interface_disable(intf); |
| 720 | intf->disconnected = false; |
| 721 | |
| 722 | ret = gb_interface_enable(intf); |
| 723 | if (ret) { |
| 724 | dev_err(&svc->dev, "failed to enable interface %u: %d\n", |
| 725 | intf->interface_id, ret); |
| 726 | |
| 727 | gb_interface_deactivate(intf); |
| 728 | } |
Johan Hovold | 36602a2 | 2016-04-23 18:47:25 +0200 | [diff] [blame] | 729 | |
| 730 | mutex_unlock(&intf->mutex); |
Johan Hovold | 844fcbf | 2016-04-23 18:47:23 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 733 | static void gb_svc_process_intf_hotplug(struct gb_operation *operation) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 734 | { |
Johan Hovold | 24456a09 | 2015-12-02 18:23:27 +0100 | [diff] [blame] | 735 | struct gb_svc_intf_hotplug_request *request; |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 736 | struct gb_connection *connection = operation->connection; |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 737 | struct gb_svc *svc = gb_connection_get_data(connection); |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 738 | struct gb_host_device *hd = connection->hd; |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 739 | struct gb_module *module; |
Johan Hovold | bb2533a | 2016-03-09 12:20:34 +0100 | [diff] [blame] | 740 | u8 intf_id; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 741 | int ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 742 | |
Johan Hovold | 24456a09 | 2015-12-02 18:23:27 +0100 | [diff] [blame] | 743 | /* The request message size has already been verified. */ |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 744 | request = operation->request->payload; |
Johan Hovold | 24456a09 | 2015-12-02 18:23:27 +0100 | [diff] [blame] | 745 | intf_id = request->intf_id; |
| 746 | |
| 747 | dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 748 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 749 | /* All modules are considered 1x2 for now */ |
| 750 | module = gb_svc_module_lookup(svc, intf_id); |
| 751 | if (module) { |
Johan Hovold | 96fb6c3 | 2016-04-13 19:19:08 +0200 | [diff] [blame] | 752 | dev_info(&svc->dev, "mode switch detected on interface %u\n", |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 753 | intf_id); |
Johan Hovold | 96fb6c3 | 2016-04-13 19:19:08 +0200 | [diff] [blame] | 754 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 755 | return gb_svc_intf_reenable(svc, module->interfaces[0]); |
Viresh Kumar | bbaca71 | 2015-09-23 16:48:08 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 758 | module = gb_module_create(hd, intf_id, 1); |
| 759 | if (!module) { |
| 760 | dev_err(&svc->dev, "failed to create module\n"); |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 761 | return; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 762 | } |
| 763 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 764 | ret = gb_module_add(module); |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 765 | if (ret) { |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 766 | gb_module_put(module); |
Johan Hovold | 96fb6c3 | 2016-04-13 19:19:08 +0200 | [diff] [blame] | 767 | return; |
Johan Hovold | 153ff7e | 2016-03-29 18:56:08 -0400 | [diff] [blame] | 768 | } |
Viresh Kumar | 63d742b | 2015-12-23 09:07:42 +0530 | [diff] [blame] | 769 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 770 | list_add(&module->hd_node, &hd->modules); |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 771 | } |
| 772 | |
Johan Hovold | 57ccd4b | 2015-12-02 18:23:30 +0100 | [diff] [blame] | 773 | static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation) |
| 774 | { |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 775 | struct gb_svc *svc = gb_connection_get_data(operation->connection); |
Johan Hovold | 57ccd4b | 2015-12-02 18:23:30 +0100 | [diff] [blame] | 776 | struct gb_svc_intf_hot_unplug_request *request; |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 777 | struct gb_module *module; |
Johan Hovold | 57ccd4b | 2015-12-02 18:23:30 +0100 | [diff] [blame] | 778 | u8 intf_id; |
| 779 | |
| 780 | /* The request message size has already been verified. */ |
| 781 | request = operation->request->payload; |
| 782 | intf_id = request->intf_id; |
| 783 | |
| 784 | dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id); |
| 785 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 786 | /* All modules are considered 1x2 for now */ |
| 787 | module = gb_svc_module_lookup(svc, intf_id); |
| 788 | if (!module) { |
Viresh Kumar | 2f3db92 | 2015-12-04 21:30:09 +0530 | [diff] [blame] | 789 | dev_warn(&svc->dev, "could not find hot-unplug interface %u\n", |
Johan Hovold | 57ccd4b | 2015-12-02 18:23:30 +0100 | [diff] [blame] | 790 | intf_id); |
| 791 | return; |
| 792 | } |
| 793 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 794 | module->disconnected = true; |
Johan Hovold | 41d5140 | 2016-04-13 19:19:09 +0200 | [diff] [blame] | 795 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 796 | gb_module_del(module); |
| 797 | list_del(&module->hd_node); |
| 798 | gb_module_put(module); |
Johan Hovold | 57ccd4b | 2015-12-02 18:23:30 +0100 | [diff] [blame] | 799 | } |
| 800 | |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 801 | static void gb_svc_process_deferred_request(struct work_struct *work) |
| 802 | { |
| 803 | struct gb_svc_deferred_request *dr; |
| 804 | struct gb_operation *operation; |
| 805 | struct gb_svc *svc; |
| 806 | u8 type; |
| 807 | |
| 808 | dr = container_of(work, struct gb_svc_deferred_request, work); |
| 809 | operation = dr->operation; |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 810 | svc = gb_connection_get_data(operation->connection); |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 811 | type = operation->request->header->type; |
| 812 | |
| 813 | switch (type) { |
| 814 | case GB_SVC_TYPE_INTF_HOTPLUG: |
| 815 | gb_svc_process_intf_hotplug(operation); |
| 816 | break; |
Johan Hovold | 57ccd4b | 2015-12-02 18:23:30 +0100 | [diff] [blame] | 817 | case GB_SVC_TYPE_INTF_HOT_UNPLUG: |
| 818 | gb_svc_process_intf_hot_unplug(operation); |
| 819 | break; |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 820 | default: |
Viresh Kumar | b933fa4 | 2015-12-04 21:30:10 +0530 | [diff] [blame] | 821 | dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type); |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | gb_operation_put(operation); |
| 825 | kfree(dr); |
| 826 | } |
| 827 | |
| 828 | static int gb_svc_queue_deferred_request(struct gb_operation *operation) |
| 829 | { |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 830 | struct gb_svc *svc = gb_connection_get_data(operation->connection); |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 831 | struct gb_svc_deferred_request *dr; |
| 832 | |
| 833 | dr = kmalloc(sizeof(*dr), GFP_KERNEL); |
| 834 | if (!dr) |
| 835 | return -ENOMEM; |
| 836 | |
| 837 | gb_operation_get(operation); |
| 838 | |
| 839 | dr->operation = operation; |
| 840 | INIT_WORK(&dr->work, gb_svc_process_deferred_request); |
| 841 | |
Johan Hovold | 3e48aca | 2015-12-02 18:23:31 +0100 | [diff] [blame] | 842 | queue_work(svc->wq, &dr->work); |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 843 | |
| 844 | return 0; |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 845 | } |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 846 | |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 847 | /* |
| 848 | * Bringing up a module can be time consuming, as that may require lots of |
| 849 | * initialization on the module side. Over that, we may also need to download |
| 850 | * the firmware first and flash that on the module. |
| 851 | * |
Johan Hovold | 3e48aca | 2015-12-02 18:23:31 +0100 | [diff] [blame] | 852 | * 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] | 853 | * handle most of module hotplug stuff outside of the hotplug callback, with |
| 854 | * help of a workqueue. |
| 855 | */ |
| 856 | static int gb_svc_intf_hotplug_recv(struct gb_operation *op) |
| 857 | { |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 858 | struct gb_svc *svc = gb_connection_get_data(op->connection); |
Johan Hovold | d34a364 | 2015-12-02 18:23:26 +0100 | [diff] [blame] | 859 | struct gb_svc_intf_hotplug_request *request; |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 860 | |
Johan Hovold | d34a364 | 2015-12-02 18:23:26 +0100 | [diff] [blame] | 861 | if (op->request->payload_size < sizeof(*request)) { |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 862 | dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n", |
Johan Hovold | d34a364 | 2015-12-02 18:23:26 +0100 | [diff] [blame] | 863 | op->request->payload_size, sizeof(*request)); |
Viresh Kumar | 067906f | 2015-08-06 12:44:55 +0530 | [diff] [blame] | 864 | return -EINVAL; |
| 865 | } |
| 866 | |
Johan Hovold | d34a364 | 2015-12-02 18:23:26 +0100 | [diff] [blame] | 867 | request = op->request->payload; |
| 868 | |
| 869 | dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id); |
| 870 | |
Johan Hovold | 9ae4109 | 2015-12-02 18:23:29 +0100 | [diff] [blame] | 871 | return gb_svc_queue_deferred_request(op); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op) |
| 875 | { |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 876 | struct gb_svc *svc = gb_connection_get_data(op->connection); |
Johan Hovold | d34a364 | 2015-12-02 18:23:26 +0100 | [diff] [blame] | 877 | struct gb_svc_intf_hot_unplug_request *request; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 878 | |
Johan Hovold | d34a364 | 2015-12-02 18:23:26 +0100 | [diff] [blame] | 879 | if (op->request->payload_size < sizeof(*request)) { |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 880 | dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n", |
Johan Hovold | d34a364 | 2015-12-02 18:23:26 +0100 | [diff] [blame] | 881 | op->request->payload_size, sizeof(*request)); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 882 | return -EINVAL; |
| 883 | } |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 884 | |
Johan Hovold | d34a364 | 2015-12-02 18:23:26 +0100 | [diff] [blame] | 885 | request = op->request->payload; |
Johan Hovold | d34a364 | 2015-12-02 18:23:26 +0100 | [diff] [blame] | 886 | |
Johan Hovold | 57ccd4b | 2015-12-02 18:23:30 +0100 | [diff] [blame] | 887 | dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 888 | |
Johan Hovold | 57ccd4b | 2015-12-02 18:23:30 +0100 | [diff] [blame] | 889 | return gb_svc_queue_deferred_request(op); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static int gb_svc_intf_reset_recv(struct gb_operation *op) |
| 893 | { |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 894 | struct gb_svc *svc = gb_connection_get_data(op->connection); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 895 | struct gb_message *request = op->request; |
| 896 | struct gb_svc_intf_reset_request *reset; |
| 897 | u8 intf_id; |
| 898 | |
| 899 | if (request->payload_size < sizeof(*reset)) { |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 900 | dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n", |
| 901 | request->payload_size, sizeof(*reset)); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 902 | return -EINVAL; |
| 903 | } |
| 904 | reset = request->payload; |
| 905 | |
| 906 | intf_id = reset->intf_id; |
| 907 | |
| 908 | /* FIXME Reset the interface here */ |
| 909 | |
| 910 | return 0; |
| 911 | } |
| 912 | |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 913 | static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code) |
| 914 | { |
| 915 | switch (key_code) { |
| 916 | case GB_KEYCODE_ARA: |
| 917 | *code = SVC_KEY_ARA_BUTTON; |
| 918 | break; |
| 919 | default: |
| 920 | dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code); |
| 921 | return -EINVAL; |
| 922 | } |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | static int gb_svc_key_event_recv(struct gb_operation *op) |
| 928 | { |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 929 | struct gb_svc *svc = gb_connection_get_data(op->connection); |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 930 | struct gb_message *request = op->request; |
| 931 | struct gb_svc_key_event_request *key; |
| 932 | u16 code; |
| 933 | u8 event; |
| 934 | int ret; |
| 935 | |
| 936 | if (request->payload_size < sizeof(*key)) { |
| 937 | dev_warn(&svc->dev, "short key request received (%zu < %zu)\n", |
| 938 | request->payload_size, sizeof(*key)); |
| 939 | return -EINVAL; |
| 940 | } |
| 941 | |
| 942 | key = request->payload; |
| 943 | |
| 944 | ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code); |
| 945 | if (ret < 0) |
| 946 | return ret; |
| 947 | |
| 948 | event = key->key_event; |
| 949 | if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) { |
| 950 | dev_warn(&svc->dev, "unknown key event received: %u\n", event); |
| 951 | return -EINVAL; |
| 952 | } |
| 953 | |
| 954 | input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED)); |
| 955 | input_sync(svc->input); |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
Johan Hovold | 8442794 | 2016-01-19 12:51:15 +0100 | [diff] [blame] | 960 | static int gb_svc_request_handler(struct gb_operation *op) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 961 | { |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 962 | struct gb_connection *connection = op->connection; |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 963 | struct gb_svc *svc = gb_connection_get_data(connection); |
Johan Hovold | 8442794 | 2016-01-19 12:51:15 +0100 | [diff] [blame] | 964 | u8 type = op->type; |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 965 | int ret = 0; |
| 966 | |
| 967 | /* |
| 968 | * SVC requests need to follow a specific order (at least initially) and |
| 969 | * below code takes care of enforcing that. The expected order is: |
| 970 | * - PROTOCOL_VERSION |
| 971 | * - SVC_HELLO |
| 972 | * - Any other request, but the earlier two. |
| 973 | * |
| 974 | * Incoming requests are guaranteed to be serialized and so we don't |
| 975 | * need to protect 'state' for any races. |
| 976 | */ |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 977 | switch (type) { |
Viresh Kumar | 0e2462d | 2015-08-14 07:57:38 +0530 | [diff] [blame] | 978 | case GB_REQUEST_TYPE_PROTOCOL_VERSION: |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 979 | if (svc->state != GB_SVC_STATE_RESET) |
| 980 | ret = -EINVAL; |
| 981 | break; |
Viresh Kumar | ead3546 | 2015-07-21 17:44:19 +0530 | [diff] [blame] | 982 | case GB_SVC_TYPE_SVC_HELLO: |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 983 | if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION) |
| 984 | ret = -EINVAL; |
| 985 | break; |
| 986 | default: |
| 987 | if (svc->state != GB_SVC_STATE_SVC_HELLO) |
| 988 | ret = -EINVAL; |
| 989 | break; |
| 990 | } |
| 991 | |
| 992 | if (ret) { |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 993 | dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n", |
| 994 | type, svc->state); |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 995 | return ret; |
| 996 | } |
| 997 | |
| 998 | switch (type) { |
| 999 | case GB_REQUEST_TYPE_PROTOCOL_VERSION: |
| 1000 | ret = gb_svc_version_request(op); |
| 1001 | if (!ret) |
| 1002 | svc->state = GB_SVC_STATE_PROTOCOL_VERSION; |
| 1003 | return ret; |
| 1004 | case GB_SVC_TYPE_SVC_HELLO: |
| 1005 | ret = gb_svc_hello(op); |
| 1006 | if (!ret) |
| 1007 | svc->state = GB_SVC_STATE_SVC_HELLO; |
| 1008 | return ret; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 1009 | case GB_SVC_TYPE_INTF_HOTPLUG: |
| 1010 | return gb_svc_intf_hotplug_recv(op); |
| 1011 | case GB_SVC_TYPE_INTF_HOT_UNPLUG: |
| 1012 | return gb_svc_intf_hot_unplug_recv(op); |
| 1013 | case GB_SVC_TYPE_INTF_RESET: |
| 1014 | return gb_svc_intf_reset_recv(op); |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 1015 | case GB_SVC_TYPE_KEY_EVENT: |
| 1016 | return gb_svc_key_event_recv(op); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 1017 | default: |
Johan Hovold | 684156a | 2015-11-25 15:59:19 +0100 | [diff] [blame] | 1018 | dev_warn(&svc->dev, "unsupported request 0x%02x\n", type); |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 1019 | return -EINVAL; |
| 1020 | } |
| 1021 | } |
| 1022 | |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 1023 | static struct input_dev *gb_svc_input_create(struct gb_svc *svc) |
| 1024 | { |
| 1025 | struct input_dev *input_dev; |
| 1026 | |
| 1027 | input_dev = input_allocate_device(); |
| 1028 | if (!input_dev) |
| 1029 | return ERR_PTR(-ENOMEM); |
| 1030 | |
| 1031 | input_dev->name = dev_name(&svc->dev); |
| 1032 | svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0", |
| 1033 | input_dev->name); |
| 1034 | if (!svc->input_phys) |
| 1035 | goto err_free_input; |
| 1036 | |
| 1037 | input_dev->phys = svc->input_phys; |
| 1038 | input_dev->dev.parent = &svc->dev; |
| 1039 | |
| 1040 | input_set_drvdata(input_dev, svc); |
| 1041 | |
| 1042 | input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON); |
| 1043 | |
| 1044 | return input_dev; |
| 1045 | |
| 1046 | err_free_input: |
| 1047 | input_free_device(svc->input); |
| 1048 | return ERR_PTR(-ENOMEM); |
| 1049 | } |
| 1050 | |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 1051 | static void gb_svc_release(struct device *dev) |
| 1052 | { |
Johan Hovold | 88f7b96 | 2015-11-25 15:59:08 +0100 | [diff] [blame] | 1053 | struct gb_svc *svc = to_gb_svc(dev); |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 1054 | |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1055 | if (svc->connection) |
| 1056 | gb_connection_destroy(svc->connection); |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 1057 | ida_destroy(&svc->device_id_map); |
Johan Hovold | 3e48aca | 2015-12-02 18:23:31 +0100 | [diff] [blame] | 1058 | destroy_workqueue(svc->wq); |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 1059 | kfree(svc->input_phys); |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 1060 | kfree(svc); |
| 1061 | } |
| 1062 | |
| 1063 | struct device_type greybus_svc_type = { |
| 1064 | .name = "greybus_svc", |
| 1065 | .release = gb_svc_release, |
| 1066 | }; |
| 1067 | |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1068 | struct gb_svc *gb_svc_create(struct gb_host_device *hd) |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 1069 | { |
| 1070 | struct gb_svc *svc; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 1071 | |
| 1072 | svc = kzalloc(sizeof(*svc), GFP_KERNEL); |
| 1073 | if (!svc) |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1074 | return NULL; |
Alex Elder | 30c6d9d | 2015-05-22 13:02:08 -0500 | [diff] [blame] | 1075 | |
Johan Hovold | 3e48aca | 2015-12-02 18:23:31 +0100 | [diff] [blame] | 1076 | svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev)); |
| 1077 | if (!svc->wq) { |
| 1078 | kfree(svc); |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1079 | return NULL; |
Johan Hovold | 3e48aca | 2015-12-02 18:23:31 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 1082 | svc->dev.parent = &hd->dev; |
| 1083 | svc->dev.bus = &greybus_bus_type; |
| 1084 | svc->dev.type = &greybus_svc_type; |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame] | 1085 | svc->dev.groups = svc_groups; |
Johan Hovold | efe6ef7 | 2015-11-25 15:59:06 +0100 | [diff] [blame] | 1086 | svc->dev.dma_mask = svc->dev.parent->dma_mask; |
| 1087 | device_initialize(&svc->dev); |
| 1088 | |
| 1089 | dev_set_name(&svc->dev, "%d-svc", hd->bus_id); |
| 1090 | |
Johan Hovold | 6106e51 | 2015-11-25 15:59:07 +0100 | [diff] [blame] | 1091 | ida_init(&svc->device_id_map); |
Viresh Kumar | 3ccb160 | 2015-09-03 15:42:22 +0530 | [diff] [blame] | 1092 | svc->state = GB_SVC_STATE_RESET; |
Johan Hovold | f0960d0 | 2015-12-03 19:18:02 +0100 | [diff] [blame] | 1093 | svc->hd = hd; |
Viresh Kumar | d3d4484 | 2015-07-21 17:44:18 +0530 | [diff] [blame] | 1094 | |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 1095 | svc->input = gb_svc_input_create(svc); |
| 1096 | if (IS_ERR(svc->input)) { |
| 1097 | dev_err(&svc->dev, "failed to create input device: %ld\n", |
| 1098 | PTR_ERR(svc->input)); |
| 1099 | goto err_put_device; |
| 1100 | } |
| 1101 | |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 1102 | svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID, |
| 1103 | gb_svc_request_handler); |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 1104 | if (IS_ERR(svc->connection)) { |
| 1105 | dev_err(&svc->dev, "failed to create connection: %ld\n", |
| 1106 | PTR_ERR(svc->connection)); |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 1107 | goto err_free_input; |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1108 | } |
| 1109 | |
Greg Kroah-Hartman | 0ec3063 | 2016-03-22 14:30:35 -0400 | [diff] [blame] | 1110 | gb_connection_set_data(svc->connection, svc); |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1111 | |
| 1112 | return svc; |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 1113 | |
| 1114 | err_free_input: |
| 1115 | input_free_device(svc->input); |
| 1116 | err_put_device: |
| 1117 | put_device(&svc->dev); |
| 1118 | return NULL; |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | int gb_svc_add(struct gb_svc *svc) |
| 1122 | { |
| 1123 | int ret; |
| 1124 | |
| 1125 | /* |
| 1126 | * The SVC protocol is currently driven by the SVC, so the SVC device |
| 1127 | * is added from the connection request handler when enough |
| 1128 | * information has been received. |
| 1129 | */ |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 1130 | ret = gb_connection_enable(svc->connection); |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1131 | if (ret) |
| 1132 | return ret; |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 1137 | static void gb_svc_remove_modules(struct gb_svc *svc) |
Johan Hovold | 66d674c | 2016-03-09 12:20:41 +0100 | [diff] [blame] | 1138 | { |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 1139 | struct gb_host_device *hd = svc->hd; |
| 1140 | struct gb_module *module, *tmp; |
Johan Hovold | 66d674c | 2016-03-09 12:20:41 +0100 | [diff] [blame] | 1141 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 1142 | list_for_each_entry_safe(module, tmp, &hd->modules, hd_node) { |
| 1143 | gb_module_del(module); |
| 1144 | list_del(&module->hd_node); |
| 1145 | gb_module_put(module); |
Johan Hovold | 629c0d0 | 2016-03-09 12:20:43 +0100 | [diff] [blame] | 1146 | } |
Johan Hovold | 66d674c | 2016-03-09 12:20:41 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1149 | void gb_svc_del(struct gb_svc *svc) |
| 1150 | { |
Johan Hovold | 8442794 | 2016-01-19 12:51:15 +0100 | [diff] [blame] | 1151 | gb_connection_disable(svc->connection); |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1152 | |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 1153 | /* |
| 1154 | * The SVC device and input device may have been registered |
| 1155 | * from the request handler. |
| 1156 | */ |
| 1157 | if (device_is_registered(&svc->dev)) { |
Johan Hovold | 1218519 | 2016-04-23 18:47:20 +0200 | [diff] [blame] | 1158 | gb_svc_debugfs_exit(svc); |
Greg Kroah-Hartman | ed7279a | 2016-01-20 22:51:49 -0800 | [diff] [blame] | 1159 | gb_svc_watchdog_destroy(svc); |
Rui Miguel Silva | ebe99d6 | 2016-01-21 01:42:17 +0000 | [diff] [blame] | 1160 | input_unregister_device(svc->input); |
| 1161 | device_del(&svc->dev); |
| 1162 | } |
| 1163 | |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1164 | flush_workqueue(svc->wq); |
Johan Hovold | 66d674c | 2016-03-09 12:20:41 +0100 | [diff] [blame] | 1165 | |
Johan Hovold | b15d97d | 2016-04-23 18:47:24 +0200 | [diff] [blame] | 1166 | gb_svc_remove_modules(svc); |
Johan Hovold | 7adeaae7 | 2015-12-07 15:05:37 +0100 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | void gb_svc_put(struct gb_svc *svc) |
| 1170 | { |
| 1171 | put_device(&svc->dev); |
| 1172 | } |