usX2Yhwdep.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Driver for Tascam US-X2Y USB soundcards
  3. *
  4. * FPGA Loader + ALSA Startup
  5. *
  6. * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/interrupt.h>
  23. #include <linux/usb.h>
  24. #include <sound/core.h>
  25. #include <sound/memalloc.h>
  26. #include <sound/pcm.h>
  27. #include <sound/hwdep.h>
  28. #include "usx2y.h"
  29. #include "usbusx2y.h"
  30. #include "usX2Yhwdep.h"
  31. int usX2Y_hwdep_pcm_new(struct snd_card *card);
  32. static int snd_us428ctls_vm_fault(struct vm_area_struct *area,
  33. struct vm_fault *vmf)
  34. {
  35. unsigned long offset;
  36. struct page * page;
  37. void *vaddr;
  38. snd_printdd("ENTER, start %lXh, pgoff %ld\n",
  39. area->vm_start,
  40. vmf->pgoff);
  41. offset = vmf->pgoff << PAGE_SHIFT;
  42. vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset;
  43. page = virt_to_page(vaddr);
  44. get_page(page);
  45. vmf->page = page;
  46. snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n",
  47. vaddr, page);
  48. return 0;
  49. }
  50. static struct vm_operations_struct us428ctls_vm_ops = {
  51. .fault = snd_us428ctls_vm_fault,
  52. };
  53. static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
  54. {
  55. unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
  56. struct usX2Ydev *us428 = hw->private_data;
  57. // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs?
  58. // so as long as the device isn't fully initialised yet we return -EBUSY here.
  59. if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
  60. return -EBUSY;
  61. /* if userspace tries to mmap beyond end of our buffer, fail */
  62. if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
  63. snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem));
  64. return -EINVAL;
  65. }
  66. if (!us428->us428ctls_sharedmem) {
  67. init_waitqueue_head(&us428->us428ctls_wait_queue_head);
  68. if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
  69. return -ENOMEM;
  70. memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
  71. us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
  72. }
  73. area->vm_ops = &us428ctls_vm_ops;
  74. area->vm_flags |= VM_RESERVED | VM_DONTEXPAND;
  75. area->vm_private_data = hw->private_data;
  76. return 0;
  77. }
  78. static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
  79. {
  80. unsigned int mask = 0;
  81. struct usX2Ydev *us428 = hw->private_data;
  82. struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
  83. if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
  84. return POLLHUP;
  85. poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
  86. if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
  87. mask |= POLLIN;
  88. return mask;
  89. }
  90. static int snd_usX2Y_hwdep_open(struct snd_hwdep *hw, struct file *file)
  91. {
  92. return 0;
  93. }
  94. static int snd_usX2Y_hwdep_release(struct snd_hwdep *hw, struct file *file)
  95. {
  96. return 0;
  97. }
  98. static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
  99. struct snd_hwdep_dsp_status *info)
  100. {
  101. static char *type_ids[USX2Y_TYPE_NUMS] = {
  102. [USX2Y_TYPE_122] = "us122",
  103. [USX2Y_TYPE_224] = "us224",
  104. [USX2Y_TYPE_428] = "us428",
  105. };
  106. struct usX2Ydev *us428 = hw->private_data;
  107. int id = -1;
  108. switch (le16_to_cpu(us428->chip.dev->descriptor.idProduct)) {
  109. case USB_ID_US122:
  110. id = USX2Y_TYPE_122;
  111. break;
  112. case USB_ID_US224:
  113. id = USX2Y_TYPE_224;
  114. break;
  115. case USB_ID_US428:
  116. id = USX2Y_TYPE_428;
  117. break;
  118. }
  119. if (0 > id)
  120. return -ENODEV;
  121. strcpy(info->id, type_ids[id]);
  122. info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
  123. if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
  124. info->chip_ready = 1;
  125. info->version = USX2Y_DRIVER_VERSION;
  126. return 0;
  127. }
  128. static int usX2Y_create_usbmidi(struct snd_card *card)
  129. {
  130. static struct snd_usb_midi_endpoint_info quirk_data_1 = {
  131. .out_ep = 0x06,
  132. .in_ep = 0x06,
  133. .out_cables = 0x001,
  134. .in_cables = 0x001
  135. };
  136. static struct snd_usb_audio_quirk quirk_1 = {
  137. .vendor_name = "TASCAM",
  138. .product_name = NAME_ALLCAPS,
  139. .ifnum = 0,
  140. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  141. .data = &quirk_data_1
  142. };
  143. static struct snd_usb_midi_endpoint_info quirk_data_2 = {
  144. .out_ep = 0x06,
  145. .in_ep = 0x06,
  146. .out_cables = 0x003,
  147. .in_cables = 0x003
  148. };
  149. static struct snd_usb_audio_quirk quirk_2 = {
  150. .vendor_name = "TASCAM",
  151. .product_name = "US428",
  152. .ifnum = 0,
  153. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  154. .data = &quirk_data_2
  155. };
  156. struct usb_device *dev = usX2Y(card)->chip.dev;
  157. struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
  158. struct snd_usb_audio_quirk *quirk =
  159. le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
  160. &quirk_2 : &quirk_1;
  161. snd_printdd("usX2Y_create_usbmidi \n");
  162. return snd_usb_create_midi_interface(&usX2Y(card)->chip, iface, quirk);
  163. }
  164. static int usX2Y_create_alsa_devices(struct snd_card *card)
  165. {
  166. int err;
  167. do {
  168. if ((err = usX2Y_create_usbmidi(card)) < 0) {
  169. snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
  170. break;
  171. }
  172. if ((err = usX2Y_audio_create(card)) < 0)
  173. break;
  174. if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
  175. break;
  176. if ((err = snd_card_register(card)) < 0)
  177. break;
  178. } while (0);
  179. return err;
  180. }
  181. static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
  182. struct snd_hwdep_dsp_image *dsp)
  183. {
  184. struct usX2Ydev *priv = hw->private_data;
  185. int lret, err = -EINVAL;
  186. snd_printdd( "dsp_load %s\n", dsp->name);
  187. if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
  188. struct usb_device* dev = priv->chip.dev;
  189. char *buf = kmalloc(dsp->length, GFP_KERNEL);
  190. if (!buf)
  191. return -ENOMEM;
  192. if (copy_from_user(buf, dsp->image, dsp->length)) {
  193. kfree(buf);
  194. return -EFAULT;
  195. }
  196. err = usb_set_interface(dev, 0, 1);
  197. if (err)
  198. snd_printk(KERN_ERR "usb_set_interface error \n");
  199. else
  200. err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
  201. kfree(buf);
  202. }
  203. if (err)
  204. return err;
  205. if (dsp->index == 1) {
  206. msleep(250); // give the device some time
  207. err = usX2Y_AsyncSeq04_init(priv);
  208. if (err) {
  209. snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
  210. return err;
  211. }
  212. err = usX2Y_In04_init(priv);
  213. if (err) {
  214. snd_printk(KERN_ERR "usX2Y_In04_init error \n");
  215. return err;
  216. }
  217. err = usX2Y_create_alsa_devices(hw->card);
  218. if (err) {
  219. snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
  220. snd_card_free(hw->card);
  221. return err;
  222. }
  223. priv->chip_status |= USX2Y_STAT_CHIP_INIT;
  224. snd_printdd("%s: alsa all started\n", hw->name);
  225. }
  226. return err;
  227. }
  228. int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
  229. {
  230. int err;
  231. struct snd_hwdep *hw;
  232. if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
  233. return err;
  234. hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
  235. hw->private_data = usX2Y(card);
  236. hw->ops.open = snd_usX2Y_hwdep_open;
  237. hw->ops.release = snd_usX2Y_hwdep_release;
  238. hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
  239. hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
  240. hw->ops.mmap = snd_us428ctls_mmap;
  241. hw->ops.poll = snd_us428ctls_poll;
  242. hw->exclusive = 1;
  243. sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
  244. return 0;
  245. }