brcm2708: add linux 4.19 support
[oweals/openwrt.git] / target / linux / brcm2708 / patches-4.19 / 950-0459-staging-bcm2835-audio-Operate-non-atomic-PCM-ops.patch
1 From 8f5871c73ba767c9443eb02c4ca0cb7df56982e8 Mon Sep 17 00:00:00 2001
2 From: Takashi Iwai <tiwai@suse.de>
3 Date: Tue, 4 Sep 2018 17:58:49 +0200
4 Subject: [PATCH 459/703] staging: bcm2835-audio: Operate non-atomic PCM ops
5
6 commit 5c7883e5f27e829f3f3a2ba174d4a724bfd5f026 upstream.
7
8 This is the most significant part in the patch series.
9
10 The bcm2835-audio driver used to queue the commands to vc04 core via
11 workqueue, but basically the whole accesses to vc04 core are done in
12 the sleepable context, including the callback calls.  In such a case,
13 rewriting the code using non-atomic PCM ops will simplify the logic a
14 lot.
15
16 This patch does it: all workqueue are gone and each former-work
17 implementation is now directly called from PCM ops like trigger and
18 write transfer.
19
20 Along with it, the DMA position updater, bcm2835_playback_fifo(), was
21 also rewritten to use a simpler logic.  Now it handles the XRUN and
22 draining properly by calling snd_pcm_stop() conditionally.
23
24 The current position is kept in atomic_t value so that it can be read
25 concurrently from the pointer callback.
26
27 Also, the bcm2835_audio_instance object is allocated at the beginning
28 of bcm2835_audio_open().  This makes the resource management clearer.
29
30 Signed-off-by: Takashi Iwai <tiwai@suse.de>
31 Tested-by: Stefan Wahren <stefan.wahren@i2se.com>
32 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
33 ---
34  .../vc04_services/bcm2835-audio/bcm2835-pcm.c |  74 +++---
35  .../bcm2835-audio/bcm2835-vchiq.c             | 244 +++---------------
36  .../vc04_services/bcm2835-audio/bcm2835.h     |   9 +-
37  3 files changed, 82 insertions(+), 245 deletions(-)
38
39 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c
40 +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-pcm.c
41 @@ -11,7 +11,8 @@
42  /* hardware definition */
43  static const struct snd_pcm_hardware snd_bcm2835_playback_hw = {
44         .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
45 -       SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
46 +                SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
47 +                SNDRV_PCM_INFO_DRAIN_TRIGGER),
48         .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
49         .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
50         .rate_min = 8000,
51 @@ -27,7 +28,8 @@ static const struct snd_pcm_hardware snd
52  
53  static const struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
54         .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
55 -       SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
56 +                SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
57 +                SNDRV_PCM_INFO_DRAIN_TRIGGER),
58         .formats = SNDRV_PCM_FMTBIT_S16_LE,
59         .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
60         SNDRV_PCM_RATE_48000,
61 @@ -47,42 +49,34 @@ static void snd_bcm2835_playback_free(st
62         kfree(runtime->private_data);
63  }
64  
65 -void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream)
66 +void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream,
67 +                          unsigned int bytes)
68  {
69 -       unsigned int consumed = 0;
70 -       int new_period = 0;
71 +       struct snd_pcm_substream *substream = alsa_stream->substream;
72 +       unsigned int pos;
73  
74 -       audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
75 -               alsa_stream ? alsa_stream->substream : 0);
76 +       if (!alsa_stream->period_size)
77 +               return;
78  
79 -       consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
80 -
81 -       /* We get called only if playback was triggered, So, the number of buffers we retrieve in
82 -        * each iteration are the buffers that have been played out already
83 -        */
84 -
85 -       if (alsa_stream->period_size) {
86 -               if ((alsa_stream->pos / alsa_stream->period_size) !=
87 -                       ((alsa_stream->pos + consumed) / alsa_stream->period_size))
88 -                       new_period = 1;
89 -       }
90 -       audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
91 -               alsa_stream->pos,
92 -               consumed,
93 -               alsa_stream->buffer_size,
94 -               (int) (alsa_stream->period_size * alsa_stream->substream->runtime->periods),
95 -               frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
96 -               new_period);
97 -       if (alsa_stream->buffer_size) {
98 -               alsa_stream->pos += consumed & ~(1 << 30);
99 -               alsa_stream->pos %= alsa_stream->buffer_size;
100 +       if (bytes >= alsa_stream->buffer_size) {
101 +               snd_pcm_stream_lock(substream);
102 +               snd_pcm_stop(substream,
103 +                            alsa_stream->draining ?
104 +                            SNDRV_PCM_STATE_SETUP :
105 +                            SNDRV_PCM_STATE_XRUN);
106 +               snd_pcm_stream_unlock(substream);
107 +               return;
108         }
109  
110 -       if (alsa_stream->substream) {
111 -               if (new_period)
112 -                       snd_pcm_period_elapsed(alsa_stream->substream);
113 -       } else {
114 -               audio_warning(" unexpected NULL substream\n");
115 +       pos = atomic_read(&alsa_stream->pos);
116 +       pos += bytes;
117 +       pos %= alsa_stream->buffer_size;
118 +       atomic_set(&alsa_stream->pos, pos);
119 +
120 +       alsa_stream->period_offset += bytes;
121 +       if (alsa_stream->period_offset >= alsa_stream->period_size) {
122 +               alsa_stream->period_offset %= alsa_stream->period_size;
123 +               snd_pcm_period_elapsed(substream);
124         }
125  }
126  
127 @@ -246,7 +240,8 @@ static int snd_bcm2835_pcm_prepare(struc
128  
129         alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
130         alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
131 -       alsa_stream->pos = 0;
132 +       atomic_set(&alsa_stream->pos, 0);
133 +       alsa_stream->period_offset = 0;
134         alsa_stream->draining = false;
135  
136         return 0;
137 @@ -283,7 +278,7 @@ static int snd_bcm2835_pcm_trigger(struc
138                 return bcm2835_audio_start(alsa_stream);
139         case SNDRV_PCM_TRIGGER_DRAIN:
140                 alsa_stream->draining = true;
141 -               return 0;
142 +               return bcm2835_audio_drain(alsa_stream);
143         case SNDRV_PCM_TRIGGER_STOP:
144                 return bcm2835_audio_stop(alsa_stream);
145         default:
146 @@ -300,7 +295,7 @@ snd_bcm2835_pcm_pointer(struct snd_pcm_s
147  
148         return snd_pcm_indirect_playback_pointer(substream,
149                 &alsa_stream->pcm_indirect,
150 -               alsa_stream->pos);
151 +               atomic_read(&alsa_stream->pos));
152  }
153  
154  /* operators */
155 @@ -338,6 +333,7 @@ int snd_bcm2835_new_pcm(struct bcm2835_c
156         if (err < 0)
157                 return err;
158         pcm->private_data = chip;
159 +       pcm->nonatomic = true;
160         strcpy(pcm->name, "bcm2835 ALSA");
161         chip->pcm = pcm;
162         chip->dest = AUDIO_DEST_AUTO;
163 @@ -367,6 +363,7 @@ int snd_bcm2835_new_spdif_pcm(struct bcm
164                 return err;
165  
166         pcm->private_data = chip;
167 +       pcm->nonatomic = true;
168         strcpy(pcm->name, "bcm2835 IEC958/HDMI");
169         chip->pcm_spdif = pcm;
170         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
171 @@ -395,6 +392,7 @@ int snd_bcm2835_new_simple_pcm(struct bc
172                 return err;
173  
174         pcm->private_data = chip;
175 +       pcm->nonatomic = true;
176         strcpy(pcm->name, name);
177         chip->pcm = pcm;
178         chip->dest = route;
179 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c
180 +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835-vchiq.c
181 @@ -26,10 +26,6 @@
182  
183  /* ---- Private Constants and Types ------------------------------------------ */
184  
185 -#define BCM2835_AUDIO_STOP           0
186 -#define BCM2835_AUDIO_START          1
187 -#define BCM2835_AUDIO_WRITE          2
188 -
189  /* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
190  #ifdef AUDIO_DEBUG_ENABLE
191  #define LOG_ERR(fmt, arg...)   pr_err("%s:%d " fmt, __func__, __LINE__, ##arg)
192 @@ -55,17 +51,6 @@ struct bcm2835_audio_instance {
193  
194  static bool force_bulk;
195  
196 -/* ---- Private Variables ---------------------------------------------------- */
197 -
198 -/* ---- Private Function Prototypes ------------------------------------------ */
199 -
200 -/* ---- Private Functions ---------------------------------------------------- */
201 -
202 -static int bcm2835_audio_stop_worker(struct bcm2835_alsa_stream *alsa_stream);
203 -static int bcm2835_audio_start_worker(struct bcm2835_alsa_stream *alsa_stream);
204 -static int bcm2835_audio_write_worker(struct bcm2835_alsa_stream *alsa_stream,
205 -                                     unsigned int count, void *src);
206 -
207  static void bcm2835_audio_lock(struct bcm2835_audio_instance *instance)
208  {
209         mutex_lock(&instance->vchi_mutex);
210 @@ -135,108 +120,6 @@ static const u32 BCM2835_AUDIO_WRITE_COO
211  static const u32 BCM2835_AUDIO_WRITE_COOKIE2 = ('D' << 24 | 'A' << 16 |
212                                                 'T' << 8  | 'A');
213  
214 -struct bcm2835_audio_work {
215 -       struct work_struct my_work;
216 -       struct bcm2835_alsa_stream *alsa_stream;
217 -       int cmd;
218 -       void *src;
219 -       unsigned int count;
220 -};
221 -
222 -static void my_wq_function(struct work_struct *work)
223 -{
224 -       struct bcm2835_audio_work *w =
225 -               container_of(work, struct bcm2835_audio_work, my_work);
226 -       int ret = -9;
227 -
228 -       switch (w->cmd) {
229 -       case BCM2835_AUDIO_START:
230 -               ret = bcm2835_audio_start_worker(w->alsa_stream);
231 -               break;
232 -       case BCM2835_AUDIO_STOP:
233 -               ret = bcm2835_audio_stop_worker(w->alsa_stream);
234 -               break;
235 -       case BCM2835_AUDIO_WRITE:
236 -               ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
237 -                                                w->src);
238 -               break;
239 -       default:
240 -               LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
241 -               break;
242 -       }
243 -       kfree((void *)work);
244 -}
245 -
246 -int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream)
247 -{
248 -       struct bcm2835_audio_work *work;
249 -
250 -       work = kmalloc(sizeof(*work), GFP_ATOMIC);
251 -       /*--- Queue some work (item 1) ---*/
252 -       if (!work) {
253 -               LOG_ERR(" .. Error: NULL work kmalloc\n");
254 -               return -ENOMEM;
255 -       }
256 -       INIT_WORK(&work->my_work, my_wq_function);
257 -       work->alsa_stream = alsa_stream;
258 -       work->cmd = BCM2835_AUDIO_START;
259 -       if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
260 -               kfree(work);
261 -               return -EBUSY;
262 -       }
263 -       return 0;
264 -}
265 -
266 -int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream)
267 -{
268 -       struct bcm2835_audio_work *work;
269 -
270 -       work = kmalloc(sizeof(*work), GFP_ATOMIC);
271 -       /*--- Queue some work (item 1) ---*/
272 -       if (!work) {
273 -               LOG_ERR(" .. Error: NULL work kmalloc\n");
274 -               return -ENOMEM;
275 -       }
276 -       INIT_WORK(&work->my_work, my_wq_function);
277 -       work->alsa_stream = alsa_stream;
278 -       work->cmd = BCM2835_AUDIO_STOP;
279 -       if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
280 -               kfree(work);
281 -               return -EBUSY;
282 -       }
283 -       return 0;
284 -}
285 -
286 -int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
287 -                       unsigned int count, void *src)
288 -{
289 -       struct bcm2835_audio_work *work;
290 -
291 -       work = kmalloc(sizeof(*work), GFP_ATOMIC);
292 -       /*--- Queue some work (item 1) ---*/
293 -       if (!work) {
294 -               LOG_ERR(" .. Error: NULL work kmalloc\n");
295 -               return -ENOMEM;
296 -       }
297 -       INIT_WORK(&work->my_work, my_wq_function);
298 -       work->alsa_stream = alsa_stream;
299 -       work->cmd = BCM2835_AUDIO_WRITE;
300 -       work->src = src;
301 -       work->count = count;
302 -       if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
303 -               kfree(work);
304 -               return -EBUSY;
305 -       }
306 -       return 0;
307 -}
308 -
309 -static void my_workqueue_quit(struct bcm2835_alsa_stream *alsa_stream)
310 -{
311 -       flush_workqueue(alsa_stream->my_wq);
312 -       destroy_workqueue(alsa_stream->my_wq);
313 -       alsa_stream->my_wq = NULL;
314 -}
315 -
316  static void audio_vchi_callback(void *param,
317                                 const VCHI_CALLBACK_REASON_T reason,
318                                 void *msg_handle)
319 @@ -249,47 +132,27 @@ static void audio_vchi_callback(void *pa
320         if (reason != VCHI_CALLBACK_MSG_AVAILABLE)
321                 return;
322  
323 -       if (!instance) {
324 -               LOG_ERR(" .. instance is null\n");
325 -               BUG();
326 -               return;
327 -       }
328 -       if (!instance->vchi_handle) {
329 -               LOG_ERR(" .. instance->vchi_handle is null\n");
330 -               BUG();
331 -               return;
332 -       }
333         status = vchi_msg_dequeue(instance->vchi_handle,
334                                   &m, sizeof(m), &msg_len, VCHI_FLAGS_NONE);
335         if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
336 -               LOG_DBG(" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
337 -                       instance, m.u.result.success);
338                 instance->result = m.u.result.success;
339                 complete(&instance->msg_avail_comp);
340         } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
341 -               struct bcm2835_alsa_stream *alsa_stream = instance->alsa_stream;
342 -
343 -               LOG_DBG(" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
344 -                       instance, m.u.complete.count);
345                 if (m.u.complete.cookie1 != BCM2835_AUDIO_WRITE_COOKIE1 ||
346                     m.u.complete.cookie2 != BCM2835_AUDIO_WRITE_COOKIE2)
347 -                       LOG_ERR(" .. response is corrupt\n");
348 -               else if (alsa_stream) {
349 -                       atomic_add(m.u.complete.count,
350 -                                  &alsa_stream->retrieved);
351 -                       bcm2835_playback_fifo(alsa_stream);
352 -               } else {
353 -                       LOG_ERR(" .. unexpected alsa_stream=%p\n",
354 -                               alsa_stream);
355 -               }
356 +                       LOG_ERR("invalid cookie\n");
357 +               else
358 +                       bcm2835_playback_fifo(instance->alsa_stream,
359 +                                             m.u.complete.count);
360         } else {
361 -               LOG_ERR(" .. unexpected m.type=%d\n", m.type);
362 +               LOG_ERR("unexpected callback type=%d\n", m.type);
363         }
364  }
365  
366 -static struct bcm2835_audio_instance *
367 +static int
368  vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
369 -                  VCHI_CONNECTION_T *vchi_connection)
370 +                  VCHI_CONNECTION_T *vchi_connection,
371 +                  struct bcm2835_audio_instance *instance)
372  {
373         SERVICE_CREATION_T params = {
374                 .version                = VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
375 @@ -298,23 +161,14 @@ vc_vchi_audio_init(VCHI_INSTANCE_T vchi_
376                 .rx_fifo_size           = 0,
377                 .tx_fifo_size           = 0,
378                 .callback               = audio_vchi_callback,
379 +               .callback_param         = instance,
380                 .want_unaligned_bulk_rx = 1, //TODO: remove VCOS_FALSE
381                 .want_unaligned_bulk_tx = 1, //TODO: remove VCOS_FALSE
382                 .want_crc               = 0
383         };
384 -       struct bcm2835_audio_instance *instance;
385         int status;
386  
387 -       /* Allocate memory for this instance */
388 -       instance = kzalloc(sizeof(*instance), GFP_KERNEL);
389 -       if (!instance)
390 -               return ERR_PTR(-ENOMEM);
391 -
392 -       /* Create a lock for exclusive, serialized VCHI connection access */
393 -       mutex_init(&instance->vchi_mutex);
394         /* Open the VCHI service connections */
395 -       params.callback_param = instance,
396 -
397         status = vchi_service_open(vchi_instance, &params,
398                                    &instance->vchi_handle);
399  
400 @@ -322,16 +176,16 @@ vc_vchi_audio_init(VCHI_INSTANCE_T vchi_
401                 LOG_ERR("%s: failed to open VCHI service connection (status=%d)\n",
402                         __func__, status);
403                 kfree(instance);
404 -               return ERR_PTR(-EPERM);
405 +               return -EPERM;
406         }
407  
408         /* Finished with the service for now */
409         vchi_service_release(instance->vchi_handle);
410  
411 -       return instance;
412 +       return 0;
413  }
414  
415 -static int vc_vchi_audio_deinit(struct bcm2835_audio_instance *instance)
416 +static void vc_vchi_audio_deinit(struct bcm2835_audio_instance *instance)
417  {
418         int status;
419  
420 @@ -346,10 +200,6 @@ static int vc_vchi_audio_deinit(struct b
421         }
422  
423         mutex_unlock(&instance->vchi_mutex);
424 -
425 -       kfree(instance);
426 -
427 -       return 0;
428  }
429  
430  int bcm2835_new_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx)
431 @@ -387,39 +237,25 @@ void bcm2835_free_vchi_ctx(struct bcm283
432         vchi_ctx->vchi_instance = NULL;
433  }
434  
435 -static int bcm2835_audio_open_connection(struct bcm2835_alsa_stream *alsa_stream)
436 -{
437 -       struct bcm2835_audio_instance *instance =
438 -               (struct bcm2835_audio_instance *)alsa_stream->instance;
439 -       struct bcm2835_vchi_ctx *vhci_ctx = alsa_stream->chip->vchi_ctx;
440 -
441 -       /* Initialize an instance of the audio service */
442 -       instance = vc_vchi_audio_init(vhci_ctx->vchi_instance,
443 -                                     vhci_ctx->vchi_connection);
444 -
445 -       if (IS_ERR(instance))
446 -               return PTR_ERR(instance);
447 -
448 -       instance->alsa_stream = alsa_stream;
449 -       alsa_stream->instance = instance;
450 -
451 -       return 0;
452 -}
453 -
454  int bcm2835_audio_open(struct bcm2835_alsa_stream *alsa_stream)
455  {
456 +       struct bcm2835_vchi_ctx *vchi_ctx = alsa_stream->chip->vchi_ctx;
457         struct bcm2835_audio_instance *instance;
458         int err;
459  
460 -       alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
461 -       if (!alsa_stream->my_wq)
462 +       /* Allocate memory for this instance */
463 +       instance = kzalloc(sizeof(*instance), GFP_KERNEL);
464 +       if (!instance)
465                 return -ENOMEM;
466 +       mutex_init(&instance->vchi_mutex);
467 +       instance->alsa_stream = alsa_stream;
468 +       alsa_stream->instance = instance;
469  
470 -       err = bcm2835_audio_open_connection(alsa_stream);
471 +       err = vc_vchi_audio_init(vchi_ctx->vchi_instance,
472 +                                vchi_ctx->vchi_connection,
473 +                                instance);
474         if (err < 0)
475 -               goto free_wq;
476 -
477 -       instance = alsa_stream->instance;
478 +               goto free_instance;
479  
480         err = bcm2835_audio_send_simple(instance, VC_AUDIO_MSG_TYPE_OPEN,
481                                         false);
482 @@ -438,8 +274,9 @@ int bcm2835_audio_open(struct bcm2835_al
483  
484   deinit:
485         vc_vchi_audio_deinit(instance);
486 - free_wq:
487 -       destroy_workqueue(alsa_stream->my_wq);
488 + free_instance:
489 +       alsa_stream->instance = NULL;
490 +       kfree(instance);
491         return err;
492  }
493  
494 @@ -478,37 +315,46 @@ int bcm2835_audio_set_params(struct bcm2
495         return bcm2835_audio_send_msg(alsa_stream->instance, &m, true);
496  }
497  
498 -static int bcm2835_audio_start_worker(struct bcm2835_alsa_stream *alsa_stream)
499 +int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream)
500  {
501         return bcm2835_audio_send_simple(alsa_stream->instance,
502                                          VC_AUDIO_MSG_TYPE_START, false);
503  }
504  
505 -static int bcm2835_audio_stop_worker(struct bcm2835_alsa_stream *alsa_stream)
506 +int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream)
507  {
508         return bcm2835_audio_send_simple(alsa_stream->instance,
509                                          VC_AUDIO_MSG_TYPE_STOP, false);
510  }
511  
512 +int bcm2835_audio_drain(struct bcm2835_alsa_stream *alsa_stream)
513 +{
514 +       struct vc_audio_msg m = {
515 +               .type = VC_AUDIO_MSG_TYPE_STOP,
516 +               .u.stop.draining = 1,
517 +       };
518 +
519 +       return bcm2835_audio_send_msg(alsa_stream->instance, &m, false);
520 +}
521 +
522  int bcm2835_audio_close(struct bcm2835_alsa_stream *alsa_stream)
523  {
524         struct bcm2835_audio_instance *instance = alsa_stream->instance;
525         int err;
526  
527 -       my_workqueue_quit(alsa_stream);
528 -
529         err = bcm2835_audio_send_simple(alsa_stream->instance,
530                                         VC_AUDIO_MSG_TYPE_CLOSE, true);
531  
532         /* Stop the audio service */
533         vc_vchi_audio_deinit(instance);
534         alsa_stream->instance = NULL;
535 +       kfree(instance);
536  
537         return err;
538  }
539  
540 -static int bcm2835_audio_write_worker(struct bcm2835_alsa_stream *alsa_stream,
541 -                                     unsigned int size, void *src)
542 +int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
543 +                       unsigned int size, void *src)
544  {
545         struct bcm2835_audio_instance *instance = alsa_stream->instance;
546         struct vc_audio_msg m = {
547 @@ -558,13 +404,5 @@ static int bcm2835_audio_write_worker(st
548         return err;
549  }
550  
551 -unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream)
552 -{
553 -       unsigned int count = atomic_read(&alsa_stream->retrieved);
554 -
555 -       atomic_sub(count, &alsa_stream->retrieved);
556 -       return count;
557 -}
558 -
559  module_param(force_bulk, bool, 0444);
560  MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
561 --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
562 +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
563 @@ -121,13 +121,12 @@ struct bcm2835_alsa_stream {
564  
565         int draining;
566  
567 -       unsigned int pos;
568 +       atomic_t pos;
569 +       unsigned int period_offset;
570         unsigned int buffer_size;
571         unsigned int period_size;
572  
573 -       atomic_t retrieved;
574         struct bcm2835_audio_instance *instance;
575 -       struct workqueue_struct *my_wq;
576         int idx;
577  };
578  
579 @@ -152,11 +151,13 @@ int bcm2835_audio_set_params(struct bcm2
580                              unsigned int bps);
581  int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream);
582  int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream);
583 +int bcm2835_audio_drain(struct bcm2835_alsa_stream *alsa_stream);
584  int bcm2835_audio_set_ctls(struct bcm2835_alsa_stream *alsa_stream);
585  int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
586                         unsigned int count,
587                         void *src);
588 -void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream);
589 +void bcm2835_playback_fifo(struct bcm2835_alsa_stream *alsa_stream,
590 +                          unsigned int size);
591  unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream);
592  
593  #endif /* __SOUND_ARM_BCM2835_H */