Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) 1996-2001 Vojtech Pavlik
0004  */
0005 
0006 /*
0007  * This is just a very simple driver that can dump the data
0008  * out of the joystick port into the syslog ...
0009  */
0010 
0011 /*
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/gameport.h>
0016 #include <linux/kernel.h>
0017 #include <linux/delay.h>
0018 #include <linux/slab.h>
0019 
0020 #define DRIVER_DESC "Gameport data dumper module"
0021 
0022 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
0023 MODULE_DESCRIPTION(DRIVER_DESC);
0024 MODULE_LICENSE("GPL");
0025 
0026 #define BUF_SIZE 256
0027 
0028 struct joydump {
0029     unsigned int time;
0030     unsigned char data;
0031 };
0032 
0033 static int joydump_connect(struct gameport *gameport, struct gameport_driver *drv)
0034 {
0035     struct joydump *buf;    /* all entries */
0036     struct joydump *dump, *prev;    /* one entry each */
0037     int axes[4], buttons;
0038     int i, j, t, timeout;
0039     unsigned long flags;
0040     unsigned char u;
0041 
0042     printk(KERN_INFO "joydump: ,------------------ START ----------------.\n");
0043     printk(KERN_INFO "joydump: | Dumping: %30s |\n", gameport->phys);
0044     printk(KERN_INFO "joydump: | Speed: %28d kHz |\n", gameport->speed);
0045 
0046     if (gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
0047 
0048         printk(KERN_INFO "joydump: | Raw mode not available - trying cooked.    |\n");
0049 
0050         if (gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
0051 
0052             printk(KERN_INFO "joydump: | Cooked not available either. Failing.   |\n");
0053             printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
0054             return -ENODEV;
0055         }
0056 
0057         gameport_cooked_read(gameport, axes, &buttons);
0058 
0059         for (i = 0; i < 4; i++)
0060             printk(KERN_INFO "joydump: | Axis %d: %4d.                           |\n", i, axes[i]);
0061         printk(KERN_INFO "joydump: | Buttons %02x.                             |\n", buttons);
0062         printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
0063     }
0064 
0065     timeout = gameport_time(gameport, 10000); /* 10 ms */
0066 
0067     buf = kmalloc_array(BUF_SIZE, sizeof(struct joydump), GFP_KERNEL);
0068     if (!buf) {
0069         printk(KERN_INFO "joydump: no memory for testing\n");
0070         goto jd_end;
0071     }
0072     dump = buf;
0073     t = 0;
0074     i = 1;
0075 
0076     local_irq_save(flags);
0077 
0078     u = gameport_read(gameport);
0079 
0080     dump->data = u;
0081     dump->time = t;
0082     dump++;
0083 
0084     gameport_trigger(gameport);
0085 
0086     while (i < BUF_SIZE && t < timeout) {
0087 
0088         dump->data = gameport_read(gameport);
0089 
0090         if (dump->data ^ u) {
0091             u = dump->data;
0092             dump->time = t;
0093             i++;
0094             dump++;
0095         }
0096         t++;
0097     }
0098 
0099     local_irq_restore(flags);
0100 
0101 /*
0102  * Dump data.
0103  */
0104 
0105     t = i;
0106     dump = buf;
0107     prev = dump;
0108 
0109     printk(KERN_INFO "joydump: >------------------ DATA -----------------<\n");
0110     printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ", 0, 0);
0111     for (j = 7; j >= 0; j--)
0112         printk("%d", (dump->data >> j) & 1);
0113     printk(" |\n");
0114     dump++;
0115 
0116     for (i = 1; i < t; i++, dump++, prev++) {
0117         printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ",
0118             i, dump->time - prev->time);
0119         for (j = 7; j >= 0; j--)
0120             printk("%d", (dump->data >> j) & 1);
0121         printk(" |\n");
0122     }
0123     kfree(buf);
0124 
0125 jd_end:
0126     printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
0127 
0128     return 0;
0129 }
0130 
0131 static void joydump_disconnect(struct gameport *gameport)
0132 {
0133     gameport_close(gameport);
0134 }
0135 
0136 static struct gameport_driver joydump_drv = {
0137     .driver     = {
0138         .name   = "joydump",
0139     },
0140     .description    = DRIVER_DESC,
0141     .connect    = joydump_connect,
0142     .disconnect = joydump_disconnect,
0143 };
0144 
0145 module_gameport_driver(joydump_drv);