GNU Linux-libre 4.9.309-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         if (!jack->input_dev)
52                 return 0;
53
54         /* If the input device is registered with the input subsystem
55          * then we need to use a different deallocator. */
56         if (jack->registered)
57                 input_unregister_device(jack->input_dev);
58         else
59                 input_free_device(jack->input_dev);
60         jack->input_dev = NULL;
61 #endif /* CONFIG_SND_JACK_INPUT_DEV */
62         return 0;
63 }
64
65 static int snd_jack_dev_free(struct snd_device *device)
66 {
67         struct snd_jack *jack = device->device_data;
68         struct snd_card *card = device->card;
69         struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
70
71         down_write(&card->controls_rwsem);
72         list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
73                 list_del_init(&jack_kctl->list);
74                 snd_ctl_remove(card, jack_kctl->kctl);
75         }
76         up_write(&card->controls_rwsem);
77
78         if (jack->private_free)
79                 jack->private_free(jack);
80
81         snd_jack_dev_disconnect(device);
82
83         kfree(jack->id);
84         kfree(jack);
85
86         return 0;
87 }
88
89 #ifdef CONFIG_SND_JACK_INPUT_DEV
90 static int snd_jack_dev_register(struct snd_device *device)
91 {
92         struct snd_jack *jack = device->device_data;
93         struct snd_card *card = device->card;
94         int err, i;
95
96         snprintf(jack->name, sizeof(jack->name), "%s %s",
97                  card->shortname, jack->id);
98
99         if (!jack->input_dev)
100                 return 0;
101
102         jack->input_dev->name = jack->name;
103
104         /* Default to the sound card device. */
105         if (!jack->input_dev->dev.parent)
106                 jack->input_dev->dev.parent = snd_card_get_device_link(card);
107
108         /* Add capabilities for any keys that are enabled */
109         for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
110                 int testbit = SND_JACK_BTN_0 >> i;
111
112                 if (!(jack->type & testbit))
113                         continue;
114
115                 if (!jack->key[i])
116                         jack->key[i] = BTN_0 + i;
117
118                 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
119         }
120
121         err = input_register_device(jack->input_dev);
122         if (err == 0)
123                 jack->registered = 1;
124
125         return err;
126 }
127 #endif /* CONFIG_SND_JACK_INPUT_DEV */
128
129 static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
130 {
131         struct snd_jack_kctl *jack_kctl;
132
133         jack_kctl = kctl->private_data;
134         if (jack_kctl) {
135                 list_del(&jack_kctl->list);
136                 kfree(jack_kctl);
137         }
138 }
139
140 static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
141 {
142         list_add_tail(&jack_kctl->list, &jack->kctl_list);
143 }
144
145 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
146 {
147         struct snd_kcontrol *kctl;
148         struct snd_jack_kctl *jack_kctl;
149         int err;
150
151         kctl = snd_kctl_jack_new(name, card);
152         if (!kctl)
153                 return NULL;
154
155         err = snd_ctl_add(card, kctl);
156         if (err < 0)
157                 return NULL;
158
159         jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
160
161         if (!jack_kctl)
162                 goto error;
163
164         jack_kctl->kctl = kctl;
165         jack_kctl->mask_bits = mask;
166
167         kctl->private_data = jack_kctl;
168         kctl->private_free = snd_jack_kctl_private_free;
169
170         return jack_kctl;
171 error:
172         snd_ctl_free_one(kctl);
173         return NULL;
174 }
175
176 /**
177  * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
178  * @jack:  the jack instance which the kctl will attaching to
179  * @name:  the name for the snd_kcontrol object
180  * @mask:  a bitmask of enum snd_jack_type values that can be detected
181  *         by this snd_jack_kctl object.
182  *
183  * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
184  *
185  * Return: Zero if successful, or a negative error code on failure.
186  */
187 int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
188 {
189         struct snd_jack_kctl *jack_kctl;
190
191         jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
192         if (!jack_kctl)
193                 return -ENOMEM;
194
195         snd_jack_kctl_add(jack, jack_kctl);
196         return 0;
197 }
198 EXPORT_SYMBOL(snd_jack_add_new_kctl);
199
200 /**
201  * snd_jack_new - Create a new jack
202  * @card:  the card instance
203  * @id:    an identifying string for this jack
204  * @type:  a bitmask of enum snd_jack_type values that can be detected by
205  *         this jack
206  * @jjack: Used to provide the allocated jack object to the caller.
207  * @initial_kctl: if true, create a kcontrol and add it to the jack list.
208  * @phantom_jack: Don't create a input device for phantom jacks.
209  *
210  * Creates a new jack object.
211  *
212  * Return: Zero if successful, or a negative error code on failure.
213  * On success @jjack will be initialised.
214  */
215 int snd_jack_new(struct snd_card *card, const char *id, int type,
216                  struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
217 {
218         struct snd_jack *jack;
219         struct snd_jack_kctl *jack_kctl = NULL;
220         int err;
221         static struct snd_device_ops ops = {
222                 .dev_free = snd_jack_dev_free,
223 #ifdef CONFIG_SND_JACK_INPUT_DEV
224                 .dev_register = snd_jack_dev_register,
225                 .dev_disconnect = snd_jack_dev_disconnect,
226 #endif /* CONFIG_SND_JACK_INPUT_DEV */
227         };
228
229         if (initial_kctl) {
230                 jack_kctl = snd_jack_kctl_new(card, id, type);
231                 if (!jack_kctl)
232                         return -ENOMEM;
233         }
234
235         jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
236         if (jack == NULL)
237                 return -ENOMEM;
238
239         jack->id = kstrdup(id, GFP_KERNEL);
240         if (jack->id == NULL) {
241                 kfree(jack);
242                 return -ENOMEM;
243         }
244
245         /* don't creat input device for phantom jack */
246         if (!phantom_jack) {
247 #ifdef CONFIG_SND_JACK_INPUT_DEV
248                 int i;
249
250                 jack->input_dev = input_allocate_device();
251                 if (jack->input_dev == NULL) {
252                         err = -ENOMEM;
253                         goto fail_input;
254                 }
255
256                 jack->input_dev->phys = "ALSA";
257
258                 jack->type = type;
259
260                 for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
261                         if (type & (1 << i))
262                                 input_set_capability(jack->input_dev, EV_SW,
263                                                      jack_switch_types[i]);
264
265 #endif /* CONFIG_SND_JACK_INPUT_DEV */
266         }
267
268         err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
269         if (err < 0)
270                 goto fail_input;
271
272         jack->card = card;
273         INIT_LIST_HEAD(&jack->kctl_list);
274
275         if (initial_kctl)
276                 snd_jack_kctl_add(jack, jack_kctl);
277
278         *jjack = jack;
279
280         return 0;
281
282 fail_input:
283 #ifdef CONFIG_SND_JACK_INPUT_DEV
284         input_free_device(jack->input_dev);
285 #endif
286         kfree(jack->id);
287         kfree(jack);
288         return err;
289 }
290 EXPORT_SYMBOL(snd_jack_new);
291
292 #ifdef CONFIG_SND_JACK_INPUT_DEV
293 /**
294  * snd_jack_set_parent - Set the parent device for a jack
295  *
296  * @jack:   The jack to configure
297  * @parent: The device to set as parent for the jack.
298  *
299  * Set the parent for the jack devices in the device tree.  This
300  * function is only valid prior to registration of the jack.  If no
301  * parent is configured then the parent device will be the sound card.
302  */
303 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
304 {
305         WARN_ON(jack->registered);
306         if (!jack->input_dev)
307                 return;
308
309         jack->input_dev->dev.parent = parent;
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  *
358  * @jack:   The jack to report status for
359  * @status: The current status of the jack
360  */
361 void snd_jack_report(struct snd_jack *jack, int status)
362 {
363         struct snd_jack_kctl *jack_kctl;
364 #ifdef CONFIG_SND_JACK_INPUT_DEV
365         int i;
366 #endif
367
368         if (!jack)
369                 return;
370
371         list_for_each_entry(jack_kctl, &jack->kctl_list, list)
372                 snd_kctl_jack_report(jack->card, jack_kctl->kctl,
373                                             status & jack_kctl->mask_bits);
374
375 #ifdef CONFIG_SND_JACK_INPUT_DEV
376         if (!jack->input_dev)
377                 return;
378
379         for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
380                 int testbit = SND_JACK_BTN_0 >> i;
381
382                 if (jack->type & testbit)
383                         input_report_key(jack->input_dev, jack->key[i],
384                                          status & testbit);
385         }
386
387         for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
388                 int testbit = 1 << i;
389                 if (jack->type & testbit)
390                         input_report_switch(jack->input_dev,
391                                             jack_switch_types[i],
392                                             status & testbit);
393         }
394
395         input_sync(jack->input_dev);
396 #endif /* CONFIG_SND_JACK_INPUT_DEV */
397 }
398 EXPORT_SYMBOL(snd_jack_report);