1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Information interface for ALSA driver
4*4882a593Smuzhiyun * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/init.h>
8*4882a593Smuzhiyun #include <linux/time.h>
9*4882a593Smuzhiyun #include <linux/mm.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/string.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <sound/core.h>
14*4882a593Smuzhiyun #include <sound/minors.h>
15*4882a593Smuzhiyun #include <sound/info.h>
16*4882a593Smuzhiyun #include <linux/utsname.h>
17*4882a593Smuzhiyun #include <linux/proc_fs.h>
18*4882a593Smuzhiyun #include <linux/mutex.h>
19*4882a593Smuzhiyun #include <stdarg.h>
20*4882a593Smuzhiyun
snd_info_check_reserved_words(const char * str)21*4882a593Smuzhiyun int snd_info_check_reserved_words(const char *str)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun static const char * const reserved[] =
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun "version",
26*4882a593Smuzhiyun "meminfo",
27*4882a593Smuzhiyun "memdebug",
28*4882a593Smuzhiyun "detect",
29*4882a593Smuzhiyun "devices",
30*4882a593Smuzhiyun "oss",
31*4882a593Smuzhiyun "cards",
32*4882a593Smuzhiyun "timers",
33*4882a593Smuzhiyun "synth",
34*4882a593Smuzhiyun "pcm",
35*4882a593Smuzhiyun "seq",
36*4882a593Smuzhiyun NULL
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun const char * const *xstr = reserved;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun while (*xstr) {
41*4882a593Smuzhiyun if (!strcmp(*xstr, str))
42*4882a593Smuzhiyun return 0;
43*4882a593Smuzhiyun xstr++;
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun if (!strncmp(str, "card", 4))
46*4882a593Smuzhiyun return 0;
47*4882a593Smuzhiyun return 1;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun static DEFINE_MUTEX(info_mutex);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun struct snd_info_private_data {
53*4882a593Smuzhiyun struct snd_info_buffer *rbuffer;
54*4882a593Smuzhiyun struct snd_info_buffer *wbuffer;
55*4882a593Smuzhiyun struct snd_info_entry *entry;
56*4882a593Smuzhiyun void *file_private_data;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun static int snd_info_version_init(void);
60*4882a593Smuzhiyun static void snd_info_disconnect(struct snd_info_entry *entry);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun static struct snd_info_entry *snd_proc_root;
67*4882a593Smuzhiyun struct snd_info_entry *snd_seq_root;
68*4882a593Smuzhiyun EXPORT_SYMBOL(snd_seq_root);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun #ifdef CONFIG_SND_OSSEMUL
71*4882a593Smuzhiyun struct snd_info_entry *snd_oss_root;
72*4882a593Smuzhiyun #endif
73*4882a593Smuzhiyun
alloc_info_private(struct snd_info_entry * entry,struct snd_info_private_data ** ret)74*4882a593Smuzhiyun static int alloc_info_private(struct snd_info_entry *entry,
75*4882a593Smuzhiyun struct snd_info_private_data **ret)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun struct snd_info_private_data *data;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun if (!entry || !entry->p)
80*4882a593Smuzhiyun return -ENODEV;
81*4882a593Smuzhiyun if (!try_module_get(entry->module))
82*4882a593Smuzhiyun return -EFAULT;
83*4882a593Smuzhiyun data = kzalloc(sizeof(*data), GFP_KERNEL);
84*4882a593Smuzhiyun if (!data) {
85*4882a593Smuzhiyun module_put(entry->module);
86*4882a593Smuzhiyun return -ENOMEM;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun data->entry = entry;
89*4882a593Smuzhiyun *ret = data;
90*4882a593Smuzhiyun return 0;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
valid_pos(loff_t pos,size_t count)93*4882a593Smuzhiyun static bool valid_pos(loff_t pos, size_t count)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun if (pos < 0 || (long) pos != pos || (ssize_t) count < 0)
96*4882a593Smuzhiyun return false;
97*4882a593Smuzhiyun if ((unsigned long) pos + (unsigned long) count < (unsigned long) pos)
98*4882a593Smuzhiyun return false;
99*4882a593Smuzhiyun return true;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * file ops for binary proc files
104*4882a593Smuzhiyun */
snd_info_entry_llseek(struct file * file,loff_t offset,int orig)105*4882a593Smuzhiyun static loff_t snd_info_entry_llseek(struct file *file, loff_t offset, int orig)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun struct snd_info_private_data *data;
108*4882a593Smuzhiyun struct snd_info_entry *entry;
109*4882a593Smuzhiyun loff_t ret = -EINVAL, size;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun data = file->private_data;
112*4882a593Smuzhiyun entry = data->entry;
113*4882a593Smuzhiyun mutex_lock(&entry->access);
114*4882a593Smuzhiyun if (entry->c.ops->llseek) {
115*4882a593Smuzhiyun ret = entry->c.ops->llseek(entry,
116*4882a593Smuzhiyun data->file_private_data,
117*4882a593Smuzhiyun file, offset, orig);
118*4882a593Smuzhiyun goto out;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun size = entry->size;
122*4882a593Smuzhiyun switch (orig) {
123*4882a593Smuzhiyun case SEEK_SET:
124*4882a593Smuzhiyun break;
125*4882a593Smuzhiyun case SEEK_CUR:
126*4882a593Smuzhiyun offset += file->f_pos;
127*4882a593Smuzhiyun break;
128*4882a593Smuzhiyun case SEEK_END:
129*4882a593Smuzhiyun if (!size)
130*4882a593Smuzhiyun goto out;
131*4882a593Smuzhiyun offset += size;
132*4882a593Smuzhiyun break;
133*4882a593Smuzhiyun default:
134*4882a593Smuzhiyun goto out;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun if (offset < 0)
137*4882a593Smuzhiyun goto out;
138*4882a593Smuzhiyun if (size && offset > size)
139*4882a593Smuzhiyun offset = size;
140*4882a593Smuzhiyun file->f_pos = offset;
141*4882a593Smuzhiyun ret = offset;
142*4882a593Smuzhiyun out:
143*4882a593Smuzhiyun mutex_unlock(&entry->access);
144*4882a593Smuzhiyun return ret;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
snd_info_entry_read(struct file * file,char __user * buffer,size_t count,loff_t * offset)147*4882a593Smuzhiyun static ssize_t snd_info_entry_read(struct file *file, char __user *buffer,
148*4882a593Smuzhiyun size_t count, loff_t * offset)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun struct snd_info_private_data *data = file->private_data;
151*4882a593Smuzhiyun struct snd_info_entry *entry = data->entry;
152*4882a593Smuzhiyun size_t size;
153*4882a593Smuzhiyun loff_t pos;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun pos = *offset;
156*4882a593Smuzhiyun if (!valid_pos(pos, count))
157*4882a593Smuzhiyun return -EIO;
158*4882a593Smuzhiyun if (pos >= entry->size)
159*4882a593Smuzhiyun return 0;
160*4882a593Smuzhiyun size = entry->size - pos;
161*4882a593Smuzhiyun size = min(count, size);
162*4882a593Smuzhiyun size = entry->c.ops->read(entry, data->file_private_data,
163*4882a593Smuzhiyun file, buffer, size, pos);
164*4882a593Smuzhiyun if ((ssize_t) size > 0)
165*4882a593Smuzhiyun *offset = pos + size;
166*4882a593Smuzhiyun return size;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
snd_info_entry_write(struct file * file,const char __user * buffer,size_t count,loff_t * offset)169*4882a593Smuzhiyun static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer,
170*4882a593Smuzhiyun size_t count, loff_t * offset)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun struct snd_info_private_data *data = file->private_data;
173*4882a593Smuzhiyun struct snd_info_entry *entry = data->entry;
174*4882a593Smuzhiyun ssize_t size = 0;
175*4882a593Smuzhiyun loff_t pos;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun pos = *offset;
178*4882a593Smuzhiyun if (!valid_pos(pos, count))
179*4882a593Smuzhiyun return -EIO;
180*4882a593Smuzhiyun if (count > 0) {
181*4882a593Smuzhiyun size_t maxsize = entry->size - pos;
182*4882a593Smuzhiyun count = min(count, maxsize);
183*4882a593Smuzhiyun size = entry->c.ops->write(entry, data->file_private_data,
184*4882a593Smuzhiyun file, buffer, count, pos);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun if (size > 0)
187*4882a593Smuzhiyun *offset = pos + size;
188*4882a593Smuzhiyun return size;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
snd_info_entry_poll(struct file * file,poll_table * wait)191*4882a593Smuzhiyun static __poll_t snd_info_entry_poll(struct file *file, poll_table *wait)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun struct snd_info_private_data *data = file->private_data;
194*4882a593Smuzhiyun struct snd_info_entry *entry = data->entry;
195*4882a593Smuzhiyun __poll_t mask = 0;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun if (entry->c.ops->poll)
198*4882a593Smuzhiyun return entry->c.ops->poll(entry,
199*4882a593Smuzhiyun data->file_private_data,
200*4882a593Smuzhiyun file, wait);
201*4882a593Smuzhiyun if (entry->c.ops->read)
202*4882a593Smuzhiyun mask |= EPOLLIN | EPOLLRDNORM;
203*4882a593Smuzhiyun if (entry->c.ops->write)
204*4882a593Smuzhiyun mask |= EPOLLOUT | EPOLLWRNORM;
205*4882a593Smuzhiyun return mask;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
snd_info_entry_ioctl(struct file * file,unsigned int cmd,unsigned long arg)208*4882a593Smuzhiyun static long snd_info_entry_ioctl(struct file *file, unsigned int cmd,
209*4882a593Smuzhiyun unsigned long arg)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun struct snd_info_private_data *data = file->private_data;
212*4882a593Smuzhiyun struct snd_info_entry *entry = data->entry;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun if (!entry->c.ops->ioctl)
215*4882a593Smuzhiyun return -ENOTTY;
216*4882a593Smuzhiyun return entry->c.ops->ioctl(entry, data->file_private_data,
217*4882a593Smuzhiyun file, cmd, arg);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
snd_info_entry_mmap(struct file * file,struct vm_area_struct * vma)220*4882a593Smuzhiyun static int snd_info_entry_mmap(struct file *file, struct vm_area_struct *vma)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun struct inode *inode = file_inode(file);
223*4882a593Smuzhiyun struct snd_info_private_data *data;
224*4882a593Smuzhiyun struct snd_info_entry *entry;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun data = file->private_data;
227*4882a593Smuzhiyun if (data == NULL)
228*4882a593Smuzhiyun return 0;
229*4882a593Smuzhiyun entry = data->entry;
230*4882a593Smuzhiyun if (!entry->c.ops->mmap)
231*4882a593Smuzhiyun return -ENXIO;
232*4882a593Smuzhiyun return entry->c.ops->mmap(entry, data->file_private_data,
233*4882a593Smuzhiyun inode, file, vma);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
snd_info_entry_open(struct inode * inode,struct file * file)236*4882a593Smuzhiyun static int snd_info_entry_open(struct inode *inode, struct file *file)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun struct snd_info_entry *entry = PDE_DATA(inode);
239*4882a593Smuzhiyun struct snd_info_private_data *data;
240*4882a593Smuzhiyun int mode, err;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun mutex_lock(&info_mutex);
243*4882a593Smuzhiyun err = alloc_info_private(entry, &data);
244*4882a593Smuzhiyun if (err < 0)
245*4882a593Smuzhiyun goto unlock;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun mode = file->f_flags & O_ACCMODE;
248*4882a593Smuzhiyun if (((mode == O_RDONLY || mode == O_RDWR) && !entry->c.ops->read) ||
249*4882a593Smuzhiyun ((mode == O_WRONLY || mode == O_RDWR) && !entry->c.ops->write)) {
250*4882a593Smuzhiyun err = -ENODEV;
251*4882a593Smuzhiyun goto error;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun if (entry->c.ops->open) {
255*4882a593Smuzhiyun err = entry->c.ops->open(entry, mode, &data->file_private_data);
256*4882a593Smuzhiyun if (err < 0)
257*4882a593Smuzhiyun goto error;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun file->private_data = data;
261*4882a593Smuzhiyun mutex_unlock(&info_mutex);
262*4882a593Smuzhiyun return 0;
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun error:
265*4882a593Smuzhiyun kfree(data);
266*4882a593Smuzhiyun module_put(entry->module);
267*4882a593Smuzhiyun unlock:
268*4882a593Smuzhiyun mutex_unlock(&info_mutex);
269*4882a593Smuzhiyun return err;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
snd_info_entry_release(struct inode * inode,struct file * file)272*4882a593Smuzhiyun static int snd_info_entry_release(struct inode *inode, struct file *file)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun struct snd_info_private_data *data = file->private_data;
275*4882a593Smuzhiyun struct snd_info_entry *entry = data->entry;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun if (entry->c.ops->release)
278*4882a593Smuzhiyun entry->c.ops->release(entry, file->f_flags & O_ACCMODE,
279*4882a593Smuzhiyun data->file_private_data);
280*4882a593Smuzhiyun module_put(entry->module);
281*4882a593Smuzhiyun kfree(data);
282*4882a593Smuzhiyun return 0;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun static const struct proc_ops snd_info_entry_operations =
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun .proc_lseek = snd_info_entry_llseek,
288*4882a593Smuzhiyun .proc_read = snd_info_entry_read,
289*4882a593Smuzhiyun .proc_write = snd_info_entry_write,
290*4882a593Smuzhiyun .proc_poll = snd_info_entry_poll,
291*4882a593Smuzhiyun .proc_ioctl = snd_info_entry_ioctl,
292*4882a593Smuzhiyun .proc_mmap = snd_info_entry_mmap,
293*4882a593Smuzhiyun .proc_open = snd_info_entry_open,
294*4882a593Smuzhiyun .proc_release = snd_info_entry_release,
295*4882a593Smuzhiyun };
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun /*
298*4882a593Smuzhiyun * file ops for text proc files
299*4882a593Smuzhiyun */
snd_info_text_entry_write(struct file * file,const char __user * buffer,size_t count,loff_t * offset)300*4882a593Smuzhiyun static ssize_t snd_info_text_entry_write(struct file *file,
301*4882a593Smuzhiyun const char __user *buffer,
302*4882a593Smuzhiyun size_t count, loff_t *offset)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun struct seq_file *m = file->private_data;
305*4882a593Smuzhiyun struct snd_info_private_data *data = m->private;
306*4882a593Smuzhiyun struct snd_info_entry *entry = data->entry;
307*4882a593Smuzhiyun struct snd_info_buffer *buf;
308*4882a593Smuzhiyun loff_t pos;
309*4882a593Smuzhiyun size_t next;
310*4882a593Smuzhiyun int err = 0;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun if (!entry->c.text.write)
313*4882a593Smuzhiyun return -EIO;
314*4882a593Smuzhiyun pos = *offset;
315*4882a593Smuzhiyun if (!valid_pos(pos, count))
316*4882a593Smuzhiyun return -EIO;
317*4882a593Smuzhiyun next = pos + count;
318*4882a593Smuzhiyun /* don't handle too large text inputs */
319*4882a593Smuzhiyun if (next > 16 * 1024)
320*4882a593Smuzhiyun return -EIO;
321*4882a593Smuzhiyun mutex_lock(&entry->access);
322*4882a593Smuzhiyun buf = data->wbuffer;
323*4882a593Smuzhiyun if (!buf) {
324*4882a593Smuzhiyun data->wbuffer = buf = kzalloc(sizeof(*buf), GFP_KERNEL);
325*4882a593Smuzhiyun if (!buf) {
326*4882a593Smuzhiyun err = -ENOMEM;
327*4882a593Smuzhiyun goto error;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun if (next > buf->len) {
331*4882a593Smuzhiyun char *nbuf = kvzalloc(PAGE_ALIGN(next), GFP_KERNEL);
332*4882a593Smuzhiyun if (!nbuf) {
333*4882a593Smuzhiyun err = -ENOMEM;
334*4882a593Smuzhiyun goto error;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun kvfree(buf->buffer);
337*4882a593Smuzhiyun buf->buffer = nbuf;
338*4882a593Smuzhiyun buf->len = PAGE_ALIGN(next);
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun if (copy_from_user(buf->buffer + pos, buffer, count)) {
341*4882a593Smuzhiyun err = -EFAULT;
342*4882a593Smuzhiyun goto error;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun buf->size = next;
345*4882a593Smuzhiyun error:
346*4882a593Smuzhiyun mutex_unlock(&entry->access);
347*4882a593Smuzhiyun if (err < 0)
348*4882a593Smuzhiyun return err;
349*4882a593Smuzhiyun *offset = next;
350*4882a593Smuzhiyun return count;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
snd_info_seq_show(struct seq_file * seq,void * p)353*4882a593Smuzhiyun static int snd_info_seq_show(struct seq_file *seq, void *p)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun struct snd_info_private_data *data = seq->private;
356*4882a593Smuzhiyun struct snd_info_entry *entry = data->entry;
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun if (!entry->c.text.read) {
359*4882a593Smuzhiyun return -EIO;
360*4882a593Smuzhiyun } else {
361*4882a593Smuzhiyun data->rbuffer->buffer = (char *)seq; /* XXX hack! */
362*4882a593Smuzhiyun entry->c.text.read(entry, data->rbuffer);
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun return 0;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun
snd_info_text_entry_open(struct inode * inode,struct file * file)367*4882a593Smuzhiyun static int snd_info_text_entry_open(struct inode *inode, struct file *file)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun struct snd_info_entry *entry = PDE_DATA(inode);
370*4882a593Smuzhiyun struct snd_info_private_data *data;
371*4882a593Smuzhiyun int err;
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun mutex_lock(&info_mutex);
374*4882a593Smuzhiyun err = alloc_info_private(entry, &data);
375*4882a593Smuzhiyun if (err < 0)
376*4882a593Smuzhiyun goto unlock;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun data->rbuffer = kzalloc(sizeof(*data->rbuffer), GFP_KERNEL);
379*4882a593Smuzhiyun if (!data->rbuffer) {
380*4882a593Smuzhiyun err = -ENOMEM;
381*4882a593Smuzhiyun goto error;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun if (entry->size)
384*4882a593Smuzhiyun err = single_open_size(file, snd_info_seq_show, data,
385*4882a593Smuzhiyun entry->size);
386*4882a593Smuzhiyun else
387*4882a593Smuzhiyun err = single_open(file, snd_info_seq_show, data);
388*4882a593Smuzhiyun if (err < 0)
389*4882a593Smuzhiyun goto error;
390*4882a593Smuzhiyun mutex_unlock(&info_mutex);
391*4882a593Smuzhiyun return 0;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun error:
394*4882a593Smuzhiyun kfree(data->rbuffer);
395*4882a593Smuzhiyun kfree(data);
396*4882a593Smuzhiyun module_put(entry->module);
397*4882a593Smuzhiyun unlock:
398*4882a593Smuzhiyun mutex_unlock(&info_mutex);
399*4882a593Smuzhiyun return err;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
snd_info_text_entry_release(struct inode * inode,struct file * file)402*4882a593Smuzhiyun static int snd_info_text_entry_release(struct inode *inode, struct file *file)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun struct seq_file *m = file->private_data;
405*4882a593Smuzhiyun struct snd_info_private_data *data = m->private;
406*4882a593Smuzhiyun struct snd_info_entry *entry = data->entry;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun if (data->wbuffer && entry->c.text.write)
409*4882a593Smuzhiyun entry->c.text.write(entry, data->wbuffer);
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun single_release(inode, file);
412*4882a593Smuzhiyun kfree(data->rbuffer);
413*4882a593Smuzhiyun if (data->wbuffer) {
414*4882a593Smuzhiyun kvfree(data->wbuffer->buffer);
415*4882a593Smuzhiyun kfree(data->wbuffer);
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun
418*4882a593Smuzhiyun module_put(entry->module);
419*4882a593Smuzhiyun kfree(data);
420*4882a593Smuzhiyun return 0;
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun static const struct proc_ops snd_info_text_entry_ops =
424*4882a593Smuzhiyun {
425*4882a593Smuzhiyun .proc_open = snd_info_text_entry_open,
426*4882a593Smuzhiyun .proc_release = snd_info_text_entry_release,
427*4882a593Smuzhiyun .proc_write = snd_info_text_entry_write,
428*4882a593Smuzhiyun .proc_lseek = seq_lseek,
429*4882a593Smuzhiyun .proc_read = seq_read,
430*4882a593Smuzhiyun };
431*4882a593Smuzhiyun
create_subdir(struct module * mod,const char * name)432*4882a593Smuzhiyun static struct snd_info_entry *create_subdir(struct module *mod,
433*4882a593Smuzhiyun const char *name)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun struct snd_info_entry *entry;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun entry = snd_info_create_module_entry(mod, name, NULL);
438*4882a593Smuzhiyun if (!entry)
439*4882a593Smuzhiyun return NULL;
440*4882a593Smuzhiyun entry->mode = S_IFDIR | 0555;
441*4882a593Smuzhiyun if (snd_info_register(entry) < 0) {
442*4882a593Smuzhiyun snd_info_free_entry(entry);
443*4882a593Smuzhiyun return NULL;
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun return entry;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun static struct snd_info_entry *
449*4882a593Smuzhiyun snd_info_create_entry(const char *name, struct snd_info_entry *parent,
450*4882a593Smuzhiyun struct module *module);
451*4882a593Smuzhiyun
snd_info_init(void)452*4882a593Smuzhiyun int __init snd_info_init(void)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun snd_proc_root = snd_info_create_entry("asound", NULL, THIS_MODULE);
455*4882a593Smuzhiyun if (!snd_proc_root)
456*4882a593Smuzhiyun return -ENOMEM;
457*4882a593Smuzhiyun snd_proc_root->mode = S_IFDIR | 0555;
458*4882a593Smuzhiyun snd_proc_root->p = proc_mkdir("asound", NULL);
459*4882a593Smuzhiyun if (!snd_proc_root->p)
460*4882a593Smuzhiyun goto error;
461*4882a593Smuzhiyun #ifdef CONFIG_SND_OSSEMUL
462*4882a593Smuzhiyun snd_oss_root = create_subdir(THIS_MODULE, "oss");
463*4882a593Smuzhiyun if (!snd_oss_root)
464*4882a593Smuzhiyun goto error;
465*4882a593Smuzhiyun #endif
466*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_SEQUENCER)
467*4882a593Smuzhiyun snd_seq_root = create_subdir(THIS_MODULE, "seq");
468*4882a593Smuzhiyun if (!snd_seq_root)
469*4882a593Smuzhiyun goto error;
470*4882a593Smuzhiyun #endif
471*4882a593Smuzhiyun if (snd_info_version_init() < 0 ||
472*4882a593Smuzhiyun snd_minor_info_init() < 0 ||
473*4882a593Smuzhiyun snd_minor_info_oss_init() < 0 ||
474*4882a593Smuzhiyun snd_card_info_init() < 0 ||
475*4882a593Smuzhiyun snd_info_minor_register() < 0)
476*4882a593Smuzhiyun goto error;
477*4882a593Smuzhiyun return 0;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun error:
480*4882a593Smuzhiyun snd_info_free_entry(snd_proc_root);
481*4882a593Smuzhiyun return -ENOMEM;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun
snd_info_done(void)484*4882a593Smuzhiyun int __exit snd_info_done(void)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun snd_info_free_entry(snd_proc_root);
487*4882a593Smuzhiyun return 0;
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun
snd_card_id_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)490*4882a593Smuzhiyun static void snd_card_id_read(struct snd_info_entry *entry,
491*4882a593Smuzhiyun struct snd_info_buffer *buffer)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun struct snd_card *card = entry->private_data;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun snd_iprintf(buffer, "%s\n", card->id);
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun /*
499*4882a593Smuzhiyun * create a card proc file
500*4882a593Smuzhiyun * called from init.c
501*4882a593Smuzhiyun */
snd_info_card_create(struct snd_card * card)502*4882a593Smuzhiyun int snd_info_card_create(struct snd_card *card)
503*4882a593Smuzhiyun {
504*4882a593Smuzhiyun char str[8];
505*4882a593Smuzhiyun struct snd_info_entry *entry;
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun if (snd_BUG_ON(!card))
508*4882a593Smuzhiyun return -ENXIO;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun sprintf(str, "card%i", card->number);
511*4882a593Smuzhiyun entry = create_subdir(card->module, str);
512*4882a593Smuzhiyun if (!entry)
513*4882a593Smuzhiyun return -ENOMEM;
514*4882a593Smuzhiyun card->proc_root = entry;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun return snd_card_ro_proc_new(card, "id", card, snd_card_id_read);
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun /*
520*4882a593Smuzhiyun * register the card proc file
521*4882a593Smuzhiyun * called from init.c
522*4882a593Smuzhiyun * can be called multiple times for reinitialization
523*4882a593Smuzhiyun */
snd_info_card_register(struct snd_card * card)524*4882a593Smuzhiyun int snd_info_card_register(struct snd_card *card)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun struct proc_dir_entry *p;
527*4882a593Smuzhiyun int err;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun if (snd_BUG_ON(!card))
530*4882a593Smuzhiyun return -ENXIO;
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun err = snd_info_register(card->proc_root);
533*4882a593Smuzhiyun if (err < 0)
534*4882a593Smuzhiyun return err;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun if (!strcmp(card->id, card->proc_root->name))
537*4882a593Smuzhiyun return 0;
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun if (card->proc_root_link)
540*4882a593Smuzhiyun return 0;
541*4882a593Smuzhiyun p = proc_symlink(card->id, snd_proc_root->p, card->proc_root->name);
542*4882a593Smuzhiyun if (!p)
543*4882a593Smuzhiyun return -ENOMEM;
544*4882a593Smuzhiyun card->proc_root_link = p;
545*4882a593Smuzhiyun return 0;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun /*
549*4882a593Smuzhiyun * called on card->id change
550*4882a593Smuzhiyun */
snd_info_card_id_change(struct snd_card * card)551*4882a593Smuzhiyun void snd_info_card_id_change(struct snd_card *card)
552*4882a593Smuzhiyun {
553*4882a593Smuzhiyun mutex_lock(&info_mutex);
554*4882a593Smuzhiyun if (card->proc_root_link) {
555*4882a593Smuzhiyun proc_remove(card->proc_root_link);
556*4882a593Smuzhiyun card->proc_root_link = NULL;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun if (strcmp(card->id, card->proc_root->name))
559*4882a593Smuzhiyun card->proc_root_link = proc_symlink(card->id,
560*4882a593Smuzhiyun snd_proc_root->p,
561*4882a593Smuzhiyun card->proc_root->name);
562*4882a593Smuzhiyun mutex_unlock(&info_mutex);
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun /*
566*4882a593Smuzhiyun * de-register the card proc file
567*4882a593Smuzhiyun * called from init.c
568*4882a593Smuzhiyun */
snd_info_card_disconnect(struct snd_card * card)569*4882a593Smuzhiyun void snd_info_card_disconnect(struct snd_card *card)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun if (!card)
572*4882a593Smuzhiyun return;
573*4882a593Smuzhiyun mutex_lock(&info_mutex);
574*4882a593Smuzhiyun proc_remove(card->proc_root_link);
575*4882a593Smuzhiyun card->proc_root_link = NULL;
576*4882a593Smuzhiyun if (card->proc_root)
577*4882a593Smuzhiyun snd_info_disconnect(card->proc_root);
578*4882a593Smuzhiyun mutex_unlock(&info_mutex);
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /*
582*4882a593Smuzhiyun * release the card proc file resources
583*4882a593Smuzhiyun * called from init.c
584*4882a593Smuzhiyun */
snd_info_card_free(struct snd_card * card)585*4882a593Smuzhiyun int snd_info_card_free(struct snd_card *card)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun if (!card)
588*4882a593Smuzhiyun return 0;
589*4882a593Smuzhiyun snd_info_free_entry(card->proc_root);
590*4882a593Smuzhiyun card->proc_root = NULL;
591*4882a593Smuzhiyun return 0;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun /**
596*4882a593Smuzhiyun * snd_info_get_line - read one line from the procfs buffer
597*4882a593Smuzhiyun * @buffer: the procfs buffer
598*4882a593Smuzhiyun * @line: the buffer to store
599*4882a593Smuzhiyun * @len: the max. buffer size
600*4882a593Smuzhiyun *
601*4882a593Smuzhiyun * Reads one line from the buffer and stores the string.
602*4882a593Smuzhiyun *
603*4882a593Smuzhiyun * Return: Zero if successful, or 1 if error or EOF.
604*4882a593Smuzhiyun */
snd_info_get_line(struct snd_info_buffer * buffer,char * line,int len)605*4882a593Smuzhiyun int snd_info_get_line(struct snd_info_buffer *buffer, char *line, int len)
606*4882a593Smuzhiyun {
607*4882a593Smuzhiyun int c;
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun if (snd_BUG_ON(!buffer))
610*4882a593Smuzhiyun return 1;
611*4882a593Smuzhiyun if (!buffer->buffer)
612*4882a593Smuzhiyun return 1;
613*4882a593Smuzhiyun if (len <= 0 || buffer->stop || buffer->error)
614*4882a593Smuzhiyun return 1;
615*4882a593Smuzhiyun while (!buffer->stop) {
616*4882a593Smuzhiyun c = buffer->buffer[buffer->curr++];
617*4882a593Smuzhiyun if (buffer->curr >= buffer->size)
618*4882a593Smuzhiyun buffer->stop = 1;
619*4882a593Smuzhiyun if (c == '\n')
620*4882a593Smuzhiyun break;
621*4882a593Smuzhiyun if (len > 1) {
622*4882a593Smuzhiyun len--;
623*4882a593Smuzhiyun *line++ = c;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun *line = '\0';
627*4882a593Smuzhiyun return 0;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun EXPORT_SYMBOL(snd_info_get_line);
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun /**
632*4882a593Smuzhiyun * snd_info_get_str - parse a string token
633*4882a593Smuzhiyun * @dest: the buffer to store the string token
634*4882a593Smuzhiyun * @src: the original string
635*4882a593Smuzhiyun * @len: the max. length of token - 1
636*4882a593Smuzhiyun *
637*4882a593Smuzhiyun * Parses the original string and copy a token to the given
638*4882a593Smuzhiyun * string buffer.
639*4882a593Smuzhiyun *
640*4882a593Smuzhiyun * Return: The updated pointer of the original string so that
641*4882a593Smuzhiyun * it can be used for the next call.
642*4882a593Smuzhiyun */
snd_info_get_str(char * dest,const char * src,int len)643*4882a593Smuzhiyun const char *snd_info_get_str(char *dest, const char *src, int len)
644*4882a593Smuzhiyun {
645*4882a593Smuzhiyun int c;
646*4882a593Smuzhiyun
647*4882a593Smuzhiyun while (*src == ' ' || *src == '\t')
648*4882a593Smuzhiyun src++;
649*4882a593Smuzhiyun if (*src == '"' || *src == '\'') {
650*4882a593Smuzhiyun c = *src++;
651*4882a593Smuzhiyun while (--len > 0 && *src && *src != c) {
652*4882a593Smuzhiyun *dest++ = *src++;
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun if (*src == c)
655*4882a593Smuzhiyun src++;
656*4882a593Smuzhiyun } else {
657*4882a593Smuzhiyun while (--len > 0 && *src && *src != ' ' && *src != '\t') {
658*4882a593Smuzhiyun *dest++ = *src++;
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun *dest = 0;
662*4882a593Smuzhiyun while (*src == ' ' || *src == '\t')
663*4882a593Smuzhiyun src++;
664*4882a593Smuzhiyun return src;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun EXPORT_SYMBOL(snd_info_get_str);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun /*
669*4882a593Smuzhiyun * snd_info_create_entry - create an info entry
670*4882a593Smuzhiyun * @name: the proc file name
671*4882a593Smuzhiyun * @parent: the parent directory
672*4882a593Smuzhiyun *
673*4882a593Smuzhiyun * Creates an info entry with the given file name and initializes as
674*4882a593Smuzhiyun * the default state.
675*4882a593Smuzhiyun *
676*4882a593Smuzhiyun * Usually called from other functions such as
677*4882a593Smuzhiyun * snd_info_create_card_entry().
678*4882a593Smuzhiyun *
679*4882a593Smuzhiyun * Return: The pointer of the new instance, or %NULL on failure.
680*4882a593Smuzhiyun */
681*4882a593Smuzhiyun static struct snd_info_entry *
snd_info_create_entry(const char * name,struct snd_info_entry * parent,struct module * module)682*4882a593Smuzhiyun snd_info_create_entry(const char *name, struct snd_info_entry *parent,
683*4882a593Smuzhiyun struct module *module)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun struct snd_info_entry *entry;
686*4882a593Smuzhiyun entry = kzalloc(sizeof(*entry), GFP_KERNEL);
687*4882a593Smuzhiyun if (entry == NULL)
688*4882a593Smuzhiyun return NULL;
689*4882a593Smuzhiyun entry->name = kstrdup(name, GFP_KERNEL);
690*4882a593Smuzhiyun if (entry->name == NULL) {
691*4882a593Smuzhiyun kfree(entry);
692*4882a593Smuzhiyun return NULL;
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun entry->mode = S_IFREG | 0444;
695*4882a593Smuzhiyun entry->content = SNDRV_INFO_CONTENT_TEXT;
696*4882a593Smuzhiyun mutex_init(&entry->access);
697*4882a593Smuzhiyun INIT_LIST_HEAD(&entry->children);
698*4882a593Smuzhiyun INIT_LIST_HEAD(&entry->list);
699*4882a593Smuzhiyun entry->parent = parent;
700*4882a593Smuzhiyun entry->module = module;
701*4882a593Smuzhiyun if (parent) {
702*4882a593Smuzhiyun mutex_lock(&parent->access);
703*4882a593Smuzhiyun list_add_tail(&entry->list, &parent->children);
704*4882a593Smuzhiyun mutex_unlock(&parent->access);
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun return entry;
707*4882a593Smuzhiyun }
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun /**
710*4882a593Smuzhiyun * snd_info_create_module_entry - create an info entry for the given module
711*4882a593Smuzhiyun * @module: the module pointer
712*4882a593Smuzhiyun * @name: the file name
713*4882a593Smuzhiyun * @parent: the parent directory
714*4882a593Smuzhiyun *
715*4882a593Smuzhiyun * Creates a new info entry and assigns it to the given module.
716*4882a593Smuzhiyun *
717*4882a593Smuzhiyun * Return: The pointer of the new instance, or %NULL on failure.
718*4882a593Smuzhiyun */
snd_info_create_module_entry(struct module * module,const char * name,struct snd_info_entry * parent)719*4882a593Smuzhiyun struct snd_info_entry *snd_info_create_module_entry(struct module * module,
720*4882a593Smuzhiyun const char *name,
721*4882a593Smuzhiyun struct snd_info_entry *parent)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun if (!parent)
724*4882a593Smuzhiyun parent = snd_proc_root;
725*4882a593Smuzhiyun return snd_info_create_entry(name, parent, module);
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun EXPORT_SYMBOL(snd_info_create_module_entry);
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun /**
730*4882a593Smuzhiyun * snd_info_create_card_entry - create an info entry for the given card
731*4882a593Smuzhiyun * @card: the card instance
732*4882a593Smuzhiyun * @name: the file name
733*4882a593Smuzhiyun * @parent: the parent directory
734*4882a593Smuzhiyun *
735*4882a593Smuzhiyun * Creates a new info entry and assigns it to the given card.
736*4882a593Smuzhiyun *
737*4882a593Smuzhiyun * Return: The pointer of the new instance, or %NULL on failure.
738*4882a593Smuzhiyun */
snd_info_create_card_entry(struct snd_card * card,const char * name,struct snd_info_entry * parent)739*4882a593Smuzhiyun struct snd_info_entry *snd_info_create_card_entry(struct snd_card *card,
740*4882a593Smuzhiyun const char *name,
741*4882a593Smuzhiyun struct snd_info_entry * parent)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun if (!parent)
744*4882a593Smuzhiyun parent = card->proc_root;
745*4882a593Smuzhiyun return snd_info_create_entry(name, parent, card->module);
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun EXPORT_SYMBOL(snd_info_create_card_entry);
748*4882a593Smuzhiyun
snd_info_disconnect(struct snd_info_entry * entry)749*4882a593Smuzhiyun static void snd_info_disconnect(struct snd_info_entry *entry)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun struct snd_info_entry *p;
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun if (!entry->p)
754*4882a593Smuzhiyun return;
755*4882a593Smuzhiyun list_for_each_entry(p, &entry->children, list)
756*4882a593Smuzhiyun snd_info_disconnect(p);
757*4882a593Smuzhiyun proc_remove(entry->p);
758*4882a593Smuzhiyun entry->p = NULL;
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun /**
762*4882a593Smuzhiyun * snd_info_free_entry - release the info entry
763*4882a593Smuzhiyun * @entry: the info entry
764*4882a593Smuzhiyun *
765*4882a593Smuzhiyun * Releases the info entry.
766*4882a593Smuzhiyun */
snd_info_free_entry(struct snd_info_entry * entry)767*4882a593Smuzhiyun void snd_info_free_entry(struct snd_info_entry * entry)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun struct snd_info_entry *p, *n;
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun if (!entry)
772*4882a593Smuzhiyun return;
773*4882a593Smuzhiyun if (entry->p) {
774*4882a593Smuzhiyun mutex_lock(&info_mutex);
775*4882a593Smuzhiyun snd_info_disconnect(entry);
776*4882a593Smuzhiyun mutex_unlock(&info_mutex);
777*4882a593Smuzhiyun }
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun /* free all children at first */
780*4882a593Smuzhiyun list_for_each_entry_safe(p, n, &entry->children, list)
781*4882a593Smuzhiyun snd_info_free_entry(p);
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun p = entry->parent;
784*4882a593Smuzhiyun if (p) {
785*4882a593Smuzhiyun mutex_lock(&p->access);
786*4882a593Smuzhiyun list_del(&entry->list);
787*4882a593Smuzhiyun mutex_unlock(&p->access);
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun kfree(entry->name);
790*4882a593Smuzhiyun if (entry->private_free)
791*4882a593Smuzhiyun entry->private_free(entry);
792*4882a593Smuzhiyun kfree(entry);
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun EXPORT_SYMBOL(snd_info_free_entry);
795*4882a593Smuzhiyun
__snd_info_register(struct snd_info_entry * entry)796*4882a593Smuzhiyun static int __snd_info_register(struct snd_info_entry *entry)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun struct proc_dir_entry *root, *p = NULL;
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun if (snd_BUG_ON(!entry))
801*4882a593Smuzhiyun return -ENXIO;
802*4882a593Smuzhiyun root = entry->parent == NULL ? snd_proc_root->p : entry->parent->p;
803*4882a593Smuzhiyun mutex_lock(&info_mutex);
804*4882a593Smuzhiyun if (entry->p || !root)
805*4882a593Smuzhiyun goto unlock;
806*4882a593Smuzhiyun if (S_ISDIR(entry->mode)) {
807*4882a593Smuzhiyun p = proc_mkdir_mode(entry->name, entry->mode, root);
808*4882a593Smuzhiyun if (!p) {
809*4882a593Smuzhiyun mutex_unlock(&info_mutex);
810*4882a593Smuzhiyun return -ENOMEM;
811*4882a593Smuzhiyun }
812*4882a593Smuzhiyun } else {
813*4882a593Smuzhiyun const struct proc_ops *ops;
814*4882a593Smuzhiyun if (entry->content == SNDRV_INFO_CONTENT_DATA)
815*4882a593Smuzhiyun ops = &snd_info_entry_operations;
816*4882a593Smuzhiyun else
817*4882a593Smuzhiyun ops = &snd_info_text_entry_ops;
818*4882a593Smuzhiyun p = proc_create_data(entry->name, entry->mode, root,
819*4882a593Smuzhiyun ops, entry);
820*4882a593Smuzhiyun if (!p) {
821*4882a593Smuzhiyun mutex_unlock(&info_mutex);
822*4882a593Smuzhiyun return -ENOMEM;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun proc_set_size(p, entry->size);
825*4882a593Smuzhiyun }
826*4882a593Smuzhiyun entry->p = p;
827*4882a593Smuzhiyun unlock:
828*4882a593Smuzhiyun mutex_unlock(&info_mutex);
829*4882a593Smuzhiyun return 0;
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun
832*4882a593Smuzhiyun /**
833*4882a593Smuzhiyun * snd_info_register - register the info entry
834*4882a593Smuzhiyun * @entry: the info entry
835*4882a593Smuzhiyun *
836*4882a593Smuzhiyun * Registers the proc info entry.
837*4882a593Smuzhiyun * The all children entries are registered recursively.
838*4882a593Smuzhiyun *
839*4882a593Smuzhiyun * Return: Zero if successful, or a negative error code on failure.
840*4882a593Smuzhiyun */
snd_info_register(struct snd_info_entry * entry)841*4882a593Smuzhiyun int snd_info_register(struct snd_info_entry *entry)
842*4882a593Smuzhiyun {
843*4882a593Smuzhiyun struct snd_info_entry *p;
844*4882a593Smuzhiyun int err;
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun if (!entry->p) {
847*4882a593Smuzhiyun err = __snd_info_register(entry);
848*4882a593Smuzhiyun if (err < 0)
849*4882a593Smuzhiyun return err;
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun list_for_each_entry(p, &entry->children, list) {
853*4882a593Smuzhiyun err = snd_info_register(p);
854*4882a593Smuzhiyun if (err < 0)
855*4882a593Smuzhiyun return err;
856*4882a593Smuzhiyun }
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun return 0;
859*4882a593Smuzhiyun }
860*4882a593Smuzhiyun EXPORT_SYMBOL(snd_info_register);
861*4882a593Smuzhiyun
862*4882a593Smuzhiyun /**
863*4882a593Smuzhiyun * snd_card_rw_proc_new - Create a read/write text proc file entry for the card
864*4882a593Smuzhiyun * @card: the card instance
865*4882a593Smuzhiyun * @name: the file name
866*4882a593Smuzhiyun * @private_data: the arbitrary private data
867*4882a593Smuzhiyun * @read: the read callback
868*4882a593Smuzhiyun * @write: the write callback, NULL for read-only
869*4882a593Smuzhiyun *
870*4882a593Smuzhiyun * This proc file entry will be registered via snd_card_register() call, and
871*4882a593Smuzhiyun * it will be removed automatically at the card removal, too.
872*4882a593Smuzhiyun */
snd_card_rw_proc_new(struct snd_card * card,const char * name,void * private_data,void (* read)(struct snd_info_entry *,struct snd_info_buffer *),void (* write)(struct snd_info_entry * entry,struct snd_info_buffer * buffer))873*4882a593Smuzhiyun int snd_card_rw_proc_new(struct snd_card *card, const char *name,
874*4882a593Smuzhiyun void *private_data,
875*4882a593Smuzhiyun void (*read)(struct snd_info_entry *,
876*4882a593Smuzhiyun struct snd_info_buffer *),
877*4882a593Smuzhiyun void (*write)(struct snd_info_entry *entry,
878*4882a593Smuzhiyun struct snd_info_buffer *buffer))
879*4882a593Smuzhiyun {
880*4882a593Smuzhiyun struct snd_info_entry *entry;
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun entry = snd_info_create_card_entry(card, name, card->proc_root);
883*4882a593Smuzhiyun if (!entry)
884*4882a593Smuzhiyun return -ENOMEM;
885*4882a593Smuzhiyun snd_info_set_text_ops(entry, private_data, read);
886*4882a593Smuzhiyun if (write) {
887*4882a593Smuzhiyun entry->mode |= 0200;
888*4882a593Smuzhiyun entry->c.text.write = write;
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun return 0;
891*4882a593Smuzhiyun }
892*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(snd_card_rw_proc_new);
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun /*
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun */
897*4882a593Smuzhiyun
snd_info_version_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)898*4882a593Smuzhiyun static void snd_info_version_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun snd_iprintf(buffer,
901*4882a593Smuzhiyun "Advanced Linux Sound Architecture Driver Version k%s.\n",
902*4882a593Smuzhiyun init_utsname()->release);
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun
snd_info_version_init(void)905*4882a593Smuzhiyun static int __init snd_info_version_init(void)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun struct snd_info_entry *entry;
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun entry = snd_info_create_module_entry(THIS_MODULE, "version", NULL);
910*4882a593Smuzhiyun if (entry == NULL)
911*4882a593Smuzhiyun return -ENOMEM;
912*4882a593Smuzhiyun entry->c.text.read = snd_info_version_read;
913*4882a593Smuzhiyun return snd_info_register(entry); /* freed in error path */
914*4882a593Smuzhiyun }
915