mtpav.c 20 KB

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