Blame view

sound/core/hwdep.c 13.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
  /*
   *  Hardware dependent layer
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
3
   *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   *
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
  #include <linux/major.h>
  #include <linux/init.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
  #include <linux/slab.h>
  #include <linux/time.h>
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
25
  #include <linux/mutex.h>
da155d5b4   Paul Gortmaker   sound: Add module...
26
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
30
31
  #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...
32
  MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
  MODULE_DESCRIPTION("Hardware dependent layer");
  MODULE_LICENSE("GPL");
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
35
  static LIST_HEAD(snd_hwdep_devices);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
36
  static DEFINE_MUTEX(register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37

d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
38
39
  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...
40
  static int snd_hwdep_dev_disconnect(struct snd_device *device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42

f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
43
44
  static struct snd_hwdep *snd_hwdep_search(struct snd_card *card, int device)
  {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
45
  	struct snd_hwdep *hwdep;
9244b2c30   Johannes Berg   [ALSA] alsa core:...
46
  	list_for_each_entry(hwdep, &snd_hwdep_devices, list)
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
47
48
  		if (hwdep->card == card && hwdep->device == device)
  			return hwdep;
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
49
50
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
  
  static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
54
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
57
58
  	if (hw->ops.llseek)
  		return hw->ops.llseek(hw, file, offset, orig);
  	return -ENXIO;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
59
60
  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
61
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
62
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
66
  	if (hw->ops.read)
  		return hw->ops.read(hw, buf, count, offset);
  	return -ENXIO;	
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
67
68
  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
69
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
70
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
75
76
77
78
  	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...
79
  	struct snd_hwdep *hw;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
  	int err;
  	wait_queue_t wait;
f19028601   Clemens Ladisch   [ALSA] fix improp...
82
  	if (major == snd_major) {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
83
84
  		hw = snd_lookup_minor_data(iminor(inode),
  					   SNDRV_DEVICE_TYPE_HWDEP);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
  #ifdef CONFIG_SND_OSSEMUL
f19028601   Clemens Ladisch   [ALSA] fix improp...
86
  	} else if (major == SOUND_MAJOR) {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
87
88
  		hw = snd_lookup_oss_minor_data(iminor(inode),
  					       SNDRV_OSS_DEVICE_TYPE_DMFM);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
  #endif
f19028601   Clemens Ladisch   [ALSA] fix improp...
90
  	} else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
  		return -ENXIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
  	if (hw == NULL)
  		return -ENODEV;
a0830dbd4   Takashi Iwai   ALSA: Add a refer...
94
95
  	if (!try_module_get(hw->card->module)) {
  		snd_card_unref(hw->card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
  		return -EFAULT;
a0830dbd4   Takashi Iwai   ALSA: Add a refer...
97
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
  
  	init_waitqueue_entry(&wait, current);
  	add_wait_queue(&hw->open_wait, &wait);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
101
  	mutex_lock(&hw->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
104
105
106
  	while (1) {
  		if (hw->exclusive && hw->used > 0) {
  			err = -EBUSY;
  			break;
  		}
345d0b196   Takashi Iwai   ALSA: hwdep - Mak...
107
108
109
110
  		if (!hw->ops.open) {
  			err = 0;
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
112
113
114
115
116
117
118
119
120
121
  		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 ...
122
  		mutex_unlock(&hw->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
  		schedule();
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
124
  		mutex_lock(&hw->open_mutex);
0914f7961   Takashi Iwai   ALSA: Avoid endle...
125
126
127
128
  		if (hw->card->shutdown) {
  			err = -ENODEV;
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  		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 ...
145
  	mutex_unlock(&hw->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
  	if (err < 0)
  		module_put(hw->card->module);
a0830dbd4   Takashi Iwai   ALSA: Add a refer...
148
  	snd_card_unref(hw->card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
149
150
151
152
153
  	return err;
  }
  
  static int snd_hwdep_release(struct inode *inode, struct file * file)
  {
345d0b196   Takashi Iwai   ALSA: hwdep - Mak...
154
  	int err = 0;
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
155
  	struct snd_hwdep *hw = file->private_data;
104326f8d   Florin Malita   [ALSA] Dereferenc...
156
  	struct module *mod = hw->card->module;
8fa58af7d   Karsten Wiese   [ALSA] snd_hwdep_...
157

1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
158
  	mutex_lock(&hw->open_mutex);
8fa58af7d   Karsten Wiese   [ALSA] snd_hwdep_...
159
  	if (hw->ops.release)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
  		err = hw->ops.release(hw, file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
162
  	if (hw->used > 0)
  		hw->used--;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
163
  	mutex_unlock(&hw->open_mutex);
8fa58af7d   Karsten Wiese   [ALSA] snd_hwdep_...
164
165
166
  	wake_up(&hw->open_wait);
  
  	snd_card_file_remove(hw->card, file);
104326f8d   Florin Malita   [ALSA] Dereferenc...
167
  	module_put(mod);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
170
171
172
  	return err;
  }
  
  static unsigned int snd_hwdep_poll(struct file * file, poll_table * wait)
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
173
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
176
177
  	if (hw->ops.poll)
  		return hw->ops.poll(hw, file, wait);
  	return 0;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
178
179
  static int snd_hwdep_info(struct snd_hwdep *hw,
  			  struct snd_hwdep_info __user *_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
181
  	struct snd_hwdep_info info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
186
187
188
189
190
191
  	
  	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...
192
193
  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
194
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
195
  	struct snd_hwdep_dsp_status info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196
197
198
199
200
201
202
203
204
205
206
207
  	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...
208
209
  static int snd_hwdep_dsp_load(struct snd_hwdep *hw,
  			      struct snd_hwdep_dsp_image __user *_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
211
  	struct snd_hwdep_dsp_image info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  	int err;
  	
  	if (! hw->ops.dsp_load)
  		return -ENXIO;
  	memset(&info, 0, sizeof(info));
  	if (copy_from_user(&info, _info, sizeof(info)))
  		return -EFAULT;
  	/* check whether the dsp was already loaded */
  	if (hw->dsp_loaded & (1 << info.index))
  		return -EBUSY;
  	if (!access_ok(VERIFY_READ, info.image, info.length))
  		return -EFAULT;
  	err = hw->ops.dsp_load(hw, &info);
  	if (err < 0)
  		return err;
  	hw->dsp_loaded |= (1 << info.index);
  	return 0;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
