Skip to content

Commit

Permalink
applespi: Make touchpad-init command async like other commands.
Browse files Browse the repository at this point in the history
This simplifies the code overall and ensures the contraints on commands
and response are fullfilled for the init command(s) too.
  • Loading branch information
roadrunner2 committed Aug 28, 2017
1 parent 3a87751 commit 43c7341
Showing 1 changed file with 41 additions and 65 deletions.
106 changes: 41 additions & 65 deletions applespi.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ struct applespi_data {
struct spi_transfer st_t;
struct spi_message wr_m;

int init_cmd_idx;
bool want_cl_led_on;
bool have_cl_led_on;
unsigned want_bl_level;
Expand Down Expand Up @@ -440,22 +441,6 @@ applespi_setup_spi_message(struct spi_message *message, int num_txfrs, ...)
va_end(txfrs);
}

static int
applespi_sync(struct applespi_data *applespi, struct spi_message *message)
{
struct spi_device *spi;
int status;

spi = applespi->spi;

status = spi_sync(spi, message);

if (status == 0)
status = message->actual_length;

return status;
}

static int
applespi_async(struct applespi_data *applespi, struct spi_message *message,
void (*complete)(void *))
Expand Down Expand Up @@ -485,38 +470,6 @@ applespi_check_write_status(struct applespi_data *applespi, int sts)
return ret;
}

static inline ssize_t
applespi_sync_write(struct applespi_data *applespi, unsigned log_mask)
{
/*
The Windows driver always seems to do a 256 byte write, followed
by a 4 byte read with CS still the same, followed by a toggling of
CS and a 256 byte read for the real response. We will get the real
response on a read after an interrupt.
*/
struct spi_transfer t1;
struct spi_transfer t2;
struct spi_transfer t3;
struct spi_message m;
ssize_t ret;

applespi_setup_write_txfr(applespi, &t1, &t2, &t3);
applespi_setup_spi_message(&m, 3, &t1, &t2, &t3);

ret = applespi_sync(applespi, &m);

debug_print(log_mask, "--- %s ---------------------------\n",
applespi_debug_facility(log_mask));
debug_print_buffer(log_mask, "write ", applespi->tx_buffer,
APPLESPI_PACKET_SIZE);
debug_print_buffer(log_mask, "status ", applespi->tx_status,
APPLESPI_STATUS_SIZE);

applespi_check_write_status(applespi, ret);

return ret;
}

static int applespi_find_settings_field(const char *name)
{
int i;
Expand Down Expand Up @@ -636,22 +589,6 @@ static int applespi_enable_spi(struct applespi_data *applespi)
return 0;
}

static void applespi_init(struct applespi_data *applespi)
{
int i;
ssize_t items = ARRAY_SIZE(applespi_init_commands);

applespi->cmd_log_mask = DBG_CMD_TP_INI;
applespi->cmd_msg_queued = true;

for (i=0; i < items; i++) {
memcpy(applespi->tx_buffer, applespi_init_commands[i], 256);
applespi_sync_write(applespi, DBG_CMD_TP_INI);
}

pr_info("modeswitch done.\n");
}

static int applespi_send_cmd_msg(struct applespi_data *applespi);

static void applespi_cmd_msg_complete(struct applespi_data *applespi)
Expand Down Expand Up @@ -691,8 +628,20 @@ applespi_send_cmd_msg(struct applespi_data *applespi)
if (applespi->cmd_msg_queued)
return 0;

/* are we processing init commands? */
if (applespi->init_cmd_idx >= 0) {
memcpy(applespi->tx_buffer,
applespi_init_commands[applespi->init_cmd_idx],
APPLESPI_PACKET_SIZE);

applespi->init_cmd_idx++;
if (applespi->init_cmd_idx >= ARRAY_SIZE(applespi_init_commands))
applespi->init_cmd_idx = -1;

applespi->cmd_log_mask = DBG_CMD_TP_INI;

/* do we need caps-lock command? */
if (applespi->want_cl_led_on != applespi->have_cl_led_on) {
} else if (applespi->want_cl_led_on != applespi->have_cl_led_on) {
applespi->have_cl_led_on = applespi->want_cl_led_on;
applespi->cmd_log_mask = DBG_CMD_CL;

Expand Down Expand Up @@ -755,6 +704,18 @@ applespi_send_cmd_msg(struct applespi_data *applespi)
return sts;
}

static void applespi_init(struct applespi_data *applespi)
{
unsigned long flags;

spin_lock_irqsave(&applespi->cmd_msg_lock, flags);

applespi->init_cmd_idx = 0;
applespi_send_cmd_msg(applespi);

spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
}

static int
applespi_set_capsl_led(struct applespi_data *applespi, bool capslock_on)
{
Expand Down Expand Up @@ -994,6 +955,16 @@ applespi_handle_keyboard_event(struct applespi_data *applespi,
memcpy(&applespi->last_keys_pressed, keyboard_protocol->keys_pressed, sizeof(applespi->last_keys_pressed));
}

static void
applespi_handle_cmd_response(struct applespi_data *applespi,
struct keyboard_protocol *keyboard_protocol)
{
if (keyboard_protocol->device == PACKET_DEV_TPAD &&
memcmp(((u8 *) keyboard_protocol) + 8,
applespi_init_commands[0] + 8, 4) == 0)
pr_info("modeswitch done.\n");
}

static void
applespi_got_data(struct applespi_data *applespi)
{
Expand Down Expand Up @@ -1025,6 +996,7 @@ applespi_got_data(struct applespi_data *applespi)
debug_print_buffer(applespi->cmd_log_mask, "read ",
applespi->rx_buffer, APPLESPI_PACKET_SIZE);

applespi_handle_cmd_response(applespi, keyboard_protocol);
} else {
debug_print(DBG_RD_UNKN, "--- %s ---------------------------\n",
applespi_debug_facility(DBG_RD_UNKN));
Expand Down Expand Up @@ -1057,14 +1029,18 @@ static void applespi_async_read_complete(void *context)
static u32 applespi_notify(acpi_handle gpe_device, u32 gpe, void *context)
{
struct applespi_data *applespi = context;
unsigned long flags;

debug_print(DBG_RD_IRQ, "--- %s ---------------------------\n",
applespi_debug_facility(DBG_RD_IRQ));

applespi_setup_read_txfr(applespi, &applespi->dl_t, &applespi->rd_t);
applespi_setup_spi_message(&applespi->rd_m, 2, &applespi->dl_t, &applespi->rd_t);

spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
applespi_async(applespi, &applespi->rd_m, applespi_async_read_complete);
spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);

return ACPI_INTERRUPT_HANDLED;
}

Expand Down

0 comments on commit 43c7341

Please sign in to comment.