2 * Jack abstraction layer
4 * Copyright 2008 Wolfson Microelectronics
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/input.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <sound/jack.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
29 struct snd_jack_kctl {
30 struct snd_kcontrol *kctl;
31 struct list_head list; /* list of controls belong to the same jack */
32 unsigned int mask_bits; /* only masked status bits are reported via kctl */
35 #ifdef CONFIG_SND_JACK_INPUT_DEV
36 static int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
40 SW_JACK_PHYSICAL_INSERT,
44 #endif /* CONFIG_SND_JACK_INPUT_DEV */
46 static int snd_jack_dev_disconnect(struct snd_device *device)
48 #ifdef CONFIG_SND_JACK_INPUT_DEV
49 struct snd_jack *jack = device->device_data;
51 mutex_lock(&jack->input_dev_lock);
52 if (!jack->input_dev) {
53 mutex_unlock(&jack->input_dev_lock);
57 /* If the input device is registered with the input subsystem
58 * then we need to use a different deallocator. */
60 input_unregister_device(jack->input_dev);
62 input_free_device(jack->input_dev);
63 jack->input_dev = NULL;
64 mutex_unlock(&jack->input_dev_lock);
65 #endif /* CONFIG_SND_JACK_INPUT_DEV */
69 static int snd_jack_dev_free(struct snd_device *device)
71 struct snd_jack *jack = device->device_data;
72 struct snd_card *card = device->card;
73 struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
75 down_write(&card->controls_rwsem);
76 list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
77 list_del_init(&jack_kctl->list);
78 snd_ctl_remove(card, jack_kctl->kctl);
80 up_write(&card->controls_rwsem);
82 if (jack->private_free)
83 jack->private_free(jack);
85 snd_jack_dev_disconnect(device);
93 #ifdef CONFIG_SND_JACK_INPUT_DEV
94 static int snd_jack_dev_register(struct snd_device *device)
96 struct snd_jack *jack = device->device_data;
97 struct snd_card *card = device->card;
100 snprintf(jack->name, sizeof(jack->name), "%s %s",
101 card->shortname, jack->id);
103 mutex_lock(&jack->input_dev_lock);
104 if (!jack->input_dev) {
105 mutex_unlock(&jack->input_dev_lock);
109 jack->input_dev->name = jack->name;
111 /* Default to the sound card device. */
112 if (!jack->input_dev->dev.parent)
113 jack->input_dev->dev.parent = snd_card_get_device_link(card);
115 /* Add capabilities for any keys that are enabled */
116 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
117 int testbit = SND_JACK_BTN_0 >> i;
119 if (!(jack->type & testbit))
123 jack->key[i] = BTN_0 + i;
125 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
128 err = input_register_device(jack->input_dev);
130 jack->registered = 1;
132 mutex_unlock(&jack->input_dev_lock);
135 #endif /* CONFIG_SND_JACK_INPUT_DEV */
137 static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
139 struct snd_jack_kctl *jack_kctl;
141 jack_kctl = kctl->private_data;
143 list_del(&jack_kctl->list);
148 static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
150 list_add_tail(&jack_kctl->list, &jack->kctl_list);
153 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
155 struct snd_kcontrol *kctl;
156 struct snd_jack_kctl *jack_kctl;
159 kctl = snd_kctl_jack_new(name, card);
163 err = snd_ctl_add(card, kctl);
167 jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
172 jack_kctl->kctl = kctl;
173 jack_kctl->mask_bits = mask;
175 kctl->private_data = jack_kctl;
176 kctl->private_free = snd_jack_kctl_private_free;
180 snd_ctl_free_one(kctl);
185 * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
186 * @jack: the jack instance which the kctl will attaching to
187 * @name: the name for the snd_kcontrol object
188 * @mask: a bitmask of enum snd_jack_type values that can be detected
189 * by this snd_jack_kctl object.
191 * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
193 * Return: Zero if successful, or a negative error code on failure.
195 int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
197 struct snd_jack_kctl *jack_kctl;
199 jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
203 snd_jack_kctl_add(jack, jack_kctl);
206 EXPORT_SYMBOL(snd_jack_add_new_kctl);
209 * snd_jack_new - Create a new jack
210 * @card: the card instance
211 * @id: an identifying string for this jack
212 * @type: a bitmask of enum snd_jack_type values that can be detected by
214 * @jjack: Used to provide the allocated jack object to the caller.
215 * @initial_kctl: if true, create a kcontrol and add it to the jack list.
216 * @phantom_jack: Don't create a input device for phantom jacks.
218 * Creates a new jack object.
220 * Return: Zero if successful, or a negative error code on failure.
221 * On success @jjack will be initialised.
223 int snd_jack_new(struct snd_card *card, const char *id, int type,
224 struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
226 struct snd_jack *jack;
227 struct snd_jack_kctl *jack_kctl = NULL;
229 static struct snd_device_ops ops = {
230 .dev_free = snd_jack_dev_free,
231 #ifdef CONFIG_SND_JACK_INPUT_DEV
232 .dev_register = snd_jack_dev_register,
233 .dev_disconnect = snd_jack_dev_disconnect,
234 #endif /* CONFIG_SND_JACK_INPUT_DEV */
238 jack_kctl = snd_jack_kctl_new(card, id, type);
243 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
247 jack->id = kstrdup(id, GFP_KERNEL);
248 if (jack->id == NULL) {
253 #ifdef CONFIG_SND_JACK_INPUT_DEV
254 mutex_init(&jack->input_dev_lock);
256 /* don't create input device for phantom jack */
260 jack->input_dev = input_allocate_device();
261 if (jack->input_dev == NULL) {
266 jack->input_dev->phys = "ALSA";
270 for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
272 input_set_capability(jack->input_dev, EV_SW,
273 jack_switch_types[i]);
276 #endif /* CONFIG_SND_JACK_INPUT_DEV */
278 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
283 INIT_LIST_HEAD(&jack->kctl_list);
286 snd_jack_kctl_add(jack, jack_kctl);
293 #ifdef CONFIG_SND_JACK_INPUT_DEV
294 input_free_device(jack->input_dev);
300 EXPORT_SYMBOL(snd_jack_new);
302 #ifdef CONFIG_SND_JACK_INPUT_DEV
304 * snd_jack_set_parent - Set the parent device for a jack
306 * @jack: The jack to configure
307 * @parent: The device to set as parent for the jack.
309 * Set the parent for the jack devices in the device tree. This
310 * function is only valid prior to registration of the jack. If no
311 * parent is configured then the parent device will be the sound card.
313 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
315 WARN_ON(jack->registered);
316 mutex_lock(&jack->input_dev_lock);
317 if (!jack->input_dev) {
318 mutex_unlock(&jack->input_dev_lock);
322 jack->input_dev->dev.parent = parent;
323 mutex_unlock(&jack->input_dev_lock);
325 EXPORT_SYMBOL(snd_jack_set_parent);
328 * snd_jack_set_key - Set a key mapping on a jack
330 * @jack: The jack to configure
331 * @type: Jack report type for this key
332 * @keytype: Input layer key type to be reported
334 * Map a SND_JACK_BTN_* button type to an input layer key, allowing
335 * reporting of keys on accessories via the jack abstraction. If no
336 * mapping is provided but keys are enabled in the jack type then
337 * BTN_n numeric buttons will be reported.
339 * If jacks are not reporting via the input API this call will have no
342 * Note that this is intended to be use by simple devices with small
343 * numbers of keys that can be reported. It is also possible to
344 * access the input device directly - devices with complex input
345 * capabilities on accessories should consider doing this rather than
346 * using this abstraction.
348 * This function may only be called prior to registration of the jack.
350 * Return: Zero if successful, or a negative error code on failure.
352 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
355 int key = fls(SND_JACK_BTN_0) - fls(type);
357 WARN_ON(jack->registered);
359 if (!keytype || key >= ARRAY_SIZE(jack->key))
363 jack->key[key] = keytype;
366 EXPORT_SYMBOL(snd_jack_set_key);
367 #endif /* CONFIG_SND_JACK_INPUT_DEV */
370 * snd_jack_report - Report the current status of a jack
371 * Note: This function uses mutexes and should be called from a
372 * context which can sleep (such as a workqueue).
374 * @jack: The jack to report status for
375 * @status: The current status of the jack
377 void snd_jack_report(struct snd_jack *jack, int status)
379 struct snd_jack_kctl *jack_kctl;
380 #ifdef CONFIG_SND_JACK_INPUT_DEV
381 struct input_dev *idev;
388 list_for_each_entry(jack_kctl, &jack->kctl_list, list)
389 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
390 status & jack_kctl->mask_bits);
392 #ifdef CONFIG_SND_JACK_INPUT_DEV
393 idev = input_get_device(jack->input_dev);
397 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
398 int testbit = SND_JACK_BTN_0 >> i;
400 if (jack->type & testbit)
401 input_report_key(idev, jack->key[i],
405 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
406 int testbit = 1 << i;
407 if (jack->type & testbit)
408 input_report_switch(idev,
409 jack_switch_types[i],
414 input_put_device(idev);
415 #endif /* CONFIG_SND_JACK_INPUT_DEV */
417 EXPORT_SYMBOL(snd_jack_report);