/*
* Cypress Trackpad PS/2 mouse driver
*
* Copyright (c) 2012 Cypress Semiconductor Corporation.
*
* Author:
* Dudley Du <dudl@cypress.com>
*
* Additional contributors include:
* Kamal Mostafa <kamal@canonical.com>
* Kyle Fazzari <git@status.e4ward.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/serio.h>
#include <linux/libps2.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include "cypress_ps2.h"
#undef CYTP_DEBUG_VERBOSE /* define this and DEBUG for more verbose dump */
static void cypress_set_packet_size(struct psmouse *psmouse, unsigned int n)
{
struct cytp_data *cytp = psmouse->private;
cytp->pkt_size = n;
}
static const unsigned char cytp_rate[] = {10, 20, 40, 60, 100, 200};
static const unsigned char cytp_resolution[] = {0x00, 0x01, 0x02, 0x03};
static int cypress_ps2_sendbyte(struct psmouse *psmouse, int value)
{
struct ps2dev *ps2dev = &psmouse->ps2dev;
if (ps2_sendbyte(ps2dev, value & 0xff, CYTP_CMD_TIMEOUT) < 0) {
psmouse_dbg(psmouse,
"sending command 0x%02x failed, resp 0x%02x\n",
value & 0xff, ps2dev->nak);
if (ps2dev->nak == CYTP_PS2_RETRY)
return CYTP_PS2_RETRY;
else
return CYTP_PS2_ERROR;
}
#ifdef CYTP_DEBUG_VERBOSE
psmouse_dbg(psmouse, "sending command 0x%02x succeeded, resp 0xfa\n",
value & 0xff);
#endif
return 0;
}
static int cypress_ps2_ext_cmd(struct psmouse *psmouse, unsigned short cmd,
unsigned char data)
{
struct ps2dev *ps2dev = &psmouse->ps2dev;
int tries = CYTP_PS2_CMD_TRIES;
int rc;
ps2_begin_command(ps2dev);
do {
/*
* Send extension command byte (0xE8 or 0xF3).
* If sending the command fails, send recovery command
* to make the device return to the ready state.
*/
rc = cypress_ps2_sendbyte(psmouse, cmd & 0xff);
if (rc == CYTP_PS2_RETRY) {
rc = cypress_ps2_sendbyte(psmouse, 0x00);
if (rc == CYTP_PS2_RETRY)
rc = cypress_ps2_sendbyte(psmouse, 0x0a);
}
if (rc == CYTP_PS2_ERROR)
continue;
rc = cypress_ps2_sendbyte(psmouse, data);
if (rc == CYTP_PS2_RETRY)
rc = cypress_ps2_sendbyte(psmouse, data);
if (rc == CYTP_PS2_ERROR)
continue;
else
break;
} while (--tries > 0);
ps2_end_command(ps2dev);
return rc;
}
static int cypress_ps2_read_cmd_status(struct psmouse *psmouse,
unsigned char cmd,
unsigned char *param)
{
int rc;
struct ps2dev *ps2dev = &psmouse->ps2dev;
enum psmouse_state old_state;
int pktsize;
ps2_begin_command(&psmouse->ps2dev);
old_state = psmouse->state;
psmouse->state = PSMOUSE_CMD_MODE;
psmouse->pktcnt = 0;
pktsize = (cmd == CYTP_CMD_READ_TP_METRICS) ? 8 : 3;
memset(param, 0, pktsize);
rc = cypress_ps2_sendbyte(psmouse, 0xe9);
if (rc < 0)
goto out;
wait_event_timeout(ps2dev->wait,
(psmouse->pktcnt >= pktsize),
msecs_to_jiffies(CYTP_CMD_TIMEOUT));
memcpy(param, psmouse->packet, pktsize);
psmouse_dbg(psmouse, "Command 0x%02x response data (0x): %*ph\n",
cmd, pktsize, param);
out:
psmouse->state = old_state;
psmouse->pktcnt = 0;
ps2_end_command(&psmouse->ps2dev);
return rc;
}
static bool cypress_verify_cmd_state(struct psmouse *psmouse,
unsigned char cmd, unsigned char *param)
{
bool rate_match = false;
bool resolution_match = false;
int i;
/* callers will do further checking. */
if (cmd == CYTP_CMD_READ_CYPRESS_ID ||
cmd == CYTP_CMD_STANDARD_MODE ||
cmd == CYTP_CMD_READ_TP_METRICS)
return true;
if ((~param[0] & DFLT_RESP_BITS_VALID) == DFLT_RESP_BITS_VALID &&
(param[0] & DFLT_RESP_BIT_MODE) == DFLT_RESP_STREAM_MODE) {
for (i = 0; i < sizeof(cytp_resolution); i++)
if (cytp_resolution[i] == param[1])
resolution_match = true;
for (i = 0; i < sizeof(cytp_rate); i++)
if (cytp_rate[i] == param[2])
rate_match = true;
if (resolution_match && rate_match)
return true;
}
psmouse_dbg(psmouse, "verify cmd state failed.\n");
return false;
}
static int cypress_send_ext_cmd(struct psmouse *psmouse, unsigned char cmd,
unsigned char *param)
{
int tries = CYTP_PS2_CMD_TRIES;
int rc;
psmouse_dbg(psmouse, "send extension cmd 0x%02x, [%d %d %d %d]\n",
cmd, DECODE_CMD_AA(cmd), DECODE_CMD_BB(cmd),
DECODE_CMD_CC(cmd), DECODE_CMD_DD(cmd));
do {
cypress_ps2_ext_cmd(psmouse,
PSMOUSE_CMD_SETRES, DECODE_CMD_DD(cmd));
cypress_ps2_ext_cmd(psmouse,
PSMOUSE_CMD_SETRES, DECODE_CMD_CC(cmd));
cypress_ps2_ext_cmd(psmouse,
PSMOUSE_CMD_SETRES, DECODE_CMD_BB(cmd));
cypress_ps2_ext_cmd(psmouse,
PSMOUSE_CMD_SETRES, DECODE_CMD_AA(cmd));
rc = cypress_ps2_read_cmd_status(psmouse, cmd, param);
if (rc)
continue;
if (cypress_verify_cmd_state(psmouse, cmd, param))
return 0;
} while (--tries > 0);
return -EIO;
}
int cypress_detect(struct psmouse *psmouse, bool set_properties)
{
unsigned char param[3];
if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
return -ENODEV;
/* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
if (param[0] != 0x33 || param[1] != 0xCC)
return -ENODEV;
if (set_properties) {
psmouse->vendor = "Cypress";
psmouse->name = "Trackpad";
}
return 0;
}
static int cypress_read_fw_version(struct psmouse *psmouse)
{
struct cytp_data *cytp = psmouse->private;
unsigned char param[3];
if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_CYPRESS_ID, param))
return -ENODEV;
/* Check for Cypress Trackpad signature bytes: 0x33 0xCC */
if (param[0] != 0x33 || param[1] != 0xCC)
return -ENODEV;
cytp->fw_version = param[2] & FW_VERSION_MASX;
cytp->tp_metrics_supported = (param[2] & TP_METRICS_MASK) ? 1 : 0;
/*
* Trackpad fw_version 11 (in Dell XPS12) yields a bogus response to
* CYTP_CMD_READ_TP_METRICS so do not try to use it. LP: #1103594.
*/
if (cytp->fw_version >= 11)
cytp->tp_metrics_supported = 0;
psmouse_dbg(psmouse, "cytp->fw_version = %d\n", cytp->fw_version);
psmouse_dbg(psmouse, "cytp->tp_metrics_supported = %d\n",
cytp->tp_metrics_supported);
return 0;
}
static int cypress_read_tp_metrics(struct psmouse *psmouse)
{
struct cytp_data *cytp = psmouse->private;
unsigned char param[8];
/* set default values for tp metrics. */
cytp->tp_width = CYTP_DEFAULT_WIDTH;
cytp->tp_high = CYTP_DEFAULT_HIGH;
cytp->tp_max_abs_x = CYTP_ABS_MAX_X;
cytp->tp_max_abs_y = CYTP_ABS_MAX_Y;
cytp->tp_min_pressure = CYTP_MIN_PRESSURE;
cytp->tp_max_pressure = CYTP_MAX_PRESSURE;
cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
if (!cytp->tp_metrics_supported)
return 0;
memset(param, 0, sizeof(param));
if (cypress_send_ext_cmd(psmouse, CYTP_CMD_READ_TP_METRICS, param) == 0) {
/* Update trackpad parameters. */
cytp->tp_max_abs_x = (param[1] << 8) | param[0];
cytp->tp_max_abs_y = (param[3] << 8) | param[2];
cytp->tp_min_pressure = param[4];
cytp->tp_max_pressure = param[5];
}
if (!cytp->tp_max_pressure ||
cytp->tp_max_pressure < cytp->tp_min_pressure ||
!cytp->tp_width || !cytp->tp_high ||
!cytp->tp_max_abs_x ||
cytp->tp_max_abs_x < cytp->tp_width ||
!cytp->tp_max_abs_y ||
cytp->tp_max_abs_y < cytp->tp_high)
return -EINVAL;
cytp->tp_res_x = cytp->tp_max_abs_x / cytp->tp_width;
cytp->tp_res_y = cytp->tp_max_abs_y / cytp->tp_high;
#ifdef CYTP_DEBUG_VERBOSE
psmouse_dbg(psmouse, "Dump trackpad hardware configuration as below:\n");
psmouse_dbg(psmouse, "cytp->tp_width = %d\n", cytp->tp_width);
psmouse_dbg(psmouse, "cytp->tp_high = %d\n", cytp->tp_high);
psmouse_dbg(psmouse, "cytp->tp_max_abs_x = %d\n", cytp->tp_max_abs_x);
psmouse_dbg(psmouse, "cytp->tp_max_abs_y = %d\n", cytp->tp_max_abs_y);
psmouse_dbg(psmouse, "cytp->tp_min_pressure = %d\n"