hwdep.c 13 KB

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