• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
snd_jack_dev_disconnect(struct snd_device * device)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 
snd_jack_dev_free(struct snd_device * device)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 	down_write(&card->controls_rwsem);
68 	list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
69 		list_del_init(&jack_kctl->list);
70 		snd_ctl_remove(card, jack_kctl->kctl);
71 	}
72 	up_write(&card->controls_rwsem);
73 
74 	if (jack->private_free)
75 		jack->private_free(jack);
76 
77 	snd_jack_dev_disconnect(device);
78 
79 	kfree(jack->id);
80 	kfree(jack);
81 
82 	return 0;
83 }
84 
snd_jack_dev_register(struct snd_device * device)85 static int snd_jack_dev_register(struct snd_device *device)
86 {
87 	struct snd_jack *jack = device->device_data;
88 	struct snd_card *card = device->card;
89 	int err, i;
90 
91 	snprintf(jack->name, sizeof(jack->name), "%s %s",
92 		 card->shortname, jack->id);
93 
94 	if (!jack->input_dev)
95 		return 0;
96 
97 	jack->input_dev->name = jack->name;
98 
99 	/* Default to the sound card device. */
100 	if (!jack->input_dev->dev.parent)
101 		jack->input_dev->dev.parent = snd_card_get_device_link(card);
102 
103 	/* Add capabilities for any keys that are enabled */
104 	for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
105 		int testbit = SND_JACK_BTN_0 >> i;
106 
107 		if (!(jack->type & testbit))
108 			continue;
109 
110 		if (!jack->key[i])
111 			jack->key[i] = BTN_0 + i;
112 
113 		input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
114 	}
115 
116 	err = input_register_device(jack->input_dev);
117 	if (err == 0)
118 		jack->registered = 1;
119 
120 	return err;
121 }
122 
snd_jack_kctl_private_free(struct snd_kcontrol * kctl)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 
snd_jack_kctl_add(struct snd_jack * jack,struct snd_jack_kctl * jack_kctl)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 
snd_jack_kctl_new(struct snd_card * card,const char * name,unsigned int mask)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  */
snd_jack_add_new_kctl(struct snd_jack * jack,const char * name,int mask)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  */
snd_jack_new(struct snd_card * card,const char * id,int type,struct snd_jack ** jjack,bool initial_kctl,bool phantom_jack)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 	int i;
216 	static struct snd_device_ops ops = {
217 		.dev_free = snd_jack_dev_free,
218 		.dev_register = snd_jack_dev_register,
219 		.dev_disconnect = snd_jack_dev_disconnect,
220 	};
221 
222 	if (initial_kctl) {
223 		jack_kctl = snd_jack_kctl_new(card, id, type);
224 		if (!jack_kctl)
225 			return -ENOMEM;
226 	}
227 
228 	jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
229 	if (jack == NULL)
230 		return -ENOMEM;
231 
232 	jack->id = kstrdup(id, GFP_KERNEL);
233 	if (jack->id == NULL) {
234 		kfree(jack);
235 		return -ENOMEM;
236 	}
237 
238 	/* don't creat input device for phantom jack */
239 	if (!phantom_jack) {
240 		jack->input_dev = input_allocate_device();
241 		if (jack->input_dev == NULL) {
242 			err = -ENOMEM;
243 			goto fail_input;
244 		}
245 
246 		jack->input_dev->phys = "ALSA";
247 
248 		jack->type = type;
249 
250 		for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
251 			if (type & (1 << i))
252 				input_set_capability(jack->input_dev, EV_SW,
253 						     jack_switch_types[i]);
254 
255 	}
256 
257 	err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
258 	if (err < 0)
259 		goto fail_input;
260 
261 	jack->card = card;
262 	INIT_LIST_HEAD(&jack->kctl_list);
263 
264 	if (initial_kctl)
265 		snd_jack_kctl_add(jack, jack_kctl);
266 
267 	*jjack = jack;
268 
269 	return 0;
270 
271 fail_input:
272 	input_free_device(jack->input_dev);
273 	kfree(jack->id);
274 	kfree(jack);
275 	return err;
276 }
277 EXPORT_SYMBOL(snd_jack_new);
278 
279 /**
280  * snd_jack_set_parent - Set the parent device for a jack
281  *
282  * @jack:   The jack to configure
283  * @parent: The device to set as parent for the jack.
284  *
285  * Set the parent for the jack devices in the device tree.  This
286  * function is only valid prior to registration of the jack.  If no
287  * parent is configured then the parent device will be the sound card.
288  */
snd_jack_set_parent(struct snd_jack * jack,struct device * parent)289 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
290 {
291 	WARN_ON(jack->registered);
292 	if (!jack->input_dev)
293 		return;
294 
295 	jack->input_dev->dev.parent = parent;
296 }
297 EXPORT_SYMBOL(snd_jack_set_parent);
298 
299 /**
300  * snd_jack_set_key - Set a key mapping on a jack
301  *
302  * @jack:    The jack to configure
303  * @type:    Jack report type for this key
304  * @keytype: Input layer key type to be reported
305  *
306  * Map a SND_JACK_BTN_ button type to an input layer key, allowing
307  * reporting of keys on accessories via the jack abstraction.  If no
308  * mapping is provided but keys are enabled in the jack type then
309  * BTN_n numeric buttons will be reported.
310  *
311  * If jacks are not reporting via the input API this call will have no
312  * effect.
313  *
314  * Note that this is intended to be use by simple devices with small
315  * numbers of keys that can be reported.  It is also possible to
316  * access the input device directly - devices with complex input
317  * capabilities on accessories should consider doing this rather than
318  * using this abstraction.
319  *
320  * This function may only be called prior to registration of the jack.
321  *
322  * Return: Zero if successful, or a negative error code on failure.
323  */
snd_jack_set_key(struct snd_jack * jack,enum snd_jack_types type,int keytype)324 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
325 		     int keytype)
326 {
327 	int key = fls(SND_JACK_BTN_0) - fls(type);
328 
329 	WARN_ON(jack->registered);
330 
331 	if (!keytype || key >= ARRAY_SIZE(jack->key))
332 		return -EINVAL;
333 
334 	jack->type |= type;
335 	jack->key[key] = keytype;
336 
337 	return 0;
338 }
339 EXPORT_SYMBOL(snd_jack_set_key);
340 
341 /**
342  * snd_jack_report - Report the current status of a jack
343  *
344  * @jack:   The jack to report status for
345  * @status: The current status of the jack
346  */
snd_jack_report(struct snd_jack * jack,int status)347 void snd_jack_report(struct snd_jack *jack, int status)
348 {
349 	struct snd_jack_kctl *jack_kctl;
350 	int i;
351 
352 	if (!jack)
353 		return;
354 
355 	list_for_each_entry(jack_kctl, &jack->kctl_list, list)
356 		snd_kctl_jack_report(jack->card, jack_kctl->kctl,
357 					    status & jack_kctl->mask_bits);
358 
359 	if (!jack->input_dev)
360 		return;
361 
362 	for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
363 		int testbit = SND_JACK_BTN_0 >> i;
364 
365 		if (jack->type & testbit)
366 			input_report_key(jack->input_dev, jack->key[i],
367 					 status & testbit);
368 	}
369 
370 	for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
371 		int testbit = 1 << i;
372 		if (jack->type & testbit)
373 			input_report_switch(jack->input_dev,
374 					    jack_switch_types[i],
375 					    status & testbit);
376 	}
377 
378 	input_sync(jack->input_dev);
379 
380 }
381 EXPORT_SYMBOL(snd_jack_report);
382