asihpi.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Asihpi soundcard
  4. * Copyright (c) by AudioScience Inc <[email protected]>
  5. *
  6. * The following is not a condition of use, merely a request:
  7. * If you modify this program, particularly if you fix errors, AudioScience Inc
  8. * would appreciate it if you grant us the right to use those modifications
  9. * for any purpose including commercial applications.
  10. */
  11. #include "hpi_internal.h"
  12. #include "hpi_version.h"
  13. #include "hpimsginit.h"
  14. #include "hpioctl.h"
  15. #include "hpicmn.h"
  16. #include <linux/pci.h>
  17. #include <linux/init.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/slab.h>
  20. #include <linux/time.h>
  21. #include <linux/wait.h>
  22. #include <linux/module.h>
  23. #include <sound/core.h>
  24. #include <sound/control.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/info.h>
  28. #include <sound/initval.h>
  29. #include <sound/tlv.h>
  30. #include <sound/hwdep.h>
  31. MODULE_LICENSE("GPL");
  32. MODULE_AUTHOR("AudioScience inc. <[email protected]>");
  33. MODULE_DESCRIPTION("AudioScience ALSA ASI5xxx ASI6xxx ASI87xx ASI89xx "
  34. HPI_VER_STRING);
  35. #if defined CONFIG_SND_DEBUG_VERBOSE
  36. /**
  37. * snd_printddd - very verbose debug printk
  38. * @format: format string
  39. *
  40. * Works like snd_printk() for debugging purposes.
  41. * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
  42. * Must set snd module debug parameter to 3 to enable at runtime.
  43. */
  44. #define snd_printddd(format, args...) \
  45. __snd_printk(3, __FILE__, __LINE__, format, ##args)
  46. #else
  47. #define snd_printddd(format, args...) do { } while (0)
  48. #endif
  49. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
  50. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  51. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  52. static bool enable_hpi_hwdep = 1;
  53. module_param_array(index, int, NULL, 0444);
  54. MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
  55. module_param_array(id, charp, NULL, 0444);
  56. MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
  57. module_param_array(enable, bool, NULL, 0444);
  58. MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
  59. module_param(enable_hpi_hwdep, bool, 0644);
  60. MODULE_PARM_DESC(enable_hpi_hwdep,
  61. "ALSA enable HPI hwdep for AudioScience soundcard ");
  62. /* identify driver */
  63. #ifdef KERNEL_ALSA_BUILD
  64. static char *build_info = "Built using headers from kernel source";
  65. module_param(build_info, charp, 0444);
  66. MODULE_PARM_DESC(build_info, "Built using headers from kernel source");
  67. #else
  68. static char *build_info = "Built within ALSA source";
  69. module_param(build_info, charp, 0444);
  70. MODULE_PARM_DESC(build_info, "Built within ALSA source");
  71. #endif
  72. /* set to 1 to dump every control from adapter to log */
  73. static const int mixer_dump;
  74. #define DEFAULT_SAMPLERATE 44100
  75. static int adapter_fs = DEFAULT_SAMPLERATE;
  76. /* defaults */
  77. #define PERIODS_MIN 2
  78. #define PERIOD_BYTES_MIN 2048
  79. #define BUFFER_BYTES_MAX (512 * 1024)
  80. #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
  81. struct clk_source {
  82. int source;
  83. int index;
  84. const char *name;
  85. };
  86. struct clk_cache {
  87. int count;
  88. int has_local;
  89. struct clk_source s[MAX_CLOCKSOURCES];
  90. };
  91. /* Per card data */
  92. struct snd_card_asihpi {
  93. struct snd_card *card;
  94. struct pci_dev *pci;
  95. struct hpi_adapter *hpi;
  96. /* In low latency mode there is only one stream, a pointer to its
  97. * private data is stored here on trigger and cleared on stop.
  98. * The interrupt handler uses it as a parameter when calling
  99. * snd_card_asihpi_timer_function().
  100. */
  101. struct snd_card_asihpi_pcm *llmode_streampriv;
  102. void (*pcm_start)(struct snd_pcm_substream *substream);
  103. void (*pcm_stop)(struct snd_pcm_substream *substream);
  104. u32 h_mixer;
  105. struct clk_cache cc;
  106. u16 can_dma;
  107. u16 support_grouping;
  108. u16 support_mrx;
  109. u16 update_interval_frames;
  110. u16 in_max_chans;
  111. u16 out_max_chans;
  112. u16 in_min_chans;
  113. u16 out_min_chans;
  114. };
  115. /* Per stream data */
  116. struct snd_card_asihpi_pcm {
  117. struct timer_list timer;
  118. unsigned int respawn_timer;
  119. unsigned int hpi_buffer_attached;
  120. unsigned int buffer_bytes;
  121. unsigned int period_bytes;
  122. unsigned int bytes_per_sec;
  123. unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
  124. unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
  125. unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
  126. unsigned int drained_count;
  127. struct snd_pcm_substream *substream;
  128. u32 h_stream;
  129. struct hpi_format format;
  130. };
  131. /* universal stream verbs work with out or in stream handles */
  132. /* Functions to allow driver to give a buffer to HPI for busmastering */
  133. static u16 hpi_stream_host_buffer_attach(
  134. u32 h_stream, /* handle to outstream. */
  135. u32 size_in_bytes, /* size in bytes of bus mastering buffer */
  136. u32 pci_address
  137. )
  138. {
  139. struct hpi_message hm;
  140. struct hpi_response hr;
  141. unsigned int obj = hpi_handle_object(h_stream);
  142. if (!h_stream)
  143. return HPI_ERROR_INVALID_OBJ;
  144. hpi_init_message_response(&hm, &hr, obj,
  145. obj == HPI_OBJ_OSTREAM ?
  146. HPI_OSTREAM_HOSTBUFFER_ALLOC :
  147. HPI_ISTREAM_HOSTBUFFER_ALLOC);
  148. hpi_handle_to_indexes(h_stream, &hm.adapter_index,
  149. &hm.obj_index);
  150. hm.u.d.u.buffer.buffer_size = size_in_bytes;
  151. hm.u.d.u.buffer.pci_address = pci_address;
  152. hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
  153. hpi_send_recv(&hm, &hr);
  154. return hr.error;
  155. }
  156. static u16 hpi_stream_host_buffer_detach(u32 h_stream)
  157. {
  158. struct hpi_message hm;
  159. struct hpi_response hr;
  160. unsigned int obj = hpi_handle_object(h_stream);
  161. if (!h_stream)
  162. return HPI_ERROR_INVALID_OBJ;
  163. hpi_init_message_response(&hm, &hr, obj,
  164. obj == HPI_OBJ_OSTREAM ?
  165. HPI_OSTREAM_HOSTBUFFER_FREE :
  166. HPI_ISTREAM_HOSTBUFFER_FREE);
  167. hpi_handle_to_indexes(h_stream, &hm.adapter_index,
  168. &hm.obj_index);
  169. hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
  170. hpi_send_recv(&hm, &hr);
  171. return hr.error;
  172. }
  173. static inline u16 hpi_stream_start(u32 h_stream)
  174. {
  175. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  176. return hpi_outstream_start(h_stream);
  177. else
  178. return hpi_instream_start(h_stream);
  179. }
  180. static inline u16 hpi_stream_stop(u32 h_stream)
  181. {
  182. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  183. return hpi_outstream_stop(h_stream);
  184. else
  185. return hpi_instream_stop(h_stream);
  186. }
  187. static inline u16 hpi_stream_get_info_ex(
  188. u32 h_stream,
  189. u16 *pw_state,
  190. u32 *pbuffer_size,
  191. u32 *pdata_in_buffer,
  192. u32 *psample_count,
  193. u32 *pauxiliary_data
  194. )
  195. {
  196. u16 e;
  197. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  198. e = hpi_outstream_get_info_ex(h_stream, pw_state,
  199. pbuffer_size, pdata_in_buffer,
  200. psample_count, pauxiliary_data);
  201. else
  202. e = hpi_instream_get_info_ex(h_stream, pw_state,
  203. pbuffer_size, pdata_in_buffer,
  204. psample_count, pauxiliary_data);
  205. return e;
  206. }
  207. static inline u16 hpi_stream_group_add(
  208. u32 h_master,
  209. u32 h_stream)
  210. {
  211. if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
  212. return hpi_outstream_group_add(h_master, h_stream);
  213. else
  214. return hpi_instream_group_add(h_master, h_stream);
  215. }
  216. static inline u16 hpi_stream_group_reset(u32 h_stream)
  217. {
  218. if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
  219. return hpi_outstream_group_reset(h_stream);
  220. else
  221. return hpi_instream_group_reset(h_stream);
  222. }
  223. static u16 handle_error(u16 err, int line, char *filename)
  224. {
  225. if (err)
  226. printk(KERN_WARNING
  227. "in file %s, line %d: HPI error %d\n",
  228. filename, line, err);
  229. return err;
  230. }
  231. #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
  232. /***************************** GENERAL PCM ****************/
  233. static void print_hwparams(struct snd_pcm_substream *substream,
  234. struct snd_pcm_hw_params *p)
  235. {
  236. char name[16];
  237. snd_pcm_debug_name(substream, name, sizeof(name));
  238. snd_printdd("%s HWPARAMS\n", name);
  239. snd_printdd(" samplerate=%dHz channels=%d format=%d subformat=%d\n",
  240. params_rate(p), params_channels(p),
  241. params_format(p), params_subformat(p));
  242. snd_printdd(" buffer=%dB period=%dB period_size=%dB periods=%d\n",
  243. params_buffer_bytes(p), params_period_bytes(p),
  244. params_period_size(p), params_periods(p));
  245. snd_printdd(" buffer_size=%d access=%d data_rate=%dB/s\n",
  246. params_buffer_size(p), params_access(p),
  247. params_rate(p) * params_channels(p) *
  248. snd_pcm_format_width(params_format(p)) / 8);
  249. }
  250. #define INVALID_FORMAT (__force snd_pcm_format_t)(-1)
  251. static const snd_pcm_format_t hpi_to_alsa_formats[] = {
  252. INVALID_FORMAT, /* INVALID */
  253. SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
  254. SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
  255. INVALID_FORMAT, /* HPI_FORMAT_MPEG_L1 3 */
  256. SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
  257. SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
  258. INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC2 6 */
  259. INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC3 7 */
  260. SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
  261. INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
  262. INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
  263. SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
  264. INVALID_FORMAT, /* HPI_FORMAT_RAW_BITSTREAM 12 */
  265. INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
  266. SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
  267. #if 1
  268. /* ALSA can't handle 3 byte sample size together with power-of-2
  269. * constraint on buffer_bytes, so disable this format
  270. */
  271. INVALID_FORMAT
  272. #else
  273. /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
  274. #endif
  275. };
  276. static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
  277. u16 *hpi_format)
  278. {
  279. u16 format;
  280. for (format = HPI_FORMAT_PCM8_UNSIGNED;
  281. format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  282. if (hpi_to_alsa_formats[format] == alsa_format) {
  283. *hpi_format = format;
  284. return 0;
  285. }
  286. }
  287. snd_printd(KERN_WARNING "failed match for alsa format %d\n",
  288. alsa_format);
  289. *hpi_format = 0;
  290. return -EINVAL;
  291. }
  292. static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
  293. struct snd_pcm_hardware *pcmhw)
  294. {
  295. u16 err;
  296. u32 h_control;
  297. u32 sample_rate;
  298. int idx;
  299. unsigned int rate_min = 200000;
  300. unsigned int rate_max = 0;
  301. unsigned int rates = 0;
  302. if (asihpi->support_mrx) {
  303. rates |= SNDRV_PCM_RATE_CONTINUOUS;
  304. rates |= SNDRV_PCM_RATE_8000_96000;
  305. rate_min = 8000;
  306. rate_max = 100000;
  307. } else {
  308. /* on cards without SRC,
  309. valid rates are determined by sampleclock */
  310. err = hpi_mixer_get_control(asihpi->h_mixer,
  311. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  312. HPI_CONTROL_SAMPLECLOCK, &h_control);
  313. if (err) {
  314. dev_err(&asihpi->pci->dev,
  315. "No local sampleclock, err %d\n", err);
  316. }
  317. for (idx = -1; idx < 100; idx++) {
  318. if (idx == -1) {
  319. if (hpi_sample_clock_get_sample_rate(h_control,
  320. &sample_rate))
  321. continue;
  322. } else if (hpi_sample_clock_query_local_rate(h_control,
  323. idx, &sample_rate)) {
  324. break;
  325. }
  326. rate_min = min(rate_min, sample_rate);
  327. rate_max = max(rate_max, sample_rate);
  328. switch (sample_rate) {
  329. case 5512:
  330. rates |= SNDRV_PCM_RATE_5512;
  331. break;
  332. case 8000:
  333. rates |= SNDRV_PCM_RATE_8000;
  334. break;
  335. case 11025:
  336. rates |= SNDRV_PCM_RATE_11025;
  337. break;
  338. case 16000:
  339. rates |= SNDRV_PCM_RATE_16000;
  340. break;
  341. case 22050:
  342. rates |= SNDRV_PCM_RATE_22050;
  343. break;
  344. case 32000:
  345. rates |= SNDRV_PCM_RATE_32000;
  346. break;
  347. case 44100:
  348. rates |= SNDRV_PCM_RATE_44100;
  349. break;
  350. case 48000:
  351. rates |= SNDRV_PCM_RATE_48000;
  352. break;
  353. case 64000:
  354. rates |= SNDRV_PCM_RATE_64000;
  355. break;
  356. case 88200:
  357. rates |= SNDRV_PCM_RATE_88200;
  358. break;
  359. case 96000:
  360. rates |= SNDRV_PCM_RATE_96000;
  361. break;
  362. case 176400:
  363. rates |= SNDRV_PCM_RATE_176400;
  364. break;
  365. case 192000:
  366. rates |= SNDRV_PCM_RATE_192000;
  367. break;
  368. default: /* some other rate */
  369. rates |= SNDRV_PCM_RATE_KNOT;
  370. }
  371. }
  372. }
  373. pcmhw->rates = rates;
  374. pcmhw->rate_min = rate_min;
  375. pcmhw->rate_max = rate_max;
  376. }
  377. static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
  378. struct snd_pcm_hw_params *params)
  379. {
  380. struct snd_pcm_runtime *runtime = substream->runtime;
  381. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  382. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  383. int err;
  384. u16 format;
  385. int width;
  386. unsigned int bytes_per_sec;
  387. print_hwparams(substream, params);
  388. err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
  389. if (err)
  390. return err;
  391. hpi_handle_error(hpi_format_create(&dpcm->format,
  392. params_channels(params),
  393. format, params_rate(params), 0, 0));
  394. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  395. if (hpi_instream_reset(dpcm->h_stream) != 0)
  396. return -EINVAL;
  397. if (hpi_instream_set_format(
  398. dpcm->h_stream, &dpcm->format) != 0)
  399. return -EINVAL;
  400. }
  401. dpcm->hpi_buffer_attached = 0;
  402. if (card->can_dma) {
  403. err = hpi_stream_host_buffer_attach(dpcm->h_stream,
  404. params_buffer_bytes(params), runtime->dma_addr);
  405. if (err == 0) {
  406. snd_printdd(
  407. "stream_host_buffer_attach success %u %lu\n",
  408. params_buffer_bytes(params),
  409. (unsigned long)runtime->dma_addr);
  410. } else {
  411. snd_printd("stream_host_buffer_attach error %d\n",
  412. err);
  413. return -ENOMEM;
  414. }
  415. err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
  416. &dpcm->hpi_buffer_attached, NULL, NULL, NULL);
  417. }
  418. bytes_per_sec = params_rate(params) * params_channels(params);
  419. width = snd_pcm_format_width(params_format(params));
  420. bytes_per_sec *= width;
  421. bytes_per_sec /= 8;
  422. if (width < 0 || bytes_per_sec == 0)
  423. return -EINVAL;
  424. dpcm->bytes_per_sec = bytes_per_sec;
  425. dpcm->buffer_bytes = params_buffer_bytes(params);
  426. dpcm->period_bytes = params_period_bytes(params);
  427. return 0;
  428. }
  429. static int
  430. snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
  431. {
  432. struct snd_pcm_runtime *runtime = substream->runtime;
  433. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  434. if (dpcm->hpi_buffer_attached)
  435. hpi_stream_host_buffer_detach(dpcm->h_stream);
  436. return 0;
  437. }
  438. static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
  439. {
  440. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  441. kfree(dpcm);
  442. }
  443. static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
  444. substream)
  445. {
  446. struct snd_pcm_runtime *runtime = substream->runtime;
  447. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  448. int expiry;
  449. expiry = HZ / 200;
  450. expiry = max(expiry, 1); /* don't let it be zero! */
  451. mod_timer(&dpcm->timer, jiffies + expiry);
  452. dpcm->respawn_timer = 1;
  453. }
  454. static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
  455. {
  456. struct snd_pcm_runtime *runtime = substream->runtime;
  457. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  458. dpcm->respawn_timer = 0;
  459. del_timer(&dpcm->timer);
  460. }
  461. static void snd_card_asihpi_pcm_int_start(struct snd_pcm_substream *substream)
  462. {
  463. struct snd_card_asihpi_pcm *dpcm;
  464. struct snd_card_asihpi *card;
  465. dpcm = (struct snd_card_asihpi_pcm *)substream->runtime->private_data;
  466. card = snd_pcm_substream_chip(substream);
  467. WARN_ON(in_interrupt());
  468. card->llmode_streampriv = dpcm;
  469. hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
  470. HPI_ADAPTER_PROPERTY_IRQ_RATE,
  471. card->update_interval_frames, 0));
  472. }
  473. static void snd_card_asihpi_pcm_int_stop(struct snd_pcm_substream *substream)
  474. {
  475. struct snd_card_asihpi *card;
  476. card = snd_pcm_substream_chip(substream);
  477. hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
  478. HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
  479. card->llmode_streampriv = NULL;
  480. }
  481. static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
  482. int cmd)
  483. {
  484. struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
  485. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  486. struct snd_pcm_substream *s;
  487. u16 e;
  488. char name[16];
  489. snd_pcm_debug_name(substream, name, sizeof(name));
  490. switch (cmd) {
  491. case SNDRV_PCM_TRIGGER_START:
  492. snd_printdd("%s trigger start\n", name);
  493. snd_pcm_group_for_each_entry(s, substream) {
  494. struct snd_pcm_runtime *runtime = s->runtime;
  495. struct snd_card_asihpi_pcm *ds = runtime->private_data;
  496. if (snd_pcm_substream_chip(s) != card)
  497. continue;
  498. /* don't link Cap and Play */
  499. if (substream->stream != s->stream)
  500. continue;
  501. ds->drained_count = 0;
  502. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  503. /* How do I know how much valid data is present
  504. * in buffer? Must be at least one period!
  505. * Guessing 2 periods, but if
  506. * buffer is bigger it may contain even more
  507. * data??
  508. */
  509. unsigned int preload = ds->period_bytes * 1;
  510. snd_printddd("%d preload %d\n", s->number, preload);
  511. hpi_handle_error(hpi_outstream_write_buf(
  512. ds->h_stream,
  513. &runtime->dma_area[0],
  514. preload,
  515. &ds->format));
  516. ds->pcm_buf_host_rw_ofs = preload;
  517. }
  518. if (card->support_grouping) {
  519. snd_printdd("%d group\n", s->number);
  520. e = hpi_stream_group_add(
  521. dpcm->h_stream,
  522. ds->h_stream);
  523. if (!e) {
  524. snd_pcm_trigger_done(s, substream);
  525. } else {
  526. hpi_handle_error(e);
  527. break;
  528. }
  529. } else
  530. break;
  531. }
  532. /* start the master stream */
  533. card->pcm_start(substream);
  534. if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
  535. !card->can_dma)
  536. hpi_handle_error(hpi_stream_start(dpcm->h_stream));
  537. break;
  538. case SNDRV_PCM_TRIGGER_STOP:
  539. snd_printdd("%s trigger stop\n", name);
  540. card->pcm_stop(substream);
  541. snd_pcm_group_for_each_entry(s, substream) {
  542. if (snd_pcm_substream_chip(s) != card)
  543. continue;
  544. /* don't link Cap and Play */
  545. if (substream->stream != s->stream)
  546. continue;
  547. /*? workaround linked streams don't
  548. transition to SETUP 20070706*/
  549. __snd_pcm_set_state(s->runtime, SNDRV_PCM_STATE_SETUP);
  550. if (card->support_grouping) {
  551. snd_printdd("%d group\n", s->number);
  552. snd_pcm_trigger_done(s, substream);
  553. } else
  554. break;
  555. }
  556. /* _prepare and _hwparams reset the stream */
  557. hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
  558. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  559. hpi_handle_error(
  560. hpi_outstream_reset(dpcm->h_stream));
  561. if (card->support_grouping)
  562. hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
  563. break;
  564. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  565. snd_printdd("%s trigger pause release\n", name);
  566. card->pcm_start(substream);
  567. hpi_handle_error(hpi_stream_start(dpcm->h_stream));
  568. break;
  569. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  570. snd_printdd("%s trigger pause push\n", name);
  571. card->pcm_stop(substream);
  572. hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
  573. break;
  574. default:
  575. snd_printd(KERN_ERR "\tINVALID\n");
  576. return -EINVAL;
  577. }
  578. return 0;
  579. }
  580. /*algorithm outline
  581. Without linking degenerates to getting single stream pos etc
  582. Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
  583. */
  584. /*
  585. pcm_buf_dma_ofs=get_buf_pos(s);
  586. for_each_linked_stream(s) {
  587. pcm_buf_dma_ofs=get_buf_pos(s);
  588. min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
  589. new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
  590. }
  591. timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
  592. for_each_linked_stream(s) {
  593. s->pcm_buf_dma_ofs = min_buf_pos;
  594. if (new_data > period_bytes) {
  595. if (mmap) {
  596. irq_pos = (irq_pos + period_bytes) % buffer_bytes;
  597. if (playback) {
  598. write(period_bytes);
  599. } else {
  600. read(period_bytes);
  601. }
  602. }
  603. snd_pcm_period_elapsed(s);
  604. }
  605. }
  606. */
  607. /** Minimum of 2 modulo values. Works correctly when the difference between
  608. * the values is less than half the modulus
  609. */
  610. static inline unsigned int modulo_min(unsigned int a, unsigned int b,
  611. unsigned long int modulus)
  612. {
  613. unsigned int result;
  614. if (((a-b) % modulus) < (modulus/2))
  615. result = b;
  616. else
  617. result = a;
  618. return result;
  619. }
  620. /** Timer function, equivalent to interrupt service routine for cards
  621. */
  622. static void snd_card_asihpi_timer_function(struct timer_list *t)
  623. {
  624. struct snd_card_asihpi_pcm *dpcm = from_timer(dpcm, t, timer);
  625. struct snd_pcm_substream *substream = dpcm->substream;
  626. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  627. struct snd_pcm_runtime *runtime;
  628. struct snd_pcm_substream *s;
  629. unsigned int newdata = 0;
  630. unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
  631. unsigned int remdata, xfercount, next_jiffies;
  632. int first = 1;
  633. int loops = 0;
  634. u16 state;
  635. u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
  636. char name[16];
  637. snd_pcm_debug_name(substream, name, sizeof(name));
  638. /* find minimum newdata and buffer pos in group */
  639. snd_pcm_group_for_each_entry(s, substream) {
  640. struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
  641. runtime = s->runtime;
  642. if (snd_pcm_substream_chip(s) != card)
  643. continue;
  644. /* don't link Cap and Play */
  645. if (substream->stream != s->stream)
  646. continue;
  647. hpi_handle_error(hpi_stream_get_info_ex(
  648. ds->h_stream, &state,
  649. &buffer_size, &bytes_avail,
  650. &samples_played, &on_card_bytes));
  651. /* number of bytes in on-card buffer */
  652. runtime->delay = on_card_bytes;
  653. if (!card->can_dma)
  654. on_card_bytes = bytes_avail;
  655. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  656. pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
  657. if (state == HPI_STATE_STOPPED) {
  658. if (bytes_avail == 0) {
  659. hpi_handle_error(hpi_stream_start(ds->h_stream));
  660. snd_printdd("P%d start\n", s->number);
  661. ds->drained_count = 0;
  662. }
  663. } else if (state == HPI_STATE_DRAINED) {
  664. snd_printd(KERN_WARNING "P%d drained\n",
  665. s->number);
  666. ds->drained_count++;
  667. if (ds->drained_count > 20) {
  668. snd_pcm_stop_xrun(s);
  669. continue;
  670. }
  671. } else {
  672. ds->drained_count = 0;
  673. }
  674. } else
  675. pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
  676. if (first) {
  677. /* can't statically init min when wrap is involved */
  678. min_buf_pos = pcm_buf_dma_ofs;
  679. newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
  680. first = 0;
  681. } else {
  682. min_buf_pos =
  683. modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
  684. newdata = min(
  685. (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
  686. newdata);
  687. }
  688. snd_printddd(
  689. "timer1, %s, %d, S=%d, elap=%d, rw=%d, dsp=%d, left=%d, aux=%d, space=%d, hw_ptr=%ld, appl_ptr=%ld\n",
  690. name, s->number, state,
  691. ds->pcm_buf_elapsed_dma_ofs,
  692. ds->pcm_buf_host_rw_ofs,
  693. pcm_buf_dma_ofs,
  694. (int)bytes_avail,
  695. (int)on_card_bytes,
  696. buffer_size-bytes_avail,
  697. (unsigned long)frames_to_bytes(runtime,
  698. runtime->status->hw_ptr),
  699. (unsigned long)frames_to_bytes(runtime,
  700. runtime->control->appl_ptr)
  701. );
  702. loops++;
  703. }
  704. pcm_buf_dma_ofs = min_buf_pos;
  705. remdata = newdata % dpcm->period_bytes;
  706. xfercount = newdata - remdata; /* a multiple of period_bytes */
  707. /* come back when on_card_bytes has decreased enough to allow
  708. write to happen, or when data has been consumed to make another
  709. period
  710. */
  711. if (xfercount && (on_card_bytes > dpcm->period_bytes))
  712. next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
  713. else
  714. next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
  715. next_jiffies = max(next_jiffies, 1U);
  716. dpcm->timer.expires = jiffies + next_jiffies;
  717. snd_printddd("timer2, jif=%d, buf_pos=%d, newdata=%d, xfer=%d\n",
  718. next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
  719. snd_pcm_group_for_each_entry(s, substream) {
  720. struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
  721. /* don't link Cap and Play */
  722. if (substream->stream != s->stream)
  723. continue;
  724. /* Store dma offset for use by pointer callback */
  725. ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
  726. if (xfercount &&
  727. /* Limit use of on card fifo for playback */
  728. ((on_card_bytes <= ds->period_bytes) ||
  729. (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
  730. {
  731. unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
  732. unsigned int xfer1, xfer2;
  733. char *pd = &s->runtime->dma_area[buf_ofs];
  734. if (card->can_dma) { /* buffer wrap is handled at lower level */
  735. xfer1 = xfercount;
  736. xfer2 = 0;
  737. } else {
  738. xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
  739. xfer2 = xfercount - xfer1;
  740. }
  741. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  742. snd_printddd("write1, P=%d, xfer=%d, buf_ofs=%d\n",
  743. s->number, xfer1, buf_ofs);
  744. hpi_handle_error(
  745. hpi_outstream_write_buf(
  746. ds->h_stream, pd, xfer1,
  747. &ds->format));
  748. if (xfer2) {
  749. pd = s->runtime->dma_area;
  750. snd_printddd("write2, P=%d, xfer=%d, buf_ofs=%d\n",
  751. s->number,
  752. xfercount - xfer1, buf_ofs);
  753. hpi_handle_error(
  754. hpi_outstream_write_buf(
  755. ds->h_stream, pd,
  756. xfercount - xfer1,
  757. &ds->format));
  758. }
  759. } else {
  760. snd_printddd("read1, C=%d, xfer=%d\n",
  761. s->number, xfer1);
  762. hpi_handle_error(
  763. hpi_instream_read_buf(
  764. ds->h_stream,
  765. pd, xfer1));
  766. if (xfer2) {
  767. pd = s->runtime->dma_area;
  768. snd_printddd("read2, C=%d, xfer=%d\n",
  769. s->number, xfer2);
  770. hpi_handle_error(
  771. hpi_instream_read_buf(
  772. ds->h_stream,
  773. pd, xfer2));
  774. }
  775. }
  776. /* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
  777. ds->pcm_buf_host_rw_ofs += xfercount;
  778. ds->pcm_buf_elapsed_dma_ofs += xfercount;
  779. snd_pcm_period_elapsed(s);
  780. }
  781. }
  782. if (!card->hpi->interrupt_mode && dpcm->respawn_timer)
  783. add_timer(&dpcm->timer);
  784. }
  785. static void snd_card_asihpi_isr(struct hpi_adapter *a)
  786. {
  787. struct snd_card_asihpi *asihpi;
  788. WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
  789. asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
  790. if (asihpi->llmode_streampriv)
  791. snd_card_asihpi_timer_function(
  792. &asihpi->llmode_streampriv->timer);
  793. }
  794. /***************************** PLAYBACK OPS ****************/
  795. static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
  796. substream)
  797. {
  798. struct snd_pcm_runtime *runtime = substream->runtime;
  799. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  800. snd_printdd("P%d prepare\n", substream->number);
  801. hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
  802. dpcm->pcm_buf_host_rw_ofs = 0;
  803. dpcm->pcm_buf_dma_ofs = 0;
  804. dpcm->pcm_buf_elapsed_dma_ofs = 0;
  805. return 0;
  806. }
  807. static snd_pcm_uframes_t
  808. snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
  809. {
  810. struct snd_pcm_runtime *runtime = substream->runtime;
  811. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  812. snd_pcm_uframes_t ptr;
  813. char name[16];
  814. snd_pcm_debug_name(substream, name, sizeof(name));
  815. ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
  816. snd_printddd("%s, pointer=%ld\n", name, (unsigned long)ptr);
  817. return ptr;
  818. }
  819. static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
  820. u32 h_stream)
  821. {
  822. struct hpi_format hpi_format;
  823. u16 format;
  824. u16 err;
  825. u32 h_control;
  826. u32 sample_rate = 48000;
  827. u64 formats = 0;
  828. /* on cards without SRC, must query at valid rate,
  829. * maybe set by external sync
  830. */
  831. err = hpi_mixer_get_control(asihpi->h_mixer,
  832. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  833. HPI_CONTROL_SAMPLECLOCK, &h_control);
  834. if (!err)
  835. err = hpi_sample_clock_get_sample_rate(h_control,
  836. &sample_rate);
  837. for (format = HPI_FORMAT_PCM8_UNSIGNED;
  838. format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  839. err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
  840. format, sample_rate, 128000, 0);
  841. if (!err)
  842. err = hpi_outstream_query_format(h_stream, &hpi_format);
  843. if (!err && (hpi_to_alsa_formats[format] != INVALID_FORMAT))
  844. formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
  845. }
  846. return formats;
  847. }
  848. static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
  849. {
  850. struct snd_pcm_runtime *runtime = substream->runtime;
  851. struct snd_card_asihpi_pcm *dpcm;
  852. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  853. struct snd_pcm_hardware snd_card_asihpi_playback;
  854. int err;
  855. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  856. if (dpcm == NULL)
  857. return -ENOMEM;
  858. err = hpi_outstream_open(card->hpi->adapter->index,
  859. substream->number, &dpcm->h_stream);
  860. hpi_handle_error(err);
  861. if (err)
  862. kfree(dpcm);
  863. if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
  864. return -EBUSY;
  865. if (err)
  866. return -EIO;
  867. /*? also check ASI5000 samplerate source
  868. If external, only support external rate.
  869. If internal and other stream playing, can't switch
  870. */
  871. timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
  872. dpcm->substream = substream;
  873. runtime->private_data = dpcm;
  874. runtime->private_free = snd_card_asihpi_runtime_free;
  875. memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
  876. if (!card->hpi->interrupt_mode) {
  877. snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
  878. snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
  879. snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  880. snd_card_asihpi_playback.periods_min = PERIODS_MIN;
  881. snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
  882. } else {
  883. size_t pbmin = card->update_interval_frames *
  884. card->out_max_chans;
  885. snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
  886. snd_card_asihpi_playback.period_bytes_min = pbmin;
  887. snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  888. snd_card_asihpi_playback.periods_min = PERIODS_MIN;
  889. snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / pbmin;
  890. }
  891. /* snd_card_asihpi_playback.fifo_size = 0; */
  892. snd_card_asihpi_playback.channels_max = card->out_max_chans;
  893. snd_card_asihpi_playback.channels_min = card->out_min_chans;
  894. snd_card_asihpi_playback.formats =
  895. snd_card_asihpi_playback_formats(card, dpcm->h_stream);
  896. snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
  897. snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
  898. SNDRV_PCM_INFO_DOUBLE |
  899. SNDRV_PCM_INFO_BATCH |
  900. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  901. SNDRV_PCM_INFO_PAUSE |
  902. SNDRV_PCM_INFO_MMAP |
  903. SNDRV_PCM_INFO_MMAP_VALID;
  904. if (card->support_grouping) {
  905. snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
  906. snd_pcm_set_sync(substream);
  907. }
  908. /* struct is copied, so can create initializer dynamically */
  909. runtime->hw = snd_card_asihpi_playback;
  910. if (card->can_dma)
  911. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  912. SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  913. if (err < 0)
  914. return err;
  915. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  916. card->update_interval_frames);
  917. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  918. card->update_interval_frames, UINT_MAX);
  919. snd_printdd("playback open\n");
  920. return 0;
  921. }
  922. static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
  923. {
  924. struct snd_pcm_runtime *runtime = substream->runtime;
  925. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  926. hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
  927. snd_printdd("playback close\n");
  928. return 0;
  929. }
  930. static const struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
  931. .open = snd_card_asihpi_playback_open,
  932. .close = snd_card_asihpi_playback_close,
  933. .hw_params = snd_card_asihpi_pcm_hw_params,
  934. .hw_free = snd_card_asihpi_hw_free,
  935. .prepare = snd_card_asihpi_playback_prepare,
  936. .trigger = snd_card_asihpi_trigger,
  937. .pointer = snd_card_asihpi_playback_pointer,
  938. };
  939. /***************************** CAPTURE OPS ****************/
  940. static snd_pcm_uframes_t
  941. snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
  942. {
  943. struct snd_pcm_runtime *runtime = substream->runtime;
  944. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  945. char name[16];
  946. snd_pcm_debug_name(substream, name, sizeof(name));
  947. snd_printddd("%s, pointer=%d\n", name, dpcm->pcm_buf_dma_ofs);
  948. /* NOTE Unlike playback can't use actual samples_played
  949. for the capture position, because those samples aren't yet in
  950. the local buffer available for reading.
  951. */
  952. return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
  953. }
  954. static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
  955. {
  956. struct snd_pcm_runtime *runtime = substream->runtime;
  957. struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
  958. hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
  959. dpcm->pcm_buf_host_rw_ofs = 0;
  960. dpcm->pcm_buf_dma_ofs = 0;
  961. dpcm->pcm_buf_elapsed_dma_ofs = 0;
  962. snd_printdd("Capture Prepare %d\n", substream->number);
  963. return 0;
  964. }
  965. static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
  966. u32 h_stream)
  967. {
  968. struct hpi_format hpi_format;
  969. u16 format;
  970. u16 err;
  971. u32 h_control;
  972. u32 sample_rate = 48000;
  973. u64 formats = 0;
  974. /* on cards without SRC, must query at valid rate,
  975. maybe set by external sync */
  976. err = hpi_mixer_get_control(asihpi->h_mixer,
  977. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  978. HPI_CONTROL_SAMPLECLOCK, &h_control);
  979. if (!err)
  980. err = hpi_sample_clock_get_sample_rate(h_control,
  981. &sample_rate);
  982. for (format = HPI_FORMAT_PCM8_UNSIGNED;
  983. format <= HPI_FORMAT_PCM24_SIGNED; format++) {
  984. err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
  985. format, sample_rate, 128000, 0);
  986. if (!err)
  987. err = hpi_instream_query_format(h_stream, &hpi_format);
  988. if (!err && (hpi_to_alsa_formats[format] != INVALID_FORMAT))
  989. formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
  990. }
  991. return formats;
  992. }
  993. static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
  994. {
  995. struct snd_pcm_runtime *runtime = substream->runtime;
  996. struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
  997. struct snd_card_asihpi_pcm *dpcm;
  998. struct snd_pcm_hardware snd_card_asihpi_capture;
  999. int err;
  1000. dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
  1001. if (dpcm == NULL)
  1002. return -ENOMEM;
  1003. snd_printdd("capture open adapter %d stream %d\n",
  1004. card->hpi->adapter->index, substream->number);
  1005. err = hpi_handle_error(
  1006. hpi_instream_open(card->hpi->adapter->index,
  1007. substream->number, &dpcm->h_stream));
  1008. if (err)
  1009. kfree(dpcm);
  1010. if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
  1011. return -EBUSY;
  1012. if (err)
  1013. return -EIO;
  1014. timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
  1015. dpcm->substream = substream;
  1016. runtime->private_data = dpcm;
  1017. runtime->private_free = snd_card_asihpi_runtime_free;
  1018. memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
  1019. if (!card->hpi->interrupt_mode) {
  1020. snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
  1021. snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
  1022. snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  1023. snd_card_asihpi_capture.periods_min = PERIODS_MIN;
  1024. snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
  1025. } else {
  1026. size_t pbmin = card->update_interval_frames *
  1027. card->out_max_chans;
  1028. snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
  1029. snd_card_asihpi_capture.period_bytes_min = pbmin;
  1030. snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
  1031. snd_card_asihpi_capture.periods_min = PERIODS_MIN;
  1032. snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / pbmin;
  1033. }
  1034. /* snd_card_asihpi_capture.fifo_size = 0; */
  1035. snd_card_asihpi_capture.channels_max = card->in_max_chans;
  1036. snd_card_asihpi_capture.channels_min = card->in_min_chans;
  1037. snd_card_asihpi_capture.formats =
  1038. snd_card_asihpi_capture_formats(card, dpcm->h_stream);
  1039. snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
  1040. snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
  1041. SNDRV_PCM_INFO_MMAP |
  1042. SNDRV_PCM_INFO_MMAP_VALID;
  1043. if (card->support_grouping)
  1044. snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
  1045. runtime->hw = snd_card_asihpi_capture;
  1046. if (card->can_dma)
  1047. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  1048. SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
  1049. if (err < 0)
  1050. return err;
  1051. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1052. card->update_interval_frames);
  1053. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1054. card->update_interval_frames, UINT_MAX);
  1055. snd_pcm_set_sync(substream);
  1056. return 0;
  1057. }
  1058. static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
  1059. {
  1060. struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
  1061. hpi_handle_error(hpi_instream_close(dpcm->h_stream));
  1062. return 0;
  1063. }
  1064. static const struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
  1065. .open = snd_card_asihpi_capture_open,
  1066. .close = snd_card_asihpi_capture_close,
  1067. .hw_params = snd_card_asihpi_pcm_hw_params,
  1068. .hw_free = snd_card_asihpi_hw_free,
  1069. .prepare = snd_card_asihpi_capture_prepare,
  1070. .trigger = snd_card_asihpi_trigger,
  1071. .pointer = snd_card_asihpi_capture_pointer,
  1072. };
  1073. static int snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, int device)
  1074. {
  1075. struct snd_pcm *pcm;
  1076. int err;
  1077. u16 num_instreams, num_outstreams, x16;
  1078. u32 x32;
  1079. err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
  1080. &num_outstreams, &num_instreams,
  1081. &x16, &x32, &x16);
  1082. err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
  1083. num_outstreams, num_instreams, &pcm);
  1084. if (err < 0)
  1085. return err;
  1086. /* pointer to ops struct is stored, dont change ops afterwards! */
  1087. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1088. &snd_card_asihpi_playback_mmap_ops);
  1089. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1090. &snd_card_asihpi_capture_mmap_ops);
  1091. pcm->private_data = asihpi;
  1092. pcm->info_flags = 0;
  1093. strcpy(pcm->name, "Asihpi PCM");
  1094. /*? do we want to emulate MMAP for non-BBM cards?
  1095. Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
  1096. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  1097. &asihpi->pci->dev,
  1098. 64*1024, BUFFER_BYTES_MAX);
  1099. return 0;
  1100. }
  1101. /***************************** MIXER CONTROLS ****************/
  1102. struct hpi_control {
  1103. u32 h_control;
  1104. u16 control_type;
  1105. u16 src_node_type;
  1106. u16 src_node_index;
  1107. u16 dst_node_type;
  1108. u16 dst_node_index;
  1109. u16 band;
  1110. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; /* copied to snd_ctl_elem_id.name[44]; */
  1111. };
  1112. static const char * const asihpi_tuner_band_names[] = {
  1113. "invalid",
  1114. "AM",
  1115. "FM mono",
  1116. "TV NTSC-M",
  1117. "FM stereo",
  1118. "AUX",
  1119. "TV PAL BG",
  1120. "TV PAL I",
  1121. "TV PAL DK",
  1122. "TV SECAM",
  1123. "TV DAB",
  1124. };
  1125. /* Number of strings must match the enumerations for HPI_TUNER_BAND in hpi.h */
  1126. compile_time_assert(
  1127. (ARRAY_SIZE(asihpi_tuner_band_names) ==
  1128. (HPI_TUNER_BAND_LAST+1)),
  1129. assert_tuner_band_names_size);
  1130. static const char * const asihpi_src_names[] = {
  1131. "no source",
  1132. "PCM",
  1133. "Line",
  1134. "Digital",
  1135. "Tuner",
  1136. "RF",
  1137. "Clock",
  1138. "Bitstream",
  1139. "Mic",
  1140. "Net",
  1141. "Analog",
  1142. "Adapter",
  1143. "RTP",
  1144. "Internal",
  1145. "AVB",
  1146. "BLU-Link"
  1147. };
  1148. /* Number of strings must match the enumerations for HPI_SOURCENODES in hpi.h */
  1149. compile_time_assert(
  1150. (ARRAY_SIZE(asihpi_src_names) ==
  1151. (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
  1152. assert_src_names_size);
  1153. static const char * const asihpi_dst_names[] = {
  1154. "no destination",
  1155. "PCM",
  1156. "Line",
  1157. "Digital",
  1158. "RF",
  1159. "Speaker",
  1160. "Net",
  1161. "Analog",
  1162. "RTP",
  1163. "AVB",
  1164. "Internal",
  1165. "BLU-Link"
  1166. };
  1167. /* Number of strings must match the enumerations for HPI_DESTNODES in hpi.h */
  1168. compile_time_assert(
  1169. (ARRAY_SIZE(asihpi_dst_names) ==
  1170. (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
  1171. assert_dst_names_size);
  1172. static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
  1173. struct snd_card_asihpi *asihpi)
  1174. {
  1175. int err;
  1176. err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
  1177. if (err < 0)
  1178. return err;
  1179. else if (mixer_dump)
  1180. dev_info(&asihpi->pci->dev, "added %s(%d)\n", ctl->name, ctl->index);
  1181. return 0;
  1182. }
  1183. /* Convert HPI control name and location into ALSA control name */
  1184. static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
  1185. struct hpi_control *hpi_ctl,
  1186. char *name)
  1187. {
  1188. char *dir;
  1189. memset(snd_control, 0, sizeof(*snd_control));
  1190. snd_control->name = hpi_ctl->name;
  1191. snd_control->private_value = hpi_ctl->h_control;
  1192. snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1193. snd_control->index = 0;
  1194. if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
  1195. dir = ""; /* clock is neither capture nor playback */
  1196. else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
  1197. dir = "Capture "; /* On or towards a PCM capture destination*/
  1198. else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
  1199. (!hpi_ctl->dst_node_type))
  1200. dir = "Capture "; /* On a source node that is not PCM playback */
  1201. else if (hpi_ctl->src_node_type &&
  1202. (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
  1203. (hpi_ctl->dst_node_type))
  1204. dir = "Monitor Playback "; /* Between an input and an output */
  1205. else
  1206. dir = "Playback "; /* PCM Playback source, or output node */
  1207. if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
  1208. sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
  1209. asihpi_src_names[hpi_ctl->src_node_type],
  1210. hpi_ctl->src_node_index,
  1211. asihpi_dst_names[hpi_ctl->dst_node_type],
  1212. hpi_ctl->dst_node_index,
  1213. dir, name);
  1214. else if (hpi_ctl->dst_node_type) {
  1215. sprintf(hpi_ctl->name, "%s %d %s%s",
  1216. asihpi_dst_names[hpi_ctl->dst_node_type],
  1217. hpi_ctl->dst_node_index,
  1218. dir, name);
  1219. } else {
  1220. sprintf(hpi_ctl->name, "%s %d %s%s",
  1221. asihpi_src_names[hpi_ctl->src_node_type],
  1222. hpi_ctl->src_node_index,
  1223. dir, name);
  1224. }
  1225. /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
  1226. hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
  1227. }
  1228. /*------------------------------------------------------------
  1229. Volume controls
  1230. ------------------------------------------------------------*/
  1231. #define VOL_STEP_mB 1
  1232. static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
  1233. struct snd_ctl_elem_info *uinfo)
  1234. {
  1235. u32 h_control = kcontrol->private_value;
  1236. u32 count;
  1237. u16 err;
  1238. /* native gains are in millibels */
  1239. short min_gain_mB;
  1240. short max_gain_mB;
  1241. short step_gain_mB;
  1242. err = hpi_volume_query_range(h_control,
  1243. &min_gain_mB, &max_gain_mB, &step_gain_mB);
  1244. if (err) {
  1245. max_gain_mB = 0;
  1246. min_gain_mB = -10000;
  1247. step_gain_mB = VOL_STEP_mB;
  1248. }
  1249. err = hpi_meter_query_channels(h_control, &count);
  1250. if (err)
  1251. count = HPI_MAX_CHANNELS;
  1252. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1253. uinfo->count = count;
  1254. uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
  1255. uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
  1256. uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
  1257. return 0;
  1258. }
  1259. static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
  1260. struct snd_ctl_elem_value *ucontrol)
  1261. {
  1262. u32 h_control = kcontrol->private_value;
  1263. short an_gain_mB[HPI_MAX_CHANNELS];
  1264. hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
  1265. ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
  1266. ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
  1267. return 0;
  1268. }
  1269. static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
  1270. struct snd_ctl_elem_value *ucontrol)
  1271. {
  1272. u32 h_control = kcontrol->private_value;
  1273. short an_gain_mB[HPI_MAX_CHANNELS];
  1274. an_gain_mB[0] =
  1275. (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
  1276. an_gain_mB[1] =
  1277. (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
  1278. /* change = asihpi->mixer_volume[addr][0] != left ||
  1279. asihpi->mixer_volume[addr][1] != right;
  1280. */
  1281. hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
  1282. return 1;
  1283. }
  1284. static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
  1285. #define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
  1286. static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
  1287. struct snd_ctl_elem_value *ucontrol)
  1288. {
  1289. u32 h_control = kcontrol->private_value;
  1290. u32 mute;
  1291. hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
  1292. ucontrol->value.integer.value[0] = mute ? 0 : 1;
  1293. return 0;
  1294. }
  1295. static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
  1296. struct snd_ctl_elem_value *ucontrol)
  1297. {
  1298. u32 h_control = kcontrol->private_value;
  1299. /* HPI currently only supports all or none muting of multichannel volume
  1300. ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
  1301. */
  1302. int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
  1303. hpi_handle_error(hpi_volume_set_mute(h_control, mute));
  1304. return 1;
  1305. }
  1306. static int snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
  1307. struct hpi_control *hpi_ctl)
  1308. {
  1309. struct snd_card *card = asihpi->card;
  1310. struct snd_kcontrol_new snd_control;
  1311. int err;
  1312. u32 mute;
  1313. asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
  1314. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1315. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1316. snd_control.info = snd_asihpi_volume_info;
  1317. snd_control.get = snd_asihpi_volume_get;
  1318. snd_control.put = snd_asihpi_volume_put;
  1319. snd_control.tlv.p = db_scale_100;
  1320. err = ctl_add(card, &snd_control, asihpi);
  1321. if (err)
  1322. return err;
  1323. if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
  1324. asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
  1325. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1326. snd_control.info = snd_asihpi_volume_mute_info;
  1327. snd_control.get = snd_asihpi_volume_mute_get;
  1328. snd_control.put = snd_asihpi_volume_mute_put;
  1329. err = ctl_add(card, &snd_control, asihpi);
  1330. }
  1331. return err;
  1332. }
  1333. /*------------------------------------------------------------
  1334. Level controls
  1335. ------------------------------------------------------------*/
  1336. static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
  1337. struct snd_ctl_elem_info *uinfo)
  1338. {
  1339. u32 h_control = kcontrol->private_value;
  1340. u16 err;
  1341. short min_gain_mB;
  1342. short max_gain_mB;
  1343. short step_gain_mB;
  1344. err =
  1345. hpi_level_query_range(h_control, &min_gain_mB,
  1346. &max_gain_mB, &step_gain_mB);
  1347. if (err) {
  1348. max_gain_mB = 2400;
  1349. min_gain_mB = -1000;
  1350. step_gain_mB = 100;
  1351. }
  1352. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1353. uinfo->count = 2;
  1354. uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
  1355. uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
  1356. uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
  1357. return 0;
  1358. }
  1359. static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
  1360. struct snd_ctl_elem_value *ucontrol)
  1361. {
  1362. u32 h_control = kcontrol->private_value;
  1363. short an_gain_mB[HPI_MAX_CHANNELS];
  1364. hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
  1365. ucontrol->value.integer.value[0] =
  1366. an_gain_mB[0] / HPI_UNITS_PER_dB;
  1367. ucontrol->value.integer.value[1] =
  1368. an_gain_mB[1] / HPI_UNITS_PER_dB;
  1369. return 0;
  1370. }
  1371. static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
  1372. struct snd_ctl_elem_value *ucontrol)
  1373. {
  1374. int change;
  1375. u32 h_control = kcontrol->private_value;
  1376. short an_gain_mB[HPI_MAX_CHANNELS];
  1377. an_gain_mB[0] =
  1378. (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
  1379. an_gain_mB[1] =
  1380. (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
  1381. /* change = asihpi->mixer_level[addr][0] != left ||
  1382. asihpi->mixer_level[addr][1] != right;
  1383. */
  1384. change = 1;
  1385. hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
  1386. return change;
  1387. }
  1388. static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
  1389. static int snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
  1390. struct hpi_control *hpi_ctl)
  1391. {
  1392. struct snd_card *card = asihpi->card;
  1393. struct snd_kcontrol_new snd_control;
  1394. /* can't use 'volume' cos some nodes have volume as well */
  1395. asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
  1396. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  1397. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  1398. snd_control.info = snd_asihpi_level_info;
  1399. snd_control.get = snd_asihpi_level_get;
  1400. snd_control.put = snd_asihpi_level_put;
  1401. snd_control.tlv.p = db_scale_level;
  1402. return ctl_add(card, &snd_control, asihpi);
  1403. }
  1404. /*------------------------------------------------------------
  1405. AESEBU controls
  1406. ------------------------------------------------------------*/
  1407. /* AESEBU format */
  1408. static const char * const asihpi_aesebu_format_names[] = {
  1409. "N/A", "S/PDIF", "AES/EBU" };
  1410. static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
  1411. struct snd_ctl_elem_info *uinfo)
  1412. {
  1413. return snd_ctl_enum_info(uinfo, 1, 3, asihpi_aesebu_format_names);
  1414. }
  1415. static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
  1416. struct snd_ctl_elem_value *ucontrol,
  1417. u16 (*func)(u32, u16 *))
  1418. {
  1419. u32 h_control = kcontrol->private_value;
  1420. u16 source, err;
  1421. err = func(h_control, &source);
  1422. /* default to N/A */
  1423. ucontrol->value.enumerated.item[0] = 0;
  1424. /* return success but set the control to N/A */
  1425. if (err)
  1426. return 0;
  1427. if (source == HPI_AESEBU_FORMAT_SPDIF)
  1428. ucontrol->value.enumerated.item[0] = 1;
  1429. if (source == HPI_AESEBU_FORMAT_AESEBU)
  1430. ucontrol->value.enumerated.item[0] = 2;
  1431. return 0;
  1432. }
  1433. static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
  1434. struct snd_ctl_elem_value *ucontrol,
  1435. u16 (*func)(u32, u16))
  1436. {
  1437. u32 h_control = kcontrol->private_value;
  1438. /* default to S/PDIF */
  1439. u16 source = HPI_AESEBU_FORMAT_SPDIF;
  1440. if (ucontrol->value.enumerated.item[0] == 1)
  1441. source = HPI_AESEBU_FORMAT_SPDIF;
  1442. if (ucontrol->value.enumerated.item[0] == 2)
  1443. source = HPI_AESEBU_FORMAT_AESEBU;
  1444. if (func(h_control, source) != 0)
  1445. return -EINVAL;
  1446. return 1;
  1447. }
  1448. static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
  1449. struct snd_ctl_elem_value *ucontrol) {
  1450. return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
  1451. hpi_aesebu_receiver_get_format);
  1452. }
  1453. static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
  1454. struct snd_ctl_elem_value *ucontrol) {
  1455. return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
  1456. hpi_aesebu_receiver_set_format);
  1457. }
  1458. static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
  1459. struct snd_ctl_elem_info *uinfo)
  1460. {
  1461. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1462. uinfo->count = 1;
  1463. uinfo->value.integer.min = 0;
  1464. uinfo->value.integer.max = 0X1F;
  1465. uinfo->value.integer.step = 1;
  1466. return 0;
  1467. }
  1468. static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
  1469. struct snd_ctl_elem_value *ucontrol) {
  1470. u32 h_control = kcontrol->private_value;
  1471. u16 status;
  1472. hpi_handle_error(hpi_aesebu_receiver_get_error_status(
  1473. h_control, &status));
  1474. ucontrol->value.integer.value[0] = status;
  1475. return 0;
  1476. }
  1477. static int snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
  1478. struct hpi_control *hpi_ctl)
  1479. {
  1480. struct snd_card *card = asihpi->card;
  1481. struct snd_kcontrol_new snd_control;
  1482. asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
  1483. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1484. snd_control.info = snd_asihpi_aesebu_format_info;
  1485. snd_control.get = snd_asihpi_aesebu_rx_format_get;
  1486. snd_control.put = snd_asihpi_aesebu_rx_format_put;
  1487. if (ctl_add(card, &snd_control, asihpi) < 0)
  1488. return -EINVAL;
  1489. asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
  1490. snd_control.access =
  1491. SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  1492. snd_control.info = snd_asihpi_aesebu_rxstatus_info;
  1493. snd_control.get = snd_asihpi_aesebu_rxstatus_get;
  1494. return ctl_add(card, &snd_control, asihpi);
  1495. }
  1496. static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
  1497. struct snd_ctl_elem_value *ucontrol) {
  1498. return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
  1499. hpi_aesebu_transmitter_get_format);
  1500. }
  1501. static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
  1502. struct snd_ctl_elem_value *ucontrol) {
  1503. return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
  1504. hpi_aesebu_transmitter_set_format);
  1505. }
  1506. static int snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
  1507. struct hpi_control *hpi_ctl)
  1508. {
  1509. struct snd_card *card = asihpi->card;
  1510. struct snd_kcontrol_new snd_control;
  1511. asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
  1512. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1513. snd_control.info = snd_asihpi_aesebu_format_info;
  1514. snd_control.get = snd_asihpi_aesebu_tx_format_get;
  1515. snd_control.put = snd_asihpi_aesebu_tx_format_put;
  1516. return ctl_add(card, &snd_control, asihpi);
  1517. }
  1518. /*------------------------------------------------------------
  1519. Tuner controls
  1520. ------------------------------------------------------------*/
  1521. /* Gain */
  1522. static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
  1523. struct snd_ctl_elem_info *uinfo)
  1524. {
  1525. u32 h_control = kcontrol->private_value;
  1526. u16 err;
  1527. short idx;
  1528. u16 gain_range[3];
  1529. for (idx = 0; idx < 3; idx++) {
  1530. err = hpi_tuner_query_gain(h_control,
  1531. idx, &gain_range[idx]);
  1532. if (err != 0)
  1533. return err;
  1534. }
  1535. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1536. uinfo->count = 1;
  1537. uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
  1538. uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
  1539. uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
  1540. return 0;
  1541. }
  1542. static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
  1543. struct snd_ctl_elem_value *ucontrol)
  1544. {
  1545. /*
  1546. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1547. */
  1548. u32 h_control = kcontrol->private_value;
  1549. short gain;
  1550. hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
  1551. ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
  1552. return 0;
  1553. }
  1554. static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
  1555. struct snd_ctl_elem_value *ucontrol)
  1556. {
  1557. /*
  1558. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1559. */
  1560. u32 h_control = kcontrol->private_value;
  1561. short gain;
  1562. gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
  1563. hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
  1564. return 1;
  1565. }
  1566. /* Band */
  1567. static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
  1568. u16 *band_list, u32 len) {
  1569. u32 h_control = kcontrol->private_value;
  1570. u16 err = 0;
  1571. u32 i;
  1572. for (i = 0; i < len; i++) {
  1573. err = hpi_tuner_query_band(
  1574. h_control, i, &band_list[i]);
  1575. if (err != 0)
  1576. break;
  1577. }
  1578. if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
  1579. return -EIO;
  1580. return i;
  1581. }
  1582. static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
  1583. struct snd_ctl_elem_info *uinfo)
  1584. {
  1585. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1586. int num_bands = 0;
  1587. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1588. HPI_TUNER_BAND_LAST);
  1589. if (num_bands < 0)
  1590. return num_bands;
  1591. return snd_ctl_enum_info(uinfo, 1, num_bands, asihpi_tuner_band_names);
  1592. }
  1593. static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
  1594. struct snd_ctl_elem_value *ucontrol)
  1595. {
  1596. u32 h_control = kcontrol->private_value;
  1597. /*
  1598. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1599. */
  1600. u16 band, idx;
  1601. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1602. __always_unused u32 num_bands;
  1603. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1604. HPI_TUNER_BAND_LAST);
  1605. hpi_handle_error(hpi_tuner_get_band(h_control, &band));
  1606. ucontrol->value.enumerated.item[0] = -1;
  1607. for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
  1608. if (tuner_bands[idx] == band) {
  1609. ucontrol->value.enumerated.item[0] = idx;
  1610. break;
  1611. }
  1612. return 0;
  1613. }
  1614. static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
  1615. struct snd_ctl_elem_value *ucontrol)
  1616. {
  1617. /*
  1618. struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
  1619. */
  1620. u32 h_control = kcontrol->private_value;
  1621. unsigned int idx;
  1622. u16 band;
  1623. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1624. __always_unused u32 num_bands;
  1625. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1626. HPI_TUNER_BAND_LAST);
  1627. idx = ucontrol->value.enumerated.item[0];
  1628. if (idx >= ARRAY_SIZE(tuner_bands))
  1629. idx = ARRAY_SIZE(tuner_bands) - 1;
  1630. band = tuner_bands[idx];
  1631. hpi_handle_error(hpi_tuner_set_band(h_control, band));
  1632. return 1;
  1633. }
  1634. /* Freq */
  1635. static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
  1636. struct snd_ctl_elem_info *uinfo)
  1637. {
  1638. u32 h_control = kcontrol->private_value;
  1639. u16 err;
  1640. u16 tuner_bands[HPI_TUNER_BAND_LAST];
  1641. u16 num_bands = 0, band_iter, idx;
  1642. u32 freq_range[3], temp_freq_range[3];
  1643. num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
  1644. HPI_TUNER_BAND_LAST);
  1645. freq_range[0] = INT_MAX;
  1646. freq_range[1] = 0;
  1647. freq_range[2] = INT_MAX;
  1648. for (band_iter = 0; band_iter < num_bands; band_iter++) {
  1649. for (idx = 0; idx < 3; idx++) {
  1650. err = hpi_tuner_query_frequency(h_control,
  1651. idx, tuner_bands[band_iter],
  1652. &temp_freq_range[idx]);
  1653. if (err != 0)
  1654. return err;
  1655. }
  1656. /* skip band with bogus stepping */
  1657. if (temp_freq_range[2] <= 0)
  1658. continue;
  1659. if (temp_freq_range[0] < freq_range[0])
  1660. freq_range[0] = temp_freq_range[0];
  1661. if (temp_freq_range[1] > freq_range[1])
  1662. freq_range[1] = temp_freq_range[1];
  1663. if (temp_freq_range[2] < freq_range[2])
  1664. freq_range[2] = temp_freq_range[2];
  1665. }
  1666. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1667. uinfo->count = 1;
  1668. uinfo->value.integer.min = ((int)freq_range[0]);
  1669. uinfo->value.integer.max = ((int)freq_range[1]);
  1670. uinfo->value.integer.step = ((int)freq_range[2]);
  1671. return 0;
  1672. }
  1673. static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
  1674. struct snd_ctl_elem_value *ucontrol)
  1675. {
  1676. u32 h_control = kcontrol->private_value;
  1677. u32 freq;
  1678. hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
  1679. ucontrol->value.integer.value[0] = freq;
  1680. return 0;
  1681. }
  1682. static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
  1683. struct snd_ctl_elem_value *ucontrol)
  1684. {
  1685. u32 h_control = kcontrol->private_value;
  1686. u32 freq;
  1687. freq = ucontrol->value.integer.value[0];
  1688. hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
  1689. return 1;
  1690. }
  1691. /* Tuner control group initializer */
  1692. static int snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
  1693. struct hpi_control *hpi_ctl)
  1694. {
  1695. struct snd_card *card = asihpi->card;
  1696. struct snd_kcontrol_new snd_control;
  1697. snd_control.private_value = hpi_ctl->h_control;
  1698. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1699. if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
  1700. asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
  1701. snd_control.info = snd_asihpi_tuner_gain_info;
  1702. snd_control.get = snd_asihpi_tuner_gain_get;
  1703. snd_control.put = snd_asihpi_tuner_gain_put;
  1704. if (ctl_add(card, &snd_control, asihpi) < 0)
  1705. return -EINVAL;
  1706. }
  1707. asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
  1708. snd_control.info = snd_asihpi_tuner_band_info;
  1709. snd_control.get = snd_asihpi_tuner_band_get;
  1710. snd_control.put = snd_asihpi_tuner_band_put;
  1711. if (ctl_add(card, &snd_control, asihpi) < 0)
  1712. return -EINVAL;
  1713. asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
  1714. snd_control.info = snd_asihpi_tuner_freq_info;
  1715. snd_control.get = snd_asihpi_tuner_freq_get;
  1716. snd_control.put = snd_asihpi_tuner_freq_put;
  1717. return ctl_add(card, &snd_control, asihpi);
  1718. }
  1719. /*------------------------------------------------------------
  1720. Meter controls
  1721. ------------------------------------------------------------*/
  1722. static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
  1723. struct snd_ctl_elem_info *uinfo)
  1724. {
  1725. u32 h_control = kcontrol->private_value;
  1726. u32 count;
  1727. u16 err;
  1728. err = hpi_meter_query_channels(h_control, &count);
  1729. if (err)
  1730. count = HPI_MAX_CHANNELS;
  1731. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1732. uinfo->count = count;
  1733. uinfo->value.integer.min = 0;
  1734. uinfo->value.integer.max = 0x7FFFFFFF;
  1735. return 0;
  1736. }
  1737. /* linear values for 10dB steps */
  1738. static const int log2lin[] = {
  1739. 0x7FFFFFFF, /* 0dB */
  1740. 679093956,
  1741. 214748365,
  1742. 67909396,
  1743. 21474837,
  1744. 6790940,
  1745. 2147484, /* -60dB */
  1746. 679094,
  1747. 214748, /* -80 */
  1748. 67909,
  1749. 21475, /* -100 */
  1750. 6791,
  1751. 2147,
  1752. 679,
  1753. 214,
  1754. 68,
  1755. 21,
  1756. 7,
  1757. 2
  1758. };
  1759. static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
  1760. struct snd_ctl_elem_value *ucontrol)
  1761. {
  1762. u32 h_control = kcontrol->private_value;
  1763. short an_gain_mB[HPI_MAX_CHANNELS], i;
  1764. u16 err;
  1765. err = hpi_meter_get_peak(h_control, an_gain_mB);
  1766. for (i = 0; i < HPI_MAX_CHANNELS; i++) {
  1767. if (err) {
  1768. ucontrol->value.integer.value[i] = 0;
  1769. } else if (an_gain_mB[i] >= 0) {
  1770. ucontrol->value.integer.value[i] =
  1771. an_gain_mB[i] << 16;
  1772. } else {
  1773. /* -ve is log value in millibels < -60dB,
  1774. * convert to (roughly!) linear,
  1775. */
  1776. ucontrol->value.integer.value[i] =
  1777. log2lin[an_gain_mB[i] / -1000];
  1778. }
  1779. }
  1780. return 0;
  1781. }
  1782. static int snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
  1783. struct hpi_control *hpi_ctl, int subidx)
  1784. {
  1785. struct snd_card *card = asihpi->card;
  1786. struct snd_kcontrol_new snd_control;
  1787. asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
  1788. snd_control.access =
  1789. SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  1790. snd_control.info = snd_asihpi_meter_info;
  1791. snd_control.get = snd_asihpi_meter_get;
  1792. snd_control.index = subidx;
  1793. return ctl_add(card, &snd_control, asihpi);
  1794. }
  1795. /*------------------------------------------------------------
  1796. Multiplexer controls
  1797. ------------------------------------------------------------*/
  1798. static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
  1799. {
  1800. u32 h_control = snd_control->private_value;
  1801. struct hpi_control hpi_ctl;
  1802. int s, err;
  1803. for (s = 0; s < 32; s++) {
  1804. err = hpi_multiplexer_query_source(h_control, s,
  1805. &hpi_ctl.
  1806. src_node_type,
  1807. &hpi_ctl.
  1808. src_node_index);
  1809. if (err)
  1810. break;
  1811. }
  1812. return s;
  1813. }
  1814. static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
  1815. struct snd_ctl_elem_info *uinfo)
  1816. {
  1817. u16 src_node_type, src_node_index;
  1818. u32 h_control = kcontrol->private_value;
  1819. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1820. uinfo->count = 1;
  1821. uinfo->value.enumerated.items =
  1822. snd_card_asihpi_mux_count_sources(kcontrol);
  1823. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  1824. uinfo->value.enumerated.item =
  1825. uinfo->value.enumerated.items - 1;
  1826. hpi_multiplexer_query_source(h_control,
  1827. uinfo->value.enumerated.item,
  1828. &src_node_type, &src_node_index);
  1829. sprintf(uinfo->value.enumerated.name, "%s %d",
  1830. asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
  1831. src_node_index);
  1832. return 0;
  1833. }
  1834. static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
  1835. struct snd_ctl_elem_value *ucontrol)
  1836. {
  1837. u32 h_control = kcontrol->private_value;
  1838. u16 source_type, source_index;
  1839. u16 src_node_type, src_node_index;
  1840. int s;
  1841. hpi_handle_error(hpi_multiplexer_get_source(h_control,
  1842. &source_type, &source_index));
  1843. /* Should cache this search result! */
  1844. for (s = 0; s < 256; s++) {
  1845. if (hpi_multiplexer_query_source(h_control, s,
  1846. &src_node_type, &src_node_index))
  1847. break;
  1848. if ((source_type == src_node_type)
  1849. && (source_index == src_node_index)) {
  1850. ucontrol->value.enumerated.item[0] = s;
  1851. return 0;
  1852. }
  1853. }
  1854. snd_printd(KERN_WARNING
  1855. "Control %x failed to match mux source %hu %hu\n",
  1856. h_control, source_type, source_index);
  1857. ucontrol->value.enumerated.item[0] = 0;
  1858. return 0;
  1859. }
  1860. static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
  1861. struct snd_ctl_elem_value *ucontrol)
  1862. {
  1863. int change;
  1864. u32 h_control = kcontrol->private_value;
  1865. u16 source_type, source_index;
  1866. u16 e;
  1867. change = 1;
  1868. e = hpi_multiplexer_query_source(h_control,
  1869. ucontrol->value.enumerated.item[0],
  1870. &source_type, &source_index);
  1871. if (!e)
  1872. hpi_handle_error(
  1873. hpi_multiplexer_set_source(h_control,
  1874. source_type, source_index));
  1875. return change;
  1876. }
  1877. static int snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
  1878. struct hpi_control *hpi_ctl)
  1879. {
  1880. struct snd_card *card = asihpi->card;
  1881. struct snd_kcontrol_new snd_control;
  1882. asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
  1883. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1884. snd_control.info = snd_asihpi_mux_info;
  1885. snd_control.get = snd_asihpi_mux_get;
  1886. snd_control.put = snd_asihpi_mux_put;
  1887. return ctl_add(card, &snd_control, asihpi);
  1888. }
  1889. /*------------------------------------------------------------
  1890. Channel mode controls
  1891. ------------------------------------------------------------*/
  1892. static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
  1893. struct snd_ctl_elem_info *uinfo)
  1894. {
  1895. static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
  1896. "invalid",
  1897. "Normal", "Swap",
  1898. "From Left", "From Right",
  1899. "To Left", "To Right"
  1900. };
  1901. u32 h_control = kcontrol->private_value;
  1902. u16 mode;
  1903. int i;
  1904. const char *mapped_names[6];
  1905. int valid_modes = 0;
  1906. /* HPI channel mode values can be from 1 to 6
  1907. Some adapters only support a contiguous subset
  1908. */
  1909. for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
  1910. if (!hpi_channel_mode_query_mode(
  1911. h_control, i, &mode)) {
  1912. mapped_names[valid_modes] = mode_names[mode];
  1913. valid_modes++;
  1914. }
  1915. if (!valid_modes)
  1916. return -EINVAL;
  1917. return snd_ctl_enum_info(uinfo, 1, valid_modes, mapped_names);
  1918. }
  1919. static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
  1920. struct snd_ctl_elem_value *ucontrol)
  1921. {
  1922. u32 h_control = kcontrol->private_value;
  1923. u16 mode;
  1924. if (hpi_channel_mode_get(h_control, &mode))
  1925. mode = 1;
  1926. ucontrol->value.enumerated.item[0] = mode - 1;
  1927. return 0;
  1928. }
  1929. static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
  1930. struct snd_ctl_elem_value *ucontrol)
  1931. {
  1932. int change;
  1933. u32 h_control = kcontrol->private_value;
  1934. change = 1;
  1935. hpi_handle_error(hpi_channel_mode_set(h_control,
  1936. ucontrol->value.enumerated.item[0] + 1));
  1937. return change;
  1938. }
  1939. static int snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
  1940. struct hpi_control *hpi_ctl)
  1941. {
  1942. struct snd_card *card = asihpi->card;
  1943. struct snd_kcontrol_new snd_control;
  1944. asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
  1945. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
  1946. snd_control.info = snd_asihpi_cmode_info;
  1947. snd_control.get = snd_asihpi_cmode_get;
  1948. snd_control.put = snd_asihpi_cmode_put;
  1949. return ctl_add(card, &snd_control, asihpi);
  1950. }
  1951. /*------------------------------------------------------------
  1952. Sampleclock source controls
  1953. ------------------------------------------------------------*/
  1954. static const char * const sampleclock_sources[] = {
  1955. "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
  1956. "SMPTE", "Digital1", "Auto", "Network", "Invalid",
  1957. "Prev Module", "BLU-Link",
  1958. "Digital2", "Digital3", "Digital4", "Digital5",
  1959. "Digital6", "Digital7", "Digital8"};
  1960. /* Number of strings must match expected enumerated values */
  1961. compile_time_assert(
  1962. (ARRAY_SIZE(sampleclock_sources) == MAX_CLOCKSOURCES),
  1963. assert_sampleclock_sources_size);
  1964. static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
  1965. struct snd_ctl_elem_info *uinfo)
  1966. {
  1967. struct snd_card_asihpi *asihpi =
  1968. (struct snd_card_asihpi *)(kcontrol->private_data);
  1969. struct clk_cache *clkcache = &asihpi->cc;
  1970. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1971. uinfo->count = 1;
  1972. uinfo->value.enumerated.items = clkcache->count;
  1973. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  1974. uinfo->value.enumerated.item =
  1975. uinfo->value.enumerated.items - 1;
  1976. strcpy(uinfo->value.enumerated.name,
  1977. clkcache->s[uinfo->value.enumerated.item].name);
  1978. return 0;
  1979. }
  1980. static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
  1981. struct snd_ctl_elem_value *ucontrol)
  1982. {
  1983. struct snd_card_asihpi *asihpi =
  1984. (struct snd_card_asihpi *)(kcontrol->private_data);
  1985. struct clk_cache *clkcache = &asihpi->cc;
  1986. u32 h_control = kcontrol->private_value;
  1987. u16 source, srcindex = 0;
  1988. int i;
  1989. ucontrol->value.enumerated.item[0] = 0;
  1990. if (hpi_sample_clock_get_source(h_control, &source))
  1991. source = 0;
  1992. if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  1993. if (hpi_sample_clock_get_source_index(h_control, &srcindex))
  1994. srcindex = 0;
  1995. for (i = 0; i < clkcache->count; i++)
  1996. if ((clkcache->s[i].source == source) &&
  1997. (clkcache->s[i].index == srcindex))
  1998. break;
  1999. ucontrol->value.enumerated.item[0] = i;
  2000. return 0;
  2001. }
  2002. static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
  2003. struct snd_ctl_elem_value *ucontrol)
  2004. {
  2005. struct snd_card_asihpi *asihpi =
  2006. (struct snd_card_asihpi *)(kcontrol->private_data);
  2007. struct clk_cache *clkcache = &asihpi->cc;
  2008. unsigned int item;
  2009. int change;
  2010. u32 h_control = kcontrol->private_value;
  2011. change = 1;
  2012. item = ucontrol->value.enumerated.item[0];
  2013. if (item >= clkcache->count)
  2014. item = clkcache->count-1;
  2015. hpi_handle_error(hpi_sample_clock_set_source(
  2016. h_control, clkcache->s[item].source));
  2017. if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  2018. hpi_handle_error(hpi_sample_clock_set_source_index(
  2019. h_control, clkcache->s[item].index));
  2020. return change;
  2021. }
  2022. /*------------------------------------------------------------
  2023. Clkrate controls
  2024. ------------------------------------------------------------*/
  2025. /* Need to change this to enumerated control with list of rates */
  2026. static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
  2027. struct snd_ctl_elem_info *uinfo)
  2028. {
  2029. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2030. uinfo->count = 1;
  2031. uinfo->value.integer.min = 8000;
  2032. uinfo->value.integer.max = 192000;
  2033. uinfo->value.integer.step = 100;
  2034. return 0;
  2035. }
  2036. static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
  2037. struct snd_ctl_elem_value *ucontrol)
  2038. {
  2039. u32 h_control = kcontrol->private_value;
  2040. u32 rate;
  2041. u16 e;
  2042. e = hpi_sample_clock_get_local_rate(h_control, &rate);
  2043. if (!e)
  2044. ucontrol->value.integer.value[0] = rate;
  2045. else
  2046. ucontrol->value.integer.value[0] = 0;
  2047. return 0;
  2048. }
  2049. static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
  2050. struct snd_ctl_elem_value *ucontrol)
  2051. {
  2052. int change;
  2053. u32 h_control = kcontrol->private_value;
  2054. /* change = asihpi->mixer_clkrate[addr][0] != left ||
  2055. asihpi->mixer_clkrate[addr][1] != right;
  2056. */
  2057. change = 1;
  2058. hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
  2059. ucontrol->value.integer.value[0]));
  2060. return change;
  2061. }
  2062. static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
  2063. struct snd_ctl_elem_info *uinfo)
  2064. {
  2065. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2066. uinfo->count = 1;
  2067. uinfo->value.integer.min = 8000;
  2068. uinfo->value.integer.max = 192000;
  2069. uinfo->value.integer.step = 100;
  2070. return 0;
  2071. }
  2072. static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
  2073. struct snd_ctl_elem_value *ucontrol)
  2074. {
  2075. u32 h_control = kcontrol->private_value;
  2076. u32 rate;
  2077. u16 e;
  2078. e = hpi_sample_clock_get_sample_rate(h_control, &rate);
  2079. if (!e)
  2080. ucontrol->value.integer.value[0] = rate;
  2081. else
  2082. ucontrol->value.integer.value[0] = 0;
  2083. return 0;
  2084. }
  2085. static int snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
  2086. struct hpi_control *hpi_ctl)
  2087. {
  2088. struct snd_card *card;
  2089. struct snd_kcontrol_new snd_control;
  2090. struct clk_cache *clkcache;
  2091. u32 hSC = hpi_ctl->h_control;
  2092. int has_aes_in = 0;
  2093. int i, j;
  2094. u16 source;
  2095. if (snd_BUG_ON(!asihpi))
  2096. return -EINVAL;
  2097. card = asihpi->card;
  2098. clkcache = &asihpi->cc;
  2099. snd_control.private_value = hpi_ctl->h_control;
  2100. clkcache->has_local = 0;
  2101. for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
  2102. if (hpi_sample_clock_query_source(hSC,
  2103. i, &source))
  2104. break;
  2105. clkcache->s[i].source = source;
  2106. clkcache->s[i].index = 0;
  2107. clkcache->s[i].name = sampleclock_sources[source];
  2108. if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
  2109. has_aes_in = 1;
  2110. if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
  2111. clkcache->has_local = 1;
  2112. }
  2113. if (has_aes_in)
  2114. /* already will have picked up index 0 above */
  2115. for (j = 1; j < 8; j++) {
  2116. if (hpi_sample_clock_query_source_index(hSC,
  2117. j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
  2118. &source))
  2119. break;
  2120. clkcache->s[i].source =
  2121. HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
  2122. clkcache->s[i].index = j;
  2123. clkcache->s[i].name = sampleclock_sources[
  2124. j+HPI_SAMPLECLOCK_SOURCE_LAST];
  2125. i++;
  2126. }
  2127. clkcache->count = i;
  2128. asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
  2129. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
  2130. snd_control.info = snd_asihpi_clksrc_info;
  2131. snd_control.get = snd_asihpi_clksrc_get;
  2132. snd_control.put = snd_asihpi_clksrc_put;
  2133. if (ctl_add(card, &snd_control, asihpi) < 0)
  2134. return -EINVAL;
  2135. if (clkcache->has_local) {
  2136. asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
  2137. snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
  2138. snd_control.info = snd_asihpi_clklocal_info;
  2139. snd_control.get = snd_asihpi_clklocal_get;
  2140. snd_control.put = snd_asihpi_clklocal_put;
  2141. if (ctl_add(card, &snd_control, asihpi) < 0)
  2142. return -EINVAL;
  2143. }
  2144. asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
  2145. snd_control.access =
  2146. SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
  2147. snd_control.info = snd_asihpi_clkrate_info;
  2148. snd_control.get = snd_asihpi_clkrate_get;
  2149. return ctl_add(card, &snd_control, asihpi);
  2150. }
  2151. /*------------------------------------------------------------
  2152. Mixer
  2153. ------------------------------------------------------------*/
  2154. static int snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
  2155. {
  2156. struct snd_card *card;
  2157. unsigned int idx = 0;
  2158. unsigned int subindex = 0;
  2159. int err;
  2160. struct hpi_control hpi_ctl, prev_ctl;
  2161. if (snd_BUG_ON(!asihpi))
  2162. return -EINVAL;
  2163. card = asihpi->card;
  2164. strcpy(card->mixername, "Asihpi Mixer");
  2165. err =
  2166. hpi_mixer_open(asihpi->hpi->adapter->index,
  2167. &asihpi->h_mixer);
  2168. hpi_handle_error(err);
  2169. if (err)
  2170. return -err;
  2171. memset(&prev_ctl, 0, sizeof(prev_ctl));
  2172. prev_ctl.control_type = -1;
  2173. for (idx = 0; idx < 2000; idx++) {
  2174. err = hpi_mixer_get_control_by_index(
  2175. asihpi->h_mixer,
  2176. idx,
  2177. &hpi_ctl.src_node_type,
  2178. &hpi_ctl.src_node_index,
  2179. &hpi_ctl.dst_node_type,
  2180. &hpi_ctl.dst_node_index,
  2181. &hpi_ctl.control_type,
  2182. &hpi_ctl.h_control);
  2183. if (err) {
  2184. if (err == HPI_ERROR_CONTROL_DISABLED) {
  2185. if (mixer_dump)
  2186. dev_info(&asihpi->pci->dev,
  2187. "Disabled HPI Control(%d)\n",
  2188. idx);
  2189. continue;
  2190. } else
  2191. break;
  2192. }
  2193. hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
  2194. hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
  2195. /* ASI50xx in SSX mode has multiple meters on the same node.
  2196. Use subindex to create distinct ALSA controls
  2197. for any duplicated controls.
  2198. */
  2199. if ((hpi_ctl.control_type == prev_ctl.control_type) &&
  2200. (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
  2201. (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
  2202. (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
  2203. (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
  2204. subindex++;
  2205. else
  2206. subindex = 0;
  2207. prev_ctl = hpi_ctl;
  2208. switch (hpi_ctl.control_type) {
  2209. case HPI_CONTROL_VOLUME:
  2210. err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
  2211. break;
  2212. case HPI_CONTROL_LEVEL:
  2213. err = snd_asihpi_level_add(asihpi, &hpi_ctl);
  2214. break;
  2215. case HPI_CONTROL_MULTIPLEXER:
  2216. err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
  2217. break;
  2218. case HPI_CONTROL_CHANNEL_MODE:
  2219. err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
  2220. break;
  2221. case HPI_CONTROL_METER:
  2222. err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
  2223. break;
  2224. case HPI_CONTROL_SAMPLECLOCK:
  2225. err = snd_asihpi_sampleclock_add(
  2226. asihpi, &hpi_ctl);
  2227. break;
  2228. case HPI_CONTROL_CONNECTION: /* ignore these */
  2229. continue;
  2230. case HPI_CONTROL_TUNER:
  2231. err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
  2232. break;
  2233. case HPI_CONTROL_AESEBU_TRANSMITTER:
  2234. err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
  2235. break;
  2236. case HPI_CONTROL_AESEBU_RECEIVER:
  2237. err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
  2238. break;
  2239. case HPI_CONTROL_VOX:
  2240. case HPI_CONTROL_BITSTREAM:
  2241. case HPI_CONTROL_MICROPHONE:
  2242. case HPI_CONTROL_PARAMETRIC_EQ:
  2243. case HPI_CONTROL_COMPANDER:
  2244. default:
  2245. if (mixer_dump)
  2246. dev_info(&asihpi->pci->dev,
  2247. "Untranslated HPI Control (%d) %d %d %d %d %d\n",
  2248. idx,
  2249. hpi_ctl.control_type,
  2250. hpi_ctl.src_node_type,
  2251. hpi_ctl.src_node_index,
  2252. hpi_ctl.dst_node_type,
  2253. hpi_ctl.dst_node_index);
  2254. continue;
  2255. }
  2256. if (err < 0)
  2257. return err;
  2258. }
  2259. if (HPI_ERROR_INVALID_OBJ_INDEX != err)
  2260. hpi_handle_error(err);
  2261. dev_info(&asihpi->pci->dev, "%d mixer controls found\n", idx);
  2262. return 0;
  2263. }
  2264. /*------------------------------------------------------------
  2265. /proc interface
  2266. ------------------------------------------------------------*/
  2267. static void
  2268. snd_asihpi_proc_read(struct snd_info_entry *entry,
  2269. struct snd_info_buffer *buffer)
  2270. {
  2271. struct snd_card_asihpi *asihpi = entry->private_data;
  2272. u32 h_control;
  2273. u32 rate = 0;
  2274. u16 source = 0;
  2275. u16 num_outstreams;
  2276. u16 num_instreams;
  2277. u16 version;
  2278. u32 serial_number;
  2279. u16 type;
  2280. int err;
  2281. snd_iprintf(buffer, "ASIHPI driver proc file\n");
  2282. hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
  2283. &num_outstreams, &num_instreams,
  2284. &version, &serial_number, &type));
  2285. snd_iprintf(buffer,
  2286. "Adapter type ASI%4X\nHardware Index %d\n"
  2287. "%d outstreams\n%d instreams\n",
  2288. type, asihpi->hpi->adapter->index,
  2289. num_outstreams, num_instreams);
  2290. snd_iprintf(buffer,
  2291. "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
  2292. serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
  2293. ((version >> 13) * 100) + ((version >> 7) & 0x3f));
  2294. err = hpi_mixer_get_control(asihpi->h_mixer,
  2295. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  2296. HPI_CONTROL_SAMPLECLOCK, &h_control);
  2297. if (!err) {
  2298. err = hpi_sample_clock_get_sample_rate(h_control, &rate);
  2299. err += hpi_sample_clock_get_source(h_control, &source);
  2300. if (!err)
  2301. snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
  2302. rate, sampleclock_sources[source]);
  2303. }
  2304. }
  2305. static void snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
  2306. {
  2307. snd_card_ro_proc_new(asihpi->card, "info", asihpi,
  2308. snd_asihpi_proc_read);
  2309. }
  2310. /*------------------------------------------------------------
  2311. HWDEP
  2312. ------------------------------------------------------------*/
  2313. static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
  2314. {
  2315. if (enable_hpi_hwdep)
  2316. return 0;
  2317. else
  2318. return -ENODEV;
  2319. }
  2320. static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
  2321. {
  2322. if (enable_hpi_hwdep)
  2323. return asihpi_hpi_release(file);
  2324. else
  2325. return -ENODEV;
  2326. }
  2327. static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
  2328. unsigned int cmd, unsigned long arg)
  2329. {
  2330. if (enable_hpi_hwdep)
  2331. return asihpi_hpi_ioctl(file, cmd, arg);
  2332. else
  2333. return -ENODEV;
  2334. }
  2335. /* results in /dev/snd/hwC#D0 file for each card with index #
  2336. also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
  2337. */
  2338. static int snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi, int device)
  2339. {
  2340. struct snd_hwdep *hw;
  2341. int err;
  2342. err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
  2343. if (err < 0)
  2344. return err;
  2345. strcpy(hw->name, "asihpi (HPI)");
  2346. hw->iface = SNDRV_HWDEP_IFACE_LAST;
  2347. hw->ops.open = snd_asihpi_hpi_open;
  2348. hw->ops.ioctl = snd_asihpi_hpi_ioctl;
  2349. hw->ops.release = snd_asihpi_hpi_release;
  2350. hw->private_data = asihpi;
  2351. return 0;
  2352. }
  2353. /*------------------------------------------------------------
  2354. CARD
  2355. ------------------------------------------------------------*/
  2356. static int snd_asihpi_probe(struct pci_dev *pci_dev,
  2357. const struct pci_device_id *pci_id)
  2358. {
  2359. int err;
  2360. struct hpi_adapter *hpi;
  2361. struct snd_card *card;
  2362. struct snd_card_asihpi *asihpi;
  2363. u32 h_control;
  2364. u32 h_stream;
  2365. u32 adapter_index;
  2366. static int dev;
  2367. if (dev >= SNDRV_CARDS)
  2368. return -ENODEV;
  2369. /* Should this be enable[hpi->index] ? */
  2370. if (!enable[dev]) {
  2371. dev++;
  2372. return -ENOENT;
  2373. }
  2374. /* Initialise low-level HPI driver */
  2375. err = asihpi_adapter_probe(pci_dev, pci_id);
  2376. if (err < 0)
  2377. return err;
  2378. hpi = pci_get_drvdata(pci_dev);
  2379. adapter_index = hpi->adapter->index;
  2380. /* first try to give the card the same index as its hardware index */
  2381. err = snd_card_new(&pci_dev->dev, adapter_index, id[adapter_index],
  2382. THIS_MODULE, sizeof(struct snd_card_asihpi), &card);
  2383. if (err < 0) {
  2384. /* if that fails, try the default index==next available */
  2385. err = snd_card_new(&pci_dev->dev, index[dev], id[dev],
  2386. THIS_MODULE, sizeof(struct snd_card_asihpi),
  2387. &card);
  2388. if (err < 0)
  2389. return err;
  2390. dev_warn(&pci_dev->dev, "Adapter index %d->ALSA index %d\n",
  2391. adapter_index, card->number);
  2392. }
  2393. asihpi = card->private_data;
  2394. asihpi->card = card;
  2395. asihpi->pci = pci_dev;
  2396. asihpi->hpi = hpi;
  2397. hpi->snd_card = card;
  2398. err = hpi_adapter_get_property(adapter_index,
  2399. HPI_ADAPTER_PROPERTY_CAPS1,
  2400. NULL, &asihpi->support_grouping);
  2401. if (err)
  2402. asihpi->support_grouping = 0;
  2403. err = hpi_adapter_get_property(adapter_index,
  2404. HPI_ADAPTER_PROPERTY_CAPS2,
  2405. &asihpi->support_mrx, NULL);
  2406. if (err)
  2407. asihpi->support_mrx = 0;
  2408. err = hpi_adapter_get_property(adapter_index,
  2409. HPI_ADAPTER_PROPERTY_INTERVAL,
  2410. NULL, &asihpi->update_interval_frames);
  2411. if (err)
  2412. asihpi->update_interval_frames = 512;
  2413. if (hpi->interrupt_mode) {
  2414. asihpi->pcm_start = snd_card_asihpi_pcm_int_start;
  2415. asihpi->pcm_stop = snd_card_asihpi_pcm_int_stop;
  2416. hpi->interrupt_callback = snd_card_asihpi_isr;
  2417. } else {
  2418. asihpi->pcm_start = snd_card_asihpi_pcm_timer_start;
  2419. asihpi->pcm_stop = snd_card_asihpi_pcm_timer_stop;
  2420. }
  2421. hpi_handle_error(hpi_instream_open(adapter_index,
  2422. 0, &h_stream));
  2423. err = hpi_instream_host_buffer_free(h_stream);
  2424. asihpi->can_dma = (!err);
  2425. hpi_handle_error(hpi_instream_close(h_stream));
  2426. if (!asihpi->can_dma)
  2427. asihpi->update_interval_frames *= 2;
  2428. err = hpi_adapter_get_property(adapter_index,
  2429. HPI_ADAPTER_PROPERTY_CURCHANNELS,
  2430. &asihpi->in_max_chans, &asihpi->out_max_chans);
  2431. if (err) {
  2432. asihpi->in_max_chans = 2;
  2433. asihpi->out_max_chans = 2;
  2434. }
  2435. if (asihpi->out_max_chans > 2) { /* assume LL mode */
  2436. asihpi->out_min_chans = asihpi->out_max_chans;
  2437. asihpi->in_min_chans = asihpi->in_max_chans;
  2438. asihpi->support_grouping = 0;
  2439. } else {
  2440. asihpi->out_min_chans = 1;
  2441. asihpi->in_min_chans = 1;
  2442. }
  2443. dev_info(&pci_dev->dev, "Has dma:%d, grouping:%d, mrx:%d, uif:%d\n",
  2444. asihpi->can_dma,
  2445. asihpi->support_grouping,
  2446. asihpi->support_mrx,
  2447. asihpi->update_interval_frames
  2448. );
  2449. err = snd_card_asihpi_pcm_new(asihpi, 0);
  2450. if (err < 0) {
  2451. dev_err(&pci_dev->dev, "pcm_new failed\n");
  2452. goto __nodev;
  2453. }
  2454. err = snd_card_asihpi_mixer_new(asihpi);
  2455. if (err < 0) {
  2456. dev_err(&pci_dev->dev, "mixer_new failed\n");
  2457. goto __nodev;
  2458. }
  2459. err = hpi_mixer_get_control(asihpi->h_mixer,
  2460. HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
  2461. HPI_CONTROL_SAMPLECLOCK, &h_control);
  2462. if (!err)
  2463. err = hpi_sample_clock_set_local_rate(
  2464. h_control, adapter_fs);
  2465. snd_asihpi_proc_init(asihpi);
  2466. /* always create, can be enabled or disabled dynamically
  2467. by enable_hwdep module param*/
  2468. snd_asihpi_hpi_new(asihpi, 0);
  2469. strcpy(card->driver, "ASIHPI");
  2470. sprintf(card->shortname, "AudioScience ASI%4X",
  2471. asihpi->hpi->adapter->type);
  2472. sprintf(card->longname, "%s %i",
  2473. card->shortname, adapter_index);
  2474. err = snd_card_register(card);
  2475. if (!err) {
  2476. dev++;
  2477. return 0;
  2478. }
  2479. __nodev:
  2480. snd_card_free(card);
  2481. dev_err(&pci_dev->dev, "snd_asihpi_probe error %d\n", err);
  2482. return err;
  2483. }
  2484. static void snd_asihpi_remove(struct pci_dev *pci_dev)
  2485. {
  2486. struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
  2487. /* Stop interrupts */
  2488. if (hpi->interrupt_mode) {
  2489. hpi->interrupt_callback = NULL;
  2490. hpi_handle_error(hpi_adapter_set_property(hpi->adapter->index,
  2491. HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
  2492. }
  2493. snd_card_free(hpi->snd_card);
  2494. hpi->snd_card = NULL;
  2495. asihpi_adapter_remove(pci_dev);
  2496. }
  2497. static const struct pci_device_id asihpi_pci_tbl[] = {
  2498. {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
  2499. HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
  2500. (kernel_ulong_t)HPI_6205},
  2501. {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
  2502. HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
  2503. (kernel_ulong_t)HPI_6000},
  2504. {0,}
  2505. };
  2506. MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
  2507. static struct pci_driver driver = {
  2508. .name = KBUILD_MODNAME,
  2509. .id_table = asihpi_pci_tbl,
  2510. .probe = snd_asihpi_probe,
  2511. .remove = snd_asihpi_remove,
  2512. };
  2513. static int __init snd_asihpi_init(void)
  2514. {
  2515. asihpi_init();
  2516. return pci_register_driver(&driver);
  2517. }
  2518. static void __exit snd_asihpi_exit(void)
  2519. {
  2520. pci_unregister_driver(&driver);
  2521. asihpi_exit();
  2522. }
  2523. module_init(snd_asihpi_init)
  2524. module_exit(snd_asihpi_exit)