1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
31
32 if (jack->registered)
33 input_unregister_device(jack->input_dev);
34 else
35 input_free_device(jack->input_dev);
36
37 kfree(jack->id);
38 kfree(jack);
39
40 return 0;
41}
42
43static int snd_jack_dev_register(struct snd_device *device)
44{
45 struct snd_jack *jack = device->device_data;
46 struct snd_card *card = device->card;
47 int err;
48
49 snprintf(jack->name, sizeof(jack->name), "%s %s",
50 card->shortname, jack->id);
51 jack->input_dev->name = jack->name;
52
53
54 if (!jack->input_dev->dev.parent)
55 jack->input_dev->dev.parent = card->dev;
56
57 err = input_register_device(jack->input_dev);
58 if (err == 0)
59 jack->registered = 1;
60
61 return err;
62}
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77int snd_jack_new(struct snd_card *card, const char *id, int type,
78 struct snd_jack **jjack)
79{
80 struct snd_jack *jack;
81 int err;
82 static struct snd_device_ops ops = {
83 .dev_free = snd_jack_dev_free,
84 .dev_register = snd_jack_dev_register,
85 };
86
87 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
88 if (jack == NULL)
89 return -ENOMEM;
90
91 jack->id = kstrdup(id, GFP_KERNEL);
92
93 jack->input_dev = input_allocate_device();
94 if (jack->input_dev == NULL) {
95 err = -ENOMEM;
96 goto fail_input;
97 }
98
99 jack->input_dev->phys = "ALSA";
100
101 jack->type = type;
102
103 if (type & SND_JACK_HEADPHONE)
104 input_set_capability(jack->input_dev, EV_SW,
105 SW_HEADPHONE_INSERT);
106 if (type & SND_JACK_LINEOUT)
107 input_set_capability(jack->input_dev, EV_SW,
108 SW_LINEOUT_INSERT);
109 if (type & SND_JACK_MICROPHONE)
110 input_set_capability(jack->input_dev, EV_SW,
111 SW_MICROPHONE_INSERT);
112 if (type & SND_JACK_MECHANICAL)
113 input_set_capability(jack->input_dev, EV_SW,
114 SW_JACK_PHYSICAL_INSERT);
115
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
133
134
135
136
137
138
139
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
151
152
153
154
155void snd_jack_report(struct snd_jack *jack, int status)
156{
157 if (!jack)
158 return;
159
160 if (jack->type & SND_JACK_HEADPHONE)
161 input_report_switch(jack->input_dev, SW_HEADPHONE_INSERT,
162 status & SND_JACK_HEADPHONE);
163 if (jack->type & SND_JACK_LINEOUT)
164 input_report_switch(jack->input_dev, SW_LINEOUT_INSERT,
165 status & SND_JACK_LINEOUT);
166 if (jack->type & SND_JACK_MICROPHONE)
167 input_report_switch(jack->input_dev, SW_MICROPHONE_INSERT,
168 status & SND_JACK_MICROPHONE);
169 if (jack->type & SND_JACK_MECHANICAL)
170 input_report_switch(jack->input_dev, SW_JACK_PHYSICAL_INSERT,
171 status & SND_JACK_MECHANICAL);
172
173 input_sync(jack->input_dev);
174}
175EXPORT_SYMBOL(snd_jack_report);
176
177MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
178MODULE_DESCRIPTION("Jack detection support for ALSA");
179MODULE_LICENSE("GPL");
180