asihpi.c 80 KB

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