mixart.c 39 KB

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