asihpi.c 80 KB

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