midi.c (42249094f79422fbf5ed4b54eeb48ff096809b8f) midi.c (0ba41d917eeb87f608cf147f870ff2f4c1056bab)
1/*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
4 * Copyright (c) 2002-2009 Clemens Ladisch
5 * All rights reserved.
6 *
7 * Based on the OSS usb-midi driver by NAGANO Daisuke,
8 * NetBSD's umidi driver by Takuya SHIOZAKI,

--- 177 unchanged lines hidden (view full) ---

186
187/*
188 * Submits the URB, with error handling.
189 */
190static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
191{
192 int err = usb_submit_urb(urb, flags);
193 if (err < 0 && err != -ENODEV)
1/*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
4 * Copyright (c) 2002-2009 Clemens Ladisch
5 * All rights reserved.
6 *
7 * Based on the OSS usb-midi driver by NAGANO Daisuke,
8 * NetBSD's umidi driver by Takuya SHIOZAKI,

--- 177 unchanged lines hidden (view full) ---

186
187/*
188 * Submits the URB, with error handling.
189 */
190static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags)
191{
192 int err = usb_submit_urb(urb, flags);
193 if (err < 0 && err != -ENODEV)
194 snd_printk(KERN_ERR "usb_submit_urb: %d\n", err);
194 dev_err(&urb->dev->dev, "usb_submit_urb: %d\n", err);
195 return err;
196}
197
198/*
199 * Error handling for URB completion functions.
200 */
195 return err;
196}
197
198/*
199 * Error handling for URB completion functions.
200 */
201static int snd_usbmidi_urb_error(int status)
201static int snd_usbmidi_urb_error(const struct urb *urb)
202{
202{
203 switch (status) {
203 switch (urb->status) {
204 /* manually unlinked, or device gone */
205 case -ENOENT:
206 case -ECONNRESET:
207 case -ESHUTDOWN:
208 case -ENODEV:
209 return -ENODEV;
210 /* errors that might occur during unplugging */
211 case -EPROTO:
212 case -ETIME:
213 case -EILSEQ:
214 return -EIO;
215 default:
204 /* manually unlinked, or device gone */
205 case -ENOENT:
206 case -ECONNRESET:
207 case -ESHUTDOWN:
208 case -ENODEV:
209 return -ENODEV;
210 /* errors that might occur during unplugging */
211 case -EPROTO:
212 case -ETIME:
213 case -EILSEQ:
214 return -EIO;
215 default:
216 snd_printk(KERN_ERR "urb status %d\n", status);
216 dev_err(&urb->dev->dev, "urb status %d\n", urb->status);
217 return 0; /* continue */
218 }
219}
220
221/*
222 * Receives a chunk of MIDI data.
223 */
224static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
225 uint8_t* data, int length)
226{
227 struct usbmidi_in_port* port = &ep->ports[portidx];
228
229 if (!port->substream) {
217 return 0; /* continue */
218 }
219}
220
221/*
222 * Receives a chunk of MIDI data.
223 */
224static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx,
225 uint8_t* data, int length)
226{
227 struct usbmidi_in_port* port = &ep->ports[portidx];
228
229 if (!port->substream) {
230 snd_printd("unexpected port %d!\n", portidx);
230 dev_dbg(&ep->umidi->dev->dev, "unexpected port %d!\n", portidx);
231 return;
232 }
233 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
234 return;
235 snd_rawmidi_receive(port->substream, data, length);
236}
237
238#ifdef DUMP_PACKETS

--- 15 unchanged lines hidden (view full) ---

254{
255 struct snd_usb_midi_in_endpoint* ep = urb->context;
256
257 if (urb->status == 0) {
258 dump_urb("received", urb->transfer_buffer, urb->actual_length);
259 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
260 urb->actual_length);
261 } else {
231 return;
232 }
233 if (!test_bit(port->substream->number, &ep->umidi->input_triggered))
234 return;
235 snd_rawmidi_receive(port->substream, data, length);
236}
237
238#ifdef DUMP_PACKETS

--- 15 unchanged lines hidden (view full) ---

