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