hwdep.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 struct snd_hwdep *snd_hwdep_devices[SNDRV_CARDS * SNDRV_MINOR_HWDEPS];
  36. static DECLARE_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_unregister(struct snd_device *device);
  41. /*
  42. */
  43. static loff_t snd_hwdep_llseek(struct file * file, loff_t offset, int orig)
  44. {
  45. struct snd_hwdep *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,
  51. size_t count, loff_t *offset)
  52. {
  53. struct snd_hwdep *hw = file->private_data;
  54. if (hw->ops.read)
  55. return hw->ops.read(hw, buf, count, offset);
  56. return -ENXIO;
  57. }
  58. static ssize_t snd_hwdep_write(struct file * file, const char __user *buf,
  59. size_t count, loff_t *offset)
  60. {
  61. struct snd_hwdep *hw = file->private_data;
  62. if (hw->ops.write)
  63. return hw->ops.write(hw, buf, count, offset);
  64. return -ENXIO;
  65. }
  66. static int snd_hwdep_open(struct inode *inode, struct file * file)
  67. {
  68. int major = imajor(inode);
  69. int cardnum;
  70. int device;
  71. struct snd_hwdep *hw;
  72. int err;
  73. wait_queue_t wait;
  74. if (major == snd_major) {
  75. cardnum = SNDRV_MINOR_CARD(iminor(inode));
  76. device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_HWDEP;
  77. #ifdef CONFIG_SND_OSSEMUL
  78. } else if (major == SOUND_MAJOR) {
  79. cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
  80. device = 0;
  81. #endif
  82. } else
  83. return -ENXIO;
  84. cardnum %= SNDRV_CARDS;
  85. device %= SNDRV_MINOR_HWDEPS;
  86. hw = snd_hwdep_devices[(cardnum * SNDRV_MINOR_HWDEPS) + device];
  87. if (hw == NULL)
  88. return -ENODEV;
  89. if (!hw->ops.open)
  90. return -ENXIO;
  91. #ifdef CONFIG_SND_OSSEMUL
  92. if (major == SOUND_MAJOR && hw->oss_type < 0)
  93. return -ENXIO;
  94. #endif
  95. if (!try_module_get(hw->card->module))
  96. return -EFAULT;
  97. init_waitqueue_entry(&wait, current);
  98. add_wait_queue(&hw->open_wait, &wait);
  99. down(&hw->open_mutex);
  100. while (1) {
  101. if (hw->exclusive && hw->used > 0) {
  102. err = -EBUSY;
  103. break;
  104. }
  105. err = hw->ops.open(hw, file);
  106. if (err >= 0)
  107. break;
  108. if (err == -EAGAIN) {
  109. if (file->f_flags & O_NONBLOCK) {
  110. err = -EBUSY;
  111. break;
  112. }
  113. } else
  114. break;
  115. set_current_state(TASK_INTERRUPTIBLE);
  116. up(&hw->open_mutex);
  117. schedule();
  118. down(&hw->open_mutex);
  119. if (signal_pending(current)) {
  120. err = -ERESTARTSYS;
  121. break;
  122. }
  123. }
  124. remove_wait_queue(&hw->open_wait, &wait);
  125. if (err >= 0) {
  126. err = snd_card_file_add(hw->card, file);
  127. if (err >= 0) {
  128. file->private_data = hw;
  129. hw->used++;
  130. } else {
  131. if (hw->ops.release)
  132. hw->ops.release(hw, file);
  133. }
  134. }
  135. up(&hw->open_mutex);
  136. if (err < 0)
  137. module_put(hw->card->module);
  138. return err;
  139. }
  140. static int snd_hwdep_release(struct inode *inode, struct file * file)
  141. {
  142. int err = -ENXIO;
  143. struct snd_hwdep *hw = file->private_data;
  144. down(&hw->open_mutex);
  145. if (hw->ops.release) {
  146. err = hw->ops.release(hw, file);
  147. wake_up(&hw->open_wait);
  148. }
  149. if (hw->used > 0)
  150. hw->used--;
  151. snd_card_file_remove(hw->card, file);
  152. up(&hw->open_mutex);
  153. module_put(hw->card->module);
  154. return err;
  155. }
  156. static unsigned int snd_hwdep_poll(struct file * file, poll_table * wait)
  157. {
  158. struct snd_hwdep *hw = file->private_data;
  159. if (hw->ops.poll)
  160. return hw->ops.poll(hw, file, wait);
  161. return 0;
  162. }
  163. static int snd_hwdep_info(struct snd_hwdep *hw,
  164. struct snd_hwdep_info __user *_info)
  165. {
  166. struct snd_hwdep_info info;
  167. memset(&info, 0, sizeof(info));
  168. info.card = hw->card->number;
  169. strlcpy(info.id, hw->id, sizeof(info.id));
  170. strlcpy(info.name, hw->name, sizeof(info.name));
  171. info.iface = hw->iface;
  172. if (copy_to_user(_info, &info, sizeof(info)))
  173. return -EFAULT;
  174. return 0;
  175. }
  176. static int snd_hwdep_dsp_status(struct snd_hwdep *hw,
  177. struct snd_hwdep_dsp_status __user *_info)
  178. {
  179. struct snd_hwdep_dsp_status 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(struct snd_hwdep *hw,
  192. struct snd_hwdep_dsp_image __user *_info)
  193. {
  194. struct snd_hwdep_dsp_image info;
  195. int err;
  196. if (! hw->ops.dsp_load)
  197. return -ENXIO;
  198. memset(&info, 0, sizeof(info));
  199. if (copy_from_user(&info, _info, sizeof(info)))
  200. return -EFAULT;
  201. /* check whether the dsp was already loaded */
  202. if (hw->dsp_loaded & (1 << info.index))
  203. return -EBUSY;
  204. if (!access_ok(VERIFY_READ, info.image, info.length))
  205. return -EFAULT;
  206. err = hw->ops.dsp_load(hw, &info);
  207. if (err < 0)
  208. return err;
  209. hw->dsp_loaded |= (1 << info.index);
  210. return 0;
  211. }
  212. static long snd_hwdep_ioctl(struct file * file, unsigned int cmd,
  213. unsigned long arg)
  214. {
  215. struct snd_hwdep *hw = file->private_data;
  216. void __user *argp = (void __user *)arg;
  217. switch (cmd) {
  218. case SNDRV_HWDEP_IOCTL_PVERSION:
  219. return put_user(SNDRV_HWDEP_VERSION, (int __user *)argp);
  220. case SNDRV_HWDEP_IOCTL_INFO:
  221. return snd_hwdep_info(hw, argp);
  222. case SNDRV_HWDEP_IOCTL_DSP_STATUS:
  223. return snd_hwdep_dsp_status(hw, argp);
  224. case SNDRV_HWDEP_IOCTL_DSP_LOAD:
  225. return snd_hwdep_dsp_load(hw, argp);
  226. }
  227. if (hw->ops.ioctl)
  228. return hw->ops.ioctl(hw, file, cmd, arg);
  229. return -ENOTTY;
  230. }
  231. static int snd_hwdep_mmap(struct file * file, struct vm_area_struct * vma)
  232. {
  233. struct snd_hwdep *hw = file->private_data;
  234. if (hw->ops.mmap)
  235. return hw->ops.mmap(hw, file, vma);
  236. return -ENXIO;
  237. }
  238. static int snd_hwdep_control_ioctl(struct snd_card *card,
  239. struct snd_ctl_file * control,
  240. unsigned int cmd, unsigned long arg)
  241. {
  242. unsigned int tmp;
  243. tmp = card->number * SNDRV_MINOR_HWDEPS;
  244. switch (cmd) {
  245. case SNDRV_CTL_IOCTL_HWDEP_NEXT_DEVICE:
  246. {
  247. int device;
  248. if (get_user(device, (int __user *)arg))
  249. return -EFAULT;
  250. device = device < 0 ? 0 : device + 1;
  251. while (device < SNDRV_MINOR_HWDEPS) {
  252. if (snd_hwdep_devices[tmp + device])
  253. break;
  254. device++;
  255. }
  256. if (device >= SNDRV_MINOR_HWDEPS)
  257. device = -1;
  258. if (put_user(device, (int __user *)arg))
  259. return -EFAULT;
  260. return 0;
  261. }
  262. case SNDRV_CTL_IOCTL_HWDEP_INFO:
  263. {
  264. struct snd_hwdep_info __user *info = (struct snd_hwdep_info __user *)arg;
  265. int device;
  266. struct snd_hwdep *hwdep;
  267. if (get_user(device, &info->device))
  268. return -EFAULT;
  269. if (device < 0 || device >= SNDRV_MINOR_HWDEPS)
  270. return -ENXIO;
  271. hwdep = snd_hwdep_devices[tmp + device];
  272. if (hwdep == NULL)
  273. return -ENXIO;
  274. return snd_hwdep_info(hwdep, info);
  275. }
  276. }
  277. return -ENOIOCTLCMD;
  278. }
  279. #ifdef CONFIG_COMPAT
  280. #include "hwdep_compat.c"
  281. #else
  282. #define snd_hwdep_ioctl_compat NULL
  283. #endif
  284. /*
  285. */
  286. static struct file_operations snd_hwdep_f_ops =
  287. {
  288. .owner = THIS_MODULE,
  289. .llseek = snd_hwdep_llseek,
  290. .read = snd_hwdep_read,
  291. .write = snd_hwdep_write,
  292. .open = snd_hwdep_open,
  293. .release = snd_hwdep_release,
  294. .poll = snd_hwdep_poll,
  295. .unlocked_ioctl = snd_hwdep_ioctl,
  296. .compat_ioctl = snd_hwdep_ioctl_compat,
  297. .mmap = snd_hwdep_mmap,
  298. };
  299. static struct snd_minor snd_hwdep_reg =
  300. {
  301. .comment = "hardware dependent",
  302. .f_ops = &snd_hwdep_f_ops,
  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_unregister = snd_hwdep_dev_unregister
  326. };
  327. snd_assert(rhwdep != NULL, return -EINVAL);
  328. *rhwdep = NULL;
  329. snd_assert(card != NULL, return -ENXIO);
  330. hwdep = kzalloc(sizeof(*hwdep), GFP_KERNEL);
  331. if (hwdep == NULL)
  332. return -ENOMEM;
  333. hwdep->card = card;
  334. hwdep->device = device;
  335. if (id) {
  336. strlcpy(hwdep->id, id, sizeof(hwdep->id));
  337. }
  338. #ifdef CONFIG_SND_OSSEMUL
  339. hwdep->oss_type = -1;
  340. #endif
  341. if ((err = snd_device_new(card, SNDRV_DEV_HWDEP, hwdep, &ops)) < 0) {
  342. snd_hwdep_free(hwdep);
  343. return err;
  344. }
  345. init_waitqueue_head(&hwdep->open_wait);
  346. init_MUTEX(&hwdep->open_mutex);
  347. *rhwdep = hwdep;
  348. return 0;
  349. }
  350. static int snd_hwdep_free(struct snd_hwdep *hwdep)
  351. {
  352. snd_assert(hwdep != NULL, return -ENXIO);
  353. if (hwdep->private_free)
  354. hwdep->private_free(hwdep);
  355. kfree(hwdep);
  356. return 0;
  357. }
  358. static int snd_hwdep_dev_free(struct snd_device *device)
  359. {
  360. struct snd_hwdep *hwdep = device->device_data;
  361. return snd_hwdep_free(hwdep);
  362. }
  363. static int snd_hwdep_dev_register(struct snd_device *device)
  364. {
  365. struct snd_hwdep *hwdep = device->device_data;
  366. int idx, err;
  367. char name[32];
  368. down(&register_mutex);
  369. idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
  370. if (snd_hwdep_devices[idx]) {
  371. up(&register_mutex);
  372. return -EBUSY;
  373. }
  374. snd_hwdep_devices[idx] = hwdep;
  375. sprintf(name, "hwC%iD%i", hwdep->card->number, hwdep->device);
  376. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_HWDEP,
  377. hwdep->card, hwdep->device,
  378. &snd_hwdep_reg, name)) < 0) {
  379. snd_printk(KERN_ERR "unable to register hardware dependent device %i:%i\n",
  380. hwdep->card->number, hwdep->device);
  381. snd_hwdep_devices[idx] = NULL;
  382. up(&register_mutex);
  383. return err;
  384. }
  385. #ifdef CONFIG_SND_OSSEMUL
  386. hwdep->ossreg = 0;
  387. if (hwdep->oss_type >= 0) {
  388. if ((hwdep->oss_type == SNDRV_OSS_DEVICE_TYPE_DMFM) && (hwdep->device != 0)) {
  389. snd_printk (KERN_WARNING "only hwdep device 0 can be registered as OSS direct FM device!\n");
  390. } else {
  391. if (snd_register_oss_device(hwdep->oss_type,
  392. hwdep->card, hwdep->device,
  393. &snd_hwdep_reg, hwdep->oss_dev) < 0) {
  394. snd_printk(KERN_ERR "unable to register OSS compatibility device %i:%i\n",
  395. hwdep->card->number, hwdep->device);
  396. } else
  397. hwdep->ossreg = 1;
  398. }
  399. }
  400. #endif
  401. up(&register_mutex);
  402. return 0;
  403. }
  404. static int snd_hwdep_dev_unregister(struct snd_device *device)
  405. {
  406. struct snd_hwdep *hwdep = device->device_data;
  407. int idx;
  408. snd_assert(hwdep != NULL, return -ENXIO);
  409. down(&register_mutex);
  410. idx = (hwdep->card->number * SNDRV_MINOR_HWDEPS) + hwdep->device;
  411. if (snd_hwdep_devices[idx] != hwdep) {
  412. up(&register_mutex);
  413. return -EINVAL;
  414. }
  415. #ifdef CONFIG_SND_OSSEMUL
  416. if (hwdep->ossreg)
  417. snd_unregister_oss_device(hwdep->oss_type, hwdep->card, hwdep->device);
  418. #endif
  419. snd_unregister_device(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card, hwdep->device);
  420. snd_hwdep_devices[idx] = NULL;
  421. up(&register_mutex);
  422. return snd_hwdep_free(hwdep);
  423. }
  424. /*
  425. * Info interface
  426. */
  427. static void snd_hwdep_proc_read(struct snd_info_entry *entry,
  428. struct snd_info_buffer *buffer)
  429. {
  430. int idx;
  431. struct snd_hwdep *hwdep;
  432. down(&register_mutex);
  433. for (idx = 0; idx < SNDRV_CARDS * SNDRV_MINOR_HWDEPS; idx++) {
  434. hwdep = snd_hwdep_devices[idx];
  435. if (hwdep == NULL)
  436. continue;
  437. snd_iprintf(buffer, "%02i-%02i: %s\n",
  438. idx / SNDRV_MINOR_HWDEPS,
  439. idx % SNDRV_MINOR_HWDEPS,
  440. hwdep->name);
  441. }
  442. up(&register_mutex);
  443. }
  444. /*
  445. * ENTRY functions
  446. */
  447. static struct snd_info_entry *snd_hwdep_proc_entry = NULL;
  448. static int __init alsa_hwdep_init(void)
  449. {
  450. struct snd_info_entry *entry;
  451. memset(snd_hwdep_devices, 0, sizeof(snd_hwdep_devices));
  452. if ((entry = snd_info_create_module_entry(THIS_MODULE, "hwdep", NULL)) != NULL) {
  453. entry->c.text.read_size = 512;
  454. entry->c.text.read = snd_hwdep_proc_read;
  455. if (snd_info_register(entry) < 0) {
  456. snd_info_free_entry(entry);
  457. entry = NULL;
  458. }
  459. }
  460. snd_hwdep_proc_entry = entry;
  461. snd_ctl_register_ioctl(snd_hwdep_control_ioctl);
  462. snd_ctl_register_ioctl_compat(snd_hwdep_control_ioctl);
  463. return 0;
  464. }
  465. static void __exit alsa_hwdep_exit(void)
  466. {
  467. snd_ctl_unregister_ioctl(snd_hwdep_control_ioctl);
  468. snd_ctl_unregister_ioctl_compat(snd_hwdep_control_ioctl);
  469. if (snd_hwdep_proc_entry) {
  470. snd_info_unregister(snd_hwdep_proc_entry);
  471. snd_hwdep_proc_entry = NULL;
  472. }
  473. }
  474. module_init(alsa_hwdep_init)
  475. module_exit(alsa_hwdep_exit)
  476. EXPORT_SYMBOL(snd_hwdep_new);