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