GNU Linux-libre 5.15.54-gnu
[releases.git] / drivers / platform / surface / surface_aggregator_registry.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Surface System Aggregator Module (SSAM) client device registry.
4  *
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
7  * for these devices.
8  *
9  * Copyright (C) 2020-2021 Maximilian Luz <luzmaximilian@gmail.com>
10  */
11
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>
20
21 #include <linux/surface_aggregator/controller.h>
22 #include <linux/surface_aggregator/device.h>
23
24
25 /* -- Device registry. ------------------------------------------------------ */
26
27 /*
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
32  *
33  *      ssam:dd:cc:tt:ii:ff
34  *
35  * Where, 'dd', 'cc', 'tt', 'ii', and 'ff' are the two-digit hexadecimal
36  * values mentioned above, respectively.
37  */
38
39 /* Root node. */
40 static const struct software_node ssam_node_root = {
41         .name = "ssam_platform_hub",
42 };
43
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,
48 };
49
50 /* AC adapter. */
51 static const struct software_node ssam_node_bat_ac = {
52         .name = "ssam:01:02:01:01:01",
53         .parent = &ssam_node_root,
54 };
55
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,
60 };
61
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,
66 };
67
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,
72 };
73
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,
78 };
79
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,
84 };
85
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,
90 };
91
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,
96 };
97
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,
102 };
103
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,
108 };
109
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,
114 };
115
116 /* HID keyboard. */
117 static const struct software_node ssam_node_hid_main_keyboard = {
118         .name = "ssam:01:15:02:01:00",
119         .parent = &ssam_node_root,
120 };
121
122 /* HID touchpad. */
123 static const struct software_node ssam_node_hid_main_touchpad = {
124         .name = "ssam:01:15:02:03:00",
125         .parent = &ssam_node_root,
126 };
127
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,
132 };
133
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,
138 };
139
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,
144 };
145
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,
150 };
151
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,
156 };
157
158 /*
159  * Devices for 5th- and 6th-generations models:
160  * - Surface Book 2,
161  * - Surface Laptop 1 and 2,
162  * - Surface Pro 5 and 6.
163  */
164 static const struct software_node *ssam_node_group_gen5[] = {
165         &ssam_node_root,
166         &ssam_node_tmp_pprof,
167         NULL,
168 };
169
170 /* Devices for Surface Book 3. */
171 static const struct software_node *ssam_node_group_sb3[] = {
172         &ssam_node_root,
173         &ssam_node_hub_base,
174         &ssam_node_bat_ac,
175         &ssam_node_bat_main,
176         &ssam_node_bat_sb3base,
177         &ssam_node_tmp_pprof,
178         &ssam_node_bas_dtx,
179         &ssam_node_hid_base_keyboard,
180         &ssam_node_hid_base_touchpad,
181         &ssam_node_hid_base_iid5,
182         &ssam_node_hid_base_iid6,
183         NULL,
184 };
185
186 /* Devices for Surface Laptop 3 and 4. */
187 static const struct software_node *ssam_node_group_sl3[] = {
188         &ssam_node_root,
189         &ssam_node_bat_ac,
190         &ssam_node_bat_main,
191         &ssam_node_tmp_pprof,
192         &ssam_node_hid_main_keyboard,
193         &ssam_node_hid_main_touchpad,
194         &ssam_node_hid_main_iid5,
195         NULL,
196 };
197
198 /* Devices for Surface Laptop Studio. */
199 static const struct software_node *ssam_node_group_sls[] = {
200         &ssam_node_root,
201         &ssam_node_bat_ac,
202         &ssam_node_bat_main,
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,
210         NULL,
211 };
212
213 /* Devices for Surface Laptop Go. */
214 static const struct software_node *ssam_node_group_slg1[] = {
215         &ssam_node_root,
216         &ssam_node_bat_ac,
217         &ssam_node_bat_main,
218         &ssam_node_tmp_pprof,
219         NULL,
220 };
221
222 /* Devices for Surface Pro 7 and Surface Pro 7+. */
223 static const struct software_node *ssam_node_group_sp7[] = {
224         &ssam_node_root,
225         &ssam_node_bat_ac,
226         &ssam_node_bat_main,
227         &ssam_node_tmp_pprof,
228         NULL,
229 };
230
231
232 /* -- Device registry helper functions. ------------------------------------- */
233
234 static int ssam_uid_from_string(const char *str, struct ssam_device_uid *uid)
235 {
236         u8 d, tc, tid, iid, fn;
237         int n;
238
239         n = sscanf(str, "ssam:%hhx:%hhx:%hhx:%hhx:%hhx", &d, &tc, &tid, &iid, &fn);
240         if (n != 5)
241                 return -EINVAL;
242
243         uid->domain = d;
244         uid->category = tc;
245         uid->target = tid;
246         uid->instance = iid;
247         uid->function = fn;
248
249         return 0;
250 }
251
252 static int ssam_hub_remove_devices_fn(struct device *dev, void *data)
253 {
254         if (!is_ssam_device(dev))
255                 return 0;
256
257         ssam_device_remove(to_ssam_device(dev));
258         return 0;
259 }
260
261 static void ssam_hub_remove_devices(struct device *parent)
262 {
263         device_for_each_child_reverse(parent, NULL, ssam_hub_remove_devices_fn);
264 }
265
266 static int ssam_hub_add_device(struct device *parent, struct ssam_controller *ctrl,
267                                struct fwnode_handle *node)
268 {
269         struct ssam_device_uid uid;
270         struct ssam_device *sdev;
271         int status;
272
273         status = ssam_uid_from_string(fwnode_get_name(node), &uid);
274         if (status)
275                 return status;
276
277         sdev = ssam_device_alloc(ctrl, uid);
278         if (!sdev)
279                 return -ENOMEM;
280
281         sdev->dev.parent = parent;
282         sdev->dev.fwnode = node;
283
284         status = ssam_device_add(sdev);
285         if (status)
286                 ssam_device_put(sdev);
287
288         return status;
289 }
290
291 static int ssam_hub_add_devices(struct device *parent, struct ssam_controller *ctrl,
292                                 struct fwnode_handle *node)
293 {
294         struct fwnode_handle *child;
295         int status;
296
297         fwnode_for_each_child_node(node, child) {
298                 /*
299                  * Try to add the device specified in the firmware node. If
300                  * this fails with -EINVAL, the node does not specify any SSAM
301                  * device, so ignore it and continue with the next one.
302                  */
303
304                 status = ssam_hub_add_device(parent, ctrl, child);
305                 if (status && status != -EINVAL)
306                         goto err;
307         }
308
309         return 0;
310 err:
311         ssam_hub_remove_devices(parent);
312         return status;
313 }
314
315
316 /* -- SSAM base-hub driver. ------------------------------------------------- */
317
318 /*
319  * Some devices (especially battery) may need a bit of time to be fully usable
320  * after being (re-)connected. This delay has been determined via
321  * experimentation.
322  */
323 #define SSAM_BASE_UPDATE_CONNECT_DELAY          msecs_to_jiffies(2500)
324
325 enum ssam_base_hub_state {
326         SSAM_BASE_HUB_UNINITIALIZED,
327         SSAM_BASE_HUB_CONNECTED,
328         SSAM_BASE_HUB_DISCONNECTED,
329 };
330
331 struct ssam_base_hub {
332         struct ssam_device *sdev;
333
334         enum ssam_base_hub_state state;
335         struct delayed_work update_work;
336
337         struct ssam_event_notifier notif;
338 };
339
340 SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_query_opmode, u8, {
341         .target_category = SSAM_SSH_TC_BAS,
342         .target_id       = 0x01,
343         .command_id      = 0x0d,
344         .instance_id     = 0x00,
345 });
346
347 #define SSAM_BAS_OPMODE_TABLET          0x00
348 #define SSAM_EVENT_BAS_CID_CONNECTION   0x0c
349
350 static int ssam_base_hub_query_state(struct ssam_base_hub *hub, enum ssam_base_hub_state *state)
351 {
352         u8 opmode;
353         int status;
354
355         status = ssam_retry(ssam_bas_query_opmode, hub->sdev->ctrl, &opmode);
356         if (status < 0) {
357                 dev_err(&hub->sdev->dev, "failed to query base state: %d\n", status);
358                 return status;
359         }
360
361         if (opmode != SSAM_BAS_OPMODE_TABLET)
362                 *state = SSAM_BASE_HUB_CONNECTED;
363         else
364                 *state = SSAM_BASE_HUB_DISCONNECTED;
365
366         return 0;
367 }
368
369 static ssize_t ssam_base_hub_state_show(struct device *dev, struct device_attribute *attr,
370                                         char *buf)
371 {
372         struct ssam_base_hub *hub = dev_get_drvdata(dev);
373         bool connected = hub->state == SSAM_BASE_HUB_CONNECTED;
374
375         return sysfs_emit(buf, "%d\n", connected);
376 }
377
378 static struct device_attribute ssam_base_hub_attr_state =
379         __ATTR(state, 0444, ssam_base_hub_state_show, NULL);
380
381 static struct attribute *ssam_base_hub_attrs[] = {
382         &ssam_base_hub_attr_state.attr,
383         NULL,
384 };
385
386 static const struct attribute_group ssam_base_hub_group = {
387         .attrs = ssam_base_hub_attrs,
388 };
389
390 static void ssam_base_hub_update_workfn(struct work_struct *work)
391 {
392         struct ssam_base_hub *hub = container_of(work, struct ssam_base_hub, update_work.work);
393         struct fwnode_handle *node = dev_fwnode(&hub->sdev->dev);
394         enum ssam_base_hub_state state;
395         int status = 0;
396
397         status = ssam_base_hub_query_state(hub, &state);
398         if (status)
399                 return;
400
401         if (hub->state == state)
402                 return;
403         hub->state = state;
404
405         if (hub->state == SSAM_BASE_HUB_CONNECTED)
406                 status = ssam_hub_add_devices(&hub->sdev->dev, hub->sdev->ctrl, node);
407         else
408                 ssam_hub_remove_devices(&hub->sdev->dev);
409
410         if (status)
411                 dev_err(&hub->sdev->dev, "failed to update base-hub devices: %d\n", status);
412 }
413
414 static u32 ssam_base_hub_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
415 {
416         struct ssam_base_hub *hub = container_of(nf, struct ssam_base_hub, notif);
417         unsigned long delay;
418
419         if (event->command_id != SSAM_EVENT_BAS_CID_CONNECTION)
420                 return 0;
421
422         if (event->length < 1) {
423                 dev_err(&hub->sdev->dev, "unexpected payload size: %u\n", event->length);
424                 return 0;
425         }
426
427         /*
428          * Delay update when the base is being connected to give devices/EC
429          * some time to set up.
430          */
431         delay = event->data[0] ? SSAM_BASE_UPDATE_CONNECT_DELAY : 0;
432
433         schedule_delayed_work(&hub->update_work, delay);
434
435         /*
436          * Do not return SSAM_NOTIF_HANDLED: The event should be picked up and
437          * consumed by the detachment system driver. We're just a (more or less)
438          * silent observer.
439          */
440         return 0;
441 }
442
443 static int __maybe_unused ssam_base_hub_resume(struct device *dev)
444 {
445         struct ssam_base_hub *hub = dev_get_drvdata(dev);
446
447         schedule_delayed_work(&hub->update_work, 0);
448         return 0;
449 }
450 static SIMPLE_DEV_PM_OPS(ssam_base_hub_pm_ops, NULL, ssam_base_hub_resume);
451
452 static int ssam_base_hub_probe(struct ssam_device *sdev)
453 {
454         struct ssam_base_hub *hub;
455         int status;
456
457         hub = devm_kzalloc(&sdev->dev, sizeof(*hub), GFP_KERNEL);
458         if (!hub)
459                 return -ENOMEM;
460
461         hub->sdev = sdev;
462         hub->state = SSAM_BASE_HUB_UNINITIALIZED;
463
464         hub->notif.base.priority = INT_MAX;  /* This notifier should run first. */
465         hub->notif.base.fn = ssam_base_hub_notif;
466         hub->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
467         hub->notif.event.id.target_category = SSAM_SSH_TC_BAS,
468         hub->notif.event.id.instance = 0,
469         hub->notif.event.mask = SSAM_EVENT_MASK_NONE;
470         hub->notif.event.flags = SSAM_EVENT_SEQUENCED;
471
472         INIT_DELAYED_WORK(&hub->update_work, ssam_base_hub_update_workfn);
473
474         ssam_device_set_drvdata(sdev, hub);
475
476         status = ssam_notifier_register(sdev->ctrl, &hub->notif);
477         if (status)
478                 return status;
479
480         status = sysfs_create_group(&sdev->dev.kobj, &ssam_base_hub_group);
481         if (status)
482                 goto err;
483
484         schedule_delayed_work(&hub->update_work, 0);
485         return 0;
486
487 err:
488         ssam_notifier_unregister(sdev->ctrl, &hub->notif);
489         cancel_delayed_work_sync(&hub->update_work);
490         ssam_hub_remove_devices(&sdev->dev);
491         return status;
492 }
493
494 static void ssam_base_hub_remove(struct ssam_device *sdev)
495 {
496         struct ssam_base_hub *hub = ssam_device_get_drvdata(sdev);
497
498         sysfs_remove_group(&sdev->dev.kobj, &ssam_base_hub_group);
499
500         ssam_notifier_unregister(sdev->ctrl, &hub->notif);
501         cancel_delayed_work_sync(&hub->update_work);
502         ssam_hub_remove_devices(&sdev->dev);
503 }
504
505 static const struct ssam_device_id ssam_base_hub_match[] = {
506         { SSAM_VDEV(HUB, 0x02, SSAM_ANY_IID, 0x00) },
507         { },
508 };
509
510 static struct ssam_device_driver ssam_base_hub_driver = {
511         .probe = ssam_base_hub_probe,
512         .remove = ssam_base_hub_remove,
513         .match_table = ssam_base_hub_match,
514         .driver = {
515                 .name = "surface_aggregator_base_hub",
516                 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
517                 .pm = &ssam_base_hub_pm_ops,
518         },
519 };
520
521
522 /* -- SSAM platform/meta-hub driver. ---------------------------------------- */
523
524 static const struct acpi_device_id ssam_platform_hub_match[] = {
525         /* Surface Pro 4, 5, and 6 (OMBR < 0x10) */
526         { "MSHW0081", (unsigned long)ssam_node_group_gen5 },
527
528         /* Surface Pro 6 (OMBR >= 0x10) */
529         { "MSHW0111", (unsigned long)ssam_node_group_gen5 },
530
531         /* Surface Pro 7 */
532         { "MSHW0116", (unsigned long)ssam_node_group_sp7 },
533
534         /* Surface Pro 7+ */
535         { "MSHW0119", (unsigned long)ssam_node_group_sp7 },
536
537         /* Surface Book 2 */
538         { "MSHW0107", (unsigned long)ssam_node_group_gen5 },
539
540         /* Surface Book 3 */
541         { "MSHW0117", (unsigned long)ssam_node_group_sb3 },
542
543         /* Surface Laptop 1 */
544         { "MSHW0086", (unsigned long)ssam_node_group_gen5 },
545
546         /* Surface Laptop 2 */
547         { "MSHW0112", (unsigned long)ssam_node_group_gen5 },
548
549         /* Surface Laptop 3 (13", Intel) */
550         { "MSHW0114", (unsigned long)ssam_node_group_sl3 },
551
552         /* Surface Laptop 3 (15", AMD) and 4 (15", AMD) */
553         { "MSHW0110", (unsigned long)ssam_node_group_sl3 },
554
555         /* Surface Laptop 4 (13", Intel) */
556         { "MSHW0250", (unsigned long)ssam_node_group_sl3 },
557
558         /* Surface Laptop Go 1 */
559         { "MSHW0118", (unsigned long)ssam_node_group_slg1 },
560
561         /* Surface Laptop Studio */
562         { "MSHW0123", (unsigned long)ssam_node_group_sls },
563
564         { },
565 };
566 MODULE_DEVICE_TABLE(acpi, ssam_platform_hub_match);
567
568 static int ssam_platform_hub_probe(struct platform_device *pdev)
569 {
570         const struct software_node **nodes;
571         struct ssam_controller *ctrl;
572         struct fwnode_handle *root;
573         int status;
574
575         nodes = (const struct software_node **)acpi_device_get_match_data(&pdev->dev);
576         if (!nodes)
577                 return -ENODEV;
578
579         /*
580          * As we're adding the SSAM client devices as children under this device
581          * and not the SSAM controller, we need to add a device link to the
582          * controller to ensure that we remove all of our devices before the
583          * controller is removed. This also guarantees proper ordering for
584          * suspend/resume of the devices on this hub.
585          */
586         ctrl = ssam_client_bind(&pdev->dev);
587         if (IS_ERR(ctrl))
588                 return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
589
590         status = software_node_register_node_group(nodes);
591         if (status)
592                 return status;
593
594         root = software_node_fwnode(&ssam_node_root);
595         if (!root) {
596                 software_node_unregister_node_group(nodes);
597                 return -ENOENT;
598         }
599
600         set_secondary_fwnode(&pdev->dev, root);
601
602         status = ssam_hub_add_devices(&pdev->dev, ctrl, root);
603         if (status) {
604                 set_secondary_fwnode(&pdev->dev, NULL);
605                 software_node_unregister_node_group(nodes);
606         }
607
608         platform_set_drvdata(pdev, nodes);
609         return status;
610 }
611
612 static int ssam_platform_hub_remove(struct platform_device *pdev)
613 {
614         const struct software_node **nodes = platform_get_drvdata(pdev);
615
616         ssam_hub_remove_devices(&pdev->dev);
617         set_secondary_fwnode(&pdev->dev, NULL);
618         software_node_unregister_node_group(nodes);
619         return 0;
620 }
621
622 static struct platform_driver ssam_platform_hub_driver = {
623         .probe = ssam_platform_hub_probe,
624         .remove = ssam_platform_hub_remove,
625         .driver = {
626                 .name = "surface_aggregator_platform_hub",
627                 .acpi_match_table = ssam_platform_hub_match,
628                 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
629         },
630 };
631
632
633 /* -- Module initialization. ------------------------------------------------ */
634
635 static int __init ssam_device_hub_init(void)
636 {
637         int status;
638
639         status = platform_driver_register(&ssam_platform_hub_driver);
640         if (status)
641                 return status;
642
643         status = ssam_device_driver_register(&ssam_base_hub_driver);
644         if (status)
645                 platform_driver_unregister(&ssam_platform_hub_driver);
646
647         return status;
648 }
649 module_init(ssam_device_hub_init);
650
651 static void __exit ssam_device_hub_exit(void)
652 {
653         ssam_device_driver_unregister(&ssam_base_hub_driver);
654         platform_driver_unregister(&ssam_platform_hub_driver);
655 }
656 module_exit(ssam_device_hub_exit);
657
658 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
659 MODULE_DESCRIPTION("Device-registry for Surface System Aggregator Module");
660 MODULE_LICENSE("GPL");