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