hwdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 <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@suse.cz>");
  33. MODULE_DESCRIPTION("Hardware dependent layer");
  34. MODULE_LICENSE("GPL");
  35. static snd_hwdep_t *snd_hwdep_devices[SNDRV_CARDS * SNDRV_MINOR_HWDEPS];
  36. static DECLARE_MUTEX(register_mutex);
  37. static int snd_hwdep_free(snd_hwdep_t *hwdep);
  38. static int snd_hwdep_dev_free(snd_device_t *device);
  39. static int snd_hwdep_dev_register(snd_device_t *device);
  40. static int snd_hwdep_dev_unregister(snd_device_t *device);
  41. /*
  42. */
  43. static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
  44. {
  45. snd_hwdep_t *hw = file->private_data;
  46. if (hw->ops.llseek)
  47. return hw->ops.llseek(hw, file, offset, orig);
  48. return -ENXIO;
  49. }
  50. static ssize_t snd_hwdep_read(struct file * file, char __user *buf, size_t count, loff_t *offset)
  51. {
  52. snd_hwdep_t *hw = file->private_data;
  53. if (hw->ops.read)
  54. return hw->ops.read(hw, buf, count, offset);
  55. return -ENXIO;
  56. }
  57. static ssize_t snd_hwdep_write(struct file * file, const char __user *buf, size_t count, loff_t *offset)
  58. {
  59. snd_hwdep_t *hw = file->private_data;
  60. if (hw->ops.write)
  61. return hw->ops.write(hw, buf, count, offset);
  62. return -ENXIO;
  63. }
  64. static int snd_hwdep_open(struct inode *inode, struct file * file)
  65. {
  66. int major = imajor(inode);
  67. int cardnum;
  68. int device;
  69. snd_hwdep_t *hw;
  70. int err;
  71. wait_queue_t wait;
  72. switch (major) {
  73. case CONFIG_SND_MAJOR:
  74. cardnum = SNDRV_MINOR_CARD(iminor(inode));
  75. device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_HWDEP;
  76. break;
  77. #ifdef CONFIG_SND_OSSEMUL
  78. case SOUND_MAJOR:
  79. cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
  80. device = 0;
  81. break;
  82. #endif
  83. default:
  84. return -ENXIO;
  85. }
  86. cardnum %= SNDRV_CARDS;
  87. device %= SNDRV_MINOR_HWDEPS;
  88. hw = snd_hwdep_devices[(cardnum * SNDRV_MINOR_HWDEPS) + device];
  89. if (hw == NULL)
  90. return -ENODEV;
  91. if (!hw->ops.open)
  92. return -ENXIO;
  93. #ifdef CONFIG_SND_OSSEMUL
  94. if (major == SOUND_MAJOR && hw->oss_type < 0)
  95. return -ENXIO;
  96. #endif
  97. if (!try_module_get(hw->card->module))
  98. return -EFAULT;
  99. init_waitqueue_entry(&wait, current);
  100. add_wait_queue(&hw->open_wait, &wait);
  101. down(&hw->open_mutex);
  102. while (1) {
  103. if (hw->exclusive && hw->used > 0) {
  104. err = -EBUSY;
  105. break;
  106. }
  107. err = hw->ops.open(hw, file);
  108. if (err >= 0)
  109. break;
  110. if (err == -EAGAIN) {
  111. if (file->f_flags & O_NONBLOCK) {
  112. err = -EBUSY;
  113. break;
  114. }
  115. } else
  116. break;
  117. set_current_state(TASK_INTERRUPTIBLE);
  118. up(&hw->open_mutex);
  119. schedule();
  120. down(&hw->open_mutex);
  121. if (signal_pending(current)) {
  122. err = -ERESTARTSYS;
  123. break;
  124. }
  125. }
  126. remove_wait_queue(&hw->open_wait, &wait);
  127. if (err >= 0) {
  128. err = snd_card_file_add(hw->card, file);
  129. if (err >= 0) {
  130. file->private_data = hw;
  131. hw->used++;
  132. } else {
  133. if (hw->ops.release)
  134. hw->ops.release(hw, file);
  135. }
  136. }
  137. up(&hw->open_mutex);
  138. if (err < 0)
  139. module_put(hw->card->module);
  140. return err;
  141. }
  142. static int snd_hwdep_release(struct inode *inode, struct file * file)
  143. {
  144. int err = -ENXIO;
  145. snd_hwdep_t *hw = file->private_data;
  146. down(&hw->open_mutex);
  147. if (hw->ops.release) {
  148. err = hw->ops.release(hw, file);
  149. wake_up(&hw->open_wait);
  150. }
  151. if (hw->used > 0)
  152. hw->used--;
  153. snd_card_file_remove(hw->card, file);
  154. up(&hw->open_mutex);
  155. module_put(hw->card->module);
  156. return err;
  157. }
  158. static unsigned int snd_hwdep_poll(struct file * file, poll_table * wait)
  159. {
  160. snd_hwdep_t *hw = file->private_data;
  161. if (hw->ops.poll)
  162. return hw->ops.poll(hw, file, wait);
  163. return 0;
  164. }
  165. static int snd_hwdep_info(snd_hwdep_t *hw, snd_hwdep_info_t __user *_info)
  166. {
  167. snd_hwdep_info_t info;
  168. memset(&info, 0, sizeof(info));
  169. info.card = hw->card->number;
  170. strlcpy(info.id, hw->id, sizeof(info.id));
  171. strlcpy(info.name, hw->name, sizeof(info.name));
  172. info.iface = hw->iface;
  173. if (copy_to_user(_info, &info, sizeof(info)))
  174. return -EFAULT;
  175. return 0;
  176. }
  177. static int snd_hwdep_dsp_status(snd_hwdep_t *hw, snd_hwdep_dsp_status_t __user *_info)
  178. {
  179. snd_hwdep_dsp_status_t info;
  180. int err;
  181. if (! hw->ops.dsp_status)
  182. return -ENXIO;
  183. memset(&info, 0, sizeof(info));
  184. info.dsp_loaded = hw->dsp_loaded;
  185. if ((err = hw->ops.dsp_status(hw, &info)) < 0)
  186. return err;
  187. if (copy_to_user(_info, &info, sizeof(info)))
  188. return -EFAULT;
  189. return 0;
  190. }
  191. static int snd_hwdep_dsp_load(snd_hwdep_t *hw, snd_hwdep_dsp_image_t __user *_info)
  192. {
  193. snd_hwdep_dsp_image_t 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, unsigned long arg)
  212. {
  213. snd_hwdep_t *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. snd_hwdep_t *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(snd_card_t * card, snd_ctl_file_t * control,
  237. unsigned int cmd, unsigned long arg)
  238. {
  239. unsigned int tmp;
  240. tmp = card->number * SNDRV_MINOR_HWDEPS;
  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. device = device < 0 ? 0 : device + 1;
  248. while (device < SNDRV_MINOR_HWDEPS) {
  249. if (snd_hwdep_devices[tmp + device])
  250. break;
  251. device++;
  252. }
  253. if (device >= SNDRV_MINOR_HWDEPS)
  254. device = -1;
  255. if (put_user(device, (int __user *)arg))
  256. return -EFAULT;
  257. return 0;
  258. }
  259. case SNDRV_CTL_IOCTL_HWDEP_INFO:
  260. {
  261. snd_hwdep_info_t __user *info = (snd_hwdep_info_t __user *)arg;
  262. int device;
  263. snd_hwdep_t *hwdep;
  264. if (get_user(device, &info->device))
  265. return -EFAULT;
  266. if (device < 0 || device >= SNDRV_MINOR_HWDEPS)
  267. return -ENXIO;
  268. hwdep = snd_hwdep_devices[tmp + device];
  269. if (hwdep == NULL)
  270. return -ENXIO;
  271. return snd_hwdep_info(hwdep, info);
  272. }
  273. }
  274. return -ENOIOCTLCMD;
  275. }
  276. #ifdef CONFIG_COMPAT
  277. #include "hwdep_compat.c"
  278. #else
  279. #define snd_hwdep_ioctl_compat NULL
  280. #endif
  281. /*
  282. */
  283. static struct file_operations snd_hwdep_f_ops =
  284. {
  285. .owner = THIS_MODULE,
  286. .llseek = snd_hwdep_llseek,
  287. .read = snd_hwdep_read,
  288. .write = snd_hwdep_write,
  289. .open = snd_hwdep_open,
  290. .release = snd_hwdep_release,
  291. .poll = snd_hwdep_poll,
  292. .unlocked_ioctl = snd_hwdep_ioctl,
  293. .compat_ioctl = snd_hwdep_ioctl_compat,
  294. .mmap = snd_hwdep_mmap,
  295. };
  296. static snd_minor_t snd_hwdep_reg =
  297. {
  298. .comment = "hardware dependent",
  299. .f_ops = &snd_hwdep_f_ops,
  300. };
  301. /**
  302. * snd_hwdep_new - create a new hwdep instance
  303. * @card: the card instance
  304. * @id: the id string
  305. * @device: the device index (zero-based)
  306. * @rhwdep: the pointer to store the new hwdep instance
  307. *
  308. * Creates a new hwdep instance with the given index on the card.
  309. * The callbacks (hwdep->ops) must be set on the returned instance
  310. * after this call manually by the caller.
  311. *
  312. * Returns zero if successful, or a negative error code on failure.
  313. */
  314. int snd_hwdep_new(snd_card_t * card, char *id, int device, snd_hwdep_t ** rhwdep)
  315. {
  316. snd_hwdep_t *hwdep;
  317. int err;
  318. static snd_device_ops_t ops = {
  319. .dev_free = snd_hwdep_dev_free,
  320. .dev_register = snd_hwdep_dev_register,
  321. .dev_unregister = snd_hwdep_dev_unregister
  322. };
  323. snd_assert(rhwdep != NULL, return -EINVAL);
  324. *rhwdep = NULL;
  325. snd_assert(card != NULL, return -ENXIO);
  326. hwdep = kcalloc(1, sizeof(*hwdep), GFP_KERNEL);
  327. if (hwdep == NULL)
  328. return -ENOMEM;
  329. hwdep->card = card;
  330. hwdep->device = device;
  331. if (id) {
  332. strlcpy(hwdep->id, id, sizeof(hwdep->id));
  333. }
  334. #ifdef CONFIG_SND_OSSEMUL
  335. hwdep->oss_type = -1;
  336. #endif
  337. if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
  338. snd_hwdep_free(hwdep);
  339. return err;
  340. }
  341. init_waitqueue_head(&hwdep->open_wait);
  342. init_MUTEX(&hwdep->open_mutex);
  343. *rhwdep = hwdep;
  344. return 0;
  345. }
  346. static int snd_hwdep_free(snd_hwdep_t *hwdep)
  347. {
  348. snd_assert(hwdep != NULL, return -ENXIO);
  349. if (hwdep->private_free)
  350. hwdep->private_free(hwdep);
  351. kfree(hwdep);
  352. return 0;
  353. }
  354. static int snd_hwdep_dev_free(snd_device_t *device)
  355. {
  356. snd_hwdep_t *hwdep = device->device_data;
  357. return snd_hwdep_free(hwdep);
  358. }
  359. static int snd_hwdep_dev_register(snd_device_t *device)
  360. {
  361. snd_hwdep_t *hwdep = device->device_data;
  362. int idx, err;
  363. char name[32];
  364. down(&register_mutex);
  365. idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
  366. if (snd_hwdep_devices[idx]) {
  367. up(&register_mutex);
  368. return -EBUSY;
  369. }
  370. snd_hwdep_devices[idx] = hwdep;
  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_reg, name)) < 0) {
  375. snd_printk(KERN_ERR "unable to register hardware dependent device %i:%i\n",
  376. hwdep->card->number, hwdep->device);
  377. snd_hwdep_devices[idx] = NULL;
  378. up(&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_reg, hwdep->oss_dev) < 0) {
  390. snd_printk(KERN_ERR "unable to register OSS compatibility device %i:%i\n",
  391. hwdep->card->number, hwdep->device);
  392. } else
  393. hwdep->ossreg = 1;
  394. }
  395. }
  396. #endif
  397. up(&register_mutex);
  398. return 0;
  399. }
  400. static int snd_hwdep_dev_unregister(snd_device_t *device)
  401. {
  402. snd_hwdep_t *hwdep = device->device_data;
  403. int idx;
  404. snd_assert(hwdep != NULL, return -ENXIO);
  405. down(&register_mutex);
  406. idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
  407. if (snd_hwdep_devices[idx] != hwdep) {
  408. up(&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. snd_hwdep_devices[idx] = NULL;
  417. up(&register_mutex);
  418. return snd_hwdep_free(hwdep);
  419. }
  420. /*
  421. * Info interface
  422. */
  423. static void snd_hwdep_proc_read(snd_info_entry_t *entry,
  424. snd_info_buffer_t * buffer)
  425. {
  426. int idx;
  427. snd_hwdep_t *hwdep;
  428. down(&register_mutex);
  429. for (idx = 0; idx < SNDRV_CARDS * SNDRV_MINOR_HWDEPS; idx++) {
  430. hwdep = snd_hwdep_devices[idx];
  431. if (hwdep == NULL)
  432. continue;
  433. snd_iprintf(buffer, "%02i-%02i: %s\n",
  434. idx / SNDRV_MINOR_HWDEPS,
  435. idx % SNDRV_MINOR_HWDEPS,
  436. hwdep->name);
  437. }
  438. up(&register_mutex);
  439. }
  440. /*
  441. * ENTRY functions
  442. */
  443. static snd_info_entry_t *snd_hwdep_proc_entry = NULL;
  444. static int __init alsa_hwdep_init(void)
  445. {
  446. snd_info_entry_t *entry;
  447. memset(snd_hwdep_devices, 0, sizeof(snd_hwdep_devices));
  448. if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
  449. entry->c.text.read_size = 512;
  450. entry->c.text.read = snd_hwdep_proc_read;
  451. if (snd_info_register(entry) < 0) {
  452. snd_info_free_entry(entry);
  453. entry = NULL;
  454. }
  455. }
  456. snd_hwdep_proc_entry = entry;
  457. snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
  458. snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl);
  459. return 0;
  460. }
  461. static void __exit alsa_hwdep_exit(void)
  462. {
  463. snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
  464. snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl);
  465. if (snd_hwdep_proc_entry) {
  466. snd_info_unregister(snd_hwdep_proc_entry);
  467. snd_hwdep_proc_entry = NULL;
  468. }
  469. }
  470. module_init(alsa_hwdep_init)
  471. module_exit(alsa_hwdep_exit)
  472. EXPORT_SYMBOL(snd_hwdep_new);