blob: ae912d3aee4e55b4a3e9d9c19339bf2a7b685641 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * FM801 gameport driver for Linux
3 *
4 * Copyright (c) by Takashi Iwai <tiwai@suse.de>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23#include <asm/io.h>
24#include <linux/delay.h>
25#include <linux/errno.h>
26#include <linux/ioport.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/pci.h>
30#include <linux/init.h>
31#include <linux/slab.h>
32#include <linux/gameport.h>
33
34#define PCI_VENDOR_ID_FORTEMEDIA 0x1319
35#define PCI_DEVICE_ID_FM801_GP 0x0802
36
37#define HAVE_COOKED
38
39struct fm801_gp {
40 struct gameport *gameport;
41 struct resource *res_port;
42};
43
44#ifdef HAVE_COOKED
45static int fm801_gp_cooked_read(struct gameport *gameport, int *axes, int *buttons)
46{
47 unsigned short w;
48
49 w = inw(gameport->io + 2);
50 *buttons = (~w >> 14) & 0x03;
51 axes[0] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5);
52 w = inw(gameport->io + 4);
53 axes[1] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5);
54 w = inw(gameport->io + 6);
55 *buttons |= ((~w >> 14) & 0x03) << 2;
56 axes[2] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5);
57 w = inw(gameport->io + 8);
58 axes[3] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5);
59 outw(0xff, gameport->io); /* reset */
60
61 return 0;
62}
63#endif
64
65static int fm801_gp_open(struct gameport *gameport, int mode)
66{
67 switch (mode) {
68#ifdef HAVE_COOKED
69 case GAMEPORT_MODE_COOKED:
70 return 0;
71#endif
72 case GAMEPORT_MODE_RAW:
73 return 0;
74 default:
75 return -1;
76 }
77
78 return 0;
79}
80
Bill Pemberton5298cc42012-11-23 21:38:25 -080081static int fm801_gp_probe(struct pci_dev *pci, const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct fm801_gp *gp;
84 struct gameport *port;
Jeff Garzikb435fdc2006-10-12 01:06:53 -040085 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Pekka Enberga97e1482005-09-06 15:18:33 -070087 gp = kzalloc(sizeof(struct fm801_gp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 port = gameport_allocate_port();
89 if (!gp || !port) {
90 printk(KERN_ERR "fm801-gp: Memory allocation failed\n");
Jeff Garzikb435fdc2006-10-12 01:06:53 -040091 error = -ENOMEM;
92 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
Jeff Garzikb435fdc2006-10-12 01:06:53 -040095 error = pci_enable_device(pci);
96 if (error)
97 goto err_out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 port->open = fm801_gp_open;
100#ifdef HAVE_COOKED
101 port->cooked_read = fm801_gp_cooked_read;
102#endif
103 gameport_set_name(port, "FM801");
104 gameport_set_phys(port, "pci%s/gameport0", pci_name(pci));
105 port->dev.parent = &pci->dev;
106 port->io = pci_resource_start(pci, 0);
107
108 gp->gameport = port;
109 gp->res_port = request_region(port->io, 0x10, "FM801 GP");
110 if (!gp->res_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 printk(KERN_DEBUG "fm801-gp: unable to grab region 0x%x-0x%x\n",
112 port->io, port->io + 0x0f);
Jeff Garzikb435fdc2006-10-12 01:06:53 -0400113 error = -EBUSY;
114 goto err_out_disable_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
117 pci_set_drvdata(pci, gp);
118
119 outb(0x60, port->io + 0x0d); /* enable joystick 1 and 2 */
120 gameport_register_port(port);
121
122 return 0;
Jeff Garzikb435fdc2006-10-12 01:06:53 -0400123
124 err_out_disable_dev:
125 pci_disable_device(pci);
126 err_out_free:
127 gameport_free_port(port);
128 kfree(gp);
129 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Bill Pembertone2619cf2012-11-23 21:50:47 -0800132static void fm801_gp_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct fm801_gp *gp = pci_get_drvdata(pci);
135
Dmitry Torokhovd345d972010-09-29 18:17:16 -0700136 gameport_unregister_port(gp->gameport);
137 release_resource(gp->res_port);
138 kfree(gp);
139
140 pci_disable_device(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Márton Németha9844b12010-01-09 23:23:58 -0800143static const struct pci_device_id fm801_gp_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 { PCI_VENDOR_ID_FORTEMEDIA, PCI_DEVICE_ID_FM801_GP, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
145 { 0 }
146};
Axel Lin5d066472012-04-21 23:37:53 -0700147MODULE_DEVICE_TABLE(pci, fm801_gp_id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149static struct pci_driver fm801_gp_driver = {
150 .name = "FM801_gameport",
151 .id_table = fm801_gp_id_table,
152 .probe = fm801_gp_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800153 .remove = fm801_gp_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154};
155
Axel Lin5d066472012-04-21 23:37:53 -0700156module_pci_driver(fm801_gp_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Dmitry Torokhovf445da82009-04-17 20:35:57 -0700158MODULE_DESCRIPTION("FM801 gameport driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
160MODULE_LICENSE("GPL");