asihpi.c 80 KB

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