patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / isa / gus / gus_mem_proc.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  GUS's memory access via proc filesystem
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
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  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <sound/driver.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include <sound/gus.h>
26 #include <sound/info.h>
27
28 typedef struct gus_proc_private {
29         int rom;                /* data are in ROM */
30         unsigned int address;
31         unsigned int size;
32         snd_gus_card_t * gus;
33 } gus_proc_private_t;
34
35 static long snd_gf1_mem_proc_dump(snd_info_entry_t *entry, void *file_private_data,
36                                   struct file *file, char __user *buf, long count)
37 {
38         long size;
39         gus_proc_private_t *priv = snd_magic_cast(gus_proc_private_t, entry->private_data, return -ENXIO);
40         snd_gus_card_t *gus = priv->gus;
41         int err;
42
43         size = count;
44         if (file->f_pos + size > priv->size)
45                 size = (long)priv->size - file->f_pos;
46         if (size > 0) {
47                 if ((err = snd_gus_dram_read(gus, buf, file->f_pos, size, priv->rom)) < 0)
48                         return err;
49                 file->f_pos += size;
50                 return size;
51         }
52         return 0;
53 }                       
54
55 static long long snd_gf1_mem_proc_llseek(snd_info_entry_t *entry,
56                                         void *private_file_data,
57                                         struct file *file,
58                                         long long offset,
59                                         int orig)
60 {
61         gus_proc_private_t *priv = snd_magic_cast(gus_proc_private_t, entry->private_data, return -ENXIO);
62
63         switch (orig) {
64         case 0: /* SEEK_SET */
65                 file->f_pos = offset;
66                 break;
67         case 1: /* SEEK_CUR */
68                 file->f_pos += offset;
69                 break;
70         case 2: /* SEEK_END, offset is negative */
71                 file->f_pos = priv->size + offset;
72                 break;
73         default:
74                 return -EINVAL;
75         }
76         if (file->f_pos > priv->size)
77                 file->f_pos = priv->size;
78         return file->f_pos;
79 }
80
81 static void snd_gf1_mem_proc_free(snd_info_entry_t *entry)
82 {
83         gus_proc_private_t *priv = snd_magic_cast(gus_proc_private_t, entry->private_data, return);
84         snd_magic_kfree(priv);
85 }
86
87 static struct snd_info_entry_ops snd_gf1_mem_proc_ops = {
88         .read = snd_gf1_mem_proc_dump,
89         .llseek = snd_gf1_mem_proc_llseek,
90 };
91
92 int snd_gf1_mem_proc_init(snd_gus_card_t * gus)
93 {
94         int idx;
95         char name[16];
96         gus_proc_private_t *priv;
97         snd_info_entry_t *entry;
98
99         for (idx = 0; idx < 4; idx++) {
100                 if (gus->gf1.mem_alloc.banks_8[idx].size > 0) {
101                         priv = snd_magic_kcalloc(gus_proc_private_t, 0, GFP_KERNEL);
102                         if (priv == NULL)
103                                 return -ENOMEM;
104                         priv->gus = gus;
105                         sprintf(name, "gus-ram-%i", idx);
106                         if (! snd_card_proc_new(gus->card, name, &entry)) {
107                                 entry->content = SNDRV_INFO_CONTENT_DATA;
108                                 entry->private_data = priv;
109                                 entry->private_free = snd_gf1_mem_proc_free;
110                                 entry->c.ops = &snd_gf1_mem_proc_ops;
111                                 priv->address = gus->gf1.mem_alloc.banks_8[idx].address;
112                                 priv->size = entry->size = gus->gf1.mem_alloc.banks_8[idx].size;
113                         }
114                 }
115         }
116         for (idx = 0; idx < 4; idx++) {
117                 if (gus->gf1.rom_present & (1 << idx)) {
118                         priv = snd_magic_kcalloc(gus_proc_private_t, 0, GFP_KERNEL);
119                         if (priv == NULL)
120                                 return -ENOMEM;
121                         priv->rom = 1;
122                         priv->gus = gus;
123                         sprintf(name, "gus-rom-%i", idx);
124                         if (! snd_card_proc_new(gus->card, name, &entry)) {
125                                 entry->content = SNDRV_INFO_CONTENT_DATA;
126                                 entry->private_data = priv;
127                                 entry->private_free = snd_gf1_mem_proc_free;
128                                 entry->c.ops = &snd_gf1_mem_proc_ops;
129                                 priv->address = idx * 4096 * 1024;
130                                 priv->size = entry->size = gus->gf1.rom_memory;
131                         }
132                 }
133         }
134         return 0;
135 }