Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 25 | #include <sound/jack.h> |
| 26 | #include <sound/core.h> |
Jie Yang | 9058cbe | 2015-04-27 21:20:56 +0800 | [diff] [blame] | 27 | #include <sound/control.h> |
| 28 | |
| 29 | struct snd_jack_kctl { |
| 30 | struct snd_kcontrol *kctl; |
| 31 | struct list_head list; /* list of controls belong to the same jack */ |
| 32 | unsigned int mask_bits; /* only masked status bits are reported via kctl */ |
| 33 | }; |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 34 | |
Mark Brown | 53803ae | 2012-02-07 19:48:47 +0000 | [diff] [blame] | 35 | static int jack_switch_types[SND_JACK_SWITCH_TYPES] = { |
Mark Brown | bd8a71a | 2009-01-03 16:56:56 +0000 | [diff] [blame] | 36 | SW_HEADPHONE_INSERT, |
| 37 | SW_MICROPHONE_INSERT, |
| 38 | SW_LINEOUT_INSERT, |
| 39 | SW_JACK_PHYSICAL_INSERT, |
Jani Nikula | d506fc3 | 2009-01-07 11:54:25 +0200 | [diff] [blame] | 40 | SW_VIDEOOUT_INSERT, |
David Henningsson | 7c2f8e4 | 2011-10-05 15:53:25 +0200 | [diff] [blame] | 41 | SW_LINEIN_INSERT, |
Mark Brown | bd8a71a | 2009-01-03 16:56:56 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Takashi Iwai | 32b8544 | 2013-11-14 15:35:46 +0100 | [diff] [blame] | 44 | static int snd_jack_dev_disconnect(struct snd_device *device) |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 45 | { |
| 46 | struct snd_jack *jack = device->device_data; |
| 47 | |
Takashi Iwai | 32b8544 | 2013-11-14 15:35:46 +0100 | [diff] [blame] | 48 | if (!jack->input_dev) |
| 49 | return 0; |
Takashi Iwai | 9d59065 | 2009-04-14 16:13:58 +0200 | [diff] [blame] | 50 | |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 51 | /* If the input device is registered with the input subsystem |
| 52 | * then we need to use a different deallocator. */ |
| 53 | if (jack->registered) |
| 54 | input_unregister_device(jack->input_dev); |
| 55 | else |
| 56 | input_free_device(jack->input_dev); |
Takashi Iwai | 32b8544 | 2013-11-14 15:35:46 +0100 | [diff] [blame] | 57 | jack->input_dev = NULL; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int snd_jack_dev_free(struct snd_device *device) |
| 62 | { |
| 63 | struct snd_jack *jack = device->device_data; |
Jie Yang | 9058cbe | 2015-04-27 21:20:56 +0800 | [diff] [blame] | 64 | struct snd_card *card = device->card; |
| 65 | struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl; |
Takashi Iwai | 32b8544 | 2013-11-14 15:35:46 +0100 | [diff] [blame] | 66 | |
Jie Yang | 9058cbe | 2015-04-27 21:20:56 +0800 | [diff] [blame] | 67 | list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) { |
| 68 | list_del_init(&jack_kctl->list); |
| 69 | snd_ctl_remove(card, jack_kctl->kctl); |
| 70 | } |
Takashi Iwai | 32b8544 | 2013-11-14 15:35:46 +0100 | [diff] [blame] | 71 | if (jack->private_free) |
| 72 | jack->private_free(jack); |
| 73 | |
| 74 | snd_jack_dev_disconnect(device); |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 75 | |
Matthew Ranostay | 282cd76 | 2008-10-25 01:05:29 -0400 | [diff] [blame] | 76 | kfree(jack->id); |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 77 | kfree(jack); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int snd_jack_dev_register(struct snd_device *device) |
| 83 | { |
| 84 | struct snd_jack *jack = device->device_data; |
| 85 | struct snd_card *card = device->card; |
Mark Brown | ebb812c | 2010-03-17 18:07:12 +0000 | [diff] [blame] | 86 | int err, i; |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 87 | |
| 88 | snprintf(jack->name, sizeof(jack->name), "%s %s", |
Takashi Iwai | 2678f60 | 2009-02-18 16:46:27 +0100 | [diff] [blame] | 89 | card->shortname, jack->id); |
Takashi Iwai | 43b2cd5 | 2015-04-30 15:25:00 +0200 | [diff] [blame] | 90 | |
| 91 | if (!jack->input_dev) |
| 92 | return 0; |
| 93 | |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 94 | jack->input_dev->name = jack->name; |
| 95 | |
| 96 | /* Default to the sound card device. */ |
| 97 | if (!jack->input_dev->dev.parent) |
Kay Sievers | 1f3fff7 | 2009-06-10 19:50:33 +0200 | [diff] [blame] | 98 | jack->input_dev->dev.parent = snd_card_get_device_link(card); |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 99 | |
Mark Brown | ebb812c | 2010-03-17 18:07:12 +0000 | [diff] [blame] | 100 | /* Add capabilities for any keys that are enabled */ |
| 101 | for (i = 0; i < ARRAY_SIZE(jack->key); i++) { |
| 102 | int testbit = SND_JACK_BTN_0 >> i; |
| 103 | |
| 104 | if (!(jack->type & testbit)) |
| 105 | continue; |
| 106 | |
| 107 | if (!jack->key[i]) |
| 108 | jack->key[i] = BTN_0 + i; |
| 109 | |
| 110 | input_set_capability(jack->input_dev, EV_KEY, jack->key[i]); |
| 111 | } |
| 112 | |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 113 | err = input_register_device(jack->input_dev); |
| 114 | if (err == 0) |
| 115 | jack->registered = 1; |
| 116 | |
| 117 | return err; |
| 118 | } |
| 119 | |
Jie Yang | 9058cbe | 2015-04-27 21:20:56 +0800 | [diff] [blame] | 120 | static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl) |
| 121 | { |
| 122 | struct snd_jack_kctl *jack_kctl; |
| 123 | |
| 124 | jack_kctl = kctl->private_data; |
| 125 | if (jack_kctl) { |
| 126 | list_del(&jack_kctl->list); |
| 127 | kfree(jack_kctl); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl) |
| 132 | { |
| 133 | list_add_tail(&jack_kctl->list, &jack->kctl_list); |
| 134 | } |
| 135 | |
| 136 | static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask) |
| 137 | { |
| 138 | struct snd_kcontrol *kctl; |
| 139 | struct snd_jack_kctl *jack_kctl; |
| 140 | int err; |
| 141 | |
Jie Yang | 2ba2dfa | 2015-04-27 21:20:59 +0800 | [diff] [blame] | 142 | kctl = snd_kctl_jack_new(name, card); |
Jie Yang | 9058cbe | 2015-04-27 21:20:56 +0800 | [diff] [blame] | 143 | if (!kctl) |
| 144 | return NULL; |
| 145 | |
| 146 | err = snd_ctl_add(card, kctl); |
| 147 | if (err < 0) |
| 148 | return NULL; |
| 149 | |
| 150 | jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL); |
| 151 | |
| 152 | if (!jack_kctl) |
| 153 | goto error; |
| 154 | |
| 155 | jack_kctl->kctl = kctl; |
| 156 | jack_kctl->mask_bits = mask; |
| 157 | |
| 158 | kctl->private_data = jack_kctl; |
| 159 | kctl->private_free = snd_jack_kctl_private_free; |
| 160 | |
| 161 | return jack_kctl; |
| 162 | error: |
| 163 | snd_ctl_free_one(kctl); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack |
| 169 | * @jack: the jack instance which the kctl will attaching to |
| 170 | * @name: the name for the snd_kcontrol object |
| 171 | * @mask: a bitmask of enum snd_jack_type values that can be detected |
| 172 | * by this snd_jack_kctl object. |
| 173 | * |
| 174 | * Creates a new snd_kcontrol object and adds it to the jack kctl_list. |
| 175 | * |
| 176 | * Return: Zero if successful, or a negative error code on failure. |
| 177 | */ |
| 178 | int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask) |
| 179 | { |
| 180 | struct snd_jack_kctl *jack_kctl; |
| 181 | |
| 182 | jack_kctl = snd_jack_kctl_new(jack->card, name, mask); |
| 183 | if (!jack_kctl) |
| 184 | return -ENOMEM; |
| 185 | |
| 186 | snd_jack_kctl_add(jack, jack_kctl); |
| 187 | return 0; |
| 188 | } |
| 189 | EXPORT_SYMBOL(snd_jack_add_new_kctl); |
| 190 | |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 191 | /** |
| 192 | * snd_jack_new - Create a new jack |
| 193 | * @card: the card instance |
| 194 | * @id: an identifying string for this jack |
| 195 | * @type: a bitmask of enum snd_jack_type values that can be detected by |
| 196 | * this jack |
| 197 | * @jjack: Used to provide the allocated jack object to the caller. |
Jie Yang | 4e3f0dc | 2015-04-27 21:20:58 +0800 | [diff] [blame] | 198 | * @initial_kctl: if true, create a kcontrol and add it to the jack list. |
| 199 | * @phantom_jack: Don't create a input device for phantom jacks. |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 200 | * |
| 201 | * Creates a new jack object. |
| 202 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 203 | * Return: Zero if successful, or a negative error code on failure. |
| 204 | * On success @jjack will be initialised. |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 205 | */ |
| 206 | int snd_jack_new(struct snd_card *card, const char *id, int type, |
Jie Yang | 4e3f0dc | 2015-04-27 21:20:58 +0800 | [diff] [blame] | 207 | struct snd_jack **jjack, bool initial_kctl, bool phantom_jack) |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 208 | { |
| 209 | struct snd_jack *jack; |
Jie Yang | 4e3f0dc | 2015-04-27 21:20:58 +0800 | [diff] [blame] | 210 | struct snd_jack_kctl *jack_kctl = NULL; |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 211 | int err; |
Mark Brown | bd8a71a | 2009-01-03 16:56:56 +0000 | [diff] [blame] | 212 | int i; |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 213 | static struct snd_device_ops ops = { |
| 214 | .dev_free = snd_jack_dev_free, |
| 215 | .dev_register = snd_jack_dev_register, |
Takashi Iwai | 32b8544 | 2013-11-14 15:35:46 +0100 | [diff] [blame] | 216 | .dev_disconnect = snd_jack_dev_disconnect, |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 217 | }; |
| 218 | |
Jie Yang | 4e3f0dc | 2015-04-27 21:20:58 +0800 | [diff] [blame] | 219 | if (initial_kctl) { |
| 220 | jack_kctl = snd_jack_kctl_new(card, id, type); |
| 221 | if (!jack_kctl) |
| 222 | return -ENOMEM; |
| 223 | } |
| 224 | |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 225 | jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL); |
| 226 | if (jack == NULL) |
| 227 | return -ENOMEM; |
| 228 | |
Matthew Ranostay | 282cd76 | 2008-10-25 01:05:29 -0400 | [diff] [blame] | 229 | jack->id = kstrdup(id, GFP_KERNEL); |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 230 | |
Jie Yang | 4e3f0dc | 2015-04-27 21:20:58 +0800 | [diff] [blame] | 231 | /* don't creat input device for phantom jack */ |
| 232 | if (!phantom_jack) { |
| 233 | jack->input_dev = input_allocate_device(); |
| 234 | if (jack->input_dev == NULL) { |
| 235 | err = -ENOMEM; |
| 236 | goto fail_input; |
| 237 | } |
| 238 | |
| 239 | jack->input_dev->phys = "ALSA"; |
| 240 | |
| 241 | jack->type = type; |
| 242 | |
| 243 | for (i = 0; i < SND_JACK_SWITCH_TYPES; i++) |
| 244 | if (type & (1 << i)) |
| 245 | input_set_capability(jack->input_dev, EV_SW, |
| 246 | jack_switch_types[i]); |
| 247 | |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 250 | err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops); |
| 251 | if (err < 0) |
| 252 | goto fail_input; |
| 253 | |
Jie Yang | 9058cbe | 2015-04-27 21:20:56 +0800 | [diff] [blame] | 254 | jack->card = card; |
| 255 | INIT_LIST_HEAD(&jack->kctl_list); |
| 256 | |
Jie Yang | 4e3f0dc | 2015-04-27 21:20:58 +0800 | [diff] [blame] | 257 | if (initial_kctl) |
| 258 | snd_jack_kctl_add(jack, jack_kctl); |
| 259 | |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 260 | *jjack = jack; |
| 261 | |
| 262 | return 0; |
| 263 | |
| 264 | fail_input: |
| 265 | input_free_device(jack->input_dev); |
Lu Guanqun | eeda276b | 2011-02-21 13:45:04 +0800 | [diff] [blame] | 266 | kfree(jack->id); |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 267 | kfree(jack); |
| 268 | return err; |
| 269 | } |
| 270 | EXPORT_SYMBOL(snd_jack_new); |
| 271 | |
| 272 | /** |
| 273 | * snd_jack_set_parent - Set the parent device for a jack |
| 274 | * |
| 275 | * @jack: The jack to configure |
| 276 | * @parent: The device to set as parent for the jack. |
| 277 | * |
Mark Brown | a2e888f | 2012-05-07 17:10:21 +0100 | [diff] [blame] | 278 | * Set the parent for the jack devices in the device tree. This |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 279 | * function is only valid prior to registration of the jack. If no |
| 280 | * parent is configured then the parent device will be the sound card. |
| 281 | */ |
| 282 | void snd_jack_set_parent(struct snd_jack *jack, struct device *parent) |
| 283 | { |
| 284 | WARN_ON(jack->registered); |
Takashi Iwai | 43b2cd5 | 2015-04-30 15:25:00 +0200 | [diff] [blame] | 285 | if (!jack->input_dev) |
| 286 | return; |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 287 | |
| 288 | jack->input_dev->dev.parent = parent; |
| 289 | } |
| 290 | EXPORT_SYMBOL(snd_jack_set_parent); |
| 291 | |
| 292 | /** |
Mark Brown | ebb812c | 2010-03-17 18:07:12 +0000 | [diff] [blame] | 293 | * snd_jack_set_key - Set a key mapping on a jack |
| 294 | * |
| 295 | * @jack: The jack to configure |
| 296 | * @type: Jack report type for this key |
| 297 | * @keytype: Input layer key type to be reported |
| 298 | * |
| 299 | * Map a SND_JACK_BTN_ button type to an input layer key, allowing |
| 300 | * reporting of keys on accessories via the jack abstraction. If no |
| 301 | * mapping is provided but keys are enabled in the jack type then |
| 302 | * BTN_n numeric buttons will be reported. |
| 303 | * |
Mark Brown | a2e888f | 2012-05-07 17:10:21 +0100 | [diff] [blame] | 304 | * If jacks are not reporting via the input API this call will have no |
| 305 | * effect. |
| 306 | * |
Mark Brown | ebb812c | 2010-03-17 18:07:12 +0000 | [diff] [blame] | 307 | * Note that this is intended to be use by simple devices with small |
| 308 | * numbers of keys that can be reported. It is also possible to |
| 309 | * access the input device directly - devices with complex input |
| 310 | * capabilities on accessories should consider doing this rather than |
| 311 | * using this abstraction. |
| 312 | * |
| 313 | * This function may only be called prior to registration of the jack. |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 314 | * |
| 315 | * Return: Zero if successful, or a negative error code on failure. |
Mark Brown | ebb812c | 2010-03-17 18:07:12 +0000 | [diff] [blame] | 316 | */ |
| 317 | int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type, |
| 318 | int keytype) |
| 319 | { |
| 320 | int key = fls(SND_JACK_BTN_0) - fls(type); |
| 321 | |
| 322 | WARN_ON(jack->registered); |
| 323 | |
| 324 | if (!keytype || key >= ARRAY_SIZE(jack->key)) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | jack->type |= type; |
| 328 | jack->key[key] = keytype; |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | EXPORT_SYMBOL(snd_jack_set_key); |
| 333 | |
| 334 | /** |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 335 | * snd_jack_report - Report the current status of a jack |
| 336 | * |
| 337 | * @jack: The jack to report status for |
| 338 | * @status: The current status of the jack |
| 339 | */ |
| 340 | void snd_jack_report(struct snd_jack *jack, int status) |
| 341 | { |
Jie Yang | 9058cbe | 2015-04-27 21:20:56 +0800 | [diff] [blame] | 342 | struct snd_jack_kctl *jack_kctl; |
Mark Brown | bd8a71a | 2009-01-03 16:56:56 +0000 | [diff] [blame] | 343 | int i; |
| 344 | |
Mark Brown | 9a3f371 | 2008-10-15 17:07:47 +0100 | [diff] [blame] | 345 | if (!jack) |
| 346 | return; |
| 347 | |
Jie Yang | 6ed9495 | 2015-04-30 20:22:46 +0800 | [diff] [blame] | 348 | list_for_each_entry(jack_kctl, &jack->kctl_list, list) |
| 349 | snd_kctl_jack_report(jack->card, jack_kctl->kctl, |
| 350 | status & jack_kctl->mask_bits); |
| 351 | |
| 352 | if (!jack->input_dev) |
| 353 | return; |
| 354 | |
Mark Brown | ebb812c | 2010-03-17 18:07:12 +0000 | [diff] [blame] | 355 | for (i = 0; i < ARRAY_SIZE(jack->key); i++) { |
| 356 | int testbit = SND_JACK_BTN_0 >> i; |
| 357 | |
| 358 | if (jack->type & testbit) |
| 359 | input_report_key(jack->input_dev, jack->key[i], |
| 360 | status & testbit); |
| 361 | } |
| 362 | |
Mark Brown | 1c6e555 | 2010-03-17 15:36:38 +0000 | [diff] [blame] | 363 | for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) { |
Mark Brown | bd8a71a | 2009-01-03 16:56:56 +0000 | [diff] [blame] | 364 | int testbit = 1 << i; |
| 365 | if (jack->type & testbit) |
Mark Brown | 1c6e555 | 2010-03-17 15:36:38 +0000 | [diff] [blame] | 366 | input_report_switch(jack->input_dev, |
| 367 | jack_switch_types[i], |
Mark Brown | bd8a71a | 2009-01-03 16:56:56 +0000 | [diff] [blame] | 368 | status & testbit); |
| 369 | } |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 370 | |
| 371 | input_sync(jack->input_dev); |
Jie Yang | 9058cbe | 2015-04-27 21:20:56 +0800 | [diff] [blame] | 372 | |
Mark Brown | e76d8ce | 2008-07-28 19:05:35 +0100 | [diff] [blame] | 373 | } |
| 374 | EXPORT_SYMBOL(snd_jack_report); |