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 | ||
d7e37336 | 130 | if (urb == 0) { |
705ececd MG |
131 | dev_err(line6->ifcdev, "Out of memory\n"); |
132 | return -ENOMEM; | |
133 | } | |
1027f476 | 134 | #ifdef CONFIG_LINE6_USB_DUMP_CTRL |
705ececd MG |
135 | line6_write_hexdump(line6, 'S', data, length); |
136 | #endif | |
137 | ||
d7e37336 | 138 | transfer_buffer = kmalloc(length, GFP_ATOMIC); |
705ececd | 139 | |
d7e37336 | 140 | if (transfer_buffer == 0) { |
705ececd MG |
141 | usb_free_urb(urb); |
142 | dev_err(line6->ifcdev, "Out of memory\n"); | |
143 | return -ENOMEM; | |
144 | } | |
145 | ||
146 | memcpy(transfer_buffer, data, length); | |
d7e37336 GKH |
147 | usb_fill_int_urb(urb, line6->usbdev, |
148 | usb_sndbulkpipe(line6->usbdev, | |
149 | line6->ep_control_write), | |
150 | transfer_buffer, length, midi_sent, line6, | |
151 | line6->interval); | |
705ececd MG |
152 | urb->actual_length = 0; |
153 | retval = usb_submit_urb(urb, GFP_ATOMIC); | |
154 | ||
d7e37336 | 155 | if (retval < 0) { |
705ececd MG |
156 | dev_err(line6->ifcdev, "usb_submit_urb failed\n"); |
157 | usb_free_urb(urb); | |
158 | return -EINVAL; | |
159 | } | |
160 | ||
161 | ++line6->line6midi->num_active_send_urbs; | |
162 | ||
d7e37336 | 163 | switch (line6->usbdev->descriptor.idProduct) { |
705ececd MG |
164 | case LINE6_DEVID_BASSPODXT: |
165 | case LINE6_DEVID_BASSPODXTLIVE: | |
166 | case LINE6_DEVID_BASSPODXTPRO: | |
167 | case LINE6_DEVID_PODXT: | |
168 | case LINE6_DEVID_PODXTLIVE: | |
169 | case LINE6_DEVID_PODXTPRO: | |
170 | case LINE6_DEVID_POCKETPOD: | |
1027f476 MG |
171 | line6_pod_midi_postprocess((struct usb_line6_pod *)line6, data, |
172 | length); | |
705ececd MG |
173 | break; |
174 | ||
e1a164d7 MG |
175 | case LINE6_DEVID_VARIAX: |
176 | break; | |
177 | ||
705ececd MG |
178 | default: |
179 | MISSING_CASE; | |
180 | } | |
181 | ||
182 | return 0; | |
183 | } | |
184 | ||
185 | static int line6_midi_output_open(struct snd_rawmidi_substream *substream) | |
186 | { | |
187 | return 0; | |
188 | } | |
189 | ||
190 | static int line6_midi_output_close(struct snd_rawmidi_substream *substream) | |
191 | { | |
192 | return 0; | |
193 | } | |
194 | ||
d7e37336 GKH |
195 | static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream, |
196 | int up) | |
705ececd MG |
197 | { |
198 | unsigned long flags; | |
e1a164d7 MG |
199 | struct usb_line6 *line6 = |
200 | line6_rawmidi_substream_midi(substream)->line6; | |
705ececd MG |
201 | |
202 | line6->line6midi->substream_transmit = substream; | |
203 | spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags); | |
204 | ||
d7e37336 | 205 | if (line6->line6midi->num_active_send_urbs == 0) |
705ececd MG |
206 | line6_midi_transmit(substream); |
207 | ||
208 | spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags); | |
209 | } | |
210 | ||
211 | static void line6_midi_output_drain(struct snd_rawmidi_substream *substream) | |
212 | { | |
e1a164d7 MG |
213 | struct usb_line6 *line6 = |
214 | line6_rawmidi_substream_midi(substream)->line6; | |
1027f476 | 215 | struct snd_line6_midi *midi = line6->line6midi; |
e1a164d7 MG |
216 | wait_event_interruptible(midi->send_wait, |
217 | midi->num_active_send_urbs == 0); | |
705ececd MG |
218 | } |
219 | ||
220 | static int line6_midi_input_open(struct snd_rawmidi_substream *substream) | |
221 | { | |
222 | return 0; | |
223 | } | |
224 | ||
225 | static int line6_midi_input_close(struct snd_rawmidi_substream *substream) | |
226 | { | |
227 | return 0; | |
228 | } | |
229 | ||
d7e37336 GKH |
230 | static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream, |
231 | int up) | |
705ececd | 232 | { |
e1a164d7 MG |
233 | struct usb_line6 *line6 = |
234 | line6_rawmidi_substream_midi(substream)->line6; | |
705ececd | 235 | |
d7e37336 | 236 | if (up) |
705ececd MG |
237 | line6->line6midi->substream_receive = substream; |
238 | else | |
239 | line6->line6midi->substream_receive = 0; | |
240 | } | |
241 | ||
242 | static struct snd_rawmidi_ops line6_midi_output_ops = { | |
243 | .open = line6_midi_output_open, | |
244 | .close = line6_midi_output_close, | |
245 | .trigger = line6_midi_output_trigger, | |
246 | .drain = line6_midi_output_drain, | |
247 | }; | |
248 | ||
249 | static struct snd_rawmidi_ops line6_midi_input_ops = { | |
250 | .open = line6_midi_input_open, | |
251 | .close = line6_midi_input_close, | |
252 | .trigger = line6_midi_input_trigger, | |
253 | }; | |
254 | ||
255 | /* | |
256 | Cleanup the Line6 MIDI device. | |
257 | */ | |
258 | static void line6_cleanup_midi(struct snd_rawmidi *rmidi) | |
259 | { | |
260 | } | |
261 | ||
262 | /* Create a MIDI device */ | |
263 | static int snd_line6_new_midi(struct snd_line6_midi *line6midi) | |
264 | { | |
265 | struct snd_rawmidi *rmidi; | |
266 | int err; | |
267 | ||
d7e37336 GKH |
268 | err = snd_rawmidi_new(line6midi->line6->card, "Line6 MIDI", 0, 1, 1, |
269 | &rmidi); | |
270 | if (err < 0) | |
705ececd MG |
271 | return err; |
272 | ||
273 | rmidi->private_data = line6midi; | |
274 | rmidi->private_free = line6_cleanup_midi; | |
1027f476 | 275 | strcpy(rmidi->id, line6midi->line6->properties->id); |
705ececd MG |
276 | strcpy(rmidi->name, line6midi->line6->properties->name); |
277 | ||
278 | rmidi->info_flags = | |
e1a164d7 MG |
279 | SNDRV_RAWMIDI_INFO_OUTPUT | |
280 | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX; | |
705ececd | 281 | |
d7e37336 GKH |
282 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, |
283 | &line6_midi_output_ops); | |
284 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, | |
285 | &line6_midi_input_ops); | |
705ececd MG |
286 | return 0; |
287 | } | |
288 | ||
289 | /* | |
290 | "read" request on "midi_mask_transmit" special file. | |
291 | */ | |
77491e52 GKH |
292 | static ssize_t midi_get_midi_mask_transmit(struct device *dev, |
293 | struct device_attribute *attr, | |
294 | char *buf) | |
705ececd MG |
295 | { |
296 | struct usb_interface *interface = to_usb_interface(dev); | |
297 | struct usb_line6 *line6 = usb_get_intfdata(interface); | |
298 | return sprintf(buf, "%d\n", line6->line6midi->midi_mask_transmit); | |
299 | } | |
300 | ||
301 | /* | |
302 | "write" request on "midi_mask" special file. | |
303 | */ | |
77491e52 GKH |
304 | static ssize_t midi_set_midi_mask_transmit(struct device *dev, |
305 | struct device_attribute *attr, | |
306 | const char *buf, size_t count) | |
705ececd MG |
307 | { |
308 | struct usb_interface *interface = to_usb_interface(dev); | |
309 | struct usb_line6 *line6 = usb_get_intfdata(interface); | |
334a33d8 SB |
310 | unsigned long value; |
311 | int ret; | |
312 | ||
313 | ret = strict_strtoul(buf, 10, &value); | |
314 | if (ret) | |
315 | return ret; | |
316 | ||
705ececd MG |
317 | line6->line6midi->midi_mask_transmit = value; |
318 | return count; | |
319 | } | |
320 | ||
321 | /* | |
322 | "read" request on "midi_mask_receive" special file. | |
323 | */ | |
77491e52 GKH |
324 | static ssize_t midi_get_midi_mask_receive(struct device *dev, |
325 | struct device_attribute *attr, | |
326 | char *buf) | |
705ececd MG |
327 | { |
328 | struct usb_interface *interface = to_usb_interface(dev); | |
329 | struct usb_line6 *line6 = usb_get_intfdata(interface); | |
330 | return sprintf(buf, "%d\n", line6->line6midi->midi_mask_receive); | |
331 | } | |
332 | ||
333 | /* | |
334 | "write" request on "midi_mask" special file. | |
335 | */ | |
77491e52 GKH |
336 | static ssize_t midi_set_midi_mask_receive(struct device *dev, |
337 | struct device_attribute *attr, | |
338 | const char *buf, size_t count) | |
705ececd MG |
339 | { |
340 | struct usb_interface *interface = to_usb_interface(dev); | |
341 | struct usb_line6 *line6 = usb_get_intfdata(interface); | |
334a33d8 SB |
342 | unsigned long value; |
343 | int ret; | |
344 | ||
345 | ret = strict_strtoul(buf, 10, &value); | |
346 | if (ret) | |
347 | return ret; | |
348 | ||
705ececd MG |
349 | line6->line6midi->midi_mask_receive = value; |
350 | return count; | |
351 | } | |
352 | ||
a3a972a0 | 353 | static DEVICE_ATTR(midi_mask_transmit, S_IWUSR | S_IRUGO, |
e1a164d7 | 354 | midi_get_midi_mask_transmit, midi_set_midi_mask_transmit); |
a3a972a0 | 355 | static DEVICE_ATTR(midi_mask_receive, S_IWUSR | S_IRUGO, |
e1a164d7 | 356 | midi_get_midi_mask_receive, midi_set_midi_mask_receive); |
705ececd MG |
357 | |
358 | /* MIDI device destructor */ | |
359 | static int snd_line6_midi_free(struct snd_device *device) | |
360 | { | |
361 | struct snd_line6_midi *line6midi = device->device_data; | |
e1a164d7 MG |
362 | device_remove_file(line6midi->line6->ifcdev, |
363 | &dev_attr_midi_mask_transmit); | |
364 | device_remove_file(line6midi->line6->ifcdev, | |
365 | &dev_attr_midi_mask_receive); | |
1027f476 MG |
366 | line6_midibuf_destroy(&line6midi->midibuf_in); |
367 | line6_midibuf_destroy(&line6midi->midibuf_out); | |
705ececd MG |
368 | return 0; |
369 | } | |
370 | ||
371 | /* | |
372 | Initialize the Line6 MIDI subsystem. | |
373 | */ | |
374 | int line6_init_midi(struct usb_line6 *line6) | |
375 | { | |
376 | static struct snd_device_ops midi_ops = { | |
377 | .dev_free = snd_line6_midi_free, | |
378 | }; | |
379 | ||
380 | int err; | |
381 | struct snd_line6_midi *line6midi; | |
382 | ||
027360c5 GKH |
383 | if (!(line6->properties->capabilities & LINE6_BIT_CONTROL)) { |
384 | /* skip MIDI initialization and report success */ | |
385 | return 0; | |
386 | } | |
705ececd MG |
387 | |
388 | line6midi = kzalloc(sizeof(struct snd_line6_midi), GFP_KERNEL); | |
389 | ||
d7e37336 | 390 | if (line6midi == NULL) |
705ececd MG |
391 | return -ENOMEM; |
392 | ||
1027f476 | 393 | err = line6_midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0); |
d7e37336 | 394 | if (err < 0) |
705ececd MG |
395 | return err; |
396 | ||
1027f476 | 397 | err = line6_midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1); |
d7e37336 | 398 | if (err < 0) |
705ececd MG |
399 | return err; |
400 | ||
401 | line6midi->line6 = line6; | |
402 | line6midi->midi_mask_transmit = 1; | |
403 | line6midi->midi_mask_receive = 4; | |
404 | line6->line6midi = line6midi; | |
405 | ||
d7e37336 GKH |
406 | err = snd_device_new(line6->card, SNDRV_DEV_RAWMIDI, line6midi, |
407 | &midi_ops); | |
408 | if (err < 0) | |
705ececd MG |
409 | return err; |
410 | ||
411 | snd_card_set_dev(line6->card, line6->ifcdev); | |
412 | ||
d7e37336 GKH |
413 | err = snd_line6_new_midi(line6midi); |
414 | if (err < 0) | |
705ececd MG |
415 | return err; |
416 | ||
d7e37336 GKH |
417 | err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_transmit); |
418 | if (err < 0) | |
705ececd MG |
419 | return err; |
420 | ||
d7e37336 GKH |
421 | err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_receive); |
422 | if (err < 0) | |
705ececd MG |
423 | return err; |
424 | ||
425 | init_waitqueue_head(&line6midi->send_wait); | |
426 | spin_lock_init(&line6midi->send_urb_lock); | |
427 | spin_lock_init(&line6midi->midi_transmit_lock); | |
428 | return 0; | |
429 | } |