cregit-Linux how code gets into the kernel

Release 4.11 drivers/input/rmi4/rmi_driver.c

/*
 * Copyright (c) 2011-2016 Synaptics Incorporated
 * Copyright (c) 2011 Unixphere
 *
 * This driver provides the core support for a single RMI4-based device.
 *
 * The RMI4 specification can be found here (URL split for line length):
 *
 * http://www.synaptics.com/sites/default/files/
 *      511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
 *
 * 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/bitmap.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/irq.h>
#include <linux/pm.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <uapi/linux/input.h>
#include <linux/rmi.h>
#include "rmi_bus.h"
#include "rmi_driver.h"


#define HAS_NONSTANDARD_PDT_MASK 0x40

#define RMI4_MAX_PAGE 0xff

#define RMI4_PAGE_SIZE 0x100

#define RMI4_PAGE_MASK 0xFF00


#define RMI_DEVICE_RESET_CMD	0x01

#define DEFAULT_RESET_DELAY_MS	100


void rmi_free_function_list(struct rmi_device *rmi_dev) { struct rmi_function *fn, *tmp; struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Freeing function list\n"); devm_kfree(&rmi_dev->dev, data->irq_memory); data->irq_memory = NULL; data->irq_status = NULL; data->fn_irq_bits = NULL; data->current_irq_mask = NULL; data->new_irq_mask = NULL; data->f01_container = NULL; data->f34_container = NULL; /* Doing it in the reverse order so F01 will be removed last */ list_for_each_entry_safe_reverse(fn, tmp, &data->function_list, node) { list_del(&fn->node); rmi_unregister_function(fn); } }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan6351.22%133.33%
Nick Dyer6048.78%266.67%
Total123100.00%3100.00%


