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 <sound/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/pci.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/mutex.h>
  29. #include <sound/core.h>
  30. #include <sound/initval.h>
  31. #include <sound/info.h>
  32. #include <sound/control.h>
  33. #include <sound/pcm.h>
  34. #include <sound/pcm_params.h>
  35. #include "mixart.h"
  36. #include "mixart_hwdep.h"
  37. #include "mixart_core.h"
  38. #include "mixart_mixer.h"
  39. #define CARD_NAME "miXart"
  40. MODULE_AUTHOR("Digigram <alsa@digigram.com>");
  41. MODULE_DESCRIPTION("Digigram " CARD_NAME);
  42. MODULE_LICENSE("GPL");
  43. MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");
  44. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  45. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  46. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  47. module_param_array(index, int, NULL, 0444);
  48. MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");
  49. module_param_array(id, charp, NULL, 0444);
  50. MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");
  51. module_param_array(enable, bool, NULL, 0444);
  52. MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");
  53. /*
  54. */
  55. static struct pci_device_id snd_mixart_ids[] = {
  56. { 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */
  57. { 0, }
  58. };
  59. MODULE_DEVICE_TABLE(pci, snd_mixart_ids);
  60. static int mixart_set_pipe_state(struct mixart_mgr *mgr,
  61. struct mixart_pipe *pipe, int start)
  62. {
  63. struct mixart_group_state_req group_state;
  64. struct mixart_group_state_resp group_state_resp;
  65. struct mixart_msg request;
  66. int err;
  67. u32 system_msg_uid;
  68. switch(pipe->status) {
  69. case PIPE_RUNNING:
  70. case PIPE_CLOCK_SET:
  71. if(start) return 0; /* already started */
  72. break;
  73. case PIPE_STOPPED:
  74. if(!start) return 0; /* already stopped */
  75. break;
  76. default:
  77. snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");
  78. return -EINVAL; /* function called with wrong pipe status */
  79. }
  80. system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */
  81. /* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */
  82. request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;
  83. request.uid = (struct mixart_uid){0,0};
  84. request.data = &system_msg_uid;
  85. request.size = sizeof(system_msg_uid);
  86. err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);
  87. if(err) {
  88. snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");
  89. return err;
  90. }
  91. /* start or stop the pipe (1 pipe) */
  92. memset(&group_state, 0, sizeof(group_state));
  93. group_state.pipe_count = 1;
  94. group_state.pipe_uid[0] = pipe->group_uid;
  95. if(start)
  96. request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;
  97. else
  98. request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;
  99. request.uid = pipe->group_uid; /*(struct mixart_uid){0,0};*/
  100. request.data = &group_state;
  101. request.size = sizeof(group_state);
  102. err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
  103. if (err < 0 || group_state_resp.txx_status != 0) {
  104. snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
  105. return -EINVAL;
  106. }
  107. if(start) {
  108. u32 stat;
  109. group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */
  110. err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);
  111. if (err < 0 || group_state_resp.txx_status != 0) {
  112. snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);
  113. return -EINVAL;
  114. }
  115. /* in case of start send a synchro top */
  116. request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;
  117. request.uid = (struct mixart_uid){0,0};
  118. request.data = NULL;
  119. request.size = 0;
  120. err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);
  121. if (err < 0 || stat != 0) {
  122. snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);
  123. return -EINVAL;
  124. }
  125. pipe->status = PIPE_RUNNING;
  126. }
  127. else /* !start */
  128. pipe->status = PIPE_STOPPED;
  129. return 0;
  130. }
  131. static int mixart_set_clock(struct mixart_mgr *mgr,
  132. struct mixart_pipe *pipe, unsigned int rate)
  133. {
  134. struct mixart_msg request;
  135. struct mixart_clock_properties clock_properties;
  136. struct mixart_clock_properties_resp clock_prop_resp;
  137. int err;
  138. switch(pipe->status) {
  139. case PIPE_CLOCK_SET:
  140. break;
  141. case PIPE_RUNNING:
  142. if(rate != 0)
  143. break;
  144. default:
  145. if(rate == 0)
  146. return 0; /* nothing to do */
  147. else {
  148. snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);
  149. return -EINVAL;
  150. }
  151. }
  152. memset(&clock_properties, 0, sizeof(clock_properties));
  153. clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;
  154. clock_properties.clock_mode = CM_STANDALONE;
  155. clock_properties.frequency = rate;
  156. clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */
  157. clock_properties.uid_caller[0] = pipe->group_uid;
  158. snd_printdd("mixart_set_clock to %d kHz\n", rate);
  159. request.message_id = MSG_CLOCK_SET_PROPERTIES;
  160. request.uid = mgr->uid_console_manager;
  161. request.data = &clock_properties;
  162. request.size = sizeof(clock_properties);
  163. err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);
  164. if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {
  165. 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);
  166. return -EINVAL;
  167. }
  168. if(rate) pipe->status = PIPE_CLOCK_SET;
  169. else pipe->status = PIPE_RUNNING;
  170. return 0;
  171. }
  172. /*
  173. * Allocate or reference output pipe for analog IOs (pcmp0/1)
  174. */
  175. struct mixart_pipe *
  176. snd_mixart_add_ref_pipe(struct snd_mixart *chip, int pcm_number, int capture,
  177. int monitoring)
  178. {
  179. int stream_count;
  180. struct mixart_pipe *pipe;
  181. struct mixart_msg request;
  182. if(capture) {
  183. if (pcm_number == MIXART_PCM_ANALOG) {
  184. pipe = &(chip->pipe_in_ana); /* analog inputs */
  185. } else {
  186. pipe = &(chip->pipe_in_dig); /* digital inputs */
  187. }
  188. request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;
  189. stream_count = MIXART_CAPTURE_STREAMS;
  190. } else {
  191. if (pcm_number == MIXART_PCM_ANALOG) {
  192. pipe = &(chip->pipe_out_ana); /* analog outputs */
  193. } else {
  194. pipe = &(chip->pipe_out_dig); /* digital outputs */
  195. }
  196. request.message_id = MSG_STREAM_ADD_INPUT_GROUP;
  197. stream_count = MIXART_PLAYBACK_STREAMS;
  198. }
  199. /* a new stream is opened and there are already all streams in use */
  200. if( (monitoring == 0) && (pipe->references >= stream_count) ) {
  201. return NULL;
  202. }
  203. /* pipe is not yet defined */
  204. if( pipe->status == PIPE_UNDEFINED ) {
  205. int err, i;
  206. struct {
  207. struct mixart_streaming_group_req sgroup_req;
  208. struct mixart_streaming_group sgroup_resp;
  209. } *buf;
  210. snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);
  211. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  212. if (!buf)
  213. return NULL;
  214. request.uid = (struct mixart_uid){0,0}; /* should be StreamManagerUID, but zero is OK if there is only one ! */
  215. request.data = &buf->sgroup_req;
  216. request.size = sizeof(buf->sgroup_req);
  217. memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));
  218. buf->sgroup_req.stream_count = stream_count;
  219. buf->sgroup_req.channel_count = 2;
  220. buf->sgroup_req.latency = 256;
  221. buf->sgroup_req.connector = pipe->uid_left_connector; /* the left connector */
  222. for (i=0; i<stream_count; i++) {
  223. int j;
  224. struct mixart_flowinfo *flowinfo;
  225. struct mixart_bufferinfo *bufferinfo;
  226. /* we don't yet know the format, so config 16 bit pcm audio for instance */
  227. buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;
  228. buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;
  229. buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */
  230. /* find the right bufferinfo_array */
  231. j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;
  232. if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */
  233. buf->sgroup_req.flow_entry[i] = j;
  234. flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;
  235. flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(struct mixart_bufferinfo));
  236. flowinfo[j].bufferinfo_count = 1; /* 1 will set the miXart to ring-buffer mode ! */
  237. bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
  238. bufferinfo[j].buffer_address = 0; /* buffer is not yet allocated */
  239. bufferinfo[j].available_length = 0; /* buffer is not yet allocated */
  240. /* construct the identifier of the stream buffer received in the interrupts ! */
  241. bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;
  242. if(capture) {
  243. bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;
  244. }
  245. }
  246. err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);
  247. if((err < 0) || (buf->sgroup_resp.status != 0)) {
  248. snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);
  249. kfree(buf);
  250. return NULL;
  251. }
  252. pipe->group_uid = buf->sgroup_resp.group; /* id of the pipe, as returned by embedded */
  253. pipe->stream_count = buf->sgroup_resp.stream_count;
  254. /* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */
  255. pipe->status = PIPE_STOPPED;
  256. kfree(buf);
  257. }
  258. if(monitoring) pipe->monitoring = 1;
  259. else pipe->references++;
  260. return pipe;
  261. }
  262. int snd_mixart_kill_ref_pipe(struct mixart_mgr *mgr,
  263. struct mixart_pipe *pipe, int monitoring)
  264. {
  265. int err = 0;
  266. if(pipe->status == PIPE_UNDEFINED)
  267. return 0;
  268. if(monitoring)
  269. pipe->monitoring = 0;
  270. else
  271. pipe->references--;
  272. if((pipe->references <= 0) && (pipe->monitoring == 0)) {
  273. struct mixart_msg request;
  274. struct mixart_delete_group_resp delete_resp;
  275. /* release the clock */
  276. err = mixart_set_clock( mgr, pipe, 0);
  277. if( err < 0 ) {
  278. snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");
  279. }
  280. /* stop the pipe */
  281. err = mixart_set_pipe_state(mgr, pipe, 0);
  282. if( err < 0 ) {
  283. snd_printk(KERN_ERR "error stopping pipe!\n");
  284. }
  285. request.message_id = MSG_STREAM_DELETE_GROUP;
  286. request.uid = (struct mixart_uid){0,0};
  287. request.data = &pipe->group_uid; /* the streaming group ! */
  288. request.size = sizeof(pipe->group_uid);
  289. /* delete the pipe */
  290. err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);
  291. if ((err < 0) || (delete_resp.status != 0)) {
  292. snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);
  293. }
  294. pipe->group_uid = (struct mixart_uid){0,0};
  295. pipe->stream_count = 0;
  296. pipe->status = PIPE_UNDEFINED;
  297. }
  298. return err;
  299. }
  300. static int mixart_set_stream_state(struct mixart_stream *stream, int start)
  301. {
  302. struct snd_mixart *chip;
  303. struct mixart_stream_state_req stream_state_req;
  304. struct mixart_msg request;
  305. if(!stream->substream)
  306. return -EINVAL;
  307. memset(&stream_state_req, 0, sizeof(stream_state_req));
  308. stream_state_req.stream_count = 1;
  309. stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;
  310. stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;
  311. if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  312. request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;
  313. else
  314. request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;
  315. request.uid = (struct mixart_uid){0,0};
  316. request.data = &stream_state_req;
  317. request.size = sizeof(stream_state_req);
  318. stream->abs_period_elapsed = 0; /* reset stream pos */
  319. stream->buf_periods = 0;
  320. stream->buf_period_frag = 0;
  321. chip = snd_pcm_substream_chip(stream->substream);
  322. return snd_mixart_send_msg_nonblock(chip->mgr, &request);
  323. }
  324. /*
  325. * Trigger callback
  326. */
  327. static int snd_mixart_trigger(struct snd_pcm_substream *subs, int cmd)
  328. {
  329. struct mixart_stream *stream = subs->runtime->private_data;
  330. switch (cmd) {
  331. case SNDRV_PCM_TRIGGER_START:
  332. snd_printdd("SNDRV_PCM_TRIGGER_START\n");
  333. /* START_STREAM */
  334. if( mixart_set_stream_state(stream, 1) )
  335. return -EINVAL;
  336. stream->status = MIXART_STREAM_STATUS_RUNNING;
  337. break;
  338. case SNDRV_PCM_TRIGGER_STOP:
  339. /* STOP_STREAM */
  340. if( mixart_set_stream_state(stream, 0) )
  341. return -EINVAL;
  342. stream->status = MIXART_STREAM_STATUS_OPEN;
  343. snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");
  344. break;
  345. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  346. /* TODO */
  347. stream->status = MIXART_STREAM_STATUS_PAUSE;
  348. snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");
  349. break;
  350. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  351. /* TODO */
  352. stream->status = MIXART_STREAM_STATUS_RUNNING;
  353. snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");
  354. break;
  355. default:
  356. return -EINVAL;
  357. }
  358. return 0;
  359. }
  360. static int mixart_sync_nonblock_events(struct mixart_mgr *mgr)
  361. {
  362. unsigned long timeout = jiffies + HZ;
  363. while (atomic_read(&mgr->msg_processed) > 0) {
  364. if (time_after(jiffies, timeout)) {
  365. snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");
  366. return -EBUSY;
  367. }
  368. schedule_timeout_uninterruptible(1);
  369. }
  370. return 0;
  371. }
  372. /*
  373. * prepare callback for all pcms
  374. */
  375. static int snd_mixart_prepare(struct snd_pcm_substream *subs)
  376. {
  377. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  378. struct mixart_stream *stream = subs->runtime->private_data;
  379. /* TODO de façon non bloquante, réappliquer les hw_params (rate, bits, codec) */
  380. snd_printdd("snd_mixart_prepare\n");
  381. mixart_sync_nonblock_events(chip->mgr);
  382. /* only the first stream can choose the sample rate */
  383. /* the further opened streams will be limited to its frequency (see open) */
  384. if(chip->mgr->ref_count_rate == 1)
  385. chip->mgr->sample_rate = subs->runtime->rate;
  386. /* set the clock only once (first stream) on the same pipe */
  387. if(stream->pipe->references == 1) {
  388. if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )
  389. return -EINVAL;
  390. }
  391. return 0;
  392. }
  393. static int mixart_set_format(struct mixart_stream *stream, snd_pcm_format_t format)
  394. {
  395. int err;
  396. struct snd_mixart *chip;
  397. struct mixart_msg request;
  398. struct mixart_stream_param_desc stream_param;
  399. struct mixart_return_uid resp;
  400. chip = snd_pcm_substream_chip(stream->substream);
  401. memset(&stream_param, 0, sizeof(stream_param));
  402. stream_param.coding_type = CT_LINEAR;
  403. stream_param.number_of_channel = stream->channels;
  404. stream_param.sampling_freq = chip->mgr->sample_rate;
  405. if(stream_param.sampling_freq == 0)
  406. stream_param.sampling_freq = 44100; /* if frequency not yet defined, use some default */
  407. switch(format){
  408. case SNDRV_PCM_FORMAT_U8:
  409. stream_param.sample_type = ST_INTEGER_8;
  410. stream_param.sample_size = 8;
  411. break;
  412. case SNDRV_PCM_FORMAT_S16_LE:
  413. stream_param.sample_type = ST_INTEGER_16LE;
  414. stream_param.sample_size = 16;
  415. break;
  416. case SNDRV_PCM_FORMAT_S16_BE:
  417. stream_param.sample_type = ST_INTEGER_16BE;
  418. stream_param.sample_size = 16;
  419. break;
  420. case SNDRV_PCM_FORMAT_S24_3LE:
  421. stream_param.sample_type = ST_INTEGER_24LE;
  422. stream_param.sample_size = 24;
  423. break;
  424. case SNDRV_PCM_FORMAT_S24_3BE:
  425. stream_param.sample_type = ST_INTEGER_24BE;
  426. stream_param.sample_size = 24;
  427. break;
  428. case SNDRV_PCM_FORMAT_FLOAT_LE:
  429. stream_param.sample_type = ST_FLOATING_POINT_32LE;
  430. stream_param.sample_size = 32;
  431. break;
  432. case SNDRV_PCM_FORMAT_FLOAT_BE:
  433. stream_param.sample_type = ST_FLOATING_POINT_32BE;
  434. stream_param.sample_size = 32;
  435. break;
  436. default:
  437. snd_printk(KERN_ERR "error mixart_set_format() : unknown format\n");
  438. return -EINVAL;
  439. }
  440. snd_printdd("set SNDRV_PCM_FORMAT sample_type(%d) sample_size(%d) freq(%d) channels(%d)\n",
  441. stream_param.sample_type, stream_param.sample_size, stream_param.sampling_freq, stream->channels);
  442. /* TODO: what else to configure ? */
  443. /* stream_param.samples_per_frame = 2; */
  444. /* stream_param.bytes_per_frame = 4; */
  445. /* stream_param.bytes_per_sample = 2; */
  446. stream_param.pipe_count = 1; /* set to 1 */
  447. stream_param.stream_count = 1; /* set to 1 */
  448. stream_param.stream_desc[0].uid_pipe = stream->pipe->group_uid;
  449. stream_param.stream_desc[0].stream_idx = stream->substream->number;
  450. request.message_id = MSG_STREAM_SET_INPUT_STAGE_PARAM;
  451. request.uid = (struct mixart_uid){0,0};
  452. request.data = &stream_param;
  453. request.size = sizeof(stream_param);
  454. err = snd_mixart_send_msg(chip->mgr, &request, sizeof(resp), &resp);
  455. if((err < 0) || resp.error_code) {
  456. snd_printk(KERN_ERR "MSG_STREAM_SET_INPUT_STAGE_PARAM err=%x; resp=%x\n", err, resp.error_code);
  457. return -EINVAL;
  458. }
  459. return 0;
  460. }
  461. /*
  462. * HW_PARAMS callback for all pcms
  463. */
  464. static int snd_mixart_hw_params(struct snd_pcm_substream *subs,
  465. struct snd_pcm_hw_params *hw)
  466. {
  467. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  468. struct mixart_mgr *mgr = chip->mgr;
  469. struct mixart_stream *stream = subs->runtime->private_data;
  470. snd_pcm_format_t format;
  471. int err;
  472. int channels;
  473. /* set up channels */
  474. channels = params_channels(hw);
  475. /* set up format for the stream */
  476. format = params_format(hw);
  477. mutex_lock(&mgr->setup_mutex);
  478. /* update the stream levels */
  479. if( stream->pcm_number <= MIXART_PCM_DIGITAL ) {
  480. int is_aes = stream->pcm_number > MIXART_PCM_ANALOG;
  481. if( subs->stream == SNDRV_PCM_STREAM_PLAYBACK )
  482. mixart_update_playback_stream_level(chip, is_aes, subs->number);
  483. else
  484. mixart_update_capture_stream_level( chip, is_aes);
  485. }
  486. stream->channels = channels;
  487. /* set the format to the board */
  488. err = mixart_set_format(stream, format);
  489. if(err < 0) {
  490. return err;
  491. }
  492. /* allocate buffer */
  493. err = snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw));
  494. if (err > 0) {
  495. struct mixart_bufferinfo *bufferinfo;
  496. int i = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (stream->pcm_number * (MIXART_PLAYBACK_STREAMS+MIXART_CAPTURE_STREAMS)) + subs->number;
  497. if( subs->stream == SNDRV_PCM_STREAM_CAPTURE ) {
  498. i += MIXART_PLAYBACK_STREAMS; /* in array capture is behind playback */
  499. }
  500. bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;
  501. bufferinfo[i].buffer_address = subs->runtime->dma_addr;
  502. bufferinfo[i].available_length = subs->runtime->dma_bytes;
  503. /* bufferinfo[i].buffer_id is already defined */
  504. snd_printdd("snd_mixart_hw_params(pcm %d) : dma_addr(%x) dma_bytes(%x) subs-number(%d)\n", i,
  505. bufferinfo[i].buffer_address,
  506. bufferinfo[i].available_length,
  507. subs->number);
  508. }
  509. mutex_unlock(&mgr->setup_mutex);
  510. return err;
  511. }
  512. static int snd_mixart_hw_free(struct snd_pcm_substream *subs)
  513. {
  514. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  515. snd_pcm_lib_free_pages(subs);
  516. mixart_sync_nonblock_events(chip->mgr);
  517. return 0;
  518. }
  519. /*
  520. * TODO CONFIGURATION SPACE for all pcms, mono pcm must update channels_max
  521. */
  522. static struct snd_pcm_hardware snd_mixart_analog_caps =
  523. {
  524. .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  525. SNDRV_PCM_INFO_MMAP_VALID |
  526. SNDRV_PCM_INFO_PAUSE),
  527. .formats = ( SNDRV_PCM_FMTBIT_U8 |
  528. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  529. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
  530. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
  531. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  532. .rate_min = 8000,
  533. .rate_max = 48000,
  534. .channels_min = 1,
  535. .channels_max = 2,
  536. .buffer_bytes_max = (32*1024),
  537. .period_bytes_min = 256, /* 256 frames U8 mono*/
  538. .period_bytes_max = (16*1024),
  539. .periods_min = 2,
  540. .periods_max = (32*1024/256),
  541. };
  542. static struct snd_pcm_hardware snd_mixart_digital_caps =
  543. {
  544. .info = ( SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  545. SNDRV_PCM_INFO_MMAP_VALID |
  546. SNDRV_PCM_INFO_PAUSE),
  547. .formats = ( SNDRV_PCM_FMTBIT_U8 |
  548. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  549. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |
  550. SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE ),
  551. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  552. .rate_min = 32000,
  553. .rate_max = 48000,
  554. .channels_min = 1,
  555. .channels_max = 2,
  556. .buffer_bytes_max = (32*1024),
  557. .period_bytes_min = 256, /* 256 frames U8 mono*/
  558. .period_bytes_max = (16*1024),
  559. .periods_min = 2,
  560. .periods_max = (32*1024/256),
  561. };
  562. static int snd_mixart_playback_open(struct snd_pcm_substream *subs)
  563. {
  564. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  565. struct mixart_mgr *mgr = chip->mgr;
  566. struct snd_pcm_runtime *runtime = subs->runtime;
  567. struct snd_pcm *pcm = subs->pcm;
  568. struct mixart_stream *stream;
  569. struct mixart_pipe *pipe;
  570. int err = 0;
  571. int pcm_number;
  572. mutex_lock(&mgr->setup_mutex);
  573. if ( pcm == chip->pcm ) {
  574. pcm_number = MIXART_PCM_ANALOG;
  575. runtime->hw = snd_mixart_analog_caps;
  576. } else {
  577. snd_assert ( pcm == chip->pcm_dig );
  578. pcm_number = MIXART_PCM_DIGITAL;
  579. runtime->hw = snd_mixart_digital_caps;
  580. }
  581. snd_printdd("snd_mixart_playback_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
  582. /* get stream info */
  583. stream = &(chip->playback_stream[pcm_number][subs->number]);
  584. if (stream->status != MIXART_STREAM_STATUS_FREE){
  585. /* streams in use */
  586. snd_printk(KERN_ERR "snd_mixart_playback_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
  587. err = -EBUSY;
  588. goto _exit_open;
  589. }
  590. /* get pipe pointer (out pipe) */
  591. pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 0, 0);
  592. if (pipe == NULL) {
  593. err = -EINVAL;
  594. goto _exit_open;
  595. }
  596. /* start the pipe if necessary */
  597. err = mixart_set_pipe_state(chip->mgr, pipe, 1);
  598. if( err < 0 ) {
  599. snd_printk(KERN_ERR "error starting pipe!\n");
  600. snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
  601. err = -EINVAL;
  602. goto _exit_open;
  603. }
  604. stream->pipe = pipe;
  605. stream->pcm_number = pcm_number;
  606. stream->status = MIXART_STREAM_STATUS_OPEN;
  607. stream->substream = subs;
  608. stream->channels = 0; /* not configured yet */
  609. runtime->private_data = stream;
  610. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  611. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
  612. /* if a sample rate is already used, another stream cannot change */
  613. if(mgr->ref_count_rate++) {
  614. if(mgr->sample_rate) {
  615. runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
  616. }
  617. }
  618. _exit_open:
  619. mutex_unlock(&mgr->setup_mutex);
  620. return err;
  621. }
  622. static int snd_mixart_capture_open(struct snd_pcm_substream *subs)
  623. {
  624. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  625. struct mixart_mgr *mgr = chip->mgr;
  626. struct snd_pcm_runtime *runtime = subs->runtime;
  627. struct snd_pcm *pcm = subs->pcm;
  628. struct mixart_stream *stream;
  629. struct mixart_pipe *pipe;
  630. int err = 0;
  631. int pcm_number;
  632. mutex_lock(&mgr->setup_mutex);
  633. if ( pcm == chip->pcm ) {
  634. pcm_number = MIXART_PCM_ANALOG;
  635. runtime->hw = snd_mixart_analog_caps;
  636. } else {
  637. snd_assert ( pcm == chip->pcm_dig );
  638. pcm_number = MIXART_PCM_DIGITAL;
  639. runtime->hw = snd_mixart_digital_caps;
  640. }
  641. runtime->hw.channels_min = 2; /* for instance, no mono */
  642. snd_printdd("snd_mixart_capture_open C%d/P%d/Sub%d\n", chip->chip_idx, pcm_number, subs->number);
  643. /* get stream info */
  644. stream = &(chip->capture_stream[pcm_number]);
  645. if (stream->status != MIXART_STREAM_STATUS_FREE){
  646. /* streams in use */
  647. snd_printk(KERN_ERR "snd_mixart_capture_open C%d/P%d/Sub%d in use\n", chip->chip_idx, pcm_number, subs->number);
  648. err = -EBUSY;
  649. goto _exit_open;
  650. }
  651. /* get pipe pointer (in pipe) */
  652. pipe = snd_mixart_add_ref_pipe(chip, pcm_number, 1, 0);
  653. if (pipe == NULL) {
  654. err = -EINVAL;
  655. goto _exit_open;
  656. }
  657. /* start the pipe if necessary */
  658. err = mixart_set_pipe_state(chip->mgr, pipe, 1);
  659. if( err < 0 ) {
  660. snd_printk(KERN_ERR "error starting pipe!\n");
  661. snd_mixart_kill_ref_pipe(chip->mgr, pipe, 0);
  662. err = -EINVAL;
  663. goto _exit_open;
  664. }
  665. stream->pipe = pipe;
  666. stream->pcm_number = pcm_number;
  667. stream->status = MIXART_STREAM_STATUS_OPEN;
  668. stream->substream = subs;
  669. stream->channels = 0; /* not configured yet */
  670. runtime->private_data = stream;
  671. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  672. snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 64);
  673. /* if a sample rate is already used, another stream cannot change */
  674. if(mgr->ref_count_rate++) {
  675. if(mgr->sample_rate) {
  676. runtime->hw.rate_min = runtime->hw.rate_max = mgr->sample_rate;
  677. }
  678. }
  679. _exit_open:
  680. mutex_unlock(&mgr->setup_mutex);
  681. return err;
  682. }
  683. static int snd_mixart_close(struct snd_pcm_substream *subs)
  684. {
  685. struct snd_mixart *chip = snd_pcm_substream_chip(subs);
  686. struct mixart_mgr *mgr = chip->mgr;
  687. struct mixart_stream *stream = subs->runtime->private_data;
  688. mutex_lock(&mgr->setup_mutex);
  689. snd_printdd("snd_mixart_close C%d/P%d/Sub%d\n", chip->chip_idx, stream->pcm_number, subs->number);
  690. /* sample rate released */
  691. if(--mgr->ref_count_rate == 0) {
  692. mgr->sample_rate = 0;
  693. }
  694. /* delete pipe */
  695. if (snd_mixart_kill_ref_pipe(mgr, stream->pipe, 0 ) < 0) {
  696. snd_printk(KERN_ERR "error snd_mixart_kill_ref_pipe C%dP%d\n", chip->chip_idx, stream->pcm_number);
  697. }
  698. stream->pipe = NULL;
  699. stream->status = MIXART_STREAM_STATUS_FREE;
  700. stream->substream = NULL;
  701. mutex_unlock(&mgr->setup_mutex);
  702. return 0;
  703. }
  704. static snd_pcm_uframes_t snd_mixart_stream_pointer(struct snd_pcm_substream *subs)
  705. {
  706. struct snd_pcm_runtime *runtime = subs->runtime;
  707. struct mixart_stream *stream = runtime->private_data;
  708. return (snd_pcm_uframes_t)((stream->buf_periods * runtime->period_size) + stream->buf_period_frag);
  709. }
  710. static struct snd_pcm_ops snd_mixart_playback_ops = {
  711. .open = snd_mixart_playback_open,
  712. .close = snd_mixart_close,
  713. .ioctl = snd_pcm_lib_ioctl,
  714. .prepare = snd_mixart_prepare,
  715. .hw_params = snd_mixart_hw_params,
  716. .hw_free = snd_mixart_hw_free,
  717. .trigger = snd_mixart_trigger,
  718. .pointer = snd_mixart_stream_pointer,
  719. };
  720. static struct snd_pcm_ops snd_mixart_capture_ops = {
  721. .open = snd_mixart_capture_open,
  722. .close = snd_mixart_close,
  723. .ioctl = snd_pcm_lib_ioctl,
  724. .prepare = snd_mixart_prepare,
  725. .hw_params = snd_mixart_hw_params,
  726. .hw_free = snd_mixart_hw_free,
  727. .trigger = snd_mixart_trigger,
  728. .pointer = snd_mixart_stream_pointer,
  729. };
  730. static void preallocate_buffers(struct snd_mixart *chip, struct snd_pcm *pcm)
  731. {
  732. #if 0
  733. struct snd_pcm_substream *subs;
  734. int stream;
  735. for (stream = 0; stream < 2; stream++) {
  736. int idx = 0;
  737. for (subs = pcm->streams[stream].substream; subs; subs = subs->next, idx++)
  738. /* set up the unique device id with the chip index */
  739. subs->dma_device.id = subs->pcm->device << 16 |
  740. subs->stream << 8 | (subs->number + 1) |
  741. (chip->chip_idx + 1) << 24;
  742. }
  743. #endif
  744. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  745. snd_dma_pci_data(chip->mgr->pci), 32*1024, 32*1024);
  746. }
  747. /*
  748. */
  749. static int snd_mixart_pcm_analog(struct snd_mixart *chip)
  750. {
  751. int err;
  752. struct snd_pcm *pcm;
  753. char name[32];
  754. sprintf(name, "miXart analog %d", chip->chip_idx);
  755. if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_ANALOG,
  756. MIXART_PLAYBACK_STREAMS,
  757. MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
  758. snd_printk(KERN_ERR "cannot create the analog pcm %d\n", chip->chip_idx);
  759. return err;
  760. }
  761. pcm->private_data = chip;
  762. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
  763. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
  764. pcm->info_flags = 0;
  765. strcpy(pcm->name, name);
  766. preallocate_buffers(chip, pcm);
  767. chip->pcm = pcm;
  768. return 0;
  769. }
  770. /*
  771. */
  772. static int snd_mixart_pcm_digital(struct snd_mixart *chip)
  773. {
  774. int err;
  775. struct snd_pcm *pcm;
  776. char name[32];
  777. sprintf(name, "miXart AES/EBU %d", chip->chip_idx);
  778. if ((err = snd_pcm_new(chip->card, name, MIXART_PCM_DIGITAL,
  779. MIXART_PLAYBACK_STREAMS,
  780. MIXART_CAPTURE_STREAMS, &pcm)) < 0) {
  781. snd_printk(KERN_ERR "cannot create the digital pcm %d\n", chip->chip_idx);
  782. return err;
  783. }
  784. pcm->private_data = chip;
  785. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mixart_playback_ops);
  786. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mixart_capture_ops);
  787. pcm->info_flags = 0;
  788. strcpy(pcm->name, name);
  789. preallocate_buffers(chip, pcm);
  790. chip->pcm_dig = pcm;
  791. return 0;
  792. }
  793. static int snd_mixart_chip_free(struct snd_mixart *chip)
  794. {
  795. kfree(chip);
  796. return 0;
  797. }
  798. static int snd_mixart_chip_dev_free(struct snd_device *device)
  799. {
  800. struct snd_mixart *chip = device->device_data;
  801. return snd_mixart_chip_free(chip);
  802. }
  803. /*
  804. */
  805. static int __devinit snd_mixart_create(struct mixart_mgr *mgr, struct snd_card *card, int idx)
  806. {
  807. int err;
  808. struct snd_mixart *chip;
  809. static struct snd_device_ops ops = {
  810. .dev_free = snd_mixart_chip_dev_free,
  811. };
  812. mgr->chip[idx] = chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  813. if (! chip) {
  814. snd_printk(KERN_ERR "cannot allocate chip\n");
  815. return -ENOMEM;
  816. }
  817. chip->card = card;
  818. chip->chip_idx = idx;
  819. chip->mgr = mgr;
  820. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  821. snd_mixart_chip_free(chip);
  822. return err;
  823. }
  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 = ioremap_nocache(mgr->mem[i].phys,
  1070. pci_resource_len(pci, i));
  1071. if (!mgr->mem[i].virt) {
  1072. printk(KERN_ERR "unable to remap resource 0x%lx\n",
  1073. mgr->mem[i].phys);
  1074. snd_mixart_free(mgr);
  1075. return -EBUSY;
  1076. }
  1077. }
  1078. if (request_irq(pci->irq, snd_mixart_interrupt, IRQF_SHARED,
  1079. CARD_NAME, mgr)) {
  1080. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1081. snd_mixart_free(mgr);
  1082. return -EBUSY;
  1083. }
  1084. mgr->irq = pci->irq;
  1085. sprintf(mgr->shortname, "Digigram miXart");
  1086. sprintf(mgr->longname, "%s at 0x%lx & 0x%lx, irq %i", mgr->shortname, mgr->mem[0].phys, mgr->mem[1].phys, mgr->irq);
  1087. /* ISR spinlock */
  1088. spin_lock_init(&mgr->lock);
  1089. /* init mailbox */
  1090. mgr->msg_fifo_readptr = 0;
  1091. mgr->msg_fifo_writeptr = 0;
  1092. spin_lock_init(&mgr->msg_lock);
  1093. mutex_init(&mgr->msg_mutex);
  1094. init_waitqueue_head(&mgr->msg_sleep);
  1095. atomic_set(&mgr->msg_processed, 0);
  1096. /* init setup mutex*/
  1097. mutex_init(&mgr->setup_mutex);
  1098. /* init message taslket */
  1099. tasklet_init(&mgr->msg_taskq, snd_mixart_msg_tasklet, (unsigned long) mgr);
  1100. /* card assignment */
  1101. mgr->num_cards = MIXART_MAX_CARDS; /* 4 FIXME: configurable? */
  1102. for (i = 0; i < mgr->num_cards; i++) {
  1103. struct snd_card *card;
  1104. char tmpid[16];
  1105. int idx;
  1106. if (index[dev] < 0)
  1107. idx = index[dev];
  1108. else
  1109. idx = index[dev] + i;
  1110. snprintf(tmpid, sizeof(tmpid), "%s-%d", id[dev] ? id[dev] : "MIXART", i);
  1111. card = snd_card_new(idx, tmpid, THIS_MODULE, 0);
  1112. if (! card) {
  1113. snd_printk(KERN_ERR "cannot allocate the card %d\n", i);
  1114. snd_mixart_free(mgr);
  1115. return -ENOMEM;
  1116. }
  1117. strcpy(card->driver, CARD_NAME);
  1118. sprintf(card->shortname, "%s [PCM #%d]", mgr->shortname, i);
  1119. sprintf(card->longname, "%s [PCM #%d]", mgr->longname, i);
  1120. if ((err = snd_mixart_create(mgr, card, i)) < 0) {
  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)