hwdep.c 13 KB

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