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