mixart.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. /*
  2. * Driver for Digigram miXart soundcards
  3. *
  4. * main file with alsa callbacks
  5. *
  6. * Copyright (c) 2003 by Digigram <alsa@digigram.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <sound/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/pci.h>
  26. #include <linux/moduleparam.h>
  27. #include <sound/core.h>
  28. #include <sound/initval.h>
  29. #include <sound/info.h>
  30. #include <sound/control.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include "mixart.h"
  34. #include "mixart_hwdep.h"
  35. #include "mixart_core.h"
  36. #include "mixart_mixer.h"
  37. #define CARD_NAME "miXart"
  38. MODULE_AUTHOR("Digigram <alsa@digigram.com>");
  39. MODULE_DESCRIPTION("Digigram " CARD_NAME);
  40. MODULE_LICENSE("GPL");
  41. MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");
  42. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  43. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  44. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  45. module_param_array(index, int, NULL, 0444);
  46. MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
  47. module_param_array(id, charp, NULL, 0444);
  48. MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
  49. module_param_array(enable, bool, NULL, 0444);
  50. MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
  51. /*
  52. */
  53. static struct pci_device_id snd_mixart_ids[] = {
  54. { 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
  55. { 0, }
  56. };
  57. MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
  58. static int mixart_set_pipe_state(struct mixart_mgr *mgr,
  59. struct mixart_pipe *pipe, int start)
  60. {
  61. struct mixart_group_state_req group_state;
  62. struct mixart_group_state_resp group_state_resp;
  63. struct mixart_msg request;
  64. int err;
  65. u32 system_msg_uid;
  66. switch(pipe->status) {
  67. case PIPE_RUNNING:
  68. case PIPE_CLOCK_SET:
  69. if(start) return 0; /* already started */
  70. break;
  71. case PIPE_STOPPED:
  72. if(!start) return 0; /* already stopped */
  73. break;
  74. default:
  75. snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
  76. return -EINVAL; /* function called with wrong pipe status */
  77. }
  78. system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
  79. /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
  80. request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
  81. request.uid = (struct mixart_uid){0,0};
  82. request.data = &system_msg_uid;
  83. request.size = sizeof(system_msg_uid);
  84. err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
  85. if(err) {
  86. snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
  87. return err;
  88. }
  89. /* start or stop the pipe (1 pipe) */
  90. memset(&group_state, 0, sizeof(group_state));
  91. group_state.pipe_count = 1;
  92. group_state.pipe_uid[0] = pipe->group_uid;
  93. if(start)
  94. request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
  95. else
  96. request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
  97. request.uid = pipe->group_uid; /*(struct mixart_uid){0,0};*/
  98. request.data = &group_state;
  99. request.size = sizeof(group_state);
  100. err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
  101. if (err < 0 || group_state_resp.txx_status != 0) {
  102. snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
  103. return -EINVAL;
  104. }
  105. if(start) {
  106. u32 stat;
  107. group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
  108. err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
  109. if (err < 0 || group_state_resp.txx_status != 0) {
  110. snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
  111. return -EINVAL;
  112. }
  113. /* in case of start send a synchro top */
  114. request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
  115. request.uid = (struct mixart_uid){0,0};
  116. request.data = NULL;
  117. request.size = 0;
  118. err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
  119. if (err < 0 || stat != 0) {
  120. snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
  121. return -EINVAL;
  122. }
  123. pipe->status = PIPE_RUNNING;
  124. }
  125. else /* !start */
  126. pipe->status = PIPE_STOPPED;
  127. return 0;
  128. }
  129. static int mixart_set_clock(struct mixart_mgr *mgr,
  130. struct mixart_pipe *pipe, unsigned int rate)
  131. {
  132. struct mixart_msg request;
  133. struct mixart_clock_properties clock_properties;
  134. struct mixart_clock_properties_resp clock_prop_resp;
  135. int err;
  136. switch(pipe->status) {
  137. case PIPE_CLOCK_SET:
  138. break;
  139. case PIPE_RUNNING:
  140. if(rate != 0)
  141. break;
  142. default:
  143. if(rate == 0)
  144. return 0; /* nothing to do */
  145. else {
  146. snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
  147. return -EINVAL;
  148. }
  149. }
  150. memset(&clock_properties, 0, sizeof(clock_properties));
  151. clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
  152. clock_properties.clock_mode = CM_STANDALONE;
  153. clock_properties.frequency = rate;
  154. clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
  155. clock_properties.uid_caller[0] = pipe->group_uid;
  156. snd_printdd("mixart_set_clock to %d kHz\n", rate);
  157. request.message_id = MSG_CLOCK_SET_PROPERTIES;
  158. request.uid = mgr->uid_console_manager;
  159. request.data = &clock_properties;
  160. request.size = sizeof(clock_properties);
  161. err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
  162. if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
  163. snd_printk(KERN_ERR "error MSG_CLOCK_SET_PROPERTIES err=%x stat=%x mod=%x !\n", err, clock_prop_resp.status, clock_prop_resp.clock_mode);
  164. return -EINVAL;
  165. }
  166. if(rate) pipe->status = PIPE_CLOCK_SET;
  167. else pipe->status = PIPE_RUNNING;
  168. return 0;
  169. }
  170. /*
  171. * Allocate or reference output pipe for analog IOs (pcmp0/1)
  172. */
  173. struct mixart_pipe *
  174. snd_mixart_add_ref_pipe(struct snd_mixart *chip, int pcm_number, int capture,
  175. int monitoring)
  176. {
  177. int stream_count;
  178. struct mixart_pipe *pipe;
  179. struct mixart_msg request;
  180. if(capture) {
  181. if (pcm_number == MIXART_PCM_ANALOG) {
  182. pipe = &(chip->pipe_in_ana); /* analog inputs */
  183. } else {
  184. pipe = &(chip->pipe_in_dig); /* digital inputs */
  185. }
  186. request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
  187. stream_count = MIXART_CAPTURE_STREAMS;
  188. } else {
  189. if (pcm_number == MIXART_PCM_ANALOG) {
  190. pipe = &(chip->pipe_out_ana); /* analog outputs */
  191. } else {
  192. pipe = &(chip->pipe_out_dig); /* digital outputs */
  193. }
  194. request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
  195. stream_count = MIXART_PLAYBACK_STREAMS;
  196. }
  197. /* a new stream is opened and there are already all streams in use */
  198. if( (monitoring == 0) && (pipe->references >= stream_count) ) {
  199. return NULL;
  200. }
  201. /* pipe is not yet defined */
  202. if( pipe->status == PIPE_UNDEFINED ) {
  203. int err, i;
  204. struct {
  205. struct mixart_streaming_group_req sgroup_req;
  206. struct mixart_streaming_group sgroup_resp;
  207. } *buf;
  208. snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
  209. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  210. if (!buf)
  211. return NULL;
  212. request.uid = (struct mixart_uid){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */
  213. request.data = &buf->sgroup_req;
  214. request.size = sizeof(buf->sgroup_req);
  215. memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
  216. buf->sgroup_req.stream_count = stream_count;
  217. buf->sgroup_req.channel_count = 2;
  218. buf->sgroup_req.latency = 256;
  219. buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */
  220. for (i=0; i<stream_count; i++) {
  221. int j;
  222. struct mixart_flowinfo *flowinfo;
  223. struct mixart_bufferinfo *bufferinfo;
  224. /* we don't yet know the format, so config 16 bit pcm audio for instance */
  225. buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
  226. buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
  227. buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
  228. /* find the right bufferinfo_array */
  229. j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
  230. if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
  231. buf->sgroup_req.flow_entry[i] = j;
  232. flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
  233. flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(struct mixart_bufferinfo));
  234. flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */
  235. bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
  236. bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */
  237. bufferinfo[j].available_length = 0; /* buffer is not yet allocated */
  238. /* construct the identifier of the stream buffer received in the interrupts ! */
  239. bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
  240. if(capture) {
  241. bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
  242. }
  243. }
  244. err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
  245. if((err < 0) || (buf->sgroup_resp.status != 0)) {
  246. snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
  247. kfree(buf);
  248. return NULL;
  249. }
  250. pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */
  251. pipe->stream_count = buf->sgroup_resp.stream_count;
  252. /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
  253. pipe->status = PIPE_STOPPED;
  254. kfree(buf);
  255. }
  256. if(monitoring) pipe->monitoring = 1;
  257. else pipe->references++;
  258. return pipe;
  259. }
  260. int snd_mixart_kill_ref_pipe(struct mixart_mgr *mgr,
  261. struct mixart_pipe *pipe, int monitoring)
  262. {
  263. int err = 0;
  264. if(pipe->status == PIPE_UNDEFINED)
  265. return 0;
  266. if(monitoring)
  267. pipe->monitoring = 0;
  268. else
  269. pipe->references--;
  270. if((pipe->references <= 0) && (pipe->monitoring == 0)) {
  271. struct mixart_msg request;
  272. struct mixart_delete_group_resp delete_resp;
  273. /* release the clock */
  274. err = mixart_set_clock( mgr, pipe, 0);
  275. if( err < 0 ) {
  276. snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
  277. }
  278. /* stop the pipe */
  279. err = mixart_set_pipe_state(mgr, pipe, 0);
  280. if( err < 0 ) {
  281. snd_printk(KERN_ERR "error stopping pipe!\n");
  282. }
  283. request.message_id = MSG_STREAM_DELETE_GROUP;
  284. request.uid = (struct mixart_uid){0,0};
  285. request.data = &pipe->group_uid; /* the streaming group ! */
  286. request.size = sizeof(pipe->group_uid);
  287. /* delete the pipe */
  288. err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
  289. if ((err < 0) || (delete_resp.status != 0)) {
  290. snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
  291. }
  292. pipe->group_uid = (struct mixart_uid){0,0};
  293. pipe->stream_count = 0;
  294. pipe->status = PIPE_UNDEFINED;
  295. }
  296. return err;
  297. }
  298. static int mixart_set_stream_state(struct mixart_stream *stream, int start)
  299. {
  300. struct snd_mixart *chip;
  301. struct mixart_stream_state_req stream_state_req;
  302. struct mixart_msg request;
  303. if(!stream->substream)
  304. return -EINVAL;
  305. memset(&stream_state_req, 0, sizeof(stream_state_req));
  306. stream_state_req.stream_count = 1;
  307. stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
  308. stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
  309. if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  310. request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
  311. else
  312. request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
  313. request.uid = (struct mixart_uid){0,0};
  314. request.data = &stream_state_req;
  315. request.size = sizeof(stream_state_req);
  316. stream->abs_period_elapsed = 0; /* reset stream pos */
  317. stream->buf_periods = 0;
  318. stream->buf_period_frag = 0;
  319. chip = snd_pcm_substream_chip(stream->substream);
  320. return snd_mixart_send_msg_nonblock(chip->mgr, &request);
  321. }
  322. /*
  323. * Trigger callback
  324. */
  325. static int snd_mixart_trigger(struct snd_pcm_substream *subs, int cmd)
  326. {
  327. struct mixart_stream *stream = subs->runtime->private_data;
  328. switch (cmd) {
  329. case SNDRV_PCM_TRIGGER_START:
  330. snd_printdd("SNDRV_PCM_TRIGGER_START\n");
  331. /* START_STREAM */
  332. if( mixart_set_stream_state(stream, 1) )
  333. return -EINVAL;
  334. stream->status = MIXART_STREAM_STATUS_RUNNING;
  335. break;
  336. case SNDRV_PCM_TRIGGER_STOP:
  337. /* STOP_STREAM */
  338. if( mixart_set_stream_state(stream, 0) )
  339. return -EINVAL;
  340. stream->status = MIXART_STREAM_STATUS_OPEN;
  341. snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
  342. break;
  343. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  344. /* TODO */
  345. stream->status = MIXART_STREAM_STATUS_PAUSE;
  346. snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
  347. break;
  348. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  349. /* TODO */
  350. stream->status = MIXART_STREAM_STATUS_RUNNING;
  351. snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
  352. break;
  353. default:
  354. return -EINVAL;
  355. }
  356. return 0;
  357. }
  358. static int mixart_sync_nonblock_events(struct mixart_mgr *mgr)
  359. {
  360. unsigned long timeout = jiffies + HZ;
  361. while (atomic_read(&mgr->msg_processed) > 0) {
  362. if (time_after(jiffies, timeout)) {
  363. snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
  364. return -EBUSY;
  365. }
  366. schedule_timeout_uninterruptible(1);
  367. }
  368. return 0;
  369. }
  370. /*
  371. * prepare callback for all pcms
  372. */
  373. static int snd_mixart_prepare(struct snd_pcm_substream *subs)
  374. {
  375. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  376. struct mixart_stream *stream = subs->runtime->private_data;
  377. /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
  378. snd_printdd("snd_mixart_prepare\n");
  379. mixart_sync_nonblock_events(chip->mgr);
  380. /* only the first stream can choose the sample rate */
  381. /* the further opened streams will be limited to its frequency (see open) */
  382. if(chip->mgr->ref_count_rate == 1)
  383. chip->mgr->sample_rate = subs->runtime->rate;
  384. /* set the clock only once (first stream) on the same pipe */
  385. if(stream->pipe->references == 1) {
  386. if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
  387. return -EINVAL;
  388. }
  389. return 0;
  390. }
  391. static int mixart_set_format(struct mixart_stream *stream, snd_pcm_format_t format)
  392. {
  393. int err;
  394. struct snd_mixart *chip;
  395. struct mixart_msg request;
  396. struct mixart_stream_param_desc stream_param;
  397. struct mixart_return_uid resp;
  398. chip = snd_pcm_substream_chip(stream->substream);
  399. memset(&stream_param, 0, sizeof(stream_param));
  400. stream_param.coding_type = CT_LINEAR;
  401. stream_param.number_of_channel = stream->channels;
  402. stream_param.sampling_freq = chip->mgr->sample_rate;
  403. if(stream_param.sampling_freq == 0)
  404. stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
  405. switch(format){
  406. case SNDRV_PCM_FORMAT_U8:
  407. stream_param.sample_type = ST_INTEGER_8;
  408. stream_param.sample_size = 8;
  409. break;
  410. case SNDRV_PCM_FORMAT_S16_LE:
  411. stream_param.sample_type = ST_INTEGER_16LE;
  412. stream_param.sample_size = 16;
  413. break;
  414. case SNDRV_PCM_FORMAT_S16_BE:
  415. stream_param.sample_type = ST_INTEGER_16BE;
  416. stream_param.sample_size = 16;
  417. break;
  418. case SNDRV_PCM_FORMAT_S24_3LE:
  419. stream_param.sample_type = ST_INTEGER_24LE;
  420. stream_param.sample_size = 24;
  421. break;
  422. case SNDRV_PCM_FORMAT_S24_3BE:
  423. stream_param.sample_type = ST_INTEGER_24BE;
  424. stream_param.sample_size = 24;
  425. break;
  426. case SNDRV_PCM_FORMAT_FLOAT_LE:
  427. stream_param.sample_type = ST_FLOATING_POINT_32LE;
  428. stream_param.sample_size = 32;
  429. break;
  430. case SNDRV_PCM_FORMAT_FLOAT_BE:
  431. stream_param.sample_type = ST_FLOATING_POINT_32BE;
  432. stream_param.sample_size = 32;
  433. break;
  434. default:
  435. snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
  436. return -EINVAL;
  437. }
  438. snd_printdd("set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
  439. stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
  440. /* TODO: what else to configure ? */
  441. /* stream_param.samples_per_frame = 2; */
  442. /* stream_param.bytes_per_frame = 4; */
  443. /* stream_param.bytes_per_sample = 2; */
  444. stream_param.pipe_count = 1; /* set to 1 */
  445. stream_param.stream_count = 1; /* set to 1 */
  446. stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
  447. stream_param.stream_desc[0].stream_idx = stream->substream->number;
  448. request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
  449. request.uid = (struct mixart_uid){0,0};
  450. request.data = &stream_param;
  451. request.size = sizeof(stream_param);
  452. err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
  453. if((err < 0) || resp.error_code) {
  454. snd_printk(KERN_ERR "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", err, resp.error_code);
  455. return -EINVAL;
  456. }
  457. return 0;
  458. }
  459. /*
  460. * HW_PARAMS callback for all pcms
  461. */
  462. static int snd_mixart_hw_params(struct snd_pcm_substream *subs,
  463. struct snd_pcm_hw_params *hw)
  464. {
  465. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  466. struct mixart_mgr *mgr = chip->mgr;
  467. struct mixart_stream *stream = subs->runtime->private_data;
  468. snd_pcm_format_t format;
  469. int err;
  470. int channels;
  471. /* set up channels */
  472. channels = params_channels(hw);
  473. /* set up format for the stream */
  474. format = params_format(hw);
  475. down(&mgr->setup_mutex);
  476. /* update the stream levels */
  477. if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
  478. int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
  479. if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
  480. mixart_update_playback_stream_level(chip, is_aes, subs->number);
  481. else
  482. mixart_update_capture_stream_level( chip, is_aes);
  483. }
  484. stream->channels = channels;
  485. /* set the format to the board */
  486. err = mixart_set_format(stream, format);
  487. if(err < 0) {
  488. return err;
  489. }
  490. /* allocate buffer */
  491. err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
  492. if (err > 0) {
  493. struct mixart_bufferinfo *bufferinfo;
  494. int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number;
  495. if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) {
  496. i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */
  497. }
  498. bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
  499. bufferinfo[i].buffer_address = subs->runtime->dma_addr;
  500. bufferinfo[i].available_length = subs->runtime->dma_bytes;
  501. /* bufferinfo[i].buffer_id is already defined */
  502. snd_printdd("snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", i,
  503. bufferinfo[i].buffer_address,
  504. bufferinfo[i].available_length,
  505. subs->number);
  506. }
  507. up(&mgr->setup_mutex);
  508. return err;
  509. }
  510. static int snd_mixart_hw_free(struct snd_pcm_substream *subs)
  511. {
  512. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  513. snd_pcm_lib_free_pages(subs);
  514. mixart_sync_nonblock_events(chip->mgr);
  515. return 0;
  516. }
  517. /*
  518. * TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
  519. */
  520. static struct snd_pcm_hardware snd_mixart_analog_caps =
  521. {
  522. .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  523. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  524. SNDRV_PCM_INFO_PAUSE),
  525. .formats = ( SNDRV_PCM_FMTBIT_U8 |
  526. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  527. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
  528. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
  529. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  530. .rate_min = 8000,
  531. .rate_max = 48000,
  532. .channels_min = 1,
  533. .channels_max = 2,
  534. .buffer_bytes_max = (32*1024),
  535. .period_bytes_min = 256, /* 256 frames U8 mono*/
  536. .period_bytes_max = (16*1024),
  537. .periods_min = 2,
  538. .periods_max = (32*1024/256),
  539. };
  540. static struct snd_pcm_hardware snd_mixart_digital_caps =
  541. {
  542. .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  543. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  544. SNDRV_PCM_INFO_PAUSE),
  545. .formats = ( SNDRV_PCM_FMTBIT_U8 |
  546. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  547. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
  548. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
  549. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  550. .rate_min = 32000,
  551. .rate_max = 48000,
  552. .channels_min = 1,
  553. .channels_max = 2,
  554. .buffer_bytes_max = (32*1024),
  555. .period_bytes_min = 256, /* 256 frames U8 mono*/
  556. .period_bytes_max = (16*1024),
  557. .periods_min = 2,
  558. .periods_max = (32*1024/256),
  559. };
  560. static int snd_mixart_playback_open(struct snd_pcm_substream *subs)
  561. {
  562. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  563. struct mixart_mgr *mgr = chip->mgr;
  564. struct snd_pcm_runtime *runtime = subs->runtime;
  565. struct snd_pcm *pcm = subs->pcm;
  566. struct mixart_stream *stream;
  567. struct mixart_pipe *pipe;
  568. int err = 0;
  569. int pcm_number;
  570. down(&mgr->setup_mutex);
  571. if ( pcm == chip->pcm ) {
  572. pcm_number = MIXART_PCM_ANALOG;
  573. runtime->hw = snd_mixart_analog_caps;
  574. } else {
  575. snd_assert ( pcm == chip->pcm_dig );
  576. pcm_number = MIXART_PCM_DIGITAL;
  577. runtime->hw = snd_mixart_digital_caps;
  578. }
  579. snd_printdd("snd_mixart_playback_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
  580. /* get stream info */
  581. stream = &(chip->playback_stream[pcm_number][subs->number]);
  582. if (stream->status != MIXART_STREAM_STATUS_FREE){
  583. /* streams in use */
  584. snd_printk(KERN_ERR "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
  585. err = -EBUSY;
  586. goto _exit_open;
  587. }
  588. /* get pipe pointer (out pipe) */
  589. pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
  590. if (pipe == NULL) {
  591. err = -EINVAL;
  592. goto _exit_open;
  593. }
  594. /* start the pipe if necessary */
  595. err = mixart_set_pipe_state(chip->mgr, pipe, 1);
  596. if( err < 0 ) {
  597. snd_printk(KERN_ERR "error starting pipe!\n");
  598. snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
  599. err = -EINVAL;
  600. goto _exit_open;
  601. }
  602. stream->pipe = pipe;
  603. stream->pcm_number = pcm_number;
  604. stream->status = MIXART_STREAM_STATUS_OPEN;
  605. stream->substream = subs;
  606. stream->channels = 0; /* not configured yet */
  607. runtime->private_data = stream;
  608. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  609. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
  610. /* if a sample rate is already used, another stream cannot change */
  611. if(mgr->ref_count_rate++) {
  612. if(mgr->sample_rate) {
  613. runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
  614. }
  615. }
  616. _exit_open:
  617. up(&mgr->setup_mutex);
  618. return err;
  619. }
  620. static int snd_mixart_capture_open(struct snd_pcm_substream *subs)
  621. {
  622. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  623. struct mixart_mgr *mgr = chip->mgr;
  624. struct snd_pcm_runtime *runtime = subs->runtime;
  625. struct snd_pcm *pcm = subs->pcm;
  626. struct mixart_stream *stream;
  627. struct mixart_pipe *pipe;
  628. int err = 0;
  629. int pcm_number;
  630. down(&mgr->setup_mutex);
  631. if ( pcm == chip->pcm ) {
  632. pcm_number = MIXART_PCM_ANALOG;
  633. runtime->hw = snd_mixart_analog_caps;
  634. } else {
  635. snd_assert ( pcm == chip->pcm_dig );
  636. pcm_number = MIXART_PCM_DIGITAL;
  637. runtime->hw = snd_mixart_digital_caps;
  638. }
  639. runtime->hw.channels_min = 2; /* for instance, no mono */
  640. snd_printdd("snd_mixart_capture_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
  641. /* get stream info */
  642. stream = &(chip->capture_stream[pcm_number]);
  643. if (stream->status != MIXART_STREAM_STATUS_FREE){
  644. /* streams in use */
  645. snd_printk(KERN_ERR "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
  646. err = -EBUSY;
  647. goto _exit_open;
  648. }
  649. /* get pipe pointer (in pipe) */
  650. pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
  651. if (pipe == NULL) {
  652. err = -EINVAL;
  653. goto _exit_open;
  654. }
  655. /* start the pipe if necessary */
  656. err = mixart_set_pipe_state(chip->mgr, pipe, 1);
  657. if( err < 0 ) {
  658. snd_printk(KERN_ERR "error starting pipe!\n");
  659. snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
  660. err = -EINVAL;
  661. goto _exit_open;
  662. }
  663. stream->pipe = pipe;
  664. stream->pcm_number = pcm_number;
  665. stream->status = MIXART_STREAM_STATUS_OPEN;
  666. stream->substream = subs;
  667. stream->channels = 0; /* not configured yet */
  668. runtime->private_data = stream;
  669. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  670. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
  671. /* if a sample rate is already used, another stream cannot change */
  672. if(mgr->ref_count_rate++) {
  673. if(mgr->sample_rate) {
  674. runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
  675. }
  676. }
  677. _exit_open:
  678. up(&mgr->setup_mutex);
  679. return err;
  680. }
  681. static int snd_mixart_close(struct snd_pcm_substream *subs)
  682. {
  683. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  684. struct mixart_mgr *mgr = chip->mgr;
  685. struct mixart_stream *stream = subs->runtime->private_data;
  686. down(&mgr->setup_mutex);
  687. snd_printdd("snd_mixart_close C%d/P%d/Sub%d\n", chip->chip_idx, stream->pcm_number, subs->number);
  688. /* sample rate released */
  689. if(--mgr->ref_count_rate == 0) {
  690. mgr->sample_rate = 0;
  691. }
  692. /* delete pipe */
  693. if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
  694. snd_printk(KERN_ERR "error snd_mixart_kill_ref_pipe C%dP%d\n", chip->chip_idx, stream->pcm_number);
  695. }
  696. stream->pipe = NULL;
  697. stream->status = MIXART_STREAM_STATUS_FREE;
  698. stream->substream = NULL;
  699. up(&mgr->setup_mutex);
  700. return 0;
  701. }
  702. static snd_pcm_uframes_t snd_mixart_stream_pointer(struct snd_pcm_substream *subs)
  703. {
  704. struct snd_pcm_runtime *runtime = subs->runtime;
  705. struct mixart_stream *stream = runtime->private_data;
  706. return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
  707. }
  708. static struct snd_pcm_ops snd_mixart_playback_ops = {
  709. .open = snd_mixart_playback_open,
  710. .close = snd_mixart_close,
  711. .ioctl = snd_pcm_lib_ioctl,
  712. .prepare = snd_mixart_prepare,
  713. .hw_params = snd_mixart_hw_params,
  714. .hw_free = snd_mixart_hw_free,
  715. .trigger = snd_mixart_trigger,
  716. .pointer = snd_mixart_stream_pointer,
  717. };
  718. static struct snd_pcm_ops snd_mixart_capture_ops = {
  719. .open = snd_mixart_capture_open,
  720. .close = snd_mixart_close,
  721. .ioctl = snd_pcm_lib_ioctl,
  722. .prepare = snd_mixart_prepare,
  723. .hw_params = snd_mixart_hw_params,
  724. .hw_free = snd_mixart_hw_free,
  725. .trigger = snd_mixart_trigger,
  726. .pointer = snd_mixart_stream_pointer,
  727. };
  728. static void preallocate_buffers(struct snd_mixart *chip, struct snd_pcm *pcm)
  729. {
  730. #if 0
  731. struct snd_pcm_substream *subs;
  732. int stream;
  733. for (stream = 0; stream < 2; stream++) {
  734. int idx = 0;
  735. for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
  736. /* set up the unique device id with the chip index */
  737. subs->dma_device.id = subs->pcm->device << 16 |
  738. subs->stream << 8 | (subs->number + 1) |
  739. (chip->chip_idx + 1) << 24;
  740. }
  741. #endif
  742. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  743. snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
  744. }
  745. /*
  746. */
  747. static int snd_mixart_pcm_analog(struct snd_mixart *chip)
  748. {
  749. int err;
  750. struct snd_pcm *pcm;
  751. char name[32];
  752. sprintf(name, "miXart analog %d", chip->chip_idx);
  753. if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
  754. MIXART_PLAYBACK_STREAMS,
  755. MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
  756. snd_printk(KERN_ERR "cannot create the analog pcm %d\n", chip->chip_idx);
  757. return err;
  758. }
  759. pcm->private_data = chip;
  760. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
  761. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
  762. pcm->info_flags = 0;
  763. strcpy(pcm->name, name);
  764. preallocate_buffers(chip, pcm);
  765. chip->pcm = pcm;
  766. return 0;
  767. }
  768. /*
  769. */
  770. static int snd_mixart_pcm_digital(struct snd_mixart *chip)
  771. {
  772. int err;
  773. struct snd_pcm *pcm;
  774. char name[32];
  775. sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
  776. if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
  777. MIXART_PLAYBACK_STREAMS,
  778. MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
  779. snd_printk(KERN_ERR "cannot create the digital pcm %d\n", chip->chip_idx);
  780. return err;
  781. }
  782. pcm->private_data = chip;
  783. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
  784. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
  785. pcm->info_flags = 0;
  786. strcpy(pcm->name, name);
  787. preallocate_buffers(chip, pcm);
  788. chip->pcm_dig = pcm;
  789. return 0;
  790. }
  791. static int snd_mixart_chip_free(struct snd_mixart *chip)
  792. {
  793. kfree(chip);
  794. return 0;
  795. }
  796. static int snd_mixart_chip_dev_free(struct snd_device *device)
  797. {
  798. struct snd_mixart *chip = device->device_data;
  799. return snd_mixart_chip_free(chip);
  800. }
  801. /*
  802. */
  803. static int __devinit snd_mixart_create(struct mixart_mgr *mgr, struct snd_card *card, int idx)
  804. {
  805. int err;
  806. struct snd_mixart *chip;
  807. static struct snd_device_ops ops = {
  808. .dev_free = snd_mixart_chip_dev_free,
  809. };
  810. mgr->chip[idx] = chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  811. if (! chip) {
  812. snd_printk(KERN_ERR "cannot allocate chip\n");
  813. return -ENOMEM;
  814. }
  815. chip->card = card;
  816. chip->chip_idx = idx;
  817. chip->mgr = mgr;
  818. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  819. snd_mixart_chip_free(chip);
  820. return err;
  821. }
  822. snd_card_set_dev(card, &mgr->pci->dev);
  823. return 0;
  824. }
  825. int snd_mixart_create_pcm(struct snd_mixart* chip)
  826. {
  827. int err;
  828. err = snd_mixart_pcm_analog(chip);
  829. if (err < 0)
  830. return err;
  831. if(chip->mgr->board_type == MIXART_DAUGHTER_TYPE_AES) {
  832. err = snd_mixart_pcm_digital(chip);
  833. if (err < 0)
  834. return err;
  835. }
  836. return err;
  837. }
  838. /*
  839. * release all the cards assigned to a manager instance
  840. */
  841. static int snd_mixart_free(struct mixart_mgr *mgr)
  842. {
  843. unsigned int i;
  844. for (i = 0; i < mgr->num_cards; i++) {
  845. if (mgr->chip[i])
  846. snd_card_free(mgr->chip[i]->card);
  847. }
  848. /* stop mailbox */
  849. snd_mixart_exit_mailbox(mgr);
  850. /* release irq */
  851. if (mgr->irq >= 0)
  852. free_irq(mgr->irq, (void *)mgr);
  853. /* reset board if some firmware was loaded */
  854. if(mgr->dsp_loaded) {
  855. snd_mixart_reset_board(mgr);
  856. snd_printdd("reset miXart !\n");
  857. }
  858. /* release the i/o ports */
  859. for (i = 0; i < 2; i++) {
  860. if (mgr->mem[i].virt)
  861. iounmap(mgr->mem[i].virt);
  862. }
  863. pci_release_regions(mgr->pci);
  864. /* free flowarray */
  865. if(mgr->flowinfo.area) {
  866. snd_dma_free_pages(&mgr->flowinfo);
  867. mgr->flowinfo.area = NULL;
  868. }
  869. /* free bufferarray */
  870. if(mgr->bufferinfo.area) {
  871. snd_dma_free_pages(&mgr->bufferinfo);
  872. mgr->bufferinfo.area = NULL;
  873. }
  874. pci_disable_device(mgr->pci);
  875. kfree(mgr);
  876. return 0;
  877. }
  878. /*
  879. * proc interface
  880. */
  881. static long long snd_mixart_BA0_llseek(struct snd_info_entry *entry,
  882. void *private_file_data,
  883. struct file *file,
  884. long long offset,
  885. int orig)
  886. {
  887. offset = offset & ~3; /* 4 bytes aligned */
  888. switch(orig) {
  889. case 0: /* SEEK_SET */
  890. file->f_pos = offset;
  891. break;
  892. case 1: /* SEEK_CUR */
  893. file->f_pos += offset;
  894. break;
  895. case 2: /* SEEK_END, offset is negative */
  896. file->f_pos = MIXART_BA0_SIZE + offset;
  897. break;
  898. default:
  899. return -EINVAL;
  900. }
  901. if(file->f_pos > MIXART_BA0_SIZE)
  902. file->f_pos = MIXART_BA0_SIZE;
  903. return file->f_pos;
  904. }
  905. static long long snd_mixart_BA1_llseek(struct snd_info_entry *entry,
  906. void *private_file_data,
  907. struct file *file,
  908. long long offset,
  909. int orig)
  910. {
  911. offset = offset & ~3; /* 4 bytes aligned */
  912. switch(orig) {
  913. case 0: /* SEEK_SET */
  914. file->f_pos = offset;
  915. break;
  916. case 1: /* SEEK_CUR */
  917. file->f_pos += offset;
  918. break;
  919. case 2: /* SEEK_END, offset is negative */
  920. file->f_pos = MIXART_BA1_SIZE + offset;
  921. break;
  922. default:
  923. return -EINVAL;
  924. }
  925. if(file->f_pos > MIXART_BA1_SIZE)
  926. file->f_pos = MIXART_BA1_SIZE;
  927. return file->f_pos;
  928. }
  929. /*
  930. mixart_BA0 proc interface for BAR 0 - read callback
  931. */
  932. static long snd_mixart_BA0_read(struct snd_info_entry *entry, void *file_private_data,
  933. struct file *file, char __user *buf,
  934. unsigned long count, unsigned long pos)
  935. {
  936. struct mixart_mgr *mgr = entry->private_data;
  937. count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
  938. if(count <= 0)
  939. return 0;
  940. if(pos + count > MIXART_BA0_SIZE)
  941. count = (long)(MIXART_BA0_SIZE - pos);
  942. if(copy_to_user_fromio(buf, MIXART_MEM( mgr, pos ), count))
  943. return -EFAULT;
  944. return count;
  945. }
  946. /*
  947. mixart_BA1 proc interface for BAR 1 - read callback
  948. */
  949. static long snd_mixart_BA1_read(struct snd_info_entry *entry, void *file_private_data,
  950. struct file *file, char __user *buf,
  951. unsigned long count, unsigned long pos)
  952. {
  953. struct mixart_mgr *mgr = entry->private_data;
  954. count = count & ~3; /* make sure the read size is a multiple of 4 bytes */
  955. if(count <= 0)
  956. return 0;
  957. if(pos + count > MIXART_BA1_SIZE)
  958. count = (long)(MIXART_BA1_SIZE - pos);
  959. if(copy_to_user_fromio(buf, MIXART_REG( mgr, pos ), count))
  960. return -EFAULT;
  961. return count;
  962. }
  963. static struct snd_info_entry_ops snd_mixart_proc_ops_BA0 = {
  964. .read = snd_mixart_BA0_read,
  965. .llseek = snd_mixart_BA0_llseek
  966. };
  967. static struct snd_info_entry_ops snd_mixart_proc_ops_BA1 = {
  968. .read = snd_mixart_BA1_read,
  969. .llseek = snd_mixart_BA1_llseek
  970. };
  971. static void snd_mixart_proc_read(struct snd_info_entry *entry,
  972. struct snd_info_buffer *buffer)
  973. {
  974. struct snd_mixart *chip = entry->private_data;
  975. u32 ref;
  976. snd_iprintf(buffer, "Digigram miXart (alsa card %d)\n\n", chip->chip_idx);
  977. /* stats available when embedded OS is running */
  978. if (chip->mgr->dsp_loaded & ( 1 << MIXART_MOTHERBOARD_ELF_INDEX)) {
  979. snd_iprintf(buffer, "- hardware -\n");
  980. switch (chip->mgr->board_type ) {
  981. case MIXART_DAUGHTER_TYPE_NONE : snd_iprintf(buffer, "\tmiXart8 (no daughter board)\n\n"); break;
  982. case MIXART_DAUGHTER_TYPE_AES : snd_iprintf(buffer, "\tmiXart8 AES/EBU\n\n"); break;
  983. case MIXART_DAUGHTER_TYPE_COBRANET : snd_iprintf(buffer, "\tmiXart8 Cobranet\n\n"); break;
  984. default: snd_iprintf(buffer, "\tUNKNOWN!\n\n"); break;
  985. }
  986. snd_iprintf(buffer, "- system load -\n");
  987. /* get perf reference */
  988. ref = readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_SYSTEM_LOAD_OFFSET));
  989. if (ref) {
  990. u32 mailbox = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_MAILBX_LOAD_OFFSET)) / ref;
  991. u32 streaming = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_STREAM_LOAD_OFFSET)) / ref;
  992. u32 interr = 100 * readl_be( MIXART_MEM( chip->mgr, MIXART_PSEUDOREG_PERF_INTERR_LOAD_OFFSET)) / ref;
  993. snd_iprintf(buffer, "\tstreaming : %d\n", streaming);
  994. snd_iprintf(buffer, "\tmailbox : %d\n", mailbox);
  995. snd_iprintf(buffer, "\tinterrups handling : %d\n\n", interr);
  996. }
  997. } /* endif elf loaded */
  998. }
  999. static void __devinit snd_mixart_proc_init(struct snd_mixart *chip)
  1000. {
  1001. struct snd_info_entry *entry;
  1002. /* text interface to read perf and temp meters */
  1003. if (! snd_card_proc_new(chip->card, "board_info", &entry)) {
  1004. entry->private_data = chip;
  1005. entry->c.text.read_size = 1024;
  1006. entry->c.text.read = snd_mixart_proc_read;
  1007. }
  1008. if (! snd_card_proc_new(chip->card, "mixart_BA0", &entry)) {
  1009. entry->content = SNDRV_INFO_CONTENT_DATA;
  1010. entry->private_data = chip->mgr;
  1011. entry->c.ops = &snd_mixart_proc_ops_BA0;
  1012. entry->size = MIXART_BA0_SIZE;
  1013. }
  1014. if (! snd_card_proc_new(chip->card, "mixart_BA1", &entry)) {
  1015. entry->content = SNDRV_INFO_CONTENT_DATA;
  1016. entry->private_data = chip->mgr;
  1017. entry->c.ops = &snd_mixart_proc_ops_BA1;
  1018. entry->size = MIXART_BA1_SIZE;
  1019. }
  1020. }
  1021. /* end of proc interface */
  1022. /*
  1023. * probe function - creates the card manager
  1024. */
  1025. static int __devinit snd_mixart_probe(struct pci_dev *pci,
  1026. const struct pci_device_id *pci_id)
  1027. {
  1028. static int dev;
  1029. struct mixart_mgr *mgr;
  1030. unsigned int i;
  1031. int err;
  1032. size_t size;
  1033. /*
  1034. */
  1035. if (dev >= SNDRV_CARDS)
  1036. return -ENODEV;
  1037. if (! enable[dev]) {
  1038. dev++;
  1039. return -ENOENT;
  1040. }
  1041. /* enable PCI device */
  1042. if ((err = pci_enable_device(pci)) < 0)
  1043. return err;
  1044. pci_set_master(pci);
  1045. /* check if we can restrict PCI DMA transfers to 32 bits */
  1046. if (pci_set_dma_mask(pci, 0xffffffff) < 0) {
  1047. snd_printk(KERN_ERR "architecture does not support 32bit PCI busmaster DMA\n");
  1048. pci_disable_device(pci);
  1049. return -ENXIO;
  1050. }
  1051. /*
  1052. */
  1053. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  1054. if (! mgr) {
  1055. pci_disable_device(pci);
  1056. return -ENOMEM;
  1057. }
  1058. mgr->pci = pci;
  1059. mgr->irq = -1;
  1060. /* resource assignment */
  1061. if ((err = pci_request_regions(pci, CARD_NAME)) < 0) {
  1062. kfree(mgr);
  1063. pci_disable_device(pci);
  1064. return err;
  1065. }
  1066. for (i = 0; i < 2; i++) {
  1067. mgr->mem[i].phys = pci_resource_start(pci, i);
  1068. mgr->mem[i].virt = ioremap_nocache(mgr->mem[i].phys,
  1069. pci_resource_len(pci, i));
  1070. }
  1071. if (request_irq(pci->irq, snd_mixart_interrupt, SA_INTERRUPT|SA_SHIRQ, CARD_NAME, (void *)mgr)) {
  1072. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1073. snd_mixart_free(mgr);
  1074. return -EBUSY;
  1075. }
  1076. mgr->irq = pci->irq;
  1077. sprintf(mgr->shortname, "Digigram miXart");
  1078. sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
  1079. /* ISR spinlock */
  1080. spin_lock_init(&mgr->lock);
  1081. /* init mailbox */
  1082. mgr->msg_fifo_readptr = 0;
  1083. mgr->msg_fifo_writeptr = 0;
  1084. spin_lock_init(&mgr->msg_lock);
  1085. init_MUTEX(&mgr->msg_mutex);
  1086. init_waitqueue_head(&mgr->msg_sleep);
  1087. atomic_set(&mgr->msg_processed, 0);
  1088. /* init setup mutex*/
  1089. init_MUTEX(&mgr->setup_mutex);
  1090. /* init message taslket */
  1091. tasklet_init(&mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
  1092. /* card assignment */
  1093. mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */
  1094. for (i = 0; i < mgr->num_cards; i++) {
  1095. struct snd_card *card;
  1096. char tmpid[16];
  1097. int idx;
  1098. if (index[dev] < 0)
  1099. idx = index[dev];
  1100. else
  1101. idx = index[dev] + i;
  1102. snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
  1103. card = snd_card_new(idx, tmpid, THIS_MODULE, 0);
  1104. if (! card) {
  1105. snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
  1106. snd_mixart_free(mgr);
  1107. return -ENOMEM;
  1108. }
  1109. strcpy(card->driver, CARD_NAME);
  1110. sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
  1111. sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
  1112. if ((err = snd_mixart_create(mgr, card, i)) < 0) {
  1113. snd_mixart_free(mgr);
  1114. return err;
  1115. }
  1116. if(i==0) {
  1117. /* init proc interface only for chip0 */
  1118. snd_mixart_proc_init(mgr->chip[i]);
  1119. }
  1120. if ((err = snd_card_register(card)) < 0) {
  1121. snd_mixart_free(mgr);
  1122. return err;
  1123. }
  1124. }
  1125. /* init firmware status (mgr->dsp_loaded reset in hwdep_new) */
  1126. mgr->board_type = MIXART_DAUGHTER_TYPE_NONE;
  1127. /* create array of streaminfo */
  1128. size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
  1129. sizeof(struct mixart_flowinfo)) );
  1130. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1131. size, &mgr->flowinfo) < 0) {
  1132. snd_mixart_free(mgr);
  1133. return -ENOMEM;
  1134. }
  1135. /* init streaminfo_array */
  1136. memset(mgr->flowinfo.area, 0, size);
  1137. /* create array of bufferinfo */
  1138. size = PAGE_ALIGN( (MIXART_MAX_STREAM_PER_CARD * MIXART_MAX_CARDS *
  1139. sizeof(struct mixart_bufferinfo)) );
  1140. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1141. size, &mgr->bufferinfo) < 0) {
  1142. snd_mixart_free(mgr);
  1143. return -ENOMEM;
  1144. }
  1145. /* init bufferinfo_array */
  1146. memset(mgr->bufferinfo.area, 0, size);
  1147. /* set up firmware */
  1148. err = snd_mixart_setup_firmware(mgr);
  1149. if (err < 0) {
  1150. snd_mixart_free(mgr);
  1151. return err;
  1152. }
  1153. pci_set_drvdata(pci, mgr);
  1154. dev++;
  1155. return 0;
  1156. }
  1157. static void __devexit snd_mixart_remove(struct pci_dev *pci)
  1158. {
  1159. snd_mixart_free(pci_get_drvdata(pci));
  1160. pci_set_drvdata(pci, NULL);
  1161. }
  1162. static struct pci_driver driver = {
  1163. .name = "Digigram miXart",
  1164. .id_table = snd_mixart_ids,
  1165. .probe = snd_mixart_probe,
  1166. .remove = __devexit_p(snd_mixart_remove),
  1167. };
  1168. static int __init alsa_card_mixart_init(void)
  1169. {
  1170. return pci_register_driver(&driver);
  1171. }
  1172. static void __exit alsa_card_mixart_exit(void)
  1173. {
  1174. pci_unregister_driver(&driver);
  1175. }
  1176. module_init(alsa_card_mixart_init)
  1177. module_exit(alsa_card_mixart_exit)