2 * drivers/misc/goldfish_audio.c
4 * Copyright (C) 2007 Google, Inc.
5 * Copyright (C) 2012 Intel, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/miscdevice.h>
21 #include <linux/platform_device.h>
22 #include <linux/types.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
26 #include <linux/sched.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/uaccess.h>
29 #include <linux/goldfish.h>
31 MODULE_AUTHOR("Google, Inc.");
32 MODULE_DESCRIPTION("Android QEMU Audio Driver");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION("1.0");
36 struct goldfish_audio
{
37 char __iomem
*reg_base
;
40 wait_queue_head_t wait
;
42 char __iomem
*buffer_virt
; /* combined buffer virtual address */
43 unsigned long buffer_phys
; /* combined buffer physical address */
45 char __iomem
*write_buffer1
; /* write buffer 1 virtual address */
46 char __iomem
*write_buffer2
; /* write buffer 2 virtual address */
47 char __iomem
*read_buffer
; /* read buffer virtual address */
49 int read_supported
; /* true if we have audio input support */
53 * We will allocate two read buffers and two write buffers.
54 * Having two read buffers facilitate stereo -> mono conversion.
55 * Having two write buffers facilitate interleaved IO.
57 #define READ_BUFFER_SIZE 16384
58 #define WRITE_BUFFER_SIZE 16384
59 #define COMBINED_BUFFER_SIZE ((2 * READ_BUFFER_SIZE) + \
60 (2 * WRITE_BUFFER_SIZE))
62 #define AUDIO_READ(data, addr) (readl(data->reg_base + addr))
63 #define AUDIO_WRITE(data, addr, x) (writel(x, data->reg_base + addr))
64 #define AUDIO_WRITE64(data, addr, addr2, x) \
65 (gf_write64((u64)(x), data->reg_base + addr, data->reg_base+addr2))
68 * temporary variable used between goldfish_audio_probe() and
69 * goldfish_audio_open()
71 static struct goldfish_audio
*audio_data
;
74 /* audio status register */
75 AUDIO_INT_STATUS
= 0x00,
76 /* set this to enable IRQ */
77 AUDIO_INT_ENABLE
= 0x04,
78 /* set these to specify buffer addresses */
79 AUDIO_SET_WRITE_BUFFER_1
= 0x08,
80 AUDIO_SET_WRITE_BUFFER_2
= 0x0C,
81 /* set number of bytes in buffer to write */
82 AUDIO_WRITE_BUFFER_1
= 0x10,
83 AUDIO_WRITE_BUFFER_2
= 0x14,
84 AUDIO_SET_WRITE_BUFFER_1_HIGH
= 0x28,
85 AUDIO_SET_WRITE_BUFFER_2_HIGH
= 0x30,
87 /* true if audio input is supported */
88 AUDIO_READ_SUPPORTED
= 0x18,
89 /* buffer to use for audio input */
90 AUDIO_SET_READ_BUFFER
= 0x1C,
91 AUDIO_SET_READ_BUFFER_HIGH
= 0x34,
93 /* driver writes number of bytes to read */
94 AUDIO_START_READ
= 0x20,
96 /* number of bytes available in read buffer */
97 AUDIO_READ_BUFFER_AVAILABLE
= 0x24,
99 /* AUDIO_INT_STATUS bits */
101 /* this bit set when it is safe to write more bytes to the buffer */
102 AUDIO_INT_WRITE_BUFFER_1_EMPTY
= 1U << 0,
103 AUDIO_INT_WRITE_BUFFER_2_EMPTY
= 1U << 1,
104 AUDIO_INT_READ_BUFFER_FULL
= 1U << 2,
106 AUDIO_INT_MASK
= AUDIO_INT_WRITE_BUFFER_1_EMPTY
|
107 AUDIO_INT_WRITE_BUFFER_2_EMPTY
|
108 AUDIO_INT_READ_BUFFER_FULL
,
112 static atomic_t open_count
= ATOMIC_INIT(0);
115 static ssize_t
goldfish_audio_read(struct file
*fp
, char __user
*buf
,
116 size_t count
, loff_t
*pos
)
118 struct goldfish_audio
*data
= fp
->private_data
;
122 if (!data
->read_supported
)
126 length
= (count
> READ_BUFFER_SIZE
? READ_BUFFER_SIZE
: count
);
127 AUDIO_WRITE(data
, AUDIO_START_READ
, length
);
129 wait_event_interruptible(data
->wait
,
130 (data
->buffer_status
& AUDIO_INT_READ_BUFFER_FULL
));
132 length
= AUDIO_READ(data
,
133 AUDIO_READ_BUFFER_AVAILABLE
);
135 /* copy data to user space */
136 if (copy_to_user(buf
, data
->read_buffer
, length
))
146 static ssize_t
goldfish_audio_write(struct file
*fp
, const char __user
*buf
,
147 size_t count
, loff_t
*pos
)
149 struct goldfish_audio
*data
= fp
->private_data
;
150 unsigned long irq_flags
;
155 ssize_t copy
= count
;
157 if (copy
> WRITE_BUFFER_SIZE
)
158 copy
= WRITE_BUFFER_SIZE
;
159 wait_event_interruptible(data
->wait
, (data
->buffer_status
&
160 (AUDIO_INT_WRITE_BUFFER_1_EMPTY
|
161 AUDIO_INT_WRITE_BUFFER_2_EMPTY
)));
163 if ((data
->buffer_status
& AUDIO_INT_WRITE_BUFFER_1_EMPTY
) != 0)
164 kbuf
= data
->write_buffer1
;
166 kbuf
= data
->write_buffer2
;
168 /* copy from user space to the appropriate buffer */
169 if (copy_from_user(kbuf
, buf
, copy
)) {
174 spin_lock_irqsave(&data
->lock
, irq_flags
);
176 * clear the buffer empty flag, and signal the emulator
177 * to start writing the buffer
179 if (kbuf
== data
->write_buffer1
) {
180 data
->buffer_status
&= ~AUDIO_INT_WRITE_BUFFER_1_EMPTY
;
181 AUDIO_WRITE(data
, AUDIO_WRITE_BUFFER_1
, copy
);
183 data
->buffer_status
&= ~AUDIO_INT_WRITE_BUFFER_2_EMPTY
;
184 AUDIO_WRITE(data
, AUDIO_WRITE_BUFFER_2
, copy
);
186 spin_unlock_irqrestore(&data
->lock
, irq_flags
);
195 static int goldfish_audio_open(struct inode
*ip
, struct file
*fp
)
200 if (atomic_inc_return(&open_count
) == 1) {
201 fp
->private_data
= audio_data
;
202 audio_data
->buffer_status
= (AUDIO_INT_WRITE_BUFFER_1_EMPTY
|
203 AUDIO_INT_WRITE_BUFFER_2_EMPTY
);
204 AUDIO_WRITE(audio_data
, AUDIO_INT_ENABLE
, AUDIO_INT_MASK
);
207 atomic_dec(&open_count
);
212 static int goldfish_audio_release(struct inode
*ip
, struct file
*fp
)
214 atomic_dec(&open_count
);
215 /* FIXME: surely this is wrong for the multi-opened case */
216 AUDIO_WRITE(audio_data
, AUDIO_INT_ENABLE
, 0);
220 static long goldfish_audio_ioctl(struct file
*fp
, unsigned int cmd
,
223 /* temporary workaround, until we switch to the ALSA API */
230 static irqreturn_t
goldfish_audio_interrupt(int irq
, void *dev_id
)
232 unsigned long irq_flags
;
233 struct goldfish_audio
*data
= dev_id
;
236 spin_lock_irqsave(&data
->lock
, irq_flags
);
238 /* read buffer status flags */
239 status
= AUDIO_READ(data
, AUDIO_INT_STATUS
);
240 status
&= AUDIO_INT_MASK
;
242 * if buffers are newly empty, wake up blocked
243 * goldfish_audio_write() call
246 data
->buffer_status
= status
;
247 wake_up(&data
->wait
);
250 spin_unlock_irqrestore(&data
->lock
, irq_flags
);
251 return status
? IRQ_HANDLED
: IRQ_NONE
;
254 /* file operations for /dev/eac */
255 static const struct file_operations goldfish_audio_fops
= {
256 .owner
= THIS_MODULE
,
257 .read
= goldfish_audio_read
,
258 .write
= goldfish_audio_write
,
259 .open
= goldfish_audio_open
,
260 .release
= goldfish_audio_release
,
261 .unlocked_ioctl
= goldfish_audio_ioctl
,
264 static struct miscdevice goldfish_audio_device
= {
265 .minor
= MISC_DYNAMIC_MINOR
,
267 .fops
= &goldfish_audio_fops
,
270 static int goldfish_audio_probe(struct platform_device
*pdev
)
274 struct goldfish_audio
*data
;
277 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
280 goto err_data_alloc_failed
;
282 spin_lock_init(&data
->lock
);
283 init_waitqueue_head(&data
->wait
);
284 platform_set_drvdata(pdev
, data
);
286 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
288 dev_err(&pdev
->dev
, "platform_get_resource failed\n");
292 data
->reg_base
= ioremap(r
->start
, PAGE_SIZE
);
293 if (data
->reg_base
== NULL
) {
298 data
->irq
= platform_get_irq(pdev
, 0);
300 dev_err(&pdev
->dev
, "platform_get_irq failed\n");
304 data
->buffer_virt
= dma_alloc_coherent(&pdev
->dev
,
305 COMBINED_BUFFER_SIZE
, &buf_addr
, GFP_KERNEL
);
306 if (data
->buffer_virt
== 0) {
308 dev_err(&pdev
->dev
, "allocate buffer failed\n");
309 goto err_alloc_write_buffer_failed
;
311 data
->buffer_phys
= buf_addr
;
312 data
->write_buffer1
= data
->buffer_virt
;
313 data
->write_buffer2
= data
->buffer_virt
+ WRITE_BUFFER_SIZE
;
314 data
->read_buffer
= data
->buffer_virt
+ 2 * WRITE_BUFFER_SIZE
;
316 ret
= request_irq(data
->irq
, goldfish_audio_interrupt
,
317 IRQF_SHARED
, pdev
->name
, data
);
319 dev_err(&pdev
->dev
, "request_irq failed\n");
320 goto err_request_irq_failed
;
323 ret
= misc_register(&goldfish_audio_device
);
326 "misc_register returned %d in goldfish_audio_init\n",
328 goto err_misc_register_failed
;
331 AUDIO_WRITE64(data
, AUDIO_SET_WRITE_BUFFER_1
,
332 AUDIO_SET_WRITE_BUFFER_1_HIGH
, buf_addr
);
333 buf_addr
+= WRITE_BUFFER_SIZE
;
335 AUDIO_WRITE64(data
, AUDIO_SET_WRITE_BUFFER_2
,
336 AUDIO_SET_WRITE_BUFFER_2_HIGH
, buf_addr
);
338 buf_addr
+= WRITE_BUFFER_SIZE
;
340 data
->read_supported
= AUDIO_READ(data
, AUDIO_READ_SUPPORTED
);
341 if (data
->read_supported
)
342 AUDIO_WRITE64(data
, AUDIO_SET_READ_BUFFER
,
343 AUDIO_SET_READ_BUFFER_HIGH
, buf_addr
);
348 err_misc_register_failed
:
349 free_irq(data
->irq
, data
);
350 err_request_irq_failed
:
351 dma_free_coherent(&pdev
->dev
, COMBINED_BUFFER_SIZE
,
352 data
->buffer_virt
, data
->buffer_phys
);
353 err_alloc_write_buffer_failed
:
355 iounmap(data
->reg_base
);
358 err_data_alloc_failed
:
362 static int goldfish_audio_remove(struct platform_device
*pdev
)
364 struct goldfish_audio
*data
= platform_get_drvdata(pdev
);
366 misc_deregister(&goldfish_audio_device
);
367 free_irq(data
->irq
, data
);
368 dma_free_coherent(&pdev
->dev
, COMBINED_BUFFER_SIZE
,
369 data
->buffer_virt
, data
->buffer_phys
);
370 iounmap(data
->reg_base
);
376 static struct platform_driver goldfish_audio_driver
= {
377 .probe
= goldfish_audio_probe
,
378 .remove
= goldfish_audio_remove
,
380 .name
= "goldfish_audio"
384 module_platform_driver(goldfish_audio_driver
);