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