asihpi.c 79 KB

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