Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Line 6 Linux USB driver
0004  *
0005  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
0006  */
0007 
0008 #include <linux/slab.h>
0009 #include <linux/export.h>
0010 #include <sound/core.h>
0011 #include <sound/control.h>
0012 #include <sound/pcm.h>
0013 #include <sound/pcm_params.h>
0014 
0015 #include "capture.h"
0016 #include "driver.h"
0017 #include "playback.h"
0018 
0019 /* impulse response volume controls */
0020 static int snd_line6_impulse_volume_info(struct snd_kcontrol *kcontrol,
0021                      struct snd_ctl_elem_info *uinfo)
0022 {
0023     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0024     uinfo->count = 1;
0025     uinfo->value.integer.min = 0;
0026     uinfo->value.integer.max = 255;
0027     return 0;
0028 }
0029 
0030 static int snd_line6_impulse_volume_get(struct snd_kcontrol *kcontrol,
0031                     struct snd_ctl_elem_value *ucontrol)
0032 {
0033     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0034 
0035     ucontrol->value.integer.value[0] = line6pcm->impulse_volume;
0036     return 0;
0037 }
0038 
0039 static int snd_line6_impulse_volume_put(struct snd_kcontrol *kcontrol,
0040                     struct snd_ctl_elem_value *ucontrol)
0041 {
0042     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0043     int value = ucontrol->value.integer.value[0];
0044     int err;
0045 
0046     if (line6pcm->impulse_volume == value)
0047         return 0;
0048 
0049     line6pcm->impulse_volume = value;
0050     if (value > 0) {
0051         err = line6_pcm_acquire(line6pcm, LINE6_STREAM_IMPULSE, true);
0052         if (err < 0) {
0053             line6pcm->impulse_volume = 0;
0054             return err;
0055         }
0056     } else {
0057         line6_pcm_release(line6pcm, LINE6_STREAM_IMPULSE);
0058     }
0059     return 1;
0060 }
0061 
0062 /* impulse response period controls */
0063 static int snd_line6_impulse_period_info(struct snd_kcontrol *kcontrol,
0064                      struct snd_ctl_elem_info *uinfo)
0065 {
0066     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0067     uinfo->count = 1;
0068     uinfo->value.integer.min = 0;
0069     uinfo->value.integer.max = 2000;
0070     return 0;
0071 }
0072 
0073 static int snd_line6_impulse_period_get(struct snd_kcontrol *kcontrol,
0074                     struct snd_ctl_elem_value *ucontrol)
0075 {
0076     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0077 
0078     ucontrol->value.integer.value[0] = line6pcm->impulse_period;
0079     return 0;
0080 }
0081 
0082 static int snd_line6_impulse_period_put(struct snd_kcontrol *kcontrol,
0083                     struct snd_ctl_elem_value *ucontrol)
0084 {
0085     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0086     int value = ucontrol->value.integer.value[0];
0087 
0088     if (line6pcm->impulse_period == value)
0089         return 0;
0090 
0091     line6pcm->impulse_period = value;
0092     return 1;
0093 }
0094 
0095 /*
0096     Unlink all currently active URBs.
0097 */
0098 static void line6_unlink_audio_urbs(struct snd_line6_pcm *line6pcm,
0099                     struct line6_pcm_stream *pcms)
0100 {
0101     int i;
0102 
0103     for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
0104         if (test_bit(i, &pcms->active_urbs)) {
0105             if (!test_and_set_bit(i, &pcms->unlink_urbs))
0106                 usb_unlink_urb(pcms->urbs[i]);
0107         }
0108     }
0109 }
0110 
0111 /*
0112     Wait until unlinking of all currently active URBs has been finished.
0113 */
0114 static void line6_wait_clear_audio_urbs(struct snd_line6_pcm *line6pcm,
0115                     struct line6_pcm_stream *pcms)
0116 {
0117     int timeout = HZ;
0118     int i;
0119     int alive;
0120 
0121     do {
0122         alive = 0;
0123         for (i = 0; i < line6pcm->line6->iso_buffers; i++) {
0124             if (test_bit(i, &pcms->active_urbs))
0125                 alive++;
0126         }
0127         if (!alive)
0128             break;
0129         set_current_state(TASK_UNINTERRUPTIBLE);
0130         schedule_timeout(1);
0131     } while (--timeout > 0);
0132     if (alive)
0133         dev_err(line6pcm->line6->ifcdev,
0134             "timeout: still %d active urbs..\n", alive);
0135 }
0136 
0137 static inline struct line6_pcm_stream *
0138 get_stream(struct snd_line6_pcm *line6pcm, int direction)
0139 {
0140     return (direction == SNDRV_PCM_STREAM_PLAYBACK) ?
0141         &line6pcm->out : &line6pcm->in;
0142 }
0143 
0144 /* allocate a buffer if not opened yet;
0145  * call this in line6pcm.state_mutex
0146  */
0147 static int line6_buffer_acquire(struct snd_line6_pcm *line6pcm,
0148                 struct line6_pcm_stream *pstr, int direction, int type)
0149 {
0150     const int pkt_size =
0151         (direction == SNDRV_PCM_STREAM_PLAYBACK) ?
0152             line6pcm->max_packet_size_out :
0153             line6pcm->max_packet_size_in;
0154 
0155     /* Invoked multiple times in a row so allocate once only */
0156     if (!test_and_set_bit(type, &pstr->opened) && !pstr->buffer) {
0157         pstr->buffer =
0158             kmalloc(array3_size(line6pcm->line6->iso_buffers,
0159                         LINE6_ISO_PACKETS, pkt_size),
0160                 GFP_KERNEL);
0161         if (!pstr->buffer)
0162             return -ENOMEM;
0163     }
0164     return 0;
0165 }
0166 
0167 /* free a buffer if all streams are closed;
0168  * call this in line6pcm.state_mutex
0169  */
0170 static void line6_buffer_release(struct snd_line6_pcm *line6pcm,
0171                  struct line6_pcm_stream *pstr, int type)
0172 {
0173     clear_bit(type, &pstr->opened);
0174     if (!pstr->opened) {
0175         line6_wait_clear_audio_urbs(line6pcm, pstr);
0176         kfree(pstr->buffer);
0177         pstr->buffer = NULL;
0178     }
0179 }
0180 
0181 /* start a PCM stream */
0182 static int line6_stream_start(struct snd_line6_pcm *line6pcm, int direction,
0183                   int type)
0184 {
0185     unsigned long flags;
0186     struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
0187     int ret = 0;
0188 
0189     spin_lock_irqsave(&pstr->lock, flags);
0190     if (!test_and_set_bit(type, &pstr->running) &&
0191         !(pstr->active_urbs || pstr->unlink_urbs)) {
0192         pstr->count = 0;
0193         /* Submit all currently available URBs */
0194         if (direction == SNDRV_PCM_STREAM_PLAYBACK)
0195             ret = line6_submit_audio_out_all_urbs(line6pcm);
0196         else
0197             ret = line6_submit_audio_in_all_urbs(line6pcm);
0198     }
0199 
0200     if (ret < 0)
0201         clear_bit(type, &pstr->running);
0202     spin_unlock_irqrestore(&pstr->lock, flags);
0203     return ret;
0204 }
0205 
0206 /* stop a PCM stream; this doesn't sync with the unlinked URBs */
0207 static void line6_stream_stop(struct snd_line6_pcm *line6pcm, int direction,
0208               int type)
0209 {
0210     unsigned long flags;
0211     struct line6_pcm_stream *pstr = get_stream(line6pcm, direction);
0212 
0213     spin_lock_irqsave(&pstr->lock, flags);
0214     clear_bit(type, &pstr->running);
0215     if (!pstr->running) {
0216         spin_unlock_irqrestore(&pstr->lock, flags);
0217         line6_unlink_audio_urbs(line6pcm, pstr);
0218         spin_lock_irqsave(&pstr->lock, flags);
0219         if (direction == SNDRV_PCM_STREAM_CAPTURE) {
0220             line6pcm->prev_fbuf = NULL;
0221             line6pcm->prev_fsize = 0;
0222         }
0223     }
0224     spin_unlock_irqrestore(&pstr->lock, flags);
0225 }
0226 
0227 /* common PCM trigger callback */
0228 int snd_line6_trigger(struct snd_pcm_substream *substream, int cmd)
0229 {
0230     struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
0231     struct snd_pcm_substream *s;
0232     int err;
0233 
0234     clear_bit(LINE6_FLAG_PREPARED, &line6pcm->flags);
0235 
0236     snd_pcm_group_for_each_entry(s, substream) {
0237         if (s->pcm->card != substream->pcm->card)
0238             continue;
0239 
0240         switch (cmd) {
0241         case SNDRV_PCM_TRIGGER_START:
0242         case SNDRV_PCM_TRIGGER_RESUME:
0243             if (s->stream == SNDRV_PCM_STREAM_CAPTURE &&
0244                 (line6pcm->line6->properties->capabilities &
0245                     LINE6_CAP_IN_NEEDS_OUT)) {
0246                 err = line6_stream_start(line6pcm, SNDRV_PCM_STREAM_PLAYBACK,
0247                          LINE6_STREAM_CAPTURE_HELPER);
0248                 if (err < 0)
0249                     return err;
0250             }
0251             err = line6_stream_start(line6pcm, s->stream,
0252                          LINE6_STREAM_PCM);
0253             if (err < 0)
0254                 return err;
0255             break;
0256 
0257         case SNDRV_PCM_TRIGGER_STOP:
0258         case SNDRV_PCM_TRIGGER_SUSPEND:
0259             if (s->stream == SNDRV_PCM_STREAM_CAPTURE &&
0260                 (line6pcm->line6->properties->capabilities &
0261                     LINE6_CAP_IN_NEEDS_OUT)) {
0262                 line6_stream_stop(line6pcm, SNDRV_PCM_STREAM_PLAYBACK,
0263                       LINE6_STREAM_CAPTURE_HELPER);
0264             }
0265             line6_stream_stop(line6pcm, s->stream,
0266                       LINE6_STREAM_PCM);
0267             break;
0268 
0269         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0270             if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
0271                 return -EINVAL;
0272             set_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
0273             break;
0274 
0275         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0276             if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
0277                 return -EINVAL;
0278             clear_bit(LINE6_FLAG_PAUSE_PLAYBACK, &line6pcm->flags);
0279             break;
0280 
0281         default:
0282             return -EINVAL;
0283         }
0284     }
0285 
0286     return 0;
0287 }
0288 
0289 /* common PCM pointer callback */
0290 snd_pcm_uframes_t snd_line6_pointer(struct snd_pcm_substream *substream)
0291 {
0292     struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
0293     struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
0294 
0295     return pstr->pos_done;
0296 }
0297 
0298 /* Acquire and optionally start duplex streams:
0299  * type is either LINE6_STREAM_IMPULSE or LINE6_STREAM_MONITOR
0300  */
0301 int line6_pcm_acquire(struct snd_line6_pcm *line6pcm, int type, bool start)
0302 {
0303     struct line6_pcm_stream *pstr;
0304     int ret = 0, dir;
0305 
0306     /* TODO: We should assert SNDRV_PCM_STREAM_PLAYBACK/CAPTURE == 0/1 */
0307     mutex_lock(&line6pcm->state_mutex);
0308     for (dir = 0; dir < 2; dir++) {
0309         pstr = get_stream(line6pcm, dir);
0310         ret = line6_buffer_acquire(line6pcm, pstr, dir, type);
0311         if (ret < 0)
0312             goto error;
0313         if (!pstr->running)
0314             line6_wait_clear_audio_urbs(line6pcm, pstr);
0315     }
0316     if (start) {
0317         for (dir = 0; dir < 2; dir++) {
0318             ret = line6_stream_start(line6pcm, dir, type);
0319             if (ret < 0)
0320                 goto error;
0321         }
0322     }
0323  error:
0324     mutex_unlock(&line6pcm->state_mutex);
0325     if (ret < 0)
0326         line6_pcm_release(line6pcm, type);
0327     return ret;
0328 }
0329 EXPORT_SYMBOL_GPL(line6_pcm_acquire);
0330 
0331 /* Stop and release duplex streams */
0332 void line6_pcm_release(struct snd_line6_pcm *line6pcm, int type)
0333 {
0334     struct line6_pcm_stream *pstr;
0335     int dir;
0336 
0337     mutex_lock(&line6pcm->state_mutex);
0338     for (dir = 0; dir < 2; dir++)
0339         line6_stream_stop(line6pcm, dir, type);
0340     for (dir = 0; dir < 2; dir++) {
0341         pstr = get_stream(line6pcm, dir);
0342         line6_buffer_release(line6pcm, pstr, type);
0343     }
0344     mutex_unlock(&line6pcm->state_mutex);
0345 }
0346 EXPORT_SYMBOL_GPL(line6_pcm_release);
0347 
0348 /* common PCM hw_params callback */
0349 int snd_line6_hw_params(struct snd_pcm_substream *substream,
0350             struct snd_pcm_hw_params *hw_params)
0351 {
0352     int ret;
0353     struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
0354     struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
0355 
0356     mutex_lock(&line6pcm->state_mutex);
0357     ret = line6_buffer_acquire(line6pcm, pstr, substream->stream,
0358                                LINE6_STREAM_PCM);
0359     if (ret < 0)
0360         goto error;
0361 
0362     pstr->period = params_period_bytes(hw_params);
0363  error:
0364     mutex_unlock(&line6pcm->state_mutex);
0365     return ret;
0366 }
0367 
0368 /* common PCM hw_free callback */
0369 int snd_line6_hw_free(struct snd_pcm_substream *substream)
0370 {
0371     struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
0372     struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
0373 
0374     mutex_lock(&line6pcm->state_mutex);
0375     line6_buffer_release(line6pcm, pstr, LINE6_STREAM_PCM);
0376     mutex_unlock(&line6pcm->state_mutex);
0377     return 0;
0378 }
0379 
0380 
0381 /* control info callback */
0382 static int snd_line6_control_playback_info(struct snd_kcontrol *kcontrol,
0383                        struct snd_ctl_elem_info *uinfo)
0384 {
0385     uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0386     uinfo->count = 2;
0387     uinfo->value.integer.min = 0;
0388     uinfo->value.integer.max = 256;
0389     return 0;
0390 }
0391 
0392 /* control get callback */
0393 static int snd_line6_control_playback_get(struct snd_kcontrol *kcontrol,
0394                       struct snd_ctl_elem_value *ucontrol)
0395 {
0396     int i;
0397     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0398 
0399     for (i = 0; i < 2; i++)
0400         ucontrol->value.integer.value[i] = line6pcm->volume_playback[i];
0401 
0402     return 0;
0403 }
0404 
0405 /* control put callback */
0406 static int snd_line6_control_playback_put(struct snd_kcontrol *kcontrol,
0407                       struct snd_ctl_elem_value *ucontrol)
0408 {
0409     int i, changed = 0;
0410     struct snd_line6_pcm *line6pcm = snd_kcontrol_chip(kcontrol);
0411 
0412     for (i = 0; i < 2; i++)
0413         if (line6pcm->volume_playback[i] !=
0414             ucontrol->value.integer.value[i]) {
0415             line6pcm->volume_playback[i] =
0416                 ucontrol->value.integer.value[i];
0417             changed = 1;
0418         }
0419 
0420     return changed;
0421 }
0422 
0423 /* control definition */
0424 static const struct snd_kcontrol_new line6_controls[] = {
0425     {
0426         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0427         .name = "PCM Playback Volume",
0428         .info = snd_line6_control_playback_info,
0429         .get = snd_line6_control_playback_get,
0430         .put = snd_line6_control_playback_put
0431     },
0432     {
0433         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0434         .name = "Impulse Response Volume",
0435         .info = snd_line6_impulse_volume_info,
0436         .get = snd_line6_impulse_volume_get,
0437         .put = snd_line6_impulse_volume_put
0438     },
0439     {
0440         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
0441         .name = "Impulse Response Period",
0442         .info = snd_line6_impulse_period_info,
0443         .get = snd_line6_impulse_period_get,
0444         .put = snd_line6_impulse_period_put
0445     },
0446 };
0447 
0448 /*
0449     Cleanup the PCM device.
0450 */
0451 static void cleanup_urbs(struct line6_pcm_stream *pcms, int iso_buffers)
0452 {
0453     int i;
0454 
0455     /* Most likely impossible in current code... */
0456     if (pcms->urbs == NULL)
0457         return;
0458 
0459     for (i = 0; i < iso_buffers; i++) {
0460         if (pcms->urbs[i]) {
0461             usb_kill_urb(pcms->urbs[i]);
0462             usb_free_urb(pcms->urbs[i]);
0463         }
0464     }
0465     kfree(pcms->urbs);
0466     pcms->urbs = NULL;
0467 }
0468 
0469 static void line6_cleanup_pcm(struct snd_pcm *pcm)
0470 {
0471     struct snd_line6_pcm *line6pcm = snd_pcm_chip(pcm);
0472 
0473     cleanup_urbs(&line6pcm->out, line6pcm->line6->iso_buffers);
0474     cleanup_urbs(&line6pcm->in, line6pcm->line6->iso_buffers);
0475     kfree(line6pcm);
0476 }
0477 
0478 /* create a PCM device */
0479 static int snd_line6_new_pcm(struct usb_line6 *line6, struct snd_pcm **pcm_ret)
0480 {
0481     struct snd_pcm *pcm;
0482     int err;
0483 
0484     err = snd_pcm_new(line6->card, (char *)line6->properties->name,
0485               0, 1, 1, pcm_ret);
0486     if (err < 0)
0487         return err;
0488     pcm = *pcm_ret;
0489     strcpy(pcm->name, line6->properties->name);
0490 
0491     /* set operators */
0492     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
0493             &snd_line6_playback_ops);
0494     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_line6_capture_ops);
0495 
0496     /* pre-allocation of buffers */
0497     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
0498                        NULL, 64 * 1024, 128 * 1024);
0499     return 0;
0500 }
0501 
0502 /*
0503     Sync with PCM stream stops.
0504 */
0505 void line6_pcm_disconnect(struct snd_line6_pcm *line6pcm)
0506 {
0507     line6_unlink_audio_urbs(line6pcm, &line6pcm->out);
0508     line6_unlink_audio_urbs(line6pcm, &line6pcm->in);
0509     line6_wait_clear_audio_urbs(line6pcm, &line6pcm->out);
0510     line6_wait_clear_audio_urbs(line6pcm, &line6pcm->in);
0511 }
0512 
0513 /*
0514     Create and register the PCM device and mixer entries.
0515     Create URBs for playback and capture.
0516 */
0517 int line6_init_pcm(struct usb_line6 *line6,
0518            struct line6_pcm_properties *properties)
0519 {
0520     int i, err;
0521     unsigned ep_read = line6->properties->ep_audio_r;
0522     unsigned ep_write = line6->properties->ep_audio_w;
0523     struct snd_pcm *pcm;
0524     struct snd_line6_pcm *line6pcm;
0525 
0526     if (!(line6->properties->capabilities & LINE6_CAP_PCM))
0527         return 0;   /* skip PCM initialization and report success */
0528 
0529     err = snd_line6_new_pcm(line6, &pcm);
0530     if (err < 0)
0531         return err;
0532 
0533     line6pcm = kzalloc(sizeof(*line6pcm), GFP_KERNEL);
0534     if (!line6pcm)
0535         return -ENOMEM;
0536 
0537     mutex_init(&line6pcm->state_mutex);
0538     line6pcm->pcm = pcm;
0539     line6pcm->properties = properties;
0540     line6pcm->volume_playback[0] = line6pcm->volume_playback[1] = 255;
0541     line6pcm->volume_monitor = 255;
0542     line6pcm->line6 = line6;
0543 
0544     spin_lock_init(&line6pcm->out.lock);
0545     spin_lock_init(&line6pcm->in.lock);
0546     line6pcm->impulse_period = LINE6_IMPULSE_DEFAULT_PERIOD;
0547 
0548     line6->line6pcm = line6pcm;
0549 
0550     pcm->private_data = line6pcm;
0551     pcm->private_free = line6_cleanup_pcm;
0552 
0553     line6pcm->max_packet_size_in =
0554         usb_maxpacket(line6->usbdev,
0555             usb_rcvisocpipe(line6->usbdev, ep_read));
0556     line6pcm->max_packet_size_out =
0557         usb_maxpacket(line6->usbdev,
0558             usb_sndisocpipe(line6->usbdev, ep_write));
0559     if (!line6pcm->max_packet_size_in || !line6pcm->max_packet_size_out) {
0560         dev_err(line6pcm->line6->ifcdev,
0561             "cannot get proper max packet size\n");
0562         return -EINVAL;
0563     }
0564 
0565     err = line6_create_audio_out_urbs(line6pcm);
0566     if (err < 0)
0567         return err;
0568 
0569     err = line6_create_audio_in_urbs(line6pcm);
0570     if (err < 0)
0571         return err;
0572 
0573     /* mixer: */
0574     for (i = 0; i < ARRAY_SIZE(line6_controls); i++) {
0575         err = snd_ctl_add(line6->card,
0576                   snd_ctl_new1(&line6_controls[i], line6pcm));
0577         if (err < 0)
0578             return err;
0579     }
0580 
0581     return 0;
0582 }
0583 EXPORT_SYMBOL_GPL(line6_init_pcm);
0584 
0585 /* prepare pcm callback */
0586 int snd_line6_prepare(struct snd_pcm_substream *substream)
0587 {
0588     struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
0589     struct line6_pcm_stream *pstr = get_stream(line6pcm, substream->stream);
0590 
0591     mutex_lock(&line6pcm->state_mutex);
0592     if (!pstr->running)
0593         line6_wait_clear_audio_urbs(line6pcm, pstr);
0594 
0595     if (!test_and_set_bit(LINE6_FLAG_PREPARED, &line6pcm->flags)) {
0596         line6pcm->out.count = 0;
0597         line6pcm->out.pos = 0;
0598         line6pcm->out.pos_done = 0;
0599         line6pcm->out.bytes = 0;
0600         line6pcm->in.count = 0;
0601         line6pcm->in.pos_done = 0;
0602         line6pcm->in.bytes = 0;
0603     }
0604 
0605     mutex_unlock(&line6pcm->state_mutex);
0606     return 0;
0607 }