mixart.c 39 KB

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