1 // SPDX-License-Identifier: GPL-2.0+
3 * Surface System Aggregator Module (SSAM) client device registry.
5 * Registry for non-platform/non-ACPI SSAM client devices, i.e. devices that
6 * cannot be auto-detected. Provides device-hubs and performs instantiation
9 * Copyright (C) 2020-2021 Maximilian Luz <luzmaximilian@gmail.com>
12 #include <linux/acpi.h>
13 #include <linux/kernel.h>
14 #include <linux/limits.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/types.h>
21 #include <linux/surface_aggregator/controller.h>
22 #include <linux/surface_aggregator/device.h>
25 /* -- Device registry. ------------------------------------------------------ */
28 * SSAM device names follow the SSAM module alias, meaning they are prefixed
29 * with 'ssam:', followed by domain, category, target ID, instance ID, and
30 * function, each encoded as two-digit hexadecimal, separated by ':'. In other
31 * words, it follows the scheme
35 * Where, 'dd', 'cc', 'tt', 'ii', and 'ff' are the two-digit hexadecimal
36 * values mentioned above, respectively.
40 static const struct software_node ssam_node_root = {
41 .name = "ssam_platform_hub",
44 /* Base device hub (devices attached to Surface Book 3 base). */
45 static const struct software_node ssam_node_hub_base = {
46 .name = "ssam:00:00:02:00:00",
47 .parent = &ssam_node_root,
51 static const struct software_node ssam_node_bat_ac = {
52 .name = "ssam:01:02:01:01:01",
53 .parent = &ssam_node_root,
56 /* Primary battery. */
57 static const struct software_node ssam_node_bat_main = {
58 .name = "ssam:01:02:01:01:00",
59 .parent = &ssam_node_root,
62 /* Secondary battery (Surface Book 3). */
63 static const struct software_node ssam_node_bat_sb3base = {
64 .name = "ssam:01:02:02:01:00",
65 .parent = &ssam_node_hub_base,
68 /* Platform profile / performance-mode device. */
69 static const struct software_node ssam_node_tmp_pprof = {
70 .name = "ssam:01:03:01:00:01",
71 .parent = &ssam_node_root,
74 /* DTX / detachment-system device (Surface Book 3). */
75 static const struct software_node ssam_node_bas_dtx = {
76 .name = "ssam:01:11:01:00:00",
77 .parent = &ssam_node_root,
81 static const struct software_node ssam_node_hid_main_keyboard = {
82 .name = "ssam:01:15:02:01:00",
83 .parent = &ssam_node_root,
87 static const struct software_node ssam_node_hid_main_touchpad = {
88 .name = "ssam:01:15:02:03:00",
89 .parent = &ssam_node_root,
92 /* HID device instance 5 (unknown HID device). */
93 static const struct software_node ssam_node_hid_main_iid5 = {
94 .name = "ssam:01:15:02:05:00",
95 .parent = &ssam_node_root,
98 /* HID keyboard (base hub). */
99 static const struct software_node ssam_node_hid_base_keyboard = {
100 .name = "ssam:01:15:02:01:00",
101 .parent = &ssam_node_hub_base,
104 /* HID touchpad (base hub). */
105 static const struct software_node ssam_node_hid_base_touchpad = {
106 .name = "ssam:01:15:02:03:00",
107 .parent = &ssam_node_hub_base,
110 /* HID device instance 5 (unknown HID device, base hub). */
111 static const struct software_node ssam_node_hid_base_iid5 = {
112 .name = "ssam:01:15:02:05:00",
113 .parent = &ssam_node_hub_base,
116 /* HID device instance 6 (unknown HID device, base hub). */
117 static const struct software_node ssam_node_hid_base_iid6 = {
118 .name = "ssam:01:15:02:06:00",
119 .parent = &ssam_node_hub_base,
122 /* Devices for Surface Book 2. */
123 static const struct software_node *ssam_node_group_sb2[] = {
125 &ssam_node_tmp_pprof,
129 /* Devices for Surface Book 3. */
130 static const struct software_node *ssam_node_group_sb3[] = {
135 &ssam_node_bat_sb3base,
136 &ssam_node_tmp_pprof,
138 &ssam_node_hid_base_keyboard,
139 &ssam_node_hid_base_touchpad,
140 &ssam_node_hid_base_iid5,
141 &ssam_node_hid_base_iid6,
145 /* Devices for Surface Laptop 1. */
146 static const struct software_node *ssam_node_group_sl1[] = {
148 &ssam_node_tmp_pprof,
152 /* Devices for Surface Laptop 2. */
153 static const struct software_node *ssam_node_group_sl2[] = {
155 &ssam_node_tmp_pprof,
159 /* Devices for Surface Laptop 3. */
160 static const struct software_node *ssam_node_group_sl3[] = {
164 &ssam_node_tmp_pprof,
165 &ssam_node_hid_main_keyboard,
166 &ssam_node_hid_main_touchpad,
167 &ssam_node_hid_main_iid5,
171 /* Devices for Surface Laptop Go. */
172 static const struct software_node *ssam_node_group_slg1[] = {
176 &ssam_node_tmp_pprof,
180 /* Devices for Surface Pro 5. */
181 static const struct software_node *ssam_node_group_sp5[] = {
183 &ssam_node_tmp_pprof,
187 /* Devices for Surface Pro 6. */
188 static const struct software_node *ssam_node_group_sp6[] = {
190 &ssam_node_tmp_pprof,
194 /* Devices for Surface Pro 7. */
195 static const struct software_node *ssam_node_group_sp7[] = {
199 &ssam_node_tmp_pprof,
204 /* -- Device registry helper functions. ------------------------------------- */
206 static int ssam_uid_from_string(const char *str, struct ssam_device_uid *uid)
208 u8 d, tc, tid, iid, fn;
211 n = sscanf(str, "ssam:%hhx:%hhx:%hhx:%hhx:%hhx", &d, &tc, &tid, &iid, &fn);
224 static int ssam_hub_remove_devices_fn(struct device *dev, void *data)
226 if (!is_ssam_device(dev))
229 ssam_device_remove(to_ssam_device(dev));
233 static void ssam_hub_remove_devices(struct device *parent)
235 device_for_each_child_reverse(parent, NULL, ssam_hub_remove_devices_fn);
238 static int ssam_hub_add_device(struct device *parent, struct ssam_controller *ctrl,
239 struct fwnode_handle *node)
241 struct ssam_device_uid uid;
242 struct ssam_device *sdev;
245 status = ssam_uid_from_string(fwnode_get_name(node), &uid);
249 sdev = ssam_device_alloc(ctrl, uid);
253 sdev->dev.parent = parent;
254 sdev->dev.fwnode = node;
256 status = ssam_device_add(sdev);
258 ssam_device_put(sdev);
263 static int ssam_hub_add_devices(struct device *parent, struct ssam_controller *ctrl,
264 struct fwnode_handle *node)
266 struct fwnode_handle *child;
269 fwnode_for_each_child_node(node, child) {
271 * Try to add the device specified in the firmware node. If
272 * this fails with -EINVAL, the node does not specify any SSAM
273 * device, so ignore it and continue with the next one.
276 status = ssam_hub_add_device(parent, ctrl, child);
277 if (status && status != -EINVAL)
283 ssam_hub_remove_devices(parent);
288 /* -- SSAM base-hub driver. ------------------------------------------------- */
290 enum ssam_base_hub_state {
291 SSAM_BASE_HUB_UNINITIALIZED,
292 SSAM_BASE_HUB_CONNECTED,
293 SSAM_BASE_HUB_DISCONNECTED,
296 struct ssam_base_hub {
297 struct ssam_device *sdev;
299 struct mutex lock; /* Guards state update checks and transitions. */
300 enum ssam_base_hub_state state;
302 struct ssam_event_notifier notif;
305 SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_query_opmode, u8, {
306 .target_category = SSAM_SSH_TC_BAS,
312 #define SSAM_BAS_OPMODE_TABLET 0x00
313 #define SSAM_EVENT_BAS_CID_CONNECTION 0x0c
315 static int ssam_base_hub_query_state(struct ssam_base_hub *hub, enum ssam_base_hub_state *state)
320 status = ssam_retry(ssam_bas_query_opmode, hub->sdev->ctrl, &opmode);
322 dev_err(&hub->sdev->dev, "failed to query base state: %d\n", status);
326 if (opmode != SSAM_BAS_OPMODE_TABLET)
327 *state = SSAM_BASE_HUB_CONNECTED;
329 *state = SSAM_BASE_HUB_DISCONNECTED;
334 static ssize_t ssam_base_hub_state_show(struct device *dev, struct device_attribute *attr,
337 struct ssam_base_hub *hub = dev_get_drvdata(dev);
340 mutex_lock(&hub->lock);
341 connected = hub->state == SSAM_BASE_HUB_CONNECTED;
342 mutex_unlock(&hub->lock);
344 return sysfs_emit(buf, "%d\n", connected);
347 static struct device_attribute ssam_base_hub_attr_state =
348 __ATTR(state, 0444, ssam_base_hub_state_show, NULL);
350 static struct attribute *ssam_base_hub_attrs[] = {
351 &ssam_base_hub_attr_state.attr,
355 const struct attribute_group ssam_base_hub_group = {
356 .attrs = ssam_base_hub_attrs,
359 static int __ssam_base_hub_update(struct ssam_base_hub *hub, enum ssam_base_hub_state new)
361 struct fwnode_handle *node = dev_fwnode(&hub->sdev->dev);
364 lockdep_assert_held(&hub->lock);
366 if (hub->state == new)
370 if (hub->state == SSAM_BASE_HUB_CONNECTED)
371 status = ssam_hub_add_devices(&hub->sdev->dev, hub->sdev->ctrl, node);
373 ssam_hub_remove_devices(&hub->sdev->dev);
376 dev_err(&hub->sdev->dev, "failed to update base-hub devices: %d\n", status);
381 static int ssam_base_hub_update(struct ssam_base_hub *hub)
383 enum ssam_base_hub_state state;
386 mutex_lock(&hub->lock);
388 status = ssam_base_hub_query_state(hub, &state);
390 status = __ssam_base_hub_update(hub, state);
392 mutex_unlock(&hub->lock);
396 static u32 ssam_base_hub_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
398 struct ssam_base_hub *hub;
399 struct ssam_device *sdev;
400 enum ssam_base_hub_state new;
402 hub = container_of(nf, struct ssam_base_hub, notif);
405 if (event->command_id != SSAM_EVENT_BAS_CID_CONNECTION)
408 if (event->length < 1) {
409 dev_err(&sdev->dev, "unexpected payload size: %u\n",
415 new = SSAM_BASE_HUB_CONNECTED;
417 new = SSAM_BASE_HUB_DISCONNECTED;
419 mutex_lock(&hub->lock);
420 __ssam_base_hub_update(hub, new);
421 mutex_unlock(&hub->lock);
424 * Do not return SSAM_NOTIF_HANDLED: The event should be picked up and
425 * consumed by the detachment system driver. We're just a (more or less)
431 static int __maybe_unused ssam_base_hub_resume(struct device *dev)
433 return ssam_base_hub_update(dev_get_drvdata(dev));
435 static SIMPLE_DEV_PM_OPS(ssam_base_hub_pm_ops, NULL, ssam_base_hub_resume);
437 static int ssam_base_hub_probe(struct ssam_device *sdev)
439 struct ssam_base_hub *hub;
442 hub = devm_kzalloc(&sdev->dev, sizeof(*hub), GFP_KERNEL);
446 mutex_init(&hub->lock);
449 hub->state = SSAM_BASE_HUB_UNINITIALIZED;
451 hub->notif.base.priority = INT_MAX; /* This notifier should run first. */
452 hub->notif.base.fn = ssam_base_hub_notif;
453 hub->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
454 hub->notif.event.id.target_category = SSAM_SSH_TC_BAS,
455 hub->notif.event.id.instance = 0,
456 hub->notif.event.mask = SSAM_EVENT_MASK_NONE;
457 hub->notif.event.flags = SSAM_EVENT_SEQUENCED;
459 ssam_device_set_drvdata(sdev, hub);
461 status = ssam_notifier_register(sdev->ctrl, &hub->notif);
465 status = ssam_base_hub_update(hub);
469 status = sysfs_create_group(&sdev->dev.kobj, &ssam_base_hub_group);
476 ssam_notifier_unregister(sdev->ctrl, &hub->notif);
477 ssam_hub_remove_devices(&sdev->dev);
479 mutex_destroy(&hub->lock);
483 static void ssam_base_hub_remove(struct ssam_device *sdev)
485 struct ssam_base_hub *hub = ssam_device_get_drvdata(sdev);
487 sysfs_remove_group(&sdev->dev.kobj, &ssam_base_hub_group);
489 ssam_notifier_unregister(sdev->ctrl, &hub->notif);
490 ssam_hub_remove_devices(&sdev->dev);
492 mutex_destroy(&hub->lock);
495 static const struct ssam_device_id ssam_base_hub_match[] = {
496 { SSAM_VDEV(HUB, 0x02, SSAM_ANY_IID, 0x00) },
500 static struct ssam_device_driver ssam_base_hub_driver = {
501 .probe = ssam_base_hub_probe,
502 .remove = ssam_base_hub_remove,
503 .match_table = ssam_base_hub_match,
505 .name = "surface_aggregator_base_hub",
506 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
507 .pm = &ssam_base_hub_pm_ops,
512 /* -- SSAM platform/meta-hub driver. ---------------------------------------- */
514 static const struct acpi_device_id ssam_platform_hub_match[] = {
515 /* Surface Pro 4, 5, and 6 (OMBR < 0x10) */
516 { "MSHW0081", (unsigned long)ssam_node_group_sp5 },
518 /* Surface Pro 6 (OMBR >= 0x10) */
519 { "MSHW0111", (unsigned long)ssam_node_group_sp6 },
522 { "MSHW0116", (unsigned long)ssam_node_group_sp7 },
525 { "MSHW0107", (unsigned long)ssam_node_group_sb2 },
528 { "MSHW0117", (unsigned long)ssam_node_group_sb3 },
530 /* Surface Laptop 1 */
531 { "MSHW0086", (unsigned long)ssam_node_group_sl1 },
533 /* Surface Laptop 2 */
534 { "MSHW0112", (unsigned long)ssam_node_group_sl2 },
536 /* Surface Laptop 3 (13", Intel) */
537 { "MSHW0114", (unsigned long)ssam_node_group_sl3 },
539 /* Surface Laptop 3 (15", AMD) */
540 { "MSHW0110", (unsigned long)ssam_node_group_sl3 },
542 /* Surface Laptop Go 1 */
543 { "MSHW0118", (unsigned long)ssam_node_group_slg1 },
547 MODULE_DEVICE_TABLE(acpi, ssam_platform_hub_match);
549 static int ssam_platform_hub_probe(struct platform_device *pdev)
551 const struct software_node **nodes;
552 struct ssam_controller *ctrl;
553 struct fwnode_handle *root;
556 nodes = (const struct software_node **)acpi_device_get_match_data(&pdev->dev);
561 * As we're adding the SSAM client devices as children under this device
562 * and not the SSAM controller, we need to add a device link to the
563 * controller to ensure that we remove all of our devices before the
564 * controller is removed. This also guarantees proper ordering for
565 * suspend/resume of the devices on this hub.
567 ctrl = ssam_client_bind(&pdev->dev);
569 return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
571 status = software_node_register_node_group(nodes);
575 root = software_node_fwnode(&ssam_node_root);
577 software_node_unregister_node_group(nodes);
581 set_secondary_fwnode(&pdev->dev, root);
583 status = ssam_hub_add_devices(&pdev->dev, ctrl, root);
585 set_secondary_fwnode(&pdev->dev, NULL);
586 software_node_unregister_node_group(nodes);
589 platform_set_drvdata(pdev, nodes);
593 static int ssam_platform_hub_remove(struct platform_device *pdev)
595 const struct software_node **nodes = platform_get_drvdata(pdev);
597 ssam_hub_remove_devices(&pdev->dev);
598 set_secondary_fwnode(&pdev->dev, NULL);
599 software_node_unregister_node_group(nodes);
603 static struct platform_driver ssam_platform_hub_driver = {
604 .probe = ssam_platform_hub_probe,
605 .remove = ssam_platform_hub_remove,
607 .name = "surface_aggregator_platform_hub",
608 .acpi_match_table = ssam_platform_hub_match,
609 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
614 /* -- Module initialization. ------------------------------------------------ */
616 static int __init ssam_device_hub_init(void)
620 status = platform_driver_register(&ssam_platform_hub_driver);
624 status = ssam_device_driver_register(&ssam_base_hub_driver);
626 platform_driver_unregister(&ssam_platform_hub_driver);
630 module_init(ssam_device_hub_init);
632 static void __exit ssam_device_hub_exit(void)
634 ssam_device_driver_unregister(&ssam_base_hub_driver);
635 platform_driver_unregister(&ssam_platform_hub_driver);
637 module_exit(ssam_device_hub_exit);
639 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
640 MODULE_DESCRIPTION("Device-registry for Surface System Aggregator Module");
641 MODULE_LICENSE("GPL");