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