asihpi.c 79 KB

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