sony-laptop: Prepare the platform driver for multiple users.
[linux-2.6] / drivers / misc / sony-laptop.c
1 /*
2  * ACPI Sony Notebook Control Driver (SNC)
3  *
4  * Copyright (C) 2004-2005 Stelian Pop <stelian@popies.net>
5  * Copyright (C) 2007 Mattia Dongili <malattia@linux.it>
6  *
7  * Parts of this driver inspired from asus_acpi.c and ibm_acpi.c
8  * which are copyrighted by their respective authors.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/backlight.h>
32 #include <linux/platform_device.h>
33 #include <linux/err.h>
34 #include <acpi/acpi_drivers.h>
35 #include <acpi/acpi_bus.h>
36 #include <asm/uaccess.h>
37
38 #define SONY_NC_CLASS           "sony"
39 #define SONY_NC_HID             "SNY5001"
40 #define SONY_NC_DRIVER_NAME     "ACPI Sony Notebook Control Driver v0.4"
41
42 /* the device uses 1-based values, while the backlight subsystem uses
43    0-based values */
44 #define SONY_MAX_BRIGHTNESS     8
45
46 #define LOG_PFX                 KERN_WARNING "sony-laptop: "
47
48 MODULE_AUTHOR("Stelian Pop, Mattia Dongili");
49 MODULE_DESCRIPTION(SONY_NC_DRIVER_NAME);
50 MODULE_LICENSE("GPL");
51
52 static int debug;
53 module_param(debug, int, 0);
54 MODULE_PARM_DESC(debug, "set this to 1 (and RTFM) if you want to help "
55                  "the development of this driver");
56
57 /*********** Platform Device ***********/
58
59 static atomic_t sony_pf_users = ATOMIC_INIT(0);
60 static struct platform_driver sony_pf_driver = {
61         .driver = {
62                    .name = "sony-laptop",
63                    .owner = THIS_MODULE,
64                    }
65 };
66 static struct platform_device *sony_pf_device;
67
68 static int sony_pf_add(void)
69 {
70         int ret = 0;
71
72         /* don't run again if already initialized */
73         if (atomic_add_return(1, &sony_pf_users) > 1)
74                 return 0;
75
76         ret = platform_driver_register(&sony_pf_driver);
77         if (ret)
78                 goto out;
79
80         sony_pf_device = platform_device_alloc("sony-laptop", -1);
81         if (!sony_pf_device) {
82                 ret = -ENOMEM;
83                 goto out_platform_registered;
84         }
85
86         ret = platform_device_add(sony_pf_device);
87         if (ret)
88                 goto out_platform_alloced;
89
90         return 0;
91
92       out_platform_alloced:
93         platform_device_put(sony_pf_device);
94         sony_pf_device = NULL;
95       out_platform_registered:
96         platform_driver_unregister(&sony_pf_driver);
97       out:
98         atomic_dec(&sony_pf_users);
99         return ret;
100 }
101
102 static void sony_pf_remove(void)
103 {
104         /* deregister only after the last user has gone */
105         if (!atomic_dec_and_test(&sony_pf_users))
106                 return;
107
108         platform_device_del(sony_pf_device);
109         platform_device_put(sony_pf_device);
110         platform_driver_unregister(&sony_pf_driver);
111 }
112
113 /*********** SNC (SNY5001) Device ***********/
114
115 static ssize_t sony_nc_sysfs_show(struct device *, struct device_attribute *,
116                               char *);
117 static ssize_t sony_nc_sysfs_store(struct device *, struct device_attribute *,
118                                const char *, size_t);
119 static int boolean_validate(const int, const int);
120 static int brightness_default_validate(const int, const int);
121
122 #define SNC_VALIDATE_IN         0
123 #define SNC_VALIDATE_OUT        1
124
125 struct sony_nc_value {
126         char *name;             /* name of the entry */
127         char **acpiget;         /* names of the ACPI get function */
128         char **acpiset;         /* names of the ACPI set function */
129         int (*validate)(const int, const int);  /* input/output validation */
130         int value;              /* current setting */
131         int valid;              /* Has ever been set */
132         int debug;              /* active only in debug mode ? */
133         struct device_attribute devattr;        /* sysfs atribute */
134 };
135
136 #define SNC_HANDLE_NAMES(_name, _values...) \
137         static char *snc_##_name[] = { _values, NULL }
138
139 #define SNC_HANDLE(_name, _getters, _setters, _validate, _debug) \
140         { \
141                 .name           = __stringify(_name), \
142                 .acpiget        = _getters, \
143                 .acpiset        = _setters, \
144                 .validate       = _validate, \
145                 .debug          = _debug, \
146                 .devattr        = __ATTR(_name, 0, sony_nc_sysfs_show, sony_nc_sysfs_store), \
147         }
148
149 #define SNC_HANDLE_NULL { .name = NULL }
150
151 SNC_HANDLE_NAMES(fnkey_get, "GHKE");
152
153 SNC_HANDLE_NAMES(brightness_def_get, "GPBR");
154 SNC_HANDLE_NAMES(brightness_def_set, "SPBR");
155
156 SNC_HANDLE_NAMES(cdpower_get, "GCDP");
157 SNC_HANDLE_NAMES(cdpower_set, "SCDP", "CDPW");
158
159 SNC_HANDLE_NAMES(audiopower_get, "GAZP");
160 SNC_HANDLE_NAMES(audiopower_set, "AZPW");
161
162 SNC_HANDLE_NAMES(lanpower_get, "GLNP");
163 SNC_HANDLE_NAMES(lanpower_set, "LNPW");
164
165 SNC_HANDLE_NAMES(PID_get, "GPID");
166
167 SNC_HANDLE_NAMES(CTR_get, "GCTR");
168 SNC_HANDLE_NAMES(CTR_set, "SCTR");
169
170 SNC_HANDLE_NAMES(PCR_get, "GPCR");
171 SNC_HANDLE_NAMES(PCR_set, "SPCR");
172
173 SNC_HANDLE_NAMES(CMI_get, "GCMI");
174 SNC_HANDLE_NAMES(CMI_set, "SCMI");
175
176 static struct sony_nc_value sony_nc_values[] = {
177         SNC_HANDLE(brightness_default, snc_brightness_def_get,
178                         snc_brightness_def_set, brightness_default_validate, 0),
179         SNC_HANDLE(fnkey, snc_fnkey_get, NULL, NULL, 0),
180         SNC_HANDLE(cdpower, snc_cdpower_get, snc_cdpower_set, boolean_validate, 0),
181         SNC_HANDLE(audiopower, snc_audiopower_get, snc_audiopower_set,
182                         boolean_validate, 0),
183         SNC_HANDLE(lanpower, snc_lanpower_get, snc_lanpower_set,
184                         boolean_validate, 1),
185         /* unknown methods */
186         SNC_HANDLE(PID, snc_PID_get, NULL, NULL, 1),
187         SNC_HANDLE(CTR, snc_CTR_get, snc_CTR_set, NULL, 1),
188         SNC_HANDLE(PCR, snc_PCR_get, snc_PCR_set, NULL, 1),
189         SNC_HANDLE(CMI, snc_CMI_get, snc_CMI_set, NULL, 1),
190         SNC_HANDLE_NULL
191 };
192
193 static acpi_handle sony_nc_acpi_handle;
194 static struct acpi_device *sony_nc_acpi_device = NULL;
195
196 /*
197  * acpi_evaluate_object wrappers
198  */
199 static int acpi_callgetfunc(acpi_handle handle, char *name, int *result)
200 {
201         struct acpi_buffer output;
202         union acpi_object out_obj;
203         acpi_status status;
204
205         output.length = sizeof(out_obj);
206         output.pointer = &out_obj;
207
208         status = acpi_evaluate_object(handle, name, NULL, &output);
209         if ((status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER)) {
210                 *result = out_obj.integer.value;
211                 return 0;
212         }
213
214         printk(LOG_PFX "acpi_callreadfunc failed\n");
215
216         return -1;
217 }
218
219 static int acpi_callsetfunc(acpi_handle handle, char *name, int value,
220                             int *result)
221 {
222         struct acpi_object_list params;
223         union acpi_object in_obj;
224         struct acpi_buffer output;
225         union acpi_object out_obj;
226         acpi_status status;
227
228         params.count = 1;
229         params.pointer = &in_obj;
230         in_obj.type = ACPI_TYPE_INTEGER;
231         in_obj.integer.value = value;
232
233         output.length = sizeof(out_obj);
234         output.pointer = &out_obj;
235
236         status = acpi_evaluate_object(handle, name, &params, &output);
237         if (status == AE_OK) {
238                 if (result != NULL) {
239                         if (out_obj.type != ACPI_TYPE_INTEGER) {
240                                 printk(LOG_PFX "acpi_evaluate_object bad "
241                                        "return type\n");
242                                 return -1;
243                         }
244                         *result = out_obj.integer.value;
245                 }
246                 return 0;
247         }
248
249         printk(LOG_PFX "acpi_evaluate_object failed\n");
250
251         return -1;
252 }
253
254 /*
255  * sony_nc_values input/output validate functions
256  */
257
258 /* brightness_default_validate:
259  *
260  * manipulate input output values to keep consistency with the
261  * backlight framework for which brightness values are 0-based.
262  */
263 static int brightness_default_validate(const int direction, const int value)
264 {
265         switch (direction) {
266                 case SNC_VALIDATE_OUT:
267                         return value - 1;
268                 case SNC_VALIDATE_IN:
269                         if (value >= 0 && value < SONY_MAX_BRIGHTNESS)
270                                 return value + 1;
271         }
272         return -EINVAL;
273 }
274
275 /* boolean_validate:
276  *
277  * on input validate boolean values 0/1, on output just pass the
278  * received value.
279  */
280 static int boolean_validate(const int direction, const int value)
281 {
282         if (direction == SNC_VALIDATE_IN) {
283                 if (value != 0 && value != 1)
284                         return -EINVAL;
285         }
286         return value;
287 }
288
289 /*
290  * Sysfs show/store common to all sony_nc_values
291  */
292 static ssize_t sony_nc_sysfs_show(struct device *dev, struct device_attribute *attr,
293                               char *buffer)
294 {
295         int value;
296         struct sony_nc_value *item =
297             container_of(attr, struct sony_nc_value, devattr);
298
299         if (!*item->acpiget)
300                 return -EIO;
301
302         if (acpi_callgetfunc(sony_nc_acpi_handle, *item->acpiget, &value) < 0)
303                 return -EIO;
304
305         if (item->validate)
306                 value = item->validate(SNC_VALIDATE_OUT, value);
307
308         return snprintf(buffer, PAGE_SIZE, "%d\n", value);
309 }
310
311 static ssize_t sony_nc_sysfs_store(struct device *dev,
312                                struct device_attribute *attr,
313                                const char *buffer, size_t count)
314 {
315         int value;
316         struct sony_nc_value *item =
317             container_of(attr, struct sony_nc_value, devattr);
318
319         if (!item->acpiset)
320                 return -EIO;
321
322         if (count > 31)
323                 return -EINVAL;
324
325         value = simple_strtoul(buffer, NULL, 10);
326
327         if (item->validate)
328                 value = item->validate(SNC_VALIDATE_IN, value);
329
330         if (value < 0)
331                 return value;
332
333         if (acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset, value, NULL) < 0)
334                 return -EIO;
335         item->value = value;
336         item->valid = 1;
337         return count;
338 }
339
340
341 /*
342  * Backlight device
343  */
344 static int sony_backlight_update_status(struct backlight_device *bd)
345 {
346         return acpi_callsetfunc(sony_nc_acpi_handle, "SBRT",
347                                 bd->props.brightness + 1, NULL);
348 }
349
350 static int sony_backlight_get_brightness(struct backlight_device *bd)
351 {
352         int value;
353
354         if (acpi_callgetfunc(sony_nc_acpi_handle, "GBRT", &value))
355                 return 0;
356         /* brightness levels are 1-based, while backlight ones are 0-based */
357         return value - 1;
358 }
359
360 static struct backlight_device *sony_backlight_device;
361 static struct backlight_ops sony_backlight_ops = {
362         .update_status = sony_backlight_update_status,
363         .get_brightness = sony_backlight_get_brightness,
364 };
365
366 /*
367  * ACPI callbacks
368  */
369 static void sony_acpi_notify(acpi_handle handle, u32 event, void *data)
370 {
371         if (debug)
372                 printk(LOG_PFX "sony_acpi_notify, event: %d\n", event);
373         acpi_bus_generate_event(sony_nc_acpi_device, 1, event);
374 }
375
376 static acpi_status sony_walk_callback(acpi_handle handle, u32 level,
377                                       void *context, void **return_value)
378 {
379         struct acpi_namespace_node *node;
380         union acpi_operand_object *operand;
381
382         node = (struct acpi_namespace_node *)handle;
383         operand = (union acpi_operand_object *)node->object;
384
385         printk(LOG_PFX "method: name: %4.4s, args %X\n", node->name.ascii,
386                (u32) operand->method.param_count);
387
388         return AE_OK;
389 }
390
391 /*
392  * ACPI device
393  */
394 static int sony_nc_resume(struct acpi_device *device)
395 {
396         struct sony_nc_value *item;
397
398         for (item = sony_nc_values; item->name; item++) {
399                 int ret;
400
401                 if (!item->valid)
402                         continue;
403                 ret = acpi_callsetfunc(sony_nc_acpi_handle, *item->acpiset,
404                                        item->value, NULL);
405                 if (ret < 0) {
406                         printk("%s: %d\n", __FUNCTION__, ret);
407                         break;
408                 }
409         }
410         return 0;
411 }
412
413 static int sony_nc_add(struct acpi_device *device)
414 {
415         acpi_status status;
416         int result = 0;
417         acpi_handle handle;
418         struct sony_nc_value *item;
419
420         sony_nc_acpi_device = device;
421
422         sony_nc_acpi_handle = device->handle;
423
424         if (debug) {
425                 status = acpi_walk_namespace(ACPI_TYPE_METHOD, sony_nc_acpi_handle,
426                                              1, sony_walk_callback, NULL, NULL);
427                 if (ACPI_FAILURE(status)) {
428                         printk(LOG_PFX "unable to walk acpi resources\n");
429                         result = -ENODEV;
430                         goto outwalk;
431                 }
432         }
433
434         status = acpi_install_notify_handler(sony_nc_acpi_handle,
435                                              ACPI_DEVICE_NOTIFY,
436                                              sony_acpi_notify, NULL);
437         if (ACPI_FAILURE(status)) {
438                 printk(LOG_PFX "unable to install notify handler\n");
439                 result = -ENODEV;
440                 goto outwalk;
441         }
442
443         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle, "GBRT", &handle))) {
444                 sony_backlight_device = backlight_device_register("sony", NULL,
445                                                                   NULL,
446                                                                   &sony_backlight_ops);
447
448                 if (IS_ERR(sony_backlight_device)) {
449                         printk(LOG_PFX "unable to register backlight device\n");
450                         sony_backlight_device = NULL;
451                 } else {
452                         sony_backlight_device->props.brightness =
453                             sony_backlight_get_brightness
454                             (sony_backlight_device);
455                         sony_backlight_device->props.max_brightness = 
456                             SONY_MAX_BRIGHTNESS - 1;
457                 }
458
459         }
460
461         if (sony_pf_add())
462                 goto outbacklight;
463
464         /* create sony_pf sysfs attributes related to the SNC device */
465         for (item = sony_nc_values; item->name; ++item) {
466
467                 if (!debug && item->debug)
468                         continue;
469
470                 /* find the available acpiget as described in the DSDT */
471                 for (; item->acpiget && *item->acpiget; ++item->acpiget) {
472                         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
473                                                          *item->acpiget,
474                                                          &handle))) {
475                                 if (debug)
476                                         printk(LOG_PFX "Found %s getter: %s\n",
477                                                item->name, *item->acpiget);
478                                 item->devattr.attr.mode |= S_IRUGO;
479                                 break;
480                         }
481                 }
482
483                 /* find the available acpiset as described in the DSDT */
484                 for (; item->acpiset && *item->acpiset; ++item->acpiset) {
485                         if (ACPI_SUCCESS(acpi_get_handle(sony_nc_acpi_handle,
486                                                          *item->acpiset,
487                                                          &handle))) {
488                                 if (debug)
489                                         printk(LOG_PFX "Found %s setter: %s\n",
490                                                item->name, *item->acpiset);
491                                 item->devattr.attr.mode |= S_IWUSR;
492                                 break;
493                         }
494                 }
495
496                 if (item->devattr.attr.mode != 0) {
497                         result =
498                             device_create_file(&sony_pf_device->dev,
499                                                &item->devattr);
500                         if (result)
501                                 goto out_sysfs;
502                 }
503         }
504
505         printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully installed\n");
506
507         return 0;
508
509       out_sysfs:
510         for (item = sony_nc_values; item->name; ++item) {
511                 device_remove_file(&sony_pf_device->dev, &item->devattr);
512         }
513         sony_pf_remove();
514       outbacklight:
515         if (sony_backlight_device)
516                 backlight_device_unregister(sony_backlight_device);
517
518         status = acpi_remove_notify_handler(sony_nc_acpi_handle,
519                                             ACPI_DEVICE_NOTIFY,
520                                             sony_acpi_notify);
521         if (ACPI_FAILURE(status))
522                 printk(LOG_PFX "unable to remove notify handler\n");
523       outwalk:
524         return result;
525 }
526
527 static int sony_nc_remove(struct acpi_device *device, int type)
528 {
529         acpi_status status;
530         struct sony_nc_value *item;
531
532         if (sony_backlight_device)
533                 backlight_device_unregister(sony_backlight_device);
534
535         sony_nc_acpi_device = NULL;
536
537         status = acpi_remove_notify_handler(sony_nc_acpi_handle,
538                                             ACPI_DEVICE_NOTIFY,
539                                             sony_acpi_notify);
540         if (ACPI_FAILURE(status))
541                 printk(LOG_PFX "unable to remove notify handler\n");
542
543         for (item = sony_nc_values; item->name; ++item) {
544                 device_remove_file(&sony_pf_device->dev, &item->devattr);
545         }
546
547         sony_pf_remove();
548
549         printk(KERN_INFO SONY_NC_DRIVER_NAME " successfully removed\n");
550
551         return 0;
552 }
553
554 static struct acpi_driver sony_nc_driver = {
555         .name = SONY_NC_DRIVER_NAME,
556         .class = SONY_NC_CLASS,
557         .ids = SONY_NC_HID,
558         .ops = {
559                 .add = sony_nc_add,
560                 .remove = sony_nc_remove,
561                 .resume = sony_nc_resume,
562                 },
563 };
564
565 static int __init sony_laptop_init(void)
566 {
567         return acpi_bus_register_driver(&sony_nc_driver);
568 }
569
570 static void __exit sony_laptop_exit(void)
571 {
572         acpi_bus_unregister_driver(&sony_nc_driver);
573 }
574
575 module_init(sony_laptop_init);
576 module_exit(sony_laptop_exit);