Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 */
40static struct ssam_controller *__ssam_controller;
41static 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 */
52struct 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
65out:
66 spin_unlock(&__ssam_controller_lock);
67 return ctrl;
68}
69EXPORT_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 */
81static 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 */
101static 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 */
127int 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}
166EXPORT_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 */
203struct 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}
225EXPORT_SYMBOL_GPL(ssam_client_bind);
226
227
228/* -- Glue layer (serdev_device -> ssam_controller). ------------------------ */
229
230static size_t ssam_receive_buf(struct serdev_device *dev, const u8 *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
242static void ssam_write_wakeup(struct serdev_device *dev)
243{
244 ssam_controller_write_wakeup(serdev_device_get_drvdata(dev));
245}
246
247static 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
255static 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
272static 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}
289static DEVICE_ATTR_RO(firmware_version);
290
291static struct attribute *ssam_sam_attrs[] = {
292 &dev_attr_firmware_version.attr,
293 NULL
294};
295
296static const struct attribute_group ssam_sam_group = {
297 .name = "sam",
298 .attrs = ssam_sam_attrs,
299};
300
301
302/* -- ACPI based device setup. ---------------------------------------------- */
303
304static 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
355static 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
365static 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
395static 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
415static 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
433static 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
456err_irq:
457 ssam_ctrl_notif_d0_entry(c);
458err_notif:
459 ssam_ctrl_notif_display_on(c);
460 return status;
461}
462
463static 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
489static 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
516static 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
530static 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
569static 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
590static 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
603static const struct dev_pm_ops ssam_serial_hub_pm_ops = { };
604
605#endif /* CONFIG_PM_SLEEP */
606
607
608/* -- Device/driver setup. -------------------------------------------------- */
609
610static const struct acpi_gpio_params gpio_ssam_wakeup_int = { 0, 0, false };
611static const struct acpi_gpio_params gpio_ssam_wakeup = { 1, 0, false };
612
613static 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
619static int ssam_serial_hub_probe(struct serdev_device *serdev)
620{
621 struct device *dev = &serdev->dev;
622 struct acpi_device *ssh = ACPI_COMPANION(dev);
623 struct ssam_controller *ctrl;
624 acpi_status astatus;
625 int status;
626
627 status = gpiod_count(dev, NULL);
628 if (status < 0)
629 return dev_err_probe(dev, status, "no GPIO found\n");
630
631 status = devm_acpi_dev_add_driver_gpios(dev, ssam_acpi_gpios);
632 if (status)
633 return status;
634
635 /* Allocate controller. */
636 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
637 if (!ctrl)
638 return -ENOMEM;
639
640 /* Initialize controller. */
641 status = ssam_controller_init(ctrl, serdev);
642 if (status) {
643 dev_err_probe(dev, status, "failed to initialize ssam controller\n");
644 goto err_ctrl_init;
645 }
646
647 ssam_controller_lock(ctrl);
648
649 /* Set up serdev device. */
650 serdev_device_set_drvdata(serdev, ctrl);
651 serdev_device_set_client_ops(serdev, &ssam_serdev_ops);
652 status = serdev_device_open(serdev);
653 if (status) {
654 dev_err_probe(dev, status, "failed to open serdev device\n");
655 goto err_devopen;
656 }
657
658 astatus = ssam_serdev_setup_via_acpi(ssh->handle, serdev);
659 if (ACPI_FAILURE(astatus)) {
660 status = dev_err_probe(dev, -ENXIO, "failed to setup serdev\n");
661 goto err_devinit;
662 }
663
664 /* Start controller. */
665 status = ssam_controller_start(ctrl);
666 if (status)
667 goto err_devinit;
668
669 ssam_controller_unlock(ctrl);
670
671 /*
672 * Initial SAM requests: Log version and notify default/init power
673 * states.
674 */
675 status = ssam_log_firmware_version(ctrl);
676 if (status) {
677 dev_err_probe(dev, status, "failed to get firmware version\n");
678 goto err_initrq;
679 }
680
681 status = ssam_ctrl_notif_d0_entry(ctrl);
682 if (status) {
683 dev_err_probe(dev, status, "D0-entry notification failed\n");
684 goto err_initrq;
685 }
686
687 status = ssam_ctrl_notif_display_on(ctrl);
688 if (status) {
689 dev_err_probe(dev, status, "display-on notification failed\n");
690 goto err_initrq;
691 }
692
693 status = sysfs_create_group(&dev->kobj, &ssam_sam_group);
694 if (status)
695 goto err_initrq;
696
697 /* Set up IRQ. */
698 status = ssam_irq_setup(ctrl);
699 if (status) {
700 dev_err_probe(dev, status, "failed to setup IRQ\n");
701 goto err_irq;
702 }
703
704 /* Finally, set main controller reference. */
705 status = ssam_try_set_controller(ctrl);
706 if (WARN_ON(status)) /* Currently, we're the only provider. */
707 goto err_mainref;
708
709 /*
710 * TODO: The EC can wake up the system via the associated GPIO interrupt
711 * in multiple situations. One of which is the remaining battery
712 * capacity falling below a certain threshold. Normally, we should
713 * use the device_init_wakeup function, however, the EC also seems
714 * to have other reasons for waking up the system and it seems
715 * that Windows has additional checks whether the system should be
716 * resumed. In short, this causes some spurious unwanted wake-ups.
717 * For now let's thus default power/wakeup to false.
718 */
719 device_set_wakeup_capable(dev, true);
720 acpi_dev_clear_dependencies(ssh);
721
722 return 0;
723
724err_mainref:
725 ssam_irq_free(ctrl);
726err_irq:
727 sysfs_remove_group(&dev->kobj, &ssam_sam_group);
728err_initrq:
729 ssam_controller_lock(ctrl);
730 ssam_controller_shutdown(ctrl);
731err_devinit:
732 serdev_device_close(serdev);
733err_devopen:
734 ssam_controller_destroy(ctrl);
735 ssam_controller_unlock(ctrl);
736err_ctrl_init:
737 kfree(ctrl);
738 return status;
739}
740
741static void ssam_serial_hub_remove(struct serdev_device *serdev)
742{
743 struct ssam_controller *ctrl = serdev_device_get_drvdata(serdev);
744 int status;
745
746 /* Clear static reference so that no one else can get a new one. */
747 ssam_clear_controller();
748
749 /* Disable and free IRQ. */
750 ssam_irq_free(ctrl);
751
752 sysfs_remove_group(&serdev->dev.kobj, &ssam_sam_group);
753 ssam_controller_lock(ctrl);
754
755 /* Remove all client devices. */
756 ssam_remove_clients(&serdev->dev);
757
758 /* Act as if suspending to silence events. */
759 status = ssam_ctrl_notif_display_off(ctrl);
760 if (status) {
761 dev_err(&serdev->dev, "display-off notification failed: %d\n",
762 status);
763 }
764
765 status = ssam_ctrl_notif_d0_exit(ctrl);
766 if (status) {
767 dev_err(&serdev->dev, "D0-exit notification failed: %d\n",
768 status);
769 }
770
771 /* Shut down controller and remove serdev device reference from it. */
772 ssam_controller_shutdown(ctrl);
773
774 /* Shut down actual transport. */
775 serdev_device_wait_until_sent(serdev, 0);
776 serdev_device_close(serdev);
777
778 /* Drop our controller reference. */
779 ssam_controller_unlock(ctrl);
780 ssam_controller_put(ctrl);
781
782 device_set_wakeup_capable(&serdev->dev, false);
783}
784
785static const struct acpi_device_id ssam_serial_hub_match[] = {
786 { "MSHW0084", 0 },
787 { },
788};
789MODULE_DEVICE_TABLE(acpi, ssam_serial_hub_match);
790
791static struct serdev_device_driver ssam_serial_hub = {
792 .probe = ssam_serial_hub_probe,
793 .remove = ssam_serial_hub_remove,
794 .driver = {
795 .name = "surface_serial_hub",
796 .acpi_match_table = ssam_serial_hub_match,
797 .pm = &ssam_serial_hub_pm_ops,
798 .shutdown = ssam_serial_hub_shutdown,
799 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
800 },
801};
802
803
804/* -- Module setup. --------------------------------------------------------- */
805
806static int __init ssam_core_init(void)
807{
808 int status;
809
810 status = ssam_bus_register();
811 if (status)
812 goto err_bus;
813
814 status = ssh_ctrl_packet_cache_init();
815 if (status)
816 goto err_cpkg;
817
818 status = ssam_event_item_cache_init();
819 if (status)
820 goto err_evitem;
821
822 status = serdev_device_driver_register(&ssam_serial_hub);
823 if (status)
824 goto err_register;
825
826 return 0;
827
828err_register:
829 ssam_event_item_cache_destroy();
830err_evitem:
831 ssh_ctrl_packet_cache_destroy();
832err_cpkg:
833 ssam_bus_unregister();
834err_bus:
835 return status;
836}
837subsys_initcall(ssam_core_init);
838
839static void __exit ssam_core_exit(void)
840{
841 serdev_device_driver_unregister(&ssam_serial_hub);
842 ssam_event_item_cache_destroy();
843 ssh_ctrl_packet_cache_destroy();
844 ssam_bus_unregister();
845}
846module_exit(ssam_core_exit);
847
848MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
849MODULE_DESCRIPTION("Subsystem and Surface Serial Hub driver for Surface System Aggregator Module");
850MODULE_LICENSE("GPL");