hwdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 (!try_module_get(hw->card->module))
  90. return -EFAULT;
  91. init_waitqueue_entry(&wait, current);
  92. add_wait_queue(&hw->open_wait, &wait);
  93. mutex_lock(&hw->open_mutex);
  94. while (1) {
  95. if (hw->exclusive && hw->used > 0) {
  96. err = -EBUSY;
  97. break;
  98. }
  99. if (!hw->ops.open) {
  100. err = 0;
  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 = 0;
  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. if (device < 0)
  248. device = 0;
  249. else if (device < SNDRV_MINOR_HWDEPS)
  250. device++;
  251. else
  252. device = SNDRV_MINOR_HWDEPS;
  253. while (device < SNDRV_MINOR_HWDEPS) {
  254. if (snd_hwdep_search(card, device))
  255. break;
  256. device++;
  257. }
  258. if (device >= SNDRV_MINOR_HWDEPS)
  259. device = -1;
  260. mutex_unlock(&register_mutex);
  261. if (put_user(device, (int __user *)arg))
  262. return -EFAULT;
  263. return 0;
  264. }
  265. case SNDRV_CTL_IOCTL_HWDEP_INFO:
  266. {
  267. struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
  268. int device, err;
  269. struct snd_hwdep *hwdep;
  270. if (get_user(device, &info->device))
  271. return -EFAULT;
  272. mutex_lock(&register_mutex);
  273. hwdep = snd_hwdep_search(card, device);
  274. if (hwdep)
  275. err = snd_hwdep_info(hwdep, info);
  276. else
  277. err = -ENXIO;
  278. mutex_unlock(&register_mutex);
  279. return err;
  280. }
  281. }
  282. return -ENOIOCTLCMD;
  283. }
  284. #ifdef CONFIG_COMPAT
  285. #include "hwdep_compat.c"
  286. #else
  287. #define snd_hwdep_ioctl_compat NULL
  288. #endif
  289. /*
  290. */
  291. static const struct file_operations snd_hwdep_f_ops =
  292. {
  293. .owner = THIS_MODULE,
  294. .llseek = snd_hwdep_llseek,
  295. .read = snd_hwdep_read,
  296. .write = snd_hwdep_write,
  297. .open = snd_hwdep_open,
  298. .release = snd_hwdep_release,
  299. .poll = snd_hwdep_poll,
  300. .unlocked_ioctl = snd_hwdep_ioctl,
  301. .compat_ioctl = snd_hwdep_ioctl_compat,
  302. .mmap = snd_hwdep_mmap,
  303. };
  304. /**
  305. * snd_hwdep_new - create a new hwdep instance
  306. * @card: the card instance
  307. * @id: the id string
  308. * @device: the device index (zero-based)
  309. * @rhwdep: the pointer to store the new hwdep instance
  310. *
  311. * Creates a new hwdep instance with the given index on the card.
  312. * The callbacks (hwdep->ops) must be set on the returned instance
  313. * after this call manually by the caller.
  314. *
  315. * Returns zero if successful, or a negative error code on failure.
  316. */
  317. int snd_hwdep_new(struct snd_card *card, char *id, int device,
  318. struct snd_hwdep **rhwdep)
  319. {
  320. struct snd_hwdep *hwdep;
  321. int err;
  322. static struct snd_device_ops ops = {
  323. .dev_free = snd_hwdep_dev_free,
  324. .dev_register = snd_hwdep_dev_register,
  325. .dev_disconnect = snd_hwdep_dev_disconnect,
  326. };
  327. if (snd_BUG_ON(!card))
  328. return -ENXIO;
  329. if (rhwdep)
  330. *rhwdep = NULL;
  331. hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
  332. if (hwdep == NULL) {
  333. snd_printk(KERN_ERR "hwdep: cannot allocate\n");
  334. return -ENOMEM;
  335. }
  336. hwdep->card = card;
  337. hwdep->device = device;
  338. if (id)
  339. strlcpy(hwdep->id, id, sizeof(hwdep->id));
  340. #ifdef CONFIG_SND_OSSEMUL
  341. hwdep->oss_type = -1;
  342. #endif
  343. if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
  344. snd_hwdep_free(hwdep);
  345. return err;
  346. }
  347. init_waitqueue_head(&hwdep->open_wait);
  348. mutex_init(&hwdep->open_mutex);
  349. if (rhwdep)
  350. *rhwdep = hwdep;
  351. return 0;
  352. }
  353. static int snd_hwdep_free(struct snd_hwdep *hwdep)
  354. {
  355. if (!hwdep)
  356. return 0;
  357. if (hwdep->private_free)
  358. hwdep->private_free(hwdep);
  359. kfree(hwdep);
  360. return 0;
  361. }
  362. static int snd_hwdep_dev_free(struct snd_device *device)
  363. {
  364. struct snd_hwdep *hwdep = device->device_data;
  365. return snd_hwdep_free(hwdep);
  366. }
  367. static int snd_hwdep_dev_register(struct snd_device *device)
  368. {
  369. struct snd_hwdep *hwdep = device->device_data;
  370. int err;
  371. char name[32];
  372. mutex_lock(&register_mutex);
  373. if (snd_hwdep_search(hwdep->card, hwdep->device)) {
  374. mutex_unlock(&register_mutex);
  375. return -EBUSY;
  376. }
  377. list_add_tail(&hwdep->list, &snd_hwdep_devices);
  378. sprintf(name, "hwC%iD%i", hwdep->card->number, hwdep->device);
  379. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
  380. hwdep->card, hwdep->device,
  381. &snd_hwdep_f_ops, hwdep, name)) < 0) {
  382. snd_printk(KERN_ERR "unable to register hardware dependent device %i:%i\n",
  383. hwdep->card->number, hwdep->device);
  384. list_del(&hwdep->list);
  385. mutex_unlock(&register_mutex);
  386. return err;
  387. }
  388. #ifdef CONFIG_SND_OSSEMUL
  389. hwdep->ossreg = 0;
  390. if (hwdep->oss_type >= 0) {
  391. if ((hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM) && (hwdep->device != 0)) {
  392. snd_printk (KERN_WARNING "only hwdep device 0 can be registered as OSS direct FM device!\n");
  393. } else {
  394. if (snd_register_oss_device(hwdep->oss_type,
  395. hwdep->card, hwdep->device,
  396. &snd_hwdep_f_ops, hwdep,
  397. hwdep->oss_dev) < 0) {
  398. snd_printk(KERN_ERR "unable to register OSS compatibility device %i:%i\n",
  399. hwdep->card->number, hwdep->device);
  400. } else
  401. hwdep->ossreg = 1;
  402. }
  403. }
  404. #endif
  405. mutex_unlock(&register_mutex);
  406. return 0;
  407. }
  408. static int snd_hwdep_dev_disconnect(struct snd_device *device)
  409. {
  410. struct snd_hwdep *hwdep = device->device_data;
  411. if (snd_BUG_ON(!hwdep))
  412. return -ENXIO;
  413. mutex_lock(&register_mutex);
  414. if (snd_hwdep_search(hwdep->card, hwdep->device) != hwdep) {
  415. mutex_unlock(&register_mutex);
  416. return -EINVAL;
  417. }
  418. #ifdef CONFIG_SND_OSSEMUL
  419. if (hwdep->ossreg)
  420. snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
  421. #endif
  422. snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
  423. list_del_init(&hwdep->list);
  424. mutex_unlock(&register_mutex);
  425. return 0;
  426. }
  427. #ifdef CONFIG_PROC_FS
  428. /*
  429. * Info interface
  430. */
  431. static void snd_hwdep_proc_read(struct snd_info_entry *entry,
  432. struct snd_info_buffer *buffer)
  433. {
  434. struct snd_hwdep *hwdep;
  435. mutex_lock(&register_mutex);
  436. list_for_each_entry(hwdep, &snd_hwdep_devices, list)
  437. snd_iprintf(buffer, "%02i-%02i: %s\n",
  438. hwdep->card->number, hwdep->device, hwdep->name);
  439. mutex_unlock(&register_mutex);
  440. }
  441. static struct snd_info_entry *snd_hwdep_proc_entry;
  442. static void __init snd_hwdep_proc_init(void)
  443. {
  444. struct snd_info_entry *entry;
  445. if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
  446. entry->c.text.read = snd_hwdep_proc_read;
  447. if (snd_info_register(entry) < 0) {
  448. snd_info_free_entry(entry);
  449. entry = NULL;
  450. }
  451. }
  452. snd_hwdep_proc_entry = entry;
  453. }
  454. static void __exit snd_hwdep_proc_done(void)
  455. {
  456. snd_info_free_entry(snd_hwdep_proc_entry);
  457. }
  458. #else /* !CONFIG_PROC_FS */
  459. #define snd_hwdep_proc_init()
  460. #define snd_hwdep_proc_done()
  461. #endif /* CONFIG_PROC_FS */
  462. /*
  463. * ENTRY functions
  464. */
  465. static int __init alsa_hwdep_init(void)
  466. {
  467. snd_hwdep_proc_init();
  468. snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
  469. snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl);
  470. return 0;
  471. }
  472. static void __exit alsa_hwdep_exit(void)
  473. {
  474. snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
  475. snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl);
  476. snd_hwdep_proc_done();
  477. }
  478. module_init(alsa_hwdep_init)
  479. module_exit(alsa_hwdep_exit)
  480. EXPORT_SYMBOL(snd_hwdep_new);