1 // SPDX-License-Identifier: GPL-2.0+
3 * Surface Book (gen. 2 and later) detachment system (DTX) driver.
5 * Provides a user-space interface to properly handle clipboard/tablet
6 * (containing screen and processor) detachment from the base of the device
7 * (containing the keyboard and optionally a discrete GPU). Allows to
8 * acknowledge (to speed things up), abort (e.g. in case the dGPU is still in
9 * use), or request detachment via user-space.
15 #include <linux/input.h>
16 #include <linux/ioctl.h>
17 #include <linux/kernel.h>
18 #include <linux/kfifo.h>
19 #include <linux/kref.h>
20 #include <linux/miscdevice.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/poll.h>
25 #include <linux/rwsem.h>
26 #include <linux/slab.h>
27 #include <linux/workqueue.h>
29 #include <linux/surface_aggregator/controller.h>
30 #include <linux/surface_aggregator/device.h>
31 #include <linux/surface_aggregator/dtx.h>
34 /* -- SSAM interface. ------------------------------------------------------- */
36 enum sam_event_cid_bas {
37 SAM_EVENT_CID_DTX_CONNECTION = 0x0c,
38 SAM_EVENT_CID_DTX_REQUEST = 0x0e,
39 SAM_EVENT_CID_DTX_CANCEL = 0x0f,
40 SAM_EVENT_CID_DTX_LATCH_STATUS = 0x11,
43 enum ssam_bas_base_state {
44 SSAM_BAS_BASE_STATE_DETACH_SUCCESS = 0x00,
45 SSAM_BAS_BASE_STATE_ATTACHED = 0x01,
46 SSAM_BAS_BASE_STATE_NOT_FEASIBLE = 0x02,
49 enum ssam_bas_latch_status {
50 SSAM_BAS_LATCH_STATUS_CLOSED = 0x00,
51 SSAM_BAS_LATCH_STATUS_OPENED = 0x01,
52 SSAM_BAS_LATCH_STATUS_FAILED_TO_OPEN = 0x02,
53 SSAM_BAS_LATCH_STATUS_FAILED_TO_REMAIN_OPEN = 0x03,
54 SSAM_BAS_LATCH_STATUS_FAILED_TO_CLOSE = 0x04,
57 enum ssam_bas_cancel_reason {
58 SSAM_BAS_CANCEL_REASON_NOT_FEASIBLE = 0x00, /* Low battery. */
59 SSAM_BAS_CANCEL_REASON_TIMEOUT = 0x02,
60 SSAM_BAS_CANCEL_REASON_FAILED_TO_OPEN = 0x03,
61 SSAM_BAS_CANCEL_REASON_FAILED_TO_REMAIN_OPEN = 0x04,
62 SSAM_BAS_CANCEL_REASON_FAILED_TO_CLOSE = 0x05,
65 struct ssam_bas_base_info {
70 static_assert(sizeof(struct ssam_bas_base_info) == 2);
72 SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_lock, {
73 .target_category = SSAM_SSH_TC_BAS,
79 SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_unlock, {
80 .target_category = SSAM_SSH_TC_BAS,
86 SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_request, {
87 .target_category = SSAM_SSH_TC_BAS,
93 SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_confirm, {
94 .target_category = SSAM_SSH_TC_BAS,
100 SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_heartbeat, {
101 .target_category = SSAM_SSH_TC_BAS,
107 SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_cancel, {
108 .target_category = SSAM_SSH_TC_BAS,
114 SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_base, struct ssam_bas_base_info, {
115 .target_category = SSAM_SSH_TC_BAS,
121 SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_device_mode, u8, {
122 .target_category = SSAM_SSH_TC_BAS,
128 SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_latch_status, u8, {
129 .target_category = SSAM_SSH_TC_BAS,
136 /* -- Main structures. ------------------------------------------------------ */
138 enum sdtx_device_state {
139 SDTX_DEVICE_SHUTDOWN_BIT = BIT(0),
140 SDTX_DEVICE_DIRTY_BASE_BIT = BIT(1),
141 SDTX_DEVICE_DIRTY_MODE_BIT = BIT(2),
142 SDTX_DEVICE_DIRTY_LATCH_BIT = BIT(3),
147 struct rw_semaphore lock; /* Guards device and controller reference. */
150 struct ssam_controller *ctrl;
153 struct miscdevice mdev;
154 wait_queue_head_t waitq;
155 struct mutex write_lock; /* Guards order of events/notifications. */
156 struct rw_semaphore client_lock; /* Guards client list. */
157 struct list_head client_list;
159 struct delayed_work state_work;
161 struct ssam_bas_base_info base;
166 struct delayed_work mode_work;
167 struct input_dev *mode_switch;
169 struct ssam_event_notifier notif;
172 enum sdtx_client_state {
173 SDTX_CLIENT_EVENTS_ENABLED_BIT = BIT(0),
177 struct sdtx_device *ddev;
178 struct list_head node;
181 struct fasync_struct *fasync;
183 struct mutex read_lock; /* Guards FIFO buffer read access. */
184 DECLARE_KFIFO(buffer, u8, 512);
187 static void __sdtx_device_release(struct kref *kref)
189 struct sdtx_device *ddev = container_of(kref, struct sdtx_device, kref);
191 mutex_destroy(&ddev->write_lock);
195 static struct sdtx_device *sdtx_device_get(struct sdtx_device *ddev)
198 kref_get(&ddev->kref);
203 static void sdtx_device_put(struct sdtx_device *ddev)
206 kref_put(&ddev->kref, __sdtx_device_release);
210 /* -- Firmware value translations. ------------------------------------------ */
212 static u16 sdtx_translate_base_state(struct sdtx_device *ddev, u8 state)
215 case SSAM_BAS_BASE_STATE_ATTACHED:
216 return SDTX_BASE_ATTACHED;
218 case SSAM_BAS_BASE_STATE_DETACH_SUCCESS:
219 return SDTX_BASE_DETACHED;
221 case SSAM_BAS_BASE_STATE_NOT_FEASIBLE:
222 return SDTX_DETACH_NOT_FEASIBLE;
225 dev_err(ddev->dev, "unknown base state: %#04x\n", state);
226 return SDTX_UNKNOWN(state);
230 static u16 sdtx_translate_latch_status(struct sdtx_device *ddev, u8 status)
233 case SSAM_BAS_LATCH_STATUS_CLOSED:
234 return SDTX_LATCH_CLOSED;
236 case SSAM_BAS_LATCH_STATUS_OPENED:
237 return SDTX_LATCH_OPENED;
239 case SSAM_BAS_LATCH_STATUS_FAILED_TO_OPEN:
240 return SDTX_ERR_FAILED_TO_OPEN;
242 case SSAM_BAS_LATCH_STATUS_FAILED_TO_REMAIN_OPEN:
243 return SDTX_ERR_FAILED_TO_REMAIN_OPEN;
245 case SSAM_BAS_LATCH_STATUS_FAILED_TO_CLOSE:
246 return SDTX_ERR_FAILED_TO_CLOSE;
249 dev_err(ddev->dev, "unknown latch status: %#04x\n", status);
250 return SDTX_UNKNOWN(status);
254 static u16 sdtx_translate_cancel_reason(struct sdtx_device *ddev, u8 reason)
257 case SSAM_BAS_CANCEL_REASON_NOT_FEASIBLE:
258 return SDTX_DETACH_NOT_FEASIBLE;
260 case SSAM_BAS_CANCEL_REASON_TIMEOUT:
261 return SDTX_DETACH_TIMEDOUT;
263 case SSAM_BAS_CANCEL_REASON_FAILED_TO_OPEN:
264 return SDTX_ERR_FAILED_TO_OPEN;
266 case SSAM_BAS_CANCEL_REASON_FAILED_TO_REMAIN_OPEN:
267 return SDTX_ERR_FAILED_TO_REMAIN_OPEN;
269 case SSAM_BAS_CANCEL_REASON_FAILED_TO_CLOSE:
270 return SDTX_ERR_FAILED_TO_CLOSE;
273 dev_err(ddev->dev, "unknown cancel reason: %#04x\n", reason);
274 return SDTX_UNKNOWN(reason);
279 /* -- IOCTLs. --------------------------------------------------------------- */
281 static int sdtx_ioctl_get_base_info(struct sdtx_device *ddev,
282 struct sdtx_base_info __user *buf)
284 struct ssam_bas_base_info raw;
285 struct sdtx_base_info info;
288 lockdep_assert_held_read(&ddev->lock);
290 status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &raw);
294 info.state = sdtx_translate_base_state(ddev, raw.state);
295 info.base_id = SDTX_BASE_TYPE_SSH(raw.base_id);
297 if (copy_to_user(buf, &info, sizeof(info)))
303 static int sdtx_ioctl_get_device_mode(struct sdtx_device *ddev, u16 __user *buf)
308 lockdep_assert_held_read(&ddev->lock);
310 status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
314 return put_user(mode, buf);
317 static int sdtx_ioctl_get_latch_status(struct sdtx_device *ddev, u16 __user *buf)
322 lockdep_assert_held_read(&ddev->lock);
324 status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &latch);
328 return put_user(sdtx_translate_latch_status(ddev, latch), buf);
331 static long __surface_dtx_ioctl(struct sdtx_client *client, unsigned int cmd, unsigned long arg)
333 struct sdtx_device *ddev = client->ddev;
335 lockdep_assert_held_read(&ddev->lock);
338 case SDTX_IOCTL_EVENTS_ENABLE:
339 set_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags);
342 case SDTX_IOCTL_EVENTS_DISABLE:
343 clear_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags);
346 case SDTX_IOCTL_LATCH_LOCK:
347 return ssam_retry(ssam_bas_latch_lock, ddev->ctrl);
349 case SDTX_IOCTL_LATCH_UNLOCK:
350 return ssam_retry(ssam_bas_latch_unlock, ddev->ctrl);
352 case SDTX_IOCTL_LATCH_REQUEST:
353 return ssam_retry(ssam_bas_latch_request, ddev->ctrl);
355 case SDTX_IOCTL_LATCH_CONFIRM:
356 return ssam_retry(ssam_bas_latch_confirm, ddev->ctrl);
358 case SDTX_IOCTL_LATCH_HEARTBEAT:
359 return ssam_retry(ssam_bas_latch_heartbeat, ddev->ctrl);
361 case SDTX_IOCTL_LATCH_CANCEL:
362 return ssam_retry(ssam_bas_latch_cancel, ddev->ctrl);
364 case SDTX_IOCTL_GET_BASE_INFO:
365 return sdtx_ioctl_get_base_info(ddev, (struct sdtx_base_info __user *)arg);
367 case SDTX_IOCTL_GET_DEVICE_MODE:
368 return sdtx_ioctl_get_device_mode(ddev, (u16 __user *)arg);
370 case SDTX_IOCTL_GET_LATCH_STATUS:
371 return sdtx_ioctl_get_latch_status(ddev, (u16 __user *)arg);
378 static long surface_dtx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
380 struct sdtx_client *client = file->private_data;
383 if (down_read_killable(&client->ddev->lock))
386 if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &client->ddev->flags)) {
387 up_read(&client->ddev->lock);
391 status = __surface_dtx_ioctl(client, cmd, arg);
393 up_read(&client->ddev->lock);
398 /* -- File operations. ------------------------------------------------------ */
400 static int surface_dtx_open(struct inode *inode, struct file *file)
402 struct sdtx_device *ddev = container_of(file->private_data, struct sdtx_device, mdev);
403 struct sdtx_client *client;
405 /* Initialize client. */
406 client = kzalloc(sizeof(*client), GFP_KERNEL);
410 client->ddev = sdtx_device_get(ddev);
412 INIT_LIST_HEAD(&client->node);
414 mutex_init(&client->read_lock);
415 INIT_KFIFO(client->buffer);
417 file->private_data = client;
420 down_write(&ddev->client_lock);
423 * Do not add a new client if the device has been shut down. Note that
424 * it's enough to hold the client_lock here as, during shutdown, we
425 * only acquire that lock and remove clients after marking the device
428 if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
429 up_write(&ddev->client_lock);
430 mutex_destroy(&client->read_lock);
431 sdtx_device_put(client->ddev);
436 list_add_tail(&client->node, &ddev->client_list);
437 up_write(&ddev->client_lock);
439 stream_open(inode, file);
443 static int surface_dtx_release(struct inode *inode, struct file *file)
445 struct sdtx_client *client = file->private_data;
448 down_write(&client->ddev->client_lock);
449 list_del(&client->node);
450 up_write(&client->ddev->client_lock);
453 sdtx_device_put(client->ddev);
454 mutex_destroy(&client->read_lock);
460 static ssize_t surface_dtx_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
462 struct sdtx_client *client = file->private_data;
463 struct sdtx_device *ddev = client->ddev;
467 if (down_read_killable(&ddev->lock))
470 /* Make sure we're not shut down. */
471 if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
472 up_read(&ddev->lock);
477 /* Check availability, wait if necessary. */
478 if (kfifo_is_empty(&client->buffer)) {
479 up_read(&ddev->lock);
481 if (file->f_flags & O_NONBLOCK)
484 status = wait_event_interruptible(ddev->waitq,
485 !kfifo_is_empty(&client->buffer) ||
486 test_bit(SDTX_DEVICE_SHUTDOWN_BIT,
491 if (down_read_killable(&ddev->lock))
494 /* Need to check that we're not shut down again. */
495 if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
496 up_read(&ddev->lock);
501 /* Try to read from FIFO. */
502 if (mutex_lock_interruptible(&client->read_lock)) {
503 up_read(&ddev->lock);
507 status = kfifo_to_user(&client->buffer, buf, count, &copied);
508 mutex_unlock(&client->read_lock);
511 up_read(&ddev->lock);
515 /* We might not have gotten anything, check this here. */
516 if (copied == 0 && (file->f_flags & O_NONBLOCK)) {
517 up_read(&ddev->lock);
520 } while (copied == 0);
522 up_read(&ddev->lock);
526 static __poll_t surface_dtx_poll(struct file *file, struct poll_table_struct *pt)
528 struct sdtx_client *client = file->private_data;
531 if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &client->ddev->flags))
532 return EPOLLHUP | EPOLLERR;
534 poll_wait(file, &client->ddev->waitq, pt);
536 if (!kfifo_is_empty(&client->buffer))
537 events |= EPOLLIN | EPOLLRDNORM;
542 static int surface_dtx_fasync(int fd, struct file *file, int on)
544 struct sdtx_client *client = file->private_data;
546 return fasync_helper(fd, file, on, &client->fasync);
549 static const struct file_operations surface_dtx_fops = {
550 .owner = THIS_MODULE,
551 .open = surface_dtx_open,
552 .release = surface_dtx_release,
553 .read = surface_dtx_read,
554 .poll = surface_dtx_poll,
555 .fasync = surface_dtx_fasync,
556 .unlocked_ioctl = surface_dtx_ioctl,
557 .compat_ioctl = surface_dtx_ioctl,
562 /* -- Event handling/forwarding. -------------------------------------------- */
565 * The device operation mode is not immediately updated on the EC when the
566 * base has been connected, i.e. querying the device mode inside the
567 * connection event callback yields an outdated value. Thus, we can only
568 * determine the new tablet-mode switch and device mode values after some
571 * These delays have been chosen by experimenting. We first delay on connect
572 * events, then check and validate the device mode against the base state and
573 * if invalid delay again by the "recheck" delay.
575 #define SDTX_DEVICE_MODE_DELAY_CONNECT msecs_to_jiffies(100)
576 #define SDTX_DEVICE_MODE_DELAY_RECHECK msecs_to_jiffies(100)
578 struct sdtx_status_event {
583 struct sdtx_base_info_event {
585 struct sdtx_base_info v;
588 union sdtx_generic_event {
589 struct sdtx_event common;
590 struct sdtx_status_event status;
591 struct sdtx_base_info_event base;
594 static void sdtx_update_device_mode(struct sdtx_device *ddev, unsigned long delay);
596 /* Must be executed with ddev->write_lock held. */
597 static void sdtx_push_event(struct sdtx_device *ddev, struct sdtx_event *evt)
599 const size_t len = sizeof(struct sdtx_event) + evt->length;
600 struct sdtx_client *client;
602 lockdep_assert_held(&ddev->write_lock);
604 down_read(&ddev->client_lock);
605 list_for_each_entry(client, &ddev->client_list, node) {
606 if (!test_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags))
609 if (likely(kfifo_avail(&client->buffer) >= len))
610 kfifo_in(&client->buffer, (const u8 *)evt, len);
612 dev_warn(ddev->dev, "event buffer overrun\n");
614 kill_fasync(&client->fasync, SIGIO, POLL_IN);
616 up_read(&ddev->client_lock);
618 wake_up_interruptible(&ddev->waitq);
621 static u32 sdtx_notifier(struct ssam_event_notifier *nf, const struct ssam_event *in)
623 struct sdtx_device *ddev = container_of(nf, struct sdtx_device, notif);
624 union sdtx_generic_event event;
627 /* Validate event payload length. */
628 switch (in->command_id) {
629 case SAM_EVENT_CID_DTX_CONNECTION:
630 len = 2 * sizeof(u8);
633 case SAM_EVENT_CID_DTX_REQUEST:
637 case SAM_EVENT_CID_DTX_CANCEL:
641 case SAM_EVENT_CID_DTX_LATCH_STATUS:
649 if (in->length != len) {
651 "unexpected payload size for event %#04x: got %u, expected %zu\n",
652 in->command_id, in->length, len);
656 mutex_lock(&ddev->write_lock);
658 /* Translate event. */
659 switch (in->command_id) {
660 case SAM_EVENT_CID_DTX_CONNECTION:
661 clear_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags);
663 /* If state has not changed: do not send new event. */
664 if (ddev->state.base.state == in->data[0] &&
665 ddev->state.base.base_id == in->data[1])
668 ddev->state.base.state = in->data[0];
669 ddev->state.base.base_id = in->data[1];
671 event.base.e.length = sizeof(struct sdtx_base_info);
672 event.base.e.code = SDTX_EVENT_BASE_CONNECTION;
673 event.base.v.state = sdtx_translate_base_state(ddev, in->data[0]);
674 event.base.v.base_id = SDTX_BASE_TYPE_SSH(in->data[1]);
677 case SAM_EVENT_CID_DTX_REQUEST:
678 event.common.code = SDTX_EVENT_REQUEST;
679 event.common.length = 0;
682 case SAM_EVENT_CID_DTX_CANCEL:
683 event.status.e.length = sizeof(u16);
684 event.status.e.code = SDTX_EVENT_CANCEL;
685 event.status.v = sdtx_translate_cancel_reason(ddev, in->data[0]);
688 case SAM_EVENT_CID_DTX_LATCH_STATUS:
689 clear_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags);
691 /* If state has not changed: do not send new event. */
692 if (ddev->state.latch_status == in->data[0])
695 ddev->state.latch_status = in->data[0];
697 event.status.e.length = sizeof(u16);
698 event.status.e.code = SDTX_EVENT_LATCH_STATUS;
699 event.status.v = sdtx_translate_latch_status(ddev, in->data[0]);
703 sdtx_push_event(ddev, &event.common);
705 /* Update device mode on base connection change. */
706 if (in->command_id == SAM_EVENT_CID_DTX_CONNECTION) {
709 delay = in->data[0] ? SDTX_DEVICE_MODE_DELAY_CONNECT : 0;
710 sdtx_update_device_mode(ddev, delay);
714 mutex_unlock(&ddev->write_lock);
715 return SSAM_NOTIF_HANDLED;
719 /* -- State update functions. ----------------------------------------------- */
721 static bool sdtx_device_mode_invalid(u8 mode, u8 base_state)
723 return ((base_state == SSAM_BAS_BASE_STATE_ATTACHED) &&
724 (mode == SDTX_DEVICE_MODE_TABLET)) ||
725 ((base_state == SSAM_BAS_BASE_STATE_DETACH_SUCCESS) &&
726 (mode != SDTX_DEVICE_MODE_TABLET));
729 static void sdtx_device_mode_workfn(struct work_struct *work)
731 struct sdtx_device *ddev = container_of(work, struct sdtx_device, mode_work.work);
732 struct sdtx_status_event event;
733 struct ssam_bas_base_info base;
737 /* Get operation mode. */
738 status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
740 dev_err(ddev->dev, "failed to get device mode: %d\n", status);
745 status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &base);
747 dev_err(ddev->dev, "failed to get base info: %d\n", status);
752 * In some cases (specifically when attaching the base), the device
753 * mode isn't updated right away. Thus we check if the device mode
754 * makes sense for the given base state and try again later if it
757 if (sdtx_device_mode_invalid(mode, base.state)) {
758 dev_dbg(ddev->dev, "device mode is invalid, trying again\n");
759 sdtx_update_device_mode(ddev, SDTX_DEVICE_MODE_DELAY_RECHECK);
763 mutex_lock(&ddev->write_lock);
764 clear_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags);
766 /* Avoid sending duplicate device-mode events. */
767 if (ddev->state.device_mode == mode) {
768 mutex_unlock(&ddev->write_lock);
772 ddev->state.device_mode = mode;
774 event.e.length = sizeof(u16);
775 event.e.code = SDTX_EVENT_DEVICE_MODE;
778 sdtx_push_event(ddev, &event.e);
780 /* Send SW_TABLET_MODE event. */
781 tablet = mode != SDTX_DEVICE_MODE_LAPTOP;
782 input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet);
783 input_sync(ddev->mode_switch);
785 mutex_unlock(&ddev->write_lock);
788 static void sdtx_update_device_mode(struct sdtx_device *ddev, unsigned long delay)
790 schedule_delayed_work(&ddev->mode_work, delay);
793 /* Must be executed with ddev->write_lock held. */
794 static void __sdtx_device_state_update_base(struct sdtx_device *ddev,
795 struct ssam_bas_base_info info)
797 struct sdtx_base_info_event event;
799 lockdep_assert_held(&ddev->write_lock);
801 /* Prevent duplicate events. */
802 if (ddev->state.base.state == info.state &&
803 ddev->state.base.base_id == info.base_id)
806 ddev->state.base = info;
808 event.e.length = sizeof(struct sdtx_base_info);
809 event.e.code = SDTX_EVENT_BASE_CONNECTION;
810 event.v.state = sdtx_translate_base_state(ddev, info.state);
811 event.v.base_id = SDTX_BASE_TYPE_SSH(info.base_id);
813 sdtx_push_event(ddev, &event.e);
816 /* Must be executed with ddev->write_lock held. */
817 static void __sdtx_device_state_update_mode(struct sdtx_device *ddev, u8 mode)
819 struct sdtx_status_event event;
823 * Note: This function must be called after updating the base state
824 * via __sdtx_device_state_update_base(), as we rely on the updated
825 * base state value in the validity check below.
828 lockdep_assert_held(&ddev->write_lock);
830 if (sdtx_device_mode_invalid(mode, ddev->state.base.state)) {
831 dev_dbg(ddev->dev, "device mode is invalid, trying again\n");
832 sdtx_update_device_mode(ddev, SDTX_DEVICE_MODE_DELAY_RECHECK);
836 /* Prevent duplicate events. */
837 if (ddev->state.device_mode == mode)
840 ddev->state.device_mode = mode;
843 event.e.length = sizeof(u16);
844 event.e.code = SDTX_EVENT_DEVICE_MODE;
847 sdtx_push_event(ddev, &event.e);
849 /* Send SW_TABLET_MODE event. */
850 tablet = mode != SDTX_DEVICE_MODE_LAPTOP;
851 input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet);
852 input_sync(ddev->mode_switch);
855 /* Must be executed with ddev->write_lock held. */
856 static void __sdtx_device_state_update_latch(struct sdtx_device *ddev, u8 status)
858 struct sdtx_status_event event;
860 lockdep_assert_held(&ddev->write_lock);
862 /* Prevent duplicate events. */
863 if (ddev->state.latch_status == status)
866 ddev->state.latch_status = status;
868 event.e.length = sizeof(struct sdtx_base_info);
869 event.e.code = SDTX_EVENT_BASE_CONNECTION;
870 event.v = sdtx_translate_latch_status(ddev, status);
872 sdtx_push_event(ddev, &event.e);
875 static void sdtx_device_state_workfn(struct work_struct *work)
877 struct sdtx_device *ddev = container_of(work, struct sdtx_device, state_work.work);
878 struct ssam_bas_base_info base;
882 /* Mark everything as dirty. */
883 set_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags);
884 set_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags);
885 set_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags);
888 * Ensure that the state gets marked as dirty before continuing to
889 * query it. Necessary to ensure that clear_bit() calls in
890 * sdtx_notifier() and sdtx_device_mode_workfn() actually clear these
891 * bits if an event is received while updating the state here.
893 smp_mb__after_atomic();
895 status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &base);
897 dev_err(ddev->dev, "failed to get base state: %d\n", status);
901 status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
903 dev_err(ddev->dev, "failed to get device mode: %d\n", status);
907 status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &latch);
909 dev_err(ddev->dev, "failed to get latch status: %d\n", status);
913 mutex_lock(&ddev->write_lock);
916 * If the respective dirty-bit has been cleared, an event has been
917 * received, updating this state. The queried state may thus be out of
918 * date. At this point, we can safely assume that the state provided
919 * by the event is either up to date, or we're about to receive
920 * another event updating it.
923 if (test_and_clear_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags))
924 __sdtx_device_state_update_base(ddev, base);
926 if (test_and_clear_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags))
927 __sdtx_device_state_update_mode(ddev, mode);
929 if (test_and_clear_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags))
930 __sdtx_device_state_update_latch(ddev, latch);
932 mutex_unlock(&ddev->write_lock);
935 static void sdtx_update_device_state(struct sdtx_device *ddev, unsigned long delay)
937 schedule_delayed_work(&ddev->state_work, delay);
941 /* -- Common device initialization. ----------------------------------------- */
943 static int sdtx_device_init(struct sdtx_device *ddev, struct device *dev,
944 struct ssam_controller *ctrl)
946 int status, tablet_mode;
948 /* Basic initialization. */
949 kref_init(&ddev->kref);
950 init_rwsem(&ddev->lock);
954 ddev->mdev.minor = MISC_DYNAMIC_MINOR;
955 ddev->mdev.name = "surface_dtx";
956 ddev->mdev.nodename = "surface/dtx";
957 ddev->mdev.fops = &surface_dtx_fops;
959 ddev->notif.base.priority = 1;
960 ddev->notif.base.fn = sdtx_notifier;
961 ddev->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
962 ddev->notif.event.id.target_category = SSAM_SSH_TC_BAS;
963 ddev->notif.event.id.instance = 0;
964 ddev->notif.event.mask = SSAM_EVENT_MASK_NONE;
965 ddev->notif.event.flags = SSAM_EVENT_SEQUENCED;
967 init_waitqueue_head(&ddev->waitq);
968 mutex_init(&ddev->write_lock);
969 init_rwsem(&ddev->client_lock);
970 INIT_LIST_HEAD(&ddev->client_list);
972 INIT_DELAYED_WORK(&ddev->mode_work, sdtx_device_mode_workfn);
973 INIT_DELAYED_WORK(&ddev->state_work, sdtx_device_state_workfn);
976 * Get current device state. We want to guarantee that events are only
977 * sent when state actually changes. Thus we cannot use special
978 * "uninitialized" values, as that would cause problems when manually
979 * querying the state in surface_dtx_pm_complete(). I.e. we would not
980 * be able to detect state changes there if no change event has been
981 * received between driver initialization and first device suspension.
983 * Note that we also need to do this before registering the event
984 * notifier, as that may access the state values.
986 status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &ddev->state.base);
990 status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &ddev->state.device_mode);
994 status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &ddev->state.latch_status);
998 /* Set up tablet mode switch. */
999 ddev->mode_switch = input_allocate_device();
1000 if (!ddev->mode_switch)
1003 ddev->mode_switch->name = "Microsoft Surface DTX Device Mode Switch";
1004 ddev->mode_switch->phys = "ssam/01:11:01:00:00/input0";
1005 ddev->mode_switch->id.bustype = BUS_HOST;
1006 ddev->mode_switch->dev.parent = ddev->dev;
1008 tablet_mode = (ddev->state.device_mode != SDTX_DEVICE_MODE_LAPTOP);
1009 input_set_capability(ddev->mode_switch, EV_SW, SW_TABLET_MODE);
1010 input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet_mode);
1012 status = input_register_device(ddev->mode_switch);
1014 input_free_device(ddev->mode_switch);
1018 /* Set up event notifier. */
1019 status = ssam_notifier_register(ddev->ctrl, &ddev->notif);
1023 /* Register miscdevice. */
1024 status = misc_register(&ddev->mdev);
1029 * Update device state in case it has changed between getting the
1030 * initial mode and registering the event notifier.
1032 sdtx_update_device_state(ddev, 0);
1036 ssam_notifier_unregister(ddev->ctrl, &ddev->notif);
1037 cancel_delayed_work_sync(&ddev->mode_work);
1039 input_unregister_device(ddev->mode_switch);
1043 static struct sdtx_device *sdtx_device_create(struct device *dev, struct ssam_controller *ctrl)
1045 struct sdtx_device *ddev;
1048 ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
1050 return ERR_PTR(-ENOMEM);
1052 status = sdtx_device_init(ddev, dev, ctrl);
1054 sdtx_device_put(ddev);
1055 return ERR_PTR(status);
1061 static void sdtx_device_destroy(struct sdtx_device *ddev)
1063 struct sdtx_client *client;
1066 * Mark device as shut-down. Prevent new clients from being added and
1067 * new operations from being executed.
1069 set_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags);
1071 /* Disable notifiers, prevent new events from arriving. */
1072 ssam_notifier_unregister(ddev->ctrl, &ddev->notif);
1074 /* Stop mode_work, prevent access to mode_switch. */
1075 cancel_delayed_work_sync(&ddev->mode_work);
1077 /* Stop state_work. */
1078 cancel_delayed_work_sync(&ddev->state_work);
1080 /* With mode_work canceled, we can unregister the mode_switch. */
1081 input_unregister_device(ddev->mode_switch);
1083 /* Wake up async clients. */
1084 down_write(&ddev->client_lock);
1085 list_for_each_entry(client, &ddev->client_list, node) {
1086 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
1088 up_write(&ddev->client_lock);
1090 /* Wake up blocking clients. */
1091 wake_up_interruptible(&ddev->waitq);
1094 * Wait for clients to finish their current operation. After this, the
1095 * controller and device references are guaranteed to be no longer in
1098 down_write(&ddev->lock);
1101 up_write(&ddev->lock);
1103 /* Finally remove the misc-device. */
1104 misc_deregister(&ddev->mdev);
1107 * We're now guaranteed that sdtx_device_open() won't be called any
1108 * more, so we can now drop out reference.
1110 sdtx_device_put(ddev);
1114 /* -- PM ops. --------------------------------------------------------------- */
1116 #ifdef CONFIG_PM_SLEEP
1118 static void surface_dtx_pm_complete(struct device *dev)
1120 struct sdtx_device *ddev = dev_get_drvdata(dev);
1123 * Normally, the EC will store events while suspended (i.e. in
1124 * display-off state) and release them when resumed (i.e. transitioned
1125 * to display-on state). During hibernation, however, the EC will be
1126 * shut down and does not store events. Furthermore, events might be
1127 * dropped during prolonged suspension (it is currently unknown how
1128 * big this event buffer is and how it behaves on overruns).
1130 * To prevent any problems, we update the device state here. We do
1131 * this delayed to ensure that any events sent by the EC directly
1132 * after resuming will be handled first. The delay below has been
1133 * chosen (experimentally), so that there should be ample time for
1134 * these events to be handled, before we check and, if necessary,
1137 sdtx_update_device_state(ddev, msecs_to_jiffies(1000));
1140 static const struct dev_pm_ops surface_dtx_pm_ops = {
1141 .complete = surface_dtx_pm_complete,
1144 #else /* CONFIG_PM_SLEEP */
1146 static const struct dev_pm_ops surface_dtx_pm_ops = {};
1148 #endif /* CONFIG_PM_SLEEP */
1151 /* -- Platform driver. ------------------------------------------------------ */
1153 static int surface_dtx_platform_probe(struct platform_device *pdev)
1155 struct ssam_controller *ctrl;
1156 struct sdtx_device *ddev;
1159 ctrl = ssam_client_bind(&pdev->dev);
1161 return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
1163 ddev = sdtx_device_create(&pdev->dev, ctrl);
1165 return PTR_ERR(ddev);
1167 platform_set_drvdata(pdev, ddev);
1171 static int surface_dtx_platform_remove(struct platform_device *pdev)
1173 sdtx_device_destroy(platform_get_drvdata(pdev));
1177 static const struct acpi_device_id surface_dtx_acpi_match[] = {
1181 MODULE_DEVICE_TABLE(acpi, surface_dtx_acpi_match);
1183 static struct platform_driver surface_dtx_platform_driver = {
1184 .probe = surface_dtx_platform_probe,
1185 .remove = surface_dtx_platform_remove,
1187 .name = "surface_dtx_pltf",
1188 .acpi_match_table = surface_dtx_acpi_match,
1189 .pm = &surface_dtx_pm_ops,
1190 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1195 /* -- SSAM device driver. --------------------------------------------------- */
1197 #ifdef CONFIG_SURFACE_AGGREGATOR_BUS
1199 static int surface_dtx_ssam_probe(struct ssam_device *sdev)
1201 struct sdtx_device *ddev;
1203 ddev = sdtx_device_create(&sdev->dev, sdev->ctrl);
1205 return PTR_ERR(ddev);
1207 ssam_device_set_drvdata(sdev, ddev);
1211 static void surface_dtx_ssam_remove(struct ssam_device *sdev)
1213 sdtx_device_destroy(ssam_device_get_drvdata(sdev));
1216 static const struct ssam_device_id surface_dtx_ssam_match[] = {
1217 { SSAM_SDEV(BAS, 0x01, 0x00, 0x00) },
1220 MODULE_DEVICE_TABLE(ssam, surface_dtx_ssam_match);
1222 static struct ssam_device_driver surface_dtx_ssam_driver = {
1223 .probe = surface_dtx_ssam_probe,
1224 .remove = surface_dtx_ssam_remove,
1225 .match_table = surface_dtx_ssam_match,
1227 .name = "surface_dtx",
1228 .pm = &surface_dtx_pm_ops,
1229 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1233 static int ssam_dtx_driver_register(void)
1235 return ssam_device_driver_register(&surface_dtx_ssam_driver);
1238 static void ssam_dtx_driver_unregister(void)
1240 ssam_device_driver_unregister(&surface_dtx_ssam_driver);
1243 #else /* CONFIG_SURFACE_AGGREGATOR_BUS */
1245 static int ssam_dtx_driver_register(void)
1250 static void ssam_dtx_driver_unregister(void)
1254 #endif /* CONFIG_SURFACE_AGGREGATOR_BUS */
1257 /* -- Module setup. --------------------------------------------------------- */
1259 static int __init surface_dtx_init(void)
1263 status = ssam_dtx_driver_register();
1267 status = platform_driver_register(&surface_dtx_platform_driver);
1269 ssam_dtx_driver_unregister();
1273 module_init(surface_dtx_init);
1275 static void __exit surface_dtx_exit(void)
1277 platform_driver_unregister(&surface_dtx_platform_driver);
1278 ssam_dtx_driver_unregister();
1280 module_exit(surface_dtx_exit);
1283 MODULE_DESCRIPTION("Detachment-system driver for Surface System Aggregator Module");
1284 MODULE_LICENSE("GPL");