diff options
Diffstat (limited to 'sound/usb/usbmidi.c')
-rw-r--r-- | sound/usb/usbmidi.c | 111 |
1 files changed, 89 insertions, 22 deletions
diff --git a/sound/usb/usbmidi.c b/sound/usb/usbmidi.c index 5778a9b725ec..93dedde3c428 100644 --- a/sound/usb/usbmidi.c +++ b/sound/usb/usbmidi.c | |||
@@ -44,6 +44,7 @@ | |||
44 | #include <linux/string.h> | 44 | #include <linux/string.h> |
45 | #include <linux/init.h> | 45 | #include <linux/init.h> |
46 | #include <linux/slab.h> | 46 | #include <linux/slab.h> |
47 | #include <linux/timer.h> | ||
47 | #include <linux/usb.h> | 48 | #include <linux/usb.h> |
48 | #include <sound/core.h> | 49 | #include <sound/core.h> |
49 | #include <sound/minors.h> | 50 | #include <sound/minors.h> |
@@ -56,6 +57,12 @@ | |||
56 | */ | 57 | */ |
57 | /* #define DUMP_PACKETS */ | 58 | /* #define DUMP_PACKETS */ |
58 | 59 | ||
60 | /* | ||
61 | * how long to wait after some USB errors, so that khubd can disconnect() us | ||
62 | * without too many spurious errors | ||
63 | */ | ||
64 | #define ERROR_DELAY_JIFFIES (HZ / 10) | ||
65 | |||
59 | 66 | ||
60 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); | 67 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); |
61 | MODULE_DESCRIPTION("USB Audio/MIDI helper module"); | 68 | MODULE_DESCRIPTION("USB Audio/MIDI helper module"); |
@@ -100,6 +107,7 @@ struct snd_usb_midi { | |||
100 | snd_rawmidi_t* rmidi; | 107 | snd_rawmidi_t* rmidi; |
101 | struct usb_protocol_ops* usb_protocol_ops; | 108 | struct usb_protocol_ops* usb_protocol_ops; |
102 | struct list_head list; | 109 | struct list_head list; |
110 | struct timer_list error_timer; | ||
103 | 111 | ||
104 | struct snd_usb_midi_endpoint { | 112 | struct snd_usb_midi_endpoint { |
105 | snd_usb_midi_out_endpoint_t *out; | 113 | snd_usb_midi_out_endpoint_t *out; |
@@ -141,7 +149,8 @@ struct snd_usb_midi_in_endpoint { | |||
141 | struct usbmidi_in_port { | 149 | struct usbmidi_in_port { |
142 | snd_rawmidi_substream_t* substream; | 150 | snd_rawmidi_substream_t* substream; |
143 | } ports[0x10]; | 151 | } ports[0x10]; |
144 | int seen_f5; | 152 | u8 seen_f5; |
153 | u8 error_resubmit; | ||
145 | int current_port; | 154 | int current_port; |
146 | }; | 155 | }; |
147 | 156 | ||
@@ -167,14 +176,22 @@ static int snd_usbmidi_submit_urb(struct urb* urb, int flags) | |||
167 | */ | 176 | */ |
168 | static int snd_usbmidi_urb_error(int status) | 177 | static int snd_usbmidi_urb_error(int status) |
169 | { | 178 | { |
170 | if (status == -ENOENT) | 179 | switch (status) { |
171 | return status; /* killed */ | 180 | /* manually unlinked, or device gone */ |
172 | if (status == -EILSEQ || | 181 | case -ENOENT: |
173 | status == -ECONNRESET || | 182 | case -ECONNRESET: |
174 | status == -ETIMEDOUT) | 183 | case -ESHUTDOWN: |
175 | return -ENODEV; /* device removed/shutdown */ | 184 | case -ENODEV: |
176 | snd_printk(KERN_ERR "urb status %d\n", status); | 185 | return -ENODEV; |
177 | return 0; /* continue */ | 186 | /* errors that might occur during unplugging */ |
187 | case -EPROTO: /* EHCI */ | ||
188 | case -ETIMEDOUT: /* OHCI */ | ||
189 | case -EILSEQ: /* UHCI */ | ||
190 | return -EIO; | ||
191 | default: | ||
192 | snd_printk(KERN_ERR "urb status %d\n", status); | ||
193 | return 0; /* continue */ | ||
194 | } | ||
178 | } | 195 | } |
179 | 196 | ||
180 | /* | 197 | /* |
@@ -218,8 +235,15 @@ static void snd_usbmidi_in_urb_complete(struct urb* urb, struct pt_regs *regs) | |||
218 | ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer, | 235 | ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer, |
219 | urb->actual_length); | 236 | urb->actual_length); |
220 | } else { | 237 | } else { |
221 | if (snd_usbmidi_urb_error(urb->status) < 0) | 238 | int err = snd_usbmidi_urb_error(urb->status); |
239 | if (err < 0) { | ||
240 | if (err != -ENODEV) { | ||
241 | ep->error_resubmit = 1; | ||
242 | mod_timer(&ep->umidi->error_timer, | ||
243 | jiffies + ERROR_DELAY_JIFFIES); | ||
244 | } | ||
222 | return; | 245 | return; |
246 | } | ||
223 | } | 247 | } |
224 | 248 | ||
225 | if (usb_pipe_needs_resubmit(urb->pipe)) { | 249 | if (usb_pipe_needs_resubmit(urb->pipe)) { |
@@ -236,8 +260,13 @@ static void snd_usbmidi_out_urb_complete(struct urb* urb, struct pt_regs *regs) | |||
236 | ep->urb_active = 0; | 260 | ep->urb_active = 0; |
237 | spin_unlock(&ep->buffer_lock); | 261 | spin_unlock(&ep->buffer_lock); |
238 | if (urb->status < 0) { | 262 | if (urb->status < 0) { |
239 | if (snd_usbmidi_urb_error(urb->status) < 0) | 263 | int err = snd_usbmidi_urb_error(urb->status); |
264 | if (err < 0) { | ||
265 | if (err != -ENODEV) | ||
266 | mod_timer(&ep->umidi->error_timer, | ||
267 | jiffies + ERROR_DELAY_JIFFIES); | ||
240 | return; | 268 | return; |
269 | } | ||
241 | } | 270 | } |
242 | snd_usbmidi_do_output(ep); | 271 | snd_usbmidi_do_output(ep); |
243 | } | 272 | } |
@@ -276,6 +305,24 @@ static void snd_usbmidi_out_tasklet(unsigned long data) | |||
276 | snd_usbmidi_do_output(ep); | 305 | snd_usbmidi_do_output(ep); |
277 | } | 306 | } |
278 | 307 | ||
308 | /* called after transfers had been interrupted due to some USB error */ | ||
309 | static void snd_usbmidi_error_timer(unsigned long data) | ||
310 | { | ||
311 | snd_usb_midi_t *umidi = (snd_usb_midi_t *)data; | ||
312 | int i; | ||
313 | |||
314 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | ||
315 | snd_usb_midi_in_endpoint_t *in = umidi->endpoints[i].in; | ||
316 | if (in && in->error_resubmit) { | ||
317 | in->error_resubmit = 0; | ||
318 | in->urb->dev = umidi->chip->dev; | ||
319 | snd_usbmidi_submit_urb(in->urb, GFP_ATOMIC); | ||
320 | } | ||
321 | if (umidi->endpoints[i].out) | ||
322 | snd_usbmidi_do_output(umidi->endpoints[i].out); | ||
323 | } | ||
324 | } | ||
325 | |||
279 | /* helper function to send static data that may not DMA-able */ | 326 | /* helper function to send static data that may not DMA-able */ |
280 | static int send_bulk_static_data(snd_usb_midi_out_endpoint_t* ep, | 327 | static int send_bulk_static_data(snd_usb_midi_out_endpoint_t* ep, |
281 | const void *data, int len) | 328 | const void *data, int len) |
@@ -594,17 +641,20 @@ static void snd_usbmidi_emagic_finish_out(snd_usb_midi_out_endpoint_t* ep) | |||
594 | static void snd_usbmidi_emagic_input(snd_usb_midi_in_endpoint_t* ep, | 641 | static void snd_usbmidi_emagic_input(snd_usb_midi_in_endpoint_t* ep, |
595 | uint8_t* buffer, int buffer_length) | 642 | uint8_t* buffer, int buffer_length) |
596 | { | 643 | { |
597 | /* ignore padding bytes at end of buffer */ | 644 | int i; |
598 | while (buffer_length > 0 && buffer[buffer_length - 1] == 0xff) | 645 | |
599 | --buffer_length; | 646 | /* FF indicates end of valid data */ |
647 | for (i = 0; i < buffer_length; ++i) | ||
648 | if (buffer[i] == 0xff) { | ||
649 | buffer_length = i; | ||
650 | break; | ||
651 | } | ||
600 | 652 | ||
601 | /* handle F5 at end of last buffer */ | 653 | /* handle F5 at end of last buffer */ |
602 | if (ep->seen_f5) | 654 | if (ep->seen_f5) |
603 | goto switch_port; | 655 | goto switch_port; |
604 | 656 | ||
605 | while (buffer_length > 0) { | 657 | while (buffer_length > 0) { |
606 | int i; | ||
607 | |||
608 | /* determine size of data until next F5 */ | 658 | /* determine size of data until next F5 */ |
609 | for (i = 0; i < buffer_length; ++i) | 659 | for (i = 0; i < buffer_length; ++i) |
610 | if (buffer[i] == 0xf5) | 660 | if (buffer[i] == 0xf5) |
@@ -671,6 +721,10 @@ static void snd_usbmidi_emagic_output(snd_usb_midi_out_endpoint_t* ep) | |||
671 | break; | 721 | break; |
672 | } | 722 | } |
673 | } | 723 | } |
724 | if (buf_free < ep->max_transfer && buf_free > 0) { | ||
725 | *buf = 0xff; | ||
726 | --buf_free; | ||
727 | } | ||
674 | ep->urb->transfer_buffer_length = ep->max_transfer - buf_free; | 728 | ep->urb->transfer_buffer_length = ep->max_transfer - buf_free; |
675 | } | 729 | } |
676 | 730 | ||
@@ -765,7 +819,10 @@ static snd_rawmidi_ops_t snd_usbmidi_input_ops = { | |||
765 | static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t* ep) | 819 | static void snd_usbmidi_in_endpoint_delete(snd_usb_midi_in_endpoint_t* ep) |
766 | { | 820 | { |
767 | if (ep->urb) { | 821 | if (ep->urb) { |
768 | kfree(ep->urb->transfer_buffer); | 822 | usb_buffer_free(ep->umidi->chip->dev, |
823 | ep->urb->transfer_buffer_length, | ||
824 | ep->urb->transfer_buffer, | ||
825 | ep->urb->transfer_dma); | ||
769 | usb_free_urb(ep->urb); | 826 | usb_free_urb(ep->urb); |
770 | } | 827 | } |
771 | kfree(ep); | 828 | kfree(ep); |
@@ -799,7 +856,8 @@ static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t* umidi, | |||
799 | else | 856 | else |
800 | pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep); | 857 | pipe = usb_rcvbulkpipe(umidi->chip->dev, ep_info->in_ep); |
801 | length = usb_maxpacket(umidi->chip->dev, pipe, 0); | 858 | length = usb_maxpacket(umidi->chip->dev, pipe, 0); |
802 | buffer = kmalloc(length, GFP_KERNEL); | 859 | buffer = usb_buffer_alloc(umidi->chip->dev, length, GFP_KERNEL, |
860 | &ep->urb->transfer_dma); | ||
803 | if (!buffer) { | 861 | if (!buffer) { |
804 | snd_usbmidi_in_endpoint_delete(ep); | 862 | snd_usbmidi_in_endpoint_delete(ep); |
805 | return -ENOMEM; | 863 | return -ENOMEM; |
@@ -812,6 +870,7 @@ static int snd_usbmidi_in_endpoint_create(snd_usb_midi_t* umidi, | |||
812 | usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, length, | 870 | usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, length, |
813 | snd_usb_complete_callback(snd_usbmidi_in_urb_complete), | 871 | snd_usb_complete_callback(snd_usbmidi_in_urb_complete), |
814 | ep); | 872 | ep); |
873 | ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; | ||
815 | 874 | ||
816 | rep->in = ep; | 875 | rep->in = ep; |
817 | return 0; | 876 | return 0; |
@@ -832,10 +891,10 @@ static unsigned int snd_usbmidi_count_bits(unsigned int x) | |||
832 | */ | 891 | */ |
833 | static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t* ep) | 892 | static void snd_usbmidi_out_endpoint_delete(snd_usb_midi_out_endpoint_t* ep) |
834 | { | 893 | { |
835 | if (ep->tasklet.func) | ||
836 | tasklet_kill(&ep->tasklet); | ||
837 | if (ep->urb) { | 894 | if (ep->urb) { |
838 | kfree(ep->urb->transfer_buffer); | 895 | usb_buffer_free(ep->umidi->chip->dev, ep->max_transfer, |
896 | ep->urb->transfer_buffer, | ||
897 | ep->urb->transfer_dma); | ||
839 | usb_free_urb(ep->urb); | 898 | usb_free_urb(ep->urb); |
840 | } | 899 | } |
841 | kfree(ep); | 900 | kfree(ep); |
@@ -867,7 +926,8 @@ static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t* umidi, | |||
867 | /* we never use interrupt output pipes */ | 926 | /* we never use interrupt output pipes */ |
868 | pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep); | 927 | pipe = usb_sndbulkpipe(umidi->chip->dev, ep_info->out_ep); |
869 | ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1); | 928 | ep->max_transfer = usb_maxpacket(umidi->chip->dev, pipe, 1); |
870 | buffer = kmalloc(ep->max_transfer, GFP_KERNEL); | 929 | buffer = usb_buffer_alloc(umidi->chip->dev, ep->max_transfer, |
930 | GFP_KERNEL, &ep->urb->transfer_dma); | ||
871 | if (!buffer) { | 931 | if (!buffer) { |
872 | snd_usbmidi_out_endpoint_delete(ep); | 932 | snd_usbmidi_out_endpoint_delete(ep); |
873 | return -ENOMEM; | 933 | return -ENOMEM; |
@@ -875,6 +935,7 @@ static int snd_usbmidi_out_endpoint_create(snd_usb_midi_t* umidi, | |||
875 | usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, | 935 | usb_fill_bulk_urb(ep->urb, umidi->chip->dev, pipe, buffer, |
876 | ep->max_transfer, | 936 | ep->max_transfer, |
877 | snd_usb_complete_callback(snd_usbmidi_out_urb_complete), ep); | 937 | snd_usb_complete_callback(snd_usbmidi_out_urb_complete), ep); |
938 | ep->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; | ||
878 | 939 | ||
879 | spin_lock_init(&ep->buffer_lock); | 940 | spin_lock_init(&ep->buffer_lock); |
880 | tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep); | 941 | tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep); |
@@ -918,8 +979,11 @@ void snd_usbmidi_disconnect(struct list_head* p) | |||
918 | int i; | 979 | int i; |
919 | 980 | ||
920 | umidi = list_entry(p, snd_usb_midi_t, list); | 981 | umidi = list_entry(p, snd_usb_midi_t, list); |
982 | del_timer_sync(&umidi->error_timer); | ||
921 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { | 983 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
922 | snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i]; | 984 | snd_usb_midi_endpoint_t* ep = &umidi->endpoints[i]; |
985 | if (ep->out) | ||
986 | tasklet_kill(&ep->out->tasklet); | ||
923 | if (ep->out && ep->out->urb) { | 987 | if (ep->out && ep->out->urb) { |
924 | usb_kill_urb(ep->out->urb); | 988 | usb_kill_urb(ep->out->urb); |
925 | if (umidi->usb_protocol_ops->finish_out_endpoint) | 989 | if (umidi->usb_protocol_ops->finish_out_endpoint) |
@@ -1480,6 +1544,9 @@ int snd_usb_create_midi_interface(snd_usb_audio_t* chip, | |||
1480 | umidi->iface = iface; | 1544 | umidi->iface = iface; |
1481 | umidi->quirk = quirk; | 1545 | umidi->quirk = quirk; |
1482 | umidi->usb_protocol_ops = &snd_usbmidi_standard_ops; | 1546 | umidi->usb_protocol_ops = &snd_usbmidi_standard_ops; |
1547 | init_timer(&umidi->error_timer); | ||
1548 | umidi->error_timer.function = snd_usbmidi_error_timer; | ||
1549 | umidi->error_timer.data = (unsigned long)umidi; | ||
1483 | 1550 | ||
1484 | /* detect the endpoint(s) to use */ | 1551 | /* detect the endpoint(s) to use */ |
1485 | memset(endpoints, 0, sizeof(endpoints)); | 1552 | memset(endpoints, 0, sizeof(endpoints)); |