mtpav.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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/module.h>
  55. #include <linux/err.h>
  56. #include <linux/platform_device.h>
  57. #include <linux/ioport.h>
  58. #include <linux/io.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. /*
  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. /*
  258. snd_printk(KERN_DEBUG "new outport: 0x%x\n",
  259. (unsigned int) portp->hwport);
  260. */
  261. if (!(outbyte & 0x80) && portp->running_status)
  262. snd_mtpav_send_byte(mtp_card, portp->running_status);
  263. }
  264. // send data
  265. do {
  266. if (outbyte & 0x80)
  267. portp->running_status = outbyte;
  268. snd_mtpav_send_byte(mtp_card, outbyte);
  269. } while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
  270. }
  271. static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
  272. {
  273. struct mtpav *mtp_card = substream->rmidi->private_data;
  274. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  275. unsigned long flags;
  276. spin_lock_irqsave(&mtp_card->spinlock, flags);
  277. snd_mtpav_output_port_write(mtp_card, portp, substream);
  278. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  279. }
  280. /*
  281. * mtpav control
  282. */
  283. static void snd_mtpav_portscan(struct mtpav *chip) // put mtp into smart routing mode
  284. {
  285. u8 p;
  286. for (p = 0; p < 8; p++) {
  287. snd_mtpav_send_byte(chip, 0xf5);
  288. snd_mtpav_send_byte(chip, p);
  289. snd_mtpav_send_byte(chip, 0xfe);
  290. }
  291. }
  292. /*
  293. */
  294. static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
  295. {
  296. struct mtpav *mtp_card = substream->rmidi->private_data;
  297. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  298. unsigned long flags;
  299. spin_lock_irqsave(&mtp_card->spinlock, flags);
  300. portp->mode |= MTPAV_MODE_INPUT_OPENED;
  301. portp->input = substream;
  302. if (mtp_card->share_irq++ == 0)
  303. snd_mtpav_mputreg(mtp_card, CREG, (SIGC_INTEN | SIGC_WRITE)); // enable pport interrupts
  304. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  305. return 0;
  306. }
  307. /*
  308. */
  309. static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
  310. {
  311. struct mtpav *mtp_card = substream->rmidi->private_data;
  312. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  313. unsigned long flags;
  314. spin_lock_irqsave(&mtp_card->spinlock, flags);
  315. portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
  316. portp->input = NULL;
  317. if (--mtp_card->share_irq == 0)
  318. snd_mtpav_mputreg(mtp_card, CREG, 0); // disable pport interrupts
  319. spin_unlock_irqrestore(&mtp_card->spinlock, flags);
  320. return 0;
  321. }
  322. /*
  323. */
  324. static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
  325. {
  326. struct mtpav *mtp_card = substream->rmidi->private_data;
  327. struct mtpav_port *portp = &mtp_card->ports[substream->number];
  328. unsigned long flags;
  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. struct mtpav *chip = (struct mtpav *)data;
  343. int p;
  344. spin_lock_irqsave(&chip->spinlock, flags);
  345. /* reprogram timer */
  346. mod_timer(&chip->timer, 1 + jiffies);
  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. mod_timer(&chip->timer, 1 + jiffies);
  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(KERN_DEBUG "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 snd_mtpav_get_ISA(struct mtpav *mcard)
  482. {
  483. if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
  484. snd_printk(KERN_ERR "MTVAP port 0x%lx is busy\n", port);
  485. return -EBUSY;
  486. }
  487. mcard->port = port;
  488. if (request_irq(irq, snd_mtpav_irqh, 0, "MOTU MTPAV", mcard)) {
  489. snd_printk(KERN_ERR "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 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 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 snd_mtpav_probe(struct platform_device *dev)
  575. {
  576. struct snd_card *card;
  577. int err;
  578. struct mtpav *mtp_card;
  579. err = snd_card_new(&dev->dev, index, id, THIS_MODULE,
  580. 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. mtp_card->card = card;
  586. mtp_card->irq = -1;
  587. mtp_card->share_irq = 0;
  588. mtp_card->inmidistate = 0;
  589. mtp_card->outmidihwport = 0xffffffff;
  590. setup_timer(&mtp_card->timer, snd_mtpav_output_timer,
  591. (unsigned long) mtp_card);
  592. card->private_free = snd_mtpav_free;
  593. err = snd_mtpav_get_RAWMIDI(mtp_card);
  594. if (err < 0)
  595. goto __error;
  596. mtp_card->inmidiport = mtp_card->num_ports + MTPAV_PIDX_BROADCAST;
  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. err = snd_card_register(mtp_card->card);
  606. if (err < 0)
  607. goto __error;
  608. platform_set_drvdata(dev, card);
  609. printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
  610. return 0;
  611. __error:
  612. snd_card_free(card);
  613. return err;
  614. }
  615. static int snd_mtpav_remove(struct platform_device *devptr)
  616. {
  617. snd_card_free(platform_get_drvdata(devptr));
  618. return 0;
  619. }
  620. #define SND_MTPAV_DRIVER "snd_mtpav"
  621. static struct platform_driver snd_mtpav_driver = {
  622. .probe = snd_mtpav_probe,
  623. .remove = snd_mtpav_remove,
  624. .driver = {
  625. .name = SND_MTPAV_DRIVER,
  626. },
  627. };
  628. static int __init alsa_card_mtpav_init(void)
  629. {
  630. int err;
  631. if ((err = platform_driver_register(&snd_mtpav_driver)) < 0)
  632. return err;
  633. device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
  634. if (!IS_ERR(device)) {
  635. if (platform_get_drvdata(device))
  636. return 0;
  637. platform_device_unregister(device);
  638. err = -ENODEV;
  639. } else
  640. err = PTR_ERR(device);
  641. platform_driver_unregister(&snd_mtpav_driver);
  642. return err;
  643. }
  644. static void __exit alsa_card_mtpav_exit(void)
  645. {
  646. platform_device_unregister(device);
  647. platform_driver_unregister(&snd_mtpav_driver);
  648. }
  649. module_init(alsa_card_mtpav_init)
  650. module_exit(alsa_card_mtpav_exit)