4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/debugfs.h>
11 #include <linux/input.h>
12 #include <linux/workqueue.h>
16 #define SVC_KEY_ARA_BUTTON KEY_A
18 #define SVC_INTF_EJECT_TIMEOUT 9000
20 struct gb_svc_deferred_request {
21 struct work_struct work;
22 struct gb_operation *operation;
26 static ssize_t endo_id_show(struct device *dev,
27 struct device_attribute *attr, char *buf)
29 struct gb_svc *svc = to_gb_svc(dev);
31 return sprintf(buf, "0x%04x\n", svc->endo_id);
33 static DEVICE_ATTR_RO(endo_id);
35 static ssize_t ap_intf_id_show(struct device *dev,
36 struct device_attribute *attr, char *buf)
38 struct gb_svc *svc = to_gb_svc(dev);
40 return sprintf(buf, "%u\n", svc->ap_intf_id);
42 static DEVICE_ATTR_RO(ap_intf_id);
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,
54 struct gb_svc *svc = to_gb_svc(dev);
55 unsigned short intf_id;
58 ret = kstrtou16(buf, 10, &intf_id);
62 dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
64 ret = gb_svc_intf_eject(svc, intf_id);
70 static DEVICE_ATTR_WO(intf_eject);
72 static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
75 struct gb_svc *svc = to_gb_svc(dev);
77 return sprintf(buf, "%s\n",
78 gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
81 static ssize_t watchdog_store(struct device *dev,
82 struct device_attribute *attr, const char *buf,
85 struct gb_svc *svc = to_gb_svc(dev);
89 retval = strtobool(buf, &user_request);
94 retval = gb_svc_watchdog_enable(svc);
96 retval = gb_svc_watchdog_disable(svc);
101 static DEVICE_ATTR_RW(watchdog);
103 static int gb_svc_pwrmon_rail_count_get(struct gb_svc *svc, u8 *value)
105 struct gb_svc_pwrmon_rail_count_get_response response;
108 ret = gb_operation_sync(svc->connection,
109 GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET, NULL, 0,
110 &response, sizeof(response));
112 dev_err(&svc->dev, "failed to get rail count: %d\n", ret);
116 *value = response.rail_count;
121 static int gb_svc_pwrmon_rail_names_get(struct gb_svc *svc,
122 struct gb_svc_pwrmon_rail_names_get_response *response,
127 ret = gb_operation_sync(svc->connection,
128 GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET, NULL, 0,
131 dev_err(&svc->dev, "failed to get rail names: %d\n", ret);
138 static int gb_svc_pwrmon_sample_get(struct gb_svc *svc, u8 rail_id,
139 u8 measurement_type, u32 *value)
141 struct gb_svc_pwrmon_sample_get_request request;
142 struct gb_svc_pwrmon_sample_get_response response;
145 request.rail_id = rail_id;
146 request.measurement_type = measurement_type;
148 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_PWRMON_SAMPLE_GET,
149 &request, sizeof(request),
150 &response, sizeof(response));
152 dev_err(&svc->dev, "failed to get rail sample: %d\n", ret);
156 if (response.result) {
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:
163 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
170 *value = le32_to_cpu(response.measurement);
175 int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id,
176 u8 measurement_type, u32 *value)
178 struct gb_svc_pwrmon_intf_sample_get_request request;
179 struct gb_svc_pwrmon_intf_sample_get_response response;
182 request.intf_id = intf_id;
183 request.measurement_type = measurement_type;
185 ret = gb_operation_sync(svc->connection,
186 GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET,
187 &request, sizeof(request),
188 &response, sizeof(response));
190 dev_err(&svc->dev, "failed to get intf sample: %d\n", ret);
194 if (response.result) {
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:
201 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
208 *value = le32_to_cpu(response.measurement);
213 static struct attribute *svc_attrs[] = {
214 &dev_attr_endo_id.attr,
215 &dev_attr_ap_intf_id.attr,
216 &dev_attr_intf_eject.attr,
217 &dev_attr_watchdog.attr,
220 ATTRIBUTE_GROUPS(svc);
222 int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
224 struct gb_svc_intf_device_id_request request;
226 request.intf_id = intf_id;
227 request.device_id = device_id;
229 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
230 &request, sizeof(request), NULL, 0);
233 int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
235 struct gb_svc_intf_eject_request request;
238 request.intf_id = intf_id;
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.
244 ret = gb_operation_sync_timeout(svc->connection,
245 GB_SVC_TYPE_INTF_EJECT, &request,
246 sizeof(request), NULL, 0,
247 SVC_INTF_EJECT_TIMEOUT);
249 dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
256 int gb_svc_intf_vsys_set(struct gb_svc *svc, u8 intf_id, bool enable)
258 /* FIXME: implement */
263 int gb_svc_intf_refclk_set(struct gb_svc *svc, u8 intf_id, bool enable)
265 /* FIXME: implement */
270 int gb_svc_intf_unipro_set(struct gb_svc *svc, u8 intf_id, bool enable)
272 /* FIXME: implement */
277 int gb_svc_intf_activate(struct gb_svc *svc, u8 intf_id, u8 *intf_type)
279 /* FIXME: implement */
281 *intf_type = GB_SVC_INTF_TYPE_GREYBUS;
286 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
289 struct gb_svc_dme_peer_get_request request;
290 struct gb_svc_dme_peer_get_response response;
294 request.intf_id = intf_id;
295 request.attr = cpu_to_le16(attr);
296 request.selector = cpu_to_le16(selector);
298 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
299 &request, sizeof(request),
300 &response, sizeof(response));
302 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
303 intf_id, attr, selector, ret);
307 result = le16_to_cpu(response.result_code);
309 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
310 intf_id, attr, selector, result);
315 *value = le32_to_cpu(response.attr_value);
319 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
321 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
324 struct gb_svc_dme_peer_set_request request;
325 struct gb_svc_dme_peer_set_response response;
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);
334 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
335 &request, sizeof(request),
336 &response, sizeof(response));
338 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
339 intf_id, attr, selector, value, ret);
343 result = le16_to_cpu(response.result_code);
345 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
346 intf_id, attr, selector, value, result);
352 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
354 int gb_svc_connection_create(struct gb_svc *svc,
355 u8 intf1_id, u16 cport1_id,
356 u8 intf2_id, u16 cport2_id,
359 struct gb_svc_conn_create_request request;
361 request.intf1_id = intf1_id;
362 request.cport1_id = cpu_to_le16(cport1_id);
363 request.intf2_id = intf2_id;
364 request.cport2_id = cpu_to_le16(cport2_id);
365 request.tc = 0; /* TC0 */
366 request.flags = cport_flags;
368 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
369 &request, sizeof(request), NULL, 0);
371 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
373 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
374 u8 intf2_id, u16 cport2_id)
376 struct gb_svc_conn_destroy_request request;
377 struct gb_connection *connection = svc->connection;
380 request.intf1_id = intf1_id;
381 request.cport1_id = cpu_to_le16(cport1_id);
382 request.intf2_id = intf2_id;
383 request.cport2_id = cpu_to_le16(cport2_id);
385 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
386 &request, sizeof(request), NULL, 0);
388 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
389 intf1_id, cport1_id, intf2_id, cport2_id, ret);
392 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
394 /* Creates bi-directional routes between the devices */
395 int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
396 u8 intf2_id, u8 dev2_id)
398 struct gb_svc_route_create_request request;
400 request.intf1_id = intf1_id;
401 request.dev1_id = dev1_id;
402 request.intf2_id = intf2_id;
403 request.dev2_id = dev2_id;
405 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
406 &request, sizeof(request), NULL, 0);
409 /* Destroys bi-directional routes between the devices */
410 void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
412 struct gb_svc_route_destroy_request request;
415 request.intf1_id = intf1_id;
416 request.intf2_id = intf2_id;
418 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
419 &request, sizeof(request), NULL, 0);
421 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
422 intf1_id, intf2_id, ret);
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)
431 struct gb_svc_intf_set_pwrm_request request;
432 struct gb_svc_intf_set_pwrm_response response;
435 request.intf_id = intf_id;
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;
443 request.flags = flags;
444 request.quirks = cpu_to_le32(quirks);
446 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
447 &request, sizeof(request),
448 &response, sizeof(response));
452 return le16_to_cpu(response.result_code);
454 EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
456 int gb_svc_ping(struct gb_svc *svc)
458 return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
460 GB_OPERATION_TIMEOUT_DEFAULT * 2);
462 EXPORT_SYMBOL_GPL(gb_svc_ping);
464 static int gb_svc_version_request(struct gb_operation *op)
466 struct gb_connection *connection = op->connection;
467 struct gb_svc *svc = gb_connection_get_data(connection);
468 struct gb_protocol_version_request *request;
469 struct gb_protocol_version_response *response;
471 if (op->request->payload_size < sizeof(*request)) {
472 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
473 op->request->payload_size,
478 request = op->request->payload;
480 if (request->major > GB_SVC_VERSION_MAJOR) {
481 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
482 request->major, GB_SVC_VERSION_MAJOR);
486 svc->protocol_major = request->major;
487 svc->protocol_minor = request->minor;
489 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
492 response = op->response->payload;
493 response->major = svc->protocol_major;
494 response->minor = svc->protocol_minor;
499 static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf,
500 size_t len, loff_t *offset)
502 struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
503 struct gb_svc *svc = pwrmon_rails->svc;
508 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
509 GB_SVC_PWRMON_TYPE_VOL, &value);
512 "failed to get voltage sample %u: %d\n",
513 pwrmon_rails->id, ret);
517 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
519 return simple_read_from_buffer(buf, len, offset, buff, desc);
522 static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf,
523 size_t len, loff_t *offset)
525 struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
526 struct gb_svc *svc = pwrmon_rails->svc;
531 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
532 GB_SVC_PWRMON_TYPE_CURR, &value);
535 "failed to get current sample %u: %d\n",
536 pwrmon_rails->id, ret);
540 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
542 return simple_read_from_buffer(buf, len, offset, buff, desc);
545 static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf,
546 size_t len, loff_t *offset)
548 struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
549 struct gb_svc *svc = pwrmon_rails->svc;
554 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
555 GB_SVC_PWRMON_TYPE_PWR, &value);
557 dev_err(&svc->dev, "failed to get power sample %u: %d\n",
558 pwrmon_rails->id, ret);
562 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
564 return simple_read_from_buffer(buf, len, offset, buff, desc);
567 static const struct file_operations pwrmon_debugfs_voltage_fops = {
568 .read = pwr_debugfs_voltage_read,
571 static const struct file_operations pwrmon_debugfs_current_fops = {
572 .read = pwr_debugfs_current_read,
575 static const struct file_operations pwrmon_debugfs_power_fops = {
576 .read = pwr_debugfs_power_read,
579 static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc)
585 dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry);
586 if (IS_ERR_OR_NULL(dent))
589 if (gb_svc_pwrmon_rail_count_get(svc, &svc->rail_count))
590 goto err_pwrmon_debugfs;
592 if (!svc->rail_count || svc->rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT)
593 goto err_pwrmon_debugfs;
595 bufsize = GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * svc->rail_count;
597 svc->rail_names = kzalloc(bufsize, GFP_KERNEL);
598 if (!svc->rail_names)
599 goto err_pwrmon_debugfs;
601 svc->pwrmon_rails = kcalloc(svc->rail_count, sizeof(*svc->pwrmon_rails),
603 if (!svc->pwrmon_rails)
604 goto err_pwrmon_debugfs_free;
606 if (gb_svc_pwrmon_rail_names_get(svc, svc->rail_names, bufsize))
607 goto err_pwrmon_debugfs_free;
609 for (i = 0; i < svc->rail_count; i++) {
611 struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i];
612 char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE];
614 snprintf(fname, sizeof(fname), "%s",
615 (char *)&svc->rail_names->name[i]);
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);
630 err_pwrmon_debugfs_free:
631 kfree(svc->rail_names);
632 svc->rail_names = NULL;
634 kfree(svc->pwrmon_rails);
635 svc->pwrmon_rails = NULL;
638 debugfs_remove(dent);
641 static void gb_svc_debugfs_init(struct gb_svc *svc)
643 svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
645 gb_svc_pwrmon_debugfs_init(svc);
648 static void gb_svc_debugfs_exit(struct gb_svc *svc)
650 debugfs_remove_recursive(svc->debugfs_dentry);
651 kfree(svc->rail_names);
654 static int gb_svc_hello(struct gb_operation *op)
656 struct gb_connection *connection = op->connection;
657 struct gb_svc *svc = gb_connection_get_data(connection);
658 struct gb_svc_hello_request *hello_request;
661 if (op->request->payload_size < sizeof(*hello_request)) {
662 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
663 op->request->payload_size,
664 sizeof(*hello_request));
668 hello_request = op->request->payload;
669 svc->endo_id = le16_to_cpu(hello_request->endo_id);
670 svc->ap_intf_id = hello_request->interface_id;
672 ret = device_add(&svc->dev);
674 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
678 ret = input_register_device(svc->input);
680 dev_err(&svc->dev, "failed to register input: %d\n", ret);
681 device_del(&svc->dev);
685 ret = gb_svc_watchdog_create(svc);
687 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
688 input_unregister_device(svc->input);
689 device_del(&svc->dev);
693 gb_svc_debugfs_init(svc);
698 static struct gb_interface *gb_svc_interface_lookup(struct gb_svc *svc,
701 struct gb_host_device *hd = svc->hd;
702 struct gb_module *module;
703 size_t num_interfaces;
706 list_for_each_entry(module, &hd->modules, hd_node) {
707 module_id = module->module_id;
708 num_interfaces = module->num_interfaces;
710 if (intf_id >= module_id &&
711 intf_id < module_id + num_interfaces) {
712 return module->interfaces[intf_id - module_id];
719 static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
721 struct gb_host_device *hd = svc->hd;
722 struct gb_module *module;
724 list_for_each_entry(module, &hd->modules, hd_node) {
725 if (module->module_id == module_id)
732 static void gb_svc_intf_reenable(struct gb_svc *svc, struct gb_interface *intf)
736 mutex_lock(&intf->mutex);
738 /* Mark as disconnected to prevent I/O during disable. */
739 intf->disconnected = true;
740 gb_interface_disable(intf);
741 intf->disconnected = false;
743 ret = gb_interface_enable(intf);
745 dev_err(&svc->dev, "failed to enable interface %u: %d\n",
746 intf->interface_id, ret);
748 gb_interface_deactivate(intf);
751 mutex_unlock(&intf->mutex);
754 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
756 struct gb_svc_intf_hotplug_request *request;
757 struct gb_connection *connection = operation->connection;
758 struct gb_svc *svc = gb_connection_get_data(connection);
759 struct gb_host_device *hd = connection->hd;
760 struct gb_module *module;
764 /* The request message size has already been verified. */
765 request = operation->request->payload;
766 intf_id = request->intf_id;
768 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
770 /* All modules are considered 1x2 for now */
771 module = gb_svc_module_lookup(svc, intf_id);
773 dev_info(&svc->dev, "mode switch detected on interface %u\n",
776 return gb_svc_intf_reenable(svc, module->interfaces[0]);
779 module = gb_module_create(hd, intf_id, 1);
781 dev_err(&svc->dev, "failed to create module\n");
785 ret = gb_module_add(module);
787 gb_module_put(module);
791 list_add(&module->hd_node, &hd->modules);
794 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
796 struct gb_svc *svc = gb_connection_get_data(operation->connection);
797 struct gb_svc_intf_hot_unplug_request *request;
798 struct gb_module *module;
801 /* The request message size has already been verified. */
802 request = operation->request->payload;
803 intf_id = request->intf_id;
805 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
807 /* All modules are considered 1x2 for now */
808 module = gb_svc_module_lookup(svc, intf_id);
810 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
815 module->disconnected = true;
817 gb_module_del(module);
818 list_del(&module->hd_node);
819 gb_module_put(module);
822 static void gb_svc_process_module_inserted(struct gb_operation *operation)
824 struct gb_svc_module_inserted_request *request;
825 struct gb_connection *connection = operation->connection;
826 struct gb_svc *svc = gb_connection_get_data(connection);
827 struct gb_host_device *hd = svc->hd;
828 struct gb_module *module;
829 size_t num_interfaces;
834 /* The request message size has already been verified. */
835 request = operation->request->payload;
836 module_id = request->primary_intf_id;
837 num_interfaces = request->intf_count;
838 flags = le16_to_cpu(request->flags);
840 dev_dbg(&svc->dev, "%s - id = %u, num_interfaces = %zu, flags = 0x%04x\n",
841 __func__, module_id, num_interfaces, flags);
843 if (flags & GB_SVC_MODULE_INSERTED_FLAG_NO_PRIMARY) {
844 dev_warn(&svc->dev, "no primary interface detected on module %u\n",
848 module = gb_svc_module_lookup(svc, module_id);
850 dev_warn(&svc->dev, "unexpected module-inserted event %u\n",
855 module = gb_module_create(hd, module_id, num_interfaces);
857 dev_err(&svc->dev, "failed to create module\n");
861 ret = gb_module_add(module);
863 gb_module_put(module);
867 list_add(&module->hd_node, &hd->modules);
870 static void gb_svc_process_module_removed(struct gb_operation *operation)
872 struct gb_svc_module_removed_request *request;
873 struct gb_connection *connection = operation->connection;
874 struct gb_svc *svc = gb_connection_get_data(connection);
875 struct gb_module *module;
878 /* The request message size has already been verified. */
879 request = operation->request->payload;
880 module_id = request->primary_intf_id;
882 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, module_id);
884 module = gb_svc_module_lookup(svc, module_id);
886 dev_warn(&svc->dev, "unexpected module-removed event %u\n",
891 module->disconnected = true;
893 gb_module_del(module);
894 list_del(&module->hd_node);
895 gb_module_put(module);
898 static void gb_svc_process_intf_mailbox_event(struct gb_operation *operation)
900 struct gb_svc_intf_mailbox_event_request *request;
901 struct gb_connection *connection = operation->connection;
902 struct gb_svc *svc = gb_connection_get_data(connection);
903 struct gb_interface *intf;
908 /* The request message size has already been verified. */
909 request = operation->request->payload;
910 intf_id = request->intf_id;
911 result_code = le16_to_cpu(request->result_code);
912 mailbox = le32_to_cpu(request->mailbox);
914 dev_dbg(&svc->dev, "%s - id = %u, result = 0x%04x, mailbox = 0x%08x\n",
915 __func__, intf_id, result_code, mailbox);
917 intf = gb_svc_interface_lookup(svc, intf_id);
919 dev_warn(&svc->dev, "unexpected mailbox event %u\n", intf_id);
925 "mailbox event %u with UniPro error: 0x%04x\n",
926 intf_id, result_code);
927 goto err_disable_interface;
930 if (mailbox != GB_SVC_INTF_MAILBOX_GREYBUS) {
932 "mailbox event %u with unexected value: 0x%08x\n",
934 goto err_disable_interface;
937 dev_info(&svc->dev, "mode switch detected on interface %u\n", intf_id);
939 gb_svc_intf_reenable(svc, intf);
943 err_disable_interface:
944 mutex_lock(&intf->mutex);
945 gb_interface_disable(intf);
946 gb_interface_deactivate(intf);
947 mutex_unlock(&intf->mutex);
950 static void gb_svc_process_deferred_request(struct work_struct *work)
952 struct gb_svc_deferred_request *dr;
953 struct gb_operation *operation;
957 dr = container_of(work, struct gb_svc_deferred_request, work);
958 operation = dr->operation;
959 svc = gb_connection_get_data(operation->connection);
960 type = operation->request->header->type;
963 case GB_SVC_TYPE_INTF_HOTPLUG:
964 gb_svc_process_intf_hotplug(operation);
966 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
967 gb_svc_process_intf_hot_unplug(operation);
969 case GB_SVC_TYPE_MODULE_INSERTED:
970 gb_svc_process_module_inserted(operation);
972 case GB_SVC_TYPE_MODULE_REMOVED:
973 gb_svc_process_module_removed(operation);
975 case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
976 gb_svc_process_intf_mailbox_event(operation);
979 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
982 gb_operation_put(operation);
986 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
988 struct gb_svc *svc = gb_connection_get_data(operation->connection);
989 struct gb_svc_deferred_request *dr;
991 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
995 gb_operation_get(operation);
997 dr->operation = operation;
998 INIT_WORK(&dr->work, gb_svc_process_deferred_request);
1000 queue_work(svc->wq, &dr->work);
1006 * Bringing up a module can be time consuming, as that may require lots of
1007 * initialization on the module side. Over that, we may also need to download
1008 * the firmware first and flash that on the module.
1010 * In order not to make other svc events wait for all this to finish,
1011 * handle most of module hotplug stuff outside of the hotplug callback, with
1012 * help of a workqueue.
1014 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
1016 struct gb_svc *svc = gb_connection_get_data(op->connection);
1017 struct gb_svc_intf_hotplug_request *request;
1019 if (op->request->payload_size < sizeof(*request)) {
1020 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
1021 op->request->payload_size, sizeof(*request));
1025 request = op->request->payload;
1027 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
1029 return gb_svc_queue_deferred_request(op);
1032 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
1034 struct gb_svc *svc = gb_connection_get_data(op->connection);
1035 struct gb_svc_intf_hot_unplug_request *request;
1037 if (op->request->payload_size < sizeof(*request)) {
1038 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
1039 op->request->payload_size, sizeof(*request));
1043 request = op->request->payload;
1045 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
1047 return gb_svc_queue_deferred_request(op);
1050 static int gb_svc_intf_reset_recv(struct gb_operation *op)
1052 struct gb_svc *svc = gb_connection_get_data(op->connection);
1053 struct gb_message *request = op->request;
1054 struct gb_svc_intf_reset_request *reset;
1057 if (request->payload_size < sizeof(*reset)) {
1058 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
1059 request->payload_size, sizeof(*reset));
1062 reset = request->payload;
1064 intf_id = reset->intf_id;
1066 /* FIXME Reset the interface here */
1071 static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
1074 case GB_KEYCODE_ARA:
1075 *code = SVC_KEY_ARA_BUTTON;
1078 dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
1085 static int gb_svc_key_event_recv(struct gb_operation *op)
1087 struct gb_svc *svc = gb_connection_get_data(op->connection);
1088 struct gb_message *request = op->request;
1089 struct gb_svc_key_event_request *key;
1094 if (request->payload_size < sizeof(*key)) {
1095 dev_warn(&svc->dev, "short key request received (%zu < %zu)\n",
1096 request->payload_size, sizeof(*key));
1100 key = request->payload;
1102 ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
1106 event = key->key_event;
1107 if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) {
1108 dev_warn(&svc->dev, "unknown key event received: %u\n", event);
1112 input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
1113 input_sync(svc->input);
1118 static int gb_svc_module_inserted_recv(struct gb_operation *op)
1120 struct gb_svc *svc = gb_connection_get_data(op->connection);
1121 struct gb_svc_module_inserted_request *request;
1123 if (op->request->payload_size < sizeof(*request)) {
1124 dev_warn(&svc->dev, "short module-inserted request received (%zu < %zu)\n",
1125 op->request->payload_size, sizeof(*request));
1129 request = op->request->payload;
1131 dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
1132 request->primary_intf_id);
1134 return gb_svc_queue_deferred_request(op);
1137 static int gb_svc_module_removed_recv(struct gb_operation *op)
1139 struct gb_svc *svc = gb_connection_get_data(op->connection);
1140 struct gb_svc_module_removed_request *request;
1142 if (op->request->payload_size < sizeof(*request)) {
1143 dev_warn(&svc->dev, "short module-removed request received (%zu < %zu)\n",
1144 op->request->payload_size, sizeof(*request));
1148 request = op->request->payload;
1150 dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
1151 request->primary_intf_id);
1153 return gb_svc_queue_deferred_request(op);
1156 static int gb_svc_intf_mailbox_event_recv(struct gb_operation *op)
1158 struct gb_svc *svc = gb_connection_get_data(op->connection);
1159 struct gb_svc_intf_mailbox_event_request *request;
1161 if (op->request->payload_size < sizeof(*request)) {
1162 dev_warn(&svc->dev, "short mailbox request received (%zu < %zu)\n",
1163 op->request->payload_size, sizeof(*request));
1167 request = op->request->payload;
1169 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
1171 return gb_svc_queue_deferred_request(op);
1174 static int gb_svc_request_handler(struct gb_operation *op)
1176 struct gb_connection *connection = op->connection;
1177 struct gb_svc *svc = gb_connection_get_data(connection);
1182 * SVC requests need to follow a specific order (at least initially) and
1183 * below code takes care of enforcing that. The expected order is:
1184 * - PROTOCOL_VERSION
1186 * - Any other request, but the earlier two.
1188 * Incoming requests are guaranteed to be serialized and so we don't
1189 * need to protect 'state' for any races.
1192 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
1193 if (svc->state != GB_SVC_STATE_RESET)
1196 case GB_SVC_TYPE_SVC_HELLO:
1197 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
1201 if (svc->state != GB_SVC_STATE_SVC_HELLO)
1207 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
1213 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
1214 ret = gb_svc_version_request(op);
1216 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
1218 case GB_SVC_TYPE_SVC_HELLO:
1219 ret = gb_svc_hello(op);
1221 svc->state = GB_SVC_STATE_SVC_HELLO;
1223 case GB_SVC_TYPE_INTF_HOTPLUG:
1224 return gb_svc_intf_hotplug_recv(op);
1225 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
1226 return gb_svc_intf_hot_unplug_recv(op);
1227 case GB_SVC_TYPE_INTF_RESET:
1228 return gb_svc_intf_reset_recv(op);
1229 case GB_SVC_TYPE_KEY_EVENT:
1230 return gb_svc_key_event_recv(op);
1231 case GB_SVC_TYPE_MODULE_INSERTED:
1232 return gb_svc_module_inserted_recv(op);
1233 case GB_SVC_TYPE_MODULE_REMOVED:
1234 return gb_svc_module_removed_recv(op);
1235 case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
1236 return gb_svc_intf_mailbox_event_recv(op);
1238 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
1243 static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
1245 struct input_dev *input_dev;
1247 input_dev = input_allocate_device();
1249 return ERR_PTR(-ENOMEM);
1251 input_dev->name = dev_name(&svc->dev);
1252 svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
1254 if (!svc->input_phys)
1255 goto err_free_input;
1257 input_dev->phys = svc->input_phys;
1258 input_dev->dev.parent = &svc->dev;
1260 input_set_drvdata(input_dev, svc);
1262 input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
1267 input_free_device(svc->input);
1268 return ERR_PTR(-ENOMEM);
1271 static void gb_svc_release(struct device *dev)
1273 struct gb_svc *svc = to_gb_svc(dev);
1275 if (svc->connection)
1276 gb_connection_destroy(svc->connection);
1277 ida_destroy(&svc->device_id_map);
1278 destroy_workqueue(svc->wq);
1279 kfree(svc->input_phys);
1283 struct device_type greybus_svc_type = {
1284 .name = "greybus_svc",
1285 .release = gb_svc_release,
1288 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
1292 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
1296 svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
1302 svc->dev.parent = &hd->dev;
1303 svc->dev.bus = &greybus_bus_type;
1304 svc->dev.type = &greybus_svc_type;
1305 svc->dev.groups = svc_groups;
1306 svc->dev.dma_mask = svc->dev.parent->dma_mask;
1307 device_initialize(&svc->dev);
1309 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
1311 ida_init(&svc->device_id_map);
1312 svc->state = GB_SVC_STATE_RESET;
1315 svc->input = gb_svc_input_create(svc);
1316 if (IS_ERR(svc->input)) {
1317 dev_err(&svc->dev, "failed to create input device: %ld\n",
1318 PTR_ERR(svc->input));
1319 goto err_put_device;
1322 svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
1323 gb_svc_request_handler);
1324 if (IS_ERR(svc->connection)) {
1325 dev_err(&svc->dev, "failed to create connection: %ld\n",
1326 PTR_ERR(svc->connection));
1327 goto err_free_input;
1330 gb_connection_set_data(svc->connection, svc);
1335 input_free_device(svc->input);
1337 put_device(&svc->dev);
1341 int gb_svc_add(struct gb_svc *svc)
1346 * The SVC protocol is currently driven by the SVC, so the SVC device
1347 * is added from the connection request handler when enough
1348 * information has been received.
1350 ret = gb_connection_enable(svc->connection);
1357 static void gb_svc_remove_modules(struct gb_svc *svc)
1359 struct gb_host_device *hd = svc->hd;
1360 struct gb_module *module, *tmp;
1362 list_for_each_entry_safe(module, tmp, &hd->modules, hd_node) {
1363 gb_module_del(module);
1364 list_del(&module->hd_node);
1365 gb_module_put(module);
1369 void gb_svc_del(struct gb_svc *svc)
1371 gb_connection_disable(svc->connection);
1374 * The SVC device and input device may have been registered
1375 * from the request handler.
1377 if (device_is_registered(&svc->dev)) {
1378 gb_svc_debugfs_exit(svc);
1379 gb_svc_watchdog_destroy(svc);
1380 input_unregister_device(svc->input);
1381 device_del(&svc->dev);
1384 flush_workqueue(svc->wq);
1386 gb_svc_remove_modules(svc);
1389 void gb_svc_put(struct gb_svc *svc)
1391 put_device(&svc->dev);