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