Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Surface System Aggregator Module (SSAM) tablet mode switch driver.
0004  *
0005  * Copyright (C) 2022 Maximilian Luz <luzmaximilian@gmail.com>
0006  */
0007 
0008 #include <asm/unaligned.h>
0009 #include <linux/input.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/types.h>
0013 #include <linux/workqueue.h>
0014 
0015 #include <linux/surface_aggregator/controller.h>
0016 #include <linux/surface_aggregator/device.h>
0017 
0018 
0019 /* -- SSAM generic tablet switch driver framework. -------------------------- */
0020 
0021 struct ssam_tablet_sw;
0022 
0023 struct ssam_tablet_sw_ops {
0024     int (*get_state)(struct ssam_tablet_sw *sw, u32 *state);
0025     const char *(*state_name)(struct ssam_tablet_sw *sw, u32 state);
0026     bool (*state_is_tablet_mode)(struct ssam_tablet_sw *sw, u32 state);
0027 };
0028 
0029 struct ssam_tablet_sw {
0030     struct ssam_device *sdev;
0031 
0032     u32 state;
0033     struct work_struct update_work;
0034     struct input_dev *mode_switch;
0035 
0036     struct ssam_tablet_sw_ops ops;
0037     struct ssam_event_notifier notif;
0038 };
0039 
0040 struct ssam_tablet_sw_desc {
0041     struct {
0042         const char *name;
0043         const char *phys;
0044     } dev;
0045 
0046     struct {
0047         u32 (*notify)(struct ssam_event_notifier *nf, const struct ssam_event *event);
0048         int (*get_state)(struct ssam_tablet_sw *sw, u32 *state);
0049         const char *(*state_name)(struct ssam_tablet_sw *sw, u32 state);
0050         bool (*state_is_tablet_mode)(struct ssam_tablet_sw *sw, u32 state);
0051     } ops;
0052 
0053     struct {
0054         struct ssam_event_registry reg;
0055         struct ssam_event_id id;
0056         enum ssam_event_mask mask;
0057         u8 flags;
0058     } event;
0059 };
0060 
0061 static ssize_t state_show(struct device *dev, struct device_attribute *attr, char *buf)
0062 {
0063     struct ssam_tablet_sw *sw = dev_get_drvdata(dev);
0064     const char *state = sw->ops.state_name(sw, sw->state);
0065 
0066     return sysfs_emit(buf, "%s\n", state);
0067 }
0068 static DEVICE_ATTR_RO(state);
0069 
0070 static struct attribute *ssam_tablet_sw_attrs[] = {
0071     &dev_attr_state.attr,
0072     NULL,
0073 };
0074 
0075 static const struct attribute_group ssam_tablet_sw_group = {
0076     .attrs = ssam_tablet_sw_attrs,
0077 };
0078 
0079 static void ssam_tablet_sw_update_workfn(struct work_struct *work)
0080 {
0081     struct ssam_tablet_sw *sw = container_of(work, struct ssam_tablet_sw, update_work);
0082     int tablet, status;
0083     u32 state;
0084 
0085     status = sw->ops.get_state(sw, &state);
0086     if (status)
0087         return;
0088 
0089     if (sw->state == state)
0090         return;
0091     sw->state = state;
0092 
0093     /* Send SW_TABLET_MODE event. */
0094     tablet = sw->ops.state_is_tablet_mode(sw, state);
0095     input_report_switch(sw->mode_switch, SW_TABLET_MODE, tablet);
0096     input_sync(sw->mode_switch);
0097 }
0098 
0099 static int __maybe_unused ssam_tablet_sw_resume(struct device *dev)
0100 {
0101     struct ssam_tablet_sw *sw = dev_get_drvdata(dev);
0102 
0103     schedule_work(&sw->update_work);
0104     return 0;
0105 }
0106 static SIMPLE_DEV_PM_OPS(ssam_tablet_sw_pm_ops, NULL, ssam_tablet_sw_resume);
0107 
0108 static int ssam_tablet_sw_probe(struct ssam_device *sdev)
0109 {
0110     const struct ssam_tablet_sw_desc *desc;
0111     struct ssam_tablet_sw *sw;
0112     int tablet, status;
0113 
0114     desc = ssam_device_get_match_data(sdev);
0115     if (!desc) {
0116         WARN(1, "no driver match data specified");
0117         return -EINVAL;
0118     }
0119 
0120     sw = devm_kzalloc(&sdev->dev, sizeof(*sw), GFP_KERNEL);
0121     if (!sw)
0122         return -ENOMEM;
0123 
0124     sw->sdev = sdev;
0125 
0126     sw->ops.get_state = desc->ops.get_state;
0127     sw->ops.state_name = desc->ops.state_name;
0128     sw->ops.state_is_tablet_mode = desc->ops.state_is_tablet_mode;
0129 
0130     INIT_WORK(&sw->update_work, ssam_tablet_sw_update_workfn);
0131 
0132     ssam_device_set_drvdata(sdev, sw);
0133 
0134     /* Get initial state. */
0135     status = sw->ops.get_state(sw, &sw->state);
0136     if (status)
0137         return status;
0138 
0139     /* Set up tablet mode switch. */
0140     sw->mode_switch = devm_input_allocate_device(&sdev->dev);
0141     if (!sw->mode_switch)
0142         return -ENOMEM;
0143 
0144     sw->mode_switch->name = desc->dev.name;
0145     sw->mode_switch->phys = desc->dev.phys;
0146     sw->mode_switch->id.bustype = BUS_HOST;
0147     sw->mode_switch->dev.parent = &sdev->dev;
0148 
0149     tablet = sw->ops.state_is_tablet_mode(sw, sw->state);
0150     input_set_capability(sw->mode_switch, EV_SW, SW_TABLET_MODE);
0151     input_report_switch(sw->mode_switch, SW_TABLET_MODE, tablet);
0152 
0153     status = input_register_device(sw->mode_switch);
0154     if (status)
0155         return status;
0156 
0157     /* Set up notifier. */
0158     sw->notif.base.priority = 0;
0159     sw->notif.base.fn = desc->ops.notify;
0160     sw->notif.event.reg = desc->event.reg;
0161     sw->notif.event.id = desc->event.id;
0162     sw->notif.event.mask = desc->event.mask;
0163     sw->notif.event.flags = SSAM_EVENT_SEQUENCED;
0164 
0165     status = ssam_device_notifier_register(sdev, &sw->notif);
0166     if (status)
0167         return status;
0168 
0169     status = sysfs_create_group(&sdev->dev.kobj, &ssam_tablet_sw_group);
0170     if (status)
0171         goto err;
0172 
0173     /* We might have missed events during setup, so check again. */
0174     schedule_work(&sw->update_work);
0175     return 0;
0176 
0177 err:
0178     ssam_device_notifier_unregister(sdev, &sw->notif);
0179     cancel_work_sync(&sw->update_work);
0180     return status;
0181 }
0182 
0183 static void ssam_tablet_sw_remove(struct ssam_device *sdev)
0184 {
0185     struct ssam_tablet_sw *sw = ssam_device_get_drvdata(sdev);
0186 
0187     sysfs_remove_group(&sdev->dev.kobj, &ssam_tablet_sw_group);
0188 
0189     ssam_device_notifier_unregister(sdev, &sw->notif);
0190     cancel_work_sync(&sw->update_work);
0191 }
0192 
0193 
0194 /* -- SSAM KIP tablet switch implementation. -------------------------------- */
0195 
0196 #define SSAM_EVENT_KIP_CID_COVER_STATE_CHANGED  0x1d
0197 
0198 enum ssam_kip_cover_state {
0199     SSAM_KIP_COVER_STATE_DISCONNECTED  = 0x01,
0200     SSAM_KIP_COVER_STATE_CLOSED        = 0x02,
0201     SSAM_KIP_COVER_STATE_LAPTOP        = 0x03,
0202     SSAM_KIP_COVER_STATE_FOLDED_CANVAS = 0x04,
0203     SSAM_KIP_COVER_STATE_FOLDED_BACK   = 0x05,
0204 };
0205 
0206 static const char *ssam_kip_cover_state_name(struct ssam_tablet_sw *sw, u32 state)
0207 {
0208     switch (state) {
0209     case SSAM_KIP_COVER_STATE_DISCONNECTED:
0210         return "disconnected";
0211 
0212     case SSAM_KIP_COVER_STATE_CLOSED:
0213         return "closed";
0214 
0215     case SSAM_KIP_COVER_STATE_LAPTOP:
0216         return "laptop";
0217 
0218     case SSAM_KIP_COVER_STATE_FOLDED_CANVAS:
0219         return "folded-canvas";
0220 
0221     case SSAM_KIP_COVER_STATE_FOLDED_BACK:
0222         return "folded-back";
0223 
0224     default:
0225         dev_warn(&sw->sdev->dev, "unknown KIP cover state: %u\n", state);
0226         return "<unknown>";
0227     }
0228 }
0229 
0230 static bool ssam_kip_cover_state_is_tablet_mode(struct ssam_tablet_sw *sw, u32 state)
0231 {
0232     switch (state) {
0233     case SSAM_KIP_COVER_STATE_DISCONNECTED:
0234     case SSAM_KIP_COVER_STATE_FOLDED_CANVAS:
0235     case SSAM_KIP_COVER_STATE_FOLDED_BACK:
0236         return true;
0237 
0238     case SSAM_KIP_COVER_STATE_CLOSED:
0239     case SSAM_KIP_COVER_STATE_LAPTOP:
0240         return false;
0241 
0242     default:
0243         dev_warn(&sw->sdev->dev, "unknown KIP cover state: %d\n", sw->state);
0244         return true;
0245     }
0246 }
0247 
0248 SSAM_DEFINE_SYNC_REQUEST_R(__ssam_kip_get_cover_state, u8, {
0249     .target_category = SSAM_SSH_TC_KIP,
0250     .target_id       = 0x01,
0251     .command_id      = 0x1d,
0252     .instance_id     = 0x00,
0253 });
0254 
0255 static int ssam_kip_get_cover_state(struct ssam_tablet_sw *sw, u32 *state)
0256 {
0257     int status;
0258     u8 raw;
0259 
0260     status = ssam_retry(__ssam_kip_get_cover_state, sw->sdev->ctrl, &raw);
0261     if (status < 0) {
0262         dev_err(&sw->sdev->dev, "failed to query KIP lid state: %d\n", status);
0263         return status;
0264     }
0265 
0266     *state = raw;
0267     return 0;
0268 }
0269 
0270 static u32 ssam_kip_sw_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
0271 {
0272     struct ssam_tablet_sw *sw = container_of(nf, struct ssam_tablet_sw, notif);
0273 
0274     if (event->command_id != SSAM_EVENT_KIP_CID_COVER_STATE_CHANGED)
0275         return 0;   /* Return "unhandled". */
0276 
0277     if (event->length < 1)
0278         dev_warn(&sw->sdev->dev, "unexpected payload size: %u\n", event->length);
0279 
0280     schedule_work(&sw->update_work);
0281     return SSAM_NOTIF_HANDLED;
0282 }
0283 
0284 static const struct ssam_tablet_sw_desc ssam_kip_sw_desc = {
0285     .dev = {
0286         .name = "Microsoft Surface KIP Tablet Mode Switch",
0287         .phys = "ssam/01:0e:01:00:01/input0",
0288     },
0289     .ops = {
0290         .notify = ssam_kip_sw_notif,
0291         .get_state = ssam_kip_get_cover_state,
0292         .state_name = ssam_kip_cover_state_name,
0293         .state_is_tablet_mode = ssam_kip_cover_state_is_tablet_mode,
0294     },
0295     .event = {
0296         .reg = SSAM_EVENT_REGISTRY_SAM,
0297         .id = {
0298             .target_category = SSAM_SSH_TC_KIP,
0299             .instance = 0,
0300         },
0301         .mask = SSAM_EVENT_MASK_TARGET,
0302     },
0303 };
0304 
0305 
0306 /* -- SSAM POS tablet switch implementation. -------------------------------- */
0307 
0308 static bool tablet_mode_in_slate_state = true;
0309 module_param(tablet_mode_in_slate_state, bool, 0644);
0310 MODULE_PARM_DESC(tablet_mode_in_slate_state, "Enable tablet mode in slate device posture, default is 'true'");
0311 
0312 #define SSAM_EVENT_POS_CID_POSTURE_CHANGED  0x03
0313 #define SSAM_POS_MAX_SOURCES            4
0314 
0315 enum ssam_pos_state {
0316     SSAM_POS_POSTURE_LID_CLOSED = 0x00,
0317     SSAM_POS_POSTURE_LAPTOP     = 0x01,
0318     SSAM_POS_POSTURE_SLATE      = 0x02,
0319     SSAM_POS_POSTURE_TABLET     = 0x03,
0320 };
0321 
0322 struct ssam_sources_list {
0323     __le32 count;
0324     __le32 id[SSAM_POS_MAX_SOURCES];
0325 } __packed;
0326 
0327 static const char *ssam_pos_state_name(struct ssam_tablet_sw *sw, u32 state)
0328 {
0329     switch (state) {
0330     case SSAM_POS_POSTURE_LID_CLOSED:
0331         return "closed";
0332 
0333     case SSAM_POS_POSTURE_LAPTOP:
0334         return "laptop";
0335 
0336     case SSAM_POS_POSTURE_SLATE:
0337         return "slate";
0338 
0339     case SSAM_POS_POSTURE_TABLET:
0340         return "tablet";
0341 
0342     default:
0343         dev_warn(&sw->sdev->dev, "unknown device posture: %u\n", state);
0344         return "<unknown>";
0345     }
0346 }
0347 
0348 static bool ssam_pos_state_is_tablet_mode(struct ssam_tablet_sw *sw, u32 state)
0349 {
0350     switch (state) {
0351     case SSAM_POS_POSTURE_LAPTOP:
0352     case SSAM_POS_POSTURE_LID_CLOSED:
0353         return false;
0354 
0355     case SSAM_POS_POSTURE_SLATE:
0356         return tablet_mode_in_slate_state;
0357 
0358     case SSAM_POS_POSTURE_TABLET:
0359         return true;
0360 
0361     default:
0362         dev_warn(&sw->sdev->dev, "unknown device posture: %u\n", state);
0363         return true;
0364     }
0365 }
0366 
0367 static int ssam_pos_get_sources_list(struct ssam_tablet_sw *sw, struct ssam_sources_list *sources)
0368 {
0369     struct ssam_request rqst;
0370     struct ssam_response rsp;
0371     int status;
0372 
0373     rqst.target_category = SSAM_SSH_TC_POS;
0374     rqst.target_id = 0x01;
0375     rqst.command_id = 0x01;
0376     rqst.instance_id = 0x00;
0377     rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
0378     rqst.length = 0;
0379     rqst.payload = NULL;
0380 
0381     rsp.capacity = sizeof(*sources);
0382     rsp.length = 0;
0383     rsp.pointer = (u8 *)sources;
0384 
0385     status = ssam_retry(ssam_request_sync_onstack, sw->sdev->ctrl, &rqst, &rsp, 0);
0386     if (status)
0387         return status;
0388 
0389     /* We need at least the 'sources->count' field. */
0390     if (rsp.length < sizeof(__le32)) {
0391         dev_err(&sw->sdev->dev, "received source list response is too small\n");
0392         return -EPROTO;
0393     }
0394 
0395     /* Make sure 'sources->count' matches with the response length. */
0396     if (get_unaligned_le32(&sources->count) * sizeof(__le32) + sizeof(__le32) != rsp.length) {
0397         dev_err(&sw->sdev->dev, "mismatch between number of sources and response size\n");
0398         return -EPROTO;
0399     }
0400 
0401     return 0;
0402 }
0403 
0404 static int ssam_pos_get_source(struct ssam_tablet_sw *sw, u32 *source_id)
0405 {
0406     struct ssam_sources_list sources = {};
0407     int status;
0408 
0409     status = ssam_pos_get_sources_list(sw, &sources);
0410     if (status)
0411         return status;
0412 
0413     if (get_unaligned_le32(&sources.count) == 0) {
0414         dev_err(&sw->sdev->dev, "no posture sources found\n");
0415         return -ENODEV;
0416     }
0417 
0418     /*
0419      * We currently don't know what to do with more than one posture
0420      * source. At the moment, only one source seems to be used/provided.
0421      * The WARN_ON() here should hopefully let us know quickly once there
0422      * is a device that provides multiple sources, at which point we can
0423      * then try to figure out how to handle them.
0424      */
0425     WARN_ON(get_unaligned_le32(&sources.count) > 1);
0426 
0427     *source_id = get_unaligned_le32(&sources.id[0]);
0428     return 0;
0429 }
0430 
0431 SSAM_DEFINE_SYNC_REQUEST_WR(__ssam_pos_get_posture_for_source, __le32, __le32, {
0432     .target_category = SSAM_SSH_TC_POS,
0433     .target_id       = 0x01,
0434     .command_id      = 0x02,
0435     .instance_id     = 0x00,
0436 });
0437 
0438 static int ssam_pos_get_posture_for_source(struct ssam_tablet_sw *sw, u32 source_id, u32 *posture)
0439 {
0440     __le32 source_le = cpu_to_le32(source_id);
0441     __le32 rspval_le = 0;
0442     int status;
0443 
0444     status = ssam_retry(__ssam_pos_get_posture_for_source, sw->sdev->ctrl,
0445                 &source_le, &rspval_le);
0446     if (status)
0447         return status;
0448 
0449     *posture = le32_to_cpu(rspval_le);
0450     return 0;
0451 }
0452 
0453 static int ssam_pos_get_posture(struct ssam_tablet_sw *sw, u32 *state)
0454 {
0455     u32 source_id;
0456     int status;
0457 
0458     status = ssam_pos_get_source(sw, &source_id);
0459     if (status) {
0460         dev_err(&sw->sdev->dev, "failed to get posture source ID: %d\n", status);
0461         return status;
0462     }
0463 
0464     status = ssam_pos_get_posture_for_source(sw, source_id, state);
0465     if (status) {
0466         dev_err(&sw->sdev->dev, "failed to get posture value for source %u: %d\n",
0467             source_id, status);
0468         return status;
0469     }
0470 
0471     return 0;
0472 }
0473 
0474 static u32 ssam_pos_sw_notif(struct ssam_event_notifier *nf, const struct ssam_event *event)
0475 {
0476     struct ssam_tablet_sw *sw = container_of(nf, struct ssam_tablet_sw, notif);
0477 
0478     if (event->command_id != SSAM_EVENT_POS_CID_POSTURE_CHANGED)
0479         return 0;   /* Return "unhandled". */
0480 
0481     if (event->length != sizeof(__le32) * 3)
0482         dev_warn(&sw->sdev->dev, "unexpected payload size: %u\n", event->length);
0483 
0484     schedule_work(&sw->update_work);
0485     return SSAM_NOTIF_HANDLED;
0486 }
0487 
0488 static const struct ssam_tablet_sw_desc ssam_pos_sw_desc = {
0489     .dev = {
0490         .name = "Microsoft Surface POS Tablet Mode Switch",
0491         .phys = "ssam/01:26:01:00:01/input0",
0492     },
0493     .ops = {
0494         .notify = ssam_pos_sw_notif,
0495         .get_state = ssam_pos_get_posture,
0496         .state_name = ssam_pos_state_name,
0497         .state_is_tablet_mode = ssam_pos_state_is_tablet_mode,
0498     },
0499     .event = {
0500         .reg = SSAM_EVENT_REGISTRY_SAM,
0501         .id = {
0502             .target_category = SSAM_SSH_TC_POS,
0503             .instance = 0,
0504         },
0505         .mask = SSAM_EVENT_MASK_TARGET,
0506     },
0507 };
0508 
0509 
0510 /* -- Driver registration. -------------------------------------------------- */
0511 
0512 static const struct ssam_device_id ssam_tablet_sw_match[] = {
0513     { SSAM_SDEV(KIP, 0x01, 0x00, 0x01), (unsigned long)&ssam_kip_sw_desc },
0514     { SSAM_SDEV(POS, 0x01, 0x00, 0x01), (unsigned long)&ssam_pos_sw_desc },
0515     { },
0516 };
0517 MODULE_DEVICE_TABLE(ssam, ssam_tablet_sw_match);
0518 
0519 static struct ssam_device_driver ssam_tablet_sw_driver = {
0520     .probe = ssam_tablet_sw_probe,
0521     .remove = ssam_tablet_sw_remove,
0522     .match_table = ssam_tablet_sw_match,
0523     .driver = {
0524         .name = "surface_aggregator_tablet_mode_switch",
0525         .probe_type = PROBE_PREFER_ASYNCHRONOUS,
0526         .pm = &ssam_tablet_sw_pm_ops,
0527     },
0528 };
0529 module_ssam_device_driver(ssam_tablet_sw_driver);
0530 
0531 MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
0532 MODULE_DESCRIPTION("Tablet mode switch driver for Surface devices using the Surface Aggregator Module");
0533 MODULE_LICENSE("GPL");