hwdep.c 13 KB

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