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