12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a91c3fb2SAntonio Ospite /*
3a91c3fb2SAntonio Ospite * Linux driver for M2Tech hiFace compatible devices
4a91c3fb2SAntonio Ospite *
5a91c3fb2SAntonio Ospite * Copyright 2012-2013 (C) M2TECH S.r.l and Amarula Solutions B.V.
6a91c3fb2SAntonio Ospite *
7a91c3fb2SAntonio Ospite * Authors: Michael Trimarchi <michael@amarulasolutions.com>
8a91c3fb2SAntonio Ospite * Antonio Ospite <ao2@amarulasolutions.com>
9a91c3fb2SAntonio Ospite *
10a91c3fb2SAntonio Ospite * The driver is based on the work done in TerraTec DMX 6Fire USB
11a91c3fb2SAntonio Ospite */
12a91c3fb2SAntonio Ospite
13a91c3fb2SAntonio Ospite #include <linux/slab.h>
14a91c3fb2SAntonio Ospite #include <sound/pcm.h>
15a91c3fb2SAntonio Ospite
16a91c3fb2SAntonio Ospite #include "pcm.h"
17a91c3fb2SAntonio Ospite #include "chip.h"
18a91c3fb2SAntonio Ospite
19a91c3fb2SAntonio Ospite #define OUT_EP 0x2
20a91c3fb2SAntonio Ospite #define PCM_N_URBS 8
21a91c3fb2SAntonio Ospite #define PCM_PACKET_SIZE 4096
22a91c3fb2SAntonio Ospite #define PCM_BUFFER_SIZE (2 * PCM_N_URBS * PCM_PACKET_SIZE)
23a91c3fb2SAntonio Ospite
24a91c3fb2SAntonio Ospite struct pcm_urb {
25a91c3fb2SAntonio Ospite struct hiface_chip *chip;
26a91c3fb2SAntonio Ospite
27a91c3fb2SAntonio Ospite struct urb instance;
28a91c3fb2SAntonio Ospite struct usb_anchor submitted;
29a91c3fb2SAntonio Ospite u8 *buffer;
30a91c3fb2SAntonio Ospite };
31a91c3fb2SAntonio Ospite
32a91c3fb2SAntonio Ospite struct pcm_substream {
33a91c3fb2SAntonio Ospite spinlock_t lock;
34a91c3fb2SAntonio Ospite struct snd_pcm_substream *instance;
35a91c3fb2SAntonio Ospite
36a91c3fb2SAntonio Ospite bool active;
37a91c3fb2SAntonio Ospite snd_pcm_uframes_t dma_off; /* current position in alsa dma_area */
38a91c3fb2SAntonio Ospite snd_pcm_uframes_t period_off; /* current position in current period */
39a91c3fb2SAntonio Ospite };
40a91c3fb2SAntonio Ospite
41a91c3fb2SAntonio Ospite enum { /* pcm streaming states */
42a91c3fb2SAntonio Ospite STREAM_DISABLED, /* no pcm streaming */
43a91c3fb2SAntonio Ospite STREAM_STARTING, /* pcm streaming requested, waiting to become ready */
44a91c3fb2SAntonio Ospite STREAM_RUNNING, /* pcm streaming running */
45a91c3fb2SAntonio Ospite STREAM_STOPPING
46a91c3fb2SAntonio Ospite };
47a91c3fb2SAntonio Ospite
48a91c3fb2SAntonio Ospite struct pcm_runtime {
49a91c3fb2SAntonio Ospite struct hiface_chip *chip;
50a91c3fb2SAntonio Ospite struct snd_pcm *instance;
51a91c3fb2SAntonio Ospite
52a91c3fb2SAntonio Ospite struct pcm_substream playback;
53a91c3fb2SAntonio Ospite bool panic; /* if set driver won't do anymore pcm on device */
54a91c3fb2SAntonio Ospite
55a91c3fb2SAntonio Ospite struct pcm_urb out_urbs[PCM_N_URBS];
56a91c3fb2SAntonio Ospite
57a91c3fb2SAntonio Ospite struct mutex stream_mutex;
58a91c3fb2SAntonio Ospite u8 stream_state; /* one of STREAM_XXX */
59a91c3fb2SAntonio Ospite u8 extra_freq;
60a91c3fb2SAntonio Ospite wait_queue_head_t stream_wait_queue;
61a91c3fb2SAntonio Ospite bool stream_wait_cond;
62a91c3fb2SAntonio Ospite };
63a91c3fb2SAntonio Ospite
64a91c3fb2SAntonio Ospite static const unsigned int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000,
65a91c3fb2SAntonio Ospite 352800, 384000 };
66a91c3fb2SAntonio Ospite static const struct snd_pcm_hw_constraint_list constraints_extra_rates = {
67a91c3fb2SAntonio Ospite .count = ARRAY_SIZE(rates),
68a91c3fb2SAntonio Ospite .list = rates,
69a91c3fb2SAntonio Ospite .mask = 0,
70a91c3fb2SAntonio Ospite };
71a91c3fb2SAntonio Ospite
72a91c3fb2SAntonio Ospite static const struct snd_pcm_hardware pcm_hw = {
73a91c3fb2SAntonio Ospite .info = SNDRV_PCM_INFO_MMAP |
74a91c3fb2SAntonio Ospite SNDRV_PCM_INFO_INTERLEAVED |
75a91c3fb2SAntonio Ospite SNDRV_PCM_INFO_BLOCK_TRANSFER |
76a91c3fb2SAntonio Ospite SNDRV_PCM_INFO_PAUSE |
77a91c3fb2SAntonio Ospite SNDRV_PCM_INFO_MMAP_VALID |
78a91c3fb2SAntonio Ospite SNDRV_PCM_INFO_BATCH,
79a91c3fb2SAntonio Ospite
80a91c3fb2SAntonio Ospite .formats = SNDRV_PCM_FMTBIT_S32_LE,
81a91c3fb2SAntonio Ospite
82a91c3fb2SAntonio Ospite .rates = SNDRV_PCM_RATE_44100 |
83a91c3fb2SAntonio Ospite SNDRV_PCM_RATE_48000 |
84a91c3fb2SAntonio Ospite SNDRV_PCM_RATE_88200 |
85a91c3fb2SAntonio Ospite SNDRV_PCM_RATE_96000 |
86a91c3fb2SAntonio Ospite SNDRV_PCM_RATE_176400 |
87a91c3fb2SAntonio Ospite SNDRV_PCM_RATE_192000,
88a91c3fb2SAntonio Ospite
89a91c3fb2SAntonio Ospite .rate_min = 44100,
90a91c3fb2SAntonio Ospite .rate_max = 192000, /* changes in hiface_pcm_open to support extra rates */
91a91c3fb2SAntonio Ospite .channels_min = 2,
92a91c3fb2SAntonio Ospite .channels_max = 2,
93a91c3fb2SAntonio Ospite .buffer_bytes_max = PCM_BUFFER_SIZE,
94a91c3fb2SAntonio Ospite .period_bytes_min = PCM_PACKET_SIZE,
95a91c3fb2SAntonio Ospite .period_bytes_max = PCM_BUFFER_SIZE,
96a91c3fb2SAntonio Ospite .periods_min = 2,
97a91c3fb2SAntonio Ospite .periods_max = 1024
98a91c3fb2SAntonio Ospite };
99a91c3fb2SAntonio Ospite
100a91c3fb2SAntonio Ospite /* message values used to change the sample rate */
101a91c3fb2SAntonio Ospite #define HIFACE_SET_RATE_REQUEST 0xb0
102a91c3fb2SAntonio Ospite
103a91c3fb2SAntonio Ospite #define HIFACE_RATE_44100 0x43
104a91c3fb2SAntonio Ospite #define HIFACE_RATE_48000 0x4b
105a91c3fb2SAntonio Ospite #define HIFACE_RATE_88200 0x42
106a91c3fb2SAntonio Ospite #define HIFACE_RATE_96000 0x4a
107a91c3fb2SAntonio Ospite #define HIFACE_RATE_176400 0x40
108a91c3fb2SAntonio Ospite #define HIFACE_RATE_192000 0x48
109150116bcSMichael Trimarchi #define HIFACE_RATE_352800 0x58
110a91c3fb2SAntonio Ospite #define HIFACE_RATE_384000 0x68
111a91c3fb2SAntonio Ospite
hiface_pcm_set_rate(struct pcm_runtime * rt,unsigned int rate)112a91c3fb2SAntonio Ospite static int hiface_pcm_set_rate(struct pcm_runtime *rt, unsigned int rate)
113a91c3fb2SAntonio Ospite {
114a91c3fb2SAntonio Ospite struct usb_device *device = rt->chip->dev;
115a91c3fb2SAntonio Ospite u16 rate_value;
116a91c3fb2SAntonio Ospite int ret;
117a91c3fb2SAntonio Ospite
118a91c3fb2SAntonio Ospite /* We are already sure that the rate is supported here thanks to
119a91c3fb2SAntonio Ospite * ALSA constraints
120a91c3fb2SAntonio Ospite */
121a91c3fb2SAntonio Ospite switch (rate) {
122a91c3fb2SAntonio Ospite case 44100:
123a91c3fb2SAntonio Ospite rate_value = HIFACE_RATE_44100;
124a91c3fb2SAntonio Ospite break;
125a91c3fb2SAntonio Ospite case 48000:
126a91c3fb2SAntonio Ospite rate_value = HIFACE_RATE_48000;
127a91c3fb2SAntonio Ospite break;
128a91c3fb2SAntonio Ospite case 88200:
129a91c3fb2SAntonio Ospite rate_value = HIFACE_RATE_88200;
130a91c3fb2SAntonio Ospite break;
131a91c3fb2SAntonio Ospite case 96000:
132a91c3fb2SAntonio Ospite rate_value = HIFACE_RATE_96000;
133a91c3fb2SAntonio Ospite break;
134a91c3fb2SAntonio Ospite case 176400:
135a91c3fb2SAntonio Ospite rate_value = HIFACE_RATE_176400;
136a91c3fb2SAntonio Ospite break;
137a91c3fb2SAntonio Ospite case 192000:
138a91c3fb2SAntonio Ospite rate_value = HIFACE_RATE_192000;
139a91c3fb2SAntonio Ospite break;
140150116bcSMichael Trimarchi case 352800:
141150116bcSMichael Trimarchi rate_value = HIFACE_RATE_352800;
142a91c3fb2SAntonio Ospite break;
143a91c3fb2SAntonio Ospite case 384000:
144a91c3fb2SAntonio Ospite rate_value = HIFACE_RATE_384000;
145a91c3fb2SAntonio Ospite break;
146a91c3fb2SAntonio Ospite default:
147a91c3fb2SAntonio Ospite dev_err(&device->dev, "Unsupported rate %d\n", rate);
148a91c3fb2SAntonio Ospite return -EINVAL;
149a91c3fb2SAntonio Ospite }
150a91c3fb2SAntonio Ospite
151a91c3fb2SAntonio Ospite /*
152a91c3fb2SAntonio Ospite * USBIO: Vendor 0xb0(wValue=0x0043, wIndex=0x0000)
153a91c3fb2SAntonio Ospite * 43 b0 43 00 00 00 00 00
154a91c3fb2SAntonio Ospite * USBIO: Vendor 0xb0(wValue=0x004b, wIndex=0x0000)
155a91c3fb2SAntonio Ospite * 43 b0 4b 00 00 00 00 00
156a91c3fb2SAntonio Ospite * This control message doesn't have any ack from the
157a91c3fb2SAntonio Ospite * other side
158a91c3fb2SAntonio Ospite */
15910fbd979SGreg Kroah-Hartman ret = usb_control_msg_send(device, 0,
160a91c3fb2SAntonio Ospite HIFACE_SET_RATE_REQUEST,
161a91c3fb2SAntonio Ospite USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_OTHER,
16210fbd979SGreg Kroah-Hartman rate_value, 0, NULL, 0, 100, GFP_KERNEL);
16310fbd979SGreg Kroah-Hartman if (ret)
164a91c3fb2SAntonio Ospite dev_err(&device->dev, "Error setting samplerate %d.\n", rate);
165a91c3fb2SAntonio Ospite
16610fbd979SGreg Kroah-Hartman return ret;
167c31db083SOliver Neukum }
168c31db083SOliver Neukum
hiface_pcm_get_substream(struct snd_pcm_substream * alsa_sub)169a91c3fb2SAntonio Ospite static struct pcm_substream *hiface_pcm_get_substream(struct snd_pcm_substream
170a91c3fb2SAntonio Ospite *alsa_sub)
171a91c3fb2SAntonio Ospite {
172a91c3fb2SAntonio Ospite struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
173a91c3fb2SAntonio Ospite struct device *device = &rt->chip->dev->dev;
174a91c3fb2SAntonio Ospite
175a91c3fb2SAntonio Ospite if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
176a91c3fb2SAntonio Ospite return &rt->playback;
177a91c3fb2SAntonio Ospite
178a91c3fb2SAntonio Ospite dev_err(device, "Error getting pcm substream slot.\n");
179a91c3fb2SAntonio Ospite return NULL;
180a91c3fb2SAntonio Ospite }
181a91c3fb2SAntonio Ospite
182a91c3fb2SAntonio Ospite /* call with stream_mutex locked */
hiface_pcm_stream_stop(struct pcm_runtime * rt)183a91c3fb2SAntonio Ospite static void hiface_pcm_stream_stop(struct pcm_runtime *rt)
184a91c3fb2SAntonio Ospite {
185a91c3fb2SAntonio Ospite int i, time;
186a91c3fb2SAntonio Ospite
187a91c3fb2SAntonio Ospite if (rt->stream_state != STREAM_DISABLED) {
188a91c3fb2SAntonio Ospite rt->stream_state = STREAM_STOPPING;
189a91c3fb2SAntonio Ospite
190a91c3fb2SAntonio Ospite for (i = 0; i < PCM_N_URBS; i++) {
191a91c3fb2SAntonio Ospite time = usb_wait_anchor_empty_timeout(
192a91c3fb2SAntonio Ospite &rt->out_urbs[i].submitted, 100);
193a91c3fb2SAntonio Ospite if (!time)
194a91c3fb2SAntonio Ospite usb_kill_anchored_urbs(
195a91c3fb2SAntonio Ospite &rt->out_urbs[i].submitted);
196a91c3fb2SAntonio Ospite usb_kill_urb(&rt->out_urbs[i].instance);
197a91c3fb2SAntonio Ospite }
198a91c3fb2SAntonio Ospite
199a91c3fb2SAntonio Ospite rt->stream_state = STREAM_DISABLED;
200a91c3fb2SAntonio Ospite }
201a91c3fb2SAntonio Ospite }
202a91c3fb2SAntonio Ospite
203a91c3fb2SAntonio Ospite /* call with stream_mutex locked */
hiface_pcm_stream_start(struct pcm_runtime * rt)204a91c3fb2SAntonio Ospite static int hiface_pcm_stream_start(struct pcm_runtime *rt)
205a91c3fb2SAntonio Ospite {
206a91c3fb2SAntonio Ospite int ret = 0;
207a91c3fb2SAntonio Ospite int i;
208a91c3fb2SAntonio Ospite
209a91c3fb2SAntonio Ospite if (rt->stream_state == STREAM_DISABLED) {
210a91c3fb2SAntonio Ospite
211a91c3fb2SAntonio Ospite /* reset panic state when starting a new stream */
212a91c3fb2SAntonio Ospite rt->panic = false;
213a91c3fb2SAntonio Ospite
214a91c3fb2SAntonio Ospite /* submit our out urbs zero init */
215a91c3fb2SAntonio Ospite rt->stream_state = STREAM_STARTING;
216a91c3fb2SAntonio Ospite for (i = 0; i < PCM_N_URBS; i++) {
217a91c3fb2SAntonio Ospite memset(rt->out_urbs[i].buffer, 0, PCM_PACKET_SIZE);
218a91c3fb2SAntonio Ospite usb_anchor_urb(&rt->out_urbs[i].instance,
219a91c3fb2SAntonio Ospite &rt->out_urbs[i].submitted);
220a91c3fb2SAntonio Ospite ret = usb_submit_urb(&rt->out_urbs[i].instance,
221a91c3fb2SAntonio Ospite GFP_ATOMIC);
222a91c3fb2SAntonio Ospite if (ret) {
223a91c3fb2SAntonio Ospite hiface_pcm_stream_stop(rt);
224a91c3fb2SAntonio Ospite return ret;
225a91c3fb2SAntonio Ospite }
226a91c3fb2SAntonio Ospite }
227a91c3fb2SAntonio Ospite
228*4e3b8650Swangjianli /* wait for first out urb to return (sent in urb handler) */
229a91c3fb2SAntonio Ospite wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
230a91c3fb2SAntonio Ospite HZ);
231a91c3fb2SAntonio Ospite if (rt->stream_wait_cond) {
232a91c3fb2SAntonio Ospite struct device *device = &rt->chip->dev->dev;
233a91c3fb2SAntonio Ospite dev_dbg(device, "%s: Stream is running wakeup event\n",
234a91c3fb2SAntonio Ospite __func__);
235a91c3fb2SAntonio Ospite rt->stream_state = STREAM_RUNNING;
236a91c3fb2SAntonio Ospite } else {
237a91c3fb2SAntonio Ospite hiface_pcm_stream_stop(rt);
238a91c3fb2SAntonio Ospite return -EIO;
239a91c3fb2SAntonio Ospite }
240a91c3fb2SAntonio Ospite }
241a91c3fb2SAntonio Ospite return ret;
242a91c3fb2SAntonio Ospite }
243a91c3fb2SAntonio Ospite
244a91c3fb2SAntonio Ospite /* The hardware wants word-swapped 32-bit values */
memcpy_swahw32(u8 * dest,u8 * src,unsigned int n)245a91c3fb2SAntonio Ospite static void memcpy_swahw32(u8 *dest, u8 *src, unsigned int n)
246a91c3fb2SAntonio Ospite {
247a91c3fb2SAntonio Ospite unsigned int i;
248a91c3fb2SAntonio Ospite
249a91c3fb2SAntonio Ospite for (i = 0; i < n / 4; i++)
250a91c3fb2SAntonio Ospite ((u32 *)dest)[i] = swahw32(((u32 *)src)[i]);
251a91c3fb2SAntonio Ospite }
252a91c3fb2SAntonio Ospite
253a91c3fb2SAntonio Ospite /* call with substream locked */
254a91c3fb2SAntonio Ospite /* returns true if a period elapsed */
hiface_pcm_playback(struct pcm_substream * sub,struct pcm_urb * urb)255a91c3fb2SAntonio Ospite static bool hiface_pcm_playback(struct pcm_substream *sub, struct pcm_urb *urb)
256a91c3fb2SAntonio Ospite {
257a91c3fb2SAntonio Ospite struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
258a91c3fb2SAntonio Ospite struct device *device = &urb->chip->dev->dev;
259a91c3fb2SAntonio Ospite u8 *source;
260a91c3fb2SAntonio Ospite unsigned int pcm_buffer_size;
261a91c3fb2SAntonio Ospite
262a91c3fb2SAntonio Ospite WARN_ON(alsa_rt->format != SNDRV_PCM_FORMAT_S32_LE);
263a91c3fb2SAntonio Ospite
264a91c3fb2SAntonio Ospite pcm_buffer_size = snd_pcm_lib_buffer_bytes(sub->instance);
265a91c3fb2SAntonio Ospite
266a91c3fb2SAntonio Ospite if (sub->dma_off + PCM_PACKET_SIZE <= pcm_buffer_size) {
267a91c3fb2SAntonio Ospite dev_dbg(device, "%s: (1) buffer_size %#x dma_offset %#x\n", __func__,
268a91c3fb2SAntonio Ospite (unsigned int) pcm_buffer_size,
269a91c3fb2SAntonio Ospite (unsigned int) sub->dma_off);
270a91c3fb2SAntonio Ospite
271a91c3fb2SAntonio Ospite source = alsa_rt->dma_area + sub->dma_off;
272a91c3fb2SAntonio Ospite memcpy_swahw32(urb->buffer, source, PCM_PACKET_SIZE);
273a91c3fb2SAntonio Ospite } else {
274a91c3fb2SAntonio Ospite /* wrap around at end of ring buffer */
275a91c3fb2SAntonio Ospite unsigned int len;
276a91c3fb2SAntonio Ospite
277a91c3fb2SAntonio Ospite dev_dbg(device, "%s: (2) buffer_size %#x dma_offset %#x\n", __func__,
278a91c3fb2SAntonio Ospite (unsigned int) pcm_buffer_size,
279a91c3fb2SAntonio Ospite (unsigned int) sub->dma_off);
280a91c3fb2SAntonio Ospite
281a91c3fb2SAntonio Ospite len = pcm_buffer_size - sub->dma_off;
282a91c3fb2SAntonio Ospite
283a91c3fb2SAntonio Ospite source = alsa_rt->dma_area + sub->dma_off;
284a91c3fb2SAntonio Ospite memcpy_swahw32(urb->buffer, source, len);
285a91c3fb2SAntonio Ospite
286a91c3fb2SAntonio Ospite source = alsa_rt->dma_area;
287a91c3fb2SAntonio Ospite memcpy_swahw32(urb->buffer + len, source,
288a91c3fb2SAntonio Ospite PCM_PACKET_SIZE - len);
289a91c3fb2SAntonio Ospite }
290a91c3fb2SAntonio Ospite sub->dma_off += PCM_PACKET_SIZE;
291a91c3fb2SAntonio Ospite if (sub->dma_off >= pcm_buffer_size)
292a91c3fb2SAntonio Ospite sub->dma_off -= pcm_buffer_size;
293a91c3fb2SAntonio Ospite
294a91c3fb2SAntonio Ospite sub->period_off += PCM_PACKET_SIZE;
295a91c3fb2SAntonio Ospite if (sub->period_off >= alsa_rt->period_size) {
296a91c3fb2SAntonio Ospite sub->period_off %= alsa_rt->period_size;
297a91c3fb2SAntonio Ospite return true;
298a91c3fb2SAntonio Ospite }
299a91c3fb2SAntonio Ospite return false;
300a91c3fb2SAntonio Ospite }
301a91c3fb2SAntonio Ospite
hiface_pcm_out_urb_handler(struct urb * usb_urb)302a91c3fb2SAntonio Ospite static void hiface_pcm_out_urb_handler(struct urb *usb_urb)
303a91c3fb2SAntonio Ospite {
304a91c3fb2SAntonio Ospite struct pcm_urb *out_urb = usb_urb->context;
305a91c3fb2SAntonio Ospite struct pcm_runtime *rt = out_urb->chip->pcm;
306a91c3fb2SAntonio Ospite struct pcm_substream *sub;
307a91c3fb2SAntonio Ospite bool do_period_elapsed = false;
308a91c3fb2SAntonio Ospite unsigned long flags;
309a91c3fb2SAntonio Ospite int ret;
310a91c3fb2SAntonio Ospite
311a91c3fb2SAntonio Ospite if (rt->panic || rt->stream_state == STREAM_STOPPING)
312a91c3fb2SAntonio Ospite return;
313a91c3fb2SAntonio Ospite
314a91c3fb2SAntonio Ospite if (unlikely(usb_urb->status == -ENOENT || /* unlinked */
315a91c3fb2SAntonio Ospite usb_urb->status == -ENODEV || /* device removed */
316a91c3fb2SAntonio Ospite usb_urb->status == -ECONNRESET || /* unlinked */
317a91c3fb2SAntonio Ospite usb_urb->status == -ESHUTDOWN)) { /* device disabled */
318a91c3fb2SAntonio Ospite goto out_fail;
319a91c3fb2SAntonio Ospite }
320a91c3fb2SAntonio Ospite
321a91c3fb2SAntonio Ospite if (rt->stream_state == STREAM_STARTING) {
322a91c3fb2SAntonio Ospite rt->stream_wait_cond = true;
323a91c3fb2SAntonio Ospite wake_up(&rt->stream_wait_queue);
324a91c3fb2SAntonio Ospite }
325a91c3fb2SAntonio Ospite
326a91c3fb2SAntonio Ospite /* now send our playback data (if a free out urb was found) */
327a91c3fb2SAntonio Ospite sub = &rt->playback;
328a91c3fb2SAntonio Ospite spin_lock_irqsave(&sub->lock, flags);
329a91c3fb2SAntonio Ospite if (sub->active)
330a91c3fb2SAntonio Ospite do_period_elapsed = hiface_pcm_playback(sub, out_urb);
331a91c3fb2SAntonio Ospite else
332a91c3fb2SAntonio Ospite memset(out_urb->buffer, 0, PCM_PACKET_SIZE);
333a91c3fb2SAntonio Ospite
334a91c3fb2SAntonio Ospite spin_unlock_irqrestore(&sub->lock, flags);
335a91c3fb2SAntonio Ospite
336a91c3fb2SAntonio Ospite if (do_period_elapsed)
337a91c3fb2SAntonio Ospite snd_pcm_period_elapsed(sub->instance);
338a91c3fb2SAntonio Ospite
339a91c3fb2SAntonio Ospite ret = usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
340a91c3fb2SAntonio Ospite if (ret < 0)
341a91c3fb2SAntonio Ospite goto out_fail;
342a91c3fb2SAntonio Ospite
343a91c3fb2SAntonio Ospite return;
344a91c3fb2SAntonio Ospite
345a91c3fb2SAntonio Ospite out_fail:
346a91c3fb2SAntonio Ospite rt->panic = true;
347a91c3fb2SAntonio Ospite }
348a91c3fb2SAntonio Ospite
hiface_pcm_open(struct snd_pcm_substream * alsa_sub)349a91c3fb2SAntonio Ospite static int hiface_pcm_open(struct snd_pcm_substream *alsa_sub)
350a91c3fb2SAntonio Ospite {
351a91c3fb2SAntonio Ospite struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
352a91c3fb2SAntonio Ospite struct pcm_substream *sub = NULL;
353a91c3fb2SAntonio Ospite struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
354a91c3fb2SAntonio Ospite int ret;
355a91c3fb2SAntonio Ospite
356a91c3fb2SAntonio Ospite if (rt->panic)
357a91c3fb2SAntonio Ospite return -EPIPE;
358a91c3fb2SAntonio Ospite
359a91c3fb2SAntonio Ospite mutex_lock(&rt->stream_mutex);
360a91c3fb2SAntonio Ospite alsa_rt->hw = pcm_hw;
361a91c3fb2SAntonio Ospite
362a91c3fb2SAntonio Ospite if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
363a91c3fb2SAntonio Ospite sub = &rt->playback;
364a91c3fb2SAntonio Ospite
365a91c3fb2SAntonio Ospite if (!sub) {
366a91c3fb2SAntonio Ospite struct device *device = &rt->chip->dev->dev;
367a91c3fb2SAntonio Ospite mutex_unlock(&rt->stream_mutex);
368a91c3fb2SAntonio Ospite dev_err(device, "Invalid stream type\n");
369a91c3fb2SAntonio Ospite return -EINVAL;
370a91c3fb2SAntonio Ospite }
371a91c3fb2SAntonio Ospite
372a91c3fb2SAntonio Ospite if (rt->extra_freq) {
373a91c3fb2SAntonio Ospite alsa_rt->hw.rates |= SNDRV_PCM_RATE_KNOT;
374a91c3fb2SAntonio Ospite alsa_rt->hw.rate_max = 384000;
375a91c3fb2SAntonio Ospite
376a91c3fb2SAntonio Ospite /* explicit constraints needed as we added SNDRV_PCM_RATE_KNOT */
377a91c3fb2SAntonio Ospite ret = snd_pcm_hw_constraint_list(alsa_sub->runtime, 0,
378a91c3fb2SAntonio Ospite SNDRV_PCM_HW_PARAM_RATE,
379a91c3fb2SAntonio Ospite &constraints_extra_rates);
380a91c3fb2SAntonio Ospite if (ret < 0) {
381a91c3fb2SAntonio Ospite mutex_unlock(&rt->stream_mutex);
382a91c3fb2SAntonio Ospite return ret;
383a91c3fb2SAntonio Ospite }
384a91c3fb2SAntonio Ospite }
385a91c3fb2SAntonio Ospite
386a91c3fb2SAntonio Ospite sub->instance = alsa_sub;
387a91c3fb2SAntonio Ospite sub->active = false;
388a91c3fb2SAntonio Ospite mutex_unlock(&rt->stream_mutex);
389a91c3fb2SAntonio Ospite return 0;
390a91c3fb2SAntonio Ospite }
391a91c3fb2SAntonio Ospite
hiface_pcm_close(struct snd_pcm_substream * alsa_sub)392a91c3fb2SAntonio Ospite static int hiface_pcm_close(struct snd_pcm_substream *alsa_sub)
393a91c3fb2SAntonio Ospite {
394a91c3fb2SAntonio Ospite struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
395a91c3fb2SAntonio Ospite struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
396a91c3fb2SAntonio Ospite unsigned long flags;
397a91c3fb2SAntonio Ospite
398a91c3fb2SAntonio Ospite if (rt->panic)
399a91c3fb2SAntonio Ospite return 0;
400a91c3fb2SAntonio Ospite
401a91c3fb2SAntonio Ospite mutex_lock(&rt->stream_mutex);
402a91c3fb2SAntonio Ospite if (sub) {
403a91c3fb2SAntonio Ospite hiface_pcm_stream_stop(rt);
404a91c3fb2SAntonio Ospite
405a91c3fb2SAntonio Ospite /* deactivate substream */
406a91c3fb2SAntonio Ospite spin_lock_irqsave(&sub->lock, flags);
407a91c3fb2SAntonio Ospite sub->instance = NULL;
408a91c3fb2SAntonio Ospite sub->active = false;
409a91c3fb2SAntonio Ospite spin_unlock_irqrestore(&sub->lock, flags);
410a91c3fb2SAntonio Ospite
411a91c3fb2SAntonio Ospite }
412a91c3fb2SAntonio Ospite mutex_unlock(&rt->stream_mutex);
413a91c3fb2SAntonio Ospite return 0;
414a91c3fb2SAntonio Ospite }
415a91c3fb2SAntonio Ospite
hiface_pcm_prepare(struct snd_pcm_substream * alsa_sub)416a91c3fb2SAntonio Ospite static int hiface_pcm_prepare(struct snd_pcm_substream *alsa_sub)
417a91c3fb2SAntonio Ospite {
418a91c3fb2SAntonio Ospite struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
419a91c3fb2SAntonio Ospite struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
420a91c3fb2SAntonio Ospite struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
421a91c3fb2SAntonio Ospite int ret;
422a91c3fb2SAntonio Ospite
423a91c3fb2SAntonio Ospite if (rt->panic)
424a91c3fb2SAntonio Ospite return -EPIPE;
425a91c3fb2SAntonio Ospite if (!sub)
426a91c3fb2SAntonio Ospite return -ENODEV;
427a91c3fb2SAntonio Ospite
428a91c3fb2SAntonio Ospite mutex_lock(&rt->stream_mutex);
429a91c3fb2SAntonio Ospite
430995c6a7fSJussi Laako hiface_pcm_stream_stop(rt);
431995c6a7fSJussi Laako
432a91c3fb2SAntonio Ospite sub->dma_off = 0;
433a91c3fb2SAntonio Ospite sub->period_off = 0;
434a91c3fb2SAntonio Ospite
435a91c3fb2SAntonio Ospite if (rt->stream_state == STREAM_DISABLED) {
436a91c3fb2SAntonio Ospite
437a91c3fb2SAntonio Ospite ret = hiface_pcm_set_rate(rt, alsa_rt->rate);
438a91c3fb2SAntonio Ospite if (ret) {
439a91c3fb2SAntonio Ospite mutex_unlock(&rt->stream_mutex);
440a91c3fb2SAntonio Ospite return ret;
441a91c3fb2SAntonio Ospite }
442a91c3fb2SAntonio Ospite ret = hiface_pcm_stream_start(rt);
443a91c3fb2SAntonio Ospite if (ret) {
444a91c3fb2SAntonio Ospite mutex_unlock(&rt->stream_mutex);
445a91c3fb2SAntonio Ospite return ret;
446a91c3fb2SAntonio Ospite }
447a91c3fb2SAntonio Ospite }
448a91c3fb2SAntonio Ospite mutex_unlock(&rt->stream_mutex);
449a91c3fb2SAntonio Ospite return 0;
450a91c3fb2SAntonio Ospite }
451a91c3fb2SAntonio Ospite
hiface_pcm_trigger(struct snd_pcm_substream * alsa_sub,int cmd)452a91c3fb2SAntonio Ospite static int hiface_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
453a91c3fb2SAntonio Ospite {
454a91c3fb2SAntonio Ospite struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
455a91c3fb2SAntonio Ospite struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
456a91c3fb2SAntonio Ospite
457a91c3fb2SAntonio Ospite if (rt->panic)
458a91c3fb2SAntonio Ospite return -EPIPE;
459a91c3fb2SAntonio Ospite if (!sub)
460a91c3fb2SAntonio Ospite return -ENODEV;
461a91c3fb2SAntonio Ospite
462a91c3fb2SAntonio Ospite switch (cmd) {
463a91c3fb2SAntonio Ospite case SNDRV_PCM_TRIGGER_START:
464a91c3fb2SAntonio Ospite case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
465a91c3fb2SAntonio Ospite spin_lock_irq(&sub->lock);
466a91c3fb2SAntonio Ospite sub->active = true;
467a91c3fb2SAntonio Ospite spin_unlock_irq(&sub->lock);
468a91c3fb2SAntonio Ospite return 0;
469a91c3fb2SAntonio Ospite
470a91c3fb2SAntonio Ospite case SNDRV_PCM_TRIGGER_STOP:
471a91c3fb2SAntonio Ospite case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
472a91c3fb2SAntonio Ospite spin_lock_irq(&sub->lock);
473a91c3fb2SAntonio Ospite sub->active = false;
474a91c3fb2SAntonio Ospite spin_unlock_irq(&sub->lock);
475a91c3fb2SAntonio Ospite return 0;
476a91c3fb2SAntonio Ospite
477a91c3fb2SAntonio Ospite default:
478a91c3fb2SAntonio Ospite return -EINVAL;
479a91c3fb2SAntonio Ospite }
480a91c3fb2SAntonio Ospite }
481a91c3fb2SAntonio Ospite
hiface_pcm_pointer(struct snd_pcm_substream * alsa_sub)482a91c3fb2SAntonio Ospite static snd_pcm_uframes_t hiface_pcm_pointer(struct snd_pcm_substream *alsa_sub)
483a91c3fb2SAntonio Ospite {
484a91c3fb2SAntonio Ospite struct pcm_substream *sub = hiface_pcm_get_substream(alsa_sub);
485a91c3fb2SAntonio Ospite struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
486a91c3fb2SAntonio Ospite unsigned long flags;
487a91c3fb2SAntonio Ospite snd_pcm_uframes_t dma_offset;
488a91c3fb2SAntonio Ospite
489a91c3fb2SAntonio Ospite if (rt->panic || !sub)
490fee4b700SEldad Zack return SNDRV_PCM_POS_XRUN;
491a91c3fb2SAntonio Ospite
492a91c3fb2SAntonio Ospite spin_lock_irqsave(&sub->lock, flags);
493a91c3fb2SAntonio Ospite dma_offset = sub->dma_off;
494a91c3fb2SAntonio Ospite spin_unlock_irqrestore(&sub->lock, flags);
495a91c3fb2SAntonio Ospite return bytes_to_frames(alsa_sub->runtime, dma_offset);
496a91c3fb2SAntonio Ospite }
497a91c3fb2SAntonio Ospite
49831cb1fb4SArvind Yadav static const struct snd_pcm_ops pcm_ops = {
499a91c3fb2SAntonio Ospite .open = hiface_pcm_open,
500a91c3fb2SAntonio Ospite .close = hiface_pcm_close,
501a91c3fb2SAntonio Ospite .prepare = hiface_pcm_prepare,
502a91c3fb2SAntonio Ospite .trigger = hiface_pcm_trigger,
503a91c3fb2SAntonio Ospite .pointer = hiface_pcm_pointer,
504a91c3fb2SAntonio Ospite };
505a91c3fb2SAntonio Ospite
hiface_pcm_init_urb(struct pcm_urb * urb,struct hiface_chip * chip,unsigned int ep,void (* handler)(struct urb *))506a91c3fb2SAntonio Ospite static int hiface_pcm_init_urb(struct pcm_urb *urb,
507a91c3fb2SAntonio Ospite struct hiface_chip *chip,
508a91c3fb2SAntonio Ospite unsigned int ep,
509a91c3fb2SAntonio Ospite void (*handler)(struct urb *))
510a91c3fb2SAntonio Ospite {
511a91c3fb2SAntonio Ospite urb->chip = chip;
512a91c3fb2SAntonio Ospite usb_init_urb(&urb->instance);
513a91c3fb2SAntonio Ospite
514a91c3fb2SAntonio Ospite urb->buffer = kzalloc(PCM_PACKET_SIZE, GFP_KERNEL);
515a91c3fb2SAntonio Ospite if (!urb->buffer)
516a91c3fb2SAntonio Ospite return -ENOMEM;
517a91c3fb2SAntonio Ospite
518a91c3fb2SAntonio Ospite usb_fill_bulk_urb(&urb->instance, chip->dev,
519a91c3fb2SAntonio Ospite usb_sndbulkpipe(chip->dev, ep), (void *)urb->buffer,
520a91c3fb2SAntonio Ospite PCM_PACKET_SIZE, handler, urb);
5215935b952STakashi Iwai if (usb_urb_ep_type_check(&urb->instance))
5225935b952STakashi Iwai return -EINVAL;
523a91c3fb2SAntonio Ospite init_usb_anchor(&urb->submitted);
524a91c3fb2SAntonio Ospite
525a91c3fb2SAntonio Ospite return 0;
526a91c3fb2SAntonio Ospite }
527a91c3fb2SAntonio Ospite
hiface_pcm_abort(struct hiface_chip * chip)528a91c3fb2SAntonio Ospite void hiface_pcm_abort(struct hiface_chip *chip)
529a91c3fb2SAntonio Ospite {
530a91c3fb2SAntonio Ospite struct pcm_runtime *rt = chip->pcm;
531a91c3fb2SAntonio Ospite
532a91c3fb2SAntonio Ospite if (rt) {
533a91c3fb2SAntonio Ospite rt->panic = true;
534a91c3fb2SAntonio Ospite
535a91c3fb2SAntonio Ospite mutex_lock(&rt->stream_mutex);
536a91c3fb2SAntonio Ospite hiface_pcm_stream_stop(rt);
537a91c3fb2SAntonio Ospite mutex_unlock(&rt->stream_mutex);
538a91c3fb2SAntonio Ospite }
539a91c3fb2SAntonio Ospite }
540a91c3fb2SAntonio Ospite
hiface_pcm_destroy(struct hiface_chip * chip)541a91c3fb2SAntonio Ospite static void hiface_pcm_destroy(struct hiface_chip *chip)
542a91c3fb2SAntonio Ospite {
543a91c3fb2SAntonio Ospite struct pcm_runtime *rt = chip->pcm;
544a91c3fb2SAntonio Ospite int i;
545a91c3fb2SAntonio Ospite
546a91c3fb2SAntonio Ospite for (i = 0; i < PCM_N_URBS; i++)
547a91c3fb2SAntonio Ospite kfree(rt->out_urbs[i].buffer);
548a91c3fb2SAntonio Ospite
549a91c3fb2SAntonio Ospite kfree(chip->pcm);
550a91c3fb2SAntonio Ospite chip->pcm = NULL;
551a91c3fb2SAntonio Ospite }
552a91c3fb2SAntonio Ospite
hiface_pcm_free(struct snd_pcm * pcm)553a91c3fb2SAntonio Ospite static void hiface_pcm_free(struct snd_pcm *pcm)
554a91c3fb2SAntonio Ospite {
555a91c3fb2SAntonio Ospite struct pcm_runtime *rt = pcm->private_data;
556a91c3fb2SAntonio Ospite
557a91c3fb2SAntonio Ospite if (rt)
558a91c3fb2SAntonio Ospite hiface_pcm_destroy(rt->chip);
559a91c3fb2SAntonio Ospite }
560a91c3fb2SAntonio Ospite
hiface_pcm_init(struct hiface_chip * chip,u8 extra_freq)561a91c3fb2SAntonio Ospite int hiface_pcm_init(struct hiface_chip *chip, u8 extra_freq)
562a91c3fb2SAntonio Ospite {
563a91c3fb2SAntonio Ospite int i;
564a91c3fb2SAntonio Ospite int ret;
565a91c3fb2SAntonio Ospite struct snd_pcm *pcm;
566a91c3fb2SAntonio Ospite struct pcm_runtime *rt;
567a91c3fb2SAntonio Ospite
568a91c3fb2SAntonio Ospite rt = kzalloc(sizeof(*rt), GFP_KERNEL);
569a91c3fb2SAntonio Ospite if (!rt)
570a91c3fb2SAntonio Ospite return -ENOMEM;
571a91c3fb2SAntonio Ospite
572a91c3fb2SAntonio Ospite rt->chip = chip;
573a91c3fb2SAntonio Ospite rt->stream_state = STREAM_DISABLED;
574a91c3fb2SAntonio Ospite if (extra_freq)
575a91c3fb2SAntonio Ospite rt->extra_freq = 1;
576a91c3fb2SAntonio Ospite
577a91c3fb2SAntonio Ospite init_waitqueue_head(&rt->stream_wait_queue);
578a91c3fb2SAntonio Ospite mutex_init(&rt->stream_mutex);
579a91c3fb2SAntonio Ospite spin_lock_init(&rt->playback.lock);
580a91c3fb2SAntonio Ospite
5815935b952STakashi Iwai for (i = 0; i < PCM_N_URBS; i++) {
5825935b952STakashi Iwai ret = hiface_pcm_init_urb(&rt->out_urbs[i], chip, OUT_EP,
583a91c3fb2SAntonio Ospite hiface_pcm_out_urb_handler);
5845935b952STakashi Iwai if (ret < 0)
5853d92aa45SWenwen Wang goto error;
5865935b952STakashi Iwai }
587a91c3fb2SAntonio Ospite
588a91c3fb2SAntonio Ospite ret = snd_pcm_new(chip->card, "USB-SPDIF Audio", 0, 1, 0, &pcm);
589a91c3fb2SAntonio Ospite if (ret < 0) {
590a91c3fb2SAntonio Ospite dev_err(&chip->dev->dev, "Cannot create pcm instance\n");
5913d92aa45SWenwen Wang goto error;
592a91c3fb2SAntonio Ospite }
593a91c3fb2SAntonio Ospite
594a91c3fb2SAntonio Ospite pcm->private_data = rt;
595a91c3fb2SAntonio Ospite pcm->private_free = hiface_pcm_free;
596a91c3fb2SAntonio Ospite
59775b1a8f9SJoe Perches strscpy(pcm->name, "USB-SPDIF Audio", sizeof(pcm->name));
598a91c3fb2SAntonio Ospite snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
599d0aa558cSTakashi Iwai snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
600d7867ee7STakashi Iwai NULL, 0, 0);
601a91c3fb2SAntonio Ospite
602a91c3fb2SAntonio Ospite rt->instance = pcm;
603a91c3fb2SAntonio Ospite
604a91c3fb2SAntonio Ospite chip->pcm = rt;
605a91c3fb2SAntonio Ospite return 0;
6063d92aa45SWenwen Wang
6073d92aa45SWenwen Wang error:
6083d92aa45SWenwen Wang for (i = 0; i < PCM_N_URBS; i++)
6093d92aa45SWenwen Wang kfree(rt->out_urbs[i].buffer);
6103d92aa45SWenwen Wang kfree(rt);
6113d92aa45SWenwen Wang return ret;
612a91c3fb2SAntonio Ospite }
613