0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 #include <linux/acpi.h>
0045 #include <linux/crc16.h>
0046 #include <linux/debugfs.h>
0047 #include <linux/delay.h>
0048 #include <linux/efi.h>
0049 #include <linux/input.h>
0050 #include <linux/input/mt.h>
0051 #include <linux/ktime.h>
0052 #include <linux/leds.h>
0053 #include <linux/module.h>
0054 #include <linux/spinlock.h>
0055 #include <linux/spi/spi.h>
0056 #include <linux/wait.h>
0057 #include <linux/workqueue.h>
0058
0059 #include <asm/barrier.h>
0060 #include <asm/unaligned.h>
0061
0062 #define CREATE_TRACE_POINTS
0063 #include "applespi.h"
0064 #include "applespi_trace.h"
0065
0066 #define APPLESPI_PACKET_SIZE 256
0067 #define APPLESPI_STATUS_SIZE 4
0068
0069 #define PACKET_TYPE_READ 0x20
0070 #define PACKET_TYPE_WRITE 0x40
0071 #define PACKET_DEV_KEYB 0x01
0072 #define PACKET_DEV_TPAD 0x02
0073 #define PACKET_DEV_INFO 0xd0
0074
0075 #define MAX_ROLLOVER 6
0076
0077 #define MAX_FINGERS 11
0078 #define MAX_FINGER_ORIENTATION 16384
0079 #define MAX_PKTS_PER_MSG 2
0080
0081 #define KBD_BL_LEVEL_MIN 32U
0082 #define KBD_BL_LEVEL_MAX 255U
0083 #define KBD_BL_LEVEL_SCALE 1000000U
0084 #define KBD_BL_LEVEL_ADJ \
0085 ((KBD_BL_LEVEL_MAX - KBD_BL_LEVEL_MIN) * KBD_BL_LEVEL_SCALE / 255U)
0086
0087 #define EFI_BL_LEVEL_NAME L"KeyboardBacklightLevel"
0088 #define EFI_BL_LEVEL_GUID EFI_GUID(0xa076d2af, 0x9678, 0x4386, 0x8b, 0x58, 0x1f, 0xc8, 0xef, 0x04, 0x16, 0x19)
0089
0090 #define APPLE_FLAG_FKEY 0x01
0091
0092 #define SPI_RW_CHG_DELAY_US 100
0093
0094 #define SYNAPTICS_VENDOR_ID 0x06cb
0095
0096 static unsigned int fnmode = 1;
0097 module_param(fnmode, uint, 0644);
0098 MODULE_PARM_DESC(fnmode, "Mode of Fn key on Apple keyboards (0 = disabled, [1] = fkeyslast, 2 = fkeysfirst)");
0099
0100 static unsigned int fnremap;
0101 module_param(fnremap, uint, 0644);
0102 MODULE_PARM_DESC(fnremap, "Remap Fn key ([0] = no-remap; 1 = left-ctrl, 2 = left-shift, 3 = left-alt, 4 = left-meta, 6 = right-shift, 7 = right-alt, 8 = right-meta)");
0103
0104 static bool iso_layout;
0105 module_param(iso_layout, bool, 0644);
0106 MODULE_PARM_DESC(iso_layout, "Enable/Disable hardcoded ISO-layout of the keyboard. ([0] = disabled, 1 = enabled)");
0107
0108 static char touchpad_dimensions[40];
0109 module_param_string(touchpad_dimensions, touchpad_dimensions,
0110 sizeof(touchpad_dimensions), 0444);
0111 MODULE_PARM_DESC(touchpad_dimensions, "The pixel dimensions of the touchpad, as XxY+W+H .");
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 struct keyboard_protocol {
0126 u8 unknown1;
0127 u8 modifiers;
0128 u8 unknown2;
0129 u8 keys_pressed[MAX_ROLLOVER];
0130 u8 fn_pressed;
0131 __le16 crc16;
0132 };
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 struct tp_finger {
0155 __le16 origin;
0156 __le16 abs_x;
0157 __le16 abs_y;
0158 __le16 rel_x;
0159 __le16 rel_y;
0160 __le16 tool_major;
0161 __le16 tool_minor;
0162 __le16 orientation;
0163 __le16 touch_major;
0164 __le16 touch_minor;
0165 __le16 unused[2];
0166 __le16 pressure;
0167 __le16 multi;
0168 __le16 crc16;
0169 };
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 struct touchpad_protocol {
0184 u8 unknown1[1];
0185 u8 clicked;
0186 u8 unknown2[28];
0187 u8 number_of_fingers;
0188 u8 clicked2;
0189 u8 unknown3[16];
0190 struct tp_finger fingers[];
0191 };
0192
0193
0194
0195
0196
0197
0198
0199
0200 struct command_protocol_tp_info {
0201 __le16 crc16;
0202 };
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216 struct touchpad_info_protocol {
0217 u8 unknown1[105];
0218 u8 model_flags;
0219 u8 model_no;
0220 u8 unknown2[3];
0221 __le16 crc16;
0222 };
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 struct command_protocol_mt_init {
0233 __le16 cmd;
0234 __le16 crc16;
0235 };
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246 struct command_protocol_capsl {
0247 u8 unknown;
0248 u8 led;
0249 __le16 crc16;
0250 };
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262 struct command_protocol_bl {
0263 __le16 const1;
0264 __le16 level;
0265 __le16 const2;
0266 __le16 crc16;
0267 };
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300 struct message {
0301 __le16 type;
0302 u8 zero;
0303 u8 counter;
0304 __le16 rsp_buf_len;
0305 __le16 length;
0306 union {
0307 struct keyboard_protocol keyboard;
0308 struct touchpad_protocol touchpad;
0309 struct touchpad_info_protocol tp_info;
0310 struct command_protocol_tp_info tp_info_command;
0311 struct command_protocol_mt_init init_mt_command;
0312 struct command_protocol_capsl capsl_command;
0313 struct command_protocol_bl bl_command;
0314 u8 data[0];
0315 };
0316 };
0317
0318
0319 #define MSG_HEADER_SIZE 8
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346 struct spi_packet {
0347 u8 flags;
0348 u8 device;
0349 __le16 offset;
0350 __le16 remaining;
0351 __le16 length;
0352 u8 data[246];
0353 __le16 crc16;
0354 };
0355
0356 struct spi_settings {
0357 u64 spi_cs_delay;
0358 u64 reset_a2r_usec;
0359 u64 reset_rec_usec;
0360 };
0361
0362
0363 struct applespi_tp_info {
0364 int x_min;
0365 int y_min;
0366 int x_max;
0367 int y_max;
0368 };
0369
0370 struct applespi_data {
0371 struct spi_device *spi;
0372 struct spi_settings spi_settings;
0373 struct input_dev *keyboard_input_dev;
0374 struct input_dev *touchpad_input_dev;
0375
0376 u8 *tx_buffer;
0377 u8 *tx_status;
0378 u8 *rx_buffer;
0379
0380 u8 *msg_buf;
0381 unsigned int saved_msg_len;
0382
0383 struct applespi_tp_info tp_info;
0384
0385 u8 last_keys_pressed[MAX_ROLLOVER];
0386 u8 last_keys_fn_pressed[MAX_ROLLOVER];
0387 u8 last_fn_pressed;
0388 struct input_mt_pos pos[MAX_FINGERS];
0389 int slots[MAX_FINGERS];
0390 int gpe;
0391 acpi_handle sien;
0392 acpi_handle sist;
0393
0394 struct spi_transfer dl_t;
0395 struct spi_transfer rd_t;
0396 struct spi_message rd_m;
0397
0398 struct spi_transfer ww_t;
0399 struct spi_transfer wd_t;
0400 struct spi_transfer wr_t;
0401 struct spi_transfer st_t;
0402 struct spi_message wr_m;
0403
0404 bool want_tp_info_cmd;
0405 bool want_mt_init_cmd;
0406 bool want_cl_led_on;
0407 bool have_cl_led_on;
0408 unsigned int want_bl_level;
0409 unsigned int have_bl_level;
0410 unsigned int cmd_msg_cntr;
0411
0412 spinlock_t cmd_msg_lock;
0413 ktime_t cmd_msg_queued;
0414 enum applespi_evt_type cmd_evt_type;
0415
0416 struct led_classdev backlight_info;
0417
0418 bool suspended;
0419 bool drain;
0420 wait_queue_head_t drain_complete;
0421 bool read_active;
0422 bool write_active;
0423
0424 struct work_struct work;
0425 struct touchpad_info_protocol rcvd_tp_info;
0426
0427 struct dentry *debugfs_root;
0428 bool debug_tp_dim;
0429 char tp_dim_val[40];
0430 int tp_dim_min_x;
0431 int tp_dim_max_x;
0432 int tp_dim_min_y;
0433 int tp_dim_max_y;
0434 };
0435
0436 static const unsigned char applespi_scancodes[] = {
0437 0, 0, 0, 0,
0438 KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
0439 KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
0440 KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z,
0441 KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0,
0442 KEY_ENTER, KEY_ESC, KEY_BACKSPACE, KEY_TAB, KEY_SPACE, KEY_MINUS,
0443 KEY_EQUAL, KEY_LEFTBRACE, KEY_RIGHTBRACE, KEY_BACKSLASH, 0,
0444 KEY_SEMICOLON, KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA, KEY_DOT, KEY_SLASH,
0445 KEY_CAPSLOCK,
0446 KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9,
0447 KEY_F10, KEY_F11, KEY_F12, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0448 KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP,
0449 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_102ND,
0450 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0451 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_RO, 0, KEY_YEN, 0, 0, 0, 0, 0,
0452 0, KEY_KATAKANAHIRAGANA, KEY_MUHENKAN
0453 };
0454
0455
0456
0457
0458
0459 static const unsigned char applespi_controlcodes[] = {
0460 KEY_LEFTCTRL,
0461 KEY_LEFTSHIFT,
0462 KEY_LEFTALT,
0463 KEY_LEFTMETA,
0464 0,
0465 KEY_RIGHTSHIFT,
0466 KEY_RIGHTALT,
0467 KEY_RIGHTMETA
0468 };
0469
0470 struct applespi_key_translation {
0471 u16 from;
0472 u16 to;
0473 u8 flags;
0474 };
0475
0476 static const struct applespi_key_translation applespi_fn_codes[] = {
0477 { KEY_BACKSPACE, KEY_DELETE },
0478 { KEY_ENTER, KEY_INSERT },
0479 { KEY_F1, KEY_BRIGHTNESSDOWN, APPLE_FLAG_FKEY },
0480 { KEY_F2, KEY_BRIGHTNESSUP, APPLE_FLAG_FKEY },
0481 { KEY_F3, KEY_SCALE, APPLE_FLAG_FKEY },
0482 { KEY_F4, KEY_DASHBOARD, APPLE_FLAG_FKEY },
0483 { KEY_F5, KEY_KBDILLUMDOWN, APPLE_FLAG_FKEY },
0484 { KEY_F6, KEY_KBDILLUMUP, APPLE_FLAG_FKEY },
0485 { KEY_F7, KEY_PREVIOUSSONG, APPLE_FLAG_FKEY },
0486 { KEY_F8, KEY_PLAYPAUSE, APPLE_FLAG_FKEY },
0487 { KEY_F9, KEY_NEXTSONG, APPLE_FLAG_FKEY },
0488 { KEY_F10, KEY_MUTE, APPLE_FLAG_FKEY },
0489 { KEY_F11, KEY_VOLUMEDOWN, APPLE_FLAG_FKEY },
0490 { KEY_F12, KEY_VOLUMEUP, APPLE_FLAG_FKEY },
0491 { KEY_RIGHT, KEY_END },
0492 { KEY_LEFT, KEY_HOME },
0493 { KEY_DOWN, KEY_PAGEDOWN },
0494 { KEY_UP, KEY_PAGEUP },
0495 { }
0496 };
0497
0498 static const struct applespi_key_translation apple_iso_keyboard[] = {
0499 { KEY_GRAVE, KEY_102ND },
0500 { KEY_102ND, KEY_GRAVE },
0501 { }
0502 };
0503
0504 struct applespi_tp_model_info {
0505 u16 model;
0506 struct applespi_tp_info tp_info;
0507 };
0508
0509 static const struct applespi_tp_model_info applespi_tp_models[] = {
0510 {
0511 .model = 0x04,
0512 .tp_info = { -5087, -182, 5579, 6089 },
0513 },
0514 {
0515 .model = 0x05,
0516 .tp_info = { -6243, -170, 6749, 7685 },
0517 },
0518 {
0519 .model = 0x06,
0520 .tp_info = { -7456, -163, 7976, 9283 },
0521 },
0522 {}
0523 };
0524
0525 typedef void (*applespi_trace_fun)(enum applespi_evt_type,
0526 enum applespi_pkt_type, u8 *, size_t);
0527
0528 static applespi_trace_fun applespi_get_trace_fun(enum applespi_evt_type type)
0529 {
0530 switch (type) {
0531 case ET_CMD_TP_INI:
0532 return trace_applespi_tp_ini_cmd;
0533 case ET_CMD_BL:
0534 return trace_applespi_backlight_cmd;
0535 case ET_CMD_CL:
0536 return trace_applespi_caps_lock_cmd;
0537 case ET_RD_KEYB:
0538 return trace_applespi_keyboard_data;
0539 case ET_RD_TPAD:
0540 return trace_applespi_touchpad_data;
0541 case ET_RD_UNKN:
0542 return trace_applespi_unknown_data;
0543 default:
0544 WARN_ONCE(1, "Unknown msg type %d", type);
0545 return trace_applespi_unknown_data;
0546 }
0547 }
0548
0549 static void applespi_setup_read_txfrs(struct applespi_data *applespi)
0550 {
0551 struct spi_message *msg = &applespi->rd_m;
0552 struct spi_transfer *dl_t = &applespi->dl_t;
0553 struct spi_transfer *rd_t = &applespi->rd_t;
0554
0555 memset(dl_t, 0, sizeof(*dl_t));
0556 memset(rd_t, 0, sizeof(*rd_t));
0557
0558 dl_t->delay.value = applespi->spi_settings.spi_cs_delay;
0559 dl_t->delay.unit = SPI_DELAY_UNIT_USECS;
0560
0561 rd_t->rx_buf = applespi->rx_buffer;
0562 rd_t->len = APPLESPI_PACKET_SIZE;
0563
0564 spi_message_init(msg);
0565 spi_message_add_tail(dl_t, msg);
0566 spi_message_add_tail(rd_t, msg);
0567 }
0568
0569 static void applespi_setup_write_txfrs(struct applespi_data *applespi)
0570 {
0571 struct spi_message *msg = &applespi->wr_m;
0572 struct spi_transfer *wt_t = &applespi->ww_t;
0573 struct spi_transfer *dl_t = &applespi->wd_t;
0574 struct spi_transfer *wr_t = &applespi->wr_t;
0575 struct spi_transfer *st_t = &applespi->st_t;
0576
0577 memset(wt_t, 0, sizeof(*wt_t));
0578 memset(dl_t, 0, sizeof(*dl_t));
0579 memset(wr_t, 0, sizeof(*wr_t));
0580 memset(st_t, 0, sizeof(*st_t));
0581
0582
0583
0584
0585
0586
0587
0588 wt_t->delay.value = SPI_RW_CHG_DELAY_US;
0589 wt_t->delay.unit = SPI_DELAY_UNIT_USECS;
0590 wt_t->cs_change = 1;
0591
0592 dl_t->delay.value = applespi->spi_settings.spi_cs_delay;
0593 dl_t->delay.unit = SPI_DELAY_UNIT_USECS;
0594
0595 wr_t->tx_buf = applespi->tx_buffer;
0596 wr_t->len = APPLESPI_PACKET_SIZE;
0597 wr_t->delay.value = SPI_RW_CHG_DELAY_US;
0598 wr_t->delay.unit = SPI_DELAY_UNIT_USECS;
0599
0600 st_t->rx_buf = applespi->tx_status;
0601 st_t->len = APPLESPI_STATUS_SIZE;
0602
0603 spi_message_init(msg);
0604 spi_message_add_tail(wt_t, msg);
0605 spi_message_add_tail(dl_t, msg);
0606 spi_message_add_tail(wr_t, msg);
0607 spi_message_add_tail(st_t, msg);
0608 }
0609
0610 static int applespi_async(struct applespi_data *applespi,
0611 struct spi_message *message, void (*complete)(void *))
0612 {
0613 message->complete = complete;
0614 message->context = applespi;
0615
0616 return spi_async(applespi->spi, message);
0617 }
0618
0619 static inline bool applespi_check_write_status(struct applespi_data *applespi,
0620 int sts)
0621 {
0622 static u8 status_ok[] = { 0xac, 0x27, 0x68, 0xd5 };
0623
0624 if (sts < 0) {
0625 dev_warn(&applespi->spi->dev, "Error writing to device: %d\n",
0626 sts);
0627 return false;
0628 }
0629
0630 if (memcmp(applespi->tx_status, status_ok, APPLESPI_STATUS_SIZE)) {
0631 dev_warn(&applespi->spi->dev, "Error writing to device: %*ph\n",
0632 APPLESPI_STATUS_SIZE, applespi->tx_status);
0633 return false;
0634 }
0635
0636 return true;
0637 }
0638
0639 static int applespi_get_spi_settings(struct applespi_data *applespi)
0640 {
0641 struct acpi_device *adev = ACPI_COMPANION(&applespi->spi->dev);
0642 const union acpi_object *o;
0643 struct spi_settings *settings = &applespi->spi_settings;
0644
0645 if (!acpi_dev_get_property(adev, "spiCSDelay", ACPI_TYPE_BUFFER, &o))
0646 settings->spi_cs_delay = *(u64 *)o->buffer.pointer;
0647 else
0648 dev_warn(&applespi->spi->dev,
0649 "Property spiCSDelay not found\n");
0650
0651 if (!acpi_dev_get_property(adev, "resetA2RUsec", ACPI_TYPE_BUFFER, &o))
0652 settings->reset_a2r_usec = *(u64 *)o->buffer.pointer;
0653 else
0654 dev_warn(&applespi->spi->dev,
0655 "Property resetA2RUsec not found\n");
0656
0657 if (!acpi_dev_get_property(adev, "resetRecUsec", ACPI_TYPE_BUFFER, &o))
0658 settings->reset_rec_usec = *(u64 *)o->buffer.pointer;
0659 else
0660 dev_warn(&applespi->spi->dev,
0661 "Property resetRecUsec not found\n");
0662
0663 dev_dbg(&applespi->spi->dev,
0664 "SPI settings: spi_cs_delay=%llu reset_a2r_usec=%llu reset_rec_usec=%llu\n",
0665 settings->spi_cs_delay, settings->reset_a2r_usec,
0666 settings->reset_rec_usec);
0667
0668 return 0;
0669 }
0670
0671 static int applespi_setup_spi(struct applespi_data *applespi)
0672 {
0673 int sts;
0674
0675 sts = applespi_get_spi_settings(applespi);
0676 if (sts)
0677 return sts;
0678
0679 spin_lock_init(&applespi->cmd_msg_lock);
0680 init_waitqueue_head(&applespi->drain_complete);
0681
0682 return 0;
0683 }
0684
0685 static int applespi_enable_spi(struct applespi_data *applespi)
0686 {
0687 acpi_status acpi_sts;
0688 unsigned long long spi_status;
0689
0690
0691 acpi_sts = acpi_evaluate_integer(applespi->sist, NULL, NULL,
0692 &spi_status);
0693 if (ACPI_SUCCESS(acpi_sts) && spi_status)
0694 return 0;
0695
0696
0697 acpi_sts = acpi_execute_simple_method(applespi->sien, NULL, 1);
0698 if (ACPI_FAILURE(acpi_sts)) {
0699 dev_err(&applespi->spi->dev, "SIEN failed: %s\n",
0700 acpi_format_exception(acpi_sts));
0701 return -ENODEV;
0702 }
0703
0704
0705
0706
0707
0708
0709
0710 msleep(50);
0711
0712 return 0;
0713 }
0714
0715 static int applespi_send_cmd_msg(struct applespi_data *applespi);
0716
0717 static void applespi_msg_complete(struct applespi_data *applespi,
0718 bool is_write_msg, bool is_read_compl)
0719 {
0720 unsigned long flags;
0721
0722 spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
0723
0724 if (is_read_compl)
0725 applespi->read_active = false;
0726 if (is_write_msg)
0727 applespi->write_active = false;
0728
0729 if (applespi->drain && !applespi->write_active)
0730 wake_up_all(&applespi->drain_complete);
0731
0732 if (is_write_msg) {
0733 applespi->cmd_msg_queued = 0;
0734 applespi_send_cmd_msg(applespi);
0735 }
0736
0737 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
0738 }
0739
0740 static void applespi_async_write_complete(void *context)
0741 {
0742 struct applespi_data *applespi = context;
0743 enum applespi_evt_type evt_type = applespi->cmd_evt_type;
0744
0745 applespi_get_trace_fun(evt_type)(evt_type, PT_WRITE,
0746 applespi->tx_buffer,
0747 APPLESPI_PACKET_SIZE);
0748 applespi_get_trace_fun(evt_type)(evt_type, PT_STATUS,
0749 applespi->tx_status,
0750 APPLESPI_STATUS_SIZE);
0751
0752 udelay(SPI_RW_CHG_DELAY_US);
0753
0754 if (!applespi_check_write_status(applespi, applespi->wr_m.status)) {
0755
0756
0757
0758
0759 applespi_msg_complete(applespi, true, false);
0760 }
0761 }
0762
0763 static int applespi_send_cmd_msg(struct applespi_data *applespi)
0764 {
0765 u16 crc;
0766 int sts;
0767 struct spi_packet *packet = (struct spi_packet *)applespi->tx_buffer;
0768 struct message *message = (struct message *)packet->data;
0769 u16 msg_len;
0770 u8 device;
0771
0772
0773 if (applespi->drain)
0774 return 0;
0775
0776
0777 if (applespi->cmd_msg_queued) {
0778 if (ktime_ms_delta(ktime_get(), applespi->cmd_msg_queued) < 1000)
0779 return 0;
0780
0781 dev_warn(&applespi->spi->dev, "Command %d timed out\n",
0782 applespi->cmd_evt_type);
0783
0784 applespi->cmd_msg_queued = 0;
0785 applespi->write_active = false;
0786 }
0787
0788
0789 memset(packet, 0, APPLESPI_PACKET_SIZE);
0790
0791
0792 if (applespi->want_tp_info_cmd) {
0793 applespi->want_tp_info_cmd = false;
0794 applespi->want_mt_init_cmd = true;
0795 applespi->cmd_evt_type = ET_CMD_TP_INI;
0796
0797
0798 device = PACKET_DEV_INFO;
0799
0800 message->type = cpu_to_le16(0x1020);
0801 msg_len = sizeof(message->tp_info_command);
0802
0803 message->zero = 0x02;
0804 message->rsp_buf_len = cpu_to_le16(0x0200);
0805
0806 } else if (applespi->want_mt_init_cmd) {
0807 applespi->want_mt_init_cmd = false;
0808 applespi->cmd_evt_type = ET_CMD_TP_INI;
0809
0810
0811 device = PACKET_DEV_TPAD;
0812
0813 message->type = cpu_to_le16(0x0252);
0814 msg_len = sizeof(message->init_mt_command);
0815
0816 message->init_mt_command.cmd = cpu_to_le16(0x0102);
0817
0818
0819 } else if (applespi->want_cl_led_on != applespi->have_cl_led_on) {
0820 applespi->have_cl_led_on = applespi->want_cl_led_on;
0821 applespi->cmd_evt_type = ET_CMD_CL;
0822
0823
0824 device = PACKET_DEV_KEYB;
0825
0826 message->type = cpu_to_le16(0x0151);
0827 msg_len = sizeof(message->capsl_command);
0828
0829 message->capsl_command.unknown = 0x01;
0830 message->capsl_command.led = applespi->have_cl_led_on ? 2 : 0;
0831
0832
0833 } else if (applespi->want_bl_level != applespi->have_bl_level) {
0834 applespi->have_bl_level = applespi->want_bl_level;
0835 applespi->cmd_evt_type = ET_CMD_BL;
0836
0837
0838 device = PACKET_DEV_KEYB;
0839
0840 message->type = cpu_to_le16(0xB051);
0841 msg_len = sizeof(message->bl_command);
0842
0843 message->bl_command.const1 = cpu_to_le16(0x01B0);
0844 message->bl_command.level =
0845 cpu_to_le16(applespi->have_bl_level);
0846
0847 if (applespi->have_bl_level > 0)
0848 message->bl_command.const2 = cpu_to_le16(0x01F4);
0849 else
0850 message->bl_command.const2 = cpu_to_le16(0x0001);
0851
0852
0853 } else {
0854 return 0;
0855 }
0856
0857
0858 packet->flags = PACKET_TYPE_WRITE;
0859 packet->device = device;
0860 packet->length = cpu_to_le16(MSG_HEADER_SIZE + msg_len);
0861
0862 message->counter = applespi->cmd_msg_cntr++ % (U8_MAX + 1);
0863
0864 message->length = cpu_to_le16(msg_len - 2);
0865 if (!message->rsp_buf_len)
0866 message->rsp_buf_len = message->length;
0867
0868 crc = crc16(0, (u8 *)message, le16_to_cpu(packet->length) - 2);
0869 put_unaligned_le16(crc, &message->data[msg_len - 2]);
0870
0871 crc = crc16(0, (u8 *)packet, sizeof(*packet) - 2);
0872 packet->crc16 = cpu_to_le16(crc);
0873
0874
0875 sts = applespi_async(applespi, &applespi->wr_m,
0876 applespi_async_write_complete);
0877 if (sts) {
0878 dev_warn(&applespi->spi->dev,
0879 "Error queueing async write to device: %d\n", sts);
0880 return sts;
0881 }
0882
0883 applespi->cmd_msg_queued = ktime_get_coarse();
0884 applespi->write_active = true;
0885
0886 return 0;
0887 }
0888
0889 static void applespi_init(struct applespi_data *applespi, bool is_resume)
0890 {
0891 unsigned long flags;
0892
0893 spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
0894
0895 if (is_resume)
0896 applespi->want_mt_init_cmd = true;
0897 else
0898 applespi->want_tp_info_cmd = true;
0899 applespi_send_cmd_msg(applespi);
0900
0901 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
0902 }
0903
0904 static int applespi_set_capsl_led(struct applespi_data *applespi,
0905 bool capslock_on)
0906 {
0907 unsigned long flags;
0908 int sts;
0909
0910 spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
0911
0912 applespi->want_cl_led_on = capslock_on;
0913 sts = applespi_send_cmd_msg(applespi);
0914
0915 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
0916
0917 return sts;
0918 }
0919
0920 static void applespi_set_bl_level(struct led_classdev *led_cdev,
0921 enum led_brightness value)
0922 {
0923 struct applespi_data *applespi =
0924 container_of(led_cdev, struct applespi_data, backlight_info);
0925 unsigned long flags;
0926
0927 spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
0928
0929 if (value == 0) {
0930 applespi->want_bl_level = value;
0931 } else {
0932
0933
0934
0935
0936
0937 applespi->want_bl_level =
0938 ((value * KBD_BL_LEVEL_ADJ) / KBD_BL_LEVEL_SCALE +
0939 KBD_BL_LEVEL_MIN);
0940 }
0941
0942 applespi_send_cmd_msg(applespi);
0943
0944 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
0945 }
0946
0947 static int applespi_event(struct input_dev *dev, unsigned int type,
0948 unsigned int code, int value)
0949 {
0950 struct applespi_data *applespi = input_get_drvdata(dev);
0951
0952 switch (type) {
0953 case EV_LED:
0954 applespi_set_capsl_led(applespi, !!test_bit(LED_CAPSL, dev->led));
0955 return 0;
0956 }
0957
0958 return -EINVAL;
0959 }
0960
0961
0962
0963 static inline int le16_to_int(__le16 x)
0964 {
0965 return (signed short)le16_to_cpu(x);
0966 }
0967
0968 static void applespi_debug_update_dimensions(struct applespi_data *applespi,
0969 const struct tp_finger *f)
0970 {
0971 applespi->tp_dim_min_x = min(applespi->tp_dim_min_x,
0972 le16_to_int(f->abs_x));
0973 applespi->tp_dim_max_x = max(applespi->tp_dim_max_x,
0974 le16_to_int(f->abs_x));
0975 applespi->tp_dim_min_y = min(applespi->tp_dim_min_y,
0976 le16_to_int(f->abs_y));
0977 applespi->tp_dim_max_y = max(applespi->tp_dim_max_y,
0978 le16_to_int(f->abs_y));
0979 }
0980
0981 static int applespi_tp_dim_open(struct inode *inode, struct file *file)
0982 {
0983 struct applespi_data *applespi = inode->i_private;
0984
0985 file->private_data = applespi;
0986
0987 snprintf(applespi->tp_dim_val, sizeof(applespi->tp_dim_val),
0988 "0x%.4x %dx%d+%u+%u\n",
0989 applespi->touchpad_input_dev->id.product,
0990 applespi->tp_dim_min_x, applespi->tp_dim_min_y,
0991 applespi->tp_dim_max_x - applespi->tp_dim_min_x,
0992 applespi->tp_dim_max_y - applespi->tp_dim_min_y);
0993
0994 return nonseekable_open(inode, file);
0995 }
0996
0997 static ssize_t applespi_tp_dim_read(struct file *file, char __user *buf,
0998 size_t len, loff_t *off)
0999 {
1000 struct applespi_data *applespi = file->private_data;
1001
1002 return simple_read_from_buffer(buf, len, off, applespi->tp_dim_val,
1003 strlen(applespi->tp_dim_val));
1004 }
1005
1006 static const struct file_operations applespi_tp_dim_fops = {
1007 .owner = THIS_MODULE,
1008 .open = applespi_tp_dim_open,
1009 .read = applespi_tp_dim_read,
1010 .llseek = no_llseek,
1011 };
1012
1013 static void report_finger_data(struct input_dev *input, int slot,
1014 const struct input_mt_pos *pos,
1015 const struct tp_finger *f)
1016 {
1017 input_mt_slot(input, slot);
1018 input_mt_report_slot_state(input, MT_TOOL_FINGER, true);
1019
1020 input_report_abs(input, ABS_MT_TOUCH_MAJOR,
1021 le16_to_int(f->touch_major) << 1);
1022 input_report_abs(input, ABS_MT_TOUCH_MINOR,
1023 le16_to_int(f->touch_minor) << 1);
1024 input_report_abs(input, ABS_MT_WIDTH_MAJOR,
1025 le16_to_int(f->tool_major) << 1);
1026 input_report_abs(input, ABS_MT_WIDTH_MINOR,
1027 le16_to_int(f->tool_minor) << 1);
1028 input_report_abs(input, ABS_MT_ORIENTATION,
1029 MAX_FINGER_ORIENTATION - le16_to_int(f->orientation));
1030 input_report_abs(input, ABS_MT_POSITION_X, pos->x);
1031 input_report_abs(input, ABS_MT_POSITION_Y, pos->y);
1032 }
1033
1034 static void report_tp_state(struct applespi_data *applespi,
1035 struct touchpad_protocol *t)
1036 {
1037 const struct tp_finger *f;
1038 struct input_dev *input;
1039 const struct applespi_tp_info *tp_info = &applespi->tp_info;
1040 int i, n;
1041
1042
1043 input = smp_load_acquire(&applespi->touchpad_input_dev);
1044 if (!input)
1045 return;
1046
1047 n = 0;
1048
1049 for (i = 0; i < t->number_of_fingers; i++) {
1050 f = &t->fingers[i];
1051 if (le16_to_int(f->touch_major) == 0)
1052 continue;
1053 applespi->pos[n].x = le16_to_int(f->abs_x);
1054 applespi->pos[n].y = tp_info->y_min + tp_info->y_max -
1055 le16_to_int(f->abs_y);
1056 n++;
1057
1058 if (applespi->debug_tp_dim)
1059 applespi_debug_update_dimensions(applespi, f);
1060 }
1061
1062 input_mt_assign_slots(input, applespi->slots, applespi->pos, n, 0);
1063
1064 for (i = 0; i < n; i++)
1065 report_finger_data(input, applespi->slots[i],
1066 &applespi->pos[i], &t->fingers[i]);
1067
1068 input_mt_sync_frame(input);
1069 input_report_key(input, BTN_LEFT, t->clicked);
1070
1071 input_sync(input);
1072 }
1073
1074 static const struct applespi_key_translation *
1075 applespi_find_translation(const struct applespi_key_translation *table, u16 key)
1076 {
1077 const struct applespi_key_translation *trans;
1078
1079 for (trans = table; trans->from; trans++)
1080 if (trans->from == key)
1081 return trans;
1082
1083 return NULL;
1084 }
1085
1086 static unsigned int applespi_translate_fn_key(unsigned int key, int fn_pressed)
1087 {
1088 const struct applespi_key_translation *trans;
1089 int do_translate;
1090
1091 trans = applespi_find_translation(applespi_fn_codes, key);
1092 if (trans) {
1093 if (trans->flags & APPLE_FLAG_FKEY)
1094 do_translate = (fnmode == 2 && fn_pressed) ||
1095 (fnmode == 1 && !fn_pressed);
1096 else
1097 do_translate = fn_pressed;
1098
1099 if (do_translate)
1100 key = trans->to;
1101 }
1102
1103 return key;
1104 }
1105
1106 static unsigned int applespi_translate_iso_layout(unsigned int key)
1107 {
1108 const struct applespi_key_translation *trans;
1109
1110 trans = applespi_find_translation(apple_iso_keyboard, key);
1111 if (trans)
1112 key = trans->to;
1113
1114 return key;
1115 }
1116
1117 static unsigned int applespi_code_to_key(u8 code, int fn_pressed)
1118 {
1119 unsigned int key = applespi_scancodes[code];
1120
1121 if (fnmode)
1122 key = applespi_translate_fn_key(key, fn_pressed);
1123 if (iso_layout)
1124 key = applespi_translate_iso_layout(key);
1125 return key;
1126 }
1127
1128 static void
1129 applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)
1130 {
1131 unsigned char tmp;
1132 u8 bit = BIT((fnremap - 1) & 0x07);
1133
1134 if (!fnremap || fnremap > ARRAY_SIZE(applespi_controlcodes) ||
1135 !applespi_controlcodes[fnremap - 1])
1136 return;
1137
1138 tmp = keyboard_protocol->fn_pressed;
1139 keyboard_protocol->fn_pressed = !!(keyboard_protocol->modifiers & bit);
1140 if (tmp)
1141 keyboard_protocol->modifiers |= bit;
1142 else
1143 keyboard_protocol->modifiers &= ~bit;
1144 }
1145
1146 static void
1147 applespi_handle_keyboard_event(struct applespi_data *applespi,
1148 struct keyboard_protocol *keyboard_protocol)
1149 {
1150 unsigned int key;
1151 int i;
1152
1153 compiletime_assert(ARRAY_SIZE(applespi_controlcodes) ==
1154 sizeof_field(struct keyboard_protocol, modifiers) * 8,
1155 "applespi_controlcodes has wrong number of entries");
1156
1157
1158 if (!memchr_inv(keyboard_protocol->keys_pressed, 1, MAX_ROLLOVER))
1159 return;
1160
1161
1162 applespi_remap_fn_key(keyboard_protocol);
1163
1164
1165 for (i = 0; i < MAX_ROLLOVER; i++) {
1166 if (memchr(keyboard_protocol->keys_pressed,
1167 applespi->last_keys_pressed[i], MAX_ROLLOVER))
1168 continue;
1169
1170 key = applespi_code_to_key(applespi->last_keys_pressed[i],
1171 applespi->last_keys_fn_pressed[i]);
1172 input_report_key(applespi->keyboard_input_dev, key, 0);
1173 applespi->last_keys_fn_pressed[i] = 0;
1174 }
1175
1176
1177 for (i = 0; i < MAX_ROLLOVER; i++) {
1178 if (keyboard_protocol->keys_pressed[i] <
1179 ARRAY_SIZE(applespi_scancodes) &&
1180 keyboard_protocol->keys_pressed[i] > 0) {
1181 key = applespi_code_to_key(
1182 keyboard_protocol->keys_pressed[i],
1183 keyboard_protocol->fn_pressed);
1184 input_report_key(applespi->keyboard_input_dev, key, 1);
1185 applespi->last_keys_fn_pressed[i] =
1186 keyboard_protocol->fn_pressed;
1187 }
1188 }
1189
1190
1191 for (i = 0; i < ARRAY_SIZE(applespi_controlcodes); i++) {
1192 if (keyboard_protocol->modifiers & BIT(i))
1193 input_report_key(applespi->keyboard_input_dev,
1194 applespi_controlcodes[i], 1);
1195 else
1196 input_report_key(applespi->keyboard_input_dev,
1197 applespi_controlcodes[i], 0);
1198 }
1199
1200
1201 if (keyboard_protocol->fn_pressed && !applespi->last_fn_pressed)
1202 input_report_key(applespi->keyboard_input_dev, KEY_FN, 1);
1203 else if (!keyboard_protocol->fn_pressed && applespi->last_fn_pressed)
1204 input_report_key(applespi->keyboard_input_dev, KEY_FN, 0);
1205 applespi->last_fn_pressed = keyboard_protocol->fn_pressed;
1206
1207
1208 input_sync(applespi->keyboard_input_dev);
1209 memcpy(&applespi->last_keys_pressed, keyboard_protocol->keys_pressed,
1210 sizeof(applespi->last_keys_pressed));
1211 }
1212
1213 static const struct applespi_tp_info *applespi_find_touchpad_info(u8 model)
1214 {
1215 const struct applespi_tp_model_info *info;
1216
1217 for (info = applespi_tp_models; info->model; info++) {
1218 if (info->model == model)
1219 return &info->tp_info;
1220 }
1221
1222 return NULL;
1223 }
1224
1225 static int
1226 applespi_register_touchpad_device(struct applespi_data *applespi,
1227 struct touchpad_info_protocol *rcvd_tp_info)
1228 {
1229 const struct applespi_tp_info *tp_info;
1230 struct input_dev *touchpad_input_dev;
1231 int sts;
1232
1233
1234 tp_info = applespi_find_touchpad_info(rcvd_tp_info->model_no);
1235 if (!tp_info) {
1236 dev_warn(&applespi->spi->dev,
1237 "Unknown touchpad model %x - falling back to MB8 touchpad\n",
1238 rcvd_tp_info->model_no);
1239 tp_info = &applespi_tp_models[0].tp_info;
1240 }
1241
1242 applespi->tp_info = *tp_info;
1243
1244 if (touchpad_dimensions[0]) {
1245 int x, y, w, h;
1246
1247 sts = sscanf(touchpad_dimensions, "%dx%d+%u+%u", &x, &y, &w, &h);
1248 if (sts == 4) {
1249 dev_info(&applespi->spi->dev,
1250 "Overriding touchpad dimensions from module param\n");
1251 applespi->tp_info.x_min = x;
1252 applespi->tp_info.y_min = y;
1253 applespi->tp_info.x_max = x + w;
1254 applespi->tp_info.y_max = y + h;
1255 } else {
1256 dev_warn(&applespi->spi->dev,
1257 "Invalid touchpad dimensions '%s': must be in the form XxY+W+H\n",
1258 touchpad_dimensions);
1259 touchpad_dimensions[0] = '\0';
1260 }
1261 }
1262 if (!touchpad_dimensions[0]) {
1263 snprintf(touchpad_dimensions, sizeof(touchpad_dimensions),
1264 "%dx%d+%u+%u",
1265 applespi->tp_info.x_min,
1266 applespi->tp_info.y_min,
1267 applespi->tp_info.x_max - applespi->tp_info.x_min,
1268 applespi->tp_info.y_max - applespi->tp_info.y_min);
1269 }
1270
1271
1272 touchpad_input_dev = devm_input_allocate_device(&applespi->spi->dev);
1273 if (!touchpad_input_dev) {
1274 dev_err(&applespi->spi->dev,
1275 "Failed to allocate touchpad input device\n");
1276 return -ENOMEM;
1277 }
1278
1279 touchpad_input_dev->name = "Apple SPI Touchpad";
1280 touchpad_input_dev->phys = "applespi/input1";
1281 touchpad_input_dev->dev.parent = &applespi->spi->dev;
1282 touchpad_input_dev->id.bustype = BUS_SPI;
1283 touchpad_input_dev->id.vendor = SYNAPTICS_VENDOR_ID;
1284 touchpad_input_dev->id.product =
1285 rcvd_tp_info->model_no << 8 | rcvd_tp_info->model_flags;
1286
1287
1288 input_set_capability(touchpad_input_dev, EV_REL, REL_X);
1289 input_set_capability(touchpad_input_dev, EV_REL, REL_Y);
1290
1291 __set_bit(INPUT_PROP_POINTER, touchpad_input_dev->propbit);
1292 __set_bit(INPUT_PROP_BUTTONPAD, touchpad_input_dev->propbit);
1293
1294
1295 input_set_abs_params(touchpad_input_dev, ABS_MT_TOUCH_MAJOR,
1296 0, 5000, 0, 0);
1297 input_set_abs_params(touchpad_input_dev, ABS_MT_TOUCH_MINOR,
1298 0, 5000, 0, 0);
1299
1300
1301 input_set_abs_params(touchpad_input_dev, ABS_MT_WIDTH_MAJOR,
1302 0, 5000, 0, 0);
1303 input_set_abs_params(touchpad_input_dev, ABS_MT_WIDTH_MINOR,
1304 0, 5000, 0, 0);
1305
1306
1307 input_set_abs_params(touchpad_input_dev, ABS_MT_ORIENTATION,
1308 -MAX_FINGER_ORIENTATION, MAX_FINGER_ORIENTATION,
1309 0, 0);
1310
1311
1312 input_set_abs_params(touchpad_input_dev, ABS_MT_POSITION_X,
1313 applespi->tp_info.x_min, applespi->tp_info.x_max,
1314 0, 0);
1315 input_set_abs_params(touchpad_input_dev, ABS_MT_POSITION_Y,
1316 applespi->tp_info.y_min, applespi->tp_info.y_max,
1317 0, 0);
1318
1319
1320 input_set_capability(touchpad_input_dev, EV_KEY, BTN_LEFT);
1321
1322
1323 sts = input_mt_init_slots(touchpad_input_dev, MAX_FINGERS,
1324 INPUT_MT_POINTER | INPUT_MT_DROP_UNUSED |
1325 INPUT_MT_TRACK);
1326 if (sts) {
1327 dev_err(&applespi->spi->dev,
1328 "failed to initialize slots: %d", sts);
1329 return sts;
1330 }
1331
1332
1333 sts = input_register_device(touchpad_input_dev);
1334 if (sts) {
1335 dev_err(&applespi->spi->dev,
1336 "Unable to register touchpad input device (%d)\n", sts);
1337 return sts;
1338 }
1339
1340
1341 smp_store_release(&applespi->touchpad_input_dev, touchpad_input_dev);
1342
1343 return 0;
1344 }
1345
1346 static void applespi_worker(struct work_struct *work)
1347 {
1348 struct applespi_data *applespi =
1349 container_of(work, struct applespi_data, work);
1350
1351 applespi_register_touchpad_device(applespi, &applespi->rcvd_tp_info);
1352 }
1353
1354 static void applespi_handle_cmd_response(struct applespi_data *applespi,
1355 struct spi_packet *packet,
1356 struct message *message)
1357 {
1358 if (packet->device == PACKET_DEV_INFO &&
1359 le16_to_cpu(message->type) == 0x1020) {
1360
1361
1362
1363
1364 applespi->rcvd_tp_info = message->tp_info;
1365 schedule_work(&applespi->work);
1366 return;
1367 }
1368
1369 if (le16_to_cpu(message->length) != 0x0000) {
1370 dev_warn_ratelimited(&applespi->spi->dev,
1371 "Received unexpected write response: length=%x\n",
1372 le16_to_cpu(message->length));
1373 return;
1374 }
1375
1376 if (packet->device == PACKET_DEV_TPAD &&
1377 le16_to_cpu(message->type) == 0x0252 &&
1378 le16_to_cpu(message->rsp_buf_len) == 0x0002)
1379 dev_info(&applespi->spi->dev, "modeswitch done.\n");
1380 }
1381
1382 static bool applespi_verify_crc(struct applespi_data *applespi, u8 *buffer,
1383 size_t buflen)
1384 {
1385 u16 crc;
1386
1387 crc = crc16(0, buffer, buflen);
1388 if (crc) {
1389 dev_warn_ratelimited(&applespi->spi->dev,
1390 "Received corrupted packet (crc mismatch)\n");
1391 trace_applespi_bad_crc(ET_RD_CRC, READ, buffer, buflen);
1392
1393 return false;
1394 }
1395
1396 return true;
1397 }
1398
1399 static void applespi_debug_print_read_packet(struct applespi_data *applespi,
1400 struct spi_packet *packet)
1401 {
1402 unsigned int evt_type;
1403
1404 if (packet->flags == PACKET_TYPE_READ &&
1405 packet->device == PACKET_DEV_KEYB)
1406 evt_type = ET_RD_KEYB;
1407 else if (packet->flags == PACKET_TYPE_READ &&
1408 packet->device == PACKET_DEV_TPAD)
1409 evt_type = ET_RD_TPAD;
1410 else if (packet->flags == PACKET_TYPE_WRITE)
1411 evt_type = applespi->cmd_evt_type;
1412 else
1413 evt_type = ET_RD_UNKN;
1414
1415 applespi_get_trace_fun(evt_type)(evt_type, PT_READ, applespi->rx_buffer,
1416 APPLESPI_PACKET_SIZE);
1417 }
1418
1419 static void applespi_got_data(struct applespi_data *applespi)
1420 {
1421 struct spi_packet *packet;
1422 struct message *message;
1423 unsigned int msg_len;
1424 unsigned int off;
1425 unsigned int rem;
1426 unsigned int len;
1427
1428
1429 if (!applespi_verify_crc(applespi, applespi->rx_buffer,
1430 APPLESPI_PACKET_SIZE)) {
1431 unsigned long flags;
1432
1433 spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
1434
1435 if (applespi->drain) {
1436 applespi->read_active = false;
1437 applespi->write_active = false;
1438
1439 wake_up_all(&applespi->drain_complete);
1440 }
1441
1442 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
1443
1444 return;
1445 }
1446
1447 packet = (struct spi_packet *)applespi->rx_buffer;
1448
1449 applespi_debug_print_read_packet(applespi, packet);
1450
1451 off = le16_to_cpu(packet->offset);
1452 rem = le16_to_cpu(packet->remaining);
1453 len = le16_to_cpu(packet->length);
1454
1455 if (len > sizeof(packet->data)) {
1456 dev_warn_ratelimited(&applespi->spi->dev,
1457 "Received corrupted packet (invalid packet length %u)\n",
1458 len);
1459 goto msg_complete;
1460 }
1461
1462
1463 if (rem > 0 || off > 0) {
1464 if (off != applespi->saved_msg_len) {
1465 dev_warn_ratelimited(&applespi->spi->dev,
1466 "Received unexpected offset (got %u, expected %u)\n",
1467 off, applespi->saved_msg_len);
1468 goto msg_complete;
1469 }
1470
1471 if (off + rem > MAX_PKTS_PER_MSG * APPLESPI_PACKET_SIZE) {
1472 dev_warn_ratelimited(&applespi->spi->dev,
1473 "Received message too large (size %u)\n",
1474 off + rem);
1475 goto msg_complete;
1476 }
1477
1478 if (off + len > MAX_PKTS_PER_MSG * APPLESPI_PACKET_SIZE) {
1479 dev_warn_ratelimited(&applespi->spi->dev,
1480 "Received message too large (size %u)\n",
1481 off + len);
1482 goto msg_complete;
1483 }
1484
1485 memcpy(applespi->msg_buf + off, &packet->data, len);
1486 applespi->saved_msg_len += len;
1487
1488 if (rem > 0)
1489 return;
1490
1491 message = (struct message *)applespi->msg_buf;
1492 msg_len = applespi->saved_msg_len;
1493 } else {
1494 message = (struct message *)&packet->data;
1495 msg_len = len;
1496 }
1497
1498
1499 if (!applespi_verify_crc(applespi, (u8 *)message, msg_len))
1500 goto msg_complete;
1501
1502 if (le16_to_cpu(message->length) != msg_len - MSG_HEADER_SIZE - 2) {
1503 dev_warn_ratelimited(&applespi->spi->dev,
1504 "Received corrupted packet (invalid message length %u - expected %u)\n",
1505 le16_to_cpu(message->length),
1506 msg_len - MSG_HEADER_SIZE - 2);
1507 goto msg_complete;
1508 }
1509
1510
1511 if (packet->flags == PACKET_TYPE_READ &&
1512 packet->device == PACKET_DEV_KEYB) {
1513 applespi_handle_keyboard_event(applespi, &message->keyboard);
1514
1515 } else if (packet->flags == PACKET_TYPE_READ &&
1516 packet->device == PACKET_DEV_TPAD) {
1517 struct touchpad_protocol *tp;
1518 size_t tp_len;
1519
1520 tp = &message->touchpad;
1521 tp_len = struct_size(tp, fingers, tp->number_of_fingers);
1522
1523 if (le16_to_cpu(message->length) + 2 != tp_len) {
1524 dev_warn_ratelimited(&applespi->spi->dev,
1525 "Received corrupted packet (invalid message length %u - num-fingers %u, tp-len %zu)\n",
1526 le16_to_cpu(message->length),
1527 tp->number_of_fingers, tp_len);
1528 goto msg_complete;
1529 }
1530
1531 if (tp->number_of_fingers > MAX_FINGERS) {
1532 dev_warn_ratelimited(&applespi->spi->dev,
1533 "Number of reported fingers (%u) exceeds max (%u))\n",
1534 tp->number_of_fingers,
1535 MAX_FINGERS);
1536 tp->number_of_fingers = MAX_FINGERS;
1537 }
1538
1539 report_tp_state(applespi, tp);
1540
1541 } else if (packet->flags == PACKET_TYPE_WRITE) {
1542 applespi_handle_cmd_response(applespi, packet, message);
1543 }
1544
1545 msg_complete:
1546 applespi->saved_msg_len = 0;
1547
1548 applespi_msg_complete(applespi, packet->flags == PACKET_TYPE_WRITE,
1549 true);
1550 }
1551
1552 static void applespi_async_read_complete(void *context)
1553 {
1554 struct applespi_data *applespi = context;
1555
1556 if (applespi->rd_m.status < 0) {
1557 dev_warn(&applespi->spi->dev, "Error reading from device: %d\n",
1558 applespi->rd_m.status);
1559
1560
1561
1562
1563
1564 applespi_msg_complete(applespi, true, true);
1565 } else {
1566 applespi_got_data(applespi);
1567 }
1568
1569 acpi_finish_gpe(NULL, applespi->gpe);
1570 }
1571
1572 static u32 applespi_notify(acpi_handle gpe_device, u32 gpe, void *context)
1573 {
1574 struct applespi_data *applespi = context;
1575 int sts;
1576 unsigned long flags;
1577
1578 trace_applespi_irq_received(ET_RD_IRQ, PT_READ);
1579
1580 spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
1581
1582 if (!applespi->suspended) {
1583 sts = applespi_async(applespi, &applespi->rd_m,
1584 applespi_async_read_complete);
1585 if (sts)
1586 dev_warn(&applespi->spi->dev,
1587 "Error queueing async read to device: %d\n",
1588 sts);
1589 else
1590 applespi->read_active = true;
1591 }
1592
1593 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
1594
1595 return ACPI_INTERRUPT_HANDLED;
1596 }
1597
1598 static int applespi_get_saved_bl_level(struct applespi_data *applespi)
1599 {
1600 efi_status_t sts = EFI_NOT_FOUND;
1601 u16 efi_data = 0;
1602 unsigned long efi_data_len = sizeof(efi_data);
1603
1604 if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
1605 sts = efi.get_variable(EFI_BL_LEVEL_NAME, &EFI_BL_LEVEL_GUID,
1606 NULL, &efi_data_len, &efi_data);
1607 if (sts != EFI_SUCCESS && sts != EFI_NOT_FOUND)
1608 dev_warn(&applespi->spi->dev,
1609 "Error getting backlight level from EFI vars: 0x%lx\n",
1610 sts);
1611
1612 return sts != EFI_SUCCESS ? -ENODEV : efi_data;
1613 }
1614
1615 static void applespi_save_bl_level(struct applespi_data *applespi,
1616 unsigned int level)
1617 {
1618 efi_status_t sts = EFI_UNSUPPORTED;
1619 u32 efi_attr;
1620 u16 efi_data;
1621
1622 efi_data = (u16)level;
1623 efi_attr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |
1624 EFI_VARIABLE_RUNTIME_ACCESS;
1625
1626 if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE))
1627 sts = efi.set_variable(EFI_BL_LEVEL_NAME, &EFI_BL_LEVEL_GUID,
1628 efi_attr, sizeof(efi_data), &efi_data);
1629 if (sts != EFI_SUCCESS)
1630 dev_warn(&applespi->spi->dev,
1631 "Error saving backlight level to EFI vars: 0x%lx\n", sts);
1632 }
1633
1634 static int applespi_probe(struct spi_device *spi)
1635 {
1636 struct applespi_data *applespi;
1637 acpi_handle spi_handle = ACPI_HANDLE(&spi->dev);
1638 acpi_status acpi_sts;
1639 int sts, i;
1640 unsigned long long gpe, usb_status;
1641
1642
1643 acpi_sts = acpi_evaluate_integer(spi_handle, "UIST", NULL, &usb_status);
1644 if (ACPI_SUCCESS(acpi_sts) && usb_status) {
1645
1646 dev_info(&spi->dev, "USB interface already enabled\n");
1647 return -ENODEV;
1648 }
1649
1650
1651 applespi = devm_kzalloc(&spi->dev, sizeof(*applespi), GFP_KERNEL);
1652 if (!applespi)
1653 return -ENOMEM;
1654
1655 applespi->spi = spi;
1656
1657 INIT_WORK(&applespi->work, applespi_worker);
1658
1659
1660 spi_set_drvdata(spi, applespi);
1661
1662
1663 applespi->tx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE,
1664 GFP_KERNEL);
1665 applespi->tx_status = devm_kmalloc(&spi->dev, APPLESPI_STATUS_SIZE,
1666 GFP_KERNEL);
1667 applespi->rx_buffer = devm_kmalloc(&spi->dev, APPLESPI_PACKET_SIZE,
1668 GFP_KERNEL);
1669 applespi->msg_buf = devm_kmalloc_array(&spi->dev, MAX_PKTS_PER_MSG,
1670 APPLESPI_PACKET_SIZE,
1671 GFP_KERNEL);
1672
1673 if (!applespi->tx_buffer || !applespi->tx_status ||
1674 !applespi->rx_buffer || !applespi->msg_buf)
1675 return -ENOMEM;
1676
1677
1678 applespi_setup_read_txfrs(applespi);
1679 applespi_setup_write_txfrs(applespi);
1680
1681
1682 acpi_sts = acpi_get_handle(spi_handle, "SIEN", &applespi->sien);
1683 if (ACPI_FAILURE(acpi_sts)) {
1684 dev_err(&applespi->spi->dev,
1685 "Failed to get SIEN ACPI method handle: %s\n",
1686 acpi_format_exception(acpi_sts));
1687 return -ENODEV;
1688 }
1689
1690 acpi_sts = acpi_get_handle(spi_handle, "SIST", &applespi->sist);
1691 if (ACPI_FAILURE(acpi_sts)) {
1692 dev_err(&applespi->spi->dev,
1693 "Failed to get SIST ACPI method handle: %s\n",
1694 acpi_format_exception(acpi_sts));
1695 return -ENODEV;
1696 }
1697
1698
1699 sts = applespi_setup_spi(applespi);
1700 if (sts)
1701 return sts;
1702
1703 sts = applespi_enable_spi(applespi);
1704 if (sts)
1705 return sts;
1706
1707
1708 applespi->keyboard_input_dev = devm_input_allocate_device(&spi->dev);
1709
1710 if (!applespi->keyboard_input_dev)
1711 return -ENOMEM;
1712
1713 applespi->keyboard_input_dev->name = "Apple SPI Keyboard";
1714 applespi->keyboard_input_dev->phys = "applespi/input0";
1715 applespi->keyboard_input_dev->dev.parent = &spi->dev;
1716 applespi->keyboard_input_dev->id.bustype = BUS_SPI;
1717
1718 applespi->keyboard_input_dev->evbit[0] =
1719 BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) | BIT_MASK(EV_REP);
1720 applespi->keyboard_input_dev->ledbit[0] = BIT_MASK(LED_CAPSL);
1721
1722 input_set_drvdata(applespi->keyboard_input_dev, applespi);
1723 applespi->keyboard_input_dev->event = applespi_event;
1724
1725 for (i = 0; i < ARRAY_SIZE(applespi_scancodes); i++)
1726 if (applespi_scancodes[i])
1727 input_set_capability(applespi->keyboard_input_dev,
1728 EV_KEY, applespi_scancodes[i]);
1729
1730 for (i = 0; i < ARRAY_SIZE(applespi_controlcodes); i++)
1731 if (applespi_controlcodes[i])
1732 input_set_capability(applespi->keyboard_input_dev,
1733 EV_KEY, applespi_controlcodes[i]);
1734
1735 for (i = 0; i < ARRAY_SIZE(applespi_fn_codes); i++)
1736 if (applespi_fn_codes[i].to)
1737 input_set_capability(applespi->keyboard_input_dev,
1738 EV_KEY, applespi_fn_codes[i].to);
1739
1740 input_set_capability(applespi->keyboard_input_dev, EV_KEY, KEY_FN);
1741
1742 sts = input_register_device(applespi->keyboard_input_dev);
1743 if (sts) {
1744 dev_err(&applespi->spi->dev,
1745 "Unable to register keyboard input device (%d)\n", sts);
1746 return -ENODEV;
1747 }
1748
1749
1750
1751
1752
1753 acpi_sts = acpi_evaluate_integer(spi_handle, "_GPE", NULL, &gpe);
1754 if (ACPI_FAILURE(acpi_sts)) {
1755 dev_err(&applespi->spi->dev,
1756 "Failed to obtain GPE for SPI slave device: %s\n",
1757 acpi_format_exception(acpi_sts));
1758 return -ENODEV;
1759 }
1760 applespi->gpe = (int)gpe;
1761
1762 acpi_sts = acpi_install_gpe_handler(NULL, applespi->gpe,
1763 ACPI_GPE_LEVEL_TRIGGERED,
1764 applespi_notify, applespi);
1765 if (ACPI_FAILURE(acpi_sts)) {
1766 dev_err(&applespi->spi->dev,
1767 "Failed to install GPE handler for GPE %d: %s\n",
1768 applespi->gpe, acpi_format_exception(acpi_sts));
1769 return -ENODEV;
1770 }
1771
1772 applespi->suspended = false;
1773
1774 acpi_sts = acpi_enable_gpe(NULL, applespi->gpe);
1775 if (ACPI_FAILURE(acpi_sts)) {
1776 dev_err(&applespi->spi->dev,
1777 "Failed to enable GPE handler for GPE %d: %s\n",
1778 applespi->gpe, acpi_format_exception(acpi_sts));
1779 acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify);
1780 return -ENODEV;
1781 }
1782
1783
1784 applespi_init(applespi, false);
1785
1786
1787
1788
1789
1790
1791 device_wakeup_enable(&spi->dev);
1792
1793
1794 sts = applespi_get_saved_bl_level(applespi);
1795 if (sts >= 0)
1796 applespi_set_bl_level(&applespi->backlight_info, sts);
1797
1798 applespi->backlight_info.name = "spi::kbd_backlight";
1799 applespi->backlight_info.default_trigger = "kbd-backlight";
1800 applespi->backlight_info.brightness_set = applespi_set_bl_level;
1801
1802 sts = devm_led_classdev_register(&spi->dev, &applespi->backlight_info);
1803 if (sts)
1804 dev_warn(&applespi->spi->dev,
1805 "Unable to register keyboard backlight class dev (%d)\n",
1806 sts);
1807
1808
1809 applespi->debugfs_root = debugfs_create_dir("applespi", NULL);
1810
1811 debugfs_create_bool("enable_tp_dim", 0600, applespi->debugfs_root,
1812 &applespi->debug_tp_dim);
1813
1814 debugfs_create_file("tp_dim", 0400, applespi->debugfs_root, applespi,
1815 &applespi_tp_dim_fops);
1816
1817 return 0;
1818 }
1819
1820 static void applespi_drain_writes(struct applespi_data *applespi)
1821 {
1822 unsigned long flags;
1823
1824 spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
1825
1826 applespi->drain = true;
1827 wait_event_lock_irq(applespi->drain_complete, !applespi->write_active,
1828 applespi->cmd_msg_lock);
1829
1830 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
1831 }
1832
1833 static void applespi_drain_reads(struct applespi_data *applespi)
1834 {
1835 unsigned long flags;
1836
1837 spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
1838
1839 wait_event_lock_irq(applespi->drain_complete, !applespi->read_active,
1840 applespi->cmd_msg_lock);
1841
1842 applespi->suspended = true;
1843
1844 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
1845 }
1846
1847 static void applespi_remove(struct spi_device *spi)
1848 {
1849 struct applespi_data *applespi = spi_get_drvdata(spi);
1850
1851 applespi_drain_writes(applespi);
1852
1853 acpi_disable_gpe(NULL, applespi->gpe);
1854 acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify);
1855 device_wakeup_disable(&spi->dev);
1856
1857 applespi_drain_reads(applespi);
1858
1859 debugfs_remove_recursive(applespi->debugfs_root);
1860 }
1861
1862 static void applespi_shutdown(struct spi_device *spi)
1863 {
1864 struct applespi_data *applespi = spi_get_drvdata(spi);
1865
1866 applespi_save_bl_level(applespi, applespi->have_bl_level);
1867 }
1868
1869 static int applespi_poweroff_late(struct device *dev)
1870 {
1871 struct spi_device *spi = to_spi_device(dev);
1872 struct applespi_data *applespi = spi_get_drvdata(spi);
1873
1874 applespi_save_bl_level(applespi, applespi->have_bl_level);
1875
1876 return 0;
1877 }
1878
1879 static int __maybe_unused applespi_suspend(struct device *dev)
1880 {
1881 struct spi_device *spi = to_spi_device(dev);
1882 struct applespi_data *applespi = spi_get_drvdata(spi);
1883 acpi_status acpi_sts;
1884 int sts;
1885
1886
1887 sts = applespi_set_capsl_led(applespi, false);
1888 if (sts)
1889 dev_warn(&applespi->spi->dev,
1890 "Failed to turn off caps-lock led (%d)\n", sts);
1891
1892 applespi_drain_writes(applespi);
1893
1894
1895 acpi_sts = acpi_disable_gpe(NULL, applespi->gpe);
1896 if (ACPI_FAILURE(acpi_sts))
1897 dev_err(&applespi->spi->dev,
1898 "Failed to disable GPE handler for GPE %d: %s\n",
1899 applespi->gpe, acpi_format_exception(acpi_sts));
1900
1901 applespi_drain_reads(applespi);
1902
1903 return 0;
1904 }
1905
1906 static int __maybe_unused applespi_resume(struct device *dev)
1907 {
1908 struct spi_device *spi = to_spi_device(dev);
1909 struct applespi_data *applespi = spi_get_drvdata(spi);
1910 acpi_status acpi_sts;
1911 unsigned long flags;
1912
1913
1914 spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
1915
1916 applespi->drain = false;
1917 applespi->have_cl_led_on = false;
1918 applespi->have_bl_level = 0;
1919 applespi->cmd_msg_queued = 0;
1920 applespi->read_active = false;
1921 applespi->write_active = false;
1922
1923 applespi->suspended = false;
1924
1925 spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
1926
1927
1928 applespi_enable_spi(applespi);
1929
1930
1931 acpi_sts = acpi_enable_gpe(NULL, applespi->gpe);
1932 if (ACPI_FAILURE(acpi_sts))
1933 dev_err(&applespi->spi->dev,
1934 "Failed to re-enable GPE handler for GPE %d: %s\n",
1935 applespi->gpe, acpi_format_exception(acpi_sts));
1936
1937
1938 applespi_init(applespi, true);
1939
1940 return 0;
1941 }
1942
1943 static const struct acpi_device_id applespi_acpi_match[] = {
1944 { "APP000D", 0 },
1945 { }
1946 };
1947 MODULE_DEVICE_TABLE(acpi, applespi_acpi_match);
1948
1949 static const struct dev_pm_ops applespi_pm_ops = {
1950 SET_SYSTEM_SLEEP_PM_OPS(applespi_suspend, applespi_resume)
1951 .poweroff_late = applespi_poweroff_late,
1952 };
1953
1954 static struct spi_driver applespi_driver = {
1955 .driver = {
1956 .name = "applespi",
1957 .acpi_match_table = applespi_acpi_match,
1958 .pm = &applespi_pm_ops,
1959 },
1960 .probe = applespi_probe,
1961 .remove = applespi_remove,
1962 .shutdown = applespi_shutdown,
1963 };
1964
1965 module_spi_driver(applespi_driver)
1966
1967 MODULE_LICENSE("GPL v2");
1968 MODULE_DESCRIPTION("MacBook(Pro) SPI Keyboard/Touchpad driver");
1969 MODULE_AUTHOR("Federico Lorenzi");
1970 MODULE_AUTHOR("Ronald Tschalär");