| 1 | |
| 2 | |
| 3 | * Jack abstraction layer
|
| 4 | *
|
| 5 | * Copyright 2008 Wolfson Microelectronics
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/input.h>
|
| 9 | #include <linux/slab.h>
|
| 10 | #include <linux/module.h>
|
| 11 | #include <linux/ctype.h>
|
| 12 | #include <linux/mm.h>
|
| 13 | #include <linux/debugfs.h>
|
| 14 | #include <sound/jack.h>
|
| 15 | #include <sound/core.h>
|
| 16 | #include <sound/control.h>
|
| 17 |
|
| 18 | struct snd_jack_kctl {
|
| 19 | struct snd_kcontrol *kctl;
|
| 20 | struct list_head list;
|
| 21 | unsigned int mask_bits;
|
| 22 | struct snd_jack *jack;
|
| 23 | bool sw_inject_enable;
|
| 24 | #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
|
| 25 | struct dentry *jack_debugfs_root;
|
| 26 | #endif
|
| 27 | };
|
| 28 |
|
| 29 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 30 | static const int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
|
| 31 | SW_HEADPHONE_INSERT,
|
| 32 | SW_MICROPHONE_INSERT,
|
| 33 | SW_LINEOUT_INSERT,
|
| 34 | SW_JACK_PHYSICAL_INSERT,
|
| 35 | SW_VIDEOOUT_INSERT,
|
| 36 | SW_LINEIN_INSERT,
|
| 37 | SW_USB_INSERT,
|
| 38 | };
|
| 39 | #endif
|
| 40 |
|
| 41 | static void snd_jack_remove_debugfs(struct snd_jack *jack);
|
| 42 |
|
| 43 | static int snd_jack_dev_disconnect(struct snd_device *device)
|
| 44 | {
|
| 45 | struct snd_jack *jack = device->device_data;
|
| 46 |
|
| 47 | snd_jack_remove_debugfs(jack);
|
| 48 |
|
| 49 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 50 | guard(mutex)(&jack->input_dev_lock);
|
| 51 | if (!jack->input_dev)
|
| 52 | return 0;
|
| 53 |
|
| 54 | |
| 55 | * then we need to use a different deallocator.
|
| 56 | if (jack->registered)
|
| 57 | input_unregister_device(jack->input_dev);
|
| 58 | else
|
| 59 | input_free_device(jack->input_dev);
|
| 60 | jack->input_dev = NULL;
|
| 61 | #endif
|
| 62 | return 0;
|
| 63 | }
|
| 64 |
|
| 65 | static int snd_jack_dev_free(struct snd_device *device)
|
| 66 | {
|
| 67 | struct snd_jack *jack = device->device_data;
|
| 68 | struct snd_card *card = device->card;
|
| 69 | struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
|
| 70 |
|
| 71 | list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
|
| 72 | list_del_init(&jack_kctl->list);
|
| 73 | snd_ctl_remove(card, jack_kctl->kctl);
|
| 74 | }
|
| 75 |
|
| 76 | if (jack->private_free)
|
| 77 | jack->private_free(jack);
|
| 78 |
|
| 79 | snd_jack_dev_disconnect(device);
|
| 80 |
|
| 81 | kfree(jack->id);
|
| 82 | kfree(jack);
|
| 83 |
|
| 84 | return 0;
|
| 85 | }
|
| 86 |
|
| 87 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 88 | static int snd_jack_dev_register(struct snd_device *device)
|
| 89 | {
|
| 90 | struct snd_jack *jack = device->device_data;
|
| 91 | struct snd_card *card = device->card;
|
| 92 | int err, i;
|
| 93 |
|
| 94 | snprintf(jack->name, sizeof(jack->name), "%s %s",
|
| 95 | card->shortname, jack->id);
|
| 96 |
|
| 97 | guard(mutex)(&jack->input_dev_lock);
|
| 98 | if (!jack->input_dev)
|
| 99 | return 0;
|
| 100 |
|
| 101 | jack->input_dev->name = jack->name;
|
| 102 |
|
| 103 |
|
| 104 | if (!jack->input_dev->dev.parent)
|
| 105 | jack->input_dev->dev.parent = snd_card_get_device_link(card);
|
| 106 |
|
| 107 |
|
| 108 | for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
|
| 109 | int testbit = SND_JACK_BTN_0 >> i;
|
| 110 |
|
| 111 | if (!(jack->type & testbit))
|
| 112 | continue;
|
| 113 |
|
| 114 | if (!jack->key[i])
|
| 115 | jack->key[i] = BTN_0 + i;
|
| 116 |
|
| 117 | input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
|
| 118 | }
|
| 119 |
|
| 120 | err = input_register_device(jack->input_dev);
|
| 121 | if (err == 0)
|
| 122 | jack->registered = 1;
|
| 123 |
|
| 124 | return err;
|
| 125 | }
|
| 126 | #endif
|
| 127 |
|
| 128 | #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
|
| 129 | static void snd_jack_inject_report(struct snd_jack_kctl *jack_kctl, int status)
|
| 130 | {
|
| 131 | struct snd_jack *jack;
|
| 132 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 133 | int i;
|
| 134 | #endif
|
| 135 | if (!jack_kctl)
|
| 136 | return;
|
| 137 |
|
| 138 | jack = jack_kctl->jack;
|
| 139 |
|
| 140 | if (jack_kctl->sw_inject_enable)
|
| 141 | snd_kctl_jack_report(jack->card, jack_kctl->kctl,
|
| 142 | status & jack_kctl->mask_bits);
|
| 143 |
|
| 144 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 145 | if (!jack->input_dev)
|
| 146 | return;
|
| 147 |
|
| 148 | for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
|
| 149 | int testbit = ((SND_JACK_BTN_0 >> i) & jack_kctl->mask_bits);
|
| 150 |
|
| 151 | if (jack->type & testbit)
|
| 152 | input_report_key(jack->input_dev, jack->key[i],
|
| 153 | status & testbit);
|
| 154 | }
|
| 155 |
|
| 156 | for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
|
| 157 | int testbit = ((1 << i) & jack_kctl->mask_bits);
|
| 158 |
|
| 159 | if (jack->type & testbit)
|
| 160 | input_report_switch(jack->input_dev,
|
| 161 | jack_switch_types[i],
|
| 162 | status & testbit);
|
| 163 | }
|
| 164 |
|
| 165 | input_sync(jack->input_dev);
|
| 166 | #endif
|
| 167 | }
|
| 168 |
|
| 169 | static ssize_t sw_inject_enable_read(struct file *file,
|
| 170 | char __user *to, size_t count, loff_t *ppos)
|
| 171 | {
|
| 172 | struct snd_jack_kctl *jack_kctl = file->private_data;
|
| 173 | int len, ret;
|
| 174 | char buf[128];
|
| 175 |
|
| 176 | len = scnprintf(buf, sizeof(buf), "%s: %s\t\t%s: %i\n", "Jack", jack_kctl->kctl->id.name,
|
| 177 | "Inject Enabled", jack_kctl->sw_inject_enable);
|
| 178 | ret = simple_read_from_buffer(to, count, ppos, buf, len);
|
| 179 |
|
| 180 | return ret;
|
| 181 | }
|
| 182 |
|
| 183 | static ssize_t sw_inject_enable_write(struct file *file,
|
| 184 | const char __user *from, size_t count, loff_t *ppos)
|
| 185 | {
|
| 186 | struct snd_jack_kctl *jack_kctl = file->private_data;
|
| 187 | int ret, err;
|
| 188 | unsigned long enable;
|
| 189 | char buf[8] = { 0 };
|
| 190 |
|
| 191 | ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
|
| 192 | err = kstrtoul(buf, 0, &enable);
|
| 193 | if (err)
|
| 194 | return err;
|
| 195 |
|
| 196 | if (jack_kctl->sw_inject_enable == (!!enable))
|
| 197 | return ret;
|
| 198 |
|
| 199 | jack_kctl->sw_inject_enable = !!enable;
|
| 200 |
|
| 201 | if (!jack_kctl->sw_inject_enable)
|
| 202 | snd_jack_report(jack_kctl->jack, jack_kctl->jack->hw_status_cache);
|
| 203 |
|
| 204 | return ret;
|
| 205 | }
|
| 206 |
|
| 207 | static ssize_t jackin_inject_write(struct file *file,
|
| 208 | const char __user *from, size_t count, loff_t *ppos)
|
| 209 | {
|
| 210 | struct snd_jack_kctl *jack_kctl = file->private_data;
|
| 211 | int ret, err;
|
| 212 | unsigned long enable;
|
| 213 | char buf[8] = { 0 };
|
| 214 |
|
| 215 | if (!jack_kctl->sw_inject_enable)
|
| 216 | return -EINVAL;
|
| 217 |
|
| 218 | ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
|
| 219 | err = kstrtoul(buf, 0, &enable);
|
| 220 | if (err)
|
| 221 | return err;
|
| 222 |
|
| 223 | snd_jack_inject_report(jack_kctl, !!enable ? jack_kctl->mask_bits : 0);
|
| 224 |
|
| 225 | return ret;
|
| 226 | }
|
| 227 |
|
| 228 | static ssize_t jack_kctl_id_read(struct file *file,
|
| 229 | char __user *to, size_t count, loff_t *ppos)
|
| 230 | {
|
| 231 | struct snd_jack_kctl *jack_kctl = file->private_data;
|
| 232 | char buf[64];
|
| 233 | int len, ret;
|
| 234 |
|
| 235 | len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->id.name);
|
| 236 | ret = simple_read_from_buffer(to, count, ppos, buf, len);
|
| 237 |
|
| 238 | return ret;
|
| 239 | }
|
| 240 |
|
| 241 |
|
| 242 | static const char * const jack_events_name[] = {
|
| 243 | "HEADPHONE(0x0001)", "MICROPHONE(0x0002)", "LINEOUT(0x0004)",
|
| 244 | "MECHANICAL(0x0008)", "VIDEOOUT(0x0010)", "LINEIN(0x0020)",
|
| 245 | "USB(0x0040)", "", "", "BTN_5(0x0200)", "BTN_4(0x0400)",
|
| 246 | "BTN_3(0x0800)", "BTN_2(0x1000)", "BTN_1(0x2000)", "BTN_0(0x4000)",
|
| 247 | "",
|
| 248 | };
|
| 249 |
|
| 250 |
|
| 251 | static int parse_mask_bits(unsigned int mask_bits, char *buf, size_t buf_size)
|
| 252 | {
|
| 253 | int len = scnprintf(buf, buf_size, "0x%04x", mask_bits);
|
| 254 | int i;
|
| 255 |
|
| 256 | for (i = 0; i < ARRAY_SIZE(jack_events_name); i++)
|
| 257 | if (mask_bits & (1 << i))
|
| 258 | len += scnprintf(buf + len, buf_size - len, " %s", jack_events_name[i]);
|
| 259 | len += scnprintf(buf + len, buf_size - len, "\n");
|
| 260 |
|
| 261 | return len;
|
| 262 | }
|
| 263 |
|
| 264 | static ssize_t jack_kctl_mask_bits_read(struct file *file,
|
| 265 | char __user *to, size_t count, loff_t *ppos)
|
| 266 | {
|
| 267 | struct snd_jack_kctl *jack_kctl = file->private_data;
|
| 268 | char buf[256];
|
| 269 | int len, ret;
|
| 270 |
|
| 271 | len = parse_mask_bits(jack_kctl->mask_bits, buf, sizeof(buf));
|
| 272 | ret = simple_read_from_buffer(to, count, ppos, buf, len);
|
| 273 |
|
| 274 | return ret;
|
| 275 | }
|
| 276 |
|
| 277 | static ssize_t jack_kctl_status_read(struct file *file,
|
| 278 | char __user *to, size_t count, loff_t *ppos)
|
| 279 | {
|
| 280 | struct snd_jack_kctl *jack_kctl = file->private_data;
|
| 281 | char buf[16];
|
| 282 | int len, ret;
|
| 283 |
|
| 284 | len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->private_value ?
|
| 285 | "Plugged" : "Unplugged");
|
| 286 | ret = simple_read_from_buffer(to, count, ppos, buf, len);
|
| 287 |
|
| 288 | return ret;
|
| 289 | }
|
| 290 |
|
| 291 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 292 | static ssize_t jack_type_read(struct file *file,
|
| 293 | char __user *to, size_t count, loff_t *ppos)
|
| 294 | {
|
| 295 | struct snd_jack_kctl *jack_kctl = file->private_data;
|
| 296 | char buf[256];
|
| 297 | int len, ret;
|
| 298 |
|
| 299 | len = parse_mask_bits(jack_kctl->jack->type, buf, sizeof(buf));
|
| 300 | ret = simple_read_from_buffer(to, count, ppos, buf, len);
|
| 301 |
|
| 302 | return ret;
|
| 303 | }
|
| 304 |
|
| 305 | static const struct file_operations jack_type_fops = {
|
| 306 | .open = simple_open,
|
| 307 | .read = jack_type_read,
|
| 308 | .llseek = default_llseek,
|
| 309 | };
|
| 310 | #endif
|
| 311 |
|
| 312 | static const struct file_operations sw_inject_enable_fops = {
|
| 313 | .open = simple_open,
|
| 314 | .read = sw_inject_enable_read,
|
| 315 | .write = sw_inject_enable_write,
|
| 316 | .llseek = default_llseek,
|
| 317 | };
|
| 318 |
|
| 319 | static const struct file_operations jackin_inject_fops = {
|
| 320 | .open = simple_open,
|
| 321 | .write = jackin_inject_write,
|
| 322 | .llseek = default_llseek,
|
| 323 | };
|
| 324 |
|
| 325 | static const struct file_operations jack_kctl_id_fops = {
|
| 326 | .open = simple_open,
|
| 327 | .read = jack_kctl_id_read,
|
| 328 | .llseek = default_llseek,
|
| 329 | };
|
| 330 |
|
| 331 | static const struct file_operations jack_kctl_mask_bits_fops = {
|
| 332 | .open = simple_open,
|
| 333 | .read = jack_kctl_mask_bits_read,
|
| 334 | .llseek = default_llseek,
|
| 335 | };
|
| 336 |
|
| 337 | static const struct file_operations jack_kctl_status_fops = {
|
| 338 | .open = simple_open,
|
| 339 | .read = jack_kctl_status_read,
|
| 340 | .llseek = default_llseek,
|
| 341 | };
|
| 342 |
|
| 343 | static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
|
| 344 | struct snd_jack_kctl *jack_kctl)
|
| 345 | {
|
| 346 | char *tname;
|
| 347 | int i;
|
| 348 |
|
| 349 |
|
| 350 | if (strstr(jack_kctl->kctl->id.name, "Phantom"))
|
| 351 | return 0;
|
| 352 |
|
| 353 | tname = kstrdup(jack_kctl->kctl->id.name, GFP_KERNEL);
|
| 354 | if (!tname)
|
| 355 | return -ENOMEM;
|
| 356 |
|
| 357 |
|
| 358 | for (i = 0; tname[i]; i++)
|
| 359 | if (!isalnum(tname[i]))
|
| 360 | tname[i] = '_';
|
| 361 |
|
| 362 | jack_kctl->jack_debugfs_root = debugfs_create_dir(tname, jack->card->debugfs_root);
|
| 363 | kfree(tname);
|
| 364 |
|
| 365 | debugfs_create_file("sw_inject_enable", 0644, jack_kctl->jack_debugfs_root, jack_kctl,
|
| 366 | &sw_inject_enable_fops);
|
| 367 |
|
| 368 | debugfs_create_file("jackin_inject", 0200, jack_kctl->jack_debugfs_root, jack_kctl,
|
| 369 | &jackin_inject_fops);
|
| 370 |
|
| 371 | debugfs_create_file("kctl_id", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
|
| 372 | &jack_kctl_id_fops);
|
| 373 |
|
| 374 | debugfs_create_file("mask_bits", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
|
| 375 | &jack_kctl_mask_bits_fops);
|
| 376 |
|
| 377 | debugfs_create_file("status", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
|
| 378 | &jack_kctl_status_fops);
|
| 379 |
|
| 380 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 381 | debugfs_create_file("type", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
|
| 382 | &jack_type_fops);
|
| 383 | #endif
|
| 384 | return 0;
|
| 385 | }
|
| 386 |
|
| 387 | static void snd_jack_remove_debugfs(struct snd_jack *jack)
|
| 388 | {
|
| 389 | struct snd_jack_kctl *jack_kctl;
|
| 390 |
|
| 391 | list_for_each_entry(jack_kctl, &jack->kctl_list, list) {
|
| 392 | debugfs_remove(jack_kctl->jack_debugfs_root);
|
| 393 | jack_kctl->jack_debugfs_root = NULL;
|
| 394 | }
|
| 395 | }
|
| 396 | #else
|
| 397 | static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
|
| 398 | struct snd_jack_kctl *jack_kctl)
|
| 399 | {
|
| 400 | return 0;
|
| 401 | }
|
| 402 |
|
| 403 | static void snd_jack_remove_debugfs(struct snd_jack *jack)
|
| 404 | {
|
| 405 | }
|
| 406 | #endif
|
| 407 |
|
| 408 | static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
|
| 409 | {
|
| 410 | struct snd_jack_kctl *jack_kctl;
|
| 411 |
|
| 412 | jack_kctl = kctl->private_data;
|
| 413 | if (jack_kctl) {
|
| 414 | list_del(&jack_kctl->list);
|
| 415 | kfree(jack_kctl);
|
| 416 | }
|
| 417 | }
|
| 418 |
|
| 419 | static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
|
| 420 | {
|
| 421 | jack_kctl->jack = jack;
|
| 422 | list_add_tail(&jack_kctl->list, &jack->kctl_list);
|
| 423 | snd_jack_debugfs_add_inject_node(jack, jack_kctl);
|
| 424 | }
|
| 425 |
|
| 426 | static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
|
| 427 | {
|
| 428 | struct snd_kcontrol *kctl;
|
| 429 | struct snd_jack_kctl *jack_kctl;
|
| 430 | int err;
|
| 431 |
|
| 432 | kctl = snd_kctl_jack_new(name, card);
|
| 433 | if (!kctl)
|
| 434 | return NULL;
|
| 435 |
|
| 436 | err = snd_ctl_add(card, kctl);
|
| 437 | if (err < 0)
|
| 438 | return NULL;
|
| 439 |
|
| 440 | jack_kctl = kzalloc_obj(*jack_kctl);
|
| 441 |
|
| 442 | if (!jack_kctl)
|
| 443 | goto error;
|
| 444 |
|
| 445 | jack_kctl->kctl = kctl;
|
| 446 | jack_kctl->mask_bits = mask;
|
| 447 |
|
| 448 | kctl->private_data = jack_kctl;
|
| 449 | kctl->private_free = snd_jack_kctl_private_free;
|
| 450 |
|
| 451 | return jack_kctl;
|
| 452 | error:
|
| 453 | snd_ctl_free_one(kctl);
|
| 454 | return NULL;
|
| 455 | }
|
| 456 |
|
| 457 | |
| 458 | * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
|
| 459 | * @jack: the jack instance which the kctl will attaching to
|
| 460 | * @name: the name for the snd_kcontrol object
|
| 461 | * @mask: a bitmask of enum snd_jack_type values that can be detected
|
| 462 | * by this snd_jack_kctl object.
|
| 463 | *
|
| 464 | * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
|
| 465 | *
|
| 466 | * Return: Zero if successful, or a negative error code on failure.
|
| 467 |
|
| 468 | int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
|
| 469 | {
|
| 470 | struct snd_jack_kctl *jack_kctl;
|
| 471 |
|
| 472 | jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
|
| 473 | if (!jack_kctl)
|
| 474 | return -ENOMEM;
|
| 475 |
|
| 476 | snd_jack_kctl_add(jack, jack_kctl);
|
| 477 | return 0;
|
| 478 | }
|
| 479 | EXPORT_SYMBOL(snd_jack_add_new_kctl);
|
| 480 |
|
| 481 | |
| 482 | * snd_jack_new - Create a new jack
|
| 483 | * @card: the card instance
|
| 484 | * @id: an identifying string for this jack
|
| 485 | * @type: a bitmask of enum snd_jack_type values that can be detected by
|
| 486 | * this jack
|
| 487 | * @jjack: Used to provide the allocated jack object to the caller.
|
| 488 | * @initial_kctl: if true, create a kcontrol and add it to the jack list.
|
| 489 | * @phantom_jack: Don't create a input device for phantom jacks.
|
| 490 | *
|
| 491 | * Creates a new jack object.
|
| 492 | *
|
| 493 | * Return: Zero if successful, or a negative error code on failure.
|
| 494 | * On success @jjack will be initialised.
|
| 495 |
|
| 496 | int snd_jack_new(struct snd_card *card, const char *id, int type,
|
| 497 | struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
|
| 498 | {
|
| 499 | struct snd_jack *jack;
|
| 500 | struct snd_jack_kctl *jack_kctl = NULL;
|
| 501 | int err;
|
| 502 | static const struct snd_device_ops ops = {
|
| 503 | .dev_free = snd_jack_dev_free,
|
| 504 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 505 | .dev_register = snd_jack_dev_register,
|
| 506 | #endif
|
| 507 | .dev_disconnect = snd_jack_dev_disconnect,
|
| 508 | };
|
| 509 |
|
| 510 | if (initial_kctl) {
|
| 511 | jack_kctl = snd_jack_kctl_new(card, id, type);
|
| 512 | if (!jack_kctl)
|
| 513 | return -ENOMEM;
|
| 514 | }
|
| 515 |
|
| 516 | jack = kzalloc_obj(struct snd_jack);
|
| 517 | if (jack == NULL)
|
| 518 | return -ENOMEM;
|
| 519 |
|
| 520 | jack->id = kstrdup(id, GFP_KERNEL);
|
| 521 | if (jack->id == NULL) {
|
| 522 | kfree(jack);
|
| 523 | return -ENOMEM;
|
| 524 | }
|
| 525 |
|
| 526 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 527 | mutex_init(&jack->input_dev_lock);
|
| 528 |
|
| 529 |
|
| 530 | if (!phantom_jack) {
|
| 531 | int i;
|
| 532 |
|
| 533 | jack->input_dev = input_allocate_device();
|
| 534 | if (jack->input_dev == NULL) {
|
| 535 | err = -ENOMEM;
|
| 536 | goto fail_input;
|
| 537 | }
|
| 538 |
|
| 539 | jack->input_dev->phys = "ALSA";
|
| 540 |
|
| 541 | jack->type = type;
|
| 542 |
|
| 543 | for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
|
| 544 | if (type & (1 << i))
|
| 545 | input_set_capability(jack->input_dev, EV_SW,
|
| 546 | jack_switch_types[i]);
|
| 547 |
|
| 548 | }
|
| 549 | #endif
|
| 550 |
|
| 551 | err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
|
| 552 | if (err < 0)
|
| 553 | goto fail_input;
|
| 554 |
|
| 555 | jack->card = card;
|
| 556 | INIT_LIST_HEAD(&jack->kctl_list);
|
| 557 |
|
| 558 | if (initial_kctl)
|
| 559 | snd_jack_kctl_add(jack, jack_kctl);
|
| 560 |
|
| 561 | *jjack = jack;
|
| 562 |
|
| 563 | return 0;
|
| 564 |
|
| 565 | fail_input:
|
| 566 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 567 | input_free_device(jack->input_dev);
|
| 568 | #endif
|
| 569 | kfree(jack->id);
|
| 570 | kfree(jack);
|
| 571 | return err;
|
| 572 | }
|
| 573 | EXPORT_SYMBOL(snd_jack_new);
|
| 574 |
|
| 575 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 576 | |
| 577 | * snd_jack_set_key - Set a key mapping on a jack
|
| 578 | *
|
| 579 | * @jack: The jack to configure
|
| 580 | * @type: Jack report type for this key
|
| 581 | * @keytype: Input layer key type to be reported
|
| 582 | *
|
| 583 | * Map a SND_JACK_BTN_* button type to an input layer key, allowing
|
| 584 | * reporting of keys on accessories via the jack abstraction. If no
|
| 585 | * mapping is provided but keys are enabled in the jack type then
|
| 586 | * BTN_n numeric buttons will be reported.
|
| 587 | *
|
| 588 | * If jacks are not reporting via the input API this call will have no
|
| 589 | * effect.
|
| 590 | *
|
| 591 | * Note that this is intended to be use by simple devices with small
|
| 592 | * numbers of keys that can be reported. It is also possible to
|
| 593 | * access the input device directly - devices with complex input
|
| 594 | * capabilities on accessories should consider doing this rather than
|
| 595 | * using this abstraction.
|
| 596 | *
|
| 597 | * This function may only be called prior to registration of the jack.
|
| 598 | *
|
| 599 | * Return: Zero if successful, or a negative error code on failure.
|
| 600 |
|
| 601 | int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
|
| 602 | int keytype)
|
| 603 | {
|
| 604 | int key = fls(SND_JACK_BTN_0) - fls(type);
|
| 605 |
|
| 606 | WARN_ON(jack->registered);
|
| 607 |
|
| 608 | if (!keytype || key >= ARRAY_SIZE(jack->key))
|
| 609 | return -EINVAL;
|
| 610 |
|
| 611 | jack->type |= type;
|
| 612 | jack->key[key] = keytype;
|
| 613 | return 0;
|
| 614 | }
|
| 615 | EXPORT_SYMBOL(snd_jack_set_key);
|
| 616 | #endif
|
| 617 |
|
| 618 | |
| 619 | * snd_jack_report - Report the current status of a jack
|
| 620 | * Note: This function uses mutexes and should be called from a
|
| 621 | * context which can sleep (such as a workqueue).
|
| 622 | *
|
| 623 | * @jack: The jack to report status for
|
| 624 | * @status: The current status of the jack
|
| 625 |
|
| 626 | void snd_jack_report(struct snd_jack *jack, int status)
|
| 627 | {
|
| 628 | struct snd_jack_kctl *jack_kctl;
|
| 629 | unsigned int mask_bits = 0;
|
| 630 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 631 | struct input_dev *idev;
|
| 632 | int i;
|
| 633 | #endif
|
| 634 |
|
| 635 | if (!jack)
|
| 636 | return;
|
| 637 |
|
| 638 | jack->hw_status_cache = status;
|
| 639 |
|
| 640 | list_for_each_entry(jack_kctl, &jack->kctl_list, list)
|
| 641 | if (jack_kctl->sw_inject_enable)
|
| 642 | mask_bits |= jack_kctl->mask_bits;
|
| 643 | else
|
| 644 | snd_kctl_jack_report(jack->card, jack_kctl->kctl,
|
| 645 | status & jack_kctl->mask_bits);
|
| 646 |
|
| 647 | #ifdef CONFIG_SND_JACK_INPUT_DEV
|
| 648 | idev = input_get_device(jack->input_dev);
|
| 649 | if (!idev)
|
| 650 | return;
|
| 651 |
|
| 652 | for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
|
| 653 | int testbit = ((SND_JACK_BTN_0 >> i) & ~mask_bits);
|
| 654 |
|
| 655 | if (jack->type & testbit)
|
| 656 | input_report_key(idev, jack->key[i],
|
| 657 | status & testbit);
|
| 658 | }
|
| 659 |
|
| 660 | for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
|
| 661 | int testbit = ((1 << i) & ~mask_bits);
|
| 662 |
|
| 663 | if (jack->type & testbit)
|
| 664 | input_report_switch(idev,
|
| 665 | jack_switch_types[i],
|
| 666 | status & testbit);
|
| 667 | }
|
| 668 |
|
| 669 | input_sync(idev);
|
| 670 | input_put_device(idev);
|
| 671 | #endif
|
| 672 | }
|
| 673 | EXPORT_SYMBOL(snd_jack_report);
|