blob: 27ae809edd7050ed4a84e1ea41a98039592d24c0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 1999-2002 Vojtech Pavlik
3*
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 */
David Howells607ca462012-10-13 10:46:48 +01008#ifndef _SERIO_H
9#define _SERIO_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070012#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/interrupt.h>
14#include <linux/list.h>
15#include <linux/spinlock.h>
Arjan van de Venc4e32e92006-02-19 00:21:55 -050016#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/device.h>
18#include <linux/mod_devicetable.h>
David Howells607ca462012-10-13 10:46:48 +010019#include <uapi/linux/serio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21struct serio {
22 void *port_data;
23
24 char name[32];
25 char phys[32];
Hans de Goede0456c662014-04-19 20:39:35 -070026 char firmware_id[128];
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070028 bool manual_bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30 struct serio_device_id id;
31
Dmitry Torokhovb5e8e7f2016-07-25 11:36:54 -070032 /* Protects critical sections from port's interrupt handler */
33 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 int (*write)(struct serio *, unsigned char);
36 int (*open)(struct serio *);
37 void (*close)(struct serio *);
38 int (*start)(struct serio *);
39 void (*stop)(struct serio *);
40
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -070041 struct serio *parent;
Dmitry Torokhovb5e8e7f2016-07-25 11:36:54 -070042 /* Entry in parent->children list */
43 struct list_head child_node;
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -070044 struct list_head children;
Dmitry Torokhovb5e8e7f2016-07-25 11:36:54 -070045 /* Level of nesting in serio hierarchy */
46 unsigned int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Dmitry Torokhovb5e8e7f2016-07-25 11:36:54 -070048 /*
49 * serio->drv is accessed from interrupt handlers; when modifying
50 * caller should acquire serio->drv_mutex and serio->lock.
51 */
52 struct serio_driver *drv;
53 /* Protects serio->drv so attributes can pin current driver */
54 struct mutex drv_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 struct list_head node;
Dmitry Torokhovb5e8e7f2016-07-25 11:36:54 -070059
60 /*
61 * For use by PS/2 layer when several ports share hardware and
62 * may get indigestion when exposed to concurrent access (i8042).
63 */
64 struct mutex *ps2_cmd_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66#define to_serio_port(d) container_of(d, struct serio, dev)
67
68struct serio_driver {
Dmitry Torokhovceee4272010-09-13 23:53:55 -070069 const char *description;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Dmitry Torokhovceee4272010-09-13 23:53:55 -070071 const struct serio_device_id *id_table;
Dmitry Torokhov7e044e02009-05-09 16:08:05 -070072 bool manual_bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 void (*write_wakeup)(struct serio *);
David Howells7d12e782006-10-05 14:55:46 +010075 irqreturn_t (*interrupt)(struct serio *, unsigned char, unsigned int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 int (*connect)(struct serio *, struct serio_driver *drv);
77 int (*reconnect)(struct serio *);
78 void (*disconnect)(struct serio *);
79 void (*cleanup)(struct serio *);
80
81 struct device_driver driver;
82};
83#define to_serio_driver(d) container_of(d, struct serio_driver, driver)
84
85int serio_open(struct serio *serio, struct serio_driver *drv);
86void serio_close(struct serio *serio);
87void serio_rescan(struct serio *serio);
88void serio_reconnect(struct serio *serio);
David Howells7d12e782006-10-05 14:55:46 +010089irqreturn_t serio_interrupt(struct serio *serio, unsigned char data, unsigned int flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91void __serio_register_port(struct serio *serio, struct module *owner);
Paul Gortmakereb5589a2011-05-27 09:02:11 -040092
93/* use a define to avoid include chaining to get THIS_MODULE */
94#define serio_register_port(serio) \
95 __serio_register_port(serio, THIS_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97void serio_unregister_port(struct serio *serio);
Dmitry Torokhovdbf4ccd2005-06-01 02:40:01 -050098void serio_unregister_child_port(struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Paul Gortmakereb5589a2011-05-27 09:02:11 -0400100int __must_check __serio_register_driver(struct serio_driver *drv,
101 struct module *owner, const char *mod_name);
102
103/* use a define to avoid include chaining to get THIS_MODULE & friends */
104#define serio_register_driver(drv) \
105 __serio_register_driver(drv, THIS_MODULE, KBUILD_MODNAME)
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107void serio_unregister_driver(struct serio_driver *drv);
108
Axel Linfa7f86d2012-04-03 23:50:15 -0700109/**
110 * module_serio_driver() - Helper macro for registering a serio driver
111 * @__serio_driver: serio_driver struct
112 *
113 * Helper macro for serio drivers which do not do anything special in
114 * module init/exit. This eliminates a lot of boilerplate. Each module
115 * may only use this macro once, and calling it replaces module_init()
116 * and module_exit().
117 */
118#define module_serio_driver(__serio_driver) \
119 module_driver(__serio_driver, serio_register_driver, \
120 serio_unregister_driver)
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static inline int serio_write(struct serio *serio, unsigned char data)
123{
124 if (serio->write)
125 return serio->write(serio, data);
126 else
127 return -1;
128}
129
130static inline void serio_drv_write_wakeup(struct serio *serio)
131{
132 if (serio->drv && serio->drv->write_wakeup)
133 serio->drv->write_wakeup(serio);
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*
Akinobu Mita0b280022006-03-26 01:38:58 -0800137 * Use the following functions to manipulate serio's per-port
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * driver-specific data.
139 */
140static inline void *serio_get_drvdata(struct serio *serio)
141{
142 return dev_get_drvdata(&serio->dev);
143}
144
145static inline void serio_set_drvdata(struct serio *serio, void *data)
146{
147 dev_set_drvdata(&serio->dev, data);
148}
149
150/*
Akinobu Mita0b280022006-03-26 01:38:58 -0800151 * Use the following functions to protect critical sections in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * driver code from port's interrupt handler
153 */
154static inline void serio_pause_rx(struct serio *serio)
155{
156 spin_lock_irq(&serio->lock);
157}
158
159static inline void serio_continue_rx(struct serio *serio)
160{
161 spin_unlock_irq(&serio->lock);
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#endif