Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2006,2007 Daniel Mack |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 6 | #include <linux/device.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 7 | #include <linux/usb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 8 | #include <linux/gfp.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 9 | #include <sound/rawmidi.h> |
Daniel Mack | e431cf4 | 2009-03-28 21:19:49 +0100 | [diff] [blame] | 10 | #include <sound/core.h> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 11 | #include <sound/pcm.h> |
| 12 | |
Daniel Mack | 936e7d0 | 2009-04-01 19:05:39 +0200 | [diff] [blame] | 13 | #include "device.h" |
| 14 | #include "midi.h" |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 15 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 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 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 28 | struct snd_usb_caiaqdev *cdev = substream->rmidi->private_data; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 29 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 30 | if (!cdev) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 31 | return; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 32 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 33 | cdev->midi_receive_substream = up ? substream : NULL; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 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 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 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; |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame] | 48 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 52 | static void snd_usb_caiaq_midi_send(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 53 | struct snd_rawmidi_substream *substream) |
| 54 | { |
| 55 | int len, ret; |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 56 | struct device *dev = caiaqdev_to_dev(cdev); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 57 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 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, |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame] | 61 | EP1_BUFSIZE - 3); |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 62 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 63 | if (len <= 0) |
| 64 | return; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 65 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 66 | cdev->midi_out_buf[2] = len; |
| 67 | cdev->midi_out_urb.transfer_buffer_length = len+3; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 68 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 69 | ret = usb_submit_urb(&cdev->midi_out_urb, GFP_ATOMIC); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 70 | if (ret < 0) |
Daniel Mack | f1f6b8f | 2013-03-03 20:46:22 +0100 | [diff] [blame] | 71 | dev_err(dev, |
| 72 | "snd_usb_caiaq_midi_send(%p): usb_submit_urb() failed," |
| 73 | "ret=%d, len=%d\n", substream, ret, len); |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame] | 74 | else |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 75 | cdev->midi_out_active = 1; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void snd_usb_caiaq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up) |
| 79 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 80 | struct snd_usb_caiaqdev *cdev = substream->rmidi->private_data; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 81 | |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame] | 82 | if (up) { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 83 | cdev->midi_out_substream = substream; |
| 84 | if (!cdev->midi_out_active) |
| 85 | snd_usb_caiaq_midi_send(cdev, substream); |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame] | 86 | } else { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 87 | cdev->midi_out_substream = NULL; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 88 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
Takashi Iwai | f43e540 | 2017-01-05 17:30:12 +0100 | [diff] [blame] | 92 | static const struct snd_rawmidi_ops snd_usb_caiaq_midi_output = |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 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 | |
Takashi Iwai | f43e540 | 2017-01-05 17:30:12 +0100 | [diff] [blame] | 99 | static const struct snd_rawmidi_ops snd_usb_caiaq_midi_input = |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 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 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 106 | void snd_usb_caiaq_midi_handle_input(struct snd_usb_caiaqdev *cdev, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 107 | int port, const char *buf, int len) |
| 108 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 109 | if (!cdev->midi_receive_substream) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 110 | return; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 111 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 112 | snd_rawmidi_receive(cdev->midi_receive_substream, buf, len); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Randy Dunlap | 599c3e7 | 2008-01-16 14:56:04 +0100 | [diff] [blame] | 115 | int snd_usb_caiaq_midi_init(struct snd_usb_caiaqdev *device) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 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 | |
Takashi Iwai | eaae55d | 2011-02-14 22:45:59 +0100 | [diff] [blame] | 128 | strlcpy(rmidi->name, device->product_name, sizeof(rmidi->name)); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 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; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 135 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 136 | &snd_usb_caiaq_midi_output); |
| 137 | } |
| 138 | |
| 139 | if (device->spec.num_midi_in > 0) { |
| 140 | rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 141 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 142 | &snd_usb_caiaq_midi_input); |
| 143 | } |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 144 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 145 | device->rmidi = rmidi; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | void snd_usb_caiaq_midi_output_done(struct urb* urb) |
| 151 | { |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 152 | struct snd_usb_caiaqdev *cdev = urb->context; |
Daniel Mack | 9318dce | 2009-06-01 21:36:23 +0200 | [diff] [blame] | 153 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 154 | cdev->midi_out_active = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 155 | if (urb->status != 0) |
| 156 | return; |
| 157 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 158 | if (!cdev->midi_out_substream) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 159 | return; |
| 160 | |
Daniel Mack | 1c8470c | 2013-03-03 20:46:21 +0100 | [diff] [blame] | 161 | snd_usb_caiaq_midi_send(cdev, cdev->midi_out_substream); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 162 | } |