aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Brown <broonie@opensource.wolfsonmicro.com>2008-07-28 14:05:35 -0400
committerJaroslav Kysela <perex@perex.cz>2008-07-29 15:32:03 -0400
commite76d8ceaaff9d7fc1ba2b1963a9f34151832223b (patch)
tree4041759593dbc10c35c96976e60ee759de3f9dbe
parent0b18cb1854152a62492aae088cb80cbeb5c0288d (diff)
ALSA: Add jack reporting API
Currently very few systems provide information about jack status to user space, even though many have hardware facilities to do detection. Those systems that do use an input device with the existing SW_HEADPHONE_INSERT switch type to do so, often independently of ALSA. This patch introduces a standard method for representing jacks to user space into ALSA. It allows drivers to register jacks for a sound card with the input subsystem, binding the input device to the card to help user space associate the input devices with their sound cards. The created input devices are named in the form "card longname jack" where jack is provided by the driver when allocating a jack. By default the parent for the input device is the sound card but this can be overridden by the card driver. The existing user space API with SW_HEADPHONE_INSERT is preserved. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Jaroslav Kysela <perex@perex.cz>
-rw-r--r--include/sound/core.h1
-rw-r--r--include/sound/jack.h75
-rw-r--r--sound/core/jack.c163
3 files changed, 239 insertions, 0 deletions
diff --git a/include/sound/core.h b/include/sound/core.h
index 558b96284bd2..1a4ff0bdcf6a 100644
--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -63,6 +63,7 @@ typedef int __bitwise snd_device_type_t;
63#define SNDRV_DEV_INFO ((__force snd_device_type_t) 0x1006) 63#define SNDRV_DEV_INFO ((__force snd_device_type_t) 0x1006)
64#define SNDRV_DEV_BUS ((__force snd_device_type_t) 0x1007) 64#define SNDRV_DEV_BUS ((__force snd_device_type_t) 0x1007)
65#define SNDRV_DEV_CODEC ((__force snd_device_type_t) 0x1008) 65#define SNDRV_DEV_CODEC ((__force snd_device_type_t) 0x1008)
66#define SNDRV_DEV_JACK ((__force snd_device_type_t) 0x1009)
66#define SNDRV_DEV_LOWLEVEL ((__force snd_device_type_t) 0x2000) 67#define SNDRV_DEV_LOWLEVEL ((__force snd_device_type_t) 0x2000)
67 68
68typedef int __bitwise snd_device_state_t; 69typedef int __bitwise snd_device_state_t;
diff --git a/include/sound/jack.h b/include/sound/jack.h
new file mode 100644
index 000000000000..b1b2b8b59adb
--- /dev/null
+++ b/include/sound/jack.h
@@ -0,0 +1,75 @@
1#ifndef __SOUND_JACK_H
2#define __SOUND_JACK_H
3
4/*
5 * Jack abstraction layer
6 *
7 * Copyright 2008 Wolfson Microelectronics plc
8 *
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26#include <sound/core.h>
27
28struct input_dev;
29
30/**
31 * Jack types which can be reported. These values are used as a
32 * bitmask.
33 */
34enum snd_jack_types {
35 SND_JACK_HEADPHONE = 0x0001,
36 SND_JACK_MICROPHONE = 0x0002,
37 SND_JACK_HEADSET = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE,
38};
39
40struct snd_jack {
41 struct input_dev *input_dev;
42 int registered;
43 int type;
44 const char *id;
45 char name[100];
46};
47
48#ifdef CONFIG_SND_JACK
49
50int snd_jack_new(struct snd_card *card, const char *id, int type,
51 struct snd_jack **jack);
52void snd_jack_set_parent(struct snd_jack *jack, struct device *parent);
53
54void snd_jack_report(struct snd_jack *jack, int status);
55
56#else
57
58static inline int snd_jack_new(struct snd_card *card, const char *id, int type,
59 struct snd_jack **jack)
60{
61 return 0;
62}
63
64static inline void snd_jack_set_parent(struct snd_jack *jack,
65 struct device *parent)
66{
67}
68
69static inline void snd_jack_report(struct snd_jack *jack, int status)
70{
71}
72
73#endif
74
75#endif
diff --git a/sound/core/jack.c b/sound/core/jack.c
new file mode 100644
index 000000000000..8133a2b173a5
--- /dev/null
+++ b/sound/core/jack.c
@@ -0,0 +1,163 @@
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
26static int snd_jack_dev_free(struct snd_device *device)
27{
28 struct snd_jack *jack = device->device_data;
29
30 /* If the input device is registered with the input subsystem
31 * then we need to use a different deallocator. */
32 if (jack->registered)
33 input_unregister_device(jack->input_dev);
34 else
35 input_free_device(jack->input_dev);
36
37 kfree(jack);
38
39 return 0;
40}
41
42static int snd_jack_dev_register(struct snd_device *device)
43{
44 struct snd_jack *jack = device->device_data;
45 struct snd_card *card = device->card;
46 int err;
47
48 snprintf(jack->name, sizeof(jack->name), "%s %s",
49 card->longname, jack->id);
50 jack->input_dev->name = jack->name;
51
52 /* Default to the sound card device. */
53 if (!jack->input_dev->dev.parent)
54 jack->input_dev->dev.parent = card->dev;
55
56 err = input_register_device(jack->input_dev);
57 if (err == 0)
58 jack->registered = 1;
59
60 return err;
61}
62
63/**
64 * snd_jack_new - Create a new jack
65 * @card: the card instance
66 * @id: an identifying string for this jack
67 * @type: a bitmask of enum snd_jack_type values that can be detected by
68 * this jack
69 * @jjack: Used to provide the allocated jack object to the caller.
70 *
71 * Creates a new jack object.
72 *
73 * Returns zero if successful, or a negative error code on failure.
74 * On success jjack will be initialised.
75 */
76int snd_jack_new(struct snd_card *card, const char *id, int type,
77 struct snd_jack **jjack)
78{
79 struct snd_jack *jack;
80 int err;
81 static struct snd_device_ops ops = {
82 .dev_free = snd_jack_dev_free,
83 .dev_register = snd_jack_dev_register,
84 };
85
86 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
87 if (jack == NULL)
88 return -ENOMEM;
89
90 jack->id = id;
91
92 jack->input_dev = input_allocate_device();
93 if (jack->input_dev == NULL) {
94 err = -ENOMEM;
95 goto fail_input;
96 }
97
98 jack->input_dev->phys = "ALSA";
99
100 jack->type = type;
101
102 if (type & SND_JACK_HEADPHONE)
103 input_set_capability(jack->input_dev, EV_SW,
104 SW_HEADPHONE_INSERT);
105 if (type & SND_JACK_MICROPHONE)
106 input_set_capability(jack->input_dev, EV_SW,
107 SW_MICROPHONE_INSERT);
108
109 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
110 if (err < 0)
111 goto fail_input;
112
113 *jjack = jack;
114
115 return 0;
116
117fail_input:
118 input_free_device(jack->input_dev);
119 kfree(jack);
120 return err;
121}
122EXPORT_SYMBOL(snd_jack_new);
123
124/**
125 * snd_jack_set_parent - Set the parent device for a jack
126 *
127 * @jack: The jack to configure
128 * @parent: The device to set as parent for the jack.
129 *
130 * Set the parent for the jack input device in the device tree. This
131 * function is only valid prior to registration of the jack. If no
132 * parent is configured then the parent device will be the sound card.
133 */
134void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
135{
136 WARN_ON(jack->registered);
137
138 jack->input_dev->dev.parent = parent;
139}
140EXPORT_SYMBOL(snd_jack_set_parent);
141
142/**
143 * snd_jack_report - Report the current status of a jack
144 *
145 * @jack: The jack to report status for
146 * @status: The current status of the jack
147 */
148void snd_jack_report(struct snd_jack *jack, int status)
149{
150 if (jack->type & SND_JACK_HEADPHONE)
151 input_report_switch(jack->input_dev, SW_HEADPHONE_INSERT,
152 status & SND_JACK_HEADPHONE);
153 if (jack->type & SND_JACK_MICROPHONE)
154 input_report_switch(jack->input_dev, SW_MICROPHONE_INSERT,
155 status & SND_JACK_MICROPHONE);
156
157 input_sync(jack->input_dev);
158}
159EXPORT_SYMBOL(snd_jack_report);
160
161MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
162MODULE_DESCRIPTION("Jack detection support for ALSA");
163MODULE_LICENSE("GPL");