]>
Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
523f1dce DM |
2 | /* |
3 | * Copyright (c) 2006,2007 Daniel Mack | |
523f1dce DM |
4 | */ |
5 | ||
f1f6b8f6 | 6 | #include <linux/device.h> |
523f1dce | 7 | #include <linux/usb.h> |
5a0e3ad6 | 8 | #include <linux/gfp.h> |
523f1dce | 9 | #include <sound/rawmidi.h> |
e431cf45 | 10 | #include <sound/core.h> |
523f1dce DM |
11 | #include <sound/pcm.h> |
12 | ||
936e7d03 DM |
13 | #include "device.h" |
14 | #include "midi.h" | |
523f1dce | 15 | |
523f1dce DM |
16 | static int snd_usb_caiaq_midi_input_open(struct snd_rawmidi_substream *substream) |
17 | { | |
18 | return 0; | |
19 | } | |
20 | ||
21 | static int snd_usb_caiaq_midi_input_close(struct snd_rawmidi_substream *substream) | |
22 | { | |
23 | return 0; | |
24 | } | |
25 | ||
26 | static void snd_usb_caiaq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up) | |
27 | { | |
1c8470ce | 28 | struct snd_usb_caiaqdev *cdev = substream->rmidi->private_data; |
523f1dce | 29 | |
1c8470ce | 30 | if (!cdev) |
523f1dce | 31 | return; |
9318dce5 | 32 | |
1c8470ce | 33 | cdev->midi_receive_substream = up ? substream : NULL; |
523f1dce DM |
34 | } |
35 | ||
36 | ||
37 | static int snd_usb_caiaq_midi_output_open(struct snd_rawmidi_substream *substream) | |
38 | { | |
39 | return 0; | |
40 | } | |
41 | ||
42 | static int snd_usb_caiaq_midi_output_close(struct snd_rawmidi_substream *substream) | |
43 | { | |
1c8470ce DM |
44 | 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; | |
f3f80a92 | 48 | } |
523f1dce DM |
49 | return 0; |
50 | } | |
51 | ||
1c8470ce | 52 | static void snd_usb_caiaq_midi_send(struct snd_usb_caiaqdev *cdev, |
523f1dce DM |
53 | struct snd_rawmidi_substream *substream) |
54 | { | |
55 | int len, ret; | |
f1f6b8f6 | 56 | struct device *dev = caiaqdev_to_dev(cdev); |
9318dce5 | 57 | |
1c8470ce DM |
58 | 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, | |
f3f80a92 | 61 | EP1_BUFSIZE - 3); |
9318dce5 | 62 | |
523f1dce DM |
63 | if (len <= 0) |
64 | return; | |
9318dce5 | 65 | |
1c8470ce DM |
66 | cdev->midi_out_buf[2] = len; |
67 | cdev->midi_out_urb.transfer_buffer_length = len+3; | |
9318dce5 | 68 | |
1c8470ce | 69 | ret = usb_submit_urb(&cdev->midi_out_urb, GFP_ATOMIC); |
523f1dce | 70 | if (ret < 0) |
f1f6b8f6 DM |
71 | dev_err(dev, |
72 | "snd_usb_caiaq_midi_send(%p): usb_submit_urb() failed," | |
73 | "ret=%d, len=%d\n", substream, ret, len); | |
f3f80a92 | 74 | else |
1c8470ce | 75 | cdev->midi_out_active = 1; |
523f1dce DM |
76 | } |
77 | ||
78 | static void snd_usb_caiaq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up) | |
79 | { | |
1c8470ce | 80 | struct snd_usb_caiaqdev *cdev = substream->rmidi->private_data; |
9318dce5 | 81 | |
f3f80a92 | 82 | if (up) { |
1c8470ce DM |
83 | cdev->midi_out_substream = substream; |
84 | if (!cdev->midi_out_active) | |
85 | snd_usb_caiaq_midi_send(cdev, substream); | |
f3f80a92 | 86 | } else { |
1c8470ce | 87 | cdev->midi_out_substream = NULL; |
523f1dce | 88 | } |
523f1dce DM |
89 | } |
90 | ||
91 | ||
f43e5407 | 92 | static const struct snd_rawmidi_ops snd_usb_caiaq_midi_output = |
523f1dce DM |
93 | { |
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 | ||
f43e5407 | 99 | static const struct snd_rawmidi_ops snd_usb_caiaq_midi_input = |
523f1dce DM |
100 | { |
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 | ||
1c8470ce | 106 | void snd_usb_caiaq_midi_handle_input(struct snd_usb_caiaqdev *cdev, |
523f1dce DM |
107 | int port, const char *buf, int len) |
108 | { | |
1c8470ce | 109 | if (!cdev->midi_receive_substream) |
523f1dce | 110 | return; |
9318dce5 | 111 | |
1c8470ce | 112 | snd_rawmidi_receive(cdev->midi_receive_substream, buf, len); |
523f1dce DM |
113 | } |
114 | ||
599c3e76 | 115 | int snd_usb_caiaq_midi_init(struct snd_usb_caiaqdev *device) |
523f1dce DM |
116 | { |
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 | ||
eaae55da | 128 | strlcpy(rmidi->name, device->product_name, sizeof(rmidi->name)); |
523f1dce DM |
129 | |
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; | |
9318dce5 | 135 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, |
523f1dce DM |
136 | &snd_usb_caiaq_midi_output); |
137 | } | |
138 | ||
139 | if (device->spec.num_midi_in > 0) { | |
140 | rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT; | |
9318dce5 | 141 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, |
523f1dce DM |
142 | &snd_usb_caiaq_midi_input); |
143 | } | |
9318dce5 | 144 | |
523f1dce DM |
145 | device->rmidi = rmidi; |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | void snd_usb_caiaq_midi_output_done(struct urb* urb) | |
151 | { | |
1c8470ce | 152 | struct snd_usb_caiaqdev *cdev = urb->context; |
9318dce5 | 153 | |
1c8470ce | 154 | cdev->midi_out_active = 0; |
523f1dce DM |
155 | if (urb->status != 0) |
156 | return; | |
157 | ||
1c8470ce | 158 | if (!cdev->midi_out_substream) |
523f1dce DM |
159 | return; |
160 | ||
1c8470ce | 161 | snd_usb_caiaq_midi_send(cdev, cdev->midi_out_substream); |
523f1dce | 162 | } |