| // SPDX-License-Identifier: GPL-2.0+ |
| /* |
| * u_audio.c -- interface to USB gadget "ALSA sound card" utilities |
| * |
| * Copyright (C) 2016 |
| * Author: Ruslan Bilovol <ruslan.bilovol@gmail.com> |
| * |
| * Sound card implementation was cut-and-pasted with changes |
| * from f_uac2.c and has: |
| * Copyright (C) 2011 |
| * Yadwinder Singh (yadi.brar01@gmail.com) |
| * Jaswinder Singh (jaswinder.singh@linaro.org) |
| */ |
| |
| #include <linux/proc_fs.h> |
| #include <linux/module.h> |
| #include <linux/spinlock_types.h> |
| #include <linux/tdm_bridge.h> |
| #include <sound/core.h> |
| #include <sound/pcm.h> |
| #include <sound/pcm_params.h> |
| #include <linux/delay.h> |
| #include <linux/debugfs.h> |
| #include <linux/amlogic/usb-v2.h> |
| |
| #include "u_audio.h" |
| |
| #define BUFF_SIZE_MAX (PAGE_SIZE * 16) |
| #define PRD_SIZE_MAX PAGE_SIZE |
| #define MIN_PERIODS 4 |
| #define TSBUF_LEN_MAX (5000 * sizeof(u64)) |
| |
| #define PROC_FOPS_FROM_OPEN(open_op) \ |
| ((const struct file_operations){ \ |
| .open = (open_op), \ |
| .read = seq_read, \ |
| .write = NULL, \ |
| .llseek = seq_lseek, \ |
| .release = single_release, \ |
| }) |
| |
| struct tsbuffer { |
| unsigned char *buf; |
| const u32 len; // 5000 * sizeof(u64); |
| u32 wr_off; |
| u32 rd_off; |
| spinlock_t busy_lock; |
| const char *file_name; |
| const struct file_operations proc_fops; |
| }; |
| |
| static int sof_tsb_open(struct inode *inode, struct file *file); |
| |
| static struct tsbuffer sof_tsb = { |
| .len = TSBUF_LEN_MAX, |
| .file_name = "sof_tsb", |
| .proc_fops = PROC_FOPS_FROM_OPEN(sof_tsb_open), |
| }; |
| static struct work_struct cap_work; |
| static struct work_struct con_work; |
| |
| static u32 last_free_run; |
| |
| static int skip_packet; |
| module_param(skip_packet, int, 0660); |
| MODULE_PARM_DESC(skip_packet, "skip uac packets for debugging"); |
| |
| static unsigned int uac_irq_cnt; |
| module_param(uac_irq_cnt, uint, 0444); |
| MODULE_PARM_DESC(uac_irq_cnt, "uac irq cnt"); |
| |
| static unsigned int uac_data_count; |
| module_param(uac_data_count, uint, 0444); |
| MODULE_PARM_DESC(uac_data_count, "the data in uac"); |
| |
| static unsigned int free_run; |
| module_param(free_run, uint, 0660); |
| MODULE_PARM_DESC(free_run, "write data to tdm_bridge"); |
| |
| static unsigned int capture_status; |
| module_param(capture_status, uint, 0444); |
| MODULE_PARM_DESC(capture_status, "the status of capture on UAC: 1 means capturing"); |
| |
| static unsigned int connected_status; |
| module_param(connected_status, uint, 0444); |
| MODULE_PARM_DESC(connected_status, "the status of USB connected with Host dev: 1 means connected"); |
| |
| static unsigned int save_ts = 1; |
| module_param(save_ts, uint, 0660); |
| MODULE_PARM_DESC(save_ts, "check if saving timestamps for uac irq"); |
| |
| static unsigned int dbg_flag; |
| module_param(dbg_flag, uint, 0660); |
| MODULE_PARM_DESC(dbg_flag, "debug uac run"); |
| |
| void capture_change_status(int on_off); |
| extern u64 meson_timestamp(void); |
| unsigned int get_uac_irq_cnt(void) |
| { |
| return uac_irq_cnt; |
| } |
| EXPORT_SYMBOL_GPL(get_uac_irq_cnt); |
| |
| unsigned int get_uac_rx_cnt(void) |
| { |
| return uac_data_count; |
| } |
| EXPORT_SYMBOL_GPL(get_uac_rx_cnt); |
| |
| static int tdm_ready; |
| static struct snd_uac_chip *g_uac; |
| static struct notifier_block USB_nb; |
| |
| struct uac_req { |
| struct uac_rtd_params *pp; /* parent param */ |
| struct usb_request *req; |
| }; |
| |
| /* Runtime data params for one stream */ |
| struct uac_rtd_params { |
| struct snd_uac_chip *uac; /* parent chip */ |
| bool ep_enabled; /* if the ep is enabled */ |
| |
| struct snd_pcm_substream *ss; |
| |
| /* Ring buffer */ |
| ssize_t hw_ptr; |
| |
| void *rbuf; |
| |
| unsigned max_psize; /* MaxPacketSize of endpoint */ |
| struct uac_req *ureq; |
| |
| spinlock_t lock; |
| }; |
| |
| struct snd_uac_chip { |
| struct g_audio *audio_dev; |
| |
| struct uac_rtd_params p_prm; |
| struct uac_rtd_params c_prm; |
| |
| struct snd_card *card; |
| struct snd_pcm *pcm; |
| |
| /* timekeeping for the playback endpoint */ |
| unsigned int p_interval; |
| unsigned int p_residue; |
| |
| /* pre-calculated values for playback iso completion */ |
| unsigned int p_pktsize; |
| unsigned int p_pktsize_residue; |
| unsigned int p_framesize; |
| }; |
| |
| static const struct snd_pcm_hardware uac_pcm_hardware = { |
| .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
| | SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
| | SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, |
| .rates = SNDRV_PCM_RATE_CONTINUOUS, |
| .periods_max = BUFF_SIZE_MAX / PRD_SIZE_MAX, |
| .buffer_bytes_max = BUFF_SIZE_MAX, |
| .period_bytes_max = PRD_SIZE_MAX, |
| .periods_min = MIN_PERIODS, |
| }; |
| |
| static void u_audio_reset_for_tdm(void) |
| { |
| uac_irq_cnt = 0; |
| uac_data_count = 0; |
| pr_info("%s ...\n", __func__); |
| } |
| |
| static void init_tsbuffer(struct tsbuffer *tsb) |
| { |
| if (tsb->buf) |
| return; |
| |
| tsb->buf = kzalloc(tsb->len * 2, GFP_KERNEL); |
| if (!tsb->buf) { |
| pr_err("%s, no mem for timestamp buffer\n", __func__); |
| return; |
| } |
| if (!proc_create(tsb->file_name, S_IFREG | 0444, NULL, &tsb->proc_fops |
| )) |
| pr_err("proc for uac error\n"); |
| spin_lock_init(&tsb->busy_lock); |
| } |
| |
| static int reset_tsbuffer(struct tsbuffer *tsb) |
| { |
| unsigned long flags; |
| |
| pr_info("tsbuffer reset"); |
| spin_lock_irqsave(&tsb->busy_lock, flags); |
| tsb->wr_off = 0; |
| tsb->rd_off = 0; |
| spin_unlock_irqrestore(&tsb->busy_lock, flags); |
| |
| return 0; |
| } |
| |
| static int tsbuffer_append(struct tsbuffer *tsb, u64 timestamps) |
| { |
| unsigned long flags; |
| |
| spin_lock_irqsave(&tsb->busy_lock, flags); |
| *(u64 *)(tsb->buf + tsb->wr_off) = timestamps; |
| tsb->wr_off += sizeof(u64); |
| |
| if (tsb->wr_off >= tsb->len * 2) |
| tsb->wr_off = 0; |
| |
| if (tsb->wr_off == tsb->rd_off) |
| pr_err("timestamp buffer overrun\n"); |
| spin_unlock_irqrestore(&tsb->busy_lock, flags); |
| |
| return 0; |
| } |
| |
| static int dump_tsbuffer(struct seq_file *s, void *what) |
| { |
| int last_wr_off = 0; |
| int len = 0; |
| struct tsbuffer *tsb = (struct tsbuffer *)(s->private); |
| unsigned char *buf = tsb->buf; |
| char *dest = s->buf + s->count; |
| |
| last_wr_off = tsb->wr_off; |
| if (last_wr_off >= tsb->rd_off) |
| len = last_wr_off - tsb->rd_off; |
| else /* last_wr_off < tsb->rd_off */ |
| len = tsb->len * 2 - tsb->rd_off + last_wr_off; |
| |
| len /= sizeof(u64); |
| len *= sizeof(u64); |
| |
| if (s->count + len >= s->size) { |
| s->count = s->size; |
| pr_err("%s seq file buff is full\n", __func__); |
| return 0; |
| } |
| |
| if (last_wr_off >= tsb->rd_off) { |
| memcpy(dest, buf + tsb->rd_off, len); |
| } else { /* last_wr_off < tsb->rd_off */ |
| int n = tsb->len * 2 - tsb->rd_off; |
| |
| memcpy(dest, buf + tsb->rd_off, n); |
| memcpy(dest + n, buf, last_wr_off); |
| } |
| |
| tsb->rd_off += len; |
| if (tsb->rd_off >= tsb->len * 2) |
| tsb->rd_off -= tsb->len * 2; |
| |
| s->count += len; |
| return 0; |
| } |
| |
| static int sof_tsb_open(struct inode *inode, struct file *file) |
| { |
| return single_open_size(file, dump_tsbuffer, &sof_tsb, 2 * sof_tsb.len); |
| } |
| |
| static void u_audio_iso_cap_complete(struct usb_ep *ep, struct usb_request *req) |
| { |
| unsigned pending; |
| unsigned long flags, flags2; |
| unsigned int hw_ptr; |
| int status = req->status; |
| struct uac_req *ur = req->context; |
| struct snd_pcm_substream *substream; |
| struct snd_pcm_runtime *runtime; |
| struct g_audio *audio_dev; |
| struct uac_params *params; |
| struct uac_rtd_params *prm = ur->pp; |
| struct snd_uac_chip *uac = prm->uac; |
| |
| /* i/f shutting down */ |
| if (!prm->ep_enabled || req->status == -ESHUTDOWN) |
| return; |
| |
| /* |
| * We can't really do much about bad xfers. |
| * Afterall, the ISOCH xfers could fail legitimately. |
| */ |
| if (status) |
| pr_debug("%s: iso_complete status(%d) %d/%d\n", |
| __func__, status, req->actual, req->length); |
| |
| substream = prm->ss; |
| audio_dev = g_uac->audio_dev; |
| params = &audio_dev->params; |
| |
| if (req->actual > 0) { |
| capture_change_status(1); |
| } |
| |
| spin_lock_irqsave(&prm->lock, flags); |
| //tdm_ready = aml_tdm_br_state(); |
| if (last_free_run && !free_run) { |
| if (tdm_ready) { |
| aml_tdm_br_stop(); |
| tdm_ready = 0; |
| } |
| last_free_run = free_run; |
| } |
| if (!last_free_run && free_run) { |
| if (!tdm_ready) { |
| tdm_ready = 1; |
| aml_tdm_br_pre_start(params->c_ssize, params->c_srate); |
| } |
| last_free_run = free_run; |
| } |
| if (free_run && tdm_ready) { |
| if (uac_irq_cnt > 40 && skip_packet > 0) { |
| skip_packet -= 1; |
| spin_unlock_irqrestore(&prm->lock, flags); |
| goto exit; |
| } |
| uac_irq_cnt++; |
| |
| uac_data_count += req->actual; |
| if (req->actual > aml_tdm_br_write_data(req->buf, req->actual)) { |
| pr_err("%s(), tdm_bridge does not have enough space to write.\n", __func__); |
| u_audio_reset_for_tdm(); |
| } |
| spin_unlock_irqrestore(&prm->lock, flags); |
| goto exit ; |
| } |
| spin_unlock_irqrestore(&prm->lock, flags); |
| |
| /* Do nothing if ALSA isn't active */ |
| if (!substream) |
| goto exit; |
| |
| snd_pcm_stream_lock_irqsave(substream, flags2); |
| |
| runtime = substream->runtime; |
| if (!runtime || !snd_pcm_running(substream)) { |
| snd_pcm_stream_unlock_irqrestore(substream, flags2); |
| goto exit; |
| } |
| |
| spin_lock_irqsave(&prm->lock, flags); |
| |
| hw_ptr = prm->hw_ptr; |
| |
| spin_unlock_irqrestore(&prm->lock, flags); |
| |
| /* Pack USB load in ALSA ring buffer */ |
| pending = runtime->dma_bytes - hw_ptr; |
| |
| if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| if (unlikely(pending < req->actual)) { |
| memcpy(runtime->dma_area + hw_ptr, req->buf, pending); |
| memcpy(runtime->dma_area, req->buf + pending, |
| req->actual - pending); |
| } else { |
| memcpy(runtime->dma_area + hw_ptr, req->buf, |
| req->actual); |
| } |
| uac_irq_cnt++; |
| uac_data_count += req->actual; |
| } |
| |
| spin_lock_irqsave(&prm->lock, flags); |
| /* update hw_ptr after data is copied to memory */ |
| prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes; |
| hw_ptr = prm->hw_ptr; |
| spin_unlock_irqrestore(&prm->lock, flags); |
| snd_pcm_stream_unlock_irqrestore(substream, flags2); |
| |
| if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual) |
| snd_pcm_period_elapsed(substream); |
| |
| exit: |
| if (usb_ep_queue(ep, req, GFP_ATOMIC)) |
| dev_err(uac->card->dev, "%d Error!\n", __LINE__); |
| } |
| |
| static void u_audio_iso_playback_complete(struct usb_ep *ep, struct usb_request *req) |
| { |
| unsigned pending; |
| unsigned long flags, flags2; |
| unsigned int hw_ptr; |
| int status = req->status; |
| struct uac_req *ur = req->context; |
| struct snd_pcm_substream *substream; |
| struct snd_pcm_runtime *runtime; |
| struct uac_rtd_params *prm = ur->pp; |
| struct snd_uac_chip *uac = prm->uac; |
| |
| /* i/f shutting down */ |
| if (!prm->ep_enabled || req->status == -ESHUTDOWN) |
| return; |
| |
| /* |
| * We can't really do much about bad xfers. |
| * Afterall, the ISOCH xfers could fail legitimately. |
| */ |
| if (status) |
| pr_debug("%s: iso_complete status(%d) %d/%d\n", |
| __func__, status, req->actual, req->length); |
| |
| substream = prm->ss; |
| |
| /* Do nothing if ALSA isn't active */ |
| if (!substream) |
| goto exit; |
| |
| snd_pcm_stream_lock_irqsave(substream, flags2); |
| |
| runtime = substream->runtime; |
| if (!runtime || !snd_pcm_running(substream)) { |
| snd_pcm_stream_unlock_irqrestore(substream, flags2); |
| goto exit; |
| } |
| |
| spin_lock_irqsave(&prm->lock, flags); |
| |
| if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| /* |
| * If there is a residue from this division, add it to the |
| * residue accumulator. |
| */ |
| req->length = uac->p_pktsize; |
| uac->p_residue += uac->p_pktsize_residue; |
| |
| /* |
| * Whenever there are more bytes in the accumulator than we |
| * need to add one more sample frame, increase this packet's |
| * size and decrease the accumulator. |
| */ |
| if (uac->p_residue / uac->p_interval >= uac->p_framesize) { |
| req->length += uac->p_framesize; |
| uac->p_residue -= uac->p_framesize * |
| uac->p_interval; |
| } |
| |
| req->actual = req->length; |
| } |
| |
| hw_ptr = prm->hw_ptr; |
| |
| spin_unlock_irqrestore(&prm->lock, flags); |
| |
| /* Pack USB load in ALSA ring buffer */ |
| pending = runtime->dma_bytes - hw_ptr; |
| |
| if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| if (unlikely(pending < req->actual)) { |
| memcpy(req->buf, runtime->dma_area + hw_ptr, pending); |
| memcpy(req->buf + pending, runtime->dma_area, |
| req->actual - pending); |
| } else { |
| memcpy(req->buf, runtime->dma_area + hw_ptr, |
| req->actual); |
| } |
| } |
| |
| spin_lock_irqsave(&prm->lock, flags); |
| /* update hw_ptr after data is copied to memory */ |
| prm->hw_ptr = (hw_ptr + req->actual) % runtime->dma_bytes; |
| hw_ptr = prm->hw_ptr; |
| spin_unlock_irqrestore(&prm->lock, flags); |
| snd_pcm_stream_unlock_irqrestore(substream, flags2); |
| |
| if ((hw_ptr % snd_pcm_lib_period_bytes(substream)) < req->actual) |
| snd_pcm_period_elapsed(substream); |
| |
| exit: |
| if (usb_ep_queue(ep, req, GFP_ATOMIC)) |
| dev_err(uac->card->dev, "%d Error!\n", __LINE__); |
| } |
| |
| static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| { |
| struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); |
| struct uac_rtd_params *prm; |
| struct g_audio *audio_dev; |
| struct uac_params *params; |
| unsigned long flags; |
| int err = 0; |
| |
| audio_dev = uac->audio_dev; |
| params = &audio_dev->params; |
| |
| if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| prm = &uac->p_prm; |
| else |
| prm = &uac->c_prm; |
| |
| spin_lock_irqsave(&prm->lock, flags); |
| |
| /* Reset */ |
| prm->hw_ptr = 0; |
| |
| uac_irq_cnt = 0; |
| uac_data_count = 0; |
| |
| switch (cmd) { |
| case SNDRV_PCM_TRIGGER_START: |
| case SNDRV_PCM_TRIGGER_RESUME: |
| prm->ss = substream; |
| break; |
| case SNDRV_PCM_TRIGGER_STOP: |
| case SNDRV_PCM_TRIGGER_SUSPEND: |
| prm->ss = NULL; |
| break; |
| default: |
| err = -EINVAL; |
| } |
| |
| spin_unlock_irqrestore(&prm->lock, flags); |
| |
| /* Clear buffer after Play stops */ |
| if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && !prm->ss) |
| memset(prm->rbuf, 0, prm->max_psize * params->req_number); |
| |
| return err; |
| } |
| |
| static snd_pcm_uframes_t uac_pcm_pointer(struct snd_pcm_substream *substream) |
| { |
| struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); |
| struct uac_rtd_params *prm; |
| |
| if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| prm = &uac->p_prm; |
| else |
| prm = &uac->c_prm; |
| |
| return bytes_to_frames(substream->runtime, prm->hw_ptr); |
| } |
| |
| static int uac_pcm_hw_params(struct snd_pcm_substream *substream, |
| struct snd_pcm_hw_params *hw_params) |
| { |
| return snd_pcm_lib_malloc_pages(substream, |
| params_buffer_bytes(hw_params)); |
| } |
| |
| static int uac_pcm_hw_free(struct snd_pcm_substream *substream) |
| { |
| return snd_pcm_lib_free_pages(substream); |
| } |
| |
| static int uac_pcm_open(struct snd_pcm_substream *substream) |
| { |
| struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); |
| struct snd_pcm_runtime *runtime = substream->runtime; |
| struct g_audio *audio_dev; |
| struct uac_params *params; |
| int p_ssize, c_ssize; |
| int p_srate, c_srate; |
| int p_chmask, c_chmask; |
| |
| audio_dev = uac->audio_dev; |
| params = &audio_dev->params; |
| p_ssize = params->p_ssize; |
| c_ssize = params->c_ssize; |
| p_srate = params->p_srate; |
| c_srate = params->c_srate; |
| p_chmask = params->p_chmask; |
| c_chmask = params->c_chmask; |
| uac->p_residue = 0; |
| |
| runtime->hw = uac_pcm_hardware; |
| |
| if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| spin_lock_init(&uac->p_prm.lock); |
| runtime->hw.rate_min = p_srate; |
| switch (p_ssize) { |
| case 3: |
| runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; |
| break; |
| case 4: |
| runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; |
| break; |
| default: |
| runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; |
| break; |
| } |
| runtime->hw.channels_min = num_channels(p_chmask); |
| runtime->hw.period_bytes_min = 2 * uac->p_prm.max_psize |
| / runtime->hw.periods_min; |
| } else { |
| spin_lock_init(&uac->c_prm.lock); |
| runtime->hw.rate_min = c_srate; |
| switch (c_ssize) { |
| case 3: |
| runtime->hw.formats = SNDRV_PCM_FMTBIT_S24_3LE; |
| break; |
| case 4: |
| runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE; |
| break; |
| default: |
| runtime->hw.formats = SNDRV_PCM_FMTBIT_S16_LE; |
| break; |
| } |
| runtime->hw.channels_min = num_channels(c_chmask); |
| runtime->hw.period_bytes_min = 2 * uac->c_prm.max_psize |
| / runtime->hw.periods_min; |
| } |
| |
| runtime->hw.rate_max = runtime->hw.rate_min; |
| runtime->hw.channels_max = runtime->hw.channels_min; |
| |
| snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
| |
| if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| if (!tdm_ready) { |
| pr_info("UAC-cap prepare TDM for playing out\n"); |
| tdm_ready = 1; |
| aml_tdm_br_pre_start(c_ssize, c_srate); |
| } |
| } |
| |
| return 0; |
| } |
| |
| /* ALSA cries without these function pointers */ |
| static int uac_pcm_close(struct snd_pcm_substream *substream) |
| { |
| if (tdm_ready) { |
| pr_info("%s() stop tdm_bridge\n", __func__); |
| aml_tdm_br_stop(); |
| tdm_ready = 0; |
| } |
| return 0; |
| } |
| static int uac_pcm_null(struct snd_pcm_substream *substream) |
| { |
| return 0; |
| } |
| |
| static const struct snd_pcm_ops uac_pcm_ops = { |
| .open = uac_pcm_open, |
| .close = uac_pcm_close, |
| .ioctl = snd_pcm_lib_ioctl, |
| .hw_params = uac_pcm_hw_params, |
| .hw_free = uac_pcm_hw_free, |
| .trigger = uac_pcm_trigger, |
| .pointer = uac_pcm_pointer, |
| .prepare = uac_pcm_null, |
| }; |
| |
| static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep) |
| { |
| struct snd_uac_chip *uac = prm->uac; |
| struct g_audio *audio_dev; |
| struct uac_params *params; |
| int i; |
| |
| if (!prm->ep_enabled) |
| return; |
| |
| prm->ep_enabled = false; |
| |
| audio_dev = uac->audio_dev; |
| params = &audio_dev->params; |
| |
| for (i = 0; i < params->req_number; i++) { |
| if (prm->ureq[i].req) { |
| usb_ep_dequeue(ep, prm->ureq[i].req); |
| usb_ep_free_request(ep, prm->ureq[i].req); |
| prm->ureq[i].req = NULL; |
| } |
| } |
| |
| if (usb_ep_disable(ep)) |
| dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__); |
| } |
| |
| int u_audio_start_capture(struct g_audio *audio_dev) |
| { |
| struct snd_uac_chip *uac = audio_dev->uac; |
| struct usb_gadget *gadget = audio_dev->gadget; |
| struct device *dev = &gadget->dev; |
| struct usb_request *req; |
| struct usb_ep *ep; |
| struct uac_rtd_params *prm; |
| struct uac_params *params = &audio_dev->params; |
| int req_len, i; |
| |
| pr_info("%s ...\n", __func__); |
| if (!connected_status) |
| reset_tsbuffer(&sof_tsb); |
| |
| usb_change_status(1); |
| u_audio_reset_for_tdm(); |
| ep = audio_dev->out_ep; |
| prm = &uac->c_prm; |
| config_ep_by_speed(gadget, &audio_dev->func, ep); |
| req_len = prm->max_psize; |
| |
| prm->ep_enabled = true; |
| usb_ep_enable(ep); |
| for (i = 0; i < params->req_number; i++) { |
| if (!prm->ureq[i].req) { |
| req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
| if (req == NULL) |
| return -ENOMEM; |
| |
| prm->ureq[i].req = req; |
| prm->ureq[i].pp = prm; |
| |
| req->zero = 0; |
| req->context = &prm->ureq[i]; |
| req->length = req_len; |
| req->complete = u_audio_iso_cap_complete; |
| req->buf = prm->rbuf + i * prm->max_psize; |
| } |
| |
| if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) |
| dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| } |
| |
| tdm_ready = aml_tdm_br_state(); |
| if (!tdm_ready && free_run) { |
| aml_tdm_br_pre_start(params->c_ssize, params->c_srate); |
| tdm_ready = 1; |
| } |
| |
| |
| last_free_run = free_run; |
| return 0; |
| } |
| EXPORT_SYMBOL_GPL(u_audio_start_capture); |
| |
| void u_audio_stop_capture(struct g_audio *audio_dev) |
| { |
| struct snd_uac_chip *uac = audio_dev->uac; |
| |
| tdm_ready = aml_tdm_br_state(); |
| pr_info("%s tdm_ready:%d, uac_irq_cnt:%d\n", __func__, tdm_ready, uac_irq_cnt); |
| if (tdm_ready && free_run) { |
| pr_debug("%s() stop tdm_bridge\n", __func__); |
| aml_tdm_br_stop(); |
| tdm_ready = 0; |
| } |
| /* Reset */ |
| uac_irq_cnt = 0; |
| uac_data_count = 0; |
| capture_change_status(0); |
| |
| free_ep(&uac->c_prm, audio_dev->out_ep); |
| } |
| EXPORT_SYMBOL_GPL(u_audio_stop_capture); |
| |
| int u_audio_start_playback(struct g_audio *audio_dev) |
| { |
| struct snd_uac_chip *uac = audio_dev->uac; |
| struct usb_gadget *gadget = audio_dev->gadget; |
| struct device *dev = &gadget->dev; |
| struct usb_request *req; |
| struct usb_ep *ep; |
| struct uac_rtd_params *prm; |
| struct uac_params *params = &audio_dev->params; |
| unsigned int factor, rate; |
| const struct usb_endpoint_descriptor *ep_desc; |
| int req_len, i; |
| |
| ep = audio_dev->in_ep; |
| prm = &uac->p_prm; |
| config_ep_by_speed(gadget, &audio_dev->func, ep); |
| |
| ep_desc = ep->desc; |
| |
| /* pre-calculate the playback endpoint's interval */ |
| if (gadget->speed == USB_SPEED_FULL) |
| factor = 1000; |
| else |
| factor = 8000; |
| |
| /* pre-compute some values for iso_complete() */ |
| uac->p_framesize = params->p_ssize * |
| num_channels(params->p_chmask); |
| rate = params->p_srate * uac->p_framesize; |
| uac->p_interval = factor / (1 << (ep_desc->bInterval - 1)); |
| uac->p_pktsize = min_t(unsigned int, rate / uac->p_interval, |
| prm->max_psize); |
| |
| if (uac->p_pktsize < prm->max_psize) |
| uac->p_pktsize_residue = rate % uac->p_interval; |
| else |
| uac->p_pktsize_residue = 0; |
| |
| req_len = uac->p_pktsize; |
| uac->p_residue = 0; |
| |
| prm->ep_enabled = true; |
| usb_ep_enable(ep); |
| |
| for (i = 0; i < params->req_number; i++) { |
| if (!prm->ureq[i].req) { |
| req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
| if (req == NULL) |
| return -ENOMEM; |
| |
| prm->ureq[i].req = req; |
| prm->ureq[i].pp = prm; |
| |
| req->zero = 0; |
| req->context = &prm->ureq[i]; |
| req->length = req_len; |
| req->complete = u_audio_iso_playback_complete; |
| req->buf = prm->rbuf + i * prm->max_psize; |
| } |
| |
| if (usb_ep_queue(ep, prm->ureq[i].req, GFP_ATOMIC)) |
| dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); |
| } |
| |
| return 0; |
| } |
| EXPORT_SYMBOL_GPL(u_audio_start_playback); |
| |
| void u_audio_stop_playback(struct g_audio *audio_dev) |
| { |
| struct snd_uac_chip *uac = audio_dev->uac; |
| |
| free_ep(&uac->p_prm, audio_dev->in_ep); |
| } |
| EXPORT_SYMBOL_GPL(u_audio_stop_playback); |
| |
| static void uac_add_timestamp(void) |
| { |
| u64 cur_tm = 0; |
| |
| if (sof_tsb.buf && save_ts) { |
| cur_tm = meson_timestamp(); |
| tsbuffer_append(&sof_tsb, cur_tm); |
| } |
| } |
| |
| static ssize_t capture_status_show(struct device *dev, |
| struct device_attribute *attr, char *buf) |
| { |
| pr_debug("%s %d\n", __func__, capture_status); |
| return sprintf(buf, "%d\n", capture_status); |
| } |
| |
| static ssize_t connected_status_show(struct device *dev, |
| struct device_attribute *attr, char *buf) |
| { |
| pr_debug("%s %d\n", __func__, connected_status); |
| return sprintf(buf, "%d\n", connected_status); |
| } |
| |
| DEVICE_ATTR_RO(capture_status); |
| DEVICE_ATTR_RO(connected_status); |
| const static struct attribute *uac_attrs[] = { |
| &dev_attr_capture_status.attr, |
| &dev_attr_connected_status.attr, |
| NULL, |
| }; |
| |
| static void uac_cap_work_func(struct work_struct *work) |
| { |
| struct kobject *kobj = NULL; |
| int ret = 0; |
| char *status[2] = { |
| (capture_status ? "CAPTURE_STATUS=1" : "CAPTURE_STATUS=0"), NULL |
| }; |
| |
| if (!g_uac || !g_uac->card) |
| return; |
| |
| kobj = &g_uac->card->card_dev.kobj; |
| |
| pr_debug("%s capture_status:%d\n", __func__, capture_status); |
| sysfs_notify(kobj, NULL, "capture_status"); |
| ret = kobject_uevent_env(kobj, KOBJ_CHANGE, status); |
| if (ret) { |
| pr_err("%s ERROR sending uevent %s, err=%d\n", __func__, |
| status[0], ret); |
| return; |
| } |
| pr_info("%s: sent uevent %s\n", __func__, status[0]); |
| } |
| |
| static void uac_con_work_func(struct work_struct *work) |
| { |
| struct kobject *kobj = NULL; |
| int ret = 0; |
| char *status[2] = { (connected_status ? "CONNECTED_STATUS=1" : |
| "CONNECTED_STATUS=0"), |
| NULL }; |
| |
| if (!g_uac || !g_uac->card) |
| return; |
| |
| kobj = &g_uac->card->card_dev.kobj; |
| |
| sysfs_notify(kobj, NULL, "connected_status"); |
| ret = kobject_uevent_env(kobj, KOBJ_CHANGE, status); |
| if (ret) { |
| pr_err("%s ERROR sending uevent %s, err=%d\n", __func__, |
| status[0], ret); |
| return; |
| } |
| pr_info("%s: sent uevent %s\n", __func__, status[0]); |
| } |
| |
| static int aml_usb_notifier_func(struct notifier_block *nb, |
| unsigned long event, void *data) |
| { |
| pr_info("%s ...\n", __func__); |
| |
| tdm_ready = aml_tdm_br_state(); |
| if (tdm_ready) { |
| pr_info("%s() stop tdm_bridge\n", __func__); |
| aml_tdm_br_stop(); |
| tdm_ready = 0; |
| } |
| uac_irq_cnt = 0; |
| uac_data_count = 0; |
| capture_change_status(0); |
| |
| usb_change_status(0); |
| reset_tsbuffer(&sof_tsb); |
| |
| return 0; |
| } |
| |
| void capture_change_status(int on_off) |
| { |
| if (capture_status != on_off) { |
| capture_status = on_off; |
| if (cap_work.func) |
| schedule_work(&cap_work); |
| } |
| } |
| |
| void usb_change_status(int on_off) |
| { |
| if (connected_status != on_off) { |
| connected_status = on_off; |
| if (con_work.func) |
| schedule_work(&con_work); |
| } |
| } |
| |
| |
| int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, |
| const char *card_name) |
| { |
| struct snd_uac_chip *uac; |
| struct snd_card *card; |
| struct snd_pcm *pcm; |
| struct uac_params *params; |
| int p_chmask, c_chmask; |
| int err; |
| |
| if (!g_audio) |
| return -EINVAL; |
| |
| uac = kzalloc(sizeof(*uac), GFP_KERNEL); |
| if (!uac) |
| return -ENOMEM; |
| g_audio->uac = uac; |
| uac->audio_dev = g_audio; |
| |
| params = &g_audio->params; |
| p_chmask = params->p_chmask; |
| c_chmask = params->c_chmask; |
| |
| if (c_chmask) { |
| struct uac_rtd_params *prm = &uac->c_prm; |
| |
| uac->c_prm.uac = uac; |
| prm->max_psize = g_audio->out_ep_maxpsize; |
| |
| prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req), |
| GFP_KERNEL); |
| if (!prm->ureq) { |
| err = -ENOMEM; |
| goto fail; |
| } |
| |
| prm->rbuf = kcalloc(params->req_number, prm->max_psize, |
| GFP_KERNEL); |
| if (!prm->rbuf) { |
| prm->max_psize = 0; |
| err = -ENOMEM; |
| goto fail; |
| } |
| } |
| |
| if (p_chmask) { |
| struct uac_rtd_params *prm = &uac->p_prm; |
| |
| uac->p_prm.uac = uac; |
| prm->max_psize = g_audio->in_ep_maxpsize; |
| |
| prm->ureq = kcalloc(params->req_number, sizeof(struct uac_req), |
| GFP_KERNEL); |
| if (!prm->ureq) { |
| err = -ENOMEM; |
| goto fail; |
| } |
| |
| prm->rbuf = kcalloc(params->req_number, prm->max_psize, |
| GFP_KERNEL); |
| if (!prm->rbuf) { |
| prm->max_psize = 0; |
| err = -ENOMEM; |
| goto fail; |
| } |
| } |
| |
| /* Choose any slot, with no id */ |
| err = snd_card_new(&g_audio->gadget->dev, |
| -1, NULL, THIS_MODULE, 0, &card); |
| if (err < 0) |
| goto fail; |
| |
| uac->card = card; |
| |
| /* |
| * Create first PCM device |
| * Create a substream only for non-zero channel streams |
| */ |
| err = snd_pcm_new(uac->card, pcm_name, 0, |
| p_chmask ? 1 : 0, c_chmask ? 1 : 0, &pcm); |
| if (err < 0) |
| goto snd_fail; |
| |
| strlcpy(pcm->name, pcm_name, sizeof(pcm->name)); |
| pcm->private_data = uac; |
| uac->pcm = pcm; |
| |
| snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops); |
| snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops); |
| |
| strlcpy(card->driver, card_name, sizeof(card->driver)); |
| strlcpy(card->shortname, card_name, sizeof(card->shortname)); |
| sprintf(card->longname, "%s %i", card_name, card->dev->id); |
| |
| snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS, |
| snd_dma_continuous_data(GFP_KERNEL), 0, BUFF_SIZE_MAX); |
| |
| err = snd_card_register(card); |
| |
| if (err) |
| goto snd_fail; |
| |
| init_tsbuffer(&sof_tsb); |
| if (!sysfs_create_files(&card->card_dev.kobj, uac_attrs)) { |
| INIT_WORK(&cap_work, uac_cap_work_func); |
| INIT_WORK(&con_work, uac_con_work_func); |
| } |
| g_uac = uac; |
| |
| USB_nb.notifier_call = aml_usb_notifier_func; |
| aml_usb_disconn_register_notifier(&USB_nb); |
| |
| aml_usb_sof_register_callback(uac_add_timestamp); |
| |
| return 0; |
| |
| snd_fail: |
| snd_card_free(card); |
| fail: |
| kfree(uac->p_prm.ureq); |
| kfree(uac->c_prm.ureq); |
| kfree(uac->p_prm.rbuf); |
| kfree(uac->c_prm.rbuf); |
| kfree(uac); |
| |
| return err; |
| } |
| EXPORT_SYMBOL_GPL(g_audio_setup); |
| |
| void g_audio_cleanup(struct g_audio *g_audio) |
| { |
| struct snd_uac_chip *uac; |
| struct snd_card *card; |
| |
| if (!g_audio || !g_audio->uac) |
| return; |
| |
| uac = g_audio->uac; |
| card = uac->card; |
| sysfs_remove_files(&card->card_dev.kobj, uac_attrs); |
| if (card) |
| snd_card_free(card); |
| |
| kfree(uac->p_prm.ureq); |
| kfree(uac->c_prm.ureq); |
| kfree(uac->p_prm.rbuf); |
| kfree(uac->c_prm.rbuf); |
| kfree(uac); |
| g_uac = NULL; |
| aml_usb_disconn_unregister_notifier(&USB_nb); |
| } |
| EXPORT_SYMBOL_GPL(g_audio_cleanup); |
| |
| MODULE_LICENSE("GPL"); |
| MODULE_DESCRIPTION("USB gadget \"ALSA sound card\" utilities"); |
| MODULE_AUTHOR("Ruslan Bilovol"); |