hwdep.c 13 KB

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