hwdep.c 13 KB

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