mtpav.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. /*
  2. * MOTU Midi Timepiece ALSA Main routines
  3. * Copyright by Michael T. Mayers (c) Jan 09, 2000
  4. * mail: michael@tweakoz.com
  5. * Thanks to John Galbraith
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. *
  22. * This driver is for the 'Mark Of The Unicorn' (MOTU)
  23. * MidiTimePiece AV multiport MIDI interface
  24. *
  25. * IOPORTS
  26. * -------
  27. * 8 MIDI Ins and 8 MIDI outs
  28. * Video Sync In (BNC), Word Sync Out (BNC),
  29. * ADAT Sync Out (DB9)
  30. * SMPTE in/out (1/4")
  31. * 2 programmable pedal/footswitch inputs and 4 programmable MIDI controller knobs.
  32. * Macintosh RS422 serial port
  33. * RS422 "network" port for ganging multiple MTP's
  34. * PC Parallel Port ( which this driver currently uses )
  35. *
  36. * MISC FEATURES
  37. * -------------
  38. * Hardware MIDI routing, merging, and filtering
  39. * MIDI Synchronization to Video, ADAT, SMPTE and other Clock sources
  40. * 128 'scene' memories, recallable from MIDI program change
  41. *
  42. *
  43. * ChangeLog
  44. * Jun 11 2001 Takashi Iwai <tiwai@suse.de>
  45. * - Recoded & debugged
  46. * - Added timer interrupt for midi outputs
  47. * - hwports is between 1 and 8, which specifies the number of hardware ports.
  48. * The three global ports, computer, adat and broadcast ports, are created
  49. * always after h/w and remote ports.
  50. *
  51. */
  52. #include <sound/driver.h>
  53. #include <linux/init.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/slab.h>
  56. #include <linux/ioport.h>
  57. #include <linux/moduleparam.h>
  58. #include <sound/core.h>
  59. #include <sound/initval.h>
  60. #include <sound/rawmidi.h>
  61. #include <linux/delay.h>
  62. #include <asm/io.h>
  63. /*
  64. * globals
  65. */
  66. MODULE_AUTHOR("Michael T. Mayers");
  67. MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
  68. MODULE_LICENSE("GPL");
  69. MODULE_SUPPORTED_DEVICE("{{MOTU,MidiTimePiece AV multiport MIDI}}");
  70. // io resources
  71. #define MTPAV_IOBASE 0x378
  72. #define MTPAV_IRQ 7
  73. #define MTPAV_MAX_PORTS 8
  74. static int index = SNDRV_DEFAULT_IDX1;
  75. static char *id = SNDRV_DEFAULT_STR1;
  76. static long port = MTPAV_IOBASE; /* 0x378, 0x278 */
  77. static int irq = MTPAV_IRQ; /* 7, 5 */
  78. static int hwports = MTPAV_MAX_PORTS; /* use hardware ports 1-8 */
  79. module_param(index, int, 0444);
  80. MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
  81. module_param(id, charp, 0444);
  82. MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
  83. module_param(port, long, 0444);
  84. MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
  85. module_param(irq, int, 0444);
  86. MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
  87. module_param(hwports, int, 0444);
  88. MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
  89. /*
  90. * defines
  91. */
  92. //#define USE_FAKE_MTP // don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
  93. // parallel port usage masks
  94. #define SIGS_BYTE 0x08
  95. #define SIGS_RFD 0x80
  96. #define SIGS_IRQ 0x40
  97. #define SIGS_IN0 0x10
  98. #define SIGS_IN1 0x20
  99. #define SIGC_WRITE 0x04
  100. #define SIGC_READ 0x08
  101. #define SIGC_INTEN 0x10
  102. #define DREG 0
  103. #define SREG 1
  104. #define CREG 2
  105. //
  106. #define MTPAV_MODE_INPUT_OPENED 0x01
  107. #define MTPAV_MODE_OUTPUT_OPENED 0x02
  108. #define MTPAV_MODE_INPUT_TRIGGERED 0x04
  109. #define MTPAV_MODE_OUTPUT_TRIGGERED 0x08
  110. #define NUMPORTS (0x12+1)
  111. /*
  112. */
  113. typedef struct mtpav_port {
  114. u8 number;
  115. u8 hwport;
  116. u8 mode;
  117. u8 running_status;
  118. snd_rawmidi_substream_t *input;
  119. snd_rawmidi_substream_t *output;
  120. } mtpav_port_t;
  121. typedef struct mtpav {
  122. snd_card_t *card;
  123. unsigned long port;
  124. struct resource *res_port;
  125. int irq; /* interrupt (for inputs) */
  126. spinlock_t spinlock;
  127. int share_irq; /* number of accesses to input interrupts */
  128. int istimer; /* number of accesses to timer interrupts */
  129. struct timer_list timer; /* timer interrupts for outputs */
  130. snd_rawmidi_t *rmidi;
  131. int num_ports; /* number of hw ports (1-8) */
  132. mtpav_port_t ports[NUMPORTS]; /* all ports including computer, adat and bc */
  133. u32 inmidiport; /* selected input midi port */
  134. u32 inmidistate; /* during midi command 0xf5 */
  135. u32 outmidihwport; /* selected output midi hw port */
  136. } mtpav_t;
  137. /*
  138. * global instance
  139. * hey, we handle at most only one card..
  140. */
  141. static mtpav_t *mtp_card;
  142. /*
  143. * possible hardware ports (selected by 0xf5 port message)
  144. * 0x00 all ports
  145. * 0x01 .. 0x08 this MTP's ports 1..8
  146. * 0x09 .. 0x10 networked MTP's ports (9..16)
  147. * 0x11 networked MTP's computer port
  148. * 0x63 to ADAT
  149. *
  150. * mappig:
  151. * subdevice 0 - (X-1) ports
  152. * X - (2*X-1) networked ports
  153. * X computer
  154. * X+1 ADAT
  155. * X+2 all ports
  156. *
  157. * where X = chip->num_ports
  158. */
  159. #define MTPAV_PIDX_COMPUTER 0
  160. #define MTPAV_PIDX_ADAT 1
  161. #define MTPAV_PIDX_BROADCAST 2
  162. static int translate_subdevice_to_hwport(mtpav_t *chip, int subdev)
  163. {
  164. if (subdev < 0)
  165. return 0x01; /* invalid - use port 0 as default */
  166. else if (subdev < chip->num_ports)
  167. return subdev + 1; /* single mtp port */
  168. else if (subdev < chip->num_ports * 2)
  169. return subdev - chip->num_ports + 0x09; /* remote port */
  170. else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
  171. return 0x11; /* computer port */
  172. else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
  173. return 0x63; /* ADAT */
  174. return 0; /* all ports */
  175. }
  176. static int translate_hwport_to_subdevice(mtpav_t *chip, int hwport)
  177. {
  178. int p;
  179. if (hwport <= 0x00) /* all ports */
  180. return chip->num_ports + MTPAV_PIDX_BROADCAST;
  181. else if (hwport <= 0x08) { /* single port */
  182. p = hwport - 1;
  183. if (p >= chip->num_ports)
  184. p = 0;
  185. return p;
  186. } else if (hwport <= 0x10) { /* remote port */
  187. p = hwport - 0x09 + chip->num_ports;
  188. if (p >= chip->num_ports * 2)
  189. p = chip->num_ports;
  190. return p;
  191. } else if (hwport == 0x11) /* computer port */
  192. return chip->num_ports + MTPAV_PIDX_COMPUTER;
  193. else /* ADAT */
  194. return chip->num_ports + MTPAV_PIDX_ADAT;
  195. }
  196. /*
  197. */
  198. static u8 snd_mtpav_getreg(mtpav_t *chip, u16 reg)
  199. {
  200. u8 rval = 0;
  201. if (reg == SREG) {
  202. rval = inb(chip->port + SREG);
  203. rval = (rval & 0xf8);
  204. } else if (reg == CREG) {
  205. rval = inb(chip->port + CREG);
  206. rval = (rval & 0x1c);
  207. }
  208. return rval;
  209. }
  210. /*
  211. */
  212. static void snd_mtpav_mputreg(mtpav_t *chip, u16 reg, u8 val)
  213. {
  214. if (reg == DREG) {
  215. outb(val, chip->port + DREG);
  216. } else if (reg == CREG) {
  217. outb(val, chip->port + CREG);
  218. }
  219. }
  220. /*
  221. */
  222. static void snd_mtpav_wait_rfdhi(mtpav_t *chip)
  223. {
  224. int counts = 10000;
  225. u8 sbyte;
  226. sbyte = snd_mtpav_getreg(chip, SREG);
  227. while (!(sbyte & SIGS_RFD) && counts--) {
  228. sbyte = snd_mtpav_getreg(chip, SREG);
  229. udelay(10);
  230. }
  231. }
  232. static void snd_mtpav_send_byte(mtpav_t *chip, u8 byte)
  233. {
  234. u8 tcbyt;
  235. u8 clrwrite;
  236. u8 setwrite;
  237. snd_mtpav_wait_rfdhi(chip);
  238. /////////////////
  239. tcbyt = snd_mtpav_getreg(chip, CREG);
  240. clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
  241. setwrite = tcbyt | SIGC_WRITE;
  242. snd_mtpav_mputreg(chip, DREG, byte);
  243. snd_mtpav_mputreg(chip, CREG, clrwrite); // clear write bit
  244. snd_mtpav_mputreg(chip, CREG, setwrite); // set write bit
  245. }
  246. /*
  247. */
  248. /* call this with spin lock held */
  249. static void snd_mtpav_output_port_write(mtpav_port_t *port,
  250. snd_rawmidi_substream_t *substream)
  251. {
  252. u8 outbyte;
  253. // Get the outbyte first, so we can emulate running status if
  254. // necessary
  255. if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
  256. return;
  257. // send port change command if necessary
  258. if (port->hwport != mtp_card->outmidihwport) {
  259. mtp_card->outmidihwport = port->hwport;
  260. snd_mtpav_send_byte(mtp_card, 0xf5);
  261. snd_mtpav_send_byte(mtp_card, port->hwport);
  262. //snd_printk("new outport: 0x%x\n", (unsigned int) port->hwport);
  263. if (!(outbyte & 0x80) && port->running_status)
  264. snd_mtpav_send_byte(mtp_card, port->running_status);
  265. }
  266. // send data
  267. do {
  268. if (outbyte & 0x80)
  269. port->running_status = outbyte;
  270. snd_mtpav_send_byte(mtp_card, outbyte);
  271. } while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
  272. }
  273. static void snd_mtpav_output_write(snd_rawmidi_substream_t * substream)
  274. {
  275. mtpav_port_t *port = &mtp_card->ports[substream->number];
  276. unsigned long flags;
  277. spin_lock_irqsave(&mtp_card->spinlock, flags);
  278. snd_mtpav_output_port_write(port, substream);
  279. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  280. }
  281. /*
  282. * mtpav control
  283. */
  284. static void snd_mtpav_portscan(mtpav_t *chip) // put mtp into smart routing mode
  285. {
  286. u8 p;
  287. for (p = 0; p < 8; p++) {
  288. snd_mtpav_send_byte(chip, 0xf5);
  289. snd_mtpav_send_byte(chip, p);
  290. snd_mtpav_send_byte(chip, 0xfe);
  291. }
  292. }
  293. /*
  294. */
  295. static int snd_mtpav_input_open(snd_rawmidi_substream_t * substream)
  296. {
  297. unsigned long flags;
  298. mtpav_port_t *portp = &mtp_card->ports[substream->number];
  299. //printk("mtpav port: %d opened\n", (int) substream->number);
  300. spin_lock_irqsave(&mtp_card->spinlock, flags);
  301. portp->mode |= MTPAV_MODE_INPUT_OPENED;
  302. portp->input = substream;
  303. if (mtp_card->share_irq++ == 0)
  304. snd_mtpav_mputreg(mtp_card, CREG, (SIGC_INTEN | SIGC_WRITE)); // enable pport interrupts
  305. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  306. return 0;
  307. }
  308. /*
  309. */
  310. static int snd_mtpav_input_close(snd_rawmidi_substream_t *substream)
  311. {
  312. unsigned long flags;
  313. mtpav_port_t *portp = &mtp_card->ports[substream->number];
  314. //printk("mtpav port: %d closed\n", (int) portp);
  315. spin_lock_irqsave(&mtp_card->spinlock, flags);
  316. portp->mode &= (~MTPAV_MODE_INPUT_OPENED);
  317. portp->input = NULL;
  318. if (--mtp_card->share_irq == 0)
  319. snd_mtpav_mputreg(mtp_card, CREG, 0); // disable pport interrupts
  320. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  321. return 0;
  322. }
  323. /*
  324. */
  325. static void snd_mtpav_input_trigger(snd_rawmidi_substream_t * substream, int up)
  326. {
  327. unsigned long flags;
  328. mtpav_port_t *portp = &mtp_card->ports[substream->number];
  329. spin_lock_irqsave(&mtp_card->spinlock, flags);
  330. if (up)
  331. portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
  332. else
  333. portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
  334. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  335. }
  336. /*
  337. * timer interrupt for outputs
  338. */
  339. static void snd_mtpav_output_timer(unsigned long data)
  340. {
  341. unsigned long flags;
  342. mtpav_t *chip = (mtpav_t *)data;
  343. int p;
  344. spin_lock_irqsave(&chip->spinlock, flags);
  345. /* reprogram timer */
  346. chip->timer.expires = 1 + jiffies;
  347. add_timer(&chip->timer);
  348. /* process each port */
  349. for (p = 0; p <= chip->num_ports * 2 + MTPAV_PIDX_BROADCAST; p++) {
  350. mtpav_port_t *portp = &mtp_card->ports[p];
  351. if ((portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED) && portp->output)
  352. snd_mtpav_output_port_write(portp, portp->output);
  353. }
  354. spin_unlock_irqrestore(&chip->spinlock, flags);
  355. }
  356. /* spinlock held! */
  357. static void snd_mtpav_add_output_timer(mtpav_t *chip)
  358. {
  359. init_timer(&chip->timer);
  360. chip->timer.function = snd_mtpav_output_timer;
  361. chip->timer.data = (unsigned long) mtp_card;
  362. chip->timer.expires = 1 + jiffies;
  363. add_timer(&chip->timer);
  364. }
  365. /* spinlock held! */
  366. static void snd_mtpav_remove_output_timer(mtpav_t *chip)
  367. {
  368. del_timer(&chip->timer);
  369. }
  370. /*
  371. */
  372. static int snd_mtpav_output_open(snd_rawmidi_substream_t * substream)
  373. {
  374. unsigned long flags;
  375. mtpav_port_t *portp = &mtp_card->ports[substream->number];
  376. spin_lock_irqsave(&mtp_card->spinlock, flags);
  377. portp->mode |= MTPAV_MODE_OUTPUT_OPENED;
  378. portp->output = substream;
  379. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  380. return 0;
  381. };
  382. /*
  383. */
  384. static int snd_mtpav_output_close(snd_rawmidi_substream_t * substream)
  385. {
  386. unsigned long flags;
  387. mtpav_port_t *portp = &mtp_card->ports[substream->number];
  388. spin_lock_irqsave(&mtp_card->spinlock, flags);
  389. portp->mode &= (~MTPAV_MODE_OUTPUT_OPENED);
  390. portp->output = NULL;
  391. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  392. return 0;
  393. };
  394. /*
  395. */
  396. static void snd_mtpav_output_trigger(snd_rawmidi_substream_t * substream, int up)
  397. {
  398. unsigned long flags;
  399. mtpav_port_t *portp = &mtp_card->ports[substream->number];
  400. spin_lock_irqsave(&mtp_card->spinlock, flags);
  401. if (up) {
  402. if (! (portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED)) {
  403. if (mtp_card->istimer++ == 0)
  404. snd_mtpav_add_output_timer(mtp_card);
  405. portp->mode |= MTPAV_MODE_OUTPUT_TRIGGERED;
  406. }
  407. } else {
  408. portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
  409. if (--mtp_card->istimer == 0)
  410. snd_mtpav_remove_output_timer(mtp_card);
  411. }
  412. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  413. if (up)
  414. snd_mtpav_output_write(substream);
  415. }
  416. /*
  417. * midi interrupt for inputs
  418. */
  419. static void snd_mtpav_inmidi_process(mtpav_t *mcrd, u8 inbyte)
  420. {
  421. mtpav_port_t *portp;
  422. if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
  423. return;
  424. portp = &mcrd->ports[mcrd->inmidiport];
  425. if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED) {
  426. snd_rawmidi_receive(portp->input, &inbyte, 1);
  427. }
  428. }
  429. static void snd_mtpav_inmidi_h(mtpav_t * mcrd, u8 inbyte)
  430. {
  431. snd_assert(mcrd, return);
  432. if (inbyte >= 0xf8) {
  433. /* real-time midi code */
  434. snd_mtpav_inmidi_process(mcrd, inbyte);
  435. return;
  436. }
  437. if (mcrd->inmidistate == 0) { // awaiting command
  438. if (inbyte == 0xf5) // MTP port #
  439. mcrd->inmidistate = 1;
  440. else
  441. snd_mtpav_inmidi_process(mcrd, inbyte);
  442. } else if (mcrd->inmidistate) {
  443. mcrd->inmidiport = translate_hwport_to_subdevice(mcrd, inbyte);
  444. mcrd->inmidistate = 0;
  445. }
  446. }
  447. static void snd_mtpav_read_bytes(mtpav_t * mcrd)
  448. {
  449. u8 clrread, setread;
  450. u8 mtp_read_byte;
  451. u8 sr, cbyt;
  452. int i;
  453. u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
  454. //printk("snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt);
  455. if (!(sbyt & SIGS_BYTE))
  456. return;
  457. cbyt = snd_mtpav_getreg(mcrd, CREG);
  458. clrread = cbyt & (SIGC_READ ^ 0xff);
  459. setread = cbyt | SIGC_READ;
  460. do {
  461. mtp_read_byte = 0;
  462. for (i = 0; i < 4; i++) {
  463. snd_mtpav_mputreg(mcrd, CREG, setread);
  464. sr = snd_mtpav_getreg(mcrd, SREG);
  465. snd_mtpav_mputreg(mcrd, CREG, clrread);
  466. sr &= SIGS_IN0 | SIGS_IN1;
  467. sr >>= 4;
  468. mtp_read_byte |= sr << (i * 2);
  469. }
  470. snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
  471. sbyt = snd_mtpav_getreg(mcrd, SREG);
  472. } while (sbyt & SIGS_BYTE);
  473. }
  474. static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id, struct pt_regs *regs)
  475. {
  476. mtpav_t *mcard = dev_id;
  477. //printk("irqh()\n");
  478. spin_lock(&mcard->spinlock);
  479. snd_mtpav_read_bytes(mcard);
  480. spin_unlock(&mcard->spinlock);
  481. return IRQ_HANDLED;
  482. }
  483. /*
  484. * get ISA resources
  485. */
  486. static int snd_mtpav_get_ISA(mtpav_t * mcard)
  487. {
  488. if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
  489. snd_printk("MTVAP port 0x%lx is busy\n", port);
  490. return -EBUSY;
  491. }
  492. mcard->port = port;
  493. if (request_irq(irq, snd_mtpav_irqh, SA_INTERRUPT, "MOTU MTPAV", (void *)mcard)) {
  494. snd_printk("MTVAP IRQ %d busy\n", irq);
  495. return -EBUSY;
  496. }
  497. mcard->irq = irq;
  498. return 0;
  499. }
  500. /*
  501. */
  502. static snd_rawmidi_ops_t snd_mtpav_output = {
  503. .open = snd_mtpav_output_open,
  504. .close = snd_mtpav_output_close,
  505. .trigger = snd_mtpav_output_trigger,
  506. };
  507. static snd_rawmidi_ops_t snd_mtpav_input = {
  508. .open = snd_mtpav_input_open,
  509. .close = snd_mtpav_input_close,
  510. .trigger = snd_mtpav_input_trigger,
  511. };
  512. /*
  513. * get RAWMIDI resources
  514. */
  515. static void snd_mtpav_set_name(mtpav_t *chip, snd_rawmidi_substream_t *substream)
  516. {
  517. if (substream->number >= 0 && substream->number < chip->num_ports)
  518. sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
  519. else if (substream->number >= 8 && substream->number < chip->num_ports * 2)
  520. sprintf(substream->name, "MTP remote %d", (substream->number % chip->num_ports) + 1);
  521. else if (substream->number == chip->num_ports * 2)
  522. strcpy(substream->name, "MTP computer");
  523. else if (substream->number == chip->num_ports * 2 + 1)
  524. strcpy(substream->name, "MTP ADAT");
  525. else
  526. strcpy(substream->name, "MTP broadcast");
  527. }
  528. static int snd_mtpav_get_RAWMIDI(mtpav_t * mcard)
  529. {
  530. int rval = 0;
  531. snd_rawmidi_t *rawmidi;
  532. snd_rawmidi_substream_t *substream;
  533. struct list_head *list;
  534. //printk("entering snd_mtpav_get_RAWMIDI\n");
  535. if (hwports < 1)
  536. mcard->num_ports = 1;
  537. else if (hwports > 8)
  538. mcard->num_ports = 8;
  539. else
  540. mcard->num_ports = hwports;
  541. if ((rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
  542. mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
  543. mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
  544. &mcard->rmidi)) < 0)
  545. return rval;
  546. rawmidi = mcard->rmidi;
  547. list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
  548. substream = list_entry(list, snd_rawmidi_substream_t, list);
  549. snd_mtpav_set_name(mcard, substream);
  550. substream->ops = &snd_mtpav_input;
  551. }
  552. list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
  553. substream = list_entry(list, snd_rawmidi_substream_t, list);
  554. snd_mtpav_set_name(mcard, substream);
  555. substream->ops = &snd_mtpav_output;
  556. mcard->ports[substream->number].hwport = translate_subdevice_to_hwport(mcard, substream->number);
  557. }
  558. rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
  559. SNDRV_RAWMIDI_INFO_DUPLEX;
  560. sprintf(rawmidi->name, "MTP AV MIDI");
  561. //printk("exiting snd_mtpav_get_RAWMIDI() \n");
  562. return 0;
  563. }
  564. /*
  565. */
  566. static mtpav_t *new_mtpav(void)
  567. {
  568. mtpav_t *ncrd = kcalloc(1, sizeof(*ncrd), GFP_KERNEL);
  569. if (ncrd != NULL) {
  570. spin_lock_init(&ncrd->spinlock);
  571. init_timer(&ncrd->timer);
  572. ncrd->card = NULL;
  573. ncrd->irq = -1;
  574. ncrd->share_irq = 0;
  575. ncrd->inmidiport = 0xffffffff;
  576. ncrd->inmidistate = 0;
  577. ncrd->outmidihwport = 0xffffffff;
  578. }
  579. return ncrd;
  580. }
  581. /*
  582. */
  583. static void free_mtpav(mtpav_t * crd)
  584. {
  585. unsigned long flags;
  586. spin_lock_irqsave(&crd->spinlock, flags);
  587. if (crd->istimer > 0)
  588. snd_mtpav_remove_output_timer(crd);
  589. spin_unlock_irqrestore(&crd->spinlock, flags);
  590. if (crd->irq >= 0)
  591. free_irq(crd->irq, (void *)crd);
  592. if (crd->res_port) {
  593. release_resource(crd->res_port);
  594. kfree_nocheck(crd->res_port);
  595. }
  596. kfree(crd);
  597. }
  598. /*
  599. */
  600. static int __init alsa_card_mtpav_init(void)
  601. {
  602. int err = 0;
  603. char longname_buffer[80];
  604. mtp_card = new_mtpav();
  605. if (mtp_card == NULL)
  606. return -ENOMEM;
  607. mtp_card->card = snd_card_new(index, id, THIS_MODULE, 0);
  608. if (mtp_card->card == NULL) {
  609. free_mtpav(mtp_card);
  610. return -ENOMEM;
  611. }
  612. err = snd_mtpav_get_ISA(mtp_card);
  613. //printk("snd_mtpav_get_ISA returned: %d\n", err);
  614. if (err < 0)
  615. goto __error;
  616. strcpy(mtp_card->card->driver, "MTPAV");
  617. strcpy(mtp_card->card->shortname, "MTPAV on parallel port");
  618. memset(longname_buffer, 0, sizeof(longname_buffer));
  619. sprintf(longname_buffer, "MTPAV on parallel port at");
  620. err = snd_mtpav_get_RAWMIDI(mtp_card);
  621. //snd_printk("snd_mtapv_get_RAWMIDI returned: %d\n", err);
  622. if (err < 0)
  623. goto __error;
  624. err = snd_card_register(mtp_card->card); // don't snd_card_register until AFTER all cards reources done!
  625. //printk("snd_card_register returned %d\n", err);
  626. if (err < 0)
  627. goto __error;
  628. snd_mtpav_portscan(mtp_card);
  629. printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
  630. return 0;
  631. __error:
  632. snd_card_free(mtp_card->card);
  633. free_mtpav(mtp_card);
  634. return err;
  635. }
  636. /*
  637. */
  638. static void __exit alsa_card_mtpav_exit(void)
  639. {
  640. if (mtp_card == NULL)
  641. return;
  642. if (mtp_card->card)
  643. snd_card_free(mtp_card->card);
  644. free_mtpav(mtp_card);
  645. }
  646. /*
  647. */
  648. module_init(alsa_card_mtpav_init)
  649. module_exit(alsa_card_mtpav_exit)