blob: 0dbd618e3c942bf264d8f44d681fc4d1adc312d5 [file] [log] [blame]
Mark Browne76d8ce2008-07-28 19:05:35 +01001/*
2 * Jack abstraction layer
3 *
4 * Copyright 2008 Wolfson Microelectronics
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Mark Browne76d8ce2008-07-28 19:05:35 +010025#include <sound/jack.h>
26#include <sound/core.h>
Jie Yang9058cbe2015-04-27 21:20:56 +080027#include <sound/control.h>
28
29struct 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 */
33};
Mark Browne76d8ce2008-07-28 19:05:35 +010034
Mark Brown53803ae2012-02-07 19:48:47 +000035static int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
Mark Brownbd8a71a2009-01-03 16:56:56 +000036 SW_HEADPHONE_INSERT,
37 SW_MICROPHONE_INSERT,
38 SW_LINEOUT_INSERT,
39 SW_JACK_PHYSICAL_INSERT,
Jani Nikulad506fc32009-01-07 11:54:25 +020040 SW_VIDEOOUT_INSERT,
David Henningsson7c2f8e42011-10-05 15:53:25 +020041 SW_LINEIN_INSERT,
Mark Brownbd8a71a2009-01-03 16:56:56 +000042};
43
Takashi Iwai32b85442013-11-14 15:35:46 +010044static int snd_jack_dev_disconnect(struct snd_device *device)
Mark Browne76d8ce2008-07-28 19:05:35 +010045{
46 struct snd_jack *jack = device->device_data;
47
Takashi Iwai32b85442013-11-14 15:35:46 +010048 if (!jack->input_dev)
49 return 0;
Takashi Iwai9d590652009-04-14 16:13:58 +020050
Mark Browne76d8ce2008-07-28 19:05:35 +010051 /* If the input device is registered with the input subsystem
52 * then we need to use a different deallocator. */
53 if (jack->registered)
54 input_unregister_device(jack->input_dev);
55 else
56 input_free_device(jack->input_dev);
Takashi Iwai32b85442013-11-14 15:35:46 +010057 jack->input_dev = NULL;
58 return 0;
59}
60
61static int snd_jack_dev_free(struct snd_device *device)
62{
63 struct snd_jack *jack = device->device_data;
Jie Yang9058cbe2015-04-27 21:20:56 +080064 struct snd_card *card = device->card;
65 struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
Takashi Iwai32b85442013-11-14 15:35:46 +010066
Jie Yang9058cbe2015-04-27 21:20:56 +080067 list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
68 list_del_init(&jack_kctl->list);
69 snd_ctl_remove(card, jack_kctl->kctl);
70 }
Takashi Iwai32b85442013-11-14 15:35:46 +010071 if (jack->private_free)
72 jack->private_free(jack);
73
74 snd_jack_dev_disconnect(device);
Mark Browne76d8ce2008-07-28 19:05:35 +010075
Matthew Ranostay282cd762008-10-25 01:05:29 -040076 kfree(jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +010077 kfree(jack);
78
79 return 0;
80}
81
82static int snd_jack_dev_register(struct snd_device *device)
83{
84 struct snd_jack *jack = device->device_data;
85 struct snd_card *card = device->card;
Mark Brownebb812c2010-03-17 18:07:12 +000086 int err, i;
Mark Browne76d8ce2008-07-28 19:05:35 +010087
88 snprintf(jack->name, sizeof(jack->name), "%s %s",
Takashi Iwai2678f602009-02-18 16:46:27 +010089 card->shortname, jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +010090 jack->input_dev->name = jack->name;
91
92 /* Default to the sound card device. */
93 if (!jack->input_dev->dev.parent)
Kay Sievers1f3fff72009-06-10 19:50:33 +020094 jack->input_dev->dev.parent = snd_card_get_device_link(card);
Mark Browne76d8ce2008-07-28 19:05:35 +010095
Mark Brownebb812c2010-03-17 18:07:12 +000096 /* Add capabilities for any keys that are enabled */
97 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
98 int testbit = SND_JACK_BTN_0 >> i;
99
100 if (!(jack->type & testbit))
101 continue;
102
103 if (!jack->key[i])
104 jack->key[i] = BTN_0 + i;
105
106 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
107 }
108
Mark Browne76d8ce2008-07-28 19:05:35 +0100109 err = input_register_device(jack->input_dev);
110 if (err == 0)
111 jack->registered = 1;
112
113 return err;
114}
115
Jie Yang9058cbe2015-04-27 21:20:56 +0800116static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
117{
118 struct snd_jack_kctl *jack_kctl;
119
120 jack_kctl = kctl->private_data;
121 if (jack_kctl) {
122 list_del(&jack_kctl->list);
123 kfree(jack_kctl);
124 }
125}
126
127static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
128{
129 list_add_tail(&jack_kctl->list, &jack->kctl_list);
130}
131
132static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
133{
134 struct snd_kcontrol *kctl;
135 struct snd_jack_kctl *jack_kctl;
136 int err;
137
Jie Yang2ba2dfa2015-04-27 21:20:59 +0800138 kctl = snd_kctl_jack_new(name, card);
Jie Yang9058cbe2015-04-27 21:20:56 +0800139 if (!kctl)
140 return NULL;
141
142 err = snd_ctl_add(card, kctl);
143 if (err < 0)
144 return NULL;
145
146 jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
147
148 if (!jack_kctl)
149 goto error;
150
151 jack_kctl->kctl = kctl;
152 jack_kctl->mask_bits = mask;
153
154 kctl->private_data = jack_kctl;
155 kctl->private_free = snd_jack_kctl_private_free;
156
157 return jack_kctl;
158error:
159 snd_ctl_free_one(kctl);
160 return NULL;
161}
162
163/**
164 * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
165 * @jack: the jack instance which the kctl will attaching to
166 * @name: the name for the snd_kcontrol object
167 * @mask: a bitmask of enum snd_jack_type values that can be detected
168 * by this snd_jack_kctl object.
169 *
170 * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
171 *
172 * Return: Zero if successful, or a negative error code on failure.
173 */
174int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
175{
176 struct snd_jack_kctl *jack_kctl;
177
178 jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
179 if (!jack_kctl)
180 return -ENOMEM;
181
182 snd_jack_kctl_add(jack, jack_kctl);
183 return 0;
184}
185EXPORT_SYMBOL(snd_jack_add_new_kctl);
186
Mark Browne76d8ce2008-07-28 19:05:35 +0100187/**
188 * snd_jack_new - Create a new jack
189 * @card: the card instance
190 * @id: an identifying string for this jack
191 * @type: a bitmask of enum snd_jack_type values that can be detected by
192 * this jack
193 * @jjack: Used to provide the allocated jack object to the caller.
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800194 * @initial_kctl: if true, create a kcontrol and add it to the jack list.
195 * @phantom_jack: Don't create a input device for phantom jacks.
Mark Browne76d8ce2008-07-28 19:05:35 +0100196 *
197 * Creates a new jack object.
198 *
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100199 * Return: Zero if successful, or a negative error code on failure.
200 * On success @jjack will be initialised.
Mark Browne76d8ce2008-07-28 19:05:35 +0100201 */
202int snd_jack_new(struct snd_card *card, const char *id, int type,
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800203 struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
Mark Browne76d8ce2008-07-28 19:05:35 +0100204{
205 struct snd_jack *jack;
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800206 struct snd_jack_kctl *jack_kctl = NULL;
Mark Browne76d8ce2008-07-28 19:05:35 +0100207 int err;
Mark Brownbd8a71a2009-01-03 16:56:56 +0000208 int i;
Mark Browne76d8ce2008-07-28 19:05:35 +0100209 static struct snd_device_ops ops = {
210 .dev_free = snd_jack_dev_free,
211 .dev_register = snd_jack_dev_register,
Takashi Iwai32b85442013-11-14 15:35:46 +0100212 .dev_disconnect = snd_jack_dev_disconnect,
Mark Browne76d8ce2008-07-28 19:05:35 +0100213 };
214
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800215 if (initial_kctl) {
216 jack_kctl = snd_jack_kctl_new(card, id, type);
217 if (!jack_kctl)
218 return -ENOMEM;
219 }
220
Mark Browne76d8ce2008-07-28 19:05:35 +0100221 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
222 if (jack == NULL)
223 return -ENOMEM;
224
Matthew Ranostay282cd762008-10-25 01:05:29 -0400225 jack->id = kstrdup(id, GFP_KERNEL);
Mark Browne76d8ce2008-07-28 19:05:35 +0100226
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800227 /* don't creat input device for phantom jack */
228 if (!phantom_jack) {
229 jack->input_dev = input_allocate_device();
230 if (jack->input_dev == NULL) {
231 err = -ENOMEM;
232 goto fail_input;
233 }
234
235 jack->input_dev->phys = "ALSA";
236
237 jack->type = type;
238
239 for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
240 if (type & (1 << i))
241 input_set_capability(jack->input_dev, EV_SW,
242 jack_switch_types[i]);
243
Mark Browne76d8ce2008-07-28 19:05:35 +0100244 }
245
Mark Browne76d8ce2008-07-28 19:05:35 +0100246 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
247 if (err < 0)
248 goto fail_input;
249
Jie Yang9058cbe2015-04-27 21:20:56 +0800250 jack->card = card;
251 INIT_LIST_HEAD(&jack->kctl_list);
252
Jie Yang4e3f0dc2015-04-27 21:20:58 +0800253 if (initial_kctl)
254 snd_jack_kctl_add(jack, jack_kctl);
255
Mark Browne76d8ce2008-07-28 19:05:35 +0100256 *jjack = jack;
257
258 return 0;
259
260fail_input:
261 input_free_device(jack->input_dev);
Lu Guanquneeda276b2011-02-21 13:45:04 +0800262 kfree(jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +0100263 kfree(jack);
264 return err;
265}
266EXPORT_SYMBOL(snd_jack_new);
267
268/**
269 * snd_jack_set_parent - Set the parent device for a jack
270 *
271 * @jack: The jack to configure
272 * @parent: The device to set as parent for the jack.
273 *
Mark Browna2e888f2012-05-07 17:10:21 +0100274 * Set the parent for the jack devices in the device tree. This
Mark Browne76d8ce2008-07-28 19:05:35 +0100275 * function is only valid prior to registration of the jack. If no
276 * parent is configured then the parent device will be the sound card.
277 */
278void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
279{
280 WARN_ON(jack->registered);
281
282 jack->input_dev->dev.parent = parent;
283}
284EXPORT_SYMBOL(snd_jack_set_parent);
285
286/**
Mark Brownebb812c2010-03-17 18:07:12 +0000287 * snd_jack_set_key - Set a key mapping on a jack
288 *
289 * @jack: The jack to configure
290 * @type: Jack report type for this key
291 * @keytype: Input layer key type to be reported
292 *
293 * Map a SND_JACK_BTN_ button type to an input layer key, allowing
294 * reporting of keys on accessories via the jack abstraction. If no
295 * mapping is provided but keys are enabled in the jack type then
296 * BTN_n numeric buttons will be reported.
297 *
Mark Browna2e888f2012-05-07 17:10:21 +0100298 * If jacks are not reporting via the input API this call will have no
299 * effect.
300 *
Mark Brownebb812c2010-03-17 18:07:12 +0000301 * Note that this is intended to be use by simple devices with small
302 * numbers of keys that can be reported. It is also possible to
303 * access the input device directly - devices with complex input
304 * capabilities on accessories should consider doing this rather than
305 * using this abstraction.
306 *
307 * This function may only be called prior to registration of the jack.
Yacine Belkadieb7c06e2013-03-11 22:05:14 +0100308 *
309 * Return: Zero if successful, or a negative error code on failure.
Mark Brownebb812c2010-03-17 18:07:12 +0000310 */
311int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
312 int keytype)
313{
314 int key = fls(SND_JACK_BTN_0) - fls(type);
315
316 WARN_ON(jack->registered);
317
318 if (!keytype || key >= ARRAY_SIZE(jack->key))
319 return -EINVAL;
320
321 jack->type |= type;
322 jack->key[key] = keytype;
323
324 return 0;
325}
326EXPORT_SYMBOL(snd_jack_set_key);
327
328/**
Mark Browne76d8ce2008-07-28 19:05:35 +0100329 * snd_jack_report - Report the current status of a jack
330 *
331 * @jack: The jack to report status for
332 * @status: The current status of the jack
333 */
334void snd_jack_report(struct snd_jack *jack, int status)
335{
Jie Yang9058cbe2015-04-27 21:20:56 +0800336 struct snd_jack_kctl *jack_kctl;
Mark Brownbd8a71a2009-01-03 16:56:56 +0000337 int i;
338
Mark Brown9a3f3712008-10-15 17:07:47 +0100339 if (!jack)
340 return;
341
Jie Yang6ed94952015-04-30 20:22:46 +0800342 list_for_each_entry(jack_kctl, &jack->kctl_list, list)
343 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
344 status & jack_kctl->mask_bits);
345
346 if (!jack->input_dev)
347 return;
348
Mark Brownebb812c2010-03-17 18:07:12 +0000349 for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
350 int testbit = SND_JACK_BTN_0 >> i;
351
352 if (jack->type & testbit)
353 input_report_key(jack->input_dev, jack->key[i],
354 status & testbit);
355 }
356
Mark Brown1c6e5552010-03-17 15:36:38 +0000357 for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
Mark Brownbd8a71a2009-01-03 16:56:56 +0000358 int testbit = 1 << i;
359 if (jack->type & testbit)
Mark Brown1c6e5552010-03-17 15:36:38 +0000360 input_report_switch(jack->input_dev,
361 jack_switch_types[i],
Mark Brownbd8a71a2009-01-03 16:56:56 +0000362 status & testbit);
363 }
Mark Browne76d8ce2008-07-28 19:05:35 +0100364
365 input_sync(jack->input_dev);
Jie Yang9058cbe2015-04-27 21:20:56 +0800366
Mark Browne76d8ce2008-07-28 19:05:35 +0100367}
368EXPORT_SYMBOL(snd_jack_report);
369
370MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
371MODULE_DESCRIPTION("Jack detection support for ALSA");
372MODULE_LICENSE("GPL");