kernel.org linux-2.6.10
[linux-2.6.git] / drivers / char / sonypi.c
1 /*
2  * Sony Programmable I/O Control Device driver for VAIO
3  *
4  * Copyright (C) 2001-2004 Stelian Pop <stelian@popies.net>
5  *
6  * Copyright (C) 2001-2002 AlcĂ´ve <www.alcove.com>
7  *
8  * Copyright (C) 2001 Michael Ashley <m.ashley@unsw.edu.au>
9  *
10  * Copyright (C) 2001 Junichi Morita <jun1m@mars.dti.ne.jp>
11  *
12  * Copyright (C) 2000 Takaya Kinjo <t-kinjo@tc4.so-net.ne.jp>
13  *
14  * Copyright (C) 2000 Andrew Tridgell <tridge@valinux.com>
15  *
16  * Earlier work by Werner Almesberger, Paul `Rusty' Russell and Paul Mackerras.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31  *
32  */
33
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/input.h>
37 #include <linux/pci.h>
38 #include <linux/sched.h>
39 #include <linux/init.h>
40 #include <linux/interrupt.h>
41 #include <linux/miscdevice.h>
42 #include <linux/poll.h>
43 #include <linux/delay.h>
44 #include <linux/wait.h>
45 #include <linux/acpi.h>
46 #include <linux/dmi.h>
47 #include <linux/err.h>
48
49 #include <asm/uaccess.h>
50 #include <asm/io.h>
51 #include <asm/system.h>
52
53 #include "sonypi.h"
54 #include <linux/sonypi.h>
55
56 MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
57 MODULE_DESCRIPTION("Sony Programmable I/O Control Device driver");
58 MODULE_LICENSE("GPL");
59 MODULE_VERSION(SONYPI_DRIVER_VERSION);
60
61 static int minor = -1;
62 module_param(minor, int, 0);
63 MODULE_PARM_DESC(minor,
64                  "minor number of the misc device, default is -1 (automatic)");
65
66 static int verbose;             /* = 0 */
67 module_param(verbose, int, 0644);
68 MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
69
70 static int fnkeyinit;           /* = 0 */
71 module_param(fnkeyinit, int, 0444);
72 MODULE_PARM_DESC(fnkeyinit,
73                  "set this if your Fn keys do not generate any event");
74
75 static int camera;              /* = 0 */
76 module_param(camera, int, 0444);
77 MODULE_PARM_DESC(camera,
78                  "set this if you have a MotionEye camera (PictureBook series)");
79
80 static int compat;              /* = 0 */
81 module_param(compat, int, 0444);
82 MODULE_PARM_DESC(compat,
83                  "set this if you want to enable backward compatibility mode");
84
85 static unsigned long mask = 0xffffffff;
86 module_param(mask, ulong, 0644);
87 MODULE_PARM_DESC(mask,
88                  "set this to the mask of event you want to enable (see doc)");
89
90 static int useinput = 1;
91 module_param(useinput, int, 0444);
92 MODULE_PARM_DESC(useinput,
93                  "set this if you would like sonypi to feed events to the input subsystem");
94
95 static struct sonypi_device sonypi_device;
96
97 static int sonypi_ec_write(u8 addr, u8 value)
98 {
99 #ifdef CONFIG_ACPI_EC
100         if (SONYPI_ACPI_ACTIVE)
101                 return ec_write(addr, value);
102 #endif
103         wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
104         outb_p(0x81, SONYPI_CST_IOPORT);
105         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
106         outb_p(addr, SONYPI_DATA_IOPORT);
107         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
108         outb_p(value, SONYPI_DATA_IOPORT);
109         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
110         return 0;
111 }
112
113 static int sonypi_ec_read(u8 addr, u8 *value)
114 {
115 #ifdef CONFIG_ACPI_EC
116         if (SONYPI_ACPI_ACTIVE)
117                 return ec_read(addr, value);
118 #endif
119         wait_on_command(1, inb_p(SONYPI_CST_IOPORT) & 3, ITERATIONS_LONG);
120         outb_p(0x80, SONYPI_CST_IOPORT);
121         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
122         outb_p(addr, SONYPI_DATA_IOPORT);
123         wait_on_command(0, inb_p(SONYPI_CST_IOPORT) & 2, ITERATIONS_LONG);
124         *value = inb_p(SONYPI_DATA_IOPORT);
125         return 0;
126 }
127
128 static int ec_read16(u8 addr, u16 *value)
129 {
130         u8 val_lb, val_hb;
131         if (sonypi_ec_read(addr, &val_lb))
132                 return -1;
133         if (sonypi_ec_read(addr + 1, &val_hb))
134                 return -1;
135         *value = val_lb | (val_hb << 8);
136         return 0;
137 }
138
139 /* Initializes the device - this comes from the AML code in the ACPI bios */
140 static void sonypi_type1_srs(void)
141 {
142         u32 v;
143
144         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
145         v = (v & 0xFFFF0000) | ((u32) sonypi_device.ioport1);
146         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
147
148         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
149         v = (v & 0xFFF0FFFF) |
150             (((u32) sonypi_device.ioport1 ^ sonypi_device.ioport2) << 16);
151         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
152
153         v = inl(SONYPI_IRQ_PORT);
154         v &= ~(((u32) 0x3) << SONYPI_IRQ_SHIFT);
155         v |= (((u32) sonypi_device.bits) << SONYPI_IRQ_SHIFT);
156         outl(v, SONYPI_IRQ_PORT);
157
158         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
159         v = (v & 0xFF1FFFFF) | 0x00C00000;
160         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
161 }
162
163 static void sonypi_type2_srs(void)
164 {
165         if (sonypi_ec_write(SONYPI_SHIB, (sonypi_device.ioport1 & 0xFF00) >> 8))
166                 printk(KERN_WARNING "ec_write failed\n");
167         if (sonypi_ec_write(SONYPI_SLOB, sonypi_device.ioport1 & 0x00FF))
168                 printk(KERN_WARNING "ec_write failed\n");
169         if (sonypi_ec_write(SONYPI_SIRQ, sonypi_device.bits))
170                 printk(KERN_WARNING "ec_write failed\n");
171         udelay(10);
172 }
173
174 /* Disables the device - this comes from the AML code in the ACPI bios */
175 static void sonypi_type1_dis(void)
176 {
177         u32 v;
178
179         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
180         v = v & 0xFF3FFFFF;
181         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
182
183         v = inl(SONYPI_IRQ_PORT);
184         v |= (0x3 << SONYPI_IRQ_SHIFT);
185         outl(v, SONYPI_IRQ_PORT);
186 }
187
188 static void sonypi_type2_dis(void)
189 {
190         if (sonypi_ec_write(SONYPI_SHIB, 0))
191                 printk(KERN_WARNING "ec_write failed\n");
192         if (sonypi_ec_write(SONYPI_SLOB, 0))
193                 printk(KERN_WARNING "ec_write failed\n");
194         if (sonypi_ec_write(SONYPI_SIRQ, 0))
195                 printk(KERN_WARNING "ec_write failed\n");
196 }
197
198 static u8 sonypi_call1(u8 dev)
199 {
200         u8 v1, v2;
201
202         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
203         outb(dev, sonypi_device.ioport2);
204         v1 = inb_p(sonypi_device.ioport2);
205         v2 = inb_p(sonypi_device.ioport1);
206         return v2;
207 }
208
209 static u8 sonypi_call2(u8 dev, u8 fn)
210 {
211         u8 v1;
212
213         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
214         outb(dev, sonypi_device.ioport2);
215         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
216         outb(fn, sonypi_device.ioport1);
217         v1 = inb_p(sonypi_device.ioport1);
218         return v1;
219 }
220
221 static u8 sonypi_call3(u8 dev, u8 fn, u8 v)
222 {
223         u8 v1;
224
225         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
226         outb(dev, sonypi_device.ioport2);
227         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
228         outb(fn, sonypi_device.ioport1);
229         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
230         outb(v, sonypi_device.ioport1);
231         v1 = inb_p(sonypi_device.ioport1);
232         return v1;
233 }
234
235 #if 0
236 /* Get brightness, hue etc. Unreliable... */
237 static u8 sonypi_read(u8 fn)
238 {
239         u8 v1, v2;
240         int n = 100;
241
242         while (n--) {
243                 v1 = sonypi_call2(0x8f, fn);
244                 v2 = sonypi_call2(0x8f, fn);
245                 if (v1 == v2 && v1 != 0xff)
246                         return v1;
247         }
248         return 0xff;
249 }
250 #endif
251
252 /* Set brightness, hue etc */
253 static void sonypi_set(u8 fn, u8 v)
254 {
255         wait_on_command(0, sonypi_call3(0x90, fn, v), ITERATIONS_SHORT);
256 }
257
258 /* Tests if the camera is ready */
259 static int sonypi_camera_ready(void)
260 {
261         u8 v;
262
263         v = sonypi_call2(0x8f, SONYPI_CAMERA_STATUS);
264         return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
265 }
266
267 /* Turns the camera off */
268 static void sonypi_camera_off(void)
269 {
270         sonypi_set(SONYPI_CAMERA_PICTURE, SONYPI_CAMERA_MUTE_MASK);
271
272         if (!sonypi_device.camera_power)
273                 return;
274
275         sonypi_call2(0x91, 0);
276         sonypi_device.camera_power = 0;
277 }
278
279 /* Turns the camera on */
280 static void sonypi_camera_on(void)
281 {
282         int i, j;
283
284         if (sonypi_device.camera_power)
285                 return;
286
287         for (j = 5; j > 0; j--) {
288
289                 while (sonypi_call2(0x91, 0x1)) {
290                         set_current_state(TASK_UNINTERRUPTIBLE);
291                         schedule_timeout(1);
292                 }
293                 sonypi_call1(0x93);
294
295                 for (i = 400; i > 0; i--) {
296                         if (sonypi_camera_ready())
297                                 break;
298                         set_current_state(TASK_UNINTERRUPTIBLE);
299                         schedule_timeout(1);
300                 }
301                 if (i)
302                         break;
303         }
304
305         if (j == 0) {
306                 printk(KERN_WARNING "sonypi: failed to power on camera\n");
307                 return;
308         }
309
310         sonypi_set(0x10, 0x5a);
311         sonypi_device.camera_power = 1;
312 }
313
314 /* sets the bluetooth subsystem power state */
315 static void sonypi_setbluetoothpower(u8 state)
316 {
317         state = !!state;
318
319         if (sonypi_device.bluetooth_power == state)
320                 return;
321
322         sonypi_call2(0x96, state);
323         sonypi_call1(0x82);
324         sonypi_device.bluetooth_power = state;
325 }
326
327 static void input_keyrelease(void *data)
328 {
329         struct input_dev *input_dev;
330         int key;
331
332         while (1) {
333                 if (kfifo_get(sonypi_device.input_fifo,
334                               (unsigned char *)&input_dev,
335                               sizeof(input_dev)) != sizeof(input_dev))
336                         return;
337                 if (kfifo_get(sonypi_device.input_fifo,
338                               (unsigned char *)&key,
339                               sizeof(key)) != sizeof(key))
340                         return;
341
342                 msleep(10);
343                 input_report_key(input_dev, key, 0);
344                 input_sync(input_dev);
345         }
346 }
347
348 /* Interrupt handler: some event is available */
349 static irqreturn_t sonypi_irq(int irq, void *dev_id, struct pt_regs *regs)
350 {
351         u8 v1, v2, event = 0;
352         int i, j;
353
354         v1 = inb_p(sonypi_device.ioport1);
355         v2 = inb_p(sonypi_device.ioport1 + sonypi_device.evtype_offset);
356
357         for (i = 0; sonypi_eventtypes[i].model; i++) {
358                 if (sonypi_device.model != sonypi_eventtypes[i].model)
359                         continue;
360                 if ((v2 & sonypi_eventtypes[i].data) !=
361                     sonypi_eventtypes[i].data)
362                         continue;
363                 if (!(mask & sonypi_eventtypes[i].mask))
364                         continue;
365                 for (j = 0; sonypi_eventtypes[i].events[j].event; j++) {
366                         if (v1 == sonypi_eventtypes[i].events[j].data) {
367                                 event = sonypi_eventtypes[i].events[j].event;
368                                 goto found;
369                         }
370                 }
371         }
372
373         if (verbose)
374                 printk(KERN_WARNING
375                        "sonypi: unknown event port1=0x%02x,port2=0x%02x\n",
376                        v1, v2);
377         /* We need to return IRQ_HANDLED here because there *are*
378          * events belonging to the sonypi device we don't know about,
379          * but we still don't want those to pollute the logs... */
380         return IRQ_HANDLED;
381
382 found:
383         if (verbose > 1)
384                 printk(KERN_INFO
385                        "sonypi: event port1=0x%02x,port2=0x%02x\n", v1, v2);
386
387         if (useinput) {
388                 struct input_dev *input_jog_dev = &sonypi_device.input_jog_dev;
389                 struct input_dev *input_key_dev = &sonypi_device.input_key_dev;
390                 switch (event) {
391                 case SONYPI_EVENT_JOGDIAL_UP:
392                 case SONYPI_EVENT_JOGDIAL_UP_PRESSED:
393                         input_report_rel(input_jog_dev, REL_WHEEL, 1);
394                         break;
395                 case SONYPI_EVENT_JOGDIAL_DOWN:
396                 case SONYPI_EVENT_JOGDIAL_DOWN_PRESSED:
397                         input_report_rel(input_jog_dev, REL_WHEEL, -1);
398                         break;
399                 case SONYPI_EVENT_JOGDIAL_PRESSED: {
400                         int key = BTN_MIDDLE;
401                         input_report_key(input_jog_dev, key, 1);
402                         kfifo_put(sonypi_device.input_fifo,
403                                   (unsigned char *)&input_jog_dev,
404                                   sizeof(input_jog_dev));
405                         kfifo_put(sonypi_device.input_fifo,
406                                   (unsigned char *)&key, sizeof(key));
407                         break;
408                 }
409                 case SONYPI_EVENT_FNKEY_RELEASED:
410                         /* Nothing, not all VAIOs generate this event */
411                         break;
412                 }
413                 input_sync(input_jog_dev);
414
415                 for (i = 0; sonypi_inputkeys[i].sonypiev; i++) {
416                         int key;
417
418                         if (event != sonypi_inputkeys[i].sonypiev)
419                                 continue;
420
421                         key = sonypi_inputkeys[i].inputev;
422                         input_report_key(input_key_dev, key, 1);
423                         kfifo_put(sonypi_device.input_fifo,
424                                   (unsigned char *)&input_key_dev,
425                                   sizeof(input_key_dev));
426                         kfifo_put(sonypi_device.input_fifo,
427                                   (unsigned char *)&key, sizeof(key));
428                 }
429                 input_sync(input_key_dev);
430                 schedule_work(&sonypi_device.input_work);
431         }
432
433         kfifo_put(sonypi_device.fifo, (unsigned char *)&event, sizeof(event));
434         kill_fasync(&sonypi_device.fifo_async, SIGIO, POLL_IN);
435         wake_up_interruptible(&sonypi_device.fifo_proc_list);
436
437         return IRQ_HANDLED;
438 }
439
440 /* External camera command (exported to the motion eye v4l driver) */
441 int sonypi_camera_command(int command, u8 value)
442 {
443         if (!camera)
444                 return -EIO;
445
446         down(&sonypi_device.lock);
447
448         switch (command) {
449         case SONYPI_COMMAND_SETCAMERA:
450                 if (value)
451                         sonypi_camera_on();
452                 else
453                         sonypi_camera_off();
454                 break;
455         case SONYPI_COMMAND_SETCAMERABRIGHTNESS:
456                 sonypi_set(SONYPI_CAMERA_BRIGHTNESS, value);
457                 break;
458         case SONYPI_COMMAND_SETCAMERACONTRAST:
459                 sonypi_set(SONYPI_CAMERA_CONTRAST, value);
460                 break;
461         case SONYPI_COMMAND_SETCAMERAHUE:
462                 sonypi_set(SONYPI_CAMERA_HUE, value);
463                 break;
464         case SONYPI_COMMAND_SETCAMERACOLOR:
465                 sonypi_set(SONYPI_CAMERA_COLOR, value);
466                 break;
467         case SONYPI_COMMAND_SETCAMERASHARPNESS:
468                 sonypi_set(SONYPI_CAMERA_SHARPNESS, value);
469                 break;
470         case SONYPI_COMMAND_SETCAMERAPICTURE:
471                 sonypi_set(SONYPI_CAMERA_PICTURE, value);
472                 break;
473         case SONYPI_COMMAND_SETCAMERAAGC:
474                 sonypi_set(SONYPI_CAMERA_AGC, value);
475                 break;
476         default:
477                 printk(KERN_ERR "sonypi: sonypi_camera_command invalid: %d\n",
478                        command);
479                 break;
480         }
481         up(&sonypi_device.lock);
482         return 0;
483 }
484
485 EXPORT_SYMBOL(sonypi_camera_command);
486
487 static int sonypi_misc_fasync(int fd, struct file *filp, int on)
488 {
489         int retval;
490
491         retval = fasync_helper(fd, filp, on, &sonypi_device.fifo_async);
492         if (retval < 0)
493                 return retval;
494         return 0;
495 }
496
497 static int sonypi_misc_release(struct inode *inode, struct file *file)
498 {
499         sonypi_misc_fasync(-1, file, 0);
500         down(&sonypi_device.lock);
501         sonypi_device.open_count--;
502         up(&sonypi_device.lock);
503         return 0;
504 }
505
506 static int sonypi_misc_open(struct inode *inode, struct file *file)
507 {
508         down(&sonypi_device.lock);
509         /* Flush input queue on first open */
510         if (!sonypi_device.open_count)
511                 kfifo_reset(sonypi_device.fifo);
512         sonypi_device.open_count++;
513         up(&sonypi_device.lock);
514         return 0;
515 }
516
517 static ssize_t sonypi_misc_read(struct file *file, char __user *buf,
518                                 size_t count, loff_t *pos)
519 {
520         ssize_t ret;
521         unsigned char c;
522
523         if ((kfifo_len(sonypi_device.fifo) == 0) &&
524             (file->f_flags & O_NONBLOCK))
525                 return -EAGAIN;
526
527         ret = wait_event_interruptible(sonypi_device.fifo_proc_list,
528                                        kfifo_len(sonypi_device.fifo) != 0);
529         if (ret)
530                 return ret;
531
532         while (ret < count &&
533                (kfifo_get(sonypi_device.fifo, &c, sizeof(c)) == sizeof(c))) {
534                 if (put_user(c, buf++))
535                         return -EFAULT;
536                 ret++;
537         }
538
539         if (ret > 0)
540                 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
541
542         return ret;
543 }
544
545 static unsigned int sonypi_misc_poll(struct file *file, poll_table *wait)
546 {
547         poll_wait(file, &sonypi_device.fifo_proc_list, wait);
548         if (kfifo_len(sonypi_device.fifo))
549                 return POLLIN | POLLRDNORM;
550         return 0;
551 }
552
553 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp,
554                              unsigned int cmd, unsigned long arg)
555 {
556         int ret = 0;
557         void __user *argp = (void __user *)arg;
558         u8 val8;
559         u16 val16;
560
561         down(&sonypi_device.lock);
562         switch (cmd) {
563         case SONYPI_IOCGBRT:
564                 if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
565                         ret = -EIO;
566                         break;
567                 }
568                 if (copy_to_user(argp, &val8, sizeof(val8)))
569                         ret = -EFAULT;
570                 break;
571         case SONYPI_IOCSBRT:
572                 if (copy_from_user(&val8, argp, sizeof(val8))) {
573                         ret = -EFAULT;
574                         break;
575                 }
576                 if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
577                         ret = -EIO;
578                 break;
579         case SONYPI_IOCGBAT1CAP:
580                 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
581                         ret = -EIO;
582                         break;
583                 }
584                 if (copy_to_user(argp, &val16, sizeof(val16)))
585                         ret = -EFAULT;
586                 break;
587         case SONYPI_IOCGBAT1REM:
588                 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
589                         ret = -EIO;
590                         break;
591                 }
592                 if (copy_to_user(argp, &val16, sizeof(val16)))
593                         ret = -EFAULT;
594                 break;
595         case SONYPI_IOCGBAT2CAP:
596                 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
597                         ret = -EIO;
598                         break;
599                 }
600                 if (copy_to_user(argp, &val16, sizeof(val16)))
601                         ret = -EFAULT;
602                 break;
603         case SONYPI_IOCGBAT2REM:
604                 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
605                         ret = -EIO;
606                         break;
607                 }
608                 if (copy_to_user(argp, &val16, sizeof(val16)))
609                         ret = -EFAULT;
610                 break;
611         case SONYPI_IOCGBATFLAGS:
612                 if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
613                         ret = -EIO;
614                         break;
615                 }
616                 val8 &= 0x07;
617                 if (copy_to_user(argp, &val8, sizeof(val8)))
618                         ret = -EFAULT;
619                 break;
620         case SONYPI_IOCGBLUE:
621                 val8 = sonypi_device.bluetooth_power;
622                 if (copy_to_user(argp, &val8, sizeof(val8)))
623                         ret = -EFAULT;
624                 break;
625         case SONYPI_IOCSBLUE:
626                 if (copy_from_user(&val8, argp, sizeof(val8))) {
627                         ret = -EFAULT;
628                         break;
629                 }
630                 sonypi_setbluetoothpower(val8);
631                 break;
632         default:
633                 ret = -EINVAL;
634         }
635         up(&sonypi_device.lock);
636         return ret;
637 }
638
639 static struct file_operations sonypi_misc_fops = {
640         .owner          = THIS_MODULE,
641         .read           = sonypi_misc_read,
642         .poll           = sonypi_misc_poll,
643         .open           = sonypi_misc_open,
644         .release        = sonypi_misc_release,
645         .fasync         = sonypi_misc_fasync,
646         .ioctl          = sonypi_misc_ioctl,
647 };
648
649 struct miscdevice sonypi_misc_device = {
650         .minor          = -1,
651         .name           = "sonypi",
652         .fops           = &sonypi_misc_fops,
653 };
654
655 static void sonypi_enable(unsigned int camera_on)
656 {
657         if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2)
658                 sonypi_type2_srs();
659         else
660                 sonypi_type1_srs();
661
662         sonypi_call1(0x82);
663         sonypi_call2(0x81, 0xff);
664         sonypi_call1(compat ? 0x92 : 0x82);
665
666         /* Enable ACPI mode to get Fn key events */
667         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
668                 outb(0xf0, 0xb2);
669
670         if (camera && camera_on)
671                 sonypi_camera_on();
672 }
673
674 static int sonypi_disable(void)
675 {
676         sonypi_call2(0x81, 0);  /* make sure we don't get any more events */
677         if (camera)
678                 sonypi_camera_off();
679
680         /* disable ACPI mode */
681         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
682                 outb(0xf1, 0xb2);
683
684         if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2)
685                 sonypi_type2_dis();
686         else
687                 sonypi_type1_dis();
688         return 0;
689 }
690
691 #ifdef CONFIG_PM
692 static int old_camera_power;
693
694 static int sonypi_suspend(struct device *dev, u32 state, u32 level)
695 {
696         if (level == SUSPEND_DISABLE) {
697                 old_camera_power = sonypi_device.camera_power;
698                 sonypi_disable();
699         }
700         return 0;
701 }
702
703 static int sonypi_resume(struct device *dev, u32 level)
704 {
705         if (level == RESUME_ENABLE)
706                 sonypi_enable(old_camera_power);
707         return 0;
708 }
709 #endif
710
711 static void sonypi_shutdown(struct device *dev)
712 {
713         sonypi_disable();
714 }
715
716 static struct device_driver sonypi_driver = {
717         .name           = "sonypi",
718         .bus            = &platform_bus_type,
719 #ifdef CONFIG_PM
720         .suspend        = sonypi_suspend,
721         .resume         = sonypi_resume,
722 #endif
723         .shutdown       = sonypi_shutdown,
724 };
725
726 static int __devinit sonypi_probe(void)
727 {
728         int i, ret;
729         struct sonypi_ioport_list *ioport_list;
730         struct sonypi_irq_list *irq_list;
731         struct pci_dev *pcidev;
732
733         pcidev = pci_get_device(PCI_VENDOR_ID_INTEL,
734                                 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
735
736         sonypi_device.dev = pcidev;
737         sonypi_device.model = pcidev ?
738                 SONYPI_DEVICE_MODEL_TYPE1 : SONYPI_DEVICE_MODEL_TYPE2;
739
740         sonypi_device.fifo_lock = SPIN_LOCK_UNLOCKED;
741         sonypi_device.fifo = kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
742                                          &sonypi_device.fifo_lock);
743         if (IS_ERR(sonypi_device.fifo)) {
744                 printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
745                 ret = PTR_ERR(sonypi_device.fifo);
746                 goto out_fifo;
747         }
748
749         init_waitqueue_head(&sonypi_device.fifo_proc_list);
750         init_MUTEX(&sonypi_device.lock);
751         sonypi_device.bluetooth_power = -1;
752
753         if (pcidev && pci_enable_device(pcidev)) {
754                 printk(KERN_ERR "sonypi: pci_enable_device failed\n");
755                 ret = -EIO;
756                 goto out_pcienable;
757         }
758
759         sonypi_misc_device.minor = (minor == -1) ? MISC_DYNAMIC_MINOR : minor;
760         if ((ret = misc_register(&sonypi_misc_device))) {
761                 printk(KERN_ERR "sonypi: misc_register failed\n");
762                 goto out_miscreg;
763         }
764
765         if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
766                 ioport_list = sonypi_type2_ioport_list;
767                 sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
768                 sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
769                 irq_list = sonypi_type2_irq_list;
770         } else {
771                 ioport_list = sonypi_type1_ioport_list;
772                 sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
773                 sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
774                 irq_list = sonypi_type1_irq_list;
775         }
776
777         for (i = 0; ioport_list[i].port1; i++) {
778                 if (request_region(ioport_list[i].port1,
779                                    sonypi_device.region_size,
780                                    "Sony Programable I/O Device")) {
781                         /* get the ioport */
782                         sonypi_device.ioport1 = ioport_list[i].port1;
783                         sonypi_device.ioport2 = ioport_list[i].port2;
784                         break;
785                 }
786         }
787         if (!sonypi_device.ioport1) {
788                 printk(KERN_ERR "sonypi: request_region failed\n");
789                 ret = -ENODEV;
790                 goto out_reqreg;
791         }
792
793         for (i = 0; irq_list[i].irq; i++) {
794
795                 sonypi_device.irq = irq_list[i].irq;
796                 sonypi_device.bits = irq_list[i].bits;
797
798                 if (!request_irq(sonypi_device.irq, sonypi_irq,
799                                  SA_SHIRQ, "sonypi", sonypi_irq))
800                         break;
801         }
802
803         if (!irq_list[i].irq) {
804                 printk(KERN_ERR "sonypi: request_irq failed\n");
805                 ret = -ENODEV;
806                 goto out_reqirq;
807         }
808
809         if (useinput) {
810                 /* Initialize the Input Drivers: jogdial */
811                 int i;
812                 sonypi_device.input_jog_dev.evbit[0] =
813                         BIT(EV_KEY) | BIT(EV_REL);
814                 sonypi_device.input_jog_dev.keybit[LONG(BTN_MOUSE)] =
815                         BIT(BTN_MIDDLE);
816                 sonypi_device.input_jog_dev.relbit[0] = BIT(REL_WHEEL);
817                 sonypi_device.input_jog_dev.name =
818                         kmalloc(sizeof(SONYPI_JOG_INPUTNAME), GFP_KERNEL);
819                 if (!sonypi_device.input_jog_dev.name) {
820                         printk(KERN_ERR "sonypi: kmalloc failed\n");
821                         ret = -ENOMEM;
822                         goto out_inkmallocinput1;
823                 }
824                 sprintf(sonypi_device.input_jog_dev.name, SONYPI_JOG_INPUTNAME);
825                 sonypi_device.input_jog_dev.id.bustype = BUS_ISA;
826                 sonypi_device.input_jog_dev.id.vendor = PCI_VENDOR_ID_SONY;
827
828                 input_register_device(&sonypi_device.input_jog_dev);
829                 printk(KERN_INFO "%s input method installed.\n",
830                        sonypi_device.input_jog_dev.name);
831
832                 /* Initialize the Input Drivers: special keys */
833                 sonypi_device.input_key_dev.evbit[0] = BIT(EV_KEY);
834                 for (i = 0; sonypi_inputkeys[i].sonypiev; i++)
835                         if (sonypi_inputkeys[i].inputev)
836                                 set_bit(sonypi_inputkeys[i].inputev,
837                                         sonypi_device.input_key_dev.keybit);
838                 sonypi_device.input_key_dev.name =
839                         kmalloc(sizeof(SONYPI_KEY_INPUTNAME), GFP_KERNEL);
840                 if (!sonypi_device.input_key_dev.name) {
841                         printk(KERN_ERR "sonypi: kmalloc failed\n");
842                         ret = -ENOMEM;
843                         goto out_inkmallocinput2;
844                 }
845                 sprintf(sonypi_device.input_key_dev.name, SONYPI_KEY_INPUTNAME);
846                 sonypi_device.input_key_dev.id.bustype = BUS_ISA;
847                 sonypi_device.input_key_dev.id.vendor = PCI_VENDOR_ID_SONY;
848
849                 input_register_device(&sonypi_device.input_key_dev);
850                 printk(KERN_INFO "%s input method installed.\n",
851                        sonypi_device.input_key_dev.name);
852
853                 sonypi_device.input_fifo_lock = SPIN_LOCK_UNLOCKED;
854                 sonypi_device.input_fifo =
855                         kfifo_alloc(SONYPI_BUF_SIZE, GFP_KERNEL,
856                                     &sonypi_device.input_fifo_lock);
857                 if (IS_ERR(sonypi_device.input_fifo)) {
858                         printk(KERN_ERR "sonypi: kfifo_alloc failed\n");
859                         ret = PTR_ERR(sonypi_device.input_fifo);
860                         goto out_infifo;
861                 }
862
863                 INIT_WORK(&sonypi_device.input_work, input_keyrelease, NULL);
864         }
865
866         sonypi_device.pdev = platform_device_register_simple("sonypi", -1,
867                                                              NULL, 0);
868         if (IS_ERR(sonypi_device.pdev)) {
869                 ret = PTR_ERR(sonypi_device.pdev);
870                 goto out_platformdev;
871         }
872
873         sonypi_enable(0);
874
875         printk(KERN_INFO "sonypi: Sony Programmable I/O Controller Driver"
876                "v%s.\n", SONYPI_DRIVER_VERSION);
877         printk(KERN_INFO "sonypi: detected %s model, "
878                "verbose = %d, fnkeyinit = %s, camera = %s, "
879                "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
880                (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) ?
881                         "type1" : "type2",
882                verbose,
883                fnkeyinit ? "on" : "off",
884                camera ? "on" : "off",
885                compat ? "on" : "off",
886                mask,
887                useinput ? "on" : "off",
888                SONYPI_ACPI_ACTIVE ? "on" : "off");
889         printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
890                sonypi_device.irq,
891                sonypi_device.ioport1, sonypi_device.ioport2);
892
893         if (minor == -1)
894                 printk(KERN_INFO "sonypi: device allocated minor is %d\n",
895                        sonypi_misc_device.minor);
896
897         return 0;
898
899 out_platformdev:
900         kfifo_free(sonypi_device.input_fifo);
901 out_infifo:
902         input_unregister_device(&sonypi_device.input_key_dev);
903         kfree(sonypi_device.input_key_dev.name);
904 out_inkmallocinput2:
905         input_unregister_device(&sonypi_device.input_jog_dev);
906         kfree(sonypi_device.input_jog_dev.name);
907 out_inkmallocinput1:
908         free_irq(sonypi_device.irq, sonypi_irq);
909 out_reqirq:
910         release_region(sonypi_device.ioport1, sonypi_device.region_size);
911 out_reqreg:
912         misc_deregister(&sonypi_misc_device);
913 out_miscreg:
914         if (pcidev)
915                 pci_disable_device(pcidev);
916 out_pcienable:
917         kfifo_free(sonypi_device.fifo);
918 out_fifo:
919         pci_dev_put(sonypi_device.dev);
920         return ret;
921 }
922
923 static void __devexit sonypi_remove(void)
924 {
925         sonypi_disable();
926
927         platform_device_unregister(sonypi_device.pdev);
928
929         if (useinput) {
930                 input_unregister_device(&sonypi_device.input_key_dev);
931                 kfree(sonypi_device.input_key_dev.name);
932                 input_unregister_device(&sonypi_device.input_jog_dev);
933                 kfree(sonypi_device.input_jog_dev.name);
934                 kfifo_free(sonypi_device.input_fifo);
935         }
936
937         free_irq(sonypi_device.irq, sonypi_irq);
938         release_region(sonypi_device.ioport1, sonypi_device.region_size);
939         misc_deregister(&sonypi_misc_device);
940         if (sonypi_device.dev)
941                 pci_disable_device(sonypi_device.dev);
942         kfifo_free(sonypi_device.fifo);
943         pci_dev_put(sonypi_device.dev);
944         printk(KERN_INFO "sonypi: removed.\n");
945 }
946
947 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
948         {
949                 .ident = "Sony Vaio",
950                 .matches = {
951                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
952                         DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
953                 },
954         },
955         {
956                 .ident = "Sony Vaio",
957                 .matches = {
958                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
959                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
960                 },
961         },
962         { }
963 };
964
965 static int __init sonypi_init(void)
966 {
967         int ret;
968
969         if (!dmi_check_system(sonypi_dmi_table))
970                 return -ENODEV;
971
972         ret = driver_register(&sonypi_driver);
973         if (ret)
974                 return ret;
975
976         ret = sonypi_probe();
977         if (ret)
978                 driver_unregister(&sonypi_driver);
979
980         return ret;
981 }
982
983 static void __exit sonypi_exit(void)
984 {
985         driver_unregister(&sonypi_driver);
986         sonypi_remove();
987 }
988
989 module_init(sonypi_init);
990 module_exit(sonypi_exit);