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