Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/drivers/staging/goldfish/goldfish_audio.c

  1 /*
  2  * drivers/misc/goldfish_audio.c
  3  *
  4  * Copyright (C) 2007 Google, Inc.
  5  * Copyright (C) 2012 Intel, Inc.
  6  *
  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.
 10  *
 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.
 15  *
 16  */
 17 
 18 #include <linux/module.h>
 19 #include <linux/miscdevice.h>
 20 #include <linux/fs.h>
 21 #include <linux/platform_device.h>
 22 #include <linux/types.h>
 23 #include <linux/pci.h>
 24 #include <linux/interrupt.h>
 25 #include <linux/io.h>
 26 #include <linux/sched.h>
 27 #include <linux/dma-mapping.h>
 28 #include <linux/uaccess.h>
 29 #include <linux/goldfish.h>
 30 
 31 MODULE_AUTHOR("Google, Inc.");
 32 MODULE_DESCRIPTION("Android QEMU Audio Driver");
 33 MODULE_LICENSE("GPL");
 34 MODULE_VERSION("1.0");
 35 
 36 struct goldfish_audio {
 37         char __iomem *reg_base;
 38         int irq;
 39         spinlock_t lock;
 40         wait_queue_head_t wait;
 41 
 42         char __iomem *buffer_virt;      /* combined buffer virtual address */
 43         unsigned long buffer_phys;      /* combined buffer physical address */
 44 
 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 */
 48         int buffer_status;
 49         int read_supported;         /* true if we have audio input support */
 50 };
 51 
 52 /*
 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.
 56  */
 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))
 61 
 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))
 66 
 67 /*
 68  *  temporary variable used between goldfish_audio_probe() and
 69  *  goldfish_audio_open()
 70  */
 71 static struct goldfish_audio *audio_data;
 72 
 73 enum {
 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,
 86 
 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,
 92 
 93         /* driver writes number of bytes to read */
 94         AUDIO_START_READ  = 0x20,
 95 
 96         /* number of bytes available in read buffer */
 97         AUDIO_READ_BUFFER_AVAILABLE  = 0x24,
 98 
 99         /* AUDIO_INT_STATUS bits */
100 
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,
105 
106         AUDIO_INT_MASK                  = AUDIO_INT_WRITE_BUFFER_1_EMPTY |
107                                           AUDIO_INT_WRITE_BUFFER_2_EMPTY |
108                                           AUDIO_INT_READ_BUFFER_FULL,
109 };
110 
111 
112 static atomic_t open_count = ATOMIC_INIT(0);
113 
114 
115 static ssize_t goldfish_audio_read(struct file *fp, char __user *buf,
116                                                 size_t count, loff_t *pos)
117 {
118         struct goldfish_audio *data = fp->private_data;
119         int length;
120         int result = 0;
121 
122         if (!data->read_supported)
123                 return -ENODEV;
124 
125         while (count > 0) {
126                 length = (count > READ_BUFFER_SIZE ? READ_BUFFER_SIZE : count);
127                 AUDIO_WRITE(data, AUDIO_START_READ, length);
128 
129                 wait_event_interruptible(data->wait,
130                         (data->buffer_status & AUDIO_INT_READ_BUFFER_FULL));
131 
132                 length = AUDIO_READ(data,
133                                                 AUDIO_READ_BUFFER_AVAILABLE);
134 
135                 /* copy data to user space */
136                 if (copy_to_user(buf, data->read_buffer, length))
137                         return -EFAULT;
138 
139                 result += length;
140                 buf += length;
141                 count -= length;
142         }
143         return result;
144 }
145 
146 static ssize_t goldfish_audio_write(struct file *fp, const char __user *buf,
147                                                  size_t count, loff_t *pos)
148 {
149         struct goldfish_audio *data = fp->private_data;
150         unsigned long irq_flags;
151         ssize_t result = 0;
152         char __iomem *kbuf;
153 
154         while (count > 0) {
155                 ssize_t copy = count;
156 
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)));
162 
163                 if ((data->buffer_status & AUDIO_INT_WRITE_BUFFER_1_EMPTY) != 0)
164                         kbuf = data->write_buffer1;
165                 else
166                         kbuf = data->write_buffer2;
167 
168                 /* copy from user space to the appropriate buffer */
169                 if (copy_from_user(kbuf, buf, copy)) {
170                         result = -EFAULT;
171                         break;
172                 }
173 
174                 spin_lock_irqsave(&data->lock, irq_flags);
175                 /*
176                  *  clear the buffer empty flag, and signal the emulator
177                  *  to start writing the buffer
178                  */
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);
182                 } else {
183                         data->buffer_status &= ~AUDIO_INT_WRITE_BUFFER_2_EMPTY;
184                         AUDIO_WRITE(data, AUDIO_WRITE_BUFFER_2, copy);
185                 }
186                 spin_unlock_irqrestore(&data->lock, irq_flags);
187 
188                 buf += copy;
189                 result += copy;
190                 count -= copy;
191         }
192         return result;
193 }
194 
195 static int goldfish_audio_open(struct inode *ip, struct file *fp)
196 {
197         if (!audio_data)
198                 return -ENODEV;
199 
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);
205                 return 0;
206         } else {
207                 atomic_dec(&open_count);
208                 return -EBUSY;
209         }
210 }
211 
212 static int goldfish_audio_release(struct inode *ip, struct file *fp)
213 {
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);
217         return 0;
218 }
219 
220 static long goldfish_audio_ioctl(struct file *fp, unsigned int cmd,
221                                                         unsigned long arg)
222 {
223         /* temporary workaround, until we switch to the ALSA API */
224         if (cmd == 315)
225                 return -1;
226         else
227                 return 0;
228 }
229 
230 static irqreturn_t goldfish_audio_interrupt(int irq, void *dev_id)
231 {
232         unsigned long irq_flags;
233         struct goldfish_audio   *data = dev_id;
234         u32 status;
235 
236         spin_lock_irqsave(&data->lock, irq_flags);
237 
238         /* read buffer status flags */
239         status = AUDIO_READ(data, AUDIO_INT_STATUS);
240         status &= AUDIO_INT_MASK;
241         /*
242          *  if buffers are newly empty, wake up blocked
243          *  goldfish_audio_write() call
244          */
245         if (status) {
246                 data->buffer_status = status;
247                 wake_up(&data->wait);
248         }
249 
250         spin_unlock_irqrestore(&data->lock, irq_flags);
251         return status ? IRQ_HANDLED : IRQ_NONE;
252 }
253 
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,
262 };
263 
264 static struct miscdevice goldfish_audio_device = {
265         .minor = MISC_DYNAMIC_MINOR,
266         .name = "eac",
267         .fops = &goldfish_audio_fops,
268 };
269 
270 static int goldfish_audio_probe(struct platform_device *pdev)
271 {
272         int ret;
273         struct resource *r;
274         struct goldfish_audio *data;
275         dma_addr_t buf_addr;
276 
277         data = kzalloc(sizeof(*data), GFP_KERNEL);
278         if (data == NULL) {
279                 ret = -ENOMEM;
280                 goto err_data_alloc_failed;
281         }
282         spin_lock_init(&data->lock);
283         init_waitqueue_head(&data->wait);
284         platform_set_drvdata(pdev, data);
285 
286         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
287         if (r == NULL) {
288                 dev_err(&pdev->dev, "platform_get_resource failed\n");
289                 ret = -ENODEV;
290                 goto err_no_io_base;
291         }
292         data->reg_base = ioremap(r->start, PAGE_SIZE);
293         if (data->reg_base == NULL) {
294                 ret = -ENOMEM;
295                 goto err_no_io_base;
296         }
297 
298         data->irq = platform_get_irq(pdev, 0);
299         if (data->irq < 0) {
300                 dev_err(&pdev->dev, "platform_get_irq failed\n");
301                 ret = -ENODEV;
302                 goto err_no_irq;
303         }
304         data->buffer_virt = dma_alloc_coherent(&pdev->dev,
305                                 COMBINED_BUFFER_SIZE, &buf_addr, GFP_KERNEL);
306         if (data->buffer_virt == 0) {
307                 ret = -ENOMEM;
308                 dev_err(&pdev->dev, "allocate buffer failed\n");
309                 goto err_alloc_write_buffer_failed;
310         }
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;
315 
316         ret = request_irq(data->irq, goldfish_audio_interrupt,
317                                         IRQF_SHARED, pdev->name, data);
318         if (ret) {
319                 dev_err(&pdev->dev, "request_irq failed\n");
320                 goto err_request_irq_failed;
321         }
322 
323         ret = misc_register(&goldfish_audio_device);
324         if (ret) {
325                 dev_err(&pdev->dev,
326                         "misc_register returned %d in goldfish_audio_init\n",
327                                                                 ret);
328                 goto err_misc_register_failed;
329         }
330 
331         AUDIO_WRITE64(data, AUDIO_SET_WRITE_BUFFER_1,
332                                 AUDIO_SET_WRITE_BUFFER_1_HIGH, buf_addr);
333         buf_addr += WRITE_BUFFER_SIZE;
334 
335         AUDIO_WRITE64(data, AUDIO_SET_WRITE_BUFFER_2,
336                                 AUDIO_SET_WRITE_BUFFER_2_HIGH, buf_addr);
337 
338         buf_addr += WRITE_BUFFER_SIZE;
339 
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);
344 
345         audio_data = data;
346         return 0;
347 
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:
354 err_no_irq:
355         iounmap(data->reg_base);
356 err_no_io_base:
357         kfree(data);
358 err_data_alloc_failed:
359         return ret;
360 }
361 
362 static int goldfish_audio_remove(struct platform_device *pdev)
363 {
364         struct goldfish_audio *data = platform_get_drvdata(pdev);
365 
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);
371         kfree(data);
372         audio_data = NULL;
373         return 0;
374 }
375 
376 static struct platform_driver goldfish_audio_driver = {
377         .probe          = goldfish_audio_probe,
378         .remove         = goldfish_audio_remove,
379         .driver = {
380                 .name = "goldfish_audio"
381         }
382 };
383 
384 module_platform_driver(goldfish_audio_driver);
385 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us