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
12 #include <linux/acpi.h>
13 #include <linux/kernel.h>
14 #include <linux/limits.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/types.h>
19 #include <linux/workqueue.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,
80 /* HID keyboard (TID1). */
81 static const struct software_node ssam_node_hid_tid1_keyboard = {
82 .name = "ssam:01:15:01:01:00",
83 .parent = &ssam_node_root,
86 /* HID pen stash (TID1; pen taken / stashed away evens). */
87 static const struct software_node ssam_node_hid_tid1_penstash = {
88 .name = "ssam:01:15:01:02:00",
89 .parent = &ssam_node_root,
92 /* HID touchpad (TID1). */
93 static const struct software_node ssam_node_hid_tid1_touchpad = {
94 .name = "ssam:01:15:01:03:00",
95 .parent = &ssam_node_root,
98 /* HID device instance 6 (TID1, unknown HID device). */
99 static const struct software_node ssam_node_hid_tid1_iid6 = {
100 .name = "ssam:01:15:01:06:00",
101 .parent = &ssam_node_root,
104 /* HID device instance 7 (TID1, unknown HID device). */
105 static const struct software_node ssam_node_hid_tid1_iid7 = {
106 .name = "ssam:01:15:01:07:00",
107 .parent = &ssam_node_root,
110 /* HID system controls (TID1). */
111 static const struct software_node ssam_node_hid_tid1_sysctrl = {
112 .name = "ssam:01:15:01:08:00",
113 .parent = &ssam_node_root,
117 static const struct software_node ssam_node_hid_main_keyboard = {
118 .name = "ssam:01:15:02:01:00",
119 .parent = &ssam_node_root,
123 static const struct software_node ssam_node_hid_main_touchpad = {
124 .name = "ssam:01:15:02:03:00",
125 .parent = &ssam_node_root,
128 /* HID device instance 5 (unknown HID device). */
129 static const struct software_node ssam_node_hid_main_iid5 = {
130 .name = "ssam:01:15:02:05:00",
131 .parent = &ssam_node_root,
134 /* HID keyboard (base hub). */
135 static const struct software_node ssam_node_hid_base_keyboard = {
136 .name = "ssam:01:15:02:01:00",
137 .parent = &ssam_node_hub_base,
140 /* HID touchpad (base hub). */
141 static const struct software_node ssam_node_hid_base_touchpad = {
142 .name = "ssam:01:15:02:03:00",
143 .parent = &ssam_node_hub_base,
146 /* HID device instance 5 (unknown HID device, base hub). */
147 static const struct software_node ssam_node_hid_base_iid5 = {
148 .name = "ssam:01:15:02:05:00",
149 .parent = &ssam_node_hub_base,
152 /* HID device instance 6 (unknown HID device, base hub). */
153 static const struct software_node ssam_node_hid_base_iid6 = {
154 .name = "ssam:01:15:02:06:00",
155 .parent = &ssam_node_hub_base,
159 * Devices for 5th- and 6th-generations models:
161 * - Surface Laptop 1 and 2,
162 * - Surface Pro 5 and 6.
164 static const struct software_node *ssam_node_group_gen5[] = {
166 &ssam_node_tmp_pprof,
170 /* Devices for Surface Book 3. */
171 static const struct software_node *ssam_node_group_sb3[] = {
176 &ssam_node_bat_sb3base,
177 &ssam_node_tmp_pprof,
179 &ssam_node_hid_base_keyboard,
180 &ssam_node_hid_base_touchpad,
181 &ssam_node_hid_base_iid5,
182 &ssam_node_hid_base_iid6,
186 /* Devices for Surface Laptop 3 and 4. */
187 static const struct software_node *ssam_node_group_sl3[] = {
191 &ssam_node_tmp_pprof,
192 &ssam_node_hid_main_keyboard,
193 &ssam_node_hid_main_touchpad,
194 &ssam_node_hid_main_iid5,
198 /* Devices for Surface Laptop Studio. */
199 static const struct software_node *ssam_node_group_sls[] = {
203 &ssam_node_tmp_pprof,
204 &ssam_node_hid_tid1_keyboard,
205 &ssam_node_hid_tid1_penstash,
206 &ssam_node_hid_tid1_touchpad,
207 &ssam_node_hid_tid1_iid6,
208 &ssam_node_hid_tid1_iid7,
209 &ssam_node_hid_tid1_sysctrl,
213 /* Devices for Surface Laptop Go. */
214 static const struct software_node *ssam_node_group_slg1[] = {
218 &ssam_node_tmp_pprof,
222 /* Devices for Surface Pro 7 and Surface Pro 7+. */
223 static const struct software_node *ssam_node_group_sp7[] = {
227 &ssam_node_tmp_pprof,
231 static const struct software_node *ssam_node_group_sp8[] = {
235 &ssam_node_tmp_pprof,
236 /* TODO: Add support for keyboard cover. */
241 /* -- Device registry helper functions. ------------------------------------- */
243 static int ssam_uid_from_string(const char *str, struct ssam_device_uid *uid)
245 u8 d, tc, tid, iid, fn;
248 n = sscanf(str, "ssam:%hhx:%hhx:%hhx:%hhx:%hhx", &d, &tc, &tid, &iid, &fn);
261 static int ssam_hub_add_device(struct device *parent, struct ssam_controller *ctrl,
262 struct fwnode_handle *node)
264 struct ssam_device_uid uid;
265 struct ssam_device *sdev;
268 status = ssam_uid_from_string(fwnode_get_name(node), &uid);
272 sdev = ssam_device_alloc(ctrl, uid);
276 sdev->dev.parent = parent;
277 sdev->dev.fwnode = node;
279 status = ssam_device_add(sdev);
281 ssam_device_put(sdev);
286 static int ssam_hub_register_clients(struct device *parent, struct ssam_controller *ctrl,
287 struct fwnode_handle *node)
289 struct fwnode_handle *child;
292 fwnode_for_each_child_node(node, child) {
294 * Try to add the device specified in the firmware node. If
295 * this fails with -EINVAL, the node does not specify any SSAM
296 * device, so ignore it and continue with the next one.
299 status = ssam_hub_add_device(parent, ctrl, child);
300 if (status && status != -EINVAL)
306 ssam_remove_clients(parent);
311 /* -- SSAM base-hub driver. ------------------------------------------------- */
314 * Some devices (especially battery) may need a bit of time to be fully usable
315 * after being (re-)connected. This delay has been determined via
318 #define SSAM_BASE_UPDATE_CONNECT_DELAY msecs_to_jiffies(2500)
320 enum ssam_base_hub_state {
321 SSAM_BASE_HUB_UNINITIALIZED,
322 SSAM_BASE_HUB_CONNECTED,
323 SSAM_BASE_HUB_DISCONNECTED,
326 struct ssam_base_hub {
327 struct ssam_device *sdev;
329 enum ssam_base_hub_state state;
330 struct delayed_work update_work;
332 struct ssam_event_notifier notif;
335 SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_query_opmode, u8, {
336 .target_category = SSAM_SSH_TC_BAS,
342 #define SSAM_BAS_OPMODE_TABLET 0x00
343 #define SSAM_EVENT_BAS_CID_CONNECTION 0x0c
345 static int ssam_base_hub_query_state(struct ssam_base_hub *hub, enum ssam_base_hub_state *state)
350 status = ssam_retry(ssam_bas_query_opmode, hub->sdev->ctrl, &opmode);
352 dev_err(&hub->sdev->dev, "failed to query base state: %d\n", status);
356 if (opmode != SSAM_BAS_OPMODE_TABLET)
357 *state = SSAM_BASE_HUB_CONNECTED;
359 *state = SSAM_BASE_HUB_DISCONNECTED;
364 static ssize_t ssam_base_hub_state_show(struct device *dev, struct device_attribute *attr,
367 struct ssam_base_hub *hub = dev_get_drvdata(dev);
368 bool connected = hub->state == SSAM_BASE_HUB_CONNECTED;
370 return sysfs_emit(buf, "%d\n", connected);
373 static struct device_attribute ssam_base_hub_attr_state =
374 __ATTR(state, 0444, ssam_base_hub_state_show, NULL);
376 static struct attribute *ssam_base_hub_attrs[] = {
377 &ssam_base_hub_attr_state.attr,
381 static const struct attribute_group ssam_base_hub_group = {
382 .attrs = ssam_base_hub_attrs,
385 static void ssam_base_hub_update_workfn(struct work_struct *work)
387 struct ssam_base_hub *hub = container_of(work, struct ssam_base_hub, update_work.work);
388 struct fwnode_handle *node = dev_fwnode(&hub->sdev->dev);
389 enum ssam_base_hub_state state;
392 status = ssam_base_hub_query_state(hub, &state);
396 if (hub->state == state)
400 if (hub->state == SSAM_BASE_HUB_CONNECTED)
401 status = ssam_hub_register_clients(&hub->sdev->dev, hub->sdev->ctrl, node);
403 ssam_remove_clients(&hub->sdev->dev);
406 dev_err(&hub->sdev->dev, "failed to update base-hub devices: %d\n", status);
409 static u32 ssam_base_hub_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
411 struct ssam_base_hub *hub = container_of(nf, struct ssam_base_hub, notif);
414 if (event->command_id != SSAM_EVENT_BAS_CID_CONNECTION)
417 if (event->length < 1) {
418 dev_err(&hub->sdev->dev, "unexpected payload size: %u\n", event->length);
423 * Delay update when the base is being connected to give devices/EC
424 * some time to set up.
426 delay = event->data[0] ? SSAM_BASE_UPDATE_CONNECT_DELAY : 0;
428 schedule_delayed_work(&hub->update_work, delay);
431 * Do not return SSAM_NOTIF_HANDLED: The event should be picked up and
432 * consumed by the detachment system driver. We're just a (more or less)
438 static int __maybe_unused ssam_base_hub_resume(struct device *dev)
440 struct ssam_base_hub *hub = dev_get_drvdata(dev);
442 schedule_delayed_work(&hub->update_work, 0);
445 static SIMPLE_DEV_PM_OPS(ssam_base_hub_pm_ops, NULL, ssam_base_hub_resume);
447 static int ssam_base_hub_probe(struct ssam_device *sdev)
449 struct ssam_base_hub *hub;
452 hub = devm_kzalloc(&sdev->dev, sizeof(*hub), GFP_KERNEL);
457 hub->state = SSAM_BASE_HUB_UNINITIALIZED;
459 hub->notif.base.priority = INT_MAX; /* This notifier should run first. */
460 hub->notif.base.fn = ssam_base_hub_notif;
461 hub->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
462 hub->notif.event.id.target_category = SSAM_SSH_TC_BAS,
463 hub->notif.event.id.instance = 0,
464 hub->notif.event.mask = SSAM_EVENT_MASK_NONE;
465 hub->notif.event.flags = SSAM_EVENT_SEQUENCED;
467 INIT_DELAYED_WORK(&hub->update_work, ssam_base_hub_update_workfn);
469 ssam_device_set_drvdata(sdev, hub);
471 status = ssam_notifier_register(sdev->ctrl, &hub->notif);
475 status = sysfs_create_group(&sdev->dev.kobj, &ssam_base_hub_group);
479 schedule_delayed_work(&hub->update_work, 0);
483 ssam_notifier_unregister(sdev->ctrl, &hub->notif);
484 cancel_delayed_work_sync(&hub->update_work);
485 ssam_remove_clients(&sdev->dev);
489 static void ssam_base_hub_remove(struct ssam_device *sdev)
491 struct ssam_base_hub *hub = ssam_device_get_drvdata(sdev);
493 sysfs_remove_group(&sdev->dev.kobj, &ssam_base_hub_group);
495 ssam_notifier_unregister(sdev->ctrl, &hub->notif);
496 cancel_delayed_work_sync(&hub->update_work);
497 ssam_remove_clients(&sdev->dev);
500 static const struct ssam_device_id ssam_base_hub_match[] = {
501 { SSAM_VDEV(HUB, 0x02, SSAM_ANY_IID, 0x00) },
505 static struct ssam_device_driver ssam_base_hub_driver = {
506 .probe = ssam_base_hub_probe,
507 .remove = ssam_base_hub_remove,
508 .match_table = ssam_base_hub_match,
510 .name = "surface_aggregator_base_hub",
511 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
512 .pm = &ssam_base_hub_pm_ops,
517 /* -- SSAM platform/meta-hub driver. ---------------------------------------- */
519 static const struct acpi_device_id ssam_platform_hub_match[] = {
520 /* Surface Pro 4, 5, and 6 (OMBR < 0x10) */
521 { "MSHW0081", (unsigned long)ssam_node_group_gen5 },
523 /* Surface Pro 6 (OMBR >= 0x10) */
524 { "MSHW0111", (unsigned long)ssam_node_group_gen5 },
527 { "MSHW0116", (unsigned long)ssam_node_group_sp7 },
530 { "MSHW0119", (unsigned long)ssam_node_group_sp7 },
533 { "MSHW0263", (unsigned long)ssam_node_group_sp8 },
536 { "MSHW0107", (unsigned long)ssam_node_group_gen5 },
539 { "MSHW0117", (unsigned long)ssam_node_group_sb3 },
541 /* Surface Laptop 1 */
542 { "MSHW0086", (unsigned long)ssam_node_group_gen5 },
544 /* Surface Laptop 2 */
545 { "MSHW0112", (unsigned long)ssam_node_group_gen5 },
547 /* Surface Laptop 3 (13", Intel) */
548 { "MSHW0114", (unsigned long)ssam_node_group_sl3 },
550 /* Surface Laptop 3 (15", AMD) and 4 (15", AMD) */
551 { "MSHW0110", (unsigned long)ssam_node_group_sl3 },
553 /* Surface Laptop 4 (13", Intel) */
554 { "MSHW0250", (unsigned long)ssam_node_group_sl3 },
556 /* Surface Laptop Go 1 */
557 { "MSHW0118", (unsigned long)ssam_node_group_slg1 },
559 /* Surface Laptop Studio */
560 { "MSHW0123", (unsigned long)ssam_node_group_sls },
564 MODULE_DEVICE_TABLE(acpi, ssam_platform_hub_match);
566 static int ssam_platform_hub_probe(struct platform_device *pdev)
568 const struct software_node **nodes;
569 struct ssam_controller *ctrl;
570 struct fwnode_handle *root;
573 nodes = (const struct software_node **)acpi_device_get_match_data(&pdev->dev);
578 * As we're adding the SSAM client devices as children under this device
579 * and not the SSAM controller, we need to add a device link to the
580 * controller to ensure that we remove all of our devices before the
581 * controller is removed. This also guarantees proper ordering for
582 * suspend/resume of the devices on this hub.
584 ctrl = ssam_client_bind(&pdev->dev);
586 return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
588 status = software_node_register_node_group(nodes);
592 root = software_node_fwnode(&ssam_node_root);
594 software_node_unregister_node_group(nodes);
598 set_secondary_fwnode(&pdev->dev, root);
600 status = ssam_hub_register_clients(&pdev->dev, ctrl, root);
602 set_secondary_fwnode(&pdev->dev, NULL);
603 software_node_unregister_node_group(nodes);
606 platform_set_drvdata(pdev, nodes);
610 static int ssam_platform_hub_remove(struct platform_device *pdev)
612 const struct software_node **nodes = platform_get_drvdata(pdev);
614 ssam_remove_clients(&pdev->dev);
615 set_secondary_fwnode(&pdev->dev, NULL);
616 software_node_unregister_node_group(nodes);
620 static struct platform_driver ssam_platform_hub_driver = {
621 .probe = ssam_platform_hub_probe,
622 .remove = ssam_platform_hub_remove,
624 .name = "surface_aggregator_platform_hub",
625 .acpi_match_table = ssam_platform_hub_match,
626 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
631 /* -- Module initialization. ------------------------------------------------ */
633 static int __init ssam_device_hub_init(void)
637 status = platform_driver_register(&ssam_platform_hub_driver);
641 status = ssam_device_driver_register(&ssam_base_hub_driver);
643 platform_driver_unregister(&ssam_platform_hub_driver);
647 module_init(ssam_device_hub_init);
649 static void __exit ssam_device_hub_exit(void)
651 ssam_device_driver_unregister(&ssam_base_hub_driver);
652 platform_driver_unregister(&ssam_platform_hub_driver);
654 module_exit(ssam_device_hub_exit);
657 MODULE_DESCRIPTION("Device-registry for Surface System Aggregator Module");
658 MODULE_LICENSE("GPL");