hwdep.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. scoped_guard(mutex, &hw->open_mutex) {
  137. if (hw->ops.release)
  138. err = hw->ops.release(hw, file);
  139. if (hw->used > 0)
  140. hw->used--;
  141. }
  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. strscpy(info.id, hw->id, sizeof(info.id));
  161. strscpy(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. err = hw->ops.dsp_status(hw, &info);
  177. if (err < 0)
  178. return err;
  179. if (copy_to_user(_info, &info, sizeof(info)))
  180. return -EFAULT;
  181. return 0;
  182. }
  183. static int snd_hwdep_dsp_load(struct snd_hwdep *hw,
  184. struct snd_hwdep_dsp_image *info)
  185. {
  186. int err;
  187. if (! hw->ops.dsp_load)
  188. return -ENXIO;
  189. if (info->index >= 32)
  190. return -EINVAL;
  191. /* check whether the dsp was already loaded */
  192. if (hw->dsp_loaded & (1u << info->index))
  193. return -EBUSY;
  194. err = hw->ops.dsp_load(hw, info);
  195. if (err < 0)
  196. return err;
  197. hw->dsp_loaded |= (1u << info->index);
  198. return 0;
  199. }
  200. static int snd_hwdep_dsp_load_user(struct snd_hwdep *hw,
  201. struct snd_hwdep_dsp_image __user *_info)
  202. {
  203. struct snd_hwdep_dsp_image info = {};
  204. if (copy_from_user(&info, _info, sizeof(info)))
  205. return -EFAULT;
  206. return snd_hwdep_dsp_load(hw, &info);
  207. }
  208. static long snd_hwdep_ioctl(struct file * file, unsigned int cmd,
  209. unsigned long arg)
  210. {
  211. struct snd_hwdep *hw = file->private_data;
  212. void __user *argp = (void __user *)arg;
  213. switch (cmd) {
  214. case SNDRV_HWDEP_IOCTL_PVERSION:
  215. return put_user(SNDRV_HWDEP_VERSION, (int __user *)argp);
  216. case SNDRV_HWDEP_IOCTL_INFO:
  217. return snd_hwdep_info(hw, argp);
  218. case SNDRV_HWDEP_IOCTL_DSP_STATUS:
  219. return snd_hwdep_dsp_status(hw, argp);
  220. case SNDRV_HWDEP_IOCTL_DSP_LOAD:
  221. return snd_hwdep_dsp_load_user(hw, argp);
  222. }
  223. if (hw->ops.ioctl)
  224. return hw->ops.ioctl(hw, file, cmd, arg);
  225. return -ENOTTY;
  226. }
  227. static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
  228. {
  229. struct snd_hwdep *hw = file->private_data;
  230. if (hw->ops.mmap)
  231. return hw->ops.mmap(hw, file, vma);
  232. return -ENXIO;
  233. }
  234. static int snd_hwdep_control_ioctl(struct snd_card *card,
  235. struct snd_ctl_file * control,
  236. unsigned int cmd, unsigned long arg)
  237. {
  238. switch (cmd) {
  239. case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
  240. {
  241. int device;
  242. if (get_user(device, (int __user *)arg))
  243. return -EFAULT;
  244. scoped_guard(mutex, &register_mutex) {
  245. if (device < 0)
  246. device = 0;
  247. else if (device < SNDRV_MINOR_HWDEPS)
  248. device++;
  249. else
  250. device = SNDRV_MINOR_HWDEPS;
  251. while (device < SNDRV_MINOR_HWDEPS) {
  252. if (snd_hwdep_search(card, device))
  253. break;
  254. device++;
  255. }
  256. if (device >= SNDRV_MINOR_HWDEPS)
  257. device = -1;
  258. }
  259. if (put_user(device, (int __user *)arg))
  260. return -EFAULT;
  261. return 0;
  262. }
  263. case SNDRV_CTL_IOCTL_HWDEP_INFO:
  264. {
  265. struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
  266. int device;
  267. struct snd_hwdep *hwdep;
  268. if (get_user(device, &info->device))
  269. return -EFAULT;
  270. scoped_guard(mutex, &register_mutex) {
  271. hwdep = snd_hwdep_search(card, device);
  272. if (!hwdep)
  273. return -ENXIO;
  274. return snd_hwdep_info(hwdep, info);
  275. }
  276. break;
  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 snd_hwdep_free(struct snd_hwdep *hwdep)
  302. {
  303. if (!hwdep)
  304. return;
  305. if (hwdep->private_free)
  306. hwdep->private_free(hwdep);
  307. put_device(hwdep->dev);
  308. kfree(hwdep);
  309. }
  310. /**
  311. * snd_hwdep_new - create a new hwdep instance
  312. * @card: the card instance
  313. * @id: the id string
  314. * @device: the device index (zero-based)
  315. * @rhwdep: the pointer to store the new hwdep instance
  316. *
  317. * Creates a new hwdep instance with the given index on the card.
  318. * The callbacks (hwdep->ops) must be set on the returned instance
  319. * after this call manually by the caller.
  320. *
  321. * Return: Zero if successful, or a negative error code on failure.
  322. */
  323. int snd_hwdep_new(struct snd_card *card, char *id, int device,
  324. struct snd_hwdep **rhwdep)
  325. {
  326. struct snd_hwdep *hwdep;
  327. int err;
  328. static const struct snd_device_ops ops = {
  329. .dev_free = snd_hwdep_dev_free,
  330. .dev_register = snd_hwdep_dev_register,
  331. .dev_disconnect = snd_hwdep_dev_disconnect,
  332. };
  333. if (snd_BUG_ON(!card))
  334. return -ENXIO;
  335. if (rhwdep)
  336. *rhwdep = NULL;
  337. hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
  338. if (!hwdep)
  339. return -ENOMEM;
  340. init_waitqueue_head(&hwdep->open_wait);
  341. mutex_init(&hwdep->open_mutex);
  342. hwdep->card = card;
  343. hwdep->device = device;
  344. if (id)
  345. strscpy(hwdep->id, id, sizeof(hwdep->id));
  346. err = snd_device_alloc(&hwdep->dev, card);
  347. if (err < 0) {
  348. snd_hwdep_free(hwdep);
  349. return err;
  350. }
  351. dev_set_name(hwdep->dev, "hwC%iD%i", card->number, device);
  352. #ifdef CONFIG_SND_OSSEMUL
  353. hwdep->oss_type = -1;
  354. #endif
  355. err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops);
  356. if (err < 0) {
  357. snd_hwdep_free(hwdep);
  358. return err;
  359. }
  360. if (rhwdep)
  361. *rhwdep = hwdep;
  362. return 0;
  363. }
  364. EXPORT_SYMBOL(snd_hwdep_new);
  365. static int snd_hwdep_dev_free(struct snd_device *device)
  366. {
  367. snd_hwdep_free(device->device_data);
  368. return 0;
  369. }
  370. static int snd_hwdep_dev_register(struct snd_device *device)
  371. {
  372. struct snd_hwdep *hwdep = device->device_data;
  373. struct snd_card *card = hwdep->card;
  374. int err;
  375. guard(mutex)(&register_mutex);
  376. if (snd_hwdep_search(card, hwdep->device))
  377. return -EBUSY;
  378. list_add_tail(&hwdep->list, &snd_hwdep_devices);
  379. err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
  380. hwdep->card, hwdep->device,
  381. &snd_hwdep_f_ops, hwdep, hwdep->dev);
  382. if (err < 0) {
  383. dev_err(hwdep->dev, "unable to register\n");
  384. list_del(&hwdep->list);
  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. return 0;
  404. }
  405. static int snd_hwdep_dev_disconnect(struct snd_device *device)
  406. {
  407. struct snd_hwdep *hwdep = device->device_data;
  408. if (snd_BUG_ON(!hwdep))
  409. return -ENXIO;
  410. guard(mutex)(&register_mutex);
  411. if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep)
  412. return -EINVAL;
  413. guard(mutex)(&hwdep->open_mutex);
  414. wake_up(&hwdep->open_wait);
  415. #ifdef CONFIG_SND_OSSEMUL
  416. if (hwdep->ossreg)
  417. snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
  418. #endif
  419. snd_unregister_device(hwdep->dev);
  420. list_del_init(&hwdep->list);
  421. return 0;
  422. }
  423. #ifdef CONFIG_SND_PROC_FS
  424. /*
  425. * Info interface
  426. */
  427. static void snd_hwdep_proc_read(struct snd_info_entry *entry,
  428. struct snd_info_buffer *buffer)
  429. {
  430. struct snd_hwdep *hwdep;
  431. guard(mutex)(&register_mutex);
  432. list_for_each_entry(hwdep, &snd_hwdep_devices, list)
  433. snd_iprintf(buffer, "%02i-%02i: %s\n",
  434. hwdep->card->number, hwdep->device, hwdep->name);
  435. }
  436. static struct snd_info_entry *snd_hwdep_proc_entry;
  437. static void __init snd_hwdep_proc_init(void)
  438. {
  439. struct snd_info_entry *entry;
  440. entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL);
  441. if (entry) {
  442. entry->c.text.read = snd_hwdep_proc_read;
  443. if (snd_info_register(entry) < 0) {
  444. snd_info_free_entry(entry);
  445. entry = NULL;
  446. }
  447. }
  448. snd_hwdep_proc_entry = entry;
  449. }
  450. static void __exit snd_hwdep_proc_done(void)
  451. {
  452. snd_info_free_entry(snd_hwdep_proc_entry);
  453. }
  454. #else /* !CONFIG_SND_PROC_FS */
  455. #define snd_hwdep_proc_init()
  456. #define snd_hwdep_proc_done()
  457. #endif /* CONFIG_SND_PROC_FS */
  458. /*
  459. * ENTRY functions
  460. */
  461. static int __init alsa_hwdep_init(void)
  462. {
  463. snd_hwdep_proc_init();
  464. snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
  465. snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl);
  466. return 0;
  467. }
  468. static void __exit alsa_hwdep_exit(void)
  469. {
  470. snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
  471. snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl);
  472. snd_hwdep_proc_done();
  473. }
  474. module_init(alsa_hwdep_init)
  475. module_exit(alsa_hwdep_exit)