Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Jack abstraction layer
0004  *
0005  *  Copyright 2008 Wolfson Microelectronics
0006  */
0007 
0008 #include <linux/input.h>
0009 #include <linux/slab.h>
0010 #include <linux/module.h>
0011 #include <linux/ctype.h>
0012 #include <linux/mm.h>
0013 #include <linux/debugfs.h>
0014 #include <sound/jack.h>
0015 #include <sound/core.h>
0016 #include <sound/control.h>
0017 
0018 struct snd_jack_kctl {
0019     struct snd_kcontrol *kctl;
0020     struct list_head list;  /* list of controls belong to the same jack */
0021     unsigned int mask_bits; /* only masked status bits are reported via kctl */
0022     struct snd_jack *jack;  /* pointer to struct snd_jack */
0023     bool sw_inject_enable;  /* allow to inject plug event via debugfs */
0024 #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
0025     struct dentry *jack_debugfs_root; /* jack_kctl debugfs root */
0026 #endif
0027 };
0028 
0029 #ifdef CONFIG_SND_JACK_INPUT_DEV
0030 static const int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
0031     SW_HEADPHONE_INSERT,
0032     SW_MICROPHONE_INSERT,
0033     SW_LINEOUT_INSERT,
0034     SW_JACK_PHYSICAL_INSERT,
0035     SW_VIDEOOUT_INSERT,
0036     SW_LINEIN_INSERT,
0037 };
0038 #endif /* CONFIG_SND_JACK_INPUT_DEV */
0039 
0040 static int snd_jack_dev_disconnect(struct snd_device *device)
0041 {
0042 #ifdef CONFIG_SND_JACK_INPUT_DEV
0043     struct snd_jack *jack = device->device_data;
0044 
0045     mutex_lock(&jack->input_dev_lock);
0046     if (!jack->input_dev) {
0047         mutex_unlock(&jack->input_dev_lock);
0048         return 0;
0049     }
0050 
0051     /* If the input device is registered with the input subsystem
0052      * then we need to use a different deallocator. */
0053     if (jack->registered)
0054         input_unregister_device(jack->input_dev);
0055     else
0056         input_free_device(jack->input_dev);
0057     jack->input_dev = NULL;
0058     mutex_unlock(&jack->input_dev_lock);
0059 #endif /* CONFIG_SND_JACK_INPUT_DEV */
0060     return 0;
0061 }
0062 
0063 static int snd_jack_dev_free(struct snd_device *device)
0064 {
0065     struct snd_jack *jack = device->device_data;
0066     struct snd_card *card = device->card;
0067     struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
0068 
0069     down_write(&card->controls_rwsem);
0070     list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
0071         list_del_init(&jack_kctl->list);
0072         snd_ctl_remove(card, jack_kctl->kctl);
0073     }
0074     up_write(&card->controls_rwsem);
0075 
0076     if (jack->private_free)
0077         jack->private_free(jack);
0078 
0079     snd_jack_dev_disconnect(device);
0080 
0081     kfree(jack->id);
0082     kfree(jack);
0083 
0084     return 0;
0085 }
0086 
0087 #ifdef CONFIG_SND_JACK_INPUT_DEV
0088 static int snd_jack_dev_register(struct snd_device *device)
0089 {
0090     struct snd_jack *jack = device->device_data;
0091     struct snd_card *card = device->card;
0092     int err, i;
0093 
0094     snprintf(jack->name, sizeof(jack->name), "%s %s",
0095          card->shortname, jack->id);
0096 
0097     mutex_lock(&jack->input_dev_lock);
0098     if (!jack->input_dev) {
0099         mutex_unlock(&jack->input_dev_lock);
0100         return 0;
0101     }
0102 
0103     jack->input_dev->name = jack->name;
0104 
0105     /* Default to the sound card device. */
0106     if (!jack->input_dev->dev.parent)
0107         jack->input_dev->dev.parent = snd_card_get_device_link(card);
0108 
0109     /* Add capabilities for any keys that are enabled */
0110     for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
0111         int testbit = SND_JACK_BTN_0 >> i;
0112 
0113         if (!(jack->type & testbit))
0114             continue;
0115 
0116         if (!jack->key[i])
0117             jack->key[i] = BTN_0 + i;
0118 
0119         input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
0120     }
0121 
0122     err = input_register_device(jack->input_dev);
0123     if (err == 0)
0124         jack->registered = 1;
0125 
0126     mutex_unlock(&jack->input_dev_lock);
0127     return err;
0128 }
0129 #endif /* CONFIG_SND_JACK_INPUT_DEV */
0130 
0131 #ifdef CONFIG_SND_JACK_INJECTION_DEBUG
0132 static void snd_jack_inject_report(struct snd_jack_kctl *jack_kctl, int status)
0133 {
0134     struct snd_jack *jack;
0135 #ifdef CONFIG_SND_JACK_INPUT_DEV
0136     int i;
0137 #endif
0138     if (!jack_kctl)
0139         return;
0140 
0141     jack = jack_kctl->jack;
0142 
0143     if (jack_kctl->sw_inject_enable)
0144         snd_kctl_jack_report(jack->card, jack_kctl->kctl,
0145                      status & jack_kctl->mask_bits);
0146 
0147 #ifdef CONFIG_SND_JACK_INPUT_DEV
0148     if (!jack->input_dev)
0149         return;
0150 
0151     for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
0152         int testbit = ((SND_JACK_BTN_0 >> i) & jack_kctl->mask_bits);
0153 
0154         if (jack->type & testbit)
0155             input_report_key(jack->input_dev, jack->key[i],
0156                      status & testbit);
0157     }
0158 
0159     for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
0160         int testbit = ((1 << i) & jack_kctl->mask_bits);
0161 
0162         if (jack->type & testbit)
0163             input_report_switch(jack->input_dev,
0164                         jack_switch_types[i],
0165                         status & testbit);
0166     }
0167 
0168     input_sync(jack->input_dev);
0169 #endif /* CONFIG_SND_JACK_INPUT_DEV */
0170 }
0171 
0172 static ssize_t sw_inject_enable_read(struct file *file,
0173                      char __user *to, size_t count, loff_t *ppos)
0174 {
0175     struct snd_jack_kctl *jack_kctl = file->private_data;
0176     int len, ret;
0177     char buf[128];
0178 
0179     len = scnprintf(buf, sizeof(buf), "%s: %s\t\t%s: %i\n", "Jack", jack_kctl->kctl->id.name,
0180             "Inject Enabled", jack_kctl->sw_inject_enable);
0181     ret = simple_read_from_buffer(to, count, ppos, buf, len);
0182 
0183     return ret;
0184 }
0185 
0186 static ssize_t sw_inject_enable_write(struct file *file,
0187                       const char __user *from, size_t count, loff_t *ppos)
0188 {
0189     struct snd_jack_kctl *jack_kctl = file->private_data;
0190     int ret, err;
0191     unsigned long enable;
0192     char buf[8] = { 0 };
0193 
0194     ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
0195     err = kstrtoul(buf, 0, &enable);
0196     if (err)
0197         return err;
0198 
0199     if (jack_kctl->sw_inject_enable == (!!enable))
0200         return ret;
0201 
0202     jack_kctl->sw_inject_enable = !!enable;
0203 
0204     if (!jack_kctl->sw_inject_enable)
0205         snd_jack_report(jack_kctl->jack, jack_kctl->jack->hw_status_cache);
0206 
0207     return ret;
0208 }
0209 
0210 static ssize_t jackin_inject_write(struct file *file,
0211                    const char __user *from, size_t count, loff_t *ppos)
0212 {
0213     struct snd_jack_kctl *jack_kctl = file->private_data;
0214     int ret, err;
0215     unsigned long enable;
0216     char buf[8] = { 0 };
0217 
0218     if (!jack_kctl->sw_inject_enable)
0219         return -EINVAL;
0220 
0221     ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, from, count);
0222     err = kstrtoul(buf, 0, &enable);
0223     if (err)
0224         return err;
0225 
0226     snd_jack_inject_report(jack_kctl, !!enable ? jack_kctl->mask_bits : 0);
0227 
0228     return ret;
0229 }
0230 
0231 static ssize_t jack_kctl_id_read(struct file *file,
0232                  char __user *to, size_t count, loff_t *ppos)
0233 {
0234     struct snd_jack_kctl *jack_kctl = file->private_data;
0235     char buf[64];
0236     int len, ret;
0237 
0238     len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->id.name);
0239     ret = simple_read_from_buffer(to, count, ppos, buf, len);
0240 
0241     return ret;
0242 }
0243 
0244 /* the bit definition is aligned with snd_jack_types in jack.h */
0245 static const char * const jack_events_name[] = {
0246     "HEADPHONE(0x0001)", "MICROPHONE(0x0002)", "LINEOUT(0x0004)",
0247     "MECHANICAL(0x0008)", "VIDEOOUT(0x0010)", "LINEIN(0x0020)",
0248     "", "", "", "BTN_5(0x0200)", "BTN_4(0x0400)", "BTN_3(0x0800)",
0249     "BTN_2(0x1000)", "BTN_1(0x2000)", "BTN_0(0x4000)", "",
0250 };
0251 
0252 /* the recommended buffer size is 256 */
0253 static int parse_mask_bits(unsigned int mask_bits, char *buf, size_t buf_size)
0254 {
0255     int i;
0256 
0257     scnprintf(buf, buf_size, "0x%04x", mask_bits);
0258 
0259     for (i = 0; i < ARRAY_SIZE(jack_events_name); i++)
0260         if (mask_bits & (1 << i)) {
0261             strlcat(buf, " ", buf_size);
0262             strlcat(buf, jack_events_name[i], buf_size);
0263         }
0264     strlcat(buf, "\n", buf_size);
0265 
0266     return strlen(buf);
0267 }
0268 
0269 static ssize_t jack_kctl_mask_bits_read(struct file *file,
0270                     char __user *to, size_t count, loff_t *ppos)
0271 {
0272     struct snd_jack_kctl *jack_kctl = file->private_data;
0273     char buf[256];
0274     int len, ret;
0275 
0276     len = parse_mask_bits(jack_kctl->mask_bits, buf, sizeof(buf));
0277     ret = simple_read_from_buffer(to, count, ppos, buf, len);
0278 
0279     return ret;
0280 }
0281 
0282 static ssize_t jack_kctl_status_read(struct file *file,
0283                      char __user *to, size_t count, loff_t *ppos)
0284 {
0285     struct snd_jack_kctl *jack_kctl = file->private_data;
0286     char buf[16];
0287     int len, ret;
0288 
0289     len = scnprintf(buf, sizeof(buf), "%s\n", jack_kctl->kctl->private_value ?
0290             "Plugged" : "Unplugged");
0291     ret = simple_read_from_buffer(to, count, ppos, buf, len);
0292 
0293     return ret;
0294 }
0295 
0296 #ifdef CONFIG_SND_JACK_INPUT_DEV
0297 static ssize_t jack_type_read(struct file *file,
0298                   char __user *to, size_t count, loff_t *ppos)
0299 {
0300     struct snd_jack_kctl *jack_kctl = file->private_data;
0301     char buf[256];
0302     int len, ret;
0303 
0304     len = parse_mask_bits(jack_kctl->jack->type, buf, sizeof(buf));
0305     ret = simple_read_from_buffer(to, count, ppos, buf, len);
0306 
0307     return ret;
0308 }
0309 
0310 static const struct file_operations jack_type_fops = {
0311     .open = simple_open,
0312     .read = jack_type_read,
0313     .llseek = default_llseek,
0314 };
0315 #endif
0316 
0317 static const struct file_operations sw_inject_enable_fops = {
0318     .open = simple_open,
0319     .read = sw_inject_enable_read,
0320     .write = sw_inject_enable_write,
0321     .llseek = default_llseek,
0322 };
0323 
0324 static const struct file_operations jackin_inject_fops = {
0325     .open = simple_open,
0326     .write = jackin_inject_write,
0327     .llseek = default_llseek,
0328 };
0329 
0330 static const struct file_operations jack_kctl_id_fops = {
0331     .open = simple_open,
0332     .read = jack_kctl_id_read,
0333     .llseek = default_llseek,
0334 };
0335 
0336 static const struct file_operations jack_kctl_mask_bits_fops = {
0337     .open = simple_open,
0338     .read = jack_kctl_mask_bits_read,
0339     .llseek = default_llseek,
0340 };
0341 
0342 static const struct file_operations jack_kctl_status_fops = {
0343     .open = simple_open,
0344     .read = jack_kctl_status_read,
0345     .llseek = default_llseek,
0346 };
0347 
0348 static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
0349                         struct snd_jack_kctl *jack_kctl)
0350 {
0351     char *tname;
0352     int i;
0353 
0354     /* Don't create injection interface for Phantom jacks */
0355     if (strstr(jack_kctl->kctl->id.name, "Phantom"))
0356         return 0;
0357 
0358     tname = kstrdup(jack_kctl->kctl->id.name, GFP_KERNEL);
0359     if (!tname)
0360         return -ENOMEM;
0361 
0362     /* replace the chars which are not suitable for folder's name with _ */
0363     for (i = 0; tname[i]; i++)
0364         if (!isalnum(tname[i]))
0365             tname[i] = '_';
0366 
0367     jack_kctl->jack_debugfs_root = debugfs_create_dir(tname, jack->card->debugfs_root);
0368     kfree(tname);
0369 
0370     debugfs_create_file("sw_inject_enable", 0644, jack_kctl->jack_debugfs_root, jack_kctl,
0371                 &sw_inject_enable_fops);
0372 
0373     debugfs_create_file("jackin_inject", 0200, jack_kctl->jack_debugfs_root, jack_kctl,
0374                 &jackin_inject_fops);
0375 
0376     debugfs_create_file("kctl_id", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
0377                 &jack_kctl_id_fops);
0378 
0379     debugfs_create_file("mask_bits", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
0380                 &jack_kctl_mask_bits_fops);
0381 
0382     debugfs_create_file("status", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
0383                 &jack_kctl_status_fops);
0384 
0385 #ifdef CONFIG_SND_JACK_INPUT_DEV
0386     debugfs_create_file("type", 0444, jack_kctl->jack_debugfs_root, jack_kctl,
0387                 &jack_type_fops);
0388 #endif
0389     return 0;
0390 }
0391 
0392 static void snd_jack_debugfs_clear_inject_node(struct snd_jack_kctl *jack_kctl)
0393 {
0394     debugfs_remove(jack_kctl->jack_debugfs_root);
0395     jack_kctl->jack_debugfs_root = NULL;
0396 }
0397 #else /* CONFIG_SND_JACK_INJECTION_DEBUG */
0398 static int snd_jack_debugfs_add_inject_node(struct snd_jack *jack,
0399                         struct snd_jack_kctl *jack_kctl)
0400 {
0401     return 0;
0402 }
0403 
0404 static void snd_jack_debugfs_clear_inject_node(struct snd_jack_kctl *jack_kctl)
0405 {
0406 }
0407 #endif /* CONFIG_SND_JACK_INJECTION_DEBUG */
0408 
0409 static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
0410 {
0411     struct snd_jack_kctl *jack_kctl;
0412 
0413     jack_kctl = kctl->private_data;
0414     if (jack_kctl) {
0415         snd_jack_debugfs_clear_inject_node(jack_kctl);
0416         list_del(&jack_kctl->list);
0417         kfree(jack_kctl);
0418     }
0419 }
0420 
0421 static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
0422 {
0423     jack_kctl->jack = jack;
0424     list_add_tail(&jack_kctl->list, &jack->kctl_list);
0425     snd_jack_debugfs_add_inject_node(jack, jack_kctl);
0426 }
0427 
0428 static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
0429 {
0430     struct snd_kcontrol *kctl;
0431     struct snd_jack_kctl *jack_kctl;
0432     int err;
0433 
0434     kctl = snd_kctl_jack_new(name, card);
0435     if (!kctl)
0436         return NULL;
0437 
0438     err = snd_ctl_add(card, kctl);
0439     if (err < 0)
0440         return NULL;
0441 
0442     jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
0443 
0444     if (!jack_kctl)
0445         goto error;
0446 
0447     jack_kctl->kctl = kctl;
0448     jack_kctl->mask_bits = mask;
0449 
0450     kctl->private_data = jack_kctl;
0451     kctl->private_free = snd_jack_kctl_private_free;
0452 
0453     return jack_kctl;
0454 error:
0455     snd_ctl_free_one(kctl);
0456     return NULL;
0457 }
0458 
0459 /**
0460  * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
0461  * @jack:  the jack instance which the kctl will attaching to
0462  * @name:  the name for the snd_kcontrol object
0463  * @mask:  a bitmask of enum snd_jack_type values that can be detected
0464  *         by this snd_jack_kctl object.
0465  *
0466  * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
0467  *
0468  * Return: Zero if successful, or a negative error code on failure.
0469  */
0470 int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
0471 {
0472     struct snd_jack_kctl *jack_kctl;
0473 
0474     jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
0475     if (!jack_kctl)
0476         return -ENOMEM;
0477 
0478     snd_jack_kctl_add(jack, jack_kctl);
0479     return 0;
0480 }
0481 EXPORT_SYMBOL(snd_jack_add_new_kctl);
0482 
0483 /**
0484  * snd_jack_new - Create a new jack
0485  * @card:  the card instance
0486  * @id:    an identifying string for this jack
0487  * @type:  a bitmask of enum snd_jack_type values that can be detected by
0488  *         this jack
0489  * @jjack: Used to provide the allocated jack object to the caller.
0490  * @initial_kctl: if true, create a kcontrol and add it to the jack list.
0491  * @phantom_jack: Don't create a input device for phantom jacks.
0492  *
0493  * Creates a new jack object.
0494  *
0495  * Return: Zero if successful, or a negative error code on failure.
0496  * On success @jjack will be initialised.
0497  */
0498 int snd_jack_new(struct snd_card *card, const char *id, int type,
0499          struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
0500 {
0501     struct snd_jack *jack;
0502     struct snd_jack_kctl *jack_kctl = NULL;
0503     int err;
0504     static const struct snd_device_ops ops = {
0505         .dev_free = snd_jack_dev_free,
0506 #ifdef CONFIG_SND_JACK_INPUT_DEV
0507         .dev_register = snd_jack_dev_register,
0508         .dev_disconnect = snd_jack_dev_disconnect,
0509 #endif /* CONFIG_SND_JACK_INPUT_DEV */
0510     };
0511 
0512     if (initial_kctl) {
0513         jack_kctl = snd_jack_kctl_new(card, id, type);
0514         if (!jack_kctl)
0515             return -ENOMEM;
0516     }
0517 
0518     jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
0519     if (jack == NULL)
0520         return -ENOMEM;
0521 
0522     jack->id = kstrdup(id, GFP_KERNEL);
0523     if (jack->id == NULL) {
0524         kfree(jack);
0525         return -ENOMEM;
0526     }
0527 
0528 #ifdef CONFIG_SND_JACK_INPUT_DEV
0529     mutex_init(&jack->input_dev_lock);
0530 
0531     /* don't create input device for phantom jack */
0532     if (!phantom_jack) {
0533         int i;
0534 
0535         jack->input_dev = input_allocate_device();
0536         if (jack->input_dev == NULL) {
0537             err = -ENOMEM;
0538             goto fail_input;
0539         }
0540 
0541         jack->input_dev->phys = "ALSA";
0542 
0543         jack->type = type;
0544 
0545         for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
0546             if (type & (1 << i))
0547                 input_set_capability(jack->input_dev, EV_SW,
0548                              jack_switch_types[i]);
0549 
0550     }
0551 #endif /* CONFIG_SND_JACK_INPUT_DEV */
0552 
0553     err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
0554     if (err < 0)
0555         goto fail_input;
0556 
0557     jack->card = card;
0558     INIT_LIST_HEAD(&jack->kctl_list);
0559 
0560     if (initial_kctl)
0561         snd_jack_kctl_add(jack, jack_kctl);
0562 
0563     *jjack = jack;
0564 
0565     return 0;
0566 
0567 fail_input:
0568 #ifdef CONFIG_SND_JACK_INPUT_DEV
0569     input_free_device(jack->input_dev);
0570 #endif
0571     kfree(jack->id);
0572     kfree(jack);
0573     return err;
0574 }
0575 EXPORT_SYMBOL(snd_jack_new);
0576 
0577 #ifdef CONFIG_SND_JACK_INPUT_DEV
0578 /**
0579  * snd_jack_set_parent - Set the parent device for a jack
0580  *
0581  * @jack:   The jack to configure
0582  * @parent: The device to set as parent for the jack.
0583  *
0584  * Set the parent for the jack devices in the device tree.  This
0585  * function is only valid prior to registration of the jack.  If no
0586  * parent is configured then the parent device will be the sound card.
0587  */
0588 void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
0589 {
0590     WARN_ON(jack->registered);
0591     mutex_lock(&jack->input_dev_lock);
0592     if (!jack->input_dev) {
0593         mutex_unlock(&jack->input_dev_lock);
0594         return;
0595     }
0596 
0597     jack->input_dev->dev.parent = parent;
0598     mutex_unlock(&jack->input_dev_lock);
0599 }
0600 EXPORT_SYMBOL(snd_jack_set_parent);
0601 
0602 /**
0603  * snd_jack_set_key - Set a key mapping on a jack
0604  *
0605  * @jack:    The jack to configure
0606  * @type:    Jack report type for this key
0607  * @keytype: Input layer key type to be reported
0608  *
0609  * Map a SND_JACK_BTN_* button type to an input layer key, allowing
0610  * reporting of keys on accessories via the jack abstraction.  If no
0611  * mapping is provided but keys are enabled in the jack type then
0612  * BTN_n numeric buttons will be reported.
0613  *
0614  * If jacks are not reporting via the input API this call will have no
0615  * effect.
0616  *
0617  * Note that this is intended to be use by simple devices with small
0618  * numbers of keys that can be reported.  It is also possible to
0619  * access the input device directly - devices with complex input
0620  * capabilities on accessories should consider doing this rather than
0621  * using this abstraction.
0622  *
0623  * This function may only be called prior to registration of the jack.
0624  *
0625  * Return: Zero if successful, or a negative error code on failure.
0626  */
0627 int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
0628              int keytype)
0629 {
0630     int key = fls(SND_JACK_BTN_0) - fls(type);
0631 
0632     WARN_ON(jack->registered);
0633 
0634     if (!keytype || key >= ARRAY_SIZE(jack->key))
0635         return -EINVAL;
0636 
0637     jack->type |= type;
0638     jack->key[key] = keytype;
0639     return 0;
0640 }
0641 EXPORT_SYMBOL(snd_jack_set_key);
0642 #endif /* CONFIG_SND_JACK_INPUT_DEV */
0643 
0644 /**
0645  * snd_jack_report - Report the current status of a jack
0646  * Note: This function uses mutexes and should be called from a
0647  * context which can sleep (such as a workqueue).
0648  *
0649  * @jack:   The jack to report status for
0650  * @status: The current status of the jack
0651  */
0652 void snd_jack_report(struct snd_jack *jack, int status)
0653 {
0654     struct snd_jack_kctl *jack_kctl;
0655     unsigned int mask_bits = 0;
0656 #ifdef CONFIG_SND_JACK_INPUT_DEV
0657     int i;
0658 #endif
0659 
0660     if (!jack)
0661         return;
0662 
0663     jack->hw_status_cache = status;
0664 
0665     list_for_each_entry(jack_kctl, &jack->kctl_list, list)
0666         if (jack_kctl->sw_inject_enable)
0667             mask_bits |= jack_kctl->mask_bits;
0668         else
0669             snd_kctl_jack_report(jack->card, jack_kctl->kctl,
0670                          status & jack_kctl->mask_bits);
0671 
0672 #ifdef CONFIG_SND_JACK_INPUT_DEV
0673     mutex_lock(&jack->input_dev_lock);
0674     if (!jack->input_dev) {
0675         mutex_unlock(&jack->input_dev_lock);
0676         return;
0677     }
0678 
0679     for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
0680         int testbit = ((SND_JACK_BTN_0 >> i) & ~mask_bits);
0681 
0682         if (jack->type & testbit)
0683             input_report_key(jack->input_dev, jack->key[i],
0684                      status & testbit);
0685     }
0686 
0687     for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
0688         int testbit = ((1 << i) & ~mask_bits);
0689 
0690         if (jack->type & testbit)
0691             input_report_switch(jack->input_dev,
0692                         jack_switch_types[i],
0693                         status & testbit);
0694     }
0695 
0696     input_sync(jack->input_dev);
0697     mutex_unlock(&jack->input_dev_lock);
0698 #endif /* CONFIG_SND_JACK_INPUT_DEV */
0699 }
0700 EXPORT_SYMBOL(snd_jack_report);