jack.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #include <linux/input.h>
  22. #include <linux/slab.h>
  23. #include <linux/module.h>
  24. #include <sound/jack.h>
  25. #include <sound/core.h>
  26. #include <sound/control.h>
  27. struct snd_jack_kctl {
  28. struct snd_kcontrol *kctl;
  29. struct list_head list; /* list of controls belong to the same jack */
  30. unsigned int mask_bits; /* only masked status bits are reported via kctl */
  31. };
  32. static int jack_switch_types[SND_JACK_SWITCH_TYPES] = {
  33. SW_HEADPHONE_INSERT,
  34. SW_MICROPHONE_INSERT,
  35. SW_LINEOUT_INSERT,
  36. SW_JACK_PHYSICAL_INSERT,
  37. SW_VIDEOOUT_INSERT,
  38. SW_LINEIN_INSERT,
  39. };
  40. static int snd_jack_dev_disconnect(struct snd_device *device)
  41. {
  42. struct snd_jack *jack = device->device_data;
  43. if (!jack->input_dev)
  44. return 0;
  45. /* If the input device is registered with the input subsystem
  46. * then we need to use a different deallocator. */
  47. if (jack->registered)
  48. input_unregister_device(jack->input_dev);
  49. else
  50. input_free_device(jack->input_dev);
  51. jack->input_dev = NULL;
  52. return 0;
  53. }
  54. static int snd_jack_dev_free(struct snd_device *device)
  55. {
  56. struct snd_jack *jack = device->device_data;
  57. struct snd_card *card = device->card;
  58. struct snd_jack_kctl *jack_kctl, *tmp_jack_kctl;
  59. list_for_each_entry_safe(jack_kctl, tmp_jack_kctl, &jack->kctl_list, list) {
  60. list_del_init(&jack_kctl->list);
  61. snd_ctl_remove(card, jack_kctl->kctl);
  62. }
  63. if (jack->private_free)
  64. jack->private_free(jack);
  65. snd_jack_dev_disconnect(device);
  66. kfree(jack->id);
  67. kfree(jack);
  68. return 0;
  69. }
  70. static int snd_jack_dev_register(struct snd_device *device)
  71. {
  72. struct snd_jack *jack = device->device_data;
  73. struct snd_card *card = device->card;
  74. int err, i;
  75. snprintf(jack->name, sizeof(jack->name), "%s %s",
  76. card->shortname, jack->id);
  77. if (!jack->input_dev)
  78. return 0;
  79. jack->input_dev->name = jack->name;
  80. /* Default to the sound card device. */
  81. if (!jack->input_dev->dev.parent)
  82. jack->input_dev->dev.parent = snd_card_get_device_link(card);
  83. /* Add capabilities for any keys that are enabled */
  84. for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
  85. int testbit = SND_JACK_BTN_0 >> i;
  86. if (!(jack->type & testbit))
  87. continue;
  88. if (!jack->key[i])
  89. jack->key[i] = BTN_0 + i;
  90. input_set_capability(jack->input_dev, EV_KEY, jack->key[i]);
  91. }
  92. err = input_register_device(jack->input_dev);
  93. if (err == 0)
  94. jack->registered = 1;
  95. return err;
  96. }
  97. static void snd_jack_kctl_private_free(struct snd_kcontrol *kctl)
  98. {
  99. struct snd_jack_kctl *jack_kctl;
  100. jack_kctl = kctl->private_data;
  101. if (jack_kctl) {
  102. list_del(&jack_kctl->list);
  103. kfree(jack_kctl);
  104. }
  105. }
  106. static void snd_jack_kctl_add(struct snd_jack *jack, struct snd_jack_kctl *jack_kctl)
  107. {
  108. list_add_tail(&jack_kctl->list, &jack->kctl_list);
  109. }
  110. static struct snd_jack_kctl * snd_jack_kctl_new(struct snd_card *card, const char *name, unsigned int mask)
  111. {
  112. struct snd_kcontrol *kctl;
  113. struct snd_jack_kctl *jack_kctl;
  114. int err;
  115. kctl = snd_kctl_jack_new(name, card);
  116. if (!kctl)
  117. return NULL;
  118. err = snd_ctl_add(card, kctl);
  119. if (err < 0)
  120. return NULL;
  121. jack_kctl = kzalloc(sizeof(*jack_kctl), GFP_KERNEL);
  122. if (!jack_kctl)
  123. goto error;
  124. jack_kctl->kctl = kctl;
  125. jack_kctl->mask_bits = mask;
  126. kctl->private_data = jack_kctl;
  127. kctl->private_free = snd_jack_kctl_private_free;
  128. return jack_kctl;
  129. error:
  130. snd_ctl_free_one(kctl);
  131. return NULL;
  132. }
  133. /**
  134. * snd_jack_add_new_kctl - Create a new snd_jack_kctl and add it to jack
  135. * @jack: the jack instance which the kctl will attaching to
  136. * @name: the name for the snd_kcontrol object
  137. * @mask: a bitmask of enum snd_jack_type values that can be detected
  138. * by this snd_jack_kctl object.
  139. *
  140. * Creates a new snd_kcontrol object and adds it to the jack kctl_list.
  141. *
  142. * Return: Zero if successful, or a negative error code on failure.
  143. */
  144. int snd_jack_add_new_kctl(struct snd_jack *jack, const char * name, int mask)
  145. {
  146. struct snd_jack_kctl *jack_kctl;
  147. jack_kctl = snd_jack_kctl_new(jack->card, name, mask);
  148. if (!jack_kctl)
  149. return -ENOMEM;
  150. snd_jack_kctl_add(jack, jack_kctl);
  151. return 0;
  152. }
  153. EXPORT_SYMBOL(snd_jack_add_new_kctl);
  154. /**
  155. * snd_jack_new - Create a new jack
  156. * @card: the card instance
  157. * @id: an identifying string for this jack
  158. * @type: a bitmask of enum snd_jack_type values that can be detected by
  159. * this jack
  160. * @jjack: Used to provide the allocated jack object to the caller.
  161. * @initial_kctl: if true, create a kcontrol and add it to the jack list.
  162. * @phantom_jack: Don't create a input device for phantom jacks.
  163. *
  164. * Creates a new jack object.
  165. *
  166. * Return: Zero if successful, or a negative error code on failure.
  167. * On success @jjack will be initialised.
  168. */
  169. int snd_jack_new(struct snd_card *card, const char *id, int type,
  170. struct snd_jack **jjack, bool initial_kctl, bool phantom_jack)
  171. {
  172. struct snd_jack *jack;
  173. struct snd_jack_kctl *jack_kctl = NULL;
  174. int err;
  175. int i;
  176. static struct snd_device_ops ops = {
  177. .dev_free = snd_jack_dev_free,
  178. .dev_register = snd_jack_dev_register,
  179. .dev_disconnect = snd_jack_dev_disconnect,
  180. };
  181. if (initial_kctl) {
  182. jack_kctl = snd_jack_kctl_new(card, id, type);
  183. if (!jack_kctl)
  184. return -ENOMEM;
  185. }
  186. jack = kzalloc(sizeof(struct snd_jack), GFP_KERNEL);
  187. if (jack == NULL)
  188. return -ENOMEM;
  189. jack->id = kstrdup(id, GFP_KERNEL);
  190. /* don't creat input device for phantom jack */
  191. if (!phantom_jack) {
  192. jack->input_dev = input_allocate_device();
  193. if (jack->input_dev == NULL) {
  194. err = -ENOMEM;
  195. goto fail_input;
  196. }
  197. jack->input_dev->phys = "ALSA";
  198. jack->type = type;
  199. for (i = 0; i < SND_JACK_SWITCH_TYPES; i++)
  200. if (type & (1 << i))
  201. input_set_capability(jack->input_dev, EV_SW,
  202. jack_switch_types[i]);
  203. }
  204. err = snd_device_new(card, SNDRV_DEV_JACK, jack, &ops);
  205. if (err < 0)
  206. goto fail_input;
  207. jack->card = card;
  208. INIT_LIST_HEAD(&jack->kctl_list);
  209. if (initial_kctl)
  210. snd_jack_kctl_add(jack, jack_kctl);
  211. *jjack = jack;
  212. return 0;
  213. fail_input:
  214. input_free_device(jack->input_dev);
  215. kfree(jack->id);
  216. kfree(jack);
  217. return err;
  218. }
  219. EXPORT_SYMBOL(snd_jack_new);
  220. /**
  221. * snd_jack_set_parent - Set the parent device for a jack
  222. *
  223. * @jack: The jack to configure
  224. * @parent: The device to set as parent for the jack.
  225. *
  226. * Set the parent for the jack devices in the device tree. This
  227. * function is only valid prior to registration of the jack. If no
  228. * parent is configured then the parent device will be the sound card.
  229. */
  230. void snd_jack_set_parent(struct snd_jack *jack, struct device *parent)
  231. {
  232. WARN_ON(jack->registered);
  233. if (!jack->input_dev)
  234. return;
  235. jack->input_dev->dev.parent = parent;
  236. }
  237. EXPORT_SYMBOL(snd_jack_set_parent);
  238. /**
  239. * snd_jack_set_key - Set a key mapping on a jack
  240. *
  241. * @jack: The jack to configure
  242. * @type: Jack report type for this key
  243. * @keytype: Input layer key type to be reported
  244. *
  245. * Map a SND_JACK_BTN_ button type to an input layer key, allowing
  246. * reporting of keys on accessories via the jack abstraction. If no
  247. * mapping is provided but keys are enabled in the jack type then
  248. * BTN_n numeric buttons will be reported.
  249. *
  250. * If jacks are not reporting via the input API this call will have no
  251. * effect.
  252. *
  253. * Note that this is intended to be use by simple devices with small
  254. * numbers of keys that can be reported. It is also possible to
  255. * access the input device directly - devices with complex input
  256. * capabilities on accessories should consider doing this rather than
  257. * using this abstraction.
  258. *
  259. * This function may only be called prior to registration of the jack.
  260. *
  261. * Return: Zero if successful, or a negative error code on failure.
  262. */
  263. int snd_jack_set_key(struct snd_jack *jack, enum snd_jack_types type,
  264. int keytype)
  265. {
  266. int key = fls(SND_JACK_BTN_0) - fls(type);
  267. WARN_ON(jack->registered);
  268. if (!keytype || key >= ARRAY_SIZE(jack->key))
  269. return -EINVAL;
  270. jack->type |= type;
  271. jack->key[key] = keytype;
  272. return 0;
  273. }
  274. EXPORT_SYMBOL(snd_jack_set_key);
  275. /**
  276. * snd_jack_report - Report the current status of a jack
  277. *
  278. * @jack: The jack to report status for
  279. * @status: The current status of the jack
  280. */
  281. void snd_jack_report(struct snd_jack *jack, int status)
  282. {
  283. struct snd_jack_kctl *jack_kctl;
  284. int i;
  285. if (!jack)
  286. return;
  287. list_for_each_entry(jack_kctl, &jack->kctl_list, list)
  288. snd_kctl_jack_report(jack->card, jack_kctl->kctl,
  289. status & jack_kctl->mask_bits);
  290. if (!jack->input_dev)
  291. return;
  292. for (i = 0; i < ARRAY_SIZE(jack->key); i++) {
  293. int testbit = SND_JACK_BTN_0 >> i;
  294. if (jack->type & testbit)
  295. input_report_key(jack->input_dev, jack->key[i],
  296. status & testbit);
  297. }
  298. for (i = 0; i < ARRAY_SIZE(jack_switch_types); i++) {
  299. int testbit = 1 << i;
  300. if (jack->type & testbit)
  301. input_report_switch(jack->input_dev,
  302. jack_switch_types[i],
  303. status & testbit);
  304. }
  305. input_sync(jack->input_dev);
  306. }
  307. EXPORT_SYMBOL(snd_jack_report);