GNU Linux-libre 4.9.337-gnu1
[releases.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 #ifdef CONFIG_SND_JACK_INPUT_DEV
36 static int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
37         SW_HEADPHONE_INSERT,
38         SW_MICROPHONE_INSERT,
39         SW_LINEOUT_INSERT,
40         SW_JACK_PHYSICAL_INSERT,
41         SW_VIDEOOUT_INSERT,
42         SW_LINEIN_INSERT,
43 };
44 #endif /* CONFIG_SND_JACK_INPUT_DEV */
45
46 static int snd_jack_dev_disconnect(struct snd_device *device)
47 {
48 #ifdef CONFIG_SND_JACK_INPUT_DEV
49         struct snd_jack *jack = device->device_data;
50
51         mutex_lock(&jack->input_dev_lock);
52         if (!jack->input_dev) {
53                 mutex_unlock(&jack->input_dev_lock);
54                 return 0;
55         }
56
57         /* If the input device is registered with the input subsystem
58          * then we need to use a different deallocator. */
59         if (jack->registered)
60                 input_unregister_device(jack->input_dev);
61         else
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 */
66         return 0;
67 }
68
69 static int snd_jack_dev_free(struct snd_device *device)
70 {
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;
74
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);
79         }
80         up_write(&card->controls_rwsem);
81
82         if (jack->private_free)
83                 jack->private_free(jack);
84
85         snd_jack_dev_disconnect(device);
86
87         kfree(jack->id);
88         kfree(jack);
89
90         return 0;
91 }
92
93 #ifdef CONFIG_SND_JACK_INPUT_DEV
94 static int snd_jack_dev_register(struct snd_device *device)
95 {
96         struct snd_jack *jack = device->device_data;
97         struct snd_card *card = device->card;
98         int err, i;
99
100         snprintf(jack->name, sizeof(jack->name), "%s %s",
101                  card->shortname, jack->id);
102
103         mutex_lock(&jack->input_dev_lock);
104         if (!jack->input_dev) {
105                 mutex_unlock(&jack->input_dev_lock);
106                 return 0;
107         }
108
109         jack->input_dev->name = jack->name;
110
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);
114
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;
118
119                 if (!(jack->type & testbit))
120                         continue;
121
122                 if (!jack->key[i])
123                         jack->key[i] = BTN_0 + i;
124
125                 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
126         }
127
128         err = input_register_device(jack->input_dev);
129         if (err == 0)
130                 jack->registered = 1;
131
132         mutex_unlock(&jack->input_dev_lock);
133         return err;
134 }
135 #endif /* CONFIG_SND_JACK_INPUT_DEV */
136
137 static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
138 {
139         struct snd_jack_kctl *jack_kctl;
140
141         jack_kctl = kctl->private_data;
142         if (jack_kctl) {
143                 list_del(&jack_kctl->list);
144                 kfree(jack_kctl);
145         }
146 }
147
148 static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
149 {
150         list_add_tail(&jack_kctl->list, &jack->kctl_list);
151 }
152
153 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
154 {
155         struct snd_kcontrol *kctl;
156         struct snd_jack_kctl *jack_kctl;
157         int err;
158
159         kctl = snd_kctl_jack_new(name, card);
160         if (!kctl)
161                 return NULL;
162
163         err = snd_ctl_add(card, kctl);
164         if (err < 0)
165                 return NULL;
166
167         jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
168
169         if (!jack_kctl)
170                 goto error;
171
172         jack_kctl->kctl = kctl;
173         jack_kctl->mask_bits = mask;
174
175         kctl->private_data = jack_kctl;
176         kctl->private_free = snd_jack_kctl_private_free;
177
178         return jack_kctl;
179 error:
180         snd_ctl_free_one(kctl);
181         return NULL;
182 }
183
184 /**
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.
190  *
191  * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
192  *
193  * Return: Zero if successful, or a negative error code on failure.
194  */
195 int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
196 {
197         struct snd_jack_kctl *jack_kctl;
198
199         jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
200         if (!jack_kctl)
201                 return -ENOMEM;
202
203         snd_jack_kctl_add(jack, jack_kctl);
204         return 0;
205 }
206 EXPORT_SYMBOL(snd_jack_add_new_kctl);
207
208 /**
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
213  *         this jack
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.
217  *
218  * Creates a new jack object.
219  *
220  * Return: Zero if successful, or a negative error code on failure.
221  * On success @jjack will be initialised.
222  */
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)
225 {
226         struct snd_jack *jack;
227         struct snd_jack_kctl *jack_kctl = NULL;
228         int err;
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 */
235         };
236
237         if (initial_kctl) {
238                 jack_kctl = snd_jack_kctl_new(card, id, type);
239                 if (!jack_kctl)
240                         return -ENOMEM;
241         }
242
243         jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
244         if (jack == NULL)
245                 return -ENOMEM;
246
247         jack->id = kstrdup(id, GFP_KERNEL);
248         if (jack->id == NULL) {
249                 kfree(jack);
250                 return -ENOMEM;
251         }
252
253 #ifdef CONFIG_SND_JACK_INPUT_DEV
254         mutex_init(&jack->input_dev_lock);
255
256         /* don't create input device for phantom jack */
257         if (!phantom_jack) {
258                 int i;
259
260                 jack->input_dev = input_allocate_device();
261                 if (jack->input_dev == NULL) {
262                         err = -ENOMEM;
263                         goto fail_input;
264                 }
265
266                 jack->input_dev->phys = "ALSA";
267
268                 jack->type = type;
269
270                 for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
271                         if (type & (1 << i))
272                                 input_set_capability(jack->input_dev, EV_SW,
273                                                      jack_switch_types[i]);
274
275         }
276 #endif /* CONFIG_SND_JACK_INPUT_DEV */
277
278         err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
279         if (err < 0)
280                 goto fail_input;
281
282         jack->card = card;
283         INIT_LIST_HEAD(&jack->kctl_list);
284
285         if (initial_kctl)
286                 snd_jack_kctl_add(jack, jack_kctl);
287
288         *jjack = jack;
289
290         return 0;
291
292 fail_input:
293 #ifdef CONFIG_SND_JACK_INPUT_DEV
294         input_free_device(jack->input_dev);
295 #endif
296         kfree(jack->id);
297         kfree(jack);
298         return err;
299 }
300 EXPORT_SYMBOL(snd_jack_new);
301
302 #ifdef CONFIG_SND_JACK_INPUT_DEV
303 /**
304  * snd_jack_set_parent - Set the parent device for a jack
305  *
306  * @jack:   The jack to configure
307  * @parent: The device to set as parent for the jack.
308  *
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.
312  */
313 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
314 {
315         WARN_ON(jack->registered);
316         mutex_lock(&jack->input_dev_lock);
317         if (!jack->input_dev) {
318                 mutex_unlock(&jack->input_dev_lock);
319                 return;
320         }
321
322         jack->input_dev->dev.parent = parent;
323         mutex_unlock(&jack->input_dev_lock);
324 }
325 EXPORT_SYMBOL(snd_jack_set_parent);
326
327 /**
328  * snd_jack_set_key - Set a key mapping on a jack
329  *
330  * @jack:    The jack to configure
331  * @type:    Jack report type for this key
332  * @keytype: Input layer key type to be reported
333  *
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.
338  *
339  * If jacks are not reporting via the input API this call will have no
340  * effect.
341  *
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.
347  *
348  * This function may only be called prior to registration of the jack.
349  *
350  * Return: Zero if successful, or a negative error code on failure.
351  */
352 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
353                      int keytype)
354 {
355         int key = fls(SND_JACK_BTN_0) - fls(type);
356
357         WARN_ON(jack->registered);
358
359         if (!keytype || key >= ARRAY_SIZE(jack->key))
360                 return -EINVAL;
361
362         jack->type |= type;
363         jack->key[key] = keytype;
364         return 0;
365 }
366 EXPORT_SYMBOL(snd_jack_set_key);
367 #endif /* CONFIG_SND_JACK_INPUT_DEV */
368
369 /**
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).
373  *
374  * @jack:   The jack to report status for
375  * @status: The current status of the jack
376  */
377 void snd_jack_report(struct snd_jack *jack, int status)
378 {
379         struct snd_jack_kctl *jack_kctl;
380 #ifdef CONFIG_SND_JACK_INPUT_DEV
381         int i;
382 #endif
383
384         if (!jack)
385                 return;
386
387         list_for_each_entry(jack_kctl, &jack->kctl_list, list)
388                 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
389                                             status & jack_kctl->mask_bits);
390
391 #ifdef CONFIG_SND_JACK_INPUT_DEV
392         mutex_lock(&jack->input_dev_lock);
393         if (!jack->input_dev) {
394                 mutex_unlock(&jack->input_dev_lock);
395                 return;
396         }
397
398         for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
399                 int testbit = SND_JACK_BTN_0 >> i;
400
401                 if (jack->type & testbit)
402                         input_report_key(jack->input_dev, jack->key[i],
403                                          status & testbit);
404         }
405
406         for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
407                 int testbit = 1 << i;
408                 if (jack->type & testbit)
409                         input_report_switch(jack->input_dev,
410                                             jack_switch_types[i],
411                                             status & testbit);
412         }
413
414         input_sync(jack->input_dev);
415         mutex_unlock(&jack->input_dev_lock);
416 #endif /* CONFIG_SND_JACK_INPUT_DEV */
417 }
418 EXPORT_SYMBOL(snd_jack_report);