sequencer.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /*
  2. * sound/oss/sequencer.c
  3. *
  4. * The sequencer personality manager.
  5. */
  6. /*
  7. * Copyright (C) by Hannu Savolainen 1993-1997
  8. *
  9. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11. * for more info.
  12. */
  13. /*
  14. * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
  15. * Alan Cox : reformatted and fixed a pair of null pointer bugs
  16. */
  17. #include <linux/kmod.h>
  18. #include <linux/spinlock.h>
  19. #include "sound_config.h"
  20. #include "midi_ctrl.h"
  21. #include "sleep.h"
  22. static int sequencer_ok;
  23. static struct sound_timer_operations *tmr;
  24. static int tmr_no = -1; /* Currently selected timer */
  25. static int pending_timer = -1; /* For timer change operation */
  26. extern unsigned long seq_time;
  27. static int obsolete_api_used;
  28. static DEFINE_SPINLOCK(lock);
  29. /*
  30. * Local counts for number of synth and MIDI devices. These are initialized
  31. * by the sequencer_open.
  32. */
  33. static int max_mididev;
  34. static int max_synthdev;
  35. /*
  36. * The seq_mode gives the operating mode of the sequencer:
  37. * 1 = level1 (the default)
  38. * 2 = level2 (extended capabilities)
  39. */
  40. #define SEQ_1 1
  41. #define SEQ_2 2
  42. static int seq_mode = SEQ_1;
  43. static DECLARE_WAIT_QUEUE_HEAD(seq_sleeper);
  44. static DECLARE_WAIT_QUEUE_HEAD(midi_sleeper);
  45. static int midi_opened[MAX_MIDI_DEV];
  46. static int midi_written[MAX_MIDI_DEV];
  47. static unsigned long prev_input_time;
  48. static int prev_event_time;
  49. #include "tuning.h"
  50. #define EV_SZ 8
  51. #define IEV_SZ 8
  52. static unsigned char *queue;
  53. static unsigned char *iqueue;
  54. static volatile int qhead, qtail, qlen;
  55. static volatile int iqhead, iqtail, iqlen;
  56. static volatile int seq_playing;
  57. static volatile int sequencer_busy;
  58. static int output_threshold;
  59. static long pre_event_timeout;
  60. static unsigned synth_open_mask;
  61. static int seq_queue(unsigned char *note, char nonblock);
  62. static void seq_startplay(void);
  63. static int seq_sync(void);
  64. static void seq_reset(void);
  65. #if MAX_SYNTH_DEV > 15
  66. #error Too many synthesizer devices enabled.
  67. #endif
  68. int sequencer_read(int dev, struct file *file, char __user *buf, int count)
  69. {
  70. int c = count, p = 0;
  71. int ev_len;
  72. unsigned long flags;
  73. dev = dev >> 4;
  74. ev_len = seq_mode == SEQ_1 ? 4 : 8;
  75. spin_lock_irqsave(&lock,flags);
  76. if (!iqlen)
  77. {
  78. spin_unlock_irqrestore(&lock,flags);
  79. if (file->f_flags & O_NONBLOCK) {
  80. return -EAGAIN;
  81. }
  82. oss_broken_sleep_on(&midi_sleeper, pre_event_timeout);
  83. spin_lock_irqsave(&lock,flags);
  84. if (!iqlen)
  85. {
  86. spin_unlock_irqrestore(&lock,flags);
  87. return 0;
  88. }
  89. }
  90. while (iqlen && c >= ev_len)
  91. {
  92. char *fixit = (char *) &iqueue[iqhead * IEV_SZ];
  93. spin_unlock_irqrestore(&lock,flags);
  94. if (copy_to_user(&(buf)[p], fixit, ev_len))
  95. return count - c;
  96. p += ev_len;
  97. c -= ev_len;
  98. spin_lock_irqsave(&lock,flags);
  99. iqhead = (iqhead + 1) % SEQ_MAX_QUEUE;
  100. iqlen--;
  101. }
  102. spin_unlock_irqrestore(&lock,flags);
  103. return count - c;
  104. }
  105. static void sequencer_midi_output(int dev)
  106. {
  107. /*
  108. * Currently NOP
  109. */
  110. }
  111. void seq_copy_to_input(unsigned char *event_rec, int len)
  112. {
  113. unsigned long flags;
  114. /*
  115. * Verify that the len is valid for the current mode.
  116. */
  117. if (len != 4 && len != 8)
  118. return;
  119. if ((seq_mode == SEQ_1) != (len == 4))
  120. return;
  121. if (iqlen >= (SEQ_MAX_QUEUE - 1))
  122. return; /* Overflow */
  123. spin_lock_irqsave(&lock,flags);
  124. memcpy(&iqueue[iqtail * IEV_SZ], event_rec, len);
  125. iqlen++;
  126. iqtail = (iqtail + 1) % SEQ_MAX_QUEUE;
  127. wake_up(&midi_sleeper);
  128. spin_unlock_irqrestore(&lock,flags);
  129. }
  130. EXPORT_SYMBOL(seq_copy_to_input);
  131. static void sequencer_midi_input(int dev, unsigned char data)
  132. {
  133. unsigned int tstamp;
  134. unsigned char event_rec[4];
  135. if (data == 0xfe) /* Ignore active sensing */
  136. return;
  137. tstamp = jiffies - seq_time;
  138. if (tstamp != prev_input_time)
  139. {
  140. tstamp = (tstamp << 8) | SEQ_WAIT;
  141. seq_copy_to_input((unsigned char *) &tstamp, 4);
  142. prev_input_time = tstamp;
  143. }
  144. event_rec[0] = SEQ_MIDIPUTC;
  145. event_rec[1] = data;
  146. event_rec[2] = dev;
  147. event_rec[3] = 0;
  148. seq_copy_to_input(event_rec, 4);
  149. }
  150. void seq_input_event(unsigned char *event_rec, int len)
  151. {
  152. unsigned long this_time;
  153. if (seq_mode == SEQ_2)
  154. this_time = tmr->get_time(tmr_no);
  155. else
  156. this_time = jiffies - seq_time;
  157. if (this_time != prev_input_time)
  158. {
  159. unsigned char tmp_event[8];
  160. tmp_event[0] = EV_TIMING;
  161. tmp_event[1] = TMR_WAIT_ABS;
  162. tmp_event[2] = 0;
  163. tmp_event[3] = 0;
  164. *(unsigned int *) &tmp_event[4] = this_time;
  165. seq_copy_to_input(tmp_event, 8);
  166. prev_input_time = this_time;
  167. }
  168. seq_copy_to_input(event_rec, len);
  169. }
  170. EXPORT_SYMBOL(seq_input_event);
  171. int sequencer_write(int dev, struct file *file, const char __user *buf, int count)
  172. {
  173. unsigned char event_rec[EV_SZ], ev_code;
  174. int p = 0, c, ev_size;
  175. int mode = translate_mode(file);
  176. dev = dev >> 4;
  177. if (mode == OPEN_READ)
  178. return -EIO;
  179. c = count;
  180. while (c >= 4)
  181. {
  182. if (copy_from_user((char *) event_rec, &(buf)[p], 4))
  183. goto out;
  184. ev_code = event_rec[0];
  185. if (ev_code == SEQ_FULLSIZE)
  186. {
  187. int err, fmt;
  188. dev = *(unsigned short *) &event_rec[2];
  189. if (dev < 0 || dev >= max_synthdev || synth_devs[dev] == NULL)
  190. return -ENXIO;
  191. if (!(synth_open_mask & (1 << dev)))
  192. return -ENXIO;
  193. fmt = (*(short *) &event_rec[0]) & 0xffff;
  194. err = synth_devs[dev]->load_patch(dev, fmt, buf + p, c, 0);
  195. if (err < 0)
  196. return err;
  197. return err;
  198. }
  199. if (ev_code >= 128)
  200. {
  201. if (seq_mode == SEQ_2 && ev_code == SEQ_EXTENDED)
  202. {
  203. printk(KERN_WARNING "Sequencer: Invalid level 2 event %x\n", ev_code);
  204. return -EINVAL;
  205. }
  206. ev_size = 8;
  207. if (c < ev_size)
  208. {
  209. if (!seq_playing)
  210. seq_startplay();
  211. return count - c;
  212. }
  213. if (copy_from_user((char *)&event_rec[4],
  214. &(buf)[p + 4], 4))
  215. goto out;
  216. }
  217. else
  218. {
  219. if (seq_mode == SEQ_2)
  220. {
  221. printk(KERN_WARNING "Sequencer: 4 byte event in level 2 mode\n");
  222. return -EINVAL;
  223. }
  224. ev_size = 4;
  225. if (event_rec[0] != SEQ_MIDIPUTC)
  226. obsolete_api_used = 1;
  227. }
  228. if (event_rec[0] == SEQ_MIDIPUTC)
  229. {
  230. if (!midi_opened[event_rec[2]])
  231. {
  232. int err, mode;
  233. int dev = event_rec[2];
  234. if (dev >= max_mididev || midi_devs[dev]==NULL)
  235. {
  236. /*printk("Sequencer Error: Nonexistent MIDI device %d\n", dev);*/
  237. return -ENXIO;
  238. }
  239. mode = translate_mode(file);
  240. if ((err = midi_devs[dev]->open(dev, mode,
  241. sequencer_midi_input, sequencer_midi_output)) < 0)
  242. {
  243. seq_reset();
  244. printk(KERN_WARNING "Sequencer Error: Unable to open Midi #%d\n", dev);
  245. return err;
  246. }
  247. midi_opened[dev] = 1;
  248. }
  249. }
  250. if (!seq_queue(event_rec, (file->f_flags & (O_NONBLOCK) ? 1 : 0)))
  251. {
  252. int processed = count - c;
  253. if (!seq_playing)
  254. seq_startplay();
  255. if (!processed && (file->f_flags & O_NONBLOCK))
  256. return -EAGAIN;
  257. else
  258. return processed;
  259. }
  260. p += ev_size;
  261. c -= ev_size;
  262. }
  263. if (!seq_playing)
  264. seq_startplay();
  265. out:
  266. return count;
  267. }
  268. static int seq_queue(unsigned char *note, char nonblock)
  269. {
  270. /*
  271. * Test if there is space in the queue
  272. */
  273. if (qlen >= SEQ_MAX_QUEUE)
  274. if (!seq_playing)
  275. seq_startplay(); /*
  276. * Give chance to drain the queue
  277. */
  278. if (!nonblock && qlen >= SEQ_MAX_QUEUE && !waitqueue_active(&seq_sleeper)) {
  279. /*
  280. * Sleep until there is enough space on the queue
  281. */
  282. oss_broken_sleep_on(&seq_sleeper, MAX_SCHEDULE_TIMEOUT);
  283. }
  284. if (qlen >= SEQ_MAX_QUEUE)
  285. {
  286. return 0; /*
  287. * To be sure
  288. */
  289. }
  290. memcpy(&queue[qtail * EV_SZ], note, EV_SZ);
  291. qtail = (qtail + 1) % SEQ_MAX_QUEUE;
  292. qlen++;
  293. return 1;
  294. }
  295. static int extended_event(unsigned char *q)
  296. {
  297. int dev = q[2];
  298. if (dev < 0 || dev >= max_synthdev)
  299. return -ENXIO;
  300. if (!(synth_open_mask & (1 << dev)))
  301. return -ENXIO;
  302. switch (q[1])
  303. {
  304. case SEQ_NOTEOFF:
  305. synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
  306. break;
  307. case SEQ_NOTEON:
  308. if (q[4] > 127 && q[4] != 255)
  309. return 0;
  310. if (q[5] == 0)
  311. {
  312. synth_devs[dev]->kill_note(dev, q[3], q[4], q[5]);
  313. break;
  314. }
  315. synth_devs[dev]->start_note(dev, q[3], q[4], q[5]);
  316. break;
  317. case SEQ_PGMCHANGE:
  318. synth_devs[dev]->set_instr(dev, q[3], q[4]);
  319. break;
  320. case SEQ_AFTERTOUCH:
  321. synth_devs[dev]->aftertouch(dev, q[3], q[4]);
  322. break;
  323. case SEQ_BALANCE:
  324. synth_devs[dev]->panning(dev, q[3], (char) q[4]);
  325. break;
  326. case SEQ_CONTROLLER:
  327. synth_devs[dev]->controller(dev, q[3], q[4], (short) (q[5] | (q[6] << 8)));
  328. break;
  329. case SEQ_VOLMODE:
  330. if (synth_devs[dev]->volume_method != NULL)
  331. synth_devs[dev]->volume_method(dev, q[3]);
  332. break;
  333. default:
  334. return -EINVAL;
  335. }
  336. return 0;
  337. }
  338. static int find_voice(int dev, int chn, int note)
  339. {
  340. unsigned short key;
  341. int i;
  342. key = (chn << 8) | (note + 1);
  343. for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
  344. if (synth_devs[dev]->alloc.map[i] == key)
  345. return i;
  346. return -1;
  347. }
  348. static int alloc_voice(int dev, int chn, int note)
  349. {
  350. unsigned short key;
  351. int voice;
  352. key = (chn << 8) | (note + 1);
  353. voice = synth_devs[dev]->alloc_voice(dev, chn, note,
  354. &synth_devs[dev]->alloc);
  355. synth_devs[dev]->alloc.map[voice] = key;
  356. synth_devs[dev]->alloc.alloc_times[voice] =
  357. synth_devs[dev]->alloc.timestamp++;
  358. return voice;
  359. }
  360. static void seq_chn_voice_event(unsigned char *event_rec)
  361. {
  362. #define dev event_rec[1]
  363. #define cmd event_rec[2]
  364. #define chn event_rec[3]
  365. #define note event_rec[4]
  366. #define parm event_rec[5]
  367. int voice = -1;
  368. if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
  369. return;
  370. if (!(synth_open_mask & (1 << dev)))
  371. return;
  372. if (!synth_devs[dev])
  373. return;
  374. if (seq_mode == SEQ_2)
  375. {
  376. if (synth_devs[dev]->alloc_voice)
  377. voice = find_voice(dev, chn, note);
  378. if (cmd == MIDI_NOTEON && parm == 0)
  379. {
  380. cmd = MIDI_NOTEOFF;
  381. parm = 64;
  382. }
  383. }
  384. switch (cmd)
  385. {
  386. case MIDI_NOTEON:
  387. if (note > 127 && note != 255) /* Not a seq2 feature */
  388. return;
  389. if (voice == -1 && seq_mode == SEQ_2 && synth_devs[dev]->alloc_voice)
  390. {
  391. /* Internal synthesizer (FM, GUS, etc) */
  392. voice = alloc_voice(dev, chn, note);
  393. }
  394. if (voice == -1)
  395. voice = chn;
  396. if (seq_mode == SEQ_2 && (int) dev < num_synths)
  397. {
  398. /*
  399. * The MIDI channel 10 is a percussive channel. Use the note
  400. * number to select the proper patch (128 to 255) to play.
  401. */
  402. if (chn == 9)
  403. {
  404. synth_devs[dev]->set_instr(dev, voice, 128 + note);
  405. synth_devs[dev]->chn_info[chn].pgm_num = 128 + note;
  406. }
  407. synth_devs[dev]->setup_voice(dev, voice, chn);
  408. }
  409. synth_devs[dev]->start_note(dev, voice, note, parm);
  410. break;
  411. case MIDI_NOTEOFF:
  412. if (voice == -1)
  413. voice = chn;
  414. synth_devs[dev]->kill_note(dev, voice, note, parm);
  415. break;
  416. case MIDI_KEY_PRESSURE:
  417. if (voice == -1)
  418. voice = chn;
  419. synth_devs[dev]->aftertouch(dev, voice, parm);
  420. break;
  421. default:;
  422. }
  423. #undef dev
  424. #undef cmd
  425. #undef chn
  426. #undef note
  427. #undef parm
  428. }
  429. static void seq_chn_common_event(unsigned char *event_rec)
  430. {
  431. unsigned char dev = event_rec[1];
  432. unsigned char cmd = event_rec[2];
  433. unsigned char chn = event_rec[3];
  434. unsigned char p1 = event_rec[4];
  435. /* unsigned char p2 = event_rec[5]; */
  436. unsigned short w14 = *(short *) &event_rec[6];
  437. if ((int) dev > max_synthdev || synth_devs[dev] == NULL)
  438. return;
  439. if (!(synth_open_mask & (1 << dev)))
  440. return;
  441. if (!synth_devs[dev])
  442. return;
  443. switch (cmd)
  444. {
  445. case MIDI_PGM_CHANGE:
  446. if (seq_mode == SEQ_2)
  447. {
  448. if (chn > 15)
  449. break;
  450. synth_devs[dev]->chn_info[chn].pgm_num = p1;
  451. if ((int) dev >= num_synths)
  452. synth_devs[dev]->set_instr(dev, chn, p1);
  453. }
  454. else
  455. synth_devs[dev]->set_instr(dev, chn, p1);
  456. break;
  457. case MIDI_CTL_CHANGE:
  458. if (seq_mode == SEQ_2)
  459. {
  460. if (chn > 15 || p1 > 127)
  461. break;
  462. synth_devs[dev]->chn_info[chn].controllers[p1] = w14 & 0x7f;
  463. if (p1 < 32) /* Setting MSB should clear LSB to 0 */
  464. synth_devs[dev]->chn_info[chn].controllers[p1 + 32] = 0;
  465. if ((int) dev < num_synths)
  466. {
  467. int val = w14 & 0x7f;
  468. int i, key;
  469. if (p1 < 64) /* Combine MSB and LSB */
  470. {
  471. val = ((synth_devs[dev]->
  472. chn_info[chn].controllers[p1 & ~32] & 0x7f) << 7)
  473. | (synth_devs[dev]->
  474. chn_info[chn].controllers[p1 | 32] & 0x7f);
  475. p1 &= ~32;
  476. }
  477. /* Handle all playing notes on this channel */
  478. key = ((int) chn << 8);
  479. for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
  480. if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
  481. synth_devs[dev]->controller(dev, i, p1, val);
  482. }
  483. else
  484. synth_devs[dev]->controller(dev, chn, p1, w14);
  485. }
  486. else /* Mode 1 */
  487. synth_devs[dev]->controller(dev, chn, p1, w14);
  488. break;
  489. case MIDI_PITCH_BEND:
  490. if (seq_mode == SEQ_2)
  491. {
  492. if (chn > 15)
  493. break;
  494. synth_devs[dev]->chn_info[chn].bender_value = w14;
  495. if ((int) dev < num_synths)
  496. {
  497. /* Handle all playing notes on this channel */
  498. int i, key;
  499. key = (chn << 8);
  500. for (i = 0; i < synth_devs[dev]->alloc.max_voice; i++)
  501. if ((synth_devs[dev]->alloc.map[i] & 0xff00) == key)
  502. synth_devs[dev]->bender(dev, i, w14);
  503. }
  504. else
  505. synth_devs[dev]->bender(dev, chn, w14);
  506. }
  507. else /* MODE 1 */
  508. synth_devs[dev]->bender(dev, chn, w14);
  509. break;
  510. default:;
  511. }
  512. }
  513. static int seq_timing_event(unsigned char *event_rec)
  514. {
  515. unsigned char cmd = event_rec[1];
  516. unsigned int parm = *(int *) &event_rec[4];
  517. if (seq_mode == SEQ_2)
  518. {
  519. int ret;
  520. if ((ret = tmr->event(tmr_no, event_rec)) == TIMER_ARMED)
  521. if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
  522. wake_up(&seq_sleeper);
  523. return ret;
  524. }
  525. switch (cmd)
  526. {
  527. case TMR_WAIT_REL:
  528. parm += prev_event_time;
  529. /*
  530. * NOTE! No break here. Execution of TMR_WAIT_REL continues in the
  531. * next case (TMR_WAIT_ABS)
  532. */
  533. case TMR_WAIT_ABS:
  534. if (parm > 0)
  535. {
  536. long time;
  537. time = parm;
  538. prev_event_time = time;
  539. seq_playing = 1;
  540. request_sound_timer(time);
  541. if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
  542. wake_up(&seq_sleeper);
  543. return TIMER_ARMED;
  544. }
  545. break;
  546. case TMR_START:
  547. seq_time = jiffies;
  548. prev_input_time = 0;
  549. prev_event_time = 0;
  550. break;
  551. case TMR_STOP:
  552. break;
  553. case TMR_CONTINUE:
  554. break;
  555. case TMR_TEMPO:
  556. break;
  557. case TMR_ECHO:
  558. parm = (parm << 8 | SEQ_ECHO);
  559. seq_copy_to_input((unsigned char *) &parm, 4);
  560. break;
  561. default:;
  562. }
  563. return TIMER_NOT_ARMED;
  564. }
  565. static void seq_local_event(unsigned char *event_rec)
  566. {
  567. unsigned char cmd = event_rec[1];
  568. unsigned int parm = *((unsigned int *) &event_rec[4]);
  569. switch (cmd)
  570. {
  571. case LOCL_STARTAUDIO:
  572. DMAbuf_start_devices(parm);
  573. break;
  574. default:;
  575. }
  576. }
  577. static void seq_sysex_message(unsigned char *event_rec)
  578. {
  579. unsigned int dev = event_rec[1];
  580. int i, l = 0;
  581. unsigned char *buf = &event_rec[2];
  582. if (dev > max_synthdev)
  583. return;
  584. if (!(synth_open_mask & (1 << dev)))
  585. return;
  586. if (!synth_devs[dev])
  587. return;
  588. l = 0;
  589. for (i = 0; i < 6 && buf[i] != 0xff; i++)
  590. l = i + 1;
  591. if (!synth_devs[dev]->send_sysex)
  592. return;
  593. if (l > 0)
  594. synth_devs[dev]->send_sysex(dev, buf, l);
  595. }
  596. static int play_event(unsigned char *q)
  597. {
  598. /*
  599. * NOTE! This routine returns
  600. * 0 = normal event played.
  601. * 1 = Timer armed. Suspend playback until timer callback.
  602. * 2 = MIDI output buffer full. Restore queue and suspend until timer
  603. */
  604. unsigned int *delay;
  605. switch (q[0])
  606. {
  607. case SEQ_NOTEOFF:
  608. if (synth_open_mask & (1 << 0))
  609. if (synth_devs[0])
  610. synth_devs[0]->kill_note(0, q[1], 255, q[3]);
  611. break;
  612. case SEQ_NOTEON:
  613. if (q[4] < 128 || q[4] == 255)
  614. if (synth_open_mask & (1 << 0))
  615. if (synth_devs[0])
  616. synth_devs[0]->start_note(0, q[1], q[2], q[3]);
  617. break;
  618. case SEQ_WAIT:
  619. delay = (unsigned int *) q; /*
  620. * Bytes 1 to 3 are containing the *
  621. * delay in 'ticks'
  622. */
  623. *delay = (*delay >> 8) & 0xffffff;
  624. if (*delay > 0)
  625. {
  626. long time;
  627. seq_playing = 1;
  628. time = *delay;
  629. prev_event_time = time;
  630. request_sound_timer(time);
  631. if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
  632. wake_up(&seq_sleeper);
  633. /*
  634. * The timer is now active and will reinvoke this function
  635. * after the timer expires. Return to the caller now.
  636. */
  637. return 1;
  638. }
  639. break;
  640. case SEQ_PGMCHANGE:
  641. if (synth_open_mask & (1 << 0))
  642. if (synth_devs[0])
  643. synth_devs[0]->set_instr(0, q[1], q[2]);
  644. break;
  645. case SEQ_SYNCTIMER: /*
  646. * Reset timer
  647. */
  648. seq_time = jiffies;
  649. prev_input_time = 0;
  650. prev_event_time = 0;
  651. break;
  652. case SEQ_MIDIPUTC: /*
  653. * Put a midi character
  654. */
  655. if (midi_opened[q[2]])
  656. {
  657. int dev;
  658. dev = q[2];
  659. if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
  660. break;
  661. if (!midi_devs[dev]->outputc(dev, q[1]))
  662. {
  663. /*
  664. * Output FIFO is full. Wait one timer cycle and try again.
  665. */
  666. seq_playing = 1;
  667. request_sound_timer(-1);
  668. return 2;
  669. }
  670. else
  671. midi_written[dev] = 1;
  672. }
  673. break;
  674. case SEQ_ECHO:
  675. seq_copy_to_input(q, 4); /*
  676. * Echo back to the process
  677. */
  678. break;
  679. case SEQ_PRIVATE:
  680. if ((int) q[1] < max_synthdev)
  681. synth_devs[q[1]]->hw_control(q[1], q);
  682. break;
  683. case SEQ_EXTENDED:
  684. extended_event(q);
  685. break;
  686. case EV_CHN_VOICE:
  687. seq_chn_voice_event(q);
  688. break;
  689. case EV_CHN_COMMON:
  690. seq_chn_common_event(q);
  691. break;
  692. case EV_TIMING:
  693. if (seq_timing_event(q) == TIMER_ARMED)
  694. {
  695. return 1;
  696. }
  697. break;
  698. case EV_SEQ_LOCAL:
  699. seq_local_event(q);
  700. break;
  701. case EV_SYSEX:
  702. seq_sysex_message(q);
  703. break;
  704. default:;
  705. }
  706. return 0;
  707. }
  708. /* called also as timer in irq context */
  709. static void seq_startplay(void)
  710. {
  711. int this_one, action;
  712. unsigned long flags;
  713. while (qlen > 0)
  714. {
  715. spin_lock_irqsave(&lock,flags);
  716. qhead = ((this_one = qhead) + 1) % SEQ_MAX_QUEUE;
  717. qlen--;
  718. spin_unlock_irqrestore(&lock,flags);
  719. seq_playing = 1;
  720. if ((action = play_event(&queue[this_one * EV_SZ])))
  721. { /* Suspend playback. Next timer routine invokes this routine again */
  722. if (action == 2)
  723. {
  724. qlen++;
  725. qhead = this_one;
  726. }
  727. return;
  728. }
  729. }
  730. seq_playing = 0;
  731. if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
  732. wake_up(&seq_sleeper);
  733. }
  734. static void reset_controllers(int dev, unsigned char *controller, int update_dev)
  735. {
  736. int i;
  737. for (i = 0; i < 128; i++)
  738. controller[i] = ctrl_def_values[i];
  739. }
  740. static void setup_mode2(void)
  741. {
  742. int dev;
  743. max_synthdev = num_synths;
  744. for (dev = 0; dev < num_midis; dev++)
  745. {
  746. if (midi_devs[dev] && midi_devs[dev]->converter != NULL)
  747. {
  748. synth_devs[max_synthdev++] = midi_devs[dev]->converter;
  749. }
  750. }
  751. for (dev = 0; dev < max_synthdev; dev++)
  752. {
  753. int chn;
  754. synth_devs[dev]->sysex_ptr = 0;
  755. synth_devs[dev]->emulation = 0;
  756. for (chn = 0; chn < 16; chn++)
  757. {
  758. synth_devs[dev]->chn_info[chn].pgm_num = 0;
  759. reset_controllers(dev,
  760. synth_devs[dev]->chn_info[chn].controllers,0);
  761. synth_devs[dev]->chn_info[chn].bender_value = (1 << 7); /* Neutral */
  762. synth_devs[dev]->chn_info[chn].bender_range = 200;
  763. }
  764. }
  765. max_mididev = 0;
  766. seq_mode = SEQ_2;
  767. }
  768. int sequencer_open(int dev, struct file *file)
  769. {
  770. int retval, mode, i;
  771. int level, tmp;
  772. if (!sequencer_ok)
  773. sequencer_init();
  774. level = ((dev & 0x0f) == SND_DEV_SEQ2) ? 2 : 1;
  775. dev = dev >> 4;
  776. mode = translate_mode(file);
  777. if (!sequencer_ok)
  778. {
  779. /* printk("Sound card: sequencer not initialized\n");*/
  780. return -ENXIO;
  781. }
  782. if (dev) /* Patch manager device (obsolete) */
  783. return -ENXIO;
  784. if(synth_devs[dev] == NULL)
  785. request_module("synth0");
  786. if (mode == OPEN_READ)
  787. {
  788. if (!num_midis)
  789. {
  790. /*printk("Sequencer: No MIDI devices. Input not possible\n");*/
  791. sequencer_busy = 0;
  792. return -ENXIO;
  793. }
  794. }
  795. if (sequencer_busy)
  796. {
  797. return -EBUSY;
  798. }
  799. sequencer_busy = 1;
  800. obsolete_api_used = 0;
  801. max_mididev = num_midis;
  802. max_synthdev = num_synths;
  803. pre_event_timeout = MAX_SCHEDULE_TIMEOUT;
  804. seq_mode = SEQ_1;
  805. if (pending_timer != -1)
  806. {
  807. tmr_no = pending_timer;
  808. pending_timer = -1;
  809. }
  810. if (tmr_no == -1) /* Not selected yet */
  811. {
  812. int i, best;
  813. best = -1;
  814. for (i = 0; i < num_sound_timers; i++)
  815. if (sound_timer_devs[i] && sound_timer_devs[i]->priority > best)
  816. {
  817. tmr_no = i;
  818. best = sound_timer_devs[i]->priority;
  819. }
  820. if (tmr_no == -1) /* Should not be */
  821. tmr_no = 0;
  822. }
  823. tmr = sound_timer_devs[tmr_no];
  824. if (level == 2)
  825. {
  826. if (tmr == NULL)
  827. {
  828. /*printk("sequencer: No timer for level 2\n");*/
  829. sequencer_busy = 0;
  830. return -ENXIO;
  831. }
  832. setup_mode2();
  833. }
  834. if (!max_synthdev && !max_mididev)
  835. {
  836. sequencer_busy=0;
  837. return -ENXIO;
  838. }
  839. synth_open_mask = 0;
  840. for (i = 0; i < max_mididev; i++)
  841. {
  842. midi_opened[i] = 0;
  843. midi_written[i] = 0;
  844. }
  845. for (i = 0; i < max_synthdev; i++)
  846. {
  847. if (synth_devs[i]==NULL)
  848. continue;
  849. if (!try_module_get(synth_devs[i]->owner))
  850. continue;
  851. if ((tmp = synth_devs[i]->open(i, mode)) < 0)
  852. {
  853. printk(KERN_WARNING "Sequencer: Warning! Cannot open synth device #%d (%d)\n", i, tmp);
  854. if (synth_devs[i]->midi_dev)
  855. printk(KERN_WARNING "(Maps to MIDI dev #%d)\n", synth_devs[i]->midi_dev);
  856. }
  857. else
  858. {
  859. synth_open_mask |= (1 << i);
  860. if (synth_devs[i]->midi_dev)
  861. midi_opened[synth_devs[i]->midi_dev] = 1;
  862. }
  863. }
  864. seq_time = jiffies;
  865. prev_input_time = 0;
  866. prev_event_time = 0;
  867. if (seq_mode == SEQ_1 && (mode == OPEN_READ || mode == OPEN_READWRITE))
  868. {
  869. /*
  870. * Initialize midi input devices
  871. */
  872. for (i = 0; i < max_mididev; i++)
  873. if (!midi_opened[i] && midi_devs[i])
  874. {
  875. if (!try_module_get(midi_devs[i]->owner))
  876. continue;
  877. if ((retval = midi_devs[i]->open(i, mode,
  878. sequencer_midi_input, sequencer_midi_output)) >= 0)
  879. {
  880. midi_opened[i] = 1;
  881. }
  882. }
  883. }
  884. if (seq_mode == SEQ_2) {
  885. if (try_module_get(tmr->owner))
  886. tmr->open(tmr_no, seq_mode);
  887. }
  888. init_waitqueue_head(&seq_sleeper);
  889. init_waitqueue_head(&midi_sleeper);
  890. output_threshold = SEQ_MAX_QUEUE / 2;
  891. return 0;
  892. }
  893. static void seq_drain_midi_queues(void)
  894. {
  895. int i, n;
  896. /*
  897. * Give the Midi drivers time to drain their output queues
  898. */
  899. n = 1;
  900. while (!signal_pending(current) && n)
  901. {
  902. n = 0;
  903. for (i = 0; i < max_mididev; i++)
  904. if (midi_opened[i] && midi_written[i])
  905. if (midi_devs[i]->buffer_status != NULL)
  906. if (midi_devs[i]->buffer_status(i))
  907. n++;
  908. /*
  909. * Let's have a delay
  910. */
  911. if (n)
  912. oss_broken_sleep_on(&seq_sleeper, HZ/10);
  913. }
  914. }
  915. void sequencer_release(int dev, struct file *file)
  916. {
  917. int i;
  918. int mode = translate_mode(file);
  919. dev = dev >> 4;
  920. /*
  921. * Wait until the queue is empty (if we don't have nonblock)
  922. */
  923. if (mode != OPEN_READ && !(file->f_flags & O_NONBLOCK))
  924. {
  925. while (!signal_pending(current) && qlen > 0)
  926. {
  927. seq_sync();
  928. oss_broken_sleep_on(&seq_sleeper, 3*HZ);
  929. /* Extra delay */
  930. }
  931. }
  932. if (mode != OPEN_READ)
  933. seq_drain_midi_queues(); /*
  934. * Ensure the output queues are empty
  935. */
  936. seq_reset();
  937. if (mode != OPEN_READ)
  938. seq_drain_midi_queues(); /*
  939. * Flush the all notes off messages
  940. */
  941. for (i = 0; i < max_synthdev; i++)
  942. {
  943. if (synth_open_mask & (1 << i)) /*
  944. * Actually opened
  945. */
  946. if (synth_devs[i])
  947. {
  948. synth_devs[i]->close(i);
  949. module_put(synth_devs[i]->owner);
  950. if (synth_devs[i]->midi_dev)
  951. midi_opened[synth_devs[i]->midi_dev] = 0;
  952. }
  953. }
  954. for (i = 0; i < max_mididev; i++)
  955. {
  956. if (midi_opened[i]) {
  957. midi_devs[i]->close(i);
  958. module_put(midi_devs[i]->owner);
  959. }
  960. }
  961. if (seq_mode == SEQ_2) {
  962. tmr->close(tmr_no);
  963. module_put(tmr->owner);
  964. }
  965. if (obsolete_api_used)
  966. printk(KERN_WARNING "/dev/music: Obsolete (4 byte) API was used by %s\n", current->comm);
  967. sequencer_busy = 0;
  968. }
  969. static int seq_sync(void)
  970. {
  971. if (qlen && !seq_playing && !signal_pending(current))
  972. seq_startplay();
  973. if (qlen > 0)
  974. oss_broken_sleep_on(&seq_sleeper, HZ);
  975. return qlen;
  976. }
  977. static void midi_outc(int dev, unsigned char data)
  978. {
  979. /*
  980. * NOTE! Calls sleep(). Don't call this from interrupt.
  981. */
  982. int n;
  983. unsigned long flags;
  984. /*
  985. * This routine sends one byte to the Midi channel.
  986. * If the output FIFO is full, it waits until there
  987. * is space in the queue
  988. */
  989. n = 3 * HZ; /* Timeout */
  990. spin_lock_irqsave(&lock,flags);
  991. while (n && !midi_devs[dev]->outputc(dev, data)) {
  992. oss_broken_sleep_on(&seq_sleeper, HZ/25);
  993. n--;
  994. }
  995. spin_unlock_irqrestore(&lock,flags);
  996. }
  997. static void seq_reset(void)
  998. {
  999. /*
  1000. * NOTE! Calls sleep(). Don't call this from interrupt.
  1001. */
  1002. int i;
  1003. int chn;
  1004. unsigned long flags;
  1005. sound_stop_timer();
  1006. seq_time = jiffies;
  1007. prev_input_time = 0;
  1008. prev_event_time = 0;
  1009. qlen = qhead = qtail = 0;
  1010. iqlen = iqhead = iqtail = 0;
  1011. for (i = 0; i < max_synthdev; i++)
  1012. if (synth_open_mask & (1 << i))
  1013. if (synth_devs[i])
  1014. synth_devs[i]->reset(i);
  1015. if (seq_mode == SEQ_2)
  1016. {
  1017. for (chn = 0; chn < 16; chn++)
  1018. for (i = 0; i < max_synthdev; i++)
  1019. if (synth_open_mask & (1 << i))
  1020. if (synth_devs[i])
  1021. {
  1022. synth_devs[i]->controller(i, chn, 123, 0); /* All notes off */
  1023. synth_devs[i]->controller(i, chn, 121, 0); /* Reset all ctl */
  1024. synth_devs[i]->bender(i, chn, 1 << 13); /* Bender off */
  1025. }
  1026. }
  1027. else /* seq_mode == SEQ_1 */
  1028. {
  1029. for (i = 0; i < max_mididev; i++)
  1030. if (midi_written[i]) /*
  1031. * Midi used. Some notes may still be playing
  1032. */
  1033. {
  1034. /*
  1035. * Sending just a ACTIVE SENSING message should be enough to stop all
  1036. * playing notes. Since there are devices not recognizing the
  1037. * active sensing, we have to send some all notes off messages also.
  1038. */
  1039. midi_outc(i, 0xfe);
  1040. for (chn = 0; chn < 16; chn++)
  1041. {
  1042. midi_outc(i, (unsigned char) (0xb0 + (chn & 0x0f))); /* control change */
  1043. midi_outc(i, 0x7b); /* All notes off */
  1044. midi_outc(i, 0); /* Dummy parameter */
  1045. }
  1046. midi_devs[i]->close(i);
  1047. midi_written[i] = 0;
  1048. midi_opened[i] = 0;
  1049. }
  1050. }
  1051. seq_playing = 0;
  1052. spin_lock_irqsave(&lock,flags);
  1053. if (waitqueue_active(&seq_sleeper)) {
  1054. /* printk( "Sequencer Warning: Unexpected sleeping process - Waking up\n"); */
  1055. wake_up(&seq_sleeper);
  1056. }
  1057. spin_unlock_irqrestore(&lock,flags);
  1058. }
  1059. static void seq_panic(void)
  1060. {
  1061. /*
  1062. * This routine is called by the application in case the user
  1063. * wants to reset the system to the default state.
  1064. */
  1065. seq_reset();
  1066. /*
  1067. * Since some of the devices don't recognize the active sensing and
  1068. * all notes off messages, we have to shut all notes manually.
  1069. *
  1070. * TO BE IMPLEMENTED LATER
  1071. */
  1072. /*
  1073. * Also return the controllers to their default states
  1074. */
  1075. }
  1076. int sequencer_ioctl(int dev, struct file *file, unsigned int cmd, void __user *arg)
  1077. {
  1078. int midi_dev, orig_dev, val, err;
  1079. int mode = translate_mode(file);
  1080. struct synth_info inf;
  1081. struct seq_event_rec event_rec;
  1082. int __user *p = arg;
  1083. orig_dev = dev = dev >> 4;
  1084. switch (cmd)
  1085. {
  1086. case SNDCTL_TMR_TIMEBASE:
  1087. case SNDCTL_TMR_TEMPO:
  1088. case SNDCTL_TMR_START:
  1089. case SNDCTL_TMR_STOP:
  1090. case SNDCTL_TMR_CONTINUE:
  1091. case SNDCTL_TMR_METRONOME:
  1092. case SNDCTL_TMR_SOURCE:
  1093. if (seq_mode != SEQ_2)
  1094. return -EINVAL;
  1095. return tmr->ioctl(tmr_no, cmd, arg);
  1096. case SNDCTL_TMR_SELECT:
  1097. if (seq_mode != SEQ_2)
  1098. return -EINVAL;
  1099. if (get_user(pending_timer, p))
  1100. return -EFAULT;
  1101. if (pending_timer < 0 || pending_timer >= num_sound_timers || sound_timer_devs[pending_timer] == NULL)
  1102. {
  1103. pending_timer = -1;
  1104. return -EINVAL;
  1105. }
  1106. val = pending_timer;
  1107. break;
  1108. case SNDCTL_SEQ_PANIC:
  1109. seq_panic();
  1110. return -EINVAL;
  1111. case SNDCTL_SEQ_SYNC:
  1112. if (mode == OPEN_READ)
  1113. return 0;
  1114. while (qlen > 0 && !signal_pending(current))
  1115. seq_sync();
  1116. return qlen ? -EINTR : 0;
  1117. case SNDCTL_SEQ_RESET:
  1118. seq_reset();
  1119. return 0;
  1120. case SNDCTL_SEQ_TESTMIDI:
  1121. if (__get_user(midi_dev, p))
  1122. return -EFAULT;
  1123. if (midi_dev < 0 || midi_dev >= max_mididev || !midi_devs[midi_dev])
  1124. return -ENXIO;
  1125. if (!midi_opened[midi_dev] &&
  1126. (err = midi_devs[midi_dev]->open(midi_dev, mode, sequencer_midi_input,
  1127. sequencer_midi_output)) < 0)
  1128. return err;
  1129. midi_opened[midi_dev] = 1;
  1130. return 0;
  1131. case SNDCTL_SEQ_GETINCOUNT:
  1132. if (mode == OPEN_WRITE)
  1133. return 0;
  1134. val = iqlen;
  1135. break;
  1136. case SNDCTL_SEQ_GETOUTCOUNT:
  1137. if (mode == OPEN_READ)
  1138. return 0;
  1139. val = SEQ_MAX_QUEUE - qlen;
  1140. break;
  1141. case SNDCTL_SEQ_GETTIME:
  1142. if (seq_mode == SEQ_2)
  1143. return tmr->ioctl(tmr_no, cmd, arg);
  1144. val = jiffies - seq_time;
  1145. break;
  1146. case SNDCTL_SEQ_CTRLRATE:
  1147. /*
  1148. * If *arg == 0, just return the current rate
  1149. */
  1150. if (seq_mode == SEQ_2)
  1151. return tmr->ioctl(tmr_no, cmd, arg);
  1152. if (get_user(val, p))
  1153. return -EFAULT;
  1154. if (val != 0)
  1155. return -EINVAL;
  1156. val = HZ;
  1157. break;
  1158. case SNDCTL_SEQ_RESETSAMPLES:
  1159. case SNDCTL_SYNTH_REMOVESAMPLE:
  1160. case SNDCTL_SYNTH_CONTROL:
  1161. if (get_user(dev, p))
  1162. return -EFAULT;
  1163. if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
  1164. return -ENXIO;
  1165. if (!(synth_open_mask & (1 << dev)) && !orig_dev)
  1166. return -EBUSY;
  1167. return synth_devs[dev]->ioctl(dev, cmd, arg);
  1168. case SNDCTL_SEQ_NRSYNTHS:
  1169. val = max_synthdev;
  1170. break;
  1171. case SNDCTL_SEQ_NRMIDIS:
  1172. val = max_mididev;
  1173. break;
  1174. case SNDCTL_SYNTH_MEMAVL:
  1175. if (get_user(dev, p))
  1176. return -EFAULT;
  1177. if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
  1178. return -ENXIO;
  1179. if (!(synth_open_mask & (1 << dev)) && !orig_dev)
  1180. return -EBUSY;
  1181. val = synth_devs[dev]->ioctl(dev, cmd, arg);
  1182. break;
  1183. case SNDCTL_FM_4OP_ENABLE:
  1184. if (get_user(dev, p))
  1185. return -EFAULT;
  1186. if (dev < 0 || dev >= num_synths || synth_devs[dev] == NULL)
  1187. return -ENXIO;
  1188. if (!(synth_open_mask & (1 << dev)))
  1189. return -ENXIO;
  1190. synth_devs[dev]->ioctl(dev, cmd, arg);
  1191. return 0;
  1192. case SNDCTL_SYNTH_INFO:
  1193. if (get_user(dev, &((struct synth_info __user *)arg)->device))
  1194. return -EFAULT;
  1195. if (dev < 0 || dev >= max_synthdev)
  1196. return -ENXIO;
  1197. if (!(synth_open_mask & (1 << dev)) && !orig_dev)
  1198. return -EBUSY;
  1199. return synth_devs[dev]->ioctl(dev, cmd, arg);
  1200. /* Like SYNTH_INFO but returns ID in the name field */
  1201. case SNDCTL_SYNTH_ID:
  1202. if (get_user(dev, &((struct synth_info __user *)arg)->device))
  1203. return -EFAULT;
  1204. if (dev < 0 || dev >= max_synthdev)
  1205. return -ENXIO;
  1206. if (!(synth_open_mask & (1 << dev)) && !orig_dev)
  1207. return -EBUSY;
  1208. memcpy(&inf, synth_devs[dev]->info, sizeof(inf));
  1209. strlcpy(inf.name, synth_devs[dev]->id, sizeof(inf.name));
  1210. inf.device = dev;
  1211. return copy_to_user(arg, &inf, sizeof(inf))?-EFAULT:0;
  1212. case SNDCTL_SEQ_OUTOFBAND:
  1213. if (copy_from_user(&event_rec, arg, sizeof(event_rec)))
  1214. return -EFAULT;
  1215. play_event(event_rec.arr);
  1216. return 0;
  1217. case SNDCTL_MIDI_INFO:
  1218. if (get_user(dev, &((struct midi_info __user *)arg)->device))
  1219. return -EFAULT;
  1220. if (dev < 0 || dev >= max_mididev || !midi_devs[dev])
  1221. return -ENXIO;
  1222. midi_devs[dev]->info.device = dev;
  1223. return copy_to_user(arg, &midi_devs[dev]->info, sizeof(struct midi_info))?-EFAULT:0;
  1224. case SNDCTL_SEQ_THRESHOLD:
  1225. if (get_user(val, p))
  1226. return -EFAULT;
  1227. if (val < 1)
  1228. val = 1;
  1229. if (val >= SEQ_MAX_QUEUE)
  1230. val = SEQ_MAX_QUEUE - 1;
  1231. output_threshold = val;
  1232. return 0;
  1233. case SNDCTL_MIDI_PRETIME:
  1234. if (get_user(val, p))
  1235. return -EFAULT;
  1236. if (val < 0)
  1237. val = 0;
  1238. val = (HZ * val) / 10;
  1239. pre_event_timeout = val;
  1240. break;
  1241. default:
  1242. if (mode == OPEN_READ)
  1243. return -EIO;
  1244. if (!synth_devs[0])
  1245. return -ENXIO;
  1246. if (!(synth_open_mask & (1 << 0)))
  1247. return -ENXIO;
  1248. if (!synth_devs[0]->ioctl)
  1249. return -EINVAL;
  1250. return synth_devs[0]->ioctl(0, cmd, arg);
  1251. }
  1252. return put_user(val, p);
  1253. }
  1254. /* No kernel lock - we're using the global irq lock here */
  1255. unsigned int sequencer_poll(int dev, struct file *file, poll_table * wait)
  1256. {
  1257. unsigned long flags;
  1258. unsigned int mask = 0;
  1259. dev = dev >> 4;
  1260. spin_lock_irqsave(&lock,flags);
  1261. /* input */
  1262. poll_wait(file, &midi_sleeper, wait);
  1263. if (iqlen)
  1264. mask |= POLLIN | POLLRDNORM;
  1265. /* output */
  1266. poll_wait(file, &seq_sleeper, wait);
  1267. if ((SEQ_MAX_QUEUE - qlen) >= output_threshold)
  1268. mask |= POLLOUT | POLLWRNORM;
  1269. spin_unlock_irqrestore(&lock,flags);
  1270. return mask;
  1271. }
  1272. void sequencer_timer(unsigned long dummy)
  1273. {
  1274. seq_startplay();
  1275. }
  1276. EXPORT_SYMBOL(sequencer_timer);
  1277. int note_to_freq(int note_num)
  1278. {
  1279. /*
  1280. * This routine converts a midi note to a frequency (multiplied by 1000)
  1281. */
  1282. int note, octave, note_freq;
  1283. static int notes[] =
  1284. {
  1285. 261632, 277189, 293671, 311132, 329632, 349232,
  1286. 369998, 391998, 415306, 440000, 466162, 493880
  1287. };
  1288. #define BASE_OCTAVE 5
  1289. octave = note_num / 12;
  1290. note = note_num % 12;
  1291. note_freq = notes[note];
  1292. if (octave < BASE_OCTAVE)
  1293. note_freq >>= (BASE_OCTAVE - octave);
  1294. else if (octave > BASE_OCTAVE)
  1295. note_freq <<= (octave - BASE_OCTAVE);
  1296. /*
  1297. * note_freq >>= 1;
  1298. */
  1299. return note_freq;
  1300. }
  1301. EXPORT_SYMBOL(note_to_freq);
  1302. unsigned long compute_finetune(unsigned long base_freq, int bend, int range,
  1303. int vibrato_cents)
  1304. {
  1305. unsigned long amount;
  1306. int negative, semitones, cents, multiplier = 1;
  1307. if (!bend)
  1308. return base_freq;
  1309. if (!range)
  1310. return base_freq;
  1311. if (!base_freq)
  1312. return base_freq;
  1313. if (range >= 8192)
  1314. range = 8192;
  1315. bend = bend * range / 8192; /* Convert to cents */
  1316. bend += vibrato_cents;
  1317. if (!bend)
  1318. return base_freq;
  1319. negative = bend < 0 ? 1 : 0;
  1320. if (bend < 0)
  1321. bend *= -1;
  1322. if (bend > range)
  1323. bend = range;
  1324. /*
  1325. if (bend > 2399)
  1326. bend = 2399;
  1327. */
  1328. while (bend > 2399)
  1329. {
  1330. multiplier *= 4;
  1331. bend -= 2400;
  1332. }
  1333. semitones = bend / 100;
  1334. cents = bend % 100;
  1335. amount = (int) (semitone_tuning[semitones] * multiplier * cent_tuning[cents]) / 10000;
  1336. if (negative)
  1337. return (base_freq * 10000) / amount; /* Bend down */
  1338. else
  1339. return (base_freq * amount) / 10000; /* Bend up */
  1340. }
  1341. EXPORT_SYMBOL(compute_finetune);
  1342. void sequencer_init(void)
  1343. {
  1344. if (sequencer_ok)
  1345. return;
  1346. queue = vmalloc(SEQ_MAX_QUEUE * EV_SZ);
  1347. if (queue == NULL)
  1348. {
  1349. printk(KERN_ERR "sequencer: Can't allocate memory for sequencer output queue\n");
  1350. return;
  1351. }
  1352. iqueue = vmalloc(SEQ_MAX_QUEUE * IEV_SZ);
  1353. if (iqueue == NULL)
  1354. {
  1355. printk(KERN_ERR "sequencer: Can't allocate memory for sequencer input queue\n");
  1356. vfree(queue);
  1357. return;
  1358. }
  1359. sequencer_ok = 1;
  1360. }
  1361. EXPORT_SYMBOL(sequencer_init);
  1362. void sequencer_unload(void)
  1363. {
  1364. vfree(queue);
  1365. vfree(iqueue);
  1366. queue = iqueue = NULL;
  1367. }