blob: b2da10c9916a5d4e666079f1f3d9688637dd5e46 [file] [log] [blame]
Mark Browne76d8ce2008-07-28 19:05:35 +01001/*
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
Mark Brownbd8a71a2009-01-03 16:56:56 +000026static int jack_types[] = {
27 SW_HEADPHONE_INSERT,
28 SW_MICROPHONE_INSERT,
29 SW_LINEOUT_INSERT,
30 SW_JACK_PHYSICAL_INSERT,
31};
32
Mark Browne76d8ce2008-07-28 19:05:35 +010033static int snd_jack_dev_free(struct snd_device *device)
34{
35 struct snd_jack *jack = device->device_data;
36
37 /* If the input device is registered with the input subsystem
38 * then we need to use a different deallocator. */
39 if (jack->registered)
40 input_unregister_device(jack->input_dev);
41 else
42 input_free_device(jack->input_dev);
43
Matthew Ranostay282cd762008-10-25 01:05:29 -040044 kfree(jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +010045 kfree(jack);
46
47 return 0;
48}
49
50static int snd_jack_dev_register(struct snd_device *device)
51{
52 struct snd_jack *jack = device->device_data;
53 struct snd_card *card = device->card;
54 int err;
55
56 snprintf(jack->name, sizeof(jack->name), "%s %s",
57 card->longname, jack->id);
58 jack->input_dev->name = jack->name;
59
60 /* Default to the sound card device. */
61 if (!jack->input_dev->dev.parent)
62 jack->input_dev->dev.parent = card->dev;
63
64 err = input_register_device(jack->input_dev);
65 if (err == 0)
66 jack->registered = 1;
67
68 return err;
69}
70
71/**
72 * snd_jack_new - Create a new jack
73 * @card: the card instance
74 * @id: an identifying string for this jack
75 * @type: a bitmask of enum snd_jack_type values that can be detected by
76 * this jack
77 * @jjack: Used to provide the allocated jack object to the caller.
78 *
79 * Creates a new jack object.
80 *
81 * Returns zero if successful, or a negative error code on failure.
82 * On success jjack will be initialised.
83 */
84int snd_jack_new(struct snd_card *card, const char *id, int type,
85 struct snd_jack **jjack)
86{
87 struct snd_jack *jack;
88 int err;
Mark Brownbd8a71a2009-01-03 16:56:56 +000089 int i;
Mark Browne76d8ce2008-07-28 19:05:35 +010090 static struct snd_device_ops ops = {
91 .dev_free = snd_jack_dev_free,
92 .dev_register = snd_jack_dev_register,
93 };
94
95 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
96 if (jack == NULL)
97 return -ENOMEM;
98
Matthew Ranostay282cd762008-10-25 01:05:29 -040099 jack->id = kstrdup(id, GFP_KERNEL);
Mark Browne76d8ce2008-07-28 19:05:35 +0100100
101 jack->input_dev = input_allocate_device();
102 if (jack->input_dev == NULL) {
103 err = -ENOMEM;
104 goto fail_input;
105 }
106
107 jack->input_dev->phys = "ALSA";
108
109 jack->type = type;
110
Mark Brownbd8a71a2009-01-03 16:56:56 +0000111 for (i = 0; i < ARRAY_SIZE(jack_types); i++)
112 if (type & (1 << i))
113 input_set_capability(jack->input_dev, EV_SW,
114 jack_types[i]);
Mark Browne76d8ce2008-07-28 19:05:35 +0100115
116 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
117 if (err < 0)
118 goto fail_input;
119
120 *jjack = jack;
121
122 return 0;
123
124fail_input:
125 input_free_device(jack->input_dev);
126 kfree(jack);
127 return err;
128}
129EXPORT_SYMBOL(snd_jack_new);
130
131/**
132 * snd_jack_set_parent - Set the parent device for a jack
133 *
134 * @jack: The jack to configure
135 * @parent: The device to set as parent for the jack.
136 *
137 * Set the parent for the jack input device in the device tree. This
138 * function is only valid prior to registration of the jack. If no
139 * parent is configured then the parent device will be the sound card.
140 */
141void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
142{
143 WARN_ON(jack->registered);
144
145 jack->input_dev->dev.parent = parent;
146}
147EXPORT_SYMBOL(snd_jack_set_parent);
148
149/**
150 * snd_jack_report - Report the current status of a jack
151 *
152 * @jack: The jack to report status for
153 * @status: The current status of the jack
154 */
155void snd_jack_report(struct snd_jack *jack, int status)
156{
Mark Brownbd8a71a2009-01-03 16:56:56 +0000157 int i;
158
Mark Brown9a3f3712008-10-15 17:07:47 +0100159 if (!jack)
160 return;
161
Mark Brownbd8a71a2009-01-03 16:56:56 +0000162 for (i = 0; i < ARRAY_SIZE(jack_types); i++) {
163 int testbit = 1 << i;
164 if (jack->type & testbit)
165 input_report_switch(jack->input_dev, jack_types[i],
166 status & testbit);
167 }
Mark Browne76d8ce2008-07-28 19:05:35 +0100168
169 input_sync(jack->input_dev);
170}
171EXPORT_SYMBOL(snd_jack_report);
172
173MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
174MODULE_DESCRIPTION("Jack detection support for ALSA");
175MODULE_LICENSE("GPL");