230
231
  static long snd_hwdep_ioctl(struct file * file, unsigned int cmd,
  			    unsigned long arg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
233
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  	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:
  		return snd_hwdep_dsp_load(hw, argp);
  	}
  	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...
252
  	struct snd_hwdep *hw = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
  	if (hw->ops.mmap)
  		return hw->ops.mmap(hw, file, vma);
  	return -ENXIO;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
257
258
  static int snd_hwdep_control_ioctl(struct snd_card *card,
  				   struct snd_ctl_file * control,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
  				   unsigned int cmd, unsigned long arg)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261
262
263
264
265
266
267
  	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 ...
268
  			mutex_lock(&register_mutex);
f7b2bb854   Dan Carpenter   ALSA: hwdep: sile...
269
270
271
272
273
274
275
  
  			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
276
  			while (device < SNDRV_MINOR_HWDEPS) {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
277
  				if (snd_hwdep_search(card, device))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
279
280
281
282
  					break;
  				device++;
  			}
  			if (device >= SNDRV_MINOR_HWDEPS)
  				device = -1;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
283
  			mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
285
286
287
288
289
  			if (put_user(device, (int __user *)arg))
  				return -EFAULT;
  			return 0;
  		}
  	case SNDRV_CTL_IOCTL_HWDEP_INFO:
  		{
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
290
  			struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
291
  			int device, err;
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
292
  			struct snd_hwdep *hwdep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
293
294
295
  
  			if (get_user(device, &info->device))
  				return -EFAULT;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
296
  			mutex_lock(&register_mutex);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
297
298
299
300
301
  			hwdep = snd_hwdep_search(card, device);
  			if (hwdep)
  				err = snd_hwdep_info(hwdep, info);
  			else
  				err = -ENXIO;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
302
  			mutex_unlock(&register_mutex);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
303
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
304
305
306
307
308
309
310
311
312
313
314
315
316
317
  		}
  	}
  	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...
