1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Tascam US-X2Y USB soundcards
5 * FPGA Loader + ALSA Startup
7 * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de>
10 #include <linux/interrupt.h>
11 #include <linux/slab.h>
12 #include <linux/usb.h>
13 #include <sound/core.h>
14 #include <sound/memalloc.h>
15 #include <sound/pcm.h>
16 #include <sound/hwdep.h>
19 #include "usX2Yhwdep.h"
21 static vm_fault_t
snd_us428ctls_vm_fault(struct vm_fault
*vmf
)
27 snd_printdd("ENTER, start %lXh, pgoff %ld\n",
31 offset
= vmf
->pgoff
<< PAGE_SHIFT
;
32 vaddr
= (char *)((struct usX2Ydev
*)vmf
->vma
->vm_private_data
)->us428ctls_sharedmem
+ offset
;
33 page
= virt_to_page(vaddr
);
37 snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n",
43 static const struct vm_operations_struct us428ctls_vm_ops
= {
44 .fault
= snd_us428ctls_vm_fault
,
47 static int snd_us428ctls_mmap(struct snd_hwdep
* hw
, struct file
*filp
, struct vm_area_struct
*area
)
49 unsigned long size
= (unsigned long)(area
->vm_end
- area
->vm_start
);
50 struct usX2Ydev
*us428
= hw
->private_data
;
52 // FIXME this hwdep interface is used twice: fpga download and mmap for controlling Lights etc. Maybe better using 2 hwdep devs?
53 // so as long as the device isn't fully initialised yet we return -EBUSY here.
54 if (!(us428
->chip_status
& USX2Y_STAT_CHIP_INIT
))
57 /* if userspace tries to mmap beyond end of our buffer, fail */
58 if (size
> PAGE_ALIGN(sizeof(struct us428ctls_sharedmem
))) {
59 snd_printd( "%lu > %lu\n", size
, (unsigned long)sizeof(struct us428ctls_sharedmem
));
63 if (!us428
->us428ctls_sharedmem
) {
64 init_waitqueue_head(&us428
->us428ctls_wait_queue_head
);
65 us428
->us428ctls_sharedmem
= alloc_pages_exact(sizeof(struct us428ctls_sharedmem
), GFP_KERNEL
);
66 if (!us428
->us428ctls_sharedmem
)
68 memset(us428
->us428ctls_sharedmem
, -1, sizeof(struct us428ctls_sharedmem
));
69 us428
->us428ctls_sharedmem
->CtlSnapShotLast
= -2;
71 area
->vm_ops
= &us428ctls_vm_ops
;
72 area
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
73 area
->vm_private_data
= hw
->private_data
;
77 static __poll_t
snd_us428ctls_poll(struct snd_hwdep
*hw
, struct file
*file
, poll_table
*wait
)
80 struct usX2Ydev
*us428
= hw
->private_data
;
81 struct us428ctls_sharedmem
*shm
= us428
->us428ctls_sharedmem
;
82 if (us428
->chip_status
& USX2Y_STAT_CHIP_HUP
)
85 poll_wait(file
, &us428
->us428ctls_wait_queue_head
, wait
);
87 if (shm
!= NULL
&& shm
->CtlSnapShotLast
!= shm
->CtlSnapShotRed
)
94 static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep
*hw
,
95 struct snd_hwdep_dsp_status
*info
)
97 static const char * const type_ids
[USX2Y_TYPE_NUMS
] = {
98 [USX2Y_TYPE_122
] = "us122",
99 [USX2Y_TYPE_224
] = "us224",
100 [USX2Y_TYPE_428
] = "us428",
102 struct usX2Ydev
*us428
= hw
->private_data
;
105 switch (le16_to_cpu(us428
->dev
->descriptor
.idProduct
)) {
118 strcpy(info
->id
, type_ids
[id
]);
119 info
->num_dsps
= 2; // 0: Prepad Data, 1: FPGA Code
120 if (us428
->chip_status
& USX2Y_STAT_CHIP_INIT
)
121 info
->chip_ready
= 1;
122 info
->version
= USX2Y_DRIVER_VERSION
;
127 static int usX2Y_create_usbmidi(struct snd_card
*card
)
129 static const struct snd_usb_midi_endpoint_info quirk_data_1
= {
135 static const struct snd_usb_audio_quirk quirk_1
= {
136 .vendor_name
= "TASCAM",
137 .product_name
= NAME_ALLCAPS
,
139 .type
= QUIRK_MIDI_FIXED_ENDPOINT
,
140 .data
= &quirk_data_1
142 static const struct snd_usb_midi_endpoint_info quirk_data_2
= {
148 static const struct snd_usb_audio_quirk quirk_2
= {
149 .vendor_name
= "TASCAM",
150 .product_name
= "US428",
152 .type
= QUIRK_MIDI_FIXED_ENDPOINT
,
153 .data
= &quirk_data_2
155 struct usb_device
*dev
= usX2Y(card
)->dev
;
156 struct usb_interface
*iface
= usb_ifnum_to_if(dev
, 0);
157 const struct snd_usb_audio_quirk
*quirk
=
158 le16_to_cpu(dev
->descriptor
.idProduct
) == USB_ID_US428
?
161 snd_printdd("usX2Y_create_usbmidi \n");
162 return snd_usbmidi_create(card
, iface
, &usX2Y(card
)->midi_list
, quirk
);
165 static int usX2Y_create_alsa_devices(struct snd_card
*card
)
170 if ((err
= usX2Y_create_usbmidi(card
)) < 0) {
171 snd_printk(KERN_ERR
"usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err
);
174 if ((err
= usX2Y_audio_create(card
)) < 0)
176 if ((err
= usX2Y_hwdep_pcm_new(card
)) < 0)
178 if ((err
= snd_card_register(card
)) < 0)
185 static int snd_usX2Y_hwdep_dsp_load(struct snd_hwdep
*hw
,
186 struct snd_hwdep_dsp_image
*dsp
)
188 struct usX2Ydev
*priv
= hw
->private_data
;
189 struct usb_device
* dev
= priv
->dev
;
193 snd_printdd( "dsp_load %s\n", dsp
->name
);
195 buf
= memdup_user(dsp
->image
, dsp
->length
);
199 err
= usb_set_interface(dev
, 0, 1);
201 snd_printk(KERN_ERR
"usb_set_interface error \n");
203 err
= usb_bulk_msg(dev
, usb_sndbulkpipe(dev
, 2), buf
, dsp
->length
, &lret
, 6000);
207 if (dsp
->index
== 1) {
208 msleep(250); // give the device some time
209 err
= usX2Y_AsyncSeq04_init(priv
);
211 snd_printk(KERN_ERR
"usX2Y_AsyncSeq04_init error \n");
214 err
= usX2Y_In04_init(priv
);
216 snd_printk(KERN_ERR
"usX2Y_In04_init error \n");
219 err
= usX2Y_create_alsa_devices(hw
->card
);
221 snd_printk(KERN_ERR
"usX2Y_create_alsa_devices error %i \n", err
);
222 snd_card_free(hw
->card
);
225 priv
->chip_status
|= USX2Y_STAT_CHIP_INIT
;
226 snd_printdd("%s: alsa all started\n", hw
->name
);
232 int usX2Y_hwdep_new(struct snd_card
*card
, struct usb_device
* device
)
235 struct snd_hwdep
*hw
;
237 if ((err
= snd_hwdep_new(card
, SND_USX2Y_LOADER_ID
, 0, &hw
)) < 0)
240 hw
->iface
= SNDRV_HWDEP_IFACE_USX2Y
;
241 hw
->private_data
= usX2Y(card
);
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
;
247 sprintf(hw
->name
, "/dev/bus/usb/%03d/%03d", device
->bus
->busnum
, device
->devnum
);