Path: blob/master/drivers/input/touchscreen/hampshire.c
15111 views
/*1* Hampshire serial touchscreen driver2*3* Copyright (c) 2010 Adam Bennett4* Based on the dynapro driver (c) Tias Guns5*6*/78/*9* This program is free software; you can redistribute it and/or modify it10* under the terms of the GNU General Public License version 2 as published by11* the Free Software Foundation.12*/1314/*15* 2010/04/08 Adam Bennett <[email protected]>16* Copied dynapro.c and edited for Hampshire 4-byte protocol17*/1819#include <linux/errno.h>20#include <linux/kernel.h>21#include <linux/module.h>22#include <linux/slab.h>23#include <linux/input.h>24#include <linux/serio.h>25#include <linux/init.h>2627#define DRIVER_DESC "Hampshire serial touchscreen driver"2829MODULE_AUTHOR("Adam Bennett <[email protected]>");30MODULE_DESCRIPTION(DRIVER_DESC);31MODULE_LICENSE("GPL");3233/*34* Definitions & global arrays.35*/3637#define HAMPSHIRE_FORMAT_TOUCH_BIT 0x4038#define HAMPSHIRE_FORMAT_LENGTH 439#define HAMPSHIRE_RESPONSE_BEGIN_BYTE 0x804041#define HAMPSHIRE_MIN_XC 042#define HAMPSHIRE_MAX_XC 0x100043#define HAMPSHIRE_MIN_YC 044#define HAMPSHIRE_MAX_YC 0x10004546#define HAMPSHIRE_GET_XC(data) (((data[3] & 0x0c) >> 2) | (data[1] << 2) | ((data[0] & 0x38) << 6))47#define HAMPSHIRE_GET_YC(data) ((data[3] & 0x03) | (data[2] << 2) | ((data[0] & 0x07) << 9))48#define HAMPSHIRE_GET_TOUCHED(data) (HAMPSHIRE_FORMAT_TOUCH_BIT & data[0])4950/*51* Per-touchscreen data.52*/5354struct hampshire {55struct input_dev *dev;56struct serio *serio;57int idx;58unsigned char data[HAMPSHIRE_FORMAT_LENGTH];59char phys[32];60};6162static void hampshire_process_data(struct hampshire *phampshire)63{64struct input_dev *dev = phampshire->dev;6566if (HAMPSHIRE_FORMAT_LENGTH == ++phampshire->idx) {67input_report_abs(dev, ABS_X, HAMPSHIRE_GET_XC(phampshire->data));68input_report_abs(dev, ABS_Y, HAMPSHIRE_GET_YC(phampshire->data));69input_report_key(dev, BTN_TOUCH,70HAMPSHIRE_GET_TOUCHED(phampshire->data));71input_sync(dev);7273phampshire->idx = 0;74}75}7677static irqreturn_t hampshire_interrupt(struct serio *serio,78unsigned char data, unsigned int flags)79{80struct hampshire *phampshire = serio_get_drvdata(serio);8182phampshire->data[phampshire->idx] = data;8384if (HAMPSHIRE_RESPONSE_BEGIN_BYTE & phampshire->data[0])85hampshire_process_data(phampshire);86else87dev_dbg(&serio->dev, "unknown/unsynchronized data: %x\n",88phampshire->data[0]);8990return IRQ_HANDLED;91}9293static void hampshire_disconnect(struct serio *serio)94{95struct hampshire *phampshire = serio_get_drvdata(serio);9697input_get_device(phampshire->dev);98input_unregister_device(phampshire->dev);99serio_close(serio);100serio_set_drvdata(serio, NULL);101input_put_device(phampshire->dev);102kfree(phampshire);103}104105/*106* hampshire_connect() is the routine that is called when someone adds a107* new serio device that supports hampshire protocol and registers it as108* an input device. This is usually accomplished using inputattach.109*/110111static int hampshire_connect(struct serio *serio, struct serio_driver *drv)112{113struct hampshire *phampshire;114struct input_dev *input_dev;115int err;116117phampshire = kzalloc(sizeof(struct hampshire), GFP_KERNEL);118input_dev = input_allocate_device();119if (!phampshire || !input_dev) {120err = -ENOMEM;121goto fail1;122}123124phampshire->serio = serio;125phampshire->dev = input_dev;126snprintf(phampshire->phys, sizeof(phampshire->phys),127"%s/input0", serio->phys);128129input_dev->name = "Hampshire Serial TouchScreen";130input_dev->phys = phampshire->phys;131input_dev->id.bustype = BUS_RS232;132input_dev->id.vendor = SERIO_HAMPSHIRE;133input_dev->id.product = 0;134input_dev->id.version = 0x0001;135input_dev->dev.parent = &serio->dev;136input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);137input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);138input_set_abs_params(phampshire->dev, ABS_X,139HAMPSHIRE_MIN_XC, HAMPSHIRE_MAX_XC, 0, 0);140input_set_abs_params(phampshire->dev, ABS_Y,141HAMPSHIRE_MIN_YC, HAMPSHIRE_MAX_YC, 0, 0);142143serio_set_drvdata(serio, phampshire);144145err = serio_open(serio, drv);146if (err)147goto fail2;148149err = input_register_device(phampshire->dev);150if (err)151goto fail3;152153return 0;154155fail3: serio_close(serio);156fail2: serio_set_drvdata(serio, NULL);157fail1: input_free_device(input_dev);158kfree(phampshire);159return err;160}161162/*163* The serio driver structure.164*/165166static struct serio_device_id hampshire_serio_ids[] = {167{168.type = SERIO_RS232,169.proto = SERIO_HAMPSHIRE,170.id = SERIO_ANY,171.extra = SERIO_ANY,172},173{ 0 }174};175176MODULE_DEVICE_TABLE(serio, hampshire_serio_ids);177178static struct serio_driver hampshire_drv = {179.driver = {180.name = "hampshire",181},182.description = DRIVER_DESC,183.id_table = hampshire_serio_ids,184.interrupt = hampshire_interrupt,185.connect = hampshire_connect,186.disconnect = hampshire_disconnect,187};188189/*190* The functions for inserting/removing us as a module.191*/192193static int __init hampshire_init(void)194{195return serio_register_driver(&hampshire_drv);196}197198static void __exit hampshire_exit(void)199{200serio_unregister_driver(&hampshire_drv);201}202203module_init(hampshire_init);204module_exit(hampshire_exit);205206207