blob: 512fbb3ee604903c115fab14a130e8adf32bef35 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Mack523f1dc2007-03-26 19:11:24 +02002/*
3 * Copyright (c) 2006,2007 Daniel Mack
Daniel Mack523f1dc2007-03-26 19:11:24 +02004*/
5
Daniel Mackf1f6b8f2013-03-03 20:46:22 +01006#include <linux/device.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +02007#include <linux/usb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/gfp.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +02009#include <sound/rawmidi.h>
Daniel Macke431cf42009-03-28 21:19:49 +010010#include <sound/core.h>
Daniel Mack523f1dc2007-03-26 19:11:24 +020011#include <sound/pcm.h>
12
Daniel Mack936e7d02009-04-01 19:05:39 +020013#include "device.h"
14#include "midi.h"
Daniel Mack523f1dc2007-03-26 19:11:24 +020015
Daniel Mack523f1dc2007-03-26 19:11:24 +020016static int snd_usb_caiaq_midi_input_open(struct snd_rawmidi_substream *substream)
17{
18 return 0;
19}
20
21static int snd_usb_caiaq_midi_input_close(struct snd_rawmidi_substream *substream)
22{
23 return 0;
24}
25
26static void snd_usb_caiaq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
27{
Daniel Mack1c8470c2013-03-03 20:46:21 +010028 struct snd_usb_caiaqdev *cdev = substream->rmidi->private_data;
Daniel Mack523f1dc2007-03-26 19:11:24 +020029
Daniel Mack1c8470c2013-03-03 20:46:21 +010030 if (!cdev)
Daniel Mack523f1dc2007-03-26 19:11:24 +020031 return;
Daniel Mack9318dce2009-06-01 21:36:23 +020032
Daniel Mack1c8470c2013-03-03 20:46:21 +010033 cdev->midi_receive_substream = up ? substream : NULL;
Daniel Mack523f1dc2007-03-26 19:11:24 +020034}
35
36
37static int snd_usb_caiaq_midi_output_open(struct snd_rawmidi_substream *substream)
38{
39 return 0;
40}
41
42static int snd_usb_caiaq_midi_output_close(struct snd_rawmidi_substream *substream)
43{
Daniel Mack1c8470c2013-03-03 20:46:21 +010044 struct snd_usb_caiaqdev *cdev = substream->rmidi->private_data;
45 if (cdev->midi_out_active) {
46 usb_kill_urb(&cdev->midi_out_urb);
47 cdev->midi_out_active = 0;
Takashi Iwaif3f80a92009-01-08 15:32:56 +010048 }
Daniel Mack523f1dc2007-03-26 19:11:24 +020049 return 0;
50}
51
Daniel Mack1c8470c2013-03-03 20:46:21 +010052static void snd_usb_caiaq_midi_send(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +020053 struct snd_rawmidi_substream *substream)
54{
55 int len, ret;
Daniel Mackf1f6b8f2013-03-03 20:46:22 +010056 struct device *dev = caiaqdev_to_dev(cdev);
Daniel Mack9318dce2009-06-01 21:36:23 +020057
Daniel Mack1c8470c2013-03-03 20:46:21 +010058 cdev->midi_out_buf[0] = EP1_CMD_MIDI_WRITE;
59 cdev->midi_out_buf[1] = 0; /* port */
60 len = snd_rawmidi_transmit(substream, cdev->midi_out_buf + 3,
Takashi Iwaif3f80a92009-01-08 15:32:56 +010061 EP1_BUFSIZE - 3);
Daniel Mack9318dce2009-06-01 21:36:23 +020062
Daniel Mack523f1dc2007-03-26 19:11:24 +020063 if (len <= 0)
64 return;
Daniel Mack9318dce2009-06-01 21:36:23 +020065
Daniel Mack1c8470c2013-03-03 20:46:21 +010066 cdev->midi_out_buf[2] = len;
67 cdev->midi_out_urb.transfer_buffer_length = len+3;
Daniel Mack9318dce2009-06-01 21:36:23 +020068
Daniel Mack1c8470c2013-03-03 20:46:21 +010069 ret = usb_submit_urb(&cdev->midi_out_urb, GFP_ATOMIC);
Daniel Mack523f1dc2007-03-26 19:11:24 +020070 if (ret < 0)
Daniel Mackf1f6b8f2013-03-03 20:46:22 +010071 dev_err(dev,
72 "snd_usb_caiaq_midi_send(%p): usb_submit_urb() failed,"
73 "ret=%d, len=%d\n", substream, ret, len);
Takashi Iwaif3f80a92009-01-08 15:32:56 +010074 else
Daniel Mack1c8470c2013-03-03 20:46:21 +010075 cdev->midi_out_active = 1;
Daniel Mack523f1dc2007-03-26 19:11:24 +020076}
77
78static void snd_usb_caiaq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
79{
Daniel Mack1c8470c2013-03-03 20:46:21 +010080 struct snd_usb_caiaqdev *cdev = substream->rmidi->private_data;
Daniel Mack9318dce2009-06-01 21:36:23 +020081
Takashi Iwaif3f80a92009-01-08 15:32:56 +010082 if (up) {
Daniel Mack1c8470c2013-03-03 20:46:21 +010083 cdev->midi_out_substream = substream;
84 if (!cdev->midi_out_active)
85 snd_usb_caiaq_midi_send(cdev, substream);
Takashi Iwaif3f80a92009-01-08 15:32:56 +010086 } else {
Daniel Mack1c8470c2013-03-03 20:46:21 +010087 cdev->midi_out_substream = NULL;
Daniel Mack523f1dc2007-03-26 19:11:24 +020088 }
Daniel Mack523f1dc2007-03-26 19:11:24 +020089}
90
91
Takashi Iwaif43e5402017-01-05 17:30:12 +010092static const struct snd_rawmidi_ops snd_usb_caiaq_midi_output =
Daniel Mack523f1dc2007-03-26 19:11:24 +020093{
94 .open = snd_usb_caiaq_midi_output_open,
95 .close = snd_usb_caiaq_midi_output_close,
96 .trigger = snd_usb_caiaq_midi_output_trigger,
97};
98
Takashi Iwaif43e5402017-01-05 17:30:12 +010099static const struct snd_rawmidi_ops snd_usb_caiaq_midi_input =
Daniel Mack523f1dc2007-03-26 19:11:24 +0200100{
101 .open = snd_usb_caiaq_midi_input_open,
102 .close = snd_usb_caiaq_midi_input_close,
103 .trigger = snd_usb_caiaq_midi_input_trigger,
104};
105
Daniel Mack1c8470c2013-03-03 20:46:21 +0100106void snd_usb_caiaq_midi_handle_input(struct snd_usb_caiaqdev *cdev,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200107 int port, const char *buf, int len)
108{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100109 if (!cdev->midi_receive_substream)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200110 return;
Daniel Mack9318dce2009-06-01 21:36:23 +0200111
Daniel Mack1c8470c2013-03-03 20:46:21 +0100112 snd_rawmidi_receive(cdev->midi_receive_substream, buf, len);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200113}
114
Randy Dunlap599c3e72008-01-16 14:56:04 +0100115int snd_usb_caiaq_midi_init(struct snd_usb_caiaqdev *device)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200116{
117 int ret;
118 struct snd_rawmidi *rmidi;
119
120 ret = snd_rawmidi_new(device->chip.card, device->product_name, 0,
121 device->spec.num_midi_out,
122 device->spec.num_midi_in,
123 &rmidi);
124
125 if (ret < 0)
126 return ret;
127
Takashi Iwaieaae55d2011-02-14 22:45:59 +0100128 strlcpy(rmidi->name, device->product_name, sizeof(rmidi->name));
Daniel Mack523f1dc2007-03-26 19:11:24 +0200129
130 rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX;
131 rmidi->private_data = device;
132
133 if (device->spec.num_midi_out > 0) {
134 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
Daniel Mack9318dce2009-06-01 21:36:23 +0200135 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200136 &snd_usb_caiaq_midi_output);
137 }
138
139 if (device->spec.num_midi_in > 0) {
140 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
Daniel Mack9318dce2009-06-01 21:36:23 +0200141 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
Daniel Mack523f1dc2007-03-26 19:11:24 +0200142 &snd_usb_caiaq_midi_input);
143 }
Daniel Mack9318dce2009-06-01 21:36:23 +0200144
Daniel Mack523f1dc2007-03-26 19:11:24 +0200145 device->rmidi = rmidi;
146
147 return 0;
148}
149
150void snd_usb_caiaq_midi_output_done(struct urb* urb)
151{
Daniel Mack1c8470c2013-03-03 20:46:21 +0100152 struct snd_usb_caiaqdev *cdev = urb->context;
Daniel Mack9318dce2009-06-01 21:36:23 +0200153
Daniel Mack1c8470c2013-03-03 20:46:21 +0100154 cdev->midi_out_active = 0;
Daniel Mack523f1dc2007-03-26 19:11:24 +0200155 if (urb->status != 0)
156 return;
157
Daniel Mack1c8470c2013-03-03 20:46:21 +0100158 if (!cdev->midi_out_substream)
Daniel Mack523f1dc2007-03-26 19:11:24 +0200159 return;
160
Daniel Mack1c8470c2013-03-03 20:46:21 +0100161 snd_usb_caiaq_midi_send(cdev, cdev->midi_out_substream);
Daniel Mack523f1dc2007-03-26 19:11:24 +0200162}