Staging: line6: remove KERNEL_VERSION checks
[firefly-linux-kernel-4.4.55.git] / drivers / staging / line6 / pcm.c
1 /*
2  * Line6 Linux USB driver - 0.8.0
3  *
4  * Copyright (C) 2004-2009 Markus Grabner (grabner@icg.tugraz.at)
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License as
8  *      published by the Free Software Foundation, version 2.
9  *
10  */
11
12 #include "driver.h"
13
14 #include <sound/core.h>
15 #include <sound/control.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18
19 #include "audio.h"
20 #include "capture.h"
21 #include "playback.h"
22 #include "pod.h"
23
24
25 /* trigger callback */
26 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
27 {
28         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
29         struct snd_pcm_substream *s;
30         int err;
31         unsigned long flags;
32
33         spin_lock_irqsave(&line6pcm->lock_trigger, flags);
34         clear_bit(BIT_PREPARED, &line6pcm->flags);
35
36         snd_pcm_group_for_each_entry(s, substream) {
37                 switch(s->stream) {
38                 case SNDRV_PCM_STREAM_PLAYBACK:
39                         err = snd_line6_playback_trigger(s, cmd);
40
41                         if(err < 0) {
42                                 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
43                                 return err;
44                         }
45
46                         break;
47
48                 case SNDRV_PCM_STREAM_CAPTURE:
49                         err = snd_line6_capture_trigger(s, cmd);
50
51                         if(err < 0) {
52                                 spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
53                                 return err;
54                         }
55
56                         break;
57
58                 default:
59                         dev_err(s2m(substream), "Unknown stream direction %d\n", s->stream);
60                 }
61         }
62
63         spin_unlock_irqrestore(&line6pcm->lock_trigger, flags);
64         return 0;
65 }
66
67 /* control info callback */
68 static int snd_line6_control_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) {
69         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
70         uinfo->count = 2;
71         uinfo->value.integer.min = 0;
72         uinfo->value.integer.max = 256;
73         return 0;
74 }
75
76 /* control get callback */
77 static int snd_line6_control_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) {
78         int i;
79         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
80
81         for(i = 2; i--;)
82                 ucontrol->value.integer.value[i] = line6pcm->volume[i];
83
84         return 0;
85 }
86
87 /* control put callback */
88 static int snd_line6_control_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) {
89         int i, changed = 0;
90         struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
91
92         for(i = 2; i--;)
93                 if(line6pcm->volume[i] != ucontrol->value.integer.value[i]) {
94                         line6pcm->volume[i] = ucontrol->value.integer.value[i];
95                         changed = 1;
96                 }
97
98         return changed;
99 }
100
101 /* control definition */
102 static struct snd_kcontrol_new line6_control = {
103         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
104         .name = "PCM Playback Volume",
105         .index = 0,
106         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
107         .info = snd_line6_control_info,
108         .get = snd_line6_control_get,
109         .put = snd_line6_control_put
110 };
111
112 /*
113         Cleanup the PCM device.
114 */
115 static void line6_cleanup_pcm(struct snd_pcm *pcm)
116 {
117         int i;
118         struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
119
120         for(i = LINE6_ISO_BUFFERS; i--;) {
121                 if(line6pcm->urb_audio_out[i]) {
122                         usb_kill_urb(line6pcm->urb_audio_out[i]);
123                         usb_free_urb(line6pcm->urb_audio_out[i]);
124                 }
125                 if(line6pcm->urb_audio_in[i]) {
126                         usb_kill_urb(line6pcm->urb_audio_in[i]);
127                         usb_free_urb(line6pcm->urb_audio_in[i]);
128                 }
129         }
130 }
131
132 /* create a PCM device */
133 static int snd_line6_new_pcm(struct snd_line6_pcm *line6pcm)
134 {
135         struct snd_pcm *pcm;
136         int err;
137
138         if((err = snd_pcm_new(line6pcm->line6->card, (char *)line6pcm->line6->properties->name, 0, 1, 1, &pcm)) < 0)
139                 return err;
140
141         pcm->private_data = line6pcm;
142         pcm->private_free = line6_cleanup_pcm;
143         line6pcm->pcm = pcm;
144         strcpy(pcm->name, line6pcm->line6->properties->name);
145
146         /* set operators */
147         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_line6_playback_ops);
148         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
149
150         /* pre-allocation of buffers */
151         snd_pcm_lib_preallocate_pages_for_all(pcm,
152                                                                                                                                                                 SNDRV_DMA_TYPE_CONTINUOUS,
153                                                                                                                                                                 snd_dma_continuous_data(GFP_KERNEL),
154                                                                                                                                                                 64 * 1024, 128 * 1024);
155
156         return 0;
157 }
158
159 /* PCM device destructor */
160 static int snd_line6_pcm_free(struct snd_device *device)
161 {
162         return 0;
163 }
164
165 /*
166         Create and register the PCM device and mixer entries.
167         Create URBs for playback and capture.
168 */
169 int line6_init_pcm(struct usb_line6 *line6, struct line6_pcm_properties *properties)
170 {
171         static struct snd_device_ops pcm_ops = {
172                 .dev_free = snd_line6_pcm_free,
173         };
174
175         int err;
176         int ep_read = 0, ep_write = 0;
177         struct snd_line6_pcm *line6pcm;
178
179         if(!(line6->properties->capabilities & LINE6_BIT_PCM))
180                 return 0;  /* skip PCM initialization and report success */
181
182         /* initialize PCM subsystem based on product id: */
183         switch(line6->product) {
184         case LINE6_DEVID_BASSPODXT:
185   case LINE6_DEVID_BASSPODXTLIVE:
186   case LINE6_DEVID_BASSPODXTPRO:
187   case LINE6_DEVID_PODXT:
188   case LINE6_DEVID_PODXTLIVE:
189   case LINE6_DEVID_PODXTPRO:
190                 ep_read  = 0x82;
191                 ep_write = 0x01;
192                 break;
193
194   case LINE6_DEVID_PODX3:
195   case LINE6_DEVID_PODX3LIVE:
196                 ep_read  = 0x86;
197                 ep_write = 0x02;
198                 break;
199
200   case LINE6_DEVID_POCKETPOD:
201                 ep_read  = 0x82;
202                 ep_write = 0x02;
203                 break;
204
205     case LINE6_DEVID_GUITARPORT:
206         case LINE6_DEVID_TONEPORT_GX:
207                 ep_read  = 0x82;
208                 ep_write = 0x01;
209                 break;
210
211         case LINE6_DEVID_TONEPORT_UX1:
212                 ep_read  = 0x00;
213                 ep_write = 0x00;
214                 break;
215
216         case LINE6_DEVID_TONEPORT_UX2:
217                 ep_read  = 0x87;
218                 ep_write = 0x00;
219                 break;
220
221         default:
222                 MISSING_CASE;
223         }
224
225         line6pcm = kzalloc(sizeof(struct snd_line6_pcm), GFP_KERNEL);
226
227         if(line6pcm == NULL)
228                 return -ENOMEM;
229
230         line6pcm->volume[0] = line6pcm->volume[1] = 128;
231         line6pcm->line6 = line6;
232         line6pcm->ep_audio_read = ep_read;
233         line6pcm->ep_audio_write = ep_write;
234         line6pcm->max_packet_size = usb_maxpacket(line6->usbdev, usb_rcvintpipe(line6->usbdev, ep_read), 0);
235         line6pcm->properties = properties;
236         line6->line6pcm = line6pcm;
237
238         /* PCM device: */
239         if((err = snd_device_new(line6->card, SNDRV_DEV_PCM, line6, &pcm_ops)) < 0)
240                 return err;
241
242         snd_card_set_dev(line6->card, line6->ifcdev);
243
244         if((err = snd_line6_new_pcm(line6pcm)) < 0)
245                 return err;
246
247         spin_lock_init(&line6pcm->lock_audio_out);
248         spin_lock_init(&line6pcm->lock_audio_in);
249         spin_lock_init(&line6pcm->lock_trigger);
250
251         if((err = create_audio_out_urbs(line6pcm)) < 0)
252                 return err;
253
254         if((err = create_audio_in_urbs(line6pcm)) < 0)
255                 return err;
256
257         /* mixer: */
258         if((err = snd_ctl_add(line6->card, snd_ctl_new1(&line6_control, line6pcm))) < 0)
259                 return err;
260
261         return 0;
262 }
263
264 /* prepare pcm callback */
265 int snd_line6_prepare(struct snd_pcm_substream *substream)
266 {
267         struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
268
269         if(!test_and_set_bit(BIT_PREPARED, &line6pcm->flags)) {
270                 unlink_wait_clear_audio_out_urbs(line6pcm);
271                 line6pcm->pos_out = 0;
272                 line6pcm->pos_out_done = 0;
273
274                 unlink_wait_clear_audio_in_urbs(line6pcm);
275                 line6pcm->bytes_out = 0;
276                 line6pcm->pos_in_done = 0;
277                 line6pcm->bytes_in = 0;
278         }
279
280         return 0;
281 }