Staging: comedi: s526: fixes for pulse generator
[safe/jmp/linux-2.6] / drivers / staging / hv / osd.c
1 /*
2  *
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * Authors:
19  *   Haiyang Zhang <haiyangz@microsoft.com>
20  *   Hank Janssen  <hjanssen@microsoft.com>
21  *
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/mm.h>
28 #include <linux/highmem.h>
29 #include <linux/vmalloc.h>
30 #include <linux/ioport.h>
31 #include <linux/irq.h>
32 #include <linux/interrupt.h>
33 #include <linux/wait.h>
34 #include <linux/spinlock.h>
35 #include <linux/workqueue.h>
36 #include <linux/kernel.h>
37 #include <linux/jiffies.h>
38 #include <linux/delay.h>
39 #include <linux/time.h>
40 #include <linux/io.h>
41 #include <linux/bitops.h>
42 #include "osd.h"
43
44 struct osd_callback_struct {
45         struct work_struct work;
46         void (*callback)(void *);
47         void *data;
48 };
49
50 void *osd_VirtualAllocExec(unsigned int size)
51 {
52 #ifdef __x86_64__
53         return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL_EXEC);
54 #else
55         return __vmalloc(size, GFP_KERNEL,
56                          __pgprot(__PAGE_KERNEL & (~_PAGE_NX)));
57 #endif
58 }
59
60 void *osd_PageAlloc(unsigned int count)
61 {
62         void *p;
63
64         p = (void *)__get_free_pages(GFP_KERNEL, get_order(count * PAGE_SIZE));
65         if (p)
66                 memset(p, 0, count * PAGE_SIZE);
67         return p;
68
69         /* struct page* page = alloc_page(GFP_KERNEL|__GFP_ZERO); */
70         /* void *p; */
71
72         /* BUGBUG: We need to use kmap in case we are in HIMEM region */
73         /* p = page_address(page); */
74         /* if (p) memset(p, 0, PAGE_SIZE); */
75         /* return p; */
76 }
77 EXPORT_SYMBOL_GPL(osd_PageAlloc);
78
79 void osd_PageFree(void *page, unsigned int count)
80 {
81         free_pages((unsigned long)page, get_order(count * PAGE_SIZE));
82         /*struct page* p = virt_to_page(page);
83         __free_page(p);*/
84 }
85 EXPORT_SYMBOL_GPL(osd_PageFree);
86
87 struct osd_waitevent *osd_WaitEventCreate(void)
88 {
89         struct osd_waitevent *wait = kmalloc(sizeof(struct osd_waitevent),
90                                              GFP_KERNEL);
91         if (!wait)
92                 return NULL;
93
94         wait->condition = 0;
95         init_waitqueue_head(&wait->event);
96         return wait;
97 }
98 EXPORT_SYMBOL_GPL(osd_WaitEventCreate);
99
100 void osd_WaitEventSet(struct osd_waitevent *waitEvent)
101 {
102         waitEvent->condition = 1;
103         wake_up_interruptible(&waitEvent->event);
104 }
105 EXPORT_SYMBOL_GPL(osd_WaitEventSet);
106
107 int osd_WaitEventWait(struct osd_waitevent *waitEvent)
108 {
109         int ret = 0;
110
111         ret = wait_event_interruptible(waitEvent->event,
112                                        waitEvent->condition);
113         waitEvent->condition = 0;
114         return ret;
115 }
116 EXPORT_SYMBOL_GPL(osd_WaitEventWait);
117
118 int osd_WaitEventWaitEx(struct osd_waitevent *waitEvent, u32 TimeoutInMs)
119 {
120         int ret = 0;
121
122         ret = wait_event_interruptible_timeout(waitEvent->event,
123                                                waitEvent->condition,
124                                                msecs_to_jiffies(TimeoutInMs));
125         waitEvent->condition = 0;
126         return ret;
127 }
128 EXPORT_SYMBOL_GPL(osd_WaitEventWaitEx);
129
130 static void osd_callback_work(struct work_struct *work)
131 {
132         struct osd_callback_struct *cb = container_of(work,
133                                                 struct osd_callback_struct,
134                                                 work);
135         (cb->callback)(cb->data);
136         kfree(cb);
137 }
138
139 int osd_schedule_callback(struct workqueue_struct *wq,
140                           void (*func)(void *),
141                           void *data)
142 {
143         struct osd_callback_struct *cb;
144
145         cb = kmalloc(sizeof(*cb), GFP_KERNEL);
146         if (!cb) {
147                 printk(KERN_ERR "unable to allocate memory in osd_schedule_callback\n");
148                 return -1;
149         }
150
151         cb->callback = func;
152         cb->data = data;
153         INIT_WORK(&cb->work, osd_callback_work);
154         return queue_work(wq, &cb->work);
155 }
156