usX2Yhwdep.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/slab.h>
  24. #include <linux/usb.h>
  25. #include <sound/core.h>
  26. #include <sound/memalloc.h>
  27. #include <sound/pcm.h>
  28. #include <sound/hwdep.h>
  29. #include "usx2y.h"
  30. #include "usbusx2y.h"
  31. #include "usX2Yhwdep.h"
  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 const 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_dsp_status(struct snd_hwdep *hw,
  91. struct snd_hwdep_dsp_status *info)
  92. {
  93. static char *type_ids[USX2Y_TYPE_NUMS] = {
  94. [USX2Y_TYPE_122] = "us122",
  95. [USX2Y_TYPE_224] = "us224",
  96. [USX2Y_TYPE_428] = "us428",
  97. };
  98. struct usX2Ydev *us428 = hw->private_data;
  99. int id = -1;
  100. switch (le16_to_cpu(us428->dev->descriptor.idProduct)) {
  101. case USB_ID_US122:
  102. id = USX2Y_TYPE_122;
  103. break;
  104. case USB_ID_US224:
  105. id = USX2Y_TYPE_224;
  106. break;
  107. case USB_ID_US428:
  108. id = USX2Y_TYPE_428;
  109. break;
  110. }
  111. if (0 > id)
  112. return -ENODEV;
  113. strcpy(info->id, type_ids[id]);
  114. info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
  115. if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
  116. info->chip_ready = 1;
  117. info->version = USX2Y_DRIVER_VERSION;
  118. return 0;
  119. }
  120. static int usX2Y_create_usbmidi(struct snd_card *card)
  121. {
  122. static struct snd_usb_midi_endpoint_info quirk_data_1 = {
  123. .out_ep = 0x06,
  124. .in_ep = 0x06,
  125. .out_cables = 0x001,
  126. .in_cables = 0x001
  127. };
  128. static struct snd_usb_audio_quirk quirk_1 = {
  129. .vendor_name = "TASCAM",
  130. .product_name = NAME_ALLCAPS,
  131. .ifnum = 0,
  132. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  133. .data = &quirk_data_1
  134. };
  135. static struct snd_usb_midi_endpoint_info quirk_data_2 = {
  136. .out_ep = 0x06,
  137. .in_ep = 0x06,
  138. .out_cables = 0x003,
  139. .in_cables = 0x003
  140. };
  141. static struct snd_usb_audio_quirk quirk_2 = {
  142. .vendor_name = "TASCAM",
  143. .product_name = "US428",
  144. .ifnum = 0,
  145. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  146. .data = &quirk_data_2
  147. };
  148. struct usb_device *dev = usX2Y(card)->dev;
  149. struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
  150. struct snd_usb_audio_quirk *quirk =
  151. le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
  152. &quirk_2 : &quirk_1;
  153. snd_printdd("usX2Y_create_usbmidi \n");
  154. return snd_usbmidi_create(card, iface, &usX2Y(card)->midi_list, quirk);
  155. }
  156. static int usX2Y_create_alsa_devices(struct snd_card *card)
  157. {
  158. int err;
  159. do {
  160. if ((err = usX2Y_create_usbmidi(card)) < 0) {
  161. snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
  162. break;
  163. }
  164. if ((err = usX2Y_audio_create(card)) < 0)
  165. break;
  166. if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
  167. break;
  168. if ((err = snd_card_register(card)) < 0)
  169. break;
  170. } while (0);
  171. return err;
  172. }
  173. static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
  174. struct snd_hwdep_dsp_image *dsp)
  175. {
  176. struct usX2Ydev *priv = hw->private_data;
  177. int lret, err = -EINVAL;
  178. snd_printdd( "dsp_load %s\n", dsp->name);
  179. if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
  180. struct usb_device* dev = priv->dev;
  181. char *buf;
  182. buf = memdup_user(dsp->image, dsp->length);
  183. if (IS_ERR(buf))
  184. return PTR_ERR(buf);
  185. err = usb_set_interface(dev, 0, 1);
  186. if (err)
  187. snd_printk(KERN_ERR "usb_set_interface error \n");
  188. else
  189. err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
  190. kfree(buf);
  191. }
  192. if (err)
  193. return err;
  194. if (dsp->index == 1) {
  195. msleep(250); // give the device some time
  196. err = usX2Y_AsyncSeq04_init(priv);
  197. if (err) {
  198. snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
  199. return err;
  200. }
  201. err = usX2Y_In04_init(priv);
  202. if (err) {
  203. snd_printk(KERN_ERR "usX2Y_In04_init error \n");
  204. return err;
  205. }
  206. err = usX2Y_create_alsa_devices(hw->card);
  207. if (err) {
  208. snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
  209. snd_card_free(hw->card);
  210. return err;
  211. }
  212. priv->chip_status |= USX2Y_STAT_CHIP_INIT;
  213. snd_printdd("%s: alsa all started\n", hw->name);
  214. }
  215. return err;
  216. }
  217. int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
  218. {
  219. int err;
  220. struct snd_hwdep *hw;
  221. if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
  222. return err;
  223. hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
  224. hw->private_data = usX2Y(card);
  225. hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
  226. hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
  227. hw->ops.mmap = snd_us428ctls_mmap;
  228. hw->ops.poll = snd_us428ctls_poll;
  229. hw->exclusive = 1;
  230. sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
  231. return 0;
  232. }