hwdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 <linux/module.h>
  27. #include <sound/core.h>
  28. #include <sound/control.h>
  29. #include <sound/minors.h>
  30. #include <sound/hwdep.h>
  31. #include <sound/info.h>
  32. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  33. MODULE_DESCRIPTION("Hardware dependent layer");
  34. MODULE_LICENSE("GPL");
  35. static LIST_HEAD(snd_hwdep_devices);
  36. static DEFINE_MUTEX(register_mutex);
  37. static int snd_hwdep_free(struct snd_hwdep *hwdep);
  38. static int snd_hwdep_dev_free(struct snd_device *device);
  39. static int snd_hwdep_dev_register(struct snd_device *device);
  40. static int snd_hwdep_dev_disconnect(struct snd_device *device);
  41. static struct snd_hwdep *snd_hwdep_search(struct snd_card *card, int device)
  42. {
  43. struct snd_hwdep *hwdep;
  44. list_for_each_entry(hwdep, &snd_hwdep_devices, list)
  45. if (hwdep->card == card && hwdep->device == device)
  46. return hwdep;
  47. return NULL;
  48. }
  49. static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
  50. {
  51. struct snd_hwdep *hw = file->private_data;
  52. if (hw->ops.llseek)
  53. return hw->ops.llseek(hw, file, offset, orig);
  54. return -ENXIO;
  55. }
  56. static ssize_t snd_hwdep_read(struct file * file, char __user *buf,
  57. size_t count, loff_t *offset)
  58. {
  59. struct snd_hwdep *hw = file->private_data;
  60. if (hw->ops.read)
  61. return hw->ops.read(hw, buf, count, offset);
  62. return -ENXIO;
  63. }
  64. static ssize_t snd_hwdep_write(struct file * file, const char __user *buf,
  65. size_t count, loff_t *offset)
  66. {
  67. struct snd_hwdep *hw = file->private_data;
  68. if (hw->ops.write)
  69. return hw->ops.write(hw, buf, count, offset);
  70. return -ENXIO;
  71. }
  72. static int snd_hwdep_open(struct inode *inode, struct file * file)
  73. {
  74. int major = imajor(inode);
  75. struct snd_hwdep *hw;
  76. int err;
  77. wait_queue_t wait;
  78. if (major == snd_major) {
  79. hw = snd_lookup_minor_data(iminor(inode),
  80. SNDRV_DEVICE_TYPE_HWDEP);
  81. #ifdef CONFIG_SND_OSSEMUL
  82. } else if (major == SOUND_MAJOR) {
  83. hw = snd_lookup_oss_minor_data(iminor(inode),
  84. SNDRV_OSS_DEVICE_TYPE_DMFM);
  85. #endif
  86. } else
  87. return -ENXIO;
  88. if (hw == NULL)
  89. return -ENODEV;
  90. if (!try_module_get(hw->card->module))
  91. return -EFAULT;
  92. init_waitqueue_entry(&wait, current);
  93. add_wait_queue(&hw->open_wait, &wait);
  94. mutex_lock(&hw->open_mutex);
  95. while (1) {
  96. if (hw->exclusive && hw->used > 0) {
  97. err = -EBUSY;
  98. break;
  99. }
  100. if (!hw->ops.open) {
  101. err = 0;
  102. break;
  103. }
  104. err = hw->ops.open(hw, file);
  105. if (err >= 0)
  106. break;
  107. if (err == -EAGAIN) {
  108. if (file->f_flags & O_NONBLOCK) {
  109. err = -EBUSY;
  110. break;
  111. }
  112. } else
  113. break;
  114. set_current_state(TASK_INTERRUPTIBLE);
  115. mutex_unlock(&hw->open_mutex);
  116. schedule();
  117. mutex_lock(&hw->open_mutex);
  118. if (signal_pending(current)) {
  119. err = -ERESTARTSYS;
  120. break;
  121. }
  122. }
  123. remove_wait_queue(&hw->open_wait, &wait);
  124. if (err >= 0) {
  125. err = snd_card_file_add(hw->card, file);
  126. if (err >= 0) {
  127. file->private_data = hw;
  128. hw->used++;
  129. } else {
  130. if (hw->ops.release)
  131. hw->ops.release(hw, file);
  132. }
  133. }
  134. mutex_unlock(&hw->open_mutex);
  135. if (err < 0)
  136. module_put(hw->card->module);
  137. return err;
  138. }
  139. static int snd_hwdep_release(struct inode *inode, struct file * file)
  140. {
  141. int err = 0;
  142. struct snd_hwdep *hw = file->private_data;
  143. struct module *mod = hw->card->module;
  144. mutex_lock(&hw->open_mutex);
  145. if (hw->ops.release)
  146. err = hw->ops.release(hw, file);
  147. if (hw->used > 0)
  148. hw->used--;
  149. mutex_unlock(&hw->open_mutex);
  150. wake_up(&hw->open_wait);
  151. snd_card_file_remove(hw->card, file);
  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. if (device < 0)
  249. device = 0;
  250. else if (device < SNDRV_MINOR_HWDEPS)
  251. device++;
  252. else
  253. device = SNDRV_MINOR_HWDEPS;
  254. while (device < SNDRV_MINOR_HWDEPS) {
  255. if (snd_hwdep_search(card, device))
  256. break;
  257. device++;
  258. }
  259. if (device >= SNDRV_MINOR_HWDEPS)
  260. device = -1;
  261. mutex_unlock(&register_mutex);
  262. if (put_user(device, (int __user *)arg))
  263. return -EFAULT;
  264. return 0;
  265. }
  266. case SNDRV_CTL_IOCTL_HWDEP_INFO:
  267. {
  268. struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
  269. int device, err;
  270. struct snd_hwdep *hwdep;
  271. if (get_user(device, &info->device))
  272. return -EFAULT;
  273. mutex_lock(&register_mutex);
  274. hwdep = snd_hwdep_search(card, device);
  275. if (hwdep)
  276. err = snd_hwdep_info(hwdep, info);
  277. else
  278. err = -ENXIO;
  279. mutex_unlock(&register_mutex);
  280. return err;
  281. }
  282. }
  283. return -ENOIOCTLCMD;
  284. }
  285. #ifdef CONFIG_COMPAT
  286. #include "hwdep_compat.c"
  287. #else
  288. #define snd_hwdep_ioctl_compat NULL
  289. #endif
  290. /*
  291. */
  292. static const struct file_operations snd_hwdep_f_ops =
  293. {
  294. .owner = THIS_MODULE,
  295. .llseek = snd_hwdep_llseek,
  296. .read = snd_hwdep_read,
  297. .write = snd_hwdep_write,
  298. .open = snd_hwdep_open,
  299. .release = snd_hwdep_release,
  300. .poll = snd_hwdep_poll,
  301. .unlocked_ioctl = snd_hwdep_ioctl,
  302. .compat_ioctl = snd_hwdep_ioctl_compat,
  303. .mmap = snd_hwdep_mmap,
  304. };
  305. /**
  306. * snd_hwdep_new - create a new hwdep instance
  307. * @card: the card instance
  308. * @id: the id string
  309. * @device: the device index (zero-based)
  310. * @rhwdep: the pointer to store the new hwdep instance
  311. *
  312. * Creates a new hwdep instance with the given index on the card.
  313. * The callbacks (hwdep->ops) must be set on the returned instance
  314. * after this call manually by the caller.
  315. *
  316. * Returns zero if successful, or a negative error code on failure.
  317. */
  318. int snd_hwdep_new(struct snd_card *card, char *id, int device,
  319. struct snd_hwdep **rhwdep)
  320. {
  321. struct snd_hwdep *hwdep;
  322. int err;
  323. static struct snd_device_ops ops = {
  324. .dev_free = snd_hwdep_dev_free,
  325. .dev_register = snd_hwdep_dev_register,
  326. .dev_disconnect = snd_hwdep_dev_disconnect,
  327. };
  328. if (snd_BUG_ON(!card))
  329. return -ENXIO;
  330. if (rhwdep)
  331. *rhwdep = NULL;
  332. hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
  333. if (hwdep == NULL) {
  334. snd_printk(KERN_ERR "hwdep: cannot allocate\n");
  335. return -ENOMEM;
  336. }
  337. hwdep->card = card;
  338. hwdep->device = device;
  339. if (id)
  340. strlcpy(hwdep->id, id, sizeof(hwdep->id));
  341. #ifdef CONFIG_SND_OSSEMUL
  342. hwdep->oss_type = -1;
  343. #endif
  344. if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
  345. snd_hwdep_free(hwdep);
  346. return err;
  347. }
  348. init_waitqueue_head(&hwdep->open_wait);
  349. mutex_init(&hwdep->open_mutex);
  350. if (rhwdep)
  351. *rhwdep = hwdep;
  352. return 0;
  353. }
  354. static int snd_hwdep_free(struct snd_hwdep *hwdep)
  355. {
  356. if (!hwdep)
  357. return 0;
  358. if (hwdep->private_free)
  359. hwdep->private_free(hwdep);
  360. kfree(hwdep);
  361. return 0;
  362. }
  363. static int snd_hwdep_dev_free(struct snd_device *device)
  364. {
  365. struct snd_hwdep *hwdep = device->device_data;
  366. return snd_hwdep_free(hwdep);
  367. }
  368. static int snd_hwdep_dev_register(struct snd_device *device)
  369. {
  370. struct snd_hwdep *hwdep = device->device_data;
  371. int err;
  372. char name[32];
  373. mutex_lock(&register_mutex);
  374. if (snd_hwdep_search(hwdep->card, hwdep->device)) {
  375. mutex_unlock(&register_mutex);
  376. return -EBUSY;
  377. }
  378. list_add_tail(&hwdep->list, &snd_hwdep_devices);
  379. sprintf(name, "hwC%iD%i", hwdep->card->number, hwdep->device);
  380. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
  381. hwdep->card, hwdep->device,
  382. &snd_hwdep_f_ops, hwdep, name)) < 0) {
  383. snd_printk(KERN_ERR "unable to register hardware dependent device %i:%i\n",
  384. hwdep->card->number, hwdep->device);
  385. list_del(&hwdep->list);
  386. mutex_unlock(&register_mutex);
  387. return err;
  388. }
  389. #ifdef CONFIG_SND_OSSEMUL
  390. hwdep->ossreg = 0;
  391. if (hwdep->oss_type >= 0) {
  392. if ((hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM) && (hwdep->device != 0)) {
  393. snd_printk (KERN_WARNING "only hwdep device 0 can be registered as OSS direct FM device!\n");
  394. } else {
  395. if (snd_register_oss_device(hwdep->oss_type,
  396. hwdep->card, hwdep->device,
  397. &snd_hwdep_f_ops, hwdep,
  398. hwdep->oss_dev) < 0) {
  399. snd_printk(KERN_ERR "unable to register OSS compatibility device %i:%i\n",
  400. hwdep->card->number, hwdep->device);
  401. } else
  402. hwdep->ossreg = 1;
  403. }
  404. }
  405. #endif
  406. mutex_unlock(&register_mutex);
  407. return 0;
  408. }
  409. static int snd_hwdep_dev_disconnect(struct snd_device *device)
  410. {
  411. struct snd_hwdep *hwdep = device->device_data;
  412. if (snd_BUG_ON(!hwdep))
  413. return -ENXIO;
  414. mutex_lock(&register_mutex);
  415. if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep) {
  416. mutex_unlock(&register_mutex);
  417. return -EINVAL;
  418. }
  419. #ifdef CONFIG_SND_OSSEMUL
  420. if (hwdep->ossreg)
  421. snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
  422. #endif
  423. snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
  424. list_del_init(&hwdep->list);
  425. mutex_unlock(&register_mutex);
  426. return 0;
  427. }
  428. #ifdef CONFIG_PROC_FS
  429. /*
  430. * Info interface
  431. */
  432. static void snd_hwdep_proc_read(struct snd_info_entry *entry,
  433. struct snd_info_buffer *buffer)
  434. {
  435. struct snd_hwdep *hwdep;
  436. mutex_lock(&register_mutex);
  437. list_for_each_entry(hwdep, &snd_hwdep_devices, list)
  438. snd_iprintf(buffer, "%02i-%02i: %s\n",
  439. hwdep->card->number, hwdep->device, hwdep->name);
  440. mutex_unlock(&register_mutex);
  441. }
  442. static struct snd_info_entry *snd_hwdep_proc_entry;
  443. static void __init snd_hwdep_proc_init(void)
  444. {
  445. struct snd_info_entry *entry;
  446. if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
  447. entry->c.text.read = snd_hwdep_proc_read;
  448. if (snd_info_register(entry) < 0) {
  449. snd_info_free_entry(entry);
  450. entry = NULL;
  451. }
  452. }
  453. snd_hwdep_proc_entry = entry;
  454. }
  455. static void __exit snd_hwdep_proc_done(void)
  456. {
  457. snd_info_free_entry(snd_hwdep_proc_entry);
  458. }
  459. #else /* !CONFIG_PROC_FS */
  460. #define snd_hwdep_proc_init()
  461. #define snd_hwdep_proc_done()
  462. #endif /* CONFIG_PROC_FS */
  463. /*
  464. * ENTRY functions
  465. */
  466. static int __init alsa_hwdep_init(void)
  467. {
  468. snd_hwdep_proc_init();
  469. snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
  470. snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl);
  471. return 0;
  472. }
  473. static void __exit alsa_hwdep_exit(void)
  474. {
  475. snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
  476. snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl);
  477. snd_hwdep_proc_done();
  478. }
  479. module_init(alsa_hwdep_init)
  480. module_exit(alsa_hwdep_exit)
  481. EXPORT_SYMBOL(snd_hwdep_new);