usX2Yhwdep.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <sound/driver.h>
  23. #include <linux/interrupt.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. int usX2Y_hwdep_pcm_new(struct snd_card *card);
  33. static struct page * snd_us428ctls_vm_nopage(struct vm_area_struct *area, unsigned long address, int *type)
  34. {
  35. unsigned long offset;
  36. struct page * page;
  37. void *vaddr;
  38. snd_printdd("ENTER, start %lXh, ofs %lXh, pgoff %ld, addr %lXh\n",
  39. area->vm_start,
  40. address - area->vm_start,
  41. (address - area->vm_start) >> PAGE_SHIFT,
  42. address);
  43. offset = area->vm_pgoff << PAGE_SHIFT;
  44. offset += address - area->vm_start;
  45. snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
  46. vaddr = (char*)((struct usX2Ydev *)area->vm_private_data)->us428ctls_sharedmem + offset;
  47. page = virt_to_page(vaddr);
  48. get_page(page);
  49. snd_printdd( "vaddr=%p made us428ctls_vm_nopage() return %p; offset=%lX\n", vaddr, page, offset);
  50. if (type)
  51. *type = VM_FAULT_MINOR;
  52. return page;
  53. }
  54. static struct vm_operations_struct us428ctls_vm_ops = {
  55. .nopage = snd_us428ctls_vm_nopage,
  56. };
  57. static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
  58. {
  59. unsigned long size = (unsigned long)(area->vm_end - area->vm_start);
  60. struct usX2Ydev *us428 = hw->private_data;
  61. // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs?
  62. // so as long as the device isn't fully initialised yet we return -EBUSY here.
  63. if (!(us428->chip_status & USX2Y_STAT_CHIP_INIT))
  64. return -EBUSY;
  65. /* if userspace tries to mmap beyond end of our buffer, fail */
  66. if (size > PAGE_ALIGN(sizeof(struct us428ctls_sharedmem))) {
  67. snd_printd( "%lu > %lu\n", size, (unsigned long)sizeof(struct us428ctls_sharedmem));
  68. return -EINVAL;
  69. }
  70. if (!us428->us428ctls_sharedmem) {
  71. init_waitqueue_head(&us428->us428ctls_wait_queue_head);
  72. if(!(us428->us428ctls_sharedmem = snd_malloc_pages(sizeof(struct us428ctls_sharedmem), GFP_KERNEL)))
  73. return -ENOMEM;
  74. memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
  75. us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
  76. }
  77. area->vm_ops = &us428ctls_vm_ops;
  78. area->vm_flags |= VM_RESERVED;
  79. area->vm_private_data = hw->private_data;
  80. return 0;
  81. }
  82. static unsigned int snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
  83. {
  84. unsigned int mask = 0;
  85. struct usX2Ydev *us428 = hw->private_data;
  86. struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
  87. if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
  88. return POLLHUP;
  89. poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
  90. if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
  91. mask |= POLLIN;
  92. return mask;
  93. }
  94. static int snd_usX2Y_hwdep_open(struct snd_hwdep *hw, struct file *file)
  95. {
  96. return 0;
  97. }
  98. static int snd_usX2Y_hwdep_release(struct snd_hwdep *hw, struct file *file)
  99. {
  100. return 0;
  101. }
  102. static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
  103. struct snd_hwdep_dsp_status *info)
  104. {
  105. static char *type_ids[USX2Y_TYPE_NUMS] = {
  106. [USX2Y_TYPE_122] = "us122",
  107. [USX2Y_TYPE_224] = "us224",
  108. [USX2Y_TYPE_428] = "us428",
  109. };
  110. struct usX2Ydev *us428 = hw->private_data;
  111. int id = -1;
  112. switch (le16_to_cpu(us428->chip.dev->descriptor.idProduct)) {
  113. case USB_ID_US122:
  114. id = USX2Y_TYPE_122;
  115. break;
  116. case USB_ID_US224:
  117. id = USX2Y_TYPE_224;
  118. break;
  119. case USB_ID_US428:
  120. id = USX2Y_TYPE_428;
  121. break;
  122. }
  123. if (0 > id)
  124. return -ENODEV;
  125. strcpy(info->id, type_ids[id]);
  126. info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
  127. if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
  128. info->chip_ready = 1;
  129. info->version = USX2Y_DRIVER_VERSION;
  130. return 0;
  131. }
  132. static int usX2Y_create_usbmidi(struct snd_card *card)
  133. {
  134. static struct snd_usb_midi_endpoint_info quirk_data_1 = {
  135. .out_ep = 0x06,
  136. .in_ep = 0x06,
  137. .out_cables = 0x001,
  138. .in_cables = 0x001
  139. };
  140. static struct snd_usb_audio_quirk quirk_1 = {
  141. .vendor_name = "TASCAM",
  142. .product_name = NAME_ALLCAPS,
  143. .ifnum = 0,
  144. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  145. .data = &quirk_data_1
  146. };
  147. static struct snd_usb_midi_endpoint_info quirk_data_2 = {
  148. .out_ep = 0x06,
  149. .in_ep = 0x06,
  150. .out_cables = 0x003,
  151. .in_cables = 0x003
  152. };
  153. static struct snd_usb_audio_quirk quirk_2 = {
  154. .vendor_name = "TASCAM",
  155. .product_name = "US428",
  156. .ifnum = 0,
  157. .type = QUIRK_MIDI_FIXED_ENDPOINT,
  158. .data = &quirk_data_2
  159. };
  160. struct usb_device *dev = usX2Y(card)->chip.dev;
  161. struct usb_interface *iface = usb_ifnum_to_if(dev, 0);
  162. struct snd_usb_audio_quirk *quirk =
  163. le16_to_cpu(dev->descriptor.idProduct) == USB_ID_US428 ?
  164. &quirk_2 : &quirk_1;
  165. snd_printdd("usX2Y_create_usbmidi \n");
  166. return snd_usb_create_midi_interface(&usX2Y(card)->chip, iface, quirk);
  167. }
  168. static int usX2Y_create_alsa_devices(struct snd_card *card)
  169. {
  170. int err;
  171. do {
  172. if ((err = usX2Y_create_usbmidi(card)) < 0) {
  173. snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
  174. break;
  175. }
  176. if ((err = usX2Y_audio_create(card)) < 0)
  177. break;
  178. if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
  179. break;
  180. if ((err = snd_card_register(card)) < 0)
  181. break;
  182. } while (0);
  183. return err;
  184. }
  185. static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep *hw,
  186. struct snd_hwdep_dsp_image *dsp)
  187. {
  188. struct usX2Ydev *priv = hw->private_data;
  189. int lret, err = -EINVAL;
  190. snd_printdd( "dsp_load %s\n", dsp->name);
  191. if (access_ok(VERIFY_READ, dsp->image, dsp->length)) {
  192. struct usb_device* dev = priv->chip.dev;
  193. char *buf = kmalloc(dsp->length, GFP_KERNEL);
  194. if (!buf)
  195. return -ENOMEM;
  196. if (copy_from_user(buf, dsp->image, dsp->length)) {
  197. kfree(buf);
  198. return -EFAULT;
  199. }
  200. err = usb_set_interface(dev, 0, 1);
  201. if (err)
  202. snd_printk(KERN_ERR "usb_set_interface error \n");
  203. else
  204. err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
  205. kfree(buf);
  206. }
  207. if (err)
  208. return err;
  209. if (dsp->index == 1) {
  210. msleep(250); // give the device some time
  211. err = usX2Y_AsyncSeq04_init(priv);
  212. if (err) {
  213. snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
  214. return err;
  215. }
  216. err = usX2Y_In04_init(priv);
  217. if (err) {
  218. snd_printk(KERN_ERR "usX2Y_In04_init error \n");
  219. return err;
  220. }
  221. err = usX2Y_create_alsa_devices(hw->card);
  222. if (err) {
  223. snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
  224. snd_card_free(hw->card);
  225. return err;
  226. }
  227. priv->chip_status |= USX2Y_STAT_CHIP_INIT;
  228. snd_printdd("%s: alsa all started\n", hw->name);
  229. }
  230. return err;
  231. }
  232. int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
  233. {
  234. int err;
  235. struct snd_hwdep *hw;
  236. if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
  237. return err;
  238. hw->iface = SNDRV_HWDEP_IFACE_USX2Y;
  239. hw->private_data = usX2Y(card);
  240. hw->ops.open = snd_usX2Y_hwdep_open;
  241. hw->ops.release = snd_usX2Y_hwdep_release;
  242. hw->ops.dsp_status = snd_usX2Y_hwdep_dsp_status;
  243. hw->ops.dsp_load = snd_usX2Y_hwdep_dsp_load;
  244. hw->ops.mmap = snd_us428ctls_mmap;
  245. hw->ops.poll = snd_us428ctls_poll;
  246. hw->exclusive = 1;
  247. sprintf(hw->name, "/proc/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
  248. return 0;
  249. }