asihpi.c 83 KB

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