GNU Linux-libre 5.4.257-gnu1
[releases.git] / sound / core / jack.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Jack abstraction layer
4  *
5  *  Copyright 2008 Wolfson Microelectronics
6  */
7
8 #include <linux/input.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <sound/jack.h>
12 #include <sound/core.h>
13 #include <sound/control.h>
14
15 struct snd_jack_kctl {
16         struct snd_kcontrol *kctl;
17         struct list_head list;  /* list of controls belong to the same jack */
18         unsigned int mask_bits; /* only masked status bits are reported via kctl */
19 };
20
21 #ifdef CONFIG_SND_JACK_INPUT_DEV
22 static int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
23         SW_HEADPHONE_INSERT,
24         SW_MICROPHONE_INSERT,
25         SW_LINEOUT_INSERT,
26         SW_JACK_PHYSICAL_INSERT,
27         SW_VIDEOOUT_INSERT,
28         SW_LINEIN_INSERT,
29 };
30 #endif /* CONFIG_SND_JACK_INPUT_DEV */
31
32 static int snd_jack_dev_disconnect(struct snd_device *device)
33 {
34 #ifdef CONFIG_SND_JACK_INPUT_DEV
35         struct snd_jack *jack = device->device_data;
36
37         mutex_lock(&jack->input_dev_lock);
38         if (!jack->input_dev) {
39                 mutex_unlock(&jack->input_dev_lock);
40                 return 0;
41         }
42
43         /* If the input device is registered with the input subsystem
44          * then we need to use a different deallocator. */
45         if (jack->registered)
46                 input_unregister_device(jack->input_dev);
47         else
48                 input_free_device(jack->input_dev);
49         jack->input_dev = NULL;
50         mutex_unlock(&jack->input_dev_lock);
51 #endif /* CONFIG_SND_JACK_INPUT_DEV */
52         return 0;
53 }
54
55 static int snd_jack_dev_free(struct snd_device *device)
56 {
57         struct snd_jack *jack = device->device_data;
58         struct snd_card *card = device->card;
59         struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
60
61         down_write(&card->controls_rwsem);
62         list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
63                 list_del_init(&jack_kctl->list);
64                 snd_ctl_remove(card, jack_kctl->kctl);
65         }
66         up_write(&card->controls_rwsem);
67
68         if (jack->private_free)
69                 jack->private_free(jack);
70
71         snd_jack_dev_disconnect(device);
72
73         kfree(jack->id);
74         kfree(jack);
75
76         return 0;
77 }
78
79 #ifdef CONFIG_SND_JACK_INPUT_DEV
80 static int snd_jack_dev_register(struct snd_device *device)
81 {
82         struct snd_jack *jack = device->device_data;
83         struct snd_card *card = device->card;
84         int err, i;
85
86         snprintf(jack->name, sizeof(jack->name), "%s %s",
87                  card->shortname, jack->id);
88
89         mutex_lock(&jack->input_dev_lock);
90         if (!jack->input_dev) {
91                 mutex_unlock(&jack->input_dev_lock);
92                 return 0;
93         }
94
95         jack->input_dev->name = jack->name;
96
97         /* Default to the sound card device. */
98         if (!jack->input_dev->dev.parent)
99                 jack->input_dev->dev.parent = snd_card_get_device_link(card);
100
101         /* Add capabilities for any keys that are enabled */
102         for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
103                 int testbit = SND_JACK_BTN_0 >> i;
104
105                 if (!(jack->type & testbit))
106                         continue;
107
108                 if (!jack->key[i])
109                         jack->key[i] = BTN_0 + i;
110
111                 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
112         }
113
114         err = input_register_device(jack->input_dev);
115         if (err == 0)
116                 jack->registered = 1;
117
118         mutex_unlock(&jack->input_dev_lock);
119         return err;
120 }
121 #endif /* CONFIG_SND_JACK_INPUT_DEV */
122
123 static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
124 {
125         struct snd_jack_kctl *jack_kctl;
126
127         jack_kctl = kctl->private_data;
128         if (jack_kctl) {
129                 list_del(&jack_kctl->list);
130                 kfree(jack_kctl);
131         }
132 }
133
134 static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
135 {
136         list_add_tail(&jack_kctl->list, &jack->kctl_list);
137 }
138
139 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
140 {
141         struct snd_kcontrol *kctl;
142         struct snd_jack_kctl *jack_kctl;
143         int err;
144
145         kctl = snd_kctl_jack_new(name, card);
146         if (!kctl)
147                 return NULL;
148
149         err = snd_ctl_add(card, kctl);
150         if (err < 0)
151                 return NULL;
152
153         jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
154
155         if (!jack_kctl)
156                 goto error;
157
158         jack_kctl->kctl = kctl;
159         jack_kctl->mask_bits = mask;
160
161         kctl->private_data = jack_kctl;
162         kctl->private_free = snd_jack_kctl_private_free;
163
164         return jack_kctl;
165 error:
166         snd_ctl_free_one(kctl);
167         return NULL;
168 }
169
170 /**
171  * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
172  * @jack:  the jack instance which the kctl will attaching to
173  * @name:  the name for the snd_kcontrol object
174  * @mask:  a bitmask of enum snd_jack_type values that can be detected
175  *         by this snd_jack_kctl object.
176  *
177  * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
178  *
179  * Return: Zero if successful, or a negative error code on failure.
180  */
181 int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
182 {
183         struct snd_jack_kctl *jack_kctl;
184
185         jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
186         if (!jack_kctl)
187                 return -ENOMEM;
188
189         snd_jack_kctl_add(jack, jack_kctl);
190         return 0;
191 }
192 EXPORT_SYMBOL(snd_jack_add_new_kctl);
193
194 /**
195  * snd_jack_new - Create a new jack
196  * @card:  the card instance
197  * @id:    an identifying string for this jack
198  * @type:  a bitmask of enum snd_jack_type values that can be detected by
199  *         this jack
200  * @jjack: Used to provide the allocated jack object to the caller.
201  * @initial_kctl: if true, create a kcontrol and add it to the jack list.
202  * @phantom_jack: Don't create a input device for phantom jacks.
203  *
204  * Creates a new jack object.
205  *
206  * Return: Zero if successful, or a negative error code on failure.
207  * On success @jjack will be initialised.
208  */
209 int snd_jack_new(struct snd_card *card, const char *id, int type,
210                  struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
211 {
212         struct snd_jack *jack;
213         struct snd_jack_kctl *jack_kctl = NULL;
214         int err;
215         static struct snd_device_ops ops = {
216                 .dev_free = snd_jack_dev_free,
217 #ifdef CONFIG_SND_JACK_INPUT_DEV
218                 .dev_register = snd_jack_dev_register,
219                 .dev_disconnect = snd_jack_dev_disconnect,
220 #endif /* CONFIG_SND_JACK_INPUT_DEV */
221         };
222
223         if (initial_kctl) {
224                 jack_kctl = snd_jack_kctl_new(card, id, type);
225                 if (!jack_kctl)
226                         return -ENOMEM;
227         }
228
229         jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
230         if (jack == NULL)
231                 return -ENOMEM;
232
233         jack->id = kstrdup(id, GFP_KERNEL);
234         if (jack->id == NULL) {
235                 kfree(jack);
236                 return -ENOMEM;
237         }
238
239 #ifdef CONFIG_SND_JACK_INPUT_DEV
240         mutex_init(&jack->input_dev_lock);
241
242         /* don't create input device for phantom jack */
243         if (!phantom_jack) {
244                 int i;
245
246                 jack->input_dev = input_allocate_device();
247                 if (jack->input_dev == NULL) {
248                         err = -ENOMEM;
249                         goto fail_input;
250                 }
251
252                 jack->input_dev->phys = "ALSA";
253
254                 jack->type = type;
255
256                 for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
257                         if (type & (1 << i))
258                                 input_set_capability(jack->input_dev, EV_SW,
259                                                      jack_switch_types[i]);
260
261         }
262 #endif /* CONFIG_SND_JACK_INPUT_DEV */
263
264         err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
265         if (err < 0)
266                 goto fail_input;
267
268         jack->card = card;
269         INIT_LIST_HEAD(&jack->kctl_list);
270
271         if (initial_kctl)
272                 snd_jack_kctl_add(jack, jack_kctl);
273
274         *jjack = jack;
275
276         return 0;
277
278 fail_input:
279 #ifdef CONFIG_SND_JACK_INPUT_DEV
280         input_free_device(jack->input_dev);
281 #endif
282         kfree(jack->id);
283         kfree(jack);
284         return err;
285 }
286 EXPORT_SYMBOL(snd_jack_new);
287
288 #ifdef CONFIG_SND_JACK_INPUT_DEV
289 /**
290  * snd_jack_set_parent - Set the parent device for a jack
291  *
292  * @jack:   The jack to configure
293  * @parent: The device to set as parent for the jack.
294  *
295  * Set the parent for the jack devices in the device tree.  This
296  * function is only valid prior to registration of the jack.  If no
297  * parent is configured then the parent device will be the sound card.
298  */
299 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
300 {
301         WARN_ON(jack->registered);
302         mutex_lock(&jack->input_dev_lock);
303         if (!jack->input_dev) {
304                 mutex_unlock(&jack->input_dev_lock);
305                 return;
306         }
307
308         jack->input_dev->dev.parent = parent;
309         mutex_unlock(&jack->input_dev_lock);
310 }
311 EXPORT_SYMBOL(snd_jack_set_parent);
312
313 /**
314  * snd_jack_set_key - Set a key mapping on a jack
315  *
316  * @jack:    The jack to configure
317  * @type:    Jack report type for this key
318  * @keytype: Input layer key type to be reported
319  *
320  * Map a SND_JACK_BTN_* button type to an input layer key, allowing
321  * reporting of keys on accessories via the jack abstraction.  If no
322  * mapping is provided but keys are enabled in the jack type then
323  * BTN_n numeric buttons will be reported.
324  *
325  * If jacks are not reporting via the input API this call will have no
326  * effect.
327  *
328  * Note that this is intended to be use by simple devices with small
329  * numbers of keys that can be reported.  It is also possible to
330  * access the input device directly - devices with complex input
331  * capabilities on accessories should consider doing this rather than
332  * using this abstraction.
333  *
334  * This function may only be called prior to registration of the jack.
335  *
336  * Return: Zero if successful, or a negative error code on failure.
337  */
338 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
339                      int keytype)
340 {
341         int key = fls(SND_JACK_BTN_0) - fls(type);
342
343         WARN_ON(jack->registered);
344
345         if (!keytype || key >= ARRAY_SIZE(jack->key))
346                 return -EINVAL;
347
348         jack->type |= type;
349         jack->key[key] = keytype;
350         return 0;
351 }
352 EXPORT_SYMBOL(snd_jack_set_key);
353 #endif /* CONFIG_SND_JACK_INPUT_DEV */
354
355 /**
356  * snd_jack_report - Report the current status of a jack
357  * Note: This function uses mutexes and should be called from a
358  * context which can sleep (such as a workqueue).
359  *
360  * @jack:   The jack to report status for
361  * @status: The current status of the jack
362  */
363 void snd_jack_report(struct snd_jack *jack, int status)
364 {
365         struct snd_jack_kctl *jack_kctl;
366 #ifdef CONFIG_SND_JACK_INPUT_DEV
367         struct input_dev *idev;
368         int i;
369 #endif
370
371         if (!jack)
372                 return;
373
374         list_for_each_entry(jack_kctl, &jack->kctl_list, list)
375                 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
376                                             status & jack_kctl->mask_bits);
377
378 #ifdef CONFIG_SND_JACK_INPUT_DEV
379         idev = input_get_device(jack->input_dev);
380         if (!idev)
381                 return;
382
383         for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
384                 int testbit = SND_JACK_BTN_0 >> i;
385
386                 if (jack->type & testbit)
387                         input_report_key(idev, jack->key[i],
388                                          status & testbit);
389         }
390
391         for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
392                 int testbit = 1 << i;
393                 if (jack->type & testbit)
394                         input_report_switch(idev,
395                                             jack_switch_types[i],
396                                             status & testbit);
397         }
398
399         input_sync(idev);
400         input_put_device(idev);
401 #endif /* CONFIG_SND_JACK_INPUT_DEV */
402 }
403 EXPORT_SYMBOL(snd_jack_report);