Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Linux driver for M2Tech hiFace compatible devices
0004  *
0005  * Copyright 2012-2013 (C) M2TECH S.r.l and Amarula Solutions B.V.
0006  *
0007  * Authors:  Michael Trimarchi <michael@amarulasolutions.com>
0008  *           Antonio Ospite <ao2@amarulasolutions.com>
0009  *
0010  * The driver is based on the work done in TerraTec DMX 6Fire USB
0011  */
0012 
0013 #include <linux/slab.h>
0014 #include <sound/pcm.h>
0015 
0016 #include "pcm.h"
0017 #include "chip.h"
0018 
0019 #define OUT_EP          0x2
0020 #define PCM_N_URBS      8
0021 #define PCM_PACKET_SIZE 4096
0022 #define PCM_BUFFER_SIZE (2 * PCM_N_URBS * PCM_PACKET_SIZE)
0023 
0024 struct pcm_urb {
0025     struct hiface_chip *chip;
0026 
0027     struct urb instance;
0028     struct usb_anchor submitted;
0029     u8 *buffer;
0030 };
0031 
0032 struct pcm_substream {
0033     spinlock_t lock;
0034     struct snd_pcm_substream *instance;
0035 
0036     bool active;
0037     snd_pcm_uframes_t dma_off;    /* current position in alsa dma_area */
0038     snd_pcm_uframes_t period_off; /* current position in current period */
0039 };
0040 
0041 enum { /* pcm streaming states */
0042     STREAM_DISABLED, /* no pcm streaming */
0043     STREAM_STARTING, /* pcm streaming requested, waiting to become ready */
0044     STREAM_RUNNING,  /* pcm streaming running */
0045     STREAM_STOPPING
0046 };
0047 
0048 struct pcm_runtime {
0049     struct hiface_chip *chip;
0050     struct snd_pcm *instance;
0051 
0052     struct pcm_substream playback;
0053     bool panic; /* if set driver won't do anymore pcm on device */
0054 
0055     struct pcm_urb out_urbs[PCM_N_URBS];
0056 
0057     struct mutex stream_mutex;
0058     u8 stream_state; /* one of STREAM_XXX */
0059     u8 extra_freq;
0060     wait_queue_head_t stream_wait_queue;
0061     bool stream_wait_cond;
0062 };
0063 
0064 static const unsigned int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000,
0065                       352800, 384000 };
0066 static const struct snd_pcm_hw_constraint_list constraints_extra_rates = {
0067     .count = ARRAY_SIZE(rates),
0068     .list = rates,
0069     .mask = 0,
0070 };
0071 
0072 static const struct snd_pcm_hardware pcm_hw = {
0073     .info = SNDRV_PCM_INFO_MMAP |
0074         SNDRV_PCM_INFO_INTERLEAVED |
0075         SNDRV_PCM_INFO_BLOCK_TRANSFER |
0076         SNDRV_PCM_INFO_PAUSE |
0077         SNDRV_PCM_INFO_MMAP_VALID |
0078         SNDRV_PCM_INFO_BATCH,
0079 
0080     .formats = SNDRV_PCM_FMTBIT_S32_LE,
0081 
0082     .rates = SNDRV_PCM_RATE_44100 |
0083         SNDRV_PCM_RATE_48000 |
0084         SNDRV_PCM_RATE_88200 |
0085         SNDRV_PCM_RATE_96000 |
0086         SNDRV_PCM_RATE_176400 |
0087         SNDRV_PCM_RATE_192000,
0088 
0089     .rate_min = 44100,
0090     .rate_max = 192000, /* changes in hiface_pcm_open to support extra rates */
0091     .channels_min = 2,
0092     .channels_max = 2,
0093     .buffer_bytes_max = PCM_BUFFER_SIZE,
0094     .period_bytes_min = PCM_PACKET_SIZE,
0095     .period_bytes_max = PCM_BUFFER_SIZE,
0096     .periods_min = 2,
0097     .periods_max = 1024
0098 };
0099 
0100 /* message values used to change the sample rate */
0101 #define HIFACE_SET_RATE_REQUEST 0xb0
0102 
0103 #define HIFACE_RATE_44100  0x43
0104 #define HIFACE_RATE_48000  0x4b
0105 #define HIFACE_RATE_88200  0x42
0106 #define HIFACE_RATE_96000  0x4a
0107 #define HIFACE_RATE_176400 0x40
0108 #define HIFACE_RATE_192000 0x48
0109 #define HIFACE_RATE_352800 0x58
0110 #define HIFACE_RATE_384000 0x68
0111 
0112 static int hiface_pcm_set_rate(struct pcm_runtime *rt, unsigned int rate)
0113 {
0114     struct usb_device *device = rt->chip->dev;
0115     u16 rate_value;
0116     int ret;
0117 
0118     /* We are already sure that the rate is supported here thanks to
0119      * ALSA constraints
0120      */
0121     switch (rate) {
0122     case 44100:
0123         rate_value = HIFACE_RATE_44100;
0124         break;
0125     case 48000:
0126         rate_value = HIFACE_RATE_48000;
0127         break;
0128     case 88200:
0129         rate_value = HIFACE_RATE_88200;
0130         break;
0131     case 96000:
0132         rate_value = HIFACE_RATE_96000;
0133         break;
0134     case 176400:
0135         rate_value = HIFACE_RATE_176400;
0136         break;
0137     case 192000:
0138         rate_value = HIFACE_RATE_192000;
0139         break;
0140     case 352800:
0141         rate_value = HIFACE_RATE_352800;
0142         break;
0143     case 384000:
0144         rate_value = HIFACE_RATE_384000;
0145         break;
0146     default:
0147         dev_err(&device->dev, "Unsupported rate %d\n", rate);
0148         return -EINVAL;
0149     }
0150 
0151     /*
0152      * USBIO: Vendor 0xb0(wValue=0x0043, wIndex=0x0000)
0153      * 43 b0 43 00 00 00 00 00
0154      * USBIO: Vendor 0xb0(wValue=0x004b, wIndex=0x0000)
0155      * 43 b0 4b 00 00 00 00 00
0156      * This control message doesn't have any ack from the
0157      * other side
0158      */
0159     ret = usb_control_msg_send(device, 0,
0160                    HIFACE_SET_RATE_REQUEST,
0161                    USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
0162                    rate_value, 0, NULL, 0, 100, GFP_KERNEL);
0163     if (ret)
0164         dev_err(&device->dev, "Error setting samplerate %d.\n", rate);
0165 
0166     return ret;
0167 }
0168 
0169 static struct pcm_substream *hiface_pcm_get_substream(struct snd_pcm_substream
0170                               *alsa_sub)
0171 {
0172     struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
0173     struct device *device = &rt->chip->dev->dev;
0174 
0175     if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
0176         return &rt->playback;
0177 
0178     dev_err(device, "Error getting pcm substream slot.\n");
0179     return NULL;
0180 }
0181 
0182 /* call with stream_mutex locked */
0183 static void hiface_pcm_stream_stop(struct pcm_runtime *rt)
0184 {
0185     int i, time;
0186 
0187     if (rt->stream_state != STREAM_DISABLED) {
0188         rt->stream_state = STREAM_STOPPING;
0189 
0190         for (i = 0; i < PCM_N_URBS; i++) {
0191             time = usb_wait_anchor_empty_timeout(
0192                     &rt->out_urbs[i].submitted, 100);
0193             if (!time)
0194                 usb_kill_anchored_urbs(
0195                     &rt->out_urbs[i].submitted);
0196             usb_kill_urb(&rt->out_urbs[i].instance);
0197         }
0198 
0199         rt->stream_state = STREAM_DISABLED;
0200     }
0201 }
0202 
0203 /* call with stream_mutex locked */
0204 static int hiface_pcm_stream_start(struct pcm_runtime *rt)
0205 {
0206     int ret = 0;
0207     int i;
0208 
0209     if (rt->stream_state == STREAM_DISABLED) {
0210 
0211         /* reset panic state when starting a new stream */
0212         rt->panic = false;
0213 
0214         /* submit our out urbs zero init */
0215         rt->stream_state = STREAM_STARTING;
0216         for (i = 0; i < PCM_N_URBS; i++) {
0217             memset(rt->out_urbs[i].buffer, 0, PCM_PACKET_SIZE);
0218             usb_anchor_urb(&rt->out_urbs[i].instance,
0219                        &rt->out_urbs[i].submitted);
0220             ret = usb_submit_urb(&rt->out_urbs[i].instance,
0221                          GFP_ATOMIC);
0222             if (ret) {
0223                 hiface_pcm_stream_stop(rt);
0224                 return ret;
0225             }
0226         }
0227 
0228         /* wait for first out urb to return (sent in urb handler) */
0229         wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
0230                    HZ);
0231         if (rt->stream_wait_cond) {
0232             struct device *device = &rt->chip->dev->dev;
0233             dev_dbg(device, "%s: Stream is running wakeup event\n",
0234                  __func__);
0235             rt->stream_state = STREAM_RUNNING;
0236         } else {
0237             hiface_pcm_stream_stop(rt);
0238             return -EIO;
0239         }
0240     }
0241     return ret;
0242 }
0243 
0244 /* The hardware wants word-swapped 32-bit values */
0245 static void memcpy_swahw32(u8 *dest, u8 *src, unsigned int n)
0246 {
0247     unsigned int i;
0248 
0249     for (i = 0; i < n / 4; i++)
0250         ((u32 *)dest)[i] = swahw32(((u32 *)src)[i]);
0251 }
0252 
0253 /* call with substream locked */
0254 /* returns true if a period elapsed */
0255 static bool hiface_pcm_playback(struct pcm_substream *sub, struct pcm_urb *urb)
0256 {
0257     struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
0258     struct device *device = &urb->chip->dev->dev;
0259     u8 *source;
0260     unsigned int pcm_buffer_size;
0261 
0262     WARN_ON(alsa_rt->format != SNDRV_PCM_FORMAT_S32_LE);
0263 
0264     pcm_buffer_size = snd_pcm_lib_buffer_bytes(sub->instance);
0265 
0266     if (sub->dma_off + PCM_PACKET_SIZE <= pcm_buffer_size) {
0267         dev_dbg(device, "%s: (1) buffer_size %#x dma_offset %#x\n", __func__,
0268              (unsigned int) pcm_buffer_size,
0269              (unsigned int) sub->dma_off);
0270 
0271         source = alsa_rt->dma_area + sub->dma_off;
0272         memcpy_swahw32(urb->buffer, source, PCM_PACKET_SIZE);
0273     } else {
0274         /* wrap around at end of ring buffer */
0275         unsigned int len;
0276 
0277         dev_dbg(device, "%s: (2) buffer_size %#x dma_offset %#x\n", __func__,
0278              (unsigned int) pcm_buffer_size,
0279              (unsigned int) sub->dma_off);
0280 
0281         len = pcm_buffer_size - sub->dma_off;
0282 
0283         source = alsa_rt->dma_area + sub->dma_off;
0284         memcpy_swahw32(urb->buffer, source, len);
0285 
0286         source = alsa_rt->dma_area;
0287         memcpy_swahw32(urb->buffer + len, source,
0288                    PCM_PACKET_SIZE - len);
0289     }
0290     sub->dma_off += PCM_PACKET_SIZE;
0291     if (sub->dma_off >= pcm_buffer_size)
0292         sub->dma_off -= pcm_buffer_size;
0293 
0294     sub->period_off += PCM_PACKET_SIZE;
0295     if (sub->period_off >= alsa_rt->period_size) {
0296         sub->period_off %= alsa_rt->period_size;
0297         return true;
0298     }
0299     return false;
0300 }
0301 
0302 static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
0303 {
0304     struct pcm_urb *out_urb = usb_urb->context;
0305     struct pcm_runtime *rt = out_urb->chip->pcm;
0306     struct pcm_substream *sub;
0307     bool do_period_elapsed = false;
0308     unsigned long flags;
0309     int ret;
0310 
0311     if (rt->panic || rt->stream_state == STREAM_STOPPING)
0312         return;
0313 
0314     if (unlikely(usb_urb->status == -ENOENT ||  /* unlinked */
0315              usb_urb->status == -ENODEV ||  /* device removed */
0316              usb_urb->status == -ECONNRESET ||  /* unlinked */
0317              usb_urb->status == -ESHUTDOWN)) {  /* device disabled */
0318         goto out_fail;
0319     }
0320 
0321     if (rt->stream_state == STREAM_STARTING) {
0322         rt->stream_wait_cond = true;
0323         wake_up(&rt->stream_wait_queue);
0324     }
0325 
0326     /* now send our playback data (if a free out urb was found) */
0327     sub = &rt->playback;
0328     spin_lock_irqsave(&sub->lock, flags);
0329     if (sub->active)
0330         do_period_elapsed = hiface_pcm_playback(sub, out_urb);
0331     else
0332         memset(out_urb->buffer, 0, PCM_PACKET_SIZE);
0333 
0334     spin_unlock_irqrestore(&sub->lock, flags);
0335 
0336     if (do_period_elapsed)
0337         snd_pcm_period_elapsed(sub->instance);
0338 
0339     ret = usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
0340     if (ret < 0)
0341         goto out_fail;
0342 
0343     return;
0344 
0345 out_fail:
0346     rt->panic = true;
0347 }
0348 
0349 static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
0350 {
0351     struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
0352     struct pcm_substream *sub = NULL;
0353     struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
0354     int ret;
0355 
0356     if (rt->panic)
0357         return -EPIPE;
0358 
0359     mutex_lock(&rt->stream_mutex);
0360     alsa_rt->hw = pcm_hw;
0361 
0362     if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
0363         sub = &rt->playback;
0364 
0365     if (!sub) {
0366         struct device *device = &rt->chip->dev->dev;
0367         mutex_unlock(&rt->stream_mutex);
0368         dev_err(device, "Invalid stream type\n");
0369         return -EINVAL;
0370     }
0371 
0372     if (rt->extra_freq) {
0373         alsa_rt->hw.rates |= SNDRV_PCM_RATE_KNOT;
0374         alsa_rt->hw.rate_max = 384000;
0375 
0376         /* explicit constraints needed as we added SNDRV_PCM_RATE_KNOT */
0377         ret = snd_pcm_hw_constraint_list(alsa_sub->runtime, 0,
0378                          SNDRV_PCM_HW_PARAM_RATE,
0379                          &constraints_extra_rates);
0380         if (ret < 0) {
0381             mutex_unlock(&rt->stream_mutex);
0382             return ret;
0383         }
0384     }
0385 
0386     sub->instance = alsa_sub;
0387     sub->active = false;
0388     mutex_unlock(&rt->stream_mutex);
0389     return 0;
0390 }
0391 
0392 static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
0393 {
0394     struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
0395     struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
0396     unsigned long flags;
0397 
0398     if (rt->panic)
0399         return 0;
0400 
0401     mutex_lock(&rt->stream_mutex);
0402     if (sub) {
0403         hiface_pcm_stream_stop(rt);
0404 
0405         /* deactivate substream */
0406         spin_lock_irqsave(&sub->lock, flags);
0407         sub->instance = NULL;
0408         sub->active = false;
0409         spin_unlock_irqrestore(&sub->lock, flags);
0410 
0411     }
0412     mutex_unlock(&rt->stream_mutex);
0413     return 0;
0414 }
0415 
0416 static int hiface_pcm_prepare(struct snd_pcm_substream *alsa_sub)
0417 {
0418     struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
0419     struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
0420     struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
0421     int ret;
0422 
0423     if (rt->panic)
0424         return -EPIPE;
0425     if (!sub)
0426         return -ENODEV;
0427 
0428     mutex_lock(&rt->stream_mutex);
0429 
0430     hiface_pcm_stream_stop(rt);
0431 
0432     sub->dma_off = 0;
0433     sub->period_off = 0;
0434 
0435     if (rt->stream_state == STREAM_DISABLED) {
0436 
0437         ret = hiface_pcm_set_rate(rt, alsa_rt->rate);
0438         if (ret) {
0439             mutex_unlock(&rt->stream_mutex);
0440             return ret;
0441         }
0442         ret = hiface_pcm_stream_start(rt);
0443         if (ret) {
0444             mutex_unlock(&rt->stream_mutex);
0445             return ret;
0446         }
0447     }
0448     mutex_unlock(&rt->stream_mutex);
0449     return 0;
0450 }
0451 
0452 static int hiface_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
0453 {
0454     struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
0455     struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
0456 
0457     if (rt->panic)
0458         return -EPIPE;
0459     if (!sub)
0460         return -ENODEV;
0461 
0462     switch (cmd) {
0463     case SNDRV_PCM_TRIGGER_START:
0464     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0465         spin_lock_irq(&sub->lock);
0466         sub->active = true;
0467         spin_unlock_irq(&sub->lock);
0468         return 0;
0469 
0470     case SNDRV_PCM_TRIGGER_STOP:
0471     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0472         spin_lock_irq(&sub->lock);
0473         sub->active = false;
0474         spin_unlock_irq(&sub->lock);
0475         return 0;
0476 
0477     default:
0478         return -EINVAL;
0479     }
0480 }
0481 
0482 static snd_pcm_uframes_t hiface_pcm_pointer(struct snd_pcm_substream *alsa_sub)
0483 {
0484     struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
0485     struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
0486     unsigned long flags;
0487     snd_pcm_uframes_t dma_offset;
0488 
0489     if (rt->panic || !sub)
0490         return SNDRV_PCM_POS_XRUN;
0491 
0492     spin_lock_irqsave(&sub->lock, flags);
0493     dma_offset = sub->dma_off;
0494     spin_unlock_irqrestore(&sub->lock, flags);
0495     return bytes_to_frames(alsa_sub->runtime, dma_offset);
0496 }
0497 
0498 static const struct snd_pcm_ops pcm_ops = {
0499     .open = hiface_pcm_open,
0500     .close = hiface_pcm_close,
0501     .prepare = hiface_pcm_prepare,
0502     .trigger = hiface_pcm_trigger,
0503     .pointer = hiface_pcm_pointer,
0504 };
0505 
0506 static int hiface_pcm_init_urb(struct pcm_urb *urb,
0507                    struct hiface_chip *chip,
0508                    unsigned int ep,
0509                    void (*handler)(struct urb *))
0510 {
0511     urb->chip = chip;
0512     usb_init_urb(&urb->instance);
0513 
0514     urb->buffer = kzalloc(PCM_PACKET_SIZE, GFP_KERNEL);
0515     if (!urb->buffer)
0516         return -ENOMEM;
0517 
0518     usb_fill_bulk_urb(&urb->instance, chip->dev,
0519               usb_sndbulkpipe(chip->dev, ep), (void *)urb->buffer,
0520               PCM_PACKET_SIZE, handler, urb);
0521     if (usb_urb_ep_type_check(&urb->instance))
0522         return -EINVAL;
0523     init_usb_anchor(&urb->submitted);
0524 
0525     return 0;
0526 }
0527 
0528 void hiface_pcm_abort(struct hiface_chip *chip)
0529 {
0530     struct pcm_runtime *rt = chip->pcm;
0531 
0532     if (rt) {
0533         rt->panic = true;
0534 
0535         mutex_lock(&rt->stream_mutex);
0536         hiface_pcm_stream_stop(rt);
0537         mutex_unlock(&rt->stream_mutex);
0538     }
0539 }
0540 
0541 static void hiface_pcm_destroy(struct hiface_chip *chip)
0542 {
0543     struct pcm_runtime *rt = chip->pcm;
0544     int i;
0545 
0546     for (i = 0; i < PCM_N_URBS; i++)
0547         kfree(rt->out_urbs[i].buffer);
0548 
0549     kfree(chip->pcm);
0550     chip->pcm = NULL;
0551 }
0552 
0553 static void hiface_pcm_free(struct snd_pcm *pcm)
0554 {
0555     struct pcm_runtime *rt = pcm->private_data;
0556 
0557     if (rt)
0558         hiface_pcm_destroy(rt->chip);
0559 }
0560 
0561 int hiface_pcm_init(struct hiface_chip *chip, u8 extra_freq)
0562 {
0563     int i;
0564     int ret;
0565     struct snd_pcm *pcm;
0566     struct pcm_runtime *rt;
0567 
0568     rt = kzalloc(sizeof(*rt), GFP_KERNEL);
0569     if (!rt)
0570         return -ENOMEM;
0571 
0572     rt->chip = chip;
0573     rt->stream_state = STREAM_DISABLED;
0574     if (extra_freq)
0575         rt->extra_freq = 1;
0576 
0577     init_waitqueue_head(&rt->stream_wait_queue);
0578     mutex_init(&rt->stream_mutex);
0579     spin_lock_init(&rt->playback.lock);
0580 
0581     for (i = 0; i < PCM_N_URBS; i++) {
0582         ret = hiface_pcm_init_urb(&rt->out_urbs[i], chip, OUT_EP,
0583                     hiface_pcm_out_urb_handler);
0584         if (ret < 0)
0585             goto error;
0586     }
0587 
0588     ret = snd_pcm_new(chip->card, "USB-SPDIF Audio", 0, 1, 0, &pcm);
0589     if (ret < 0) {
0590         dev_err(&chip->dev->dev, "Cannot create pcm instance\n");
0591         goto error;
0592     }
0593 
0594     pcm->private_data = rt;
0595     pcm->private_free = hiface_pcm_free;
0596 
0597     strscpy(pcm->name, "USB-SPDIF Audio", sizeof(pcm->name));
0598     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
0599     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
0600                        NULL, 0, 0);
0601 
0602     rt->instance = pcm;
0603 
0604     chip->pcm = rt;
0605     return 0;
0606 
0607 error:
0608     for (i = 0; i < PCM_N_URBS; i++)
0609         kfree(rt->out_urbs[i].buffer);
0610     kfree(rt);
0611     return ret;
0612 }