Merge tag 'devicetree-for-5.6' of git://git.kernel.org/pub/scm/linux/kernel/git/robh...
[linux-2.6-microblaze.git] / sound / usb / usx2y / usX2Yhwdep.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Tascam US-X2Y USB soundcards
4  *
5  * FPGA Loader + ALSA Startup
6  *
7  * Copyright (c) 2003 by Karsten Wiese <annabellesgarden@yahoo.de>
8  */
9
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>
17 #include "usx2y.h"
18 #include "usbusx2y.h"
19 #include "usX2Yhwdep.h"
20
21 static vm_fault_t snd_us428ctls_vm_fault(struct vm_fault *vmf)
22 {
23         unsigned long offset;
24         struct page * page;
25         void *vaddr;
26
27         snd_printdd("ENTER, start %lXh, pgoff %ld\n",
28                    vmf->vma->vm_start,
29                    vmf->pgoff);
30         
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);
34         get_page(page);
35         vmf->page = page;
36
37         snd_printdd("vaddr=%p made us428ctls_vm_fault() page %p\n",
38                     vaddr, page);
39
40         return 0;
41 }
42
43 static const struct vm_operations_struct us428ctls_vm_ops = {
44         .fault = snd_us428ctls_vm_fault,
45 };
46
47 static int snd_us428ctls_mmap(struct snd_hwdep * hw, struct file *filp, struct vm_area_struct *area)
48 {
49         unsigned long   size = (unsigned long)(area->vm_end - area->vm_start);
50         struct usX2Ydev *us428 = hw->private_data;
51
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))
55                 return -EBUSY;
56
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)); 
60                 return -EINVAL;
61         }
62
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)
67                         return -ENOMEM;
68                 memset(us428->us428ctls_sharedmem, -1, sizeof(struct us428ctls_sharedmem));
69                 us428->us428ctls_sharedmem->CtlSnapShotLast = -2;
70         }
71         area->vm_ops = &us428ctls_vm_ops;
72         area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
73         area->vm_private_data = hw->private_data;
74         return 0;
75 }
76
77 static __poll_t snd_us428ctls_poll(struct snd_hwdep *hw, struct file *file, poll_table *wait)
78 {
79         __poll_t        mask = 0;
80         struct usX2Ydev *us428 = hw->private_data;
81         struct us428ctls_sharedmem *shm = us428->us428ctls_sharedmem;
82         if (us428->chip_status & USX2Y_STAT_CHIP_HUP)
83                 return EPOLLHUP;
84
85         poll_wait(file, &us428->us428ctls_wait_queue_head, wait);
86
87         if (shm != NULL && shm->CtlSnapShotLast != shm->CtlSnapShotRed)
88                 mask |= EPOLLIN;
89
90         return mask;
91 }
92
93
94 static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
95                                       struct snd_hwdep_dsp_status *info)
96 {
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",
101         };
102         struct usX2Ydev *us428 = hw->private_data;
103         int id = -1;
104
105         switch (le16_to_cpu(us428->dev->descriptor.idProduct)) {
106         case USB_ID_US122:
107                 id = USX2Y_TYPE_122;
108                 break;
109         case USB_ID_US224:
110                 id = USX2Y_TYPE_224;
111                 break;
112         case USB_ID_US428:
113                 id = USX2Y_TYPE_428;
114                 break;
115         }
116         if (0 > id)
117                 return -ENODEV;
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;
123         return 0;
124 }
125
126
127 static int usX2Y_create_usbmidi(struct snd_card *card)
128 {
129         static const struct snd_usb_midi_endpoint_info quirk_data_1 = {
130                 .out_ep = 0x06,
131                 .in_ep = 0x06,
132                 .out_cables =   0x001,
133                 .in_cables =    0x001
134         };
135         static const struct snd_usb_audio_quirk quirk_1 = {
136                 .vendor_name =  "TASCAM",
137                 .product_name = NAME_ALLCAPS,
138                 .ifnum =        0,
139                 .type = QUIRK_MIDI_FIXED_ENDPOINT,
140                 .data = &quirk_data_1
141         };
142         static const struct snd_usb_midi_endpoint_info quirk_data_2 = {
143                 .out_ep = 0x06,
144                 .in_ep = 0x06,
145                 .out_cables =   0x003,
146                 .in_cables =    0x003
147         };
148         static const struct snd_usb_audio_quirk quirk_2 = {
149                 .vendor_name =  "TASCAM",
150                 .product_name = "US428",
151                 .ifnum =        0,
152                 .type = QUIRK_MIDI_FIXED_ENDPOINT,
153                 .data = &quirk_data_2
154         };
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 ?
159                 &quirk_2 : &quirk_1;
160
161         snd_printdd("usX2Y_create_usbmidi \n");
162         return snd_usbmidi_create(card, iface, &usX2Y(card)->midi_list, quirk);
163 }
164
165 static int usX2Y_create_alsa_devices(struct snd_card *card)
166 {
167         int err;
168
169         do {
170                 if ((err = usX2Y_create_usbmidi(card)) < 0) {
171                         snd_printk(KERN_ERR "usX2Y_create_alsa_devices: usX2Y_create_usbmidi error %i \n", err);
172                         break;
173                 }
174                 if ((err = usX2Y_audio_create(card)) < 0) 
175                         break;
176                 if ((err = usX2Y_hwdep_pcm_new(card)) < 0)
177                         break;
178                 if ((err = snd_card_register(card)) < 0)
179                         break;
180         } while (0);
181
182         return err;
183
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         struct usb_device* dev = priv->dev;
190         int lret, err;
191         char *buf;
192
193         snd_printdd( "dsp_load %s\n", dsp->name);
194
195         buf = memdup_user(dsp->image, dsp->length);
196         if (IS_ERR(buf))
197                 return PTR_ERR(buf);
198
199         err = usb_set_interface(dev, 0, 1);
200         if (err)
201                 snd_printk(KERN_ERR "usb_set_interface error \n");
202         else
203                 err = usb_bulk_msg(dev, usb_sndbulkpipe(dev, 2), buf, dsp->length, &lret, 6000);
204         kfree(buf);
205         if (err)
206                 return err;
207         if (dsp->index == 1) {
208                 msleep(250);                            // give the device some time
209                 err = usX2Y_AsyncSeq04_init(priv);
210                 if (err) {
211                         snd_printk(KERN_ERR "usX2Y_AsyncSeq04_init error \n");
212                         return err;
213                 }
214                 err = usX2Y_In04_init(priv);
215                 if (err) {
216                         snd_printk(KERN_ERR "usX2Y_In04_init error \n");
217                         return err;
218                 }
219                 err = usX2Y_create_alsa_devices(hw->card);
220                 if (err) {
221                         snd_printk(KERN_ERR "usX2Y_create_alsa_devices error %i \n", err);
222                         snd_card_free(hw->card);
223                         return err;
224                 }
225                 priv->chip_status |= USX2Y_STAT_CHIP_INIT; 
226                 snd_printdd("%s: alsa all started\n", hw->name);
227         }
228         return err;
229 }
230
231
232 int usX2Y_hwdep_new(struct snd_card *card, struct usb_device* device)
233 {
234         int err;
235         struct snd_hwdep *hw;
236
237         if ((err = snd_hwdep_new(card, SND_USX2Y_LOADER_ID, 0, &hw)) < 0)
238                 return err;
239
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;
246         hw->exclusive = 1;
247         sprintf(hw->name, "/dev/bus/usb/%03d/%03d", device->bus->busnum, device->devnum);
248         return 0;
249 }
250