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 SW_VIDEOOUT_INSERT, 32 }; 33 34 static 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 45 kfree(jack->id); 46 kfree(jack); 47 48 return 0; 49 } 50 51 static 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", 58 card->shortname, jack->id); 59 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 */ 85 int 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; 90 int i; 91 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 100 jack->id = kstrdup(id, GFP_KERNEL); 101 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 112 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]); 116 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 125 fail_input: 126 input_free_device(jack->input_dev); 127 kfree(jack); 128 return err; 129 } 130 EXPORT_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 */ 142 void 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 } 148 EXPORT_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 */ 156 void snd_jack_report(struct snd_jack *jack, int status) 157 { 158 int i; 159 160 if (!jack) 161 return; 162 163 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 } 169 170 input_sync(jack->input_dev); 171 } 172 EXPORT_SYMBOL(snd_jack_report); 173 174 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 175 MODULE_DESCRIPTION("Jack detection support for ALSA"); 176 MODULE_LICENSE("GPL"); 177