318
  static const struct file_operations snd_hwdep_f_ops =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
322
323
324
325
326
327
328
329
330
  {
  	.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...
331
332
333
334
  static void release_hwdep_device(struct device *dev)
  {
  	kfree(container_of(dev, struct snd_hwdep, dev));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
336
337
338
339
340
341
342
343
344
345
  /**
   * 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 ...
346
   * Return: Zero if successful, or a negative error code on failure.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
   */
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
348
349
  int snd_hwdep_new(struct snd_card *card, char *id, int device,
  		  struct snd_hwdep **rhwdep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
350
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
351
  	struct snd_hwdep *hwdep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
352
  	int err;
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
353
  	static struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
  		.dev_free = snd_hwdep_dev_free,
  		.dev_register = snd_hwdep_dev_register,
c461482c8   Takashi Iwai   [ALSA] Unregister...
356
  		.dev_disconnect = snd_hwdep_dev_disconnect,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
  	};
7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
358
359
360
361
  	if (snd_BUG_ON(!card))
  		return -ENXIO;
  	if (rhwdep)
  		*rhwdep = NULL;
ca2c09665   Takashi Iwai   [ALSA] Replace wi...
362
  	hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
ec0e9937a   Takashi Iwai   ALSA: core: Drop ...
363
  	if (!hwdep)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
  		return -ENOMEM;
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
365
366
367
  
  	init_waitqueue_head(&hwdep->open_wait);
  	mutex_init(&hwdep->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
  	hwdep->card = card;
  	hwdep->device = device;
73e77ba02   Takashi Iwai   [ALSA] Add error ...
370
  	if (id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
371
  		strlcpy(hwdep->id, id, sizeof(hwdep->id));
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
372
373
374
375
  
  	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
376
377
378
  #ifdef CONFIG_SND_OSSEMUL
  	hwdep->oss_type = -1;
  #endif
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
379
380
381
382
  
  	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
383
384
  		return err;
  	}
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
385

7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
386
387
  	if (rhwdep)
  		*rhwdep = hwdep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
  	return 0;
  }
6776a5d71   Takashi Iwai   ALSA: Move EXPORT...
390
  EXPORT_SYMBOL(snd_hwdep_new);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391

7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
392
  static int snd_hwdep_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
  {
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
394
  	struct snd_hwdep *hwdep = device->device_data;
7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
395
396
  	if (!hwdep)
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
398
  	if (hwdep->private_free)
  		hwdep->private_free(hwdep);
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
399
  	put_device(&hwdep->dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
401
  	return 0;
  }
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
402
  static int snd_hwdep_dev_register(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
403
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
404
  	struct snd_hwdep *hwdep = device->device_data;
2ebef69fc   Takashi Iwai   ALSA: hwdep: Use ...
405
  	struct snd_card *card = hwdep->card;
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
406
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407

1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
408
  	mutex_lock(&register_mutex);
2ebef69fc   Takashi Iwai   ALSA: hwdep: Use ...
409
  	if (snd_hwdep_search(card, hwdep->device)) {
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
410
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
411
412
  		return -EBUSY;
  	}
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
413
  	list_add_tail(&hwdep->list, &snd_hwdep_devices);
40a4b2638   Takashi Iwai   ALSA: Simplify sn...
414
415
416
  	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...
417
  	if (err < 0) {
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
418
419
  		dev_err(&hwdep->dev, "unable to register
  ");
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
420
  		list_del(&hwdep->list);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
421
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422
423
  		return err;
  	}
caa751bad   Takashi Iwai   ALSA: Create sysf...
424

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
426
427
  #ifdef CONFIG_SND_OSSEMUL
  	hwdep->ossreg = 0;
  	if (hwdep->oss_type >= 0) {
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
428
429
430
  		if (hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM &&
  		    hwdep->device)
  			dev_warn(&hwdep->dev,
2ebef69fc   Takashi Iwai   ALSA: hwdep: Use ...
431
432
  				 "only hwdep device 0 can be registered as OSS direct FM device!
  ");
7b4616000   Takashi Iwai   ALSA: hwdep: Embe...
433
434
435
436
437
438
439
440
  		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
441
442
  	}
  #endif
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
443
  	mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
445
  	return 0;
  }
c461482c8   Takashi Iwai   [ALSA] Unregister...
446
  static int snd_hwdep_dev_disconnect(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
448
  	struct snd_hwdep *hwdep = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
449

7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
450
451
  	if (snd_BUG_ON(!hwdep))
  		return -ENXIO;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
452
  	mutex_lock(&register_mutex);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
453
  	if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep) {
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
454
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
456
  		return -EINVAL;
  	}
0914f7961   Takashi Iwai   ALSA: Avoid endle...
457
458
  	mutex_lock(&hwdep->open_mutex);
  	wake_up(&hwdep->open_wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
460
461
462
  #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...
463
  	snd_unregister_device(&hwdep->dev);
c461482c8   Takashi Iwai   [ALSA] Unregister...
464
  	list_del_init(&hwdep->list);
0914f7961   Takashi Iwai   ALSA: Avoid endle...
465
  	mutex_unlock(&hwdep->open_mutex);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
466
  	mutex_unlock(&register_mutex);
c461482c8   Takashi Iwai   [ALSA] Unregister...
467
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468
  }
e28563cce   Takashi Iwai   [ALSA] Optimize f...
469
  #ifdef CONFIG_PROC_FS
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
472
  /*
   *  Info interface
   */
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
473
474
  static void snd_hwdep_proc_read(struct snd_info_entry *entry,
  				struct snd_info_buffer *buffer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
  {
d9a98de21   Takashi Iwai   [ALSA] Remove xxx...
476
  	struct snd_hwdep *hwdep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477

1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
478
  	mutex_lock(&register_mutex);
9244b2c30   Johannes Berg   [ALSA] alsa core:...
479
  	list_for_each_entry(hwdep, &snd_hwdep_devices, list)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
481
  		snd_iprintf(buffer, "%02i-%02i: %s
  ",
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
482
  			    hwdep->card->number, hwdep->device, hwdep->name);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
483
  	mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
  }
e28563cce   Takashi Iwai   [ALSA] Optimize f...
485
  static struct snd_info_entry *snd_hwdep_proc_entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
  	if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
492
493
494
495
496
497
498
  		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...
499
500
501
502
  }
  
  static void __exit snd_hwdep_proc_done(void)
  {
746d4a02e   Takashi Iwai   [ALSA] Fix discon...
503
  	snd_info_free_entry(snd_hwdep_proc_entry);
e28563cce   Takashi Iwai   [ALSA] Optimize f...
504
505
506
507
508
509
510
511
512
513
514
515
516
517
  }
  #else /* !CONFIG_PROC_FS */
  #define snd_hwdep_proc_init()
  #define snd_hwdep_proc_done()
  #endif /* CONFIG_PROC_FS */
  
  
  /*
   *  ENTRY functions
   */
  
  static int __init alsa_hwdep_init(void)
  {
  	snd_hwdep_proc_init();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
518
519
520
521
522
523
524
525
526
  	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...
527
  	snd_hwdep_proc_done();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
529
530
531
  }
  
  module_init(alsa_hwdep_init)
  module_exit(alsa_hwdep_exit)