• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Surface Serial Hub (SSH) driver for communication with the Surface/System
4  * Aggregator Module (SSAM/SAM).
5  *
6  * Provides access to a SAM-over-SSH connected EC via a controller device.
7  * Handles communication via requests as well as enabling, disabling, and
8  * relaying of events.
9  *
10  * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
11  */
12 
13 #include <linux/acpi.h>
14 #include <linux/atomic.h>
15 #include <linux/completion.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/kernel.h>
18 #include <linux/kref.h>
19 #include <linux/module.h>
20 #include <linux/pm.h>
21 #include <linux/serdev.h>
22 #include <linux/sysfs.h>
23 
24 #include <linux/surface_aggregator/controller.h>
25 
26 #include "bus.h"
27 #include "controller.h"
28 
29 #define CREATE_TRACE_POINTS
30 #include "trace.h"
31 
32 
33 /* -- Static controller reference. ------------------------------------------ */
34 
35 /*
36  * Main controller reference. The corresponding lock must be held while
37  * accessing (reading/writing) the reference.
38  */
39 static struct ssam_controller *__ssam_controller;
40 static DEFINE_SPINLOCK(__ssam_controller_lock);
41 
42 /**
43  * ssam_get_controller() - Get reference to SSAM controller.
44  *
45  * Returns a reference to the SSAM controller of the system or %NULL if there
46  * is none, it hasn't been set up yet, or it has already been unregistered.
47  * This function automatically increments the reference count of the
48  * controller, thus the calling party must ensure that ssam_controller_put()
49  * is called when it doesn't need the controller any more.
50  */
ssam_get_controller(void)51 struct ssam_controller *ssam_get_controller(void)
52 {
53 	struct ssam_controller *ctrl;
54 
55 	spin_lock(&__ssam_controller_lock);
56 
57 	ctrl = __ssam_controller;
58 	if (!ctrl)
59 		goto out;
60 
61 	if (WARN_ON(!kref_get_unless_zero(&ctrl->kref)))
62 		ctrl = NULL;
63 
64 out:
65 	spin_unlock(&__ssam_controller_lock);
66 	return ctrl;
67 }
68 EXPORT_SYMBOL_GPL(ssam_get_controller);
69 
70 /**
71  * ssam_try_set_controller() - Try to set the main controller reference.
72  * @ctrl: The controller to which the reference should point.
73  *
74  * Set the main controller reference to the given pointer if the reference
75  * hasn't been set already.
76  *
77  * Return: Returns zero on success or %-EEXIST if the reference has already
78  * been set.
79  */
ssam_try_set_controller(struct ssam_controller * ctrl)80 static int ssam_try_set_controller(struct ssam_controller *ctrl)
81 {
82 	int status = 0;
83 
84 	spin_lock(&__ssam_controller_lock);
85 	if (!__ssam_controller)
86 		__ssam_controller = ctrl;
87 	else
88 		status = -EEXIST;
89 	spin_unlock(&__ssam_controller_lock);
90 
91 	return status;
92 }
93 
94 /**
95  * ssam_clear_controller() - Remove/clear the main controller reference.
96  *
97  * Clears the main controller reference, i.e. sets it to %NULL. This function
98  * should be called before the controller is shut down.
99  */
ssam_clear_controller(void)100 static void ssam_clear_controller(void)
101 {
102 	spin_lock(&__ssam_controller_lock);
103 	__ssam_controller = NULL;
104 	spin_unlock(&__ssam_controller_lock);
105 }
106 
107 /**
108  * ssam_client_link() - Link an arbitrary client device to the controller.
109  * @c: The controller to link to.
110  * @client: The client device.
111  *
112  * Link an arbitrary client device to the controller by creating a device link
113  * between it as consumer and the controller device as provider. This function
114  * can be used for non-SSAM devices (or SSAM devices not registered as child
115  * under the controller) to guarantee that the controller is valid for as long
116  * as the driver of the client device is bound, and that proper suspend and
117  * resume ordering is guaranteed.
118  *
119  * The device link does not have to be destructed manually. It is removed
120  * automatically once the driver of the client device unbinds.
121  *
122  * Return: Returns zero on success, %-ENODEV if the controller is not ready or
123  * going to be removed soon, or %-ENOMEM if the device link could not be
124  * created for other reasons.
125  */
ssam_client_link(struct ssam_controller * c,struct device * client)126 int ssam_client_link(struct ssam_controller *c, struct device *client)
127 {
128 	const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
129 	struct device_link *link;
130 	struct device *ctrldev;
131 
132 	ssam_controller_statelock(c);
133 
134 	if (c->state != SSAM_CONTROLLER_STARTED) {
135 		ssam_controller_stateunlock(c);
136 		return -ENODEV;
137 	}
138 
139 	ctrldev = ssam_controller_device(c);
140 	if (!ctrldev) {
141 		ssam_controller_stateunlock(c);
142 		return -ENODEV;
143 	}
144 
145 	link = device_link_add(client, ctrldev, flags);
146 	if (!link) {
147 		ssam_controller_stateunlock(c);
148 		return -ENOMEM;
149 	}
150 
151 	/*
152 	 * Return -ENODEV if supplier driver is on its way to be removed. In
153 	 * this case, the controller won't be around for much longer and the
154 	 * device link is not going to save us any more, as unbinding is
155 	 * already in progress.
156 	 */
157 	if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {
158 		ssam_controller_stateunlock(c);
159 		return -ENODEV;
160 	}
161 
162 	ssam_controller_stateunlock(c);
163 	return 0;
164 }
165 EXPORT_SYMBOL_GPL(ssam_client_link);
166 
167 /**
168  * ssam_client_bind() - Bind an arbitrary client device to the controller.
169  * @client: The client device.
170  *
171  * Link an arbitrary client device to the controller by creating a device link
172  * between it as consumer and the main controller device as provider. This
173  * function can be used for non-SSAM devices to guarantee that the controller
174  * returned by this function is valid for as long as the driver of the client
175  * device is bound, and that proper suspend and resume ordering is guaranteed.
176  *
177  * This function does essentially the same as ssam_client_link(), except that
178  * it first fetches the main controller reference, then creates the link, and
179  * finally returns this reference. Note that this function does not increment
180  * the reference counter of the controller, as, due to the link, the
181  * controller lifetime is assured as long as the driver of the client device
182  * is bound.
183  *
184  * It is not valid to use the controller reference obtained by this method
185  * outside of the driver bound to the client device at the time of calling
186  * this function, without first incrementing the reference count of the
187  * controller via ssam_controller_get(). Even after doing this, care must be
188  * taken that requests are only submitted and notifiers are only
189  * (un-)registered when the controller is active and not suspended. In other
190  * words: The device link only lives as long as the client driver is bound and
191  * any guarantees enforced by this link (e.g. active controller state) can
192  * only be relied upon as long as this link exists and may need to be enforced
193  * in other ways afterwards.
194  *
195  * The created device link does not have to be destructed manually. It is
196  * removed automatically once the driver of the client device unbinds.
197  *
198  * Return: Returns the controller on success, an error pointer with %-ENODEV
199  * if the controller is not present, not ready or going to be removed soon, or
200  * %-ENOMEM if the device link could not be created for other reasons.
201  */
ssam_client_bind(struct device * client)202 struct ssam_controller *ssam_client_bind(struct device *client)
203 {
204 	struct ssam_controller *c;
205 	int status;
206 
207 	c = ssam_get_controller();
208 	if (!c)
209 		return ERR_PTR(-ENODEV);
210 
211 	status = ssam_client_link(c, client);
212 
213 	/*
214 	 * Note that we can drop our controller reference in both success and
215 	 * failure cases: On success, we have bound the controller lifetime
216 	 * inherently to the client driver lifetime, i.e. it the controller is
217 	 * now guaranteed to outlive the client driver. On failure, we're not
218 	 * going to use the controller any more.
219 	 */
220 	ssam_controller_put(c);
221 
222 	return status >= 0 ? c : ERR_PTR(status);
223 }
224 EXPORT_SYMBOL_GPL(ssam_client_bind);
225 
226 
227 /* -- Glue layer (serdev_device -> ssam_controller). ------------------------ */
228 
ssam_receive_buf(struct serdev_device * dev,const unsigned char * buf,size_t n)229 static int ssam_receive_buf(struct serdev_device *dev, const unsigned char *buf,
230 			    size_t n)
231 {
232 	struct ssam_controller *ctrl;
233 	int ret;
234 
235 	ctrl = serdev_device_get_drvdata(dev);
236 	ret = ssam_controller_receive_buf(ctrl, buf, n);
237 
238 	return ret < 0 ? 0 : ret;
239 }
240 
ssam_write_wakeup(struct serdev_device * dev)241 static void ssam_write_wakeup(struct serdev_device *dev)
242 {
243 	ssam_controller_write_wakeup(serdev_device_get_drvdata(dev));
244 }
245 
246 static const struct serdev_device_ops ssam_serdev_ops = {
247 	.receive_buf = ssam_receive_buf,
248 	.write_wakeup = ssam_write_wakeup,
249 };
250 
251 
252 /* -- SysFS and misc. ------------------------------------------------------- */
253 
ssam_log_firmware_version(struct ssam_controller * ctrl)254 static int ssam_log_firmware_version(struct ssam_controller *ctrl)
255 {
256 	u32 version, a, b, c;
257 	int status;
258 
259 	status = ssam_get_firmware_version(ctrl, &version);
260 	if (status)
261 		return status;
262 
263 	a = (version >> 24) & 0xff;
264 	b = ((version >> 8) & 0xffff);
265 	c = version & 0xff;
266 
267 	ssam_info(ctrl, "SAM firmware version: %u.%u.%u\n", a, b, c);
268 	return 0;
269 }
270 
firmware_version_show(struct device * dev,struct device_attribute * attr,char * buf)271 static ssize_t firmware_version_show(struct device *dev,
272 				     struct device_attribute *attr, char *buf)
273 {
274 	struct ssam_controller *ctrl = dev_get_drvdata(dev);
275 	u32 version, a, b, c;
276 	int status;
277 
278 	status = ssam_get_firmware_version(ctrl, &version);
279 	if (status < 0)
280 		return status;
281 
282 	a = (version >> 24) & 0xff;
283 	b = ((version >> 8) & 0xffff);
284 	c = version & 0xff;
285 
286 	return sysfs_emit(buf, "%u.%u.%u\n", a, b, c);
287 }
288 static DEVICE_ATTR_RO(firmware_version);
289 
290 static struct attribute *ssam_sam_attrs[] = {
291 	&dev_attr_firmware_version.attr,
292 	NULL
293 };
294 
295 static const struct attribute_group ssam_sam_group = {
296 	.name = "sam",
297 	.attrs = ssam_sam_attrs,
298 };
299 
300 
301 /* -- ACPI based device setup. ---------------------------------------------- */
302 
ssam_serdev_setup_via_acpi_crs(struct acpi_resource * rsc,void * ctx)303 static acpi_status ssam_serdev_setup_via_acpi_crs(struct acpi_resource *rsc,
304 						  void *ctx)
305 {
306 	struct serdev_device *serdev = ctx;
307 	struct acpi_resource_uart_serialbus *uart;
308 	bool flow_control;
309 	int status = 0;
310 
311 	if (!serdev_acpi_get_uart_resource(rsc, &uart))
312 		return AE_OK;
313 
314 	/* Set up serdev device. */
315 	serdev_device_set_baudrate(serdev, uart->default_baud_rate);
316 
317 	/* serdev currently only supports RTSCTS flow control. */
318 	if (uart->flow_control & (~((u8)ACPI_UART_FLOW_CONTROL_HW))) {
319 		dev_warn(&serdev->dev, "setup: unsupported flow control (value: %#04x)\n",
320 			 uart->flow_control);
321 	}
322 
323 	/* Set RTSCTS flow control. */
324 	flow_control = uart->flow_control & ACPI_UART_FLOW_CONTROL_HW;
325 	serdev_device_set_flow_control(serdev, flow_control);
326 
327 	/* serdev currently only supports EVEN/ODD parity. */
328 	switch (uart->parity) {
329 	case ACPI_UART_PARITY_NONE:
330 		status = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
331 		break;
332 	case ACPI_UART_PARITY_EVEN:
333 		status = serdev_device_set_parity(serdev, SERDEV_PARITY_EVEN);
334 		break;
335 	case ACPI_UART_PARITY_ODD:
336 		status = serdev_device_set_parity(serdev, SERDEV_PARITY_ODD);
337 		break;
338 	default:
339 		dev_warn(&serdev->dev, "setup: unsupported parity (value: %#04x)\n",
340 			 uart->parity);
341 		break;
342 	}
343 
344 	if (status) {
345 		dev_err(&serdev->dev, "setup: failed to set parity (value: %#04x, error: %d)\n",
346 			uart->parity, status);
347 		return AE_ERROR;
348 	}
349 
350 	/* We've found the resource and are done. */
351 	return AE_CTRL_TERMINATE;
352 }
353 
ssam_serdev_setup_via_acpi(acpi_handle handle,struct serdev_device * serdev)354 static acpi_status ssam_serdev_setup_via_acpi(acpi_handle handle,
355 					      struct serdev_device *serdev)
356 {
357 	return acpi_walk_resources(handle, METHOD_NAME__CRS,
358 				   ssam_serdev_setup_via_acpi_crs, serdev);
359 }
360 
361 
362 /* -- Power management. ----------------------------------------------------- */
363 
ssam_serial_hub_shutdown(struct device * dev)364 static void ssam_serial_hub_shutdown(struct device *dev)
365 {
366 	struct ssam_controller *c = dev_get_drvdata(dev);
367 	int status;
368 
369 	/*
370 	 * Try to disable notifiers, signal display-off and D0-exit, ignore any
371 	 * errors.
372 	 *
373 	 * Note: It has not been established yet if this is actually
374 	 * necessary/useful for shutdown.
375 	 */
376 
377 	status = ssam_notifier_disable_registered(c);
378 	if (status) {
379 		ssam_err(c, "pm: failed to disable notifiers for shutdown: %d\n",
380 			 status);
381 	}
382 
383 	status = ssam_ctrl_notif_display_off(c);
384 	if (status)
385 		ssam_err(c, "pm: display-off notification failed: %d\n", status);
386 
387 	status = ssam_ctrl_notif_d0_exit(c);
388 	if (status)
389 		ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
390 }
391 
392 #ifdef CONFIG_PM_SLEEP
393 
ssam_serial_hub_pm_prepare(struct device * dev)394 static int ssam_serial_hub_pm_prepare(struct device *dev)
395 {
396 	struct ssam_controller *c = dev_get_drvdata(dev);
397 	int status;
398 
399 	/*
400 	 * Try to signal display-off, This will quiesce events.
401 	 *
402 	 * Note: Signaling display-off/display-on should normally be done from
403 	 * some sort of display state notifier. As that is not available,
404 	 * signal it here.
405 	 */
406 
407 	status = ssam_ctrl_notif_display_off(c);
408 	if (status)
409 		ssam_err(c, "pm: display-off notification failed: %d\n", status);
410 
411 	return status;
412 }
413 
ssam_serial_hub_pm_complete(struct device * dev)414 static void ssam_serial_hub_pm_complete(struct device *dev)
415 {
416 	struct ssam_controller *c = dev_get_drvdata(dev);
417 	int status;
418 
419 	/*
420 	 * Try to signal display-on. This will restore events.
421 	 *
422 	 * Note: Signaling display-off/display-on should normally be done from
423 	 * some sort of display state notifier. As that is not available,
424 	 * signal it here.
425 	 */
426 
427 	status = ssam_ctrl_notif_display_on(c);
428 	if (status)
429 		ssam_err(c, "pm: display-on notification failed: %d\n", status);
430 }
431 
ssam_serial_hub_pm_suspend(struct device * dev)432 static int ssam_serial_hub_pm_suspend(struct device *dev)
433 {
434 	struct ssam_controller *c = dev_get_drvdata(dev);
435 	int status;
436 
437 	/*
438 	 * Try to signal D0-exit, enable IRQ wakeup if specified. Abort on
439 	 * error.
440 	 */
441 
442 	status = ssam_ctrl_notif_d0_exit(c);
443 	if (status) {
444 		ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
445 		goto err_notif;
446 	}
447 
448 	status = ssam_irq_arm_for_wakeup(c);
449 	if (status)
450 		goto err_irq;
451 
452 	WARN_ON(ssam_controller_suspend(c));
453 	return 0;
454 
455 err_irq:
456 	ssam_ctrl_notif_d0_entry(c);
457 err_notif:
458 	ssam_ctrl_notif_display_on(c);
459 	return status;
460 }
461 
ssam_serial_hub_pm_resume(struct device * dev)462 static int ssam_serial_hub_pm_resume(struct device *dev)
463 {
464 	struct ssam_controller *c = dev_get_drvdata(dev);
465 	int status;
466 
467 	WARN_ON(ssam_controller_resume(c));
468 
469 	/*
470 	 * Try to disable IRQ wakeup (if specified) and signal D0-entry. In
471 	 * case of errors, log them and try to restore normal operation state
472 	 * as far as possible.
473 	 *
474 	 * Note: Signaling display-off/display-on should normally be done from
475 	 * some sort of display state notifier. As that is not available,
476 	 * signal it here.
477 	 */
478 
479 	ssam_irq_disarm_wakeup(c);
480 
481 	status = ssam_ctrl_notif_d0_entry(c);
482 	if (status)
483 		ssam_err(c, "pm: D0-entry notification failed: %d\n", status);
484 
485 	return 0;
486 }
487 
ssam_serial_hub_pm_freeze(struct device * dev)488 static int ssam_serial_hub_pm_freeze(struct device *dev)
489 {
490 	struct ssam_controller *c = dev_get_drvdata(dev);
491 	int status;
492 
493 	/*
494 	 * During hibernation image creation, we only have to ensure that the
495 	 * EC doesn't send us any events. This is done via the display-off
496 	 * and D0-exit notifications. Note that this sets up the wakeup IRQ
497 	 * on the EC side, however, we have disabled it by default on our side
498 	 * and won't enable it here.
499 	 *
500 	 * See ssam_serial_hub_poweroff() for more details on the hibernation
501 	 * process.
502 	 */
503 
504 	status = ssam_ctrl_notif_d0_exit(c);
505 	if (status) {
506 		ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
507 		ssam_ctrl_notif_display_on(c);
508 		return status;
509 	}
510 
511 	WARN_ON(ssam_controller_suspend(c));
512 	return 0;
513 }
514 
ssam_serial_hub_pm_thaw(struct device * dev)515 static int ssam_serial_hub_pm_thaw(struct device *dev)
516 {
517 	struct ssam_controller *c = dev_get_drvdata(dev);
518 	int status;
519 
520 	WARN_ON(ssam_controller_resume(c));
521 
522 	status = ssam_ctrl_notif_d0_entry(c);
523 	if (status)
524 		ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
525 
526 	return status;
527 }
528 
ssam_serial_hub_pm_poweroff(struct device * dev)529 static int ssam_serial_hub_pm_poweroff(struct device *dev)
530 {
531 	struct ssam_controller *c = dev_get_drvdata(dev);
532 	int status;
533 
534 	/*
535 	 * When entering hibernation and powering off the system, the EC, at
536 	 * least on some models, may disable events. Without us taking care of
537 	 * that, this leads to events not being enabled/restored when the
538 	 * system resumes from hibernation, resulting SAM-HID subsystem devices
539 	 * (i.e. keyboard, touchpad) not working, AC-plug/AC-unplug events being
540 	 * gone, etc.
541 	 *
542 	 * To avoid these issues, we disable all registered events here (this is
543 	 * likely not actually required) and restore them during the drivers PM
544 	 * restore callback.
545 	 *
546 	 * Wakeup from the EC interrupt is not supported during hibernation,
547 	 * so don't arm the IRQ here.
548 	 */
549 
550 	status = ssam_notifier_disable_registered(c);
551 	if (status) {
552 		ssam_err(c, "pm: failed to disable notifiers for hibernation: %d\n",
553 			 status);
554 		return status;
555 	}
556 
557 	status = ssam_ctrl_notif_d0_exit(c);
558 	if (status) {
559 		ssam_err(c, "pm: D0-exit notification failed: %d\n", status);
560 		ssam_notifier_restore_registered(c);
561 		return status;
562 	}
563 
564 	WARN_ON(ssam_controller_suspend(c));
565 	return 0;
566 }
567 
ssam_serial_hub_pm_restore(struct device * dev)568 static int ssam_serial_hub_pm_restore(struct device *dev)
569 {
570 	struct ssam_controller *c = dev_get_drvdata(dev);
571 	int status;
572 
573 	/*
574 	 * Ignore but log errors, try to restore state as much as possible in
575 	 * case of failures. See ssam_serial_hub_poweroff() for more details on
576 	 * the hibernation process.
577 	 */
578 
579 	WARN_ON(ssam_controller_resume(c));
580 
581 	status = ssam_ctrl_notif_d0_entry(c);
582 	if (status)
583 		ssam_err(c, "pm: D0-entry notification failed: %d\n", status);
584 
585 	ssam_notifier_restore_registered(c);
586 	return 0;
587 }
588 
589 static const struct dev_pm_ops ssam_serial_hub_pm_ops = {
590 	.prepare  = ssam_serial_hub_pm_prepare,
591 	.complete = ssam_serial_hub_pm_complete,
592 	.suspend  = ssam_serial_hub_pm_suspend,
593 	.resume   = ssam_serial_hub_pm_resume,
594 	.freeze   = ssam_serial_hub_pm_freeze,
595 	.thaw     = ssam_serial_hub_pm_thaw,
596 	.poweroff = ssam_serial_hub_pm_poweroff,
597 	.restore  = ssam_serial_hub_pm_restore,
598 };
599 
600 #else /* CONFIG_PM_SLEEP */
601 
602 static const struct dev_pm_ops ssam_serial_hub_pm_ops = { };
603 
604 #endif /* CONFIG_PM_SLEEP */
605 
606 
607 /* -- Device/driver setup. -------------------------------------------------- */
608 
609 static const struct acpi_gpio_params gpio_ssam_wakeup_int = { 0, 0, false };
610 static const struct acpi_gpio_params gpio_ssam_wakeup     = { 1, 0, false };
611 
612 static const struct acpi_gpio_mapping ssam_acpi_gpios[] = {
613 	{ "ssam_wakeup-int-gpio", &gpio_ssam_wakeup_int, 1 },
614 	{ "ssam_wakeup-gpio",     &gpio_ssam_wakeup,     1 },
615 	{ },
616 };
617 
ssam_serial_hub_probe(struct serdev_device * serdev)618 static int ssam_serial_hub_probe(struct serdev_device *serdev)
619 {
620 	struct acpi_device *ssh = ACPI_COMPANION(&serdev->dev);
621 	struct ssam_controller *ctrl;
622 	acpi_status astatus;
623 	int status;
624 
625 	if (gpiod_count(&serdev->dev, NULL) < 0)
626 		return -ENODEV;
627 
628 	status = devm_acpi_dev_add_driver_gpios(&serdev->dev, ssam_acpi_gpios);
629 	if (status)
630 		return status;
631 
632 	/* Allocate controller. */
633 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
634 	if (!ctrl)
635 		return -ENOMEM;
636 
637 	/* Initialize controller. */
638 	status = ssam_controller_init(ctrl, serdev);
639 	if (status)
640 		goto err_ctrl_init;
641 
642 	ssam_controller_lock(ctrl);
643 
644 	/* Set up serdev device. */
645 	serdev_device_set_drvdata(serdev, ctrl);
646 	serdev_device_set_client_ops(serdev, &ssam_serdev_ops);
647 	status = serdev_device_open(serdev);
648 	if (status)
649 		goto err_devopen;
650 
651 	astatus = ssam_serdev_setup_via_acpi(ssh->handle, serdev);
652 	if (ACPI_FAILURE(astatus)) {
653 		status = -ENXIO;
654 		goto err_devinit;
655 	}
656 
657 	/* Start controller. */
658 	status = ssam_controller_start(ctrl);
659 	if (status)
660 		goto err_devinit;
661 
662 	ssam_controller_unlock(ctrl);
663 
664 	/*
665 	 * Initial SAM requests: Log version and notify default/init power
666 	 * states.
667 	 */
668 	status = ssam_log_firmware_version(ctrl);
669 	if (status)
670 		goto err_initrq;
671 
672 	status = ssam_ctrl_notif_d0_entry(ctrl);
673 	if (status)
674 		goto err_initrq;
675 
676 	status = ssam_ctrl_notif_display_on(ctrl);
677 	if (status)
678 		goto err_initrq;
679 
680 	status = sysfs_create_group(&serdev->dev.kobj, &ssam_sam_group);
681 	if (status)
682 		goto err_initrq;
683 
684 	/* Set up IRQ. */
685 	status = ssam_irq_setup(ctrl);
686 	if (status)
687 		goto err_irq;
688 
689 	/* Finally, set main controller reference. */
690 	status = ssam_try_set_controller(ctrl);
691 	if (WARN_ON(status))	/* Currently, we're the only provider. */
692 		goto err_mainref;
693 
694 	/*
695 	 * TODO: The EC can wake up the system via the associated GPIO interrupt
696 	 *       in multiple situations. One of which is the remaining battery
697 	 *       capacity falling below a certain threshold. Normally, we should
698 	 *       use the device_init_wakeup function, however, the EC also seems
699 	 *       to have other reasons for waking up the system and it seems
700 	 *       that Windows has additional checks whether the system should be
701 	 *       resumed. In short, this causes some spurious unwanted wake-ups.
702 	 *       For now let's thus default power/wakeup to false.
703 	 */
704 	device_set_wakeup_capable(&serdev->dev, true);
705 	acpi_dev_clear_dependencies(ssh);
706 
707 	return 0;
708 
709 err_mainref:
710 	ssam_irq_free(ctrl);
711 err_irq:
712 	sysfs_remove_group(&serdev->dev.kobj, &ssam_sam_group);
713 err_initrq:
714 	ssam_controller_lock(ctrl);
715 	ssam_controller_shutdown(ctrl);
716 err_devinit:
717 	serdev_device_close(serdev);
718 err_devopen:
719 	ssam_controller_destroy(ctrl);
720 	ssam_controller_unlock(ctrl);
721 err_ctrl_init:
722 	kfree(ctrl);
723 	return status;
724 }
725 
ssam_serial_hub_remove(struct serdev_device * serdev)726 static void ssam_serial_hub_remove(struct serdev_device *serdev)
727 {
728 	struct ssam_controller *ctrl = serdev_device_get_drvdata(serdev);
729 	int status;
730 
731 	/* Clear static reference so that no one else can get a new one. */
732 	ssam_clear_controller();
733 
734 	/* Disable and free IRQ. */
735 	ssam_irq_free(ctrl);
736 
737 	sysfs_remove_group(&serdev->dev.kobj, &ssam_sam_group);
738 	ssam_controller_lock(ctrl);
739 
740 	/* Remove all client devices. */
741 	ssam_controller_remove_clients(ctrl);
742 
743 	/* Act as if suspending to silence events. */
744 	status = ssam_ctrl_notif_display_off(ctrl);
745 	if (status) {
746 		dev_err(&serdev->dev, "display-off notification failed: %d\n",
747 			status);
748 	}
749 
750 	status = ssam_ctrl_notif_d0_exit(ctrl);
751 	if (status) {
752 		dev_err(&serdev->dev, "D0-exit notification failed: %d\n",
753 			status);
754 	}
755 
756 	/* Shut down controller and remove serdev device reference from it. */
757 	ssam_controller_shutdown(ctrl);
758 
759 	/* Shut down actual transport. */
760 	serdev_device_wait_until_sent(serdev, 0);
761 	serdev_device_close(serdev);
762 
763 	/* Drop our controller reference. */
764 	ssam_controller_unlock(ctrl);
765 	ssam_controller_put(ctrl);
766 
767 	device_set_wakeup_capable(&serdev->dev, false);
768 }
769 
770 static const struct acpi_device_id ssam_serial_hub_match[] = {
771 	{ "MSHW0084", 0 },
772 	{ },
773 };
774 MODULE_DEVICE_TABLE(acpi, ssam_serial_hub_match);
775 
776 static struct serdev_device_driver ssam_serial_hub = {
777 	.probe = ssam_serial_hub_probe,
778 	.remove = ssam_serial_hub_remove,
779 	.driver = {
780 		.name = "surface_serial_hub",
781 		.acpi_match_table = ssam_serial_hub_match,
782 		.pm = &ssam_serial_hub_pm_ops,
783 		.shutdown = ssam_serial_hub_shutdown,
784 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
785 	},
786 };
787 
788 
789 /* -- Module setup. --------------------------------------------------------- */
790 
ssam_core_init(void)791 static int __init ssam_core_init(void)
792 {
793 	int status;
794 
795 	status = ssam_bus_register();
796 	if (status)
797 		goto err_bus;
798 
799 	status = ssh_ctrl_packet_cache_init();
800 	if (status)
801 		goto err_cpkg;
802 
803 	status = ssam_event_item_cache_init();
804 	if (status)
805 		goto err_evitem;
806 
807 	status = serdev_device_driver_register(&ssam_serial_hub);
808 	if (status)
809 		goto err_register;
810 
811 	return 0;
812 
813 err_register:
814 	ssam_event_item_cache_destroy();
815 err_evitem:
816 	ssh_ctrl_packet_cache_destroy();
817 err_cpkg:
818 	ssam_bus_unregister();
819 err_bus:
820 	return status;
821 }
822 subsys_initcall(ssam_core_init);
823 
ssam_core_exit(void)824 static void __exit ssam_core_exit(void)
825 {
826 	serdev_device_driver_unregister(&ssam_serial_hub);
827 	ssam_event_item_cache_destroy();
828 	ssh_ctrl_packet_cache_destroy();
829 	ssam_bus_unregister();
830 }
831 module_exit(ssam_core_exit);
832 
833 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
834 MODULE_DESCRIPTION("Subsystem and Surface Serial Hub driver for Surface System Aggregator Module");
835 MODULE_LICENSE("GPL");
836