VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / char / sonypi.c
1 /*
2  * Sony Programmable I/O Control Device driver for VAIO
3  *
4  * Copyright (C) 2001-2003 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/sysdev.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 static struct sonypi_device sonypi_device;
57 static int minor = -1;
58 static int verbose; /* = 0 */
59 static int fnkeyinit; /* = 0 */
60 static int camera; /* = 0 */
61 static int compat; /* = 0 */
62 static int useinput = 1;
63 static unsigned long mask = 0xffffffff;
64
65 /* Inits the queue */
66 static inline void sonypi_initq(void) {
67         sonypi_device.queue.head = sonypi_device.queue.tail = 0;
68         sonypi_device.queue.len = 0;
69         sonypi_device.queue.s_lock = (spinlock_t)SPIN_LOCK_UNLOCKED;
70         init_waitqueue_head(&sonypi_device.queue.proc_list);
71 }
72
73 /* Pulls an event from the queue */
74 static inline unsigned char sonypi_pullq(void) {
75         unsigned char result;
76         unsigned long flags;
77
78         spin_lock_irqsave(&sonypi_device.queue.s_lock, flags);
79         if (!sonypi_device.queue.len) {
80                 spin_unlock_irqrestore(&sonypi_device.queue.s_lock, flags);
81                 return 0;
82         }
83         result = sonypi_device.queue.buf[sonypi_device.queue.head];
84         sonypi_device.queue.head++;
85         sonypi_device.queue.head &= (SONYPI_BUF_SIZE - 1);
86         sonypi_device.queue.len--;
87         spin_unlock_irqrestore(&sonypi_device.queue.s_lock, flags);
88         return result;
89 }
90
91 /* Pushes an event into the queue */
92 static inline void sonypi_pushq(unsigned char event) {
93         unsigned long flags;
94
95         spin_lock_irqsave(&sonypi_device.queue.s_lock, flags);
96         if (sonypi_device.queue.len == SONYPI_BUF_SIZE) {
97                 /* remove the first element */
98                 sonypi_device.queue.head++;
99                 sonypi_device.queue.head &= (SONYPI_BUF_SIZE - 1);
100                 sonypi_device.queue.len--;
101         }
102         sonypi_device.queue.buf[sonypi_device.queue.tail] = event;
103         sonypi_device.queue.tail++;
104         sonypi_device.queue.tail &= (SONYPI_BUF_SIZE - 1);
105         sonypi_device.queue.len++;
106
107         kill_fasync(&sonypi_device.queue.fasync, SIGIO, POLL_IN);
108         wake_up_interruptible(&sonypi_device.queue.proc_list);
109         spin_unlock_irqrestore(&sonypi_device.queue.s_lock, flags);
110 }
111
112 /* Tests if the queue is empty */
113 static inline int sonypi_emptyq(void) {
114         int result;
115         unsigned long flags;
116
117         spin_lock_irqsave(&sonypi_device.queue.s_lock, flags);
118         result = (sonypi_device.queue.len == 0);
119         spin_unlock_irqrestore(&sonypi_device.queue.s_lock, flags);
120         return result;
121 }
122
123 static int ec_read16(u8 addr, u16 *value) {
124         u8 val_lb, val_hb;
125         if (sonypi_ec_read(addr, &val_lb))
126                 return -1;
127         if (sonypi_ec_read(addr + 1, &val_hb))
128                 return -1;
129         *value = val_lb | (val_hb << 8);
130         return 0;
131 }
132
133 /* Initializes the device - this comes from the AML code in the ACPI bios */
134 static void sonypi_type1_srs(void) {
135         u32 v;
136
137         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
138         v = (v & 0xFFFF0000) | ((u32)sonypi_device.ioport1);
139         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
140
141         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
142         v = (v & 0xFFF0FFFF) | 
143             (((u32)sonypi_device.ioport1 ^ sonypi_device.ioport2) << 16);
144         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
145
146         v = inl(SONYPI_IRQ_PORT);
147         v &= ~(((u32)0x3) << SONYPI_IRQ_SHIFT);
148         v |= (((u32)sonypi_device.bits) << SONYPI_IRQ_SHIFT);
149         outl(v, SONYPI_IRQ_PORT);
150
151         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
152         v = (v & 0xFF1FFFFF) | 0x00C00000;
153         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
154 }
155
156 static void sonypi_type2_srs(void) {
157         if (sonypi_ec_write(SONYPI_SHIB, (sonypi_device.ioport1 & 0xFF00) >> 8))
158                 printk(KERN_WARNING "ec_write failed\n");
159         if (sonypi_ec_write(SONYPI_SLOB,  sonypi_device.ioport1 & 0x00FF))
160                 printk(KERN_WARNING "ec_write failed\n");
161         if (sonypi_ec_write(SONYPI_SIRQ,  sonypi_device.bits))
162                 printk(KERN_WARNING "ec_write failed\n");
163         udelay(10);
164 }
165
166 /* Disables the device - this comes from the AML code in the ACPI bios */
167 static void sonypi_type1_dis(void) {
168         u32 v;
169
170         pci_read_config_dword(sonypi_device.dev, SONYPI_G10A, &v);
171         v = v & 0xFF3FFFFF;
172         pci_write_config_dword(sonypi_device.dev, SONYPI_G10A, v);
173
174         v = inl(SONYPI_IRQ_PORT);
175         v |= (0x3 << SONYPI_IRQ_SHIFT);
176         outl(v, SONYPI_IRQ_PORT);
177 }
178
179 static void sonypi_type2_dis(void) {
180         if (sonypi_ec_write(SONYPI_SHIB, 0))
181                 printk(KERN_WARNING "ec_write failed\n");
182         if (sonypi_ec_write(SONYPI_SLOB, 0))
183                 printk(KERN_WARNING "ec_write failed\n");
184         if (sonypi_ec_write(SONYPI_SIRQ, 0))
185                 printk(KERN_WARNING "ec_write failed\n");
186 }
187
188 static u8 sonypi_call1(u8 dev) {
189         u8 v1, v2;
190
191         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
192         outb(dev, sonypi_device.ioport2);
193         v1 = inb_p(sonypi_device.ioport2);
194         v2 = inb_p(sonypi_device.ioport1);
195         return v2;
196 }
197
198 static u8 sonypi_call2(u8 dev, u8 fn) {
199         u8 v1;
200
201         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
202         outb(dev, sonypi_device.ioport2);
203         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
204         outb(fn, sonypi_device.ioport1);
205         v1 = inb_p(sonypi_device.ioport1);
206         return v1;
207 }
208
209 static u8 sonypi_call3(u8 dev, u8 fn, u8 v) {
210         u8 v1;
211
212         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
213         outb(dev, sonypi_device.ioport2);
214         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
215         outb(fn, sonypi_device.ioport1);
216         wait_on_command(0, inb_p(sonypi_device.ioport2) & 2, ITERATIONS_LONG);
217         outb(v, sonypi_device.ioport1);
218         v1 = inb_p(sonypi_device.ioport1);
219         return v1;
220 }
221
222 static u8 sonypi_read(u8 fn) {
223         u8 v1, v2;
224         int n = 100;
225
226         while (n--) {
227                 v1 = sonypi_call2(0x8f, fn);
228                 v2 = sonypi_call2(0x8f, fn);
229                 if (v1 == v2 && v1 != 0xff)
230                         return v1;
231         }
232         return 0xff;
233 }
234
235 /* Set brightness, hue etc */
236 static void sonypi_set(u8 fn, u8 v) {
237         
238         wait_on_command(0, sonypi_call3(0x90, fn, v), ITERATIONS_SHORT);
239 }
240
241 /* Tests if the camera is ready */
242 static int sonypi_camera_ready(void) {
243         u8 v;
244
245         v = sonypi_call2(0x8f, SONYPI_CAMERA_STATUS);
246         return (v != 0xff && (v & SONYPI_CAMERA_STATUS_READY));
247 }
248
249 /* Turns the camera off */
250 static void sonypi_camera_off(void) {
251
252         sonypi_set(SONYPI_CAMERA_PICTURE, SONYPI_CAMERA_MUTE_MASK);
253
254         if (!sonypi_device.camera_power)
255                 return;
256
257         sonypi_call2(0x91, 0); 
258         sonypi_device.camera_power = 0;
259 }
260
261 /* Turns the camera on */
262 static void sonypi_camera_on(void) {
263         int i, j;
264
265         if (sonypi_device.camera_power)
266                 return;
267
268         for (j = 5; j > 0; j--) {
269
270                 while (sonypi_call2(0x91, 0x1)) {
271                         set_current_state(TASK_UNINTERRUPTIBLE);
272                         schedule_timeout(1);
273                 }
274                 sonypi_call1(0x93);
275
276                 for (i = 400; i > 0; i--) {
277                         if (sonypi_camera_ready())
278                                 break;
279                         set_current_state(TASK_UNINTERRUPTIBLE);
280                         schedule_timeout(1);
281                 }
282                 if (i)
283                         break;
284         }
285         
286         if (j == 0) {
287                 printk(KERN_WARNING "sonypi: failed to power on camera\n");
288                 return;
289         }
290
291         sonypi_set(0x10, 0x5a);
292         sonypi_device.camera_power = 1;
293 }
294
295 /* sets the bluetooth subsystem power state */
296 static void sonypi_setbluetoothpower(u8 state) {
297
298         state = !!state;
299         if (sonypi_device.bluetooth_power == state) 
300                 return;
301         
302         sonypi_call2(0x96, state);
303         sonypi_call1(0x82);
304         sonypi_device.bluetooth_power = state;
305 }
306
307 /* Interrupt handler: some event is available */
308 static irqreturn_t sonypi_irq(int irq, void *dev_id, struct pt_regs *regs) {
309         u8 v1, v2, event = 0;
310         int i, j;
311
312         v1 = inb_p(sonypi_device.ioport1);
313         v2 = inb_p(sonypi_device.ioport1 + sonypi_device.evtype_offset);
314
315         for (i = 0; sonypi_eventtypes[i].model; i++) {
316                 if (sonypi_device.model != sonypi_eventtypes[i].model)
317                         continue;
318                 if ((v2 & sonypi_eventtypes[i].data) != sonypi_eventtypes[i].data)
319                         continue;
320                 if (! (mask & sonypi_eventtypes[i].mask))
321                         continue;
322                 for (j = 0; sonypi_eventtypes[i].events[j].event; j++) {
323                         if (v1 == sonypi_eventtypes[i].events[j].data) {
324                                 event = sonypi_eventtypes[i].events[j].event;
325                                 goto found;
326                         }
327                 }
328         }
329
330         if (verbose)
331                 printk(KERN_WARNING 
332                        "sonypi: unknown event port1=0x%02x,port2=0x%02x\n",v1,v2);
333         /* We need to return IRQ_HANDLED here because there *are*
334          * events belonging to the sonypi device we don't know about, 
335          * but we still don't want those to pollute the logs... */
336         return IRQ_HANDLED;
337
338 found:
339         if (verbose > 1)
340                 printk(KERN_INFO 
341                        "sonypi: event port1=0x%02x,port2=0x%02x\n", v1, v2);
342
343 #ifdef SONYPI_USE_INPUT
344         if (useinput) {
345                 struct input_dev *jog_dev = &sonypi_device.jog_dev;
346                 if (event == SONYPI_EVENT_JOGDIAL_PRESSED)
347                         input_report_key(jog_dev, BTN_MIDDLE, 1);
348                 else if (event == SONYPI_EVENT_ANYBUTTON_RELEASED)
349                         input_report_key(jog_dev, BTN_MIDDLE, 0);
350                 else if ((event == SONYPI_EVENT_JOGDIAL_UP) ||
351                          (event == SONYPI_EVENT_JOGDIAL_UP_PRESSED))
352                         input_report_rel(jog_dev, REL_WHEEL, 1);
353                 else if ((event == SONYPI_EVENT_JOGDIAL_DOWN) ||
354                          (event == SONYPI_EVENT_JOGDIAL_DOWN_PRESSED))
355                         input_report_rel(jog_dev, REL_WHEEL, -1);
356                 input_sync(jog_dev);
357         }
358 #endif /* SONYPI_USE_INPUT */
359         sonypi_pushq(event);
360         return IRQ_HANDLED;
361 }
362
363 /* External camera command (exported to the motion eye v4l driver) */
364 u8 sonypi_camera_command(int command, u8 value) {
365         u8 ret = 0;
366
367         if (!camera)
368                 return 0;
369
370         down(&sonypi_device.lock);
371
372         switch(command) {
373                 case SONYPI_COMMAND_GETCAMERA:
374                         ret = sonypi_camera_ready();
375                         break;
376                 case SONYPI_COMMAND_SETCAMERA:
377                         if (value)
378                                 sonypi_camera_on();
379                         else
380                                 sonypi_camera_off();
381                         break;
382                 case SONYPI_COMMAND_GETCAMERABRIGHTNESS:
383                         ret = sonypi_read(SONYPI_CAMERA_BRIGHTNESS);
384                         break;
385                 case SONYPI_COMMAND_SETCAMERABRIGHTNESS:
386                         sonypi_set(SONYPI_CAMERA_BRIGHTNESS, value);
387                         break;
388                 case SONYPI_COMMAND_GETCAMERACONTRAST:
389                         ret = sonypi_read(SONYPI_CAMERA_CONTRAST);
390                         break;
391                 case SONYPI_COMMAND_SETCAMERACONTRAST:
392                         sonypi_set(SONYPI_CAMERA_CONTRAST, value);
393                         break;
394                 case SONYPI_COMMAND_GETCAMERAHUE:
395                         ret = sonypi_read(SONYPI_CAMERA_HUE);
396                         break;
397                 case SONYPI_COMMAND_SETCAMERAHUE:
398                         sonypi_set(SONYPI_CAMERA_HUE, value);
399                         break;
400                 case SONYPI_COMMAND_GETCAMERACOLOR:
401                         ret = sonypi_read(SONYPI_CAMERA_COLOR);
402                         break;
403                 case SONYPI_COMMAND_SETCAMERACOLOR:
404                         sonypi_set(SONYPI_CAMERA_COLOR, value);
405                         break;
406                 case SONYPI_COMMAND_GETCAMERASHARPNESS:
407                         ret = sonypi_read(SONYPI_CAMERA_SHARPNESS);
408                         break;
409                 case SONYPI_COMMAND_SETCAMERASHARPNESS:
410                         sonypi_set(SONYPI_CAMERA_SHARPNESS, value);
411                         break;
412                 case SONYPI_COMMAND_GETCAMERAPICTURE:
413                         ret = sonypi_read(SONYPI_CAMERA_PICTURE);
414                         break;
415                 case SONYPI_COMMAND_SETCAMERAPICTURE:
416                         sonypi_set(SONYPI_CAMERA_PICTURE, value);
417                         break;
418                 case SONYPI_COMMAND_GETCAMERAAGC:
419                         ret = sonypi_read(SONYPI_CAMERA_AGC);
420                         break;
421                 case SONYPI_COMMAND_SETCAMERAAGC:
422                         sonypi_set(SONYPI_CAMERA_AGC, value);
423                         break;
424                 case SONYPI_COMMAND_GETCAMERADIRECTION:
425                         ret = sonypi_read(SONYPI_CAMERA_STATUS);
426                         ret &= SONYPI_DIRECTION_BACKWARDS;
427                         break;
428                 case SONYPI_COMMAND_GETCAMERAROMVERSION:
429                         ret = sonypi_read(SONYPI_CAMERA_ROMVERSION);
430                         break;
431                 case SONYPI_COMMAND_GETCAMERAREVISION:
432                         ret = sonypi_read(SONYPI_CAMERA_REVISION);
433                         break;
434         }
435         up(&sonypi_device.lock);
436         return ret;
437 }
438
439 static int sonypi_misc_fasync(int fd, struct file *filp, int on) {
440         int retval;
441
442         retval = fasync_helper(fd, filp, on, &sonypi_device.queue.fasync);
443         if (retval < 0)
444                 return retval;
445         return 0;
446 }
447
448 static int sonypi_misc_release(struct inode * inode, struct file * file) {
449         sonypi_misc_fasync(-1, file, 0);
450         down(&sonypi_device.lock);
451         sonypi_device.open_count--;
452         up(&sonypi_device.lock);
453         return 0;
454 }
455
456 static int sonypi_misc_open(struct inode * inode, struct file * file) {
457         down(&sonypi_device.lock);
458         /* Flush input queue on first open */
459         if (!sonypi_device.open_count)
460                 sonypi_initq();
461         sonypi_device.open_count++;
462         up(&sonypi_device.lock);
463         return 0;
464 }
465
466 static ssize_t sonypi_misc_read(struct file * file, char __user * buf,
467                         size_t count, loff_t *pos)
468 {
469         DECLARE_WAITQUEUE(wait, current);
470         ssize_t i = count;
471         unsigned char c;
472
473         if (sonypi_emptyq()) {
474                 if (file->f_flags & O_NONBLOCK)
475                         return -EAGAIN;
476                 add_wait_queue(&sonypi_device.queue.proc_list, &wait);
477 repeat:
478                 set_current_state(TASK_INTERRUPTIBLE);
479                 if (sonypi_emptyq() && !signal_pending(current)) {
480                         schedule();
481                         goto repeat;
482                 }
483                 current->state = TASK_RUNNING;
484                 remove_wait_queue(&sonypi_device.queue.proc_list, &wait);
485         }
486         while (i > 0 && !sonypi_emptyq()) {
487                 c = sonypi_pullq();
488                 put_user(c, buf++);
489                 i--;
490         }
491         if (count - i) {
492                 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
493                 return count-i;
494         }
495         if (signal_pending(current))
496                 return -ERESTARTSYS;
497         return 0;
498 }
499
500 static unsigned int sonypi_misc_poll(struct file *file, poll_table * wait) {
501         poll_wait(file, &sonypi_device.queue.proc_list, wait);
502         if (!sonypi_emptyq())
503                 return POLLIN | POLLRDNORM;
504         return 0;
505 }
506
507 static int sonypi_misc_ioctl(struct inode *ip, struct file *fp, 
508                              unsigned int cmd, unsigned long arg) {
509         int ret = 0;
510         void __user *argp = (void __user *)arg;
511         u8 val8;
512         u16 val16;
513
514         down(&sonypi_device.lock);
515         switch (cmd) {
516         case SONYPI_IOCGBRT:
517                 if (sonypi_ec_read(SONYPI_LCD_LIGHT, &val8)) {
518                         ret = -EIO;
519                         break;
520                 }
521                 if (copy_to_user(argp, &val8, sizeof(val8)))
522                         ret = -EFAULT;
523                 break;
524         case SONYPI_IOCSBRT:
525                 if (copy_from_user(&val8, argp, sizeof(val8))) {
526                         ret = -EFAULT;
527                         break;
528                 }
529                 if (sonypi_ec_write(SONYPI_LCD_LIGHT, val8))
530                         ret = -EIO;
531                 break;
532         case SONYPI_IOCGBAT1CAP:
533                 if (ec_read16(SONYPI_BAT1_FULL, &val16)) {
534                         ret = -EIO;
535                         break;
536                 }
537                 if (copy_to_user(argp, &val16, sizeof(val16)))
538                         ret = -EFAULT;
539                 break;
540         case SONYPI_IOCGBAT1REM:
541                 if (ec_read16(SONYPI_BAT1_LEFT, &val16)) {
542                         ret = -EIO;
543                         break;
544                 }
545                 if (copy_to_user(argp, &val16, sizeof(val16)))
546                         ret = -EFAULT;
547                 break;
548         case SONYPI_IOCGBAT2CAP:
549                 if (ec_read16(SONYPI_BAT2_FULL, &val16)) {
550                         ret = -EIO;
551                         break;
552                 }
553                 if (copy_to_user(argp, &val16, sizeof(val16)))
554                         ret = -EFAULT;
555                 break;
556         case SONYPI_IOCGBAT2REM:
557                 if (ec_read16(SONYPI_BAT2_LEFT, &val16)) {
558                         ret = -EIO;
559                         break;
560                 }
561                 if (copy_to_user(argp, &val16, sizeof(val16)))
562                         ret = -EFAULT;
563                 break;
564         case SONYPI_IOCGBATFLAGS:
565                 if (sonypi_ec_read(SONYPI_BAT_FLAGS, &val8)) {
566                         ret = -EIO;
567                         break;
568                 }
569                 val8 &= 0x07;
570                 if (copy_to_user(argp, &val8, sizeof(val8)))
571                         ret = -EFAULT;
572                 break;
573         case SONYPI_IOCGBLUE:
574                 val8 = sonypi_device.bluetooth_power;
575                 if (copy_to_user(argp, &val8, sizeof(val8)))
576                         ret = -EFAULT;
577                 break;
578         case SONYPI_IOCSBLUE:
579                 if (copy_from_user(&val8, argp, sizeof(val8))) {
580                         ret = -EFAULT;
581                         break;
582                 }
583                 sonypi_setbluetoothpower(val8);
584                 break;
585         default:
586                 ret = -EINVAL;
587         }
588         up(&sonypi_device.lock);
589         return ret;
590 }
591
592 static struct file_operations sonypi_misc_fops = {
593         .owner          = THIS_MODULE,
594         .read           = sonypi_misc_read,
595         .poll           = sonypi_misc_poll,
596         .open           = sonypi_misc_open,
597         .release        = sonypi_misc_release,
598         .fasync         = sonypi_misc_fasync,
599         .ioctl          = sonypi_misc_ioctl,
600 };
601
602 struct miscdevice sonypi_misc_device = {
603         -1, "sonypi", &sonypi_misc_fops
604 };
605
606 #ifdef CONFIG_PM
607 static int old_camera_power;
608
609 static int sonypi_suspend(struct sys_device *dev, u32 state) {
610         sonypi_call2(0x81, 0); /* make sure we don't get any more events */
611         if (camera) {
612                 old_camera_power = sonypi_device.camera_power;
613                 sonypi_camera_off();
614         }
615         if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2)
616                 sonypi_type2_dis();
617         else
618                 sonypi_type1_dis();
619         /* disable ACPI mode */
620         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
621                 outb(0xf1, 0xb2);
622         return 0;
623 }
624
625 static int sonypi_resume(struct sys_device *dev) {
626         /* Enable ACPI mode to get Fn key events */
627         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
628                 outb(0xf0, 0xb2);
629         if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2)
630                 sonypi_type2_srs();
631         else
632                 sonypi_type1_srs();
633         sonypi_call1(0x82);
634         sonypi_call2(0x81, 0xff);
635         if (compat)
636                 sonypi_call1(0x92); 
637         else
638                 sonypi_call1(0x82);
639         if (camera && old_camera_power)
640                 sonypi_camera_on();
641         return 0;
642 }
643
644 /* Old PM scheme */
645 static int sonypi_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data) {
646
647         switch (rqst) {
648                 case PM_SUSPEND:
649                         sonypi_suspend(NULL, 0);
650                         break;
651                 case PM_RESUME:
652                         sonypi_resume(NULL);
653                         break;
654         }
655         return 0;
656 }
657
658 /* New PM scheme (device model) */
659 static struct sysdev_class sonypi_sysclass = {
660         set_kset_name("sonypi"),
661         .suspend = sonypi_suspend,
662         .resume = sonypi_resume,
663 };
664
665 static struct sys_device sonypi_sysdev = {
666         .id = 0,
667         .cls = &sonypi_sysclass,
668 };
669 #endif
670
671 static int __devinit sonypi_probe(struct pci_dev *pcidev) {
672         int i, ret;
673         struct sonypi_ioport_list *ioport_list;
674         struct sonypi_irq_list *irq_list;
675
676         sonypi_device.dev = pcidev;
677         if (pcidev)
678                 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE1;
679         else
680                 sonypi_device.model = SONYPI_DEVICE_MODEL_TYPE2;
681         sonypi_initq();
682         init_MUTEX(&sonypi_device.lock);
683         sonypi_device.bluetooth_power = 0;
684         
685         if (pcidev && pci_enable_device(pcidev)) {
686                 printk(KERN_ERR "sonypi: pci_enable_device failed\n");
687                 ret = -EIO;
688                 goto out1;
689         }
690
691         sonypi_misc_device.minor = (minor == -1) ? 
692                 MISC_DYNAMIC_MINOR : minor;
693         if ((ret = misc_register(&sonypi_misc_device))) {
694                 printk(KERN_ERR "sonypi: misc_register failed\n");
695                 goto out1;
696         }
697
698         if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2) {
699                 ioport_list = sonypi_type2_ioport_list;
700                 sonypi_device.region_size = SONYPI_TYPE2_REGION_SIZE;
701                 sonypi_device.evtype_offset = SONYPI_TYPE2_EVTYPE_OFFSET;
702                 irq_list = sonypi_type2_irq_list;
703         }
704         else {
705                 ioport_list = sonypi_type1_ioport_list;
706                 sonypi_device.region_size = SONYPI_TYPE1_REGION_SIZE;
707                 sonypi_device.evtype_offset = SONYPI_TYPE1_EVTYPE_OFFSET;
708                 irq_list = sonypi_type1_irq_list;
709         }
710
711         for (i = 0; ioport_list[i].port1; i++) {
712                 if (request_region(ioport_list[i].port1, 
713                                    sonypi_device.region_size, 
714                                    "Sony Programable I/O Device")) {
715                         /* get the ioport */
716                         sonypi_device.ioport1 = ioport_list[i].port1;
717                         sonypi_device.ioport2 = ioport_list[i].port2;
718                         break;
719                 }
720         }
721         if (!sonypi_device.ioport1) {
722                 printk(KERN_ERR "sonypi: request_region failed\n");
723                 ret = -ENODEV;
724                 goto out2;
725         }
726
727         for (i = 0; irq_list[i].irq; i++) {
728
729                 sonypi_device.irq = irq_list[i].irq;
730                 sonypi_device.bits = irq_list[i].bits;
731
732                 /* Enable sonypi IRQ settings */
733                 if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2)
734                         sonypi_type2_srs();
735                 else
736                         sonypi_type1_srs();
737
738                 sonypi_call1(0x82);
739                 sonypi_call2(0x81, 0xff);
740                 if (compat)
741                         sonypi_call1(0x92); 
742                 else
743                         sonypi_call1(0x82);
744
745                 /* Now try requesting the irq from the system */
746                 if (!request_irq(sonypi_device.irq, sonypi_irq, 
747                                  SA_SHIRQ, "sonypi", sonypi_irq))
748                         break;
749
750                 /* If request_irq failed, disable sonypi IRQ settings */
751                 if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2)
752                         sonypi_type2_dis();
753                 else
754                         sonypi_type1_dis();
755         }
756
757         if (!irq_list[i].irq) {
758                 printk(KERN_ERR "sonypi: request_irq failed\n");
759                 ret = -ENODEV;
760                 goto out3;
761         }
762
763 #ifdef CONFIG_PM
764         sonypi_device.pm = pm_register(PM_PCI_DEV, 0, sonypi_pm_callback);
765
766         if (sysdev_class_register(&sonypi_sysclass) != 0) {
767                 printk(KERN_ERR "sonypi: sysdev_class_register failed\n");
768                 ret = -ENODEV;
769                 goto out4;
770         }
771         if (sysdev_register(&sonypi_sysdev) != 0) {
772                 printk(KERN_ERR "sonypi: sysdev_register failed\n");
773                 ret = -ENODEV;
774                 goto out5;
775         }
776 #endif
777
778         /* Enable ACPI mode to get Fn key events */
779         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
780                 outb(0xf0, 0xb2);
781
782         printk(KERN_INFO "sonypi: Sony Programmable I/O Controller Driver v%d.%d.\n",
783                SONYPI_DRIVER_MAJORVERSION,
784                SONYPI_DRIVER_MINORVERSION);
785         printk(KERN_INFO "sonypi: detected %s model, "
786                "verbose = %d, fnkeyinit = %s, camera = %s, "
787                "compat = %s, mask = 0x%08lx, useinput = %s, acpi = %s\n",
788                (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE1) ?
789                         "type1" : "type2",
790                verbose,
791                fnkeyinit ? "on" : "off",
792                camera ? "on" : "off",
793                compat ? "on" : "off",
794                mask,
795                useinput ? "on" : "off",
796                SONYPI_ACPI_ACTIVE ? "on" : "off");
797         printk(KERN_INFO "sonypi: enabled at irq=%d, port1=0x%x, port2=0x%x\n",
798                sonypi_device.irq, 
799                sonypi_device.ioport1, sonypi_device.ioport2);
800
801         if (minor == -1)
802                 printk(KERN_INFO "sonypi: device allocated minor is %d\n",
803                        sonypi_misc_device.minor);
804
805 #ifdef SONYPI_USE_INPUT
806         if (useinput) {
807                 /* Initialize the Input Drivers: */
808                 sonypi_device.jog_dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
809                 sonypi_device.jog_dev.keybit[LONG(BTN_MOUSE)] = BIT(BTN_MIDDLE);
810                 sonypi_device.jog_dev.relbit[0] = BIT(REL_WHEEL);
811                 sonypi_device.jog_dev.name = (char *) kmalloc(
812                         sizeof(SONYPI_INPUTNAME), GFP_KERNEL);
813                 sprintf(sonypi_device.jog_dev.name, SONYPI_INPUTNAME);
814                 sonypi_device.jog_dev.id.bustype = BUS_ISA;
815                 sonypi_device.jog_dev.id.vendor = PCI_VENDOR_ID_SONY;
816           
817                 input_register_device(&sonypi_device.jog_dev);
818                 printk(KERN_INFO "%s installed.\n", sonypi_device.jog_dev.name);
819         }
820 #endif /* SONYPI_USE_INPUT */
821
822         return 0;
823
824 #ifdef CONFIG_PM
825 out5:
826         sysdev_class_unregister(&sonypi_sysclass);
827 out4:
828         free_irq(sonypi_device.irq, sonypi_irq);
829 #endif
830 out3:
831         release_region(sonypi_device.ioport1, sonypi_device.region_size);
832 out2:
833         misc_deregister(&sonypi_misc_device);
834 out1:
835         return ret;
836 }
837
838 static void __devexit sonypi_remove(void) {
839
840 #ifdef CONFIG_PM
841         pm_unregister(sonypi_device.pm);
842
843         sysdev_unregister(&sonypi_sysdev);
844         sysdev_class_unregister(&sonypi_sysclass);
845 #endif
846
847         sonypi_call2(0x81, 0); /* make sure we don't get any more events */
848         
849 #ifdef SONYPI_USE_INPUT
850         if (useinput) {
851                 input_unregister_device(&sonypi_device.jog_dev);
852                 kfree(sonypi_device.jog_dev.name);
853         }
854 #endif /* SONYPI_USE_INPUT */
855
856         if (camera)
857                 sonypi_camera_off();
858         if (sonypi_device.model == SONYPI_DEVICE_MODEL_TYPE2)
859                 sonypi_type2_dis();
860         else
861                 sonypi_type1_dis();
862         /* disable ACPI mode */
863         if (!SONYPI_ACPI_ACTIVE && fnkeyinit)
864                 outb(0xf1, 0xb2);
865         free_irq(sonypi_device.irq, sonypi_irq);
866         release_region(sonypi_device.ioport1, sonypi_device.region_size);
867         misc_deregister(&sonypi_misc_device);
868         printk(KERN_INFO "sonypi: removed.\n");
869 }
870
871 static struct dmi_system_id __initdata sonypi_dmi_table[] = {
872         {
873                 .ident = "Sony Vaio",
874                 .matches = {
875                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
876                         DMI_MATCH(DMI_PRODUCT_NAME, "PCG-"),
877                 },
878         },
879         {
880                 .ident = "Sony Vaio",
881                 .matches = {
882                         DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
883                         DMI_MATCH(DMI_PRODUCT_NAME, "VGN-"),
884                 },
885         },
886         { }
887 };
888
889 static int __init sonypi_init_module(void)
890 {
891         struct pci_dev *pcidev = NULL;
892         if (dmi_check_system(sonypi_dmi_table)) {
893                 pcidev = pci_find_device(PCI_VENDOR_ID_INTEL, 
894                                          PCI_DEVICE_ID_INTEL_82371AB_3, 
895                                          NULL);
896                 return sonypi_probe(pcidev);
897         }
898         else
899                 return -ENODEV;
900 }
901
902 static void __exit sonypi_cleanup_module(void) {
903         sonypi_remove();
904 }
905
906 #ifndef MODULE
907 static int __init sonypi_setup(char *str)  {
908         int ints[8];
909
910         str = get_options(str, ARRAY_SIZE(ints), ints);
911         if (ints[0] <= 0) 
912                 goto out;
913         minor = ints[1];
914         if (ints[0] == 1)
915                 goto out;
916         verbose = ints[2];
917         if (ints[0] == 2)
918                 goto out;
919         fnkeyinit = ints[3];
920         if (ints[0] == 3)
921                 goto out;
922         camera = ints[4];
923         if (ints[0] == 4)
924                 goto out;
925         compat = ints[5];
926         if (ints[0] == 5)
927                 goto out;
928         mask = ints[6];
929         if (ints[0] == 6)
930                 goto out;
931         useinput = ints[7];
932 out:
933         return 1;
934 }
935
936 __setup("sonypi=", sonypi_setup);
937 #endif /* !MODULE */
938         
939 /* Module entry points */
940 module_init(sonypi_init_module);
941 module_exit(sonypi_cleanup_module);
942
943 MODULE_AUTHOR("Stelian Pop <stelian@popies.net>");
944 MODULE_DESCRIPTION("Sony Programmable I/O Control Device driver");
945 MODULE_LICENSE("GPL");
946
947
948 MODULE_PARM(minor,"i");
949 MODULE_PARM_DESC(minor, "minor number of the misc device, default is -1 (automatic)");
950 MODULE_PARM(verbose,"i");
951 MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
952 MODULE_PARM(fnkeyinit,"i");
953 MODULE_PARM_DESC(fnkeyinit, "set this if your Fn keys do not generate any event");
954 MODULE_PARM(camera,"i");
955 MODULE_PARM_DESC(camera, "set this if you have a MotionEye camera (PictureBook series)");
956 MODULE_PARM(compat,"i");
957 MODULE_PARM_DESC(compat, "set this if you want to enable backward compatibility mode");
958 MODULE_PARM(mask, "i");
959 MODULE_PARM_DESC(mask, "set this to the mask of event you want to enable (see doc)");
960 MODULE_PARM(useinput, "i");
961 MODULE_PARM_DESC(useinput, "if you have a jogdial, set this if you would like it to use the modern Linux Input Driver system");
962
963 EXPORT_SYMBOL(sonypi_camera_command);