static int reset_one_function(struct rmi_function *fn) { struct rmi_function_handler *fh; int retval = 0; if (!fn || !fn->dev.driver) return 0; fh = to_rmi_function_handler(fn->dev.driver); if (fh->reset) { retval = fh->reset(fn); if (retval < 0) dev_err(&fn->dev, "Reset failed with code %d.\n", retval); } return retval; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan85100.00%1100.00%
Total85100.00%1100.00%


static int configure_one_function(struct rmi_function *fn) { struct rmi_function_handler *fh; int retval = 0; if (!fn || !fn->dev.driver) return 0; fh = to_rmi_function_handler(fn->dev.driver); if (fh->config) { retval = fh->config(fn); if (retval < 0) dev_err(&fn->dev, "Config failed with code %d.\n", retval); } return retval; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan85100.00%1100.00%
Total85100.00%1100.00%


static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev) { struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); struct rmi_function *entry; int retval; list_for_each_entry(entry, &data->function_list, node) { retval = reset_one_function(entry); if (retval < 0) return retval; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan61100.00%1100.00%
Total61100.00%1100.00%


static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev) { struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); struct rmi_function *entry; int retval; list_for_each_entry(entry, &data->function_list, node) { retval = configure_one_function(entry); if (retval < 0) return retval; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan61100.00%1100.00%
Total61100.00%1100.00%


static void process_one_interrupt(struct rmi_driver_data *data, struct rmi_function *fn) { struct rmi_function_handler *fh; if (!fn || !fn->dev.driver) return; fh = to_rmi_function_handler(fn->dev.driver); if (fh->attention) { bitmap_and(data->fn_irq_bits, data->irq_status, fn->irq_mask, data->irq_count); if (!bitmap_empty(data->fn_irq_bits, data->irq_count)) fh->attention(fn, data->fn_irq_bits); } }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan97100.00%1100.00%
Total97100.00%1100.00%


static int rmi_process_interrupt_requests(struct rmi_device *rmi_dev) { struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); struct device *dev = &rmi_dev->dev; struct rmi_function *entry; int error; if (!data) return 0; if (!data->attn_data.data) { error = rmi_read_block(rmi_dev, data->f01_container->fd.data_base_addr + 1, data->irq_status, data->num_of_irq_regs); if (error < 0) { dev_err(dev, "Failed to read irqs, code=%d\n", error); return error; } } mutex_lock(&data->irq_mutex); bitmap_and(data->irq_status, data->irq_status, data->current_irq_mask, data->irq_count); /* * At this point, irq_status has all bits that are set in the * interrupt status register and are enabled. */ mutex_unlock(&data->irq_mutex); /* * It would be nice to be able to use irq_chip to handle these * nested IRQs. Unfortunately, most of the current customers for * this driver are using older kernels (3.0.x) that don't support * the features required for that. Once they've shifted to more * recent kernels (say, 3.3 and higher), this should be switched to * use irq_chip. */ list_for_each_entry(entry, &data->function_list, node) process_one_interrupt(data, entry); if (data->input) input_sync(data->input); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan17097.70%133.33%
Benjamin Tissoires31.72%133.33%
Björn Andersson10.57%133.33%
Total174100.00%3100.00%


void rmi_set_attn_data(struct rmi_device *rmi_dev, unsigned long irq_status, void *data, size_t size) { struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev); struct rmi4_attn_data attn_data; void *fifo_data; if (!drvdata->enabled) return; fifo_data = kmemdup(data, size, GFP_ATOMIC); if (!fifo_data) return; attn_data.irq_status = irq_status; attn_data.size = size; attn_data.data = fifo_data; kfifo_put(&drvdata->attn_fifo, attn_data); }

Contributors

PersonTokensPropCommitsCommitProp
Benjamin Tissoires6568.42%150.00%
Andrew Duggan3031.58%150.00%
Total95100.00%2100.00%

EXPORT_SYMBOL_GPL(rmi_set_attn_data);
static irqreturn_t rmi_irq_fn(int irq, void *dev_id) { struct rmi_device *rmi_dev = dev_id; struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev); struct rmi4_attn_data attn_data = {0}; int ret, count; count = kfifo_get(&drvdata->attn_fifo, &attn_data); if (count) { *(drvdata->irq_status) = attn_data.irq_status; drvdata->attn_data = attn_data; } ret = rmi_process_interrupt_requests(rmi_dev); if (ret) rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Failed to process interrupt request: %d\n", ret); if (count) kfree(attn_data.data); if (!kfifo_is_empty(&drvdata->attn_fifo)) return rmi_irq_fn(irq, dev_id); return IRQ_HANDLED; }

Contributors

PersonTokensPropCommitsCommitProp
Benjamin Tissoires7050.00%250.00%
Björn Andersson3625.71%125.00%
Andrew Duggan3424.29%125.00%
Total140100.00%4100.00%


static int rmi_irq_init(struct rmi_device *rmi_dev) { struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev); struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); int irq_flags = irq_get_trigger_type(pdata->irq); int ret; if (!irq_flags) irq_flags = IRQF_TRIGGER_LOW; ret = devm_request_threaded_irq(&rmi_dev->dev, pdata->irq, NULL, rmi_irq_fn, irq_flags | IRQF_ONESHOT, dev_name(rmi_dev->xport->dev), rmi_dev); if (ret < 0) { dev_err(&rmi_dev->dev, "Failed to register interrupt %d\n", pdata->irq); return ret; } data->enabled = true; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Björn Andersson9879.67%133.33%
Benjamin Tissoires1915.45%133.33%
Andrew Duggan64.88%133.33%
Total123100.00%3100.00%


struct rmi_function *rmi_find_function(struct rmi_device *rmi_dev, u8 number) { struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); struct rmi_function *entry; list_for_each_entry(entry, &data->function_list, node) { if (entry->fd.function_number == number) return entry; } return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Benjamin Tissoires59100.00%1100.00%
Total59100.00%1100.00%