254{
255 struct snd_usb_midi_in_endpoint* ep = urb->context;
256
257 if (urb->status == 0) {
258 dump_urb("received", urb->transfer_buffer, urb->actual_length);
259 ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer,
260 urb->actual_length);
261 } else {
262 int err = snd_usbmidi_urb_error(urb->status);
262 int err = snd_usbmidi_urb_error(urb);
263 if (err < 0) {
264 if (err != -ENODEV) {
265 ep->error_resubmit = 1;
266 mod_timer(&ep->umidi->error_timer,
267 jiffies + ERROR_DELAY_JIFFIES);
268 }
269 return;
270 }

--- 13 unchanged lines hidden (view full) ---

284 urb_index = context - ep->urbs;
285 ep->active_urbs &= ~(1 << urb_index);
286 if (unlikely(ep->drain_urbs)) {
287 ep->drain_urbs &= ~(1 << urb_index);
288 wake_up(&ep->drain_wait);
289 }
290 spin_unlock(&ep->buffer_lock);
291 if (urb->status < 0) {
263 if (err < 0) {
264 if (err != -ENODEV) {
265 ep->error_resubmit = 1;
266 mod_timer(&ep->umidi->error_timer,
267 jiffies + ERROR_DELAY_JIFFIES);
268 }
269 return;
270 }

--- 13 unchanged lines hidden (view full) ---

284 urb_index = context - ep->urbs;
285 ep->active_urbs &= ~(1 << urb_index);
286 if (unlikely(ep->drain_urbs)) {
287 ep->drain_urbs &= ~(1 << urb_index);
288 wake_up(&ep->drain_wait);
289 }
290 spin_unlock(&ep->buffer_lock);
291 if (urb->status < 0) {
292 int err = snd_usbmidi_urb_error(urb->status);
292 int err = snd_usbmidi_urb_error(urb);
293 if (err < 0) {
294 if (err != -ENODEV)
295 mod_timer(&ep->umidi->error_timer,
296 jiffies + ERROR_DELAY_JIFFIES);
297 return;
298 }
299 }
300 snd_usbmidi_do_output(ep);

--- 1362 unchanged lines hidden (view full) ---

1663 int stream, int number,
1664 struct snd_rawmidi_substream ** rsubstream)
1665{
1666 struct port_info *port_info;
1667 const char *name_format;
1668
1669 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1670 if (!substream) {
293 if (err < 0) {
294 if (err != -ENODEV)
295 mod_timer(&ep->umidi->error_timer,
296 jiffies + ERROR_DELAY_JIFFIES);
297 return;
298 }
299 }
300 snd_usbmidi_do_output(ep);

--- 1362 unchanged lines hidden (view full) ---

1663 int stream, int number,
1664 struct snd_rawmidi_substream ** rsubstream)
1665{
1666 struct port_info *port_info;
1667 const char *name_format;
1668
1669 struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number);
1670 if (!substream) {
1671 snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number);
1671 dev_err(&umidi->dev->dev, "substream %d:%d not found\n", stream, number);
1672 return;
1673 }
1674
1675 /* TODO: read port name from jack descriptor */
1676 port_info = find_port_info(umidi, number);
1677 name_format = port_info ? port_info->name : "%s MIDI %d";
1678 snprintf(substream->name, sizeof(substream->name),
1679 name_format, umidi->card->shortname, number + 1);

--- 32 unchanged lines hidden (view full) ---

1712 }
1713 if (endpoints[i].in_cables & (1 << j)) {
1714 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1715 &umidi->endpoints[i].in->ports[j].substream);
1716 ++in_ports;
1717 }
1718 }
1719 }
1672 return;
1673 }
1674
1675 /* TODO: read port name from jack descriptor */
1676 port_info = find_port_info(umidi, number);
1677 name_format = port_info ? port_info->name : "%s MIDI %d";
1678 snprintf(substream->name, sizeof(substream->name),
1679 name_format, umidi->card->shortname, number + 1);

--- 32 unchanged lines hidden (view full) ---

1712 }
1713 if (endpoints[i].in_cables & (1 << j)) {
1714 snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports,
1715 &umidi->endpoints[i].in->ports[j].substream);
1716 ++in_ports;
1717 }
1718 }
1719 }
1720 snd_printdd(KERN_INFO "created %d output and %d input ports\n",
1720 dev_dbg(&umidi->dev->dev, "created %d output and %d input ports\n",
1721 out_ports, in_ports);
1722 return 0;
1723}
1724
1725/*
1726 * Returns MIDIStreaming device capabilities.
1727 */
1728static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,

--- 13 unchanged lines hidden (view full) ---

1742 return -ENXIO;
1743 hostif = &intf->altsetting[0];
1744 intfd = get_iface_desc(hostif);
1745 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1746 if (hostif->extralen >= 7 &&
1747 ms_header->bLength >= 7 &&
1748 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1749 ms_header->bDescriptorSubtype == UAC_HEADER)
1721 out_ports, in_ports);
1722 return 0;
1723}
1724
1725/*
1726 * Returns MIDIStreaming device capabilities.
1727 */
1728static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi,

