blob: 210b89de06d74c9b79264b7b3e59c40785d0e97a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Functions for the OPL4 proc file
3 * Copyright (c) 2003 by Clemens Ladisch <clemens@ladisch.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include "opl4_local.h"
21#include <linux/vmalloc.h>
22#include <sound/info.h>
23
24#ifdef CONFIG_PROC_FS
25
Takashi Iwaia42dd422005-11-17 14:13:47 +010026static int snd_opl4_mem_proc_open(struct snd_info_entry *entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 unsigned short mode, void **file_private_data)
28{
Takashi Iwaia42dd422005-11-17 14:13:47 +010029 struct snd_opl4 *opl4 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Ingo Molnaref9f0a42006-01-16 16:31:42 +010031 mutex_lock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 if (opl4->memory_access) {
Ingo Molnaref9f0a42006-01-16 16:31:42 +010033 mutex_unlock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 return -EBUSY;
35 }
36 opl4->memory_access++;
Ingo Molnaref9f0a42006-01-16 16:31:42 +010037 mutex_unlock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return 0;
39}
40
Takashi Iwaia42dd422005-11-17 14:13:47 +010041static int snd_opl4_mem_proc_release(struct snd_info_entry *entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 unsigned short mode, void *file_private_data)
43{
Takashi Iwaia42dd422005-11-17 14:13:47 +010044 struct snd_opl4 *opl4 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Ingo Molnaref9f0a42006-01-16 16:31:42 +010046 mutex_lock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 opl4->memory_access--;
Ingo Molnaref9f0a42006-01-16 16:31:42 +010048 mutex_unlock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return 0;
50}
51
Takashi Iwai24e4a122010-04-13 11:22:01 +020052static ssize_t snd_opl4_mem_proc_read(struct snd_info_entry *entry,
53 void *file_private_data,
54 struct file *file, char __user *_buf,
55 size_t count, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Takashi Iwaia42dd422005-11-17 14:13:47 +010057 struct snd_opl4 *opl4 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 char* buf;
59
Takashi Iwaid97e1b72010-04-13 11:33:54 +020060 buf = vmalloc(count);
61 if (!buf)
62 return -ENOMEM;
63 snd_opl4_read_memory(opl4, buf, pos, count);
64 if (copy_to_user(_buf, buf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 vfree(buf);
Takashi Iwaid97e1b72010-04-13 11:33:54 +020066 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
Takashi Iwaid97e1b72010-04-13 11:33:54 +020068 vfree(buf);
69 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Takashi Iwai24e4a122010-04-13 11:22:01 +020072static ssize_t snd_opl4_mem_proc_write(struct snd_info_entry *entry,
73 void *file_private_data,
74 struct file *file,
75 const char __user *_buf,
76 size_t count, size_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Takashi Iwaia42dd422005-11-17 14:13:47 +010078 struct snd_opl4 *opl4 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 char *buf;
80
Takashi Iwaid97e1b72010-04-13 11:33:54 +020081 buf = vmalloc(count);
82 if (!buf)
83 return -ENOMEM;
84 if (copy_from_user(buf, _buf, count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 vfree(buf);
Takashi Iwaid97e1b72010-04-13 11:33:54 +020086 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Takashi Iwaid97e1b72010-04-13 11:33:54 +020088 snd_opl4_write_memory(opl4, buf, pos, count);
89 vfree(buf);
90 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Takashi Iwai24e4a122010-04-13 11:22:01 +020093static loff_t snd_opl4_mem_proc_llseek(struct snd_info_entry *entry,
94 void *file_private_data,
95 struct file *file,
96 loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 switch (orig) {
Josef 'Jeff' Sipekdd47a332006-09-21 11:32:43 +020099 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 file->f_pos = offset;
101 break;
Josef 'Jeff' Sipekdd47a332006-09-21 11:32:43 +0200102 case SEEK_CUR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 file->f_pos += offset;
104 break;
Josef 'Jeff' Sipekdd47a332006-09-21 11:32:43 +0200105 case SEEK_END: /* offset is negative */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 file->f_pos = entry->size + offset;
107 break;
108 default:
109 return -EINVAL;
110 }
111 if (file->f_pos > entry->size)
112 file->f_pos = entry->size;
113 return file->f_pos;
114}
115
116static struct snd_info_entry_ops snd_opl4_mem_proc_ops = {
117 .open = snd_opl4_mem_proc_open,
118 .release = snd_opl4_mem_proc_release,
119 .read = snd_opl4_mem_proc_read,
120 .write = snd_opl4_mem_proc_write,
121 .llseek = snd_opl4_mem_proc_llseek,
122};
123
Takashi Iwaia42dd422005-11-17 14:13:47 +0100124int snd_opl4_create_proc(struct snd_opl4 *opl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Takashi Iwaia42dd422005-11-17 14:13:47 +0100126 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 entry = snd_info_create_card_entry(opl4->card, "opl4-mem", opl4->card->proc_root);
129 if (entry) {
130 if (opl4->hardware < OPL3_HW_OPL4_ML) {
131 /* OPL4 can access 4 MB external ROM/SRAM */
132 entry->mode |= S_IWUSR;
133 entry->size = 4 * 1024 * 1024;
134 } else {
135 /* OPL4-ML has 1 MB internal ROM */
136 entry->size = 1 * 1024 * 1024;
137 }
138 entry->content = SNDRV_INFO_CONTENT_DATA;
139 entry->c.ops = &snd_opl4_mem_proc_ops;
140 entry->module = THIS_MODULE;
141 entry->private_data = opl4;
142 if (snd_info_register(entry) < 0) {
143 snd_info_free_entry(entry);
144 entry = NULL;
145 }
146 }
147 opl4->proc_entry = entry;
148 return 0;
149}
150
Takashi Iwaia42dd422005-11-17 14:13:47 +0100151void snd_opl4_free_proc(struct snd_opl4 *opl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Takashi Iwai746d4a02006-06-23 14:37:59 +0200153 snd_info_free_entry(opl4->proc_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156#endif /* CONFIG_PROC_FS */