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 static const struct software_node *ssam_node_group_sp8[] = {
232 	&ssam_node_root,
233 	&ssam_node_bat_ac,
234 	&ssam_node_bat_main,
235 	&ssam_node_tmp_pprof,
236 	/* TODO: Add support for keyboard cover. */
237 	NULL,
238 };
239 
240 
241 /* -- Device registry helper functions. ------------------------------------- */
242 
243 static int ssam_uid_from_string(const char *str, struct ssam_device_uid *uid)
244 {
245 	u8 d, tc, tid, iid, fn;
246 	int n;
247 
248 	n = sscanf(str, "ssam:%hhx:%hhx:%hhx:%hhx:%hhx", &d, &tc, &tid, &iid, &fn);
249 	if (n != 5)
250 		return -EINVAL;
251 
252 	uid->domain = d;
253 	uid->category = tc;
254 	uid->target = tid;
255 	uid->instance = iid;
256 	uid->function = fn;
257 
258 	return 0;
259 }
260 
261 static int ssam_hub_add_device(struct device *parent, struct ssam_controller *ctrl,
262 			       struct fwnode_handle *node)
263 {
264 	struct ssam_device_uid uid;
265 	struct ssam_device *sdev;
266 	int status;
267 
268 	status = ssam_uid_from_string(fwnode_get_name(node), &uid);
269 	if (status)
270 		return status;
271 
272 	sdev = ssam_device_alloc(ctrl, uid);
273 	if (!sdev)
274 		return -ENOMEM;
275 
276 	sdev->dev.parent = parent;
277 	sdev->dev.fwnode = node;
278 
279 	status = ssam_device_add(sdev);
280 	if (status)
281 		ssam_device_put(sdev);
282 
283 	return status;
284 }
285 
286 static int ssam_hub_register_clients(struct device *parent, struct ssam_controller *ctrl,
287 				     struct fwnode_handle *node)
288 {
289 	struct fwnode_handle *child;
290 	int status;
291 
292 	fwnode_for_each_child_node(node, child) {
293 		/*
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.
297 		 */
298 
299 		status = ssam_hub_add_device(parent, ctrl, child);
300 		if (status && status != -EINVAL)
301 			goto err;
302 	}
303 
304 	return 0;
305 err:
306 	ssam_remove_clients(parent);
307 	return status;
308 }
309 
310 
311 /* -- SSAM base-hub driver. ------------------------------------------------- */
312 
313 /*
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
316  * experimentation.
317  */
318 #define SSAM_BASE_UPDATE_CONNECT_DELAY		msecs_to_jiffies(2500)
319 
320 enum ssam_base_hub_state {
321 	SSAM_BASE_HUB_UNINITIALIZED,
322 	SSAM_BASE_HUB_CONNECTED,
323 	SSAM_BASE_HUB_DISCONNECTED,
324 };
325 
326 struct ssam_base_hub {
327 	struct ssam_device *sdev;
328 
329 	enum ssam_base_hub_state state;
330 	struct delayed_work update_work;
331 
332 	struct ssam_event_notifier notif;
333 };
334 
335 SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_query_opmode, u8, {
336 	.target_category = SSAM_SSH_TC_BAS,
337 	.target_id       = 0x01,
338 	.command_id      = 0x0d,
339 	.instance_id     = 0x00,
340 });
341 
342 #define SSAM_BAS_OPMODE_TABLET		0x00
343 #define SSAM_EVENT_BAS_CID_CONNECTION	0x0c
344 
345 static int ssam_base_hub_query_state(struct ssam_base_hub *hub, enum ssam_base_hub_state *state)
346 {
347 	u8 opmode;
348 	int status;
349 
350 	status = ssam_retry(ssam_bas_query_opmode, hub->sdev->ctrl, &opmode);
351 	if (status < 0) {
352 		dev_err(&hub->sdev->dev, "failed to query base state: %d\n", status);
353 		return status;
354 	}
355 
356 	if (opmode != SSAM_BAS_OPMODE_TABLET)
357 		*state = SSAM_BASE_HUB_CONNECTED;
358 	else
359 		*state = SSAM_BASE_HUB_DISCONNECTED;
360 
361 	return 0;
362 }
363 
364 static ssize_t ssam_base_hub_state_show(struct device *dev, struct device_attribute *attr,
365 					char *buf)
366 {
367 	struct ssam_base_hub *hub = dev_get_drvdata(dev);
368 	bool connected = hub->state == SSAM_BASE_HUB_CONNECTED;
369 
370 	return sysfs_emit(buf, "%d\n", connected);
371 }
372 
373 static struct device_attribute ssam_base_hub_attr_state =
374 	__ATTR(state, 0444, ssam_base_hub_state_show, NULL);
375 
376 static struct attribute *ssam_base_hub_attrs[] = {
377 	&ssam_base_hub_attr_state.attr,
378 	NULL,
379 };
380 
381 static const struct attribute_group ssam_base_hub_group = {
382 	.attrs = ssam_base_hub_attrs,
383 };
384 
385 static void ssam_base_hub_update_workfn(struct work_struct *work)
386 {
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;
390 	int status = 0;
391 
392 	status = ssam_base_hub_query_state(hub, &state);
393 	if (status)
394 		return;
395 
396 	if (hub->state == state)
397 		return;
398 	hub->state = state;
399 
400 	if (hub->state == SSAM_BASE_HUB_CONNECTED)
401 		status = ssam_hub_register_clients(&hub->sdev->dev, hub->sdev->ctrl, node);
402 	else
403 		ssam_remove_clients(&hub->sdev->dev);
404 
405 	if (status)
406 		dev_err(&hub->sdev->dev, "failed to update base-hub devices: %d\n", status);
407 }
408 
409 static u32 ssam_base_hub_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
410 {
411 	struct ssam_base_hub *hub = container_of(nf, struct ssam_base_hub, notif);
412 	unsigned long delay;
413 
414 	if (event->command_id != SSAM_EVENT_BAS_CID_CONNECTION)
415 		return 0;
416 
417 	if (event->length < 1) {
418 		dev_err(&hub->sdev->dev, "unexpected payload size: %u\n", event->length);
419 		return 0;
420 	}
421 
422 	/*
423 	 * Delay update when the base is being connected to give devices/EC
424 	 * some time to set up.
425 	 */
426 	delay = event->data[0] ? SSAM_BASE_UPDATE_CONNECT_DELAY : 0;
427 
428 	schedule_delayed_work(&hub->update_work, delay);
429 
430 	/*
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)
433 	 * silent observer.
434 	 */
435 	return 0;
436 }
437 
438 static int __maybe_unused ssam_base_hub_resume(struct device *dev)
439 {
440 	struct ssam_base_hub *hub = dev_get_drvdata(dev);
441 
442 	schedule_delayed_work(&hub->update_work, 0);
443 	return 0;
444 }
445 static SIMPLE_DEV_PM_OPS(ssam_base_hub_pm_ops, NULL, ssam_base_hub_resume);
446 
447 static int ssam_base_hub_probe(struct ssam_device *sdev)
448 {
449 	struct ssam_base_hub *hub;
450 	int status;
451 
452 	hub = devm_kzalloc(&sdev->dev, sizeof(*hub), GFP_KERNEL);
453 	if (!hub)
454 		return -ENOMEM;
455 
456 	hub->sdev = sdev;
457 	hub->state = SSAM_BASE_HUB_UNINITIALIZED;
458 
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;
466 
467 	INIT_DELAYED_WORK(&hub->update_work, ssam_base_hub_update_workfn);
468 
469 	ssam_device_set_drvdata(sdev, hub);
470 
471 	status = ssam_notifier_register(sdev->ctrl, &hub->notif);
472 	if (status)
473 		return status;
474 
475 	status = sysfs_create_group(&sdev->dev.kobj, &ssam_base_hub_group);
476 	if (status)
477 		goto err;
478 
479 	schedule_delayed_work(&hub->update_work, 0);
480 	return 0;
481 
482 err:
483 	ssam_notifier_unregister(sdev->ctrl, &hub->notif);
484 	cancel_delayed_work_sync(&hub->update_work);
485 	ssam_remove_clients(&sdev->dev);
486 	return status;
487 }
488 
489 static void ssam_base_hub_remove(struct ssam_device *sdev)
490 {
491 	struct ssam_base_hub *hub = ssam_device_get_drvdata(sdev);
492 
493 	sysfs_remove_group(&sdev->dev.kobj, &ssam_base_hub_group);
494 
495 	ssam_notifier_unregister(sdev->ctrl, &hub->notif);
496 	cancel_delayed_work_sync(&hub->update_work);
497 	ssam_remove_clients(&sdev->dev);
498 }
499 
500 static const struct ssam_device_id ssam_base_hub_match[] = {
501 	{ SSAM_VDEV(HUB, 0x02, SSAM_ANY_IID, 0x00) },
502 	{ },
503 };
504 
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,
509 	.driver = {
510 		.name = "surface_aggregator_base_hub",
511 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
512 		.pm = &ssam_base_hub_pm_ops,
513 	},
514 };
515 
516 
517 /* -- SSAM platform/meta-hub driver. ---------------------------------------- */
518 
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 },
522 
523 	/* Surface Pro 6 (OMBR >= 0x10) */
524 	{ "MSHW0111", (unsigned long)ssam_node_group_gen5 },
525 
526 	/* Surface Pro 7 */
527 	{ "MSHW0116", (unsigned long)ssam_node_group_sp7 },
528 
529 	/* Surface Pro 7+ */
530 	{ "MSHW0119", (unsigned long)ssam_node_group_sp7 },
531 
532 	/* Surface Pro 8 */
533 	{ "MSHW0263", (unsigned long)ssam_node_group_sp8 },
534 
535 	/* Surface Book 2 */
536 	{ "MSHW0107", (unsigned long)ssam_node_group_gen5 },
537 
538 	/* Surface Book 3 */
539 	{ "MSHW0117", (unsigned long)ssam_node_group_sb3 },
540 
541 	/* Surface Laptop 1 */
542 	{ "MSHW0086", (unsigned long)ssam_node_group_gen5 },
543 
544 	/* Surface Laptop 2 */
545 	{ "MSHW0112", (unsigned long)ssam_node_group_gen5 },
546 
547 	/* Surface Laptop 3 (13", Intel) */
548 	{ "MSHW0114", (unsigned long)ssam_node_group_sl3 },
549 
550 	/* Surface Laptop 3 (15", AMD) and 4 (15", AMD) */
551 	{ "MSHW0110", (unsigned long)ssam_node_group_sl3 },
552 
553 	/* Surface Laptop 4 (13", Intel) */
554 	{ "MSHW0250", (unsigned long)ssam_node_group_sl3 },
555 
556 	/* Surface Laptop Go 1 */
557 	{ "MSHW0118", (unsigned long)ssam_node_group_slg1 },
558 
559 	/* Surface Laptop Studio */
560 	{ "MSHW0123", (unsigned long)ssam_node_group_sls },
561 
562 	{ },
563 };
564 MODULE_DEVICE_TABLE(acpi, ssam_platform_hub_match);
565 
566 static int ssam_platform_hub_probe(struct platform_device *pdev)
567 {
568 	const struct software_node **nodes;
569 	struct ssam_controller *ctrl;
570 	struct fwnode_handle *root;
571 	int status;
572 
573 	nodes = (const struct software_node **)acpi_device_get_match_data(&pdev->dev);
574 	if (!nodes)
575 		return -ENODEV;
576 
577 	/*
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.
583 	 */
584 	ctrl = ssam_client_bind(&pdev->dev);
585 	if (IS_ERR(ctrl))
586 		return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
587 
588 	status = software_node_register_node_group(nodes);
589 	if (status)
590 		return status;
591 
592 	root = software_node_fwnode(&ssam_node_root);
593 	if (!root) {
594 		software_node_unregister_node_group(nodes);
595 		return -ENOENT;
596 	}
597 
598 	set_secondary_fwnode(&pdev->dev, root);
599 
600 	status = ssam_hub_register_clients(&pdev->dev, ctrl, root);
601 	if (status) {
602 		set_secondary_fwnode(&pdev->dev, NULL);
603 		software_node_unregister_node_group(nodes);
604 	}
605 
606 	platform_set_drvdata(pdev, nodes);
607 	return status;
608 }
609 
610 static int ssam_platform_hub_remove(struct platform_device *pdev)
611 {
612 	const struct software_node **nodes = platform_get_drvdata(pdev);
613 
614 	ssam_remove_clients(&pdev->dev);
615 	set_secondary_fwnode(&pdev->dev, NULL);
616 	software_node_unregister_node_group(nodes);
617 	return 0;
618 }
619 
620 static struct platform_driver ssam_platform_hub_driver = {
621 	.probe = ssam_platform_hub_probe,
622 	.remove = ssam_platform_hub_remove,
623 	.driver = {
624 		.name = "surface_aggregator_platform_hub",
625 		.acpi_match_table = ssam_platform_hub_match,
626 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
627 	},
628 };
629 
630 
631 /* -- Module initialization. ------------------------------------------------ */
632 
633 static int __init ssam_device_hub_init(void)
634 {
635 	int status;
636 
637 	status = platform_driver_register(&ssam_platform_hub_driver);
638 	if (status)
639 		return status;
640 
641 	status = ssam_device_driver_register(&ssam_base_hub_driver);
642 	if (status)
643 		platform_driver_unregister(&ssam_platform_hub_driver);
644 
645 	return status;
646 }
647 module_init(ssam_device_hub_init);
648 
649 static void __exit ssam_device_hub_exit(void)
650 {
651 	ssam_device_driver_unregister(&ssam_base_hub_driver);
652 	platform_driver_unregister(&ssam_platform_hub_driver);
653 }
654 module_exit(ssam_device_hub_exit);
655 
656 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
657 MODULE_DESCRIPTION("Device-registry for Surface System Aggregator Module");
658 MODULE_LICENSE("GPL");
659