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