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_disconnect(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. struct module *mod = hw->card->module;
  146. mutex_lock(&hw->open_mutex);
  147. if (hw->ops.release) {
  148. err = hw->ops.release(hw, file);
  149. wake_up(&hw->open_wait);
  150. }
  151. if (hw->used > 0)
  152. hw->used--;
  153. snd_card_file_remove(hw->card, file);
  154. mutex_unlock(&hw->open_mutex);
  155. module_put(mod);
  156. return err;
  157. }
  158. static unsigned int snd_hwdep_poll(struct file * file, poll_table * wait)
  159. {
  160. struct snd_hwdep *hw = file->private_data;
  161. if (hw->ops.poll)
  162. return hw->ops.poll(hw, file, wait);
  163. return 0;
  164. }
  165. static int snd_hwdep_info(struct snd_hwdep *hw,
  166. struct snd_hwdep_info __user *_info)
  167. {
  168. struct snd_hwdep_info info;
  169. memset(&info, 0, sizeof(info));
  170. info.card = hw->card->number;
  171. strlcpy(info.id, hw->id, sizeof(info.id));
  172. strlcpy(info.name, hw->name, sizeof(info.name));
  173. info.iface = hw->iface;
  174. if (copy_to_user(_info, &info, sizeof(info)))
  175. return -EFAULT;
  176. return 0;
  177. }
  178. static int snd_hwdep_dsp_status(struct snd_hwdep *hw,
  179. struct snd_hwdep_dsp_status __user *_info)
  180. {
  181. struct snd_hwdep_dsp_status info;
  182. int err;
  183. if (! hw->ops.dsp_status)
  184. return -ENXIO;
  185. memset(&info, 0, sizeof(info));
  186. info.dsp_loaded = hw->dsp_loaded;
  187. if ((err = hw->ops.dsp_status(hw, &info)) < 0)
  188. return err;
  189. if (copy_to_user(_info, &info, sizeof(info)))
  190. return -EFAULT;
  191. return 0;
  192. }
  193. static int snd_hwdep_dsp_load(struct snd_hwdep *hw,
  194. struct snd_hwdep_dsp_image __user *_info)
  195. {
  196. struct snd_hwdep_dsp_image info;
  197. int err;
  198. if (! hw->ops.dsp_load)
  199. return -ENXIO;
  200. memset(&info, 0, sizeof(info));
  201. if (copy_from_user(&info, _info, sizeof(info)))
  202. return -EFAULT;
  203. /* check whether the dsp was already loaded */
  204. if (hw->dsp_loaded & (1 << info.index))
  205. return -EBUSY;
  206. if (!access_ok(VERIFY_READ, info.image, info.length))
  207. return -EFAULT;
  208. err = hw->ops.dsp_load(hw, &info);
  209. if (err < 0)
  210. return err;
  211. hw->dsp_loaded |= (1 << info.index);
  212. return 0;
  213. }
  214. static long snd_hwdep_ioctl(struct file * file, unsigned int cmd,
  215. unsigned long arg)
  216. {
  217. struct snd_hwdep *hw = file->private_data;
  218. void __user *argp = (void __user *)arg;
  219. switch (cmd) {
  220. case SNDRV_HWDEP_IOCTL_PVERSION:
  221. return put_user(SNDRV_HWDEP_VERSION, (int __user *)argp);
  222. case SNDRV_HWDEP_IOCTL_INFO:
  223. return snd_hwdep_info(hw, argp);
  224. case SNDRV_HWDEP_IOCTL_DSP_STATUS:
  225. return snd_hwdep_dsp_status(hw, argp);
  226. case SNDRV_HWDEP_IOCTL_DSP_LOAD:
  227. return snd_hwdep_dsp_load(hw, argp);
  228. }
  229. if (hw->ops.ioctl)
  230. return hw->ops.ioctl(hw, file, cmd, arg);
  231. return -ENOTTY;
  232. }
  233. static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
  234. {
  235. struct snd_hwdep *hw = file->private_data;
  236. if (hw->ops.mmap)
  237. return hw->ops.mmap(hw, file, vma);
  238. return -ENXIO;
  239. }
  240. static int snd_hwdep_control_ioctl(struct snd_card *card,
  241. struct snd_ctl_file * control,
  242. unsigned int cmd, unsigned long arg)
  243. {
  244. switch (cmd) {
  245. case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
  246. {
  247. int device;
  248. if (get_user(device, (int __user *)arg))
  249. return -EFAULT;
  250. mutex_lock(&register_mutex);
  251. device = device < 0 ? 0 : device + 1;
  252. while (device < SNDRV_MINOR_HWDEPS) {
  253. if (snd_hwdep_search(card, device))
  254. break;
  255. device++;
  256. }
  257. if (device >= SNDRV_MINOR_HWDEPS)
  258. device = -1;
  259. mutex_unlock(&register_mutex);
  260. if (put_user(device, (int __user *)arg))
  261. return -EFAULT;
  262. return 0;
  263. }
  264. case SNDRV_CTL_IOCTL_HWDEP_INFO:
  265. {
  266. struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
  267. int device, err;
  268. struct snd_hwdep *hwdep;
  269. if (get_user(device, &info->device))
  270. return -EFAULT;
  271. mutex_lock(&register_mutex);
  272. hwdep = snd_hwdep_search(card, device);
  273. if (hwdep)
  274. err = snd_hwdep_info(hwdep, info);
  275. else
  276. err = -ENXIO;
  277. mutex_unlock(&register_mutex);
  278. return err;
  279. }
  280. }
  281. return -ENOIOCTLCMD;
  282. }
  283. #ifdef CONFIG_COMPAT
  284. #include "hwdep_compat.c"
  285. #else
  286. #define snd_hwdep_ioctl_compat NULL
  287. #endif
  288. /*
  289. */
  290. static struct file_operations snd_hwdep_f_ops =
  291. {
  292. .owner = THIS_MODULE,
  293. .llseek = snd_hwdep_llseek,
  294. .read = snd_hwdep_read,
  295. .write = snd_hwdep_write,
  296. .open = snd_hwdep_open,
  297. .release = snd_hwdep_release,
  298. .poll = snd_hwdep_poll,
  299. .unlocked_ioctl = snd_hwdep_ioctl,
  300. .compat_ioctl = snd_hwdep_ioctl_compat,
  301. .mmap = snd_hwdep_mmap,
  302. };
  303. /**
  304. * snd_hwdep_new - create a new hwdep instance
  305. * @card: the card instance
  306. * @id: the id string
  307. * @device: the device index (zero-based)
  308. * @rhwdep: the pointer to store the new hwdep instance
  309. *
  310. * Creates a new hwdep instance with the given index on the card.
  311. * The callbacks (hwdep->ops) must be set on the returned instance
  312. * after this call manually by the caller.
  313. *
  314. * Returns zero if successful, or a negative error code on failure.
  315. */
  316. int snd_hwdep_new(struct snd_card *card, char *id, int device,
  317. struct snd_hwdep **rhwdep)
  318. {
  319. struct snd_hwdep *hwdep;
  320. int err;
  321. static struct snd_device_ops ops = {
  322. .dev_free = snd_hwdep_dev_free,
  323. .dev_register = snd_hwdep_dev_register,
  324. .dev_disconnect = snd_hwdep_dev_disconnect,
  325. };
  326. snd_assert(rhwdep != NULL, return -EINVAL);
  327. *rhwdep = NULL;
  328. snd_assert(card != NULL, return -ENXIO);
  329. hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
  330. if (hwdep == NULL) {
  331. snd_printk(KERN_ERR "hwdep: cannot allocate\n");
  332. return -ENOMEM;
  333. }
  334. hwdep->card = card;
  335. hwdep->device = device;
  336. if (id)
  337. strlcpy(hwdep->id, id, sizeof(hwdep->id));
  338. #ifdef CONFIG_SND_OSSEMUL
  339. hwdep->oss_type = -1;
  340. #endif
  341. if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
  342. snd_hwdep_free(hwdep);
  343. return err;
  344. }
  345. init_waitqueue_head(&hwdep->open_wait);
  346. mutex_init(&hwdep->open_mutex);
  347. *rhwdep = hwdep;
  348. return 0;
  349. }
  350. static int snd_hwdep_free(struct snd_hwdep *hwdep)
  351. {
  352. snd_assert(hwdep != NULL, return -ENXIO);
  353. if (hwdep->private_free)
  354. hwdep->private_free(hwdep);
  355. kfree(hwdep);
  356. return 0;
  357. }
  358. static int snd_hwdep_dev_free(struct snd_device *device)
  359. {
  360. struct snd_hwdep *hwdep = device->device_data;
  361. return snd_hwdep_free(hwdep);
  362. }
  363. static int snd_hwdep_dev_register(struct snd_device *device)
  364. {
  365. struct snd_hwdep *hwdep = device->device_data;
  366. int err;
  367. char name[32];
  368. mutex_lock(&register_mutex);
  369. if (snd_hwdep_search(hwdep->card, hwdep->device)) {
  370. mutex_unlock(&register_mutex);
  371. return -EBUSY;
  372. }
  373. list_add_tail(&hwdep->list, &snd_hwdep_devices);
  374. sprintf(name, "hwC%iD%i", hwdep->card->number, hwdep->device);
  375. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
  376. hwdep->card, hwdep->device,
  377. &snd_hwdep_f_ops, hwdep, name)) < 0) {
  378. snd_printk(KERN_ERR "unable to register hardware dependent device %i:%i\n",
  379. hwdep->card->number, hwdep->device);
  380. list_del(&hwdep->list);
  381. mutex_unlock(&register_mutex);
  382. return err;
  383. }
  384. #ifdef CONFIG_SND_OSSEMUL
  385. hwdep->ossreg = 0;
  386. if (hwdep->oss_type >= 0) {
  387. if ((hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM) && (hwdep->device != 0)) {
  388. snd_printk (KERN_WARNING "only hwdep device 0 can be registered as OSS direct FM device!\n");
  389. } else {
  390. if (snd_register_oss_device(hwdep->oss_type,
  391. hwdep->card, hwdep->device,
  392. &snd_hwdep_f_ops, hwdep,
  393. hwdep->oss_dev) < 0) {
  394. snd_printk(KERN_ERR "unable to register OSS compatibility device %i:%i\n",
  395. hwdep->card->number, hwdep->device);
  396. } else
  397. hwdep->ossreg = 1;
  398. }
  399. }
  400. #endif
  401. mutex_unlock(&register_mutex);
  402. return 0;
  403. }
  404. static int snd_hwdep_dev_disconnect(struct snd_device *device)
  405. {
  406. struct snd_hwdep *hwdep = device->device_data;
  407. snd_assert(hwdep != NULL, return -ENXIO);
  408. mutex_lock(&register_mutex);
  409. if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep) {
  410. mutex_unlock(&register_mutex);
  411. return -EINVAL;
  412. }
  413. #ifdef CONFIG_SND_OSSEMUL
  414. if (hwdep->ossreg)
  415. snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
  416. #endif
  417. snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
  418. list_del_init(&hwdep->list);
  419. mutex_unlock(&register_mutex);
  420. return 0;
  421. }
  422. #ifdef CONFIG_PROC_FS
  423. /*
  424. * Info interface
  425. */
  426. static void snd_hwdep_proc_read(struct snd_info_entry *entry,
  427. struct snd_info_buffer *buffer)
  428. {
  429. struct list_head *p;
  430. struct snd_hwdep *hwdep;
  431. mutex_lock(&register_mutex);
  432. list_for_each(p, &snd_hwdep_devices) {
  433. hwdep = list_entry(p, struct snd_hwdep, list);
  434. snd_iprintf(buffer, "%02i-%02i: %s\n",
  435. hwdep->card->number, hwdep->device, hwdep->name);
  436. }
  437. mutex_unlock(&register_mutex);
  438. }
  439. static struct snd_info_entry *snd_hwdep_proc_entry;
  440. static void __init snd_hwdep_proc_init(void)
  441. {
  442. struct snd_info_entry *entry;
  443. if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
  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_free_entry(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);