]> err.no Git - linux-2.6/blob - drivers/misc/hdpuftrs/hdpu_cpustate.c
Merge /pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / misc / hdpuftrs / hdpu_cpustate.c
1 /*
2  *      Sky CPU State Driver
3  *
4  *      Copyright (C) 2002 Brian Waite
5  *
6  *      This driver allows use of the CPU state bits
7  *      It exports the /dev/sky_cpustate and also
8  *      /proc/sky_cpustate pseudo-file for status information.
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  *
15  */
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/spinlock.h>
20 #include <linux/miscdevice.h>
21 #include <linux/proc_fs.h>
22 #include <linux/platform_device.h>
23 #include <asm/uaccess.h>
24 #include <linux/hdpu_features.h>
25
26 #define SKY_CPUSTATE_VERSION            "1.1"
27
28 static int hdpu_cpustate_probe(struct platform_device *pdev);
29 static int hdpu_cpustate_remove(struct platform_device *pdev);
30
31 struct cpustate_t cpustate;
32
33 static int cpustate_get_ref(int excl)
34 {
35
36         int retval = -EBUSY;
37
38         spin_lock(&cpustate.lock);
39
40         if (cpustate.excl)
41                 goto out_busy;
42
43         if (excl) {
44                 if (cpustate.open_count)
45                         goto out_busy;
46                 cpustate.excl = 1;
47         }
48
49         cpustate.open_count++;
50         retval = 0;
51
52       out_busy:
53         spin_unlock(&cpustate.lock);
54         return retval;
55 }
56
57 static int cpustate_free_ref(void)
58 {
59
60         spin_lock(&cpustate.lock);
61
62         cpustate.excl = 0;
63         cpustate.open_count--;
64
65         spin_unlock(&cpustate.lock);
66         return 0;
67 }
68
69 unsigned char cpustate_get_state(void)
70 {
71
72         return cpustate.cached_val;
73 }
74
75 void cpustate_set_state(unsigned char new_state)
76 {
77         unsigned int state = (new_state << 21);
78
79 #ifdef DEBUG_CPUSTATE
80         printk("CPUSTATE -> 0x%x\n", new_state);
81 #endif
82         spin_lock(&cpustate.lock);
83         cpustate.cached_val = new_state;
84         writel((0xff << 21), cpustate.clr_addr);
85         writel(state, cpustate.set_addr);
86         spin_unlock(&cpustate.lock);
87 }
88
89 /*
90  *      Now all the various file operations that we export.
91  */
92
93 static ssize_t cpustate_read(struct file *file, char *buf,
94                              size_t count, loff_t * ppos)
95 {
96         unsigned char data;
97
98         if (count < 0)
99                 return -EFAULT;
100         if (count == 0)
101                 return 0;
102
103         data = cpustate_get_state();
104         if (copy_to_user(buf, &data, sizeof(unsigned char)))
105                 return -EFAULT;
106         return sizeof(unsigned char);
107 }
108
109 static ssize_t cpustate_write(struct file *file, const char *buf,
110                               size_t count, loff_t * ppos)
111 {
112         unsigned char data;
113
114         if (count < 0)
115                 return -EFAULT;
116
117         if (count == 0)
118                 return 0;
119
120         if (copy_from_user((unsigned char *)&data, buf, sizeof(unsigned char)))
121                 return -EFAULT;
122
123         cpustate_set_state(data);
124         return sizeof(unsigned char);
125 }
126
127 static int cpustate_open(struct inode *inode, struct file *file)
128 {
129         return cpustate_get_ref((file->f_flags & O_EXCL));
130 }
131
132 static int cpustate_release(struct inode *inode, struct file *file)
133 {
134         return cpustate_free_ref();
135 }
136
137 /*
138  *      Info exported via "/proc/sky_cpustate".
139  */
140 static int cpustate_read_proc(char *page, char **start, off_t off,
141                               int count, int *eof, void *data)
142 {
143         char *p = page;
144         int len = 0;
145
146         p += sprintf(p, "CPU State: %04x\n", cpustate_get_state());
147         len = p - page;
148
149         if (len <= off + count)
150                 *eof = 1;
151         *start = page + off;
152         len -= off;
153         if (len > count)
154                 len = count;
155         if (len < 0)
156                 len = 0;
157         return len;
158 }
159
160 static struct platform_driver hdpu_cpustate_driver = {
161         .probe = hdpu_cpustate_probe,
162         .remove = hdpu_cpustate_remove,
163         .driver = {
164                 .name = HDPU_CPUSTATE_NAME,
165         },
166 };
167
168 /*
169  *      The various file operations we support.
170  */
171 static const struct file_operations cpustate_fops = {
172       owner:THIS_MODULE,
173       open:cpustate_open,
174       release:cpustate_release,
175       read:cpustate_read,
176       write:cpustate_write,
177       fasync:NULL,
178       poll:NULL,
179       ioctl:NULL,
180       llseek:no_llseek,
181
182 };
183
184 static struct miscdevice cpustate_dev = {
185         MISC_DYNAMIC_MINOR,
186         "sky_cpustate",
187         &cpustate_fops
188 };
189
190 static int hdpu_cpustate_probe(struct platform_device *pdev)
191 {
192         struct resource *res;
193         struct proc_dir_entry *proc_de;
194         int ret;
195
196         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197         cpustate.set_addr = (unsigned long *)res->start;
198         cpustate.clr_addr = (unsigned long *)res->end - 1;
199
200         ret = misc_register(&cpustate_dev);
201         if (ret) {
202                 printk(KERN_WARNING "sky_cpustate: Unable to register misc "
203                                         "device.\n");
204                 cpustate.set_addr = NULL;
205                 cpustate.clr_addr = NULL;
206                 return ret;
207         }
208
209         proc_de = create_proc_read_entry("sky_cpustate", 0, 0,
210                                         cpustate_read_proc, NULL);
211         if (proc_de == NULL)
212                 printk(KERN_WARNING "sky_cpustate: Unable to create proc "
213                                         "dir entry\n");
214
215         printk(KERN_INFO "Sky CPU State Driver v" SKY_CPUSTATE_VERSION "\n");
216         return 0;
217 }
218
219 static int hdpu_cpustate_remove(struct platform_device *pdev)
220 {
221
222         cpustate.set_addr = NULL;
223         cpustate.clr_addr = NULL;
224
225         remove_proc_entry("sky_cpustate", NULL);
226         misc_deregister(&cpustate_dev);
227         return 0;
228
229 }
230
231 static int __init cpustate_init(void)
232 {
233         int rc;
234         rc = platform_driver_register(&hdpu_cpustate_driver);
235         return rc;
236 }
237
238 static void __exit cpustate_exit(void)
239 {
240         platform_driver_unregister(&hdpu_cpustate_driver);
241 }
242
243 module_init(cpustate_init);
244 module_exit(cpustate_exit);
245
246 MODULE_AUTHOR("Brian Waite");
247 MODULE_LICENSE("GPL");