Commit | Line | Data |
---|---|---|
705ececd | 1 | /* |
e1a164d7 | 2 | * Line6 Linux USB driver - 0.9.1beta |
705ececd | 3 | * |
1027f476 | 4 | * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at) |
705ececd MG |
5 | * |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation, version 2. | |
9 | * | |
10 | */ | |
11 | ||
5a0e3ad6 | 12 | #include <linux/slab.h> |
1027f476 | 13 | #include <linux/usb.h> |
705ececd MG |
14 | #include <sound/core.h> |
15 | #include <sound/rawmidi.h> | |
16 | ||
17 | #include "audio.h" | |
1027f476 | 18 | #include "driver.h" |
705ececd MG |
19 | #include "midi.h" |
20 | #include "pod.h" | |
21 | #include "usbdefs.h" | |
22 | ||
d7e37336 GKH |
23 | #define line6_rawmidi_substream_midi(substream) \ |
24 | ((struct snd_line6_midi *)((substream)->rmidi->private_data)) | |
705ececd | 25 | |
d7e37336 GKH |
26 | static int send_midi_async(struct usb_line6 *line6, unsigned char *data, |
27 | int length); | |
705ececd | 28 | |
705ececd MG |
29 | /* |
30 | Pass data received via USB to MIDI. | |
31 | */ | |
d7e37336 GKH |
32 | void line6_midi_receive(struct usb_line6 *line6, unsigned char *data, |
33 | int length) | |
705ececd | 34 | { |
d7e37336 GKH |
35 | if (line6->line6midi->substream_receive) |
36 | snd_rawmidi_receive(line6->line6midi->substream_receive, | |
37 | data, length); | |
705ececd MG |
38 | } |
39 | ||
40 | /* | |
41 | Read data from MIDI buffer and transmit them via USB. | |
42 | */ | |
43 | static void line6_midi_transmit(struct snd_rawmidi_substream *substream) | |
44 | { | |
e1a164d7 MG |
45 | struct usb_line6 *line6 = |
46 | line6_rawmidi_substream_midi(substream)->line6; | |
705ececd MG |
47 | struct snd_line6_midi *line6midi = line6->line6midi; |
48 | struct MidiBuffer *mb = &line6midi->midibuf_out; | |
49 | unsigned long flags; | |
50 | unsigned char chunk[line6->max_packet_size]; | |
51 | int req, done; | |
52 | ||
53 | spin_lock_irqsave(&line6->line6midi->midi_transmit_lock, flags); | |
54 | ||
d7e37336 | 55 | for (;;) { |
1027f476 | 56 | req = min(line6_midibuf_bytes_free(mb), line6->max_packet_size); |
705ececd MG |
57 | done = snd_rawmidi_transmit_peek(substream, chunk, req); |
58 | ||
d7e37336 | 59 | if (done == 0) |
705ececd MG |
60 | break; |
61 | ||
1027f476 | 62 | #ifdef CONFIG_LINE6_USB_DUMP_MIDI |
705ececd MG |
63 | line6_write_hexdump(line6, 's', chunk, done); |
64 | #endif | |
1027f476 | 65 | line6_midibuf_write(mb, chunk, done); |
705ececd MG |
66 | snd_rawmidi_transmit_ack(substream, done); |
67 | } | |
68 | ||
d7e37336 | 69 | for (;;) { |
1027f476 | 70 | done = line6_midibuf_read(mb, chunk, line6->max_packet_size); |
705ececd | 71 | |
d7e37336 | 72 | if (done == 0) |
705ececd MG |
73 | break; |
74 | ||
e1a164d7 MG |
75 | if (line6_midibuf_skip_message |
76 | (mb, line6midi->midi_mask_transmit)) | |
705ececd MG |
77 | continue; |
78 | ||
79 | send_midi_async(line6, chunk, done); | |
80 | } | |
81 | ||
82 | spin_unlock_irqrestore(&line6->line6midi->midi_transmit_lock, flags); | |
83 | } | |
84 | ||
85 | /* | |
86 | Notification of completion of MIDI transmission. | |
87 | */ | |
0c7ab158 | 88 | static void midi_sent(struct urb *urb) |
705ececd MG |
89 | { |
90 | unsigned long flags; | |
91 | int status; | |
92 | int num; | |
93 | struct usb_line6 *line6 = (struct usb_line6 *)urb->context; | |
94 | ||
95 | status = urb->status; | |
96 | kfree(urb->transfer_buffer); | |
97 | usb_free_urb(urb); | |
98 | ||
d7e37336 | 99 | if (status == -ESHUTDOWN) |
705ececd MG |
100 | return; |
101 | ||
102 | spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags); | |
103 | num = --line6->line6midi->num_active_send_urbs; | |
104 | ||
d7e37336 | 105 | if (num == 0) { |
705ececd MG |
106 | line6_midi_transmit(line6->line6midi->substream_transmit); |
107 | num = line6->line6midi->num_active_send_urbs; | |
108 | } | |
109 | ||
d7e37336 | 110 | if (num == 0) |
1027f476 | 111 | wake_up(&line6->line6midi->send_wait); |
705ececd MG |
112 | |
113 | spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags); | |
114 | } | |
115 | ||
116 | /* | |
117 | Send an asynchronous MIDI message. | |
118 | Assumes that line6->line6midi->send_urb_lock is held | |
119 | (i.e., this function is serialized). | |
120 | */ | |
d7e37336 GKH |
121 | static int send_midi_async(struct usb_line6 *line6, unsigned char *data, |
122 | int length) | |
705ececd MG |
123 | { |
124 | struct urb *urb; | |
125 | int retval; | |
126 | unsigned char *transfer_buffer; | |
127 | ||
128 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
129 | ||
597a1e7c | 130 | if (urb == NULL) { |
705ececd MG |
131 | dev_err(line6->ifcdev, "Out of memory\n"); |
132 | return -ENOMEM; | |
133 | } | |
705ececd | 134 | |
cff86387 | 135 | transfer_buffer = kmemdup(data, length, GFP_ATOMIC); |
705ececd | 136 | |
597a1e7c | 137 | if (transfer_buffer == NULL) { |
705ececd MG |
138 | usb_free_urb(urb); |
139 | dev_err(line6->ifcdev, "Out of memory\n"); | |
140 | return -ENOMEM; | |
141 | } | |
142 | ||
d7e37336 GKH |
143 | usb_fill_int_urb(urb, line6->usbdev, |
144 | usb_sndbulkpipe(line6->usbdev, | |
145 | line6->ep_control_write), | |
146 | transfer_buffer, length, midi_sent, line6, | |
147 | line6->interval); | |
705ececd MG |
148 | urb->actual_length = 0; |
149 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
150 | ||
d7e37336 | 151 | if (retval < 0) { |
705ececd MG |
152 | dev_err(line6->ifcdev, "usb_submit_urb failed\n"); |
153 | usb_free_urb(urb); | |
154 | return -EINVAL; | |
155 | } | |
156 | ||
157 | ++line6->line6midi->num_active_send_urbs; | |
158 | ||
d7e37336 | 159 | switch (line6->usbdev->descriptor.idProduct) { |
705ececd MG |
160 | case LINE6_DEVID_BASSPODXT: |
161 | case LINE6_DEVID_BASSPODXTLIVE: | |
162 | case LINE6_DEVID_BASSPODXTPRO: | |
163 | case LINE6_DEVID_PODXT: | |
164 | case LINE6_DEVID_PODXTLIVE: | |
165 | case LINE6_DEVID_PODXTPRO: | |
166 | case LINE6_DEVID_POCKETPOD: | |
1027f476 MG |
167 | line6_pod_midi_postprocess((struct usb_line6_pod *)line6, data, |
168 | length); | |
705ececd MG |
169 | break; |
170 | ||
e1a164d7 | 171 | case LINE6_DEVID_VARIAX: |
1dc403ff | 172 | case LINE6_DEVID_PODHD300: |
4c6fb5fc | 173 | case LINE6_DEVID_PODHD500: |
e1a164d7 MG |
174 | break; |
175 | ||
705ececd MG |
176 | default: |
177 | MISSING_CASE; | |
178 | } | |
179 | ||
180 | return 0; | |
181 | } | |
182 | ||
183 | static int line6_midi_output_open(struct snd_rawmidi_substream *substream) | |
184 | { | |
185 | return 0; | |
186 | } | |
187 | ||
188 | static int line6_midi_output_close(struct snd_rawmidi_substream *substream) | |
189 | { | |
190 | return 0; | |
191 | } | |
192 | ||
d7e37336 GKH |
193 | static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream, |
194 | int up) | |
705ececd MG |
195 | { |
196 | unsigned long flags; | |
e1a164d7 MG |
197 | struct usb_line6 *line6 = |
198 | line6_rawmidi_substream_midi(substream)->line6; | |
705ececd MG |
199 | |
200 | line6->line6midi->substream_transmit = substream; | |
201 | spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags); | |
202 | ||
d7e37336 | 203 | if (line6->line6midi->num_active_send_urbs == 0) |
705ececd MG |
204 | line6_midi_transmit(substream); |
205 | ||
206 | spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags); | |
207 | } | |
208 | ||
209 | static void line6_midi_output_drain(struct snd_rawmidi_substream *substream) | |
210 | { | |
e1a164d7 MG |
211 | struct usb_line6 *line6 = |
212 | line6_rawmidi_substream_midi(substream)->line6; | |
1027f476 | 213 | struct snd_line6_midi *midi = line6->line6midi; |
e1a164d7 MG |
214 | wait_event_interruptible(midi->send_wait, |
215 | midi->num_active_send_urbs == 0); | |
705ececd MG |
216 | } |
217 | ||
218 | static int line6_midi_input_open(struct snd_rawmidi_substream *substream) | |
219 | { | |
220 | return 0; | |
221 | } | |
222 | ||
223 | static int line6_midi_input_close(struct snd_rawmidi_substream *substream) | |
224 | { | |
225 | return 0; | |
226 | } | |
227 | ||
d7e37336 GKH |
228 | static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream, |
229 | int up) | |
705ececd | 230 | { |
e1a164d7 MG |
231 | struct usb_line6 *line6 = |
232 | line6_rawmidi_substream_midi(substream)->line6; | |
705ececd | 233 | |
d7e37336 | 234 | if (up) |
705ececd MG |
235 | line6->line6midi->substream_receive = substream; |
236 | else | |
237 | line6->line6midi->substream_receive = 0; | |
238 | } | |
239 | ||
240 | static struct snd_rawmidi_ops line6_midi_output_ops = { | |
241 | .open = line6_midi_output_open, | |
242 | .close = line6_midi_output_close, | |
243 | .trigger = line6_midi_output_trigger, | |
244 | .drain = line6_midi_output_drain, | |
245 | }; | |
246 | ||
247 | static struct snd_rawmidi_ops line6_midi_input_ops = { | |
248 | .open = line6_midi_input_open, | |
249 | .close = line6_midi_input_close, | |
250 | .trigger = line6_midi_input_trigger, | |
251 | }; | |
252 | ||
253 | /* | |
254 | Cleanup the Line6 MIDI device. | |
255 | */ | |
256 | static void line6_cleanup_midi(struct snd_rawmidi *rmidi) | |
257 | { | |
258 | } | |
259 | ||
260 | /* Create a MIDI device */ | |
261 | static int snd_line6_new_midi(struct snd_line6_midi *line6midi) | |
262 | { | |
263 | struct snd_rawmidi *rmidi; | |
264 | int err; | |
265 | ||
d7e37336 GKH |
266 | err = snd_rawmidi_new(line6midi->line6->card, "Line6 MIDI", 0, 1, 1, |
267 | &rmidi); | |
268 | if (err < 0) | |
705ececd MG |
269 | return err; |
270 | ||
271 | rmidi->private_data = line6midi; | |
272 | rmidi->private_free = line6_cleanup_midi; | |
1027f476 | 273 | strcpy(rmidi->id, line6midi->line6->properties->id); |
705ececd MG |
274 | strcpy(rmidi->name, line6midi->line6->properties->name); |
275 | ||
276 | rmidi->info_flags = | |
e1a164d7 MG |
277 | SNDRV_RAWMIDI_INFO_OUTPUT | |
278 | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX; | |
705ececd | 279 | |
d7e37336 GKH |
280 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, |
281 | &line6_midi_output_ops); | |
282 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, | |
283 | &line6_midi_input_ops); | |
705ececd MG |
284 | return 0; |
285 | } | |
286 | ||
287 | /* | |
288 | "read" request on "midi_mask_transmit" special file. | |
289 | */ | |
77491e52 GKH |
290 | static ssize_t midi_get_midi_mask_transmit(struct device *dev, |
291 | struct device_attribute *attr, | |
292 | char *buf) | |
705ececd MG |
293 | { |
294 | struct usb_interface *interface = to_usb_interface(dev); | |
295 | struct usb_line6 *line6 = usb_get_intfdata(interface); | |
296 | return sprintf(buf, "%d\n", line6->line6midi->midi_mask_transmit); | |
297 | } | |
298 | ||
299 | /* | |
300 | "write" request on "midi_mask" special file. | |
301 | */ | |
77491e52 GKH |
302 | static ssize_t midi_set_midi_mask_transmit(struct device *dev, |
303 | struct device_attribute *attr, | |
304 | const char *buf, size_t count) | |
705ececd MG |
305 | { |
306 | struct usb_interface *interface = to_usb_interface(dev); | |
307 | struct usb_line6 *line6 = usb_get_intfdata(interface); | |
4d85fae0 | 308 | unsigned short value; |
334a33d8 SB |
309 | int ret; |
310 | ||
4d85fae0 | 311 | ret = kstrtou16(buf, 10, &value); |
334a33d8 SB |
312 | if (ret) |
313 | return ret; | |
314 | ||
705ececd MG |
315 | line6->line6midi->midi_mask_transmit = value; |
316 | return count; | |
317 | } | |
318 | ||
319 | /* | |
320 | "read" request on "midi_mask_receive" special file. | |
321 | */ | |
77491e52 GKH |
322 | static ssize_t midi_get_midi_mask_receive(struct device *dev, |
323 | struct device_attribute *attr, | |
324 | char *buf) | |
705ececd MG |
325 | { |
326 | struct usb_interface *interface = to_usb_interface(dev); | |
327 | struct usb_line6 *line6 = usb_get_intfdata(interface); | |
328 | return sprintf(buf, "%d\n", line6->line6midi->midi_mask_receive); | |
329 | } | |
330 | ||
331 | /* | |
332 | "write" request on "midi_mask" special file. | |
333 | */ | |
77491e52 GKH |
334 | static ssize_t midi_set_midi_mask_receive(struct device *dev, |
335 | struct device_attribute *attr, | |
336 | const char *buf, size_t count) | |
705ececd MG |
337 | { |
338 | struct usb_interface *interface = to_usb_interface(dev); | |
339 | struct usb_line6 *line6 = usb_get_intfdata(interface); | |
251c3948 | 340 | unsigned short value; |
334a33d8 SB |
341 | int ret; |
342 | ||
251c3948 | 343 | ret = kstrtou16(buf, 10, &value); |
334a33d8 SB |
344 | if (ret) |
345 | return ret; | |
346 | ||
705ececd MG |
347 | line6->line6midi->midi_mask_receive = value; |
348 | return count; | |
349 | } | |
350 | ||
a3a972a0 | 351 | static DEVICE_ATTR(midi_mask_transmit, S_IWUSR | S_IRUGO, |
e1a164d7 | 352 | midi_get_midi_mask_transmit, midi_set_midi_mask_transmit); |
a3a972a0 | 353 | static DEVICE_ATTR(midi_mask_receive, S_IWUSR | S_IRUGO, |
e1a164d7 | 354 | midi_get_midi_mask_receive, midi_set_midi_mask_receive); |
705ececd MG |
355 | |
356 | /* MIDI device destructor */ | |
357 | static int snd_line6_midi_free(struct snd_device *device) | |
358 | { | |
359 | struct snd_line6_midi *line6midi = device->device_data; | |
e1a164d7 MG |
360 | device_remove_file(line6midi->line6->ifcdev, |
361 | &dev_attr_midi_mask_transmit); | |
362 | device_remove_file(line6midi->line6->ifcdev, | |
363 | &dev_attr_midi_mask_receive); | |
1027f476 MG |
364 | line6_midibuf_destroy(&line6midi->midibuf_in); |
365 | line6_midibuf_destroy(&line6midi->midibuf_out); | |
705ececd MG |
366 | return 0; |
367 | } | |
368 | ||
369 | /* | |
370 | Initialize the Line6 MIDI subsystem. | |
371 | */ | |
372 | int line6_init_midi(struct usb_line6 *line6) | |
373 | { | |
374 | static struct snd_device_ops midi_ops = { | |
375 | .dev_free = snd_line6_midi_free, | |
376 | }; | |
377 | ||
378 | int err; | |
379 | struct snd_line6_midi *line6midi; | |
380 | ||
027360c5 GKH |
381 | if (!(line6->properties->capabilities & LINE6_BIT_CONTROL)) { |
382 | /* skip MIDI initialization and report success */ | |
383 | return 0; | |
384 | } | |
705ececd MG |
385 | |
386 | line6midi = kzalloc(sizeof(struct snd_line6_midi), GFP_KERNEL); | |
387 | ||
d7e37336 | 388 | if (line6midi == NULL) |
705ececd MG |
389 | return -ENOMEM; |
390 | ||
1027f476 | 391 | err = line6_midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0); |
982d6ab5 JJ |
392 | if (err < 0) { |
393 | kfree(line6midi); | |
705ececd | 394 | return err; |
982d6ab5 | 395 | } |
705ececd | 396 | |
1027f476 | 397 | err = line6_midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1); |
982d6ab5 JJ |
398 | if (err < 0) { |
399 | kfree(line6midi->midibuf_in.buf); | |
400 | kfree(line6midi); | |
705ececd | 401 | return err; |
982d6ab5 | 402 | } |
705ececd MG |
403 | |
404 | line6midi->line6 = line6; | |
4c6fb5fc | 405 | |
bc8fa144 | 406 | switch (line6->product) { |
4c6fb5fc MG |
407 | case LINE6_DEVID_PODHD300: |
408 | case LINE6_DEVID_PODHD500: | |
409 | line6midi->midi_mask_transmit = 1; | |
410 | line6midi->midi_mask_receive = 1; | |
411 | break; | |
412 | ||
413 | default: | |
414 | line6midi->midi_mask_transmit = 1; | |
415 | line6midi->midi_mask_receive = 4; | |
416 | } | |
417 | ||
705ececd MG |
418 | line6->line6midi = line6midi; |
419 | ||
d7e37336 GKH |
420 | err = snd_device_new(line6->card, SNDRV_DEV_RAWMIDI, line6midi, |
421 | &midi_ops); | |
422 | if (err < 0) | |
705ececd MG |
423 | return err; |
424 | ||
425 | snd_card_set_dev(line6->card, line6->ifcdev); | |
426 | ||
d7e37336 GKH |
427 | err = snd_line6_new_midi(line6midi); |
428 | if (err < 0) | |
705ececd MG |
429 | return err; |
430 | ||
d7e37336 GKH |
431 | err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_transmit); |
432 | if (err < 0) | |
705ececd MG |
433 | return err; |
434 | ||
d7e37336 GKH |
435 | err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_receive); |
436 | if (err < 0) | |
705ececd MG |
437 | return err; |
438 | ||
439 | init_waitqueue_head(&line6midi->send_wait); | |
440 | spin_lock_init(&line6midi->send_urb_lock); | |
441 | spin_lock_init(&line6midi->midi_transmit_lock); | |
442 | return 0; | |
443 | } |