Blame view

sound/core/hwdep.c 12.6 KB
1a59d1b8e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
  /*
   *  Hardware dependent layer
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
4
   *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
  #include <linux/major.h>
  #include <linux/init.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
  #include <linux/slab.h>
  #include <linux/time.h>
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
10
  #include <linux/mutex.h>
da155d5b4   Paul Gortmaker   sound: Add module...
11
  #include <linux/module.h>
174cd4b1e   Ingo Molnar   sched/headers: Pr...
12
  #include <linux/sched/signal.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
17
  #include <sound/core.h>
  #include <sound/control.h>
  #include <sound/minors.h>
  #include <sound/hwdep.h>
  #include <sound/info.h>
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
18
  MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
  MODULE_DESCRIPTION("Hardware dependent layer");
  MODULE_LICENSE("GPL");
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
21
  static LIST_HEAD(snd_hwdep_devices);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
22
  static DEFINE_MUTEX(register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23

d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
24
25
  static int snd_hwdep_dev_free(struct snd_device *device);
  static int snd_hwdep_dev_register(struct snd_device *device);
c461482c8   Takashi Iwai   [ALSA] Unregister...
26
  static int snd_hwdep_dev_disconnect(struct snd_device *device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28

f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
29
30
  static struct snd_hwdep *snd_hwdep_search(struct snd_card *card, int device)
  {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
31
  	struct snd_hwdep *hwdep;
9244b2c30   Johannes Berg   [ALSA] alsa core:...
32
  	list_for_each_entry(hwdep, &snd_hwdep_devices, list)
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
33
34
  		if (hwdep->card == card && hwdep->device == device)
  			return hwdep;
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
35
36
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
  
  static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
40
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
  	if (hw->ops.llseek)
  		return hw->ops.llseek(hw, file, offset, orig);
  	return -ENXIO;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
45
46
  static ssize_t snd_hwdep_read(struct file * file, char __user *buf,
  			      size_t count, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
48
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
  	if (hw->ops.read)
  		return hw->ops.read(hw, buf, count, offset);
  	return -ENXIO;	
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
53
54
  static ssize_t snd_hwdep_write(struct file * file, const char __user *buf,
  			       size_t count, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
56
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
59
60
61
62
63
64
  	if (hw->ops.write)
  		return hw->ops.write(hw, buf, count, offset);
  	return -ENXIO;	
  }
  
  static int snd_hwdep_open(struct inode *inode, struct file * file)
  {
  	int major = imajor(inode);
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
65
  	struct snd_hwdep *hw;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
66
  	int err;
ac6424b98   Ingo Molnar   sched/wait: Renam...
67
  	wait_queue_entry_t wait;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68

f19028601   Clemens Ladisch   [ALSA] fix improp...
69
  	if (major == snd_major) {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
70
71
  		hw = snd_lookup_minor_data(iminor(inode),
  					   SNDRV_DEVICE_TYPE_HWDEP);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
  #ifdef CONFIG_SND_OSSEMUL
f19028601   Clemens Ladisch   [ALSA] fix improp...
73
  	} else if (major == SOUND_MAJOR) {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
74
75
  		hw = snd_lookup_oss_minor_data(iminor(inode),
  					       SNDRV_OSS_DEVICE_TYPE_DMFM);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
  #endif
f19028601   Clemens Ladisch   [ALSA] fix improp...
77
  	} else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
  		return -ENXIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
80
  	if (hw == NULL)
  		return -ENODEV;
a0830dbd4   Takashi Iwai   ALSA: Add a refer...
81
82
  	if (!try_module_get(hw->card->module)) {
  		snd_card_unref(hw->card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
  		return -EFAULT;
a0830dbd4   Takashi Iwai   ALSA: Add a refer...
84
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
87
  
  	init_waitqueue_entry(&wait, current);
  	add_wait_queue(&hw->open_wait, &wait);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
88
  	mutex_lock(&hw->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
93
  	while (1) {
  		if (hw->exclusive && hw->used > 0) {
  			err = -EBUSY;
  			break;
  		}
345d0b196   Takashi Iwai   ALSA: hwdep - Mak...
94
95
96
97
  		if (!hw->ops.open) {
  			err = 0;
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
101
102
103
104
105
106
107
108
  		err = hw->ops.open(hw, file);
  		if (err >= 0)
  			break;
  		if (err == -EAGAIN) {
  			if (file->f_flags & O_NONBLOCK) {
  				err = -EBUSY;
  				break;
  			}
  		} else
  			break;
  		set_current_state(TASK_INTERRUPTIBLE);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
109
  		mutex_unlock(&hw->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
  		schedule();
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
111
  		mutex_lock(&hw->open_mutex);
0914f7961   Takashi Iwai   ALSA: Avoid endle...
112
113
114
115
  		if (hw->card->shutdown) {
  			err = -ENODEV;
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  		if (signal_pending(current)) {
  			err = -ERESTARTSYS;
  			break;
  		}
  	}
  	remove_wait_queue(&hw->open_wait, &wait);
  	if (err >= 0) {
  		err = snd_card_file_add(hw->card, file);
  		if (err >= 0) {
  			file->private_data = hw;
  			hw->used++;
  		} else {
  			if (hw->ops.release)
  				hw->ops.release(hw, file);
  		}
  	}
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
132
  	mutex_unlock(&hw->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
  	if (err < 0)
  		module_put(hw->card->module);
a0830dbd4   Takashi Iwai   ALSA: Add a refer...
135
  	snd_card_unref(hw->card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
139
140
  	return err;
  }
  
  static int snd_hwdep_release(struct inode *inode, struct file * file)
  {
345d0b196   Takashi Iwai   ALSA: hwdep - Mak...
141
  	int err = 0;
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
142
  	struct snd_hwdep *hw = file->private_data;
104326f8d   Florin Malita   [ALSA] Dereferenc...
143
  	struct module *mod = hw->card->module;
8fa58af7d   Karsten Wiese   [ALSA] snd_hwdep_...
144

1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
145
  	mutex_lock(&hw->open_mutex);
8fa58af7d   Karsten Wiese   [ALSA] snd_hwdep_...
146
  	if (hw->ops.release)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
  		err = hw->ops.release(hw, file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
  	if (hw->used > 0)
  		hw->used--;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
150
  	mutex_unlock(&hw->open_mutex);
8fa58af7d   Karsten Wiese   [ALSA] snd_hwdep_...
151
152
153
  	wake_up(&hw->open_wait);
  
  	snd_card_file_remove(hw->card, file);
104326f8d   Florin Malita   [ALSA] Dereferenc...
154
  	module_put(mod);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
  	return err;
  }
680ef72ab   Al Viro   sound: annotate -...
157
  static __poll_t snd_hwdep_poll(struct file * file, poll_table * wait)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
159
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
162
163
  	if (hw->ops.poll)
  		return hw->ops.poll(hw, file, wait);
  	return 0;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
164
165
  static int snd_hwdep_info(struct snd_hwdep *hw,
  			  struct snd_hwdep_info __user *_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
167
  	struct snd_hwdep_info info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
170
171
172
173
174
175
176
177
  	
  	memset(&info, 0, sizeof(info));
  	info.card = hw->card->number;
  	strlcpy(info.id, hw->id, sizeof(info.id));	
  	strlcpy(info.name, hw->name, sizeof(info.name));
  	info.iface = hw->iface;
  	if (copy_to_user(_info, &info, sizeof(info)))
  		return -EFAULT;
  	return 0;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
178
179
  static int snd_hwdep_dsp_status(struct snd_hwdep *hw,
  				struct snd_hwdep_dsp_status __user *_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
181
  	struct snd_hwdep_dsp_status info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
186
187
188
189
190
191
192
193
  	int err;
  	
  	if (! hw->ops.dsp_status)
  		return -ENXIO;
  	memset(&info, 0, sizeof(info));
  	info.dsp_loaded = hw->dsp_loaded;
  	if ((err = hw->ops.dsp_status(hw, &info)) < 0)
  		return err;
  	if (copy_to_user(_info, &info, sizeof(info)))
  		return -EFAULT;
  	return 0;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
194
  static int snd_hwdep_dsp_load(struct snd_hwdep *hw,
18d122c02   Arnd Bergmann   ALSA: compat_ioct...
195
  			      struct snd_hwdep_dsp_image *info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
197
198
199
200
  	int err;
  	
  	if (! hw->ops.dsp_load)
  		return -ENXIO;
18d122c02   Arnd Bergmann   ALSA: compat_ioct...
201
  	if (info->index >= 32)
c4fd43793   Dan Carpenter   ALSA: hwdep: prev...
202
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
  	/* check whether the dsp was already loaded */
