hwdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hardware dependent layer
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #include <linux/major.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/time.h>
  10. #include <linux/mutex.h>
  11. #include <linux/module.h>
  12. #include <linux/sched/signal.h>
  13. #include <sound/core.h>
  14. #include <sound/control.h>
  15. #include <sound/minors.h>
  16. #include <sound/hwdep.h>
  17. #include <sound/info.h>
  18. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  19. MODULE_DESCRIPTION("Hardware dependent layer");
  20. MODULE_LICENSE("GPL");
  21. static LIST_HEAD(snd_hwdep_devices);
  22. static DEFINE_MUTEX(register_mutex);
  23. static int snd_hwdep_dev_free(struct snd_device *device);
  24. static int snd_hwdep_dev_register(struct snd_device *device);
  25. static int snd_hwdep_dev_disconnect(struct snd_device *device);
  26. static struct snd_hwdep *snd_hwdep_search(struct snd_card *card, int device)
  27. {
  28. struct snd_hwdep *hwdep;
  29. list_for_each_entry(hwdep, &snd_hwdep_devices, list)
  30. if (hwdep->card == card && hwdep->device == device)
  31. return hwdep;
  32. return NULL;
  33. }
  34. static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
  35. {
  36. struct snd_hwdep *hw = file->private_data;
  37. if (hw->ops.llseek)
  38. return hw->ops.llseek(hw, file, offset, orig);
  39. return -ENXIO;
  40. }
  41. static ssize_t snd_hwdep_read(struct file * file, char __user *buf,
  42. size_t count, loff_t *offset)
  43. {
  44. struct snd_hwdep *hw = file->private_data;
  45. if (hw->ops.read)
  46. return hw->ops.read(hw, buf, count, offset);
  47. return -ENXIO;
  48. }
  49. static ssize_t snd_hwdep_write(struct file * file, const char __user *buf,
  50. size_t count, loff_t *offset)
  51. {
  52. struct snd_hwdep *hw = file->private_data;
  53. if (hw->ops.write)
  54. return hw->ops.write(hw, buf, count, offset);
  55. return -ENXIO;
  56. }
  57. static int snd_hwdep_open(struct inode *inode, struct file * file)
  58. {
  59. int major = imajor(inode);
  60. struct snd_hwdep *hw;
  61. int err;
  62. wait_queue_entry_t wait;
  63. if (major == snd_major) {
  64. hw = snd_lookup_minor_data(iminor(inode),
  65. SNDRV_DEVICE_TYPE_HWDEP);
  66. #ifdef CONFIG_SND_OSSEMUL
  67. } else if (major == SOUND_MAJOR) {
  68. hw = snd_lookup_oss_minor_data(iminor(inode),
  69. SNDRV_OSS_DEVICE_TYPE_DMFM);
  70. #endif
  71. } else
  72. return -ENXIO;
  73. if (hw == NULL)
  74. return -ENODEV;
  75. if (!try_module_get(hw->card->module)) {
  76. snd_card_unref(hw->card);
  77. return -EFAULT;
  78. }
  79. init_waitqueue_entry(&wait, current);
  80. add_wait_queue(&hw->open_wait, &wait);
  81. mutex_lock(&hw->open_mutex);
  82. while (1) {
  83. if (hw->exclusive && hw->used > 0) {
  84. err = -EBUSY;
  85. break;
  86. }
  87. if (!hw->ops.open) {
  88. err = 0;
  89. break;
  90. }
  91. err = hw->ops.open(hw, file);
  92. if (err >= 0)
  93. break;
  94. if (err == -EAGAIN) {
  95. if (file->f_flags & O_NONBLOCK) {
  96. err = -EBUSY;
  97. break;
  98. }
  99. } else
  100. break;
  101. set_current_state(TASK_INTERRUPTIBLE);
  102. mutex_unlock(&hw->open_mutex);
  103. schedule();
  104. mutex_lock(&hw->open_mutex);
  105. if (hw->card->shutdown) {
  106. err = -ENODEV;
  107. break;
  108. }
  109. if (signal_pending(current)) {
  110. err = -ERESTARTSYS;
  111. break;
  112. }
  113. }
  114. remove_wait_queue(&hw->open_wait, &wait);
  115. if (err >= 0) {
  116. err = snd_card_file_add(hw->card, file);
  117. if (err >= 0) {
  118. file->private_data = hw;
  119. hw->used++;
  120. } else {
  121. if (hw->ops.release)
  122. hw->ops.release(hw, file);
  123. }
  124. }
  125. mutex_unlock(&hw->open_mutex);
  126. if (err < 0)
  127. module_put(hw->card->module);
  128. snd_card_unref(hw->card);
  129. return err;
  130. }
  131. static int snd_hwdep_release(struct inode *inode, struct file * file)
  132. {
  133. int err = 0;
  134. struct snd_hwdep *hw = file->private_data;
  135. struct module *mod = hw->card->module;
  136. mutex_lock(&hw->open_mutex);
  137. if (hw->ops.release)
  138. err = hw->ops.release(hw, file);
  139. if (hw->used > 0)
  140. hw->used--;
  141. mutex_unlock(&hw->open_mutex);
  142. wake_up(&hw->open_wait);
  143. snd_card_file_remove(hw->card, file);
  144. module_put(mod);
  145. return err;
  146. }
  147. static __poll_t snd_hwdep_poll(struct file * file, poll_table * wait)
  148. {
  149. struct snd_hwdep *hw = file->private_data;
  150. if (hw->ops.poll)
  151. return hw->ops.poll(hw, file, wait);
  152. return 0;
  153. }
  154. static int snd_hwdep_info(struct snd_hwdep *hw,
  155. struct snd_hwdep_info __user *_info)
  156. {
  157. struct snd_hwdep_info info;
  158. memset(&info, 0, sizeof(info));
  159. info.card = hw->card->number;
  160. strlcpy(info.id, hw->id, sizeof(info.id));
  161. strlcpy(info.name, hw->name, sizeof(info.name));
  162. info.iface = hw->iface;
  163. if (copy_to_user(_info, &info, sizeof(info)))
  164. return -EFAULT;
  165. return 0;
  166. }
  167. static int snd_hwdep_dsp_status(struct snd_hwdep *hw,
  168. struct snd_hwdep_dsp_status __user *_info)
  169. {
  170. struct snd_hwdep_dsp_status info;
  171. int err;
  172. if (! hw->ops.dsp_status)
  173. return -ENXIO;
  174. memset(&info, 0, sizeof(info));
  175. info.dsp_loaded = hw->dsp_loaded;
  176. if ((err = hw->ops.dsp_status(hw, &info)) < 0)
  177. return err;
  178. if (copy_to_user(_info, &info, sizeof(info)))
  179. return -EFAULT;
  180. return 0;
  181. }
  182. static int snd_hwdep_dsp_load(struct snd_hwdep *hw,
  183. struct snd_hwdep_dsp_image *info)
  184. {
  185. int err;
  186. if (! hw->ops.dsp_load)
  187. return -ENXIO;
  188. if (info->index >= 32)
  189. return -EINVAL;
  190. /* check whether the dsp was already loaded */
  191. if (hw->dsp_loaded & (1u << info->index))
  192. return -EBUSY;
  193. err = hw->ops.dsp_load(hw, info);
  194. if (err < 0)
  195. return err;
  196. hw->dsp_loaded |= (1u << info->index);
  197. return 0;
  198. }
  199. static int snd_hwdep_dsp_load_user(struct snd_hwdep *hw,
  200. struct snd_hwdep_dsp_image __user *_info)
  201. {
  202. struct snd_hwdep_dsp_image info = {};
  203. if (copy_from_user(&info, _info, sizeof(info)))
  204. return -EFAULT;
  205. return snd_hwdep_dsp_load(hw, &info);
  206. }
  207. static long snd_hwdep_ioctl(struct file * file, unsigned int cmd,
  208. unsigned long arg)
  209. {
  210. struct snd_hwdep *hw = file->private_data;
  211. void __user *argp = (void __user *)arg;
  212. switch (cmd) {
  213. case SNDRV_HWDEP_IOCTL_PVERSION:
  214. return put_user(SNDRV_HWDEP_VERSION, (int __user *)argp);
  215. case SNDRV_HWDEP_IOCTL_INFO:
  216. return snd_hwdep_info(hw, argp);
  217. case SNDRV_HWDEP_IOCTL_DSP_STATUS:
  218. return snd_hwdep_dsp_status(hw, argp);
  219. case SNDRV_HWDEP_IOCTL_DSP_LOAD:
  220. return snd_hwdep_dsp_load_user(hw, argp);
  221. }
  222. if (hw->ops.ioctl)
  223. return hw->ops.ioctl(hw, file, cmd, arg);
  224. return -ENOTTY;
  225. }
  226. static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
  227. {
  228. struct snd_hwdep *hw = file->private_data;
  229. if (hw->ops.mmap)
  230. return hw->ops.mmap(hw, file, vma);
  231. return -ENXIO;
  232. }
  233. static int snd_hwdep_control_ioctl(struct snd_card *card,
  234. struct snd_ctl_file * control,
  235. unsigned int cmd, unsigned long arg)
  236. {
  237. switch (cmd) {
  238. case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
  239. {
  240. int device;
  241. if (get_user(device, (int __user *)arg))
  242. return -EFAULT;
  243. mutex_lock(&register_mutex);
  244. if (device < 0)
  245. device = 0;
  246. else if (device < SNDRV_MINOR_HWDEPS)
  247. device++;
  248. else
  249. device = SNDRV_MINOR_HWDEPS;
  250. while (device < SNDRV_MINOR_HWDEPS) {
  251. if (snd_hwdep_search(card, device))
  252. break;
  253. device++;
  254. }
  255. if (device >= SNDRV_MINOR_HWDEPS)
  256. device = -1;
  257. mutex_unlock(&register_mutex);
  258. if (put_user(device, (int __user *)arg))
  259. return -EFAULT;
  260. return 0;
  261. }
  262. case SNDRV_CTL_IOCTL_HWDEP_INFO:
  263. {
  264. struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
  265. int device, err;
  266. struct snd_hwdep *hwdep;
  267. if (get_user(device, &info->device))
  268. return -EFAULT;
  269. mutex_lock(&register_mutex);
  270. hwdep = snd_hwdep_search(card, device);
  271. if (hwdep)
  272. err = snd_hwdep_info(hwdep, info);
  273. else
  274. err = -ENXIO;
  275. mutex_unlock(&register_mutex);
  276. return err;
  277. }
  278. }
  279. return -ENOIOCTLCMD;
  280. }
  281. #ifdef CONFIG_COMPAT
  282. #include "hwdep_compat.c"
  283. #else
  284. #define snd_hwdep_ioctl_compat NULL
  285. #endif
  286. /*
  287. */
  288. static const struct file_operations snd_hwdep_f_ops =
  289. {
  290. .owner = THIS_MODULE,
  291. .llseek = snd_hwdep_llseek,
  292. .read = snd_hwdep_read,
  293. .write = snd_hwdep_write,
  294. .open = snd_hwdep_open,
  295. .release = snd_hwdep_release,
  296. .poll = snd_hwdep_poll,
  297. .unlocked_ioctl = snd_hwdep_ioctl,
  298. .compat_ioctl = snd_hwdep_ioctl_compat,
  299. .mmap = snd_hwdep_mmap,
  300. };
  301. static void release_hwdep_device(struct device *dev)
  302. {
  303. kfree(container_of(dev, struct snd_hwdep, dev));
  304. }
  305. /**
  306. * snd_hwdep_new - create a new hwdep instance
  307. * @card: the card instance
  308. * @id: the id string
  309. * @device: the device index (zero-based)
  310. * @rhwdep: the pointer to store the new hwdep instance
  311. *
  312. * Creates a new hwdep instance with the given index on the card.
  313. * The callbacks (hwdep->ops) must be set on the returned instance
  314. * after this call manually by the caller.
  315. *
  316. * Return: Zero if successful, or a negative error code on failure.
  317. */
  318. int snd_hwdep_new(struct snd_card *card, char *id, int device,
  319. struct snd_hwdep **rhwdep)
  320. {
  321. struct snd_hwdep *hwdep;
  322. int err;
  323. static const struct snd_device_ops ops = {
  324. .dev_free = snd_hwdep_dev_free,
  325. .dev_register = snd_hwdep_dev_register,
  326. .dev_disconnect = snd_hwdep_dev_disconnect,
  327. };
  328. if (snd_BUG_ON(!card))
  329. return -ENXIO;
  330. if (rhwdep)
  331. *rhwdep = NULL;
  332. hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
  333. if (!hwdep)
  334. return -ENOMEM;
  335. init_waitqueue_head(&hwdep->open_wait);
  336. mutex_init(&hwdep->open_mutex);
  337. hwdep->card = card;
  338. hwdep->device = device;
  339. if (id)
  340. strlcpy(hwdep->id, id, sizeof(hwdep->id));
  341. snd_device_initialize(&hwdep->dev, card);
  342. hwdep->dev.release = release_hwdep_device;
  343. dev_set_name(&hwdep->dev, "hwC%iD%i", card->number, device);
  344. #ifdef CONFIG_SND_OSSEMUL
  345. hwdep->oss_type = -1;
  346. #endif
  347. err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops);
  348. if (err < 0) {
  349. put_device(&hwdep->dev);
  350. return err;
  351. }
  352. if (rhwdep)
  353. *rhwdep = hwdep;
  354. return 0;
  355. }
  356. EXPORT_SYMBOL(snd_hwdep_new);
  357. static int snd_hwdep_dev_free(struct snd_device *device)
  358. {
  359. struct snd_hwdep *hwdep = device->device_data;
  360. if (!hwdep)
  361. return 0;
  362. if (hwdep->private_free)
  363. hwdep->private_free(hwdep);
  364. put_device(&hwdep->dev);
  365. return 0;
  366. }
  367. static int snd_hwdep_dev_register(struct snd_device *device)
  368. {
  369. struct snd_hwdep *hwdep = device->device_data;
  370. struct snd_card *card = hwdep->card;
  371. int err;
  372. mutex_lock(&register_mutex);
  373. if (snd_hwdep_search(card, hwdep->device)) {
  374. mutex_unlock(&register_mutex);
  375. return -EBUSY;
  376. }
  377. list_add_tail(&hwdep->list, &snd_hwdep_devices);
  378. err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
  379. hwdep->card, hwdep->device,
  380. &snd_hwdep_f_ops, hwdep, &hwdep->dev);
  381. if (err < 0) {
  382. dev_err(&hwdep->dev, "unable to register\n");
  383. list_del(&hwdep->list);
  384. mutex_unlock(&register_mutex);
  385. return err;
  386. }
  387. #ifdef CONFIG_SND_OSSEMUL
  388. hwdep->ossreg = 0;
  389. if (hwdep->oss_type >= 0) {
  390. if (hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM &&
  391. hwdep->device)
  392. dev_warn(&hwdep->dev,
  393. "only hwdep device 0 can be registered as OSS direct FM device!\n");
  394. else if (snd_register_oss_device(hwdep->oss_type,
  395. card, hwdep->device,
  396. &snd_hwdep_f_ops, hwdep) < 0)
  397. dev_warn(&hwdep->dev,
  398. "unable to register OSS compatibility device\n");
  399. else
  400. hwdep->ossreg = 1;
  401. }
  402. #endif
  403. mutex_unlock(&register_mutex);
  404. return 0;
  405. }
  406. static int snd_hwdep_dev_disconnect(struct snd_device *device)
  407. {
  408. struct snd_hwdep *hwdep = device->device_data;
  409. if (snd_BUG_ON(!hwdep))
  410. return -ENXIO;
  411. mutex_lock(&register_mutex);
  412. if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep) {
  413. mutex_unlock(&register_mutex);
  414. return -EINVAL;
  415. }
  416. mutex_lock(&hwdep->open_mutex);
  417. wake_up(&hwdep->open_wait);
  418. #ifdef CONFIG_SND_OSSEMUL
  419. if (hwdep->ossreg)
  420. snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
  421. #endif
  422. snd_unregister_device(&hwdep->dev);
  423. list_del_init(&hwdep->list);
  424. mutex_unlock(&hwdep->open_mutex);
  425. mutex_unlock(&register_mutex);
  426. return 0;
  427. }
  428. #ifdef CONFIG_SND_PROC_FS
  429. /*
  430. * Info interface
  431. */
  432. static void snd_hwdep_proc_read(struct snd_info_entry *entry,
  433. struct snd_info_buffer *buffer)
  434. {
  435. struct snd_hwdep *hwdep;
  436. mutex_lock(&register_mutex);
  437. list_for_each_entry(hwdep, &snd_hwdep_devices, list)
  438. snd_iprintf(buffer, "%02i-%02i: %s\n",
  439. hwdep->card->number, hwdep->device, hwdep->name);
  440. mutex_unlock(&register_mutex);
  441. }
  442. static struct snd_info_entry *snd_hwdep_proc_entry;
  443. static void __init snd_hwdep_proc_init(void)
  444. {
  445. struct snd_info_entry *entry;
  446. if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
  447. entry->c.text.read = snd_hwdep_proc_read;
  448. if (snd_info_register(entry) < 0) {
  449. snd_info_free_entry(entry);
  450. entry = NULL;
  451. }
  452. }
  453. snd_hwdep_proc_entry = entry;
  454. }
  455. static void __exit snd_hwdep_proc_done(void)
  456. {
  457. snd_info_free_entry(snd_hwdep_proc_entry);
  458. }
  459. #else /* !CONFIG_SND_PROC_FS */
  460. #define snd_hwdep_proc_init()
  461. #define snd_hwdep_proc_done()
  462. #endif /* CONFIG_SND_PROC_FS */
  463. /*
  464. * ENTRY functions
  465. */
  466. static int __init alsa_hwdep_init(void)
  467. {
  468. snd_hwdep_proc_init();
  469. snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
  470. snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl);
  471. return 0;
  472. }
  473. static void __exit alsa_hwdep_exit(void)
  474. {
  475. snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
  476. snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl);
  477. snd_hwdep_proc_done();
  478. }
  479. module_init(alsa_hwdep_init)
  480. module_exit(alsa_hwdep_exit)