ALSA: Reduce boilerplate for new jack types
[safe/jmp/linux-2.6] / sound / core / jack.c
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
26 static int jack_types[] = {
27         SW_HEADPHONE_INSERT,
28         SW_MICROPHONE_INSERT,
29         SW_LINEOUT_INSERT,
30         SW_JACK_PHYSICAL_INSERT,
31 };
32
33 static 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
44         kfree(jack->id);
45         kfree(jack);
46
47         return 0;
48 }
49
50 static 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  */
84 int 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;
89         int i;
90         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
99         jack->id = kstrdup(id, GFP_KERNEL);
100
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
111         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]);
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
124 fail_input:
125         input_free_device(jack->input_dev);
126         kfree(jack);
127         return err;
128 }
129 EXPORT_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  */
141 void 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 }
147 EXPORT_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  */
155 void snd_jack_report(struct snd_jack *jack, int status)
156 {
157         int i;
158
159         if (!jack)
160                 return;
161
162         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         }
168
169         input_sync(jack->input_dev);
170 }
171 EXPORT_SYMBOL(snd_jack_report);
172
173 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
174 MODULE_DESCRIPTION("Jack detection support for ALSA");
175 MODULE_LICENSE("GPL");