static int suspend_one_function(struct rmi_function *fn) { struct rmi_function_handler *fh; int retval = 0; if (!fn || !fn->dev.driver) return 0; fh = to_rmi_function_handler(fn->dev.driver); if (fh->suspend) { retval = fh->suspend(fn); if (retval < 0) dev_err(&fn->dev, "Suspend failed with code %d.\n", retval); } return retval; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan85100.00%1100.00%
Total85100.00%1100.00%


static int rmi_suspend_functions(struct rmi_device *rmi_dev) { struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); struct rmi_function *entry; int retval; list_for_each_entry(entry, &data->function_list, node) { retval = suspend_one_function(entry); if (retval < 0) return retval; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan61100.00%1100.00%
Total61100.00%1100.00%


static int resume_one_function(struct rmi_function *fn) { struct rmi_function_handler *fh; int retval = 0; if (!fn || !fn->dev.driver) return 0; fh = to_rmi_function_handler(fn->dev.driver); if (fh->resume) { retval = fh->resume(fn); if (retval < 0) dev_err(&fn->dev, "Resume failed with code %d.\n", retval); } return retval; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan85100.00%1100.00%
Total85100.00%1100.00%


static int rmi_resume_functions(struct rmi_device *rmi_dev) { struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); struct rmi_function *entry; int retval; list_for_each_entry(entry, &data->function_list, node) { retval = resume_one_function(entry); if (retval < 0) return retval; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan61100.00%1100.00%
Total61100.00%1100.00%


int rmi_enable_sensor(struct rmi_device *rmi_dev) { int retval = 0; retval = rmi_driver_process_config_requests(rmi_dev); if (retval < 0) return retval; return rmi_process_interrupt_requests(rmi_dev); }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan3697.30%150.00%
Nick Dyer12.70%150.00%
Total37100.00%2100.00%

/** * rmi_driver_set_input_params - set input device id and other data. * * @rmi_dev: Pointer to an RMI device * @input: Pointer to input device * */
static int rmi_driver_set_input_params(struct rmi_device *rmi_dev, struct input_dev *input) { input->name = SYNAPTICS_INPUT_DEVICE_NAME; input->id.vendor = SYNAPTICS_VENDOR_ID; input->id.bustype = BUS_RMI; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan41100.00%1100.00%
Total41100.00%1100.00%


static void rmi_driver_set_input_name(struct rmi_device *rmi_dev, struct input_dev *input) { struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); const char *device_name = rmi_f01_get_product_ID(data->f01_container); char *name; name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL, "Synaptics %s", device_name); if (!name) return; input->name = name; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan7298.63%150.00%
Nick Dyer11.37%150.00%
Total73100.00%2100.00%


static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev, unsigned long *mask) { int error = 0; struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); struct device *dev = &rmi_dev->dev; mutex_lock(&data->irq_mutex); bitmap_or(data->new_irq_mask, data->current_irq_mask, mask, data->irq_count); error = rmi_write_block(rmi_dev, data->f01_container->fd.control_base_addr + 1, data->new_irq_mask, data->num_of_irq_regs); if (error < 0) { dev_err(dev, "%s: Failed to change enabled interrupts!", __func__); goto error_unlock; } bitmap_copy(data->current_irq_mask, data->new_irq_mask, data->num_of_irq_regs); error_unlock: mutex_unlock(&data->irq_mutex); return error; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan142100.00%1100.00%
Total142100.00%1100.00%


static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev, unsigned long *mask) { int error = 0; struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); struct device *dev = &rmi_dev->dev; mutex_lock(&data->irq_mutex); bitmap_andnot(data->new_irq_mask, data->current_irq_mask, mask, data->irq_count); error = rmi_write_block(rmi_dev, data->f01_container->fd.control_base_addr + 1, data->new_irq_mask, data->num_of_irq_regs); if (error < 0) { dev_err(dev, "%s: Failed to change enabled interrupts!", __func__); goto error_unlock; } bitmap_copy(data->current_irq_mask, data->new_irq_mask, data->num_of_irq_regs); error_unlock: mutex_unlock(&data->irq_mutex); return error; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan142100.00%1100.00%
Total142100.00%1100.00%


static int rmi_driver_reset_handler(struct rmi_device *rmi_dev) { struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); int error; /* * Can get called before the driver is fully ready to deal with * this situation. */ if (!data || !data->f01_container) { dev_warn(&rmi_dev->dev, "Not ready to handle reset yet!\n"); return 0; } error = rmi_read_block(rmi_dev, data->f01_container->fd.control_base_addr + 1, data->current_irq_mask, data->num_of_irq_regs); if (error < 0) { dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n", __func__); return error; } error = rmi_driver_process_reset_requests(rmi_dev); if (error < 0) return error; error = rmi_driver_process_config_requests(rmi_dev); if (error < 0) return error; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan136100.00%1100.00%
Total136100.00%1100.00%


static int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry, u16 pdt_address) { u8 buf[RMI_PDT_ENTRY_SIZE]; int error; error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE); if (error) { dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n", pdt_address, error); return error; } entry->page_start = pdt_address & RMI4_PAGE_MASK; entry->query_base_addr = buf[0]; entry->command_base_addr = buf[1]; entry->control_base_addr = buf[2]; entry->data_base_addr = buf[3]; entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK; entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5; entry->function_number = buf[5]; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan14599.32%150.00%
Benjamin Tissoires10.68%150.00%
Total146100.00%2100.00%


static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt, struct rmi_function_descriptor *fd) { fd->query_base_addr = pdt->query_base_addr + pdt->page_start; fd->command_base_addr = pdt->command_base_addr + pdt->page_start; fd->control_base_addr = pdt->control_base_addr + pdt->page_start; fd->data_base_addr = pdt->data_base_addr + pdt->page_start; fd->function_number = pdt->function_number; fd->interrupt_source_count = pdt->interrupt_source_count; fd->function_version = pdt->function_version; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan89100.00%1100.00%
Total89100.00%1100.00%

#define RMI_SCAN_CONTINUE 0 #define RMI_SCAN_DONE 1
static int rmi_scan_pdt_page(struct rmi_device *rmi_dev, int page, int *empty_pages, void *ctx, int (*callback)(struct rmi_device *rmi_dev, void *ctx, const struct pdt_entry *entry)) { struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev); struct pdt_entry pdt_entry; u16 page_start = RMI4_PAGE_SIZE * page; u16 pdt_start = page_start + PDT_START_SCAN_LOCATION; u16 pdt_end = page_start + PDT_END_SCAN_LOCATION; u16 addr; int error; int retval; for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) { error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr); if (error) return error; if (RMI4_END_OF_PDT(pdt_entry.function_number)) break; retval = callback(rmi_dev, ctx, &pdt_entry); if (retval != RMI_SCAN_CONTINUE) return retval; } /* * Count number of empty PDT pages. If a gap of two pages * or more is found, stop scanning. */ if (addr == pdt_start) ++*empty_pages; else *empty_pages = 0; return (data->bootloader_mode || *empty_pages >= 2) ? RMI_SCAN_DONE : RMI_SCAN_CONTINUE; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan16486.32%133.33%
Nick Dyer2613.68%266.67%
Total190100.00%3100.00%


int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx, int (*callback)(struct rmi_device *rmi_dev, void *ctx, const struct pdt_entry *entry)) { int page; int empty_pages = 0; int retval = RMI_SCAN_DONE; for (page = 0; page <= RMI4_MAX_PAGE; page++) { retval = rmi_scan_pdt_page(rmi_dev, page, &empty_pages, ctx, callback); if (retval != RMI_SCAN_CONTINUE) break; } return retval < 0 ? retval : 0; }

