Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 
0003 /*
0004  *  HID driver for UC-Logic devices not fully compliant with HID standard
0005  *
0006  *  Copyright (c) 2022 José Expósito <jose.exposito89@gmail.com>
0007  */
0008 
0009 #include <kunit/test.h>
0010 #include "./hid-uclogic-rdesc.h"
0011 
0012 struct uclogic_template_case {
0013     const char *name;
0014     const __u8 *template;
0015     size_t template_size;
0016     const s32 *param_list;
0017     size_t param_num;
0018     const __u8 *expected;
0019 };
0020 
0021 static const s32 params_pen_all[UCLOGIC_RDESC_PH_ID_NUM] = {
0022     [UCLOGIC_RDESC_PEN_PH_ID_X_LM] = 0xAA,
0023     [UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0xBB,
0024     [UCLOGIC_RDESC_PEN_PH_ID_Y_LM] = 0xCC,
0025     [UCLOGIC_RDESC_PEN_PH_ID_Y_PM] = 0xDD,
0026     [UCLOGIC_RDESC_PEN_PH_ID_PRESSURE_LM] = 0xEE,
0027 };
0028 
0029 static const s32 params_pen_some[] = {
0030     [UCLOGIC_RDESC_PEN_PH_ID_X_LM] = 0xAA,
0031     [UCLOGIC_RDESC_PEN_PH_ID_X_PM] = 0xBB,
0032 };
0033 
0034 static const s32 params_frame_all[UCLOGIC_RDESC_PH_ID_NUM] = {
0035     [UCLOGIC_RDESC_FRAME_PH_ID_UM] = 0xFF,
0036 };
0037 
0038 static const __u8 template_empty[] = { };
0039 static const __u8 template_small[] = { 0x00 };
0040 static const __u8 template_no_ph[] = { 0xAA, 0xFE, 0xAA, 0xED, 0x1D };
0041 
0042 static const __u8 template_pen_ph_end[] = {
0043     0xAA, 0xBB, UCLOGIC_RDESC_PEN_PH_HEAD
0044 };
0045 
0046 static const __u8 template_btn_ph_end[] = {
0047     0xAA, 0xBB, UCLOGIC_RDESC_FRAME_PH_BTN_HEAD
0048 };
0049 
0050 static const __u8 template_pen_all_params[] = {
0051     UCLOGIC_RDESC_PEN_PH(X_LM),
0052     0x47, UCLOGIC_RDESC_PEN_PH(X_PM),
0053     0x27, UCLOGIC_RDESC_PEN_PH(Y_LM),
0054     UCLOGIC_RDESC_PEN_PH(Y_PM),
0055     0x00, UCLOGIC_RDESC_PEN_PH(PRESSURE_LM),
0056 };
0057 
0058 static const __u8 expected_pen_all_params[] = {
0059     0xAA, 0x00, 0x00, 0x00,
0060     0x47, 0xBB, 0x00, 0x00, 0x00,
0061     0x27, 0xCC, 0x00, 0x00, 0x00,
0062     0xDD, 0x00, 0x00, 0x00,
0063     0x00, 0xEE, 0x00, 0x00, 0x00,
0064 };
0065 
0066 static const __u8 template_frame_all_params[] = {
0067     0x01, 0x02,
0068     UCLOGIC_RDESC_FRAME_PH_BTN,
0069     0x99,
0070 };
0071 
0072 static const __u8 expected_frame_all_params[] = {
0073     0x01, 0x02,
0074     0x2A, 0xFF, 0x00,
0075     0x99,
0076 };
0077 
0078 static const __u8 template_pen_some_params[] = {
0079     0x01, 0x02,
0080     UCLOGIC_RDESC_PEN_PH(X_LM),
0081     0x03, UCLOGIC_RDESC_PEN_PH(X_PM),
0082     0x04, 0x05,
0083 };
0084 
0085 static const __u8 expected_pen_some_params[] = {
0086     0x01, 0x02,
0087     0xAA, 0x00, 0x00, 0x00,
0088     0x03, 0xBB, 0x00, 0x00, 0x00,
0089     0x04, 0x05,
0090 };
0091 
0092 static const __u8 template_params_none[] = {
0093     0x27, UCLOGIC_RDESC_PEN_PH(Y_LM),
0094     UCLOGIC_RDESC_PEN_PH(Y_PM),
0095     0x00, UCLOGIC_RDESC_PEN_PH(PRESSURE_LM),
0096 };
0097 
0098 static struct uclogic_template_case uclogic_template_cases[] = {
0099     {
0100         .name = "Empty template",
0101         .template = template_empty,
0102         .template_size = sizeof(template_empty),
0103         .param_list = params_pen_all,
0104         .param_num = ARRAY_SIZE(params_pen_all),
0105         .expected = template_empty,
0106     },
0107     {
0108         .name = "Template smaller than the placeholder",
0109         .template = template_small,
0110         .template_size = sizeof(template_small),
0111         .param_list = params_pen_all,
0112         .param_num = ARRAY_SIZE(params_pen_all),
0113         .expected = template_small,
0114     },
0115     {
0116         .name = "No placeholder",
0117         .template = template_no_ph,
0118         .template_size = sizeof(template_no_ph),
0119         .param_list = params_pen_all,
0120         .param_num = ARRAY_SIZE(params_pen_all),
0121         .expected = template_no_ph,
0122     },
0123     {
0124         .name = "Pen placeholder at the end, without ID",
0125         .template = template_pen_ph_end,
0126         .template_size = sizeof(template_pen_ph_end),
0127         .param_list = params_pen_all,
0128         .param_num = ARRAY_SIZE(params_pen_all),
0129         .expected = template_pen_ph_end,
0130     },
0131     {
0132         .name = "Frame button placeholder at the end, without ID",
0133         .template = template_btn_ph_end,
0134         .template_size = sizeof(template_btn_ph_end),
0135         .param_list = params_frame_all,
0136         .param_num = ARRAY_SIZE(params_frame_all),
0137         .expected = template_btn_ph_end,
0138     },
0139     {
0140         .name = "All params present in the pen template",
0141         .template = template_pen_all_params,
0142         .template_size = sizeof(template_pen_all_params),
0143         .param_list = params_pen_all,
0144         .param_num = ARRAY_SIZE(params_pen_all),
0145         .expected = expected_pen_all_params,
0146     },
0147     {
0148         .name = "All params present in the frame template",
0149         .template = template_frame_all_params,
0150         .template_size = sizeof(template_frame_all_params),
0151         .param_list = params_frame_all,
0152         .param_num = ARRAY_SIZE(params_frame_all),
0153         .expected = expected_frame_all_params,
0154     },
0155     {
0156         .name = "Some params present in the pen template (complete param list)",
0157         .template = template_pen_some_params,
0158         .template_size = sizeof(template_pen_some_params),
0159         .param_list = params_pen_all,
0160         .param_num = ARRAY_SIZE(params_pen_all),
0161         .expected = expected_pen_some_params,
0162     },
0163     {
0164         .name = "Some params present in the pen template (incomplete param list)",
0165         .template = template_pen_some_params,
0166         .template_size = sizeof(template_pen_some_params),
0167         .param_list = params_pen_some,
0168         .param_num = ARRAY_SIZE(params_pen_some),
0169         .expected = expected_pen_some_params,
0170     },
0171     {
0172         .name = "No params present in the template",
0173         .template = template_params_none,
0174         .template_size = sizeof(template_params_none),
0175         .param_list = params_pen_some,
0176         .param_num = ARRAY_SIZE(params_pen_some),
0177         .expected = template_params_none,
0178     },
0179 };
0180 
0181 static void uclogic_template_case_desc(struct uclogic_template_case *t,
0182                        char *desc)
0183 {
0184     strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
0185 }
0186 
0187 KUNIT_ARRAY_PARAM(uclogic_template, uclogic_template_cases,
0188           uclogic_template_case_desc);
0189 
0190 static void uclogic_template_test(struct kunit *test)
0191 {
0192     __u8 *res;
0193     const struct uclogic_template_case *params = test->param_value;
0194 
0195     res = uclogic_rdesc_template_apply(params->template,
0196                        params->template_size,
0197                        params->param_list,
0198                        params->param_num);
0199     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, res);
0200     KUNIT_EXPECT_EQ(test, 0,
0201             memcmp(res, params->expected, params->template_size));
0202     kfree(res);
0203 }
0204 
0205 static struct kunit_case hid_uclogic_rdesc_test_cases[] = {
0206     KUNIT_CASE_PARAM(uclogic_template_test, uclogic_template_gen_params),
0207     {}
0208 };
0209 
0210 static struct kunit_suite hid_uclogic_rdesc_test_suite = {
0211     .name = "hid-uclogic-rdesc-test",
0212     .test_cases = hid_uclogic_rdesc_test_cases,
0213 };
0214 
0215 kunit_test_suite(hid_uclogic_rdesc_test_suite);
0216 
0217 MODULE_DESCRIPTION("KUnit tests for the UC-Logic driver");
0218 MODULE_LICENSE("GPL");
0219 MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");