ALSA: Jack: handle jack embedded kcontrol creating within ctljack
[firefly-linux-kernel-4.4.55.git] / sound / core / jack.c
1 /*
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>
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>
28
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 */
33 };
34
35 static int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
36         SW_HEADPHONE_INSERT,
37         SW_MICROPHONE_INSERT,
38         SW_LINEOUT_INSERT,
39         SW_JACK_PHYSICAL_INSERT,
40         SW_VIDEOOUT_INSERT,
41         SW_LINEIN_INSERT,
42 };
43
44 static int snd_jack_dev_disconnect(struct snd_device *device)
45 {
46         struct snd_jack *jack = device->device_data;
47
48         if (!jack->input_dev)
49                 return 0;
50
51         /* 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);
57         jack->input_dev = NULL;
58         return 0;
59 }
60
61 static int snd_jack_dev_free(struct snd_device *device)
62 {
63         struct snd_jack *jack = device->device_data;
64         struct snd_card *card = device->card;
65         struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
66
67         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         }
71         if (jack->private_free)
72                 jack->private_free(jack);
73
74         snd_jack_dev_disconnect(device);
75
76         kfree(jack->id);
77         kfree(jack);
78
79         return 0;
80 }
81
82 static 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;
86         int err, i;
87
88         snprintf(jack->name, sizeof(jack->name), "%s %s",
89                  card->shortname, jack->id);
90         jack->input_dev->name = jack->name;
91
92         /* Default to the sound card device. */
93         if (!jack->input_dev->dev.parent)
94                 jack->input_dev->dev.parent = snd_card_get_device_link(card);
95
96         /* 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
109         err = input_register_device(jack->input_dev);
110         if (err == 0)
111                 jack->registered = 1;
112
113         return err;
114 }
115
116 static 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
127 static 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
132 static 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
138         kctl = snd_kctl_jack_new(name, card, card);
139         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;
158 error:
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  */
174 int 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 }
185 EXPORT_SYMBOL(snd_jack_add_new_kctl);
186
187 /**
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.
194  *
195  * Creates a new jack object.
196  *
197  * Return: Zero if successful, or a negative error code on failure.
198  * On success @jjack will be initialised.
199  */
200 int snd_jack_new(struct snd_card *card, const char *id, int type,
201                  struct snd_jack **jjack)
202 {
203         struct snd_jack *jack;
204         int err;
205         int i;
206         static struct snd_device_ops ops = {
207                 .dev_free = snd_jack_dev_free,
208                 .dev_register = snd_jack_dev_register,
209                 .dev_disconnect = snd_jack_dev_disconnect,
210         };
211
212         jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
213         if (jack == NULL)
214                 return -ENOMEM;
215
216         jack->id = kstrdup(id, GFP_KERNEL);
217
218         jack->input_dev = input_allocate_device();
219         if (jack->input_dev == NULL) {
220                 err = -ENOMEM;
221                 goto fail_input;
222         }
223
224         jack->input_dev->phys = "ALSA";
225
226         jack->type = type;
227
228         for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
229                 if (type & (1 << i))
230                         input_set_capability(jack->input_dev, EV_SW,
231                                              jack_switch_types[i]);
232
233         err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
234         if (err < 0)
235                 goto fail_input;
236
237         jack->card = card;
238         INIT_LIST_HEAD(&jack->kctl_list);
239
240         *jjack = jack;
241
242         return 0;
243
244 fail_input:
245         input_free_device(jack->input_dev);
246         kfree(jack->id);
247         kfree(jack);
248         return err;
249 }
250 EXPORT_SYMBOL(snd_jack_new);
251
252 /**
253  * snd_jack_set_parent - Set the parent device for a jack
254  *
255  * @jack:   The jack to configure
256  * @parent: The device to set as parent for the jack.
257  *
258  * Set the parent for the jack devices in the device tree.  This
259  * function is only valid prior to registration of the jack.  If no
260  * parent is configured then the parent device will be the sound card.
261  */
262 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
263 {
264         WARN_ON(jack->registered);
265
266         jack->input_dev->dev.parent = parent;
267 }
268 EXPORT_SYMBOL(snd_jack_set_parent);
269
270 /**
271  * snd_jack_set_key - Set a key mapping on a jack
272  *
273  * @jack:    The jack to configure
274  * @type:    Jack report type for this key
275  * @keytype: Input layer key type to be reported
276  *
277  * Map a SND_JACK_BTN_ button type to an input layer key, allowing
278  * reporting of keys on accessories via the jack abstraction.  If no
279  * mapping is provided but keys are enabled in the jack type then
280  * BTN_n numeric buttons will be reported.
281  *
282  * If jacks are not reporting via the input API this call will have no
283  * effect.
284  *
285  * Note that this is intended to be use by simple devices with small
286  * numbers of keys that can be reported.  It is also possible to
287  * access the input device directly - devices with complex input
288  * capabilities on accessories should consider doing this rather than
289  * using this abstraction.
290  *
291  * This function may only be called prior to registration of the jack.
292  *
293  * Return: Zero if successful, or a negative error code on failure.
294  */
295 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
296                      int keytype)
297 {
298         int key = fls(SND_JACK_BTN_0) - fls(type);
299
300         WARN_ON(jack->registered);
301
302         if (!keytype || key >= ARRAY_SIZE(jack->key))
303                 return -EINVAL;
304
305         jack->type |= type;
306         jack->key[key] = keytype;
307
308         return 0;
309 }
310 EXPORT_SYMBOL(snd_jack_set_key);
311
312 /**
313  * snd_jack_report - Report the current status of a jack
314  *
315  * @jack:   The jack to report status for
316  * @status: The current status of the jack
317  */
318 void snd_jack_report(struct snd_jack *jack, int status)
319 {
320         struct snd_jack_kctl *jack_kctl;
321         int i;
322
323         if (!jack)
324                 return;
325
326         for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
327                 int testbit = SND_JACK_BTN_0 >> i;
328
329                 if (jack->type & testbit)
330                         input_report_key(jack->input_dev, jack->key[i],
331                                          status & testbit);
332         }
333
334         for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
335                 int testbit = 1 << i;
336                 if (jack->type & testbit)
337                         input_report_switch(jack->input_dev,
338                                             jack_switch_types[i],
339                                             status & testbit);
340         }
341
342         input_sync(jack->input_dev);
343
344         list_for_each_entry(jack_kctl, &jack->kctl_list, list)
345                 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
346                                             status & jack_kctl->mask_bits);
347
348 }
349 EXPORT_SYMBOL(snd_jack_report);
350
351 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
352 MODULE_DESCRIPTION("Jack detection support for ALSA");
353 MODULE_LICENSE("GPL");