mtpav.c 20 KB

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