hwdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. if (major == snd_major) {
  73. cardnum = SNDRV_MINOR_CARD(iminor(inode));
  74. device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_HWDEP;
  75. #ifdef CONFIG_SND_OSSEMUL
  76. } else if (major == SOUND_MAJOR) {
  77. cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
  78. device = 0;
  79. #endif
  80. } else
  81. return -ENXIO;
  82. cardnum %= SNDRV_CARDS;
  83. device %= SNDRV_MINOR_HWDEPS;
  84. hw = snd_hwdep_devices[(cardnum * SNDRV_MINOR_HWDEPS) + device];
  85. if (hw == NULL)
  86. return -ENODEV;
  87. if (!hw->ops.open)
  88. return -ENXIO;
  89. #ifdef CONFIG_SND_OSSEMUL
  90. if (major == SOUND_MAJOR && hw->oss_type < 0)
  91. return -ENXIO;
  92. #endif
  93. if (!try_module_get(hw->card->module))
  94. return -EFAULT;
  95. init_waitqueue_entry(&wait, current);
  96. add_wait_queue(&hw->open_wait, &wait);
  97. down(&hw->open_mutex);
  98. while (1) {
  99. if (hw->exclusive && hw->used > 0) {
  100. err = -EBUSY;
  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. up(&hw->open_mutex);
  115. schedule();
  116. down(&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. up(&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 = -ENXIO;
  141. snd_hwdep_t *hw = file->private_data;
  142. down(&hw->open_mutex);
  143. if (hw->ops.release) {
  144. err = hw->ops.release(hw, file);
  145. wake_up(&hw->open_wait);
  146. }
  147. if (hw->used > 0)
  148. hw->used--;
  149. snd_card_file_remove(hw->card, file);
  150. up(&hw->open_mutex);
  151. module_put(hw->card->module);
  152. return err;
  153. }
  154. static unsigned int snd_hwdep_poll(struct file * file, poll_table * wait)
  155. {
  156. snd_hwdep_t *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(snd_hwdep_t *hw, snd_hwdep_info_t __user *_info)
  162. {
  163. snd_hwdep_info_t 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(snd_hwdep_t *hw, snd_hwdep_dsp_status_t __user *_info)
  174. {
  175. snd_hwdep_dsp_status_t 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(snd_hwdep_t *hw, snd_hwdep_dsp_image_t __user *_info)
  188. {
  189. snd_hwdep_dsp_image_t info;
  190. int err;
  191. if (! hw->ops.dsp_load)
  192. return -ENXIO;
  193. memset(&info, 0, sizeof(info));
  194. if (copy_from_user(&info, _info, sizeof(info)))
  195. return -EFAULT;
  196. /* check whether the dsp was already loaded */
  197. if (hw->dsp_loaded & (1 << info.index))
  198. return -EBUSY;
  199. if (!access_ok(VERIFY_READ, info.image, info.length))
  200. return -EFAULT;
  201. err = hw->ops.dsp_load(hw, &info);
  202. if (err < 0)
  203. return err;
  204. hw->dsp_loaded |= (1 << info.index);
  205. return 0;
  206. }
  207. static long snd_hwdep_ioctl(struct file * file, unsigned int cmd, unsigned long arg)
  208. {
  209. snd_hwdep_t *hw = file->private_data;
  210. void __user *argp = (void __user *)arg;
  211. switch (cmd) {
  212. case SNDRV_HWDEP_IOCTL_PVERSION:
  213. return put_user(SNDRV_HWDEP_VERSION, (int __user *)argp);
  214. case SNDRV_HWDEP_IOCTL_INFO:
  215. return snd_hwdep_info(hw, argp);
  216. case SNDRV_HWDEP_IOCTL_DSP_STATUS:
  217. return snd_hwdep_dsp_status(hw, argp);
  218. case SNDRV_HWDEP_IOCTL_DSP_LOAD:
  219. return snd_hwdep_dsp_load(hw, argp);
  220. }
  221. if (hw->ops.ioctl)
  222. return hw->ops.ioctl(hw, file, cmd, arg);
  223. return -ENOTTY;
  224. }
  225. static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
  226. {
  227. snd_hwdep_t *hw = file->private_data;
  228. if (hw->ops.mmap)
  229. return hw->ops.mmap(hw, file, vma);
  230. return -ENXIO;
  231. }
  232. static int snd_hwdep_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
  233. unsigned int cmd, unsigned long arg)
  234. {
  235. unsigned int tmp;
  236. tmp = card->number * SNDRV_MINOR_HWDEPS;
  237. switch (cmd) {
  238. case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
  239. {
  240. int device;
  241. if (get_user(device, (int __user *)arg))
  242. return -EFAULT;
  243. device = device < 0 ? 0 : device + 1;
  244. while (device < SNDRV_MINOR_HWDEPS) {
  245. if (snd_hwdep_devices[tmp + device])
  246. break;
  247. device++;
  248. }
  249. if (device >= SNDRV_MINOR_HWDEPS)
  250. device = -1;
  251. if (put_user(device, (int __user *)arg))
  252. return -EFAULT;
  253. return 0;
  254. }
  255. case SNDRV_CTL_IOCTL_HWDEP_INFO:
  256. {
  257. snd_hwdep_info_t __user *info = (snd_hwdep_info_t __user *)arg;
  258. int device;
  259. snd_hwdep_t *hwdep;
  260. if (get_user(device, &info->device))
  261. return -EFAULT;
  262. if (device < 0 || device >= SNDRV_MINOR_HWDEPS)
  263. return -ENXIO;
  264. hwdep = snd_hwdep_devices[tmp + device];
  265. if (hwdep == NULL)
  266. return -ENXIO;
  267. return snd_hwdep_info(hwdep, info);
  268. }
  269. }
  270. return -ENOIOCTLCMD;
  271. }
  272. #ifdef CONFIG_COMPAT
  273. #include "hwdep_compat.c"
  274. #else
  275. #define snd_hwdep_ioctl_compat NULL
  276. #endif
  277. /*
  278. */
  279. static struct file_operations snd_hwdep_f_ops =
  280. {
  281. .owner = THIS_MODULE,
  282. .llseek = snd_hwdep_llseek,
  283. .read = snd_hwdep_read,
  284. .write = snd_hwdep_write,
  285. .open = snd_hwdep_open,
  286. .release = snd_hwdep_release,
  287. .poll = snd_hwdep_poll,
  288. .unlocked_ioctl = snd_hwdep_ioctl,
  289. .compat_ioctl = snd_hwdep_ioctl_compat,
  290. .mmap = snd_hwdep_mmap,
  291. };
  292. static snd_minor_t snd_hwdep_reg =
  293. {
  294. .comment = "hardware dependent",
  295. .f_ops = &snd_hwdep_f_ops,
  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(snd_card_t * card, char *id, int device, snd_hwdep_t ** rhwdep)
  311. {
  312. snd_hwdep_t *hwdep;
  313. int err;
  314. static snd_device_ops_t ops = {
  315. .dev_free = snd_hwdep_dev_free,
  316. .dev_register = snd_hwdep_dev_register,
  317. .dev_unregister = snd_hwdep_dev_unregister
  318. };
  319. snd_assert(rhwdep != NULL, return -EINVAL);
  320. *rhwdep = NULL;
  321. snd_assert(card != NULL, return -ENXIO);
  322. hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
  323. if (hwdep == NULL)
  324. return -ENOMEM;
  325. hwdep->card = card;
  326. hwdep->device = device;
  327. if (id) {
  328. strlcpy(hwdep->id, id, sizeof(hwdep->id));
  329. }
  330. #ifdef CONFIG_SND_OSSEMUL
  331. hwdep->oss_type = -1;
  332. #endif
  333. if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
  334. snd_hwdep_free(hwdep);
  335. return err;
  336. }
  337. init_waitqueue_head(&hwdep->open_wait);
  338. init_MUTEX(&hwdep->open_mutex);
  339. *rhwdep = hwdep;
  340. return 0;
  341. }
  342. static int snd_hwdep_free(snd_hwdep_t *hwdep)
  343. {
  344. snd_assert(hwdep != NULL, return -ENXIO);
  345. if (hwdep->private_free)
  346. hwdep->private_free(hwdep);
  347. kfree(hwdep);
  348. return 0;
  349. }
  350. static int snd_hwdep_dev_free(snd_device_t *device)
  351. {
  352. snd_hwdep_t *hwdep = device->device_data;
  353. return snd_hwdep_free(hwdep);
  354. }
  355. static int snd_hwdep_dev_register(snd_device_t *device)
  356. {
  357. snd_hwdep_t *hwdep = device->device_data;
  358. int idx, err;
  359. char name[32];
  360. down(&register_mutex);
  361. idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
  362. if (snd_hwdep_devices[idx]) {
  363. up(&register_mutex);
  364. return -EBUSY;
  365. }
  366. snd_hwdep_devices[idx] = hwdep;
  367. sprintf(name, "hwC%iD%i", hwdep->card->number, hwdep->device);
  368. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
  369. hwdep->card, hwdep->device,
  370. &snd_hwdep_reg, name)) < 0) {
  371. snd_printk(KERN_ERR "unable to register hardware dependent device %i:%i\n",
  372. hwdep->card->number, hwdep->device);
  373. snd_hwdep_devices[idx] = NULL;
  374. up(&register_mutex);
  375. return err;
  376. }
  377. #ifdef CONFIG_SND_OSSEMUL
  378. hwdep->ossreg = 0;
  379. if (hwdep->oss_type >= 0) {
  380. if ((hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM) && (hwdep->device != 0)) {
  381. snd_printk (KERN_WARNING "only hwdep device 0 can be registered as OSS direct FM device!\n");
  382. } else {
  383. if (snd_register_oss_device(hwdep->oss_type,
  384. hwdep->card, hwdep->device,
  385. &snd_hwdep_reg, hwdep->oss_dev) < 0) {
  386. snd_printk(KERN_ERR "unable to register OSS compatibility device %i:%i\n",
  387. hwdep->card->number, hwdep->device);
  388. } else
  389. hwdep->ossreg = 1;
  390. }
  391. }
  392. #endif
  393. up(&register_mutex);
  394. return 0;
  395. }
  396. static int snd_hwdep_dev_unregister(snd_device_t *device)
  397. {
  398. snd_hwdep_t *hwdep = device->device_data;
  399. int idx;
  400. snd_assert(hwdep != NULL, return -ENXIO);
  401. down(&register_mutex);
  402. idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
  403. if (snd_hwdep_devices[idx] != hwdep) {
  404. up(&register_mutex);
  405. return -EINVAL;
  406. }
  407. #ifdef CONFIG_SND_OSSEMUL
  408. if (hwdep->ossreg)
  409. snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
  410. #endif
  411. snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
  412. snd_hwdep_devices[idx] = NULL;
  413. up(&register_mutex);
  414. return snd_hwdep_free(hwdep);
  415. }
  416. /*
  417. * Info interface
  418. */
  419. static void snd_hwdep_proc_read(snd_info_entry_t *entry,
  420. snd_info_buffer_t * buffer)
  421. {
  422. int idx;
  423. snd_hwdep_t *hwdep;
  424. down(&register_mutex);
  425. for (idx = 0; idx < SNDRV_CARDS * SNDRV_MINOR_HWDEPS; idx++) {
  426. hwdep = snd_hwdep_devices[idx];
  427. if (hwdep == NULL)
  428. continue;
  429. snd_iprintf(buffer, "%02i-%02i: %s\n",
  430. idx / SNDRV_MINOR_HWDEPS,
  431. idx % SNDRV_MINOR_HWDEPS,
  432. hwdep->name);
  433. }
  434. up(&register_mutex);
  435. }
  436. /*
  437. * ENTRY functions
  438. */
  439. static snd_info_entry_t *snd_hwdep_proc_entry = NULL;
  440. static int __init alsa_hwdep_init(void)
  441. {
  442. snd_info_entry_t *entry;
  443. memset(snd_hwdep_devices, 0, sizeof(snd_hwdep_devices));
  444. if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
  445. entry->c.text.read_size = 512;
  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. snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
  454. snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl);
  455. return 0;
  456. }
  457. static void __exit alsa_hwdep_exit(void)
  458. {
  459. snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
  460. snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl);
  461. if (snd_hwdep_proc_entry) {
  462. snd_info_unregister(snd_hwdep_proc_entry);
  463. snd_hwdep_proc_entry = NULL;
  464. }
  465. }
  466. module_init(alsa_hwdep_init)
  467. module_exit(alsa_hwdep_exit)
  468. EXPORT_SYMBOL(snd_hwdep_new);