asihpi.c 79 KB

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