hwdep.c 13 KB

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