Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Information interface for ALSA driver |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/time.h> |
Andrea Righi | 27ac792 | 2008-07-23 21:28:13 -0700 | [diff] [blame] | 24 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/smp_lock.h> |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 26 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <sound/core.h> |
| 28 | #include <sound/minors.h> |
| 29 | #include <sound/info.h> |
| 30 | #include <sound/version.h> |
| 31 | #include <linux/proc_fs.h> |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <stdarg.h> |
| 34 | |
| 35 | /* |
| 36 | * |
| 37 | */ |
| 38 | |
Takashi Iwai | e28563c | 2005-12-01 10:42:42 +0100 | [diff] [blame] | 39 | #ifdef CONFIG_PROC_FS |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | int snd_info_check_reserved_words(const char *str) |
| 42 | { |
| 43 | static char *reserved[] = |
| 44 | { |
| 45 | "version", |
| 46 | "meminfo", |
| 47 | "memdebug", |
| 48 | "detect", |
| 49 | "devices", |
| 50 | "oss", |
| 51 | "cards", |
| 52 | "timers", |
| 53 | "synth", |
| 54 | "pcm", |
| 55 | "seq", |
| 56 | NULL |
| 57 | }; |
| 58 | char **xstr = reserved; |
| 59 | |
| 60 | while (*xstr) { |
| 61 | if (!strcmp(*xstr, str)) |
| 62 | return 0; |
| 63 | xstr++; |
| 64 | } |
| 65 | if (!strncmp(str, "card", 4)) |
| 66 | return 0; |
| 67 | return 1; |
| 68 | } |
| 69 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 70 | static DEFINE_MUTEX(info_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 72 | struct snd_info_private_data { |
| 73 | struct snd_info_buffer *rbuffer; |
| 74 | struct snd_info_buffer *wbuffer; |
| 75 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | void *file_private_data; |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 77 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | static int snd_info_version_init(void); |
| 80 | static int snd_info_version_done(void); |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 81 | static void snd_info_disconnect(struct snd_info_entry *entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 84 | /* resize the proc r/w buffer */ |
| 85 | static int resize_info_buffer(struct snd_info_buffer *buffer, |
| 86 | unsigned int nsize) |
| 87 | { |
| 88 | char *nbuf; |
| 89 | |
| 90 | nsize = PAGE_ALIGN(nsize); |
| 91 | nbuf = kmalloc(nsize, GFP_KERNEL); |
| 92 | if (! nbuf) |
| 93 | return -ENOMEM; |
| 94 | |
| 95 | memcpy(nbuf, buffer->buffer, buffer->len); |
| 96 | kfree(buffer->buffer); |
| 97 | buffer->buffer = nbuf; |
| 98 | buffer->len = nsize; |
| 99 | return 0; |
| 100 | } |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /** |
| 103 | * snd_iprintf - printf on the procfs buffer |
| 104 | * @buffer: the procfs buffer |
| 105 | * @fmt: the printf format |
| 106 | * |
| 107 | * Outputs the string on the procfs buffer just like printf(). |
| 108 | * |
| 109 | * Returns the size of output string. |
| 110 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 111 | int snd_iprintf(struct snd_info_buffer *buffer, char *fmt,...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
| 113 | va_list args; |
| 114 | int len, res; |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 115 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Takashi Iwai | f001c3a | 2006-04-28 15:13:41 +0200 | [diff] [blame] | 117 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | if (buffer->stop || buffer->error) |
| 119 | return 0; |
| 120 | len = buffer->len - buffer->size; |
| 121 | va_start(args, fmt); |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 122 | for (;;) { |
Takashi Iwai | dbedca3 | 2006-10-18 19:09:46 +0200 | [diff] [blame] | 123 | va_list ap; |
| 124 | va_copy(ap, args); |
| 125 | res = vsnprintf(buffer->buffer + buffer->curr, len, fmt, ap); |
| 126 | va_end(ap); |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 127 | if (res < len) |
| 128 | break; |
| 129 | err = resize_info_buffer(buffer, buffer->len + PAGE_SIZE); |
| 130 | if (err < 0) |
| 131 | break; |
| 132 | len = buffer->len - buffer->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 134 | va_end(args); |
| 135 | |
| 136 | if (err < 0) |
| 137 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | buffer->curr += res; |
| 139 | buffer->size += res; |
| 140 | return res; |
| 141 | } |
| 142 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 143 | EXPORT_SYMBOL(snd_iprintf); |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* |
| 146 | |
| 147 | */ |
| 148 | |
Takashi Iwai | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 149 | static struct proc_dir_entry *snd_proc_root; |
| 150 | struct snd_info_entry *snd_seq_root; |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 151 | EXPORT_SYMBOL(snd_seq_root); |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | #ifdef CONFIG_SND_OSSEMUL |
Takashi Iwai | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 154 | struct snd_info_entry *snd_oss_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | #endif |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | static void snd_remove_proc_entry(struct proc_dir_entry *parent, |
| 158 | struct proc_dir_entry *de) |
| 159 | { |
| 160 | if (de) |
| 161 | remove_proc_entry(de->name, parent); |
| 162 | } |
| 163 | |
| 164 | static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig) |
| 165 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 166 | struct snd_info_private_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | struct snd_info_entry *entry; |
| 168 | loff_t ret; |
| 169 | |
| 170 | data = file->private_data; |
| 171 | entry = data->entry; |
| 172 | lock_kernel(); |
| 173 | switch (entry->content) { |
| 174 | case SNDRV_INFO_CONTENT_TEXT: |
| 175 | switch (orig) { |
Josef 'Jeff' Sipek | e6f8f10 | 2006-09-21 11:31:58 +0200 | [diff] [blame] | 176 | case SEEK_SET: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | file->f_pos = offset; |
| 178 | ret = file->f_pos; |
| 179 | goto out; |
Josef 'Jeff' Sipek | e6f8f10 | 2006-09-21 11:31:58 +0200 | [diff] [blame] | 180 | case SEEK_CUR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | file->f_pos += offset; |
| 182 | ret = file->f_pos; |
| 183 | goto out; |
Josef 'Jeff' Sipek | e6f8f10 | 2006-09-21 11:31:58 +0200 | [diff] [blame] | 184 | case SEEK_END: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | default: |
| 186 | ret = -EINVAL; |
| 187 | goto out; |
| 188 | } |
| 189 | break; |
| 190 | case SNDRV_INFO_CONTENT_DATA: |
| 191 | if (entry->c.ops->llseek) { |
| 192 | ret = entry->c.ops->llseek(entry, |
| 193 | data->file_private_data, |
| 194 | file, offset, orig); |
| 195 | goto out; |
| 196 | } |
| 197 | break; |
| 198 | } |
| 199 | ret = -ENXIO; |
| 200 | out: |
| 201 | unlock_kernel(); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | static ssize_t snd_info_entry_read(struct file *file, char __user *buffer, |
| 206 | size_t count, loff_t * offset) |
| 207 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 208 | struct snd_info_private_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | struct snd_info_entry *entry; |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 210 | struct snd_info_buffer *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | size_t size = 0; |
| 212 | loff_t pos; |
| 213 | |
| 214 | data = file->private_data; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 215 | if (snd_BUG_ON(!data)) |
| 216 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | pos = *offset; |
| 218 | if (pos < 0 || (long) pos != pos || (ssize_t) count < 0) |
| 219 | return -EIO; |
| 220 | if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos) |
| 221 | return -EIO; |
| 222 | entry = data->entry; |
| 223 | switch (entry->content) { |
| 224 | case SNDRV_INFO_CONTENT_TEXT: |
| 225 | buf = data->rbuffer; |
| 226 | if (buf == NULL) |
| 227 | return -EIO; |
| 228 | if (pos >= buf->size) |
| 229 | return 0; |
| 230 | size = buf->size - pos; |
| 231 | size = min(count, size); |
| 232 | if (copy_to_user(buffer, buf->buffer + pos, size)) |
| 233 | return -EFAULT; |
| 234 | break; |
| 235 | case SNDRV_INFO_CONTENT_DATA: |
| 236 | if (entry->c.ops->read) |
| 237 | size = entry->c.ops->read(entry, |
| 238 | data->file_private_data, |
| 239 | file, buffer, count, pos); |
| 240 | break; |
| 241 | } |
| 242 | if ((ssize_t) size > 0) |
| 243 | *offset = pos + size; |
| 244 | return size; |
| 245 | } |
| 246 | |
| 247 | static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer, |
| 248 | size_t count, loff_t * offset) |
| 249 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 250 | struct snd_info_private_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | struct snd_info_entry *entry; |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 252 | struct snd_info_buffer *buf; |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 253 | ssize_t size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | loff_t pos; |
| 255 | |
| 256 | data = file->private_data; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 257 | if (snd_BUG_ON(!data)) |
| 258 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | entry = data->entry; |
| 260 | pos = *offset; |
| 261 | if (pos < 0 || (long) pos != pos || (ssize_t) count < 0) |
| 262 | return -EIO; |
| 263 | if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos) |
| 264 | return -EIO; |
| 265 | switch (entry->content) { |
| 266 | case SNDRV_INFO_CONTENT_TEXT: |
| 267 | buf = data->wbuffer; |
| 268 | if (buf == NULL) |
| 269 | return -EIO; |
Clemens Ladisch | f4a747f | 2006-05-02 15:33:25 +0200 | [diff] [blame] | 270 | mutex_lock(&entry->access); |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 271 | if (pos + count >= buf->len) { |
| 272 | if (resize_info_buffer(buf, pos + count)) { |
| 273 | mutex_unlock(&entry->access); |
| 274 | return -ENOMEM; |
| 275 | } |
| 276 | } |
| 277 | if (copy_from_user(buf->buffer + pos, buffer, count)) { |
| 278 | mutex_unlock(&entry->access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return -EFAULT; |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 280 | } |
| 281 | buf->size = pos + count; |
| 282 | mutex_unlock(&entry->access); |
| 283 | size = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | break; |
| 285 | case SNDRV_INFO_CONTENT_DATA: |
| 286 | if (entry->c.ops->write) |
| 287 | size = entry->c.ops->write(entry, |
| 288 | data->file_private_data, |
| 289 | file, buffer, count, pos); |
| 290 | break; |
| 291 | } |
| 292 | if ((ssize_t) size > 0) |
| 293 | *offset = pos + size; |
| 294 | return size; |
| 295 | } |
| 296 | |
| 297 | static int snd_info_entry_open(struct inode *inode, struct file *file) |
| 298 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 299 | struct snd_info_entry *entry; |
| 300 | struct snd_info_private_data *data; |
| 301 | struct snd_info_buffer *buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | struct proc_dir_entry *p; |
| 303 | int mode, err; |
| 304 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 305 | mutex_lock(&info_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | p = PDE(inode); |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 307 | entry = p == NULL ? NULL : (struct snd_info_entry *)p->data; |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 308 | if (entry == NULL || ! entry->p) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 309 | mutex_unlock(&info_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | return -ENODEV; |
| 311 | } |
| 312 | if (!try_module_get(entry->module)) { |
| 313 | err = -EFAULT; |
| 314 | goto __error1; |
| 315 | } |
| 316 | mode = file->f_flags & O_ACCMODE; |
| 317 | if (mode == O_RDONLY || mode == O_RDWR) { |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 318 | if ((entry->content == SNDRV_INFO_CONTENT_DATA && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | entry->c.ops->read == NULL)) { |
| 320 | err = -ENODEV; |
| 321 | goto __error; |
| 322 | } |
| 323 | } |
| 324 | if (mode == O_WRONLY || mode == O_RDWR) { |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 325 | if ((entry->content == SNDRV_INFO_CONTENT_DATA && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | entry->c.ops->write == NULL)) { |
| 327 | err = -ENODEV; |
| 328 | goto __error; |
| 329 | } |
| 330 | } |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 331 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | if (data == NULL) { |
| 333 | err = -ENOMEM; |
| 334 | goto __error; |
| 335 | } |
| 336 | data->entry = entry; |
| 337 | switch (entry->content) { |
| 338 | case SNDRV_INFO_CONTENT_TEXT: |
| 339 | if (mode == O_RDONLY || mode == O_RDWR) { |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 340 | buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 341 | if (buffer == NULL) |
| 342 | goto __nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | data->rbuffer = buffer; |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 344 | buffer->len = PAGE_SIZE; |
| 345 | buffer->buffer = kmalloc(buffer->len, GFP_KERNEL); |
| 346 | if (buffer->buffer == NULL) |
| 347 | goto __nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | if (mode == O_WRONLY || mode == O_RDWR) { |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 350 | buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 351 | if (buffer == NULL) |
| 352 | goto __nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | data->wbuffer = buffer; |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 354 | buffer->len = PAGE_SIZE; |
| 355 | buffer->buffer = kmalloc(buffer->len, GFP_KERNEL); |
| 356 | if (buffer->buffer == NULL) |
| 357 | goto __nomem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | break; |
| 360 | case SNDRV_INFO_CONTENT_DATA: /* data */ |
| 361 | if (entry->c.ops->open) { |
| 362 | if ((err = entry->c.ops->open(entry, mode, |
| 363 | &data->file_private_data)) < 0) { |
| 364 | kfree(data); |
| 365 | goto __error; |
| 366 | } |
| 367 | } |
| 368 | break; |
| 369 | } |
| 370 | file->private_data = data; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 371 | mutex_unlock(&info_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if (entry->content == SNDRV_INFO_CONTENT_TEXT && |
| 373 | (mode == O_RDONLY || mode == O_RDWR)) { |
| 374 | if (entry->c.text.read) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 375 | mutex_lock(&entry->access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | entry->c.text.read(entry, data->rbuffer); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 377 | mutex_unlock(&entry->access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | return 0; |
| 381 | |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 382 | __nomem: |
| 383 | if (data->rbuffer) { |
| 384 | kfree(data->rbuffer->buffer); |
| 385 | kfree(data->rbuffer); |
| 386 | } |
| 387 | if (data->wbuffer) { |
| 388 | kfree(data->wbuffer->buffer); |
| 389 | kfree(data->wbuffer); |
| 390 | } |
| 391 | kfree(data); |
| 392 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | __error: |
| 394 | module_put(entry->module); |
| 395 | __error1: |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 396 | mutex_unlock(&info_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return err; |
| 398 | } |
| 399 | |
| 400 | static int snd_info_entry_release(struct inode *inode, struct file *file) |
| 401 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 402 | struct snd_info_entry *entry; |
| 403 | struct snd_info_private_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | int mode; |
| 405 | |
| 406 | mode = file->f_flags & O_ACCMODE; |
| 407 | data = file->private_data; |
| 408 | entry = data->entry; |
| 409 | switch (entry->content) { |
| 410 | case SNDRV_INFO_CONTENT_TEXT: |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 411 | if (data->rbuffer) { |
| 412 | kfree(data->rbuffer->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | kfree(data->rbuffer); |
| 414 | } |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 415 | if (data->wbuffer) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | if (entry->c.text.write) { |
| 417 | entry->c.text.write(entry, data->wbuffer); |
| 418 | if (data->wbuffer->error) { |
| 419 | snd_printk(KERN_WARNING "data write error to %s (%i)\n", |
| 420 | entry->name, |
| 421 | data->wbuffer->error); |
| 422 | } |
| 423 | } |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 424 | kfree(data->wbuffer->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | kfree(data->wbuffer); |
| 426 | } |
| 427 | break; |
| 428 | case SNDRV_INFO_CONTENT_DATA: |
| 429 | if (entry->c.ops->release) |
| 430 | entry->c.ops->release(entry, mode, |
| 431 | data->file_private_data); |
| 432 | break; |
| 433 | } |
| 434 | module_put(entry->module); |
| 435 | kfree(data); |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static unsigned int snd_info_entry_poll(struct file *file, poll_table * wait) |
| 440 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 441 | struct snd_info_private_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | struct snd_info_entry *entry; |
| 443 | unsigned int mask; |
| 444 | |
| 445 | data = file->private_data; |
| 446 | if (data == NULL) |
| 447 | return 0; |
| 448 | entry = data->entry; |
| 449 | mask = 0; |
| 450 | switch (entry->content) { |
| 451 | case SNDRV_INFO_CONTENT_DATA: |
| 452 | if (entry->c.ops->poll) |
| 453 | return entry->c.ops->poll(entry, |
| 454 | data->file_private_data, |
| 455 | file, wait); |
| 456 | if (entry->c.ops->read) |
| 457 | mask |= POLLIN | POLLRDNORM; |
| 458 | if (entry->c.ops->write) |
| 459 | mask |= POLLOUT | POLLWRNORM; |
| 460 | break; |
| 461 | } |
| 462 | return mask; |
| 463 | } |
| 464 | |
Ingo Molnar | d99e988 | 2006-01-09 16:44:46 +0100 | [diff] [blame] | 465 | static long snd_info_entry_ioctl(struct file *file, unsigned int cmd, |
| 466 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 468 | struct snd_info_private_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | struct snd_info_entry *entry; |
| 470 | |
| 471 | data = file->private_data; |
| 472 | if (data == NULL) |
| 473 | return 0; |
| 474 | entry = data->entry; |
| 475 | switch (entry->content) { |
| 476 | case SNDRV_INFO_CONTENT_DATA: |
| 477 | if (entry->c.ops->ioctl) |
| 478 | return entry->c.ops->ioctl(entry, |
| 479 | data->file_private_data, |
| 480 | file, cmd, arg); |
| 481 | break; |
| 482 | } |
| 483 | return -ENOTTY; |
| 484 | } |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma) |
| 487 | { |
Josef Sipek | 7bc5632 | 2006-12-08 02:37:40 -0800 | [diff] [blame] | 488 | struct inode *inode = file->f_path.dentry->d_inode; |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 489 | struct snd_info_private_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | struct snd_info_entry *entry; |
| 491 | |
| 492 | data = file->private_data; |
| 493 | if (data == NULL) |
| 494 | return 0; |
| 495 | entry = data->entry; |
| 496 | switch (entry->content) { |
| 497 | case SNDRV_INFO_CONTENT_DATA: |
| 498 | if (entry->c.ops->mmap) |
| 499 | return entry->c.ops->mmap(entry, |
| 500 | data->file_private_data, |
| 501 | inode, file, vma); |
| 502 | break; |
| 503 | } |
| 504 | return -ENXIO; |
| 505 | } |
| 506 | |
Arjan van de Ven | 9c2e08c | 2007-02-12 00:55:37 -0800 | [diff] [blame] | 507 | static const struct file_operations snd_info_entry_operations = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Ingo Molnar | d99e988 | 2006-01-09 16:44:46 +0100 | [diff] [blame] | 509 | .owner = THIS_MODULE, |
| 510 | .llseek = snd_info_entry_llseek, |
| 511 | .read = snd_info_entry_read, |
| 512 | .write = snd_info_entry_write, |
| 513 | .poll = snd_info_entry_poll, |
| 514 | .unlocked_ioctl = snd_info_entry_ioctl, |
| 515 | .mmap = snd_info_entry_mmap, |
| 516 | .open = snd_info_entry_open, |
| 517 | .release = snd_info_entry_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | }; |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | int __init snd_info_init(void) |
| 521 | { |
| 522 | struct proc_dir_entry *p; |
| 523 | |
Alexey Dobriyan | 99b7623 | 2009-03-25 22:48:06 +0300 | [diff] [blame^] | 524 | p = create_proc_entry("asound", S_IFDIR | S_IRUGO | S_IXUGO, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | if (p == NULL) |
| 526 | return -ENOMEM; |
| 527 | snd_proc_root = p; |
| 528 | #ifdef CONFIG_SND_OSSEMUL |
| 529 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 530 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if ((entry = snd_info_create_module_entry(THIS_MODULE, "oss", NULL)) == NULL) |
| 532 | return -ENOMEM; |
| 533 | entry->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 534 | if (snd_info_register(entry) < 0) { |
| 535 | snd_info_free_entry(entry); |
| 536 | return -ENOMEM; |
| 537 | } |
| 538 | snd_oss_root = entry; |
| 539 | } |
| 540 | #endif |
| 541 | #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE) |
| 542 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 543 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | if ((entry = snd_info_create_module_entry(THIS_MODULE, "seq", NULL)) == NULL) |
| 545 | return -ENOMEM; |
| 546 | entry->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 547 | if (snd_info_register(entry) < 0) { |
| 548 | snd_info_free_entry(entry); |
| 549 | return -ENOMEM; |
| 550 | } |
| 551 | snd_seq_root = entry; |
| 552 | } |
| 553 | #endif |
| 554 | snd_info_version_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | snd_minor_info_init(); |
| 556 | snd_minor_info_oss_init(); |
| 557 | snd_card_info_init(); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | int __exit snd_info_done(void) |
| 562 | { |
| 563 | snd_card_info_done(); |
| 564 | snd_minor_info_oss_done(); |
| 565 | snd_minor_info_done(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | snd_info_version_done(); |
| 567 | if (snd_proc_root) { |
| 568 | #if defined(CONFIG_SND_SEQUENCER) || defined(CONFIG_SND_SEQUENCER_MODULE) |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 569 | snd_info_free_entry(snd_seq_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | #endif |
| 571 | #ifdef CONFIG_SND_OSSEMUL |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 572 | snd_info_free_entry(snd_oss_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | #endif |
Alexey Dobriyan | c74c120 | 2008-04-29 01:01:44 -0700 | [diff] [blame] | 574 | snd_remove_proc_entry(NULL, snd_proc_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | |
| 581 | */ |
| 582 | |
| 583 | |
| 584 | /* |
| 585 | * create a card proc file |
| 586 | * called from init.c |
| 587 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 588 | int snd_info_card_create(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | { |
| 590 | char str[8]; |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 591 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 593 | if (snd_BUG_ON(!card)) |
| 594 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | sprintf(str, "card%i", card->number); |
| 597 | if ((entry = snd_info_create_module_entry(card->module, str, NULL)) == NULL) |
| 598 | return -ENOMEM; |
| 599 | entry->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 600 | if (snd_info_register(entry) < 0) { |
| 601 | snd_info_free_entry(entry); |
| 602 | return -ENOMEM; |
| 603 | } |
| 604 | card->proc_root = entry; |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * register the card proc file |
| 610 | * called from init.c |
| 611 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 612 | int snd_info_card_register(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | { |
| 614 | struct proc_dir_entry *p; |
| 615 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 616 | if (snd_BUG_ON(!card)) |
| 617 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
| 619 | if (!strcmp(card->id, card->proc_root->name)) |
| 620 | return 0; |
| 621 | |
| 622 | p = proc_symlink(card->id, snd_proc_root, card->proc_root->name); |
| 623 | if (p == NULL) |
| 624 | return -ENOMEM; |
| 625 | card->proc_root_link = p; |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | /* |
Jaroslav Kysela | c2eb9c4 | 2008-11-12 16:31:37 +0100 | [diff] [blame] | 630 | * called on card->id change |
| 631 | */ |
| 632 | void snd_info_card_id_change(struct snd_card *card) |
| 633 | { |
| 634 | mutex_lock(&info_mutex); |
| 635 | if (card->proc_root_link) { |
| 636 | snd_remove_proc_entry(snd_proc_root, card->proc_root_link); |
| 637 | card->proc_root_link = NULL; |
| 638 | } |
| 639 | if (strcmp(card->id, card->proc_root->name)) |
| 640 | card->proc_root_link = proc_symlink(card->id, |
| 641 | snd_proc_root, |
| 642 | card->proc_root->name); |
| 643 | mutex_unlock(&info_mutex); |
| 644 | } |
| 645 | |
| 646 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | * de-register the card proc file |
| 648 | * called from init.c |
| 649 | */ |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 650 | void snd_info_card_disconnect(struct snd_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 652 | if (!card) |
| 653 | return; |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 654 | mutex_lock(&info_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | if (card->proc_root_link) { |
| 656 | snd_remove_proc_entry(snd_proc_root, card->proc_root_link); |
| 657 | card->proc_root_link = NULL; |
| 658 | } |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 659 | if (card->proc_root) |
| 660 | snd_info_disconnect(card->proc_root); |
| 661 | mutex_unlock(&info_mutex); |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * release the card proc file resources |
| 666 | * called from init.c |
| 667 | */ |
| 668 | int snd_info_card_free(struct snd_card *card) |
| 669 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 670 | if (!card) |
| 671 | return 0; |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 672 | snd_info_free_entry(card->proc_root); |
| 673 | card->proc_root = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | |
| 678 | /** |
| 679 | * snd_info_get_line - read one line from the procfs buffer |
| 680 | * @buffer: the procfs buffer |
| 681 | * @line: the buffer to store |
| 682 | * @len: the max. buffer size - 1 |
| 683 | * |
| 684 | * Reads one line from the buffer and stores the string. |
| 685 | * |
| 686 | * Returns zero if successful, or 1 if error or EOF. |
| 687 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 688 | int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
| 690 | int c = -1; |
| 691 | |
| 692 | if (len <= 0 || buffer->stop || buffer->error) |
| 693 | return 1; |
| 694 | while (--len > 0) { |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 695 | c = buffer->buffer[buffer->curr++]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | if (c == '\n') { |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 697 | if (buffer->curr >= buffer->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | buffer->stop = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | break; |
| 700 | } |
| 701 | *line++ = c; |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 702 | if (buffer->curr >= buffer->size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | buffer->stop = 1; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | while (c != '\n' && !buffer->stop) { |
Takashi Iwai | 7e4eeec | 2006-04-28 15:13:40 +0200 | [diff] [blame] | 708 | c = buffer->buffer[buffer->curr++]; |
| 709 | if (buffer->curr >= buffer->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | buffer->stop = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | *line = '\0'; |
| 713 | return 0; |
| 714 | } |
| 715 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 716 | EXPORT_SYMBOL(snd_info_get_line); |
| 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | /** |
Henrik Kretzschmar | 856def8 | 2005-07-08 13:53:42 +0200 | [diff] [blame] | 719 | * snd_info_get_str - parse a string token |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | * @dest: the buffer to store the string token |
| 721 | * @src: the original string |
| 722 | * @len: the max. length of token - 1 |
| 723 | * |
| 724 | * Parses the original string and copy a token to the given |
| 725 | * string buffer. |
| 726 | * |
| 727 | * Returns the updated pointer of the original string so that |
| 728 | * it can be used for the next call. |
| 729 | */ |
| 730 | char *snd_info_get_str(char *dest, char *src, int len) |
| 731 | { |
| 732 | int c; |
| 733 | |
| 734 | while (*src == ' ' || *src == '\t') |
| 735 | src++; |
| 736 | if (*src == '"' || *src == '\'') { |
| 737 | c = *src++; |
| 738 | while (--len > 0 && *src && *src != c) { |
| 739 | *dest++ = *src++; |
| 740 | } |
| 741 | if (*src == c) |
| 742 | src++; |
| 743 | } else { |
| 744 | while (--len > 0 && *src && *src != ' ' && *src != '\t') { |
| 745 | *dest++ = *src++; |
| 746 | } |
| 747 | } |
| 748 | *dest = 0; |
| 749 | while (*src == ' ' || *src == '\t') |
| 750 | src++; |
| 751 | return src; |
| 752 | } |
| 753 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 754 | EXPORT_SYMBOL(snd_info_get_str); |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | /** |
| 757 | * snd_info_create_entry - create an info entry |
| 758 | * @name: the proc file name |
| 759 | * |
| 760 | * Creates an info entry with the given file name and initializes as |
| 761 | * the default state. |
| 762 | * |
| 763 | * Usually called from other functions such as |
| 764 | * snd_info_create_card_entry(). |
| 765 | * |
| 766 | * Returns the pointer of the new instance, or NULL on failure. |
| 767 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 768 | static struct snd_info_entry *snd_info_create_entry(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 770 | struct snd_info_entry *entry; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 771 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | if (entry == NULL) |
| 773 | return NULL; |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 774 | entry->name = kstrdup(name, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | if (entry->name == NULL) { |
| 776 | kfree(entry); |
| 777 | return NULL; |
| 778 | } |
| 779 | entry->mode = S_IFREG | S_IRUGO; |
| 780 | entry->content = SNDRV_INFO_CONTENT_TEXT; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 781 | mutex_init(&entry->access); |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 782 | INIT_LIST_HEAD(&entry->children); |
| 783 | INIT_LIST_HEAD(&entry->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | return entry; |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * snd_info_create_module_entry - create an info entry for the given module |
| 789 | * @module: the module pointer |
| 790 | * @name: the file name |
| 791 | * @parent: the parent directory |
| 792 | * |
| 793 | * Creates a new info entry and assigns it to the given module. |
| 794 | * |
| 795 | * Returns the pointer of the new instance, or NULL on failure. |
| 796 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 797 | struct snd_info_entry *snd_info_create_module_entry(struct module * module, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | const char *name, |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 799 | struct snd_info_entry *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 801 | struct snd_info_entry *entry = snd_info_create_entry(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | if (entry) { |
| 803 | entry->module = module; |
| 804 | entry->parent = parent; |
| 805 | } |
| 806 | return entry; |
| 807 | } |
| 808 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 809 | EXPORT_SYMBOL(snd_info_create_module_entry); |
| 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | /** |
| 812 | * snd_info_create_card_entry - create an info entry for the given card |
| 813 | * @card: the card instance |
| 814 | * @name: the file name |
| 815 | * @parent: the parent directory |
| 816 | * |
| 817 | * Creates a new info entry and assigns it to the given card. |
| 818 | * |
| 819 | * Returns the pointer of the new instance, or NULL on failure. |
| 820 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 821 | struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | const char *name, |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 823 | struct snd_info_entry * parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 825 | struct snd_info_entry *entry = snd_info_create_entry(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | if (entry) { |
| 827 | entry->module = card->module; |
| 828 | entry->card = card; |
| 829 | entry->parent = parent; |
| 830 | } |
| 831 | return entry; |
| 832 | } |
| 833 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 834 | EXPORT_SYMBOL(snd_info_create_card_entry); |
| 835 | |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 836 | static void snd_info_disconnect(struct snd_info_entry *entry) |
| 837 | { |
| 838 | struct list_head *p, *n; |
| 839 | struct proc_dir_entry *root; |
| 840 | |
| 841 | list_for_each_safe(p, n, &entry->children) { |
| 842 | snd_info_disconnect(list_entry(p, struct snd_info_entry, list)); |
| 843 | } |
| 844 | |
| 845 | if (! entry->p) |
| 846 | return; |
| 847 | list_del_init(&entry->list); |
| 848 | root = entry->parent == NULL ? snd_proc_root : entry->parent->p; |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 849 | snd_BUG_ON(!root); |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 850 | snd_remove_proc_entry(root, entry->p); |
| 851 | entry->p = NULL; |
| 852 | } |
| 853 | |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 854 | static int snd_info_dev_free_entry(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 856 | struct snd_info_entry *entry = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | snd_info_free_entry(entry); |
| 858 | return 0; |
| 859 | } |
| 860 | |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 861 | static int snd_info_dev_register_entry(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 863 | struct snd_info_entry *entry = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | return snd_info_register(entry); |
| 865 | } |
| 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | /** |
| 868 | * snd_card_proc_new - create an info entry for the given card |
| 869 | * @card: the card instance |
| 870 | * @name: the file name |
| 871 | * @entryp: the pointer to store the new info entry |
| 872 | * |
| 873 | * Creates a new info entry and assigns it to the given card. |
| 874 | * Unlike snd_info_create_card_entry(), this function registers the |
| 875 | * info entry as an ALSA device component, so that it can be |
| 876 | * unregistered/released without explicit call. |
| 877 | * Also, you don't have to register this entry via snd_info_register(), |
| 878 | * since this will be registered by snd_card_register() automatically. |
| 879 | * |
| 880 | * The parent is assumed as card->proc_root. |
| 881 | * |
| 882 | * For releasing this entry, use snd_device_free() instead of |
| 883 | * snd_info_free_entry(). |
| 884 | * |
| 885 | * Returns zero if successful, or a negative error code on failure. |
| 886 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 887 | int snd_card_proc_new(struct snd_card *card, const char *name, |
| 888 | struct snd_info_entry **entryp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 890 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | .dev_free = snd_info_dev_free_entry, |
| 892 | .dev_register = snd_info_dev_register_entry, |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 893 | /* disconnect is done via snd_info_card_disconnect() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | }; |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 895 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | int err; |
| 897 | |
| 898 | entry = snd_info_create_card_entry(card, name, card->proc_root); |
| 899 | if (! entry) |
| 900 | return -ENOMEM; |
| 901 | if ((err = snd_device_new(card, SNDRV_DEV_INFO, entry, &ops)) < 0) { |
| 902 | snd_info_free_entry(entry); |
| 903 | return err; |
| 904 | } |
| 905 | if (entryp) |
| 906 | *entryp = entry; |
| 907 | return 0; |
| 908 | } |
| 909 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 910 | EXPORT_SYMBOL(snd_card_proc_new); |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /** |
| 913 | * snd_info_free_entry - release the info entry |
| 914 | * @entry: the info entry |
| 915 | * |
| 916 | * Releases the info entry. Don't call this after registered. |
| 917 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 918 | void snd_info_free_entry(struct snd_info_entry * entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | { |
| 920 | if (entry == NULL) |
| 921 | return; |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 922 | if (entry->p) { |
| 923 | mutex_lock(&info_mutex); |
| 924 | snd_info_disconnect(entry); |
| 925 | mutex_unlock(&info_mutex); |
| 926 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | kfree(entry->name); |
| 928 | if (entry->private_free) |
| 929 | entry->private_free(entry); |
| 930 | kfree(entry); |
| 931 | } |
| 932 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 933 | EXPORT_SYMBOL(snd_info_free_entry); |
| 934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | /** |
| 936 | * snd_info_register - register the info entry |
| 937 | * @entry: the info entry |
| 938 | * |
| 939 | * Registers the proc info entry. |
| 940 | * |
| 941 | * Returns zero if successful, or a negative error code on failure. |
| 942 | */ |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 943 | int snd_info_register(struct snd_info_entry * entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | { |
| 945 | struct proc_dir_entry *root, *p = NULL; |
| 946 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 947 | if (snd_BUG_ON(!entry)) |
| 948 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | root = entry->parent == NULL ? snd_proc_root : entry->parent->p; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 950 | mutex_lock(&info_mutex); |
Alexey Dobriyan | 99b7623 | 2009-03-25 22:48:06 +0300 | [diff] [blame^] | 951 | p = create_proc_entry(entry->name, entry->mode, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | if (!p) { |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 953 | mutex_unlock(&info_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | return -ENOMEM; |
| 955 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | if (!S_ISDIR(entry->mode)) |
| 957 | p->proc_fops = &snd_info_entry_operations; |
| 958 | p->size = entry->size; |
| 959 | p->data = entry; |
| 960 | entry->p = p; |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 961 | if (entry->parent) |
| 962 | list_add_tail(&entry->list, &entry->parent->children); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 963 | mutex_unlock(&info_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | |
Takashi Iwai | c0d3fb3 | 2006-04-28 15:13:39 +0200 | [diff] [blame] | 967 | EXPORT_SYMBOL(snd_info_register); |
| 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | /* |
| 970 | |
| 971 | */ |
| 972 | |
Takashi Iwai | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 973 | static struct snd_info_entry *snd_info_version_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 975 | static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
| 977 | snd_iprintf(buffer, |
| 978 | "Advanced Linux Sound Architecture Driver Version " |
| 979 | CONFIG_SND_VERSION CONFIG_SND_DATE ".\n" |
| 980 | ); |
| 981 | } |
| 982 | |
| 983 | static int __init snd_info_version_init(void) |
| 984 | { |
Takashi Iwai | 24c1f93 | 2005-11-17 13:58:48 +0100 | [diff] [blame] | 985 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
| 987 | entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL); |
| 988 | if (entry == NULL) |
| 989 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | entry->c.text.read = snd_info_version_read; |
| 991 | if (snd_info_register(entry) < 0) { |
| 992 | snd_info_free_entry(entry); |
| 993 | return -ENOMEM; |
| 994 | } |
| 995 | snd_info_version_entry = entry; |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | static int __exit snd_info_version_done(void) |
| 1000 | { |
Takashi Iwai | 746d4a0 | 2006-06-23 14:37:59 +0200 | [diff] [blame] | 1001 | snd_info_free_entry(snd_info_version_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | return 0; |
| 1003 | } |
| 1004 | |
| 1005 | #endif /* CONFIG_PROC_FS */ |