hwdep.c 13 KB

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