mixart.c 40 KB

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