--- 13 unchanged lines hidden (view full) ---

1742 return -ENXIO;
1743 hostif = &intf->altsetting[0];
1744 intfd = get_iface_desc(hostif);
1745 ms_header = (struct usb_ms_header_descriptor*)hostif->extra;
1746 if (hostif->extralen >= 7 &&
1747 ms_header->bLength >= 7 &&
1748 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
1749 ms_header->bDescriptorSubtype == UAC_HEADER)
1750 snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n",
1750 dev_dbg(&umidi->dev->dev, "MIDIStreaming version %02x.%02x\n",
1751 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1752 else
1751 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1752 else
1753 snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n");
1753 dev_warn(&umidi->dev->dev,
1754 "MIDIStreaming interface descriptor not found\n");
1754
1755 epidx = 0;
1756 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1757 hostep = &hostif->endpoint[i];
1758 ep = get_ep_desc(hostep);
1759 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
1760 continue;
1761 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1762 if (hostep->extralen < 4 ||
1763 ms_ep->bLength < 4 ||
1764 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1765 ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
1766 continue;
1767 if (usb_endpoint_dir_out(ep)) {
1768 if (endpoints[epidx].out_ep) {
1769 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1755
1756 epidx = 0;
1757 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1758 hostep = &hostif->endpoint[i];
1759 ep = get_ep_desc(hostep);
1760 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
1761 continue;
1762 ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra;
1763 if (hostep->extralen < 4 ||
1764 ms_ep->bLength < 4 ||
1765 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
1766 ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
1767 continue;
1768 if (usb_endpoint_dir_out(ep)) {
1769 if (endpoints[epidx].out_ep) {
1770 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1770 snd_printk(KERN_WARNING "too many endpoints\n");
1771 dev_warn(&umidi->dev->dev,
1772 "too many endpoints\n");
1771 break;
1772 }
1773 }
1774 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1775 if (usb_endpoint_xfer_int(ep))
1776 endpoints[epidx].out_interval = ep->bInterval;
1777 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1778 /*
1779 * Low speed bulk transfers don't exist, so
1780 * force interrupt transfers for devices like
1781 * ESI MIDI Mate that try to use them anyway.
1782 */
1783 endpoints[epidx].out_interval = 1;
1784 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1773 break;
1774 }
1775 }
1776 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1777 if (usb_endpoint_xfer_int(ep))
1778 endpoints[epidx].out_interval = ep->bInterval;
1779 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1780 /*
1781 * Low speed bulk transfers don't exist, so
1782 * force interrupt transfers for devices like
1783 * ESI MIDI Mate that try to use them anyway.
1784 */
1785 endpoints[epidx].out_interval = 1;
1786 endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1785 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1787 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
1786 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1787 } else {
1788 if (endpoints[epidx].in_ep) {
1789 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1788 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1789 } else {
1790 if (endpoints[epidx].in_ep) {
1791 if (++epidx >= MIDI_MAX_ENDPOINTS) {
1790 snd_printk(KERN_WARNING "too many endpoints\n");
1792 dev_warn(&umidi->dev->dev,
1793 "too many endpoints\n");
1791 break;
1792 }
1793 }
1794 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1795 if (usb_endpoint_xfer_int(ep))
1796 endpoints[epidx].in_interval = ep->bInterval;
1797 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1798 endpoints[epidx].in_interval = 1;
1799 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1794 break;
1795 }
1796 }
1797 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1798 if (usb_endpoint_xfer_int(ep))
1799 endpoints[epidx].in_interval = ep->bInterval;
1800 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
1801 endpoints[epidx].in_interval = 1;
1802 endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1;
1800 snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n",
1803 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
1801 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1802 }
1803 }
1804 return 0;
1805}
1806
1807static int roland_load_info(struct snd_kcontrol *kcontrol,
1808 struct snd_ctl_elem_info *info)

--- 51 unchanged lines hidden (view full) ---

1860
1861 hostif = &intf->altsetting[1];
1862 intfd = get_iface_desc(hostif);
1863 if (intfd->bNumEndpoints != 2 ||
1864 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1865 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1866 return;
1867
1804 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
1805 }
1806 }
1807 return 0;
1808}
1809
1810static int roland_load_info(struct snd_kcontrol *kcontrol,
1811 struct snd_ctl_elem_info *info)

--- 51 unchanged lines hidden (view full) ---

