Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * $Id: cobra.c,v 1.19 2002/01/22 20:26:52 vojtech Exp $
  3  *
  4  *  Copyright (c) 1999-2001 Vojtech Pavlik
  5  */
  6 
  7 /*
  8  * Creative Labs Blaster GamePad Cobra driver for Linux
  9  */
 10 
 11 /*
 12  * This program is free software; you can redistribute it and/or modify
 13  * it under the terms of the GNU General Public License as published by
 14  * the Free Software Foundation; either version 2 of the License, or
 15  * (at your option) any later version.
 16  *
 17  * This program is distributed in the hope that it will be useful,
 18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20  * GNU General Public License for more details.
 21  *
 22  * You should have received a copy of the GNU General Public License
 23  * along with this program; if not, write to the Free Software
 24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 25  *
 26  * Should you need to contact me, the author, you can do so either by
 27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
 29  */
 30 
 31 #include <linux/kernel.h>
 32 #include <linux/module.h>
 33 #include <linux/slab.h>
 34 #include <linux/init.h>
 35 #include <linux/gameport.h>
 36 #include <linux/input.h>
 37 
 38 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 39 MODULE_DESCRIPTION("Creative Labs Blaster GamePad Cobra driver");
 40 MODULE_LICENSE("GPL");
 41 
 42 #define COBRA_MAX_STROBE        45      /* 45 us max wait for first strobe */
 43 #define COBRA_REFRESH_TIME      HZ/50   /* 20 ms between reads */
 44 #define COBRA_LENGTH            36
 45 
 46 static char* cobra_name = "Creative Labs Blaster GamePad Cobra";
 47 
 48 static int cobra_btn[] = { BTN_START, BTN_SELECT, BTN_TL, BTN_TR, BTN_X, BTN_Y, BTN_Z, BTN_A, BTN_B, BTN_C, BTN_TL2, BTN_TR2, 0 };
 49 
 50 struct cobra {
 51         struct gameport *gameport;
 52         struct timer_list timer;
 53         struct input_dev dev[2];
 54         int used;
 55         int reads;
 56         int bads;
 57         unsigned char exists;
 58         char phys[2][32];
 59 };
 60 
 61 static unsigned char cobra_read_packet(struct gameport *gameport, unsigned int *data)
 62 {
 63         unsigned long flags;
 64         unsigned char u, v, w;
 65         __u64 buf[2];
 66         int r[2], t[2];
 67         int i, j, ret;
 68 
 69         int strobe = gameport_time(gameport, COBRA_MAX_STROBE);
 70 
 71         for (i = 0; i < 2; i++) {
 72                 r[i] = buf[i] = 0;
 73                 t[i] = COBRA_MAX_STROBE;
 74         }
 75 
 76         local_irq_save(flags);
 77 
 78         u = gameport_read(gameport);
 79 
 80         do {
 81                 t[0]--; t[1]--;
 82                 v = gameport_read(gameport);
 83                 for (i = 0, w = u ^ v; i < 2 && w; i++, w >>= 2)
 84                         if (w & 0x30) {
 85                                 if ((w & 0x30) < 0x30 && r[i] < COBRA_LENGTH && t[i] > 0) {
 86                                         buf[i] |= (__u64)((w >> 5) & 1) << r[i]++;
 87                                         t[i] = strobe;
 88                                         u = v;
 89                                 } else t[i] = 0;
 90                         }
 91         } while (t[0] > 0 || t[1] > 0);
 92 
 93         local_irq_restore(flags);
 94 
 95         ret = 0;
 96 
 97         for (i = 0; i < 2; i++) {
 98 
 99                 if (r[i] != COBRA_LENGTH) continue;
100 
101                 for (j = 0; j < COBRA_LENGTH && (buf[i] & 0x04104107f) ^ 0x041041040; j++)
102                         buf[i] = (buf[i] >> 1) | ((__u64)(buf[i] & 1) << (COBRA_LENGTH - 1));
103 
104                 if (j < COBRA_LENGTH) ret |= (1 << i);
105 
106                 data[i] = ((buf[i] >>  7) & 0x000001f) | ((buf[i] >>  8) & 0x00003e0)
107                         | ((buf[i] >>  9) & 0x0007c00) | ((buf[i] >> 10) & 0x00f8000)
108                         | ((buf[i] >> 11) & 0x1f00000);
109 
110         }
111 
112         return ret;
113 }
114 
115 static void cobra_timer(unsigned long private)
116 {
117         struct cobra *cobra = (void *) private;
118         struct input_dev *dev;
119         unsigned int data[2];
120         int i, j, r;
121 
122         cobra->reads++;
123 
124         if ((r = cobra_read_packet(cobra->gameport, data)) != cobra->exists)
125                 cobra->bads++;
126         else
127 
128         for (i = 0; i < 2; i++)
129                 if (cobra->exists & r & (1 << i)) {
130 
131                         dev = cobra->dev + i;
132 
133                         input_report_abs(dev, ABS_X, ((data[i] >> 4) & 1) - ((data[i] >> 3) & 1));
134                         input_report_abs(dev, ABS_Y, ((data[i] >> 2) & 1) - ((data[i] >> 1) & 1));
135 
136                         for (j = 0; cobra_btn[j]; j++)
137                                 input_report_key(dev, cobra_btn[j], data[i] & (0x20 << j));
138 
139                         input_sync(dev);
140 
141                 }
142 
143         mod_timer(&cobra->timer, jiffies + COBRA_REFRESH_TIME);
144 }
145 
146 static int cobra_open(struct input_dev *dev)
147 {
148         struct cobra *cobra = dev->private;
149         if (!cobra->used++)
150                 mod_timer(&cobra->timer, jiffies + COBRA_REFRESH_TIME);
151         return 0;
152 }
153 
154 static void cobra_close(struct input_dev *dev)
155 {
156         struct cobra *cobra = dev->private;
157         if (!--cobra->used)
158                 del_timer(&cobra->timer);
159 }
160 
161 static void cobra_connect(struct gameport *gameport, struct gameport_dev *dev)
162 {
163         struct cobra *cobra;
164         unsigned int data[2];
165         int i, j;
166 
167         if (!(cobra = kmalloc(sizeof(struct cobra), GFP_KERNEL)))
168                 return;
169         memset(cobra, 0, sizeof(struct cobra));
170 
171         gameport->private = cobra;
172 
173         cobra->gameport = gameport;
174         init_timer(&cobra->timer);
175         cobra->timer.data = (long) cobra;
176         cobra->timer.function = cobra_timer;
177 
178         if (gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
179                 goto fail1;
180 
181         cobra->exists = cobra_read_packet(gameport, data);
182 
183         for (i = 0; i < 2; i++)
184                 if ((cobra->exists >> i) & data[i] & 1) {
185                         printk(KERN_WARNING "cobra.c: Device %d on %s has the Ext bit set. ID is: %d"
186                                 " Contact vojtech@ucw.cz\n", i, gameport->phys, (data[i] >> 2) & 7);
187                         cobra->exists &= ~(1 << i);
188                 }
189 
190         if (!cobra->exists)
191                 goto fail2;
192 
193         for (i = 0; i < 2; i++)
194                 if ((cobra->exists >> i) & 1) {
195 
196                         sprintf(cobra->phys[i], "%s/input%d", gameport->phys, i);
197 
198                         cobra->dev[i].private = cobra;
199                         cobra->dev[i].open = cobra_open;
200                         cobra->dev[i].close = cobra_close;
201 
202                         cobra->dev[i].name = cobra_name;
203                         cobra->dev[i].phys = cobra->phys[i];
204                         cobra->dev[i].id.bustype = BUS_GAMEPORT;
205                         cobra->dev[i].id.vendor = GAMEPORT_ID_VENDOR_CREATIVE;
206                         cobra->dev[i].id.product = 0x0008;
207                         cobra->dev[i].id.version = 0x0100;
208 
209                         cobra->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
210                         cobra->dev[i].absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
211 
212                         for (j = 0; cobra_btn[j]; j++)
213                                 set_bit(cobra_btn[j], cobra->dev[i].keybit);
214 
215                         cobra->dev[i].absmin[ABS_X] = -1; cobra->dev[i].absmax[ABS_X] = 1;
216                         cobra->dev[i].absmin[ABS_Y] = -1; cobra->dev[i].absmax[ABS_Y] = 1;
217 
218                         input_register_device(cobra->dev + i);
219                         printk(KERN_INFO "input: %s on %s\n", cobra_name, gameport->phys);
220                 }
221 
222         return;
223 fail2:  gameport_close(gameport);
224 fail1:  kfree(cobra);
225 }
226 
227 static void cobra_disconnect(struct gameport *gameport)
228 {
229         int i;
230 
231         struct cobra *cobra = gameport->private;
232         for (i = 0; i < 2; i++)
233                 if ((cobra->exists >> i) & 1)
234                         input_unregister_device(cobra->dev + i);
235         gameport_close(gameport);
236         kfree(cobra);
237 }
238 
239 static struct gameport_dev cobra_dev = {
240         .connect =      cobra_connect,
241         .disconnect =   cobra_disconnect,
242 };
243 
244 int __init cobra_init(void)
245 {
246         gameport_register_device(&cobra_dev);
247         return 0;
248 }
249 
250 void __exit cobra_exit(void)
251 {
252         gameport_unregister_device(&cobra_dev);
253 }
254 
255 module_init(cobra_init);
256 module_exit(cobra_exit);
257 
  This page was automatically generated by the LXR engine.