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