/*
* Copyright (c) 2013 Andrew Duggan <aduggan@synaptics.com>
* Copyright (c) 2013 Synaptics Incorporated
* Copyright (c) 2014 Benjamin Tissoires <benjamin.tissoires@gmail.com>
* Copyright (c) 2014 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#include <linux/kernel.h>
#include <linux/hid.h>
#include <linux/input.h>
#include <linux/input/mt.h>
#include <linux/module.h>
#include <linux/pm.h>
#include <linux/slab.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include "hid-ids.h"
#define RMI_MOUSE_REPORT_ID 0x01 /* Mouse emulation Report */
#define RMI_WRITE_REPORT_ID 0x09 /* Output Report */
#define RMI_READ_ADDR_REPORT_ID 0x0a /* Output Report */
#define RMI_READ_DATA_REPORT_ID 0x0b /* Input Report */
#define RMI_ATTN_REPORT_ID 0x0c /* Input Report */
#define RMI_SET_RMI_MODE_REPORT_ID 0x0f /* Feature Report */
/* flags */
#define RMI_READ_REQUEST_PENDING BIT(0)
#define RMI_READ_DATA_PENDING BIT(1)
#define RMI_STARTED BIT(2)
/* device flags */
#define RMI_DEVICE BIT(0)
#define RMI_DEVICE_HAS_PHYS_BUTTONS BIT(1)
enum rmi_mode_type {
RMI_MODE_OFF = 0,
RMI_MODE_ATTN_REPORTS = 1,
RMI_MODE_NO_PACKED_ATTN_REPORTS = 2,
};
struct rmi_function {
unsigned page; /* page of the function */
u16 query_base_addr; /* base address for queries */
u16 command_base_addr; /* base address for commands */
u16 control_base_addr; /* base address for controls */
u16 data_base_addr; /* base address for datas */
unsigned int interrupt_base; /* cross-function interrupt number
* (uniq in the device)*/
unsigned int interrupt_count; /* number of interrupts */
unsigned int report_size; /* size of a report */
unsigned long irq_mask; /* mask of the interrupts
* (to be applied against ATTN IRQ) */
};
/**
* struct rmi_data - stores information for hid communication
*
* @page_mutex: Locks current page to avoid changing pages in unexpected ways.
* @page: Keeps track of the current virtual page
*
* @wait: Used for waiting for read data
*
* @writeReport: output buffer when writing RMI registers
* @readReport: input buffer when reading RMI registers
*
* @input_report_size: size of an input report (advertised by HID)
* @output_report_size: size of an output report (advertised by HID)
*
* @flags: flags for the current device (started, reading, etc...)
*
* @f11: placeholder of internal RMI function F11 description
* @f30: placeholder of internal RMI function F30 description
*
* @max_fingers: maximum finger count reported by the device
* @max_x: maximum x value reported by the device
* @max_y: maximum y value reported by the device
*
* @gpio_led_count: count of GPIOs + LEDs reported by F30
* @button_count: actual physical buttons count
* @button_mask: button mask used to decode GPIO ATTN reports
* @button_state_mask: pull state of the buttons
*
* @input: pointer to the kernel input device
*
* @reset_work: worker which will be called in case of a mouse report
* @hdev: pointer to the struct hid_device
*/
struct rmi_data {
struct mutex page_mutex;
int page;
wait_queue_head_t wait;
u8 *writeReport;
u8 *readReport;
int input_report_size;
int output_report_size;
unsigned long flags;
struct rmi_function f11;
struct rmi_function f30;
unsigned int max_fingers;
unsigned int max_x;
unsigned int max_y;
unsigned int x_size_mm;
unsigned int y_size_mm;
unsigned int gpio_led_count;
unsigned int button_count;
unsigned long button_mask;
unsigned long button_state_mask;
struct input_dev *input;
struct work_struct reset_work;
struct hid_device *hdev;
unsigned long device_flags;
};
#define RMI_PAGE(addr) (((addr) >> 8) & 0xff)
static int rmi_write_report(struct hid_device *hdev, u8 *report, int len);
/**
* rmi_set_page - Set RMI page
* @hdev: The pointer to the hid_device struct
* @page: The new page address.
*
* RMI devices have 16-bit addressing, but some of the physical
* implementations (like SMBus) only have 8-bit addressing. So RMI implements
* a page address at 0xff of every page so we can reliable page addresses
* every 256 registers.
*
* The page_mutex lock must be held when this function is entered.
*
* Returns zero on success, non-zero on failure.
*/
static int rmi_set_page(struct hid_device *hdev, u8 page)
{
struct rmi_data *data = hid_get_drvdata(hdev);
int retval;
data->writeReport[0] = RMI_WRITE_REPORT_ID;
data->writeReport[1] = 1;
data->writeReport[2] = 0xFF;
data->writeReport[4] = page;
retval = rmi_write_report(hdev, data->writeReport,
data->output_report_size);
if (retval != data->output_report_size) {
dev_err(&hdev->dev,
"%s: set page failed: %d.", __func__, retval);
return retval;
}
data->page = page;
return 0;
}
static int rmi_set_mode(struct hid_device *hdev, u8 mode)
{
int ret;
u8 txbuf[2] = {RMI_SET_RMI_MODE_REPORT_ID,