mtpav.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 <linux/init.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/err.h>
  55. #include <linux/platform_device.h>
  56. #include <linux/ioport.h>
  57. #include <linux/io.h>
  58. #include <linux/moduleparam.h>
  59. #include <sound/core.h>
  60. #include <sound/initval.h>
  61. #include <sound/rawmidi.h>
  62. #include <linux/delay.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. static struct platform_device *device;
  90. /*
  91. * defines
  92. */
  93. //#define USE_FAKE_MTP // don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
  94. // parallel port usage masks
  95. #define SIGS_BYTE 0x08
  96. #define SIGS_RFD 0x80
  97. #define SIGS_IRQ 0x40
  98. #define SIGS_IN0 0x10
  99. #define SIGS_IN1 0x20
  100. #define SIGC_WRITE 0x04
  101. #define SIGC_READ 0x08
  102. #define SIGC_INTEN 0x10
  103. #define DREG 0
  104. #define SREG 1
  105. #define CREG 2
  106. //
  107. #define MTPAV_MODE_INPUT_OPENED 0x01
  108. #define MTPAV_MODE_OUTPUT_OPENED 0x02
  109. #define MTPAV_MODE_INPUT_TRIGGERED 0x04
  110. #define MTPAV_MODE_OUTPUT_TRIGGERED 0x08
  111. #define NUMPORTS (0x12+1)
  112. /*
  113. */
  114. struct mtpav_port {
  115. u8 number;
  116. u8 hwport;
  117. u8 mode;
  118. u8 running_status;
  119. struct snd_rawmidi_substream *input;
  120. struct snd_rawmidi_substream *output;
  121. };
  122. struct mtpav {
  123. struct snd_card *card;
  124. unsigned long port;
  125. struct resource *res_port;
  126. int irq; /* interrupt (for inputs) */
  127. spinlock_t spinlock;
  128. int share_irq; /* number of accesses to input interrupts */
  129. int istimer; /* number of accesses to timer interrupts */
  130. struct timer_list timer; /* timer interrupts for outputs */
  131. struct snd_rawmidi *rmidi;
  132. int num_ports; /* number of hw ports (1-8) */
  133. struct mtpav_port ports[NUMPORTS]; /* all ports including computer, adat and bc */
  134. u32 inmidiport; /* selected input midi port */
  135. u32 inmidistate; /* during midi command 0xf5 */
  136. u32 outmidihwport; /* selected output midi hw port */
  137. };
  138. /*
  139. * possible hardware ports (selected by 0xf5 port message)
  140. * 0x00 all ports
  141. * 0x01 .. 0x08 this MTP's ports 1..8
  142. * 0x09 .. 0x10 networked MTP's ports (9..16)
  143. * 0x11 networked MTP's computer port
  144. * 0x63 to ADAT
  145. *
  146. * mappig:
  147. * subdevice 0 - (X-1) ports
  148. * X - (2*X-1) networked ports
  149. * X computer
  150. * X+1 ADAT
  151. * X+2 all ports
  152. *
  153. * where X = chip->num_ports
  154. */
  155. #define MTPAV_PIDX_COMPUTER 0
  156. #define MTPAV_PIDX_ADAT 1
  157. #define MTPAV_PIDX_BROADCAST 2
  158. static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
  159. {
  160. if (subdev < 0)
  161. return 0x01; /* invalid - use port 0 as default */
  162. else if (subdev < chip->num_ports)
  163. return subdev + 1; /* single mtp port */
  164. else if (subdev < chip->num_ports * 2)
  165. return subdev - chip->num_ports + 0x09; /* remote port */
  166. else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
  167. return 0x11; /* computer port */
  168. else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
  169. return 0x63; /* ADAT */
  170. return 0; /* all ports */
  171. }
  172. static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
  173. {
  174. int p;
  175. if (hwport <= 0x00) /* all ports */
  176. return chip->num_ports + MTPAV_PIDX_BROADCAST;
  177. else if (hwport <= 0x08) { /* single port */
  178. p = hwport - 1;
  179. if (p >= chip->num_ports)
  180. p = 0;
  181. return p;
  182. } else if (hwport <= 0x10) { /* remote port */
  183. p = hwport - 0x09 + chip->num_ports;
  184. if (p >= chip->num_ports * 2)
  185. p = chip->num_ports;
  186. return p;
  187. } else if (hwport == 0x11) /* computer port */
  188. return chip->num_ports + MTPAV_PIDX_COMPUTER;
  189. else /* ADAT */
  190. return chip->num_ports + MTPAV_PIDX_ADAT;
  191. }
  192. /*
  193. */
  194. static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
  195. {
  196. u8 rval = 0;
  197. if (reg == SREG) {
  198. rval = inb(chip->port + SREG);
  199. rval = (rval & 0xf8);
  200. } else if (reg == CREG) {
  201. rval = inb(chip->port + CREG);
  202. rval = (rval & 0x1c);
  203. }
  204. return rval;
  205. }
  206. /*
  207. */
  208. static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
  209. {
  210. if (reg == DREG || reg == CREG)
  211. outb(val, chip->port + reg);
  212. }
  213. /*
  214. */
  215. static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
  216. {
  217. int counts = 10000;
  218. u8 sbyte;
  219. sbyte = snd_mtpav_getreg(chip, SREG);
  220. while (!(sbyte & SIGS_RFD) && counts--) {
  221. sbyte = snd_mtpav_getreg(chip, SREG);
  222. udelay(10);
  223. }
  224. }
  225. static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
  226. {
  227. u8 tcbyt;
  228. u8 clrwrite;
  229. u8 setwrite;
  230. snd_mtpav_wait_rfdhi(chip);
  231. /////////////////
  232. tcbyt = snd_mtpav_getreg(chip, CREG);
  233. clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
  234. setwrite = tcbyt | SIGC_WRITE;
  235. snd_mtpav_mputreg(chip, DREG, byte);
  236. snd_mtpav_mputreg(chip, CREG, clrwrite); // clear write bit
  237. snd_mtpav_mputreg(chip, CREG, setwrite); // set write bit
  238. }
  239. /*
  240. */
  241. /* call this with spin lock held */
  242. static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
  243. struct mtpav_port *portp,
  244. struct snd_rawmidi_substream *substream)
  245. {
  246. u8 outbyte;
  247. // Get the outbyte first, so we can emulate running status if
  248. // necessary
  249. if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
  250. return;
  251. // send port change command if necessary
  252. if (portp->hwport != mtp_card->outmidihwport) {
  253. mtp_card->outmidihwport = portp->hwport;
  254. snd_mtpav_send_byte(mtp_card, 0xf5);
  255. snd_mtpav_send_byte(mtp_card, portp->hwport);
  256. /*
  257. snd_printk(KERN_DEBUG "new outport: 0x%x\n",
  258. (unsigned int) portp->hwport);
  259. */
  260. if (!(outbyte & 0x80) && portp->running_status)
  261. snd_mtpav_send_byte(mtp_card, portp->running_status);
  262. }
  263. // send data
  264. do {
  265. if (outbyte & 0x80)
  266. portp->running_status = outbyte;
  267. snd_mtpav_send_byte(mtp_card, outbyte);
  268. } while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
  269. }
  270. static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
  271. {
  272. struct mtpav *mtp_card = substream->rmidi->private_data;
  273. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  274. unsigned long flags;
  275. spin_lock_irqsave(&mtp_card->spinlock, flags);
  276. snd_mtpav_output_port_write(mtp_card, portp, substream);
  277. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  278. }
  279. /*
  280. * mtpav control
  281. */
  282. static void snd_mtpav_portscan(struct mtpav *chip) // put mtp into smart routing mode
  283. {
  284. u8 p;
  285. for (p = 0; p < 8; p++) {
  286. snd_mtpav_send_byte(chip, 0xf5);
  287. snd_mtpav_send_byte(chip, p);
  288. snd_mtpav_send_byte(chip, 0xfe);
  289. }
  290. }
  291. /*
  292. */
  293. static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
  294. {
  295. struct mtpav *mtp_card = substream->rmidi->private_data;
  296. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  297. unsigned long flags;
  298. spin_lock_irqsave(&mtp_card->spinlock, flags);
  299. portp->mode |= MTPAV_MODE_INPUT_OPENED;
  300. portp->input = substream;
  301. if (mtp_card->share_irq++ == 0)
  302. snd_mtpav_mputreg(mtp_card, CREG, (SIGC_INTEN | SIGC_WRITE)); // enable pport interrupts
  303. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  304. return 0;
  305. }
  306. /*
  307. */
  308. static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
  309. {
  310. struct mtpav *mtp_card = substream->rmidi->private_data;
  311. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  312. unsigned long flags;
  313. spin_lock_irqsave(&mtp_card->spinlock, flags);
  314. portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
  315. portp->input = NULL;
  316. if (--mtp_card->share_irq == 0)
  317. snd_mtpav_mputreg(mtp_card, CREG, 0); // disable pport interrupts
  318. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  319. return 0;
  320. }
  321. /*
  322. */
  323. static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
  324. {
  325. struct mtpav *mtp_card = substream->rmidi->private_data;
  326. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  327. unsigned long flags;
  328. spin_lock_irqsave(&mtp_card->spinlock, flags);
  329. if (up)
  330. portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
  331. else
  332. portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
  333. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  334. }
  335. /*
  336. * timer interrupt for outputs
  337. */
  338. static void snd_mtpav_output_timer(unsigned long data)
  339. {
  340. unsigned long flags;
  341. struct mtpav *chip = (struct mtpav *)data;
  342. int p;
  343. spin_lock_irqsave(&chip->spinlock, flags);
  344. /* reprogram timer */
  345. chip->timer.expires = 1 + jiffies;
  346. add_timer(&chip->timer);
  347. /* process each port */
  348. for (p = 0; p <= chip->num_ports * 2 + MTPAV_PIDX_BROADCAST; p++) {
  349. struct mtpav_port *portp = &chip->ports[p];
  350. if ((portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED) && portp->output)
  351. snd_mtpav_output_port_write(chip, portp, portp->output);
  352. }
  353. spin_unlock_irqrestore(&chip->spinlock, flags);
  354. }
  355. /* spinlock held! */
  356. static void snd_mtpav_add_output_timer(struct mtpav *chip)
  357. {
  358. chip->timer.expires = 1 + jiffies;
  359. add_timer(&chip->timer);
  360. }
  361. /* spinlock held! */
  362. static void snd_mtpav_remove_output_timer(struct mtpav *chip)
  363. {
  364. del_timer(&chip->timer);
  365. }
  366. /*
  367. */
  368. static int snd_mtpav_output_open(struct snd_rawmidi_substream *substream)
  369. {
  370. struct mtpav *mtp_card = substream->rmidi->private_data;
  371. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  372. unsigned long flags;
  373. spin_lock_irqsave(&mtp_card->spinlock, flags);
  374. portp->mode |= MTPAV_MODE_OUTPUT_OPENED;
  375. portp->output = substream;
  376. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  377. return 0;
  378. };
  379. /*
  380. */
  381. static int snd_mtpav_output_close(struct snd_rawmidi_substream *substream)
  382. {
  383. struct mtpav *mtp_card = substream->rmidi->private_data;
  384. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  385. unsigned long flags;
  386. spin_lock_irqsave(&mtp_card->spinlock, flags);
  387. portp->mode &= ~MTPAV_MODE_OUTPUT_OPENED;
  388. portp->output = NULL;
  389. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  390. return 0;
  391. };
  392. /*
  393. */
  394. static void snd_mtpav_output_trigger(struct snd_rawmidi_substream *substream, int up)
  395. {
  396. struct mtpav *mtp_card = substream->rmidi->private_data;
  397. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  398. unsigned long flags;
  399. spin_lock_irqsave(&mtp_card->spinlock, flags);
  400. if (up) {
  401. if (! (portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED)) {
  402. if (mtp_card->istimer++ == 0)
  403. snd_mtpav_add_output_timer(mtp_card);
  404. portp->mode |= MTPAV_MODE_OUTPUT_TRIGGERED;
  405. }
  406. } else {
  407. portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
  408. if (--mtp_card->istimer == 0)
  409. snd_mtpav_remove_output_timer(mtp_card);
  410. }
  411. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  412. if (up)
  413. snd_mtpav_output_write(substream);
  414. }
  415. /*
  416. * midi interrupt for inputs
  417. */
  418. static void snd_mtpav_inmidi_process(struct mtpav *mcrd, u8 inbyte)
  419. {
  420. struct mtpav_port *portp;
  421. if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
  422. return;
  423. portp = &mcrd->ports[mcrd->inmidiport];
  424. if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED)
  425. snd_rawmidi_receive(portp->input, &inbyte, 1);
  426. }
  427. static void snd_mtpav_inmidi_h(struct mtpav *mcrd, u8 inbyte)
  428. {
  429. if (inbyte >= 0xf8) {
  430. /* real-time midi code */
  431. snd_mtpav_inmidi_process(mcrd, inbyte);
  432. return;
  433. }
  434. if (mcrd->inmidistate == 0) { // awaiting command
  435. if (inbyte == 0xf5) // MTP port #
  436. mcrd->inmidistate = 1;
  437. else
  438. snd_mtpav_inmidi_process(mcrd, inbyte);
  439. } else if (mcrd->inmidistate) {
  440. mcrd->inmidiport = translate_hwport_to_subdevice(mcrd, inbyte);
  441. mcrd->inmidistate = 0;
  442. }
  443. }
  444. static void snd_mtpav_read_bytes(struct mtpav *mcrd)
  445. {
  446. u8 clrread, setread;
  447. u8 mtp_read_byte;
  448. u8 sr, cbyt;
  449. int i;
  450. u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
  451. /* printk(KERN_DEBUG "snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt); */
  452. if (!(sbyt & SIGS_BYTE))
  453. return;
  454. cbyt = snd_mtpav_getreg(mcrd, CREG);
  455. clrread = cbyt & (SIGC_READ ^ 0xff);
  456. setread = cbyt | SIGC_READ;
  457. do {
  458. mtp_read_byte = 0;
  459. for (i = 0; i < 4; i++) {
  460. snd_mtpav_mputreg(mcrd, CREG, setread);
  461. sr = snd_mtpav_getreg(mcrd, SREG);
  462. snd_mtpav_mputreg(mcrd, CREG, clrread);
  463. sr &= SIGS_IN0 | SIGS_IN1;
  464. sr >>= 4;
  465. mtp_read_byte |= sr << (i * 2);
  466. }
  467. snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
  468. sbyt = snd_mtpav_getreg(mcrd, SREG);
  469. } while (sbyt & SIGS_BYTE);
  470. }
  471. static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id)
  472. {
  473. struct mtpav *mcard = dev_id;
  474. spin_lock(&mcard->spinlock);
  475. snd_mtpav_read_bytes(mcard);
  476. spin_unlock(&mcard->spinlock);
  477. return IRQ_HANDLED;
  478. }
  479. /*
  480. * get ISA resources
  481. */
  482. static int __devinit snd_mtpav_get_ISA(struct mtpav * mcard)
  483. {
  484. if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
  485. snd_printk(KERN_ERR "MTVAP port 0x%lx is busy\n", port);
  486. return -EBUSY;
  487. }
  488. mcard->port = port;
  489. if (request_irq(irq, snd_mtpav_irqh, IRQF_DISABLED, "MOTU MTPAV", mcard)) {
  490. snd_printk(KERN_ERR "MTVAP IRQ %d busy\n", irq);
  491. return -EBUSY;
  492. }
  493. mcard->irq = irq;
  494. return 0;
  495. }
  496. /*
  497. */
  498. static struct snd_rawmidi_ops snd_mtpav_output = {
  499. .open = snd_mtpav_output_open,
  500. .close = snd_mtpav_output_close,
  501. .trigger = snd_mtpav_output_trigger,
  502. };
  503. static struct snd_rawmidi_ops snd_mtpav_input = {
  504. .open = snd_mtpav_input_open,
  505. .close = snd_mtpav_input_close,
  506. .trigger = snd_mtpav_input_trigger,
  507. };
  508. /*
  509. * get RAWMIDI resources
  510. */
  511. static void __devinit snd_mtpav_set_name(struct mtpav *chip,
  512. struct snd_rawmidi_substream *substream)
  513. {
  514. if (substream->number >= 0 && substream->number < chip->num_ports)
  515. sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
  516. else if (substream->number >= 8 && substream->number < chip->num_ports * 2)
  517. sprintf(substream->name, "MTP remote %d", (substream->number % chip->num_ports) + 1);
  518. else if (substream->number == chip->num_ports * 2)
  519. strcpy(substream->name, "MTP computer");
  520. else if (substream->number == chip->num_ports * 2 + 1)
  521. strcpy(substream->name, "MTP ADAT");
  522. else
  523. strcpy(substream->name, "MTP broadcast");
  524. }
  525. static int __devinit snd_mtpav_get_RAWMIDI(struct mtpav *mcard)
  526. {
  527. int rval;
  528. struct snd_rawmidi *rawmidi;
  529. struct snd_rawmidi_substream *substream;
  530. struct list_head *list;
  531. if (hwports < 1)
  532. hwports = 1;
  533. else if (hwports > 8)
  534. hwports = 8;
  535. mcard->num_ports = hwports;
  536. if ((rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
  537. mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
  538. mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
  539. &mcard->rmidi)) < 0)
  540. return rval;
  541. rawmidi = mcard->rmidi;
  542. rawmidi->private_data = mcard;
  543. list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
  544. substream = list_entry(list, struct snd_rawmidi_substream, list);
  545. snd_mtpav_set_name(mcard, substream);
  546. substream->ops = &snd_mtpav_input;
  547. }
  548. list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
  549. substream = list_entry(list, struct snd_rawmidi_substream, list);
  550. snd_mtpav_set_name(mcard, substream);
  551. substream->ops = &snd_mtpav_output;
  552. mcard->ports[substream->number].hwport = translate_subdevice_to_hwport(mcard, substream->number);
  553. }
  554. rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
  555. SNDRV_RAWMIDI_INFO_DUPLEX;
  556. sprintf(rawmidi->name, "MTP AV MIDI");
  557. return 0;
  558. }
  559. /*
  560. */
  561. static void snd_mtpav_free(struct snd_card *card)
  562. {
  563. struct mtpav *crd = card->private_data;
  564. unsigned long flags;
  565. spin_lock_irqsave(&crd->spinlock, flags);
  566. if (crd->istimer > 0)
  567. snd_mtpav_remove_output_timer(crd);
  568. spin_unlock_irqrestore(&crd->spinlock, flags);
  569. if (crd->irq >= 0)
  570. free_irq(crd->irq, (void *)crd);
  571. release_and_free_resource(crd->res_port);
  572. }
  573. /*
  574. */
  575. static int __devinit snd_mtpav_probe(struct platform_device *dev)
  576. {
  577. struct snd_card *card;
  578. int err;
  579. struct mtpav *mtp_card;
  580. err = snd_card_create(index, id, THIS_MODULE, sizeof(*mtp_card), &card);
  581. if (err < 0)
  582. return err;
  583. mtp_card = card->private_data;
  584. spin_lock_init(&mtp_card->spinlock);
  585. init_timer(&mtp_card->timer);
  586. mtp_card->card = card;
  587. mtp_card->irq = -1;
  588. mtp_card->share_irq = 0;
  589. mtp_card->inmidistate = 0;
  590. mtp_card->outmidihwport = 0xffffffff;
  591. init_timer(&mtp_card->timer);
  592. mtp_card->timer.function = snd_mtpav_output_timer;
  593. mtp_card->timer.data = (unsigned long) mtp_card;
  594. card->private_free = snd_mtpav_free;
  595. err = snd_mtpav_get_RAWMIDI(mtp_card);
  596. if (err < 0)
  597. goto __error;
  598. mtp_card->inmidiport = mtp_card->num_ports + MTPAV_PIDX_BROADCAST;
  599. err = snd_mtpav_get_ISA(mtp_card);
  600. if (err < 0)
  601. goto __error;
  602. strcpy(card->driver, "MTPAV");
  603. strcpy(card->shortname, "MTPAV on parallel port");
  604. snprintf(card->longname, sizeof(card->longname),
  605. "MTPAV on parallel port at 0x%lx", port);
  606. snd_mtpav_portscan(mtp_card);
  607. snd_card_set_dev(card, &dev->dev);
  608. err = snd_card_register(mtp_card->card);
  609. if (err < 0)
  610. goto __error;
  611. platform_set_drvdata(dev, card);
  612. printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
  613. return 0;
  614. __error:
  615. snd_card_free(card);
  616. return err;
  617. }
  618. static int __devexit snd_mtpav_remove(struct platform_device *devptr)
  619. {
  620. snd_card_free(platform_get_drvdata(devptr));
  621. platform_set_drvdata(devptr, NULL);
  622. return 0;
  623. }
  624. #define SND_MTPAV_DRIVER "snd_mtpav"
  625. static struct platform_driver snd_mtpav_driver = {
  626. .probe = snd_mtpav_probe,
  627. .remove = __devexit_p(snd_mtpav_remove),
  628. .driver = {
  629. .name = SND_MTPAV_DRIVER
  630. },
  631. };
  632. static int __init alsa_card_mtpav_init(void)
  633. {
  634. int err;
  635. if ((err = platform_driver_register(&snd_mtpav_driver)) < 0)
  636. return err;
  637. device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
  638. if (!IS_ERR(device)) {
  639. if (platform_get_drvdata(device))
  640. return 0;
  641. platform_device_unregister(device);
  642. err = -ENODEV;
  643. } else
  644. err = PTR_ERR(device);
  645. platform_driver_unregister(&snd_mtpav_driver);
  646. return err;
  647. }
  648. static void __exit alsa_card_mtpav_exit(void)
  649. {
  650. platform_device_unregister(device);
  651. platform_driver_unregister(&snd_mtpav_driver);
  652. }
  653. module_init(alsa_card_mtpav_init)
  654. module_exit(alsa_card_mtpav_exit)