diff options
-rw-r--r-- | drivers/platform/Kconfig | 4 | ||||
-rw-r--r-- | drivers/platform/goldfish/Kconfig | 5 | ||||
-rw-r--r-- | drivers/platform/goldfish/Makefile | 1 | ||||
-rw-r--r-- | drivers/platform/goldfish/goldfish_pipe.c | 611 |
4 files changed, 621 insertions, 0 deletions
diff --git a/drivers/platform/Kconfig b/drivers/platform/Kconfig index 8390dca2b4e1..69616aeaa966 100644 --- a/drivers/platform/Kconfig +++ b/drivers/platform/Kconfig | |||
@@ -1,3 +1,7 @@ | |||
1 | if X86 | 1 | if X86 |
2 | source "drivers/platform/x86/Kconfig" | 2 | source "drivers/platform/x86/Kconfig" |
3 | endif | 3 | endif |
4 | if GOLDFISH | ||
5 | source "drivers/platform/goldfish/Kconfig" | ||
6 | endif | ||
7 | |||
diff --git a/drivers/platform/goldfish/Kconfig b/drivers/platform/goldfish/Kconfig new file mode 100644 index 000000000000..635ef25cc722 --- /dev/null +++ b/drivers/platform/goldfish/Kconfig | |||
@@ -0,0 +1,5 @@ | |||
1 | config GOLDFISH_PIPE | ||
2 | tristate "Goldfish virtual device for QEMU pipes" | ||
3 | ---help--- | ||
4 | This is a virtual device to drive the QEMU pipe interface used by | ||
5 | the Goldfish Android Virtual Device. | ||
diff --git a/drivers/platform/goldfish/Makefile b/drivers/platform/goldfish/Makefile index 6c591f6b2707..a0022395eee9 100644 --- a/drivers/platform/goldfish/Makefile +++ b/drivers/platform/goldfish/Makefile | |||
@@ -2,3 +2,4 @@ | |||
2 | # Makefile for Goldfish platform specific drivers | 2 | # Makefile for Goldfish platform specific drivers |
3 | # | 3 | # |
4 | obj-$(CONFIG_GOLDFISH) += pdev_bus.o | 4 | obj-$(CONFIG_GOLDFISH) += pdev_bus.o |
5 | obj-$(CONFIG_GOLDFISH_PIPE) += goldfish_pipe.o | ||
diff --git a/drivers/platform/goldfish/goldfish_pipe.c b/drivers/platform/goldfish/goldfish_pipe.c new file mode 100644 index 000000000000..58dae1f1fdd4 --- /dev/null +++ b/drivers/platform/goldfish/goldfish_pipe.c | |||
@@ -0,0 +1,611 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2011 Google, Inc. | ||
3 | * Copyright (C) 2012 Intel, Inc. | ||
4 | * Copyright (C) 2013 Intel, Inc. | ||
5 | * | ||
6 | * This software is licensed under the terms of the GNU General Public | ||
7 | * License version 2, as published by the Free Software Foundation, and | ||
8 | * may be copied, distributed, and modified under those terms. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | */ | ||
16 | |||
17 | /* This source file contains the implementation of a special device driver | ||
18 | * that intends to provide a *very* fast communication channel between the | ||
19 | * guest system and the QEMU emulator. | ||
20 | * | ||
21 | * Usage from the guest is simply the following (error handling simplified): | ||
22 | * | ||
23 | * int fd = open("/dev/qemu_pipe",O_RDWR); | ||
24 | * .... write() or read() through the pipe. | ||
25 | * | ||
26 | * This driver doesn't deal with the exact protocol used during the session. | ||
27 | * It is intended to be as simple as something like: | ||
28 | * | ||
29 | * // do this _just_ after opening the fd to connect to a specific | ||
30 | * // emulator service. | ||
31 | * const char* msg = "<pipename>"; | ||
32 | * if (write(fd, msg, strlen(msg)+1) < 0) { | ||
33 | * ... could not connect to <pipename> service | ||
34 | * close(fd); | ||
35 | * } | ||
36 | * | ||
37 | * // after this, simply read() and write() to communicate with the | ||
38 | * // service. Exact protocol details left as an exercise to the reader. | ||
39 | * | ||
40 | * This driver is very fast because it doesn't copy any data through | ||
41 | * intermediate buffers, since the emulator is capable of translating | ||
42 | * guest user addresses into host ones. | ||
43 | * | ||
44 | * Note that we must however ensure that each user page involved in the | ||
45 | * exchange is properly mapped during a transfer. | ||
46 | */ | ||
47 | |||
48 | #include <linux/module.h> | ||
49 | #include <linux/interrupt.h> | ||
50 | #include <linux/kernel.h> | ||
51 | #include <linux/spinlock.h> | ||
52 | #include <linux/miscdevice.h> | ||
53 | #include <linux/platform_device.h> | ||
54 | #include <linux/poll.h> | ||
55 | #include <linux/sched.h> | ||
56 | #include <linux/bitops.h> | ||
57 | #include <linux/slab.h> | ||
58 | #include <linux/io.h> | ||
59 | |||
60 | /* | ||
61 | * IMPORTANT: The following constants must match the ones used and defined | ||
62 | * in external/qemu/hw/goldfish_pipe.c in the Android source tree. | ||
63 | */ | ||
64 | |||
65 | /* pipe device registers */ | ||
66 | #define PIPE_REG_COMMAND 0x00 /* write: value = command */ | ||
67 | #define PIPE_REG_STATUS 0x04 /* read */ | ||
68 | #define PIPE_REG_CHANNEL 0x08 /* read/write: channel id */ | ||
69 | #define PIPE_REG_SIZE 0x0c /* read/write: buffer size */ | ||
70 | #define PIPE_REG_ADDRESS 0x10 /* write: physical address */ | ||
71 | #define PIPE_REG_WAKES 0x14 /* read: wake flags */ | ||
72 | #define PIPE_REG_PARAMS_ADDR_LOW 0x18 /* read/write: batch data address */ | ||
73 | #define PIPE_REG_PARAMS_ADDR_HIGH 0x1c /* read/write: batch data address */ | ||
74 | #define PIPE_REG_ACCESS_PARAMS 0x20 /* write: batch access */ | ||
75 | |||
76 | /* list of commands for PIPE_REG_COMMAND */ | ||
77 | #define CMD_OPEN 1 /* open new channel */ | ||
78 | #define CMD_CLOSE 2 /* close channel (from guest) */ | ||
79 | #define CMD_POLL 3 /* poll read/write status */ | ||
80 | |||
81 | /* List of bitflags returned in status of CMD_POLL command */ | ||
82 | #define PIPE_POLL_IN (1 << 0) | ||
83 | #define PIPE_POLL_OUT (1 << 1) | ||
84 | #define PIPE_POLL_HUP (1 << 2) | ||
85 | |||
86 | /* The following commands are related to write operations */ | ||
87 | #define CMD_WRITE_BUFFER 4 /* send a user buffer to the emulator */ | ||
88 | #define CMD_WAKE_ON_WRITE 5 /* tell the emulator to wake us when writing | ||
89 | is possible */ | ||
90 | |||
91 | /* The following commands are related to read operations, they must be | ||
92 | * listed in the same order than the corresponding write ones, since we | ||
93 | * will use (CMD_READ_BUFFER - CMD_WRITE_BUFFER) as a special offset | ||
94 | * in goldfish_pipe_read_write() below. | ||
95 | */ | ||
96 | #define CMD_READ_BUFFER 6 /* receive a user buffer from the emulator */ | ||
97 | #define CMD_WAKE_ON_READ 7 /* tell the emulator to wake us when reading | ||
98 | * is possible */ | ||
99 | |||
100 | /* Possible status values used to signal errors - see goldfish_pipe_error_convert */ | ||
101 | #define PIPE_ERROR_INVAL -1 | ||
102 | #define PIPE_ERROR_AGAIN -2 | ||
103 | #define PIPE_ERROR_NOMEM -3 | ||
104 | #define PIPE_ERROR_IO -4 | ||
105 | |||
106 | /* Bit-flags used to signal events from the emulator */ | ||
107 | #define PIPE_WAKE_CLOSED (1 << 0) /* emulator closed pipe */ | ||
108 | #define PIPE_WAKE_READ (1 << 1) /* pipe can now be read from */ | ||
109 | #define PIPE_WAKE_WRITE (1 << 2) /* pipe can now be written to */ | ||
110 | |||
111 | struct access_params { | ||
112 | u32 channel; | ||
113 | u32 size; | ||
114 | u32 address; | ||
115 | u32 cmd; | ||
116 | u32 result; | ||
117 | /* reserved for future extension */ | ||
118 | u32 flags; | ||
119 | }; | ||
120 | |||
121 | /* The global driver data. Holds a reference to the i/o page used to | ||
122 | * communicate with the emulator, and a wake queue for blocked tasks | ||
123 | * waiting to be awoken. | ||
124 | */ | ||
125 | struct goldfish_pipe_dev { | ||
126 | spinlock_t lock; | ||
127 | unsigned char __iomem *base; | ||
128 | struct access_params *aps; | ||
129 | int irq; | ||
130 | }; | ||
131 | |||
132 | static struct goldfish_pipe_dev pipe_dev[1]; | ||
133 | |||
134 | /* This data type models a given pipe instance */ | ||
135 | struct goldfish_pipe { | ||
136 | struct goldfish_pipe_dev *dev; | ||
137 | struct mutex lock; | ||
138 | unsigned long flags; | ||
139 | wait_queue_head_t wake_queue; | ||
140 | }; | ||
141 | |||
142 | |||
143 | /* Bit flags for the 'flags' field */ | ||
144 | enum { | ||
145 | BIT_CLOSED_ON_HOST = 0, /* pipe closed by host */ | ||
146 | BIT_WAKE_ON_WRITE = 1, /* want to be woken on writes */ | ||
147 | BIT_WAKE_ON_READ = 2, /* want to be woken on reads */ | ||
148 | }; | ||
149 | |||
150 | |||
151 | static u32 goldfish_cmd_status(struct goldfish_pipe *pipe, u32 cmd) | ||
152 | { | ||
153 | unsigned long flags; | ||
154 | u32 status; | ||
155 | struct goldfish_pipe_dev *dev = pipe->dev; | ||
156 | |||
157 | spin_lock_irqsave(&dev->lock, flags); | ||
158 | writel((u32)pipe, dev->base + PIPE_REG_CHANNEL); | ||
159 | writel(cmd, dev->base + PIPE_REG_COMMAND); | ||
160 | status = readl(dev->base + PIPE_REG_STATUS); | ||
161 | spin_unlock_irqrestore(&dev->lock, flags); | ||
162 | return status; | ||
163 | } | ||
164 | |||
165 | static void goldfish_cmd(struct goldfish_pipe *pipe, u32 cmd) | ||
166 | { | ||
167 | unsigned long flags; | ||
168 | struct goldfish_pipe_dev *dev = pipe->dev; | ||
169 | |||
170 | spin_lock_irqsave(&dev->lock, flags); | ||
171 | writel((u32)pipe, dev->base + PIPE_REG_CHANNEL); | ||
172 | writel(cmd, dev->base + PIPE_REG_COMMAND); | ||
173 | spin_unlock_irqrestore(&dev->lock, flags); | ||
174 | } | ||
175 | |||
176 | /* This function converts an error code returned by the emulator through | ||
177 | * the PIPE_REG_STATUS i/o register into a valid negative errno value. | ||
178 | */ | ||
179 | static int goldfish_pipe_error_convert(int status) | ||
180 | { | ||
181 | switch (status) { | ||
182 | case PIPE_ERROR_AGAIN: | ||
183 | return -EAGAIN; | ||
184 | case PIPE_ERROR_NOMEM: | ||
185 | return -ENOMEM; | ||
186 | case PIPE_ERROR_IO: | ||
187 | return -EIO; | ||
188 | default: | ||
189 | return -EINVAL; | ||
190 | } | ||
191 | } | ||
192 | |||
193 | /* | ||
194 | * Notice: QEMU will return 0 for un-known register access, indicating | ||
195 | * param_acess is supported or not | ||
196 | */ | ||
197 | static int valid_batchbuffer_addr(struct goldfish_pipe_dev *dev, | ||
198 | struct access_params *aps) | ||
199 | { | ||
200 | u32 aph, apl; | ||
201 | u64 paddr; | ||
202 | aph = readl(dev->base + PIPE_REG_PARAMS_ADDR_HIGH); | ||
203 | apl = readl(dev->base + PIPE_REG_PARAMS_ADDR_LOW); | ||
204 | |||
205 | paddr = ((u64)aph << 32) | apl; | ||
206 | if (paddr != (__pa(aps))) | ||
207 | return 0; | ||
208 | return 1; | ||
209 | } | ||
210 | |||
211 | /* 0 on success */ | ||
212 | static int setup_access_params_addr(struct platform_device *pdev, | ||
213 | struct goldfish_pipe_dev *dev) | ||
214 | { | ||
215 | u64 paddr; | ||
216 | struct access_params *aps; | ||
217 | |||
218 | aps = devm_kzalloc(&pdev->dev, sizeof(struct access_params), GFP_KERNEL); | ||
219 | if (!aps) | ||
220 | return -1; | ||
221 | |||
222 | /* FIXME */ | ||
223 | paddr = __pa(aps); | ||
224 | writel((u32)(paddr >> 32), dev->base + PIPE_REG_PARAMS_ADDR_HIGH); | ||
225 | writel((u32)paddr, dev->base + PIPE_REG_PARAMS_ADDR_LOW); | ||
226 | |||
227 | if (valid_batchbuffer_addr(dev, aps)) { | ||
228 | dev->aps = aps; | ||
229 | return 0; | ||
230 | } else | ||
231 | return -1; | ||
232 | } | ||
233 | |||
234 | /* A value that will not be set by qemu emulator */ | ||
235 | #define INITIAL_BATCH_RESULT (0xdeadbeaf) | ||
236 | static int access_with_param(struct goldfish_pipe_dev *dev, const int cmd, | ||
237 | unsigned long address, unsigned long avail, | ||
238 | struct goldfish_pipe *pipe, int *status) | ||
239 | { | ||
240 | struct access_params *aps = dev->aps; | ||
241 | |||
242 | if (aps == NULL) | ||
243 | return -1; | ||
244 | |||
245 | aps->result = INITIAL_BATCH_RESULT; | ||
246 | aps->channel = (unsigned long)pipe; | ||
247 | aps->size = avail; | ||
248 | aps->address = address; | ||
249 | aps->cmd = cmd; | ||
250 | writel(cmd, dev->base + PIPE_REG_ACCESS_PARAMS); | ||
251 | /* | ||
252 | * If the aps->result has not changed, that means | ||
253 | * that the batch command failed | ||
254 | */ | ||
255 | if (aps->result == INITIAL_BATCH_RESULT) | ||
256 | return -1; | ||
257 | *status = aps->result; | ||
258 | return 0; | ||
259 | } | ||
260 | |||
261 | /* This function is used for both reading from and writing to a given | ||
262 | * pipe. | ||
263 | */ | ||
264 | static ssize_t goldfish_pipe_read_write(struct file *filp, char __user *buffer, | ||
265 | size_t bufflen, int is_write) | ||
266 | { | ||
267 | unsigned long irq_flags; | ||
268 | struct goldfish_pipe *pipe = filp->private_data; | ||
269 | struct goldfish_pipe_dev *dev = pipe->dev; | ||
270 | const int cmd_offset = is_write ? 0 | ||
271 | : (CMD_READ_BUFFER - CMD_WRITE_BUFFER); | ||
272 | unsigned long address, address_end; | ||
273 | int ret = 0; | ||
274 | |||
275 | /* If the emulator already closed the pipe, no need to go further */ | ||
276 | if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags)) | ||
277 | return -EIO; | ||
278 | |||
279 | /* Null reads or writes succeeds */ | ||
280 | if (unlikely(bufflen) == 0) | ||
281 | return 0; | ||
282 | |||
283 | /* Check the buffer range for access */ | ||
284 | if (!access_ok(is_write ? VERIFY_WRITE : VERIFY_READ, | ||
285 | buffer, bufflen)) | ||
286 | return -EFAULT; | ||
287 | |||
288 | /* Serialize access to the pipe */ | ||
289 | if (mutex_lock_interruptible(&pipe->lock)) | ||
290 | return -ERESTARTSYS; | ||
291 | |||
292 | address = (unsigned long)(void *)buffer; | ||
293 | address_end = address + bufflen; | ||
294 | |||
295 | while (address < address_end) { | ||
296 | unsigned long page_end = (address & PAGE_MASK) + PAGE_SIZE; | ||
297 | unsigned long next = page_end < address_end ? page_end | ||
298 | : address_end; | ||
299 | unsigned long avail = next - address; | ||
300 | int status, wakeBit; | ||
301 | |||
302 | /* Ensure that the corresponding page is properly mapped */ | ||
303 | if (is_write) { | ||
304 | char c; | ||
305 | /* Ensure that the page is mapped and readable */ | ||
306 | if (__get_user(c, (char __user *)address)) { | ||
307 | if (!ret) | ||
308 | ret = -EFAULT; | ||
309 | break; | ||
310 | } | ||
311 | } else { | ||
312 | /* Ensure that the page is mapped and writable */ | ||
313 | if (__put_user(0, (char __user *)address)) { | ||
314 | if (!ret) | ||
315 | ret = -EFAULT; | ||
316 | break; | ||
317 | } | ||
318 | } | ||
319 | |||
320 | /* Now, try to transfer the bytes in the current page */ | ||
321 | spin_lock_irqsave(&dev->lock, irq_flags); | ||
322 | if (access_with_param(dev, CMD_WRITE_BUFFER + cmd_offset, | ||
323 | address, avail, pipe, &status)) { | ||
324 | writel((u32)pipe, dev->base + PIPE_REG_CHANNEL); | ||
325 | writel(avail, dev->base + PIPE_REG_SIZE); | ||
326 | writel(address, dev->base + PIPE_REG_ADDRESS); | ||
327 | writel(CMD_WRITE_BUFFER + cmd_offset, | ||
328 | dev->base + PIPE_REG_COMMAND); | ||
329 | status = readl(dev->base + PIPE_REG_STATUS); | ||
330 | } | ||
331 | spin_unlock_irqrestore(&dev->lock, irq_flags); | ||
332 | |||
333 | if (status > 0) { /* Correct transfer */ | ||
334 | ret += status; | ||
335 | address += status; | ||
336 | continue; | ||
337 | } | ||
338 | |||
339 | if (status == 0) /* EOF */ | ||
340 | break; | ||
341 | |||
342 | /* An error occured. If we already transfered stuff, just | ||
343 | * return with its count. We expect the next call to return | ||
344 | * an error code */ | ||
345 | if (ret > 0) | ||
346 | break; | ||
347 | |||
348 | /* If the error is not PIPE_ERROR_AGAIN, or if we are not in | ||
349 | * non-blocking mode, just return the error code. | ||
350 | */ | ||
351 | if (status != PIPE_ERROR_AGAIN || | ||
352 | (filp->f_flags & O_NONBLOCK) != 0) { | ||
353 | ret = goldfish_pipe_error_convert(status); | ||
354 | break; | ||
355 | } | ||
356 | |||
357 | /* We will have to wait until more data/space is available. | ||
358 | * First, mark the pipe as waiting for a specific wake signal. | ||
359 | */ | ||
360 | wakeBit = is_write ? BIT_WAKE_ON_WRITE : BIT_WAKE_ON_READ; | ||
361 | set_bit(wakeBit, &pipe->flags); | ||
362 | |||
363 | /* Tell the emulator we're going to wait for a wake event */ | ||
364 | goldfish_cmd(pipe, CMD_WAKE_ON_WRITE + cmd_offset); | ||
365 | |||
366 | /* Unlock the pipe, then wait for the wake signal */ | ||
367 | mutex_unlock(&pipe->lock); | ||
368 | |||
369 | while (test_bit(wakeBit, &pipe->flags)) { | ||
370 | if (wait_event_interruptible( | ||
371 | pipe->wake_queue, | ||
372 | !test_bit(wakeBit, &pipe->flags))) | ||
373 | return -ERESTARTSYS; | ||
374 | |||
375 | if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags)) | ||
376 | return -EIO; | ||
377 | } | ||
378 | |||
379 | /* Try to re-acquire the lock */ | ||
380 | if (mutex_lock_interruptible(&pipe->lock)) | ||
381 | return -ERESTARTSYS; | ||
382 | |||
383 | /* Try the transfer again */ | ||
384 | continue; | ||
385 | } | ||
386 | mutex_unlock(&pipe->lock); | ||
387 | return ret; | ||
388 | } | ||
389 | |||
390 | static ssize_t goldfish_pipe_read(struct file *filp, char __user *buffer, | ||
391 | size_t bufflen, loff_t *ppos) | ||
392 | { | ||
393 | return goldfish_pipe_read_write(filp, buffer, bufflen, 0); | ||
394 | } | ||
395 | |||
396 | static ssize_t goldfish_pipe_write(struct file *filp, | ||
397 | const char __user *buffer, size_t bufflen, | ||
398 | loff_t *ppos) | ||
399 | { | ||
400 | return goldfish_pipe_read_write(filp, (char __user *)buffer, | ||
401 | bufflen, 1); | ||
402 | } | ||
403 | |||
404 | |||
405 | static unsigned int goldfish_pipe_poll(struct file *filp, poll_table *wait) | ||
406 | { | ||
407 | struct goldfish_pipe *pipe = filp->private_data; | ||
408 | unsigned int mask = 0; | ||
409 | int status; | ||
410 | |||
411 | mutex_lock(&pipe->lock); | ||
412 | |||
413 | poll_wait(filp, &pipe->wake_queue, wait); | ||
414 | |||
415 | status = goldfish_cmd_status(pipe, CMD_POLL); | ||
416 | |||
417 | mutex_unlock(&pipe->lock); | ||
418 | |||
419 | if (status & PIPE_POLL_IN) | ||
420 | mask |= POLLIN | POLLRDNORM; | ||
421 | |||
422 | if (status & PIPE_POLL_OUT) | ||
423 | mask |= POLLOUT | POLLWRNORM; | ||
424 | |||
425 | if (status & PIPE_POLL_HUP) | ||
426 | mask |= POLLHUP; | ||
427 | |||
428 | if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags)) | ||
429 | mask |= POLLERR; | ||
430 | |||
431 | return mask; | ||
432 | } | ||
433 | |||
434 | static irqreturn_t goldfish_pipe_interrupt(int irq, void *dev_id) | ||
435 | { | ||
436 | struct goldfish_pipe_dev *dev = dev_id; | ||
437 | unsigned long irq_flags; | ||
438 | int count = 0; | ||
439 | |||
440 | /* We're going to read from the emulator a list of (channel,flags) | ||
441 | * pairs corresponding to the wake events that occured on each | ||
442 | * blocked pipe (i.e. channel). | ||
443 | */ | ||
444 | spin_lock_irqsave(&dev->lock, irq_flags); | ||
445 | for (;;) { | ||
446 | /* First read the channel, 0 means the end of the list */ | ||
447 | struct goldfish_pipe *pipe; | ||
448 | unsigned long wakes; | ||
449 | unsigned long channel = readl(dev->base + PIPE_REG_CHANNEL); | ||
450 | |||
451 | if (channel == 0) | ||
452 | break; | ||
453 | |||
454 | /* Convert channel to struct pipe pointer + read wake flags */ | ||
455 | wakes = readl(dev->base + PIPE_REG_WAKES); | ||
456 | pipe = (struct goldfish_pipe *)(ptrdiff_t)channel; | ||
457 | |||
458 | /* Did the emulator just closed a pipe? */ | ||
459 | if (wakes & PIPE_WAKE_CLOSED) { | ||
460 | set_bit(BIT_CLOSED_ON_HOST, &pipe->flags); | ||
461 | wakes |= PIPE_WAKE_READ | PIPE_WAKE_WRITE; | ||
462 | } | ||
463 | if (wakes & PIPE_WAKE_READ) | ||
464 | clear_bit(BIT_WAKE_ON_READ, &pipe->flags); | ||
465 | if (wakes & PIPE_WAKE_WRITE) | ||
466 | clear_bit(BIT_WAKE_ON_WRITE, &pipe->flags); | ||
467 | |||
468 | wake_up_interruptible(&pipe->wake_queue); | ||
469 | count++; | ||
470 | } | ||
471 | spin_unlock_irqrestore(&dev->lock, irq_flags); | ||
472 | |||
473 | return (count == 0) ? IRQ_NONE : IRQ_HANDLED; | ||
474 | } | ||
475 | |||
476 | /** | ||
477 | * goldfish_pipe_open - open a channel to the AVD | ||
478 | * @inode: inode of device | ||
479 | * @file: file struct of opener | ||
480 | * | ||
481 | * Create a new pipe link between the emulator and the use application. | ||
482 | * Each new request produces a new pipe. | ||
483 | * | ||
484 | * Note: we use the pipe ID as a mux. All goldfish emulations are 32bit | ||
485 | * right now so this is fine. A move to 64bit will need this addressing | ||
486 | */ | ||
487 | static int goldfish_pipe_open(struct inode *inode, struct file *file) | ||
488 | { | ||
489 | struct goldfish_pipe *pipe; | ||
490 | struct goldfish_pipe_dev *dev = pipe_dev; | ||
491 | int32_t status; | ||
492 | |||
493 | /* Allocate new pipe kernel object */ | ||
494 | pipe = kzalloc(sizeof(*pipe), GFP_KERNEL); | ||
495 | if (pipe == NULL) | ||
496 | return -ENOMEM; | ||
497 | |||
498 | pipe->dev = dev; | ||
499 | mutex_init(&pipe->lock); | ||
500 | init_waitqueue_head(&pipe->wake_queue); | ||
501 | |||
502 | /* | ||
503 | * Now, tell the emulator we're opening a new pipe. We use the | ||
504 | * pipe object's address as the channel identifier for simplicity. | ||
505 | */ | ||
506 | |||
507 | status = goldfish_cmd_status(pipe, CMD_OPEN); | ||
508 | if (status < 0) { | ||
509 | kfree(pipe); | ||
510 | return status; | ||
511 | } | ||
512 | |||
513 | /* All is done, save the pipe into the file's private data field */ | ||
514 | file->private_data = pipe; | ||
515 | return 0; | ||
516 | } | ||
517 | |||
518 | static int goldfish_pipe_release(struct inode *inode, struct file *filp) | ||
519 | { | ||
520 | struct goldfish_pipe *pipe = filp->private_data; | ||
521 | |||
522 | /* The guest is closing the channel, so tell the emulator right now */ | ||
523 | goldfish_cmd(pipe, CMD_CLOSE); | ||
524 | kfree(pipe); | ||
525 | filp->private_data = NULL; | ||
526 | return 0; | ||
527 | } | ||
528 | |||
529 | static const struct file_operations goldfish_pipe_fops = { | ||
530 | .owner = THIS_MODULE, | ||
531 | .read = goldfish_pipe_read, | ||
532 | .write = goldfish_pipe_write, | ||
533 | .poll = goldfish_pipe_poll, | ||
534 | .open = goldfish_pipe_open, | ||
535 | .release = goldfish_pipe_release, | ||
536 | }; | ||
537 | |||
538 | static struct miscdevice goldfish_pipe_device = { | ||
539 | .minor = MISC_DYNAMIC_MINOR, | ||
540 | .name = "goldfish_pipe", | ||
541 | .fops = &goldfish_pipe_fops, | ||
542 | }; | ||
543 | |||
544 | static int goldfish_pipe_probe(struct platform_device *pdev) | ||
545 | { | ||
546 | int err; | ||
547 | struct resource *r; | ||
548 | struct goldfish_pipe_dev *dev = pipe_dev; | ||
549 | |||
550 | /* not thread safe, but this should not happen */ | ||
551 | WARN_ON(dev->base != NULL); | ||
552 | |||
553 | spin_lock_init(&dev->lock); | ||
554 | |||
555 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | ||
556 | if (r == NULL || resource_size(r) < PAGE_SIZE) { | ||
557 | dev_err(&pdev->dev, "can't allocate i/o page\n"); | ||
558 | return -EINVAL; | ||
559 | } | ||
560 | dev->base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE); | ||
561 | if (dev->base == NULL) { | ||
562 | dev_err(&pdev->dev, "ioremap failed\n"); | ||
563 | return -EINVAL; | ||
564 | } | ||
565 | |||
566 | r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | ||
567 | if (r == NULL) { | ||
568 | err = -EINVAL; | ||
569 | goto error; | ||
570 | } | ||
571 | dev->irq = r->start; | ||
572 | |||
573 | err = devm_request_irq(&pdev->dev, dev->irq, goldfish_pipe_interrupt, | ||
574 | IRQF_SHARED, "goldfish_pipe", dev); | ||
575 | if (err) { | ||
576 | dev_err(&pdev->dev, "unable to allocate IRQ\n"); | ||
577 | goto error; | ||
578 | } | ||
579 | |||
580 | err = misc_register(&goldfish_pipe_device); | ||
581 | if (err) { | ||
582 | dev_err(&pdev->dev, "unable to register device\n"); | ||
583 | goto error; | ||
584 | } | ||
585 | setup_access_params_addr(pdev, dev); | ||
586 | return 0; | ||
587 | |||
588 | error: | ||
589 | dev->base = NULL; | ||
590 | return err; | ||
591 | } | ||
592 | |||
593 | static int goldfish_pipe_remove(struct platform_device *pdev) | ||
594 | { | ||
595 | struct goldfish_pipe_dev *dev = pipe_dev; | ||
596 | misc_deregister(&goldfish_pipe_device); | ||
597 | dev->base = NULL; | ||
598 | return 0; | ||
599 | } | ||
600 | |||
601 | static struct platform_driver goldfish_pipe = { | ||
602 | .probe = goldfish_pipe_probe, | ||
603 | .remove = goldfish_pipe_remove, | ||
604 | .driver = { | ||
605 | .name = "goldfish_pipe" | ||
606 | } | ||
607 | }; | ||
608 | |||
609 | module_platform_driver(goldfish_pipe); | ||
610 | MODULE_AUTHOR("David Turner <digit@google.com>"); | ||
611 | MODULE_LICENSE("GPL"); | ||