asihpi.c 81 KB

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