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