1863
1864 hostif = &intf->altsetting[1];
1865 intfd = get_iface_desc(hostif);
1866 if (intfd->bNumEndpoints != 2 ||
1867 (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1868 (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
1869 return;
1870
1868 snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n",
1871 dev_dbg(&umidi->dev->dev, "switching to altsetting %d with int ep\n",
1869 intfd->bAlternateSetting);
1870 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1871 intfd->bAlternateSetting);
1872
1873 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
1874 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
1875 umidi->roland_load_ctl = NULL;
1876}

--- 165 unchanged lines hidden (view full) ---

2042 * the descriptor array, like the driver for that other OS does.
2043 *
2044 * There is one interrupt input endpoint for all input ports, one
2045 * bulk output endpoint for even-numbered ports, and one for odd-
2046 * numbered ports. Both bulk output endpoints have corresponding
2047 * input bulk endpoints (at indices 1 and 3) which aren't used.
2048 */
2049 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
1872 intfd->bAlternateSetting);
1873 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
1874 intfd->bAlternateSetting);
1875
1876 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
1877 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
1878 umidi->roland_load_ctl = NULL;
1879}

--- 165 unchanged lines hidden (view full) ---

2045 * the descriptor array, like the driver for that other OS does.
2046 *
2047 * There is one interrupt input endpoint for all input ports, one
2048 * bulk output endpoint for even-numbered ports, and one for odd-
2049 * numbered ports. Both bulk output endpoints have corresponding
2050 * input bulk endpoints (at indices 1 and 3) which aren't used.
2051 */
2052 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
2050 snd_printdd(KERN_ERR "not enough endpoints\n");
2053 dev_dbg(&umidi->dev->dev, "not enough endpoints\n");
2051 return -ENOENT;
2052 }
2053
2054 epd = get_endpoint(hostif, 0);
2055 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
2054 return -ENOENT;
2055 }
2056
2057 epd = get_endpoint(hostif, 0);
2058 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
2056 snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n");
2059 dev_dbg(&umidi->dev->dev, "endpoint[0] isn't interrupt\n");
2057 return -ENXIO;
2058 }
2059 epd = get_endpoint(hostif, 2);
2060 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
2060 return -ENXIO;
2061 }
2062 epd = get_endpoint(hostif, 2);
2063 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
2061 snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n");
2064 dev_dbg(&umidi->dev->dev, "endpoint[2] isn't bulk output\n");
2062 return -ENXIO;
2063 }
2064 if (endpoint->out_cables > 0x0001) {
2065 epd = get_endpoint(hostif, 4);
2066 if (!usb_endpoint_dir_out(epd) ||
2067 !usb_endpoint_xfer_bulk(epd)) {
2065 return -ENXIO;
2066 }
2067 if (endpoint->out_cables > 0x0001) {
2068 epd = get_endpoint(hostif, 4);
2069 if (!usb_endpoint_dir_out(epd) ||
2070 !usb_endpoint_xfer_bulk(epd)) {
2068 snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n");
2071 dev_dbg(&umidi->dev->dev, "endpoint[4] isn't bulk output\n");
2069 return -ENXIO;
2070 }
2071 }
2072
2073 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2074 ep_info.out_interval = 0;
2075 ep_info.out_cables = endpoint->out_cables & 0x5555;
2076 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);

--- 207 unchanged lines hidden (view full) ---

2284 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2285 3, 0x40, 0x60, 0, NULL, 0, 1000);
2286 if (err < 0)
2287 break;
2288
2289 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2290 break;
2291 default:
2072 return -ENXIO;
2073 }
2074 }
2075
2076 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
2077 ep_info.out_interval = 0;
2078 ep_info.out_cables = endpoint->out_cables & 0x5555;
2079 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]);

--- 207 unchanged lines hidden (view full) ---

2287 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2288 3, 0x40, 0x60, 0, NULL, 0, 1000);
2289 if (err < 0)
2290 break;
2291
2292 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2293 break;
2294 default:
2292 snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
2295 dev_err(&umidi->dev->dev, "invalid quirk type %d\n", quirk->type);
2293 err = -ENXIO;
2294 break;
2295 }
2296 if (err < 0) {
2297 kfree(umidi);
2298 return err;
2299 }
2300

--- 29 unchanged lines hidden ---
2296 err = -ENXIO;
2297 break;
2298 }
2299 if (err < 0) {
2300 kfree(umidi);
2301 return err;
2302 }
2303

--- 29 unchanged lines hidden ---