Contributors

PersonTokensPropCommitsCommitProp
Andrew Duggan8891.67%150.00%
Nick Dyer88.33%150.00%
Total96100.00%2100.00%


int rmi_read_register_desc(struct rmi_device *d, u16 addr, struct rmi_register_descriptor *rdesc) { int ret; u8 size_presence_reg; u8 buf[35]; int presense_offset = 1; u8 *struct_buf; int reg; int offset = 0; int map_offset = 0; int i; int b; /* * The first register of the register descriptor is the size of * the register descriptor's presense register. */ ret = rmi_read(d, addr, &size_presence_reg); if (ret) return ret; ++addr; if (size_presence_reg < 0 || size_presence_reg > 35) return -EIO; memset(buf, 0, sizeof(buf)); /* * The presence register contains the size of the register structure * and a bitmap which identified which packet registers are present * for this particular register type (ie query, control, or data). */ ret = rmi_read_block(d, addr, buf, size_presence_reg); if (ret) return ret; ++addr; if (buf[0] == 0) { presense_offset = 3; rdesc->struct_size = buf[1] | (buf[2] << 8); } else { rdesc->struct_size = buf[0]; } for (i = presense_offset; i < size_presence_reg; i++) { for (b = 0; b < 8; b++) { if (buf[i] & (0x1 << b)) bitmap_set(rdesc->presense_map, map_offset, 1); ++map_offset; } } rdesc->num_registers = bitmap_weight(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS); rdesc->registers = devm_kzalloc(&d->dev, rdesc->num_registers * sizeof(struct rmi_register_desc_item), GFP_KERNEL); if (!rdesc->registers) return -ENOMEM; /* * Allocate a temporary buffer to hold the register structure. * I'm not using devm_kzalloc here since it will not be retained * after exiting this function */ struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL); if (!struct_buf) return -ENOMEM; /* * The register structure contains information about every packet * register of this type. This includes the size of the packet * register and a bitmap of all subpackets contained in the packet * register. */ ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size); if (ret) goto free_struct_buff; reg = find_first_bit