blob: 14b8a4ee690dfb4b5b8df9c5159f396c1677a171 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mark Browne76d8ce2008-07-28 19:05:35 +010024#include <sound/jack.h>
25#include <sound/core.h>
26
Mark Brownbd8a71a2009-01-03 16:56:56 +000027static int jack_types[] = {
28 SW_HEADPHONE_INSERT,
29 SW_MICROPHONE_INSERT,
30 SW_LINEOUT_INSERT,
31 SW_JACK_PHYSICAL_INSERT,
Jani Nikulad506fc32009-01-07 11:54:25 +020032 SW_VIDEOOUT_INSERT,
Mark Brownbd8a71a2009-01-03 16:56:56 +000033};
34
Mark Browne76d8ce2008-07-28 19:05:35 +010035static int snd_jack_dev_free(struct snd_device *device)
36{
37 struct snd_jack *jack = device->device_data;
38
Takashi Iwai9d590652009-04-14 16:13:58 +020039 if (jack->private_free)
40 jack->private_free(jack);
41
Mark Browne76d8ce2008-07-28 19:05:35 +010042 /* If the input device is registered with the input subsystem
43 * then we need to use a different deallocator. */
44 if (jack->registered)
45 input_unregister_device(jack->input_dev);
46 else
47 input_free_device(jack->input_dev);
48
Matthew Ranostay282cd762008-10-25 01:05:29 -040049 kfree(jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +010050 kfree(jack);
51
52 return 0;
53}
54
55static int snd_jack_dev_register(struct snd_device *device)
56{
57 struct snd_jack *jack = device->device_data;
58 struct snd_card *card = device->card;
59 int err;
60
61 snprintf(jack->name, sizeof(jack->name), "%s %s",
Takashi Iwai2678f602009-02-18 16:46:27 +010062 card->shortname, jack->id);
Mark Browne76d8ce2008-07-28 19:05:35 +010063 jack->input_dev->name = jack->name;
64
65 /* Default to the sound card device. */
66 if (!jack->input_dev->dev.parent)
Kay Sievers1f3fff72009-06-10 19:50:33 +020067 jack->input_dev->dev.parent = snd_card_get_device_link(card);
Mark Browne76d8ce2008-07-28 19:05:35 +010068
69 err = input_register_device(jack->input_dev);
70 if (err == 0)
71 jack->registered = 1;
72
73 return err;
74}
75
76/**
77 * snd_jack_new - Create a new jack
78 * @card: the card instance
79 * @id: an identifying string for this jack
80 * @type: a bitmask of enum snd_jack_type values that can be detected by
81 * this jack
82 * @jjack: Used to provide the allocated jack object to the caller.
83 *
84 * Creates a new jack object.
85 *
86 * Returns zero if successful, or a negative error code on failure.
87 * On success jjack will be initialised.
88 */
89int snd_jack_new(struct snd_card *card, const char *id, int type,
90 struct snd_jack **jjack)
91{
92 struct snd_jack *jack;
93 int err;
Mark Brownbd8a71a2009-01-03 16:56:56 +000094 int i;
Mark Browne76d8ce2008-07-28 19:05:35 +010095 static struct snd_device_ops ops = {
96 .dev_free = snd_jack_dev_free,
97 .dev_register = snd_jack_dev_register,
98 };
99
100 jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
101 if (jack == NULL)
102 return -ENOMEM;
103
Matthew Ranostay282cd762008-10-25 01:05:29 -0400104 jack->id = kstrdup(id, GFP_KERNEL);
Mark Browne76d8ce2008-07-28 19:05:35 +0100105
106 jack->input_dev = input_allocate_device();
107 if (jack->input_dev == NULL) {
108 err = -ENOMEM;
109 goto fail_input;
110 }
111
112 jack->input_dev->phys = "ALSA";
113
114 jack->type = type;
115
Mark Brownbd8a71a2009-01-03 16:56:56 +0000116 for (i = 0; i < ARRAY_SIZE(jack_types); i++)
117 if (type & (1 << i))
118 input_set_capability(jack->input_dev, EV_SW,
119 jack_types[i]);
Mark Browne76d8ce2008-07-28 19:05:35 +0100120
121 err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
122 if (err < 0)
123 goto fail_input;
124
125 *jjack = jack;
126
127 return 0;
128
129fail_input:
130 input_free_device(jack->input_dev);
131 kfree(jack);
132 return err;
133}
134EXPORT_SYMBOL(snd_jack_new);
135
136/**
137 * snd_jack_set_parent - Set the parent device for a jack
138 *
139 * @jack: The jack to configure
140 * @parent: The device to set as parent for the jack.
141 *
142 * Set the parent for the jack input device in the device tree. This
143 * function is only valid prior to registration of the jack. If no
144 * parent is configured then the parent device will be the sound card.
145 */
146void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
147{
148 WARN_ON(jack->registered);
149
150 jack->input_dev->dev.parent = parent;
151}
152EXPORT_SYMBOL(snd_jack_set_parent);
153
154/**
155 * snd_jack_report - Report the current status of a jack
156 *
157 * @jack: The jack to report status for
158 * @status: The current status of the jack
159 */
160void snd_jack_report(struct snd_jack *jack, int status)
161{
Mark Brownbd8a71a2009-01-03 16:56:56 +0000162 int i;
163
Mark Brown9a3f3712008-10-15 17:07:47 +0100164 if (!jack)
165 return;
166
Mark Brownbd8a71a2009-01-03 16:56:56 +0000167 for (i = 0; i < ARRAY_SIZE(jack_types); i++) {
168 int testbit = 1 << i;
169 if (jack->type & testbit)
170 input_report_switch(jack->input_dev, jack_types[i],
171 status & testbit);
172 }
Mark Browne76d8ce2008-07-28 19:05:35 +0100173
174 input_sync(jack->input_dev);
175}
176EXPORT_SYMBOL(snd_jack_report);
177
178MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
179MODULE_DESCRIPTION("Jack detection support for ALSA");
180MODULE_LICENSE("GPL");