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
28struct gus_proc_private {
29	int rom;		/* data are in ROM */
30	unsigned int address;
31	unsigned int size;
32	struct snd_gus_card * gus;
33};
34
35static long snd_gf1_mem_proc_dump(struct snd_info_entry *entry, void *file_private_data,
36			          struct file *file, char __user *buf,
37			          unsigned long count, unsigned long pos)
38{
39	long size;
40	struct gus_proc_private *priv = entry->private_data;
41	struct snd_gus_card *gus = priv->gus;
42	int err;
43
44	size = count;
45	if (pos + size > priv->size)
46		size = (long)priv->size - pos;
47	if (size > 0) {
48		if ((err = snd_gus_dram_read(gus, buf, pos, size, priv->rom)) < 0)
49			return err;
50		return size;
51	}
52	return 0;
53}
54
55static long long snd_gf1_mem_proc_llseek(struct snd_info_entry *entry,
56					void *private_file_data,
57					struct file *file,
58					long long offset,
59					int orig)
60{
61	struct gus_proc_private *priv = entry->private_data;
62
63	switch (orig) {
64	case SEEK_SET:
65		file->f_pos = offset;
66		break;
67	case SEEK_CUR:
68		file->f_pos += offset;
69		break;
70	case 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
81static void snd_gf1_mem_proc_free(struct snd_info_entry *entry)
82{
83	struct gus_proc_private *priv = entry->private_data;
84	kfree(priv);
85}
86
87static 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
92int snd_gf1_mem_proc_init(struct snd_gus_card * gus)
93{
94	int idx;
95	char name[16];
96	struct gus_proc_private *priv;
97	struct snd_info_entry *entry;
98
99	for (idx = 0; idx < 4; idx++) {
100		if (gus->gf1.mem_alloc.banks_8[idx].size > 0) {
101			priv = kzalloc(sizeof(*priv), 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 = kzalloc(sizeof(*priv), 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}
136