18d122c02   Arnd Bergmann   ALSA: compat_ioct...
204
  	if (hw->dsp_loaded & (1u << info->index))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
  		return -EBUSY;
18d122c02   Arnd Bergmann   ALSA: compat_ioct...
206
  	err = hw->ops.dsp_load(hw, info);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
  	if (err < 0)
  		return err;
18d122c02   Arnd Bergmann   ALSA: compat_ioct...
209
  	hw->dsp_loaded |= (1u << info->index);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
  	return 0;
  }
18d122c02   Arnd Bergmann   ALSA: compat_ioct...
212
213
214
215
216
217
218
219
220
  static int snd_hwdep_dsp_load_user(struct snd_hwdep *hw,
  				   struct snd_hwdep_dsp_image __user *_info)
  {
  	struct snd_hwdep_dsp_image info = {};
  
  	if (copy_from_user(&info, _info, sizeof(info)))
  		return -EFAULT;
  	return snd_hwdep_dsp_load(hw, &info);
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
221
222
  static long snd_hwdep_ioctl(struct file * file, unsigned int cmd,
  			    unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
224
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
226
227
228
229
230
231
232
233
  	void __user *argp = (void __user *)arg;
  	switch (cmd) {
  	case SNDRV_HWDEP_IOCTL_PVERSION:
  		return put_user(SNDRV_HWDEP_VERSION, (int __user *)argp);
  	case SNDRV_HWDEP_IOCTL_INFO:
  		return snd_hwdep_info(hw, argp);
  	case SNDRV_HWDEP_IOCTL_DSP_STATUS:
  		return snd_hwdep_dsp_status(hw, argp);
  	case SNDRV_HWDEP_IOCTL_DSP_LOAD:
18d122c02   Arnd Bergmann   ALSA: compat_ioct...
234
  		return snd_hwdep_dsp_load_user(hw, argp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
235
236
237
238
239
240
241
242
  	}
  	if (hw->ops.ioctl)
  		return hw->ops.ioctl(hw, file, cmd, arg);
  	return -ENOTTY;
  }
  
  static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
243
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
247
  	if (hw->ops.mmap)
  		return hw->ops.mmap(hw, file, vma);
  	return -ENXIO;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
248
249
  static int snd_hwdep_control_ioctl(struct snd_card *card,
  				   struct snd_ctl_file * control,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
251
  				   unsigned int cmd, unsigned long arg)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
253
254
255
256
257
258
  	switch (cmd) {
  	case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
  		{
  			int device;
  
  			if (get_user(device, (int __user *)arg))
  				return -EFAULT;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
259
  			mutex_lock(&register_mutex);
f7b2bb854   Dan Carpenter   ALSA: hwdep: sile...
260
261
262
263
264
265
266
  
  			if (device < 0)
  				device = 0;
  			else if (device < SNDRV_MINOR_HWDEPS)
  				device++;
  			else
  				device = SNDRV_MINOR_HWDEPS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
  			while (device < SNDRV_MINOR_HWDEPS) {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
268
  				if (snd_hwdep_search(card, device))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
271
272
273
  					break;
  				device++;
  			}
  			if (device >= SNDRV_MINOR_HWDEPS)
  				device = -1;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
274
  			mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275
276
277
278
279
280
  			if (put_user(device, (int __user *)arg))
  				return -EFAULT;
  			return 0;
  		}
  	case SNDRV_CTL_IOCTL_HWDEP_INFO:
  		{
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
281
  			struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
282
  			int device, err;
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
283
  			struct snd_hwdep *hwdep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
285
286
  
  			if (get_user(device, &info->device))
  				return -EFAULT;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
287
  			mutex_lock(&register_mutex);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
288
289
290
291
292
  			hwdep = snd_hwdep_search(card, device);
  			if (hwdep)
  				err = snd_hwdep_info(hwdep, info);
  			else
  				err = -ENXIO;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
293
  			mutex_unlock(&register_mutex);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
294
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
296
297
298
299
300
301
302
303
304
305
306
307
308
  		}
  	}
  	return -ENOIOCTLCMD;
  }
  
  #ifdef CONFIG_COMPAT
  #include "hwdep_compat.c"
  #else
  #define snd_hwdep_ioctl_compat	NULL
  #endif
  
  /*
  
   */
9c2e08c59   Arjan van de Ven   [PATCH] mark stru...
309
  static const struct file_operations snd_hwdep_f_ops =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
310
311
312
313
314
315
316
317
318
319
320
321
  {
  	.owner = 	THIS_MODULE,
  	.llseek =	snd_hwdep_llseek,
  	.read = 	snd_hwdep_read,
  	.write =	snd_hwdep_write,
  	.open =		snd_hwdep_open,
  	.release =	snd_hwdep_release,
  	.poll =		snd_hwdep_poll,
  	.unlocked_ioctl =	snd_hwdep_ioctl,
  	.compat_ioctl =	snd_hwdep_ioctl_compat,
  	.mmap =		snd_hwdep_mmap,
  };
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
322
323
324
325
  static void release_hwdep_device(struct device *dev)
  {
  	kfree(container_of(dev, struct snd_hwdep, dev));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
327
328
329
330
331
332
333
334
335
336
  /**
   * snd_hwdep_new - create a new hwdep instance
   * @card: the card instance
   * @id: the id string
   * @device: the device index (zero-based)
   * @rhwdep: the pointer to store the new hwdep instance
   *
   * Creates a new hwdep instance with the given index on the card.
   * The callbacks (hwdep->ops) must be set on the returned instance
   * after this call manually by the caller.
   *
eb7c06e8e   Yacine Belkadi   ALSA: add/change ...
337
   * Return: Zero if successful, or a negative error code on failure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
   */
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
339
340
  int snd_hwdep_new(struct snd_card *card, char *id, int device,
  		  struct snd_hwdep **rhwdep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
341
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
342
  	struct snd_hwdep *hwdep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
  	int err;
f15ee210c   Takashi Iwai   ALSA: core: Const...
344
  	static const struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
346
  		.dev_free = snd_hwdep_dev_free,
  		.dev_register = snd_hwdep_dev_register,
c461482c8   Takashi Iwai   [ALSA] Unregister...
347
  		.dev_disconnect = snd_hwdep_dev_disconnect,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
  	};
7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
349
350
351
352
  	if (snd_BUG_ON(!card))
  		return -ENXIO;
  	if (rhwdep)
  		*rhwdep = NULL;
ca2c09665   Takashi Iwai   [ALSA] Replace wi...
353
  	hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
ec0e9937a   Takashi Iwai   ALSA: core: Drop ...
354
  	if (!hwdep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
  		return -ENOMEM;
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
356
357
358
  
  	init_waitqueue_head(&hwdep->open_wait);
  	mutex_init(&hwdep->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
  	hwdep->card = card;
  	hwdep->device = device;
73e77ba02   Takashi Iwai   [ALSA] Add error ...
361
  	if (id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
362
  		strlcpy(hwdep->id, id, sizeof(hwdep->id));
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
363
364
365
366
  
  	snd_device_initialize(&hwdep->dev, card);
  	hwdep->dev.release = release_hwdep_device;
  	dev_set_name(&hwdep->dev, "hwC%iD%i", card->number, device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
368
369
  #ifdef CONFIG_SND_OSSEMUL
  	hwdep->oss_type = -1;
  #endif
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
370
371
372
373
  
  	err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops);
  	if (err < 0) {
  		put_device(&hwdep->dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374
375
  		return err;
  	}
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
376

7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
377
378
  	if (rhwdep)
  		*rhwdep = hwdep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
380
  	return 0;
  }
6776a5d71   Takashi Iwai   ALSA: Move EXPORT...
381
  EXPORT_SYMBOL(snd_hwdep_new);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
382

7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
383
  static int snd_hwdep_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
  {
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
385
  	struct snd_hwdep *hwdep = device->device_data;
7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
386
387
  	if (!hwdep)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
  	if (hwdep->private_free)
  		hwdep->private_free(hwdep);
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
390
  	put_device(&hwdep->dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
  	return 0;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
393
  static int snd_hwdep_dev_register(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
395
  	struct snd_hwdep *hwdep = device->device_data;
2ebef69fc   Takashi Iwai   ALSA: hwdep: Use ...
396
  	struct snd_card *card = hwdep->card;
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
397
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
398

1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
399
  	mutex_lock(&register_mutex);
2ebef69fc   Takashi Iwai   ALSA: hwdep: Use ...
400
  	if (snd_hwdep_search(card, hwdep->device)) {
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
401
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
  		return -EBUSY;
  	}
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
404
  	list_add_tail(&hwdep->list, &snd_hwdep_devices);
40a4b2638   Takashi Iwai   ALSA: Simplify sn...
405
406
407
  	err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
  				  hwdep->card, hwdep->device,
  				  &snd_hwdep_f_ops, hwdep, &hwdep->dev);
71e2e1c14   Takashi Iwai   ALSA: hwdep: Allo...
408
  	if (err < 0) {
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
409
410
  		dev_err(&hwdep->dev, "unable to register
  ");
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
411
  		list_del(&hwdep->list);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
412
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
  		return err;
  	}
caa751bad   Takashi Iwai   ALSA: Create sysf...
415

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
417
418
  #ifdef CONFIG_SND_OSSEMUL
  	hwdep->ossreg = 0;
  	if (hwdep->oss_type >= 0) {
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
419
420
421
  		if (hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM &&
  		    hwdep->device)
  			dev_warn(&hwdep->dev,
2ebef69fc   Takashi Iwai   ALSA: hwdep: Use ...
422
423
  				 "only hwdep device 0 can be registered as OSS direct FM device!
  ");
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
424
425
426
427
428
429
430
431
  		else if (snd_register_oss_device(hwdep->oss_type,
  						 card, hwdep->device,
  						 &snd_hwdep_f_ops, hwdep) < 0)
  			dev_warn(&hwdep->dev,
  				 "unable to register OSS compatibility device
  ");
  		else
  			hwdep->ossreg = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
  	}
  #endif
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
434
  	mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
436
  	return 0;
  }
c461482c8   Takashi Iwai   [ALSA] Unregister...
437
  static int snd_hwdep_dev_disconnect(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
439
  	struct snd_hwdep *hwdep = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440

7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
441
442
  	if (snd_BUG_ON(!hwdep))
  		return -ENXIO;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
443
  	mutex_lock(&register_mutex);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
444
  	if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep) {
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
445
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
446
447
  		return -EINVAL;
  	}
0914f7961   Takashi Iwai   ALSA: Avoid endle...
448
449
  	mutex_lock(&hwdep->open_mutex);
  	wake_up(&hwdep->open_wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
452
453
  #ifdef CONFIG_SND_OSSEMUL
  	if (hwdep->ossreg)
  		snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
  #endif
40a4b2638   Takashi Iwai   ALSA: Simplify sn...
454
  	snd_unregister_device(&hwdep->dev);
c461482c8   Takashi Iwai   [ALSA] Unregister...
455
  	list_del_init(&hwdep->list);
0914f7961   Takashi Iwai   ALSA: Avoid endle...
456
  	mutex_unlock(&hwdep->open_mutex);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
457
  	mutex_unlock(&register_mutex);
c461482c8   Takashi Iwai   [ALSA] Unregister...
458
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
  }
cd6a65036   Jie Yang   ALSA: replace CON...
460
  #ifdef CONFIG_SND_PROC_FS
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
461
462
463
  /*
   *  Info interface
   */
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
464
465
  static void snd_hwdep_proc_read(struct snd_info_entry *entry,
  				struct snd_info_buffer *buffer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
467
  	struct snd_hwdep *hwdep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468

1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
469
  	mutex_lock(&register_mutex);
9244b2c30   Johannes Berg   [ALSA] alsa core:...
470
  	list_for_each_entry(hwdep, &snd_hwdep_devices, list)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
472
  		snd_iprintf(buffer, "%02i-%02i: %s
  ",
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
473
  			    hwdep->card->number, hwdep->device, hwdep->name);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
474
  	mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
  }
e28563cce   Takashi Iwai   [ALSA] Optimize f...
476
  static struct snd_info_entry *snd_hwdep_proc_entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477

e28563cce   Takashi Iwai   [ALSA] Optimize f...
478
  static void __init snd_hwdep_proc_init(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
480
  	struct snd_info_entry *entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
  	if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
483
484
485
486
487
488
489
  		entry->c.text.read = snd_hwdep_proc_read;
  		if (snd_info_register(entry) < 0) {
  			snd_info_free_entry(entry);
  			entry = NULL;
  		}
  	}
  	snd_hwdep_proc_entry = entry;
e28563cce   Takashi Iwai   [ALSA] Optimize f...
490
491
492
493
  }
  
  static void __exit snd_hwdep_proc_done(void)
  {
746d4a02e   Takashi Iwai   [ALSA] Fix discon...
494
  	snd_info_free_entry(snd_hwdep_proc_entry);
e28563cce   Takashi Iwai   [ALSA] Optimize f...
495
  }
cd6a65036   Jie Yang   ALSA: replace CON...
496
  #else /* !CONFIG_SND_PROC_FS */
e28563cce   Takashi Iwai   [ALSA] Optimize f...
497
498
  #define snd_hwdep_proc_init()
  #define snd_hwdep_proc_done()
cd6a65036   Jie Yang   ALSA: replace CON...
499
  #endif /* CONFIG_SND_PROC_FS */
e28563cce   Takashi Iwai   [ALSA] Optimize f...
500
501
502
503
504
505
506
507
508
  
  
  /*
   *  ENTRY functions
   */
  
  static int __init alsa_hwdep_init(void)
  {
  	snd_hwdep_proc_init();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
510
511
512
513
514
515
516
517
  	snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
  	snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl);
  	return 0;
  }
  
  static void __exit alsa_hwdep_exit(void)
  {
  	snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
  	snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl);
e28563cce   Takashi Iwai   [ALSA] Optimize f...
518
  	snd_hwdep_proc_done();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
520
521
522
  }
  
  module_init(alsa_hwdep_init)
  module_exit(alsa_hwdep_exit)