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 <sound/jack.h>
24 #include <sound/core.h>
26 static int jack_switch_types[] = {
30 SW_JACK_PHYSICAL_INSERT,
34 static int snd_jack_dev_free(struct snd_device *device)
36 struct snd_jack *jack = device->device_data;
38 if (jack->private_free)
39 jack->private_free(jack);
41 /* If the input device is registered with the input subsystem
42 * then we need to use a different deallocator. */
44 input_unregister_device(jack->input_dev);
46 input_free_device(jack->input_dev);
54 static int snd_jack_dev_register(struct snd_device *device)
56 struct snd_jack *jack = device->device_data;
57 struct snd_card *card = device->card;
60 snprintf(jack->name, sizeof(jack->name), "%s %s",
61 card->shortname, jack->id);
62 jack->input_dev->name = jack->name;
64 /* Default to the sound card device. */
65 if (!jack->input_dev->dev.parent)
66 jack->input_dev->dev.parent = snd_card_get_device_link(card);
68 /* Add capabilities for any keys that are enabled */
69 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
70 int testbit = SND_JACK_BTN_0 >> i;
72 if (!(jack->type & testbit))
76 jack->key[i] = BTN_0 + i;
78 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
81 err = input_register_device(jack->input_dev);
89 * snd_jack_new - Create a new jack
90 * @card: the card instance
91 * @id: an identifying string for this jack
92 * @type: a bitmask of enum snd_jack_type values that can be detected by
94 * @jjack: Used to provide the allocated jack object to the caller.
96 * Creates a new jack object.
98 * Returns zero if successful, or a negative error code on failure.
99 * On success jjack will be initialised.
101 int snd_jack_new(struct snd_card *card, const char *id, int type,
102 struct snd_jack **jjack)
104 struct snd_jack *jack;
107 static struct snd_device_ops ops = {
108 .dev_free = snd_jack_dev_free,
109 .dev_register = snd_jack_dev_register,
112 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
116 jack->id = kstrdup(id, GFP_KERNEL);
118 jack->input_dev = input_allocate_device();
119 if (jack->input_dev == NULL) {
124 jack->input_dev->phys = "ALSA";
128 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++)
130 input_set_capability(jack->input_dev, EV_SW,
131 jack_switch_types[i]);
133 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
142 input_free_device(jack->input_dev);
146 EXPORT_SYMBOL(snd_jack_new);
149 * snd_jack_set_parent - Set the parent device for a jack
151 * @jack: The jack to configure
152 * @parent: The device to set as parent for the jack.
154 * Set the parent for the jack input device in the device tree. This
155 * function is only valid prior to registration of the jack. If no
156 * parent is configured then the parent device will be the sound card.
158 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
160 WARN_ON(jack->registered);
162 jack->input_dev->dev.parent = parent;
164 EXPORT_SYMBOL(snd_jack_set_parent);
167 * snd_jack_set_key - Set a key mapping on a jack
169 * @jack: The jack to configure
170 * @type: Jack report type for this key
171 * @keytype: Input layer key type to be reported
173 * Map a SND_JACK_BTN_ button type to an input layer key, allowing
174 * reporting of keys on accessories via the jack abstraction. If no
175 * mapping is provided but keys are enabled in the jack type then
176 * BTN_n numeric buttons will be reported.
178 * Note that this is intended to be use by simple devices with small
179 * numbers of keys that can be reported. It is also possible to
180 * access the input device directly - devices with complex input
181 * capabilities on accessories should consider doing this rather than
182 * using this abstraction.
184 * This function may only be called prior to registration of the jack.
186 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
189 int key = fls(SND_JACK_BTN_0) - fls(type);
191 WARN_ON(jack->registered);
193 if (!keytype || key >= ARRAY_SIZE(jack->key))
197 jack->key[key] = keytype;
201 EXPORT_SYMBOL(snd_jack_set_key);
204 * snd_jack_report - Report the current status of a jack
206 * @jack: The jack to report status for
207 * @status: The current status of the jack
209 void snd_jack_report(struct snd_jack *jack, int status)
216 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
217 int testbit = SND_JACK_BTN_0 >> i;
219 if (jack->type & testbit)
220 input_report_key(jack->input_dev, jack->key[i],
224 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
225 int testbit = 1 << i;
226 if (jack->type & testbit)
227 input_report_switch(jack->input_dev,
228 jack_switch_types[i],
232 input_sync(jack->input_dev);
234 EXPORT_SYMBOL(snd_jack_report);
236 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
237 MODULE_DESCRIPTION("Jack detection support for ALSA");
238 MODULE_LICENSE("GPL");