mixart.c 39 KB

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