mixart.c 40 KB

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