asihpi.c 80 KB

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