hwdep.c 13 KB

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