ALSA: ctxfi: Clean up DAIO resource allocation
cover ALSA: ctxfi: Clean up DAIO resource allocation
Clean up the resource allocation in atc_get_resources() and related functions, on top of the recent SPDIFI1 fixes.
The earlier refactoring introduced a kernel crash on hw20k2, where daio_device_index() returned -EINVAL for the SPDIFI1 type and the value was used as an unsigned array offset. On my development system (64G memory), this did not manifest as a crash, making the bug difficult to catch during testing.
Tested on the real hw20k2 hardware with mem=8G to verify the original crash and confirm these patches.
Harin Lee (3):
ALSA: ctxfi: Rename SPDIFI1 to SPDIFI_BAY
ALSA: ctxfi: Use matching DAIO type for da_desc
ALSA: ctxfi: Precompute SRC allocation loop bound
sound/pci/ctxfi/ctatc.c | 27 ++++++++++++--------------- sound/pci/ctxfi/ctdaio.c | 5 ++--- sound/pci/ctxfi/ctdaio.h | 2 +- 3 files changed, 15 insertions(+), 19 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/3 ALSA: ctxfi: Rename SPDIFI1 to SPDIFI_BAY
Rename the SPDIFI1 enum value to SPDIFI_BAY to better reflect its purpose as the S/PDIF input on the internal drive bay, as opposed to the S/PDIF input via Flexijack or optical (SPDIFIO; not SPDIFI-zero).
--- sound/pci/ctxfi/ctatc.c | 4 ++-- sound/pci/ctxfi/ctdaio.c | 6 +++--- sound/pci/ctxfi/ctdaio.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-)
sound/pci/ctxfi/ctatc.c +2 -2
@@ -1429,10 +1429,10 @@ static int atc_get_resources(struct ct_atc *atc)for (i =0 ; i <NUM_DAIOTYP ; i++) {if (((i ==MIC ) && !cap.dedicated_mic) || ((i ==RCA ) && !cap.dedicated_rca) || - i ==SPDIFI1 ) + i ==SPDIFI_BAY )continue ;if (atc->model ==CTSB073X && i ==SPDIFIO ) - da_desc.type =SPDIFI1 ; + da_desc.type =SPDIFI_BAY ;else da_desc.type = i; da_desc.output = (i <LINEIM ) || (i ==RCA );
sound/pci/ctxfi/ctdaio.c +3 -3
@@ -35,7 +35,7 @@ static const struct daio_rsc_idx idx_20k1[NUM_DAIOTYP] = { [LINEIM] = {.left = 0x1b5, .right = 0x1bd}, [SPDIFOO] = {.left = 0x20, .right = 0x21}, [SPDIFIO] = {.left = 0x15, .right = 0x1d}, - [SPDIFI1] = {.left = 0x95, .right = 0x9d}, + [SPDIFI_BAY] = {.left = 0x95, .right = 0x9d}, };static const struct daio_rsc_idx idx_20k2[NUM_DAIOTYP ] = {
@@ -106,7 +106,7 @@ static int daio_device_index(enum DAIOTYP type, struct hw *hw)switch (type) {case SPDIFOO :return 0 ;case SPDIFIO :return 0 ; -case SPDIFI1 :return 1 ; +case SPDIFI_BAY :return 1 ;case LINEO1 :return 4 ;case LINEO2 :return 7 ;case LINEO3 :return 5 ;
@@ -120,7 +120,7 @@ static int daio_device_index(enum DAIOTYP type, struct hw *hw)switch (type) {case SPDIFOO :return 0 ;case SPDIFIO :return 0 ; -case SPDIFI1 :return 1 ; +case SPDIFI_BAY :return 1 ;case LINEO1 :return 4 ;case LINEO2 :return 7 ;case LINEO3 :return 5 ;
sound/pci/ctxfi/ctdaio.h +1 -1
@@ -32,7 +32,7 @@ enum DAIOTYP {SPDIFIO ,/* S/PDIF In (Flexijack/Optical) on the card */ MIC ,/* Dedicated mic on Titanium HD */ RCA ,/* Dedicated RCA on SE-300PCIE */ -SPDIFI1 ,/* S/PDIF In on internal Drive Bay */ +SPDIFI_BAY ,/* S/PDIF In on internal drive bay */ NUM_DAIOTYP };
Signature · 1 line
2.53.0
PATCH 2/3 ALSA: ctxfi: Use matching DAIO type for da_desc
In reply to Harin LeeSkip the unused DAIO type per model (SPDIFIO on CTSB073X, SPDIFI_BAY on all others) and use the matching DAIO type directly as da_desc type. This removes the mismatch between the actual DAIO resource and the da_desc type like SPDIFI_BAY (formerly SPDIFI1). Update related functions accordingly, and drop the unreachable SPDIFI_BAY case from the hw20k2 daio_device_index().
--- sound/pci/ctxfi/ctatc.c | 17 ++++++++--------- sound/pci/ctxfi/ctdaio.c | 1 - 2 files changed, 8 insertions(+), 10 deletions(-)
sound/pci/ctxfi/ctatc.c +8 -9
@@ -1121,7 +1121,8 @@ static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)static int atc_spdif_in_unmute (struct ct_atc *atc,unsigned char state) { -return atc_daio_unmute (atc, state,SPDIFIO ); +return atc_daio_unmute (atc, state, (atc->model ==CTSB073X ) ? +SPDIFI_BAY :SPDIFIO ); }static int atc_spdif_out_get_status (struct ct_atc *atc,unsigned int *status)
@@ -1429,12 +1430,9 @@ static int atc_get_resources(struct ct_atc *atc)for (i =0 ; i <NUM_DAIOTYP ; i++) {if (((i ==MIC ) && !cap.dedicated_mic) || ((i ==RCA ) && !cap.dedicated_rca) || - i ==SPDIFI_BAY ) + (i == ((atc->model ==CTSB073X ) ?SPDIFIO :SPDIFI_BAY )))continue ; -if (atc->model ==CTSB073X && i ==SPDIFIO ) - da_desc.type =SPDIFI_BAY ; -else - da_desc.type = i; + da_desc.type = i; da_desc.output = (i <LINEIM ) || (i ==RCA ); err = daio_mgr->get_daio (daio_mgr, &da_desc, (struct daio **)&atc->daios[i]);
@@ -1569,10 +1567,11 @@ static void atc_connect_resources(struct ct_atc *atc) mixer->set_input_right (mixer,MIX_MIC_IN , &src->rsc); } - dai = container_of(atc->daios[SPDIFIO], struct dai, daio); + dai =container_of (atc->daios[(atc->model ==CTSB073X ) ? + SPDIFI_BAY : SPDIFIO], struct dai, daio);atc_connect_dai (atc->rsc_mgrs[SRC ], dai, - (struct src **)&atc->srcs[0 ], - (struct srcimp **)&atc->srcimps[0 ]); + (struct src **)&atc->srcs[0 ], + (struct srcimp **)&atc->srcimps[0 ]); src = atc->srcs[0 ]; mixer->set_input_left (mixer,MIX_SPDIF_IN , &src->rsc);
sound/pci/ctxfi/ctdaio.c +0 -1
@@ -120,7 +120,6 @@ static int daio_device_index(enum DAIOTYP type, struct hw *hw)switch (type) {case SPDIFOO :return 0 ;case SPDIFIO :return 0 ; -case SPDIFI_BAY :return 1 ;case LINEO1 :return 4 ;case LINEO2 :return 7 ;case LINEO3 :return 5 ;
Signature · 1 line
2.53.0
Re: [PATCH 2/3] ALSA: ctxfi: Use matching DAIO type for da_desc
sound/pci/ctxfi/ctatc.c static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state) · line 1125
Quoted 7 lines · Harin Lee
sound/pci/ctxfi/ctatc.c +2
-1@@ -1121,7 +1121,8 @@ static int atc_spdif_out_unmute(struct ct_atc *atc, unsigned char state)static int atc_spdif_in_unmute (struct ct_atc *atc,unsigned char state) { -return atc_daio_unmute (atc, state,SPDIFIO ); +return atc_daio_unmute (atc, state, (atc->model ==CTSB073X ) ? +SPDIFI_BAY :SPDIFIO );
It's error-prone to use ternary operators as open code in many places. Better to define a function instead.
sound/pci/ctxfi/ctatc.c static int atc_get_resources(struct ct_atc *atc) · line 1433
Quoted 9 lines
sound/pci/ctxfi/ctatc.c +1
-1}static int atc_spdif_out_get_status (struct ct_atc *atc,unsigned int *status)@@ -1429,12 +1430,9 @@ static int atc_get_resources(struct ct_atc *atc)for (i =0 ; i <NUM_DAIOTYP ; i++) {if (((i ==MIC ) && !cap.dedicated_mic) || ((i ==RCA ) && !cap.dedicated_rca) || - i ==SPDIFI_BAY ) + (i == ((atc->model ==CTSB073X ) ?SPDIFIO :SPDIFI_BAY )))
... and this is even reversed value assignment which is rather confusing. Rather write with a simple if/else, such as:
if ((i == SPDFIO && atc->model == CTSB073X) || (i == SPDIFI_BAY && atc->model != CTSB073X)) continue;
sound/pci/ctxfi/ctatc.c static void atc_connect_resources(struct ct_atc *atc) · line 1574
Quoted 5 lines
sound/pci/ctxfi/ctatc.c +2
-2atc_connect_dai (atc->rsc_mgrs[SRC ], dai, - (struct src **)&atc->srcs[0 ], - (struct srcimp **)&atc->srcimps[0 ]); + (struct src **)&atc->srcs[0 ], + (struct srcimp **)&atc->srcimps[0 ]);
Avoid unnecessary spacing changes. The previous code is fine about indentation.
thanks,
Takashi
PATCH 3/3 ALSA: ctxfi: Precompute SRC allocation loop bound
In reply to Harin LeeReplace the capability checks in the SRC and SRCIMP allocation loops with a precomputed loop bound. Cards with a dedicated mic input (SB1270, OK0010) allocate all NUM_ATC_SRCS entries, otherwise stop at 4.
--- sound/pci/ctxfi/ctatc.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
sound/pci/ctxfi/ctatc.c +4 -6
@@ -1405,9 +1405,11 @@ static int atc_get_resources(struct ct_atc *atc)struct sum_desc sum_dsc = {0 };struct sum_mgr *sum_mgr;struct capabilities cap; +int atc_srcs_limit;int err, i; cap = atc->capabilities (atc); + atc_srcs_limit = cap.dedicated_mic ?NUM_ATC_SRCS :4 ; atc->daios =kcalloc (NUM_DAIOTYP ,sizeof (void *),GFP_KERNEL );if (!atc->daios)
@@ -1448,9 +1450,7 @@ static int atc_get_resources(struct ct_atc *atc) src_dsc.multi =1 ; src_dsc.msr = atc->msr; src_dsc.mode =ARCRW ; -for (i =0 ; i <NUM_ATC_SRCS ; i++) { -if (((i >3 ) && !cap.dedicated_mic)) -continue ; +for (i =0 ; i < atc_srcs_limit; i++) { err = src_mgr->get_src (src_mgr, &src_dsc, (struct src **)&atc->srcs[i]);if (err)
@@ -1459,9 +1459,7 @@ static int atc_get_resources(struct ct_atc *atc) srcimp_mgr = atc->rsc_mgrs[SRCIMP ]; srcimp_dsc.msr =8 ; -for (i =0 ; i <NUM_ATC_SRCS ; i++) { -if (((i >3 ) && !cap.dedicated_mic)) -continue ; +for (i =0 ; i < atc_srcs_limit; i++) { err = srcimp_mgr->get_srcimp (srcimp_mgr, &srcimp_dsc, (struct srcimp **)&atc->srcimps[i]);if (err)
Signature · 1 line
2.53.0