ALSA: Add support for key reporting via the jack interface
[pandora-kernel.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 <sound/jack.h>
24 #include <sound/core.h>
25
26 static int jack_switch_types[] = {
27         SW_HEADPHONE_INSERT,
28         SW_MICROPHONE_INSERT,
29         SW_LINEOUT_INSERT,
30         SW_JACK_PHYSICAL_INSERT,
31         SW_VIDEOOUT_INSERT,
32 };
33
34 static int snd_jack_dev_free(struct snd_device *device)
35 {
36         struct snd_jack *jack = device->device_data;
37
38         if (jack->private_free)
39                 jack->private_free(jack);
40
41         /* If the input device is registered with the input subsystem
42          * then we need to use a different deallocator. */
43         if (jack->registered)
44                 input_unregister_device(jack->input_dev);
45         else
46                 input_free_device(jack->input_dev);
47
48         kfree(jack->id);
49         kfree(jack);
50
51         return 0;
52 }
53
54 static int snd_jack_dev_register(struct snd_device *device)
55 {
56         struct snd_jack *jack = device->device_data;
57         struct snd_card *card = device->card;
58         int err, i;
59
60         snprintf(jack->name, sizeof(jack->name), "%s %s",
61                  card->shortname, jack->id);
62         jack->input_dev->name = jack->name;
63
64         /* Default to the sound card device. */
65         if (!jack->input_dev->dev.parent)
66                 jack->input_dev->dev.parent = snd_card_get_device_link(card);
67
68         /* Add capabilities for any keys that are enabled */
69         for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
70                 int testbit = SND_JACK_BTN_0 >> i;
71
72                 if (!(jack->type & testbit))
73                         continue;
74
75                 if (!jack->key[i])
76                         jack->key[i] = BTN_0 + i;
77
78                 input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
79         }
80
81         err = input_register_device(jack->input_dev);
82         if (err == 0)
83                 jack->registered = 1;
84
85         return err;
86 }
87
88 /**
89  * snd_jack_new - Create a new jack
90  * @card:  the card instance
91  * @id:    an identifying string for this jack
92  * @type:  a bitmask of enum snd_jack_type values that can be detected by
93  *         this jack
94  * @jjack: Used to provide the allocated jack object to the caller.
95  *
96  * Creates a new jack object.
97  *
98  * Returns zero if successful, or a negative error code on failure.
99  * On success jjack will be initialised.
100  */
101 int snd_jack_new(struct snd_card *card, const char *id, int type,
102                  struct snd_jack **jjack)
103 {
104         struct snd_jack *jack;
105         int err;
106         int i;
107         static struct snd_device_ops ops = {
108                 .dev_free = snd_jack_dev_free,
109                 .dev_register = snd_jack_dev_register,
110         };
111
112         jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
113         if (jack == NULL)
114                 return -ENOMEM;
115
116         jack->id = kstrdup(id, GFP_KERNEL);
117
118         jack->input_dev = input_allocate_device();
119         if (jack->input_dev == NULL) {
120                 err = -ENOMEM;
121                 goto fail_input;
122         }
123
124         jack->input_dev->phys = "ALSA";
125
126         jack->type = type;
127
128         for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++)
129                 if (type & (1 << i))
130                         input_set_capability(jack->input_dev, EV_SW,
131                                              jack_switch_types[i]);
132
133         err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
134         if (err < 0)
135                 goto fail_input;
136
137         *jjack = jack;
138
139         return 0;
140
141 fail_input:
142         input_free_device(jack->input_dev);
143         kfree(jack);
144         return err;
145 }
146 EXPORT_SYMBOL(snd_jack_new);
147
148 /**
149  * snd_jack_set_parent - Set the parent device for a jack
150  *
151  * @jack:   The jack to configure
152  * @parent: The device to set as parent for the jack.
153  *
154  * Set the parent for the jack input device in the device tree.  This
155  * function is only valid prior to registration of the jack.  If no
156  * parent is configured then the parent device will be the sound card.
157  */
158 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
159 {
160         WARN_ON(jack->registered);
161
162         jack->input_dev->dev.parent = parent;
163 }
164 EXPORT_SYMBOL(snd_jack_set_parent);
165
166 /**
167  * snd_jack_set_key - Set a key mapping on a jack
168  *
169  * @jack:    The jack to configure
170  * @type:    Jack report type for this key
171  * @keytype: Input layer key type to be reported
172  *
173  * Map a SND_JACK_BTN_ button type to an input layer key, allowing
174  * reporting of keys on accessories via the jack abstraction.  If no
175  * mapping is provided but keys are enabled in the jack type then
176  * BTN_n numeric buttons will be reported.
177  *
178  * Note that this is intended to be use by simple devices with small
179  * numbers of keys that can be reported.  It is also possible to
180  * access the input device directly - devices with complex input
181  * capabilities on accessories should consider doing this rather than
182  * using this abstraction.
183  *
184  * This function may only be called prior to registration of the jack.
185  */
186 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
187                      int keytype)
188 {
189         int key = fls(SND_JACK_BTN_0) - fls(type);
190
191         WARN_ON(jack->registered);
192
193         if (!keytype || key >= ARRAY_SIZE(jack->key))
194                 return -EINVAL;
195
196         jack->type |= type;
197         jack->key[key] = keytype;
198
199         return 0;
200 }
201 EXPORT_SYMBOL(snd_jack_set_key);
202
203 /**
204  * snd_jack_report - Report the current status of a jack
205  *
206  * @jack:   The jack to report status for
207  * @status: The current status of the jack
208  */
209 void snd_jack_report(struct snd_jack *jack, int status)
210 {
211         int i;
212
213         if (!jack)
214                 return;
215
216         for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
217                 int testbit = SND_JACK_BTN_0 >> i;
218
219                 if (jack->type & testbit)
220                         input_report_key(jack->input_dev, jack->key[i],
221                                          status & testbit);
222         }
223
224         for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
225                 int testbit = 1 << i;
226                 if (jack->type & testbit)
227                         input_report_switch(jack->input_dev,
228                                             jack_switch_types[i],
229                                             status & testbit);
230         }
231
232         input_sync(jack->input_dev);
233 }
234 EXPORT_SYMBOL(snd_jack_report);
235
236 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
237 MODULE_DESCRIPTION("Jack detection support for ALSA");
238 MODULE_LICENSE("GPL");