diff options
| author | Linus Torvalds <torvalds@linux-foundation.org> | 2026-09-13 12:27:00 -0700 |
|---|---|---|
| committer | Linus Torvalds <torvalds@linux-foundation.org> | 2026-09-13 12:27:00 -0700 |
| commit | 22098763a10d9c1340827fcf6edab66f153b27f0 (patch) | |
| tree | 3962f95a34df034652ff93686cdd156c7197f2d9 /drivers/input/gameport | |
| download | linux-stable-22098763a10d9c1340827fcf6edab66f153b27f0.tar.gz linux-stable-22098763a10d9c1340827fcf6edab66f153b27f0.zip | |
Merge tag 'trace-v7.3-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-tracegrafted
Pull tracing fixes from Steven Rostedt:
- Don't destroy user event fields when removal fails
User event fields are destroyed before the event is removed from
visibility. But that can fail leaving the still visible event with no
fields. Move the destroying of the fields to after the event is
successfully removed from visibility.
- Initialize function graph state is fork before calling
copy_exec_state()
For non-CLONE_VM forks, copy_exec_state() allocates a new
task_exec_state. If that allocation fails, ftrace_graph_exit_task()
will free the tasks ret_stack pointer. Since that pointer is still
using the parent's ret_stack, it mistakenly frees the parent's
pointer too.
Call ftrace_graph_init() on the task first which will NULL out the
new tasks's ret_stack and if the copy fails, it will not free
anything.
- Remove FGRAPH_MAX_INDEX
The macro FGRAPH_MAX_INDEX was added but never used. Remove it.
- Save ent_size in function graph printing of nested functions
The function graph tracer needs to look at the next event to see if
the next event is the return of the current function entry. If it is,
it prints a single line:
ktime_get();
Otherwise it prints it like a nested function:
tick_nohz_irq_exit() {
ktime_get();
kcpustat_irq_exit();
}
In order to look at the next event, it must save the current event so
that it has the information to print from it. It saves the event in
the iterator descriptor called "ent". What it doesn't save is the
ent_size of the event which is now used to know if the function graph
arguments are to be printed. The peek doesn't save the size so the
size used happens to be that of the size of the last event that was
seen.
Save the entry event size in the iterator descriptor so that the
correct size is used.
- Fix several errors with freeing data in the histogram code
The histogram code had a lot of leaked or or incorrect accounting
when failures happen. Correct them.
- Fix histogram regression of .percent and .graph modifiers
Up until 6.3 histogram values could have "percent" or "graph"
modifiers that changed how they were printed. But a change that added
restricting histograms values from being strings, stack traces and
other modifiers inadvertently prevented them from using the percent
and graph modifiers, which were legal use cases for values.
Put back the percent and graph modifiers.
- Fix various typos in the comments
- Set the trace_clock before initializing a histogram with clock
argument
The histogram API allows the user to specific which trace clock to
use via a "clock=" string. The histogram is set up first before the
clock is checked. If the passed in clock is not valid, it exits
without fully fixing up the histogram leaving it on the list and a
use-after-free can trigger.
Update the clock argument first and if it fails then exit gracefully
before the histogram trigger is placed on any lists.
- Restore :mod: trailer after parsing in ftrace_set_clr_event
The function ftrace_set_clr_event() modifies the parse string and
needs to put it back to what was passed in. It searches for ":mod:"
via a strsep() but fails to put back the first ':' in the string.
Add back the ':' in the passed in string.
- Take trace_array reference when opening a tracer options file
The options files are dynamically created and some tracers add their
own options. When a tracer adds their own list of options, the
trace_array holding them has an array to hold the list of options for
each tracer. This array increases in size via a krealloc(), and the
new entry gets a newly allocated array to hold the options of the new
tracer being added.
The element in each entry of the tracer's option array holds a
pointer back to the trace_array, a pointer to the tracer it is
associated to, a pointer to the flags of the option.
The issue is that these arrays are freed when the trace_array is
freed when its instance it represents is removed from the instances
directory. There's a race that an open of one of these options files
can happen when the instance is being removed.
Add a new helper function to be called by the open function of the
options file to iterate all existing trace_arrays under a lock and
find the one that has the given option element in one of it's tracer
arrays. If found, then update the associated trace_array's reference
counter to keep it from being freed. If not found, have the open call
return -ENODEV.
- Disable interrupts when acquiring the lock in rb_wake_up_waiters()
The function rb_wake_up_waiters() assumes it will be called in
interrupt context and does not disable irqs when taking
cpu_buffer->reader_lock, which can be called in hard interrupt
context. The issue is in PREEMPT_RT, this function is called in
thread context leaving this lock open to a deadlock.
Take the lock with interrupts disabled.
- Use rcu_assign_pointer() for tmp_ops filter hash
The tmp_ops used in update_ftrace_direct_mod() assigns its
filter_hash field directly, but that field is annotated as __rcu and
sparse complains. Assign it with rcu_assign_pointer()
- Fix use-after-free in enable_trigger_private_data_free()
The trace_event_call is accessed through the event_trigger_data's
trace_event_file pointer to put the trace_event_call on freeing. The
issue is that the trace_event_file data may have been freed already
causing a use-after-free. Add a field to the event_trigger_data that
points directly to the trace_event_call so that it can decrement its
reference directly without needing to go through the
trace_event_file.
- Fix accounting of buffer data remote headers
trace_buffer_desc_size() and trace_remote_alloc_buffer() undercount
the number of pages is needed for the asked for size as it doesn't
take into account the meta data on each page. Add a helper function
to do the calculation properly and use that in these functions.
- Catch nr_page_va overflow in ring_buffer_desc sizing
The number of pages per remote ring buffer is capped by
ring_buffer_desc::nr_page_va (32 bits). A buffer_size large enough to
overflow that field would silently allocate a descriptor smaller than
what was asked for.
- Do not resize the subbuf order if any per_cpu buffer is disabled
The mmapping of ring buffers disables resizing the subbuffers, but it
is done per-cpu whereas the subbuf size change is done for all the
per_cpu buffers under the buffer->mutex. It could change the size of
some while the mapping is happening on others. Have the resize of the
subbuf order check all the per_cpu buffers under the lock to see if
any of them is disabled before starting and causing an inconsistency
between buffers that are being mapped.
* tag 'trace-v7.3-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: (25 commits)
ring-buffer: Check resize_disabled before publishing the new subbuf order
tracing/remotes: Catch nr_page_va overflow in ring_buffer_desc sizing
tracing/remotes: Account for ring buffer page header in size calculation
tracing: Don't dereference trace_event_file in deferred trigger free
ftrace: Use rcu_assign_pointer() for tmp_ops filter hash
ring-buffer: Acquire the lock with irqsave in rb_wake_up_waiters()
tracing: Take trace_array reference when opening a tracer options file
tracing: Fix ring_buffer_read_page_size() kernel-doc
tracing: Restore :mod: trailer after parsing in ftrace_set_clr_event()
tracing: Fix memory corruption from a "STACKTRACE" histogram key
tracing: Fix memory corruption from the histogram stacktrace modifier
tracing: Undo the registration when enabling the histogram trigger fails
tracing: Take the reference before publishing the named histogram trigger
tracing: Set the trace clock before registering the histogram trigger
tracing: Fix typo "preceeded" in comment
tracing: Fix typo "availabe" in comment
tracing: Let histogram values keep the percent and graph modifiers
tracing: Keep the entry count when the histogram stats allocation fails
tracing: Free histogram the field rejected for a bad modifier
tracing: Free histogram the var ref when its initialization fails
...
Diffstat (limited to 'drivers/input/gameport')
| -rw-r--r-- | drivers/input/gameport/Kconfig | 67 | ||||
| -rw-r--r-- | drivers/input/gameport/Makefile | 12 | ||||
| -rw-r--r-- | drivers/input/gameport/emu10k1-gp.c | 108 | ||||
| -rw-r--r-- | drivers/input/gameport/fm801-gp.c | 144 | ||||
| -rw-r--r-- | drivers/input/gameport/gameport.c | 887 | ||||
| -rw-r--r-- | drivers/input/gameport/lightning.c | 322 | ||||
| -rw-r--r-- | drivers/input/gameport/ns558.c | 267 |
7 files changed, 1807 insertions, 0 deletions
diff --git a/drivers/input/gameport/Kconfig b/drivers/input/gameport/Kconfig new file mode 100644 index 000000000..fe73b26e6 --- /dev/null +++ b/drivers/input/gameport/Kconfig @@ -0,0 +1,67 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Gameport configuration +# +config GAMEPORT + tristate "Gameport support" + depends on !UML + help + Gameport support is for the standard 15-pin PC gameport. If you + have a joystick, gamepad, gameport card, a soundcard with a gameport + or anything else that uses the gameport, say Y or M here and also to + at least one of the hardware specific drivers. + + For Ensoniq AudioPCI (ES1370), AudioPCI 97 (ES1371), ESS Solo1, + S3 SonicVibes, Trident 4DWave, SiS7018, and ALi 5451 gameport + support is provided by the sound drivers, so you won't need any + from the below listed modules. You still need to say Y here. + + If unsure, say Y. + + To compile this driver as a module, choose M here: the + module will be called gameport. + +if GAMEPORT + +config GAMEPORT_NS558 + tristate "Classic ISA and PnP gameport support" + depends on ISA + help + Say Y here if you have an ISA or PnP gameport. + + If unsure, say Y. + + To compile this driver as a module, choose M here: the + module will be called ns558. + +config GAMEPORT_L4 + tristate "PDPI Lightning 4 gamecard support" + depends on ISA + help + Say Y here if you have a PDPI Lightning 4 gamecard. + + To compile this driver as a module, choose M here: the + module will be called lightning. + +config GAMEPORT_EMU10K1 + tristate "SB Live and Audigy gameport support" + depends on PCI + help + Say Y here if you have a SoundBlaster Live! or SoundBlaster + Audigy card and want to use its gameport. + + To compile this driver as a module, choose M here: the + module will be called emu10k1-gp. + +config GAMEPORT_FM801 + tristate "ForteMedia FM801 gameport support" + depends on PCI && HAS_IOPORT + help + Say Y here if you have ForteMedia FM801 PCI audio controller + (Abit AU10, Genius Sound Maker, HP Workstation zx2000, + and others), and want to use its gameport. + + To compile this driver as a module, choose M here: the + module will be called fm801-gp. + +endif diff --git a/drivers/input/gameport/Makefile b/drivers/input/gameport/Makefile new file mode 100644 index 000000000..73ad8fe4d --- /dev/null +++ b/drivers/input/gameport/Makefile @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Makefile for the gameport drivers. +# + +# Each configuration option enables a list of files. + +obj-$(CONFIG_GAMEPORT) += gameport.o +obj-$(CONFIG_GAMEPORT_EMU10K1) += emu10k1-gp.o +obj-$(CONFIG_GAMEPORT_FM801) += fm801-gp.o +obj-$(CONFIG_GAMEPORT_L4) += lightning.o +obj-$(CONFIG_GAMEPORT_NS558) += ns558.o diff --git a/drivers/input/gameport/emu10k1-gp.c b/drivers/input/gameport/emu10k1-gp.c new file mode 100644 index 000000000..ee97621df --- /dev/null +++ b/drivers/input/gameport/emu10k1-gp.c @@ -0,0 +1,108 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2001 Vojtech Pavlik + */ + +/* + * EMU10k1 - SB Live / Audigy - gameport driver for Linux + */ + +#include <asm/io.h> + +#include <linux/module.h> +#include <linux/ioport.h> +#include <linux/gameport.h> +#include <linux/slab.h> +#include <linux/pci.h> + +MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); +MODULE_DESCRIPTION("EMU10k1 gameport driver"); +MODULE_LICENSE("GPL"); + +struct emu { + struct pci_dev *dev; + struct gameport *gameport; + int io; + int size; +}; + +static const struct pci_device_id emu_tbl[] = { + + { 0x1102, 0x7002, PCI_ANY_ID, PCI_ANY_ID }, /* SB Live gameport */ + { 0x1102, 0x7003, PCI_ANY_ID, PCI_ANY_ID }, /* Audigy gameport */ + { 0x1102, 0x7004, PCI_ANY_ID, PCI_ANY_ID }, /* Dell SB Live */ + { 0x1102, 0x7005, PCI_ANY_ID, PCI_ANY_ID }, /* Audigy LS gameport */ + { 0, } +}; + +MODULE_DEVICE_TABLE(pci, emu_tbl); + +static int emu_probe(struct pci_dev *pdev, const struct pci_device_id *ent) +{ + struct emu *emu; + struct gameport *port; + int error; + + emu = kzalloc_obj(*emu); + port = gameport_allocate_port(); + if (!emu || !port) { + printk(KERN_ERR "emu10k1-gp: Memory allocation failed\n"); + error = -ENOMEM; + goto err_out_free; + } + + error = pci_enable_device(pdev); + if (error) + goto err_out_free; + + emu->io = pci_resource_start(pdev, 0); + emu->size = pci_resource_len(pdev, 0); + + emu->dev = pdev; + emu->gameport = port; + + gameport_set_name(port, "EMU10K1"); + gameport_set_phys(port, "pci%s/gameport0", pci_name(pdev)); + port->dev.parent = &pdev->dev; + port->io = emu->io; + + if (!request_region(emu->io, emu->size, "emu10k1-gp")) { + printk(KERN_ERR "emu10k1-gp: unable to grab region 0x%x-0x%x\n", + emu->io, emu->io + emu->size - 1); + error = -EBUSY; + goto err_out_disable_dev; + } + + pci_set_drvdata(pdev, emu); + + gameport_register_port(port); + + return 0; + + err_out_disable_dev: + pci_disable_device(pdev); + err_out_free: + gameport_free_port(port); + kfree(emu); + return error; +} + +static void emu_remove(struct pci_dev *pdev) +{ + struct emu *emu = pci_get_drvdata(pdev); + + gameport_unregister_port(emu->gameport); + release_region(emu->io, emu->size); + kfree(emu); + + pci_disable_device(pdev); +} + +static struct pci_driver emu_driver = { + .name = "Emu10k1_gameport", + .id_table = emu_tbl, + .probe = emu_probe, + .remove = emu_remove, +}; + +module_pci_driver(emu_driver); diff --git a/drivers/input/gameport/fm801-gp.c b/drivers/input/gameport/fm801-gp.c new file mode 100644 index 000000000..1e8c6c044 --- /dev/null +++ b/drivers/input/gameport/fm801-gp.c @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * FM801 gameport driver for Linux + * + * Copyright (c) by Takashi Iwai <tiwai@suse.de> + */ + +#include <asm/io.h> +#include <linux/delay.h> +#include <linux/errno.h> +#include <linux/ioport.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/pci.h> +#include <linux/slab.h> +#include <linux/gameport.h> + +#define PCI_VENDOR_ID_FORTEMEDIA 0x1319 +#define PCI_DEVICE_ID_FM801_GP 0x0802 + +#define HAVE_COOKED + +struct fm801_gp { + struct gameport *gameport; + struct resource *res_port; +}; + +#ifdef HAVE_COOKED +static int fm801_gp_cooked_read(struct gameport *gameport, int *axes, int *buttons) +{ + unsigned short w; + + w = inw(gameport->io + 2); + *buttons = (~w >> 14) & 0x03; + axes[0] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5); + w = inw(gameport->io + 4); + axes[1] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5); + w = inw(gameport->io + 6); + *buttons |= ((~w >> 14) & 0x03) << 2; + axes[2] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5); + w = inw(gameport->io + 8); + axes[3] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5); + outw(0xff, gameport->io); /* reset */ + + return 0; +} +#endif + +static int fm801_gp_open(struct gameport *gameport, int mode) +{ + switch (mode) { +#ifdef HAVE_COOKED + case GAMEPORT_MODE_COOKED: + return 0; +#endif + case GAMEPORT_MODE_RAW: + return 0; + default: + return -1; + } + + return 0; +} + +static int fm801_gp_probe(struct pci_dev *pci, const struct pci_device_id *id) +{ + struct fm801_gp *gp; + struct gameport *port; + int error; + + gp = kzalloc_obj(*gp); + port = gameport_allocate_port(); + if (!gp || !port) { + printk(KERN_ERR "fm801-gp: Memory allocation failed\n"); + error = -ENOMEM; + goto err_out_free; + } + + error = pci_enable_device(pci); + if (error) + goto err_out_free; + + port->open = fm801_gp_open; +#ifdef HAVE_COOKED + port->cooked_read = fm801_gp_cooked_read; +#endif + gameport_set_name(port, "FM801"); + gameport_set_phys(port, "pci%s/gameport0", pci_name(pci)); + port->dev.parent = &pci->dev; + port->io = pci_resource_start(pci, 0); + + gp->gameport = port; + gp->res_port = request_region(port->io, 0x10, "FM801 GP"); + if (!gp->res_port) { + printk(KERN_DEBUG "fm801-gp: unable to grab region 0x%x-0x%x\n", + port->io, port->io + 0x0f); + error = -EBUSY; + goto err_out_disable_dev; + } + + pci_set_drvdata(pci, gp); + + outb(0x60, port->io + 0x0d); /* enable joystick 1 and 2 */ + gameport_register_port(port); + + return 0; + + err_out_disable_dev: + pci_disable_device(pci); + err_out_free: + gameport_free_port(port); + kfree(gp); + return error; +} + +static void fm801_gp_remove(struct pci_dev *pci) +{ + struct fm801_gp *gp = pci_get_drvdata(pci); + + gameport_unregister_port(gp->gameport); + release_resource(gp->res_port); + kfree(gp); + + pci_disable_device(pci); +} + +static const struct pci_device_id fm801_gp_id_table[] = { + { PCI_VDEVICE(FORTEMEDIA, PCI_DEVICE_ID_FM801_GP) }, + { } +}; +MODULE_DEVICE_TABLE(pci, fm801_gp_id_table); + +static struct pci_driver fm801_gp_driver = { + .name = "FM801_gameport", + .id_table = fm801_gp_id_table, + .probe = fm801_gp_probe, + .remove = fm801_gp_remove, +}; + +module_pci_driver(fm801_gp_driver); + +MODULE_DESCRIPTION("FM801 gameport driver"); +MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); +MODULE_LICENSE("GPL"); diff --git a/drivers/input/gameport/gameport.c b/drivers/input/gameport/gameport.c new file mode 100644 index 000000000..9707b155b --- /dev/null +++ b/drivers/input/gameport/gameport.c @@ -0,0 +1,887 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Generic gameport layer + * + * Copyright (c) 1999-2002 Vojtech Pavlik + * Copyright (c) 2005 Dmitry Torokhov + */ + + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include <linux/export.h> +#include <linux/stddef.h> +#include <linux/module.h> +#include <linux/io.h> +#include <linux/ioport.h> +#include <linux/init.h> +#include <linux/gameport.h> +#include <linux/slab.h> +#include <linux/delay.h> +#include <linux/workqueue.h> +#include <linux/sched.h> /* HZ */ +#include <linux/mutex.h> +#include <linux/timekeeping.h> + +MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); +MODULE_DESCRIPTION("Generic gameport layer"); +MODULE_LICENSE("GPL"); + +static bool use_ktime = true; +module_param(use_ktime, bool, 0400); +MODULE_PARM_DESC(use_ktime, "Use ktime for measuring I/O speed"); + +/* + * gameport_mutex protects entire gameport subsystem and is taken + * every time gameport port or driver registrered or unregistered. + */ +static DEFINE_MUTEX(gameport_mutex); + +static LIST_HEAD(gameport_list); + +static const struct bus_type gameport_bus; + +static void gameport_add_port(struct gameport *gameport); +static void gameport_attach_driver(struct gameport_driver *drv); +static void gameport_reconnect_port(struct gameport *gameport); +static void gameport_disconnect_port(struct gameport *gameport); + +#if defined(__i386__) + +#include <linux/i8253.h> + +#define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0)) +#define GET_TIME(x) do { x = get_time_pit(); } while (0) + +static unsigned int get_time_pit(void) +{ + unsigned long flags; + unsigned int count; + + raw_spin_lock_irqsave(&i8253_lock, flags); + outb_p(0x00, 0x43); + count = inb_p(0x40); + count |= inb_p(0x40) << 8; + raw_spin_unlock_irqrestore(&i8253_lock, flags); + + return count; +} + +#endif + + + +/* + * gameport_measure_speed() measures the gameport i/o speed. + */ + +static int gameport_measure_speed(struct gameport *gameport) +{ + unsigned int i, t, tx; + u64 t1, t2, t3; + unsigned long flags; + + if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW)) + return 0; + + tx = ~0; + + for (i = 0; i < 50; i++) { + local_irq_save(flags); + t1 = ktime_get_ns(); + for (t = 0; t < 50; t++) + gameport_read(gameport); + t2 = ktime_get_ns(); + t3 = ktime_get_ns(); + local_irq_restore(flags); + udelay(i * 10); + t = (t2 - t1) - (t3 - t2); + if (t < tx) + tx = t; + } + + gameport_close(gameport); + t = 1000000 * 50; + if (tx) + t /= tx; + return t; +} + +static int old_gameport_measure_speed(struct gameport *gameport) +{ +#if defined(__i386__) + + unsigned int i, t, t1, t2, t3, tx; + unsigned long flags; + + if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW)) + return 0; + + tx = 1 << 30; + + for(i = 0; i < 50; i++) { + local_irq_save(flags); + GET_TIME(t1); + for (t = 0; t < 50; t++) gameport_read(gameport); + GET_TIME(t2); + GET_TIME(t3); + local_irq_restore(flags); + udelay(i * 10); + if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t; + } + + gameport_close(gameport); + return 59659 / (tx < 1 ? 1 : tx); + +#elif defined (__x86_64__) + + unsigned int i, t; + unsigned long tx, t1, t2, flags; + + if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW)) + return 0; + + tx = 1 << 30; + + for(i = 0; i < 50; i++) { + local_irq_save(flags); + t1 = rdtsc(); + for (t = 0; t < 50; t++) gameport_read(gameport); + t2 = rdtsc(); + local_irq_restore(flags); + udelay(i * 10); + if (t2 - t1 < tx) tx = t2 - t1; + } + + gameport_close(gameport); + return (this_cpu_read(cpu_info.loops_per_jiffy) * + (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx); + +#else + + unsigned int j, t = 0; + + if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW)) + return 0; + + j = jiffies; while (j == jiffies); + j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); } + + gameport_close(gameport); + return t * HZ / 1000; + +#endif +} + +void gameport_start_polling(struct gameport *gameport) +{ + spin_lock(&gameport->timer_lock); + + if (!gameport->poll_cnt++) { + BUG_ON(!gameport->poll_handler); + BUG_ON(!gameport->poll_interval); + mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval)); + } + + spin_unlock(&gameport->timer_lock); +} +EXPORT_SYMBOL(gameport_start_polling); + +void gameport_stop_polling(struct gameport *gameport) +{ + spin_lock(&gameport->timer_lock); + + if (!--gameport->poll_cnt) + timer_delete(&gameport->poll_timer); + + spin_unlock(&gameport->timer_lock); +} +EXPORT_SYMBOL(gameport_stop_polling); + +static void gameport_run_poll_handler(struct timer_list *t) +{ + struct gameport *gameport = timer_container_of(gameport, t, + poll_timer); + + gameport->poll_handler(gameport); + if (gameport->poll_cnt) + mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval)); +} + +/* + * Basic gameport -> driver core mappings + */ + +static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv) +{ + int error; + + gameport->dev.driver = &drv->driver; + if (drv->connect(gameport, drv)) { + gameport->dev.driver = NULL; + return -ENODEV; + } + + error = device_bind_driver(&gameport->dev); + if (error) { + dev_warn(&gameport->dev, + "device_bind_driver() failed for %s (%s) and %s, error: %d\n", + gameport->phys, gameport->name, + drv->description, error); + drv->disconnect(gameport); + gameport->dev.driver = NULL; + return error; + } + + return 0; +} + +static void gameport_find_driver(struct gameport *gameport) +{ + int error; + + error = device_attach(&gameport->dev); + if (error < 0) + dev_warn(&gameport->dev, + "device_attach() failed for %s (%s), error: %d\n", + gameport->phys, gameport->name, error); +} + + +/* + * Gameport event processing. + */ + +enum gameport_event_type { + GAMEPORT_REGISTER_PORT, + GAMEPORT_ATTACH_DRIVER, +}; + +struct gameport_event { + enum gameport_event_type type; + void *object; + struct module *owner; + struct list_head node; +}; + +static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */ +static LIST_HEAD(gameport_event_list); + +static struct gameport_event *gameport_get_event(void) +{ + struct gameport_event *event = NULL; + unsigned long flags; + + spin_lock_irqsave(&gameport_event_lock, flags); + + if (!list_empty(&gameport_event_list)) { + event = list_first_entry(&gameport_event_list, + struct gameport_event, node); + list_del_init(&event->node); + } + + spin_unlock_irqrestore(&gameport_event_lock, flags); + return event; +} + +static void gameport_free_event(struct gameport_event *event) +{ + module_put(event->owner); + kfree(event); +} + +static void gameport_remove_duplicate_events(struct gameport_event *event) +{ + struct gameport_event *e, *next; + unsigned long flags; + + spin_lock_irqsave(&gameport_event_lock, flags); + + list_for_each_entry_safe(e, next, &gameport_event_list, node) { + if (event->object == e->object) { + /* + * If this event is of different type we should not + * look further - we only suppress duplicate events + * that were sent back-to-back. + */ + if (event->type != e->type) + break; + + list_del_init(&e->node); + gameport_free_event(e); + } + } + + spin_unlock_irqrestore(&gameport_event_lock, flags); +} + + +static void gameport_handle_events(struct work_struct *work) +{ + struct gameport_event *event; + + mutex_lock(&gameport_mutex); + + /* + * Note that we handle only one event here to give swsusp + * a chance to freeze kgameportd thread. Gameport events + * should be pretty rare so we are not concerned about + * taking performance hit. + */ + if ((event = gameport_get_event())) { + + switch (event->type) { + + case GAMEPORT_REGISTER_PORT: + gameport_add_port(event->object); + break; + + case GAMEPORT_ATTACH_DRIVER: + gameport_attach_driver(event->object); + break; + } + + gameport_remove_duplicate_events(event); + gameport_free_event(event); + } + + mutex_unlock(&gameport_mutex); +} + +static DECLARE_WORK(gameport_event_work, gameport_handle_events); + +static int gameport_queue_event(void *object, struct module *owner, + enum gameport_event_type event_type) +{ + unsigned long flags; + struct gameport_event *event; + int retval = 0; + + spin_lock_irqsave(&gameport_event_lock, flags); + + /* + * Scan event list for the other events for the same gameport port, + * starting with the most recent one. If event is the same we + * do not need add new one. If event is of different type we + * need to add this event and should not look further because + * we need to preserve sequence of distinct events. + */ + list_for_each_entry_reverse(event, &gameport_event_list, node) { + if (event->object == object) { + if (event->type == event_type) + goto out; + break; + } + } + + event = kmalloc_obj(*event, GFP_ATOMIC); + if (!event) { + pr_err("Not enough memory to queue event %d\n", event_type); + retval = -ENOMEM; + goto out; + } + + if (!try_module_get(owner)) { + pr_warn("Can't get module reference, dropping event %d\n", + event_type); + kfree(event); + retval = -EINVAL; + goto out; + } + + event->type = event_type; + event->object = object; + event->owner = owner; + + list_add_tail(&event->node, &gameport_event_list); + queue_work(system_long_wq, &gameport_event_work); + +out: + spin_unlock_irqrestore(&gameport_event_lock, flags); + return retval; +} + +/* + * Remove all events that have been submitted for a given object, + * be it a gameport port or a driver. + */ +static void gameport_remove_pending_events(void *object) +{ + struct gameport_event *event, *next; + unsigned long flags; + + spin_lock_irqsave(&gameport_event_lock, flags); + + list_for_each_entry_safe(event, next, &gameport_event_list, node) { + if (event->object == object) { + list_del_init(&event->node); + gameport_free_event(event); + } + } + + spin_unlock_irqrestore(&gameport_event_lock, flags); +} + +/* + * Destroy child gameport port (if any) that has not been fully registered yet. + * + * Note that we rely on the fact that port can have only one child and therefore + * only one child registration request can be pending. Additionally, children + * are registered by driver's connect() handler so there can't be a grandchild + * pending registration together with a child. + */ +static struct gameport *gameport_get_pending_child(struct gameport *parent) +{ + struct gameport_event *event; + struct gameport *gameport, *child = NULL; + unsigned long flags; + + spin_lock_irqsave(&gameport_event_lock, flags); + + list_for_each_entry(event, &gameport_event_list, node) { + if (event->type == GAMEPORT_REGISTER_PORT) { + gameport = event->object; + if (gameport->parent == parent) { + child = gameport; + break; + } + } + } + + spin_unlock_irqrestore(&gameport_event_lock, flags); + return child; +} + +/* + * Gameport port operations + */ + +static ssize_t gameport_description_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct gameport *gameport = to_gameport_port(dev); + + return sprintf(buf, "%s\n", gameport->name); +} +static DEVICE_ATTR(description, S_IRUGO, gameport_description_show, NULL); + +static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) +{ + struct gameport *gameport = to_gameport_port(dev); + struct device_driver *drv; + int error; + + error = mutex_lock_interruptible(&gameport_mutex); + if (error) + return error; + + if (!strncmp(buf, "none", count)) { + gameport_disconnect_port(gameport); + } else if (!strncmp(buf, "reconnect", count)) { + gameport_reconnect_port(gameport); + } else if (!strncmp(buf, "rescan", count)) { + gameport_disconnect_port(gameport); + gameport_find_driver(gameport); + } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) { + gameport_disconnect_port(gameport); + error = gameport_bind_driver(gameport, to_gameport_driver(drv)); + } else { + error = -EINVAL; + } + + mutex_unlock(&gameport_mutex); + + return error ? error : count; +} +static DEVICE_ATTR_WO(drvctl); + +static struct attribute *gameport_device_attrs[] = { + &dev_attr_description.attr, + &dev_attr_drvctl.attr, + NULL, +}; +ATTRIBUTE_GROUPS(gameport_device); + +static void gameport_release_port(struct device *dev) +{ + struct gameport *gameport = to_gameport_port(dev); + + kfree(gameport); + module_put(THIS_MODULE); +} + +void gameport_set_phys(struct gameport *gameport, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args); + va_end(args); +} +EXPORT_SYMBOL(gameport_set_phys); + +static void gameport_default_trigger(struct gameport *gameport) +{ +#ifdef CONFIG_HAS_IOPORT + outb(0xff, gameport->io); +#endif +} + +static unsigned char gameport_default_read(struct gameport *gameport) +{ +#ifdef CONFIG_HAS_IOPORT + return inb(gameport->io); +#else + return 0xff; +#endif +} + +static void gameport_setup_default_handlers(struct gameport *gameport) +{ + if ((!gameport->trigger || !gameport->read) && + !IS_ENABLED(CONFIG_HAS_IOPORT)) + dev_err(&gameport->dev, + "I/O port access is required for %s (%s) but is not available\n", + gameport->phys, gameport->name); + + if (!gameport->trigger) + gameport->trigger = gameport_default_trigger; + if (!gameport->read) + gameport->read = gameport_default_read; +} + +/* + * Prepare gameport port for registration. + */ +static void gameport_init_port(struct gameport *gameport) +{ + static atomic_t gameport_no = ATOMIC_INIT(-1); + + __module_get(THIS_MODULE); + + mutex_init(&gameport->drv_mutex); + device_initialize(&gameport->dev); + dev_set_name(&gameport->dev, "gameport%lu", + (unsigned long)atomic_inc_return(&gameport_no)); + gameport->dev.bus = &gameport_bus; + gameport->dev.release = gameport_release_port; + if (gameport->parent) + gameport->dev.parent = &gameport->parent->dev; + + gameport_setup_default_handlers(gameport); + INIT_LIST_HEAD(&gameport->node); + spin_lock_init(&gameport->timer_lock); + timer_setup(&gameport->poll_timer, gameport_run_poll_handler, 0); +} + +/* + * Complete gameport port registration. + * Driver core will attempt to find appropriate driver for the port. + */ +static void gameport_add_port(struct gameport *gameport) +{ + int error; + + if (gameport->parent) + gameport->parent->child = gameport; + + gameport->speed = use_ktime ? + gameport_measure_speed(gameport) : + old_gameport_measure_speed(gameport); + + list_add_tail(&gameport->node, &gameport_list); + + if (gameport->io) + dev_info(&gameport->dev, "%s is %s, io %#x, speed %dkHz\n", + gameport->name, gameport->phys, gameport->io, gameport->speed); + else + dev_info(&gameport->dev, "%s is %s, speed %dkHz\n", + gameport->name, gameport->phys, gameport->speed); + + error = device_add(&gameport->dev); + if (error) + dev_err(&gameport->dev, + "device_add() failed for %s (%s), error: %d\n", + gameport->phys, gameport->name, error); +} + +/* + * gameport_destroy_port() completes deregistration process and removes + * port from the system + */ +static void gameport_destroy_port(struct gameport *gameport) +{ + struct gameport *child; + + child = gameport_get_pending_child(gameport); + if (child) { + gameport_remove_pending_events(child); + put_device(&child->dev); + } + + if (gameport->parent) { + gameport->parent->child = NULL; + gameport->parent = NULL; + } + + if (device_is_registered(&gameport->dev)) + device_del(&gameport->dev); + + list_del_init(&gameport->node); + + gameport_remove_pending_events(gameport); + put_device(&gameport->dev); +} + +/* + * Reconnect gameport port and all its children (re-initialize attached devices) + */ +static void gameport_reconnect_port(struct gameport *gameport) +{ + do { + if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) { + gameport_disconnect_port(gameport); + gameport_find_driver(gameport); + /* Ok, old children are now gone, we are done */ + break; + } + gameport = gameport->child; + } while (gameport); +} + +/* + * gameport_disconnect_port() unbinds a port from its driver. As a side effect + * all child ports are unbound and destroyed. + */ +static void gameport_disconnect_port(struct gameport *gameport) +{ + struct gameport *s, *parent; + + if (gameport->child) { + /* + * Children ports should be disconnected and destroyed + * first, staring with the leaf one, since we don't want + * to do recursion + */ + for (s = gameport; s->child; s = s->child) + /* empty */; + + do { + parent = s->parent; + + device_release_driver(&s->dev); + gameport_destroy_port(s); + } while ((s = parent) != gameport); + } + + /* + * Ok, no children left, now disconnect this port + */ + device_release_driver(&gameport->dev); +} + +/* + * Submits register request to kgameportd for subsequent execution. + * Note that port registration is always asynchronous. + */ +void __gameport_register_port(struct gameport *gameport, struct module *owner) +{ + gameport_init_port(gameport); + gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT); +} +EXPORT_SYMBOL(__gameport_register_port); + +/* + * Synchronously unregisters gameport port. + */ +void gameport_unregister_port(struct gameport *gameport) +{ + mutex_lock(&gameport_mutex); + gameport_disconnect_port(gameport); + gameport_destroy_port(gameport); + mutex_unlock(&gameport_mutex); +} +EXPORT_SYMBOL(gameport_unregister_port); + + +/* + * Gameport driver operations + */ + +static ssize_t description_show(struct device_driver *drv, char *buf) +{ + struct gameport_driver *driver = to_gameport_driver(drv); + return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)"); +} +static DRIVER_ATTR_RO(description); + +static struct attribute *gameport_driver_attrs[] = { + &driver_attr_description.attr, + NULL +}; +ATTRIBUTE_GROUPS(gameport_driver); + +static int gameport_driver_probe(struct device *dev) +{ + struct gameport *gameport = to_gameport_port(dev); + struct gameport_driver *drv = to_gameport_driver(dev->driver); + + drv->connect(gameport, drv); + return gameport->drv ? 0 : -ENODEV; +} + +static void gameport_driver_remove(struct device *dev) +{ + struct gameport *gameport = to_gameport_port(dev); + struct gameport_driver *drv = to_gameport_driver(dev->driver); + + drv->disconnect(gameport); +} + +static void gameport_attach_driver(struct gameport_driver *drv) +{ + int error; + + error = driver_attach(&drv->driver); + if (error) + pr_err("driver_attach() failed for %s, error: %d\n", + drv->driver.name, error); +} + +int __gameport_register_driver(struct gameport_driver *drv, struct module *owner, + const char *mod_name) +{ + int error; + + drv->driver.bus = &gameport_bus; + drv->driver.owner = owner; + drv->driver.mod_name = mod_name; + + /* + * Temporarily disable automatic binding because probing + * takes long time and we are better off doing it in kgameportd + */ + drv->ignore = true; + + error = driver_register(&drv->driver); + if (error) { + pr_err("driver_register() failed for %s, error: %d\n", + drv->driver.name, error); + return error; + } + + /* + * Reset ignore flag and let kgameportd bind the driver to free ports + */ + drv->ignore = false; + error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER); + if (error) { + driver_unregister(&drv->driver); + return error; + } + + return 0; +} +EXPORT_SYMBOL(__gameport_register_driver); + +void gameport_unregister_driver(struct gameport_driver *drv) +{ + struct gameport *gameport; + + mutex_lock(&gameport_mutex); + + drv->ignore = true; /* so gameport_find_driver ignores it */ + gameport_remove_pending_events(drv); + +start_over: + list_for_each_entry(gameport, &gameport_list, node) { + if (gameport->drv == drv) { + gameport_disconnect_port(gameport); + gameport_find_driver(gameport); + /* we could've deleted some ports, restart */ + goto start_over; + } + } + + driver_unregister(&drv->driver); + + mutex_unlock(&gameport_mutex); +} +EXPORT_SYMBOL(gameport_unregister_driver); + +static int gameport_bus_match(struct device *dev, const struct device_driver *drv) +{ + const struct gameport_driver *gameport_drv = to_gameport_driver(drv); + + return !gameport_drv->ignore; +} + +static const struct bus_type gameport_bus = { + .name = "gameport", + .dev_groups = gameport_device_groups, + .drv_groups = gameport_driver_groups, + .match = gameport_bus_match, + .probe = gameport_driver_probe, + .remove = gameport_driver_remove, +}; + +static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv) +{ + mutex_lock(&gameport->drv_mutex); + gameport->drv = drv; + mutex_unlock(&gameport->drv_mutex); +} + +int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode) +{ + if (gameport->open) { + if (gameport->open(gameport, mode)) { + return -1; + } + } else { + if (mode != GAMEPORT_MODE_RAW) + return -1; + } + + gameport_set_drv(gameport, drv); + return 0; +} +EXPORT_SYMBOL(gameport_open); + +void gameport_close(struct gameport *gameport) +{ + timer_delete_sync(&gameport->poll_timer); + gameport->poll_handler = NULL; + gameport->poll_interval = 0; + gameport_set_drv(gameport, NULL); + if (gameport->close) + gameport->close(gameport); +} +EXPORT_SYMBOL(gameport_close); + +static int __init gameport_init(void) +{ + int error; + + error = bus_register(&gameport_bus); + if (error) { + pr_err("failed to register gameport bus, error: %d\n", error); + return error; + } + + + return 0; +} + +static void __exit gameport_exit(void) +{ + bus_unregister(&gameport_bus); + + /* + * There should not be any outstanding events but work may + * still be scheduled so simply cancel it. + */ + cancel_work_sync(&gameport_event_work); +} + +subsys_initcall(gameport_init); +module_exit(gameport_exit); diff --git a/drivers/input/gameport/lightning.c b/drivers/input/gameport/lightning.c new file mode 100644 index 000000000..2ce717b25 --- /dev/null +++ b/drivers/input/gameport/lightning.c @@ -0,0 +1,322 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 1998-2001 Vojtech Pavlik + */ + +/* + * PDPI Lightning 4 gamecard driver for Linux. + */ + +#include <asm/io.h> +#include <linux/delay.h> +#include <linux/errno.h> +#include <linux/ioport.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/init.h> +#include <linux/gameport.h> + +#define L4_PORT 0x201 +#define L4_SELECT_ANALOG 0xa4 +#define L4_SELECT_DIGITAL 0xa5 +#define L4_SELECT_SECONDARY 0xa6 +#define L4_CMD_ID 0x80 +#define L4_CMD_GETCAL 0x92 +#define L4_CMD_SETCAL 0x93 +#define L4_ID 0x04 +#define L4_BUSY 0x01 +#define L4_TIMEOUT 80 /* 80 us */ + +MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); +MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver"); +MODULE_LICENSE("GPL"); + +struct l4 { + struct gameport *gameport; + unsigned char port; +}; + +static struct l4 l4_ports[8]; + +/* + * l4_wait_ready() waits for the L4 to become ready. + */ + +static int l4_wait_ready(void) +{ + unsigned int t = L4_TIMEOUT; + + while ((inb(L4_PORT) & L4_BUSY) && t > 0) t--; + return -(t <= 0); +} + +/* + * l4_cooked_read() reads data from the Lightning 4. + */ + +static int l4_cooked_read(struct gameport *gameport, int *axes, int *buttons) +{ + struct l4 *l4 = gameport->port_data; + unsigned char status; + int i, result = -1; + + outb(L4_SELECT_ANALOG, L4_PORT); + outb(L4_SELECT_DIGITAL + (l4->port >> 2), L4_PORT); + + if (inb(L4_PORT) & L4_BUSY) goto fail; + outb(l4->port & 3, L4_PORT); + + if (l4_wait_ready()) goto fail; + status = inb(L4_PORT); + + for (i = 0; i < 4; i++) + if (status & (1 << i)) { + if (l4_wait_ready()) goto fail; + axes[i] = inb(L4_PORT); + if (axes[i] > 252) axes[i] = -1; + } + + if (status & 0x10) { + if (l4_wait_ready()) goto fail; + *buttons = inb(L4_PORT) & 0x0f; + } + + result = 0; + +fail: outb(L4_SELECT_ANALOG, L4_PORT); + return result; +} + +static int l4_open(struct gameport *gameport, int mode) +{ + struct l4 *l4 = gameport->port_data; + + if (l4->port != 0 && mode != GAMEPORT_MODE_COOKED) + return -1; + outb(L4_SELECT_ANALOG, L4_PORT); + return 0; +} + +/* + * l4_getcal() reads the L4 with calibration values. + */ + +static int l4_getcal(int port, int *cal) +{ + int i, result = -1; + + outb(L4_SELECT_ANALOG, L4_PORT); + outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT); + if (inb(L4_PORT) & L4_BUSY) + goto out; + + outb(L4_CMD_GETCAL, L4_PORT); + if (l4_wait_ready()) + goto out; + + if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2)) + goto out; + + if (l4_wait_ready()) + goto out; + outb(port & 3, L4_PORT); + + for (i = 0; i < 4; i++) { + if (l4_wait_ready()) + goto out; + cal[i] = inb(L4_PORT); + } + + result = 0; + +out: outb(L4_SELECT_ANALOG, L4_PORT); + return result; +} + +/* + * l4_setcal() programs the L4 with calibration values. + */ + +static int l4_setcal(int port, int *cal) +{ + int i, result = -1; + + outb(L4_SELECT_ANALOG, L4_PORT); + outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT); + if (inb(L4_PORT) & L4_BUSY) + goto out; + + outb(L4_CMD_SETCAL, L4_PORT); + if (l4_wait_ready()) + goto out; + + if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2)) + goto out; + + if (l4_wait_ready()) + goto out; + outb(port & 3, L4_PORT); + + for (i = 0; i < 4; i++) { + if (l4_wait_ready()) + goto out; + outb(cal[i], L4_PORT); + } + + result = 0; + +out: outb(L4_SELECT_ANALOG, L4_PORT); + return result; +} + +/* + * l4_calibrate() calibrates the L4 for the attached device, so + * that the device's resistance fits into the L4's 8-bit range. + */ + +static int l4_calibrate(struct gameport *gameport, int *axes, int *max) +{ + int i, t; + int cal[4]; + struct l4 *l4 = gameport->port_data; + + if (l4_getcal(l4->port, cal)) + return -1; + + for (i = 0; i < 4; i++) { + t = (max[i] * cal[i]) / 200; + t = (t < 1) ? 1 : ((t > 255) ? 255 : t); + axes[i] = (axes[i] < 0) ? -1 : (axes[i] * cal[i]) / t; + axes[i] = (axes[i] > 252) ? 252 : axes[i]; + cal[i] = t; + } + + if (l4_setcal(l4->port, cal)) + return -1; + + return 0; +} + +static int __init l4_create_ports(int card_no) +{ + struct l4 *l4; + struct gameport *port; + int i, idx; + + for (i = 0; i < 4; i++) { + + idx = card_no * 4 + i; + l4 = &l4_ports[idx]; + + if (!(l4->gameport = port = gameport_allocate_port())) { + printk(KERN_ERR "lightning: Memory allocation failed\n"); + while (--i >= 0) { + gameport_free_port(l4->gameport); + l4->gameport = NULL; + } + return -ENOMEM; + } + l4->port = idx; + + port->port_data = l4; + port->open = l4_open; + port->cooked_read = l4_cooked_read; + port->calibrate = l4_calibrate; + + gameport_set_name(port, "PDPI Lightning 4"); + gameport_set_phys(port, "isa%04x/gameport%d", L4_PORT, idx); + + if (idx == 0) + port->io = L4_PORT; + } + + return 0; +} + +static int __init l4_add_card(int card_no) +{ + int cal[4] = { 255, 255, 255, 255 }; + int i, rev, result; + struct l4 *l4; + + outb(L4_SELECT_ANALOG, L4_PORT); + outb(L4_SELECT_DIGITAL + card_no, L4_PORT); + + if (inb(L4_PORT) & L4_BUSY) + return -1; + outb(L4_CMD_ID, L4_PORT); + + if (l4_wait_ready()) + return -1; + + if (inb(L4_PORT) != L4_SELECT_DIGITAL + card_no) + return -1; + + if (l4_wait_ready()) + return -1; + if (inb(L4_PORT) != L4_ID) + return -1; + + if (l4_wait_ready()) + return -1; + rev = inb(L4_PORT); + + if (!rev) + return -1; + + result = l4_create_ports(card_no); + if (result) + return result; + + printk(KERN_INFO "gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n", + card_no ? "secondary" : "primary", rev >> 4, rev, L4_PORT); + + for (i = 0; i < 4; i++) { + l4 = &l4_ports[card_no * 4 + i]; + + if (rev > 0x28) /* on 2.9+ the setcal command works correctly */ + l4_setcal(l4->port, cal); + gameport_register_port(l4->gameport); + } + + return 0; +} + +static int __init l4_init(void) +{ + int i, cards = 0; + + if (!request_region(L4_PORT, 1, "lightning")) + return -EBUSY; + + for (i = 0; i < 2; i++) + if (l4_add_card(i) == 0) + cards++; + + outb(L4_SELECT_ANALOG, L4_PORT); + + if (!cards) { + release_region(L4_PORT, 1); + return -ENODEV; + } + + return 0; +} + +static void __exit l4_exit(void) +{ + int i; + int cal[4] = { 59, 59, 59, 59 }; + + for (i = 0; i < 8; i++) + if (l4_ports[i].gameport) { + l4_setcal(l4_ports[i].port, cal); + gameport_unregister_port(l4_ports[i].gameport); + } + + outb(L4_SELECT_ANALOG, L4_PORT); + release_region(L4_PORT, 1); +} + +module_init(l4_init); +module_exit(l4_exit); diff --git a/drivers/input/gameport/ns558.c b/drivers/input/gameport/ns558.c new file mode 100644 index 000000000..f70a96c4f --- /dev/null +++ b/drivers/input/gameport/ns558.c @@ -0,0 +1,267 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 1999-2001 Vojtech Pavlik + * Copyright (c) 1999 Brian Gerst + */ + +/* + * NS558 based standard IBM game port driver for Linux + */ + +#include <asm/io.h> + +#include <linux/module.h> +#include <linux/ioport.h> +#include <linux/init.h> +#include <linux/delay.h> +#include <linux/gameport.h> +#include <linux/slab.h> +#include <linux/pnp.h> + +MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); +MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver"); +MODULE_LICENSE("GPL"); + +static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209, + 0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 }; + +struct ns558 { + int type; + int io; + int size; + struct pnp_dev *dev; + struct gameport *gameport; + struct list_head node; +}; + +static LIST_HEAD(ns558_list); + +/* + * ns558_isa_probe() tries to find an isa gameport at the + * specified address, and also checks for mirrors. + * A joystick must be attached for this to work. + */ + +static int ns558_isa_probe(int io) +{ + int i, j, b; + unsigned char c, u, v; + struct ns558 *ns558; + struct gameport *port; + +/* + * No one should be using this address. + */ + + if (!request_region(io, 1, "ns558-isa")) + return -EBUSY; + +/* + * We must not be able to write arbitrary values to the port. + * The lower two axis bits must be 1 after a write. + */ + + c = inb(io); + outb(~c & ~3, io); + if (~(u = v = inb(io)) & 3) { + outb(c, io); + release_region(io, 1); + return -ENODEV; + } +/* + * After a trigger, there must be at least some bits changing. + */ + + for (i = 0; i < 1000; i++) v &= inb(io); + + if (u == v) { + outb(c, io); + release_region(io, 1); + return -ENODEV; + } + msleep(3); +/* + * After some time (4ms) the axes shouldn't change anymore. + */ + + u = inb(io); + for (i = 0; i < 1000; i++) + if ((u ^ inb(io)) & 0xf) { + outb(c, io); + release_region(io, 1); + return -ENODEV; + } +/* + * And now find the number of mirrors of the port. + */ + + for (i = 1; i < 5; i++) { + + release_region(io & (-1 << (i - 1)), (1 << (i - 1))); + + if (!request_region(io & (-1 << i), (1 << i), "ns558-isa")) + break; /* Don't disturb anyone */ + + outb(0xff, io & (-1 << i)); + for (j = b = 0; j < 1000; j++) + if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++; + msleep(3); + + if (b > 300) { /* We allow 30% difference */ + release_region(io & (-1 << i), (1 << i)); + break; + } + } + + i--; + + if (i != 4) { + if (!request_region(io & (-1 << i), (1 << i), "ns558-isa")) + return -EBUSY; + } + + ns558 = kzalloc_obj(*ns558); + port = gameport_allocate_port(); + if (!ns558 || !port) { + printk(KERN_ERR "ns558: Memory allocation failed.\n"); + release_region(io & (-1 << i), (1 << i)); + kfree(ns558); + gameport_free_port(port); + return -ENOMEM; + } + + ns558->io = io; + ns558->size = 1 << i; + ns558->gameport = port; + + port->io = io; + gameport_set_name(port, "NS558 ISA Gameport"); + gameport_set_phys(port, "isa%04x/gameport0", io & (-1 << i)); + + gameport_register_port(port); + + list_add(&ns558->node, &ns558_list); + + return 0; +} + +#ifdef CONFIG_PNP + +static const struct pnp_device_id pnp_devids[] = { + { .id = "@P@0001" }, /* ALS 100 */ + { .id = "@P@0020" }, /* ALS 200 */ + { .id = "@P@1001" }, /* ALS 100+ */ + { .id = "@P@2001" }, /* ALS 120 */ + { .id = "ASB16fd" }, /* AdLib NSC16 */ + { .id = "AZT3001" }, /* AZT1008 */ + { .id = "CDC0001" }, /* Opl3-SAx */ + { .id = "CSC0001" }, /* CS4232 */ + { .id = "CSC000f" }, /* CS4236 */ + { .id = "CSC0101" }, /* CS4327 */ + { .id = "CTL7001" }, /* SB16 */ + { .id = "CTL7002" }, /* AWE64 */ + { .id = "CTL7005" }, /* Vibra16 */ + { .id = "ENS2020" }, /* SoundscapeVIVO */ + { .id = "ESS0001" }, /* ES1869 */ + { .id = "ESS0005" }, /* ES1878 */ + { .id = "ESS6880" }, /* ES688 */ + { .id = "IBM0012" }, /* CS4232 */ + { .id = "OPT0001" }, /* OPTi Audio16 */ + { .id = "YMH0006" }, /* Opl3-SA */ + { .id = "YMH0022" }, /* Opl3-SAx */ + { .id = "PNPb02f" }, /* Generic */ + { } +}; + +MODULE_DEVICE_TABLE(pnp, pnp_devids); + +static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did) +{ + int ioport, iolen; + struct ns558 *ns558; + struct gameport *port; + + if (!pnp_port_valid(dev, 0)) { + printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n"); + return -ENODEV; + } + + ioport = pnp_port_start(dev, 0); + iolen = pnp_port_len(dev, 0); + + if (!request_region(ioport, iolen, "ns558-pnp")) + return -EBUSY; + + ns558 = kzalloc_obj(*ns558); + port = gameport_allocate_port(); + if (!ns558 || !port) { + printk(KERN_ERR "ns558: Memory allocation failed\n"); + kfree(ns558); + gameport_free_port(port); + return -ENOMEM; + } + + ns558->io = ioport; + ns558->size = iolen; + ns558->dev = dev; + ns558->gameport = port; + + gameport_set_name(port, "NS558 PnP Gameport"); + gameport_set_phys(port, "pnp%s/gameport0", dev_name(&dev->dev)); + port->dev.parent = &dev->dev; + port->io = ioport; + + gameport_register_port(port); + + list_add_tail(&ns558->node, &ns558_list); + return 0; +} + +static struct pnp_driver ns558_pnp_driver = { + .name = "ns558", + .id_table = pnp_devids, + .probe = ns558_pnp_probe, +}; + +#else + +static struct pnp_driver ns558_pnp_driver; + +#endif + +static int __init ns558_init(void) +{ + int i = 0; + int error; + + error = pnp_register_driver(&ns558_pnp_driver); + if (error && error != -ENODEV) /* should be ENOSYS really */ + return error; + +/* + * Probe ISA ports after PnP, so that PnP ports that are already + * enabled get detected as PnP. This may be suboptimal in multi-device + * configurations, but saves hassle with simple setups. + */ + + while (ns558_isa_portlist[i]) + ns558_isa_probe(ns558_isa_portlist[i++]); + + return list_empty(&ns558_list) && error ? -ENODEV : 0; +} + +static void __exit ns558_exit(void) +{ + struct ns558 *ns558, *safe; + + list_for_each_entry_safe(ns558, safe, &ns558_list, node) { + gameport_unregister_port(ns558->gameport); + release_region(ns558->io & ~(ns558->size - 1), ns558->size); + kfree(ns558); + } + + pnp_unregister_driver(&ns558_pnp_driver); +} + +module_init(ns558_init); +module_exit(ns558_exit); |
